]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/arm-tdep.c
gdb: remove target_gdbarch
[thirdparty/binutils-gdb.git] / gdb / arm-tdep.c
CommitLineData
ed9a39eb 1/* Common target dependent code for GDB on ARM systems.
0fd88904 2
213516ef 3 Copyright (C) 1988-2023 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
ef0f16cc 22#include <ctype.h>
34e8f22d 23
4de283e4 24#include "frame.h"
83b6e1f1 25#include "language.h"
4de283e4
TT
26#include "inferior.h"
27#include "infrun.h"
28#include "gdbcmd.h"
29#include "gdbcore.h"
ef0f16cc 30#include "dis-asm.h"
4de283e4
TT
31#include "disasm.h"
32#include "regcache.h"
33#include "reggroups.h"
34#include "target-float.h"
35#include "value.h"
d55e5aa6 36#include "arch-utils.h"
4de283e4
TT
37#include "osabi.h"
38#include "frame-unwind.h"
39#include "frame-base.h"
40#include "trad-frame.h"
41#include "objfiles.h"
a01567f4 42#include "dwarf2.h"
82ca8957 43#include "dwarf2/frame.h"
4de283e4
TT
44#include "gdbtypes.h"
45#include "prologue-value.h"
46#include "remote.h"
47#include "target-descriptions.h"
48#include "user-regs.h"
49#include "observable.h"
5f661e03 50#include "count-one-bits.h"
4de283e4 51
d55e5aa6 52#include "arch/arm.h"
4de283e4 53#include "arch/arm-get-next-pcs.h"
34e8f22d 54#include "arm-tdep.h"
d026e67e 55#include "sim/sim-arm.h"
4de283e4 56
d55e5aa6 57#include "elf-bfd.h"
4de283e4 58#include "coff/internal.h"
d55e5aa6 59#include "elf/arm.h"
4de283e4 60
4de283e4
TT
61#include "record.h"
62#include "record-full.h"
63#include <algorithm>
64
c2fd7fae
AKS
65#include "producer.h"
66
b121eeb9 67#if GDB_SELF_TEST
268a13a5 68#include "gdbsupport/selftest.h"
b121eeb9
YQ
69#endif
70
491144b5 71static bool arm_debug;
6529d2dd 72
7cb6d92a
SM
73/* Print an "arm" debug statement. */
74
75#define arm_debug_printf(fmt, ...) \
76 debug_prefixed_printf_cond (arm_debug, "arm", fmt, ##__VA_ARGS__)
77
082fc60d
RE
78/* Macros for setting and testing a bit in a minimal symbol that marks
79 it as Thumb function. The MSB of the minimal symbol's "info" field
f594e5e9 80 is used for this purpose.
082fc60d
RE
81
82 MSYMBOL_SET_SPECIAL Actually sets the "special" bit.
f594e5e9 83 MSYMBOL_IS_SPECIAL Tests the "special" bit in a minimal symbol. */
082fc60d 84
0963b4bd 85#define MSYMBOL_SET_SPECIAL(msym) \
e165fcef 86 (msym)->set_target_flag_1 (true)
082fc60d
RE
87
88#define MSYMBOL_IS_SPECIAL(msym) \
e165fcef 89 (msym)->target_flag_1 ()
082fc60d 90
60c5725c
DJ
91struct arm_mapping_symbol
92{
227031b2 93 CORE_ADDR value;
60c5725c 94 char type;
54cc7474
SM
95
96 bool operator< (const arm_mapping_symbol &other) const
97 { return this->value < other.value; }
60c5725c 98};
54cc7474
SM
99
100typedef std::vector<arm_mapping_symbol> arm_mapping_symbol_vec;
60c5725c 101
bd5766ec 102struct arm_per_bfd
60c5725c 103{
bd5766ec 104 explicit arm_per_bfd (size_t num_sections)
4838e44c
SM
105 : section_maps (new arm_mapping_symbol_vec[num_sections]),
106 section_maps_sorted (new bool[num_sections] ())
54cc7474
SM
107 {}
108
bd5766ec 109 DISABLE_COPY_AND_ASSIGN (arm_per_bfd);
54cc7474
SM
110
111 /* Information about mapping symbols ($a, $d, $t) in the objfile.
112
113 The format is an array of vectors of arm_mapping_symbols, there is one
114 vector for each section of the objfile (the array is index by BFD section
115 index).
116
117 For each section, the vector of arm_mapping_symbol is sorted by
118 symbol value (address). */
119 std::unique_ptr<arm_mapping_symbol_vec[]> section_maps;
4838e44c
SM
120
121 /* For each corresponding element of section_maps above, is this vector
122 sorted. */
123 std::unique_ptr<bool[]> section_maps_sorted;
60c5725c
DJ
124};
125
bd5766ec 126/* Per-bfd data used for mapping symbols. */
08b8a139 127static const registry<bfd>::key<arm_per_bfd> arm_bfd_data_key;
1b7f24cd 128
afd7eef0
RE
129/* The list of available "set arm ..." and "show arm ..." commands. */
130static struct cmd_list_element *setarmcmdlist = NULL;
131static struct cmd_list_element *showarmcmdlist = NULL;
132
fd50bc42
RE
133/* The type of floating-point to use. Keep this in sync with enum
134 arm_float_model, and the help string in _initialize_arm_tdep. */
40478521 135static const char *const fp_model_strings[] =
fd50bc42
RE
136{
137 "auto",
138 "softfpa",
139 "fpa",
140 "softvfp",
28e97307
DJ
141 "vfp",
142 NULL
fd50bc42
RE
143};
144
145/* A variable that can be configured by the user. */
146static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
147static const char *current_fp_model = "auto";
148
28e97307 149/* The ABI to use. Keep this in sync with arm_abi_kind. */
40478521 150static const char *const arm_abi_strings[] =
28e97307
DJ
151{
152 "auto",
153 "APCS",
154 "AAPCS",
155 NULL
156};
157
158/* A variable that can be configured by the user. */
159static enum arm_abi_kind arm_abi_global = ARM_ABI_AUTO;
160static const char *arm_abi_string = "auto";
161
0428b8f5 162/* The execution mode to assume. */
40478521 163static const char *const arm_mode_strings[] =
0428b8f5
DJ
164 {
165 "auto",
166 "arm",
68770265
MGD
167 "thumb",
168 NULL
0428b8f5
DJ
169 };
170
171static const char *arm_fallback_mode_string = "auto";
172static const char *arm_force_mode_string = "auto";
173
f32bf4a4
YQ
174/* The standard register names, and all the valid aliases for them. Note
175 that `fp', `sp' and `pc' are not added in this alias list, because they
176 have been added as builtin user registers in
177 std-regs.c:_initialize_frame_reg. */
123dc839
DJ
178static const struct
179{
180 const char *name;
181 int regnum;
182} arm_register_aliases[] = {
183 /* Basic register numbers. */
184 { "r0", 0 },
185 { "r1", 1 },
186 { "r2", 2 },
187 { "r3", 3 },
188 { "r4", 4 },
189 { "r5", 5 },
190 { "r6", 6 },
191 { "r7", 7 },
192 { "r8", 8 },
193 { "r9", 9 },
194 { "r10", 10 },
195 { "r11", 11 },
196 { "r12", 12 },
197 { "r13", 13 },
198 { "r14", 14 },
199 { "r15", 15 },
200 /* Synonyms (argument and variable registers). */
201 { "a1", 0 },
202 { "a2", 1 },
203 { "a3", 2 },
204 { "a4", 3 },
205 { "v1", 4 },
206 { "v2", 5 },
207 { "v3", 6 },
208 { "v4", 7 },
209 { "v5", 8 },
210 { "v6", 9 },
211 { "v7", 10 },
212 { "v8", 11 },
213 /* Other platform-specific names for r9. */
214 { "sb", 9 },
215 { "tr", 9 },
216 /* Special names. */
217 { "ip", 12 },
123dc839 218 { "lr", 14 },
123dc839
DJ
219 /* Names used by GCC (not listed in the ARM EABI). */
220 { "sl", 10 },
123dc839
DJ
221 /* A special name from the older ATPCS. */
222 { "wr", 7 },
223};
bc90b915 224
123dc839 225static const char *const arm_register_names[] =
da59e081
JM
226{"r0", "r1", "r2", "r3", /* 0 1 2 3 */
227 "r4", "r5", "r6", "r7", /* 4 5 6 7 */
228 "r8", "r9", "r10", "r11", /* 8 9 10 11 */
229 "r12", "sp", "lr", "pc", /* 12 13 14 15 */
230 "f0", "f1", "f2", "f3", /* 16 17 18 19 */
231 "f4", "f5", "f6", "f7", /* 20 21 22 23 */
94c30b78 232 "fps", "cpsr" }; /* 24 25 */
ed9a39eb 233
65b48a81
PB
234/* Holds the current set of options to be passed to the disassembler. */
235static char *arm_disassembler_options;
236
afd7eef0
RE
237/* Valid register name styles. */
238static const char **valid_disassembly_styles;
ed9a39eb 239
afd7eef0
RE
240/* Disassembly style to use. Default to "std" register names. */
241static const char *disassembly_style;
96baa820 242
d105cce5 243/* All possible arm target descriptors. */
92d48a1e 244static struct target_desc *tdesc_arm_list[ARM_FP_TYPE_INVALID][2];
d105cce5
AH
245static struct target_desc *tdesc_arm_mprofile_list[ARM_M_TYPE_INVALID];
246
ed9a39eb 247/* This is used to keep the bfd arch_info in sync with the disassembly
afd7eef0 248 style. */
eb4c3f4a 249static void set_disassembly_style_sfunc (const char *, int,
ed9a39eb 250 struct cmd_list_element *);
65b48a81
PB
251static void show_disassembly_style_sfunc (struct ui_file *, int,
252 struct cmd_list_element *,
253 const char *);
ed9a39eb 254
05d1431c 255static enum register_status arm_neon_quad_read (struct gdbarch *gdbarch,
849d0ba8 256 readable_regcache *regcache,
05d1431c 257 int regnum, gdb_byte *buf);
58d6951d
DJ
258static void arm_neon_quad_write (struct gdbarch *gdbarch,
259 struct regcache *regcache,
260 int regnum, const gdb_byte *buf);
261
e7cf25a8 262static CORE_ADDR
553cb527 263 arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self);
e7cf25a8
YQ
264
265
d9311bfa
AT
266/* get_next_pcs operations. */
267static struct arm_get_next_pcs_ops arm_get_next_pcs_ops = {
268 arm_get_next_pcs_read_memory_unsigned_integer,
269 arm_get_next_pcs_syscall_next_pc,
270 arm_get_next_pcs_addr_bits_remove,
ed443b61
YQ
271 arm_get_next_pcs_is_thumb,
272 NULL,
d9311bfa
AT
273};
274
9b8d791a 275struct arm_prologue_cache
c3b4394c 276{
eb5492fa
DJ
277 /* The stack pointer at the time this frame was created; i.e. the
278 caller's stack pointer when this function was called. It is used
279 to identify this frame. */
ae7e2f45
CL
280 CORE_ADDR sp;
281
282 /* Additional stack pointers used by M-profile with Security extension. */
283 /* Use msp_s / psp_s to hold the values of msp / psp when there is
284 no Security extension. */
285 CORE_ADDR msp_s;
286 CORE_ADDR msp_ns;
287 CORE_ADDR psp_s;
288 CORE_ADDR psp_ns;
289
290 /* Active stack pointer. */
291 int active_sp_regnum;
0d12d61b
YR
292 int active_msp_regnum;
293 int active_psp_regnum;
eb5492fa 294
4be43953
DJ
295 /* The frame base for this frame is just prev_sp - frame size.
296 FRAMESIZE is the distance from the frame pointer to the
297 initial stack pointer. */
eb5492fa 298
c3b4394c 299 int framesize;
eb5492fa
DJ
300
301 /* The register used to hold the frame pointer for this frame. */
c3b4394c 302 int framereg;
eb5492fa 303
a01567f4
LM
304 /* True if the return address is signed, false otherwise. */
305 gdb::optional<bool> ra_signed_state;
306
eb5492fa 307 /* Saved register offsets. */
098caef4 308 trad_frame_saved_reg *saved_regs;
0824193f
CL
309
310 arm_prologue_cache() = default;
c3b4394c 311};
ed9a39eb 312
8c9ae6df
YR
313
314/* Reconstruct T bit in program status register from LR value. */
315
316static inline ULONGEST
317reconstruct_t_bit(struct gdbarch *gdbarch, CORE_ADDR lr, ULONGEST psr)
318{
319 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
320 if (IS_THUMB_ADDR (lr))
321 psr |= t_bit;
322 else
323 psr &= ~t_bit;
324
325 return psr;
326}
327
0824193f
CL
328/* Initialize CACHE fields for which zero is not adequate (CACHE is
329 expected to have been ZALLOC'ed before calling this function). */
330
331static void
332arm_cache_init (struct arm_prologue_cache *cache, struct gdbarch *gdbarch)
333{
ae7e2f45
CL
334 cache->active_sp_regnum = ARM_SP_REGNUM;
335
0824193f
CL
336 cache->saved_regs = trad_frame_alloc_saved_regs (gdbarch);
337}
338
339/* Similar to the previous function, but extracts GDBARCH from FRAME. */
340
341static void
bd2b40ac 342arm_cache_init (struct arm_prologue_cache *cache, frame_info_ptr frame)
0824193f
CL
343{
344 struct gdbarch *gdbarch = get_frame_arch (frame);
08106042 345 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
0824193f
CL
346
347 arm_cache_init (cache, gdbarch);
fe642a5b 348 cache->sp = get_frame_register_unsigned (frame, ARM_SP_REGNUM);
ae7e2f45
CL
349
350 if (tdep->have_sec_ext)
351 {
b0b23e06
TS
352 const CORE_ADDR msp_val
353 = get_frame_register_unsigned (frame, tdep->m_profile_msp_regnum);
354 const CORE_ADDR psp_val
355 = get_frame_register_unsigned (frame, tdep->m_profile_psp_regnum);
356
357 cache->msp_s
358 = get_frame_register_unsigned (frame, tdep->m_profile_msp_s_regnum);
359 cache->msp_ns
360 = get_frame_register_unsigned (frame, tdep->m_profile_msp_ns_regnum);
361 cache->psp_s
362 = get_frame_register_unsigned (frame, tdep->m_profile_psp_s_regnum);
363 cache->psp_ns
364 = get_frame_register_unsigned (frame, tdep->m_profile_psp_ns_regnum);
365
366 /* Identify what msp is alias for (msp_s or msp_ns). */
0d12d61b
YR
367 if (msp_val == cache->msp_s)
368 cache->active_msp_regnum = tdep->m_profile_msp_s_regnum;
369 else if (msp_val == cache->msp_ns)
370 cache->active_msp_regnum = tdep->m_profile_msp_ns_regnum;
b0b23e06
TS
371 else
372 {
373 warning (_("Invalid state, unable to determine msp alias, assuming "
374 "msp_s."));
375 cache->active_msp_regnum = tdep->m_profile_msp_s_regnum;
376 }
377
378 /* Identify what psp is alias for (psp_s or psp_ns). */
0d12d61b
YR
379 if (psp_val == cache->psp_s)
380 cache->active_psp_regnum = tdep->m_profile_psp_s_regnum;
381 else if (psp_val == cache->psp_ns)
382 cache->active_psp_regnum = tdep->m_profile_psp_ns_regnum;
b0b23e06
TS
383 else
384 {
385 warning (_("Invalid state, unable to determine psp alias, assuming "
386 "psp_s."));
387 cache->active_psp_regnum = tdep->m_profile_psp_s_regnum;
388 }
389
390 /* Identify what sp is alias for (msp_s, msp_ns, psp_s or psp_ns). */
391 if (msp_val == cache->sp)
392 cache->active_sp_regnum = cache->active_msp_regnum;
393 else if (psp_val == cache->sp)
394 cache->active_sp_regnum = cache->active_psp_regnum;
395 else
396 {
397 warning (_("Invalid state, unable to determine sp alias, assuming "
398 "msp."));
399 cache->active_sp_regnum = cache->active_msp_regnum;
400 }
ae7e2f45
CL
401 }
402 else if (tdep->is_m)
403 {
b0b23e06 404 cache->msp_s
f3f7ecc9 405 = get_frame_register_unsigned (frame, tdep->m_profile_msp_regnum);
b0b23e06 406 cache->psp_s
f3f7ecc9 407 = get_frame_register_unsigned (frame, tdep->m_profile_psp_regnum);
b0b23e06
TS
408
409 /* Identify what sp is alias for (msp or psp). */
410 if (cache->msp_s == cache->sp)
411 cache->active_sp_regnum = tdep->m_profile_msp_regnum;
412 else if (cache->psp_s == cache->sp)
413 cache->active_sp_regnum = tdep->m_profile_psp_regnum;
414 else
415 {
416 warning (_("Invalid state, unable to determine sp alias, assuming "
417 "msp."));
418 cache->active_sp_regnum = tdep->m_profile_msp_regnum;
419 }
ae7e2f45
CL
420 }
421 else
b0b23e06
TS
422 {
423 cache->msp_s
424 = get_frame_register_unsigned (frame, ARM_SP_REGNUM);
425
426 cache->active_sp_regnum = ARM_SP_REGNUM;
427 }
ae7e2f45
CL
428}
429
430/* Return the requested stack pointer value (in REGNUM), taking into
431 account whether we have a Security extension or an M-profile
432 CPU. */
433
434static CORE_ADDR
435arm_cache_get_sp_register (struct arm_prologue_cache *cache,
436 arm_gdbarch_tdep *tdep, int regnum)
437{
ae7e2f45
CL
438 if (tdep->have_sec_ext)
439 {
440 if (regnum == tdep->m_profile_msp_s_regnum)
441 return cache->msp_s;
442 if (regnum == tdep->m_profile_msp_ns_regnum)
443 return cache->msp_ns;
444 if (regnum == tdep->m_profile_psp_s_regnum)
445 return cache->psp_s;
446 if (regnum == tdep->m_profile_psp_ns_regnum)
447 return cache->psp_ns;
0d12d61b
YR
448 if (regnum == tdep->m_profile_msp_regnum)
449 return arm_cache_get_sp_register (cache, tdep, cache->active_msp_regnum);
450 if (regnum == tdep->m_profile_psp_regnum)
451 return arm_cache_get_sp_register (cache, tdep, cache->active_psp_regnum);
b9b66a3a
YR
452 if (regnum == ARM_SP_REGNUM)
453 return arm_cache_get_sp_register (cache, tdep, cache->active_sp_regnum);
ae7e2f45
CL
454 }
455 else if (tdep->is_m)
456 {
457 if (regnum == tdep->m_profile_msp_regnum)
458 return cache->msp_s;
459 if (regnum == tdep->m_profile_psp_regnum)
460 return cache->psp_s;
b9b66a3a
YR
461 if (regnum == ARM_SP_REGNUM)
462 return arm_cache_get_sp_register (cache, tdep, cache->active_sp_regnum);
ae7e2f45 463 }
b9b66a3a
YR
464 else if (regnum == ARM_SP_REGNUM)
465 return cache->sp;
ae7e2f45
CL
466
467 gdb_assert_not_reached ("Invalid SP selection");
468}
469
470/* Return the previous stack address, depending on which SP register
471 is active. */
472
473static CORE_ADDR
474arm_cache_get_prev_sp_value (struct arm_prologue_cache *cache, arm_gdbarch_tdep *tdep)
475{
476 CORE_ADDR val = arm_cache_get_sp_register (cache, tdep, cache->active_sp_regnum);
477 return val;
478}
479
480/* Set the active stack pointer to VAL. */
481
482static void
483arm_cache_set_active_sp_value (struct arm_prologue_cache *cache,
484 arm_gdbarch_tdep *tdep, CORE_ADDR val)
485{
ae7e2f45
CL
486 if (tdep->have_sec_ext)
487 {
488 if (cache->active_sp_regnum == tdep->m_profile_msp_s_regnum)
489 cache->msp_s = val;
490 else if (cache->active_sp_regnum == tdep->m_profile_msp_ns_regnum)
491 cache->msp_ns = val;
492 else if (cache->active_sp_regnum == tdep->m_profile_psp_s_regnum)
493 cache->psp_s = val;
494 else if (cache->active_sp_regnum == tdep->m_profile_psp_ns_regnum)
495 cache->psp_ns = val;
496
497 return;
498 }
499 else if (tdep->is_m)
500 {
501 if (cache->active_sp_regnum == tdep->m_profile_msp_regnum)
502 cache->msp_s = val;
503 else if (cache->active_sp_regnum == tdep->m_profile_psp_regnum)
504 cache->psp_s = val;
505
506 return;
507 }
b9b66a3a
YR
508 else if (cache->active_sp_regnum == ARM_SP_REGNUM)
509 {
510 cache->sp = val;
511 return;
512 }
ae7e2f45
CL
513
514 gdb_assert_not_reached ("Invalid SP selection");
0824193f
CL
515}
516
d65edaa0 517/* Return true if REGNUM is one of the alternative stack pointers. */
ef273377
CL
518
519static bool
d65edaa0 520arm_is_alternative_sp_register (arm_gdbarch_tdep *tdep, int regnum)
ef273377 521{
d65edaa0 522 if ((regnum == tdep->m_profile_msp_regnum)
ef273377
CL
523 || (regnum == tdep->m_profile_msp_s_regnum)
524 || (regnum == tdep->m_profile_msp_ns_regnum)
525 || (regnum == tdep->m_profile_psp_regnum)
526 || (regnum == tdep->m_profile_psp_s_regnum)
527 || (regnum == tdep->m_profile_psp_ns_regnum))
528 return true;
529 else
530 return false;
531}
532
533/* Set the active stack pointer to SP_REGNUM. */
534
535static void
536arm_cache_switch_prev_sp (struct arm_prologue_cache *cache,
537 arm_gdbarch_tdep *tdep, int sp_regnum)
538{
d65edaa0 539 gdb_assert (arm_is_alternative_sp_register (tdep, sp_regnum));
ef273377
CL
540
541 if (tdep->have_sec_ext)
4d9fd868
TS
542 {
543 gdb_assert (sp_regnum != tdep->m_profile_msp_regnum
544 && sp_regnum != tdep->m_profile_psp_regnum);
545
546 if (sp_regnum == tdep->m_profile_msp_s_regnum
547 || sp_regnum == tdep->m_profile_psp_s_regnum)
548 {
549 cache->active_msp_regnum = tdep->m_profile_msp_s_regnum;
550 cache->active_psp_regnum = tdep->m_profile_psp_s_regnum;
551 }
552 else if (sp_regnum == tdep->m_profile_msp_ns_regnum
553 || sp_regnum == tdep->m_profile_psp_ns_regnum)
554 {
555 cache->active_msp_regnum = tdep->m_profile_msp_ns_regnum;
556 cache->active_psp_regnum = tdep->m_profile_psp_ns_regnum;
557 }
558 }
ef273377
CL
559
560 cache->active_sp_regnum = sp_regnum;
561}
562
9ecab40c
SM
563namespace {
564
565/* Abstract class to read ARM instructions from memory. */
566
567class arm_instruction_reader
568{
569public:
2c5b1849 570 /* Read a 4 bytes instruction from memory using the BYTE_ORDER endianness. */
9ecab40c
SM
571 virtual uint32_t read (CORE_ADDR memaddr, bfd_endian byte_order) const = 0;
572};
573
574/* Read instructions from target memory. */
575
576class target_arm_instruction_reader : public arm_instruction_reader
577{
578public:
579 uint32_t read (CORE_ADDR memaddr, bfd_endian byte_order) const override
580 {
581 return read_code_unsigned_integer (memaddr, 4, byte_order);
582 }
583};
584
585} /* namespace */
586
587static CORE_ADDR arm_analyze_prologue
588 (struct gdbarch *gdbarch, CORE_ADDR prologue_start, CORE_ADDR prologue_end,
589 struct arm_prologue_cache *cache, const arm_instruction_reader &insn_reader);
0d39a070 590
cca44b1b
JB
591/* Architecture version for displaced stepping. This effects the behaviour of
592 certain instructions, and really should not be hard-wired. */
593
594#define DISPLACED_STEPPING_ARCH_VERSION 5
595
c7ae7675 596/* See arm-tdep.h. */
c906108c 597
491144b5 598bool arm_apcs_32 = true;
ef273377 599bool arm_unwind_secure_frames = true;
c906108c 600
9779414d
DJ
601/* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode. */
602
478fd957 603int
9779414d
DJ
604arm_psr_thumb_bit (struct gdbarch *gdbarch)
605{
08106042 606 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
345bd07c
SM
607
608 if (tdep->is_m)
9779414d
DJ
609 return XPSR_T;
610 else
611 return CPSR_T;
612}
613
d0e59a68
AT
614/* Determine if the processor is currently executing in Thumb mode. */
615
616int
617arm_is_thumb (struct regcache *regcache)
618{
619 ULONGEST cpsr;
ac7936df 620 ULONGEST t_bit = arm_psr_thumb_bit (regcache->arch ());
d0e59a68
AT
621
622 cpsr = regcache_raw_get_unsigned (regcache, ARM_PS_REGNUM);
623
624 return (cpsr & t_bit) != 0;
625}
626
60270718
AB
627/* Determine if FRAME is executing in Thumb mode. FRAME must be an ARM
628 frame. */
b39cc962 629
25b41d01 630int
bd2b40ac 631arm_frame_is_thumb (frame_info_ptr frame)
b39cc962 632{
60270718
AB
633 /* Check the architecture of FRAME. */
634 struct gdbarch *gdbarch = get_frame_arch (frame);
635 gdb_assert (gdbarch_bfd_arch_info (gdbarch)->arch == bfd_arch_arm);
b39cc962
DJ
636
637 /* Every ARM frame unwinder can unwind the T bit of the CPSR, either
638 directly (from a signal frame or dummy frame) or by interpreting
639 the saved LR (from a prologue or DWARF frame). So consult it and
640 trust the unwinders. */
60270718 641 CORE_ADDR cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
b39cc962 642
60270718
AB
643 /* Find and extract the thumb bit. */
644 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
9779414d 645 return (cpsr & t_bit) != 0;
b39cc962
DJ
646}
647
f9d67f43
DJ
648/* Search for the mapping symbol covering MEMADDR. If one is found,
649 return its type. Otherwise, return 0. If START is non-NULL,
650 set *START to the location of the mapping symbol. */
c906108c 651
f9d67f43
DJ
652static char
653arm_find_mapping_symbol (CORE_ADDR memaddr, CORE_ADDR *start)
c906108c 654{
60c5725c 655 struct obj_section *sec;
0428b8f5 656
60c5725c
DJ
657 /* If there are mapping symbols, consult them. */
658 sec = find_pc_section (memaddr);
659 if (sec != NULL)
660 {
98badbfd 661 arm_per_bfd *data = arm_bfd_data_key.get (sec->objfile->obfd.get ());
60c5725c
DJ
662 if (data != NULL)
663 {
4838e44c
SM
664 unsigned int section_idx = sec->the_bfd_section->index;
665 arm_mapping_symbol_vec &map
666 = data->section_maps[section_idx];
667
668 /* Sort the vector on first use. */
669 if (!data->section_maps_sorted[section_idx])
670 {
671 std::sort (map.begin (), map.end ());
672 data->section_maps_sorted[section_idx] = true;
673 }
674
0c1bcd23 675 arm_mapping_symbol map_key = { memaddr - sec->addr (), 0 };
54cc7474
SM
676 arm_mapping_symbol_vec::const_iterator it
677 = std::lower_bound (map.begin (), map.end (), map_key);
678
679 /* std::lower_bound finds the earliest ordered insertion
680 point. If the symbol at this position starts at this exact
681 address, we use that; otherwise, the preceding
682 mapping symbol covers this address. */
683 if (it < map.end ())
60c5725c 684 {
54cc7474 685 if (it->value == map_key.value)
60c5725c 686 {
f9d67f43 687 if (start)
0c1bcd23 688 *start = it->value + sec->addr ();
54cc7474 689 return it->type;
60c5725c
DJ
690 }
691 }
54cc7474
SM
692
693 if (it > map.begin ())
694 {
695 arm_mapping_symbol_vec::const_iterator prev_it
696 = it - 1;
697
698 if (start)
0c1bcd23 699 *start = prev_it->value + sec->addr ();
54cc7474
SM
700 return prev_it->type;
701 }
60c5725c
DJ
702 }
703 }
704
f9d67f43
DJ
705 return 0;
706}
707
708/* Determine if the program counter specified in MEMADDR is in a Thumb
709 function. This function should be called for addresses unrelated to
710 any executing frame; otherwise, prefer arm_frame_is_thumb. */
711
e3039479 712int
9779414d 713arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
f9d67f43 714{
7cbd4a93 715 struct bound_minimal_symbol sym;
f9d67f43 716 char type;
187b041e 717 arm_displaced_step_copy_insn_closure *dsc = nullptr;
08106042 718 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
187b041e
SM
719
720 if (gdbarch_displaced_step_copy_insn_closure_by_addr_p (gdbarch))
721 dsc = ((arm_displaced_step_copy_insn_closure * )
722 gdbarch_displaced_step_copy_insn_closure_by_addr
723 (gdbarch, current_inferior (), memaddr));
a42244db
YQ
724
725 /* If checking the mode of displaced instruction in copy area, the mode
726 should be determined by instruction on the original address. */
727 if (dsc)
728 {
136821d9
SM
729 displaced_debug_printf ("check mode of %.8lx instead of %.8lx",
730 (unsigned long) dsc->insn_addr,
731 (unsigned long) memaddr);
a42244db
YQ
732 memaddr = dsc->insn_addr;
733 }
f9d67f43
DJ
734
735 /* If bit 0 of the address is set, assume this is a Thumb address. */
736 if (IS_THUMB_ADDR (memaddr))
737 return 1;
738
739 /* If the user wants to override the symbol table, let him. */
740 if (strcmp (arm_force_mode_string, "arm") == 0)
741 return 0;
742 if (strcmp (arm_force_mode_string, "thumb") == 0)
743 return 1;
744
9779414d 745 /* ARM v6-M and v7-M are always in Thumb mode. */
345bd07c 746 if (tdep->is_m)
9779414d
DJ
747 return 1;
748
f9d67f43
DJ
749 /* If there are mapping symbols, consult them. */
750 type = arm_find_mapping_symbol (memaddr, NULL);
751 if (type)
752 return type == 't';
753
ed9a39eb 754 /* Thumb functions have a "special" bit set in minimal symbols. */
c906108c 755 sym = lookup_minimal_symbol_by_pc (memaddr);
7cbd4a93
TT
756 if (sym.minsym)
757 return (MSYMBOL_IS_SPECIAL (sym.minsym));
0428b8f5
DJ
758
759 /* If the user wants to override the fallback mode, let them. */
760 if (strcmp (arm_fallback_mode_string, "arm") == 0)
761 return 0;
762 if (strcmp (arm_fallback_mode_string, "thumb") == 0)
763 return 1;
764
765 /* If we couldn't find any symbol, but we're talking to a running
766 target, then trust the current value of $cpsr. This lets
767 "display/i $pc" always show the correct mode (though if there is
768 a symbol table we will not reach here, so it still may not be
18819fa6 769 displayed in the mode it will be executed). */
9dccd06e 770 if (target_has_registers ())
18819fa6 771 return arm_frame_is_thumb (get_current_frame ());
0428b8f5
DJ
772
773 /* Otherwise we're out of luck; we assume ARM. */
774 return 0;
c906108c
SS
775}
776
8b73ee20
LM
777static inline bool
778arm_m_addr_is_lockup (CORE_ADDR addr)
779{
780 switch (addr)
781 {
782 /* Values for lockup state.
783 For more details see "B1.5.15 Unrecoverable exception cases" in
784 both ARMv6-M and ARMv7-M Architecture Reference Manuals, or
785 see "B4.32 Lockup" in ARMv8-M Architecture Reference Manual. */
786 case 0xeffffffe:
787 case 0xfffffffe:
788 case 0xffffffff:
789 return true;
790
791 default:
792 /* Address is not lockup. */
793 return false;
794 }
795}
796
ca90e760 797/* Determine if the address specified equals any of these magic return
55ea94da 798 values, called EXC_RETURN, defined by the ARM v6-M, v7-M and v8-M
8b73ee20
LM
799 architectures. Also include lockup magic PC value.
800 Check also for FNC_RETURN if we have the v8-M security extension.
ca90e760
FH
801
802 From ARMv6-M Reference Manual B1.5.8
803 Table B1-5 Exception return behavior
804
805 EXC_RETURN Return To Return Stack
806 0xFFFFFFF1 Handler mode Main
807 0xFFFFFFF9 Thread mode Main
808 0xFFFFFFFD Thread mode Process
809
810 From ARMv7-M Reference Manual B1.5.8
811 Table B1-8 EXC_RETURN definition of exception return behavior, no FP
812
813 EXC_RETURN Return To Return Stack
814 0xFFFFFFF1 Handler mode Main
815 0xFFFFFFF9 Thread mode Main
816 0xFFFFFFFD Thread mode Process
817
818 Table B1-9 EXC_RETURN definition of exception return behavior, with
819 FP
820
821 EXC_RETURN Return To Return Stack Frame Type
822 0xFFFFFFE1 Handler mode Main Extended
823 0xFFFFFFE9 Thread mode Main Extended
824 0xFFFFFFED Thread mode Process Extended
825 0xFFFFFFF1 Handler mode Main Basic
826 0xFFFFFFF9 Thread mode Main Basic
827 0xFFFFFFFD Thread mode Process Basic
828
829 For more details see "B1.5.8 Exception return behavior"
55ea94da
FH
830 in both ARMv6-M and ARMv7-M Architecture Reference Manuals.
831
8db533e7
TS
832 From ARMv8-M Architecture Technical Reference, D1.2.95
833 FType, Mode and SPSEL bits are to be considered when the Security
834 Extension is not implemented.
55ea94da 835
8db533e7
TS
836 EXC_RETURN Return To Return Stack Frame Type
837 0xFFFFFFA0 Handler mode Main Extended
838 0xFFFFFFA8 Thread mode Main Extended
839 0xFFFFFFAC Thread mode Process Extended
840 0xFFFFFFB0 Handler mode Main Standard
841 0xFFFFFFB8 Thread mode Main Standard
842 0xFFFFFFBC Thread mode Process Standard */
ca90e760
FH
843
844static int
ef273377
CL
845arm_m_addr_is_magic (struct gdbarch *gdbarch, CORE_ADDR addr)
846{
8b73ee20
LM
847 if (arm_m_addr_is_lockup (addr))
848 return 1;
849
08106042 850 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ef273377
CL
851 if (tdep->have_sec_ext)
852 {
853 switch ((addr & 0xff000000))
854 {
855 case 0xff000000: /* EXC_RETURN pattern. */
856 case 0xfe000000: /* FNC_RETURN pattern. */
857 return 1;
858 default:
859 return 0;
860 }
861 }
862 else
863 {
864 switch (addr)
865 {
866 /* Values from ARMv8-M Architecture Technical Reference. */
8db533e7
TS
867 case 0xffffffa0:
868 case 0xffffffa8:
869 case 0xffffffac:
ef273377
CL
870 case 0xffffffb0:
871 case 0xffffffb8:
872 case 0xffffffbc:
873 /* Values from Tables in B1.5.8 the EXC_RETURN definitions of
874 the exception return behavior. */
875 case 0xffffffe1:
876 case 0xffffffe9:
877 case 0xffffffed:
878 case 0xfffffff1:
879 case 0xfffffff9:
880 case 0xfffffffd:
881 /* Address is magic. */
882 return 1;
ca90e760 883
ef273377
CL
884 default:
885 /* Address is not magic. */
886 return 0;
887 }
ca90e760
FH
888 }
889}
890
181c1381 891/* Remove useless bits from addresses in a running program. */
34e8f22d 892static CORE_ADDR
24568a2c 893arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
c906108c 894{
08106042 895 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
345bd07c 896
2ae28aa9
YQ
897 /* On M-profile devices, do not strip the low bit from EXC_RETURN
898 (the magic exception return address). */
ef273377 899 if (tdep->is_m && arm_m_addr_is_magic (gdbarch, val))
2ae28aa9
YQ
900 return val;
901
a3a2ee65 902 if (arm_apcs_32)
dd6be234 903 return UNMAKE_THUMB_ADDR (val);
c906108c 904 else
a3a2ee65 905 return (val & 0x03fffffc);
c906108c
SS
906}
907
0d39a070 908/* Return 1 if PC is the start of a compiler helper function which
e0634ccf
UW
909 can be safely ignored during prologue skipping. IS_THUMB is true
910 if the function is known to be a Thumb function due to the way it
911 is being called. */
0d39a070 912static int
e0634ccf 913skip_prologue_function (struct gdbarch *gdbarch, CORE_ADDR pc, int is_thumb)
0d39a070 914{
e0634ccf 915 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7cbd4a93 916 struct bound_minimal_symbol msym;
0d39a070
DJ
917
918 msym = lookup_minimal_symbol_by_pc (pc);
7cbd4a93 919 if (msym.minsym != NULL
4aeddc50 920 && msym.value_address () == pc
c9d95fa3 921 && msym.minsym->linkage_name () != NULL)
e0634ccf 922 {
c9d95fa3 923 const char *name = msym.minsym->linkage_name ();
0d39a070 924
e0634ccf
UW
925 /* The GNU linker's Thumb call stub to foo is named
926 __foo_from_thumb. */
927 if (strstr (name, "_from_thumb") != NULL)
928 name += 2;
0d39a070 929
e0634ccf
UW
930 /* On soft-float targets, __truncdfsf2 is called to convert promoted
931 arguments to their argument types in non-prototyped
932 functions. */
61012eef 933 if (startswith (name, "__truncdfsf2"))
e0634ccf 934 return 1;
61012eef 935 if (startswith (name, "__aeabi_d2f"))
e0634ccf 936 return 1;
0d39a070 937
e0634ccf 938 /* Internal functions related to thread-local storage. */
61012eef 939 if (startswith (name, "__tls_get_addr"))
e0634ccf 940 return 1;
61012eef 941 if (startswith (name, "__aeabi_read_tp"))
e0634ccf
UW
942 return 1;
943 }
944 else
945 {
946 /* If we run against a stripped glibc, we may be unable to identify
947 special functions by name. Check for one important case,
948 __aeabi_read_tp, by comparing the *code* against the default
949 implementation (this is hand-written ARM assembler in glibc). */
950
951 if (!is_thumb
198cd59d 952 && read_code_unsigned_integer (pc, 4, byte_order_for_code)
e0634ccf 953 == 0xe3e00a0f /* mov r0, #0xffff0fff */
198cd59d 954 && read_code_unsigned_integer (pc + 4, 4, byte_order_for_code)
e0634ccf
UW
955 == 0xe240f01f) /* sub pc, r0, #31 */
956 return 1;
957 }
ec3d575a 958
0d39a070
DJ
959 return 0;
960}
961
621c6d5b
YQ
962/* Extract the immediate from instruction movw/movt of encoding T. INSN1 is
963 the first 16-bit of instruction, and INSN2 is the second 16-bit of
964 instruction. */
965#define EXTRACT_MOVW_MOVT_IMM_T(insn1, insn2) \
966 ((bits ((insn1), 0, 3) << 12) \
967 | (bits ((insn1), 10, 10) << 11) \
968 | (bits ((insn2), 12, 14) << 8) \
969 | bits ((insn2), 0, 7))
970
971/* Extract the immediate from instruction movw/movt of encoding A. INSN is
972 the 32-bit instruction. */
973#define EXTRACT_MOVW_MOVT_IMM_A(insn) \
974 ((bits ((insn), 16, 19) << 12) \
975 | bits ((insn), 0, 11))
976
ec3d575a
UW
977/* Decode immediate value; implements ThumbExpandImmediate pseudo-op. */
978
979static unsigned int
980thumb_expand_immediate (unsigned int imm)
981{
982 unsigned int count = imm >> 7;
983
984 if (count < 8)
985 switch (count / 2)
986 {
987 case 0:
988 return imm & 0xff;
989 case 1:
990 return (imm & 0xff) | ((imm & 0xff) << 16);
991 case 2:
992 return ((imm & 0xff) << 8) | ((imm & 0xff) << 24);
993 case 3:
994 return (imm & 0xff) | ((imm & 0xff) << 8)
995 | ((imm & 0xff) << 16) | ((imm & 0xff) << 24);
996 }
997
998 return (0x80 | (imm & 0x7f)) << (32 - count);
999}
1000
540314bd
YQ
1001/* Return 1 if the 16-bit Thumb instruction INSN restores SP in
1002 epilogue, 0 otherwise. */
1003
1004static int
1005thumb_instruction_restores_sp (unsigned short insn)
1006{
1007 return (insn == 0x46bd /* mov sp, r7 */
1008 || (insn & 0xff80) == 0xb000 /* add sp, imm */
1009 || (insn & 0xfe00) == 0xbc00); /* pop <registers> */
1010}
1011
29d73ae4
DJ
1012/* Analyze a Thumb prologue, looking for a recognizable stack frame
1013 and frame pointer. Scan until we encounter a store that could
0d39a070
DJ
1014 clobber the stack frame unexpectedly, or an unknown instruction.
1015 Return the last address which is definitely safe to skip for an
1016 initial breakpoint. */
c906108c
SS
1017
1018static CORE_ADDR
29d73ae4
DJ
1019thumb_analyze_prologue (struct gdbarch *gdbarch,
1020 CORE_ADDR start, CORE_ADDR limit,
1021 struct arm_prologue_cache *cache)
c906108c 1022{
08106042 1023 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
0d39a070 1024 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
e17a4113 1025 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
29d73ae4
DJ
1026 int i;
1027 pv_t regs[16];
29d73ae4 1028 CORE_ADDR offset;
ec3d575a 1029 CORE_ADDR unrecognized_pc = 0;
da3c6d4a 1030
29d73ae4
DJ
1031 for (i = 0; i < 16; i++)
1032 regs[i] = pv_register (i, 0);
f7b7ed97 1033 pv_area stack (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
29d73ae4 1034
29d73ae4 1035 while (start < limit)
c906108c 1036 {
29d73ae4 1037 unsigned short insn;
a01567f4 1038 gdb::optional<bool> ra_signed_state;
29d73ae4 1039
198cd59d 1040 insn = read_code_unsigned_integer (start, 2, byte_order_for_code);
9d4fde75 1041
94c30b78 1042 if ((insn & 0xfe00) == 0xb400) /* push { rlist } */
da59e081 1043 {
29d73ae4
DJ
1044 int regno;
1045 int mask;
4be43953 1046
f7b7ed97 1047 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953 1048 break;
29d73ae4
DJ
1049
1050 /* Bits 0-7 contain a mask for registers R0-R7. Bit 8 says
1051 whether to save LR (R14). */
1052 mask = (insn & 0xff) | ((insn & 0x100) << 6);
1053
1054 /* Calculate offsets of saved R0-R7 and LR. */
1055 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
1056 if (mask & (1 << regno))
1057 {
29d73ae4
DJ
1058 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
1059 -4);
f7b7ed97 1060 stack.store (regs[ARM_SP_REGNUM], 4, regs[regno]);
29d73ae4 1061 }
da59e081 1062 }
1db01f22 1063 else if ((insn & 0xff80) == 0xb080) /* sub sp, #imm */
da59e081 1064 {
29d73ae4 1065 offset = (insn & 0x7f) << 2; /* get scaled offset */
1db01f22
YQ
1066 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
1067 -offset);
da59e081 1068 }
808f7ab1
YQ
1069 else if (thumb_instruction_restores_sp (insn))
1070 {
1071 /* Don't scan past the epilogue. */
1072 break;
1073 }
0d39a070
DJ
1074 else if ((insn & 0xf800) == 0xa800) /* add Rd, sp, #imm */
1075 regs[bits (insn, 8, 10)] = pv_add_constant (regs[ARM_SP_REGNUM],
1076 (insn & 0xff) << 2);
1077 else if ((insn & 0xfe00) == 0x1c00 /* add Rd, Rn, #imm */
1078 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
1079 regs[bits (insn, 0, 2)] = pv_add_constant (regs[bits (insn, 3, 5)],
1080 bits (insn, 6, 8));
1081 else if ((insn & 0xf800) == 0x3000 /* add Rd, #imm */
1082 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
1083 regs[bits (insn, 8, 10)] = pv_add_constant (regs[bits (insn, 8, 10)],
1084 bits (insn, 0, 7));
1085 else if ((insn & 0xfe00) == 0x1800 /* add Rd, Rn, Rm */
1086 && pv_is_register (regs[bits (insn, 6, 8)], ARM_SP_REGNUM)
1087 && pv_is_constant (regs[bits (insn, 3, 5)]))
1088 regs[bits (insn, 0, 2)] = pv_add (regs[bits (insn, 3, 5)],
1089 regs[bits (insn, 6, 8)]);
1090 else if ((insn & 0xff00) == 0x4400 /* add Rd, Rm */
1091 && pv_is_constant (regs[bits (insn, 3, 6)]))
1092 {
1093 int rd = (bit (insn, 7) << 3) + bits (insn, 0, 2);
1094 int rm = bits (insn, 3, 6);
1095 regs[rd] = pv_add (regs[rd], regs[rm]);
1096 }
29d73ae4 1097 else if ((insn & 0xff00) == 0x4600) /* mov hi, lo or mov lo, hi */
da59e081 1098 {
29d73ae4
DJ
1099 int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
1100 int src_reg = (insn & 0x78) >> 3;
1101 regs[dst_reg] = regs[src_reg];
da59e081 1102 }
29d73ae4 1103 else if ((insn & 0xf800) == 0x9000) /* str rd, [sp, #off] */
da59e081 1104 {
29d73ae4
DJ
1105 /* Handle stores to the stack. Normally pushes are used,
1106 but with GCC -mtpcs-frame, there may be other stores
1107 in the prologue to create the frame. */
1108 int regno = (insn >> 8) & 0x7;
1109 pv_t addr;
1110
1111 offset = (insn & 0xff) << 2;
1112 addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
1113
f7b7ed97 1114 if (stack.store_would_trash (addr))
29d73ae4
DJ
1115 break;
1116
f7b7ed97 1117 stack.store (addr, 4, regs[regno]);
da59e081 1118 }
0d39a070
DJ
1119 else if ((insn & 0xf800) == 0x6000) /* str rd, [rn, #off] */
1120 {
1121 int rd = bits (insn, 0, 2);
1122 int rn = bits (insn, 3, 5);
1123 pv_t addr;
1124
1125 offset = bits (insn, 6, 10) << 2;
1126 addr = pv_add_constant (regs[rn], offset);
1127
f7b7ed97 1128 if (stack.store_would_trash (addr))
0d39a070
DJ
1129 break;
1130
f7b7ed97 1131 stack.store (addr, 4, regs[rd]);
0d39a070
DJ
1132 }
1133 else if (((insn & 0xf800) == 0x7000 /* strb Rd, [Rn, #off] */
1134 || (insn & 0xf800) == 0x8000) /* strh Rd, [Rn, #off] */
1135 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
1136 /* Ignore stores of argument registers to the stack. */
1137 ;
1138 else if ((insn & 0xf800) == 0xc800 /* ldmia Rn!, { registers } */
1139 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
1140 /* Ignore block loads from the stack, potentially copying
1141 parameters from memory. */
1142 ;
1143 else if ((insn & 0xf800) == 0x9800 /* ldr Rd, [Rn, #immed] */
1144 || ((insn & 0xf800) == 0x6800 /* ldr Rd, [sp, #immed] */
1145 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM)))
1146 /* Similarly ignore single loads from the stack. */
1147 ;
1148 else if ((insn & 0xffc0) == 0x0000 /* lsls Rd, Rm, #0 */
1149 || (insn & 0xffc0) == 0x1c00) /* add Rd, Rn, #0 */
1150 /* Skip register copies, i.e. saves to another register
1151 instead of the stack. */
1152 ;
1153 else if ((insn & 0xf800) == 0x2000) /* movs Rd, #imm */
1154 /* Recognize constant loads; even with small stacks these are necessary
1155 on Thumb. */
1156 regs[bits (insn, 8, 10)] = pv_constant (bits (insn, 0, 7));
1157 else if ((insn & 0xf800) == 0x4800) /* ldr Rd, [pc, #imm] */
1158 {
1159 /* Constant pool loads, for the same reason. */
1160 unsigned int constant;
1161 CORE_ADDR loc;
1162
1163 loc = start + 4 + bits (insn, 0, 7) * 4;
1164 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1165 regs[bits (insn, 8, 10)] = pv_constant (constant);
1166 }
db24da6d 1167 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instructions. */
0d39a070 1168 {
0d39a070
DJ
1169 unsigned short inst2;
1170
198cd59d
YQ
1171 inst2 = read_code_unsigned_integer (start + 2, 2,
1172 byte_order_for_code);
a01567f4 1173 uint32_t whole_insn = (insn << 16) | inst2;
0d39a070
DJ
1174
1175 if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
1176 {
1177 /* BL, BLX. Allow some special function calls when
1178 skipping the prologue; GCC generates these before
1179 storing arguments to the stack. */
1180 CORE_ADDR nextpc;
1181 int j1, j2, imm1, imm2;
1182
1183 imm1 = sbits (insn, 0, 10);
1184 imm2 = bits (inst2, 0, 10);
1185 j1 = bit (inst2, 13);
1186 j2 = bit (inst2, 11);
1187
1188 offset = ((imm1 << 12) + (imm2 << 1));
1189 offset ^= ((!j2) << 22) | ((!j1) << 23);
1190
1191 nextpc = start + 4 + offset;
1192 /* For BLX make sure to clear the low bits. */
1193 if (bit (inst2, 12) == 0)
1194 nextpc = nextpc & 0xfffffffc;
1195
e0634ccf
UW
1196 if (!skip_prologue_function (gdbarch, nextpc,
1197 bit (inst2, 12) != 0))
0d39a070
DJ
1198 break;
1199 }
ec3d575a 1200
0963b4bd
MS
1201 else if ((insn & 0xffd0) == 0xe900 /* stmdb Rn{!},
1202 { registers } */
ec3d575a
UW
1203 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1204 {
1205 pv_t addr = regs[bits (insn, 0, 3)];
1206 int regno;
1207
f7b7ed97 1208 if (stack.store_would_trash (addr))
ec3d575a
UW
1209 break;
1210
1211 /* Calculate offsets of saved registers. */
1212 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
1213 if (inst2 & (1 << regno))
1214 {
1215 addr = pv_add_constant (addr, -4);
f7b7ed97 1216 stack.store (addr, 4, regs[regno]);
ec3d575a
UW
1217 }
1218
1219 if (insn & 0x0020)
1220 regs[bits (insn, 0, 3)] = addr;
1221 }
1222
fcaa1071
CL
1223 /* vstmdb Rn{!}, { D-registers } (aka vpush). */
1224 else if ((insn & 0xff20) == 0xed20
1225 && (inst2 & 0x0f00) == 0x0b00
1226 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1227 {
1228 /* Address SP points to. */
1229 pv_t addr = regs[bits (insn, 0, 3)];
1230
1231 /* Number of registers saved. */
1232 unsigned int number = bits (inst2, 0, 7) >> 1;
1233
1234 /* First register to save. */
1235 int vd = bits (inst2, 12, 15) | (bits (insn, 6, 6) << 4);
1236
1237 if (stack.store_would_trash (addr))
1238 break;
1239
1240 /* Calculate offsets of saved registers. */
1241 for (; number > 0; number--)
1242 {
1243 addr = pv_add_constant (addr, -8);
1244 stack.store (addr, 8, pv_register (ARM_D0_REGNUM
1245 + vd + number, 0));
1246 }
1247
1248 /* Writeback SP to account for the saved registers. */
1249 regs[bits (insn, 0, 3)] = addr;
1250 }
1251
0963b4bd
MS
1252 else if ((insn & 0xff50) == 0xe940 /* strd Rt, Rt2,
1253 [Rn, #+/-imm]{!} */
ec3d575a
UW
1254 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1255 {
1256 int regno1 = bits (inst2, 12, 15);
1257 int regno2 = bits (inst2, 8, 11);
1258 pv_t addr = regs[bits (insn, 0, 3)];
1259
1260 offset = inst2 & 0xff;
1261 if (insn & 0x0080)
1262 addr = pv_add_constant (addr, offset);
1263 else
1264 addr = pv_add_constant (addr, -offset);
1265
f7b7ed97 1266 if (stack.store_would_trash (addr))
ec3d575a
UW
1267 break;
1268
f7b7ed97
TT
1269 stack.store (addr, 4, regs[regno1]);
1270 stack.store (pv_add_constant (addr, 4),
1271 4, regs[regno2]);
ec3d575a
UW
1272
1273 if (insn & 0x0020)
1274 regs[bits (insn, 0, 3)] = addr;
1275 }
1276
1277 else if ((insn & 0xfff0) == 0xf8c0 /* str Rt,[Rn,+/-#imm]{!} */
1278 && (inst2 & 0x0c00) == 0x0c00
1279 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1280 {
1281 int regno = bits (inst2, 12, 15);
1282 pv_t addr = regs[bits (insn, 0, 3)];
1283
1284 offset = inst2 & 0xff;
1285 if (inst2 & 0x0200)
1286 addr = pv_add_constant (addr, offset);
1287 else
1288 addr = pv_add_constant (addr, -offset);
1289
f7b7ed97 1290 if (stack.store_would_trash (addr))
ec3d575a
UW
1291 break;
1292
f7b7ed97 1293 stack.store (addr, 4, regs[regno]);
ec3d575a
UW
1294
1295 if (inst2 & 0x0100)
1296 regs[bits (insn, 0, 3)] = addr;
1297 }
1298
1299 else if ((insn & 0xfff0) == 0xf8c0 /* str.w Rt,[Rn,#imm] */
1300 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1301 {
1302 int regno = bits (inst2, 12, 15);
1303 pv_t addr;
1304
1305 offset = inst2 & 0xfff;
1306 addr = pv_add_constant (regs[bits (insn, 0, 3)], offset);
1307
f7b7ed97 1308 if (stack.store_would_trash (addr))
ec3d575a
UW
1309 break;
1310
f7b7ed97 1311 stack.store (addr, 4, regs[regno]);
ec3d575a
UW
1312 }
1313
1314 else if ((insn & 0xffd0) == 0xf880 /* str{bh}.w Rt,[Rn,#imm] */
0d39a070 1315 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 1316 /* Ignore stores of argument registers to the stack. */
0d39a070 1317 ;
ec3d575a
UW
1318
1319 else if ((insn & 0xffd0) == 0xf800 /* str{bh} Rt,[Rn,#+/-imm] */
1320 && (inst2 & 0x0d00) == 0x0c00
0d39a070 1321 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 1322 /* Ignore stores of argument registers to the stack. */
0d39a070 1323 ;
ec3d575a 1324
0963b4bd
MS
1325 else if ((insn & 0xffd0) == 0xe890 /* ldmia Rn[!],
1326 { registers } */
ec3d575a
UW
1327 && (inst2 & 0x8000) == 0x0000
1328 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1329 /* Ignore block loads from the stack, potentially copying
1330 parameters from memory. */
0d39a070 1331 ;
ec3d575a 1332
f8c6d152 1333 else if ((insn & 0xff70) == 0xe950 /* ldrd Rt, Rt2,
0963b4bd 1334 [Rn, #+/-imm] */
0d39a070 1335 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 1336 /* Similarly ignore dual loads from the stack. */
0d39a070 1337 ;
ec3d575a
UW
1338
1339 else if ((insn & 0xfff0) == 0xf850 /* ldr Rt,[Rn,#+/-imm] */
1340 && (inst2 & 0x0d00) == 0x0c00
0d39a070 1341 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 1342 /* Similarly ignore single loads from the stack. */
0d39a070 1343 ;
ec3d575a
UW
1344
1345 else if ((insn & 0xfff0) == 0xf8d0 /* ldr.w Rt,[Rn,#imm] */
0d39a070 1346 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 1347 /* Similarly ignore single loads from the stack. */
0d39a070 1348 ;
ec3d575a
UW
1349
1350 else if ((insn & 0xfbf0) == 0xf100 /* add.w Rd, Rn, #imm */
1351 && (inst2 & 0x8000) == 0x0000)
1352 {
1353 unsigned int imm = ((bits (insn, 10, 10) << 11)
1354 | (bits (inst2, 12, 14) << 8)
1355 | bits (inst2, 0, 7));
1356
1357 regs[bits (inst2, 8, 11)]
1358 = pv_add_constant (regs[bits (insn, 0, 3)],
1359 thumb_expand_immediate (imm));
1360 }
1361
1362 else if ((insn & 0xfbf0) == 0xf200 /* addw Rd, Rn, #imm */
1363 && (inst2 & 0x8000) == 0x0000)
0d39a070 1364 {
ec3d575a
UW
1365 unsigned int imm = ((bits (insn, 10, 10) << 11)
1366 | (bits (inst2, 12, 14) << 8)
1367 | bits (inst2, 0, 7));
1368
1369 regs[bits (inst2, 8, 11)]
1370 = pv_add_constant (regs[bits (insn, 0, 3)], imm);
1371 }
1372
1373 else if ((insn & 0xfbf0) == 0xf1a0 /* sub.w Rd, Rn, #imm */
1374 && (inst2 & 0x8000) == 0x0000)
1375 {
1376 unsigned int imm = ((bits (insn, 10, 10) << 11)
1377 | (bits (inst2, 12, 14) << 8)
1378 | bits (inst2, 0, 7));
1379
1380 regs[bits (inst2, 8, 11)]
1381 = pv_add_constant (regs[bits (insn, 0, 3)],
1382 - (CORE_ADDR) thumb_expand_immediate (imm));
1383 }
1384
1385 else if ((insn & 0xfbf0) == 0xf2a0 /* subw Rd, Rn, #imm */
1386 && (inst2 & 0x8000) == 0x0000)
1387 {
1388 unsigned int imm = ((bits (insn, 10, 10) << 11)
1389 | (bits (inst2, 12, 14) << 8)
1390 | bits (inst2, 0, 7));
1391
1392 regs[bits (inst2, 8, 11)]
1393 = pv_add_constant (regs[bits (insn, 0, 3)], - (CORE_ADDR) imm);
1394 }
1395
1396 else if ((insn & 0xfbff) == 0xf04f) /* mov.w Rd, #const */
1397 {
1398 unsigned int imm = ((bits (insn, 10, 10) << 11)
1399 | (bits (inst2, 12, 14) << 8)
1400 | bits (inst2, 0, 7));
1401
1402 regs[bits (inst2, 8, 11)]
1403 = pv_constant (thumb_expand_immediate (imm));
1404 }
1405
1406 else if ((insn & 0xfbf0) == 0xf240) /* movw Rd, #const */
1407 {
621c6d5b
YQ
1408 unsigned int imm
1409 = EXTRACT_MOVW_MOVT_IMM_T (insn, inst2);
ec3d575a
UW
1410
1411 regs[bits (inst2, 8, 11)] = pv_constant (imm);
1412 }
1413
1414 else if (insn == 0xea5f /* mov.w Rd,Rm */
1415 && (inst2 & 0xf0f0) == 0)
1416 {
1417 int dst_reg = (inst2 & 0x0f00) >> 8;
1418 int src_reg = inst2 & 0xf;
1419 regs[dst_reg] = regs[src_reg];
1420 }
1421
1422 else if ((insn & 0xff7f) == 0xf85f) /* ldr.w Rt,<label> */
1423 {
1424 /* Constant pool loads. */
1425 unsigned int constant;
1426 CORE_ADDR loc;
1427
cac395ea 1428 offset = bits (inst2, 0, 11);
ec3d575a
UW
1429 if (insn & 0x0080)
1430 loc = start + 4 + offset;
1431 else
1432 loc = start + 4 - offset;
1433
1434 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1435 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1436 }
1437
1438 else if ((insn & 0xff7f) == 0xe95f) /* ldrd Rt,Rt2,<label> */
1439 {
1440 /* Constant pool loads. */
1441 unsigned int constant;
1442 CORE_ADDR loc;
1443
cac395ea 1444 offset = bits (inst2, 0, 7) << 2;
ec3d575a
UW
1445 if (insn & 0x0080)
1446 loc = start + 4 + offset;
1447 else
1448 loc = start + 4 - offset;
1449
1450 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1451 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1452
1453 constant = read_memory_unsigned_integer (loc + 4, 4, byte_order);
1454 regs[bits (inst2, 8, 11)] = pv_constant (constant);
1455 }
a01567f4
LM
1456 /* Start of ARMv8.1-m PACBTI extension instructions. */
1457 else if (IS_PAC (whole_insn))
1458 {
1459 /* LR and SP are input registers. PAC is in R12. LR is
1460 signed from this point onwards. NOP space. */
1461 ra_signed_state = true;
1462 }
1463 else if (IS_PACBTI (whole_insn))
1464 {
1465 /* LR and SP are input registers. PAC is in R12 and PC is a
1466 valid BTI landing pad. LR is signed from this point onwards.
1467 NOP space. */
1468 ra_signed_state = true;
1469 }
1470 else if (IS_BTI (whole_insn))
1471 {
1472 /* Valid BTI landing pad. NOP space. */
1473 }
1474 else if (IS_PACG (whole_insn))
1475 {
1476 /* Sign Rn using Rm and store the PAC in Rd. Rd is signed from
1477 this point onwards. */
1478 ra_signed_state = true;
1479 }
1480 else if (IS_AUT (whole_insn) || IS_AUTG (whole_insn))
1481 {
1482 /* These instructions appear close to the epilogue, when signed
1483 pointers are getting authenticated. */
1484 ra_signed_state = false;
1485 }
1486 /* End of ARMv8.1-m PACBTI extension instructions */
ec3d575a
UW
1487 else if (thumb2_instruction_changes_pc (insn, inst2))
1488 {
1489 /* Don't scan past anything that might change control flow. */
0d39a070
DJ
1490 break;
1491 }
ec3d575a
UW
1492 else
1493 {
1494 /* The optimizer might shove anything into the prologue,
1495 so we just skip what we don't recognize. */
1496 unrecognized_pc = start;
1497 }
0d39a070 1498
a01567f4
LM
1499 /* Make sure we are dealing with a target that supports ARMv8.1-m
1500 PACBTI. */
1501 if (cache != nullptr && tdep->have_pacbti
1502 && ra_signed_state.has_value ())
1503 {
1504 arm_debug_printf ("Found pacbti instruction at %s",
1505 paddress (gdbarch, start));
1506 arm_debug_printf ("RA is %s",
05d63baf 1507 *ra_signed_state ? "signed" : "not signed");
a01567f4
LM
1508 cache->ra_signed_state = ra_signed_state;
1509 }
1510
0d39a070
DJ
1511 start += 2;
1512 }
ec3d575a 1513 else if (thumb_instruction_changes_pc (insn))
3d74b771 1514 {
ec3d575a 1515 /* Don't scan past anything that might change control flow. */
da3c6d4a 1516 break;
3d74b771 1517 }
ec3d575a
UW
1518 else
1519 {
1520 /* The optimizer might shove anything into the prologue,
1521 so we just skip what we don't recognize. */
1522 unrecognized_pc = start;
1523 }
29d73ae4
DJ
1524
1525 start += 2;
c906108c
SS
1526 }
1527
7cb6d92a
SM
1528 arm_debug_printf ("Prologue scan stopped at %s",
1529 paddress (gdbarch, start));
0d39a070 1530
ec3d575a
UW
1531 if (unrecognized_pc == 0)
1532 unrecognized_pc = start;
1533
29d73ae4 1534 if (cache == NULL)
f7b7ed97 1535 return unrecognized_pc;
29d73ae4 1536
29d73ae4
DJ
1537 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1538 {
1539 /* Frame pointer is fp. Frame size is constant. */
1540 cache->framereg = ARM_FP_REGNUM;
1541 cache->framesize = -regs[ARM_FP_REGNUM].k;
1542 }
1543 else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
1544 {
1545 /* Frame pointer is r7. Frame size is constant. */
1546 cache->framereg = THUMB_FP_REGNUM;
1547 cache->framesize = -regs[THUMB_FP_REGNUM].k;
1548 }
72a2e3dc 1549 else
29d73ae4
DJ
1550 {
1551 /* Try the stack pointer... this is a bit desperate. */
1552 cache->framereg = ARM_SP_REGNUM;
1553 cache->framesize = -regs[ARM_SP_REGNUM].k;
1554 }
29d73ae4 1555
de76473c 1556 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
f7b7ed97 1557 if (stack.find_reg (gdbarch, i, &offset))
10245fe8
YR
1558 {
1559 cache->saved_regs[i].set_addr (offset);
1560 if (i == ARM_SP_REGNUM)
1561 arm_cache_set_active_sp_value(cache, tdep, offset);
1562 }
29d73ae4 1563
ec3d575a 1564 return unrecognized_pc;
c906108c
SS
1565}
1566
621c6d5b
YQ
1567
1568/* Try to analyze the instructions starting from PC, which load symbol
1569 __stack_chk_guard. Return the address of instruction after loading this
1570 symbol, set the dest register number to *BASEREG, and set the size of
1571 instructions for loading symbol in OFFSET. Return 0 if instructions are
1572 not recognized. */
1573
1574static CORE_ADDR
1575arm_analyze_load_stack_chk_guard(CORE_ADDR pc, struct gdbarch *gdbarch,
1576 unsigned int *destreg, int *offset)
1577{
1578 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1579 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1580 unsigned int low, high, address;
1581
1582 address = 0;
1583 if (is_thumb)
1584 {
1585 unsigned short insn1
198cd59d 1586 = read_code_unsigned_integer (pc, 2, byte_order_for_code);
621c6d5b
YQ
1587
1588 if ((insn1 & 0xf800) == 0x4800) /* ldr Rd, #immed */
1589 {
1590 *destreg = bits (insn1, 8, 10);
1591 *offset = 2;
6ae274b7
YQ
1592 address = (pc & 0xfffffffc) + 4 + (bits (insn1, 0, 7) << 2);
1593 address = read_memory_unsigned_integer (address, 4,
1594 byte_order_for_code);
621c6d5b
YQ
1595 }
1596 else if ((insn1 & 0xfbf0) == 0xf240) /* movw Rd, #const */
1597 {
1598 unsigned short insn2
198cd59d 1599 = read_code_unsigned_integer (pc + 2, 2, byte_order_for_code);
621c6d5b
YQ
1600
1601 low = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1602
1603 insn1
198cd59d 1604 = read_code_unsigned_integer (pc + 4, 2, byte_order_for_code);
621c6d5b 1605 insn2
198cd59d 1606 = read_code_unsigned_integer (pc + 6, 2, byte_order_for_code);
621c6d5b
YQ
1607
1608 /* movt Rd, #const */
1609 if ((insn1 & 0xfbc0) == 0xf2c0)
1610 {
1611 high = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1612 *destreg = bits (insn2, 8, 11);
1613 *offset = 8;
1614 address = (high << 16 | low);
1615 }
1616 }
1617 }
1618 else
1619 {
2e9e421f 1620 unsigned int insn
198cd59d 1621 = read_code_unsigned_integer (pc, 4, byte_order_for_code);
2e9e421f 1622
6ae274b7 1623 if ((insn & 0x0e5f0000) == 0x041f0000) /* ldr Rd, [PC, #immed] */
2e9e421f 1624 {
6ae274b7
YQ
1625 address = bits (insn, 0, 11) + pc + 8;
1626 address = read_memory_unsigned_integer (address, 4,
1627 byte_order_for_code);
1628
2e9e421f
UW
1629 *destreg = bits (insn, 12, 15);
1630 *offset = 4;
1631 }
1632 else if ((insn & 0x0ff00000) == 0x03000000) /* movw Rd, #const */
1633 {
1634 low = EXTRACT_MOVW_MOVT_IMM_A (insn);
1635
1636 insn
198cd59d 1637 = read_code_unsigned_integer (pc + 4, 4, byte_order_for_code);
2e9e421f
UW
1638
1639 if ((insn & 0x0ff00000) == 0x03400000) /* movt Rd, #const */
1640 {
1641 high = EXTRACT_MOVW_MOVT_IMM_A (insn);
1642 *destreg = bits (insn, 12, 15);
1643 *offset = 8;
1644 address = (high << 16 | low);
1645 }
1646 }
621c6d5b
YQ
1647 }
1648
1649 return address;
1650}
1651
1652/* Try to skip a sequence of instructions used for stack protector. If PC
0963b4bd
MS
1653 points to the first instruction of this sequence, return the address of
1654 first instruction after this sequence, otherwise, return original PC.
621c6d5b
YQ
1655
1656 On arm, this sequence of instructions is composed of mainly three steps,
1657 Step 1: load symbol __stack_chk_guard,
1658 Step 2: load from address of __stack_chk_guard,
1659 Step 3: store it to somewhere else.
1660
1661 Usually, instructions on step 2 and step 3 are the same on various ARM
1662 architectures. On step 2, it is one instruction 'ldr Rx, [Rn, #0]', and
1663 on step 3, it is also one instruction 'str Rx, [r7, #immd]'. However,
1664 instructions in step 1 vary from different ARM architectures. On ARMv7,
1665 they are,
1666
1667 movw Rn, #:lower16:__stack_chk_guard
1668 movt Rn, #:upper16:__stack_chk_guard
1669
1670 On ARMv5t, it is,
1671
1672 ldr Rn, .Label
1673 ....
1674 .Lable:
1675 .word __stack_chk_guard
1676
1677 Since ldr/str is a very popular instruction, we can't use them as
1678 'fingerprint' or 'signature' of stack protector sequence. Here we choose
1679 sequence {movw/movt, ldr}/ldr/str plus symbol __stack_chk_guard, if not
1680 stripped, as the 'fingerprint' of a stack protector cdoe sequence. */
1681
1682static CORE_ADDR
1683arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
1684{
1685 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
22e048c9 1686 unsigned int basereg;
7cbd4a93 1687 struct bound_minimal_symbol stack_chk_guard;
621c6d5b
YQ
1688 int offset;
1689 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1690 CORE_ADDR addr;
1691
1692 /* Try to parse the instructions in Step 1. */
1693 addr = arm_analyze_load_stack_chk_guard (pc, gdbarch,
1694 &basereg, &offset);
1695 if (!addr)
1696 return pc;
1697
1698 stack_chk_guard = lookup_minimal_symbol_by_pc (addr);
6041179a
JB
1699 /* ADDR must correspond to a symbol whose name is __stack_chk_guard.
1700 Otherwise, this sequence cannot be for stack protector. */
1701 if (stack_chk_guard.minsym == NULL
c9d95fa3 1702 || !startswith (stack_chk_guard.minsym->linkage_name (), "__stack_chk_guard"))
621c6d5b
YQ
1703 return pc;
1704
1705 if (is_thumb)
1706 {
1707 unsigned int destreg;
1708 unsigned short insn
198cd59d 1709 = read_code_unsigned_integer (pc + offset, 2, byte_order_for_code);
621c6d5b
YQ
1710
1711 /* Step 2: ldr Rd, [Rn, #immed], encoding T1. */
1712 if ((insn & 0xf800) != 0x6800)
1713 return pc;
1714 if (bits (insn, 3, 5) != basereg)
1715 return pc;
1716 destreg = bits (insn, 0, 2);
1717
198cd59d
YQ
1718 insn = read_code_unsigned_integer (pc + offset + 2, 2,
1719 byte_order_for_code);
621c6d5b
YQ
1720 /* Step 3: str Rd, [Rn, #immed], encoding T1. */
1721 if ((insn & 0xf800) != 0x6000)
1722 return pc;
1723 if (destreg != bits (insn, 0, 2))
1724 return pc;
1725 }
1726 else
1727 {
1728 unsigned int destreg;
1729 unsigned int insn
198cd59d 1730 = read_code_unsigned_integer (pc + offset, 4, byte_order_for_code);
621c6d5b
YQ
1731
1732 /* Step 2: ldr Rd, [Rn, #immed], encoding A1. */
1733 if ((insn & 0x0e500000) != 0x04100000)
1734 return pc;
1735 if (bits (insn, 16, 19) != basereg)
1736 return pc;
1737 destreg = bits (insn, 12, 15);
1738 /* Step 3: str Rd, [Rn, #immed], encoding A1. */
198cd59d 1739 insn = read_code_unsigned_integer (pc + offset + 4,
621c6d5b
YQ
1740 4, byte_order_for_code);
1741 if ((insn & 0x0e500000) != 0x04000000)
1742 return pc;
1743 if (bits (insn, 12, 15) != destreg)
1744 return pc;
1745 }
1746 /* The size of total two instructions ldr/str is 4 on Thumb-2, while 8
1747 on arm. */
1748 if (is_thumb)
1749 return pc + offset + 4;
1750 else
1751 return pc + offset + 8;
1752}
1753
da3c6d4a
MS
1754/* Advance the PC across any function entry prologue instructions to
1755 reach some "real" code.
34e8f22d
RE
1756
1757 The APCS (ARM Procedure Call Standard) defines the following
ed9a39eb 1758 prologue:
c906108c 1759
c5aa993b
JM
1760 mov ip, sp
1761 [stmfd sp!, {a1,a2,a3,a4}]
1762 stmfd sp!, {...,fp,ip,lr,pc}
ed9a39eb
JM
1763 [stfe f7, [sp, #-12]!]
1764 [stfe f6, [sp, #-12]!]
1765 [stfe f5, [sp, #-12]!]
1766 [stfe f4, [sp, #-12]!]
0963b4bd 1767 sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn. */
c906108c 1768
34e8f22d 1769static CORE_ADDR
6093d2eb 1770arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
c906108c 1771{
22f2cf64 1772 CORE_ADDR func_addr, func_end_addr, limit_pc;
c906108c 1773
a89fea3c
JL
1774 /* See if we can determine the end of the prologue via the symbol table.
1775 If so, then return either PC, or the PC after the prologue, whichever
1776 is greater. */
22f2cf64
TV
1777 bool func_addr_found
1778 = find_pc_partial_function (pc, NULL, &func_addr, &func_end_addr);
1779
1780 /* Whether the function is thumb mode or not. */
1781 bool func_is_thumb = false;
1782
1783 if (func_addr_found)
c906108c 1784 {
d80b854b
UW
1785 CORE_ADDR post_prologue_pc
1786 = skip_prologue_using_sal (gdbarch, func_addr);
43f3e411 1787 struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);
0d39a070 1788
621c6d5b
YQ
1789 if (post_prologue_pc)
1790 post_prologue_pc
1791 = arm_skip_stack_protector (post_prologue_pc, gdbarch);
1792
1793
0d39a070
DJ
1794 /* GCC always emits a line note before the prologue and another
1795 one after, even if the two are at the same address or on the
1796 same line. Take advantage of this so that we do not need to
1797 know every instruction that might appear in the prologue. We
1798 will have producer information for most binaries; if it is
1799 missing (e.g. for -gstabs), assuming the GNU tools. */
1800 if (post_prologue_pc
43f3e411 1801 && (cust == NULL
ab5f850e
SM
1802 || cust->producer () == NULL
1803 || startswith (cust->producer (), "GNU ")
1804 || producer_is_llvm (cust->producer ())))
0d39a070
DJ
1805 return post_prologue_pc;
1806
a89fea3c 1807 if (post_prologue_pc != 0)
0d39a070
DJ
1808 {
1809 CORE_ADDR analyzed_limit;
1810
1811 /* For non-GCC compilers, make sure the entire line is an
1812 acceptable prologue; GDB will round this function's
1813 return value up to the end of the following line so we
1814 can not skip just part of a line (and we do not want to).
1815
1816 RealView does not treat the prologue specially, but does
1817 associate prologue code with the opening brace; so this
1818 lets us skip the first line if we think it is the opening
1819 brace. */
22f2cf64
TV
1820 func_is_thumb = arm_pc_is_thumb (gdbarch, func_addr);
1821 if (func_is_thumb)
0d39a070
DJ
1822 analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
1823 post_prologue_pc, NULL);
1824 else
9ecab40c
SM
1825 analyzed_limit
1826 = arm_analyze_prologue (gdbarch, func_addr, post_prologue_pc,
1827 NULL, target_arm_instruction_reader ());
0d39a070
DJ
1828
1829 if (analyzed_limit != post_prologue_pc)
1830 return func_addr;
1831
1832 return post_prologue_pc;
1833 }
c906108c
SS
1834 }
1835
a89fea3c
JL
1836 /* Can't determine prologue from the symbol table, need to examine
1837 instructions. */
c906108c 1838
a89fea3c
JL
1839 /* Find an upper limit on the function prologue using the debug
1840 information. If the debug information could not be used to provide
1841 that bound, then use an arbitrary large number as the upper bound. */
0963b4bd 1842 /* Like arm_scan_prologue, stop no later than pc + 64. */
d80b854b 1843 limit_pc = skip_prologue_using_sal (gdbarch, pc);
a89fea3c
JL
1844 if (limit_pc == 0)
1845 limit_pc = pc + 64; /* Magic. */
1846
22f2cf64
TV
1847 /* Set the correct adjustment based on whether the function is thumb mode or
1848 not. We use it to get the address of the last instruction in the
1849 function (as opposed to the first address of the next function). */
05d63baf 1850 CORE_ADDR adjustment = func_is_thumb ? 2 : 4;
22f2cf64
TV
1851
1852 limit_pc
05d63baf
LM
1853 = func_end_addr == 0 ? limit_pc : std::min (limit_pc,
1854 func_end_addr - adjustment);
c906108c 1855
29d73ae4 1856 /* Check if this is Thumb code. */
9779414d 1857 if (arm_pc_is_thumb (gdbarch, pc))
a89fea3c 1858 return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
21daaaaf 1859 else
9ecab40c
SM
1860 return arm_analyze_prologue (gdbarch, pc, limit_pc, NULL,
1861 target_arm_instruction_reader ());
c906108c 1862}
94c30b78 1863
c906108c
SS
1864/* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
1865 This function decodes a Thumb function prologue to determine:
1866 1) the size of the stack frame
1867 2) which registers are saved on it
1868 3) the offsets of saved regs
1869 4) the offset from the stack pointer to the frame pointer
c906108c 1870
da59e081
JM
1871 A typical Thumb function prologue would create this stack frame
1872 (offsets relative to FP)
c906108c
SS
1873 old SP -> 24 stack parameters
1874 20 LR
1875 16 R7
1876 R7 -> 0 local variables (16 bytes)
1877 SP -> -12 additional stack space (12 bytes)
1878 The frame size would thus be 36 bytes, and the frame offset would be
0963b4bd 1879 12 bytes. The frame register is R7.
da59e081 1880
da3c6d4a
MS
1881 The comments for thumb_skip_prolog() describe the algorithm we use
1882 to detect the end of the prolog. */
c5aa993b 1883
c906108c 1884static void
be8626e0 1885thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
b39cc962 1886 CORE_ADDR block_addr, struct arm_prologue_cache *cache)
c906108c
SS
1887{
1888 CORE_ADDR prologue_start;
1889 CORE_ADDR prologue_end;
c906108c 1890
b39cc962
DJ
1891 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1892 &prologue_end))
c906108c 1893 {
ec3d575a
UW
1894 /* See comment in arm_scan_prologue for an explanation of
1895 this heuristics. */
1896 if (prologue_end > prologue_start + 64)
1897 {
1898 prologue_end = prologue_start + 64;
1899 }
c906108c
SS
1900 }
1901 else
f7060f85
DJ
1902 /* We're in the boondocks: we have no idea where the start of the
1903 function is. */
1904 return;
c906108c 1905
325fac50 1906 prologue_end = std::min (prologue_end, prev_pc);
c906108c 1907
be8626e0 1908 thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
1909}
1910
f303bc3e
YQ
1911/* Return 1 if the ARM instruction INSN restores SP in epilogue, 0
1912 otherwise. */
1913
1914static int
1915arm_instruction_restores_sp (unsigned int insn)
1916{
1917 if (bits (insn, 28, 31) != INST_NV)
1918 {
1919 if ((insn & 0x0df0f000) == 0x0080d000
1920 /* ADD SP (register or immediate). */
1921 || (insn & 0x0df0f000) == 0x0040d000
1922 /* SUB SP (register or immediate). */
1923 || (insn & 0x0ffffff0) == 0x01a0d000
1924 /* MOV SP. */
1925 || (insn & 0x0fff0000) == 0x08bd0000
1926 /* POP (LDMIA). */
1927 || (insn & 0x0fff0000) == 0x049d0000)
1928 /* POP of a single register. */
1929 return 1;
1930 }
1931
1932 return 0;
1933}
1934
9ecab40c
SM
1935/* Implement immediate value decoding, as described in section A5.2.4
1936 (Modified immediate constants in ARM instructions) of the ARM Architecture
1937 Reference Manual (ARMv7-A and ARMv7-R edition). */
1938
1939static uint32_t
1940arm_expand_immediate (uint32_t imm)
1941{
1942 /* Immediate values are 12 bits long. */
1943 gdb_assert ((imm & 0xfffff000) == 0);
1944
1945 uint32_t unrotated_value = imm & 0xff;
1946 uint32_t rotate_amount = (imm & 0xf00) >> 7;
1947
1948 if (rotate_amount == 0)
1949 return unrotated_value;
1950
1951 return ((unrotated_value >> rotate_amount)
1952 | (unrotated_value << (32 - rotate_amount)));
1953}
1954
0d39a070
DJ
1955/* Analyze an ARM mode prologue starting at PROLOGUE_START and
1956 continuing no further than PROLOGUE_END. If CACHE is non-NULL,
1957 fill it in. Return the first address not recognized as a prologue
1958 instruction.
eb5492fa 1959
0d39a070
DJ
1960 We recognize all the instructions typically found in ARM prologues,
1961 plus harmless instructions which can be skipped (either for analysis
1962 purposes, or a more restrictive set that can be skipped when finding
1963 the end of the prologue). */
1964
1965static CORE_ADDR
1966arm_analyze_prologue (struct gdbarch *gdbarch,
1967 CORE_ADDR prologue_start, CORE_ADDR prologue_end,
9ecab40c
SM
1968 struct arm_prologue_cache *cache,
1969 const arm_instruction_reader &insn_reader)
0d39a070 1970{
0d39a070
DJ
1971 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1972 int regno;
1973 CORE_ADDR offset, current_pc;
1974 pv_t regs[ARM_FPS_REGNUM];
0d39a070 1975 CORE_ADDR unrecognized_pc = 0;
08106042 1976 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
0d39a070
DJ
1977
1978 /* Search the prologue looking for instructions that set up the
96baa820 1979 frame pointer, adjust the stack pointer, and save registers.
ed9a39eb 1980
96baa820
JM
1981 Be careful, however, and if it doesn't look like a prologue,
1982 don't try to scan it. If, for instance, a frameless function
1983 begins with stmfd sp!, then we will tell ourselves there is
b8d5e71d 1984 a frame, which will confuse stack traceback, as well as "finish"
96baa820 1985 and other operations that rely on a knowledge of the stack
0d39a070 1986 traceback. */
d4473757 1987
4be43953
DJ
1988 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1989 regs[regno] = pv_register (regno, 0);
f7b7ed97 1990 pv_area stack (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
4be43953 1991
94c30b78
MS
1992 for (current_pc = prologue_start;
1993 current_pc < prologue_end;
f43845b3 1994 current_pc += 4)
96baa820 1995 {
9ecab40c 1996 uint32_t insn = insn_reader.read (current_pc, byte_order_for_code);
9d4fde75 1997
94c30b78 1998 if (insn == 0xe1a0c00d) /* mov ip, sp */
f43845b3 1999 {
4be43953 2000 regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
28cd8767
JG
2001 continue;
2002 }
0d39a070
DJ
2003 else if ((insn & 0xfff00000) == 0xe2800000 /* add Rd, Rn, #n */
2004 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767 2005 {
9ecab40c 2006 uint32_t imm = arm_expand_immediate (insn & 0xfff);
0d39a070 2007 int rd = bits (insn, 12, 15);
0d39a070 2008 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
28cd8767
JG
2009 continue;
2010 }
0d39a070
DJ
2011 else if ((insn & 0xfff00000) == 0xe2400000 /* sub Rd, Rn, #n */
2012 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767 2013 {
9ecab40c 2014 uint32_t imm = arm_expand_immediate (insn & 0xfff);
0d39a070 2015 int rd = bits (insn, 12, 15);
0d39a070 2016 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
f43845b3
MS
2017 continue;
2018 }
0963b4bd
MS
2019 else if ((insn & 0xffff0fff) == 0xe52d0004) /* str Rd,
2020 [sp, #-4]! */
f43845b3 2021 {
f7b7ed97 2022 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
2023 break;
2024 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
f7b7ed97
TT
2025 stack.store (regs[ARM_SP_REGNUM], 4,
2026 regs[bits (insn, 12, 15)]);
f43845b3
MS
2027 continue;
2028 }
2029 else if ((insn & 0xffff0000) == 0xe92d0000)
d4473757
KB
2030 /* stmfd sp!, {..., fp, ip, lr, pc}
2031 or
2032 stmfd sp!, {a1, a2, a3, a4} */
c906108c 2033 {
d4473757 2034 int mask = insn & 0xffff;
ed9a39eb 2035
f7b7ed97 2036 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
2037 break;
2038
94c30b78 2039 /* Calculate offsets of saved registers. */
34e8f22d 2040 for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
d4473757
KB
2041 if (mask & (1 << regno))
2042 {
0963b4bd
MS
2043 regs[ARM_SP_REGNUM]
2044 = pv_add_constant (regs[ARM_SP_REGNUM], -4);
f7b7ed97 2045 stack.store (regs[ARM_SP_REGNUM], 4, regs[regno]);
d4473757
KB
2046 }
2047 }
0d39a070
DJ
2048 else if ((insn & 0xffff0000) == 0xe54b0000 /* strb rx,[r11,#-n] */
2049 || (insn & 0xffff00f0) == 0xe14b00b0 /* strh rx,[r11,#-n] */
f8bf5763 2050 || (insn & 0xffffc000) == 0xe50b0000) /* str rx,[r11,#-n] */
b8d5e71d
MS
2051 {
2052 /* No need to add this to saved_regs -- it's just an arg reg. */
2053 continue;
2054 }
0d39a070
DJ
2055 else if ((insn & 0xffff0000) == 0xe5cd0000 /* strb rx,[sp,#n] */
2056 || (insn & 0xffff00f0) == 0xe1cd00b0 /* strh rx,[sp,#n] */
f8bf5763 2057 || (insn & 0xffffc000) == 0xe58d0000) /* str rx,[sp,#n] */
f43845b3
MS
2058 {
2059 /* No need to add this to saved_regs -- it's just an arg reg. */
2060 continue;
2061 }
0963b4bd
MS
2062 else if ((insn & 0xfff00000) == 0xe8800000 /* stm Rn,
2063 { registers } */
0d39a070
DJ
2064 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
2065 {
2066 /* No need to add this to saved_regs -- it's just arg regs. */
2067 continue;
2068 }
d4473757
KB
2069 else if ((insn & 0xfffff000) == 0xe24cb000) /* sub fp, ip #n */
2070 {
9ecab40c 2071 uint32_t imm = arm_expand_immediate (insn & 0xfff);
4be43953 2072 regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
d4473757
KB
2073 }
2074 else if ((insn & 0xfffff000) == 0xe24dd000) /* sub sp, sp #n */
2075 {
9ecab40c 2076 uint32_t imm = arm_expand_immediate(insn & 0xfff);
4be43953 2077 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
d4473757 2078 }
0963b4bd
MS
2079 else if ((insn & 0xffff7fff) == 0xed6d0103 /* stfe f?,
2080 [sp, -#c]! */
345bd07c 2081 && tdep->have_fpa_registers)
d4473757 2082 {
f7b7ed97 2083 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
2084 break;
2085
2086 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
34e8f22d 2087 regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
f7b7ed97 2088 stack.store (regs[ARM_SP_REGNUM], 12, regs[regno]);
d4473757 2089 }
0963b4bd
MS
2090 else if ((insn & 0xffbf0fff) == 0xec2d0200 /* sfmfd f0, 4,
2091 [sp!] */
345bd07c 2092 && tdep->have_fpa_registers)
d4473757
KB
2093 {
2094 int n_saved_fp_regs;
2095 unsigned int fp_start_reg, fp_bound_reg;
2096
f7b7ed97 2097 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
2098 break;
2099
94c30b78 2100 if ((insn & 0x800) == 0x800) /* N0 is set */
96baa820 2101 {
d4473757
KB
2102 if ((insn & 0x40000) == 0x40000) /* N1 is set */
2103 n_saved_fp_regs = 3;
2104 else
2105 n_saved_fp_regs = 1;
96baa820 2106 }
d4473757 2107 else
96baa820 2108 {
d4473757
KB
2109 if ((insn & 0x40000) == 0x40000) /* N1 is set */
2110 n_saved_fp_regs = 2;
2111 else
2112 n_saved_fp_regs = 4;
96baa820 2113 }
d4473757 2114
34e8f22d 2115 fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
d4473757
KB
2116 fp_bound_reg = fp_start_reg + n_saved_fp_regs;
2117 for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
96baa820 2118 {
4be43953 2119 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
f7b7ed97
TT
2120 stack.store (regs[ARM_SP_REGNUM], 12,
2121 regs[fp_start_reg++]);
96baa820 2122 }
c906108c 2123 }
0d39a070
DJ
2124 else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
2125 {
2126 /* Allow some special function calls when skipping the
2127 prologue; GCC generates these before storing arguments to
2128 the stack. */
2129 CORE_ADDR dest = BranchDest (current_pc, insn);
2130
e0634ccf 2131 if (skip_prologue_function (gdbarch, dest, 0))
0d39a070
DJ
2132 continue;
2133 else
2134 break;
2135 }
d4473757 2136 else if ((insn & 0xf0000000) != 0xe0000000)
0963b4bd 2137 break; /* Condition not true, exit early. */
0d39a070
DJ
2138 else if (arm_instruction_changes_pc (insn))
2139 /* Don't scan past anything that might change control flow. */
2140 break;
f303bc3e
YQ
2141 else if (arm_instruction_restores_sp (insn))
2142 {
2143 /* Don't scan past the epilogue. */
2144 break;
2145 }
d19f7eee
UW
2146 else if ((insn & 0xfe500000) == 0xe8100000 /* ldm */
2147 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
2148 /* Ignore block loads from the stack, potentially copying
2149 parameters from memory. */
2150 continue;
2151 else if ((insn & 0xfc500000) == 0xe4100000
2152 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
2153 /* Similarly ignore single loads from the stack. */
2154 continue;
0d39a070
DJ
2155 else if ((insn & 0xffff0ff0) == 0xe1a00000)
2156 /* MOV Rd, Rm. Skip register copies, i.e. saves to another
2157 register instead of the stack. */
d4473757 2158 continue;
0d39a070
DJ
2159 else
2160 {
21daaaaf
YQ
2161 /* The optimizer might shove anything into the prologue, if
2162 we build up cache (cache != NULL) from scanning prologue,
2163 we just skip what we don't recognize and scan further to
2164 make cache as complete as possible. However, if we skip
2165 prologue, we'll stop immediately on unrecognized
2166 instruction. */
0d39a070 2167 unrecognized_pc = current_pc;
21daaaaf
YQ
2168 if (cache != NULL)
2169 continue;
2170 else
2171 break;
0d39a070 2172 }
c906108c
SS
2173 }
2174
0d39a070
DJ
2175 if (unrecognized_pc == 0)
2176 unrecognized_pc = current_pc;
2177
0d39a070
DJ
2178 if (cache)
2179 {
4072f920
YQ
2180 int framereg, framesize;
2181
2182 /* The frame size is just the distance from the frame register
2183 to the original stack pointer. */
2184 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
2185 {
2186 /* Frame pointer is fp. */
2187 framereg = ARM_FP_REGNUM;
2188 framesize = -regs[ARM_FP_REGNUM].k;
2189 }
2190 else
2191 {
2192 /* Try the stack pointer... this is a bit desperate. */
2193 framereg = ARM_SP_REGNUM;
2194 framesize = -regs[ARM_SP_REGNUM].k;
2195 }
2196
0d39a070
DJ
2197 cache->framereg = framereg;
2198 cache->framesize = framesize;
2199
2200 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
f7b7ed97 2201 if (stack.find_reg (gdbarch, regno, &offset))
10245fe8
YR
2202 {
2203 cache->saved_regs[regno].set_addr (offset);
2204 if (regno == ARM_SP_REGNUM)
2205 arm_cache_set_active_sp_value(cache, tdep, offset);
2206 }
0d39a070
DJ
2207 }
2208
7cb6d92a
SM
2209 arm_debug_printf ("Prologue scan stopped at %s",
2210 paddress (gdbarch, unrecognized_pc));
4be43953 2211
0d39a070
DJ
2212 return unrecognized_pc;
2213}
2214
2215static void
bd2b40ac 2216arm_scan_prologue (frame_info_ptr this_frame,
0d39a070
DJ
2217 struct arm_prologue_cache *cache)
2218{
2219 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2220 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
bec2ab5a 2221 CORE_ADDR prologue_start, prologue_end;
0d39a070
DJ
2222 CORE_ADDR prev_pc = get_frame_pc (this_frame);
2223 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
08106042 2224 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
0d39a070
DJ
2225
2226 /* Assume there is no frame until proven otherwise. */
2227 cache->framereg = ARM_SP_REGNUM;
2228 cache->framesize = 0;
2229
2230 /* Check for Thumb prologue. */
2231 if (arm_frame_is_thumb (this_frame))
2232 {
2233 thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
2234 return;
2235 }
2236
2237 /* Find the function prologue. If we can't find the function in
2238 the symbol table, peek in the stack frame to find the PC. */
2239 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
2240 &prologue_end))
2241 {
2242 /* One way to find the end of the prologue (which works well
dda83cd7 2243 for unoptimized code) is to do the following:
0d39a070
DJ
2244
2245 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
2246
2247 if (sal.line == 0)
2248 prologue_end = prev_pc;
2249 else if (sal.end < prologue_end)
2250 prologue_end = sal.end;
2251
2252 This mechanism is very accurate so long as the optimizer
2253 doesn't move any instructions from the function body into the
2254 prologue. If this happens, sal.end will be the last
2255 instruction in the first hunk of prologue code just before
2256 the first instruction that the scheduler has moved from
2257 the body to the prologue.
2258
2259 In order to make sure that we scan all of the prologue
2260 instructions, we use a slightly less accurate mechanism which
2261 may scan more than necessary. To help compensate for this
2262 lack of accuracy, the prologue scanning loop below contains
2263 several clauses which'll cause the loop to terminate early if
2264 an implausible prologue instruction is encountered.
2265
2266 The expression
2267
2268 prologue_start + 64
2269
2270 is a suitable endpoint since it accounts for the largest
2271 possible prologue plus up to five instructions inserted by
2272 the scheduler. */
2273
2274 if (prologue_end > prologue_start + 64)
2275 {
2276 prologue_end = prologue_start + 64; /* See above. */
2277 }
2278 }
2279 else
2280 {
2281 /* We have no symbol information. Our only option is to assume this
2282 function has a standard stack frame and the normal frame register.
2283 Then, we can find the value of our frame pointer on entrance to
2284 the callee (or at the present moment if this is the innermost frame).
2285 The value stored there should be the address of the stmfd + 8. */
2286 CORE_ADDR frame_loc;
7913a64c 2287 ULONGEST return_value;
0d39a070 2288
9e237747 2289 /* AAPCS does not use a frame register, so we can abort here. */
345bd07c 2290 if (tdep->arm_abi == ARM_ABI_AAPCS)
dda83cd7 2291 return;
9e237747 2292
0d39a070 2293 frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
7913a64c
YQ
2294 if (!safe_read_memory_unsigned_integer (frame_loc, 4, byte_order,
2295 &return_value))
dda83cd7 2296 return;
0d39a070 2297 else
dda83cd7
SM
2298 {
2299 prologue_start = gdbarch_addr_bits_remove
0d39a070 2300 (gdbarch, return_value) - 8;
dda83cd7
SM
2301 prologue_end = prologue_start + 64; /* See above. */
2302 }
0d39a070
DJ
2303 }
2304
2305 if (prev_pc < prologue_end)
2306 prologue_end = prev_pc;
2307
9ecab40c
SM
2308 arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache,
2309 target_arm_instruction_reader ());
c906108c
SS
2310}
2311
eb5492fa 2312static struct arm_prologue_cache *
bd2b40ac 2313arm_make_prologue_cache (frame_info_ptr this_frame)
c906108c 2314{
eb5492fa
DJ
2315 int reg;
2316 struct arm_prologue_cache *cache;
1ef3351b 2317 CORE_ADDR unwound_fp, prev_sp;
c5aa993b 2318
35d5d4ee 2319 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 2320 arm_cache_init (cache, this_frame);
c906108c 2321
a262aec2 2322 arm_scan_prologue (this_frame, cache);
848cfffb 2323
a262aec2 2324 unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
eb5492fa
DJ
2325 if (unwound_fp == 0)
2326 return cache;
c906108c 2327
ae7e2f45 2328 arm_gdbarch_tdep *tdep =
08106042 2329 gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45 2330
1ef3351b
YR
2331 prev_sp = unwound_fp + cache->framesize;
2332 arm_cache_set_active_sp_value (cache, tdep, prev_sp);
c906108c 2333
eb5492fa
DJ
2334 /* Calculate actual addresses of saved registers using offsets
2335 determined by arm_scan_prologue. */
a262aec2 2336 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
a9a87d35 2337 if (cache->saved_regs[reg].is_addr ())
1ef3351b
YR
2338 cache->saved_regs[reg].set_addr (cache->saved_regs[reg].addr () +
2339 prev_sp);
eb5492fa
DJ
2340
2341 return cache;
c906108c
SS
2342}
2343
c1ee9414
LM
2344/* Implementation of the stop_reason hook for arm_prologue frames. */
2345
2346static enum unwind_stop_reason
bd2b40ac 2347arm_prologue_unwind_stop_reason (frame_info_ptr this_frame,
c1ee9414
LM
2348 void **this_cache)
2349{
2350 struct arm_prologue_cache *cache;
2351 CORE_ADDR pc;
2352
2353 if (*this_cache == NULL)
2354 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 2355 cache = (struct arm_prologue_cache *) *this_cache;
c1ee9414
LM
2356
2357 /* This is meant to halt the backtrace at "_start". */
2358 pc = get_frame_pc (this_frame);
345bd07c 2359 gdbarch *arch = get_frame_arch (this_frame);
08106042 2360 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (arch);
345bd07c 2361 if (pc <= tdep->lowest_pc)
c1ee9414
LM
2362 return UNWIND_OUTERMOST;
2363
2364 /* If we've hit a wall, stop. */
ae7e2f45 2365 if (arm_cache_get_prev_sp_value (cache, tdep) == 0)
c1ee9414
LM
2366 return UNWIND_OUTERMOST;
2367
2368 return UNWIND_NO_REASON;
2369}
2370
eb5492fa
DJ
2371/* Our frame ID for a normal frame is the current function's starting PC
2372 and the caller's SP when we were called. */
c906108c 2373
148754e5 2374static void
bd2b40ac 2375arm_prologue_this_id (frame_info_ptr this_frame,
eb5492fa
DJ
2376 void **this_cache,
2377 struct frame_id *this_id)
c906108c 2378{
eb5492fa
DJ
2379 struct arm_prologue_cache *cache;
2380 struct frame_id id;
2c404490 2381 CORE_ADDR pc, func;
f079148d 2382
eb5492fa 2383 if (*this_cache == NULL)
a262aec2 2384 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 2385 cache = (struct arm_prologue_cache *) *this_cache;
2a451106 2386
ae7e2f45 2387 arm_gdbarch_tdep *tdep
08106042 2388 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45 2389
0e9e9abd
UW
2390 /* Use function start address as part of the frame ID. If we cannot
2391 identify the start address (due to missing symbol information),
2392 fall back to just using the current PC. */
c1ee9414 2393 pc = get_frame_pc (this_frame);
2c404490 2394 func = get_frame_func (this_frame);
0e9e9abd
UW
2395 if (!func)
2396 func = pc;
2397
ae7e2f45 2398 id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep), func);
eb5492fa 2399 *this_id = id;
c906108c
SS
2400}
2401
a262aec2 2402static struct value *
bd2b40ac 2403arm_prologue_prev_register (frame_info_ptr this_frame,
eb5492fa 2404 void **this_cache,
a262aec2 2405 int prev_regnum)
24de872b 2406{
24568a2c 2407 struct gdbarch *gdbarch = get_frame_arch (this_frame);
24de872b 2408 struct arm_prologue_cache *cache;
ef273377 2409 CORE_ADDR sp_value;
24de872b 2410
eb5492fa 2411 if (*this_cache == NULL)
a262aec2 2412 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 2413 cache = (struct arm_prologue_cache *) *this_cache;
24de872b 2414
08106042 2415 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
a01567f4
LM
2416
2417 /* If this frame has signed the return address, mark it as so. */
2418 if (tdep->have_pacbti && cache->ra_signed_state.has_value ()
2419 && *cache->ra_signed_state)
2420 set_frame_previous_pc_masked (this_frame);
2421
eb5492fa 2422 /* If we are asked to unwind the PC, then we need to return the LR
b39cc962
DJ
2423 instead. The prologue may save PC, but it will point into this
2424 frame's prologue, not the next frame's resume location. Also
2425 strip the saved T bit. A valid LR may have the low bit set, but
2426 a valid PC never does. */
eb5492fa 2427 if (prev_regnum == ARM_PC_REGNUM)
b39cc962
DJ
2428 {
2429 CORE_ADDR lr;
2430
2431 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2432 return frame_unwind_got_constant (this_frame, prev_regnum,
24568a2c 2433 arm_addr_bits_remove (gdbarch, lr));
b39cc962 2434 }
24de872b 2435
eb5492fa 2436 /* SP is generally not saved to the stack, but this frame is
a262aec2 2437 identified by the next frame's stack pointer at the time of the call.
eb5492fa
DJ
2438 The value was already reconstructed into PREV_SP. */
2439 if (prev_regnum == ARM_SP_REGNUM)
ae7e2f45
CL
2440 return frame_unwind_got_constant (this_frame, prev_regnum,
2441 arm_cache_get_prev_sp_value (cache, tdep));
eb5492fa 2442
ef273377
CL
2443 /* The value might be one of the alternative SP, if so, use the
2444 value already constructed. */
d65edaa0 2445 if (arm_is_alternative_sp_register (tdep, prev_regnum))
ef273377
CL
2446 {
2447 sp_value = arm_cache_get_sp_register (cache, tdep, prev_regnum);
2448 return frame_unwind_got_constant (this_frame, prev_regnum, sp_value);
2449 }
2450
b39cc962
DJ
2451 /* The CPSR may have been changed by the call instruction and by the
2452 called function. The only bit we can reconstruct is the T bit,
2453 by checking the low bit of LR as of the call. This is a reliable
2454 indicator of Thumb-ness except for some ARM v4T pre-interworking
2455 Thumb code, which could get away with a clear low bit as long as
2456 the called function did not use bx. Guess that all other
2457 bits are unchanged; the condition flags are presumably lost,
2458 but the processor status is likely valid. */
2459 if (prev_regnum == ARM_PS_REGNUM)
2460 {
8c9ae6df
YR
2461 ULONGEST cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
2462 CORE_ADDR lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
b39cc962 2463
8c9ae6df 2464 cpsr = reconstruct_t_bit (gdbarch, lr, cpsr);
b39cc962
DJ
2465 return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
2466 }
2467
a262aec2
DJ
2468 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
2469 prev_regnum);
eb5492fa
DJ
2470}
2471
6bd434d6 2472static frame_unwind arm_prologue_unwind = {
a154d838 2473 "arm prologue",
eb5492fa 2474 NORMAL_FRAME,
c1ee9414 2475 arm_prologue_unwind_stop_reason,
eb5492fa 2476 arm_prologue_this_id,
a262aec2
DJ
2477 arm_prologue_prev_register,
2478 NULL,
2479 default_frame_sniffer
eb5492fa
DJ
2480};
2481
0e9e9abd
UW
2482/* Maintain a list of ARM exception table entries per objfile, similar to the
2483 list of mapping symbols. We only cache entries for standard ARM-defined
2484 personality routines; the cache will contain only the frame unwinding
2485 instructions associated with the entry (not the descriptors). */
2486
0e9e9abd
UW
2487struct arm_exidx_entry
2488{
227031b2 2489 CORE_ADDR addr;
0e9e9abd 2490 gdb_byte *entry;
7a5d944b
TT
2491
2492 bool operator< (const arm_exidx_entry &other) const
2493 {
2494 return addr < other.addr;
2495 }
0e9e9abd 2496};
0e9e9abd
UW
2497
2498struct arm_exidx_data
2499{
7a5d944b 2500 std::vector<std::vector<arm_exidx_entry>> section_maps;
0e9e9abd
UW
2501};
2502
a2726d4f 2503/* Per-BFD key to store exception handling information. */
08b8a139 2504static const registry<bfd>::key<arm_exidx_data> arm_exidx_data_key;
0e9e9abd
UW
2505
2506static struct obj_section *
2507arm_obj_section_from_vma (struct objfile *objfile, bfd_vma vma)
2508{
5250cbc8 2509 for (obj_section *osect : objfile->sections ())
fd361982 2510 if (bfd_section_flags (osect->the_bfd_section) & SEC_ALLOC)
0e9e9abd
UW
2511 {
2512 bfd_vma start, size;
fd361982
AM
2513 start = bfd_section_vma (osect->the_bfd_section);
2514 size = bfd_section_size (osect->the_bfd_section);
0e9e9abd
UW
2515
2516 if (start <= vma && vma < start + size)
2517 return osect;
2518 }
2519
2520 return NULL;
2521}
2522
2523/* Parse contents of exception table and exception index sections
2524 of OBJFILE, and fill in the exception table entry cache.
2525
2526 For each entry that refers to a standard ARM-defined personality
2527 routine, extract the frame unwinding instructions (from either
2528 the index or the table section). The unwinding instructions
2529 are normalized by:
2530 - extracting them from the rest of the table data
2531 - converting to host endianness
2532 - appending the implicit 0xb0 ("Finish") code
2533
2534 The extracted and normalized instructions are stored for later
2535 retrieval by the arm_find_exidx_entry routine. */
2536
2537static void
2538arm_exidx_new_objfile (struct objfile *objfile)
2539{
0e9e9abd
UW
2540 struct arm_exidx_data *data;
2541 asection *exidx, *extab;
2542 bfd_vma exidx_vma = 0, extab_vma = 0;
0e9e9abd
UW
2543 LONGEST i;
2544
2545 /* If we've already touched this file, do nothing. */
74daa597 2546 if (arm_exidx_data_key.get (objfile->obfd.get ()) != nullptr)
0e9e9abd
UW
2547 return;
2548
2549 /* Read contents of exception table and index. */
98badbfd
TT
2550 exidx = bfd_get_section_by_name (objfile->obfd.get (),
2551 ELF_STRING_ARM_unwind);
984c7238 2552 gdb::byte_vector exidx_data;
0e9e9abd
UW
2553 if (exidx)
2554 {
fd361982
AM
2555 exidx_vma = bfd_section_vma (exidx);
2556 exidx_data.resize (bfd_section_size (exidx));
0e9e9abd 2557
98badbfd 2558 if (!bfd_get_section_contents (objfile->obfd.get (), exidx,
984c7238
TT
2559 exidx_data.data (), 0,
2560 exidx_data.size ()))
2561 return;
0e9e9abd
UW
2562 }
2563
98badbfd 2564 extab = bfd_get_section_by_name (objfile->obfd.get (), ".ARM.extab");
984c7238 2565 gdb::byte_vector extab_data;
0e9e9abd
UW
2566 if (extab)
2567 {
fd361982
AM
2568 extab_vma = bfd_section_vma (extab);
2569 extab_data.resize (bfd_section_size (extab));
0e9e9abd 2570
98badbfd 2571 if (!bfd_get_section_contents (objfile->obfd.get (), extab,
984c7238
TT
2572 extab_data.data (), 0,
2573 extab_data.size ()))
2574 return;
0e9e9abd
UW
2575 }
2576
2577 /* Allocate exception table data structure. */
98badbfd 2578 data = arm_exidx_data_key.emplace (objfile->obfd.get ());
7a5d944b 2579 data->section_maps.resize (objfile->obfd->section_count);
0e9e9abd
UW
2580
2581 /* Fill in exception table. */
984c7238 2582 for (i = 0; i < exidx_data.size () / 8; i++)
0e9e9abd
UW
2583 {
2584 struct arm_exidx_entry new_exidx_entry;
984c7238
TT
2585 bfd_vma idx = bfd_h_get_32 (objfile->obfd, exidx_data.data () + i * 8);
2586 bfd_vma val = bfd_h_get_32 (objfile->obfd,
2587 exidx_data.data () + i * 8 + 4);
0e9e9abd
UW
2588 bfd_vma addr = 0, word = 0;
2589 int n_bytes = 0, n_words = 0;
2590 struct obj_section *sec;
2591 gdb_byte *entry = NULL;
2592
2593 /* Extract address of start of function. */
2594 idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2595 idx += exidx_vma + i * 8;
2596
2597 /* Find section containing function and compute section offset. */
2598 sec = arm_obj_section_from_vma (objfile, idx);
2599 if (sec == NULL)
2600 continue;
fd361982 2601 idx -= bfd_section_vma (sec->the_bfd_section);
0e9e9abd
UW
2602
2603 /* Determine address of exception table entry. */
2604 if (val == 1)
2605 {
2606 /* EXIDX_CANTUNWIND -- no exception table entry present. */
2607 }
2608 else if ((val & 0xff000000) == 0x80000000)
2609 {
2610 /* Exception table entry embedded in .ARM.exidx
2611 -- must be short form. */
2612 word = val;
2613 n_bytes = 3;
2614 }
2615 else if (!(val & 0x80000000))
2616 {
2617 /* Exception table entry in .ARM.extab. */
2618 addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2619 addr += exidx_vma + i * 8 + 4;
2620
984c7238 2621 if (addr >= extab_vma && addr + 4 <= extab_vma + extab_data.size ())
0e9e9abd
UW
2622 {
2623 word = bfd_h_get_32 (objfile->obfd,
984c7238 2624 extab_data.data () + addr - extab_vma);
0e9e9abd
UW
2625 addr += 4;
2626
2627 if ((word & 0xff000000) == 0x80000000)
2628 {
2629 /* Short form. */
2630 n_bytes = 3;
2631 }
2632 else if ((word & 0xff000000) == 0x81000000
2633 || (word & 0xff000000) == 0x82000000)
2634 {
2635 /* Long form. */
2636 n_bytes = 2;
2637 n_words = ((word >> 16) & 0xff);
2638 }
2639 else if (!(word & 0x80000000))
2640 {
2641 bfd_vma pers;
2642 struct obj_section *pers_sec;
2643 int gnu_personality = 0;
2644
2645 /* Custom personality routine. */
2646 pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2647 pers = UNMAKE_THUMB_ADDR (pers + addr - 4);
2648
2649 /* Check whether we've got one of the variants of the
2650 GNU personality routines. */
2651 pers_sec = arm_obj_section_from_vma (objfile, pers);
2652 if (pers_sec)
2653 {
2654 static const char *personality[] =
2655 {
2656 "__gcc_personality_v0",
2657 "__gxx_personality_v0",
2658 "__gcj_personality_v0",
2659 "__gnu_objc_personality_v0",
2660 NULL
2661 };
2662
0c1bcd23 2663 CORE_ADDR pc = pers + pers_sec->offset ();
0e9e9abd
UW
2664 int k;
2665
2666 for (k = 0; personality[k]; k++)
2667 if (lookup_minimal_symbol_by_pc_name
2668 (pc, personality[k], objfile))
2669 {
2670 gnu_personality = 1;
2671 break;
2672 }
2673 }
2674
2675 /* If so, the next word contains a word count in the high
2676 byte, followed by the same unwind instructions as the
2677 pre-defined forms. */
2678 if (gnu_personality
984c7238 2679 && addr + 4 <= extab_vma + extab_data.size ())
0e9e9abd
UW
2680 {
2681 word = bfd_h_get_32 (objfile->obfd,
984c7238
TT
2682 (extab_data.data ()
2683 + addr - extab_vma));
0e9e9abd
UW
2684 addr += 4;
2685 n_bytes = 3;
2686 n_words = ((word >> 24) & 0xff);
2687 }
2688 }
2689 }
2690 }
2691
2692 /* Sanity check address. */
2693 if (n_words)
984c7238
TT
2694 if (addr < extab_vma
2695 || addr + 4 * n_words > extab_vma + extab_data.size ())
0e9e9abd
UW
2696 n_words = n_bytes = 0;
2697
2698 /* The unwind instructions reside in WORD (only the N_BYTES least
2699 significant bytes are valid), followed by N_WORDS words in the
2700 extab section starting at ADDR. */
2701 if (n_bytes || n_words)
2702 {
224c3ddb
SM
2703 gdb_byte *p = entry
2704 = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack,
2705 n_bytes + n_words * 4 + 1);
0e9e9abd
UW
2706
2707 while (n_bytes--)
2708 *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);
2709
2710 while (n_words--)
2711 {
2712 word = bfd_h_get_32 (objfile->obfd,
984c7238 2713 extab_data.data () + addr - extab_vma);
0e9e9abd
UW
2714 addr += 4;
2715
2716 *p++ = (gdb_byte) ((word >> 24) & 0xff);
2717 *p++ = (gdb_byte) ((word >> 16) & 0xff);
2718 *p++ = (gdb_byte) ((word >> 8) & 0xff);
2719 *p++ = (gdb_byte) (word & 0xff);
2720 }
2721
2722 /* Implied "Finish" to terminate the list. */
2723 *p++ = 0xb0;
2724 }
2725
2726 /* Push entry onto vector. They are guaranteed to always
2727 appear in order of increasing addresses. */
2728 new_exidx_entry.addr = idx;
2729 new_exidx_entry.entry = entry;
7a5d944b
TT
2730 data->section_maps[sec->the_bfd_section->index].push_back
2731 (new_exidx_entry);
0e9e9abd 2732 }
0e9e9abd
UW
2733}
2734
2735/* Search for the exception table entry covering MEMADDR. If one is found,
2736 return a pointer to its data. Otherwise, return 0. If START is non-NULL,
2737 set *START to the start of the region covered by this entry. */
2738
2739static gdb_byte *
2740arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
2741{
2742 struct obj_section *sec;
2743
2744 sec = find_pc_section (memaddr);
2745 if (sec != NULL)
2746 {
2747 struct arm_exidx_data *data;
0c1bcd23 2748 struct arm_exidx_entry map_key = { memaddr - sec->addr (), 0 };
0e9e9abd 2749
98badbfd 2750 data = arm_exidx_data_key.get (sec->objfile->obfd.get ());
0e9e9abd
UW
2751 if (data != NULL)
2752 {
7a5d944b
TT
2753 std::vector<arm_exidx_entry> &map
2754 = data->section_maps[sec->the_bfd_section->index];
2755 if (!map.empty ())
0e9e9abd 2756 {
7a5d944b 2757 auto idx = std::lower_bound (map.begin (), map.end (), map_key);
0e9e9abd 2758
7a5d944b 2759 /* std::lower_bound finds the earliest ordered insertion
0e9e9abd
UW
2760 point. If the following symbol starts at this exact
2761 address, we use that; otherwise, the preceding
2762 exception table entry covers this address. */
7a5d944b 2763 if (idx < map.end ())
0e9e9abd 2764 {
7a5d944b 2765 if (idx->addr == map_key.addr)
0e9e9abd
UW
2766 {
2767 if (start)
0c1bcd23 2768 *start = idx->addr + sec->addr ();
7a5d944b 2769 return idx->entry;
0e9e9abd
UW
2770 }
2771 }
2772
7a5d944b 2773 if (idx > map.begin ())
0e9e9abd 2774 {
7a5d944b 2775 idx = idx - 1;
0e9e9abd 2776 if (start)
0c1bcd23 2777 *start = idx->addr + sec->addr ();
7a5d944b 2778 return idx->entry;
0e9e9abd
UW
2779 }
2780 }
2781 }
2782 }
2783
2784 return NULL;
2785}
2786
2787/* Given the current frame THIS_FRAME, and its associated frame unwinding
2788 instruction list from the ARM exception table entry ENTRY, allocate and
2789 return a prologue cache structure describing how to unwind this frame.
2790
2791 Return NULL if the unwinding instruction list contains a "spare",
2792 "reserved" or "refuse to unwind" instruction as defined in section
2793 "9.3 Frame unwinding instructions" of the "Exception Handling ABI
2794 for the ARM Architecture" document. */
2795
2796static struct arm_prologue_cache *
bd2b40ac 2797arm_exidx_fill_cache (frame_info_ptr this_frame, gdb_byte *entry)
0e9e9abd
UW
2798{
2799 CORE_ADDR vsp = 0;
2800 int vsp_valid = 0;
2801
2802 struct arm_prologue_cache *cache;
2803 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 2804 arm_cache_init (cache, this_frame);
0e9e9abd
UW
2805
2806 for (;;)
2807 {
2808 gdb_byte insn;
2809
2810 /* Whenever we reload SP, we actually have to retrieve its
2811 actual value in the current frame. */
2812 if (!vsp_valid)
2813 {
a9a87d35 2814 if (cache->saved_regs[ARM_SP_REGNUM].is_realreg ())
0e9e9abd 2815 {
098caef4 2816 int reg = cache->saved_regs[ARM_SP_REGNUM].realreg ();
0e9e9abd
UW
2817 vsp = get_frame_register_unsigned (this_frame, reg);
2818 }
2819 else
2820 {
098caef4 2821 CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr ();
0e9e9abd
UW
2822 vsp = get_frame_memory_unsigned (this_frame, addr, 4);
2823 }
2824
2825 vsp_valid = 1;
2826 }
2827
2828 /* Decode next unwind instruction. */
2829 insn = *entry++;
2830
2831 if ((insn & 0xc0) == 0)
2832 {
2833 int offset = insn & 0x3f;
2834 vsp += (offset << 2) + 4;
2835 }
2836 else if ((insn & 0xc0) == 0x40)
2837 {
2838 int offset = insn & 0x3f;
2839 vsp -= (offset << 2) + 4;
2840 }
2841 else if ((insn & 0xf0) == 0x80)
2842 {
2843 int mask = ((insn & 0xf) << 8) | *entry++;
2844 int i;
2845
2846 /* The special case of an all-zero mask identifies
2847 "Refuse to unwind". We return NULL to fall back
2848 to the prologue analyzer. */
2849 if (mask == 0)
2850 return NULL;
2851
2852 /* Pop registers r4..r15 under mask. */
2853 for (i = 0; i < 12; i++)
2854 if (mask & (1 << i))
2855 {
098caef4 2856 cache->saved_regs[4 + i].set_addr (vsp);
0e9e9abd
UW
2857 vsp += 4;
2858 }
2859
2860 /* Special-case popping SP -- we need to reload vsp. */
2861 if (mask & (1 << (ARM_SP_REGNUM - 4)))
2862 vsp_valid = 0;
2863 }
2864 else if ((insn & 0xf0) == 0x90)
2865 {
2866 int reg = insn & 0xf;
2867
2868 /* Reserved cases. */
2869 if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
2870 return NULL;
2871
2872 /* Set SP from another register and mark VSP for reload. */
2873 cache->saved_regs[ARM_SP_REGNUM] = cache->saved_regs[reg];
2874 vsp_valid = 0;
2875 }
2876 else if ((insn & 0xf0) == 0xa0)
2877 {
2878 int count = insn & 0x7;
2879 int pop_lr = (insn & 0x8) != 0;
2880 int i;
2881
2882 /* Pop r4..r[4+count]. */
2883 for (i = 0; i <= count; i++)
2884 {
098caef4 2885 cache->saved_regs[4 + i].set_addr (vsp);
0e9e9abd
UW
2886 vsp += 4;
2887 }
2888
2889 /* If indicated by flag, pop LR as well. */
2890 if (pop_lr)
2891 {
098caef4 2892 cache->saved_regs[ARM_LR_REGNUM].set_addr (vsp);
0e9e9abd
UW
2893 vsp += 4;
2894 }
2895 }
2896 else if (insn == 0xb0)
2897 {
2898 /* We could only have updated PC by popping into it; if so, it
2899 will show up as address. Otherwise, copy LR into PC. */
a9a87d35 2900 if (!cache->saved_regs[ARM_PC_REGNUM].is_addr ())
0e9e9abd
UW
2901 cache->saved_regs[ARM_PC_REGNUM]
2902 = cache->saved_regs[ARM_LR_REGNUM];
2903
2904 /* We're done. */
2905 break;
2906 }
2907 else if (insn == 0xb1)
2908 {
2909 int mask = *entry++;
2910 int i;
2911
2912 /* All-zero mask and mask >= 16 is "spare". */
2913 if (mask == 0 || mask >= 16)
2914 return NULL;
2915
2916 /* Pop r0..r3 under mask. */
2917 for (i = 0; i < 4; i++)
2918 if (mask & (1 << i))
2919 {
098caef4 2920 cache->saved_regs[i].set_addr (vsp);
0e9e9abd
UW
2921 vsp += 4;
2922 }
2923 }
2924 else if (insn == 0xb2)
2925 {
2926 ULONGEST offset = 0;
2927 unsigned shift = 0;
2928
2929 do
2930 {
2931 offset |= (*entry & 0x7f) << shift;
2932 shift += 7;
2933 }
2934 while (*entry++ & 0x80);
2935
2936 vsp += 0x204 + (offset << 2);
2937 }
2938 else if (insn == 0xb3)
2939 {
2940 int start = *entry >> 4;
2941 int count = (*entry++) & 0xf;
2942 int i;
2943
2944 /* Only registers D0..D15 are valid here. */
2945 if (start + count >= 16)
2946 return NULL;
2947
2948 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2949 for (i = 0; i <= count; i++)
2950 {
098caef4 2951 cache->saved_regs[ARM_D0_REGNUM + start + i].set_addr (vsp);
0e9e9abd
UW
2952 vsp += 8;
2953 }
2954
2955 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2956 vsp += 4;
2957 }
2958 else if ((insn & 0xf8) == 0xb8)
2959 {
2960 int count = insn & 0x7;
2961 int i;
2962
2963 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2964 for (i = 0; i <= count; i++)
2965 {
098caef4 2966 cache->saved_regs[ARM_D0_REGNUM + 8 + i].set_addr (vsp);
0e9e9abd
UW
2967 vsp += 8;
2968 }
2969
2970 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2971 vsp += 4;
2972 }
2973 else if (insn == 0xc6)
2974 {
2975 int start = *entry >> 4;
2976 int count = (*entry++) & 0xf;
2977 int i;
2978
2979 /* Only registers WR0..WR15 are valid. */
2980 if (start + count >= 16)
2981 return NULL;
2982
2983 /* Pop iwmmx registers WR[start]..WR[start+count]. */
2984 for (i = 0; i <= count; i++)
2985 {
098caef4 2986 cache->saved_regs[ARM_WR0_REGNUM + start + i].set_addr (vsp);
0e9e9abd
UW
2987 vsp += 8;
2988 }
2989 }
2990 else if (insn == 0xc7)
2991 {
2992 int mask = *entry++;
2993 int i;
2994
2995 /* All-zero mask and mask >= 16 is "spare". */
2996 if (mask == 0 || mask >= 16)
2997 return NULL;
2998
2999 /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask. */
3000 for (i = 0; i < 4; i++)
3001 if (mask & (1 << i))
3002 {
098caef4 3003 cache->saved_regs[ARM_WCGR0_REGNUM + i].set_addr (vsp);
0e9e9abd
UW
3004 vsp += 4;
3005 }
3006 }
3007 else if ((insn & 0xf8) == 0xc0)
3008 {
3009 int count = insn & 0x7;
3010 int i;
3011
3012 /* Pop iwmmx registers WR[10]..WR[10+count]. */
3013 for (i = 0; i <= count; i++)
3014 {
098caef4 3015 cache->saved_regs[ARM_WR0_REGNUM + 10 + i].set_addr (vsp);
0e9e9abd
UW
3016 vsp += 8;
3017 }
3018 }
3019 else if (insn == 0xc8)
3020 {
3021 int start = *entry >> 4;
3022 int count = (*entry++) & 0xf;
3023 int i;
3024
3025 /* Only registers D0..D31 are valid. */
3026 if (start + count >= 16)
3027 return NULL;
3028
3029 /* Pop VFP double-precision registers
3030 D[16+start]..D[16+start+count]. */
3031 for (i = 0; i <= count; i++)
3032 {
098caef4 3033 cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].set_addr (vsp);
0e9e9abd
UW
3034 vsp += 8;
3035 }
3036 }
3037 else if (insn == 0xc9)
3038 {
3039 int start = *entry >> 4;
3040 int count = (*entry++) & 0xf;
3041 int i;
3042
3043 /* Pop VFP double-precision registers D[start]..D[start+count]. */
3044 for (i = 0; i <= count; i++)
3045 {
098caef4 3046 cache->saved_regs[ARM_D0_REGNUM + start + i].set_addr (vsp);
0e9e9abd
UW
3047 vsp += 8;
3048 }
3049 }
3050 else if ((insn & 0xf8) == 0xd0)
3051 {
3052 int count = insn & 0x7;
3053 int i;
3054
3055 /* Pop VFP double-precision registers D[8]..D[8+count]. */
3056 for (i = 0; i <= count; i++)
3057 {
098caef4 3058 cache->saved_regs[ARM_D0_REGNUM + 8 + i].set_addr (vsp);
0e9e9abd
UW
3059 vsp += 8;
3060 }
3061 }
3062 else
3063 {
3064 /* Everything else is "spare". */
3065 return NULL;
3066 }
3067 }
3068
3069 /* If we restore SP from a register, assume this was the frame register.
3070 Otherwise just fall back to SP as frame register. */
a9a87d35 3071 if (cache->saved_regs[ARM_SP_REGNUM].is_realreg ())
098caef4 3072 cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg ();
0e9e9abd
UW
3073 else
3074 cache->framereg = ARM_SP_REGNUM;
3075
3076 /* Determine offset to previous frame. */
3077 cache->framesize
3078 = vsp - get_frame_register_unsigned (this_frame, cache->framereg);
3079
3080 /* We already got the previous SP. */
ae7e2f45 3081 arm_gdbarch_tdep *tdep
08106042 3082 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45 3083 arm_cache_set_active_sp_value (cache, tdep, vsp);
0e9e9abd
UW
3084
3085 return cache;
3086}
3087
3088/* Unwinding via ARM exception table entries. Note that the sniffer
3089 already computes a filled-in prologue cache, which is then used
3090 with the same arm_prologue_this_id and arm_prologue_prev_register
3091 routines also used for prologue-parsing based unwinding. */
3092
3093static int
3094arm_exidx_unwind_sniffer (const struct frame_unwind *self,
bd2b40ac 3095 frame_info_ptr this_frame,
0e9e9abd
UW
3096 void **this_prologue_cache)
3097{
3098 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3099 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3100 CORE_ADDR addr_in_block, exidx_region, func_start;
3101 struct arm_prologue_cache *cache;
3102 gdb_byte *entry;
3103
3104 /* See if we have an ARM exception table entry covering this address. */
3105 addr_in_block = get_frame_address_in_block (this_frame);
3106 entry = arm_find_exidx_entry (addr_in_block, &exidx_region);
3107 if (!entry)
3108 return 0;
3109
3110 /* The ARM exception table does not describe unwind information
3111 for arbitrary PC values, but is guaranteed to be correct only
3112 at call sites. We have to decide here whether we want to use
3113 ARM exception table information for this frame, or fall back
3114 to using prologue parsing. (Note that if we have DWARF CFI,
3115 this sniffer isn't even called -- CFI is always preferred.)
3116
3117 Before we make this decision, however, we check whether we
3118 actually have *symbol* information for the current frame.
3119 If not, prologue parsing would not work anyway, so we might
3120 as well use the exception table and hope for the best. */
3121 if (find_pc_partial_function (addr_in_block, NULL, &func_start, NULL))
3122 {
3123 int exc_valid = 0;
3124
3125 /* If the next frame is "normal", we are at a call site in this
3126 frame, so exception information is guaranteed to be valid. */
3127 if (get_next_frame (this_frame)
3128 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
3129 exc_valid = 1;
3130
3026cdbd
JK
3131 /* Some syscalls keep PC pointing to the SVC instruction itself. */
3132 for (int shift = 0; shift <= 1 && !exc_valid; ++shift)
0e9e9abd 3133 {
3026cdbd
JK
3134 /* We also assume exception information is valid if we're currently
3135 blocked in a system call. The system library is supposed to
3136 ensure this, so that e.g. pthread cancellation works. */
3137 if (arm_frame_is_thumb (this_frame))
3138 {
3139 ULONGEST insn;
3140
3141 if (safe_read_memory_unsigned_integer ((get_frame_pc (this_frame)
3142 - (shift ? 2 : 0)),
3143 2, byte_order_for_code,
3144 &insn)
3145 && (insn & 0xff00) == 0xdf00 /* svc */)
3146 exc_valid = 1;
3147 }
3148 else
3149 {
3150 ULONGEST insn;
3151
3152 if (safe_read_memory_unsigned_integer ((get_frame_pc (this_frame)
3153 - (shift ? 4 : 0)),
3154 4, byte_order_for_code,
3155 &insn)
3156 && (insn & 0x0f000000) == 0x0f000000 /* svc */)
3157 exc_valid = 1;
3158 }
d9311bfa
AT
3159 }
3160
0e9e9abd
UW
3161 /* Bail out if we don't know that exception information is valid. */
3162 if (!exc_valid)
3163 return 0;
3164
3165 /* The ARM exception index does not mark the *end* of the region
3166 covered by the entry, and some functions will not have any entry.
3167 To correctly recognize the end of the covered region, the linker
3168 should have inserted dummy records with a CANTUNWIND marker.
3169
3170 Unfortunately, current versions of GNU ld do not reliably do
3171 this, and thus we may have found an incorrect entry above.
3172 As a (temporary) sanity check, we only use the entry if it
3173 lies *within* the bounds of the function. Note that this check
3174 might reject perfectly valid entries that just happen to cover
3175 multiple functions; therefore this check ought to be removed
3176 once the linker is fixed. */
3177 if (func_start > exidx_region)
3178 return 0;
3179 }
3180
3181 /* Decode the list of unwinding instructions into a prologue cache.
3182 Note that this may fail due to e.g. a "refuse to unwind" code. */
3183 cache = arm_exidx_fill_cache (this_frame, entry);
3184 if (!cache)
3185 return 0;
3186
3187 *this_prologue_cache = cache;
3188 return 1;
3189}
3190
3191struct frame_unwind arm_exidx_unwind = {
a154d838 3192 "arm exidx",
0e9e9abd 3193 NORMAL_FRAME,
8fbca658 3194 default_frame_unwind_stop_reason,
0e9e9abd
UW
3195 arm_prologue_this_id,
3196 arm_prologue_prev_register,
3197 NULL,
3198 arm_exidx_unwind_sniffer
3199};
3200
779aa56f 3201static struct arm_prologue_cache *
bd2b40ac 3202arm_make_epilogue_frame_cache (frame_info_ptr this_frame)
779aa56f
YQ
3203{
3204 struct arm_prologue_cache *cache;
779aa56f
YQ
3205 int reg;
3206
3207 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 3208 arm_cache_init (cache, this_frame);
779aa56f
YQ
3209
3210 /* Still rely on the offset calculated from prologue. */
3211 arm_scan_prologue (this_frame, cache);
3212
3213 /* Since we are in epilogue, the SP has been restored. */
ae7e2f45 3214 arm_gdbarch_tdep *tdep
08106042 3215 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45
CL
3216 arm_cache_set_active_sp_value (cache, tdep,
3217 get_frame_register_unsigned (this_frame,
3218 ARM_SP_REGNUM));
779aa56f
YQ
3219
3220 /* Calculate actual addresses of saved registers using offsets
3221 determined by arm_scan_prologue. */
3222 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
a9a87d35 3223 if (cache->saved_regs[reg].is_addr ())
098caef4 3224 cache->saved_regs[reg].set_addr (cache->saved_regs[reg].addr ()
ae7e2f45 3225 + arm_cache_get_prev_sp_value (cache, tdep));
779aa56f
YQ
3226
3227 return cache;
3228}
3229
3230/* Implementation of function hook 'this_id' in
3231 'struct frame_uwnind' for epilogue unwinder. */
3232
3233static void
bd2b40ac 3234arm_epilogue_frame_this_id (frame_info_ptr this_frame,
779aa56f
YQ
3235 void **this_cache,
3236 struct frame_id *this_id)
3237{
3238 struct arm_prologue_cache *cache;
3239 CORE_ADDR pc, func;
3240
3241 if (*this_cache == NULL)
3242 *this_cache = arm_make_epilogue_frame_cache (this_frame);
3243 cache = (struct arm_prologue_cache *) *this_cache;
3244
3245 /* Use function start address as part of the frame ID. If we cannot
3246 identify the start address (due to missing symbol information),
3247 fall back to just using the current PC. */
3248 pc = get_frame_pc (this_frame);
3249 func = get_frame_func (this_frame);
fb3f3d25 3250 if (func == 0)
779aa56f
YQ
3251 func = pc;
3252
ae7e2f45 3253 arm_gdbarch_tdep *tdep
08106042 3254 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45 3255 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep), pc);
779aa56f
YQ
3256}
3257
3258/* Implementation of function hook 'prev_register' in
3259 'struct frame_uwnind' for epilogue unwinder. */
3260
3261static struct value *
bd2b40ac 3262arm_epilogue_frame_prev_register (frame_info_ptr this_frame,
779aa56f
YQ
3263 void **this_cache, int regnum)
3264{
779aa56f
YQ
3265 if (*this_cache == NULL)
3266 *this_cache = arm_make_epilogue_frame_cache (this_frame);
779aa56f
YQ
3267
3268 return arm_prologue_prev_register (this_frame, this_cache, regnum);
3269}
3270
3271static int arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch,
3272 CORE_ADDR pc);
3273static int thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch,
3274 CORE_ADDR pc);
3275
3276/* Implementation of function hook 'sniffer' in
3277 'struct frame_uwnind' for epilogue unwinder. */
3278
3279static int
3280arm_epilogue_frame_sniffer (const struct frame_unwind *self,
bd2b40ac 3281 frame_info_ptr this_frame,
779aa56f
YQ
3282 void **this_prologue_cache)
3283{
3284 if (frame_relative_level (this_frame) == 0)
3285 {
3286 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3287 CORE_ADDR pc = get_frame_pc (this_frame);
3288
3289 if (arm_frame_is_thumb (this_frame))
3290 return thumb_stack_frame_destroyed_p (gdbarch, pc);
3291 else
3292 return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
3293 }
3294 else
3295 return 0;
3296}
3297
3298/* Frame unwinder from epilogue. */
3299
3300static const struct frame_unwind arm_epilogue_frame_unwind =
3301{
a154d838 3302 "arm epilogue",
779aa56f
YQ
3303 NORMAL_FRAME,
3304 default_frame_unwind_stop_reason,
3305 arm_epilogue_frame_this_id,
3306 arm_epilogue_frame_prev_register,
3307 NULL,
3308 arm_epilogue_frame_sniffer,
3309};
3310
80d8d390
YQ
3311/* Recognize GCC's trampoline for thumb call-indirect. If we are in a
3312 trampoline, return the target PC. Otherwise return 0.
3313
3314 void call0a (char c, short s, int i, long l) {}
3315
3316 int main (void)
3317 {
3318 (*pointer_to_call0a) (c, s, i, l);
3319 }
3320
3321 Instead of calling a stub library function _call_via_xx (xx is
3322 the register name), GCC may inline the trampoline in the object
3323 file as below (register r2 has the address of call0a).
3324
3325 .global main
3326 .type main, %function
3327 ...
3328 bl .L1
3329 ...
3330 .size main, .-main
3331
3332 .L1:
3333 bx r2
3334
3335 The trampoline 'bx r2' doesn't belong to main. */
3336
3337static CORE_ADDR
bd2b40ac 3338arm_skip_bx_reg (frame_info_ptr frame, CORE_ADDR pc)
80d8d390
YQ
3339{
3340 /* The heuristics of recognizing such trampoline is that FRAME is
3341 executing in Thumb mode and the instruction on PC is 'bx Rm'. */
3342 if (arm_frame_is_thumb (frame))
3343 {
3344 gdb_byte buf[2];
3345
3346 if (target_read_memory (pc, buf, 2) == 0)
3347 {
3348 struct gdbarch *gdbarch = get_frame_arch (frame);
3349 enum bfd_endian byte_order_for_code
3350 = gdbarch_byte_order_for_code (gdbarch);
3351 uint16_t insn
3352 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3353
3354 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
3355 {
3356 CORE_ADDR dest
3357 = get_frame_register_unsigned (frame, bits (insn, 3, 6));
3358
3359 /* Clear the LSB so that gdb core sets step-resume
3360 breakpoint at the right address. */
3361 return UNMAKE_THUMB_ADDR (dest);
3362 }
3363 }
3364 }
3365
3366 return 0;
3367}
3368
909cf6ea 3369static struct arm_prologue_cache *
bd2b40ac 3370arm_make_stub_cache (frame_info_ptr this_frame)
909cf6ea 3371{
909cf6ea 3372 struct arm_prologue_cache *cache;
909cf6ea 3373
35d5d4ee 3374 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 3375 arm_cache_init (cache, this_frame);
909cf6ea 3376
ae7e2f45 3377 arm_gdbarch_tdep *tdep
08106042 3378 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45
CL
3379 arm_cache_set_active_sp_value (cache, tdep,
3380 get_frame_register_unsigned (this_frame,
3381 ARM_SP_REGNUM));
909cf6ea
DJ
3382
3383 return cache;
3384}
3385
3386/* Our frame ID for a stub frame is the current SP and LR. */
3387
3388static void
bd2b40ac 3389arm_stub_this_id (frame_info_ptr this_frame,
909cf6ea
DJ
3390 void **this_cache,
3391 struct frame_id *this_id)
3392{
3393 struct arm_prologue_cache *cache;
3394
3395 if (*this_cache == NULL)
a262aec2 3396 *this_cache = arm_make_stub_cache (this_frame);
9a3c8263 3397 cache = (struct arm_prologue_cache *) *this_cache;
909cf6ea 3398
ae7e2f45 3399 arm_gdbarch_tdep *tdep
08106042 3400 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45
CL
3401 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep),
3402 get_frame_pc (this_frame));
909cf6ea
DJ
3403}
3404
a262aec2
DJ
3405static int
3406arm_stub_unwind_sniffer (const struct frame_unwind *self,
bd2b40ac 3407 frame_info_ptr this_frame,
a262aec2 3408 void **this_prologue_cache)
909cf6ea 3409{
93d42b30 3410 CORE_ADDR addr_in_block;
948f8e3d 3411 gdb_byte dummy[4];
18d18ac8
YQ
3412 CORE_ADDR pc, start_addr;
3413 const char *name;
909cf6ea 3414
a262aec2 3415 addr_in_block = get_frame_address_in_block (this_frame);
18d18ac8 3416 pc = get_frame_pc (this_frame);
3e5d3a5a 3417 if (in_plt_section (addr_in_block)
fc36e839
DE
3418 /* We also use the stub winder if the target memory is unreadable
3419 to avoid having the prologue unwinder trying to read it. */
18d18ac8
YQ
3420 || target_read_memory (pc, dummy, 4) != 0)
3421 return 1;
3422
3423 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0
3424 && arm_skip_bx_reg (this_frame, pc) != 0)
a262aec2 3425 return 1;
909cf6ea 3426
a262aec2 3427 return 0;
909cf6ea
DJ
3428}
3429
a262aec2 3430struct frame_unwind arm_stub_unwind = {
a154d838 3431 "arm stub",
a262aec2 3432 NORMAL_FRAME,
8fbca658 3433 default_frame_unwind_stop_reason,
a262aec2
DJ
3434 arm_stub_this_id,
3435 arm_prologue_prev_register,
3436 NULL,
3437 arm_stub_unwind_sniffer
3438};
3439
2ae28aa9
YQ
3440/* Put here the code to store, into CACHE->saved_regs, the addresses
3441 of the saved registers of frame described by THIS_FRAME. CACHE is
3442 returned. */
3443
3444static struct arm_prologue_cache *
bd2b40ac 3445arm_m_exception_cache (frame_info_ptr this_frame)
2ae28aa9
YQ
3446{
3447 struct gdbarch *gdbarch = get_frame_arch (this_frame);
08106042 3448 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
2ae28aa9 3449 struct arm_prologue_cache *cache;
2ae28aa9
YQ
3450
3451 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 3452 arm_cache_init (cache, this_frame);
2ae28aa9 3453
55ea94da
FH
3454 /* ARMv7-M Architecture Reference "B1.5.6 Exception entry behavior"
3455 describes which bits in LR that define which stack was used prior
3456 to the exception and if FPU is used (causing extended stack frame). */
3457
8b73ee20
LM
3458 /* In the lockup state PC contains a lockup magic value.
3459 The PC value of the the next outer frame is irreversibly
3460 lost. The other registers are intact so LR likely contains
3461 PC of some frame next to the outer one, but we cannot analyze
3462 the next outer frame without knowing its PC
3463 therefore we do not know SP fixup for this frame.
3464 Some heuristics to resynchronize SP might be possible.
3465 For simplicity, just terminate the unwinding to prevent it going
3466 astray and attempting to read data/addresses it shouldn't,
3467 which may cause further issues due to side-effects. */
3468 CORE_ADDR pc = get_frame_pc (this_frame);
3469 if (arm_m_addr_is_lockup (pc))
3470 {
3471 /* The lockup can be real just in the innermost frame
3472 as the CPU is stopped and cannot create more frames.
3473 If we hit lockup magic PC in the other frame, it is
3474 just a sentinel at the top of stack: do not warn then. */
3475 if (frame_relative_level (this_frame) == 0)
3476 warning (_("ARM M in lockup state, stack unwinding terminated."));
3477
3478 /* Terminate any further stack unwinding. */
3479 arm_cache_set_active_sp_value (cache, tdep, 0);
3480 return cache;
3481 }
3482
df4860da 3483 CORE_ADDR lr = get_frame_register_unsigned (this_frame, ARM_LR_REGNUM);
ef273377 3484
148ca9dd
YR
3485 /* ARMv7-M Architecture Reference "A2.3.1 Arm core registers"
3486 states that LR is set to 0xffffffff on reset. ARMv8-M Architecture
3487 Reference "B3.3 Registers" states that LR is set to 0xffffffff on warm
3488 reset if Main Extension is implemented, otherwise the value is unknown. */
3489 if (lr == 0xffffffff)
3490 {
619cce4c
TS
3491 /* Terminate any further stack unwinding. */
3492 arm_cache_set_active_sp_value (cache, tdep, 0);
148ca9dd
YR
3493 return cache;
3494 }
3495
df4860da
TS
3496 /* Check FNC_RETURN indicator bits (24-31). */
3497 bool fnc_return = (((lr >> 24) & 0xff) == 0xfe);
3498 if (fnc_return)
ef273377 3499 {
df4860da
TS
3500 /* FNC_RETURN is only valid for targets with Security Extension. */
3501 if (!tdep->have_sec_ext)
3502 {
3503 error (_("While unwinding an exception frame, found unexpected Link "
3504 "Register value %s that requires the security extension, "
3505 "but the extension was not found or is disabled. This "
3506 "should not happen and may be caused by corrupt data or a "
3507 "bug in GDB."), phex (lr, ARM_INT_REGISTER_SIZE));
3508 }
3509
8c9ae6df
YR
3510 if (!arm_unwind_secure_frames)
3511 {
3512 warning (_("Non-secure to secure stack unwinding disabled."));
ef273377 3513
619cce4c
TS
3514 /* Terminate any further stack unwinding. */
3515 arm_cache_set_active_sp_value (cache, tdep, 0);
8c9ae6df
YR
3516 return cache;
3517 }
3518
df4860da 3519 ULONGEST xpsr = get_frame_register_unsigned (this_frame, ARM_PS_REGNUM);
b2e9e754 3520 if ((xpsr & 0x1ff) != 0)
8c9ae6df
YR
3521 /* Handler mode: This is the mode that exceptions are handled in. */
3522 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_msp_s_regnum);
ef273377 3523 else
8c9ae6df
YR
3524 /* Thread mode: This is the normal mode that programs run in. */
3525 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_psp_s_regnum);
ef273377 3526
df4860da 3527 CORE_ADDR unwound_sp = arm_cache_get_prev_sp_value (cache, tdep);
55ea94da 3528
8c9ae6df
YR
3529 /* Stack layout for a function call from Secure to Non-Secure state
3530 (ARMv8-M section B3.16):
55ea94da 3531
8c9ae6df
YR
3532 SP Offset
3533
3534 +-------------------+
3535 0x08 | |
3536 +-------------------+ <-- Original SP
3537 0x04 | Partial xPSR |
3538 +-------------------+
3539 0x00 | Return Address |
3540 +===================+ <-- New SP */
3541
3542 cache->saved_regs[ARM_PC_REGNUM].set_addr (unwound_sp + 0x00);
3543 cache->saved_regs[ARM_LR_REGNUM].set_addr (unwound_sp + 0x00);
3544 cache->saved_regs[ARM_PS_REGNUM].set_addr (unwound_sp + 0x04);
3545
3546 arm_cache_set_active_sp_value (cache, tdep, unwound_sp + 0x08);
ef273377
CL
3547
3548 return cache;
3549 }
3550
3551 /* Check EXC_RETURN indicator bits (24-31). */
df4860da 3552 bool exc_return = (((lr >> 24) & 0xff) == 0xff);
ef273377 3553 if (exc_return)
55ea94da 3554 {
df4860da
TS
3555 int sp_regnum;
3556 bool secure_stack_used = false;
3557 bool default_callee_register_stacking = false;
3558 bool exception_domain_is_secure = false;
3559 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3560
ef273377 3561 /* Check EXC_RETURN bit SPSEL if Main or Thread (process) stack used. */
df4860da 3562 bool process_stack_used = (bit (lr, 2) != 0);
ef273377
CL
3563
3564 if (tdep->have_sec_ext)
3565 {
df4860da
TS
3566 secure_stack_used = (bit (lr, 6) != 0);
3567 default_callee_register_stacking = (bit (lr, 5) != 0);
23295de1 3568 exception_domain_is_secure = (bit (lr, 0) != 0);
ef273377
CL
3569
3570 /* Unwinding from non-secure to secure can trip security
3571 measures. In order to avoid the debugger being
3572 intrusive, rely on the user to configure the requested
3573 mode. */
3574 if (secure_stack_used && !exception_domain_is_secure
3575 && !arm_unwind_secure_frames)
3576 {
3577 warning (_("Non-secure to secure stack unwinding disabled."));
3578
619cce4c
TS
3579 /* Terminate any further stack unwinding. */
3580 arm_cache_set_active_sp_value (cache, tdep, 0);
ef273377
CL
3581 return cache;
3582 }
3583
3584 if (process_stack_used)
3585 {
3586 if (secure_stack_used)
3587 /* Secure thread (process) stack used, use PSP_S as SP. */
df4860da 3588 sp_regnum = tdep->m_profile_psp_s_regnum;
ef273377
CL
3589 else
3590 /* Non-secure thread (process) stack used, use PSP_NS as SP. */
df4860da 3591 sp_regnum = tdep->m_profile_psp_ns_regnum;
ef273377
CL
3592 }
3593 else
3594 {
3595 if (secure_stack_used)
3596 /* Secure main stack used, use MSP_S as SP. */
df4860da 3597 sp_regnum = tdep->m_profile_msp_s_regnum;
ef273377
CL
3598 else
3599 /* Non-secure main stack used, use MSP_NS as SP. */
df4860da 3600 sp_regnum = tdep->m_profile_msp_ns_regnum;
ef273377
CL
3601 }
3602 }
3603 else
3604 {
3605 if (process_stack_used)
3606 /* Thread (process) stack used, use PSP as SP. */
df4860da 3607 sp_regnum = tdep->m_profile_psp_regnum;
ef273377
CL
3608 else
3609 /* Main stack used, use MSP as SP. */
df4860da
TS
3610 sp_regnum = tdep->m_profile_msp_regnum;
3611 }
3612
3613 /* Set the active SP regnum. */
3614 arm_cache_switch_prev_sp (cache, tdep, sp_regnum);
3615
3616 /* Fetch the SP to use for this frame. */
3617 CORE_ADDR unwound_sp = arm_cache_get_prev_sp_value (cache, tdep);
3618
3619 /* Exception entry context stacking are described in ARMv8-M (section
3620 B3.19) and ARMv7-M (sections B1.5.6 and B1.5.7) Architecture Reference
3621 Manuals.
3622
3623 The following figure shows the structure of the stack frame when
3624 Security and Floating-point extensions are present.
3625
3626 SP Offsets
3627 Without With
3628 Callee Regs Callee Regs
3629 (Secure -> Non-Secure)
3630 +-------------------+
3631 0xA8 | | 0xD0
3632 +===================+ --+ <-- Original SP
3633 0xA4 | S31 | 0xCC |
3634 +-------------------+ |
3635 ... | Additional FP context
3636 +-------------------+ |
3637 0x68 | S16 | 0x90 |
3638 +===================+ --+
3639 0x64 | Reserved | 0x8C |
3640 +-------------------+ |
3641 0x60 | FPSCR | 0x88 |
3642 +-------------------+ |
3643 0x5C | S15 | 0x84 | FP context
3644 +-------------------+ |
3645 ... |
3646 +-------------------+ |
3647 0x20 | S0 | 0x48 |
3648 +===================+ --+
3649 0x1C | xPSR | 0x44 |
3650 +-------------------+ |
3651 0x18 | Return address | 0x40 |
3652 +-------------------+ |
3653 0x14 | LR(R14) | 0x3C |
3654 +-------------------+ |
3655 0x10 | R12 | 0x38 | State context
3656 +-------------------+ |
3657 0x0C | R3 | 0x34 |
3658 +-------------------+ |
3659 ... |
3660 +-------------------+ |
3661 0x00 | R0 | 0x28 |
3662 +===================+ --+
3663 | R11 | 0x24 |
3664 +-------------------+ |
3665 ... |
3666 +-------------------+ | Additional state
3667 | R4 | 0x08 | context when
3668 +-------------------+ | transitioning from
3669 | Reserved | 0x04 | Secure to Non-Secure
3670 +-------------------+ |
3671 | Magic signature | 0x00 |
3672 +===================+ --+ <-- New SP */
3673
3674 uint32_t sp_r0_offset = 0;
3675
3676 /* With the Security extension, the hardware saves R4..R11 too. */
3677 if (tdep->have_sec_ext && secure_stack_used
23295de1 3678 && (!default_callee_register_stacking || !exception_domain_is_secure))
df4860da
TS
3679 {
3680 /* Read R4..R11 from the integer callee registers. */
3681 cache->saved_regs[4].set_addr (unwound_sp + 0x08);
3682 cache->saved_regs[5].set_addr (unwound_sp + 0x0C);
3683 cache->saved_regs[6].set_addr (unwound_sp + 0x10);
3684 cache->saved_regs[7].set_addr (unwound_sp + 0x14);
3685 cache->saved_regs[8].set_addr (unwound_sp + 0x18);
3686 cache->saved_regs[9].set_addr (unwound_sp + 0x1C);
3687 cache->saved_regs[10].set_addr (unwound_sp + 0x20);
3688 cache->saved_regs[11].set_addr (unwound_sp + 0x24);
3689 sp_r0_offset = 0x28;
3690 }
3691
3692 /* The hardware saves eight 32-bit words, comprising xPSR,
3693 ReturnAddress, LR (R14), R12, R3, R2, R1, R0. See details in
3694 "B1.5.6 Exception entry behavior" in
3695 "ARMv7-M Architecture Reference Manual". */
3696 cache->saved_regs[0].set_addr (unwound_sp + sp_r0_offset);
3697 cache->saved_regs[1].set_addr (unwound_sp + sp_r0_offset + 0x04);
3698 cache->saved_regs[2].set_addr (unwound_sp + sp_r0_offset + 0x08);
3699 cache->saved_regs[3].set_addr (unwound_sp + sp_r0_offset + 0x0C);
3700 cache->saved_regs[ARM_IP_REGNUM].set_addr (unwound_sp + sp_r0_offset
3701 + 0x10);
3702 cache->saved_regs[ARM_LR_REGNUM].set_addr (unwound_sp + sp_r0_offset
3703 + 0x14);
3704 cache->saved_regs[ARM_PC_REGNUM].set_addr (unwound_sp + sp_r0_offset
3705 + 0x18);
3706 cache->saved_regs[ARM_PS_REGNUM].set_addr (unwound_sp + sp_r0_offset
3707 + 0x1C);
3708
3709 /* Check EXC_RETURN bit FTYPE if extended stack frame (FPU regs stored)
3710 type used. */
3711 bool extended_frame_used = (bit (lr, 4) == 0);
3712 if (extended_frame_used)
3713 {
3714 ULONGEST fpccr;
60c90d8c 3715 ULONGEST fpcar;
df4860da
TS
3716
3717 /* Read FPCCR register. */
ce6c3d25
TS
3718 if (!safe_read_memory_unsigned_integer (FPCCR, ARM_INT_REGISTER_SIZE,
3719 byte_order, &fpccr))
3720 {
3721 warning (_("Could not fetch required FPCCR content. Further "
3722 "unwinding is impossible."));
3723 arm_cache_set_active_sp_value (cache, tdep, 0);
3724 return cache;
3725 }
df4860da 3726
60c90d8c
TS
3727 /* Read FPCAR register. */
3728 if (!safe_read_memory_unsigned_integer (FPCAR, ARM_INT_REGISTER_SIZE,
3729 byte_order, &fpcar))
3730 {
3731 warning (_("Could not fetch FPCAR content. Further unwinding of "
3732 "FP register values will be unreliable."));
3733 fpcar = 0;
3734 }
3735
3736 bool fpccr_aspen = bit (fpccr, 31);
3737 bool fpccr_lspen = bit (fpccr, 30);
3738 bool fpccr_ts = bit (fpccr, 26);
3739 bool fpccr_lspact = bit (fpccr, 0);
3740
3741 /* The LSPEN and ASPEN bits indicate if the lazy state preservation
3742 for FP registers is enabled or disabled. The LSPACT bit indicate,
3743 together with FPCAR, if the lazy state preservation feature is
3744 active for the current frame or for another frame.
3745 See "Lazy context save of FP state", in B1.5.7, also ARM AN298,
3746 supported by Cortex-M4F architecture for details. */
3747 bool fpcar_points_to_this_frame = ((unwound_sp + sp_r0_offset + 0x20)
3748 == (fpcar & ~0x7));
3749 bool read_fp_regs_from_stack = (!(fpccr_aspen && fpccr_lspen
3750 && fpccr_lspact
3751 && fpcar_points_to_this_frame));
df4860da
TS
3752
3753 /* Extended stack frame type used. */
60c90d8c 3754 if (read_fp_regs_from_stack)
df4860da 3755 {
60c90d8c
TS
3756 CORE_ADDR addr = unwound_sp + sp_r0_offset + 0x20;
3757 for (int i = 0; i < 8; i++)
3758 {
3759 cache->saved_regs[ARM_D0_REGNUM + i].set_addr (addr);
3760 addr += 8;
3761 }
df4860da
TS
3762 }
3763 cache->saved_regs[ARM_FPSCR_REGNUM].set_addr (unwound_sp
3764 + sp_r0_offset + 0x60);
ef273377 3765
df4860da
TS
3766 if (tdep->have_sec_ext && !default_callee_register_stacking
3767 && fpccr_ts)
3768 {
3769 /* Handle floating-point callee saved registers. */
60c90d8c 3770 if (read_fp_regs_from_stack)
df4860da 3771 {
60c90d8c
TS
3772 CORE_ADDR addr = unwound_sp + sp_r0_offset + 0x68;
3773 for (int i = 8; i < 16; i++)
3774 {
3775 cache->saved_regs[ARM_D0_REGNUM + i].set_addr (addr);
3776 addr += 8;
3777 }
df4860da 3778 }
2ae28aa9 3779
df4860da
TS
3780 arm_cache_set_active_sp_value (cache, tdep,
3781 unwound_sp + sp_r0_offset + 0xA8);
3782 }
3783 else
ef273377 3784 {
df4860da
TS
3785 /* Offset 0x64 is reserved. */
3786 arm_cache_set_active_sp_value (cache, tdep,
3787 unwound_sp + sp_r0_offset + 0x68);
ef273377 3788 }
ef273377
CL
3789 }
3790 else
3791 {
df4860da 3792 /* Standard stack frame type used. */
2d9cf99d 3793 arm_cache_set_active_sp_value (cache, tdep,
df4860da 3794 unwound_sp + sp_r0_offset + 0x20);
ef273377 3795 }
55ea94da 3796
df4860da
TS
3797 /* If bit 9 of the saved xPSR is set, then there is a four-byte
3798 aligner between the top of the 32-byte stack frame and the
3799 previous context's stack pointer. */
3800 ULONGEST xpsr;
ce6c3d25
TS
3801 if (!safe_read_memory_unsigned_integer (cache->saved_regs[ARM_PS_REGNUM]
3802 .addr (), ARM_INT_REGISTER_SIZE,
3803 byte_order, &xpsr))
3804 {
3805 warning (_("Could not fetch required XPSR content. Further "
3806 "unwinding is impossible."));
3807 arm_cache_set_active_sp_value (cache, tdep, 0);
3808 return cache;
3809 }
3810
df4860da
TS
3811 if (bit (xpsr, 9) != 0)
3812 {
3813 CORE_ADDR new_sp = arm_cache_get_prev_sp_value (cache, tdep) + 4;
3814 arm_cache_set_active_sp_value (cache, tdep, new_sp);
3815 }
2ae28aa9 3816
df4860da
TS
3817 return cache;
3818 }
3819
f34652de
PA
3820 internal_error (_("While unwinding an exception frame, "
3821 "found unexpected Link Register value "
3822 "%s. This should not happen and may "
3823 "be caused by corrupt data or a bug in"
3824 " GDB."),
df4860da 3825 phex (lr, ARM_INT_REGISTER_SIZE));
2ae28aa9
YQ
3826}
3827
ce6c3d25
TS
3828/* Implementation of the stop_reason hook for arm_m_exception frames. */
3829
3830static enum unwind_stop_reason
3831arm_m_exception_frame_unwind_stop_reason (frame_info_ptr this_frame,
3832 void **this_cache)
3833{
3834 struct arm_prologue_cache *cache;
3835 arm_gdbarch_tdep *tdep
3836 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
3837
3838 if (*this_cache == NULL)
3839 *this_cache = arm_m_exception_cache (this_frame);
3840 cache = (struct arm_prologue_cache *) *this_cache;
3841
3842 /* If we've hit a wall, stop. */
3843 if (arm_cache_get_prev_sp_value (cache, tdep) == 0)
3844 return UNWIND_OUTERMOST;
3845
3846 return UNWIND_NO_REASON;
3847}
3848
2ae28aa9
YQ
3849/* Implementation of function hook 'this_id' in
3850 'struct frame_uwnind'. */
3851
3852static void
bd2b40ac 3853arm_m_exception_this_id (frame_info_ptr this_frame,
2ae28aa9
YQ
3854 void **this_cache,
3855 struct frame_id *this_id)
3856{
3857 struct arm_prologue_cache *cache;
3858
3859 if (*this_cache == NULL)
3860 *this_cache = arm_m_exception_cache (this_frame);
9a3c8263 3861 cache = (struct arm_prologue_cache *) *this_cache;
2ae28aa9
YQ
3862
3863 /* Our frame ID for a stub frame is the current SP and LR. */
ae7e2f45 3864 arm_gdbarch_tdep *tdep
08106042 3865 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45 3866 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep),
2ae28aa9
YQ
3867 get_frame_pc (this_frame));
3868}
3869
3870/* Implementation of function hook 'prev_register' in
3871 'struct frame_uwnind'. */
3872
3873static struct value *
bd2b40ac 3874arm_m_exception_prev_register (frame_info_ptr this_frame,
2ae28aa9
YQ
3875 void **this_cache,
3876 int prev_regnum)
3877{
2ae28aa9 3878 struct arm_prologue_cache *cache;
ef273377 3879 CORE_ADDR sp_value;
2ae28aa9
YQ
3880
3881 if (*this_cache == NULL)
3882 *this_cache = arm_m_exception_cache (this_frame);
9a3c8263 3883 cache = (struct arm_prologue_cache *) *this_cache;
2ae28aa9
YQ
3884
3885 /* The value was already reconstructed into PREV_SP. */
ae7e2f45 3886 arm_gdbarch_tdep *tdep
08106042 3887 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
2ae28aa9
YQ
3888 if (prev_regnum == ARM_SP_REGNUM)
3889 return frame_unwind_got_constant (this_frame, prev_regnum,
ae7e2f45 3890 arm_cache_get_prev_sp_value (cache, tdep));
2ae28aa9 3891
8c9ae6df
YR
3892 /* If we are asked to unwind the PC, strip the saved T bit. */
3893 if (prev_regnum == ARM_PC_REGNUM)
3894 {
3895 struct value *value = trad_frame_get_prev_register (this_frame,
3896 cache->saved_regs,
3897 prev_regnum);
3898 CORE_ADDR pc = value_as_address (value);
3899 return frame_unwind_got_constant (this_frame, prev_regnum,
287de656 3900 UNMAKE_THUMB_ADDR (pc));
8c9ae6df
YR
3901 }
3902
ef273377
CL
3903 /* The value might be one of the alternative SP, if so, use the
3904 value already constructed. */
d65edaa0 3905 if (arm_is_alternative_sp_register (tdep, prev_regnum))
ef273377
CL
3906 {
3907 sp_value = arm_cache_get_sp_register (cache, tdep, prev_regnum);
3908 return frame_unwind_got_constant (this_frame, prev_regnum, sp_value);
3909 }
3910
8c9ae6df
YR
3911 /* If we are asked to unwind the xPSR, set T bit if PC is in thumb mode.
3912 LR register is unreliable as it contains FNC_RETURN or EXC_RETURN
3913 pattern. */
3914 if (prev_regnum == ARM_PS_REGNUM)
3915 {
3916 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3917 struct value *value = trad_frame_get_prev_register (this_frame,
3918 cache->saved_regs,
3919 ARM_PC_REGNUM);
3920 CORE_ADDR pc = value_as_address (value);
3921 value = trad_frame_get_prev_register (this_frame, cache->saved_regs,
3922 ARM_PS_REGNUM);
3923 ULONGEST xpsr = value_as_long (value);
3924
3925 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
3926 xpsr = reconstruct_t_bit (gdbarch, pc, xpsr);
3927 return frame_unwind_got_constant (this_frame, ARM_PS_REGNUM, xpsr);
3928 }
3929
2ae28aa9
YQ
3930 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
3931 prev_regnum);
3932}
3933
3934/* Implementation of function hook 'sniffer' in
3935 'struct frame_uwnind'. */
3936
3937static int
3938arm_m_exception_unwind_sniffer (const struct frame_unwind *self,
bd2b40ac 3939 frame_info_ptr this_frame,
2ae28aa9
YQ
3940 void **this_prologue_cache)
3941{
ef273377 3942 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2ae28aa9
YQ
3943 CORE_ADDR this_pc = get_frame_pc (this_frame);
3944
3945 /* No need to check is_m; this sniffer is only registered for
3946 M-profile architectures. */
3947
ca90e760 3948 /* Check if exception frame returns to a magic PC value. */
ef273377 3949 return arm_m_addr_is_magic (gdbarch, this_pc);
2ae28aa9
YQ
3950}
3951
8b73ee20
LM
3952/* Frame unwinder for M-profile exceptions (EXC_RETURN on stack),
3953 lockup and secure/nonsecure interstate function calls (FNC_RETURN). */
2ae28aa9
YQ
3954
3955struct frame_unwind arm_m_exception_unwind =
3956{
8b73ee20 3957 "arm m exception lockup sec_fnc",
2ae28aa9 3958 SIGTRAMP_FRAME,
ce6c3d25 3959 arm_m_exception_frame_unwind_stop_reason,
2ae28aa9
YQ
3960 arm_m_exception_this_id,
3961 arm_m_exception_prev_register,
3962 NULL,
3963 arm_m_exception_unwind_sniffer
3964};
3965
24de872b 3966static CORE_ADDR
bd2b40ac 3967arm_normal_frame_base (frame_info_ptr this_frame, void **this_cache)
24de872b
DJ
3968{
3969 struct arm_prologue_cache *cache;
3970
eb5492fa 3971 if (*this_cache == NULL)
a262aec2 3972 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 3973 cache = (struct arm_prologue_cache *) *this_cache;
eb5492fa 3974
ae7e2f45 3975 arm_gdbarch_tdep *tdep
08106042 3976 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45 3977 return arm_cache_get_prev_sp_value (cache, tdep) - cache->framesize;
24de872b
DJ
3978}
3979
eb5492fa
DJ
3980struct frame_base arm_normal_base = {
3981 &arm_prologue_unwind,
3982 arm_normal_frame_base,
3983 arm_normal_frame_base,
3984 arm_normal_frame_base
3985};
3986
5cf11483
TS
3987struct arm_dwarf2_prev_register_cache
3988{
3bfdcabb 3989 /* Cached value of the corresponding stack pointer for the inner frame. */
5cf11483
TS
3990 CORE_ADDR sp;
3991 CORE_ADDR msp;
3992 CORE_ADDR msp_s;
3993 CORE_ADDR msp_ns;
3994 CORE_ADDR psp;
3995 CORE_ADDR psp_s;
3996 CORE_ADDR psp_ns;
3997};
3998
b39cc962 3999static struct value *
bd2b40ac 4000arm_dwarf2_prev_register (frame_info_ptr this_frame, void **this_cache,
b39cc962
DJ
4001 int regnum)
4002{
24568a2c 4003 struct gdbarch * gdbarch = get_frame_arch (this_frame);
08106042 4004 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
8c9ae6df
YR
4005 CORE_ADDR lr;
4006 ULONGEST cpsr;
5cf11483
TS
4007 arm_dwarf2_prev_register_cache *cache
4008 = ((arm_dwarf2_prev_register_cache *)
4009 dwarf2_frame_get_fn_data (this_frame, this_cache,
4010 arm_dwarf2_prev_register));
4011
4012 if (!cache)
4013 {
4014 const unsigned int size = sizeof (struct arm_dwarf2_prev_register_cache);
4015 cache = ((arm_dwarf2_prev_register_cache *)
4016 dwarf2_frame_allocate_fn_data (this_frame, this_cache,
4017 arm_dwarf2_prev_register, size));
4018
4019 if (tdep->have_sec_ext)
4020 {
4021 cache->sp
4022 = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
4023
4024 cache->msp_s
4025 = get_frame_register_unsigned (this_frame,
4026 tdep->m_profile_msp_s_regnum);
4027 cache->msp_ns
4028 = get_frame_register_unsigned (this_frame,
4029 tdep->m_profile_msp_ns_regnum);
4030 cache->psp_s
4031 = get_frame_register_unsigned (this_frame,
4032 tdep->m_profile_psp_s_regnum);
4033 cache->psp_ns
4034 = get_frame_register_unsigned (this_frame,
4035 tdep->m_profile_psp_ns_regnum);
4036 }
4037 else if (tdep->is_m)
4038 {
4039 cache->sp
4040 = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
4041
4042 cache->msp
4043 = get_frame_register_unsigned (this_frame,
4044 tdep->m_profile_msp_regnum);
4045 cache->psp
4046 = get_frame_register_unsigned (this_frame,
4047 tdep->m_profile_psp_regnum);
4048 }
4049 }
b39cc962 4050
42e11f36 4051 if (regnum == ARM_PC_REGNUM)
b39cc962 4052 {
b39cc962
DJ
4053 /* The PC is normally copied from the return column, which
4054 describes saves of LR. However, that version may have an
4055 extra bit set to indicate Thumb state. The bit is not
4056 part of the PC. */
a01567f4
LM
4057
4058 /* Record in the frame whether the return address was signed. */
4059 if (tdep->have_pacbti)
4060 {
4061 CORE_ADDR ra_auth_code
4062 = frame_unwind_register_unsigned (this_frame,
4063 tdep->pacbti_pseudo_base);
4064
4065 if (ra_auth_code != 0)
4066 set_frame_previous_pc_masked (this_frame);
4067 }
4068
b39cc962
DJ
4069 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
4070 return frame_unwind_got_constant (this_frame, regnum,
24568a2c 4071 arm_addr_bits_remove (gdbarch, lr));
42e11f36
TS
4072 }
4073 else if (regnum == ARM_PS_REGNUM)
4074 {
b39cc962 4075 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
ca38c58e 4076 cpsr = get_frame_register_unsigned (this_frame, regnum);
b39cc962 4077 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
8c9ae6df 4078 cpsr = reconstruct_t_bit (gdbarch, lr, cpsr);
ca38c58e 4079 return frame_unwind_got_constant (this_frame, regnum, cpsr);
b39cc962 4080 }
a6e4a48c
YR
4081 else if (arm_is_alternative_sp_register (tdep, regnum))
4082 {
4083 /* Handle the alternative SP registers on Cortex-M. */
4084 bool override_with_sp_value = false;
4085 CORE_ADDR val;
4086
4087 if (tdep->have_sec_ext)
4088 {
a6e4a48c 4089 bool is_msp = (regnum == tdep->m_profile_msp_regnum)
5cf11483 4090 && (cache->msp_s == cache->sp || cache->msp_ns == cache->sp);
a6e4a48c 4091 bool is_msp_s = (regnum == tdep->m_profile_msp_s_regnum)
5cf11483 4092 && (cache->msp_s == cache->sp);
a6e4a48c 4093 bool is_msp_ns = (regnum == tdep->m_profile_msp_ns_regnum)
5cf11483 4094 && (cache->msp_ns == cache->sp);
a6e4a48c 4095 bool is_psp = (regnum == tdep->m_profile_psp_regnum)
5cf11483 4096 && (cache->psp_s == cache->sp || cache->psp_ns == cache->sp);
a6e4a48c 4097 bool is_psp_s = (regnum == tdep->m_profile_psp_s_regnum)
5cf11483 4098 && (cache->psp_s == cache->sp);
a6e4a48c 4099 bool is_psp_ns = (regnum == tdep->m_profile_psp_ns_regnum)
5cf11483 4100 && (cache->psp_ns == cache->sp);
a6e4a48c
YR
4101
4102 override_with_sp_value = is_msp || is_msp_s || is_msp_ns
4103 || is_psp || is_psp_s || is_psp_ns;
4104
4105 }
4106 else if (tdep->is_m)
4107 {
5cf11483
TS
4108 bool is_msp = (regnum == tdep->m_profile_msp_regnum)
4109 && (cache->sp == cache->msp);
4110 bool is_psp = (regnum == tdep->m_profile_psp_regnum)
4111 && (cache->sp == cache->psp);
a6e4a48c
YR
4112
4113 override_with_sp_value = is_msp || is_psp;
4114 }
4115
4116 if (override_with_sp_value)
4117 {
4118 /* Use value of SP from previous frame. */
bd2b40ac 4119 frame_info_ptr prev_frame = get_prev_frame (this_frame);
a6e4a48c
YR
4120 if (prev_frame)
4121 val = get_frame_register_unsigned (prev_frame, ARM_SP_REGNUM);
4122 else
4123 val = get_frame_base (this_frame);
4124 }
4125 else
4126 /* Use value for the register from previous frame. */
4127 val = get_frame_register_unsigned (this_frame, regnum);
4128
4129 return frame_unwind_got_constant (this_frame, regnum, val);
4130 }
42e11f36 4131
f34652de 4132 internal_error (_("Unexpected register %d"), regnum);
b39cc962
DJ
4133}
4134
c9cf6e20 4135/* Implement the stack_frame_destroyed_p gdbarch method. */
4024ca99
UW
4136
4137static int
c9cf6e20 4138thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
4024ca99
UW
4139{
4140 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4141 unsigned int insn, insn2;
4142 int found_return = 0, found_stack_adjust = 0;
4143 CORE_ADDR func_start, func_end;
4144 CORE_ADDR scan_pc;
4145 gdb_byte buf[4];
4146
4147 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
4148 return 0;
4149
4150 /* The epilogue is a sequence of instructions along the following lines:
4151
4152 - add stack frame size to SP or FP
4153 - [if frame pointer used] restore SP from FP
4154 - restore registers from SP [may include PC]
4155 - a return-type instruction [if PC wasn't already restored]
4156
4157 In a first pass, we scan forward from the current PC and verify the
4158 instructions we find as compatible with this sequence, ending in a
4159 return instruction.
4160
4161 However, this is not sufficient to distinguish indirect function calls
4162 within a function from indirect tail calls in the epilogue in some cases.
4163 Therefore, if we didn't already find any SP-changing instruction during
4164 forward scan, we add a backward scanning heuristic to ensure we actually
4165 are in the epilogue. */
4166
4167 scan_pc = pc;
4168 while (scan_pc < func_end && !found_return)
4169 {
4170 if (target_read_memory (scan_pc, buf, 2))
4171 break;
4172
4173 scan_pc += 2;
4174 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
4175
4176 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
4177 found_return = 1;
4178 else if (insn == 0x46f7) /* mov pc, lr */
4179 found_return = 1;
540314bd 4180 else if (thumb_instruction_restores_sp (insn))
4024ca99 4181 {
b7576e5c 4182 if ((insn & 0xff00) == 0xbd00) /* pop <registers, PC> */
4024ca99
UW
4183 found_return = 1;
4184 }
db24da6d 4185 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instruction */
4024ca99
UW
4186 {
4187 if (target_read_memory (scan_pc, buf, 2))
4188 break;
4189
4190 scan_pc += 2;
4191 insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);
4192
4193 if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
4194 {
4024ca99
UW
4195 if (insn2 & 0x8000) /* <registers> include PC. */
4196 found_return = 1;
4197 }
4198 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
4199 && (insn2 & 0x0fff) == 0x0b04)
4200 {
4024ca99
UW
4201 if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC. */
4202 found_return = 1;
4203 }
4204 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
4205 && (insn2 & 0x0e00) == 0x0a00)
6b65d1b6 4206 ;
4024ca99
UW
4207 else
4208 break;
4209 }
4210 else
4211 break;
4212 }
4213
4214 if (!found_return)
4215 return 0;
4216
4217 /* Since any instruction in the epilogue sequence, with the possible
4218 exception of return itself, updates the stack pointer, we need to
4219 scan backwards for at most one instruction. Try either a 16-bit or
4220 a 32-bit instruction. This is just a heuristic, so we do not worry
0963b4bd 4221 too much about false positives. */
4024ca99 4222
6b65d1b6
YQ
4223 if (pc - 4 < func_start)
4224 return 0;
4225 if (target_read_memory (pc - 4, buf, 4))
4226 return 0;
4024ca99 4227
6b65d1b6
YQ
4228 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
4229 insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);
4230
4231 if (thumb_instruction_restores_sp (insn2))
4232 found_stack_adjust = 1;
4233 else if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
4234 found_stack_adjust = 1;
4235 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
4236 && (insn2 & 0x0fff) == 0x0b04)
4237 found_stack_adjust = 1;
4238 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
4239 && (insn2 & 0x0e00) == 0x0a00)
4240 found_stack_adjust = 1;
4024ca99
UW
4241
4242 return found_stack_adjust;
4243}
4244
4024ca99 4245static int
c58b006a 4246arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch, CORE_ADDR pc)
4024ca99
UW
4247{
4248 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4249 unsigned int insn;
f303bc3e 4250 int found_return;
4024ca99
UW
4251 CORE_ADDR func_start, func_end;
4252
4024ca99
UW
4253 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
4254 return 0;
4255
4256 /* We are in the epilogue if the previous instruction was a stack
4257 adjustment and the next instruction is a possible return (bx, mov
4258 pc, or pop). We could have to scan backwards to find the stack
4259 adjustment, or forwards to find the return, but this is a decent
4260 approximation. First scan forwards. */
4261
4262 found_return = 0;
4263 insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
4264 if (bits (insn, 28, 31) != INST_NV)
4265 {
4266 if ((insn & 0x0ffffff0) == 0x012fff10)
4267 /* BX. */
4268 found_return = 1;
4269 else if ((insn & 0x0ffffff0) == 0x01a0f000)
4270 /* MOV PC. */
4271 found_return = 1;
4272 else if ((insn & 0x0fff0000) == 0x08bd0000
4273 && (insn & 0x0000c000) != 0)
4274 /* POP (LDMIA), including PC or LR. */
4275 found_return = 1;
4276 }
4277
4278 if (!found_return)
4279 return 0;
4280
4281 /* Scan backwards. This is just a heuristic, so do not worry about
4282 false positives from mode changes. */
4283
4284 if (pc < func_start + 4)
4285 return 0;
4286
4287 insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
f303bc3e 4288 if (arm_instruction_restores_sp (insn))
4024ca99
UW
4289 return 1;
4290
4291 return 0;
4292}
4293
c58b006a
YQ
4294/* Implement the stack_frame_destroyed_p gdbarch method. */
4295
4296static int
4297arm_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
4298{
4299 if (arm_pc_is_thumb (gdbarch, pc))
4300 return thumb_stack_frame_destroyed_p (gdbarch, pc);
4301 else
4302 return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
4303}
4024ca99 4304
2dd604e7
RE
4305/* When arguments must be pushed onto the stack, they go on in reverse
4306 order. The code below implements a FILO (stack) to do this. */
4307
0fecb1a7 4308struct arm_stack_item
2dd604e7
RE
4309{
4310 int len;
0fecb1a7 4311 struct arm_stack_item *prev;
7c543f7b 4312 gdb_byte *data;
2dd604e7
RE
4313};
4314
0fecb1a7
TT
4315static struct arm_stack_item *
4316push_stack_item (struct arm_stack_item *prev, const gdb_byte *contents,
4317 int len)
2dd604e7 4318{
0fecb1a7
TT
4319 struct arm_stack_item *si;
4320 si = XNEW (struct arm_stack_item);
7c543f7b 4321 si->data = (gdb_byte *) xmalloc (len);
2dd604e7
RE
4322 si->len = len;
4323 si->prev = prev;
4324 memcpy (si->data, contents, len);
4325 return si;
4326}
4327
0fecb1a7
TT
4328static struct arm_stack_item *
4329pop_stack_item (struct arm_stack_item *si)
2dd604e7 4330{
0fecb1a7 4331 struct arm_stack_item *dead = si;
2dd604e7
RE
4332 si = si->prev;
4333 xfree (dead->data);
4334 xfree (dead);
4335 return si;
4336}
4337
030197b4
AB
4338/* Implement the gdbarch type alignment method, overrides the generic
4339 alignment algorithm for anything that is arm specific. */
2af48f68 4340
030197b4
AB
4341static ULONGEST
4342arm_type_align (gdbarch *gdbarch, struct type *t)
2af48f68 4343{
2af48f68 4344 t = check_typedef (t);
bd63c870 4345 if (t->code () == TYPE_CODE_ARRAY && t->is_vector ())
2af48f68 4346 {
030197b4
AB
4347 /* Use the natural alignment for vector types (the same for
4348 scalar type), but the maximum alignment is 64-bit. */
df86565b 4349 if (t->length () > 8)
030197b4 4350 return 8;
c4312b19 4351 else
df86565b 4352 return t->length ();
2af48f68 4353 }
030197b4
AB
4354
4355 /* Allow the common code to calculate the alignment. */
4356 return 0;
2af48f68
PB
4357}
4358
90445bd3
DJ
4359/* Possible base types for a candidate for passing and returning in
4360 VFP registers. */
4361
4362enum arm_vfp_cprc_base_type
4363{
4364 VFP_CPRC_UNKNOWN,
4365 VFP_CPRC_SINGLE,
4366 VFP_CPRC_DOUBLE,
4367 VFP_CPRC_VEC64,
4368 VFP_CPRC_VEC128
4369};
4370
4371/* The length of one element of base type B. */
4372
4373static unsigned
4374arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
4375{
4376 switch (b)
4377 {
4378 case VFP_CPRC_SINGLE:
4379 return 4;
4380 case VFP_CPRC_DOUBLE:
4381 return 8;
4382 case VFP_CPRC_VEC64:
4383 return 8;
4384 case VFP_CPRC_VEC128:
4385 return 16;
4386 default:
f34652de 4387 internal_error (_("Invalid VFP CPRC type: %d."),
90445bd3
DJ
4388 (int) b);
4389 }
4390}
4391
4392/* The character ('s', 'd' or 'q') for the type of VFP register used
4393 for passing base type B. */
4394
4395static int
4396arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
4397{
4398 switch (b)
4399 {
4400 case VFP_CPRC_SINGLE:
4401 return 's';
4402 case VFP_CPRC_DOUBLE:
4403 return 'd';
4404 case VFP_CPRC_VEC64:
4405 return 'd';
4406 case VFP_CPRC_VEC128:
4407 return 'q';
4408 default:
f34652de 4409 internal_error (_("Invalid VFP CPRC type: %d."),
90445bd3
DJ
4410 (int) b);
4411 }
4412}
4413
4414/* Determine whether T may be part of a candidate for passing and
4415 returning in VFP registers, ignoring the limit on the total number
4416 of components. If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
4417 classification of the first valid component found; if it is not
4418 VFP_CPRC_UNKNOWN, all components must have the same classification
4419 as *BASE_TYPE. If it is found that T contains a type not permitted
4420 for passing and returning in VFP registers, a type differently
4421 classified from *BASE_TYPE, or two types differently classified
4422 from each other, return -1, otherwise return the total number of
4423 base-type elements found (possibly 0 in an empty structure or
817e0957
YQ
4424 array). Vector types are not currently supported, matching the
4425 generic AAPCS support. */
90445bd3
DJ
4426
4427static int
4428arm_vfp_cprc_sub_candidate (struct type *t,
4429 enum arm_vfp_cprc_base_type *base_type)
4430{
4431 t = check_typedef (t);
78134374 4432 switch (t->code ())
90445bd3
DJ
4433 {
4434 case TYPE_CODE_FLT:
df86565b 4435 switch (t->length ())
90445bd3
DJ
4436 {
4437 case 4:
4438 if (*base_type == VFP_CPRC_UNKNOWN)
4439 *base_type = VFP_CPRC_SINGLE;
4440 else if (*base_type != VFP_CPRC_SINGLE)
4441 return -1;
4442 return 1;
4443
4444 case 8:
4445 if (*base_type == VFP_CPRC_UNKNOWN)
4446 *base_type = VFP_CPRC_DOUBLE;
4447 else if (*base_type != VFP_CPRC_DOUBLE)
4448 return -1;
4449 return 1;
4450
4451 default:
4452 return -1;
4453 }
4454 break;
4455
817e0957
YQ
4456 case TYPE_CODE_COMPLEX:
4457 /* Arguments of complex T where T is one of the types float or
4458 double get treated as if they are implemented as:
4459
4460 struct complexT
4461 {
4462 T real;
4463 T imag;
5f52445b
YQ
4464 };
4465
4466 */
df86565b 4467 switch (t->length ())
817e0957
YQ
4468 {
4469 case 8:
4470 if (*base_type == VFP_CPRC_UNKNOWN)
4471 *base_type = VFP_CPRC_SINGLE;
4472 else if (*base_type != VFP_CPRC_SINGLE)
4473 return -1;
4474 return 2;
4475
4476 case 16:
4477 if (*base_type == VFP_CPRC_UNKNOWN)
4478 *base_type = VFP_CPRC_DOUBLE;
4479 else if (*base_type != VFP_CPRC_DOUBLE)
4480 return -1;
4481 return 2;
4482
4483 default:
4484 return -1;
4485 }
4486 break;
4487
90445bd3
DJ
4488 case TYPE_CODE_ARRAY:
4489 {
bd63c870 4490 if (t->is_vector ())
90445bd3 4491 {
c4312b19
YQ
4492 /* A 64-bit or 128-bit containerized vector type are VFP
4493 CPRCs. */
df86565b 4494 switch (t->length ())
c4312b19
YQ
4495 {
4496 case 8:
4497 if (*base_type == VFP_CPRC_UNKNOWN)
4498 *base_type = VFP_CPRC_VEC64;
4499 return 1;
4500 case 16:
4501 if (*base_type == VFP_CPRC_UNKNOWN)
4502 *base_type = VFP_CPRC_VEC128;
4503 return 1;
4504 default:
4505 return -1;
4506 }
4507 }
4508 else
4509 {
4510 int count;
4511 unsigned unitlen;
4512
27710edb 4513 count = arm_vfp_cprc_sub_candidate (t->target_type (),
c4312b19
YQ
4514 base_type);
4515 if (count == -1)
4516 return -1;
df86565b 4517 if (t->length () == 0)
c4312b19
YQ
4518 {
4519 gdb_assert (count == 0);
4520 return 0;
4521 }
4522 else if (count == 0)
4523 return -1;
4524 unitlen = arm_vfp_cprc_unit_length (*base_type);
df86565b
SM
4525 gdb_assert ((t->length () % unitlen) == 0);
4526 return t->length () / unitlen;
90445bd3 4527 }
90445bd3
DJ
4528 }
4529 break;
4530
4531 case TYPE_CODE_STRUCT:
4532 {
4533 int count = 0;
4534 unsigned unitlen;
4535 int i;
1f704f76 4536 for (i = 0; i < t->num_fields (); i++)
90445bd3 4537 {
1040b979
YQ
4538 int sub_count = 0;
4539
c819a338 4540 if (!t->field (i).is_static ())
940da03e 4541 sub_count = arm_vfp_cprc_sub_candidate (t->field (i).type (),
1040b979 4542 base_type);
90445bd3
DJ
4543 if (sub_count == -1)
4544 return -1;
4545 count += sub_count;
4546 }
df86565b 4547 if (t->length () == 0)
90445bd3
DJ
4548 {
4549 gdb_assert (count == 0);
4550 return 0;
4551 }
4552 else if (count == 0)
4553 return -1;
4554 unitlen = arm_vfp_cprc_unit_length (*base_type);
df86565b 4555 if (t->length () != unitlen * count)
90445bd3
DJ
4556 return -1;
4557 return count;
4558 }
4559
4560 case TYPE_CODE_UNION:
4561 {
4562 int count = 0;
4563 unsigned unitlen;
4564 int i;
1f704f76 4565 for (i = 0; i < t->num_fields (); i++)
90445bd3 4566 {
940da03e 4567 int sub_count = arm_vfp_cprc_sub_candidate (t->field (i).type (),
90445bd3
DJ
4568 base_type);
4569 if (sub_count == -1)
4570 return -1;
4571 count = (count > sub_count ? count : sub_count);
4572 }
df86565b 4573 if (t->length () == 0)
90445bd3
DJ
4574 {
4575 gdb_assert (count == 0);
4576 return 0;
4577 }
4578 else if (count == 0)
4579 return -1;
4580 unitlen = arm_vfp_cprc_unit_length (*base_type);
df86565b 4581 if (t->length () != unitlen * count)
90445bd3
DJ
4582 return -1;
4583 return count;
4584 }
4585
4586 default:
4587 break;
4588 }
4589
4590 return -1;
4591}
4592
4593/* Determine whether T is a VFP co-processor register candidate (CPRC)
4594 if passed to or returned from a non-variadic function with the VFP
4595 ABI in effect. Return 1 if it is, 0 otherwise. If it is, set
4596 *BASE_TYPE to the base type for T and *COUNT to the number of
4597 elements of that base type before returning. */
4598
4599static int
4600arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
4601 int *count)
4602{
4603 enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
4604 int c = arm_vfp_cprc_sub_candidate (t, &b);
4605 if (c <= 0 || c > 4)
4606 return 0;
4607 *base_type = b;
4608 *count = c;
4609 return 1;
4610}
4611
4612/* Return 1 if the VFP ABI should be used for passing arguments to and
4613 returning values from a function of type FUNC_TYPE, 0
4614 otherwise. */
4615
4616static int
4617arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
4618{
08106042 4619 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
345bd07c 4620
90445bd3
DJ
4621 /* Variadic functions always use the base ABI. Assume that functions
4622 without debug info are not variadic. */
a409645d 4623 if (func_type && check_typedef (func_type)->has_varargs ())
90445bd3 4624 return 0;
345bd07c 4625
90445bd3
DJ
4626 /* The VFP ABI is only supported as a variant of AAPCS. */
4627 if (tdep->arm_abi != ARM_ABI_AAPCS)
4628 return 0;
345bd07c
SM
4629
4630 return tdep->fp_model == ARM_FLOAT_VFP;
90445bd3
DJ
4631}
4632
4633/* We currently only support passing parameters in integer registers, which
4634 conforms with GCC's default model, and VFP argument passing following
4635 the VFP variant of AAPCS. Several other variants exist and
2dd604e7
RE
4636 we should probably support some of them based on the selected ABI. */
4637
4638static CORE_ADDR
7d9b040b 4639arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6a65450a 4640 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
cf84fa6b
AH
4641 struct value **args, CORE_ADDR sp,
4642 function_call_return_method return_method,
6a65450a 4643 CORE_ADDR struct_addr)
2dd604e7 4644{
e17a4113 4645 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2dd604e7
RE
4646 int argnum;
4647 int argreg;
4648 int nstack;
0fecb1a7 4649 struct arm_stack_item *si = NULL;
90445bd3
DJ
4650 int use_vfp_abi;
4651 struct type *ftype;
4652 unsigned vfp_regs_free = (1 << 16) - 1;
08106042 4653 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
90445bd3
DJ
4654
4655 /* Determine the type of this function and whether the VFP ABI
4656 applies. */
d0c97917 4657 ftype = check_typedef (function->type ());
78134374 4658 if (ftype->code () == TYPE_CODE_PTR)
27710edb 4659 ftype = check_typedef (ftype->target_type ());
90445bd3 4660 use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
2dd604e7 4661
6a65450a
AC
4662 /* Set the return address. For the ARM, the return breakpoint is
4663 always at BP_ADDR. */
9779414d 4664 if (arm_pc_is_thumb (gdbarch, bp_addr))
9dca5578 4665 bp_addr |= 1;
6a65450a 4666 regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
2dd604e7
RE
4667
4668 /* Walk through the list of args and determine how large a temporary
4669 stack is required. Need to take care here as structs may be
7a9dd1b2 4670 passed on the stack, and we have to push them. */
2dd604e7
RE
4671 nstack = 0;
4672
4673 argreg = ARM_A1_REGNUM;
4674 nstack = 0;
4675
2dd604e7
RE
4676 /* The struct_return pointer occupies the first parameter
4677 passing register. */
cf84fa6b 4678 if (return_method == return_method_struct)
2dd604e7 4679 {
7cb6d92a
SM
4680 arm_debug_printf ("struct return in %s = %s",
4681 gdbarch_register_name (gdbarch, argreg),
4682 paddress (gdbarch, struct_addr));
4683
2dd604e7
RE
4684 regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
4685 argreg++;
4686 }
4687
4688 for (argnum = 0; argnum < nargs; argnum++)
4689 {
4690 int len;
4691 struct type *arg_type;
4692 struct type *target_type;
4693 enum type_code typecode;
8c6363cf 4694 const bfd_byte *val;
2af48f68 4695 int align;
90445bd3
DJ
4696 enum arm_vfp_cprc_base_type vfp_base_type;
4697 int vfp_base_count;
4698 int may_use_core_reg = 1;
2dd604e7 4699
d0c97917 4700 arg_type = check_typedef (args[argnum]->type ());
df86565b 4701 len = arg_type->length ();
27710edb 4702 target_type = arg_type->target_type ();
78134374 4703 typecode = arg_type->code ();
efaf1ae0 4704 val = args[argnum]->contents ().data ();
2dd604e7 4705
030197b4 4706 align = type_align (arg_type);
2af48f68 4707 /* Round alignment up to a whole number of words. */
f0452268
AH
4708 align = (align + ARM_INT_REGISTER_SIZE - 1)
4709 & ~(ARM_INT_REGISTER_SIZE - 1);
2af48f68 4710 /* Different ABIs have different maximum alignments. */
345bd07c 4711 if (tdep->arm_abi == ARM_ABI_APCS)
2af48f68
PB
4712 {
4713 /* The APCS ABI only requires word alignment. */
f0452268 4714 align = ARM_INT_REGISTER_SIZE;
2af48f68
PB
4715 }
4716 else
4717 {
4718 /* The AAPCS requires at most doubleword alignment. */
f0452268
AH
4719 if (align > ARM_INT_REGISTER_SIZE * 2)
4720 align = ARM_INT_REGISTER_SIZE * 2;
2af48f68
PB
4721 }
4722
90445bd3
DJ
4723 if (use_vfp_abi
4724 && arm_vfp_call_candidate (arg_type, &vfp_base_type,
4725 &vfp_base_count))
4726 {
4727 int regno;
4728 int unit_length;
4729 int shift;
4730 unsigned mask;
4731
4732 /* Because this is a CPRC it cannot go in a core register or
4733 cause a core register to be skipped for alignment.
4734 Either it goes in VFP registers and the rest of this loop
4735 iteration is skipped for this argument, or it goes on the
4736 stack (and the stack alignment code is correct for this
4737 case). */
4738 may_use_core_reg = 0;
4739
4740 unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
4741 shift = unit_length / 4;
4742 mask = (1 << (shift * vfp_base_count)) - 1;
4743 for (regno = 0; regno < 16; regno += shift)
4744 if (((vfp_regs_free >> regno) & mask) == mask)
4745 break;
4746
4747 if (regno < 16)
4748 {
4749 int reg_char;
4750 int reg_scaled;
4751 int i;
4752
4753 vfp_regs_free &= ~(mask << regno);
4754 reg_scaled = regno / shift;
4755 reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
4756 for (i = 0; i < vfp_base_count; i++)
4757 {
4758 char name_buf[4];
4759 int regnum;
58d6951d
DJ
4760 if (reg_char == 'q')
4761 arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
90445bd3 4762 val + i * unit_length);
58d6951d
DJ
4763 else
4764 {
8c042590
PM
4765 xsnprintf (name_buf, sizeof (name_buf), "%c%d",
4766 reg_char, reg_scaled + i);
58d6951d
DJ
4767 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
4768 strlen (name_buf));
b66f5587 4769 regcache->cooked_write (regnum, val + i * unit_length);
58d6951d 4770 }
90445bd3
DJ
4771 }
4772 continue;
4773 }
4774 else
4775 {
4776 /* This CPRC could not go in VFP registers, so all VFP
4777 registers are now marked as used. */
4778 vfp_regs_free = 0;
4779 }
4780 }
4781
85102364 4782 /* Push stack padding for doubleword alignment. */
2af48f68
PB
4783 if (nstack & (align - 1))
4784 {
f0452268
AH
4785 si = push_stack_item (si, val, ARM_INT_REGISTER_SIZE);
4786 nstack += ARM_INT_REGISTER_SIZE;
2af48f68
PB
4787 }
4788
4789 /* Doubleword aligned quantities must go in even register pairs. */
90445bd3
DJ
4790 if (may_use_core_reg
4791 && argreg <= ARM_LAST_ARG_REGNUM
f0452268 4792 && align > ARM_INT_REGISTER_SIZE
2af48f68
PB
4793 && argreg & 1)
4794 argreg++;
4795
2dd604e7
RE
4796 /* If the argument is a pointer to a function, and it is a
4797 Thumb function, create a LOCAL copy of the value and set
4798 the THUMB bit in it. */
4799 if (TYPE_CODE_PTR == typecode
4800 && target_type != NULL
78134374 4801 && TYPE_CODE_FUNC == check_typedef (target_type)->code ())
2dd604e7 4802 {
e17a4113 4803 CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
9779414d 4804 if (arm_pc_is_thumb (gdbarch, regval))
2dd604e7 4805 {
224c3ddb 4806 bfd_byte *copy = (bfd_byte *) alloca (len);
8c6363cf 4807 store_unsigned_integer (copy, len, byte_order,
e17a4113 4808 MAKE_THUMB_ADDR (regval));
8c6363cf 4809 val = copy;
2dd604e7
RE
4810 }
4811 }
4812
4813 /* Copy the argument to general registers or the stack in
4814 register-sized pieces. Large arguments are split between
4815 registers and stack. */
4816 while (len > 0)
4817 {
f0452268
AH
4818 int partial_len = len < ARM_INT_REGISTER_SIZE
4819 ? len : ARM_INT_REGISTER_SIZE;
ef9bd0b8
YQ
4820 CORE_ADDR regval
4821 = extract_unsigned_integer (val, partial_len, byte_order);
2dd604e7 4822
90445bd3 4823 if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
2dd604e7
RE
4824 {
4825 /* The argument is being passed in a general purpose
4826 register. */
e17a4113 4827 if (byte_order == BFD_ENDIAN_BIG)
f0452268 4828 regval <<= (ARM_INT_REGISTER_SIZE - partial_len) * 8;
7cb6d92a
SM
4829
4830 arm_debug_printf ("arg %d in %s = 0x%s", argnum,
4831 gdbarch_register_name (gdbarch, argreg),
4832 phex (regval, ARM_INT_REGISTER_SIZE));
4833
2dd604e7
RE
4834 regcache_cooked_write_unsigned (regcache, argreg, regval);
4835 argreg++;
4836 }
4837 else
4838 {
f0452268 4839 gdb_byte buf[ARM_INT_REGISTER_SIZE];
ef9bd0b8
YQ
4840
4841 memset (buf, 0, sizeof (buf));
4842 store_unsigned_integer (buf, partial_len, byte_order, regval);
4843
2dd604e7 4844 /* Push the arguments onto the stack. */
7cb6d92a 4845 arm_debug_printf ("arg %d @ sp + %d", argnum, nstack);
f0452268
AH
4846 si = push_stack_item (si, buf, ARM_INT_REGISTER_SIZE);
4847 nstack += ARM_INT_REGISTER_SIZE;
2dd604e7
RE
4848 }
4849
4850 len -= partial_len;
4851 val += partial_len;
4852 }
4853 }
4854 /* If we have an odd number of words to push, then decrement the stack
4855 by one word now, so first stack argument will be dword aligned. */
4856 if (nstack & 4)
4857 sp -= 4;
4858
4859 while (si)
4860 {
4861 sp -= si->len;
4862 write_memory (sp, si->data, si->len);
4863 si = pop_stack_item (si);
4864 }
4865
4866 /* Finally, update teh SP register. */
4867 regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
4868
4869 return sp;
4870}
4871
f53f0d0b
PB
4872
4873/* Always align the frame to an 8-byte boundary. This is required on
4874 some platforms and harmless on the rest. */
4875
4876static CORE_ADDR
4877arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
4878{
4879 /* Align the stack to eight bytes. */
4880 return sp & ~ (CORE_ADDR) 7;
4881}
4882
c906108c 4883static void
12b27276 4884print_fpu_flags (struct ui_file *file, int flags)
c906108c 4885{
c5aa993b 4886 if (flags & (1 << 0))
0426ad51 4887 gdb_puts ("IVO ", file);
c5aa993b 4888 if (flags & (1 << 1))
0426ad51 4889 gdb_puts ("DVZ ", file);
c5aa993b 4890 if (flags & (1 << 2))
0426ad51 4891 gdb_puts ("OFL ", file);
c5aa993b 4892 if (flags & (1 << 3))
0426ad51 4893 gdb_puts ("UFL ", file);
c5aa993b 4894 if (flags & (1 << 4))
0426ad51 4895 gdb_puts ("INX ", file);
a11ac3b3 4896 gdb_putc ('\n', file);
c906108c
SS
4897}
4898
5e74b15c
RE
4899/* Print interesting information about the floating point processor
4900 (if present) or emulator. */
34e8f22d 4901static void
d855c300 4902arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
bd2b40ac 4903 frame_info_ptr frame, const char *args)
c906108c 4904{
9c9acae0 4905 unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
c5aa993b
JM
4906 int type;
4907
4908 type = (status >> 24) & 127;
edefbb7c 4909 if (status & (1 << 31))
6cb06a8c 4910 gdb_printf (file, _("Hardware FPU type %d\n"), type);
edefbb7c 4911 else
6cb06a8c 4912 gdb_printf (file, _("Software FPU type %d\n"), type);
edefbb7c 4913 /* i18n: [floating point unit] mask */
0426ad51 4914 gdb_puts (_("mask: "), file);
12b27276 4915 print_fpu_flags (file, status >> 16);
edefbb7c 4916 /* i18n: [floating point unit] flags */
0426ad51 4917 gdb_puts (_("flags: "), file);
12b27276 4918 print_fpu_flags (file, status);
c906108c
SS
4919}
4920
27067745
UW
4921/* Construct the ARM extended floating point type. */
4922static struct type *
4923arm_ext_type (struct gdbarch *gdbarch)
4924{
08106042 4925 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
27067745
UW
4926
4927 if (!tdep->arm_ext_type)
77c5f496
TT
4928 {
4929 type_allocator alloc (gdbarch);
4930 tdep->arm_ext_type
4931 = init_float_type (alloc, -1, "builtin_type_arm_ext",
4932 floatformats_arm_ext);
4933 }
27067745
UW
4934
4935 return tdep->arm_ext_type;
4936}
4937
58d6951d
DJ
4938static struct type *
4939arm_neon_double_type (struct gdbarch *gdbarch)
4940{
08106042 4941 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d
DJ
4942
4943 if (tdep->neon_double_type == NULL)
4944 {
4945 struct type *t, *elem;
4946
4947 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
4948 TYPE_CODE_UNION);
4949 elem = builtin_type (gdbarch)->builtin_uint8;
4950 append_composite_type_field (t, "u8", init_vector_type (elem, 8));
4951 elem = builtin_type (gdbarch)->builtin_uint16;
4952 append_composite_type_field (t, "u16", init_vector_type (elem, 4));
4953 elem = builtin_type (gdbarch)->builtin_uint32;
4954 append_composite_type_field (t, "u32", init_vector_type (elem, 2));
4955 elem = builtin_type (gdbarch)->builtin_uint64;
4956 append_composite_type_field (t, "u64", elem);
4957 elem = builtin_type (gdbarch)->builtin_float;
4958 append_composite_type_field (t, "f32", init_vector_type (elem, 2));
4959 elem = builtin_type (gdbarch)->builtin_double;
4960 append_composite_type_field (t, "f64", elem);
4961
2062087b 4962 t->set_is_vector (true);
d0e39ea2 4963 t->set_name ("neon_d");
58d6951d
DJ
4964 tdep->neon_double_type = t;
4965 }
4966
4967 return tdep->neon_double_type;
4968}
4969
4970/* FIXME: The vector types are not correctly ordered on big-endian
4971 targets. Just as s0 is the low bits of d0, d0[0] is also the low
4972 bits of d0 - regardless of what unit size is being held in d0. So
4973 the offset of the first uint8 in d0 is 7, but the offset of the
4974 first float is 4. This code works as-is for little-endian
4975 targets. */
4976
4977static struct type *
4978arm_neon_quad_type (struct gdbarch *gdbarch)
4979{
08106042 4980 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d
DJ
4981
4982 if (tdep->neon_quad_type == NULL)
4983 {
4984 struct type *t, *elem;
4985
4986 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
4987 TYPE_CODE_UNION);
4988 elem = builtin_type (gdbarch)->builtin_uint8;
4989 append_composite_type_field (t, "u8", init_vector_type (elem, 16));
4990 elem = builtin_type (gdbarch)->builtin_uint16;
4991 append_composite_type_field (t, "u16", init_vector_type (elem, 8));
4992 elem = builtin_type (gdbarch)->builtin_uint32;
4993 append_composite_type_field (t, "u32", init_vector_type (elem, 4));
4994 elem = builtin_type (gdbarch)->builtin_uint64;
4995 append_composite_type_field (t, "u64", init_vector_type (elem, 2));
4996 elem = builtin_type (gdbarch)->builtin_float;
4997 append_composite_type_field (t, "f32", init_vector_type (elem, 4));
4998 elem = builtin_type (gdbarch)->builtin_double;
4999 append_composite_type_field (t, "f64", init_vector_type (elem, 2));
5000
2062087b 5001 t->set_is_vector (true);
d0e39ea2 5002 t->set_name ("neon_q");
58d6951d
DJ
5003 tdep->neon_quad_type = t;
5004 }
5005
5006 return tdep->neon_quad_type;
5007}
5008
ecbf5d4f
LM
5009/* Return true if REGNUM is a Q pseudo register. Return false
5010 otherwise.
5011
5012 REGNUM is the raw register number and not a pseudo-relative register
5013 number. */
5014
5015static bool
5016is_q_pseudo (struct gdbarch *gdbarch, int regnum)
5017{
08106042 5018 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ecbf5d4f 5019
ae66a8f1
SP
5020 /* Q pseudo registers are available for both NEON (Q0~Q15) and
5021 MVE (Q0~Q7) features. */
ecbf5d4f
LM
5022 if (tdep->have_q_pseudos
5023 && regnum >= tdep->q_pseudo_base
5024 && regnum < (tdep->q_pseudo_base + tdep->q_pseudo_count))
5025 return true;
5026
5027 return false;
5028}
5029
5030/* Return true if REGNUM is a VFP S pseudo register. Return false
5031 otherwise.
5032
5033 REGNUM is the raw register number and not a pseudo-relative register
5034 number. */
5035
5036static bool
5037is_s_pseudo (struct gdbarch *gdbarch, int regnum)
5038{
08106042 5039 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ecbf5d4f
LM
5040
5041 if (tdep->have_s_pseudos
5042 && regnum >= tdep->s_pseudo_base
5043 && regnum < (tdep->s_pseudo_base + tdep->s_pseudo_count))
5044 return true;
5045
5046 return false;
5047}
5048
ae66a8f1
SP
5049/* Return true if REGNUM is a MVE pseudo register (P0). Return false
5050 otherwise.
5051
5052 REGNUM is the raw register number and not a pseudo-relative register
5053 number. */
5054
5055static bool
5056is_mve_pseudo (struct gdbarch *gdbarch, int regnum)
5057{
08106042 5058 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ae66a8f1
SP
5059
5060 if (tdep->have_mve
5061 && regnum >= tdep->mve_pseudo_base
5062 && regnum < tdep->mve_pseudo_base + tdep->mve_pseudo_count)
5063 return true;
5064
5065 return false;
5066}
5067
a01567f4
LM
5068/* Return true if REGNUM is a PACBTI pseudo register (ra_auth_code). Return
5069 false otherwise.
5070
5071 REGNUM is the raw register number and not a pseudo-relative register
5072 number. */
5073
5074static bool
5075is_pacbti_pseudo (struct gdbarch *gdbarch, int regnum)
5076{
08106042 5077 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
a01567f4
LM
5078
5079 if (tdep->have_pacbti
5080 && regnum >= tdep->pacbti_pseudo_base
5081 && regnum < tdep->pacbti_pseudo_base + tdep->pacbti_pseudo_count)
5082 return true;
5083
5084 return false;
5085}
5086
34e8f22d
RE
5087/* Return the GDB type object for the "standard" data type of data in
5088 register N. */
5089
5090static struct type *
7a5ea0d4 5091arm_register_type (struct gdbarch *gdbarch, int regnum)
032758dc 5092{
08106042 5093 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d 5094
ecbf5d4f 5095 if (is_s_pseudo (gdbarch, regnum))
58d6951d
DJ
5096 return builtin_type (gdbarch)->builtin_float;
5097
ecbf5d4f 5098 if (is_q_pseudo (gdbarch, regnum))
58d6951d
DJ
5099 return arm_neon_quad_type (gdbarch);
5100
ae66a8f1
SP
5101 if (is_mve_pseudo (gdbarch, regnum))
5102 return builtin_type (gdbarch)->builtin_int16;
5103
a01567f4
LM
5104 if (is_pacbti_pseudo (gdbarch, regnum))
5105 return builtin_type (gdbarch)->builtin_uint32;
5106
58d6951d
DJ
5107 /* If the target description has register information, we are only
5108 in this function so that we can override the types of
5109 double-precision registers for NEON. */
5110 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
5111 {
5112 struct type *t = tdesc_register_type (gdbarch, regnum);
5113
5114 if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
78134374 5115 && t->code () == TYPE_CODE_FLT
ecbf5d4f 5116 && tdep->have_neon)
58d6951d
DJ
5117 return arm_neon_double_type (gdbarch);
5118 else
5119 return t;
5120 }
5121
34e8f22d 5122 if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
58d6951d 5123 {
ecbf5d4f 5124 if (!tdep->have_fpa_registers)
58d6951d
DJ
5125 return builtin_type (gdbarch)->builtin_void;
5126
5127 return arm_ext_type (gdbarch);
5128 }
e4c16157 5129 else if (regnum == ARM_SP_REGNUM)
0dfff4cb 5130 return builtin_type (gdbarch)->builtin_data_ptr;
e4c16157 5131 else if (regnum == ARM_PC_REGNUM)
0dfff4cb 5132 return builtin_type (gdbarch)->builtin_func_ptr;
ff6f572f
DJ
5133 else if (regnum >= ARRAY_SIZE (arm_register_names))
5134 /* These registers are only supported on targets which supply
5135 an XML description. */
df4df182 5136 return builtin_type (gdbarch)->builtin_int0;
032758dc 5137 else
df4df182 5138 return builtin_type (gdbarch)->builtin_uint32;
032758dc
AC
5139}
5140
ff6f572f
DJ
5141/* Map a DWARF register REGNUM onto the appropriate GDB register
5142 number. */
5143
5144static int
d3f73121 5145arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
ff6f572f
DJ
5146{
5147 /* Core integer regs. */
5148 if (reg >= 0 && reg <= 15)
5149 return reg;
5150
5151 /* Legacy FPA encoding. These were once used in a way which
5152 overlapped with VFP register numbering, so their use is
5153 discouraged, but GDB doesn't support the ARM toolchain
5154 which used them for VFP. */
5155 if (reg >= 16 && reg <= 23)
5156 return ARM_F0_REGNUM + reg - 16;
5157
5158 /* New assignments for the FPA registers. */
5159 if (reg >= 96 && reg <= 103)
5160 return ARM_F0_REGNUM + reg - 96;
5161
5162 /* WMMX register assignments. */
5163 if (reg >= 104 && reg <= 111)
5164 return ARM_WCGR0_REGNUM + reg - 104;
5165
5166 if (reg >= 112 && reg <= 127)
5167 return ARM_WR0_REGNUM + reg - 112;
5168
a01567f4
LM
5169 /* PACBTI register containing the Pointer Authentication Code. */
5170 if (reg == ARM_DWARF_RA_AUTH_CODE)
5171 {
08106042 5172 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
a01567f4
LM
5173
5174 if (tdep->have_pacbti)
5175 return tdep->pacbti_pseudo_base;
5176
5177 return -1;
5178 }
5179
ff6f572f
DJ
5180 if (reg >= 192 && reg <= 199)
5181 return ARM_WC0_REGNUM + reg - 192;
5182
58d6951d
DJ
5183 /* VFP v2 registers. A double precision value is actually
5184 in d1 rather than s2, but the ABI only defines numbering
5185 for the single precision registers. This will "just work"
5186 in GDB for little endian targets (we'll read eight bytes,
5187 starting in s0 and then progressing to s1), but will be
5188 reversed on big endian targets with VFP. This won't
5189 be a problem for the new Neon quad registers; you're supposed
5190 to use DW_OP_piece for those. */
5191 if (reg >= 64 && reg <= 95)
5192 {
5193 char name_buf[4];
5194
8c042590 5195 xsnprintf (name_buf, sizeof (name_buf), "s%d", reg - 64);
58d6951d
DJ
5196 return user_reg_map_name_to_regnum (gdbarch, name_buf,
5197 strlen (name_buf));
5198 }
5199
5200 /* VFP v3 / Neon registers. This range is also used for VFP v2
5201 registers, except that it now describes d0 instead of s0. */
5202 if (reg >= 256 && reg <= 287)
5203 {
5204 char name_buf[4];
5205
8c042590 5206 xsnprintf (name_buf, sizeof (name_buf), "d%d", reg - 256);
58d6951d
DJ
5207 return user_reg_map_name_to_regnum (gdbarch, name_buf,
5208 strlen (name_buf));
5209 }
5210
ff6f572f
DJ
5211 return -1;
5212}
5213
26216b98
AC
5214/* Map GDB internal REGNUM onto the Arm simulator register numbers. */
5215static int
e7faf938 5216arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
26216b98
AC
5217{
5218 int reg = regnum;
e7faf938 5219 gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
26216b98 5220
ff6f572f
DJ
5221 if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
5222 return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
5223
5224 if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
5225 return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
5226
5227 if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
5228 return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
5229
26216b98
AC
5230 if (reg < NUM_GREGS)
5231 return SIM_ARM_R0_REGNUM + reg;
5232 reg -= NUM_GREGS;
5233
5234 if (reg < NUM_FREGS)
5235 return SIM_ARM_FP0_REGNUM + reg;
5236 reg -= NUM_FREGS;
5237
5238 if (reg < NUM_SREGS)
5239 return SIM_ARM_FPS_REGNUM + reg;
5240 reg -= NUM_SREGS;
5241
f34652de 5242 internal_error (_("Bad REGNUM %d"), regnum);
26216b98 5243}
34e8f22d 5244
a01567f4
LM
5245static const unsigned char op_lit0 = DW_OP_lit0;
5246
5247static void
5248arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
5249 struct dwarf2_frame_state_reg *reg,
bd2b40ac 5250 frame_info_ptr this_frame)
a01567f4 5251{
a6e4a48c
YR
5252 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
5253
a01567f4
LM
5254 if (is_pacbti_pseudo (gdbarch, regnum))
5255 {
5256 /* Initialize RA_AUTH_CODE to zero. */
5257 reg->how = DWARF2_FRAME_REG_SAVED_VAL_EXP;
5258 reg->loc.exp.start = &op_lit0;
5259 reg->loc.exp.len = 1;
5260 return;
5261 }
5262
42e11f36 5263 if (regnum == ARM_PC_REGNUM || regnum == ARM_PS_REGNUM)
a01567f4 5264 {
a01567f4
LM
5265 reg->how = DWARF2_FRAME_REG_FN;
5266 reg->loc.fn = arm_dwarf2_prev_register;
a01567f4 5267 }
42e11f36
TS
5268 else if (regnum == ARM_SP_REGNUM)
5269 reg->how = DWARF2_FRAME_REG_CFA;
a6e4a48c
YR
5270 else if (arm_is_alternative_sp_register (tdep, regnum))
5271 {
5272 /* Handle the alternative SP registers on Cortex-M. */
5273 reg->how = DWARF2_FRAME_REG_FN;
5274 reg->loc.fn = arm_dwarf2_prev_register;
5275 }
a01567f4
LM
5276}
5277
d9311bfa
AT
5278/* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
5279 the buffer to be NEW_LEN bytes ending at ENDADDR. Return
5280 NULL if an error occurs. BUF is freed. */
c906108c 5281
d9311bfa
AT
5282static gdb_byte *
5283extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
5284 int old_len, int new_len)
5285{
5286 gdb_byte *new_buf;
5287 int bytes_to_read = new_len - old_len;
c906108c 5288
d9311bfa
AT
5289 new_buf = (gdb_byte *) xmalloc (new_len);
5290 memcpy (new_buf + bytes_to_read, buf, old_len);
5291 xfree (buf);
198cd59d 5292 if (target_read_code (endaddr - new_len, new_buf, bytes_to_read) != 0)
d9311bfa
AT
5293 {
5294 xfree (new_buf);
5295 return NULL;
c906108c 5296 }
d9311bfa 5297 return new_buf;
c906108c
SS
5298}
5299
d9311bfa
AT
5300/* An IT block is at most the 2-byte IT instruction followed by
5301 four 4-byte instructions. The furthest back we must search to
5302 find an IT block that affects the current instruction is thus
5303 2 + 3 * 4 == 14 bytes. */
5304#define MAX_IT_BLOCK_PREFIX 14
177321bd 5305
d9311bfa
AT
5306/* Use a quick scan if there are more than this many bytes of
5307 code. */
5308#define IT_SCAN_THRESHOLD 32
177321bd 5309
d9311bfa
AT
5310/* Adjust a breakpoint's address to move breakpoints out of IT blocks.
5311 A breakpoint in an IT block may not be hit, depending on the
5312 condition flags. */
ad527d2e 5313static CORE_ADDR
d9311bfa 5314arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
c906108c 5315{
d9311bfa
AT
5316 gdb_byte *buf;
5317 char map_type;
5318 CORE_ADDR boundary, func_start;
5319 int buf_len;
5320 enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
5321 int i, any, last_it, last_it_count;
08106042 5322 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
177321bd 5323
d9311bfa 5324 /* If we are using BKPT breakpoints, none of this is necessary. */
345bd07c 5325 if (tdep->thumb2_breakpoint == NULL)
d9311bfa 5326 return bpaddr;
177321bd 5327
d9311bfa
AT
5328 /* ARM mode does not have this problem. */
5329 if (!arm_pc_is_thumb (gdbarch, bpaddr))
5330 return bpaddr;
177321bd 5331
d9311bfa
AT
5332 /* We are setting a breakpoint in Thumb code that could potentially
5333 contain an IT block. The first step is to find how much Thumb
5334 code there is; we do not need to read outside of known Thumb
5335 sequences. */
5336 map_type = arm_find_mapping_symbol (bpaddr, &boundary);
5337 if (map_type == 0)
5338 /* Thumb-2 code must have mapping symbols to have a chance. */
5339 return bpaddr;
9dca5578 5340
d9311bfa 5341 bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
177321bd 5342
d9311bfa
AT
5343 if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
5344 && func_start > boundary)
5345 boundary = func_start;
9dca5578 5346
d9311bfa
AT
5347 /* Search for a candidate IT instruction. We have to do some fancy
5348 footwork to distinguish a real IT instruction from the second
5349 half of a 32-bit instruction, but there is no need for that if
5350 there's no candidate. */
325fac50 5351 buf_len = std::min (bpaddr - boundary, (CORE_ADDR) MAX_IT_BLOCK_PREFIX);
d9311bfa
AT
5352 if (buf_len == 0)
5353 /* No room for an IT instruction. */
5354 return bpaddr;
c906108c 5355
d9311bfa 5356 buf = (gdb_byte *) xmalloc (buf_len);
198cd59d 5357 if (target_read_code (bpaddr - buf_len, buf, buf_len) != 0)
d9311bfa
AT
5358 return bpaddr;
5359 any = 0;
5360 for (i = 0; i < buf_len; i += 2)
c906108c 5361 {
d9311bfa
AT
5362 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5363 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
25b41d01 5364 {
d9311bfa
AT
5365 any = 1;
5366 break;
25b41d01 5367 }
c906108c 5368 }
d9311bfa
AT
5369
5370 if (any == 0)
c906108c 5371 {
d9311bfa
AT
5372 xfree (buf);
5373 return bpaddr;
f9d67f43
DJ
5374 }
5375
5376 /* OK, the code bytes before this instruction contain at least one
5377 halfword which resembles an IT instruction. We know that it's
5378 Thumb code, but there are still two possibilities. Either the
5379 halfword really is an IT instruction, or it is the second half of
5380 a 32-bit Thumb instruction. The only way we can tell is to
5381 scan forwards from a known instruction boundary. */
5382 if (bpaddr - boundary > IT_SCAN_THRESHOLD)
5383 {
5384 int definite;
5385
5386 /* There's a lot of code before this instruction. Start with an
5387 optimistic search; it's easy to recognize halfwords that can
5388 not be the start of a 32-bit instruction, and use that to
5389 lock on to the instruction boundaries. */
5390 buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
5391 if (buf == NULL)
5392 return bpaddr;
5393 buf_len = IT_SCAN_THRESHOLD;
5394
5395 definite = 0;
5396 for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
5397 {
5398 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5399 if (thumb_insn_size (inst1) == 2)
5400 {
5401 definite = 1;
5402 break;
5403 }
5404 }
5405
5406 /* At this point, if DEFINITE, BUF[I] is the first place we
5407 are sure that we know the instruction boundaries, and it is far
5408 enough from BPADDR that we could not miss an IT instruction
5409 affecting BPADDR. If ! DEFINITE, give up - start from a
5410 known boundary. */
5411 if (! definite)
5412 {
0963b4bd
MS
5413 buf = extend_buffer_earlier (buf, bpaddr, buf_len,
5414 bpaddr - boundary);
f9d67f43
DJ
5415 if (buf == NULL)
5416 return bpaddr;
5417 buf_len = bpaddr - boundary;
5418 i = 0;
5419 }
5420 }
5421 else
5422 {
5423 buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
5424 if (buf == NULL)
5425 return bpaddr;
5426 buf_len = bpaddr - boundary;
5427 i = 0;
5428 }
5429
5430 /* Scan forwards. Find the last IT instruction before BPADDR. */
5431 last_it = -1;
5432 last_it_count = 0;
5433 while (i < buf_len)
5434 {
5435 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5436 last_it_count--;
5437 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5438 {
5439 last_it = i;
5440 if (inst1 & 0x0001)
5441 last_it_count = 4;
5442 else if (inst1 & 0x0002)
5443 last_it_count = 3;
5444 else if (inst1 & 0x0004)
5445 last_it_count = 2;
5446 else
5447 last_it_count = 1;
5448 }
5449 i += thumb_insn_size (inst1);
5450 }
5451
5452 xfree (buf);
5453
5454 if (last_it == -1)
5455 /* There wasn't really an IT instruction after all. */
5456 return bpaddr;
5457
5458 if (last_it_count < 1)
5459 /* It was too far away. */
5460 return bpaddr;
5461
5462 /* This really is a trouble spot. Move the breakpoint to the IT
5463 instruction. */
5464 return bpaddr - buf_len + last_it;
5465}
5466
cca44b1b 5467/* ARM displaced stepping support.
c906108c 5468
cca44b1b 5469 Generally ARM displaced stepping works as follows:
c906108c 5470
cca44b1b 5471 1. When an instruction is to be single-stepped, it is first decoded by
2ba163c8
SM
5472 arm_process_displaced_insn. Depending on the type of instruction, it is
5473 then copied to a scratch location, possibly in a modified form. The
5474 copy_* set of functions performs such modification, as necessary. A
5475 breakpoint is placed after the modified instruction in the scratch space
5476 to return control to GDB. Note in particular that instructions which
5477 modify the PC will no longer do so after modification.
c5aa993b 5478
cca44b1b
JB
5479 2. The instruction is single-stepped, by setting the PC to the scratch
5480 location address, and resuming. Control returns to GDB when the
5481 breakpoint is hit.
c5aa993b 5482
cca44b1b
JB
5483 3. A cleanup function (cleanup_*) is called corresponding to the copy_*
5484 function used for the current instruction. This function's job is to
5485 put the CPU/memory state back to what it would have been if the
5486 instruction had been executed unmodified in its original location. */
c5aa993b 5487
cca44b1b
JB
5488/* NOP instruction (mov r0, r0). */
5489#define ARM_NOP 0xe1a00000
34518530 5490#define THUMB_NOP 0x4600
cca44b1b
JB
5491
5492/* Helper for register reads for displaced stepping. In particular, this
5493 returns the PC as it would be seen by the instruction at its original
5494 location. */
5495
5496ULONGEST
1152d984 5497displaced_read_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
36073a92 5498 int regno)
cca44b1b
JB
5499{
5500 ULONGEST ret;
36073a92 5501 CORE_ADDR from = dsc->insn_addr;
cca44b1b 5502
bf9f652a 5503 if (regno == ARM_PC_REGNUM)
cca44b1b 5504 {
4db71c0b
YQ
5505 /* Compute pipeline offset:
5506 - When executing an ARM instruction, PC reads as the address of the
5507 current instruction plus 8.
5508 - When executing a Thumb instruction, PC reads as the address of the
5509 current instruction plus 4. */
5510
36073a92 5511 if (!dsc->is_thumb)
4db71c0b
YQ
5512 from += 8;
5513 else
5514 from += 4;
5515
136821d9
SM
5516 displaced_debug_printf ("read pc value %.8lx",
5517 (unsigned long) from);
4db71c0b 5518 return (ULONGEST) from;
cca44b1b 5519 }
c906108c 5520 else
cca44b1b
JB
5521 {
5522 regcache_cooked_read_unsigned (regs, regno, &ret);
136821d9
SM
5523
5524 displaced_debug_printf ("read r%d value %.8lx",
5525 regno, (unsigned long) ret);
5526
cca44b1b
JB
5527 return ret;
5528 }
c906108c
SS
5529}
5530
cca44b1b
JB
5531static int
5532displaced_in_arm_mode (struct regcache *regs)
5533{
5534 ULONGEST ps;
ac7936df 5535 ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
66e810cd 5536
cca44b1b 5537 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
66e810cd 5538
9779414d 5539 return (ps & t_bit) == 0;
cca44b1b 5540}
66e810cd 5541
cca44b1b 5542/* Write to the PC as from a branch instruction. */
c906108c 5543
cca44b1b 5544static void
1152d984 5545branch_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
36073a92 5546 ULONGEST val)
c906108c 5547{
36073a92 5548 if (!dsc->is_thumb)
cca44b1b
JB
5549 /* Note: If bits 0/1 are set, this branch would be unpredictable for
5550 architecture versions < 6. */
0963b4bd
MS
5551 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5552 val & ~(ULONGEST) 0x3);
cca44b1b 5553 else
0963b4bd
MS
5554 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5555 val & ~(ULONGEST) 0x1);
cca44b1b 5556}
66e810cd 5557
cca44b1b
JB
5558/* Write to the PC as from a branch-exchange instruction. */
5559
5560static void
5561bx_write_pc (struct regcache *regs, ULONGEST val)
5562{
5563 ULONGEST ps;
ac7936df 5564 ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
cca44b1b
JB
5565
5566 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
5567
5568 if ((val & 1) == 1)
c906108c 5569 {
9779414d 5570 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
cca44b1b
JB
5571 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
5572 }
5573 else if ((val & 2) == 0)
5574 {
9779414d 5575 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 5576 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
c906108c
SS
5577 }
5578 else
5579 {
cca44b1b
JB
5580 /* Unpredictable behaviour. Try to do something sensible (switch to ARM
5581 mode, align dest to 4 bytes). */
5582 warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
9779414d 5583 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 5584 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
c906108c
SS
5585 }
5586}
ed9a39eb 5587
cca44b1b 5588/* Write to the PC as if from a load instruction. */
ed9a39eb 5589
34e8f22d 5590static void
1152d984 5591load_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
36073a92 5592 ULONGEST val)
ed9a39eb 5593{
cca44b1b
JB
5594 if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
5595 bx_write_pc (regs, val);
5596 else
36073a92 5597 branch_write_pc (regs, dsc, val);
cca44b1b 5598}
be8626e0 5599
cca44b1b
JB
5600/* Write to the PC as if from an ALU instruction. */
5601
5602static void
1152d984 5603alu_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
36073a92 5604 ULONGEST val)
cca44b1b 5605{
36073a92 5606 if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
cca44b1b
JB
5607 bx_write_pc (regs, val);
5608 else
36073a92 5609 branch_write_pc (regs, dsc, val);
cca44b1b
JB
5610}
5611
5612/* Helper for writing to registers for displaced stepping. Writing to the PC
5613 has a varying effects depending on the instruction which does the write:
5614 this is controlled by the WRITE_PC argument. */
5615
5616void
1152d984 5617displaced_write_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
cca44b1b
JB
5618 int regno, ULONGEST val, enum pc_write_style write_pc)
5619{
bf9f652a 5620 if (regno == ARM_PC_REGNUM)
08216dd7 5621 {
136821d9
SM
5622 displaced_debug_printf ("writing pc %.8lx", (unsigned long) val);
5623
cca44b1b 5624 switch (write_pc)
08216dd7 5625 {
cca44b1b 5626 case BRANCH_WRITE_PC:
36073a92 5627 branch_write_pc (regs, dsc, val);
08216dd7
RE
5628 break;
5629
cca44b1b
JB
5630 case BX_WRITE_PC:
5631 bx_write_pc (regs, val);
24b21115 5632 break;
cca44b1b
JB
5633
5634 case LOAD_WRITE_PC:
36073a92 5635 load_write_pc (regs, dsc, val);
24b21115 5636 break;
cca44b1b
JB
5637
5638 case ALU_WRITE_PC:
36073a92 5639 alu_write_pc (regs, dsc, val);
24b21115 5640 break;
cca44b1b
JB
5641
5642 case CANNOT_WRITE_PC:
5643 warning (_("Instruction wrote to PC in an unexpected way when "
5644 "single-stepping"));
08216dd7
RE
5645 break;
5646
5647 default:
f34652de 5648 internal_error (_("Invalid argument to displaced_write_reg"));
08216dd7 5649 }
b508a996 5650
cca44b1b 5651 dsc->wrote_to_pc = 1;
b508a996 5652 }
ed9a39eb 5653 else
b508a996 5654 {
136821d9
SM
5655 displaced_debug_printf ("writing r%d value %.8lx",
5656 regno, (unsigned long) val);
cca44b1b 5657 regcache_cooked_write_unsigned (regs, regno, val);
b508a996 5658 }
34e8f22d
RE
5659}
5660
cca44b1b
JB
5661/* This function is used to concisely determine if an instruction INSN
5662 references PC. Register fields of interest in INSN should have the
0963b4bd
MS
5663 corresponding fields of BITMASK set to 0b1111. The function
5664 returns return 1 if any of these fields in INSN reference the PC
5665 (also 0b1111, r15), else it returns 0. */
67255d04
RE
5666
5667static int
cca44b1b 5668insn_references_pc (uint32_t insn, uint32_t bitmask)
67255d04 5669{
cca44b1b 5670 uint32_t lowbit = 1;
67255d04 5671
cca44b1b
JB
5672 while (bitmask != 0)
5673 {
5674 uint32_t mask;
44e1a9eb 5675
cca44b1b
JB
5676 for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
5677 ;
67255d04 5678
cca44b1b
JB
5679 if (!lowbit)
5680 break;
67255d04 5681
cca44b1b 5682 mask = lowbit * 0xf;
67255d04 5683
cca44b1b
JB
5684 if ((insn & mask) == mask)
5685 return 1;
5686
5687 bitmask &= ~mask;
67255d04
RE
5688 }
5689
cca44b1b
JB
5690 return 0;
5691}
2af48f68 5692
cca44b1b
JB
5693/* The simplest copy function. Many instructions have the same effect no
5694 matter what address they are executed at: in those cases, use this. */
67255d04 5695
cca44b1b 5696static int
1152d984
SM
5697arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn, const char *iname,
5698 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 5699{
136821d9
SM
5700 displaced_debug_printf ("copying insn %.8lx, opcode/class '%s' unmodified",
5701 (unsigned long) insn, iname);
67255d04 5702
cca44b1b 5703 dsc->modinsn[0] = insn;
67255d04 5704
cca44b1b
JB
5705 return 0;
5706}
5707
34518530
YQ
5708static int
5709thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
5710 uint16_t insn2, const char *iname,
1152d984 5711 arm_displaced_step_copy_insn_closure *dsc)
34518530 5712{
136821d9
SM
5713 displaced_debug_printf ("copying insn %.4x %.4x, opcode/class '%s' "
5714 "unmodified", insn1, insn2, iname);
34518530
YQ
5715
5716 dsc->modinsn[0] = insn1;
5717 dsc->modinsn[1] = insn2;
5718 dsc->numinsns = 2;
5719
5720 return 0;
5721}
5722
5723/* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
5724 modification. */
5725static int
615234c1 5726thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, uint16_t insn,
34518530 5727 const char *iname,
1152d984 5728 arm_displaced_step_copy_insn_closure *dsc)
34518530 5729{
136821d9
SM
5730 displaced_debug_printf ("copying insn %.4x, opcode/class '%s' unmodified",
5731 insn, iname);
34518530
YQ
5732
5733 dsc->modinsn[0] = insn;
5734
5735 return 0;
5736}
5737
cca44b1b
JB
5738/* Preload instructions with immediate offset. */
5739
5740static void
1152d984
SM
5741cleanup_preload (struct gdbarch *gdbarch, regcache *regs,
5742 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
5743{
5744 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5745 if (!dsc->u.preload.immed)
5746 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5747}
5748
7ff120b4
YQ
5749static void
5750install_preload (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5751 arm_displaced_step_copy_insn_closure *dsc, unsigned int rn)
cca44b1b 5752{
cca44b1b 5753 ULONGEST rn_val;
cca44b1b
JB
5754 /* Preload instructions:
5755
5756 {pli/pld} [rn, #+/-imm]
5757 ->
5758 {pli/pld} [r0, #+/-imm]. */
5759
36073a92
YQ
5760 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5761 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 5762 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
cca44b1b
JB
5763 dsc->u.preload.immed = 1;
5764
cca44b1b 5765 dsc->cleanup = &cleanup_preload;
cca44b1b
JB
5766}
5767
cca44b1b 5768static int
7ff120b4 5769arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
1152d984 5770 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
5771{
5772 unsigned int rn = bits (insn, 16, 19);
cca44b1b 5773
7ff120b4
YQ
5774 if (!insn_references_pc (insn, 0x000f0000ul))
5775 return arm_copy_unmodified (gdbarch, insn, "preload", dsc);
cca44b1b 5776
136821d9 5777 displaced_debug_printf ("copying preload insn %.8lx", (unsigned long) insn);
cca44b1b 5778
7ff120b4
YQ
5779 dsc->modinsn[0] = insn & 0xfff0ffff;
5780
5781 install_preload (gdbarch, regs, dsc, rn);
5782
5783 return 0;
5784}
5785
34518530
YQ
5786static int
5787thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
1152d984 5788 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
5789{
5790 unsigned int rn = bits (insn1, 0, 3);
5791 unsigned int u_bit = bit (insn1, 7);
5792 int imm12 = bits (insn2, 0, 11);
5793 ULONGEST pc_val;
5794
5795 if (rn != ARM_PC_REGNUM)
5796 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);
5797
5798 /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
5799 PLD (literal) Encoding T1. */
136821d9
SM
5800 displaced_debug_printf ("copying pld/pli pc (0x%x) %c imm12 %.4x",
5801 (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
5802 imm12);
34518530
YQ
5803
5804 if (!u_bit)
5805 imm12 = -1 * imm12;
5806
5807 /* Rewrite instruction {pli/pld} PC imm12 into:
5808 Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12
5809
5810 {pli/pld} [r0, r1]
5811
5812 Cleanup: r0 <- tmp[0], r1 <- tmp[1]. */
5813
5814 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5815 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5816
5817 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
5818
5819 displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
5820 displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
5821 dsc->u.preload.immed = 0;
5822
5823 /* {pli/pld} [r0, r1] */
5824 dsc->modinsn[0] = insn1 & 0xfff0;
5825 dsc->modinsn[1] = 0xf001;
5826 dsc->numinsns = 2;
5827
5828 dsc->cleanup = &cleanup_preload;
5829 return 0;
5830}
5831
7ff120b4
YQ
5832/* Preload instructions with register offset. */
5833
5834static void
5835install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5836 arm_displaced_step_copy_insn_closure *dsc, unsigned int rn,
7ff120b4
YQ
5837 unsigned int rm)
5838{
5839 ULONGEST rn_val, rm_val;
5840
cca44b1b
JB
5841 /* Preload register-offset instructions:
5842
5843 {pli/pld} [rn, rm {, shift}]
5844 ->
5845 {pli/pld} [r0, r1 {, shift}]. */
5846
36073a92
YQ
5847 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5848 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5849 rn_val = displaced_read_reg (regs, dsc, rn);
5850 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5851 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5852 displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
cca44b1b
JB
5853 dsc->u.preload.immed = 0;
5854
cca44b1b 5855 dsc->cleanup = &cleanup_preload;
7ff120b4
YQ
5856}
5857
5858static int
5859arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
5860 struct regcache *regs,
1152d984 5861 arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
5862{
5863 unsigned int rn = bits (insn, 16, 19);
5864 unsigned int rm = bits (insn, 0, 3);
5865
5866
5867 if (!insn_references_pc (insn, 0x000f000ful))
5868 return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);
5869
136821d9
SM
5870 displaced_debug_printf ("copying preload insn %.8lx",
5871 (unsigned long) insn);
7ff120b4
YQ
5872
5873 dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
cca44b1b 5874
7ff120b4 5875 install_preload_reg (gdbarch, regs, dsc, rn, rm);
cca44b1b
JB
5876 return 0;
5877}
5878
5879/* Copy/cleanup coprocessor load and store instructions. */
5880
5881static void
6e39997a 5882cleanup_copro_load_store (struct gdbarch *gdbarch,
cca44b1b 5883 struct regcache *regs,
1152d984 5884 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 5885{
36073a92 5886 ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5887
5888 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5889
5890 if (dsc->u.ldst.writeback)
5891 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
5892}
5893
7ff120b4
YQ
5894static void
5895install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5896 arm_displaced_step_copy_insn_closure *dsc,
7ff120b4 5897 int writeback, unsigned int rn)
cca44b1b 5898{
cca44b1b 5899 ULONGEST rn_val;
cca44b1b 5900
cca44b1b
JB
5901 /* Coprocessor load/store instructions:
5902
5903 {stc/stc2} [<Rn>, #+/-imm] (and other immediate addressing modes)
5904 ->
5905 {stc/stc2} [r0, #+/-imm].
5906
5907 ldc/ldc2 are handled identically. */
5908
36073a92
YQ
5909 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5910 rn_val = displaced_read_reg (regs, dsc, rn);
2b16b2e3
YQ
5911 /* PC should be 4-byte aligned. */
5912 rn_val = rn_val & 0xfffffffc;
cca44b1b
JB
5913 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5914
7ff120b4 5915 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
5916 dsc->u.ldst.rn = rn;
5917
7ff120b4
YQ
5918 dsc->cleanup = &cleanup_copro_load_store;
5919}
5920
5921static int
5922arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
5923 struct regcache *regs,
1152d984 5924 arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
5925{
5926 unsigned int rn = bits (insn, 16, 19);
5927
5928 if (!insn_references_pc (insn, 0x000f0000ul))
5929 return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);
5930
136821d9
SM
5931 displaced_debug_printf ("copying coprocessor load/store insn %.8lx",
5932 (unsigned long) insn);
7ff120b4 5933
cca44b1b
JB
5934 dsc->modinsn[0] = insn & 0xfff0ffff;
5935
7ff120b4 5936 install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);
cca44b1b
JB
5937
5938 return 0;
5939}
5940
34518530
YQ
5941static int
5942thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
5943 uint16_t insn2, struct regcache *regs,
1152d984 5944 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
5945{
5946 unsigned int rn = bits (insn1, 0, 3);
5947
5948 if (rn != ARM_PC_REGNUM)
5949 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
5950 "copro load/store", dsc);
5951
136821d9
SM
5952 displaced_debug_printf ("copying coprocessor load/store insn %.4x%.4x",
5953 insn1, insn2);
34518530
YQ
5954
5955 dsc->modinsn[0] = insn1 & 0xfff0;
5956 dsc->modinsn[1] = insn2;
5957 dsc->numinsns = 2;
5958
5959 /* This function is called for copying instruction LDC/LDC2/VLDR, which
5960 doesn't support writeback, so pass 0. */
5961 install_copro_load_store (gdbarch, regs, dsc, 0, rn);
5962
5963 return 0;
5964}
5965
cca44b1b
JB
5966/* Clean up branch instructions (actually perform the branch, by setting
5967 PC). */
5968
5969static void
6e39997a 5970cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5971 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 5972{
36073a92 5973 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
5974 int branch_taken = condition_true (dsc->u.branch.cond, status);
5975 enum pc_write_style write_pc = dsc->u.branch.exchange
5976 ? BX_WRITE_PC : BRANCH_WRITE_PC;
5977
5978 if (!branch_taken)
5979 return;
5980
5981 if (dsc->u.branch.link)
5982 {
8c8dba6d 5983 /* The value of LR should be the next insn of current one. In order
85102364 5984 not to confuse logic handling later insn `bx lr', if current insn mode
8c8dba6d
YQ
5985 is Thumb, the bit 0 of LR value should be set to 1. */
5986 ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;
5987
5988 if (dsc->is_thumb)
5989 next_insn_addr |= 0x1;
5990
5991 displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
5992 CANNOT_WRITE_PC);
cca44b1b
JB
5993 }
5994
bf9f652a 5995 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
cca44b1b
JB
5996}
5997
5998/* Copy B/BL/BLX instructions with immediate destinations. */
5999
7ff120b4
YQ
6000static void
6001install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6002 arm_displaced_step_copy_insn_closure *dsc,
7ff120b4
YQ
6003 unsigned int cond, int exchange, int link, long offset)
6004{
6005 /* Implement "BL<cond> <label>" as:
6006
6007 Preparation: cond <- instruction condition
6008 Insn: mov r0, r0 (nop)
6009 Cleanup: if (condition true) { r14 <- pc; pc <- label }.
6010
6011 B<cond> similar, but don't set r14 in cleanup. */
6012
6013 dsc->u.branch.cond = cond;
6014 dsc->u.branch.link = link;
6015 dsc->u.branch.exchange = exchange;
6016
2b16b2e3
YQ
6017 dsc->u.branch.dest = dsc->insn_addr;
6018 if (link && exchange)
6019 /* For BLX, offset is computed from the Align (PC, 4). */
6020 dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;
6021
7ff120b4 6022 if (dsc->is_thumb)
2b16b2e3 6023 dsc->u.branch.dest += 4 + offset;
7ff120b4 6024 else
2b16b2e3 6025 dsc->u.branch.dest += 8 + offset;
7ff120b4
YQ
6026
6027 dsc->cleanup = &cleanup_branch;
6028}
cca44b1b 6029static int
7ff120b4 6030arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
1152d984 6031 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
6032{
6033 unsigned int cond = bits (insn, 28, 31);
6034 int exchange = (cond == 0xf);
6035 int link = exchange || bit (insn, 24);
cca44b1b
JB
6036 long offset;
6037
136821d9
SM
6038 displaced_debug_printf ("copying %s immediate insn %.8lx",
6039 (exchange) ? "blx" : (link) ? "bl" : "b",
6040 (unsigned long) insn);
cca44b1b
JB
6041 if (exchange)
6042 /* For BLX, set bit 0 of the destination. The cleanup_branch function will
6043 then arrange the switch into Thumb mode. */
6044 offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
6045 else
6046 offset = bits (insn, 0, 23) << 2;
6047
6048 if (bit (offset, 25))
6049 offset = offset | ~0x3ffffff;
6050
cca44b1b
JB
6051 dsc->modinsn[0] = ARM_NOP;
6052
7ff120b4 6053 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
cca44b1b
JB
6054 return 0;
6055}
6056
34518530
YQ
6057static int
6058thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
6059 uint16_t insn2, struct regcache *regs,
1152d984 6060 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
6061{
6062 int link = bit (insn2, 14);
6063 int exchange = link && !bit (insn2, 12);
6064 int cond = INST_AL;
6065 long offset = 0;
6066 int j1 = bit (insn2, 13);
6067 int j2 = bit (insn2, 11);
6068 int s = sbits (insn1, 10, 10);
6069 int i1 = !(j1 ^ bit (insn1, 10));
6070 int i2 = !(j2 ^ bit (insn1, 10));
6071
6072 if (!link && !exchange) /* B */
6073 {
6074 offset = (bits (insn2, 0, 10) << 1);
6075 if (bit (insn2, 12)) /* Encoding T4 */
6076 {
6077 offset |= (bits (insn1, 0, 9) << 12)
6078 | (i2 << 22)
6079 | (i1 << 23)
6080 | (s << 24);
6081 cond = INST_AL;
6082 }
6083 else /* Encoding T3 */
6084 {
6085 offset |= (bits (insn1, 0, 5) << 12)
6086 | (j1 << 18)
6087 | (j2 << 19)
6088 | (s << 20);
6089 cond = bits (insn1, 6, 9);
6090 }
6091 }
6092 else
6093 {
6094 offset = (bits (insn1, 0, 9) << 12);
6095 offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
6096 offset |= exchange ?
6097 (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
6098 }
6099
136821d9
SM
6100 displaced_debug_printf ("copying %s insn %.4x %.4x with offset %.8lx",
6101 link ? (exchange) ? "blx" : "bl" : "b",
6102 insn1, insn2, offset);
34518530
YQ
6103
6104 dsc->modinsn[0] = THUMB_NOP;
6105
6106 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
6107 return 0;
6108}
6109
6110/* Copy B Thumb instructions. */
6111static int
615234c1 6112thumb_copy_b (struct gdbarch *gdbarch, uint16_t insn,
1152d984 6113 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
6114{
6115 unsigned int cond = 0;
6116 int offset = 0;
6117 unsigned short bit_12_15 = bits (insn, 12, 15);
6118 CORE_ADDR from = dsc->insn_addr;
6119
6120 if (bit_12_15 == 0xd)
6121 {
6122 /* offset = SignExtend (imm8:0, 32) */
6123 offset = sbits ((insn << 1), 0, 8);
6124 cond = bits (insn, 8, 11);
6125 }
6126 else if (bit_12_15 == 0xe) /* Encoding T2 */
6127 {
6128 offset = sbits ((insn << 1), 0, 11);
6129 cond = INST_AL;
6130 }
6131
136821d9
SM
6132 displaced_debug_printf ("copying b immediate insn %.4x with offset %d",
6133 insn, offset);
34518530
YQ
6134
6135 dsc->u.branch.cond = cond;
6136 dsc->u.branch.link = 0;
6137 dsc->u.branch.exchange = 0;
6138 dsc->u.branch.dest = from + 4 + offset;
6139
6140 dsc->modinsn[0] = THUMB_NOP;
6141
6142 dsc->cleanup = &cleanup_branch;
6143
6144 return 0;
6145}
6146
cca44b1b
JB
6147/* Copy BX/BLX with register-specified destinations. */
6148
7ff120b4
YQ
6149static void
6150install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6151 arm_displaced_step_copy_insn_closure *dsc, int link,
7ff120b4 6152 unsigned int cond, unsigned int rm)
cca44b1b 6153{
cca44b1b
JB
6154 /* Implement {BX,BLX}<cond> <reg>" as:
6155
6156 Preparation: cond <- instruction condition
6157 Insn: mov r0, r0 (nop)
6158 Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
6159
6160 Don't set r14 in cleanup for BX. */
6161
36073a92 6162 dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6163
6164 dsc->u.branch.cond = cond;
6165 dsc->u.branch.link = link;
cca44b1b 6166
7ff120b4 6167 dsc->u.branch.exchange = 1;
cca44b1b
JB
6168
6169 dsc->cleanup = &cleanup_branch;
7ff120b4 6170}
cca44b1b 6171
7ff120b4
YQ
6172static int
6173arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
1152d984 6174 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
6175{
6176 unsigned int cond = bits (insn, 28, 31);
6177 /* BX: x12xxx1x
6178 BLX: x12xxx3x. */
6179 int link = bit (insn, 5);
6180 unsigned int rm = bits (insn, 0, 3);
6181
136821d9 6182 displaced_debug_printf ("copying insn %.8lx", (unsigned long) insn);
7ff120b4
YQ
6183
6184 dsc->modinsn[0] = ARM_NOP;
6185
6186 install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
cca44b1b
JB
6187 return 0;
6188}
6189
34518530
YQ
6190static int
6191thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
6192 struct regcache *regs,
1152d984 6193 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
6194{
6195 int link = bit (insn, 7);
6196 unsigned int rm = bits (insn, 3, 6);
6197
136821d9 6198 displaced_debug_printf ("copying insn %.4x", (unsigned short) insn);
34518530
YQ
6199
6200 dsc->modinsn[0] = THUMB_NOP;
6201
6202 install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);
6203
6204 return 0;
6205}
6206
6207
0963b4bd 6208/* Copy/cleanup arithmetic/logic instruction with immediate RHS. */
cca44b1b
JB
6209
6210static void
6e39997a 6211cleanup_alu_imm (struct gdbarch *gdbarch,
1152d984 6212 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6213{
36073a92 6214 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6215 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6216 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6217 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6218}
6219
6220static int
7ff120b4 6221arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
1152d984 6222 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
6223{
6224 unsigned int rn = bits (insn, 16, 19);
6225 unsigned int rd = bits (insn, 12, 15);
6226 unsigned int op = bits (insn, 21, 24);
6227 int is_mov = (op == 0xd);
6228 ULONGEST rd_val, rn_val;
cca44b1b
JB
6229
6230 if (!insn_references_pc (insn, 0x000ff000ul))
7ff120b4 6231 return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
cca44b1b 6232
136821d9
SM
6233 displaced_debug_printf ("copying immediate %s insn %.8lx",
6234 is_mov ? "move" : "ALU",
6235 (unsigned long) insn);
cca44b1b
JB
6236
6237 /* Instruction is of form:
6238
6239 <op><cond> rd, [rn,] #imm
6240
6241 Rewrite as:
6242
6243 Preparation: tmp1, tmp2 <- r0, r1;
6244 r0, r1 <- rd, rn
6245 Insn: <op><cond> r0, r1, #imm
6246 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6247 */
6248
36073a92
YQ
6249 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6250 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6251 rn_val = displaced_read_reg (regs, dsc, rn);
6252 rd_val = displaced_read_reg (regs, dsc, rd);
cca44b1b
JB
6253 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6254 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6255 dsc->rd = rd;
6256
6257 if (is_mov)
6258 dsc->modinsn[0] = insn & 0xfff00fff;
6259 else
6260 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
6261
6262 dsc->cleanup = &cleanup_alu_imm;
6263
6264 return 0;
6265}
6266
34518530
YQ
6267static int
6268thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
6269 uint16_t insn2, struct regcache *regs,
1152d984 6270 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
6271{
6272 unsigned int op = bits (insn1, 5, 8);
6273 unsigned int rn, rm, rd;
6274 ULONGEST rd_val, rn_val;
6275
6276 rn = bits (insn1, 0, 3); /* Rn */
6277 rm = bits (insn2, 0, 3); /* Rm */
6278 rd = bits (insn2, 8, 11); /* Rd */
6279
6280 /* This routine is only called for instruction MOV. */
6281 gdb_assert (op == 0x2 && rn == 0xf);
6282
6283 if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
6284 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);
6285
136821d9 6286 displaced_debug_printf ("copying reg %s insn %.4x%.4x", "ALU", insn1, insn2);
34518530
YQ
6287
6288 /* Instruction is of form:
6289
6290 <op><cond> rd, [rn,] #imm
6291
6292 Rewrite as:
6293
6294 Preparation: tmp1, tmp2 <- r0, r1;
6295 r0, r1 <- rd, rn
6296 Insn: <op><cond> r0, r1, #imm
6297 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6298 */
6299
6300 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6301 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6302 rn_val = displaced_read_reg (regs, dsc, rn);
6303 rd_val = displaced_read_reg (regs, dsc, rd);
6304 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6305 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6306 dsc->rd = rd;
6307
6308 dsc->modinsn[0] = insn1;
6309 dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
6310 dsc->numinsns = 2;
6311
6312 dsc->cleanup = &cleanup_alu_imm;
6313
6314 return 0;
6315}
6316
cca44b1b
JB
6317/* Copy/cleanup arithmetic/logic insns with register RHS. */
6318
6319static void
6e39997a 6320cleanup_alu_reg (struct gdbarch *gdbarch,
1152d984 6321 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
6322{
6323 ULONGEST rd_val;
6324 int i;
6325
36073a92 6326 rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6327
6328 for (i = 0; i < 3; i++)
6329 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6330
6331 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6332}
6333
7ff120b4
YQ
6334static void
6335install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6336 arm_displaced_step_copy_insn_closure *dsc,
7ff120b4 6337 unsigned int rd, unsigned int rn, unsigned int rm)
cca44b1b 6338{
cca44b1b 6339 ULONGEST rd_val, rn_val, rm_val;
cca44b1b 6340
cca44b1b
JB
6341 /* Instruction is of form:
6342
6343 <op><cond> rd, [rn,] rm [, <shift>]
6344
6345 Rewrite as:
6346
6347 Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
6348 r0, r1, r2 <- rd, rn, rm
ef713951 6349 Insn: <op><cond> r0, [r1,] r2 [, <shift>]
cca44b1b
JB
6350 Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
6351 */
6352
36073a92
YQ
6353 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6354 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6355 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6356 rd_val = displaced_read_reg (regs, dsc, rd);
6357 rn_val = displaced_read_reg (regs, dsc, rn);
6358 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6359 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6360 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6361 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6362 dsc->rd = rd;
6363
7ff120b4
YQ
6364 dsc->cleanup = &cleanup_alu_reg;
6365}
6366
6367static int
6368arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
1152d984 6369 arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
6370{
6371 unsigned int op = bits (insn, 21, 24);
6372 int is_mov = (op == 0xd);
6373
6374 if (!insn_references_pc (insn, 0x000ff00ful))
6375 return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);
6376
136821d9
SM
6377 displaced_debug_printf ("copying reg %s insn %.8lx",
6378 is_mov ? "move" : "ALU", (unsigned long) insn);
7ff120b4 6379
cca44b1b
JB
6380 if (is_mov)
6381 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
6382 else
6383 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
6384
7ff120b4
YQ
6385 install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
6386 bits (insn, 0, 3));
cca44b1b
JB
6387 return 0;
6388}
6389
34518530
YQ
6390static int
6391thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
6392 struct regcache *regs,
1152d984 6393 arm_displaced_step_copy_insn_closure *dsc)
34518530 6394{
ef713951 6395 unsigned rm, rd;
34518530 6396
ef713951
YQ
6397 rm = bits (insn, 3, 6);
6398 rd = (bit (insn, 7) << 3) | bits (insn, 0, 2);
34518530 6399
ef713951 6400 if (rd != ARM_PC_REGNUM && rm != ARM_PC_REGNUM)
34518530
YQ
6401 return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);
6402
136821d9 6403 displaced_debug_printf ("copying ALU reg insn %.4x", (unsigned short) insn);
34518530 6404
ef713951 6405 dsc->modinsn[0] = ((insn & 0xff00) | 0x10);
34518530 6406
ef713951 6407 install_alu_reg (gdbarch, regs, dsc, rd, rd, rm);
34518530
YQ
6408
6409 return 0;
6410}
6411
cca44b1b
JB
6412/* Cleanup/copy arithmetic/logic insns with shifted register RHS. */
6413
6414static void
6e39997a 6415cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
cca44b1b 6416 struct regcache *regs,
1152d984 6417 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6418{
36073a92 6419 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6420 int i;
6421
6422 for (i = 0; i < 4; i++)
6423 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6424
6425 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6426}
6427
7ff120b4
YQ
6428static void
6429install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6430 arm_displaced_step_copy_insn_closure *dsc,
7ff120b4
YQ
6431 unsigned int rd, unsigned int rn, unsigned int rm,
6432 unsigned rs)
cca44b1b 6433{
7ff120b4 6434 int i;
cca44b1b 6435 ULONGEST rd_val, rn_val, rm_val, rs_val;
cca44b1b 6436
cca44b1b
JB
6437 /* Instruction is of form:
6438
6439 <op><cond> rd, [rn,] rm, <shift> rs
6440
6441 Rewrite as:
6442
6443 Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
6444 r0, r1, r2, r3 <- rd, rn, rm, rs
6445 Insn: <op><cond> r0, r1, r2, <shift> r3
6446 Cleanup: tmp5 <- r0
6447 r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
6448 rd <- tmp5
6449 */
6450
6451 for (i = 0; i < 4; i++)
36073a92 6452 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b 6453
36073a92
YQ
6454 rd_val = displaced_read_reg (regs, dsc, rd);
6455 rn_val = displaced_read_reg (regs, dsc, rn);
6456 rm_val = displaced_read_reg (regs, dsc, rm);
6457 rs_val = displaced_read_reg (regs, dsc, rs);
cca44b1b
JB
6458 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6459 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6460 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6461 displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
6462 dsc->rd = rd;
7ff120b4
YQ
6463 dsc->cleanup = &cleanup_alu_shifted_reg;
6464}
6465
6466static int
6467arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
6468 struct regcache *regs,
1152d984 6469 arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
6470{
6471 unsigned int op = bits (insn, 21, 24);
6472 int is_mov = (op == 0xd);
6473 unsigned int rd, rn, rm, rs;
6474
6475 if (!insn_references_pc (insn, 0x000fff0ful))
6476 return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
6477
136821d9
SM
6478 displaced_debug_printf ("copying shifted reg %s insn %.8lx",
6479 is_mov ? "move" : "ALU",
6480 (unsigned long) insn);
7ff120b4
YQ
6481
6482 rn = bits (insn, 16, 19);
6483 rm = bits (insn, 0, 3);
6484 rs = bits (insn, 8, 11);
6485 rd = bits (insn, 12, 15);
cca44b1b
JB
6486
6487 if (is_mov)
6488 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
6489 else
6490 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
6491
7ff120b4 6492 install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);
cca44b1b
JB
6493
6494 return 0;
6495}
6496
6497/* Clean up load instructions. */
6498
6499static void
6e39997a 6500cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6501 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
6502{
6503 ULONGEST rt_val, rt_val2 = 0, rn_val;
cca44b1b 6504
36073a92 6505 rt_val = displaced_read_reg (regs, dsc, 0);
cca44b1b 6506 if (dsc->u.ldst.xfersize == 8)
36073a92
YQ
6507 rt_val2 = displaced_read_reg (regs, dsc, 1);
6508 rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
6509
6510 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6511 if (dsc->u.ldst.xfersize > 4)
6512 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6513 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6514 if (!dsc->u.ldst.immed)
6515 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6516
6517 /* Handle register writeback. */
6518 if (dsc->u.ldst.writeback)
6519 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6520 /* Put result in right place. */
6521 displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
6522 if (dsc->u.ldst.xfersize == 8)
6523 displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
6524}
6525
6526/* Clean up store instructions. */
6527
6528static void
6e39997a 6529cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6530 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6531{
36073a92 6532 ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
6533
6534 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6535 if (dsc->u.ldst.xfersize > 4)
6536 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6537 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6538 if (!dsc->u.ldst.immed)
6539 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6540 if (!dsc->u.ldst.restore_r4)
6541 displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
6542
6543 /* Writeback. */
6544 if (dsc->u.ldst.writeback)
6545 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6546}
6547
6548/* Copy "extra" load/store instructions. These are halfword/doubleword
6549 transfers, which have a different encoding to byte/word transfers. */
6550
6551static int
550dc4e2 6552arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unprivileged,
1152d984 6553 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
6554{
6555 unsigned int op1 = bits (insn, 20, 24);
6556 unsigned int op2 = bits (insn, 5, 6);
6557 unsigned int rt = bits (insn, 12, 15);
6558 unsigned int rn = bits (insn, 16, 19);
6559 unsigned int rm = bits (insn, 0, 3);
6560 char load[12] = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
6561 char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
6562 int immed = (op1 & 0x4) != 0;
6563 int opcode;
6564 ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
cca44b1b
JB
6565
6566 if (!insn_references_pc (insn, 0x000ff00ful))
7ff120b4 6567 return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);
cca44b1b 6568
136821d9
SM
6569 displaced_debug_printf ("copying %sextra load/store insn %.8lx",
6570 unprivileged ? "unprivileged " : "",
6571 (unsigned long) insn);
cca44b1b
JB
6572
6573 opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
6574
6575 if (opcode < 0)
f34652de 6576 internal_error (_("copy_extra_ld_st: instruction decode error"));
cca44b1b 6577
36073a92
YQ
6578 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6579 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6580 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 6581 if (!immed)
36073a92 6582 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 6583
36073a92 6584 rt_val = displaced_read_reg (regs, dsc, rt);
cca44b1b 6585 if (bytesize[opcode] == 8)
36073a92
YQ
6586 rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
6587 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 6588 if (!immed)
36073a92 6589 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6590
6591 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6592 if (bytesize[opcode] == 8)
6593 displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
6594 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6595 if (!immed)
6596 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
6597
6598 dsc->rd = rt;
6599 dsc->u.ldst.xfersize = bytesize[opcode];
6600 dsc->u.ldst.rn = rn;
6601 dsc->u.ldst.immed = immed;
6602 dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
6603 dsc->u.ldst.restore_r4 = 0;
6604
6605 if (immed)
6606 /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
6607 ->
6608 {ldr,str}<width><cond> r0, [r1,] [r2, #imm]. */
6609 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6610 else
6611 /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
6612 ->
6613 {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3]. */
6614 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6615
6616 dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
6617
6618 return 0;
6619}
6620
0f6f04ba 6621/* Copy byte/half word/word loads and stores. */
cca44b1b 6622
7ff120b4 6623static void
0f6f04ba 6624install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6625 arm_displaced_step_copy_insn_closure *dsc, int load,
0f6f04ba
YQ
6626 int immed, int writeback, int size, int usermode,
6627 int rt, int rm, int rn)
cca44b1b 6628{
cca44b1b 6629 ULONGEST rt_val, rn_val, rm_val = 0;
cca44b1b 6630
36073a92
YQ
6631 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6632 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 6633 if (!immed)
36073a92 6634 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 6635 if (!load)
36073a92 6636 dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);
cca44b1b 6637
36073a92
YQ
6638 rt_val = displaced_read_reg (regs, dsc, rt);
6639 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 6640 if (!immed)
36073a92 6641 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6642
6643 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6644 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6645 if (!immed)
6646 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
cca44b1b 6647 dsc->rd = rt;
0f6f04ba 6648 dsc->u.ldst.xfersize = size;
cca44b1b
JB
6649 dsc->u.ldst.rn = rn;
6650 dsc->u.ldst.immed = immed;
7ff120b4 6651 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
6652
6653 /* To write PC we can do:
6654
494e194e
YQ
6655 Before this sequence of instructions:
6656 r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
85102364 6657 r2 is the Rn value got from displaced_read_reg.
494e194e
YQ
6658
6659 Insn1: push {pc} Write address of STR instruction + offset on stack
6660 Insn2: pop {r4} Read it back from stack, r4 = addr(Insn1) + offset
6661 Insn3: sub r4, r4, pc r4 = addr(Insn1) + offset - pc
dda83cd7
SM
6662 = addr(Insn1) + offset - addr(Insn3) - 8
6663 = offset - 16
494e194e
YQ
6664 Insn4: add r4, r4, #8 r4 = offset - 8
6665 Insn5: add r0, r0, r4 r0 = from + 8 + offset - 8
dda83cd7 6666 = from + offset
494e194e 6667 Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])
cca44b1b
JB
6668
6669 Otherwise we don't know what value to write for PC, since the offset is
494e194e
YQ
6670 architecture-dependent (sometimes PC+8, sometimes PC+12). More details
6671 of this can be found in Section "Saving from r15" in
6672 http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */
cca44b1b 6673
7ff120b4
YQ
6674 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6675}
6676
34518530
YQ
6677
6678static int
6679thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
6680 uint16_t insn2, struct regcache *regs,
1152d984 6681 arm_displaced_step_copy_insn_closure *dsc, int size)
34518530
YQ
6682{
6683 unsigned int u_bit = bit (insn1, 7);
6684 unsigned int rt = bits (insn2, 12, 15);
6685 int imm12 = bits (insn2, 0, 11);
6686 ULONGEST pc_val;
6687
136821d9
SM
6688 displaced_debug_printf ("copying ldr pc (0x%x) R%d %c imm12 %.4x",
6689 (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
6690 imm12);
34518530
YQ
6691
6692 if (!u_bit)
6693 imm12 = -1 * imm12;
6694
6695 /* Rewrite instruction LDR Rt imm12 into:
6696
6697 Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12
6698
6699 LDR R0, R2, R3,
6700
6701 Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2]. */
6702
6703
6704 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6705 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6706 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6707
6708 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6709
6710 pc_val = pc_val & 0xfffffffc;
6711
6712 displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
6713 displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);
6714
6715 dsc->rd = rt;
6716
6717 dsc->u.ldst.xfersize = size;
6718 dsc->u.ldst.immed = 0;
6719 dsc->u.ldst.writeback = 0;
6720 dsc->u.ldst.restore_r4 = 0;
6721
6722 /* LDR R0, R2, R3 */
6723 dsc->modinsn[0] = 0xf852;
6724 dsc->modinsn[1] = 0x3;
6725 dsc->numinsns = 2;
6726
6727 dsc->cleanup = &cleanup_load;
6728
6729 return 0;
6730}
6731
6732static int
6733thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
6734 uint16_t insn2, struct regcache *regs,
1152d984 6735 arm_displaced_step_copy_insn_closure *dsc,
34518530
YQ
6736 int writeback, int immed)
6737{
6738 unsigned int rt = bits (insn2, 12, 15);
6739 unsigned int rn = bits (insn1, 0, 3);
6740 unsigned int rm = bits (insn2, 0, 3); /* Only valid if !immed. */
6741 /* In LDR (register), there is also a register Rm, which is not allowed to
6742 be PC, so we don't have to check it. */
6743
6744 if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
6745 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
6746 dsc);
6747
136821d9
SM
6748 displaced_debug_printf ("copying ldr r%d [r%d] insn %.4x%.4x",
6749 rt, rn, insn1, insn2);
34518530
YQ
6750
6751 install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
6752 0, rt, rm, rn);
6753
6754 dsc->u.ldst.restore_r4 = 0;
6755
6756 if (immed)
6757 /* ldr[b]<cond> rt, [rn, #imm], etc.
6758 ->
6759 ldr[b]<cond> r0, [r2, #imm]. */
6760 {
6761 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6762 dsc->modinsn[1] = insn2 & 0x0fff;
6763 }
6764 else
6765 /* ldr[b]<cond> rt, [rn, rm], etc.
6766 ->
6767 ldr[b]<cond> r0, [r2, r3]. */
6768 {
6769 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6770 dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
6771 }
6772
6773 dsc->numinsns = 2;
6774
6775 return 0;
6776}
6777
6778
7ff120b4
YQ
6779static int
6780arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
6781 struct regcache *regs,
1152d984 6782 arm_displaced_step_copy_insn_closure *dsc,
0f6f04ba 6783 int load, int size, int usermode)
7ff120b4
YQ
6784{
6785 int immed = !bit (insn, 25);
6786 int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
6787 unsigned int rt = bits (insn, 12, 15);
6788 unsigned int rn = bits (insn, 16, 19);
6789 unsigned int rm = bits (insn, 0, 3); /* Only valid if !immed. */
6790
6791 if (!insn_references_pc (insn, 0x000ff00ful))
6792 return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);
6793
136821d9
SM
6794 displaced_debug_printf ("copying %s%s r%d [r%d] insn %.8lx",
6795 load ? (size == 1 ? "ldrb" : "ldr")
6796 : (size == 1 ? "strb" : "str"),
6797 usermode ? "t" : "",
6798 rt, rn,
6799 (unsigned long) insn);
7ff120b4 6800
0f6f04ba
YQ
6801 install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
6802 usermode, rt, rm, rn);
7ff120b4 6803
bf9f652a 6804 if (load || rt != ARM_PC_REGNUM)
cca44b1b
JB
6805 {
6806 dsc->u.ldst.restore_r4 = 0;
6807
6808 if (immed)
6809 /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
6810 ->
6811 {ldr,str}[b]<cond> r0, [r2, #imm]. */
6812 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6813 else
6814 /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
6815 ->
6816 {ldr,str}[b]<cond> r0, [r2, r3]. */
6817 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6818 }
6819 else
6820 {
6821 /* We need to use r4 as scratch. Make sure it's restored afterwards. */
6822 dsc->u.ldst.restore_r4 = 1;
494e194e
YQ
6823 dsc->modinsn[0] = 0xe92d8000; /* push {pc} */
6824 dsc->modinsn[1] = 0xe8bd0010; /* pop {r4} */
cca44b1b
JB
6825 dsc->modinsn[2] = 0xe044400f; /* sub r4, r4, pc. */
6826 dsc->modinsn[3] = 0xe2844008; /* add r4, r4, #8. */
6827 dsc->modinsn[4] = 0xe0800004; /* add r0, r0, r4. */
6828
6829 /* As above. */
6830 if (immed)
6831 dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
6832 else
6833 dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
6834
cca44b1b
JB
6835 dsc->numinsns = 6;
6836 }
6837
6838 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6839
6840 return 0;
6841}
6842
6843/* Cleanup LDM instructions with fully-populated register list. This is an
6844 unfortunate corner case: it's impossible to implement correctly by modifying
6845 the instruction. The issue is as follows: we have an instruction,
6846
6847 ldm rN, {r0-r15}
6848
6849 which we must rewrite to avoid loading PC. A possible solution would be to
6850 do the load in two halves, something like (with suitable cleanup
6851 afterwards):
6852
6853 mov r8, rN
6854 ldm[id][ab] r8!, {r0-r7}
6855 str r7, <temp>
6856 ldm[id][ab] r8, {r7-r14}
6857 <bkpt>
6858
6859 but at present there's no suitable place for <temp>, since the scratch space
6860 is overwritten before the cleanup routine is called. For now, we simply
6861 emulate the instruction. */
6862
6863static void
6864cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6865 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6866{
cca44b1b
JB
6867 int inc = dsc->u.block.increment;
6868 int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
6869 int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
6870 uint32_t regmask = dsc->u.block.regmask;
6871 int regno = inc ? 0 : 15;
6872 CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
6873 int exception_return = dsc->u.block.load && dsc->u.block.user
6874 && (regmask & 0x8000) != 0;
36073a92 6875 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
6876 int do_transfer = condition_true (dsc->u.block.cond, status);
6877 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6878
6879 if (!do_transfer)
6880 return;
6881
6882 /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
6883 sensible we can do here. Complain loudly. */
6884 if (exception_return)
6885 error (_("Cannot single-step exception return"));
6886
6887 /* We don't handle any stores here for now. */
6888 gdb_assert (dsc->u.block.load != 0);
6889
136821d9
SM
6890 displaced_debug_printf ("emulating block transfer: %s %s %s",
6891 dsc->u.block.load ? "ldm" : "stm",
6892 dsc->u.block.increment ? "inc" : "dec",
6893 dsc->u.block.before ? "before" : "after");
cca44b1b
JB
6894
6895 while (regmask)
6896 {
6897 uint32_t memword;
6898
6899 if (inc)
bf9f652a 6900 while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
cca44b1b
JB
6901 regno++;
6902 else
6903 while (regno >= 0 && (regmask & (1 << regno)) == 0)
6904 regno--;
6905
6906 xfer_addr += bump_before;
6907
6908 memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
6909 displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
6910
6911 xfer_addr += bump_after;
6912
6913 regmask &= ~(1 << regno);
6914 }
6915
6916 if (dsc->u.block.writeback)
6917 displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
6918 CANNOT_WRITE_PC);
6919}
6920
6921/* Clean up an STM which included the PC in the register list. */
6922
6923static void
6924cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6925 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6926{
36073a92 6927 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b 6928 int store_executed = condition_true (dsc->u.block.cond, status);
5f661e03
SM
6929 CORE_ADDR pc_stored_at, transferred_regs
6930 = count_one_bits (dsc->u.block.regmask);
cca44b1b
JB
6931 CORE_ADDR stm_insn_addr;
6932 uint32_t pc_val;
6933 long offset;
6934 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6935
6936 /* If condition code fails, there's nothing else to do. */
6937 if (!store_executed)
6938 return;
6939
6940 if (dsc->u.block.increment)
6941 {
6942 pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
6943
6944 if (dsc->u.block.before)
6945 pc_stored_at += 4;
6946 }
6947 else
6948 {
6949 pc_stored_at = dsc->u.block.xfer_addr;
6950
6951 if (dsc->u.block.before)
6952 pc_stored_at -= 4;
6953 }
6954
6955 pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
6956 stm_insn_addr = dsc->scratch_base;
6957 offset = pc_val - stm_insn_addr;
6958
136821d9
SM
6959 displaced_debug_printf ("detected PC offset %.8lx for STM instruction",
6960 offset);
cca44b1b
JB
6961
6962 /* Rewrite the stored PC to the proper value for the non-displaced original
6963 instruction. */
6964 write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
6965 dsc->insn_addr + offset);
6966}
6967
6968/* Clean up an LDM which includes the PC in the register list. We clumped all
6969 the registers in the transferred list into a contiguous range r0...rX (to
6970 avoid loading PC directly and losing control of the debugged program), so we
6971 must undo that here. */
6972
6973static void
6e39997a 6974cleanup_block_load_pc (struct gdbarch *gdbarch,
cca44b1b 6975 struct regcache *regs,
1152d984 6976 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6977{
36073a92 6978 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
22e048c9 6979 int load_executed = condition_true (dsc->u.block.cond, status);
bf9f652a 6980 unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
5f661e03 6981 unsigned int regs_loaded = count_one_bits (mask);
cca44b1b
JB
6982 unsigned int num_to_shuffle = regs_loaded, clobbered;
6983
6984 /* The method employed here will fail if the register list is fully populated
6985 (we need to avoid loading PC directly). */
6986 gdb_assert (num_to_shuffle < 16);
6987
6988 if (!load_executed)
6989 return;
6990
6991 clobbered = (1 << num_to_shuffle) - 1;
6992
6993 while (num_to_shuffle > 0)
6994 {
6995 if ((mask & (1 << write_reg)) != 0)
6996 {
6997 unsigned int read_reg = num_to_shuffle - 1;
6998
6999 if (read_reg != write_reg)
7000 {
36073a92 7001 ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
cca44b1b 7002 displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
136821d9
SM
7003 displaced_debug_printf ("LDM: move loaded register r%d to r%d",
7004 read_reg, write_reg);
cca44b1b 7005 }
136821d9
SM
7006 else
7007 displaced_debug_printf ("LDM: register r%d already in the right "
7008 "place", write_reg);
cca44b1b
JB
7009
7010 clobbered &= ~(1 << write_reg);
7011
7012 num_to_shuffle--;
7013 }
7014
7015 write_reg--;
7016 }
7017
7018 /* Restore any registers we scribbled over. */
7019 for (write_reg = 0; clobbered != 0; write_reg++)
7020 {
7021 if ((clobbered & (1 << write_reg)) != 0)
7022 {
7023 displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
7024 CANNOT_WRITE_PC);
136821d9
SM
7025 displaced_debug_printf ("LDM: restored clobbered register r%d",
7026 write_reg);
cca44b1b
JB
7027 clobbered &= ~(1 << write_reg);
7028 }
7029 }
7030
7031 /* Perform register writeback manually. */
7032 if (dsc->u.block.writeback)
7033 {
7034 ULONGEST new_rn_val = dsc->u.block.xfer_addr;
7035
7036 if (dsc->u.block.increment)
7037 new_rn_val += regs_loaded * 4;
7038 else
7039 new_rn_val -= regs_loaded * 4;
7040
7041 displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
7042 CANNOT_WRITE_PC);
7043 }
7044}
7045
7046/* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
7047 in user-level code (in particular exception return, ldm rn, {...pc}^). */
7048
7049static int
7ff120b4
YQ
7050arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
7051 struct regcache *regs,
1152d984 7052 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7053{
7054 int load = bit (insn, 20);
7055 int user = bit (insn, 22);
7056 int increment = bit (insn, 23);
7057 int before = bit (insn, 24);
7058 int writeback = bit (insn, 21);
7059 int rn = bits (insn, 16, 19);
cca44b1b 7060
0963b4bd
MS
7061 /* Block transfers which don't mention PC can be run directly
7062 out-of-line. */
bf9f652a 7063 if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
7ff120b4 7064 return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
cca44b1b 7065
bf9f652a 7066 if (rn == ARM_PC_REGNUM)
cca44b1b 7067 {
0963b4bd
MS
7068 warning (_("displaced: Unpredictable LDM or STM with "
7069 "base register r15"));
7ff120b4 7070 return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
cca44b1b
JB
7071 }
7072
136821d9
SM
7073 displaced_debug_printf ("copying block transfer insn %.8lx",
7074 (unsigned long) insn);
cca44b1b 7075
36073a92 7076 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b
JB
7077 dsc->u.block.rn = rn;
7078
7079 dsc->u.block.load = load;
7080 dsc->u.block.user = user;
7081 dsc->u.block.increment = increment;
7082 dsc->u.block.before = before;
7083 dsc->u.block.writeback = writeback;
7084 dsc->u.block.cond = bits (insn, 28, 31);
7085
7086 dsc->u.block.regmask = insn & 0xffff;
7087
7088 if (load)
7089 {
7090 if ((insn & 0xffff) == 0xffff)
7091 {
7092 /* LDM with a fully-populated register list. This case is
7093 particularly tricky. Implement for now by fully emulating the
7094 instruction (which might not behave perfectly in all cases, but
7095 these instructions should be rare enough for that not to matter
7096 too much). */
7097 dsc->modinsn[0] = ARM_NOP;
7098
7099 dsc->cleanup = &cleanup_block_load_all;
7100 }
7101 else
7102 {
7103 /* LDM of a list of registers which includes PC. Implement by
7104 rewriting the list of registers to be transferred into a
7105 contiguous chunk r0...rX before doing the transfer, then shuffling
7106 registers into the correct places in the cleanup routine. */
7107 unsigned int regmask = insn & 0xffff;
5f661e03 7108 unsigned int num_in_list = count_one_bits (regmask), new_regmask;
bec2ab5a 7109 unsigned int i;
cca44b1b
JB
7110
7111 for (i = 0; i < num_in_list; i++)
36073a92 7112 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b
JB
7113
7114 /* Writeback makes things complicated. We need to avoid clobbering
7115 the base register with one of the registers in our modified
7116 register list, but just using a different register can't work in
7117 all cases, e.g.:
7118
7119 ldm r14!, {r0-r13,pc}
7120
7121 which would need to be rewritten as:
7122
7123 ldm rN!, {r0-r14}
7124
7125 but that can't work, because there's no free register for N.
7126
7127 Solve this by turning off the writeback bit, and emulating
7128 writeback manually in the cleanup routine. */
7129
7130 if (writeback)
7131 insn &= ~(1 << 21);
7132
7133 new_regmask = (1 << num_in_list) - 1;
7134
136821d9
SM
7135 displaced_debug_printf ("LDM r%d%s, {..., pc}: original reg list "
7136 "%.4x, modified list %.4x",
7137 rn, writeback ? "!" : "",
7138 (int) insn & 0xffff, new_regmask);
cca44b1b
JB
7139
7140 dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
7141
7142 dsc->cleanup = &cleanup_block_load_pc;
7143 }
7144 }
7145 else
7146 {
7147 /* STM of a list of registers which includes PC. Run the instruction
7148 as-is, but out of line: this will store the wrong value for the PC,
7149 so we must manually fix up the memory in the cleanup routine.
7150 Doing things this way has the advantage that we can auto-detect
7151 the offset of the PC write (which is architecture-dependent) in
7152 the cleanup routine. */
7153 dsc->modinsn[0] = insn;
7154
7155 dsc->cleanup = &cleanup_block_store_pc;
7156 }
7157
7158 return 0;
7159}
7160
34518530
YQ
7161static int
7162thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
7163 struct regcache *regs,
1152d984 7164 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 7165{
34518530
YQ
7166 int rn = bits (insn1, 0, 3);
7167 int load = bit (insn1, 4);
7168 int writeback = bit (insn1, 5);
cca44b1b 7169
34518530
YQ
7170 /* Block transfers which don't mention PC can be run directly
7171 out-of-line. */
7172 if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
7173 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);
7ff120b4 7174
34518530
YQ
7175 if (rn == ARM_PC_REGNUM)
7176 {
7177 warning (_("displaced: Unpredictable LDM or STM with "
7178 "base register r15"));
7179 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7180 "unpredictable ldm/stm", dsc);
7181 }
cca44b1b 7182
136821d9
SM
7183 displaced_debug_printf ("copying block transfer insn %.4x%.4x",
7184 insn1, insn2);
cca44b1b 7185
34518530
YQ
7186 /* Clear bit 13, since it should be always zero. */
7187 dsc->u.block.regmask = (insn2 & 0xdfff);
7188 dsc->u.block.rn = rn;
cca44b1b 7189
34518530
YQ
7190 dsc->u.block.load = load;
7191 dsc->u.block.user = 0;
7192 dsc->u.block.increment = bit (insn1, 7);
7193 dsc->u.block.before = bit (insn1, 8);
7194 dsc->u.block.writeback = writeback;
7195 dsc->u.block.cond = INST_AL;
7196 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b 7197
34518530
YQ
7198 if (load)
7199 {
7200 if (dsc->u.block.regmask == 0xffff)
7201 {
7202 /* This branch is impossible to happen. */
7203 gdb_assert (0);
7204 }
7205 else
7206 {
7207 unsigned int regmask = dsc->u.block.regmask;
5f661e03 7208 unsigned int num_in_list = count_one_bits (regmask), new_regmask;
bec2ab5a 7209 unsigned int i;
34518530
YQ
7210
7211 for (i = 0; i < num_in_list; i++)
7212 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7213
7214 if (writeback)
7215 insn1 &= ~(1 << 5);
7216
7217 new_regmask = (1 << num_in_list) - 1;
7218
136821d9
SM
7219 displaced_debug_printf ("LDM r%d%s, {..., pc}: original reg list "
7220 "%.4x, modified list %.4x",
7221 rn, writeback ? "!" : "",
7222 (int) dsc->u.block.regmask, new_regmask);
34518530
YQ
7223
7224 dsc->modinsn[0] = insn1;
7225 dsc->modinsn[1] = (new_regmask & 0xffff);
7226 dsc->numinsns = 2;
7227
7228 dsc->cleanup = &cleanup_block_load_pc;
7229 }
7230 }
7231 else
7232 {
7233 dsc->modinsn[0] = insn1;
7234 dsc->modinsn[1] = insn2;
7235 dsc->numinsns = 2;
7236 dsc->cleanup = &cleanup_block_store_pc;
7237 }
7238 return 0;
7239}
7240
d9311bfa
AT
7241/* Wrapper over read_memory_unsigned_integer for use in arm_get_next_pcs.
7242 This is used to avoid a dependency on BFD's bfd_endian enum. */
7243
7244ULONGEST
7245arm_get_next_pcs_read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
7246 int byte_order)
7247{
5f2dfcfd
AT
7248 return read_memory_unsigned_integer (memaddr, len,
7249 (enum bfd_endian) byte_order);
d9311bfa
AT
7250}
7251
7252/* Wrapper over gdbarch_addr_bits_remove for use in arm_get_next_pcs. */
7253
7254CORE_ADDR
7255arm_get_next_pcs_addr_bits_remove (struct arm_get_next_pcs *self,
7256 CORE_ADDR val)
7257{
ac7936df 7258 return gdbarch_addr_bits_remove (self->regcache->arch (), val);
d9311bfa
AT
7259}
7260
7261/* Wrapper over syscall_next_pc for use in get_next_pcs. */
7262
e7cf25a8 7263static CORE_ADDR
553cb527 7264arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self)
d9311bfa 7265{
d9311bfa
AT
7266 return 0;
7267}
7268
7269/* Wrapper over arm_is_thumb for use in arm_get_next_pcs. */
7270
7271int
7272arm_get_next_pcs_is_thumb (struct arm_get_next_pcs *self)
7273{
7274 return arm_is_thumb (self->regcache);
7275}
7276
7277/* single_step() is called just before we want to resume the inferior,
7278 if we want to single-step it but there is no hardware or kernel
7279 single-step support. We find the target of the coming instructions
7280 and breakpoint them. */
7281
a0ff9e1a 7282std::vector<CORE_ADDR>
f5ea389a 7283arm_software_single_step (struct regcache *regcache)
d9311bfa 7284{
ac7936df 7285 struct gdbarch *gdbarch = regcache->arch ();
d9311bfa 7286 struct arm_get_next_pcs next_pcs_ctx;
d9311bfa
AT
7287
7288 arm_get_next_pcs_ctor (&next_pcs_ctx,
7289 &arm_get_next_pcs_ops,
7290 gdbarch_byte_order (gdbarch),
7291 gdbarch_byte_order_for_code (gdbarch),
1b451dda 7292 0,
d9311bfa
AT
7293 regcache);
7294
a0ff9e1a 7295 std::vector<CORE_ADDR> next_pcs = arm_get_next_pcs (&next_pcs_ctx);
d9311bfa 7296
a0ff9e1a
SM
7297 for (CORE_ADDR &pc_ref : next_pcs)
7298 pc_ref = gdbarch_addr_bits_remove (gdbarch, pc_ref);
d9311bfa 7299
93f9a11f 7300 return next_pcs;
d9311bfa
AT
7301}
7302
34518530
YQ
7303/* Cleanup/copy SVC (SWI) instructions. These two functions are overridden
7304 for Linux, where some SVC instructions must be treated specially. */
7305
7306static void
7307cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 7308 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7309{
7310 CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
7311
136821d9
SM
7312 displaced_debug_printf ("cleanup for svc, resume at %.8lx",
7313 (unsigned long) resume_addr);
34518530
YQ
7314
7315 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
7316}
7317
7318
85102364 7319/* Common copy routine for svc instruction. */
34518530
YQ
7320
7321static int
7322install_svc (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 7323 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7324{
7325 /* Preparation: none.
7326 Insn: unmodified svc.
7327 Cleanup: pc <- insn_addr + insn_size. */
7328
7329 /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
7330 instruction. */
7331 dsc->wrote_to_pc = 1;
7332
7333 /* Allow OS-specific code to override SVC handling. */
bd18283a
YQ
7334 if (dsc->u.svc.copy_svc_os)
7335 return dsc->u.svc.copy_svc_os (gdbarch, regs, dsc);
7336 else
7337 {
7338 dsc->cleanup = &cleanup_svc;
7339 return 0;
7340 }
34518530
YQ
7341}
7342
7343static int
7344arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
1152d984 7345 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7346{
7347
136821d9
SM
7348 displaced_debug_printf ("copying svc insn %.8lx",
7349 (unsigned long) insn);
34518530
YQ
7350
7351 dsc->modinsn[0] = insn;
7352
7353 return install_svc (gdbarch, regs, dsc);
7354}
7355
7356static int
7357thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
1152d984 7358 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7359{
7360
136821d9 7361 displaced_debug_printf ("copying svc insn %.4x", insn);
bd18283a 7362
34518530
YQ
7363 dsc->modinsn[0] = insn;
7364
7365 return install_svc (gdbarch, regs, dsc);
cca44b1b
JB
7366}
7367
7368/* Copy undefined instructions. */
7369
7370static int
7ff120b4 7371arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
1152d984 7372 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 7373{
136821d9
SM
7374 displaced_debug_printf ("copying undefined insn %.8lx",
7375 (unsigned long) insn);
cca44b1b
JB
7376
7377 dsc->modinsn[0] = insn;
7378
7379 return 0;
7380}
7381
34518530
YQ
7382static int
7383thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
1152d984 7384 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7385{
7386
136821d9
SM
7387 displaced_debug_printf ("copying undefined insn %.4x %.4x",
7388 (unsigned short) insn1, (unsigned short) insn2);
34518530
YQ
7389
7390 dsc->modinsn[0] = insn1;
7391 dsc->modinsn[1] = insn2;
7392 dsc->numinsns = 2;
7393
7394 return 0;
7395}
7396
cca44b1b
JB
7397/* Copy unpredictable instructions. */
7398
7399static int
7ff120b4 7400arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
1152d984 7401 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 7402{
136821d9
SM
7403 displaced_debug_printf ("copying unpredictable insn %.8lx",
7404 (unsigned long) insn);
cca44b1b
JB
7405
7406 dsc->modinsn[0] = insn;
7407
7408 return 0;
7409}
7410
7411/* The decode_* functions are instruction decoding helpers. They mostly follow
7412 the presentation in the ARM ARM. */
7413
7414static int
7ff120b4
YQ
7415arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
7416 struct regcache *regs,
1152d984 7417 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7418{
7419 unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
7420 unsigned int rn = bits (insn, 16, 19);
7421
2f924de6 7422 if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0x1) == 0x0)
7ff120b4 7423 return arm_copy_unmodified (gdbarch, insn, "cps", dsc);
2f924de6 7424 else if (op1 == 0x10 && op2 == 0x0 && (rn & 0x1) == 0x1)
7ff120b4 7425 return arm_copy_unmodified (gdbarch, insn, "setend", dsc);
cca44b1b 7426 else if ((op1 & 0x60) == 0x20)
7ff120b4 7427 return arm_copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
cca44b1b 7428 else if ((op1 & 0x71) == 0x40)
7ff120b4
YQ
7429 return arm_copy_unmodified (gdbarch, insn, "neon elt/struct load/store",
7430 dsc);
cca44b1b 7431 else if ((op1 & 0x77) == 0x41)
7ff120b4 7432 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 7433 else if ((op1 & 0x77) == 0x45)
7ff120b4 7434 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pli. */
cca44b1b
JB
7435 else if ((op1 & 0x77) == 0x51)
7436 {
7437 if (rn != 0xf)
7ff120b4 7438 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b 7439 else
7ff120b4 7440 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7441 }
7442 else if ((op1 & 0x77) == 0x55)
7ff120b4 7443 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b
JB
7444 else if (op1 == 0x57)
7445 switch (op2)
7446 {
7ff120b4
YQ
7447 case 0x1: return arm_copy_unmodified (gdbarch, insn, "clrex", dsc);
7448 case 0x4: return arm_copy_unmodified (gdbarch, insn, "dsb", dsc);
7449 case 0x5: return arm_copy_unmodified (gdbarch, insn, "dmb", dsc);
7450 case 0x6: return arm_copy_unmodified (gdbarch, insn, "isb", dsc);
7451 default: return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7452 }
7453 else if ((op1 & 0x63) == 0x43)
7ff120b4 7454 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7455 else if ((op2 & 0x1) == 0x0)
7456 switch (op1 & ~0x80)
7457 {
7458 case 0x61:
7ff120b4 7459 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 7460 case 0x65:
7ff120b4 7461 return arm_copy_preload_reg (gdbarch, insn, regs, dsc); /* pli reg. */
cca44b1b 7462 case 0x71: case 0x75:
dda83cd7 7463 /* pld/pldw reg. */
7ff120b4 7464 return arm_copy_preload_reg (gdbarch, insn, regs, dsc);
cca44b1b 7465 case 0x63: case 0x67: case 0x73: case 0x77:
7ff120b4 7466 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b 7467 default:
7ff120b4 7468 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7469 }
7470 else
7ff120b4 7471 return arm_copy_undef (gdbarch, insn, dsc); /* Probably unreachable. */
cca44b1b
JB
7472}
7473
7474static int
7ff120b4
YQ
7475arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
7476 struct regcache *regs,
1152d984 7477 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7478{
7479 if (bit (insn, 27) == 0)
7ff120b4 7480 return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
cca44b1b
JB
7481 /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx. */
7482 else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
7483 {
7484 case 0x0: case 0x2:
7ff120b4 7485 return arm_copy_unmodified (gdbarch, insn, "srs", dsc);
cca44b1b
JB
7486
7487 case 0x1: case 0x3:
7ff120b4 7488 return arm_copy_unmodified (gdbarch, insn, "rfe", dsc);
cca44b1b
JB
7489
7490 case 0x4: case 0x5: case 0x6: case 0x7:
7ff120b4 7491 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b
JB
7492
7493 case 0x8:
7494 switch ((insn & 0xe00000) >> 21)
7495 {
7496 case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
7497 /* stc/stc2. */
7ff120b4 7498 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7499
7500 case 0x2:
7ff120b4 7501 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b
JB
7502
7503 default:
7ff120b4 7504 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7505 }
7506
7507 case 0x9:
7508 {
7509 int rn_f = (bits (insn, 16, 19) == 0xf);
7510 switch ((insn & 0xe00000) >> 21)
7511 {
7512 case 0x1: case 0x3:
7513 /* ldc/ldc2 imm (undefined for rn == pc). */
7ff120b4
YQ
7514 return rn_f ? arm_copy_undef (gdbarch, insn, dsc)
7515 : arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7516
7517 case 0x2:
7ff120b4 7518 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
7519
7520 case 0x4: case 0x5: case 0x6: case 0x7:
7521 /* ldc/ldc2 lit (undefined for rn != pc). */
7ff120b4
YQ
7522 return rn_f ? arm_copy_copro_load_store (gdbarch, insn, regs, dsc)
7523 : arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7524
7525 default:
7ff120b4 7526 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7527 }
7528 }
7529
7530 case 0xa:
7ff120b4 7531 return arm_copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
cca44b1b
JB
7532
7533 case 0xb:
7534 if (bits (insn, 16, 19) == 0xf)
dda83cd7 7535 /* ldc/ldc2 lit. */
7ff120b4 7536 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 7537 else
7ff120b4 7538 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7539
7540 case 0xc:
7541 if (bit (insn, 4))
7ff120b4 7542 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 7543 else
7ff120b4 7544 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7545
7546 case 0xd:
7547 if (bit (insn, 4))
7ff120b4 7548 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 7549 else
7ff120b4 7550 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7551
7552 default:
7ff120b4 7553 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7554 }
7555}
7556
7557/* Decode miscellaneous instructions in dp/misc encoding space. */
7558
7559static int
7ff120b4
YQ
7560arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
7561 struct regcache *regs,
1152d984 7562 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7563{
7564 unsigned int op2 = bits (insn, 4, 6);
7565 unsigned int op = bits (insn, 21, 22);
cca44b1b
JB
7566
7567 switch (op2)
7568 {
7569 case 0x0:
7ff120b4 7570 return arm_copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
cca44b1b
JB
7571
7572 case 0x1:
7573 if (op == 0x1) /* bx. */
7ff120b4 7574 return arm_copy_bx_blx_reg (gdbarch, insn, regs, dsc);
cca44b1b 7575 else if (op == 0x3)
7ff120b4 7576 return arm_copy_unmodified (gdbarch, insn, "clz", dsc);
cca44b1b 7577 else
7ff120b4 7578 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7579
7580 case 0x2:
7581 if (op == 0x1)
dda83cd7 7582 /* Not really supported. */
7ff120b4 7583 return arm_copy_unmodified (gdbarch, insn, "bxj", dsc);
cca44b1b 7584 else
7ff120b4 7585 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7586
7587 case 0x3:
7588 if (op == 0x1)
7ff120b4 7589 return arm_copy_bx_blx_reg (gdbarch, insn,
0963b4bd 7590 regs, dsc); /* blx register. */
cca44b1b 7591 else
7ff120b4 7592 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7593
7594 case 0x5:
7ff120b4 7595 return arm_copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
cca44b1b
JB
7596
7597 case 0x7:
7598 if (op == 0x1)
7ff120b4 7599 return arm_copy_unmodified (gdbarch, insn, "bkpt", dsc);
cca44b1b 7600 else if (op == 0x3)
dda83cd7 7601 /* Not really supported. */
7ff120b4 7602 return arm_copy_unmodified (gdbarch, insn, "smc", dsc);
86a73007 7603 /* Fall through. */
cca44b1b
JB
7604
7605 default:
7ff120b4 7606 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7607 }
7608}
7609
7610static int
7ff120b4
YQ
7611arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
7612 struct regcache *regs,
1152d984 7613 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7614{
7615 if (bit (insn, 25))
7616 switch (bits (insn, 20, 24))
7617 {
7618 case 0x10:
7ff120b4 7619 return arm_copy_unmodified (gdbarch, insn, "movw", dsc);
cca44b1b
JB
7620
7621 case 0x14:
7ff120b4 7622 return arm_copy_unmodified (gdbarch, insn, "movt", dsc);
cca44b1b
JB
7623
7624 case 0x12: case 0x16:
7ff120b4 7625 return arm_copy_unmodified (gdbarch, insn, "msr imm", dsc);
cca44b1b
JB
7626
7627 default:
7ff120b4 7628 return arm_copy_alu_imm (gdbarch, insn, regs, dsc);
cca44b1b
JB
7629 }
7630 else
7631 {
7632 uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
7633
7634 if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
7ff120b4 7635 return arm_copy_alu_reg (gdbarch, insn, regs, dsc);
cca44b1b 7636 else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
7ff120b4 7637 return arm_copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
cca44b1b 7638 else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
7ff120b4 7639 return arm_decode_miscellaneous (gdbarch, insn, regs, dsc);
cca44b1b 7640 else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
7ff120b4 7641 return arm_copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
cca44b1b 7642 else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
7ff120b4 7643 return arm_copy_unmodified (gdbarch, insn, "mul/mla", dsc);
cca44b1b 7644 else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
7ff120b4 7645 return arm_copy_unmodified (gdbarch, insn, "synch", dsc);
cca44b1b 7646 else if (op2 == 0xb || (op2 & 0xd) == 0xd)
550dc4e2 7647 /* 2nd arg means "unprivileged". */
7ff120b4
YQ
7648 return arm_copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
7649 dsc);
cca44b1b
JB
7650 }
7651
7652 /* Should be unreachable. */
7653 return 1;
7654}
7655
7656static int
7ff120b4
YQ
7657arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
7658 struct regcache *regs,
1152d984 7659 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7660{
7661 int a = bit (insn, 25), b = bit (insn, 4);
7662 uint32_t op1 = bits (insn, 20, 24);
cca44b1b
JB
7663
7664 if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
7665 || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
0f6f04ba 7666 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 0);
cca44b1b
JB
7667 else if ((!a && (op1 & 0x17) == 0x02)
7668 || (a && (op1 & 0x17) == 0x02 && !b))
0f6f04ba 7669 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 1);
cca44b1b
JB
7670 else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
7671 || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
0f6f04ba 7672 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 0);
cca44b1b
JB
7673 else if ((!a && (op1 & 0x17) == 0x03)
7674 || (a && (op1 & 0x17) == 0x03 && !b))
0f6f04ba 7675 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 1);
cca44b1b
JB
7676 else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
7677 || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
7ff120b4 7678 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
cca44b1b
JB
7679 else if ((!a && (op1 & 0x17) == 0x06)
7680 || (a && (op1 & 0x17) == 0x06 && !b))
7ff120b4 7681 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
cca44b1b
JB
7682 else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
7683 || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
7ff120b4 7684 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
cca44b1b
JB
7685 else if ((!a && (op1 & 0x17) == 0x07)
7686 || (a && (op1 & 0x17) == 0x07 && !b))
7ff120b4 7687 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
cca44b1b
JB
7688
7689 /* Should be unreachable. */
7690 return 1;
7691}
7692
7693static int
7ff120b4 7694arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
1152d984 7695 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7696{
7697 switch (bits (insn, 20, 24))
7698 {
7699 case 0x00: case 0x01: case 0x02: case 0x03:
7ff120b4 7700 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
cca44b1b
JB
7701
7702 case 0x04: case 0x05: case 0x06: case 0x07:
7ff120b4 7703 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
cca44b1b
JB
7704
7705 case 0x08: case 0x09: case 0x0a: case 0x0b:
7706 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
7ff120b4 7707 return arm_copy_unmodified (gdbarch, insn,
cca44b1b
JB
7708 "decode/pack/unpack/saturate/reverse", dsc);
7709
7710 case 0x18:
7711 if (bits (insn, 5, 7) == 0) /* op2. */
7712 {
7713 if (bits (insn, 12, 15) == 0xf)
7ff120b4 7714 return arm_copy_unmodified (gdbarch, insn, "usad8", dsc);
cca44b1b 7715 else
7ff120b4 7716 return arm_copy_unmodified (gdbarch, insn, "usada8", dsc);
cca44b1b
JB
7717 }
7718 else
7ff120b4 7719 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7720
7721 case 0x1a: case 0x1b:
7722 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 7723 return arm_copy_unmodified (gdbarch, insn, "sbfx", dsc);
cca44b1b 7724 else
7ff120b4 7725 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7726
7727 case 0x1c: case 0x1d:
7728 if (bits (insn, 5, 6) == 0x0) /* op2[1:0]. */
7729 {
7730 if (bits (insn, 0, 3) == 0xf)
7ff120b4 7731 return arm_copy_unmodified (gdbarch, insn, "bfc", dsc);
cca44b1b 7732 else
7ff120b4 7733 return arm_copy_unmodified (gdbarch, insn, "bfi", dsc);
cca44b1b
JB
7734 }
7735 else
7ff120b4 7736 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7737
7738 case 0x1e: case 0x1f:
7739 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 7740 return arm_copy_unmodified (gdbarch, insn, "ubfx", dsc);
cca44b1b 7741 else
7ff120b4 7742 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7743 }
7744
7745 /* Should be unreachable. */
7746 return 1;
7747}
7748
7749static int
615234c1 7750arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, uint32_t insn,
7ff120b4 7751 struct regcache *regs,
1152d984 7752 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7753{
7754 if (bit (insn, 25))
7ff120b4 7755 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b 7756 else
7ff120b4 7757 return arm_copy_block_xfer (gdbarch, insn, regs, dsc);
cca44b1b
JB
7758}
7759
7760static int
7ff120b4
YQ
7761arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
7762 struct regcache *regs,
1152d984 7763 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7764{
7765 unsigned int opcode = bits (insn, 20, 24);
7766
7767 switch (opcode)
7768 {
7769 case 0x04: case 0x05: /* VFP/Neon mrrc/mcrr. */
7ff120b4 7770 return arm_copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
cca44b1b
JB
7771
7772 case 0x08: case 0x0a: case 0x0c: case 0x0e:
7773 case 0x12: case 0x16:
7ff120b4 7774 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
cca44b1b
JB
7775
7776 case 0x09: case 0x0b: case 0x0d: case 0x0f:
7777 case 0x13: case 0x17:
7ff120b4 7778 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
cca44b1b
JB
7779
7780 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7781 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7782 /* Note: no writeback for these instructions. Bit 25 will always be
7783 zero though (via caller), so the following works OK. */
7ff120b4 7784 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7785 }
7786
7787 /* Should be unreachable. */
7788 return 1;
7789}
7790
34518530
YQ
7791/* Decode shifted register instructions. */
7792
7793static int
7794thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
7795 uint16_t insn2, struct regcache *regs,
1152d984 7796 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7797{
7798 /* PC is only allowed to be used in instruction MOV. */
7799
7800 unsigned int op = bits (insn1, 5, 8);
7801 unsigned int rn = bits (insn1, 0, 3);
7802
7803 if (op == 0x2 && rn == 0xf) /* MOV */
7804 return thumb2_copy_alu_imm (gdbarch, insn1, insn2, regs, dsc);
7805 else
7806 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7807 "dp (shift reg)", dsc);
7808}
7809
7810
7811/* Decode extension register load/store. Exactly the same as
7812 arm_decode_ext_reg_ld_st. */
7813
7814static int
7815thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
7816 uint16_t insn2, struct regcache *regs,
1152d984 7817 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7818{
7819 unsigned int opcode = bits (insn1, 4, 8);
7820
7821 switch (opcode)
7822 {
7823 case 0x04: case 0x05:
7824 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7825 "vfp/neon vmov", dsc);
7826
7827 case 0x08: case 0x0c: /* 01x00 */
7828 case 0x0a: case 0x0e: /* 01x10 */
7829 case 0x12: case 0x16: /* 10x10 */
7830 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7831 "vfp/neon vstm/vpush", dsc);
7832
7833 case 0x09: case 0x0d: /* 01x01 */
7834 case 0x0b: case 0x0f: /* 01x11 */
7835 case 0x13: case 0x17: /* 10x11 */
7836 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7837 "vfp/neon vldm/vpop", dsc);
7838
7839 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7840 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7841 "vstr", dsc);
7842 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7843 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2, regs, dsc);
7844 }
7845
7846 /* Should be unreachable. */
7847 return 1;
7848}
7849
cca44b1b 7850static int
12545665 7851arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn,
1152d984 7852 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7853{
7854 unsigned int op1 = bits (insn, 20, 25);
7855 int op = bit (insn, 4);
7856 unsigned int coproc = bits (insn, 8, 11);
cca44b1b
JB
7857
7858 if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
7ff120b4 7859 return arm_decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
cca44b1b
JB
7860 else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
7861 && (coproc & 0xe) != 0xa)
7862 /* stc/stc2. */
7ff120b4 7863 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7864 else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
7865 && (coproc & 0xe) != 0xa)
7866 /* ldc/ldc2 imm/lit. */
7ff120b4 7867 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 7868 else if ((op1 & 0x3e) == 0x00)
7ff120b4 7869 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b 7870 else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
7ff120b4 7871 return arm_copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
cca44b1b 7872 else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
7ff120b4 7873 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b 7874 else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
7ff120b4 7875 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
7876 else if ((op1 & 0x30) == 0x20 && !op)
7877 {
7878 if ((coproc & 0xe) == 0xa)
7ff120b4 7879 return arm_copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
cca44b1b 7880 else
7ff120b4 7881 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7882 }
7883 else if ((op1 & 0x30) == 0x20 && op)
7ff120b4 7884 return arm_copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
cca44b1b 7885 else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
7ff120b4 7886 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 7887 else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
7ff120b4 7888 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 7889 else if ((op1 & 0x30) == 0x30)
7ff120b4 7890 return arm_copy_svc (gdbarch, insn, regs, dsc);
cca44b1b 7891 else
7ff120b4 7892 return arm_copy_undef (gdbarch, insn, dsc); /* Possibly unreachable. */
cca44b1b
JB
7893}
7894
34518530
YQ
7895static int
7896thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
7897 uint16_t insn2, struct regcache *regs,
1152d984 7898 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7899{
7900 unsigned int coproc = bits (insn2, 8, 11);
34518530
YQ
7901 unsigned int bit_5_8 = bits (insn1, 5, 8);
7902 unsigned int bit_9 = bit (insn1, 9);
7903 unsigned int bit_4 = bit (insn1, 4);
34518530
YQ
7904
7905 if (bit_9 == 0)
7906 {
7907 if (bit_5_8 == 2)
7908 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7909 "neon 64bit xfer/mrrc/mrrc2/mcrr/mcrr2",
7910 dsc);
7911 else if (bit_5_8 == 0) /* UNDEFINED. */
7912 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
7913 else
7914 {
7915 /*coproc is 101x. SIMD/VFP, ext registers load/store. */
7916 if ((coproc & 0xe) == 0xa)
7917 return thumb2_decode_ext_reg_ld_st (gdbarch, insn1, insn2, regs,
7918 dsc);
7919 else /* coproc is not 101x. */
7920 {
7921 if (bit_4 == 0) /* STC/STC2. */
7922 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7923 "stc/stc2", dsc);
405feb71 7924 else /* LDC/LDC2 {literal, immediate}. */
34518530
YQ
7925 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2,
7926 regs, dsc);
7927 }
7928 }
7929 }
7930 else
7931 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "coproc", dsc);
7932
7933 return 0;
7934}
7935
7936static void
7937install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 7938 arm_displaced_step_copy_insn_closure *dsc, int rd)
34518530
YQ
7939{
7940 /* ADR Rd, #imm
7941
7942 Rewrite as:
7943
7944 Preparation: Rd <- PC
7945 Insn: ADD Rd, #imm
7946 Cleanup: Null.
7947 */
7948
7949 /* Rd <- PC */
7950 int val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
7951 displaced_write_reg (regs, dsc, rd, val, CANNOT_WRITE_PC);
7952}
7953
7954static int
7955thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 7956 arm_displaced_step_copy_insn_closure *dsc,
34518530
YQ
7957 int rd, unsigned int imm)
7958{
7959
7960 /* Encoding T2: ADDS Rd, #imm */
7961 dsc->modinsn[0] = (0x3000 | (rd << 8) | imm);
7962
7963 install_pc_relative (gdbarch, regs, dsc, rd);
7964
7965 return 0;
7966}
7967
7968static int
7969thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
7970 struct regcache *regs,
1152d984 7971 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7972{
7973 unsigned int rd = bits (insn, 8, 10);
7974 unsigned int imm8 = bits (insn, 0, 7);
7975
136821d9
SM
7976 displaced_debug_printf ("copying thumb adr r%d, #%d insn %.4x",
7977 rd, imm8, insn);
34518530
YQ
7978
7979 return thumb_copy_pc_relative_16bit (gdbarch, regs, dsc, rd, imm8);
7980}
7981
7982static int
7983thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
7984 uint16_t insn2, struct regcache *regs,
1152d984 7985 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7986{
7987 unsigned int rd = bits (insn2, 8, 11);
7988 /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
7989 extract raw immediate encoding rather than computing immediate. When
7990 generating ADD or SUB instruction, we can simply perform OR operation to
7991 set immediate into ADD. */
7992 unsigned int imm_3_8 = insn2 & 0x70ff;
7993 unsigned int imm_i = insn1 & 0x0400; /* Clear all bits except bit 10. */
7994
136821d9
SM
7995 displaced_debug_printf ("copying thumb adr r%d, #%d:%d insn %.4x%.4x",
7996 rd, imm_i, imm_3_8, insn1, insn2);
34518530
YQ
7997
7998 if (bit (insn1, 7)) /* Encoding T2 */
7999 {
8000 /* Encoding T3: SUB Rd, Rd, #imm */
8001 dsc->modinsn[0] = (0xf1a0 | rd | imm_i);
8002 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
8003 }
8004 else /* Encoding T3 */
8005 {
8006 /* Encoding T3: ADD Rd, Rd, #imm */
8007 dsc->modinsn[0] = (0xf100 | rd | imm_i);
8008 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
8009 }
8010 dsc->numinsns = 2;
8011
8012 install_pc_relative (gdbarch, regs, dsc, rd);
8013
8014 return 0;
8015}
8016
8017static int
615234c1 8018thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, uint16_t insn1,
34518530 8019 struct regcache *regs,
1152d984 8020 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
8021{
8022 unsigned int rt = bits (insn1, 8, 10);
8023 unsigned int pc;
8024 int imm8 = (bits (insn1, 0, 7) << 2);
34518530
YQ
8025
8026 /* LDR Rd, #imm8
8027
8028 Rwrite as:
8029
8030 Preparation: tmp0 <- R0, tmp2 <- R2, tmp3 <- R3, R2 <- PC, R3 <- #imm8;
8031
8032 Insn: LDR R0, [R2, R3];
8033 Cleanup: R2 <- tmp2, R3 <- tmp3, Rd <- R0, R0 <- tmp0 */
8034
136821d9 8035 displaced_debug_printf ("copying thumb ldr r%d [pc #%d]", rt, imm8);
34518530
YQ
8036
8037 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
8038 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
8039 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
8040 pc = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
8041 /* The assembler calculates the required value of the offset from the
8042 Align(PC,4) value of this instruction to the label. */
8043 pc = pc & 0xfffffffc;
8044
8045 displaced_write_reg (regs, dsc, 2, pc, CANNOT_WRITE_PC);
8046 displaced_write_reg (regs, dsc, 3, imm8, CANNOT_WRITE_PC);
8047
8048 dsc->rd = rt;
8049 dsc->u.ldst.xfersize = 4;
8050 dsc->u.ldst.rn = 0;
8051 dsc->u.ldst.immed = 0;
8052 dsc->u.ldst.writeback = 0;
8053 dsc->u.ldst.restore_r4 = 0;
8054
8055 dsc->modinsn[0] = 0x58d0; /* ldr r0, [r2, r3]*/
8056
8057 dsc->cleanup = &cleanup_load;
8058
8059 return 0;
8060}
8061
405feb71 8062/* Copy Thumb cbnz/cbz instruction. */
34518530
YQ
8063
8064static int
8065thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
8066 struct regcache *regs,
1152d984 8067 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
8068{
8069 int non_zero = bit (insn1, 11);
8070 unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
8071 CORE_ADDR from = dsc->insn_addr;
8072 int rn = bits (insn1, 0, 2);
8073 int rn_val = displaced_read_reg (regs, dsc, rn);
8074
8075 dsc->u.branch.cond = (rn_val && non_zero) || (!rn_val && !non_zero);
8076 /* CBNZ and CBZ do not affect the condition flags. If condition is true,
8077 set it INST_AL, so cleanup_branch will know branch is taken, otherwise,
8078 condition is false, let it be, cleanup_branch will do nothing. */
8079 if (dsc->u.branch.cond)
8080 {
8081 dsc->u.branch.cond = INST_AL;
8082 dsc->u.branch.dest = from + 4 + imm5;
8083 }
8084 else
8085 dsc->u.branch.dest = from + 2;
8086
8087 dsc->u.branch.link = 0;
8088 dsc->u.branch.exchange = 0;
8089
136821d9
SM
8090 displaced_debug_printf ("copying %s [r%d = 0x%x] insn %.4x to %.8lx",
8091 non_zero ? "cbnz" : "cbz",
8092 rn, rn_val, insn1, dsc->u.branch.dest);
34518530
YQ
8093
8094 dsc->modinsn[0] = THUMB_NOP;
8095
8096 dsc->cleanup = &cleanup_branch;
8097 return 0;
8098}
8099
8100/* Copy Table Branch Byte/Halfword */
8101static int
8102thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
8103 uint16_t insn2, struct regcache *regs,
1152d984 8104 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
8105{
8106 ULONGEST rn_val, rm_val;
8107 int is_tbh = bit (insn2, 4);
8108 CORE_ADDR halfwords = 0;
8109 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8110
8111 rn_val = displaced_read_reg (regs, dsc, bits (insn1, 0, 3));
8112 rm_val = displaced_read_reg (regs, dsc, bits (insn2, 0, 3));
8113
8114 if (is_tbh)
8115 {
8116 gdb_byte buf[2];
8117
8118 target_read_memory (rn_val + 2 * rm_val, buf, 2);
8119 halfwords = extract_unsigned_integer (buf, 2, byte_order);
8120 }
8121 else
8122 {
8123 gdb_byte buf[1];
8124
8125 target_read_memory (rn_val + rm_val, buf, 1);
8126 halfwords = extract_unsigned_integer (buf, 1, byte_order);
8127 }
8128
136821d9
SM
8129 displaced_debug_printf ("%s base 0x%x offset 0x%x offset 0x%x",
8130 is_tbh ? "tbh" : "tbb",
8131 (unsigned int) rn_val, (unsigned int) rm_val,
8132 (unsigned int) halfwords);
34518530
YQ
8133
8134 dsc->u.branch.cond = INST_AL;
8135 dsc->u.branch.link = 0;
8136 dsc->u.branch.exchange = 0;
8137 dsc->u.branch.dest = dsc->insn_addr + 4 + 2 * halfwords;
8138
8139 dsc->cleanup = &cleanup_branch;
8140
8141 return 0;
8142}
8143
8144static void
8145cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 8146 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
8147{
8148 /* PC <- r7 */
8149 int val = displaced_read_reg (regs, dsc, 7);
8150 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, val, BX_WRITE_PC);
8151
8152 /* r7 <- r8 */
8153 val = displaced_read_reg (regs, dsc, 8);
8154 displaced_write_reg (regs, dsc, 7, val, CANNOT_WRITE_PC);
8155
8156 /* r8 <- tmp[0] */
8157 displaced_write_reg (regs, dsc, 8, dsc->tmp[0], CANNOT_WRITE_PC);
8158
8159}
8160
8161static int
615234c1 8162thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, uint16_t insn1,
34518530 8163 struct regcache *regs,
1152d984 8164 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
8165{
8166 dsc->u.block.regmask = insn1 & 0x00ff;
8167
8168 /* Rewrite instruction: POP {rX, rY, ...,rZ, PC}
8169 to :
8170
8171 (1) register list is full, that is, r0-r7 are used.
8172 Prepare: tmp[0] <- r8
8173
8174 POP {r0, r1, ...., r6, r7}; remove PC from reglist
8175 MOV r8, r7; Move value of r7 to r8;
8176 POP {r7}; Store PC value into r7.
8177
8178 Cleanup: PC <- r7, r7 <- r8, r8 <-tmp[0]
8179
8180 (2) register list is not full, supposing there are N registers in
8181 register list (except PC, 0 <= N <= 7).
8182 Prepare: for each i, 0 - N, tmp[i] <- ri.
8183
8184 POP {r0, r1, ...., rN};
8185
8186 Cleanup: Set registers in original reglist from r0 - rN. Restore r0 - rN
8187 from tmp[] properly.
8188 */
136821d9
SM
8189 displaced_debug_printf ("copying thumb pop {%.8x, pc} insn %.4x",
8190 dsc->u.block.regmask, insn1);
34518530
YQ
8191
8192 if (dsc->u.block.regmask == 0xff)
8193 {
8194 dsc->tmp[0] = displaced_read_reg (regs, dsc, 8);
8195
8196 dsc->modinsn[0] = (insn1 & 0xfeff); /* POP {r0,r1,...,r6, r7} */
8197 dsc->modinsn[1] = 0x46b8; /* MOV r8, r7 */
8198 dsc->modinsn[2] = 0xbc80; /* POP {r7} */
8199
8200 dsc->numinsns = 3;
8201 dsc->cleanup = &cleanup_pop_pc_16bit_all;
8202 }
8203 else
8204 {
5f661e03 8205 unsigned int num_in_list = count_one_bits (dsc->u.block.regmask);
bec2ab5a
SM
8206 unsigned int i;
8207 unsigned int new_regmask;
34518530
YQ
8208
8209 for (i = 0; i < num_in_list + 1; i++)
8210 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
8211
8212 new_regmask = (1 << (num_in_list + 1)) - 1;
8213
136821d9
SM
8214 displaced_debug_printf ("POP {..., pc}: original reg list %.4x, "
8215 "modified list %.4x",
8216 (int) dsc->u.block.regmask, new_regmask);
34518530
YQ
8217
8218 dsc->u.block.regmask |= 0x8000;
8219 dsc->u.block.writeback = 0;
8220 dsc->u.block.cond = INST_AL;
8221
8222 dsc->modinsn[0] = (insn1 & ~0x1ff) | (new_regmask & 0xff);
8223
8224 dsc->cleanup = &cleanup_block_load_pc;
8225 }
8226
8227 return 0;
8228}
8229
8230static void
8231thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8232 struct regcache *regs,
1152d984 8233 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
8234{
8235 unsigned short op_bit_12_15 = bits (insn1, 12, 15);
8236 unsigned short op_bit_10_11 = bits (insn1, 10, 11);
8237 int err = 0;
8238
8239 /* 16-bit thumb instructions. */
8240 switch (op_bit_12_15)
8241 {
8242 /* Shift (imme), add, subtract, move and compare. */
8243 case 0: case 1: case 2: case 3:
8244 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8245 "shift/add/sub/mov/cmp",
8246 dsc);
8247 break;
8248 case 4:
8249 switch (op_bit_10_11)
8250 {
8251 case 0: /* Data-processing */
8252 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8253 "data-processing",
8254 dsc);
8255 break;
8256 case 1: /* Special data instructions and branch and exchange. */
8257 {
8258 unsigned short op = bits (insn1, 7, 9);
8259 if (op == 6 || op == 7) /* BX or BLX */
8260 err = thumb_copy_bx_blx_reg (gdbarch, insn1, regs, dsc);
8261 else if (bits (insn1, 6, 7) != 0) /* ADD/MOV/CMP high registers. */
8262 err = thumb_copy_alu_reg (gdbarch, insn1, regs, dsc);
8263 else
8264 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "special data",
8265 dsc);
8266 }
8267 break;
8268 default: /* LDR (literal) */
8269 err = thumb_copy_16bit_ldr_literal (gdbarch, insn1, regs, dsc);
8270 }
8271 break;
8272 case 5: case 6: case 7: case 8: case 9: /* Load/Store single data item */
8273 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldr/str", dsc);
8274 break;
8275 case 10:
8276 if (op_bit_10_11 < 2) /* Generate PC-relative address */
8277 err = thumb_decode_pc_relative_16bit (gdbarch, insn1, regs, dsc);
8278 else /* Generate SP-relative address */
8279 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "sp-relative", dsc);
8280 break;
8281 case 11: /* Misc 16-bit instructions */
8282 {
8283 switch (bits (insn1, 8, 11))
8284 {
8285 case 1: case 3: case 9: case 11: /* CBNZ, CBZ */
8286 err = thumb_copy_cbnz_cbz (gdbarch, insn1, regs, dsc);
8287 break;
8288 case 12: case 13: /* POP */
8289 if (bit (insn1, 8)) /* PC is in register list. */
8290 err = thumb_copy_pop_pc_16bit (gdbarch, insn1, regs, dsc);
8291 else
8292 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "pop", dsc);
8293 break;
8294 case 15: /* If-Then, and hints */
8295 if (bits (insn1, 0, 3))
8296 /* If-Then makes up to four following instructions conditional.
8297 IT instruction itself is not conditional, so handle it as a
8298 common unmodified instruction. */
8299 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "If-Then",
8300 dsc);
8301 else
8302 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "hints", dsc);
8303 break;
8304 default:
8305 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "misc", dsc);
8306 }
8307 }
8308 break;
8309 case 12:
8310 if (op_bit_10_11 < 2) /* Store multiple registers */
8311 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "stm", dsc);
8312 else /* Load multiple registers */
8313 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldm", dsc);
8314 break;
8315 case 13: /* Conditional branch and supervisor call */
8316 if (bits (insn1, 9, 11) != 7) /* conditional branch */
8317 err = thumb_copy_b (gdbarch, insn1, dsc);
8318 else
8319 err = thumb_copy_svc (gdbarch, insn1, regs, dsc);
8320 break;
8321 case 14: /* Unconditional branch */
8322 err = thumb_copy_b (gdbarch, insn1, dsc);
8323 break;
8324 default:
8325 err = 1;
8326 }
8327
8328 if (err)
f34652de 8329 internal_error (_("thumb_process_displaced_16bit_insn: Instruction decode error"));
34518530
YQ
8330}
8331
8332static int
8333decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
8334 uint16_t insn1, uint16_t insn2,
8335 struct regcache *regs,
1152d984 8336 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
8337{
8338 int rt = bits (insn2, 12, 15);
8339 int rn = bits (insn1, 0, 3);
8340 int op1 = bits (insn1, 7, 8);
34518530
YQ
8341
8342 switch (bits (insn1, 5, 6))
8343 {
8344 case 0: /* Load byte and memory hints */
8345 if (rt == 0xf) /* PLD/PLI */
8346 {
8347 if (rn == 0xf)
8348 /* PLD literal or Encoding T3 of PLI(immediate, literal). */
8349 return thumb2_copy_preload (gdbarch, insn1, insn2, regs, dsc);
8350 else
8351 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8352 "pli/pld", dsc);
8353 }
8354 else
8355 {
8356 if (rn == 0xf) /* LDRB/LDRSB (literal) */
8357 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8358 1);
8359 else
8360 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8361 "ldrb{reg, immediate}/ldrbt",
8362 dsc);
8363 }
8364
8365 break;
8366 case 1: /* Load halfword and memory hints. */
8367 if (rt == 0xf) /* PLD{W} and Unalloc memory hint. */
8368 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8369 "pld/unalloc memhint", dsc);
8370 else
8371 {
8372 if (rn == 0xf)
8373 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8374 2);
8375 else
8376 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8377 "ldrh/ldrht", dsc);
8378 }
8379 break;
8380 case 2: /* Load word */
8381 {
8382 int insn2_bit_8_11 = bits (insn2, 8, 11);
8383
8384 if (rn == 0xf)
8385 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc, 4);
8386 else if (op1 == 0x1) /* Encoding T3 */
8387 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs, dsc,
8388 0, 1);
8389 else /* op1 == 0x0 */
8390 {
8391 if (insn2_bit_8_11 == 0xc || (insn2_bit_8_11 & 0x9) == 0x9)
8392 /* LDR (immediate) */
8393 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8394 dsc, bit (insn2, 8), 1);
8395 else if (insn2_bit_8_11 == 0xe) /* LDRT */
8396 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8397 "ldrt", dsc);
8398 else
8399 /* LDR (register) */
8400 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8401 dsc, 0, 0);
8402 }
8403 break;
8404 }
8405 default:
8406 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
8407 break;
8408 }
8409 return 0;
8410}
8411
8412static void
8413thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8414 uint16_t insn2, struct regcache *regs,
1152d984 8415 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
8416{
8417 int err = 0;
8418 unsigned short op = bit (insn2, 15);
8419 unsigned int op1 = bits (insn1, 11, 12);
8420
8421 switch (op1)
8422 {
8423 case 1:
8424 {
8425 switch (bits (insn1, 9, 10))
8426 {
8427 case 0:
8428 if (bit (insn1, 6))
8429 {
405feb71 8430 /* Load/store {dual, exclusive}, table branch. */
34518530
YQ
8431 if (bits (insn1, 7, 8) == 1 && bits (insn1, 4, 5) == 1
8432 && bits (insn2, 5, 7) == 0)
8433 err = thumb2_copy_table_branch (gdbarch, insn1, insn2, regs,
8434 dsc);
8435 else
8436 /* PC is not allowed to use in load/store {dual, exclusive}
8437 instructions. */
8438 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8439 "load/store dual/ex", dsc);
8440 }
8441 else /* load/store multiple */
8442 {
8443 switch (bits (insn1, 7, 8))
8444 {
8445 case 0: case 3: /* SRS, RFE */
8446 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8447 "srs/rfe", dsc);
8448 break;
8449 case 1: case 2: /* LDM/STM/PUSH/POP */
8450 err = thumb2_copy_block_xfer (gdbarch, insn1, insn2, regs, dsc);
8451 break;
8452 }
8453 }
8454 break;
8455
8456 case 1:
8457 /* Data-processing (shift register). */
8458 err = thumb2_decode_dp_shift_reg (gdbarch, insn1, insn2, regs,
8459 dsc);
8460 break;
8461 default: /* Coprocessor instructions. */
8462 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8463 break;
8464 }
8465 break;
8466 }
8467 case 2: /* op1 = 2 */
8468 if (op) /* Branch and misc control. */
8469 {
8470 if (bit (insn2, 14) /* BLX/BL */
8471 || bit (insn2, 12) /* Unconditional branch */
8472 || (bits (insn1, 7, 9) != 0x7)) /* Conditional branch */
8473 err = thumb2_copy_b_bl_blx (gdbarch, insn1, insn2, regs, dsc);
8474 else
8475 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8476 "misc ctrl", dsc);
8477 }
8478 else
8479 {
8480 if (bit (insn1, 9)) /* Data processing (plain binary imm). */
8481 {
b926417a 8482 int dp_op = bits (insn1, 4, 8);
34518530 8483 int rn = bits (insn1, 0, 3);
b926417a 8484 if ((dp_op == 0 || dp_op == 0xa) && rn == 0xf)
34518530
YQ
8485 err = thumb_copy_pc_relative_32bit (gdbarch, insn1, insn2,
8486 regs, dsc);
8487 else
8488 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8489 "dp/pb", dsc);
8490 }
405feb71 8491 else /* Data processing (modified immediate) */
34518530
YQ
8492 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8493 "dp/mi", dsc);
8494 }
8495 break;
8496 case 3: /* op1 = 3 */
8497 switch (bits (insn1, 9, 10))
8498 {
8499 case 0:
8500 if (bit (insn1, 4))
8501 err = decode_thumb_32bit_ld_mem_hints (gdbarch, insn1, insn2,
8502 regs, dsc);
8503 else /* NEON Load/Store and Store single data item */
8504 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8505 "neon elt/struct load/store",
8506 dsc);
8507 break;
8508 case 1: /* op1 = 3, bits (9, 10) == 1 */
8509 switch (bits (insn1, 7, 8))
8510 {
8511 case 0: case 1: /* Data processing (register) */
8512 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8513 "dp(reg)", dsc);
8514 break;
8515 case 2: /* Multiply and absolute difference */
8516 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8517 "mul/mua/diff", dsc);
8518 break;
8519 case 3: /* Long multiply and divide */
8520 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8521 "lmul/lmua", dsc);
8522 break;
8523 }
8524 break;
8525 default: /* Coprocessor instructions */
8526 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8527 break;
8528 }
8529 break;
8530 default:
8531 err = 1;
8532 }
8533
8534 if (err)
f34652de 8535 internal_error (_("thumb_process_displaced_32bit_insn: Instruction decode error"));
34518530
YQ
8536
8537}
8538
b434a28f
YQ
8539static void
8540thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
12545665 8541 struct regcache *regs,
1152d984 8542 arm_displaced_step_copy_insn_closure *dsc)
b434a28f 8543{
34518530
YQ
8544 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8545 uint16_t insn1
8546 = read_memory_unsigned_integer (from, 2, byte_order_for_code);
8547
136821d9
SM
8548 displaced_debug_printf ("process thumb insn %.4x at %.8lx",
8549 insn1, (unsigned long) from);
34518530
YQ
8550
8551 dsc->is_thumb = 1;
8552 dsc->insn_size = thumb_insn_size (insn1);
8553 if (thumb_insn_size (insn1) == 4)
8554 {
8555 uint16_t insn2
8556 = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
8557 thumb_process_displaced_32bit_insn (gdbarch, insn1, insn2, regs, dsc);
8558 }
8559 else
8560 thumb_process_displaced_16bit_insn (gdbarch, insn1, regs, dsc);
b434a28f
YQ
8561}
8562
cca44b1b 8563void
b434a28f
YQ
8564arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8565 CORE_ADDR to, struct regcache *regs,
1152d984 8566 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
8567{
8568 int err = 0;
b434a28f
YQ
8569 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8570 uint32_t insn;
cca44b1b
JB
8571
8572 /* Most displaced instructions use a 1-instruction scratch space, so set this
8573 here and override below if/when necessary. */
8574 dsc->numinsns = 1;
8575 dsc->insn_addr = from;
8576 dsc->scratch_base = to;
8577 dsc->cleanup = NULL;
8578 dsc->wrote_to_pc = 0;
8579
b434a28f 8580 if (!displaced_in_arm_mode (regs))
12545665 8581 return thumb_process_displaced_insn (gdbarch, from, regs, dsc);
b434a28f 8582
4db71c0b
YQ
8583 dsc->is_thumb = 0;
8584 dsc->insn_size = 4;
b434a28f 8585 insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
136821d9
SM
8586 displaced_debug_printf ("stepping insn %.8lx at %.8lx",
8587 (unsigned long) insn, (unsigned long) from);
b434a28f 8588
cca44b1b 8589 if ((insn & 0xf0000000) == 0xf0000000)
7ff120b4 8590 err = arm_decode_unconditional (gdbarch, insn, regs, dsc);
cca44b1b
JB
8591 else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
8592 {
8593 case 0x0: case 0x1: case 0x2: case 0x3:
7ff120b4 8594 err = arm_decode_dp_misc (gdbarch, insn, regs, dsc);
cca44b1b
JB
8595 break;
8596
8597 case 0x4: case 0x5: case 0x6:
7ff120b4 8598 err = arm_decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
cca44b1b
JB
8599 break;
8600
8601 case 0x7:
7ff120b4 8602 err = arm_decode_media (gdbarch, insn, dsc);
cca44b1b
JB
8603 break;
8604
8605 case 0x8: case 0x9: case 0xa: case 0xb:
7ff120b4 8606 err = arm_decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
cca44b1b
JB
8607 break;
8608
8609 case 0xc: case 0xd: case 0xe: case 0xf:
12545665 8610 err = arm_decode_svc_copro (gdbarch, insn, regs, dsc);
cca44b1b
JB
8611 break;
8612 }
8613
8614 if (err)
f34652de 8615 internal_error (_("arm_process_displaced_insn: Instruction decode error"));
cca44b1b
JB
8616}
8617
8618/* Actually set up the scratch space for a displaced instruction. */
8619
8620void
8621arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
1152d984
SM
8622 CORE_ADDR to,
8623 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 8624{
08106042 8625 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
4db71c0b 8626 unsigned int i, len, offset;
cca44b1b 8627 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
05d63baf 8628 int size = dsc->is_thumb ? 2 : 4;
948f8e3d 8629 const gdb_byte *bkp_insn;
cca44b1b 8630
4db71c0b 8631 offset = 0;
cca44b1b
JB
8632 /* Poke modified instruction(s). */
8633 for (i = 0; i < dsc->numinsns; i++)
8634 {
136821d9
SM
8635 if (size == 4)
8636 displaced_debug_printf ("writing insn %.8lx at %.8lx",
8637 dsc->modinsn[i], (unsigned long) to + offset);
8638 else if (size == 2)
8639 displaced_debug_printf ("writing insn %.4x at %.8lx",
8640 (unsigned short) dsc->modinsn[i],
8641 (unsigned long) to + offset);
4db71c0b 8642
4db71c0b
YQ
8643 write_memory_unsigned_integer (to + offset, size,
8644 byte_order_for_code,
cca44b1b 8645 dsc->modinsn[i]);
4db71c0b
YQ
8646 offset += size;
8647 }
8648
8649 /* Choose the correct breakpoint instruction. */
8650 if (dsc->is_thumb)
8651 {
8652 bkp_insn = tdep->thumb_breakpoint;
8653 len = tdep->thumb_breakpoint_size;
8654 }
8655 else
8656 {
8657 bkp_insn = tdep->arm_breakpoint;
8658 len = tdep->arm_breakpoint_size;
cca44b1b
JB
8659 }
8660
8661 /* Put breakpoint afterwards. */
4db71c0b 8662 write_memory (to + offset, bkp_insn, len);
cca44b1b 8663
136821d9
SM
8664 displaced_debug_printf ("copy %s->%s", paddress (gdbarch, from),
8665 paddress (gdbarch, to));
cca44b1b
JB
8666}
8667
cca44b1b
JB
8668/* Entry point for cleaning things up after a displaced instruction has been
8669 single-stepped. */
8670
8671void
8672arm_displaced_step_fixup (struct gdbarch *gdbarch,
1152d984 8673 struct displaced_step_copy_insn_closure *dsc_,
cca44b1b 8674 CORE_ADDR from, CORE_ADDR to,
cf141dd8 8675 struct regcache *regs, bool completed_p)
cca44b1b 8676{
cf141dd8
AB
8677 /* The following block exists as a temporary measure while displaced
8678 stepping is fixed architecture at a time within GDB.
8679
8680 In an earlier implementation of displaced stepping, if GDB thought the
8681 displaced instruction had not been executed then this fix up function
8682 was never called. As a consequence, things that should be fixed by
8683 this function were left in an unfixed state.
8684
8685 However, it's not as simple as always calling this function; this
8686 function needs to be updated to decide what should be fixed up based
8687 on whether the displaced step executed or not, which requires each
8688 architecture to be considered individually.
8689
8690 Until this architecture is updated, this block replicates the old
8691 behaviour; we just restore the program counter register, and leave
8692 everything else unfixed. */
8693 if (!completed_p)
8694 {
8695 CORE_ADDR pc = regcache_read_pc (regs);
8696 pc = from + (pc - to);
8697 regcache_write_pc (regs, pc);
8698 return;
8699 }
8700
1152d984
SM
8701 arm_displaced_step_copy_insn_closure *dsc
8702 = (arm_displaced_step_copy_insn_closure *) dsc_;
cfba9872 8703
cca44b1b
JB
8704 if (dsc->cleanup)
8705 dsc->cleanup (gdbarch, regs, dsc);
8706
8707 if (!dsc->wrote_to_pc)
4db71c0b
YQ
8708 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
8709 dsc->insn_addr + dsc->insn_size);
8710
cca44b1b
JB
8711}
8712
8713#include "bfd-in2.h"
8714#include "libcoff.h"
8715
8716static int
8717gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
8718{
f0c2e3e0
AB
8719 gdb_disassemble_info *di
8720 = static_cast<gdb_disassemble_info *> (info->application_data);
e47ad6c0 8721 struct gdbarch *gdbarch = di->arch ();
9779414d
DJ
8722
8723 if (arm_pc_is_thumb (gdbarch, memaddr))
cca44b1b
JB
8724 {
8725 static asymbol *asym;
8726 static combined_entry_type ce;
8727 static struct coff_symbol_struct csym;
8728 static struct bfd fake_bfd;
8729 static bfd_target fake_target;
8730
8731 if (csym.native == NULL)
8732 {
8733 /* Create a fake symbol vector containing a Thumb symbol.
8734 This is solely so that the code in print_insn_little_arm()
8735 and print_insn_big_arm() in opcodes/arm-dis.c will detect
8736 the presence of a Thumb symbol and switch to decoding
8737 Thumb instructions. */
8738
8739 fake_target.flavour = bfd_target_coff_flavour;
8740 fake_bfd.xvec = &fake_target;
8741 ce.u.syment.n_sclass = C_THUMBEXTFUNC;
8742 csym.native = &ce;
8743 csym.symbol.the_bfd = &fake_bfd;
8744 csym.symbol.name = "fake";
8745 asym = (asymbol *) & csym;
8746 }
8747
8748 memaddr = UNMAKE_THUMB_ADDR (memaddr);
8749 info->symbols = &asym;
8750 }
8751 else
8752 info->symbols = NULL;
8753
e60eb288
YQ
8754 /* GDB is able to get bfd_mach from the exe_bfd, info->mach is
8755 accurate, so mark USER_SPECIFIED_MACHINE_TYPE bit. Otherwise,
8756 opcodes/arm-dis.c:print_insn reset info->mach, and it will trigger
7e10abd1
TT
8757 the assert on the mismatch of info->mach and
8758 bfd_get_mach (current_program_space->exec_bfd ()) in
8759 default_print_insn. */
3047c786
TV
8760 if (current_program_space->exec_bfd () != NULL
8761 && (current_program_space->exec_bfd ()->arch_info
8762 == gdbarch_bfd_arch_info (gdbarch)))
e60eb288
YQ
8763 info->flags |= USER_SPECIFIED_MACHINE_TYPE;
8764
6394c606 8765 return default_print_insn (memaddr, info);
cca44b1b
JB
8766}
8767
8768/* The following define instruction sequences that will cause ARM
8769 cpu's to take an undefined instruction trap. These are used to
8770 signal a breakpoint to GDB.
8771
8772 The newer ARMv4T cpu's are capable of operating in ARM or Thumb
8773 modes. A different instruction is required for each mode. The ARM
8774 cpu's can also be big or little endian. Thus four different
8775 instructions are needed to support all cases.
8776
8777 Note: ARMv4 defines several new instructions that will take the
8778 undefined instruction trap. ARM7TDMI is nominally ARMv4T, but does
8779 not in fact add the new instructions. The new undefined
8780 instructions in ARMv4 are all instructions that had no defined
8781 behaviour in earlier chips. There is no guarantee that they will
8782 raise an exception, but may be treated as NOP's. In practice, it
8783 may only safe to rely on instructions matching:
8784
8785 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
8786 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
8787 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
8788
0963b4bd 8789 Even this may only true if the condition predicate is true. The
cca44b1b
JB
8790 following use a condition predicate of ALWAYS so it is always TRUE.
8791
8792 There are other ways of forcing a breakpoint. GNU/Linux, RISC iX,
8793 and NetBSD all use a software interrupt rather than an undefined
8794 instruction to force a trap. This can be handled by by the
8795 abi-specific code during establishment of the gdbarch vector. */
8796
8797#define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
8798#define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
8799#define THUMB_LE_BREAKPOINT {0xbe,0xbe}
8800#define THUMB_BE_BREAKPOINT {0xbe,0xbe}
8801
948f8e3d
PA
8802static const gdb_byte arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
8803static const gdb_byte arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
8804static const gdb_byte arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
8805static const gdb_byte arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
cca44b1b 8806
cd6c3b4f
YQ
8807/* Implement the breakpoint_kind_from_pc gdbarch method. */
8808
d19280ad
YQ
8809static int
8810arm_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
cca44b1b 8811{
08106042 8812 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
177321bd 8813 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
cca44b1b 8814
9779414d 8815 if (arm_pc_is_thumb (gdbarch, *pcptr))
cca44b1b
JB
8816 {
8817 *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
177321bd
DJ
8818
8819 /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
8820 check whether we are replacing a 32-bit instruction. */
8821 if (tdep->thumb2_breakpoint != NULL)
8822 {
8823 gdb_byte buf[2];
d19280ad 8824
177321bd
DJ
8825 if (target_read_memory (*pcptr, buf, 2) == 0)
8826 {
8827 unsigned short inst1;
d19280ad 8828
177321bd 8829 inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
db24da6d 8830 if (thumb_insn_size (inst1) == 4)
d19280ad 8831 return ARM_BP_KIND_THUMB2;
177321bd
DJ
8832 }
8833 }
8834
d19280ad 8835 return ARM_BP_KIND_THUMB;
cca44b1b
JB
8836 }
8837 else
d19280ad
YQ
8838 return ARM_BP_KIND_ARM;
8839
8840}
8841
cd6c3b4f
YQ
8842/* Implement the sw_breakpoint_from_kind gdbarch method. */
8843
d19280ad
YQ
8844static const gdb_byte *
8845arm_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
8846{
08106042 8847 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
d19280ad
YQ
8848
8849 switch (kind)
cca44b1b 8850 {
d19280ad
YQ
8851 case ARM_BP_KIND_ARM:
8852 *size = tdep->arm_breakpoint_size;
cca44b1b 8853 return tdep->arm_breakpoint;
d19280ad
YQ
8854 case ARM_BP_KIND_THUMB:
8855 *size = tdep->thumb_breakpoint_size;
8856 return tdep->thumb_breakpoint;
8857 case ARM_BP_KIND_THUMB2:
8858 *size = tdep->thumb2_breakpoint_size;
8859 return tdep->thumb2_breakpoint;
8860 default:
8861 gdb_assert_not_reached ("unexpected arm breakpoint kind");
cca44b1b
JB
8862 }
8863}
8864
833b7ab5
YQ
8865/* Implement the breakpoint_kind_from_current_state gdbarch method. */
8866
8867static int
8868arm_breakpoint_kind_from_current_state (struct gdbarch *gdbarch,
8869 struct regcache *regcache,
8870 CORE_ADDR *pcptr)
8871{
8872 gdb_byte buf[4];
8873
8874 /* Check the memory pointed by PC is readable. */
8875 if (target_read_memory (regcache_read_pc (regcache), buf, 4) == 0)
8876 {
8877 struct arm_get_next_pcs next_pcs_ctx;
833b7ab5
YQ
8878
8879 arm_get_next_pcs_ctor (&next_pcs_ctx,
8880 &arm_get_next_pcs_ops,
8881 gdbarch_byte_order (gdbarch),
8882 gdbarch_byte_order_for_code (gdbarch),
8883 0,
8884 regcache);
8885
a0ff9e1a 8886 std::vector<CORE_ADDR> next_pcs = arm_get_next_pcs (&next_pcs_ctx);
833b7ab5
YQ
8887
8888 /* If MEMADDR is the next instruction of current pc, do the
8889 software single step computation, and get the thumb mode by
8890 the destination address. */
a0ff9e1a 8891 for (CORE_ADDR pc : next_pcs)
833b7ab5
YQ
8892 {
8893 if (UNMAKE_THUMB_ADDR (pc) == *pcptr)
8894 {
833b7ab5
YQ
8895 if (IS_THUMB_ADDR (pc))
8896 {
8897 *pcptr = MAKE_THUMB_ADDR (*pcptr);
8898 return arm_breakpoint_kind_from_pc (gdbarch, pcptr);
8899 }
8900 else
8901 return ARM_BP_KIND_ARM;
8902 }
8903 }
833b7ab5
YQ
8904 }
8905
8906 return arm_breakpoint_kind_from_pc (gdbarch, pcptr);
8907}
8908
cca44b1b
JB
8909/* Extract from an array REGBUF containing the (raw) register state a
8910 function return value of type TYPE, and copy that, in virtual
8911 format, into VALBUF. */
8912
8913static void
8914arm_extract_return_value (struct type *type, struct regcache *regs,
8915 gdb_byte *valbuf)
8916{
ac7936df 8917 struct gdbarch *gdbarch = regs->arch ();
cca44b1b 8918 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
08106042 8919 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
cca44b1b 8920
78134374 8921 if (TYPE_CODE_FLT == type->code ())
cca44b1b 8922 {
345bd07c 8923 switch (tdep->fp_model)
cca44b1b
JB
8924 {
8925 case ARM_FLOAT_FPA:
8926 {
8927 /* The value is in register F0 in internal format. We need to
8928 extract the raw value and then convert it to the desired
8929 internal type. */
f0452268 8930 bfd_byte tmpbuf[ARM_FP_REGISTER_SIZE];
cca44b1b 8931
dca08e1f 8932 regs->cooked_read (ARM_F0_REGNUM, tmpbuf);
3b2ca824
UW
8933 target_float_convert (tmpbuf, arm_ext_type (gdbarch),
8934 valbuf, type);
cca44b1b
JB
8935 }
8936 break;
8937
8938 case ARM_FLOAT_SOFT_FPA:
8939 case ARM_FLOAT_SOFT_VFP:
8940 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8941 not using the VFP ABI code. */
8942 case ARM_FLOAT_VFP:
dca08e1f 8943 regs->cooked_read (ARM_A1_REGNUM, valbuf);
df86565b 8944 if (type->length () > 4)
f0452268
AH
8945 regs->cooked_read (ARM_A1_REGNUM + 1,
8946 valbuf + ARM_INT_REGISTER_SIZE);
cca44b1b
JB
8947 break;
8948
8949 default:
f34652de 8950 internal_error (_("arm_extract_return_value: "
0963b4bd 8951 "Floating point model not supported"));
cca44b1b
JB
8952 break;
8953 }
8954 }
78134374
SM
8955 else if (type->code () == TYPE_CODE_INT
8956 || type->code () == TYPE_CODE_CHAR
8957 || type->code () == TYPE_CODE_BOOL
8958 || type->code () == TYPE_CODE_PTR
aa006118 8959 || TYPE_IS_REFERENCE (type)
a6617193
JB
8960 || type->code () == TYPE_CODE_ENUM
8961 || is_fixed_point_type (type))
cca44b1b 8962 {
b021a221
MS
8963 /* If the type is a plain integer, then the access is
8964 straight-forward. Otherwise we have to play around a bit
8965 more. */
df86565b 8966 int len = type->length ();
cca44b1b
JB
8967 int regno = ARM_A1_REGNUM;
8968 ULONGEST tmp;
8969
8970 while (len > 0)
8971 {
8972 /* By using store_unsigned_integer we avoid having to do
8973 anything special for small big-endian values. */
8974 regcache_cooked_read_unsigned (regs, regno++, &tmp);
8975 store_unsigned_integer (valbuf,
f0452268
AH
8976 (len > ARM_INT_REGISTER_SIZE
8977 ? ARM_INT_REGISTER_SIZE : len),
cca44b1b 8978 byte_order, tmp);
f0452268
AH
8979 len -= ARM_INT_REGISTER_SIZE;
8980 valbuf += ARM_INT_REGISTER_SIZE;
cca44b1b
JB
8981 }
8982 }
8983 else
8984 {
8985 /* For a structure or union the behaviour is as if the value had
dda83cd7
SM
8986 been stored to word-aligned memory and then loaded into
8987 registers with 32-bit load instruction(s). */
df86565b 8988 int len = type->length ();
cca44b1b 8989 int regno = ARM_A1_REGNUM;
f0452268 8990 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
cca44b1b
JB
8991
8992 while (len > 0)
8993 {
dca08e1f 8994 regs->cooked_read (regno++, tmpbuf);
cca44b1b 8995 memcpy (valbuf, tmpbuf,
f0452268
AH
8996 len > ARM_INT_REGISTER_SIZE ? ARM_INT_REGISTER_SIZE : len);
8997 len -= ARM_INT_REGISTER_SIZE;
8998 valbuf += ARM_INT_REGISTER_SIZE;
cca44b1b
JB
8999 }
9000 }
9001}
9002
9003
9004/* Will a function return an aggregate type in memory or in a
9005 register? Return 0 if an aggregate type can be returned in a
9006 register, 1 if it must be returned in memory. */
9007
9008static int
9009arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
9010{
cca44b1b
JB
9011 enum type_code code;
9012
f168693b 9013 type = check_typedef (type);
cca44b1b 9014
b13c8ab2
YQ
9015 /* Simple, non-aggregate types (ie not including vectors and
9016 complex) are always returned in a register (or registers). */
78134374 9017 code = type->code ();
b13c8ab2
YQ
9018 if (TYPE_CODE_STRUCT != code && TYPE_CODE_UNION != code
9019 && TYPE_CODE_ARRAY != code && TYPE_CODE_COMPLEX != code)
9020 return 0;
cca44b1b 9021
911627e7
TT
9022 if (TYPE_HAS_DYNAMIC_LENGTH (type))
9023 return 1;
9024
bd63c870 9025 if (TYPE_CODE_ARRAY == code && type->is_vector ())
c4312b19
YQ
9026 {
9027 /* Vector values should be returned using ARM registers if they
9028 are not over 16 bytes. */
df86565b 9029 return (type->length () > 16);
c4312b19
YQ
9030 }
9031
08106042 9032 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
345bd07c 9033 if (tdep->arm_abi != ARM_ABI_APCS)
cca44b1b 9034 {
b13c8ab2
YQ
9035 /* The AAPCS says all aggregates not larger than a word are returned
9036 in a register. */
df86565b 9037 if (type->length () <= ARM_INT_REGISTER_SIZE
bab22d06 9038 && language_pass_by_reference (type).trivially_copyable)
b13c8ab2
YQ
9039 return 0;
9040
cca44b1b
JB
9041 return 1;
9042 }
b13c8ab2
YQ
9043 else
9044 {
9045 int nRc;
cca44b1b 9046
b13c8ab2
YQ
9047 /* All aggregate types that won't fit in a register must be returned
9048 in memory. */
df86565b 9049 if (type->length () > ARM_INT_REGISTER_SIZE
bab22d06 9050 || !language_pass_by_reference (type).trivially_copyable)
b13c8ab2 9051 return 1;
cca44b1b 9052
b13c8ab2
YQ
9053 /* In the ARM ABI, "integer" like aggregate types are returned in
9054 registers. For an aggregate type to be integer like, its size
f0452268 9055 must be less than or equal to ARM_INT_REGISTER_SIZE and the
b13c8ab2
YQ
9056 offset of each addressable subfield must be zero. Note that bit
9057 fields are not addressable, and all addressable subfields of
9058 unions always start at offset zero.
cca44b1b 9059
b13c8ab2
YQ
9060 This function is based on the behaviour of GCC 2.95.1.
9061 See: gcc/arm.c: arm_return_in_memory() for details.
cca44b1b 9062
b13c8ab2
YQ
9063 Note: All versions of GCC before GCC 2.95.2 do not set up the
9064 parameters correctly for a function returning the following
9065 structure: struct { float f;}; This should be returned in memory,
9066 not a register. Richard Earnshaw sent me a patch, but I do not
9067 know of any way to detect if a function like the above has been
9068 compiled with the correct calling convention. */
9069
9070 /* Assume all other aggregate types can be returned in a register.
9071 Run a check for structures, unions and arrays. */
9072 nRc = 0;
67255d04 9073
b13c8ab2
YQ
9074 if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
9075 {
9076 int i;
9077 /* Need to check if this struct/union is "integer" like. For
9078 this to be true, its size must be less than or equal to
f0452268 9079 ARM_INT_REGISTER_SIZE and the offset of each addressable
b13c8ab2
YQ
9080 subfield must be zero. Note that bit fields are not
9081 addressable, and unions always start at offset zero. If any
9082 of the subfields is a floating point type, the struct/union
9083 cannot be an integer type. */
9084
9085 /* For each field in the object, check:
9086 1) Is it FP? --> yes, nRc = 1;
9087 2) Is it addressable (bitpos != 0) and
9088 not packed (bitsize == 0)?
9089 --> yes, nRc = 1
9090 */
9091
1f704f76 9092 for (i = 0; i < type->num_fields (); i++)
67255d04 9093 {
b13c8ab2
YQ
9094 enum type_code field_type_code;
9095
9096 field_type_code
940da03e 9097 = check_typedef (type->field (i).type ())->code ();
b13c8ab2
YQ
9098
9099 /* Is it a floating point type field? */
9100 if (field_type_code == TYPE_CODE_FLT)
67255d04
RE
9101 {
9102 nRc = 1;
9103 break;
9104 }
b13c8ab2
YQ
9105
9106 /* If bitpos != 0, then we have to care about it. */
b610c045 9107 if (type->field (i).loc_bitpos () != 0)
b13c8ab2
YQ
9108 {
9109 /* Bitfields are not addressable. If the field bitsize is
9110 zero, then the field is not packed. Hence it cannot be
9111 a bitfield or any other packed type. */
3757d2d4 9112 if (type->field (i).bitsize () == 0)
b13c8ab2
YQ
9113 {
9114 nRc = 1;
9115 break;
9116 }
9117 }
67255d04
RE
9118 }
9119 }
67255d04 9120
b13c8ab2
YQ
9121 return nRc;
9122 }
67255d04
RE
9123}
9124
34e8f22d
RE
9125/* Write into appropriate registers a function return value of type
9126 TYPE, given in virtual format. */
9127
9128static void
b508a996 9129arm_store_return_value (struct type *type, struct regcache *regs,
5238cf52 9130 const gdb_byte *valbuf)
34e8f22d 9131{
ac7936df 9132 struct gdbarch *gdbarch = regs->arch ();
e17a4113 9133 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
be8626e0 9134
78134374 9135 if (type->code () == TYPE_CODE_FLT)
34e8f22d 9136 {
f0452268 9137 gdb_byte buf[ARM_FP_REGISTER_SIZE];
08106042 9138 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
34e8f22d 9139
345bd07c 9140 switch (tdep->fp_model)
08216dd7
RE
9141 {
9142 case ARM_FLOAT_FPA:
9143
3b2ca824 9144 target_float_convert (valbuf, type, buf, arm_ext_type (gdbarch));
b66f5587 9145 regs->cooked_write (ARM_F0_REGNUM, buf);
08216dd7
RE
9146 break;
9147
fd50bc42 9148 case ARM_FLOAT_SOFT_FPA:
08216dd7 9149 case ARM_FLOAT_SOFT_VFP:
90445bd3
DJ
9150 /* ARM_FLOAT_VFP can arise if this is a variadic function so
9151 not using the VFP ABI code. */
9152 case ARM_FLOAT_VFP:
b66f5587 9153 regs->cooked_write (ARM_A1_REGNUM, valbuf);
df86565b 9154 if (type->length () > 4)
f0452268
AH
9155 regs->cooked_write (ARM_A1_REGNUM + 1,
9156 valbuf + ARM_INT_REGISTER_SIZE);
08216dd7
RE
9157 break;
9158
9159 default:
f34652de 9160 internal_error (_("arm_store_return_value: Floating "
9b20d036 9161 "point model not supported"));
08216dd7
RE
9162 break;
9163 }
34e8f22d 9164 }
78134374
SM
9165 else if (type->code () == TYPE_CODE_INT
9166 || type->code () == TYPE_CODE_CHAR
9167 || type->code () == TYPE_CODE_BOOL
9168 || type->code () == TYPE_CODE_PTR
aa006118 9169 || TYPE_IS_REFERENCE (type)
78134374 9170 || type->code () == TYPE_CODE_ENUM)
b508a996 9171 {
df86565b 9172 if (type->length () <= 4)
b508a996
RE
9173 {
9174 /* Values of one word or less are zero/sign-extended and
9175 returned in r0. */
f0452268 9176 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
b508a996
RE
9177 LONGEST val = unpack_long (type, valbuf);
9178
f0452268 9179 store_signed_integer (tmpbuf, ARM_INT_REGISTER_SIZE, byte_order, val);
b66f5587 9180 regs->cooked_write (ARM_A1_REGNUM, tmpbuf);
b508a996
RE
9181 }
9182 else
9183 {
9184 /* Integral values greater than one word are stored in consecutive
9185 registers starting with r0. This will always be a multiple of
9186 the regiser size. */
df86565b 9187 int len = type->length ();
b508a996
RE
9188 int regno = ARM_A1_REGNUM;
9189
9190 while (len > 0)
9191 {
b66f5587 9192 regs->cooked_write (regno++, valbuf);
f0452268
AH
9193 len -= ARM_INT_REGISTER_SIZE;
9194 valbuf += ARM_INT_REGISTER_SIZE;
b508a996
RE
9195 }
9196 }
9197 }
34e8f22d 9198 else
b508a996
RE
9199 {
9200 /* For a structure or union the behaviour is as if the value had
dda83cd7
SM
9201 been stored to word-aligned memory and then loaded into
9202 registers with 32-bit load instruction(s). */
df86565b 9203 int len = type->length ();
b508a996 9204 int regno = ARM_A1_REGNUM;
f0452268 9205 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
b508a996
RE
9206
9207 while (len > 0)
9208 {
9209 memcpy (tmpbuf, valbuf,
f0452268 9210 len > ARM_INT_REGISTER_SIZE ? ARM_INT_REGISTER_SIZE : len);
b66f5587 9211 regs->cooked_write (regno++, tmpbuf);
f0452268
AH
9212 len -= ARM_INT_REGISTER_SIZE;
9213 valbuf += ARM_INT_REGISTER_SIZE;
b508a996
RE
9214 }
9215 }
34e8f22d
RE
9216}
9217
2af48f68
PB
9218
9219/* Handle function return values. */
9220
9221static enum return_value_convention
6a3a010b 9222arm_return_value (struct gdbarch *gdbarch, struct value *function,
c055b101 9223 struct type *valtype, struct regcache *regcache,
5cb0f2d5 9224 struct value **read_value, const gdb_byte *writebuf)
2af48f68 9225{
08106042 9226 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
d0c97917 9227 struct type *func_type = function ? function->type () : NULL;
90445bd3
DJ
9228 enum arm_vfp_cprc_base_type vfp_base_type;
9229 int vfp_base_count;
9230
9231 if (arm_vfp_abi_for_function (gdbarch, func_type)
9232 && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
9233 {
9234 int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
9235 int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
9236 int i;
911627e7
TT
9237
9238 gdb_byte *readbuf = nullptr;
9239 if (read_value != nullptr)
9240 {
317c3ed9 9241 *read_value = value::allocate (valtype);
bbe912ba 9242 readbuf = (*read_value)->contents_raw ().data ();
911627e7
TT
9243 }
9244
90445bd3
DJ
9245 for (i = 0; i < vfp_base_count; i++)
9246 {
58d6951d
DJ
9247 if (reg_char == 'q')
9248 {
9249 if (writebuf)
9250 arm_neon_quad_write (gdbarch, regcache, i,
9251 writebuf + i * unit_length);
9252
9253 if (readbuf)
9254 arm_neon_quad_read (gdbarch, regcache, i,
9255 readbuf + i * unit_length);
9256 }
9257 else
9258 {
9259 char name_buf[4];
9260 int regnum;
9261
8c042590 9262 xsnprintf (name_buf, sizeof (name_buf), "%c%d", reg_char, i);
58d6951d
DJ
9263 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9264 strlen (name_buf));
9265 if (writebuf)
b66f5587 9266 regcache->cooked_write (regnum, writebuf + i * unit_length);
58d6951d 9267 if (readbuf)
dca08e1f 9268 regcache->cooked_read (regnum, readbuf + i * unit_length);
58d6951d 9269 }
90445bd3
DJ
9270 }
9271 return RETURN_VALUE_REGISTER_CONVENTION;
9272 }
7c00367c 9273
78134374
SM
9274 if (valtype->code () == TYPE_CODE_STRUCT
9275 || valtype->code () == TYPE_CODE_UNION
9276 || valtype->code () == TYPE_CODE_ARRAY)
2af48f68 9277 {
bab22d06
LM
9278 /* From the AAPCS document:
9279
9280 Result return:
9281
9282 A Composite Type larger than 4 bytes, or whose size cannot be
9283 determined statically by both caller and callee, is stored in memory
9284 at an address passed as an extra argument when the function was
9285 called (Parameter Passing, rule A.4). The memory to be used for the
9286 result may be modified at any point during the function call.
9287
9288 Parameter Passing:
9289
9290 A.4: If the subroutine is a function that returns a result in memory,
9291 then the address for the result is placed in r0 and the NCRN is set
9292 to r1. */
7c00367c
MK
9293 if (tdep->struct_return == pcc_struct_return
9294 || arm_return_in_memory (gdbarch, valtype))
bab22d06 9295 {
911627e7 9296 if (read_value != nullptr)
bab22d06
LM
9297 {
9298 CORE_ADDR addr;
9299
9300 regcache->cooked_read (ARM_A1_REGNUM, &addr);
911627e7 9301 *read_value = value_at_non_lval (valtype, addr);
bab22d06
LM
9302 }
9303 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
9304 }
2af48f68 9305 }
78134374 9306 else if (valtype->code () == TYPE_CODE_COMPLEX)
b13c8ab2
YQ
9307 {
9308 if (arm_return_in_memory (gdbarch, valtype))
9309 return RETURN_VALUE_STRUCT_CONVENTION;
9310 }
7052e42c 9311
2af48f68
PB
9312 if (writebuf)
9313 arm_store_return_value (valtype, regcache, writebuf);
9314
911627e7
TT
9315 if (read_value != nullptr)
9316 {
317c3ed9 9317 *read_value = value::allocate (valtype);
bbe912ba 9318 gdb_byte *readbuf = (*read_value)->contents_raw ().data ();
911627e7
TT
9319 arm_extract_return_value (valtype, regcache, readbuf);
9320 }
2af48f68
PB
9321
9322 return RETURN_VALUE_REGISTER_CONVENTION;
9323}
9324
9325
9df628e0 9326static int
bd2b40ac 9327arm_get_longjmp_target (frame_info_ptr frame, CORE_ADDR *pc)
9df628e0 9328{
e17a4113 9329 struct gdbarch *gdbarch = get_frame_arch (frame);
08106042 9330 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
e17a4113 9331 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
9df628e0 9332 CORE_ADDR jb_addr;
f0452268 9333 gdb_byte buf[ARM_INT_REGISTER_SIZE];
9df628e0 9334
60ade65d 9335 jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
9df628e0
RE
9336
9337 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
f0452268 9338 ARM_INT_REGISTER_SIZE))
9df628e0
RE
9339 return 0;
9340
f0452268 9341 *pc = extract_unsigned_integer (buf, ARM_INT_REGISTER_SIZE, byte_order);
9df628e0
RE
9342 return 1;
9343}
40eadf04
SP
9344/* A call to cmse secure entry function "foo" at "a" is modified by
9345 GNU ld as "b".
9346 a) bl xxxx <foo>
9347
9348 <foo>
9349 xxxx:
9350
9351 b) bl yyyy <__acle_se_foo>
9352
9353 section .gnu.sgstubs:
9354 <foo>
9355 yyyy: sg // secure gateway
9356 b.w xxxx <__acle_se_foo> // original_branch_dest
9357
9358 <__acle_se_foo>
9359 xxxx:
9360
9361 When the control at "b", the pc contains "yyyy" (sg address) which is a
9362 trampoline and does not exist in source code. This function returns the
9363 target pc "xxxx". For more details please refer to section 5.4
9364 (Entry functions) and section 3.4.4 (C level development flow of secure code)
9365 of "armv8-m-security-extensions-requirements-on-development-tools-engineering-specification"
9366 document on www.developer.arm.com. */
9367
9368static CORE_ADDR
9369arm_skip_cmse_entry (CORE_ADDR pc, const char *name, struct objfile *objfile)
9370{
9371 int target_len = strlen (name) + strlen ("__acle_se_") + 1;
9372 char *target_name = (char *) alloca (target_len);
9373 xsnprintf (target_name, target_len, "%s%s", "__acle_se_", name);
9374
9375 struct bound_minimal_symbol minsym
9376 = lookup_minimal_symbol (target_name, NULL, objfile);
9377
9378 if (minsym.minsym != nullptr)
4aeddc50 9379 return minsym.value_address ();
40eadf04
SP
9380
9381 return 0;
9382}
9383
9384/* Return true when SEC points to ".gnu.sgstubs" section. */
9385
9386static bool
9387arm_is_sgstubs_section (struct obj_section *sec)
9388{
9389 return (sec != nullptr
9390 && sec->the_bfd_section != nullptr
9391 && sec->the_bfd_section->name != nullptr
9392 && streq (sec->the_bfd_section->name, ".gnu.sgstubs"));
9393}
9df628e0 9394
faa95490
DJ
9395/* Recognize GCC and GNU ld's trampolines. If we are in a trampoline,
9396 return the target PC. Otherwise return 0. */
c906108c
SS
9397
9398CORE_ADDR
bd2b40ac 9399arm_skip_stub (frame_info_ptr frame, CORE_ADDR pc)
c906108c 9400{
2c02bd72 9401 const char *name;
faa95490 9402 int namelen;
c906108c
SS
9403 CORE_ADDR start_addr;
9404
9405 /* Find the starting address and name of the function containing the PC. */
9406 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
80d8d390
YQ
9407 {
9408 /* Trampoline 'bx reg' doesn't belong to any functions. Do the
9409 check here. */
9410 start_addr = arm_skip_bx_reg (frame, pc);
9411 if (start_addr != 0)
9412 return start_addr;
9413
9414 return 0;
9415 }
c906108c 9416
faa95490
DJ
9417 /* If PC is in a Thumb call or return stub, return the address of the
9418 target PC, which is in a register. The thunk functions are called
9419 _call_via_xx, where x is the register name. The possible names
3d8d5e79
DJ
9420 are r0-r9, sl, fp, ip, sp, and lr. ARM RealView has similar
9421 functions, named __ARM_call_via_r[0-7]. */
61012eef
GB
9422 if (startswith (name, "_call_via_")
9423 || startswith (name, "__ARM_call_via_"))
c906108c 9424 {
ed9a39eb 9425 /* Use the name suffix to determine which register contains the
dda83cd7 9426 target PC. */
a121b7c1 9427 static const char *table[15] =
c5aa993b
JM
9428 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
9429 "r8", "r9", "sl", "fp", "ip", "sp", "lr"
9430 };
c906108c 9431 int regno;
faa95490 9432 int offset = strlen (name) - 2;
c906108c
SS
9433
9434 for (regno = 0; regno <= 14; regno++)
faa95490 9435 if (strcmp (&name[offset], table[regno]) == 0)
52f729a7 9436 return get_frame_register_unsigned (frame, regno);
c906108c 9437 }
ed9a39eb 9438
faa95490
DJ
9439 /* GNU ld generates __foo_from_arm or __foo_from_thumb for
9440 non-interworking calls to foo. We could decode the stubs
9441 to find the target but it's easier to use the symbol table. */
9442 namelen = strlen (name);
9443 if (name[0] == '_' && name[1] == '_'
9444 && ((namelen > 2 + strlen ("_from_thumb")
61012eef 9445 && startswith (name + namelen - strlen ("_from_thumb"), "_from_thumb"))
faa95490 9446 || (namelen > 2 + strlen ("_from_arm")
61012eef 9447 && startswith (name + namelen - strlen ("_from_arm"), "_from_arm"))))
faa95490
DJ
9448 {
9449 char *target_name;
9450 int target_len = namelen - 2;
3b7344d5 9451 struct bound_minimal_symbol minsym;
faa95490
DJ
9452 struct objfile *objfile;
9453 struct obj_section *sec;
9454
9455 if (name[namelen - 1] == 'b')
9456 target_len -= strlen ("_from_thumb");
9457 else
9458 target_len -= strlen ("_from_arm");
9459
224c3ddb 9460 target_name = (char *) alloca (target_len + 1);
faa95490
DJ
9461 memcpy (target_name, name + 2, target_len);
9462 target_name[target_len] = '\0';
9463
9464 sec = find_pc_section (pc);
9465 objfile = (sec == NULL) ? NULL : sec->objfile;
9466 minsym = lookup_minimal_symbol (target_name, NULL, objfile);
3b7344d5 9467 if (minsym.minsym != NULL)
4aeddc50 9468 return minsym.value_address ();
faa95490
DJ
9469 else
9470 return 0;
9471 }
9472
40eadf04
SP
9473 struct obj_section *section = find_pc_section (pc);
9474
9475 /* Check whether SECTION points to the ".gnu.sgstubs" section. */
9476 if (arm_is_sgstubs_section (section))
9477 return arm_skip_cmse_entry (pc, name, section->objfile);
9478
c5aa993b 9479 return 0; /* not a stub */
c906108c
SS
9480}
9481
28e97307
DJ
9482static void
9483arm_update_current_architecture (void)
fd50bc42 9484{
28e97307 9485 /* If the current architecture is not ARM, we have nothing to do. */
99d9c3b9
SM
9486 gdbarch *arch = current_inferior ()->arch ();
9487 if (gdbarch_bfd_arch_info (arch)->arch != bfd_arch_arm)
28e97307 9488 return;
fd50bc42 9489
28e97307 9490 /* Update the architecture. */
b447dd03 9491 gdbarch_info info;
28e97307 9492 if (!gdbarch_update_p (info))
f34652de 9493 internal_error (_("could not update architecture"));
fd50bc42
RE
9494}
9495
9496static void
eb4c3f4a 9497set_fp_model_sfunc (const char *args, int from_tty,
fd50bc42
RE
9498 struct cmd_list_element *c)
9499{
570dc176 9500 int fp_model;
fd50bc42
RE
9501
9502 for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
9503 if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
9504 {
aead7601 9505 arm_fp_model = (enum arm_float_model) fp_model;
fd50bc42
RE
9506 break;
9507 }
9508
9509 if (fp_model == ARM_FLOAT_LAST)
f34652de 9510 internal_error (_("Invalid fp model accepted: %s."),
fd50bc42
RE
9511 current_fp_model);
9512
28e97307 9513 arm_update_current_architecture ();
fd50bc42
RE
9514}
9515
9516static void
08546159
AC
9517show_fp_model (struct ui_file *file, int from_tty,
9518 struct cmd_list_element *c, const char *value)
fd50bc42 9519{
99d9c3b9 9520 gdbarch *arch = current_inferior ()->arch ();
28e97307 9521 if (arm_fp_model == ARM_FLOAT_AUTO
99d9c3b9 9522 && gdbarch_bfd_arch_info (arch)->arch == bfd_arch_arm)
6dff2a6f 9523 {
99d9c3b9 9524 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (arch);
6dff2a6f
AB
9525
9526 gdb_printf (file, _("\
28e97307 9527The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
6dff2a6f
AB
9528 fp_model_strings[tdep->fp_model]);
9529 }
28e97307 9530 else
6cb06a8c 9531 gdb_printf (file, _("\
28e97307 9532The current ARM floating point model is \"%s\".\n"),
6cb06a8c 9533 fp_model_strings[arm_fp_model]);
28e97307
DJ
9534}
9535
9536static void
eb4c3f4a 9537arm_set_abi (const char *args, int from_tty,
28e97307
DJ
9538 struct cmd_list_element *c)
9539{
570dc176 9540 int arm_abi;
28e97307
DJ
9541
9542 for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
9543 if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
9544 {
aead7601 9545 arm_abi_global = (enum arm_abi_kind) arm_abi;
28e97307
DJ
9546 break;
9547 }
9548
9549 if (arm_abi == ARM_ABI_LAST)
f34652de 9550 internal_error (_("Invalid ABI accepted: %s."),
28e97307
DJ
9551 arm_abi_string);
9552
9553 arm_update_current_architecture ();
9554}
9555
9556static void
9557arm_show_abi (struct ui_file *file, int from_tty,
9558 struct cmd_list_element *c, const char *value)
9559{
99d9c3b9 9560 gdbarch *arch = current_inferior ()->arch ();
28e97307 9561 if (arm_abi_global == ARM_ABI_AUTO
99d9c3b9 9562 && gdbarch_bfd_arch_info (arch)->arch == bfd_arch_arm)
6dff2a6f 9563 {
99d9c3b9 9564 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (arch);
6dff2a6f
AB
9565
9566 gdb_printf (file, _("\
28e97307 9567The current ARM ABI is \"auto\" (currently \"%s\").\n"),
6dff2a6f
AB
9568 arm_abi_strings[tdep->arm_abi]);
9569 }
28e97307 9570 else
6cb06a8c
TT
9571 gdb_printf (file, _("The current ARM ABI is \"%s\".\n"),
9572 arm_abi_string);
fd50bc42
RE
9573}
9574
0428b8f5
DJ
9575static void
9576arm_show_fallback_mode (struct ui_file *file, int from_tty,
9577 struct cmd_list_element *c, const char *value)
9578{
6cb06a8c
TT
9579 gdb_printf (file,
9580 _("The current execution mode assumed "
9581 "(when symbols are unavailable) is \"%s\".\n"),
9582 arm_fallback_mode_string);
0428b8f5
DJ
9583}
9584
9585static void
9586arm_show_force_mode (struct ui_file *file, int from_tty,
9587 struct cmd_list_element *c, const char *value)
9588{
6cb06a8c
TT
9589 gdb_printf (file,
9590 _("The current execution mode assumed "
9591 "(even when symbols are available) is \"%s\".\n"),
9592 arm_force_mode_string);
0428b8f5
DJ
9593}
9594
ef273377
CL
9595static void
9596arm_show_unwind_secure_frames (struct ui_file *file, int from_tty,
9597 struct cmd_list_element *c, const char *value)
9598{
9599 gdb_printf (file,
9600 _("Usage of non-secure to secure exception stack unwinding is %s.\n"),
9601 arm_unwind_secure_frames ? "on" : "off");
9602}
9603
afd7eef0
RE
9604/* If the user changes the register disassembly style used for info
9605 register and other commands, we have to also switch the style used
9606 in opcodes for disassembly output. This function is run in the "set
9607 arm disassembly" command, and does that. */
bc90b915
FN
9608
9609static void
eb4c3f4a 9610set_disassembly_style_sfunc (const char *args, int from_tty,
65b48a81 9611 struct cmd_list_element *c)
bc90b915 9612{
65b48a81
PB
9613 /* Convert the short style name into the long style name (eg, reg-names-*)
9614 before calling the generic set_disassembler_options() function. */
9615 std::string long_name = std::string ("reg-names-") + disassembly_style;
9616 set_disassembler_options (&long_name[0]);
9617}
9618
9619static void
9620show_disassembly_style_sfunc (struct ui_file *file, int from_tty,
9621 struct cmd_list_element *c, const char *value)
9622{
9623 struct gdbarch *gdbarch = get_current_arch ();
9624 char *options = get_disassembler_options (gdbarch);
9625 const char *style = "";
9626 int len = 0;
f995bbe8 9627 const char *opt;
65b48a81
PB
9628
9629 FOR_EACH_DISASSEMBLER_OPTION (opt, options)
08dedd66 9630 if (startswith (opt, "reg-names-"))
65b48a81
PB
9631 {
9632 style = &opt[strlen ("reg-names-")];
9633 len = strcspn (style, ",");
9634 }
9635
6cb06a8c 9636 gdb_printf (file, "The disassembly style is \"%.*s\".\n", len, style);
bc90b915
FN
9637}
9638\f
966fbf70 9639/* Return the ARM register name corresponding to register I. */
a208b0cb 9640static const char *
d93859e2 9641arm_register_name (struct gdbarch *gdbarch, int i)
966fbf70 9642{
08106042 9643 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d 9644
ecbf5d4f 9645 if (is_s_pseudo (gdbarch, i))
58d6951d 9646 {
ecbf5d4f 9647 static const char *const s_pseudo_names[] = {
58d6951d
DJ
9648 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
9649 "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
9650 "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
9651 "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
9652 };
9653
ecbf5d4f 9654 return s_pseudo_names[i - tdep->s_pseudo_base];
58d6951d
DJ
9655 }
9656
ecbf5d4f 9657 if (is_q_pseudo (gdbarch, i))
58d6951d 9658 {
ecbf5d4f 9659 static const char *const q_pseudo_names[] = {
58d6951d
DJ
9660 "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
9661 "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
9662 };
9663
ecbf5d4f 9664 return q_pseudo_names[i - tdep->q_pseudo_base];
58d6951d
DJ
9665 }
9666
ae66a8f1
SP
9667 if (is_mve_pseudo (gdbarch, i))
9668 return "p0";
9669
a01567f4
LM
9670 /* RA_AUTH_CODE is used for unwinding only. Do not assign it a name. */
9671 if (is_pacbti_pseudo (gdbarch, i))
9672 return "";
9673
ff6f572f
DJ
9674 if (i >= ARRAY_SIZE (arm_register_names))
9675 /* These registers are only supported on targets which supply
9676 an XML description. */
9677 return "";
9678
ecbf5d4f 9679 /* Non-pseudo registers. */
966fbf70
RE
9680 return arm_register_names[i];
9681}
9682
082fc60d
RE
9683/* Test whether the coff symbol specific value corresponds to a Thumb
9684 function. */
9685
9686static int
9687coff_sym_is_thumb (int val)
9688{
f8bf5763
PM
9689 return (val == C_THUMBEXT
9690 || val == C_THUMBSTAT
9691 || val == C_THUMBEXTFUNC
9692 || val == C_THUMBSTATFUNC
9693 || val == C_THUMBLABEL);
082fc60d
RE
9694}
9695
9696/* arm_coff_make_msymbol_special()
9697 arm_elf_make_msymbol_special()
9698
9699 These functions test whether the COFF or ELF symbol corresponds to
9700 an address in thumb code, and set a "special" bit in a minimal
9701 symbol to indicate that it does. */
9702
34e8f22d 9703static void
082fc60d
RE
9704arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
9705{
39d911fc
TP
9706 elf_symbol_type *elfsym = (elf_symbol_type *) sym;
9707
9708 if (ARM_GET_SYM_BRANCH_TYPE (elfsym->internal_elf_sym.st_target_internal)
467d42c4 9709 == ST_BRANCH_TO_THUMB)
082fc60d
RE
9710 MSYMBOL_SET_SPECIAL (msym);
9711}
9712
34e8f22d 9713static void
082fc60d
RE
9714arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
9715{
9716 if (coff_sym_is_thumb (val))
9717 MSYMBOL_SET_SPECIAL (msym);
9718}
9719
60c5725c
DJ
9720static void
9721arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
9722 asymbol *sym)
9723{
9724 const char *name = bfd_asymbol_name (sym);
bd5766ec 9725 struct arm_per_bfd *data;
60c5725c
DJ
9726 struct arm_mapping_symbol new_map_sym;
9727
9728 gdb_assert (name[0] == '$');
9729 if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
9730 return;
9731
98badbfd 9732 data = arm_bfd_data_key.get (objfile->obfd.get ());
60c5725c 9733 if (data == NULL)
98badbfd 9734 data = arm_bfd_data_key.emplace (objfile->obfd.get (),
bd5766ec 9735 objfile->obfd->section_count);
54cc7474 9736 arm_mapping_symbol_vec &map
e6f7f6d1 9737 = data->section_maps[bfd_asymbol_section (sym)->index];
60c5725c
DJ
9738
9739 new_map_sym.value = sym->value;
9740 new_map_sym.type = name[1];
9741
4838e44c
SM
9742 /* Insert at the end, the vector will be sorted on first use. */
9743 map.push_back (new_map_sym);
60c5725c
DJ
9744}
9745
756fe439 9746static void
61a1198a 9747arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
756fe439 9748{
ac7936df 9749 struct gdbarch *gdbarch = regcache->arch ();
61a1198a 9750 regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
756fe439
DJ
9751
9752 /* If necessary, set the T bit. */
9753 if (arm_apcs_32)
9754 {
9779414d 9755 ULONGEST val, t_bit;
61a1198a 9756 regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
9779414d
DJ
9757 t_bit = arm_psr_thumb_bit (gdbarch);
9758 if (arm_pc_is_thumb (gdbarch, pc))
9759 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9760 val | t_bit);
756fe439 9761 else
61a1198a 9762 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9779414d 9763 val & ~t_bit);
756fe439
DJ
9764 }
9765}
123dc839 9766
58d6951d
DJ
9767/* Read the contents of a NEON quad register, by reading from two
9768 double registers. This is used to implement the quad pseudo
9769 registers, and for argument passing in case the quad registers are
9770 missing; vectors are passed in quad registers when using the VFP
9771 ABI, even if a NEON unit is not present. REGNUM is the index of
9772 the quad register, in [0, 15]. */
9773
05d1431c 9774static enum register_status
849d0ba8 9775arm_neon_quad_read (struct gdbarch *gdbarch, readable_regcache *regcache,
58d6951d
DJ
9776 int regnum, gdb_byte *buf)
9777{
9778 char name_buf[4];
9779 gdb_byte reg_buf[8];
9780 int offset, double_regnum;
05d1431c 9781 enum register_status status;
58d6951d 9782
8c042590 9783 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
58d6951d
DJ
9784 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9785 strlen (name_buf));
9786
9787 /* d0 is always the least significant half of q0. */
9788 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9789 offset = 8;
9790 else
9791 offset = 0;
9792
03f50fc8 9793 status = regcache->raw_read (double_regnum, reg_buf);
05d1431c
PA
9794 if (status != REG_VALID)
9795 return status;
58d6951d
DJ
9796 memcpy (buf + offset, reg_buf, 8);
9797
9798 offset = 8 - offset;
03f50fc8 9799 status = regcache->raw_read (double_regnum + 1, reg_buf);
05d1431c
PA
9800 if (status != REG_VALID)
9801 return status;
58d6951d 9802 memcpy (buf + offset, reg_buf, 8);
05d1431c
PA
9803
9804 return REG_VALID;
58d6951d
DJ
9805}
9806
ae66a8f1
SP
9807/* Read the contents of the MVE pseudo register REGNUM and store it
9808 in BUF. */
9809
9810static enum register_status
9811arm_mve_pseudo_read (struct gdbarch *gdbarch, readable_regcache *regcache,
9812 int regnum, gdb_byte *buf)
9813{
08106042 9814 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ae66a8f1
SP
9815
9816 /* P0 is the first 16 bits of VPR. */
9817 return regcache->raw_read_part (tdep->mve_vpr_regnum, 0, 2, buf);
9818}
9819
05d1431c 9820static enum register_status
849d0ba8 9821arm_pseudo_read (struct gdbarch *gdbarch, readable_regcache *regcache,
58d6951d
DJ
9822 int regnum, gdb_byte *buf)
9823{
9824 const int num_regs = gdbarch_num_regs (gdbarch);
9825 char name_buf[4];
9826 gdb_byte reg_buf[8];
9827 int offset, double_regnum;
08106042 9828 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d
DJ
9829
9830 gdb_assert (regnum >= num_regs);
58d6951d 9831
ecbf5d4f
LM
9832 if (is_q_pseudo (gdbarch, regnum))
9833 {
9834 /* Quad-precision register. */
9835 return arm_neon_quad_read (gdbarch, regcache,
9836 regnum - tdep->q_pseudo_base, buf);
9837 }
ae66a8f1
SP
9838 else if (is_mve_pseudo (gdbarch, regnum))
9839 return arm_mve_pseudo_read (gdbarch, regcache, regnum, buf);
58d6951d
DJ
9840 else
9841 {
05d1431c
PA
9842 enum register_status status;
9843
ecbf5d4f 9844 regnum -= tdep->s_pseudo_base;
58d6951d
DJ
9845 /* Single-precision register. */
9846 gdb_assert (regnum < 32);
9847
9848 /* s0 is always the least significant half of d0. */
9849 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9850 offset = (regnum & 1) ? 0 : 4;
9851 else
9852 offset = (regnum & 1) ? 4 : 0;
9853
8c042590 9854 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
58d6951d
DJ
9855 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9856 strlen (name_buf));
9857
03f50fc8 9858 status = regcache->raw_read (double_regnum, reg_buf);
05d1431c
PA
9859 if (status == REG_VALID)
9860 memcpy (buf, reg_buf + offset, 4);
9861 return status;
58d6951d
DJ
9862 }
9863}
9864
9865/* Store the contents of BUF to a NEON quad register, by writing to
9866 two double registers. This is used to implement the quad pseudo
9867 registers, and for argument passing in case the quad registers are
9868 missing; vectors are passed in quad registers when using the VFP
9869 ABI, even if a NEON unit is not present. REGNUM is the index
9870 of the quad register, in [0, 15]. */
9871
9872static void
9873arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
9874 int regnum, const gdb_byte *buf)
9875{
9876 char name_buf[4];
58d6951d
DJ
9877 int offset, double_regnum;
9878
8c042590 9879 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
58d6951d
DJ
9880 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9881 strlen (name_buf));
9882
9883 /* d0 is always the least significant half of q0. */
9884 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9885 offset = 8;
9886 else
9887 offset = 0;
9888
10eaee5f 9889 regcache->raw_write (double_regnum, buf + offset);
58d6951d 9890 offset = 8 - offset;
10eaee5f 9891 regcache->raw_write (double_regnum + 1, buf + offset);
58d6951d
DJ
9892}
9893
ae66a8f1
SP
9894/* Store the contents of BUF to the MVE pseudo register REGNUM. */
9895
9896static void
9897arm_mve_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
9898 int regnum, const gdb_byte *buf)
9899{
08106042 9900 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ae66a8f1
SP
9901
9902 /* P0 is the first 16 bits of VPR. */
9903 regcache->raw_write_part (tdep->mve_vpr_regnum, 0, 2, buf);
9904}
9905
58d6951d
DJ
9906static void
9907arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
9908 int regnum, const gdb_byte *buf)
9909{
9910 const int num_regs = gdbarch_num_regs (gdbarch);
9911 char name_buf[4];
9912 gdb_byte reg_buf[8];
9913 int offset, double_regnum;
08106042 9914 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d
DJ
9915
9916 gdb_assert (regnum >= num_regs);
58d6951d 9917
ecbf5d4f
LM
9918 if (is_q_pseudo (gdbarch, regnum))
9919 {
9920 /* Quad-precision register. */
9921 arm_neon_quad_write (gdbarch, regcache,
9922 regnum - tdep->q_pseudo_base, buf);
9923 }
ae66a8f1
SP
9924 else if (is_mve_pseudo (gdbarch, regnum))
9925 arm_mve_pseudo_write (gdbarch, regcache, regnum, buf);
58d6951d
DJ
9926 else
9927 {
ecbf5d4f 9928 regnum -= tdep->s_pseudo_base;
58d6951d
DJ
9929 /* Single-precision register. */
9930 gdb_assert (regnum < 32);
9931
9932 /* s0 is always the least significant half of d0. */
9933 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9934 offset = (regnum & 1) ? 0 : 4;
9935 else
9936 offset = (regnum & 1) ? 4 : 0;
9937
8c042590 9938 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
58d6951d
DJ
9939 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9940 strlen (name_buf));
9941
0b883586 9942 regcache->raw_read (double_regnum, reg_buf);
58d6951d 9943 memcpy (reg_buf + offset, buf, 4);
10eaee5f 9944 regcache->raw_write (double_regnum, reg_buf);
58d6951d
DJ
9945 }
9946}
9947
123dc839 9948static struct value *
bd2b40ac 9949value_of_arm_user_reg (frame_info_ptr frame, const void *baton)
123dc839 9950{
9a3c8263 9951 const int *reg_p = (const int *) baton;
123dc839
DJ
9952 return value_of_register (*reg_p, frame);
9953}
97e03143 9954\f
70f80edf
JT
9955static enum gdb_osabi
9956arm_elf_osabi_sniffer (bfd *abfd)
97e03143 9957{
2af48f68 9958 unsigned int elfosabi;
70f80edf 9959 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
97e03143 9960
70f80edf 9961 elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
97e03143 9962
28e97307
DJ
9963 if (elfosabi == ELFOSABI_ARM)
9964 /* GNU tools use this value. Check note sections in this case,
9965 as well. */
b35c1d1c
TT
9966 {
9967 for (asection *sect : gdb_bfd_sections (abfd))
9968 generic_elf_osabi_sniff_abi_tag_sections (abfd, sect, &osabi);
9969 }
97e03143 9970
28e97307 9971 /* Anything else will be handled by the generic ELF sniffer. */
70f80edf 9972 return osabi;
97e03143
RE
9973}
9974
54483882
YQ
9975static int
9976arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
dbf5d61b 9977 const struct reggroup *group)
54483882 9978{
2c291032
YQ
9979 /* FPS register's type is INT, but belongs to float_reggroup. Beside
9980 this, FPS register belongs to save_regroup, restore_reggroup, and
9981 all_reggroup, of course. */
54483882 9982 if (regnum == ARM_FPS_REGNUM)
2c291032
YQ
9983 return (group == float_reggroup
9984 || group == save_reggroup
9985 || group == restore_reggroup
9986 || group == all_reggroup);
54483882
YQ
9987 else
9988 return default_register_reggroup_p (gdbarch, regnum, group);
9989}
9990
25f8c692
JL
9991/* For backward-compatibility we allow two 'g' packet lengths with
9992 the remote protocol depending on whether FPA registers are
9993 supplied. M-profile targets do not have FPA registers, but some
9994 stubs already exist in the wild which use a 'g' packet which
9995 supplies them albeit with dummy values. The packet format which
9996 includes FPA registers should be considered deprecated for
9997 M-profile targets. */
9998
9999static void
10000arm_register_g_packet_guesses (struct gdbarch *gdbarch)
10001{
08106042 10002 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
345bd07c
SM
10003
10004 if (tdep->is_m)
25f8c692 10005 {
d105cce5
AH
10006 const target_desc *tdesc;
10007
25f8c692
JL
10008 /* If we know from the executable this is an M-profile target,
10009 cater for remote targets whose register set layout is the
10010 same as the FPA layout. */
d105cce5 10011 tdesc = arm_read_mprofile_description (ARM_M_TYPE_WITH_FPA);
25f8c692 10012 register_remote_g_packet_guess (gdbarch,
350fab54 10013 ARM_CORE_REGS_SIZE + ARM_FP_REGS_SIZE,
d105cce5 10014 tdesc);
25f8c692
JL
10015
10016 /* The regular M-profile layout. */
d105cce5 10017 tdesc = arm_read_mprofile_description (ARM_M_TYPE_M_PROFILE);
350fab54 10018 register_remote_g_packet_guess (gdbarch, ARM_CORE_REGS_SIZE,
d105cce5 10019 tdesc);
3184d3f9
JL
10020
10021 /* M-profile plus M4F VFP. */
d105cce5 10022 tdesc = arm_read_mprofile_description (ARM_M_TYPE_VFP_D16);
3184d3f9 10023 register_remote_g_packet_guess (gdbarch,
350fab54 10024 ARM_CORE_REGS_SIZE + ARM_VFP2_REGS_SIZE,
d105cce5 10025 tdesc);
ae66a8f1
SP
10026 /* M-profile plus MVE. */
10027 tdesc = arm_read_mprofile_description (ARM_M_TYPE_MVE);
10028 register_remote_g_packet_guess (gdbarch, ARM_CORE_REGS_SIZE
10029 + ARM_VFP2_REGS_SIZE
10030 + ARM_INT_REGISTER_SIZE, tdesc);
9074667a
CL
10031
10032 /* M-profile system (stack pointers). */
10033 tdesc = arm_read_mprofile_description (ARM_M_TYPE_SYSTEM);
10034 register_remote_g_packet_guess (gdbarch, 2 * ARM_INT_REGISTER_SIZE, tdesc);
25f8c692
JL
10035 }
10036
10037 /* Otherwise we don't have a useful guess. */
10038}
10039
7eb89530
YQ
10040/* Implement the code_of_frame_writable gdbarch method. */
10041
10042static int
bd2b40ac 10043arm_code_of_frame_writable (struct gdbarch *gdbarch, frame_info_ptr frame)
7eb89530 10044{
08106042 10045 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
345bd07c
SM
10046
10047 if (tdep->is_m && get_frame_type (frame) == SIGTRAMP_FRAME)
7eb89530
YQ
10048 {
10049 /* M-profile exception frames return to some magic PCs, where
10050 isn't writable at all. */
10051 return 0;
10052 }
10053 else
10054 return 1;
10055}
10056
3426ae57
AH
10057/* Implement gdbarch_gnu_triplet_regexp. If the arch name is arm then allow it
10058 to be postfixed by a version (eg armv7hl). */
10059
10060static const char *
10061arm_gnu_triplet_regexp (struct gdbarch *gdbarch)
10062{
10063 if (strcmp (gdbarch_bfd_arch_info (gdbarch)->arch_name, "arm") == 0)
10064 return "arm(v[^- ]*)?";
10065 return gdbarch_bfd_arch_info (gdbarch)->arch_name;
10066}
10067
a01567f4
LM
10068/* Implement the "get_pc_address_flags" gdbarch method. */
10069
10070static std::string
bd2b40ac 10071arm_get_pc_address_flags (frame_info_ptr frame, CORE_ADDR pc)
a01567f4
LM
10072{
10073 if (get_frame_pc_masked (frame))
10074 return "PAC";
10075
10076 return "";
10077}
10078
da3c6d4a
MS
10079/* Initialize the current architecture based on INFO. If possible,
10080 re-use an architecture from ARCHES, which is a list of
10081 architectures already created during this debugging session.
97e03143 10082
da3c6d4a
MS
10083 Called e.g. at program startup, when reading a core file, and when
10084 reading a binary file. */
97e03143 10085
39bbf761
RE
10086static struct gdbarch *
10087arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
10088{
28e97307
DJ
10089 struct gdbarch_list *best_arch;
10090 enum arm_abi_kind arm_abi = arm_abi_global;
10091 enum arm_float_model fp_model = arm_fp_model;
c1e1314d 10092 tdesc_arch_data_up tdesc_data;
7559c217
CB
10093 int i;
10094 bool is_m = false;
ae7e2f45 10095 bool have_sec_ext = false;
7559c217 10096 int vfp_register_count = 0;
ecbf5d4f 10097 bool have_s_pseudos = false, have_q_pseudos = false;
7559c217
CB
10098 bool have_wmmx_registers = false;
10099 bool have_neon = false;
10100 bool have_fpa_registers = true;
9779414d 10101 const struct target_desc *tdesc = info.target_desc;
ae66a8f1
SP
10102 bool have_vfp = false;
10103 bool have_mve = false;
a01567f4 10104 bool have_pacbti = false;
ae66a8f1 10105 int mve_vpr_regnum = -1;
ecbf5d4f 10106 int register_count = ARM_NUM_REGS;
9074667a
CL
10107 bool have_m_profile_msp = false;
10108 int m_profile_msp_regnum = -1;
10109 int m_profile_psp_regnum = -1;
ae7e2f45
CL
10110 int m_profile_msp_ns_regnum = -1;
10111 int m_profile_psp_ns_regnum = -1;
10112 int m_profile_msp_s_regnum = -1;
10113 int m_profile_psp_s_regnum = -1;
92d48a1e 10114 int tls_regnum = 0;
9779414d
DJ
10115
10116 /* If we have an object to base this architecture on, try to determine
10117 its ABI. */
10118
10119 if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
10120 {
10121 int ei_osabi, e_flags;
10122
10123 switch (bfd_get_flavour (info.abfd))
10124 {
9779414d
DJ
10125 case bfd_target_coff_flavour:
10126 /* Assume it's an old APCS-style ABI. */
10127 /* XXX WinCE? */
10128 arm_abi = ARM_ABI_APCS;
10129 break;
10130
10131 case bfd_target_elf_flavour:
10132 ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
10133 e_flags = elf_elfheader (info.abfd)->e_flags;
10134
10135 if (ei_osabi == ELFOSABI_ARM)
10136 {
10137 /* GNU tools used to use this value, but do not for EABI
10138 objects. There's nowhere to tag an EABI version
10139 anyway, so assume APCS. */
10140 arm_abi = ARM_ABI_APCS;
10141 }
d403db27 10142 else if (ei_osabi == ELFOSABI_NONE || ei_osabi == ELFOSABI_GNU)
9779414d
DJ
10143 {
10144 int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
9779414d
DJ
10145
10146 switch (eabi_ver)
10147 {
10148 case EF_ARM_EABI_UNKNOWN:
10149 /* Assume GNU tools. */
10150 arm_abi = ARM_ABI_APCS;
10151 break;
10152
10153 case EF_ARM_EABI_VER4:
10154 case EF_ARM_EABI_VER5:
10155 arm_abi = ARM_ABI_AAPCS;
10156 /* EABI binaries default to VFP float ordering.
10157 They may also contain build attributes that can
10158 be used to identify if the VFP argument-passing
10159 ABI is in use. */
10160 if (fp_model == ARM_FLOAT_AUTO)
10161 {
10162#ifdef HAVE_ELF
10163 switch (bfd_elf_get_obj_attr_int (info.abfd,
10164 OBJ_ATTR_PROC,
10165 Tag_ABI_VFP_args))
10166 {
b35b0298 10167 case AEABI_VFP_args_base:
9779414d
DJ
10168 /* "The user intended FP parameter/result
10169 passing to conform to AAPCS, base
10170 variant". */
10171 fp_model = ARM_FLOAT_SOFT_VFP;
10172 break;
b35b0298 10173 case AEABI_VFP_args_vfp:
9779414d
DJ
10174 /* "The user intended FP parameter/result
10175 passing to conform to AAPCS, VFP
10176 variant". */
10177 fp_model = ARM_FLOAT_VFP;
10178 break;
b35b0298 10179 case AEABI_VFP_args_toolchain:
9779414d
DJ
10180 /* "The user intended FP parameter/result
10181 passing to conform to tool chain-specific
10182 conventions" - we don't know any such
10183 conventions, so leave it as "auto". */
10184 break;
b35b0298 10185 case AEABI_VFP_args_compatible:
5c294fee
TG
10186 /* "Code is compatible with both the base
10187 and VFP variants; the user did not permit
10188 non-variadic functions to pass FP
10189 parameters/results" - leave it as
10190 "auto". */
10191 break;
9779414d
DJ
10192 default:
10193 /* Attribute value not mentioned in the
5c294fee 10194 November 2012 ABI, so leave it as
9779414d
DJ
10195 "auto". */
10196 break;
10197 }
10198#else
10199 fp_model = ARM_FLOAT_SOFT_VFP;
10200#endif
10201 }
10202 break;
10203
10204 default:
10205 /* Leave it as "auto". */
10206 warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
10207 break;
10208 }
10209
10210#ifdef HAVE_ELF
10211 /* Detect M-profile programs. This only works if the
10212 executable file includes build attributes; GCC does
10213 copy them to the executable, but e.g. RealView does
10214 not. */
17cbafdb
SM
10215 int attr_arch
10216 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10217 Tag_CPU_arch);
10218 int attr_profile
10219 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10220 Tag_CPU_arch_profile);
10221
9779414d
DJ
10222 /* GCC specifies the profile for v6-M; RealView only
10223 specifies the profile for architectures starting with
10224 V7 (as opposed to architectures with a tag
10225 numerically greater than TAG_CPU_ARCH_V7). */
10226 if (!tdesc_has_registers (tdesc)
10227 && (attr_arch == TAG_CPU_ARCH_V6_M
10228 || attr_arch == TAG_CPU_ARCH_V6S_M
131a355f
LM
10229 || attr_arch == TAG_CPU_ARCH_V7E_M
10230 || attr_arch == TAG_CPU_ARCH_V8M_BASE
10231 || attr_arch == TAG_CPU_ARCH_V8M_MAIN
ae66a8f1 10232 || attr_arch == TAG_CPU_ARCH_V8_1M_MAIN
9779414d 10233 || attr_profile == 'M'))
7559c217 10234 is_m = true;
a01567f4
LM
10235
10236 /* Look for attributes that indicate support for ARMv8.1-m
10237 PACBTI. */
10238 if (!tdesc_has_registers (tdesc) && is_m)
10239 {
10240 int attr_pac_extension
10241 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10242 Tag_PAC_extension);
10243
10244 int attr_bti_extension
10245 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10246 Tag_BTI_extension);
10247
10248 int attr_pacret_use
10249 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10250 Tag_PACRET_use);
10251
10252 int attr_bti_use
10253 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10254 Tag_BTI_use);
10255
10256 if (attr_pac_extension != 0 || attr_bti_extension != 0
10257 || attr_pacret_use != 0 || attr_bti_use != 0)
10258 have_pacbti = true;
10259 }
9779414d
DJ
10260#endif
10261 }
10262
10263 if (fp_model == ARM_FLOAT_AUTO)
10264 {
9779414d
DJ
10265 switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
10266 {
10267 case 0:
10268 /* Leave it as "auto". Strictly speaking this case
10269 means FPA, but almost nobody uses that now, and
10270 many toolchains fail to set the appropriate bits
10271 for the floating-point model they use. */
10272 break;
10273 case EF_ARM_SOFT_FLOAT:
10274 fp_model = ARM_FLOAT_SOFT_FPA;
10275 break;
10276 case EF_ARM_VFP_FLOAT:
10277 fp_model = ARM_FLOAT_VFP;
10278 break;
10279 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
10280 fp_model = ARM_FLOAT_SOFT_VFP;
10281 break;
10282 }
10283 }
10284
10285 if (e_flags & EF_ARM_BE8)
10286 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
10287
10288 break;
10289
10290 default:
10291 /* Leave it as "auto". */
10292 break;
10293 }
10294 }
123dc839
DJ
10295
10296 /* Check any target description for validity. */
9779414d 10297 if (tdesc_has_registers (tdesc))
123dc839
DJ
10298 {
10299 /* For most registers we require GDB's default names; but also allow
10300 the numeric names for sp / lr / pc, as a convenience. */
10301 static const char *const arm_sp_names[] = { "r13", "sp", NULL };
10302 static const char *const arm_lr_names[] = { "r14", "lr", NULL };
10303 static const char *const arm_pc_names[] = { "r15", "pc", NULL };
10304
10305 const struct tdesc_feature *feature;
58d6951d 10306 int valid_p;
123dc839 10307
9779414d 10308 feature = tdesc_find_feature (tdesc,
123dc839
DJ
10309 "org.gnu.gdb.arm.core");
10310 if (feature == NULL)
9779414d
DJ
10311 {
10312 feature = tdesc_find_feature (tdesc,
10313 "org.gnu.gdb.arm.m-profile");
10314 if (feature == NULL)
10315 return NULL;
10316 else
7559c217 10317 is_m = true;
9779414d 10318 }
123dc839
DJ
10319
10320 tdesc_data = tdesc_data_alloc ();
10321
10322 valid_p = 1;
10323 for (i = 0; i < ARM_SP_REGNUM; i++)
c1e1314d 10324 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
123dc839 10325 arm_register_names[i]);
c1e1314d 10326 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
123dc839
DJ
10327 ARM_SP_REGNUM,
10328 arm_sp_names);
c1e1314d 10329 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
123dc839
DJ
10330 ARM_LR_REGNUM,
10331 arm_lr_names);
c1e1314d 10332 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
123dc839
DJ
10333 ARM_PC_REGNUM,
10334 arm_pc_names);
9779414d 10335 if (is_m)
c1e1314d 10336 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
9779414d
DJ
10337 ARM_PS_REGNUM, "xpsr");
10338 else
c1e1314d 10339 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
9779414d 10340 ARM_PS_REGNUM, "cpsr");
123dc839
DJ
10341
10342 if (!valid_p)
c1e1314d 10343 return NULL;
123dc839 10344
9074667a
CL
10345 if (is_m)
10346 {
10347 feature = tdesc_find_feature (tdesc,
10348 "org.gnu.gdb.arm.m-system");
10349 if (feature != nullptr)
10350 {
10351 /* MSP */
10352 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10353 register_count, "msp");
10354 if (!valid_p)
10355 {
10356 warning (_("M-profile m-system feature is missing required register msp."));
10357 return nullptr;
10358 }
10359 have_m_profile_msp = true;
10360 m_profile_msp_regnum = register_count++;
10361
10362 /* PSP */
10363 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10364 register_count, "psp");
10365 if (!valid_p)
10366 {
10367 warning (_("M-profile m-system feature is missing required register psp."));
10368 return nullptr;
10369 }
10370 m_profile_psp_regnum = register_count++;
10371 }
10372 }
10373
9779414d 10374 feature = tdesc_find_feature (tdesc,
123dc839
DJ
10375 "org.gnu.gdb.arm.fpa");
10376 if (feature != NULL)
10377 {
10378 valid_p = 1;
10379 for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
c1e1314d 10380 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
123dc839
DJ
10381 arm_register_names[i]);
10382 if (!valid_p)
c1e1314d 10383 return NULL;
123dc839 10384 }
ff6f572f 10385 else
7559c217 10386 have_fpa_registers = false;
ff6f572f 10387
9779414d 10388 feature = tdesc_find_feature (tdesc,
ff6f572f
DJ
10389 "org.gnu.gdb.xscale.iwmmxt");
10390 if (feature != NULL)
10391 {
10392 static const char *const iwmmxt_names[] = {
10393 "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
10394 "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
10395 "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
10396 "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
10397 };
10398
10399 valid_p = 1;
10400 for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
10401 valid_p
c1e1314d 10402 &= tdesc_numbered_register (feature, tdesc_data.get (), i,
ff6f572f
DJ
10403 iwmmxt_names[i - ARM_WR0_REGNUM]);
10404
10405 /* Check for the control registers, but do not fail if they
10406 are missing. */
10407 for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
c1e1314d 10408 tdesc_numbered_register (feature, tdesc_data.get (), i,
ff6f572f
DJ
10409 iwmmxt_names[i - ARM_WR0_REGNUM]);
10410
10411 for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
10412 valid_p
c1e1314d 10413 &= tdesc_numbered_register (feature, tdesc_data.get (), i,
ff6f572f
DJ
10414 iwmmxt_names[i - ARM_WR0_REGNUM]);
10415
10416 if (!valid_p)
c1e1314d 10417 return NULL;
a56cc1ce 10418
7559c217 10419 have_wmmx_registers = true;
ff6f572f 10420 }
58d6951d
DJ
10421
10422 /* If we have a VFP unit, check whether the single precision registers
10423 are present. If not, then we will synthesize them as pseudo
10424 registers. */
9779414d 10425 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
10426 "org.gnu.gdb.arm.vfp");
10427 if (feature != NULL)
10428 {
10429 static const char *const vfp_double_names[] = {
10430 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
10431 "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
10432 "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
10433 "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
10434 };
10435
10436 /* Require the double precision registers. There must be either
10437 16 or 32. */
10438 valid_p = 1;
10439 for (i = 0; i < 32; i++)
10440 {
c1e1314d 10441 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
58d6951d
DJ
10442 ARM_D0_REGNUM + i,
10443 vfp_double_names[i]);
10444 if (!valid_p)
10445 break;
10446 }
2b9e5ea6
UW
10447 if (!valid_p && i == 16)
10448 valid_p = 1;
58d6951d 10449
2b9e5ea6 10450 /* Also require FPSCR. */
c1e1314d 10451 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
2b9e5ea6
UW
10452 ARM_FPSCR_REGNUM, "fpscr");
10453 if (!valid_p)
c1e1314d 10454 return NULL;
58d6951d 10455
ae66a8f1
SP
10456 have_vfp = true;
10457
58d6951d 10458 if (tdesc_unnumbered_register (feature, "s0") == 0)
ecbf5d4f 10459 have_s_pseudos = true;
58d6951d 10460
330c6ca9 10461 vfp_register_count = i;
58d6951d
DJ
10462
10463 /* If we have VFP, also check for NEON. The architecture allows
10464 NEON without VFP (integer vector operations only), but GDB
10465 does not support that. */
9779414d 10466 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
10467 "org.gnu.gdb.arm.neon");
10468 if (feature != NULL)
10469 {
10470 /* NEON requires 32 double-precision registers. */
10471 if (i != 32)
c1e1314d 10472 return NULL;
58d6951d
DJ
10473
10474 /* If there are quad registers defined by the stub, use
10475 their type; otherwise (normally) provide them with
10476 the default type. */
10477 if (tdesc_unnumbered_register (feature, "q0") == 0)
ecbf5d4f 10478 have_q_pseudos = true;
ae66a8f1
SP
10479 }
10480 }
10481
92d48a1e
JB
10482 /* Check for the TLS register feature. */
10483 feature = tdesc_find_feature (tdesc, "org.gnu.gdb.arm.tls");
10484 if (feature != nullptr)
10485 {
10486 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10487 register_count, "tpidruro");
10488 if (!valid_p)
10489 return nullptr;
10490
10491 tls_regnum = register_count;
10492 register_count++;
10493 }
10494
ae66a8f1
SP
10495 /* Check for MVE after all the checks for GPR's, VFP and Neon.
10496 MVE (Helium) is an M-profile extension. */
10497 if (is_m)
10498 {
10499 /* Do we have the MVE feature? */
10500 feature = tdesc_find_feature (tdesc,"org.gnu.gdb.arm.m-profile-mve");
10501
10502 if (feature != nullptr)
10503 {
10504 /* If we have MVE, we must always have the VPR register. */
10505 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10506 register_count, "vpr");
10507 if (!valid_p)
10508 {
10509 warning (_("MVE feature is missing required register vpr."));
10510 return nullptr;
10511 }
58d6951d 10512
ae66a8f1
SP
10513 have_mve = true;
10514 mve_vpr_regnum = register_count;
10515 register_count++;
10516
10517 /* We can't have Q pseudo registers available here, as that
10518 would mean we have NEON features, and that is only available
10519 on A and R profiles. */
10520 gdb_assert (!have_q_pseudos);
10521
10522 /* Given we have a M-profile target description, if MVE is
10523 enabled and there are VFP registers, we should have Q
10524 pseudo registers (Q0 ~ Q7). */
10525 if (have_vfp)
10526 have_q_pseudos = true;
58d6951d 10527 }
a01567f4
LM
10528
10529 /* Do we have the ARMv8.1-m PACBTI feature? */
10530 feature = tdesc_find_feature (tdesc,
10531 "org.gnu.gdb.arm.m-profile-pacbti");
10532 if (feature != nullptr)
10533 {
10534 /* By advertising this feature, the target acknowledges the
10535 presence of the ARMv8.1-m PACBTI extensions.
10536
10537 We don't care for any particular registers in this group, so
10538 the target is free to include whatever it deems appropriate.
10539
10540 The expectation is for this feature to include the PAC
10541 keys. */
10542 have_pacbti = true;
10543 }
ae7e2f45
CL
10544
10545 /* Do we have the Security extension? */
10546 feature = tdesc_find_feature (tdesc,
10547 "org.gnu.gdb.arm.secext");
10548 if (feature != nullptr)
10549 {
10550 /* Secure/Non-secure stack pointers. */
10551 /* MSP_NS */
10552 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10553 register_count, "msp_ns");
10554 if (!valid_p)
10555 {
10556 warning (_("M-profile secext feature is missing required register msp_ns."));
10557 return nullptr;
10558 }
10559 m_profile_msp_ns_regnum = register_count++;
10560
10561 /* PSP_NS */
10562 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10563 register_count, "psp_ns");
10564 if (!valid_p)
10565 {
10566 warning (_("M-profile secext feature is missing required register psp_ns."));
10567 return nullptr;
10568 }
10569 m_profile_psp_ns_regnum = register_count++;
10570
10571 /* MSP_S */
10572 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10573 register_count, "msp_s");
10574 if (!valid_p)
10575 {
10576 warning (_("M-profile secext feature is missing required register msp_s."));
10577 return nullptr;
10578 }
10579 m_profile_msp_s_regnum = register_count++;
10580
10581 /* PSP_S */
10582 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10583 register_count, "psp_s");
10584 if (!valid_p)
10585 {
10586 warning (_("M-profile secext feature is missing required register psp_s."));
10587 return nullptr;
10588 }
10589 m_profile_psp_s_regnum = register_count++;
10590
10591 have_sec_ext = true;
10592 }
10593
58d6951d 10594 }
123dc839 10595 }
39bbf761 10596
28e97307
DJ
10597 /* If there is already a candidate, use it. */
10598 for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
10599 best_arch != NULL;
10600 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
10601 {
345bd07c 10602 arm_gdbarch_tdep *tdep
08106042 10603 = gdbarch_tdep<arm_gdbarch_tdep> (best_arch->gdbarch);
345bd07c
SM
10604
10605 if (arm_abi != ARM_ABI_AUTO && arm_abi != tdep->arm_abi)
28e97307
DJ
10606 continue;
10607
345bd07c 10608 if (fp_model != ARM_FLOAT_AUTO && fp_model != tdep->fp_model)
28e97307
DJ
10609 continue;
10610
58d6951d
DJ
10611 /* There are various other properties in tdep that we do not
10612 need to check here: those derived from a target description,
10613 since gdbarches with a different target description are
10614 automatically disqualified. */
10615
9779414d 10616 /* Do check is_m, though, since it might come from the binary. */
345bd07c 10617 if (is_m != tdep->is_m)
9779414d
DJ
10618 continue;
10619
a01567f4
LM
10620 /* Also check for ARMv8.1-m PACBTI support, since it might come from
10621 the binary. */
10622 if (have_pacbti != tdep->have_pacbti)
10623 continue;
10624
28e97307
DJ
10625 /* Found a match. */
10626 break;
10627 }
97e03143 10628
28e97307 10629 if (best_arch != NULL)
c1e1314d 10630 return best_arch->gdbarch;
28e97307 10631
2b16913c
SM
10632 gdbarch *gdbarch
10633 = gdbarch_alloc (&info, gdbarch_tdep_up (new arm_gdbarch_tdep));
10634 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
97e03143 10635
28e97307
DJ
10636 /* Record additional information about the architecture we are defining.
10637 These are gdbarch discriminators, like the OSABI. */
10638 tdep->arm_abi = arm_abi;
10639 tdep->fp_model = fp_model;
9779414d 10640 tdep->is_m = is_m;
ae7e2f45 10641 tdep->have_sec_ext = have_sec_ext;
ff6f572f 10642 tdep->have_fpa_registers = have_fpa_registers;
a56cc1ce 10643 tdep->have_wmmx_registers = have_wmmx_registers;
330c6ca9
YQ
10644 gdb_assert (vfp_register_count == 0
10645 || vfp_register_count == 16
10646 || vfp_register_count == 32);
10647 tdep->vfp_register_count = vfp_register_count;
ecbf5d4f
LM
10648 tdep->have_s_pseudos = have_s_pseudos;
10649 tdep->have_q_pseudos = have_q_pseudos;
58d6951d 10650 tdep->have_neon = have_neon;
92d48a1e 10651 tdep->tls_regnum = tls_regnum;
08216dd7 10652
ae66a8f1
SP
10653 /* Adjust the MVE feature settings. */
10654 if (have_mve)
10655 {
10656 tdep->have_mve = true;
10657 tdep->mve_vpr_regnum = mve_vpr_regnum;
10658 }
10659
a01567f4
LM
10660 /* Adjust the PACBTI feature settings. */
10661 tdep->have_pacbti = have_pacbti;
10662
9074667a
CL
10663 /* Adjust the M-profile stack pointers settings. */
10664 if (have_m_profile_msp)
10665 {
10666 tdep->m_profile_msp_regnum = m_profile_msp_regnum;
10667 tdep->m_profile_psp_regnum = m_profile_psp_regnum;
ae7e2f45
CL
10668 tdep->m_profile_msp_ns_regnum = m_profile_msp_ns_regnum;
10669 tdep->m_profile_psp_ns_regnum = m_profile_psp_ns_regnum;
10670 tdep->m_profile_msp_s_regnum = m_profile_msp_s_regnum;
10671 tdep->m_profile_psp_s_regnum = m_profile_psp_s_regnum;
9074667a
CL
10672 }
10673
25f8c692
JL
10674 arm_register_g_packet_guesses (gdbarch);
10675
08216dd7 10676 /* Breakpoints. */
9d4fde75 10677 switch (info.byte_order_for_code)
67255d04
RE
10678 {
10679 case BFD_ENDIAN_BIG:
66e810cd
RE
10680 tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
10681 tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
10682 tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
10683 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
10684
67255d04
RE
10685 break;
10686
10687 case BFD_ENDIAN_LITTLE:
66e810cd
RE
10688 tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
10689 tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
10690 tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
10691 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
10692
67255d04
RE
10693 break;
10694
10695 default:
f34652de 10696 internal_error (_("arm_gdbarch_init: bad byte order for float format"));
67255d04
RE
10697 }
10698
d7b486e7
RE
10699 /* On ARM targets char defaults to unsigned. */
10700 set_gdbarch_char_signed (gdbarch, 0);
10701
53375380
PA
10702 /* wchar_t is unsigned under the AAPCS. */
10703 if (tdep->arm_abi == ARM_ABI_AAPCS)
10704 set_gdbarch_wchar_signed (gdbarch, 0);
10705 else
10706 set_gdbarch_wchar_signed (gdbarch, 1);
53375380 10707
030197b4
AB
10708 /* Compute type alignment. */
10709 set_gdbarch_type_align (gdbarch, arm_type_align);
10710
cca44b1b
JB
10711 /* Note: for displaced stepping, this includes the breakpoint, and one word
10712 of additional scratch space. This setting isn't used for anything beside
10713 displaced stepping at present. */
deb65a3c
AB
10714 set_gdbarch_displaced_step_buffer_length
10715 (gdbarch, 4 * ARM_DISPLACED_MODIFIED_INSNS);
10716 set_gdbarch_max_insn_length (gdbarch, 4);
cca44b1b 10717
9df628e0 10718 /* This should be low enough for everything. */
97e03143 10719 tdep->lowest_pc = 0x20;
94c30b78 10720 tdep->jb_pc = -1; /* Longjump support not enabled by default. */
97e03143 10721
7c00367c
MK
10722 /* The default, for both APCS and AAPCS, is to return small
10723 structures in registers. */
10724 tdep->struct_return = reg_struct_return;
10725
2dd604e7 10726 set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
f53f0d0b 10727 set_gdbarch_frame_align (gdbarch, arm_frame_align);
39bbf761 10728
7eb89530
YQ
10729 if (is_m)
10730 set_gdbarch_code_of_frame_writable (gdbarch, arm_code_of_frame_writable);
10731
756fe439
DJ
10732 set_gdbarch_write_pc (gdbarch, arm_write_pc);
10733
eb5492fa 10734 frame_base_set_default (gdbarch, &arm_normal_base);
148754e5 10735
34e8f22d 10736 /* Address manipulation. */
34e8f22d
RE
10737 set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
10738
34e8f22d
RE
10739 /* Advance PC across function entry code. */
10740 set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
10741
c9cf6e20
MG
10742 /* Detect whether PC is at a point where the stack has been destroyed. */
10743 set_gdbarch_stack_frame_destroyed_p (gdbarch, arm_stack_frame_destroyed_p);
4024ca99 10744
190dce09
UW
10745 /* Skip trampolines. */
10746 set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
10747
34e8f22d
RE
10748 /* The stack grows downward. */
10749 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
10750
10751 /* Breakpoint manipulation. */
04180708
YQ
10752 set_gdbarch_breakpoint_kind_from_pc (gdbarch, arm_breakpoint_kind_from_pc);
10753 set_gdbarch_sw_breakpoint_from_kind (gdbarch, arm_sw_breakpoint_from_kind);
833b7ab5
YQ
10754 set_gdbarch_breakpoint_kind_from_current_state (gdbarch,
10755 arm_breakpoint_kind_from_current_state);
34e8f22d
RE
10756
10757 /* Information about registers, etc. */
34e8f22d
RE
10758 set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
10759 set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
ecbf5d4f 10760 set_gdbarch_num_regs (gdbarch, register_count);
7a5ea0d4 10761 set_gdbarch_register_type (gdbarch, arm_register_type);
54483882 10762 set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);
34e8f22d 10763
ff6f572f
DJ
10764 /* This "info float" is FPA-specific. Use the generic version if we
10765 do not have FPA. */
345bd07c 10766 if (tdep->have_fpa_registers)
ff6f572f
DJ
10767 set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
10768
26216b98 10769 /* Internal <-> external register number maps. */
ff6f572f 10770 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
26216b98
AC
10771 set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
10772
34e8f22d
RE
10773 set_gdbarch_register_name (gdbarch, arm_register_name);
10774
10775 /* Returning results. */
5cb0f2d5 10776 set_gdbarch_return_value_as_value (gdbarch, arm_return_value);
34e8f22d 10777
03d48a7d
RE
10778 /* Disassembly. */
10779 set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
10780
34e8f22d
RE
10781 /* Minsymbol frobbing. */
10782 set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
10783 set_gdbarch_coff_make_msymbol_special (gdbarch,
10784 arm_coff_make_msymbol_special);
60c5725c 10785 set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
34e8f22d 10786
f9d67f43
DJ
10787 /* Thumb-2 IT block support. */
10788 set_gdbarch_adjust_breakpoint_address (gdbarch,
10789 arm_adjust_breakpoint_address);
10790
0d5de010
DJ
10791 /* Virtual tables. */
10792 set_gdbarch_vbit_in_delta (gdbarch, 1);
10793
97e03143 10794 /* Hook in the ABI-specific overrides, if they have been registered. */
4be87837 10795 gdbarch_init_osabi (info, gdbarch);
97e03143 10796
b39cc962
DJ
10797 dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
10798
eb5492fa 10799 /* Add some default predicates. */
2ae28aa9
YQ
10800 if (is_m)
10801 frame_unwind_append_unwinder (gdbarch, &arm_m_exception_unwind);
a262aec2
DJ
10802 frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
10803 dwarf2_append_unwinders (gdbarch);
0e9e9abd 10804 frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
779aa56f 10805 frame_unwind_append_unwinder (gdbarch, &arm_epilogue_frame_unwind);
a262aec2 10806 frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
eb5492fa 10807
97e03143
RE
10808 /* Now we have tuned the configuration, set a few final things,
10809 based on what the OS ABI has told us. */
10810
b8926edc
DJ
10811 /* If the ABI is not otherwise marked, assume the old GNU APCS. EABI
10812 binaries are always marked. */
10813 if (tdep->arm_abi == ARM_ABI_AUTO)
10814 tdep->arm_abi = ARM_ABI_APCS;
10815
e3039479
UW
10816 /* Watchpoints are not steppable. */
10817 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
10818
b8926edc
DJ
10819 /* We used to default to FPA for generic ARM, but almost nobody
10820 uses that now, and we now provide a way for the user to force
10821 the model. So default to the most useful variant. */
10822 if (tdep->fp_model == ARM_FLOAT_AUTO)
10823 tdep->fp_model = ARM_FLOAT_SOFT_FPA;
10824
9df628e0
RE
10825 if (tdep->jb_pc >= 0)
10826 set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
10827
08216dd7 10828 /* Floating point sizes and format. */
8da61cc4 10829 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
b8926edc 10830 if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
08216dd7 10831 {
8da61cc4
DJ
10832 set_gdbarch_double_format
10833 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10834 set_gdbarch_long_double_format
10835 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10836 }
10837 else
10838 {
10839 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
10840 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
08216dd7
RE
10841 }
10842
a01567f4
LM
10843 /* Hook used to decorate frames with signed return addresses, only available
10844 for ARMv8.1-m PACBTI. */
10845 if (is_m && have_pacbti)
10846 set_gdbarch_get_pc_address_flags (gdbarch, arm_get_pc_address_flags);
10847
dc22c61a
LM
10848 if (tdesc_data != nullptr)
10849 {
10850 set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
10851
10852 tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
ecbf5d4f 10853 register_count = gdbarch_num_regs (gdbarch);
dc22c61a
LM
10854
10855 /* Override tdesc_register_type to adjust the types of VFP
10856 registers for NEON. */
10857 set_gdbarch_register_type (gdbarch, arm_register_type);
10858 }
10859
ecbf5d4f 10860 /* Initialize the pseudo register data. */
ae66a8f1 10861 int num_pseudos = 0;
ecbf5d4f 10862 if (tdep->have_s_pseudos)
58d6951d 10863 {
ecbf5d4f
LM
10864 /* VFP single precision pseudo registers (S0~S31). */
10865 tdep->s_pseudo_base = register_count;
10866 tdep->s_pseudo_count = 32;
ae66a8f1 10867 num_pseudos += tdep->s_pseudo_count;
ecbf5d4f
LM
10868
10869 if (tdep->have_q_pseudos)
10870 {
10871 /* NEON quad precision pseudo registers (Q0~Q15). */
10872 tdep->q_pseudo_base = register_count + num_pseudos;
ae66a8f1
SP
10873
10874 if (have_neon)
10875 tdep->q_pseudo_count = 16;
10876 else if (have_mve)
10877 tdep->q_pseudo_count = ARM_MVE_NUM_Q_REGS;
10878
ecbf5d4f
LM
10879 num_pseudos += tdep->q_pseudo_count;
10880 }
ae66a8f1
SP
10881 }
10882
10883 /* Do we have any MVE pseudo registers? */
10884 if (have_mve)
10885 {
10886 tdep->mve_pseudo_base = register_count + num_pseudos;
10887 tdep->mve_pseudo_count = 1;
10888 num_pseudos += tdep->mve_pseudo_count;
10889 }
58d6951d 10890
a01567f4
LM
10891 /* Do we have any ARMv8.1-m PACBTI pseudo registers. */
10892 if (have_pacbti)
10893 {
10894 tdep->pacbti_pseudo_base = register_count + num_pseudos;
10895 tdep->pacbti_pseudo_count = 1;
10896 num_pseudos += tdep->pacbti_pseudo_count;
10897 }
10898
ae66a8f1 10899 /* Set some pseudo register hooks, if we have pseudo registers. */
a01567f4 10900 if (tdep->have_s_pseudos || have_mve || have_pacbti)
ae66a8f1 10901 {
58d6951d
DJ
10902 set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
10903 set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
10904 set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
10905 }
10906
123dc839 10907 /* Add standard register aliases. We add aliases even for those
85102364 10908 names which are used by the current architecture - it's simpler,
123dc839
DJ
10909 and does no harm, since nothing ever lists user registers. */
10910 for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
10911 user_reg_add (gdbarch, arm_register_aliases[i].name,
10912 value_of_arm_user_reg, &arm_register_aliases[i].regnum);
10913
65b48a81
PB
10914 set_gdbarch_disassembler_options (gdbarch, &arm_disassembler_options);
10915 set_gdbarch_valid_disassembler_options (gdbarch, disassembler_options_arm ());
10916
3426ae57
AH
10917 set_gdbarch_gnu_triplet_regexp (gdbarch, arm_gnu_triplet_regexp);
10918
39bbf761
RE
10919 return gdbarch;
10920}
10921
97e03143 10922static void
2af46ca0 10923arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
97e03143 10924{
08106042 10925 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
97e03143
RE
10926
10927 if (tdep == NULL)
10928 return;
10929
6cb06a8c
TT
10930 gdb_printf (file, _("arm_dump_tdep: fp_model = %i\n"),
10931 (int) tdep->fp_model);
10932 gdb_printf (file, _("arm_dump_tdep: have_fpa_registers = %i\n"),
10933 (int) tdep->have_fpa_registers);
10934 gdb_printf (file, _("arm_dump_tdep: have_wmmx_registers = %i\n"),
10935 (int) tdep->have_wmmx_registers);
10936 gdb_printf (file, _("arm_dump_tdep: vfp_register_count = %i\n"),
10937 (int) tdep->vfp_register_count);
10938 gdb_printf (file, _("arm_dump_tdep: have_s_pseudos = %s\n"),
05d63baf 10939 tdep->have_s_pseudos ? "true" : "false");
6cb06a8c
TT
10940 gdb_printf (file, _("arm_dump_tdep: s_pseudo_base = %i\n"),
10941 (int) tdep->s_pseudo_base);
10942 gdb_printf (file, _("arm_dump_tdep: s_pseudo_count = %i\n"),
10943 (int) tdep->s_pseudo_count);
10944 gdb_printf (file, _("arm_dump_tdep: have_q_pseudos = %s\n"),
05d63baf 10945 tdep->have_q_pseudos ? "true" : "false");
6cb06a8c
TT
10946 gdb_printf (file, _("arm_dump_tdep: q_pseudo_base = %i\n"),
10947 (int) tdep->q_pseudo_base);
10948 gdb_printf (file, _("arm_dump_tdep: q_pseudo_count = %i\n"),
10949 (int) tdep->q_pseudo_count);
10950 gdb_printf (file, _("arm_dump_tdep: have_neon = %i\n"),
10951 (int) tdep->have_neon);
10952 gdb_printf (file, _("arm_dump_tdep: have_mve = %s\n"),
05d63baf 10953 tdep->have_mve ? "yes" : "no");
6cb06a8c
TT
10954 gdb_printf (file, _("arm_dump_tdep: mve_vpr_regnum = %i\n"),
10955 tdep->mve_vpr_regnum);
10956 gdb_printf (file, _("arm_dump_tdep: mve_pseudo_base = %i\n"),
10957 tdep->mve_pseudo_base);
10958 gdb_printf (file, _("arm_dump_tdep: mve_pseudo_count = %i\n"),
10959 tdep->mve_pseudo_count);
9074667a
CL
10960 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_regnum = %i\n"),
10961 tdep->m_profile_msp_regnum);
10962 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_regnum = %i\n"),
10963 tdep->m_profile_psp_regnum);
ae7e2f45
CL
10964 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_ns_regnum = %i\n"),
10965 tdep->m_profile_msp_ns_regnum);
10966 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_ns_regnum = %i\n"),
10967 tdep->m_profile_psp_ns_regnum);
10968 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_s_regnum = %i\n"),
10969 tdep->m_profile_msp_s_regnum);
10970 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_s_regnum = %i\n"),
10971 tdep->m_profile_psp_s_regnum);
6cb06a8c
TT
10972 gdb_printf (file, _("arm_dump_tdep: Lowest pc = 0x%lx\n"),
10973 (unsigned long) tdep->lowest_pc);
a01567f4 10974 gdb_printf (file, _("arm_dump_tdep: have_pacbti = %s\n"),
05d63baf 10975 tdep->have_pacbti ? "yes" : "no");
a01567f4
LM
10976 gdb_printf (file, _("arm_dump_tdep: pacbti_pseudo_base = %i\n"),
10977 tdep->pacbti_pseudo_base);
10978 gdb_printf (file, _("arm_dump_tdep: pacbti_pseudo_count = %i\n"),
10979 tdep->pacbti_pseudo_count);
10980 gdb_printf (file, _("arm_dump_tdep: is_m = %s\n"),
05d63baf 10981 tdep->is_m ? "yes" : "no");
97e03143
RE
10982}
10983
0d4c07af 10984#if GDB_SELF_TEST
b121eeb9
YQ
10985namespace selftests
10986{
10987static void arm_record_test (void);
9ecab40c 10988static void arm_analyze_prologue_test ();
b121eeb9 10989}
0d4c07af 10990#endif
b121eeb9 10991
6c265988 10992void _initialize_arm_tdep ();
c906108c 10993void
6c265988 10994_initialize_arm_tdep ()
c906108c 10995{
bc90b915 10996 long length;
65b48a81 10997 int i, j;
edefbb7c
AC
10998 char regdesc[1024], *rdptr = regdesc;
10999 size_t rest = sizeof (regdesc);
085dd6e6 11000
42cf1509 11001 gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
97e03143 11002
0e9e9abd 11003 /* Add ourselves to objfile event chain. */
c90e7d63 11004 gdb::observers::new_objfile.attach (arm_exidx_new_objfile, "arm-tdep");
0e9e9abd 11005
70f80edf
JT
11006 /* Register an ELF OS ABI sniffer for ARM binaries. */
11007 gdbarch_register_osabi_sniffer (bfd_arch_arm,
11008 bfd_target_elf_flavour,
11009 arm_elf_osabi_sniffer);
11010
afd7eef0 11011 /* Add root prefix command for all "set arm"/"show arm" commands. */
f54bdb6d
SM
11012 add_setshow_prefix_cmd ("arm", no_class,
11013 _("Various ARM-specific commands."),
11014 _("Various ARM-specific commands."),
11015 &setarmcmdlist, &showarmcmdlist,
11016 &setlist, &showlist);
c5aa993b 11017
65b48a81 11018 arm_disassembler_options = xstrdup ("reg-names-std");
471b9d15
MR
11019 const disasm_options_t *disasm_options
11020 = &disassembler_options_arm ()->options;
65b48a81
PB
11021 int num_disassembly_styles = 0;
11022 for (i = 0; disasm_options->name[i] != NULL; i++)
08dedd66 11023 if (startswith (disasm_options->name[i], "reg-names-"))
65b48a81
PB
11024 num_disassembly_styles++;
11025
11026 /* Initialize the array that will be passed to add_setshow_enum_cmd(). */
8d749320 11027 valid_disassembly_styles = XNEWVEC (const char *,
65b48a81
PB
11028 num_disassembly_styles + 1);
11029 for (i = j = 0; disasm_options->name[i] != NULL; i++)
08dedd66 11030 if (startswith (disasm_options->name[i], "reg-names-"))
65b48a81
PB
11031 {
11032 size_t offset = strlen ("reg-names-");
11033 const char *style = disasm_options->name[i];
11034 valid_disassembly_styles[j++] = &style[offset];
dedb7102
TT
11035 if (strcmp (&style[offset], "std") == 0)
11036 disassembly_style = &style[offset];
65b48a81
PB
11037 length = snprintf (rdptr, rest, "%s - %s\n", &style[offset],
11038 disasm_options->description[i]);
11039 rdptr += length;
11040 rest -= length;
11041 }
94c30b78 11042 /* Mark the end of valid options. */
65b48a81 11043 valid_disassembly_styles[num_disassembly_styles] = NULL;
c906108c 11044
edefbb7c 11045 /* Create the help text. */
d7e74731
PA
11046 std::string helptext = string_printf ("%s%s%s",
11047 _("The valid values are:\n"),
11048 regdesc,
11049 _("The default is \"std\"."));
ed9a39eb 11050
edefbb7c
AC
11051 add_setshow_enum_cmd("disassembler", no_class,
11052 valid_disassembly_styles, &disassembly_style,
11053 _("Set the disassembly style."),
11054 _("Show the disassembly style."),
09b0e4b0 11055 helptext.c_str (),
2c5b56ce 11056 set_disassembly_style_sfunc,
65b48a81 11057 show_disassembly_style_sfunc,
7376b4c2 11058 &setarmcmdlist, &showarmcmdlist);
edefbb7c
AC
11059
11060 add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
11061 _("Set usage of ARM 32-bit mode."),
11062 _("Show usage of ARM 32-bit mode."),
11063 _("When off, a 26-bit PC will be used."),
2c5b56ce 11064 NULL,
0963b4bd
MS
11065 NULL, /* FIXME: i18n: Usage of ARM 32-bit
11066 mode is %s. */
26304000 11067 &setarmcmdlist, &showarmcmdlist);
c906108c 11068
fd50bc42 11069 /* Add a command to allow the user to force the FPU model. */
edefbb7c
AC
11070 add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
11071 _("Set the floating point type."),
11072 _("Show the floating point type."),
11073 _("auto - Determine the FP typefrom the OS-ABI.\n\
11074softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
11075fpa - FPA co-processor (GCC compiled).\n\
11076softvfp - Software FP with pure-endian doubles.\n\
11077vfp - VFP co-processor."),
edefbb7c 11078 set_fp_model_sfunc, show_fp_model,
7376b4c2 11079 &setarmcmdlist, &showarmcmdlist);
fd50bc42 11080
28e97307
DJ
11081 /* Add a command to allow the user to force the ABI. */
11082 add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
11083 _("Set the ABI."),
11084 _("Show the ABI."),
11085 NULL, arm_set_abi, arm_show_abi,
11086 &setarmcmdlist, &showarmcmdlist);
11087
0428b8f5
DJ
11088 /* Add two commands to allow the user to force the assumed
11089 execution mode. */
11090 add_setshow_enum_cmd ("fallback-mode", class_support,
11091 arm_mode_strings, &arm_fallback_mode_string,
11092 _("Set the mode assumed when symbols are unavailable."),
11093 _("Show the mode assumed when symbols are unavailable."),
11094 NULL, NULL, arm_show_fallback_mode,
11095 &setarmcmdlist, &showarmcmdlist);
11096 add_setshow_enum_cmd ("force-mode", class_support,
11097 arm_mode_strings, &arm_force_mode_string,
11098 _("Set the mode assumed even when symbols are available."),
11099 _("Show the mode assumed even when symbols are available."),
11100 NULL, NULL, arm_show_force_mode,
11101 &setarmcmdlist, &showarmcmdlist);
11102
ef273377
CL
11103 /* Add a command to stop triggering security exceptions when
11104 unwinding exception stacks. */
11105 add_setshow_boolean_cmd ("unwind-secure-frames", no_class, &arm_unwind_secure_frames,
11106 _("Set usage of non-secure to secure exception stack unwinding."),
11107 _("Show usage of non-secure to secure exception stack unwinding."),
11108 _("When on, the debugger can trigger memory access traps."),
11109 NULL, arm_show_unwind_secure_frames,
11110 &setarmcmdlist, &showarmcmdlist);
11111
6529d2dd 11112 /* Debugging flag. */
edefbb7c
AC
11113 add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
11114 _("Set ARM debugging."),
11115 _("Show ARM debugging."),
11116 _("When on, arm-specific debugging is enabled."),
2c5b56ce 11117 NULL,
7915a72c 11118 NULL, /* FIXME: i18n: "ARM debugging is %s. */
26304000 11119 &setdebuglist, &showdebuglist);
b121eeb9
YQ
11120
11121#if GDB_SELF_TEST
1526853e 11122 selftests::register_test ("arm-record", selftests::arm_record_test);
9ecab40c 11123 selftests::register_test ("arm_analyze_prologue", selftests::arm_analyze_prologue_test);
b121eeb9
YQ
11124#endif
11125
c906108c 11126}
72508ac0
PO
11127
11128/* ARM-reversible process record data structures. */
11129
11130#define ARM_INSN_SIZE_BYTES 4
11131#define THUMB_INSN_SIZE_BYTES 2
11132#define THUMB2_INSN_SIZE_BYTES 4
11133
11134
71e396f9
LM
11135/* Position of the bit within a 32-bit ARM instruction
11136 that defines whether the instruction is a load or store. */
72508ac0
PO
11137#define INSN_S_L_BIT_NUM 20
11138
11139#define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
dda83cd7
SM
11140 do \
11141 { \
11142 unsigned int reg_len = LENGTH; \
11143 if (reg_len) \
11144 { \
11145 REGS = XNEWVEC (uint32_t, reg_len); \
11146 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
11147 } \
11148 } \
11149 while (0)
72508ac0
PO
11150
11151#define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
dda83cd7
SM
11152 do \
11153 { \
11154 unsigned int mem_len = LENGTH; \
11155 if (mem_len) \
01add95b
SM
11156 { \
11157 MEMS = XNEWVEC (struct arm_mem_r, mem_len); \
11158 memcpy(&MEMS->len, &RECORD_BUF[0], \
11159 sizeof(struct arm_mem_r) * LENGTH); \
11160 } \
dda83cd7
SM
11161 } \
11162 while (0)
72508ac0
PO
11163
11164/* Checks whether insn is already recorded or yet to be decoded. (boolean expression). */
11165#define INSN_RECORDED(ARM_RECORD) \
dda83cd7 11166 (0 != (ARM_RECORD)->reg_rec_count || 0 != (ARM_RECORD)->mem_rec_count)
72508ac0
PO
11167
11168/* ARM memory record structure. */
11169struct arm_mem_r
11170{
11171 uint32_t len; /* Record length. */
bfbbec00 11172 uint32_t addr; /* Memory address. */
72508ac0
PO
11173};
11174
11175/* ARM instruction record contains opcode of current insn
11176 and execution state (before entry to decode_insn()),
11177 contains list of to-be-modified registers and
11178 memory blocks (on return from decode_insn()). */
11179
4748a9be 11180struct arm_insn_decode_record
72508ac0
PO
11181{
11182 struct gdbarch *gdbarch;
11183 struct regcache *regcache;
11184 CORE_ADDR this_addr; /* Address of the insn being decoded. */
11185 uint32_t arm_insn; /* Should accommodate thumb. */
11186 uint32_t cond; /* Condition code. */
11187 uint32_t opcode; /* Insn opcode. */
11188 uint32_t decode; /* Insn decode bits. */
11189 uint32_t mem_rec_count; /* No of mem records. */
11190 uint32_t reg_rec_count; /* No of reg records. */
11191 uint32_t *arm_regs; /* Registers to be saved for this record. */
11192 struct arm_mem_r *arm_mems; /* Memory to be saved for this record. */
4748a9be 11193};
72508ac0
PO
11194
11195
11196/* Checks ARM SBZ and SBO mandatory fields. */
11197
11198static int
11199sbo_sbz (uint32_t insn, uint32_t bit_num, uint32_t len, uint32_t sbo)
11200{
11201 uint32_t ones = bits (insn, bit_num - 1, (bit_num -1) + (len - 1));
11202
11203 if (!len)
11204 return 1;
11205
11206 if (!sbo)
11207 ones = ~ones;
11208
11209 while (ones)
11210 {
11211 if (!(ones & sbo))
dda83cd7
SM
11212 {
11213 return 0;
11214 }
72508ac0
PO
11215 ones = ones >> 1;
11216 }
11217 return 1;
11218}
11219
c6ec2b30
OJ
11220enum arm_record_result
11221{
11222 ARM_RECORD_SUCCESS = 0,
11223 ARM_RECORD_FAILURE = 1
11224};
11225
0d1703b8 11226enum arm_record_strx_t
72508ac0
PO
11227{
11228 ARM_RECORD_STRH=1,
11229 ARM_RECORD_STRD
0d1703b8 11230};
72508ac0 11231
0d1703b8 11232enum record_type_t
72508ac0
PO
11233{
11234 ARM_RECORD=1,
11235 THUMB_RECORD,
11236 THUMB2_RECORD
0d1703b8 11237};
72508ac0
PO
11238
11239
11240static int
4748a9be 11241arm_record_strx (arm_insn_decode_record *arm_insn_r, uint32_t *record_buf,
dda83cd7 11242 uint32_t *record_buf_mem, arm_record_strx_t str_type)
72508ac0
PO
11243{
11244
11245 struct regcache *reg_cache = arm_insn_r->regcache;
11246 ULONGEST u_regval[2]= {0};
11247
11248 uint32_t reg_src1 = 0, reg_src2 = 0;
11249 uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
72508ac0
PO
11250
11251 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11252 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
72508ac0
PO
11253
11254 if (14 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11255 {
11256 /* 1) Handle misc store, immediate offset. */
11257 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
11258 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
11259 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11260 regcache_raw_read_unsigned (reg_cache, reg_src1,
dda83cd7 11261 &u_regval[0]);
72508ac0 11262 if (ARM_PC_REGNUM == reg_src1)
dda83cd7
SM
11263 {
11264 /* If R15 was used as Rn, hence current PC+8. */
11265 u_regval[0] = u_regval[0] + 8;
11266 }
72508ac0
PO
11267 offset_8 = (immed_high << 4) | immed_low;
11268 /* Calculate target store address. */
11269 if (14 == arm_insn_r->opcode)
dda83cd7
SM
11270 {
11271 tgt_mem_addr = u_regval[0] + offset_8;
11272 }
72508ac0 11273 else
dda83cd7
SM
11274 {
11275 tgt_mem_addr = u_regval[0] - offset_8;
11276 }
72508ac0 11277 if (ARM_RECORD_STRH == str_type)
dda83cd7
SM
11278 {
11279 record_buf_mem[0] = 2;
11280 record_buf_mem[1] = tgt_mem_addr;
11281 arm_insn_r->mem_rec_count = 1;
11282 }
72508ac0 11283 else if (ARM_RECORD_STRD == str_type)
dda83cd7
SM
11284 {
11285 record_buf_mem[0] = 4;
11286 record_buf_mem[1] = tgt_mem_addr;
11287 record_buf_mem[2] = 4;
11288 record_buf_mem[3] = tgt_mem_addr + 4;
11289 arm_insn_r->mem_rec_count = 2;
11290 }
72508ac0
PO
11291 }
11292 else if (12 == arm_insn_r->opcode || 8 == arm_insn_r->opcode)
11293 {
11294 /* 2) Store, register offset. */
11295 /* Get Rm. */
11296 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11297 /* Get Rn. */
11298 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11299 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11300 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11301 if (15 == reg_src2)
dda83cd7
SM
11302 {
11303 /* If R15 was used as Rn, hence current PC+8. */
11304 u_regval[0] = u_regval[0] + 8;
11305 }
72508ac0
PO
11306 /* Calculate target store address, Rn +/- Rm, register offset. */
11307 if (12 == arm_insn_r->opcode)
dda83cd7
SM
11308 {
11309 tgt_mem_addr = u_regval[0] + u_regval[1];
11310 }
72508ac0 11311 else
dda83cd7
SM
11312 {
11313 tgt_mem_addr = u_regval[1] - u_regval[0];
11314 }
72508ac0 11315 if (ARM_RECORD_STRH == str_type)
dda83cd7
SM
11316 {
11317 record_buf_mem[0] = 2;
11318 record_buf_mem[1] = tgt_mem_addr;
11319 arm_insn_r->mem_rec_count = 1;
11320 }
72508ac0 11321 else if (ARM_RECORD_STRD == str_type)
dda83cd7
SM
11322 {
11323 record_buf_mem[0] = 4;
11324 record_buf_mem[1] = tgt_mem_addr;
11325 record_buf_mem[2] = 4;
11326 record_buf_mem[3] = tgt_mem_addr + 4;
11327 arm_insn_r->mem_rec_count = 2;
11328 }
72508ac0
PO
11329 }
11330 else if (11 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
dda83cd7 11331 || 2 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
72508ac0
PO
11332 {
11333 /* 3) Store, immediate pre-indexed. */
11334 /* 5) Store, immediate post-indexed. */
11335 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
11336 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
11337 offset_8 = (immed_high << 4) | immed_low;
11338 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11339 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11340 /* Calculate target store address, Rn +/- Rm, register offset. */
11341 if (15 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
dda83cd7
SM
11342 {
11343 tgt_mem_addr = u_regval[0] + offset_8;
11344 }
72508ac0 11345 else
dda83cd7
SM
11346 {
11347 tgt_mem_addr = u_regval[0] - offset_8;
11348 }
72508ac0 11349 if (ARM_RECORD_STRH == str_type)
dda83cd7
SM
11350 {
11351 record_buf_mem[0] = 2;
11352 record_buf_mem[1] = tgt_mem_addr;
11353 arm_insn_r->mem_rec_count = 1;
11354 }
72508ac0 11355 else if (ARM_RECORD_STRD == str_type)
dda83cd7
SM
11356 {
11357 record_buf_mem[0] = 4;
11358 record_buf_mem[1] = tgt_mem_addr;
11359 record_buf_mem[2] = 4;
11360 record_buf_mem[3] = tgt_mem_addr + 4;
11361 arm_insn_r->mem_rec_count = 2;
11362 }
72508ac0
PO
11363 /* Record Rn also as it changes. */
11364 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
11365 arm_insn_r->reg_rec_count = 1;
11366 }
11367 else if (9 == arm_insn_r->opcode || 13 == arm_insn_r->opcode
dda83cd7 11368 || 0 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
72508ac0
PO
11369 {
11370 /* 4) Store, register pre-indexed. */
11371 /* 6) Store, register post -indexed. */
11372 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11373 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11374 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11375 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11376 /* Calculate target store address, Rn +/- Rm, register offset. */
11377 if (13 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
dda83cd7
SM
11378 {
11379 tgt_mem_addr = u_regval[0] + u_regval[1];
11380 }
72508ac0 11381 else
dda83cd7
SM
11382 {
11383 tgt_mem_addr = u_regval[1] - u_regval[0];
11384 }
72508ac0 11385 if (ARM_RECORD_STRH == str_type)
dda83cd7
SM
11386 {
11387 record_buf_mem[0] = 2;
11388 record_buf_mem[1] = tgt_mem_addr;
11389 arm_insn_r->mem_rec_count = 1;
11390 }
72508ac0 11391 else if (ARM_RECORD_STRD == str_type)
dda83cd7
SM
11392 {
11393 record_buf_mem[0] = 4;
11394 record_buf_mem[1] = tgt_mem_addr;
11395 record_buf_mem[2] = 4;
11396 record_buf_mem[3] = tgt_mem_addr + 4;
11397 arm_insn_r->mem_rec_count = 2;
11398 }
72508ac0
PO
11399 /* Record Rn also as it changes. */
11400 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
11401 arm_insn_r->reg_rec_count = 1;
11402 }
11403 return 0;
11404}
11405
11406/* Handling ARM extension space insns. */
11407
11408static int
4748a9be 11409arm_record_extension_space (arm_insn_decode_record *arm_insn_r)
72508ac0 11410{
df95a9cf 11411 int ret = 0; /* Return value: -1:record failure ; 0:success */
72508ac0
PO
11412 uint32_t opcode1 = 0, opcode2 = 0, insn_op1 = 0;
11413 uint32_t record_buf[8], record_buf_mem[8];
11414 uint32_t reg_src1 = 0;
72508ac0
PO
11415 struct regcache *reg_cache = arm_insn_r->regcache;
11416 ULONGEST u_regval = 0;
11417
11418 gdb_assert (!INSN_RECORDED(arm_insn_r));
11419 /* Handle unconditional insn extension space. */
11420
11421 opcode1 = bits (arm_insn_r->arm_insn, 20, 27);
11422 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
11423 if (arm_insn_r->cond)
11424 {
11425 /* PLD has no affect on architectural state, it just affects
dda83cd7 11426 the caches. */
72508ac0 11427 if (5 == ((opcode1 & 0xE0) >> 5))
dda83cd7
SM
11428 {
11429 /* BLX(1) */
11430 record_buf[0] = ARM_PS_REGNUM;
11431 record_buf[1] = ARM_LR_REGNUM;
11432 arm_insn_r->reg_rec_count = 2;
11433 }
72508ac0
PO
11434 /* STC2, LDC2, MCR2, MRC2, CDP2: <TBD>, co-processor insn. */
11435 }
11436
11437
11438 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
11439 if (3 == opcode1 && bit (arm_insn_r->arm_insn, 4))
11440 {
11441 ret = -1;
11442 /* Undefined instruction on ARM V5; need to handle if later
dda83cd7 11443 versions define it. */
72508ac0
PO
11444 }
11445
11446 opcode1 = bits (arm_insn_r->arm_insn, 24, 27);
11447 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
11448 insn_op1 = bits (arm_insn_r->arm_insn, 20, 23);
11449
11450 /* Handle arithmetic insn extension space. */
11451 if (!opcode1 && 9 == opcode2 && 1 != arm_insn_r->cond
11452 && !INSN_RECORDED(arm_insn_r))
11453 {
11454 /* Handle MLA(S) and MUL(S). */
b020ff80 11455 if (in_inclusive_range (insn_op1, 0U, 3U))
01add95b
SM
11456 {
11457 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11458 record_buf[1] = ARM_PS_REGNUM;
11459 arm_insn_r->reg_rec_count = 2;
11460 }
b020ff80 11461 else if (in_inclusive_range (insn_op1, 4U, 15U))
01add95b
SM
11462 {
11463 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
11464 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11465 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11466 record_buf[2] = ARM_PS_REGNUM;
11467 arm_insn_r->reg_rec_count = 3;
11468 }
72508ac0
PO
11469 }
11470
11471 opcode1 = bits (arm_insn_r->arm_insn, 26, 27);
11472 opcode2 = bits (arm_insn_r->arm_insn, 23, 24);
11473 insn_op1 = bits (arm_insn_r->arm_insn, 21, 22);
11474
11475 /* Handle control insn extension space. */
11476
11477 if (!opcode1 && 2 == opcode2 && !bit (arm_insn_r->arm_insn, 20)
11478 && 1 != arm_insn_r->cond && !INSN_RECORDED(arm_insn_r))
11479 {
11480 if (!bit (arm_insn_r->arm_insn,25))
dda83cd7
SM
11481 {
11482 if (!bits (arm_insn_r->arm_insn, 4, 7))
11483 {
11484 if ((0 == insn_op1) || (2 == insn_op1))
11485 {
11486 /* MRS. */
11487 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11488 arm_insn_r->reg_rec_count = 1;
11489 }
11490 else if (1 == insn_op1)
11491 {
11492 /* CSPR is going to be changed. */
11493 record_buf[0] = ARM_PS_REGNUM;
11494 arm_insn_r->reg_rec_count = 1;
11495 }
11496 else if (3 == insn_op1)
11497 {
11498 /* SPSR is going to be changed. */
11499 /* We need to get SPSR value, which is yet to be done. */
11500 return -1;
11501 }
11502 }
11503 else if (1 == bits (arm_insn_r->arm_insn, 4, 7))
11504 {
11505 if (1 == insn_op1)
11506 {
11507 /* BX. */
11508 record_buf[0] = ARM_PS_REGNUM;
11509 arm_insn_r->reg_rec_count = 1;
11510 }
11511 else if (3 == insn_op1)
11512 {
11513 /* CLZ. */
11514 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11515 arm_insn_r->reg_rec_count = 1;
11516 }
11517 }
11518 else if (3 == bits (arm_insn_r->arm_insn, 4, 7))
11519 {
11520 /* BLX. */
11521 record_buf[0] = ARM_PS_REGNUM;
11522 record_buf[1] = ARM_LR_REGNUM;
11523 arm_insn_r->reg_rec_count = 2;
11524 }
11525 else if (5 == bits (arm_insn_r->arm_insn, 4, 7))
11526 {
11527 /* QADD, QSUB, QDADD, QDSUB */
11528 record_buf[0] = ARM_PS_REGNUM;
11529 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11530 arm_insn_r->reg_rec_count = 2;
11531 }
11532 else if (7 == bits (arm_insn_r->arm_insn, 4, 7))
11533 {
11534 /* BKPT. */
11535 record_buf[0] = ARM_PS_REGNUM;
11536 record_buf[1] = ARM_LR_REGNUM;
11537 arm_insn_r->reg_rec_count = 2;
11538
11539 /* Save SPSR also;how? */
11540 return -1;
11541 }
11542 else if(8 == bits (arm_insn_r->arm_insn, 4, 7)
11543 || 10 == bits (arm_insn_r->arm_insn, 4, 7)
11544 || 12 == bits (arm_insn_r->arm_insn, 4, 7)
11545 || 14 == bits (arm_insn_r->arm_insn, 4, 7)
11546 )
11547 {
11548 if (0 == insn_op1 || 1 == insn_op1)
11549 {
11550 /* SMLA<x><y>, SMLAW<y>, SMULW<y>. */
11551 /* We dont do optimization for SMULW<y> where we
11552 need only Rd. */
11553 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11554 record_buf[1] = ARM_PS_REGNUM;
11555 arm_insn_r->reg_rec_count = 2;
11556 }
11557 else if (2 == insn_op1)
11558 {
11559 /* SMLAL<x><y>. */
11560 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11561 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
11562 arm_insn_r->reg_rec_count = 2;
11563 }
11564 else if (3 == insn_op1)
11565 {
11566 /* SMUL<x><y>. */
11567 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11568 arm_insn_r->reg_rec_count = 1;
11569 }
11570 }
11571 }
72508ac0 11572 else
dda83cd7
SM
11573 {
11574 /* MSR : immediate form. */
11575 if (1 == insn_op1)
11576 {
11577 /* CSPR is going to be changed. */
11578 record_buf[0] = ARM_PS_REGNUM;
11579 arm_insn_r->reg_rec_count = 1;
11580 }
11581 else if (3 == insn_op1)
11582 {
11583 /* SPSR is going to be changed. */
11584 /* we need to get SPSR value, which is yet to be done */
11585 return -1;
11586 }
11587 }
72508ac0
PO
11588 }
11589
11590 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
11591 opcode2 = bits (arm_insn_r->arm_insn, 20, 24);
11592 insn_op1 = bits (arm_insn_r->arm_insn, 5, 6);
11593
11594 /* Handle load/store insn extension space. */
11595
11596 if (!opcode1 && bit (arm_insn_r->arm_insn, 7)
11597 && bit (arm_insn_r->arm_insn, 4) && 1 != arm_insn_r->cond
11598 && !INSN_RECORDED(arm_insn_r))
11599 {
11600 /* SWP/SWPB. */
11601 if (0 == insn_op1)
dda83cd7
SM
11602 {
11603 /* These insn, changes register and memory as well. */
11604 /* SWP or SWPB insn. */
11605 /* Get memory address given by Rn. */
11606 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11607 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11608 /* SWP insn ?, swaps word. */
11609 if (8 == arm_insn_r->opcode)
11610 {
11611 record_buf_mem[0] = 4;
11612 }
11613 else
11614 {
11615 /* SWPB insn, swaps only byte. */
11616 record_buf_mem[0] = 1;
11617 }
11618 record_buf_mem[1] = u_regval;
11619 arm_insn_r->mem_rec_count = 1;
11620 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11621 arm_insn_r->reg_rec_count = 1;
11622 }
72508ac0 11623 else if (1 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
dda83cd7
SM
11624 {
11625 /* STRH. */
11626 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11627 ARM_RECORD_STRH);
11628 }
72508ac0 11629 else if (2 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
dda83cd7
SM
11630 {
11631 /* LDRD. */
11632 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11633 record_buf[1] = record_buf[0] + 1;
11634 arm_insn_r->reg_rec_count = 2;
11635 }
72508ac0 11636 else if (3 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
dda83cd7
SM
11637 {
11638 /* STRD. */
11639 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11640 ARM_RECORD_STRD);
11641 }
72508ac0 11642 else if (bit (arm_insn_r->arm_insn, 20) && insn_op1 <= 3)
dda83cd7
SM
11643 {
11644 /* LDRH, LDRSB, LDRSH. */
11645 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11646 arm_insn_r->reg_rec_count = 1;
11647 }
72508ac0
PO
11648
11649 }
11650
11651 opcode1 = bits (arm_insn_r->arm_insn, 23, 27);
11652 if (24 == opcode1 && bit (arm_insn_r->arm_insn, 21)
11653 && !INSN_RECORDED(arm_insn_r))
11654 {
11655 ret = -1;
11656 /* Handle coprocessor insn extension space. */
11657 }
11658
11659 /* To be done for ARMv5 and later; as of now we return -1. */
11660 if (-1 == ret)
ca92db2d 11661 return ret;
72508ac0
PO
11662
11663 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11664 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11665
11666 return ret;
11667}
11668
11669/* Handling opcode 000 insns. */
11670
11671static int
4748a9be 11672arm_record_data_proc_misc_ld_str (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
11673{
11674 struct regcache *reg_cache = arm_insn_r->regcache;
11675 uint32_t record_buf[8], record_buf_mem[8];
11676 ULONGEST u_regval[2] = {0};
11677
8d49165d 11678 uint32_t reg_src1 = 0;
72508ac0
PO
11679 uint32_t opcode1 = 0;
11680
11681 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11682 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11683 opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
11684
2d9e6acb 11685 if (!((opcode1 & 0x19) == 0x10))
72508ac0 11686 {
2d9e6acb
YQ
11687 /* Data-processing (register) and Data-processing (register-shifted
11688 register */
11689 /* Out of 11 shifter operands mode, all the insn modifies destination
11690 register, which is specified by 13-16 decode. */
11691 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11692 record_buf[1] = ARM_PS_REGNUM;
11693 arm_insn_r->reg_rec_count = 2;
72508ac0 11694 }
2d9e6acb 11695 else if ((arm_insn_r->decode < 8) && ((opcode1 & 0x19) == 0x10))
72508ac0 11696 {
2d9e6acb
YQ
11697 /* Miscellaneous instructions */
11698
11699 if (3 == arm_insn_r->decode && 0x12 == opcode1
11700 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11701 {
11702 /* Handle BLX, branch and link/exchange. */
11703 if (9 == arm_insn_r->opcode)
11704 {
11705 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm,
11706 and R14 stores the return address. */
11707 record_buf[0] = ARM_PS_REGNUM;
11708 record_buf[1] = ARM_LR_REGNUM;
11709 arm_insn_r->reg_rec_count = 2;
11710 }
11711 }
11712 else if (7 == arm_insn_r->decode && 0x12 == opcode1)
11713 {
11714 /* Handle enhanced software breakpoint insn, BKPT. */
11715 /* CPSR is changed to be executed in ARM state, disabling normal
11716 interrupts, entering abort mode. */
11717 /* According to high vector configuration PC is set. */
11718 /* user hit breakpoint and type reverse, in
11719 that case, we need to go back with previous CPSR and
11720 Program Counter. */
11721 record_buf[0] = ARM_PS_REGNUM;
11722 record_buf[1] = ARM_LR_REGNUM;
11723 arm_insn_r->reg_rec_count = 2;
11724
11725 /* Save SPSR also; how? */
11726 return -1;
11727 }
11728 else if (1 == arm_insn_r->decode && 0x12 == opcode1
11729 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11730 {
11731 /* Handle BX, branch and link/exchange. */
11732 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm. */
11733 record_buf[0] = ARM_PS_REGNUM;
11734 arm_insn_r->reg_rec_count = 1;
11735 }
11736 else if (1 == arm_insn_r->decode && 0x16 == opcode1
11737 && sbo_sbz (arm_insn_r->arm_insn, 9, 4, 1)
11738 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1))
11739 {
11740 /* Count leading zeros: CLZ. */
11741 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11742 arm_insn_r->reg_rec_count = 1;
11743 }
11744 else if (!bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11745 && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11746 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1)
11747 && sbo_sbz (arm_insn_r->arm_insn, 1, 12, 0))
11748 {
11749 /* Handle MRS insn. */
11750 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11751 arm_insn_r->reg_rec_count = 1;
11752 }
72508ac0 11753 }
2d9e6acb 11754 else if (9 == arm_insn_r->decode && opcode1 < 0x10)
72508ac0 11755 {
2d9e6acb
YQ
11756 /* Multiply and multiply-accumulate */
11757
11758 /* Handle multiply instructions. */
11759 /* MLA, MUL, SMLAL, SMULL, UMLAL, UMULL. */
11760 if (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)
f2a883a8
SM
11761 {
11762 /* Handle MLA and MUL. */
11763 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11764 record_buf[1] = ARM_PS_REGNUM;
11765 arm_insn_r->reg_rec_count = 2;
11766 }
11767 else if (4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11768 {
11769 /* Handle SMLAL, SMULL, UMLAL, UMULL. */
11770 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11771 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11772 record_buf[2] = ARM_PS_REGNUM;
11773 arm_insn_r->reg_rec_count = 3;
11774 }
2d9e6acb
YQ
11775 }
11776 else if (9 == arm_insn_r->decode && opcode1 > 0x10)
11777 {
11778 /* Synchronization primitives */
11779
72508ac0
PO
11780 /* Handling SWP, SWPB. */
11781 /* These insn, changes register and memory as well. */
11782 /* SWP or SWPB insn. */
11783
11784 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11785 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11786 /* SWP insn ?, swaps word. */
11787 if (8 == arm_insn_r->opcode)
2d9e6acb
YQ
11788 {
11789 record_buf_mem[0] = 4;
11790 }
11791 else
11792 {
11793 /* SWPB insn, swaps only byte. */
11794 record_buf_mem[0] = 1;
11795 }
72508ac0
PO
11796 record_buf_mem[1] = u_regval[0];
11797 arm_insn_r->mem_rec_count = 1;
11798 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11799 arm_insn_r->reg_rec_count = 1;
11800 }
2d9e6acb
YQ
11801 else if (11 == arm_insn_r->decode || 13 == arm_insn_r->decode
11802 || 15 == arm_insn_r->decode)
72508ac0 11803 {
2d9e6acb
YQ
11804 if ((opcode1 & 0x12) == 2)
11805 {
11806 /* Extra load/store (unprivileged) */
11807 return -1;
11808 }
11809 else
11810 {
11811 /* Extra load/store */
11812 switch (bits (arm_insn_r->arm_insn, 5, 6))
11813 {
11814 case 1:
11815 if ((opcode1 & 0x05) == 0x0 || (opcode1 & 0x05) == 0x4)
11816 {
11817 /* STRH (register), STRH (immediate) */
11818 arm_record_strx (arm_insn_r, &record_buf[0],
11819 &record_buf_mem[0], ARM_RECORD_STRH);
11820 }
11821 else if ((opcode1 & 0x05) == 0x1)
11822 {
11823 /* LDRH (register) */
11824 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11825 arm_insn_r->reg_rec_count = 1;
72508ac0 11826
2d9e6acb
YQ
11827 if (bit (arm_insn_r->arm_insn, 21))
11828 {
11829 /* Write back to Rn. */
11830 record_buf[arm_insn_r->reg_rec_count++]
11831 = bits (arm_insn_r->arm_insn, 16, 19);
11832 }
11833 }
11834 else if ((opcode1 & 0x05) == 0x5)
11835 {
11836 /* LDRH (immediate), LDRH (literal) */
11837 int rn = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0 11838
2d9e6acb
YQ
11839 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11840 arm_insn_r->reg_rec_count = 1;
11841
11842 if (rn != 15)
11843 {
11844 /*LDRH (immediate) */
11845 if (bit (arm_insn_r->arm_insn, 21))
11846 {
11847 /* Write back to Rn. */
11848 record_buf[arm_insn_r->reg_rec_count++] = rn;
11849 }
11850 }
11851 }
11852 else
11853 return -1;
11854 break;
11855 case 2:
11856 if ((opcode1 & 0x05) == 0x0)
11857 {
11858 /* LDRD (register) */
11859 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11860 record_buf[1] = record_buf[0] + 1;
11861 arm_insn_r->reg_rec_count = 2;
11862
11863 if (bit (arm_insn_r->arm_insn, 21))
11864 {
11865 /* Write back to Rn. */
11866 record_buf[arm_insn_r->reg_rec_count++]
11867 = bits (arm_insn_r->arm_insn, 16, 19);
11868 }
11869 }
11870 else if ((opcode1 & 0x05) == 0x1)
11871 {
11872 /* LDRSB (register) */
11873 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11874 arm_insn_r->reg_rec_count = 1;
11875
11876 if (bit (arm_insn_r->arm_insn, 21))
11877 {
11878 /* Write back to Rn. */
11879 record_buf[arm_insn_r->reg_rec_count++]
11880 = bits (arm_insn_r->arm_insn, 16, 19);
11881 }
11882 }
11883 else if ((opcode1 & 0x05) == 0x4 || (opcode1 & 0x05) == 0x5)
11884 {
11885 /* LDRD (immediate), LDRD (literal), LDRSB (immediate),
11886 LDRSB (literal) */
11887 int rn = bits (arm_insn_r->arm_insn, 16, 19);
11888
11889 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11890 arm_insn_r->reg_rec_count = 1;
11891
11892 if (rn != 15)
11893 {
11894 /*LDRD (immediate), LDRSB (immediate) */
11895 if (bit (arm_insn_r->arm_insn, 21))
11896 {
11897 /* Write back to Rn. */
11898 record_buf[arm_insn_r->reg_rec_count++] = rn;
11899 }
11900 }
11901 }
11902 else
11903 return -1;
11904 break;
11905 case 3:
11906 if ((opcode1 & 0x05) == 0x0)
11907 {
11908 /* STRD (register) */
11909 arm_record_strx (arm_insn_r, &record_buf[0],
11910 &record_buf_mem[0], ARM_RECORD_STRD);
11911 }
11912 else if ((opcode1 & 0x05) == 0x1)
11913 {
11914 /* LDRSH (register) */
11915 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11916 arm_insn_r->reg_rec_count = 1;
11917
11918 if (bit (arm_insn_r->arm_insn, 21))
11919 {
11920 /* Write back to Rn. */
11921 record_buf[arm_insn_r->reg_rec_count++]
11922 = bits (arm_insn_r->arm_insn, 16, 19);
11923 }
11924 }
11925 else if ((opcode1 & 0x05) == 0x4)
11926 {
11927 /* STRD (immediate) */
11928 arm_record_strx (arm_insn_r, &record_buf[0],
11929 &record_buf_mem[0], ARM_RECORD_STRD);
11930 }
11931 else if ((opcode1 & 0x05) == 0x5)
11932 {
11933 /* LDRSH (immediate), LDRSH (literal) */
11934 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11935 arm_insn_r->reg_rec_count = 1;
11936
11937 if (bit (arm_insn_r->arm_insn, 21))
11938 {
11939 /* Write back to Rn. */
11940 record_buf[arm_insn_r->reg_rec_count++]
11941 = bits (arm_insn_r->arm_insn, 16, 19);
11942 }
11943 }
11944 else
11945 return -1;
11946 break;
11947 default:
11948 return -1;
11949 }
11950 }
72508ac0
PO
11951 }
11952 else
11953 {
11954 return -1;
11955 }
11956
11957 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11958 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11959 return 0;
11960}
11961
11962/* Handling opcode 001 insns. */
11963
11964static int
4748a9be 11965arm_record_data_proc_imm (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
11966{
11967 uint32_t record_buf[8], record_buf_mem[8];
11968
11969 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11970 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11971
11972 if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11973 && 2 == bits (arm_insn_r->arm_insn, 20, 21)
11974 && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
11975 )
11976 {
11977 /* Handle MSR insn. */
11978 if (9 == arm_insn_r->opcode)
dda83cd7
SM
11979 {
11980 /* CSPR is going to be changed. */
11981 record_buf[0] = ARM_PS_REGNUM;
11982 arm_insn_r->reg_rec_count = 1;
11983 }
72508ac0 11984 else
dda83cd7
SM
11985 {
11986 /* SPSR is going to be changed. */
11987 }
72508ac0
PO
11988 }
11989 else if (arm_insn_r->opcode <= 15)
11990 {
11991 /* Normal data processing insns. */
11992 /* Out of 11 shifter operands mode, all the insn modifies destination
dda83cd7 11993 register, which is specified by 13-16 decode. */
72508ac0
PO
11994 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11995 record_buf[1] = ARM_PS_REGNUM;
11996 arm_insn_r->reg_rec_count = 2;
11997 }
11998 else
11999 {
12000 return -1;
12001 }
12002
12003 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12004 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12005 return 0;
12006}
12007
c55978a6 12008static int
4748a9be 12009arm_record_media (arm_insn_decode_record *arm_insn_r)
c55978a6
YQ
12010{
12011 uint32_t record_buf[8];
12012
12013 switch (bits (arm_insn_r->arm_insn, 22, 24))
12014 {
12015 case 0:
12016 /* Parallel addition and subtraction, signed */
12017 case 1:
12018 /* Parallel addition and subtraction, unsigned */
12019 case 2:
12020 case 3:
12021 /* Packing, unpacking, saturation and reversal */
12022 {
12023 int rd = bits (arm_insn_r->arm_insn, 12, 15);
12024
12025 record_buf[arm_insn_r->reg_rec_count++] = rd;
12026 }
12027 break;
12028
12029 case 4:
12030 case 5:
12031 /* Signed multiplies */
12032 {
12033 int rd = bits (arm_insn_r->arm_insn, 16, 19);
12034 unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 22);
12035
12036 record_buf[arm_insn_r->reg_rec_count++] = rd;
12037 if (op1 == 0x0)
12038 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
12039 else if (op1 == 0x4)
12040 record_buf[arm_insn_r->reg_rec_count++]
12041 = bits (arm_insn_r->arm_insn, 12, 15);
12042 }
12043 break;
12044
12045 case 6:
12046 {
12047 if (bit (arm_insn_r->arm_insn, 21)
12048 && bits (arm_insn_r->arm_insn, 5, 6) == 0x2)
12049 {
12050 /* SBFX */
12051 record_buf[arm_insn_r->reg_rec_count++]
12052 = bits (arm_insn_r->arm_insn, 12, 15);
12053 }
12054 else if (bits (arm_insn_r->arm_insn, 20, 21) == 0x0
12055 && bits (arm_insn_r->arm_insn, 5, 7) == 0x0)
12056 {
12057 /* USAD8 and USADA8 */
12058 record_buf[arm_insn_r->reg_rec_count++]
12059 = bits (arm_insn_r->arm_insn, 16, 19);
12060 }
12061 }
12062 break;
12063
12064 case 7:
12065 {
12066 if (bits (arm_insn_r->arm_insn, 20, 21) == 0x3
12067 && bits (arm_insn_r->arm_insn, 5, 7) == 0x7)
12068 {
12069 /* Permanently UNDEFINED */
12070 return -1;
12071 }
12072 else
12073 {
12074 /* BFC, BFI and UBFX */
12075 record_buf[arm_insn_r->reg_rec_count++]
12076 = bits (arm_insn_r->arm_insn, 12, 15);
12077 }
12078 }
12079 break;
12080
12081 default:
12082 return -1;
12083 }
12084
12085 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12086
12087 return 0;
12088}
12089
71e396f9 12090/* Handle ARM mode instructions with opcode 010. */
72508ac0
PO
12091
12092static int
4748a9be 12093arm_record_ld_st_imm_offset (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
12094{
12095 struct regcache *reg_cache = arm_insn_r->regcache;
12096
71e396f9
LM
12097 uint32_t reg_base , reg_dest;
12098 uint32_t offset_12, tgt_mem_addr;
72508ac0 12099 uint32_t record_buf[8], record_buf_mem[8];
71e396f9
LM
12100 unsigned char wback;
12101 ULONGEST u_regval;
72508ac0 12102
71e396f9
LM
12103 /* Calculate wback. */
12104 wback = (bit (arm_insn_r->arm_insn, 24) == 0)
12105 || (bit (arm_insn_r->arm_insn, 21) == 1);
72508ac0 12106
71e396f9
LM
12107 arm_insn_r->reg_rec_count = 0;
12108 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0
PO
12109
12110 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12111 {
71e396f9
LM
12112 /* LDR (immediate), LDR (literal), LDRB (immediate), LDRB (literal), LDRBT
12113 and LDRT. */
12114
72508ac0 12115 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
71e396f9
LM
12116 record_buf[arm_insn_r->reg_rec_count++] = reg_dest;
12117
12118 /* The LDR instruction is capable of doing branching. If MOV LR, PC
12119 preceeds a LDR instruction having R15 as reg_base, it
12120 emulates a branch and link instruction, and hence we need to save
12121 CPSR and PC as well. */
12122 if (ARM_PC_REGNUM == reg_dest)
12123 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
12124
12125 /* If wback is true, also save the base register, which is going to be
12126 written to. */
12127 if (wback)
12128 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
12129 }
12130 else
12131 {
71e396f9
LM
12132 /* STR (immediate), STRB (immediate), STRBT and STRT. */
12133
72508ac0 12134 offset_12 = bits (arm_insn_r->arm_insn, 0, 11);
71e396f9
LM
12135 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
12136
12137 /* Handle bit U. */
72508ac0 12138 if (bit (arm_insn_r->arm_insn, 23))
71e396f9
LM
12139 {
12140 /* U == 1: Add the offset. */
12141 tgt_mem_addr = (uint32_t) u_regval + offset_12;
12142 }
72508ac0 12143 else
71e396f9
LM
12144 {
12145 /* U == 0: subtract the offset. */
12146 tgt_mem_addr = (uint32_t) u_regval - offset_12;
12147 }
12148
12149 /* Bit 22 tells us whether the store instruction writes 1 byte or 4
12150 bytes. */
12151 if (bit (arm_insn_r->arm_insn, 22))
12152 {
12153 /* STRB and STRBT: 1 byte. */
12154 record_buf_mem[0] = 1;
12155 }
12156 else
12157 {
12158 /* STR and STRT: 4 bytes. */
12159 record_buf_mem[0] = 4;
12160 }
12161
12162 /* Handle bit P. */
12163 if (bit (arm_insn_r->arm_insn, 24))
12164 record_buf_mem[1] = tgt_mem_addr;
12165 else
12166 record_buf_mem[1] = (uint32_t) u_regval;
72508ac0 12167
72508ac0
PO
12168 arm_insn_r->mem_rec_count = 1;
12169
71e396f9
LM
12170 /* If wback is true, also save the base register, which is going to be
12171 written to. */
12172 if (wback)
12173 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
12174 }
12175
12176 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12177 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12178 return 0;
12179}
12180
12181/* Handling opcode 011 insns. */
12182
12183static int
4748a9be 12184arm_record_ld_st_reg_offset (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
12185{
12186 struct regcache *reg_cache = arm_insn_r->regcache;
12187
12188 uint32_t shift_imm = 0;
12189 uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
12190 uint32_t offset_12 = 0, tgt_mem_addr = 0;
12191 uint32_t record_buf[8], record_buf_mem[8];
12192
12193 LONGEST s_word;
12194 ULONGEST u_regval[2];
12195
c55978a6
YQ
12196 if (bit (arm_insn_r->arm_insn, 4))
12197 return arm_record_media (arm_insn_r);
12198
72508ac0
PO
12199 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
12200 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
12201
12202 /* Handle enhanced store insns and LDRD DSP insn,
12203 order begins according to addressing modes for store insns
12204 STRH insn. */
12205
12206 /* LDR or STR? */
12207 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12208 {
12209 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
12210 /* LDR insn has a capability to do branching, if
dda83cd7
SM
12211 MOV LR, PC is preceded by LDR insn having Rn as R15
12212 in that case, it emulates branch and link insn, and hence we
12213 need to save CSPR and PC as well. */
72508ac0 12214 if (15 != reg_dest)
dda83cd7
SM
12215 {
12216 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12217 arm_insn_r->reg_rec_count = 1;
12218 }
72508ac0 12219 else
dda83cd7
SM
12220 {
12221 record_buf[0] = reg_dest;
12222 record_buf[1] = ARM_PS_REGNUM;
12223 arm_insn_r->reg_rec_count = 2;
12224 }
72508ac0
PO
12225 }
12226 else
12227 {
12228 if (! bits (arm_insn_r->arm_insn, 4, 11))
dda83cd7
SM
12229 {
12230 /* Store insn, register offset and register pre-indexed,
12231 register post-indexed. */
12232 /* Get Rm. */
12233 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
12234 /* Get Rn. */
12235 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
12236 regcache_raw_read_unsigned (reg_cache, reg_src1
12237 , &u_regval[0]);
12238 regcache_raw_read_unsigned (reg_cache, reg_src2
12239 , &u_regval[1]);
12240 if (15 == reg_src2)
12241 {
12242 /* If R15 was used as Rn, hence current PC+8. */
12243 /* Pre-indexed mode doesnt reach here ; illegal insn. */
12244 u_regval[0] = u_regval[0] + 8;
12245 }
12246 /* Calculate target store address, Rn +/- Rm, register offset. */
12247 /* U == 1. */
12248 if (bit (arm_insn_r->arm_insn, 23))
12249 {
12250 tgt_mem_addr = u_regval[0] + u_regval[1];
12251 }
12252 else
12253 {
12254 tgt_mem_addr = u_regval[1] - u_regval[0];
12255 }
12256
12257 switch (arm_insn_r->opcode)
12258 {
12259 /* STR. */
12260 case 8:
12261 case 12:
12262 /* STR. */
12263 case 9:
12264 case 13:
12265 /* STRT. */
12266 case 1:
12267 case 5:
12268 /* STR. */
12269 case 0:
12270 case 4:
12271 record_buf_mem[0] = 4;
12272 break;
12273
12274 /* STRB. */
12275 case 10:
12276 case 14:
12277 /* STRB. */
12278 case 11:
12279 case 15:
12280 /* STRBT. */
12281 case 3:
12282 case 7:
12283 /* STRB. */
12284 case 2:
12285 case 6:
12286 record_buf_mem[0] = 1;
12287 break;
12288
12289 default:
12290 gdb_assert_not_reached ("no decoding pattern found");
12291 break;
12292 }
12293 record_buf_mem[1] = tgt_mem_addr;
12294 arm_insn_r->mem_rec_count = 1;
12295
12296 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
12297 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
12298 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
12299 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
12300 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
12301 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
12302 )
12303 {
12304 /* Rn is going to be changed in pre-indexed mode and
12305 post-indexed mode as well. */
12306 record_buf[0] = reg_src2;
12307 arm_insn_r->reg_rec_count = 1;
12308 }
12309 }
72508ac0 12310 else
dda83cd7
SM
12311 {
12312 /* Store insn, scaled register offset; scaled pre-indexed. */
12313 offset_12 = bits (arm_insn_r->arm_insn, 5, 6);
12314 /* Get Rm. */
12315 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
12316 /* Get Rn. */
12317 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
12318 /* Get shift_imm. */
12319 shift_imm = bits (arm_insn_r->arm_insn, 7, 11);
12320 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
12321 regcache_raw_read_signed (reg_cache, reg_src1, &s_word);
12322 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
12323 /* Offset_12 used as shift. */
12324 switch (offset_12)
12325 {
12326 case 0:
12327 /* Offset_12 used as index. */
12328 offset_12 = u_regval[0] << shift_imm;
12329 break;
12330
12331 case 1:
05d63baf 12332 offset_12 = (!shift_imm) ? 0 : u_regval[0] >> shift_imm;
dda83cd7
SM
12333 break;
12334
12335 case 2:
12336 if (!shift_imm)
12337 {
12338 if (bit (u_regval[0], 31))
12339 {
12340 offset_12 = 0xFFFFFFFF;
12341 }
12342 else
12343 {
12344 offset_12 = 0;
12345 }
12346 }
12347 else
12348 {
12349 /* This is arithmetic shift. */
12350 offset_12 = s_word >> shift_imm;
12351 }
12352 break;
12353
12354 case 3:
12355 if (!shift_imm)
12356 {
12357 regcache_raw_read_unsigned (reg_cache, ARM_PS_REGNUM,
12358 &u_regval[1]);
12359 /* Get C flag value and shift it by 31. */
12360 offset_12 = (((bit (u_regval[1], 29)) << 31) \
12361 | (u_regval[0]) >> 1);
12362 }
12363 else
12364 {
12365 offset_12 = (u_regval[0] >> shift_imm) \
12366 | (u_regval[0] <<
12367 (sizeof(uint32_t) - shift_imm));
12368 }
12369 break;
12370
12371 default:
12372 gdb_assert_not_reached ("no decoding pattern found");
12373 break;
12374 }
12375
12376 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
12377 /* bit U set. */
12378 if (bit (arm_insn_r->arm_insn, 23))
12379 {
12380 tgt_mem_addr = u_regval[1] + offset_12;
12381 }
12382 else
12383 {
12384 tgt_mem_addr = u_regval[1] - offset_12;
12385 }
12386
12387 switch (arm_insn_r->opcode)
12388 {
12389 /* STR. */
12390 case 8:
12391 case 12:
12392 /* STR. */
12393 case 9:
12394 case 13:
12395 /* STRT. */
12396 case 1:
12397 case 5:
12398 /* STR. */
12399 case 0:
12400 case 4:
12401 record_buf_mem[0] = 4;
12402 break;
12403
12404 /* STRB. */
12405 case 10:
12406 case 14:
12407 /* STRB. */
12408 case 11:
12409 case 15:
12410 /* STRBT. */
12411 case 3:
12412 case 7:
12413 /* STRB. */
12414 case 2:
12415 case 6:
12416 record_buf_mem[0] = 1;
12417 break;
12418
12419 default:
12420 gdb_assert_not_reached ("no decoding pattern found");
12421 break;
12422 }
12423 record_buf_mem[1] = tgt_mem_addr;
12424 arm_insn_r->mem_rec_count = 1;
12425
12426 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
12427 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
12428 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
12429 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
12430 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
12431 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
12432 )
12433 {
12434 /* Rn is going to be changed in register scaled pre-indexed
12435 mode,and scaled post indexed mode. */
12436 record_buf[0] = reg_src2;
12437 arm_insn_r->reg_rec_count = 1;
12438 }
12439 }
72508ac0
PO
12440 }
12441
12442 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12443 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12444 return 0;
12445}
12446
71e396f9 12447/* Handle ARM mode instructions with opcode 100. */
72508ac0
PO
12448
12449static int
4748a9be 12450arm_record_ld_st_multiple (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
12451{
12452 struct regcache *reg_cache = arm_insn_r->regcache;
71e396f9
LM
12453 uint32_t register_count = 0, register_bits;
12454 uint32_t reg_base, addr_mode;
72508ac0 12455 uint32_t record_buf[24], record_buf_mem[48];
71e396f9
LM
12456 uint32_t wback;
12457 ULONGEST u_regval;
72508ac0 12458
71e396f9
LM
12459 /* Fetch the list of registers. */
12460 register_bits = bits (arm_insn_r->arm_insn, 0, 15);
12461 arm_insn_r->reg_rec_count = 0;
12462
12463 /* Fetch the base register that contains the address we are loading data
12464 to. */
12465 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0 12466
71e396f9
LM
12467 /* Calculate wback. */
12468 wback = (bit (arm_insn_r->arm_insn, 21) == 1);
72508ac0
PO
12469
12470 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12471 {
71e396f9 12472 /* LDM/LDMIA/LDMFD, LDMDA/LDMFA, LDMDB and LDMIB. */
72508ac0 12473
71e396f9 12474 /* Find out which registers are going to be loaded from memory. */
72508ac0 12475 while (register_bits)
71e396f9
LM
12476 {
12477 if (register_bits & 0x00000001)
12478 record_buf[arm_insn_r->reg_rec_count++] = register_count;
12479 register_bits = register_bits >> 1;
12480 register_count++;
12481 }
72508ac0 12482
71e396f9
LM
12483
12484 /* If wback is true, also save the base register, which is going to be
12485 written to. */
12486 if (wback)
12487 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
12488
12489 /* Save the CPSR register. */
12490 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
72508ac0
PO
12491 }
12492 else
12493 {
71e396f9 12494 /* STM (STMIA, STMEA), STMDA (STMED), STMDB (STMFD) and STMIB (STMFA). */
72508ac0 12495
71e396f9
LM
12496 addr_mode = bits (arm_insn_r->arm_insn, 23, 24);
12497
12498 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
12499
12500 /* Find out how many registers are going to be stored to memory. */
72508ac0 12501 while (register_bits)
71e396f9
LM
12502 {
12503 if (register_bits & 0x00000001)
12504 register_count++;
12505 register_bits = register_bits >> 1;
12506 }
72508ac0
PO
12507
12508 switch (addr_mode)
71e396f9
LM
12509 {
12510 /* STMDA (STMED): Decrement after. */
12511 case 0:
12512 record_buf_mem[1] = (uint32_t) u_regval
f0452268 12513 - register_count * ARM_INT_REGISTER_SIZE + 4;
71e396f9
LM
12514 break;
12515 /* STM (STMIA, STMEA): Increment after. */
12516 case 1:
12517 record_buf_mem[1] = (uint32_t) u_regval;
12518 break;
12519 /* STMDB (STMFD): Decrement before. */
12520 case 2:
12521 record_buf_mem[1] = (uint32_t) u_regval
f0452268 12522 - register_count * ARM_INT_REGISTER_SIZE;
71e396f9
LM
12523 break;
12524 /* STMIB (STMFA): Increment before. */
12525 case 3:
f0452268 12526 record_buf_mem[1] = (uint32_t) u_regval + ARM_INT_REGISTER_SIZE;
71e396f9
LM
12527 break;
12528 default:
12529 gdb_assert_not_reached ("no decoding pattern found");
12530 break;
12531 }
72508ac0 12532
f0452268 12533 record_buf_mem[0] = register_count * ARM_INT_REGISTER_SIZE;
71e396f9
LM
12534 arm_insn_r->mem_rec_count = 1;
12535
12536 /* If wback is true, also save the base register, which is going to be
12537 written to. */
12538 if (wback)
12539 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
12540 }
12541
12542 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12543 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12544 return 0;
12545}
12546
12547/* Handling opcode 101 insns. */
12548
12549static int
4748a9be 12550arm_record_b_bl (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
12551{
12552 uint32_t record_buf[8];
12553
12554 /* Handle B, BL, BLX(1) insns. */
12555 /* B simply branches so we do nothing here. */
12556 /* Note: BLX(1) doesnt fall here but instead it falls into
12557 extension space. */
12558 if (bit (arm_insn_r->arm_insn, 24))
01add95b
SM
12559 {
12560 record_buf[0] = ARM_LR_REGNUM;
12561 arm_insn_r->reg_rec_count = 1;
12562 }
72508ac0
PO
12563
12564 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12565
12566 return 0;
12567}
12568
72508ac0 12569static int
4748a9be 12570arm_record_unsupported_insn (arm_insn_decode_record *arm_insn_r)
72508ac0 12571{
6cb06a8c
TT
12572 gdb_printf (gdb_stderr,
12573 _("Process record does not support instruction "
12574 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
12575 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
72508ac0
PO
12576
12577 return -1;
12578}
12579
5a578da5
OJ
12580/* Record handler for vector data transfer instructions. */
12581
12582static int
4748a9be 12583arm_record_vdata_transfer_insn (arm_insn_decode_record *arm_insn_r)
5a578da5
OJ
12584{
12585 uint32_t bits_a, bit_c, bit_l, reg_t, reg_v;
12586 uint32_t record_buf[4];
12587
5a578da5
OJ
12588 reg_t = bits (arm_insn_r->arm_insn, 12, 15);
12589 reg_v = bits (arm_insn_r->arm_insn, 21, 23);
12590 bits_a = bits (arm_insn_r->arm_insn, 21, 23);
12591 bit_l = bit (arm_insn_r->arm_insn, 20);
12592 bit_c = bit (arm_insn_r->arm_insn, 8);
12593
12594 /* Handle VMOV instruction. */
12595 if (bit_l && bit_c)
12596 {
12597 record_buf[0] = reg_t;
12598 arm_insn_r->reg_rec_count = 1;
12599 }
12600 else if (bit_l && !bit_c)
12601 {
12602 /* Handle VMOV instruction. */
12603 if (bits_a == 0x00)
dda83cd7 12604 {
f1771dce 12605 record_buf[0] = reg_t;
dda83cd7
SM
12606 arm_insn_r->reg_rec_count = 1;
12607 }
5a578da5
OJ
12608 /* Handle VMRS instruction. */
12609 else if (bits_a == 0x07)
dda83cd7
SM
12610 {
12611 if (reg_t == 15)
12612 reg_t = ARM_PS_REGNUM;
5a578da5 12613
dda83cd7
SM
12614 record_buf[0] = reg_t;
12615 arm_insn_r->reg_rec_count = 1;
12616 }
5a578da5
OJ
12617 }
12618 else if (!bit_l && !bit_c)
12619 {
12620 /* Handle VMOV instruction. */
12621 if (bits_a == 0x00)
dda83cd7 12622 {
f1771dce 12623 record_buf[0] = ARM_D0_REGNUM + reg_v;
5a578da5 12624
dda83cd7
SM
12625 arm_insn_r->reg_rec_count = 1;
12626 }
5a578da5
OJ
12627 /* Handle VMSR instruction. */
12628 else if (bits_a == 0x07)
dda83cd7
SM
12629 {
12630 record_buf[0] = ARM_FPSCR_REGNUM;
12631 arm_insn_r->reg_rec_count = 1;
12632 }
5a578da5
OJ
12633 }
12634 else if (!bit_l && bit_c)
12635 {
12636 /* Handle VMOV instruction. */
12637 if (!(bits_a & 0x04))
dda83cd7
SM
12638 {
12639 record_buf[0] = (reg_v | (bit (arm_insn_r->arm_insn, 7) << 4))
12640 + ARM_D0_REGNUM;
12641 arm_insn_r->reg_rec_count = 1;
12642 }
5a578da5
OJ
12643 /* Handle VDUP instruction. */
12644 else
dda83cd7
SM
12645 {
12646 if (bit (arm_insn_r->arm_insn, 21))
12647 {
12648 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
12649 record_buf[0] = reg_v + ARM_D0_REGNUM;
12650 record_buf[1] = reg_v + ARM_D0_REGNUM + 1;
12651 arm_insn_r->reg_rec_count = 2;
12652 }
12653 else
12654 {
12655 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
12656 record_buf[0] = reg_v + ARM_D0_REGNUM;
12657 arm_insn_r->reg_rec_count = 1;
12658 }
12659 }
12660 }
12661
12662 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12663 return 0;
12664}
12665
f20f80dd
OJ
12666/* Record handler for extension register load/store instructions. */
12667
12668static int
4748a9be 12669arm_record_exreg_ld_st_insn (arm_insn_decode_record *arm_insn_r)
f20f80dd
OJ
12670{
12671 uint32_t opcode, single_reg;
12672 uint8_t op_vldm_vstm;
12673 uint32_t record_buf[8], record_buf_mem[128];
12674 ULONGEST u_regval = 0;
12675
12676 struct regcache *reg_cache = arm_insn_r->regcache;
f20f80dd
OJ
12677
12678 opcode = bits (arm_insn_r->arm_insn, 20, 24);
9fde51ed 12679 single_reg = !bit (arm_insn_r->arm_insn, 8);
f20f80dd
OJ
12680 op_vldm_vstm = opcode & 0x1b;
12681
12682 /* Handle VMOV instructions. */
12683 if ((opcode & 0x1e) == 0x04)
12684 {
9fde51ed 12685 if (bit (arm_insn_r->arm_insn, 20)) /* to_arm_registers bit 20? */
01e57735
YQ
12686 {
12687 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12688 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
12689 arm_insn_r->reg_rec_count = 2;
12690 }
f20f80dd 12691 else
01e57735 12692 {
9fde51ed
YQ
12693 uint8_t reg_m = bits (arm_insn_r->arm_insn, 0, 3);
12694 uint8_t bit_m = bit (arm_insn_r->arm_insn, 5);
f20f80dd 12695
9fde51ed 12696 if (single_reg)
01e57735 12697 {
9fde51ed
YQ
12698 /* The first S register number m is REG_M:M (M is bit 5),
12699 the corresponding D register number is REG_M:M / 2, which
12700 is REG_M. */
12701 record_buf[arm_insn_r->reg_rec_count++] = ARM_D0_REGNUM + reg_m;
12702 /* The second S register number is REG_M:M + 1, the
12703 corresponding D register number is (REG_M:M + 1) / 2.
12704 IOW, if bit M is 1, the first and second S registers
12705 are mapped to different D registers, otherwise, they are
12706 in the same D register. */
12707 if (bit_m)
12708 {
12709 record_buf[arm_insn_r->reg_rec_count++]
12710 = ARM_D0_REGNUM + reg_m + 1;
12711 }
01e57735
YQ
12712 }
12713 else
12714 {
9fde51ed 12715 record_buf[0] = ((bit_m << 4) + reg_m + ARM_D0_REGNUM);
01e57735
YQ
12716 arm_insn_r->reg_rec_count = 1;
12717 }
12718 }
f20f80dd
OJ
12719 }
12720 /* Handle VSTM and VPUSH instructions. */
12721 else if (op_vldm_vstm == 0x08 || op_vldm_vstm == 0x0a
01e57735 12722 || op_vldm_vstm == 0x12)
f20f80dd
OJ
12723 {
12724 uint32_t start_address, reg_rn, imm_off32, imm_off8, memory_count;
12725 uint32_t memory_index = 0;
12726
12727 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
12728 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12729 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
9fde51ed 12730 imm_off32 = imm_off8 << 2;
f20f80dd
OJ
12731 memory_count = imm_off8;
12732
12733 if (bit (arm_insn_r->arm_insn, 23))
01e57735 12734 start_address = u_regval;
f20f80dd 12735 else
01e57735 12736 start_address = u_regval - imm_off32;
f20f80dd
OJ
12737
12738 if (bit (arm_insn_r->arm_insn, 21))
01e57735
YQ
12739 {
12740 record_buf[0] = reg_rn;
12741 arm_insn_r->reg_rec_count = 1;
12742 }
f20f80dd
OJ
12743
12744 while (memory_count > 0)
01e57735 12745 {
9fde51ed 12746 if (single_reg)
01e57735 12747 {
9fde51ed
YQ
12748 record_buf_mem[memory_index] = 4;
12749 record_buf_mem[memory_index + 1] = start_address;
01e57735
YQ
12750 start_address = start_address + 4;
12751 memory_index = memory_index + 2;
12752 }
12753 else
12754 {
9fde51ed
YQ
12755 record_buf_mem[memory_index] = 4;
12756 record_buf_mem[memory_index + 1] = start_address;
12757 record_buf_mem[memory_index + 2] = 4;
12758 record_buf_mem[memory_index + 3] = start_address + 4;
01e57735
YQ
12759 start_address = start_address + 8;
12760 memory_index = memory_index + 4;
12761 }
12762 memory_count--;
12763 }
f20f80dd
OJ
12764 arm_insn_r->mem_rec_count = (memory_index >> 1);
12765 }
12766 /* Handle VLDM instructions. */
12767 else if (op_vldm_vstm == 0x09 || op_vldm_vstm == 0x0b
01e57735 12768 || op_vldm_vstm == 0x13)
f20f80dd
OJ
12769 {
12770 uint32_t reg_count, reg_vd;
12771 uint32_t reg_index = 0;
9fde51ed 12772 uint32_t bit_d = bit (arm_insn_r->arm_insn, 22);
f20f80dd
OJ
12773
12774 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12775 reg_count = bits (arm_insn_r->arm_insn, 0, 7);
12776
9fde51ed
YQ
12777 /* REG_VD is the first D register number. If the instruction
12778 loads memory to S registers (SINGLE_REG is TRUE), the register
12779 number is (REG_VD << 1 | bit D), so the corresponding D
12780 register number is (REG_VD << 1 | bit D) / 2 = REG_VD. */
12781 if (!single_reg)
12782 reg_vd = reg_vd | (bit_d << 4);
f20f80dd 12783
9fde51ed 12784 if (bit (arm_insn_r->arm_insn, 21) /* write back */)
01e57735 12785 record_buf[reg_index++] = bits (arm_insn_r->arm_insn, 16, 19);
f20f80dd 12786
9fde51ed
YQ
12787 /* If the instruction loads memory to D register, REG_COUNT should
12788 be divided by 2, according to the ARM Architecture Reference
12789 Manual. If the instruction loads memory to S register, divide by
12790 2 as well because two S registers are mapped to D register. */
12791 reg_count = reg_count / 2;
12792 if (single_reg && bit_d)
01e57735 12793 {
9fde51ed
YQ
12794 /* Increase the register count if S register list starts from
12795 an odd number (bit d is one). */
12796 reg_count++;
12797 }
f20f80dd 12798
9fde51ed
YQ
12799 while (reg_count > 0)
12800 {
12801 record_buf[reg_index++] = ARM_D0_REGNUM + reg_vd + reg_count - 1;
01e57735
YQ
12802 reg_count--;
12803 }
f20f80dd
OJ
12804 arm_insn_r->reg_rec_count = reg_index;
12805 }
12806 /* VSTR Vector store register. */
12807 else if ((opcode & 0x13) == 0x10)
12808 {
bec2ab5a 12809 uint32_t start_address, reg_rn, imm_off32, imm_off8;
f20f80dd
OJ
12810 uint32_t memory_index = 0;
12811
12812 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
12813 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12814 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
9fde51ed 12815 imm_off32 = imm_off8 << 2;
f20f80dd
OJ
12816
12817 if (bit (arm_insn_r->arm_insn, 23))
01e57735 12818 start_address = u_regval + imm_off32;
f20f80dd 12819 else
01e57735 12820 start_address = u_regval - imm_off32;
f20f80dd
OJ
12821
12822 if (single_reg)
01e57735 12823 {
9fde51ed
YQ
12824 record_buf_mem[memory_index] = 4;
12825 record_buf_mem[memory_index + 1] = start_address;
01e57735
YQ
12826 arm_insn_r->mem_rec_count = 1;
12827 }
f20f80dd 12828 else
01e57735 12829 {
9fde51ed
YQ
12830 record_buf_mem[memory_index] = 4;
12831 record_buf_mem[memory_index + 1] = start_address;
12832 record_buf_mem[memory_index + 2] = 4;
12833 record_buf_mem[memory_index + 3] = start_address + 4;
01e57735
YQ
12834 arm_insn_r->mem_rec_count = 2;
12835 }
f20f80dd
OJ
12836 }
12837 /* VLDR Vector load register. */
12838 else if ((opcode & 0x13) == 0x11)
12839 {
12840 uint32_t reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12841
12842 if (!single_reg)
01e57735
YQ
12843 {
12844 reg_vd = reg_vd | (bit (arm_insn_r->arm_insn, 22) << 4);
12845 record_buf[0] = ARM_D0_REGNUM + reg_vd;
12846 }
f20f80dd 12847 else
01e57735
YQ
12848 {
12849 reg_vd = (reg_vd << 1) | bit (arm_insn_r->arm_insn, 22);
9fde51ed
YQ
12850 /* Record register D rather than pseudo register S. */
12851 record_buf[0] = ARM_D0_REGNUM + reg_vd / 2;
01e57735 12852 }
f20f80dd
OJ
12853 arm_insn_r->reg_rec_count = 1;
12854 }
12855
12856 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12857 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12858 return 0;
12859}
12860
851f26ae
OJ
12861/* Record handler for arm/thumb mode VFP data processing instructions. */
12862
12863static int
4748a9be 12864arm_record_vfp_data_proc_insn (arm_insn_decode_record *arm_insn_r)
851f26ae
OJ
12865{
12866 uint32_t opc1, opc2, opc3, dp_op_sz, bit_d, reg_vd;
12867 uint32_t record_buf[4];
12868 enum insn_types {INSN_T0, INSN_T1, INSN_T2, INSN_T3, INSN_INV};
12869 enum insn_types curr_insn_type = INSN_INV;
12870
12871 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12872 opc1 = bits (arm_insn_r->arm_insn, 20, 23);
12873 opc2 = bits (arm_insn_r->arm_insn, 16, 19);
12874 opc3 = bits (arm_insn_r->arm_insn, 6, 7);
12875 dp_op_sz = bit (arm_insn_r->arm_insn, 8);
12876 bit_d = bit (arm_insn_r->arm_insn, 22);
ce887586
TT
12877 /* Mask off the "D" bit. */
12878 opc1 = opc1 & ~0x04;
851f26ae
OJ
12879
12880 /* Handle VMLA, VMLS. */
12881 if (opc1 == 0x00)
12882 {
12883 if (bit (arm_insn_r->arm_insn, 10))
dda83cd7
SM
12884 {
12885 if (bit (arm_insn_r->arm_insn, 6))
12886 curr_insn_type = INSN_T0;
12887 else
12888 curr_insn_type = INSN_T1;
12889 }
851f26ae 12890 else
dda83cd7
SM
12891 {
12892 if (dp_op_sz)
12893 curr_insn_type = INSN_T1;
12894 else
12895 curr_insn_type = INSN_T2;
12896 }
851f26ae
OJ
12897 }
12898 /* Handle VNMLA, VNMLS, VNMUL. */
12899 else if (opc1 == 0x01)
12900 {
12901 if (dp_op_sz)
dda83cd7 12902 curr_insn_type = INSN_T1;
851f26ae 12903 else
dda83cd7 12904 curr_insn_type = INSN_T2;
851f26ae
OJ
12905 }
12906 /* Handle VMUL. */
12907 else if (opc1 == 0x02 && !(opc3 & 0x01))
12908 {
12909 if (bit (arm_insn_r->arm_insn, 10))
dda83cd7
SM
12910 {
12911 if (bit (arm_insn_r->arm_insn, 6))
12912 curr_insn_type = INSN_T0;
12913 else
12914 curr_insn_type = INSN_T1;
12915 }
851f26ae 12916 else
dda83cd7
SM
12917 {
12918 if (dp_op_sz)
12919 curr_insn_type = INSN_T1;
12920 else
12921 curr_insn_type = INSN_T2;
12922 }
851f26ae
OJ
12923 }
12924 /* Handle VADD, VSUB. */
12925 else if (opc1 == 0x03)
12926 {
12927 if (!bit (arm_insn_r->arm_insn, 9))
dda83cd7
SM
12928 {
12929 if (bit (arm_insn_r->arm_insn, 6))
12930 curr_insn_type = INSN_T0;
12931 else
12932 curr_insn_type = INSN_T1;
12933 }
851f26ae 12934 else
dda83cd7
SM
12935 {
12936 if (dp_op_sz)
12937 curr_insn_type = INSN_T1;
12938 else
12939 curr_insn_type = INSN_T2;
12940 }
851f26ae
OJ
12941 }
12942 /* Handle VDIV. */
ce887586 12943 else if (opc1 == 0x08)
851f26ae
OJ
12944 {
12945 if (dp_op_sz)
dda83cd7 12946 curr_insn_type = INSN_T1;
851f26ae 12947 else
dda83cd7 12948 curr_insn_type = INSN_T2;
851f26ae
OJ
12949 }
12950 /* Handle all other vfp data processing instructions. */
12951 else if (opc1 == 0x0b)
12952 {
12953 /* Handle VMOV. */
12954 if (!(opc3 & 0x01) || (opc2 == 0x00 && opc3 == 0x01))
dda83cd7
SM
12955 {
12956 if (bit (arm_insn_r->arm_insn, 4))
12957 {
12958 if (bit (arm_insn_r->arm_insn, 6))
12959 curr_insn_type = INSN_T0;
12960 else
12961 curr_insn_type = INSN_T1;
12962 }
12963 else
12964 {
12965 if (dp_op_sz)
12966 curr_insn_type = INSN_T1;
12967 else
12968 curr_insn_type = INSN_T2;
12969 }
12970 }
851f26ae
OJ
12971 /* Handle VNEG and VABS. */
12972 else if ((opc2 == 0x01 && opc3 == 0x01)
dda83cd7
SM
12973 || (opc2 == 0x00 && opc3 == 0x03))
12974 {
12975 if (!bit (arm_insn_r->arm_insn, 11))
12976 {
12977 if (bit (arm_insn_r->arm_insn, 6))
12978 curr_insn_type = INSN_T0;
12979 else
12980 curr_insn_type = INSN_T1;
12981 }
12982 else
12983 {
12984 if (dp_op_sz)
12985 curr_insn_type = INSN_T1;
12986 else
12987 curr_insn_type = INSN_T2;
12988 }
12989 }
851f26ae
OJ
12990 /* Handle VSQRT. */
12991 else if (opc2 == 0x01 && opc3 == 0x03)
dda83cd7
SM
12992 {
12993 if (dp_op_sz)
12994 curr_insn_type = INSN_T1;
12995 else
12996 curr_insn_type = INSN_T2;
12997 }
851f26ae
OJ
12998 /* Handle VCVT. */
12999 else if (opc2 == 0x07 && opc3 == 0x03)
dda83cd7
SM
13000 {
13001 if (!dp_op_sz)
13002 curr_insn_type = INSN_T1;
13003 else
13004 curr_insn_type = INSN_T2;
13005 }
851f26ae 13006 else if (opc3 & 0x01)
dda83cd7
SM
13007 {
13008 /* Handle VCVT. */
13009 if ((opc2 == 0x08) || (opc2 & 0x0e) == 0x0c)
13010 {
13011 if (!bit (arm_insn_r->arm_insn, 18))
13012 curr_insn_type = INSN_T2;
13013 else
13014 {
13015 if (dp_op_sz)
13016 curr_insn_type = INSN_T1;
13017 else
13018 curr_insn_type = INSN_T2;
13019 }
13020 }
13021 /* Handle VCVT. */
13022 else if ((opc2 & 0x0e) == 0x0a || (opc2 & 0x0e) == 0x0e)
13023 {
13024 if (dp_op_sz)
13025 curr_insn_type = INSN_T1;
13026 else
13027 curr_insn_type = INSN_T2;
13028 }
13029 /* Handle VCVTB, VCVTT. */
13030 else if ((opc2 & 0x0e) == 0x02)
13031 curr_insn_type = INSN_T2;
13032 /* Handle VCMP, VCMPE. */
13033 else if ((opc2 & 0x0e) == 0x04)
13034 curr_insn_type = INSN_T3;
13035 }
851f26ae
OJ
13036 }
13037
13038 switch (curr_insn_type)
13039 {
13040 case INSN_T0:
dda83cd7
SM
13041 reg_vd = reg_vd | (bit_d << 4);
13042 record_buf[0] = reg_vd + ARM_D0_REGNUM;
13043 record_buf[1] = reg_vd + ARM_D0_REGNUM + 1;
13044 arm_insn_r->reg_rec_count = 2;
13045 break;
851f26ae
OJ
13046
13047 case INSN_T1:
dda83cd7
SM
13048 reg_vd = reg_vd | (bit_d << 4);
13049 record_buf[0] = reg_vd + ARM_D0_REGNUM;
13050 arm_insn_r->reg_rec_count = 1;
13051 break;
851f26ae
OJ
13052
13053 case INSN_T2:
dda83cd7
SM
13054 reg_vd = (reg_vd << 1) | bit_d;
13055 record_buf[0] = reg_vd + ARM_D0_REGNUM;
13056 arm_insn_r->reg_rec_count = 1;
13057 break;
851f26ae
OJ
13058
13059 case INSN_T3:
dda83cd7
SM
13060 record_buf[0] = ARM_FPSCR_REGNUM;
13061 arm_insn_r->reg_rec_count = 1;
13062 break;
851f26ae
OJ
13063
13064 default:
dda83cd7
SM
13065 gdb_assert_not_reached ("no decoding pattern found");
13066 break;
851f26ae
OJ
13067 }
13068
13069 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
13070 return 0;
13071}
13072
60cc5e93
OJ
13073/* Handling opcode 110 insns. */
13074
13075static int
4748a9be 13076arm_record_asimd_vfp_coproc (arm_insn_decode_record *arm_insn_r)
60cc5e93 13077{
bec2ab5a 13078 uint32_t op1, op1_ebit, coproc;
60cc5e93
OJ
13079
13080 coproc = bits (arm_insn_r->arm_insn, 8, 11);
13081 op1 = bits (arm_insn_r->arm_insn, 20, 25);
13082 op1_ebit = bit (arm_insn_r->arm_insn, 20);
13083
13084 if ((coproc & 0x0e) == 0x0a)
13085 {
13086 /* Handle extension register ld/st instructions. */
13087 if (!(op1 & 0x20))
dda83cd7 13088 return arm_record_exreg_ld_st_insn (arm_insn_r);
60cc5e93
OJ
13089
13090 /* 64-bit transfers between arm core and extension registers. */
13091 if ((op1 & 0x3e) == 0x04)
dda83cd7 13092 return arm_record_exreg_ld_st_insn (arm_insn_r);
60cc5e93
OJ
13093 }
13094 else
13095 {
13096 /* Handle coprocessor ld/st instructions. */
13097 if (!(op1 & 0x3a))
dda83cd7
SM
13098 {
13099 /* Store. */
13100 if (!op1_ebit)
13101 return arm_record_unsupported_insn (arm_insn_r);
13102 else
13103 /* Load. */
13104 return arm_record_unsupported_insn (arm_insn_r);
13105 }
60cc5e93
OJ
13106
13107 /* Move to coprocessor from two arm core registers. */
13108 if (op1 == 0x4)
dda83cd7 13109 return arm_record_unsupported_insn (arm_insn_r);
60cc5e93
OJ
13110
13111 /* Move to two arm core registers from coprocessor. */
13112 if (op1 == 0x5)
dda83cd7
SM
13113 {
13114 uint32_t reg_t[2];
60cc5e93 13115
dda83cd7
SM
13116 reg_t[0] = bits (arm_insn_r->arm_insn, 12, 15);
13117 reg_t[1] = bits (arm_insn_r->arm_insn, 16, 19);
13118 arm_insn_r->reg_rec_count = 2;
60cc5e93 13119
dda83cd7
SM
13120 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, reg_t);
13121 return 0;
60cc5e93
OJ
13122 }
13123 }
13124 return arm_record_unsupported_insn (arm_insn_r);
13125}
13126
72508ac0
PO
13127/* Handling opcode 111 insns. */
13128
13129static int
4748a9be 13130arm_record_coproc_data_proc (arm_insn_decode_record *arm_insn_r)
72508ac0 13131{
2d9e6acb 13132 uint32_t op, op1_ebit, coproc, bits_24_25;
345bd07c 13133 arm_gdbarch_tdep *tdep
08106042 13134 = gdbarch_tdep<arm_gdbarch_tdep> (arm_insn_r->gdbarch);
72508ac0 13135 struct regcache *reg_cache = arm_insn_r->regcache;
72508ac0
PO
13136
13137 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 24, 27);
60cc5e93 13138 coproc = bits (arm_insn_r->arm_insn, 8, 11);
60cc5e93
OJ
13139 op1_ebit = bit (arm_insn_r->arm_insn, 20);
13140 op = bit (arm_insn_r->arm_insn, 4);
2d9e6acb 13141 bits_24_25 = bits (arm_insn_r->arm_insn, 24, 25);
97dfe206
OJ
13142
13143 /* Handle arm SWI/SVC system call instructions. */
2d9e6acb 13144 if (bits_24_25 == 0x3)
97dfe206
OJ
13145 {
13146 if (tdep->arm_syscall_record != NULL)
dda83cd7
SM
13147 {
13148 ULONGEST svc_operand, svc_number;
97dfe206 13149
dda83cd7 13150 svc_operand = (0x00ffffff & arm_insn_r->arm_insn);
97dfe206 13151
dda83cd7
SM
13152 if (svc_operand) /* OABI. */
13153 svc_number = svc_operand - 0x900000;
13154 else /* EABI. */
13155 regcache_raw_read_unsigned (reg_cache, 7, &svc_number);
97dfe206 13156
dda83cd7
SM
13157 return tdep->arm_syscall_record (reg_cache, svc_number);
13158 }
97dfe206 13159 else
dda83cd7 13160 {
6cb06a8c 13161 gdb_printf (gdb_stderr, _("no syscall record support\n"));
dda83cd7
SM
13162 return -1;
13163 }
97dfe206 13164 }
2d9e6acb 13165 else if (bits_24_25 == 0x02)
60cc5e93 13166 {
2d9e6acb
YQ
13167 if (op)
13168 {
13169 if ((coproc & 0x0e) == 0x0a)
13170 {
13171 /* 8, 16, and 32-bit transfer */
13172 return arm_record_vdata_transfer_insn (arm_insn_r);
13173 }
13174 else
13175 {
13176 if (op1_ebit)
13177 {
13178 /* MRC, MRC2 */
13179 uint32_t record_buf[1];
13180
13181 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
13182 if (record_buf[0] == 15)
13183 record_buf[0] = ARM_PS_REGNUM;
60cc5e93 13184
2d9e6acb
YQ
13185 arm_insn_r->reg_rec_count = 1;
13186 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count,
13187 record_buf);
13188 return 0;
13189 }
13190 else
13191 {
13192 /* MCR, MCR2 */
13193 return -1;
13194 }
13195 }
13196 }
13197 else
13198 {
13199 if ((coproc & 0x0e) == 0x0a)
13200 {
13201 /* VFP data-processing instructions. */
13202 return arm_record_vfp_data_proc_insn (arm_insn_r);
13203 }
13204 else
13205 {
13206 /* CDP, CDP2 */
13207 return -1;
13208 }
13209 }
60cc5e93 13210 }
97dfe206
OJ
13211 else
13212 {
2d9e6acb 13213 unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 25);
60cc5e93 13214
2d9e6acb
YQ
13215 if (op1 == 5)
13216 {
13217 if ((coproc & 0x0e) != 0x0a)
13218 {
13219 /* MRRC, MRRC2 */
13220 return -1;
13221 }
13222 }
13223 else if (op1 == 4 || op1 == 5)
13224 {
13225 if ((coproc & 0x0e) == 0x0a)
13226 {
13227 /* 64-bit transfers between ARM core and extension */
13228 return -1;
13229 }
13230 else if (op1 == 4)
13231 {
13232 /* MCRR, MCRR2 */
13233 return -1;
13234 }
13235 }
13236 else if (op1 == 0 || op1 == 1)
13237 {
13238 /* UNDEFINED */
13239 return -1;
13240 }
13241 else
13242 {
13243 if ((coproc & 0x0e) == 0x0a)
13244 {
13245 /* Extension register load/store */
13246 }
13247 else
13248 {
13249 /* STC, STC2, LDC, LDC2 */
13250 }
13251 return -1;
13252 }
97dfe206 13253 }
72508ac0 13254
2d9e6acb 13255 return -1;
72508ac0
PO
13256}
13257
13258/* Handling opcode 000 insns. */
13259
13260static int
4748a9be 13261thumb_record_shift_add_sub (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13262{
13263 uint32_t record_buf[8];
13264 uint32_t reg_src1 = 0;
13265
13266 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13267
13268 record_buf[0] = ARM_PS_REGNUM;
13269 record_buf[1] = reg_src1;
13270 thumb_insn_r->reg_rec_count = 2;
13271
13272 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13273
13274 return 0;
13275}
13276
13277
13278/* Handling opcode 001 insns. */
13279
13280static int
4748a9be 13281thumb_record_add_sub_cmp_mov (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13282{
13283 uint32_t record_buf[8];
13284 uint32_t reg_src1 = 0;
13285
13286 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13287
13288 record_buf[0] = ARM_PS_REGNUM;
13289 record_buf[1] = reg_src1;
13290 thumb_insn_r->reg_rec_count = 2;
13291
13292 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13293
13294 return 0;
13295}
13296
13297/* Handling opcode 010 insns. */
13298
13299static int
4748a9be 13300thumb_record_ld_st_reg_offset (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13301{
13302 struct regcache *reg_cache = thumb_insn_r->regcache;
13303 uint32_t record_buf[8], record_buf_mem[8];
13304
13305 uint32_t reg_src1 = 0, reg_src2 = 0;
13306 uint32_t opcode1 = 0, opcode2 = 0, opcode3 = 0;
13307
13308 ULONGEST u_regval[2] = {0};
13309
13310 opcode1 = bits (thumb_insn_r->arm_insn, 10, 12);
13311
13312 if (bit (thumb_insn_r->arm_insn, 12))
13313 {
13314 /* Handle load/store register offset. */
b121eeb9
YQ
13315 uint32_t opB = bits (thumb_insn_r->arm_insn, 9, 11);
13316
b020ff80 13317 if (in_inclusive_range (opB, 4U, 7U))
dda83cd7
SM
13318 {
13319 /* LDR(2), LDRB(2) , LDRH(2), LDRSB, LDRSH. */
13320 reg_src1 = bits (thumb_insn_r->arm_insn,0, 2);
13321 record_buf[0] = reg_src1;
13322 thumb_insn_r->reg_rec_count = 1;
13323 }
b020ff80 13324 else if (in_inclusive_range (opB, 0U, 2U))
dda83cd7
SM
13325 {
13326 /* STR(2), STRB(2), STRH(2) . */
13327 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
13328 reg_src2 = bits (thumb_insn_r->arm_insn, 6, 8);
13329 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
13330 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
13331 if (0 == opB)
13332 record_buf_mem[0] = 4; /* STR (2). */
13333 else if (2 == opB)
13334 record_buf_mem[0] = 1; /* STRB (2). */
13335 else if (1 == opB)
13336 record_buf_mem[0] = 2; /* STRH (2). */
13337 record_buf_mem[1] = u_regval[0] + u_regval[1];
13338 thumb_insn_r->mem_rec_count = 1;
13339 }
72508ac0
PO
13340 }
13341 else if (bit (thumb_insn_r->arm_insn, 11))
13342 {
13343 /* Handle load from literal pool. */
13344 /* LDR(3). */
13345 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13346 record_buf[0] = reg_src1;
13347 thumb_insn_r->reg_rec_count = 1;
13348 }
13349 else if (opcode1)
13350 {
b121eeb9 13351 /* Special data instructions and branch and exchange */
72508ac0
PO
13352 opcode2 = bits (thumb_insn_r->arm_insn, 8, 9);
13353 opcode3 = bits (thumb_insn_r->arm_insn, 0, 2);
13354 if ((3 == opcode2) && (!opcode3))
dda83cd7
SM
13355 {
13356 /* Branch with exchange. */
13357 record_buf[0] = ARM_PS_REGNUM;
13358 thumb_insn_r->reg_rec_count = 1;
13359 }
72508ac0 13360 else
dda83cd7 13361 {
1f33efec
YQ
13362 /* Format 8; special data processing insns. */
13363 record_buf[0] = ARM_PS_REGNUM;
13364 record_buf[1] = (bit (thumb_insn_r->arm_insn, 7) << 3
13365 | bits (thumb_insn_r->arm_insn, 0, 2));
dda83cd7
SM
13366 thumb_insn_r->reg_rec_count = 2;
13367 }
72508ac0
PO
13368 }
13369 else
13370 {
13371 /* Format 5; data processing insns. */
13372 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13373 if (bit (thumb_insn_r->arm_insn, 7))
dda83cd7
SM
13374 {
13375 reg_src1 = reg_src1 + 8;
13376 }
72508ac0
PO
13377 record_buf[0] = ARM_PS_REGNUM;
13378 record_buf[1] = reg_src1;
13379 thumb_insn_r->reg_rec_count = 2;
13380 }
13381
13382 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13383 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13384 record_buf_mem);
72508ac0
PO
13385
13386 return 0;
13387}
13388
13389/* Handling opcode 001 insns. */
13390
13391static int
4748a9be 13392thumb_record_ld_st_imm_offset (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13393{
13394 struct regcache *reg_cache = thumb_insn_r->regcache;
13395 uint32_t record_buf[8], record_buf_mem[8];
13396
13397 uint32_t reg_src1 = 0;
13398 uint32_t opcode = 0, immed_5 = 0;
13399
13400 ULONGEST u_regval = 0;
13401
13402 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
13403
13404 if (opcode)
13405 {
13406 /* LDR(1). */
13407 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13408 record_buf[0] = reg_src1;
13409 thumb_insn_r->reg_rec_count = 1;
13410 }
13411 else
13412 {
13413 /* STR(1). */
13414 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
13415 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
13416 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
13417 record_buf_mem[0] = 4;
13418 record_buf_mem[1] = u_regval + (immed_5 * 4);
13419 thumb_insn_r->mem_rec_count = 1;
13420 }
13421
13422 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13423 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13424 record_buf_mem);
72508ac0
PO
13425
13426 return 0;
13427}
13428
13429/* Handling opcode 100 insns. */
13430
13431static int
4748a9be 13432thumb_record_ld_st_stack (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13433{
13434 struct regcache *reg_cache = thumb_insn_r->regcache;
13435 uint32_t record_buf[8], record_buf_mem[8];
13436
13437 uint32_t reg_src1 = 0;
13438 uint32_t opcode = 0, immed_8 = 0, immed_5 = 0;
13439
13440 ULONGEST u_regval = 0;
13441
13442 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
13443
13444 if (3 == opcode)
13445 {
13446 /* LDR(4). */
13447 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13448 record_buf[0] = reg_src1;
13449 thumb_insn_r->reg_rec_count = 1;
13450 }
13451 else if (1 == opcode)
13452 {
13453 /* LDRH(1). */
13454 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13455 record_buf[0] = reg_src1;
13456 thumb_insn_r->reg_rec_count = 1;
13457 }
13458 else if (2 == opcode)
13459 {
13460 /* STR(3). */
13461 immed_8 = bits (thumb_insn_r->arm_insn, 0, 7);
13462 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
13463 record_buf_mem[0] = 4;
13464 record_buf_mem[1] = u_regval + (immed_8 * 4);
13465 thumb_insn_r->mem_rec_count = 1;
13466 }
13467 else if (0 == opcode)
13468 {
13469 /* STRH(1). */
13470 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
13471 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
13472 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
13473 record_buf_mem[0] = 2;
13474 record_buf_mem[1] = u_regval + (immed_5 * 2);
13475 thumb_insn_r->mem_rec_count = 1;
13476 }
13477
13478 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13479 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13480 record_buf_mem);
72508ac0
PO
13481
13482 return 0;
13483}
13484
13485/* Handling opcode 101 insns. */
13486
13487static int
4748a9be 13488thumb_record_misc (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13489{
13490 struct regcache *reg_cache = thumb_insn_r->regcache;
13491
b121eeb9 13492 uint32_t opcode = 0;
72508ac0 13493 uint32_t register_bits = 0, register_count = 0;
bec2ab5a 13494 uint32_t index = 0, start_address = 0;
72508ac0
PO
13495 uint32_t record_buf[24], record_buf_mem[48];
13496 uint32_t reg_src1;
13497
13498 ULONGEST u_regval = 0;
13499
13500 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
72508ac0 13501
b121eeb9 13502 if (opcode == 0 || opcode == 1)
72508ac0 13503 {
b121eeb9
YQ
13504 /* ADR and ADD (SP plus immediate) */
13505
72508ac0
PO
13506 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13507 record_buf[0] = reg_src1;
13508 thumb_insn_r->reg_rec_count = 1;
13509 }
b121eeb9 13510 else
72508ac0 13511 {
b121eeb9
YQ
13512 /* Miscellaneous 16-bit instructions */
13513 uint32_t opcode2 = bits (thumb_insn_r->arm_insn, 8, 11);
13514
13515 switch (opcode2)
13516 {
13517 case 6:
13518 /* SETEND and CPS */
13519 break;
13520 case 0:
13521 /* ADD/SUB (SP plus immediate) */
13522 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13523 record_buf[0] = ARM_SP_REGNUM;
13524 thumb_insn_r->reg_rec_count = 1;
13525 break;
13526 case 1: /* fall through */
13527 case 3: /* fall through */
13528 case 9: /* fall through */
13529 case 11:
13530 /* CBNZ, CBZ */
b121eeb9
YQ
13531 break;
13532 case 2:
13533 /* SXTH, SXTB, UXTH, UXTB */
13534 record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
13535 thumb_insn_r->reg_rec_count = 1;
13536 break;
13537 case 4: /* fall through */
13538 case 5:
13539 /* PUSH. */
13540 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13541 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
13542 while (register_bits)
13543 {
13544 if (register_bits & 0x00000001)
13545 register_count++;
13546 register_bits = register_bits >> 1;
13547 }
13548 start_address = u_regval - \
13549 (4 * (bit (thumb_insn_r->arm_insn, 8) + register_count));
13550 thumb_insn_r->mem_rec_count = register_count;
13551 while (register_count)
13552 {
13553 record_buf_mem[(register_count * 2) - 1] = start_address;
13554 record_buf_mem[(register_count * 2) - 2] = 4;
13555 start_address = start_address + 4;
13556 register_count--;
13557 }
13558 record_buf[0] = ARM_SP_REGNUM;
13559 thumb_insn_r->reg_rec_count = 1;
13560 break;
13561 case 10:
13562 /* REV, REV16, REVSH */
ba14f379
YQ
13563 record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
13564 thumb_insn_r->reg_rec_count = 1;
b121eeb9
YQ
13565 break;
13566 case 12: /* fall through */
13567 case 13:
13568 /* POP. */
13569 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13570 while (register_bits)
13571 {
13572 if (register_bits & 0x00000001)
13573 record_buf[index++] = register_count;
13574 register_bits = register_bits >> 1;
13575 register_count++;
13576 }
13577 record_buf[index++] = ARM_PS_REGNUM;
13578 record_buf[index++] = ARM_SP_REGNUM;
13579 thumb_insn_r->reg_rec_count = index;
13580 break;
13581 case 0xe:
13582 /* BKPT insn. */
13583 /* Handle enhanced software breakpoint insn, BKPT. */
13584 /* CPSR is changed to be executed in ARM state, disabling normal
13585 interrupts, entering abort mode. */
13586 /* According to high vector configuration PC is set. */
13587 /* User hits breakpoint and type reverse, in that case, we need to go back with
13588 previous CPSR and Program Counter. */
13589 record_buf[0] = ARM_PS_REGNUM;
13590 record_buf[1] = ARM_LR_REGNUM;
13591 thumb_insn_r->reg_rec_count = 2;
13592 /* We need to save SPSR value, which is not yet done. */
6cb06a8c
TT
13593 gdb_printf (gdb_stderr,
13594 _("Process record does not support instruction "
13595 "0x%0x at address %s.\n"),
13596 thumb_insn_r->arm_insn,
13597 paddress (thumb_insn_r->gdbarch,
13598 thumb_insn_r->this_addr));
b121eeb9
YQ
13599 return -1;
13600
13601 case 0xf:
13602 /* If-Then, and hints */
13603 break;
13604 default:
13605 return -1;
13606 };
72508ac0
PO
13607 }
13608
13609 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13610 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13611 record_buf_mem);
72508ac0
PO
13612
13613 return 0;
13614}
13615
13616/* Handling opcode 110 insns. */
13617
13618static int
4748a9be 13619thumb_record_ldm_stm_swi (arm_insn_decode_record *thumb_insn_r)
72508ac0 13620{
345bd07c 13621 arm_gdbarch_tdep *tdep
08106042 13622 = gdbarch_tdep<arm_gdbarch_tdep> (thumb_insn_r->gdbarch);
72508ac0
PO
13623 struct regcache *reg_cache = thumb_insn_r->regcache;
13624
13625 uint32_t ret = 0; /* function return value: -1:record failure ; 0:success */
13626 uint32_t reg_src1 = 0;
13627 uint32_t opcode1 = 0, opcode2 = 0, register_bits = 0, register_count = 0;
bec2ab5a 13628 uint32_t index = 0, start_address = 0;
72508ac0
PO
13629 uint32_t record_buf[24], record_buf_mem[48];
13630
13631 ULONGEST u_regval = 0;
13632
13633 opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
13634 opcode2 = bits (thumb_insn_r->arm_insn, 11, 12);
13635
13636 if (1 == opcode2)
13637 {
13638
13639 /* LDMIA. */
13640 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13641 /* Get Rn. */
13642 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13643 while (register_bits)
dda83cd7
SM
13644 {
13645 if (register_bits & 0x00000001)
13646 record_buf[index++] = register_count;
13647 register_bits = register_bits >> 1;
13648 register_count++;
13649 }
f969241e
OJ
13650 record_buf[index++] = reg_src1;
13651 thumb_insn_r->reg_rec_count = index;
72508ac0
PO
13652 }
13653 else if (0 == opcode2)
13654 {
13655 /* It handles both STMIA. */
13656 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13657 /* Get Rn. */
13658 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13659 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
13660 while (register_bits)
dda83cd7
SM
13661 {
13662 if (register_bits & 0x00000001)
13663 register_count++;
13664 register_bits = register_bits >> 1;
13665 }
72508ac0
PO
13666 start_address = u_regval;
13667 thumb_insn_r->mem_rec_count = register_count;
13668 while (register_count)
dda83cd7
SM
13669 {
13670 record_buf_mem[(register_count * 2) - 1] = start_address;
13671 record_buf_mem[(register_count * 2) - 2] = 4;
13672 start_address = start_address + 4;
13673 register_count--;
13674 }
72508ac0
PO
13675 }
13676 else if (0x1F == opcode1)
13677 {
dda83cd7
SM
13678 /* Handle arm syscall insn. */
13679 if (tdep->arm_syscall_record != NULL)
13680 {
13681 regcache_raw_read_unsigned (reg_cache, 7, &u_regval);
13682 ret = tdep->arm_syscall_record (reg_cache, u_regval);
13683 }
13684 else
13685 {
6cb06a8c 13686 gdb_printf (gdb_stderr, _("no syscall record support\n"));
dda83cd7
SM
13687 return -1;
13688 }
72508ac0
PO
13689 }
13690
13691 /* B (1), conditional branch is automatically taken care in process_record,
13692 as PC is saved there. */
13693
13694 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13695 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13696 record_buf_mem);
72508ac0
PO
13697
13698 return ret;
13699}
13700
13701/* Handling opcode 111 insns. */
13702
13703static int
4748a9be 13704thumb_record_branch (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13705{
13706 uint32_t record_buf[8];
13707 uint32_t bits_h = 0;
13708
13709 bits_h = bits (thumb_insn_r->arm_insn, 11, 12);
13710
13711 if (2 == bits_h || 3 == bits_h)
13712 {
13713 /* BL */
13714 record_buf[0] = ARM_LR_REGNUM;
13715 thumb_insn_r->reg_rec_count = 1;
13716 }
13717 else if (1 == bits_h)
13718 {
13719 /* BLX(1). */
13720 record_buf[0] = ARM_PS_REGNUM;
13721 record_buf[1] = ARM_LR_REGNUM;
13722 thumb_insn_r->reg_rec_count = 2;
13723 }
13724
13725 /* B(2) is automatically taken care in process_record, as PC is
13726 saved there. */
13727
13728 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13729
13730 return 0;
13731}
13732
c6ec2b30
OJ
13733/* Handler for thumb2 load/store multiple instructions. */
13734
13735static int
4748a9be 13736thumb2_record_ld_st_multiple (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13737{
13738 struct regcache *reg_cache = thumb2_insn_r->regcache;
13739
13740 uint32_t reg_rn, op;
13741 uint32_t register_bits = 0, register_count = 0;
13742 uint32_t index = 0, start_address = 0;
13743 uint32_t record_buf[24], record_buf_mem[48];
13744
13745 ULONGEST u_regval = 0;
13746
13747 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13748 op = bits (thumb2_insn_r->arm_insn, 23, 24);
13749
13750 if (0 == op || 3 == op)
13751 {
13752 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
dda83cd7
SM
13753 {
13754 /* Handle RFE instruction. */
13755 record_buf[0] = ARM_PS_REGNUM;
13756 thumb2_insn_r->reg_rec_count = 1;
13757 }
c6ec2b30 13758 else
dda83cd7
SM
13759 {
13760 /* Handle SRS instruction after reading banked SP. */
13761 return arm_record_unsupported_insn (thumb2_insn_r);
13762 }
c6ec2b30
OJ
13763 }
13764 else if (1 == op || 2 == op)
13765 {
13766 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
dda83cd7
SM
13767 {
13768 /* Handle LDM/LDMIA/LDMFD and LDMDB/LDMEA instructions. */
13769 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
13770 while (register_bits)
13771 {
13772 if (register_bits & 0x00000001)
13773 record_buf[index++] = register_count;
13774
13775 register_count++;
13776 register_bits = register_bits >> 1;
13777 }
13778 record_buf[index++] = reg_rn;
13779 record_buf[index++] = ARM_PS_REGNUM;
13780 thumb2_insn_r->reg_rec_count = index;
13781 }
c6ec2b30 13782 else
dda83cd7
SM
13783 {
13784 /* Handle STM/STMIA/STMEA and STMDB/STMFD. */
13785 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
13786 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
13787 while (register_bits)
13788 {
13789 if (register_bits & 0x00000001)
13790 register_count++;
13791
13792 register_bits = register_bits >> 1;
13793 }
13794
13795 if (1 == op)
13796 {
13797 /* Start address calculation for LDMDB/LDMEA. */
13798 start_address = u_regval;
13799 }
13800 else if (2 == op)
13801 {
13802 /* Start address calculation for LDMDB/LDMEA. */
13803 start_address = u_regval - register_count * 4;
13804 }
13805
13806 thumb2_insn_r->mem_rec_count = register_count;
13807 while (register_count)
13808 {
13809 record_buf_mem[register_count * 2 - 1] = start_address;
13810 record_buf_mem[register_count * 2 - 2] = 4;
13811 start_address = start_address + 4;
13812 register_count--;
13813 }
13814 record_buf[0] = reg_rn;
13815 record_buf[1] = ARM_PS_REGNUM;
13816 thumb2_insn_r->reg_rec_count = 2;
13817 }
c6ec2b30
OJ
13818 }
13819
13820 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
dda83cd7 13821 record_buf_mem);
c6ec2b30 13822 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13823 record_buf);
c6ec2b30
OJ
13824 return ARM_RECORD_SUCCESS;
13825}
13826
13827/* Handler for thumb2 load/store (dual/exclusive) and table branch
13828 instructions. */
13829
13830static int
4748a9be 13831thumb2_record_ld_st_dual_ex_tbb (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13832{
13833 struct regcache *reg_cache = thumb2_insn_r->regcache;
13834
13835 uint32_t reg_rd, reg_rn, offset_imm;
13836 uint32_t reg_dest1, reg_dest2;
13837 uint32_t address, offset_addr;
13838 uint32_t record_buf[8], record_buf_mem[8];
13839 uint32_t op1, op2, op3;
c6ec2b30
OJ
13840
13841 ULONGEST u_regval[2];
13842
13843 op1 = bits (thumb2_insn_r->arm_insn, 23, 24);
13844 op2 = bits (thumb2_insn_r->arm_insn, 20, 21);
13845 op3 = bits (thumb2_insn_r->arm_insn, 4, 7);
13846
13847 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
13848 {
13849 if(!(1 == op1 && 1 == op2 && (0 == op3 || 1 == op3)))
dda83cd7
SM
13850 {
13851 reg_dest1 = bits (thumb2_insn_r->arm_insn, 12, 15);
13852 record_buf[0] = reg_dest1;
13853 record_buf[1] = ARM_PS_REGNUM;
13854 thumb2_insn_r->reg_rec_count = 2;
13855 }
c6ec2b30
OJ
13856
13857 if (3 == op2 || (op1 & 2) || (1 == op1 && 1 == op2 && 7 == op3))
dda83cd7
SM
13858 {
13859 reg_dest2 = bits (thumb2_insn_r->arm_insn, 8, 11);
13860 record_buf[2] = reg_dest2;
13861 thumb2_insn_r->reg_rec_count = 3;
13862 }
c6ec2b30
OJ
13863 }
13864 else
13865 {
13866 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13867 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
13868
13869 if (0 == op1 && 0 == op2)
dda83cd7
SM
13870 {
13871 /* Handle STREX. */
13872 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13873 address = u_regval[0] + (offset_imm * 4);
13874 record_buf_mem[0] = 4;
13875 record_buf_mem[1] = address;
13876 thumb2_insn_r->mem_rec_count = 1;
13877 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
13878 record_buf[0] = reg_rd;
13879 thumb2_insn_r->reg_rec_count = 1;
13880 }
c6ec2b30 13881 else if (1 == op1 && 0 == op2)
dda83cd7
SM
13882 {
13883 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
13884 record_buf[0] = reg_rd;
13885 thumb2_insn_r->reg_rec_count = 1;
13886 address = u_regval[0];
13887 record_buf_mem[1] = address;
13888
13889 if (4 == op3)
13890 {
13891 /* Handle STREXB. */
13892 record_buf_mem[0] = 1;
13893 thumb2_insn_r->mem_rec_count = 1;
13894 }
13895 else if (5 == op3)
13896 {
13897 /* Handle STREXH. */
13898 record_buf_mem[0] = 2 ;
13899 thumb2_insn_r->mem_rec_count = 1;
13900 }
13901 else if (7 == op3)
13902 {
13903 /* Handle STREXD. */
13904 address = u_regval[0];
13905 record_buf_mem[0] = 4;
13906 record_buf_mem[2] = 4;
13907 record_buf_mem[3] = address + 4;
13908 thumb2_insn_r->mem_rec_count = 2;
13909 }
13910 }
c6ec2b30 13911 else
dda83cd7
SM
13912 {
13913 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13914
13915 if (bit (thumb2_insn_r->arm_insn, 24))
13916 {
13917 if (bit (thumb2_insn_r->arm_insn, 23))
13918 offset_addr = u_regval[0] + (offset_imm * 4);
13919 else
13920 offset_addr = u_regval[0] - (offset_imm * 4);
13921
13922 address = offset_addr;
13923 }
13924 else
13925 address = u_regval[0];
13926
13927 record_buf_mem[0] = 4;
13928 record_buf_mem[1] = address;
13929 record_buf_mem[2] = 4;
13930 record_buf_mem[3] = address + 4;
13931 thumb2_insn_r->mem_rec_count = 2;
13932 record_buf[0] = reg_rn;
13933 thumb2_insn_r->reg_rec_count = 1;
13934 }
c6ec2b30
OJ
13935 }
13936
13937 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13938 record_buf);
c6ec2b30 13939 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
dda83cd7 13940 record_buf_mem);
c6ec2b30
OJ
13941 return ARM_RECORD_SUCCESS;
13942}
13943
13944/* Handler for thumb2 data processing (shift register and modified immediate)
13945 instructions. */
13946
13947static int
4748a9be 13948thumb2_record_data_proc_sreg_mimm (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13949{
13950 uint32_t reg_rd, op;
13951 uint32_t record_buf[8];
13952
13953 op = bits (thumb2_insn_r->arm_insn, 21, 24);
13954 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
13955
13956 if ((0 == op || 4 == op || 8 == op || 13 == op) && 15 == reg_rd)
13957 {
13958 record_buf[0] = ARM_PS_REGNUM;
13959 thumb2_insn_r->reg_rec_count = 1;
13960 }
13961 else
13962 {
13963 record_buf[0] = reg_rd;
13964 record_buf[1] = ARM_PS_REGNUM;
13965 thumb2_insn_r->reg_rec_count = 2;
13966 }
13967
13968 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13969 record_buf);
c6ec2b30
OJ
13970 return ARM_RECORD_SUCCESS;
13971}
13972
13973/* Generic handler for thumb2 instructions which effect destination and PS
13974 registers. */
13975
13976static int
4748a9be 13977thumb2_record_ps_dest_generic (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13978{
13979 uint32_t reg_rd;
13980 uint32_t record_buf[8];
13981
13982 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
13983
13984 record_buf[0] = reg_rd;
13985 record_buf[1] = ARM_PS_REGNUM;
13986 thumb2_insn_r->reg_rec_count = 2;
13987
13988 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13989 record_buf);
c6ec2b30
OJ
13990 return ARM_RECORD_SUCCESS;
13991}
13992
13993/* Handler for thumb2 branch and miscellaneous control instructions. */
13994
13995static int
4748a9be 13996thumb2_record_branch_misc_cntrl (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13997{
13998 uint32_t op, op1, op2;
13999 uint32_t record_buf[8];
14000
14001 op = bits (thumb2_insn_r->arm_insn, 20, 26);
14002 op1 = bits (thumb2_insn_r->arm_insn, 12, 14);
14003 op2 = bits (thumb2_insn_r->arm_insn, 8, 11);
14004
14005 /* Handle MSR insn. */
14006 if (!(op1 & 0x2) && 0x38 == op)
14007 {
14008 if (!(op2 & 0x3))
dda83cd7
SM
14009 {
14010 /* CPSR is going to be changed. */
14011 record_buf[0] = ARM_PS_REGNUM;
14012 thumb2_insn_r->reg_rec_count = 1;
14013 }
c6ec2b30 14014 else
dda83cd7
SM
14015 {
14016 arm_record_unsupported_insn(thumb2_insn_r);
14017 return -1;
14018 }
c6ec2b30
OJ
14019 }
14020 else if (4 == (op1 & 0x5) || 5 == (op1 & 0x5))
14021 {
14022 /* BLX. */
14023 record_buf[0] = ARM_PS_REGNUM;
14024 record_buf[1] = ARM_LR_REGNUM;
14025 thumb2_insn_r->reg_rec_count = 2;
14026 }
14027
14028 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 14029 record_buf);
c6ec2b30
OJ
14030 return ARM_RECORD_SUCCESS;
14031}
14032
14033/* Handler for thumb2 store single data item instructions. */
14034
14035static int
4748a9be 14036thumb2_record_str_single_data (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
14037{
14038 struct regcache *reg_cache = thumb2_insn_r->regcache;
14039
14040 uint32_t reg_rn, reg_rm, offset_imm, shift_imm;
14041 uint32_t address, offset_addr;
14042 uint32_t record_buf[8], record_buf_mem[8];
14043 uint32_t op1, op2;
14044
14045 ULONGEST u_regval[2];
14046
14047 op1 = bits (thumb2_insn_r->arm_insn, 21, 23);
14048 op2 = bits (thumb2_insn_r->arm_insn, 6, 11);
14049 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
14050 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
14051
14052 if (bit (thumb2_insn_r->arm_insn, 23))
14053 {
14054 /* T2 encoding. */
14055 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 11);
14056 offset_addr = u_regval[0] + offset_imm;
14057 address = offset_addr;
14058 }
14059 else
14060 {
14061 /* T3 encoding. */
14062 if ((0 == op1 || 1 == op1 || 2 == op1) && !(op2 & 0x20))
dda83cd7
SM
14063 {
14064 /* Handle STRB (register). */
14065 reg_rm = bits (thumb2_insn_r->arm_insn, 0, 3);
14066 regcache_raw_read_unsigned (reg_cache, reg_rm, &u_regval[1]);
14067 shift_imm = bits (thumb2_insn_r->arm_insn, 4, 5);
14068 offset_addr = u_regval[1] << shift_imm;
14069 address = u_regval[0] + offset_addr;
14070 }
c6ec2b30 14071 else
dda83cd7
SM
14072 {
14073 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
14074 if (bit (thumb2_insn_r->arm_insn, 10))
14075 {
14076 if (bit (thumb2_insn_r->arm_insn, 9))
14077 offset_addr = u_regval[0] + offset_imm;
14078 else
14079 offset_addr = u_regval[0] - offset_imm;
14080
14081 address = offset_addr;
14082 }
14083 else
14084 address = u_regval[0];
14085 }
c6ec2b30
OJ
14086 }
14087
14088 switch (op1)
14089 {
14090 /* Store byte instructions. */
14091 case 4:
14092 case 0:
dda83cd7
SM
14093 record_buf_mem[0] = 1;
14094 break;
c6ec2b30
OJ
14095 /* Store half word instructions. */
14096 case 1:
14097 case 5:
dda83cd7
SM
14098 record_buf_mem[0] = 2;
14099 break;
c6ec2b30
OJ
14100 /* Store word instructions. */
14101 case 2:
14102 case 6:
dda83cd7
SM
14103 record_buf_mem[0] = 4;
14104 break;
c6ec2b30
OJ
14105
14106 default:
dda83cd7
SM
14107 gdb_assert_not_reached ("no decoding pattern found");
14108 break;
c6ec2b30
OJ
14109 }
14110
14111 record_buf_mem[1] = address;
14112 thumb2_insn_r->mem_rec_count = 1;
14113 record_buf[0] = reg_rn;
14114 thumb2_insn_r->reg_rec_count = 1;
14115
14116 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 14117 record_buf);
c6ec2b30 14118 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
dda83cd7 14119 record_buf_mem);
c6ec2b30
OJ
14120 return ARM_RECORD_SUCCESS;
14121}
14122
14123/* Handler for thumb2 load memory hints instructions. */
14124
14125static int
4748a9be 14126thumb2_record_ld_mem_hints (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
14127{
14128 uint32_t record_buf[8];
14129 uint32_t reg_rt, reg_rn;
14130
14131 reg_rt = bits (thumb2_insn_r->arm_insn, 12, 15);
14132 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
14133
14134 if (ARM_PC_REGNUM != reg_rt)
14135 {
14136 record_buf[0] = reg_rt;
14137 record_buf[1] = reg_rn;
14138 record_buf[2] = ARM_PS_REGNUM;
14139 thumb2_insn_r->reg_rec_count = 3;
14140
14141 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 14142 record_buf);
c6ec2b30
OJ
14143 return ARM_RECORD_SUCCESS;
14144 }
14145
14146 return ARM_RECORD_FAILURE;
14147}
14148
14149/* Handler for thumb2 load word instructions. */
14150
14151static int
4748a9be 14152thumb2_record_ld_word (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30 14153{
c6ec2b30
OJ
14154 uint32_t record_buf[8];
14155
14156 record_buf[0] = bits (thumb2_insn_r->arm_insn, 12, 15);
14157 record_buf[1] = ARM_PS_REGNUM;
14158 thumb2_insn_r->reg_rec_count = 2;
14159
14160 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 14161 record_buf);
c6ec2b30
OJ
14162 return ARM_RECORD_SUCCESS;
14163}
14164
14165/* Handler for thumb2 long multiply, long multiply accumulate, and
14166 divide instructions. */
14167
14168static int
4748a9be 14169thumb2_record_lmul_lmla_div (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
14170{
14171 uint32_t opcode1 = 0, opcode2 = 0;
14172 uint32_t record_buf[8];
c6ec2b30
OJ
14173
14174 opcode1 = bits (thumb2_insn_r->arm_insn, 20, 22);
14175 opcode2 = bits (thumb2_insn_r->arm_insn, 4, 7);
14176
14177 if (0 == opcode1 || 2 == opcode1 || (opcode1 >= 4 && opcode1 <= 6))
14178 {
14179 /* Handle SMULL, UMULL, SMULAL. */
14180 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
14181 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
14182 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
14183 record_buf[2] = ARM_PS_REGNUM;
14184 thumb2_insn_r->reg_rec_count = 3;
14185 }
14186 else if (1 == opcode1 || 3 == opcode2)
14187 {
14188 /* Handle SDIV and UDIV. */
14189 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
14190 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
14191 record_buf[2] = ARM_PS_REGNUM;
14192 thumb2_insn_r->reg_rec_count = 3;
14193 }
14194 else
14195 return ARM_RECORD_FAILURE;
14196
14197 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 14198 record_buf);
c6ec2b30
OJ
14199 return ARM_RECORD_SUCCESS;
14200}
14201
60cc5e93
OJ
14202/* Record handler for thumb32 coprocessor instructions. */
14203
14204static int
4748a9be 14205thumb2_record_coproc_insn (arm_insn_decode_record *thumb2_insn_r)
60cc5e93
OJ
14206{
14207 if (bit (thumb2_insn_r->arm_insn, 25))
14208 return arm_record_coproc_data_proc (thumb2_insn_r);
14209 else
14210 return arm_record_asimd_vfp_coproc (thumb2_insn_r);
14211}
14212
1e1b6563
OJ
14213/* Record handler for advance SIMD structure load/store instructions. */
14214
14215static int
4748a9be 14216thumb2_record_asimd_struct_ld_st (arm_insn_decode_record *thumb2_insn_r)
1e1b6563
OJ
14217{
14218 struct regcache *reg_cache = thumb2_insn_r->regcache;
14219 uint32_t l_bit, a_bit, b_bits;
14220 uint32_t record_buf[128], record_buf_mem[128];
bec2ab5a 14221 uint32_t reg_rn, reg_vd, address, f_elem;
1e1b6563
OJ
14222 uint32_t index_r = 0, index_e = 0, bf_regs = 0, index_m = 0, loop_t = 0;
14223 uint8_t f_ebytes;
14224
14225 l_bit = bit (thumb2_insn_r->arm_insn, 21);
14226 a_bit = bit (thumb2_insn_r->arm_insn, 23);
14227 b_bits = bits (thumb2_insn_r->arm_insn, 8, 11);
14228 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
14229 reg_vd = bits (thumb2_insn_r->arm_insn, 12, 15);
14230 reg_vd = (bit (thumb2_insn_r->arm_insn, 22) << 4) | reg_vd;
14231 f_ebytes = (1 << bits (thumb2_insn_r->arm_insn, 6, 7));
1e1b6563
OJ
14232 f_elem = 8 / f_ebytes;
14233
14234 if (!l_bit)
14235 {
14236 ULONGEST u_regval = 0;
14237 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
14238 address = u_regval;
14239
14240 if (!a_bit)
dda83cd7
SM
14241 {
14242 /* Handle VST1. */
14243 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
14244 {
14245 if (b_bits == 0x07)
14246 bf_regs = 1;
14247 else if (b_bits == 0x0a)
14248 bf_regs = 2;
14249 else if (b_bits == 0x06)
14250 bf_regs = 3;
14251 else if (b_bits == 0x02)
14252 bf_regs = 4;
14253 else
14254 bf_regs = 0;
14255
14256 for (index_r = 0; index_r < bf_regs; index_r++)
14257 {
14258 for (index_e = 0; index_e < f_elem; index_e++)
14259 {
14260 record_buf_mem[index_m++] = f_ebytes;
14261 record_buf_mem[index_m++] = address;
14262 address = address + f_ebytes;
14263 thumb2_insn_r->mem_rec_count += 1;
14264 }
14265 }
14266 }
14267 /* Handle VST2. */
14268 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
14269 {
14270 if (b_bits == 0x09 || b_bits == 0x08)
14271 bf_regs = 1;
14272 else if (b_bits == 0x03)
14273 bf_regs = 2;
14274 else
14275 bf_regs = 0;
14276
14277 for (index_r = 0; index_r < bf_regs; index_r++)
14278 for (index_e = 0; index_e < f_elem; index_e++)
14279 {
14280 for (loop_t = 0; loop_t < 2; loop_t++)
14281 {
14282 record_buf_mem[index_m++] = f_ebytes;
14283 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
14284 thumb2_insn_r->mem_rec_count += 1;
14285 }
14286 address = address + (2 * f_ebytes);
14287 }
14288 }
14289 /* Handle VST3. */
14290 else if ((b_bits & 0x0e) == 0x04)
14291 {
14292 for (index_e = 0; index_e < f_elem; index_e++)
14293 {
14294 for (loop_t = 0; loop_t < 3; loop_t++)
14295 {
14296 record_buf_mem[index_m++] = f_ebytes;
14297 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
14298 thumb2_insn_r->mem_rec_count += 1;
14299 }
14300 address = address + (3 * f_ebytes);
14301 }
14302 }
14303 /* Handle VST4. */
14304 else if (!(b_bits & 0x0e))
14305 {
14306 for (index_e = 0; index_e < f_elem; index_e++)
14307 {
14308 for (loop_t = 0; loop_t < 4; loop_t++)
14309 {
14310 record_buf_mem[index_m++] = f_ebytes;
14311 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
14312 thumb2_insn_r->mem_rec_count += 1;
14313 }
14314 address = address + (4 * f_ebytes);
14315 }
14316 }
14317 }
1e1b6563 14318 else
dda83cd7
SM
14319 {
14320 uint8_t bft_size = bits (thumb2_insn_r->arm_insn, 10, 11);
14321
14322 if (bft_size == 0x00)
14323 f_ebytes = 1;
14324 else if (bft_size == 0x01)
14325 f_ebytes = 2;
14326 else if (bft_size == 0x02)
14327 f_ebytes = 4;
14328 else
14329 f_ebytes = 0;
14330
14331 /* Handle VST1. */
14332 if (!(b_bits & 0x0b) || b_bits == 0x08)
14333 thumb2_insn_r->mem_rec_count = 1;
14334 /* Handle VST2. */
14335 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09)
14336 thumb2_insn_r->mem_rec_count = 2;
14337 /* Handle VST3. */
14338 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a)
14339 thumb2_insn_r->mem_rec_count = 3;
14340 /* Handle VST4. */
14341 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b)
14342 thumb2_insn_r->mem_rec_count = 4;
14343
14344 for (index_m = 0; index_m < thumb2_insn_r->mem_rec_count; index_m++)
14345 {
14346 record_buf_mem[index_m] = f_ebytes;
14347 record_buf_mem[index_m] = address + (index_m * f_ebytes);
14348 }
14349 }
1e1b6563
OJ
14350 }
14351 else
14352 {
14353 if (!a_bit)
dda83cd7
SM
14354 {
14355 /* Handle VLD1. */
14356 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
14357 thumb2_insn_r->reg_rec_count = 1;
14358 /* Handle VLD2. */
14359 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
14360 thumb2_insn_r->reg_rec_count = 2;
14361 /* Handle VLD3. */
14362 else if ((b_bits & 0x0e) == 0x04)
14363 thumb2_insn_r->reg_rec_count = 3;
14364 /* Handle VLD4. */
14365 else if (!(b_bits & 0x0e))
14366 thumb2_insn_r->reg_rec_count = 4;
14367 }
1e1b6563 14368 else
dda83cd7
SM
14369 {
14370 /* Handle VLD1. */
14371 if (!(b_bits & 0x0b) || b_bits == 0x08 || b_bits == 0x0c)
14372 thumb2_insn_r->reg_rec_count = 1;
14373 /* Handle VLD2. */
14374 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09 || b_bits == 0x0d)
14375 thumb2_insn_r->reg_rec_count = 2;
14376 /* Handle VLD3. */
14377 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a || b_bits == 0x0e)
14378 thumb2_insn_r->reg_rec_count = 3;
14379 /* Handle VLD4. */
14380 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b || b_bits == 0x0f)
14381 thumb2_insn_r->reg_rec_count = 4;
14382
14383 for (index_r = 0; index_r < thumb2_insn_r->reg_rec_count; index_r++)
14384 record_buf[index_r] = reg_vd + ARM_D0_REGNUM + index_r;
14385 }
1e1b6563
OJ
14386 }
14387
14388 if (bits (thumb2_insn_r->arm_insn, 0, 3) != 15)
14389 {
14390 record_buf[index_r] = reg_rn;
14391 thumb2_insn_r->reg_rec_count += 1;
14392 }
14393
14394 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 14395 record_buf);
1e1b6563 14396 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
dda83cd7 14397 record_buf_mem);
1e1b6563
OJ
14398 return 0;
14399}
14400
c6ec2b30
OJ
14401/* Decodes thumb2 instruction type and invokes its record handler. */
14402
14403static unsigned int
4748a9be 14404thumb2_record_decode_insn_handler (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
14405{
14406 uint32_t op, op1, op2;
14407
14408 op = bit (thumb2_insn_r->arm_insn, 15);
14409 op1 = bits (thumb2_insn_r->arm_insn, 27, 28);
14410 op2 = bits (thumb2_insn_r->arm_insn, 20, 26);
14411
14412 if (op1 == 0x01)
14413 {
14414 if (!(op2 & 0x64 ))
dda83cd7
SM
14415 {
14416 /* Load/store multiple instruction. */
14417 return thumb2_record_ld_st_multiple (thumb2_insn_r);
14418 }
b121eeb9 14419 else if ((op2 & 0x64) == 0x4)
dda83cd7
SM
14420 {
14421 /* Load/store (dual/exclusive) and table branch instruction. */
14422 return thumb2_record_ld_st_dual_ex_tbb (thumb2_insn_r);
14423 }
b121eeb9 14424 else if ((op2 & 0x60) == 0x20)
dda83cd7
SM
14425 {
14426 /* Data-processing (shifted register). */
14427 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
14428 }
c6ec2b30 14429 else if (op2 & 0x40)
dda83cd7
SM
14430 {
14431 /* Co-processor instructions. */
14432 return thumb2_record_coproc_insn (thumb2_insn_r);
14433 }
c6ec2b30
OJ
14434 }
14435 else if (op1 == 0x02)
14436 {
14437 if (op)
dda83cd7
SM
14438 {
14439 /* Branches and miscellaneous control instructions. */
14440 return thumb2_record_branch_misc_cntrl (thumb2_insn_r);
14441 }
c6ec2b30 14442 else if (op2 & 0x20)
dda83cd7
SM
14443 {
14444 /* Data-processing (plain binary immediate) instruction. */
14445 return thumb2_record_ps_dest_generic (thumb2_insn_r);
14446 }
c6ec2b30 14447 else
dda83cd7
SM
14448 {
14449 /* Data-processing (modified immediate). */
14450 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
14451 }
c6ec2b30
OJ
14452 }
14453 else if (op1 == 0x03)
14454 {
14455 if (!(op2 & 0x71 ))
dda83cd7
SM
14456 {
14457 /* Store single data item. */
14458 return thumb2_record_str_single_data (thumb2_insn_r);
14459 }
c6ec2b30 14460 else if (!((op2 & 0x71) ^ 0x10))
dda83cd7
SM
14461 {
14462 /* Advanced SIMD or structure load/store instructions. */
14463 return thumb2_record_asimd_struct_ld_st (thumb2_insn_r);
14464 }
c6ec2b30 14465 else if (!((op2 & 0x67) ^ 0x01))
dda83cd7
SM
14466 {
14467 /* Load byte, memory hints instruction. */
14468 return thumb2_record_ld_mem_hints (thumb2_insn_r);
14469 }
c6ec2b30 14470 else if (!((op2 & 0x67) ^ 0x03))
dda83cd7
SM
14471 {
14472 /* Load halfword, memory hints instruction. */
14473 return thumb2_record_ld_mem_hints (thumb2_insn_r);
14474 }
c6ec2b30 14475 else if (!((op2 & 0x67) ^ 0x05))
dda83cd7
SM
14476 {
14477 /* Load word instruction. */
14478 return thumb2_record_ld_word (thumb2_insn_r);
14479 }
c6ec2b30 14480 else if (!((op2 & 0x70) ^ 0x20))
dda83cd7
SM
14481 {
14482 /* Data-processing (register) instruction. */
14483 return thumb2_record_ps_dest_generic (thumb2_insn_r);
14484 }
c6ec2b30 14485 else if (!((op2 & 0x78) ^ 0x30))
dda83cd7
SM
14486 {
14487 /* Multiply, multiply accumulate, abs diff instruction. */
14488 return thumb2_record_ps_dest_generic (thumb2_insn_r);
14489 }
c6ec2b30 14490 else if (!((op2 & 0x78) ^ 0x38))
dda83cd7
SM
14491 {
14492 /* Long multiply, long multiply accumulate, and divide. */
14493 return thumb2_record_lmul_lmla_div (thumb2_insn_r);
14494 }
c6ec2b30 14495 else if (op2 & 0x40)
dda83cd7
SM
14496 {
14497 /* Co-processor instructions. */
14498 return thumb2_record_coproc_insn (thumb2_insn_r);
14499 }
c6ec2b30
OJ
14500 }
14501
14502 return -1;
14503}
72508ac0 14504
ffdbe864 14505namespace {
0833fb8f 14506/* Abstract instruction reader. */
728a7913 14507
0833fb8f 14508class abstract_instruction_reader
728a7913
YQ
14509{
14510public:
0833fb8f
LM
14511 /* Read one instruction of size LEN from address MEMADDR and using
14512 BYTE_ORDER endianness. */
728a7913 14513
0833fb8f
LM
14514 virtual ULONGEST read (CORE_ADDR memaddr, const size_t len,
14515 enum bfd_endian byte_order) = 0;
728a7913
YQ
14516};
14517
14518/* Instruction reader from real target. */
14519
0833fb8f 14520class instruction_reader : public abstract_instruction_reader
728a7913
YQ
14521{
14522 public:
0833fb8f
LM
14523 ULONGEST read (CORE_ADDR memaddr, const size_t len,
14524 enum bfd_endian byte_order) override
728a7913 14525 {
0833fb8f 14526 return read_code_unsigned_integer (memaddr, len, byte_order);
728a7913
YQ
14527 }
14528};
14529
ffdbe864
YQ
14530} // namespace
14531
4748a9be 14532typedef int (*sti_arm_hdl_fp_t) (arm_insn_decode_record*);
72508ac0
PO
14533
14534/* Decode arm/thumb insn depending on condition cods and opcodes; and
14535 dispatch it. */
14536
14537static int
0833fb8f 14538decode_insn (abstract_instruction_reader &reader,
4748a9be 14539 arm_insn_decode_record *arm_record,
728a7913 14540 record_type_t record_type, uint32_t insn_size)
72508ac0
PO
14541{
14542
01e57735
YQ
14543 /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm
14544 instruction. */
0fa9c223 14545 static const sti_arm_hdl_fp_t arm_handle_insn[8] =
72508ac0
PO
14546 {
14547 arm_record_data_proc_misc_ld_str, /* 000. */
14548 arm_record_data_proc_imm, /* 001. */
14549 arm_record_ld_st_imm_offset, /* 010. */
14550 arm_record_ld_st_reg_offset, /* 011. */
14551 arm_record_ld_st_multiple, /* 100. */
14552 arm_record_b_bl, /* 101. */
60cc5e93 14553 arm_record_asimd_vfp_coproc, /* 110. */
72508ac0
PO
14554 arm_record_coproc_data_proc /* 111. */
14555 };
14556
01e57735
YQ
14557 /* (Starting from numerical 0); bits 13,14,15 decodes type of thumb
14558 instruction. */
0fa9c223 14559 static const sti_arm_hdl_fp_t thumb_handle_insn[8] =
72508ac0
PO
14560 { \
14561 thumb_record_shift_add_sub, /* 000. */
14562 thumb_record_add_sub_cmp_mov, /* 001. */
14563 thumb_record_ld_st_reg_offset, /* 010. */
14564 thumb_record_ld_st_imm_offset, /* 011. */
14565 thumb_record_ld_st_stack, /* 100. */
14566 thumb_record_misc, /* 101. */
14567 thumb_record_ldm_stm_swi, /* 110. */
14568 thumb_record_branch /* 111. */
14569 };
14570
14571 uint32_t ret = 0; /* return value: negative:failure 0:success. */
14572 uint32_t insn_id = 0;
0833fb8f
LM
14573 enum bfd_endian code_endian
14574 = gdbarch_byte_order_for_code (arm_record->gdbarch);
14575 arm_record->arm_insn
14576 = reader.read (arm_record->this_addr, insn_size, code_endian);
72508ac0 14577
0833fb8f 14578 if (ARM_RECORD == record_type)
72508ac0
PO
14579 {
14580 arm_record->cond = bits (arm_record->arm_insn, 28, 31);
14581 insn_id = bits (arm_record->arm_insn, 25, 27);
ca92db2d
YQ
14582
14583 if (arm_record->cond == 0xf)
14584 ret = arm_record_extension_space (arm_record);
14585 else
01e57735 14586 {
ca92db2d
YQ
14587 /* If this insn has fallen into extension space
14588 then we need not decode it anymore. */
01e57735
YQ
14589 ret = arm_handle_insn[insn_id] (arm_record);
14590 }
ca92db2d
YQ
14591 if (ret != ARM_RECORD_SUCCESS)
14592 {
14593 arm_record_unsupported_insn (arm_record);
14594 ret = -1;
14595 }
72508ac0
PO
14596 }
14597 else if (THUMB_RECORD == record_type)
14598 {
14599 /* As thumb does not have condition codes, we set negative. */
14600 arm_record->cond = -1;
14601 insn_id = bits (arm_record->arm_insn, 13, 15);
14602 ret = thumb_handle_insn[insn_id] (arm_record);
ca92db2d
YQ
14603 if (ret != ARM_RECORD_SUCCESS)
14604 {
14605 arm_record_unsupported_insn (arm_record);
14606 ret = -1;
14607 }
72508ac0
PO
14608 }
14609 else if (THUMB2_RECORD == record_type)
14610 {
c6ec2b30
OJ
14611 /* As thumb does not have condition codes, we set negative. */
14612 arm_record->cond = -1;
14613
14614 /* Swap first half of 32bit thumb instruction with second half. */
14615 arm_record->arm_insn
01e57735 14616 = (arm_record->arm_insn >> 16) | (arm_record->arm_insn << 16);
c6ec2b30 14617
ca92db2d 14618 ret = thumb2_record_decode_insn_handler (arm_record);
c6ec2b30 14619
ca92db2d 14620 if (ret != ARM_RECORD_SUCCESS)
01e57735
YQ
14621 {
14622 arm_record_unsupported_insn (arm_record);
14623 ret = -1;
14624 }
72508ac0
PO
14625 }
14626 else
14627 {
14628 /* Throw assertion. */
14629 gdb_assert_not_reached ("not a valid instruction, could not decode");
14630 }
14631
14632 return ret;
14633}
14634
b121eeb9
YQ
14635#if GDB_SELF_TEST
14636namespace selftests {
14637
0833fb8f
LM
14638/* Instruction reader class for selftests.
14639
14640 For 16-bit Thumb instructions, an array of uint16_t should be used.
b121eeb9 14641
0833fb8f
LM
14642 For 32-bit Thumb instructions and regular 32-bit Arm instructions, an array
14643 of uint32_t should be used. */
14644
14645template<typename T>
14646class instruction_reader_selftest : public abstract_instruction_reader
b121eeb9
YQ
14647{
14648public:
14649 template<size_t SIZE>
0833fb8f
LM
14650 instruction_reader_selftest (const T (&insns)[SIZE])
14651 : m_insns (insns), m_insns_size (SIZE)
b121eeb9
YQ
14652 {}
14653
0833fb8f
LM
14654 ULONGEST read (CORE_ADDR memaddr, const size_t length,
14655 enum bfd_endian byte_order) override
b121eeb9 14656 {
0833fb8f
LM
14657 SELF_CHECK (length == sizeof (T));
14658 SELF_CHECK (memaddr % sizeof (T) == 0);
14659 SELF_CHECK ((memaddr / sizeof (T)) < m_insns_size);
b121eeb9 14660
0833fb8f 14661 return m_insns[memaddr / sizeof (T)];
b121eeb9
YQ
14662 }
14663
14664private:
0833fb8f
LM
14665 const T *m_insns;
14666 const size_t m_insns_size;
b121eeb9
YQ
14667};
14668
14669static void
14670arm_record_test (void)
14671{
14672 struct gdbarch_info info;
b121eeb9
YQ
14673 info.bfd_arch_info = bfd_scan_arch ("arm");
14674
14675 struct gdbarch *gdbarch = gdbarch_find_by_info (info);
14676
14677 SELF_CHECK (gdbarch != NULL);
14678
14679 /* 16-bit Thumb instructions. */
14680 {
4748a9be 14681 arm_insn_decode_record arm_record;
b121eeb9 14682
4748a9be 14683 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
b121eeb9
YQ
14684 arm_record.gdbarch = gdbarch;
14685
0833fb8f
LM
14686 /* Use the endian-free representation of the instructions here. The test
14687 will handle endianness conversions. */
b121eeb9
YQ
14688 static const uint16_t insns[] = {
14689 /* db b2 uxtb r3, r3 */
14690 0xb2db,
14691 /* cd 58 ldr r5, [r1, r3] */
14692 0x58cd,
14693 };
14694
0833fb8f 14695 instruction_reader_selftest<uint16_t> reader (insns);
b121eeb9
YQ
14696 int ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14697 THUMB_INSN_SIZE_BYTES);
14698
14699 SELF_CHECK (ret == 0);
14700 SELF_CHECK (arm_record.mem_rec_count == 0);
14701 SELF_CHECK (arm_record.reg_rec_count == 1);
14702 SELF_CHECK (arm_record.arm_regs[0] == 3);
14703
14704 arm_record.this_addr += 2;
14705 ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14706 THUMB_INSN_SIZE_BYTES);
14707
14708 SELF_CHECK (ret == 0);
14709 SELF_CHECK (arm_record.mem_rec_count == 0);
14710 SELF_CHECK (arm_record.reg_rec_count == 1);
14711 SELF_CHECK (arm_record.arm_regs[0] == 5);
14712 }
14713
14714 /* 32-bit Thumb-2 instructions. */
14715 {
4748a9be 14716 arm_insn_decode_record arm_record;
b121eeb9 14717
4748a9be 14718 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
b121eeb9
YQ
14719 arm_record.gdbarch = gdbarch;
14720
0833fb8f
LM
14721 /* Use the endian-free representation of the instruction here. The test
14722 will handle endianness conversions. */
14723 static const uint32_t insns[] = {
14724 /* mrc 15, 0, r7, cr13, cr0, {3} */
14725 0x7f70ee1d,
b121eeb9
YQ
14726 };
14727
0833fb8f 14728 instruction_reader_selftest<uint32_t> reader (insns);
b121eeb9
YQ
14729 int ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
14730 THUMB2_INSN_SIZE_BYTES);
14731
14732 SELF_CHECK (ret == 0);
14733 SELF_CHECK (arm_record.mem_rec_count == 0);
14734 SELF_CHECK (arm_record.reg_rec_count == 1);
14735 SELF_CHECK (arm_record.arm_regs[0] == 7);
14736 }
0833fb8f
LM
14737
14738 /* 32-bit instructions. */
14739 {
14740 arm_insn_decode_record arm_record;
14741
14742 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
14743 arm_record.gdbarch = gdbarch;
14744
14745 /* Use the endian-free representation of the instruction here. The test
14746 will handle endianness conversions. */
14747 static const uint32_t insns[] = {
14748 /* mov r5, r0 */
14749 0xe1a05000,
14750 };
14751
14752 instruction_reader_selftest<uint32_t> reader (insns);
14753 int ret = decode_insn (reader, &arm_record, ARM_RECORD,
14754 ARM_INSN_SIZE_BYTES);
14755
14756 SELF_CHECK (ret == 0);
14757 }
b121eeb9 14758}
9ecab40c
SM
14759
14760/* Instruction reader from manually cooked instruction sequences. */
14761
14762class test_arm_instruction_reader : public arm_instruction_reader
14763{
14764public:
14765 explicit test_arm_instruction_reader (gdb::array_view<const uint32_t> insns)
14766 : m_insns (insns)
14767 {}
14768
14769 uint32_t read (CORE_ADDR memaddr, enum bfd_endian byte_order) const override
14770 {
14771 SELF_CHECK (memaddr % 4 == 0);
14772 SELF_CHECK (memaddr / 4 < m_insns.size ());
14773
14774 return m_insns[memaddr / 4];
14775 }
14776
14777private:
14778 const gdb::array_view<const uint32_t> m_insns;
14779};
14780
14781static void
14782arm_analyze_prologue_test ()
14783{
14784 for (bfd_endian endianness : {BFD_ENDIAN_LITTLE, BFD_ENDIAN_BIG})
14785 {
14786 struct gdbarch_info info;
9ecab40c
SM
14787 info.byte_order = endianness;
14788 info.byte_order_for_code = endianness;
14789 info.bfd_arch_info = bfd_scan_arch ("arm");
14790
14791 struct gdbarch *gdbarch = gdbarch_find_by_info (info);
14792
14793 SELF_CHECK (gdbarch != NULL);
14794
14795 /* The "sub" instruction contains an immediate value rotate count of 0,
14796 which resulted in a 32-bit shift of a 32-bit value, caught by
14797 UBSan. */
14798 const uint32_t insns[] = {
14799 0xe92d4ff0, /* push {r4, r5, r6, r7, r8, r9, sl, fp, lr} */
14800 0xe1a05000, /* mov r5, r0 */
14801 0xe5903020, /* ldr r3, [r0, #32] */
14802 0xe24dd044, /* sub sp, sp, #68 ; 0x44 */
14803 };
14804
14805 test_arm_instruction_reader mem_reader (insns);
14806 arm_prologue_cache cache;
0824193f 14807 arm_cache_init (&cache, gdbarch);
9ecab40c
SM
14808
14809 arm_analyze_prologue (gdbarch, 0, sizeof (insns) - 1, &cache, mem_reader);
14810 }
14811}
14812
b121eeb9
YQ
14813} // namespace selftests
14814#endif /* GDB_SELF_TEST */
72508ac0
PO
14815
14816/* Cleans up local record registers and memory allocations. */
14817
14818static void
4748a9be 14819deallocate_reg_mem (arm_insn_decode_record *record)
72508ac0
PO
14820{
14821 xfree (record->arm_regs);
14822 xfree (record->arm_mems);
14823}
14824
14825
01e57735 14826/* Parse the current instruction and record the values of the registers and
72508ac0
PO
14827 memory that will be changed in current instruction to record_arch_list".
14828 Return -1 if something is wrong. */
14829
14830int
01e57735
YQ
14831arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
14832 CORE_ADDR insn_addr)
72508ac0
PO
14833{
14834
72508ac0
PO
14835 uint32_t no_of_rec = 0;
14836 uint32_t ret = 0; /* return value: -1:record failure ; 0:success */
14837 ULONGEST t_bit = 0, insn_id = 0;
14838
14839 ULONGEST u_regval = 0;
14840
4748a9be 14841 arm_insn_decode_record arm_record;
72508ac0 14842
4748a9be 14843 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
72508ac0
PO
14844 arm_record.regcache = regcache;
14845 arm_record.this_addr = insn_addr;
14846 arm_record.gdbarch = gdbarch;
14847
14848
14849 if (record_debug > 1)
14850 {
6cb06a8c
TT
14851 gdb_printf (gdb_stdlog, "Process record: arm_process_record "
14852 "addr = %s\n",
14853 paddress (gdbarch, arm_record.this_addr));
72508ac0
PO
14854 }
14855
728a7913 14856 instruction_reader reader;
0833fb8f
LM
14857 enum bfd_endian code_endian
14858 = gdbarch_byte_order_for_code (arm_record.gdbarch);
14859 arm_record.arm_insn
14860 = reader.read (arm_record.this_addr, 2, code_endian);
72508ac0
PO
14861
14862 /* Check the insn, whether it is thumb or arm one. */
14863
14864 t_bit = arm_psr_thumb_bit (arm_record.gdbarch);
14865 regcache_raw_read_unsigned (arm_record.regcache, ARM_PS_REGNUM, &u_regval);
14866
14867
14868 if (!(u_regval & t_bit))
14869 {
14870 /* We are decoding arm insn. */
728a7913 14871 ret = decode_insn (reader, &arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES);
72508ac0
PO
14872 }
14873 else
14874 {
14875 insn_id = bits (arm_record.arm_insn, 11, 15);
14876 /* is it thumb2 insn? */
14877 if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id))
01e57735 14878 {
728a7913 14879 ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
01e57735
YQ
14880 THUMB2_INSN_SIZE_BYTES);
14881 }
72508ac0 14882 else
01e57735
YQ
14883 {
14884 /* We are decoding thumb insn. */
728a7913
YQ
14885 ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14886 THUMB_INSN_SIZE_BYTES);
01e57735 14887 }
72508ac0
PO
14888 }
14889
14890 if (0 == ret)
14891 {
14892 /* Record registers. */
25ea693b 14893 record_full_arch_list_add_reg (arm_record.regcache, ARM_PC_REGNUM);
72508ac0 14894 if (arm_record.arm_regs)
01e57735
YQ
14895 {
14896 for (no_of_rec = 0; no_of_rec < arm_record.reg_rec_count; no_of_rec++)
14897 {
14898 if (record_full_arch_list_add_reg
25ea693b 14899 (arm_record.regcache , arm_record.arm_regs[no_of_rec]))
01e57735
YQ
14900 ret = -1;
14901 }
14902 }
72508ac0
PO
14903 /* Record memories. */
14904 if (arm_record.arm_mems)
01e57735
YQ
14905 {
14906 for (no_of_rec = 0; no_of_rec < arm_record.mem_rec_count; no_of_rec++)
14907 {
14908 if (record_full_arch_list_add_mem
14909 ((CORE_ADDR)arm_record.arm_mems[no_of_rec].addr,
25ea693b 14910 arm_record.arm_mems[no_of_rec].len))
01e57735
YQ
14911 ret = -1;
14912 }
14913 }
72508ac0 14914
25ea693b 14915 if (record_full_arch_list_add_end ())
01e57735 14916 ret = -1;
72508ac0
PO
14917 }
14918
14919
14920 deallocate_reg_mem (&arm_record);
14921
14922 return ret;
14923}
d105cce5
AH
14924
14925/* See arm-tdep.h. */
14926
14927const target_desc *
92d48a1e 14928arm_read_description (arm_fp_type fp_type, bool tls)
d105cce5 14929{
92d48a1e 14930 struct target_desc *tdesc = tdesc_arm_list[fp_type][tls];
d105cce5
AH
14931
14932 if (tdesc == nullptr)
14933 {
92d48a1e
JB
14934 tdesc = arm_create_target_description (fp_type, tls);
14935 tdesc_arm_list[fp_type][tls] = tdesc;
d105cce5
AH
14936 }
14937
14938 return tdesc;
14939}
14940
14941/* See arm-tdep.h. */
14942
14943const target_desc *
14944arm_read_mprofile_description (arm_m_profile_type m_type)
14945{
14946 struct target_desc *tdesc = tdesc_arm_mprofile_list[m_type];
14947
14948 if (tdesc == nullptr)
14949 {
14950 tdesc = arm_create_mprofile_target_description (m_type);
14951 tdesc_arm_mprofile_list[m_type] = tdesc;
14952 }
14953
14954 return tdesc;
14955}