]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/arm-tdep.c
gdb, gdbserver, gdbsupport: remove includes of early headers
[thirdparty/binutils-gdb.git] / gdb / arm-tdep.c
CommitLineData
ed9a39eb 1/* Common target dependent code for GDB on ARM systems.
0fd88904 2
1d506c26 3 Copyright (C) 1988-2024 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 20
ef0f16cc 21#include <ctype.h>
34e8f22d 22
4de283e4 23#include "frame.h"
83b6e1f1 24#include "language.h"
4de283e4
TT
25#include "inferior.h"
26#include "infrun.h"
27#include "gdbcmd.h"
28#include "gdbcore.h"
ef0f16cc 29#include "dis-asm.h"
4de283e4
TT
30#include "disasm.h"
31#include "regcache.h"
32#include "reggroups.h"
33#include "target-float.h"
34#include "value.h"
d55e5aa6 35#include "arch-utils.h"
4de283e4
TT
36#include "osabi.h"
37#include "frame-unwind.h"
38#include "frame-base.h"
39#include "trad-frame.h"
40#include "objfiles.h"
a01567f4 41#include "dwarf2.h"
82ca8957 42#include "dwarf2/frame.h"
4de283e4
TT
43#include "gdbtypes.h"
44#include "prologue-value.h"
45#include "remote.h"
46#include "target-descriptions.h"
47#include "user-regs.h"
48#include "observable.h"
5f661e03 49#include "count-one-bits.h"
4de283e4 50
d55e5aa6 51#include "arch/arm.h"
4de283e4 52#include "arch/arm-get-next-pcs.h"
34e8f22d 53#include "arm-tdep.h"
d026e67e 54#include "sim/sim-arm.h"
4de283e4 55
d55e5aa6 56#include "elf-bfd.h"
4de283e4 57#include "coff/internal.h"
d55e5aa6 58#include "elf/arm.h"
4de283e4 59
4de283e4
TT
60#include "record.h"
61#include "record-full.h"
62#include <algorithm>
63
c2fd7fae
AKS
64#include "producer.h"
65
b121eeb9 66#if GDB_SELF_TEST
268a13a5 67#include "gdbsupport/selftest.h"
b121eeb9
YQ
68#endif
69
491144b5 70static bool arm_debug;
6529d2dd 71
7cb6d92a
SM
72/* Print an "arm" debug statement. */
73
74#define arm_debug_printf(fmt, ...) \
75 debug_prefixed_printf_cond (arm_debug, "arm", fmt, ##__VA_ARGS__)
76
082fc60d
RE
77/* Macros for setting and testing a bit in a minimal symbol that marks
78 it as Thumb function. The MSB of the minimal symbol's "info" field
f594e5e9 79 is used for this purpose.
082fc60d
RE
80
81 MSYMBOL_SET_SPECIAL Actually sets the "special" bit.
f594e5e9 82 MSYMBOL_IS_SPECIAL Tests the "special" bit in a minimal symbol. */
082fc60d 83
0963b4bd 84#define MSYMBOL_SET_SPECIAL(msym) \
e165fcef 85 (msym)->set_target_flag_1 (true)
082fc60d
RE
86
87#define MSYMBOL_IS_SPECIAL(msym) \
e165fcef 88 (msym)->target_flag_1 ()
082fc60d 89
60c5725c
DJ
90struct arm_mapping_symbol
91{
227031b2 92 CORE_ADDR value;
60c5725c 93 char type;
54cc7474
SM
94
95 bool operator< (const arm_mapping_symbol &other) const
96 { return this->value < other.value; }
60c5725c 97};
54cc7474
SM
98
99typedef std::vector<arm_mapping_symbol> arm_mapping_symbol_vec;
60c5725c 100
bd5766ec 101struct arm_per_bfd
60c5725c 102{
bd5766ec 103 explicit arm_per_bfd (size_t num_sections)
4838e44c
SM
104 : section_maps (new arm_mapping_symbol_vec[num_sections]),
105 section_maps_sorted (new bool[num_sections] ())
54cc7474
SM
106 {}
107
bd5766ec 108 DISABLE_COPY_AND_ASSIGN (arm_per_bfd);
54cc7474
SM
109
110 /* Information about mapping symbols ($a, $d, $t) in the objfile.
111
112 The format is an array of vectors of arm_mapping_symbols, there is one
113 vector for each section of the objfile (the array is index by BFD section
114 index).
115
116 For each section, the vector of arm_mapping_symbol is sorted by
117 symbol value (address). */
118 std::unique_ptr<arm_mapping_symbol_vec[]> section_maps;
4838e44c
SM
119
120 /* For each corresponding element of section_maps above, is this vector
121 sorted. */
122 std::unique_ptr<bool[]> section_maps_sorted;
60c5725c
DJ
123};
124
bd5766ec 125/* Per-bfd data used for mapping symbols. */
08b8a139 126static const registry<bfd>::key<arm_per_bfd> arm_bfd_data_key;
1b7f24cd 127
afd7eef0
RE
128/* The list of available "set arm ..." and "show arm ..." commands. */
129static struct cmd_list_element *setarmcmdlist = NULL;
130static struct cmd_list_element *showarmcmdlist = NULL;
131
fd50bc42
RE
132/* The type of floating-point to use. Keep this in sync with enum
133 arm_float_model, and the help string in _initialize_arm_tdep. */
40478521 134static const char *const fp_model_strings[] =
fd50bc42
RE
135{
136 "auto",
137 "softfpa",
138 "fpa",
139 "softvfp",
28e97307
DJ
140 "vfp",
141 NULL
fd50bc42
RE
142};
143
144/* A variable that can be configured by the user. */
145static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
146static const char *current_fp_model = "auto";
147
28e97307 148/* The ABI to use. Keep this in sync with arm_abi_kind. */
40478521 149static const char *const arm_abi_strings[] =
28e97307
DJ
150{
151 "auto",
152 "APCS",
153 "AAPCS",
154 NULL
155};
156
157/* A variable that can be configured by the user. */
158static enum arm_abi_kind arm_abi_global = ARM_ABI_AUTO;
159static const char *arm_abi_string = "auto";
160
0428b8f5 161/* The execution mode to assume. */
40478521 162static const char *const arm_mode_strings[] =
0428b8f5
DJ
163 {
164 "auto",
165 "arm",
68770265
MGD
166 "thumb",
167 NULL
0428b8f5
DJ
168 };
169
170static const char *arm_fallback_mode_string = "auto";
171static const char *arm_force_mode_string = "auto";
172
f32bf4a4
YQ
173/* The standard register names, and all the valid aliases for them. Note
174 that `fp', `sp' and `pc' are not added in this alias list, because they
175 have been added as builtin user registers in
176 std-regs.c:_initialize_frame_reg. */
123dc839
DJ
177static const struct
178{
179 const char *name;
180 int regnum;
181} arm_register_aliases[] = {
182 /* Basic register numbers. */
183 { "r0", 0 },
184 { "r1", 1 },
185 { "r2", 2 },
186 { "r3", 3 },
187 { "r4", 4 },
188 { "r5", 5 },
189 { "r6", 6 },
190 { "r7", 7 },
191 { "r8", 8 },
192 { "r9", 9 },
193 { "r10", 10 },
194 { "r11", 11 },
195 { "r12", 12 },
196 { "r13", 13 },
197 { "r14", 14 },
198 { "r15", 15 },
199 /* Synonyms (argument and variable registers). */
200 { "a1", 0 },
201 { "a2", 1 },
202 { "a3", 2 },
203 { "a4", 3 },
204 { "v1", 4 },
205 { "v2", 5 },
206 { "v3", 6 },
207 { "v4", 7 },
208 { "v5", 8 },
209 { "v6", 9 },
210 { "v7", 10 },
211 { "v8", 11 },
212 /* Other platform-specific names for r9. */
213 { "sb", 9 },
214 { "tr", 9 },
215 /* Special names. */
216 { "ip", 12 },
123dc839 217 { "lr", 14 },
123dc839
DJ
218 /* Names used by GCC (not listed in the ARM EABI). */
219 { "sl", 10 },
123dc839
DJ
220 /* A special name from the older ATPCS. */
221 { "wr", 7 },
222};
bc90b915 223
123dc839 224static const char *const arm_register_names[] =
da59e081
JM
225{"r0", "r1", "r2", "r3", /* 0 1 2 3 */
226 "r4", "r5", "r6", "r7", /* 4 5 6 7 */
227 "r8", "r9", "r10", "r11", /* 8 9 10 11 */
228 "r12", "sp", "lr", "pc", /* 12 13 14 15 */
229 "f0", "f1", "f2", "f3", /* 16 17 18 19 */
230 "f4", "f5", "f6", "f7", /* 20 21 22 23 */
94c30b78 231 "fps", "cpsr" }; /* 24 25 */
ed9a39eb 232
65b48a81 233/* Holds the current set of options to be passed to the disassembler. */
c05dd511 234static std::string arm_disassembler_options;
65b48a81 235
afd7eef0
RE
236/* Valid register name styles. */
237static const char **valid_disassembly_styles;
ed9a39eb 238
afd7eef0
RE
239/* Disassembly style to use. Default to "std" register names. */
240static const char *disassembly_style;
96baa820 241
d105cce5 242/* All possible arm target descriptors. */
92d48a1e 243static struct target_desc *tdesc_arm_list[ARM_FP_TYPE_INVALID][2];
d105cce5
AH
244static struct target_desc *tdesc_arm_mprofile_list[ARM_M_TYPE_INVALID];
245
ed9a39eb 246/* This is used to keep the bfd arch_info in sync with the disassembly
afd7eef0 247 style. */
eb4c3f4a 248static void set_disassembly_style_sfunc (const char *, int,
ed9a39eb 249 struct cmd_list_element *);
65b48a81
PB
250static void show_disassembly_style_sfunc (struct ui_file *, int,
251 struct cmd_list_element *,
252 const char *);
ed9a39eb 253
05d1431c 254static enum register_status arm_neon_quad_read (struct gdbarch *gdbarch,
849d0ba8 255 readable_regcache *regcache,
05d1431c 256 int regnum, gdb_byte *buf);
58d6951d
DJ
257static void arm_neon_quad_write (struct gdbarch *gdbarch,
258 struct regcache *regcache,
259 int regnum, const gdb_byte *buf);
260
e7cf25a8 261static CORE_ADDR
553cb527 262 arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self);
e7cf25a8
YQ
263
264
d9311bfa
AT
265/* get_next_pcs operations. */
266static struct arm_get_next_pcs_ops arm_get_next_pcs_ops = {
267 arm_get_next_pcs_read_memory_unsigned_integer,
268 arm_get_next_pcs_syscall_next_pc,
269 arm_get_next_pcs_addr_bits_remove,
ed443b61
YQ
270 arm_get_next_pcs_is_thumb,
271 NULL,
d9311bfa
AT
272};
273
9b8d791a 274struct arm_prologue_cache
c3b4394c 275{
eb5492fa
DJ
276 /* The stack pointer at the time this frame was created; i.e. the
277 caller's stack pointer when this function was called. It is used
278 to identify this frame. */
ae7e2f45
CL
279 CORE_ADDR sp;
280
281 /* Additional stack pointers used by M-profile with Security extension. */
282 /* Use msp_s / psp_s to hold the values of msp / psp when there is
283 no Security extension. */
284 CORE_ADDR msp_s;
285 CORE_ADDR msp_ns;
286 CORE_ADDR psp_s;
287 CORE_ADDR psp_ns;
288
289 /* Active stack pointer. */
290 int active_sp_regnum;
0d12d61b
YR
291 int active_msp_regnum;
292 int active_psp_regnum;
eb5492fa 293
4be43953
DJ
294 /* The frame base for this frame is just prev_sp - frame size.
295 FRAMESIZE is the distance from the frame pointer to the
296 initial stack pointer. */
eb5492fa 297
c3b4394c 298 int framesize;
eb5492fa
DJ
299
300 /* The register used to hold the frame pointer for this frame. */
c3b4394c 301 int framereg;
eb5492fa 302
a01567f4 303 /* True if the return address is signed, false otherwise. */
6b09f134 304 std::optional<bool> ra_signed_state;
a01567f4 305
eb5492fa 306 /* Saved register offsets. */
098caef4 307 trad_frame_saved_reg *saved_regs;
0824193f
CL
308
309 arm_prologue_cache() = default;
c3b4394c 310};
ed9a39eb 311
8c9ae6df
YR
312
313/* Reconstruct T bit in program status register from LR value. */
314
315static inline ULONGEST
316reconstruct_t_bit(struct gdbarch *gdbarch, CORE_ADDR lr, ULONGEST psr)
317{
318 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
319 if (IS_THUMB_ADDR (lr))
320 psr |= t_bit;
321 else
322 psr &= ~t_bit;
323
324 return psr;
325}
326
0824193f
CL
327/* Initialize CACHE fields for which zero is not adequate (CACHE is
328 expected to have been ZALLOC'ed before calling this function). */
329
330static void
331arm_cache_init (struct arm_prologue_cache *cache, struct gdbarch *gdbarch)
332{
ae7e2f45
CL
333 cache->active_sp_regnum = ARM_SP_REGNUM;
334
0824193f
CL
335 cache->saved_regs = trad_frame_alloc_saved_regs (gdbarch);
336}
337
338/* Similar to the previous function, but extracts GDBARCH from FRAME. */
339
340static void
8480a37e 341arm_cache_init (struct arm_prologue_cache *cache, const frame_info_ptr &frame)
0824193f
CL
342{
343 struct gdbarch *gdbarch = get_frame_arch (frame);
08106042 344 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
0824193f
CL
345
346 arm_cache_init (cache, gdbarch);
fe642a5b 347 cache->sp = get_frame_register_unsigned (frame, ARM_SP_REGNUM);
ae7e2f45
CL
348
349 if (tdep->have_sec_ext)
350 {
b0b23e06
TS
351 const CORE_ADDR msp_val
352 = get_frame_register_unsigned (frame, tdep->m_profile_msp_regnum);
353 const CORE_ADDR psp_val
354 = get_frame_register_unsigned (frame, tdep->m_profile_psp_regnum);
355
356 cache->msp_s
357 = get_frame_register_unsigned (frame, tdep->m_profile_msp_s_regnum);
358 cache->msp_ns
359 = get_frame_register_unsigned (frame, tdep->m_profile_msp_ns_regnum);
360 cache->psp_s
361 = get_frame_register_unsigned (frame, tdep->m_profile_psp_s_regnum);
362 cache->psp_ns
363 = get_frame_register_unsigned (frame, tdep->m_profile_psp_ns_regnum);
364
365 /* Identify what msp is alias for (msp_s or msp_ns). */
0d12d61b
YR
366 if (msp_val == cache->msp_s)
367 cache->active_msp_regnum = tdep->m_profile_msp_s_regnum;
368 else if (msp_val == cache->msp_ns)
369 cache->active_msp_regnum = tdep->m_profile_msp_ns_regnum;
b0b23e06
TS
370 else
371 {
372 warning (_("Invalid state, unable to determine msp alias, assuming "
373 "msp_s."));
374 cache->active_msp_regnum = tdep->m_profile_msp_s_regnum;
375 }
376
377 /* Identify what psp is alias for (psp_s or psp_ns). */
0d12d61b
YR
378 if (psp_val == cache->psp_s)
379 cache->active_psp_regnum = tdep->m_profile_psp_s_regnum;
380 else if (psp_val == cache->psp_ns)
381 cache->active_psp_regnum = tdep->m_profile_psp_ns_regnum;
b0b23e06
TS
382 else
383 {
384 warning (_("Invalid state, unable to determine psp alias, assuming "
385 "psp_s."));
386 cache->active_psp_regnum = tdep->m_profile_psp_s_regnum;
387 }
388
389 /* Identify what sp is alias for (msp_s, msp_ns, psp_s or psp_ns). */
390 if (msp_val == cache->sp)
391 cache->active_sp_regnum = cache->active_msp_regnum;
392 else if (psp_val == cache->sp)
393 cache->active_sp_regnum = cache->active_psp_regnum;
394 else
395 {
396 warning (_("Invalid state, unable to determine sp alias, assuming "
397 "msp."));
398 cache->active_sp_regnum = cache->active_msp_regnum;
399 }
ae7e2f45
CL
400 }
401 else if (tdep->is_m)
402 {
b0b23e06 403 cache->msp_s
f3f7ecc9 404 = get_frame_register_unsigned (frame, tdep->m_profile_msp_regnum);
b0b23e06 405 cache->psp_s
f3f7ecc9 406 = get_frame_register_unsigned (frame, tdep->m_profile_psp_regnum);
b0b23e06
TS
407
408 /* Identify what sp is alias for (msp or psp). */
409 if (cache->msp_s == cache->sp)
410 cache->active_sp_regnum = tdep->m_profile_msp_regnum;
411 else if (cache->psp_s == cache->sp)
412 cache->active_sp_regnum = tdep->m_profile_psp_regnum;
413 else
414 {
415 warning (_("Invalid state, unable to determine sp alias, assuming "
416 "msp."));
417 cache->active_sp_regnum = tdep->m_profile_msp_regnum;
418 }
ae7e2f45
CL
419 }
420 else
b0b23e06
TS
421 {
422 cache->msp_s
423 = get_frame_register_unsigned (frame, ARM_SP_REGNUM);
424
425 cache->active_sp_regnum = ARM_SP_REGNUM;
426 }
ae7e2f45
CL
427}
428
429/* Return the requested stack pointer value (in REGNUM), taking into
430 account whether we have a Security extension or an M-profile
431 CPU. */
432
433static CORE_ADDR
434arm_cache_get_sp_register (struct arm_prologue_cache *cache,
435 arm_gdbarch_tdep *tdep, int regnum)
436{
ae7e2f45
CL
437 if (tdep->have_sec_ext)
438 {
439 if (regnum == tdep->m_profile_msp_s_regnum)
440 return cache->msp_s;
441 if (regnum == tdep->m_profile_msp_ns_regnum)
442 return cache->msp_ns;
443 if (regnum == tdep->m_profile_psp_s_regnum)
444 return cache->psp_s;
445 if (regnum == tdep->m_profile_psp_ns_regnum)
446 return cache->psp_ns;
0d12d61b
YR
447 if (regnum == tdep->m_profile_msp_regnum)
448 return arm_cache_get_sp_register (cache, tdep, cache->active_msp_regnum);
449 if (regnum == tdep->m_profile_psp_regnum)
450 return arm_cache_get_sp_register (cache, tdep, cache->active_psp_regnum);
b9b66a3a
YR
451 if (regnum == ARM_SP_REGNUM)
452 return arm_cache_get_sp_register (cache, tdep, cache->active_sp_regnum);
ae7e2f45
CL
453 }
454 else if (tdep->is_m)
455 {
456 if (regnum == tdep->m_profile_msp_regnum)
457 return cache->msp_s;
458 if (regnum == tdep->m_profile_psp_regnum)
459 return cache->psp_s;
b9b66a3a
YR
460 if (regnum == ARM_SP_REGNUM)
461 return arm_cache_get_sp_register (cache, tdep, cache->active_sp_regnum);
ae7e2f45 462 }
b9b66a3a
YR
463 else if (regnum == ARM_SP_REGNUM)
464 return cache->sp;
ae7e2f45
CL
465
466 gdb_assert_not_reached ("Invalid SP selection");
467}
468
469/* Return the previous stack address, depending on which SP register
470 is active. */
471
472static CORE_ADDR
473arm_cache_get_prev_sp_value (struct arm_prologue_cache *cache, arm_gdbarch_tdep *tdep)
474{
475 CORE_ADDR val = arm_cache_get_sp_register (cache, tdep, cache->active_sp_regnum);
476 return val;
477}
478
479/* Set the active stack pointer to VAL. */
480
481static void
482arm_cache_set_active_sp_value (struct arm_prologue_cache *cache,
483 arm_gdbarch_tdep *tdep, CORE_ADDR val)
484{
ae7e2f45
CL
485 if (tdep->have_sec_ext)
486 {
487 if (cache->active_sp_regnum == tdep->m_profile_msp_s_regnum)
488 cache->msp_s = val;
489 else if (cache->active_sp_regnum == tdep->m_profile_msp_ns_regnum)
490 cache->msp_ns = val;
491 else if (cache->active_sp_regnum == tdep->m_profile_psp_s_regnum)
492 cache->psp_s = val;
493 else if (cache->active_sp_regnum == tdep->m_profile_psp_ns_regnum)
494 cache->psp_ns = val;
495
496 return;
497 }
498 else if (tdep->is_m)
499 {
500 if (cache->active_sp_regnum == tdep->m_profile_msp_regnum)
501 cache->msp_s = val;
502 else if (cache->active_sp_regnum == tdep->m_profile_psp_regnum)
503 cache->psp_s = val;
504
505 return;
506 }
b9b66a3a
YR
507 else if (cache->active_sp_regnum == ARM_SP_REGNUM)
508 {
509 cache->sp = val;
510 return;
511 }
ae7e2f45
CL
512
513 gdb_assert_not_reached ("Invalid SP selection");
0824193f
CL
514}
515
d65edaa0 516/* Return true if REGNUM is one of the alternative stack pointers. */
ef273377
CL
517
518static bool
d65edaa0 519arm_is_alternative_sp_register (arm_gdbarch_tdep *tdep, int regnum)
ef273377 520{
d65edaa0 521 if ((regnum == tdep->m_profile_msp_regnum)
ef273377
CL
522 || (regnum == tdep->m_profile_msp_s_regnum)
523 || (regnum == tdep->m_profile_msp_ns_regnum)
524 || (regnum == tdep->m_profile_psp_regnum)
525 || (regnum == tdep->m_profile_psp_s_regnum)
526 || (regnum == tdep->m_profile_psp_ns_regnum))
527 return true;
528 else
529 return false;
530}
531
532/* Set the active stack pointer to SP_REGNUM. */
533
534static void
535arm_cache_switch_prev_sp (struct arm_prologue_cache *cache,
536 arm_gdbarch_tdep *tdep, int sp_regnum)
537{
d65edaa0 538 gdb_assert (arm_is_alternative_sp_register (tdep, sp_regnum));
ef273377
CL
539
540 if (tdep->have_sec_ext)
4d9fd868
TS
541 {
542 gdb_assert (sp_regnum != tdep->m_profile_msp_regnum
543 && sp_regnum != tdep->m_profile_psp_regnum);
544
545 if (sp_regnum == tdep->m_profile_msp_s_regnum
546 || sp_regnum == tdep->m_profile_psp_s_regnum)
547 {
548 cache->active_msp_regnum = tdep->m_profile_msp_s_regnum;
549 cache->active_psp_regnum = tdep->m_profile_psp_s_regnum;
550 }
551 else if (sp_regnum == tdep->m_profile_msp_ns_regnum
552 || sp_regnum == tdep->m_profile_psp_ns_regnum)
553 {
554 cache->active_msp_regnum = tdep->m_profile_msp_ns_regnum;
555 cache->active_psp_regnum = tdep->m_profile_psp_ns_regnum;
556 }
557 }
ef273377
CL
558
559 cache->active_sp_regnum = sp_regnum;
560}
561
9ecab40c
SM
562namespace {
563
564/* Abstract class to read ARM instructions from memory. */
565
566class arm_instruction_reader
567{
568public:
2c5b1849 569 /* Read a 4 bytes instruction from memory using the BYTE_ORDER endianness. */
9ecab40c
SM
570 virtual uint32_t read (CORE_ADDR memaddr, bfd_endian byte_order) const = 0;
571};
572
573/* Read instructions from target memory. */
574
575class target_arm_instruction_reader : public arm_instruction_reader
576{
577public:
578 uint32_t read (CORE_ADDR memaddr, bfd_endian byte_order) const override
579 {
580 return read_code_unsigned_integer (memaddr, 4, byte_order);
581 }
582};
583
584} /* namespace */
585
586static CORE_ADDR arm_analyze_prologue
587 (struct gdbarch *gdbarch, CORE_ADDR prologue_start, CORE_ADDR prologue_end,
588 struct arm_prologue_cache *cache, const arm_instruction_reader &insn_reader);
0d39a070 589
cca44b1b
JB
590/* Architecture version for displaced stepping. This effects the behaviour of
591 certain instructions, and really should not be hard-wired. */
592
593#define DISPLACED_STEPPING_ARCH_VERSION 5
594
c7ae7675 595/* See arm-tdep.h. */
c906108c 596
491144b5 597bool arm_apcs_32 = true;
ef273377 598bool arm_unwind_secure_frames = true;
c906108c 599
9779414d
DJ
600/* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode. */
601
478fd957 602int
9779414d
DJ
603arm_psr_thumb_bit (struct gdbarch *gdbarch)
604{
08106042 605 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
345bd07c
SM
606
607 if (tdep->is_m)
9779414d
DJ
608 return XPSR_T;
609 else
610 return CPSR_T;
611}
612
d0e59a68
AT
613/* Determine if the processor is currently executing in Thumb mode. */
614
615int
616arm_is_thumb (struct regcache *regcache)
617{
618 ULONGEST cpsr;
ac7936df 619 ULONGEST t_bit = arm_psr_thumb_bit (regcache->arch ());
d0e59a68
AT
620
621 cpsr = regcache_raw_get_unsigned (regcache, ARM_PS_REGNUM);
622
623 return (cpsr & t_bit) != 0;
624}
625
60270718
AB
626/* Determine if FRAME is executing in Thumb mode. FRAME must be an ARM
627 frame. */
b39cc962 628
25b41d01 629int
8480a37e 630arm_frame_is_thumb (const frame_info_ptr &frame)
b39cc962 631{
60270718
AB
632 /* Check the architecture of FRAME. */
633 struct gdbarch *gdbarch = get_frame_arch (frame);
634 gdb_assert (gdbarch_bfd_arch_info (gdbarch)->arch == bfd_arch_arm);
b39cc962
DJ
635
636 /* Every ARM frame unwinder can unwind the T bit of the CPSR, either
637 directly (from a signal frame or dummy frame) or by interpreting
638 the saved LR (from a prologue or DWARF frame). So consult it and
639 trust the unwinders. */
60270718 640 CORE_ADDR cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
b39cc962 641
60270718
AB
642 /* Find and extract the thumb bit. */
643 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
9779414d 644 return (cpsr & t_bit) != 0;
b39cc962
DJ
645}
646
f9d67f43
DJ
647/* Search for the mapping symbol covering MEMADDR. If one is found,
648 return its type. Otherwise, return 0. If START is non-NULL,
649 set *START to the location of the mapping symbol. */
c906108c 650
f9d67f43
DJ
651static char
652arm_find_mapping_symbol (CORE_ADDR memaddr, CORE_ADDR *start)
c906108c 653{
60c5725c 654 struct obj_section *sec;
0428b8f5 655
60c5725c
DJ
656 /* If there are mapping symbols, consult them. */
657 sec = find_pc_section (memaddr);
658 if (sec != NULL)
659 {
98badbfd 660 arm_per_bfd *data = arm_bfd_data_key.get (sec->objfile->obfd.get ());
60c5725c
DJ
661 if (data != NULL)
662 {
4838e44c
SM
663 unsigned int section_idx = sec->the_bfd_section->index;
664 arm_mapping_symbol_vec &map
665 = data->section_maps[section_idx];
666
667 /* Sort the vector on first use. */
668 if (!data->section_maps_sorted[section_idx])
669 {
670 std::sort (map.begin (), map.end ());
671 data->section_maps_sorted[section_idx] = true;
672 }
673
0c1bcd23 674 arm_mapping_symbol map_key = { memaddr - sec->addr (), 0 };
54cc7474
SM
675 arm_mapping_symbol_vec::const_iterator it
676 = std::lower_bound (map.begin (), map.end (), map_key);
677
678 /* std::lower_bound finds the earliest ordered insertion
679 point. If the symbol at this position starts at this exact
680 address, we use that; otherwise, the preceding
681 mapping symbol covers this address. */
682 if (it < map.end ())
60c5725c 683 {
54cc7474 684 if (it->value == map_key.value)
60c5725c 685 {
f9d67f43 686 if (start)
0c1bcd23 687 *start = it->value + sec->addr ();
54cc7474 688 return it->type;
60c5725c
DJ
689 }
690 }
54cc7474
SM
691
692 if (it > map.begin ())
693 {
694 arm_mapping_symbol_vec::const_iterator prev_it
695 = it - 1;
696
697 if (start)
0c1bcd23 698 *start = prev_it->value + sec->addr ();
54cc7474
SM
699 return prev_it->type;
700 }
60c5725c
DJ
701 }
702 }
703
f9d67f43
DJ
704 return 0;
705}
706
707/* Determine if the program counter specified in MEMADDR is in a Thumb
708 function. This function should be called for addresses unrelated to
709 any executing frame; otherwise, prefer arm_frame_is_thumb. */
710
e3039479 711int
9779414d 712arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
f9d67f43 713{
7cbd4a93 714 struct bound_minimal_symbol sym;
f9d67f43 715 char type;
187b041e 716 arm_displaced_step_copy_insn_closure *dsc = nullptr;
08106042 717 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
187b041e
SM
718
719 if (gdbarch_displaced_step_copy_insn_closure_by_addr_p (gdbarch))
720 dsc = ((arm_displaced_step_copy_insn_closure * )
721 gdbarch_displaced_step_copy_insn_closure_by_addr
722 (gdbarch, current_inferior (), memaddr));
a42244db
YQ
723
724 /* If checking the mode of displaced instruction in copy area, the mode
725 should be determined by instruction on the original address. */
726 if (dsc)
727 {
136821d9
SM
728 displaced_debug_printf ("check mode of %.8lx instead of %.8lx",
729 (unsigned long) dsc->insn_addr,
730 (unsigned long) memaddr);
a42244db
YQ
731 memaddr = dsc->insn_addr;
732 }
f9d67f43
DJ
733
734 /* If bit 0 of the address is set, assume this is a Thumb address. */
735 if (IS_THUMB_ADDR (memaddr))
736 return 1;
737
738 /* If the user wants to override the symbol table, let him. */
739 if (strcmp (arm_force_mode_string, "arm") == 0)
740 return 0;
741 if (strcmp (arm_force_mode_string, "thumb") == 0)
742 return 1;
743
9779414d 744 /* ARM v6-M and v7-M are always in Thumb mode. */
345bd07c 745 if (tdep->is_m)
9779414d
DJ
746 return 1;
747
f9d67f43
DJ
748 /* If there are mapping symbols, consult them. */
749 type = arm_find_mapping_symbol (memaddr, NULL);
750 if (type)
751 return type == 't';
752
ed9a39eb 753 /* Thumb functions have a "special" bit set in minimal symbols. */
c906108c 754 sym = lookup_minimal_symbol_by_pc (memaddr);
7cbd4a93
TT
755 if (sym.minsym)
756 return (MSYMBOL_IS_SPECIAL (sym.minsym));
0428b8f5
DJ
757
758 /* If the user wants to override the fallback mode, let them. */
759 if (strcmp (arm_fallback_mode_string, "arm") == 0)
760 return 0;
761 if (strcmp (arm_fallback_mode_string, "thumb") == 0)
762 return 1;
763
764 /* If we couldn't find any symbol, but we're talking to a running
765 target, then trust the current value of $cpsr. This lets
766 "display/i $pc" always show the correct mode (though if there is
767 a symbol table we will not reach here, so it still may not be
18819fa6 768 displayed in the mode it will be executed). */
9dccd06e 769 if (target_has_registers ())
18819fa6 770 return arm_frame_is_thumb (get_current_frame ());
0428b8f5
DJ
771
772 /* Otherwise we're out of luck; we assume ARM. */
773 return 0;
c906108c
SS
774}
775
8b73ee20
LM
776static inline bool
777arm_m_addr_is_lockup (CORE_ADDR addr)
778{
779 switch (addr)
780 {
781 /* Values for lockup state.
782 For more details see "B1.5.15 Unrecoverable exception cases" in
783 both ARMv6-M and ARMv7-M Architecture Reference Manuals, or
784 see "B4.32 Lockup" in ARMv8-M Architecture Reference Manual. */
785 case 0xeffffffe:
786 case 0xfffffffe:
787 case 0xffffffff:
788 return true;
789
790 default:
791 /* Address is not lockup. */
792 return false;
793 }
794}
795
ca90e760 796/* Determine if the address specified equals any of these magic return
55ea94da 797 values, called EXC_RETURN, defined by the ARM v6-M, v7-M and v8-M
8b73ee20
LM
798 architectures. Also include lockup magic PC value.
799 Check also for FNC_RETURN if we have the v8-M security extension.
ca90e760
FH
800
801 From ARMv6-M Reference Manual B1.5.8
802 Table B1-5 Exception return behavior
803
804 EXC_RETURN Return To Return Stack
805 0xFFFFFFF1 Handler mode Main
806 0xFFFFFFF9 Thread mode Main
807 0xFFFFFFFD Thread mode Process
808
809 From ARMv7-M Reference Manual B1.5.8
810 Table B1-8 EXC_RETURN definition of exception return behavior, no FP
811
812 EXC_RETURN Return To Return Stack
813 0xFFFFFFF1 Handler mode Main
814 0xFFFFFFF9 Thread mode Main
815 0xFFFFFFFD Thread mode Process
816
817 Table B1-9 EXC_RETURN definition of exception return behavior, with
818 FP
819
820 EXC_RETURN Return To Return Stack Frame Type
821 0xFFFFFFE1 Handler mode Main Extended
822 0xFFFFFFE9 Thread mode Main Extended
823 0xFFFFFFED Thread mode Process Extended
824 0xFFFFFFF1 Handler mode Main Basic
825 0xFFFFFFF9 Thread mode Main Basic
826 0xFFFFFFFD Thread mode Process Basic
827
828 For more details see "B1.5.8 Exception return behavior"
55ea94da
FH
829 in both ARMv6-M and ARMv7-M Architecture Reference Manuals.
830
8db533e7
TS
831 From ARMv8-M Architecture Technical Reference, D1.2.95
832 FType, Mode and SPSEL bits are to be considered when the Security
833 Extension is not implemented.
55ea94da 834
8db533e7
TS
835 EXC_RETURN Return To Return Stack Frame Type
836 0xFFFFFFA0 Handler mode Main Extended
837 0xFFFFFFA8 Thread mode Main Extended
838 0xFFFFFFAC Thread mode Process Extended
839 0xFFFFFFB0 Handler mode Main Standard
840 0xFFFFFFB8 Thread mode Main Standard
841 0xFFFFFFBC Thread mode Process Standard */
ca90e760
FH
842
843static int
ef273377
CL
844arm_m_addr_is_magic (struct gdbarch *gdbarch, CORE_ADDR addr)
845{
8b73ee20
LM
846 if (arm_m_addr_is_lockup (addr))
847 return 1;
848
08106042 849 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ef273377
CL
850 if (tdep->have_sec_ext)
851 {
852 switch ((addr & 0xff000000))
853 {
854 case 0xff000000: /* EXC_RETURN pattern. */
855 case 0xfe000000: /* FNC_RETURN pattern. */
856 return 1;
857 default:
858 return 0;
859 }
860 }
861 else
862 {
863 switch (addr)
864 {
865 /* Values from ARMv8-M Architecture Technical Reference. */
8db533e7
TS
866 case 0xffffffa0:
867 case 0xffffffa8:
868 case 0xffffffac:
ef273377
CL
869 case 0xffffffb0:
870 case 0xffffffb8:
871 case 0xffffffbc:
872 /* Values from Tables in B1.5.8 the EXC_RETURN definitions of
873 the exception return behavior. */
874 case 0xffffffe1:
875 case 0xffffffe9:
876 case 0xffffffed:
877 case 0xfffffff1:
878 case 0xfffffff9:
879 case 0xfffffffd:
880 /* Address is magic. */
881 return 1;
ca90e760 882
ef273377
CL
883 default:
884 /* Address is not magic. */
885 return 0;
886 }
ca90e760
FH
887 }
888}
889
181c1381 890/* Remove useless bits from addresses in a running program. */
34e8f22d 891static CORE_ADDR
24568a2c 892arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
c906108c 893{
08106042 894 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
345bd07c 895
2ae28aa9
YQ
896 /* On M-profile devices, do not strip the low bit from EXC_RETURN
897 (the magic exception return address). */
ef273377 898 if (tdep->is_m && arm_m_addr_is_magic (gdbarch, val))
2ae28aa9
YQ
899 return val;
900
a3a2ee65 901 if (arm_apcs_32)
dd6be234 902 return UNMAKE_THUMB_ADDR (val);
c906108c 903 else
a3a2ee65 904 return (val & 0x03fffffc);
c906108c
SS
905}
906
0d39a070 907/* Return 1 if PC is the start of a compiler helper function which
e0634ccf
UW
908 can be safely ignored during prologue skipping. IS_THUMB is true
909 if the function is known to be a Thumb function due to the way it
910 is being called. */
0d39a070 911static int
e0634ccf 912skip_prologue_function (struct gdbarch *gdbarch, CORE_ADDR pc, int is_thumb)
0d39a070 913{
e0634ccf 914 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7cbd4a93 915 struct bound_minimal_symbol msym;
0d39a070
DJ
916
917 msym = lookup_minimal_symbol_by_pc (pc);
7cbd4a93 918 if (msym.minsym != NULL
4aeddc50 919 && msym.value_address () == pc
c9d95fa3 920 && msym.minsym->linkage_name () != NULL)
e0634ccf 921 {
c9d95fa3 922 const char *name = msym.minsym->linkage_name ();
0d39a070 923
e0634ccf
UW
924 /* The GNU linker's Thumb call stub to foo is named
925 __foo_from_thumb. */
926 if (strstr (name, "_from_thumb") != NULL)
927 name += 2;
0d39a070 928
e0634ccf
UW
929 /* On soft-float targets, __truncdfsf2 is called to convert promoted
930 arguments to their argument types in non-prototyped
931 functions. */
61012eef 932 if (startswith (name, "__truncdfsf2"))
e0634ccf 933 return 1;
61012eef 934 if (startswith (name, "__aeabi_d2f"))
e0634ccf 935 return 1;
0d39a070 936
e0634ccf 937 /* Internal functions related to thread-local storage. */
61012eef 938 if (startswith (name, "__tls_get_addr"))
e0634ccf 939 return 1;
61012eef 940 if (startswith (name, "__aeabi_read_tp"))
e0634ccf
UW
941 return 1;
942 }
943 else
944 {
945 /* If we run against a stripped glibc, we may be unable to identify
946 special functions by name. Check for one important case,
947 __aeabi_read_tp, by comparing the *code* against the default
948 implementation (this is hand-written ARM assembler in glibc). */
949
950 if (!is_thumb
198cd59d 951 && read_code_unsigned_integer (pc, 4, byte_order_for_code)
e0634ccf 952 == 0xe3e00a0f /* mov r0, #0xffff0fff */
198cd59d 953 && read_code_unsigned_integer (pc + 4, 4, byte_order_for_code)
e0634ccf
UW
954 == 0xe240f01f) /* sub pc, r0, #31 */
955 return 1;
956 }
ec3d575a 957
0d39a070
DJ
958 return 0;
959}
960
621c6d5b
YQ
961/* Extract the immediate from instruction movw/movt of encoding T. INSN1 is
962 the first 16-bit of instruction, and INSN2 is the second 16-bit of
963 instruction. */
964#define EXTRACT_MOVW_MOVT_IMM_T(insn1, insn2) \
965 ((bits ((insn1), 0, 3) << 12) \
966 | (bits ((insn1), 10, 10) << 11) \
967 | (bits ((insn2), 12, 14) << 8) \
968 | bits ((insn2), 0, 7))
969
970/* Extract the immediate from instruction movw/movt of encoding A. INSN is
971 the 32-bit instruction. */
972#define EXTRACT_MOVW_MOVT_IMM_A(insn) \
973 ((bits ((insn), 16, 19) << 12) \
974 | bits ((insn), 0, 11))
975
ec3d575a
UW
976/* Decode immediate value; implements ThumbExpandImmediate pseudo-op. */
977
978static unsigned int
979thumb_expand_immediate (unsigned int imm)
980{
981 unsigned int count = imm >> 7;
982
983 if (count < 8)
984 switch (count / 2)
985 {
986 case 0:
987 return imm & 0xff;
988 case 1:
989 return (imm & 0xff) | ((imm & 0xff) << 16);
990 case 2:
991 return ((imm & 0xff) << 8) | ((imm & 0xff) << 24);
992 case 3:
993 return (imm & 0xff) | ((imm & 0xff) << 8)
994 | ((imm & 0xff) << 16) | ((imm & 0xff) << 24);
995 }
996
997 return (0x80 | (imm & 0x7f)) << (32 - count);
998}
999
540314bd
YQ
1000/* Return 1 if the 16-bit Thumb instruction INSN restores SP in
1001 epilogue, 0 otherwise. */
1002
1003static int
1004thumb_instruction_restores_sp (unsigned short insn)
1005{
1006 return (insn == 0x46bd /* mov sp, r7 */
1007 || (insn & 0xff80) == 0xb000 /* add sp, imm */
1008 || (insn & 0xfe00) == 0xbc00); /* pop <registers> */
1009}
1010
29d73ae4
DJ
1011/* Analyze a Thumb prologue, looking for a recognizable stack frame
1012 and frame pointer. Scan until we encounter a store that could
0d39a070
DJ
1013 clobber the stack frame unexpectedly, or an unknown instruction.
1014 Return the last address which is definitely safe to skip for an
1015 initial breakpoint. */
c906108c
SS
1016
1017static CORE_ADDR
29d73ae4
DJ
1018thumb_analyze_prologue (struct gdbarch *gdbarch,
1019 CORE_ADDR start, CORE_ADDR limit,
1020 struct arm_prologue_cache *cache)
c906108c 1021{
08106042 1022 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
0d39a070 1023 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
e17a4113 1024 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
29d73ae4
DJ
1025 int i;
1026 pv_t regs[16];
29d73ae4 1027 CORE_ADDR offset;
ec3d575a 1028 CORE_ADDR unrecognized_pc = 0;
da3c6d4a 1029
29d73ae4
DJ
1030 for (i = 0; i < 16; i++)
1031 regs[i] = pv_register (i, 0);
f7b7ed97 1032 pv_area stack (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
29d73ae4 1033
29d73ae4 1034 while (start < limit)
c906108c 1035 {
29d73ae4 1036 unsigned short insn;
6b09f134 1037 std::optional<bool> ra_signed_state;
29d73ae4 1038
198cd59d 1039 insn = read_code_unsigned_integer (start, 2, byte_order_for_code);
9d4fde75 1040
94c30b78 1041 if ((insn & 0xfe00) == 0xb400) /* push { rlist } */
da59e081 1042 {
29d73ae4
DJ
1043 int regno;
1044 int mask;
4be43953 1045
f7b7ed97 1046 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953 1047 break;
29d73ae4
DJ
1048
1049 /* Bits 0-7 contain a mask for registers R0-R7. Bit 8 says
1050 whether to save LR (R14). */
1051 mask = (insn & 0xff) | ((insn & 0x100) << 6);
1052
1053 /* Calculate offsets of saved R0-R7 and LR. */
1054 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
1055 if (mask & (1 << regno))
1056 {
29d73ae4
DJ
1057 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
1058 -4);
f7b7ed97 1059 stack.store (regs[ARM_SP_REGNUM], 4, regs[regno]);
29d73ae4 1060 }
da59e081 1061 }
1db01f22 1062 else if ((insn & 0xff80) == 0xb080) /* sub sp, #imm */
da59e081 1063 {
29d73ae4 1064 offset = (insn & 0x7f) << 2; /* get scaled offset */
1db01f22
YQ
1065 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
1066 -offset);
da59e081 1067 }
808f7ab1
YQ
1068 else if (thumb_instruction_restores_sp (insn))
1069 {
1070 /* Don't scan past the epilogue. */
1071 break;
1072 }
0d39a070
DJ
1073 else if ((insn & 0xf800) == 0xa800) /* add Rd, sp, #imm */
1074 regs[bits (insn, 8, 10)] = pv_add_constant (regs[ARM_SP_REGNUM],
1075 (insn & 0xff) << 2);
1076 else if ((insn & 0xfe00) == 0x1c00 /* add Rd, Rn, #imm */
1077 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
1078 regs[bits (insn, 0, 2)] = pv_add_constant (regs[bits (insn, 3, 5)],
1079 bits (insn, 6, 8));
1080 else if ((insn & 0xf800) == 0x3000 /* add Rd, #imm */
1081 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
1082 regs[bits (insn, 8, 10)] = pv_add_constant (regs[bits (insn, 8, 10)],
1083 bits (insn, 0, 7));
1084 else if ((insn & 0xfe00) == 0x1800 /* add Rd, Rn, Rm */
1085 && pv_is_register (regs[bits (insn, 6, 8)], ARM_SP_REGNUM)
1086 && pv_is_constant (regs[bits (insn, 3, 5)]))
1087 regs[bits (insn, 0, 2)] = pv_add (regs[bits (insn, 3, 5)],
1088 regs[bits (insn, 6, 8)]);
1089 else if ((insn & 0xff00) == 0x4400 /* add Rd, Rm */
1090 && pv_is_constant (regs[bits (insn, 3, 6)]))
1091 {
1092 int rd = (bit (insn, 7) << 3) + bits (insn, 0, 2);
1093 int rm = bits (insn, 3, 6);
1094 regs[rd] = pv_add (regs[rd], regs[rm]);
1095 }
29d73ae4 1096 else if ((insn & 0xff00) == 0x4600) /* mov hi, lo or mov lo, hi */
da59e081 1097 {
29d73ae4
DJ
1098 int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
1099 int src_reg = (insn & 0x78) >> 3;
1100 regs[dst_reg] = regs[src_reg];
da59e081 1101 }
29d73ae4 1102 else if ((insn & 0xf800) == 0x9000) /* str rd, [sp, #off] */
da59e081 1103 {
29d73ae4
DJ
1104 /* Handle stores to the stack. Normally pushes are used,
1105 but with GCC -mtpcs-frame, there may be other stores
1106 in the prologue to create the frame. */
1107 int regno = (insn >> 8) & 0x7;
1108 pv_t addr;
1109
1110 offset = (insn & 0xff) << 2;
1111 addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
1112
f7b7ed97 1113 if (stack.store_would_trash (addr))
29d73ae4
DJ
1114 break;
1115
f7b7ed97 1116 stack.store (addr, 4, regs[regno]);
da59e081 1117 }
0d39a070
DJ
1118 else if ((insn & 0xf800) == 0x6000) /* str rd, [rn, #off] */
1119 {
1120 int rd = bits (insn, 0, 2);
1121 int rn = bits (insn, 3, 5);
1122 pv_t addr;
1123
1124 offset = bits (insn, 6, 10) << 2;
1125 addr = pv_add_constant (regs[rn], offset);
1126
f7b7ed97 1127 if (stack.store_would_trash (addr))
0d39a070
DJ
1128 break;
1129
f7b7ed97 1130 stack.store (addr, 4, regs[rd]);
0d39a070
DJ
1131 }
1132 else if (((insn & 0xf800) == 0x7000 /* strb Rd, [Rn, #off] */
1133 || (insn & 0xf800) == 0x8000) /* strh Rd, [Rn, #off] */
1134 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
1135 /* Ignore stores of argument registers to the stack. */
1136 ;
1137 else if ((insn & 0xf800) == 0xc800 /* ldmia Rn!, { registers } */
1138 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
1139 /* Ignore block loads from the stack, potentially copying
1140 parameters from memory. */
1141 ;
1142 else if ((insn & 0xf800) == 0x9800 /* ldr Rd, [Rn, #immed] */
1143 || ((insn & 0xf800) == 0x6800 /* ldr Rd, [sp, #immed] */
1144 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM)))
1145 /* Similarly ignore single loads from the stack. */
1146 ;
1147 else if ((insn & 0xffc0) == 0x0000 /* lsls Rd, Rm, #0 */
1148 || (insn & 0xffc0) == 0x1c00) /* add Rd, Rn, #0 */
1149 /* Skip register copies, i.e. saves to another register
1150 instead of the stack. */
1151 ;
1152 else if ((insn & 0xf800) == 0x2000) /* movs Rd, #imm */
1153 /* Recognize constant loads; even with small stacks these are necessary
1154 on Thumb. */
1155 regs[bits (insn, 8, 10)] = pv_constant (bits (insn, 0, 7));
1156 else if ((insn & 0xf800) == 0x4800) /* ldr Rd, [pc, #imm] */
1157 {
1158 /* Constant pool loads, for the same reason. */
1159 unsigned int constant;
1160 CORE_ADDR loc;
1161
1162 loc = start + 4 + bits (insn, 0, 7) * 4;
1163 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1164 regs[bits (insn, 8, 10)] = pv_constant (constant);
1165 }
db24da6d 1166 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instructions. */
0d39a070 1167 {
0d39a070
DJ
1168 unsigned short inst2;
1169
198cd59d
YQ
1170 inst2 = read_code_unsigned_integer (start + 2, 2,
1171 byte_order_for_code);
a01567f4 1172 uint32_t whole_insn = (insn << 16) | inst2;
0d39a070
DJ
1173
1174 if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
1175 {
1176 /* BL, BLX. Allow some special function calls when
1177 skipping the prologue; GCC generates these before
1178 storing arguments to the stack. */
1179 CORE_ADDR nextpc;
1180 int j1, j2, imm1, imm2;
1181
1182 imm1 = sbits (insn, 0, 10);
1183 imm2 = bits (inst2, 0, 10);
1184 j1 = bit (inst2, 13);
1185 j2 = bit (inst2, 11);
1186
1187 offset = ((imm1 << 12) + (imm2 << 1));
1188 offset ^= ((!j2) << 22) | ((!j1) << 23);
1189
1190 nextpc = start + 4 + offset;
1191 /* For BLX make sure to clear the low bits. */
1192 if (bit (inst2, 12) == 0)
1193 nextpc = nextpc & 0xfffffffc;
1194
e0634ccf
UW
1195 if (!skip_prologue_function (gdbarch, nextpc,
1196 bit (inst2, 12) != 0))
0d39a070
DJ
1197 break;
1198 }
ec3d575a 1199
0963b4bd
MS
1200 else if ((insn & 0xffd0) == 0xe900 /* stmdb Rn{!},
1201 { registers } */
ec3d575a
UW
1202 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1203 {
1204 pv_t addr = regs[bits (insn, 0, 3)];
1205 int regno;
1206
f7b7ed97 1207 if (stack.store_would_trash (addr))
ec3d575a
UW
1208 break;
1209
1210 /* Calculate offsets of saved registers. */
1211 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
1212 if (inst2 & (1 << regno))
1213 {
1214 addr = pv_add_constant (addr, -4);
f7b7ed97 1215 stack.store (addr, 4, regs[regno]);
ec3d575a
UW
1216 }
1217
1218 if (insn & 0x0020)
1219 regs[bits (insn, 0, 3)] = addr;
1220 }
1221
fcaa1071
CL
1222 /* vstmdb Rn{!}, { D-registers } (aka vpush). */
1223 else if ((insn & 0xff20) == 0xed20
1224 && (inst2 & 0x0f00) == 0x0b00
1225 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1226 {
1227 /* Address SP points to. */
1228 pv_t addr = regs[bits (insn, 0, 3)];
1229
1230 /* Number of registers saved. */
1231 unsigned int number = bits (inst2, 0, 7) >> 1;
1232
1233 /* First register to save. */
1234 int vd = bits (inst2, 12, 15) | (bits (insn, 6, 6) << 4);
1235
1236 if (stack.store_would_trash (addr))
1237 break;
1238
1239 /* Calculate offsets of saved registers. */
1240 for (; number > 0; number--)
1241 {
1242 addr = pv_add_constant (addr, -8);
1243 stack.store (addr, 8, pv_register (ARM_D0_REGNUM
1244 + vd + number, 0));
1245 }
1246
1247 /* Writeback SP to account for the saved registers. */
1248 regs[bits (insn, 0, 3)] = addr;
1249 }
1250
0963b4bd
MS
1251 else if ((insn & 0xff50) == 0xe940 /* strd Rt, Rt2,
1252 [Rn, #+/-imm]{!} */
ec3d575a
UW
1253 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1254 {
1255 int regno1 = bits (inst2, 12, 15);
1256 int regno2 = bits (inst2, 8, 11);
1257 pv_t addr = regs[bits (insn, 0, 3)];
1258
1259 offset = inst2 & 0xff;
1260 if (insn & 0x0080)
1261 addr = pv_add_constant (addr, offset);
1262 else
1263 addr = pv_add_constant (addr, -offset);
1264
f7b7ed97 1265 if (stack.store_would_trash (addr))
ec3d575a
UW
1266 break;
1267
f7b7ed97
TT
1268 stack.store (addr, 4, regs[regno1]);
1269 stack.store (pv_add_constant (addr, 4),
1270 4, regs[regno2]);
ec3d575a
UW
1271
1272 if (insn & 0x0020)
1273 regs[bits (insn, 0, 3)] = addr;
1274 }
1275
1276 else if ((insn & 0xfff0) == 0xf8c0 /* str Rt,[Rn,+/-#imm]{!} */
1277 && (inst2 & 0x0c00) == 0x0c00
1278 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1279 {
1280 int regno = bits (inst2, 12, 15);
1281 pv_t addr = regs[bits (insn, 0, 3)];
1282
1283 offset = inst2 & 0xff;
1284 if (inst2 & 0x0200)
1285 addr = pv_add_constant (addr, offset);
1286 else
1287 addr = pv_add_constant (addr, -offset);
1288
f7b7ed97 1289 if (stack.store_would_trash (addr))
ec3d575a
UW
1290 break;
1291
f7b7ed97 1292 stack.store (addr, 4, regs[regno]);
ec3d575a
UW
1293
1294 if (inst2 & 0x0100)
1295 regs[bits (insn, 0, 3)] = addr;
1296 }
1297
1298 else if ((insn & 0xfff0) == 0xf8c0 /* str.w Rt,[Rn,#imm] */
1299 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1300 {
1301 int regno = bits (inst2, 12, 15);
1302 pv_t addr;
1303
1304 offset = inst2 & 0xfff;
1305 addr = pv_add_constant (regs[bits (insn, 0, 3)], offset);
1306
f7b7ed97 1307 if (stack.store_would_trash (addr))
ec3d575a
UW
1308 break;
1309
f7b7ed97 1310 stack.store (addr, 4, regs[regno]);
ec3d575a
UW
1311 }
1312
1313 else if ((insn & 0xffd0) == 0xf880 /* str{bh}.w Rt,[Rn,#imm] */
0d39a070 1314 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 1315 /* Ignore stores of argument registers to the stack. */
0d39a070 1316 ;
ec3d575a
UW
1317
1318 else if ((insn & 0xffd0) == 0xf800 /* str{bh} Rt,[Rn,#+/-imm] */
1319 && (inst2 & 0x0d00) == 0x0c00
0d39a070 1320 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 1321 /* Ignore stores of argument registers to the stack. */
0d39a070 1322 ;
ec3d575a 1323
0963b4bd
MS
1324 else if ((insn & 0xffd0) == 0xe890 /* ldmia Rn[!],
1325 { registers } */
ec3d575a
UW
1326 && (inst2 & 0x8000) == 0x0000
1327 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1328 /* Ignore block loads from the stack, potentially copying
1329 parameters from memory. */
0d39a070 1330 ;
ec3d575a 1331
f8c6d152 1332 else if ((insn & 0xff70) == 0xe950 /* ldrd Rt, Rt2,
0963b4bd 1333 [Rn, #+/-imm] */
0d39a070 1334 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 1335 /* Similarly ignore dual loads from the stack. */
0d39a070 1336 ;
ec3d575a
UW
1337
1338 else if ((insn & 0xfff0) == 0xf850 /* ldr Rt,[Rn,#+/-imm] */
1339 && (inst2 & 0x0d00) == 0x0c00
0d39a070 1340 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 1341 /* Similarly ignore single loads from the stack. */
0d39a070 1342 ;
ec3d575a
UW
1343
1344 else if ((insn & 0xfff0) == 0xf8d0 /* ldr.w Rt,[Rn,#imm] */
0d39a070 1345 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 1346 /* Similarly ignore single loads from the stack. */
0d39a070 1347 ;
ec3d575a
UW
1348
1349 else if ((insn & 0xfbf0) == 0xf100 /* add.w Rd, Rn, #imm */
1350 && (inst2 & 0x8000) == 0x0000)
1351 {
1352 unsigned int imm = ((bits (insn, 10, 10) << 11)
1353 | (bits (inst2, 12, 14) << 8)
1354 | bits (inst2, 0, 7));
1355
1356 regs[bits (inst2, 8, 11)]
1357 = pv_add_constant (regs[bits (insn, 0, 3)],
1358 thumb_expand_immediate (imm));
1359 }
1360
1361 else if ((insn & 0xfbf0) == 0xf200 /* addw Rd, Rn, #imm */
1362 && (inst2 & 0x8000) == 0x0000)
0d39a070 1363 {
ec3d575a
UW
1364 unsigned int imm = ((bits (insn, 10, 10) << 11)
1365 | (bits (inst2, 12, 14) << 8)
1366 | bits (inst2, 0, 7));
1367
1368 regs[bits (inst2, 8, 11)]
1369 = pv_add_constant (regs[bits (insn, 0, 3)], imm);
1370 }
1371
1372 else if ((insn & 0xfbf0) == 0xf1a0 /* sub.w Rd, Rn, #imm */
1373 && (inst2 & 0x8000) == 0x0000)
1374 {
1375 unsigned int imm = ((bits (insn, 10, 10) << 11)
1376 | (bits (inst2, 12, 14) << 8)
1377 | bits (inst2, 0, 7));
1378
1379 regs[bits (inst2, 8, 11)]
1380 = pv_add_constant (regs[bits (insn, 0, 3)],
1381 - (CORE_ADDR) thumb_expand_immediate (imm));
1382 }
1383
1384 else if ((insn & 0xfbf0) == 0xf2a0 /* subw Rd, Rn, #imm */
1385 && (inst2 & 0x8000) == 0x0000)
1386 {
1387 unsigned int imm = ((bits (insn, 10, 10) << 11)
1388 | (bits (inst2, 12, 14) << 8)
1389 | bits (inst2, 0, 7));
1390
1391 regs[bits (inst2, 8, 11)]
1392 = pv_add_constant (regs[bits (insn, 0, 3)], - (CORE_ADDR) imm);
1393 }
1394
1395 else if ((insn & 0xfbff) == 0xf04f) /* mov.w Rd, #const */
1396 {
1397 unsigned int imm = ((bits (insn, 10, 10) << 11)
1398 | (bits (inst2, 12, 14) << 8)
1399 | bits (inst2, 0, 7));
1400
1401 regs[bits (inst2, 8, 11)]
1402 = pv_constant (thumb_expand_immediate (imm));
1403 }
1404
1405 else if ((insn & 0xfbf0) == 0xf240) /* movw Rd, #const */
1406 {
621c6d5b
YQ
1407 unsigned int imm
1408 = EXTRACT_MOVW_MOVT_IMM_T (insn, inst2);
ec3d575a
UW
1409
1410 regs[bits (inst2, 8, 11)] = pv_constant (imm);
1411 }
1412
1413 else if (insn == 0xea5f /* mov.w Rd,Rm */
1414 && (inst2 & 0xf0f0) == 0)
1415 {
1416 int dst_reg = (inst2 & 0x0f00) >> 8;
1417 int src_reg = inst2 & 0xf;
1418 regs[dst_reg] = regs[src_reg];
1419 }
1420
1421 else if ((insn & 0xff7f) == 0xf85f) /* ldr.w Rt,<label> */
1422 {
1423 /* Constant pool loads. */
1424 unsigned int constant;
1425 CORE_ADDR loc;
1426
cac395ea 1427 offset = bits (inst2, 0, 11);
ec3d575a
UW
1428 if (insn & 0x0080)
1429 loc = start + 4 + offset;
1430 else
1431 loc = start + 4 - offset;
1432
1433 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1434 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1435 }
1436
1437 else if ((insn & 0xff7f) == 0xe95f) /* ldrd Rt,Rt2,<label> */
1438 {
1439 /* Constant pool loads. */
1440 unsigned int constant;
1441 CORE_ADDR loc;
1442
cac395ea 1443 offset = bits (inst2, 0, 7) << 2;
ec3d575a
UW
1444 if (insn & 0x0080)
1445 loc = start + 4 + offset;
1446 else
1447 loc = start + 4 - offset;
1448
1449 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1450 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1451
1452 constant = read_memory_unsigned_integer (loc + 4, 4, byte_order);
1453 regs[bits (inst2, 8, 11)] = pv_constant (constant);
1454 }
a01567f4
LM
1455 /* Start of ARMv8.1-m PACBTI extension instructions. */
1456 else if (IS_PAC (whole_insn))
1457 {
1458 /* LR and SP are input registers. PAC is in R12. LR is
1459 signed from this point onwards. NOP space. */
1460 ra_signed_state = true;
1461 }
1462 else if (IS_PACBTI (whole_insn))
1463 {
1464 /* LR and SP are input registers. PAC is in R12 and PC is a
1465 valid BTI landing pad. LR is signed from this point onwards.
1466 NOP space. */
1467 ra_signed_state = true;
1468 }
1469 else if (IS_BTI (whole_insn))
1470 {
1471 /* Valid BTI landing pad. NOP space. */
1472 }
1473 else if (IS_PACG (whole_insn))
1474 {
1475 /* Sign Rn using Rm and store the PAC in Rd. Rd is signed from
1476 this point onwards. */
1477 ra_signed_state = true;
1478 }
1479 else if (IS_AUT (whole_insn) || IS_AUTG (whole_insn))
1480 {
1481 /* These instructions appear close to the epilogue, when signed
1482 pointers are getting authenticated. */
1483 ra_signed_state = false;
1484 }
1485 /* End of ARMv8.1-m PACBTI extension instructions */
ec3d575a
UW
1486 else if (thumb2_instruction_changes_pc (insn, inst2))
1487 {
1488 /* Don't scan past anything that might change control flow. */
0d39a070
DJ
1489 break;
1490 }
ec3d575a
UW
1491 else
1492 {
1493 /* The optimizer might shove anything into the prologue,
1494 so we just skip what we don't recognize. */
1495 unrecognized_pc = start;
1496 }
0d39a070 1497
a01567f4
LM
1498 /* Make sure we are dealing with a target that supports ARMv8.1-m
1499 PACBTI. */
1500 if (cache != nullptr && tdep->have_pacbti
1501 && ra_signed_state.has_value ())
1502 {
1503 arm_debug_printf ("Found pacbti instruction at %s",
1504 paddress (gdbarch, start));
1505 arm_debug_printf ("RA is %s",
05d63baf 1506 *ra_signed_state ? "signed" : "not signed");
a01567f4
LM
1507 cache->ra_signed_state = ra_signed_state;
1508 }
1509
0d39a070
DJ
1510 start += 2;
1511 }
ec3d575a 1512 else if (thumb_instruction_changes_pc (insn))
3d74b771 1513 {
ec3d575a 1514 /* Don't scan past anything that might change control flow. */
da3c6d4a 1515 break;
3d74b771 1516 }
ec3d575a
UW
1517 else
1518 {
1519 /* The optimizer might shove anything into the prologue,
1520 so we just skip what we don't recognize. */
1521 unrecognized_pc = start;
1522 }
29d73ae4
DJ
1523
1524 start += 2;
c906108c
SS
1525 }
1526
7cb6d92a
SM
1527 arm_debug_printf ("Prologue scan stopped at %s",
1528 paddress (gdbarch, start));
0d39a070 1529
ec3d575a
UW
1530 if (unrecognized_pc == 0)
1531 unrecognized_pc = start;
1532
29d73ae4 1533 if (cache == NULL)
f7b7ed97 1534 return unrecognized_pc;
29d73ae4 1535
29d73ae4
DJ
1536 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1537 {
1538 /* Frame pointer is fp. Frame size is constant. */
1539 cache->framereg = ARM_FP_REGNUM;
1540 cache->framesize = -regs[ARM_FP_REGNUM].k;
1541 }
1542 else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
1543 {
1544 /* Frame pointer is r7. Frame size is constant. */
1545 cache->framereg = THUMB_FP_REGNUM;
1546 cache->framesize = -regs[THUMB_FP_REGNUM].k;
1547 }
72a2e3dc 1548 else
29d73ae4
DJ
1549 {
1550 /* Try the stack pointer... this is a bit desperate. */
1551 cache->framereg = ARM_SP_REGNUM;
1552 cache->framesize = -regs[ARM_SP_REGNUM].k;
1553 }
29d73ae4 1554
de76473c 1555 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
f7b7ed97 1556 if (stack.find_reg (gdbarch, i, &offset))
10245fe8
YR
1557 {
1558 cache->saved_regs[i].set_addr (offset);
1559 if (i == ARM_SP_REGNUM)
1560 arm_cache_set_active_sp_value(cache, tdep, offset);
1561 }
29d73ae4 1562
ec3d575a 1563 return unrecognized_pc;
c906108c
SS
1564}
1565
621c6d5b
YQ
1566
1567/* Try to analyze the instructions starting from PC, which load symbol
1568 __stack_chk_guard. Return the address of instruction after loading this
1569 symbol, set the dest register number to *BASEREG, and set the size of
1570 instructions for loading symbol in OFFSET. Return 0 if instructions are
1571 not recognized. */
1572
1573static CORE_ADDR
1574arm_analyze_load_stack_chk_guard(CORE_ADDR pc, struct gdbarch *gdbarch,
1575 unsigned int *destreg, int *offset)
1576{
1577 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1578 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1579 unsigned int low, high, address;
1580
1581 address = 0;
1582 if (is_thumb)
1583 {
1584 unsigned short insn1
198cd59d 1585 = read_code_unsigned_integer (pc, 2, byte_order_for_code);
621c6d5b
YQ
1586
1587 if ((insn1 & 0xf800) == 0x4800) /* ldr Rd, #immed */
1588 {
1589 *destreg = bits (insn1, 8, 10);
1590 *offset = 2;
6ae274b7
YQ
1591 address = (pc & 0xfffffffc) + 4 + (bits (insn1, 0, 7) << 2);
1592 address = read_memory_unsigned_integer (address, 4,
1593 byte_order_for_code);
621c6d5b
YQ
1594 }
1595 else if ((insn1 & 0xfbf0) == 0xf240) /* movw Rd, #const */
1596 {
1597 unsigned short insn2
198cd59d 1598 = read_code_unsigned_integer (pc + 2, 2, byte_order_for_code);
621c6d5b
YQ
1599
1600 low = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1601
1602 insn1
198cd59d 1603 = read_code_unsigned_integer (pc + 4, 2, byte_order_for_code);
621c6d5b 1604 insn2
198cd59d 1605 = read_code_unsigned_integer (pc + 6, 2, byte_order_for_code);
621c6d5b
YQ
1606
1607 /* movt Rd, #const */
1608 if ((insn1 & 0xfbc0) == 0xf2c0)
1609 {
1610 high = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1611 *destreg = bits (insn2, 8, 11);
1612 *offset = 8;
1613 address = (high << 16 | low);
1614 }
1615 }
1616 }
1617 else
1618 {
2e9e421f 1619 unsigned int insn
198cd59d 1620 = read_code_unsigned_integer (pc, 4, byte_order_for_code);
2e9e421f 1621
6ae274b7 1622 if ((insn & 0x0e5f0000) == 0x041f0000) /* ldr Rd, [PC, #immed] */
2e9e421f 1623 {
6ae274b7
YQ
1624 address = bits (insn, 0, 11) + pc + 8;
1625 address = read_memory_unsigned_integer (address, 4,
1626 byte_order_for_code);
1627
2e9e421f
UW
1628 *destreg = bits (insn, 12, 15);
1629 *offset = 4;
1630 }
1631 else if ((insn & 0x0ff00000) == 0x03000000) /* movw Rd, #const */
1632 {
1633 low = EXTRACT_MOVW_MOVT_IMM_A (insn);
1634
1635 insn
198cd59d 1636 = read_code_unsigned_integer (pc + 4, 4, byte_order_for_code);
2e9e421f
UW
1637
1638 if ((insn & 0x0ff00000) == 0x03400000) /* movt Rd, #const */
1639 {
1640 high = EXTRACT_MOVW_MOVT_IMM_A (insn);
1641 *destreg = bits (insn, 12, 15);
1642 *offset = 8;
1643 address = (high << 16 | low);
1644 }
1645 }
621c6d5b
YQ
1646 }
1647
1648 return address;
1649}
1650
1651/* Try to skip a sequence of instructions used for stack protector. If PC
0963b4bd
MS
1652 points to the first instruction of this sequence, return the address of
1653 first instruction after this sequence, otherwise, return original PC.
621c6d5b
YQ
1654
1655 On arm, this sequence of instructions is composed of mainly three steps,
1656 Step 1: load symbol __stack_chk_guard,
1657 Step 2: load from address of __stack_chk_guard,
1658 Step 3: store it to somewhere else.
1659
1660 Usually, instructions on step 2 and step 3 are the same on various ARM
1661 architectures. On step 2, it is one instruction 'ldr Rx, [Rn, #0]', and
1662 on step 3, it is also one instruction 'str Rx, [r7, #immd]'. However,
1663 instructions in step 1 vary from different ARM architectures. On ARMv7,
1664 they are,
1665
1666 movw Rn, #:lower16:__stack_chk_guard
1667 movt Rn, #:upper16:__stack_chk_guard
1668
1669 On ARMv5t, it is,
1670
1671 ldr Rn, .Label
1672 ....
1673 .Lable:
1674 .word __stack_chk_guard
1675
1676 Since ldr/str is a very popular instruction, we can't use them as
1677 'fingerprint' or 'signature' of stack protector sequence. Here we choose
1678 sequence {movw/movt, ldr}/ldr/str plus symbol __stack_chk_guard, if not
1679 stripped, as the 'fingerprint' of a stack protector cdoe sequence. */
1680
1681static CORE_ADDR
1682arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
1683{
1684 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
22e048c9 1685 unsigned int basereg;
7cbd4a93 1686 struct bound_minimal_symbol stack_chk_guard;
621c6d5b
YQ
1687 int offset;
1688 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1689 CORE_ADDR addr;
1690
1691 /* Try to parse the instructions in Step 1. */
1692 addr = arm_analyze_load_stack_chk_guard (pc, gdbarch,
1693 &basereg, &offset);
1694 if (!addr)
1695 return pc;
1696
1697 stack_chk_guard = lookup_minimal_symbol_by_pc (addr);
6041179a
JB
1698 /* ADDR must correspond to a symbol whose name is __stack_chk_guard.
1699 Otherwise, this sequence cannot be for stack protector. */
1700 if (stack_chk_guard.minsym == NULL
c9d95fa3 1701 || !startswith (stack_chk_guard.minsym->linkage_name (), "__stack_chk_guard"))
621c6d5b
YQ
1702 return pc;
1703
1704 if (is_thumb)
1705 {
1706 unsigned int destreg;
1707 unsigned short insn
198cd59d 1708 = read_code_unsigned_integer (pc + offset, 2, byte_order_for_code);
621c6d5b
YQ
1709
1710 /* Step 2: ldr Rd, [Rn, #immed], encoding T1. */
1711 if ((insn & 0xf800) != 0x6800)
1712 return pc;
1713 if (bits (insn, 3, 5) != basereg)
1714 return pc;
1715 destreg = bits (insn, 0, 2);
1716
198cd59d
YQ
1717 insn = read_code_unsigned_integer (pc + offset + 2, 2,
1718 byte_order_for_code);
621c6d5b
YQ
1719 /* Step 3: str Rd, [Rn, #immed], encoding T1. */
1720 if ((insn & 0xf800) != 0x6000)
1721 return pc;
1722 if (destreg != bits (insn, 0, 2))
1723 return pc;
1724 }
1725 else
1726 {
1727 unsigned int destreg;
1728 unsigned int insn
198cd59d 1729 = read_code_unsigned_integer (pc + offset, 4, byte_order_for_code);
621c6d5b
YQ
1730
1731 /* Step 2: ldr Rd, [Rn, #immed], encoding A1. */
1732 if ((insn & 0x0e500000) != 0x04100000)
1733 return pc;
1734 if (bits (insn, 16, 19) != basereg)
1735 return pc;
1736 destreg = bits (insn, 12, 15);
1737 /* Step 3: str Rd, [Rn, #immed], encoding A1. */
198cd59d 1738 insn = read_code_unsigned_integer (pc + offset + 4,
621c6d5b
YQ
1739 4, byte_order_for_code);
1740 if ((insn & 0x0e500000) != 0x04000000)
1741 return pc;
1742 if (bits (insn, 12, 15) != destreg)
1743 return pc;
1744 }
1745 /* The size of total two instructions ldr/str is 4 on Thumb-2, while 8
1746 on arm. */
1747 if (is_thumb)
1748 return pc + offset + 4;
1749 else
1750 return pc + offset + 8;
1751}
1752
da3c6d4a
MS
1753/* Advance the PC across any function entry prologue instructions to
1754 reach some "real" code.
34e8f22d
RE
1755
1756 The APCS (ARM Procedure Call Standard) defines the following
ed9a39eb 1757 prologue:
c906108c 1758
c5aa993b
JM
1759 mov ip, sp
1760 [stmfd sp!, {a1,a2,a3,a4}]
1761 stmfd sp!, {...,fp,ip,lr,pc}
ed9a39eb
JM
1762 [stfe f7, [sp, #-12]!]
1763 [stfe f6, [sp, #-12]!]
1764 [stfe f5, [sp, #-12]!]
1765 [stfe f4, [sp, #-12]!]
0963b4bd 1766 sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn. */
c906108c 1767
34e8f22d 1768static CORE_ADDR
6093d2eb 1769arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
c906108c 1770{
22f2cf64 1771 CORE_ADDR func_addr, func_end_addr, limit_pc;
c906108c 1772
a89fea3c
JL
1773 /* See if we can determine the end of the prologue via the symbol table.
1774 If so, then return either PC, or the PC after the prologue, whichever
1775 is greater. */
22f2cf64
TV
1776 bool func_addr_found
1777 = find_pc_partial_function (pc, NULL, &func_addr, &func_end_addr);
1778
1779 /* Whether the function is thumb mode or not. */
1780 bool func_is_thumb = false;
1781
1782 if (func_addr_found)
c906108c 1783 {
d80b854b
UW
1784 CORE_ADDR post_prologue_pc
1785 = skip_prologue_using_sal (gdbarch, func_addr);
43f3e411 1786 struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);
0d39a070 1787
621c6d5b
YQ
1788 if (post_prologue_pc)
1789 post_prologue_pc
1790 = arm_skip_stack_protector (post_prologue_pc, gdbarch);
1791
1792
0d39a070
DJ
1793 /* GCC always emits a line note before the prologue and another
1794 one after, even if the two are at the same address or on the
1795 same line. Take advantage of this so that we do not need to
1796 know every instruction that might appear in the prologue. We
1797 will have producer information for most binaries; if it is
1798 missing (e.g. for -gstabs), assuming the GNU tools. */
1799 if (post_prologue_pc
43f3e411 1800 && (cust == NULL
ab5f850e
SM
1801 || cust->producer () == NULL
1802 || startswith (cust->producer (), "GNU ")
1803 || producer_is_llvm (cust->producer ())))
0d39a070
DJ
1804 return post_prologue_pc;
1805
a89fea3c 1806 if (post_prologue_pc != 0)
0d39a070
DJ
1807 {
1808 CORE_ADDR analyzed_limit;
1809
1810 /* For non-GCC compilers, make sure the entire line is an
1811 acceptable prologue; GDB will round this function's
1812 return value up to the end of the following line so we
1813 can not skip just part of a line (and we do not want to).
1814
1815 RealView does not treat the prologue specially, but does
1816 associate prologue code with the opening brace; so this
1817 lets us skip the first line if we think it is the opening
1818 brace. */
22f2cf64
TV
1819 func_is_thumb = arm_pc_is_thumb (gdbarch, func_addr);
1820 if (func_is_thumb)
0d39a070
DJ
1821 analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
1822 post_prologue_pc, NULL);
1823 else
9ecab40c
SM
1824 analyzed_limit
1825 = arm_analyze_prologue (gdbarch, func_addr, post_prologue_pc,
1826 NULL, target_arm_instruction_reader ());
0d39a070
DJ
1827
1828 if (analyzed_limit != post_prologue_pc)
1829 return func_addr;
1830
1831 return post_prologue_pc;
1832 }
c906108c
SS
1833 }
1834
a89fea3c
JL
1835 /* Can't determine prologue from the symbol table, need to examine
1836 instructions. */
c906108c 1837
a89fea3c
JL
1838 /* Find an upper limit on the function prologue using the debug
1839 information. If the debug information could not be used to provide
1840 that bound, then use an arbitrary large number as the upper bound. */
0963b4bd 1841 /* Like arm_scan_prologue, stop no later than pc + 64. */
d80b854b 1842 limit_pc = skip_prologue_using_sal (gdbarch, pc);
a89fea3c
JL
1843 if (limit_pc == 0)
1844 limit_pc = pc + 64; /* Magic. */
1845
22f2cf64
TV
1846 /* Set the correct adjustment based on whether the function is thumb mode or
1847 not. We use it to get the address of the last instruction in the
1848 function (as opposed to the first address of the next function). */
05d63baf 1849 CORE_ADDR adjustment = func_is_thumb ? 2 : 4;
22f2cf64
TV
1850
1851 limit_pc
05d63baf
LM
1852 = func_end_addr == 0 ? limit_pc : std::min (limit_pc,
1853 func_end_addr - adjustment);
c906108c 1854
29d73ae4 1855 /* Check if this is Thumb code. */
9779414d 1856 if (arm_pc_is_thumb (gdbarch, pc))
a89fea3c 1857 return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
21daaaaf 1858 else
9ecab40c
SM
1859 return arm_analyze_prologue (gdbarch, pc, limit_pc, NULL,
1860 target_arm_instruction_reader ());
c906108c 1861}
94c30b78 1862
c906108c
SS
1863/* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
1864 This function decodes a Thumb function prologue to determine:
1865 1) the size of the stack frame
1866 2) which registers are saved on it
1867 3) the offsets of saved regs
1868 4) the offset from the stack pointer to the frame pointer
c906108c 1869
da59e081
JM
1870 A typical Thumb function prologue would create this stack frame
1871 (offsets relative to FP)
c906108c
SS
1872 old SP -> 24 stack parameters
1873 20 LR
1874 16 R7
1875 R7 -> 0 local variables (16 bytes)
1876 SP -> -12 additional stack space (12 bytes)
1877 The frame size would thus be 36 bytes, and the frame offset would be
0963b4bd 1878 12 bytes. The frame register is R7.
da59e081 1879
da3c6d4a
MS
1880 The comments for thumb_skip_prolog() describe the algorithm we use
1881 to detect the end of the prolog. */
c5aa993b 1882
c906108c 1883static void
be8626e0 1884thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
b39cc962 1885 CORE_ADDR block_addr, struct arm_prologue_cache *cache)
c906108c
SS
1886{
1887 CORE_ADDR prologue_start;
1888 CORE_ADDR prologue_end;
c906108c 1889
b39cc962
DJ
1890 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1891 &prologue_end))
c906108c 1892 {
ec3d575a
UW
1893 /* See comment in arm_scan_prologue for an explanation of
1894 this heuristics. */
1895 if (prologue_end > prologue_start + 64)
1896 {
1897 prologue_end = prologue_start + 64;
1898 }
c906108c
SS
1899 }
1900 else
f7060f85
DJ
1901 /* We're in the boondocks: we have no idea where the start of the
1902 function is. */
1903 return;
c906108c 1904
325fac50 1905 prologue_end = std::min (prologue_end, prev_pc);
c906108c 1906
be8626e0 1907 thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
1908}
1909
f303bc3e
YQ
1910/* Return 1 if the ARM instruction INSN restores SP in epilogue, 0
1911 otherwise. */
1912
1913static int
1914arm_instruction_restores_sp (unsigned int insn)
1915{
1916 if (bits (insn, 28, 31) != INST_NV)
1917 {
1918 if ((insn & 0x0df0f000) == 0x0080d000
1919 /* ADD SP (register or immediate). */
1920 || (insn & 0x0df0f000) == 0x0040d000
1921 /* SUB SP (register or immediate). */
1922 || (insn & 0x0ffffff0) == 0x01a0d000
1923 /* MOV SP. */
1924 || (insn & 0x0fff0000) == 0x08bd0000
1925 /* POP (LDMIA). */
1926 || (insn & 0x0fff0000) == 0x049d0000)
1927 /* POP of a single register. */
1928 return 1;
1929 }
1930
1931 return 0;
1932}
1933
9ecab40c
SM
1934/* Implement immediate value decoding, as described in section A5.2.4
1935 (Modified immediate constants in ARM instructions) of the ARM Architecture
1936 Reference Manual (ARMv7-A and ARMv7-R edition). */
1937
1938static uint32_t
1939arm_expand_immediate (uint32_t imm)
1940{
1941 /* Immediate values are 12 bits long. */
1942 gdb_assert ((imm & 0xfffff000) == 0);
1943
1944 uint32_t unrotated_value = imm & 0xff;
1945 uint32_t rotate_amount = (imm & 0xf00) >> 7;
1946
1947 if (rotate_amount == 0)
1948 return unrotated_value;
1949
1950 return ((unrotated_value >> rotate_amount)
1951 | (unrotated_value << (32 - rotate_amount)));
1952}
1953
0d39a070
DJ
1954/* Analyze an ARM mode prologue starting at PROLOGUE_START and
1955 continuing no further than PROLOGUE_END. If CACHE is non-NULL,
1956 fill it in. Return the first address not recognized as a prologue
1957 instruction.
eb5492fa 1958
0d39a070
DJ
1959 We recognize all the instructions typically found in ARM prologues,
1960 plus harmless instructions which can be skipped (either for analysis
1961 purposes, or a more restrictive set that can be skipped when finding
1962 the end of the prologue). */
1963
1964static CORE_ADDR
1965arm_analyze_prologue (struct gdbarch *gdbarch,
1966 CORE_ADDR prologue_start, CORE_ADDR prologue_end,
9ecab40c
SM
1967 struct arm_prologue_cache *cache,
1968 const arm_instruction_reader &insn_reader)
0d39a070 1969{
0d39a070
DJ
1970 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1971 int regno;
1972 CORE_ADDR offset, current_pc;
1973 pv_t regs[ARM_FPS_REGNUM];
0d39a070 1974 CORE_ADDR unrecognized_pc = 0;
08106042 1975 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
0d39a070
DJ
1976
1977 /* Search the prologue looking for instructions that set up the
96baa820 1978 frame pointer, adjust the stack pointer, and save registers.
ed9a39eb 1979
96baa820
JM
1980 Be careful, however, and if it doesn't look like a prologue,
1981 don't try to scan it. If, for instance, a frameless function
1982 begins with stmfd sp!, then we will tell ourselves there is
b8d5e71d 1983 a frame, which will confuse stack traceback, as well as "finish"
96baa820 1984 and other operations that rely on a knowledge of the stack
0d39a070 1985 traceback. */
d4473757 1986
4be43953
DJ
1987 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1988 regs[regno] = pv_register (regno, 0);
f7b7ed97 1989 pv_area stack (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
4be43953 1990
94c30b78
MS
1991 for (current_pc = prologue_start;
1992 current_pc < prologue_end;
f43845b3 1993 current_pc += 4)
96baa820 1994 {
9ecab40c 1995 uint32_t insn = insn_reader.read (current_pc, byte_order_for_code);
9d4fde75 1996
94c30b78 1997 if (insn == 0xe1a0c00d) /* mov ip, sp */
f43845b3 1998 {
4be43953 1999 regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
28cd8767
JG
2000 continue;
2001 }
0d39a070
DJ
2002 else if ((insn & 0xfff00000) == 0xe2800000 /* add Rd, Rn, #n */
2003 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767 2004 {
9ecab40c 2005 uint32_t imm = arm_expand_immediate (insn & 0xfff);
0d39a070 2006 int rd = bits (insn, 12, 15);
0d39a070 2007 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
28cd8767
JG
2008 continue;
2009 }
0d39a070
DJ
2010 else if ((insn & 0xfff00000) == 0xe2400000 /* sub Rd, Rn, #n */
2011 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767 2012 {
9ecab40c 2013 uint32_t imm = arm_expand_immediate (insn & 0xfff);
0d39a070 2014 int rd = bits (insn, 12, 15);
0d39a070 2015 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
f43845b3
MS
2016 continue;
2017 }
0963b4bd
MS
2018 else if ((insn & 0xffff0fff) == 0xe52d0004) /* str Rd,
2019 [sp, #-4]! */
f43845b3 2020 {
f7b7ed97 2021 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
2022 break;
2023 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
f7b7ed97
TT
2024 stack.store (regs[ARM_SP_REGNUM], 4,
2025 regs[bits (insn, 12, 15)]);
f43845b3
MS
2026 continue;
2027 }
2028 else if ((insn & 0xffff0000) == 0xe92d0000)
d4473757
KB
2029 /* stmfd sp!, {..., fp, ip, lr, pc}
2030 or
2031 stmfd sp!, {a1, a2, a3, a4} */
c906108c 2032 {
d4473757 2033 int mask = insn & 0xffff;
ed9a39eb 2034
f7b7ed97 2035 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
2036 break;
2037
94c30b78 2038 /* Calculate offsets of saved registers. */
34e8f22d 2039 for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
d4473757
KB
2040 if (mask & (1 << regno))
2041 {
0963b4bd
MS
2042 regs[ARM_SP_REGNUM]
2043 = pv_add_constant (regs[ARM_SP_REGNUM], -4);
f7b7ed97 2044 stack.store (regs[ARM_SP_REGNUM], 4, regs[regno]);
d4473757
KB
2045 }
2046 }
0d39a070
DJ
2047 else if ((insn & 0xffff0000) == 0xe54b0000 /* strb rx,[r11,#-n] */
2048 || (insn & 0xffff00f0) == 0xe14b00b0 /* strh rx,[r11,#-n] */
f8bf5763 2049 || (insn & 0xffffc000) == 0xe50b0000) /* str rx,[r11,#-n] */
b8d5e71d
MS
2050 {
2051 /* No need to add this to saved_regs -- it's just an arg reg. */
2052 continue;
2053 }
0d39a070
DJ
2054 else if ((insn & 0xffff0000) == 0xe5cd0000 /* strb rx,[sp,#n] */
2055 || (insn & 0xffff00f0) == 0xe1cd00b0 /* strh rx,[sp,#n] */
f8bf5763 2056 || (insn & 0xffffc000) == 0xe58d0000) /* str rx,[sp,#n] */
f43845b3
MS
2057 {
2058 /* No need to add this to saved_regs -- it's just an arg reg. */
2059 continue;
2060 }
0963b4bd
MS
2061 else if ((insn & 0xfff00000) == 0xe8800000 /* stm Rn,
2062 { registers } */
0d39a070
DJ
2063 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
2064 {
2065 /* No need to add this to saved_regs -- it's just arg regs. */
2066 continue;
2067 }
d4473757
KB
2068 else if ((insn & 0xfffff000) == 0xe24cb000) /* sub fp, ip #n */
2069 {
9ecab40c 2070 uint32_t imm = arm_expand_immediate (insn & 0xfff);
4be43953 2071 regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
d4473757
KB
2072 }
2073 else if ((insn & 0xfffff000) == 0xe24dd000) /* sub sp, sp #n */
2074 {
9ecab40c 2075 uint32_t imm = arm_expand_immediate(insn & 0xfff);
4be43953 2076 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
d4473757 2077 }
0963b4bd
MS
2078 else if ((insn & 0xffff7fff) == 0xed6d0103 /* stfe f?,
2079 [sp, -#c]! */
345bd07c 2080 && tdep->have_fpa_registers)
d4473757 2081 {
f7b7ed97 2082 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
2083 break;
2084
2085 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
34e8f22d 2086 regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
f7b7ed97 2087 stack.store (regs[ARM_SP_REGNUM], 12, regs[regno]);
d4473757 2088 }
0963b4bd
MS
2089 else if ((insn & 0xffbf0fff) == 0xec2d0200 /* sfmfd f0, 4,
2090 [sp!] */
345bd07c 2091 && tdep->have_fpa_registers)
d4473757
KB
2092 {
2093 int n_saved_fp_regs;
2094 unsigned int fp_start_reg, fp_bound_reg;
2095
f7b7ed97 2096 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
2097 break;
2098
94c30b78 2099 if ((insn & 0x800) == 0x800) /* N0 is set */
96baa820 2100 {
d4473757
KB
2101 if ((insn & 0x40000) == 0x40000) /* N1 is set */
2102 n_saved_fp_regs = 3;
2103 else
2104 n_saved_fp_regs = 1;
96baa820 2105 }
d4473757 2106 else
96baa820 2107 {
d4473757
KB
2108 if ((insn & 0x40000) == 0x40000) /* N1 is set */
2109 n_saved_fp_regs = 2;
2110 else
2111 n_saved_fp_regs = 4;
96baa820 2112 }
d4473757 2113
34e8f22d 2114 fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
d4473757
KB
2115 fp_bound_reg = fp_start_reg + n_saved_fp_regs;
2116 for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
96baa820 2117 {
4be43953 2118 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
f7b7ed97
TT
2119 stack.store (regs[ARM_SP_REGNUM], 12,
2120 regs[fp_start_reg++]);
96baa820 2121 }
c906108c 2122 }
0d39a070
DJ
2123 else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
2124 {
2125 /* Allow some special function calls when skipping the
2126 prologue; GCC generates these before storing arguments to
2127 the stack. */
2128 CORE_ADDR dest = BranchDest (current_pc, insn);
2129
e0634ccf 2130 if (skip_prologue_function (gdbarch, dest, 0))
0d39a070
DJ
2131 continue;
2132 else
2133 break;
2134 }
d4473757 2135 else if ((insn & 0xf0000000) != 0xe0000000)
0963b4bd 2136 break; /* Condition not true, exit early. */
0d39a070
DJ
2137 else if (arm_instruction_changes_pc (insn))
2138 /* Don't scan past anything that might change control flow. */
2139 break;
f303bc3e
YQ
2140 else if (arm_instruction_restores_sp (insn))
2141 {
2142 /* Don't scan past the epilogue. */
2143 break;
2144 }
d19f7eee
UW
2145 else if ((insn & 0xfe500000) == 0xe8100000 /* ldm */
2146 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
2147 /* Ignore block loads from the stack, potentially copying
2148 parameters from memory. */
2149 continue;
2150 else if ((insn & 0xfc500000) == 0xe4100000
2151 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
2152 /* Similarly ignore single loads from the stack. */
2153 continue;
0d39a070
DJ
2154 else if ((insn & 0xffff0ff0) == 0xe1a00000)
2155 /* MOV Rd, Rm. Skip register copies, i.e. saves to another
2156 register instead of the stack. */
d4473757 2157 continue;
0d39a070
DJ
2158 else
2159 {
21daaaaf
YQ
2160 /* The optimizer might shove anything into the prologue, if
2161 we build up cache (cache != NULL) from scanning prologue,
2162 we just skip what we don't recognize and scan further to
2163 make cache as complete as possible. However, if we skip
2164 prologue, we'll stop immediately on unrecognized
2165 instruction. */
0d39a070 2166 unrecognized_pc = current_pc;
21daaaaf
YQ
2167 if (cache != NULL)
2168 continue;
2169 else
2170 break;
0d39a070 2171 }
c906108c
SS
2172 }
2173
0d39a070
DJ
2174 if (unrecognized_pc == 0)
2175 unrecognized_pc = current_pc;
2176
0d39a070
DJ
2177 if (cache)
2178 {
4072f920
YQ
2179 int framereg, framesize;
2180
2181 /* The frame size is just the distance from the frame register
2182 to the original stack pointer. */
2183 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
2184 {
2185 /* Frame pointer is fp. */
2186 framereg = ARM_FP_REGNUM;
2187 framesize = -regs[ARM_FP_REGNUM].k;
2188 }
2189 else
2190 {
2191 /* Try the stack pointer... this is a bit desperate. */
2192 framereg = ARM_SP_REGNUM;
2193 framesize = -regs[ARM_SP_REGNUM].k;
2194 }
2195
0d39a070
DJ
2196 cache->framereg = framereg;
2197 cache->framesize = framesize;
2198
2199 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
f7b7ed97 2200 if (stack.find_reg (gdbarch, regno, &offset))
10245fe8
YR
2201 {
2202 cache->saved_regs[regno].set_addr (offset);
2203 if (regno == ARM_SP_REGNUM)
2204 arm_cache_set_active_sp_value(cache, tdep, offset);
2205 }
0d39a070
DJ
2206 }
2207
7cb6d92a
SM
2208 arm_debug_printf ("Prologue scan stopped at %s",
2209 paddress (gdbarch, unrecognized_pc));
4be43953 2210
0d39a070
DJ
2211 return unrecognized_pc;
2212}
2213
2214static void
8480a37e 2215arm_scan_prologue (const frame_info_ptr &this_frame,
0d39a070
DJ
2216 struct arm_prologue_cache *cache)
2217{
2218 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2219 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
bec2ab5a 2220 CORE_ADDR prologue_start, prologue_end;
0d39a070
DJ
2221 CORE_ADDR prev_pc = get_frame_pc (this_frame);
2222 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
08106042 2223 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
0d39a070
DJ
2224
2225 /* Assume there is no frame until proven otherwise. */
2226 cache->framereg = ARM_SP_REGNUM;
2227 cache->framesize = 0;
2228
2229 /* Check for Thumb prologue. */
2230 if (arm_frame_is_thumb (this_frame))
2231 {
2232 thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
2233 return;
2234 }
2235
2236 /* Find the function prologue. If we can't find the function in
2237 the symbol table, peek in the stack frame to find the PC. */
2238 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
2239 &prologue_end))
2240 {
2241 /* One way to find the end of the prologue (which works well
dda83cd7 2242 for unoptimized code) is to do the following:
0d39a070
DJ
2243
2244 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
2245
2246 if (sal.line == 0)
2247 prologue_end = prev_pc;
2248 else if (sal.end < prologue_end)
2249 prologue_end = sal.end;
2250
2251 This mechanism is very accurate so long as the optimizer
2252 doesn't move any instructions from the function body into the
2253 prologue. If this happens, sal.end will be the last
2254 instruction in the first hunk of prologue code just before
2255 the first instruction that the scheduler has moved from
2256 the body to the prologue.
2257
2258 In order to make sure that we scan all of the prologue
2259 instructions, we use a slightly less accurate mechanism which
2260 may scan more than necessary. To help compensate for this
2261 lack of accuracy, the prologue scanning loop below contains
2262 several clauses which'll cause the loop to terminate early if
2263 an implausible prologue instruction is encountered.
2264
2265 The expression
2266
2267 prologue_start + 64
2268
2269 is a suitable endpoint since it accounts for the largest
2270 possible prologue plus up to five instructions inserted by
2271 the scheduler. */
2272
2273 if (prologue_end > prologue_start + 64)
2274 {
2275 prologue_end = prologue_start + 64; /* See above. */
2276 }
2277 }
2278 else
2279 {
2280 /* We have no symbol information. Our only option is to assume this
2281 function has a standard stack frame and the normal frame register.
2282 Then, we can find the value of our frame pointer on entrance to
2283 the callee (or at the present moment if this is the innermost frame).
2284 The value stored there should be the address of the stmfd + 8. */
2285 CORE_ADDR frame_loc;
7913a64c 2286 ULONGEST return_value;
0d39a070 2287
9e237747 2288 /* AAPCS does not use a frame register, so we can abort here. */
345bd07c 2289 if (tdep->arm_abi == ARM_ABI_AAPCS)
dda83cd7 2290 return;
9e237747 2291
0d39a070 2292 frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
7913a64c
YQ
2293 if (!safe_read_memory_unsigned_integer (frame_loc, 4, byte_order,
2294 &return_value))
dda83cd7 2295 return;
0d39a070 2296 else
dda83cd7
SM
2297 {
2298 prologue_start = gdbarch_addr_bits_remove
0d39a070 2299 (gdbarch, return_value) - 8;
dda83cd7
SM
2300 prologue_end = prologue_start + 64; /* See above. */
2301 }
0d39a070
DJ
2302 }
2303
2304 if (prev_pc < prologue_end)
2305 prologue_end = prev_pc;
2306
9ecab40c
SM
2307 arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache,
2308 target_arm_instruction_reader ());
c906108c
SS
2309}
2310
eb5492fa 2311static struct arm_prologue_cache *
8480a37e 2312arm_make_prologue_cache (const frame_info_ptr &this_frame)
c906108c 2313{
eb5492fa
DJ
2314 int reg;
2315 struct arm_prologue_cache *cache;
1ef3351b 2316 CORE_ADDR unwound_fp, prev_sp;
c5aa993b 2317
35d5d4ee 2318 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 2319 arm_cache_init (cache, this_frame);
c906108c 2320
a262aec2 2321 arm_scan_prologue (this_frame, cache);
848cfffb 2322
a262aec2 2323 unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
eb5492fa
DJ
2324 if (unwound_fp == 0)
2325 return cache;
c906108c 2326
ae7e2f45 2327 arm_gdbarch_tdep *tdep =
08106042 2328 gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45 2329
1ef3351b
YR
2330 prev_sp = unwound_fp + cache->framesize;
2331 arm_cache_set_active_sp_value (cache, tdep, prev_sp);
c906108c 2332
eb5492fa
DJ
2333 /* Calculate actual addresses of saved registers using offsets
2334 determined by arm_scan_prologue. */
a262aec2 2335 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
a9a87d35 2336 if (cache->saved_regs[reg].is_addr ())
1ef3351b
YR
2337 cache->saved_regs[reg].set_addr (cache->saved_regs[reg].addr () +
2338 prev_sp);
eb5492fa
DJ
2339
2340 return cache;
c906108c
SS
2341}
2342
c1ee9414
LM
2343/* Implementation of the stop_reason hook for arm_prologue frames. */
2344
2345static enum unwind_stop_reason
8480a37e 2346arm_prologue_unwind_stop_reason (const frame_info_ptr &this_frame,
c1ee9414
LM
2347 void **this_cache)
2348{
2349 struct arm_prologue_cache *cache;
2350 CORE_ADDR pc;
2351
2352 if (*this_cache == NULL)
2353 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 2354 cache = (struct arm_prologue_cache *) *this_cache;
c1ee9414
LM
2355
2356 /* This is meant to halt the backtrace at "_start". */
2357 pc = get_frame_pc (this_frame);
345bd07c 2358 gdbarch *arch = get_frame_arch (this_frame);
08106042 2359 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (arch);
345bd07c 2360 if (pc <= tdep->lowest_pc)
c1ee9414
LM
2361 return UNWIND_OUTERMOST;
2362
2363 /* If we've hit a wall, stop. */
ae7e2f45 2364 if (arm_cache_get_prev_sp_value (cache, tdep) == 0)
c1ee9414
LM
2365 return UNWIND_OUTERMOST;
2366
2367 return UNWIND_NO_REASON;
2368}
2369
eb5492fa
DJ
2370/* Our frame ID for a normal frame is the current function's starting PC
2371 and the caller's SP when we were called. */
c906108c 2372
148754e5 2373static void
8480a37e 2374arm_prologue_this_id (const frame_info_ptr &this_frame,
eb5492fa
DJ
2375 void **this_cache,
2376 struct frame_id *this_id)
c906108c 2377{
eb5492fa
DJ
2378 struct arm_prologue_cache *cache;
2379 struct frame_id id;
2c404490 2380 CORE_ADDR pc, func;
f079148d 2381
eb5492fa 2382 if (*this_cache == NULL)
a262aec2 2383 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 2384 cache = (struct arm_prologue_cache *) *this_cache;
2a451106 2385
ae7e2f45 2386 arm_gdbarch_tdep *tdep
08106042 2387 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45 2388
0e9e9abd
UW
2389 /* Use function start address as part of the frame ID. If we cannot
2390 identify the start address (due to missing symbol information),
2391 fall back to just using the current PC. */
c1ee9414 2392 pc = get_frame_pc (this_frame);
2c404490 2393 func = get_frame_func (this_frame);
0e9e9abd
UW
2394 if (!func)
2395 func = pc;
2396
ae7e2f45 2397 id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep), func);
eb5492fa 2398 *this_id = id;
c906108c
SS
2399}
2400
a262aec2 2401static struct value *
8480a37e 2402arm_prologue_prev_register (const frame_info_ptr &this_frame,
eb5492fa 2403 void **this_cache,
a262aec2 2404 int prev_regnum)
24de872b 2405{
24568a2c 2406 struct gdbarch *gdbarch = get_frame_arch (this_frame);
24de872b 2407 struct arm_prologue_cache *cache;
ef273377 2408 CORE_ADDR sp_value;
24de872b 2409
eb5492fa 2410 if (*this_cache == NULL)
a262aec2 2411 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 2412 cache = (struct arm_prologue_cache *) *this_cache;
24de872b 2413
08106042 2414 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
a01567f4
LM
2415
2416 /* If this frame has signed the return address, mark it as so. */
2417 if (tdep->have_pacbti && cache->ra_signed_state.has_value ()
2418 && *cache->ra_signed_state)
2419 set_frame_previous_pc_masked (this_frame);
2420
eb5492fa 2421 /* If we are asked to unwind the PC, then we need to return the LR
b39cc962
DJ
2422 instead. The prologue may save PC, but it will point into this
2423 frame's prologue, not the next frame's resume location. Also
2424 strip the saved T bit. A valid LR may have the low bit set, but
2425 a valid PC never does. */
eb5492fa 2426 if (prev_regnum == ARM_PC_REGNUM)
b39cc962
DJ
2427 {
2428 CORE_ADDR lr;
2429
2430 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2431 return frame_unwind_got_constant (this_frame, prev_regnum,
24568a2c 2432 arm_addr_bits_remove (gdbarch, lr));
b39cc962 2433 }
24de872b 2434
eb5492fa 2435 /* SP is generally not saved to the stack, but this frame is
a262aec2 2436 identified by the next frame's stack pointer at the time of the call.
eb5492fa
DJ
2437 The value was already reconstructed into PREV_SP. */
2438 if (prev_regnum == ARM_SP_REGNUM)
ae7e2f45
CL
2439 return frame_unwind_got_constant (this_frame, prev_regnum,
2440 arm_cache_get_prev_sp_value (cache, tdep));
eb5492fa 2441
ef273377
CL
2442 /* The value might be one of the alternative SP, if so, use the
2443 value already constructed. */
d65edaa0 2444 if (arm_is_alternative_sp_register (tdep, prev_regnum))
ef273377
CL
2445 {
2446 sp_value = arm_cache_get_sp_register (cache, tdep, prev_regnum);
2447 return frame_unwind_got_constant (this_frame, prev_regnum, sp_value);
2448 }
2449
b39cc962
DJ
2450 /* The CPSR may have been changed by the call instruction and by the
2451 called function. The only bit we can reconstruct is the T bit,
2452 by checking the low bit of LR as of the call. This is a reliable
2453 indicator of Thumb-ness except for some ARM v4T pre-interworking
2454 Thumb code, which could get away with a clear low bit as long as
2455 the called function did not use bx. Guess that all other
2456 bits are unchanged; the condition flags are presumably lost,
2457 but the processor status is likely valid. */
2458 if (prev_regnum == ARM_PS_REGNUM)
2459 {
8c9ae6df
YR
2460 ULONGEST cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
2461 CORE_ADDR lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
b39cc962 2462
8c9ae6df 2463 cpsr = reconstruct_t_bit (gdbarch, lr, cpsr);
b39cc962
DJ
2464 return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
2465 }
2466
a262aec2
DJ
2467 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
2468 prev_regnum);
eb5492fa
DJ
2469}
2470
6bd434d6 2471static frame_unwind arm_prologue_unwind = {
a154d838 2472 "arm prologue",
eb5492fa 2473 NORMAL_FRAME,
c1ee9414 2474 arm_prologue_unwind_stop_reason,
eb5492fa 2475 arm_prologue_this_id,
a262aec2
DJ
2476 arm_prologue_prev_register,
2477 NULL,
2478 default_frame_sniffer
eb5492fa
DJ
2479};
2480
0e9e9abd
UW
2481/* Maintain a list of ARM exception table entries per objfile, similar to the
2482 list of mapping symbols. We only cache entries for standard ARM-defined
2483 personality routines; the cache will contain only the frame unwinding
2484 instructions associated with the entry (not the descriptors). */
2485
0e9e9abd
UW
2486struct arm_exidx_entry
2487{
227031b2 2488 CORE_ADDR addr;
0e9e9abd 2489 gdb_byte *entry;
7a5d944b
TT
2490
2491 bool operator< (const arm_exidx_entry &other) const
2492 {
2493 return addr < other.addr;
2494 }
0e9e9abd 2495};
0e9e9abd
UW
2496
2497struct arm_exidx_data
2498{
7a5d944b 2499 std::vector<std::vector<arm_exidx_entry>> section_maps;
0e9e9abd
UW
2500};
2501
a2726d4f 2502/* Per-BFD key to store exception handling information. */
08b8a139 2503static const registry<bfd>::key<arm_exidx_data> arm_exidx_data_key;
0e9e9abd
UW
2504
2505static struct obj_section *
2506arm_obj_section_from_vma (struct objfile *objfile, bfd_vma vma)
2507{
5250cbc8 2508 for (obj_section *osect : objfile->sections ())
fd361982 2509 if (bfd_section_flags (osect->the_bfd_section) & SEC_ALLOC)
0e9e9abd
UW
2510 {
2511 bfd_vma start, size;
fd361982
AM
2512 start = bfd_section_vma (osect->the_bfd_section);
2513 size = bfd_section_size (osect->the_bfd_section);
0e9e9abd
UW
2514
2515 if (start <= vma && vma < start + size)
2516 return osect;
2517 }
2518
2519 return NULL;
2520}
2521
2522/* Parse contents of exception table and exception index sections
2523 of OBJFILE, and fill in the exception table entry cache.
2524
2525 For each entry that refers to a standard ARM-defined personality
2526 routine, extract the frame unwinding instructions (from either
2527 the index or the table section). The unwinding instructions
2528 are normalized by:
2529 - extracting them from the rest of the table data
2530 - converting to host endianness
2531 - appending the implicit 0xb0 ("Finish") code
2532
2533 The extracted and normalized instructions are stored for later
2534 retrieval by the arm_find_exidx_entry routine. */
2535
2536static void
2537arm_exidx_new_objfile (struct objfile *objfile)
2538{
0e9e9abd
UW
2539 struct arm_exidx_data *data;
2540 asection *exidx, *extab;
2541 bfd_vma exidx_vma = 0, extab_vma = 0;
0e9e9abd
UW
2542 LONGEST i;
2543
2544 /* If we've already touched this file, do nothing. */
74daa597 2545 if (arm_exidx_data_key.get (objfile->obfd.get ()) != nullptr)
0e9e9abd
UW
2546 return;
2547
2548 /* Read contents of exception table and index. */
98badbfd
TT
2549 exidx = bfd_get_section_by_name (objfile->obfd.get (),
2550 ELF_STRING_ARM_unwind);
984c7238 2551 gdb::byte_vector exidx_data;
0e9e9abd
UW
2552 if (exidx)
2553 {
fd361982
AM
2554 exidx_vma = bfd_section_vma (exidx);
2555 exidx_data.resize (bfd_section_size (exidx));
0e9e9abd 2556
98badbfd 2557 if (!bfd_get_section_contents (objfile->obfd.get (), exidx,
984c7238
TT
2558 exidx_data.data (), 0,
2559 exidx_data.size ()))
2560 return;
0e9e9abd
UW
2561 }
2562
98badbfd 2563 extab = bfd_get_section_by_name (objfile->obfd.get (), ".ARM.extab");
984c7238 2564 gdb::byte_vector extab_data;
0e9e9abd
UW
2565 if (extab)
2566 {
fd361982
AM
2567 extab_vma = bfd_section_vma (extab);
2568 extab_data.resize (bfd_section_size (extab));
0e9e9abd 2569
98badbfd 2570 if (!bfd_get_section_contents (objfile->obfd.get (), extab,
984c7238
TT
2571 extab_data.data (), 0,
2572 extab_data.size ()))
2573 return;
0e9e9abd
UW
2574 }
2575
2576 /* Allocate exception table data structure. */
98badbfd 2577 data = arm_exidx_data_key.emplace (objfile->obfd.get ());
7a5d944b 2578 data->section_maps.resize (objfile->obfd->section_count);
0e9e9abd
UW
2579
2580 /* Fill in exception table. */
984c7238 2581 for (i = 0; i < exidx_data.size () / 8; i++)
0e9e9abd
UW
2582 {
2583 struct arm_exidx_entry new_exidx_entry;
984c7238
TT
2584 bfd_vma idx = bfd_h_get_32 (objfile->obfd, exidx_data.data () + i * 8);
2585 bfd_vma val = bfd_h_get_32 (objfile->obfd,
2586 exidx_data.data () + i * 8 + 4);
0e9e9abd
UW
2587 bfd_vma addr = 0, word = 0;
2588 int n_bytes = 0, n_words = 0;
2589 struct obj_section *sec;
2590 gdb_byte *entry = NULL;
2591
2592 /* Extract address of start of function. */
2593 idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2594 idx += exidx_vma + i * 8;
2595
2596 /* Find section containing function and compute section offset. */
2597 sec = arm_obj_section_from_vma (objfile, idx);
2598 if (sec == NULL)
2599 continue;
fd361982 2600 idx -= bfd_section_vma (sec->the_bfd_section);
0e9e9abd
UW
2601
2602 /* Determine address of exception table entry. */
2603 if (val == 1)
2604 {
2605 /* EXIDX_CANTUNWIND -- no exception table entry present. */
2606 }
2607 else if ((val & 0xff000000) == 0x80000000)
2608 {
2609 /* Exception table entry embedded in .ARM.exidx
2610 -- must be short form. */
2611 word = val;
2612 n_bytes = 3;
2613 }
2614 else if (!(val & 0x80000000))
2615 {
2616 /* Exception table entry in .ARM.extab. */
2617 addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2618 addr += exidx_vma + i * 8 + 4;
2619
984c7238 2620 if (addr >= extab_vma && addr + 4 <= extab_vma + extab_data.size ())
0e9e9abd
UW
2621 {
2622 word = bfd_h_get_32 (objfile->obfd,
984c7238 2623 extab_data.data () + addr - extab_vma);
0e9e9abd
UW
2624 addr += 4;
2625
2626 if ((word & 0xff000000) == 0x80000000)
2627 {
2628 /* Short form. */
2629 n_bytes = 3;
2630 }
2631 else if ((word & 0xff000000) == 0x81000000
2632 || (word & 0xff000000) == 0x82000000)
2633 {
2634 /* Long form. */
2635 n_bytes = 2;
2636 n_words = ((word >> 16) & 0xff);
2637 }
2638 else if (!(word & 0x80000000))
2639 {
2640 bfd_vma pers;
2641 struct obj_section *pers_sec;
2642 int gnu_personality = 0;
2643
2644 /* Custom personality routine. */
2645 pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2646 pers = UNMAKE_THUMB_ADDR (pers + addr - 4);
2647
2648 /* Check whether we've got one of the variants of the
2649 GNU personality routines. */
2650 pers_sec = arm_obj_section_from_vma (objfile, pers);
2651 if (pers_sec)
2652 {
2653 static const char *personality[] =
2654 {
2655 "__gcc_personality_v0",
2656 "__gxx_personality_v0",
2657 "__gcj_personality_v0",
2658 "__gnu_objc_personality_v0",
2659 NULL
2660 };
2661
0c1bcd23 2662 CORE_ADDR pc = pers + pers_sec->offset ();
0e9e9abd
UW
2663 int k;
2664
2665 for (k = 0; personality[k]; k++)
2666 if (lookup_minimal_symbol_by_pc_name
2667 (pc, personality[k], objfile))
2668 {
2669 gnu_personality = 1;
2670 break;
2671 }
2672 }
2673
2674 /* If so, the next word contains a word count in the high
2675 byte, followed by the same unwind instructions as the
2676 pre-defined forms. */
2677 if (gnu_personality
984c7238 2678 && addr + 4 <= extab_vma + extab_data.size ())
0e9e9abd
UW
2679 {
2680 word = bfd_h_get_32 (objfile->obfd,
984c7238
TT
2681 (extab_data.data ()
2682 + addr - extab_vma));
0e9e9abd
UW
2683 addr += 4;
2684 n_bytes = 3;
2685 n_words = ((word >> 24) & 0xff);
2686 }
2687 }
2688 }
2689 }
2690
2691 /* Sanity check address. */
2692 if (n_words)
984c7238
TT
2693 if (addr < extab_vma
2694 || addr + 4 * n_words > extab_vma + extab_data.size ())
0e9e9abd
UW
2695 n_words = n_bytes = 0;
2696
2697 /* The unwind instructions reside in WORD (only the N_BYTES least
2698 significant bytes are valid), followed by N_WORDS words in the
2699 extab section starting at ADDR. */
2700 if (n_bytes || n_words)
2701 {
224c3ddb 2702 gdb_byte *p = entry
bae2a57f 2703 = (gdb_byte *) obstack_alloc (&objfile->per_bfd->storage_obstack,
224c3ddb 2704 n_bytes + n_words * 4 + 1);
0e9e9abd
UW
2705
2706 while (n_bytes--)
2707 *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);
2708
2709 while (n_words--)
2710 {
2711 word = bfd_h_get_32 (objfile->obfd,
984c7238 2712 extab_data.data () + addr - extab_vma);
0e9e9abd
UW
2713 addr += 4;
2714
2715 *p++ = (gdb_byte) ((word >> 24) & 0xff);
2716 *p++ = (gdb_byte) ((word >> 16) & 0xff);
2717 *p++ = (gdb_byte) ((word >> 8) & 0xff);
2718 *p++ = (gdb_byte) (word & 0xff);
2719 }
2720
2721 /* Implied "Finish" to terminate the list. */
2722 *p++ = 0xb0;
2723 }
2724
2725 /* Push entry onto vector. They are guaranteed to always
2726 appear in order of increasing addresses. */
2727 new_exidx_entry.addr = idx;
2728 new_exidx_entry.entry = entry;
7a5d944b
TT
2729 data->section_maps[sec->the_bfd_section->index].push_back
2730 (new_exidx_entry);
0e9e9abd 2731 }
0e9e9abd
UW
2732}
2733
2734/* Search for the exception table entry covering MEMADDR. If one is found,
2735 return a pointer to its data. Otherwise, return 0. If START is non-NULL,
2736 set *START to the start of the region covered by this entry. */
2737
2738static gdb_byte *
2739arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
2740{
2741 struct obj_section *sec;
2742
2743 sec = find_pc_section (memaddr);
2744 if (sec != NULL)
2745 {
2746 struct arm_exidx_data *data;
0c1bcd23 2747 struct arm_exidx_entry map_key = { memaddr - sec->addr (), 0 };
0e9e9abd 2748
98badbfd 2749 data = arm_exidx_data_key.get (sec->objfile->obfd.get ());
0e9e9abd
UW
2750 if (data != NULL)
2751 {
7a5d944b
TT
2752 std::vector<arm_exidx_entry> &map
2753 = data->section_maps[sec->the_bfd_section->index];
2754 if (!map.empty ())
0e9e9abd 2755 {
7a5d944b 2756 auto idx = std::lower_bound (map.begin (), map.end (), map_key);
0e9e9abd 2757
7a5d944b 2758 /* std::lower_bound finds the earliest ordered insertion
0e9e9abd
UW
2759 point. If the following symbol starts at this exact
2760 address, we use that; otherwise, the preceding
2761 exception table entry covers this address. */
7a5d944b 2762 if (idx < map.end ())
0e9e9abd 2763 {
7a5d944b 2764 if (idx->addr == map_key.addr)
0e9e9abd
UW
2765 {
2766 if (start)
0c1bcd23 2767 *start = idx->addr + sec->addr ();
7a5d944b 2768 return idx->entry;
0e9e9abd
UW
2769 }
2770 }
2771
7a5d944b 2772 if (idx > map.begin ())
0e9e9abd 2773 {
7a5d944b 2774 idx = idx - 1;
0e9e9abd 2775 if (start)
0c1bcd23 2776 *start = idx->addr + sec->addr ();
7a5d944b 2777 return idx->entry;
0e9e9abd
UW
2778 }
2779 }
2780 }
2781 }
2782
2783 return NULL;
2784}
2785
2786/* Given the current frame THIS_FRAME, and its associated frame unwinding
2787 instruction list from the ARM exception table entry ENTRY, allocate and
2788 return a prologue cache structure describing how to unwind this frame.
2789
2790 Return NULL if the unwinding instruction list contains a "spare",
2791 "reserved" or "refuse to unwind" instruction as defined in section
2792 "9.3 Frame unwinding instructions" of the "Exception Handling ABI
2793 for the ARM Architecture" document. */
2794
2795static struct arm_prologue_cache *
8480a37e 2796arm_exidx_fill_cache (const frame_info_ptr &this_frame, gdb_byte *entry)
0e9e9abd
UW
2797{
2798 CORE_ADDR vsp = 0;
2799 int vsp_valid = 0;
2800
2801 struct arm_prologue_cache *cache;
2802 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 2803 arm_cache_init (cache, this_frame);
0e9e9abd
UW
2804
2805 for (;;)
2806 {
2807 gdb_byte insn;
2808
2809 /* Whenever we reload SP, we actually have to retrieve its
2810 actual value in the current frame. */
2811 if (!vsp_valid)
2812 {
a9a87d35 2813 if (cache->saved_regs[ARM_SP_REGNUM].is_realreg ())
0e9e9abd 2814 {
098caef4 2815 int reg = cache->saved_regs[ARM_SP_REGNUM].realreg ();
0e9e9abd
UW
2816 vsp = get_frame_register_unsigned (this_frame, reg);
2817 }
2818 else
2819 {
098caef4 2820 CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr ();
0e9e9abd
UW
2821 vsp = get_frame_memory_unsigned (this_frame, addr, 4);
2822 }
2823
2824 vsp_valid = 1;
2825 }
2826
2827 /* Decode next unwind instruction. */
2828 insn = *entry++;
2829
2830 if ((insn & 0xc0) == 0)
2831 {
2832 int offset = insn & 0x3f;
2833 vsp += (offset << 2) + 4;
2834 }
2835 else if ((insn & 0xc0) == 0x40)
2836 {
2837 int offset = insn & 0x3f;
2838 vsp -= (offset << 2) + 4;
2839 }
2840 else if ((insn & 0xf0) == 0x80)
2841 {
2842 int mask = ((insn & 0xf) << 8) | *entry++;
2843 int i;
2844
2845 /* The special case of an all-zero mask identifies
2846 "Refuse to unwind". We return NULL to fall back
2847 to the prologue analyzer. */
2848 if (mask == 0)
2849 return NULL;
2850
2851 /* Pop registers r4..r15 under mask. */
2852 for (i = 0; i < 12; i++)
2853 if (mask & (1 << i))
2854 {
098caef4 2855 cache->saved_regs[4 + i].set_addr (vsp);
0e9e9abd
UW
2856 vsp += 4;
2857 }
2858
2859 /* Special-case popping SP -- we need to reload vsp. */
2860 if (mask & (1 << (ARM_SP_REGNUM - 4)))
2861 vsp_valid = 0;
2862 }
2863 else if ((insn & 0xf0) == 0x90)
2864 {
2865 int reg = insn & 0xf;
2866
2867 /* Reserved cases. */
2868 if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
2869 return NULL;
2870
2871 /* Set SP from another register and mark VSP for reload. */
2872 cache->saved_regs[ARM_SP_REGNUM] = cache->saved_regs[reg];
2873 vsp_valid = 0;
2874 }
2875 else if ((insn & 0xf0) == 0xa0)
2876 {
2877 int count = insn & 0x7;
2878 int pop_lr = (insn & 0x8) != 0;
2879 int i;
2880
2881 /* Pop r4..r[4+count]. */
2882 for (i = 0; i <= count; i++)
2883 {
098caef4 2884 cache->saved_regs[4 + i].set_addr (vsp);
0e9e9abd
UW
2885 vsp += 4;
2886 }
2887
2888 /* If indicated by flag, pop LR as well. */
2889 if (pop_lr)
2890 {
098caef4 2891 cache->saved_regs[ARM_LR_REGNUM].set_addr (vsp);
0e9e9abd
UW
2892 vsp += 4;
2893 }
2894 }
2895 else if (insn == 0xb0)
2896 {
2897 /* We could only have updated PC by popping into it; if so, it
2898 will show up as address. Otherwise, copy LR into PC. */
a9a87d35 2899 if (!cache->saved_regs[ARM_PC_REGNUM].is_addr ())
0e9e9abd
UW
2900 cache->saved_regs[ARM_PC_REGNUM]
2901 = cache->saved_regs[ARM_LR_REGNUM];
2902
2903 /* We're done. */
2904 break;
2905 }
2906 else if (insn == 0xb1)
2907 {
2908 int mask = *entry++;
2909 int i;
2910
2911 /* All-zero mask and mask >= 16 is "spare". */
2912 if (mask == 0 || mask >= 16)
2913 return NULL;
2914
2915 /* Pop r0..r3 under mask. */
2916 for (i = 0; i < 4; i++)
2917 if (mask & (1 << i))
2918 {
098caef4 2919 cache->saved_regs[i].set_addr (vsp);
0e9e9abd
UW
2920 vsp += 4;
2921 }
2922 }
2923 else if (insn == 0xb2)
2924 {
2925 ULONGEST offset = 0;
2926 unsigned shift = 0;
2927
2928 do
2929 {
2930 offset |= (*entry & 0x7f) << shift;
2931 shift += 7;
2932 }
2933 while (*entry++ & 0x80);
2934
2935 vsp += 0x204 + (offset << 2);
2936 }
2937 else if (insn == 0xb3)
2938 {
2939 int start = *entry >> 4;
2940 int count = (*entry++) & 0xf;
2941 int i;
2942
2943 /* Only registers D0..D15 are valid here. */
2944 if (start + count >= 16)
2945 return NULL;
2946
2947 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2948 for (i = 0; i <= count; i++)
2949 {
098caef4 2950 cache->saved_regs[ARM_D0_REGNUM + start + i].set_addr (vsp);
0e9e9abd
UW
2951 vsp += 8;
2952 }
2953
2954 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2955 vsp += 4;
2956 }
2957 else if ((insn & 0xf8) == 0xb8)
2958 {
2959 int count = insn & 0x7;
2960 int i;
2961
2962 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2963 for (i = 0; i <= count; i++)
2964 {
098caef4 2965 cache->saved_regs[ARM_D0_REGNUM + 8 + i].set_addr (vsp);
0e9e9abd
UW
2966 vsp += 8;
2967 }
2968
2969 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2970 vsp += 4;
2971 }
2972 else if (insn == 0xc6)
2973 {
2974 int start = *entry >> 4;
2975 int count = (*entry++) & 0xf;
2976 int i;
2977
2978 /* Only registers WR0..WR15 are valid. */
2979 if (start + count >= 16)
2980 return NULL;
2981
2982 /* Pop iwmmx registers WR[start]..WR[start+count]. */
2983 for (i = 0; i <= count; i++)
2984 {
098caef4 2985 cache->saved_regs[ARM_WR0_REGNUM + start + i].set_addr (vsp);
0e9e9abd
UW
2986 vsp += 8;
2987 }
2988 }
2989 else if (insn == 0xc7)
2990 {
2991 int mask = *entry++;
2992 int i;
2993
2994 /* All-zero mask and mask >= 16 is "spare". */
2995 if (mask == 0 || mask >= 16)
2996 return NULL;
2997
2998 /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask. */
2999 for (i = 0; i < 4; i++)
3000 if (mask & (1 << i))
3001 {
098caef4 3002 cache->saved_regs[ARM_WCGR0_REGNUM + i].set_addr (vsp);
0e9e9abd
UW
3003 vsp += 4;
3004 }
3005 }
3006 else if ((insn & 0xf8) == 0xc0)
3007 {
3008 int count = insn & 0x7;
3009 int i;
3010
3011 /* Pop iwmmx registers WR[10]..WR[10+count]. */
3012 for (i = 0; i <= count; i++)
3013 {
098caef4 3014 cache->saved_regs[ARM_WR0_REGNUM + 10 + i].set_addr (vsp);
0e9e9abd
UW
3015 vsp += 8;
3016 }
3017 }
3018 else if (insn == 0xc8)
3019 {
3020 int start = *entry >> 4;
3021 int count = (*entry++) & 0xf;
3022 int i;
3023
3024 /* Only registers D0..D31 are valid. */
3025 if (start + count >= 16)
3026 return NULL;
3027
3028 /* Pop VFP double-precision registers
3029 D[16+start]..D[16+start+count]. */
3030 for (i = 0; i <= count; i++)
3031 {
098caef4 3032 cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].set_addr (vsp);
0e9e9abd
UW
3033 vsp += 8;
3034 }
3035 }
3036 else if (insn == 0xc9)
3037 {
3038 int start = *entry >> 4;
3039 int count = (*entry++) & 0xf;
3040 int i;
3041
3042 /* Pop VFP double-precision registers D[start]..D[start+count]. */
3043 for (i = 0; i <= count; i++)
3044 {
098caef4 3045 cache->saved_regs[ARM_D0_REGNUM + start + i].set_addr (vsp);
0e9e9abd
UW
3046 vsp += 8;
3047 }
3048 }
3049 else if ((insn & 0xf8) == 0xd0)
3050 {
3051 int count = insn & 0x7;
3052 int i;
3053
3054 /* Pop VFP double-precision registers D[8]..D[8+count]. */
3055 for (i = 0; i <= count; i++)
3056 {
098caef4 3057 cache->saved_regs[ARM_D0_REGNUM + 8 + i].set_addr (vsp);
0e9e9abd
UW
3058 vsp += 8;
3059 }
3060 }
3061 else
3062 {
3063 /* Everything else is "spare". */
3064 return NULL;
3065 }
3066 }
3067
3068 /* If we restore SP from a register, assume this was the frame register.
3069 Otherwise just fall back to SP as frame register. */
a9a87d35 3070 if (cache->saved_regs[ARM_SP_REGNUM].is_realreg ())
098caef4 3071 cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg ();
0e9e9abd
UW
3072 else
3073 cache->framereg = ARM_SP_REGNUM;
3074
3075 /* Determine offset to previous frame. */
3076 cache->framesize
3077 = vsp - get_frame_register_unsigned (this_frame, cache->framereg);
3078
3079 /* We already got the previous SP. */
ae7e2f45 3080 arm_gdbarch_tdep *tdep
08106042 3081 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45 3082 arm_cache_set_active_sp_value (cache, tdep, vsp);
0e9e9abd
UW
3083
3084 return cache;
3085}
3086
3087/* Unwinding via ARM exception table entries. Note that the sniffer
3088 already computes a filled-in prologue cache, which is then used
3089 with the same arm_prologue_this_id and arm_prologue_prev_register
3090 routines also used for prologue-parsing based unwinding. */
3091
3092static int
3093arm_exidx_unwind_sniffer (const struct frame_unwind *self,
8480a37e 3094 const frame_info_ptr &this_frame,
0e9e9abd
UW
3095 void **this_prologue_cache)
3096{
3097 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3098 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3099 CORE_ADDR addr_in_block, exidx_region, func_start;
3100 struct arm_prologue_cache *cache;
3101 gdb_byte *entry;
3102
3103 /* See if we have an ARM exception table entry covering this address. */
3104 addr_in_block = get_frame_address_in_block (this_frame);
3105 entry = arm_find_exidx_entry (addr_in_block, &exidx_region);
3106 if (!entry)
3107 return 0;
3108
3109 /* The ARM exception table does not describe unwind information
3110 for arbitrary PC values, but is guaranteed to be correct only
3111 at call sites. We have to decide here whether we want to use
3112 ARM exception table information for this frame, or fall back
3113 to using prologue parsing. (Note that if we have DWARF CFI,
3114 this sniffer isn't even called -- CFI is always preferred.)
3115
3116 Before we make this decision, however, we check whether we
3117 actually have *symbol* information for the current frame.
3118 If not, prologue parsing would not work anyway, so we might
3119 as well use the exception table and hope for the best. */
3120 if (find_pc_partial_function (addr_in_block, NULL, &func_start, NULL))
3121 {
3122 int exc_valid = 0;
3123
3124 /* If the next frame is "normal", we are at a call site in this
3125 frame, so exception information is guaranteed to be valid. */
3126 if (get_next_frame (this_frame)
3127 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
3128 exc_valid = 1;
3129
3026cdbd
JK
3130 /* Some syscalls keep PC pointing to the SVC instruction itself. */
3131 for (int shift = 0; shift <= 1 && !exc_valid; ++shift)
0e9e9abd 3132 {
3026cdbd
JK
3133 /* We also assume exception information is valid if we're currently
3134 blocked in a system call. The system library is supposed to
3135 ensure this, so that e.g. pthread cancellation works. */
3136 if (arm_frame_is_thumb (this_frame))
3137 {
3138 ULONGEST insn;
3139
3140 if (safe_read_memory_unsigned_integer ((get_frame_pc (this_frame)
3141 - (shift ? 2 : 0)),
3142 2, byte_order_for_code,
3143 &insn)
3144 && (insn & 0xff00) == 0xdf00 /* svc */)
3145 exc_valid = 1;
3146 }
3147 else
3148 {
3149 ULONGEST insn;
3150
3151 if (safe_read_memory_unsigned_integer ((get_frame_pc (this_frame)
3152 - (shift ? 4 : 0)),
3153 4, byte_order_for_code,
3154 &insn)
3155 && (insn & 0x0f000000) == 0x0f000000 /* svc */)
3156 exc_valid = 1;
3157 }
d9311bfa
AT
3158 }
3159
0e9e9abd
UW
3160 /* Bail out if we don't know that exception information is valid. */
3161 if (!exc_valid)
3162 return 0;
3163
3164 /* The ARM exception index does not mark the *end* of the region
3165 covered by the entry, and some functions will not have any entry.
3166 To correctly recognize the end of the covered region, the linker
3167 should have inserted dummy records with a CANTUNWIND marker.
3168
3169 Unfortunately, current versions of GNU ld do not reliably do
3170 this, and thus we may have found an incorrect entry above.
3171 As a (temporary) sanity check, we only use the entry if it
3172 lies *within* the bounds of the function. Note that this check
3173 might reject perfectly valid entries that just happen to cover
3174 multiple functions; therefore this check ought to be removed
3175 once the linker is fixed. */
3176 if (func_start > exidx_region)
3177 return 0;
3178 }
3179
3180 /* Decode the list of unwinding instructions into a prologue cache.
3181 Note that this may fail due to e.g. a "refuse to unwind" code. */
3182 cache = arm_exidx_fill_cache (this_frame, entry);
3183 if (!cache)
3184 return 0;
3185
3186 *this_prologue_cache = cache;
3187 return 1;
3188}
3189
3190struct frame_unwind arm_exidx_unwind = {
a154d838 3191 "arm exidx",
0e9e9abd 3192 NORMAL_FRAME,
8fbca658 3193 default_frame_unwind_stop_reason,
0e9e9abd
UW
3194 arm_prologue_this_id,
3195 arm_prologue_prev_register,
3196 NULL,
3197 arm_exidx_unwind_sniffer
3198};
3199
779aa56f 3200static struct arm_prologue_cache *
8480a37e 3201arm_make_epilogue_frame_cache (const frame_info_ptr &this_frame)
779aa56f
YQ
3202{
3203 struct arm_prologue_cache *cache;
779aa56f
YQ
3204 int reg;
3205
3206 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 3207 arm_cache_init (cache, this_frame);
779aa56f
YQ
3208
3209 /* Still rely on the offset calculated from prologue. */
3210 arm_scan_prologue (this_frame, cache);
3211
3212 /* Since we are in epilogue, the SP has been restored. */
ae7e2f45 3213 arm_gdbarch_tdep *tdep
08106042 3214 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45
CL
3215 arm_cache_set_active_sp_value (cache, tdep,
3216 get_frame_register_unsigned (this_frame,
3217 ARM_SP_REGNUM));
779aa56f
YQ
3218
3219 /* Calculate actual addresses of saved registers using offsets
3220 determined by arm_scan_prologue. */
3221 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
a9a87d35 3222 if (cache->saved_regs[reg].is_addr ())
098caef4 3223 cache->saved_regs[reg].set_addr (cache->saved_regs[reg].addr ()
ae7e2f45 3224 + arm_cache_get_prev_sp_value (cache, tdep));
779aa56f
YQ
3225
3226 return cache;
3227}
3228
3229/* Implementation of function hook 'this_id' in
3230 'struct frame_uwnind' for epilogue unwinder. */
3231
3232static void
8480a37e 3233arm_epilogue_frame_this_id (const frame_info_ptr &this_frame,
779aa56f
YQ
3234 void **this_cache,
3235 struct frame_id *this_id)
3236{
3237 struct arm_prologue_cache *cache;
3238 CORE_ADDR pc, func;
3239
3240 if (*this_cache == NULL)
3241 *this_cache = arm_make_epilogue_frame_cache (this_frame);
3242 cache = (struct arm_prologue_cache *) *this_cache;
3243
3244 /* Use function start address as part of the frame ID. If we cannot
3245 identify the start address (due to missing symbol information),
3246 fall back to just using the current PC. */
3247 pc = get_frame_pc (this_frame);
3248 func = get_frame_func (this_frame);
fb3f3d25 3249 if (func == 0)
779aa56f
YQ
3250 func = pc;
3251
ae7e2f45 3252 arm_gdbarch_tdep *tdep
08106042 3253 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
44acb017 3254 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep), func);
779aa56f
YQ
3255}
3256
3257/* Implementation of function hook 'prev_register' in
3258 'struct frame_uwnind' for epilogue unwinder. */
3259
3260static struct value *
8480a37e 3261arm_epilogue_frame_prev_register (const frame_info_ptr &this_frame,
779aa56f
YQ
3262 void **this_cache, int regnum)
3263{
779aa56f
YQ
3264 if (*this_cache == NULL)
3265 *this_cache = arm_make_epilogue_frame_cache (this_frame);
779aa56f
YQ
3266
3267 return arm_prologue_prev_register (this_frame, this_cache, regnum);
3268}
3269
3270static int arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch,
3271 CORE_ADDR pc);
3272static int thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch,
3273 CORE_ADDR pc);
3274
3275/* Implementation of function hook 'sniffer' in
3276 'struct frame_uwnind' for epilogue unwinder. */
3277
3278static int
3279arm_epilogue_frame_sniffer (const struct frame_unwind *self,
8480a37e 3280 const frame_info_ptr &this_frame,
779aa56f
YQ
3281 void **this_prologue_cache)
3282{
3283 if (frame_relative_level (this_frame) == 0)
3284 {
3285 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3286 CORE_ADDR pc = get_frame_pc (this_frame);
3287
3288 if (arm_frame_is_thumb (this_frame))
3289 return thumb_stack_frame_destroyed_p (gdbarch, pc);
3290 else
3291 return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
3292 }
3293 else
3294 return 0;
3295}
3296
3297/* Frame unwinder from epilogue. */
3298
3299static const struct frame_unwind arm_epilogue_frame_unwind =
3300{
a154d838 3301 "arm epilogue",
779aa56f
YQ
3302 NORMAL_FRAME,
3303 default_frame_unwind_stop_reason,
3304 arm_epilogue_frame_this_id,
3305 arm_epilogue_frame_prev_register,
3306 NULL,
3307 arm_epilogue_frame_sniffer,
3308};
3309
80d8d390
YQ
3310/* Recognize GCC's trampoline for thumb call-indirect. If we are in a
3311 trampoline, return the target PC. Otherwise return 0.
3312
3313 void call0a (char c, short s, int i, long l) {}
3314
3315 int main (void)
3316 {
3317 (*pointer_to_call0a) (c, s, i, l);
3318 }
3319
3320 Instead of calling a stub library function _call_via_xx (xx is
3321 the register name), GCC may inline the trampoline in the object
3322 file as below (register r2 has the address of call0a).
3323
3324 .global main
3325 .type main, %function
3326 ...
3327 bl .L1
3328 ...
3329 .size main, .-main
3330
3331 .L1:
3332 bx r2
3333
3334 The trampoline 'bx r2' doesn't belong to main. */
3335
3336static CORE_ADDR
8480a37e 3337arm_skip_bx_reg (const frame_info_ptr &frame, CORE_ADDR pc)
80d8d390
YQ
3338{
3339 /* The heuristics of recognizing such trampoline is that FRAME is
3340 executing in Thumb mode and the instruction on PC is 'bx Rm'. */
3341 if (arm_frame_is_thumb (frame))
3342 {
3343 gdb_byte buf[2];
3344
3345 if (target_read_memory (pc, buf, 2) == 0)
3346 {
3347 struct gdbarch *gdbarch = get_frame_arch (frame);
3348 enum bfd_endian byte_order_for_code
3349 = gdbarch_byte_order_for_code (gdbarch);
3350 uint16_t insn
3351 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3352
3353 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
3354 {
3355 CORE_ADDR dest
3356 = get_frame_register_unsigned (frame, bits (insn, 3, 6));
3357
3358 /* Clear the LSB so that gdb core sets step-resume
3359 breakpoint at the right address. */
3360 return UNMAKE_THUMB_ADDR (dest);
3361 }
3362 }
3363 }
3364
3365 return 0;
3366}
3367
909cf6ea 3368static struct arm_prologue_cache *
8480a37e 3369arm_make_stub_cache (const frame_info_ptr &this_frame)
909cf6ea 3370{
909cf6ea 3371 struct arm_prologue_cache *cache;
909cf6ea 3372
35d5d4ee 3373 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 3374 arm_cache_init (cache, this_frame);
909cf6ea 3375
ae7e2f45 3376 arm_gdbarch_tdep *tdep
08106042 3377 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45
CL
3378 arm_cache_set_active_sp_value (cache, tdep,
3379 get_frame_register_unsigned (this_frame,
3380 ARM_SP_REGNUM));
909cf6ea
DJ
3381
3382 return cache;
3383}
3384
3385/* Our frame ID for a stub frame is the current SP and LR. */
3386
3387static void
8480a37e 3388arm_stub_this_id (const frame_info_ptr &this_frame,
909cf6ea
DJ
3389 void **this_cache,
3390 struct frame_id *this_id)
3391{
3392 struct arm_prologue_cache *cache;
3393
3394 if (*this_cache == NULL)
a262aec2 3395 *this_cache = arm_make_stub_cache (this_frame);
9a3c8263 3396 cache = (struct arm_prologue_cache *) *this_cache;
909cf6ea 3397
ae7e2f45 3398 arm_gdbarch_tdep *tdep
08106042 3399 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45
CL
3400 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep),
3401 get_frame_pc (this_frame));
909cf6ea
DJ
3402}
3403
a262aec2
DJ
3404static int
3405arm_stub_unwind_sniffer (const struct frame_unwind *self,
8480a37e 3406 const frame_info_ptr &this_frame,
a262aec2 3407 void **this_prologue_cache)
909cf6ea 3408{
93d42b30 3409 CORE_ADDR addr_in_block;
948f8e3d 3410 gdb_byte dummy[4];
18d18ac8
YQ
3411 CORE_ADDR pc, start_addr;
3412 const char *name;
909cf6ea 3413
a262aec2 3414 addr_in_block = get_frame_address_in_block (this_frame);
18d18ac8 3415 pc = get_frame_pc (this_frame);
3e5d3a5a 3416 if (in_plt_section (addr_in_block)
fc36e839
DE
3417 /* We also use the stub winder if the target memory is unreadable
3418 to avoid having the prologue unwinder trying to read it. */
18d18ac8
YQ
3419 || target_read_memory (pc, dummy, 4) != 0)
3420 return 1;
3421
3422 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0
3423 && arm_skip_bx_reg (this_frame, pc) != 0)
a262aec2 3424 return 1;
909cf6ea 3425
a262aec2 3426 return 0;
909cf6ea
DJ
3427}
3428
a262aec2 3429struct frame_unwind arm_stub_unwind = {
a154d838 3430 "arm stub",
a262aec2 3431 NORMAL_FRAME,
8fbca658 3432 default_frame_unwind_stop_reason,
a262aec2
DJ
3433 arm_stub_this_id,
3434 arm_prologue_prev_register,
3435 NULL,
3436 arm_stub_unwind_sniffer
3437};
3438
2ae28aa9
YQ
3439/* Put here the code to store, into CACHE->saved_regs, the addresses
3440 of the saved registers of frame described by THIS_FRAME. CACHE is
3441 returned. */
3442
3443static struct arm_prologue_cache *
8480a37e 3444arm_m_exception_cache (const frame_info_ptr &this_frame)
2ae28aa9
YQ
3445{
3446 struct gdbarch *gdbarch = get_frame_arch (this_frame);
08106042 3447 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
2ae28aa9 3448 struct arm_prologue_cache *cache;
2ae28aa9
YQ
3449
3450 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 3451 arm_cache_init (cache, this_frame);
2ae28aa9 3452
55ea94da
FH
3453 /* ARMv7-M Architecture Reference "B1.5.6 Exception entry behavior"
3454 describes which bits in LR that define which stack was used prior
3455 to the exception and if FPU is used (causing extended stack frame). */
3456
8b73ee20
LM
3457 /* In the lockup state PC contains a lockup magic value.
3458 The PC value of the the next outer frame is irreversibly
3459 lost. The other registers are intact so LR likely contains
3460 PC of some frame next to the outer one, but we cannot analyze
3461 the next outer frame without knowing its PC
3462 therefore we do not know SP fixup for this frame.
3463 Some heuristics to resynchronize SP might be possible.
3464 For simplicity, just terminate the unwinding to prevent it going
3465 astray and attempting to read data/addresses it shouldn't,
3466 which may cause further issues due to side-effects. */
3467 CORE_ADDR pc = get_frame_pc (this_frame);
3468 if (arm_m_addr_is_lockup (pc))
3469 {
3470 /* The lockup can be real just in the innermost frame
3471 as the CPU is stopped and cannot create more frames.
3472 If we hit lockup magic PC in the other frame, it is
3473 just a sentinel at the top of stack: do not warn then. */
3474 if (frame_relative_level (this_frame) == 0)
3475 warning (_("ARM M in lockup state, stack unwinding terminated."));
3476
3477 /* Terminate any further stack unwinding. */
3478 arm_cache_set_active_sp_value (cache, tdep, 0);
3479 return cache;
3480 }
3481
df4860da 3482 CORE_ADDR lr = get_frame_register_unsigned (this_frame, ARM_LR_REGNUM);
ef273377 3483
148ca9dd
YR
3484 /* ARMv7-M Architecture Reference "A2.3.1 Arm core registers"
3485 states that LR is set to 0xffffffff on reset. ARMv8-M Architecture
3486 Reference "B3.3 Registers" states that LR is set to 0xffffffff on warm
3487 reset if Main Extension is implemented, otherwise the value is unknown. */
3488 if (lr == 0xffffffff)
3489 {
619cce4c
TS
3490 /* Terminate any further stack unwinding. */
3491 arm_cache_set_active_sp_value (cache, tdep, 0);
148ca9dd
YR
3492 return cache;
3493 }
3494
df4860da
TS
3495 /* Check FNC_RETURN indicator bits (24-31). */
3496 bool fnc_return = (((lr >> 24) & 0xff) == 0xfe);
3497 if (fnc_return)
ef273377 3498 {
df4860da
TS
3499 /* FNC_RETURN is only valid for targets with Security Extension. */
3500 if (!tdep->have_sec_ext)
3501 {
3502 error (_("While unwinding an exception frame, found unexpected Link "
3503 "Register value %s that requires the security extension, "
3504 "but the extension was not found or is disabled. This "
3505 "should not happen and may be caused by corrupt data or a "
3506 "bug in GDB."), phex (lr, ARM_INT_REGISTER_SIZE));
3507 }
3508
8c9ae6df
YR
3509 if (!arm_unwind_secure_frames)
3510 {
3511 warning (_("Non-secure to secure stack unwinding disabled."));
ef273377 3512
619cce4c
TS
3513 /* Terminate any further stack unwinding. */
3514 arm_cache_set_active_sp_value (cache, tdep, 0);
8c9ae6df
YR
3515 return cache;
3516 }
3517
df4860da 3518 ULONGEST xpsr = get_frame_register_unsigned (this_frame, ARM_PS_REGNUM);
b2e9e754 3519 if ((xpsr & 0x1ff) != 0)
8c9ae6df
YR
3520 /* Handler mode: This is the mode that exceptions are handled in. */
3521 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_msp_s_regnum);
ef273377 3522 else
8c9ae6df
YR
3523 /* Thread mode: This is the normal mode that programs run in. */
3524 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_psp_s_regnum);
ef273377 3525
df4860da 3526 CORE_ADDR unwound_sp = arm_cache_get_prev_sp_value (cache, tdep);
55ea94da 3527
8c9ae6df
YR
3528 /* Stack layout for a function call from Secure to Non-Secure state
3529 (ARMv8-M section B3.16):
55ea94da 3530
8c9ae6df
YR
3531 SP Offset
3532
3533 +-------------------+
3534 0x08 | |
3535 +-------------------+ <-- Original SP
3536 0x04 | Partial xPSR |
3537 +-------------------+
3538 0x00 | Return Address |
3539 +===================+ <-- New SP */
3540
3541 cache->saved_regs[ARM_PC_REGNUM].set_addr (unwound_sp + 0x00);
3542 cache->saved_regs[ARM_LR_REGNUM].set_addr (unwound_sp + 0x00);
3543 cache->saved_regs[ARM_PS_REGNUM].set_addr (unwound_sp + 0x04);
3544
3545 arm_cache_set_active_sp_value (cache, tdep, unwound_sp + 0x08);
ef273377
CL
3546
3547 return cache;
3548 }
3549
3550 /* Check EXC_RETURN indicator bits (24-31). */
df4860da 3551 bool exc_return = (((lr >> 24) & 0xff) == 0xff);
ef273377 3552 if (exc_return)
55ea94da 3553 {
df4860da
TS
3554 int sp_regnum;
3555 bool secure_stack_used = false;
3556 bool default_callee_register_stacking = false;
3557 bool exception_domain_is_secure = false;
3558 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3559
ef273377 3560 /* Check EXC_RETURN bit SPSEL if Main or Thread (process) stack used. */
df4860da 3561 bool process_stack_used = (bit (lr, 2) != 0);
ef273377
CL
3562
3563 if (tdep->have_sec_ext)
3564 {
df4860da
TS
3565 secure_stack_used = (bit (lr, 6) != 0);
3566 default_callee_register_stacking = (bit (lr, 5) != 0);
23295de1 3567 exception_domain_is_secure = (bit (lr, 0) != 0);
ef273377
CL
3568
3569 /* Unwinding from non-secure to secure can trip security
3570 measures. In order to avoid the debugger being
3571 intrusive, rely on the user to configure the requested
3572 mode. */
3573 if (secure_stack_used && !exception_domain_is_secure
3574 && !arm_unwind_secure_frames)
3575 {
3576 warning (_("Non-secure to secure stack unwinding disabled."));
3577
619cce4c
TS
3578 /* Terminate any further stack unwinding. */
3579 arm_cache_set_active_sp_value (cache, tdep, 0);
ef273377
CL
3580 return cache;
3581 }
3582
3583 if (process_stack_used)
3584 {
3585 if (secure_stack_used)
3586 /* Secure thread (process) stack used, use PSP_S as SP. */
df4860da 3587 sp_regnum = tdep->m_profile_psp_s_regnum;
ef273377
CL
3588 else
3589 /* Non-secure thread (process) stack used, use PSP_NS as SP. */
df4860da 3590 sp_regnum = tdep->m_profile_psp_ns_regnum;
ef273377
CL
3591 }
3592 else
3593 {
3594 if (secure_stack_used)
3595 /* Secure main stack used, use MSP_S as SP. */
df4860da 3596 sp_regnum = tdep->m_profile_msp_s_regnum;
ef273377
CL
3597 else
3598 /* Non-secure main stack used, use MSP_NS as SP. */
df4860da 3599 sp_regnum = tdep->m_profile_msp_ns_regnum;
ef273377
CL
3600 }
3601 }
3602 else
3603 {
3604 if (process_stack_used)
3605 /* Thread (process) stack used, use PSP as SP. */
df4860da 3606 sp_regnum = tdep->m_profile_psp_regnum;
ef273377
CL
3607 else
3608 /* Main stack used, use MSP as SP. */
df4860da
TS
3609 sp_regnum = tdep->m_profile_msp_regnum;
3610 }
3611
3612 /* Set the active SP regnum. */
3613 arm_cache_switch_prev_sp (cache, tdep, sp_regnum);
3614
3615 /* Fetch the SP to use for this frame. */
3616 CORE_ADDR unwound_sp = arm_cache_get_prev_sp_value (cache, tdep);
3617
3618 /* Exception entry context stacking are described in ARMv8-M (section
3619 B3.19) and ARMv7-M (sections B1.5.6 and B1.5.7) Architecture Reference
3620 Manuals.
3621
3622 The following figure shows the structure of the stack frame when
3623 Security and Floating-point extensions are present.
3624
3625 SP Offsets
3626 Without With
3627 Callee Regs Callee Regs
3628 (Secure -> Non-Secure)
3629 +-------------------+
3630 0xA8 | | 0xD0
3631 +===================+ --+ <-- Original SP
3632 0xA4 | S31 | 0xCC |
3633 +-------------------+ |
3634 ... | Additional FP context
3635 +-------------------+ |
3636 0x68 | S16 | 0x90 |
3637 +===================+ --+
3638 0x64 | Reserved | 0x8C |
3639 +-------------------+ |
3640 0x60 | FPSCR | 0x88 |
3641 +-------------------+ |
3642 0x5C | S15 | 0x84 | FP context
3643 +-------------------+ |
3644 ... |
3645 +-------------------+ |
3646 0x20 | S0 | 0x48 |
3647 +===================+ --+
3648 0x1C | xPSR | 0x44 |
3649 +-------------------+ |
3650 0x18 | Return address | 0x40 |
3651 +-------------------+ |
3652 0x14 | LR(R14) | 0x3C |
3653 +-------------------+ |
3654 0x10 | R12 | 0x38 | State context
3655 +-------------------+ |
3656 0x0C | R3 | 0x34 |
3657 +-------------------+ |
3658 ... |
3659 +-------------------+ |
3660 0x00 | R0 | 0x28 |
3661 +===================+ --+
3662 | R11 | 0x24 |
3663 +-------------------+ |
3664 ... |
3665 +-------------------+ | Additional state
3666 | R4 | 0x08 | context when
3667 +-------------------+ | transitioning from
3668 | Reserved | 0x04 | Secure to Non-Secure
3669 +-------------------+ |
3670 | Magic signature | 0x00 |
3671 +===================+ --+ <-- New SP */
3672
3673 uint32_t sp_r0_offset = 0;
3674
3675 /* With the Security extension, the hardware saves R4..R11 too. */
3676 if (tdep->have_sec_ext && secure_stack_used
23295de1 3677 && (!default_callee_register_stacking || !exception_domain_is_secure))
df4860da
TS
3678 {
3679 /* Read R4..R11 from the integer callee registers. */
3680 cache->saved_regs[4].set_addr (unwound_sp + 0x08);
3681 cache->saved_regs[5].set_addr (unwound_sp + 0x0C);
3682 cache->saved_regs[6].set_addr (unwound_sp + 0x10);
3683 cache->saved_regs[7].set_addr (unwound_sp + 0x14);
3684 cache->saved_regs[8].set_addr (unwound_sp + 0x18);
3685 cache->saved_regs[9].set_addr (unwound_sp + 0x1C);
3686 cache->saved_regs[10].set_addr (unwound_sp + 0x20);
3687 cache->saved_regs[11].set_addr (unwound_sp + 0x24);
3688 sp_r0_offset = 0x28;
3689 }
3690
3691 /* The hardware saves eight 32-bit words, comprising xPSR,
3692 ReturnAddress, LR (R14), R12, R3, R2, R1, R0. See details in
3693 "B1.5.6 Exception entry behavior" in
3694 "ARMv7-M Architecture Reference Manual". */
3695 cache->saved_regs[0].set_addr (unwound_sp + sp_r0_offset);
3696 cache->saved_regs[1].set_addr (unwound_sp + sp_r0_offset + 0x04);
3697 cache->saved_regs[2].set_addr (unwound_sp + sp_r0_offset + 0x08);
3698 cache->saved_regs[3].set_addr (unwound_sp + sp_r0_offset + 0x0C);
3699 cache->saved_regs[ARM_IP_REGNUM].set_addr (unwound_sp + sp_r0_offset
3700 + 0x10);
3701 cache->saved_regs[ARM_LR_REGNUM].set_addr (unwound_sp + sp_r0_offset
3702 + 0x14);
3703 cache->saved_regs[ARM_PC_REGNUM].set_addr (unwound_sp + sp_r0_offset
3704 + 0x18);
3705 cache->saved_regs[ARM_PS_REGNUM].set_addr (unwound_sp + sp_r0_offset
3706 + 0x1C);
3707
3708 /* Check EXC_RETURN bit FTYPE if extended stack frame (FPU regs stored)
3709 type used. */
3710 bool extended_frame_used = (bit (lr, 4) == 0);
3711 if (extended_frame_used)
3712 {
3713 ULONGEST fpccr;
60c90d8c 3714 ULONGEST fpcar;
df4860da
TS
3715
3716 /* Read FPCCR register. */
ce6c3d25
TS
3717 if (!safe_read_memory_unsigned_integer (FPCCR, ARM_INT_REGISTER_SIZE,
3718 byte_order, &fpccr))
3719 {
3720 warning (_("Could not fetch required FPCCR content. Further "
3721 "unwinding is impossible."));
3722 arm_cache_set_active_sp_value (cache, tdep, 0);
3723 return cache;
3724 }
df4860da 3725
60c90d8c
TS
3726 /* Read FPCAR register. */
3727 if (!safe_read_memory_unsigned_integer (FPCAR, ARM_INT_REGISTER_SIZE,
3728 byte_order, &fpcar))
3729 {
3730 warning (_("Could not fetch FPCAR content. Further unwinding of "
3731 "FP register values will be unreliable."));
3732 fpcar = 0;
3733 }
3734
3735 bool fpccr_aspen = bit (fpccr, 31);
3736 bool fpccr_lspen = bit (fpccr, 30);
3737 bool fpccr_ts = bit (fpccr, 26);
3738 bool fpccr_lspact = bit (fpccr, 0);
3739
3740 /* The LSPEN and ASPEN bits indicate if the lazy state preservation
3741 for FP registers is enabled or disabled. The LSPACT bit indicate,
3742 together with FPCAR, if the lazy state preservation feature is
3743 active for the current frame or for another frame.
3744 See "Lazy context save of FP state", in B1.5.7, also ARM AN298,
3745 supported by Cortex-M4F architecture for details. */
3746 bool fpcar_points_to_this_frame = ((unwound_sp + sp_r0_offset + 0x20)
3747 == (fpcar & ~0x7));
3748 bool read_fp_regs_from_stack = (!(fpccr_aspen && fpccr_lspen
3749 && fpccr_lspact
3750 && fpcar_points_to_this_frame));
df4860da
TS
3751
3752 /* Extended stack frame type used. */
60c90d8c 3753 if (read_fp_regs_from_stack)
df4860da 3754 {
60c90d8c
TS
3755 CORE_ADDR addr = unwound_sp + sp_r0_offset + 0x20;
3756 for (int i = 0; i < 8; i++)
3757 {
3758 cache->saved_regs[ARM_D0_REGNUM + i].set_addr (addr);
3759 addr += 8;
3760 }
df4860da
TS
3761 }
3762 cache->saved_regs[ARM_FPSCR_REGNUM].set_addr (unwound_sp
3763 + sp_r0_offset + 0x60);
ef273377 3764
df4860da
TS
3765 if (tdep->have_sec_ext && !default_callee_register_stacking
3766 && fpccr_ts)
3767 {
3768 /* Handle floating-point callee saved registers. */
60c90d8c 3769 if (read_fp_regs_from_stack)
df4860da 3770 {
60c90d8c
TS
3771 CORE_ADDR addr = unwound_sp + sp_r0_offset + 0x68;
3772 for (int i = 8; i < 16; i++)
3773 {
3774 cache->saved_regs[ARM_D0_REGNUM + i].set_addr (addr);
3775 addr += 8;
3776 }
df4860da 3777 }
2ae28aa9 3778
df4860da
TS
3779 arm_cache_set_active_sp_value (cache, tdep,
3780 unwound_sp + sp_r0_offset + 0xA8);
3781 }
3782 else
ef273377 3783 {
df4860da
TS
3784 /* Offset 0x64 is reserved. */
3785 arm_cache_set_active_sp_value (cache, tdep,
3786 unwound_sp + sp_r0_offset + 0x68);
ef273377 3787 }
ef273377
CL
3788 }
3789 else
3790 {
df4860da 3791 /* Standard stack frame type used. */
2d9cf99d 3792 arm_cache_set_active_sp_value (cache, tdep,
df4860da 3793 unwound_sp + sp_r0_offset + 0x20);
ef273377 3794 }
55ea94da 3795
df4860da
TS
3796 /* If bit 9 of the saved xPSR is set, then there is a four-byte
3797 aligner between the top of the 32-byte stack frame and the
3798 previous context's stack pointer. */
3799 ULONGEST xpsr;
ce6c3d25
TS
3800 if (!safe_read_memory_unsigned_integer (cache->saved_regs[ARM_PS_REGNUM]
3801 .addr (), ARM_INT_REGISTER_SIZE,
3802 byte_order, &xpsr))
3803 {
3804 warning (_("Could not fetch required XPSR content. Further "
3805 "unwinding is impossible."));
3806 arm_cache_set_active_sp_value (cache, tdep, 0);
3807 return cache;
3808 }
3809
df4860da
TS
3810 if (bit (xpsr, 9) != 0)
3811 {
3812 CORE_ADDR new_sp = arm_cache_get_prev_sp_value (cache, tdep) + 4;
3813 arm_cache_set_active_sp_value (cache, tdep, new_sp);
3814 }
2ae28aa9 3815
df4860da
TS
3816 return cache;
3817 }
3818
f34652de
PA
3819 internal_error (_("While unwinding an exception frame, "
3820 "found unexpected Link Register value "
3821 "%s. This should not happen and may "
3822 "be caused by corrupt data or a bug in"
3823 " GDB."),
df4860da 3824 phex (lr, ARM_INT_REGISTER_SIZE));
2ae28aa9
YQ
3825}
3826
ce6c3d25
TS
3827/* Implementation of the stop_reason hook for arm_m_exception frames. */
3828
3829static enum unwind_stop_reason
8480a37e 3830arm_m_exception_frame_unwind_stop_reason (const frame_info_ptr &this_frame,
ce6c3d25
TS
3831 void **this_cache)
3832{
3833 struct arm_prologue_cache *cache;
3834 arm_gdbarch_tdep *tdep
3835 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
3836
3837 if (*this_cache == NULL)
3838 *this_cache = arm_m_exception_cache (this_frame);
3839 cache = (struct arm_prologue_cache *) *this_cache;
3840
3841 /* If we've hit a wall, stop. */
3842 if (arm_cache_get_prev_sp_value (cache, tdep) == 0)
3843 return UNWIND_OUTERMOST;
3844
3845 return UNWIND_NO_REASON;
3846}
3847
2ae28aa9
YQ
3848/* Implementation of function hook 'this_id' in
3849 'struct frame_uwnind'. */
3850
3851static void
8480a37e 3852arm_m_exception_this_id (const frame_info_ptr &this_frame,
2ae28aa9
YQ
3853 void **this_cache,
3854 struct frame_id *this_id)
3855{
3856 struct arm_prologue_cache *cache;
3857
3858 if (*this_cache == NULL)
3859 *this_cache = arm_m_exception_cache (this_frame);
9a3c8263 3860 cache = (struct arm_prologue_cache *) *this_cache;
2ae28aa9
YQ
3861
3862 /* Our frame ID for a stub frame is the current SP and LR. */
ae7e2f45 3863 arm_gdbarch_tdep *tdep
08106042 3864 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45 3865 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep),
2ae28aa9
YQ
3866 get_frame_pc (this_frame));
3867}
3868
3869/* Implementation of function hook 'prev_register' in
3870 'struct frame_uwnind'. */
3871
3872static struct value *
8480a37e 3873arm_m_exception_prev_register (const frame_info_ptr &this_frame,
2ae28aa9
YQ
3874 void **this_cache,
3875 int prev_regnum)
3876{
2ae28aa9 3877 struct arm_prologue_cache *cache;
ef273377 3878 CORE_ADDR sp_value;
2ae28aa9
YQ
3879
3880 if (*this_cache == NULL)
3881 *this_cache = arm_m_exception_cache (this_frame);
9a3c8263 3882 cache = (struct arm_prologue_cache *) *this_cache;
2ae28aa9
YQ
3883
3884 /* The value was already reconstructed into PREV_SP. */
ae7e2f45 3885 arm_gdbarch_tdep *tdep
08106042 3886 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
2ae28aa9
YQ
3887 if (prev_regnum == ARM_SP_REGNUM)
3888 return frame_unwind_got_constant (this_frame, prev_regnum,
ae7e2f45 3889 arm_cache_get_prev_sp_value (cache, tdep));
2ae28aa9 3890
8c9ae6df
YR
3891 /* If we are asked to unwind the PC, strip the saved T bit. */
3892 if (prev_regnum == ARM_PC_REGNUM)
3893 {
3894 struct value *value = trad_frame_get_prev_register (this_frame,
3895 cache->saved_regs,
3896 prev_regnum);
3897 CORE_ADDR pc = value_as_address (value);
3898 return frame_unwind_got_constant (this_frame, prev_regnum,
287de656 3899 UNMAKE_THUMB_ADDR (pc));
8c9ae6df
YR
3900 }
3901
ef273377
CL
3902 /* The value might be one of the alternative SP, if so, use the
3903 value already constructed. */
d65edaa0 3904 if (arm_is_alternative_sp_register (tdep, prev_regnum))
ef273377
CL
3905 {
3906 sp_value = arm_cache_get_sp_register (cache, tdep, prev_regnum);
3907 return frame_unwind_got_constant (this_frame, prev_regnum, sp_value);
3908 }
3909
8c9ae6df
YR
3910 /* If we are asked to unwind the xPSR, set T bit if PC is in thumb mode.
3911 LR register is unreliable as it contains FNC_RETURN or EXC_RETURN
3912 pattern. */
3913 if (prev_regnum == ARM_PS_REGNUM)
3914 {
3915 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3916 struct value *value = trad_frame_get_prev_register (this_frame,
3917 cache->saved_regs,
3918 ARM_PC_REGNUM);
3919 CORE_ADDR pc = value_as_address (value);
3920 value = trad_frame_get_prev_register (this_frame, cache->saved_regs,
3921 ARM_PS_REGNUM);
3922 ULONGEST xpsr = value_as_long (value);
3923
3924 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
3925 xpsr = reconstruct_t_bit (gdbarch, pc, xpsr);
3926 return frame_unwind_got_constant (this_frame, ARM_PS_REGNUM, xpsr);
3927 }
3928
2ae28aa9
YQ
3929 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
3930 prev_regnum);
3931}
3932
3933/* Implementation of function hook 'sniffer' in
3934 'struct frame_uwnind'. */
3935
3936static int
3937arm_m_exception_unwind_sniffer (const struct frame_unwind *self,
8480a37e 3938 const frame_info_ptr &this_frame,
2ae28aa9
YQ
3939 void **this_prologue_cache)
3940{
ef273377 3941 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2ae28aa9
YQ
3942 CORE_ADDR this_pc = get_frame_pc (this_frame);
3943
3944 /* No need to check is_m; this sniffer is only registered for
3945 M-profile architectures. */
3946
ca90e760 3947 /* Check if exception frame returns to a magic PC value. */
ef273377 3948 return arm_m_addr_is_magic (gdbarch, this_pc);
2ae28aa9
YQ
3949}
3950
8b73ee20
LM
3951/* Frame unwinder for M-profile exceptions (EXC_RETURN on stack),
3952 lockup and secure/nonsecure interstate function calls (FNC_RETURN). */
2ae28aa9
YQ
3953
3954struct frame_unwind arm_m_exception_unwind =
3955{
8b73ee20 3956 "arm m exception lockup sec_fnc",
2ae28aa9 3957 SIGTRAMP_FRAME,
ce6c3d25 3958 arm_m_exception_frame_unwind_stop_reason,
2ae28aa9
YQ
3959 arm_m_exception_this_id,
3960 arm_m_exception_prev_register,
3961 NULL,
3962 arm_m_exception_unwind_sniffer
3963};
3964
24de872b 3965static CORE_ADDR
8480a37e 3966arm_normal_frame_base (const frame_info_ptr &this_frame, void **this_cache)
24de872b
DJ
3967{
3968 struct arm_prologue_cache *cache;
3969
eb5492fa 3970 if (*this_cache == NULL)
a262aec2 3971 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 3972 cache = (struct arm_prologue_cache *) *this_cache;
eb5492fa 3973
ae7e2f45 3974 arm_gdbarch_tdep *tdep
08106042 3975 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45 3976 return arm_cache_get_prev_sp_value (cache, tdep) - cache->framesize;
24de872b
DJ
3977}
3978
eb5492fa
DJ
3979struct frame_base arm_normal_base = {
3980 &arm_prologue_unwind,
3981 arm_normal_frame_base,
3982 arm_normal_frame_base,
3983 arm_normal_frame_base
3984};
3985
5cf11483
TS
3986struct arm_dwarf2_prev_register_cache
3987{
3bfdcabb 3988 /* Cached value of the corresponding stack pointer for the inner frame. */
5cf11483
TS
3989 CORE_ADDR sp;
3990 CORE_ADDR msp;
3991 CORE_ADDR msp_s;
3992 CORE_ADDR msp_ns;
3993 CORE_ADDR psp;
3994 CORE_ADDR psp_s;
3995 CORE_ADDR psp_ns;
3996};
3997
b39cc962 3998static struct value *
8480a37e 3999arm_dwarf2_prev_register (const frame_info_ptr &this_frame, void **this_cache,
b39cc962
DJ
4000 int regnum)
4001{
24568a2c 4002 struct gdbarch * gdbarch = get_frame_arch (this_frame);
08106042 4003 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
8c9ae6df
YR
4004 CORE_ADDR lr;
4005 ULONGEST cpsr;
5cf11483
TS
4006 arm_dwarf2_prev_register_cache *cache
4007 = ((arm_dwarf2_prev_register_cache *)
4008 dwarf2_frame_get_fn_data (this_frame, this_cache,
4009 arm_dwarf2_prev_register));
4010
4011 if (!cache)
4012 {
4013 const unsigned int size = sizeof (struct arm_dwarf2_prev_register_cache);
4014 cache = ((arm_dwarf2_prev_register_cache *)
4015 dwarf2_frame_allocate_fn_data (this_frame, this_cache,
4016 arm_dwarf2_prev_register, size));
4017
4018 if (tdep->have_sec_ext)
4019 {
4020 cache->sp
4021 = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
4022
4023 cache->msp_s
4024 = get_frame_register_unsigned (this_frame,
4025 tdep->m_profile_msp_s_regnum);
4026 cache->msp_ns
4027 = get_frame_register_unsigned (this_frame,
4028 tdep->m_profile_msp_ns_regnum);
4029 cache->psp_s
4030 = get_frame_register_unsigned (this_frame,
4031 tdep->m_profile_psp_s_regnum);
4032 cache->psp_ns
4033 = get_frame_register_unsigned (this_frame,
4034 tdep->m_profile_psp_ns_regnum);
4035 }
4036 else if (tdep->is_m)
4037 {
4038 cache->sp
4039 = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
4040
4041 cache->msp
4042 = get_frame_register_unsigned (this_frame,
4043 tdep->m_profile_msp_regnum);
4044 cache->psp
4045 = get_frame_register_unsigned (this_frame,
4046 tdep->m_profile_psp_regnum);
4047 }
4048 }
b39cc962 4049
42e11f36 4050 if (regnum == ARM_PC_REGNUM)
b39cc962 4051 {
b39cc962
DJ
4052 /* The PC is normally copied from the return column, which
4053 describes saves of LR. However, that version may have an
4054 extra bit set to indicate Thumb state. The bit is not
4055 part of the PC. */
a01567f4
LM
4056
4057 /* Record in the frame whether the return address was signed. */
4058 if (tdep->have_pacbti)
4059 {
4060 CORE_ADDR ra_auth_code
4061 = frame_unwind_register_unsigned (this_frame,
4062 tdep->pacbti_pseudo_base);
4063
4064 if (ra_auth_code != 0)
4065 set_frame_previous_pc_masked (this_frame);
4066 }
4067
b39cc962
DJ
4068 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
4069 return frame_unwind_got_constant (this_frame, regnum,
24568a2c 4070 arm_addr_bits_remove (gdbarch, lr));
42e11f36
TS
4071 }
4072 else if (regnum == ARM_PS_REGNUM)
4073 {
b39cc962 4074 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
ca38c58e 4075 cpsr = get_frame_register_unsigned (this_frame, regnum);
b39cc962 4076 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
8c9ae6df 4077 cpsr = reconstruct_t_bit (gdbarch, lr, cpsr);
ca38c58e 4078 return frame_unwind_got_constant (this_frame, regnum, cpsr);
b39cc962 4079 }
a6e4a48c
YR
4080 else if (arm_is_alternative_sp_register (tdep, regnum))
4081 {
4082 /* Handle the alternative SP registers on Cortex-M. */
4083 bool override_with_sp_value = false;
4084 CORE_ADDR val;
4085
4086 if (tdep->have_sec_ext)
4087 {
a6e4a48c 4088 bool is_msp = (regnum == tdep->m_profile_msp_regnum)
5cf11483 4089 && (cache->msp_s == cache->sp || cache->msp_ns == cache->sp);
a6e4a48c 4090 bool is_msp_s = (regnum == tdep->m_profile_msp_s_regnum)
5cf11483 4091 && (cache->msp_s == cache->sp);
a6e4a48c 4092 bool is_msp_ns = (regnum == tdep->m_profile_msp_ns_regnum)
5cf11483 4093 && (cache->msp_ns == cache->sp);
a6e4a48c 4094 bool is_psp = (regnum == tdep->m_profile_psp_regnum)
5cf11483 4095 && (cache->psp_s == cache->sp || cache->psp_ns == cache->sp);
a6e4a48c 4096 bool is_psp_s = (regnum == tdep->m_profile_psp_s_regnum)
5cf11483 4097 && (cache->psp_s == cache->sp);
a6e4a48c 4098 bool is_psp_ns = (regnum == tdep->m_profile_psp_ns_regnum)
5cf11483 4099 && (cache->psp_ns == cache->sp);
a6e4a48c
YR
4100
4101 override_with_sp_value = is_msp || is_msp_s || is_msp_ns
4102 || is_psp || is_psp_s || is_psp_ns;
4103
4104 }
4105 else if (tdep->is_m)
4106 {
5cf11483
TS
4107 bool is_msp = (regnum == tdep->m_profile_msp_regnum)
4108 && (cache->sp == cache->msp);
4109 bool is_psp = (regnum == tdep->m_profile_psp_regnum)
4110 && (cache->sp == cache->psp);
a6e4a48c
YR
4111
4112 override_with_sp_value = is_msp || is_psp;
4113 }
4114
4115 if (override_with_sp_value)
4116 {
4117 /* Use value of SP from previous frame. */
bd2b40ac 4118 frame_info_ptr prev_frame = get_prev_frame (this_frame);
a6e4a48c
YR
4119 if (prev_frame)
4120 val = get_frame_register_unsigned (prev_frame, ARM_SP_REGNUM);
4121 else
4122 val = get_frame_base (this_frame);
4123 }
4124 else
4125 /* Use value for the register from previous frame. */
4126 val = get_frame_register_unsigned (this_frame, regnum);
4127
4128 return frame_unwind_got_constant (this_frame, regnum, val);
4129 }
42e11f36 4130
f34652de 4131 internal_error (_("Unexpected register %d"), regnum);
b39cc962
DJ
4132}
4133
c9cf6e20 4134/* Implement the stack_frame_destroyed_p gdbarch method. */
4024ca99
UW
4135
4136static int
c9cf6e20 4137thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
4024ca99
UW
4138{
4139 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4140 unsigned int insn, insn2;
4141 int found_return = 0, found_stack_adjust = 0;
4142 CORE_ADDR func_start, func_end;
4143 CORE_ADDR scan_pc;
4144 gdb_byte buf[4];
4145
4146 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
4147 return 0;
4148
4149 /* The epilogue is a sequence of instructions along the following lines:
4150
4151 - add stack frame size to SP or FP
4152 - [if frame pointer used] restore SP from FP
4153 - restore registers from SP [may include PC]
4154 - a return-type instruction [if PC wasn't already restored]
4155
4156 In a first pass, we scan forward from the current PC and verify the
4157 instructions we find as compatible with this sequence, ending in a
4158 return instruction.
4159
4160 However, this is not sufficient to distinguish indirect function calls
4161 within a function from indirect tail calls in the epilogue in some cases.
4162 Therefore, if we didn't already find any SP-changing instruction during
4163 forward scan, we add a backward scanning heuristic to ensure we actually
4164 are in the epilogue. */
4165
4166 scan_pc = pc;
4167 while (scan_pc < func_end && !found_return)
4168 {
4169 if (target_read_memory (scan_pc, buf, 2))
4170 break;
4171
4172 scan_pc += 2;
4173 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
4174
4175 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
4176 found_return = 1;
4177 else if (insn == 0x46f7) /* mov pc, lr */
4178 found_return = 1;
540314bd 4179 else if (thumb_instruction_restores_sp (insn))
4024ca99 4180 {
b7576e5c 4181 if ((insn & 0xff00) == 0xbd00) /* pop <registers, PC> */
4024ca99
UW
4182 found_return = 1;
4183 }
db24da6d 4184 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instruction */
4024ca99
UW
4185 {
4186 if (target_read_memory (scan_pc, buf, 2))
4187 break;
4188
4189 scan_pc += 2;
4190 insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);
4191
4192 if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
4193 {
4024ca99
UW
4194 if (insn2 & 0x8000) /* <registers> include PC. */
4195 found_return = 1;
4196 }
4197 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
4198 && (insn2 & 0x0fff) == 0x0b04)
4199 {
4024ca99
UW
4200 if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC. */
4201 found_return = 1;
4202 }
4203 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
4204 && (insn2 & 0x0e00) == 0x0a00)
6b65d1b6 4205 ;
4024ca99
UW
4206 else
4207 break;
4208 }
4209 else
4210 break;
4211 }
4212
4213 if (!found_return)
4214 return 0;
4215
4216 /* Since any instruction in the epilogue sequence, with the possible
4217 exception of return itself, updates the stack pointer, we need to
4218 scan backwards for at most one instruction. Try either a 16-bit or
4219 a 32-bit instruction. This is just a heuristic, so we do not worry
0963b4bd 4220 too much about false positives. */
4024ca99 4221
6b65d1b6
YQ
4222 if (pc - 4 < func_start)
4223 return 0;
4224 if (target_read_memory (pc - 4, buf, 4))
4225 return 0;
4024ca99 4226
6b65d1b6
YQ
4227 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
4228 insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);
4229
4230 if (thumb_instruction_restores_sp (insn2))
4231 found_stack_adjust = 1;
4232 else if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
4233 found_stack_adjust = 1;
4234 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
4235 && (insn2 & 0x0fff) == 0x0b04)
4236 found_stack_adjust = 1;
4237 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
4238 && (insn2 & 0x0e00) == 0x0a00)
4239 found_stack_adjust = 1;
4024ca99
UW
4240
4241 return found_stack_adjust;
4242}
4243
4024ca99 4244static int
c58b006a 4245arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch, CORE_ADDR pc)
4024ca99
UW
4246{
4247 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4248 unsigned int insn;
f303bc3e 4249 int found_return;
4024ca99
UW
4250 CORE_ADDR func_start, func_end;
4251
4024ca99
UW
4252 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
4253 return 0;
4254
4255 /* We are in the epilogue if the previous instruction was a stack
4256 adjustment and the next instruction is a possible return (bx, mov
4257 pc, or pop). We could have to scan backwards to find the stack
4258 adjustment, or forwards to find the return, but this is a decent
4259 approximation. First scan forwards. */
4260
4261 found_return = 0;
4262 insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
4263 if (bits (insn, 28, 31) != INST_NV)
4264 {
4265 if ((insn & 0x0ffffff0) == 0x012fff10)
4266 /* BX. */
4267 found_return = 1;
4268 else if ((insn & 0x0ffffff0) == 0x01a0f000)
4269 /* MOV PC. */
4270 found_return = 1;
4271 else if ((insn & 0x0fff0000) == 0x08bd0000
4272 && (insn & 0x0000c000) != 0)
4273 /* POP (LDMIA), including PC or LR. */
4274 found_return = 1;
4275 }
4276
4277 if (!found_return)
4278 return 0;
4279
4280 /* Scan backwards. This is just a heuristic, so do not worry about
4281 false positives from mode changes. */
4282
4283 if (pc < func_start + 4)
4284 return 0;
4285
4286 insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
f303bc3e 4287 if (arm_instruction_restores_sp (insn))
4024ca99
UW
4288 return 1;
4289
4290 return 0;
4291}
4292
c58b006a
YQ
4293/* Implement the stack_frame_destroyed_p gdbarch method. */
4294
4295static int
4296arm_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
4297{
4298 if (arm_pc_is_thumb (gdbarch, pc))
4299 return thumb_stack_frame_destroyed_p (gdbarch, pc);
4300 else
4301 return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
4302}
4024ca99 4303
2dd604e7
RE
4304/* When arguments must be pushed onto the stack, they go on in reverse
4305 order. The code below implements a FILO (stack) to do this. */
4306
0fecb1a7 4307struct arm_stack_item
2dd604e7
RE
4308{
4309 int len;
0fecb1a7 4310 struct arm_stack_item *prev;
7c543f7b 4311 gdb_byte *data;
2dd604e7
RE
4312};
4313
0fecb1a7
TT
4314static struct arm_stack_item *
4315push_stack_item (struct arm_stack_item *prev, const gdb_byte *contents,
4316 int len)
2dd604e7 4317{
0fecb1a7
TT
4318 struct arm_stack_item *si;
4319 si = XNEW (struct arm_stack_item);
7c543f7b 4320 si->data = (gdb_byte *) xmalloc (len);
2dd604e7
RE
4321 si->len = len;
4322 si->prev = prev;
4323 memcpy (si->data, contents, len);
4324 return si;
4325}
4326
0fecb1a7
TT
4327static struct arm_stack_item *
4328pop_stack_item (struct arm_stack_item *si)
2dd604e7 4329{
0fecb1a7 4330 struct arm_stack_item *dead = si;
2dd604e7
RE
4331 si = si->prev;
4332 xfree (dead->data);
4333 xfree (dead);
4334 return si;
4335}
4336
030197b4
AB
4337/* Implement the gdbarch type alignment method, overrides the generic
4338 alignment algorithm for anything that is arm specific. */
2af48f68 4339
030197b4
AB
4340static ULONGEST
4341arm_type_align (gdbarch *gdbarch, struct type *t)
2af48f68 4342{
2af48f68 4343 t = check_typedef (t);
bd63c870 4344 if (t->code () == TYPE_CODE_ARRAY && t->is_vector ())
2af48f68 4345 {
030197b4
AB
4346 /* Use the natural alignment for vector types (the same for
4347 scalar type), but the maximum alignment is 64-bit. */
df86565b 4348 if (t->length () > 8)
030197b4 4349 return 8;
c4312b19 4350 else
df86565b 4351 return t->length ();
2af48f68 4352 }
030197b4
AB
4353
4354 /* Allow the common code to calculate the alignment. */
4355 return 0;
2af48f68
PB
4356}
4357
90445bd3
DJ
4358/* Possible base types for a candidate for passing and returning in
4359 VFP registers. */
4360
4361enum arm_vfp_cprc_base_type
4362{
4363 VFP_CPRC_UNKNOWN,
4364 VFP_CPRC_SINGLE,
4365 VFP_CPRC_DOUBLE,
4366 VFP_CPRC_VEC64,
4367 VFP_CPRC_VEC128
4368};
4369
4370/* The length of one element of base type B. */
4371
4372static unsigned
4373arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
4374{
4375 switch (b)
4376 {
4377 case VFP_CPRC_SINGLE:
4378 return 4;
4379 case VFP_CPRC_DOUBLE:
4380 return 8;
4381 case VFP_CPRC_VEC64:
4382 return 8;
4383 case VFP_CPRC_VEC128:
4384 return 16;
4385 default:
f34652de 4386 internal_error (_("Invalid VFP CPRC type: %d."),
90445bd3
DJ
4387 (int) b);
4388 }
4389}
4390
4391/* The character ('s', 'd' or 'q') for the type of VFP register used
4392 for passing base type B. */
4393
4394static int
4395arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
4396{
4397 switch (b)
4398 {
4399 case VFP_CPRC_SINGLE:
4400 return 's';
4401 case VFP_CPRC_DOUBLE:
4402 return 'd';
4403 case VFP_CPRC_VEC64:
4404 return 'd';
4405 case VFP_CPRC_VEC128:
4406 return 'q';
4407 default:
f34652de 4408 internal_error (_("Invalid VFP CPRC type: %d."),
90445bd3
DJ
4409 (int) b);
4410 }
4411}
4412
4413/* Determine whether T may be part of a candidate for passing and
4414 returning in VFP registers, ignoring the limit on the total number
4415 of components. If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
4416 classification of the first valid component found; if it is not
4417 VFP_CPRC_UNKNOWN, all components must have the same classification
4418 as *BASE_TYPE. If it is found that T contains a type not permitted
4419 for passing and returning in VFP registers, a type differently
4420 classified from *BASE_TYPE, or two types differently classified
4421 from each other, return -1, otherwise return the total number of
4422 base-type elements found (possibly 0 in an empty structure or
817e0957
YQ
4423 array). Vector types are not currently supported, matching the
4424 generic AAPCS support. */
90445bd3
DJ
4425
4426static int
4427arm_vfp_cprc_sub_candidate (struct type *t,
4428 enum arm_vfp_cprc_base_type *base_type)
4429{
4430 t = check_typedef (t);
78134374 4431 switch (t->code ())
90445bd3
DJ
4432 {
4433 case TYPE_CODE_FLT:
df86565b 4434 switch (t->length ())
90445bd3
DJ
4435 {
4436 case 4:
4437 if (*base_type == VFP_CPRC_UNKNOWN)
4438 *base_type = VFP_CPRC_SINGLE;
4439 else if (*base_type != VFP_CPRC_SINGLE)
4440 return -1;
4441 return 1;
4442
4443 case 8:
4444 if (*base_type == VFP_CPRC_UNKNOWN)
4445 *base_type = VFP_CPRC_DOUBLE;
4446 else if (*base_type != VFP_CPRC_DOUBLE)
4447 return -1;
4448 return 1;
4449
4450 default:
4451 return -1;
4452 }
4453 break;
4454
817e0957
YQ
4455 case TYPE_CODE_COMPLEX:
4456 /* Arguments of complex T where T is one of the types float or
4457 double get treated as if they are implemented as:
4458
4459 struct complexT
4460 {
4461 T real;
4462 T imag;
5f52445b
YQ
4463 };
4464
4465 */
df86565b 4466 switch (t->length ())
817e0957
YQ
4467 {
4468 case 8:
4469 if (*base_type == VFP_CPRC_UNKNOWN)
4470 *base_type = VFP_CPRC_SINGLE;
4471 else if (*base_type != VFP_CPRC_SINGLE)
4472 return -1;
4473 return 2;
4474
4475 case 16:
4476 if (*base_type == VFP_CPRC_UNKNOWN)
4477 *base_type = VFP_CPRC_DOUBLE;
4478 else if (*base_type != VFP_CPRC_DOUBLE)
4479 return -1;
4480 return 2;
4481
4482 default:
4483 return -1;
4484 }
4485 break;
4486
90445bd3
DJ
4487 case TYPE_CODE_ARRAY:
4488 {
bd63c870 4489 if (t->is_vector ())
90445bd3 4490 {
c4312b19
YQ
4491 /* A 64-bit or 128-bit containerized vector type are VFP
4492 CPRCs. */
df86565b 4493 switch (t->length ())
c4312b19
YQ
4494 {
4495 case 8:
4496 if (*base_type == VFP_CPRC_UNKNOWN)
4497 *base_type = VFP_CPRC_VEC64;
4498 return 1;
4499 case 16:
4500 if (*base_type == VFP_CPRC_UNKNOWN)
4501 *base_type = VFP_CPRC_VEC128;
4502 return 1;
4503 default:
4504 return -1;
4505 }
4506 }
4507 else
4508 {
4509 int count;
4510 unsigned unitlen;
4511
27710edb 4512 count = arm_vfp_cprc_sub_candidate (t->target_type (),
c4312b19
YQ
4513 base_type);
4514 if (count == -1)
4515 return -1;
df86565b 4516 if (t->length () == 0)
c4312b19
YQ
4517 {
4518 gdb_assert (count == 0);
4519 return 0;
4520 }
4521 else if (count == 0)
4522 return -1;
4523 unitlen = arm_vfp_cprc_unit_length (*base_type);
df86565b
SM
4524 gdb_assert ((t->length () % unitlen) == 0);
4525 return t->length () / unitlen;
90445bd3 4526 }
90445bd3
DJ
4527 }
4528 break;
4529
4530 case TYPE_CODE_STRUCT:
4531 {
4532 int count = 0;
4533 unsigned unitlen;
4534 int i;
1f704f76 4535 for (i = 0; i < t->num_fields (); i++)
90445bd3 4536 {
1040b979
YQ
4537 int sub_count = 0;
4538
c819a338 4539 if (!t->field (i).is_static ())
940da03e 4540 sub_count = arm_vfp_cprc_sub_candidate (t->field (i).type (),
1040b979 4541 base_type);
90445bd3
DJ
4542 if (sub_count == -1)
4543 return -1;
4544 count += sub_count;
4545 }
df86565b 4546 if (t->length () == 0)
90445bd3
DJ
4547 {
4548 gdb_assert (count == 0);
4549 return 0;
4550 }
4551 else if (count == 0)
4552 return -1;
4553 unitlen = arm_vfp_cprc_unit_length (*base_type);
df86565b 4554 if (t->length () != unitlen * count)
90445bd3
DJ
4555 return -1;
4556 return count;
4557 }
4558
4559 case TYPE_CODE_UNION:
4560 {
4561 int count = 0;
4562 unsigned unitlen;
4563 int i;
1f704f76 4564 for (i = 0; i < t->num_fields (); i++)
90445bd3 4565 {
940da03e 4566 int sub_count = arm_vfp_cprc_sub_candidate (t->field (i).type (),
90445bd3
DJ
4567 base_type);
4568 if (sub_count == -1)
4569 return -1;
4570 count = (count > sub_count ? count : sub_count);
4571 }
df86565b 4572 if (t->length () == 0)
90445bd3
DJ
4573 {
4574 gdb_assert (count == 0);
4575 return 0;
4576 }
4577 else if (count == 0)
4578 return -1;
4579 unitlen = arm_vfp_cprc_unit_length (*base_type);
df86565b 4580 if (t->length () != unitlen * count)
90445bd3
DJ
4581 return -1;
4582 return count;
4583 }
4584
4585 default:
4586 break;
4587 }
4588
4589 return -1;
4590}
4591
4592/* Determine whether T is a VFP co-processor register candidate (CPRC)
4593 if passed to or returned from a non-variadic function with the VFP
4594 ABI in effect. Return 1 if it is, 0 otherwise. If it is, set
4595 *BASE_TYPE to the base type for T and *COUNT to the number of
4596 elements of that base type before returning. */
4597
4598static int
4599arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
4600 int *count)
4601{
4602 enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
4603 int c = arm_vfp_cprc_sub_candidate (t, &b);
4604 if (c <= 0 || c > 4)
4605 return 0;
4606 *base_type = b;
4607 *count = c;
4608 return 1;
4609}
4610
4611/* Return 1 if the VFP ABI should be used for passing arguments to and
4612 returning values from a function of type FUNC_TYPE, 0
4613 otherwise. */
4614
4615static int
4616arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
4617{
08106042 4618 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
345bd07c 4619
90445bd3
DJ
4620 /* Variadic functions always use the base ABI. Assume that functions
4621 without debug info are not variadic. */
a409645d 4622 if (func_type && check_typedef (func_type)->has_varargs ())
90445bd3 4623 return 0;
345bd07c 4624
90445bd3
DJ
4625 /* The VFP ABI is only supported as a variant of AAPCS. */
4626 if (tdep->arm_abi != ARM_ABI_AAPCS)
4627 return 0;
345bd07c
SM
4628
4629 return tdep->fp_model == ARM_FLOAT_VFP;
90445bd3
DJ
4630}
4631
4632/* We currently only support passing parameters in integer registers, which
4633 conforms with GCC's default model, and VFP argument passing following
4634 the VFP variant of AAPCS. Several other variants exist and
2dd604e7
RE
4635 we should probably support some of them based on the selected ABI. */
4636
4637static CORE_ADDR
7d9b040b 4638arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6a65450a 4639 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
cf84fa6b
AH
4640 struct value **args, CORE_ADDR sp,
4641 function_call_return_method return_method,
6a65450a 4642 CORE_ADDR struct_addr)
2dd604e7 4643{
e17a4113 4644 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2dd604e7
RE
4645 int argnum;
4646 int argreg;
4647 int nstack;
0fecb1a7 4648 struct arm_stack_item *si = NULL;
90445bd3
DJ
4649 int use_vfp_abi;
4650 struct type *ftype;
4651 unsigned vfp_regs_free = (1 << 16) - 1;
08106042 4652 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
90445bd3
DJ
4653
4654 /* Determine the type of this function and whether the VFP ABI
4655 applies. */
d0c97917 4656 ftype = check_typedef (function->type ());
78134374 4657 if (ftype->code () == TYPE_CODE_PTR)
27710edb 4658 ftype = check_typedef (ftype->target_type ());
90445bd3 4659 use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
2dd604e7 4660
6a65450a
AC
4661 /* Set the return address. For the ARM, the return breakpoint is
4662 always at BP_ADDR. */
9779414d 4663 if (arm_pc_is_thumb (gdbarch, bp_addr))
9dca5578 4664 bp_addr |= 1;
6a65450a 4665 regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
2dd604e7
RE
4666
4667 /* Walk through the list of args and determine how large a temporary
4668 stack is required. Need to take care here as structs may be
7a9dd1b2 4669 passed on the stack, and we have to push them. */
2dd604e7
RE
4670 nstack = 0;
4671
4672 argreg = ARM_A1_REGNUM;
4673 nstack = 0;
4674
2dd604e7
RE
4675 /* The struct_return pointer occupies the first parameter
4676 passing register. */
cf84fa6b 4677 if (return_method == return_method_struct)
2dd604e7 4678 {
7cb6d92a
SM
4679 arm_debug_printf ("struct return in %s = %s",
4680 gdbarch_register_name (gdbarch, argreg),
4681 paddress (gdbarch, struct_addr));
4682
2dd604e7
RE
4683 regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
4684 argreg++;
4685 }
4686
4687 for (argnum = 0; argnum < nargs; argnum++)
4688 {
4689 int len;
4690 struct type *arg_type;
4691 struct type *target_type;
4692 enum type_code typecode;
8c6363cf 4693 const bfd_byte *val;
2af48f68 4694 int align;
90445bd3
DJ
4695 enum arm_vfp_cprc_base_type vfp_base_type;
4696 int vfp_base_count;
4697 int may_use_core_reg = 1;
2dd604e7 4698
d0c97917 4699 arg_type = check_typedef (args[argnum]->type ());
df86565b 4700 len = arg_type->length ();
27710edb 4701 target_type = arg_type->target_type ();
78134374 4702 typecode = arg_type->code ();
efaf1ae0 4703 val = args[argnum]->contents ().data ();
2dd604e7 4704
030197b4 4705 align = type_align (arg_type);
2af48f68 4706 /* Round alignment up to a whole number of words. */
f0452268
AH
4707 align = (align + ARM_INT_REGISTER_SIZE - 1)
4708 & ~(ARM_INT_REGISTER_SIZE - 1);
2af48f68 4709 /* Different ABIs have different maximum alignments. */
345bd07c 4710 if (tdep->arm_abi == ARM_ABI_APCS)
2af48f68
PB
4711 {
4712 /* The APCS ABI only requires word alignment. */
f0452268 4713 align = ARM_INT_REGISTER_SIZE;
2af48f68
PB
4714 }
4715 else
4716 {
4717 /* The AAPCS requires at most doubleword alignment. */
f0452268
AH
4718 if (align > ARM_INT_REGISTER_SIZE * 2)
4719 align = ARM_INT_REGISTER_SIZE * 2;
2af48f68
PB
4720 }
4721
90445bd3
DJ
4722 if (use_vfp_abi
4723 && arm_vfp_call_candidate (arg_type, &vfp_base_type,
4724 &vfp_base_count))
4725 {
4726 int regno;
4727 int unit_length;
4728 int shift;
4729 unsigned mask;
4730
4731 /* Because this is a CPRC it cannot go in a core register or
4732 cause a core register to be skipped for alignment.
4733 Either it goes in VFP registers and the rest of this loop
4734 iteration is skipped for this argument, or it goes on the
4735 stack (and the stack alignment code is correct for this
4736 case). */
4737 may_use_core_reg = 0;
4738
4739 unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
4740 shift = unit_length / 4;
4741 mask = (1 << (shift * vfp_base_count)) - 1;
4742 for (regno = 0; regno < 16; regno += shift)
4743 if (((vfp_regs_free >> regno) & mask) == mask)
4744 break;
4745
4746 if (regno < 16)
4747 {
4748 int reg_char;
4749 int reg_scaled;
4750 int i;
4751
4752 vfp_regs_free &= ~(mask << regno);
4753 reg_scaled = regno / shift;
4754 reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
4755 for (i = 0; i < vfp_base_count; i++)
4756 {
4757 char name_buf[4];
4758 int regnum;
58d6951d
DJ
4759 if (reg_char == 'q')
4760 arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
90445bd3 4761 val + i * unit_length);
58d6951d
DJ
4762 else
4763 {
8c042590
PM
4764 xsnprintf (name_buf, sizeof (name_buf), "%c%d",
4765 reg_char, reg_scaled + i);
58d6951d
DJ
4766 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
4767 strlen (name_buf));
b66f5587 4768 regcache->cooked_write (regnum, val + i * unit_length);
58d6951d 4769 }
90445bd3
DJ
4770 }
4771 continue;
4772 }
4773 else
4774 {
4775 /* This CPRC could not go in VFP registers, so all VFP
4776 registers are now marked as used. */
4777 vfp_regs_free = 0;
4778 }
4779 }
4780
85102364 4781 /* Push stack padding for doubleword alignment. */
2af48f68
PB
4782 if (nstack & (align - 1))
4783 {
f0452268
AH
4784 si = push_stack_item (si, val, ARM_INT_REGISTER_SIZE);
4785 nstack += ARM_INT_REGISTER_SIZE;
2af48f68
PB
4786 }
4787
4788 /* Doubleword aligned quantities must go in even register pairs. */
90445bd3
DJ
4789 if (may_use_core_reg
4790 && argreg <= ARM_LAST_ARG_REGNUM
f0452268 4791 && align > ARM_INT_REGISTER_SIZE
2af48f68
PB
4792 && argreg & 1)
4793 argreg++;
4794
2dd604e7
RE
4795 /* If the argument is a pointer to a function, and it is a
4796 Thumb function, create a LOCAL copy of the value and set
4797 the THUMB bit in it. */
4798 if (TYPE_CODE_PTR == typecode
4799 && target_type != NULL
78134374 4800 && TYPE_CODE_FUNC == check_typedef (target_type)->code ())
2dd604e7 4801 {
e17a4113 4802 CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
9779414d 4803 if (arm_pc_is_thumb (gdbarch, regval))
2dd604e7 4804 {
224c3ddb 4805 bfd_byte *copy = (bfd_byte *) alloca (len);
8c6363cf 4806 store_unsigned_integer (copy, len, byte_order,
e17a4113 4807 MAKE_THUMB_ADDR (regval));
8c6363cf 4808 val = copy;
2dd604e7
RE
4809 }
4810 }
4811
4812 /* Copy the argument to general registers or the stack in
4813 register-sized pieces. Large arguments are split between
4814 registers and stack. */
4815 while (len > 0)
4816 {
f0452268
AH
4817 int partial_len = len < ARM_INT_REGISTER_SIZE
4818 ? len : ARM_INT_REGISTER_SIZE;
ef9bd0b8
YQ
4819 CORE_ADDR regval
4820 = extract_unsigned_integer (val, partial_len, byte_order);
2dd604e7 4821
90445bd3 4822 if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
2dd604e7
RE
4823 {
4824 /* The argument is being passed in a general purpose
4825 register. */
7cb6d92a
SM
4826 arm_debug_printf ("arg %d in %s = 0x%s", argnum,
4827 gdbarch_register_name (gdbarch, argreg),
4828 phex (regval, ARM_INT_REGISTER_SIZE));
4829
2dd604e7
RE
4830 regcache_cooked_write_unsigned (regcache, argreg, regval);
4831 argreg++;
4832 }
4833 else
4834 {
f0452268 4835 gdb_byte buf[ARM_INT_REGISTER_SIZE];
ef9bd0b8
YQ
4836
4837 memset (buf, 0, sizeof (buf));
4838 store_unsigned_integer (buf, partial_len, byte_order, regval);
4839
2dd604e7 4840 /* Push the arguments onto the stack. */
7cb6d92a 4841 arm_debug_printf ("arg %d @ sp + %d", argnum, nstack);
f0452268
AH
4842 si = push_stack_item (si, buf, ARM_INT_REGISTER_SIZE);
4843 nstack += ARM_INT_REGISTER_SIZE;
2dd604e7
RE
4844 }
4845
4846 len -= partial_len;
4847 val += partial_len;
4848 }
4849 }
4850 /* If we have an odd number of words to push, then decrement the stack
4851 by one word now, so first stack argument will be dword aligned. */
4852 if (nstack & 4)
4853 sp -= 4;
4854
4855 while (si)
4856 {
4857 sp -= si->len;
4858 write_memory (sp, si->data, si->len);
4859 si = pop_stack_item (si);
4860 }
4861
4862 /* Finally, update teh SP register. */
4863 regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
4864
4865 return sp;
4866}
4867
f53f0d0b
PB
4868
4869/* Always align the frame to an 8-byte boundary. This is required on
4870 some platforms and harmless on the rest. */
4871
4872static CORE_ADDR
4873arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
4874{
4875 /* Align the stack to eight bytes. */
4876 return sp & ~ (CORE_ADDR) 7;
4877}
4878
c906108c 4879static void
12b27276 4880print_fpu_flags (struct ui_file *file, int flags)
c906108c 4881{
c5aa993b 4882 if (flags & (1 << 0))
0426ad51 4883 gdb_puts ("IVO ", file);
c5aa993b 4884 if (flags & (1 << 1))
0426ad51 4885 gdb_puts ("DVZ ", file);
c5aa993b 4886 if (flags & (1 << 2))
0426ad51 4887 gdb_puts ("OFL ", file);
c5aa993b 4888 if (flags & (1 << 3))
0426ad51 4889 gdb_puts ("UFL ", file);
c5aa993b 4890 if (flags & (1 << 4))
0426ad51 4891 gdb_puts ("INX ", file);
a11ac3b3 4892 gdb_putc ('\n', file);
c906108c
SS
4893}
4894
5e74b15c
RE
4895/* Print interesting information about the floating point processor
4896 (if present) or emulator. */
34e8f22d 4897static void
d855c300 4898arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
8480a37e 4899 const frame_info_ptr &frame, const char *args)
c906108c 4900{
9c9acae0 4901 unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
c5aa993b
JM
4902 int type;
4903
4904 type = (status >> 24) & 127;
edefbb7c 4905 if (status & (1 << 31))
6cb06a8c 4906 gdb_printf (file, _("Hardware FPU type %d\n"), type);
edefbb7c 4907 else
6cb06a8c 4908 gdb_printf (file, _("Software FPU type %d\n"), type);
edefbb7c 4909 /* i18n: [floating point unit] mask */
0426ad51 4910 gdb_puts (_("mask: "), file);
12b27276 4911 print_fpu_flags (file, status >> 16);
edefbb7c 4912 /* i18n: [floating point unit] flags */
0426ad51 4913 gdb_puts (_("flags: "), file);
12b27276 4914 print_fpu_flags (file, status);
c906108c
SS
4915}
4916
27067745
UW
4917/* Construct the ARM extended floating point type. */
4918static struct type *
4919arm_ext_type (struct gdbarch *gdbarch)
4920{
08106042 4921 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
27067745
UW
4922
4923 if (!tdep->arm_ext_type)
77c5f496
TT
4924 {
4925 type_allocator alloc (gdbarch);
4926 tdep->arm_ext_type
4927 = init_float_type (alloc, -1, "builtin_type_arm_ext",
4928 floatformats_arm_ext);
4929 }
27067745
UW
4930
4931 return tdep->arm_ext_type;
4932}
4933
58d6951d
DJ
4934static struct type *
4935arm_neon_double_type (struct gdbarch *gdbarch)
4936{
08106042 4937 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d
DJ
4938
4939 if (tdep->neon_double_type == NULL)
4940 {
4941 struct type *t, *elem;
4942
4943 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
4944 TYPE_CODE_UNION);
4945 elem = builtin_type (gdbarch)->builtin_uint8;
4946 append_composite_type_field (t, "u8", init_vector_type (elem, 8));
4947 elem = builtin_type (gdbarch)->builtin_uint16;
4948 append_composite_type_field (t, "u16", init_vector_type (elem, 4));
4949 elem = builtin_type (gdbarch)->builtin_uint32;
4950 append_composite_type_field (t, "u32", init_vector_type (elem, 2));
4951 elem = builtin_type (gdbarch)->builtin_uint64;
4952 append_composite_type_field (t, "u64", elem);
4953 elem = builtin_type (gdbarch)->builtin_float;
4954 append_composite_type_field (t, "f32", init_vector_type (elem, 2));
4955 elem = builtin_type (gdbarch)->builtin_double;
4956 append_composite_type_field (t, "f64", elem);
4957
2062087b 4958 t->set_is_vector (true);
d0e39ea2 4959 t->set_name ("neon_d");
58d6951d
DJ
4960 tdep->neon_double_type = t;
4961 }
4962
4963 return tdep->neon_double_type;
4964}
4965
4966/* FIXME: The vector types are not correctly ordered on big-endian
4967 targets. Just as s0 is the low bits of d0, d0[0] is also the low
4968 bits of d0 - regardless of what unit size is being held in d0. So
4969 the offset of the first uint8 in d0 is 7, but the offset of the
4970 first float is 4. This code works as-is for little-endian
4971 targets. */
4972
4973static struct type *
4974arm_neon_quad_type (struct gdbarch *gdbarch)
4975{
08106042 4976 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d
DJ
4977
4978 if (tdep->neon_quad_type == NULL)
4979 {
4980 struct type *t, *elem;
4981
4982 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
4983 TYPE_CODE_UNION);
4984 elem = builtin_type (gdbarch)->builtin_uint8;
4985 append_composite_type_field (t, "u8", init_vector_type (elem, 16));
4986 elem = builtin_type (gdbarch)->builtin_uint16;
4987 append_composite_type_field (t, "u16", init_vector_type (elem, 8));
4988 elem = builtin_type (gdbarch)->builtin_uint32;
4989 append_composite_type_field (t, "u32", init_vector_type (elem, 4));
4990 elem = builtin_type (gdbarch)->builtin_uint64;
4991 append_composite_type_field (t, "u64", init_vector_type (elem, 2));
4992 elem = builtin_type (gdbarch)->builtin_float;
4993 append_composite_type_field (t, "f32", init_vector_type (elem, 4));
4994 elem = builtin_type (gdbarch)->builtin_double;
4995 append_composite_type_field (t, "f64", init_vector_type (elem, 2));
4996
2062087b 4997 t->set_is_vector (true);
d0e39ea2 4998 t->set_name ("neon_q");
58d6951d
DJ
4999 tdep->neon_quad_type = t;
5000 }
5001
5002 return tdep->neon_quad_type;
5003}
5004
ecbf5d4f
LM
5005/* Return true if REGNUM is a Q pseudo register. Return false
5006 otherwise.
5007
5008 REGNUM is the raw register number and not a pseudo-relative register
5009 number. */
5010
5011static bool
5012is_q_pseudo (struct gdbarch *gdbarch, int regnum)
5013{
08106042 5014 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ecbf5d4f 5015
ae66a8f1
SP
5016 /* Q pseudo registers are available for both NEON (Q0~Q15) and
5017 MVE (Q0~Q7) features. */
ecbf5d4f
LM
5018 if (tdep->have_q_pseudos
5019 && regnum >= tdep->q_pseudo_base
5020 && regnum < (tdep->q_pseudo_base + tdep->q_pseudo_count))
5021 return true;
5022
5023 return false;
5024}
5025
5026/* Return true if REGNUM is a VFP S pseudo register. Return false
5027 otherwise.
5028
5029 REGNUM is the raw register number and not a pseudo-relative register
5030 number. */
5031
5032static bool
5033is_s_pseudo (struct gdbarch *gdbarch, int regnum)
5034{
08106042 5035 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ecbf5d4f
LM
5036
5037 if (tdep->have_s_pseudos
5038 && regnum >= tdep->s_pseudo_base
5039 && regnum < (tdep->s_pseudo_base + tdep->s_pseudo_count))
5040 return true;
5041
5042 return false;
5043}
5044
ae66a8f1
SP
5045/* Return true if REGNUM is a MVE pseudo register (P0). Return false
5046 otherwise.
5047
5048 REGNUM is the raw register number and not a pseudo-relative register
5049 number. */
5050
5051static bool
5052is_mve_pseudo (struct gdbarch *gdbarch, int regnum)
5053{
08106042 5054 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ae66a8f1
SP
5055
5056 if (tdep->have_mve
5057 && regnum >= tdep->mve_pseudo_base
5058 && regnum < tdep->mve_pseudo_base + tdep->mve_pseudo_count)
5059 return true;
5060
5061 return false;
5062}
5063
a01567f4
LM
5064/* Return true if REGNUM is a PACBTI pseudo register (ra_auth_code). Return
5065 false otherwise.
5066
5067 REGNUM is the raw register number and not a pseudo-relative register
5068 number. */
5069
5070static bool
5071is_pacbti_pseudo (struct gdbarch *gdbarch, int regnum)
5072{
08106042 5073 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
a01567f4
LM
5074
5075 if (tdep->have_pacbti
5076 && regnum >= tdep->pacbti_pseudo_base
5077 && regnum < tdep->pacbti_pseudo_base + tdep->pacbti_pseudo_count)
5078 return true;
5079
5080 return false;
5081}
5082
34e8f22d
RE
5083/* Return the GDB type object for the "standard" data type of data in
5084 register N. */
5085
5086static struct type *
7a5ea0d4 5087arm_register_type (struct gdbarch *gdbarch, int regnum)
032758dc 5088{
08106042 5089 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d 5090
ecbf5d4f 5091 if (is_s_pseudo (gdbarch, regnum))
58d6951d
DJ
5092 return builtin_type (gdbarch)->builtin_float;
5093
ecbf5d4f 5094 if (is_q_pseudo (gdbarch, regnum))
58d6951d
DJ
5095 return arm_neon_quad_type (gdbarch);
5096
ae66a8f1
SP
5097 if (is_mve_pseudo (gdbarch, regnum))
5098 return builtin_type (gdbarch)->builtin_int16;
5099
a01567f4
LM
5100 if (is_pacbti_pseudo (gdbarch, regnum))
5101 return builtin_type (gdbarch)->builtin_uint32;
5102
58d6951d
DJ
5103 /* If the target description has register information, we are only
5104 in this function so that we can override the types of
5105 double-precision registers for NEON. */
5106 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
5107 {
5108 struct type *t = tdesc_register_type (gdbarch, regnum);
5109
5110 if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
78134374 5111 && t->code () == TYPE_CODE_FLT
ecbf5d4f 5112 && tdep->have_neon)
58d6951d
DJ
5113 return arm_neon_double_type (gdbarch);
5114 else
5115 return t;
5116 }
5117
34e8f22d 5118 if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
58d6951d 5119 {
ecbf5d4f 5120 if (!tdep->have_fpa_registers)
58d6951d
DJ
5121 return builtin_type (gdbarch)->builtin_void;
5122
5123 return arm_ext_type (gdbarch);
5124 }
e4c16157 5125 else if (regnum == ARM_SP_REGNUM)
0dfff4cb 5126 return builtin_type (gdbarch)->builtin_data_ptr;
e4c16157 5127 else if (regnum == ARM_PC_REGNUM)
0dfff4cb 5128 return builtin_type (gdbarch)->builtin_func_ptr;
ff6f572f
DJ
5129 else if (regnum >= ARRAY_SIZE (arm_register_names))
5130 /* These registers are only supported on targets which supply
5131 an XML description. */
df4df182 5132 return builtin_type (gdbarch)->builtin_int0;
032758dc 5133 else
df4df182 5134 return builtin_type (gdbarch)->builtin_uint32;
032758dc
AC
5135}
5136
ff6f572f
DJ
5137/* Map a DWARF register REGNUM onto the appropriate GDB register
5138 number. */
5139
5140static int
d3f73121 5141arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
ff6f572f
DJ
5142{
5143 /* Core integer regs. */
5144 if (reg >= 0 && reg <= 15)
5145 return reg;
5146
5147 /* Legacy FPA encoding. These were once used in a way which
5148 overlapped with VFP register numbering, so their use is
5149 discouraged, but GDB doesn't support the ARM toolchain
5150 which used them for VFP. */
5151 if (reg >= 16 && reg <= 23)
5152 return ARM_F0_REGNUM + reg - 16;
5153
5154 /* New assignments for the FPA registers. */
5155 if (reg >= 96 && reg <= 103)
5156 return ARM_F0_REGNUM + reg - 96;
5157
5158 /* WMMX register assignments. */
5159 if (reg >= 104 && reg <= 111)
5160 return ARM_WCGR0_REGNUM + reg - 104;
5161
5162 if (reg >= 112 && reg <= 127)
5163 return ARM_WR0_REGNUM + reg - 112;
5164
a01567f4
LM
5165 /* PACBTI register containing the Pointer Authentication Code. */
5166 if (reg == ARM_DWARF_RA_AUTH_CODE)
5167 {
08106042 5168 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
a01567f4
LM
5169
5170 if (tdep->have_pacbti)
5171 return tdep->pacbti_pseudo_base;
5172
5173 return -1;
5174 }
5175
ff6f572f
DJ
5176 if (reg >= 192 && reg <= 199)
5177 return ARM_WC0_REGNUM + reg - 192;
5178
58d6951d
DJ
5179 /* VFP v2 registers. A double precision value is actually
5180 in d1 rather than s2, but the ABI only defines numbering
5181 for the single precision registers. This will "just work"
5182 in GDB for little endian targets (we'll read eight bytes,
5183 starting in s0 and then progressing to s1), but will be
5184 reversed on big endian targets with VFP. This won't
5185 be a problem for the new Neon quad registers; you're supposed
5186 to use DW_OP_piece for those. */
5187 if (reg >= 64 && reg <= 95)
5188 {
5189 char name_buf[4];
5190
8c042590 5191 xsnprintf (name_buf, sizeof (name_buf), "s%d", reg - 64);
58d6951d
DJ
5192 return user_reg_map_name_to_regnum (gdbarch, name_buf,
5193 strlen (name_buf));
5194 }
5195
5196 /* VFP v3 / Neon registers. This range is also used for VFP v2
5197 registers, except that it now describes d0 instead of s0. */
5198 if (reg >= 256 && reg <= 287)
5199 {
5200 char name_buf[4];
5201
8c042590 5202 xsnprintf (name_buf, sizeof (name_buf), "d%d", reg - 256);
58d6951d
DJ
5203 return user_reg_map_name_to_regnum (gdbarch, name_buf,
5204 strlen (name_buf));
5205 }
5206
ff6f572f
DJ
5207 return -1;
5208}
5209
26216b98
AC
5210/* Map GDB internal REGNUM onto the Arm simulator register numbers. */
5211static int
e7faf938 5212arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
26216b98
AC
5213{
5214 int reg = regnum;
e7faf938 5215 gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
26216b98 5216
ff6f572f
DJ
5217 if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
5218 return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
5219
5220 if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
5221 return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
5222
5223 if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
5224 return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
5225
26216b98
AC
5226 if (reg < NUM_GREGS)
5227 return SIM_ARM_R0_REGNUM + reg;
5228 reg -= NUM_GREGS;
5229
5230 if (reg < NUM_FREGS)
5231 return SIM_ARM_FP0_REGNUM + reg;
5232 reg -= NUM_FREGS;
5233
5234 if (reg < NUM_SREGS)
5235 return SIM_ARM_FPS_REGNUM + reg;
5236 reg -= NUM_SREGS;
5237
f34652de 5238 internal_error (_("Bad REGNUM %d"), regnum);
26216b98 5239}
34e8f22d 5240
a01567f4
LM
5241static const unsigned char op_lit0 = DW_OP_lit0;
5242
5243static void
5244arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
5245 struct dwarf2_frame_state_reg *reg,
8480a37e 5246 const frame_info_ptr &this_frame)
a01567f4 5247{
a6e4a48c
YR
5248 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
5249
a01567f4
LM
5250 if (is_pacbti_pseudo (gdbarch, regnum))
5251 {
5252 /* Initialize RA_AUTH_CODE to zero. */
5253 reg->how = DWARF2_FRAME_REG_SAVED_VAL_EXP;
5254 reg->loc.exp.start = &op_lit0;
5255 reg->loc.exp.len = 1;
5256 return;
5257 }
5258
42e11f36 5259 if (regnum == ARM_PC_REGNUM || regnum == ARM_PS_REGNUM)
a01567f4 5260 {
a01567f4
LM
5261 reg->how = DWARF2_FRAME_REG_FN;
5262 reg->loc.fn = arm_dwarf2_prev_register;
a01567f4 5263 }
42e11f36
TS
5264 else if (regnum == ARM_SP_REGNUM)
5265 reg->how = DWARF2_FRAME_REG_CFA;
a6e4a48c
YR
5266 else if (arm_is_alternative_sp_register (tdep, regnum))
5267 {
5268 /* Handle the alternative SP registers on Cortex-M. */
5269 reg->how = DWARF2_FRAME_REG_FN;
5270 reg->loc.fn = arm_dwarf2_prev_register;
5271 }
a01567f4
LM
5272}
5273
d9311bfa
AT
5274/* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
5275 the buffer to be NEW_LEN bytes ending at ENDADDR. Return
5276 NULL if an error occurs. BUF is freed. */
c906108c 5277
d9311bfa
AT
5278static gdb_byte *
5279extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
5280 int old_len, int new_len)
5281{
5282 gdb_byte *new_buf;
5283 int bytes_to_read = new_len - old_len;
c906108c 5284
d9311bfa
AT
5285 new_buf = (gdb_byte *) xmalloc (new_len);
5286 memcpy (new_buf + bytes_to_read, buf, old_len);
5287 xfree (buf);
198cd59d 5288 if (target_read_code (endaddr - new_len, new_buf, bytes_to_read) != 0)
d9311bfa
AT
5289 {
5290 xfree (new_buf);
5291 return NULL;
c906108c 5292 }
d9311bfa 5293 return new_buf;
c906108c
SS
5294}
5295
d9311bfa
AT
5296/* An IT block is at most the 2-byte IT instruction followed by
5297 four 4-byte instructions. The furthest back we must search to
5298 find an IT block that affects the current instruction is thus
5299 2 + 3 * 4 == 14 bytes. */
5300#define MAX_IT_BLOCK_PREFIX 14
177321bd 5301
d9311bfa
AT
5302/* Use a quick scan if there are more than this many bytes of
5303 code. */
5304#define IT_SCAN_THRESHOLD 32
177321bd 5305
d9311bfa
AT
5306/* Adjust a breakpoint's address to move breakpoints out of IT blocks.
5307 A breakpoint in an IT block may not be hit, depending on the
5308 condition flags. */
ad527d2e 5309static CORE_ADDR
d9311bfa 5310arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
c906108c 5311{
d9311bfa
AT
5312 gdb_byte *buf;
5313 char map_type;
5314 CORE_ADDR boundary, func_start;
5315 int buf_len;
5316 enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
5317 int i, any, last_it, last_it_count;
08106042 5318 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
177321bd 5319
d9311bfa 5320 /* If we are using BKPT breakpoints, none of this is necessary. */
345bd07c 5321 if (tdep->thumb2_breakpoint == NULL)
d9311bfa 5322 return bpaddr;
177321bd 5323
d9311bfa
AT
5324 /* ARM mode does not have this problem. */
5325 if (!arm_pc_is_thumb (gdbarch, bpaddr))
5326 return bpaddr;
177321bd 5327
d9311bfa
AT
5328 /* We are setting a breakpoint in Thumb code that could potentially
5329 contain an IT block. The first step is to find how much Thumb
5330 code there is; we do not need to read outside of known Thumb
5331 sequences. */
5332 map_type = arm_find_mapping_symbol (bpaddr, &boundary);
5333 if (map_type == 0)
5334 /* Thumb-2 code must have mapping symbols to have a chance. */
5335 return bpaddr;
9dca5578 5336
d9311bfa 5337 bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
177321bd 5338
8c3273ee
SM
5339 if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL))
5340 {
5341 func_start = gdbarch_addr_bits_remove (gdbarch, func_start);
5342 if (func_start > boundary)
5343 boundary = func_start;
5344 }
9dca5578 5345
d9311bfa
AT
5346 /* Search for a candidate IT instruction. We have to do some fancy
5347 footwork to distinguish a real IT instruction from the second
5348 half of a 32-bit instruction, but there is no need for that if
5349 there's no candidate. */
325fac50 5350 buf_len = std::min (bpaddr - boundary, (CORE_ADDR) MAX_IT_BLOCK_PREFIX);
d9311bfa
AT
5351 if (buf_len == 0)
5352 /* No room for an IT instruction. */
5353 return bpaddr;
c906108c 5354
d9311bfa 5355 buf = (gdb_byte *) xmalloc (buf_len);
198cd59d 5356 if (target_read_code (bpaddr - buf_len, buf, buf_len) != 0)
d9311bfa
AT
5357 return bpaddr;
5358 any = 0;
5359 for (i = 0; i < buf_len; i += 2)
c906108c 5360 {
d9311bfa
AT
5361 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5362 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
25b41d01 5363 {
d9311bfa
AT
5364 any = 1;
5365 break;
25b41d01 5366 }
c906108c 5367 }
d9311bfa
AT
5368
5369 if (any == 0)
c906108c 5370 {
d9311bfa
AT
5371 xfree (buf);
5372 return bpaddr;
f9d67f43
DJ
5373 }
5374
5375 /* OK, the code bytes before this instruction contain at least one
5376 halfword which resembles an IT instruction. We know that it's
5377 Thumb code, but there are still two possibilities. Either the
5378 halfword really is an IT instruction, or it is the second half of
5379 a 32-bit Thumb instruction. The only way we can tell is to
5380 scan forwards from a known instruction boundary. */
5381 if (bpaddr - boundary > IT_SCAN_THRESHOLD)
5382 {
5383 int definite;
5384
5385 /* There's a lot of code before this instruction. Start with an
5386 optimistic search; it's easy to recognize halfwords that can
5387 not be the start of a 32-bit instruction, and use that to
5388 lock on to the instruction boundaries. */
5389 buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
5390 if (buf == NULL)
5391 return bpaddr;
5392 buf_len = IT_SCAN_THRESHOLD;
5393
5394 definite = 0;
5395 for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
5396 {
5397 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5398 if (thumb_insn_size (inst1) == 2)
5399 {
5400 definite = 1;
5401 break;
5402 }
5403 }
5404
5405 /* At this point, if DEFINITE, BUF[I] is the first place we
5406 are sure that we know the instruction boundaries, and it is far
5407 enough from BPADDR that we could not miss an IT instruction
5408 affecting BPADDR. If ! DEFINITE, give up - start from a
5409 known boundary. */
5410 if (! definite)
5411 {
0963b4bd
MS
5412 buf = extend_buffer_earlier (buf, bpaddr, buf_len,
5413 bpaddr - boundary);
f9d67f43
DJ
5414 if (buf == NULL)
5415 return bpaddr;
5416 buf_len = bpaddr - boundary;
5417 i = 0;
5418 }
5419 }
5420 else
5421 {
5422 buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
5423 if (buf == NULL)
5424 return bpaddr;
5425 buf_len = bpaddr - boundary;
5426 i = 0;
5427 }
5428
5429 /* Scan forwards. Find the last IT instruction before BPADDR. */
5430 last_it = -1;
5431 last_it_count = 0;
5432 while (i < buf_len)
5433 {
5434 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5435 last_it_count--;
5436 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5437 {
5438 last_it = i;
5439 if (inst1 & 0x0001)
5440 last_it_count = 4;
5441 else if (inst1 & 0x0002)
5442 last_it_count = 3;
5443 else if (inst1 & 0x0004)
5444 last_it_count = 2;
5445 else
5446 last_it_count = 1;
5447 }
5448 i += thumb_insn_size (inst1);
5449 }
5450
5451 xfree (buf);
5452
5453 if (last_it == -1)
5454 /* There wasn't really an IT instruction after all. */
5455 return bpaddr;
5456
5457 if (last_it_count < 1)
5458 /* It was too far away. */
5459 return bpaddr;
5460
5461 /* This really is a trouble spot. Move the breakpoint to the IT
5462 instruction. */
5463 return bpaddr - buf_len + last_it;
5464}
5465
cca44b1b 5466/* ARM displaced stepping support.
c906108c 5467
cca44b1b 5468 Generally ARM displaced stepping works as follows:
c906108c 5469
cca44b1b 5470 1. When an instruction is to be single-stepped, it is first decoded by
2ba163c8
SM
5471 arm_process_displaced_insn. Depending on the type of instruction, it is
5472 then copied to a scratch location, possibly in a modified form. The
5473 copy_* set of functions performs such modification, as necessary. A
5474 breakpoint is placed after the modified instruction in the scratch space
5475 to return control to GDB. Note in particular that instructions which
5476 modify the PC will no longer do so after modification.
c5aa993b 5477
cca44b1b
JB
5478 2. The instruction is single-stepped, by setting the PC to the scratch
5479 location address, and resuming. Control returns to GDB when the
5480 breakpoint is hit.
c5aa993b 5481
cca44b1b
JB
5482 3. A cleanup function (cleanup_*) is called corresponding to the copy_*
5483 function used for the current instruction. This function's job is to
5484 put the CPU/memory state back to what it would have been if the
5485 instruction had been executed unmodified in its original location. */
c5aa993b 5486
cca44b1b
JB
5487/* NOP instruction (mov r0, r0). */
5488#define ARM_NOP 0xe1a00000
34518530 5489#define THUMB_NOP 0x4600
cca44b1b
JB
5490
5491/* Helper for register reads for displaced stepping. In particular, this
5492 returns the PC as it would be seen by the instruction at its original
5493 location. */
5494
5495ULONGEST
1152d984 5496displaced_read_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
36073a92 5497 int regno)
cca44b1b
JB
5498{
5499 ULONGEST ret;
36073a92 5500 CORE_ADDR from = dsc->insn_addr;
cca44b1b 5501
bf9f652a 5502 if (regno == ARM_PC_REGNUM)
cca44b1b 5503 {
4db71c0b
YQ
5504 /* Compute pipeline offset:
5505 - When executing an ARM instruction, PC reads as the address of the
5506 current instruction plus 8.
5507 - When executing a Thumb instruction, PC reads as the address of the
5508 current instruction plus 4. */
5509
36073a92 5510 if (!dsc->is_thumb)
4db71c0b
YQ
5511 from += 8;
5512 else
5513 from += 4;
5514
136821d9
SM
5515 displaced_debug_printf ("read pc value %.8lx",
5516 (unsigned long) from);
4db71c0b 5517 return (ULONGEST) from;
cca44b1b 5518 }
c906108c 5519 else
cca44b1b
JB
5520 {
5521 regcache_cooked_read_unsigned (regs, regno, &ret);
136821d9
SM
5522
5523 displaced_debug_printf ("read r%d value %.8lx",
5524 regno, (unsigned long) ret);
5525
cca44b1b
JB
5526 return ret;
5527 }
c906108c
SS
5528}
5529
cca44b1b
JB
5530static int
5531displaced_in_arm_mode (struct regcache *regs)
5532{
5533 ULONGEST ps;
ac7936df 5534 ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
66e810cd 5535
cca44b1b 5536 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
66e810cd 5537
9779414d 5538 return (ps & t_bit) == 0;
cca44b1b 5539}
66e810cd 5540
cca44b1b 5541/* Write to the PC as from a branch instruction. */
c906108c 5542
cca44b1b 5543static void
1152d984 5544branch_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
36073a92 5545 ULONGEST val)
c906108c 5546{
36073a92 5547 if (!dsc->is_thumb)
cca44b1b
JB
5548 /* Note: If bits 0/1 are set, this branch would be unpredictable for
5549 architecture versions < 6. */
0963b4bd
MS
5550 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5551 val & ~(ULONGEST) 0x3);
cca44b1b 5552 else
0963b4bd
MS
5553 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5554 val & ~(ULONGEST) 0x1);
cca44b1b 5555}
66e810cd 5556
cca44b1b
JB
5557/* Write to the PC as from a branch-exchange instruction. */
5558
5559static void
5560bx_write_pc (struct regcache *regs, ULONGEST val)
5561{
5562 ULONGEST ps;
ac7936df 5563 ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
cca44b1b
JB
5564
5565 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
5566
5567 if ((val & 1) == 1)
c906108c 5568 {
9779414d 5569 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
cca44b1b
JB
5570 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
5571 }
5572 else if ((val & 2) == 0)
5573 {
9779414d 5574 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 5575 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
c906108c
SS
5576 }
5577 else
5578 {
cca44b1b
JB
5579 /* Unpredictable behaviour. Try to do something sensible (switch to ARM
5580 mode, align dest to 4 bytes). */
5581 warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
9779414d 5582 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 5583 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
c906108c
SS
5584 }
5585}
ed9a39eb 5586
cca44b1b 5587/* Write to the PC as if from a load instruction. */
ed9a39eb 5588
34e8f22d 5589static void
1152d984 5590load_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
36073a92 5591 ULONGEST val)
ed9a39eb 5592{
cca44b1b
JB
5593 if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
5594 bx_write_pc (regs, val);
5595 else
36073a92 5596 branch_write_pc (regs, dsc, val);
cca44b1b 5597}
be8626e0 5598
cca44b1b
JB
5599/* Write to the PC as if from an ALU instruction. */
5600
5601static void
1152d984 5602alu_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
36073a92 5603 ULONGEST val)
cca44b1b 5604{
36073a92 5605 if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
cca44b1b
JB
5606 bx_write_pc (regs, val);
5607 else
36073a92 5608 branch_write_pc (regs, dsc, val);
cca44b1b
JB
5609}
5610
5611/* Helper for writing to registers for displaced stepping. Writing to the PC
5612 has a varying effects depending on the instruction which does the write:
5613 this is controlled by the WRITE_PC argument. */
5614
5615void
1152d984 5616displaced_write_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
cca44b1b
JB
5617 int regno, ULONGEST val, enum pc_write_style write_pc)
5618{
bf9f652a 5619 if (regno == ARM_PC_REGNUM)
08216dd7 5620 {
136821d9
SM
5621 displaced_debug_printf ("writing pc %.8lx", (unsigned long) val);
5622
cca44b1b 5623 switch (write_pc)
08216dd7 5624 {
cca44b1b 5625 case BRANCH_WRITE_PC:
36073a92 5626 branch_write_pc (regs, dsc, val);
08216dd7
RE
5627 break;
5628
cca44b1b
JB
5629 case BX_WRITE_PC:
5630 bx_write_pc (regs, val);
24b21115 5631 break;
cca44b1b
JB
5632
5633 case LOAD_WRITE_PC:
36073a92 5634 load_write_pc (regs, dsc, val);
24b21115 5635 break;
cca44b1b
JB
5636
5637 case ALU_WRITE_PC:
36073a92 5638 alu_write_pc (regs, dsc, val);
24b21115 5639 break;
cca44b1b
JB
5640
5641 case CANNOT_WRITE_PC:
5642 warning (_("Instruction wrote to PC in an unexpected way when "
5643 "single-stepping"));
08216dd7
RE
5644 break;
5645
5646 default:
f34652de 5647 internal_error (_("Invalid argument to displaced_write_reg"));
08216dd7 5648 }
b508a996 5649
cca44b1b 5650 dsc->wrote_to_pc = 1;
b508a996 5651 }
ed9a39eb 5652 else
b508a996 5653 {
136821d9
SM
5654 displaced_debug_printf ("writing r%d value %.8lx",
5655 regno, (unsigned long) val);
cca44b1b 5656 regcache_cooked_write_unsigned (regs, regno, val);
b508a996 5657 }
34e8f22d
RE
5658}
5659
cca44b1b
JB
5660/* This function is used to concisely determine if an instruction INSN
5661 references PC. Register fields of interest in INSN should have the
0963b4bd
MS
5662 corresponding fields of BITMASK set to 0b1111. The function
5663 returns return 1 if any of these fields in INSN reference the PC
5664 (also 0b1111, r15), else it returns 0. */
67255d04
RE
5665
5666static int
cca44b1b 5667insn_references_pc (uint32_t insn, uint32_t bitmask)
67255d04 5668{
cca44b1b 5669 uint32_t lowbit = 1;
67255d04 5670
cca44b1b
JB
5671 while (bitmask != 0)
5672 {
5673 uint32_t mask;
44e1a9eb 5674
cca44b1b
JB
5675 for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
5676 ;
67255d04 5677
cca44b1b
JB
5678 if (!lowbit)
5679 break;
67255d04 5680
cca44b1b 5681 mask = lowbit * 0xf;
67255d04 5682
cca44b1b
JB
5683 if ((insn & mask) == mask)
5684 return 1;
5685
5686 bitmask &= ~mask;
67255d04
RE
5687 }
5688
cca44b1b
JB
5689 return 0;
5690}
2af48f68 5691
cca44b1b
JB
5692/* The simplest copy function. Many instructions have the same effect no
5693 matter what address they are executed at: in those cases, use this. */
67255d04 5694
cca44b1b 5695static int
1152d984
SM
5696arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn, const char *iname,
5697 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 5698{
136821d9
SM
5699 displaced_debug_printf ("copying insn %.8lx, opcode/class '%s' unmodified",
5700 (unsigned long) insn, iname);
67255d04 5701
cca44b1b 5702 dsc->modinsn[0] = insn;
67255d04 5703
cca44b1b
JB
5704 return 0;
5705}
5706
34518530
YQ
5707static int
5708thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
5709 uint16_t insn2, const char *iname,
1152d984 5710 arm_displaced_step_copy_insn_closure *dsc)
34518530 5711{
136821d9
SM
5712 displaced_debug_printf ("copying insn %.4x %.4x, opcode/class '%s' "
5713 "unmodified", insn1, insn2, iname);
34518530
YQ
5714
5715 dsc->modinsn[0] = insn1;
5716 dsc->modinsn[1] = insn2;
5717 dsc->numinsns = 2;
5718
5719 return 0;
5720}
5721
5722/* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
5723 modification. */
5724static int
615234c1 5725thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, uint16_t insn,
34518530 5726 const char *iname,
1152d984 5727 arm_displaced_step_copy_insn_closure *dsc)
34518530 5728{
136821d9
SM
5729 displaced_debug_printf ("copying insn %.4x, opcode/class '%s' unmodified",
5730 insn, iname);
34518530
YQ
5731
5732 dsc->modinsn[0] = insn;
5733
5734 return 0;
5735}
5736
cca44b1b
JB
5737/* Preload instructions with immediate offset. */
5738
5739static void
1152d984
SM
5740cleanup_preload (struct gdbarch *gdbarch, regcache *regs,
5741 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
5742{
5743 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5744 if (!dsc->u.preload.immed)
5745 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5746}
5747
7ff120b4
YQ
5748static void
5749install_preload (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5750 arm_displaced_step_copy_insn_closure *dsc, unsigned int rn)
cca44b1b 5751{
cca44b1b 5752 ULONGEST rn_val;
cca44b1b
JB
5753 /* Preload instructions:
5754
5755 {pli/pld} [rn, #+/-imm]
5756 ->
5757 {pli/pld} [r0, #+/-imm]. */
5758
36073a92
YQ
5759 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5760 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 5761 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
cca44b1b
JB
5762 dsc->u.preload.immed = 1;
5763
cca44b1b 5764 dsc->cleanup = &cleanup_preload;
cca44b1b
JB
5765}
5766
cca44b1b 5767static int
7ff120b4 5768arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
1152d984 5769 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
5770{
5771 unsigned int rn = bits (insn, 16, 19);
cca44b1b 5772
7ff120b4
YQ
5773 if (!insn_references_pc (insn, 0x000f0000ul))
5774 return arm_copy_unmodified (gdbarch, insn, "preload", dsc);
cca44b1b 5775
136821d9 5776 displaced_debug_printf ("copying preload insn %.8lx", (unsigned long) insn);
cca44b1b 5777
7ff120b4
YQ
5778 dsc->modinsn[0] = insn & 0xfff0ffff;
5779
5780 install_preload (gdbarch, regs, dsc, rn);
5781
5782 return 0;
5783}
5784
34518530
YQ
5785static int
5786thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
1152d984 5787 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
5788{
5789 unsigned int rn = bits (insn1, 0, 3);
5790 unsigned int u_bit = bit (insn1, 7);
5791 int imm12 = bits (insn2, 0, 11);
5792 ULONGEST pc_val;
5793
5794 if (rn != ARM_PC_REGNUM)
5795 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);
5796
5797 /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
5798 PLD (literal) Encoding T1. */
136821d9
SM
5799 displaced_debug_printf ("copying pld/pli pc (0x%x) %c imm12 %.4x",
5800 (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
5801 imm12);
34518530
YQ
5802
5803 if (!u_bit)
5804 imm12 = -1 * imm12;
5805
5806 /* Rewrite instruction {pli/pld} PC imm12 into:
5807 Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12
5808
5809 {pli/pld} [r0, r1]
5810
5811 Cleanup: r0 <- tmp[0], r1 <- tmp[1]. */
5812
5813 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5814 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5815
5816 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
5817
5818 displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
5819 displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
5820 dsc->u.preload.immed = 0;
5821
5822 /* {pli/pld} [r0, r1] */
5823 dsc->modinsn[0] = insn1 & 0xfff0;
5824 dsc->modinsn[1] = 0xf001;
5825 dsc->numinsns = 2;
5826
5827 dsc->cleanup = &cleanup_preload;
5828 return 0;
5829}
5830
7ff120b4
YQ
5831/* Preload instructions with register offset. */
5832
5833static void
5834install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5835 arm_displaced_step_copy_insn_closure *dsc, unsigned int rn,
7ff120b4
YQ
5836 unsigned int rm)
5837{
5838 ULONGEST rn_val, rm_val;
5839
cca44b1b
JB
5840 /* Preload register-offset instructions:
5841
5842 {pli/pld} [rn, rm {, shift}]
5843 ->
5844 {pli/pld} [r0, r1 {, shift}]. */
5845
36073a92
YQ
5846 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5847 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5848 rn_val = displaced_read_reg (regs, dsc, rn);
5849 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5850 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5851 displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
cca44b1b
JB
5852 dsc->u.preload.immed = 0;
5853
cca44b1b 5854 dsc->cleanup = &cleanup_preload;
7ff120b4
YQ
5855}
5856
5857static int
5858arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
5859 struct regcache *regs,
1152d984 5860 arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
5861{
5862 unsigned int rn = bits (insn, 16, 19);
5863 unsigned int rm = bits (insn, 0, 3);
5864
5865
5866 if (!insn_references_pc (insn, 0x000f000ful))
5867 return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);
5868
136821d9
SM
5869 displaced_debug_printf ("copying preload insn %.8lx",
5870 (unsigned long) insn);
7ff120b4
YQ
5871
5872 dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
cca44b1b 5873
7ff120b4 5874 install_preload_reg (gdbarch, regs, dsc, rn, rm);
cca44b1b
JB
5875 return 0;
5876}
5877
5878/* Copy/cleanup coprocessor load and store instructions. */
5879
5880static void
6e39997a 5881cleanup_copro_load_store (struct gdbarch *gdbarch,
cca44b1b 5882 struct regcache *regs,
1152d984 5883 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 5884{
36073a92 5885 ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5886
5887 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5888
5889 if (dsc->u.ldst.writeback)
5890 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
5891}
5892
7ff120b4
YQ
5893static void
5894install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5895 arm_displaced_step_copy_insn_closure *dsc,
7ff120b4 5896 int writeback, unsigned int rn)
cca44b1b 5897{
cca44b1b 5898 ULONGEST rn_val;
cca44b1b 5899
cca44b1b
JB
5900 /* Coprocessor load/store instructions:
5901
5902 {stc/stc2} [<Rn>, #+/-imm] (and other immediate addressing modes)
5903 ->
5904 {stc/stc2} [r0, #+/-imm].
5905
5906 ldc/ldc2 are handled identically. */
5907
36073a92
YQ
5908 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5909 rn_val = displaced_read_reg (regs, dsc, rn);
2b16b2e3
YQ
5910 /* PC should be 4-byte aligned. */
5911 rn_val = rn_val & 0xfffffffc;
cca44b1b
JB
5912 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5913
7ff120b4 5914 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
5915 dsc->u.ldst.rn = rn;
5916
7ff120b4
YQ
5917 dsc->cleanup = &cleanup_copro_load_store;
5918}
5919
5920static int
5921arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
5922 struct regcache *regs,
1152d984 5923 arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
5924{
5925 unsigned int rn = bits (insn, 16, 19);
5926
5927 if (!insn_references_pc (insn, 0x000f0000ul))
5928 return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);
5929
136821d9
SM
5930 displaced_debug_printf ("copying coprocessor load/store insn %.8lx",
5931 (unsigned long) insn);
7ff120b4 5932
cca44b1b
JB
5933 dsc->modinsn[0] = insn & 0xfff0ffff;
5934
7ff120b4 5935 install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);
cca44b1b
JB
5936
5937 return 0;
5938}
5939
34518530
YQ
5940static int
5941thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
5942 uint16_t insn2, struct regcache *regs,
1152d984 5943 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
5944{
5945 unsigned int rn = bits (insn1, 0, 3);
5946
5947 if (rn != ARM_PC_REGNUM)
5948 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
5949 "copro load/store", dsc);
5950
136821d9
SM
5951 displaced_debug_printf ("copying coprocessor load/store insn %.4x%.4x",
5952 insn1, insn2);
34518530
YQ
5953
5954 dsc->modinsn[0] = insn1 & 0xfff0;
5955 dsc->modinsn[1] = insn2;
5956 dsc->numinsns = 2;
5957
5958 /* This function is called for copying instruction LDC/LDC2/VLDR, which
5959 doesn't support writeback, so pass 0. */
5960 install_copro_load_store (gdbarch, regs, dsc, 0, rn);
5961
5962 return 0;
5963}
5964
cca44b1b
JB
5965/* Clean up branch instructions (actually perform the branch, by setting
5966 PC). */
5967
5968static void
6e39997a 5969cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5970 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 5971{
36073a92 5972 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
5973 int branch_taken = condition_true (dsc->u.branch.cond, status);
5974 enum pc_write_style write_pc = dsc->u.branch.exchange
5975 ? BX_WRITE_PC : BRANCH_WRITE_PC;
5976
5977 if (!branch_taken)
5978 return;
5979
5980 if (dsc->u.branch.link)
5981 {
8c8dba6d 5982 /* The value of LR should be the next insn of current one. In order
85102364 5983 not to confuse logic handling later insn `bx lr', if current insn mode
8c8dba6d
YQ
5984 is Thumb, the bit 0 of LR value should be set to 1. */
5985 ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;
5986
5987 if (dsc->is_thumb)
5988 next_insn_addr |= 0x1;
5989
5990 displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
5991 CANNOT_WRITE_PC);
cca44b1b
JB
5992 }
5993
bf9f652a 5994 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
cca44b1b
JB
5995}
5996
5997/* Copy B/BL/BLX instructions with immediate destinations. */
5998
7ff120b4
YQ
5999static void
6000install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6001 arm_displaced_step_copy_insn_closure *dsc,
7ff120b4
YQ
6002 unsigned int cond, int exchange, int link, long offset)
6003{
6004 /* Implement "BL<cond> <label>" as:
6005
6006 Preparation: cond <- instruction condition
6007 Insn: mov r0, r0 (nop)
6008 Cleanup: if (condition true) { r14 <- pc; pc <- label }.
6009
6010 B<cond> similar, but don't set r14 in cleanup. */
6011
6012 dsc->u.branch.cond = cond;
6013 dsc->u.branch.link = link;
6014 dsc->u.branch.exchange = exchange;
6015
2b16b2e3
YQ
6016 dsc->u.branch.dest = dsc->insn_addr;
6017 if (link && exchange)
6018 /* For BLX, offset is computed from the Align (PC, 4). */
6019 dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;
6020
7ff120b4 6021 if (dsc->is_thumb)
2b16b2e3 6022 dsc->u.branch.dest += 4 + offset;
7ff120b4 6023 else
2b16b2e3 6024 dsc->u.branch.dest += 8 + offset;
7ff120b4
YQ
6025
6026 dsc->cleanup = &cleanup_branch;
6027}
cca44b1b 6028static int
7ff120b4 6029arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
1152d984 6030 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
6031{
6032 unsigned int cond = bits (insn, 28, 31);
6033 int exchange = (cond == 0xf);
6034 int link = exchange || bit (insn, 24);
cca44b1b
JB
6035 long offset;
6036
136821d9
SM
6037 displaced_debug_printf ("copying %s immediate insn %.8lx",
6038 (exchange) ? "blx" : (link) ? "bl" : "b",
6039 (unsigned long) insn);
cca44b1b
JB
6040 if (exchange)
6041 /* For BLX, set bit 0 of the destination. The cleanup_branch function will
6042 then arrange the switch into Thumb mode. */
6043 offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
6044 else
6045 offset = bits (insn, 0, 23) << 2;
6046
6047 if (bit (offset, 25))
6048 offset = offset | ~0x3ffffff;
6049
cca44b1b
JB
6050 dsc->modinsn[0] = ARM_NOP;
6051
7ff120b4 6052 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
cca44b1b
JB
6053 return 0;
6054}
6055
34518530
YQ
6056static int
6057thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
6058 uint16_t insn2, struct regcache *regs,
1152d984 6059 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
6060{
6061 int link = bit (insn2, 14);
6062 int exchange = link && !bit (insn2, 12);
6063 int cond = INST_AL;
6064 long offset = 0;
6065 int j1 = bit (insn2, 13);
6066 int j2 = bit (insn2, 11);
6067 int s = sbits (insn1, 10, 10);
6068 int i1 = !(j1 ^ bit (insn1, 10));
6069 int i2 = !(j2 ^ bit (insn1, 10));
6070
6071 if (!link && !exchange) /* B */
6072 {
6073 offset = (bits (insn2, 0, 10) << 1);
6074 if (bit (insn2, 12)) /* Encoding T4 */
6075 {
6076 offset |= (bits (insn1, 0, 9) << 12)
6077 | (i2 << 22)
6078 | (i1 << 23)
6079 | (s << 24);
6080 cond = INST_AL;
6081 }
6082 else /* Encoding T3 */
6083 {
6084 offset |= (bits (insn1, 0, 5) << 12)
6085 | (j1 << 18)
6086 | (j2 << 19)
6087 | (s << 20);
6088 cond = bits (insn1, 6, 9);
6089 }
6090 }
6091 else
6092 {
6093 offset = (bits (insn1, 0, 9) << 12);
6094 offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
6095 offset |= exchange ?
6096 (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
6097 }
6098
136821d9
SM
6099 displaced_debug_printf ("copying %s insn %.4x %.4x with offset %.8lx",
6100 link ? (exchange) ? "blx" : "bl" : "b",
6101 insn1, insn2, offset);
34518530
YQ
6102
6103 dsc->modinsn[0] = THUMB_NOP;
6104
6105 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
6106 return 0;
6107}
6108
6109/* Copy B Thumb instructions. */
6110static int
615234c1 6111thumb_copy_b (struct gdbarch *gdbarch, uint16_t insn,
1152d984 6112 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
6113{
6114 unsigned int cond = 0;
6115 int offset = 0;
6116 unsigned short bit_12_15 = bits (insn, 12, 15);
6117 CORE_ADDR from = dsc->insn_addr;
6118
6119 if (bit_12_15 == 0xd)
6120 {
6121 /* offset = SignExtend (imm8:0, 32) */
6122 offset = sbits ((insn << 1), 0, 8);
6123 cond = bits (insn, 8, 11);
6124 }
6125 else if (bit_12_15 == 0xe) /* Encoding T2 */
6126 {
6127 offset = sbits ((insn << 1), 0, 11);
6128 cond = INST_AL;
6129 }
6130
136821d9
SM
6131 displaced_debug_printf ("copying b immediate insn %.4x with offset %d",
6132 insn, offset);
34518530
YQ
6133
6134 dsc->u.branch.cond = cond;
6135 dsc->u.branch.link = 0;
6136 dsc->u.branch.exchange = 0;
6137 dsc->u.branch.dest = from + 4 + offset;
6138
6139 dsc->modinsn[0] = THUMB_NOP;
6140
6141 dsc->cleanup = &cleanup_branch;
6142
6143 return 0;
6144}
6145
cca44b1b
JB
6146/* Copy BX/BLX with register-specified destinations. */
6147
7ff120b4
YQ
6148static void
6149install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6150 arm_displaced_step_copy_insn_closure *dsc, int link,
7ff120b4 6151 unsigned int cond, unsigned int rm)
cca44b1b 6152{
cca44b1b
JB
6153 /* Implement {BX,BLX}<cond> <reg>" as:
6154
6155 Preparation: cond <- instruction condition
6156 Insn: mov r0, r0 (nop)
6157 Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
6158
6159 Don't set r14 in cleanup for BX. */
6160
36073a92 6161 dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6162
6163 dsc->u.branch.cond = cond;
6164 dsc->u.branch.link = link;
cca44b1b 6165
7ff120b4 6166 dsc->u.branch.exchange = 1;
cca44b1b
JB
6167
6168 dsc->cleanup = &cleanup_branch;
7ff120b4 6169}
cca44b1b 6170
7ff120b4
YQ
6171static int
6172arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
1152d984 6173 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
6174{
6175 unsigned int cond = bits (insn, 28, 31);
6176 /* BX: x12xxx1x
6177 BLX: x12xxx3x. */
6178 int link = bit (insn, 5);
6179 unsigned int rm = bits (insn, 0, 3);
6180
136821d9 6181 displaced_debug_printf ("copying insn %.8lx", (unsigned long) insn);
7ff120b4
YQ
6182
6183 dsc->modinsn[0] = ARM_NOP;
6184
6185 install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
cca44b1b
JB
6186 return 0;
6187}
6188
34518530
YQ
6189static int
6190thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
6191 struct regcache *regs,
1152d984 6192 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
6193{
6194 int link = bit (insn, 7);
6195 unsigned int rm = bits (insn, 3, 6);
6196
136821d9 6197 displaced_debug_printf ("copying insn %.4x", (unsigned short) insn);
34518530
YQ
6198
6199 dsc->modinsn[0] = THUMB_NOP;
6200
6201 install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);
6202
6203 return 0;
6204}
6205
6206
0963b4bd 6207/* Copy/cleanup arithmetic/logic instruction with immediate RHS. */
cca44b1b
JB
6208
6209static void
6e39997a 6210cleanup_alu_imm (struct gdbarch *gdbarch,
1152d984 6211 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6212{
36073a92 6213 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6214 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6215 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6216 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6217}
6218
6219static int
7ff120b4 6220arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
1152d984 6221 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
6222{
6223 unsigned int rn = bits (insn, 16, 19);
6224 unsigned int rd = bits (insn, 12, 15);
6225 unsigned int op = bits (insn, 21, 24);
6226 int is_mov = (op == 0xd);
6227 ULONGEST rd_val, rn_val;
cca44b1b
JB
6228
6229 if (!insn_references_pc (insn, 0x000ff000ul))
7ff120b4 6230 return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
cca44b1b 6231
136821d9
SM
6232 displaced_debug_printf ("copying immediate %s insn %.8lx",
6233 is_mov ? "move" : "ALU",
6234 (unsigned long) insn);
cca44b1b
JB
6235
6236 /* Instruction is of form:
6237
6238 <op><cond> rd, [rn,] #imm
6239
6240 Rewrite as:
6241
6242 Preparation: tmp1, tmp2 <- r0, r1;
6243 r0, r1 <- rd, rn
6244 Insn: <op><cond> r0, r1, #imm
6245 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6246 */
6247
36073a92
YQ
6248 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6249 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6250 rn_val = displaced_read_reg (regs, dsc, rn);
6251 rd_val = displaced_read_reg (regs, dsc, rd);
cca44b1b
JB
6252 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6253 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6254 dsc->rd = rd;
6255
6256 if (is_mov)
6257 dsc->modinsn[0] = insn & 0xfff00fff;
6258 else
6259 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
6260
6261 dsc->cleanup = &cleanup_alu_imm;
6262
6263 return 0;
6264}
6265
34518530
YQ
6266static int
6267thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
6268 uint16_t insn2, struct regcache *regs,
1152d984 6269 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
6270{
6271 unsigned int op = bits (insn1, 5, 8);
6272 unsigned int rn, rm, rd;
6273 ULONGEST rd_val, rn_val;
6274
6275 rn = bits (insn1, 0, 3); /* Rn */
6276 rm = bits (insn2, 0, 3); /* Rm */
6277 rd = bits (insn2, 8, 11); /* Rd */
6278
6279 /* This routine is only called for instruction MOV. */
6280 gdb_assert (op == 0x2 && rn == 0xf);
6281
6282 if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
6283 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);
6284
136821d9 6285 displaced_debug_printf ("copying reg %s insn %.4x%.4x", "ALU", insn1, insn2);
34518530
YQ
6286
6287 /* Instruction is of form:
6288
6289 <op><cond> rd, [rn,] #imm
6290
6291 Rewrite as:
6292
6293 Preparation: tmp1, tmp2 <- r0, r1;
6294 r0, r1 <- rd, rn
6295 Insn: <op><cond> r0, r1, #imm
6296 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6297 */
6298
6299 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6300 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6301 rn_val = displaced_read_reg (regs, dsc, rn);
6302 rd_val = displaced_read_reg (regs, dsc, rd);
6303 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6304 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6305 dsc->rd = rd;
6306
6307 dsc->modinsn[0] = insn1;
6308 dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
6309 dsc->numinsns = 2;
6310
6311 dsc->cleanup = &cleanup_alu_imm;
6312
6313 return 0;
6314}
6315
cca44b1b
JB
6316/* Copy/cleanup arithmetic/logic insns with register RHS. */
6317
6318static void
6e39997a 6319cleanup_alu_reg (struct gdbarch *gdbarch,
1152d984 6320 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
6321{
6322 ULONGEST rd_val;
6323 int i;
6324
36073a92 6325 rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6326
6327 for (i = 0; i < 3; i++)
6328 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6329
6330 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6331}
6332
7ff120b4
YQ
6333static void
6334install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6335 arm_displaced_step_copy_insn_closure *dsc,
7ff120b4 6336 unsigned int rd, unsigned int rn, unsigned int rm)
cca44b1b 6337{
cca44b1b 6338 ULONGEST rd_val, rn_val, rm_val;
cca44b1b 6339
cca44b1b
JB
6340 /* Instruction is of form:
6341
6342 <op><cond> rd, [rn,] rm [, <shift>]
6343
6344 Rewrite as:
6345
6346 Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
6347 r0, r1, r2 <- rd, rn, rm
ef713951 6348 Insn: <op><cond> r0, [r1,] r2 [, <shift>]
cca44b1b
JB
6349 Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
6350 */
6351
36073a92
YQ
6352 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6353 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6354 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6355 rd_val = displaced_read_reg (regs, dsc, rd);
6356 rn_val = displaced_read_reg (regs, dsc, rn);
6357 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6358 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6359 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6360 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6361 dsc->rd = rd;
6362
7ff120b4
YQ
6363 dsc->cleanup = &cleanup_alu_reg;
6364}
6365
6366static int
6367arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
1152d984 6368 arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
6369{
6370 unsigned int op = bits (insn, 21, 24);
6371 int is_mov = (op == 0xd);
6372
6373 if (!insn_references_pc (insn, 0x000ff00ful))
6374 return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);
6375
136821d9
SM
6376 displaced_debug_printf ("copying reg %s insn %.8lx",
6377 is_mov ? "move" : "ALU", (unsigned long) insn);
7ff120b4 6378
cca44b1b
JB
6379 if (is_mov)
6380 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
6381 else
6382 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
6383
7ff120b4
YQ
6384 install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
6385 bits (insn, 0, 3));
cca44b1b
JB
6386 return 0;
6387}
6388
34518530
YQ
6389static int
6390thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
6391 struct regcache *regs,
1152d984 6392 arm_displaced_step_copy_insn_closure *dsc)
34518530 6393{
ef713951 6394 unsigned rm, rd;
34518530 6395
ef713951
YQ
6396 rm = bits (insn, 3, 6);
6397 rd = (bit (insn, 7) << 3) | bits (insn, 0, 2);
34518530 6398
ef713951 6399 if (rd != ARM_PC_REGNUM && rm != ARM_PC_REGNUM)
34518530
YQ
6400 return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);
6401
136821d9 6402 displaced_debug_printf ("copying ALU reg insn %.4x", (unsigned short) insn);
34518530 6403
ef713951 6404 dsc->modinsn[0] = ((insn & 0xff00) | 0x10);
34518530 6405
ef713951 6406 install_alu_reg (gdbarch, regs, dsc, rd, rd, rm);
34518530
YQ
6407
6408 return 0;
6409}
6410
cca44b1b
JB
6411/* Cleanup/copy arithmetic/logic insns with shifted register RHS. */
6412
6413static void
6e39997a 6414cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
cca44b1b 6415 struct regcache *regs,
1152d984 6416 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6417{
36073a92 6418 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6419 int i;
6420
6421 for (i = 0; i < 4; i++)
6422 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6423
6424 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6425}
6426
7ff120b4
YQ
6427static void
6428install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6429 arm_displaced_step_copy_insn_closure *dsc,
7ff120b4
YQ
6430 unsigned int rd, unsigned int rn, unsigned int rm,
6431 unsigned rs)
cca44b1b 6432{
7ff120b4 6433 int i;
cca44b1b 6434 ULONGEST rd_val, rn_val, rm_val, rs_val;
cca44b1b 6435
cca44b1b
JB
6436 /* Instruction is of form:
6437
6438 <op><cond> rd, [rn,] rm, <shift> rs
6439
6440 Rewrite as:
6441
6442 Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
6443 r0, r1, r2, r3 <- rd, rn, rm, rs
6444 Insn: <op><cond> r0, r1, r2, <shift> r3
6445 Cleanup: tmp5 <- r0
6446 r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
6447 rd <- tmp5
6448 */
6449
6450 for (i = 0; i < 4; i++)
36073a92 6451 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b 6452
36073a92
YQ
6453 rd_val = displaced_read_reg (regs, dsc, rd);
6454 rn_val = displaced_read_reg (regs, dsc, rn);
6455 rm_val = displaced_read_reg (regs, dsc, rm);
6456 rs_val = displaced_read_reg (regs, dsc, rs);
cca44b1b
JB
6457 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6458 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6459 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6460 displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
6461 dsc->rd = rd;
7ff120b4
YQ
6462 dsc->cleanup = &cleanup_alu_shifted_reg;
6463}
6464
6465static int
6466arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
6467 struct regcache *regs,
1152d984 6468 arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
6469{
6470 unsigned int op = bits (insn, 21, 24);
6471 int is_mov = (op == 0xd);
6472 unsigned int rd, rn, rm, rs;
6473
6474 if (!insn_references_pc (insn, 0x000fff0ful))
6475 return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
6476
136821d9
SM
6477 displaced_debug_printf ("copying shifted reg %s insn %.8lx",
6478 is_mov ? "move" : "ALU",
6479 (unsigned long) insn);
7ff120b4
YQ
6480
6481 rn = bits (insn, 16, 19);
6482 rm = bits (insn, 0, 3);
6483 rs = bits (insn, 8, 11);
6484 rd = bits (insn, 12, 15);
cca44b1b
JB
6485
6486 if (is_mov)
6487 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
6488 else
6489 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
6490
7ff120b4 6491 install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);
cca44b1b
JB
6492
6493 return 0;
6494}
6495
6496/* Clean up load instructions. */
6497
6498static void
6e39997a 6499cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6500 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
6501{
6502 ULONGEST rt_val, rt_val2 = 0, rn_val;
cca44b1b 6503
36073a92 6504 rt_val = displaced_read_reg (regs, dsc, 0);
cca44b1b 6505 if (dsc->u.ldst.xfersize == 8)
36073a92
YQ
6506 rt_val2 = displaced_read_reg (regs, dsc, 1);
6507 rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
6508
6509 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6510 if (dsc->u.ldst.xfersize > 4)
6511 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6512 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6513 if (!dsc->u.ldst.immed)
6514 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6515
6516 /* Handle register writeback. */
6517 if (dsc->u.ldst.writeback)
6518 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6519 /* Put result in right place. */
6520 displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
6521 if (dsc->u.ldst.xfersize == 8)
6522 displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
6523}
6524
6525/* Clean up store instructions. */
6526
6527static void
6e39997a 6528cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6529 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6530{
36073a92 6531 ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
6532
6533 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6534 if (dsc->u.ldst.xfersize > 4)
6535 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6536 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6537 if (!dsc->u.ldst.immed)
6538 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6539 if (!dsc->u.ldst.restore_r4)
6540 displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
6541
6542 /* Writeback. */
6543 if (dsc->u.ldst.writeback)
6544 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6545}
6546
6547/* Copy "extra" load/store instructions. These are halfword/doubleword
6548 transfers, which have a different encoding to byte/word transfers. */
6549
6550static int
550dc4e2 6551arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unprivileged,
1152d984 6552 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
6553{
6554 unsigned int op1 = bits (insn, 20, 24);
6555 unsigned int op2 = bits (insn, 5, 6);
6556 unsigned int rt = bits (insn, 12, 15);
6557 unsigned int rn = bits (insn, 16, 19);
6558 unsigned int rm = bits (insn, 0, 3);
6559 char load[12] = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
6560 char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
6561 int immed = (op1 & 0x4) != 0;
6562 int opcode;
6563 ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
cca44b1b
JB
6564
6565 if (!insn_references_pc (insn, 0x000ff00ful))
7ff120b4 6566 return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);
cca44b1b 6567
136821d9
SM
6568 displaced_debug_printf ("copying %sextra load/store insn %.8lx",
6569 unprivileged ? "unprivileged " : "",
6570 (unsigned long) insn);
cca44b1b
JB
6571
6572 opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
6573
6574 if (opcode < 0)
f34652de 6575 internal_error (_("copy_extra_ld_st: instruction decode error"));
cca44b1b 6576
36073a92
YQ
6577 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6578 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6579 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 6580 if (!immed)
36073a92 6581 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 6582
36073a92 6583 rt_val = displaced_read_reg (regs, dsc, rt);
cca44b1b 6584 if (bytesize[opcode] == 8)
36073a92
YQ
6585 rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
6586 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 6587 if (!immed)
36073a92 6588 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6589
6590 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6591 if (bytesize[opcode] == 8)
6592 displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
6593 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6594 if (!immed)
6595 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
6596
6597 dsc->rd = rt;
6598 dsc->u.ldst.xfersize = bytesize[opcode];
6599 dsc->u.ldst.rn = rn;
6600 dsc->u.ldst.immed = immed;
6601 dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
6602 dsc->u.ldst.restore_r4 = 0;
6603
6604 if (immed)
6605 /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
6606 ->
6607 {ldr,str}<width><cond> r0, [r1,] [r2, #imm]. */
6608 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6609 else
6610 /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
6611 ->
6612 {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3]. */
6613 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6614
6615 dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
6616
6617 return 0;
6618}
6619
0f6f04ba 6620/* Copy byte/half word/word loads and stores. */
cca44b1b 6621
7ff120b4 6622static void
0f6f04ba 6623install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6624 arm_displaced_step_copy_insn_closure *dsc, int load,
0f6f04ba
YQ
6625 int immed, int writeback, int size, int usermode,
6626 int rt, int rm, int rn)
cca44b1b 6627{
cca44b1b 6628 ULONGEST rt_val, rn_val, rm_val = 0;
cca44b1b 6629
36073a92
YQ
6630 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6631 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 6632 if (!immed)
36073a92 6633 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 6634 if (!load)
36073a92 6635 dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);
cca44b1b 6636
36073a92
YQ
6637 rt_val = displaced_read_reg (regs, dsc, rt);
6638 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 6639 if (!immed)
36073a92 6640 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6641
6642 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6643 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6644 if (!immed)
6645 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
cca44b1b 6646 dsc->rd = rt;
0f6f04ba 6647 dsc->u.ldst.xfersize = size;
cca44b1b
JB
6648 dsc->u.ldst.rn = rn;
6649 dsc->u.ldst.immed = immed;
7ff120b4 6650 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
6651
6652 /* To write PC we can do:
6653
494e194e
YQ
6654 Before this sequence of instructions:
6655 r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
85102364 6656 r2 is the Rn value got from displaced_read_reg.
494e194e
YQ
6657
6658 Insn1: push {pc} Write address of STR instruction + offset on stack
6659 Insn2: pop {r4} Read it back from stack, r4 = addr(Insn1) + offset
6660 Insn3: sub r4, r4, pc r4 = addr(Insn1) + offset - pc
dda83cd7
SM
6661 = addr(Insn1) + offset - addr(Insn3) - 8
6662 = offset - 16
494e194e
YQ
6663 Insn4: add r4, r4, #8 r4 = offset - 8
6664 Insn5: add r0, r0, r4 r0 = from + 8 + offset - 8
dda83cd7 6665 = from + offset
494e194e 6666 Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])
cca44b1b
JB
6667
6668 Otherwise we don't know what value to write for PC, since the offset is
494e194e
YQ
6669 architecture-dependent (sometimes PC+8, sometimes PC+12). More details
6670 of this can be found in Section "Saving from r15" in
6671 http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */
cca44b1b 6672
7ff120b4
YQ
6673 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6674}
6675
34518530
YQ
6676
6677static int
6678thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
6679 uint16_t insn2, struct regcache *regs,
1152d984 6680 arm_displaced_step_copy_insn_closure *dsc, int size)
34518530
YQ
6681{
6682 unsigned int u_bit = bit (insn1, 7);
6683 unsigned int rt = bits (insn2, 12, 15);
6684 int imm12 = bits (insn2, 0, 11);
6685 ULONGEST pc_val;
6686
136821d9
SM
6687 displaced_debug_printf ("copying ldr pc (0x%x) R%d %c imm12 %.4x",
6688 (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
6689 imm12);
34518530
YQ
6690
6691 if (!u_bit)
6692 imm12 = -1 * imm12;
6693
6694 /* Rewrite instruction LDR Rt imm12 into:
6695
6696 Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12
6697
6698 LDR R0, R2, R3,
6699
6700 Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2]. */
6701
6702
6703 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6704 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6705 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6706
6707 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6708
6709 pc_val = pc_val & 0xfffffffc;
6710
6711 displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
6712 displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);
6713
6714 dsc->rd = rt;
6715
6716 dsc->u.ldst.xfersize = size;
6717 dsc->u.ldst.immed = 0;
6718 dsc->u.ldst.writeback = 0;
6719 dsc->u.ldst.restore_r4 = 0;
6720
6721 /* LDR R0, R2, R3 */
6722 dsc->modinsn[0] = 0xf852;
6723 dsc->modinsn[1] = 0x3;
6724 dsc->numinsns = 2;
6725
6726 dsc->cleanup = &cleanup_load;
6727
6728 return 0;
6729}
6730
6731static int
6732thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
6733 uint16_t insn2, struct regcache *regs,
1152d984 6734 arm_displaced_step_copy_insn_closure *dsc,
34518530
YQ
6735 int writeback, int immed)
6736{
6737 unsigned int rt = bits (insn2, 12, 15);
6738 unsigned int rn = bits (insn1, 0, 3);
6739 unsigned int rm = bits (insn2, 0, 3); /* Only valid if !immed. */
6740 /* In LDR (register), there is also a register Rm, which is not allowed to
6741 be PC, so we don't have to check it. */
6742
6743 if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
6744 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
6745 dsc);
6746
136821d9
SM
6747 displaced_debug_printf ("copying ldr r%d [r%d] insn %.4x%.4x",
6748 rt, rn, insn1, insn2);
34518530
YQ
6749
6750 install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
6751 0, rt, rm, rn);
6752
6753 dsc->u.ldst.restore_r4 = 0;
6754
6755 if (immed)
6756 /* ldr[b]<cond> rt, [rn, #imm], etc.
6757 ->
6758 ldr[b]<cond> r0, [r2, #imm]. */
6759 {
6760 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6761 dsc->modinsn[1] = insn2 & 0x0fff;
6762 }
6763 else
6764 /* ldr[b]<cond> rt, [rn, rm], etc.
6765 ->
6766 ldr[b]<cond> r0, [r2, r3]. */
6767 {
6768 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6769 dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
6770 }
6771
6772 dsc->numinsns = 2;
6773
6774 return 0;
6775}
6776
6777
7ff120b4
YQ
6778static int
6779arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
6780 struct regcache *regs,
1152d984 6781 arm_displaced_step_copy_insn_closure *dsc,
0f6f04ba 6782 int load, int size, int usermode)
7ff120b4
YQ
6783{
6784 int immed = !bit (insn, 25);
6785 int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
6786 unsigned int rt = bits (insn, 12, 15);
6787 unsigned int rn = bits (insn, 16, 19);
6788 unsigned int rm = bits (insn, 0, 3); /* Only valid if !immed. */
6789
6790 if (!insn_references_pc (insn, 0x000ff00ful))
6791 return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);
6792
136821d9
SM
6793 displaced_debug_printf ("copying %s%s r%d [r%d] insn %.8lx",
6794 load ? (size == 1 ? "ldrb" : "ldr")
6795 : (size == 1 ? "strb" : "str"),
6796 usermode ? "t" : "",
6797 rt, rn,
6798 (unsigned long) insn);
7ff120b4 6799
0f6f04ba
YQ
6800 install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
6801 usermode, rt, rm, rn);
7ff120b4 6802
bf9f652a 6803 if (load || rt != ARM_PC_REGNUM)
cca44b1b
JB
6804 {
6805 dsc->u.ldst.restore_r4 = 0;
6806
6807 if (immed)
6808 /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
6809 ->
6810 {ldr,str}[b]<cond> r0, [r2, #imm]. */
6811 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6812 else
6813 /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
6814 ->
6815 {ldr,str}[b]<cond> r0, [r2, r3]. */
6816 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6817 }
6818 else
6819 {
6820 /* We need to use r4 as scratch. Make sure it's restored afterwards. */
6821 dsc->u.ldst.restore_r4 = 1;
494e194e
YQ
6822 dsc->modinsn[0] = 0xe92d8000; /* push {pc} */
6823 dsc->modinsn[1] = 0xe8bd0010; /* pop {r4} */
cca44b1b
JB
6824 dsc->modinsn[2] = 0xe044400f; /* sub r4, r4, pc. */
6825 dsc->modinsn[3] = 0xe2844008; /* add r4, r4, #8. */
6826 dsc->modinsn[4] = 0xe0800004; /* add r0, r0, r4. */
6827
6828 /* As above. */
6829 if (immed)
6830 dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
6831 else
6832 dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
6833
cca44b1b
JB
6834 dsc->numinsns = 6;
6835 }
6836
6837 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6838
6839 return 0;
6840}
6841
6842/* Cleanup LDM instructions with fully-populated register list. This is an
6843 unfortunate corner case: it's impossible to implement correctly by modifying
6844 the instruction. The issue is as follows: we have an instruction,
6845
6846 ldm rN, {r0-r15}
6847
6848 which we must rewrite to avoid loading PC. A possible solution would be to
6849 do the load in two halves, something like (with suitable cleanup
6850 afterwards):
6851
6852 mov r8, rN
6853 ldm[id][ab] r8!, {r0-r7}
6854 str r7, <temp>
6855 ldm[id][ab] r8, {r7-r14}
6856 <bkpt>
6857
6858 but at present there's no suitable place for <temp>, since the scratch space
6859 is overwritten before the cleanup routine is called. For now, we simply
6860 emulate the instruction. */
6861
6862static void
6863cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6864 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6865{
cca44b1b
JB
6866 int inc = dsc->u.block.increment;
6867 int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
6868 int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
6869 uint32_t regmask = dsc->u.block.regmask;
6870 int regno = inc ? 0 : 15;
6871 CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
6872 int exception_return = dsc->u.block.load && dsc->u.block.user
6873 && (regmask & 0x8000) != 0;
36073a92 6874 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
6875 int do_transfer = condition_true (dsc->u.block.cond, status);
6876 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6877
6878 if (!do_transfer)
6879 return;
6880
6881 /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
6882 sensible we can do here. Complain loudly. */
6883 if (exception_return)
6884 error (_("Cannot single-step exception return"));
6885
6886 /* We don't handle any stores here for now. */
6887 gdb_assert (dsc->u.block.load != 0);
6888
136821d9
SM
6889 displaced_debug_printf ("emulating block transfer: %s %s %s",
6890 dsc->u.block.load ? "ldm" : "stm",
6891 dsc->u.block.increment ? "inc" : "dec",
6892 dsc->u.block.before ? "before" : "after");
cca44b1b
JB
6893
6894 while (regmask)
6895 {
6896 uint32_t memword;
6897
6898 if (inc)
bf9f652a 6899 while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
cca44b1b
JB
6900 regno++;
6901 else
6902 while (regno >= 0 && (regmask & (1 << regno)) == 0)
6903 regno--;
6904
6905 xfer_addr += bump_before;
6906
6907 memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
6908 displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
6909
6910 xfer_addr += bump_after;
6911
6912 regmask &= ~(1 << regno);
6913 }
6914
6915 if (dsc->u.block.writeback)
6916 displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
6917 CANNOT_WRITE_PC);
6918}
6919
6920/* Clean up an STM which included the PC in the register list. */
6921
6922static void
6923cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6924 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6925{
36073a92 6926 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b 6927 int store_executed = condition_true (dsc->u.block.cond, status);
5f661e03
SM
6928 CORE_ADDR pc_stored_at, transferred_regs
6929 = count_one_bits (dsc->u.block.regmask);
cca44b1b
JB
6930 CORE_ADDR stm_insn_addr;
6931 uint32_t pc_val;
6932 long offset;
6933 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6934
6935 /* If condition code fails, there's nothing else to do. */
6936 if (!store_executed)
6937 return;
6938
6939 if (dsc->u.block.increment)
6940 {
6941 pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
6942
6943 if (dsc->u.block.before)
6944 pc_stored_at += 4;
6945 }
6946 else
6947 {
6948 pc_stored_at = dsc->u.block.xfer_addr;
6949
6950 if (dsc->u.block.before)
6951 pc_stored_at -= 4;
6952 }
6953
6954 pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
6955 stm_insn_addr = dsc->scratch_base;
6956 offset = pc_val - stm_insn_addr;
6957
136821d9
SM
6958 displaced_debug_printf ("detected PC offset %.8lx for STM instruction",
6959 offset);
cca44b1b
JB
6960
6961 /* Rewrite the stored PC to the proper value for the non-displaced original
6962 instruction. */
6963 write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
6964 dsc->insn_addr + offset);
6965}
6966
6967/* Clean up an LDM which includes the PC in the register list. We clumped all
6968 the registers in the transferred list into a contiguous range r0...rX (to
6969 avoid loading PC directly and losing control of the debugged program), so we
6970 must undo that here. */
6971
6972static void
6e39997a 6973cleanup_block_load_pc (struct gdbarch *gdbarch,
cca44b1b 6974 struct regcache *regs,
1152d984 6975 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6976{
36073a92 6977 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
22e048c9 6978 int load_executed = condition_true (dsc->u.block.cond, status);
bf9f652a 6979 unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
5f661e03 6980 unsigned int regs_loaded = count_one_bits (mask);
cca44b1b
JB
6981 unsigned int num_to_shuffle = regs_loaded, clobbered;
6982
6983 /* The method employed here will fail if the register list is fully populated
6984 (we need to avoid loading PC directly). */
6985 gdb_assert (num_to_shuffle < 16);
6986
6987 if (!load_executed)
6988 return;
6989
6990 clobbered = (1 << num_to_shuffle) - 1;
6991
6992 while (num_to_shuffle > 0)
6993 {
6994 if ((mask & (1 << write_reg)) != 0)
6995 {
6996 unsigned int read_reg = num_to_shuffle - 1;
6997
6998 if (read_reg != write_reg)
6999 {
36073a92 7000 ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
cca44b1b 7001 displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
136821d9
SM
7002 displaced_debug_printf ("LDM: move loaded register r%d to r%d",
7003 read_reg, write_reg);
cca44b1b 7004 }
136821d9
SM
7005 else
7006 displaced_debug_printf ("LDM: register r%d already in the right "
7007 "place", write_reg);
cca44b1b
JB
7008
7009 clobbered &= ~(1 << write_reg);
7010
7011 num_to_shuffle--;
7012 }
7013
7014 write_reg--;
7015 }
7016
7017 /* Restore any registers we scribbled over. */
7018 for (write_reg = 0; clobbered != 0; write_reg++)
7019 {
7020 if ((clobbered & (1 << write_reg)) != 0)
7021 {
7022 displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
7023 CANNOT_WRITE_PC);
136821d9
SM
7024 displaced_debug_printf ("LDM: restored clobbered register r%d",
7025 write_reg);
cca44b1b
JB
7026 clobbered &= ~(1 << write_reg);
7027 }
7028 }
7029
7030 /* Perform register writeback manually. */
7031 if (dsc->u.block.writeback)
7032 {
7033 ULONGEST new_rn_val = dsc->u.block.xfer_addr;
7034
7035 if (dsc->u.block.increment)
7036 new_rn_val += regs_loaded * 4;
7037 else
7038 new_rn_val -= regs_loaded * 4;
7039
7040 displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
7041 CANNOT_WRITE_PC);
7042 }
7043}
7044
7045/* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
7046 in user-level code (in particular exception return, ldm rn, {...pc}^). */
7047
7048static int
7ff120b4
YQ
7049arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
7050 struct regcache *regs,
1152d984 7051 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7052{
7053 int load = bit (insn, 20);
7054 int user = bit (insn, 22);
7055 int increment = bit (insn, 23);
7056 int before = bit (insn, 24);
7057 int writeback = bit (insn, 21);
7058 int rn = bits (insn, 16, 19);
cca44b1b 7059
0963b4bd
MS
7060 /* Block transfers which don't mention PC can be run directly
7061 out-of-line. */
bf9f652a 7062 if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
7ff120b4 7063 return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
cca44b1b 7064
bf9f652a 7065 if (rn == ARM_PC_REGNUM)
cca44b1b 7066 {
0963b4bd
MS
7067 warning (_("displaced: Unpredictable LDM or STM with "
7068 "base register r15"));
7ff120b4 7069 return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
cca44b1b
JB
7070 }
7071
136821d9
SM
7072 displaced_debug_printf ("copying block transfer insn %.8lx",
7073 (unsigned long) insn);
cca44b1b 7074
36073a92 7075 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b
JB
7076 dsc->u.block.rn = rn;
7077
7078 dsc->u.block.load = load;
7079 dsc->u.block.user = user;
7080 dsc->u.block.increment = increment;
7081 dsc->u.block.before = before;
7082 dsc->u.block.writeback = writeback;
7083 dsc->u.block.cond = bits (insn, 28, 31);
7084
7085 dsc->u.block.regmask = insn & 0xffff;
7086
7087 if (load)
7088 {
7089 if ((insn & 0xffff) == 0xffff)
7090 {
7091 /* LDM with a fully-populated register list. This case is
7092 particularly tricky. Implement for now by fully emulating the
7093 instruction (which might not behave perfectly in all cases, but
7094 these instructions should be rare enough for that not to matter
7095 too much). */
7096 dsc->modinsn[0] = ARM_NOP;
7097
7098 dsc->cleanup = &cleanup_block_load_all;
7099 }
7100 else
7101 {
7102 /* LDM of a list of registers which includes PC. Implement by
7103 rewriting the list of registers to be transferred into a
7104 contiguous chunk r0...rX before doing the transfer, then shuffling
7105 registers into the correct places in the cleanup routine. */
7106 unsigned int regmask = insn & 0xffff;
5f661e03 7107 unsigned int num_in_list = count_one_bits (regmask), new_regmask;
bec2ab5a 7108 unsigned int i;
cca44b1b
JB
7109
7110 for (i = 0; i < num_in_list; i++)
36073a92 7111 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b
JB
7112
7113 /* Writeback makes things complicated. We need to avoid clobbering
7114 the base register with one of the registers in our modified
7115 register list, but just using a different register can't work in
7116 all cases, e.g.:
7117
7118 ldm r14!, {r0-r13,pc}
7119
7120 which would need to be rewritten as:
7121
7122 ldm rN!, {r0-r14}
7123
7124 but that can't work, because there's no free register for N.
7125
7126 Solve this by turning off the writeback bit, and emulating
7127 writeback manually in the cleanup routine. */
7128
7129 if (writeback)
7130 insn &= ~(1 << 21);
7131
7132 new_regmask = (1 << num_in_list) - 1;
7133
136821d9
SM
7134 displaced_debug_printf ("LDM r%d%s, {..., pc}: original reg list "
7135 "%.4x, modified list %.4x",
7136 rn, writeback ? "!" : "",
7137 (int) insn & 0xffff, new_regmask);
cca44b1b
JB
7138
7139 dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
7140
7141 dsc->cleanup = &cleanup_block_load_pc;
7142 }
7143 }
7144 else
7145 {
7146 /* STM of a list of registers which includes PC. Run the instruction
7147 as-is, but out of line: this will store the wrong value for the PC,
7148 so we must manually fix up the memory in the cleanup routine.
7149 Doing things this way has the advantage that we can auto-detect
7150 the offset of the PC write (which is architecture-dependent) in
7151 the cleanup routine. */
7152 dsc->modinsn[0] = insn;
7153
7154 dsc->cleanup = &cleanup_block_store_pc;
7155 }
7156
7157 return 0;
7158}
7159
34518530
YQ
7160static int
7161thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
7162 struct regcache *regs,
1152d984 7163 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 7164{
34518530
YQ
7165 int rn = bits (insn1, 0, 3);
7166 int load = bit (insn1, 4);
7167 int writeback = bit (insn1, 5);
cca44b1b 7168
34518530
YQ
7169 /* Block transfers which don't mention PC can be run directly
7170 out-of-line. */
7171 if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
7172 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);
7ff120b4 7173
34518530
YQ
7174 if (rn == ARM_PC_REGNUM)
7175 {
7176 warning (_("displaced: Unpredictable LDM or STM with "
7177 "base register r15"));
7178 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7179 "unpredictable ldm/stm", dsc);
7180 }
cca44b1b 7181
136821d9
SM
7182 displaced_debug_printf ("copying block transfer insn %.4x%.4x",
7183 insn1, insn2);
cca44b1b 7184
34518530
YQ
7185 /* Clear bit 13, since it should be always zero. */
7186 dsc->u.block.regmask = (insn2 & 0xdfff);
7187 dsc->u.block.rn = rn;
cca44b1b 7188
34518530
YQ
7189 dsc->u.block.load = load;
7190 dsc->u.block.user = 0;
7191 dsc->u.block.increment = bit (insn1, 7);
7192 dsc->u.block.before = bit (insn1, 8);
7193 dsc->u.block.writeback = writeback;
7194 dsc->u.block.cond = INST_AL;
7195 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b 7196
34518530
YQ
7197 if (load)
7198 {
7199 if (dsc->u.block.regmask == 0xffff)
7200 {
7201 /* This branch is impossible to happen. */
7202 gdb_assert (0);
7203 }
7204 else
7205 {
7206 unsigned int regmask = dsc->u.block.regmask;
5f661e03 7207 unsigned int num_in_list = count_one_bits (regmask), new_regmask;
bec2ab5a 7208 unsigned int i;
34518530
YQ
7209
7210 for (i = 0; i < num_in_list; i++)
7211 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7212
7213 if (writeback)
7214 insn1 &= ~(1 << 5);
7215
7216 new_regmask = (1 << num_in_list) - 1;
7217
136821d9
SM
7218 displaced_debug_printf ("LDM r%d%s, {..., pc}: original reg list "
7219 "%.4x, modified list %.4x",
7220 rn, writeback ? "!" : "",
7221 (int) dsc->u.block.regmask, new_regmask);
34518530
YQ
7222
7223 dsc->modinsn[0] = insn1;
7224 dsc->modinsn[1] = (new_regmask & 0xffff);
7225 dsc->numinsns = 2;
7226
7227 dsc->cleanup = &cleanup_block_load_pc;
7228 }
7229 }
7230 else
7231 {
7232 dsc->modinsn[0] = insn1;
7233 dsc->modinsn[1] = insn2;
7234 dsc->numinsns = 2;
7235 dsc->cleanup = &cleanup_block_store_pc;
7236 }
7237 return 0;
7238}
7239
d9311bfa
AT
7240/* Wrapper over read_memory_unsigned_integer for use in arm_get_next_pcs.
7241 This is used to avoid a dependency on BFD's bfd_endian enum. */
7242
7243ULONGEST
7244arm_get_next_pcs_read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
7245 int byte_order)
7246{
5f2dfcfd
AT
7247 return read_memory_unsigned_integer (memaddr, len,
7248 (enum bfd_endian) byte_order);
d9311bfa
AT
7249}
7250
7251/* Wrapper over gdbarch_addr_bits_remove for use in arm_get_next_pcs. */
7252
7253CORE_ADDR
7254arm_get_next_pcs_addr_bits_remove (struct arm_get_next_pcs *self,
7255 CORE_ADDR val)
7256{
e4e20d45
SM
7257 return gdbarch_addr_bits_remove
7258 (gdb::checked_static_cast<regcache *> (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{
e4e20d45 7274 return arm_is_thumb (gdb::checked_static_cast<regcache *> (self->regcache));
d9311bfa
AT
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);
d182e398 7603 [[fallthrough]];
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);
b4b9074d
TT
8920
8921 while (type->code () == TYPE_CODE_RANGE)
8922 type = check_typedef (type->target_type ());
cca44b1b 8923
78134374 8924 if (TYPE_CODE_FLT == type->code ())
cca44b1b 8925 {
345bd07c 8926 switch (tdep->fp_model)
cca44b1b
JB
8927 {
8928 case ARM_FLOAT_FPA:
8929 {
8930 /* The value is in register F0 in internal format. We need to
8931 extract the raw value and then convert it to the desired
8932 internal type. */
f0452268 8933 bfd_byte tmpbuf[ARM_FP_REGISTER_SIZE];
cca44b1b 8934
dca08e1f 8935 regs->cooked_read (ARM_F0_REGNUM, tmpbuf);
3b2ca824
UW
8936 target_float_convert (tmpbuf, arm_ext_type (gdbarch),
8937 valbuf, type);
cca44b1b
JB
8938 }
8939 break;
8940
8941 case ARM_FLOAT_SOFT_FPA:
8942 case ARM_FLOAT_SOFT_VFP:
8943 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8944 not using the VFP ABI code. */
8945 case ARM_FLOAT_VFP:
dca08e1f 8946 regs->cooked_read (ARM_A1_REGNUM, valbuf);
df86565b 8947 if (type->length () > 4)
f0452268
AH
8948 regs->cooked_read (ARM_A1_REGNUM + 1,
8949 valbuf + ARM_INT_REGISTER_SIZE);
cca44b1b
JB
8950 break;
8951
8952 default:
f34652de 8953 internal_error (_("arm_extract_return_value: "
0963b4bd 8954 "Floating point model not supported"));
cca44b1b
JB
8955 break;
8956 }
8957 }
78134374
SM
8958 else if (type->code () == TYPE_CODE_INT
8959 || type->code () == TYPE_CODE_CHAR
8960 || type->code () == TYPE_CODE_BOOL
8961 || type->code () == TYPE_CODE_PTR
aa006118 8962 || TYPE_IS_REFERENCE (type)
a6617193
JB
8963 || type->code () == TYPE_CODE_ENUM
8964 || is_fixed_point_type (type))
cca44b1b 8965 {
b021a221
MS
8966 /* If the type is a plain integer, then the access is
8967 straight-forward. Otherwise we have to play around a bit
8968 more. */
df86565b 8969 int len = type->length ();
cca44b1b
JB
8970 int regno = ARM_A1_REGNUM;
8971 ULONGEST tmp;
8972
8973 while (len > 0)
8974 {
8975 /* By using store_unsigned_integer we avoid having to do
8976 anything special for small big-endian values. */
8977 regcache_cooked_read_unsigned (regs, regno++, &tmp);
8978 store_unsigned_integer (valbuf,
f0452268
AH
8979 (len > ARM_INT_REGISTER_SIZE
8980 ? ARM_INT_REGISTER_SIZE : len),
cca44b1b 8981 byte_order, tmp);
f0452268
AH
8982 len -= ARM_INT_REGISTER_SIZE;
8983 valbuf += ARM_INT_REGISTER_SIZE;
cca44b1b
JB
8984 }
8985 }
8986 else
8987 {
8988 /* For a structure or union the behaviour is as if the value had
dda83cd7
SM
8989 been stored to word-aligned memory and then loaded into
8990 registers with 32-bit load instruction(s). */
df86565b 8991 int len = type->length ();
cca44b1b 8992 int regno = ARM_A1_REGNUM;
f0452268 8993 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
cca44b1b
JB
8994
8995 while (len > 0)
8996 {
dca08e1f 8997 regs->cooked_read (regno++, tmpbuf);
cca44b1b 8998 memcpy (valbuf, tmpbuf,
f0452268
AH
8999 len > ARM_INT_REGISTER_SIZE ? ARM_INT_REGISTER_SIZE : len);
9000 len -= ARM_INT_REGISTER_SIZE;
9001 valbuf += ARM_INT_REGISTER_SIZE;
cca44b1b
JB
9002 }
9003 }
9004}
9005
9006
9007/* Will a function return an aggregate type in memory or in a
9008 register? Return 0 if an aggregate type can be returned in a
9009 register, 1 if it must be returned in memory. */
9010
9011static int
9012arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
9013{
cca44b1b
JB
9014 enum type_code code;
9015
f168693b 9016 type = check_typedef (type);
cca44b1b 9017
b13c8ab2
YQ
9018 /* Simple, non-aggregate types (ie not including vectors and
9019 complex) are always returned in a register (or registers). */
78134374 9020 code = type->code ();
b13c8ab2
YQ
9021 if (TYPE_CODE_STRUCT != code && TYPE_CODE_UNION != code
9022 && TYPE_CODE_ARRAY != code && TYPE_CODE_COMPLEX != code)
9023 return 0;
cca44b1b 9024
911627e7
TT
9025 if (TYPE_HAS_DYNAMIC_LENGTH (type))
9026 return 1;
9027
bd63c870 9028 if (TYPE_CODE_ARRAY == code && type->is_vector ())
c4312b19
YQ
9029 {
9030 /* Vector values should be returned using ARM registers if they
9031 are not over 16 bytes. */
df86565b 9032 return (type->length () > 16);
c4312b19
YQ
9033 }
9034
08106042 9035 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
345bd07c 9036 if (tdep->arm_abi != ARM_ABI_APCS)
cca44b1b 9037 {
b13c8ab2
YQ
9038 /* The AAPCS says all aggregates not larger than a word are returned
9039 in a register. */
df86565b 9040 if (type->length () <= ARM_INT_REGISTER_SIZE
bab22d06 9041 && language_pass_by_reference (type).trivially_copyable)
b13c8ab2
YQ
9042 return 0;
9043
cca44b1b
JB
9044 return 1;
9045 }
b13c8ab2
YQ
9046 else
9047 {
9048 int nRc;
cca44b1b 9049
b13c8ab2
YQ
9050 /* All aggregate types that won't fit in a register must be returned
9051 in memory. */
df86565b 9052 if (type->length () > ARM_INT_REGISTER_SIZE
bab22d06 9053 || !language_pass_by_reference (type).trivially_copyable)
b13c8ab2 9054 return 1;
cca44b1b 9055
b13c8ab2
YQ
9056 /* In the ARM ABI, "integer" like aggregate types are returned in
9057 registers. For an aggregate type to be integer like, its size
f0452268 9058 must be less than or equal to ARM_INT_REGISTER_SIZE and the
b13c8ab2
YQ
9059 offset of each addressable subfield must be zero. Note that bit
9060 fields are not addressable, and all addressable subfields of
9061 unions always start at offset zero.
cca44b1b 9062
b13c8ab2
YQ
9063 This function is based on the behaviour of GCC 2.95.1.
9064 See: gcc/arm.c: arm_return_in_memory() for details.
cca44b1b 9065
b13c8ab2
YQ
9066 Note: All versions of GCC before GCC 2.95.2 do not set up the
9067 parameters correctly for a function returning the following
9068 structure: struct { float f;}; This should be returned in memory,
9069 not a register. Richard Earnshaw sent me a patch, but I do not
9070 know of any way to detect if a function like the above has been
9071 compiled with the correct calling convention. */
9072
9073 /* Assume all other aggregate types can be returned in a register.
9074 Run a check for structures, unions and arrays. */
9075 nRc = 0;
67255d04 9076
b13c8ab2
YQ
9077 if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
9078 {
9079 int i;
9080 /* Need to check if this struct/union is "integer" like. For
9081 this to be true, its size must be less than or equal to
f0452268 9082 ARM_INT_REGISTER_SIZE and the offset of each addressable
b13c8ab2
YQ
9083 subfield must be zero. Note that bit fields are not
9084 addressable, and unions always start at offset zero. If any
9085 of the subfields is a floating point type, the struct/union
9086 cannot be an integer type. */
9087
9088 /* For each field in the object, check:
9089 1) Is it FP? --> yes, nRc = 1;
9090 2) Is it addressable (bitpos != 0) and
9091 not packed (bitsize == 0)?
9092 --> yes, nRc = 1
9093 */
9094
1f704f76 9095 for (i = 0; i < type->num_fields (); i++)
67255d04 9096 {
b13c8ab2
YQ
9097 enum type_code field_type_code;
9098
9099 field_type_code
940da03e 9100 = check_typedef (type->field (i).type ())->code ();
b13c8ab2
YQ
9101
9102 /* Is it a floating point type field? */
9103 if (field_type_code == TYPE_CODE_FLT)
67255d04
RE
9104 {
9105 nRc = 1;
9106 break;
9107 }
b13c8ab2
YQ
9108
9109 /* If bitpos != 0, then we have to care about it. */
b610c045 9110 if (type->field (i).loc_bitpos () != 0)
b13c8ab2
YQ
9111 {
9112 /* Bitfields are not addressable. If the field bitsize is
9113 zero, then the field is not packed. Hence it cannot be
9114 a bitfield or any other packed type. */
3757d2d4 9115 if (type->field (i).bitsize () == 0)
b13c8ab2
YQ
9116 {
9117 nRc = 1;
9118 break;
9119 }
9120 }
67255d04
RE
9121 }
9122 }
67255d04 9123
b13c8ab2
YQ
9124 return nRc;
9125 }
67255d04
RE
9126}
9127
34e8f22d
RE
9128/* Write into appropriate registers a function return value of type
9129 TYPE, given in virtual format. */
9130
9131static void
b508a996 9132arm_store_return_value (struct type *type, struct regcache *regs,
5238cf52 9133 const gdb_byte *valbuf)
34e8f22d 9134{
ac7936df 9135 struct gdbarch *gdbarch = regs->arch ();
e17a4113 9136 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
be8626e0 9137
47231c30
TT
9138 while (type->code () == TYPE_CODE_RANGE)
9139 type = check_typedef (type->target_type ());
9140
78134374 9141 if (type->code () == TYPE_CODE_FLT)
34e8f22d 9142 {
f0452268 9143 gdb_byte buf[ARM_FP_REGISTER_SIZE];
08106042 9144 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
34e8f22d 9145
345bd07c 9146 switch (tdep->fp_model)
08216dd7
RE
9147 {
9148 case ARM_FLOAT_FPA:
9149
3b2ca824 9150 target_float_convert (valbuf, type, buf, arm_ext_type (gdbarch));
b66f5587 9151 regs->cooked_write (ARM_F0_REGNUM, buf);
08216dd7
RE
9152 break;
9153
fd50bc42 9154 case ARM_FLOAT_SOFT_FPA:
08216dd7 9155 case ARM_FLOAT_SOFT_VFP:
90445bd3
DJ
9156 /* ARM_FLOAT_VFP can arise if this is a variadic function so
9157 not using the VFP ABI code. */
9158 case ARM_FLOAT_VFP:
b66f5587 9159 regs->cooked_write (ARM_A1_REGNUM, valbuf);
df86565b 9160 if (type->length () > 4)
f0452268
AH
9161 regs->cooked_write (ARM_A1_REGNUM + 1,
9162 valbuf + ARM_INT_REGISTER_SIZE);
08216dd7
RE
9163 break;
9164
9165 default:
f34652de 9166 internal_error (_("arm_store_return_value: Floating "
9b20d036 9167 "point model not supported"));
08216dd7
RE
9168 break;
9169 }
34e8f22d 9170 }
78134374
SM
9171 else if (type->code () == TYPE_CODE_INT
9172 || type->code () == TYPE_CODE_CHAR
9173 || type->code () == TYPE_CODE_BOOL
9174 || type->code () == TYPE_CODE_PTR
aa006118 9175 || TYPE_IS_REFERENCE (type)
a23cf0c2
TT
9176 || type->code () == TYPE_CODE_ENUM
9177 || is_fixed_point_type (type))
b508a996 9178 {
df86565b 9179 if (type->length () <= 4)
b508a996
RE
9180 {
9181 /* Values of one word or less are zero/sign-extended and
9182 returned in r0. */
f0452268 9183 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
b508a996 9184
a23cf0c2
TT
9185 if (is_fixed_point_type (type))
9186 {
9187 gdb_mpz unscaled;
9188 unscaled.read (gdb::make_array_view (valbuf, type->length ()),
9189 byte_order, type->is_unsigned ());
9190 unscaled.write (gdb::make_array_view (tmpbuf, sizeof (tmpbuf)),
9191 byte_order, type->is_unsigned ());
9192 }
9193 else
9194 {
9195 LONGEST val = unpack_long (type, valbuf);
9196 store_signed_integer (tmpbuf, ARM_INT_REGISTER_SIZE, byte_order, val);
9197 }
b66f5587 9198 regs->cooked_write (ARM_A1_REGNUM, tmpbuf);
b508a996
RE
9199 }
9200 else
9201 {
9202 /* Integral values greater than one word are stored in consecutive
9203 registers starting with r0. This will always be a multiple of
9204 the regiser size. */
df86565b 9205 int len = type->length ();
b508a996
RE
9206 int regno = ARM_A1_REGNUM;
9207
9208 while (len > 0)
9209 {
b66f5587 9210 regs->cooked_write (regno++, valbuf);
f0452268
AH
9211 len -= ARM_INT_REGISTER_SIZE;
9212 valbuf += ARM_INT_REGISTER_SIZE;
b508a996
RE
9213 }
9214 }
9215 }
34e8f22d 9216 else
b508a996
RE
9217 {
9218 /* For a structure or union the behaviour is as if the value had
dda83cd7
SM
9219 been stored to word-aligned memory and then loaded into
9220 registers with 32-bit load instruction(s). */
df86565b 9221 int len = type->length ();
b508a996 9222 int regno = ARM_A1_REGNUM;
f0452268 9223 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
b508a996
RE
9224
9225 while (len > 0)
9226 {
9227 memcpy (tmpbuf, valbuf,
f0452268 9228 len > ARM_INT_REGISTER_SIZE ? ARM_INT_REGISTER_SIZE : len);
b66f5587 9229 regs->cooked_write (regno++, tmpbuf);
f0452268
AH
9230 len -= ARM_INT_REGISTER_SIZE;
9231 valbuf += ARM_INT_REGISTER_SIZE;
b508a996
RE
9232 }
9233 }
34e8f22d
RE
9234}
9235
2af48f68
PB
9236
9237/* Handle function return values. */
9238
9239static enum return_value_convention
6a3a010b 9240arm_return_value (struct gdbarch *gdbarch, struct value *function,
c055b101 9241 struct type *valtype, struct regcache *regcache,
5cb0f2d5 9242 struct value **read_value, const gdb_byte *writebuf)
2af48f68 9243{
08106042 9244 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
d0c97917 9245 struct type *func_type = function ? function->type () : NULL;
90445bd3
DJ
9246 enum arm_vfp_cprc_base_type vfp_base_type;
9247 int vfp_base_count;
9248
9249 if (arm_vfp_abi_for_function (gdbarch, func_type)
9250 && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
9251 {
9252 int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
9253 int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
9254 int i;
911627e7
TT
9255
9256 gdb_byte *readbuf = nullptr;
9257 if (read_value != nullptr)
9258 {
317c3ed9 9259 *read_value = value::allocate (valtype);
bbe912ba 9260 readbuf = (*read_value)->contents_raw ().data ();
911627e7
TT
9261 }
9262
90445bd3
DJ
9263 for (i = 0; i < vfp_base_count; i++)
9264 {
58d6951d
DJ
9265 if (reg_char == 'q')
9266 {
9267 if (writebuf)
9268 arm_neon_quad_write (gdbarch, regcache, i,
9269 writebuf + i * unit_length);
9270
9271 if (readbuf)
9272 arm_neon_quad_read (gdbarch, regcache, i,
9273 readbuf + i * unit_length);
9274 }
9275 else
9276 {
9277 char name_buf[4];
9278 int regnum;
9279
8c042590 9280 xsnprintf (name_buf, sizeof (name_buf), "%c%d", reg_char, i);
58d6951d
DJ
9281 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9282 strlen (name_buf));
9283 if (writebuf)
b66f5587 9284 regcache->cooked_write (regnum, writebuf + i * unit_length);
58d6951d 9285 if (readbuf)
dca08e1f 9286 regcache->cooked_read (regnum, readbuf + i * unit_length);
58d6951d 9287 }
90445bd3
DJ
9288 }
9289 return RETURN_VALUE_REGISTER_CONVENTION;
9290 }
7c00367c 9291
78134374
SM
9292 if (valtype->code () == TYPE_CODE_STRUCT
9293 || valtype->code () == TYPE_CODE_UNION
9294 || valtype->code () == TYPE_CODE_ARRAY)
2af48f68 9295 {
bab22d06
LM
9296 /* From the AAPCS document:
9297
9298 Result return:
9299
9300 A Composite Type larger than 4 bytes, or whose size cannot be
9301 determined statically by both caller and callee, is stored in memory
9302 at an address passed as an extra argument when the function was
9303 called (Parameter Passing, rule A.4). The memory to be used for the
9304 result may be modified at any point during the function call.
9305
9306 Parameter Passing:
9307
9308 A.4: If the subroutine is a function that returns a result in memory,
9309 then the address for the result is placed in r0 and the NCRN is set
9310 to r1. */
7c00367c
MK
9311 if (tdep->struct_return == pcc_struct_return
9312 || arm_return_in_memory (gdbarch, valtype))
bab22d06 9313 {
911627e7 9314 if (read_value != nullptr)
bab22d06
LM
9315 {
9316 CORE_ADDR addr;
9317
9318 regcache->cooked_read (ARM_A1_REGNUM, &addr);
911627e7 9319 *read_value = value_at_non_lval (valtype, addr);
bab22d06
LM
9320 }
9321 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
9322 }
2af48f68 9323 }
78134374 9324 else if (valtype->code () == TYPE_CODE_COMPLEX)
b13c8ab2
YQ
9325 {
9326 if (arm_return_in_memory (gdbarch, valtype))
9327 return RETURN_VALUE_STRUCT_CONVENTION;
9328 }
7052e42c 9329
2af48f68
PB
9330 if (writebuf)
9331 arm_store_return_value (valtype, regcache, writebuf);
9332
911627e7
TT
9333 if (read_value != nullptr)
9334 {
317c3ed9 9335 *read_value = value::allocate (valtype);
bbe912ba 9336 gdb_byte *readbuf = (*read_value)->contents_raw ().data ();
911627e7
TT
9337 arm_extract_return_value (valtype, regcache, readbuf);
9338 }
2af48f68
PB
9339
9340 return RETURN_VALUE_REGISTER_CONVENTION;
9341}
9342
9343
9df628e0 9344static int
8480a37e 9345arm_get_longjmp_target (const frame_info_ptr &frame, CORE_ADDR *pc)
9df628e0 9346{
e17a4113 9347 struct gdbarch *gdbarch = get_frame_arch (frame);
08106042 9348 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
e17a4113 9349 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
9df628e0 9350 CORE_ADDR jb_addr;
f0452268 9351 gdb_byte buf[ARM_INT_REGISTER_SIZE];
9df628e0 9352
60ade65d 9353 jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
9df628e0
RE
9354
9355 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
f0452268 9356 ARM_INT_REGISTER_SIZE))
9df628e0
RE
9357 return 0;
9358
f0452268 9359 *pc = extract_unsigned_integer (buf, ARM_INT_REGISTER_SIZE, byte_order);
9df628e0
RE
9360 return 1;
9361}
40eadf04
SP
9362/* A call to cmse secure entry function "foo" at "a" is modified by
9363 GNU ld as "b".
9364 a) bl xxxx <foo>
9365
9366 <foo>
9367 xxxx:
9368
9369 b) bl yyyy <__acle_se_foo>
9370
9371 section .gnu.sgstubs:
9372 <foo>
9373 yyyy: sg // secure gateway
9374 b.w xxxx <__acle_se_foo> // original_branch_dest
9375
9376 <__acle_se_foo>
9377 xxxx:
9378
9379 When the control at "b", the pc contains "yyyy" (sg address) which is a
9380 trampoline and does not exist in source code. This function returns the
9381 target pc "xxxx". For more details please refer to section 5.4
9382 (Entry functions) and section 3.4.4 (C level development flow of secure code)
9383 of "armv8-m-security-extensions-requirements-on-development-tools-engineering-specification"
9384 document on www.developer.arm.com. */
9385
9386static CORE_ADDR
9387arm_skip_cmse_entry (CORE_ADDR pc, const char *name, struct objfile *objfile)
9388{
9389 int target_len = strlen (name) + strlen ("__acle_se_") + 1;
9390 char *target_name = (char *) alloca (target_len);
9391 xsnprintf (target_name, target_len, "%s%s", "__acle_se_", name);
9392
9393 struct bound_minimal_symbol minsym
9394 = lookup_minimal_symbol (target_name, NULL, objfile);
9395
9396 if (minsym.minsym != nullptr)
4aeddc50 9397 return minsym.value_address ();
40eadf04
SP
9398
9399 return 0;
9400}
9401
9402/* Return true when SEC points to ".gnu.sgstubs" section. */
9403
9404static bool
9405arm_is_sgstubs_section (struct obj_section *sec)
9406{
9407 return (sec != nullptr
9408 && sec->the_bfd_section != nullptr
9409 && sec->the_bfd_section->name != nullptr
9410 && streq (sec->the_bfd_section->name, ".gnu.sgstubs"));
9411}
9df628e0 9412
faa95490
DJ
9413/* Recognize GCC and GNU ld's trampolines. If we are in a trampoline,
9414 return the target PC. Otherwise return 0. */
c906108c
SS
9415
9416CORE_ADDR
8480a37e 9417arm_skip_stub (const frame_info_ptr &frame, CORE_ADDR pc)
c906108c 9418{
2c02bd72 9419 const char *name;
faa95490 9420 int namelen;
c906108c
SS
9421 CORE_ADDR start_addr;
9422
9423 /* Find the starting address and name of the function containing the PC. */
9424 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
80d8d390
YQ
9425 {
9426 /* Trampoline 'bx reg' doesn't belong to any functions. Do the
9427 check here. */
9428 start_addr = arm_skip_bx_reg (frame, pc);
9429 if (start_addr != 0)
9430 return start_addr;
9431
9432 return 0;
9433 }
c906108c 9434
faa95490
DJ
9435 /* If PC is in a Thumb call or return stub, return the address of the
9436 target PC, which is in a register. The thunk functions are called
9437 _call_via_xx, where x is the register name. The possible names
3d8d5e79
DJ
9438 are r0-r9, sl, fp, ip, sp, and lr. ARM RealView has similar
9439 functions, named __ARM_call_via_r[0-7]. */
61012eef
GB
9440 if (startswith (name, "_call_via_")
9441 || startswith (name, "__ARM_call_via_"))
c906108c 9442 {
ed9a39eb 9443 /* Use the name suffix to determine which register contains the
dda83cd7 9444 target PC. */
a121b7c1 9445 static const char *table[15] =
c5aa993b
JM
9446 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
9447 "r8", "r9", "sl", "fp", "ip", "sp", "lr"
9448 };
c906108c 9449 int regno;
faa95490 9450 int offset = strlen (name) - 2;
c906108c
SS
9451
9452 for (regno = 0; regno <= 14; regno++)
faa95490 9453 if (strcmp (&name[offset], table[regno]) == 0)
52f729a7 9454 return get_frame_register_unsigned (frame, regno);
c906108c 9455 }
ed9a39eb 9456
faa95490
DJ
9457 /* GNU ld generates __foo_from_arm or __foo_from_thumb for
9458 non-interworking calls to foo. We could decode the stubs
9459 to find the target but it's easier to use the symbol table. */
9460 namelen = strlen (name);
9461 if (name[0] == '_' && name[1] == '_'
9462 && ((namelen > 2 + strlen ("_from_thumb")
61012eef 9463 && startswith (name + namelen - strlen ("_from_thumb"), "_from_thumb"))
faa95490 9464 || (namelen > 2 + strlen ("_from_arm")
61012eef 9465 && startswith (name + namelen - strlen ("_from_arm"), "_from_arm"))))
faa95490
DJ
9466 {
9467 char *target_name;
9468 int target_len = namelen - 2;
3b7344d5 9469 struct bound_minimal_symbol minsym;
faa95490
DJ
9470 struct objfile *objfile;
9471 struct obj_section *sec;
9472
9473 if (name[namelen - 1] == 'b')
9474 target_len -= strlen ("_from_thumb");
9475 else
9476 target_len -= strlen ("_from_arm");
9477
224c3ddb 9478 target_name = (char *) alloca (target_len + 1);
faa95490
DJ
9479 memcpy (target_name, name + 2, target_len);
9480 target_name[target_len] = '\0';
9481
9482 sec = find_pc_section (pc);
9483 objfile = (sec == NULL) ? NULL : sec->objfile;
9484 minsym = lookup_minimal_symbol (target_name, NULL, objfile);
3b7344d5 9485 if (minsym.minsym != NULL)
4aeddc50 9486 return minsym.value_address ();
faa95490
DJ
9487 else
9488 return 0;
9489 }
9490
40eadf04
SP
9491 struct obj_section *section = find_pc_section (pc);
9492
9493 /* Check whether SECTION points to the ".gnu.sgstubs" section. */
9494 if (arm_is_sgstubs_section (section))
9495 return arm_skip_cmse_entry (pc, name, section->objfile);
9496
c5aa993b 9497 return 0; /* not a stub */
c906108c
SS
9498}
9499
28e97307
DJ
9500static void
9501arm_update_current_architecture (void)
fd50bc42 9502{
28e97307 9503 /* If the current architecture is not ARM, we have nothing to do. */
99d9c3b9
SM
9504 gdbarch *arch = current_inferior ()->arch ();
9505 if (gdbarch_bfd_arch_info (arch)->arch != bfd_arch_arm)
28e97307 9506 return;
fd50bc42 9507
28e97307 9508 /* Update the architecture. */
b447dd03 9509 gdbarch_info info;
28e97307 9510 if (!gdbarch_update_p (info))
f34652de 9511 internal_error (_("could not update architecture"));
fd50bc42
RE
9512}
9513
9514static void
eb4c3f4a 9515set_fp_model_sfunc (const char *args, int from_tty,
fd50bc42
RE
9516 struct cmd_list_element *c)
9517{
570dc176 9518 int fp_model;
fd50bc42
RE
9519
9520 for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
9521 if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
9522 {
aead7601 9523 arm_fp_model = (enum arm_float_model) fp_model;
fd50bc42
RE
9524 break;
9525 }
9526
9527 if (fp_model == ARM_FLOAT_LAST)
f34652de 9528 internal_error (_("Invalid fp model accepted: %s."),
fd50bc42
RE
9529 current_fp_model);
9530
28e97307 9531 arm_update_current_architecture ();
fd50bc42
RE
9532}
9533
9534static void
08546159
AC
9535show_fp_model (struct ui_file *file, int from_tty,
9536 struct cmd_list_element *c, const char *value)
fd50bc42 9537{
99d9c3b9 9538 gdbarch *arch = current_inferior ()->arch ();
28e97307 9539 if (arm_fp_model == ARM_FLOAT_AUTO
99d9c3b9 9540 && gdbarch_bfd_arch_info (arch)->arch == bfd_arch_arm)
6dff2a6f 9541 {
99d9c3b9 9542 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (arch);
6dff2a6f
AB
9543
9544 gdb_printf (file, _("\
28e97307 9545The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
6dff2a6f
AB
9546 fp_model_strings[tdep->fp_model]);
9547 }
28e97307 9548 else
6cb06a8c 9549 gdb_printf (file, _("\
28e97307 9550The current ARM floating point model is \"%s\".\n"),
6cb06a8c 9551 fp_model_strings[arm_fp_model]);
28e97307
DJ
9552}
9553
9554static void
eb4c3f4a 9555arm_set_abi (const char *args, int from_tty,
28e97307
DJ
9556 struct cmd_list_element *c)
9557{
570dc176 9558 int arm_abi;
28e97307
DJ
9559
9560 for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
9561 if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
9562 {
aead7601 9563 arm_abi_global = (enum arm_abi_kind) arm_abi;
28e97307
DJ
9564 break;
9565 }
9566
9567 if (arm_abi == ARM_ABI_LAST)
f34652de 9568 internal_error (_("Invalid ABI accepted: %s."),
28e97307
DJ
9569 arm_abi_string);
9570
9571 arm_update_current_architecture ();
9572}
9573
9574static void
9575arm_show_abi (struct ui_file *file, int from_tty,
9576 struct cmd_list_element *c, const char *value)
9577{
99d9c3b9 9578 gdbarch *arch = current_inferior ()->arch ();
28e97307 9579 if (arm_abi_global == ARM_ABI_AUTO
99d9c3b9 9580 && gdbarch_bfd_arch_info (arch)->arch == bfd_arch_arm)
6dff2a6f 9581 {
99d9c3b9 9582 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (arch);
6dff2a6f
AB
9583
9584 gdb_printf (file, _("\
28e97307 9585The current ARM ABI is \"auto\" (currently \"%s\").\n"),
6dff2a6f
AB
9586 arm_abi_strings[tdep->arm_abi]);
9587 }
28e97307 9588 else
6cb06a8c
TT
9589 gdb_printf (file, _("The current ARM ABI is \"%s\".\n"),
9590 arm_abi_string);
fd50bc42
RE
9591}
9592
0428b8f5
DJ
9593static void
9594arm_show_fallback_mode (struct ui_file *file, int from_tty,
9595 struct cmd_list_element *c, const char *value)
9596{
6cb06a8c
TT
9597 gdb_printf (file,
9598 _("The current execution mode assumed "
9599 "(when symbols are unavailable) is \"%s\".\n"),
9600 arm_fallback_mode_string);
0428b8f5
DJ
9601}
9602
9603static void
9604arm_show_force_mode (struct ui_file *file, int from_tty,
9605 struct cmd_list_element *c, const char *value)
9606{
6cb06a8c
TT
9607 gdb_printf (file,
9608 _("The current execution mode assumed "
9609 "(even when symbols are available) is \"%s\".\n"),
9610 arm_force_mode_string);
0428b8f5
DJ
9611}
9612
ef273377
CL
9613static void
9614arm_show_unwind_secure_frames (struct ui_file *file, int from_tty,
9615 struct cmd_list_element *c, const char *value)
9616{
9617 gdb_printf (file,
9618 _("Usage of non-secure to secure exception stack unwinding is %s.\n"),
9619 arm_unwind_secure_frames ? "on" : "off");
9620}
9621
afd7eef0
RE
9622/* If the user changes the register disassembly style used for info
9623 register and other commands, we have to also switch the style used
9624 in opcodes for disassembly output. This function is run in the "set
9625 arm disassembly" command, and does that. */
bc90b915
FN
9626
9627static void
eb4c3f4a 9628set_disassembly_style_sfunc (const char *args, int from_tty,
65b48a81 9629 struct cmd_list_element *c)
bc90b915 9630{
65b48a81
PB
9631 /* Convert the short style name into the long style name (eg, reg-names-*)
9632 before calling the generic set_disassembler_options() function. */
9633 std::string long_name = std::string ("reg-names-") + disassembly_style;
9634 set_disassembler_options (&long_name[0]);
9635}
9636
9637static void
9638show_disassembly_style_sfunc (struct ui_file *file, int from_tty,
9639 struct cmd_list_element *c, const char *value)
9640{
9641 struct gdbarch *gdbarch = get_current_arch ();
9f1c9448 9642 const char *options = get_disassembler_options (gdbarch);
65b48a81
PB
9643 const char *style = "";
9644 int len = 0;
f995bbe8 9645 const char *opt;
65b48a81
PB
9646
9647 FOR_EACH_DISASSEMBLER_OPTION (opt, options)
08dedd66 9648 if (startswith (opt, "reg-names-"))
65b48a81
PB
9649 {
9650 style = &opt[strlen ("reg-names-")];
9651 len = strcspn (style, ",");
9652 }
9653
6cb06a8c 9654 gdb_printf (file, "The disassembly style is \"%.*s\".\n", len, style);
bc90b915
FN
9655}
9656\f
966fbf70 9657/* Return the ARM register name corresponding to register I. */
a208b0cb 9658static const char *
d93859e2 9659arm_register_name (struct gdbarch *gdbarch, int i)
966fbf70 9660{
08106042 9661 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d 9662
ecbf5d4f 9663 if (is_s_pseudo (gdbarch, i))
58d6951d 9664 {
ecbf5d4f 9665 static const char *const s_pseudo_names[] = {
58d6951d
DJ
9666 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
9667 "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
9668 "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
9669 "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
9670 };
9671
ecbf5d4f 9672 return s_pseudo_names[i - tdep->s_pseudo_base];
58d6951d
DJ
9673 }
9674
ecbf5d4f 9675 if (is_q_pseudo (gdbarch, i))
58d6951d 9676 {
ecbf5d4f 9677 static const char *const q_pseudo_names[] = {
58d6951d
DJ
9678 "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
9679 "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
9680 };
9681
ecbf5d4f 9682 return q_pseudo_names[i - tdep->q_pseudo_base];
58d6951d
DJ
9683 }
9684
ae66a8f1
SP
9685 if (is_mve_pseudo (gdbarch, i))
9686 return "p0";
9687
a01567f4
LM
9688 /* RA_AUTH_CODE is used for unwinding only. Do not assign it a name. */
9689 if (is_pacbti_pseudo (gdbarch, i))
9690 return "";
9691
ff6f572f
DJ
9692 if (i >= ARRAY_SIZE (arm_register_names))
9693 /* These registers are only supported on targets which supply
9694 an XML description. */
9695 return "";
9696
ecbf5d4f 9697 /* Non-pseudo registers. */
966fbf70
RE
9698 return arm_register_names[i];
9699}
9700
082fc60d
RE
9701/* Test whether the coff symbol specific value corresponds to a Thumb
9702 function. */
9703
9704static int
9705coff_sym_is_thumb (int val)
9706{
f8bf5763
PM
9707 return (val == C_THUMBEXT
9708 || val == C_THUMBSTAT
9709 || val == C_THUMBEXTFUNC
9710 || val == C_THUMBSTATFUNC
9711 || val == C_THUMBLABEL);
082fc60d
RE
9712}
9713
9714/* arm_coff_make_msymbol_special()
9715 arm_elf_make_msymbol_special()
9716
9717 These functions test whether the COFF or ELF symbol corresponds to
9718 an address in thumb code, and set a "special" bit in a minimal
9719 symbol to indicate that it does. */
9720
34e8f22d 9721static void
082fc60d
RE
9722arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
9723{
39d911fc
TP
9724 elf_symbol_type *elfsym = (elf_symbol_type *) sym;
9725
9726 if (ARM_GET_SYM_BRANCH_TYPE (elfsym->internal_elf_sym.st_target_internal)
467d42c4 9727 == ST_BRANCH_TO_THUMB)
082fc60d
RE
9728 MSYMBOL_SET_SPECIAL (msym);
9729}
9730
34e8f22d 9731static void
082fc60d
RE
9732arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
9733{
9734 if (coff_sym_is_thumb (val))
9735 MSYMBOL_SET_SPECIAL (msym);
9736}
9737
60c5725c
DJ
9738static void
9739arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
9740 asymbol *sym)
9741{
9742 const char *name = bfd_asymbol_name (sym);
bd5766ec 9743 struct arm_per_bfd *data;
60c5725c
DJ
9744 struct arm_mapping_symbol new_map_sym;
9745
9746 gdb_assert (name[0] == '$');
9747 if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
9748 return;
9749
98badbfd 9750 data = arm_bfd_data_key.get (objfile->obfd.get ());
60c5725c 9751 if (data == NULL)
98badbfd 9752 data = arm_bfd_data_key.emplace (objfile->obfd.get (),
bd5766ec 9753 objfile->obfd->section_count);
54cc7474 9754 arm_mapping_symbol_vec &map
e6f7f6d1 9755 = data->section_maps[bfd_asymbol_section (sym)->index];
60c5725c
DJ
9756
9757 new_map_sym.value = sym->value;
9758 new_map_sym.type = name[1];
9759
4838e44c
SM
9760 /* Insert at the end, the vector will be sorted on first use. */
9761 map.push_back (new_map_sym);
60c5725c
DJ
9762}
9763
756fe439 9764static void
61a1198a 9765arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
756fe439 9766{
ac7936df 9767 struct gdbarch *gdbarch = regcache->arch ();
61a1198a 9768 regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
756fe439
DJ
9769
9770 /* If necessary, set the T bit. */
9771 if (arm_apcs_32)
9772 {
9779414d 9773 ULONGEST val, t_bit;
61a1198a 9774 regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
9779414d
DJ
9775 t_bit = arm_psr_thumb_bit (gdbarch);
9776 if (arm_pc_is_thumb (gdbarch, pc))
9777 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9778 val | t_bit);
756fe439 9779 else
61a1198a 9780 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9779414d 9781 val & ~t_bit);
756fe439
DJ
9782 }
9783}
123dc839 9784
58d6951d
DJ
9785/* Read the contents of a NEON quad register, by reading from two
9786 double registers. This is used to implement the quad pseudo
9787 registers, and for argument passing in case the quad registers are
9788 missing; vectors are passed in quad registers when using the VFP
9789 ABI, even if a NEON unit is not present. REGNUM is the index of
9790 the quad register, in [0, 15]. */
9791
05d1431c 9792static enum register_status
849d0ba8 9793arm_neon_quad_read (struct gdbarch *gdbarch, readable_regcache *regcache,
58d6951d
DJ
9794 int regnum, gdb_byte *buf)
9795{
9796 char name_buf[4];
9797 gdb_byte reg_buf[8];
a0bfd1bf 9798 int double_regnum;
05d1431c 9799 enum register_status status;
58d6951d 9800
8c042590 9801 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
58d6951d
DJ
9802 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9803 strlen (name_buf));
9804
03f50fc8 9805 status = regcache->raw_read (double_regnum, reg_buf);
05d1431c
PA
9806 if (status != REG_VALID)
9807 return status;
a0bfd1bf 9808 memcpy (buf, reg_buf, 8);
58d6951d 9809
03f50fc8 9810 status = regcache->raw_read (double_regnum + 1, reg_buf);
05d1431c
PA
9811 if (status != REG_VALID)
9812 return status;
a0bfd1bf 9813 memcpy (buf + 8, reg_buf, 8);
05d1431c
PA
9814
9815 return REG_VALID;
58d6951d
DJ
9816}
9817
f8a311f0
SM
9818/* Read the contents of a NEON quad register, by reading from two double
9819 registers, and return it as a value. QUAD_REG_INDEX is the index of the quad
9820 register, in [0, 15]. */
ae66a8f1 9821
f8a311f0 9822static value *
8480a37e 9823arm_neon_quad_read_value (gdbarch *gdbarch, const frame_info_ptr &next_frame,
f8a311f0
SM
9824 int pseudo_reg_num, int quad_reg_index)
9825{
9826 std::string raw_reg_name = string_printf ("d%d", quad_reg_index << 1);
9827 int double_regnum
9828 = user_reg_map_name_to_regnum (gdbarch, raw_reg_name.c_str (),
9829 raw_reg_name.length ());
9830
9831 return pseudo_from_concat_raw (next_frame, pseudo_reg_num, double_regnum,
9832 double_regnum + 1);
9833}
9834
9835/* Read the contents of the MVE pseudo register REGNUM and return it as a
9836 value. */
9837static value *
8480a37e 9838arm_mve_pseudo_read_value (gdbarch *gdbarch, const frame_info_ptr &next_frame,
f8a311f0 9839 int pseudo_reg_num)
ae66a8f1 9840{
08106042 9841 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ae66a8f1
SP
9842
9843 /* P0 is the first 16 bits of VPR. */
f8a311f0
SM
9844 return pseudo_from_raw_part (next_frame, pseudo_reg_num,
9845 tdep->mve_vpr_regnum, 0);
ae66a8f1
SP
9846}
9847
f8a311f0 9848static value *
8480a37e 9849arm_pseudo_read_value (gdbarch *gdbarch, const frame_info_ptr &next_frame,
f8a311f0 9850 const int pseudo_reg_num)
58d6951d 9851{
08106042 9852 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d 9853
f8a311f0 9854 gdb_assert (pseudo_reg_num >= gdbarch_num_regs (gdbarch));
58d6951d 9855
f8a311f0 9856 if (is_q_pseudo (gdbarch, pseudo_reg_num))
ecbf5d4f
LM
9857 {
9858 /* Quad-precision register. */
f8a311f0
SM
9859 return arm_neon_quad_read_value (gdbarch, next_frame, pseudo_reg_num,
9860 pseudo_reg_num - tdep->q_pseudo_base);
ecbf5d4f 9861 }
f8a311f0
SM
9862 else if (is_mve_pseudo (gdbarch, pseudo_reg_num))
9863 return arm_mve_pseudo_read_value (gdbarch, next_frame, pseudo_reg_num);
58d6951d
DJ
9864 else
9865 {
f8a311f0 9866 int s_reg_index = pseudo_reg_num - tdep->s_pseudo_base;
05d1431c 9867
58d6951d 9868 /* Single-precision register. */
f8a311f0 9869 gdb_assert (s_reg_index < 32);
58d6951d
DJ
9870
9871 /* s0 is always the least significant half of d0. */
f8a311f0 9872 int offset;
58d6951d 9873 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
f8a311f0 9874 offset = (s_reg_index & 1) ? 0 : 4;
58d6951d 9875 else
f8a311f0 9876 offset = (s_reg_index & 1) ? 4 : 0;
58d6951d 9877
f8a311f0
SM
9878 std::string raw_reg_name = string_printf ("d%d", s_reg_index >> 1);
9879 int double_regnum
9880 = user_reg_map_name_to_regnum (gdbarch, raw_reg_name.c_str (),
9881 raw_reg_name.length ());
58d6951d 9882
f8a311f0
SM
9883 return pseudo_from_raw_part (next_frame, pseudo_reg_num, double_regnum,
9884 offset);
58d6951d
DJ
9885 }
9886}
9887
9888/* Store the contents of BUF to a NEON quad register, by writing to
9889 two double registers. This is used to implement the quad pseudo
9890 registers, and for argument passing in case the quad registers are
9891 missing; vectors are passed in quad registers when using the VFP
9892 ABI, even if a NEON unit is not present. REGNUM is the index
9893 of the quad register, in [0, 15]. */
9894
9895static void
9896arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
9897 int regnum, const gdb_byte *buf)
9898{
9899 char name_buf[4];
a0bfd1bf 9900 int double_regnum;
58d6951d 9901
8c042590 9902 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
58d6951d
DJ
9903 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9904 strlen (name_buf));
9905
a0bfd1bf
TT
9906 regcache->raw_write (double_regnum, buf);
9907 regcache->raw_write (double_regnum + 1, buf + 8);
58d6951d
DJ
9908}
9909
fa751379 9910static void
8480a37e 9911arm_neon_quad_write (gdbarch *gdbarch, const frame_info_ptr &next_frame,
fa751379
SM
9912 int quad_reg_index, gdb::array_view<const gdb_byte> buf)
9913{
9914 std::string raw_reg_name = string_printf ("d%d", quad_reg_index << 1);
9915 int double_regnum
9916 = user_reg_map_name_to_regnum (gdbarch, raw_reg_name.data (),
9917 raw_reg_name.length ());
9918
9919 pseudo_to_concat_raw (next_frame, buf, double_regnum, double_regnum + 1);
9920}
9921
ae66a8f1
SP
9922/* Store the contents of BUF to the MVE pseudo register REGNUM. */
9923
9924static void
8480a37e 9925arm_mve_pseudo_write (gdbarch *gdbarch, const frame_info_ptr &next_frame,
fa751379 9926 int pseudo_reg_num, gdb::array_view<const gdb_byte> buf)
ae66a8f1 9927{
08106042 9928 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ae66a8f1
SP
9929
9930 /* P0 is the first 16 bits of VPR. */
fa751379 9931 pseudo_to_raw_part(next_frame, buf, tdep->mve_vpr_regnum, 0);
ae66a8f1
SP
9932}
9933
58d6951d 9934static void
8480a37e 9935arm_pseudo_write (gdbarch *gdbarch, const frame_info_ptr &next_frame,
fa751379
SM
9936 const int pseudo_reg_num,
9937 gdb::array_view<const gdb_byte> buf)
58d6951d 9938{
08106042 9939 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d 9940
fa751379 9941 gdb_assert (pseudo_reg_num >= gdbarch_num_regs (gdbarch));
58d6951d 9942
fa751379 9943 if (is_q_pseudo (gdbarch, pseudo_reg_num))
ecbf5d4f
LM
9944 {
9945 /* Quad-precision register. */
fa751379
SM
9946 arm_neon_quad_write (gdbarch, next_frame,
9947 pseudo_reg_num - tdep->q_pseudo_base, buf);
ecbf5d4f 9948 }
fa751379
SM
9949 else if (is_mve_pseudo (gdbarch, pseudo_reg_num))
9950 arm_mve_pseudo_write (gdbarch, next_frame, pseudo_reg_num, buf);
58d6951d
DJ
9951 else
9952 {
fa751379
SM
9953 int s_reg_index = pseudo_reg_num - tdep->s_pseudo_base;
9954
58d6951d 9955 /* Single-precision register. */
fa751379 9956 gdb_assert (s_reg_index < 32);
58d6951d
DJ
9957
9958 /* s0 is always the least significant half of d0. */
fa751379 9959 int offset;
58d6951d 9960 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
fa751379 9961 offset = (s_reg_index & 1) ? 0 : 4;
58d6951d 9962 else
fa751379 9963 offset = (s_reg_index & 1) ? 4 : 0;
58d6951d 9964
fa751379
SM
9965 std::string raw_reg_name = string_printf ("d%d", s_reg_index >> 1);
9966 int double_regnum
9967 = user_reg_map_name_to_regnum (gdbarch, raw_reg_name.c_str (),
9968 raw_reg_name.length ());
58d6951d 9969
fa751379 9970 pseudo_to_raw_part (next_frame, buf, double_regnum, offset);
58d6951d
DJ
9971 }
9972}
9973
123dc839 9974static struct value *
8480a37e 9975value_of_arm_user_reg (const frame_info_ptr &frame, const void *baton)
123dc839 9976{
9a3c8263 9977 const int *reg_p = (const int *) baton;
a7952927 9978 return value_of_register (*reg_p, get_next_frame_sentinel_okay (frame));
123dc839 9979}
a7952927 9980
70f80edf
JT
9981static enum gdb_osabi
9982arm_elf_osabi_sniffer (bfd *abfd)
97e03143 9983{
2af48f68 9984 unsigned int elfosabi;
70f80edf 9985 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
97e03143 9986
70f80edf 9987 elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
97e03143 9988
28e97307
DJ
9989 if (elfosabi == ELFOSABI_ARM)
9990 /* GNU tools use this value. Check note sections in this case,
9991 as well. */
b35c1d1c
TT
9992 {
9993 for (asection *sect : gdb_bfd_sections (abfd))
9994 generic_elf_osabi_sniff_abi_tag_sections (abfd, sect, &osabi);
9995 }
97e03143 9996
28e97307 9997 /* Anything else will be handled by the generic ELF sniffer. */
70f80edf 9998 return osabi;
97e03143
RE
9999}
10000
54483882
YQ
10001static int
10002arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
dbf5d61b 10003 const struct reggroup *group)
54483882 10004{
2c291032
YQ
10005 /* FPS register's type is INT, but belongs to float_reggroup. Beside
10006 this, FPS register belongs to save_regroup, restore_reggroup, and
10007 all_reggroup, of course. */
54483882 10008 if (regnum == ARM_FPS_REGNUM)
2c291032
YQ
10009 return (group == float_reggroup
10010 || group == save_reggroup
10011 || group == restore_reggroup
10012 || group == all_reggroup);
54483882
YQ
10013 else
10014 return default_register_reggroup_p (gdbarch, regnum, group);
10015}
10016
25f8c692
JL
10017/* For backward-compatibility we allow two 'g' packet lengths with
10018 the remote protocol depending on whether FPA registers are
10019 supplied. M-profile targets do not have FPA registers, but some
10020 stubs already exist in the wild which use a 'g' packet which
10021 supplies them albeit with dummy values. The packet format which
10022 includes FPA registers should be considered deprecated for
10023 M-profile targets. */
10024
10025static void
10026arm_register_g_packet_guesses (struct gdbarch *gdbarch)
10027{
08106042 10028 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
345bd07c
SM
10029
10030 if (tdep->is_m)
25f8c692 10031 {
d105cce5
AH
10032 const target_desc *tdesc;
10033
25f8c692
JL
10034 /* If we know from the executable this is an M-profile target,
10035 cater for remote targets whose register set layout is the
10036 same as the FPA layout. */
d105cce5 10037 tdesc = arm_read_mprofile_description (ARM_M_TYPE_WITH_FPA);
25f8c692 10038 register_remote_g_packet_guess (gdbarch,
350fab54 10039 ARM_CORE_REGS_SIZE + ARM_FP_REGS_SIZE,
d105cce5 10040 tdesc);
25f8c692
JL
10041
10042 /* The regular M-profile layout. */
d105cce5 10043 tdesc = arm_read_mprofile_description (ARM_M_TYPE_M_PROFILE);
350fab54 10044 register_remote_g_packet_guess (gdbarch, ARM_CORE_REGS_SIZE,
d105cce5 10045 tdesc);
3184d3f9
JL
10046
10047 /* M-profile plus M4F VFP. */
d105cce5 10048 tdesc = arm_read_mprofile_description (ARM_M_TYPE_VFP_D16);
3184d3f9 10049 register_remote_g_packet_guess (gdbarch,
350fab54 10050 ARM_CORE_REGS_SIZE + ARM_VFP2_REGS_SIZE,
d105cce5 10051 tdesc);
ae66a8f1
SP
10052 /* M-profile plus MVE. */
10053 tdesc = arm_read_mprofile_description (ARM_M_TYPE_MVE);
10054 register_remote_g_packet_guess (gdbarch, ARM_CORE_REGS_SIZE
10055 + ARM_VFP2_REGS_SIZE
10056 + ARM_INT_REGISTER_SIZE, tdesc);
9074667a
CL
10057
10058 /* M-profile system (stack pointers). */
10059 tdesc = arm_read_mprofile_description (ARM_M_TYPE_SYSTEM);
10060 register_remote_g_packet_guess (gdbarch, 2 * ARM_INT_REGISTER_SIZE, tdesc);
25f8c692
JL
10061 }
10062
10063 /* Otherwise we don't have a useful guess. */
10064}
10065
7eb89530
YQ
10066/* Implement the code_of_frame_writable gdbarch method. */
10067
10068static int
8480a37e 10069arm_code_of_frame_writable (struct gdbarch *gdbarch, const frame_info_ptr &frame)
7eb89530 10070{
08106042 10071 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
345bd07c
SM
10072
10073 if (tdep->is_m && get_frame_type (frame) == SIGTRAMP_FRAME)
7eb89530
YQ
10074 {
10075 /* M-profile exception frames return to some magic PCs, where
10076 isn't writable at all. */
10077 return 0;
10078 }
10079 else
10080 return 1;
10081}
10082
3426ae57
AH
10083/* Implement gdbarch_gnu_triplet_regexp. If the arch name is arm then allow it
10084 to be postfixed by a version (eg armv7hl). */
10085
10086static const char *
10087arm_gnu_triplet_regexp (struct gdbarch *gdbarch)
10088{
10089 if (strcmp (gdbarch_bfd_arch_info (gdbarch)->arch_name, "arm") == 0)
10090 return "arm(v[^- ]*)?";
10091 return gdbarch_bfd_arch_info (gdbarch)->arch_name;
10092}
10093
a01567f4
LM
10094/* Implement the "get_pc_address_flags" gdbarch method. */
10095
10096static std::string
8480a37e 10097arm_get_pc_address_flags (const frame_info_ptr &frame, CORE_ADDR pc)
a01567f4
LM
10098{
10099 if (get_frame_pc_masked (frame))
10100 return "PAC";
10101
10102 return "";
10103}
10104
da3c6d4a
MS
10105/* Initialize the current architecture based on INFO. If possible,
10106 re-use an architecture from ARCHES, which is a list of
10107 architectures already created during this debugging session.
97e03143 10108
da3c6d4a
MS
10109 Called e.g. at program startup, when reading a core file, and when
10110 reading a binary file. */
97e03143 10111
39bbf761
RE
10112static struct gdbarch *
10113arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
10114{
28e97307
DJ
10115 struct gdbarch_list *best_arch;
10116 enum arm_abi_kind arm_abi = arm_abi_global;
10117 enum arm_float_model fp_model = arm_fp_model;
c1e1314d 10118 tdesc_arch_data_up tdesc_data;
7559c217
CB
10119 int i;
10120 bool is_m = false;
ae7e2f45 10121 bool have_sec_ext = false;
7559c217 10122 int vfp_register_count = 0;
ecbf5d4f 10123 bool have_s_pseudos = false, have_q_pseudos = false;
7559c217
CB
10124 bool have_wmmx_registers = false;
10125 bool have_neon = false;
10126 bool have_fpa_registers = true;
9779414d 10127 const struct target_desc *tdesc = info.target_desc;
ae66a8f1
SP
10128 bool have_vfp = false;
10129 bool have_mve = false;
a01567f4 10130 bool have_pacbti = false;
ae66a8f1 10131 int mve_vpr_regnum = -1;
ecbf5d4f 10132 int register_count = ARM_NUM_REGS;
9074667a
CL
10133 bool have_m_profile_msp = false;
10134 int m_profile_msp_regnum = -1;
10135 int m_profile_psp_regnum = -1;
ae7e2f45
CL
10136 int m_profile_msp_ns_regnum = -1;
10137 int m_profile_psp_ns_regnum = -1;
10138 int m_profile_msp_s_regnum = -1;
10139 int m_profile_psp_s_regnum = -1;
92d48a1e 10140 int tls_regnum = 0;
9779414d
DJ
10141
10142 /* If we have an object to base this architecture on, try to determine
10143 its ABI. */
10144
10145 if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
10146 {
10147 int ei_osabi, e_flags;
10148
10149 switch (bfd_get_flavour (info.abfd))
10150 {
9779414d
DJ
10151 case bfd_target_coff_flavour:
10152 /* Assume it's an old APCS-style ABI. */
10153 /* XXX WinCE? */
10154 arm_abi = ARM_ABI_APCS;
10155 break;
10156
10157 case bfd_target_elf_flavour:
10158 ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
10159 e_flags = elf_elfheader (info.abfd)->e_flags;
10160
10161 if (ei_osabi == ELFOSABI_ARM)
10162 {
10163 /* GNU tools used to use this value, but do not for EABI
10164 objects. There's nowhere to tag an EABI version
10165 anyway, so assume APCS. */
10166 arm_abi = ARM_ABI_APCS;
10167 }
d403db27 10168 else if (ei_osabi == ELFOSABI_NONE || ei_osabi == ELFOSABI_GNU)
9779414d
DJ
10169 {
10170 int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
9779414d
DJ
10171
10172 switch (eabi_ver)
10173 {
10174 case EF_ARM_EABI_UNKNOWN:
10175 /* Assume GNU tools. */
10176 arm_abi = ARM_ABI_APCS;
10177 break;
10178
10179 case EF_ARM_EABI_VER4:
10180 case EF_ARM_EABI_VER5:
10181 arm_abi = ARM_ABI_AAPCS;
10182 /* EABI binaries default to VFP float ordering.
10183 They may also contain build attributes that can
10184 be used to identify if the VFP argument-passing
10185 ABI is in use. */
10186 if (fp_model == ARM_FLOAT_AUTO)
10187 {
10188#ifdef HAVE_ELF
10189 switch (bfd_elf_get_obj_attr_int (info.abfd,
10190 OBJ_ATTR_PROC,
10191 Tag_ABI_VFP_args))
10192 {
b35b0298 10193 case AEABI_VFP_args_base:
9779414d
DJ
10194 /* "The user intended FP parameter/result
10195 passing to conform to AAPCS, base
10196 variant". */
10197 fp_model = ARM_FLOAT_SOFT_VFP;
10198 break;
b35b0298 10199 case AEABI_VFP_args_vfp:
9779414d
DJ
10200 /* "The user intended FP parameter/result
10201 passing to conform to AAPCS, VFP
10202 variant". */
10203 fp_model = ARM_FLOAT_VFP;
10204 break;
b35b0298 10205 case AEABI_VFP_args_toolchain:
9779414d
DJ
10206 /* "The user intended FP parameter/result
10207 passing to conform to tool chain-specific
10208 conventions" - we don't know any such
10209 conventions, so leave it as "auto". */
10210 break;
b35b0298 10211 case AEABI_VFP_args_compatible:
5c294fee
TG
10212 /* "Code is compatible with both the base
10213 and VFP variants; the user did not permit
10214 non-variadic functions to pass FP
10215 parameters/results" - leave it as
10216 "auto". */
10217 break;
9779414d
DJ
10218 default:
10219 /* Attribute value not mentioned in the
5c294fee 10220 November 2012 ABI, so leave it as
9779414d
DJ
10221 "auto". */
10222 break;
10223 }
10224#else
10225 fp_model = ARM_FLOAT_SOFT_VFP;
10226#endif
10227 }
10228 break;
10229
10230 default:
10231 /* Leave it as "auto". */
10232 warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
10233 break;
10234 }
10235
10236#ifdef HAVE_ELF
10237 /* Detect M-profile programs. This only works if the
10238 executable file includes build attributes; GCC does
10239 copy them to the executable, but e.g. RealView does
10240 not. */
17cbafdb
SM
10241 int attr_arch
10242 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10243 Tag_CPU_arch);
10244 int attr_profile
10245 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10246 Tag_CPU_arch_profile);
10247
9779414d
DJ
10248 /* GCC specifies the profile for v6-M; RealView only
10249 specifies the profile for architectures starting with
10250 V7 (as opposed to architectures with a tag
10251 numerically greater than TAG_CPU_ARCH_V7). */
10252 if (!tdesc_has_registers (tdesc)
10253 && (attr_arch == TAG_CPU_ARCH_V6_M
10254 || attr_arch == TAG_CPU_ARCH_V6S_M
131a355f
LM
10255 || attr_arch == TAG_CPU_ARCH_V7E_M
10256 || attr_arch == TAG_CPU_ARCH_V8M_BASE
10257 || attr_arch == TAG_CPU_ARCH_V8M_MAIN
ae66a8f1 10258 || attr_arch == TAG_CPU_ARCH_V8_1M_MAIN
9779414d 10259 || attr_profile == 'M'))
7559c217 10260 is_m = true;
a01567f4
LM
10261
10262 /* Look for attributes that indicate support for ARMv8.1-m
10263 PACBTI. */
10264 if (!tdesc_has_registers (tdesc) && is_m)
10265 {
10266 int attr_pac_extension
10267 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10268 Tag_PAC_extension);
10269
10270 int attr_bti_extension
10271 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10272 Tag_BTI_extension);
10273
10274 int attr_pacret_use
10275 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10276 Tag_PACRET_use);
10277
10278 int attr_bti_use
10279 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10280 Tag_BTI_use);
10281
10282 if (attr_pac_extension != 0 || attr_bti_extension != 0
10283 || attr_pacret_use != 0 || attr_bti_use != 0)
10284 have_pacbti = true;
10285 }
9779414d
DJ
10286#endif
10287 }
10288
10289 if (fp_model == ARM_FLOAT_AUTO)
10290 {
9779414d
DJ
10291 switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
10292 {
10293 case 0:
10294 /* Leave it as "auto". Strictly speaking this case
10295 means FPA, but almost nobody uses that now, and
10296 many toolchains fail to set the appropriate bits
10297 for the floating-point model they use. */
10298 break;
10299 case EF_ARM_SOFT_FLOAT:
10300 fp_model = ARM_FLOAT_SOFT_FPA;
10301 break;
10302 case EF_ARM_VFP_FLOAT:
10303 fp_model = ARM_FLOAT_VFP;
10304 break;
10305 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
10306 fp_model = ARM_FLOAT_SOFT_VFP;
10307 break;
10308 }
10309 }
10310
10311 if (e_flags & EF_ARM_BE8)
10312 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
10313
10314 break;
10315
10316 default:
10317 /* Leave it as "auto". */
10318 break;
10319 }
10320 }
123dc839
DJ
10321
10322 /* Check any target description for validity. */
9779414d 10323 if (tdesc_has_registers (tdesc))
123dc839
DJ
10324 {
10325 /* For most registers we require GDB's default names; but also allow
10326 the numeric names for sp / lr / pc, as a convenience. */
10327 static const char *const arm_sp_names[] = { "r13", "sp", NULL };
10328 static const char *const arm_lr_names[] = { "r14", "lr", NULL };
10329 static const char *const arm_pc_names[] = { "r15", "pc", NULL };
10330
10331 const struct tdesc_feature *feature;
58d6951d 10332 int valid_p;
123dc839 10333
9779414d 10334 feature = tdesc_find_feature (tdesc,
123dc839
DJ
10335 "org.gnu.gdb.arm.core");
10336 if (feature == NULL)
9779414d
DJ
10337 {
10338 feature = tdesc_find_feature (tdesc,
10339 "org.gnu.gdb.arm.m-profile");
10340 if (feature == NULL)
10341 return NULL;
10342 else
7559c217 10343 is_m = true;
9779414d 10344 }
123dc839
DJ
10345
10346 tdesc_data = tdesc_data_alloc ();
10347
10348 valid_p = 1;
10349 for (i = 0; i < ARM_SP_REGNUM; i++)
c1e1314d 10350 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
123dc839 10351 arm_register_names[i]);
c1e1314d 10352 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
123dc839
DJ
10353 ARM_SP_REGNUM,
10354 arm_sp_names);
c1e1314d 10355 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
123dc839
DJ
10356 ARM_LR_REGNUM,
10357 arm_lr_names);
c1e1314d 10358 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
123dc839
DJ
10359 ARM_PC_REGNUM,
10360 arm_pc_names);
9779414d 10361 if (is_m)
c1e1314d 10362 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
9779414d
DJ
10363 ARM_PS_REGNUM, "xpsr");
10364 else
c1e1314d 10365 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
9779414d 10366 ARM_PS_REGNUM, "cpsr");
123dc839
DJ
10367
10368 if (!valid_p)
c1e1314d 10369 return NULL;
123dc839 10370
9074667a
CL
10371 if (is_m)
10372 {
10373 feature = tdesc_find_feature (tdesc,
10374 "org.gnu.gdb.arm.m-system");
10375 if (feature != nullptr)
10376 {
10377 /* MSP */
10378 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10379 register_count, "msp");
10380 if (!valid_p)
10381 {
10382 warning (_("M-profile m-system feature is missing required register msp."));
10383 return nullptr;
10384 }
10385 have_m_profile_msp = true;
10386 m_profile_msp_regnum = register_count++;
10387
10388 /* PSP */
10389 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10390 register_count, "psp");
10391 if (!valid_p)
10392 {
10393 warning (_("M-profile m-system feature is missing required register psp."));
10394 return nullptr;
10395 }
10396 m_profile_psp_regnum = register_count++;
10397 }
10398 }
10399
9779414d 10400 feature = tdesc_find_feature (tdesc,
123dc839
DJ
10401 "org.gnu.gdb.arm.fpa");
10402 if (feature != NULL)
10403 {
10404 valid_p = 1;
10405 for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
c1e1314d 10406 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
123dc839
DJ
10407 arm_register_names[i]);
10408 if (!valid_p)
c1e1314d 10409 return NULL;
123dc839 10410 }
ff6f572f 10411 else
7559c217 10412 have_fpa_registers = false;
ff6f572f 10413
9779414d 10414 feature = tdesc_find_feature (tdesc,
ff6f572f
DJ
10415 "org.gnu.gdb.xscale.iwmmxt");
10416 if (feature != NULL)
10417 {
10418 static const char *const iwmmxt_names[] = {
10419 "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
10420 "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
10421 "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
10422 "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
10423 };
10424
10425 valid_p = 1;
10426 for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
10427 valid_p
c1e1314d 10428 &= tdesc_numbered_register (feature, tdesc_data.get (), i,
ff6f572f
DJ
10429 iwmmxt_names[i - ARM_WR0_REGNUM]);
10430
10431 /* Check for the control registers, but do not fail if they
10432 are missing. */
10433 for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
c1e1314d 10434 tdesc_numbered_register (feature, tdesc_data.get (), i,
ff6f572f
DJ
10435 iwmmxt_names[i - ARM_WR0_REGNUM]);
10436
10437 for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
10438 valid_p
c1e1314d 10439 &= tdesc_numbered_register (feature, tdesc_data.get (), i,
ff6f572f
DJ
10440 iwmmxt_names[i - ARM_WR0_REGNUM]);
10441
10442 if (!valid_p)
c1e1314d 10443 return NULL;
a56cc1ce 10444
7559c217 10445 have_wmmx_registers = true;
ff6f572f 10446 }
58d6951d
DJ
10447
10448 /* If we have a VFP unit, check whether the single precision registers
10449 are present. If not, then we will synthesize them as pseudo
10450 registers. */
9779414d 10451 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
10452 "org.gnu.gdb.arm.vfp");
10453 if (feature != NULL)
10454 {
10455 static const char *const vfp_double_names[] = {
10456 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
10457 "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
10458 "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
10459 "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
10460 };
10461
10462 /* Require the double precision registers. There must be either
10463 16 or 32. */
10464 valid_p = 1;
10465 for (i = 0; i < 32; i++)
10466 {
c1e1314d 10467 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
58d6951d
DJ
10468 ARM_D0_REGNUM + i,
10469 vfp_double_names[i]);
10470 if (!valid_p)
10471 break;
10472 }
2b9e5ea6
UW
10473 if (!valid_p && i == 16)
10474 valid_p = 1;
58d6951d 10475
2b9e5ea6 10476 /* Also require FPSCR. */
c1e1314d 10477 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
2b9e5ea6
UW
10478 ARM_FPSCR_REGNUM, "fpscr");
10479 if (!valid_p)
c1e1314d 10480 return NULL;
58d6951d 10481
ae66a8f1
SP
10482 have_vfp = true;
10483
58d6951d 10484 if (tdesc_unnumbered_register (feature, "s0") == 0)
ecbf5d4f 10485 have_s_pseudos = true;
58d6951d 10486
330c6ca9 10487 vfp_register_count = i;
58d6951d
DJ
10488
10489 /* If we have VFP, also check for NEON. The architecture allows
10490 NEON without VFP (integer vector operations only), but GDB
10491 does not support that. */
9779414d 10492 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
10493 "org.gnu.gdb.arm.neon");
10494 if (feature != NULL)
10495 {
10496 /* NEON requires 32 double-precision registers. */
10497 if (i != 32)
c1e1314d 10498 return NULL;
58d6951d
DJ
10499
10500 /* If there are quad registers defined by the stub, use
10501 their type; otherwise (normally) provide them with
10502 the default type. */
10503 if (tdesc_unnumbered_register (feature, "q0") == 0)
ecbf5d4f 10504 have_q_pseudos = true;
ae66a8f1
SP
10505 }
10506 }
10507
92d48a1e
JB
10508 /* Check for the TLS register feature. */
10509 feature = tdesc_find_feature (tdesc, "org.gnu.gdb.arm.tls");
10510 if (feature != nullptr)
10511 {
10512 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10513 register_count, "tpidruro");
10514 if (!valid_p)
10515 return nullptr;
10516
10517 tls_regnum = register_count;
10518 register_count++;
10519 }
10520
ae66a8f1
SP
10521 /* Check for MVE after all the checks for GPR's, VFP and Neon.
10522 MVE (Helium) is an M-profile extension. */
10523 if (is_m)
10524 {
10525 /* Do we have the MVE feature? */
10526 feature = tdesc_find_feature (tdesc,"org.gnu.gdb.arm.m-profile-mve");
10527
10528 if (feature != nullptr)
10529 {
10530 /* If we have MVE, we must always have the VPR register. */
10531 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10532 register_count, "vpr");
10533 if (!valid_p)
10534 {
10535 warning (_("MVE feature is missing required register vpr."));
10536 return nullptr;
10537 }
58d6951d 10538
ae66a8f1
SP
10539 have_mve = true;
10540 mve_vpr_regnum = register_count;
10541 register_count++;
10542
10543 /* We can't have Q pseudo registers available here, as that
10544 would mean we have NEON features, and that is only available
10545 on A and R profiles. */
10546 gdb_assert (!have_q_pseudos);
10547
10548 /* Given we have a M-profile target description, if MVE is
10549 enabled and there are VFP registers, we should have Q
10550 pseudo registers (Q0 ~ Q7). */
10551 if (have_vfp)
10552 have_q_pseudos = true;
58d6951d 10553 }
a01567f4
LM
10554
10555 /* Do we have the ARMv8.1-m PACBTI feature? */
10556 feature = tdesc_find_feature (tdesc,
10557 "org.gnu.gdb.arm.m-profile-pacbti");
10558 if (feature != nullptr)
10559 {
10560 /* By advertising this feature, the target acknowledges the
10561 presence of the ARMv8.1-m PACBTI extensions.
10562
10563 We don't care for any particular registers in this group, so
10564 the target is free to include whatever it deems appropriate.
10565
10566 The expectation is for this feature to include the PAC
10567 keys. */
10568 have_pacbti = true;
10569 }
ae7e2f45
CL
10570
10571 /* Do we have the Security extension? */
10572 feature = tdesc_find_feature (tdesc,
10573 "org.gnu.gdb.arm.secext");
10574 if (feature != nullptr)
10575 {
10576 /* Secure/Non-secure stack pointers. */
10577 /* MSP_NS */
10578 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10579 register_count, "msp_ns");
10580 if (!valid_p)
10581 {
10582 warning (_("M-profile secext feature is missing required register msp_ns."));
10583 return nullptr;
10584 }
10585 m_profile_msp_ns_regnum = register_count++;
10586
10587 /* PSP_NS */
10588 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10589 register_count, "psp_ns");
10590 if (!valid_p)
10591 {
10592 warning (_("M-profile secext feature is missing required register psp_ns."));
10593 return nullptr;
10594 }
10595 m_profile_psp_ns_regnum = register_count++;
10596
10597 /* MSP_S */
10598 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10599 register_count, "msp_s");
10600 if (!valid_p)
10601 {
10602 warning (_("M-profile secext feature is missing required register msp_s."));
10603 return nullptr;
10604 }
10605 m_profile_msp_s_regnum = register_count++;
10606
10607 /* PSP_S */
10608 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10609 register_count, "psp_s");
10610 if (!valid_p)
10611 {
10612 warning (_("M-profile secext feature is missing required register psp_s."));
10613 return nullptr;
10614 }
10615 m_profile_psp_s_regnum = register_count++;
10616
10617 have_sec_ext = true;
10618 }
10619
58d6951d 10620 }
123dc839 10621 }
39bbf761 10622
28e97307
DJ
10623 /* If there is already a candidate, use it. */
10624 for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
10625 best_arch != NULL;
10626 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
10627 {
345bd07c 10628 arm_gdbarch_tdep *tdep
08106042 10629 = gdbarch_tdep<arm_gdbarch_tdep> (best_arch->gdbarch);
345bd07c
SM
10630
10631 if (arm_abi != ARM_ABI_AUTO && arm_abi != tdep->arm_abi)
28e97307
DJ
10632 continue;
10633
345bd07c 10634 if (fp_model != ARM_FLOAT_AUTO && fp_model != tdep->fp_model)
28e97307
DJ
10635 continue;
10636
58d6951d
DJ
10637 /* There are various other properties in tdep that we do not
10638 need to check here: those derived from a target description,
10639 since gdbarches with a different target description are
10640 automatically disqualified. */
10641
9779414d 10642 /* Do check is_m, though, since it might come from the binary. */
345bd07c 10643 if (is_m != tdep->is_m)
9779414d
DJ
10644 continue;
10645
a01567f4
LM
10646 /* Also check for ARMv8.1-m PACBTI support, since it might come from
10647 the binary. */
10648 if (have_pacbti != tdep->have_pacbti)
10649 continue;
10650
28e97307
DJ
10651 /* Found a match. */
10652 break;
10653 }
97e03143 10654
28e97307 10655 if (best_arch != NULL)
c1e1314d 10656 return best_arch->gdbarch;
28e97307 10657
2b16913c
SM
10658 gdbarch *gdbarch
10659 = gdbarch_alloc (&info, gdbarch_tdep_up (new arm_gdbarch_tdep));
10660 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
97e03143 10661
28e97307
DJ
10662 /* Record additional information about the architecture we are defining.
10663 These are gdbarch discriminators, like the OSABI. */
10664 tdep->arm_abi = arm_abi;
10665 tdep->fp_model = fp_model;
9779414d 10666 tdep->is_m = is_m;
ae7e2f45 10667 tdep->have_sec_ext = have_sec_ext;
ff6f572f 10668 tdep->have_fpa_registers = have_fpa_registers;
a56cc1ce 10669 tdep->have_wmmx_registers = have_wmmx_registers;
330c6ca9
YQ
10670 gdb_assert (vfp_register_count == 0
10671 || vfp_register_count == 16
10672 || vfp_register_count == 32);
10673 tdep->vfp_register_count = vfp_register_count;
ecbf5d4f
LM
10674 tdep->have_s_pseudos = have_s_pseudos;
10675 tdep->have_q_pseudos = have_q_pseudos;
58d6951d 10676 tdep->have_neon = have_neon;
92d48a1e 10677 tdep->tls_regnum = tls_regnum;
08216dd7 10678
ae66a8f1
SP
10679 /* Adjust the MVE feature settings. */
10680 if (have_mve)
10681 {
10682 tdep->have_mve = true;
10683 tdep->mve_vpr_regnum = mve_vpr_regnum;
10684 }
10685
a01567f4
LM
10686 /* Adjust the PACBTI feature settings. */
10687 tdep->have_pacbti = have_pacbti;
10688
9074667a
CL
10689 /* Adjust the M-profile stack pointers settings. */
10690 if (have_m_profile_msp)
10691 {
10692 tdep->m_profile_msp_regnum = m_profile_msp_regnum;
10693 tdep->m_profile_psp_regnum = m_profile_psp_regnum;
ae7e2f45
CL
10694 tdep->m_profile_msp_ns_regnum = m_profile_msp_ns_regnum;
10695 tdep->m_profile_psp_ns_regnum = m_profile_psp_ns_regnum;
10696 tdep->m_profile_msp_s_regnum = m_profile_msp_s_regnum;
10697 tdep->m_profile_psp_s_regnum = m_profile_psp_s_regnum;
9074667a
CL
10698 }
10699
25f8c692
JL
10700 arm_register_g_packet_guesses (gdbarch);
10701
08216dd7 10702 /* Breakpoints. */
9d4fde75 10703 switch (info.byte_order_for_code)
67255d04
RE
10704 {
10705 case BFD_ENDIAN_BIG:
66e810cd
RE
10706 tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
10707 tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
10708 tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
10709 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
10710
67255d04
RE
10711 break;
10712
10713 case BFD_ENDIAN_LITTLE:
66e810cd
RE
10714 tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
10715 tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
10716 tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
10717 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
10718
67255d04
RE
10719 break;
10720
10721 default:
f34652de 10722 internal_error (_("arm_gdbarch_init: bad byte order for float format"));
67255d04
RE
10723 }
10724
d7b486e7
RE
10725 /* On ARM targets char defaults to unsigned. */
10726 set_gdbarch_char_signed (gdbarch, 0);
10727
53375380
PA
10728 /* wchar_t is unsigned under the AAPCS. */
10729 if (tdep->arm_abi == ARM_ABI_AAPCS)
10730 set_gdbarch_wchar_signed (gdbarch, 0);
10731 else
10732 set_gdbarch_wchar_signed (gdbarch, 1);
53375380 10733
030197b4
AB
10734 /* Compute type alignment. */
10735 set_gdbarch_type_align (gdbarch, arm_type_align);
10736
cca44b1b
JB
10737 /* Note: for displaced stepping, this includes the breakpoint, and one word
10738 of additional scratch space. This setting isn't used for anything beside
10739 displaced stepping at present. */
deb65a3c
AB
10740 set_gdbarch_displaced_step_buffer_length
10741 (gdbarch, 4 * ARM_DISPLACED_MODIFIED_INSNS);
10742 set_gdbarch_max_insn_length (gdbarch, 4);
cca44b1b 10743
9df628e0 10744 /* This should be low enough for everything. */
97e03143 10745 tdep->lowest_pc = 0x20;
94c30b78 10746 tdep->jb_pc = -1; /* Longjump support not enabled by default. */
97e03143 10747
7c00367c
MK
10748 /* The default, for both APCS and AAPCS, is to return small
10749 structures in registers. */
10750 tdep->struct_return = reg_struct_return;
10751
2dd604e7 10752 set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
f53f0d0b 10753 set_gdbarch_frame_align (gdbarch, arm_frame_align);
39bbf761 10754
7eb89530
YQ
10755 if (is_m)
10756 set_gdbarch_code_of_frame_writable (gdbarch, arm_code_of_frame_writable);
10757
756fe439
DJ
10758 set_gdbarch_write_pc (gdbarch, arm_write_pc);
10759
eb5492fa 10760 frame_base_set_default (gdbarch, &arm_normal_base);
148754e5 10761
34e8f22d 10762 /* Address manipulation. */
34e8f22d
RE
10763 set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
10764
34e8f22d
RE
10765 /* Advance PC across function entry code. */
10766 set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
10767
c9cf6e20
MG
10768 /* Detect whether PC is at a point where the stack has been destroyed. */
10769 set_gdbarch_stack_frame_destroyed_p (gdbarch, arm_stack_frame_destroyed_p);
4024ca99 10770
190dce09
UW
10771 /* Skip trampolines. */
10772 set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
10773
34e8f22d
RE
10774 /* The stack grows downward. */
10775 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
10776
10777 /* Breakpoint manipulation. */
04180708
YQ
10778 set_gdbarch_breakpoint_kind_from_pc (gdbarch, arm_breakpoint_kind_from_pc);
10779 set_gdbarch_sw_breakpoint_from_kind (gdbarch, arm_sw_breakpoint_from_kind);
833b7ab5
YQ
10780 set_gdbarch_breakpoint_kind_from_current_state (gdbarch,
10781 arm_breakpoint_kind_from_current_state);
34e8f22d
RE
10782
10783 /* Information about registers, etc. */
34e8f22d
RE
10784 set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
10785 set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
ecbf5d4f 10786 set_gdbarch_num_regs (gdbarch, register_count);
7a5ea0d4 10787 set_gdbarch_register_type (gdbarch, arm_register_type);
54483882 10788 set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);
34e8f22d 10789
ff6f572f
DJ
10790 /* This "info float" is FPA-specific. Use the generic version if we
10791 do not have FPA. */
345bd07c 10792 if (tdep->have_fpa_registers)
ff6f572f
DJ
10793 set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
10794
26216b98 10795 /* Internal <-> external register number maps. */
ff6f572f 10796 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
26216b98
AC
10797 set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
10798
34e8f22d
RE
10799 set_gdbarch_register_name (gdbarch, arm_register_name);
10800
10801 /* Returning results. */
5cb0f2d5 10802 set_gdbarch_return_value_as_value (gdbarch, arm_return_value);
34e8f22d 10803
03d48a7d
RE
10804 /* Disassembly. */
10805 set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
10806
34e8f22d
RE
10807 /* Minsymbol frobbing. */
10808 set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
10809 set_gdbarch_coff_make_msymbol_special (gdbarch,
10810 arm_coff_make_msymbol_special);
60c5725c 10811 set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
34e8f22d 10812
f9d67f43
DJ
10813 /* Thumb-2 IT block support. */
10814 set_gdbarch_adjust_breakpoint_address (gdbarch,
10815 arm_adjust_breakpoint_address);
10816
0d5de010
DJ
10817 /* Virtual tables. */
10818 set_gdbarch_vbit_in_delta (gdbarch, 1);
10819
97e03143 10820 /* Hook in the ABI-specific overrides, if they have been registered. */
4be87837 10821 gdbarch_init_osabi (info, gdbarch);
97e03143 10822
b39cc962
DJ
10823 dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
10824
eb5492fa 10825 /* Add some default predicates. */
2ae28aa9
YQ
10826 if (is_m)
10827 frame_unwind_append_unwinder (gdbarch, &arm_m_exception_unwind);
a262aec2
DJ
10828 frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
10829 dwarf2_append_unwinders (gdbarch);
0e9e9abd 10830 frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
779aa56f 10831 frame_unwind_append_unwinder (gdbarch, &arm_epilogue_frame_unwind);
a262aec2 10832 frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
eb5492fa 10833
97e03143
RE
10834 /* Now we have tuned the configuration, set a few final things,
10835 based on what the OS ABI has told us. */
10836
b8926edc
DJ
10837 /* If the ABI is not otherwise marked, assume the old GNU APCS. EABI
10838 binaries are always marked. */
10839 if (tdep->arm_abi == ARM_ABI_AUTO)
10840 tdep->arm_abi = ARM_ABI_APCS;
10841
e3039479
UW
10842 /* Watchpoints are not steppable. */
10843 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
10844
b8926edc
DJ
10845 /* We used to default to FPA for generic ARM, but almost nobody
10846 uses that now, and we now provide a way for the user to force
10847 the model. So default to the most useful variant. */
10848 if (tdep->fp_model == ARM_FLOAT_AUTO)
10849 tdep->fp_model = ARM_FLOAT_SOFT_FPA;
10850
9df628e0
RE
10851 if (tdep->jb_pc >= 0)
10852 set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
10853
08216dd7 10854 /* Floating point sizes and format. */
8da61cc4 10855 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
b8926edc 10856 if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
08216dd7 10857 {
8da61cc4
DJ
10858 set_gdbarch_double_format
10859 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10860 set_gdbarch_long_double_format
10861 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10862 }
10863 else
10864 {
10865 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
10866 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
08216dd7
RE
10867 }
10868
a01567f4
LM
10869 /* Hook used to decorate frames with signed return addresses, only available
10870 for ARMv8.1-m PACBTI. */
10871 if (is_m && have_pacbti)
10872 set_gdbarch_get_pc_address_flags (gdbarch, arm_get_pc_address_flags);
10873
dc22c61a
LM
10874 if (tdesc_data != nullptr)
10875 {
10876 set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
10877
10878 tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
ecbf5d4f 10879 register_count = gdbarch_num_regs (gdbarch);
dc22c61a
LM
10880
10881 /* Override tdesc_register_type to adjust the types of VFP
10882 registers for NEON. */
10883 set_gdbarch_register_type (gdbarch, arm_register_type);
10884 }
10885
ecbf5d4f 10886 /* Initialize the pseudo register data. */
ae66a8f1 10887 int num_pseudos = 0;
ecbf5d4f 10888 if (tdep->have_s_pseudos)
58d6951d 10889 {
ecbf5d4f
LM
10890 /* VFP single precision pseudo registers (S0~S31). */
10891 tdep->s_pseudo_base = register_count;
10892 tdep->s_pseudo_count = 32;
ae66a8f1 10893 num_pseudos += tdep->s_pseudo_count;
ecbf5d4f
LM
10894
10895 if (tdep->have_q_pseudos)
10896 {
10897 /* NEON quad precision pseudo registers (Q0~Q15). */
10898 tdep->q_pseudo_base = register_count + num_pseudos;
ae66a8f1
SP
10899
10900 if (have_neon)
10901 tdep->q_pseudo_count = 16;
10902 else if (have_mve)
10903 tdep->q_pseudo_count = ARM_MVE_NUM_Q_REGS;
10904
ecbf5d4f
LM
10905 num_pseudos += tdep->q_pseudo_count;
10906 }
ae66a8f1
SP
10907 }
10908
10909 /* Do we have any MVE pseudo registers? */
10910 if (have_mve)
10911 {
10912 tdep->mve_pseudo_base = register_count + num_pseudos;
10913 tdep->mve_pseudo_count = 1;
10914 num_pseudos += tdep->mve_pseudo_count;
10915 }
58d6951d 10916
a01567f4
LM
10917 /* Do we have any ARMv8.1-m PACBTI pseudo registers. */
10918 if (have_pacbti)
10919 {
10920 tdep->pacbti_pseudo_base = register_count + num_pseudos;
10921 tdep->pacbti_pseudo_count = 1;
10922 num_pseudos += tdep->pacbti_pseudo_count;
10923 }
10924
ae66a8f1 10925 /* Set some pseudo register hooks, if we have pseudo registers. */
a01567f4 10926 if (tdep->have_s_pseudos || have_mve || have_pacbti)
ae66a8f1 10927 {
58d6951d 10928 set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
f8a311f0 10929 set_gdbarch_pseudo_register_read_value (gdbarch, arm_pseudo_read_value);
fa751379 10930 set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
58d6951d
DJ
10931 }
10932
123dc839 10933 /* Add standard register aliases. We add aliases even for those
85102364 10934 names which are used by the current architecture - it's simpler,
123dc839
DJ
10935 and does no harm, since nothing ever lists user registers. */
10936 for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
10937 user_reg_add (gdbarch, arm_register_aliases[i].name,
10938 value_of_arm_user_reg, &arm_register_aliases[i].regnum);
10939
65b48a81
PB
10940 set_gdbarch_disassembler_options (gdbarch, &arm_disassembler_options);
10941 set_gdbarch_valid_disassembler_options (gdbarch, disassembler_options_arm ());
10942
3426ae57
AH
10943 set_gdbarch_gnu_triplet_regexp (gdbarch, arm_gnu_triplet_regexp);
10944
39bbf761
RE
10945 return gdbarch;
10946}
10947
97e03143 10948static void
2af46ca0 10949arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
97e03143 10950{
08106042 10951 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
97e03143
RE
10952
10953 if (tdep == NULL)
10954 return;
10955
6cb06a8c
TT
10956 gdb_printf (file, _("arm_dump_tdep: fp_model = %i\n"),
10957 (int) tdep->fp_model);
10958 gdb_printf (file, _("arm_dump_tdep: have_fpa_registers = %i\n"),
10959 (int) tdep->have_fpa_registers);
10960 gdb_printf (file, _("arm_dump_tdep: have_wmmx_registers = %i\n"),
10961 (int) tdep->have_wmmx_registers);
10962 gdb_printf (file, _("arm_dump_tdep: vfp_register_count = %i\n"),
10963 (int) tdep->vfp_register_count);
10964 gdb_printf (file, _("arm_dump_tdep: have_s_pseudos = %s\n"),
05d63baf 10965 tdep->have_s_pseudos ? "true" : "false");
6cb06a8c
TT
10966 gdb_printf (file, _("arm_dump_tdep: s_pseudo_base = %i\n"),
10967 (int) tdep->s_pseudo_base);
10968 gdb_printf (file, _("arm_dump_tdep: s_pseudo_count = %i\n"),
10969 (int) tdep->s_pseudo_count);
10970 gdb_printf (file, _("arm_dump_tdep: have_q_pseudos = %s\n"),
05d63baf 10971 tdep->have_q_pseudos ? "true" : "false");
6cb06a8c
TT
10972 gdb_printf (file, _("arm_dump_tdep: q_pseudo_base = %i\n"),
10973 (int) tdep->q_pseudo_base);
10974 gdb_printf (file, _("arm_dump_tdep: q_pseudo_count = %i\n"),
10975 (int) tdep->q_pseudo_count);
10976 gdb_printf (file, _("arm_dump_tdep: have_neon = %i\n"),
10977 (int) tdep->have_neon);
10978 gdb_printf (file, _("arm_dump_tdep: have_mve = %s\n"),
05d63baf 10979 tdep->have_mve ? "yes" : "no");
6cb06a8c
TT
10980 gdb_printf (file, _("arm_dump_tdep: mve_vpr_regnum = %i\n"),
10981 tdep->mve_vpr_regnum);
10982 gdb_printf (file, _("arm_dump_tdep: mve_pseudo_base = %i\n"),
10983 tdep->mve_pseudo_base);
10984 gdb_printf (file, _("arm_dump_tdep: mve_pseudo_count = %i\n"),
10985 tdep->mve_pseudo_count);
9074667a
CL
10986 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_regnum = %i\n"),
10987 tdep->m_profile_msp_regnum);
10988 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_regnum = %i\n"),
10989 tdep->m_profile_psp_regnum);
ae7e2f45
CL
10990 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_ns_regnum = %i\n"),
10991 tdep->m_profile_msp_ns_regnum);
10992 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_ns_regnum = %i\n"),
10993 tdep->m_profile_psp_ns_regnum);
10994 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_s_regnum = %i\n"),
10995 tdep->m_profile_msp_s_regnum);
10996 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_s_regnum = %i\n"),
10997 tdep->m_profile_psp_s_regnum);
6cb06a8c
TT
10998 gdb_printf (file, _("arm_dump_tdep: Lowest pc = 0x%lx\n"),
10999 (unsigned long) tdep->lowest_pc);
a01567f4 11000 gdb_printf (file, _("arm_dump_tdep: have_pacbti = %s\n"),
05d63baf 11001 tdep->have_pacbti ? "yes" : "no");
a01567f4
LM
11002 gdb_printf (file, _("arm_dump_tdep: pacbti_pseudo_base = %i\n"),
11003 tdep->pacbti_pseudo_base);
11004 gdb_printf (file, _("arm_dump_tdep: pacbti_pseudo_count = %i\n"),
11005 tdep->pacbti_pseudo_count);
11006 gdb_printf (file, _("arm_dump_tdep: is_m = %s\n"),
05d63baf 11007 tdep->is_m ? "yes" : "no");
97e03143
RE
11008}
11009
0d4c07af 11010#if GDB_SELF_TEST
b121eeb9
YQ
11011namespace selftests
11012{
11013static void arm_record_test (void);
9ecab40c 11014static void arm_analyze_prologue_test ();
b121eeb9 11015}
0d4c07af 11016#endif
b121eeb9 11017
6c265988 11018void _initialize_arm_tdep ();
c906108c 11019void
6c265988 11020_initialize_arm_tdep ()
c906108c 11021{
bc90b915 11022 long length;
65b48a81 11023 int i, j;
edefbb7c
AC
11024 char regdesc[1024], *rdptr = regdesc;
11025 size_t rest = sizeof (regdesc);
085dd6e6 11026
42cf1509 11027 gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
97e03143 11028
0e9e9abd 11029 /* Add ourselves to objfile event chain. */
c90e7d63 11030 gdb::observers::new_objfile.attach (arm_exidx_new_objfile, "arm-tdep");
0e9e9abd 11031
70f80edf
JT
11032 /* Register an ELF OS ABI sniffer for ARM binaries. */
11033 gdbarch_register_osabi_sniffer (bfd_arch_arm,
11034 bfd_target_elf_flavour,
11035 arm_elf_osabi_sniffer);
11036
afd7eef0 11037 /* Add root prefix command for all "set arm"/"show arm" commands. */
f54bdb6d
SM
11038 add_setshow_prefix_cmd ("arm", no_class,
11039 _("Various ARM-specific commands."),
11040 _("Various ARM-specific commands."),
11041 &setarmcmdlist, &showarmcmdlist,
11042 &setlist, &showlist);
c5aa993b 11043
c05dd511 11044 arm_disassembler_options = "reg-names-std";
471b9d15
MR
11045 const disasm_options_t *disasm_options
11046 = &disassembler_options_arm ()->options;
65b48a81
PB
11047 int num_disassembly_styles = 0;
11048 for (i = 0; disasm_options->name[i] != NULL; i++)
08dedd66 11049 if (startswith (disasm_options->name[i], "reg-names-"))
65b48a81
PB
11050 num_disassembly_styles++;
11051
11052 /* Initialize the array that will be passed to add_setshow_enum_cmd(). */
8d749320 11053 valid_disassembly_styles = XNEWVEC (const char *,
65b48a81
PB
11054 num_disassembly_styles + 1);
11055 for (i = j = 0; disasm_options->name[i] != NULL; i++)
08dedd66 11056 if (startswith (disasm_options->name[i], "reg-names-"))
65b48a81
PB
11057 {
11058 size_t offset = strlen ("reg-names-");
11059 const char *style = disasm_options->name[i];
11060 valid_disassembly_styles[j++] = &style[offset];
dedb7102
TT
11061 if (strcmp (&style[offset], "std") == 0)
11062 disassembly_style = &style[offset];
65b48a81
PB
11063 length = snprintf (rdptr, rest, "%s - %s\n", &style[offset],
11064 disasm_options->description[i]);
11065 rdptr += length;
11066 rest -= length;
11067 }
94c30b78 11068 /* Mark the end of valid options. */
65b48a81 11069 valid_disassembly_styles[num_disassembly_styles] = NULL;
c906108c 11070
edefbb7c 11071 /* Create the help text. */
d7e74731
PA
11072 std::string helptext = string_printf ("%s%s%s",
11073 _("The valid values are:\n"),
11074 regdesc,
11075 _("The default is \"std\"."));
ed9a39eb 11076
edefbb7c
AC
11077 add_setshow_enum_cmd("disassembler", no_class,
11078 valid_disassembly_styles, &disassembly_style,
11079 _("Set the disassembly style."),
11080 _("Show the disassembly style."),
09b0e4b0 11081 helptext.c_str (),
2c5b56ce 11082 set_disassembly_style_sfunc,
65b48a81 11083 show_disassembly_style_sfunc,
7376b4c2 11084 &setarmcmdlist, &showarmcmdlist);
edefbb7c
AC
11085
11086 add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
11087 _("Set usage of ARM 32-bit mode."),
11088 _("Show usage of ARM 32-bit mode."),
11089 _("When off, a 26-bit PC will be used."),
2c5b56ce 11090 NULL,
0963b4bd
MS
11091 NULL, /* FIXME: i18n: Usage of ARM 32-bit
11092 mode is %s. */
26304000 11093 &setarmcmdlist, &showarmcmdlist);
c906108c 11094
fd50bc42 11095 /* Add a command to allow the user to force the FPU model. */
edefbb7c
AC
11096 add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
11097 _("Set the floating point type."),
11098 _("Show the floating point type."),
11099 _("auto - Determine the FP typefrom the OS-ABI.\n\
11100softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
11101fpa - FPA co-processor (GCC compiled).\n\
11102softvfp - Software FP with pure-endian doubles.\n\
11103vfp - VFP co-processor."),
edefbb7c 11104 set_fp_model_sfunc, show_fp_model,
7376b4c2 11105 &setarmcmdlist, &showarmcmdlist);
fd50bc42 11106
28e97307
DJ
11107 /* Add a command to allow the user to force the ABI. */
11108 add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
11109 _("Set the ABI."),
11110 _("Show the ABI."),
11111 NULL, arm_set_abi, arm_show_abi,
11112 &setarmcmdlist, &showarmcmdlist);
11113
0428b8f5
DJ
11114 /* Add two commands to allow the user to force the assumed
11115 execution mode. */
11116 add_setshow_enum_cmd ("fallback-mode", class_support,
11117 arm_mode_strings, &arm_fallback_mode_string,
11118 _("Set the mode assumed when symbols are unavailable."),
11119 _("Show the mode assumed when symbols are unavailable."),
11120 NULL, NULL, arm_show_fallback_mode,
11121 &setarmcmdlist, &showarmcmdlist);
11122 add_setshow_enum_cmd ("force-mode", class_support,
11123 arm_mode_strings, &arm_force_mode_string,
11124 _("Set the mode assumed even when symbols are available."),
11125 _("Show the mode assumed even when symbols are available."),
11126 NULL, NULL, arm_show_force_mode,
11127 &setarmcmdlist, &showarmcmdlist);
11128
ef273377
CL
11129 /* Add a command to stop triggering security exceptions when
11130 unwinding exception stacks. */
11131 add_setshow_boolean_cmd ("unwind-secure-frames", no_class, &arm_unwind_secure_frames,
11132 _("Set usage of non-secure to secure exception stack unwinding."),
11133 _("Show usage of non-secure to secure exception stack unwinding."),
11134 _("When on, the debugger can trigger memory access traps."),
11135 NULL, arm_show_unwind_secure_frames,
11136 &setarmcmdlist, &showarmcmdlist);
11137
6529d2dd 11138 /* Debugging flag. */
edefbb7c
AC
11139 add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
11140 _("Set ARM debugging."),
11141 _("Show ARM debugging."),
11142 _("When on, arm-specific debugging is enabled."),
2c5b56ce 11143 NULL,
7915a72c 11144 NULL, /* FIXME: i18n: "ARM debugging is %s. */
26304000 11145 &setdebuglist, &showdebuglist);
b121eeb9
YQ
11146
11147#if GDB_SELF_TEST
1526853e 11148 selftests::register_test ("arm-record", selftests::arm_record_test);
9ecab40c 11149 selftests::register_test ("arm_analyze_prologue", selftests::arm_analyze_prologue_test);
b121eeb9
YQ
11150#endif
11151
c906108c 11152}
72508ac0
PO
11153
11154/* ARM-reversible process record data structures. */
11155
11156#define ARM_INSN_SIZE_BYTES 4
11157#define THUMB_INSN_SIZE_BYTES 2
11158#define THUMB2_INSN_SIZE_BYTES 4
11159
11160
71e396f9
LM
11161/* Position of the bit within a 32-bit ARM instruction
11162 that defines whether the instruction is a load or store. */
72508ac0
PO
11163#define INSN_S_L_BIT_NUM 20
11164
11165#define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
dda83cd7
SM
11166 do \
11167 { \
11168 unsigned int reg_len = LENGTH; \
11169 if (reg_len) \
11170 { \
11171 REGS = XNEWVEC (uint32_t, reg_len); \
11172 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
11173 } \
11174 } \
11175 while (0)
72508ac0
PO
11176
11177#define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
dda83cd7
SM
11178 do \
11179 { \
11180 unsigned int mem_len = LENGTH; \
11181 if (mem_len) \
01add95b
SM
11182 { \
11183 MEMS = XNEWVEC (struct arm_mem_r, mem_len); \
11184 memcpy(&MEMS->len, &RECORD_BUF[0], \
11185 sizeof(struct arm_mem_r) * LENGTH); \
11186 } \
dda83cd7
SM
11187 } \
11188 while (0)
72508ac0
PO
11189
11190/* Checks whether insn is already recorded or yet to be decoded. (boolean expression). */
11191#define INSN_RECORDED(ARM_RECORD) \
dda83cd7 11192 (0 != (ARM_RECORD)->reg_rec_count || 0 != (ARM_RECORD)->mem_rec_count)
72508ac0
PO
11193
11194/* ARM memory record structure. */
11195struct arm_mem_r
11196{
11197 uint32_t len; /* Record length. */
bfbbec00 11198 uint32_t addr; /* Memory address. */
72508ac0
PO
11199};
11200
11201/* ARM instruction record contains opcode of current insn
11202 and execution state (before entry to decode_insn()),
11203 contains list of to-be-modified registers and
11204 memory blocks (on return from decode_insn()). */
11205
4748a9be 11206struct arm_insn_decode_record
72508ac0
PO
11207{
11208 struct gdbarch *gdbarch;
11209 struct regcache *regcache;
11210 CORE_ADDR this_addr; /* Address of the insn being decoded. */
11211 uint32_t arm_insn; /* Should accommodate thumb. */
11212 uint32_t cond; /* Condition code. */
11213 uint32_t opcode; /* Insn opcode. */
11214 uint32_t decode; /* Insn decode bits. */
11215 uint32_t mem_rec_count; /* No of mem records. */
11216 uint32_t reg_rec_count; /* No of reg records. */
11217 uint32_t *arm_regs; /* Registers to be saved for this record. */
11218 struct arm_mem_r *arm_mems; /* Memory to be saved for this record. */
4748a9be 11219};
72508ac0
PO
11220
11221
11222/* Checks ARM SBZ and SBO mandatory fields. */
11223
11224static int
11225sbo_sbz (uint32_t insn, uint32_t bit_num, uint32_t len, uint32_t sbo)
11226{
11227 uint32_t ones = bits (insn, bit_num - 1, (bit_num -1) + (len - 1));
11228
11229 if (!len)
11230 return 1;
11231
11232 if (!sbo)
11233 ones = ~ones;
11234
11235 while (ones)
11236 {
11237 if (!(ones & sbo))
dda83cd7
SM
11238 {
11239 return 0;
11240 }
72508ac0
PO
11241 ones = ones >> 1;
11242 }
11243 return 1;
11244}
11245
c6ec2b30
OJ
11246enum arm_record_result
11247{
11248 ARM_RECORD_SUCCESS = 0,
11249 ARM_RECORD_FAILURE = 1
11250};
11251
0d1703b8 11252enum arm_record_strx_t
72508ac0
PO
11253{
11254 ARM_RECORD_STRH=1,
11255 ARM_RECORD_STRD
0d1703b8 11256};
72508ac0 11257
0d1703b8 11258enum record_type_t
72508ac0
PO
11259{
11260 ARM_RECORD=1,
11261 THUMB_RECORD,
11262 THUMB2_RECORD
0d1703b8 11263};
72508ac0
PO
11264
11265
11266static int
4748a9be 11267arm_record_strx (arm_insn_decode_record *arm_insn_r, uint32_t *record_buf,
dda83cd7 11268 uint32_t *record_buf_mem, arm_record_strx_t str_type)
72508ac0
PO
11269{
11270
11271 struct regcache *reg_cache = arm_insn_r->regcache;
11272 ULONGEST u_regval[2]= {0};
11273
11274 uint32_t reg_src1 = 0, reg_src2 = 0;
11275 uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
72508ac0
PO
11276
11277 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11278 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
72508ac0
PO
11279
11280 if (14 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11281 {
11282 /* 1) Handle misc store, immediate offset. */
11283 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
11284 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
11285 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11286 regcache_raw_read_unsigned (reg_cache, reg_src1,
dda83cd7 11287 &u_regval[0]);
72508ac0 11288 if (ARM_PC_REGNUM == reg_src1)
dda83cd7
SM
11289 {
11290 /* If R15 was used as Rn, hence current PC+8. */
11291 u_regval[0] = u_regval[0] + 8;
11292 }
72508ac0
PO
11293 offset_8 = (immed_high << 4) | immed_low;
11294 /* Calculate target store address. */
11295 if (14 == arm_insn_r->opcode)
dda83cd7
SM
11296 {
11297 tgt_mem_addr = u_regval[0] + offset_8;
11298 }
72508ac0 11299 else
dda83cd7
SM
11300 {
11301 tgt_mem_addr = u_regval[0] - offset_8;
11302 }
72508ac0 11303 if (ARM_RECORD_STRH == str_type)
dda83cd7
SM
11304 {
11305 record_buf_mem[0] = 2;
11306 record_buf_mem[1] = tgt_mem_addr;
11307 arm_insn_r->mem_rec_count = 1;
11308 }
72508ac0 11309 else if (ARM_RECORD_STRD == str_type)
dda83cd7
SM
11310 {
11311 record_buf_mem[0] = 4;
11312 record_buf_mem[1] = tgt_mem_addr;
11313 record_buf_mem[2] = 4;
11314 record_buf_mem[3] = tgt_mem_addr + 4;
11315 arm_insn_r->mem_rec_count = 2;
11316 }
72508ac0
PO
11317 }
11318 else if (12 == arm_insn_r->opcode || 8 == arm_insn_r->opcode)
11319 {
11320 /* 2) Store, register offset. */
11321 /* Get Rm. */
11322 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11323 /* Get Rn. */
11324 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11325 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11326 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11327 if (15 == reg_src2)
dda83cd7
SM
11328 {
11329 /* If R15 was used as Rn, hence current PC+8. */
11330 u_regval[0] = u_regval[0] + 8;
11331 }
72508ac0
PO
11332 /* Calculate target store address, Rn +/- Rm, register offset. */
11333 if (12 == arm_insn_r->opcode)
dda83cd7
SM
11334 {
11335 tgt_mem_addr = u_regval[0] + u_regval[1];
11336 }
72508ac0 11337 else
dda83cd7
SM
11338 {
11339 tgt_mem_addr = u_regval[1] - u_regval[0];
11340 }
72508ac0 11341 if (ARM_RECORD_STRH == str_type)
dda83cd7
SM
11342 {
11343 record_buf_mem[0] = 2;
11344 record_buf_mem[1] = tgt_mem_addr;
11345 arm_insn_r->mem_rec_count = 1;
11346 }
72508ac0 11347 else if (ARM_RECORD_STRD == str_type)
dda83cd7
SM
11348 {
11349 record_buf_mem[0] = 4;
11350 record_buf_mem[1] = tgt_mem_addr;
11351 record_buf_mem[2] = 4;
11352 record_buf_mem[3] = tgt_mem_addr + 4;
11353 arm_insn_r->mem_rec_count = 2;
11354 }
72508ac0
PO
11355 }
11356 else if (11 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
dda83cd7 11357 || 2 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
72508ac0
PO
11358 {
11359 /* 3) Store, immediate pre-indexed. */
11360 /* 5) Store, immediate post-indexed. */
11361 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
11362 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
11363 offset_8 = (immed_high << 4) | immed_low;
11364 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11365 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11366 /* Calculate target store address, Rn +/- Rm, register offset. */
11367 if (15 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
dda83cd7
SM
11368 {
11369 tgt_mem_addr = u_regval[0] + offset_8;
11370 }
72508ac0 11371 else
dda83cd7
SM
11372 {
11373 tgt_mem_addr = u_regval[0] - offset_8;
11374 }
72508ac0 11375 if (ARM_RECORD_STRH == str_type)
dda83cd7
SM
11376 {
11377 record_buf_mem[0] = 2;
11378 record_buf_mem[1] = tgt_mem_addr;
11379 arm_insn_r->mem_rec_count = 1;
11380 }
72508ac0 11381 else if (ARM_RECORD_STRD == str_type)
dda83cd7
SM
11382 {
11383 record_buf_mem[0] = 4;
11384 record_buf_mem[1] = tgt_mem_addr;
11385 record_buf_mem[2] = 4;
11386 record_buf_mem[3] = tgt_mem_addr + 4;
11387 arm_insn_r->mem_rec_count = 2;
11388 }
72508ac0
PO
11389 /* Record Rn also as it changes. */
11390 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
11391 arm_insn_r->reg_rec_count = 1;
11392 }
11393 else if (9 == arm_insn_r->opcode || 13 == arm_insn_r->opcode
dda83cd7 11394 || 0 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
72508ac0
PO
11395 {
11396 /* 4) Store, register pre-indexed. */
11397 /* 6) Store, register post -indexed. */
11398 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11399 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11400 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11401 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11402 /* Calculate target store address, Rn +/- Rm, register offset. */
11403 if (13 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
dda83cd7
SM
11404 {
11405 tgt_mem_addr = u_regval[0] + u_regval[1];
11406 }
72508ac0 11407 else
dda83cd7
SM
11408 {
11409 tgt_mem_addr = u_regval[1] - u_regval[0];
11410 }
72508ac0 11411 if (ARM_RECORD_STRH == str_type)
dda83cd7
SM
11412 {
11413 record_buf_mem[0] = 2;
11414 record_buf_mem[1] = tgt_mem_addr;
11415 arm_insn_r->mem_rec_count = 1;
11416 }
72508ac0 11417 else if (ARM_RECORD_STRD == str_type)
dda83cd7
SM
11418 {
11419 record_buf_mem[0] = 4;
11420 record_buf_mem[1] = tgt_mem_addr;
11421 record_buf_mem[2] = 4;
11422 record_buf_mem[3] = tgt_mem_addr + 4;
11423 arm_insn_r->mem_rec_count = 2;
11424 }
72508ac0
PO
11425 /* Record Rn also as it changes. */
11426 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
11427 arm_insn_r->reg_rec_count = 1;
11428 }
11429 return 0;
11430}
11431
11432/* Handling ARM extension space insns. */
11433
11434static int
4748a9be 11435arm_record_extension_space (arm_insn_decode_record *arm_insn_r)
72508ac0 11436{
df95a9cf 11437 int ret = 0; /* Return value: -1:record failure ; 0:success */
72508ac0
PO
11438 uint32_t opcode1 = 0, opcode2 = 0, insn_op1 = 0;
11439 uint32_t record_buf[8], record_buf_mem[8];
11440 uint32_t reg_src1 = 0;
72508ac0
PO
11441 struct regcache *reg_cache = arm_insn_r->regcache;
11442 ULONGEST u_regval = 0;
11443
11444 gdb_assert (!INSN_RECORDED(arm_insn_r));
11445 /* Handle unconditional insn extension space. */
11446
11447 opcode1 = bits (arm_insn_r->arm_insn, 20, 27);
11448 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
11449 if (arm_insn_r->cond)
11450 {
11451 /* PLD has no affect on architectural state, it just affects
dda83cd7 11452 the caches. */
72508ac0 11453 if (5 == ((opcode1 & 0xE0) >> 5))
dda83cd7
SM
11454 {
11455 /* BLX(1) */
11456 record_buf[0] = ARM_PS_REGNUM;
11457 record_buf[1] = ARM_LR_REGNUM;
11458 arm_insn_r->reg_rec_count = 2;
11459 }
72508ac0
PO
11460 /* STC2, LDC2, MCR2, MRC2, CDP2: <TBD>, co-processor insn. */
11461 }
11462
11463
11464 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
11465 if (3 == opcode1 && bit (arm_insn_r->arm_insn, 4))
11466 {
11467 ret = -1;
11468 /* Undefined instruction on ARM V5; need to handle if later
dda83cd7 11469 versions define it. */
72508ac0
PO
11470 }
11471
11472 opcode1 = bits (arm_insn_r->arm_insn, 24, 27);
11473 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
11474 insn_op1 = bits (arm_insn_r->arm_insn, 20, 23);
11475
11476 /* Handle arithmetic insn extension space. */
11477 if (!opcode1 && 9 == opcode2 && 1 != arm_insn_r->cond
11478 && !INSN_RECORDED(arm_insn_r))
11479 {
11480 /* Handle MLA(S) and MUL(S). */
b020ff80 11481 if (in_inclusive_range (insn_op1, 0U, 3U))
01add95b
SM
11482 {
11483 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11484 record_buf[1] = ARM_PS_REGNUM;
11485 arm_insn_r->reg_rec_count = 2;
11486 }
b020ff80 11487 else if (in_inclusive_range (insn_op1, 4U, 15U))
01add95b
SM
11488 {
11489 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
11490 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11491 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11492 record_buf[2] = ARM_PS_REGNUM;
11493 arm_insn_r->reg_rec_count = 3;
11494 }
72508ac0
PO
11495 }
11496
11497 opcode1 = bits (arm_insn_r->arm_insn, 26, 27);
11498 opcode2 = bits (arm_insn_r->arm_insn, 23, 24);
11499 insn_op1 = bits (arm_insn_r->arm_insn, 21, 22);
11500
11501 /* Handle control insn extension space. */
11502
11503 if (!opcode1 && 2 == opcode2 && !bit (arm_insn_r->arm_insn, 20)
11504 && 1 != arm_insn_r->cond && !INSN_RECORDED(arm_insn_r))
11505 {
11506 if (!bit (arm_insn_r->arm_insn,25))
dda83cd7
SM
11507 {
11508 if (!bits (arm_insn_r->arm_insn, 4, 7))
11509 {
11510 if ((0 == insn_op1) || (2 == insn_op1))
11511 {
11512 /* MRS. */
11513 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11514 arm_insn_r->reg_rec_count = 1;
11515 }
11516 else if (1 == insn_op1)
11517 {
11518 /* CSPR is going to be changed. */
11519 record_buf[0] = ARM_PS_REGNUM;
11520 arm_insn_r->reg_rec_count = 1;
11521 }
11522 else if (3 == insn_op1)
11523 {
11524 /* SPSR is going to be changed. */
11525 /* We need to get SPSR value, which is yet to be done. */
11526 return -1;
11527 }
11528 }
11529 else if (1 == bits (arm_insn_r->arm_insn, 4, 7))
11530 {
11531 if (1 == insn_op1)
11532 {
11533 /* BX. */
11534 record_buf[0] = ARM_PS_REGNUM;
11535 arm_insn_r->reg_rec_count = 1;
11536 }
11537 else if (3 == insn_op1)
11538 {
11539 /* CLZ. */
11540 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11541 arm_insn_r->reg_rec_count = 1;
11542 }
11543 }
11544 else if (3 == bits (arm_insn_r->arm_insn, 4, 7))
11545 {
11546 /* BLX. */
11547 record_buf[0] = ARM_PS_REGNUM;
11548 record_buf[1] = ARM_LR_REGNUM;
11549 arm_insn_r->reg_rec_count = 2;
11550 }
11551 else if (5 == bits (arm_insn_r->arm_insn, 4, 7))
11552 {
11553 /* QADD, QSUB, QDADD, QDSUB */
11554 record_buf[0] = ARM_PS_REGNUM;
11555 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11556 arm_insn_r->reg_rec_count = 2;
11557 }
11558 else if (7 == bits (arm_insn_r->arm_insn, 4, 7))
11559 {
11560 /* BKPT. */
11561 record_buf[0] = ARM_PS_REGNUM;
11562 record_buf[1] = ARM_LR_REGNUM;
11563 arm_insn_r->reg_rec_count = 2;
11564
11565 /* Save SPSR also;how? */
11566 return -1;
11567 }
11568 else if(8 == bits (arm_insn_r->arm_insn, 4, 7)
11569 || 10 == bits (arm_insn_r->arm_insn, 4, 7)
11570 || 12 == bits (arm_insn_r->arm_insn, 4, 7)
11571 || 14 == bits (arm_insn_r->arm_insn, 4, 7)
11572 )
11573 {
11574 if (0 == insn_op1 || 1 == insn_op1)
11575 {
11576 /* SMLA<x><y>, SMLAW<y>, SMULW<y>. */
11577 /* We dont do optimization for SMULW<y> where we
11578 need only Rd. */
11579 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11580 record_buf[1] = ARM_PS_REGNUM;
11581 arm_insn_r->reg_rec_count = 2;
11582 }
11583 else if (2 == insn_op1)
11584 {
11585 /* SMLAL<x><y>. */
11586 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11587 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
11588 arm_insn_r->reg_rec_count = 2;
11589 }
11590 else if (3 == insn_op1)
11591 {
11592 /* SMUL<x><y>. */
11593 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11594 arm_insn_r->reg_rec_count = 1;
11595 }
11596 }
11597 }
72508ac0 11598 else
dda83cd7
SM
11599 {
11600 /* MSR : immediate form. */
11601 if (1 == insn_op1)
11602 {
11603 /* CSPR is going to be changed. */
11604 record_buf[0] = ARM_PS_REGNUM;
11605 arm_insn_r->reg_rec_count = 1;
11606 }
11607 else if (3 == insn_op1)
11608 {
11609 /* SPSR is going to be changed. */
11610 /* we need to get SPSR value, which is yet to be done */
11611 return -1;
11612 }
11613 }
72508ac0
PO
11614 }
11615
11616 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
11617 opcode2 = bits (arm_insn_r->arm_insn, 20, 24);
11618 insn_op1 = bits (arm_insn_r->arm_insn, 5, 6);
11619
11620 /* Handle load/store insn extension space. */
11621
11622 if (!opcode1 && bit (arm_insn_r->arm_insn, 7)
11623 && bit (arm_insn_r->arm_insn, 4) && 1 != arm_insn_r->cond
11624 && !INSN_RECORDED(arm_insn_r))
11625 {
11626 /* SWP/SWPB. */
11627 if (0 == insn_op1)
dda83cd7
SM
11628 {
11629 /* These insn, changes register and memory as well. */
11630 /* SWP or SWPB insn. */
11631 /* Get memory address given by Rn. */
11632 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11633 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11634 /* SWP insn ?, swaps word. */
11635 if (8 == arm_insn_r->opcode)
11636 {
11637 record_buf_mem[0] = 4;
11638 }
11639 else
11640 {
11641 /* SWPB insn, swaps only byte. */
11642 record_buf_mem[0] = 1;
11643 }
11644 record_buf_mem[1] = u_regval;
11645 arm_insn_r->mem_rec_count = 1;
11646 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11647 arm_insn_r->reg_rec_count = 1;
11648 }
72508ac0 11649 else if (1 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
dda83cd7
SM
11650 {
11651 /* STRH. */
11652 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11653 ARM_RECORD_STRH);
11654 }
72508ac0 11655 else if (2 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
dda83cd7
SM
11656 {
11657 /* LDRD. */
11658 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11659 record_buf[1] = record_buf[0] + 1;
11660 arm_insn_r->reg_rec_count = 2;
11661 }
72508ac0 11662 else if (3 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
dda83cd7
SM
11663 {
11664 /* STRD. */
11665 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11666 ARM_RECORD_STRD);
11667 }
72508ac0 11668 else if (bit (arm_insn_r->arm_insn, 20) && insn_op1 <= 3)
dda83cd7
SM
11669 {
11670 /* LDRH, LDRSB, LDRSH. */
11671 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11672 arm_insn_r->reg_rec_count = 1;
11673 }
72508ac0
PO
11674
11675 }
11676
11677 opcode1 = bits (arm_insn_r->arm_insn, 23, 27);
11678 if (24 == opcode1 && bit (arm_insn_r->arm_insn, 21)
11679 && !INSN_RECORDED(arm_insn_r))
11680 {
11681 ret = -1;
11682 /* Handle coprocessor insn extension space. */
11683 }
11684
11685 /* To be done for ARMv5 and later; as of now we return -1. */
11686 if (-1 == ret)
ca92db2d 11687 return ret;
72508ac0
PO
11688
11689 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11690 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11691
11692 return ret;
11693}
11694
11695/* Handling opcode 000 insns. */
11696
11697static int
4748a9be 11698arm_record_data_proc_misc_ld_str (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
11699{
11700 struct regcache *reg_cache = arm_insn_r->regcache;
11701 uint32_t record_buf[8], record_buf_mem[8];
11702 ULONGEST u_regval[2] = {0};
11703
8d49165d 11704 uint32_t reg_src1 = 0;
72508ac0
PO
11705 uint32_t opcode1 = 0;
11706
11707 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11708 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11709 opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
11710
2d9e6acb 11711 if (!((opcode1 & 0x19) == 0x10))
72508ac0 11712 {
2d9e6acb
YQ
11713 /* Data-processing (register) and Data-processing (register-shifted
11714 register */
11715 /* Out of 11 shifter operands mode, all the insn modifies destination
11716 register, which is specified by 13-16 decode. */
11717 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11718 record_buf[1] = ARM_PS_REGNUM;
11719 arm_insn_r->reg_rec_count = 2;
72508ac0 11720 }
2d9e6acb 11721 else if ((arm_insn_r->decode < 8) && ((opcode1 & 0x19) == 0x10))
72508ac0 11722 {
2d9e6acb
YQ
11723 /* Miscellaneous instructions */
11724
11725 if (3 == arm_insn_r->decode && 0x12 == opcode1
11726 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11727 {
11728 /* Handle BLX, branch and link/exchange. */
11729 if (9 == arm_insn_r->opcode)
11730 {
11731 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm,
11732 and R14 stores the return address. */
11733 record_buf[0] = ARM_PS_REGNUM;
11734 record_buf[1] = ARM_LR_REGNUM;
11735 arm_insn_r->reg_rec_count = 2;
11736 }
11737 }
11738 else if (7 == arm_insn_r->decode && 0x12 == opcode1)
11739 {
11740 /* Handle enhanced software breakpoint insn, BKPT. */
11741 /* CPSR is changed to be executed in ARM state, disabling normal
11742 interrupts, entering abort mode. */
11743 /* According to high vector configuration PC is set. */
11744 /* user hit breakpoint and type reverse, in
11745 that case, we need to go back with previous CPSR and
11746 Program Counter. */
11747 record_buf[0] = ARM_PS_REGNUM;
11748 record_buf[1] = ARM_LR_REGNUM;
11749 arm_insn_r->reg_rec_count = 2;
11750
11751 /* Save SPSR also; how? */
11752 return -1;
11753 }
11754 else if (1 == arm_insn_r->decode && 0x12 == opcode1
11755 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11756 {
11757 /* Handle BX, branch and link/exchange. */
11758 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm. */
11759 record_buf[0] = ARM_PS_REGNUM;
11760 arm_insn_r->reg_rec_count = 1;
11761 }
11762 else if (1 == arm_insn_r->decode && 0x16 == opcode1
11763 && sbo_sbz (arm_insn_r->arm_insn, 9, 4, 1)
11764 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1))
11765 {
11766 /* Count leading zeros: CLZ. */
11767 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11768 arm_insn_r->reg_rec_count = 1;
11769 }
11770 else if (!bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11771 && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11772 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1)
11773 && sbo_sbz (arm_insn_r->arm_insn, 1, 12, 0))
11774 {
11775 /* Handle MRS insn. */
11776 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11777 arm_insn_r->reg_rec_count = 1;
11778 }
72508ac0 11779 }
2d9e6acb 11780 else if (9 == arm_insn_r->decode && opcode1 < 0x10)
72508ac0 11781 {
2d9e6acb
YQ
11782 /* Multiply and multiply-accumulate */
11783
11784 /* Handle multiply instructions. */
11785 /* MLA, MUL, SMLAL, SMULL, UMLAL, UMULL. */
11786 if (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)
f2a883a8
SM
11787 {
11788 /* Handle MLA and MUL. */
11789 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11790 record_buf[1] = ARM_PS_REGNUM;
11791 arm_insn_r->reg_rec_count = 2;
11792 }
11793 else if (4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11794 {
11795 /* Handle SMLAL, SMULL, UMLAL, UMULL. */
11796 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11797 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11798 record_buf[2] = ARM_PS_REGNUM;
11799 arm_insn_r->reg_rec_count = 3;
11800 }
2d9e6acb
YQ
11801 }
11802 else if (9 == arm_insn_r->decode && opcode1 > 0x10)
11803 {
11804 /* Synchronization primitives */
11805
72508ac0
PO
11806 /* Handling SWP, SWPB. */
11807 /* These insn, changes register and memory as well. */
11808 /* SWP or SWPB insn. */
11809
11810 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11811 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11812 /* SWP insn ?, swaps word. */
11813 if (8 == arm_insn_r->opcode)
2d9e6acb
YQ
11814 {
11815 record_buf_mem[0] = 4;
11816 }
11817 else
11818 {
11819 /* SWPB insn, swaps only byte. */
11820 record_buf_mem[0] = 1;
11821 }
72508ac0
PO
11822 record_buf_mem[1] = u_regval[0];
11823 arm_insn_r->mem_rec_count = 1;
11824 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11825 arm_insn_r->reg_rec_count = 1;
11826 }
2d9e6acb
YQ
11827 else if (11 == arm_insn_r->decode || 13 == arm_insn_r->decode
11828 || 15 == arm_insn_r->decode)
72508ac0 11829 {
2d9e6acb
YQ
11830 if ((opcode1 & 0x12) == 2)
11831 {
11832 /* Extra load/store (unprivileged) */
11833 return -1;
11834 }
11835 else
11836 {
11837 /* Extra load/store */
11838 switch (bits (arm_insn_r->arm_insn, 5, 6))
11839 {
11840 case 1:
11841 if ((opcode1 & 0x05) == 0x0 || (opcode1 & 0x05) == 0x4)
11842 {
11843 /* STRH (register), STRH (immediate) */
11844 arm_record_strx (arm_insn_r, &record_buf[0],
11845 &record_buf_mem[0], ARM_RECORD_STRH);
11846 }
11847 else if ((opcode1 & 0x05) == 0x1)
11848 {
11849 /* LDRH (register) */
11850 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11851 arm_insn_r->reg_rec_count = 1;
72508ac0 11852
2d9e6acb
YQ
11853 if (bit (arm_insn_r->arm_insn, 21))
11854 {
11855 /* Write back to Rn. */
11856 record_buf[arm_insn_r->reg_rec_count++]
11857 = bits (arm_insn_r->arm_insn, 16, 19);
11858 }
11859 }
11860 else if ((opcode1 & 0x05) == 0x5)
11861 {
11862 /* LDRH (immediate), LDRH (literal) */
11863 int rn = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0 11864
2d9e6acb
YQ
11865 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11866 arm_insn_r->reg_rec_count = 1;
11867
11868 if (rn != 15)
11869 {
11870 /*LDRH (immediate) */
11871 if (bit (arm_insn_r->arm_insn, 21))
11872 {
11873 /* Write back to Rn. */
11874 record_buf[arm_insn_r->reg_rec_count++] = rn;
11875 }
11876 }
11877 }
11878 else
11879 return -1;
11880 break;
11881 case 2:
11882 if ((opcode1 & 0x05) == 0x0)
11883 {
11884 /* LDRD (register) */
11885 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11886 record_buf[1] = record_buf[0] + 1;
11887 arm_insn_r->reg_rec_count = 2;
11888
11889 if (bit (arm_insn_r->arm_insn, 21))
11890 {
11891 /* Write back to Rn. */
11892 record_buf[arm_insn_r->reg_rec_count++]
11893 = bits (arm_insn_r->arm_insn, 16, 19);
11894 }
11895 }
11896 else if ((opcode1 & 0x05) == 0x1)
11897 {
11898 /* LDRSB (register) */
11899 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11900 arm_insn_r->reg_rec_count = 1;
11901
11902 if (bit (arm_insn_r->arm_insn, 21))
11903 {
11904 /* Write back to Rn. */
11905 record_buf[arm_insn_r->reg_rec_count++]
11906 = bits (arm_insn_r->arm_insn, 16, 19);
11907 }
11908 }
11909 else if ((opcode1 & 0x05) == 0x4 || (opcode1 & 0x05) == 0x5)
11910 {
11911 /* LDRD (immediate), LDRD (literal), LDRSB (immediate),
11912 LDRSB (literal) */
11913 int rn = bits (arm_insn_r->arm_insn, 16, 19);
11914
11915 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11916 arm_insn_r->reg_rec_count = 1;
11917
11918 if (rn != 15)
11919 {
11920 /*LDRD (immediate), LDRSB (immediate) */
11921 if (bit (arm_insn_r->arm_insn, 21))
11922 {
11923 /* Write back to Rn. */
11924 record_buf[arm_insn_r->reg_rec_count++] = rn;
11925 }
11926 }
11927 }
11928 else
11929 return -1;
11930 break;
11931 case 3:
11932 if ((opcode1 & 0x05) == 0x0)
11933 {
11934 /* STRD (register) */
11935 arm_record_strx (arm_insn_r, &record_buf[0],
11936 &record_buf_mem[0], ARM_RECORD_STRD);
11937 }
11938 else if ((opcode1 & 0x05) == 0x1)
11939 {
11940 /* LDRSH (register) */
11941 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11942 arm_insn_r->reg_rec_count = 1;
11943
11944 if (bit (arm_insn_r->arm_insn, 21))
11945 {
11946 /* Write back to Rn. */
11947 record_buf[arm_insn_r->reg_rec_count++]
11948 = bits (arm_insn_r->arm_insn, 16, 19);
11949 }
11950 }
11951 else if ((opcode1 & 0x05) == 0x4)
11952 {
11953 /* STRD (immediate) */
11954 arm_record_strx (arm_insn_r, &record_buf[0],
11955 &record_buf_mem[0], ARM_RECORD_STRD);
11956 }
11957 else if ((opcode1 & 0x05) == 0x5)
11958 {
11959 /* LDRSH (immediate), LDRSH (literal) */
11960 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11961 arm_insn_r->reg_rec_count = 1;
11962
11963 if (bit (arm_insn_r->arm_insn, 21))
11964 {
11965 /* Write back to Rn. */
11966 record_buf[arm_insn_r->reg_rec_count++]
11967 = bits (arm_insn_r->arm_insn, 16, 19);
11968 }
11969 }
11970 else
11971 return -1;
11972 break;
11973 default:
11974 return -1;
11975 }
11976 }
72508ac0
PO
11977 }
11978 else
11979 {
11980 return -1;
11981 }
11982
11983 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11984 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11985 return 0;
11986}
11987
11988/* Handling opcode 001 insns. */
11989
11990static int
4748a9be 11991arm_record_data_proc_imm (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
11992{
11993 uint32_t record_buf[8], record_buf_mem[8];
11994
11995 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11996 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11997
11998 if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11999 && 2 == bits (arm_insn_r->arm_insn, 20, 21)
12000 && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
12001 )
12002 {
12003 /* Handle MSR insn. */
12004 if (9 == arm_insn_r->opcode)
dda83cd7
SM
12005 {
12006 /* CSPR is going to be changed. */
12007 record_buf[0] = ARM_PS_REGNUM;
12008 arm_insn_r->reg_rec_count = 1;
12009 }
72508ac0 12010 else
dda83cd7
SM
12011 {
12012 /* SPSR is going to be changed. */
12013 }
72508ac0
PO
12014 }
12015 else if (arm_insn_r->opcode <= 15)
12016 {
12017 /* Normal data processing insns. */
12018 /* Out of 11 shifter operands mode, all the insn modifies destination
dda83cd7 12019 register, which is specified by 13-16 decode. */
72508ac0
PO
12020 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12021 record_buf[1] = ARM_PS_REGNUM;
12022 arm_insn_r->reg_rec_count = 2;
12023 }
12024 else
12025 {
12026 return -1;
12027 }
12028
12029 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12030 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12031 return 0;
12032}
12033
c55978a6 12034static int
4748a9be 12035arm_record_media (arm_insn_decode_record *arm_insn_r)
c55978a6
YQ
12036{
12037 uint32_t record_buf[8];
12038
12039 switch (bits (arm_insn_r->arm_insn, 22, 24))
12040 {
12041 case 0:
12042 /* Parallel addition and subtraction, signed */
12043 case 1:
12044 /* Parallel addition and subtraction, unsigned */
12045 case 2:
12046 case 3:
12047 /* Packing, unpacking, saturation and reversal */
12048 {
12049 int rd = bits (arm_insn_r->arm_insn, 12, 15);
12050
12051 record_buf[arm_insn_r->reg_rec_count++] = rd;
12052 }
12053 break;
12054
12055 case 4:
12056 case 5:
12057 /* Signed multiplies */
12058 {
12059 int rd = bits (arm_insn_r->arm_insn, 16, 19);
12060 unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 22);
12061
12062 record_buf[arm_insn_r->reg_rec_count++] = rd;
12063 if (op1 == 0x0)
12064 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
12065 else if (op1 == 0x4)
12066 record_buf[arm_insn_r->reg_rec_count++]
12067 = bits (arm_insn_r->arm_insn, 12, 15);
12068 }
12069 break;
12070
12071 case 6:
12072 {
12073 if (bit (arm_insn_r->arm_insn, 21)
12074 && bits (arm_insn_r->arm_insn, 5, 6) == 0x2)
12075 {
12076 /* SBFX */
12077 record_buf[arm_insn_r->reg_rec_count++]
12078 = bits (arm_insn_r->arm_insn, 12, 15);
12079 }
12080 else if (bits (arm_insn_r->arm_insn, 20, 21) == 0x0
12081 && bits (arm_insn_r->arm_insn, 5, 7) == 0x0)
12082 {
12083 /* USAD8 and USADA8 */
12084 record_buf[arm_insn_r->reg_rec_count++]
12085 = bits (arm_insn_r->arm_insn, 16, 19);
12086 }
12087 }
12088 break;
12089
12090 case 7:
12091 {
12092 if (bits (arm_insn_r->arm_insn, 20, 21) == 0x3
12093 && bits (arm_insn_r->arm_insn, 5, 7) == 0x7)
12094 {
12095 /* Permanently UNDEFINED */
12096 return -1;
12097 }
12098 else
12099 {
12100 /* BFC, BFI and UBFX */
12101 record_buf[arm_insn_r->reg_rec_count++]
12102 = bits (arm_insn_r->arm_insn, 12, 15);
12103 }
12104 }
12105 break;
12106
12107 default:
12108 return -1;
12109 }
12110
12111 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12112
12113 return 0;
12114}
12115
71e396f9 12116/* Handle ARM mode instructions with opcode 010. */
72508ac0
PO
12117
12118static int
4748a9be 12119arm_record_ld_st_imm_offset (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
12120{
12121 struct regcache *reg_cache = arm_insn_r->regcache;
12122
71e396f9
LM
12123 uint32_t reg_base , reg_dest;
12124 uint32_t offset_12, tgt_mem_addr;
72508ac0 12125 uint32_t record_buf[8], record_buf_mem[8];
71e396f9
LM
12126 unsigned char wback;
12127 ULONGEST u_regval;
72508ac0 12128
71e396f9
LM
12129 /* Calculate wback. */
12130 wback = (bit (arm_insn_r->arm_insn, 24) == 0)
12131 || (bit (arm_insn_r->arm_insn, 21) == 1);
72508ac0 12132
71e396f9
LM
12133 arm_insn_r->reg_rec_count = 0;
12134 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0
PO
12135
12136 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12137 {
71e396f9
LM
12138 /* LDR (immediate), LDR (literal), LDRB (immediate), LDRB (literal), LDRBT
12139 and LDRT. */
12140
72508ac0 12141 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
71e396f9
LM
12142 record_buf[arm_insn_r->reg_rec_count++] = reg_dest;
12143
12144 /* The LDR instruction is capable of doing branching. If MOV LR, PC
12145 preceeds a LDR instruction having R15 as reg_base, it
12146 emulates a branch and link instruction, and hence we need to save
12147 CPSR and PC as well. */
12148 if (ARM_PC_REGNUM == reg_dest)
12149 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
12150
12151 /* If wback is true, also save the base register, which is going to be
12152 written to. */
12153 if (wback)
12154 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
12155 }
12156 else
12157 {
71e396f9
LM
12158 /* STR (immediate), STRB (immediate), STRBT and STRT. */
12159
72508ac0 12160 offset_12 = bits (arm_insn_r->arm_insn, 0, 11);
71e396f9
LM
12161 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
12162
12163 /* Handle bit U. */
72508ac0 12164 if (bit (arm_insn_r->arm_insn, 23))
71e396f9
LM
12165 {
12166 /* U == 1: Add the offset. */
12167 tgt_mem_addr = (uint32_t) u_regval + offset_12;
12168 }
72508ac0 12169 else
71e396f9
LM
12170 {
12171 /* U == 0: subtract the offset. */
12172 tgt_mem_addr = (uint32_t) u_regval - offset_12;
12173 }
12174
12175 /* Bit 22 tells us whether the store instruction writes 1 byte or 4
12176 bytes. */
12177 if (bit (arm_insn_r->arm_insn, 22))
12178 {
12179 /* STRB and STRBT: 1 byte. */
12180 record_buf_mem[0] = 1;
12181 }
12182 else
12183 {
12184 /* STR and STRT: 4 bytes. */
12185 record_buf_mem[0] = 4;
12186 }
12187
12188 /* Handle bit P. */
12189 if (bit (arm_insn_r->arm_insn, 24))
12190 record_buf_mem[1] = tgt_mem_addr;
12191 else
12192 record_buf_mem[1] = (uint32_t) u_regval;
72508ac0 12193
72508ac0
PO
12194 arm_insn_r->mem_rec_count = 1;
12195
71e396f9
LM
12196 /* If wback is true, also save the base register, which is going to be
12197 written to. */
12198 if (wback)
12199 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
12200 }
12201
12202 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12203 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12204 return 0;
12205}
12206
12207/* Handling opcode 011 insns. */
12208
12209static int
4748a9be 12210arm_record_ld_st_reg_offset (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
12211{
12212 struct regcache *reg_cache = arm_insn_r->regcache;
12213
12214 uint32_t shift_imm = 0;
12215 uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
12216 uint32_t offset_12 = 0, tgt_mem_addr = 0;
12217 uint32_t record_buf[8], record_buf_mem[8];
12218
12219 LONGEST s_word;
12220 ULONGEST u_regval[2];
12221
c55978a6
YQ
12222 if (bit (arm_insn_r->arm_insn, 4))
12223 return arm_record_media (arm_insn_r);
12224
72508ac0
PO
12225 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
12226 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
12227
12228 /* Handle enhanced store insns and LDRD DSP insn,
12229 order begins according to addressing modes for store insns
12230 STRH insn. */
12231
12232 /* LDR or STR? */
12233 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12234 {
12235 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
12236 /* LDR insn has a capability to do branching, if
dda83cd7
SM
12237 MOV LR, PC is preceded by LDR insn having Rn as R15
12238 in that case, it emulates branch and link insn, and hence we
12239 need to save CSPR and PC as well. */
72508ac0 12240 if (15 != reg_dest)
dda83cd7
SM
12241 {
12242 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12243 arm_insn_r->reg_rec_count = 1;
12244 }
72508ac0 12245 else
dda83cd7
SM
12246 {
12247 record_buf[0] = reg_dest;
12248 record_buf[1] = ARM_PS_REGNUM;
12249 arm_insn_r->reg_rec_count = 2;
12250 }
72508ac0
PO
12251 }
12252 else
12253 {
12254 if (! bits (arm_insn_r->arm_insn, 4, 11))
dda83cd7
SM
12255 {
12256 /* Store insn, register offset and register pre-indexed,
12257 register post-indexed. */
12258 /* Get Rm. */
12259 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
12260 /* Get Rn. */
12261 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
12262 regcache_raw_read_unsigned (reg_cache, reg_src1
12263 , &u_regval[0]);
12264 regcache_raw_read_unsigned (reg_cache, reg_src2
12265 , &u_regval[1]);
12266 if (15 == reg_src2)
12267 {
12268 /* If R15 was used as Rn, hence current PC+8. */
12269 /* Pre-indexed mode doesnt reach here ; illegal insn. */
12270 u_regval[0] = u_regval[0] + 8;
12271 }
12272 /* Calculate target store address, Rn +/- Rm, register offset. */
12273 /* U == 1. */
12274 if (bit (arm_insn_r->arm_insn, 23))
12275 {
12276 tgt_mem_addr = u_regval[0] + u_regval[1];
12277 }
12278 else
12279 {
12280 tgt_mem_addr = u_regval[1] - u_regval[0];
12281 }
12282
12283 switch (arm_insn_r->opcode)
12284 {
12285 /* STR. */
12286 case 8:
12287 case 12:
12288 /* STR. */
12289 case 9:
12290 case 13:
12291 /* STRT. */
12292 case 1:
12293 case 5:
12294 /* STR. */
12295 case 0:
12296 case 4:
12297 record_buf_mem[0] = 4;
12298 break;
12299
12300 /* STRB. */
12301 case 10:
12302 case 14:
12303 /* STRB. */
12304 case 11:
12305 case 15:
12306 /* STRBT. */
12307 case 3:
12308 case 7:
12309 /* STRB. */
12310 case 2:
12311 case 6:
12312 record_buf_mem[0] = 1;
12313 break;
12314
12315 default:
12316 gdb_assert_not_reached ("no decoding pattern found");
12317 break;
12318 }
12319 record_buf_mem[1] = tgt_mem_addr;
12320 arm_insn_r->mem_rec_count = 1;
12321
12322 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
12323 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
12324 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
12325 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
12326 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
12327 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
12328 )
12329 {
12330 /* Rn is going to be changed in pre-indexed mode and
12331 post-indexed mode as well. */
12332 record_buf[0] = reg_src2;
12333 arm_insn_r->reg_rec_count = 1;
12334 }
12335 }
72508ac0 12336 else
dda83cd7
SM
12337 {
12338 /* Store insn, scaled register offset; scaled pre-indexed. */
12339 offset_12 = bits (arm_insn_r->arm_insn, 5, 6);
12340 /* Get Rm. */
12341 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
12342 /* Get Rn. */
12343 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
12344 /* Get shift_imm. */
12345 shift_imm = bits (arm_insn_r->arm_insn, 7, 11);
12346 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
12347 regcache_raw_read_signed (reg_cache, reg_src1, &s_word);
12348 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
12349 /* Offset_12 used as shift. */
12350 switch (offset_12)
12351 {
12352 case 0:
12353 /* Offset_12 used as index. */
12354 offset_12 = u_regval[0] << shift_imm;
12355 break;
12356
12357 case 1:
05d63baf 12358 offset_12 = (!shift_imm) ? 0 : u_regval[0] >> shift_imm;
dda83cd7
SM
12359 break;
12360
12361 case 2:
12362 if (!shift_imm)
12363 {
12364 if (bit (u_regval[0], 31))
12365 {
12366 offset_12 = 0xFFFFFFFF;
12367 }
12368 else
12369 {
12370 offset_12 = 0;
12371 }
12372 }
12373 else
12374 {
12375 /* This is arithmetic shift. */
12376 offset_12 = s_word >> shift_imm;
12377 }
12378 break;
12379
12380 case 3:
12381 if (!shift_imm)
12382 {
12383 regcache_raw_read_unsigned (reg_cache, ARM_PS_REGNUM,
12384 &u_regval[1]);
12385 /* Get C flag value and shift it by 31. */
12386 offset_12 = (((bit (u_regval[1], 29)) << 31) \
12387 | (u_regval[0]) >> 1);
12388 }
12389 else
12390 {
12391 offset_12 = (u_regval[0] >> shift_imm) \
12392 | (u_regval[0] <<
12393 (sizeof(uint32_t) - shift_imm));
12394 }
12395 break;
12396
12397 default:
12398 gdb_assert_not_reached ("no decoding pattern found");
12399 break;
12400 }
12401
12402 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
12403 /* bit U set. */
12404 if (bit (arm_insn_r->arm_insn, 23))
12405 {
12406 tgt_mem_addr = u_regval[1] + offset_12;
12407 }
12408 else
12409 {
12410 tgt_mem_addr = u_regval[1] - offset_12;
12411 }
12412
12413 switch (arm_insn_r->opcode)
12414 {
12415 /* STR. */
12416 case 8:
12417 case 12:
12418 /* STR. */
12419 case 9:
12420 case 13:
12421 /* STRT. */
12422 case 1:
12423 case 5:
12424 /* STR. */
12425 case 0:
12426 case 4:
12427 record_buf_mem[0] = 4;
12428 break;
12429
12430 /* STRB. */
12431 case 10:
12432 case 14:
12433 /* STRB. */
12434 case 11:
12435 case 15:
12436 /* STRBT. */
12437 case 3:
12438 case 7:
12439 /* STRB. */
12440 case 2:
12441 case 6:
12442 record_buf_mem[0] = 1;
12443 break;
12444
12445 default:
12446 gdb_assert_not_reached ("no decoding pattern found");
12447 break;
12448 }
12449 record_buf_mem[1] = tgt_mem_addr;
12450 arm_insn_r->mem_rec_count = 1;
12451
12452 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
12453 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
12454 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
12455 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
12456 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
12457 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
12458 )
12459 {
12460 /* Rn is going to be changed in register scaled pre-indexed
12461 mode,and scaled post indexed mode. */
12462 record_buf[0] = reg_src2;
12463 arm_insn_r->reg_rec_count = 1;
12464 }
12465 }
72508ac0
PO
12466 }
12467
12468 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12469 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12470 return 0;
12471}
12472
71e396f9 12473/* Handle ARM mode instructions with opcode 100. */
72508ac0
PO
12474
12475static int
4748a9be 12476arm_record_ld_st_multiple (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
12477{
12478 struct regcache *reg_cache = arm_insn_r->regcache;
71e396f9
LM
12479 uint32_t register_count = 0, register_bits;
12480 uint32_t reg_base, addr_mode;
72508ac0 12481 uint32_t record_buf[24], record_buf_mem[48];
71e396f9
LM
12482 uint32_t wback;
12483 ULONGEST u_regval;
72508ac0 12484
71e396f9
LM
12485 /* Fetch the list of registers. */
12486 register_bits = bits (arm_insn_r->arm_insn, 0, 15);
12487 arm_insn_r->reg_rec_count = 0;
12488
12489 /* Fetch the base register that contains the address we are loading data
12490 to. */
12491 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0 12492
71e396f9
LM
12493 /* Calculate wback. */
12494 wback = (bit (arm_insn_r->arm_insn, 21) == 1);
72508ac0
PO
12495
12496 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12497 {
71e396f9 12498 /* LDM/LDMIA/LDMFD, LDMDA/LDMFA, LDMDB and LDMIB. */
72508ac0 12499
71e396f9 12500 /* Find out which registers are going to be loaded from memory. */
72508ac0 12501 while (register_bits)
71e396f9
LM
12502 {
12503 if (register_bits & 0x00000001)
12504 record_buf[arm_insn_r->reg_rec_count++] = register_count;
12505 register_bits = register_bits >> 1;
12506 register_count++;
12507 }
72508ac0 12508
71e396f9
LM
12509
12510 /* If wback is true, also save the base register, which is going to be
12511 written to. */
12512 if (wback)
12513 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
12514
12515 /* Save the CPSR register. */
12516 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
72508ac0
PO
12517 }
12518 else
12519 {
71e396f9 12520 /* STM (STMIA, STMEA), STMDA (STMED), STMDB (STMFD) and STMIB (STMFA). */
72508ac0 12521
71e396f9
LM
12522 addr_mode = bits (arm_insn_r->arm_insn, 23, 24);
12523
12524 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
12525
12526 /* Find out how many registers are going to be stored to memory. */
72508ac0 12527 while (register_bits)
71e396f9
LM
12528 {
12529 if (register_bits & 0x00000001)
12530 register_count++;
12531 register_bits = register_bits >> 1;
12532 }
72508ac0
PO
12533
12534 switch (addr_mode)
71e396f9
LM
12535 {
12536 /* STMDA (STMED): Decrement after. */
12537 case 0:
12538 record_buf_mem[1] = (uint32_t) u_regval
f0452268 12539 - register_count * ARM_INT_REGISTER_SIZE + 4;
71e396f9
LM
12540 break;
12541 /* STM (STMIA, STMEA): Increment after. */
12542 case 1:
12543 record_buf_mem[1] = (uint32_t) u_regval;
12544 break;
12545 /* STMDB (STMFD): Decrement before. */
12546 case 2:
12547 record_buf_mem[1] = (uint32_t) u_regval
f0452268 12548 - register_count * ARM_INT_REGISTER_SIZE;
71e396f9
LM
12549 break;
12550 /* STMIB (STMFA): Increment before. */
12551 case 3:
f0452268 12552 record_buf_mem[1] = (uint32_t) u_regval + ARM_INT_REGISTER_SIZE;
71e396f9
LM
12553 break;
12554 default:
12555 gdb_assert_not_reached ("no decoding pattern found");
12556 break;
12557 }
72508ac0 12558
f0452268 12559 record_buf_mem[0] = register_count * ARM_INT_REGISTER_SIZE;
71e396f9
LM
12560 arm_insn_r->mem_rec_count = 1;
12561
12562 /* If wback is true, also save the base register, which is going to be
12563 written to. */
12564 if (wback)
12565 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
12566 }
12567
12568 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12569 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12570 return 0;
12571}
12572
12573/* Handling opcode 101 insns. */
12574
12575static int
4748a9be 12576arm_record_b_bl (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
12577{
12578 uint32_t record_buf[8];
12579
12580 /* Handle B, BL, BLX(1) insns. */
12581 /* B simply branches so we do nothing here. */
12582 /* Note: BLX(1) doesnt fall here but instead it falls into
12583 extension space. */
12584 if (bit (arm_insn_r->arm_insn, 24))
01add95b
SM
12585 {
12586 record_buf[0] = ARM_LR_REGNUM;
12587 arm_insn_r->reg_rec_count = 1;
12588 }
72508ac0
PO
12589
12590 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12591
12592 return 0;
12593}
12594
72508ac0 12595static int
4748a9be 12596arm_record_unsupported_insn (arm_insn_decode_record *arm_insn_r)
72508ac0 12597{
6cb06a8c
TT
12598 gdb_printf (gdb_stderr,
12599 _("Process record does not support instruction "
12600 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
12601 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
72508ac0
PO
12602
12603 return -1;
12604}
12605
5a578da5
OJ
12606/* Record handler for vector data transfer instructions. */
12607
12608static int
4748a9be 12609arm_record_vdata_transfer_insn (arm_insn_decode_record *arm_insn_r)
5a578da5
OJ
12610{
12611 uint32_t bits_a, bit_c, bit_l, reg_t, reg_v;
12612 uint32_t record_buf[4];
12613
5a578da5
OJ
12614 reg_t = bits (arm_insn_r->arm_insn, 12, 15);
12615 reg_v = bits (arm_insn_r->arm_insn, 21, 23);
12616 bits_a = bits (arm_insn_r->arm_insn, 21, 23);
12617 bit_l = bit (arm_insn_r->arm_insn, 20);
12618 bit_c = bit (arm_insn_r->arm_insn, 8);
12619
12620 /* Handle VMOV instruction. */
12621 if (bit_l && bit_c)
12622 {
12623 record_buf[0] = reg_t;
12624 arm_insn_r->reg_rec_count = 1;
12625 }
12626 else if (bit_l && !bit_c)
12627 {
12628 /* Handle VMOV instruction. */
12629 if (bits_a == 0x00)
dda83cd7 12630 {
f1771dce 12631 record_buf[0] = reg_t;
dda83cd7
SM
12632 arm_insn_r->reg_rec_count = 1;
12633 }
5a578da5
OJ
12634 /* Handle VMRS instruction. */
12635 else if (bits_a == 0x07)
dda83cd7
SM
12636 {
12637 if (reg_t == 15)
12638 reg_t = ARM_PS_REGNUM;
5a578da5 12639
dda83cd7
SM
12640 record_buf[0] = reg_t;
12641 arm_insn_r->reg_rec_count = 1;
12642 }
5a578da5
OJ
12643 }
12644 else if (!bit_l && !bit_c)
12645 {
12646 /* Handle VMOV instruction. */
12647 if (bits_a == 0x00)
dda83cd7 12648 {
f1771dce 12649 record_buf[0] = ARM_D0_REGNUM + reg_v;
5a578da5 12650
dda83cd7
SM
12651 arm_insn_r->reg_rec_count = 1;
12652 }
5a578da5
OJ
12653 /* Handle VMSR instruction. */
12654 else if (bits_a == 0x07)
dda83cd7
SM
12655 {
12656 record_buf[0] = ARM_FPSCR_REGNUM;
12657 arm_insn_r->reg_rec_count = 1;
12658 }
5a578da5
OJ
12659 }
12660 else if (!bit_l && bit_c)
12661 {
12662 /* Handle VMOV instruction. */
12663 if (!(bits_a & 0x04))
dda83cd7
SM
12664 {
12665 record_buf[0] = (reg_v | (bit (arm_insn_r->arm_insn, 7) << 4))
12666 + ARM_D0_REGNUM;
12667 arm_insn_r->reg_rec_count = 1;
12668 }
5a578da5
OJ
12669 /* Handle VDUP instruction. */
12670 else
dda83cd7
SM
12671 {
12672 if (bit (arm_insn_r->arm_insn, 21))
12673 {
12674 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
12675 record_buf[0] = reg_v + ARM_D0_REGNUM;
12676 record_buf[1] = reg_v + ARM_D0_REGNUM + 1;
12677 arm_insn_r->reg_rec_count = 2;
12678 }
12679 else
12680 {
12681 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
12682 record_buf[0] = reg_v + ARM_D0_REGNUM;
12683 arm_insn_r->reg_rec_count = 1;
12684 }
12685 }
12686 }
12687
12688 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12689 return 0;
12690}
12691
f20f80dd
OJ
12692/* Record handler for extension register load/store instructions. */
12693
12694static int
4748a9be 12695arm_record_exreg_ld_st_insn (arm_insn_decode_record *arm_insn_r)
f20f80dd
OJ
12696{
12697 uint32_t opcode, single_reg;
12698 uint8_t op_vldm_vstm;
12699 uint32_t record_buf[8], record_buf_mem[128];
12700 ULONGEST u_regval = 0;
12701
12702 struct regcache *reg_cache = arm_insn_r->regcache;
f20f80dd
OJ
12703
12704 opcode = bits (arm_insn_r->arm_insn, 20, 24);
9fde51ed 12705 single_reg = !bit (arm_insn_r->arm_insn, 8);
f20f80dd
OJ
12706 op_vldm_vstm = opcode & 0x1b;
12707
12708 /* Handle VMOV instructions. */
12709 if ((opcode & 0x1e) == 0x04)
12710 {
9fde51ed 12711 if (bit (arm_insn_r->arm_insn, 20)) /* to_arm_registers bit 20? */
01e57735
YQ
12712 {
12713 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12714 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
12715 arm_insn_r->reg_rec_count = 2;
12716 }
f20f80dd 12717 else
01e57735 12718 {
9fde51ed
YQ
12719 uint8_t reg_m = bits (arm_insn_r->arm_insn, 0, 3);
12720 uint8_t bit_m = bit (arm_insn_r->arm_insn, 5);
f20f80dd 12721
9fde51ed 12722 if (single_reg)
01e57735 12723 {
9fde51ed
YQ
12724 /* The first S register number m is REG_M:M (M is bit 5),
12725 the corresponding D register number is REG_M:M / 2, which
12726 is REG_M. */
12727 record_buf[arm_insn_r->reg_rec_count++] = ARM_D0_REGNUM + reg_m;
12728 /* The second S register number is REG_M:M + 1, the
12729 corresponding D register number is (REG_M:M + 1) / 2.
12730 IOW, if bit M is 1, the first and second S registers
12731 are mapped to different D registers, otherwise, they are
12732 in the same D register. */
12733 if (bit_m)
12734 {
12735 record_buf[arm_insn_r->reg_rec_count++]
12736 = ARM_D0_REGNUM + reg_m + 1;
12737 }
01e57735
YQ
12738 }
12739 else
12740 {
9fde51ed 12741 record_buf[0] = ((bit_m << 4) + reg_m + ARM_D0_REGNUM);
01e57735
YQ
12742 arm_insn_r->reg_rec_count = 1;
12743 }
12744 }
f20f80dd
OJ
12745 }
12746 /* Handle VSTM and VPUSH instructions. */
12747 else if (op_vldm_vstm == 0x08 || op_vldm_vstm == 0x0a
01e57735 12748 || op_vldm_vstm == 0x12)
f20f80dd
OJ
12749 {
12750 uint32_t start_address, reg_rn, imm_off32, imm_off8, memory_count;
12751 uint32_t memory_index = 0;
12752
12753 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
12754 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12755 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
9fde51ed 12756 imm_off32 = imm_off8 << 2;
f20f80dd
OJ
12757 memory_count = imm_off8;
12758
12759 if (bit (arm_insn_r->arm_insn, 23))
01e57735 12760 start_address = u_regval;
f20f80dd 12761 else
01e57735 12762 start_address = u_regval - imm_off32;
f20f80dd
OJ
12763
12764 if (bit (arm_insn_r->arm_insn, 21))
01e57735
YQ
12765 {
12766 record_buf[0] = reg_rn;
12767 arm_insn_r->reg_rec_count = 1;
12768 }
f20f80dd
OJ
12769
12770 while (memory_count > 0)
01e57735 12771 {
9fde51ed 12772 if (single_reg)
01e57735 12773 {
9fde51ed
YQ
12774 record_buf_mem[memory_index] = 4;
12775 record_buf_mem[memory_index + 1] = start_address;
01e57735
YQ
12776 start_address = start_address + 4;
12777 memory_index = memory_index + 2;
12778 }
12779 else
12780 {
9fde51ed
YQ
12781 record_buf_mem[memory_index] = 4;
12782 record_buf_mem[memory_index + 1] = start_address;
12783 record_buf_mem[memory_index + 2] = 4;
12784 record_buf_mem[memory_index + 3] = start_address + 4;
01e57735
YQ
12785 start_address = start_address + 8;
12786 memory_index = memory_index + 4;
12787 }
12788 memory_count--;
12789 }
f20f80dd
OJ
12790 arm_insn_r->mem_rec_count = (memory_index >> 1);
12791 }
12792 /* Handle VLDM instructions. */
12793 else if (op_vldm_vstm == 0x09 || op_vldm_vstm == 0x0b
01e57735 12794 || op_vldm_vstm == 0x13)
f20f80dd
OJ
12795 {
12796 uint32_t reg_count, reg_vd;
12797 uint32_t reg_index = 0;
9fde51ed 12798 uint32_t bit_d = bit (arm_insn_r->arm_insn, 22);
f20f80dd
OJ
12799
12800 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12801 reg_count = bits (arm_insn_r->arm_insn, 0, 7);
12802
9fde51ed
YQ
12803 /* REG_VD is the first D register number. If the instruction
12804 loads memory to S registers (SINGLE_REG is TRUE), the register
12805 number is (REG_VD << 1 | bit D), so the corresponding D
12806 register number is (REG_VD << 1 | bit D) / 2 = REG_VD. */
12807 if (!single_reg)
12808 reg_vd = reg_vd | (bit_d << 4);
f20f80dd 12809
9fde51ed 12810 if (bit (arm_insn_r->arm_insn, 21) /* write back */)
01e57735 12811 record_buf[reg_index++] = bits (arm_insn_r->arm_insn, 16, 19);
f20f80dd 12812
9fde51ed
YQ
12813 /* If the instruction loads memory to D register, REG_COUNT should
12814 be divided by 2, according to the ARM Architecture Reference
12815 Manual. If the instruction loads memory to S register, divide by
12816 2 as well because two S registers are mapped to D register. */
12817 reg_count = reg_count / 2;
12818 if (single_reg && bit_d)
01e57735 12819 {
9fde51ed
YQ
12820 /* Increase the register count if S register list starts from
12821 an odd number (bit d is one). */
12822 reg_count++;
12823 }
f20f80dd 12824
9fde51ed
YQ
12825 while (reg_count > 0)
12826 {
12827 record_buf[reg_index++] = ARM_D0_REGNUM + reg_vd + reg_count - 1;
01e57735
YQ
12828 reg_count--;
12829 }
f20f80dd
OJ
12830 arm_insn_r->reg_rec_count = reg_index;
12831 }
12832 /* VSTR Vector store register. */
12833 else if ((opcode & 0x13) == 0x10)
12834 {
bec2ab5a 12835 uint32_t start_address, reg_rn, imm_off32, imm_off8;
f20f80dd
OJ
12836 uint32_t memory_index = 0;
12837
12838 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
12839 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12840 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
9fde51ed 12841 imm_off32 = imm_off8 << 2;
f20f80dd
OJ
12842
12843 if (bit (arm_insn_r->arm_insn, 23))
01e57735 12844 start_address = u_regval + imm_off32;
f20f80dd 12845 else
01e57735 12846 start_address = u_regval - imm_off32;
f20f80dd
OJ
12847
12848 if (single_reg)
01e57735 12849 {
9fde51ed
YQ
12850 record_buf_mem[memory_index] = 4;
12851 record_buf_mem[memory_index + 1] = start_address;
01e57735
YQ
12852 arm_insn_r->mem_rec_count = 1;
12853 }
f20f80dd 12854 else
01e57735 12855 {
9fde51ed
YQ
12856 record_buf_mem[memory_index] = 4;
12857 record_buf_mem[memory_index + 1] = start_address;
12858 record_buf_mem[memory_index + 2] = 4;
12859 record_buf_mem[memory_index + 3] = start_address + 4;
01e57735
YQ
12860 arm_insn_r->mem_rec_count = 2;
12861 }
f20f80dd
OJ
12862 }
12863 /* VLDR Vector load register. */
12864 else if ((opcode & 0x13) == 0x11)
12865 {
12866 uint32_t reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12867
12868 if (!single_reg)
01e57735
YQ
12869 {
12870 reg_vd = reg_vd | (bit (arm_insn_r->arm_insn, 22) << 4);
12871 record_buf[0] = ARM_D0_REGNUM + reg_vd;
12872 }
f20f80dd 12873 else
01e57735
YQ
12874 {
12875 reg_vd = (reg_vd << 1) | bit (arm_insn_r->arm_insn, 22);
9fde51ed
YQ
12876 /* Record register D rather than pseudo register S. */
12877 record_buf[0] = ARM_D0_REGNUM + reg_vd / 2;
01e57735 12878 }
f20f80dd
OJ
12879 arm_insn_r->reg_rec_count = 1;
12880 }
12881
12882 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12883 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12884 return 0;
12885}
12886
851f26ae
OJ
12887/* Record handler for arm/thumb mode VFP data processing instructions. */
12888
12889static int
4748a9be 12890arm_record_vfp_data_proc_insn (arm_insn_decode_record *arm_insn_r)
851f26ae
OJ
12891{
12892 uint32_t opc1, opc2, opc3, dp_op_sz, bit_d, reg_vd;
12893 uint32_t record_buf[4];
12894 enum insn_types {INSN_T0, INSN_T1, INSN_T2, INSN_T3, INSN_INV};
12895 enum insn_types curr_insn_type = INSN_INV;
12896
12897 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12898 opc1 = bits (arm_insn_r->arm_insn, 20, 23);
12899 opc2 = bits (arm_insn_r->arm_insn, 16, 19);
12900 opc3 = bits (arm_insn_r->arm_insn, 6, 7);
12901 dp_op_sz = bit (arm_insn_r->arm_insn, 8);
12902 bit_d = bit (arm_insn_r->arm_insn, 22);
ce887586
TT
12903 /* Mask off the "D" bit. */
12904 opc1 = opc1 & ~0x04;
851f26ae
OJ
12905
12906 /* Handle VMLA, VMLS. */
12907 if (opc1 == 0x00)
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 VNMLA, VNMLS, VNMUL. */
12925 else if (opc1 == 0x01)
12926 {
12927 if (dp_op_sz)
dda83cd7 12928 curr_insn_type = INSN_T1;
851f26ae 12929 else
dda83cd7 12930 curr_insn_type = INSN_T2;
851f26ae
OJ
12931 }
12932 /* Handle VMUL. */
12933 else if (opc1 == 0x02 && !(opc3 & 0x01))
12934 {
12935 if (bit (arm_insn_r->arm_insn, 10))
dda83cd7
SM
12936 {
12937 if (bit (arm_insn_r->arm_insn, 6))
12938 curr_insn_type = INSN_T0;
12939 else
12940 curr_insn_type = INSN_T1;
12941 }
851f26ae 12942 else
dda83cd7
SM
12943 {
12944 if (dp_op_sz)
12945 curr_insn_type = INSN_T1;
12946 else
12947 curr_insn_type = INSN_T2;
12948 }
851f26ae
OJ
12949 }
12950 /* Handle VADD, VSUB. */
12951 else if (opc1 == 0x03)
12952 {
12953 if (!bit (arm_insn_r->arm_insn, 9))
dda83cd7
SM
12954 {
12955 if (bit (arm_insn_r->arm_insn, 6))
12956 curr_insn_type = INSN_T0;
12957 else
12958 curr_insn_type = INSN_T1;
12959 }
851f26ae 12960 else
dda83cd7
SM
12961 {
12962 if (dp_op_sz)
12963 curr_insn_type = INSN_T1;
12964 else
12965 curr_insn_type = INSN_T2;
12966 }
851f26ae
OJ
12967 }
12968 /* Handle VDIV. */
ce887586 12969 else if (opc1 == 0x08)
851f26ae
OJ
12970 {
12971 if (dp_op_sz)
dda83cd7 12972 curr_insn_type = INSN_T1;
851f26ae 12973 else
dda83cd7 12974 curr_insn_type = INSN_T2;
851f26ae
OJ
12975 }
12976 /* Handle all other vfp data processing instructions. */
12977 else if (opc1 == 0x0b)
12978 {
12979 /* Handle VMOV. */
12980 if (!(opc3 & 0x01) || (opc2 == 0x00 && opc3 == 0x01))
dda83cd7
SM
12981 {
12982 if (bit (arm_insn_r->arm_insn, 4))
12983 {
12984 if (bit (arm_insn_r->arm_insn, 6))
12985 curr_insn_type = INSN_T0;
12986 else
12987 curr_insn_type = INSN_T1;
12988 }
12989 else
12990 {
12991 if (dp_op_sz)
12992 curr_insn_type = INSN_T1;
12993 else
12994 curr_insn_type = INSN_T2;
12995 }
12996 }
851f26ae
OJ
12997 /* Handle VNEG and VABS. */
12998 else if ((opc2 == 0x01 && opc3 == 0x01)
dda83cd7
SM
12999 || (opc2 == 0x00 && opc3 == 0x03))
13000 {
13001 if (!bit (arm_insn_r->arm_insn, 11))
13002 {
13003 if (bit (arm_insn_r->arm_insn, 6))
13004 curr_insn_type = INSN_T0;
13005 else
13006 curr_insn_type = INSN_T1;
13007 }
13008 else
13009 {
13010 if (dp_op_sz)
13011 curr_insn_type = INSN_T1;
13012 else
13013 curr_insn_type = INSN_T2;
13014 }
13015 }
851f26ae
OJ
13016 /* Handle VSQRT. */
13017 else if (opc2 == 0x01 && opc3 == 0x03)
dda83cd7
SM
13018 {
13019 if (dp_op_sz)
13020 curr_insn_type = INSN_T1;
13021 else
13022 curr_insn_type = INSN_T2;
13023 }
851f26ae
OJ
13024 /* Handle VCVT. */
13025 else if (opc2 == 0x07 && opc3 == 0x03)
dda83cd7
SM
13026 {
13027 if (!dp_op_sz)
13028 curr_insn_type = INSN_T1;
13029 else
13030 curr_insn_type = INSN_T2;
13031 }
851f26ae 13032 else if (opc3 & 0x01)
dda83cd7
SM
13033 {
13034 /* Handle VCVT. */
13035 if ((opc2 == 0x08) || (opc2 & 0x0e) == 0x0c)
13036 {
13037 if (!bit (arm_insn_r->arm_insn, 18))
13038 curr_insn_type = INSN_T2;
13039 else
13040 {
13041 if (dp_op_sz)
13042 curr_insn_type = INSN_T1;
13043 else
13044 curr_insn_type = INSN_T2;
13045 }
13046 }
13047 /* Handle VCVT. */
13048 else if ((opc2 & 0x0e) == 0x0a || (opc2 & 0x0e) == 0x0e)
13049 {
13050 if (dp_op_sz)
13051 curr_insn_type = INSN_T1;
13052 else
13053 curr_insn_type = INSN_T2;
13054 }
13055 /* Handle VCVTB, VCVTT. */
13056 else if ((opc2 & 0x0e) == 0x02)
13057 curr_insn_type = INSN_T2;
13058 /* Handle VCMP, VCMPE. */
13059 else if ((opc2 & 0x0e) == 0x04)
13060 curr_insn_type = INSN_T3;
13061 }
851f26ae
OJ
13062 }
13063
13064 switch (curr_insn_type)
13065 {
13066 case INSN_T0:
dda83cd7
SM
13067 reg_vd = reg_vd | (bit_d << 4);
13068 record_buf[0] = reg_vd + ARM_D0_REGNUM;
13069 record_buf[1] = reg_vd + ARM_D0_REGNUM + 1;
13070 arm_insn_r->reg_rec_count = 2;
13071 break;
851f26ae
OJ
13072
13073 case INSN_T1:
dda83cd7
SM
13074 reg_vd = reg_vd | (bit_d << 4);
13075 record_buf[0] = reg_vd + ARM_D0_REGNUM;
13076 arm_insn_r->reg_rec_count = 1;
13077 break;
851f26ae
OJ
13078
13079 case INSN_T2:
dda83cd7
SM
13080 reg_vd = (reg_vd << 1) | bit_d;
13081 record_buf[0] = reg_vd + ARM_D0_REGNUM;
13082 arm_insn_r->reg_rec_count = 1;
13083 break;
851f26ae
OJ
13084
13085 case INSN_T3:
dda83cd7
SM
13086 record_buf[0] = ARM_FPSCR_REGNUM;
13087 arm_insn_r->reg_rec_count = 1;
13088 break;
851f26ae
OJ
13089
13090 default:
dda83cd7
SM
13091 gdb_assert_not_reached ("no decoding pattern found");
13092 break;
851f26ae
OJ
13093 }
13094
13095 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
13096 return 0;
13097}
13098
60cc5e93
OJ
13099/* Handling opcode 110 insns. */
13100
13101static int
4748a9be 13102arm_record_asimd_vfp_coproc (arm_insn_decode_record *arm_insn_r)
60cc5e93 13103{
bec2ab5a 13104 uint32_t op1, op1_ebit, coproc;
60cc5e93
OJ
13105
13106 coproc = bits (arm_insn_r->arm_insn, 8, 11);
13107 op1 = bits (arm_insn_r->arm_insn, 20, 25);
13108 op1_ebit = bit (arm_insn_r->arm_insn, 20);
13109
13110 if ((coproc & 0x0e) == 0x0a)
13111 {
13112 /* Handle extension register ld/st instructions. */
13113 if (!(op1 & 0x20))
dda83cd7 13114 return arm_record_exreg_ld_st_insn (arm_insn_r);
60cc5e93
OJ
13115
13116 /* 64-bit transfers between arm core and extension registers. */
13117 if ((op1 & 0x3e) == 0x04)
dda83cd7 13118 return arm_record_exreg_ld_st_insn (arm_insn_r);
60cc5e93
OJ
13119 }
13120 else
13121 {
13122 /* Handle coprocessor ld/st instructions. */
13123 if (!(op1 & 0x3a))
dda83cd7
SM
13124 {
13125 /* Store. */
13126 if (!op1_ebit)
13127 return arm_record_unsupported_insn (arm_insn_r);
13128 else
13129 /* Load. */
13130 return arm_record_unsupported_insn (arm_insn_r);
13131 }
60cc5e93
OJ
13132
13133 /* Move to coprocessor from two arm core registers. */
13134 if (op1 == 0x4)
dda83cd7 13135 return arm_record_unsupported_insn (arm_insn_r);
60cc5e93
OJ
13136
13137 /* Move to two arm core registers from coprocessor. */
13138 if (op1 == 0x5)
dda83cd7
SM
13139 {
13140 uint32_t reg_t[2];
60cc5e93 13141
dda83cd7
SM
13142 reg_t[0] = bits (arm_insn_r->arm_insn, 12, 15);
13143 reg_t[1] = bits (arm_insn_r->arm_insn, 16, 19);
13144 arm_insn_r->reg_rec_count = 2;
60cc5e93 13145
dda83cd7
SM
13146 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, reg_t);
13147 return 0;
60cc5e93
OJ
13148 }
13149 }
13150 return arm_record_unsupported_insn (arm_insn_r);
13151}
13152
72508ac0
PO
13153/* Handling opcode 111 insns. */
13154
13155static int
4748a9be 13156arm_record_coproc_data_proc (arm_insn_decode_record *arm_insn_r)
72508ac0 13157{
2d9e6acb 13158 uint32_t op, op1_ebit, coproc, bits_24_25;
345bd07c 13159 arm_gdbarch_tdep *tdep
08106042 13160 = gdbarch_tdep<arm_gdbarch_tdep> (arm_insn_r->gdbarch);
72508ac0 13161 struct regcache *reg_cache = arm_insn_r->regcache;
72508ac0
PO
13162
13163 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 24, 27);
60cc5e93 13164 coproc = bits (arm_insn_r->arm_insn, 8, 11);
60cc5e93
OJ
13165 op1_ebit = bit (arm_insn_r->arm_insn, 20);
13166 op = bit (arm_insn_r->arm_insn, 4);
2d9e6acb 13167 bits_24_25 = bits (arm_insn_r->arm_insn, 24, 25);
97dfe206
OJ
13168
13169 /* Handle arm SWI/SVC system call instructions. */
2d9e6acb 13170 if (bits_24_25 == 0x3)
97dfe206
OJ
13171 {
13172 if (tdep->arm_syscall_record != NULL)
dda83cd7
SM
13173 {
13174 ULONGEST svc_operand, svc_number;
97dfe206 13175
dda83cd7 13176 svc_operand = (0x00ffffff & arm_insn_r->arm_insn);
97dfe206 13177
dda83cd7
SM
13178 if (svc_operand) /* OABI. */
13179 svc_number = svc_operand - 0x900000;
13180 else /* EABI. */
13181 regcache_raw_read_unsigned (reg_cache, 7, &svc_number);
97dfe206 13182
dda83cd7
SM
13183 return tdep->arm_syscall_record (reg_cache, svc_number);
13184 }
97dfe206 13185 else
dda83cd7 13186 {
6cb06a8c 13187 gdb_printf (gdb_stderr, _("no syscall record support\n"));
dda83cd7
SM
13188 return -1;
13189 }
97dfe206 13190 }
2d9e6acb 13191 else if (bits_24_25 == 0x02)
60cc5e93 13192 {
2d9e6acb
YQ
13193 if (op)
13194 {
13195 if ((coproc & 0x0e) == 0x0a)
13196 {
13197 /* 8, 16, and 32-bit transfer */
13198 return arm_record_vdata_transfer_insn (arm_insn_r);
13199 }
13200 else
13201 {
13202 if (op1_ebit)
13203 {
13204 /* MRC, MRC2 */
13205 uint32_t record_buf[1];
13206
13207 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
13208 if (record_buf[0] == 15)
13209 record_buf[0] = ARM_PS_REGNUM;
60cc5e93 13210
2d9e6acb
YQ
13211 arm_insn_r->reg_rec_count = 1;
13212 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count,
13213 record_buf);
13214 return 0;
13215 }
13216 else
13217 {
13218 /* MCR, MCR2 */
13219 return -1;
13220 }
13221 }
13222 }
13223 else
13224 {
13225 if ((coproc & 0x0e) == 0x0a)
13226 {
13227 /* VFP data-processing instructions. */
13228 return arm_record_vfp_data_proc_insn (arm_insn_r);
13229 }
13230 else
13231 {
13232 /* CDP, CDP2 */
13233 return -1;
13234 }
13235 }
60cc5e93 13236 }
97dfe206
OJ
13237 else
13238 {
2d9e6acb 13239 unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 25);
60cc5e93 13240
2d9e6acb
YQ
13241 if (op1 == 5)
13242 {
13243 if ((coproc & 0x0e) != 0x0a)
13244 {
13245 /* MRRC, MRRC2 */
13246 return -1;
13247 }
13248 }
13249 else if (op1 == 4 || op1 == 5)
13250 {
13251 if ((coproc & 0x0e) == 0x0a)
13252 {
13253 /* 64-bit transfers between ARM core and extension */
13254 return -1;
13255 }
13256 else if (op1 == 4)
13257 {
13258 /* MCRR, MCRR2 */
13259 return -1;
13260 }
13261 }
13262 else if (op1 == 0 || op1 == 1)
13263 {
13264 /* UNDEFINED */
13265 return -1;
13266 }
13267 else
13268 {
13269 if ((coproc & 0x0e) == 0x0a)
13270 {
13271 /* Extension register load/store */
13272 }
13273 else
13274 {
13275 /* STC, STC2, LDC, LDC2 */
13276 }
13277 return -1;
13278 }
97dfe206 13279 }
72508ac0 13280
2d9e6acb 13281 return -1;
72508ac0
PO
13282}
13283
13284/* Handling opcode 000 insns. */
13285
13286static int
4748a9be 13287thumb_record_shift_add_sub (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13288{
13289 uint32_t record_buf[8];
13290 uint32_t reg_src1 = 0;
13291
13292 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13293
13294 record_buf[0] = ARM_PS_REGNUM;
13295 record_buf[1] = reg_src1;
13296 thumb_insn_r->reg_rec_count = 2;
13297
13298 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13299
13300 return 0;
13301}
13302
13303
13304/* Handling opcode 001 insns. */
13305
13306static int
4748a9be 13307thumb_record_add_sub_cmp_mov (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13308{
13309 uint32_t record_buf[8];
13310 uint32_t reg_src1 = 0;
13311
13312 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13313
13314 record_buf[0] = ARM_PS_REGNUM;
13315 record_buf[1] = reg_src1;
13316 thumb_insn_r->reg_rec_count = 2;
13317
13318 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13319
13320 return 0;
13321}
13322
13323/* Handling opcode 010 insns. */
13324
13325static int
4748a9be 13326thumb_record_ld_st_reg_offset (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13327{
13328 struct regcache *reg_cache = thumb_insn_r->regcache;
13329 uint32_t record_buf[8], record_buf_mem[8];
13330
13331 uint32_t reg_src1 = 0, reg_src2 = 0;
13332 uint32_t opcode1 = 0, opcode2 = 0, opcode3 = 0;
13333
13334 ULONGEST u_regval[2] = {0};
13335
13336 opcode1 = bits (thumb_insn_r->arm_insn, 10, 12);
13337
13338 if (bit (thumb_insn_r->arm_insn, 12))
13339 {
13340 /* Handle load/store register offset. */
b121eeb9
YQ
13341 uint32_t opB = bits (thumb_insn_r->arm_insn, 9, 11);
13342
b020ff80 13343 if (in_inclusive_range (opB, 4U, 7U))
dda83cd7
SM
13344 {
13345 /* LDR(2), LDRB(2) , LDRH(2), LDRSB, LDRSH. */
13346 reg_src1 = bits (thumb_insn_r->arm_insn,0, 2);
13347 record_buf[0] = reg_src1;
13348 thumb_insn_r->reg_rec_count = 1;
13349 }
b020ff80 13350 else if (in_inclusive_range (opB, 0U, 2U))
dda83cd7
SM
13351 {
13352 /* STR(2), STRB(2), STRH(2) . */
13353 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
13354 reg_src2 = bits (thumb_insn_r->arm_insn, 6, 8);
13355 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
13356 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
13357 if (0 == opB)
13358 record_buf_mem[0] = 4; /* STR (2). */
13359 else if (2 == opB)
13360 record_buf_mem[0] = 1; /* STRB (2). */
13361 else if (1 == opB)
13362 record_buf_mem[0] = 2; /* STRH (2). */
13363 record_buf_mem[1] = u_regval[0] + u_regval[1];
13364 thumb_insn_r->mem_rec_count = 1;
13365 }
72508ac0
PO
13366 }
13367 else if (bit (thumb_insn_r->arm_insn, 11))
13368 {
13369 /* Handle load from literal pool. */
13370 /* LDR(3). */
13371 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13372 record_buf[0] = reg_src1;
13373 thumb_insn_r->reg_rec_count = 1;
13374 }
13375 else if (opcode1)
13376 {
b121eeb9 13377 /* Special data instructions and branch and exchange */
72508ac0
PO
13378 opcode2 = bits (thumb_insn_r->arm_insn, 8, 9);
13379 opcode3 = bits (thumb_insn_r->arm_insn, 0, 2);
13380 if ((3 == opcode2) && (!opcode3))
dda83cd7
SM
13381 {
13382 /* Branch with exchange. */
13383 record_buf[0] = ARM_PS_REGNUM;
13384 thumb_insn_r->reg_rec_count = 1;
13385 }
72508ac0 13386 else
dda83cd7 13387 {
1f33efec
YQ
13388 /* Format 8; special data processing insns. */
13389 record_buf[0] = ARM_PS_REGNUM;
13390 record_buf[1] = (bit (thumb_insn_r->arm_insn, 7) << 3
13391 | bits (thumb_insn_r->arm_insn, 0, 2));
dda83cd7
SM
13392 thumb_insn_r->reg_rec_count = 2;
13393 }
72508ac0
PO
13394 }
13395 else
13396 {
13397 /* Format 5; data processing insns. */
13398 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13399 if (bit (thumb_insn_r->arm_insn, 7))
dda83cd7
SM
13400 {
13401 reg_src1 = reg_src1 + 8;
13402 }
72508ac0
PO
13403 record_buf[0] = ARM_PS_REGNUM;
13404 record_buf[1] = reg_src1;
13405 thumb_insn_r->reg_rec_count = 2;
13406 }
13407
13408 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13409 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13410 record_buf_mem);
72508ac0
PO
13411
13412 return 0;
13413}
13414
13415/* Handling opcode 001 insns. */
13416
13417static int
4748a9be 13418thumb_record_ld_st_imm_offset (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13419{
13420 struct regcache *reg_cache = thumb_insn_r->regcache;
13421 uint32_t record_buf[8], record_buf_mem[8];
13422
13423 uint32_t reg_src1 = 0;
13424 uint32_t opcode = 0, immed_5 = 0;
13425
13426 ULONGEST u_regval = 0;
13427
13428 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
13429
13430 if (opcode)
13431 {
13432 /* LDR(1). */
13433 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13434 record_buf[0] = reg_src1;
13435 thumb_insn_r->reg_rec_count = 1;
13436 }
13437 else
13438 {
13439 /* STR(1). */
13440 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
13441 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
13442 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
13443 record_buf_mem[0] = 4;
13444 record_buf_mem[1] = u_regval + (immed_5 * 4);
13445 thumb_insn_r->mem_rec_count = 1;
13446 }
13447
13448 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13449 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13450 record_buf_mem);
72508ac0
PO
13451
13452 return 0;
13453}
13454
13455/* Handling opcode 100 insns. */
13456
13457static int
4748a9be 13458thumb_record_ld_st_stack (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13459{
13460 struct regcache *reg_cache = thumb_insn_r->regcache;
13461 uint32_t record_buf[8], record_buf_mem[8];
13462
13463 uint32_t reg_src1 = 0;
13464 uint32_t opcode = 0, immed_8 = 0, immed_5 = 0;
13465
13466 ULONGEST u_regval = 0;
13467
13468 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
13469
13470 if (3 == opcode)
13471 {
13472 /* LDR(4). */
13473 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13474 record_buf[0] = reg_src1;
13475 thumb_insn_r->reg_rec_count = 1;
13476 }
13477 else if (1 == opcode)
13478 {
13479 /* LDRH(1). */
13480 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13481 record_buf[0] = reg_src1;
13482 thumb_insn_r->reg_rec_count = 1;
13483 }
13484 else if (2 == opcode)
13485 {
13486 /* STR(3). */
13487 immed_8 = bits (thumb_insn_r->arm_insn, 0, 7);
13488 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
13489 record_buf_mem[0] = 4;
13490 record_buf_mem[1] = u_regval + (immed_8 * 4);
13491 thumb_insn_r->mem_rec_count = 1;
13492 }
13493 else if (0 == opcode)
13494 {
13495 /* STRH(1). */
13496 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
13497 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
13498 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
13499 record_buf_mem[0] = 2;
13500 record_buf_mem[1] = u_regval + (immed_5 * 2);
13501 thumb_insn_r->mem_rec_count = 1;
13502 }
13503
13504 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13505 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13506 record_buf_mem);
72508ac0
PO
13507
13508 return 0;
13509}
13510
13511/* Handling opcode 101 insns. */
13512
13513static int
4748a9be 13514thumb_record_misc (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13515{
13516 struct regcache *reg_cache = thumb_insn_r->regcache;
13517
b121eeb9 13518 uint32_t opcode = 0;
72508ac0 13519 uint32_t register_bits = 0, register_count = 0;
bec2ab5a 13520 uint32_t index = 0, start_address = 0;
72508ac0
PO
13521 uint32_t record_buf[24], record_buf_mem[48];
13522 uint32_t reg_src1;
13523
13524 ULONGEST u_regval = 0;
13525
13526 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
72508ac0 13527
b121eeb9 13528 if (opcode == 0 || opcode == 1)
72508ac0 13529 {
b121eeb9
YQ
13530 /* ADR and ADD (SP plus immediate) */
13531
72508ac0
PO
13532 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13533 record_buf[0] = reg_src1;
13534 thumb_insn_r->reg_rec_count = 1;
13535 }
b121eeb9 13536 else
72508ac0 13537 {
b121eeb9
YQ
13538 /* Miscellaneous 16-bit instructions */
13539 uint32_t opcode2 = bits (thumb_insn_r->arm_insn, 8, 11);
13540
13541 switch (opcode2)
13542 {
13543 case 6:
13544 /* SETEND and CPS */
13545 break;
13546 case 0:
13547 /* ADD/SUB (SP plus immediate) */
13548 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13549 record_buf[0] = ARM_SP_REGNUM;
13550 thumb_insn_r->reg_rec_count = 1;
13551 break;
13552 case 1: /* fall through */
13553 case 3: /* fall through */
13554 case 9: /* fall through */
13555 case 11:
13556 /* CBNZ, CBZ */
b121eeb9
YQ
13557 break;
13558 case 2:
13559 /* SXTH, SXTB, UXTH, UXTB */
13560 record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
13561 thumb_insn_r->reg_rec_count = 1;
13562 break;
13563 case 4: /* fall through */
13564 case 5:
13565 /* PUSH. */
13566 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13567 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
13568 while (register_bits)
13569 {
13570 if (register_bits & 0x00000001)
13571 register_count++;
13572 register_bits = register_bits >> 1;
13573 }
13574 start_address = u_regval - \
13575 (4 * (bit (thumb_insn_r->arm_insn, 8) + register_count));
13576 thumb_insn_r->mem_rec_count = register_count;
13577 while (register_count)
13578 {
13579 record_buf_mem[(register_count * 2) - 1] = start_address;
13580 record_buf_mem[(register_count * 2) - 2] = 4;
13581 start_address = start_address + 4;
13582 register_count--;
13583 }
13584 record_buf[0] = ARM_SP_REGNUM;
13585 thumb_insn_r->reg_rec_count = 1;
13586 break;
13587 case 10:
13588 /* REV, REV16, REVSH */
ba14f379
YQ
13589 record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
13590 thumb_insn_r->reg_rec_count = 1;
b121eeb9
YQ
13591 break;
13592 case 12: /* fall through */
13593 case 13:
13594 /* POP. */
13595 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13596 while (register_bits)
13597 {
13598 if (register_bits & 0x00000001)
13599 record_buf[index++] = register_count;
13600 register_bits = register_bits >> 1;
13601 register_count++;
13602 }
13603 record_buf[index++] = ARM_PS_REGNUM;
13604 record_buf[index++] = ARM_SP_REGNUM;
13605 thumb_insn_r->reg_rec_count = index;
13606 break;
13607 case 0xe:
13608 /* BKPT insn. */
13609 /* Handle enhanced software breakpoint insn, BKPT. */
13610 /* CPSR is changed to be executed in ARM state, disabling normal
13611 interrupts, entering abort mode. */
13612 /* According to high vector configuration PC is set. */
13613 /* User hits breakpoint and type reverse, in that case, we need to go back with
13614 previous CPSR and Program Counter. */
13615 record_buf[0] = ARM_PS_REGNUM;
13616 record_buf[1] = ARM_LR_REGNUM;
13617 thumb_insn_r->reg_rec_count = 2;
13618 /* We need to save SPSR value, which is not yet done. */
6cb06a8c
TT
13619 gdb_printf (gdb_stderr,
13620 _("Process record does not support instruction "
13621 "0x%0x at address %s.\n"),
13622 thumb_insn_r->arm_insn,
13623 paddress (thumb_insn_r->gdbarch,
13624 thumb_insn_r->this_addr));
b121eeb9
YQ
13625 return -1;
13626
13627 case 0xf:
13628 /* If-Then, and hints */
13629 break;
13630 default:
13631 return -1;
13632 };
72508ac0
PO
13633 }
13634
13635 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13636 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13637 record_buf_mem);
72508ac0
PO
13638
13639 return 0;
13640}
13641
13642/* Handling opcode 110 insns. */
13643
13644static int
4748a9be 13645thumb_record_ldm_stm_swi (arm_insn_decode_record *thumb_insn_r)
72508ac0 13646{
345bd07c 13647 arm_gdbarch_tdep *tdep
08106042 13648 = gdbarch_tdep<arm_gdbarch_tdep> (thumb_insn_r->gdbarch);
72508ac0
PO
13649 struct regcache *reg_cache = thumb_insn_r->regcache;
13650
13651 uint32_t ret = 0; /* function return value: -1:record failure ; 0:success */
13652 uint32_t reg_src1 = 0;
13653 uint32_t opcode1 = 0, opcode2 = 0, register_bits = 0, register_count = 0;
bec2ab5a 13654 uint32_t index = 0, start_address = 0;
72508ac0
PO
13655 uint32_t record_buf[24], record_buf_mem[48];
13656
13657 ULONGEST u_regval = 0;
13658
13659 opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
13660 opcode2 = bits (thumb_insn_r->arm_insn, 11, 12);
13661
13662 if (1 == opcode2)
13663 {
13664
13665 /* LDMIA. */
13666 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13667 /* Get Rn. */
13668 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13669 while (register_bits)
dda83cd7
SM
13670 {
13671 if (register_bits & 0x00000001)
13672 record_buf[index++] = register_count;
13673 register_bits = register_bits >> 1;
13674 register_count++;
13675 }
f969241e
OJ
13676 record_buf[index++] = reg_src1;
13677 thumb_insn_r->reg_rec_count = index;
72508ac0
PO
13678 }
13679 else if (0 == opcode2)
13680 {
13681 /* It handles both STMIA. */
13682 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13683 /* Get Rn. */
13684 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13685 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
13686 while (register_bits)
dda83cd7
SM
13687 {
13688 if (register_bits & 0x00000001)
13689 register_count++;
13690 register_bits = register_bits >> 1;
13691 }
72508ac0
PO
13692 start_address = u_regval;
13693 thumb_insn_r->mem_rec_count = register_count;
13694 while (register_count)
dda83cd7
SM
13695 {
13696 record_buf_mem[(register_count * 2) - 1] = start_address;
13697 record_buf_mem[(register_count * 2) - 2] = 4;
13698 start_address = start_address + 4;
13699 register_count--;
13700 }
72508ac0
PO
13701 }
13702 else if (0x1F == opcode1)
13703 {
dda83cd7
SM
13704 /* Handle arm syscall insn. */
13705 if (tdep->arm_syscall_record != NULL)
13706 {
13707 regcache_raw_read_unsigned (reg_cache, 7, &u_regval);
13708 ret = tdep->arm_syscall_record (reg_cache, u_regval);
13709 }
13710 else
13711 {
6cb06a8c 13712 gdb_printf (gdb_stderr, _("no syscall record support\n"));
dda83cd7
SM
13713 return -1;
13714 }
72508ac0
PO
13715 }
13716
13717 /* B (1), conditional branch is automatically taken care in process_record,
13718 as PC is saved there. */
13719
13720 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13721 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13722 record_buf_mem);
72508ac0
PO
13723
13724 return ret;
13725}
13726
13727/* Handling opcode 111 insns. */
13728
13729static int
4748a9be 13730thumb_record_branch (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13731{
13732 uint32_t record_buf[8];
13733 uint32_t bits_h = 0;
13734
13735 bits_h = bits (thumb_insn_r->arm_insn, 11, 12);
13736
13737 if (2 == bits_h || 3 == bits_h)
13738 {
13739 /* BL */
13740 record_buf[0] = ARM_LR_REGNUM;
13741 thumb_insn_r->reg_rec_count = 1;
13742 }
13743 else if (1 == bits_h)
13744 {
13745 /* BLX(1). */
13746 record_buf[0] = ARM_PS_REGNUM;
13747 record_buf[1] = ARM_LR_REGNUM;
13748 thumb_insn_r->reg_rec_count = 2;
13749 }
13750
13751 /* B(2) is automatically taken care in process_record, as PC is
13752 saved there. */
13753
13754 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13755
13756 return 0;
13757}
13758
c6ec2b30
OJ
13759/* Handler for thumb2 load/store multiple instructions. */
13760
13761static int
4748a9be 13762thumb2_record_ld_st_multiple (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13763{
13764 struct regcache *reg_cache = thumb2_insn_r->regcache;
13765
13766 uint32_t reg_rn, op;
13767 uint32_t register_bits = 0, register_count = 0;
13768 uint32_t index = 0, start_address = 0;
13769 uint32_t record_buf[24], record_buf_mem[48];
13770
13771 ULONGEST u_regval = 0;
13772
13773 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13774 op = bits (thumb2_insn_r->arm_insn, 23, 24);
13775
13776 if (0 == op || 3 == op)
13777 {
13778 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
dda83cd7
SM
13779 {
13780 /* Handle RFE instruction. */
13781 record_buf[0] = ARM_PS_REGNUM;
13782 thumb2_insn_r->reg_rec_count = 1;
13783 }
c6ec2b30 13784 else
dda83cd7
SM
13785 {
13786 /* Handle SRS instruction after reading banked SP. */
13787 return arm_record_unsupported_insn (thumb2_insn_r);
13788 }
c6ec2b30
OJ
13789 }
13790 else if (1 == op || 2 == op)
13791 {
13792 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
dda83cd7
SM
13793 {
13794 /* Handle LDM/LDMIA/LDMFD and LDMDB/LDMEA instructions. */
13795 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
13796 while (register_bits)
13797 {
13798 if (register_bits & 0x00000001)
13799 record_buf[index++] = register_count;
13800
13801 register_count++;
13802 register_bits = register_bits >> 1;
13803 }
13804 record_buf[index++] = reg_rn;
13805 record_buf[index++] = ARM_PS_REGNUM;
13806 thumb2_insn_r->reg_rec_count = index;
13807 }
c6ec2b30 13808 else
dda83cd7
SM
13809 {
13810 /* Handle STM/STMIA/STMEA and STMDB/STMFD. */
13811 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
13812 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
13813 while (register_bits)
13814 {
13815 if (register_bits & 0x00000001)
13816 register_count++;
13817
13818 register_bits = register_bits >> 1;
13819 }
13820
13821 if (1 == op)
13822 {
13823 /* Start address calculation for LDMDB/LDMEA. */
13824 start_address = u_regval;
13825 }
13826 else if (2 == op)
13827 {
13828 /* Start address calculation for LDMDB/LDMEA. */
13829 start_address = u_regval - register_count * 4;
13830 }
13831
13832 thumb2_insn_r->mem_rec_count = register_count;
13833 while (register_count)
13834 {
13835 record_buf_mem[register_count * 2 - 1] = start_address;
13836 record_buf_mem[register_count * 2 - 2] = 4;
13837 start_address = start_address + 4;
13838 register_count--;
13839 }
13840 record_buf[0] = reg_rn;
13841 record_buf[1] = ARM_PS_REGNUM;
13842 thumb2_insn_r->reg_rec_count = 2;
13843 }
c6ec2b30
OJ
13844 }
13845
13846 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
dda83cd7 13847 record_buf_mem);
c6ec2b30 13848 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13849 record_buf);
c6ec2b30
OJ
13850 return ARM_RECORD_SUCCESS;
13851}
13852
13853/* Handler for thumb2 load/store (dual/exclusive) and table branch
13854 instructions. */
13855
13856static int
4748a9be 13857thumb2_record_ld_st_dual_ex_tbb (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13858{
13859 struct regcache *reg_cache = thumb2_insn_r->regcache;
13860
13861 uint32_t reg_rd, reg_rn, offset_imm;
13862 uint32_t reg_dest1, reg_dest2;
13863 uint32_t address, offset_addr;
13864 uint32_t record_buf[8], record_buf_mem[8];
13865 uint32_t op1, op2, op3;
c6ec2b30
OJ
13866
13867 ULONGEST u_regval[2];
13868
13869 op1 = bits (thumb2_insn_r->arm_insn, 23, 24);
13870 op2 = bits (thumb2_insn_r->arm_insn, 20, 21);
13871 op3 = bits (thumb2_insn_r->arm_insn, 4, 7);
13872
13873 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
13874 {
13875 if(!(1 == op1 && 1 == op2 && (0 == op3 || 1 == op3)))
dda83cd7
SM
13876 {
13877 reg_dest1 = bits (thumb2_insn_r->arm_insn, 12, 15);
13878 record_buf[0] = reg_dest1;
13879 record_buf[1] = ARM_PS_REGNUM;
13880 thumb2_insn_r->reg_rec_count = 2;
13881 }
c6ec2b30
OJ
13882
13883 if (3 == op2 || (op1 & 2) || (1 == op1 && 1 == op2 && 7 == op3))
dda83cd7
SM
13884 {
13885 reg_dest2 = bits (thumb2_insn_r->arm_insn, 8, 11);
13886 record_buf[2] = reg_dest2;
13887 thumb2_insn_r->reg_rec_count = 3;
13888 }
c6ec2b30
OJ
13889 }
13890 else
13891 {
13892 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13893 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
13894
13895 if (0 == op1 && 0 == op2)
dda83cd7
SM
13896 {
13897 /* Handle STREX. */
13898 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13899 address = u_regval[0] + (offset_imm * 4);
13900 record_buf_mem[0] = 4;
13901 record_buf_mem[1] = address;
13902 thumb2_insn_r->mem_rec_count = 1;
13903 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
13904 record_buf[0] = reg_rd;
13905 thumb2_insn_r->reg_rec_count = 1;
13906 }
c6ec2b30 13907 else if (1 == op1 && 0 == op2)
dda83cd7
SM
13908 {
13909 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
13910 record_buf[0] = reg_rd;
13911 thumb2_insn_r->reg_rec_count = 1;
13912 address = u_regval[0];
13913 record_buf_mem[1] = address;
13914
13915 if (4 == op3)
13916 {
13917 /* Handle STREXB. */
13918 record_buf_mem[0] = 1;
13919 thumb2_insn_r->mem_rec_count = 1;
13920 }
13921 else if (5 == op3)
13922 {
13923 /* Handle STREXH. */
13924 record_buf_mem[0] = 2 ;
13925 thumb2_insn_r->mem_rec_count = 1;
13926 }
13927 else if (7 == op3)
13928 {
13929 /* Handle STREXD. */
13930 address = u_regval[0];
13931 record_buf_mem[0] = 4;
13932 record_buf_mem[2] = 4;
13933 record_buf_mem[3] = address + 4;
13934 thumb2_insn_r->mem_rec_count = 2;
13935 }
13936 }
c6ec2b30 13937 else
dda83cd7
SM
13938 {
13939 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13940
13941 if (bit (thumb2_insn_r->arm_insn, 24))
13942 {
13943 if (bit (thumb2_insn_r->arm_insn, 23))
13944 offset_addr = u_regval[0] + (offset_imm * 4);
13945 else
13946 offset_addr = u_regval[0] - (offset_imm * 4);
13947
13948 address = offset_addr;
13949 }
13950 else
13951 address = u_regval[0];
13952
13953 record_buf_mem[0] = 4;
13954 record_buf_mem[1] = address;
13955 record_buf_mem[2] = 4;
13956 record_buf_mem[3] = address + 4;
13957 thumb2_insn_r->mem_rec_count = 2;
13958 record_buf[0] = reg_rn;
13959 thumb2_insn_r->reg_rec_count = 1;
13960 }
c6ec2b30
OJ
13961 }
13962
13963 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13964 record_buf);
c6ec2b30 13965 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
dda83cd7 13966 record_buf_mem);
c6ec2b30
OJ
13967 return ARM_RECORD_SUCCESS;
13968}
13969
13970/* Handler for thumb2 data processing (shift register and modified immediate)
13971 instructions. */
13972
13973static int
4748a9be 13974thumb2_record_data_proc_sreg_mimm (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13975{
13976 uint32_t reg_rd, op;
13977 uint32_t record_buf[8];
13978
13979 op = bits (thumb2_insn_r->arm_insn, 21, 24);
13980 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
13981
13982 if ((0 == op || 4 == op || 8 == op || 13 == op) && 15 == reg_rd)
13983 {
13984 record_buf[0] = ARM_PS_REGNUM;
13985 thumb2_insn_r->reg_rec_count = 1;
13986 }
13987 else
13988 {
13989 record_buf[0] = reg_rd;
13990 record_buf[1] = ARM_PS_REGNUM;
13991 thumb2_insn_r->reg_rec_count = 2;
13992 }
13993
13994 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13995 record_buf);
c6ec2b30
OJ
13996 return ARM_RECORD_SUCCESS;
13997}
13998
13999/* Generic handler for thumb2 instructions which effect destination and PS
14000 registers. */
14001
14002static int
4748a9be 14003thumb2_record_ps_dest_generic (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
14004{
14005 uint32_t reg_rd;
14006 uint32_t record_buf[8];
14007
14008 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
14009
14010 record_buf[0] = reg_rd;
14011 record_buf[1] = ARM_PS_REGNUM;
14012 thumb2_insn_r->reg_rec_count = 2;
14013
14014 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 14015 record_buf);
c6ec2b30
OJ
14016 return ARM_RECORD_SUCCESS;
14017}
14018
14019/* Handler for thumb2 branch and miscellaneous control instructions. */
14020
14021static int
4748a9be 14022thumb2_record_branch_misc_cntrl (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
14023{
14024 uint32_t op, op1, op2;
14025 uint32_t record_buf[8];
14026
14027 op = bits (thumb2_insn_r->arm_insn, 20, 26);
14028 op1 = bits (thumb2_insn_r->arm_insn, 12, 14);
14029 op2 = bits (thumb2_insn_r->arm_insn, 8, 11);
14030
14031 /* Handle MSR insn. */
14032 if (!(op1 & 0x2) && 0x38 == op)
14033 {
14034 if (!(op2 & 0x3))
dda83cd7
SM
14035 {
14036 /* CPSR is going to be changed. */
14037 record_buf[0] = ARM_PS_REGNUM;
14038 thumb2_insn_r->reg_rec_count = 1;
14039 }
c6ec2b30 14040 else
dda83cd7
SM
14041 {
14042 arm_record_unsupported_insn(thumb2_insn_r);
14043 return -1;
14044 }
c6ec2b30
OJ
14045 }
14046 else if (4 == (op1 & 0x5) || 5 == (op1 & 0x5))
14047 {
14048 /* BLX. */
14049 record_buf[0] = ARM_PS_REGNUM;
14050 record_buf[1] = ARM_LR_REGNUM;
14051 thumb2_insn_r->reg_rec_count = 2;
14052 }
14053
14054 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 14055 record_buf);
c6ec2b30
OJ
14056 return ARM_RECORD_SUCCESS;
14057}
14058
14059/* Handler for thumb2 store single data item instructions. */
14060
14061static int
4748a9be 14062thumb2_record_str_single_data (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
14063{
14064 struct regcache *reg_cache = thumb2_insn_r->regcache;
14065
14066 uint32_t reg_rn, reg_rm, offset_imm, shift_imm;
14067 uint32_t address, offset_addr;
14068 uint32_t record_buf[8], record_buf_mem[8];
14069 uint32_t op1, op2;
14070
14071 ULONGEST u_regval[2];
14072
14073 op1 = bits (thumb2_insn_r->arm_insn, 21, 23);
14074 op2 = bits (thumb2_insn_r->arm_insn, 6, 11);
14075 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
14076 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
14077
14078 if (bit (thumb2_insn_r->arm_insn, 23))
14079 {
14080 /* T2 encoding. */
14081 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 11);
14082 offset_addr = u_regval[0] + offset_imm;
14083 address = offset_addr;
14084 }
14085 else
14086 {
14087 /* T3 encoding. */
14088 if ((0 == op1 || 1 == op1 || 2 == op1) && !(op2 & 0x20))
dda83cd7
SM
14089 {
14090 /* Handle STRB (register). */
14091 reg_rm = bits (thumb2_insn_r->arm_insn, 0, 3);
14092 regcache_raw_read_unsigned (reg_cache, reg_rm, &u_regval[1]);
14093 shift_imm = bits (thumb2_insn_r->arm_insn, 4, 5);
14094 offset_addr = u_regval[1] << shift_imm;
14095 address = u_regval[0] + offset_addr;
14096 }
c6ec2b30 14097 else
dda83cd7
SM
14098 {
14099 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
14100 if (bit (thumb2_insn_r->arm_insn, 10))
14101 {
14102 if (bit (thumb2_insn_r->arm_insn, 9))
14103 offset_addr = u_regval[0] + offset_imm;
14104 else
14105 offset_addr = u_regval[0] - offset_imm;
14106
14107 address = offset_addr;
14108 }
14109 else
14110 address = u_regval[0];
14111 }
c6ec2b30
OJ
14112 }
14113
14114 switch (op1)
14115 {
14116 /* Store byte instructions. */
14117 case 4:
14118 case 0:
dda83cd7
SM
14119 record_buf_mem[0] = 1;
14120 break;
c6ec2b30
OJ
14121 /* Store half word instructions. */
14122 case 1:
14123 case 5:
dda83cd7
SM
14124 record_buf_mem[0] = 2;
14125 break;
c6ec2b30
OJ
14126 /* Store word instructions. */
14127 case 2:
14128 case 6:
dda83cd7
SM
14129 record_buf_mem[0] = 4;
14130 break;
c6ec2b30
OJ
14131
14132 default:
dda83cd7
SM
14133 gdb_assert_not_reached ("no decoding pattern found");
14134 break;
c6ec2b30
OJ
14135 }
14136
14137 record_buf_mem[1] = address;
14138 thumb2_insn_r->mem_rec_count = 1;
14139 record_buf[0] = reg_rn;
14140 thumb2_insn_r->reg_rec_count = 1;
14141
14142 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 14143 record_buf);
c6ec2b30 14144 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
dda83cd7 14145 record_buf_mem);
c6ec2b30
OJ
14146 return ARM_RECORD_SUCCESS;
14147}
14148
14149/* Handler for thumb2 load memory hints instructions. */
14150
14151static int
4748a9be 14152thumb2_record_ld_mem_hints (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
14153{
14154 uint32_t record_buf[8];
14155 uint32_t reg_rt, reg_rn;
14156
14157 reg_rt = bits (thumb2_insn_r->arm_insn, 12, 15);
14158 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
14159
14160 if (ARM_PC_REGNUM != reg_rt)
14161 {
14162 record_buf[0] = reg_rt;
14163 record_buf[1] = reg_rn;
14164 record_buf[2] = ARM_PS_REGNUM;
14165 thumb2_insn_r->reg_rec_count = 3;
14166
14167 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 14168 record_buf);
c6ec2b30
OJ
14169 return ARM_RECORD_SUCCESS;
14170 }
14171
14172 return ARM_RECORD_FAILURE;
14173}
14174
14175/* Handler for thumb2 load word instructions. */
14176
14177static int
4748a9be 14178thumb2_record_ld_word (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30 14179{
c6ec2b30
OJ
14180 uint32_t record_buf[8];
14181
14182 record_buf[0] = bits (thumb2_insn_r->arm_insn, 12, 15);
14183 record_buf[1] = ARM_PS_REGNUM;
14184 thumb2_insn_r->reg_rec_count = 2;
14185
7e4208a3
TV
14186 if ((thumb2_insn_r->arm_insn & 0xfff00900) == 0xf8500900)
14187 {
14188 /* Detected LDR(immediate), T4, with write-back bit set. Record Rn
14189 update. */
14190 record_buf[2] = bits (thumb2_insn_r->arm_insn, 16, 19);
14191 thumb2_insn_r->reg_rec_count++;
14192 }
14193
c6ec2b30 14194 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 14195 record_buf);
c6ec2b30
OJ
14196 return ARM_RECORD_SUCCESS;
14197}
14198
14199/* Handler for thumb2 long multiply, long multiply accumulate, and
14200 divide instructions. */
14201
14202static int
4748a9be 14203thumb2_record_lmul_lmla_div (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
14204{
14205 uint32_t opcode1 = 0, opcode2 = 0;
14206 uint32_t record_buf[8];
c6ec2b30
OJ
14207
14208 opcode1 = bits (thumb2_insn_r->arm_insn, 20, 22);
14209 opcode2 = bits (thumb2_insn_r->arm_insn, 4, 7);
14210
14211 if (0 == opcode1 || 2 == opcode1 || (opcode1 >= 4 && opcode1 <= 6))
14212 {
14213 /* Handle SMULL, UMULL, SMULAL. */
14214 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
14215 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
14216 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
14217 record_buf[2] = ARM_PS_REGNUM;
14218 thumb2_insn_r->reg_rec_count = 3;
14219 }
14220 else if (1 == opcode1 || 3 == opcode2)
14221 {
14222 /* Handle SDIV and UDIV. */
14223 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
14224 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
14225 record_buf[2] = ARM_PS_REGNUM;
14226 thumb2_insn_r->reg_rec_count = 3;
14227 }
14228 else
14229 return ARM_RECORD_FAILURE;
14230
14231 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 14232 record_buf);
c6ec2b30
OJ
14233 return ARM_RECORD_SUCCESS;
14234}
14235
60cc5e93
OJ
14236/* Record handler for thumb32 coprocessor instructions. */
14237
14238static int
4748a9be 14239thumb2_record_coproc_insn (arm_insn_decode_record *thumb2_insn_r)
60cc5e93
OJ
14240{
14241 if (bit (thumb2_insn_r->arm_insn, 25))
14242 return arm_record_coproc_data_proc (thumb2_insn_r);
14243 else
14244 return arm_record_asimd_vfp_coproc (thumb2_insn_r);
14245}
14246
1e1b6563
OJ
14247/* Record handler for advance SIMD structure load/store instructions. */
14248
14249static int
4748a9be 14250thumb2_record_asimd_struct_ld_st (arm_insn_decode_record *thumb2_insn_r)
1e1b6563
OJ
14251{
14252 struct regcache *reg_cache = thumb2_insn_r->regcache;
14253 uint32_t l_bit, a_bit, b_bits;
14254 uint32_t record_buf[128], record_buf_mem[128];
bec2ab5a 14255 uint32_t reg_rn, reg_vd, address, f_elem;
1e1b6563
OJ
14256 uint32_t index_r = 0, index_e = 0, bf_regs = 0, index_m = 0, loop_t = 0;
14257 uint8_t f_ebytes;
14258
14259 l_bit = bit (thumb2_insn_r->arm_insn, 21);
14260 a_bit = bit (thumb2_insn_r->arm_insn, 23);
14261 b_bits = bits (thumb2_insn_r->arm_insn, 8, 11);
14262 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
14263 reg_vd = bits (thumb2_insn_r->arm_insn, 12, 15);
14264 reg_vd = (bit (thumb2_insn_r->arm_insn, 22) << 4) | reg_vd;
14265 f_ebytes = (1 << bits (thumb2_insn_r->arm_insn, 6, 7));
1e1b6563
OJ
14266 f_elem = 8 / f_ebytes;
14267
14268 if (!l_bit)
14269 {
14270 ULONGEST u_regval = 0;
14271 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
14272 address = u_regval;
14273
14274 if (!a_bit)
dda83cd7
SM
14275 {
14276 /* Handle VST1. */
14277 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
14278 {
14279 if (b_bits == 0x07)
14280 bf_regs = 1;
14281 else if (b_bits == 0x0a)
14282 bf_regs = 2;
14283 else if (b_bits == 0x06)
14284 bf_regs = 3;
14285 else if (b_bits == 0x02)
14286 bf_regs = 4;
14287 else
14288 bf_regs = 0;
14289
14290 for (index_r = 0; index_r < bf_regs; index_r++)
14291 {
14292 for (index_e = 0; index_e < f_elem; index_e++)
14293 {
14294 record_buf_mem[index_m++] = f_ebytes;
14295 record_buf_mem[index_m++] = address;
14296 address = address + f_ebytes;
14297 thumb2_insn_r->mem_rec_count += 1;
14298 }
14299 }
14300 }
14301 /* Handle VST2. */
14302 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
14303 {
14304 if (b_bits == 0x09 || b_bits == 0x08)
14305 bf_regs = 1;
14306 else if (b_bits == 0x03)
14307 bf_regs = 2;
14308 else
14309 bf_regs = 0;
14310
14311 for (index_r = 0; index_r < bf_regs; index_r++)
14312 for (index_e = 0; index_e < f_elem; index_e++)
14313 {
14314 for (loop_t = 0; loop_t < 2; loop_t++)
14315 {
14316 record_buf_mem[index_m++] = f_ebytes;
14317 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
14318 thumb2_insn_r->mem_rec_count += 1;
14319 }
14320 address = address + (2 * f_ebytes);
14321 }
14322 }
14323 /* Handle VST3. */
14324 else if ((b_bits & 0x0e) == 0x04)
14325 {
14326 for (index_e = 0; index_e < f_elem; index_e++)
14327 {
14328 for (loop_t = 0; loop_t < 3; loop_t++)
14329 {
14330 record_buf_mem[index_m++] = f_ebytes;
14331 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
14332 thumb2_insn_r->mem_rec_count += 1;
14333 }
14334 address = address + (3 * f_ebytes);
14335 }
14336 }
14337 /* Handle VST4. */
14338 else if (!(b_bits & 0x0e))
14339 {
14340 for (index_e = 0; index_e < f_elem; index_e++)
14341 {
14342 for (loop_t = 0; loop_t < 4; loop_t++)
14343 {
14344 record_buf_mem[index_m++] = f_ebytes;
14345 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
14346 thumb2_insn_r->mem_rec_count += 1;
14347 }
14348 address = address + (4 * f_ebytes);
14349 }
14350 }
14351 }
1e1b6563 14352 else
dda83cd7
SM
14353 {
14354 uint8_t bft_size = bits (thumb2_insn_r->arm_insn, 10, 11);
14355
14356 if (bft_size == 0x00)
14357 f_ebytes = 1;
14358 else if (bft_size == 0x01)
14359 f_ebytes = 2;
14360 else if (bft_size == 0x02)
14361 f_ebytes = 4;
14362 else
14363 f_ebytes = 0;
14364
14365 /* Handle VST1. */
14366 if (!(b_bits & 0x0b) || b_bits == 0x08)
14367 thumb2_insn_r->mem_rec_count = 1;
14368 /* Handle VST2. */
14369 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09)
14370 thumb2_insn_r->mem_rec_count = 2;
14371 /* Handle VST3. */
14372 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a)
14373 thumb2_insn_r->mem_rec_count = 3;
14374 /* Handle VST4. */
14375 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b)
14376 thumb2_insn_r->mem_rec_count = 4;
14377
14378 for (index_m = 0; index_m < thumb2_insn_r->mem_rec_count; index_m++)
14379 {
14380 record_buf_mem[index_m] = f_ebytes;
14381 record_buf_mem[index_m] = address + (index_m * f_ebytes);
14382 }
14383 }
1e1b6563
OJ
14384 }
14385 else
14386 {
14387 if (!a_bit)
dda83cd7
SM
14388 {
14389 /* Handle VLD1. */
14390 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
14391 thumb2_insn_r->reg_rec_count = 1;
14392 /* Handle VLD2. */
14393 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
14394 thumb2_insn_r->reg_rec_count = 2;
14395 /* Handle VLD3. */
14396 else if ((b_bits & 0x0e) == 0x04)
14397 thumb2_insn_r->reg_rec_count = 3;
14398 /* Handle VLD4. */
14399 else if (!(b_bits & 0x0e))
14400 thumb2_insn_r->reg_rec_count = 4;
14401 }
1e1b6563 14402 else
dda83cd7
SM
14403 {
14404 /* Handle VLD1. */
14405 if (!(b_bits & 0x0b) || b_bits == 0x08 || b_bits == 0x0c)
14406 thumb2_insn_r->reg_rec_count = 1;
14407 /* Handle VLD2. */
14408 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09 || b_bits == 0x0d)
14409 thumb2_insn_r->reg_rec_count = 2;
14410 /* Handle VLD3. */
14411 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a || b_bits == 0x0e)
14412 thumb2_insn_r->reg_rec_count = 3;
14413 /* Handle VLD4. */
14414 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b || b_bits == 0x0f)
14415 thumb2_insn_r->reg_rec_count = 4;
14416
14417 for (index_r = 0; index_r < thumb2_insn_r->reg_rec_count; index_r++)
14418 record_buf[index_r] = reg_vd + ARM_D0_REGNUM + index_r;
14419 }
1e1b6563
OJ
14420 }
14421
14422 if (bits (thumb2_insn_r->arm_insn, 0, 3) != 15)
14423 {
14424 record_buf[index_r] = reg_rn;
14425 thumb2_insn_r->reg_rec_count += 1;
14426 }
14427
14428 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 14429 record_buf);
1e1b6563 14430 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
dda83cd7 14431 record_buf_mem);
1e1b6563
OJ
14432 return 0;
14433}
14434
c6ec2b30
OJ
14435/* Decodes thumb2 instruction type and invokes its record handler. */
14436
14437static unsigned int
4748a9be 14438thumb2_record_decode_insn_handler (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
14439{
14440 uint32_t op, op1, op2;
14441
14442 op = bit (thumb2_insn_r->arm_insn, 15);
14443 op1 = bits (thumb2_insn_r->arm_insn, 27, 28);
14444 op2 = bits (thumb2_insn_r->arm_insn, 20, 26);
14445
14446 if (op1 == 0x01)
14447 {
14448 if (!(op2 & 0x64 ))
dda83cd7
SM
14449 {
14450 /* Load/store multiple instruction. */
14451 return thumb2_record_ld_st_multiple (thumb2_insn_r);
14452 }
b121eeb9 14453 else if ((op2 & 0x64) == 0x4)
dda83cd7
SM
14454 {
14455 /* Load/store (dual/exclusive) and table branch instruction. */
14456 return thumb2_record_ld_st_dual_ex_tbb (thumb2_insn_r);
14457 }
b121eeb9 14458 else if ((op2 & 0x60) == 0x20)
dda83cd7
SM
14459 {
14460 /* Data-processing (shifted register). */
14461 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
14462 }
c6ec2b30 14463 else if (op2 & 0x40)
dda83cd7
SM
14464 {
14465 /* Co-processor instructions. */
14466 return thumb2_record_coproc_insn (thumb2_insn_r);
14467 }
c6ec2b30
OJ
14468 }
14469 else if (op1 == 0x02)
14470 {
14471 if (op)
dda83cd7
SM
14472 {
14473 /* Branches and miscellaneous control instructions. */
14474 return thumb2_record_branch_misc_cntrl (thumb2_insn_r);
14475 }
c6ec2b30 14476 else if (op2 & 0x20)
dda83cd7
SM
14477 {
14478 /* Data-processing (plain binary immediate) instruction. */
14479 return thumb2_record_ps_dest_generic (thumb2_insn_r);
14480 }
c6ec2b30 14481 else
dda83cd7
SM
14482 {
14483 /* Data-processing (modified immediate). */
14484 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
14485 }
c6ec2b30
OJ
14486 }
14487 else if (op1 == 0x03)
14488 {
14489 if (!(op2 & 0x71 ))
dda83cd7
SM
14490 {
14491 /* Store single data item. */
14492 return thumb2_record_str_single_data (thumb2_insn_r);
14493 }
c6ec2b30 14494 else if (!((op2 & 0x71) ^ 0x10))
dda83cd7
SM
14495 {
14496 /* Advanced SIMD or structure load/store instructions. */
14497 return thumb2_record_asimd_struct_ld_st (thumb2_insn_r);
14498 }
c6ec2b30 14499 else if (!((op2 & 0x67) ^ 0x01))
dda83cd7
SM
14500 {
14501 /* Load byte, memory hints instruction. */
14502 return thumb2_record_ld_mem_hints (thumb2_insn_r);
14503 }
c6ec2b30 14504 else if (!((op2 & 0x67) ^ 0x03))
dda83cd7
SM
14505 {
14506 /* Load halfword, memory hints instruction. */
14507 return thumb2_record_ld_mem_hints (thumb2_insn_r);
14508 }
c6ec2b30 14509 else if (!((op2 & 0x67) ^ 0x05))
dda83cd7
SM
14510 {
14511 /* Load word instruction. */
14512 return thumb2_record_ld_word (thumb2_insn_r);
14513 }
c6ec2b30 14514 else if (!((op2 & 0x70) ^ 0x20))
dda83cd7
SM
14515 {
14516 /* Data-processing (register) instruction. */
14517 return thumb2_record_ps_dest_generic (thumb2_insn_r);
14518 }
c6ec2b30 14519 else if (!((op2 & 0x78) ^ 0x30))
dda83cd7
SM
14520 {
14521 /* Multiply, multiply accumulate, abs diff instruction. */
14522 return thumb2_record_ps_dest_generic (thumb2_insn_r);
14523 }
c6ec2b30 14524 else if (!((op2 & 0x78) ^ 0x38))
dda83cd7
SM
14525 {
14526 /* Long multiply, long multiply accumulate, and divide. */
14527 return thumb2_record_lmul_lmla_div (thumb2_insn_r);
14528 }
c6ec2b30 14529 else if (op2 & 0x40)
dda83cd7
SM
14530 {
14531 /* Co-processor instructions. */
14532 return thumb2_record_coproc_insn (thumb2_insn_r);
14533 }
c6ec2b30
OJ
14534 }
14535
14536 return -1;
14537}
72508ac0 14538
ffdbe864 14539namespace {
0833fb8f 14540/* Abstract instruction reader. */
728a7913 14541
0833fb8f 14542class abstract_instruction_reader
728a7913
YQ
14543{
14544public:
0833fb8f
LM
14545 /* Read one instruction of size LEN from address MEMADDR and using
14546 BYTE_ORDER endianness. */
728a7913 14547
0833fb8f
LM
14548 virtual ULONGEST read (CORE_ADDR memaddr, const size_t len,
14549 enum bfd_endian byte_order) = 0;
728a7913
YQ
14550};
14551
14552/* Instruction reader from real target. */
14553
0833fb8f 14554class instruction_reader : public abstract_instruction_reader
728a7913
YQ
14555{
14556 public:
0833fb8f
LM
14557 ULONGEST read (CORE_ADDR memaddr, const size_t len,
14558 enum bfd_endian byte_order) override
728a7913 14559 {
0833fb8f 14560 return read_code_unsigned_integer (memaddr, len, byte_order);
728a7913
YQ
14561 }
14562};
14563
ffdbe864
YQ
14564} // namespace
14565
4748a9be 14566typedef int (*sti_arm_hdl_fp_t) (arm_insn_decode_record*);
72508ac0
PO
14567
14568/* Decode arm/thumb insn depending on condition cods and opcodes; and
14569 dispatch it. */
14570
14571static int
0833fb8f 14572decode_insn (abstract_instruction_reader &reader,
4748a9be 14573 arm_insn_decode_record *arm_record,
728a7913 14574 record_type_t record_type, uint32_t insn_size)
72508ac0
PO
14575{
14576
01e57735
YQ
14577 /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm
14578 instruction. */
0fa9c223 14579 static const sti_arm_hdl_fp_t arm_handle_insn[8] =
72508ac0
PO
14580 {
14581 arm_record_data_proc_misc_ld_str, /* 000. */
14582 arm_record_data_proc_imm, /* 001. */
14583 arm_record_ld_st_imm_offset, /* 010. */
14584 arm_record_ld_st_reg_offset, /* 011. */
14585 arm_record_ld_st_multiple, /* 100. */
14586 arm_record_b_bl, /* 101. */
60cc5e93 14587 arm_record_asimd_vfp_coproc, /* 110. */
72508ac0
PO
14588 arm_record_coproc_data_proc /* 111. */
14589 };
14590
01e57735
YQ
14591 /* (Starting from numerical 0); bits 13,14,15 decodes type of thumb
14592 instruction. */
0fa9c223 14593 static const sti_arm_hdl_fp_t thumb_handle_insn[8] =
72508ac0
PO
14594 { \
14595 thumb_record_shift_add_sub, /* 000. */
14596 thumb_record_add_sub_cmp_mov, /* 001. */
14597 thumb_record_ld_st_reg_offset, /* 010. */
14598 thumb_record_ld_st_imm_offset, /* 011. */
14599 thumb_record_ld_st_stack, /* 100. */
14600 thumb_record_misc, /* 101. */
14601 thumb_record_ldm_stm_swi, /* 110. */
14602 thumb_record_branch /* 111. */
14603 };
14604
14605 uint32_t ret = 0; /* return value: negative:failure 0:success. */
14606 uint32_t insn_id = 0;
0833fb8f
LM
14607 enum bfd_endian code_endian
14608 = gdbarch_byte_order_for_code (arm_record->gdbarch);
14609 arm_record->arm_insn
14610 = reader.read (arm_record->this_addr, insn_size, code_endian);
72508ac0 14611
0833fb8f 14612 if (ARM_RECORD == record_type)
72508ac0
PO
14613 {
14614 arm_record->cond = bits (arm_record->arm_insn, 28, 31);
14615 insn_id = bits (arm_record->arm_insn, 25, 27);
ca92db2d
YQ
14616
14617 if (arm_record->cond == 0xf)
14618 ret = arm_record_extension_space (arm_record);
14619 else
01e57735 14620 {
ca92db2d
YQ
14621 /* If this insn has fallen into extension space
14622 then we need not decode it anymore. */
01e57735
YQ
14623 ret = arm_handle_insn[insn_id] (arm_record);
14624 }
ca92db2d
YQ
14625 if (ret != ARM_RECORD_SUCCESS)
14626 {
14627 arm_record_unsupported_insn (arm_record);
14628 ret = -1;
14629 }
72508ac0
PO
14630 }
14631 else if (THUMB_RECORD == record_type)
14632 {
14633 /* As thumb does not have condition codes, we set negative. */
14634 arm_record->cond = -1;
14635 insn_id = bits (arm_record->arm_insn, 13, 15);
14636 ret = thumb_handle_insn[insn_id] (arm_record);
ca92db2d
YQ
14637 if (ret != ARM_RECORD_SUCCESS)
14638 {
14639 arm_record_unsupported_insn (arm_record);
14640 ret = -1;
14641 }
72508ac0
PO
14642 }
14643 else if (THUMB2_RECORD == record_type)
14644 {
c6ec2b30
OJ
14645 /* As thumb does not have condition codes, we set negative. */
14646 arm_record->cond = -1;
14647
14648 /* Swap first half of 32bit thumb instruction with second half. */
14649 arm_record->arm_insn
01e57735 14650 = (arm_record->arm_insn >> 16) | (arm_record->arm_insn << 16);
c6ec2b30 14651
ca92db2d 14652 ret = thumb2_record_decode_insn_handler (arm_record);
c6ec2b30 14653
ca92db2d 14654 if (ret != ARM_RECORD_SUCCESS)
01e57735
YQ
14655 {
14656 arm_record_unsupported_insn (arm_record);
14657 ret = -1;
14658 }
72508ac0
PO
14659 }
14660 else
14661 {
14662 /* Throw assertion. */
14663 gdb_assert_not_reached ("not a valid instruction, could not decode");
14664 }
14665
14666 return ret;
14667}
14668
b121eeb9
YQ
14669#if GDB_SELF_TEST
14670namespace selftests {
14671
0833fb8f
LM
14672/* Instruction reader class for selftests.
14673
14674 For 16-bit Thumb instructions, an array of uint16_t should be used.
b121eeb9 14675
0833fb8f
LM
14676 For 32-bit Thumb instructions and regular 32-bit Arm instructions, an array
14677 of uint32_t should be used. */
14678
14679template<typename T>
14680class instruction_reader_selftest : public abstract_instruction_reader
b121eeb9
YQ
14681{
14682public:
14683 template<size_t SIZE>
0833fb8f
LM
14684 instruction_reader_selftest (const T (&insns)[SIZE])
14685 : m_insns (insns), m_insns_size (SIZE)
b121eeb9
YQ
14686 {}
14687
0833fb8f
LM
14688 ULONGEST read (CORE_ADDR memaddr, const size_t length,
14689 enum bfd_endian byte_order) override
b121eeb9 14690 {
0833fb8f
LM
14691 SELF_CHECK (length == sizeof (T));
14692 SELF_CHECK (memaddr % sizeof (T) == 0);
14693 SELF_CHECK ((memaddr / sizeof (T)) < m_insns_size);
b121eeb9 14694
0833fb8f 14695 return m_insns[memaddr / sizeof (T)];
b121eeb9
YQ
14696 }
14697
14698private:
0833fb8f
LM
14699 const T *m_insns;
14700 const size_t m_insns_size;
b121eeb9
YQ
14701};
14702
14703static void
14704arm_record_test (void)
14705{
14706 struct gdbarch_info info;
b121eeb9
YQ
14707 info.bfd_arch_info = bfd_scan_arch ("arm");
14708
14709 struct gdbarch *gdbarch = gdbarch_find_by_info (info);
14710
14711 SELF_CHECK (gdbarch != NULL);
14712
14713 /* 16-bit Thumb instructions. */
14714 {
4748a9be 14715 arm_insn_decode_record arm_record;
b121eeb9 14716
4748a9be 14717 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
b121eeb9
YQ
14718 arm_record.gdbarch = gdbarch;
14719
0833fb8f
LM
14720 /* Use the endian-free representation of the instructions here. The test
14721 will handle endianness conversions. */
b121eeb9
YQ
14722 static const uint16_t insns[] = {
14723 /* db b2 uxtb r3, r3 */
14724 0xb2db,
14725 /* cd 58 ldr r5, [r1, r3] */
14726 0x58cd,
14727 };
14728
0833fb8f 14729 instruction_reader_selftest<uint16_t> reader (insns);
b121eeb9
YQ
14730 int ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14731 THUMB_INSN_SIZE_BYTES);
14732
14733 SELF_CHECK (ret == 0);
14734 SELF_CHECK (arm_record.mem_rec_count == 0);
14735 SELF_CHECK (arm_record.reg_rec_count == 1);
14736 SELF_CHECK (arm_record.arm_regs[0] == 3);
14737
14738 arm_record.this_addr += 2;
14739 ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14740 THUMB_INSN_SIZE_BYTES);
14741
14742 SELF_CHECK (ret == 0);
14743 SELF_CHECK (arm_record.mem_rec_count == 0);
14744 SELF_CHECK (arm_record.reg_rec_count == 1);
14745 SELF_CHECK (arm_record.arm_regs[0] == 5);
14746 }
14747
14748 /* 32-bit Thumb-2 instructions. */
14749 {
4748a9be 14750 arm_insn_decode_record arm_record;
b121eeb9 14751
4748a9be 14752 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
b121eeb9
YQ
14753 arm_record.gdbarch = gdbarch;
14754
0833fb8f
LM
14755 /* Use the endian-free representation of the instruction here. The test
14756 will handle endianness conversions. */
14757 static const uint32_t insns[] = {
14758 /* mrc 15, 0, r7, cr13, cr0, {3} */
14759 0x7f70ee1d,
b121eeb9
YQ
14760 };
14761
0833fb8f 14762 instruction_reader_selftest<uint32_t> reader (insns);
b121eeb9
YQ
14763 int ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
14764 THUMB2_INSN_SIZE_BYTES);
14765
14766 SELF_CHECK (ret == 0);
14767 SELF_CHECK (arm_record.mem_rec_count == 0);
14768 SELF_CHECK (arm_record.reg_rec_count == 1);
14769 SELF_CHECK (arm_record.arm_regs[0] == 7);
14770 }
0833fb8f
LM
14771
14772 /* 32-bit instructions. */
14773 {
14774 arm_insn_decode_record arm_record;
14775
14776 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
14777 arm_record.gdbarch = gdbarch;
14778
14779 /* Use the endian-free representation of the instruction here. The test
14780 will handle endianness conversions. */
14781 static const uint32_t insns[] = {
14782 /* mov r5, r0 */
14783 0xe1a05000,
14784 };
14785
14786 instruction_reader_selftest<uint32_t> reader (insns);
14787 int ret = decode_insn (reader, &arm_record, ARM_RECORD,
14788 ARM_INSN_SIZE_BYTES);
14789
14790 SELF_CHECK (ret == 0);
14791 }
b121eeb9 14792}
9ecab40c
SM
14793
14794/* Instruction reader from manually cooked instruction sequences. */
14795
14796class test_arm_instruction_reader : public arm_instruction_reader
14797{
14798public:
14799 explicit test_arm_instruction_reader (gdb::array_view<const uint32_t> insns)
14800 : m_insns (insns)
14801 {}
14802
14803 uint32_t read (CORE_ADDR memaddr, enum bfd_endian byte_order) const override
14804 {
14805 SELF_CHECK (memaddr % 4 == 0);
14806 SELF_CHECK (memaddr / 4 < m_insns.size ());
14807
14808 return m_insns[memaddr / 4];
14809 }
14810
14811private:
14812 const gdb::array_view<const uint32_t> m_insns;
14813};
14814
14815static void
14816arm_analyze_prologue_test ()
14817{
14818 for (bfd_endian endianness : {BFD_ENDIAN_LITTLE, BFD_ENDIAN_BIG})
14819 {
14820 struct gdbarch_info info;
9ecab40c
SM
14821 info.byte_order = endianness;
14822 info.byte_order_for_code = endianness;
14823 info.bfd_arch_info = bfd_scan_arch ("arm");
14824
14825 struct gdbarch *gdbarch = gdbarch_find_by_info (info);
14826
14827 SELF_CHECK (gdbarch != NULL);
14828
14829 /* The "sub" instruction contains an immediate value rotate count of 0,
14830 which resulted in a 32-bit shift of a 32-bit value, caught by
14831 UBSan. */
14832 const uint32_t insns[] = {
14833 0xe92d4ff0, /* push {r4, r5, r6, r7, r8, r9, sl, fp, lr} */
14834 0xe1a05000, /* mov r5, r0 */
14835 0xe5903020, /* ldr r3, [r0, #32] */
14836 0xe24dd044, /* sub sp, sp, #68 ; 0x44 */
14837 };
14838
14839 test_arm_instruction_reader mem_reader (insns);
14840 arm_prologue_cache cache;
0824193f 14841 arm_cache_init (&cache, gdbarch);
9ecab40c
SM
14842
14843 arm_analyze_prologue (gdbarch, 0, sizeof (insns) - 1, &cache, mem_reader);
14844 }
14845}
14846
b121eeb9
YQ
14847} // namespace selftests
14848#endif /* GDB_SELF_TEST */
72508ac0
PO
14849
14850/* Cleans up local record registers and memory allocations. */
14851
14852static void
4748a9be 14853deallocate_reg_mem (arm_insn_decode_record *record)
72508ac0
PO
14854{
14855 xfree (record->arm_regs);
14856 xfree (record->arm_mems);
14857}
14858
14859
01e57735 14860/* Parse the current instruction and record the values of the registers and
72508ac0
PO
14861 memory that will be changed in current instruction to record_arch_list".
14862 Return -1 if something is wrong. */
14863
14864int
01e57735
YQ
14865arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
14866 CORE_ADDR insn_addr)
72508ac0
PO
14867{
14868
72508ac0
PO
14869 uint32_t no_of_rec = 0;
14870 uint32_t ret = 0; /* return value: -1:record failure ; 0:success */
14871 ULONGEST t_bit = 0, insn_id = 0;
14872
14873 ULONGEST u_regval = 0;
14874
4748a9be 14875 arm_insn_decode_record arm_record;
72508ac0 14876
4748a9be 14877 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
72508ac0
PO
14878 arm_record.regcache = regcache;
14879 arm_record.this_addr = insn_addr;
14880 arm_record.gdbarch = gdbarch;
14881
14882
14883 if (record_debug > 1)
14884 {
6cb06a8c
TT
14885 gdb_printf (gdb_stdlog, "Process record: arm_process_record "
14886 "addr = %s\n",
14887 paddress (gdbarch, arm_record.this_addr));
72508ac0
PO
14888 }
14889
728a7913 14890 instruction_reader reader;
0833fb8f
LM
14891 enum bfd_endian code_endian
14892 = gdbarch_byte_order_for_code (arm_record.gdbarch);
14893 arm_record.arm_insn
14894 = reader.read (arm_record.this_addr, 2, code_endian);
72508ac0
PO
14895
14896 /* Check the insn, whether it is thumb or arm one. */
14897
14898 t_bit = arm_psr_thumb_bit (arm_record.gdbarch);
14899 regcache_raw_read_unsigned (arm_record.regcache, ARM_PS_REGNUM, &u_regval);
14900
14901
14902 if (!(u_regval & t_bit))
14903 {
14904 /* We are decoding arm insn. */
728a7913 14905 ret = decode_insn (reader, &arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES);
72508ac0
PO
14906 }
14907 else
14908 {
14909 insn_id = bits (arm_record.arm_insn, 11, 15);
14910 /* is it thumb2 insn? */
14911 if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id))
01e57735 14912 {
728a7913 14913 ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
01e57735
YQ
14914 THUMB2_INSN_SIZE_BYTES);
14915 }
72508ac0 14916 else
01e57735
YQ
14917 {
14918 /* We are decoding thumb insn. */
728a7913
YQ
14919 ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14920 THUMB_INSN_SIZE_BYTES);
01e57735 14921 }
72508ac0
PO
14922 }
14923
14924 if (0 == ret)
14925 {
14926 /* Record registers. */
25ea693b 14927 record_full_arch_list_add_reg (arm_record.regcache, ARM_PC_REGNUM);
72508ac0 14928 if (arm_record.arm_regs)
01e57735
YQ
14929 {
14930 for (no_of_rec = 0; no_of_rec < arm_record.reg_rec_count; no_of_rec++)
14931 {
14932 if (record_full_arch_list_add_reg
25ea693b 14933 (arm_record.regcache , arm_record.arm_regs[no_of_rec]))
01e57735
YQ
14934 ret = -1;
14935 }
14936 }
72508ac0
PO
14937 /* Record memories. */
14938 if (arm_record.arm_mems)
01e57735
YQ
14939 {
14940 for (no_of_rec = 0; no_of_rec < arm_record.mem_rec_count; no_of_rec++)
14941 {
14942 if (record_full_arch_list_add_mem
14943 ((CORE_ADDR)arm_record.arm_mems[no_of_rec].addr,
25ea693b 14944 arm_record.arm_mems[no_of_rec].len))
01e57735
YQ
14945 ret = -1;
14946 }
14947 }
72508ac0 14948
25ea693b 14949 if (record_full_arch_list_add_end ())
01e57735 14950 ret = -1;
72508ac0
PO
14951 }
14952
14953
14954 deallocate_reg_mem (&arm_record);
14955
14956 return ret;
14957}
d105cce5
AH
14958
14959/* See arm-tdep.h. */
14960
14961const target_desc *
92d48a1e 14962arm_read_description (arm_fp_type fp_type, bool tls)
d105cce5 14963{
92d48a1e 14964 struct target_desc *tdesc = tdesc_arm_list[fp_type][tls];
d105cce5
AH
14965
14966 if (tdesc == nullptr)
14967 {
92d48a1e
JB
14968 tdesc = arm_create_target_description (fp_type, tls);
14969 tdesc_arm_list[fp_type][tls] = tdesc;
d105cce5
AH
14970 }
14971
14972 return tdesc;
14973}
14974
14975/* See arm-tdep.h. */
14976
14977const target_desc *
14978arm_read_mprofile_description (arm_m_profile_type m_type)
14979{
14980 struct target_desc *tdesc = tdesc_arm_mprofile_list[m_type];
14981
14982 if (tdesc == nullptr)
14983 {
14984 tdesc = arm_create_mprofile_target_description (m_type);
14985 tdesc_arm_mprofile_list[m_type] = tdesc;
14986 }
14987
14988 return tdesc;
14989}