]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/arm-tdep.c
Remove some unused functions from guile code
[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
LM
125/* Per-bfd data used for mapping symbols. */
126static 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 {
bd5766ec 611 arm_per_bfd *data = arm_bfd_data_key.get (sec->objfile->obfd);
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
LM
2409/* Per-BFD key to store exception handling information. */
2410static const struct 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. */
a2726d4f 2454 if (!objfile || arm_exidx_data_key.get (objfile->obfd) != NULL)
0e9e9abd
UW
2455 return;
2456
2457 /* Read contents of exception table and index. */
a5eda10c 2458 exidx = bfd_get_section_by_name (objfile->obfd, ELF_STRING_ARM_unwind);
984c7238 2459 gdb::byte_vector exidx_data;
0e9e9abd
UW
2460 if (exidx)
2461 {
fd361982
AM
2462 exidx_vma = bfd_section_vma (exidx);
2463 exidx_data.resize (bfd_section_size (exidx));
0e9e9abd
UW
2464
2465 if (!bfd_get_section_contents (objfile->obfd, exidx,
984c7238
TT
2466 exidx_data.data (), 0,
2467 exidx_data.size ()))
2468 return;
0e9e9abd
UW
2469 }
2470
2471 extab = bfd_get_section_by_name (objfile->obfd, ".ARM.extab");
984c7238 2472 gdb::byte_vector extab_data;
0e9e9abd
UW
2473 if (extab)
2474 {
fd361982
AM
2475 extab_vma = bfd_section_vma (extab);
2476 extab_data.resize (bfd_section_size (extab));
0e9e9abd
UW
2477
2478 if (!bfd_get_section_contents (objfile->obfd, extab,
984c7238
TT
2479 extab_data.data (), 0,
2480 extab_data.size ()))
2481 return;
0e9e9abd
UW
2482 }
2483
2484 /* Allocate exception table data structure. */
a2726d4f 2485 data = arm_exidx_data_key.emplace (objfile->obfd);
7a5d944b 2486 data->section_maps.resize (objfile->obfd->section_count);
0e9e9abd
UW
2487
2488 /* Fill in exception table. */
984c7238 2489 for (i = 0; i < exidx_data.size () / 8; i++)
0e9e9abd
UW
2490 {
2491 struct arm_exidx_entry new_exidx_entry;
984c7238
TT
2492 bfd_vma idx = bfd_h_get_32 (objfile->obfd, exidx_data.data () + i * 8);
2493 bfd_vma val = bfd_h_get_32 (objfile->obfd,
2494 exidx_data.data () + i * 8 + 4);
0e9e9abd
UW
2495 bfd_vma addr = 0, word = 0;
2496 int n_bytes = 0, n_words = 0;
2497 struct obj_section *sec;
2498 gdb_byte *entry = NULL;
2499
2500 /* Extract address of start of function. */
2501 idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2502 idx += exidx_vma + i * 8;
2503
2504 /* Find section containing function and compute section offset. */
2505 sec = arm_obj_section_from_vma (objfile, idx);
2506 if (sec == NULL)
2507 continue;
fd361982 2508 idx -= bfd_section_vma (sec->the_bfd_section);
0e9e9abd
UW
2509
2510 /* Determine address of exception table entry. */
2511 if (val == 1)
2512 {
2513 /* EXIDX_CANTUNWIND -- no exception table entry present. */
2514 }
2515 else if ((val & 0xff000000) == 0x80000000)
2516 {
2517 /* Exception table entry embedded in .ARM.exidx
2518 -- must be short form. */
2519 word = val;
2520 n_bytes = 3;
2521 }
2522 else if (!(val & 0x80000000))
2523 {
2524 /* Exception table entry in .ARM.extab. */
2525 addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2526 addr += exidx_vma + i * 8 + 4;
2527
984c7238 2528 if (addr >= extab_vma && addr + 4 <= extab_vma + extab_data.size ())
0e9e9abd
UW
2529 {
2530 word = bfd_h_get_32 (objfile->obfd,
984c7238 2531 extab_data.data () + addr - extab_vma);
0e9e9abd
UW
2532 addr += 4;
2533
2534 if ((word & 0xff000000) == 0x80000000)
2535 {
2536 /* Short form. */
2537 n_bytes = 3;
2538 }
2539 else if ((word & 0xff000000) == 0x81000000
2540 || (word & 0xff000000) == 0x82000000)
2541 {
2542 /* Long form. */
2543 n_bytes = 2;
2544 n_words = ((word >> 16) & 0xff);
2545 }
2546 else if (!(word & 0x80000000))
2547 {
2548 bfd_vma pers;
2549 struct obj_section *pers_sec;
2550 int gnu_personality = 0;
2551
2552 /* Custom personality routine. */
2553 pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2554 pers = UNMAKE_THUMB_ADDR (pers + addr - 4);
2555
2556 /* Check whether we've got one of the variants of the
2557 GNU personality routines. */
2558 pers_sec = arm_obj_section_from_vma (objfile, pers);
2559 if (pers_sec)
2560 {
2561 static const char *personality[] =
2562 {
2563 "__gcc_personality_v0",
2564 "__gxx_personality_v0",
2565 "__gcj_personality_v0",
2566 "__gnu_objc_personality_v0",
2567 NULL
2568 };
2569
0c1bcd23 2570 CORE_ADDR pc = pers + pers_sec->offset ();
0e9e9abd
UW
2571 int k;
2572
2573 for (k = 0; personality[k]; k++)
2574 if (lookup_minimal_symbol_by_pc_name
2575 (pc, personality[k], objfile))
2576 {
2577 gnu_personality = 1;
2578 break;
2579 }
2580 }
2581
2582 /* If so, the next word contains a word count in the high
2583 byte, followed by the same unwind instructions as the
2584 pre-defined forms. */
2585 if (gnu_personality
984c7238 2586 && addr + 4 <= extab_vma + extab_data.size ())
0e9e9abd
UW
2587 {
2588 word = bfd_h_get_32 (objfile->obfd,
984c7238
TT
2589 (extab_data.data ()
2590 + addr - extab_vma));
0e9e9abd
UW
2591 addr += 4;
2592 n_bytes = 3;
2593 n_words = ((word >> 24) & 0xff);
2594 }
2595 }
2596 }
2597 }
2598
2599 /* Sanity check address. */
2600 if (n_words)
984c7238
TT
2601 if (addr < extab_vma
2602 || addr + 4 * n_words > extab_vma + extab_data.size ())
0e9e9abd
UW
2603 n_words = n_bytes = 0;
2604
2605 /* The unwind instructions reside in WORD (only the N_BYTES least
2606 significant bytes are valid), followed by N_WORDS words in the
2607 extab section starting at ADDR. */
2608 if (n_bytes || n_words)
2609 {
224c3ddb
SM
2610 gdb_byte *p = entry
2611 = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack,
2612 n_bytes + n_words * 4 + 1);
0e9e9abd
UW
2613
2614 while (n_bytes--)
2615 *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);
2616
2617 while (n_words--)
2618 {
2619 word = bfd_h_get_32 (objfile->obfd,
984c7238 2620 extab_data.data () + addr - extab_vma);
0e9e9abd
UW
2621 addr += 4;
2622
2623 *p++ = (gdb_byte) ((word >> 24) & 0xff);
2624 *p++ = (gdb_byte) ((word >> 16) & 0xff);
2625 *p++ = (gdb_byte) ((word >> 8) & 0xff);
2626 *p++ = (gdb_byte) (word & 0xff);
2627 }
2628
2629 /* Implied "Finish" to terminate the list. */
2630 *p++ = 0xb0;
2631 }
2632
2633 /* Push entry onto vector. They are guaranteed to always
2634 appear in order of increasing addresses. */
2635 new_exidx_entry.addr = idx;
2636 new_exidx_entry.entry = entry;
7a5d944b
TT
2637 data->section_maps[sec->the_bfd_section->index].push_back
2638 (new_exidx_entry);
0e9e9abd 2639 }
0e9e9abd
UW
2640}
2641
2642/* Search for the exception table entry covering MEMADDR. If one is found,
2643 return a pointer to its data. Otherwise, return 0. If START is non-NULL,
2644 set *START to the start of the region covered by this entry. */
2645
2646static gdb_byte *
2647arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
2648{
2649 struct obj_section *sec;
2650
2651 sec = find_pc_section (memaddr);
2652 if (sec != NULL)
2653 {
2654 struct arm_exidx_data *data;
0c1bcd23 2655 struct arm_exidx_entry map_key = { memaddr - sec->addr (), 0 };
0e9e9abd 2656
a2726d4f 2657 data = arm_exidx_data_key.get (sec->objfile->obfd);
0e9e9abd
UW
2658 if (data != NULL)
2659 {
7a5d944b
TT
2660 std::vector<arm_exidx_entry> &map
2661 = data->section_maps[sec->the_bfd_section->index];
2662 if (!map.empty ())
0e9e9abd 2663 {
7a5d944b 2664 auto idx = std::lower_bound (map.begin (), map.end (), map_key);
0e9e9abd 2665
7a5d944b 2666 /* std::lower_bound finds the earliest ordered insertion
0e9e9abd
UW
2667 point. If the following symbol starts at this exact
2668 address, we use that; otherwise, the preceding
2669 exception table entry covers this address. */
7a5d944b 2670 if (idx < map.end ())
0e9e9abd 2671 {
7a5d944b 2672 if (idx->addr == map_key.addr)
0e9e9abd
UW
2673 {
2674 if (start)
0c1bcd23 2675 *start = idx->addr + sec->addr ();
7a5d944b 2676 return idx->entry;
0e9e9abd
UW
2677 }
2678 }
2679
7a5d944b 2680 if (idx > map.begin ())
0e9e9abd 2681 {
7a5d944b 2682 idx = idx - 1;
0e9e9abd 2683 if (start)
0c1bcd23 2684 *start = idx->addr + sec->addr ();
7a5d944b 2685 return idx->entry;
0e9e9abd
UW
2686 }
2687 }
2688 }
2689 }
2690
2691 return NULL;
2692}
2693
2694/* Given the current frame THIS_FRAME, and its associated frame unwinding
2695 instruction list from the ARM exception table entry ENTRY, allocate and
2696 return a prologue cache structure describing how to unwind this frame.
2697
2698 Return NULL if the unwinding instruction list contains a "spare",
2699 "reserved" or "refuse to unwind" instruction as defined in section
2700 "9.3 Frame unwinding instructions" of the "Exception Handling ABI
2701 for the ARM Architecture" document. */
2702
2703static struct arm_prologue_cache *
2704arm_exidx_fill_cache (struct frame_info *this_frame, gdb_byte *entry)
2705{
2706 CORE_ADDR vsp = 0;
2707 int vsp_valid = 0;
2708
2709 struct arm_prologue_cache *cache;
2710 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 2711 arm_cache_init (cache, this_frame);
0e9e9abd
UW
2712
2713 for (;;)
2714 {
2715 gdb_byte insn;
2716
2717 /* Whenever we reload SP, we actually have to retrieve its
2718 actual value in the current frame. */
2719 if (!vsp_valid)
2720 {
a9a87d35 2721 if (cache->saved_regs[ARM_SP_REGNUM].is_realreg ())
0e9e9abd 2722 {
098caef4 2723 int reg = cache->saved_regs[ARM_SP_REGNUM].realreg ();
0e9e9abd
UW
2724 vsp = get_frame_register_unsigned (this_frame, reg);
2725 }
2726 else
2727 {
098caef4 2728 CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr ();
0e9e9abd
UW
2729 vsp = get_frame_memory_unsigned (this_frame, addr, 4);
2730 }
2731
2732 vsp_valid = 1;
2733 }
2734
2735 /* Decode next unwind instruction. */
2736 insn = *entry++;
2737
2738 if ((insn & 0xc0) == 0)
2739 {
2740 int offset = insn & 0x3f;
2741 vsp += (offset << 2) + 4;
2742 }
2743 else if ((insn & 0xc0) == 0x40)
2744 {
2745 int offset = insn & 0x3f;
2746 vsp -= (offset << 2) + 4;
2747 }
2748 else if ((insn & 0xf0) == 0x80)
2749 {
2750 int mask = ((insn & 0xf) << 8) | *entry++;
2751 int i;
2752
2753 /* The special case of an all-zero mask identifies
2754 "Refuse to unwind". We return NULL to fall back
2755 to the prologue analyzer. */
2756 if (mask == 0)
2757 return NULL;
2758
2759 /* Pop registers r4..r15 under mask. */
2760 for (i = 0; i < 12; i++)
2761 if (mask & (1 << i))
2762 {
098caef4 2763 cache->saved_regs[4 + i].set_addr (vsp);
0e9e9abd
UW
2764 vsp += 4;
2765 }
2766
2767 /* Special-case popping SP -- we need to reload vsp. */
2768 if (mask & (1 << (ARM_SP_REGNUM - 4)))
2769 vsp_valid = 0;
2770 }
2771 else if ((insn & 0xf0) == 0x90)
2772 {
2773 int reg = insn & 0xf;
2774
2775 /* Reserved cases. */
2776 if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
2777 return NULL;
2778
2779 /* Set SP from another register and mark VSP for reload. */
2780 cache->saved_regs[ARM_SP_REGNUM] = cache->saved_regs[reg];
2781 vsp_valid = 0;
2782 }
2783 else if ((insn & 0xf0) == 0xa0)
2784 {
2785 int count = insn & 0x7;
2786 int pop_lr = (insn & 0x8) != 0;
2787 int i;
2788
2789 /* Pop r4..r[4+count]. */
2790 for (i = 0; i <= count; i++)
2791 {
098caef4 2792 cache->saved_regs[4 + i].set_addr (vsp);
0e9e9abd
UW
2793 vsp += 4;
2794 }
2795
2796 /* If indicated by flag, pop LR as well. */
2797 if (pop_lr)
2798 {
098caef4 2799 cache->saved_regs[ARM_LR_REGNUM].set_addr (vsp);
0e9e9abd
UW
2800 vsp += 4;
2801 }
2802 }
2803 else if (insn == 0xb0)
2804 {
2805 /* We could only have updated PC by popping into it; if so, it
2806 will show up as address. Otherwise, copy LR into PC. */
a9a87d35 2807 if (!cache->saved_regs[ARM_PC_REGNUM].is_addr ())
0e9e9abd
UW
2808 cache->saved_regs[ARM_PC_REGNUM]
2809 = cache->saved_regs[ARM_LR_REGNUM];
2810
2811 /* We're done. */
2812 break;
2813 }
2814 else if (insn == 0xb1)
2815 {
2816 int mask = *entry++;
2817 int i;
2818
2819 /* All-zero mask and mask >= 16 is "spare". */
2820 if (mask == 0 || mask >= 16)
2821 return NULL;
2822
2823 /* Pop r0..r3 under mask. */
2824 for (i = 0; i < 4; i++)
2825 if (mask & (1 << i))
2826 {
098caef4 2827 cache->saved_regs[i].set_addr (vsp);
0e9e9abd
UW
2828 vsp += 4;
2829 }
2830 }
2831 else if (insn == 0xb2)
2832 {
2833 ULONGEST offset = 0;
2834 unsigned shift = 0;
2835
2836 do
2837 {
2838 offset |= (*entry & 0x7f) << shift;
2839 shift += 7;
2840 }
2841 while (*entry++ & 0x80);
2842
2843 vsp += 0x204 + (offset << 2);
2844 }
2845 else if (insn == 0xb3)
2846 {
2847 int start = *entry >> 4;
2848 int count = (*entry++) & 0xf;
2849 int i;
2850
2851 /* Only registers D0..D15 are valid here. */
2852 if (start + count >= 16)
2853 return NULL;
2854
2855 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2856 for (i = 0; i <= count; i++)
2857 {
098caef4 2858 cache->saved_regs[ARM_D0_REGNUM + start + i].set_addr (vsp);
0e9e9abd
UW
2859 vsp += 8;
2860 }
2861
2862 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2863 vsp += 4;
2864 }
2865 else if ((insn & 0xf8) == 0xb8)
2866 {
2867 int count = insn & 0x7;
2868 int i;
2869
2870 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2871 for (i = 0; i <= count; i++)
2872 {
098caef4 2873 cache->saved_regs[ARM_D0_REGNUM + 8 + i].set_addr (vsp);
0e9e9abd
UW
2874 vsp += 8;
2875 }
2876
2877 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2878 vsp += 4;
2879 }
2880 else if (insn == 0xc6)
2881 {
2882 int start = *entry >> 4;
2883 int count = (*entry++) & 0xf;
2884 int i;
2885
2886 /* Only registers WR0..WR15 are valid. */
2887 if (start + count >= 16)
2888 return NULL;
2889
2890 /* Pop iwmmx registers WR[start]..WR[start+count]. */
2891 for (i = 0; i <= count; i++)
2892 {
098caef4 2893 cache->saved_regs[ARM_WR0_REGNUM + start + i].set_addr (vsp);
0e9e9abd
UW
2894 vsp += 8;
2895 }
2896 }
2897 else if (insn == 0xc7)
2898 {
2899 int mask = *entry++;
2900 int i;
2901
2902 /* All-zero mask and mask >= 16 is "spare". */
2903 if (mask == 0 || mask >= 16)
2904 return NULL;
2905
2906 /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask. */
2907 for (i = 0; i < 4; i++)
2908 if (mask & (1 << i))
2909 {
098caef4 2910 cache->saved_regs[ARM_WCGR0_REGNUM + i].set_addr (vsp);
0e9e9abd
UW
2911 vsp += 4;
2912 }
2913 }
2914 else if ((insn & 0xf8) == 0xc0)
2915 {
2916 int count = insn & 0x7;
2917 int i;
2918
2919 /* Pop iwmmx registers WR[10]..WR[10+count]. */
2920 for (i = 0; i <= count; i++)
2921 {
098caef4 2922 cache->saved_regs[ARM_WR0_REGNUM + 10 + i].set_addr (vsp);
0e9e9abd
UW
2923 vsp += 8;
2924 }
2925 }
2926 else if (insn == 0xc8)
2927 {
2928 int start = *entry >> 4;
2929 int count = (*entry++) & 0xf;
2930 int i;
2931
2932 /* Only registers D0..D31 are valid. */
2933 if (start + count >= 16)
2934 return NULL;
2935
2936 /* Pop VFP double-precision registers
2937 D[16+start]..D[16+start+count]. */
2938 for (i = 0; i <= count; i++)
2939 {
098caef4 2940 cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].set_addr (vsp);
0e9e9abd
UW
2941 vsp += 8;
2942 }
2943 }
2944 else if (insn == 0xc9)
2945 {
2946 int start = *entry >> 4;
2947 int count = (*entry++) & 0xf;
2948 int i;
2949
2950 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2951 for (i = 0; i <= count; i++)
2952 {
098caef4 2953 cache->saved_regs[ARM_D0_REGNUM + start + i].set_addr (vsp);
0e9e9abd
UW
2954 vsp += 8;
2955 }
2956 }
2957 else if ((insn & 0xf8) == 0xd0)
2958 {
2959 int count = insn & 0x7;
2960 int i;
2961
2962 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2963 for (i = 0; i <= count; i++)
2964 {
098caef4 2965 cache->saved_regs[ARM_D0_REGNUM + 8 + i].set_addr (vsp);
0e9e9abd
UW
2966 vsp += 8;
2967 }
2968 }
2969 else
2970 {
2971 /* Everything else is "spare". */
2972 return NULL;
2973 }
2974 }
2975
2976 /* If we restore SP from a register, assume this was the frame register.
2977 Otherwise just fall back to SP as frame register. */
a9a87d35 2978 if (cache->saved_regs[ARM_SP_REGNUM].is_realreg ())
098caef4 2979 cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg ();
0e9e9abd
UW
2980 else
2981 cache->framereg = ARM_SP_REGNUM;
2982
2983 /* Determine offset to previous frame. */
2984 cache->framesize
2985 = vsp - get_frame_register_unsigned (this_frame, cache->framereg);
2986
2987 /* We already got the previous SP. */
ae7e2f45 2988 arm_gdbarch_tdep *tdep
08106042 2989 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45 2990 arm_cache_set_active_sp_value (cache, tdep, vsp);
0e9e9abd
UW
2991
2992 return cache;
2993}
2994
2995/* Unwinding via ARM exception table entries. Note that the sniffer
2996 already computes a filled-in prologue cache, which is then used
2997 with the same arm_prologue_this_id and arm_prologue_prev_register
2998 routines also used for prologue-parsing based unwinding. */
2999
3000static int
3001arm_exidx_unwind_sniffer (const struct frame_unwind *self,
3002 struct frame_info *this_frame,
3003 void **this_prologue_cache)
3004{
3005 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3006 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3007 CORE_ADDR addr_in_block, exidx_region, func_start;
3008 struct arm_prologue_cache *cache;
3009 gdb_byte *entry;
3010
3011 /* See if we have an ARM exception table entry covering this address. */
3012 addr_in_block = get_frame_address_in_block (this_frame);
3013 entry = arm_find_exidx_entry (addr_in_block, &exidx_region);
3014 if (!entry)
3015 return 0;
3016
3017 /* The ARM exception table does not describe unwind information
3018 for arbitrary PC values, but is guaranteed to be correct only
3019 at call sites. We have to decide here whether we want to use
3020 ARM exception table information for this frame, or fall back
3021 to using prologue parsing. (Note that if we have DWARF CFI,
3022 this sniffer isn't even called -- CFI is always preferred.)
3023
3024 Before we make this decision, however, we check whether we
3025 actually have *symbol* information for the current frame.
3026 If not, prologue parsing would not work anyway, so we might
3027 as well use the exception table and hope for the best. */
3028 if (find_pc_partial_function (addr_in_block, NULL, &func_start, NULL))
3029 {
3030 int exc_valid = 0;
3031
3032 /* If the next frame is "normal", we are at a call site in this
3033 frame, so exception information is guaranteed to be valid. */
3034 if (get_next_frame (this_frame)
3035 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
3036 exc_valid = 1;
3037
3038 /* We also assume exception information is valid if we're currently
3039 blocked in a system call. The system library is supposed to
d9311bfa
AT
3040 ensure this, so that e.g. pthread cancellation works. */
3041 if (arm_frame_is_thumb (this_frame))
0e9e9abd 3042 {
7913a64c 3043 ULONGEST insn;
416dc9c6 3044
7913a64c
YQ
3045 if (safe_read_memory_unsigned_integer (get_frame_pc (this_frame) - 2,
3046 2, byte_order_for_code, &insn)
d9311bfa
AT
3047 && (insn & 0xff00) == 0xdf00 /* svc */)
3048 exc_valid = 1;
0e9e9abd 3049 }
d9311bfa
AT
3050 else
3051 {
7913a64c 3052 ULONGEST insn;
416dc9c6 3053
7913a64c
YQ
3054 if (safe_read_memory_unsigned_integer (get_frame_pc (this_frame) - 4,
3055 4, byte_order_for_code, &insn)
d9311bfa
AT
3056 && (insn & 0x0f000000) == 0x0f000000 /* svc */)
3057 exc_valid = 1;
3058 }
3059
0e9e9abd
UW
3060 /* Bail out if we don't know that exception information is valid. */
3061 if (!exc_valid)
3062 return 0;
3063
3064 /* The ARM exception index does not mark the *end* of the region
3065 covered by the entry, and some functions will not have any entry.
3066 To correctly recognize the end of the covered region, the linker
3067 should have inserted dummy records with a CANTUNWIND marker.
3068
3069 Unfortunately, current versions of GNU ld do not reliably do
3070 this, and thus we may have found an incorrect entry above.
3071 As a (temporary) sanity check, we only use the entry if it
3072 lies *within* the bounds of the function. Note that this check
3073 might reject perfectly valid entries that just happen to cover
3074 multiple functions; therefore this check ought to be removed
3075 once the linker is fixed. */
3076 if (func_start > exidx_region)
3077 return 0;
3078 }
3079
3080 /* Decode the list of unwinding instructions into a prologue cache.
3081 Note that this may fail due to e.g. a "refuse to unwind" code. */
3082 cache = arm_exidx_fill_cache (this_frame, entry);
3083 if (!cache)
3084 return 0;
3085
3086 *this_prologue_cache = cache;
3087 return 1;
3088}
3089
3090struct frame_unwind arm_exidx_unwind = {
a154d838 3091 "arm exidx",
0e9e9abd 3092 NORMAL_FRAME,
8fbca658 3093 default_frame_unwind_stop_reason,
0e9e9abd
UW
3094 arm_prologue_this_id,
3095 arm_prologue_prev_register,
3096 NULL,
3097 arm_exidx_unwind_sniffer
3098};
3099
779aa56f
YQ
3100static struct arm_prologue_cache *
3101arm_make_epilogue_frame_cache (struct frame_info *this_frame)
3102{
3103 struct arm_prologue_cache *cache;
779aa56f
YQ
3104 int reg;
3105
3106 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 3107 arm_cache_init (cache, this_frame);
779aa56f
YQ
3108
3109 /* Still rely on the offset calculated from prologue. */
3110 arm_scan_prologue (this_frame, cache);
3111
3112 /* Since we are in epilogue, the SP has been restored. */
ae7e2f45 3113 arm_gdbarch_tdep *tdep
08106042 3114 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45
CL
3115 arm_cache_set_active_sp_value (cache, tdep,
3116 get_frame_register_unsigned (this_frame,
3117 ARM_SP_REGNUM));
779aa56f
YQ
3118
3119 /* Calculate actual addresses of saved registers using offsets
3120 determined by arm_scan_prologue. */
3121 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
a9a87d35 3122 if (cache->saved_regs[reg].is_addr ())
098caef4 3123 cache->saved_regs[reg].set_addr (cache->saved_regs[reg].addr ()
ae7e2f45 3124 + arm_cache_get_prev_sp_value (cache, tdep));
779aa56f
YQ
3125
3126 return cache;
3127}
3128
3129/* Implementation of function hook 'this_id' in
3130 'struct frame_uwnind' for epilogue unwinder. */
3131
3132static void
3133arm_epilogue_frame_this_id (struct frame_info *this_frame,
3134 void **this_cache,
3135 struct frame_id *this_id)
3136{
3137 struct arm_prologue_cache *cache;
3138 CORE_ADDR pc, func;
3139
3140 if (*this_cache == NULL)
3141 *this_cache = arm_make_epilogue_frame_cache (this_frame);
3142 cache = (struct arm_prologue_cache *) *this_cache;
3143
3144 /* Use function start address as part of the frame ID. If we cannot
3145 identify the start address (due to missing symbol information),
3146 fall back to just using the current PC. */
3147 pc = get_frame_pc (this_frame);
3148 func = get_frame_func (this_frame);
fb3f3d25 3149 if (func == 0)
779aa56f
YQ
3150 func = pc;
3151
ae7e2f45 3152 arm_gdbarch_tdep *tdep
08106042 3153 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45 3154 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep), pc);
779aa56f
YQ
3155}
3156
3157/* Implementation of function hook 'prev_register' in
3158 'struct frame_uwnind' for epilogue unwinder. */
3159
3160static struct value *
3161arm_epilogue_frame_prev_register (struct frame_info *this_frame,
3162 void **this_cache, int regnum)
3163{
779aa56f
YQ
3164 if (*this_cache == NULL)
3165 *this_cache = arm_make_epilogue_frame_cache (this_frame);
779aa56f
YQ
3166
3167 return arm_prologue_prev_register (this_frame, this_cache, regnum);
3168}
3169
3170static int arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch,
3171 CORE_ADDR pc);
3172static int thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch,
3173 CORE_ADDR pc);
3174
3175/* Implementation of function hook 'sniffer' in
3176 'struct frame_uwnind' for epilogue unwinder. */
3177
3178static int
3179arm_epilogue_frame_sniffer (const struct frame_unwind *self,
3180 struct frame_info *this_frame,
3181 void **this_prologue_cache)
3182{
3183 if (frame_relative_level (this_frame) == 0)
3184 {
3185 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3186 CORE_ADDR pc = get_frame_pc (this_frame);
3187
3188 if (arm_frame_is_thumb (this_frame))
3189 return thumb_stack_frame_destroyed_p (gdbarch, pc);
3190 else
3191 return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
3192 }
3193 else
3194 return 0;
3195}
3196
3197/* Frame unwinder from epilogue. */
3198
3199static const struct frame_unwind arm_epilogue_frame_unwind =
3200{
a154d838 3201 "arm epilogue",
779aa56f
YQ
3202 NORMAL_FRAME,
3203 default_frame_unwind_stop_reason,
3204 arm_epilogue_frame_this_id,
3205 arm_epilogue_frame_prev_register,
3206 NULL,
3207 arm_epilogue_frame_sniffer,
3208};
3209
80d8d390
YQ
3210/* Recognize GCC's trampoline for thumb call-indirect. If we are in a
3211 trampoline, return the target PC. Otherwise return 0.
3212
3213 void call0a (char c, short s, int i, long l) {}
3214
3215 int main (void)
3216 {
3217 (*pointer_to_call0a) (c, s, i, l);
3218 }
3219
3220 Instead of calling a stub library function _call_via_xx (xx is
3221 the register name), GCC may inline the trampoline in the object
3222 file as below (register r2 has the address of call0a).
3223
3224 .global main
3225 .type main, %function
3226 ...
3227 bl .L1
3228 ...
3229 .size main, .-main
3230
3231 .L1:
3232 bx r2
3233
3234 The trampoline 'bx r2' doesn't belong to main. */
3235
3236static CORE_ADDR
3237arm_skip_bx_reg (struct frame_info *frame, CORE_ADDR pc)
3238{
3239 /* The heuristics of recognizing such trampoline is that FRAME is
3240 executing in Thumb mode and the instruction on PC is 'bx Rm'. */
3241 if (arm_frame_is_thumb (frame))
3242 {
3243 gdb_byte buf[2];
3244
3245 if (target_read_memory (pc, buf, 2) == 0)
3246 {
3247 struct gdbarch *gdbarch = get_frame_arch (frame);
3248 enum bfd_endian byte_order_for_code
3249 = gdbarch_byte_order_for_code (gdbarch);
3250 uint16_t insn
3251 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3252
3253 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
3254 {
3255 CORE_ADDR dest
3256 = get_frame_register_unsigned (frame, bits (insn, 3, 6));
3257
3258 /* Clear the LSB so that gdb core sets step-resume
3259 breakpoint at the right address. */
3260 return UNMAKE_THUMB_ADDR (dest);
3261 }
3262 }
3263 }
3264
3265 return 0;
3266}
3267
909cf6ea 3268static struct arm_prologue_cache *
a262aec2 3269arm_make_stub_cache (struct frame_info *this_frame)
909cf6ea 3270{
909cf6ea 3271 struct arm_prologue_cache *cache;
909cf6ea 3272
35d5d4ee 3273 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 3274 arm_cache_init (cache, this_frame);
909cf6ea 3275
ae7e2f45 3276 arm_gdbarch_tdep *tdep
08106042 3277 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45
CL
3278 arm_cache_set_active_sp_value (cache, tdep,
3279 get_frame_register_unsigned (this_frame,
3280 ARM_SP_REGNUM));
909cf6ea
DJ
3281
3282 return cache;
3283}
3284
3285/* Our frame ID for a stub frame is the current SP and LR. */
3286
3287static void
a262aec2 3288arm_stub_this_id (struct frame_info *this_frame,
909cf6ea
DJ
3289 void **this_cache,
3290 struct frame_id *this_id)
3291{
3292 struct arm_prologue_cache *cache;
3293
3294 if (*this_cache == NULL)
a262aec2 3295 *this_cache = arm_make_stub_cache (this_frame);
9a3c8263 3296 cache = (struct arm_prologue_cache *) *this_cache;
909cf6ea 3297
ae7e2f45 3298 arm_gdbarch_tdep *tdep
08106042 3299 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45
CL
3300 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep),
3301 get_frame_pc (this_frame));
909cf6ea
DJ
3302}
3303
a262aec2
DJ
3304static int
3305arm_stub_unwind_sniffer (const struct frame_unwind *self,
3306 struct frame_info *this_frame,
3307 void **this_prologue_cache)
909cf6ea 3308{
93d42b30 3309 CORE_ADDR addr_in_block;
948f8e3d 3310 gdb_byte dummy[4];
18d18ac8
YQ
3311 CORE_ADDR pc, start_addr;
3312 const char *name;
909cf6ea 3313
a262aec2 3314 addr_in_block = get_frame_address_in_block (this_frame);
18d18ac8 3315 pc = get_frame_pc (this_frame);
3e5d3a5a 3316 if (in_plt_section (addr_in_block)
fc36e839
DE
3317 /* We also use the stub winder if the target memory is unreadable
3318 to avoid having the prologue unwinder trying to read it. */
18d18ac8
YQ
3319 || target_read_memory (pc, dummy, 4) != 0)
3320 return 1;
3321
3322 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0
3323 && arm_skip_bx_reg (this_frame, pc) != 0)
a262aec2 3324 return 1;
909cf6ea 3325
a262aec2 3326 return 0;
909cf6ea
DJ
3327}
3328
a262aec2 3329struct frame_unwind arm_stub_unwind = {
a154d838 3330 "arm stub",
a262aec2 3331 NORMAL_FRAME,
8fbca658 3332 default_frame_unwind_stop_reason,
a262aec2
DJ
3333 arm_stub_this_id,
3334 arm_prologue_prev_register,
3335 NULL,
3336 arm_stub_unwind_sniffer
3337};
3338
2ae28aa9
YQ
3339/* Put here the code to store, into CACHE->saved_regs, the addresses
3340 of the saved registers of frame described by THIS_FRAME. CACHE is
3341 returned. */
3342
3343static struct arm_prologue_cache *
3344arm_m_exception_cache (struct frame_info *this_frame)
3345{
3346 struct gdbarch *gdbarch = get_frame_arch (this_frame);
08106042 3347 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ef273377 3348 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2ae28aa9 3349 struct arm_prologue_cache *cache;
55ea94da 3350 CORE_ADDR lr;
ef273377 3351 CORE_ADDR sp;
2ae28aa9 3352 CORE_ADDR unwound_sp;
ef273377 3353 uint32_t sp_r0_offset = 0;
2ae28aa9 3354 LONGEST xpsr;
55ea94da 3355 uint32_t exc_return;
ef273377 3356 bool fnc_return;
55ea94da 3357 uint32_t extended_frame_used;
ef273377
CL
3358 bool secure_stack_used = false;
3359 bool default_callee_register_stacking = false;
3360 bool exception_domain_is_secure = false;
2ae28aa9
YQ
3361
3362 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 3363 arm_cache_init (cache, this_frame);
2ae28aa9 3364
55ea94da
FH
3365 /* ARMv7-M Architecture Reference "B1.5.6 Exception entry behavior"
3366 describes which bits in LR that define which stack was used prior
3367 to the exception and if FPU is used (causing extended stack frame). */
3368
3369 lr = get_frame_register_unsigned (this_frame, ARM_LR_REGNUM);
ef273377
CL
3370 sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
3371
148ca9dd
YR
3372 /* ARMv7-M Architecture Reference "A2.3.1 Arm core registers"
3373 states that LR is set to 0xffffffff on reset. ARMv8-M Architecture
3374 Reference "B3.3 Registers" states that LR is set to 0xffffffff on warm
3375 reset if Main Extension is implemented, otherwise the value is unknown. */
3376 if (lr == 0xffffffff)
3377 {
3378 /* Terminate any further stack unwinding by referring to self. */
3379 arm_cache_set_active_sp_value (cache, tdep, sp);
3380 return cache;
3381 }
3382
8c9ae6df 3383 fnc_return = (((lr >> 24) & 0xff) == 0xfe);
ef273377
CL
3384 if (tdep->have_sec_ext && fnc_return)
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
3395 xpsr = get_frame_register_unsigned (this_frame, ARM_PS_REGNUM);
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
8c9ae6df 3403 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). */
3428 exc_return = (((lr >> 24) & 0xff) == 0xff);
3429 if (exc_return)
55ea94da 3430 {
ef273377
CL
3431 /* Check EXC_RETURN bit SPSEL if Main or Thread (process) stack used. */
3432 bool process_stack_used = ((lr & (1 << 2)) != 0);
3433
3434 if (tdep->have_sec_ext)
3435 {
3436 secure_stack_used = ((lr & (1 << 6)) != 0);
3437 default_callee_register_stacking = ((lr & (1 << 5)) != 0);
3438 exception_domain_is_secure = ((lr & (1 << 0)) == 0);
3439
3440 /* Unwinding from non-secure to secure can trip security
3441 measures. In order to avoid the debugger being
3442 intrusive, rely on the user to configure the requested
3443 mode. */
3444 if (secure_stack_used && !exception_domain_is_secure
3445 && !arm_unwind_secure_frames)
3446 {
3447 warning (_("Non-secure to secure stack unwinding disabled."));
3448
3449 /* Terminate any further stack unwinding by referring to self. */
3450 arm_cache_set_active_sp_value (cache, tdep, sp);
3451 return cache;
3452 }
3453
3454 if (process_stack_used)
3455 {
3456 if (secure_stack_used)
3457 /* Secure thread (process) stack used, use PSP_S as SP. */
3458 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_psp_s_regnum);
3459 else
3460 /* Non-secure thread (process) stack used, use PSP_NS as SP. */
3461 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_psp_ns_regnum);
3462 }
3463 else
3464 {
3465 if (secure_stack_used)
3466 /* Secure main stack used, use MSP_S as SP. */
3467 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_msp_s_regnum);
3468 else
3469 /* Non-secure main stack used, use MSP_NS as SP. */
3470 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_msp_ns_regnum);
3471 }
3472 }
3473 else
3474 {
3475 if (process_stack_used)
3476 /* Thread (process) stack used, use PSP as SP. */
3477 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_psp_regnum);
3478 else
3479 /* Main stack used, use MSP as SP. */
3480 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_msp_regnum);
3481 }
55ea94da 3482 }
ef273377
CL
3483
3484 /* Fetch the SP to use for this frame. */
3485 unwound_sp = arm_cache_get_prev_sp_value (cache, tdep);
3486
2d9cf99d
YR
3487 /* Exception entry context stacking are described in ARMv8-M (section B3.19)
3488 and ARMv7-M (sections B1.5.6 and B1.5.7) Architecture Reference Manuals.
3489
3490 The following figure shows the structure of the stack frame when Security
3491 and Floating-point extensions are present.
3492
3493 SP Offsets
3494 Without With
3495 Callee Regs Callee Regs
3496 (Secure -> Non-Secure)
3497 +-------------------+
3498 0xA8 | | 0xD0
3499 +===================+ --+ <-- Original SP
3500 0xA4 | S31 | 0xCC |
3501 +-------------------+ |
3502 ... | Additional FP context
3503 +-------------------+ |
3504 0x68 | S16 | 0x90 |
3505 +===================+ --+
3506 0x64 | Reserved | 0x8C |
3507 +-------------------+ |
3508 0x60 | FPSCR | 0x88 |
3509 +-------------------+ |
3510 0x5C | S15 | 0x84 | FP context
3511 +-------------------+ |
3512 ... |
3513 +-------------------+ |
3514 0x20 | S0 | 0x48 |
3515 +===================+ --+
3516 0x1C | xPSR | 0x44 |
3517 +-------------------+ |
3518 0x18 | Return address | 0x40 |
3519 +-------------------+ |
3520 0x14 | LR(R14) | 0x3C |
3521 +-------------------+ |
3522 0x10 | R12 | 0x38 | State context
3523 +-------------------+ |
3524 0x0C | R3 | 0x34 |
3525 +-------------------+ |
3526 ... |
3527 +-------------------+ |
3528 0x00 | R0 | 0x28 |
3529 +===================+ --+
3530 | R11 | 0x24 |
3531 +-------------------+ |
3532 ... |
3533 +-------------------+ | Additional state context
3534 | R4 | 0x08 | when transitioning from
3535 +-------------------+ | Secure to Non-Secure
3536 | Reserved | 0x04 |
3537 +-------------------+ |
3538 | Magic signature | 0x00 |
3539 +===================+ --+ <-- New SP */
3540
ef273377
CL
3541 /* With the Security extension, the hardware saves R4..R11 too. */
3542 if (exc_return && tdep->have_sec_ext && secure_stack_used
3543 && (!default_callee_register_stacking || exception_domain_is_secure))
3544 {
3545 /* Read R4..R11 from the integer callee registers. */
3546 cache->saved_regs[4].set_addr (unwound_sp + 0x08);
3547 cache->saved_regs[5].set_addr (unwound_sp + 0x0C);
3548 cache->saved_regs[6].set_addr (unwound_sp + 0x10);
3549 cache->saved_regs[7].set_addr (unwound_sp + 0x14);
3550 cache->saved_regs[8].set_addr (unwound_sp + 0x18);
3551 cache->saved_regs[9].set_addr (unwound_sp + 0x1C);
3552 cache->saved_regs[10].set_addr (unwound_sp + 0x20);
3553 cache->saved_regs[11].set_addr (unwound_sp + 0x24);
3554 sp_r0_offset = 0x28;
55ea94da 3555 }
2ae28aa9
YQ
3556
3557 /* The hardware saves eight 32-bit words, comprising xPSR,
3558 ReturnAddress, LR (R14), R12, R3, R2, R1, R0. See details in
3559 "B1.5.6 Exception entry behavior" in
3560 "ARMv7-M Architecture Reference Manual". */
ef273377 3561 cache->saved_regs[0].set_addr (unwound_sp + sp_r0_offset);
1d2eeb66
YR
3562 cache->saved_regs[1].set_addr (unwound_sp + sp_r0_offset + 0x04);
3563 cache->saved_regs[2].set_addr (unwound_sp + sp_r0_offset + 0x08);
3564 cache->saved_regs[3].set_addr (unwound_sp + sp_r0_offset + 0x0C);
3565 cache->saved_regs[ARM_IP_REGNUM].set_addr (unwound_sp + sp_r0_offset + 0x10);
3566 cache->saved_regs[ARM_LR_REGNUM].set_addr (unwound_sp + sp_r0_offset + 0x14);
3567 cache->saved_regs[ARM_PC_REGNUM].set_addr (unwound_sp + sp_r0_offset + 0x18);
3568 cache->saved_regs[ARM_PS_REGNUM].set_addr (unwound_sp + sp_r0_offset + 0x1C);
2ae28aa9 3569
55ea94da
FH
3570 /* Check EXC_RETURN bit FTYPE if extended stack frame (FPU regs stored)
3571 type used. */
3572 extended_frame_used = ((lr & (1 << 4)) == 0);
3573 if (exc_return && extended_frame_used)
3574 {
3575 int i;
3576 int fpu_regs_stack_offset;
69b46464
YR
3577 ULONGEST fpccr;
3578
3579 /* Read FPCCR register. */
3580 gdb_assert (safe_read_memory_unsigned_integer (FPCCR,
3581 ARM_INT_REGISTER_SIZE,
3582 byte_order, &fpccr));
3583 bool fpccr_ts = bit (fpccr,26);
55ea94da
FH
3584
3585 /* This code does not take into account the lazy stacking, see "Lazy
dda83cd7
SM
3586 context save of FP state", in B1.5.7, also ARM AN298, supported
3587 by Cortex-M4F architecture.
3588 To fully handle this the FPCCR register (Floating-point Context
3589 Control Register) needs to be read out and the bits ASPEN and LSPEN
3590 could be checked to setup correct lazy stacked FP registers.
3591 This register is located at address 0xE000EF34. */
55ea94da
FH
3592
3593 /* Extended stack frame type used. */
ef273377 3594 fpu_regs_stack_offset = unwound_sp + sp_r0_offset + 0x20;
39fc7ff6 3595 for (i = 0; i < 8; i++)
dda83cd7 3596 {
098caef4 3597 cache->saved_regs[ARM_D0_REGNUM + i].set_addr (fpu_regs_stack_offset);
39fc7ff6 3598 fpu_regs_stack_offset += 8;
dda83cd7 3599 }
ef273377
CL
3600 cache->saved_regs[ARM_FPSCR_REGNUM].set_addr (unwound_sp + sp_r0_offset + 0x60);
3601 fpu_regs_stack_offset += 4;
55ea94da 3602
69b46464 3603 if (tdep->have_sec_ext && !default_callee_register_stacking && fpccr_ts)
ef273377
CL
3604 {
3605 /* Handle floating-point callee saved registers. */
2d9cf99d 3606 fpu_regs_stack_offset = unwound_sp + sp_r0_offset + 0x68;
39fc7ff6 3607 for (i = 8; i < 16; i++)
ef273377
CL
3608 {
3609 cache->saved_regs[ARM_D0_REGNUM + i].set_addr (fpu_regs_stack_offset);
39fc7ff6 3610 fpu_regs_stack_offset += 8;
ef273377
CL
3611 }
3612
2d9cf99d
YR
3613 arm_cache_set_active_sp_value (cache, tdep,
3614 unwound_sp + sp_r0_offset + 0xA8);
ef273377
CL
3615 }
3616 else
3617 {
3618 /* Offset 0x64 is reserved. */
2d9cf99d
YR
3619 arm_cache_set_active_sp_value (cache, tdep,
3620 unwound_sp + sp_r0_offset + 0x68);
ef273377 3621 }
55ea94da
FH
3622 }
3623 else
3624 {
3625 /* Standard stack frame type used. */
2d9cf99d
YR
3626 arm_cache_set_active_sp_value (cache, tdep,
3627 unwound_sp + sp_r0_offset + 0x20);
55ea94da
FH
3628 }
3629
2ae28aa9
YQ
3630 /* If bit 9 of the saved xPSR is set, then there is a four-byte
3631 aligner between the top of the 32-byte stack frame and the
3632 previous context's stack pointer. */
1d2eeb66
YR
3633 if (safe_read_memory_integer (unwound_sp + sp_r0_offset + 0x1C, 4,
3634 byte_order, &xpsr)
2ae28aa9 3635 && (xpsr & (1 << 9)) != 0)
ae7e2f45
CL
3636 arm_cache_set_active_sp_value (cache, tdep,
3637 arm_cache_get_prev_sp_value (cache, tdep) + 4);
2ae28aa9
YQ
3638
3639 return cache;
3640}
3641
3642/* Implementation of function hook 'this_id' in
3643 'struct frame_uwnind'. */
3644
3645static void
3646arm_m_exception_this_id (struct frame_info *this_frame,
3647 void **this_cache,
3648 struct frame_id *this_id)
3649{
3650 struct arm_prologue_cache *cache;
3651
3652 if (*this_cache == NULL)
3653 *this_cache = arm_m_exception_cache (this_frame);
9a3c8263 3654 cache = (struct arm_prologue_cache *) *this_cache;
2ae28aa9
YQ
3655
3656 /* Our frame ID for a stub frame is the current SP and LR. */
ae7e2f45 3657 arm_gdbarch_tdep *tdep
08106042 3658 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45 3659 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep),
2ae28aa9
YQ
3660 get_frame_pc (this_frame));
3661}
3662
3663/* Implementation of function hook 'prev_register' in
3664 'struct frame_uwnind'. */
3665
3666static struct value *
3667arm_m_exception_prev_register (struct frame_info *this_frame,
3668 void **this_cache,
3669 int prev_regnum)
3670{
2ae28aa9 3671 struct arm_prologue_cache *cache;
ef273377 3672 CORE_ADDR sp_value;
2ae28aa9
YQ
3673
3674 if (*this_cache == NULL)
3675 *this_cache = arm_m_exception_cache (this_frame);
9a3c8263 3676 cache = (struct arm_prologue_cache *) *this_cache;
2ae28aa9
YQ
3677
3678 /* The value was already reconstructed into PREV_SP. */
ae7e2f45 3679 arm_gdbarch_tdep *tdep
08106042 3680 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
2ae28aa9
YQ
3681 if (prev_regnum == ARM_SP_REGNUM)
3682 return frame_unwind_got_constant (this_frame, prev_regnum,
ae7e2f45 3683 arm_cache_get_prev_sp_value (cache, tdep));
2ae28aa9 3684
8c9ae6df
YR
3685 /* If we are asked to unwind the PC, strip the saved T bit. */
3686 if (prev_regnum == ARM_PC_REGNUM)
3687 {
3688 struct value *value = trad_frame_get_prev_register (this_frame,
3689 cache->saved_regs,
3690 prev_regnum);
3691 CORE_ADDR pc = value_as_address (value);
3692 return frame_unwind_got_constant (this_frame, prev_regnum,
3693 UNMAKE_THUMB_ADDR (pc));
3694 }
3695
ef273377
CL
3696 /* The value might be one of the alternative SP, if so, use the
3697 value already constructed. */
d65edaa0 3698 if (arm_is_alternative_sp_register (tdep, prev_regnum))
ef273377
CL
3699 {
3700 sp_value = arm_cache_get_sp_register (cache, tdep, prev_regnum);
3701 return frame_unwind_got_constant (this_frame, prev_regnum, sp_value);
3702 }
3703
8c9ae6df
YR
3704 /* If we are asked to unwind the xPSR, set T bit if PC is in thumb mode.
3705 LR register is unreliable as it contains FNC_RETURN or EXC_RETURN
3706 pattern. */
3707 if (prev_regnum == ARM_PS_REGNUM)
3708 {
3709 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3710 struct value *value = trad_frame_get_prev_register (this_frame,
3711 cache->saved_regs,
3712 ARM_PC_REGNUM);
3713 CORE_ADDR pc = value_as_address (value);
3714 value = trad_frame_get_prev_register (this_frame, cache->saved_regs,
3715 ARM_PS_REGNUM);
3716 ULONGEST xpsr = value_as_long (value);
3717
3718 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
3719 xpsr = reconstruct_t_bit (gdbarch, pc, xpsr);
3720 return frame_unwind_got_constant (this_frame, ARM_PS_REGNUM, xpsr);
3721 }
3722
2ae28aa9
YQ
3723 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
3724 prev_regnum);
3725}
3726
3727/* Implementation of function hook 'sniffer' in
3728 'struct frame_uwnind'. */
3729
3730static int
3731arm_m_exception_unwind_sniffer (const struct frame_unwind *self,
3732 struct frame_info *this_frame,
3733 void **this_prologue_cache)
3734{
ef273377 3735 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2ae28aa9
YQ
3736 CORE_ADDR this_pc = get_frame_pc (this_frame);
3737
3738 /* No need to check is_m; this sniffer is only registered for
3739 M-profile architectures. */
3740
ca90e760 3741 /* Check if exception frame returns to a magic PC value. */
ef273377 3742 return arm_m_addr_is_magic (gdbarch, this_pc);
2ae28aa9
YQ
3743}
3744
3745/* Frame unwinder for M-profile exceptions. */
3746
3747struct frame_unwind arm_m_exception_unwind =
3748{
a154d838 3749 "arm m exception",
2ae28aa9
YQ
3750 SIGTRAMP_FRAME,
3751 default_frame_unwind_stop_reason,
3752 arm_m_exception_this_id,
3753 arm_m_exception_prev_register,
3754 NULL,
3755 arm_m_exception_unwind_sniffer
3756};
3757
24de872b 3758static CORE_ADDR
a262aec2 3759arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
24de872b
DJ
3760{
3761 struct arm_prologue_cache *cache;
3762
eb5492fa 3763 if (*this_cache == NULL)
a262aec2 3764 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 3765 cache = (struct arm_prologue_cache *) *this_cache;
eb5492fa 3766
ae7e2f45 3767 arm_gdbarch_tdep *tdep
08106042 3768 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45 3769 return arm_cache_get_prev_sp_value (cache, tdep) - cache->framesize;
24de872b
DJ
3770}
3771
eb5492fa
DJ
3772struct frame_base arm_normal_base = {
3773 &arm_prologue_unwind,
3774 arm_normal_frame_base,
3775 arm_normal_frame_base,
3776 arm_normal_frame_base
3777};
3778
b39cc962
DJ
3779static struct value *
3780arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
3781 int regnum)
3782{
24568a2c 3783 struct gdbarch * gdbarch = get_frame_arch (this_frame);
08106042 3784 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
8c9ae6df
YR
3785 CORE_ADDR lr;
3786 ULONGEST cpsr;
b39cc962 3787
42e11f36 3788 if (regnum == ARM_PC_REGNUM)
b39cc962 3789 {
b39cc962
DJ
3790 /* The PC is normally copied from the return column, which
3791 describes saves of LR. However, that version may have an
3792 extra bit set to indicate Thumb state. The bit is not
3793 part of the PC. */
a01567f4
LM
3794
3795 /* Record in the frame whether the return address was signed. */
3796 if (tdep->have_pacbti)
3797 {
3798 CORE_ADDR ra_auth_code
3799 = frame_unwind_register_unsigned (this_frame,
3800 tdep->pacbti_pseudo_base);
3801
3802 if (ra_auth_code != 0)
3803 set_frame_previous_pc_masked (this_frame);
3804 }
3805
b39cc962
DJ
3806 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3807 return frame_unwind_got_constant (this_frame, regnum,
24568a2c 3808 arm_addr_bits_remove (gdbarch, lr));
42e11f36
TS
3809 }
3810 else if (regnum == ARM_PS_REGNUM)
3811 {
b39cc962 3812 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
ca38c58e 3813 cpsr = get_frame_register_unsigned (this_frame, regnum);
b39cc962 3814 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
8c9ae6df 3815 cpsr = reconstruct_t_bit (gdbarch, lr, cpsr);
ca38c58e 3816 return frame_unwind_got_constant (this_frame, regnum, cpsr);
b39cc962 3817 }
a6e4a48c
YR
3818 else if (arm_is_alternative_sp_register (tdep, regnum))
3819 {
3820 /* Handle the alternative SP registers on Cortex-M. */
3821 bool override_with_sp_value = false;
3822 CORE_ADDR val;
3823
3824 if (tdep->have_sec_ext)
3825 {
3826 CORE_ADDR sp
3827 = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
3828 CORE_ADDR msp_s
3829 = get_frame_register_unsigned (this_frame,
3830 tdep->m_profile_msp_s_regnum);
3831 CORE_ADDR msp_ns
3832 = get_frame_register_unsigned (this_frame,
3833 tdep->m_profile_msp_ns_regnum);
3834 CORE_ADDR psp_s
3835 = get_frame_register_unsigned (this_frame,
3836 tdep->m_profile_psp_s_regnum);
3837 CORE_ADDR psp_ns
3838 = get_frame_register_unsigned (this_frame,
3839 tdep->m_profile_psp_ns_regnum);
3840
3841 bool is_msp = (regnum == tdep->m_profile_msp_regnum)
3842 && (msp_s == sp || msp_ns == sp);
3843 bool is_msp_s = (regnum == tdep->m_profile_msp_s_regnum)
3844 && (msp_s == sp);
3845 bool is_msp_ns = (regnum == tdep->m_profile_msp_ns_regnum)
3846 && (msp_ns == sp);
3847 bool is_psp = (regnum == tdep->m_profile_psp_regnum)
3848 && (psp_s == sp || psp_ns == sp);
3849 bool is_psp_s = (regnum == tdep->m_profile_psp_s_regnum)
3850 && (psp_s == sp);
3851 bool is_psp_ns = (regnum == tdep->m_profile_psp_ns_regnum)
3852 && (psp_ns == sp);
3853
3854 override_with_sp_value = is_msp || is_msp_s || is_msp_ns
3855 || is_psp || is_psp_s || is_psp_ns;
3856
3857 }
3858 else if (tdep->is_m)
3859 {
3860 CORE_ADDR sp
3861 = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
3862 CORE_ADDR msp
3863 = get_frame_register_unsigned (this_frame,
3864 tdep->m_profile_msp_regnum);
3865 CORE_ADDR psp
3866 = get_frame_register_unsigned (this_frame,
3867 tdep->m_profile_psp_regnum);
3868
3869 bool is_msp = (regnum == tdep->m_profile_msp_regnum) && (sp == msp);
3870 bool is_psp = (regnum == tdep->m_profile_psp_regnum) && (sp == psp);
3871
3872 override_with_sp_value = is_msp || is_psp;
3873 }
3874
3875 if (override_with_sp_value)
3876 {
3877 /* Use value of SP from previous frame. */
3878 struct frame_info *prev_frame = get_prev_frame (this_frame);
3879 if (prev_frame)
3880 val = get_frame_register_unsigned (prev_frame, ARM_SP_REGNUM);
3881 else
3882 val = get_frame_base (this_frame);
3883 }
3884 else
3885 /* Use value for the register from previous frame. */
3886 val = get_frame_register_unsigned (this_frame, regnum);
3887
3888 return frame_unwind_got_constant (this_frame, regnum, val);
3889 }
42e11f36
TS
3890
3891 internal_error (__FILE__, __LINE__,
3892 _("Unexpected register %d"), regnum);
b39cc962
DJ
3893}
3894
c9cf6e20 3895/* Implement the stack_frame_destroyed_p gdbarch method. */
4024ca99
UW
3896
3897static int
c9cf6e20 3898thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
4024ca99
UW
3899{
3900 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3901 unsigned int insn, insn2;
3902 int found_return = 0, found_stack_adjust = 0;
3903 CORE_ADDR func_start, func_end;
3904 CORE_ADDR scan_pc;
3905 gdb_byte buf[4];
3906
3907 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3908 return 0;
3909
3910 /* The epilogue is a sequence of instructions along the following lines:
3911
3912 - add stack frame size to SP or FP
3913 - [if frame pointer used] restore SP from FP
3914 - restore registers from SP [may include PC]
3915 - a return-type instruction [if PC wasn't already restored]
3916
3917 In a first pass, we scan forward from the current PC and verify the
3918 instructions we find as compatible with this sequence, ending in a
3919 return instruction.
3920
3921 However, this is not sufficient to distinguish indirect function calls
3922 within a function from indirect tail calls in the epilogue in some cases.
3923 Therefore, if we didn't already find any SP-changing instruction during
3924 forward scan, we add a backward scanning heuristic to ensure we actually
3925 are in the epilogue. */
3926
3927 scan_pc = pc;
3928 while (scan_pc < func_end && !found_return)
3929 {
3930 if (target_read_memory (scan_pc, buf, 2))
3931 break;
3932
3933 scan_pc += 2;
3934 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3935
3936 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
3937 found_return = 1;
3938 else if (insn == 0x46f7) /* mov pc, lr */
3939 found_return = 1;
540314bd 3940 else if (thumb_instruction_restores_sp (insn))
4024ca99 3941 {
b7576e5c 3942 if ((insn & 0xff00) == 0xbd00) /* pop <registers, PC> */
4024ca99
UW
3943 found_return = 1;
3944 }
db24da6d 3945 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instruction */
4024ca99
UW
3946 {
3947 if (target_read_memory (scan_pc, buf, 2))
3948 break;
3949
3950 scan_pc += 2;
3951 insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3952
3953 if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3954 {
4024ca99
UW
3955 if (insn2 & 0x8000) /* <registers> include PC. */
3956 found_return = 1;
3957 }
3958 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3959 && (insn2 & 0x0fff) == 0x0b04)
3960 {
4024ca99
UW
3961 if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC. */
3962 found_return = 1;
3963 }
3964 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3965 && (insn2 & 0x0e00) == 0x0a00)
6b65d1b6 3966 ;
4024ca99
UW
3967 else
3968 break;
3969 }
3970 else
3971 break;
3972 }
3973
3974 if (!found_return)
3975 return 0;
3976
3977 /* Since any instruction in the epilogue sequence, with the possible
3978 exception of return itself, updates the stack pointer, we need to
3979 scan backwards for at most one instruction. Try either a 16-bit or
3980 a 32-bit instruction. This is just a heuristic, so we do not worry
0963b4bd 3981 too much about false positives. */
4024ca99 3982
6b65d1b6
YQ
3983 if (pc - 4 < func_start)
3984 return 0;
3985 if (target_read_memory (pc - 4, buf, 4))
3986 return 0;
4024ca99 3987
6b65d1b6
YQ
3988 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3989 insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);
3990
3991 if (thumb_instruction_restores_sp (insn2))
3992 found_stack_adjust = 1;
3993 else if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3994 found_stack_adjust = 1;
3995 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3996 && (insn2 & 0x0fff) == 0x0b04)
3997 found_stack_adjust = 1;
3998 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3999 && (insn2 & 0x0e00) == 0x0a00)
4000 found_stack_adjust = 1;
4024ca99
UW
4001
4002 return found_stack_adjust;
4003}
4004
4024ca99 4005static int
c58b006a 4006arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch, CORE_ADDR pc)
4024ca99
UW
4007{
4008 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4009 unsigned int insn;
f303bc3e 4010 int found_return;
4024ca99
UW
4011 CORE_ADDR func_start, func_end;
4012
4024ca99
UW
4013 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
4014 return 0;
4015
4016 /* We are in the epilogue if the previous instruction was a stack
4017 adjustment and the next instruction is a possible return (bx, mov
4018 pc, or pop). We could have to scan backwards to find the stack
4019 adjustment, or forwards to find the return, but this is a decent
4020 approximation. First scan forwards. */
4021
4022 found_return = 0;
4023 insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
4024 if (bits (insn, 28, 31) != INST_NV)
4025 {
4026 if ((insn & 0x0ffffff0) == 0x012fff10)
4027 /* BX. */
4028 found_return = 1;
4029 else if ((insn & 0x0ffffff0) == 0x01a0f000)
4030 /* MOV PC. */
4031 found_return = 1;
4032 else if ((insn & 0x0fff0000) == 0x08bd0000
4033 && (insn & 0x0000c000) != 0)
4034 /* POP (LDMIA), including PC or LR. */
4035 found_return = 1;
4036 }
4037
4038 if (!found_return)
4039 return 0;
4040
4041 /* Scan backwards. This is just a heuristic, so do not worry about
4042 false positives from mode changes. */
4043
4044 if (pc < func_start + 4)
4045 return 0;
4046
4047 insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
f303bc3e 4048 if (arm_instruction_restores_sp (insn))
4024ca99
UW
4049 return 1;
4050
4051 return 0;
4052}
4053
c58b006a
YQ
4054/* Implement the stack_frame_destroyed_p gdbarch method. */
4055
4056static int
4057arm_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
4058{
4059 if (arm_pc_is_thumb (gdbarch, pc))
4060 return thumb_stack_frame_destroyed_p (gdbarch, pc);
4061 else
4062 return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
4063}
4024ca99 4064
2dd604e7
RE
4065/* When arguments must be pushed onto the stack, they go on in reverse
4066 order. The code below implements a FILO (stack) to do this. */
4067
0fecb1a7 4068struct arm_stack_item
2dd604e7
RE
4069{
4070 int len;
0fecb1a7 4071 struct arm_stack_item *prev;
7c543f7b 4072 gdb_byte *data;
2dd604e7
RE
4073};
4074
0fecb1a7
TT
4075static struct arm_stack_item *
4076push_stack_item (struct arm_stack_item *prev, const gdb_byte *contents,
4077 int len)
2dd604e7 4078{
0fecb1a7
TT
4079 struct arm_stack_item *si;
4080 si = XNEW (struct arm_stack_item);
7c543f7b 4081 si->data = (gdb_byte *) xmalloc (len);
2dd604e7
RE
4082 si->len = len;
4083 si->prev = prev;
4084 memcpy (si->data, contents, len);
4085 return si;
4086}
4087
0fecb1a7
TT
4088static struct arm_stack_item *
4089pop_stack_item (struct arm_stack_item *si)
2dd604e7 4090{
0fecb1a7 4091 struct arm_stack_item *dead = si;
2dd604e7
RE
4092 si = si->prev;
4093 xfree (dead->data);
4094 xfree (dead);
4095 return si;
4096}
4097
030197b4
AB
4098/* Implement the gdbarch type alignment method, overrides the generic
4099 alignment algorithm for anything that is arm specific. */
2af48f68 4100
030197b4
AB
4101static ULONGEST
4102arm_type_align (gdbarch *gdbarch, struct type *t)
2af48f68 4103{
2af48f68 4104 t = check_typedef (t);
bd63c870 4105 if (t->code () == TYPE_CODE_ARRAY && t->is_vector ())
2af48f68 4106 {
030197b4
AB
4107 /* Use the natural alignment for vector types (the same for
4108 scalar type), but the maximum alignment is 64-bit. */
4109 if (TYPE_LENGTH (t) > 8)
4110 return 8;
c4312b19 4111 else
030197b4 4112 return TYPE_LENGTH (t);
2af48f68 4113 }
030197b4
AB
4114
4115 /* Allow the common code to calculate the alignment. */
4116 return 0;
2af48f68
PB
4117}
4118
90445bd3
DJ
4119/* Possible base types for a candidate for passing and returning in
4120 VFP registers. */
4121
4122enum arm_vfp_cprc_base_type
4123{
4124 VFP_CPRC_UNKNOWN,
4125 VFP_CPRC_SINGLE,
4126 VFP_CPRC_DOUBLE,
4127 VFP_CPRC_VEC64,
4128 VFP_CPRC_VEC128
4129};
4130
4131/* The length of one element of base type B. */
4132
4133static unsigned
4134arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
4135{
4136 switch (b)
4137 {
4138 case VFP_CPRC_SINGLE:
4139 return 4;
4140 case VFP_CPRC_DOUBLE:
4141 return 8;
4142 case VFP_CPRC_VEC64:
4143 return 8;
4144 case VFP_CPRC_VEC128:
4145 return 16;
4146 default:
4147 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
4148 (int) b);
4149 }
4150}
4151
4152/* The character ('s', 'd' or 'q') for the type of VFP register used
4153 for passing base type B. */
4154
4155static int
4156arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
4157{
4158 switch (b)
4159 {
4160 case VFP_CPRC_SINGLE:
4161 return 's';
4162 case VFP_CPRC_DOUBLE:
4163 return 'd';
4164 case VFP_CPRC_VEC64:
4165 return 'd';
4166 case VFP_CPRC_VEC128:
4167 return 'q';
4168 default:
4169 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
4170 (int) b);
4171 }
4172}
4173
4174/* Determine whether T may be part of a candidate for passing and
4175 returning in VFP registers, ignoring the limit on the total number
4176 of components. If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
4177 classification of the first valid component found; if it is not
4178 VFP_CPRC_UNKNOWN, all components must have the same classification
4179 as *BASE_TYPE. If it is found that T contains a type not permitted
4180 for passing and returning in VFP registers, a type differently
4181 classified from *BASE_TYPE, or two types differently classified
4182 from each other, return -1, otherwise return the total number of
4183 base-type elements found (possibly 0 in an empty structure or
817e0957
YQ
4184 array). Vector types are not currently supported, matching the
4185 generic AAPCS support. */
90445bd3
DJ
4186
4187static int
4188arm_vfp_cprc_sub_candidate (struct type *t,
4189 enum arm_vfp_cprc_base_type *base_type)
4190{
4191 t = check_typedef (t);
78134374 4192 switch (t->code ())
90445bd3
DJ
4193 {
4194 case TYPE_CODE_FLT:
4195 switch (TYPE_LENGTH (t))
4196 {
4197 case 4:
4198 if (*base_type == VFP_CPRC_UNKNOWN)
4199 *base_type = VFP_CPRC_SINGLE;
4200 else if (*base_type != VFP_CPRC_SINGLE)
4201 return -1;
4202 return 1;
4203
4204 case 8:
4205 if (*base_type == VFP_CPRC_UNKNOWN)
4206 *base_type = VFP_CPRC_DOUBLE;
4207 else if (*base_type != VFP_CPRC_DOUBLE)
4208 return -1;
4209 return 1;
4210
4211 default:
4212 return -1;
4213 }
4214 break;
4215
817e0957
YQ
4216 case TYPE_CODE_COMPLEX:
4217 /* Arguments of complex T where T is one of the types float or
4218 double get treated as if they are implemented as:
4219
4220 struct complexT
4221 {
4222 T real;
4223 T imag;
5f52445b
YQ
4224 };
4225
4226 */
817e0957
YQ
4227 switch (TYPE_LENGTH (t))
4228 {
4229 case 8:
4230 if (*base_type == VFP_CPRC_UNKNOWN)
4231 *base_type = VFP_CPRC_SINGLE;
4232 else if (*base_type != VFP_CPRC_SINGLE)
4233 return -1;
4234 return 2;
4235
4236 case 16:
4237 if (*base_type == VFP_CPRC_UNKNOWN)
4238 *base_type = VFP_CPRC_DOUBLE;
4239 else if (*base_type != VFP_CPRC_DOUBLE)
4240 return -1;
4241 return 2;
4242
4243 default:
4244 return -1;
4245 }
4246 break;
4247
90445bd3
DJ
4248 case TYPE_CODE_ARRAY:
4249 {
bd63c870 4250 if (t->is_vector ())
90445bd3 4251 {
c4312b19
YQ
4252 /* A 64-bit or 128-bit containerized vector type are VFP
4253 CPRCs. */
4254 switch (TYPE_LENGTH (t))
4255 {
4256 case 8:
4257 if (*base_type == VFP_CPRC_UNKNOWN)
4258 *base_type = VFP_CPRC_VEC64;
4259 return 1;
4260 case 16:
4261 if (*base_type == VFP_CPRC_UNKNOWN)
4262 *base_type = VFP_CPRC_VEC128;
4263 return 1;
4264 default:
4265 return -1;
4266 }
4267 }
4268 else
4269 {
4270 int count;
4271 unsigned unitlen;
4272
4273 count = arm_vfp_cprc_sub_candidate (TYPE_TARGET_TYPE (t),
4274 base_type);
4275 if (count == -1)
4276 return -1;
4277 if (TYPE_LENGTH (t) == 0)
4278 {
4279 gdb_assert (count == 0);
4280 return 0;
4281 }
4282 else if (count == 0)
4283 return -1;
4284 unitlen = arm_vfp_cprc_unit_length (*base_type);
4285 gdb_assert ((TYPE_LENGTH (t) % unitlen) == 0);
4286 return TYPE_LENGTH (t) / unitlen;
90445bd3 4287 }
90445bd3
DJ
4288 }
4289 break;
4290
4291 case TYPE_CODE_STRUCT:
4292 {
4293 int count = 0;
4294 unsigned unitlen;
4295 int i;
1f704f76 4296 for (i = 0; i < t->num_fields (); i++)
90445bd3 4297 {
1040b979
YQ
4298 int sub_count = 0;
4299
ceacbf6e 4300 if (!field_is_static (&t->field (i)))
940da03e 4301 sub_count = arm_vfp_cprc_sub_candidate (t->field (i).type (),
1040b979 4302 base_type);
90445bd3
DJ
4303 if (sub_count == -1)
4304 return -1;
4305 count += sub_count;
4306 }
4307 if (TYPE_LENGTH (t) == 0)
4308 {
4309 gdb_assert (count == 0);
4310 return 0;
4311 }
4312 else if (count == 0)
4313 return -1;
4314 unitlen = arm_vfp_cprc_unit_length (*base_type);
4315 if (TYPE_LENGTH (t) != unitlen * count)
4316 return -1;
4317 return count;
4318 }
4319
4320 case TYPE_CODE_UNION:
4321 {
4322 int count = 0;
4323 unsigned unitlen;
4324 int i;
1f704f76 4325 for (i = 0; i < t->num_fields (); i++)
90445bd3 4326 {
940da03e 4327 int sub_count = arm_vfp_cprc_sub_candidate (t->field (i).type (),
90445bd3
DJ
4328 base_type);
4329 if (sub_count == -1)
4330 return -1;
4331 count = (count > sub_count ? count : sub_count);
4332 }
4333 if (TYPE_LENGTH (t) == 0)
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);
4341 if (TYPE_LENGTH (t) != unitlen * count)
4342 return -1;
4343 return count;
4344 }
4345
4346 default:
4347 break;
4348 }
4349
4350 return -1;
4351}
4352
4353/* Determine whether T is a VFP co-processor register candidate (CPRC)
4354 if passed to or returned from a non-variadic function with the VFP
4355 ABI in effect. Return 1 if it is, 0 otherwise. If it is, set
4356 *BASE_TYPE to the base type for T and *COUNT to the number of
4357 elements of that base type before returning. */
4358
4359static int
4360arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
4361 int *count)
4362{
4363 enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
4364 int c = arm_vfp_cprc_sub_candidate (t, &b);
4365 if (c <= 0 || c > 4)
4366 return 0;
4367 *base_type = b;
4368 *count = c;
4369 return 1;
4370}
4371
4372/* Return 1 if the VFP ABI should be used for passing arguments to and
4373 returning values from a function of type FUNC_TYPE, 0
4374 otherwise. */
4375
4376static int
4377arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
4378{
08106042 4379 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
345bd07c 4380
90445bd3
DJ
4381 /* Variadic functions always use the base ABI. Assume that functions
4382 without debug info are not variadic. */
a409645d 4383 if (func_type && check_typedef (func_type)->has_varargs ())
90445bd3 4384 return 0;
345bd07c 4385
90445bd3
DJ
4386 /* The VFP ABI is only supported as a variant of AAPCS. */
4387 if (tdep->arm_abi != ARM_ABI_AAPCS)
4388 return 0;
345bd07c
SM
4389
4390 return tdep->fp_model == ARM_FLOAT_VFP;
90445bd3
DJ
4391}
4392
4393/* We currently only support passing parameters in integer registers, which
4394 conforms with GCC's default model, and VFP argument passing following
4395 the VFP variant of AAPCS. Several other variants exist and
2dd604e7
RE
4396 we should probably support some of them based on the selected ABI. */
4397
4398static CORE_ADDR
7d9b040b 4399arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6a65450a 4400 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
cf84fa6b
AH
4401 struct value **args, CORE_ADDR sp,
4402 function_call_return_method return_method,
6a65450a 4403 CORE_ADDR struct_addr)
2dd604e7 4404{
e17a4113 4405 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2dd604e7
RE
4406 int argnum;
4407 int argreg;
4408 int nstack;
0fecb1a7 4409 struct arm_stack_item *si = NULL;
90445bd3
DJ
4410 int use_vfp_abi;
4411 struct type *ftype;
4412 unsigned vfp_regs_free = (1 << 16) - 1;
08106042 4413 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
90445bd3
DJ
4414
4415 /* Determine the type of this function and whether the VFP ABI
4416 applies. */
4417 ftype = check_typedef (value_type (function));
78134374 4418 if (ftype->code () == TYPE_CODE_PTR)
90445bd3
DJ
4419 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
4420 use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
2dd604e7 4421
6a65450a
AC
4422 /* Set the return address. For the ARM, the return breakpoint is
4423 always at BP_ADDR. */
9779414d 4424 if (arm_pc_is_thumb (gdbarch, bp_addr))
9dca5578 4425 bp_addr |= 1;
6a65450a 4426 regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
2dd604e7
RE
4427
4428 /* Walk through the list of args and determine how large a temporary
4429 stack is required. Need to take care here as structs may be
7a9dd1b2 4430 passed on the stack, and we have to push them. */
2dd604e7
RE
4431 nstack = 0;
4432
4433 argreg = ARM_A1_REGNUM;
4434 nstack = 0;
4435
2dd604e7
RE
4436 /* The struct_return pointer occupies the first parameter
4437 passing register. */
cf84fa6b 4438 if (return_method == return_method_struct)
2dd604e7 4439 {
7cb6d92a
SM
4440 arm_debug_printf ("struct return in %s = %s",
4441 gdbarch_register_name (gdbarch, argreg),
4442 paddress (gdbarch, struct_addr));
4443
2dd604e7
RE
4444 regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
4445 argreg++;
4446 }
4447
4448 for (argnum = 0; argnum < nargs; argnum++)
4449 {
4450 int len;
4451 struct type *arg_type;
4452 struct type *target_type;
4453 enum type_code typecode;
8c6363cf 4454 const bfd_byte *val;
2af48f68 4455 int align;
90445bd3
DJ
4456 enum arm_vfp_cprc_base_type vfp_base_type;
4457 int vfp_base_count;
4458 int may_use_core_reg = 1;
2dd604e7 4459
df407dfe 4460 arg_type = check_typedef (value_type (args[argnum]));
2dd604e7
RE
4461 len = TYPE_LENGTH (arg_type);
4462 target_type = TYPE_TARGET_TYPE (arg_type);
78134374 4463 typecode = arg_type->code ();
50888e42 4464 val = value_contents (args[argnum]).data ();
2dd604e7 4465
030197b4 4466 align = type_align (arg_type);
2af48f68 4467 /* Round alignment up to a whole number of words. */
f0452268
AH
4468 align = (align + ARM_INT_REGISTER_SIZE - 1)
4469 & ~(ARM_INT_REGISTER_SIZE - 1);
2af48f68 4470 /* Different ABIs have different maximum alignments. */
345bd07c 4471 if (tdep->arm_abi == ARM_ABI_APCS)
2af48f68
PB
4472 {
4473 /* The APCS ABI only requires word alignment. */
f0452268 4474 align = ARM_INT_REGISTER_SIZE;
2af48f68
PB
4475 }
4476 else
4477 {
4478 /* The AAPCS requires at most doubleword alignment. */
f0452268
AH
4479 if (align > ARM_INT_REGISTER_SIZE * 2)
4480 align = ARM_INT_REGISTER_SIZE * 2;
2af48f68
PB
4481 }
4482
90445bd3
DJ
4483 if (use_vfp_abi
4484 && arm_vfp_call_candidate (arg_type, &vfp_base_type,
4485 &vfp_base_count))
4486 {
4487 int regno;
4488 int unit_length;
4489 int shift;
4490 unsigned mask;
4491
4492 /* Because this is a CPRC it cannot go in a core register or
4493 cause a core register to be skipped for alignment.
4494 Either it goes in VFP registers and the rest of this loop
4495 iteration is skipped for this argument, or it goes on the
4496 stack (and the stack alignment code is correct for this
4497 case). */
4498 may_use_core_reg = 0;
4499
4500 unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
4501 shift = unit_length / 4;
4502 mask = (1 << (shift * vfp_base_count)) - 1;
4503 for (regno = 0; regno < 16; regno += shift)
4504 if (((vfp_regs_free >> regno) & mask) == mask)
4505 break;
4506
4507 if (regno < 16)
4508 {
4509 int reg_char;
4510 int reg_scaled;
4511 int i;
4512
4513 vfp_regs_free &= ~(mask << regno);
4514 reg_scaled = regno / shift;
4515 reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
4516 for (i = 0; i < vfp_base_count; i++)
4517 {
4518 char name_buf[4];
4519 int regnum;
58d6951d
DJ
4520 if (reg_char == 'q')
4521 arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
90445bd3 4522 val + i * unit_length);
58d6951d
DJ
4523 else
4524 {
8c042590
PM
4525 xsnprintf (name_buf, sizeof (name_buf), "%c%d",
4526 reg_char, reg_scaled + i);
58d6951d
DJ
4527 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
4528 strlen (name_buf));
b66f5587 4529 regcache->cooked_write (regnum, val + i * unit_length);
58d6951d 4530 }
90445bd3
DJ
4531 }
4532 continue;
4533 }
4534 else
4535 {
4536 /* This CPRC could not go in VFP registers, so all VFP
4537 registers are now marked as used. */
4538 vfp_regs_free = 0;
4539 }
4540 }
4541
85102364 4542 /* Push stack padding for doubleword alignment. */
2af48f68
PB
4543 if (nstack & (align - 1))
4544 {
f0452268
AH
4545 si = push_stack_item (si, val, ARM_INT_REGISTER_SIZE);
4546 nstack += ARM_INT_REGISTER_SIZE;
2af48f68
PB
4547 }
4548
4549 /* Doubleword aligned quantities must go in even register pairs. */
90445bd3
DJ
4550 if (may_use_core_reg
4551 && argreg <= ARM_LAST_ARG_REGNUM
f0452268 4552 && align > ARM_INT_REGISTER_SIZE
2af48f68
PB
4553 && argreg & 1)
4554 argreg++;
4555
2dd604e7
RE
4556 /* If the argument is a pointer to a function, and it is a
4557 Thumb function, create a LOCAL copy of the value and set
4558 the THUMB bit in it. */
4559 if (TYPE_CODE_PTR == typecode
4560 && target_type != NULL
78134374 4561 && TYPE_CODE_FUNC == check_typedef (target_type)->code ())
2dd604e7 4562 {
e17a4113 4563 CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
9779414d 4564 if (arm_pc_is_thumb (gdbarch, regval))
2dd604e7 4565 {
224c3ddb 4566 bfd_byte *copy = (bfd_byte *) alloca (len);
8c6363cf 4567 store_unsigned_integer (copy, len, byte_order,
e17a4113 4568 MAKE_THUMB_ADDR (regval));
8c6363cf 4569 val = copy;
2dd604e7
RE
4570 }
4571 }
4572
4573 /* Copy the argument to general registers or the stack in
4574 register-sized pieces. Large arguments are split between
4575 registers and stack. */
4576 while (len > 0)
4577 {
f0452268
AH
4578 int partial_len = len < ARM_INT_REGISTER_SIZE
4579 ? len : ARM_INT_REGISTER_SIZE;
ef9bd0b8
YQ
4580 CORE_ADDR regval
4581 = extract_unsigned_integer (val, partial_len, byte_order);
2dd604e7 4582
90445bd3 4583 if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
2dd604e7
RE
4584 {
4585 /* The argument is being passed in a general purpose
4586 register. */
e17a4113 4587 if (byte_order == BFD_ENDIAN_BIG)
f0452268 4588 regval <<= (ARM_INT_REGISTER_SIZE - partial_len) * 8;
7cb6d92a
SM
4589
4590 arm_debug_printf ("arg %d in %s = 0x%s", argnum,
4591 gdbarch_register_name (gdbarch, argreg),
4592 phex (regval, ARM_INT_REGISTER_SIZE));
4593
2dd604e7
RE
4594 regcache_cooked_write_unsigned (regcache, argreg, regval);
4595 argreg++;
4596 }
4597 else
4598 {
f0452268 4599 gdb_byte buf[ARM_INT_REGISTER_SIZE];
ef9bd0b8
YQ
4600
4601 memset (buf, 0, sizeof (buf));
4602 store_unsigned_integer (buf, partial_len, byte_order, regval);
4603
2dd604e7 4604 /* Push the arguments onto the stack. */
7cb6d92a 4605 arm_debug_printf ("arg %d @ sp + %d", argnum, nstack);
f0452268
AH
4606 si = push_stack_item (si, buf, ARM_INT_REGISTER_SIZE);
4607 nstack += ARM_INT_REGISTER_SIZE;
2dd604e7
RE
4608 }
4609
4610 len -= partial_len;
4611 val += partial_len;
4612 }
4613 }
4614 /* If we have an odd number of words to push, then decrement the stack
4615 by one word now, so first stack argument will be dword aligned. */
4616 if (nstack & 4)
4617 sp -= 4;
4618
4619 while (si)
4620 {
4621 sp -= si->len;
4622 write_memory (sp, si->data, si->len);
4623 si = pop_stack_item (si);
4624 }
4625
4626 /* Finally, update teh SP register. */
4627 regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
4628
4629 return sp;
4630}
4631
f53f0d0b
PB
4632
4633/* Always align the frame to an 8-byte boundary. This is required on
4634 some platforms and harmless on the rest. */
4635
4636static CORE_ADDR
4637arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
4638{
4639 /* Align the stack to eight bytes. */
4640 return sp & ~ (CORE_ADDR) 7;
4641}
4642
c906108c 4643static void
12b27276 4644print_fpu_flags (struct ui_file *file, int flags)
c906108c 4645{
c5aa993b 4646 if (flags & (1 << 0))
0426ad51 4647 gdb_puts ("IVO ", file);
c5aa993b 4648 if (flags & (1 << 1))
0426ad51 4649 gdb_puts ("DVZ ", file);
c5aa993b 4650 if (flags & (1 << 2))
0426ad51 4651 gdb_puts ("OFL ", file);
c5aa993b 4652 if (flags & (1 << 3))
0426ad51 4653 gdb_puts ("UFL ", file);
c5aa993b 4654 if (flags & (1 << 4))
0426ad51 4655 gdb_puts ("INX ", file);
a11ac3b3 4656 gdb_putc ('\n', file);
c906108c
SS
4657}
4658
5e74b15c
RE
4659/* Print interesting information about the floating point processor
4660 (if present) or emulator. */
34e8f22d 4661static void
d855c300 4662arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
23e3a7ac 4663 struct frame_info *frame, const char *args)
c906108c 4664{
9c9acae0 4665 unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
c5aa993b
JM
4666 int type;
4667
4668 type = (status >> 24) & 127;
edefbb7c 4669 if (status & (1 << 31))
6cb06a8c 4670 gdb_printf (file, _("Hardware FPU type %d\n"), type);
edefbb7c 4671 else
6cb06a8c 4672 gdb_printf (file, _("Software FPU type %d\n"), type);
edefbb7c 4673 /* i18n: [floating point unit] mask */
0426ad51 4674 gdb_puts (_("mask: "), file);
12b27276 4675 print_fpu_flags (file, status >> 16);
edefbb7c 4676 /* i18n: [floating point unit] flags */
0426ad51 4677 gdb_puts (_("flags: "), file);
12b27276 4678 print_fpu_flags (file, status);
c906108c
SS
4679}
4680
27067745
UW
4681/* Construct the ARM extended floating point type. */
4682static struct type *
4683arm_ext_type (struct gdbarch *gdbarch)
4684{
08106042 4685 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
27067745
UW
4686
4687 if (!tdep->arm_ext_type)
4688 tdep->arm_ext_type
e9bb382b 4689 = arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
27067745
UW
4690 floatformats_arm_ext);
4691
4692 return tdep->arm_ext_type;
4693}
4694
58d6951d
DJ
4695static struct type *
4696arm_neon_double_type (struct gdbarch *gdbarch)
4697{
08106042 4698 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d
DJ
4699
4700 if (tdep->neon_double_type == NULL)
4701 {
4702 struct type *t, *elem;
4703
4704 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
4705 TYPE_CODE_UNION);
4706 elem = builtin_type (gdbarch)->builtin_uint8;
4707 append_composite_type_field (t, "u8", init_vector_type (elem, 8));
4708 elem = builtin_type (gdbarch)->builtin_uint16;
4709 append_composite_type_field (t, "u16", init_vector_type (elem, 4));
4710 elem = builtin_type (gdbarch)->builtin_uint32;
4711 append_composite_type_field (t, "u32", init_vector_type (elem, 2));
4712 elem = builtin_type (gdbarch)->builtin_uint64;
4713 append_composite_type_field (t, "u64", elem);
4714 elem = builtin_type (gdbarch)->builtin_float;
4715 append_composite_type_field (t, "f32", init_vector_type (elem, 2));
4716 elem = builtin_type (gdbarch)->builtin_double;
4717 append_composite_type_field (t, "f64", elem);
4718
2062087b 4719 t->set_is_vector (true);
d0e39ea2 4720 t->set_name ("neon_d");
58d6951d
DJ
4721 tdep->neon_double_type = t;
4722 }
4723
4724 return tdep->neon_double_type;
4725}
4726
4727/* FIXME: The vector types are not correctly ordered on big-endian
4728 targets. Just as s0 is the low bits of d0, d0[0] is also the low
4729 bits of d0 - regardless of what unit size is being held in d0. So
4730 the offset of the first uint8 in d0 is 7, but the offset of the
4731 first float is 4. This code works as-is for little-endian
4732 targets. */
4733
4734static struct type *
4735arm_neon_quad_type (struct gdbarch *gdbarch)
4736{
08106042 4737 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d
DJ
4738
4739 if (tdep->neon_quad_type == NULL)
4740 {
4741 struct type *t, *elem;
4742
4743 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
4744 TYPE_CODE_UNION);
4745 elem = builtin_type (gdbarch)->builtin_uint8;
4746 append_composite_type_field (t, "u8", init_vector_type (elem, 16));
4747 elem = builtin_type (gdbarch)->builtin_uint16;
4748 append_composite_type_field (t, "u16", init_vector_type (elem, 8));
4749 elem = builtin_type (gdbarch)->builtin_uint32;
4750 append_composite_type_field (t, "u32", init_vector_type (elem, 4));
4751 elem = builtin_type (gdbarch)->builtin_uint64;
4752 append_composite_type_field (t, "u64", init_vector_type (elem, 2));
4753 elem = builtin_type (gdbarch)->builtin_float;
4754 append_composite_type_field (t, "f32", init_vector_type (elem, 4));
4755 elem = builtin_type (gdbarch)->builtin_double;
4756 append_composite_type_field (t, "f64", init_vector_type (elem, 2));
4757
2062087b 4758 t->set_is_vector (true);
d0e39ea2 4759 t->set_name ("neon_q");
58d6951d
DJ
4760 tdep->neon_quad_type = t;
4761 }
4762
4763 return tdep->neon_quad_type;
4764}
4765
ecbf5d4f
LM
4766/* Return true if REGNUM is a Q pseudo register. Return false
4767 otherwise.
4768
4769 REGNUM is the raw register number and not a pseudo-relative register
4770 number. */
4771
4772static bool
4773is_q_pseudo (struct gdbarch *gdbarch, int regnum)
4774{
08106042 4775 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ecbf5d4f 4776
ae66a8f1
SP
4777 /* Q pseudo registers are available for both NEON (Q0~Q15) and
4778 MVE (Q0~Q7) features. */
ecbf5d4f
LM
4779 if (tdep->have_q_pseudos
4780 && regnum >= tdep->q_pseudo_base
4781 && regnum < (tdep->q_pseudo_base + tdep->q_pseudo_count))
4782 return true;
4783
4784 return false;
4785}
4786
4787/* Return true if REGNUM is a VFP S pseudo register. Return false
4788 otherwise.
4789
4790 REGNUM is the raw register number and not a pseudo-relative register
4791 number. */
4792
4793static bool
4794is_s_pseudo (struct gdbarch *gdbarch, int regnum)
4795{
08106042 4796 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ecbf5d4f
LM
4797
4798 if (tdep->have_s_pseudos
4799 && regnum >= tdep->s_pseudo_base
4800 && regnum < (tdep->s_pseudo_base + tdep->s_pseudo_count))
4801 return true;
4802
4803 return false;
4804}
4805
ae66a8f1
SP
4806/* Return true if REGNUM is a MVE pseudo register (P0). Return false
4807 otherwise.
4808
4809 REGNUM is the raw register number and not a pseudo-relative register
4810 number. */
4811
4812static bool
4813is_mve_pseudo (struct gdbarch *gdbarch, int regnum)
4814{
08106042 4815 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ae66a8f1
SP
4816
4817 if (tdep->have_mve
4818 && regnum >= tdep->mve_pseudo_base
4819 && regnum < tdep->mve_pseudo_base + tdep->mve_pseudo_count)
4820 return true;
4821
4822 return false;
4823}
4824
a01567f4
LM
4825/* Return true if REGNUM is a PACBTI pseudo register (ra_auth_code). Return
4826 false otherwise.
4827
4828 REGNUM is the raw register number and not a pseudo-relative register
4829 number. */
4830
4831static bool
4832is_pacbti_pseudo (struct gdbarch *gdbarch, int regnum)
4833{
08106042 4834 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
a01567f4
LM
4835
4836 if (tdep->have_pacbti
4837 && regnum >= tdep->pacbti_pseudo_base
4838 && regnum < tdep->pacbti_pseudo_base + tdep->pacbti_pseudo_count)
4839 return true;
4840
4841 return false;
4842}
4843
34e8f22d
RE
4844/* Return the GDB type object for the "standard" data type of data in
4845 register N. */
4846
4847static struct type *
7a5ea0d4 4848arm_register_type (struct gdbarch *gdbarch, int regnum)
032758dc 4849{
08106042 4850 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d 4851
ecbf5d4f 4852 if (is_s_pseudo (gdbarch, regnum))
58d6951d
DJ
4853 return builtin_type (gdbarch)->builtin_float;
4854
ecbf5d4f 4855 if (is_q_pseudo (gdbarch, regnum))
58d6951d
DJ
4856 return arm_neon_quad_type (gdbarch);
4857
ae66a8f1
SP
4858 if (is_mve_pseudo (gdbarch, regnum))
4859 return builtin_type (gdbarch)->builtin_int16;
4860
a01567f4
LM
4861 if (is_pacbti_pseudo (gdbarch, regnum))
4862 return builtin_type (gdbarch)->builtin_uint32;
4863
58d6951d
DJ
4864 /* If the target description has register information, we are only
4865 in this function so that we can override the types of
4866 double-precision registers for NEON. */
4867 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
4868 {
4869 struct type *t = tdesc_register_type (gdbarch, regnum);
4870
4871 if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
78134374 4872 && t->code () == TYPE_CODE_FLT
ecbf5d4f 4873 && tdep->have_neon)
58d6951d
DJ
4874 return arm_neon_double_type (gdbarch);
4875 else
4876 return t;
4877 }
4878
34e8f22d 4879 if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
58d6951d 4880 {
ecbf5d4f 4881 if (!tdep->have_fpa_registers)
58d6951d
DJ
4882 return builtin_type (gdbarch)->builtin_void;
4883
4884 return arm_ext_type (gdbarch);
4885 }
e4c16157 4886 else if (regnum == ARM_SP_REGNUM)
0dfff4cb 4887 return builtin_type (gdbarch)->builtin_data_ptr;
e4c16157 4888 else if (regnum == ARM_PC_REGNUM)
0dfff4cb 4889 return builtin_type (gdbarch)->builtin_func_ptr;
ff6f572f
DJ
4890 else if (regnum >= ARRAY_SIZE (arm_register_names))
4891 /* These registers are only supported on targets which supply
4892 an XML description. */
df4df182 4893 return builtin_type (gdbarch)->builtin_int0;
032758dc 4894 else
df4df182 4895 return builtin_type (gdbarch)->builtin_uint32;
032758dc
AC
4896}
4897
ff6f572f
DJ
4898/* Map a DWARF register REGNUM onto the appropriate GDB register
4899 number. */
4900
4901static int
d3f73121 4902arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
ff6f572f
DJ
4903{
4904 /* Core integer regs. */
4905 if (reg >= 0 && reg <= 15)
4906 return reg;
4907
4908 /* Legacy FPA encoding. These were once used in a way which
4909 overlapped with VFP register numbering, so their use is
4910 discouraged, but GDB doesn't support the ARM toolchain
4911 which used them for VFP. */
4912 if (reg >= 16 && reg <= 23)
4913 return ARM_F0_REGNUM + reg - 16;
4914
4915 /* New assignments for the FPA registers. */
4916 if (reg >= 96 && reg <= 103)
4917 return ARM_F0_REGNUM + reg - 96;
4918
4919 /* WMMX register assignments. */
4920 if (reg >= 104 && reg <= 111)
4921 return ARM_WCGR0_REGNUM + reg - 104;
4922
4923 if (reg >= 112 && reg <= 127)
4924 return ARM_WR0_REGNUM + reg - 112;
4925
a01567f4
LM
4926 /* PACBTI register containing the Pointer Authentication Code. */
4927 if (reg == ARM_DWARF_RA_AUTH_CODE)
4928 {
08106042 4929 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
a01567f4
LM
4930
4931 if (tdep->have_pacbti)
4932 return tdep->pacbti_pseudo_base;
4933
4934 return -1;
4935 }
4936
ff6f572f
DJ
4937 if (reg >= 192 && reg <= 199)
4938 return ARM_WC0_REGNUM + reg - 192;
4939
58d6951d
DJ
4940 /* VFP v2 registers. A double precision value is actually
4941 in d1 rather than s2, but the ABI only defines numbering
4942 for the single precision registers. This will "just work"
4943 in GDB for little endian targets (we'll read eight bytes,
4944 starting in s0 and then progressing to s1), but will be
4945 reversed on big endian targets with VFP. This won't
4946 be a problem for the new Neon quad registers; you're supposed
4947 to use DW_OP_piece for those. */
4948 if (reg >= 64 && reg <= 95)
4949 {
4950 char name_buf[4];
4951
8c042590 4952 xsnprintf (name_buf, sizeof (name_buf), "s%d", reg - 64);
58d6951d
DJ
4953 return user_reg_map_name_to_regnum (gdbarch, name_buf,
4954 strlen (name_buf));
4955 }
4956
4957 /* VFP v3 / Neon registers. This range is also used for VFP v2
4958 registers, except that it now describes d0 instead of s0. */
4959 if (reg >= 256 && reg <= 287)
4960 {
4961 char name_buf[4];
4962
8c042590 4963 xsnprintf (name_buf, sizeof (name_buf), "d%d", reg - 256);
58d6951d
DJ
4964 return user_reg_map_name_to_regnum (gdbarch, name_buf,
4965 strlen (name_buf));
4966 }
4967
ff6f572f
DJ
4968 return -1;
4969}
4970
26216b98
AC
4971/* Map GDB internal REGNUM onto the Arm simulator register numbers. */
4972static int
e7faf938 4973arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
26216b98
AC
4974{
4975 int reg = regnum;
e7faf938 4976 gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
26216b98 4977
ff6f572f
DJ
4978 if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
4979 return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
4980
4981 if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
4982 return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
4983
4984 if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
4985 return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
4986
26216b98
AC
4987 if (reg < NUM_GREGS)
4988 return SIM_ARM_R0_REGNUM + reg;
4989 reg -= NUM_GREGS;
4990
4991 if (reg < NUM_FREGS)
4992 return SIM_ARM_FP0_REGNUM + reg;
4993 reg -= NUM_FREGS;
4994
4995 if (reg < NUM_SREGS)
4996 return SIM_ARM_FPS_REGNUM + reg;
4997 reg -= NUM_SREGS;
4998
edefbb7c 4999 internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
26216b98 5000}
34e8f22d 5001
a01567f4
LM
5002static const unsigned char op_lit0 = DW_OP_lit0;
5003
5004static void
5005arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
5006 struct dwarf2_frame_state_reg *reg,
5007 struct frame_info *this_frame)
5008{
a6e4a48c
YR
5009 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
5010
a01567f4
LM
5011 if (is_pacbti_pseudo (gdbarch, regnum))
5012 {
5013 /* Initialize RA_AUTH_CODE to zero. */
5014 reg->how = DWARF2_FRAME_REG_SAVED_VAL_EXP;
5015 reg->loc.exp.start = &op_lit0;
5016 reg->loc.exp.len = 1;
5017 return;
5018 }
5019
42e11f36 5020 if (regnum == ARM_PC_REGNUM || regnum == ARM_PS_REGNUM)
a01567f4 5021 {
a01567f4
LM
5022 reg->how = DWARF2_FRAME_REG_FN;
5023 reg->loc.fn = arm_dwarf2_prev_register;
a01567f4 5024 }
42e11f36
TS
5025 else if (regnum == ARM_SP_REGNUM)
5026 reg->how = DWARF2_FRAME_REG_CFA;
a6e4a48c
YR
5027 else if (arm_is_alternative_sp_register (tdep, regnum))
5028 {
5029 /* Handle the alternative SP registers on Cortex-M. */
5030 reg->how = DWARF2_FRAME_REG_FN;
5031 reg->loc.fn = arm_dwarf2_prev_register;
5032 }
a01567f4
LM
5033}
5034
d9311bfa
AT
5035/* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
5036 the buffer to be NEW_LEN bytes ending at ENDADDR. Return
5037 NULL if an error occurs. BUF is freed. */
c906108c 5038
d9311bfa
AT
5039static gdb_byte *
5040extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
5041 int old_len, int new_len)
5042{
5043 gdb_byte *new_buf;
5044 int bytes_to_read = new_len - old_len;
c906108c 5045
d9311bfa
AT
5046 new_buf = (gdb_byte *) xmalloc (new_len);
5047 memcpy (new_buf + bytes_to_read, buf, old_len);
5048 xfree (buf);
198cd59d 5049 if (target_read_code (endaddr - new_len, new_buf, bytes_to_read) != 0)
d9311bfa
AT
5050 {
5051 xfree (new_buf);
5052 return NULL;
c906108c 5053 }
d9311bfa 5054 return new_buf;
c906108c
SS
5055}
5056
d9311bfa
AT
5057/* An IT block is at most the 2-byte IT instruction followed by
5058 four 4-byte instructions. The furthest back we must search to
5059 find an IT block that affects the current instruction is thus
5060 2 + 3 * 4 == 14 bytes. */
5061#define MAX_IT_BLOCK_PREFIX 14
177321bd 5062
d9311bfa
AT
5063/* Use a quick scan if there are more than this many bytes of
5064 code. */
5065#define IT_SCAN_THRESHOLD 32
177321bd 5066
d9311bfa
AT
5067/* Adjust a breakpoint's address to move breakpoints out of IT blocks.
5068 A breakpoint in an IT block may not be hit, depending on the
5069 condition flags. */
ad527d2e 5070static CORE_ADDR
d9311bfa 5071arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
c906108c 5072{
d9311bfa
AT
5073 gdb_byte *buf;
5074 char map_type;
5075 CORE_ADDR boundary, func_start;
5076 int buf_len;
5077 enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
5078 int i, any, last_it, last_it_count;
08106042 5079 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
177321bd 5080
d9311bfa 5081 /* If we are using BKPT breakpoints, none of this is necessary. */
345bd07c 5082 if (tdep->thumb2_breakpoint == NULL)
d9311bfa 5083 return bpaddr;
177321bd 5084
d9311bfa
AT
5085 /* ARM mode does not have this problem. */
5086 if (!arm_pc_is_thumb (gdbarch, bpaddr))
5087 return bpaddr;
177321bd 5088
d9311bfa
AT
5089 /* We are setting a breakpoint in Thumb code that could potentially
5090 contain an IT block. The first step is to find how much Thumb
5091 code there is; we do not need to read outside of known Thumb
5092 sequences. */
5093 map_type = arm_find_mapping_symbol (bpaddr, &boundary);
5094 if (map_type == 0)
5095 /* Thumb-2 code must have mapping symbols to have a chance. */
5096 return bpaddr;
9dca5578 5097
d9311bfa 5098 bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
177321bd 5099
d9311bfa
AT
5100 if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
5101 && func_start > boundary)
5102 boundary = func_start;
9dca5578 5103
d9311bfa
AT
5104 /* Search for a candidate IT instruction. We have to do some fancy
5105 footwork to distinguish a real IT instruction from the second
5106 half of a 32-bit instruction, but there is no need for that if
5107 there's no candidate. */
325fac50 5108 buf_len = std::min (bpaddr - boundary, (CORE_ADDR) MAX_IT_BLOCK_PREFIX);
d9311bfa
AT
5109 if (buf_len == 0)
5110 /* No room for an IT instruction. */
5111 return bpaddr;
c906108c 5112
d9311bfa 5113 buf = (gdb_byte *) xmalloc (buf_len);
198cd59d 5114 if (target_read_code (bpaddr - buf_len, buf, buf_len) != 0)
d9311bfa
AT
5115 return bpaddr;
5116 any = 0;
5117 for (i = 0; i < buf_len; i += 2)
c906108c 5118 {
d9311bfa
AT
5119 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5120 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
25b41d01 5121 {
d9311bfa
AT
5122 any = 1;
5123 break;
25b41d01 5124 }
c906108c 5125 }
d9311bfa
AT
5126
5127 if (any == 0)
c906108c 5128 {
d9311bfa
AT
5129 xfree (buf);
5130 return bpaddr;
f9d67f43
DJ
5131 }
5132
5133 /* OK, the code bytes before this instruction contain at least one
5134 halfword which resembles an IT instruction. We know that it's
5135 Thumb code, but there are still two possibilities. Either the
5136 halfword really is an IT instruction, or it is the second half of
5137 a 32-bit Thumb instruction. The only way we can tell is to
5138 scan forwards from a known instruction boundary. */
5139 if (bpaddr - boundary > IT_SCAN_THRESHOLD)
5140 {
5141 int definite;
5142
5143 /* There's a lot of code before this instruction. Start with an
5144 optimistic search; it's easy to recognize halfwords that can
5145 not be the start of a 32-bit instruction, and use that to
5146 lock on to the instruction boundaries. */
5147 buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
5148 if (buf == NULL)
5149 return bpaddr;
5150 buf_len = IT_SCAN_THRESHOLD;
5151
5152 definite = 0;
5153 for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
5154 {
5155 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5156 if (thumb_insn_size (inst1) == 2)
5157 {
5158 definite = 1;
5159 break;
5160 }
5161 }
5162
5163 /* At this point, if DEFINITE, BUF[I] is the first place we
5164 are sure that we know the instruction boundaries, and it is far
5165 enough from BPADDR that we could not miss an IT instruction
5166 affecting BPADDR. If ! DEFINITE, give up - start from a
5167 known boundary. */
5168 if (! definite)
5169 {
0963b4bd
MS
5170 buf = extend_buffer_earlier (buf, bpaddr, buf_len,
5171 bpaddr - boundary);
f9d67f43
DJ
5172 if (buf == NULL)
5173 return bpaddr;
5174 buf_len = bpaddr - boundary;
5175 i = 0;
5176 }
5177 }
5178 else
5179 {
5180 buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
5181 if (buf == NULL)
5182 return bpaddr;
5183 buf_len = bpaddr - boundary;
5184 i = 0;
5185 }
5186
5187 /* Scan forwards. Find the last IT instruction before BPADDR. */
5188 last_it = -1;
5189 last_it_count = 0;
5190 while (i < buf_len)
5191 {
5192 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5193 last_it_count--;
5194 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5195 {
5196 last_it = i;
5197 if (inst1 & 0x0001)
5198 last_it_count = 4;
5199 else if (inst1 & 0x0002)
5200 last_it_count = 3;
5201 else if (inst1 & 0x0004)
5202 last_it_count = 2;
5203 else
5204 last_it_count = 1;
5205 }
5206 i += thumb_insn_size (inst1);
5207 }
5208
5209 xfree (buf);
5210
5211 if (last_it == -1)
5212 /* There wasn't really an IT instruction after all. */
5213 return bpaddr;
5214
5215 if (last_it_count < 1)
5216 /* It was too far away. */
5217 return bpaddr;
5218
5219 /* This really is a trouble spot. Move the breakpoint to the IT
5220 instruction. */
5221 return bpaddr - buf_len + last_it;
5222}
5223
cca44b1b 5224/* ARM displaced stepping support.
c906108c 5225
cca44b1b 5226 Generally ARM displaced stepping works as follows:
c906108c 5227
cca44b1b 5228 1. When an instruction is to be single-stepped, it is first decoded by
2ba163c8
SM
5229 arm_process_displaced_insn. Depending on the type of instruction, it is
5230 then copied to a scratch location, possibly in a modified form. The
5231 copy_* set of functions performs such modification, as necessary. A
5232 breakpoint is placed after the modified instruction in the scratch space
5233 to return control to GDB. Note in particular that instructions which
5234 modify the PC will no longer do so after modification.
c5aa993b 5235
cca44b1b
JB
5236 2. The instruction is single-stepped, by setting the PC to the scratch
5237 location address, and resuming. Control returns to GDB when the
5238 breakpoint is hit.
c5aa993b 5239
cca44b1b
JB
5240 3. A cleanup function (cleanup_*) is called corresponding to the copy_*
5241 function used for the current instruction. This function's job is to
5242 put the CPU/memory state back to what it would have been if the
5243 instruction had been executed unmodified in its original location. */
c5aa993b 5244
cca44b1b
JB
5245/* NOP instruction (mov r0, r0). */
5246#define ARM_NOP 0xe1a00000
34518530 5247#define THUMB_NOP 0x4600
cca44b1b
JB
5248
5249/* Helper for register reads for displaced stepping. In particular, this
5250 returns the PC as it would be seen by the instruction at its original
5251 location. */
5252
5253ULONGEST
1152d984 5254displaced_read_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
36073a92 5255 int regno)
cca44b1b
JB
5256{
5257 ULONGEST ret;
36073a92 5258 CORE_ADDR from = dsc->insn_addr;
cca44b1b 5259
bf9f652a 5260 if (regno == ARM_PC_REGNUM)
cca44b1b 5261 {
4db71c0b
YQ
5262 /* Compute pipeline offset:
5263 - When executing an ARM instruction, PC reads as the address of the
5264 current instruction plus 8.
5265 - When executing a Thumb instruction, PC reads as the address of the
5266 current instruction plus 4. */
5267
36073a92 5268 if (!dsc->is_thumb)
4db71c0b
YQ
5269 from += 8;
5270 else
5271 from += 4;
5272
136821d9
SM
5273 displaced_debug_printf ("read pc value %.8lx",
5274 (unsigned long) from);
4db71c0b 5275 return (ULONGEST) from;
cca44b1b 5276 }
c906108c 5277 else
cca44b1b
JB
5278 {
5279 regcache_cooked_read_unsigned (regs, regno, &ret);
136821d9
SM
5280
5281 displaced_debug_printf ("read r%d value %.8lx",
5282 regno, (unsigned long) ret);
5283
cca44b1b
JB
5284 return ret;
5285 }
c906108c
SS
5286}
5287
cca44b1b
JB
5288static int
5289displaced_in_arm_mode (struct regcache *regs)
5290{
5291 ULONGEST ps;
ac7936df 5292 ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
66e810cd 5293
cca44b1b 5294 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
66e810cd 5295
9779414d 5296 return (ps & t_bit) == 0;
cca44b1b 5297}
66e810cd 5298
cca44b1b 5299/* Write to the PC as from a branch instruction. */
c906108c 5300
cca44b1b 5301static void
1152d984 5302branch_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
36073a92 5303 ULONGEST val)
c906108c 5304{
36073a92 5305 if (!dsc->is_thumb)
cca44b1b
JB
5306 /* Note: If bits 0/1 are set, this branch would be unpredictable for
5307 architecture versions < 6. */
0963b4bd
MS
5308 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5309 val & ~(ULONGEST) 0x3);
cca44b1b 5310 else
0963b4bd
MS
5311 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5312 val & ~(ULONGEST) 0x1);
cca44b1b 5313}
66e810cd 5314
cca44b1b
JB
5315/* Write to the PC as from a branch-exchange instruction. */
5316
5317static void
5318bx_write_pc (struct regcache *regs, ULONGEST val)
5319{
5320 ULONGEST ps;
ac7936df 5321 ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
cca44b1b
JB
5322
5323 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
5324
5325 if ((val & 1) == 1)
c906108c 5326 {
9779414d 5327 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
cca44b1b
JB
5328 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
5329 }
5330 else if ((val & 2) == 0)
5331 {
9779414d 5332 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 5333 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
c906108c
SS
5334 }
5335 else
5336 {
cca44b1b
JB
5337 /* Unpredictable behaviour. Try to do something sensible (switch to ARM
5338 mode, align dest to 4 bytes). */
5339 warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
9779414d 5340 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 5341 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
c906108c
SS
5342 }
5343}
ed9a39eb 5344
cca44b1b 5345/* Write to the PC as if from a load instruction. */
ed9a39eb 5346
34e8f22d 5347static void
1152d984 5348load_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
36073a92 5349 ULONGEST val)
ed9a39eb 5350{
cca44b1b
JB
5351 if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
5352 bx_write_pc (regs, val);
5353 else
36073a92 5354 branch_write_pc (regs, dsc, val);
cca44b1b 5355}
be8626e0 5356
cca44b1b
JB
5357/* Write to the PC as if from an ALU instruction. */
5358
5359static void
1152d984 5360alu_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
36073a92 5361 ULONGEST val)
cca44b1b 5362{
36073a92 5363 if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
cca44b1b
JB
5364 bx_write_pc (regs, val);
5365 else
36073a92 5366 branch_write_pc (regs, dsc, val);
cca44b1b
JB
5367}
5368
5369/* Helper for writing to registers for displaced stepping. Writing to the PC
5370 has a varying effects depending on the instruction which does the write:
5371 this is controlled by the WRITE_PC argument. */
5372
5373void
1152d984 5374displaced_write_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
cca44b1b
JB
5375 int regno, ULONGEST val, enum pc_write_style write_pc)
5376{
bf9f652a 5377 if (regno == ARM_PC_REGNUM)
08216dd7 5378 {
136821d9
SM
5379 displaced_debug_printf ("writing pc %.8lx", (unsigned long) val);
5380
cca44b1b 5381 switch (write_pc)
08216dd7 5382 {
cca44b1b 5383 case BRANCH_WRITE_PC:
36073a92 5384 branch_write_pc (regs, dsc, val);
08216dd7
RE
5385 break;
5386
cca44b1b
JB
5387 case BX_WRITE_PC:
5388 bx_write_pc (regs, val);
24b21115 5389 break;
cca44b1b
JB
5390
5391 case LOAD_WRITE_PC:
36073a92 5392 load_write_pc (regs, dsc, val);
24b21115 5393 break;
cca44b1b
JB
5394
5395 case ALU_WRITE_PC:
36073a92 5396 alu_write_pc (regs, dsc, val);
24b21115 5397 break;
cca44b1b
JB
5398
5399 case CANNOT_WRITE_PC:
5400 warning (_("Instruction wrote to PC in an unexpected way when "
5401 "single-stepping"));
08216dd7
RE
5402 break;
5403
5404 default:
97b9747c
JB
5405 internal_error (__FILE__, __LINE__,
5406 _("Invalid argument to displaced_write_reg"));
08216dd7 5407 }
b508a996 5408
cca44b1b 5409 dsc->wrote_to_pc = 1;
b508a996 5410 }
ed9a39eb 5411 else
b508a996 5412 {
136821d9
SM
5413 displaced_debug_printf ("writing r%d value %.8lx",
5414 regno, (unsigned long) val);
cca44b1b 5415 regcache_cooked_write_unsigned (regs, regno, val);
b508a996 5416 }
34e8f22d
RE
5417}
5418
cca44b1b
JB
5419/* This function is used to concisely determine if an instruction INSN
5420 references PC. Register fields of interest in INSN should have the
0963b4bd
MS
5421 corresponding fields of BITMASK set to 0b1111. The function
5422 returns return 1 if any of these fields in INSN reference the PC
5423 (also 0b1111, r15), else it returns 0. */
67255d04
RE
5424
5425static int
cca44b1b 5426insn_references_pc (uint32_t insn, uint32_t bitmask)
67255d04 5427{
cca44b1b 5428 uint32_t lowbit = 1;
67255d04 5429
cca44b1b
JB
5430 while (bitmask != 0)
5431 {
5432 uint32_t mask;
44e1a9eb 5433
cca44b1b
JB
5434 for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
5435 ;
67255d04 5436
cca44b1b
JB
5437 if (!lowbit)
5438 break;
67255d04 5439
cca44b1b 5440 mask = lowbit * 0xf;
67255d04 5441
cca44b1b
JB
5442 if ((insn & mask) == mask)
5443 return 1;
5444
5445 bitmask &= ~mask;
67255d04
RE
5446 }
5447
cca44b1b
JB
5448 return 0;
5449}
2af48f68 5450
cca44b1b
JB
5451/* The simplest copy function. Many instructions have the same effect no
5452 matter what address they are executed at: in those cases, use this. */
67255d04 5453
cca44b1b 5454static int
1152d984
SM
5455arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn, const char *iname,
5456 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 5457{
136821d9
SM
5458 displaced_debug_printf ("copying insn %.8lx, opcode/class '%s' unmodified",
5459 (unsigned long) insn, iname);
67255d04 5460
cca44b1b 5461 dsc->modinsn[0] = insn;
67255d04 5462
cca44b1b
JB
5463 return 0;
5464}
5465
34518530
YQ
5466static int
5467thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
5468 uint16_t insn2, const char *iname,
1152d984 5469 arm_displaced_step_copy_insn_closure *dsc)
34518530 5470{
136821d9
SM
5471 displaced_debug_printf ("copying insn %.4x %.4x, opcode/class '%s' "
5472 "unmodified", insn1, insn2, iname);
34518530
YQ
5473
5474 dsc->modinsn[0] = insn1;
5475 dsc->modinsn[1] = insn2;
5476 dsc->numinsns = 2;
5477
5478 return 0;
5479}
5480
5481/* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
5482 modification. */
5483static int
615234c1 5484thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, uint16_t insn,
34518530 5485 const char *iname,
1152d984 5486 arm_displaced_step_copy_insn_closure *dsc)
34518530 5487{
136821d9
SM
5488 displaced_debug_printf ("copying insn %.4x, opcode/class '%s' unmodified",
5489 insn, iname);
34518530
YQ
5490
5491 dsc->modinsn[0] = insn;
5492
5493 return 0;
5494}
5495
cca44b1b
JB
5496/* Preload instructions with immediate offset. */
5497
5498static void
1152d984
SM
5499cleanup_preload (struct gdbarch *gdbarch, regcache *regs,
5500 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
5501{
5502 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5503 if (!dsc->u.preload.immed)
5504 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5505}
5506
7ff120b4
YQ
5507static void
5508install_preload (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5509 arm_displaced_step_copy_insn_closure *dsc, unsigned int rn)
cca44b1b 5510{
cca44b1b 5511 ULONGEST rn_val;
cca44b1b
JB
5512 /* Preload instructions:
5513
5514 {pli/pld} [rn, #+/-imm]
5515 ->
5516 {pli/pld} [r0, #+/-imm]. */
5517
36073a92
YQ
5518 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5519 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 5520 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
cca44b1b
JB
5521 dsc->u.preload.immed = 1;
5522
cca44b1b 5523 dsc->cleanup = &cleanup_preload;
cca44b1b
JB
5524}
5525
cca44b1b 5526static int
7ff120b4 5527arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
1152d984 5528 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
5529{
5530 unsigned int rn = bits (insn, 16, 19);
cca44b1b 5531
7ff120b4
YQ
5532 if (!insn_references_pc (insn, 0x000f0000ul))
5533 return arm_copy_unmodified (gdbarch, insn, "preload", dsc);
cca44b1b 5534
136821d9 5535 displaced_debug_printf ("copying preload insn %.8lx", (unsigned long) insn);
cca44b1b 5536
7ff120b4
YQ
5537 dsc->modinsn[0] = insn & 0xfff0ffff;
5538
5539 install_preload (gdbarch, regs, dsc, rn);
5540
5541 return 0;
5542}
5543
34518530
YQ
5544static int
5545thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
1152d984 5546 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
5547{
5548 unsigned int rn = bits (insn1, 0, 3);
5549 unsigned int u_bit = bit (insn1, 7);
5550 int imm12 = bits (insn2, 0, 11);
5551 ULONGEST pc_val;
5552
5553 if (rn != ARM_PC_REGNUM)
5554 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);
5555
5556 /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
5557 PLD (literal) Encoding T1. */
136821d9
SM
5558 displaced_debug_printf ("copying pld/pli pc (0x%x) %c imm12 %.4x",
5559 (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
5560 imm12);
34518530
YQ
5561
5562 if (!u_bit)
5563 imm12 = -1 * imm12;
5564
5565 /* Rewrite instruction {pli/pld} PC imm12 into:
5566 Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12
5567
5568 {pli/pld} [r0, r1]
5569
5570 Cleanup: r0 <- tmp[0], r1 <- tmp[1]. */
5571
5572 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5573 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5574
5575 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
5576
5577 displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
5578 displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
5579 dsc->u.preload.immed = 0;
5580
5581 /* {pli/pld} [r0, r1] */
5582 dsc->modinsn[0] = insn1 & 0xfff0;
5583 dsc->modinsn[1] = 0xf001;
5584 dsc->numinsns = 2;
5585
5586 dsc->cleanup = &cleanup_preload;
5587 return 0;
5588}
5589
7ff120b4
YQ
5590/* Preload instructions with register offset. */
5591
5592static void
5593install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5594 arm_displaced_step_copy_insn_closure *dsc, unsigned int rn,
7ff120b4
YQ
5595 unsigned int rm)
5596{
5597 ULONGEST rn_val, rm_val;
5598
cca44b1b
JB
5599 /* Preload register-offset instructions:
5600
5601 {pli/pld} [rn, rm {, shift}]
5602 ->
5603 {pli/pld} [r0, r1 {, shift}]. */
5604
36073a92
YQ
5605 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5606 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5607 rn_val = displaced_read_reg (regs, dsc, rn);
5608 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5609 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5610 displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
cca44b1b
JB
5611 dsc->u.preload.immed = 0;
5612
cca44b1b 5613 dsc->cleanup = &cleanup_preload;
7ff120b4
YQ
5614}
5615
5616static int
5617arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
5618 struct regcache *regs,
1152d984 5619 arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
5620{
5621 unsigned int rn = bits (insn, 16, 19);
5622 unsigned int rm = bits (insn, 0, 3);
5623
5624
5625 if (!insn_references_pc (insn, 0x000f000ful))
5626 return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);
5627
136821d9
SM
5628 displaced_debug_printf ("copying preload insn %.8lx",
5629 (unsigned long) insn);
7ff120b4
YQ
5630
5631 dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
cca44b1b 5632
7ff120b4 5633 install_preload_reg (gdbarch, regs, dsc, rn, rm);
cca44b1b
JB
5634 return 0;
5635}
5636
5637/* Copy/cleanup coprocessor load and store instructions. */
5638
5639static void
6e39997a 5640cleanup_copro_load_store (struct gdbarch *gdbarch,
cca44b1b 5641 struct regcache *regs,
1152d984 5642 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 5643{
36073a92 5644 ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5645
5646 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5647
5648 if (dsc->u.ldst.writeback)
5649 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
5650}
5651
7ff120b4
YQ
5652static void
5653install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5654 arm_displaced_step_copy_insn_closure *dsc,
7ff120b4 5655 int writeback, unsigned int rn)
cca44b1b 5656{
cca44b1b 5657 ULONGEST rn_val;
cca44b1b 5658
cca44b1b
JB
5659 /* Coprocessor load/store instructions:
5660
5661 {stc/stc2} [<Rn>, #+/-imm] (and other immediate addressing modes)
5662 ->
5663 {stc/stc2} [r0, #+/-imm].
5664
5665 ldc/ldc2 are handled identically. */
5666
36073a92
YQ
5667 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5668 rn_val = displaced_read_reg (regs, dsc, rn);
2b16b2e3
YQ
5669 /* PC should be 4-byte aligned. */
5670 rn_val = rn_val & 0xfffffffc;
cca44b1b
JB
5671 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5672
7ff120b4 5673 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
5674 dsc->u.ldst.rn = rn;
5675
7ff120b4
YQ
5676 dsc->cleanup = &cleanup_copro_load_store;
5677}
5678
5679static int
5680arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
5681 struct regcache *regs,
1152d984 5682 arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
5683{
5684 unsigned int rn = bits (insn, 16, 19);
5685
5686 if (!insn_references_pc (insn, 0x000f0000ul))
5687 return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);
5688
136821d9
SM
5689 displaced_debug_printf ("copying coprocessor load/store insn %.8lx",
5690 (unsigned long) insn);
7ff120b4 5691
cca44b1b
JB
5692 dsc->modinsn[0] = insn & 0xfff0ffff;
5693
7ff120b4 5694 install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);
cca44b1b
JB
5695
5696 return 0;
5697}
5698
34518530
YQ
5699static int
5700thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
5701 uint16_t insn2, struct regcache *regs,
1152d984 5702 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
5703{
5704 unsigned int rn = bits (insn1, 0, 3);
5705
5706 if (rn != ARM_PC_REGNUM)
5707 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
5708 "copro load/store", dsc);
5709
136821d9
SM
5710 displaced_debug_printf ("copying coprocessor load/store insn %.4x%.4x",
5711 insn1, insn2);
34518530
YQ
5712
5713 dsc->modinsn[0] = insn1 & 0xfff0;
5714 dsc->modinsn[1] = insn2;
5715 dsc->numinsns = 2;
5716
5717 /* This function is called for copying instruction LDC/LDC2/VLDR, which
5718 doesn't support writeback, so pass 0. */
5719 install_copro_load_store (gdbarch, regs, dsc, 0, rn);
5720
5721 return 0;
5722}
5723
cca44b1b
JB
5724/* Clean up branch instructions (actually perform the branch, by setting
5725 PC). */
5726
5727static void
6e39997a 5728cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5729 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 5730{
36073a92 5731 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
5732 int branch_taken = condition_true (dsc->u.branch.cond, status);
5733 enum pc_write_style write_pc = dsc->u.branch.exchange
5734 ? BX_WRITE_PC : BRANCH_WRITE_PC;
5735
5736 if (!branch_taken)
5737 return;
5738
5739 if (dsc->u.branch.link)
5740 {
8c8dba6d 5741 /* The value of LR should be the next insn of current one. In order
85102364 5742 not to confuse logic handling later insn `bx lr', if current insn mode
8c8dba6d
YQ
5743 is Thumb, the bit 0 of LR value should be set to 1. */
5744 ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;
5745
5746 if (dsc->is_thumb)
5747 next_insn_addr |= 0x1;
5748
5749 displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
5750 CANNOT_WRITE_PC);
cca44b1b
JB
5751 }
5752
bf9f652a 5753 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
cca44b1b
JB
5754}
5755
5756/* Copy B/BL/BLX instructions with immediate destinations. */
5757
7ff120b4
YQ
5758static void
5759install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5760 arm_displaced_step_copy_insn_closure *dsc,
7ff120b4
YQ
5761 unsigned int cond, int exchange, int link, long offset)
5762{
5763 /* Implement "BL<cond> <label>" as:
5764
5765 Preparation: cond <- instruction condition
5766 Insn: mov r0, r0 (nop)
5767 Cleanup: if (condition true) { r14 <- pc; pc <- label }.
5768
5769 B<cond> similar, but don't set r14 in cleanup. */
5770
5771 dsc->u.branch.cond = cond;
5772 dsc->u.branch.link = link;
5773 dsc->u.branch.exchange = exchange;
5774
2b16b2e3
YQ
5775 dsc->u.branch.dest = dsc->insn_addr;
5776 if (link && exchange)
5777 /* For BLX, offset is computed from the Align (PC, 4). */
5778 dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;
5779
7ff120b4 5780 if (dsc->is_thumb)
2b16b2e3 5781 dsc->u.branch.dest += 4 + offset;
7ff120b4 5782 else
2b16b2e3 5783 dsc->u.branch.dest += 8 + offset;
7ff120b4
YQ
5784
5785 dsc->cleanup = &cleanup_branch;
5786}
cca44b1b 5787static int
7ff120b4 5788arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
1152d984 5789 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
5790{
5791 unsigned int cond = bits (insn, 28, 31);
5792 int exchange = (cond == 0xf);
5793 int link = exchange || bit (insn, 24);
cca44b1b
JB
5794 long offset;
5795
136821d9
SM
5796 displaced_debug_printf ("copying %s immediate insn %.8lx",
5797 (exchange) ? "blx" : (link) ? "bl" : "b",
5798 (unsigned long) insn);
cca44b1b
JB
5799 if (exchange)
5800 /* For BLX, set bit 0 of the destination. The cleanup_branch function will
5801 then arrange the switch into Thumb mode. */
5802 offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
5803 else
5804 offset = bits (insn, 0, 23) << 2;
5805
5806 if (bit (offset, 25))
5807 offset = offset | ~0x3ffffff;
5808
cca44b1b
JB
5809 dsc->modinsn[0] = ARM_NOP;
5810
7ff120b4 5811 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
cca44b1b
JB
5812 return 0;
5813}
5814
34518530
YQ
5815static int
5816thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
5817 uint16_t insn2, struct regcache *regs,
1152d984 5818 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
5819{
5820 int link = bit (insn2, 14);
5821 int exchange = link && !bit (insn2, 12);
5822 int cond = INST_AL;
5823 long offset = 0;
5824 int j1 = bit (insn2, 13);
5825 int j2 = bit (insn2, 11);
5826 int s = sbits (insn1, 10, 10);
5827 int i1 = !(j1 ^ bit (insn1, 10));
5828 int i2 = !(j2 ^ bit (insn1, 10));
5829
5830 if (!link && !exchange) /* B */
5831 {
5832 offset = (bits (insn2, 0, 10) << 1);
5833 if (bit (insn2, 12)) /* Encoding T4 */
5834 {
5835 offset |= (bits (insn1, 0, 9) << 12)
5836 | (i2 << 22)
5837 | (i1 << 23)
5838 | (s << 24);
5839 cond = INST_AL;
5840 }
5841 else /* Encoding T3 */
5842 {
5843 offset |= (bits (insn1, 0, 5) << 12)
5844 | (j1 << 18)
5845 | (j2 << 19)
5846 | (s << 20);
5847 cond = bits (insn1, 6, 9);
5848 }
5849 }
5850 else
5851 {
5852 offset = (bits (insn1, 0, 9) << 12);
5853 offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
5854 offset |= exchange ?
5855 (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
5856 }
5857
136821d9
SM
5858 displaced_debug_printf ("copying %s insn %.4x %.4x with offset %.8lx",
5859 link ? (exchange) ? "blx" : "bl" : "b",
5860 insn1, insn2, offset);
34518530
YQ
5861
5862 dsc->modinsn[0] = THUMB_NOP;
5863
5864 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
5865 return 0;
5866}
5867
5868/* Copy B Thumb instructions. */
5869static int
615234c1 5870thumb_copy_b (struct gdbarch *gdbarch, uint16_t insn,
1152d984 5871 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
5872{
5873 unsigned int cond = 0;
5874 int offset = 0;
5875 unsigned short bit_12_15 = bits (insn, 12, 15);
5876 CORE_ADDR from = dsc->insn_addr;
5877
5878 if (bit_12_15 == 0xd)
5879 {
5880 /* offset = SignExtend (imm8:0, 32) */
5881 offset = sbits ((insn << 1), 0, 8);
5882 cond = bits (insn, 8, 11);
5883 }
5884 else if (bit_12_15 == 0xe) /* Encoding T2 */
5885 {
5886 offset = sbits ((insn << 1), 0, 11);
5887 cond = INST_AL;
5888 }
5889
136821d9
SM
5890 displaced_debug_printf ("copying b immediate insn %.4x with offset %d",
5891 insn, offset);
34518530
YQ
5892
5893 dsc->u.branch.cond = cond;
5894 dsc->u.branch.link = 0;
5895 dsc->u.branch.exchange = 0;
5896 dsc->u.branch.dest = from + 4 + offset;
5897
5898 dsc->modinsn[0] = THUMB_NOP;
5899
5900 dsc->cleanup = &cleanup_branch;
5901
5902 return 0;
5903}
5904
cca44b1b
JB
5905/* Copy BX/BLX with register-specified destinations. */
5906
7ff120b4
YQ
5907static void
5908install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5909 arm_displaced_step_copy_insn_closure *dsc, int link,
7ff120b4 5910 unsigned int cond, unsigned int rm)
cca44b1b 5911{
cca44b1b
JB
5912 /* Implement {BX,BLX}<cond> <reg>" as:
5913
5914 Preparation: cond <- instruction condition
5915 Insn: mov r0, r0 (nop)
5916 Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
5917
5918 Don't set r14 in cleanup for BX. */
5919
36073a92 5920 dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5921
5922 dsc->u.branch.cond = cond;
5923 dsc->u.branch.link = link;
cca44b1b 5924
7ff120b4 5925 dsc->u.branch.exchange = 1;
cca44b1b
JB
5926
5927 dsc->cleanup = &cleanup_branch;
7ff120b4 5928}
cca44b1b 5929
7ff120b4
YQ
5930static int
5931arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
1152d984 5932 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
5933{
5934 unsigned int cond = bits (insn, 28, 31);
5935 /* BX: x12xxx1x
5936 BLX: x12xxx3x. */
5937 int link = bit (insn, 5);
5938 unsigned int rm = bits (insn, 0, 3);
5939
136821d9 5940 displaced_debug_printf ("copying insn %.8lx", (unsigned long) insn);
7ff120b4
YQ
5941
5942 dsc->modinsn[0] = ARM_NOP;
5943
5944 install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
cca44b1b
JB
5945 return 0;
5946}
5947
34518530
YQ
5948static int
5949thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
5950 struct regcache *regs,
1152d984 5951 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
5952{
5953 int link = bit (insn, 7);
5954 unsigned int rm = bits (insn, 3, 6);
5955
136821d9 5956 displaced_debug_printf ("copying insn %.4x", (unsigned short) insn);
34518530
YQ
5957
5958 dsc->modinsn[0] = THUMB_NOP;
5959
5960 install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);
5961
5962 return 0;
5963}
5964
5965
0963b4bd 5966/* Copy/cleanup arithmetic/logic instruction with immediate RHS. */
cca44b1b
JB
5967
5968static void
6e39997a 5969cleanup_alu_imm (struct gdbarch *gdbarch,
1152d984 5970 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 5971{
36073a92 5972 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5973 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5974 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5975 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
5976}
5977
5978static int
7ff120b4 5979arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
1152d984 5980 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
5981{
5982 unsigned int rn = bits (insn, 16, 19);
5983 unsigned int rd = bits (insn, 12, 15);
5984 unsigned int op = bits (insn, 21, 24);
5985 int is_mov = (op == 0xd);
5986 ULONGEST rd_val, rn_val;
cca44b1b
JB
5987
5988 if (!insn_references_pc (insn, 0x000ff000ul))
7ff120b4 5989 return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
cca44b1b 5990
136821d9
SM
5991 displaced_debug_printf ("copying immediate %s insn %.8lx",
5992 is_mov ? "move" : "ALU",
5993 (unsigned long) insn);
cca44b1b
JB
5994
5995 /* Instruction is of form:
5996
5997 <op><cond> rd, [rn,] #imm
5998
5999 Rewrite as:
6000
6001 Preparation: tmp1, tmp2 <- r0, r1;
6002 r0, r1 <- rd, rn
6003 Insn: <op><cond> r0, r1, #imm
6004 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6005 */
6006
36073a92
YQ
6007 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6008 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6009 rn_val = displaced_read_reg (regs, dsc, rn);
6010 rd_val = displaced_read_reg (regs, dsc, rd);
cca44b1b
JB
6011 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6012 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6013 dsc->rd = rd;
6014
6015 if (is_mov)
6016 dsc->modinsn[0] = insn & 0xfff00fff;
6017 else
6018 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
6019
6020 dsc->cleanup = &cleanup_alu_imm;
6021
6022 return 0;
6023}
6024
34518530
YQ
6025static int
6026thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
6027 uint16_t insn2, struct regcache *regs,
1152d984 6028 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
6029{
6030 unsigned int op = bits (insn1, 5, 8);
6031 unsigned int rn, rm, rd;
6032 ULONGEST rd_val, rn_val;
6033
6034 rn = bits (insn1, 0, 3); /* Rn */
6035 rm = bits (insn2, 0, 3); /* Rm */
6036 rd = bits (insn2, 8, 11); /* Rd */
6037
6038 /* This routine is only called for instruction MOV. */
6039 gdb_assert (op == 0x2 && rn == 0xf);
6040
6041 if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
6042 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);
6043
136821d9 6044 displaced_debug_printf ("copying reg %s insn %.4x%.4x", "ALU", insn1, insn2);
34518530
YQ
6045
6046 /* Instruction is of form:
6047
6048 <op><cond> rd, [rn,] #imm
6049
6050 Rewrite as:
6051
6052 Preparation: tmp1, tmp2 <- r0, r1;
6053 r0, r1 <- rd, rn
6054 Insn: <op><cond> r0, r1, #imm
6055 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6056 */
6057
6058 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6059 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6060 rn_val = displaced_read_reg (regs, dsc, rn);
6061 rd_val = displaced_read_reg (regs, dsc, rd);
6062 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6063 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6064 dsc->rd = rd;
6065
6066 dsc->modinsn[0] = insn1;
6067 dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
6068 dsc->numinsns = 2;
6069
6070 dsc->cleanup = &cleanup_alu_imm;
6071
6072 return 0;
6073}
6074
cca44b1b
JB
6075/* Copy/cleanup arithmetic/logic insns with register RHS. */
6076
6077static void
6e39997a 6078cleanup_alu_reg (struct gdbarch *gdbarch,
1152d984 6079 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
6080{
6081 ULONGEST rd_val;
6082 int i;
6083
36073a92 6084 rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6085
6086 for (i = 0; i < 3; i++)
6087 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6088
6089 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6090}
6091
7ff120b4
YQ
6092static void
6093install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6094 arm_displaced_step_copy_insn_closure *dsc,
7ff120b4 6095 unsigned int rd, unsigned int rn, unsigned int rm)
cca44b1b 6096{
cca44b1b 6097 ULONGEST rd_val, rn_val, rm_val;
cca44b1b 6098
cca44b1b
JB
6099 /* Instruction is of form:
6100
6101 <op><cond> rd, [rn,] rm [, <shift>]
6102
6103 Rewrite as:
6104
6105 Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
6106 r0, r1, r2 <- rd, rn, rm
ef713951 6107 Insn: <op><cond> r0, [r1,] r2 [, <shift>]
cca44b1b
JB
6108 Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
6109 */
6110
36073a92
YQ
6111 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6112 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6113 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6114 rd_val = displaced_read_reg (regs, dsc, rd);
6115 rn_val = displaced_read_reg (regs, dsc, rn);
6116 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6117 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6118 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6119 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6120 dsc->rd = rd;
6121
7ff120b4
YQ
6122 dsc->cleanup = &cleanup_alu_reg;
6123}
6124
6125static int
6126arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
1152d984 6127 arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
6128{
6129 unsigned int op = bits (insn, 21, 24);
6130 int is_mov = (op == 0xd);
6131
6132 if (!insn_references_pc (insn, 0x000ff00ful))
6133 return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);
6134
136821d9
SM
6135 displaced_debug_printf ("copying reg %s insn %.8lx",
6136 is_mov ? "move" : "ALU", (unsigned long) insn);
7ff120b4 6137
cca44b1b
JB
6138 if (is_mov)
6139 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
6140 else
6141 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
6142
7ff120b4
YQ
6143 install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
6144 bits (insn, 0, 3));
cca44b1b
JB
6145 return 0;
6146}
6147
34518530
YQ
6148static int
6149thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
6150 struct regcache *regs,
1152d984 6151 arm_displaced_step_copy_insn_closure *dsc)
34518530 6152{
ef713951 6153 unsigned rm, rd;
34518530 6154
ef713951
YQ
6155 rm = bits (insn, 3, 6);
6156 rd = (bit (insn, 7) << 3) | bits (insn, 0, 2);
34518530 6157
ef713951 6158 if (rd != ARM_PC_REGNUM && rm != ARM_PC_REGNUM)
34518530
YQ
6159 return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);
6160
136821d9 6161 displaced_debug_printf ("copying ALU reg insn %.4x", (unsigned short) insn);
34518530 6162
ef713951 6163 dsc->modinsn[0] = ((insn & 0xff00) | 0x10);
34518530 6164
ef713951 6165 install_alu_reg (gdbarch, regs, dsc, rd, rd, rm);
34518530
YQ
6166
6167 return 0;
6168}
6169
cca44b1b
JB
6170/* Cleanup/copy arithmetic/logic insns with shifted register RHS. */
6171
6172static void
6e39997a 6173cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
cca44b1b 6174 struct regcache *regs,
1152d984 6175 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6176{
36073a92 6177 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6178 int i;
6179
6180 for (i = 0; i < 4; i++)
6181 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6182
6183 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6184}
6185
7ff120b4
YQ
6186static void
6187install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6188 arm_displaced_step_copy_insn_closure *dsc,
7ff120b4
YQ
6189 unsigned int rd, unsigned int rn, unsigned int rm,
6190 unsigned rs)
cca44b1b 6191{
7ff120b4 6192 int i;
cca44b1b 6193 ULONGEST rd_val, rn_val, rm_val, rs_val;
cca44b1b 6194
cca44b1b
JB
6195 /* Instruction is of form:
6196
6197 <op><cond> rd, [rn,] rm, <shift> rs
6198
6199 Rewrite as:
6200
6201 Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
6202 r0, r1, r2, r3 <- rd, rn, rm, rs
6203 Insn: <op><cond> r0, r1, r2, <shift> r3
6204 Cleanup: tmp5 <- r0
6205 r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
6206 rd <- tmp5
6207 */
6208
6209 for (i = 0; i < 4; i++)
36073a92 6210 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b 6211
36073a92
YQ
6212 rd_val = displaced_read_reg (regs, dsc, rd);
6213 rn_val = displaced_read_reg (regs, dsc, rn);
6214 rm_val = displaced_read_reg (regs, dsc, rm);
6215 rs_val = displaced_read_reg (regs, dsc, rs);
cca44b1b
JB
6216 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6217 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6218 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6219 displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
6220 dsc->rd = rd;
7ff120b4
YQ
6221 dsc->cleanup = &cleanup_alu_shifted_reg;
6222}
6223
6224static int
6225arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
6226 struct regcache *regs,
1152d984 6227 arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
6228{
6229 unsigned int op = bits (insn, 21, 24);
6230 int is_mov = (op == 0xd);
6231 unsigned int rd, rn, rm, rs;
6232
6233 if (!insn_references_pc (insn, 0x000fff0ful))
6234 return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
6235
136821d9
SM
6236 displaced_debug_printf ("copying shifted reg %s insn %.8lx",
6237 is_mov ? "move" : "ALU",
6238 (unsigned long) insn);
7ff120b4
YQ
6239
6240 rn = bits (insn, 16, 19);
6241 rm = bits (insn, 0, 3);
6242 rs = bits (insn, 8, 11);
6243 rd = bits (insn, 12, 15);
cca44b1b
JB
6244
6245 if (is_mov)
6246 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
6247 else
6248 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
6249
7ff120b4 6250 install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);
cca44b1b
JB
6251
6252 return 0;
6253}
6254
6255/* Clean up load instructions. */
6256
6257static void
6e39997a 6258cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6259 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
6260{
6261 ULONGEST rt_val, rt_val2 = 0, rn_val;
cca44b1b 6262
36073a92 6263 rt_val = displaced_read_reg (regs, dsc, 0);
cca44b1b 6264 if (dsc->u.ldst.xfersize == 8)
36073a92
YQ
6265 rt_val2 = displaced_read_reg (regs, dsc, 1);
6266 rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
6267
6268 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6269 if (dsc->u.ldst.xfersize > 4)
6270 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6271 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6272 if (!dsc->u.ldst.immed)
6273 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6274
6275 /* Handle register writeback. */
6276 if (dsc->u.ldst.writeback)
6277 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6278 /* Put result in right place. */
6279 displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
6280 if (dsc->u.ldst.xfersize == 8)
6281 displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
6282}
6283
6284/* Clean up store instructions. */
6285
6286static void
6e39997a 6287cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6288 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6289{
36073a92 6290 ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
6291
6292 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6293 if (dsc->u.ldst.xfersize > 4)
6294 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6295 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6296 if (!dsc->u.ldst.immed)
6297 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6298 if (!dsc->u.ldst.restore_r4)
6299 displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
6300
6301 /* Writeback. */
6302 if (dsc->u.ldst.writeback)
6303 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6304}
6305
6306/* Copy "extra" load/store instructions. These are halfword/doubleword
6307 transfers, which have a different encoding to byte/word transfers. */
6308
6309static int
550dc4e2 6310arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unprivileged,
1152d984 6311 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
6312{
6313 unsigned int op1 = bits (insn, 20, 24);
6314 unsigned int op2 = bits (insn, 5, 6);
6315 unsigned int rt = bits (insn, 12, 15);
6316 unsigned int rn = bits (insn, 16, 19);
6317 unsigned int rm = bits (insn, 0, 3);
6318 char load[12] = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
6319 char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
6320 int immed = (op1 & 0x4) != 0;
6321 int opcode;
6322 ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
cca44b1b
JB
6323
6324 if (!insn_references_pc (insn, 0x000ff00ful))
7ff120b4 6325 return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);
cca44b1b 6326
136821d9
SM
6327 displaced_debug_printf ("copying %sextra load/store insn %.8lx",
6328 unprivileged ? "unprivileged " : "",
6329 (unsigned long) insn);
cca44b1b
JB
6330
6331 opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
6332
6333 if (opcode < 0)
6334 internal_error (__FILE__, __LINE__,
6335 _("copy_extra_ld_st: instruction decode error"));
6336
36073a92
YQ
6337 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6338 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6339 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 6340 if (!immed)
36073a92 6341 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 6342
36073a92 6343 rt_val = displaced_read_reg (regs, dsc, rt);
cca44b1b 6344 if (bytesize[opcode] == 8)
36073a92
YQ
6345 rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
6346 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 6347 if (!immed)
36073a92 6348 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6349
6350 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6351 if (bytesize[opcode] == 8)
6352 displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
6353 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6354 if (!immed)
6355 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
6356
6357 dsc->rd = rt;
6358 dsc->u.ldst.xfersize = bytesize[opcode];
6359 dsc->u.ldst.rn = rn;
6360 dsc->u.ldst.immed = immed;
6361 dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
6362 dsc->u.ldst.restore_r4 = 0;
6363
6364 if (immed)
6365 /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
6366 ->
6367 {ldr,str}<width><cond> r0, [r1,] [r2, #imm]. */
6368 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6369 else
6370 /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
6371 ->
6372 {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3]. */
6373 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6374
6375 dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
6376
6377 return 0;
6378}
6379
0f6f04ba 6380/* Copy byte/half word/word loads and stores. */
cca44b1b 6381
7ff120b4 6382static void
0f6f04ba 6383install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6384 arm_displaced_step_copy_insn_closure *dsc, int load,
0f6f04ba
YQ
6385 int immed, int writeback, int size, int usermode,
6386 int rt, int rm, int rn)
cca44b1b 6387{
cca44b1b 6388 ULONGEST rt_val, rn_val, rm_val = 0;
cca44b1b 6389
36073a92
YQ
6390 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6391 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 6392 if (!immed)
36073a92 6393 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 6394 if (!load)
36073a92 6395 dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);
cca44b1b 6396
36073a92
YQ
6397 rt_val = displaced_read_reg (regs, dsc, rt);
6398 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 6399 if (!immed)
36073a92 6400 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6401
6402 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6403 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6404 if (!immed)
6405 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
cca44b1b 6406 dsc->rd = rt;
0f6f04ba 6407 dsc->u.ldst.xfersize = size;
cca44b1b
JB
6408 dsc->u.ldst.rn = rn;
6409 dsc->u.ldst.immed = immed;
7ff120b4 6410 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
6411
6412 /* To write PC we can do:
6413
494e194e
YQ
6414 Before this sequence of instructions:
6415 r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
85102364 6416 r2 is the Rn value got from displaced_read_reg.
494e194e
YQ
6417
6418 Insn1: push {pc} Write address of STR instruction + offset on stack
6419 Insn2: pop {r4} Read it back from stack, r4 = addr(Insn1) + offset
6420 Insn3: sub r4, r4, pc r4 = addr(Insn1) + offset - pc
dda83cd7
SM
6421 = addr(Insn1) + offset - addr(Insn3) - 8
6422 = offset - 16
494e194e
YQ
6423 Insn4: add r4, r4, #8 r4 = offset - 8
6424 Insn5: add r0, r0, r4 r0 = from + 8 + offset - 8
dda83cd7 6425 = from + offset
494e194e 6426 Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])
cca44b1b
JB
6427
6428 Otherwise we don't know what value to write for PC, since the offset is
494e194e
YQ
6429 architecture-dependent (sometimes PC+8, sometimes PC+12). More details
6430 of this can be found in Section "Saving from r15" in
6431 http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */
cca44b1b 6432
7ff120b4
YQ
6433 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6434}
6435
34518530
YQ
6436
6437static int
6438thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
6439 uint16_t insn2, struct regcache *regs,
1152d984 6440 arm_displaced_step_copy_insn_closure *dsc, int size)
34518530
YQ
6441{
6442 unsigned int u_bit = bit (insn1, 7);
6443 unsigned int rt = bits (insn2, 12, 15);
6444 int imm12 = bits (insn2, 0, 11);
6445 ULONGEST pc_val;
6446
136821d9
SM
6447 displaced_debug_printf ("copying ldr pc (0x%x) R%d %c imm12 %.4x",
6448 (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
6449 imm12);
34518530
YQ
6450
6451 if (!u_bit)
6452 imm12 = -1 * imm12;
6453
6454 /* Rewrite instruction LDR Rt imm12 into:
6455
6456 Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12
6457
6458 LDR R0, R2, R3,
6459
6460 Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2]. */
6461
6462
6463 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6464 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6465 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6466
6467 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6468
6469 pc_val = pc_val & 0xfffffffc;
6470
6471 displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
6472 displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);
6473
6474 dsc->rd = rt;
6475
6476 dsc->u.ldst.xfersize = size;
6477 dsc->u.ldst.immed = 0;
6478 dsc->u.ldst.writeback = 0;
6479 dsc->u.ldst.restore_r4 = 0;
6480
6481 /* LDR R0, R2, R3 */
6482 dsc->modinsn[0] = 0xf852;
6483 dsc->modinsn[1] = 0x3;
6484 dsc->numinsns = 2;
6485
6486 dsc->cleanup = &cleanup_load;
6487
6488 return 0;
6489}
6490
6491static int
6492thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
6493 uint16_t insn2, struct regcache *regs,
1152d984 6494 arm_displaced_step_copy_insn_closure *dsc,
34518530
YQ
6495 int writeback, int immed)
6496{
6497 unsigned int rt = bits (insn2, 12, 15);
6498 unsigned int rn = bits (insn1, 0, 3);
6499 unsigned int rm = bits (insn2, 0, 3); /* Only valid if !immed. */
6500 /* In LDR (register), there is also a register Rm, which is not allowed to
6501 be PC, so we don't have to check it. */
6502
6503 if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
6504 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
6505 dsc);
6506
136821d9
SM
6507 displaced_debug_printf ("copying ldr r%d [r%d] insn %.4x%.4x",
6508 rt, rn, insn1, insn2);
34518530
YQ
6509
6510 install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
6511 0, rt, rm, rn);
6512
6513 dsc->u.ldst.restore_r4 = 0;
6514
6515 if (immed)
6516 /* ldr[b]<cond> rt, [rn, #imm], etc.
6517 ->
6518 ldr[b]<cond> r0, [r2, #imm]. */
6519 {
6520 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6521 dsc->modinsn[1] = insn2 & 0x0fff;
6522 }
6523 else
6524 /* ldr[b]<cond> rt, [rn, rm], etc.
6525 ->
6526 ldr[b]<cond> r0, [r2, r3]. */
6527 {
6528 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6529 dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
6530 }
6531
6532 dsc->numinsns = 2;
6533
6534 return 0;
6535}
6536
6537
7ff120b4
YQ
6538static int
6539arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
6540 struct regcache *regs,
1152d984 6541 arm_displaced_step_copy_insn_closure *dsc,
0f6f04ba 6542 int load, int size, int usermode)
7ff120b4
YQ
6543{
6544 int immed = !bit (insn, 25);
6545 int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
6546 unsigned int rt = bits (insn, 12, 15);
6547 unsigned int rn = bits (insn, 16, 19);
6548 unsigned int rm = bits (insn, 0, 3); /* Only valid if !immed. */
6549
6550 if (!insn_references_pc (insn, 0x000ff00ful))
6551 return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);
6552
136821d9
SM
6553 displaced_debug_printf ("copying %s%s r%d [r%d] insn %.8lx",
6554 load ? (size == 1 ? "ldrb" : "ldr")
6555 : (size == 1 ? "strb" : "str"),
6556 usermode ? "t" : "",
6557 rt, rn,
6558 (unsigned long) insn);
7ff120b4 6559
0f6f04ba
YQ
6560 install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
6561 usermode, rt, rm, rn);
7ff120b4 6562
bf9f652a 6563 if (load || rt != ARM_PC_REGNUM)
cca44b1b
JB
6564 {
6565 dsc->u.ldst.restore_r4 = 0;
6566
6567 if (immed)
6568 /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
6569 ->
6570 {ldr,str}[b]<cond> r0, [r2, #imm]. */
6571 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6572 else
6573 /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
6574 ->
6575 {ldr,str}[b]<cond> r0, [r2, r3]. */
6576 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6577 }
6578 else
6579 {
6580 /* We need to use r4 as scratch. Make sure it's restored afterwards. */
6581 dsc->u.ldst.restore_r4 = 1;
494e194e
YQ
6582 dsc->modinsn[0] = 0xe92d8000; /* push {pc} */
6583 dsc->modinsn[1] = 0xe8bd0010; /* pop {r4} */
cca44b1b
JB
6584 dsc->modinsn[2] = 0xe044400f; /* sub r4, r4, pc. */
6585 dsc->modinsn[3] = 0xe2844008; /* add r4, r4, #8. */
6586 dsc->modinsn[4] = 0xe0800004; /* add r0, r0, r4. */
6587
6588 /* As above. */
6589 if (immed)
6590 dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
6591 else
6592 dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
6593
cca44b1b
JB
6594 dsc->numinsns = 6;
6595 }
6596
6597 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6598
6599 return 0;
6600}
6601
6602/* Cleanup LDM instructions with fully-populated register list. This is an
6603 unfortunate corner case: it's impossible to implement correctly by modifying
6604 the instruction. The issue is as follows: we have an instruction,
6605
6606 ldm rN, {r0-r15}
6607
6608 which we must rewrite to avoid loading PC. A possible solution would be to
6609 do the load in two halves, something like (with suitable cleanup
6610 afterwards):
6611
6612 mov r8, rN
6613 ldm[id][ab] r8!, {r0-r7}
6614 str r7, <temp>
6615 ldm[id][ab] r8, {r7-r14}
6616 <bkpt>
6617
6618 but at present there's no suitable place for <temp>, since the scratch space
6619 is overwritten before the cleanup routine is called. For now, we simply
6620 emulate the instruction. */
6621
6622static void
6623cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6624 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6625{
cca44b1b
JB
6626 int inc = dsc->u.block.increment;
6627 int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
6628 int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
6629 uint32_t regmask = dsc->u.block.regmask;
6630 int regno = inc ? 0 : 15;
6631 CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
6632 int exception_return = dsc->u.block.load && dsc->u.block.user
6633 && (regmask & 0x8000) != 0;
36073a92 6634 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
6635 int do_transfer = condition_true (dsc->u.block.cond, status);
6636 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6637
6638 if (!do_transfer)
6639 return;
6640
6641 /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
6642 sensible we can do here. Complain loudly. */
6643 if (exception_return)
6644 error (_("Cannot single-step exception return"));
6645
6646 /* We don't handle any stores here for now. */
6647 gdb_assert (dsc->u.block.load != 0);
6648
136821d9
SM
6649 displaced_debug_printf ("emulating block transfer: %s %s %s",
6650 dsc->u.block.load ? "ldm" : "stm",
6651 dsc->u.block.increment ? "inc" : "dec",
6652 dsc->u.block.before ? "before" : "after");
cca44b1b
JB
6653
6654 while (regmask)
6655 {
6656 uint32_t memword;
6657
6658 if (inc)
bf9f652a 6659 while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
cca44b1b
JB
6660 regno++;
6661 else
6662 while (regno >= 0 && (regmask & (1 << regno)) == 0)
6663 regno--;
6664
6665 xfer_addr += bump_before;
6666
6667 memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
6668 displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
6669
6670 xfer_addr += bump_after;
6671
6672 regmask &= ~(1 << regno);
6673 }
6674
6675 if (dsc->u.block.writeback)
6676 displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
6677 CANNOT_WRITE_PC);
6678}
6679
6680/* Clean up an STM which included the PC in the register list. */
6681
6682static void
6683cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6684 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6685{
36073a92 6686 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b 6687 int store_executed = condition_true (dsc->u.block.cond, status);
5f661e03
SM
6688 CORE_ADDR pc_stored_at, transferred_regs
6689 = count_one_bits (dsc->u.block.regmask);
cca44b1b
JB
6690 CORE_ADDR stm_insn_addr;
6691 uint32_t pc_val;
6692 long offset;
6693 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6694
6695 /* If condition code fails, there's nothing else to do. */
6696 if (!store_executed)
6697 return;
6698
6699 if (dsc->u.block.increment)
6700 {
6701 pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
6702
6703 if (dsc->u.block.before)
6704 pc_stored_at += 4;
6705 }
6706 else
6707 {
6708 pc_stored_at = dsc->u.block.xfer_addr;
6709
6710 if (dsc->u.block.before)
6711 pc_stored_at -= 4;
6712 }
6713
6714 pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
6715 stm_insn_addr = dsc->scratch_base;
6716 offset = pc_val - stm_insn_addr;
6717
136821d9
SM
6718 displaced_debug_printf ("detected PC offset %.8lx for STM instruction",
6719 offset);
cca44b1b
JB
6720
6721 /* Rewrite the stored PC to the proper value for the non-displaced original
6722 instruction. */
6723 write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
6724 dsc->insn_addr + offset);
6725}
6726
6727/* Clean up an LDM which includes the PC in the register list. We clumped all
6728 the registers in the transferred list into a contiguous range r0...rX (to
6729 avoid loading PC directly and losing control of the debugged program), so we
6730 must undo that here. */
6731
6732static void
6e39997a 6733cleanup_block_load_pc (struct gdbarch *gdbarch,
cca44b1b 6734 struct regcache *regs,
1152d984 6735 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6736{
36073a92 6737 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
22e048c9 6738 int load_executed = condition_true (dsc->u.block.cond, status);
bf9f652a 6739 unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
5f661e03 6740 unsigned int regs_loaded = count_one_bits (mask);
cca44b1b
JB
6741 unsigned int num_to_shuffle = regs_loaded, clobbered;
6742
6743 /* The method employed here will fail if the register list is fully populated
6744 (we need to avoid loading PC directly). */
6745 gdb_assert (num_to_shuffle < 16);
6746
6747 if (!load_executed)
6748 return;
6749
6750 clobbered = (1 << num_to_shuffle) - 1;
6751
6752 while (num_to_shuffle > 0)
6753 {
6754 if ((mask & (1 << write_reg)) != 0)
6755 {
6756 unsigned int read_reg = num_to_shuffle - 1;
6757
6758 if (read_reg != write_reg)
6759 {
36073a92 6760 ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
cca44b1b 6761 displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
136821d9
SM
6762 displaced_debug_printf ("LDM: move loaded register r%d to r%d",
6763 read_reg, write_reg);
cca44b1b 6764 }
136821d9
SM
6765 else
6766 displaced_debug_printf ("LDM: register r%d already in the right "
6767 "place", write_reg);
cca44b1b
JB
6768
6769 clobbered &= ~(1 << write_reg);
6770
6771 num_to_shuffle--;
6772 }
6773
6774 write_reg--;
6775 }
6776
6777 /* Restore any registers we scribbled over. */
6778 for (write_reg = 0; clobbered != 0; write_reg++)
6779 {
6780 if ((clobbered & (1 << write_reg)) != 0)
6781 {
6782 displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
6783 CANNOT_WRITE_PC);
136821d9
SM
6784 displaced_debug_printf ("LDM: restored clobbered register r%d",
6785 write_reg);
cca44b1b
JB
6786 clobbered &= ~(1 << write_reg);
6787 }
6788 }
6789
6790 /* Perform register writeback manually. */
6791 if (dsc->u.block.writeback)
6792 {
6793 ULONGEST new_rn_val = dsc->u.block.xfer_addr;
6794
6795 if (dsc->u.block.increment)
6796 new_rn_val += regs_loaded * 4;
6797 else
6798 new_rn_val -= regs_loaded * 4;
6799
6800 displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
6801 CANNOT_WRITE_PC);
6802 }
6803}
6804
6805/* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
6806 in user-level code (in particular exception return, ldm rn, {...pc}^). */
6807
6808static int
7ff120b4
YQ
6809arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
6810 struct regcache *regs,
1152d984 6811 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
6812{
6813 int load = bit (insn, 20);
6814 int user = bit (insn, 22);
6815 int increment = bit (insn, 23);
6816 int before = bit (insn, 24);
6817 int writeback = bit (insn, 21);
6818 int rn = bits (insn, 16, 19);
cca44b1b 6819
0963b4bd
MS
6820 /* Block transfers which don't mention PC can be run directly
6821 out-of-line. */
bf9f652a 6822 if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
7ff120b4 6823 return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
cca44b1b 6824
bf9f652a 6825 if (rn == ARM_PC_REGNUM)
cca44b1b 6826 {
0963b4bd
MS
6827 warning (_("displaced: Unpredictable LDM or STM with "
6828 "base register r15"));
7ff120b4 6829 return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
cca44b1b
JB
6830 }
6831
136821d9
SM
6832 displaced_debug_printf ("copying block transfer insn %.8lx",
6833 (unsigned long) insn);
cca44b1b 6834
36073a92 6835 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b
JB
6836 dsc->u.block.rn = rn;
6837
6838 dsc->u.block.load = load;
6839 dsc->u.block.user = user;
6840 dsc->u.block.increment = increment;
6841 dsc->u.block.before = before;
6842 dsc->u.block.writeback = writeback;
6843 dsc->u.block.cond = bits (insn, 28, 31);
6844
6845 dsc->u.block.regmask = insn & 0xffff;
6846
6847 if (load)
6848 {
6849 if ((insn & 0xffff) == 0xffff)
6850 {
6851 /* LDM with a fully-populated register list. This case is
6852 particularly tricky. Implement for now by fully emulating the
6853 instruction (which might not behave perfectly in all cases, but
6854 these instructions should be rare enough for that not to matter
6855 too much). */
6856 dsc->modinsn[0] = ARM_NOP;
6857
6858 dsc->cleanup = &cleanup_block_load_all;
6859 }
6860 else
6861 {
6862 /* LDM of a list of registers which includes PC. Implement by
6863 rewriting the list of registers to be transferred into a
6864 contiguous chunk r0...rX before doing the transfer, then shuffling
6865 registers into the correct places in the cleanup routine. */
6866 unsigned int regmask = insn & 0xffff;
5f661e03 6867 unsigned int num_in_list = count_one_bits (regmask), new_regmask;
bec2ab5a 6868 unsigned int i;
cca44b1b
JB
6869
6870 for (i = 0; i < num_in_list; i++)
36073a92 6871 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b
JB
6872
6873 /* Writeback makes things complicated. We need to avoid clobbering
6874 the base register with one of the registers in our modified
6875 register list, but just using a different register can't work in
6876 all cases, e.g.:
6877
6878 ldm r14!, {r0-r13,pc}
6879
6880 which would need to be rewritten as:
6881
6882 ldm rN!, {r0-r14}
6883
6884 but that can't work, because there's no free register for N.
6885
6886 Solve this by turning off the writeback bit, and emulating
6887 writeback manually in the cleanup routine. */
6888
6889 if (writeback)
6890 insn &= ~(1 << 21);
6891
6892 new_regmask = (1 << num_in_list) - 1;
6893
136821d9
SM
6894 displaced_debug_printf ("LDM r%d%s, {..., pc}: original reg list "
6895 "%.4x, modified list %.4x",
6896 rn, writeback ? "!" : "",
6897 (int) insn & 0xffff, new_regmask);
cca44b1b
JB
6898
6899 dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
6900
6901 dsc->cleanup = &cleanup_block_load_pc;
6902 }
6903 }
6904 else
6905 {
6906 /* STM of a list of registers which includes PC. Run the instruction
6907 as-is, but out of line: this will store the wrong value for the PC,
6908 so we must manually fix up the memory in the cleanup routine.
6909 Doing things this way has the advantage that we can auto-detect
6910 the offset of the PC write (which is architecture-dependent) in
6911 the cleanup routine. */
6912 dsc->modinsn[0] = insn;
6913
6914 dsc->cleanup = &cleanup_block_store_pc;
6915 }
6916
6917 return 0;
6918}
6919
34518530
YQ
6920static int
6921thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
6922 struct regcache *regs,
1152d984 6923 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6924{
34518530
YQ
6925 int rn = bits (insn1, 0, 3);
6926 int load = bit (insn1, 4);
6927 int writeback = bit (insn1, 5);
cca44b1b 6928
34518530
YQ
6929 /* Block transfers which don't mention PC can be run directly
6930 out-of-line. */
6931 if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
6932 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);
7ff120b4 6933
34518530
YQ
6934 if (rn == ARM_PC_REGNUM)
6935 {
6936 warning (_("displaced: Unpredictable LDM or STM with "
6937 "base register r15"));
6938 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6939 "unpredictable ldm/stm", dsc);
6940 }
cca44b1b 6941
136821d9
SM
6942 displaced_debug_printf ("copying block transfer insn %.4x%.4x",
6943 insn1, insn2);
cca44b1b 6944
34518530
YQ
6945 /* Clear bit 13, since it should be always zero. */
6946 dsc->u.block.regmask = (insn2 & 0xdfff);
6947 dsc->u.block.rn = rn;
cca44b1b 6948
34518530
YQ
6949 dsc->u.block.load = load;
6950 dsc->u.block.user = 0;
6951 dsc->u.block.increment = bit (insn1, 7);
6952 dsc->u.block.before = bit (insn1, 8);
6953 dsc->u.block.writeback = writeback;
6954 dsc->u.block.cond = INST_AL;
6955 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b 6956
34518530
YQ
6957 if (load)
6958 {
6959 if (dsc->u.block.regmask == 0xffff)
6960 {
6961 /* This branch is impossible to happen. */
6962 gdb_assert (0);
6963 }
6964 else
6965 {
6966 unsigned int regmask = dsc->u.block.regmask;
5f661e03 6967 unsigned int num_in_list = count_one_bits (regmask), new_regmask;
bec2ab5a 6968 unsigned int i;
34518530
YQ
6969
6970 for (i = 0; i < num_in_list; i++)
6971 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
6972
6973 if (writeback)
6974 insn1 &= ~(1 << 5);
6975
6976 new_regmask = (1 << num_in_list) - 1;
6977
136821d9
SM
6978 displaced_debug_printf ("LDM r%d%s, {..., pc}: original reg list "
6979 "%.4x, modified list %.4x",
6980 rn, writeback ? "!" : "",
6981 (int) dsc->u.block.regmask, new_regmask);
34518530
YQ
6982
6983 dsc->modinsn[0] = insn1;
6984 dsc->modinsn[1] = (new_regmask & 0xffff);
6985 dsc->numinsns = 2;
6986
6987 dsc->cleanup = &cleanup_block_load_pc;
6988 }
6989 }
6990 else
6991 {
6992 dsc->modinsn[0] = insn1;
6993 dsc->modinsn[1] = insn2;
6994 dsc->numinsns = 2;
6995 dsc->cleanup = &cleanup_block_store_pc;
6996 }
6997 return 0;
6998}
6999
d9311bfa
AT
7000/* Wrapper over read_memory_unsigned_integer for use in arm_get_next_pcs.
7001 This is used to avoid a dependency on BFD's bfd_endian enum. */
7002
7003ULONGEST
7004arm_get_next_pcs_read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
7005 int byte_order)
7006{
5f2dfcfd
AT
7007 return read_memory_unsigned_integer (memaddr, len,
7008 (enum bfd_endian) byte_order);
d9311bfa
AT
7009}
7010
7011/* Wrapper over gdbarch_addr_bits_remove for use in arm_get_next_pcs. */
7012
7013CORE_ADDR
7014arm_get_next_pcs_addr_bits_remove (struct arm_get_next_pcs *self,
7015 CORE_ADDR val)
7016{
ac7936df 7017 return gdbarch_addr_bits_remove (self->regcache->arch (), val);
d9311bfa
AT
7018}
7019
7020/* Wrapper over syscall_next_pc for use in get_next_pcs. */
7021
e7cf25a8 7022static CORE_ADDR
553cb527 7023arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self)
d9311bfa 7024{
d9311bfa
AT
7025 return 0;
7026}
7027
7028/* Wrapper over arm_is_thumb for use in arm_get_next_pcs. */
7029
7030int
7031arm_get_next_pcs_is_thumb (struct arm_get_next_pcs *self)
7032{
7033 return arm_is_thumb (self->regcache);
7034}
7035
7036/* single_step() is called just before we want to resume the inferior,
7037 if we want to single-step it but there is no hardware or kernel
7038 single-step support. We find the target of the coming instructions
7039 and breakpoint them. */
7040
a0ff9e1a 7041std::vector<CORE_ADDR>
f5ea389a 7042arm_software_single_step (struct regcache *regcache)
d9311bfa 7043{
ac7936df 7044 struct gdbarch *gdbarch = regcache->arch ();
d9311bfa 7045 struct arm_get_next_pcs next_pcs_ctx;
d9311bfa
AT
7046
7047 arm_get_next_pcs_ctor (&next_pcs_ctx,
7048 &arm_get_next_pcs_ops,
7049 gdbarch_byte_order (gdbarch),
7050 gdbarch_byte_order_for_code (gdbarch),
1b451dda 7051 0,
d9311bfa
AT
7052 regcache);
7053
a0ff9e1a 7054 std::vector<CORE_ADDR> next_pcs = arm_get_next_pcs (&next_pcs_ctx);
d9311bfa 7055
a0ff9e1a
SM
7056 for (CORE_ADDR &pc_ref : next_pcs)
7057 pc_ref = gdbarch_addr_bits_remove (gdbarch, pc_ref);
d9311bfa 7058
93f9a11f 7059 return next_pcs;
d9311bfa
AT
7060}
7061
34518530
YQ
7062/* Cleanup/copy SVC (SWI) instructions. These two functions are overridden
7063 for Linux, where some SVC instructions must be treated specially. */
7064
7065static void
7066cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 7067 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7068{
7069 CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
7070
136821d9
SM
7071 displaced_debug_printf ("cleanup for svc, resume at %.8lx",
7072 (unsigned long) resume_addr);
34518530
YQ
7073
7074 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
7075}
7076
7077
85102364 7078/* Common copy routine for svc instruction. */
34518530
YQ
7079
7080static int
7081install_svc (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 7082 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7083{
7084 /* Preparation: none.
7085 Insn: unmodified svc.
7086 Cleanup: pc <- insn_addr + insn_size. */
7087
7088 /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
7089 instruction. */
7090 dsc->wrote_to_pc = 1;
7091
7092 /* Allow OS-specific code to override SVC handling. */
bd18283a
YQ
7093 if (dsc->u.svc.copy_svc_os)
7094 return dsc->u.svc.copy_svc_os (gdbarch, regs, dsc);
7095 else
7096 {
7097 dsc->cleanup = &cleanup_svc;
7098 return 0;
7099 }
34518530
YQ
7100}
7101
7102static int
7103arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
1152d984 7104 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7105{
7106
136821d9
SM
7107 displaced_debug_printf ("copying svc insn %.8lx",
7108 (unsigned long) insn);
34518530
YQ
7109
7110 dsc->modinsn[0] = insn;
7111
7112 return install_svc (gdbarch, regs, dsc);
7113}
7114
7115static int
7116thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
1152d984 7117 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7118{
7119
136821d9 7120 displaced_debug_printf ("copying svc insn %.4x", insn);
bd18283a 7121
34518530
YQ
7122 dsc->modinsn[0] = insn;
7123
7124 return install_svc (gdbarch, regs, dsc);
cca44b1b
JB
7125}
7126
7127/* Copy undefined instructions. */
7128
7129static int
7ff120b4 7130arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
1152d984 7131 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 7132{
136821d9
SM
7133 displaced_debug_printf ("copying undefined insn %.8lx",
7134 (unsigned long) insn);
cca44b1b
JB
7135
7136 dsc->modinsn[0] = insn;
7137
7138 return 0;
7139}
7140
34518530
YQ
7141static int
7142thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
1152d984 7143 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7144{
7145
136821d9
SM
7146 displaced_debug_printf ("copying undefined insn %.4x %.4x",
7147 (unsigned short) insn1, (unsigned short) insn2);
34518530
YQ
7148
7149 dsc->modinsn[0] = insn1;
7150 dsc->modinsn[1] = insn2;
7151 dsc->numinsns = 2;
7152
7153 return 0;
7154}
7155
cca44b1b
JB
7156/* Copy unpredictable instructions. */
7157
7158static int
7ff120b4 7159arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
1152d984 7160 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 7161{
136821d9
SM
7162 displaced_debug_printf ("copying unpredictable insn %.8lx",
7163 (unsigned long) insn);
cca44b1b
JB
7164
7165 dsc->modinsn[0] = insn;
7166
7167 return 0;
7168}
7169
7170/* The decode_* functions are instruction decoding helpers. They mostly follow
7171 the presentation in the ARM ARM. */
7172
7173static int
7ff120b4
YQ
7174arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
7175 struct regcache *regs,
1152d984 7176 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7177{
7178 unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
7179 unsigned int rn = bits (insn, 16, 19);
7180
2f924de6 7181 if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0x1) == 0x0)
7ff120b4 7182 return arm_copy_unmodified (gdbarch, insn, "cps", dsc);
2f924de6 7183 else if (op1 == 0x10 && op2 == 0x0 && (rn & 0x1) == 0x1)
7ff120b4 7184 return arm_copy_unmodified (gdbarch, insn, "setend", dsc);
cca44b1b 7185 else if ((op1 & 0x60) == 0x20)
7ff120b4 7186 return arm_copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
cca44b1b 7187 else if ((op1 & 0x71) == 0x40)
7ff120b4
YQ
7188 return arm_copy_unmodified (gdbarch, insn, "neon elt/struct load/store",
7189 dsc);
cca44b1b 7190 else if ((op1 & 0x77) == 0x41)
7ff120b4 7191 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 7192 else if ((op1 & 0x77) == 0x45)
7ff120b4 7193 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pli. */
cca44b1b
JB
7194 else if ((op1 & 0x77) == 0x51)
7195 {
7196 if (rn != 0xf)
7ff120b4 7197 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b 7198 else
7ff120b4 7199 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7200 }
7201 else if ((op1 & 0x77) == 0x55)
7ff120b4 7202 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b
JB
7203 else if (op1 == 0x57)
7204 switch (op2)
7205 {
7ff120b4
YQ
7206 case 0x1: return arm_copy_unmodified (gdbarch, insn, "clrex", dsc);
7207 case 0x4: return arm_copy_unmodified (gdbarch, insn, "dsb", dsc);
7208 case 0x5: return arm_copy_unmodified (gdbarch, insn, "dmb", dsc);
7209 case 0x6: return arm_copy_unmodified (gdbarch, insn, "isb", dsc);
7210 default: return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7211 }
7212 else if ((op1 & 0x63) == 0x43)
7ff120b4 7213 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7214 else if ((op2 & 0x1) == 0x0)
7215 switch (op1 & ~0x80)
7216 {
7217 case 0x61:
7ff120b4 7218 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 7219 case 0x65:
7ff120b4 7220 return arm_copy_preload_reg (gdbarch, insn, regs, dsc); /* pli reg. */
cca44b1b 7221 case 0x71: case 0x75:
dda83cd7 7222 /* pld/pldw reg. */
7ff120b4 7223 return arm_copy_preload_reg (gdbarch, insn, regs, dsc);
cca44b1b 7224 case 0x63: case 0x67: case 0x73: case 0x77:
7ff120b4 7225 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b 7226 default:
7ff120b4 7227 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7228 }
7229 else
7ff120b4 7230 return arm_copy_undef (gdbarch, insn, dsc); /* Probably unreachable. */
cca44b1b
JB
7231}
7232
7233static int
7ff120b4
YQ
7234arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
7235 struct regcache *regs,
1152d984 7236 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7237{
7238 if (bit (insn, 27) == 0)
7ff120b4 7239 return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
cca44b1b
JB
7240 /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx. */
7241 else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
7242 {
7243 case 0x0: case 0x2:
7ff120b4 7244 return arm_copy_unmodified (gdbarch, insn, "srs", dsc);
cca44b1b
JB
7245
7246 case 0x1: case 0x3:
7ff120b4 7247 return arm_copy_unmodified (gdbarch, insn, "rfe", dsc);
cca44b1b
JB
7248
7249 case 0x4: case 0x5: case 0x6: case 0x7:
7ff120b4 7250 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b
JB
7251
7252 case 0x8:
7253 switch ((insn & 0xe00000) >> 21)
7254 {
7255 case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
7256 /* stc/stc2. */
7ff120b4 7257 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7258
7259 case 0x2:
7ff120b4 7260 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b
JB
7261
7262 default:
7ff120b4 7263 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7264 }
7265
7266 case 0x9:
7267 {
7268 int rn_f = (bits (insn, 16, 19) == 0xf);
7269 switch ((insn & 0xe00000) >> 21)
7270 {
7271 case 0x1: case 0x3:
7272 /* ldc/ldc2 imm (undefined for rn == pc). */
7ff120b4
YQ
7273 return rn_f ? arm_copy_undef (gdbarch, insn, dsc)
7274 : arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7275
7276 case 0x2:
7ff120b4 7277 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
7278
7279 case 0x4: case 0x5: case 0x6: case 0x7:
7280 /* ldc/ldc2 lit (undefined for rn != pc). */
7ff120b4
YQ
7281 return rn_f ? arm_copy_copro_load_store (gdbarch, insn, regs, dsc)
7282 : arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7283
7284 default:
7ff120b4 7285 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7286 }
7287 }
7288
7289 case 0xa:
7ff120b4 7290 return arm_copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
cca44b1b
JB
7291
7292 case 0xb:
7293 if (bits (insn, 16, 19) == 0xf)
dda83cd7 7294 /* ldc/ldc2 lit. */
7ff120b4 7295 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 7296 else
7ff120b4 7297 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7298
7299 case 0xc:
7300 if (bit (insn, 4))
7ff120b4 7301 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 7302 else
7ff120b4 7303 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7304
7305 case 0xd:
7306 if (bit (insn, 4))
7ff120b4 7307 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 7308 else
7ff120b4 7309 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7310
7311 default:
7ff120b4 7312 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7313 }
7314}
7315
7316/* Decode miscellaneous instructions in dp/misc encoding space. */
7317
7318static int
7ff120b4
YQ
7319arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
7320 struct regcache *regs,
1152d984 7321 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7322{
7323 unsigned int op2 = bits (insn, 4, 6);
7324 unsigned int op = bits (insn, 21, 22);
cca44b1b
JB
7325
7326 switch (op2)
7327 {
7328 case 0x0:
7ff120b4 7329 return arm_copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
cca44b1b
JB
7330
7331 case 0x1:
7332 if (op == 0x1) /* bx. */
7ff120b4 7333 return arm_copy_bx_blx_reg (gdbarch, insn, regs, dsc);
cca44b1b 7334 else if (op == 0x3)
7ff120b4 7335 return arm_copy_unmodified (gdbarch, insn, "clz", dsc);
cca44b1b 7336 else
7ff120b4 7337 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7338
7339 case 0x2:
7340 if (op == 0x1)
dda83cd7 7341 /* Not really supported. */
7ff120b4 7342 return arm_copy_unmodified (gdbarch, insn, "bxj", dsc);
cca44b1b 7343 else
7ff120b4 7344 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7345
7346 case 0x3:
7347 if (op == 0x1)
7ff120b4 7348 return arm_copy_bx_blx_reg (gdbarch, insn,
0963b4bd 7349 regs, dsc); /* blx register. */
cca44b1b 7350 else
7ff120b4 7351 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7352
7353 case 0x5:
7ff120b4 7354 return arm_copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
cca44b1b
JB
7355
7356 case 0x7:
7357 if (op == 0x1)
7ff120b4 7358 return arm_copy_unmodified (gdbarch, insn, "bkpt", dsc);
cca44b1b 7359 else if (op == 0x3)
dda83cd7 7360 /* Not really supported. */
7ff120b4 7361 return arm_copy_unmodified (gdbarch, insn, "smc", dsc);
86a73007 7362 /* Fall through. */
cca44b1b
JB
7363
7364 default:
7ff120b4 7365 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7366 }
7367}
7368
7369static int
7ff120b4
YQ
7370arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
7371 struct regcache *regs,
1152d984 7372 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7373{
7374 if (bit (insn, 25))
7375 switch (bits (insn, 20, 24))
7376 {
7377 case 0x10:
7ff120b4 7378 return arm_copy_unmodified (gdbarch, insn, "movw", dsc);
cca44b1b
JB
7379
7380 case 0x14:
7ff120b4 7381 return arm_copy_unmodified (gdbarch, insn, "movt", dsc);
cca44b1b
JB
7382
7383 case 0x12: case 0x16:
7ff120b4 7384 return arm_copy_unmodified (gdbarch, insn, "msr imm", dsc);
cca44b1b
JB
7385
7386 default:
7ff120b4 7387 return arm_copy_alu_imm (gdbarch, insn, regs, dsc);
cca44b1b
JB
7388 }
7389 else
7390 {
7391 uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
7392
7393 if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
7ff120b4 7394 return arm_copy_alu_reg (gdbarch, insn, regs, dsc);
cca44b1b 7395 else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
7ff120b4 7396 return arm_copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
cca44b1b 7397 else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
7ff120b4 7398 return arm_decode_miscellaneous (gdbarch, insn, regs, dsc);
cca44b1b 7399 else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
7ff120b4 7400 return arm_copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
cca44b1b 7401 else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
7ff120b4 7402 return arm_copy_unmodified (gdbarch, insn, "mul/mla", dsc);
cca44b1b 7403 else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
7ff120b4 7404 return arm_copy_unmodified (gdbarch, insn, "synch", dsc);
cca44b1b 7405 else if (op2 == 0xb || (op2 & 0xd) == 0xd)
550dc4e2 7406 /* 2nd arg means "unprivileged". */
7ff120b4
YQ
7407 return arm_copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
7408 dsc);
cca44b1b
JB
7409 }
7410
7411 /* Should be unreachable. */
7412 return 1;
7413}
7414
7415static int
7ff120b4
YQ
7416arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
7417 struct regcache *regs,
1152d984 7418 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7419{
7420 int a = bit (insn, 25), b = bit (insn, 4);
7421 uint32_t op1 = bits (insn, 20, 24);
cca44b1b
JB
7422
7423 if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
7424 || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
0f6f04ba 7425 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 0);
cca44b1b
JB
7426 else if ((!a && (op1 & 0x17) == 0x02)
7427 || (a && (op1 & 0x17) == 0x02 && !b))
0f6f04ba 7428 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 1);
cca44b1b
JB
7429 else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
7430 || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
0f6f04ba 7431 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 0);
cca44b1b
JB
7432 else if ((!a && (op1 & 0x17) == 0x03)
7433 || (a && (op1 & 0x17) == 0x03 && !b))
0f6f04ba 7434 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 1);
cca44b1b
JB
7435 else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
7436 || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
7ff120b4 7437 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
cca44b1b
JB
7438 else if ((!a && (op1 & 0x17) == 0x06)
7439 || (a && (op1 & 0x17) == 0x06 && !b))
7ff120b4 7440 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
cca44b1b
JB
7441 else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
7442 || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
7ff120b4 7443 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
cca44b1b
JB
7444 else if ((!a && (op1 & 0x17) == 0x07)
7445 || (a && (op1 & 0x17) == 0x07 && !b))
7ff120b4 7446 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
cca44b1b
JB
7447
7448 /* Should be unreachable. */
7449 return 1;
7450}
7451
7452static int
7ff120b4 7453arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
1152d984 7454 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7455{
7456 switch (bits (insn, 20, 24))
7457 {
7458 case 0x00: case 0x01: case 0x02: case 0x03:
7ff120b4 7459 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
cca44b1b
JB
7460
7461 case 0x04: case 0x05: case 0x06: case 0x07:
7ff120b4 7462 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
cca44b1b
JB
7463
7464 case 0x08: case 0x09: case 0x0a: case 0x0b:
7465 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
7ff120b4 7466 return arm_copy_unmodified (gdbarch, insn,
cca44b1b
JB
7467 "decode/pack/unpack/saturate/reverse", dsc);
7468
7469 case 0x18:
7470 if (bits (insn, 5, 7) == 0) /* op2. */
7471 {
7472 if (bits (insn, 12, 15) == 0xf)
7ff120b4 7473 return arm_copy_unmodified (gdbarch, insn, "usad8", dsc);
cca44b1b 7474 else
7ff120b4 7475 return arm_copy_unmodified (gdbarch, insn, "usada8", dsc);
cca44b1b
JB
7476 }
7477 else
7ff120b4 7478 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7479
7480 case 0x1a: case 0x1b:
7481 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 7482 return arm_copy_unmodified (gdbarch, insn, "sbfx", dsc);
cca44b1b 7483 else
7ff120b4 7484 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7485
7486 case 0x1c: case 0x1d:
7487 if (bits (insn, 5, 6) == 0x0) /* op2[1:0]. */
7488 {
7489 if (bits (insn, 0, 3) == 0xf)
7ff120b4 7490 return arm_copy_unmodified (gdbarch, insn, "bfc", dsc);
cca44b1b 7491 else
7ff120b4 7492 return arm_copy_unmodified (gdbarch, insn, "bfi", dsc);
cca44b1b
JB
7493 }
7494 else
7ff120b4 7495 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7496
7497 case 0x1e: case 0x1f:
7498 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 7499 return arm_copy_unmodified (gdbarch, insn, "ubfx", dsc);
cca44b1b 7500 else
7ff120b4 7501 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7502 }
7503
7504 /* Should be unreachable. */
7505 return 1;
7506}
7507
7508static int
615234c1 7509arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, uint32_t insn,
7ff120b4 7510 struct regcache *regs,
1152d984 7511 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7512{
7513 if (bit (insn, 25))
7ff120b4 7514 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b 7515 else
7ff120b4 7516 return arm_copy_block_xfer (gdbarch, insn, regs, dsc);
cca44b1b
JB
7517}
7518
7519static int
7ff120b4
YQ
7520arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
7521 struct regcache *regs,
1152d984 7522 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7523{
7524 unsigned int opcode = bits (insn, 20, 24);
7525
7526 switch (opcode)
7527 {
7528 case 0x04: case 0x05: /* VFP/Neon mrrc/mcrr. */
7ff120b4 7529 return arm_copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
cca44b1b
JB
7530
7531 case 0x08: case 0x0a: case 0x0c: case 0x0e:
7532 case 0x12: case 0x16:
7ff120b4 7533 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
cca44b1b
JB
7534
7535 case 0x09: case 0x0b: case 0x0d: case 0x0f:
7536 case 0x13: case 0x17:
7ff120b4 7537 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
cca44b1b
JB
7538
7539 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7540 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7541 /* Note: no writeback for these instructions. Bit 25 will always be
7542 zero though (via caller), so the following works OK. */
7ff120b4 7543 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7544 }
7545
7546 /* Should be unreachable. */
7547 return 1;
7548}
7549
34518530
YQ
7550/* Decode shifted register instructions. */
7551
7552static int
7553thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
7554 uint16_t insn2, struct regcache *regs,
1152d984 7555 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7556{
7557 /* PC is only allowed to be used in instruction MOV. */
7558
7559 unsigned int op = bits (insn1, 5, 8);
7560 unsigned int rn = bits (insn1, 0, 3);
7561
7562 if (op == 0x2 && rn == 0xf) /* MOV */
7563 return thumb2_copy_alu_imm (gdbarch, insn1, insn2, regs, dsc);
7564 else
7565 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7566 "dp (shift reg)", dsc);
7567}
7568
7569
7570/* Decode extension register load/store. Exactly the same as
7571 arm_decode_ext_reg_ld_st. */
7572
7573static int
7574thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
7575 uint16_t insn2, struct regcache *regs,
1152d984 7576 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7577{
7578 unsigned int opcode = bits (insn1, 4, 8);
7579
7580 switch (opcode)
7581 {
7582 case 0x04: case 0x05:
7583 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7584 "vfp/neon vmov", dsc);
7585
7586 case 0x08: case 0x0c: /* 01x00 */
7587 case 0x0a: case 0x0e: /* 01x10 */
7588 case 0x12: case 0x16: /* 10x10 */
7589 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7590 "vfp/neon vstm/vpush", dsc);
7591
7592 case 0x09: case 0x0d: /* 01x01 */
7593 case 0x0b: case 0x0f: /* 01x11 */
7594 case 0x13: case 0x17: /* 10x11 */
7595 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7596 "vfp/neon vldm/vpop", dsc);
7597
7598 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7599 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7600 "vstr", dsc);
7601 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7602 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2, regs, dsc);
7603 }
7604
7605 /* Should be unreachable. */
7606 return 1;
7607}
7608
cca44b1b 7609static int
12545665 7610arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn,
1152d984 7611 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7612{
7613 unsigned int op1 = bits (insn, 20, 25);
7614 int op = bit (insn, 4);
7615 unsigned int coproc = bits (insn, 8, 11);
cca44b1b
JB
7616
7617 if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
7ff120b4 7618 return arm_decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
cca44b1b
JB
7619 else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
7620 && (coproc & 0xe) != 0xa)
7621 /* stc/stc2. */
7ff120b4 7622 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7623 else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
7624 && (coproc & 0xe) != 0xa)
7625 /* ldc/ldc2 imm/lit. */
7ff120b4 7626 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 7627 else if ((op1 & 0x3e) == 0x00)
7ff120b4 7628 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b 7629 else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
7ff120b4 7630 return arm_copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
cca44b1b 7631 else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
7ff120b4 7632 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b 7633 else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
7ff120b4 7634 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
7635 else if ((op1 & 0x30) == 0x20 && !op)
7636 {
7637 if ((coproc & 0xe) == 0xa)
7ff120b4 7638 return arm_copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
cca44b1b 7639 else
7ff120b4 7640 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7641 }
7642 else if ((op1 & 0x30) == 0x20 && op)
7ff120b4 7643 return arm_copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
cca44b1b 7644 else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
7ff120b4 7645 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 7646 else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
7ff120b4 7647 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 7648 else if ((op1 & 0x30) == 0x30)
7ff120b4 7649 return arm_copy_svc (gdbarch, insn, regs, dsc);
cca44b1b 7650 else
7ff120b4 7651 return arm_copy_undef (gdbarch, insn, dsc); /* Possibly unreachable. */
cca44b1b
JB
7652}
7653
34518530
YQ
7654static int
7655thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
7656 uint16_t insn2, struct regcache *regs,
1152d984 7657 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7658{
7659 unsigned int coproc = bits (insn2, 8, 11);
34518530
YQ
7660 unsigned int bit_5_8 = bits (insn1, 5, 8);
7661 unsigned int bit_9 = bit (insn1, 9);
7662 unsigned int bit_4 = bit (insn1, 4);
34518530
YQ
7663
7664 if (bit_9 == 0)
7665 {
7666 if (bit_5_8 == 2)
7667 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7668 "neon 64bit xfer/mrrc/mrrc2/mcrr/mcrr2",
7669 dsc);
7670 else if (bit_5_8 == 0) /* UNDEFINED. */
7671 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
7672 else
7673 {
7674 /*coproc is 101x. SIMD/VFP, ext registers load/store. */
7675 if ((coproc & 0xe) == 0xa)
7676 return thumb2_decode_ext_reg_ld_st (gdbarch, insn1, insn2, regs,
7677 dsc);
7678 else /* coproc is not 101x. */
7679 {
7680 if (bit_4 == 0) /* STC/STC2. */
7681 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7682 "stc/stc2", dsc);
405feb71 7683 else /* LDC/LDC2 {literal, immediate}. */
34518530
YQ
7684 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2,
7685 regs, dsc);
7686 }
7687 }
7688 }
7689 else
7690 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "coproc", dsc);
7691
7692 return 0;
7693}
7694
7695static void
7696install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 7697 arm_displaced_step_copy_insn_closure *dsc, int rd)
34518530
YQ
7698{
7699 /* ADR Rd, #imm
7700
7701 Rewrite as:
7702
7703 Preparation: Rd <- PC
7704 Insn: ADD Rd, #imm
7705 Cleanup: Null.
7706 */
7707
7708 /* Rd <- PC */
7709 int val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
7710 displaced_write_reg (regs, dsc, rd, val, CANNOT_WRITE_PC);
7711}
7712
7713static int
7714thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 7715 arm_displaced_step_copy_insn_closure *dsc,
34518530
YQ
7716 int rd, unsigned int imm)
7717{
7718
7719 /* Encoding T2: ADDS Rd, #imm */
7720 dsc->modinsn[0] = (0x3000 | (rd << 8) | imm);
7721
7722 install_pc_relative (gdbarch, regs, dsc, rd);
7723
7724 return 0;
7725}
7726
7727static int
7728thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
7729 struct regcache *regs,
1152d984 7730 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7731{
7732 unsigned int rd = bits (insn, 8, 10);
7733 unsigned int imm8 = bits (insn, 0, 7);
7734
136821d9
SM
7735 displaced_debug_printf ("copying thumb adr r%d, #%d insn %.4x",
7736 rd, imm8, insn);
34518530
YQ
7737
7738 return thumb_copy_pc_relative_16bit (gdbarch, regs, dsc, rd, imm8);
7739}
7740
7741static int
7742thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
7743 uint16_t insn2, struct regcache *regs,
1152d984 7744 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7745{
7746 unsigned int rd = bits (insn2, 8, 11);
7747 /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
7748 extract raw immediate encoding rather than computing immediate. When
7749 generating ADD or SUB instruction, we can simply perform OR operation to
7750 set immediate into ADD. */
7751 unsigned int imm_3_8 = insn2 & 0x70ff;
7752 unsigned int imm_i = insn1 & 0x0400; /* Clear all bits except bit 10. */
7753
136821d9
SM
7754 displaced_debug_printf ("copying thumb adr r%d, #%d:%d insn %.4x%.4x",
7755 rd, imm_i, imm_3_8, insn1, insn2);
34518530
YQ
7756
7757 if (bit (insn1, 7)) /* Encoding T2 */
7758 {
7759 /* Encoding T3: SUB Rd, Rd, #imm */
7760 dsc->modinsn[0] = (0xf1a0 | rd | imm_i);
7761 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
7762 }
7763 else /* Encoding T3 */
7764 {
7765 /* Encoding T3: ADD Rd, Rd, #imm */
7766 dsc->modinsn[0] = (0xf100 | rd | imm_i);
7767 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
7768 }
7769 dsc->numinsns = 2;
7770
7771 install_pc_relative (gdbarch, regs, dsc, rd);
7772
7773 return 0;
7774}
7775
7776static int
615234c1 7777thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, uint16_t insn1,
34518530 7778 struct regcache *regs,
1152d984 7779 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7780{
7781 unsigned int rt = bits (insn1, 8, 10);
7782 unsigned int pc;
7783 int imm8 = (bits (insn1, 0, 7) << 2);
34518530
YQ
7784
7785 /* LDR Rd, #imm8
7786
7787 Rwrite as:
7788
7789 Preparation: tmp0 <- R0, tmp2 <- R2, tmp3 <- R3, R2 <- PC, R3 <- #imm8;
7790
7791 Insn: LDR R0, [R2, R3];
7792 Cleanup: R2 <- tmp2, R3 <- tmp3, Rd <- R0, R0 <- tmp0 */
7793
136821d9 7794 displaced_debug_printf ("copying thumb ldr r%d [pc #%d]", rt, imm8);
34518530
YQ
7795
7796 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
7797 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
7798 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
7799 pc = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
7800 /* The assembler calculates the required value of the offset from the
7801 Align(PC,4) value of this instruction to the label. */
7802 pc = pc & 0xfffffffc;
7803
7804 displaced_write_reg (regs, dsc, 2, pc, CANNOT_WRITE_PC);
7805 displaced_write_reg (regs, dsc, 3, imm8, CANNOT_WRITE_PC);
7806
7807 dsc->rd = rt;
7808 dsc->u.ldst.xfersize = 4;
7809 dsc->u.ldst.rn = 0;
7810 dsc->u.ldst.immed = 0;
7811 dsc->u.ldst.writeback = 0;
7812 dsc->u.ldst.restore_r4 = 0;
7813
7814 dsc->modinsn[0] = 0x58d0; /* ldr r0, [r2, r3]*/
7815
7816 dsc->cleanup = &cleanup_load;
7817
7818 return 0;
7819}
7820
405feb71 7821/* Copy Thumb cbnz/cbz instruction. */
34518530
YQ
7822
7823static int
7824thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
7825 struct regcache *regs,
1152d984 7826 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7827{
7828 int non_zero = bit (insn1, 11);
7829 unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
7830 CORE_ADDR from = dsc->insn_addr;
7831 int rn = bits (insn1, 0, 2);
7832 int rn_val = displaced_read_reg (regs, dsc, rn);
7833
7834 dsc->u.branch.cond = (rn_val && non_zero) || (!rn_val && !non_zero);
7835 /* CBNZ and CBZ do not affect the condition flags. If condition is true,
7836 set it INST_AL, so cleanup_branch will know branch is taken, otherwise,
7837 condition is false, let it be, cleanup_branch will do nothing. */
7838 if (dsc->u.branch.cond)
7839 {
7840 dsc->u.branch.cond = INST_AL;
7841 dsc->u.branch.dest = from + 4 + imm5;
7842 }
7843 else
7844 dsc->u.branch.dest = from + 2;
7845
7846 dsc->u.branch.link = 0;
7847 dsc->u.branch.exchange = 0;
7848
136821d9
SM
7849 displaced_debug_printf ("copying %s [r%d = 0x%x] insn %.4x to %.8lx",
7850 non_zero ? "cbnz" : "cbz",
7851 rn, rn_val, insn1, dsc->u.branch.dest);
34518530
YQ
7852
7853 dsc->modinsn[0] = THUMB_NOP;
7854
7855 dsc->cleanup = &cleanup_branch;
7856 return 0;
7857}
7858
7859/* Copy Table Branch Byte/Halfword */
7860static int
7861thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
7862 uint16_t insn2, struct regcache *regs,
1152d984 7863 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7864{
7865 ULONGEST rn_val, rm_val;
7866 int is_tbh = bit (insn2, 4);
7867 CORE_ADDR halfwords = 0;
7868 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7869
7870 rn_val = displaced_read_reg (regs, dsc, bits (insn1, 0, 3));
7871 rm_val = displaced_read_reg (regs, dsc, bits (insn2, 0, 3));
7872
7873 if (is_tbh)
7874 {
7875 gdb_byte buf[2];
7876
7877 target_read_memory (rn_val + 2 * rm_val, buf, 2);
7878 halfwords = extract_unsigned_integer (buf, 2, byte_order);
7879 }
7880 else
7881 {
7882 gdb_byte buf[1];
7883
7884 target_read_memory (rn_val + rm_val, buf, 1);
7885 halfwords = extract_unsigned_integer (buf, 1, byte_order);
7886 }
7887
136821d9
SM
7888 displaced_debug_printf ("%s base 0x%x offset 0x%x offset 0x%x",
7889 is_tbh ? "tbh" : "tbb",
7890 (unsigned int) rn_val, (unsigned int) rm_val,
7891 (unsigned int) halfwords);
34518530
YQ
7892
7893 dsc->u.branch.cond = INST_AL;
7894 dsc->u.branch.link = 0;
7895 dsc->u.branch.exchange = 0;
7896 dsc->u.branch.dest = dsc->insn_addr + 4 + 2 * halfwords;
7897
7898 dsc->cleanup = &cleanup_branch;
7899
7900 return 0;
7901}
7902
7903static void
7904cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 7905 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7906{
7907 /* PC <- r7 */
7908 int val = displaced_read_reg (regs, dsc, 7);
7909 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, val, BX_WRITE_PC);
7910
7911 /* r7 <- r8 */
7912 val = displaced_read_reg (regs, dsc, 8);
7913 displaced_write_reg (regs, dsc, 7, val, CANNOT_WRITE_PC);
7914
7915 /* r8 <- tmp[0] */
7916 displaced_write_reg (regs, dsc, 8, dsc->tmp[0], CANNOT_WRITE_PC);
7917
7918}
7919
7920static int
615234c1 7921thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, uint16_t insn1,
34518530 7922 struct regcache *regs,
1152d984 7923 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7924{
7925 dsc->u.block.regmask = insn1 & 0x00ff;
7926
7927 /* Rewrite instruction: POP {rX, rY, ...,rZ, PC}
7928 to :
7929
7930 (1) register list is full, that is, r0-r7 are used.
7931 Prepare: tmp[0] <- r8
7932
7933 POP {r0, r1, ...., r6, r7}; remove PC from reglist
7934 MOV r8, r7; Move value of r7 to r8;
7935 POP {r7}; Store PC value into r7.
7936
7937 Cleanup: PC <- r7, r7 <- r8, r8 <-tmp[0]
7938
7939 (2) register list is not full, supposing there are N registers in
7940 register list (except PC, 0 <= N <= 7).
7941 Prepare: for each i, 0 - N, tmp[i] <- ri.
7942
7943 POP {r0, r1, ...., rN};
7944
7945 Cleanup: Set registers in original reglist from r0 - rN. Restore r0 - rN
7946 from tmp[] properly.
7947 */
136821d9
SM
7948 displaced_debug_printf ("copying thumb pop {%.8x, pc} insn %.4x",
7949 dsc->u.block.regmask, insn1);
34518530
YQ
7950
7951 if (dsc->u.block.regmask == 0xff)
7952 {
7953 dsc->tmp[0] = displaced_read_reg (regs, dsc, 8);
7954
7955 dsc->modinsn[0] = (insn1 & 0xfeff); /* POP {r0,r1,...,r6, r7} */
7956 dsc->modinsn[1] = 0x46b8; /* MOV r8, r7 */
7957 dsc->modinsn[2] = 0xbc80; /* POP {r7} */
7958
7959 dsc->numinsns = 3;
7960 dsc->cleanup = &cleanup_pop_pc_16bit_all;
7961 }
7962 else
7963 {
5f661e03 7964 unsigned int num_in_list = count_one_bits (dsc->u.block.regmask);
bec2ab5a
SM
7965 unsigned int i;
7966 unsigned int new_regmask;
34518530
YQ
7967
7968 for (i = 0; i < num_in_list + 1; i++)
7969 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7970
7971 new_regmask = (1 << (num_in_list + 1)) - 1;
7972
136821d9
SM
7973 displaced_debug_printf ("POP {..., pc}: original reg list %.4x, "
7974 "modified list %.4x",
7975 (int) dsc->u.block.regmask, new_regmask);
34518530
YQ
7976
7977 dsc->u.block.regmask |= 0x8000;
7978 dsc->u.block.writeback = 0;
7979 dsc->u.block.cond = INST_AL;
7980
7981 dsc->modinsn[0] = (insn1 & ~0x1ff) | (new_regmask & 0xff);
7982
7983 dsc->cleanup = &cleanup_block_load_pc;
7984 }
7985
7986 return 0;
7987}
7988
7989static void
7990thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
7991 struct regcache *regs,
1152d984 7992 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7993{
7994 unsigned short op_bit_12_15 = bits (insn1, 12, 15);
7995 unsigned short op_bit_10_11 = bits (insn1, 10, 11);
7996 int err = 0;
7997
7998 /* 16-bit thumb instructions. */
7999 switch (op_bit_12_15)
8000 {
8001 /* Shift (imme), add, subtract, move and compare. */
8002 case 0: case 1: case 2: case 3:
8003 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8004 "shift/add/sub/mov/cmp",
8005 dsc);
8006 break;
8007 case 4:
8008 switch (op_bit_10_11)
8009 {
8010 case 0: /* Data-processing */
8011 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8012 "data-processing",
8013 dsc);
8014 break;
8015 case 1: /* Special data instructions and branch and exchange. */
8016 {
8017 unsigned short op = bits (insn1, 7, 9);
8018 if (op == 6 || op == 7) /* BX or BLX */
8019 err = thumb_copy_bx_blx_reg (gdbarch, insn1, regs, dsc);
8020 else if (bits (insn1, 6, 7) != 0) /* ADD/MOV/CMP high registers. */
8021 err = thumb_copy_alu_reg (gdbarch, insn1, regs, dsc);
8022 else
8023 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "special data",
8024 dsc);
8025 }
8026 break;
8027 default: /* LDR (literal) */
8028 err = thumb_copy_16bit_ldr_literal (gdbarch, insn1, regs, dsc);
8029 }
8030 break;
8031 case 5: case 6: case 7: case 8: case 9: /* Load/Store single data item */
8032 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldr/str", dsc);
8033 break;
8034 case 10:
8035 if (op_bit_10_11 < 2) /* Generate PC-relative address */
8036 err = thumb_decode_pc_relative_16bit (gdbarch, insn1, regs, dsc);
8037 else /* Generate SP-relative address */
8038 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "sp-relative", dsc);
8039 break;
8040 case 11: /* Misc 16-bit instructions */
8041 {
8042 switch (bits (insn1, 8, 11))
8043 {
8044 case 1: case 3: case 9: case 11: /* CBNZ, CBZ */
8045 err = thumb_copy_cbnz_cbz (gdbarch, insn1, regs, dsc);
8046 break;
8047 case 12: case 13: /* POP */
8048 if (bit (insn1, 8)) /* PC is in register list. */
8049 err = thumb_copy_pop_pc_16bit (gdbarch, insn1, regs, dsc);
8050 else
8051 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "pop", dsc);
8052 break;
8053 case 15: /* If-Then, and hints */
8054 if (bits (insn1, 0, 3))
8055 /* If-Then makes up to four following instructions conditional.
8056 IT instruction itself is not conditional, so handle it as a
8057 common unmodified instruction. */
8058 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "If-Then",
8059 dsc);
8060 else
8061 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "hints", dsc);
8062 break;
8063 default:
8064 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "misc", dsc);
8065 }
8066 }
8067 break;
8068 case 12:
8069 if (op_bit_10_11 < 2) /* Store multiple registers */
8070 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "stm", dsc);
8071 else /* Load multiple registers */
8072 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldm", dsc);
8073 break;
8074 case 13: /* Conditional branch and supervisor call */
8075 if (bits (insn1, 9, 11) != 7) /* conditional branch */
8076 err = thumb_copy_b (gdbarch, insn1, dsc);
8077 else
8078 err = thumb_copy_svc (gdbarch, insn1, regs, dsc);
8079 break;
8080 case 14: /* Unconditional branch */
8081 err = thumb_copy_b (gdbarch, insn1, dsc);
8082 break;
8083 default:
8084 err = 1;
8085 }
8086
8087 if (err)
8088 internal_error (__FILE__, __LINE__,
8089 _("thumb_process_displaced_16bit_insn: Instruction decode error"));
8090}
8091
8092static int
8093decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
8094 uint16_t insn1, uint16_t insn2,
8095 struct regcache *regs,
1152d984 8096 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
8097{
8098 int rt = bits (insn2, 12, 15);
8099 int rn = bits (insn1, 0, 3);
8100 int op1 = bits (insn1, 7, 8);
34518530
YQ
8101
8102 switch (bits (insn1, 5, 6))
8103 {
8104 case 0: /* Load byte and memory hints */
8105 if (rt == 0xf) /* PLD/PLI */
8106 {
8107 if (rn == 0xf)
8108 /* PLD literal or Encoding T3 of PLI(immediate, literal). */
8109 return thumb2_copy_preload (gdbarch, insn1, insn2, regs, dsc);
8110 else
8111 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8112 "pli/pld", dsc);
8113 }
8114 else
8115 {
8116 if (rn == 0xf) /* LDRB/LDRSB (literal) */
8117 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8118 1);
8119 else
8120 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8121 "ldrb{reg, immediate}/ldrbt",
8122 dsc);
8123 }
8124
8125 break;
8126 case 1: /* Load halfword and memory hints. */
8127 if (rt == 0xf) /* PLD{W} and Unalloc memory hint. */
8128 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8129 "pld/unalloc memhint", dsc);
8130 else
8131 {
8132 if (rn == 0xf)
8133 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8134 2);
8135 else
8136 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8137 "ldrh/ldrht", dsc);
8138 }
8139 break;
8140 case 2: /* Load word */
8141 {
8142 int insn2_bit_8_11 = bits (insn2, 8, 11);
8143
8144 if (rn == 0xf)
8145 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc, 4);
8146 else if (op1 == 0x1) /* Encoding T3 */
8147 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs, dsc,
8148 0, 1);
8149 else /* op1 == 0x0 */
8150 {
8151 if (insn2_bit_8_11 == 0xc || (insn2_bit_8_11 & 0x9) == 0x9)
8152 /* LDR (immediate) */
8153 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8154 dsc, bit (insn2, 8), 1);
8155 else if (insn2_bit_8_11 == 0xe) /* LDRT */
8156 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8157 "ldrt", dsc);
8158 else
8159 /* LDR (register) */
8160 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8161 dsc, 0, 0);
8162 }
8163 break;
8164 }
8165 default:
8166 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
8167 break;
8168 }
8169 return 0;
8170}
8171
8172static void
8173thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8174 uint16_t insn2, struct regcache *regs,
1152d984 8175 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
8176{
8177 int err = 0;
8178 unsigned short op = bit (insn2, 15);
8179 unsigned int op1 = bits (insn1, 11, 12);
8180
8181 switch (op1)
8182 {
8183 case 1:
8184 {
8185 switch (bits (insn1, 9, 10))
8186 {
8187 case 0:
8188 if (bit (insn1, 6))
8189 {
405feb71 8190 /* Load/store {dual, exclusive}, table branch. */
34518530
YQ
8191 if (bits (insn1, 7, 8) == 1 && bits (insn1, 4, 5) == 1
8192 && bits (insn2, 5, 7) == 0)
8193 err = thumb2_copy_table_branch (gdbarch, insn1, insn2, regs,
8194 dsc);
8195 else
8196 /* PC is not allowed to use in load/store {dual, exclusive}
8197 instructions. */
8198 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8199 "load/store dual/ex", dsc);
8200 }
8201 else /* load/store multiple */
8202 {
8203 switch (bits (insn1, 7, 8))
8204 {
8205 case 0: case 3: /* SRS, RFE */
8206 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8207 "srs/rfe", dsc);
8208 break;
8209 case 1: case 2: /* LDM/STM/PUSH/POP */
8210 err = thumb2_copy_block_xfer (gdbarch, insn1, insn2, regs, dsc);
8211 break;
8212 }
8213 }
8214 break;
8215
8216 case 1:
8217 /* Data-processing (shift register). */
8218 err = thumb2_decode_dp_shift_reg (gdbarch, insn1, insn2, regs,
8219 dsc);
8220 break;
8221 default: /* Coprocessor instructions. */
8222 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8223 break;
8224 }
8225 break;
8226 }
8227 case 2: /* op1 = 2 */
8228 if (op) /* Branch and misc control. */
8229 {
8230 if (bit (insn2, 14) /* BLX/BL */
8231 || bit (insn2, 12) /* Unconditional branch */
8232 || (bits (insn1, 7, 9) != 0x7)) /* Conditional branch */
8233 err = thumb2_copy_b_bl_blx (gdbarch, insn1, insn2, regs, dsc);
8234 else
8235 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8236 "misc ctrl", dsc);
8237 }
8238 else
8239 {
8240 if (bit (insn1, 9)) /* Data processing (plain binary imm). */
8241 {
b926417a 8242 int dp_op = bits (insn1, 4, 8);
34518530 8243 int rn = bits (insn1, 0, 3);
b926417a 8244 if ((dp_op == 0 || dp_op == 0xa) && rn == 0xf)
34518530
YQ
8245 err = thumb_copy_pc_relative_32bit (gdbarch, insn1, insn2,
8246 regs, dsc);
8247 else
8248 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8249 "dp/pb", dsc);
8250 }
405feb71 8251 else /* Data processing (modified immediate) */
34518530
YQ
8252 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8253 "dp/mi", dsc);
8254 }
8255 break;
8256 case 3: /* op1 = 3 */
8257 switch (bits (insn1, 9, 10))
8258 {
8259 case 0:
8260 if (bit (insn1, 4))
8261 err = decode_thumb_32bit_ld_mem_hints (gdbarch, insn1, insn2,
8262 regs, dsc);
8263 else /* NEON Load/Store and Store single data item */
8264 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8265 "neon elt/struct load/store",
8266 dsc);
8267 break;
8268 case 1: /* op1 = 3, bits (9, 10) == 1 */
8269 switch (bits (insn1, 7, 8))
8270 {
8271 case 0: case 1: /* Data processing (register) */
8272 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8273 "dp(reg)", dsc);
8274 break;
8275 case 2: /* Multiply and absolute difference */
8276 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8277 "mul/mua/diff", dsc);
8278 break;
8279 case 3: /* Long multiply and divide */
8280 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8281 "lmul/lmua", dsc);
8282 break;
8283 }
8284 break;
8285 default: /* Coprocessor instructions */
8286 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8287 break;
8288 }
8289 break;
8290 default:
8291 err = 1;
8292 }
8293
8294 if (err)
8295 internal_error (__FILE__, __LINE__,
8296 _("thumb_process_displaced_32bit_insn: Instruction decode error"));
8297
8298}
8299
b434a28f
YQ
8300static void
8301thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
12545665 8302 struct regcache *regs,
1152d984 8303 arm_displaced_step_copy_insn_closure *dsc)
b434a28f 8304{
34518530
YQ
8305 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8306 uint16_t insn1
8307 = read_memory_unsigned_integer (from, 2, byte_order_for_code);
8308
136821d9
SM
8309 displaced_debug_printf ("process thumb insn %.4x at %.8lx",
8310 insn1, (unsigned long) from);
34518530
YQ
8311
8312 dsc->is_thumb = 1;
8313 dsc->insn_size = thumb_insn_size (insn1);
8314 if (thumb_insn_size (insn1) == 4)
8315 {
8316 uint16_t insn2
8317 = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
8318 thumb_process_displaced_32bit_insn (gdbarch, insn1, insn2, regs, dsc);
8319 }
8320 else
8321 thumb_process_displaced_16bit_insn (gdbarch, insn1, regs, dsc);
b434a28f
YQ
8322}
8323
cca44b1b 8324void
b434a28f
YQ
8325arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8326 CORE_ADDR to, struct regcache *regs,
1152d984 8327 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
8328{
8329 int err = 0;
b434a28f
YQ
8330 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8331 uint32_t insn;
cca44b1b
JB
8332
8333 /* Most displaced instructions use a 1-instruction scratch space, so set this
8334 here and override below if/when necessary. */
8335 dsc->numinsns = 1;
8336 dsc->insn_addr = from;
8337 dsc->scratch_base = to;
8338 dsc->cleanup = NULL;
8339 dsc->wrote_to_pc = 0;
8340
b434a28f 8341 if (!displaced_in_arm_mode (regs))
12545665 8342 return thumb_process_displaced_insn (gdbarch, from, regs, dsc);
b434a28f 8343
4db71c0b
YQ
8344 dsc->is_thumb = 0;
8345 dsc->insn_size = 4;
b434a28f 8346 insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
136821d9
SM
8347 displaced_debug_printf ("stepping insn %.8lx at %.8lx",
8348 (unsigned long) insn, (unsigned long) from);
b434a28f 8349
cca44b1b 8350 if ((insn & 0xf0000000) == 0xf0000000)
7ff120b4 8351 err = arm_decode_unconditional (gdbarch, insn, regs, dsc);
cca44b1b
JB
8352 else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
8353 {
8354 case 0x0: case 0x1: case 0x2: case 0x3:
7ff120b4 8355 err = arm_decode_dp_misc (gdbarch, insn, regs, dsc);
cca44b1b
JB
8356 break;
8357
8358 case 0x4: case 0x5: case 0x6:
7ff120b4 8359 err = arm_decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
cca44b1b
JB
8360 break;
8361
8362 case 0x7:
7ff120b4 8363 err = arm_decode_media (gdbarch, insn, dsc);
cca44b1b
JB
8364 break;
8365
8366 case 0x8: case 0x9: case 0xa: case 0xb:
7ff120b4 8367 err = arm_decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
cca44b1b
JB
8368 break;
8369
8370 case 0xc: case 0xd: case 0xe: case 0xf:
12545665 8371 err = arm_decode_svc_copro (gdbarch, insn, regs, dsc);
cca44b1b
JB
8372 break;
8373 }
8374
8375 if (err)
8376 internal_error (__FILE__, __LINE__,
8377 _("arm_process_displaced_insn: Instruction decode error"));
8378}
8379
8380/* Actually set up the scratch space for a displaced instruction. */
8381
8382void
8383arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
1152d984
SM
8384 CORE_ADDR to,
8385 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 8386{
08106042 8387 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
4db71c0b 8388 unsigned int i, len, offset;
cca44b1b 8389 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4db71c0b 8390 int size = dsc->is_thumb? 2 : 4;
948f8e3d 8391 const gdb_byte *bkp_insn;
cca44b1b 8392
4db71c0b 8393 offset = 0;
cca44b1b
JB
8394 /* Poke modified instruction(s). */
8395 for (i = 0; i < dsc->numinsns; i++)
8396 {
136821d9
SM
8397 if (size == 4)
8398 displaced_debug_printf ("writing insn %.8lx at %.8lx",
8399 dsc->modinsn[i], (unsigned long) to + offset);
8400 else if (size == 2)
8401 displaced_debug_printf ("writing insn %.4x at %.8lx",
8402 (unsigned short) dsc->modinsn[i],
8403 (unsigned long) to + offset);
4db71c0b 8404
4db71c0b
YQ
8405 write_memory_unsigned_integer (to + offset, size,
8406 byte_order_for_code,
cca44b1b 8407 dsc->modinsn[i]);
4db71c0b
YQ
8408 offset += size;
8409 }
8410
8411 /* Choose the correct breakpoint instruction. */
8412 if (dsc->is_thumb)
8413 {
8414 bkp_insn = tdep->thumb_breakpoint;
8415 len = tdep->thumb_breakpoint_size;
8416 }
8417 else
8418 {
8419 bkp_insn = tdep->arm_breakpoint;
8420 len = tdep->arm_breakpoint_size;
cca44b1b
JB
8421 }
8422
8423 /* Put breakpoint afterwards. */
4db71c0b 8424 write_memory (to + offset, bkp_insn, len);
cca44b1b 8425
136821d9
SM
8426 displaced_debug_printf ("copy %s->%s", paddress (gdbarch, from),
8427 paddress (gdbarch, to));
cca44b1b
JB
8428}
8429
cca44b1b
JB
8430/* Entry point for cleaning things up after a displaced instruction has been
8431 single-stepped. */
8432
8433void
8434arm_displaced_step_fixup (struct gdbarch *gdbarch,
1152d984 8435 struct displaced_step_copy_insn_closure *dsc_,
cca44b1b
JB
8436 CORE_ADDR from, CORE_ADDR to,
8437 struct regcache *regs)
8438{
1152d984
SM
8439 arm_displaced_step_copy_insn_closure *dsc
8440 = (arm_displaced_step_copy_insn_closure *) dsc_;
cfba9872 8441
cca44b1b
JB
8442 if (dsc->cleanup)
8443 dsc->cleanup (gdbarch, regs, dsc);
8444
8445 if (!dsc->wrote_to_pc)
4db71c0b
YQ
8446 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
8447 dsc->insn_addr + dsc->insn_size);
8448
cca44b1b
JB
8449}
8450
8451#include "bfd-in2.h"
8452#include "libcoff.h"
8453
8454static int
8455gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
8456{
f0c2e3e0
AB
8457 gdb_disassemble_info *di
8458 = static_cast<gdb_disassemble_info *> (info->application_data);
e47ad6c0 8459 struct gdbarch *gdbarch = di->arch ();
9779414d
DJ
8460
8461 if (arm_pc_is_thumb (gdbarch, memaddr))
cca44b1b
JB
8462 {
8463 static asymbol *asym;
8464 static combined_entry_type ce;
8465 static struct coff_symbol_struct csym;
8466 static struct bfd fake_bfd;
8467 static bfd_target fake_target;
8468
8469 if (csym.native == NULL)
8470 {
8471 /* Create a fake symbol vector containing a Thumb symbol.
8472 This is solely so that the code in print_insn_little_arm()
8473 and print_insn_big_arm() in opcodes/arm-dis.c will detect
8474 the presence of a Thumb symbol and switch to decoding
8475 Thumb instructions. */
8476
8477 fake_target.flavour = bfd_target_coff_flavour;
8478 fake_bfd.xvec = &fake_target;
8479 ce.u.syment.n_sclass = C_THUMBEXTFUNC;
8480 csym.native = &ce;
8481 csym.symbol.the_bfd = &fake_bfd;
8482 csym.symbol.name = "fake";
8483 asym = (asymbol *) & csym;
8484 }
8485
8486 memaddr = UNMAKE_THUMB_ADDR (memaddr);
8487 info->symbols = &asym;
8488 }
8489 else
8490 info->symbols = NULL;
8491
e60eb288
YQ
8492 /* GDB is able to get bfd_mach from the exe_bfd, info->mach is
8493 accurate, so mark USER_SPECIFIED_MACHINE_TYPE bit. Otherwise,
8494 opcodes/arm-dis.c:print_insn reset info->mach, and it will trigger
7e10abd1
TT
8495 the assert on the mismatch of info->mach and
8496 bfd_get_mach (current_program_space->exec_bfd ()) in
8497 default_print_insn. */
3047c786
TV
8498 if (current_program_space->exec_bfd () != NULL
8499 && (current_program_space->exec_bfd ()->arch_info
8500 == gdbarch_bfd_arch_info (gdbarch)))
e60eb288
YQ
8501 info->flags |= USER_SPECIFIED_MACHINE_TYPE;
8502
6394c606 8503 return default_print_insn (memaddr, info);
cca44b1b
JB
8504}
8505
8506/* The following define instruction sequences that will cause ARM
8507 cpu's to take an undefined instruction trap. These are used to
8508 signal a breakpoint to GDB.
8509
8510 The newer ARMv4T cpu's are capable of operating in ARM or Thumb
8511 modes. A different instruction is required for each mode. The ARM
8512 cpu's can also be big or little endian. Thus four different
8513 instructions are needed to support all cases.
8514
8515 Note: ARMv4 defines several new instructions that will take the
8516 undefined instruction trap. ARM7TDMI is nominally ARMv4T, but does
8517 not in fact add the new instructions. The new undefined
8518 instructions in ARMv4 are all instructions that had no defined
8519 behaviour in earlier chips. There is no guarantee that they will
8520 raise an exception, but may be treated as NOP's. In practice, it
8521 may only safe to rely on instructions matching:
8522
8523 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
8524 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
8525 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
8526
0963b4bd 8527 Even this may only true if the condition predicate is true. The
cca44b1b
JB
8528 following use a condition predicate of ALWAYS so it is always TRUE.
8529
8530 There are other ways of forcing a breakpoint. GNU/Linux, RISC iX,
8531 and NetBSD all use a software interrupt rather than an undefined
8532 instruction to force a trap. This can be handled by by the
8533 abi-specific code during establishment of the gdbarch vector. */
8534
8535#define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
8536#define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
8537#define THUMB_LE_BREAKPOINT {0xbe,0xbe}
8538#define THUMB_BE_BREAKPOINT {0xbe,0xbe}
8539
948f8e3d
PA
8540static const gdb_byte arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
8541static const gdb_byte arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
8542static const gdb_byte arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
8543static const gdb_byte arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
cca44b1b 8544
cd6c3b4f
YQ
8545/* Implement the breakpoint_kind_from_pc gdbarch method. */
8546
d19280ad
YQ
8547static int
8548arm_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
cca44b1b 8549{
08106042 8550 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
177321bd 8551 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
cca44b1b 8552
9779414d 8553 if (arm_pc_is_thumb (gdbarch, *pcptr))
cca44b1b
JB
8554 {
8555 *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
177321bd
DJ
8556
8557 /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
8558 check whether we are replacing a 32-bit instruction. */
8559 if (tdep->thumb2_breakpoint != NULL)
8560 {
8561 gdb_byte buf[2];
d19280ad 8562
177321bd
DJ
8563 if (target_read_memory (*pcptr, buf, 2) == 0)
8564 {
8565 unsigned short inst1;
d19280ad 8566
177321bd 8567 inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
db24da6d 8568 if (thumb_insn_size (inst1) == 4)
d19280ad 8569 return ARM_BP_KIND_THUMB2;
177321bd
DJ
8570 }
8571 }
8572
d19280ad 8573 return ARM_BP_KIND_THUMB;
cca44b1b
JB
8574 }
8575 else
d19280ad
YQ
8576 return ARM_BP_KIND_ARM;
8577
8578}
8579
cd6c3b4f
YQ
8580/* Implement the sw_breakpoint_from_kind gdbarch method. */
8581
d19280ad
YQ
8582static const gdb_byte *
8583arm_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
8584{
08106042 8585 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
d19280ad
YQ
8586
8587 switch (kind)
cca44b1b 8588 {
d19280ad
YQ
8589 case ARM_BP_KIND_ARM:
8590 *size = tdep->arm_breakpoint_size;
cca44b1b 8591 return tdep->arm_breakpoint;
d19280ad
YQ
8592 case ARM_BP_KIND_THUMB:
8593 *size = tdep->thumb_breakpoint_size;
8594 return tdep->thumb_breakpoint;
8595 case ARM_BP_KIND_THUMB2:
8596 *size = tdep->thumb2_breakpoint_size;
8597 return tdep->thumb2_breakpoint;
8598 default:
8599 gdb_assert_not_reached ("unexpected arm breakpoint kind");
cca44b1b
JB
8600 }
8601}
8602
833b7ab5
YQ
8603/* Implement the breakpoint_kind_from_current_state gdbarch method. */
8604
8605static int
8606arm_breakpoint_kind_from_current_state (struct gdbarch *gdbarch,
8607 struct regcache *regcache,
8608 CORE_ADDR *pcptr)
8609{
8610 gdb_byte buf[4];
8611
8612 /* Check the memory pointed by PC is readable. */
8613 if (target_read_memory (regcache_read_pc (regcache), buf, 4) == 0)
8614 {
8615 struct arm_get_next_pcs next_pcs_ctx;
833b7ab5
YQ
8616
8617 arm_get_next_pcs_ctor (&next_pcs_ctx,
8618 &arm_get_next_pcs_ops,
8619 gdbarch_byte_order (gdbarch),
8620 gdbarch_byte_order_for_code (gdbarch),
8621 0,
8622 regcache);
8623
a0ff9e1a 8624 std::vector<CORE_ADDR> next_pcs = arm_get_next_pcs (&next_pcs_ctx);
833b7ab5
YQ
8625
8626 /* If MEMADDR is the next instruction of current pc, do the
8627 software single step computation, and get the thumb mode by
8628 the destination address. */
a0ff9e1a 8629 for (CORE_ADDR pc : next_pcs)
833b7ab5
YQ
8630 {
8631 if (UNMAKE_THUMB_ADDR (pc) == *pcptr)
8632 {
833b7ab5
YQ
8633 if (IS_THUMB_ADDR (pc))
8634 {
8635 *pcptr = MAKE_THUMB_ADDR (*pcptr);
8636 return arm_breakpoint_kind_from_pc (gdbarch, pcptr);
8637 }
8638 else
8639 return ARM_BP_KIND_ARM;
8640 }
8641 }
833b7ab5
YQ
8642 }
8643
8644 return arm_breakpoint_kind_from_pc (gdbarch, pcptr);
8645}
8646
cca44b1b
JB
8647/* Extract from an array REGBUF containing the (raw) register state a
8648 function return value of type TYPE, and copy that, in virtual
8649 format, into VALBUF. */
8650
8651static void
8652arm_extract_return_value (struct type *type, struct regcache *regs,
8653 gdb_byte *valbuf)
8654{
ac7936df 8655 struct gdbarch *gdbarch = regs->arch ();
cca44b1b 8656 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
08106042 8657 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
cca44b1b 8658
78134374 8659 if (TYPE_CODE_FLT == type->code ())
cca44b1b 8660 {
345bd07c 8661 switch (tdep->fp_model)
cca44b1b
JB
8662 {
8663 case ARM_FLOAT_FPA:
8664 {
8665 /* The value is in register F0 in internal format. We need to
8666 extract the raw value and then convert it to the desired
8667 internal type. */
f0452268 8668 bfd_byte tmpbuf[ARM_FP_REGISTER_SIZE];
cca44b1b 8669
dca08e1f 8670 regs->cooked_read (ARM_F0_REGNUM, tmpbuf);
3b2ca824
UW
8671 target_float_convert (tmpbuf, arm_ext_type (gdbarch),
8672 valbuf, type);
cca44b1b
JB
8673 }
8674 break;
8675
8676 case ARM_FLOAT_SOFT_FPA:
8677 case ARM_FLOAT_SOFT_VFP:
8678 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8679 not using the VFP ABI code. */
8680 case ARM_FLOAT_VFP:
dca08e1f 8681 regs->cooked_read (ARM_A1_REGNUM, valbuf);
cca44b1b 8682 if (TYPE_LENGTH (type) > 4)
f0452268
AH
8683 regs->cooked_read (ARM_A1_REGNUM + 1,
8684 valbuf + ARM_INT_REGISTER_SIZE);
cca44b1b
JB
8685 break;
8686
8687 default:
0963b4bd
MS
8688 internal_error (__FILE__, __LINE__,
8689 _("arm_extract_return_value: "
8690 "Floating point model not supported"));
cca44b1b
JB
8691 break;
8692 }
8693 }
78134374
SM
8694 else if (type->code () == TYPE_CODE_INT
8695 || type->code () == TYPE_CODE_CHAR
8696 || type->code () == TYPE_CODE_BOOL
8697 || type->code () == TYPE_CODE_PTR
aa006118 8698 || TYPE_IS_REFERENCE (type)
a6617193
JB
8699 || type->code () == TYPE_CODE_ENUM
8700 || is_fixed_point_type (type))
cca44b1b 8701 {
b021a221
MS
8702 /* If the type is a plain integer, then the access is
8703 straight-forward. Otherwise we have to play around a bit
8704 more. */
cca44b1b
JB
8705 int len = TYPE_LENGTH (type);
8706 int regno = ARM_A1_REGNUM;
8707 ULONGEST tmp;
8708
8709 while (len > 0)
8710 {
8711 /* By using store_unsigned_integer we avoid having to do
8712 anything special for small big-endian values. */
8713 regcache_cooked_read_unsigned (regs, regno++, &tmp);
8714 store_unsigned_integer (valbuf,
f0452268
AH
8715 (len > ARM_INT_REGISTER_SIZE
8716 ? ARM_INT_REGISTER_SIZE : len),
cca44b1b 8717 byte_order, tmp);
f0452268
AH
8718 len -= ARM_INT_REGISTER_SIZE;
8719 valbuf += ARM_INT_REGISTER_SIZE;
cca44b1b
JB
8720 }
8721 }
8722 else
8723 {
8724 /* For a structure or union the behaviour is as if the value had
dda83cd7
SM
8725 been stored to word-aligned memory and then loaded into
8726 registers with 32-bit load instruction(s). */
cca44b1b
JB
8727 int len = TYPE_LENGTH (type);
8728 int regno = ARM_A1_REGNUM;
f0452268 8729 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
cca44b1b
JB
8730
8731 while (len > 0)
8732 {
dca08e1f 8733 regs->cooked_read (regno++, tmpbuf);
cca44b1b 8734 memcpy (valbuf, tmpbuf,
f0452268
AH
8735 len > ARM_INT_REGISTER_SIZE ? ARM_INT_REGISTER_SIZE : len);
8736 len -= ARM_INT_REGISTER_SIZE;
8737 valbuf += ARM_INT_REGISTER_SIZE;
cca44b1b
JB
8738 }
8739 }
8740}
8741
8742
8743/* Will a function return an aggregate type in memory or in a
8744 register? Return 0 if an aggregate type can be returned in a
8745 register, 1 if it must be returned in memory. */
8746
8747static int
8748arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
8749{
cca44b1b
JB
8750 enum type_code code;
8751
f168693b 8752 type = check_typedef (type);
cca44b1b 8753
b13c8ab2
YQ
8754 /* Simple, non-aggregate types (ie not including vectors and
8755 complex) are always returned in a register (or registers). */
78134374 8756 code = type->code ();
b13c8ab2
YQ
8757 if (TYPE_CODE_STRUCT != code && TYPE_CODE_UNION != code
8758 && TYPE_CODE_ARRAY != code && TYPE_CODE_COMPLEX != code)
8759 return 0;
cca44b1b 8760
bd63c870 8761 if (TYPE_CODE_ARRAY == code && type->is_vector ())
c4312b19
YQ
8762 {
8763 /* Vector values should be returned using ARM registers if they
8764 are not over 16 bytes. */
8765 return (TYPE_LENGTH (type) > 16);
8766 }
8767
08106042 8768 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
345bd07c 8769 if (tdep->arm_abi != ARM_ABI_APCS)
cca44b1b 8770 {
b13c8ab2
YQ
8771 /* The AAPCS says all aggregates not larger than a word are returned
8772 in a register. */
bab22d06
LM
8773 if (TYPE_LENGTH (type) <= ARM_INT_REGISTER_SIZE
8774 && language_pass_by_reference (type).trivially_copyable)
b13c8ab2
YQ
8775 return 0;
8776
cca44b1b
JB
8777 return 1;
8778 }
b13c8ab2
YQ
8779 else
8780 {
8781 int nRc;
cca44b1b 8782
b13c8ab2
YQ
8783 /* All aggregate types that won't fit in a register must be returned
8784 in memory. */
bab22d06
LM
8785 if (TYPE_LENGTH (type) > ARM_INT_REGISTER_SIZE
8786 || !language_pass_by_reference (type).trivially_copyable)
b13c8ab2 8787 return 1;
cca44b1b 8788
b13c8ab2
YQ
8789 /* In the ARM ABI, "integer" like aggregate types are returned in
8790 registers. For an aggregate type to be integer like, its size
f0452268 8791 must be less than or equal to ARM_INT_REGISTER_SIZE and the
b13c8ab2
YQ
8792 offset of each addressable subfield must be zero. Note that bit
8793 fields are not addressable, and all addressable subfields of
8794 unions always start at offset zero.
cca44b1b 8795
b13c8ab2
YQ
8796 This function is based on the behaviour of GCC 2.95.1.
8797 See: gcc/arm.c: arm_return_in_memory() for details.
cca44b1b 8798
b13c8ab2
YQ
8799 Note: All versions of GCC before GCC 2.95.2 do not set up the
8800 parameters correctly for a function returning the following
8801 structure: struct { float f;}; This should be returned in memory,
8802 not a register. Richard Earnshaw sent me a patch, but I do not
8803 know of any way to detect if a function like the above has been
8804 compiled with the correct calling convention. */
8805
8806 /* Assume all other aggregate types can be returned in a register.
8807 Run a check for structures, unions and arrays. */
8808 nRc = 0;
67255d04 8809
b13c8ab2
YQ
8810 if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
8811 {
8812 int i;
8813 /* Need to check if this struct/union is "integer" like. For
8814 this to be true, its size must be less than or equal to
f0452268 8815 ARM_INT_REGISTER_SIZE and the offset of each addressable
b13c8ab2
YQ
8816 subfield must be zero. Note that bit fields are not
8817 addressable, and unions always start at offset zero. If any
8818 of the subfields is a floating point type, the struct/union
8819 cannot be an integer type. */
8820
8821 /* For each field in the object, check:
8822 1) Is it FP? --> yes, nRc = 1;
8823 2) Is it addressable (bitpos != 0) and
8824 not packed (bitsize == 0)?
8825 --> yes, nRc = 1
8826 */
8827
1f704f76 8828 for (i = 0; i < type->num_fields (); i++)
67255d04 8829 {
b13c8ab2
YQ
8830 enum type_code field_type_code;
8831
8832 field_type_code
940da03e 8833 = check_typedef (type->field (i).type ())->code ();
b13c8ab2
YQ
8834
8835 /* Is it a floating point type field? */
8836 if (field_type_code == TYPE_CODE_FLT)
67255d04
RE
8837 {
8838 nRc = 1;
8839 break;
8840 }
b13c8ab2
YQ
8841
8842 /* If bitpos != 0, then we have to care about it. */
b610c045 8843 if (type->field (i).loc_bitpos () != 0)
b13c8ab2
YQ
8844 {
8845 /* Bitfields are not addressable. If the field bitsize is
8846 zero, then the field is not packed. Hence it cannot be
8847 a bitfield or any other packed type. */
8848 if (TYPE_FIELD_BITSIZE (type, i) == 0)
8849 {
8850 nRc = 1;
8851 break;
8852 }
8853 }
67255d04
RE
8854 }
8855 }
67255d04 8856
b13c8ab2
YQ
8857 return nRc;
8858 }
67255d04
RE
8859}
8860
34e8f22d
RE
8861/* Write into appropriate registers a function return value of type
8862 TYPE, given in virtual format. */
8863
8864static void
b508a996 8865arm_store_return_value (struct type *type, struct regcache *regs,
5238cf52 8866 const gdb_byte *valbuf)
34e8f22d 8867{
ac7936df 8868 struct gdbarch *gdbarch = regs->arch ();
e17a4113 8869 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
be8626e0 8870
78134374 8871 if (type->code () == TYPE_CODE_FLT)
34e8f22d 8872 {
f0452268 8873 gdb_byte buf[ARM_FP_REGISTER_SIZE];
08106042 8874 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
34e8f22d 8875
345bd07c 8876 switch (tdep->fp_model)
08216dd7
RE
8877 {
8878 case ARM_FLOAT_FPA:
8879
3b2ca824 8880 target_float_convert (valbuf, type, buf, arm_ext_type (gdbarch));
b66f5587 8881 regs->cooked_write (ARM_F0_REGNUM, buf);
08216dd7
RE
8882 break;
8883
fd50bc42 8884 case ARM_FLOAT_SOFT_FPA:
08216dd7 8885 case ARM_FLOAT_SOFT_VFP:
90445bd3
DJ
8886 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8887 not using the VFP ABI code. */
8888 case ARM_FLOAT_VFP:
b66f5587 8889 regs->cooked_write (ARM_A1_REGNUM, valbuf);
b508a996 8890 if (TYPE_LENGTH (type) > 4)
f0452268
AH
8891 regs->cooked_write (ARM_A1_REGNUM + 1,
8892 valbuf + ARM_INT_REGISTER_SIZE);
08216dd7
RE
8893 break;
8894
8895 default:
9b20d036
MS
8896 internal_error (__FILE__, __LINE__,
8897 _("arm_store_return_value: Floating "
8898 "point model not supported"));
08216dd7
RE
8899 break;
8900 }
34e8f22d 8901 }
78134374
SM
8902 else if (type->code () == TYPE_CODE_INT
8903 || type->code () == TYPE_CODE_CHAR
8904 || type->code () == TYPE_CODE_BOOL
8905 || type->code () == TYPE_CODE_PTR
aa006118 8906 || TYPE_IS_REFERENCE (type)
78134374 8907 || type->code () == TYPE_CODE_ENUM)
b508a996
RE
8908 {
8909 if (TYPE_LENGTH (type) <= 4)
8910 {
8911 /* Values of one word or less are zero/sign-extended and
8912 returned in r0. */
f0452268 8913 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
b508a996
RE
8914 LONGEST val = unpack_long (type, valbuf);
8915
f0452268 8916 store_signed_integer (tmpbuf, ARM_INT_REGISTER_SIZE, byte_order, val);
b66f5587 8917 regs->cooked_write (ARM_A1_REGNUM, tmpbuf);
b508a996
RE
8918 }
8919 else
8920 {
8921 /* Integral values greater than one word are stored in consecutive
8922 registers starting with r0. This will always be a multiple of
8923 the regiser size. */
8924 int len = TYPE_LENGTH (type);
8925 int regno = ARM_A1_REGNUM;
8926
8927 while (len > 0)
8928 {
b66f5587 8929 regs->cooked_write (regno++, valbuf);
f0452268
AH
8930 len -= ARM_INT_REGISTER_SIZE;
8931 valbuf += ARM_INT_REGISTER_SIZE;
b508a996
RE
8932 }
8933 }
8934 }
34e8f22d 8935 else
b508a996
RE
8936 {
8937 /* For a structure or union the behaviour is as if the value had
dda83cd7
SM
8938 been stored to word-aligned memory and then loaded into
8939 registers with 32-bit load instruction(s). */
b508a996
RE
8940 int len = TYPE_LENGTH (type);
8941 int regno = ARM_A1_REGNUM;
f0452268 8942 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
b508a996
RE
8943
8944 while (len > 0)
8945 {
8946 memcpy (tmpbuf, valbuf,
f0452268 8947 len > ARM_INT_REGISTER_SIZE ? ARM_INT_REGISTER_SIZE : len);
b66f5587 8948 regs->cooked_write (regno++, tmpbuf);
f0452268
AH
8949 len -= ARM_INT_REGISTER_SIZE;
8950 valbuf += ARM_INT_REGISTER_SIZE;
b508a996
RE
8951 }
8952 }
34e8f22d
RE
8953}
8954
2af48f68
PB
8955
8956/* Handle function return values. */
8957
8958static enum return_value_convention
6a3a010b 8959arm_return_value (struct gdbarch *gdbarch, struct value *function,
c055b101
CV
8960 struct type *valtype, struct regcache *regcache,
8961 gdb_byte *readbuf, const gdb_byte *writebuf)
2af48f68 8962{
08106042 8963 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
6a3a010b 8964 struct type *func_type = function ? value_type (function) : NULL;
90445bd3
DJ
8965 enum arm_vfp_cprc_base_type vfp_base_type;
8966 int vfp_base_count;
8967
8968 if (arm_vfp_abi_for_function (gdbarch, func_type)
8969 && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
8970 {
8971 int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
8972 int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
8973 int i;
8974 for (i = 0; i < vfp_base_count; i++)
8975 {
58d6951d
DJ
8976 if (reg_char == 'q')
8977 {
8978 if (writebuf)
8979 arm_neon_quad_write (gdbarch, regcache, i,
8980 writebuf + i * unit_length);
8981
8982 if (readbuf)
8983 arm_neon_quad_read (gdbarch, regcache, i,
8984 readbuf + i * unit_length);
8985 }
8986 else
8987 {
8988 char name_buf[4];
8989 int regnum;
8990
8c042590 8991 xsnprintf (name_buf, sizeof (name_buf), "%c%d", reg_char, i);
58d6951d
DJ
8992 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8993 strlen (name_buf));
8994 if (writebuf)
b66f5587 8995 regcache->cooked_write (regnum, writebuf + i * unit_length);
58d6951d 8996 if (readbuf)
dca08e1f 8997 regcache->cooked_read (regnum, readbuf + i * unit_length);
58d6951d 8998 }
90445bd3
DJ
8999 }
9000 return RETURN_VALUE_REGISTER_CONVENTION;
9001 }
7c00367c 9002
78134374
SM
9003 if (valtype->code () == TYPE_CODE_STRUCT
9004 || valtype->code () == TYPE_CODE_UNION
9005 || valtype->code () == TYPE_CODE_ARRAY)
2af48f68 9006 {
bab22d06
LM
9007 /* From the AAPCS document:
9008
9009 Result return:
9010
9011 A Composite Type larger than 4 bytes, or whose size cannot be
9012 determined statically by both caller and callee, is stored in memory
9013 at an address passed as an extra argument when the function was
9014 called (Parameter Passing, rule A.4). The memory to be used for the
9015 result may be modified at any point during the function call.
9016
9017 Parameter Passing:
9018
9019 A.4: If the subroutine is a function that returns a result in memory,
9020 then the address for the result is placed in r0 and the NCRN is set
9021 to r1. */
7c00367c
MK
9022 if (tdep->struct_return == pcc_struct_return
9023 || arm_return_in_memory (gdbarch, valtype))
bab22d06
LM
9024 {
9025 if (readbuf)
9026 {
9027 CORE_ADDR addr;
9028
9029 regcache->cooked_read (ARM_A1_REGNUM, &addr);
9030 read_memory (addr, readbuf, TYPE_LENGTH (valtype));
9031 }
9032 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
9033 }
2af48f68 9034 }
78134374 9035 else if (valtype->code () == TYPE_CODE_COMPLEX)
b13c8ab2
YQ
9036 {
9037 if (arm_return_in_memory (gdbarch, valtype))
9038 return RETURN_VALUE_STRUCT_CONVENTION;
9039 }
7052e42c 9040
2af48f68
PB
9041 if (writebuf)
9042 arm_store_return_value (valtype, regcache, writebuf);
9043
9044 if (readbuf)
9045 arm_extract_return_value (valtype, regcache, readbuf);
9046
9047 return RETURN_VALUE_REGISTER_CONVENTION;
9048}
9049
9050
9df628e0 9051static int
60ade65d 9052arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
9df628e0 9053{
e17a4113 9054 struct gdbarch *gdbarch = get_frame_arch (frame);
08106042 9055 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
e17a4113 9056 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
9df628e0 9057 CORE_ADDR jb_addr;
f0452268 9058 gdb_byte buf[ARM_INT_REGISTER_SIZE];
9df628e0 9059
60ade65d 9060 jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
9df628e0
RE
9061
9062 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
f0452268 9063 ARM_INT_REGISTER_SIZE))
9df628e0
RE
9064 return 0;
9065
f0452268 9066 *pc = extract_unsigned_integer (buf, ARM_INT_REGISTER_SIZE, byte_order);
9df628e0
RE
9067 return 1;
9068}
40eadf04
SP
9069/* A call to cmse secure entry function "foo" at "a" is modified by
9070 GNU ld as "b".
9071 a) bl xxxx <foo>
9072
9073 <foo>
9074 xxxx:
9075
9076 b) bl yyyy <__acle_se_foo>
9077
9078 section .gnu.sgstubs:
9079 <foo>
9080 yyyy: sg // secure gateway
9081 b.w xxxx <__acle_se_foo> // original_branch_dest
9082
9083 <__acle_se_foo>
9084 xxxx:
9085
9086 When the control at "b", the pc contains "yyyy" (sg address) which is a
9087 trampoline and does not exist in source code. This function returns the
9088 target pc "xxxx". For more details please refer to section 5.4
9089 (Entry functions) and section 3.4.4 (C level development flow of secure code)
9090 of "armv8-m-security-extensions-requirements-on-development-tools-engineering-specification"
9091 document on www.developer.arm.com. */
9092
9093static CORE_ADDR
9094arm_skip_cmse_entry (CORE_ADDR pc, const char *name, struct objfile *objfile)
9095{
9096 int target_len = strlen (name) + strlen ("__acle_se_") + 1;
9097 char *target_name = (char *) alloca (target_len);
9098 xsnprintf (target_name, target_len, "%s%s", "__acle_se_", name);
9099
9100 struct bound_minimal_symbol minsym
9101 = lookup_minimal_symbol (target_name, NULL, objfile);
9102
9103 if (minsym.minsym != nullptr)
4aeddc50 9104 return minsym.value_address ();
40eadf04
SP
9105
9106 return 0;
9107}
9108
9109/* Return true when SEC points to ".gnu.sgstubs" section. */
9110
9111static bool
9112arm_is_sgstubs_section (struct obj_section *sec)
9113{
9114 return (sec != nullptr
9115 && sec->the_bfd_section != nullptr
9116 && sec->the_bfd_section->name != nullptr
9117 && streq (sec->the_bfd_section->name, ".gnu.sgstubs"));
9118}
9df628e0 9119
faa95490
DJ
9120/* Recognize GCC and GNU ld's trampolines. If we are in a trampoline,
9121 return the target PC. Otherwise return 0. */
c906108c
SS
9122
9123CORE_ADDR
52f729a7 9124arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
c906108c 9125{
2c02bd72 9126 const char *name;
faa95490 9127 int namelen;
c906108c
SS
9128 CORE_ADDR start_addr;
9129
9130 /* Find the starting address and name of the function containing the PC. */
9131 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
80d8d390
YQ
9132 {
9133 /* Trampoline 'bx reg' doesn't belong to any functions. Do the
9134 check here. */
9135 start_addr = arm_skip_bx_reg (frame, pc);
9136 if (start_addr != 0)
9137 return start_addr;
9138
9139 return 0;
9140 }
c906108c 9141
faa95490
DJ
9142 /* If PC is in a Thumb call or return stub, return the address of the
9143 target PC, which is in a register. The thunk functions are called
9144 _call_via_xx, where x is the register name. The possible names
3d8d5e79
DJ
9145 are r0-r9, sl, fp, ip, sp, and lr. ARM RealView has similar
9146 functions, named __ARM_call_via_r[0-7]. */
61012eef
GB
9147 if (startswith (name, "_call_via_")
9148 || startswith (name, "__ARM_call_via_"))
c906108c 9149 {
ed9a39eb 9150 /* Use the name suffix to determine which register contains the
dda83cd7 9151 target PC. */
a121b7c1 9152 static const char *table[15] =
c5aa993b
JM
9153 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
9154 "r8", "r9", "sl", "fp", "ip", "sp", "lr"
9155 };
c906108c 9156 int regno;
faa95490 9157 int offset = strlen (name) - 2;
c906108c
SS
9158
9159 for (regno = 0; regno <= 14; regno++)
faa95490 9160 if (strcmp (&name[offset], table[regno]) == 0)
52f729a7 9161 return get_frame_register_unsigned (frame, regno);
c906108c 9162 }
ed9a39eb 9163
faa95490
DJ
9164 /* GNU ld generates __foo_from_arm or __foo_from_thumb for
9165 non-interworking calls to foo. We could decode the stubs
9166 to find the target but it's easier to use the symbol table. */
9167 namelen = strlen (name);
9168 if (name[0] == '_' && name[1] == '_'
9169 && ((namelen > 2 + strlen ("_from_thumb")
61012eef 9170 && startswith (name + namelen - strlen ("_from_thumb"), "_from_thumb"))
faa95490 9171 || (namelen > 2 + strlen ("_from_arm")
61012eef 9172 && startswith (name + namelen - strlen ("_from_arm"), "_from_arm"))))
faa95490
DJ
9173 {
9174 char *target_name;
9175 int target_len = namelen - 2;
3b7344d5 9176 struct bound_minimal_symbol minsym;
faa95490
DJ
9177 struct objfile *objfile;
9178 struct obj_section *sec;
9179
9180 if (name[namelen - 1] == 'b')
9181 target_len -= strlen ("_from_thumb");
9182 else
9183 target_len -= strlen ("_from_arm");
9184
224c3ddb 9185 target_name = (char *) alloca (target_len + 1);
faa95490
DJ
9186 memcpy (target_name, name + 2, target_len);
9187 target_name[target_len] = '\0';
9188
9189 sec = find_pc_section (pc);
9190 objfile = (sec == NULL) ? NULL : sec->objfile;
9191 minsym = lookup_minimal_symbol (target_name, NULL, objfile);
3b7344d5 9192 if (minsym.minsym != NULL)
4aeddc50 9193 return minsym.value_address ();
faa95490
DJ
9194 else
9195 return 0;
9196 }
9197
40eadf04
SP
9198 struct obj_section *section = find_pc_section (pc);
9199
9200 /* Check whether SECTION points to the ".gnu.sgstubs" section. */
9201 if (arm_is_sgstubs_section (section))
9202 return arm_skip_cmse_entry (pc, name, section->objfile);
9203
c5aa993b 9204 return 0; /* not a stub */
c906108c
SS
9205}
9206
28e97307
DJ
9207static void
9208arm_update_current_architecture (void)
fd50bc42 9209{
28e97307 9210 /* If the current architecture is not ARM, we have nothing to do. */
f5656ead 9211 if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_arm)
28e97307 9212 return;
fd50bc42 9213
28e97307 9214 /* Update the architecture. */
b447dd03 9215 gdbarch_info info;
28e97307 9216 if (!gdbarch_update_p (info))
9b20d036 9217 internal_error (__FILE__, __LINE__, _("could not update architecture"));
fd50bc42
RE
9218}
9219
9220static void
eb4c3f4a 9221set_fp_model_sfunc (const char *args, int from_tty,
fd50bc42
RE
9222 struct cmd_list_element *c)
9223{
570dc176 9224 int fp_model;
fd50bc42
RE
9225
9226 for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
9227 if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
9228 {
aead7601 9229 arm_fp_model = (enum arm_float_model) fp_model;
fd50bc42
RE
9230 break;
9231 }
9232
9233 if (fp_model == ARM_FLOAT_LAST)
edefbb7c 9234 internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
fd50bc42
RE
9235 current_fp_model);
9236
28e97307 9237 arm_update_current_architecture ();
fd50bc42
RE
9238}
9239
9240static void
08546159
AC
9241show_fp_model (struct ui_file *file, int from_tty,
9242 struct cmd_list_element *c, const char *value)
fd50bc42 9243{
28e97307 9244 if (arm_fp_model == ARM_FLOAT_AUTO
f5656ead 9245 && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
6dff2a6f
AB
9246 {
9247 arm_gdbarch_tdep *tdep
08106042 9248 = gdbarch_tdep<arm_gdbarch_tdep> (target_gdbarch ());
6dff2a6f
AB
9249
9250 gdb_printf (file, _("\
28e97307 9251The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
6dff2a6f
AB
9252 fp_model_strings[tdep->fp_model]);
9253 }
28e97307 9254 else
6cb06a8c 9255 gdb_printf (file, _("\
28e97307 9256The current ARM floating point model is \"%s\".\n"),
6cb06a8c 9257 fp_model_strings[arm_fp_model]);
28e97307
DJ
9258}
9259
9260static void
eb4c3f4a 9261arm_set_abi (const char *args, int from_tty,
28e97307
DJ
9262 struct cmd_list_element *c)
9263{
570dc176 9264 int arm_abi;
28e97307
DJ
9265
9266 for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
9267 if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
9268 {
aead7601 9269 arm_abi_global = (enum arm_abi_kind) arm_abi;
28e97307
DJ
9270 break;
9271 }
9272
9273 if (arm_abi == ARM_ABI_LAST)
9274 internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
9275 arm_abi_string);
9276
9277 arm_update_current_architecture ();
9278}
9279
9280static void
9281arm_show_abi (struct ui_file *file, int from_tty,
9282 struct cmd_list_element *c, const char *value)
9283{
28e97307 9284 if (arm_abi_global == ARM_ABI_AUTO
f5656ead 9285 && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
6dff2a6f
AB
9286 {
9287 arm_gdbarch_tdep *tdep
08106042 9288 = gdbarch_tdep<arm_gdbarch_tdep> (target_gdbarch ());
6dff2a6f
AB
9289
9290 gdb_printf (file, _("\
28e97307 9291The current ARM ABI is \"auto\" (currently \"%s\").\n"),
6dff2a6f
AB
9292 arm_abi_strings[tdep->arm_abi]);
9293 }
28e97307 9294 else
6cb06a8c
TT
9295 gdb_printf (file, _("The current ARM ABI is \"%s\".\n"),
9296 arm_abi_string);
fd50bc42
RE
9297}
9298
0428b8f5
DJ
9299static void
9300arm_show_fallback_mode (struct ui_file *file, int from_tty,
9301 struct cmd_list_element *c, const char *value)
9302{
6cb06a8c
TT
9303 gdb_printf (file,
9304 _("The current execution mode assumed "
9305 "(when symbols are unavailable) is \"%s\".\n"),
9306 arm_fallback_mode_string);
0428b8f5
DJ
9307}
9308
9309static void
9310arm_show_force_mode (struct ui_file *file, int from_tty,
9311 struct cmd_list_element *c, const char *value)
9312{
6cb06a8c
TT
9313 gdb_printf (file,
9314 _("The current execution mode assumed "
9315 "(even when symbols are available) is \"%s\".\n"),
9316 arm_force_mode_string);
0428b8f5
DJ
9317}
9318
ef273377
CL
9319static void
9320arm_show_unwind_secure_frames (struct ui_file *file, int from_tty,
9321 struct cmd_list_element *c, const char *value)
9322{
9323 gdb_printf (file,
9324 _("Usage of non-secure to secure exception stack unwinding is %s.\n"),
9325 arm_unwind_secure_frames ? "on" : "off");
9326}
9327
afd7eef0
RE
9328/* If the user changes the register disassembly style used for info
9329 register and other commands, we have to also switch the style used
9330 in opcodes for disassembly output. This function is run in the "set
9331 arm disassembly" command, and does that. */
bc90b915
FN
9332
9333static void
eb4c3f4a 9334set_disassembly_style_sfunc (const char *args, int from_tty,
65b48a81 9335 struct cmd_list_element *c)
bc90b915 9336{
65b48a81
PB
9337 /* Convert the short style name into the long style name (eg, reg-names-*)
9338 before calling the generic set_disassembler_options() function. */
9339 std::string long_name = std::string ("reg-names-") + disassembly_style;
9340 set_disassembler_options (&long_name[0]);
9341}
9342
9343static void
9344show_disassembly_style_sfunc (struct ui_file *file, int from_tty,
9345 struct cmd_list_element *c, const char *value)
9346{
9347 struct gdbarch *gdbarch = get_current_arch ();
9348 char *options = get_disassembler_options (gdbarch);
9349 const char *style = "";
9350 int len = 0;
f995bbe8 9351 const char *opt;
65b48a81
PB
9352
9353 FOR_EACH_DISASSEMBLER_OPTION (opt, options)
08dedd66 9354 if (startswith (opt, "reg-names-"))
65b48a81
PB
9355 {
9356 style = &opt[strlen ("reg-names-")];
9357 len = strcspn (style, ",");
9358 }
9359
6cb06a8c 9360 gdb_printf (file, "The disassembly style is \"%.*s\".\n", len, style);
bc90b915
FN
9361}
9362\f
966fbf70 9363/* Return the ARM register name corresponding to register I. */
a208b0cb 9364static const char *
d93859e2 9365arm_register_name (struct gdbarch *gdbarch, int i)
966fbf70 9366{
08106042 9367 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d 9368
ecbf5d4f 9369 if (is_s_pseudo (gdbarch, i))
58d6951d 9370 {
ecbf5d4f 9371 static const char *const s_pseudo_names[] = {
58d6951d
DJ
9372 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
9373 "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
9374 "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
9375 "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
9376 };
9377
ecbf5d4f 9378 return s_pseudo_names[i - tdep->s_pseudo_base];
58d6951d
DJ
9379 }
9380
ecbf5d4f 9381 if (is_q_pseudo (gdbarch, i))
58d6951d 9382 {
ecbf5d4f 9383 static const char *const q_pseudo_names[] = {
58d6951d
DJ
9384 "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
9385 "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
9386 };
9387
ecbf5d4f 9388 return q_pseudo_names[i - tdep->q_pseudo_base];
58d6951d
DJ
9389 }
9390
ae66a8f1
SP
9391 if (is_mve_pseudo (gdbarch, i))
9392 return "p0";
9393
a01567f4
LM
9394 /* RA_AUTH_CODE is used for unwinding only. Do not assign it a name. */
9395 if (is_pacbti_pseudo (gdbarch, i))
9396 return "";
9397
ff6f572f
DJ
9398 if (i >= ARRAY_SIZE (arm_register_names))
9399 /* These registers are only supported on targets which supply
9400 an XML description. */
9401 return "";
9402
ecbf5d4f 9403 /* Non-pseudo registers. */
966fbf70
RE
9404 return arm_register_names[i];
9405}
9406
082fc60d
RE
9407/* Test whether the coff symbol specific value corresponds to a Thumb
9408 function. */
9409
9410static int
9411coff_sym_is_thumb (int val)
9412{
f8bf5763
PM
9413 return (val == C_THUMBEXT
9414 || val == C_THUMBSTAT
9415 || val == C_THUMBEXTFUNC
9416 || val == C_THUMBSTATFUNC
9417 || val == C_THUMBLABEL);
082fc60d
RE
9418}
9419
9420/* arm_coff_make_msymbol_special()
9421 arm_elf_make_msymbol_special()
9422
9423 These functions test whether the COFF or ELF symbol corresponds to
9424 an address in thumb code, and set a "special" bit in a minimal
9425 symbol to indicate that it does. */
9426
34e8f22d 9427static void
082fc60d
RE
9428arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
9429{
39d911fc
TP
9430 elf_symbol_type *elfsym = (elf_symbol_type *) sym;
9431
9432 if (ARM_GET_SYM_BRANCH_TYPE (elfsym->internal_elf_sym.st_target_internal)
467d42c4 9433 == ST_BRANCH_TO_THUMB)
082fc60d
RE
9434 MSYMBOL_SET_SPECIAL (msym);
9435}
9436
34e8f22d 9437static void
082fc60d
RE
9438arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
9439{
9440 if (coff_sym_is_thumb (val))
9441 MSYMBOL_SET_SPECIAL (msym);
9442}
9443
60c5725c
DJ
9444static void
9445arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
9446 asymbol *sym)
9447{
9448 const char *name = bfd_asymbol_name (sym);
bd5766ec 9449 struct arm_per_bfd *data;
60c5725c
DJ
9450 struct arm_mapping_symbol new_map_sym;
9451
9452 gdb_assert (name[0] == '$');
9453 if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
9454 return;
9455
bd5766ec 9456 data = arm_bfd_data_key.get (objfile->obfd);
60c5725c 9457 if (data == NULL)
bd5766ec
LM
9458 data = arm_bfd_data_key.emplace (objfile->obfd,
9459 objfile->obfd->section_count);
54cc7474 9460 arm_mapping_symbol_vec &map
e6f7f6d1 9461 = data->section_maps[bfd_asymbol_section (sym)->index];
60c5725c
DJ
9462
9463 new_map_sym.value = sym->value;
9464 new_map_sym.type = name[1];
9465
4838e44c
SM
9466 /* Insert at the end, the vector will be sorted on first use. */
9467 map.push_back (new_map_sym);
60c5725c
DJ
9468}
9469
756fe439 9470static void
61a1198a 9471arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
756fe439 9472{
ac7936df 9473 struct gdbarch *gdbarch = regcache->arch ();
61a1198a 9474 regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
756fe439
DJ
9475
9476 /* If necessary, set the T bit. */
9477 if (arm_apcs_32)
9478 {
9779414d 9479 ULONGEST val, t_bit;
61a1198a 9480 regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
9779414d
DJ
9481 t_bit = arm_psr_thumb_bit (gdbarch);
9482 if (arm_pc_is_thumb (gdbarch, pc))
9483 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9484 val | t_bit);
756fe439 9485 else
61a1198a 9486 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9779414d 9487 val & ~t_bit);
756fe439
DJ
9488 }
9489}
123dc839 9490
58d6951d
DJ
9491/* Read the contents of a NEON quad register, by reading from two
9492 double registers. This is used to implement the quad pseudo
9493 registers, and for argument passing in case the quad registers are
9494 missing; vectors are passed in quad registers when using the VFP
9495 ABI, even if a NEON unit is not present. REGNUM is the index of
9496 the quad register, in [0, 15]. */
9497
05d1431c 9498static enum register_status
849d0ba8 9499arm_neon_quad_read (struct gdbarch *gdbarch, readable_regcache *regcache,
58d6951d
DJ
9500 int regnum, gdb_byte *buf)
9501{
9502 char name_buf[4];
9503 gdb_byte reg_buf[8];
9504 int offset, double_regnum;
05d1431c 9505 enum register_status status;
58d6951d 9506
8c042590 9507 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
58d6951d
DJ
9508 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9509 strlen (name_buf));
9510
9511 /* d0 is always the least significant half of q0. */
9512 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9513 offset = 8;
9514 else
9515 offset = 0;
9516
03f50fc8 9517 status = regcache->raw_read (double_regnum, reg_buf);
05d1431c
PA
9518 if (status != REG_VALID)
9519 return status;
58d6951d
DJ
9520 memcpy (buf + offset, reg_buf, 8);
9521
9522 offset = 8 - offset;
03f50fc8 9523 status = regcache->raw_read (double_regnum + 1, reg_buf);
05d1431c
PA
9524 if (status != REG_VALID)
9525 return status;
58d6951d 9526 memcpy (buf + offset, reg_buf, 8);
05d1431c
PA
9527
9528 return REG_VALID;
58d6951d
DJ
9529}
9530
ae66a8f1
SP
9531/* Read the contents of the MVE pseudo register REGNUM and store it
9532 in BUF. */
9533
9534static enum register_status
9535arm_mve_pseudo_read (struct gdbarch *gdbarch, readable_regcache *regcache,
9536 int regnum, gdb_byte *buf)
9537{
08106042 9538 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ae66a8f1
SP
9539
9540 /* P0 is the first 16 bits of VPR. */
9541 return regcache->raw_read_part (tdep->mve_vpr_regnum, 0, 2, buf);
9542}
9543
05d1431c 9544static enum register_status
849d0ba8 9545arm_pseudo_read (struct gdbarch *gdbarch, readable_regcache *regcache,
58d6951d
DJ
9546 int regnum, gdb_byte *buf)
9547{
9548 const int num_regs = gdbarch_num_regs (gdbarch);
9549 char name_buf[4];
9550 gdb_byte reg_buf[8];
9551 int offset, double_regnum;
08106042 9552 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d
DJ
9553
9554 gdb_assert (regnum >= num_regs);
58d6951d 9555
ecbf5d4f
LM
9556 if (is_q_pseudo (gdbarch, regnum))
9557 {
9558 /* Quad-precision register. */
9559 return arm_neon_quad_read (gdbarch, regcache,
9560 regnum - tdep->q_pseudo_base, buf);
9561 }
ae66a8f1
SP
9562 else if (is_mve_pseudo (gdbarch, regnum))
9563 return arm_mve_pseudo_read (gdbarch, regcache, regnum, buf);
58d6951d
DJ
9564 else
9565 {
05d1431c
PA
9566 enum register_status status;
9567
ecbf5d4f 9568 regnum -= tdep->s_pseudo_base;
58d6951d
DJ
9569 /* Single-precision register. */
9570 gdb_assert (regnum < 32);
9571
9572 /* s0 is always the least significant half of d0. */
9573 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9574 offset = (regnum & 1) ? 0 : 4;
9575 else
9576 offset = (regnum & 1) ? 4 : 0;
9577
8c042590 9578 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
58d6951d
DJ
9579 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9580 strlen (name_buf));
9581
03f50fc8 9582 status = regcache->raw_read (double_regnum, reg_buf);
05d1431c
PA
9583 if (status == REG_VALID)
9584 memcpy (buf, reg_buf + offset, 4);
9585 return status;
58d6951d
DJ
9586 }
9587}
9588
9589/* Store the contents of BUF to a NEON quad register, by writing to
9590 two double registers. This is used to implement the quad pseudo
9591 registers, and for argument passing in case the quad registers are
9592 missing; vectors are passed in quad registers when using the VFP
9593 ABI, even if a NEON unit is not present. REGNUM is the index
9594 of the quad register, in [0, 15]. */
9595
9596static void
9597arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
9598 int regnum, const gdb_byte *buf)
9599{
9600 char name_buf[4];
58d6951d
DJ
9601 int offset, double_regnum;
9602
8c042590 9603 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
58d6951d
DJ
9604 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9605 strlen (name_buf));
9606
9607 /* d0 is always the least significant half of q0. */
9608 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9609 offset = 8;
9610 else
9611 offset = 0;
9612
10eaee5f 9613 regcache->raw_write (double_regnum, buf + offset);
58d6951d 9614 offset = 8 - offset;
10eaee5f 9615 regcache->raw_write (double_regnum + 1, buf + offset);
58d6951d
DJ
9616}
9617
ae66a8f1
SP
9618/* Store the contents of BUF to the MVE pseudo register REGNUM. */
9619
9620static void
9621arm_mve_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
9622 int regnum, const gdb_byte *buf)
9623{
08106042 9624 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ae66a8f1
SP
9625
9626 /* P0 is the first 16 bits of VPR. */
9627 regcache->raw_write_part (tdep->mve_vpr_regnum, 0, 2, buf);
9628}
9629
58d6951d
DJ
9630static void
9631arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
9632 int regnum, const gdb_byte *buf)
9633{
9634 const int num_regs = gdbarch_num_regs (gdbarch);
9635 char name_buf[4];
9636 gdb_byte reg_buf[8];
9637 int offset, double_regnum;
08106042 9638 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d
DJ
9639
9640 gdb_assert (regnum >= num_regs);
58d6951d 9641
ecbf5d4f
LM
9642 if (is_q_pseudo (gdbarch, regnum))
9643 {
9644 /* Quad-precision register. */
9645 arm_neon_quad_write (gdbarch, regcache,
9646 regnum - tdep->q_pseudo_base, buf);
9647 }
ae66a8f1
SP
9648 else if (is_mve_pseudo (gdbarch, regnum))
9649 arm_mve_pseudo_write (gdbarch, regcache, regnum, buf);
58d6951d
DJ
9650 else
9651 {
ecbf5d4f 9652 regnum -= tdep->s_pseudo_base;
58d6951d
DJ
9653 /* Single-precision register. */
9654 gdb_assert (regnum < 32);
9655
9656 /* s0 is always the least significant half of d0. */
9657 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9658 offset = (regnum & 1) ? 0 : 4;
9659 else
9660 offset = (regnum & 1) ? 4 : 0;
9661
8c042590 9662 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
58d6951d
DJ
9663 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9664 strlen (name_buf));
9665
0b883586 9666 regcache->raw_read (double_regnum, reg_buf);
58d6951d 9667 memcpy (reg_buf + offset, buf, 4);
10eaee5f 9668 regcache->raw_write (double_regnum, reg_buf);
58d6951d
DJ
9669 }
9670}
9671
123dc839
DJ
9672static struct value *
9673value_of_arm_user_reg (struct frame_info *frame, const void *baton)
9674{
9a3c8263 9675 const int *reg_p = (const int *) baton;
123dc839
DJ
9676 return value_of_register (*reg_p, frame);
9677}
97e03143 9678\f
70f80edf
JT
9679static enum gdb_osabi
9680arm_elf_osabi_sniffer (bfd *abfd)
97e03143 9681{
2af48f68 9682 unsigned int elfosabi;
70f80edf 9683 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
97e03143 9684
70f80edf 9685 elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
97e03143 9686
28e97307
DJ
9687 if (elfosabi == ELFOSABI_ARM)
9688 /* GNU tools use this value. Check note sections in this case,
9689 as well. */
b35c1d1c
TT
9690 {
9691 for (asection *sect : gdb_bfd_sections (abfd))
9692 generic_elf_osabi_sniff_abi_tag_sections (abfd, sect, &osabi);
9693 }
97e03143 9694
28e97307 9695 /* Anything else will be handled by the generic ELF sniffer. */
70f80edf 9696 return osabi;
97e03143
RE
9697}
9698
54483882
YQ
9699static int
9700arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
dbf5d61b 9701 const struct reggroup *group)
54483882 9702{
2c291032
YQ
9703 /* FPS register's type is INT, but belongs to float_reggroup. Beside
9704 this, FPS register belongs to save_regroup, restore_reggroup, and
9705 all_reggroup, of course. */
54483882 9706 if (regnum == ARM_FPS_REGNUM)
2c291032
YQ
9707 return (group == float_reggroup
9708 || group == save_reggroup
9709 || group == restore_reggroup
9710 || group == all_reggroup);
54483882
YQ
9711 else
9712 return default_register_reggroup_p (gdbarch, regnum, group);
9713}
9714
25f8c692
JL
9715/* For backward-compatibility we allow two 'g' packet lengths with
9716 the remote protocol depending on whether FPA registers are
9717 supplied. M-profile targets do not have FPA registers, but some
9718 stubs already exist in the wild which use a 'g' packet which
9719 supplies them albeit with dummy values. The packet format which
9720 includes FPA registers should be considered deprecated for
9721 M-profile targets. */
9722
9723static void
9724arm_register_g_packet_guesses (struct gdbarch *gdbarch)
9725{
08106042 9726 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
345bd07c
SM
9727
9728 if (tdep->is_m)
25f8c692 9729 {
d105cce5
AH
9730 const target_desc *tdesc;
9731
25f8c692
JL
9732 /* If we know from the executable this is an M-profile target,
9733 cater for remote targets whose register set layout is the
9734 same as the FPA layout. */
d105cce5 9735 tdesc = arm_read_mprofile_description (ARM_M_TYPE_WITH_FPA);
25f8c692 9736 register_remote_g_packet_guess (gdbarch,
350fab54 9737 ARM_CORE_REGS_SIZE + ARM_FP_REGS_SIZE,
d105cce5 9738 tdesc);
25f8c692
JL
9739
9740 /* The regular M-profile layout. */
d105cce5 9741 tdesc = arm_read_mprofile_description (ARM_M_TYPE_M_PROFILE);
350fab54 9742 register_remote_g_packet_guess (gdbarch, ARM_CORE_REGS_SIZE,
d105cce5 9743 tdesc);
3184d3f9
JL
9744
9745 /* M-profile plus M4F VFP. */
d105cce5 9746 tdesc = arm_read_mprofile_description (ARM_M_TYPE_VFP_D16);
3184d3f9 9747 register_remote_g_packet_guess (gdbarch,
350fab54 9748 ARM_CORE_REGS_SIZE + ARM_VFP2_REGS_SIZE,
d105cce5 9749 tdesc);
ae66a8f1
SP
9750 /* M-profile plus MVE. */
9751 tdesc = arm_read_mprofile_description (ARM_M_TYPE_MVE);
9752 register_remote_g_packet_guess (gdbarch, ARM_CORE_REGS_SIZE
9753 + ARM_VFP2_REGS_SIZE
9754 + ARM_INT_REGISTER_SIZE, tdesc);
9074667a
CL
9755
9756 /* M-profile system (stack pointers). */
9757 tdesc = arm_read_mprofile_description (ARM_M_TYPE_SYSTEM);
9758 register_remote_g_packet_guess (gdbarch, 2 * ARM_INT_REGISTER_SIZE, tdesc);
25f8c692
JL
9759 }
9760
9761 /* Otherwise we don't have a useful guess. */
9762}
9763
7eb89530
YQ
9764/* Implement the code_of_frame_writable gdbarch method. */
9765
9766static int
9767arm_code_of_frame_writable (struct gdbarch *gdbarch, struct frame_info *frame)
9768{
08106042 9769 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
345bd07c
SM
9770
9771 if (tdep->is_m && get_frame_type (frame) == SIGTRAMP_FRAME)
7eb89530
YQ
9772 {
9773 /* M-profile exception frames return to some magic PCs, where
9774 isn't writable at all. */
9775 return 0;
9776 }
9777 else
9778 return 1;
9779}
9780
3426ae57
AH
9781/* Implement gdbarch_gnu_triplet_regexp. If the arch name is arm then allow it
9782 to be postfixed by a version (eg armv7hl). */
9783
9784static const char *
9785arm_gnu_triplet_regexp (struct gdbarch *gdbarch)
9786{
9787 if (strcmp (gdbarch_bfd_arch_info (gdbarch)->arch_name, "arm") == 0)
9788 return "arm(v[^- ]*)?";
9789 return gdbarch_bfd_arch_info (gdbarch)->arch_name;
9790}
9791
a01567f4
LM
9792/* Implement the "get_pc_address_flags" gdbarch method. */
9793
9794static std::string
9795arm_get_pc_address_flags (frame_info *frame, CORE_ADDR pc)
9796{
9797 if (get_frame_pc_masked (frame))
9798 return "PAC";
9799
9800 return "";
9801}
9802
da3c6d4a
MS
9803/* Initialize the current architecture based on INFO. If possible,
9804 re-use an architecture from ARCHES, which is a list of
9805 architectures already created during this debugging session.
97e03143 9806
da3c6d4a
MS
9807 Called e.g. at program startup, when reading a core file, and when
9808 reading a binary file. */
97e03143 9809
39bbf761
RE
9810static struct gdbarch *
9811arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
9812{
9813 struct gdbarch *gdbarch;
28e97307
DJ
9814 struct gdbarch_list *best_arch;
9815 enum arm_abi_kind arm_abi = arm_abi_global;
9816 enum arm_float_model fp_model = arm_fp_model;
c1e1314d 9817 tdesc_arch_data_up tdesc_data;
7559c217
CB
9818 int i;
9819 bool is_m = false;
ae7e2f45 9820 bool have_sec_ext = false;
7559c217 9821 int vfp_register_count = 0;
ecbf5d4f 9822 bool have_s_pseudos = false, have_q_pseudos = false;
7559c217
CB
9823 bool have_wmmx_registers = false;
9824 bool have_neon = false;
9825 bool have_fpa_registers = true;
9779414d 9826 const struct target_desc *tdesc = info.target_desc;
ae66a8f1
SP
9827 bool have_vfp = false;
9828 bool have_mve = false;
a01567f4 9829 bool have_pacbti = false;
ae66a8f1 9830 int mve_vpr_regnum = -1;
ecbf5d4f 9831 int register_count = ARM_NUM_REGS;
9074667a
CL
9832 bool have_m_profile_msp = false;
9833 int m_profile_msp_regnum = -1;
9834 int m_profile_psp_regnum = -1;
ae7e2f45
CL
9835 int m_profile_msp_ns_regnum = -1;
9836 int m_profile_psp_ns_regnum = -1;
9837 int m_profile_msp_s_regnum = -1;
9838 int m_profile_psp_s_regnum = -1;
92d48a1e 9839 int tls_regnum = 0;
9779414d
DJ
9840
9841 /* If we have an object to base this architecture on, try to determine
9842 its ABI. */
9843
9844 if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
9845 {
9846 int ei_osabi, e_flags;
9847
9848 switch (bfd_get_flavour (info.abfd))
9849 {
9779414d
DJ
9850 case bfd_target_coff_flavour:
9851 /* Assume it's an old APCS-style ABI. */
9852 /* XXX WinCE? */
9853 arm_abi = ARM_ABI_APCS;
9854 break;
9855
9856 case bfd_target_elf_flavour:
9857 ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
9858 e_flags = elf_elfheader (info.abfd)->e_flags;
9859
9860 if (ei_osabi == ELFOSABI_ARM)
9861 {
9862 /* GNU tools used to use this value, but do not for EABI
9863 objects. There's nowhere to tag an EABI version
9864 anyway, so assume APCS. */
9865 arm_abi = ARM_ABI_APCS;
9866 }
d403db27 9867 else if (ei_osabi == ELFOSABI_NONE || ei_osabi == ELFOSABI_GNU)
9779414d
DJ
9868 {
9869 int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
9779414d
DJ
9870
9871 switch (eabi_ver)
9872 {
9873 case EF_ARM_EABI_UNKNOWN:
9874 /* Assume GNU tools. */
9875 arm_abi = ARM_ABI_APCS;
9876 break;
9877
9878 case EF_ARM_EABI_VER4:
9879 case EF_ARM_EABI_VER5:
9880 arm_abi = ARM_ABI_AAPCS;
9881 /* EABI binaries default to VFP float ordering.
9882 They may also contain build attributes that can
9883 be used to identify if the VFP argument-passing
9884 ABI is in use. */
9885 if (fp_model == ARM_FLOAT_AUTO)
9886 {
9887#ifdef HAVE_ELF
9888 switch (bfd_elf_get_obj_attr_int (info.abfd,
9889 OBJ_ATTR_PROC,
9890 Tag_ABI_VFP_args))
9891 {
b35b0298 9892 case AEABI_VFP_args_base:
9779414d
DJ
9893 /* "The user intended FP parameter/result
9894 passing to conform to AAPCS, base
9895 variant". */
9896 fp_model = ARM_FLOAT_SOFT_VFP;
9897 break;
b35b0298 9898 case AEABI_VFP_args_vfp:
9779414d
DJ
9899 /* "The user intended FP parameter/result
9900 passing to conform to AAPCS, VFP
9901 variant". */
9902 fp_model = ARM_FLOAT_VFP;
9903 break;
b35b0298 9904 case AEABI_VFP_args_toolchain:
9779414d
DJ
9905 /* "The user intended FP parameter/result
9906 passing to conform to tool chain-specific
9907 conventions" - we don't know any such
9908 conventions, so leave it as "auto". */
9909 break;
b35b0298 9910 case AEABI_VFP_args_compatible:
5c294fee
TG
9911 /* "Code is compatible with both the base
9912 and VFP variants; the user did not permit
9913 non-variadic functions to pass FP
9914 parameters/results" - leave it as
9915 "auto". */
9916 break;
9779414d
DJ
9917 default:
9918 /* Attribute value not mentioned in the
5c294fee 9919 November 2012 ABI, so leave it as
9779414d
DJ
9920 "auto". */
9921 break;
9922 }
9923#else
9924 fp_model = ARM_FLOAT_SOFT_VFP;
9925#endif
9926 }
9927 break;
9928
9929 default:
9930 /* Leave it as "auto". */
9931 warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
9932 break;
9933 }
9934
9935#ifdef HAVE_ELF
9936 /* Detect M-profile programs. This only works if the
9937 executable file includes build attributes; GCC does
9938 copy them to the executable, but e.g. RealView does
9939 not. */
17cbafdb
SM
9940 int attr_arch
9941 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9942 Tag_CPU_arch);
9943 int attr_profile
9944 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9945 Tag_CPU_arch_profile);
9946
9779414d
DJ
9947 /* GCC specifies the profile for v6-M; RealView only
9948 specifies the profile for architectures starting with
9949 V7 (as opposed to architectures with a tag
9950 numerically greater than TAG_CPU_ARCH_V7). */
9951 if (!tdesc_has_registers (tdesc)
9952 && (attr_arch == TAG_CPU_ARCH_V6_M
9953 || attr_arch == TAG_CPU_ARCH_V6S_M
131a355f
LM
9954 || attr_arch == TAG_CPU_ARCH_V7E_M
9955 || attr_arch == TAG_CPU_ARCH_V8M_BASE
9956 || attr_arch == TAG_CPU_ARCH_V8M_MAIN
ae66a8f1 9957 || attr_arch == TAG_CPU_ARCH_V8_1M_MAIN
9779414d 9958 || attr_profile == 'M'))
7559c217 9959 is_m = true;
a01567f4
LM
9960
9961 /* Look for attributes that indicate support for ARMv8.1-m
9962 PACBTI. */
9963 if (!tdesc_has_registers (tdesc) && is_m)
9964 {
9965 int attr_pac_extension
9966 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9967 Tag_PAC_extension);
9968
9969 int attr_bti_extension
9970 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9971 Tag_BTI_extension);
9972
9973 int attr_pacret_use
9974 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9975 Tag_PACRET_use);
9976
9977 int attr_bti_use
9978 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9979 Tag_BTI_use);
9980
9981 if (attr_pac_extension != 0 || attr_bti_extension != 0
9982 || attr_pacret_use != 0 || attr_bti_use != 0)
9983 have_pacbti = true;
9984 }
9779414d
DJ
9985#endif
9986 }
9987
9988 if (fp_model == ARM_FLOAT_AUTO)
9989 {
9779414d
DJ
9990 switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
9991 {
9992 case 0:
9993 /* Leave it as "auto". Strictly speaking this case
9994 means FPA, but almost nobody uses that now, and
9995 many toolchains fail to set the appropriate bits
9996 for the floating-point model they use. */
9997 break;
9998 case EF_ARM_SOFT_FLOAT:
9999 fp_model = ARM_FLOAT_SOFT_FPA;
10000 break;
10001 case EF_ARM_VFP_FLOAT:
10002 fp_model = ARM_FLOAT_VFP;
10003 break;
10004 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
10005 fp_model = ARM_FLOAT_SOFT_VFP;
10006 break;
10007 }
10008 }
10009
10010 if (e_flags & EF_ARM_BE8)
10011 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
10012
10013 break;
10014
10015 default:
10016 /* Leave it as "auto". */
10017 break;
10018 }
10019 }
123dc839
DJ
10020
10021 /* Check any target description for validity. */
9779414d 10022 if (tdesc_has_registers (tdesc))
123dc839
DJ
10023 {
10024 /* For most registers we require GDB's default names; but also allow
10025 the numeric names for sp / lr / pc, as a convenience. */
10026 static const char *const arm_sp_names[] = { "r13", "sp", NULL };
10027 static const char *const arm_lr_names[] = { "r14", "lr", NULL };
10028 static const char *const arm_pc_names[] = { "r15", "pc", NULL };
10029
10030 const struct tdesc_feature *feature;
58d6951d 10031 int valid_p;
123dc839 10032
9779414d 10033 feature = tdesc_find_feature (tdesc,
123dc839
DJ
10034 "org.gnu.gdb.arm.core");
10035 if (feature == NULL)
9779414d
DJ
10036 {
10037 feature = tdesc_find_feature (tdesc,
10038 "org.gnu.gdb.arm.m-profile");
10039 if (feature == NULL)
10040 return NULL;
10041 else
7559c217 10042 is_m = true;
9779414d 10043 }
123dc839
DJ
10044
10045 tdesc_data = tdesc_data_alloc ();
10046
10047 valid_p = 1;
10048 for (i = 0; i < ARM_SP_REGNUM; i++)
c1e1314d 10049 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
123dc839 10050 arm_register_names[i]);
c1e1314d 10051 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
123dc839
DJ
10052 ARM_SP_REGNUM,
10053 arm_sp_names);
c1e1314d 10054 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
123dc839
DJ
10055 ARM_LR_REGNUM,
10056 arm_lr_names);
c1e1314d 10057 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
123dc839
DJ
10058 ARM_PC_REGNUM,
10059 arm_pc_names);
9779414d 10060 if (is_m)
c1e1314d 10061 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
9779414d
DJ
10062 ARM_PS_REGNUM, "xpsr");
10063 else
c1e1314d 10064 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
9779414d 10065 ARM_PS_REGNUM, "cpsr");
123dc839
DJ
10066
10067 if (!valid_p)
c1e1314d 10068 return NULL;
123dc839 10069
9074667a
CL
10070 if (is_m)
10071 {
10072 feature = tdesc_find_feature (tdesc,
10073 "org.gnu.gdb.arm.m-system");
10074 if (feature != nullptr)
10075 {
10076 /* MSP */
10077 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10078 register_count, "msp");
10079 if (!valid_p)
10080 {
10081 warning (_("M-profile m-system feature is missing required register msp."));
10082 return nullptr;
10083 }
10084 have_m_profile_msp = true;
10085 m_profile_msp_regnum = register_count++;
10086
10087 /* PSP */
10088 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10089 register_count, "psp");
10090 if (!valid_p)
10091 {
10092 warning (_("M-profile m-system feature is missing required register psp."));
10093 return nullptr;
10094 }
10095 m_profile_psp_regnum = register_count++;
10096 }
10097 }
10098
9779414d 10099 feature = tdesc_find_feature (tdesc,
123dc839
DJ
10100 "org.gnu.gdb.arm.fpa");
10101 if (feature != NULL)
10102 {
10103 valid_p = 1;
10104 for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
c1e1314d 10105 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
123dc839
DJ
10106 arm_register_names[i]);
10107 if (!valid_p)
c1e1314d 10108 return NULL;
123dc839 10109 }
ff6f572f 10110 else
7559c217 10111 have_fpa_registers = false;
ff6f572f 10112
9779414d 10113 feature = tdesc_find_feature (tdesc,
ff6f572f
DJ
10114 "org.gnu.gdb.xscale.iwmmxt");
10115 if (feature != NULL)
10116 {
10117 static const char *const iwmmxt_names[] = {
10118 "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
10119 "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
10120 "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
10121 "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
10122 };
10123
10124 valid_p = 1;
10125 for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
10126 valid_p
c1e1314d 10127 &= tdesc_numbered_register (feature, tdesc_data.get (), i,
ff6f572f
DJ
10128 iwmmxt_names[i - ARM_WR0_REGNUM]);
10129
10130 /* Check for the control registers, but do not fail if they
10131 are missing. */
10132 for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
c1e1314d 10133 tdesc_numbered_register (feature, tdesc_data.get (), i,
ff6f572f
DJ
10134 iwmmxt_names[i - ARM_WR0_REGNUM]);
10135
10136 for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
10137 valid_p
c1e1314d 10138 &= tdesc_numbered_register (feature, tdesc_data.get (), i,
ff6f572f
DJ
10139 iwmmxt_names[i - ARM_WR0_REGNUM]);
10140
10141 if (!valid_p)
c1e1314d 10142 return NULL;
a56cc1ce 10143
7559c217 10144 have_wmmx_registers = true;
ff6f572f 10145 }
58d6951d
DJ
10146
10147 /* If we have a VFP unit, check whether the single precision registers
10148 are present. If not, then we will synthesize them as pseudo
10149 registers. */
9779414d 10150 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
10151 "org.gnu.gdb.arm.vfp");
10152 if (feature != NULL)
10153 {
10154 static const char *const vfp_double_names[] = {
10155 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
10156 "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
10157 "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
10158 "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
10159 };
10160
10161 /* Require the double precision registers. There must be either
10162 16 or 32. */
10163 valid_p = 1;
10164 for (i = 0; i < 32; i++)
10165 {
c1e1314d 10166 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
58d6951d
DJ
10167 ARM_D0_REGNUM + i,
10168 vfp_double_names[i]);
10169 if (!valid_p)
10170 break;
10171 }
2b9e5ea6
UW
10172 if (!valid_p && i == 16)
10173 valid_p = 1;
58d6951d 10174
2b9e5ea6 10175 /* Also require FPSCR. */
c1e1314d 10176 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
2b9e5ea6
UW
10177 ARM_FPSCR_REGNUM, "fpscr");
10178 if (!valid_p)
c1e1314d 10179 return NULL;
58d6951d 10180
ae66a8f1
SP
10181 have_vfp = true;
10182
58d6951d 10183 if (tdesc_unnumbered_register (feature, "s0") == 0)
ecbf5d4f 10184 have_s_pseudos = true;
58d6951d 10185
330c6ca9 10186 vfp_register_count = i;
58d6951d
DJ
10187
10188 /* If we have VFP, also check for NEON. The architecture allows
10189 NEON without VFP (integer vector operations only), but GDB
10190 does not support that. */
9779414d 10191 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
10192 "org.gnu.gdb.arm.neon");
10193 if (feature != NULL)
10194 {
10195 /* NEON requires 32 double-precision registers. */
10196 if (i != 32)
c1e1314d 10197 return NULL;
58d6951d
DJ
10198
10199 /* If there are quad registers defined by the stub, use
10200 their type; otherwise (normally) provide them with
10201 the default type. */
10202 if (tdesc_unnumbered_register (feature, "q0") == 0)
ecbf5d4f 10203 have_q_pseudos = true;
ae66a8f1
SP
10204 }
10205 }
10206
92d48a1e
JB
10207 /* Check for the TLS register feature. */
10208 feature = tdesc_find_feature (tdesc, "org.gnu.gdb.arm.tls");
10209 if (feature != nullptr)
10210 {
10211 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10212 register_count, "tpidruro");
10213 if (!valid_p)
10214 return nullptr;
10215
10216 tls_regnum = register_count;
10217 register_count++;
10218 }
10219
ae66a8f1
SP
10220 /* Check for MVE after all the checks for GPR's, VFP and Neon.
10221 MVE (Helium) is an M-profile extension. */
10222 if (is_m)
10223 {
10224 /* Do we have the MVE feature? */
10225 feature = tdesc_find_feature (tdesc,"org.gnu.gdb.arm.m-profile-mve");
10226
10227 if (feature != nullptr)
10228 {
10229 /* If we have MVE, we must always have the VPR register. */
10230 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10231 register_count, "vpr");
10232 if (!valid_p)
10233 {
10234 warning (_("MVE feature is missing required register vpr."));
10235 return nullptr;
10236 }
58d6951d 10237
ae66a8f1
SP
10238 have_mve = true;
10239 mve_vpr_regnum = register_count;
10240 register_count++;
10241
10242 /* We can't have Q pseudo registers available here, as that
10243 would mean we have NEON features, and that is only available
10244 on A and R profiles. */
10245 gdb_assert (!have_q_pseudos);
10246
10247 /* Given we have a M-profile target description, if MVE is
10248 enabled and there are VFP registers, we should have Q
10249 pseudo registers (Q0 ~ Q7). */
10250 if (have_vfp)
10251 have_q_pseudos = true;
58d6951d 10252 }
a01567f4
LM
10253
10254 /* Do we have the ARMv8.1-m PACBTI feature? */
10255 feature = tdesc_find_feature (tdesc,
10256 "org.gnu.gdb.arm.m-profile-pacbti");
10257 if (feature != nullptr)
10258 {
10259 /* By advertising this feature, the target acknowledges the
10260 presence of the ARMv8.1-m PACBTI extensions.
10261
10262 We don't care for any particular registers in this group, so
10263 the target is free to include whatever it deems appropriate.
10264
10265 The expectation is for this feature to include the PAC
10266 keys. */
10267 have_pacbti = true;
10268 }
ae7e2f45
CL
10269
10270 /* Do we have the Security extension? */
10271 feature = tdesc_find_feature (tdesc,
10272 "org.gnu.gdb.arm.secext");
10273 if (feature != nullptr)
10274 {
10275 /* Secure/Non-secure stack pointers. */
10276 /* MSP_NS */
10277 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10278 register_count, "msp_ns");
10279 if (!valid_p)
10280 {
10281 warning (_("M-profile secext feature is missing required register msp_ns."));
10282 return nullptr;
10283 }
10284 m_profile_msp_ns_regnum = register_count++;
10285
10286 /* PSP_NS */
10287 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10288 register_count, "psp_ns");
10289 if (!valid_p)
10290 {
10291 warning (_("M-profile secext feature is missing required register psp_ns."));
10292 return nullptr;
10293 }
10294 m_profile_psp_ns_regnum = register_count++;
10295
10296 /* MSP_S */
10297 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10298 register_count, "msp_s");
10299 if (!valid_p)
10300 {
10301 warning (_("M-profile secext feature is missing required register msp_s."));
10302 return nullptr;
10303 }
10304 m_profile_msp_s_regnum = register_count++;
10305
10306 /* PSP_S */
10307 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10308 register_count, "psp_s");
10309 if (!valid_p)
10310 {
10311 warning (_("M-profile secext feature is missing required register psp_s."));
10312 return nullptr;
10313 }
10314 m_profile_psp_s_regnum = register_count++;
10315
10316 have_sec_ext = true;
10317 }
10318
58d6951d 10319 }
123dc839 10320 }
39bbf761 10321
28e97307
DJ
10322 /* If there is already a candidate, use it. */
10323 for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
10324 best_arch != NULL;
10325 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
10326 {
345bd07c 10327 arm_gdbarch_tdep *tdep
08106042 10328 = gdbarch_tdep<arm_gdbarch_tdep> (best_arch->gdbarch);
345bd07c
SM
10329
10330 if (arm_abi != ARM_ABI_AUTO && arm_abi != tdep->arm_abi)
28e97307
DJ
10331 continue;
10332
345bd07c 10333 if (fp_model != ARM_FLOAT_AUTO && fp_model != tdep->fp_model)
28e97307
DJ
10334 continue;
10335
58d6951d
DJ
10336 /* There are various other properties in tdep that we do not
10337 need to check here: those derived from a target description,
10338 since gdbarches with a different target description are
10339 automatically disqualified. */
10340
9779414d 10341 /* Do check is_m, though, since it might come from the binary. */
345bd07c 10342 if (is_m != tdep->is_m)
9779414d
DJ
10343 continue;
10344
a01567f4
LM
10345 /* Also check for ARMv8.1-m PACBTI support, since it might come from
10346 the binary. */
10347 if (have_pacbti != tdep->have_pacbti)
10348 continue;
10349
28e97307
DJ
10350 /* Found a match. */
10351 break;
10352 }
97e03143 10353
28e97307 10354 if (best_arch != NULL)
c1e1314d 10355 return best_arch->gdbarch;
28e97307 10356
345bd07c 10357 arm_gdbarch_tdep *tdep = new arm_gdbarch_tdep;
97e03143
RE
10358 gdbarch = gdbarch_alloc (&info, tdep);
10359
28e97307
DJ
10360 /* Record additional information about the architecture we are defining.
10361 These are gdbarch discriminators, like the OSABI. */
10362 tdep->arm_abi = arm_abi;
10363 tdep->fp_model = fp_model;
9779414d 10364 tdep->is_m = is_m;
ae7e2f45 10365 tdep->have_sec_ext = have_sec_ext;
ff6f572f 10366 tdep->have_fpa_registers = have_fpa_registers;
a56cc1ce 10367 tdep->have_wmmx_registers = have_wmmx_registers;
330c6ca9
YQ
10368 gdb_assert (vfp_register_count == 0
10369 || vfp_register_count == 16
10370 || vfp_register_count == 32);
10371 tdep->vfp_register_count = vfp_register_count;
ecbf5d4f
LM
10372 tdep->have_s_pseudos = have_s_pseudos;
10373 tdep->have_q_pseudos = have_q_pseudos;
58d6951d 10374 tdep->have_neon = have_neon;
92d48a1e 10375 tdep->tls_regnum = tls_regnum;
08216dd7 10376
ae66a8f1
SP
10377 /* Adjust the MVE feature settings. */
10378 if (have_mve)
10379 {
10380 tdep->have_mve = true;
10381 tdep->mve_vpr_regnum = mve_vpr_regnum;
10382 }
10383
a01567f4
LM
10384 /* Adjust the PACBTI feature settings. */
10385 tdep->have_pacbti = have_pacbti;
10386
9074667a
CL
10387 /* Adjust the M-profile stack pointers settings. */
10388 if (have_m_profile_msp)
10389 {
10390 tdep->m_profile_msp_regnum = m_profile_msp_regnum;
10391 tdep->m_profile_psp_regnum = m_profile_psp_regnum;
ae7e2f45
CL
10392 tdep->m_profile_msp_ns_regnum = m_profile_msp_ns_regnum;
10393 tdep->m_profile_psp_ns_regnum = m_profile_psp_ns_regnum;
10394 tdep->m_profile_msp_s_regnum = m_profile_msp_s_regnum;
10395 tdep->m_profile_psp_s_regnum = m_profile_psp_s_regnum;
9074667a
CL
10396 }
10397
25f8c692
JL
10398 arm_register_g_packet_guesses (gdbarch);
10399
08216dd7 10400 /* Breakpoints. */
9d4fde75 10401 switch (info.byte_order_for_code)
67255d04
RE
10402 {
10403 case BFD_ENDIAN_BIG:
66e810cd
RE
10404 tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
10405 tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
10406 tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
10407 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
10408
67255d04
RE
10409 break;
10410
10411 case BFD_ENDIAN_LITTLE:
66e810cd
RE
10412 tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
10413 tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
10414 tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
10415 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
10416
67255d04
RE
10417 break;
10418
10419 default:
10420 internal_error (__FILE__, __LINE__,
edefbb7c 10421 _("arm_gdbarch_init: bad byte order for float format"));
67255d04
RE
10422 }
10423
d7b486e7
RE
10424 /* On ARM targets char defaults to unsigned. */
10425 set_gdbarch_char_signed (gdbarch, 0);
10426
53375380
PA
10427 /* wchar_t is unsigned under the AAPCS. */
10428 if (tdep->arm_abi == ARM_ABI_AAPCS)
10429 set_gdbarch_wchar_signed (gdbarch, 0);
10430 else
10431 set_gdbarch_wchar_signed (gdbarch, 1);
53375380 10432
030197b4
AB
10433 /* Compute type alignment. */
10434 set_gdbarch_type_align (gdbarch, arm_type_align);
10435
cca44b1b
JB
10436 /* Note: for displaced stepping, this includes the breakpoint, and one word
10437 of additional scratch space. This setting isn't used for anything beside
10438 displaced stepping at present. */
e935475c 10439 set_gdbarch_max_insn_length (gdbarch, 4 * ARM_DISPLACED_MODIFIED_INSNS);
cca44b1b 10440
9df628e0 10441 /* This should be low enough for everything. */
97e03143 10442 tdep->lowest_pc = 0x20;
94c30b78 10443 tdep->jb_pc = -1; /* Longjump support not enabled by default. */
97e03143 10444
7c00367c
MK
10445 /* The default, for both APCS and AAPCS, is to return small
10446 structures in registers. */
10447 tdep->struct_return = reg_struct_return;
10448
2dd604e7 10449 set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
f53f0d0b 10450 set_gdbarch_frame_align (gdbarch, arm_frame_align);
39bbf761 10451
7eb89530
YQ
10452 if (is_m)
10453 set_gdbarch_code_of_frame_writable (gdbarch, arm_code_of_frame_writable);
10454
756fe439
DJ
10455 set_gdbarch_write_pc (gdbarch, arm_write_pc);
10456
eb5492fa 10457 frame_base_set_default (gdbarch, &arm_normal_base);
148754e5 10458
34e8f22d 10459 /* Address manipulation. */
34e8f22d
RE
10460 set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
10461
34e8f22d
RE
10462 /* Advance PC across function entry code. */
10463 set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
10464
c9cf6e20
MG
10465 /* Detect whether PC is at a point where the stack has been destroyed. */
10466 set_gdbarch_stack_frame_destroyed_p (gdbarch, arm_stack_frame_destroyed_p);
4024ca99 10467
190dce09
UW
10468 /* Skip trampolines. */
10469 set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
10470
34e8f22d
RE
10471 /* The stack grows downward. */
10472 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
10473
10474 /* Breakpoint manipulation. */
04180708
YQ
10475 set_gdbarch_breakpoint_kind_from_pc (gdbarch, arm_breakpoint_kind_from_pc);
10476 set_gdbarch_sw_breakpoint_from_kind (gdbarch, arm_sw_breakpoint_from_kind);
833b7ab5
YQ
10477 set_gdbarch_breakpoint_kind_from_current_state (gdbarch,
10478 arm_breakpoint_kind_from_current_state);
34e8f22d
RE
10479
10480 /* Information about registers, etc. */
34e8f22d
RE
10481 set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
10482 set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
ecbf5d4f 10483 set_gdbarch_num_regs (gdbarch, register_count);
7a5ea0d4 10484 set_gdbarch_register_type (gdbarch, arm_register_type);
54483882 10485 set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);
34e8f22d 10486
ff6f572f
DJ
10487 /* This "info float" is FPA-specific. Use the generic version if we
10488 do not have FPA. */
345bd07c 10489 if (tdep->have_fpa_registers)
ff6f572f
DJ
10490 set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
10491
26216b98 10492 /* Internal <-> external register number maps. */
ff6f572f 10493 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
26216b98
AC
10494 set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
10495
34e8f22d
RE
10496 set_gdbarch_register_name (gdbarch, arm_register_name);
10497
10498 /* Returning results. */
2af48f68 10499 set_gdbarch_return_value (gdbarch, arm_return_value);
34e8f22d 10500
03d48a7d
RE
10501 /* Disassembly. */
10502 set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
10503
34e8f22d
RE
10504 /* Minsymbol frobbing. */
10505 set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
10506 set_gdbarch_coff_make_msymbol_special (gdbarch,
10507 arm_coff_make_msymbol_special);
60c5725c 10508 set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
34e8f22d 10509
f9d67f43
DJ
10510 /* Thumb-2 IT block support. */
10511 set_gdbarch_adjust_breakpoint_address (gdbarch,
10512 arm_adjust_breakpoint_address);
10513
0d5de010
DJ
10514 /* Virtual tables. */
10515 set_gdbarch_vbit_in_delta (gdbarch, 1);
10516
97e03143 10517 /* Hook in the ABI-specific overrides, if they have been registered. */
4be87837 10518 gdbarch_init_osabi (info, gdbarch);
97e03143 10519
b39cc962
DJ
10520 dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
10521
eb5492fa 10522 /* Add some default predicates. */
2ae28aa9
YQ
10523 if (is_m)
10524 frame_unwind_append_unwinder (gdbarch, &arm_m_exception_unwind);
a262aec2
DJ
10525 frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
10526 dwarf2_append_unwinders (gdbarch);
0e9e9abd 10527 frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
779aa56f 10528 frame_unwind_append_unwinder (gdbarch, &arm_epilogue_frame_unwind);
a262aec2 10529 frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
eb5492fa 10530
97e03143
RE
10531 /* Now we have tuned the configuration, set a few final things,
10532 based on what the OS ABI has told us. */
10533
b8926edc
DJ
10534 /* If the ABI is not otherwise marked, assume the old GNU APCS. EABI
10535 binaries are always marked. */
10536 if (tdep->arm_abi == ARM_ABI_AUTO)
10537 tdep->arm_abi = ARM_ABI_APCS;
10538
e3039479
UW
10539 /* Watchpoints are not steppable. */
10540 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
10541
b8926edc
DJ
10542 /* We used to default to FPA for generic ARM, but almost nobody
10543 uses that now, and we now provide a way for the user to force
10544 the model. So default to the most useful variant. */
10545 if (tdep->fp_model == ARM_FLOAT_AUTO)
10546 tdep->fp_model = ARM_FLOAT_SOFT_FPA;
10547
9df628e0
RE
10548 if (tdep->jb_pc >= 0)
10549 set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
10550
08216dd7 10551 /* Floating point sizes and format. */
8da61cc4 10552 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
b8926edc 10553 if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
08216dd7 10554 {
8da61cc4
DJ
10555 set_gdbarch_double_format
10556 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10557 set_gdbarch_long_double_format
10558 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10559 }
10560 else
10561 {
10562 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
10563 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
08216dd7
RE
10564 }
10565
a01567f4
LM
10566 /* Hook used to decorate frames with signed return addresses, only available
10567 for ARMv8.1-m PACBTI. */
10568 if (is_m && have_pacbti)
10569 set_gdbarch_get_pc_address_flags (gdbarch, arm_get_pc_address_flags);
10570
dc22c61a
LM
10571 if (tdesc_data != nullptr)
10572 {
10573 set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
10574
10575 tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
ecbf5d4f 10576 register_count = gdbarch_num_regs (gdbarch);
dc22c61a
LM
10577
10578 /* Override tdesc_register_type to adjust the types of VFP
10579 registers for NEON. */
10580 set_gdbarch_register_type (gdbarch, arm_register_type);
10581 }
10582
ecbf5d4f 10583 /* Initialize the pseudo register data. */
ae66a8f1 10584 int num_pseudos = 0;
ecbf5d4f 10585 if (tdep->have_s_pseudos)
58d6951d 10586 {
ecbf5d4f
LM
10587 /* VFP single precision pseudo registers (S0~S31). */
10588 tdep->s_pseudo_base = register_count;
10589 tdep->s_pseudo_count = 32;
ae66a8f1 10590 num_pseudos += tdep->s_pseudo_count;
ecbf5d4f
LM
10591
10592 if (tdep->have_q_pseudos)
10593 {
10594 /* NEON quad precision pseudo registers (Q0~Q15). */
10595 tdep->q_pseudo_base = register_count + num_pseudos;
ae66a8f1
SP
10596
10597 if (have_neon)
10598 tdep->q_pseudo_count = 16;
10599 else if (have_mve)
10600 tdep->q_pseudo_count = ARM_MVE_NUM_Q_REGS;
10601
ecbf5d4f
LM
10602 num_pseudos += tdep->q_pseudo_count;
10603 }
ae66a8f1
SP
10604 }
10605
10606 /* Do we have any MVE pseudo registers? */
10607 if (have_mve)
10608 {
10609 tdep->mve_pseudo_base = register_count + num_pseudos;
10610 tdep->mve_pseudo_count = 1;
10611 num_pseudos += tdep->mve_pseudo_count;
10612 }
58d6951d 10613
a01567f4
LM
10614 /* Do we have any ARMv8.1-m PACBTI pseudo registers. */
10615 if (have_pacbti)
10616 {
10617 tdep->pacbti_pseudo_base = register_count + num_pseudos;
10618 tdep->pacbti_pseudo_count = 1;
10619 num_pseudos += tdep->pacbti_pseudo_count;
10620 }
10621
ae66a8f1 10622 /* Set some pseudo register hooks, if we have pseudo registers. */
a01567f4 10623 if (tdep->have_s_pseudos || have_mve || have_pacbti)
ae66a8f1 10624 {
58d6951d
DJ
10625 set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
10626 set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
10627 set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
10628 }
10629
123dc839 10630 /* Add standard register aliases. We add aliases even for those
85102364 10631 names which are used by the current architecture - it's simpler,
123dc839
DJ
10632 and does no harm, since nothing ever lists user registers. */
10633 for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
10634 user_reg_add (gdbarch, arm_register_aliases[i].name,
10635 value_of_arm_user_reg, &arm_register_aliases[i].regnum);
10636
65b48a81
PB
10637 set_gdbarch_disassembler_options (gdbarch, &arm_disassembler_options);
10638 set_gdbarch_valid_disassembler_options (gdbarch, disassembler_options_arm ());
10639
3426ae57
AH
10640 set_gdbarch_gnu_triplet_regexp (gdbarch, arm_gnu_triplet_regexp);
10641
39bbf761
RE
10642 return gdbarch;
10643}
10644
97e03143 10645static void
2af46ca0 10646arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
97e03143 10647{
08106042 10648 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
97e03143
RE
10649
10650 if (tdep == NULL)
10651 return;
10652
6cb06a8c
TT
10653 gdb_printf (file, _("arm_dump_tdep: fp_model = %i\n"),
10654 (int) tdep->fp_model);
10655 gdb_printf (file, _("arm_dump_tdep: have_fpa_registers = %i\n"),
10656 (int) tdep->have_fpa_registers);
10657 gdb_printf (file, _("arm_dump_tdep: have_wmmx_registers = %i\n"),
10658 (int) tdep->have_wmmx_registers);
10659 gdb_printf (file, _("arm_dump_tdep: vfp_register_count = %i\n"),
10660 (int) tdep->vfp_register_count);
10661 gdb_printf (file, _("arm_dump_tdep: have_s_pseudos = %s\n"),
10662 tdep->have_s_pseudos? "true" : "false");
10663 gdb_printf (file, _("arm_dump_tdep: s_pseudo_base = %i\n"),
10664 (int) tdep->s_pseudo_base);
10665 gdb_printf (file, _("arm_dump_tdep: s_pseudo_count = %i\n"),
10666 (int) tdep->s_pseudo_count);
10667 gdb_printf (file, _("arm_dump_tdep: have_q_pseudos = %s\n"),
10668 tdep->have_q_pseudos? "true" : "false");
10669 gdb_printf (file, _("arm_dump_tdep: q_pseudo_base = %i\n"),
10670 (int) tdep->q_pseudo_base);
10671 gdb_printf (file, _("arm_dump_tdep: q_pseudo_count = %i\n"),
10672 (int) tdep->q_pseudo_count);
10673 gdb_printf (file, _("arm_dump_tdep: have_neon = %i\n"),
10674 (int) tdep->have_neon);
10675 gdb_printf (file, _("arm_dump_tdep: have_mve = %s\n"),
10676 tdep->have_mve? "yes" : "no");
10677 gdb_printf (file, _("arm_dump_tdep: mve_vpr_regnum = %i\n"),
10678 tdep->mve_vpr_regnum);
10679 gdb_printf (file, _("arm_dump_tdep: mve_pseudo_base = %i\n"),
10680 tdep->mve_pseudo_base);
10681 gdb_printf (file, _("arm_dump_tdep: mve_pseudo_count = %i\n"),
10682 tdep->mve_pseudo_count);
9074667a
CL
10683 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_regnum = %i\n"),
10684 tdep->m_profile_msp_regnum);
10685 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_regnum = %i\n"),
10686 tdep->m_profile_psp_regnum);
ae7e2f45
CL
10687 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_ns_regnum = %i\n"),
10688 tdep->m_profile_msp_ns_regnum);
10689 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_ns_regnum = %i\n"),
10690 tdep->m_profile_psp_ns_regnum);
10691 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_s_regnum = %i\n"),
10692 tdep->m_profile_msp_s_regnum);
10693 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_s_regnum = %i\n"),
10694 tdep->m_profile_psp_s_regnum);
6cb06a8c
TT
10695 gdb_printf (file, _("arm_dump_tdep: Lowest pc = 0x%lx\n"),
10696 (unsigned long) tdep->lowest_pc);
a01567f4
LM
10697 gdb_printf (file, _("arm_dump_tdep: have_pacbti = %s\n"),
10698 tdep->have_pacbti? "yes" : "no");
10699 gdb_printf (file, _("arm_dump_tdep: pacbti_pseudo_base = %i\n"),
10700 tdep->pacbti_pseudo_base);
10701 gdb_printf (file, _("arm_dump_tdep: pacbti_pseudo_count = %i\n"),
10702 tdep->pacbti_pseudo_count);
10703 gdb_printf (file, _("arm_dump_tdep: is_m = %s\n"),
10704 tdep->is_m? "yes" : "no");
97e03143
RE
10705}
10706
0d4c07af 10707#if GDB_SELF_TEST
b121eeb9
YQ
10708namespace selftests
10709{
10710static void arm_record_test (void);
9ecab40c 10711static void arm_analyze_prologue_test ();
b121eeb9 10712}
0d4c07af 10713#endif
b121eeb9 10714
6c265988 10715void _initialize_arm_tdep ();
c906108c 10716void
6c265988 10717_initialize_arm_tdep ()
c906108c 10718{
bc90b915 10719 long length;
65b48a81 10720 int i, j;
edefbb7c
AC
10721 char regdesc[1024], *rdptr = regdesc;
10722 size_t rest = sizeof (regdesc);
085dd6e6 10723
42cf1509 10724 gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
97e03143 10725
0e9e9abd 10726 /* Add ourselves to objfile event chain. */
c90e7d63 10727 gdb::observers::new_objfile.attach (arm_exidx_new_objfile, "arm-tdep");
0e9e9abd 10728
70f80edf
JT
10729 /* Register an ELF OS ABI sniffer for ARM binaries. */
10730 gdbarch_register_osabi_sniffer (bfd_arch_arm,
10731 bfd_target_elf_flavour,
10732 arm_elf_osabi_sniffer);
10733
afd7eef0 10734 /* Add root prefix command for all "set arm"/"show arm" commands. */
f54bdb6d
SM
10735 add_setshow_prefix_cmd ("arm", no_class,
10736 _("Various ARM-specific commands."),
10737 _("Various ARM-specific commands."),
10738 &setarmcmdlist, &showarmcmdlist,
10739 &setlist, &showlist);
c5aa993b 10740
65b48a81 10741 arm_disassembler_options = xstrdup ("reg-names-std");
471b9d15
MR
10742 const disasm_options_t *disasm_options
10743 = &disassembler_options_arm ()->options;
65b48a81
PB
10744 int num_disassembly_styles = 0;
10745 for (i = 0; disasm_options->name[i] != NULL; i++)
08dedd66 10746 if (startswith (disasm_options->name[i], "reg-names-"))
65b48a81
PB
10747 num_disassembly_styles++;
10748
10749 /* Initialize the array that will be passed to add_setshow_enum_cmd(). */
8d749320 10750 valid_disassembly_styles = XNEWVEC (const char *,
65b48a81
PB
10751 num_disassembly_styles + 1);
10752 for (i = j = 0; disasm_options->name[i] != NULL; i++)
08dedd66 10753 if (startswith (disasm_options->name[i], "reg-names-"))
65b48a81
PB
10754 {
10755 size_t offset = strlen ("reg-names-");
10756 const char *style = disasm_options->name[i];
10757 valid_disassembly_styles[j++] = &style[offset];
dedb7102
TT
10758 if (strcmp (&style[offset], "std") == 0)
10759 disassembly_style = &style[offset];
65b48a81
PB
10760 length = snprintf (rdptr, rest, "%s - %s\n", &style[offset],
10761 disasm_options->description[i]);
10762 rdptr += length;
10763 rest -= length;
10764 }
94c30b78 10765 /* Mark the end of valid options. */
65b48a81 10766 valid_disassembly_styles[num_disassembly_styles] = NULL;
c906108c 10767
edefbb7c 10768 /* Create the help text. */
d7e74731
PA
10769 std::string helptext = string_printf ("%s%s%s",
10770 _("The valid values are:\n"),
10771 regdesc,
10772 _("The default is \"std\"."));
ed9a39eb 10773
edefbb7c
AC
10774 add_setshow_enum_cmd("disassembler", no_class,
10775 valid_disassembly_styles, &disassembly_style,
10776 _("Set the disassembly style."),
10777 _("Show the disassembly style."),
09b0e4b0 10778 helptext.c_str (),
2c5b56ce 10779 set_disassembly_style_sfunc,
65b48a81 10780 show_disassembly_style_sfunc,
7376b4c2 10781 &setarmcmdlist, &showarmcmdlist);
edefbb7c
AC
10782
10783 add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
10784 _("Set usage of ARM 32-bit mode."),
10785 _("Show usage of ARM 32-bit mode."),
10786 _("When off, a 26-bit PC will be used."),
2c5b56ce 10787 NULL,
0963b4bd
MS
10788 NULL, /* FIXME: i18n: Usage of ARM 32-bit
10789 mode is %s. */
26304000 10790 &setarmcmdlist, &showarmcmdlist);
c906108c 10791
fd50bc42 10792 /* Add a command to allow the user to force the FPU model. */
edefbb7c
AC
10793 add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
10794 _("Set the floating point type."),
10795 _("Show the floating point type."),
10796 _("auto - Determine the FP typefrom the OS-ABI.\n\
10797softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
10798fpa - FPA co-processor (GCC compiled).\n\
10799softvfp - Software FP with pure-endian doubles.\n\
10800vfp - VFP co-processor."),
edefbb7c 10801 set_fp_model_sfunc, show_fp_model,
7376b4c2 10802 &setarmcmdlist, &showarmcmdlist);
fd50bc42 10803
28e97307
DJ
10804 /* Add a command to allow the user to force the ABI. */
10805 add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
10806 _("Set the ABI."),
10807 _("Show the ABI."),
10808 NULL, arm_set_abi, arm_show_abi,
10809 &setarmcmdlist, &showarmcmdlist);
10810
0428b8f5
DJ
10811 /* Add two commands to allow the user to force the assumed
10812 execution mode. */
10813 add_setshow_enum_cmd ("fallback-mode", class_support,
10814 arm_mode_strings, &arm_fallback_mode_string,
10815 _("Set the mode assumed when symbols are unavailable."),
10816 _("Show the mode assumed when symbols are unavailable."),
10817 NULL, NULL, arm_show_fallback_mode,
10818 &setarmcmdlist, &showarmcmdlist);
10819 add_setshow_enum_cmd ("force-mode", class_support,
10820 arm_mode_strings, &arm_force_mode_string,
10821 _("Set the mode assumed even when symbols are available."),
10822 _("Show the mode assumed even when symbols are available."),
10823 NULL, NULL, arm_show_force_mode,
10824 &setarmcmdlist, &showarmcmdlist);
10825
ef273377
CL
10826 /* Add a command to stop triggering security exceptions when
10827 unwinding exception stacks. */
10828 add_setshow_boolean_cmd ("unwind-secure-frames", no_class, &arm_unwind_secure_frames,
10829 _("Set usage of non-secure to secure exception stack unwinding."),
10830 _("Show usage of non-secure to secure exception stack unwinding."),
10831 _("When on, the debugger can trigger memory access traps."),
10832 NULL, arm_show_unwind_secure_frames,
10833 &setarmcmdlist, &showarmcmdlist);
10834
6529d2dd 10835 /* Debugging flag. */
edefbb7c
AC
10836 add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
10837 _("Set ARM debugging."),
10838 _("Show ARM debugging."),
10839 _("When on, arm-specific debugging is enabled."),
2c5b56ce 10840 NULL,
7915a72c 10841 NULL, /* FIXME: i18n: "ARM debugging is %s. */
26304000 10842 &setdebuglist, &showdebuglist);
b121eeb9
YQ
10843
10844#if GDB_SELF_TEST
1526853e 10845 selftests::register_test ("arm-record", selftests::arm_record_test);
9ecab40c 10846 selftests::register_test ("arm_analyze_prologue", selftests::arm_analyze_prologue_test);
b121eeb9
YQ
10847#endif
10848
c906108c 10849}
72508ac0
PO
10850
10851/* ARM-reversible process record data structures. */
10852
10853#define ARM_INSN_SIZE_BYTES 4
10854#define THUMB_INSN_SIZE_BYTES 2
10855#define THUMB2_INSN_SIZE_BYTES 4
10856
10857
71e396f9
LM
10858/* Position of the bit within a 32-bit ARM instruction
10859 that defines whether the instruction is a load or store. */
72508ac0
PO
10860#define INSN_S_L_BIT_NUM 20
10861
10862#define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
dda83cd7
SM
10863 do \
10864 { \
10865 unsigned int reg_len = LENGTH; \
10866 if (reg_len) \
10867 { \
10868 REGS = XNEWVEC (uint32_t, reg_len); \
10869 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
10870 } \
10871 } \
10872 while (0)
72508ac0
PO
10873
10874#define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
dda83cd7
SM
10875 do \
10876 { \
10877 unsigned int mem_len = LENGTH; \
10878 if (mem_len) \
01add95b
SM
10879 { \
10880 MEMS = XNEWVEC (struct arm_mem_r, mem_len); \
10881 memcpy(&MEMS->len, &RECORD_BUF[0], \
10882 sizeof(struct arm_mem_r) * LENGTH); \
10883 } \
dda83cd7
SM
10884 } \
10885 while (0)
72508ac0
PO
10886
10887/* Checks whether insn is already recorded or yet to be decoded. (boolean expression). */
10888#define INSN_RECORDED(ARM_RECORD) \
dda83cd7 10889 (0 != (ARM_RECORD)->reg_rec_count || 0 != (ARM_RECORD)->mem_rec_count)
72508ac0
PO
10890
10891/* ARM memory record structure. */
10892struct arm_mem_r
10893{
10894 uint32_t len; /* Record length. */
bfbbec00 10895 uint32_t addr; /* Memory address. */
72508ac0
PO
10896};
10897
10898/* ARM instruction record contains opcode of current insn
10899 and execution state (before entry to decode_insn()),
10900 contains list of to-be-modified registers and
10901 memory blocks (on return from decode_insn()). */
10902
4748a9be 10903struct arm_insn_decode_record
72508ac0
PO
10904{
10905 struct gdbarch *gdbarch;
10906 struct regcache *regcache;
10907 CORE_ADDR this_addr; /* Address of the insn being decoded. */
10908 uint32_t arm_insn; /* Should accommodate thumb. */
10909 uint32_t cond; /* Condition code. */
10910 uint32_t opcode; /* Insn opcode. */
10911 uint32_t decode; /* Insn decode bits. */
10912 uint32_t mem_rec_count; /* No of mem records. */
10913 uint32_t reg_rec_count; /* No of reg records. */
10914 uint32_t *arm_regs; /* Registers to be saved for this record. */
10915 struct arm_mem_r *arm_mems; /* Memory to be saved for this record. */
4748a9be 10916};
72508ac0
PO
10917
10918
10919/* Checks ARM SBZ and SBO mandatory fields. */
10920
10921static int
10922sbo_sbz (uint32_t insn, uint32_t bit_num, uint32_t len, uint32_t sbo)
10923{
10924 uint32_t ones = bits (insn, bit_num - 1, (bit_num -1) + (len - 1));
10925
10926 if (!len)
10927 return 1;
10928
10929 if (!sbo)
10930 ones = ~ones;
10931
10932 while (ones)
10933 {
10934 if (!(ones & sbo))
dda83cd7
SM
10935 {
10936 return 0;
10937 }
72508ac0
PO
10938 ones = ones >> 1;
10939 }
10940 return 1;
10941}
10942
c6ec2b30
OJ
10943enum arm_record_result
10944{
10945 ARM_RECORD_SUCCESS = 0,
10946 ARM_RECORD_FAILURE = 1
10947};
10948
0d1703b8 10949enum arm_record_strx_t
72508ac0
PO
10950{
10951 ARM_RECORD_STRH=1,
10952 ARM_RECORD_STRD
0d1703b8 10953};
72508ac0 10954
0d1703b8 10955enum record_type_t
72508ac0
PO
10956{
10957 ARM_RECORD=1,
10958 THUMB_RECORD,
10959 THUMB2_RECORD
0d1703b8 10960};
72508ac0
PO
10961
10962
10963static int
4748a9be 10964arm_record_strx (arm_insn_decode_record *arm_insn_r, uint32_t *record_buf,
dda83cd7 10965 uint32_t *record_buf_mem, arm_record_strx_t str_type)
72508ac0
PO
10966{
10967
10968 struct regcache *reg_cache = arm_insn_r->regcache;
10969 ULONGEST u_regval[2]= {0};
10970
10971 uint32_t reg_src1 = 0, reg_src2 = 0;
10972 uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
72508ac0
PO
10973
10974 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10975 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
72508ac0
PO
10976
10977 if (14 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
10978 {
10979 /* 1) Handle misc store, immediate offset. */
10980 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
10981 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
10982 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10983 regcache_raw_read_unsigned (reg_cache, reg_src1,
dda83cd7 10984 &u_regval[0]);
72508ac0 10985 if (ARM_PC_REGNUM == reg_src1)
dda83cd7
SM
10986 {
10987 /* If R15 was used as Rn, hence current PC+8. */
10988 u_regval[0] = u_regval[0] + 8;
10989 }
72508ac0
PO
10990 offset_8 = (immed_high << 4) | immed_low;
10991 /* Calculate target store address. */
10992 if (14 == arm_insn_r->opcode)
dda83cd7
SM
10993 {
10994 tgt_mem_addr = u_regval[0] + offset_8;
10995 }
72508ac0 10996 else
dda83cd7
SM
10997 {
10998 tgt_mem_addr = u_regval[0] - offset_8;
10999 }
72508ac0 11000 if (ARM_RECORD_STRH == str_type)
dda83cd7
SM
11001 {
11002 record_buf_mem[0] = 2;
11003 record_buf_mem[1] = tgt_mem_addr;
11004 arm_insn_r->mem_rec_count = 1;
11005 }
72508ac0 11006 else if (ARM_RECORD_STRD == str_type)
dda83cd7
SM
11007 {
11008 record_buf_mem[0] = 4;
11009 record_buf_mem[1] = tgt_mem_addr;
11010 record_buf_mem[2] = 4;
11011 record_buf_mem[3] = tgt_mem_addr + 4;
11012 arm_insn_r->mem_rec_count = 2;
11013 }
72508ac0
PO
11014 }
11015 else if (12 == arm_insn_r->opcode || 8 == arm_insn_r->opcode)
11016 {
11017 /* 2) Store, register offset. */
11018 /* Get Rm. */
11019 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11020 /* Get Rn. */
11021 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11022 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11023 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11024 if (15 == reg_src2)
dda83cd7
SM
11025 {
11026 /* If R15 was used as Rn, hence current PC+8. */
11027 u_regval[0] = u_regval[0] + 8;
11028 }
72508ac0
PO
11029 /* Calculate target store address, Rn +/- Rm, register offset. */
11030 if (12 == arm_insn_r->opcode)
dda83cd7
SM
11031 {
11032 tgt_mem_addr = u_regval[0] + u_regval[1];
11033 }
72508ac0 11034 else
dda83cd7
SM
11035 {
11036 tgt_mem_addr = u_regval[1] - u_regval[0];
11037 }
72508ac0 11038 if (ARM_RECORD_STRH == str_type)
dda83cd7
SM
11039 {
11040 record_buf_mem[0] = 2;
11041 record_buf_mem[1] = tgt_mem_addr;
11042 arm_insn_r->mem_rec_count = 1;
11043 }
72508ac0 11044 else if (ARM_RECORD_STRD == str_type)
dda83cd7
SM
11045 {
11046 record_buf_mem[0] = 4;
11047 record_buf_mem[1] = tgt_mem_addr;
11048 record_buf_mem[2] = 4;
11049 record_buf_mem[3] = tgt_mem_addr + 4;
11050 arm_insn_r->mem_rec_count = 2;
11051 }
72508ac0
PO
11052 }
11053 else if (11 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
dda83cd7 11054 || 2 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
72508ac0
PO
11055 {
11056 /* 3) Store, immediate pre-indexed. */
11057 /* 5) Store, immediate post-indexed. */
11058 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
11059 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
11060 offset_8 = (immed_high << 4) | immed_low;
11061 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11062 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11063 /* Calculate target store address, Rn +/- Rm, register offset. */
11064 if (15 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
dda83cd7
SM
11065 {
11066 tgt_mem_addr = u_regval[0] + offset_8;
11067 }
72508ac0 11068 else
dda83cd7
SM
11069 {
11070 tgt_mem_addr = u_regval[0] - offset_8;
11071 }
72508ac0 11072 if (ARM_RECORD_STRH == str_type)
dda83cd7
SM
11073 {
11074 record_buf_mem[0] = 2;
11075 record_buf_mem[1] = tgt_mem_addr;
11076 arm_insn_r->mem_rec_count = 1;
11077 }
72508ac0 11078 else if (ARM_RECORD_STRD == str_type)
dda83cd7
SM
11079 {
11080 record_buf_mem[0] = 4;
11081 record_buf_mem[1] = tgt_mem_addr;
11082 record_buf_mem[2] = 4;
11083 record_buf_mem[3] = tgt_mem_addr + 4;
11084 arm_insn_r->mem_rec_count = 2;
11085 }
72508ac0
PO
11086 /* Record Rn also as it changes. */
11087 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
11088 arm_insn_r->reg_rec_count = 1;
11089 }
11090 else if (9 == arm_insn_r->opcode || 13 == arm_insn_r->opcode
dda83cd7 11091 || 0 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
72508ac0
PO
11092 {
11093 /* 4) Store, register pre-indexed. */
11094 /* 6) Store, register post -indexed. */
11095 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11096 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11097 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11098 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11099 /* Calculate target store address, Rn +/- Rm, register offset. */
11100 if (13 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
dda83cd7
SM
11101 {
11102 tgt_mem_addr = u_regval[0] + u_regval[1];
11103 }
72508ac0 11104 else
dda83cd7
SM
11105 {
11106 tgt_mem_addr = u_regval[1] - u_regval[0];
11107 }
72508ac0 11108 if (ARM_RECORD_STRH == str_type)
dda83cd7
SM
11109 {
11110 record_buf_mem[0] = 2;
11111 record_buf_mem[1] = tgt_mem_addr;
11112 arm_insn_r->mem_rec_count = 1;
11113 }
72508ac0 11114 else if (ARM_RECORD_STRD == str_type)
dda83cd7
SM
11115 {
11116 record_buf_mem[0] = 4;
11117 record_buf_mem[1] = tgt_mem_addr;
11118 record_buf_mem[2] = 4;
11119 record_buf_mem[3] = tgt_mem_addr + 4;
11120 arm_insn_r->mem_rec_count = 2;
11121 }
72508ac0
PO
11122 /* Record Rn also as it changes. */
11123 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
11124 arm_insn_r->reg_rec_count = 1;
11125 }
11126 return 0;
11127}
11128
11129/* Handling ARM extension space insns. */
11130
11131static int
4748a9be 11132arm_record_extension_space (arm_insn_decode_record *arm_insn_r)
72508ac0 11133{
df95a9cf 11134 int ret = 0; /* Return value: -1:record failure ; 0:success */
72508ac0
PO
11135 uint32_t opcode1 = 0, opcode2 = 0, insn_op1 = 0;
11136 uint32_t record_buf[8], record_buf_mem[8];
11137 uint32_t reg_src1 = 0;
72508ac0
PO
11138 struct regcache *reg_cache = arm_insn_r->regcache;
11139 ULONGEST u_regval = 0;
11140
11141 gdb_assert (!INSN_RECORDED(arm_insn_r));
11142 /* Handle unconditional insn extension space. */
11143
11144 opcode1 = bits (arm_insn_r->arm_insn, 20, 27);
11145 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
11146 if (arm_insn_r->cond)
11147 {
11148 /* PLD has no affect on architectural state, it just affects
dda83cd7 11149 the caches. */
72508ac0 11150 if (5 == ((opcode1 & 0xE0) >> 5))
dda83cd7
SM
11151 {
11152 /* BLX(1) */
11153 record_buf[0] = ARM_PS_REGNUM;
11154 record_buf[1] = ARM_LR_REGNUM;
11155 arm_insn_r->reg_rec_count = 2;
11156 }
72508ac0
PO
11157 /* STC2, LDC2, MCR2, MRC2, CDP2: <TBD>, co-processor insn. */
11158 }
11159
11160
11161 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
11162 if (3 == opcode1 && bit (arm_insn_r->arm_insn, 4))
11163 {
11164 ret = -1;
11165 /* Undefined instruction on ARM V5; need to handle if later
dda83cd7 11166 versions define it. */
72508ac0
PO
11167 }
11168
11169 opcode1 = bits (arm_insn_r->arm_insn, 24, 27);
11170 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
11171 insn_op1 = bits (arm_insn_r->arm_insn, 20, 23);
11172
11173 /* Handle arithmetic insn extension space. */
11174 if (!opcode1 && 9 == opcode2 && 1 != arm_insn_r->cond
11175 && !INSN_RECORDED(arm_insn_r))
11176 {
11177 /* Handle MLA(S) and MUL(S). */
b020ff80 11178 if (in_inclusive_range (insn_op1, 0U, 3U))
01add95b
SM
11179 {
11180 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11181 record_buf[1] = ARM_PS_REGNUM;
11182 arm_insn_r->reg_rec_count = 2;
11183 }
b020ff80 11184 else if (in_inclusive_range (insn_op1, 4U, 15U))
01add95b
SM
11185 {
11186 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
11187 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11188 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11189 record_buf[2] = ARM_PS_REGNUM;
11190 arm_insn_r->reg_rec_count = 3;
11191 }
72508ac0
PO
11192 }
11193
11194 opcode1 = bits (arm_insn_r->arm_insn, 26, 27);
11195 opcode2 = bits (arm_insn_r->arm_insn, 23, 24);
11196 insn_op1 = bits (arm_insn_r->arm_insn, 21, 22);
11197
11198 /* Handle control insn extension space. */
11199
11200 if (!opcode1 && 2 == opcode2 && !bit (arm_insn_r->arm_insn, 20)
11201 && 1 != arm_insn_r->cond && !INSN_RECORDED(arm_insn_r))
11202 {
11203 if (!bit (arm_insn_r->arm_insn,25))
dda83cd7
SM
11204 {
11205 if (!bits (arm_insn_r->arm_insn, 4, 7))
11206 {
11207 if ((0 == insn_op1) || (2 == insn_op1))
11208 {
11209 /* MRS. */
11210 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11211 arm_insn_r->reg_rec_count = 1;
11212 }
11213 else if (1 == insn_op1)
11214 {
11215 /* CSPR is going to be changed. */
11216 record_buf[0] = ARM_PS_REGNUM;
11217 arm_insn_r->reg_rec_count = 1;
11218 }
11219 else if (3 == insn_op1)
11220 {
11221 /* SPSR is going to be changed. */
11222 /* We need to get SPSR value, which is yet to be done. */
11223 return -1;
11224 }
11225 }
11226 else if (1 == bits (arm_insn_r->arm_insn, 4, 7))
11227 {
11228 if (1 == insn_op1)
11229 {
11230 /* BX. */
11231 record_buf[0] = ARM_PS_REGNUM;
11232 arm_insn_r->reg_rec_count = 1;
11233 }
11234 else if (3 == insn_op1)
11235 {
11236 /* CLZ. */
11237 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11238 arm_insn_r->reg_rec_count = 1;
11239 }
11240 }
11241 else if (3 == bits (arm_insn_r->arm_insn, 4, 7))
11242 {
11243 /* BLX. */
11244 record_buf[0] = ARM_PS_REGNUM;
11245 record_buf[1] = ARM_LR_REGNUM;
11246 arm_insn_r->reg_rec_count = 2;
11247 }
11248 else if (5 == bits (arm_insn_r->arm_insn, 4, 7))
11249 {
11250 /* QADD, QSUB, QDADD, QDSUB */
11251 record_buf[0] = ARM_PS_REGNUM;
11252 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11253 arm_insn_r->reg_rec_count = 2;
11254 }
11255 else if (7 == bits (arm_insn_r->arm_insn, 4, 7))
11256 {
11257 /* BKPT. */
11258 record_buf[0] = ARM_PS_REGNUM;
11259 record_buf[1] = ARM_LR_REGNUM;
11260 arm_insn_r->reg_rec_count = 2;
11261
11262 /* Save SPSR also;how? */
11263 return -1;
11264 }
11265 else if(8 == bits (arm_insn_r->arm_insn, 4, 7)
11266 || 10 == bits (arm_insn_r->arm_insn, 4, 7)
11267 || 12 == bits (arm_insn_r->arm_insn, 4, 7)
11268 || 14 == bits (arm_insn_r->arm_insn, 4, 7)
11269 )
11270 {
11271 if (0 == insn_op1 || 1 == insn_op1)
11272 {
11273 /* SMLA<x><y>, SMLAW<y>, SMULW<y>. */
11274 /* We dont do optimization for SMULW<y> where we
11275 need only Rd. */
11276 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11277 record_buf[1] = ARM_PS_REGNUM;
11278 arm_insn_r->reg_rec_count = 2;
11279 }
11280 else if (2 == insn_op1)
11281 {
11282 /* SMLAL<x><y>. */
11283 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11284 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
11285 arm_insn_r->reg_rec_count = 2;
11286 }
11287 else if (3 == insn_op1)
11288 {
11289 /* SMUL<x><y>. */
11290 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11291 arm_insn_r->reg_rec_count = 1;
11292 }
11293 }
11294 }
72508ac0 11295 else
dda83cd7
SM
11296 {
11297 /* MSR : immediate form. */
11298 if (1 == insn_op1)
11299 {
11300 /* CSPR is going to be changed. */
11301 record_buf[0] = ARM_PS_REGNUM;
11302 arm_insn_r->reg_rec_count = 1;
11303 }
11304 else if (3 == insn_op1)
11305 {
11306 /* SPSR is going to be changed. */
11307 /* we need to get SPSR value, which is yet to be done */
11308 return -1;
11309 }
11310 }
72508ac0
PO
11311 }
11312
11313 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
11314 opcode2 = bits (arm_insn_r->arm_insn, 20, 24);
11315 insn_op1 = bits (arm_insn_r->arm_insn, 5, 6);
11316
11317 /* Handle load/store insn extension space. */
11318
11319 if (!opcode1 && bit (arm_insn_r->arm_insn, 7)
11320 && bit (arm_insn_r->arm_insn, 4) && 1 != arm_insn_r->cond
11321 && !INSN_RECORDED(arm_insn_r))
11322 {
11323 /* SWP/SWPB. */
11324 if (0 == insn_op1)
dda83cd7
SM
11325 {
11326 /* These insn, changes register and memory as well. */
11327 /* SWP or SWPB insn. */
11328 /* Get memory address given by Rn. */
11329 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11330 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11331 /* SWP insn ?, swaps word. */
11332 if (8 == arm_insn_r->opcode)
11333 {
11334 record_buf_mem[0] = 4;
11335 }
11336 else
11337 {
11338 /* SWPB insn, swaps only byte. */
11339 record_buf_mem[0] = 1;
11340 }
11341 record_buf_mem[1] = u_regval;
11342 arm_insn_r->mem_rec_count = 1;
11343 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11344 arm_insn_r->reg_rec_count = 1;
11345 }
72508ac0 11346 else if (1 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
dda83cd7
SM
11347 {
11348 /* STRH. */
11349 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11350 ARM_RECORD_STRH);
11351 }
72508ac0 11352 else if (2 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
dda83cd7
SM
11353 {
11354 /* LDRD. */
11355 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11356 record_buf[1] = record_buf[0] + 1;
11357 arm_insn_r->reg_rec_count = 2;
11358 }
72508ac0 11359 else if (3 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
dda83cd7
SM
11360 {
11361 /* STRD. */
11362 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11363 ARM_RECORD_STRD);
11364 }
72508ac0 11365 else if (bit (arm_insn_r->arm_insn, 20) && insn_op1 <= 3)
dda83cd7
SM
11366 {
11367 /* LDRH, LDRSB, LDRSH. */
11368 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11369 arm_insn_r->reg_rec_count = 1;
11370 }
72508ac0
PO
11371
11372 }
11373
11374 opcode1 = bits (arm_insn_r->arm_insn, 23, 27);
11375 if (24 == opcode1 && bit (arm_insn_r->arm_insn, 21)
11376 && !INSN_RECORDED(arm_insn_r))
11377 {
11378 ret = -1;
11379 /* Handle coprocessor insn extension space. */
11380 }
11381
11382 /* To be done for ARMv5 and later; as of now we return -1. */
11383 if (-1 == ret)
ca92db2d 11384 return ret;
72508ac0
PO
11385
11386 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11387 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11388
11389 return ret;
11390}
11391
11392/* Handling opcode 000 insns. */
11393
11394static int
4748a9be 11395arm_record_data_proc_misc_ld_str (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
11396{
11397 struct regcache *reg_cache = arm_insn_r->regcache;
11398 uint32_t record_buf[8], record_buf_mem[8];
11399 ULONGEST u_regval[2] = {0};
11400
8d49165d 11401 uint32_t reg_src1 = 0;
72508ac0
PO
11402 uint32_t opcode1 = 0;
11403
11404 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11405 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11406 opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
11407
2d9e6acb 11408 if (!((opcode1 & 0x19) == 0x10))
72508ac0 11409 {
2d9e6acb
YQ
11410 /* Data-processing (register) and Data-processing (register-shifted
11411 register */
11412 /* Out of 11 shifter operands mode, all the insn modifies destination
11413 register, which is specified by 13-16 decode. */
11414 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11415 record_buf[1] = ARM_PS_REGNUM;
11416 arm_insn_r->reg_rec_count = 2;
72508ac0 11417 }
2d9e6acb 11418 else if ((arm_insn_r->decode < 8) && ((opcode1 & 0x19) == 0x10))
72508ac0 11419 {
2d9e6acb
YQ
11420 /* Miscellaneous instructions */
11421
11422 if (3 == arm_insn_r->decode && 0x12 == opcode1
11423 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11424 {
11425 /* Handle BLX, branch and link/exchange. */
11426 if (9 == arm_insn_r->opcode)
11427 {
11428 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm,
11429 and R14 stores the return address. */
11430 record_buf[0] = ARM_PS_REGNUM;
11431 record_buf[1] = ARM_LR_REGNUM;
11432 arm_insn_r->reg_rec_count = 2;
11433 }
11434 }
11435 else if (7 == arm_insn_r->decode && 0x12 == opcode1)
11436 {
11437 /* Handle enhanced software breakpoint insn, BKPT. */
11438 /* CPSR is changed to be executed in ARM state, disabling normal
11439 interrupts, entering abort mode. */
11440 /* According to high vector configuration PC is set. */
11441 /* user hit breakpoint and type reverse, in
11442 that case, we need to go back with previous CPSR and
11443 Program Counter. */
11444 record_buf[0] = ARM_PS_REGNUM;
11445 record_buf[1] = ARM_LR_REGNUM;
11446 arm_insn_r->reg_rec_count = 2;
11447
11448 /* Save SPSR also; how? */
11449 return -1;
11450 }
11451 else if (1 == arm_insn_r->decode && 0x12 == opcode1
11452 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11453 {
11454 /* Handle BX, branch and link/exchange. */
11455 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm. */
11456 record_buf[0] = ARM_PS_REGNUM;
11457 arm_insn_r->reg_rec_count = 1;
11458 }
11459 else if (1 == arm_insn_r->decode && 0x16 == opcode1
11460 && sbo_sbz (arm_insn_r->arm_insn, 9, 4, 1)
11461 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1))
11462 {
11463 /* Count leading zeros: CLZ. */
11464 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11465 arm_insn_r->reg_rec_count = 1;
11466 }
11467 else if (!bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11468 && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11469 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1)
11470 && sbo_sbz (arm_insn_r->arm_insn, 1, 12, 0))
11471 {
11472 /* Handle MRS insn. */
11473 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11474 arm_insn_r->reg_rec_count = 1;
11475 }
72508ac0 11476 }
2d9e6acb 11477 else if (9 == arm_insn_r->decode && opcode1 < 0x10)
72508ac0 11478 {
2d9e6acb
YQ
11479 /* Multiply and multiply-accumulate */
11480
11481 /* Handle multiply instructions. */
11482 /* MLA, MUL, SMLAL, SMULL, UMLAL, UMULL. */
11483 if (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)
f2a883a8
SM
11484 {
11485 /* Handle MLA and MUL. */
11486 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11487 record_buf[1] = ARM_PS_REGNUM;
11488 arm_insn_r->reg_rec_count = 2;
11489 }
11490 else if (4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11491 {
11492 /* Handle SMLAL, SMULL, UMLAL, UMULL. */
11493 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11494 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11495 record_buf[2] = ARM_PS_REGNUM;
11496 arm_insn_r->reg_rec_count = 3;
11497 }
2d9e6acb
YQ
11498 }
11499 else if (9 == arm_insn_r->decode && opcode1 > 0x10)
11500 {
11501 /* Synchronization primitives */
11502
72508ac0
PO
11503 /* Handling SWP, SWPB. */
11504 /* These insn, changes register and memory as well. */
11505 /* SWP or SWPB insn. */
11506
11507 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11508 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11509 /* SWP insn ?, swaps word. */
11510 if (8 == arm_insn_r->opcode)
2d9e6acb
YQ
11511 {
11512 record_buf_mem[0] = 4;
11513 }
11514 else
11515 {
11516 /* SWPB insn, swaps only byte. */
11517 record_buf_mem[0] = 1;
11518 }
72508ac0
PO
11519 record_buf_mem[1] = u_regval[0];
11520 arm_insn_r->mem_rec_count = 1;
11521 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11522 arm_insn_r->reg_rec_count = 1;
11523 }
2d9e6acb
YQ
11524 else if (11 == arm_insn_r->decode || 13 == arm_insn_r->decode
11525 || 15 == arm_insn_r->decode)
72508ac0 11526 {
2d9e6acb
YQ
11527 if ((opcode1 & 0x12) == 2)
11528 {
11529 /* Extra load/store (unprivileged) */
11530 return -1;
11531 }
11532 else
11533 {
11534 /* Extra load/store */
11535 switch (bits (arm_insn_r->arm_insn, 5, 6))
11536 {
11537 case 1:
11538 if ((opcode1 & 0x05) == 0x0 || (opcode1 & 0x05) == 0x4)
11539 {
11540 /* STRH (register), STRH (immediate) */
11541 arm_record_strx (arm_insn_r, &record_buf[0],
11542 &record_buf_mem[0], ARM_RECORD_STRH);
11543 }
11544 else if ((opcode1 & 0x05) == 0x1)
11545 {
11546 /* LDRH (register) */
11547 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11548 arm_insn_r->reg_rec_count = 1;
72508ac0 11549
2d9e6acb
YQ
11550 if (bit (arm_insn_r->arm_insn, 21))
11551 {
11552 /* Write back to Rn. */
11553 record_buf[arm_insn_r->reg_rec_count++]
11554 = bits (arm_insn_r->arm_insn, 16, 19);
11555 }
11556 }
11557 else if ((opcode1 & 0x05) == 0x5)
11558 {
11559 /* LDRH (immediate), LDRH (literal) */
11560 int rn = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0 11561
2d9e6acb
YQ
11562 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11563 arm_insn_r->reg_rec_count = 1;
11564
11565 if (rn != 15)
11566 {
11567 /*LDRH (immediate) */
11568 if (bit (arm_insn_r->arm_insn, 21))
11569 {
11570 /* Write back to Rn. */
11571 record_buf[arm_insn_r->reg_rec_count++] = rn;
11572 }
11573 }
11574 }
11575 else
11576 return -1;
11577 break;
11578 case 2:
11579 if ((opcode1 & 0x05) == 0x0)
11580 {
11581 /* LDRD (register) */
11582 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11583 record_buf[1] = record_buf[0] + 1;
11584 arm_insn_r->reg_rec_count = 2;
11585
11586 if (bit (arm_insn_r->arm_insn, 21))
11587 {
11588 /* Write back to Rn. */
11589 record_buf[arm_insn_r->reg_rec_count++]
11590 = bits (arm_insn_r->arm_insn, 16, 19);
11591 }
11592 }
11593 else if ((opcode1 & 0x05) == 0x1)
11594 {
11595 /* LDRSB (register) */
11596 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11597 arm_insn_r->reg_rec_count = 1;
11598
11599 if (bit (arm_insn_r->arm_insn, 21))
11600 {
11601 /* Write back to Rn. */
11602 record_buf[arm_insn_r->reg_rec_count++]
11603 = bits (arm_insn_r->arm_insn, 16, 19);
11604 }
11605 }
11606 else if ((opcode1 & 0x05) == 0x4 || (opcode1 & 0x05) == 0x5)
11607 {
11608 /* LDRD (immediate), LDRD (literal), LDRSB (immediate),
11609 LDRSB (literal) */
11610 int rn = bits (arm_insn_r->arm_insn, 16, 19);
11611
11612 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11613 arm_insn_r->reg_rec_count = 1;
11614
11615 if (rn != 15)
11616 {
11617 /*LDRD (immediate), LDRSB (immediate) */
11618 if (bit (arm_insn_r->arm_insn, 21))
11619 {
11620 /* Write back to Rn. */
11621 record_buf[arm_insn_r->reg_rec_count++] = rn;
11622 }
11623 }
11624 }
11625 else
11626 return -1;
11627 break;
11628 case 3:
11629 if ((opcode1 & 0x05) == 0x0)
11630 {
11631 /* STRD (register) */
11632 arm_record_strx (arm_insn_r, &record_buf[0],
11633 &record_buf_mem[0], ARM_RECORD_STRD);
11634 }
11635 else if ((opcode1 & 0x05) == 0x1)
11636 {
11637 /* LDRSH (register) */
11638 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11639 arm_insn_r->reg_rec_count = 1;
11640
11641 if (bit (arm_insn_r->arm_insn, 21))
11642 {
11643 /* Write back to Rn. */
11644 record_buf[arm_insn_r->reg_rec_count++]
11645 = bits (arm_insn_r->arm_insn, 16, 19);
11646 }
11647 }
11648 else if ((opcode1 & 0x05) == 0x4)
11649 {
11650 /* STRD (immediate) */
11651 arm_record_strx (arm_insn_r, &record_buf[0],
11652 &record_buf_mem[0], ARM_RECORD_STRD);
11653 }
11654 else if ((opcode1 & 0x05) == 0x5)
11655 {
11656 /* LDRSH (immediate), LDRSH (literal) */
11657 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11658 arm_insn_r->reg_rec_count = 1;
11659
11660 if (bit (arm_insn_r->arm_insn, 21))
11661 {
11662 /* Write back to Rn. */
11663 record_buf[arm_insn_r->reg_rec_count++]
11664 = bits (arm_insn_r->arm_insn, 16, 19);
11665 }
11666 }
11667 else
11668 return -1;
11669 break;
11670 default:
11671 return -1;
11672 }
11673 }
72508ac0
PO
11674 }
11675 else
11676 {
11677 return -1;
11678 }
11679
11680 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11681 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11682 return 0;
11683}
11684
11685/* Handling opcode 001 insns. */
11686
11687static int
4748a9be 11688arm_record_data_proc_imm (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
11689{
11690 uint32_t record_buf[8], record_buf_mem[8];
11691
11692 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11693 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11694
11695 if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11696 && 2 == bits (arm_insn_r->arm_insn, 20, 21)
11697 && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
11698 )
11699 {
11700 /* Handle MSR insn. */
11701 if (9 == arm_insn_r->opcode)
dda83cd7
SM
11702 {
11703 /* CSPR is going to be changed. */
11704 record_buf[0] = ARM_PS_REGNUM;
11705 arm_insn_r->reg_rec_count = 1;
11706 }
72508ac0 11707 else
dda83cd7
SM
11708 {
11709 /* SPSR is going to be changed. */
11710 }
72508ac0
PO
11711 }
11712 else if (arm_insn_r->opcode <= 15)
11713 {
11714 /* Normal data processing insns. */
11715 /* Out of 11 shifter operands mode, all the insn modifies destination
dda83cd7 11716 register, which is specified by 13-16 decode. */
72508ac0
PO
11717 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11718 record_buf[1] = ARM_PS_REGNUM;
11719 arm_insn_r->reg_rec_count = 2;
11720 }
11721 else
11722 {
11723 return -1;
11724 }
11725
11726 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11727 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11728 return 0;
11729}
11730
c55978a6 11731static int
4748a9be 11732arm_record_media (arm_insn_decode_record *arm_insn_r)
c55978a6
YQ
11733{
11734 uint32_t record_buf[8];
11735
11736 switch (bits (arm_insn_r->arm_insn, 22, 24))
11737 {
11738 case 0:
11739 /* Parallel addition and subtraction, signed */
11740 case 1:
11741 /* Parallel addition and subtraction, unsigned */
11742 case 2:
11743 case 3:
11744 /* Packing, unpacking, saturation and reversal */
11745 {
11746 int rd = bits (arm_insn_r->arm_insn, 12, 15);
11747
11748 record_buf[arm_insn_r->reg_rec_count++] = rd;
11749 }
11750 break;
11751
11752 case 4:
11753 case 5:
11754 /* Signed multiplies */
11755 {
11756 int rd = bits (arm_insn_r->arm_insn, 16, 19);
11757 unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 22);
11758
11759 record_buf[arm_insn_r->reg_rec_count++] = rd;
11760 if (op1 == 0x0)
11761 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
11762 else if (op1 == 0x4)
11763 record_buf[arm_insn_r->reg_rec_count++]
11764 = bits (arm_insn_r->arm_insn, 12, 15);
11765 }
11766 break;
11767
11768 case 6:
11769 {
11770 if (bit (arm_insn_r->arm_insn, 21)
11771 && bits (arm_insn_r->arm_insn, 5, 6) == 0x2)
11772 {
11773 /* SBFX */
11774 record_buf[arm_insn_r->reg_rec_count++]
11775 = bits (arm_insn_r->arm_insn, 12, 15);
11776 }
11777 else if (bits (arm_insn_r->arm_insn, 20, 21) == 0x0
11778 && bits (arm_insn_r->arm_insn, 5, 7) == 0x0)
11779 {
11780 /* USAD8 and USADA8 */
11781 record_buf[arm_insn_r->reg_rec_count++]
11782 = bits (arm_insn_r->arm_insn, 16, 19);
11783 }
11784 }
11785 break;
11786
11787 case 7:
11788 {
11789 if (bits (arm_insn_r->arm_insn, 20, 21) == 0x3
11790 && bits (arm_insn_r->arm_insn, 5, 7) == 0x7)
11791 {
11792 /* Permanently UNDEFINED */
11793 return -1;
11794 }
11795 else
11796 {
11797 /* BFC, BFI and UBFX */
11798 record_buf[arm_insn_r->reg_rec_count++]
11799 = bits (arm_insn_r->arm_insn, 12, 15);
11800 }
11801 }
11802 break;
11803
11804 default:
11805 return -1;
11806 }
11807
11808 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11809
11810 return 0;
11811}
11812
71e396f9 11813/* Handle ARM mode instructions with opcode 010. */
72508ac0
PO
11814
11815static int
4748a9be 11816arm_record_ld_st_imm_offset (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
11817{
11818 struct regcache *reg_cache = arm_insn_r->regcache;
11819
71e396f9
LM
11820 uint32_t reg_base , reg_dest;
11821 uint32_t offset_12, tgt_mem_addr;
72508ac0 11822 uint32_t record_buf[8], record_buf_mem[8];
71e396f9
LM
11823 unsigned char wback;
11824 ULONGEST u_regval;
72508ac0 11825
71e396f9
LM
11826 /* Calculate wback. */
11827 wback = (bit (arm_insn_r->arm_insn, 24) == 0)
11828 || (bit (arm_insn_r->arm_insn, 21) == 1);
72508ac0 11829
71e396f9
LM
11830 arm_insn_r->reg_rec_count = 0;
11831 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0
PO
11832
11833 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11834 {
71e396f9
LM
11835 /* LDR (immediate), LDR (literal), LDRB (immediate), LDRB (literal), LDRBT
11836 and LDRT. */
11837
72508ac0 11838 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
71e396f9
LM
11839 record_buf[arm_insn_r->reg_rec_count++] = reg_dest;
11840
11841 /* The LDR instruction is capable of doing branching. If MOV LR, PC
11842 preceeds a LDR instruction having R15 as reg_base, it
11843 emulates a branch and link instruction, and hence we need to save
11844 CPSR and PC as well. */
11845 if (ARM_PC_REGNUM == reg_dest)
11846 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
11847
11848 /* If wback is true, also save the base register, which is going to be
11849 written to. */
11850 if (wback)
11851 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
11852 }
11853 else
11854 {
71e396f9
LM
11855 /* STR (immediate), STRB (immediate), STRBT and STRT. */
11856
72508ac0 11857 offset_12 = bits (arm_insn_r->arm_insn, 0, 11);
71e396f9
LM
11858 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
11859
11860 /* Handle bit U. */
72508ac0 11861 if (bit (arm_insn_r->arm_insn, 23))
71e396f9
LM
11862 {
11863 /* U == 1: Add the offset. */
11864 tgt_mem_addr = (uint32_t) u_regval + offset_12;
11865 }
72508ac0 11866 else
71e396f9
LM
11867 {
11868 /* U == 0: subtract the offset. */
11869 tgt_mem_addr = (uint32_t) u_regval - offset_12;
11870 }
11871
11872 /* Bit 22 tells us whether the store instruction writes 1 byte or 4
11873 bytes. */
11874 if (bit (arm_insn_r->arm_insn, 22))
11875 {
11876 /* STRB and STRBT: 1 byte. */
11877 record_buf_mem[0] = 1;
11878 }
11879 else
11880 {
11881 /* STR and STRT: 4 bytes. */
11882 record_buf_mem[0] = 4;
11883 }
11884
11885 /* Handle bit P. */
11886 if (bit (arm_insn_r->arm_insn, 24))
11887 record_buf_mem[1] = tgt_mem_addr;
11888 else
11889 record_buf_mem[1] = (uint32_t) u_regval;
72508ac0 11890
72508ac0
PO
11891 arm_insn_r->mem_rec_count = 1;
11892
71e396f9
LM
11893 /* If wback is true, also save the base register, which is going to be
11894 written to. */
11895 if (wback)
11896 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
11897 }
11898
11899 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11900 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11901 return 0;
11902}
11903
11904/* Handling opcode 011 insns. */
11905
11906static int
4748a9be 11907arm_record_ld_st_reg_offset (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
11908{
11909 struct regcache *reg_cache = arm_insn_r->regcache;
11910
11911 uint32_t shift_imm = 0;
11912 uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
11913 uint32_t offset_12 = 0, tgt_mem_addr = 0;
11914 uint32_t record_buf[8], record_buf_mem[8];
11915
11916 LONGEST s_word;
11917 ULONGEST u_regval[2];
11918
c55978a6
YQ
11919 if (bit (arm_insn_r->arm_insn, 4))
11920 return arm_record_media (arm_insn_r);
11921
72508ac0
PO
11922 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11923 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11924
11925 /* Handle enhanced store insns and LDRD DSP insn,
11926 order begins according to addressing modes for store insns
11927 STRH insn. */
11928
11929 /* LDR or STR? */
11930 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11931 {
11932 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11933 /* LDR insn has a capability to do branching, if
dda83cd7
SM
11934 MOV LR, PC is preceded by LDR insn having Rn as R15
11935 in that case, it emulates branch and link insn, and hence we
11936 need to save CSPR and PC as well. */
72508ac0 11937 if (15 != reg_dest)
dda83cd7
SM
11938 {
11939 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11940 arm_insn_r->reg_rec_count = 1;
11941 }
72508ac0 11942 else
dda83cd7
SM
11943 {
11944 record_buf[0] = reg_dest;
11945 record_buf[1] = ARM_PS_REGNUM;
11946 arm_insn_r->reg_rec_count = 2;
11947 }
72508ac0
PO
11948 }
11949 else
11950 {
11951 if (! bits (arm_insn_r->arm_insn, 4, 11))
dda83cd7
SM
11952 {
11953 /* Store insn, register offset and register pre-indexed,
11954 register post-indexed. */
11955 /* Get Rm. */
11956 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11957 /* Get Rn. */
11958 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11959 regcache_raw_read_unsigned (reg_cache, reg_src1
11960 , &u_regval[0]);
11961 regcache_raw_read_unsigned (reg_cache, reg_src2
11962 , &u_regval[1]);
11963 if (15 == reg_src2)
11964 {
11965 /* If R15 was used as Rn, hence current PC+8. */
11966 /* Pre-indexed mode doesnt reach here ; illegal insn. */
11967 u_regval[0] = u_regval[0] + 8;
11968 }
11969 /* Calculate target store address, Rn +/- Rm, register offset. */
11970 /* U == 1. */
11971 if (bit (arm_insn_r->arm_insn, 23))
11972 {
11973 tgt_mem_addr = u_regval[0] + u_regval[1];
11974 }
11975 else
11976 {
11977 tgt_mem_addr = u_regval[1] - u_regval[0];
11978 }
11979
11980 switch (arm_insn_r->opcode)
11981 {
11982 /* STR. */
11983 case 8:
11984 case 12:
11985 /* STR. */
11986 case 9:
11987 case 13:
11988 /* STRT. */
11989 case 1:
11990 case 5:
11991 /* STR. */
11992 case 0:
11993 case 4:
11994 record_buf_mem[0] = 4;
11995 break;
11996
11997 /* STRB. */
11998 case 10:
11999 case 14:
12000 /* STRB. */
12001 case 11:
12002 case 15:
12003 /* STRBT. */
12004 case 3:
12005 case 7:
12006 /* STRB. */
12007 case 2:
12008 case 6:
12009 record_buf_mem[0] = 1;
12010 break;
12011
12012 default:
12013 gdb_assert_not_reached ("no decoding pattern found");
12014 break;
12015 }
12016 record_buf_mem[1] = tgt_mem_addr;
12017 arm_insn_r->mem_rec_count = 1;
12018
12019 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
12020 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
12021 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
12022 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
12023 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
12024 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
12025 )
12026 {
12027 /* Rn is going to be changed in pre-indexed mode and
12028 post-indexed mode as well. */
12029 record_buf[0] = reg_src2;
12030 arm_insn_r->reg_rec_count = 1;
12031 }
12032 }
72508ac0 12033 else
dda83cd7
SM
12034 {
12035 /* Store insn, scaled register offset; scaled pre-indexed. */
12036 offset_12 = bits (arm_insn_r->arm_insn, 5, 6);
12037 /* Get Rm. */
12038 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
12039 /* Get Rn. */
12040 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
12041 /* Get shift_imm. */
12042 shift_imm = bits (arm_insn_r->arm_insn, 7, 11);
12043 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
12044 regcache_raw_read_signed (reg_cache, reg_src1, &s_word);
12045 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
12046 /* Offset_12 used as shift. */
12047 switch (offset_12)
12048 {
12049 case 0:
12050 /* Offset_12 used as index. */
12051 offset_12 = u_regval[0] << shift_imm;
12052 break;
12053
12054 case 1:
12055 offset_12 = (!shift_imm)?0:u_regval[0] >> shift_imm;
12056 break;
12057
12058 case 2:
12059 if (!shift_imm)
12060 {
12061 if (bit (u_regval[0], 31))
12062 {
12063 offset_12 = 0xFFFFFFFF;
12064 }
12065 else
12066 {
12067 offset_12 = 0;
12068 }
12069 }
12070 else
12071 {
12072 /* This is arithmetic shift. */
12073 offset_12 = s_word >> shift_imm;
12074 }
12075 break;
12076
12077 case 3:
12078 if (!shift_imm)
12079 {
12080 regcache_raw_read_unsigned (reg_cache, ARM_PS_REGNUM,
12081 &u_regval[1]);
12082 /* Get C flag value and shift it by 31. */
12083 offset_12 = (((bit (u_regval[1], 29)) << 31) \
12084 | (u_regval[0]) >> 1);
12085 }
12086 else
12087 {
12088 offset_12 = (u_regval[0] >> shift_imm) \
12089 | (u_regval[0] <<
12090 (sizeof(uint32_t) - shift_imm));
12091 }
12092 break;
12093
12094 default:
12095 gdb_assert_not_reached ("no decoding pattern found");
12096 break;
12097 }
12098
12099 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
12100 /* bit U set. */
12101 if (bit (arm_insn_r->arm_insn, 23))
12102 {
12103 tgt_mem_addr = u_regval[1] + offset_12;
12104 }
12105 else
12106 {
12107 tgt_mem_addr = u_regval[1] - offset_12;
12108 }
12109
12110 switch (arm_insn_r->opcode)
12111 {
12112 /* STR. */
12113 case 8:
12114 case 12:
12115 /* STR. */
12116 case 9:
12117 case 13:
12118 /* STRT. */
12119 case 1:
12120 case 5:
12121 /* STR. */
12122 case 0:
12123 case 4:
12124 record_buf_mem[0] = 4;
12125 break;
12126
12127 /* STRB. */
12128 case 10:
12129 case 14:
12130 /* STRB. */
12131 case 11:
12132 case 15:
12133 /* STRBT. */
12134 case 3:
12135 case 7:
12136 /* STRB. */
12137 case 2:
12138 case 6:
12139 record_buf_mem[0] = 1;
12140 break;
12141
12142 default:
12143 gdb_assert_not_reached ("no decoding pattern found");
12144 break;
12145 }
12146 record_buf_mem[1] = tgt_mem_addr;
12147 arm_insn_r->mem_rec_count = 1;
12148
12149 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
12150 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
12151 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
12152 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
12153 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
12154 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
12155 )
12156 {
12157 /* Rn is going to be changed in register scaled pre-indexed
12158 mode,and scaled post indexed mode. */
12159 record_buf[0] = reg_src2;
12160 arm_insn_r->reg_rec_count = 1;
12161 }
12162 }
72508ac0
PO
12163 }
12164
12165 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12166 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12167 return 0;
12168}
12169
71e396f9 12170/* Handle ARM mode instructions with opcode 100. */
72508ac0
PO
12171
12172static int
4748a9be 12173arm_record_ld_st_multiple (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
12174{
12175 struct regcache *reg_cache = arm_insn_r->regcache;
71e396f9
LM
12176 uint32_t register_count = 0, register_bits;
12177 uint32_t reg_base, addr_mode;
72508ac0 12178 uint32_t record_buf[24], record_buf_mem[48];
71e396f9
LM
12179 uint32_t wback;
12180 ULONGEST u_regval;
72508ac0 12181
71e396f9
LM
12182 /* Fetch the list of registers. */
12183 register_bits = bits (arm_insn_r->arm_insn, 0, 15);
12184 arm_insn_r->reg_rec_count = 0;
12185
12186 /* Fetch the base register that contains the address we are loading data
12187 to. */
12188 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0 12189
71e396f9
LM
12190 /* Calculate wback. */
12191 wback = (bit (arm_insn_r->arm_insn, 21) == 1);
72508ac0
PO
12192
12193 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12194 {
71e396f9 12195 /* LDM/LDMIA/LDMFD, LDMDA/LDMFA, LDMDB and LDMIB. */
72508ac0 12196
71e396f9 12197 /* Find out which registers are going to be loaded from memory. */
72508ac0 12198 while (register_bits)
71e396f9
LM
12199 {
12200 if (register_bits & 0x00000001)
12201 record_buf[arm_insn_r->reg_rec_count++] = register_count;
12202 register_bits = register_bits >> 1;
12203 register_count++;
12204 }
72508ac0 12205
71e396f9
LM
12206
12207 /* If wback is true, also save the base register, which is going to be
12208 written to. */
12209 if (wback)
12210 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
12211
12212 /* Save the CPSR register. */
12213 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
72508ac0
PO
12214 }
12215 else
12216 {
71e396f9 12217 /* STM (STMIA, STMEA), STMDA (STMED), STMDB (STMFD) and STMIB (STMFA). */
72508ac0 12218
71e396f9
LM
12219 addr_mode = bits (arm_insn_r->arm_insn, 23, 24);
12220
12221 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
12222
12223 /* Find out how many registers are going to be stored to memory. */
72508ac0 12224 while (register_bits)
71e396f9
LM
12225 {
12226 if (register_bits & 0x00000001)
12227 register_count++;
12228 register_bits = register_bits >> 1;
12229 }
72508ac0
PO
12230
12231 switch (addr_mode)
71e396f9
LM
12232 {
12233 /* STMDA (STMED): Decrement after. */
12234 case 0:
12235 record_buf_mem[1] = (uint32_t) u_regval
f0452268 12236 - register_count * ARM_INT_REGISTER_SIZE + 4;
71e396f9
LM
12237 break;
12238 /* STM (STMIA, STMEA): Increment after. */
12239 case 1:
12240 record_buf_mem[1] = (uint32_t) u_regval;
12241 break;
12242 /* STMDB (STMFD): Decrement before. */
12243 case 2:
12244 record_buf_mem[1] = (uint32_t) u_regval
f0452268 12245 - register_count * ARM_INT_REGISTER_SIZE;
71e396f9
LM
12246 break;
12247 /* STMIB (STMFA): Increment before. */
12248 case 3:
f0452268 12249 record_buf_mem[1] = (uint32_t) u_regval + ARM_INT_REGISTER_SIZE;
71e396f9
LM
12250 break;
12251 default:
12252 gdb_assert_not_reached ("no decoding pattern found");
12253 break;
12254 }
72508ac0 12255
f0452268 12256 record_buf_mem[0] = register_count * ARM_INT_REGISTER_SIZE;
71e396f9
LM
12257 arm_insn_r->mem_rec_count = 1;
12258
12259 /* If wback is true, also save the base register, which is going to be
12260 written to. */
12261 if (wback)
12262 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
12263 }
12264
12265 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12266 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12267 return 0;
12268}
12269
12270/* Handling opcode 101 insns. */
12271
12272static int
4748a9be 12273arm_record_b_bl (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
12274{
12275 uint32_t record_buf[8];
12276
12277 /* Handle B, BL, BLX(1) insns. */
12278 /* B simply branches so we do nothing here. */
12279 /* Note: BLX(1) doesnt fall here but instead it falls into
12280 extension space. */
12281 if (bit (arm_insn_r->arm_insn, 24))
01add95b
SM
12282 {
12283 record_buf[0] = ARM_LR_REGNUM;
12284 arm_insn_r->reg_rec_count = 1;
12285 }
72508ac0
PO
12286
12287 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12288
12289 return 0;
12290}
12291
72508ac0 12292static int
4748a9be 12293arm_record_unsupported_insn (arm_insn_decode_record *arm_insn_r)
72508ac0 12294{
6cb06a8c
TT
12295 gdb_printf (gdb_stderr,
12296 _("Process record does not support instruction "
12297 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
12298 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
72508ac0
PO
12299
12300 return -1;
12301}
12302
5a578da5
OJ
12303/* Record handler for vector data transfer instructions. */
12304
12305static int
4748a9be 12306arm_record_vdata_transfer_insn (arm_insn_decode_record *arm_insn_r)
5a578da5
OJ
12307{
12308 uint32_t bits_a, bit_c, bit_l, reg_t, reg_v;
12309 uint32_t record_buf[4];
12310
5a578da5
OJ
12311 reg_t = bits (arm_insn_r->arm_insn, 12, 15);
12312 reg_v = bits (arm_insn_r->arm_insn, 21, 23);
12313 bits_a = bits (arm_insn_r->arm_insn, 21, 23);
12314 bit_l = bit (arm_insn_r->arm_insn, 20);
12315 bit_c = bit (arm_insn_r->arm_insn, 8);
12316
12317 /* Handle VMOV instruction. */
12318 if (bit_l && bit_c)
12319 {
12320 record_buf[0] = reg_t;
12321 arm_insn_r->reg_rec_count = 1;
12322 }
12323 else if (bit_l && !bit_c)
12324 {
12325 /* Handle VMOV instruction. */
12326 if (bits_a == 0x00)
dda83cd7 12327 {
f1771dce 12328 record_buf[0] = reg_t;
dda83cd7
SM
12329 arm_insn_r->reg_rec_count = 1;
12330 }
5a578da5
OJ
12331 /* Handle VMRS instruction. */
12332 else if (bits_a == 0x07)
dda83cd7
SM
12333 {
12334 if (reg_t == 15)
12335 reg_t = ARM_PS_REGNUM;
5a578da5 12336
dda83cd7
SM
12337 record_buf[0] = reg_t;
12338 arm_insn_r->reg_rec_count = 1;
12339 }
5a578da5
OJ
12340 }
12341 else if (!bit_l && !bit_c)
12342 {
12343 /* Handle VMOV instruction. */
12344 if (bits_a == 0x00)
dda83cd7 12345 {
f1771dce 12346 record_buf[0] = ARM_D0_REGNUM + reg_v;
5a578da5 12347
dda83cd7
SM
12348 arm_insn_r->reg_rec_count = 1;
12349 }
5a578da5
OJ
12350 /* Handle VMSR instruction. */
12351 else if (bits_a == 0x07)
dda83cd7
SM
12352 {
12353 record_buf[0] = ARM_FPSCR_REGNUM;
12354 arm_insn_r->reg_rec_count = 1;
12355 }
5a578da5
OJ
12356 }
12357 else if (!bit_l && bit_c)
12358 {
12359 /* Handle VMOV instruction. */
12360 if (!(bits_a & 0x04))
dda83cd7
SM
12361 {
12362 record_buf[0] = (reg_v | (bit (arm_insn_r->arm_insn, 7) << 4))
12363 + ARM_D0_REGNUM;
12364 arm_insn_r->reg_rec_count = 1;
12365 }
5a578da5
OJ
12366 /* Handle VDUP instruction. */
12367 else
dda83cd7
SM
12368 {
12369 if (bit (arm_insn_r->arm_insn, 21))
12370 {
12371 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
12372 record_buf[0] = reg_v + ARM_D0_REGNUM;
12373 record_buf[1] = reg_v + ARM_D0_REGNUM + 1;
12374 arm_insn_r->reg_rec_count = 2;
12375 }
12376 else
12377 {
12378 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
12379 record_buf[0] = reg_v + ARM_D0_REGNUM;
12380 arm_insn_r->reg_rec_count = 1;
12381 }
12382 }
12383 }
12384
12385 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12386 return 0;
12387}
12388
f20f80dd
OJ
12389/* Record handler for extension register load/store instructions. */
12390
12391static int
4748a9be 12392arm_record_exreg_ld_st_insn (arm_insn_decode_record *arm_insn_r)
f20f80dd
OJ
12393{
12394 uint32_t opcode, single_reg;
12395 uint8_t op_vldm_vstm;
12396 uint32_t record_buf[8], record_buf_mem[128];
12397 ULONGEST u_regval = 0;
12398
12399 struct regcache *reg_cache = arm_insn_r->regcache;
f20f80dd
OJ
12400
12401 opcode = bits (arm_insn_r->arm_insn, 20, 24);
9fde51ed 12402 single_reg = !bit (arm_insn_r->arm_insn, 8);
f20f80dd
OJ
12403 op_vldm_vstm = opcode & 0x1b;
12404
12405 /* Handle VMOV instructions. */
12406 if ((opcode & 0x1e) == 0x04)
12407 {
9fde51ed 12408 if (bit (arm_insn_r->arm_insn, 20)) /* to_arm_registers bit 20? */
01e57735
YQ
12409 {
12410 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12411 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
12412 arm_insn_r->reg_rec_count = 2;
12413 }
f20f80dd 12414 else
01e57735 12415 {
9fde51ed
YQ
12416 uint8_t reg_m = bits (arm_insn_r->arm_insn, 0, 3);
12417 uint8_t bit_m = bit (arm_insn_r->arm_insn, 5);
f20f80dd 12418
9fde51ed 12419 if (single_reg)
01e57735 12420 {
9fde51ed
YQ
12421 /* The first S register number m is REG_M:M (M is bit 5),
12422 the corresponding D register number is REG_M:M / 2, which
12423 is REG_M. */
12424 record_buf[arm_insn_r->reg_rec_count++] = ARM_D0_REGNUM + reg_m;
12425 /* The second S register number is REG_M:M + 1, the
12426 corresponding D register number is (REG_M:M + 1) / 2.
12427 IOW, if bit M is 1, the first and second S registers
12428 are mapped to different D registers, otherwise, they are
12429 in the same D register. */
12430 if (bit_m)
12431 {
12432 record_buf[arm_insn_r->reg_rec_count++]
12433 = ARM_D0_REGNUM + reg_m + 1;
12434 }
01e57735
YQ
12435 }
12436 else
12437 {
9fde51ed 12438 record_buf[0] = ((bit_m << 4) + reg_m + ARM_D0_REGNUM);
01e57735
YQ
12439 arm_insn_r->reg_rec_count = 1;
12440 }
12441 }
f20f80dd
OJ
12442 }
12443 /* Handle VSTM and VPUSH instructions. */
12444 else if (op_vldm_vstm == 0x08 || op_vldm_vstm == 0x0a
01e57735 12445 || op_vldm_vstm == 0x12)
f20f80dd
OJ
12446 {
12447 uint32_t start_address, reg_rn, imm_off32, imm_off8, memory_count;
12448 uint32_t memory_index = 0;
12449
12450 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
12451 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12452 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
9fde51ed 12453 imm_off32 = imm_off8 << 2;
f20f80dd
OJ
12454 memory_count = imm_off8;
12455
12456 if (bit (arm_insn_r->arm_insn, 23))
01e57735 12457 start_address = u_regval;
f20f80dd 12458 else
01e57735 12459 start_address = u_regval - imm_off32;
f20f80dd
OJ
12460
12461 if (bit (arm_insn_r->arm_insn, 21))
01e57735
YQ
12462 {
12463 record_buf[0] = reg_rn;
12464 arm_insn_r->reg_rec_count = 1;
12465 }
f20f80dd
OJ
12466
12467 while (memory_count > 0)
01e57735 12468 {
9fde51ed 12469 if (single_reg)
01e57735 12470 {
9fde51ed
YQ
12471 record_buf_mem[memory_index] = 4;
12472 record_buf_mem[memory_index + 1] = start_address;
01e57735
YQ
12473 start_address = start_address + 4;
12474 memory_index = memory_index + 2;
12475 }
12476 else
12477 {
9fde51ed
YQ
12478 record_buf_mem[memory_index] = 4;
12479 record_buf_mem[memory_index + 1] = start_address;
12480 record_buf_mem[memory_index + 2] = 4;
12481 record_buf_mem[memory_index + 3] = start_address + 4;
01e57735
YQ
12482 start_address = start_address + 8;
12483 memory_index = memory_index + 4;
12484 }
12485 memory_count--;
12486 }
f20f80dd
OJ
12487 arm_insn_r->mem_rec_count = (memory_index >> 1);
12488 }
12489 /* Handle VLDM instructions. */
12490 else if (op_vldm_vstm == 0x09 || op_vldm_vstm == 0x0b
01e57735 12491 || op_vldm_vstm == 0x13)
f20f80dd
OJ
12492 {
12493 uint32_t reg_count, reg_vd;
12494 uint32_t reg_index = 0;
9fde51ed 12495 uint32_t bit_d = bit (arm_insn_r->arm_insn, 22);
f20f80dd
OJ
12496
12497 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12498 reg_count = bits (arm_insn_r->arm_insn, 0, 7);
12499
9fde51ed
YQ
12500 /* REG_VD is the first D register number. If the instruction
12501 loads memory to S registers (SINGLE_REG is TRUE), the register
12502 number is (REG_VD << 1 | bit D), so the corresponding D
12503 register number is (REG_VD << 1 | bit D) / 2 = REG_VD. */
12504 if (!single_reg)
12505 reg_vd = reg_vd | (bit_d << 4);
f20f80dd 12506
9fde51ed 12507 if (bit (arm_insn_r->arm_insn, 21) /* write back */)
01e57735 12508 record_buf[reg_index++] = bits (arm_insn_r->arm_insn, 16, 19);
f20f80dd 12509
9fde51ed
YQ
12510 /* If the instruction loads memory to D register, REG_COUNT should
12511 be divided by 2, according to the ARM Architecture Reference
12512 Manual. If the instruction loads memory to S register, divide by
12513 2 as well because two S registers are mapped to D register. */
12514 reg_count = reg_count / 2;
12515 if (single_reg && bit_d)
01e57735 12516 {
9fde51ed
YQ
12517 /* Increase the register count if S register list starts from
12518 an odd number (bit d is one). */
12519 reg_count++;
12520 }
f20f80dd 12521
9fde51ed
YQ
12522 while (reg_count > 0)
12523 {
12524 record_buf[reg_index++] = ARM_D0_REGNUM + reg_vd + reg_count - 1;
01e57735
YQ
12525 reg_count--;
12526 }
f20f80dd
OJ
12527 arm_insn_r->reg_rec_count = reg_index;
12528 }
12529 /* VSTR Vector store register. */
12530 else if ((opcode & 0x13) == 0x10)
12531 {
bec2ab5a 12532 uint32_t start_address, reg_rn, imm_off32, imm_off8;
f20f80dd
OJ
12533 uint32_t memory_index = 0;
12534
12535 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
12536 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12537 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
9fde51ed 12538 imm_off32 = imm_off8 << 2;
f20f80dd
OJ
12539
12540 if (bit (arm_insn_r->arm_insn, 23))
01e57735 12541 start_address = u_regval + imm_off32;
f20f80dd 12542 else
01e57735 12543 start_address = u_regval - imm_off32;
f20f80dd
OJ
12544
12545 if (single_reg)
01e57735 12546 {
9fde51ed
YQ
12547 record_buf_mem[memory_index] = 4;
12548 record_buf_mem[memory_index + 1] = start_address;
01e57735
YQ
12549 arm_insn_r->mem_rec_count = 1;
12550 }
f20f80dd 12551 else
01e57735 12552 {
9fde51ed
YQ
12553 record_buf_mem[memory_index] = 4;
12554 record_buf_mem[memory_index + 1] = start_address;
12555 record_buf_mem[memory_index + 2] = 4;
12556 record_buf_mem[memory_index + 3] = start_address + 4;
01e57735
YQ
12557 arm_insn_r->mem_rec_count = 2;
12558 }
f20f80dd
OJ
12559 }
12560 /* VLDR Vector load register. */
12561 else if ((opcode & 0x13) == 0x11)
12562 {
12563 uint32_t reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12564
12565 if (!single_reg)
01e57735
YQ
12566 {
12567 reg_vd = reg_vd | (bit (arm_insn_r->arm_insn, 22) << 4);
12568 record_buf[0] = ARM_D0_REGNUM + reg_vd;
12569 }
f20f80dd 12570 else
01e57735
YQ
12571 {
12572 reg_vd = (reg_vd << 1) | bit (arm_insn_r->arm_insn, 22);
9fde51ed
YQ
12573 /* Record register D rather than pseudo register S. */
12574 record_buf[0] = ARM_D0_REGNUM + reg_vd / 2;
01e57735 12575 }
f20f80dd
OJ
12576 arm_insn_r->reg_rec_count = 1;
12577 }
12578
12579 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12580 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12581 return 0;
12582}
12583
851f26ae
OJ
12584/* Record handler for arm/thumb mode VFP data processing instructions. */
12585
12586static int
4748a9be 12587arm_record_vfp_data_proc_insn (arm_insn_decode_record *arm_insn_r)
851f26ae
OJ
12588{
12589 uint32_t opc1, opc2, opc3, dp_op_sz, bit_d, reg_vd;
12590 uint32_t record_buf[4];
12591 enum insn_types {INSN_T0, INSN_T1, INSN_T2, INSN_T3, INSN_INV};
12592 enum insn_types curr_insn_type = INSN_INV;
12593
12594 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12595 opc1 = bits (arm_insn_r->arm_insn, 20, 23);
12596 opc2 = bits (arm_insn_r->arm_insn, 16, 19);
12597 opc3 = bits (arm_insn_r->arm_insn, 6, 7);
12598 dp_op_sz = bit (arm_insn_r->arm_insn, 8);
12599 bit_d = bit (arm_insn_r->arm_insn, 22);
ce887586
TT
12600 /* Mask off the "D" bit. */
12601 opc1 = opc1 & ~0x04;
851f26ae
OJ
12602
12603 /* Handle VMLA, VMLS. */
12604 if (opc1 == 0x00)
12605 {
12606 if (bit (arm_insn_r->arm_insn, 10))
dda83cd7
SM
12607 {
12608 if (bit (arm_insn_r->arm_insn, 6))
12609 curr_insn_type = INSN_T0;
12610 else
12611 curr_insn_type = INSN_T1;
12612 }
851f26ae 12613 else
dda83cd7
SM
12614 {
12615 if (dp_op_sz)
12616 curr_insn_type = INSN_T1;
12617 else
12618 curr_insn_type = INSN_T2;
12619 }
851f26ae
OJ
12620 }
12621 /* Handle VNMLA, VNMLS, VNMUL. */
12622 else if (opc1 == 0x01)
12623 {
12624 if (dp_op_sz)
dda83cd7 12625 curr_insn_type = INSN_T1;
851f26ae 12626 else
dda83cd7 12627 curr_insn_type = INSN_T2;
851f26ae
OJ
12628 }
12629 /* Handle VMUL. */
12630 else if (opc1 == 0x02 && !(opc3 & 0x01))
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 VADD, VSUB. */
12648 else if (opc1 == 0x03)
12649 {
12650 if (!bit (arm_insn_r->arm_insn, 9))
dda83cd7
SM
12651 {
12652 if (bit (arm_insn_r->arm_insn, 6))
12653 curr_insn_type = INSN_T0;
12654 else
12655 curr_insn_type = INSN_T1;
12656 }
851f26ae 12657 else
dda83cd7
SM
12658 {
12659 if (dp_op_sz)
12660 curr_insn_type = INSN_T1;
12661 else
12662 curr_insn_type = INSN_T2;
12663 }
851f26ae
OJ
12664 }
12665 /* Handle VDIV. */
ce887586 12666 else if (opc1 == 0x08)
851f26ae
OJ
12667 {
12668 if (dp_op_sz)
dda83cd7 12669 curr_insn_type = INSN_T1;
851f26ae 12670 else
dda83cd7 12671 curr_insn_type = INSN_T2;
851f26ae
OJ
12672 }
12673 /* Handle all other vfp data processing instructions. */
12674 else if (opc1 == 0x0b)
12675 {
12676 /* Handle VMOV. */
12677 if (!(opc3 & 0x01) || (opc2 == 0x00 && opc3 == 0x01))
dda83cd7
SM
12678 {
12679 if (bit (arm_insn_r->arm_insn, 4))
12680 {
12681 if (bit (arm_insn_r->arm_insn, 6))
12682 curr_insn_type = INSN_T0;
12683 else
12684 curr_insn_type = INSN_T1;
12685 }
12686 else
12687 {
12688 if (dp_op_sz)
12689 curr_insn_type = INSN_T1;
12690 else
12691 curr_insn_type = INSN_T2;
12692 }
12693 }
851f26ae
OJ
12694 /* Handle VNEG and VABS. */
12695 else if ((opc2 == 0x01 && opc3 == 0x01)
dda83cd7
SM
12696 || (opc2 == 0x00 && opc3 == 0x03))
12697 {
12698 if (!bit (arm_insn_r->arm_insn, 11))
12699 {
12700 if (bit (arm_insn_r->arm_insn, 6))
12701 curr_insn_type = INSN_T0;
12702 else
12703 curr_insn_type = INSN_T1;
12704 }
12705 else
12706 {
12707 if (dp_op_sz)
12708 curr_insn_type = INSN_T1;
12709 else
12710 curr_insn_type = INSN_T2;
12711 }
12712 }
851f26ae
OJ
12713 /* Handle VSQRT. */
12714 else if (opc2 == 0x01 && opc3 == 0x03)
dda83cd7
SM
12715 {
12716 if (dp_op_sz)
12717 curr_insn_type = INSN_T1;
12718 else
12719 curr_insn_type = INSN_T2;
12720 }
851f26ae
OJ
12721 /* Handle VCVT. */
12722 else if (opc2 == 0x07 && opc3 == 0x03)
dda83cd7
SM
12723 {
12724 if (!dp_op_sz)
12725 curr_insn_type = INSN_T1;
12726 else
12727 curr_insn_type = INSN_T2;
12728 }
851f26ae 12729 else if (opc3 & 0x01)
dda83cd7
SM
12730 {
12731 /* Handle VCVT. */
12732 if ((opc2 == 0x08) || (opc2 & 0x0e) == 0x0c)
12733 {
12734 if (!bit (arm_insn_r->arm_insn, 18))
12735 curr_insn_type = INSN_T2;
12736 else
12737 {
12738 if (dp_op_sz)
12739 curr_insn_type = INSN_T1;
12740 else
12741 curr_insn_type = INSN_T2;
12742 }
12743 }
12744 /* Handle VCVT. */
12745 else if ((opc2 & 0x0e) == 0x0a || (opc2 & 0x0e) == 0x0e)
12746 {
12747 if (dp_op_sz)
12748 curr_insn_type = INSN_T1;
12749 else
12750 curr_insn_type = INSN_T2;
12751 }
12752 /* Handle VCVTB, VCVTT. */
12753 else if ((opc2 & 0x0e) == 0x02)
12754 curr_insn_type = INSN_T2;
12755 /* Handle VCMP, VCMPE. */
12756 else if ((opc2 & 0x0e) == 0x04)
12757 curr_insn_type = INSN_T3;
12758 }
851f26ae
OJ
12759 }
12760
12761 switch (curr_insn_type)
12762 {
12763 case INSN_T0:
dda83cd7
SM
12764 reg_vd = reg_vd | (bit_d << 4);
12765 record_buf[0] = reg_vd + ARM_D0_REGNUM;
12766 record_buf[1] = reg_vd + ARM_D0_REGNUM + 1;
12767 arm_insn_r->reg_rec_count = 2;
12768 break;
851f26ae
OJ
12769
12770 case INSN_T1:
dda83cd7
SM
12771 reg_vd = reg_vd | (bit_d << 4);
12772 record_buf[0] = reg_vd + ARM_D0_REGNUM;
12773 arm_insn_r->reg_rec_count = 1;
12774 break;
851f26ae
OJ
12775
12776 case INSN_T2:
dda83cd7
SM
12777 reg_vd = (reg_vd << 1) | bit_d;
12778 record_buf[0] = reg_vd + ARM_D0_REGNUM;
12779 arm_insn_r->reg_rec_count = 1;
12780 break;
851f26ae
OJ
12781
12782 case INSN_T3:
dda83cd7
SM
12783 record_buf[0] = ARM_FPSCR_REGNUM;
12784 arm_insn_r->reg_rec_count = 1;
12785 break;
851f26ae
OJ
12786
12787 default:
dda83cd7
SM
12788 gdb_assert_not_reached ("no decoding pattern found");
12789 break;
851f26ae
OJ
12790 }
12791
12792 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12793 return 0;
12794}
12795
60cc5e93
OJ
12796/* Handling opcode 110 insns. */
12797
12798static int
4748a9be 12799arm_record_asimd_vfp_coproc (arm_insn_decode_record *arm_insn_r)
60cc5e93 12800{
bec2ab5a 12801 uint32_t op1, op1_ebit, coproc;
60cc5e93
OJ
12802
12803 coproc = bits (arm_insn_r->arm_insn, 8, 11);
12804 op1 = bits (arm_insn_r->arm_insn, 20, 25);
12805 op1_ebit = bit (arm_insn_r->arm_insn, 20);
12806
12807 if ((coproc & 0x0e) == 0x0a)
12808 {
12809 /* Handle extension register ld/st instructions. */
12810 if (!(op1 & 0x20))
dda83cd7 12811 return arm_record_exreg_ld_st_insn (arm_insn_r);
60cc5e93
OJ
12812
12813 /* 64-bit transfers between arm core and extension registers. */
12814 if ((op1 & 0x3e) == 0x04)
dda83cd7 12815 return arm_record_exreg_ld_st_insn (arm_insn_r);
60cc5e93
OJ
12816 }
12817 else
12818 {
12819 /* Handle coprocessor ld/st instructions. */
12820 if (!(op1 & 0x3a))
dda83cd7
SM
12821 {
12822 /* Store. */
12823 if (!op1_ebit)
12824 return arm_record_unsupported_insn (arm_insn_r);
12825 else
12826 /* Load. */
12827 return arm_record_unsupported_insn (arm_insn_r);
12828 }
60cc5e93
OJ
12829
12830 /* Move to coprocessor from two arm core registers. */
12831 if (op1 == 0x4)
dda83cd7 12832 return arm_record_unsupported_insn (arm_insn_r);
60cc5e93
OJ
12833
12834 /* Move to two arm core registers from coprocessor. */
12835 if (op1 == 0x5)
dda83cd7
SM
12836 {
12837 uint32_t reg_t[2];
60cc5e93 12838
dda83cd7
SM
12839 reg_t[0] = bits (arm_insn_r->arm_insn, 12, 15);
12840 reg_t[1] = bits (arm_insn_r->arm_insn, 16, 19);
12841 arm_insn_r->reg_rec_count = 2;
60cc5e93 12842
dda83cd7
SM
12843 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, reg_t);
12844 return 0;
60cc5e93
OJ
12845 }
12846 }
12847 return arm_record_unsupported_insn (arm_insn_r);
12848}
12849
72508ac0
PO
12850/* Handling opcode 111 insns. */
12851
12852static int
4748a9be 12853arm_record_coproc_data_proc (arm_insn_decode_record *arm_insn_r)
72508ac0 12854{
2d9e6acb 12855 uint32_t op, op1_ebit, coproc, bits_24_25;
345bd07c 12856 arm_gdbarch_tdep *tdep
08106042 12857 = gdbarch_tdep<arm_gdbarch_tdep> (arm_insn_r->gdbarch);
72508ac0 12858 struct regcache *reg_cache = arm_insn_r->regcache;
72508ac0
PO
12859
12860 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 24, 27);
60cc5e93 12861 coproc = bits (arm_insn_r->arm_insn, 8, 11);
60cc5e93
OJ
12862 op1_ebit = bit (arm_insn_r->arm_insn, 20);
12863 op = bit (arm_insn_r->arm_insn, 4);
2d9e6acb 12864 bits_24_25 = bits (arm_insn_r->arm_insn, 24, 25);
97dfe206
OJ
12865
12866 /* Handle arm SWI/SVC system call instructions. */
2d9e6acb 12867 if (bits_24_25 == 0x3)
97dfe206
OJ
12868 {
12869 if (tdep->arm_syscall_record != NULL)
dda83cd7
SM
12870 {
12871 ULONGEST svc_operand, svc_number;
97dfe206 12872
dda83cd7 12873 svc_operand = (0x00ffffff & arm_insn_r->arm_insn);
97dfe206 12874
dda83cd7
SM
12875 if (svc_operand) /* OABI. */
12876 svc_number = svc_operand - 0x900000;
12877 else /* EABI. */
12878 regcache_raw_read_unsigned (reg_cache, 7, &svc_number);
97dfe206 12879
dda83cd7
SM
12880 return tdep->arm_syscall_record (reg_cache, svc_number);
12881 }
97dfe206 12882 else
dda83cd7 12883 {
6cb06a8c 12884 gdb_printf (gdb_stderr, _("no syscall record support\n"));
dda83cd7
SM
12885 return -1;
12886 }
97dfe206 12887 }
2d9e6acb 12888 else if (bits_24_25 == 0x02)
60cc5e93 12889 {
2d9e6acb
YQ
12890 if (op)
12891 {
12892 if ((coproc & 0x0e) == 0x0a)
12893 {
12894 /* 8, 16, and 32-bit transfer */
12895 return arm_record_vdata_transfer_insn (arm_insn_r);
12896 }
12897 else
12898 {
12899 if (op1_ebit)
12900 {
12901 /* MRC, MRC2 */
12902 uint32_t record_buf[1];
12903
12904 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12905 if (record_buf[0] == 15)
12906 record_buf[0] = ARM_PS_REGNUM;
60cc5e93 12907
2d9e6acb
YQ
12908 arm_insn_r->reg_rec_count = 1;
12909 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count,
12910 record_buf);
12911 return 0;
12912 }
12913 else
12914 {
12915 /* MCR, MCR2 */
12916 return -1;
12917 }
12918 }
12919 }
12920 else
12921 {
12922 if ((coproc & 0x0e) == 0x0a)
12923 {
12924 /* VFP data-processing instructions. */
12925 return arm_record_vfp_data_proc_insn (arm_insn_r);
12926 }
12927 else
12928 {
12929 /* CDP, CDP2 */
12930 return -1;
12931 }
12932 }
60cc5e93 12933 }
97dfe206
OJ
12934 else
12935 {
2d9e6acb 12936 unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 25);
60cc5e93 12937
2d9e6acb
YQ
12938 if (op1 == 5)
12939 {
12940 if ((coproc & 0x0e) != 0x0a)
12941 {
12942 /* MRRC, MRRC2 */
12943 return -1;
12944 }
12945 }
12946 else if (op1 == 4 || op1 == 5)
12947 {
12948 if ((coproc & 0x0e) == 0x0a)
12949 {
12950 /* 64-bit transfers between ARM core and extension */
12951 return -1;
12952 }
12953 else if (op1 == 4)
12954 {
12955 /* MCRR, MCRR2 */
12956 return -1;
12957 }
12958 }
12959 else if (op1 == 0 || op1 == 1)
12960 {
12961 /* UNDEFINED */
12962 return -1;
12963 }
12964 else
12965 {
12966 if ((coproc & 0x0e) == 0x0a)
12967 {
12968 /* Extension register load/store */
12969 }
12970 else
12971 {
12972 /* STC, STC2, LDC, LDC2 */
12973 }
12974 return -1;
12975 }
97dfe206 12976 }
72508ac0 12977
2d9e6acb 12978 return -1;
72508ac0
PO
12979}
12980
12981/* Handling opcode 000 insns. */
12982
12983static int
4748a9be 12984thumb_record_shift_add_sub (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
12985{
12986 uint32_t record_buf[8];
12987 uint32_t reg_src1 = 0;
12988
12989 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12990
12991 record_buf[0] = ARM_PS_REGNUM;
12992 record_buf[1] = reg_src1;
12993 thumb_insn_r->reg_rec_count = 2;
12994
12995 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12996
12997 return 0;
12998}
12999
13000
13001/* Handling opcode 001 insns. */
13002
13003static int
4748a9be 13004thumb_record_add_sub_cmp_mov (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13005{
13006 uint32_t record_buf[8];
13007 uint32_t reg_src1 = 0;
13008
13009 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13010
13011 record_buf[0] = ARM_PS_REGNUM;
13012 record_buf[1] = reg_src1;
13013 thumb_insn_r->reg_rec_count = 2;
13014
13015 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13016
13017 return 0;
13018}
13019
13020/* Handling opcode 010 insns. */
13021
13022static int
4748a9be 13023thumb_record_ld_st_reg_offset (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13024{
13025 struct regcache *reg_cache = thumb_insn_r->regcache;
13026 uint32_t record_buf[8], record_buf_mem[8];
13027
13028 uint32_t reg_src1 = 0, reg_src2 = 0;
13029 uint32_t opcode1 = 0, opcode2 = 0, opcode3 = 0;
13030
13031 ULONGEST u_regval[2] = {0};
13032
13033 opcode1 = bits (thumb_insn_r->arm_insn, 10, 12);
13034
13035 if (bit (thumb_insn_r->arm_insn, 12))
13036 {
13037 /* Handle load/store register offset. */
b121eeb9
YQ
13038 uint32_t opB = bits (thumb_insn_r->arm_insn, 9, 11);
13039
b020ff80 13040 if (in_inclusive_range (opB, 4U, 7U))
dda83cd7
SM
13041 {
13042 /* LDR(2), LDRB(2) , LDRH(2), LDRSB, LDRSH. */
13043 reg_src1 = bits (thumb_insn_r->arm_insn,0, 2);
13044 record_buf[0] = reg_src1;
13045 thumb_insn_r->reg_rec_count = 1;
13046 }
b020ff80 13047 else if (in_inclusive_range (opB, 0U, 2U))
dda83cd7
SM
13048 {
13049 /* STR(2), STRB(2), STRH(2) . */
13050 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
13051 reg_src2 = bits (thumb_insn_r->arm_insn, 6, 8);
13052 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
13053 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
13054 if (0 == opB)
13055 record_buf_mem[0] = 4; /* STR (2). */
13056 else if (2 == opB)
13057 record_buf_mem[0] = 1; /* STRB (2). */
13058 else if (1 == opB)
13059 record_buf_mem[0] = 2; /* STRH (2). */
13060 record_buf_mem[1] = u_regval[0] + u_regval[1];
13061 thumb_insn_r->mem_rec_count = 1;
13062 }
72508ac0
PO
13063 }
13064 else if (bit (thumb_insn_r->arm_insn, 11))
13065 {
13066 /* Handle load from literal pool. */
13067 /* LDR(3). */
13068 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13069 record_buf[0] = reg_src1;
13070 thumb_insn_r->reg_rec_count = 1;
13071 }
13072 else if (opcode1)
13073 {
b121eeb9 13074 /* Special data instructions and branch and exchange */
72508ac0
PO
13075 opcode2 = bits (thumb_insn_r->arm_insn, 8, 9);
13076 opcode3 = bits (thumb_insn_r->arm_insn, 0, 2);
13077 if ((3 == opcode2) && (!opcode3))
dda83cd7
SM
13078 {
13079 /* Branch with exchange. */
13080 record_buf[0] = ARM_PS_REGNUM;
13081 thumb_insn_r->reg_rec_count = 1;
13082 }
72508ac0 13083 else
dda83cd7 13084 {
1f33efec
YQ
13085 /* Format 8; special data processing insns. */
13086 record_buf[0] = ARM_PS_REGNUM;
13087 record_buf[1] = (bit (thumb_insn_r->arm_insn, 7) << 3
13088 | bits (thumb_insn_r->arm_insn, 0, 2));
dda83cd7
SM
13089 thumb_insn_r->reg_rec_count = 2;
13090 }
72508ac0
PO
13091 }
13092 else
13093 {
13094 /* Format 5; data processing insns. */
13095 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13096 if (bit (thumb_insn_r->arm_insn, 7))
dda83cd7
SM
13097 {
13098 reg_src1 = reg_src1 + 8;
13099 }
72508ac0
PO
13100 record_buf[0] = ARM_PS_REGNUM;
13101 record_buf[1] = reg_src1;
13102 thumb_insn_r->reg_rec_count = 2;
13103 }
13104
13105 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13106 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13107 record_buf_mem);
72508ac0
PO
13108
13109 return 0;
13110}
13111
13112/* Handling opcode 001 insns. */
13113
13114static int
4748a9be 13115thumb_record_ld_st_imm_offset (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13116{
13117 struct regcache *reg_cache = thumb_insn_r->regcache;
13118 uint32_t record_buf[8], record_buf_mem[8];
13119
13120 uint32_t reg_src1 = 0;
13121 uint32_t opcode = 0, immed_5 = 0;
13122
13123 ULONGEST u_regval = 0;
13124
13125 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
13126
13127 if (opcode)
13128 {
13129 /* LDR(1). */
13130 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13131 record_buf[0] = reg_src1;
13132 thumb_insn_r->reg_rec_count = 1;
13133 }
13134 else
13135 {
13136 /* STR(1). */
13137 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
13138 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
13139 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
13140 record_buf_mem[0] = 4;
13141 record_buf_mem[1] = u_regval + (immed_5 * 4);
13142 thumb_insn_r->mem_rec_count = 1;
13143 }
13144
13145 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13146 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13147 record_buf_mem);
72508ac0
PO
13148
13149 return 0;
13150}
13151
13152/* Handling opcode 100 insns. */
13153
13154static int
4748a9be 13155thumb_record_ld_st_stack (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13156{
13157 struct regcache *reg_cache = thumb_insn_r->regcache;
13158 uint32_t record_buf[8], record_buf_mem[8];
13159
13160 uint32_t reg_src1 = 0;
13161 uint32_t opcode = 0, immed_8 = 0, immed_5 = 0;
13162
13163 ULONGEST u_regval = 0;
13164
13165 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
13166
13167 if (3 == opcode)
13168 {
13169 /* LDR(4). */
13170 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13171 record_buf[0] = reg_src1;
13172 thumb_insn_r->reg_rec_count = 1;
13173 }
13174 else if (1 == opcode)
13175 {
13176 /* LDRH(1). */
13177 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13178 record_buf[0] = reg_src1;
13179 thumb_insn_r->reg_rec_count = 1;
13180 }
13181 else if (2 == opcode)
13182 {
13183 /* STR(3). */
13184 immed_8 = bits (thumb_insn_r->arm_insn, 0, 7);
13185 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
13186 record_buf_mem[0] = 4;
13187 record_buf_mem[1] = u_regval + (immed_8 * 4);
13188 thumb_insn_r->mem_rec_count = 1;
13189 }
13190 else if (0 == opcode)
13191 {
13192 /* STRH(1). */
13193 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
13194 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
13195 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
13196 record_buf_mem[0] = 2;
13197 record_buf_mem[1] = u_regval + (immed_5 * 2);
13198 thumb_insn_r->mem_rec_count = 1;
13199 }
13200
13201 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13202 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13203 record_buf_mem);
72508ac0
PO
13204
13205 return 0;
13206}
13207
13208/* Handling opcode 101 insns. */
13209
13210static int
4748a9be 13211thumb_record_misc (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13212{
13213 struct regcache *reg_cache = thumb_insn_r->regcache;
13214
b121eeb9 13215 uint32_t opcode = 0;
72508ac0 13216 uint32_t register_bits = 0, register_count = 0;
bec2ab5a 13217 uint32_t index = 0, start_address = 0;
72508ac0
PO
13218 uint32_t record_buf[24], record_buf_mem[48];
13219 uint32_t reg_src1;
13220
13221 ULONGEST u_regval = 0;
13222
13223 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
72508ac0 13224
b121eeb9 13225 if (opcode == 0 || opcode == 1)
72508ac0 13226 {
b121eeb9
YQ
13227 /* ADR and ADD (SP plus immediate) */
13228
72508ac0
PO
13229 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13230 record_buf[0] = reg_src1;
13231 thumb_insn_r->reg_rec_count = 1;
13232 }
b121eeb9 13233 else
72508ac0 13234 {
b121eeb9
YQ
13235 /* Miscellaneous 16-bit instructions */
13236 uint32_t opcode2 = bits (thumb_insn_r->arm_insn, 8, 11);
13237
13238 switch (opcode2)
13239 {
13240 case 6:
13241 /* SETEND and CPS */
13242 break;
13243 case 0:
13244 /* ADD/SUB (SP plus immediate) */
13245 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13246 record_buf[0] = ARM_SP_REGNUM;
13247 thumb_insn_r->reg_rec_count = 1;
13248 break;
13249 case 1: /* fall through */
13250 case 3: /* fall through */
13251 case 9: /* fall through */
13252 case 11:
13253 /* CBNZ, CBZ */
b121eeb9
YQ
13254 break;
13255 case 2:
13256 /* SXTH, SXTB, UXTH, UXTB */
13257 record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
13258 thumb_insn_r->reg_rec_count = 1;
13259 break;
13260 case 4: /* fall through */
13261 case 5:
13262 /* PUSH. */
13263 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13264 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
13265 while (register_bits)
13266 {
13267 if (register_bits & 0x00000001)
13268 register_count++;
13269 register_bits = register_bits >> 1;
13270 }
13271 start_address = u_regval - \
13272 (4 * (bit (thumb_insn_r->arm_insn, 8) + register_count));
13273 thumb_insn_r->mem_rec_count = register_count;
13274 while (register_count)
13275 {
13276 record_buf_mem[(register_count * 2) - 1] = start_address;
13277 record_buf_mem[(register_count * 2) - 2] = 4;
13278 start_address = start_address + 4;
13279 register_count--;
13280 }
13281 record_buf[0] = ARM_SP_REGNUM;
13282 thumb_insn_r->reg_rec_count = 1;
13283 break;
13284 case 10:
13285 /* REV, REV16, REVSH */
ba14f379
YQ
13286 record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
13287 thumb_insn_r->reg_rec_count = 1;
b121eeb9
YQ
13288 break;
13289 case 12: /* fall through */
13290 case 13:
13291 /* POP. */
13292 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13293 while (register_bits)
13294 {
13295 if (register_bits & 0x00000001)
13296 record_buf[index++] = register_count;
13297 register_bits = register_bits >> 1;
13298 register_count++;
13299 }
13300 record_buf[index++] = ARM_PS_REGNUM;
13301 record_buf[index++] = ARM_SP_REGNUM;
13302 thumb_insn_r->reg_rec_count = index;
13303 break;
13304 case 0xe:
13305 /* BKPT insn. */
13306 /* Handle enhanced software breakpoint insn, BKPT. */
13307 /* CPSR is changed to be executed in ARM state, disabling normal
13308 interrupts, entering abort mode. */
13309 /* According to high vector configuration PC is set. */
13310 /* User hits breakpoint and type reverse, in that case, we need to go back with
13311 previous CPSR and Program Counter. */
13312 record_buf[0] = ARM_PS_REGNUM;
13313 record_buf[1] = ARM_LR_REGNUM;
13314 thumb_insn_r->reg_rec_count = 2;
13315 /* We need to save SPSR value, which is not yet done. */
6cb06a8c
TT
13316 gdb_printf (gdb_stderr,
13317 _("Process record does not support instruction "
13318 "0x%0x at address %s.\n"),
13319 thumb_insn_r->arm_insn,
13320 paddress (thumb_insn_r->gdbarch,
13321 thumb_insn_r->this_addr));
b121eeb9
YQ
13322 return -1;
13323
13324 case 0xf:
13325 /* If-Then, and hints */
13326 break;
13327 default:
13328 return -1;
13329 };
72508ac0
PO
13330 }
13331
13332 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13333 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13334 record_buf_mem);
72508ac0
PO
13335
13336 return 0;
13337}
13338
13339/* Handling opcode 110 insns. */
13340
13341static int
4748a9be 13342thumb_record_ldm_stm_swi (arm_insn_decode_record *thumb_insn_r)
72508ac0 13343{
345bd07c 13344 arm_gdbarch_tdep *tdep
08106042 13345 = gdbarch_tdep<arm_gdbarch_tdep> (thumb_insn_r->gdbarch);
72508ac0
PO
13346 struct regcache *reg_cache = thumb_insn_r->regcache;
13347
13348 uint32_t ret = 0; /* function return value: -1:record failure ; 0:success */
13349 uint32_t reg_src1 = 0;
13350 uint32_t opcode1 = 0, opcode2 = 0, register_bits = 0, register_count = 0;
bec2ab5a 13351 uint32_t index = 0, start_address = 0;
72508ac0
PO
13352 uint32_t record_buf[24], record_buf_mem[48];
13353
13354 ULONGEST u_regval = 0;
13355
13356 opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
13357 opcode2 = bits (thumb_insn_r->arm_insn, 11, 12);
13358
13359 if (1 == opcode2)
13360 {
13361
13362 /* LDMIA. */
13363 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13364 /* Get Rn. */
13365 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13366 while (register_bits)
dda83cd7
SM
13367 {
13368 if (register_bits & 0x00000001)
13369 record_buf[index++] = register_count;
13370 register_bits = register_bits >> 1;
13371 register_count++;
13372 }
f969241e
OJ
13373 record_buf[index++] = reg_src1;
13374 thumb_insn_r->reg_rec_count = index;
72508ac0
PO
13375 }
13376 else if (0 == opcode2)
13377 {
13378 /* It handles both STMIA. */
13379 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13380 /* Get Rn. */
13381 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13382 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
13383 while (register_bits)
dda83cd7
SM
13384 {
13385 if (register_bits & 0x00000001)
13386 register_count++;
13387 register_bits = register_bits >> 1;
13388 }
72508ac0
PO
13389 start_address = u_regval;
13390 thumb_insn_r->mem_rec_count = register_count;
13391 while (register_count)
dda83cd7
SM
13392 {
13393 record_buf_mem[(register_count * 2) - 1] = start_address;
13394 record_buf_mem[(register_count * 2) - 2] = 4;
13395 start_address = start_address + 4;
13396 register_count--;
13397 }
72508ac0
PO
13398 }
13399 else if (0x1F == opcode1)
13400 {
dda83cd7
SM
13401 /* Handle arm syscall insn. */
13402 if (tdep->arm_syscall_record != NULL)
13403 {
13404 regcache_raw_read_unsigned (reg_cache, 7, &u_regval);
13405 ret = tdep->arm_syscall_record (reg_cache, u_regval);
13406 }
13407 else
13408 {
6cb06a8c 13409 gdb_printf (gdb_stderr, _("no syscall record support\n"));
dda83cd7
SM
13410 return -1;
13411 }
72508ac0
PO
13412 }
13413
13414 /* B (1), conditional branch is automatically taken care in process_record,
13415 as PC is saved there. */
13416
13417 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13418 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13419 record_buf_mem);
72508ac0
PO
13420
13421 return ret;
13422}
13423
13424/* Handling opcode 111 insns. */
13425
13426static int
4748a9be 13427thumb_record_branch (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13428{
13429 uint32_t record_buf[8];
13430 uint32_t bits_h = 0;
13431
13432 bits_h = bits (thumb_insn_r->arm_insn, 11, 12);
13433
13434 if (2 == bits_h || 3 == bits_h)
13435 {
13436 /* BL */
13437 record_buf[0] = ARM_LR_REGNUM;
13438 thumb_insn_r->reg_rec_count = 1;
13439 }
13440 else if (1 == bits_h)
13441 {
13442 /* BLX(1). */
13443 record_buf[0] = ARM_PS_REGNUM;
13444 record_buf[1] = ARM_LR_REGNUM;
13445 thumb_insn_r->reg_rec_count = 2;
13446 }
13447
13448 /* B(2) is automatically taken care in process_record, as PC is
13449 saved there. */
13450
13451 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13452
13453 return 0;
13454}
13455
c6ec2b30
OJ
13456/* Handler for thumb2 load/store multiple instructions. */
13457
13458static int
4748a9be 13459thumb2_record_ld_st_multiple (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13460{
13461 struct regcache *reg_cache = thumb2_insn_r->regcache;
13462
13463 uint32_t reg_rn, op;
13464 uint32_t register_bits = 0, register_count = 0;
13465 uint32_t index = 0, start_address = 0;
13466 uint32_t record_buf[24], record_buf_mem[48];
13467
13468 ULONGEST u_regval = 0;
13469
13470 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13471 op = bits (thumb2_insn_r->arm_insn, 23, 24);
13472
13473 if (0 == op || 3 == op)
13474 {
13475 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
dda83cd7
SM
13476 {
13477 /* Handle RFE instruction. */
13478 record_buf[0] = ARM_PS_REGNUM;
13479 thumb2_insn_r->reg_rec_count = 1;
13480 }
c6ec2b30 13481 else
dda83cd7
SM
13482 {
13483 /* Handle SRS instruction after reading banked SP. */
13484 return arm_record_unsupported_insn (thumb2_insn_r);
13485 }
c6ec2b30
OJ
13486 }
13487 else if (1 == op || 2 == op)
13488 {
13489 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
dda83cd7
SM
13490 {
13491 /* Handle LDM/LDMIA/LDMFD and LDMDB/LDMEA instructions. */
13492 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
13493 while (register_bits)
13494 {
13495 if (register_bits & 0x00000001)
13496 record_buf[index++] = register_count;
13497
13498 register_count++;
13499 register_bits = register_bits >> 1;
13500 }
13501 record_buf[index++] = reg_rn;
13502 record_buf[index++] = ARM_PS_REGNUM;
13503 thumb2_insn_r->reg_rec_count = index;
13504 }
c6ec2b30 13505 else
dda83cd7
SM
13506 {
13507 /* Handle STM/STMIA/STMEA and STMDB/STMFD. */
13508 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
13509 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
13510 while (register_bits)
13511 {
13512 if (register_bits & 0x00000001)
13513 register_count++;
13514
13515 register_bits = register_bits >> 1;
13516 }
13517
13518 if (1 == op)
13519 {
13520 /* Start address calculation for LDMDB/LDMEA. */
13521 start_address = u_regval;
13522 }
13523 else if (2 == op)
13524 {
13525 /* Start address calculation for LDMDB/LDMEA. */
13526 start_address = u_regval - register_count * 4;
13527 }
13528
13529 thumb2_insn_r->mem_rec_count = register_count;
13530 while (register_count)
13531 {
13532 record_buf_mem[register_count * 2 - 1] = start_address;
13533 record_buf_mem[register_count * 2 - 2] = 4;
13534 start_address = start_address + 4;
13535 register_count--;
13536 }
13537 record_buf[0] = reg_rn;
13538 record_buf[1] = ARM_PS_REGNUM;
13539 thumb2_insn_r->reg_rec_count = 2;
13540 }
c6ec2b30
OJ
13541 }
13542
13543 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
dda83cd7 13544 record_buf_mem);
c6ec2b30 13545 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13546 record_buf);
c6ec2b30
OJ
13547 return ARM_RECORD_SUCCESS;
13548}
13549
13550/* Handler for thumb2 load/store (dual/exclusive) and table branch
13551 instructions. */
13552
13553static int
4748a9be 13554thumb2_record_ld_st_dual_ex_tbb (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13555{
13556 struct regcache *reg_cache = thumb2_insn_r->regcache;
13557
13558 uint32_t reg_rd, reg_rn, offset_imm;
13559 uint32_t reg_dest1, reg_dest2;
13560 uint32_t address, offset_addr;
13561 uint32_t record_buf[8], record_buf_mem[8];
13562 uint32_t op1, op2, op3;
c6ec2b30
OJ
13563
13564 ULONGEST u_regval[2];
13565
13566 op1 = bits (thumb2_insn_r->arm_insn, 23, 24);
13567 op2 = bits (thumb2_insn_r->arm_insn, 20, 21);
13568 op3 = bits (thumb2_insn_r->arm_insn, 4, 7);
13569
13570 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
13571 {
13572 if(!(1 == op1 && 1 == op2 && (0 == op3 || 1 == op3)))
dda83cd7
SM
13573 {
13574 reg_dest1 = bits (thumb2_insn_r->arm_insn, 12, 15);
13575 record_buf[0] = reg_dest1;
13576 record_buf[1] = ARM_PS_REGNUM;
13577 thumb2_insn_r->reg_rec_count = 2;
13578 }
c6ec2b30
OJ
13579
13580 if (3 == op2 || (op1 & 2) || (1 == op1 && 1 == op2 && 7 == op3))
dda83cd7
SM
13581 {
13582 reg_dest2 = bits (thumb2_insn_r->arm_insn, 8, 11);
13583 record_buf[2] = reg_dest2;
13584 thumb2_insn_r->reg_rec_count = 3;
13585 }
c6ec2b30
OJ
13586 }
13587 else
13588 {
13589 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13590 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
13591
13592 if (0 == op1 && 0 == op2)
dda83cd7
SM
13593 {
13594 /* Handle STREX. */
13595 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13596 address = u_regval[0] + (offset_imm * 4);
13597 record_buf_mem[0] = 4;
13598 record_buf_mem[1] = address;
13599 thumb2_insn_r->mem_rec_count = 1;
13600 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
13601 record_buf[0] = reg_rd;
13602 thumb2_insn_r->reg_rec_count = 1;
13603 }
c6ec2b30 13604 else if (1 == op1 && 0 == op2)
dda83cd7
SM
13605 {
13606 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
13607 record_buf[0] = reg_rd;
13608 thumb2_insn_r->reg_rec_count = 1;
13609 address = u_regval[0];
13610 record_buf_mem[1] = address;
13611
13612 if (4 == op3)
13613 {
13614 /* Handle STREXB. */
13615 record_buf_mem[0] = 1;
13616 thumb2_insn_r->mem_rec_count = 1;
13617 }
13618 else if (5 == op3)
13619 {
13620 /* Handle STREXH. */
13621 record_buf_mem[0] = 2 ;
13622 thumb2_insn_r->mem_rec_count = 1;
13623 }
13624 else if (7 == op3)
13625 {
13626 /* Handle STREXD. */
13627 address = u_regval[0];
13628 record_buf_mem[0] = 4;
13629 record_buf_mem[2] = 4;
13630 record_buf_mem[3] = address + 4;
13631 thumb2_insn_r->mem_rec_count = 2;
13632 }
13633 }
c6ec2b30 13634 else
dda83cd7
SM
13635 {
13636 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13637
13638 if (bit (thumb2_insn_r->arm_insn, 24))
13639 {
13640 if (bit (thumb2_insn_r->arm_insn, 23))
13641 offset_addr = u_regval[0] + (offset_imm * 4);
13642 else
13643 offset_addr = u_regval[0] - (offset_imm * 4);
13644
13645 address = offset_addr;
13646 }
13647 else
13648 address = u_regval[0];
13649
13650 record_buf_mem[0] = 4;
13651 record_buf_mem[1] = address;
13652 record_buf_mem[2] = 4;
13653 record_buf_mem[3] = address + 4;
13654 thumb2_insn_r->mem_rec_count = 2;
13655 record_buf[0] = reg_rn;
13656 thumb2_insn_r->reg_rec_count = 1;
13657 }
c6ec2b30
OJ
13658 }
13659
13660 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13661 record_buf);
c6ec2b30 13662 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
dda83cd7 13663 record_buf_mem);
c6ec2b30
OJ
13664 return ARM_RECORD_SUCCESS;
13665}
13666
13667/* Handler for thumb2 data processing (shift register and modified immediate)
13668 instructions. */
13669
13670static int
4748a9be 13671thumb2_record_data_proc_sreg_mimm (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13672{
13673 uint32_t reg_rd, op;
13674 uint32_t record_buf[8];
13675
13676 op = bits (thumb2_insn_r->arm_insn, 21, 24);
13677 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
13678
13679 if ((0 == op || 4 == op || 8 == op || 13 == op) && 15 == reg_rd)
13680 {
13681 record_buf[0] = ARM_PS_REGNUM;
13682 thumb2_insn_r->reg_rec_count = 1;
13683 }
13684 else
13685 {
13686 record_buf[0] = reg_rd;
13687 record_buf[1] = ARM_PS_REGNUM;
13688 thumb2_insn_r->reg_rec_count = 2;
13689 }
13690
13691 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13692 record_buf);
c6ec2b30
OJ
13693 return ARM_RECORD_SUCCESS;
13694}
13695
13696/* Generic handler for thumb2 instructions which effect destination and PS
13697 registers. */
13698
13699static int
4748a9be 13700thumb2_record_ps_dest_generic (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13701{
13702 uint32_t reg_rd;
13703 uint32_t record_buf[8];
13704
13705 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
13706
13707 record_buf[0] = reg_rd;
13708 record_buf[1] = ARM_PS_REGNUM;
13709 thumb2_insn_r->reg_rec_count = 2;
13710
13711 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13712 record_buf);
c6ec2b30
OJ
13713 return ARM_RECORD_SUCCESS;
13714}
13715
13716/* Handler for thumb2 branch and miscellaneous control instructions. */
13717
13718static int
4748a9be 13719thumb2_record_branch_misc_cntrl (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13720{
13721 uint32_t op, op1, op2;
13722 uint32_t record_buf[8];
13723
13724 op = bits (thumb2_insn_r->arm_insn, 20, 26);
13725 op1 = bits (thumb2_insn_r->arm_insn, 12, 14);
13726 op2 = bits (thumb2_insn_r->arm_insn, 8, 11);
13727
13728 /* Handle MSR insn. */
13729 if (!(op1 & 0x2) && 0x38 == op)
13730 {
13731 if (!(op2 & 0x3))
dda83cd7
SM
13732 {
13733 /* CPSR is going to be changed. */
13734 record_buf[0] = ARM_PS_REGNUM;
13735 thumb2_insn_r->reg_rec_count = 1;
13736 }
c6ec2b30 13737 else
dda83cd7
SM
13738 {
13739 arm_record_unsupported_insn(thumb2_insn_r);
13740 return -1;
13741 }
c6ec2b30
OJ
13742 }
13743 else if (4 == (op1 & 0x5) || 5 == (op1 & 0x5))
13744 {
13745 /* BLX. */
13746 record_buf[0] = ARM_PS_REGNUM;
13747 record_buf[1] = ARM_LR_REGNUM;
13748 thumb2_insn_r->reg_rec_count = 2;
13749 }
13750
13751 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13752 record_buf);
c6ec2b30
OJ
13753 return ARM_RECORD_SUCCESS;
13754}
13755
13756/* Handler for thumb2 store single data item instructions. */
13757
13758static int
4748a9be 13759thumb2_record_str_single_data (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13760{
13761 struct regcache *reg_cache = thumb2_insn_r->regcache;
13762
13763 uint32_t reg_rn, reg_rm, offset_imm, shift_imm;
13764 uint32_t address, offset_addr;
13765 uint32_t record_buf[8], record_buf_mem[8];
13766 uint32_t op1, op2;
13767
13768 ULONGEST u_regval[2];
13769
13770 op1 = bits (thumb2_insn_r->arm_insn, 21, 23);
13771 op2 = bits (thumb2_insn_r->arm_insn, 6, 11);
13772 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13773 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
13774
13775 if (bit (thumb2_insn_r->arm_insn, 23))
13776 {
13777 /* T2 encoding. */
13778 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 11);
13779 offset_addr = u_regval[0] + offset_imm;
13780 address = offset_addr;
13781 }
13782 else
13783 {
13784 /* T3 encoding. */
13785 if ((0 == op1 || 1 == op1 || 2 == op1) && !(op2 & 0x20))
dda83cd7
SM
13786 {
13787 /* Handle STRB (register). */
13788 reg_rm = bits (thumb2_insn_r->arm_insn, 0, 3);
13789 regcache_raw_read_unsigned (reg_cache, reg_rm, &u_regval[1]);
13790 shift_imm = bits (thumb2_insn_r->arm_insn, 4, 5);
13791 offset_addr = u_regval[1] << shift_imm;
13792 address = u_regval[0] + offset_addr;
13793 }
c6ec2b30 13794 else
dda83cd7
SM
13795 {
13796 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13797 if (bit (thumb2_insn_r->arm_insn, 10))
13798 {
13799 if (bit (thumb2_insn_r->arm_insn, 9))
13800 offset_addr = u_regval[0] + offset_imm;
13801 else
13802 offset_addr = u_regval[0] - offset_imm;
13803
13804 address = offset_addr;
13805 }
13806 else
13807 address = u_regval[0];
13808 }
c6ec2b30
OJ
13809 }
13810
13811 switch (op1)
13812 {
13813 /* Store byte instructions. */
13814 case 4:
13815 case 0:
dda83cd7
SM
13816 record_buf_mem[0] = 1;
13817 break;
c6ec2b30
OJ
13818 /* Store half word instructions. */
13819 case 1:
13820 case 5:
dda83cd7
SM
13821 record_buf_mem[0] = 2;
13822 break;
c6ec2b30
OJ
13823 /* Store word instructions. */
13824 case 2:
13825 case 6:
dda83cd7
SM
13826 record_buf_mem[0] = 4;
13827 break;
c6ec2b30
OJ
13828
13829 default:
dda83cd7
SM
13830 gdb_assert_not_reached ("no decoding pattern found");
13831 break;
c6ec2b30
OJ
13832 }
13833
13834 record_buf_mem[1] = address;
13835 thumb2_insn_r->mem_rec_count = 1;
13836 record_buf[0] = reg_rn;
13837 thumb2_insn_r->reg_rec_count = 1;
13838
13839 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13840 record_buf);
c6ec2b30 13841 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
dda83cd7 13842 record_buf_mem);
c6ec2b30
OJ
13843 return ARM_RECORD_SUCCESS;
13844}
13845
13846/* Handler for thumb2 load memory hints instructions. */
13847
13848static int
4748a9be 13849thumb2_record_ld_mem_hints (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13850{
13851 uint32_t record_buf[8];
13852 uint32_t reg_rt, reg_rn;
13853
13854 reg_rt = bits (thumb2_insn_r->arm_insn, 12, 15);
13855 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13856
13857 if (ARM_PC_REGNUM != reg_rt)
13858 {
13859 record_buf[0] = reg_rt;
13860 record_buf[1] = reg_rn;
13861 record_buf[2] = ARM_PS_REGNUM;
13862 thumb2_insn_r->reg_rec_count = 3;
13863
13864 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13865 record_buf);
c6ec2b30
OJ
13866 return ARM_RECORD_SUCCESS;
13867 }
13868
13869 return ARM_RECORD_FAILURE;
13870}
13871
13872/* Handler for thumb2 load word instructions. */
13873
13874static int
4748a9be 13875thumb2_record_ld_word (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30 13876{
c6ec2b30
OJ
13877 uint32_t record_buf[8];
13878
13879 record_buf[0] = bits (thumb2_insn_r->arm_insn, 12, 15);
13880 record_buf[1] = ARM_PS_REGNUM;
13881 thumb2_insn_r->reg_rec_count = 2;
13882
13883 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13884 record_buf);
c6ec2b30
OJ
13885 return ARM_RECORD_SUCCESS;
13886}
13887
13888/* Handler for thumb2 long multiply, long multiply accumulate, and
13889 divide instructions. */
13890
13891static int
4748a9be 13892thumb2_record_lmul_lmla_div (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13893{
13894 uint32_t opcode1 = 0, opcode2 = 0;
13895 uint32_t record_buf[8];
c6ec2b30
OJ
13896
13897 opcode1 = bits (thumb2_insn_r->arm_insn, 20, 22);
13898 opcode2 = bits (thumb2_insn_r->arm_insn, 4, 7);
13899
13900 if (0 == opcode1 || 2 == opcode1 || (opcode1 >= 4 && opcode1 <= 6))
13901 {
13902 /* Handle SMULL, UMULL, SMULAL. */
13903 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
13904 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
13905 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
13906 record_buf[2] = ARM_PS_REGNUM;
13907 thumb2_insn_r->reg_rec_count = 3;
13908 }
13909 else if (1 == opcode1 || 3 == opcode2)
13910 {
13911 /* Handle SDIV and UDIV. */
13912 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
13913 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
13914 record_buf[2] = ARM_PS_REGNUM;
13915 thumb2_insn_r->reg_rec_count = 3;
13916 }
13917 else
13918 return ARM_RECORD_FAILURE;
13919
13920 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13921 record_buf);
c6ec2b30
OJ
13922 return ARM_RECORD_SUCCESS;
13923}
13924
60cc5e93
OJ
13925/* Record handler for thumb32 coprocessor instructions. */
13926
13927static int
4748a9be 13928thumb2_record_coproc_insn (arm_insn_decode_record *thumb2_insn_r)
60cc5e93
OJ
13929{
13930 if (bit (thumb2_insn_r->arm_insn, 25))
13931 return arm_record_coproc_data_proc (thumb2_insn_r);
13932 else
13933 return arm_record_asimd_vfp_coproc (thumb2_insn_r);
13934}
13935
1e1b6563
OJ
13936/* Record handler for advance SIMD structure load/store instructions. */
13937
13938static int
4748a9be 13939thumb2_record_asimd_struct_ld_st (arm_insn_decode_record *thumb2_insn_r)
1e1b6563
OJ
13940{
13941 struct regcache *reg_cache = thumb2_insn_r->regcache;
13942 uint32_t l_bit, a_bit, b_bits;
13943 uint32_t record_buf[128], record_buf_mem[128];
bec2ab5a 13944 uint32_t reg_rn, reg_vd, address, f_elem;
1e1b6563
OJ
13945 uint32_t index_r = 0, index_e = 0, bf_regs = 0, index_m = 0, loop_t = 0;
13946 uint8_t f_ebytes;
13947
13948 l_bit = bit (thumb2_insn_r->arm_insn, 21);
13949 a_bit = bit (thumb2_insn_r->arm_insn, 23);
13950 b_bits = bits (thumb2_insn_r->arm_insn, 8, 11);
13951 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13952 reg_vd = bits (thumb2_insn_r->arm_insn, 12, 15);
13953 reg_vd = (bit (thumb2_insn_r->arm_insn, 22) << 4) | reg_vd;
13954 f_ebytes = (1 << bits (thumb2_insn_r->arm_insn, 6, 7));
1e1b6563
OJ
13955 f_elem = 8 / f_ebytes;
13956
13957 if (!l_bit)
13958 {
13959 ULONGEST u_regval = 0;
13960 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
13961 address = u_regval;
13962
13963 if (!a_bit)
dda83cd7
SM
13964 {
13965 /* Handle VST1. */
13966 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
13967 {
13968 if (b_bits == 0x07)
13969 bf_regs = 1;
13970 else if (b_bits == 0x0a)
13971 bf_regs = 2;
13972 else if (b_bits == 0x06)
13973 bf_regs = 3;
13974 else if (b_bits == 0x02)
13975 bf_regs = 4;
13976 else
13977 bf_regs = 0;
13978
13979 for (index_r = 0; index_r < bf_regs; index_r++)
13980 {
13981 for (index_e = 0; index_e < f_elem; index_e++)
13982 {
13983 record_buf_mem[index_m++] = f_ebytes;
13984 record_buf_mem[index_m++] = address;
13985 address = address + f_ebytes;
13986 thumb2_insn_r->mem_rec_count += 1;
13987 }
13988 }
13989 }
13990 /* Handle VST2. */
13991 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
13992 {
13993 if (b_bits == 0x09 || b_bits == 0x08)
13994 bf_regs = 1;
13995 else if (b_bits == 0x03)
13996 bf_regs = 2;
13997 else
13998 bf_regs = 0;
13999
14000 for (index_r = 0; index_r < bf_regs; index_r++)
14001 for (index_e = 0; index_e < f_elem; index_e++)
14002 {
14003 for (loop_t = 0; loop_t < 2; loop_t++)
14004 {
14005 record_buf_mem[index_m++] = f_ebytes;
14006 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
14007 thumb2_insn_r->mem_rec_count += 1;
14008 }
14009 address = address + (2 * f_ebytes);
14010 }
14011 }
14012 /* Handle VST3. */
14013 else if ((b_bits & 0x0e) == 0x04)
14014 {
14015 for (index_e = 0; index_e < f_elem; index_e++)
14016 {
14017 for (loop_t = 0; loop_t < 3; loop_t++)
14018 {
14019 record_buf_mem[index_m++] = f_ebytes;
14020 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
14021 thumb2_insn_r->mem_rec_count += 1;
14022 }
14023 address = address + (3 * f_ebytes);
14024 }
14025 }
14026 /* Handle VST4. */
14027 else if (!(b_bits & 0x0e))
14028 {
14029 for (index_e = 0; index_e < f_elem; index_e++)
14030 {
14031 for (loop_t = 0; loop_t < 4; loop_t++)
14032 {
14033 record_buf_mem[index_m++] = f_ebytes;
14034 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
14035 thumb2_insn_r->mem_rec_count += 1;
14036 }
14037 address = address + (4 * f_ebytes);
14038 }
14039 }
14040 }
1e1b6563 14041 else
dda83cd7
SM
14042 {
14043 uint8_t bft_size = bits (thumb2_insn_r->arm_insn, 10, 11);
14044
14045 if (bft_size == 0x00)
14046 f_ebytes = 1;
14047 else if (bft_size == 0x01)
14048 f_ebytes = 2;
14049 else if (bft_size == 0x02)
14050 f_ebytes = 4;
14051 else
14052 f_ebytes = 0;
14053
14054 /* Handle VST1. */
14055 if (!(b_bits & 0x0b) || b_bits == 0x08)
14056 thumb2_insn_r->mem_rec_count = 1;
14057 /* Handle VST2. */
14058 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09)
14059 thumb2_insn_r->mem_rec_count = 2;
14060 /* Handle VST3. */
14061 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a)
14062 thumb2_insn_r->mem_rec_count = 3;
14063 /* Handle VST4. */
14064 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b)
14065 thumb2_insn_r->mem_rec_count = 4;
14066
14067 for (index_m = 0; index_m < thumb2_insn_r->mem_rec_count; index_m++)
14068 {
14069 record_buf_mem[index_m] = f_ebytes;
14070 record_buf_mem[index_m] = address + (index_m * f_ebytes);
14071 }
14072 }
1e1b6563
OJ
14073 }
14074 else
14075 {
14076 if (!a_bit)
dda83cd7
SM
14077 {
14078 /* Handle VLD1. */
14079 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
14080 thumb2_insn_r->reg_rec_count = 1;
14081 /* Handle VLD2. */
14082 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
14083 thumb2_insn_r->reg_rec_count = 2;
14084 /* Handle VLD3. */
14085 else if ((b_bits & 0x0e) == 0x04)
14086 thumb2_insn_r->reg_rec_count = 3;
14087 /* Handle VLD4. */
14088 else if (!(b_bits & 0x0e))
14089 thumb2_insn_r->reg_rec_count = 4;
14090 }
1e1b6563 14091 else
dda83cd7
SM
14092 {
14093 /* Handle VLD1. */
14094 if (!(b_bits & 0x0b) || b_bits == 0x08 || b_bits == 0x0c)
14095 thumb2_insn_r->reg_rec_count = 1;
14096 /* Handle VLD2. */
14097 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09 || b_bits == 0x0d)
14098 thumb2_insn_r->reg_rec_count = 2;
14099 /* Handle VLD3. */
14100 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a || b_bits == 0x0e)
14101 thumb2_insn_r->reg_rec_count = 3;
14102 /* Handle VLD4. */
14103 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b || b_bits == 0x0f)
14104 thumb2_insn_r->reg_rec_count = 4;
14105
14106 for (index_r = 0; index_r < thumb2_insn_r->reg_rec_count; index_r++)
14107 record_buf[index_r] = reg_vd + ARM_D0_REGNUM + index_r;
14108 }
1e1b6563
OJ
14109 }
14110
14111 if (bits (thumb2_insn_r->arm_insn, 0, 3) != 15)
14112 {
14113 record_buf[index_r] = reg_rn;
14114 thumb2_insn_r->reg_rec_count += 1;
14115 }
14116
14117 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 14118 record_buf);
1e1b6563 14119 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
dda83cd7 14120 record_buf_mem);
1e1b6563
OJ
14121 return 0;
14122}
14123
c6ec2b30
OJ
14124/* Decodes thumb2 instruction type and invokes its record handler. */
14125
14126static unsigned int
4748a9be 14127thumb2_record_decode_insn_handler (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
14128{
14129 uint32_t op, op1, op2;
14130
14131 op = bit (thumb2_insn_r->arm_insn, 15);
14132 op1 = bits (thumb2_insn_r->arm_insn, 27, 28);
14133 op2 = bits (thumb2_insn_r->arm_insn, 20, 26);
14134
14135 if (op1 == 0x01)
14136 {
14137 if (!(op2 & 0x64 ))
dda83cd7
SM
14138 {
14139 /* Load/store multiple instruction. */
14140 return thumb2_record_ld_st_multiple (thumb2_insn_r);
14141 }
b121eeb9 14142 else if ((op2 & 0x64) == 0x4)
dda83cd7
SM
14143 {
14144 /* Load/store (dual/exclusive) and table branch instruction. */
14145 return thumb2_record_ld_st_dual_ex_tbb (thumb2_insn_r);
14146 }
b121eeb9 14147 else if ((op2 & 0x60) == 0x20)
dda83cd7
SM
14148 {
14149 /* Data-processing (shifted register). */
14150 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
14151 }
c6ec2b30 14152 else if (op2 & 0x40)
dda83cd7
SM
14153 {
14154 /* Co-processor instructions. */
14155 return thumb2_record_coproc_insn (thumb2_insn_r);
14156 }
c6ec2b30
OJ
14157 }
14158 else if (op1 == 0x02)
14159 {
14160 if (op)
dda83cd7
SM
14161 {
14162 /* Branches and miscellaneous control instructions. */
14163 return thumb2_record_branch_misc_cntrl (thumb2_insn_r);
14164 }
c6ec2b30 14165 else if (op2 & 0x20)
dda83cd7
SM
14166 {
14167 /* Data-processing (plain binary immediate) instruction. */
14168 return thumb2_record_ps_dest_generic (thumb2_insn_r);
14169 }
c6ec2b30 14170 else
dda83cd7
SM
14171 {
14172 /* Data-processing (modified immediate). */
14173 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
14174 }
c6ec2b30
OJ
14175 }
14176 else if (op1 == 0x03)
14177 {
14178 if (!(op2 & 0x71 ))
dda83cd7
SM
14179 {
14180 /* Store single data item. */
14181 return thumb2_record_str_single_data (thumb2_insn_r);
14182 }
c6ec2b30 14183 else if (!((op2 & 0x71) ^ 0x10))
dda83cd7
SM
14184 {
14185 /* Advanced SIMD or structure load/store instructions. */
14186 return thumb2_record_asimd_struct_ld_st (thumb2_insn_r);
14187 }
c6ec2b30 14188 else if (!((op2 & 0x67) ^ 0x01))
dda83cd7
SM
14189 {
14190 /* Load byte, memory hints instruction. */
14191 return thumb2_record_ld_mem_hints (thumb2_insn_r);
14192 }
c6ec2b30 14193 else if (!((op2 & 0x67) ^ 0x03))
dda83cd7
SM
14194 {
14195 /* Load halfword, memory hints instruction. */
14196 return thumb2_record_ld_mem_hints (thumb2_insn_r);
14197 }
c6ec2b30 14198 else if (!((op2 & 0x67) ^ 0x05))
dda83cd7
SM
14199 {
14200 /* Load word instruction. */
14201 return thumb2_record_ld_word (thumb2_insn_r);
14202 }
c6ec2b30 14203 else if (!((op2 & 0x70) ^ 0x20))
dda83cd7
SM
14204 {
14205 /* Data-processing (register) instruction. */
14206 return thumb2_record_ps_dest_generic (thumb2_insn_r);
14207 }
c6ec2b30 14208 else if (!((op2 & 0x78) ^ 0x30))
dda83cd7
SM
14209 {
14210 /* Multiply, multiply accumulate, abs diff instruction. */
14211 return thumb2_record_ps_dest_generic (thumb2_insn_r);
14212 }
c6ec2b30 14213 else if (!((op2 & 0x78) ^ 0x38))
dda83cd7
SM
14214 {
14215 /* Long multiply, long multiply accumulate, and divide. */
14216 return thumb2_record_lmul_lmla_div (thumb2_insn_r);
14217 }
c6ec2b30 14218 else if (op2 & 0x40)
dda83cd7
SM
14219 {
14220 /* Co-processor instructions. */
14221 return thumb2_record_coproc_insn (thumb2_insn_r);
14222 }
c6ec2b30
OJ
14223 }
14224
14225 return -1;
14226}
72508ac0 14227
ffdbe864 14228namespace {
728a7913
YQ
14229/* Abstract memory reader. */
14230
14231class abstract_memory_reader
14232{
14233public:
14234 /* Read LEN bytes of target memory at address MEMADDR, placing the
14235 results in GDB's memory at BUF. Return true on success. */
14236
14237 virtual bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) = 0;
14238};
14239
14240/* Instruction reader from real target. */
14241
14242class instruction_reader : public abstract_memory_reader
14243{
14244 public:
632e107b 14245 bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) override
728a7913
YQ
14246 {
14247 if (target_read_memory (memaddr, buf, len))
14248 return false;
14249 else
14250 return true;
14251 }
14252};
14253
ffdbe864
YQ
14254} // namespace
14255
72508ac0 14256/* Extracts arm/thumb/thumb2 insn depending on the size, and returns 0 on success
85102364 14257and positive val on failure. */
72508ac0
PO
14258
14259static int
728a7913 14260extract_arm_insn (abstract_memory_reader& reader,
4748a9be 14261 arm_insn_decode_record *insn_record, uint32_t insn_size)
72508ac0
PO
14262{
14263 gdb_byte buf[insn_size];
14264
14265 memset (&buf[0], 0, insn_size);
14266
728a7913 14267 if (!reader.read (insn_record->this_addr, buf, insn_size))
72508ac0
PO
14268 return 1;
14269 insn_record->arm_insn = (uint32_t) extract_unsigned_integer (&buf[0],
dda83cd7 14270 insn_size,
2959fed9 14271 gdbarch_byte_order_for_code (insn_record->gdbarch));
72508ac0
PO
14272 return 0;
14273}
14274
4748a9be 14275typedef int (*sti_arm_hdl_fp_t) (arm_insn_decode_record*);
72508ac0
PO
14276
14277/* Decode arm/thumb insn depending on condition cods and opcodes; and
14278 dispatch it. */
14279
14280static int
4748a9be
TT
14281decode_insn (abstract_memory_reader &reader,
14282 arm_insn_decode_record *arm_record,
728a7913 14283 record_type_t record_type, uint32_t insn_size)
72508ac0
PO
14284{
14285
01e57735
YQ
14286 /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm
14287 instruction. */
0fa9c223 14288 static const sti_arm_hdl_fp_t arm_handle_insn[8] =
72508ac0
PO
14289 {
14290 arm_record_data_proc_misc_ld_str, /* 000. */
14291 arm_record_data_proc_imm, /* 001. */
14292 arm_record_ld_st_imm_offset, /* 010. */
14293 arm_record_ld_st_reg_offset, /* 011. */
14294 arm_record_ld_st_multiple, /* 100. */
14295 arm_record_b_bl, /* 101. */
60cc5e93 14296 arm_record_asimd_vfp_coproc, /* 110. */
72508ac0
PO
14297 arm_record_coproc_data_proc /* 111. */
14298 };
14299
01e57735
YQ
14300 /* (Starting from numerical 0); bits 13,14,15 decodes type of thumb
14301 instruction. */
0fa9c223 14302 static const sti_arm_hdl_fp_t thumb_handle_insn[8] =
72508ac0
PO
14303 { \
14304 thumb_record_shift_add_sub, /* 000. */
14305 thumb_record_add_sub_cmp_mov, /* 001. */
14306 thumb_record_ld_st_reg_offset, /* 010. */
14307 thumb_record_ld_st_imm_offset, /* 011. */
14308 thumb_record_ld_st_stack, /* 100. */
14309 thumb_record_misc, /* 101. */
14310 thumb_record_ldm_stm_swi, /* 110. */
14311 thumb_record_branch /* 111. */
14312 };
14313
14314 uint32_t ret = 0; /* return value: negative:failure 0:success. */
14315 uint32_t insn_id = 0;
14316
728a7913 14317 if (extract_arm_insn (reader, arm_record, insn_size))
72508ac0
PO
14318 {
14319 if (record_debug)
01e57735 14320 {
6cb06a8c
TT
14321 gdb_printf (gdb_stdlog,
14322 _("Process record: error reading memory at "
14323 "addr %s len = %d.\n"),
14324 paddress (arm_record->gdbarch,
14325 arm_record->this_addr), insn_size);
01e57735 14326 }
72508ac0
PO
14327 return -1;
14328 }
14329 else if (ARM_RECORD == record_type)
14330 {
14331 arm_record->cond = bits (arm_record->arm_insn, 28, 31);
14332 insn_id = bits (arm_record->arm_insn, 25, 27);
ca92db2d
YQ
14333
14334 if (arm_record->cond == 0xf)
14335 ret = arm_record_extension_space (arm_record);
14336 else
01e57735 14337 {
ca92db2d
YQ
14338 /* If this insn has fallen into extension space
14339 then we need not decode it anymore. */
01e57735
YQ
14340 ret = arm_handle_insn[insn_id] (arm_record);
14341 }
ca92db2d
YQ
14342 if (ret != ARM_RECORD_SUCCESS)
14343 {
14344 arm_record_unsupported_insn (arm_record);
14345 ret = -1;
14346 }
72508ac0
PO
14347 }
14348 else if (THUMB_RECORD == record_type)
14349 {
14350 /* As thumb does not have condition codes, we set negative. */
14351 arm_record->cond = -1;
14352 insn_id = bits (arm_record->arm_insn, 13, 15);
14353 ret = thumb_handle_insn[insn_id] (arm_record);
ca92db2d
YQ
14354 if (ret != ARM_RECORD_SUCCESS)
14355 {
14356 arm_record_unsupported_insn (arm_record);
14357 ret = -1;
14358 }
72508ac0
PO
14359 }
14360 else if (THUMB2_RECORD == record_type)
14361 {
c6ec2b30
OJ
14362 /* As thumb does not have condition codes, we set negative. */
14363 arm_record->cond = -1;
14364
14365 /* Swap first half of 32bit thumb instruction with second half. */
14366 arm_record->arm_insn
01e57735 14367 = (arm_record->arm_insn >> 16) | (arm_record->arm_insn << 16);
c6ec2b30 14368
ca92db2d 14369 ret = thumb2_record_decode_insn_handler (arm_record);
c6ec2b30 14370
ca92db2d 14371 if (ret != ARM_RECORD_SUCCESS)
01e57735
YQ
14372 {
14373 arm_record_unsupported_insn (arm_record);
14374 ret = -1;
14375 }
72508ac0
PO
14376 }
14377 else
14378 {
14379 /* Throw assertion. */
14380 gdb_assert_not_reached ("not a valid instruction, could not decode");
14381 }
14382
14383 return ret;
14384}
14385
b121eeb9
YQ
14386#if GDB_SELF_TEST
14387namespace selftests {
14388
14389/* Provide both 16-bit and 32-bit thumb instructions. */
14390
14391class instruction_reader_thumb : public abstract_memory_reader
14392{
14393public:
14394 template<size_t SIZE>
14395 instruction_reader_thumb (enum bfd_endian endian,
14396 const uint16_t (&insns)[SIZE])
14397 : m_endian (endian), m_insns (insns), m_insns_size (SIZE)
14398 {}
14399
632e107b 14400 bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) override
b121eeb9
YQ
14401 {
14402 SELF_CHECK (len == 4 || len == 2);
14403 SELF_CHECK (memaddr % 2 == 0);
14404 SELF_CHECK ((memaddr / 2) < m_insns_size);
14405
14406 store_unsigned_integer (buf, 2, m_endian, m_insns[memaddr / 2]);
14407 if (len == 4)
14408 {
14409 store_unsigned_integer (&buf[2], 2, m_endian,
14410 m_insns[memaddr / 2 + 1]);
14411 }
14412 return true;
14413 }
14414
14415private:
14416 enum bfd_endian m_endian;
14417 const uint16_t *m_insns;
14418 size_t m_insns_size;
14419};
14420
14421static void
14422arm_record_test (void)
14423{
14424 struct gdbarch_info info;
b121eeb9
YQ
14425 info.bfd_arch_info = bfd_scan_arch ("arm");
14426
14427 struct gdbarch *gdbarch = gdbarch_find_by_info (info);
14428
14429 SELF_CHECK (gdbarch != NULL);
14430
14431 /* 16-bit Thumb instructions. */
14432 {
4748a9be 14433 arm_insn_decode_record arm_record;
b121eeb9 14434
4748a9be 14435 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
b121eeb9
YQ
14436 arm_record.gdbarch = gdbarch;
14437
14438 static const uint16_t insns[] = {
14439 /* db b2 uxtb r3, r3 */
14440 0xb2db,
14441 /* cd 58 ldr r5, [r1, r3] */
14442 0x58cd,
14443 };
14444
14445 enum bfd_endian endian = gdbarch_byte_order_for_code (arm_record.gdbarch);
14446 instruction_reader_thumb reader (endian, insns);
14447 int ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14448 THUMB_INSN_SIZE_BYTES);
14449
14450 SELF_CHECK (ret == 0);
14451 SELF_CHECK (arm_record.mem_rec_count == 0);
14452 SELF_CHECK (arm_record.reg_rec_count == 1);
14453 SELF_CHECK (arm_record.arm_regs[0] == 3);
14454
14455 arm_record.this_addr += 2;
14456 ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14457 THUMB_INSN_SIZE_BYTES);
14458
14459 SELF_CHECK (ret == 0);
14460 SELF_CHECK (arm_record.mem_rec_count == 0);
14461 SELF_CHECK (arm_record.reg_rec_count == 1);
14462 SELF_CHECK (arm_record.arm_regs[0] == 5);
14463 }
14464
14465 /* 32-bit Thumb-2 instructions. */
14466 {
4748a9be 14467 arm_insn_decode_record arm_record;
b121eeb9 14468
4748a9be 14469 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
b121eeb9
YQ
14470 arm_record.gdbarch = gdbarch;
14471
14472 static const uint16_t insns[] = {
14473 /* 1d ee 70 7f mrc 15, 0, r7, cr13, cr0, {3} */
14474 0xee1d, 0x7f70,
14475 };
14476
14477 enum bfd_endian endian = gdbarch_byte_order_for_code (arm_record.gdbarch);
14478 instruction_reader_thumb reader (endian, insns);
14479 int ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
14480 THUMB2_INSN_SIZE_BYTES);
14481
14482 SELF_CHECK (ret == 0);
14483 SELF_CHECK (arm_record.mem_rec_count == 0);
14484 SELF_CHECK (arm_record.reg_rec_count == 1);
14485 SELF_CHECK (arm_record.arm_regs[0] == 7);
14486 }
14487}
9ecab40c
SM
14488
14489/* Instruction reader from manually cooked instruction sequences. */
14490
14491class test_arm_instruction_reader : public arm_instruction_reader
14492{
14493public:
14494 explicit test_arm_instruction_reader (gdb::array_view<const uint32_t> insns)
14495 : m_insns (insns)
14496 {}
14497
14498 uint32_t read (CORE_ADDR memaddr, enum bfd_endian byte_order) const override
14499 {
14500 SELF_CHECK (memaddr % 4 == 0);
14501 SELF_CHECK (memaddr / 4 < m_insns.size ());
14502
14503 return m_insns[memaddr / 4];
14504 }
14505
14506private:
14507 const gdb::array_view<const uint32_t> m_insns;
14508};
14509
14510static void
14511arm_analyze_prologue_test ()
14512{
14513 for (bfd_endian endianness : {BFD_ENDIAN_LITTLE, BFD_ENDIAN_BIG})
14514 {
14515 struct gdbarch_info info;
9ecab40c
SM
14516 info.byte_order = endianness;
14517 info.byte_order_for_code = endianness;
14518 info.bfd_arch_info = bfd_scan_arch ("arm");
14519
14520 struct gdbarch *gdbarch = gdbarch_find_by_info (info);
14521
14522 SELF_CHECK (gdbarch != NULL);
14523
14524 /* The "sub" instruction contains an immediate value rotate count of 0,
14525 which resulted in a 32-bit shift of a 32-bit value, caught by
14526 UBSan. */
14527 const uint32_t insns[] = {
14528 0xe92d4ff0, /* push {r4, r5, r6, r7, r8, r9, sl, fp, lr} */
14529 0xe1a05000, /* mov r5, r0 */
14530 0xe5903020, /* ldr r3, [r0, #32] */
14531 0xe24dd044, /* sub sp, sp, #68 ; 0x44 */
14532 };
14533
14534 test_arm_instruction_reader mem_reader (insns);
14535 arm_prologue_cache cache;
0824193f 14536 arm_cache_init (&cache, gdbarch);
9ecab40c
SM
14537
14538 arm_analyze_prologue (gdbarch, 0, sizeof (insns) - 1, &cache, mem_reader);
14539 }
14540}
14541
b121eeb9
YQ
14542} // namespace selftests
14543#endif /* GDB_SELF_TEST */
72508ac0
PO
14544
14545/* Cleans up local record registers and memory allocations. */
14546
14547static void
4748a9be 14548deallocate_reg_mem (arm_insn_decode_record *record)
72508ac0
PO
14549{
14550 xfree (record->arm_regs);
14551 xfree (record->arm_mems);
14552}
14553
14554
01e57735 14555/* Parse the current instruction and record the values of the registers and
72508ac0
PO
14556 memory that will be changed in current instruction to record_arch_list".
14557 Return -1 if something is wrong. */
14558
14559int
01e57735
YQ
14560arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
14561 CORE_ADDR insn_addr)
72508ac0
PO
14562{
14563
72508ac0
PO
14564 uint32_t no_of_rec = 0;
14565 uint32_t ret = 0; /* return value: -1:record failure ; 0:success */
14566 ULONGEST t_bit = 0, insn_id = 0;
14567
14568 ULONGEST u_regval = 0;
14569
4748a9be 14570 arm_insn_decode_record arm_record;
72508ac0 14571
4748a9be 14572 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
72508ac0
PO
14573 arm_record.regcache = regcache;
14574 arm_record.this_addr = insn_addr;
14575 arm_record.gdbarch = gdbarch;
14576
14577
14578 if (record_debug > 1)
14579 {
6cb06a8c
TT
14580 gdb_printf (gdb_stdlog, "Process record: arm_process_record "
14581 "addr = %s\n",
14582 paddress (gdbarch, arm_record.this_addr));
72508ac0
PO
14583 }
14584
728a7913
YQ
14585 instruction_reader reader;
14586 if (extract_arm_insn (reader, &arm_record, 2))
72508ac0
PO
14587 {
14588 if (record_debug)
01e57735 14589 {
6cb06a8c
TT
14590 gdb_printf (gdb_stdlog,
14591 _("Process record: error reading memory at "
14592 "addr %s len = %d.\n"),
14593 paddress (arm_record.gdbarch,
14594 arm_record.this_addr), 2);
01e57735 14595 }
72508ac0
PO
14596 return -1;
14597 }
14598
14599 /* Check the insn, whether it is thumb or arm one. */
14600
14601 t_bit = arm_psr_thumb_bit (arm_record.gdbarch);
14602 regcache_raw_read_unsigned (arm_record.regcache, ARM_PS_REGNUM, &u_regval);
14603
14604
14605 if (!(u_regval & t_bit))
14606 {
14607 /* We are decoding arm insn. */
728a7913 14608 ret = decode_insn (reader, &arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES);
72508ac0
PO
14609 }
14610 else
14611 {
14612 insn_id = bits (arm_record.arm_insn, 11, 15);
14613 /* is it thumb2 insn? */
14614 if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id))
01e57735 14615 {
728a7913 14616 ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
01e57735
YQ
14617 THUMB2_INSN_SIZE_BYTES);
14618 }
72508ac0 14619 else
01e57735
YQ
14620 {
14621 /* We are decoding thumb insn. */
728a7913
YQ
14622 ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14623 THUMB_INSN_SIZE_BYTES);
01e57735 14624 }
72508ac0
PO
14625 }
14626
14627 if (0 == ret)
14628 {
14629 /* Record registers. */
25ea693b 14630 record_full_arch_list_add_reg (arm_record.regcache, ARM_PC_REGNUM);
72508ac0 14631 if (arm_record.arm_regs)
01e57735
YQ
14632 {
14633 for (no_of_rec = 0; no_of_rec < arm_record.reg_rec_count; no_of_rec++)
14634 {
14635 if (record_full_arch_list_add_reg
25ea693b 14636 (arm_record.regcache , arm_record.arm_regs[no_of_rec]))
01e57735
YQ
14637 ret = -1;
14638 }
14639 }
72508ac0
PO
14640 /* Record memories. */
14641 if (arm_record.arm_mems)
01e57735
YQ
14642 {
14643 for (no_of_rec = 0; no_of_rec < arm_record.mem_rec_count; no_of_rec++)
14644 {
14645 if (record_full_arch_list_add_mem
14646 ((CORE_ADDR)arm_record.arm_mems[no_of_rec].addr,
25ea693b 14647 arm_record.arm_mems[no_of_rec].len))
01e57735
YQ
14648 ret = -1;
14649 }
14650 }
72508ac0 14651
25ea693b 14652 if (record_full_arch_list_add_end ())
01e57735 14653 ret = -1;
72508ac0
PO
14654 }
14655
14656
14657 deallocate_reg_mem (&arm_record);
14658
14659 return ret;
14660}
d105cce5
AH
14661
14662/* See arm-tdep.h. */
14663
14664const target_desc *
92d48a1e 14665arm_read_description (arm_fp_type fp_type, bool tls)
d105cce5 14666{
92d48a1e 14667 struct target_desc *tdesc = tdesc_arm_list[fp_type][tls];
d105cce5
AH
14668
14669 if (tdesc == nullptr)
14670 {
92d48a1e
JB
14671 tdesc = arm_create_target_description (fp_type, tls);
14672 tdesc_arm_list[fp_type][tls] = tdesc;
d105cce5
AH
14673 }
14674
14675 return tdesc;
14676}
14677
14678/* See arm-tdep.h. */
14679
14680const target_desc *
14681arm_read_mprofile_description (arm_m_profile_type m_type)
14682{
14683 struct target_desc *tdesc = tdesc_arm_mprofile_list[m_type];
14684
14685 if (tdesc == nullptr)
14686 {
14687 tdesc = arm_create_mprofile_target_description (m_type);
14688 tdesc_arm_mprofile_list[m_type] = tdesc;
14689 }
14690
14691 return tdesc;
14692}