]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/arm-tdep.c
[arm] Rename arm_cache_is_sp_register to arm_is_alternative_sp_register
[thirdparty/binutils-gdb.git] / gdb / arm-tdep.c
CommitLineData
ed9a39eb 1/* Common target dependent code for GDB on ARM systems.
0fd88904 2
4a94e368 3 Copyright (C) 1988-2022 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 19
0baeab03
PA
20#include "defs.h"
21
4de283e4 22#include <ctype.h> /* XXX for isupper (). */
34e8f22d 23
4de283e4
TT
24#include "frame.h"
25#include "inferior.h"
26#include "infrun.h"
27#include "gdbcmd.h"
28#include "gdbcore.h"
29#include "dis-asm.h" /* For register styles. */
30#include "disasm.h"
31#include "regcache.h"
32#include "reggroups.h"
33#include "target-float.h"
34#include "value.h"
d55e5aa6 35#include "arch-utils.h"
4de283e4
TT
36#include "osabi.h"
37#include "frame-unwind.h"
38#include "frame-base.h"
39#include "trad-frame.h"
40#include "objfiles.h"
a01567f4 41#include "dwarf2.h"
82ca8957 42#include "dwarf2/frame.h"
4de283e4
TT
43#include "gdbtypes.h"
44#include "prologue-value.h"
45#include "remote.h"
46#include "target-descriptions.h"
47#include "user-regs.h"
48#include "observable.h"
5f661e03 49#include "count-one-bits.h"
4de283e4 50
d55e5aa6 51#include "arch/arm.h"
4de283e4 52#include "arch/arm-get-next-pcs.h"
34e8f22d 53#include "arm-tdep.h"
4de283e4
TT
54#include "gdb/sim-arm.h"
55
d55e5aa6 56#include "elf-bfd.h"
4de283e4 57#include "coff/internal.h"
d55e5aa6 58#include "elf/arm.h"
4de283e4 59
4de283e4
TT
60#include "record.h"
61#include "record-full.h"
62#include <algorithm>
63
c2fd7fae
AKS
64#include "producer.h"
65
b121eeb9 66#if GDB_SELF_TEST
268a13a5 67#include "gdbsupport/selftest.h"
b121eeb9
YQ
68#endif
69
491144b5 70static bool arm_debug;
6529d2dd 71
7cb6d92a
SM
72/* Print an "arm" debug statement. */
73
74#define arm_debug_printf(fmt, ...) \
75 debug_prefixed_printf_cond (arm_debug, "arm", fmt, ##__VA_ARGS__)
76
082fc60d
RE
77/* Macros for setting and testing a bit in a minimal symbol that marks
78 it as Thumb function. The MSB of the minimal symbol's "info" field
f594e5e9 79 is used for this purpose.
082fc60d
RE
80
81 MSYMBOL_SET_SPECIAL Actually sets the "special" bit.
f594e5e9 82 MSYMBOL_IS_SPECIAL Tests the "special" bit in a minimal symbol. */
082fc60d 83
0963b4bd 84#define MSYMBOL_SET_SPECIAL(msym) \
e165fcef 85 (msym)->set_target_flag_1 (true)
082fc60d
RE
86
87#define MSYMBOL_IS_SPECIAL(msym) \
e165fcef 88 (msym)->target_flag_1 ()
082fc60d 89
60c5725c
DJ
90struct arm_mapping_symbol
91{
227031b2 92 CORE_ADDR value;
60c5725c 93 char type;
54cc7474
SM
94
95 bool operator< (const arm_mapping_symbol &other) const
96 { return this->value < other.value; }
60c5725c 97};
54cc7474
SM
98
99typedef std::vector<arm_mapping_symbol> arm_mapping_symbol_vec;
60c5725c 100
bd5766ec 101struct arm_per_bfd
60c5725c 102{
bd5766ec 103 explicit arm_per_bfd (size_t num_sections)
4838e44c
SM
104 : section_maps (new arm_mapping_symbol_vec[num_sections]),
105 section_maps_sorted (new bool[num_sections] ())
54cc7474
SM
106 {}
107
bd5766ec 108 DISABLE_COPY_AND_ASSIGN (arm_per_bfd);
54cc7474
SM
109
110 /* Information about mapping symbols ($a, $d, $t) in the objfile.
111
112 The format is an array of vectors of arm_mapping_symbols, there is one
113 vector for each section of the objfile (the array is index by BFD section
114 index).
115
116 For each section, the vector of arm_mapping_symbol is sorted by
117 symbol value (address). */
118 std::unique_ptr<arm_mapping_symbol_vec[]> section_maps;
4838e44c
SM
119
120 /* For each corresponding element of section_maps above, is this vector
121 sorted. */
122 std::unique_ptr<bool[]> section_maps_sorted;
60c5725c
DJ
123};
124
bd5766ec
LM
125/* Per-bfd data used for mapping symbols. */
126static bfd_key<arm_per_bfd> arm_bfd_data_key;
1b7f24cd 127
afd7eef0
RE
128/* The list of available "set arm ..." and "show arm ..." commands. */
129static struct cmd_list_element *setarmcmdlist = NULL;
130static struct cmd_list_element *showarmcmdlist = NULL;
131
fd50bc42
RE
132/* The type of floating-point to use. Keep this in sync with enum
133 arm_float_model, and the help string in _initialize_arm_tdep. */
40478521 134static const char *const fp_model_strings[] =
fd50bc42
RE
135{
136 "auto",
137 "softfpa",
138 "fpa",
139 "softvfp",
28e97307
DJ
140 "vfp",
141 NULL
fd50bc42
RE
142};
143
144/* A variable that can be configured by the user. */
145static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
146static const char *current_fp_model = "auto";
147
28e97307 148/* The ABI to use. Keep this in sync with arm_abi_kind. */
40478521 149static const char *const arm_abi_strings[] =
28e97307
DJ
150{
151 "auto",
152 "APCS",
153 "AAPCS",
154 NULL
155};
156
157/* A variable that can be configured by the user. */
158static enum arm_abi_kind arm_abi_global = ARM_ABI_AUTO;
159static const char *arm_abi_string = "auto";
160
0428b8f5 161/* The execution mode to assume. */
40478521 162static const char *const arm_mode_strings[] =
0428b8f5
DJ
163 {
164 "auto",
165 "arm",
68770265
MGD
166 "thumb",
167 NULL
0428b8f5
DJ
168 };
169
170static const char *arm_fallback_mode_string = "auto";
171static const char *arm_force_mode_string = "auto";
172
f32bf4a4
YQ
173/* The standard register names, and all the valid aliases for them. Note
174 that `fp', `sp' and `pc' are not added in this alias list, because they
175 have been added as builtin user registers in
176 std-regs.c:_initialize_frame_reg. */
123dc839
DJ
177static const struct
178{
179 const char *name;
180 int regnum;
181} arm_register_aliases[] = {
182 /* Basic register numbers. */
183 { "r0", 0 },
184 { "r1", 1 },
185 { "r2", 2 },
186 { "r3", 3 },
187 { "r4", 4 },
188 { "r5", 5 },
189 { "r6", 6 },
190 { "r7", 7 },
191 { "r8", 8 },
192 { "r9", 9 },
193 { "r10", 10 },
194 { "r11", 11 },
195 { "r12", 12 },
196 { "r13", 13 },
197 { "r14", 14 },
198 { "r15", 15 },
199 /* Synonyms (argument and variable registers). */
200 { "a1", 0 },
201 { "a2", 1 },
202 { "a3", 2 },
203 { "a4", 3 },
204 { "v1", 4 },
205 { "v2", 5 },
206 { "v3", 6 },
207 { "v4", 7 },
208 { "v5", 8 },
209 { "v6", 9 },
210 { "v7", 10 },
211 { "v8", 11 },
212 /* Other platform-specific names for r9. */
213 { "sb", 9 },
214 { "tr", 9 },
215 /* Special names. */
216 { "ip", 12 },
123dc839 217 { "lr", 14 },
123dc839
DJ
218 /* Names used by GCC (not listed in the ARM EABI). */
219 { "sl", 10 },
123dc839
DJ
220 /* A special name from the older ATPCS. */
221 { "wr", 7 },
222};
bc90b915 223
123dc839 224static const char *const arm_register_names[] =
da59e081
JM
225{"r0", "r1", "r2", "r3", /* 0 1 2 3 */
226 "r4", "r5", "r6", "r7", /* 4 5 6 7 */
227 "r8", "r9", "r10", "r11", /* 8 9 10 11 */
228 "r12", "sp", "lr", "pc", /* 12 13 14 15 */
229 "f0", "f1", "f2", "f3", /* 16 17 18 19 */
230 "f4", "f5", "f6", "f7", /* 20 21 22 23 */
94c30b78 231 "fps", "cpsr" }; /* 24 25 */
ed9a39eb 232
65b48a81
PB
233/* Holds the current set of options to be passed to the disassembler. */
234static char *arm_disassembler_options;
235
afd7eef0
RE
236/* Valid register name styles. */
237static const char **valid_disassembly_styles;
ed9a39eb 238
afd7eef0
RE
239/* Disassembly style to use. Default to "std" register names. */
240static const char *disassembly_style;
96baa820 241
d105cce5 242/* All possible arm target descriptors. */
92d48a1e 243static struct target_desc *tdesc_arm_list[ARM_FP_TYPE_INVALID][2];
d105cce5
AH
244static struct target_desc *tdesc_arm_mprofile_list[ARM_M_TYPE_INVALID];
245
ed9a39eb 246/* This is used to keep the bfd arch_info in sync with the disassembly
afd7eef0 247 style. */
eb4c3f4a 248static void set_disassembly_style_sfunc (const char *, int,
ed9a39eb 249 struct cmd_list_element *);
65b48a81
PB
250static void show_disassembly_style_sfunc (struct ui_file *, int,
251 struct cmd_list_element *,
252 const char *);
ed9a39eb 253
05d1431c 254static enum register_status arm_neon_quad_read (struct gdbarch *gdbarch,
849d0ba8 255 readable_regcache *regcache,
05d1431c 256 int regnum, gdb_byte *buf);
58d6951d
DJ
257static void arm_neon_quad_write (struct gdbarch *gdbarch,
258 struct regcache *regcache,
259 int regnum, const gdb_byte *buf);
260
e7cf25a8 261static CORE_ADDR
553cb527 262 arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self);
e7cf25a8
YQ
263
264
d9311bfa
AT
265/* get_next_pcs operations. */
266static struct arm_get_next_pcs_ops arm_get_next_pcs_ops = {
267 arm_get_next_pcs_read_memory_unsigned_integer,
268 arm_get_next_pcs_syscall_next_pc,
269 arm_get_next_pcs_addr_bits_remove,
ed443b61
YQ
270 arm_get_next_pcs_is_thumb,
271 NULL,
d9311bfa
AT
272};
273
9b8d791a 274struct arm_prologue_cache
c3b4394c 275{
eb5492fa
DJ
276 /* The stack pointer at the time this frame was created; i.e. the
277 caller's stack pointer when this function was called. It is used
278 to identify this frame. */
ae7e2f45
CL
279 CORE_ADDR sp;
280
281 /* Additional stack pointers used by M-profile with Security extension. */
282 /* Use msp_s / psp_s to hold the values of msp / psp when there is
283 no Security extension. */
284 CORE_ADDR msp_s;
285 CORE_ADDR msp_ns;
286 CORE_ADDR psp_s;
287 CORE_ADDR psp_ns;
288
289 /* Active stack pointer. */
290 int active_sp_regnum;
0d12d61b
YR
291 int active_msp_regnum;
292 int active_psp_regnum;
eb5492fa 293
4be43953
DJ
294 /* The frame base for this frame is just prev_sp - frame size.
295 FRAMESIZE is the distance from the frame pointer to the
296 initial stack pointer. */
eb5492fa 297
c3b4394c 298 int framesize;
eb5492fa
DJ
299
300 /* The register used to hold the frame pointer for this frame. */
c3b4394c 301 int framereg;
eb5492fa 302
a01567f4
LM
303 /* True if the return address is signed, false otherwise. */
304 gdb::optional<bool> ra_signed_state;
305
eb5492fa 306 /* Saved register offsets. */
098caef4 307 trad_frame_saved_reg *saved_regs;
0824193f
CL
308
309 arm_prologue_cache() = default;
c3b4394c 310};
ed9a39eb 311
8c9ae6df
YR
312
313/* Reconstruct T bit in program status register from LR value. */
314
315static inline ULONGEST
316reconstruct_t_bit(struct gdbarch *gdbarch, CORE_ADDR lr, ULONGEST psr)
317{
318 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
319 if (IS_THUMB_ADDR (lr))
320 psr |= t_bit;
321 else
322 psr &= ~t_bit;
323
324 return psr;
325}
326
ae7e2f45
CL
327/* Initialize stack pointers, and flag the active one. */
328
329static inline void
330arm_cache_init_sp (int regnum, CORE_ADDR* member,
331 struct arm_prologue_cache *cache,
332 struct frame_info *frame)
333{
334 CORE_ADDR val = get_frame_register_unsigned (frame, regnum);
335 if (val == cache->sp)
336 cache->active_sp_regnum = regnum;
337
338 *member = val;
339}
340
0824193f
CL
341/* Initialize CACHE fields for which zero is not adequate (CACHE is
342 expected to have been ZALLOC'ed before calling this function). */
343
344static void
345arm_cache_init (struct arm_prologue_cache *cache, struct gdbarch *gdbarch)
346{
ae7e2f45
CL
347 cache->active_sp_regnum = ARM_SP_REGNUM;
348
0824193f
CL
349 cache->saved_regs = trad_frame_alloc_saved_regs (gdbarch);
350}
351
352/* Similar to the previous function, but extracts GDBARCH from FRAME. */
353
354static void
355arm_cache_init (struct arm_prologue_cache *cache, struct frame_info *frame)
356{
357 struct gdbarch *gdbarch = get_frame_arch (frame);
ae7e2f45 358 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
0824193f
CL
359
360 arm_cache_init (cache, gdbarch);
fe642a5b 361 cache->sp = get_frame_register_unsigned (frame, ARM_SP_REGNUM);
ae7e2f45
CL
362
363 if (tdep->have_sec_ext)
364 {
0d12d61b
YR
365 CORE_ADDR msp_val = get_frame_register_unsigned (frame, tdep->m_profile_msp_regnum);
366 CORE_ADDR psp_val = get_frame_register_unsigned (frame, tdep->m_profile_psp_regnum);
367
ae7e2f45
CL
368 arm_cache_init_sp (tdep->m_profile_msp_s_regnum, &cache->msp_s, cache, frame);
369 arm_cache_init_sp (tdep->m_profile_psp_s_regnum, &cache->psp_s, cache, frame);
370 arm_cache_init_sp (tdep->m_profile_msp_ns_regnum, &cache->msp_ns, cache, frame);
371 arm_cache_init_sp (tdep->m_profile_psp_ns_regnum, &cache->psp_ns, cache, frame);
372
0d12d61b
YR
373 if (msp_val == cache->msp_s)
374 cache->active_msp_regnum = tdep->m_profile_msp_s_regnum;
375 else if (msp_val == cache->msp_ns)
376 cache->active_msp_regnum = tdep->m_profile_msp_ns_regnum;
377 if (psp_val == cache->psp_s)
378 cache->active_psp_regnum = tdep->m_profile_psp_s_regnum;
379 else if (psp_val == cache->psp_ns)
380 cache->active_psp_regnum = tdep->m_profile_psp_ns_regnum;
381
ae7e2f45
CL
382 /* Use MSP_S as default stack pointer. */
383 if (cache->active_sp_regnum == ARM_SP_REGNUM)
384 cache->active_sp_regnum = tdep->m_profile_msp_s_regnum;
385 }
386 else if (tdep->is_m)
387 {
388 arm_cache_init_sp (tdep->m_profile_msp_regnum, &cache->msp_s, cache, frame);
389 arm_cache_init_sp (tdep->m_profile_psp_regnum, &cache->psp_s, cache, frame);
390 }
391 else
392 arm_cache_init_sp (ARM_SP_REGNUM, &cache->msp_s, cache, frame);
393}
394
395/* Return the requested stack pointer value (in REGNUM), taking into
396 account whether we have a Security extension or an M-profile
397 CPU. */
398
399static CORE_ADDR
400arm_cache_get_sp_register (struct arm_prologue_cache *cache,
401 arm_gdbarch_tdep *tdep, int regnum)
402{
ae7e2f45
CL
403 if (tdep->have_sec_ext)
404 {
405 if (regnum == tdep->m_profile_msp_s_regnum)
406 return cache->msp_s;
407 if (regnum == tdep->m_profile_msp_ns_regnum)
408 return cache->msp_ns;
409 if (regnum == tdep->m_profile_psp_s_regnum)
410 return cache->psp_s;
411 if (regnum == tdep->m_profile_psp_ns_regnum)
412 return cache->psp_ns;
0d12d61b
YR
413 if (regnum == tdep->m_profile_msp_regnum)
414 return arm_cache_get_sp_register (cache, tdep, cache->active_msp_regnum);
415 if (regnum == tdep->m_profile_psp_regnum)
416 return arm_cache_get_sp_register (cache, tdep, cache->active_psp_regnum);
b9b66a3a
YR
417 if (regnum == ARM_SP_REGNUM)
418 return arm_cache_get_sp_register (cache, tdep, cache->active_sp_regnum);
ae7e2f45
CL
419 }
420 else if (tdep->is_m)
421 {
422 if (regnum == tdep->m_profile_msp_regnum)
423 return cache->msp_s;
424 if (regnum == tdep->m_profile_psp_regnum)
425 return cache->psp_s;
b9b66a3a
YR
426 if (regnum == ARM_SP_REGNUM)
427 return arm_cache_get_sp_register (cache, tdep, cache->active_sp_regnum);
ae7e2f45 428 }
b9b66a3a
YR
429 else if (regnum == ARM_SP_REGNUM)
430 return cache->sp;
ae7e2f45
CL
431
432 gdb_assert_not_reached ("Invalid SP selection");
433}
434
435/* Return the previous stack address, depending on which SP register
436 is active. */
437
438static CORE_ADDR
439arm_cache_get_prev_sp_value (struct arm_prologue_cache *cache, arm_gdbarch_tdep *tdep)
440{
441 CORE_ADDR val = arm_cache_get_sp_register (cache, tdep, cache->active_sp_regnum);
442 return val;
443}
444
445/* Set the active stack pointer to VAL. */
446
447static void
448arm_cache_set_active_sp_value (struct arm_prologue_cache *cache,
449 arm_gdbarch_tdep *tdep, CORE_ADDR val)
450{
ae7e2f45
CL
451 if (tdep->have_sec_ext)
452 {
453 if (cache->active_sp_regnum == tdep->m_profile_msp_s_regnum)
454 cache->msp_s = val;
455 else if (cache->active_sp_regnum == tdep->m_profile_msp_ns_regnum)
456 cache->msp_ns = val;
457 else if (cache->active_sp_regnum == tdep->m_profile_psp_s_regnum)
458 cache->psp_s = val;
459 else if (cache->active_sp_regnum == tdep->m_profile_psp_ns_regnum)
460 cache->psp_ns = val;
461
462 return;
463 }
464 else if (tdep->is_m)
465 {
466 if (cache->active_sp_regnum == tdep->m_profile_msp_regnum)
467 cache->msp_s = val;
468 else if (cache->active_sp_regnum == tdep->m_profile_psp_regnum)
469 cache->psp_s = val;
470
471 return;
472 }
b9b66a3a
YR
473 else if (cache->active_sp_regnum == ARM_SP_REGNUM)
474 {
475 cache->sp = val;
476 return;
477 }
ae7e2f45
CL
478
479 gdb_assert_not_reached ("Invalid SP selection");
0824193f
CL
480}
481
d65edaa0 482/* Return true if REGNUM is one of the alternative stack pointers. */
ef273377
CL
483
484static bool
d65edaa0 485arm_is_alternative_sp_register (arm_gdbarch_tdep *tdep, int regnum)
ef273377 486{
d65edaa0 487 if ((regnum == tdep->m_profile_msp_regnum)
ef273377
CL
488 || (regnum == tdep->m_profile_msp_s_regnum)
489 || (regnum == tdep->m_profile_msp_ns_regnum)
490 || (regnum == tdep->m_profile_psp_regnum)
491 || (regnum == tdep->m_profile_psp_s_regnum)
492 || (regnum == tdep->m_profile_psp_ns_regnum))
493 return true;
494 else
495 return false;
496}
497
498/* Set the active stack pointer to SP_REGNUM. */
499
500static void
501arm_cache_switch_prev_sp (struct arm_prologue_cache *cache,
502 arm_gdbarch_tdep *tdep, int sp_regnum)
503{
d65edaa0 504 gdb_assert (arm_is_alternative_sp_register (tdep, sp_regnum));
ef273377
CL
505
506 if (tdep->have_sec_ext)
507 gdb_assert (sp_regnum != tdep->m_profile_msp_regnum
508 && sp_regnum != tdep->m_profile_psp_regnum);
509
510 cache->active_sp_regnum = sp_regnum;
511}
512
9ecab40c
SM
513namespace {
514
515/* Abstract class to read ARM instructions from memory. */
516
517class arm_instruction_reader
518{
519public:
2c5b1849 520 /* Read a 4 bytes instruction from memory using the BYTE_ORDER endianness. */
9ecab40c
SM
521 virtual uint32_t read (CORE_ADDR memaddr, bfd_endian byte_order) const = 0;
522};
523
524/* Read instructions from target memory. */
525
526class target_arm_instruction_reader : public arm_instruction_reader
527{
528public:
529 uint32_t read (CORE_ADDR memaddr, bfd_endian byte_order) const override
530 {
531 return read_code_unsigned_integer (memaddr, 4, byte_order);
532 }
533};
534
535} /* namespace */
536
537static CORE_ADDR arm_analyze_prologue
538 (struct gdbarch *gdbarch, CORE_ADDR prologue_start, CORE_ADDR prologue_end,
539 struct arm_prologue_cache *cache, const arm_instruction_reader &insn_reader);
0d39a070 540
cca44b1b
JB
541/* Architecture version for displaced stepping. This effects the behaviour of
542 certain instructions, and really should not be hard-wired. */
543
544#define DISPLACED_STEPPING_ARCH_VERSION 5
545
c7ae7675 546/* See arm-tdep.h. */
c906108c 547
491144b5 548bool arm_apcs_32 = true;
ef273377 549bool arm_unwind_secure_frames = true;
c906108c 550
9779414d
DJ
551/* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode. */
552
478fd957 553int
9779414d
DJ
554arm_psr_thumb_bit (struct gdbarch *gdbarch)
555{
345bd07c
SM
556 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
557
558 if (tdep->is_m)
9779414d
DJ
559 return XPSR_T;
560 else
561 return CPSR_T;
562}
563
d0e59a68
AT
564/* Determine if the processor is currently executing in Thumb mode. */
565
566int
567arm_is_thumb (struct regcache *regcache)
568{
569 ULONGEST cpsr;
ac7936df 570 ULONGEST t_bit = arm_psr_thumb_bit (regcache->arch ());
d0e59a68
AT
571
572 cpsr = regcache_raw_get_unsigned (regcache, ARM_PS_REGNUM);
573
574 return (cpsr & t_bit) != 0;
575}
576
b39cc962
DJ
577/* Determine if FRAME is executing in Thumb mode. */
578
25b41d01 579int
b39cc962
DJ
580arm_frame_is_thumb (struct frame_info *frame)
581{
582 CORE_ADDR cpsr;
9779414d 583 ULONGEST t_bit = arm_psr_thumb_bit (get_frame_arch (frame));
b39cc962
DJ
584
585 /* Every ARM frame unwinder can unwind the T bit of the CPSR, either
586 directly (from a signal frame or dummy frame) or by interpreting
587 the saved LR (from a prologue or DWARF frame). So consult it and
588 trust the unwinders. */
589 cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
590
9779414d 591 return (cpsr & t_bit) != 0;
b39cc962
DJ
592}
593
f9d67f43
DJ
594/* Search for the mapping symbol covering MEMADDR. If one is found,
595 return its type. Otherwise, return 0. If START is non-NULL,
596 set *START to the location of the mapping symbol. */
c906108c 597
f9d67f43
DJ
598static char
599arm_find_mapping_symbol (CORE_ADDR memaddr, CORE_ADDR *start)
c906108c 600{
60c5725c 601 struct obj_section *sec;
0428b8f5 602
60c5725c
DJ
603 /* If there are mapping symbols, consult them. */
604 sec = find_pc_section (memaddr);
605 if (sec != NULL)
606 {
bd5766ec 607 arm_per_bfd *data = arm_bfd_data_key.get (sec->objfile->obfd);
60c5725c
DJ
608 if (data != NULL)
609 {
4838e44c
SM
610 unsigned int section_idx = sec->the_bfd_section->index;
611 arm_mapping_symbol_vec &map
612 = data->section_maps[section_idx];
613
614 /* Sort the vector on first use. */
615 if (!data->section_maps_sorted[section_idx])
616 {
617 std::sort (map.begin (), map.end ());
618 data->section_maps_sorted[section_idx] = true;
619 }
620
0c1bcd23 621 arm_mapping_symbol map_key = { memaddr - sec->addr (), 0 };
54cc7474
SM
622 arm_mapping_symbol_vec::const_iterator it
623 = std::lower_bound (map.begin (), map.end (), map_key);
624
625 /* std::lower_bound finds the earliest ordered insertion
626 point. If the symbol at this position starts at this exact
627 address, we use that; otherwise, the preceding
628 mapping symbol covers this address. */
629 if (it < map.end ())
60c5725c 630 {
54cc7474 631 if (it->value == map_key.value)
60c5725c 632 {
f9d67f43 633 if (start)
0c1bcd23 634 *start = it->value + sec->addr ();
54cc7474 635 return it->type;
60c5725c
DJ
636 }
637 }
54cc7474
SM
638
639 if (it > map.begin ())
640 {
641 arm_mapping_symbol_vec::const_iterator prev_it
642 = it - 1;
643
644 if (start)
0c1bcd23 645 *start = prev_it->value + sec->addr ();
54cc7474
SM
646 return prev_it->type;
647 }
60c5725c
DJ
648 }
649 }
650
f9d67f43
DJ
651 return 0;
652}
653
654/* Determine if the program counter specified in MEMADDR is in a Thumb
655 function. This function should be called for addresses unrelated to
656 any executing frame; otherwise, prefer arm_frame_is_thumb. */
657
e3039479 658int
9779414d 659arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
f9d67f43 660{
7cbd4a93 661 struct bound_minimal_symbol sym;
f9d67f43 662 char type;
187b041e 663 arm_displaced_step_copy_insn_closure *dsc = nullptr;
345bd07c 664 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
187b041e
SM
665
666 if (gdbarch_displaced_step_copy_insn_closure_by_addr_p (gdbarch))
667 dsc = ((arm_displaced_step_copy_insn_closure * )
668 gdbarch_displaced_step_copy_insn_closure_by_addr
669 (gdbarch, current_inferior (), memaddr));
a42244db
YQ
670
671 /* If checking the mode of displaced instruction in copy area, the mode
672 should be determined by instruction on the original address. */
673 if (dsc)
674 {
136821d9
SM
675 displaced_debug_printf ("check mode of %.8lx instead of %.8lx",
676 (unsigned long) dsc->insn_addr,
677 (unsigned long) memaddr);
a42244db
YQ
678 memaddr = dsc->insn_addr;
679 }
f9d67f43
DJ
680
681 /* If bit 0 of the address is set, assume this is a Thumb address. */
682 if (IS_THUMB_ADDR (memaddr))
683 return 1;
684
685 /* If the user wants to override the symbol table, let him. */
686 if (strcmp (arm_force_mode_string, "arm") == 0)
687 return 0;
688 if (strcmp (arm_force_mode_string, "thumb") == 0)
689 return 1;
690
9779414d 691 /* ARM v6-M and v7-M are always in Thumb mode. */
345bd07c 692 if (tdep->is_m)
9779414d
DJ
693 return 1;
694
f9d67f43
DJ
695 /* If there are mapping symbols, consult them. */
696 type = arm_find_mapping_symbol (memaddr, NULL);
697 if (type)
698 return type == 't';
699
ed9a39eb 700 /* Thumb functions have a "special" bit set in minimal symbols. */
c906108c 701 sym = lookup_minimal_symbol_by_pc (memaddr);
7cbd4a93
TT
702 if (sym.minsym)
703 return (MSYMBOL_IS_SPECIAL (sym.minsym));
0428b8f5
DJ
704
705 /* If the user wants to override the fallback mode, let them. */
706 if (strcmp (arm_fallback_mode_string, "arm") == 0)
707 return 0;
708 if (strcmp (arm_fallback_mode_string, "thumb") == 0)
709 return 1;
710
711 /* If we couldn't find any symbol, but we're talking to a running
712 target, then trust the current value of $cpsr. This lets
713 "display/i $pc" always show the correct mode (though if there is
714 a symbol table we will not reach here, so it still may not be
18819fa6 715 displayed in the mode it will be executed). */
9dccd06e 716 if (target_has_registers ())
18819fa6 717 return arm_frame_is_thumb (get_current_frame ());
0428b8f5
DJ
718
719 /* Otherwise we're out of luck; we assume ARM. */
720 return 0;
c906108c
SS
721}
722
ca90e760 723/* Determine if the address specified equals any of these magic return
55ea94da 724 values, called EXC_RETURN, defined by the ARM v6-M, v7-M and v8-M
ca90e760
FH
725 architectures.
726
727 From ARMv6-M Reference Manual B1.5.8
728 Table B1-5 Exception return behavior
729
730 EXC_RETURN Return To Return Stack
731 0xFFFFFFF1 Handler mode Main
732 0xFFFFFFF9 Thread mode Main
733 0xFFFFFFFD Thread mode Process
734
735 From ARMv7-M Reference Manual B1.5.8
736 Table B1-8 EXC_RETURN definition of exception return behavior, no FP
737
738 EXC_RETURN Return To Return Stack
739 0xFFFFFFF1 Handler mode Main
740 0xFFFFFFF9 Thread mode Main
741 0xFFFFFFFD Thread mode Process
742
743 Table B1-9 EXC_RETURN definition of exception return behavior, with
744 FP
745
746 EXC_RETURN Return To Return Stack Frame Type
747 0xFFFFFFE1 Handler mode Main Extended
748 0xFFFFFFE9 Thread mode Main Extended
749 0xFFFFFFED Thread mode Process Extended
750 0xFFFFFFF1 Handler mode Main Basic
751 0xFFFFFFF9 Thread mode Main Basic
752 0xFFFFFFFD Thread mode Process Basic
753
754 For more details see "B1.5.8 Exception return behavior"
55ea94da
FH
755 in both ARMv6-M and ARMv7-M Architecture Reference Manuals.
756
757 In the ARMv8-M Architecture Technical Reference also adds
758 for implementations without the Security Extension:
759
760 EXC_RETURN Condition
761 0xFFFFFFB0 Return to Handler mode.
762 0xFFFFFFB8 Return to Thread mode using the main stack.
763 0xFFFFFFBC Return to Thread mode using the process stack. */
ca90e760
FH
764
765static int
ef273377
CL
766arm_m_addr_is_magic (struct gdbarch *gdbarch, CORE_ADDR addr)
767{
768 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
769 if (tdep->have_sec_ext)
770 {
771 switch ((addr & 0xff000000))
772 {
773 case 0xff000000: /* EXC_RETURN pattern. */
774 case 0xfe000000: /* FNC_RETURN pattern. */
775 return 1;
776 default:
777 return 0;
778 }
779 }
780 else
781 {
782 switch (addr)
783 {
784 /* Values from ARMv8-M Architecture Technical Reference. */
785 case 0xffffffb0:
786 case 0xffffffb8:
787 case 0xffffffbc:
788 /* Values from Tables in B1.5.8 the EXC_RETURN definitions of
789 the exception return behavior. */
790 case 0xffffffe1:
791 case 0xffffffe9:
792 case 0xffffffed:
793 case 0xfffffff1:
794 case 0xfffffff9:
795 case 0xfffffffd:
796 /* Address is magic. */
797 return 1;
ca90e760 798
ef273377
CL
799 default:
800 /* Address is not magic. */
801 return 0;
802 }
ca90e760
FH
803 }
804}
805
181c1381 806/* Remove useless bits from addresses in a running program. */
34e8f22d 807static CORE_ADDR
24568a2c 808arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
c906108c 809{
345bd07c
SM
810 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
811
2ae28aa9
YQ
812 /* On M-profile devices, do not strip the low bit from EXC_RETURN
813 (the magic exception return address). */
ef273377 814 if (tdep->is_m && arm_m_addr_is_magic (gdbarch, val))
2ae28aa9
YQ
815 return val;
816
a3a2ee65 817 if (arm_apcs_32)
dd6be234 818 return UNMAKE_THUMB_ADDR (val);
c906108c 819 else
a3a2ee65 820 return (val & 0x03fffffc);
c906108c
SS
821}
822
0d39a070 823/* Return 1 if PC is the start of a compiler helper function which
e0634ccf
UW
824 can be safely ignored during prologue skipping. IS_THUMB is true
825 if the function is known to be a Thumb function due to the way it
826 is being called. */
0d39a070 827static int
e0634ccf 828skip_prologue_function (struct gdbarch *gdbarch, CORE_ADDR pc, int is_thumb)
0d39a070 829{
e0634ccf 830 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7cbd4a93 831 struct bound_minimal_symbol msym;
0d39a070
DJ
832
833 msym = lookup_minimal_symbol_by_pc (pc);
7cbd4a93 834 if (msym.minsym != NULL
4aeddc50 835 && msym.value_address () == pc
c9d95fa3 836 && msym.minsym->linkage_name () != NULL)
e0634ccf 837 {
c9d95fa3 838 const char *name = msym.minsym->linkage_name ();
0d39a070 839
e0634ccf
UW
840 /* The GNU linker's Thumb call stub to foo is named
841 __foo_from_thumb. */
842 if (strstr (name, "_from_thumb") != NULL)
843 name += 2;
0d39a070 844
e0634ccf
UW
845 /* On soft-float targets, __truncdfsf2 is called to convert promoted
846 arguments to their argument types in non-prototyped
847 functions. */
61012eef 848 if (startswith (name, "__truncdfsf2"))
e0634ccf 849 return 1;
61012eef 850 if (startswith (name, "__aeabi_d2f"))
e0634ccf 851 return 1;
0d39a070 852
e0634ccf 853 /* Internal functions related to thread-local storage. */
61012eef 854 if (startswith (name, "__tls_get_addr"))
e0634ccf 855 return 1;
61012eef 856 if (startswith (name, "__aeabi_read_tp"))
e0634ccf
UW
857 return 1;
858 }
859 else
860 {
861 /* If we run against a stripped glibc, we may be unable to identify
862 special functions by name. Check for one important case,
863 __aeabi_read_tp, by comparing the *code* against the default
864 implementation (this is hand-written ARM assembler in glibc). */
865
866 if (!is_thumb
198cd59d 867 && read_code_unsigned_integer (pc, 4, byte_order_for_code)
e0634ccf 868 == 0xe3e00a0f /* mov r0, #0xffff0fff */
198cd59d 869 && read_code_unsigned_integer (pc + 4, 4, byte_order_for_code)
e0634ccf
UW
870 == 0xe240f01f) /* sub pc, r0, #31 */
871 return 1;
872 }
ec3d575a 873
0d39a070
DJ
874 return 0;
875}
876
621c6d5b
YQ
877/* Extract the immediate from instruction movw/movt of encoding T. INSN1 is
878 the first 16-bit of instruction, and INSN2 is the second 16-bit of
879 instruction. */
880#define EXTRACT_MOVW_MOVT_IMM_T(insn1, insn2) \
881 ((bits ((insn1), 0, 3) << 12) \
882 | (bits ((insn1), 10, 10) << 11) \
883 | (bits ((insn2), 12, 14) << 8) \
884 | bits ((insn2), 0, 7))
885
886/* Extract the immediate from instruction movw/movt of encoding A. INSN is
887 the 32-bit instruction. */
888#define EXTRACT_MOVW_MOVT_IMM_A(insn) \
889 ((bits ((insn), 16, 19) << 12) \
890 | bits ((insn), 0, 11))
891
ec3d575a
UW
892/* Decode immediate value; implements ThumbExpandImmediate pseudo-op. */
893
894static unsigned int
895thumb_expand_immediate (unsigned int imm)
896{
897 unsigned int count = imm >> 7;
898
899 if (count < 8)
900 switch (count / 2)
901 {
902 case 0:
903 return imm & 0xff;
904 case 1:
905 return (imm & 0xff) | ((imm & 0xff) << 16);
906 case 2:
907 return ((imm & 0xff) << 8) | ((imm & 0xff) << 24);
908 case 3:
909 return (imm & 0xff) | ((imm & 0xff) << 8)
910 | ((imm & 0xff) << 16) | ((imm & 0xff) << 24);
911 }
912
913 return (0x80 | (imm & 0x7f)) << (32 - count);
914}
915
540314bd
YQ
916/* Return 1 if the 16-bit Thumb instruction INSN restores SP in
917 epilogue, 0 otherwise. */
918
919static int
920thumb_instruction_restores_sp (unsigned short insn)
921{
922 return (insn == 0x46bd /* mov sp, r7 */
923 || (insn & 0xff80) == 0xb000 /* add sp, imm */
924 || (insn & 0xfe00) == 0xbc00); /* pop <registers> */
925}
926
29d73ae4
DJ
927/* Analyze a Thumb prologue, looking for a recognizable stack frame
928 and frame pointer. Scan until we encounter a store that could
0d39a070
DJ
929 clobber the stack frame unexpectedly, or an unknown instruction.
930 Return the last address which is definitely safe to skip for an
931 initial breakpoint. */
c906108c
SS
932
933static CORE_ADDR
29d73ae4
DJ
934thumb_analyze_prologue (struct gdbarch *gdbarch,
935 CORE_ADDR start, CORE_ADDR limit,
936 struct arm_prologue_cache *cache)
c906108c 937{
10245fe8 938 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
0d39a070 939 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
e17a4113 940 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
29d73ae4
DJ
941 int i;
942 pv_t regs[16];
29d73ae4 943 CORE_ADDR offset;
ec3d575a 944 CORE_ADDR unrecognized_pc = 0;
da3c6d4a 945
29d73ae4
DJ
946 for (i = 0; i < 16; i++)
947 regs[i] = pv_register (i, 0);
f7b7ed97 948 pv_area stack (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
29d73ae4 949
29d73ae4 950 while (start < limit)
c906108c 951 {
29d73ae4 952 unsigned short insn;
a01567f4 953 gdb::optional<bool> ra_signed_state;
29d73ae4 954
198cd59d 955 insn = read_code_unsigned_integer (start, 2, byte_order_for_code);
9d4fde75 956
94c30b78 957 if ((insn & 0xfe00) == 0xb400) /* push { rlist } */
da59e081 958 {
29d73ae4
DJ
959 int regno;
960 int mask;
4be43953 961
f7b7ed97 962 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953 963 break;
29d73ae4
DJ
964
965 /* Bits 0-7 contain a mask for registers R0-R7. Bit 8 says
966 whether to save LR (R14). */
967 mask = (insn & 0xff) | ((insn & 0x100) << 6);
968
969 /* Calculate offsets of saved R0-R7 and LR. */
970 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
971 if (mask & (1 << regno))
972 {
29d73ae4
DJ
973 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
974 -4);
f7b7ed97 975 stack.store (regs[ARM_SP_REGNUM], 4, regs[regno]);
29d73ae4 976 }
da59e081 977 }
1db01f22 978 else if ((insn & 0xff80) == 0xb080) /* sub sp, #imm */
da59e081 979 {
29d73ae4 980 offset = (insn & 0x7f) << 2; /* get scaled offset */
1db01f22
YQ
981 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
982 -offset);
da59e081 983 }
808f7ab1
YQ
984 else if (thumb_instruction_restores_sp (insn))
985 {
986 /* Don't scan past the epilogue. */
987 break;
988 }
0d39a070
DJ
989 else if ((insn & 0xf800) == 0xa800) /* add Rd, sp, #imm */
990 regs[bits (insn, 8, 10)] = pv_add_constant (regs[ARM_SP_REGNUM],
991 (insn & 0xff) << 2);
992 else if ((insn & 0xfe00) == 0x1c00 /* add Rd, Rn, #imm */
993 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
994 regs[bits (insn, 0, 2)] = pv_add_constant (regs[bits (insn, 3, 5)],
995 bits (insn, 6, 8));
996 else if ((insn & 0xf800) == 0x3000 /* add Rd, #imm */
997 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
998 regs[bits (insn, 8, 10)] = pv_add_constant (regs[bits (insn, 8, 10)],
999 bits (insn, 0, 7));
1000 else if ((insn & 0xfe00) == 0x1800 /* add Rd, Rn, Rm */
1001 && pv_is_register (regs[bits (insn, 6, 8)], ARM_SP_REGNUM)
1002 && pv_is_constant (regs[bits (insn, 3, 5)]))
1003 regs[bits (insn, 0, 2)] = pv_add (regs[bits (insn, 3, 5)],
1004 regs[bits (insn, 6, 8)]);
1005 else if ((insn & 0xff00) == 0x4400 /* add Rd, Rm */
1006 && pv_is_constant (regs[bits (insn, 3, 6)]))
1007 {
1008 int rd = (bit (insn, 7) << 3) + bits (insn, 0, 2);
1009 int rm = bits (insn, 3, 6);
1010 regs[rd] = pv_add (regs[rd], regs[rm]);
1011 }
29d73ae4 1012 else if ((insn & 0xff00) == 0x4600) /* mov hi, lo or mov lo, hi */
da59e081 1013 {
29d73ae4
DJ
1014 int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
1015 int src_reg = (insn & 0x78) >> 3;
1016 regs[dst_reg] = regs[src_reg];
da59e081 1017 }
29d73ae4 1018 else if ((insn & 0xf800) == 0x9000) /* str rd, [sp, #off] */
da59e081 1019 {
29d73ae4
DJ
1020 /* Handle stores to the stack. Normally pushes are used,
1021 but with GCC -mtpcs-frame, there may be other stores
1022 in the prologue to create the frame. */
1023 int regno = (insn >> 8) & 0x7;
1024 pv_t addr;
1025
1026 offset = (insn & 0xff) << 2;
1027 addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
1028
f7b7ed97 1029 if (stack.store_would_trash (addr))
29d73ae4
DJ
1030 break;
1031
f7b7ed97 1032 stack.store (addr, 4, regs[regno]);
da59e081 1033 }
0d39a070
DJ
1034 else if ((insn & 0xf800) == 0x6000) /* str rd, [rn, #off] */
1035 {
1036 int rd = bits (insn, 0, 2);
1037 int rn = bits (insn, 3, 5);
1038 pv_t addr;
1039
1040 offset = bits (insn, 6, 10) << 2;
1041 addr = pv_add_constant (regs[rn], offset);
1042
f7b7ed97 1043 if (stack.store_would_trash (addr))
0d39a070
DJ
1044 break;
1045
f7b7ed97 1046 stack.store (addr, 4, regs[rd]);
0d39a070
DJ
1047 }
1048 else if (((insn & 0xf800) == 0x7000 /* strb Rd, [Rn, #off] */
1049 || (insn & 0xf800) == 0x8000) /* strh Rd, [Rn, #off] */
1050 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
1051 /* Ignore stores of argument registers to the stack. */
1052 ;
1053 else if ((insn & 0xf800) == 0xc800 /* ldmia Rn!, { registers } */
1054 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
1055 /* Ignore block loads from the stack, potentially copying
1056 parameters from memory. */
1057 ;
1058 else if ((insn & 0xf800) == 0x9800 /* ldr Rd, [Rn, #immed] */
1059 || ((insn & 0xf800) == 0x6800 /* ldr Rd, [sp, #immed] */
1060 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM)))
1061 /* Similarly ignore single loads from the stack. */
1062 ;
1063 else if ((insn & 0xffc0) == 0x0000 /* lsls Rd, Rm, #0 */
1064 || (insn & 0xffc0) == 0x1c00) /* add Rd, Rn, #0 */
1065 /* Skip register copies, i.e. saves to another register
1066 instead of the stack. */
1067 ;
1068 else if ((insn & 0xf800) == 0x2000) /* movs Rd, #imm */
1069 /* Recognize constant loads; even with small stacks these are necessary
1070 on Thumb. */
1071 regs[bits (insn, 8, 10)] = pv_constant (bits (insn, 0, 7));
1072 else if ((insn & 0xf800) == 0x4800) /* ldr Rd, [pc, #imm] */
1073 {
1074 /* Constant pool loads, for the same reason. */
1075 unsigned int constant;
1076 CORE_ADDR loc;
1077
1078 loc = start + 4 + bits (insn, 0, 7) * 4;
1079 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1080 regs[bits (insn, 8, 10)] = pv_constant (constant);
1081 }
db24da6d 1082 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instructions. */
0d39a070 1083 {
0d39a070
DJ
1084 unsigned short inst2;
1085
198cd59d
YQ
1086 inst2 = read_code_unsigned_integer (start + 2, 2,
1087 byte_order_for_code);
a01567f4 1088 uint32_t whole_insn = (insn << 16) | inst2;
0d39a070
DJ
1089
1090 if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
1091 {
1092 /* BL, BLX. Allow some special function calls when
1093 skipping the prologue; GCC generates these before
1094 storing arguments to the stack. */
1095 CORE_ADDR nextpc;
1096 int j1, j2, imm1, imm2;
1097
1098 imm1 = sbits (insn, 0, 10);
1099 imm2 = bits (inst2, 0, 10);
1100 j1 = bit (inst2, 13);
1101 j2 = bit (inst2, 11);
1102
1103 offset = ((imm1 << 12) + (imm2 << 1));
1104 offset ^= ((!j2) << 22) | ((!j1) << 23);
1105
1106 nextpc = start + 4 + offset;
1107 /* For BLX make sure to clear the low bits. */
1108 if (bit (inst2, 12) == 0)
1109 nextpc = nextpc & 0xfffffffc;
1110
e0634ccf
UW
1111 if (!skip_prologue_function (gdbarch, nextpc,
1112 bit (inst2, 12) != 0))
0d39a070
DJ
1113 break;
1114 }
ec3d575a 1115
0963b4bd
MS
1116 else if ((insn & 0xffd0) == 0xe900 /* stmdb Rn{!},
1117 { registers } */
ec3d575a
UW
1118 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1119 {
1120 pv_t addr = regs[bits (insn, 0, 3)];
1121 int regno;
1122
f7b7ed97 1123 if (stack.store_would_trash (addr))
ec3d575a
UW
1124 break;
1125
1126 /* Calculate offsets of saved registers. */
1127 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
1128 if (inst2 & (1 << regno))
1129 {
1130 addr = pv_add_constant (addr, -4);
f7b7ed97 1131 stack.store (addr, 4, regs[regno]);
ec3d575a
UW
1132 }
1133
1134 if (insn & 0x0020)
1135 regs[bits (insn, 0, 3)] = addr;
1136 }
1137
fcaa1071
CL
1138 /* vstmdb Rn{!}, { D-registers } (aka vpush). */
1139 else if ((insn & 0xff20) == 0xed20
1140 && (inst2 & 0x0f00) == 0x0b00
1141 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1142 {
1143 /* Address SP points to. */
1144 pv_t addr = regs[bits (insn, 0, 3)];
1145
1146 /* Number of registers saved. */
1147 unsigned int number = bits (inst2, 0, 7) >> 1;
1148
1149 /* First register to save. */
1150 int vd = bits (inst2, 12, 15) | (bits (insn, 6, 6) << 4);
1151
1152 if (stack.store_would_trash (addr))
1153 break;
1154
1155 /* Calculate offsets of saved registers. */
1156 for (; number > 0; number--)
1157 {
1158 addr = pv_add_constant (addr, -8);
1159 stack.store (addr, 8, pv_register (ARM_D0_REGNUM
1160 + vd + number, 0));
1161 }
1162
1163 /* Writeback SP to account for the saved registers. */
1164 regs[bits (insn, 0, 3)] = addr;
1165 }
1166
0963b4bd
MS
1167 else if ((insn & 0xff50) == 0xe940 /* strd Rt, Rt2,
1168 [Rn, #+/-imm]{!} */
ec3d575a
UW
1169 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1170 {
1171 int regno1 = bits (inst2, 12, 15);
1172 int regno2 = bits (inst2, 8, 11);
1173 pv_t addr = regs[bits (insn, 0, 3)];
1174
1175 offset = inst2 & 0xff;
1176 if (insn & 0x0080)
1177 addr = pv_add_constant (addr, offset);
1178 else
1179 addr = pv_add_constant (addr, -offset);
1180
f7b7ed97 1181 if (stack.store_would_trash (addr))
ec3d575a
UW
1182 break;
1183
f7b7ed97
TT
1184 stack.store (addr, 4, regs[regno1]);
1185 stack.store (pv_add_constant (addr, 4),
1186 4, regs[regno2]);
ec3d575a
UW
1187
1188 if (insn & 0x0020)
1189 regs[bits (insn, 0, 3)] = addr;
1190 }
1191
1192 else if ((insn & 0xfff0) == 0xf8c0 /* str Rt,[Rn,+/-#imm]{!} */
1193 && (inst2 & 0x0c00) == 0x0c00
1194 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1195 {
1196 int regno = bits (inst2, 12, 15);
1197 pv_t addr = regs[bits (insn, 0, 3)];
1198
1199 offset = inst2 & 0xff;
1200 if (inst2 & 0x0200)
1201 addr = pv_add_constant (addr, offset);
1202 else
1203 addr = pv_add_constant (addr, -offset);
1204
f7b7ed97 1205 if (stack.store_would_trash (addr))
ec3d575a
UW
1206 break;
1207
f7b7ed97 1208 stack.store (addr, 4, regs[regno]);
ec3d575a
UW
1209
1210 if (inst2 & 0x0100)
1211 regs[bits (insn, 0, 3)] = addr;
1212 }
1213
1214 else if ((insn & 0xfff0) == 0xf8c0 /* str.w Rt,[Rn,#imm] */
1215 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1216 {
1217 int regno = bits (inst2, 12, 15);
1218 pv_t addr;
1219
1220 offset = inst2 & 0xfff;
1221 addr = pv_add_constant (regs[bits (insn, 0, 3)], offset);
1222
f7b7ed97 1223 if (stack.store_would_trash (addr))
ec3d575a
UW
1224 break;
1225
f7b7ed97 1226 stack.store (addr, 4, regs[regno]);
ec3d575a
UW
1227 }
1228
1229 else if ((insn & 0xffd0) == 0xf880 /* str{bh}.w Rt,[Rn,#imm] */
0d39a070 1230 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 1231 /* Ignore stores of argument registers to the stack. */
0d39a070 1232 ;
ec3d575a
UW
1233
1234 else if ((insn & 0xffd0) == 0xf800 /* str{bh} Rt,[Rn,#+/-imm] */
1235 && (inst2 & 0x0d00) == 0x0c00
0d39a070 1236 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 1237 /* Ignore stores of argument registers to the stack. */
0d39a070 1238 ;
ec3d575a 1239
0963b4bd
MS
1240 else if ((insn & 0xffd0) == 0xe890 /* ldmia Rn[!],
1241 { registers } */
ec3d575a
UW
1242 && (inst2 & 0x8000) == 0x0000
1243 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1244 /* Ignore block loads from the stack, potentially copying
1245 parameters from memory. */
0d39a070 1246 ;
ec3d575a 1247
f8c6d152 1248 else if ((insn & 0xff70) == 0xe950 /* ldrd Rt, Rt2,
0963b4bd 1249 [Rn, #+/-imm] */
0d39a070 1250 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 1251 /* Similarly ignore dual loads from the stack. */
0d39a070 1252 ;
ec3d575a
UW
1253
1254 else if ((insn & 0xfff0) == 0xf850 /* ldr Rt,[Rn,#+/-imm] */
1255 && (inst2 & 0x0d00) == 0x0c00
0d39a070 1256 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 1257 /* Similarly ignore single loads from the stack. */
0d39a070 1258 ;
ec3d575a
UW
1259
1260 else if ((insn & 0xfff0) == 0xf8d0 /* ldr.w Rt,[Rn,#imm] */
0d39a070 1261 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 1262 /* Similarly ignore single loads from the stack. */
0d39a070 1263 ;
ec3d575a
UW
1264
1265 else if ((insn & 0xfbf0) == 0xf100 /* add.w Rd, Rn, #imm */
1266 && (inst2 & 0x8000) == 0x0000)
1267 {
1268 unsigned int imm = ((bits (insn, 10, 10) << 11)
1269 | (bits (inst2, 12, 14) << 8)
1270 | bits (inst2, 0, 7));
1271
1272 regs[bits (inst2, 8, 11)]
1273 = pv_add_constant (regs[bits (insn, 0, 3)],
1274 thumb_expand_immediate (imm));
1275 }
1276
1277 else if ((insn & 0xfbf0) == 0xf200 /* addw Rd, Rn, #imm */
1278 && (inst2 & 0x8000) == 0x0000)
0d39a070 1279 {
ec3d575a
UW
1280 unsigned int imm = ((bits (insn, 10, 10) << 11)
1281 | (bits (inst2, 12, 14) << 8)
1282 | bits (inst2, 0, 7));
1283
1284 regs[bits (inst2, 8, 11)]
1285 = pv_add_constant (regs[bits (insn, 0, 3)], imm);
1286 }
1287
1288 else if ((insn & 0xfbf0) == 0xf1a0 /* sub.w Rd, Rn, #imm */
1289 && (inst2 & 0x8000) == 0x0000)
1290 {
1291 unsigned int imm = ((bits (insn, 10, 10) << 11)
1292 | (bits (inst2, 12, 14) << 8)
1293 | bits (inst2, 0, 7));
1294
1295 regs[bits (inst2, 8, 11)]
1296 = pv_add_constant (regs[bits (insn, 0, 3)],
1297 - (CORE_ADDR) thumb_expand_immediate (imm));
1298 }
1299
1300 else if ((insn & 0xfbf0) == 0xf2a0 /* subw Rd, Rn, #imm */
1301 && (inst2 & 0x8000) == 0x0000)
1302 {
1303 unsigned int imm = ((bits (insn, 10, 10) << 11)
1304 | (bits (inst2, 12, 14) << 8)
1305 | bits (inst2, 0, 7));
1306
1307 regs[bits (inst2, 8, 11)]
1308 = pv_add_constant (regs[bits (insn, 0, 3)], - (CORE_ADDR) imm);
1309 }
1310
1311 else if ((insn & 0xfbff) == 0xf04f) /* mov.w Rd, #const */
1312 {
1313 unsigned int imm = ((bits (insn, 10, 10) << 11)
1314 | (bits (inst2, 12, 14) << 8)
1315 | bits (inst2, 0, 7));
1316
1317 regs[bits (inst2, 8, 11)]
1318 = pv_constant (thumb_expand_immediate (imm));
1319 }
1320
1321 else if ((insn & 0xfbf0) == 0xf240) /* movw Rd, #const */
1322 {
621c6d5b
YQ
1323 unsigned int imm
1324 = EXTRACT_MOVW_MOVT_IMM_T (insn, inst2);
ec3d575a
UW
1325
1326 regs[bits (inst2, 8, 11)] = pv_constant (imm);
1327 }
1328
1329 else if (insn == 0xea5f /* mov.w Rd,Rm */
1330 && (inst2 & 0xf0f0) == 0)
1331 {
1332 int dst_reg = (inst2 & 0x0f00) >> 8;
1333 int src_reg = inst2 & 0xf;
1334 regs[dst_reg] = regs[src_reg];
1335 }
1336
1337 else if ((insn & 0xff7f) == 0xf85f) /* ldr.w Rt,<label> */
1338 {
1339 /* Constant pool loads. */
1340 unsigned int constant;
1341 CORE_ADDR loc;
1342
cac395ea 1343 offset = bits (inst2, 0, 11);
ec3d575a
UW
1344 if (insn & 0x0080)
1345 loc = start + 4 + offset;
1346 else
1347 loc = start + 4 - offset;
1348
1349 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1350 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1351 }
1352
1353 else if ((insn & 0xff7f) == 0xe95f) /* ldrd Rt,Rt2,<label> */
1354 {
1355 /* Constant pool loads. */
1356 unsigned int constant;
1357 CORE_ADDR loc;
1358
cac395ea 1359 offset = bits (inst2, 0, 7) << 2;
ec3d575a
UW
1360 if (insn & 0x0080)
1361 loc = start + 4 + offset;
1362 else
1363 loc = start + 4 - offset;
1364
1365 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1366 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1367
1368 constant = read_memory_unsigned_integer (loc + 4, 4, byte_order);
1369 regs[bits (inst2, 8, 11)] = pv_constant (constant);
1370 }
a01567f4
LM
1371 /* Start of ARMv8.1-m PACBTI extension instructions. */
1372 else if (IS_PAC (whole_insn))
1373 {
1374 /* LR and SP are input registers. PAC is in R12. LR is
1375 signed from this point onwards. NOP space. */
1376 ra_signed_state = true;
1377 }
1378 else if (IS_PACBTI (whole_insn))
1379 {
1380 /* LR and SP are input registers. PAC is in R12 and PC is a
1381 valid BTI landing pad. LR is signed from this point onwards.
1382 NOP space. */
1383 ra_signed_state = true;
1384 }
1385 else if (IS_BTI (whole_insn))
1386 {
1387 /* Valid BTI landing pad. NOP space. */
1388 }
1389 else if (IS_PACG (whole_insn))
1390 {
1391 /* Sign Rn using Rm and store the PAC in Rd. Rd is signed from
1392 this point onwards. */
1393 ra_signed_state = true;
1394 }
1395 else if (IS_AUT (whole_insn) || IS_AUTG (whole_insn))
1396 {
1397 /* These instructions appear close to the epilogue, when signed
1398 pointers are getting authenticated. */
1399 ra_signed_state = false;
1400 }
1401 /* End of ARMv8.1-m PACBTI extension instructions */
ec3d575a
UW
1402 else if (thumb2_instruction_changes_pc (insn, inst2))
1403 {
1404 /* Don't scan past anything that might change control flow. */
0d39a070
DJ
1405 break;
1406 }
ec3d575a
UW
1407 else
1408 {
1409 /* The optimizer might shove anything into the prologue,
1410 so we just skip what we don't recognize. */
1411 unrecognized_pc = start;
1412 }
0d39a070 1413
a01567f4
LM
1414 /* Make sure we are dealing with a target that supports ARMv8.1-m
1415 PACBTI. */
1416 if (cache != nullptr && tdep->have_pacbti
1417 && ra_signed_state.has_value ())
1418 {
1419 arm_debug_printf ("Found pacbti instruction at %s",
1420 paddress (gdbarch, start));
1421 arm_debug_printf ("RA is %s",
1422 *ra_signed_state? "signed" : "not signed");
1423 cache->ra_signed_state = ra_signed_state;
1424 }
1425
0d39a070
DJ
1426 start += 2;
1427 }
ec3d575a 1428 else if (thumb_instruction_changes_pc (insn))
3d74b771 1429 {
ec3d575a 1430 /* Don't scan past anything that might change control flow. */
da3c6d4a 1431 break;
3d74b771 1432 }
ec3d575a
UW
1433 else
1434 {
1435 /* The optimizer might shove anything into the prologue,
1436 so we just skip what we don't recognize. */
1437 unrecognized_pc = start;
1438 }
29d73ae4
DJ
1439
1440 start += 2;
c906108c
SS
1441 }
1442
7cb6d92a
SM
1443 arm_debug_printf ("Prologue scan stopped at %s",
1444 paddress (gdbarch, start));
0d39a070 1445
ec3d575a
UW
1446 if (unrecognized_pc == 0)
1447 unrecognized_pc = start;
1448
29d73ae4 1449 if (cache == NULL)
f7b7ed97 1450 return unrecognized_pc;
29d73ae4 1451
29d73ae4
DJ
1452 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1453 {
1454 /* Frame pointer is fp. Frame size is constant. */
1455 cache->framereg = ARM_FP_REGNUM;
1456 cache->framesize = -regs[ARM_FP_REGNUM].k;
1457 }
1458 else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
1459 {
1460 /* Frame pointer is r7. Frame size is constant. */
1461 cache->framereg = THUMB_FP_REGNUM;
1462 cache->framesize = -regs[THUMB_FP_REGNUM].k;
1463 }
72a2e3dc 1464 else
29d73ae4
DJ
1465 {
1466 /* Try the stack pointer... this is a bit desperate. */
1467 cache->framereg = ARM_SP_REGNUM;
1468 cache->framesize = -regs[ARM_SP_REGNUM].k;
1469 }
29d73ae4 1470
de76473c 1471 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
f7b7ed97 1472 if (stack.find_reg (gdbarch, i, &offset))
10245fe8
YR
1473 {
1474 cache->saved_regs[i].set_addr (offset);
1475 if (i == ARM_SP_REGNUM)
1476 arm_cache_set_active_sp_value(cache, tdep, offset);
1477 }
29d73ae4 1478
ec3d575a 1479 return unrecognized_pc;
c906108c
SS
1480}
1481
621c6d5b
YQ
1482
1483/* Try to analyze the instructions starting from PC, which load symbol
1484 __stack_chk_guard. Return the address of instruction after loading this
1485 symbol, set the dest register number to *BASEREG, and set the size of
1486 instructions for loading symbol in OFFSET. Return 0 if instructions are
1487 not recognized. */
1488
1489static CORE_ADDR
1490arm_analyze_load_stack_chk_guard(CORE_ADDR pc, struct gdbarch *gdbarch,
1491 unsigned int *destreg, int *offset)
1492{
1493 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1494 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1495 unsigned int low, high, address;
1496
1497 address = 0;
1498 if (is_thumb)
1499 {
1500 unsigned short insn1
198cd59d 1501 = read_code_unsigned_integer (pc, 2, byte_order_for_code);
621c6d5b
YQ
1502
1503 if ((insn1 & 0xf800) == 0x4800) /* ldr Rd, #immed */
1504 {
1505 *destreg = bits (insn1, 8, 10);
1506 *offset = 2;
6ae274b7
YQ
1507 address = (pc & 0xfffffffc) + 4 + (bits (insn1, 0, 7) << 2);
1508 address = read_memory_unsigned_integer (address, 4,
1509 byte_order_for_code);
621c6d5b
YQ
1510 }
1511 else if ((insn1 & 0xfbf0) == 0xf240) /* movw Rd, #const */
1512 {
1513 unsigned short insn2
198cd59d 1514 = read_code_unsigned_integer (pc + 2, 2, byte_order_for_code);
621c6d5b
YQ
1515
1516 low = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1517
1518 insn1
198cd59d 1519 = read_code_unsigned_integer (pc + 4, 2, byte_order_for_code);
621c6d5b 1520 insn2
198cd59d 1521 = read_code_unsigned_integer (pc + 6, 2, byte_order_for_code);
621c6d5b
YQ
1522
1523 /* movt Rd, #const */
1524 if ((insn1 & 0xfbc0) == 0xf2c0)
1525 {
1526 high = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1527 *destreg = bits (insn2, 8, 11);
1528 *offset = 8;
1529 address = (high << 16 | low);
1530 }
1531 }
1532 }
1533 else
1534 {
2e9e421f 1535 unsigned int insn
198cd59d 1536 = read_code_unsigned_integer (pc, 4, byte_order_for_code);
2e9e421f 1537
6ae274b7 1538 if ((insn & 0x0e5f0000) == 0x041f0000) /* ldr Rd, [PC, #immed] */
2e9e421f 1539 {
6ae274b7
YQ
1540 address = bits (insn, 0, 11) + pc + 8;
1541 address = read_memory_unsigned_integer (address, 4,
1542 byte_order_for_code);
1543
2e9e421f
UW
1544 *destreg = bits (insn, 12, 15);
1545 *offset = 4;
1546 }
1547 else if ((insn & 0x0ff00000) == 0x03000000) /* movw Rd, #const */
1548 {
1549 low = EXTRACT_MOVW_MOVT_IMM_A (insn);
1550
1551 insn
198cd59d 1552 = read_code_unsigned_integer (pc + 4, 4, byte_order_for_code);
2e9e421f
UW
1553
1554 if ((insn & 0x0ff00000) == 0x03400000) /* movt Rd, #const */
1555 {
1556 high = EXTRACT_MOVW_MOVT_IMM_A (insn);
1557 *destreg = bits (insn, 12, 15);
1558 *offset = 8;
1559 address = (high << 16 | low);
1560 }
1561 }
621c6d5b
YQ
1562 }
1563
1564 return address;
1565}
1566
1567/* Try to skip a sequence of instructions used for stack protector. If PC
0963b4bd
MS
1568 points to the first instruction of this sequence, return the address of
1569 first instruction after this sequence, otherwise, return original PC.
621c6d5b
YQ
1570
1571 On arm, this sequence of instructions is composed of mainly three steps,
1572 Step 1: load symbol __stack_chk_guard,
1573 Step 2: load from address of __stack_chk_guard,
1574 Step 3: store it to somewhere else.
1575
1576 Usually, instructions on step 2 and step 3 are the same on various ARM
1577 architectures. On step 2, it is one instruction 'ldr Rx, [Rn, #0]', and
1578 on step 3, it is also one instruction 'str Rx, [r7, #immd]'. However,
1579 instructions in step 1 vary from different ARM architectures. On ARMv7,
1580 they are,
1581
1582 movw Rn, #:lower16:__stack_chk_guard
1583 movt Rn, #:upper16:__stack_chk_guard
1584
1585 On ARMv5t, it is,
1586
1587 ldr Rn, .Label
1588 ....
1589 .Lable:
1590 .word __stack_chk_guard
1591
1592 Since ldr/str is a very popular instruction, we can't use them as
1593 'fingerprint' or 'signature' of stack protector sequence. Here we choose
1594 sequence {movw/movt, ldr}/ldr/str plus symbol __stack_chk_guard, if not
1595 stripped, as the 'fingerprint' of a stack protector cdoe sequence. */
1596
1597static CORE_ADDR
1598arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
1599{
1600 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
22e048c9 1601 unsigned int basereg;
7cbd4a93 1602 struct bound_minimal_symbol stack_chk_guard;
621c6d5b
YQ
1603 int offset;
1604 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1605 CORE_ADDR addr;
1606
1607 /* Try to parse the instructions in Step 1. */
1608 addr = arm_analyze_load_stack_chk_guard (pc, gdbarch,
1609 &basereg, &offset);
1610 if (!addr)
1611 return pc;
1612
1613 stack_chk_guard = lookup_minimal_symbol_by_pc (addr);
6041179a
JB
1614 /* ADDR must correspond to a symbol whose name is __stack_chk_guard.
1615 Otherwise, this sequence cannot be for stack protector. */
1616 if (stack_chk_guard.minsym == NULL
c9d95fa3 1617 || !startswith (stack_chk_guard.minsym->linkage_name (), "__stack_chk_guard"))
621c6d5b
YQ
1618 return pc;
1619
1620 if (is_thumb)
1621 {
1622 unsigned int destreg;
1623 unsigned short insn
198cd59d 1624 = read_code_unsigned_integer (pc + offset, 2, byte_order_for_code);
621c6d5b
YQ
1625
1626 /* Step 2: ldr Rd, [Rn, #immed], encoding T1. */
1627 if ((insn & 0xf800) != 0x6800)
1628 return pc;
1629 if (bits (insn, 3, 5) != basereg)
1630 return pc;
1631 destreg = bits (insn, 0, 2);
1632
198cd59d
YQ
1633 insn = read_code_unsigned_integer (pc + offset + 2, 2,
1634 byte_order_for_code);
621c6d5b
YQ
1635 /* Step 3: str Rd, [Rn, #immed], encoding T1. */
1636 if ((insn & 0xf800) != 0x6000)
1637 return pc;
1638 if (destreg != bits (insn, 0, 2))
1639 return pc;
1640 }
1641 else
1642 {
1643 unsigned int destreg;
1644 unsigned int insn
198cd59d 1645 = read_code_unsigned_integer (pc + offset, 4, byte_order_for_code);
621c6d5b
YQ
1646
1647 /* Step 2: ldr Rd, [Rn, #immed], encoding A1. */
1648 if ((insn & 0x0e500000) != 0x04100000)
1649 return pc;
1650 if (bits (insn, 16, 19) != basereg)
1651 return pc;
1652 destreg = bits (insn, 12, 15);
1653 /* Step 3: str Rd, [Rn, #immed], encoding A1. */
198cd59d 1654 insn = read_code_unsigned_integer (pc + offset + 4,
621c6d5b
YQ
1655 4, byte_order_for_code);
1656 if ((insn & 0x0e500000) != 0x04000000)
1657 return pc;
1658 if (bits (insn, 12, 15) != destreg)
1659 return pc;
1660 }
1661 /* The size of total two instructions ldr/str is 4 on Thumb-2, while 8
1662 on arm. */
1663 if (is_thumb)
1664 return pc + offset + 4;
1665 else
1666 return pc + offset + 8;
1667}
1668
da3c6d4a
MS
1669/* Advance the PC across any function entry prologue instructions to
1670 reach some "real" code.
34e8f22d
RE
1671
1672 The APCS (ARM Procedure Call Standard) defines the following
ed9a39eb 1673 prologue:
c906108c 1674
c5aa993b
JM
1675 mov ip, sp
1676 [stmfd sp!, {a1,a2,a3,a4}]
1677 stmfd sp!, {...,fp,ip,lr,pc}
ed9a39eb
JM
1678 [stfe f7, [sp, #-12]!]
1679 [stfe f6, [sp, #-12]!]
1680 [stfe f5, [sp, #-12]!]
1681 [stfe f4, [sp, #-12]!]
0963b4bd 1682 sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn. */
c906108c 1683
34e8f22d 1684static CORE_ADDR
6093d2eb 1685arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
c906108c 1686{
a89fea3c 1687 CORE_ADDR func_addr, limit_pc;
c906108c 1688
a89fea3c
JL
1689 /* See if we can determine the end of the prologue via the symbol table.
1690 If so, then return either PC, or the PC after the prologue, whichever
1691 is greater. */
1692 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
c906108c 1693 {
d80b854b
UW
1694 CORE_ADDR post_prologue_pc
1695 = skip_prologue_using_sal (gdbarch, func_addr);
43f3e411 1696 struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);
0d39a070 1697
621c6d5b
YQ
1698 if (post_prologue_pc)
1699 post_prologue_pc
1700 = arm_skip_stack_protector (post_prologue_pc, gdbarch);
1701
1702
0d39a070
DJ
1703 /* GCC always emits a line note before the prologue and another
1704 one after, even if the two are at the same address or on the
1705 same line. Take advantage of this so that we do not need to
1706 know every instruction that might appear in the prologue. We
1707 will have producer information for most binaries; if it is
1708 missing (e.g. for -gstabs), assuming the GNU tools. */
1709 if (post_prologue_pc
43f3e411 1710 && (cust == NULL
ab5f850e
SM
1711 || cust->producer () == NULL
1712 || startswith (cust->producer (), "GNU ")
1713 || producer_is_llvm (cust->producer ())))
0d39a070
DJ
1714 return post_prologue_pc;
1715
a89fea3c 1716 if (post_prologue_pc != 0)
0d39a070
DJ
1717 {
1718 CORE_ADDR analyzed_limit;
1719
1720 /* For non-GCC compilers, make sure the entire line is an
1721 acceptable prologue; GDB will round this function's
1722 return value up to the end of the following line so we
1723 can not skip just part of a line (and we do not want to).
1724
1725 RealView does not treat the prologue specially, but does
1726 associate prologue code with the opening brace; so this
1727 lets us skip the first line if we think it is the opening
1728 brace. */
9779414d 1729 if (arm_pc_is_thumb (gdbarch, func_addr))
0d39a070
DJ
1730 analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
1731 post_prologue_pc, NULL);
1732 else
9ecab40c
SM
1733 analyzed_limit
1734 = arm_analyze_prologue (gdbarch, func_addr, post_prologue_pc,
1735 NULL, target_arm_instruction_reader ());
0d39a070
DJ
1736
1737 if (analyzed_limit != post_prologue_pc)
1738 return func_addr;
1739
1740 return post_prologue_pc;
1741 }
c906108c
SS
1742 }
1743
a89fea3c
JL
1744 /* Can't determine prologue from the symbol table, need to examine
1745 instructions. */
c906108c 1746
a89fea3c
JL
1747 /* Find an upper limit on the function prologue using the debug
1748 information. If the debug information could not be used to provide
1749 that bound, then use an arbitrary large number as the upper bound. */
0963b4bd 1750 /* Like arm_scan_prologue, stop no later than pc + 64. */
d80b854b 1751 limit_pc = skip_prologue_using_sal (gdbarch, pc);
a89fea3c
JL
1752 if (limit_pc == 0)
1753 limit_pc = pc + 64; /* Magic. */
1754
c906108c 1755
29d73ae4 1756 /* Check if this is Thumb code. */
9779414d 1757 if (arm_pc_is_thumb (gdbarch, pc))
a89fea3c 1758 return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
21daaaaf 1759 else
9ecab40c
SM
1760 return arm_analyze_prologue (gdbarch, pc, limit_pc, NULL,
1761 target_arm_instruction_reader ());
c906108c 1762}
94c30b78 1763
c5aa993b 1764/* *INDENT-OFF* */
c906108c
SS
1765/* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
1766 This function decodes a Thumb function prologue to determine:
1767 1) the size of the stack frame
1768 2) which registers are saved on it
1769 3) the offsets of saved regs
1770 4) the offset from the stack pointer to the frame pointer
c906108c 1771
da59e081
JM
1772 A typical Thumb function prologue would create this stack frame
1773 (offsets relative to FP)
c906108c
SS
1774 old SP -> 24 stack parameters
1775 20 LR
1776 16 R7
1777 R7 -> 0 local variables (16 bytes)
1778 SP -> -12 additional stack space (12 bytes)
1779 The frame size would thus be 36 bytes, and the frame offset would be
0963b4bd 1780 12 bytes. The frame register is R7.
da59e081 1781
da3c6d4a
MS
1782 The comments for thumb_skip_prolog() describe the algorithm we use
1783 to detect the end of the prolog. */
c5aa993b
JM
1784/* *INDENT-ON* */
1785
c906108c 1786static void
be8626e0 1787thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
b39cc962 1788 CORE_ADDR block_addr, struct arm_prologue_cache *cache)
c906108c
SS
1789{
1790 CORE_ADDR prologue_start;
1791 CORE_ADDR prologue_end;
c906108c 1792
b39cc962
DJ
1793 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1794 &prologue_end))
c906108c 1795 {
ec3d575a
UW
1796 /* See comment in arm_scan_prologue for an explanation of
1797 this heuristics. */
1798 if (prologue_end > prologue_start + 64)
1799 {
1800 prologue_end = prologue_start + 64;
1801 }
c906108c
SS
1802 }
1803 else
f7060f85
DJ
1804 /* We're in the boondocks: we have no idea where the start of the
1805 function is. */
1806 return;
c906108c 1807
325fac50 1808 prologue_end = std::min (prologue_end, prev_pc);
c906108c 1809
be8626e0 1810 thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
1811}
1812
f303bc3e
YQ
1813/* Return 1 if the ARM instruction INSN restores SP in epilogue, 0
1814 otherwise. */
1815
1816static int
1817arm_instruction_restores_sp (unsigned int insn)
1818{
1819 if (bits (insn, 28, 31) != INST_NV)
1820 {
1821 if ((insn & 0x0df0f000) == 0x0080d000
1822 /* ADD SP (register or immediate). */
1823 || (insn & 0x0df0f000) == 0x0040d000
1824 /* SUB SP (register or immediate). */
1825 || (insn & 0x0ffffff0) == 0x01a0d000
1826 /* MOV SP. */
1827 || (insn & 0x0fff0000) == 0x08bd0000
1828 /* POP (LDMIA). */
1829 || (insn & 0x0fff0000) == 0x049d0000)
1830 /* POP of a single register. */
1831 return 1;
1832 }
1833
1834 return 0;
1835}
1836
9ecab40c
SM
1837/* Implement immediate value decoding, as described in section A5.2.4
1838 (Modified immediate constants in ARM instructions) of the ARM Architecture
1839 Reference Manual (ARMv7-A and ARMv7-R edition). */
1840
1841static uint32_t
1842arm_expand_immediate (uint32_t imm)
1843{
1844 /* Immediate values are 12 bits long. */
1845 gdb_assert ((imm & 0xfffff000) == 0);
1846
1847 uint32_t unrotated_value = imm & 0xff;
1848 uint32_t rotate_amount = (imm & 0xf00) >> 7;
1849
1850 if (rotate_amount == 0)
1851 return unrotated_value;
1852
1853 return ((unrotated_value >> rotate_amount)
1854 | (unrotated_value << (32 - rotate_amount)));
1855}
1856
0d39a070
DJ
1857/* Analyze an ARM mode prologue starting at PROLOGUE_START and
1858 continuing no further than PROLOGUE_END. If CACHE is non-NULL,
1859 fill it in. Return the first address not recognized as a prologue
1860 instruction.
eb5492fa 1861
0d39a070
DJ
1862 We recognize all the instructions typically found in ARM prologues,
1863 plus harmless instructions which can be skipped (either for analysis
1864 purposes, or a more restrictive set that can be skipped when finding
1865 the end of the prologue). */
1866
1867static CORE_ADDR
1868arm_analyze_prologue (struct gdbarch *gdbarch,
1869 CORE_ADDR prologue_start, CORE_ADDR prologue_end,
9ecab40c
SM
1870 struct arm_prologue_cache *cache,
1871 const arm_instruction_reader &insn_reader)
0d39a070 1872{
0d39a070
DJ
1873 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1874 int regno;
1875 CORE_ADDR offset, current_pc;
1876 pv_t regs[ARM_FPS_REGNUM];
0d39a070 1877 CORE_ADDR unrecognized_pc = 0;
345bd07c 1878 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
0d39a070
DJ
1879
1880 /* Search the prologue looking for instructions that set up the
96baa820 1881 frame pointer, adjust the stack pointer, and save registers.
ed9a39eb 1882
96baa820
JM
1883 Be careful, however, and if it doesn't look like a prologue,
1884 don't try to scan it. If, for instance, a frameless function
1885 begins with stmfd sp!, then we will tell ourselves there is
b8d5e71d 1886 a frame, which will confuse stack traceback, as well as "finish"
96baa820 1887 and other operations that rely on a knowledge of the stack
0d39a070 1888 traceback. */
d4473757 1889
4be43953
DJ
1890 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1891 regs[regno] = pv_register (regno, 0);
f7b7ed97 1892 pv_area stack (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
4be43953 1893
94c30b78
MS
1894 for (current_pc = prologue_start;
1895 current_pc < prologue_end;
f43845b3 1896 current_pc += 4)
96baa820 1897 {
9ecab40c 1898 uint32_t insn = insn_reader.read (current_pc, byte_order_for_code);
9d4fde75 1899
94c30b78 1900 if (insn == 0xe1a0c00d) /* mov ip, sp */
f43845b3 1901 {
4be43953 1902 regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
28cd8767
JG
1903 continue;
1904 }
0d39a070
DJ
1905 else if ((insn & 0xfff00000) == 0xe2800000 /* add Rd, Rn, #n */
1906 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767 1907 {
9ecab40c 1908 uint32_t imm = arm_expand_immediate (insn & 0xfff);
0d39a070 1909 int rd = bits (insn, 12, 15);
0d39a070 1910 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
28cd8767
JG
1911 continue;
1912 }
0d39a070
DJ
1913 else if ((insn & 0xfff00000) == 0xe2400000 /* sub Rd, Rn, #n */
1914 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767 1915 {
9ecab40c 1916 uint32_t imm = arm_expand_immediate (insn & 0xfff);
0d39a070 1917 int rd = bits (insn, 12, 15);
0d39a070 1918 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
f43845b3
MS
1919 continue;
1920 }
0963b4bd
MS
1921 else if ((insn & 0xffff0fff) == 0xe52d0004) /* str Rd,
1922 [sp, #-4]! */
f43845b3 1923 {
f7b7ed97 1924 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
1925 break;
1926 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
f7b7ed97
TT
1927 stack.store (regs[ARM_SP_REGNUM], 4,
1928 regs[bits (insn, 12, 15)]);
f43845b3
MS
1929 continue;
1930 }
1931 else if ((insn & 0xffff0000) == 0xe92d0000)
d4473757
KB
1932 /* stmfd sp!, {..., fp, ip, lr, pc}
1933 or
1934 stmfd sp!, {a1, a2, a3, a4} */
c906108c 1935 {
d4473757 1936 int mask = insn & 0xffff;
ed9a39eb 1937
f7b7ed97 1938 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
1939 break;
1940
94c30b78 1941 /* Calculate offsets of saved registers. */
34e8f22d 1942 for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
d4473757
KB
1943 if (mask & (1 << regno))
1944 {
0963b4bd
MS
1945 regs[ARM_SP_REGNUM]
1946 = pv_add_constant (regs[ARM_SP_REGNUM], -4);
f7b7ed97 1947 stack.store (regs[ARM_SP_REGNUM], 4, regs[regno]);
d4473757
KB
1948 }
1949 }
0d39a070
DJ
1950 else if ((insn & 0xffff0000) == 0xe54b0000 /* strb rx,[r11,#-n] */
1951 || (insn & 0xffff00f0) == 0xe14b00b0 /* strh rx,[r11,#-n] */
f8bf5763 1952 || (insn & 0xffffc000) == 0xe50b0000) /* str rx,[r11,#-n] */
b8d5e71d
MS
1953 {
1954 /* No need to add this to saved_regs -- it's just an arg reg. */
1955 continue;
1956 }
0d39a070
DJ
1957 else if ((insn & 0xffff0000) == 0xe5cd0000 /* strb rx,[sp,#n] */
1958 || (insn & 0xffff00f0) == 0xe1cd00b0 /* strh rx,[sp,#n] */
f8bf5763 1959 || (insn & 0xffffc000) == 0xe58d0000) /* str rx,[sp,#n] */
f43845b3
MS
1960 {
1961 /* No need to add this to saved_regs -- it's just an arg reg. */
1962 continue;
1963 }
0963b4bd
MS
1964 else if ((insn & 0xfff00000) == 0xe8800000 /* stm Rn,
1965 { registers } */
0d39a070
DJ
1966 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1967 {
1968 /* No need to add this to saved_regs -- it's just arg regs. */
1969 continue;
1970 }
d4473757
KB
1971 else if ((insn & 0xfffff000) == 0xe24cb000) /* sub fp, ip #n */
1972 {
9ecab40c 1973 uint32_t imm = arm_expand_immediate (insn & 0xfff);
4be43953 1974 regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
d4473757
KB
1975 }
1976 else if ((insn & 0xfffff000) == 0xe24dd000) /* sub sp, sp #n */
1977 {
9ecab40c 1978 uint32_t imm = arm_expand_immediate(insn & 0xfff);
4be43953 1979 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
d4473757 1980 }
0963b4bd
MS
1981 else if ((insn & 0xffff7fff) == 0xed6d0103 /* stfe f?,
1982 [sp, -#c]! */
345bd07c 1983 && tdep->have_fpa_registers)
d4473757 1984 {
f7b7ed97 1985 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
1986 break;
1987
1988 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
34e8f22d 1989 regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
f7b7ed97 1990 stack.store (regs[ARM_SP_REGNUM], 12, regs[regno]);
d4473757 1991 }
0963b4bd
MS
1992 else if ((insn & 0xffbf0fff) == 0xec2d0200 /* sfmfd f0, 4,
1993 [sp!] */
345bd07c 1994 && tdep->have_fpa_registers)
d4473757
KB
1995 {
1996 int n_saved_fp_regs;
1997 unsigned int fp_start_reg, fp_bound_reg;
1998
f7b7ed97 1999 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
2000 break;
2001
94c30b78 2002 if ((insn & 0x800) == 0x800) /* N0 is set */
96baa820 2003 {
d4473757
KB
2004 if ((insn & 0x40000) == 0x40000) /* N1 is set */
2005 n_saved_fp_regs = 3;
2006 else
2007 n_saved_fp_regs = 1;
96baa820 2008 }
d4473757 2009 else
96baa820 2010 {
d4473757
KB
2011 if ((insn & 0x40000) == 0x40000) /* N1 is set */
2012 n_saved_fp_regs = 2;
2013 else
2014 n_saved_fp_regs = 4;
96baa820 2015 }
d4473757 2016
34e8f22d 2017 fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
d4473757
KB
2018 fp_bound_reg = fp_start_reg + n_saved_fp_regs;
2019 for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
96baa820 2020 {
4be43953 2021 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
f7b7ed97
TT
2022 stack.store (regs[ARM_SP_REGNUM], 12,
2023 regs[fp_start_reg++]);
96baa820 2024 }
c906108c 2025 }
0d39a070
DJ
2026 else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
2027 {
2028 /* Allow some special function calls when skipping the
2029 prologue; GCC generates these before storing arguments to
2030 the stack. */
2031 CORE_ADDR dest = BranchDest (current_pc, insn);
2032
e0634ccf 2033 if (skip_prologue_function (gdbarch, dest, 0))
0d39a070
DJ
2034 continue;
2035 else
2036 break;
2037 }
d4473757 2038 else if ((insn & 0xf0000000) != 0xe0000000)
0963b4bd 2039 break; /* Condition not true, exit early. */
0d39a070
DJ
2040 else if (arm_instruction_changes_pc (insn))
2041 /* Don't scan past anything that might change control flow. */
2042 break;
f303bc3e
YQ
2043 else if (arm_instruction_restores_sp (insn))
2044 {
2045 /* Don't scan past the epilogue. */
2046 break;
2047 }
d19f7eee
UW
2048 else if ((insn & 0xfe500000) == 0xe8100000 /* ldm */
2049 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
2050 /* Ignore block loads from the stack, potentially copying
2051 parameters from memory. */
2052 continue;
2053 else if ((insn & 0xfc500000) == 0xe4100000
2054 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
2055 /* Similarly ignore single loads from the stack. */
2056 continue;
0d39a070
DJ
2057 else if ((insn & 0xffff0ff0) == 0xe1a00000)
2058 /* MOV Rd, Rm. Skip register copies, i.e. saves to another
2059 register instead of the stack. */
d4473757 2060 continue;
0d39a070
DJ
2061 else
2062 {
21daaaaf
YQ
2063 /* The optimizer might shove anything into the prologue, if
2064 we build up cache (cache != NULL) from scanning prologue,
2065 we just skip what we don't recognize and scan further to
2066 make cache as complete as possible. However, if we skip
2067 prologue, we'll stop immediately on unrecognized
2068 instruction. */
0d39a070 2069 unrecognized_pc = current_pc;
21daaaaf
YQ
2070 if (cache != NULL)
2071 continue;
2072 else
2073 break;
0d39a070 2074 }
c906108c
SS
2075 }
2076
0d39a070
DJ
2077 if (unrecognized_pc == 0)
2078 unrecognized_pc = current_pc;
2079
0d39a070
DJ
2080 if (cache)
2081 {
4072f920
YQ
2082 int framereg, framesize;
2083
2084 /* The frame size is just the distance from the frame register
2085 to the original stack pointer. */
2086 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
2087 {
2088 /* Frame pointer is fp. */
2089 framereg = ARM_FP_REGNUM;
2090 framesize = -regs[ARM_FP_REGNUM].k;
2091 }
2092 else
2093 {
2094 /* Try the stack pointer... this is a bit desperate. */
2095 framereg = ARM_SP_REGNUM;
2096 framesize = -regs[ARM_SP_REGNUM].k;
2097 }
2098
0d39a070
DJ
2099 cache->framereg = framereg;
2100 cache->framesize = framesize;
2101
2102 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
f7b7ed97 2103 if (stack.find_reg (gdbarch, regno, &offset))
10245fe8
YR
2104 {
2105 cache->saved_regs[regno].set_addr (offset);
2106 if (regno == ARM_SP_REGNUM)
2107 arm_cache_set_active_sp_value(cache, tdep, offset);
2108 }
0d39a070
DJ
2109 }
2110
7cb6d92a
SM
2111 arm_debug_printf ("Prologue scan stopped at %s",
2112 paddress (gdbarch, unrecognized_pc));
4be43953 2113
0d39a070
DJ
2114 return unrecognized_pc;
2115}
2116
2117static void
2118arm_scan_prologue (struct frame_info *this_frame,
2119 struct arm_prologue_cache *cache)
2120{
2121 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2122 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
bec2ab5a 2123 CORE_ADDR prologue_start, prologue_end;
0d39a070
DJ
2124 CORE_ADDR prev_pc = get_frame_pc (this_frame);
2125 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
345bd07c 2126 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
0d39a070
DJ
2127
2128 /* Assume there is no frame until proven otherwise. */
2129 cache->framereg = ARM_SP_REGNUM;
2130 cache->framesize = 0;
2131
2132 /* Check for Thumb prologue. */
2133 if (arm_frame_is_thumb (this_frame))
2134 {
2135 thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
2136 return;
2137 }
2138
2139 /* Find the function prologue. If we can't find the function in
2140 the symbol table, peek in the stack frame to find the PC. */
2141 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
2142 &prologue_end))
2143 {
2144 /* One way to find the end of the prologue (which works well
dda83cd7 2145 for unoptimized code) is to do the following:
0d39a070
DJ
2146
2147 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
2148
2149 if (sal.line == 0)
2150 prologue_end = prev_pc;
2151 else if (sal.end < prologue_end)
2152 prologue_end = sal.end;
2153
2154 This mechanism is very accurate so long as the optimizer
2155 doesn't move any instructions from the function body into the
2156 prologue. If this happens, sal.end will be the last
2157 instruction in the first hunk of prologue code just before
2158 the first instruction that the scheduler has moved from
2159 the body to the prologue.
2160
2161 In order to make sure that we scan all of the prologue
2162 instructions, we use a slightly less accurate mechanism which
2163 may scan more than necessary. To help compensate for this
2164 lack of accuracy, the prologue scanning loop below contains
2165 several clauses which'll cause the loop to terminate early if
2166 an implausible prologue instruction is encountered.
2167
2168 The expression
2169
2170 prologue_start + 64
2171
2172 is a suitable endpoint since it accounts for the largest
2173 possible prologue plus up to five instructions inserted by
2174 the scheduler. */
2175
2176 if (prologue_end > prologue_start + 64)
2177 {
2178 prologue_end = prologue_start + 64; /* See above. */
2179 }
2180 }
2181 else
2182 {
2183 /* We have no symbol information. Our only option is to assume this
2184 function has a standard stack frame and the normal frame register.
2185 Then, we can find the value of our frame pointer on entrance to
2186 the callee (or at the present moment if this is the innermost frame).
2187 The value stored there should be the address of the stmfd + 8. */
2188 CORE_ADDR frame_loc;
7913a64c 2189 ULONGEST return_value;
0d39a070 2190
9e237747 2191 /* AAPCS does not use a frame register, so we can abort here. */
345bd07c 2192 if (tdep->arm_abi == ARM_ABI_AAPCS)
dda83cd7 2193 return;
9e237747 2194
0d39a070 2195 frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
7913a64c
YQ
2196 if (!safe_read_memory_unsigned_integer (frame_loc, 4, byte_order,
2197 &return_value))
dda83cd7 2198 return;
0d39a070 2199 else
dda83cd7
SM
2200 {
2201 prologue_start = gdbarch_addr_bits_remove
0d39a070 2202 (gdbarch, return_value) - 8;
dda83cd7
SM
2203 prologue_end = prologue_start + 64; /* See above. */
2204 }
0d39a070
DJ
2205 }
2206
2207 if (prev_pc < prologue_end)
2208 prologue_end = prev_pc;
2209
9ecab40c
SM
2210 arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache,
2211 target_arm_instruction_reader ());
c906108c
SS
2212}
2213
eb5492fa 2214static struct arm_prologue_cache *
a262aec2 2215arm_make_prologue_cache (struct frame_info *this_frame)
c906108c 2216{
eb5492fa
DJ
2217 int reg;
2218 struct arm_prologue_cache *cache;
1ef3351b 2219 CORE_ADDR unwound_fp, prev_sp;
c5aa993b 2220
35d5d4ee 2221 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 2222 arm_cache_init (cache, this_frame);
c906108c 2223
a262aec2 2224 arm_scan_prologue (this_frame, cache);
848cfffb 2225
a262aec2 2226 unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
eb5492fa
DJ
2227 if (unwound_fp == 0)
2228 return cache;
c906108c 2229
ae7e2f45
CL
2230 arm_gdbarch_tdep *tdep =
2231 (arm_gdbarch_tdep *) gdbarch_tdep (get_frame_arch (this_frame));
2232
1ef3351b
YR
2233 prev_sp = unwound_fp + cache->framesize;
2234 arm_cache_set_active_sp_value (cache, tdep, prev_sp);
c906108c 2235
eb5492fa
DJ
2236 /* Calculate actual addresses of saved registers using offsets
2237 determined by arm_scan_prologue. */
a262aec2 2238 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
a9a87d35 2239 if (cache->saved_regs[reg].is_addr ())
1ef3351b
YR
2240 cache->saved_regs[reg].set_addr (cache->saved_regs[reg].addr () +
2241 prev_sp);
eb5492fa
DJ
2242
2243 return cache;
c906108c
SS
2244}
2245
c1ee9414
LM
2246/* Implementation of the stop_reason hook for arm_prologue frames. */
2247
2248static enum unwind_stop_reason
2249arm_prologue_unwind_stop_reason (struct frame_info *this_frame,
2250 void **this_cache)
2251{
2252 struct arm_prologue_cache *cache;
2253 CORE_ADDR pc;
2254
2255 if (*this_cache == NULL)
2256 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 2257 cache = (struct arm_prologue_cache *) *this_cache;
c1ee9414
LM
2258
2259 /* This is meant to halt the backtrace at "_start". */
2260 pc = get_frame_pc (this_frame);
345bd07c
SM
2261 gdbarch *arch = get_frame_arch (this_frame);
2262 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (arch);
2263 if (pc <= tdep->lowest_pc)
c1ee9414
LM
2264 return UNWIND_OUTERMOST;
2265
2266 /* If we've hit a wall, stop. */
ae7e2f45 2267 if (arm_cache_get_prev_sp_value (cache, tdep) == 0)
c1ee9414
LM
2268 return UNWIND_OUTERMOST;
2269
2270 return UNWIND_NO_REASON;
2271}
2272
eb5492fa
DJ
2273/* Our frame ID for a normal frame is the current function's starting PC
2274 and the caller's SP when we were called. */
c906108c 2275
148754e5 2276static void
a262aec2 2277arm_prologue_this_id (struct frame_info *this_frame,
eb5492fa
DJ
2278 void **this_cache,
2279 struct frame_id *this_id)
c906108c 2280{
eb5492fa
DJ
2281 struct arm_prologue_cache *cache;
2282 struct frame_id id;
2c404490 2283 CORE_ADDR pc, func;
f079148d 2284
eb5492fa 2285 if (*this_cache == NULL)
a262aec2 2286 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 2287 cache = (struct arm_prologue_cache *) *this_cache;
2a451106 2288
ae7e2f45
CL
2289 arm_gdbarch_tdep *tdep
2290 = (arm_gdbarch_tdep *) gdbarch_tdep (get_frame_arch (this_frame));
2291
0e9e9abd
UW
2292 /* Use function start address as part of the frame ID. If we cannot
2293 identify the start address (due to missing symbol information),
2294 fall back to just using the current PC. */
c1ee9414 2295 pc = get_frame_pc (this_frame);
2c404490 2296 func = get_frame_func (this_frame);
0e9e9abd
UW
2297 if (!func)
2298 func = pc;
2299
ae7e2f45 2300 id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep), func);
eb5492fa 2301 *this_id = id;
c906108c
SS
2302}
2303
a262aec2
DJ
2304static struct value *
2305arm_prologue_prev_register (struct frame_info *this_frame,
eb5492fa 2306 void **this_cache,
a262aec2 2307 int prev_regnum)
24de872b 2308{
24568a2c 2309 struct gdbarch *gdbarch = get_frame_arch (this_frame);
24de872b 2310 struct arm_prologue_cache *cache;
ef273377 2311 CORE_ADDR sp_value;
24de872b 2312
eb5492fa 2313 if (*this_cache == NULL)
a262aec2 2314 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 2315 cache = (struct arm_prologue_cache *) *this_cache;
24de872b 2316
a01567f4
LM
2317 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
2318
2319 /* If this frame has signed the return address, mark it as so. */
2320 if (tdep->have_pacbti && cache->ra_signed_state.has_value ()
2321 && *cache->ra_signed_state)
2322 set_frame_previous_pc_masked (this_frame);
2323
eb5492fa 2324 /* If we are asked to unwind the PC, then we need to return the LR
b39cc962
DJ
2325 instead. The prologue may save PC, but it will point into this
2326 frame's prologue, not the next frame's resume location. Also
2327 strip the saved T bit. A valid LR may have the low bit set, but
2328 a valid PC never does. */
eb5492fa 2329 if (prev_regnum == ARM_PC_REGNUM)
b39cc962
DJ
2330 {
2331 CORE_ADDR lr;
2332
2333 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2334 return frame_unwind_got_constant (this_frame, prev_regnum,
24568a2c 2335 arm_addr_bits_remove (gdbarch, lr));
b39cc962 2336 }
24de872b 2337
eb5492fa 2338 /* SP is generally not saved to the stack, but this frame is
a262aec2 2339 identified by the next frame's stack pointer at the time of the call.
eb5492fa
DJ
2340 The value was already reconstructed into PREV_SP. */
2341 if (prev_regnum == ARM_SP_REGNUM)
ae7e2f45
CL
2342 return frame_unwind_got_constant (this_frame, prev_regnum,
2343 arm_cache_get_prev_sp_value (cache, tdep));
eb5492fa 2344
ef273377
CL
2345 /* The value might be one of the alternative SP, if so, use the
2346 value already constructed. */
d65edaa0 2347 if (arm_is_alternative_sp_register (tdep, prev_regnum))
ef273377
CL
2348 {
2349 sp_value = arm_cache_get_sp_register (cache, tdep, prev_regnum);
2350 return frame_unwind_got_constant (this_frame, prev_regnum, sp_value);
2351 }
2352
b39cc962
DJ
2353 /* The CPSR may have been changed by the call instruction and by the
2354 called function. The only bit we can reconstruct is the T bit,
2355 by checking the low bit of LR as of the call. This is a reliable
2356 indicator of Thumb-ness except for some ARM v4T pre-interworking
2357 Thumb code, which could get away with a clear low bit as long as
2358 the called function did not use bx. Guess that all other
2359 bits are unchanged; the condition flags are presumably lost,
2360 but the processor status is likely valid. */
2361 if (prev_regnum == ARM_PS_REGNUM)
2362 {
8c9ae6df
YR
2363 ULONGEST cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
2364 CORE_ADDR lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
b39cc962 2365
8c9ae6df 2366 cpsr = reconstruct_t_bit (gdbarch, lr, cpsr);
b39cc962
DJ
2367 return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
2368 }
2369
a262aec2
DJ
2370 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
2371 prev_regnum);
eb5492fa
DJ
2372}
2373
6bd434d6 2374static frame_unwind arm_prologue_unwind = {
a154d838 2375 "arm prologue",
eb5492fa 2376 NORMAL_FRAME,
c1ee9414 2377 arm_prologue_unwind_stop_reason,
eb5492fa 2378 arm_prologue_this_id,
a262aec2
DJ
2379 arm_prologue_prev_register,
2380 NULL,
2381 default_frame_sniffer
eb5492fa
DJ
2382};
2383
0e9e9abd
UW
2384/* Maintain a list of ARM exception table entries per objfile, similar to the
2385 list of mapping symbols. We only cache entries for standard ARM-defined
2386 personality routines; the cache will contain only the frame unwinding
2387 instructions associated with the entry (not the descriptors). */
2388
0e9e9abd
UW
2389struct arm_exidx_entry
2390{
227031b2 2391 CORE_ADDR addr;
0e9e9abd 2392 gdb_byte *entry;
7a5d944b
TT
2393
2394 bool operator< (const arm_exidx_entry &other) const
2395 {
2396 return addr < other.addr;
2397 }
0e9e9abd 2398};
0e9e9abd
UW
2399
2400struct arm_exidx_data
2401{
7a5d944b 2402 std::vector<std::vector<arm_exidx_entry>> section_maps;
0e9e9abd
UW
2403};
2404
a2726d4f
LM
2405/* Per-BFD key to store exception handling information. */
2406static const struct bfd_key<arm_exidx_data> arm_exidx_data_key;
0e9e9abd
UW
2407
2408static struct obj_section *
2409arm_obj_section_from_vma (struct objfile *objfile, bfd_vma vma)
2410{
2411 struct obj_section *osect;
2412
2413 ALL_OBJFILE_OSECTIONS (objfile, osect)
fd361982 2414 if (bfd_section_flags (osect->the_bfd_section) & SEC_ALLOC)
0e9e9abd
UW
2415 {
2416 bfd_vma start, size;
fd361982
AM
2417 start = bfd_section_vma (osect->the_bfd_section);
2418 size = bfd_section_size (osect->the_bfd_section);
0e9e9abd
UW
2419
2420 if (start <= vma && vma < start + size)
2421 return osect;
2422 }
2423
2424 return NULL;
2425}
2426
2427/* Parse contents of exception table and exception index sections
2428 of OBJFILE, and fill in the exception table entry cache.
2429
2430 For each entry that refers to a standard ARM-defined personality
2431 routine, extract the frame unwinding instructions (from either
2432 the index or the table section). The unwinding instructions
2433 are normalized by:
2434 - extracting them from the rest of the table data
2435 - converting to host endianness
2436 - appending the implicit 0xb0 ("Finish") code
2437
2438 The extracted and normalized instructions are stored for later
2439 retrieval by the arm_find_exidx_entry routine. */
2440
2441static void
2442arm_exidx_new_objfile (struct objfile *objfile)
2443{
0e9e9abd
UW
2444 struct arm_exidx_data *data;
2445 asection *exidx, *extab;
2446 bfd_vma exidx_vma = 0, extab_vma = 0;
0e9e9abd
UW
2447 LONGEST i;
2448
2449 /* If we've already touched this file, do nothing. */
a2726d4f 2450 if (!objfile || arm_exidx_data_key.get (objfile->obfd) != NULL)
0e9e9abd
UW
2451 return;
2452
2453 /* Read contents of exception table and index. */
a5eda10c 2454 exidx = bfd_get_section_by_name (objfile->obfd, ELF_STRING_ARM_unwind);
984c7238 2455 gdb::byte_vector exidx_data;
0e9e9abd
UW
2456 if (exidx)
2457 {
fd361982
AM
2458 exidx_vma = bfd_section_vma (exidx);
2459 exidx_data.resize (bfd_section_size (exidx));
0e9e9abd
UW
2460
2461 if (!bfd_get_section_contents (objfile->obfd, exidx,
984c7238
TT
2462 exidx_data.data (), 0,
2463 exidx_data.size ()))
2464 return;
0e9e9abd
UW
2465 }
2466
2467 extab = bfd_get_section_by_name (objfile->obfd, ".ARM.extab");
984c7238 2468 gdb::byte_vector extab_data;
0e9e9abd
UW
2469 if (extab)
2470 {
fd361982
AM
2471 extab_vma = bfd_section_vma (extab);
2472 extab_data.resize (bfd_section_size (extab));
0e9e9abd
UW
2473
2474 if (!bfd_get_section_contents (objfile->obfd, extab,
984c7238
TT
2475 extab_data.data (), 0,
2476 extab_data.size ()))
2477 return;
0e9e9abd
UW
2478 }
2479
2480 /* Allocate exception table data structure. */
a2726d4f 2481 data = arm_exidx_data_key.emplace (objfile->obfd);
7a5d944b 2482 data->section_maps.resize (objfile->obfd->section_count);
0e9e9abd
UW
2483
2484 /* Fill in exception table. */
984c7238 2485 for (i = 0; i < exidx_data.size () / 8; i++)
0e9e9abd
UW
2486 {
2487 struct arm_exidx_entry new_exidx_entry;
984c7238
TT
2488 bfd_vma idx = bfd_h_get_32 (objfile->obfd, exidx_data.data () + i * 8);
2489 bfd_vma val = bfd_h_get_32 (objfile->obfd,
2490 exidx_data.data () + i * 8 + 4);
0e9e9abd
UW
2491 bfd_vma addr = 0, word = 0;
2492 int n_bytes = 0, n_words = 0;
2493 struct obj_section *sec;
2494 gdb_byte *entry = NULL;
2495
2496 /* Extract address of start of function. */
2497 idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2498 idx += exidx_vma + i * 8;
2499
2500 /* Find section containing function and compute section offset. */
2501 sec = arm_obj_section_from_vma (objfile, idx);
2502 if (sec == NULL)
2503 continue;
fd361982 2504 idx -= bfd_section_vma (sec->the_bfd_section);
0e9e9abd
UW
2505
2506 /* Determine address of exception table entry. */
2507 if (val == 1)
2508 {
2509 /* EXIDX_CANTUNWIND -- no exception table entry present. */
2510 }
2511 else if ((val & 0xff000000) == 0x80000000)
2512 {
2513 /* Exception table entry embedded in .ARM.exidx
2514 -- must be short form. */
2515 word = val;
2516 n_bytes = 3;
2517 }
2518 else if (!(val & 0x80000000))
2519 {
2520 /* Exception table entry in .ARM.extab. */
2521 addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2522 addr += exidx_vma + i * 8 + 4;
2523
984c7238 2524 if (addr >= extab_vma && addr + 4 <= extab_vma + extab_data.size ())
0e9e9abd
UW
2525 {
2526 word = bfd_h_get_32 (objfile->obfd,
984c7238 2527 extab_data.data () + addr - extab_vma);
0e9e9abd
UW
2528 addr += 4;
2529
2530 if ((word & 0xff000000) == 0x80000000)
2531 {
2532 /* Short form. */
2533 n_bytes = 3;
2534 }
2535 else if ((word & 0xff000000) == 0x81000000
2536 || (word & 0xff000000) == 0x82000000)
2537 {
2538 /* Long form. */
2539 n_bytes = 2;
2540 n_words = ((word >> 16) & 0xff);
2541 }
2542 else if (!(word & 0x80000000))
2543 {
2544 bfd_vma pers;
2545 struct obj_section *pers_sec;
2546 int gnu_personality = 0;
2547
2548 /* Custom personality routine. */
2549 pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2550 pers = UNMAKE_THUMB_ADDR (pers + addr - 4);
2551
2552 /* Check whether we've got one of the variants of the
2553 GNU personality routines. */
2554 pers_sec = arm_obj_section_from_vma (objfile, pers);
2555 if (pers_sec)
2556 {
2557 static const char *personality[] =
2558 {
2559 "__gcc_personality_v0",
2560 "__gxx_personality_v0",
2561 "__gcj_personality_v0",
2562 "__gnu_objc_personality_v0",
2563 NULL
2564 };
2565
0c1bcd23 2566 CORE_ADDR pc = pers + pers_sec->offset ();
0e9e9abd
UW
2567 int k;
2568
2569 for (k = 0; personality[k]; k++)
2570 if (lookup_minimal_symbol_by_pc_name
2571 (pc, personality[k], objfile))
2572 {
2573 gnu_personality = 1;
2574 break;
2575 }
2576 }
2577
2578 /* If so, the next word contains a word count in the high
2579 byte, followed by the same unwind instructions as the
2580 pre-defined forms. */
2581 if (gnu_personality
984c7238 2582 && addr + 4 <= extab_vma + extab_data.size ())
0e9e9abd
UW
2583 {
2584 word = bfd_h_get_32 (objfile->obfd,
984c7238
TT
2585 (extab_data.data ()
2586 + addr - extab_vma));
0e9e9abd
UW
2587 addr += 4;
2588 n_bytes = 3;
2589 n_words = ((word >> 24) & 0xff);
2590 }
2591 }
2592 }
2593 }
2594
2595 /* Sanity check address. */
2596 if (n_words)
984c7238
TT
2597 if (addr < extab_vma
2598 || addr + 4 * n_words > extab_vma + extab_data.size ())
0e9e9abd
UW
2599 n_words = n_bytes = 0;
2600
2601 /* The unwind instructions reside in WORD (only the N_BYTES least
2602 significant bytes are valid), followed by N_WORDS words in the
2603 extab section starting at ADDR. */
2604 if (n_bytes || n_words)
2605 {
224c3ddb
SM
2606 gdb_byte *p = entry
2607 = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack,
2608 n_bytes + n_words * 4 + 1);
0e9e9abd
UW
2609
2610 while (n_bytes--)
2611 *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);
2612
2613 while (n_words--)
2614 {
2615 word = bfd_h_get_32 (objfile->obfd,
984c7238 2616 extab_data.data () + addr - extab_vma);
0e9e9abd
UW
2617 addr += 4;
2618
2619 *p++ = (gdb_byte) ((word >> 24) & 0xff);
2620 *p++ = (gdb_byte) ((word >> 16) & 0xff);
2621 *p++ = (gdb_byte) ((word >> 8) & 0xff);
2622 *p++ = (gdb_byte) (word & 0xff);
2623 }
2624
2625 /* Implied "Finish" to terminate the list. */
2626 *p++ = 0xb0;
2627 }
2628
2629 /* Push entry onto vector. They are guaranteed to always
2630 appear in order of increasing addresses. */
2631 new_exidx_entry.addr = idx;
2632 new_exidx_entry.entry = entry;
7a5d944b
TT
2633 data->section_maps[sec->the_bfd_section->index].push_back
2634 (new_exidx_entry);
0e9e9abd 2635 }
0e9e9abd
UW
2636}
2637
2638/* Search for the exception table entry covering MEMADDR. If one is found,
2639 return a pointer to its data. Otherwise, return 0. If START is non-NULL,
2640 set *START to the start of the region covered by this entry. */
2641
2642static gdb_byte *
2643arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
2644{
2645 struct obj_section *sec;
2646
2647 sec = find_pc_section (memaddr);
2648 if (sec != NULL)
2649 {
2650 struct arm_exidx_data *data;
0c1bcd23 2651 struct arm_exidx_entry map_key = { memaddr - sec->addr (), 0 };
0e9e9abd 2652
a2726d4f 2653 data = arm_exidx_data_key.get (sec->objfile->obfd);
0e9e9abd
UW
2654 if (data != NULL)
2655 {
7a5d944b
TT
2656 std::vector<arm_exidx_entry> &map
2657 = data->section_maps[sec->the_bfd_section->index];
2658 if (!map.empty ())
0e9e9abd 2659 {
7a5d944b 2660 auto idx = std::lower_bound (map.begin (), map.end (), map_key);
0e9e9abd 2661
7a5d944b 2662 /* std::lower_bound finds the earliest ordered insertion
0e9e9abd
UW
2663 point. If the following symbol starts at this exact
2664 address, we use that; otherwise, the preceding
2665 exception table entry covers this address. */
7a5d944b 2666 if (idx < map.end ())
0e9e9abd 2667 {
7a5d944b 2668 if (idx->addr == map_key.addr)
0e9e9abd
UW
2669 {
2670 if (start)
0c1bcd23 2671 *start = idx->addr + sec->addr ();
7a5d944b 2672 return idx->entry;
0e9e9abd
UW
2673 }
2674 }
2675
7a5d944b 2676 if (idx > map.begin ())
0e9e9abd 2677 {
7a5d944b 2678 idx = idx - 1;
0e9e9abd 2679 if (start)
0c1bcd23 2680 *start = idx->addr + sec->addr ();
7a5d944b 2681 return idx->entry;
0e9e9abd
UW
2682 }
2683 }
2684 }
2685 }
2686
2687 return NULL;
2688}
2689
2690/* Given the current frame THIS_FRAME, and its associated frame unwinding
2691 instruction list from the ARM exception table entry ENTRY, allocate and
2692 return a prologue cache structure describing how to unwind this frame.
2693
2694 Return NULL if the unwinding instruction list contains a "spare",
2695 "reserved" or "refuse to unwind" instruction as defined in section
2696 "9.3 Frame unwinding instructions" of the "Exception Handling ABI
2697 for the ARM Architecture" document. */
2698
2699static struct arm_prologue_cache *
2700arm_exidx_fill_cache (struct frame_info *this_frame, gdb_byte *entry)
2701{
2702 CORE_ADDR vsp = 0;
2703 int vsp_valid = 0;
2704
2705 struct arm_prologue_cache *cache;
2706 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 2707 arm_cache_init (cache, this_frame);
0e9e9abd
UW
2708
2709 for (;;)
2710 {
2711 gdb_byte insn;
2712
2713 /* Whenever we reload SP, we actually have to retrieve its
2714 actual value in the current frame. */
2715 if (!vsp_valid)
2716 {
a9a87d35 2717 if (cache->saved_regs[ARM_SP_REGNUM].is_realreg ())
0e9e9abd 2718 {
098caef4 2719 int reg = cache->saved_regs[ARM_SP_REGNUM].realreg ();
0e9e9abd
UW
2720 vsp = get_frame_register_unsigned (this_frame, reg);
2721 }
2722 else
2723 {
098caef4 2724 CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr ();
0e9e9abd
UW
2725 vsp = get_frame_memory_unsigned (this_frame, addr, 4);
2726 }
2727
2728 vsp_valid = 1;
2729 }
2730
2731 /* Decode next unwind instruction. */
2732 insn = *entry++;
2733
2734 if ((insn & 0xc0) == 0)
2735 {
2736 int offset = insn & 0x3f;
2737 vsp += (offset << 2) + 4;
2738 }
2739 else if ((insn & 0xc0) == 0x40)
2740 {
2741 int offset = insn & 0x3f;
2742 vsp -= (offset << 2) + 4;
2743 }
2744 else if ((insn & 0xf0) == 0x80)
2745 {
2746 int mask = ((insn & 0xf) << 8) | *entry++;
2747 int i;
2748
2749 /* The special case of an all-zero mask identifies
2750 "Refuse to unwind". We return NULL to fall back
2751 to the prologue analyzer. */
2752 if (mask == 0)
2753 return NULL;
2754
2755 /* Pop registers r4..r15 under mask. */
2756 for (i = 0; i < 12; i++)
2757 if (mask & (1 << i))
2758 {
098caef4 2759 cache->saved_regs[4 + i].set_addr (vsp);
0e9e9abd
UW
2760 vsp += 4;
2761 }
2762
2763 /* Special-case popping SP -- we need to reload vsp. */
2764 if (mask & (1 << (ARM_SP_REGNUM - 4)))
2765 vsp_valid = 0;
2766 }
2767 else if ((insn & 0xf0) == 0x90)
2768 {
2769 int reg = insn & 0xf;
2770
2771 /* Reserved cases. */
2772 if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
2773 return NULL;
2774
2775 /* Set SP from another register and mark VSP for reload. */
2776 cache->saved_regs[ARM_SP_REGNUM] = cache->saved_regs[reg];
2777 vsp_valid = 0;
2778 }
2779 else if ((insn & 0xf0) == 0xa0)
2780 {
2781 int count = insn & 0x7;
2782 int pop_lr = (insn & 0x8) != 0;
2783 int i;
2784
2785 /* Pop r4..r[4+count]. */
2786 for (i = 0; i <= count; i++)
2787 {
098caef4 2788 cache->saved_regs[4 + i].set_addr (vsp);
0e9e9abd
UW
2789 vsp += 4;
2790 }
2791
2792 /* If indicated by flag, pop LR as well. */
2793 if (pop_lr)
2794 {
098caef4 2795 cache->saved_regs[ARM_LR_REGNUM].set_addr (vsp);
0e9e9abd
UW
2796 vsp += 4;
2797 }
2798 }
2799 else if (insn == 0xb0)
2800 {
2801 /* We could only have updated PC by popping into it; if so, it
2802 will show up as address. Otherwise, copy LR into PC. */
a9a87d35 2803 if (!cache->saved_regs[ARM_PC_REGNUM].is_addr ())
0e9e9abd
UW
2804 cache->saved_regs[ARM_PC_REGNUM]
2805 = cache->saved_regs[ARM_LR_REGNUM];
2806
2807 /* We're done. */
2808 break;
2809 }
2810 else if (insn == 0xb1)
2811 {
2812 int mask = *entry++;
2813 int i;
2814
2815 /* All-zero mask and mask >= 16 is "spare". */
2816 if (mask == 0 || mask >= 16)
2817 return NULL;
2818
2819 /* Pop r0..r3 under mask. */
2820 for (i = 0; i < 4; i++)
2821 if (mask & (1 << i))
2822 {
098caef4 2823 cache->saved_regs[i].set_addr (vsp);
0e9e9abd
UW
2824 vsp += 4;
2825 }
2826 }
2827 else if (insn == 0xb2)
2828 {
2829 ULONGEST offset = 0;
2830 unsigned shift = 0;
2831
2832 do
2833 {
2834 offset |= (*entry & 0x7f) << shift;
2835 shift += 7;
2836 }
2837 while (*entry++ & 0x80);
2838
2839 vsp += 0x204 + (offset << 2);
2840 }
2841 else if (insn == 0xb3)
2842 {
2843 int start = *entry >> 4;
2844 int count = (*entry++) & 0xf;
2845 int i;
2846
2847 /* Only registers D0..D15 are valid here. */
2848 if (start + count >= 16)
2849 return NULL;
2850
2851 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2852 for (i = 0; i <= count; i++)
2853 {
098caef4 2854 cache->saved_regs[ARM_D0_REGNUM + start + i].set_addr (vsp);
0e9e9abd
UW
2855 vsp += 8;
2856 }
2857
2858 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2859 vsp += 4;
2860 }
2861 else if ((insn & 0xf8) == 0xb8)
2862 {
2863 int count = insn & 0x7;
2864 int i;
2865
2866 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2867 for (i = 0; i <= count; i++)
2868 {
098caef4 2869 cache->saved_regs[ARM_D0_REGNUM + 8 + i].set_addr (vsp);
0e9e9abd
UW
2870 vsp += 8;
2871 }
2872
2873 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2874 vsp += 4;
2875 }
2876 else if (insn == 0xc6)
2877 {
2878 int start = *entry >> 4;
2879 int count = (*entry++) & 0xf;
2880 int i;
2881
2882 /* Only registers WR0..WR15 are valid. */
2883 if (start + count >= 16)
2884 return NULL;
2885
2886 /* Pop iwmmx registers WR[start]..WR[start+count]. */
2887 for (i = 0; i <= count; i++)
2888 {
098caef4 2889 cache->saved_regs[ARM_WR0_REGNUM + start + i].set_addr (vsp);
0e9e9abd
UW
2890 vsp += 8;
2891 }
2892 }
2893 else if (insn == 0xc7)
2894 {
2895 int mask = *entry++;
2896 int i;
2897
2898 /* All-zero mask and mask >= 16 is "spare". */
2899 if (mask == 0 || mask >= 16)
2900 return NULL;
2901
2902 /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask. */
2903 for (i = 0; i < 4; i++)
2904 if (mask & (1 << i))
2905 {
098caef4 2906 cache->saved_regs[ARM_WCGR0_REGNUM + i].set_addr (vsp);
0e9e9abd
UW
2907 vsp += 4;
2908 }
2909 }
2910 else if ((insn & 0xf8) == 0xc0)
2911 {
2912 int count = insn & 0x7;
2913 int i;
2914
2915 /* Pop iwmmx registers WR[10]..WR[10+count]. */
2916 for (i = 0; i <= count; i++)
2917 {
098caef4 2918 cache->saved_regs[ARM_WR0_REGNUM + 10 + i].set_addr (vsp);
0e9e9abd
UW
2919 vsp += 8;
2920 }
2921 }
2922 else if (insn == 0xc8)
2923 {
2924 int start = *entry >> 4;
2925 int count = (*entry++) & 0xf;
2926 int i;
2927
2928 /* Only registers D0..D31 are valid. */
2929 if (start + count >= 16)
2930 return NULL;
2931
2932 /* Pop VFP double-precision registers
2933 D[16+start]..D[16+start+count]. */
2934 for (i = 0; i <= count; i++)
2935 {
098caef4 2936 cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].set_addr (vsp);
0e9e9abd
UW
2937 vsp += 8;
2938 }
2939 }
2940 else if (insn == 0xc9)
2941 {
2942 int start = *entry >> 4;
2943 int count = (*entry++) & 0xf;
2944 int i;
2945
2946 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2947 for (i = 0; i <= count; i++)
2948 {
098caef4 2949 cache->saved_regs[ARM_D0_REGNUM + start + i].set_addr (vsp);
0e9e9abd
UW
2950 vsp += 8;
2951 }
2952 }
2953 else if ((insn & 0xf8) == 0xd0)
2954 {
2955 int count = insn & 0x7;
2956 int i;
2957
2958 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2959 for (i = 0; i <= count; i++)
2960 {
098caef4 2961 cache->saved_regs[ARM_D0_REGNUM + 8 + i].set_addr (vsp);
0e9e9abd
UW
2962 vsp += 8;
2963 }
2964 }
2965 else
2966 {
2967 /* Everything else is "spare". */
2968 return NULL;
2969 }
2970 }
2971
2972 /* If we restore SP from a register, assume this was the frame register.
2973 Otherwise just fall back to SP as frame register. */
a9a87d35 2974 if (cache->saved_regs[ARM_SP_REGNUM].is_realreg ())
098caef4 2975 cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg ();
0e9e9abd
UW
2976 else
2977 cache->framereg = ARM_SP_REGNUM;
2978
2979 /* Determine offset to previous frame. */
2980 cache->framesize
2981 = vsp - get_frame_register_unsigned (this_frame, cache->framereg);
2982
2983 /* We already got the previous SP. */
ae7e2f45
CL
2984 arm_gdbarch_tdep *tdep
2985 = (arm_gdbarch_tdep *) gdbarch_tdep (get_frame_arch (this_frame));
2986 arm_cache_set_active_sp_value (cache, tdep, vsp);
0e9e9abd
UW
2987
2988 return cache;
2989}
2990
2991/* Unwinding via ARM exception table entries. Note that the sniffer
2992 already computes a filled-in prologue cache, which is then used
2993 with the same arm_prologue_this_id and arm_prologue_prev_register
2994 routines also used for prologue-parsing based unwinding. */
2995
2996static int
2997arm_exidx_unwind_sniffer (const struct frame_unwind *self,
2998 struct frame_info *this_frame,
2999 void **this_prologue_cache)
3000{
3001 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3002 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3003 CORE_ADDR addr_in_block, exidx_region, func_start;
3004 struct arm_prologue_cache *cache;
3005 gdb_byte *entry;
3006
3007 /* See if we have an ARM exception table entry covering this address. */
3008 addr_in_block = get_frame_address_in_block (this_frame);
3009 entry = arm_find_exidx_entry (addr_in_block, &exidx_region);
3010 if (!entry)
3011 return 0;
3012
3013 /* The ARM exception table does not describe unwind information
3014 for arbitrary PC values, but is guaranteed to be correct only
3015 at call sites. We have to decide here whether we want to use
3016 ARM exception table information for this frame, or fall back
3017 to using prologue parsing. (Note that if we have DWARF CFI,
3018 this sniffer isn't even called -- CFI is always preferred.)
3019
3020 Before we make this decision, however, we check whether we
3021 actually have *symbol* information for the current frame.
3022 If not, prologue parsing would not work anyway, so we might
3023 as well use the exception table and hope for the best. */
3024 if (find_pc_partial_function (addr_in_block, NULL, &func_start, NULL))
3025 {
3026 int exc_valid = 0;
3027
3028 /* If the next frame is "normal", we are at a call site in this
3029 frame, so exception information is guaranteed to be valid. */
3030 if (get_next_frame (this_frame)
3031 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
3032 exc_valid = 1;
3033
3034 /* We also assume exception information is valid if we're currently
3035 blocked in a system call. The system library is supposed to
d9311bfa
AT
3036 ensure this, so that e.g. pthread cancellation works. */
3037 if (arm_frame_is_thumb (this_frame))
0e9e9abd 3038 {
7913a64c 3039 ULONGEST insn;
416dc9c6 3040
7913a64c
YQ
3041 if (safe_read_memory_unsigned_integer (get_frame_pc (this_frame) - 2,
3042 2, byte_order_for_code, &insn)
d9311bfa
AT
3043 && (insn & 0xff00) == 0xdf00 /* svc */)
3044 exc_valid = 1;
0e9e9abd 3045 }
d9311bfa
AT
3046 else
3047 {
7913a64c 3048 ULONGEST insn;
416dc9c6 3049
7913a64c
YQ
3050 if (safe_read_memory_unsigned_integer (get_frame_pc (this_frame) - 4,
3051 4, byte_order_for_code, &insn)
d9311bfa
AT
3052 && (insn & 0x0f000000) == 0x0f000000 /* svc */)
3053 exc_valid = 1;
3054 }
3055
0e9e9abd
UW
3056 /* Bail out if we don't know that exception information is valid. */
3057 if (!exc_valid)
3058 return 0;
3059
3060 /* The ARM exception index does not mark the *end* of the region
3061 covered by the entry, and some functions will not have any entry.
3062 To correctly recognize the end of the covered region, the linker
3063 should have inserted dummy records with a CANTUNWIND marker.
3064
3065 Unfortunately, current versions of GNU ld do not reliably do
3066 this, and thus we may have found an incorrect entry above.
3067 As a (temporary) sanity check, we only use the entry if it
3068 lies *within* the bounds of the function. Note that this check
3069 might reject perfectly valid entries that just happen to cover
3070 multiple functions; therefore this check ought to be removed
3071 once the linker is fixed. */
3072 if (func_start > exidx_region)
3073 return 0;
3074 }
3075
3076 /* Decode the list of unwinding instructions into a prologue cache.
3077 Note that this may fail due to e.g. a "refuse to unwind" code. */
3078 cache = arm_exidx_fill_cache (this_frame, entry);
3079 if (!cache)
3080 return 0;
3081
3082 *this_prologue_cache = cache;
3083 return 1;
3084}
3085
3086struct frame_unwind arm_exidx_unwind = {
a154d838 3087 "arm exidx",
0e9e9abd 3088 NORMAL_FRAME,
8fbca658 3089 default_frame_unwind_stop_reason,
0e9e9abd
UW
3090 arm_prologue_this_id,
3091 arm_prologue_prev_register,
3092 NULL,
3093 arm_exidx_unwind_sniffer
3094};
3095
779aa56f
YQ
3096static struct arm_prologue_cache *
3097arm_make_epilogue_frame_cache (struct frame_info *this_frame)
3098{
3099 struct arm_prologue_cache *cache;
779aa56f
YQ
3100 int reg;
3101
3102 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 3103 arm_cache_init (cache, this_frame);
779aa56f
YQ
3104
3105 /* Still rely on the offset calculated from prologue. */
3106 arm_scan_prologue (this_frame, cache);
3107
3108 /* Since we are in epilogue, the SP has been restored. */
ae7e2f45
CL
3109 arm_gdbarch_tdep *tdep
3110 = (arm_gdbarch_tdep *) gdbarch_tdep (get_frame_arch (this_frame));
3111 arm_cache_set_active_sp_value (cache, tdep,
3112 get_frame_register_unsigned (this_frame,
3113 ARM_SP_REGNUM));
779aa56f
YQ
3114
3115 /* Calculate actual addresses of saved registers using offsets
3116 determined by arm_scan_prologue. */
3117 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
a9a87d35 3118 if (cache->saved_regs[reg].is_addr ())
098caef4 3119 cache->saved_regs[reg].set_addr (cache->saved_regs[reg].addr ()
ae7e2f45 3120 + arm_cache_get_prev_sp_value (cache, tdep));
779aa56f
YQ
3121
3122 return cache;
3123}
3124
3125/* Implementation of function hook 'this_id' in
3126 'struct frame_uwnind' for epilogue unwinder. */
3127
3128static void
3129arm_epilogue_frame_this_id (struct frame_info *this_frame,
3130 void **this_cache,
3131 struct frame_id *this_id)
3132{
3133 struct arm_prologue_cache *cache;
3134 CORE_ADDR pc, func;
3135
3136 if (*this_cache == NULL)
3137 *this_cache = arm_make_epilogue_frame_cache (this_frame);
3138 cache = (struct arm_prologue_cache *) *this_cache;
3139
3140 /* Use function start address as part of the frame ID. If we cannot
3141 identify the start address (due to missing symbol information),
3142 fall back to just using the current PC. */
3143 pc = get_frame_pc (this_frame);
3144 func = get_frame_func (this_frame);
fb3f3d25 3145 if (func == 0)
779aa56f
YQ
3146 func = pc;
3147
ae7e2f45
CL
3148 arm_gdbarch_tdep *tdep
3149 = (arm_gdbarch_tdep *) gdbarch_tdep (get_frame_arch (this_frame));
3150 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep), pc);
779aa56f
YQ
3151}
3152
3153/* Implementation of function hook 'prev_register' in
3154 'struct frame_uwnind' for epilogue unwinder. */
3155
3156static struct value *
3157arm_epilogue_frame_prev_register (struct frame_info *this_frame,
3158 void **this_cache, int regnum)
3159{
779aa56f
YQ
3160 if (*this_cache == NULL)
3161 *this_cache = arm_make_epilogue_frame_cache (this_frame);
779aa56f
YQ
3162
3163 return arm_prologue_prev_register (this_frame, this_cache, regnum);
3164}
3165
3166static int arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch,
3167 CORE_ADDR pc);
3168static int thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch,
3169 CORE_ADDR pc);
3170
3171/* Implementation of function hook 'sniffer' in
3172 'struct frame_uwnind' for epilogue unwinder. */
3173
3174static int
3175arm_epilogue_frame_sniffer (const struct frame_unwind *self,
3176 struct frame_info *this_frame,
3177 void **this_prologue_cache)
3178{
3179 if (frame_relative_level (this_frame) == 0)
3180 {
3181 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3182 CORE_ADDR pc = get_frame_pc (this_frame);
3183
3184 if (arm_frame_is_thumb (this_frame))
3185 return thumb_stack_frame_destroyed_p (gdbarch, pc);
3186 else
3187 return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
3188 }
3189 else
3190 return 0;
3191}
3192
3193/* Frame unwinder from epilogue. */
3194
3195static const struct frame_unwind arm_epilogue_frame_unwind =
3196{
a154d838 3197 "arm epilogue",
779aa56f
YQ
3198 NORMAL_FRAME,
3199 default_frame_unwind_stop_reason,
3200 arm_epilogue_frame_this_id,
3201 arm_epilogue_frame_prev_register,
3202 NULL,
3203 arm_epilogue_frame_sniffer,
3204};
3205
80d8d390
YQ
3206/* Recognize GCC's trampoline for thumb call-indirect. If we are in a
3207 trampoline, return the target PC. Otherwise return 0.
3208
3209 void call0a (char c, short s, int i, long l) {}
3210
3211 int main (void)
3212 {
3213 (*pointer_to_call0a) (c, s, i, l);
3214 }
3215
3216 Instead of calling a stub library function _call_via_xx (xx is
3217 the register name), GCC may inline the trampoline in the object
3218 file as below (register r2 has the address of call0a).
3219
3220 .global main
3221 .type main, %function
3222 ...
3223 bl .L1
3224 ...
3225 .size main, .-main
3226
3227 .L1:
3228 bx r2
3229
3230 The trampoline 'bx r2' doesn't belong to main. */
3231
3232static CORE_ADDR
3233arm_skip_bx_reg (struct frame_info *frame, CORE_ADDR pc)
3234{
3235 /* The heuristics of recognizing such trampoline is that FRAME is
3236 executing in Thumb mode and the instruction on PC is 'bx Rm'. */
3237 if (arm_frame_is_thumb (frame))
3238 {
3239 gdb_byte buf[2];
3240
3241 if (target_read_memory (pc, buf, 2) == 0)
3242 {
3243 struct gdbarch *gdbarch = get_frame_arch (frame);
3244 enum bfd_endian byte_order_for_code
3245 = gdbarch_byte_order_for_code (gdbarch);
3246 uint16_t insn
3247 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3248
3249 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
3250 {
3251 CORE_ADDR dest
3252 = get_frame_register_unsigned (frame, bits (insn, 3, 6));
3253
3254 /* Clear the LSB so that gdb core sets step-resume
3255 breakpoint at the right address. */
3256 return UNMAKE_THUMB_ADDR (dest);
3257 }
3258 }
3259 }
3260
3261 return 0;
3262}
3263
909cf6ea 3264static struct arm_prologue_cache *
a262aec2 3265arm_make_stub_cache (struct frame_info *this_frame)
909cf6ea 3266{
909cf6ea 3267 struct arm_prologue_cache *cache;
909cf6ea 3268
35d5d4ee 3269 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 3270 arm_cache_init (cache, this_frame);
909cf6ea 3271
ae7e2f45
CL
3272 arm_gdbarch_tdep *tdep
3273 = (arm_gdbarch_tdep *) gdbarch_tdep (get_frame_arch (this_frame));
3274 arm_cache_set_active_sp_value (cache, tdep,
3275 get_frame_register_unsigned (this_frame,
3276 ARM_SP_REGNUM));
909cf6ea
DJ
3277
3278 return cache;
3279}
3280
3281/* Our frame ID for a stub frame is the current SP and LR. */
3282
3283static void
a262aec2 3284arm_stub_this_id (struct frame_info *this_frame,
909cf6ea
DJ
3285 void **this_cache,
3286 struct frame_id *this_id)
3287{
3288 struct arm_prologue_cache *cache;
3289
3290 if (*this_cache == NULL)
a262aec2 3291 *this_cache = arm_make_stub_cache (this_frame);
9a3c8263 3292 cache = (struct arm_prologue_cache *) *this_cache;
909cf6ea 3293
ae7e2f45
CL
3294 arm_gdbarch_tdep *tdep
3295 = (arm_gdbarch_tdep *) gdbarch_tdep (get_frame_arch (this_frame));
3296 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep),
3297 get_frame_pc (this_frame));
909cf6ea
DJ
3298}
3299
a262aec2
DJ
3300static int
3301arm_stub_unwind_sniffer (const struct frame_unwind *self,
3302 struct frame_info *this_frame,
3303 void **this_prologue_cache)
909cf6ea 3304{
93d42b30 3305 CORE_ADDR addr_in_block;
948f8e3d 3306 gdb_byte dummy[4];
18d18ac8
YQ
3307 CORE_ADDR pc, start_addr;
3308 const char *name;
909cf6ea 3309
a262aec2 3310 addr_in_block = get_frame_address_in_block (this_frame);
18d18ac8 3311 pc = get_frame_pc (this_frame);
3e5d3a5a 3312 if (in_plt_section (addr_in_block)
fc36e839
DE
3313 /* We also use the stub winder if the target memory is unreadable
3314 to avoid having the prologue unwinder trying to read it. */
18d18ac8
YQ
3315 || target_read_memory (pc, dummy, 4) != 0)
3316 return 1;
3317
3318 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0
3319 && arm_skip_bx_reg (this_frame, pc) != 0)
a262aec2 3320 return 1;
909cf6ea 3321
a262aec2 3322 return 0;
909cf6ea
DJ
3323}
3324
a262aec2 3325struct frame_unwind arm_stub_unwind = {
a154d838 3326 "arm stub",
a262aec2 3327 NORMAL_FRAME,
8fbca658 3328 default_frame_unwind_stop_reason,
a262aec2
DJ
3329 arm_stub_this_id,
3330 arm_prologue_prev_register,
3331 NULL,
3332 arm_stub_unwind_sniffer
3333};
3334
2ae28aa9
YQ
3335/* Put here the code to store, into CACHE->saved_regs, the addresses
3336 of the saved registers of frame described by THIS_FRAME. CACHE is
3337 returned. */
3338
3339static struct arm_prologue_cache *
3340arm_m_exception_cache (struct frame_info *this_frame)
3341{
3342 struct gdbarch *gdbarch = get_frame_arch (this_frame);
9074667a 3343 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
ef273377 3344 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2ae28aa9 3345 struct arm_prologue_cache *cache;
55ea94da 3346 CORE_ADDR lr;
ef273377 3347 CORE_ADDR sp;
2ae28aa9 3348 CORE_ADDR unwound_sp;
ef273377 3349 uint32_t sp_r0_offset = 0;
2ae28aa9 3350 LONGEST xpsr;
55ea94da 3351 uint32_t exc_return;
ef273377 3352 bool fnc_return;
55ea94da 3353 uint32_t extended_frame_used;
ef273377
CL
3354 bool secure_stack_used = false;
3355 bool default_callee_register_stacking = false;
3356 bool exception_domain_is_secure = false;
2ae28aa9
YQ
3357
3358 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 3359 arm_cache_init (cache, this_frame);
2ae28aa9 3360
55ea94da
FH
3361 /* ARMv7-M Architecture Reference "B1.5.6 Exception entry behavior"
3362 describes which bits in LR that define which stack was used prior
3363 to the exception and if FPU is used (causing extended stack frame). */
3364
3365 lr = get_frame_register_unsigned (this_frame, ARM_LR_REGNUM);
ef273377
CL
3366 sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
3367
148ca9dd
YR
3368 /* ARMv7-M Architecture Reference "A2.3.1 Arm core registers"
3369 states that LR is set to 0xffffffff on reset. ARMv8-M Architecture
3370 Reference "B3.3 Registers" states that LR is set to 0xffffffff on warm
3371 reset if Main Extension is implemented, otherwise the value is unknown. */
3372 if (lr == 0xffffffff)
3373 {
3374 /* Terminate any further stack unwinding by referring to self. */
3375 arm_cache_set_active_sp_value (cache, tdep, sp);
3376 return cache;
3377 }
3378
8c9ae6df 3379 fnc_return = (((lr >> 24) & 0xff) == 0xfe);
ef273377
CL
3380 if (tdep->have_sec_ext && fnc_return)
3381 {
8c9ae6df
YR
3382 if (!arm_unwind_secure_frames)
3383 {
3384 warning (_("Non-secure to secure stack unwinding disabled."));
ef273377 3385
8c9ae6df
YR
3386 /* Terminate any further stack unwinding by referring to self. */
3387 arm_cache_set_active_sp_value (cache, tdep, sp);
3388 return cache;
3389 }
3390
3391 xpsr = get_frame_register_unsigned (this_frame, ARM_PS_REGNUM);
3392 if ((xpsr & 0xff) != 0)
3393 /* Handler mode: This is the mode that exceptions are handled in. */
3394 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_msp_s_regnum);
ef273377 3395 else
8c9ae6df
YR
3396 /* Thread mode: This is the normal mode that programs run in. */
3397 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_psp_s_regnum);
ef273377 3398
8c9ae6df 3399 unwound_sp = arm_cache_get_prev_sp_value (cache, tdep);
55ea94da 3400
8c9ae6df
YR
3401 /* Stack layout for a function call from Secure to Non-Secure state
3402 (ARMv8-M section B3.16):
55ea94da 3403
8c9ae6df
YR
3404 SP Offset
3405
3406 +-------------------+
3407 0x08 | |
3408 +-------------------+ <-- Original SP
3409 0x04 | Partial xPSR |
3410 +-------------------+
3411 0x00 | Return Address |
3412 +===================+ <-- New SP */
3413
3414 cache->saved_regs[ARM_PC_REGNUM].set_addr (unwound_sp + 0x00);
3415 cache->saved_regs[ARM_LR_REGNUM].set_addr (unwound_sp + 0x00);
3416 cache->saved_regs[ARM_PS_REGNUM].set_addr (unwound_sp + 0x04);
3417
3418 arm_cache_set_active_sp_value (cache, tdep, unwound_sp + 0x08);
ef273377
CL
3419
3420 return cache;
3421 }
3422
3423 /* Check EXC_RETURN indicator bits (24-31). */
3424 exc_return = (((lr >> 24) & 0xff) == 0xff);
3425 if (exc_return)
55ea94da 3426 {
ef273377
CL
3427 /* Check EXC_RETURN bit SPSEL if Main or Thread (process) stack used. */
3428 bool process_stack_used = ((lr & (1 << 2)) != 0);
3429
3430 if (tdep->have_sec_ext)
3431 {
3432 secure_stack_used = ((lr & (1 << 6)) != 0);
3433 default_callee_register_stacking = ((lr & (1 << 5)) != 0);
3434 exception_domain_is_secure = ((lr & (1 << 0)) == 0);
3435
3436 /* Unwinding from non-secure to secure can trip security
3437 measures. In order to avoid the debugger being
3438 intrusive, rely on the user to configure the requested
3439 mode. */
3440 if (secure_stack_used && !exception_domain_is_secure
3441 && !arm_unwind_secure_frames)
3442 {
3443 warning (_("Non-secure to secure stack unwinding disabled."));
3444
3445 /* Terminate any further stack unwinding by referring to self. */
3446 arm_cache_set_active_sp_value (cache, tdep, sp);
3447 return cache;
3448 }
3449
3450 if (process_stack_used)
3451 {
3452 if (secure_stack_used)
3453 /* Secure thread (process) stack used, use PSP_S as SP. */
3454 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_psp_s_regnum);
3455 else
3456 /* Non-secure thread (process) stack used, use PSP_NS as SP. */
3457 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_psp_ns_regnum);
3458 }
3459 else
3460 {
3461 if (secure_stack_used)
3462 /* Secure main stack used, use MSP_S as SP. */
3463 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_msp_s_regnum);
3464 else
3465 /* Non-secure main stack used, use MSP_NS as SP. */
3466 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_msp_ns_regnum);
3467 }
3468 }
3469 else
3470 {
3471 if (process_stack_used)
3472 /* Thread (process) stack used, use PSP as SP. */
3473 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_psp_regnum);
3474 else
3475 /* Main stack used, use MSP as SP. */
3476 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_msp_regnum);
3477 }
55ea94da 3478 }
ef273377
CL
3479
3480 /* Fetch the SP to use for this frame. */
3481 unwound_sp = arm_cache_get_prev_sp_value (cache, tdep);
3482
2d9cf99d
YR
3483 /* Exception entry context stacking are described in ARMv8-M (section B3.19)
3484 and ARMv7-M (sections B1.5.6 and B1.5.7) Architecture Reference Manuals.
3485
3486 The following figure shows the structure of the stack frame when Security
3487 and Floating-point extensions are present.
3488
3489 SP Offsets
3490 Without With
3491 Callee Regs Callee Regs
3492 (Secure -> Non-Secure)
3493 +-------------------+
3494 0xA8 | | 0xD0
3495 +===================+ --+ <-- Original SP
3496 0xA4 | S31 | 0xCC |
3497 +-------------------+ |
3498 ... | Additional FP context
3499 +-------------------+ |
3500 0x68 | S16 | 0x90 |
3501 +===================+ --+
3502 0x64 | Reserved | 0x8C |
3503 +-------------------+ |
3504 0x60 | FPSCR | 0x88 |
3505 +-------------------+ |
3506 0x5C | S15 | 0x84 | FP context
3507 +-------------------+ |
3508 ... |
3509 +-------------------+ |
3510 0x20 | S0 | 0x48 |
3511 +===================+ --+
3512 0x1C | xPSR | 0x44 |
3513 +-------------------+ |
3514 0x18 | Return address | 0x40 |
3515 +-------------------+ |
3516 0x14 | LR(R14) | 0x3C |
3517 +-------------------+ |
3518 0x10 | R12 | 0x38 | State context
3519 +-------------------+ |
3520 0x0C | R3 | 0x34 |
3521 +-------------------+ |
3522 ... |
3523 +-------------------+ |
3524 0x00 | R0 | 0x28 |
3525 +===================+ --+
3526 | R11 | 0x24 |
3527 +-------------------+ |
3528 ... |
3529 +-------------------+ | Additional state context
3530 | R4 | 0x08 | when transitioning from
3531 +-------------------+ | Secure to Non-Secure
3532 | Reserved | 0x04 |
3533 +-------------------+ |
3534 | Magic signature | 0x00 |
3535 +===================+ --+ <-- New SP */
3536
ef273377
CL
3537 /* With the Security extension, the hardware saves R4..R11 too. */
3538 if (exc_return && tdep->have_sec_ext && secure_stack_used
3539 && (!default_callee_register_stacking || exception_domain_is_secure))
3540 {
3541 /* Read R4..R11 from the integer callee registers. */
3542 cache->saved_regs[4].set_addr (unwound_sp + 0x08);
3543 cache->saved_regs[5].set_addr (unwound_sp + 0x0C);
3544 cache->saved_regs[6].set_addr (unwound_sp + 0x10);
3545 cache->saved_regs[7].set_addr (unwound_sp + 0x14);
3546 cache->saved_regs[8].set_addr (unwound_sp + 0x18);
3547 cache->saved_regs[9].set_addr (unwound_sp + 0x1C);
3548 cache->saved_regs[10].set_addr (unwound_sp + 0x20);
3549 cache->saved_regs[11].set_addr (unwound_sp + 0x24);
3550 sp_r0_offset = 0x28;
55ea94da 3551 }
2ae28aa9
YQ
3552
3553 /* The hardware saves eight 32-bit words, comprising xPSR,
3554 ReturnAddress, LR (R14), R12, R3, R2, R1, R0. See details in
3555 "B1.5.6 Exception entry behavior" in
3556 "ARMv7-M Architecture Reference Manual". */
ef273377 3557 cache->saved_regs[0].set_addr (unwound_sp + sp_r0_offset);
1d2eeb66
YR
3558 cache->saved_regs[1].set_addr (unwound_sp + sp_r0_offset + 0x04);
3559 cache->saved_regs[2].set_addr (unwound_sp + sp_r0_offset + 0x08);
3560 cache->saved_regs[3].set_addr (unwound_sp + sp_r0_offset + 0x0C);
3561 cache->saved_regs[ARM_IP_REGNUM].set_addr (unwound_sp + sp_r0_offset + 0x10);
3562 cache->saved_regs[ARM_LR_REGNUM].set_addr (unwound_sp + sp_r0_offset + 0x14);
3563 cache->saved_regs[ARM_PC_REGNUM].set_addr (unwound_sp + sp_r0_offset + 0x18);
3564 cache->saved_regs[ARM_PS_REGNUM].set_addr (unwound_sp + sp_r0_offset + 0x1C);
2ae28aa9 3565
55ea94da
FH
3566 /* Check EXC_RETURN bit FTYPE if extended stack frame (FPU regs stored)
3567 type used. */
3568 extended_frame_used = ((lr & (1 << 4)) == 0);
3569 if (exc_return && extended_frame_used)
3570 {
3571 int i;
3572 int fpu_regs_stack_offset;
69b46464
YR
3573 ULONGEST fpccr;
3574
3575 /* Read FPCCR register. */
3576 gdb_assert (safe_read_memory_unsigned_integer (FPCCR,
3577 ARM_INT_REGISTER_SIZE,
3578 byte_order, &fpccr));
3579 bool fpccr_ts = bit (fpccr,26);
55ea94da
FH
3580
3581 /* This code does not take into account the lazy stacking, see "Lazy
dda83cd7
SM
3582 context save of FP state", in B1.5.7, also ARM AN298, supported
3583 by Cortex-M4F architecture.
3584 To fully handle this the FPCCR register (Floating-point Context
3585 Control Register) needs to be read out and the bits ASPEN and LSPEN
3586 could be checked to setup correct lazy stacked FP registers.
3587 This register is located at address 0xE000EF34. */
55ea94da
FH
3588
3589 /* Extended stack frame type used. */
ef273377 3590 fpu_regs_stack_offset = unwound_sp + sp_r0_offset + 0x20;
39fc7ff6 3591 for (i = 0; i < 8; i++)
dda83cd7 3592 {
098caef4 3593 cache->saved_regs[ARM_D0_REGNUM + i].set_addr (fpu_regs_stack_offset);
39fc7ff6 3594 fpu_regs_stack_offset += 8;
dda83cd7 3595 }
ef273377
CL
3596 cache->saved_regs[ARM_FPSCR_REGNUM].set_addr (unwound_sp + sp_r0_offset + 0x60);
3597 fpu_regs_stack_offset += 4;
55ea94da 3598
69b46464 3599 if (tdep->have_sec_ext && !default_callee_register_stacking && fpccr_ts)
ef273377
CL
3600 {
3601 /* Handle floating-point callee saved registers. */
2d9cf99d 3602 fpu_regs_stack_offset = unwound_sp + sp_r0_offset + 0x68;
39fc7ff6 3603 for (i = 8; i < 16; i++)
ef273377
CL
3604 {
3605 cache->saved_regs[ARM_D0_REGNUM + i].set_addr (fpu_regs_stack_offset);
39fc7ff6 3606 fpu_regs_stack_offset += 8;
ef273377
CL
3607 }
3608
2d9cf99d
YR
3609 arm_cache_set_active_sp_value (cache, tdep,
3610 unwound_sp + sp_r0_offset + 0xA8);
ef273377
CL
3611 }
3612 else
3613 {
3614 /* Offset 0x64 is reserved. */
2d9cf99d
YR
3615 arm_cache_set_active_sp_value (cache, tdep,
3616 unwound_sp + sp_r0_offset + 0x68);
ef273377 3617 }
55ea94da
FH
3618 }
3619 else
3620 {
3621 /* Standard stack frame type used. */
2d9cf99d
YR
3622 arm_cache_set_active_sp_value (cache, tdep,
3623 unwound_sp + sp_r0_offset + 0x20);
55ea94da
FH
3624 }
3625
2ae28aa9
YQ
3626 /* If bit 9 of the saved xPSR is set, then there is a four-byte
3627 aligner between the top of the 32-byte stack frame and the
3628 previous context's stack pointer. */
1d2eeb66
YR
3629 if (safe_read_memory_integer (unwound_sp + sp_r0_offset + 0x1C, 4,
3630 byte_order, &xpsr)
2ae28aa9 3631 && (xpsr & (1 << 9)) != 0)
ae7e2f45
CL
3632 arm_cache_set_active_sp_value (cache, tdep,
3633 arm_cache_get_prev_sp_value (cache, tdep) + 4);
2ae28aa9
YQ
3634
3635 return cache;
3636}
3637
3638/* Implementation of function hook 'this_id' in
3639 'struct frame_uwnind'. */
3640
3641static void
3642arm_m_exception_this_id (struct frame_info *this_frame,
3643 void **this_cache,
3644 struct frame_id *this_id)
3645{
3646 struct arm_prologue_cache *cache;
3647
3648 if (*this_cache == NULL)
3649 *this_cache = arm_m_exception_cache (this_frame);
9a3c8263 3650 cache = (struct arm_prologue_cache *) *this_cache;
2ae28aa9
YQ
3651
3652 /* Our frame ID for a stub frame is the current SP and LR. */
ae7e2f45
CL
3653 arm_gdbarch_tdep *tdep
3654 = (arm_gdbarch_tdep *) gdbarch_tdep (get_frame_arch (this_frame));
3655 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep),
2ae28aa9
YQ
3656 get_frame_pc (this_frame));
3657}
3658
3659/* Implementation of function hook 'prev_register' in
3660 'struct frame_uwnind'. */
3661
3662static struct value *
3663arm_m_exception_prev_register (struct frame_info *this_frame,
3664 void **this_cache,
3665 int prev_regnum)
3666{
2ae28aa9 3667 struct arm_prologue_cache *cache;
ef273377 3668 CORE_ADDR sp_value;
2ae28aa9
YQ
3669
3670 if (*this_cache == NULL)
3671 *this_cache = arm_m_exception_cache (this_frame);
9a3c8263 3672 cache = (struct arm_prologue_cache *) *this_cache;
2ae28aa9
YQ
3673
3674 /* The value was already reconstructed into PREV_SP. */
ae7e2f45
CL
3675 arm_gdbarch_tdep *tdep
3676 = (arm_gdbarch_tdep *) gdbarch_tdep (get_frame_arch (this_frame));
2ae28aa9
YQ
3677 if (prev_regnum == ARM_SP_REGNUM)
3678 return frame_unwind_got_constant (this_frame, prev_regnum,
ae7e2f45 3679 arm_cache_get_prev_sp_value (cache, tdep));
2ae28aa9 3680
8c9ae6df
YR
3681 /* If we are asked to unwind the PC, strip the saved T bit. */
3682 if (prev_regnum == ARM_PC_REGNUM)
3683 {
3684 struct value *value = trad_frame_get_prev_register (this_frame,
3685 cache->saved_regs,
3686 prev_regnum);
3687 CORE_ADDR pc = value_as_address (value);
3688 return frame_unwind_got_constant (this_frame, prev_regnum,
3689 UNMAKE_THUMB_ADDR (pc));
3690 }
3691
ef273377
CL
3692 /* The value might be one of the alternative SP, if so, use the
3693 value already constructed. */
d65edaa0 3694 if (arm_is_alternative_sp_register (tdep, prev_regnum))
ef273377
CL
3695 {
3696 sp_value = arm_cache_get_sp_register (cache, tdep, prev_regnum);
3697 return frame_unwind_got_constant (this_frame, prev_regnum, sp_value);
3698 }
3699
8c9ae6df
YR
3700 /* If we are asked to unwind the xPSR, set T bit if PC is in thumb mode.
3701 LR register is unreliable as it contains FNC_RETURN or EXC_RETURN
3702 pattern. */
3703 if (prev_regnum == ARM_PS_REGNUM)
3704 {
3705 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3706 struct value *value = trad_frame_get_prev_register (this_frame,
3707 cache->saved_regs,
3708 ARM_PC_REGNUM);
3709 CORE_ADDR pc = value_as_address (value);
3710 value = trad_frame_get_prev_register (this_frame, cache->saved_regs,
3711 ARM_PS_REGNUM);
3712 ULONGEST xpsr = value_as_long (value);
3713
3714 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
3715 xpsr = reconstruct_t_bit (gdbarch, pc, xpsr);
3716 return frame_unwind_got_constant (this_frame, ARM_PS_REGNUM, xpsr);
3717 }
3718
2ae28aa9
YQ
3719 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
3720 prev_regnum);
3721}
3722
3723/* Implementation of function hook 'sniffer' in
3724 'struct frame_uwnind'. */
3725
3726static int
3727arm_m_exception_unwind_sniffer (const struct frame_unwind *self,
3728 struct frame_info *this_frame,
3729 void **this_prologue_cache)
3730{
ef273377 3731 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2ae28aa9
YQ
3732 CORE_ADDR this_pc = get_frame_pc (this_frame);
3733
3734 /* No need to check is_m; this sniffer is only registered for
3735 M-profile architectures. */
3736
ca90e760 3737 /* Check if exception frame returns to a magic PC value. */
ef273377 3738 return arm_m_addr_is_magic (gdbarch, this_pc);
2ae28aa9
YQ
3739}
3740
3741/* Frame unwinder for M-profile exceptions. */
3742
3743struct frame_unwind arm_m_exception_unwind =
3744{
a154d838 3745 "arm m exception",
2ae28aa9
YQ
3746 SIGTRAMP_FRAME,
3747 default_frame_unwind_stop_reason,
3748 arm_m_exception_this_id,
3749 arm_m_exception_prev_register,
3750 NULL,
3751 arm_m_exception_unwind_sniffer
3752};
3753
24de872b 3754static CORE_ADDR
a262aec2 3755arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
24de872b
DJ
3756{
3757 struct arm_prologue_cache *cache;
3758
eb5492fa 3759 if (*this_cache == NULL)
a262aec2 3760 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 3761 cache = (struct arm_prologue_cache *) *this_cache;
eb5492fa 3762
ae7e2f45
CL
3763 arm_gdbarch_tdep *tdep
3764 = (arm_gdbarch_tdep *) gdbarch_tdep (get_frame_arch (this_frame));
3765 return arm_cache_get_prev_sp_value (cache, tdep) - cache->framesize;
24de872b
DJ
3766}
3767
eb5492fa
DJ
3768struct frame_base arm_normal_base = {
3769 &arm_prologue_unwind,
3770 arm_normal_frame_base,
3771 arm_normal_frame_base,
3772 arm_normal_frame_base
3773};
3774
b39cc962
DJ
3775static struct value *
3776arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
3777 int regnum)
3778{
24568a2c 3779 struct gdbarch * gdbarch = get_frame_arch (this_frame);
a01567f4 3780 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
8c9ae6df
YR
3781 CORE_ADDR lr;
3782 ULONGEST cpsr;
b39cc962
DJ
3783
3784 switch (regnum)
3785 {
3786 case ARM_PC_REGNUM:
3787 /* The PC is normally copied from the return column, which
3788 describes saves of LR. However, that version may have an
3789 extra bit set to indicate Thumb state. The bit is not
3790 part of the PC. */
a01567f4
LM
3791
3792 /* Record in the frame whether the return address was signed. */
3793 if (tdep->have_pacbti)
3794 {
3795 CORE_ADDR ra_auth_code
3796 = frame_unwind_register_unsigned (this_frame,
3797 tdep->pacbti_pseudo_base);
3798
3799 if (ra_auth_code != 0)
3800 set_frame_previous_pc_masked (this_frame);
3801 }
3802
b39cc962
DJ
3803 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3804 return frame_unwind_got_constant (this_frame, regnum,
24568a2c 3805 arm_addr_bits_remove (gdbarch, lr));
b39cc962
DJ
3806
3807 case ARM_PS_REGNUM:
3808 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
ca38c58e 3809 cpsr = get_frame_register_unsigned (this_frame, regnum);
b39cc962 3810 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
8c9ae6df 3811 cpsr = reconstruct_t_bit (gdbarch, lr, cpsr);
ca38c58e 3812 return frame_unwind_got_constant (this_frame, regnum, cpsr);
b39cc962
DJ
3813
3814 default:
3815 internal_error (__FILE__, __LINE__,
3816 _("Unexpected register %d"), regnum);
3817 }
3818}
3819
c9cf6e20 3820/* Implement the stack_frame_destroyed_p gdbarch method. */
4024ca99
UW
3821
3822static int
c9cf6e20 3823thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
4024ca99
UW
3824{
3825 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3826 unsigned int insn, insn2;
3827 int found_return = 0, found_stack_adjust = 0;
3828 CORE_ADDR func_start, func_end;
3829 CORE_ADDR scan_pc;
3830 gdb_byte buf[4];
3831
3832 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3833 return 0;
3834
3835 /* The epilogue is a sequence of instructions along the following lines:
3836
3837 - add stack frame size to SP or FP
3838 - [if frame pointer used] restore SP from FP
3839 - restore registers from SP [may include PC]
3840 - a return-type instruction [if PC wasn't already restored]
3841
3842 In a first pass, we scan forward from the current PC and verify the
3843 instructions we find as compatible with this sequence, ending in a
3844 return instruction.
3845
3846 However, this is not sufficient to distinguish indirect function calls
3847 within a function from indirect tail calls in the epilogue in some cases.
3848 Therefore, if we didn't already find any SP-changing instruction during
3849 forward scan, we add a backward scanning heuristic to ensure we actually
3850 are in the epilogue. */
3851
3852 scan_pc = pc;
3853 while (scan_pc < func_end && !found_return)
3854 {
3855 if (target_read_memory (scan_pc, buf, 2))
3856 break;
3857
3858 scan_pc += 2;
3859 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3860
3861 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
3862 found_return = 1;
3863 else if (insn == 0x46f7) /* mov pc, lr */
3864 found_return = 1;
540314bd 3865 else if (thumb_instruction_restores_sp (insn))
4024ca99 3866 {
b7576e5c 3867 if ((insn & 0xff00) == 0xbd00) /* pop <registers, PC> */
4024ca99
UW
3868 found_return = 1;
3869 }
db24da6d 3870 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instruction */
4024ca99
UW
3871 {
3872 if (target_read_memory (scan_pc, buf, 2))
3873 break;
3874
3875 scan_pc += 2;
3876 insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3877
3878 if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3879 {
4024ca99
UW
3880 if (insn2 & 0x8000) /* <registers> include PC. */
3881 found_return = 1;
3882 }
3883 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3884 && (insn2 & 0x0fff) == 0x0b04)
3885 {
4024ca99
UW
3886 if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC. */
3887 found_return = 1;
3888 }
3889 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3890 && (insn2 & 0x0e00) == 0x0a00)
6b65d1b6 3891 ;
4024ca99
UW
3892 else
3893 break;
3894 }
3895 else
3896 break;
3897 }
3898
3899 if (!found_return)
3900 return 0;
3901
3902 /* Since any instruction in the epilogue sequence, with the possible
3903 exception of return itself, updates the stack pointer, we need to
3904 scan backwards for at most one instruction. Try either a 16-bit or
3905 a 32-bit instruction. This is just a heuristic, so we do not worry
0963b4bd 3906 too much about false positives. */
4024ca99 3907
6b65d1b6
YQ
3908 if (pc - 4 < func_start)
3909 return 0;
3910 if (target_read_memory (pc - 4, buf, 4))
3911 return 0;
4024ca99 3912
6b65d1b6
YQ
3913 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3914 insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);
3915
3916 if (thumb_instruction_restores_sp (insn2))
3917 found_stack_adjust = 1;
3918 else if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3919 found_stack_adjust = 1;
3920 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3921 && (insn2 & 0x0fff) == 0x0b04)
3922 found_stack_adjust = 1;
3923 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3924 && (insn2 & 0x0e00) == 0x0a00)
3925 found_stack_adjust = 1;
4024ca99
UW
3926
3927 return found_stack_adjust;
3928}
3929
4024ca99 3930static int
c58b006a 3931arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch, CORE_ADDR pc)
4024ca99
UW
3932{
3933 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3934 unsigned int insn;
f303bc3e 3935 int found_return;
4024ca99
UW
3936 CORE_ADDR func_start, func_end;
3937
4024ca99
UW
3938 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3939 return 0;
3940
3941 /* We are in the epilogue if the previous instruction was a stack
3942 adjustment and the next instruction is a possible return (bx, mov
3943 pc, or pop). We could have to scan backwards to find the stack
3944 adjustment, or forwards to find the return, but this is a decent
3945 approximation. First scan forwards. */
3946
3947 found_return = 0;
3948 insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
3949 if (bits (insn, 28, 31) != INST_NV)
3950 {
3951 if ((insn & 0x0ffffff0) == 0x012fff10)
3952 /* BX. */
3953 found_return = 1;
3954 else if ((insn & 0x0ffffff0) == 0x01a0f000)
3955 /* MOV PC. */
3956 found_return = 1;
3957 else if ((insn & 0x0fff0000) == 0x08bd0000
3958 && (insn & 0x0000c000) != 0)
3959 /* POP (LDMIA), including PC or LR. */
3960 found_return = 1;
3961 }
3962
3963 if (!found_return)
3964 return 0;
3965
3966 /* Scan backwards. This is just a heuristic, so do not worry about
3967 false positives from mode changes. */
3968
3969 if (pc < func_start + 4)
3970 return 0;
3971
3972 insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
f303bc3e 3973 if (arm_instruction_restores_sp (insn))
4024ca99
UW
3974 return 1;
3975
3976 return 0;
3977}
3978
c58b006a
YQ
3979/* Implement the stack_frame_destroyed_p gdbarch method. */
3980
3981static int
3982arm_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3983{
3984 if (arm_pc_is_thumb (gdbarch, pc))
3985 return thumb_stack_frame_destroyed_p (gdbarch, pc);
3986 else
3987 return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
3988}
4024ca99 3989
2dd604e7
RE
3990/* When arguments must be pushed onto the stack, they go on in reverse
3991 order. The code below implements a FILO (stack) to do this. */
3992
0fecb1a7 3993struct arm_stack_item
2dd604e7
RE
3994{
3995 int len;
0fecb1a7 3996 struct arm_stack_item *prev;
7c543f7b 3997 gdb_byte *data;
2dd604e7
RE
3998};
3999
0fecb1a7
TT
4000static struct arm_stack_item *
4001push_stack_item (struct arm_stack_item *prev, const gdb_byte *contents,
4002 int len)
2dd604e7 4003{
0fecb1a7
TT
4004 struct arm_stack_item *si;
4005 si = XNEW (struct arm_stack_item);
7c543f7b 4006 si->data = (gdb_byte *) xmalloc (len);
2dd604e7
RE
4007 si->len = len;
4008 si->prev = prev;
4009 memcpy (si->data, contents, len);
4010 return si;
4011}
4012
0fecb1a7
TT
4013static struct arm_stack_item *
4014pop_stack_item (struct arm_stack_item *si)
2dd604e7 4015{
0fecb1a7 4016 struct arm_stack_item *dead = si;
2dd604e7
RE
4017 si = si->prev;
4018 xfree (dead->data);
4019 xfree (dead);
4020 return si;
4021}
4022
030197b4
AB
4023/* Implement the gdbarch type alignment method, overrides the generic
4024 alignment algorithm for anything that is arm specific. */
2af48f68 4025
030197b4
AB
4026static ULONGEST
4027arm_type_align (gdbarch *gdbarch, struct type *t)
2af48f68 4028{
2af48f68 4029 t = check_typedef (t);
bd63c870 4030 if (t->code () == TYPE_CODE_ARRAY && t->is_vector ())
2af48f68 4031 {
030197b4
AB
4032 /* Use the natural alignment for vector types (the same for
4033 scalar type), but the maximum alignment is 64-bit. */
4034 if (TYPE_LENGTH (t) > 8)
4035 return 8;
c4312b19 4036 else
030197b4 4037 return TYPE_LENGTH (t);
2af48f68 4038 }
030197b4
AB
4039
4040 /* Allow the common code to calculate the alignment. */
4041 return 0;
2af48f68
PB
4042}
4043
90445bd3
DJ
4044/* Possible base types for a candidate for passing and returning in
4045 VFP registers. */
4046
4047enum arm_vfp_cprc_base_type
4048{
4049 VFP_CPRC_UNKNOWN,
4050 VFP_CPRC_SINGLE,
4051 VFP_CPRC_DOUBLE,
4052 VFP_CPRC_VEC64,
4053 VFP_CPRC_VEC128
4054};
4055
4056/* The length of one element of base type B. */
4057
4058static unsigned
4059arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
4060{
4061 switch (b)
4062 {
4063 case VFP_CPRC_SINGLE:
4064 return 4;
4065 case VFP_CPRC_DOUBLE:
4066 return 8;
4067 case VFP_CPRC_VEC64:
4068 return 8;
4069 case VFP_CPRC_VEC128:
4070 return 16;
4071 default:
4072 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
4073 (int) b);
4074 }
4075}
4076
4077/* The character ('s', 'd' or 'q') for the type of VFP register used
4078 for passing base type B. */
4079
4080static int
4081arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
4082{
4083 switch (b)
4084 {
4085 case VFP_CPRC_SINGLE:
4086 return 's';
4087 case VFP_CPRC_DOUBLE:
4088 return 'd';
4089 case VFP_CPRC_VEC64:
4090 return 'd';
4091 case VFP_CPRC_VEC128:
4092 return 'q';
4093 default:
4094 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
4095 (int) b);
4096 }
4097}
4098
4099/* Determine whether T may be part of a candidate for passing and
4100 returning in VFP registers, ignoring the limit on the total number
4101 of components. If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
4102 classification of the first valid component found; if it is not
4103 VFP_CPRC_UNKNOWN, all components must have the same classification
4104 as *BASE_TYPE. If it is found that T contains a type not permitted
4105 for passing and returning in VFP registers, a type differently
4106 classified from *BASE_TYPE, or two types differently classified
4107 from each other, return -1, otherwise return the total number of
4108 base-type elements found (possibly 0 in an empty structure or
817e0957
YQ
4109 array). Vector types are not currently supported, matching the
4110 generic AAPCS support. */
90445bd3
DJ
4111
4112static int
4113arm_vfp_cprc_sub_candidate (struct type *t,
4114 enum arm_vfp_cprc_base_type *base_type)
4115{
4116 t = check_typedef (t);
78134374 4117 switch (t->code ())
90445bd3
DJ
4118 {
4119 case TYPE_CODE_FLT:
4120 switch (TYPE_LENGTH (t))
4121 {
4122 case 4:
4123 if (*base_type == VFP_CPRC_UNKNOWN)
4124 *base_type = VFP_CPRC_SINGLE;
4125 else if (*base_type != VFP_CPRC_SINGLE)
4126 return -1;
4127 return 1;
4128
4129 case 8:
4130 if (*base_type == VFP_CPRC_UNKNOWN)
4131 *base_type = VFP_CPRC_DOUBLE;
4132 else if (*base_type != VFP_CPRC_DOUBLE)
4133 return -1;
4134 return 1;
4135
4136 default:
4137 return -1;
4138 }
4139 break;
4140
817e0957
YQ
4141 case TYPE_CODE_COMPLEX:
4142 /* Arguments of complex T where T is one of the types float or
4143 double get treated as if they are implemented as:
4144
4145 struct complexT
4146 {
4147 T real;
4148 T imag;
5f52445b
YQ
4149 };
4150
4151 */
817e0957
YQ
4152 switch (TYPE_LENGTH (t))
4153 {
4154 case 8:
4155 if (*base_type == VFP_CPRC_UNKNOWN)
4156 *base_type = VFP_CPRC_SINGLE;
4157 else if (*base_type != VFP_CPRC_SINGLE)
4158 return -1;
4159 return 2;
4160
4161 case 16:
4162 if (*base_type == VFP_CPRC_UNKNOWN)
4163 *base_type = VFP_CPRC_DOUBLE;
4164 else if (*base_type != VFP_CPRC_DOUBLE)
4165 return -1;
4166 return 2;
4167
4168 default:
4169 return -1;
4170 }
4171 break;
4172
90445bd3
DJ
4173 case TYPE_CODE_ARRAY:
4174 {
bd63c870 4175 if (t->is_vector ())
90445bd3 4176 {
c4312b19
YQ
4177 /* A 64-bit or 128-bit containerized vector type are VFP
4178 CPRCs. */
4179 switch (TYPE_LENGTH (t))
4180 {
4181 case 8:
4182 if (*base_type == VFP_CPRC_UNKNOWN)
4183 *base_type = VFP_CPRC_VEC64;
4184 return 1;
4185 case 16:
4186 if (*base_type == VFP_CPRC_UNKNOWN)
4187 *base_type = VFP_CPRC_VEC128;
4188 return 1;
4189 default:
4190 return -1;
4191 }
4192 }
4193 else
4194 {
4195 int count;
4196 unsigned unitlen;
4197
4198 count = arm_vfp_cprc_sub_candidate (TYPE_TARGET_TYPE (t),
4199 base_type);
4200 if (count == -1)
4201 return -1;
4202 if (TYPE_LENGTH (t) == 0)
4203 {
4204 gdb_assert (count == 0);
4205 return 0;
4206 }
4207 else if (count == 0)
4208 return -1;
4209 unitlen = arm_vfp_cprc_unit_length (*base_type);
4210 gdb_assert ((TYPE_LENGTH (t) % unitlen) == 0);
4211 return TYPE_LENGTH (t) / unitlen;
90445bd3 4212 }
90445bd3
DJ
4213 }
4214 break;
4215
4216 case TYPE_CODE_STRUCT:
4217 {
4218 int count = 0;
4219 unsigned unitlen;
4220 int i;
1f704f76 4221 for (i = 0; i < t->num_fields (); i++)
90445bd3 4222 {
1040b979
YQ
4223 int sub_count = 0;
4224
ceacbf6e 4225 if (!field_is_static (&t->field (i)))
940da03e 4226 sub_count = arm_vfp_cprc_sub_candidate (t->field (i).type (),
1040b979 4227 base_type);
90445bd3
DJ
4228 if (sub_count == -1)
4229 return -1;
4230 count += sub_count;
4231 }
4232 if (TYPE_LENGTH (t) == 0)
4233 {
4234 gdb_assert (count == 0);
4235 return 0;
4236 }
4237 else if (count == 0)
4238 return -1;
4239 unitlen = arm_vfp_cprc_unit_length (*base_type);
4240 if (TYPE_LENGTH (t) != unitlen * count)
4241 return -1;
4242 return count;
4243 }
4244
4245 case TYPE_CODE_UNION:
4246 {
4247 int count = 0;
4248 unsigned unitlen;
4249 int i;
1f704f76 4250 for (i = 0; i < t->num_fields (); i++)
90445bd3 4251 {
940da03e 4252 int sub_count = arm_vfp_cprc_sub_candidate (t->field (i).type (),
90445bd3
DJ
4253 base_type);
4254 if (sub_count == -1)
4255 return -1;
4256 count = (count > sub_count ? count : sub_count);
4257 }
4258 if (TYPE_LENGTH (t) == 0)
4259 {
4260 gdb_assert (count == 0);
4261 return 0;
4262 }
4263 else if (count == 0)
4264 return -1;
4265 unitlen = arm_vfp_cprc_unit_length (*base_type);
4266 if (TYPE_LENGTH (t) != unitlen * count)
4267 return -1;
4268 return count;
4269 }
4270
4271 default:
4272 break;
4273 }
4274
4275 return -1;
4276}
4277
4278/* Determine whether T is a VFP co-processor register candidate (CPRC)
4279 if passed to or returned from a non-variadic function with the VFP
4280 ABI in effect. Return 1 if it is, 0 otherwise. If it is, set
4281 *BASE_TYPE to the base type for T and *COUNT to the number of
4282 elements of that base type before returning. */
4283
4284static int
4285arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
4286 int *count)
4287{
4288 enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
4289 int c = arm_vfp_cprc_sub_candidate (t, &b);
4290 if (c <= 0 || c > 4)
4291 return 0;
4292 *base_type = b;
4293 *count = c;
4294 return 1;
4295}
4296
4297/* Return 1 if the VFP ABI should be used for passing arguments to and
4298 returning values from a function of type FUNC_TYPE, 0
4299 otherwise. */
4300
4301static int
4302arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
4303{
345bd07c
SM
4304 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
4305
90445bd3
DJ
4306 /* Variadic functions always use the base ABI. Assume that functions
4307 without debug info are not variadic. */
a409645d 4308 if (func_type && check_typedef (func_type)->has_varargs ())
90445bd3 4309 return 0;
345bd07c 4310
90445bd3
DJ
4311 /* The VFP ABI is only supported as a variant of AAPCS. */
4312 if (tdep->arm_abi != ARM_ABI_AAPCS)
4313 return 0;
345bd07c
SM
4314
4315 return tdep->fp_model == ARM_FLOAT_VFP;
90445bd3
DJ
4316}
4317
4318/* We currently only support passing parameters in integer registers, which
4319 conforms with GCC's default model, and VFP argument passing following
4320 the VFP variant of AAPCS. Several other variants exist and
2dd604e7
RE
4321 we should probably support some of them based on the selected ABI. */
4322
4323static CORE_ADDR
7d9b040b 4324arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6a65450a 4325 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
cf84fa6b
AH
4326 struct value **args, CORE_ADDR sp,
4327 function_call_return_method return_method,
6a65450a 4328 CORE_ADDR struct_addr)
2dd604e7 4329{
e17a4113 4330 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2dd604e7
RE
4331 int argnum;
4332 int argreg;
4333 int nstack;
0fecb1a7 4334 struct arm_stack_item *si = NULL;
90445bd3
DJ
4335 int use_vfp_abi;
4336 struct type *ftype;
4337 unsigned vfp_regs_free = (1 << 16) - 1;
345bd07c 4338 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
90445bd3
DJ
4339
4340 /* Determine the type of this function and whether the VFP ABI
4341 applies. */
4342 ftype = check_typedef (value_type (function));
78134374 4343 if (ftype->code () == TYPE_CODE_PTR)
90445bd3
DJ
4344 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
4345 use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
2dd604e7 4346
6a65450a
AC
4347 /* Set the return address. For the ARM, the return breakpoint is
4348 always at BP_ADDR. */
9779414d 4349 if (arm_pc_is_thumb (gdbarch, bp_addr))
9dca5578 4350 bp_addr |= 1;
6a65450a 4351 regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
2dd604e7
RE
4352
4353 /* Walk through the list of args and determine how large a temporary
4354 stack is required. Need to take care here as structs may be
7a9dd1b2 4355 passed on the stack, and we have to push them. */
2dd604e7
RE
4356 nstack = 0;
4357
4358 argreg = ARM_A1_REGNUM;
4359 nstack = 0;
4360
2dd604e7
RE
4361 /* The struct_return pointer occupies the first parameter
4362 passing register. */
cf84fa6b 4363 if (return_method == return_method_struct)
2dd604e7 4364 {
7cb6d92a
SM
4365 arm_debug_printf ("struct return in %s = %s",
4366 gdbarch_register_name (gdbarch, argreg),
4367 paddress (gdbarch, struct_addr));
4368
2dd604e7
RE
4369 regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
4370 argreg++;
4371 }
4372
4373 for (argnum = 0; argnum < nargs; argnum++)
4374 {
4375 int len;
4376 struct type *arg_type;
4377 struct type *target_type;
4378 enum type_code typecode;
8c6363cf 4379 const bfd_byte *val;
2af48f68 4380 int align;
90445bd3
DJ
4381 enum arm_vfp_cprc_base_type vfp_base_type;
4382 int vfp_base_count;
4383 int may_use_core_reg = 1;
2dd604e7 4384
df407dfe 4385 arg_type = check_typedef (value_type (args[argnum]));
2dd604e7
RE
4386 len = TYPE_LENGTH (arg_type);
4387 target_type = TYPE_TARGET_TYPE (arg_type);
78134374 4388 typecode = arg_type->code ();
50888e42 4389 val = value_contents (args[argnum]).data ();
2dd604e7 4390
030197b4 4391 align = type_align (arg_type);
2af48f68 4392 /* Round alignment up to a whole number of words. */
f0452268
AH
4393 align = (align + ARM_INT_REGISTER_SIZE - 1)
4394 & ~(ARM_INT_REGISTER_SIZE - 1);
2af48f68 4395 /* Different ABIs have different maximum alignments. */
345bd07c 4396 if (tdep->arm_abi == ARM_ABI_APCS)
2af48f68
PB
4397 {
4398 /* The APCS ABI only requires word alignment. */
f0452268 4399 align = ARM_INT_REGISTER_SIZE;
2af48f68
PB
4400 }
4401 else
4402 {
4403 /* The AAPCS requires at most doubleword alignment. */
f0452268
AH
4404 if (align > ARM_INT_REGISTER_SIZE * 2)
4405 align = ARM_INT_REGISTER_SIZE * 2;
2af48f68
PB
4406 }
4407
90445bd3
DJ
4408 if (use_vfp_abi
4409 && arm_vfp_call_candidate (arg_type, &vfp_base_type,
4410 &vfp_base_count))
4411 {
4412 int regno;
4413 int unit_length;
4414 int shift;
4415 unsigned mask;
4416
4417 /* Because this is a CPRC it cannot go in a core register or
4418 cause a core register to be skipped for alignment.
4419 Either it goes in VFP registers and the rest of this loop
4420 iteration is skipped for this argument, or it goes on the
4421 stack (and the stack alignment code is correct for this
4422 case). */
4423 may_use_core_reg = 0;
4424
4425 unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
4426 shift = unit_length / 4;
4427 mask = (1 << (shift * vfp_base_count)) - 1;
4428 for (regno = 0; regno < 16; regno += shift)
4429 if (((vfp_regs_free >> regno) & mask) == mask)
4430 break;
4431
4432 if (regno < 16)
4433 {
4434 int reg_char;
4435 int reg_scaled;
4436 int i;
4437
4438 vfp_regs_free &= ~(mask << regno);
4439 reg_scaled = regno / shift;
4440 reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
4441 for (i = 0; i < vfp_base_count; i++)
4442 {
4443 char name_buf[4];
4444 int regnum;
58d6951d
DJ
4445 if (reg_char == 'q')
4446 arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
90445bd3 4447 val + i * unit_length);
58d6951d
DJ
4448 else
4449 {
8c042590
PM
4450 xsnprintf (name_buf, sizeof (name_buf), "%c%d",
4451 reg_char, reg_scaled + i);
58d6951d
DJ
4452 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
4453 strlen (name_buf));
b66f5587 4454 regcache->cooked_write (regnum, val + i * unit_length);
58d6951d 4455 }
90445bd3
DJ
4456 }
4457 continue;
4458 }
4459 else
4460 {
4461 /* This CPRC could not go in VFP registers, so all VFP
4462 registers are now marked as used. */
4463 vfp_regs_free = 0;
4464 }
4465 }
4466
85102364 4467 /* Push stack padding for doubleword alignment. */
2af48f68
PB
4468 if (nstack & (align - 1))
4469 {
f0452268
AH
4470 si = push_stack_item (si, val, ARM_INT_REGISTER_SIZE);
4471 nstack += ARM_INT_REGISTER_SIZE;
2af48f68
PB
4472 }
4473
4474 /* Doubleword aligned quantities must go in even register pairs. */
90445bd3
DJ
4475 if (may_use_core_reg
4476 && argreg <= ARM_LAST_ARG_REGNUM
f0452268 4477 && align > ARM_INT_REGISTER_SIZE
2af48f68
PB
4478 && argreg & 1)
4479 argreg++;
4480
2dd604e7
RE
4481 /* If the argument is a pointer to a function, and it is a
4482 Thumb function, create a LOCAL copy of the value and set
4483 the THUMB bit in it. */
4484 if (TYPE_CODE_PTR == typecode
4485 && target_type != NULL
78134374 4486 && TYPE_CODE_FUNC == check_typedef (target_type)->code ())
2dd604e7 4487 {
e17a4113 4488 CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
9779414d 4489 if (arm_pc_is_thumb (gdbarch, regval))
2dd604e7 4490 {
224c3ddb 4491 bfd_byte *copy = (bfd_byte *) alloca (len);
8c6363cf 4492 store_unsigned_integer (copy, len, byte_order,
e17a4113 4493 MAKE_THUMB_ADDR (regval));
8c6363cf 4494 val = copy;
2dd604e7
RE
4495 }
4496 }
4497
4498 /* Copy the argument to general registers or the stack in
4499 register-sized pieces. Large arguments are split between
4500 registers and stack. */
4501 while (len > 0)
4502 {
f0452268
AH
4503 int partial_len = len < ARM_INT_REGISTER_SIZE
4504 ? len : ARM_INT_REGISTER_SIZE;
ef9bd0b8
YQ
4505 CORE_ADDR regval
4506 = extract_unsigned_integer (val, partial_len, byte_order);
2dd604e7 4507
90445bd3 4508 if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
2dd604e7
RE
4509 {
4510 /* The argument is being passed in a general purpose
4511 register. */
e17a4113 4512 if (byte_order == BFD_ENDIAN_BIG)
f0452268 4513 regval <<= (ARM_INT_REGISTER_SIZE - partial_len) * 8;
7cb6d92a
SM
4514
4515 arm_debug_printf ("arg %d in %s = 0x%s", argnum,
4516 gdbarch_register_name (gdbarch, argreg),
4517 phex (regval, ARM_INT_REGISTER_SIZE));
4518
2dd604e7
RE
4519 regcache_cooked_write_unsigned (regcache, argreg, regval);
4520 argreg++;
4521 }
4522 else
4523 {
f0452268 4524 gdb_byte buf[ARM_INT_REGISTER_SIZE];
ef9bd0b8
YQ
4525
4526 memset (buf, 0, sizeof (buf));
4527 store_unsigned_integer (buf, partial_len, byte_order, regval);
4528
2dd604e7 4529 /* Push the arguments onto the stack. */
7cb6d92a 4530 arm_debug_printf ("arg %d @ sp + %d", argnum, nstack);
f0452268
AH
4531 si = push_stack_item (si, buf, ARM_INT_REGISTER_SIZE);
4532 nstack += ARM_INT_REGISTER_SIZE;
2dd604e7
RE
4533 }
4534
4535 len -= partial_len;
4536 val += partial_len;
4537 }
4538 }
4539 /* If we have an odd number of words to push, then decrement the stack
4540 by one word now, so first stack argument will be dword aligned. */
4541 if (nstack & 4)
4542 sp -= 4;
4543
4544 while (si)
4545 {
4546 sp -= si->len;
4547 write_memory (sp, si->data, si->len);
4548 si = pop_stack_item (si);
4549 }
4550
4551 /* Finally, update teh SP register. */
4552 regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
4553
4554 return sp;
4555}
4556
f53f0d0b
PB
4557
4558/* Always align the frame to an 8-byte boundary. This is required on
4559 some platforms and harmless on the rest. */
4560
4561static CORE_ADDR
4562arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
4563{
4564 /* Align the stack to eight bytes. */
4565 return sp & ~ (CORE_ADDR) 7;
4566}
4567
c906108c 4568static void
12b27276 4569print_fpu_flags (struct ui_file *file, int flags)
c906108c 4570{
c5aa993b 4571 if (flags & (1 << 0))
0426ad51 4572 gdb_puts ("IVO ", file);
c5aa993b 4573 if (flags & (1 << 1))
0426ad51 4574 gdb_puts ("DVZ ", file);
c5aa993b 4575 if (flags & (1 << 2))
0426ad51 4576 gdb_puts ("OFL ", file);
c5aa993b 4577 if (flags & (1 << 3))
0426ad51 4578 gdb_puts ("UFL ", file);
c5aa993b 4579 if (flags & (1 << 4))
0426ad51 4580 gdb_puts ("INX ", file);
a11ac3b3 4581 gdb_putc ('\n', file);
c906108c
SS
4582}
4583
5e74b15c
RE
4584/* Print interesting information about the floating point processor
4585 (if present) or emulator. */
34e8f22d 4586static void
d855c300 4587arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
23e3a7ac 4588 struct frame_info *frame, const char *args)
c906108c 4589{
9c9acae0 4590 unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
c5aa993b
JM
4591 int type;
4592
4593 type = (status >> 24) & 127;
edefbb7c 4594 if (status & (1 << 31))
6cb06a8c 4595 gdb_printf (file, _("Hardware FPU type %d\n"), type);
edefbb7c 4596 else
6cb06a8c 4597 gdb_printf (file, _("Software FPU type %d\n"), type);
edefbb7c 4598 /* i18n: [floating point unit] mask */
0426ad51 4599 gdb_puts (_("mask: "), file);
12b27276 4600 print_fpu_flags (file, status >> 16);
edefbb7c 4601 /* i18n: [floating point unit] flags */
0426ad51 4602 gdb_puts (_("flags: "), file);
12b27276 4603 print_fpu_flags (file, status);
c906108c
SS
4604}
4605
27067745
UW
4606/* Construct the ARM extended floating point type. */
4607static struct type *
4608arm_ext_type (struct gdbarch *gdbarch)
4609{
345bd07c 4610 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
27067745
UW
4611
4612 if (!tdep->arm_ext_type)
4613 tdep->arm_ext_type
e9bb382b 4614 = arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
27067745
UW
4615 floatformats_arm_ext);
4616
4617 return tdep->arm_ext_type;
4618}
4619
58d6951d
DJ
4620static struct type *
4621arm_neon_double_type (struct gdbarch *gdbarch)
4622{
345bd07c 4623 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
58d6951d
DJ
4624
4625 if (tdep->neon_double_type == NULL)
4626 {
4627 struct type *t, *elem;
4628
4629 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
4630 TYPE_CODE_UNION);
4631 elem = builtin_type (gdbarch)->builtin_uint8;
4632 append_composite_type_field (t, "u8", init_vector_type (elem, 8));
4633 elem = builtin_type (gdbarch)->builtin_uint16;
4634 append_composite_type_field (t, "u16", init_vector_type (elem, 4));
4635 elem = builtin_type (gdbarch)->builtin_uint32;
4636 append_composite_type_field (t, "u32", init_vector_type (elem, 2));
4637 elem = builtin_type (gdbarch)->builtin_uint64;
4638 append_composite_type_field (t, "u64", elem);
4639 elem = builtin_type (gdbarch)->builtin_float;
4640 append_composite_type_field (t, "f32", init_vector_type (elem, 2));
4641 elem = builtin_type (gdbarch)->builtin_double;
4642 append_composite_type_field (t, "f64", elem);
4643
2062087b 4644 t->set_is_vector (true);
d0e39ea2 4645 t->set_name ("neon_d");
58d6951d
DJ
4646 tdep->neon_double_type = t;
4647 }
4648
4649 return tdep->neon_double_type;
4650}
4651
4652/* FIXME: The vector types are not correctly ordered on big-endian
4653 targets. Just as s0 is the low bits of d0, d0[0] is also the low
4654 bits of d0 - regardless of what unit size is being held in d0. So
4655 the offset of the first uint8 in d0 is 7, but the offset of the
4656 first float is 4. This code works as-is for little-endian
4657 targets. */
4658
4659static struct type *
4660arm_neon_quad_type (struct gdbarch *gdbarch)
4661{
345bd07c 4662 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
58d6951d
DJ
4663
4664 if (tdep->neon_quad_type == NULL)
4665 {
4666 struct type *t, *elem;
4667
4668 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
4669 TYPE_CODE_UNION);
4670 elem = builtin_type (gdbarch)->builtin_uint8;
4671 append_composite_type_field (t, "u8", init_vector_type (elem, 16));
4672 elem = builtin_type (gdbarch)->builtin_uint16;
4673 append_composite_type_field (t, "u16", init_vector_type (elem, 8));
4674 elem = builtin_type (gdbarch)->builtin_uint32;
4675 append_composite_type_field (t, "u32", init_vector_type (elem, 4));
4676 elem = builtin_type (gdbarch)->builtin_uint64;
4677 append_composite_type_field (t, "u64", init_vector_type (elem, 2));
4678 elem = builtin_type (gdbarch)->builtin_float;
4679 append_composite_type_field (t, "f32", init_vector_type (elem, 4));
4680 elem = builtin_type (gdbarch)->builtin_double;
4681 append_composite_type_field (t, "f64", init_vector_type (elem, 2));
4682
2062087b 4683 t->set_is_vector (true);
d0e39ea2 4684 t->set_name ("neon_q");
58d6951d
DJ
4685 tdep->neon_quad_type = t;
4686 }
4687
4688 return tdep->neon_quad_type;
4689}
4690
ecbf5d4f
LM
4691/* Return true if REGNUM is a Q pseudo register. Return false
4692 otherwise.
4693
4694 REGNUM is the raw register number and not a pseudo-relative register
4695 number. */
4696
4697static bool
4698is_q_pseudo (struct gdbarch *gdbarch, int regnum)
4699{
345bd07c 4700 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
ecbf5d4f 4701
ae66a8f1
SP
4702 /* Q pseudo registers are available for both NEON (Q0~Q15) and
4703 MVE (Q0~Q7) features. */
ecbf5d4f
LM
4704 if (tdep->have_q_pseudos
4705 && regnum >= tdep->q_pseudo_base
4706 && regnum < (tdep->q_pseudo_base + tdep->q_pseudo_count))
4707 return true;
4708
4709 return false;
4710}
4711
4712/* Return true if REGNUM is a VFP S pseudo register. Return false
4713 otherwise.
4714
4715 REGNUM is the raw register number and not a pseudo-relative register
4716 number. */
4717
4718static bool
4719is_s_pseudo (struct gdbarch *gdbarch, int regnum)
4720{
345bd07c 4721 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
ecbf5d4f
LM
4722
4723 if (tdep->have_s_pseudos
4724 && regnum >= tdep->s_pseudo_base
4725 && regnum < (tdep->s_pseudo_base + tdep->s_pseudo_count))
4726 return true;
4727
4728 return false;
4729}
4730
ae66a8f1
SP
4731/* Return true if REGNUM is a MVE pseudo register (P0). Return false
4732 otherwise.
4733
4734 REGNUM is the raw register number and not a pseudo-relative register
4735 number. */
4736
4737static bool
4738is_mve_pseudo (struct gdbarch *gdbarch, int regnum)
4739{
345bd07c 4740 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
ae66a8f1
SP
4741
4742 if (tdep->have_mve
4743 && regnum >= tdep->mve_pseudo_base
4744 && regnum < tdep->mve_pseudo_base + tdep->mve_pseudo_count)
4745 return true;
4746
4747 return false;
4748}
4749
a01567f4
LM
4750/* Return true if REGNUM is a PACBTI pseudo register (ra_auth_code). Return
4751 false otherwise.
4752
4753 REGNUM is the raw register number and not a pseudo-relative register
4754 number. */
4755
4756static bool
4757is_pacbti_pseudo (struct gdbarch *gdbarch, int regnum)
4758{
4759 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
4760
4761 if (tdep->have_pacbti
4762 && regnum >= tdep->pacbti_pseudo_base
4763 && regnum < tdep->pacbti_pseudo_base + tdep->pacbti_pseudo_count)
4764 return true;
4765
4766 return false;
4767}
4768
34e8f22d
RE
4769/* Return the GDB type object for the "standard" data type of data in
4770 register N. */
4771
4772static struct type *
7a5ea0d4 4773arm_register_type (struct gdbarch *gdbarch, int regnum)
032758dc 4774{
345bd07c 4775 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
58d6951d 4776
ecbf5d4f 4777 if (is_s_pseudo (gdbarch, regnum))
58d6951d
DJ
4778 return builtin_type (gdbarch)->builtin_float;
4779
ecbf5d4f 4780 if (is_q_pseudo (gdbarch, regnum))
58d6951d
DJ
4781 return arm_neon_quad_type (gdbarch);
4782
ae66a8f1
SP
4783 if (is_mve_pseudo (gdbarch, regnum))
4784 return builtin_type (gdbarch)->builtin_int16;
4785
a01567f4
LM
4786 if (is_pacbti_pseudo (gdbarch, regnum))
4787 return builtin_type (gdbarch)->builtin_uint32;
4788
58d6951d
DJ
4789 /* If the target description has register information, we are only
4790 in this function so that we can override the types of
4791 double-precision registers for NEON. */
4792 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
4793 {
4794 struct type *t = tdesc_register_type (gdbarch, regnum);
4795
4796 if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
78134374 4797 && t->code () == TYPE_CODE_FLT
ecbf5d4f 4798 && tdep->have_neon)
58d6951d
DJ
4799 return arm_neon_double_type (gdbarch);
4800 else
4801 return t;
4802 }
4803
34e8f22d 4804 if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
58d6951d 4805 {
ecbf5d4f 4806 if (!tdep->have_fpa_registers)
58d6951d
DJ
4807 return builtin_type (gdbarch)->builtin_void;
4808
4809 return arm_ext_type (gdbarch);
4810 }
e4c16157 4811 else if (regnum == ARM_SP_REGNUM)
0dfff4cb 4812 return builtin_type (gdbarch)->builtin_data_ptr;
e4c16157 4813 else if (regnum == ARM_PC_REGNUM)
0dfff4cb 4814 return builtin_type (gdbarch)->builtin_func_ptr;
ff6f572f
DJ
4815 else if (regnum >= ARRAY_SIZE (arm_register_names))
4816 /* These registers are only supported on targets which supply
4817 an XML description. */
df4df182 4818 return builtin_type (gdbarch)->builtin_int0;
032758dc 4819 else
df4df182 4820 return builtin_type (gdbarch)->builtin_uint32;
032758dc
AC
4821}
4822
ff6f572f
DJ
4823/* Map a DWARF register REGNUM onto the appropriate GDB register
4824 number. */
4825
4826static int
d3f73121 4827arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
ff6f572f
DJ
4828{
4829 /* Core integer regs. */
4830 if (reg >= 0 && reg <= 15)
4831 return reg;
4832
4833 /* Legacy FPA encoding. These were once used in a way which
4834 overlapped with VFP register numbering, so their use is
4835 discouraged, but GDB doesn't support the ARM toolchain
4836 which used them for VFP. */
4837 if (reg >= 16 && reg <= 23)
4838 return ARM_F0_REGNUM + reg - 16;
4839
4840 /* New assignments for the FPA registers. */
4841 if (reg >= 96 && reg <= 103)
4842 return ARM_F0_REGNUM + reg - 96;
4843
4844 /* WMMX register assignments. */
4845 if (reg >= 104 && reg <= 111)
4846 return ARM_WCGR0_REGNUM + reg - 104;
4847
4848 if (reg >= 112 && reg <= 127)
4849 return ARM_WR0_REGNUM + reg - 112;
4850
a01567f4
LM
4851 /* PACBTI register containing the Pointer Authentication Code. */
4852 if (reg == ARM_DWARF_RA_AUTH_CODE)
4853 {
4854 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
4855
4856 if (tdep->have_pacbti)
4857 return tdep->pacbti_pseudo_base;
4858
4859 return -1;
4860 }
4861
ff6f572f
DJ
4862 if (reg >= 192 && reg <= 199)
4863 return ARM_WC0_REGNUM + reg - 192;
4864
58d6951d
DJ
4865 /* VFP v2 registers. A double precision value is actually
4866 in d1 rather than s2, but the ABI only defines numbering
4867 for the single precision registers. This will "just work"
4868 in GDB for little endian targets (we'll read eight bytes,
4869 starting in s0 and then progressing to s1), but will be
4870 reversed on big endian targets with VFP. This won't
4871 be a problem for the new Neon quad registers; you're supposed
4872 to use DW_OP_piece for those. */
4873 if (reg >= 64 && reg <= 95)
4874 {
4875 char name_buf[4];
4876
8c042590 4877 xsnprintf (name_buf, sizeof (name_buf), "s%d", reg - 64);
58d6951d
DJ
4878 return user_reg_map_name_to_regnum (gdbarch, name_buf,
4879 strlen (name_buf));
4880 }
4881
4882 /* VFP v3 / Neon registers. This range is also used for VFP v2
4883 registers, except that it now describes d0 instead of s0. */
4884 if (reg >= 256 && reg <= 287)
4885 {
4886 char name_buf[4];
4887
8c042590 4888 xsnprintf (name_buf, sizeof (name_buf), "d%d", reg - 256);
58d6951d
DJ
4889 return user_reg_map_name_to_regnum (gdbarch, name_buf,
4890 strlen (name_buf));
4891 }
4892
ff6f572f
DJ
4893 return -1;
4894}
4895
26216b98
AC
4896/* Map GDB internal REGNUM onto the Arm simulator register numbers. */
4897static int
e7faf938 4898arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
26216b98
AC
4899{
4900 int reg = regnum;
e7faf938 4901 gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
26216b98 4902
ff6f572f
DJ
4903 if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
4904 return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
4905
4906 if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
4907 return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
4908
4909 if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
4910 return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
4911
26216b98
AC
4912 if (reg < NUM_GREGS)
4913 return SIM_ARM_R0_REGNUM + reg;
4914 reg -= NUM_GREGS;
4915
4916 if (reg < NUM_FREGS)
4917 return SIM_ARM_FP0_REGNUM + reg;
4918 reg -= NUM_FREGS;
4919
4920 if (reg < NUM_SREGS)
4921 return SIM_ARM_FPS_REGNUM + reg;
4922 reg -= NUM_SREGS;
4923
edefbb7c 4924 internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
26216b98 4925}
34e8f22d 4926
a01567f4
LM
4927static const unsigned char op_lit0 = DW_OP_lit0;
4928
4929static void
4930arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
4931 struct dwarf2_frame_state_reg *reg,
4932 struct frame_info *this_frame)
4933{
4934 if (is_pacbti_pseudo (gdbarch, regnum))
4935 {
4936 /* Initialize RA_AUTH_CODE to zero. */
4937 reg->how = DWARF2_FRAME_REG_SAVED_VAL_EXP;
4938 reg->loc.exp.start = &op_lit0;
4939 reg->loc.exp.len = 1;
4940 return;
4941 }
4942
4943 switch (regnum)
4944 {
4945 case ARM_PC_REGNUM:
4946 case ARM_PS_REGNUM:
4947 reg->how = DWARF2_FRAME_REG_FN;
4948 reg->loc.fn = arm_dwarf2_prev_register;
4949 break;
4950 case ARM_SP_REGNUM:
4951 reg->how = DWARF2_FRAME_REG_CFA;
4952 break;
4953 }
4954}
4955
d9311bfa
AT
4956/* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
4957 the buffer to be NEW_LEN bytes ending at ENDADDR. Return
4958 NULL if an error occurs. BUF is freed. */
c906108c 4959
d9311bfa
AT
4960static gdb_byte *
4961extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
4962 int old_len, int new_len)
4963{
4964 gdb_byte *new_buf;
4965 int bytes_to_read = new_len - old_len;
c906108c 4966
d9311bfa
AT
4967 new_buf = (gdb_byte *) xmalloc (new_len);
4968 memcpy (new_buf + bytes_to_read, buf, old_len);
4969 xfree (buf);
198cd59d 4970 if (target_read_code (endaddr - new_len, new_buf, bytes_to_read) != 0)
d9311bfa
AT
4971 {
4972 xfree (new_buf);
4973 return NULL;
c906108c 4974 }
d9311bfa 4975 return new_buf;
c906108c
SS
4976}
4977
d9311bfa
AT
4978/* An IT block is at most the 2-byte IT instruction followed by
4979 four 4-byte instructions. The furthest back we must search to
4980 find an IT block that affects the current instruction is thus
4981 2 + 3 * 4 == 14 bytes. */
4982#define MAX_IT_BLOCK_PREFIX 14
177321bd 4983
d9311bfa
AT
4984/* Use a quick scan if there are more than this many bytes of
4985 code. */
4986#define IT_SCAN_THRESHOLD 32
177321bd 4987
d9311bfa
AT
4988/* Adjust a breakpoint's address to move breakpoints out of IT blocks.
4989 A breakpoint in an IT block may not be hit, depending on the
4990 condition flags. */
ad527d2e 4991static CORE_ADDR
d9311bfa 4992arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
c906108c 4993{
d9311bfa
AT
4994 gdb_byte *buf;
4995 char map_type;
4996 CORE_ADDR boundary, func_start;
4997 int buf_len;
4998 enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
4999 int i, any, last_it, last_it_count;
345bd07c 5000 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
177321bd 5001
d9311bfa 5002 /* If we are using BKPT breakpoints, none of this is necessary. */
345bd07c 5003 if (tdep->thumb2_breakpoint == NULL)
d9311bfa 5004 return bpaddr;
177321bd 5005
d9311bfa
AT
5006 /* ARM mode does not have this problem. */
5007 if (!arm_pc_is_thumb (gdbarch, bpaddr))
5008 return bpaddr;
177321bd 5009
d9311bfa
AT
5010 /* We are setting a breakpoint in Thumb code that could potentially
5011 contain an IT block. The first step is to find how much Thumb
5012 code there is; we do not need to read outside of known Thumb
5013 sequences. */
5014 map_type = arm_find_mapping_symbol (bpaddr, &boundary);
5015 if (map_type == 0)
5016 /* Thumb-2 code must have mapping symbols to have a chance. */
5017 return bpaddr;
9dca5578 5018
d9311bfa 5019 bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
177321bd 5020
d9311bfa
AT
5021 if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
5022 && func_start > boundary)
5023 boundary = func_start;
9dca5578 5024
d9311bfa
AT
5025 /* Search for a candidate IT instruction. We have to do some fancy
5026 footwork to distinguish a real IT instruction from the second
5027 half of a 32-bit instruction, but there is no need for that if
5028 there's no candidate. */
325fac50 5029 buf_len = std::min (bpaddr - boundary, (CORE_ADDR) MAX_IT_BLOCK_PREFIX);
d9311bfa
AT
5030 if (buf_len == 0)
5031 /* No room for an IT instruction. */
5032 return bpaddr;
c906108c 5033
d9311bfa 5034 buf = (gdb_byte *) xmalloc (buf_len);
198cd59d 5035 if (target_read_code (bpaddr - buf_len, buf, buf_len) != 0)
d9311bfa
AT
5036 return bpaddr;
5037 any = 0;
5038 for (i = 0; i < buf_len; i += 2)
c906108c 5039 {
d9311bfa
AT
5040 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5041 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
25b41d01 5042 {
d9311bfa
AT
5043 any = 1;
5044 break;
25b41d01 5045 }
c906108c 5046 }
d9311bfa
AT
5047
5048 if (any == 0)
c906108c 5049 {
d9311bfa
AT
5050 xfree (buf);
5051 return bpaddr;
f9d67f43
DJ
5052 }
5053
5054 /* OK, the code bytes before this instruction contain at least one
5055 halfword which resembles an IT instruction. We know that it's
5056 Thumb code, but there are still two possibilities. Either the
5057 halfword really is an IT instruction, or it is the second half of
5058 a 32-bit Thumb instruction. The only way we can tell is to
5059 scan forwards from a known instruction boundary. */
5060 if (bpaddr - boundary > IT_SCAN_THRESHOLD)
5061 {
5062 int definite;
5063
5064 /* There's a lot of code before this instruction. Start with an
5065 optimistic search; it's easy to recognize halfwords that can
5066 not be the start of a 32-bit instruction, and use that to
5067 lock on to the instruction boundaries. */
5068 buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
5069 if (buf == NULL)
5070 return bpaddr;
5071 buf_len = IT_SCAN_THRESHOLD;
5072
5073 definite = 0;
5074 for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
5075 {
5076 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5077 if (thumb_insn_size (inst1) == 2)
5078 {
5079 definite = 1;
5080 break;
5081 }
5082 }
5083
5084 /* At this point, if DEFINITE, BUF[I] is the first place we
5085 are sure that we know the instruction boundaries, and it is far
5086 enough from BPADDR that we could not miss an IT instruction
5087 affecting BPADDR. If ! DEFINITE, give up - start from a
5088 known boundary. */
5089 if (! definite)
5090 {
0963b4bd
MS
5091 buf = extend_buffer_earlier (buf, bpaddr, buf_len,
5092 bpaddr - boundary);
f9d67f43
DJ
5093 if (buf == NULL)
5094 return bpaddr;
5095 buf_len = bpaddr - boundary;
5096 i = 0;
5097 }
5098 }
5099 else
5100 {
5101 buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
5102 if (buf == NULL)
5103 return bpaddr;
5104 buf_len = bpaddr - boundary;
5105 i = 0;
5106 }
5107
5108 /* Scan forwards. Find the last IT instruction before BPADDR. */
5109 last_it = -1;
5110 last_it_count = 0;
5111 while (i < buf_len)
5112 {
5113 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5114 last_it_count--;
5115 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5116 {
5117 last_it = i;
5118 if (inst1 & 0x0001)
5119 last_it_count = 4;
5120 else if (inst1 & 0x0002)
5121 last_it_count = 3;
5122 else if (inst1 & 0x0004)
5123 last_it_count = 2;
5124 else
5125 last_it_count = 1;
5126 }
5127 i += thumb_insn_size (inst1);
5128 }
5129
5130 xfree (buf);
5131
5132 if (last_it == -1)
5133 /* There wasn't really an IT instruction after all. */
5134 return bpaddr;
5135
5136 if (last_it_count < 1)
5137 /* It was too far away. */
5138 return bpaddr;
5139
5140 /* This really is a trouble spot. Move the breakpoint to the IT
5141 instruction. */
5142 return bpaddr - buf_len + last_it;
5143}
5144
cca44b1b 5145/* ARM displaced stepping support.
c906108c 5146
cca44b1b 5147 Generally ARM displaced stepping works as follows:
c906108c 5148
cca44b1b 5149 1. When an instruction is to be single-stepped, it is first decoded by
2ba163c8
SM
5150 arm_process_displaced_insn. Depending on the type of instruction, it is
5151 then copied to a scratch location, possibly in a modified form. The
5152 copy_* set of functions performs such modification, as necessary. A
5153 breakpoint is placed after the modified instruction in the scratch space
5154 to return control to GDB. Note in particular that instructions which
5155 modify the PC will no longer do so after modification.
c5aa993b 5156
cca44b1b
JB
5157 2. The instruction is single-stepped, by setting the PC to the scratch
5158 location address, and resuming. Control returns to GDB when the
5159 breakpoint is hit.
c5aa993b 5160
cca44b1b
JB
5161 3. A cleanup function (cleanup_*) is called corresponding to the copy_*
5162 function used for the current instruction. This function's job is to
5163 put the CPU/memory state back to what it would have been if the
5164 instruction had been executed unmodified in its original location. */
c5aa993b 5165
cca44b1b
JB
5166/* NOP instruction (mov r0, r0). */
5167#define ARM_NOP 0xe1a00000
34518530 5168#define THUMB_NOP 0x4600
cca44b1b
JB
5169
5170/* Helper for register reads for displaced stepping. In particular, this
5171 returns the PC as it would be seen by the instruction at its original
5172 location. */
5173
5174ULONGEST
1152d984 5175displaced_read_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
36073a92 5176 int regno)
cca44b1b
JB
5177{
5178 ULONGEST ret;
36073a92 5179 CORE_ADDR from = dsc->insn_addr;
cca44b1b 5180
bf9f652a 5181 if (regno == ARM_PC_REGNUM)
cca44b1b 5182 {
4db71c0b
YQ
5183 /* Compute pipeline offset:
5184 - When executing an ARM instruction, PC reads as the address of the
5185 current instruction plus 8.
5186 - When executing a Thumb instruction, PC reads as the address of the
5187 current instruction plus 4. */
5188
36073a92 5189 if (!dsc->is_thumb)
4db71c0b
YQ
5190 from += 8;
5191 else
5192 from += 4;
5193
136821d9
SM
5194 displaced_debug_printf ("read pc value %.8lx",
5195 (unsigned long) from);
4db71c0b 5196 return (ULONGEST) from;
cca44b1b 5197 }
c906108c 5198 else
cca44b1b
JB
5199 {
5200 regcache_cooked_read_unsigned (regs, regno, &ret);
136821d9
SM
5201
5202 displaced_debug_printf ("read r%d value %.8lx",
5203 regno, (unsigned long) ret);
5204
cca44b1b
JB
5205 return ret;
5206 }
c906108c
SS
5207}
5208
cca44b1b
JB
5209static int
5210displaced_in_arm_mode (struct regcache *regs)
5211{
5212 ULONGEST ps;
ac7936df 5213 ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
66e810cd 5214
cca44b1b 5215 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
66e810cd 5216
9779414d 5217 return (ps & t_bit) == 0;
cca44b1b 5218}
66e810cd 5219
cca44b1b 5220/* Write to the PC as from a branch instruction. */
c906108c 5221
cca44b1b 5222static void
1152d984 5223branch_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
36073a92 5224 ULONGEST val)
c906108c 5225{
36073a92 5226 if (!dsc->is_thumb)
cca44b1b
JB
5227 /* Note: If bits 0/1 are set, this branch would be unpredictable for
5228 architecture versions < 6. */
0963b4bd
MS
5229 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5230 val & ~(ULONGEST) 0x3);
cca44b1b 5231 else
0963b4bd
MS
5232 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5233 val & ~(ULONGEST) 0x1);
cca44b1b 5234}
66e810cd 5235
cca44b1b
JB
5236/* Write to the PC as from a branch-exchange instruction. */
5237
5238static void
5239bx_write_pc (struct regcache *regs, ULONGEST val)
5240{
5241 ULONGEST ps;
ac7936df 5242 ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
cca44b1b
JB
5243
5244 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
5245
5246 if ((val & 1) == 1)
c906108c 5247 {
9779414d 5248 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
cca44b1b
JB
5249 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
5250 }
5251 else if ((val & 2) == 0)
5252 {
9779414d 5253 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 5254 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
c906108c
SS
5255 }
5256 else
5257 {
cca44b1b
JB
5258 /* Unpredictable behaviour. Try to do something sensible (switch to ARM
5259 mode, align dest to 4 bytes). */
5260 warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
9779414d 5261 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 5262 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
c906108c
SS
5263 }
5264}
ed9a39eb 5265
cca44b1b 5266/* Write to the PC as if from a load instruction. */
ed9a39eb 5267
34e8f22d 5268static void
1152d984 5269load_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
36073a92 5270 ULONGEST val)
ed9a39eb 5271{
cca44b1b
JB
5272 if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
5273 bx_write_pc (regs, val);
5274 else
36073a92 5275 branch_write_pc (regs, dsc, val);
cca44b1b 5276}
be8626e0 5277
cca44b1b
JB
5278/* Write to the PC as if from an ALU instruction. */
5279
5280static void
1152d984 5281alu_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
36073a92 5282 ULONGEST val)
cca44b1b 5283{
36073a92 5284 if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
cca44b1b
JB
5285 bx_write_pc (regs, val);
5286 else
36073a92 5287 branch_write_pc (regs, dsc, val);
cca44b1b
JB
5288}
5289
5290/* Helper for writing to registers for displaced stepping. Writing to the PC
5291 has a varying effects depending on the instruction which does the write:
5292 this is controlled by the WRITE_PC argument. */
5293
5294void
1152d984 5295displaced_write_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
cca44b1b
JB
5296 int regno, ULONGEST val, enum pc_write_style write_pc)
5297{
bf9f652a 5298 if (regno == ARM_PC_REGNUM)
08216dd7 5299 {
136821d9
SM
5300 displaced_debug_printf ("writing pc %.8lx", (unsigned long) val);
5301
cca44b1b 5302 switch (write_pc)
08216dd7 5303 {
cca44b1b 5304 case BRANCH_WRITE_PC:
36073a92 5305 branch_write_pc (regs, dsc, val);
08216dd7
RE
5306 break;
5307
cca44b1b
JB
5308 case BX_WRITE_PC:
5309 bx_write_pc (regs, val);
24b21115 5310 break;
cca44b1b
JB
5311
5312 case LOAD_WRITE_PC:
36073a92 5313 load_write_pc (regs, dsc, val);
24b21115 5314 break;
cca44b1b
JB
5315
5316 case ALU_WRITE_PC:
36073a92 5317 alu_write_pc (regs, dsc, val);
24b21115 5318 break;
cca44b1b
JB
5319
5320 case CANNOT_WRITE_PC:
5321 warning (_("Instruction wrote to PC in an unexpected way when "
5322 "single-stepping"));
08216dd7
RE
5323 break;
5324
5325 default:
97b9747c
JB
5326 internal_error (__FILE__, __LINE__,
5327 _("Invalid argument to displaced_write_reg"));
08216dd7 5328 }
b508a996 5329
cca44b1b 5330 dsc->wrote_to_pc = 1;
b508a996 5331 }
ed9a39eb 5332 else
b508a996 5333 {
136821d9
SM
5334 displaced_debug_printf ("writing r%d value %.8lx",
5335 regno, (unsigned long) val);
cca44b1b 5336 regcache_cooked_write_unsigned (regs, regno, val);
b508a996 5337 }
34e8f22d
RE
5338}
5339
cca44b1b
JB
5340/* This function is used to concisely determine if an instruction INSN
5341 references PC. Register fields of interest in INSN should have the
0963b4bd
MS
5342 corresponding fields of BITMASK set to 0b1111. The function
5343 returns return 1 if any of these fields in INSN reference the PC
5344 (also 0b1111, r15), else it returns 0. */
67255d04
RE
5345
5346static int
cca44b1b 5347insn_references_pc (uint32_t insn, uint32_t bitmask)
67255d04 5348{
cca44b1b 5349 uint32_t lowbit = 1;
67255d04 5350
cca44b1b
JB
5351 while (bitmask != 0)
5352 {
5353 uint32_t mask;
44e1a9eb 5354
cca44b1b
JB
5355 for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
5356 ;
67255d04 5357
cca44b1b
JB
5358 if (!lowbit)
5359 break;
67255d04 5360
cca44b1b 5361 mask = lowbit * 0xf;
67255d04 5362
cca44b1b
JB
5363 if ((insn & mask) == mask)
5364 return 1;
5365
5366 bitmask &= ~mask;
67255d04
RE
5367 }
5368
cca44b1b
JB
5369 return 0;
5370}
2af48f68 5371
cca44b1b
JB
5372/* The simplest copy function. Many instructions have the same effect no
5373 matter what address they are executed at: in those cases, use this. */
67255d04 5374
cca44b1b 5375static int
1152d984
SM
5376arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn, const char *iname,
5377 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 5378{
136821d9
SM
5379 displaced_debug_printf ("copying insn %.8lx, opcode/class '%s' unmodified",
5380 (unsigned long) insn, iname);
67255d04 5381
cca44b1b 5382 dsc->modinsn[0] = insn;
67255d04 5383
cca44b1b
JB
5384 return 0;
5385}
5386
34518530
YQ
5387static int
5388thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
5389 uint16_t insn2, const char *iname,
1152d984 5390 arm_displaced_step_copy_insn_closure *dsc)
34518530 5391{
136821d9
SM
5392 displaced_debug_printf ("copying insn %.4x %.4x, opcode/class '%s' "
5393 "unmodified", insn1, insn2, iname);
34518530
YQ
5394
5395 dsc->modinsn[0] = insn1;
5396 dsc->modinsn[1] = insn2;
5397 dsc->numinsns = 2;
5398
5399 return 0;
5400}
5401
5402/* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
5403 modification. */
5404static int
615234c1 5405thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, uint16_t insn,
34518530 5406 const char *iname,
1152d984 5407 arm_displaced_step_copy_insn_closure *dsc)
34518530 5408{
136821d9
SM
5409 displaced_debug_printf ("copying insn %.4x, opcode/class '%s' unmodified",
5410 insn, iname);
34518530
YQ
5411
5412 dsc->modinsn[0] = insn;
5413
5414 return 0;
5415}
5416
cca44b1b
JB
5417/* Preload instructions with immediate offset. */
5418
5419static void
1152d984
SM
5420cleanup_preload (struct gdbarch *gdbarch, regcache *regs,
5421 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
5422{
5423 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5424 if (!dsc->u.preload.immed)
5425 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5426}
5427
7ff120b4
YQ
5428static void
5429install_preload (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5430 arm_displaced_step_copy_insn_closure *dsc, unsigned int rn)
cca44b1b 5431{
cca44b1b 5432 ULONGEST rn_val;
cca44b1b
JB
5433 /* Preload instructions:
5434
5435 {pli/pld} [rn, #+/-imm]
5436 ->
5437 {pli/pld} [r0, #+/-imm]. */
5438
36073a92
YQ
5439 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5440 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 5441 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
cca44b1b
JB
5442 dsc->u.preload.immed = 1;
5443
cca44b1b 5444 dsc->cleanup = &cleanup_preload;
cca44b1b
JB
5445}
5446
cca44b1b 5447static int
7ff120b4 5448arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
1152d984 5449 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
5450{
5451 unsigned int rn = bits (insn, 16, 19);
cca44b1b 5452
7ff120b4
YQ
5453 if (!insn_references_pc (insn, 0x000f0000ul))
5454 return arm_copy_unmodified (gdbarch, insn, "preload", dsc);
cca44b1b 5455
136821d9 5456 displaced_debug_printf ("copying preload insn %.8lx", (unsigned long) insn);
cca44b1b 5457
7ff120b4
YQ
5458 dsc->modinsn[0] = insn & 0xfff0ffff;
5459
5460 install_preload (gdbarch, regs, dsc, rn);
5461
5462 return 0;
5463}
5464
34518530
YQ
5465static int
5466thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
1152d984 5467 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
5468{
5469 unsigned int rn = bits (insn1, 0, 3);
5470 unsigned int u_bit = bit (insn1, 7);
5471 int imm12 = bits (insn2, 0, 11);
5472 ULONGEST pc_val;
5473
5474 if (rn != ARM_PC_REGNUM)
5475 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);
5476
5477 /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
5478 PLD (literal) Encoding T1. */
136821d9
SM
5479 displaced_debug_printf ("copying pld/pli pc (0x%x) %c imm12 %.4x",
5480 (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
5481 imm12);
34518530
YQ
5482
5483 if (!u_bit)
5484 imm12 = -1 * imm12;
5485
5486 /* Rewrite instruction {pli/pld} PC imm12 into:
5487 Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12
5488
5489 {pli/pld} [r0, r1]
5490
5491 Cleanup: r0 <- tmp[0], r1 <- tmp[1]. */
5492
5493 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5494 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5495
5496 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
5497
5498 displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
5499 displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
5500 dsc->u.preload.immed = 0;
5501
5502 /* {pli/pld} [r0, r1] */
5503 dsc->modinsn[0] = insn1 & 0xfff0;
5504 dsc->modinsn[1] = 0xf001;
5505 dsc->numinsns = 2;
5506
5507 dsc->cleanup = &cleanup_preload;
5508 return 0;
5509}
5510
7ff120b4
YQ
5511/* Preload instructions with register offset. */
5512
5513static void
5514install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5515 arm_displaced_step_copy_insn_closure *dsc, unsigned int rn,
7ff120b4
YQ
5516 unsigned int rm)
5517{
5518 ULONGEST rn_val, rm_val;
5519
cca44b1b
JB
5520 /* Preload register-offset instructions:
5521
5522 {pli/pld} [rn, rm {, shift}]
5523 ->
5524 {pli/pld} [r0, r1 {, shift}]. */
5525
36073a92
YQ
5526 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5527 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5528 rn_val = displaced_read_reg (regs, dsc, rn);
5529 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5530 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5531 displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
cca44b1b
JB
5532 dsc->u.preload.immed = 0;
5533
cca44b1b 5534 dsc->cleanup = &cleanup_preload;
7ff120b4
YQ
5535}
5536
5537static int
5538arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
5539 struct regcache *regs,
1152d984 5540 arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
5541{
5542 unsigned int rn = bits (insn, 16, 19);
5543 unsigned int rm = bits (insn, 0, 3);
5544
5545
5546 if (!insn_references_pc (insn, 0x000f000ful))
5547 return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);
5548
136821d9
SM
5549 displaced_debug_printf ("copying preload insn %.8lx",
5550 (unsigned long) insn);
7ff120b4
YQ
5551
5552 dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
cca44b1b 5553
7ff120b4 5554 install_preload_reg (gdbarch, regs, dsc, rn, rm);
cca44b1b
JB
5555 return 0;
5556}
5557
5558/* Copy/cleanup coprocessor load and store instructions. */
5559
5560static void
6e39997a 5561cleanup_copro_load_store (struct gdbarch *gdbarch,
cca44b1b 5562 struct regcache *regs,
1152d984 5563 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 5564{
36073a92 5565 ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5566
5567 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5568
5569 if (dsc->u.ldst.writeback)
5570 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
5571}
5572
7ff120b4
YQ
5573static void
5574install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5575 arm_displaced_step_copy_insn_closure *dsc,
7ff120b4 5576 int writeback, unsigned int rn)
cca44b1b 5577{
cca44b1b 5578 ULONGEST rn_val;
cca44b1b 5579
cca44b1b
JB
5580 /* Coprocessor load/store instructions:
5581
5582 {stc/stc2} [<Rn>, #+/-imm] (and other immediate addressing modes)
5583 ->
5584 {stc/stc2} [r0, #+/-imm].
5585
5586 ldc/ldc2 are handled identically. */
5587
36073a92
YQ
5588 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5589 rn_val = displaced_read_reg (regs, dsc, rn);
2b16b2e3
YQ
5590 /* PC should be 4-byte aligned. */
5591 rn_val = rn_val & 0xfffffffc;
cca44b1b
JB
5592 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5593
7ff120b4 5594 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
5595 dsc->u.ldst.rn = rn;
5596
7ff120b4
YQ
5597 dsc->cleanup = &cleanup_copro_load_store;
5598}
5599
5600static int
5601arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
5602 struct regcache *regs,
1152d984 5603 arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
5604{
5605 unsigned int rn = bits (insn, 16, 19);
5606
5607 if (!insn_references_pc (insn, 0x000f0000ul))
5608 return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);
5609
136821d9
SM
5610 displaced_debug_printf ("copying coprocessor load/store insn %.8lx",
5611 (unsigned long) insn);
7ff120b4 5612
cca44b1b
JB
5613 dsc->modinsn[0] = insn & 0xfff0ffff;
5614
7ff120b4 5615 install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);
cca44b1b
JB
5616
5617 return 0;
5618}
5619
34518530
YQ
5620static int
5621thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
5622 uint16_t insn2, struct regcache *regs,
1152d984 5623 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
5624{
5625 unsigned int rn = bits (insn1, 0, 3);
5626
5627 if (rn != ARM_PC_REGNUM)
5628 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
5629 "copro load/store", dsc);
5630
136821d9
SM
5631 displaced_debug_printf ("copying coprocessor load/store insn %.4x%.4x",
5632 insn1, insn2);
34518530
YQ
5633
5634 dsc->modinsn[0] = insn1 & 0xfff0;
5635 dsc->modinsn[1] = insn2;
5636 dsc->numinsns = 2;
5637
5638 /* This function is called for copying instruction LDC/LDC2/VLDR, which
5639 doesn't support writeback, so pass 0. */
5640 install_copro_load_store (gdbarch, regs, dsc, 0, rn);
5641
5642 return 0;
5643}
5644
cca44b1b
JB
5645/* Clean up branch instructions (actually perform the branch, by setting
5646 PC). */
5647
5648static void
6e39997a 5649cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5650 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 5651{
36073a92 5652 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
5653 int branch_taken = condition_true (dsc->u.branch.cond, status);
5654 enum pc_write_style write_pc = dsc->u.branch.exchange
5655 ? BX_WRITE_PC : BRANCH_WRITE_PC;
5656
5657 if (!branch_taken)
5658 return;
5659
5660 if (dsc->u.branch.link)
5661 {
8c8dba6d 5662 /* The value of LR should be the next insn of current one. In order
85102364 5663 not to confuse logic handling later insn `bx lr', if current insn mode
8c8dba6d
YQ
5664 is Thumb, the bit 0 of LR value should be set to 1. */
5665 ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;
5666
5667 if (dsc->is_thumb)
5668 next_insn_addr |= 0x1;
5669
5670 displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
5671 CANNOT_WRITE_PC);
cca44b1b
JB
5672 }
5673
bf9f652a 5674 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
cca44b1b
JB
5675}
5676
5677/* Copy B/BL/BLX instructions with immediate destinations. */
5678
7ff120b4
YQ
5679static void
5680install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5681 arm_displaced_step_copy_insn_closure *dsc,
7ff120b4
YQ
5682 unsigned int cond, int exchange, int link, long offset)
5683{
5684 /* Implement "BL<cond> <label>" as:
5685
5686 Preparation: cond <- instruction condition
5687 Insn: mov r0, r0 (nop)
5688 Cleanup: if (condition true) { r14 <- pc; pc <- label }.
5689
5690 B<cond> similar, but don't set r14 in cleanup. */
5691
5692 dsc->u.branch.cond = cond;
5693 dsc->u.branch.link = link;
5694 dsc->u.branch.exchange = exchange;
5695
2b16b2e3
YQ
5696 dsc->u.branch.dest = dsc->insn_addr;
5697 if (link && exchange)
5698 /* For BLX, offset is computed from the Align (PC, 4). */
5699 dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;
5700
7ff120b4 5701 if (dsc->is_thumb)
2b16b2e3 5702 dsc->u.branch.dest += 4 + offset;
7ff120b4 5703 else
2b16b2e3 5704 dsc->u.branch.dest += 8 + offset;
7ff120b4
YQ
5705
5706 dsc->cleanup = &cleanup_branch;
5707}
cca44b1b 5708static int
7ff120b4 5709arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
1152d984 5710 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
5711{
5712 unsigned int cond = bits (insn, 28, 31);
5713 int exchange = (cond == 0xf);
5714 int link = exchange || bit (insn, 24);
cca44b1b
JB
5715 long offset;
5716
136821d9
SM
5717 displaced_debug_printf ("copying %s immediate insn %.8lx",
5718 (exchange) ? "blx" : (link) ? "bl" : "b",
5719 (unsigned long) insn);
cca44b1b
JB
5720 if (exchange)
5721 /* For BLX, set bit 0 of the destination. The cleanup_branch function will
5722 then arrange the switch into Thumb mode. */
5723 offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
5724 else
5725 offset = bits (insn, 0, 23) << 2;
5726
5727 if (bit (offset, 25))
5728 offset = offset | ~0x3ffffff;
5729
cca44b1b
JB
5730 dsc->modinsn[0] = ARM_NOP;
5731
7ff120b4 5732 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
cca44b1b
JB
5733 return 0;
5734}
5735
34518530
YQ
5736static int
5737thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
5738 uint16_t insn2, struct regcache *regs,
1152d984 5739 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
5740{
5741 int link = bit (insn2, 14);
5742 int exchange = link && !bit (insn2, 12);
5743 int cond = INST_AL;
5744 long offset = 0;
5745 int j1 = bit (insn2, 13);
5746 int j2 = bit (insn2, 11);
5747 int s = sbits (insn1, 10, 10);
5748 int i1 = !(j1 ^ bit (insn1, 10));
5749 int i2 = !(j2 ^ bit (insn1, 10));
5750
5751 if (!link && !exchange) /* B */
5752 {
5753 offset = (bits (insn2, 0, 10) << 1);
5754 if (bit (insn2, 12)) /* Encoding T4 */
5755 {
5756 offset |= (bits (insn1, 0, 9) << 12)
5757 | (i2 << 22)
5758 | (i1 << 23)
5759 | (s << 24);
5760 cond = INST_AL;
5761 }
5762 else /* Encoding T3 */
5763 {
5764 offset |= (bits (insn1, 0, 5) << 12)
5765 | (j1 << 18)
5766 | (j2 << 19)
5767 | (s << 20);
5768 cond = bits (insn1, 6, 9);
5769 }
5770 }
5771 else
5772 {
5773 offset = (bits (insn1, 0, 9) << 12);
5774 offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
5775 offset |= exchange ?
5776 (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
5777 }
5778
136821d9
SM
5779 displaced_debug_printf ("copying %s insn %.4x %.4x with offset %.8lx",
5780 link ? (exchange) ? "blx" : "bl" : "b",
5781 insn1, insn2, offset);
34518530
YQ
5782
5783 dsc->modinsn[0] = THUMB_NOP;
5784
5785 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
5786 return 0;
5787}
5788
5789/* Copy B Thumb instructions. */
5790static int
615234c1 5791thumb_copy_b (struct gdbarch *gdbarch, uint16_t insn,
1152d984 5792 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
5793{
5794 unsigned int cond = 0;
5795 int offset = 0;
5796 unsigned short bit_12_15 = bits (insn, 12, 15);
5797 CORE_ADDR from = dsc->insn_addr;
5798
5799 if (bit_12_15 == 0xd)
5800 {
5801 /* offset = SignExtend (imm8:0, 32) */
5802 offset = sbits ((insn << 1), 0, 8);
5803 cond = bits (insn, 8, 11);
5804 }
5805 else if (bit_12_15 == 0xe) /* Encoding T2 */
5806 {
5807 offset = sbits ((insn << 1), 0, 11);
5808 cond = INST_AL;
5809 }
5810
136821d9
SM
5811 displaced_debug_printf ("copying b immediate insn %.4x with offset %d",
5812 insn, offset);
34518530
YQ
5813
5814 dsc->u.branch.cond = cond;
5815 dsc->u.branch.link = 0;
5816 dsc->u.branch.exchange = 0;
5817 dsc->u.branch.dest = from + 4 + offset;
5818
5819 dsc->modinsn[0] = THUMB_NOP;
5820
5821 dsc->cleanup = &cleanup_branch;
5822
5823 return 0;
5824}
5825
cca44b1b
JB
5826/* Copy BX/BLX with register-specified destinations. */
5827
7ff120b4
YQ
5828static void
5829install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5830 arm_displaced_step_copy_insn_closure *dsc, int link,
7ff120b4 5831 unsigned int cond, unsigned int rm)
cca44b1b 5832{
cca44b1b
JB
5833 /* Implement {BX,BLX}<cond> <reg>" as:
5834
5835 Preparation: cond <- instruction condition
5836 Insn: mov r0, r0 (nop)
5837 Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
5838
5839 Don't set r14 in cleanup for BX. */
5840
36073a92 5841 dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5842
5843 dsc->u.branch.cond = cond;
5844 dsc->u.branch.link = link;
cca44b1b 5845
7ff120b4 5846 dsc->u.branch.exchange = 1;
cca44b1b
JB
5847
5848 dsc->cleanup = &cleanup_branch;
7ff120b4 5849}
cca44b1b 5850
7ff120b4
YQ
5851static int
5852arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
1152d984 5853 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
5854{
5855 unsigned int cond = bits (insn, 28, 31);
5856 /* BX: x12xxx1x
5857 BLX: x12xxx3x. */
5858 int link = bit (insn, 5);
5859 unsigned int rm = bits (insn, 0, 3);
5860
136821d9 5861 displaced_debug_printf ("copying insn %.8lx", (unsigned long) insn);
7ff120b4
YQ
5862
5863 dsc->modinsn[0] = ARM_NOP;
5864
5865 install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
cca44b1b
JB
5866 return 0;
5867}
5868
34518530
YQ
5869static int
5870thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
5871 struct regcache *regs,
1152d984 5872 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
5873{
5874 int link = bit (insn, 7);
5875 unsigned int rm = bits (insn, 3, 6);
5876
136821d9 5877 displaced_debug_printf ("copying insn %.4x", (unsigned short) insn);
34518530
YQ
5878
5879 dsc->modinsn[0] = THUMB_NOP;
5880
5881 install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);
5882
5883 return 0;
5884}
5885
5886
0963b4bd 5887/* Copy/cleanup arithmetic/logic instruction with immediate RHS. */
cca44b1b
JB
5888
5889static void
6e39997a 5890cleanup_alu_imm (struct gdbarch *gdbarch,
1152d984 5891 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 5892{
36073a92 5893 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5894 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5895 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5896 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
5897}
5898
5899static int
7ff120b4 5900arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
1152d984 5901 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
5902{
5903 unsigned int rn = bits (insn, 16, 19);
5904 unsigned int rd = bits (insn, 12, 15);
5905 unsigned int op = bits (insn, 21, 24);
5906 int is_mov = (op == 0xd);
5907 ULONGEST rd_val, rn_val;
cca44b1b
JB
5908
5909 if (!insn_references_pc (insn, 0x000ff000ul))
7ff120b4 5910 return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
cca44b1b 5911
136821d9
SM
5912 displaced_debug_printf ("copying immediate %s insn %.8lx",
5913 is_mov ? "move" : "ALU",
5914 (unsigned long) insn);
cca44b1b
JB
5915
5916 /* Instruction is of form:
5917
5918 <op><cond> rd, [rn,] #imm
5919
5920 Rewrite as:
5921
5922 Preparation: tmp1, tmp2 <- r0, r1;
5923 r0, r1 <- rd, rn
5924 Insn: <op><cond> r0, r1, #imm
5925 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
5926 */
5927
36073a92
YQ
5928 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5929 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5930 rn_val = displaced_read_reg (regs, dsc, rn);
5931 rd_val = displaced_read_reg (regs, dsc, rd);
cca44b1b
JB
5932 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5933 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5934 dsc->rd = rd;
5935
5936 if (is_mov)
5937 dsc->modinsn[0] = insn & 0xfff00fff;
5938 else
5939 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
5940
5941 dsc->cleanup = &cleanup_alu_imm;
5942
5943 return 0;
5944}
5945
34518530
YQ
5946static int
5947thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
5948 uint16_t insn2, struct regcache *regs,
1152d984 5949 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
5950{
5951 unsigned int op = bits (insn1, 5, 8);
5952 unsigned int rn, rm, rd;
5953 ULONGEST rd_val, rn_val;
5954
5955 rn = bits (insn1, 0, 3); /* Rn */
5956 rm = bits (insn2, 0, 3); /* Rm */
5957 rd = bits (insn2, 8, 11); /* Rd */
5958
5959 /* This routine is only called for instruction MOV. */
5960 gdb_assert (op == 0x2 && rn == 0xf);
5961
5962 if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
5963 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);
5964
136821d9 5965 displaced_debug_printf ("copying reg %s insn %.4x%.4x", "ALU", insn1, insn2);
34518530
YQ
5966
5967 /* Instruction is of form:
5968
5969 <op><cond> rd, [rn,] #imm
5970
5971 Rewrite as:
5972
5973 Preparation: tmp1, tmp2 <- r0, r1;
5974 r0, r1 <- rd, rn
5975 Insn: <op><cond> r0, r1, #imm
5976 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
5977 */
5978
5979 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5980 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5981 rn_val = displaced_read_reg (regs, dsc, rn);
5982 rd_val = displaced_read_reg (regs, dsc, rd);
5983 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5984 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5985 dsc->rd = rd;
5986
5987 dsc->modinsn[0] = insn1;
5988 dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
5989 dsc->numinsns = 2;
5990
5991 dsc->cleanup = &cleanup_alu_imm;
5992
5993 return 0;
5994}
5995
cca44b1b
JB
5996/* Copy/cleanup arithmetic/logic insns with register RHS. */
5997
5998static void
6e39997a 5999cleanup_alu_reg (struct gdbarch *gdbarch,
1152d984 6000 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
6001{
6002 ULONGEST rd_val;
6003 int i;
6004
36073a92 6005 rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6006
6007 for (i = 0; i < 3; i++)
6008 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6009
6010 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6011}
6012
7ff120b4
YQ
6013static void
6014install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6015 arm_displaced_step_copy_insn_closure *dsc,
7ff120b4 6016 unsigned int rd, unsigned int rn, unsigned int rm)
cca44b1b 6017{
cca44b1b 6018 ULONGEST rd_val, rn_val, rm_val;
cca44b1b 6019
cca44b1b
JB
6020 /* Instruction is of form:
6021
6022 <op><cond> rd, [rn,] rm [, <shift>]
6023
6024 Rewrite as:
6025
6026 Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
6027 r0, r1, r2 <- rd, rn, rm
ef713951 6028 Insn: <op><cond> r0, [r1,] r2 [, <shift>]
cca44b1b
JB
6029 Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
6030 */
6031
36073a92
YQ
6032 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6033 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6034 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6035 rd_val = displaced_read_reg (regs, dsc, rd);
6036 rn_val = displaced_read_reg (regs, dsc, rn);
6037 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6038 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6039 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6040 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6041 dsc->rd = rd;
6042
7ff120b4
YQ
6043 dsc->cleanup = &cleanup_alu_reg;
6044}
6045
6046static int
6047arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
1152d984 6048 arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
6049{
6050 unsigned int op = bits (insn, 21, 24);
6051 int is_mov = (op == 0xd);
6052
6053 if (!insn_references_pc (insn, 0x000ff00ful))
6054 return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);
6055
136821d9
SM
6056 displaced_debug_printf ("copying reg %s insn %.8lx",
6057 is_mov ? "move" : "ALU", (unsigned long) insn);
7ff120b4 6058
cca44b1b
JB
6059 if (is_mov)
6060 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
6061 else
6062 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
6063
7ff120b4
YQ
6064 install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
6065 bits (insn, 0, 3));
cca44b1b
JB
6066 return 0;
6067}
6068
34518530
YQ
6069static int
6070thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
6071 struct regcache *regs,
1152d984 6072 arm_displaced_step_copy_insn_closure *dsc)
34518530 6073{
ef713951 6074 unsigned rm, rd;
34518530 6075
ef713951
YQ
6076 rm = bits (insn, 3, 6);
6077 rd = (bit (insn, 7) << 3) | bits (insn, 0, 2);
34518530 6078
ef713951 6079 if (rd != ARM_PC_REGNUM && rm != ARM_PC_REGNUM)
34518530
YQ
6080 return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);
6081
136821d9 6082 displaced_debug_printf ("copying ALU reg insn %.4x", (unsigned short) insn);
34518530 6083
ef713951 6084 dsc->modinsn[0] = ((insn & 0xff00) | 0x10);
34518530 6085
ef713951 6086 install_alu_reg (gdbarch, regs, dsc, rd, rd, rm);
34518530
YQ
6087
6088 return 0;
6089}
6090
cca44b1b
JB
6091/* Cleanup/copy arithmetic/logic insns with shifted register RHS. */
6092
6093static void
6e39997a 6094cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
cca44b1b 6095 struct regcache *regs,
1152d984 6096 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6097{
36073a92 6098 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6099 int i;
6100
6101 for (i = 0; i < 4; i++)
6102 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6103
6104 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6105}
6106
7ff120b4
YQ
6107static void
6108install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6109 arm_displaced_step_copy_insn_closure *dsc,
7ff120b4
YQ
6110 unsigned int rd, unsigned int rn, unsigned int rm,
6111 unsigned rs)
cca44b1b 6112{
7ff120b4 6113 int i;
cca44b1b 6114 ULONGEST rd_val, rn_val, rm_val, rs_val;
cca44b1b 6115
cca44b1b
JB
6116 /* Instruction is of form:
6117
6118 <op><cond> rd, [rn,] rm, <shift> rs
6119
6120 Rewrite as:
6121
6122 Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
6123 r0, r1, r2, r3 <- rd, rn, rm, rs
6124 Insn: <op><cond> r0, r1, r2, <shift> r3
6125 Cleanup: tmp5 <- r0
6126 r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
6127 rd <- tmp5
6128 */
6129
6130 for (i = 0; i < 4; i++)
36073a92 6131 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b 6132
36073a92
YQ
6133 rd_val = displaced_read_reg (regs, dsc, rd);
6134 rn_val = displaced_read_reg (regs, dsc, rn);
6135 rm_val = displaced_read_reg (regs, dsc, rm);
6136 rs_val = displaced_read_reg (regs, dsc, rs);
cca44b1b
JB
6137 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6138 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6139 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6140 displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
6141 dsc->rd = rd;
7ff120b4
YQ
6142 dsc->cleanup = &cleanup_alu_shifted_reg;
6143}
6144
6145static int
6146arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
6147 struct regcache *regs,
1152d984 6148 arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
6149{
6150 unsigned int op = bits (insn, 21, 24);
6151 int is_mov = (op == 0xd);
6152 unsigned int rd, rn, rm, rs;
6153
6154 if (!insn_references_pc (insn, 0x000fff0ful))
6155 return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
6156
136821d9
SM
6157 displaced_debug_printf ("copying shifted reg %s insn %.8lx",
6158 is_mov ? "move" : "ALU",
6159 (unsigned long) insn);
7ff120b4
YQ
6160
6161 rn = bits (insn, 16, 19);
6162 rm = bits (insn, 0, 3);
6163 rs = bits (insn, 8, 11);
6164 rd = bits (insn, 12, 15);
cca44b1b
JB
6165
6166 if (is_mov)
6167 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
6168 else
6169 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
6170
7ff120b4 6171 install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);
cca44b1b
JB
6172
6173 return 0;
6174}
6175
6176/* Clean up load instructions. */
6177
6178static void
6e39997a 6179cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6180 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
6181{
6182 ULONGEST rt_val, rt_val2 = 0, rn_val;
cca44b1b 6183
36073a92 6184 rt_val = displaced_read_reg (regs, dsc, 0);
cca44b1b 6185 if (dsc->u.ldst.xfersize == 8)
36073a92
YQ
6186 rt_val2 = displaced_read_reg (regs, dsc, 1);
6187 rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
6188
6189 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6190 if (dsc->u.ldst.xfersize > 4)
6191 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6192 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6193 if (!dsc->u.ldst.immed)
6194 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6195
6196 /* Handle register writeback. */
6197 if (dsc->u.ldst.writeback)
6198 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6199 /* Put result in right place. */
6200 displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
6201 if (dsc->u.ldst.xfersize == 8)
6202 displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
6203}
6204
6205/* Clean up store instructions. */
6206
6207static void
6e39997a 6208cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6209 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6210{
36073a92 6211 ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
6212
6213 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6214 if (dsc->u.ldst.xfersize > 4)
6215 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6216 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6217 if (!dsc->u.ldst.immed)
6218 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6219 if (!dsc->u.ldst.restore_r4)
6220 displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
6221
6222 /* Writeback. */
6223 if (dsc->u.ldst.writeback)
6224 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6225}
6226
6227/* Copy "extra" load/store instructions. These are halfword/doubleword
6228 transfers, which have a different encoding to byte/word transfers. */
6229
6230static int
550dc4e2 6231arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unprivileged,
1152d984 6232 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
6233{
6234 unsigned int op1 = bits (insn, 20, 24);
6235 unsigned int op2 = bits (insn, 5, 6);
6236 unsigned int rt = bits (insn, 12, 15);
6237 unsigned int rn = bits (insn, 16, 19);
6238 unsigned int rm = bits (insn, 0, 3);
6239 char load[12] = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
6240 char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
6241 int immed = (op1 & 0x4) != 0;
6242 int opcode;
6243 ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
cca44b1b
JB
6244
6245 if (!insn_references_pc (insn, 0x000ff00ful))
7ff120b4 6246 return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);
cca44b1b 6247
136821d9
SM
6248 displaced_debug_printf ("copying %sextra load/store insn %.8lx",
6249 unprivileged ? "unprivileged " : "",
6250 (unsigned long) insn);
cca44b1b
JB
6251
6252 opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
6253
6254 if (opcode < 0)
6255 internal_error (__FILE__, __LINE__,
6256 _("copy_extra_ld_st: instruction decode error"));
6257
36073a92
YQ
6258 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6259 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6260 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 6261 if (!immed)
36073a92 6262 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 6263
36073a92 6264 rt_val = displaced_read_reg (regs, dsc, rt);
cca44b1b 6265 if (bytesize[opcode] == 8)
36073a92
YQ
6266 rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
6267 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 6268 if (!immed)
36073a92 6269 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6270
6271 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6272 if (bytesize[opcode] == 8)
6273 displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
6274 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6275 if (!immed)
6276 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
6277
6278 dsc->rd = rt;
6279 dsc->u.ldst.xfersize = bytesize[opcode];
6280 dsc->u.ldst.rn = rn;
6281 dsc->u.ldst.immed = immed;
6282 dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
6283 dsc->u.ldst.restore_r4 = 0;
6284
6285 if (immed)
6286 /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
6287 ->
6288 {ldr,str}<width><cond> r0, [r1,] [r2, #imm]. */
6289 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6290 else
6291 /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
6292 ->
6293 {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3]. */
6294 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6295
6296 dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
6297
6298 return 0;
6299}
6300
0f6f04ba 6301/* Copy byte/half word/word loads and stores. */
cca44b1b 6302
7ff120b4 6303static void
0f6f04ba 6304install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6305 arm_displaced_step_copy_insn_closure *dsc, int load,
0f6f04ba
YQ
6306 int immed, int writeback, int size, int usermode,
6307 int rt, int rm, int rn)
cca44b1b 6308{
cca44b1b 6309 ULONGEST rt_val, rn_val, rm_val = 0;
cca44b1b 6310
36073a92
YQ
6311 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6312 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 6313 if (!immed)
36073a92 6314 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 6315 if (!load)
36073a92 6316 dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);
cca44b1b 6317
36073a92
YQ
6318 rt_val = displaced_read_reg (regs, dsc, rt);
6319 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 6320 if (!immed)
36073a92 6321 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6322
6323 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6324 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6325 if (!immed)
6326 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
cca44b1b 6327 dsc->rd = rt;
0f6f04ba 6328 dsc->u.ldst.xfersize = size;
cca44b1b
JB
6329 dsc->u.ldst.rn = rn;
6330 dsc->u.ldst.immed = immed;
7ff120b4 6331 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
6332
6333 /* To write PC we can do:
6334
494e194e
YQ
6335 Before this sequence of instructions:
6336 r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
85102364 6337 r2 is the Rn value got from displaced_read_reg.
494e194e
YQ
6338
6339 Insn1: push {pc} Write address of STR instruction + offset on stack
6340 Insn2: pop {r4} Read it back from stack, r4 = addr(Insn1) + offset
6341 Insn3: sub r4, r4, pc r4 = addr(Insn1) + offset - pc
dda83cd7
SM
6342 = addr(Insn1) + offset - addr(Insn3) - 8
6343 = offset - 16
494e194e
YQ
6344 Insn4: add r4, r4, #8 r4 = offset - 8
6345 Insn5: add r0, r0, r4 r0 = from + 8 + offset - 8
dda83cd7 6346 = from + offset
494e194e 6347 Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])
cca44b1b
JB
6348
6349 Otherwise we don't know what value to write for PC, since the offset is
494e194e
YQ
6350 architecture-dependent (sometimes PC+8, sometimes PC+12). More details
6351 of this can be found in Section "Saving from r15" in
6352 http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */
cca44b1b 6353
7ff120b4
YQ
6354 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6355}
6356
34518530
YQ
6357
6358static int
6359thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
6360 uint16_t insn2, struct regcache *regs,
1152d984 6361 arm_displaced_step_copy_insn_closure *dsc, int size)
34518530
YQ
6362{
6363 unsigned int u_bit = bit (insn1, 7);
6364 unsigned int rt = bits (insn2, 12, 15);
6365 int imm12 = bits (insn2, 0, 11);
6366 ULONGEST pc_val;
6367
136821d9
SM
6368 displaced_debug_printf ("copying ldr pc (0x%x) R%d %c imm12 %.4x",
6369 (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
6370 imm12);
34518530
YQ
6371
6372 if (!u_bit)
6373 imm12 = -1 * imm12;
6374
6375 /* Rewrite instruction LDR Rt imm12 into:
6376
6377 Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12
6378
6379 LDR R0, R2, R3,
6380
6381 Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2]. */
6382
6383
6384 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6385 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6386 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6387
6388 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6389
6390 pc_val = pc_val & 0xfffffffc;
6391
6392 displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
6393 displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);
6394
6395 dsc->rd = rt;
6396
6397 dsc->u.ldst.xfersize = size;
6398 dsc->u.ldst.immed = 0;
6399 dsc->u.ldst.writeback = 0;
6400 dsc->u.ldst.restore_r4 = 0;
6401
6402 /* LDR R0, R2, R3 */
6403 dsc->modinsn[0] = 0xf852;
6404 dsc->modinsn[1] = 0x3;
6405 dsc->numinsns = 2;
6406
6407 dsc->cleanup = &cleanup_load;
6408
6409 return 0;
6410}
6411
6412static int
6413thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
6414 uint16_t insn2, struct regcache *regs,
1152d984 6415 arm_displaced_step_copy_insn_closure *dsc,
34518530
YQ
6416 int writeback, int immed)
6417{
6418 unsigned int rt = bits (insn2, 12, 15);
6419 unsigned int rn = bits (insn1, 0, 3);
6420 unsigned int rm = bits (insn2, 0, 3); /* Only valid if !immed. */
6421 /* In LDR (register), there is also a register Rm, which is not allowed to
6422 be PC, so we don't have to check it. */
6423
6424 if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
6425 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
6426 dsc);
6427
136821d9
SM
6428 displaced_debug_printf ("copying ldr r%d [r%d] insn %.4x%.4x",
6429 rt, rn, insn1, insn2);
34518530
YQ
6430
6431 install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
6432 0, rt, rm, rn);
6433
6434 dsc->u.ldst.restore_r4 = 0;
6435
6436 if (immed)
6437 /* ldr[b]<cond> rt, [rn, #imm], etc.
6438 ->
6439 ldr[b]<cond> r0, [r2, #imm]. */
6440 {
6441 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6442 dsc->modinsn[1] = insn2 & 0x0fff;
6443 }
6444 else
6445 /* ldr[b]<cond> rt, [rn, rm], etc.
6446 ->
6447 ldr[b]<cond> r0, [r2, r3]. */
6448 {
6449 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6450 dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
6451 }
6452
6453 dsc->numinsns = 2;
6454
6455 return 0;
6456}
6457
6458
7ff120b4
YQ
6459static int
6460arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
6461 struct regcache *regs,
1152d984 6462 arm_displaced_step_copy_insn_closure *dsc,
0f6f04ba 6463 int load, int size, int usermode)
7ff120b4
YQ
6464{
6465 int immed = !bit (insn, 25);
6466 int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
6467 unsigned int rt = bits (insn, 12, 15);
6468 unsigned int rn = bits (insn, 16, 19);
6469 unsigned int rm = bits (insn, 0, 3); /* Only valid if !immed. */
6470
6471 if (!insn_references_pc (insn, 0x000ff00ful))
6472 return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);
6473
136821d9
SM
6474 displaced_debug_printf ("copying %s%s r%d [r%d] insn %.8lx",
6475 load ? (size == 1 ? "ldrb" : "ldr")
6476 : (size == 1 ? "strb" : "str"),
6477 usermode ? "t" : "",
6478 rt, rn,
6479 (unsigned long) insn);
7ff120b4 6480
0f6f04ba
YQ
6481 install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
6482 usermode, rt, rm, rn);
7ff120b4 6483
bf9f652a 6484 if (load || rt != ARM_PC_REGNUM)
cca44b1b
JB
6485 {
6486 dsc->u.ldst.restore_r4 = 0;
6487
6488 if (immed)
6489 /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
6490 ->
6491 {ldr,str}[b]<cond> r0, [r2, #imm]. */
6492 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6493 else
6494 /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
6495 ->
6496 {ldr,str}[b]<cond> r0, [r2, r3]. */
6497 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6498 }
6499 else
6500 {
6501 /* We need to use r4 as scratch. Make sure it's restored afterwards. */
6502 dsc->u.ldst.restore_r4 = 1;
494e194e
YQ
6503 dsc->modinsn[0] = 0xe92d8000; /* push {pc} */
6504 dsc->modinsn[1] = 0xe8bd0010; /* pop {r4} */
cca44b1b
JB
6505 dsc->modinsn[2] = 0xe044400f; /* sub r4, r4, pc. */
6506 dsc->modinsn[3] = 0xe2844008; /* add r4, r4, #8. */
6507 dsc->modinsn[4] = 0xe0800004; /* add r0, r0, r4. */
6508
6509 /* As above. */
6510 if (immed)
6511 dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
6512 else
6513 dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
6514
cca44b1b
JB
6515 dsc->numinsns = 6;
6516 }
6517
6518 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6519
6520 return 0;
6521}
6522
6523/* Cleanup LDM instructions with fully-populated register list. This is an
6524 unfortunate corner case: it's impossible to implement correctly by modifying
6525 the instruction. The issue is as follows: we have an instruction,
6526
6527 ldm rN, {r0-r15}
6528
6529 which we must rewrite to avoid loading PC. A possible solution would be to
6530 do the load in two halves, something like (with suitable cleanup
6531 afterwards):
6532
6533 mov r8, rN
6534 ldm[id][ab] r8!, {r0-r7}
6535 str r7, <temp>
6536 ldm[id][ab] r8, {r7-r14}
6537 <bkpt>
6538
6539 but at present there's no suitable place for <temp>, since the scratch space
6540 is overwritten before the cleanup routine is called. For now, we simply
6541 emulate the instruction. */
6542
6543static void
6544cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6545 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6546{
cca44b1b
JB
6547 int inc = dsc->u.block.increment;
6548 int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
6549 int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
6550 uint32_t regmask = dsc->u.block.regmask;
6551 int regno = inc ? 0 : 15;
6552 CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
6553 int exception_return = dsc->u.block.load && dsc->u.block.user
6554 && (regmask & 0x8000) != 0;
36073a92 6555 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
6556 int do_transfer = condition_true (dsc->u.block.cond, status);
6557 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6558
6559 if (!do_transfer)
6560 return;
6561
6562 /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
6563 sensible we can do here. Complain loudly. */
6564 if (exception_return)
6565 error (_("Cannot single-step exception return"));
6566
6567 /* We don't handle any stores here for now. */
6568 gdb_assert (dsc->u.block.load != 0);
6569
136821d9
SM
6570 displaced_debug_printf ("emulating block transfer: %s %s %s",
6571 dsc->u.block.load ? "ldm" : "stm",
6572 dsc->u.block.increment ? "inc" : "dec",
6573 dsc->u.block.before ? "before" : "after");
cca44b1b
JB
6574
6575 while (regmask)
6576 {
6577 uint32_t memword;
6578
6579 if (inc)
bf9f652a 6580 while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
cca44b1b
JB
6581 regno++;
6582 else
6583 while (regno >= 0 && (regmask & (1 << regno)) == 0)
6584 regno--;
6585
6586 xfer_addr += bump_before;
6587
6588 memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
6589 displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
6590
6591 xfer_addr += bump_after;
6592
6593 regmask &= ~(1 << regno);
6594 }
6595
6596 if (dsc->u.block.writeback)
6597 displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
6598 CANNOT_WRITE_PC);
6599}
6600
6601/* Clean up an STM which included the PC in the register list. */
6602
6603static void
6604cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6605 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6606{
36073a92 6607 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b 6608 int store_executed = condition_true (dsc->u.block.cond, status);
5f661e03
SM
6609 CORE_ADDR pc_stored_at, transferred_regs
6610 = count_one_bits (dsc->u.block.regmask);
cca44b1b
JB
6611 CORE_ADDR stm_insn_addr;
6612 uint32_t pc_val;
6613 long offset;
6614 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6615
6616 /* If condition code fails, there's nothing else to do. */
6617 if (!store_executed)
6618 return;
6619
6620 if (dsc->u.block.increment)
6621 {
6622 pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
6623
6624 if (dsc->u.block.before)
6625 pc_stored_at += 4;
6626 }
6627 else
6628 {
6629 pc_stored_at = dsc->u.block.xfer_addr;
6630
6631 if (dsc->u.block.before)
6632 pc_stored_at -= 4;
6633 }
6634
6635 pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
6636 stm_insn_addr = dsc->scratch_base;
6637 offset = pc_val - stm_insn_addr;
6638
136821d9
SM
6639 displaced_debug_printf ("detected PC offset %.8lx for STM instruction",
6640 offset);
cca44b1b
JB
6641
6642 /* Rewrite the stored PC to the proper value for the non-displaced original
6643 instruction. */
6644 write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
6645 dsc->insn_addr + offset);
6646}
6647
6648/* Clean up an LDM which includes the PC in the register list. We clumped all
6649 the registers in the transferred list into a contiguous range r0...rX (to
6650 avoid loading PC directly and losing control of the debugged program), so we
6651 must undo that here. */
6652
6653static void
6e39997a 6654cleanup_block_load_pc (struct gdbarch *gdbarch,
cca44b1b 6655 struct regcache *regs,
1152d984 6656 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6657{
36073a92 6658 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
22e048c9 6659 int load_executed = condition_true (dsc->u.block.cond, status);
bf9f652a 6660 unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
5f661e03 6661 unsigned int regs_loaded = count_one_bits (mask);
cca44b1b
JB
6662 unsigned int num_to_shuffle = regs_loaded, clobbered;
6663
6664 /* The method employed here will fail if the register list is fully populated
6665 (we need to avoid loading PC directly). */
6666 gdb_assert (num_to_shuffle < 16);
6667
6668 if (!load_executed)
6669 return;
6670
6671 clobbered = (1 << num_to_shuffle) - 1;
6672
6673 while (num_to_shuffle > 0)
6674 {
6675 if ((mask & (1 << write_reg)) != 0)
6676 {
6677 unsigned int read_reg = num_to_shuffle - 1;
6678
6679 if (read_reg != write_reg)
6680 {
36073a92 6681 ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
cca44b1b 6682 displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
136821d9
SM
6683 displaced_debug_printf ("LDM: move loaded register r%d to r%d",
6684 read_reg, write_reg);
cca44b1b 6685 }
136821d9
SM
6686 else
6687 displaced_debug_printf ("LDM: register r%d already in the right "
6688 "place", write_reg);
cca44b1b
JB
6689
6690 clobbered &= ~(1 << write_reg);
6691
6692 num_to_shuffle--;
6693 }
6694
6695 write_reg--;
6696 }
6697
6698 /* Restore any registers we scribbled over. */
6699 for (write_reg = 0; clobbered != 0; write_reg++)
6700 {
6701 if ((clobbered & (1 << write_reg)) != 0)
6702 {
6703 displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
6704 CANNOT_WRITE_PC);
136821d9
SM
6705 displaced_debug_printf ("LDM: restored clobbered register r%d",
6706 write_reg);
cca44b1b
JB
6707 clobbered &= ~(1 << write_reg);
6708 }
6709 }
6710
6711 /* Perform register writeback manually. */
6712 if (dsc->u.block.writeback)
6713 {
6714 ULONGEST new_rn_val = dsc->u.block.xfer_addr;
6715
6716 if (dsc->u.block.increment)
6717 new_rn_val += regs_loaded * 4;
6718 else
6719 new_rn_val -= regs_loaded * 4;
6720
6721 displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
6722 CANNOT_WRITE_PC);
6723 }
6724}
6725
6726/* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
6727 in user-level code (in particular exception return, ldm rn, {...pc}^). */
6728
6729static int
7ff120b4
YQ
6730arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
6731 struct regcache *regs,
1152d984 6732 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
6733{
6734 int load = bit (insn, 20);
6735 int user = bit (insn, 22);
6736 int increment = bit (insn, 23);
6737 int before = bit (insn, 24);
6738 int writeback = bit (insn, 21);
6739 int rn = bits (insn, 16, 19);
cca44b1b 6740
0963b4bd
MS
6741 /* Block transfers which don't mention PC can be run directly
6742 out-of-line. */
bf9f652a 6743 if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
7ff120b4 6744 return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
cca44b1b 6745
bf9f652a 6746 if (rn == ARM_PC_REGNUM)
cca44b1b 6747 {
0963b4bd
MS
6748 warning (_("displaced: Unpredictable LDM or STM with "
6749 "base register r15"));
7ff120b4 6750 return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
cca44b1b
JB
6751 }
6752
136821d9
SM
6753 displaced_debug_printf ("copying block transfer insn %.8lx",
6754 (unsigned long) insn);
cca44b1b 6755
36073a92 6756 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b
JB
6757 dsc->u.block.rn = rn;
6758
6759 dsc->u.block.load = load;
6760 dsc->u.block.user = user;
6761 dsc->u.block.increment = increment;
6762 dsc->u.block.before = before;
6763 dsc->u.block.writeback = writeback;
6764 dsc->u.block.cond = bits (insn, 28, 31);
6765
6766 dsc->u.block.regmask = insn & 0xffff;
6767
6768 if (load)
6769 {
6770 if ((insn & 0xffff) == 0xffff)
6771 {
6772 /* LDM with a fully-populated register list. This case is
6773 particularly tricky. Implement for now by fully emulating the
6774 instruction (which might not behave perfectly in all cases, but
6775 these instructions should be rare enough for that not to matter
6776 too much). */
6777 dsc->modinsn[0] = ARM_NOP;
6778
6779 dsc->cleanup = &cleanup_block_load_all;
6780 }
6781 else
6782 {
6783 /* LDM of a list of registers which includes PC. Implement by
6784 rewriting the list of registers to be transferred into a
6785 contiguous chunk r0...rX before doing the transfer, then shuffling
6786 registers into the correct places in the cleanup routine. */
6787 unsigned int regmask = insn & 0xffff;
5f661e03 6788 unsigned int num_in_list = count_one_bits (regmask), new_regmask;
bec2ab5a 6789 unsigned int i;
cca44b1b
JB
6790
6791 for (i = 0; i < num_in_list; i++)
36073a92 6792 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b
JB
6793
6794 /* Writeback makes things complicated. We need to avoid clobbering
6795 the base register with one of the registers in our modified
6796 register list, but just using a different register can't work in
6797 all cases, e.g.:
6798
6799 ldm r14!, {r0-r13,pc}
6800
6801 which would need to be rewritten as:
6802
6803 ldm rN!, {r0-r14}
6804
6805 but that can't work, because there's no free register for N.
6806
6807 Solve this by turning off the writeback bit, and emulating
6808 writeback manually in the cleanup routine. */
6809
6810 if (writeback)
6811 insn &= ~(1 << 21);
6812
6813 new_regmask = (1 << num_in_list) - 1;
6814
136821d9
SM
6815 displaced_debug_printf ("LDM r%d%s, {..., pc}: original reg list "
6816 "%.4x, modified list %.4x",
6817 rn, writeback ? "!" : "",
6818 (int) insn & 0xffff, new_regmask);
cca44b1b
JB
6819
6820 dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
6821
6822 dsc->cleanup = &cleanup_block_load_pc;
6823 }
6824 }
6825 else
6826 {
6827 /* STM of a list of registers which includes PC. Run the instruction
6828 as-is, but out of line: this will store the wrong value for the PC,
6829 so we must manually fix up the memory in the cleanup routine.
6830 Doing things this way has the advantage that we can auto-detect
6831 the offset of the PC write (which is architecture-dependent) in
6832 the cleanup routine. */
6833 dsc->modinsn[0] = insn;
6834
6835 dsc->cleanup = &cleanup_block_store_pc;
6836 }
6837
6838 return 0;
6839}
6840
34518530
YQ
6841static int
6842thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
6843 struct regcache *regs,
1152d984 6844 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6845{
34518530
YQ
6846 int rn = bits (insn1, 0, 3);
6847 int load = bit (insn1, 4);
6848 int writeback = bit (insn1, 5);
cca44b1b 6849
34518530
YQ
6850 /* Block transfers which don't mention PC can be run directly
6851 out-of-line. */
6852 if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
6853 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);
7ff120b4 6854
34518530
YQ
6855 if (rn == ARM_PC_REGNUM)
6856 {
6857 warning (_("displaced: Unpredictable LDM or STM with "
6858 "base register r15"));
6859 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6860 "unpredictable ldm/stm", dsc);
6861 }
cca44b1b 6862
136821d9
SM
6863 displaced_debug_printf ("copying block transfer insn %.4x%.4x",
6864 insn1, insn2);
cca44b1b 6865
34518530
YQ
6866 /* Clear bit 13, since it should be always zero. */
6867 dsc->u.block.regmask = (insn2 & 0xdfff);
6868 dsc->u.block.rn = rn;
cca44b1b 6869
34518530
YQ
6870 dsc->u.block.load = load;
6871 dsc->u.block.user = 0;
6872 dsc->u.block.increment = bit (insn1, 7);
6873 dsc->u.block.before = bit (insn1, 8);
6874 dsc->u.block.writeback = writeback;
6875 dsc->u.block.cond = INST_AL;
6876 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b 6877
34518530
YQ
6878 if (load)
6879 {
6880 if (dsc->u.block.regmask == 0xffff)
6881 {
6882 /* This branch is impossible to happen. */
6883 gdb_assert (0);
6884 }
6885 else
6886 {
6887 unsigned int regmask = dsc->u.block.regmask;
5f661e03 6888 unsigned int num_in_list = count_one_bits (regmask), new_regmask;
bec2ab5a 6889 unsigned int i;
34518530
YQ
6890
6891 for (i = 0; i < num_in_list; i++)
6892 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
6893
6894 if (writeback)
6895 insn1 &= ~(1 << 5);
6896
6897 new_regmask = (1 << num_in_list) - 1;
6898
136821d9
SM
6899 displaced_debug_printf ("LDM r%d%s, {..., pc}: original reg list "
6900 "%.4x, modified list %.4x",
6901 rn, writeback ? "!" : "",
6902 (int) dsc->u.block.regmask, new_regmask);
34518530
YQ
6903
6904 dsc->modinsn[0] = insn1;
6905 dsc->modinsn[1] = (new_regmask & 0xffff);
6906 dsc->numinsns = 2;
6907
6908 dsc->cleanup = &cleanup_block_load_pc;
6909 }
6910 }
6911 else
6912 {
6913 dsc->modinsn[0] = insn1;
6914 dsc->modinsn[1] = insn2;
6915 dsc->numinsns = 2;
6916 dsc->cleanup = &cleanup_block_store_pc;
6917 }
6918 return 0;
6919}
6920
d9311bfa
AT
6921/* Wrapper over read_memory_unsigned_integer for use in arm_get_next_pcs.
6922 This is used to avoid a dependency on BFD's bfd_endian enum. */
6923
6924ULONGEST
6925arm_get_next_pcs_read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
6926 int byte_order)
6927{
5f2dfcfd
AT
6928 return read_memory_unsigned_integer (memaddr, len,
6929 (enum bfd_endian) byte_order);
d9311bfa
AT
6930}
6931
6932/* Wrapper over gdbarch_addr_bits_remove for use in arm_get_next_pcs. */
6933
6934CORE_ADDR
6935arm_get_next_pcs_addr_bits_remove (struct arm_get_next_pcs *self,
6936 CORE_ADDR val)
6937{
ac7936df 6938 return gdbarch_addr_bits_remove (self->regcache->arch (), val);
d9311bfa
AT
6939}
6940
6941/* Wrapper over syscall_next_pc for use in get_next_pcs. */
6942
e7cf25a8 6943static CORE_ADDR
553cb527 6944arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self)
d9311bfa 6945{
d9311bfa
AT
6946 return 0;
6947}
6948
6949/* Wrapper over arm_is_thumb for use in arm_get_next_pcs. */
6950
6951int
6952arm_get_next_pcs_is_thumb (struct arm_get_next_pcs *self)
6953{
6954 return arm_is_thumb (self->regcache);
6955}
6956
6957/* single_step() is called just before we want to resume the inferior,
6958 if we want to single-step it but there is no hardware or kernel
6959 single-step support. We find the target of the coming instructions
6960 and breakpoint them. */
6961
a0ff9e1a 6962std::vector<CORE_ADDR>
f5ea389a 6963arm_software_single_step (struct regcache *regcache)
d9311bfa 6964{
ac7936df 6965 struct gdbarch *gdbarch = regcache->arch ();
d9311bfa 6966 struct arm_get_next_pcs next_pcs_ctx;
d9311bfa
AT
6967
6968 arm_get_next_pcs_ctor (&next_pcs_ctx,
6969 &arm_get_next_pcs_ops,
6970 gdbarch_byte_order (gdbarch),
6971 gdbarch_byte_order_for_code (gdbarch),
1b451dda 6972 0,
d9311bfa
AT
6973 regcache);
6974
a0ff9e1a 6975 std::vector<CORE_ADDR> next_pcs = arm_get_next_pcs (&next_pcs_ctx);
d9311bfa 6976
a0ff9e1a
SM
6977 for (CORE_ADDR &pc_ref : next_pcs)
6978 pc_ref = gdbarch_addr_bits_remove (gdbarch, pc_ref);
d9311bfa 6979
93f9a11f 6980 return next_pcs;
d9311bfa
AT
6981}
6982
34518530
YQ
6983/* Cleanup/copy SVC (SWI) instructions. These two functions are overridden
6984 for Linux, where some SVC instructions must be treated specially. */
6985
6986static void
6987cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6988 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
6989{
6990 CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
6991
136821d9
SM
6992 displaced_debug_printf ("cleanup for svc, resume at %.8lx",
6993 (unsigned long) resume_addr);
34518530
YQ
6994
6995 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
6996}
6997
6998
85102364 6999/* Common copy routine for svc instruction. */
34518530
YQ
7000
7001static int
7002install_svc (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 7003 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7004{
7005 /* Preparation: none.
7006 Insn: unmodified svc.
7007 Cleanup: pc <- insn_addr + insn_size. */
7008
7009 /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
7010 instruction. */
7011 dsc->wrote_to_pc = 1;
7012
7013 /* Allow OS-specific code to override SVC handling. */
bd18283a
YQ
7014 if (dsc->u.svc.copy_svc_os)
7015 return dsc->u.svc.copy_svc_os (gdbarch, regs, dsc);
7016 else
7017 {
7018 dsc->cleanup = &cleanup_svc;
7019 return 0;
7020 }
34518530
YQ
7021}
7022
7023static int
7024arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
1152d984 7025 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7026{
7027
136821d9
SM
7028 displaced_debug_printf ("copying svc insn %.8lx",
7029 (unsigned long) insn);
34518530
YQ
7030
7031 dsc->modinsn[0] = insn;
7032
7033 return install_svc (gdbarch, regs, dsc);
7034}
7035
7036static int
7037thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
1152d984 7038 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7039{
7040
136821d9 7041 displaced_debug_printf ("copying svc insn %.4x", insn);
bd18283a 7042
34518530
YQ
7043 dsc->modinsn[0] = insn;
7044
7045 return install_svc (gdbarch, regs, dsc);
cca44b1b
JB
7046}
7047
7048/* Copy undefined instructions. */
7049
7050static int
7ff120b4 7051arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
1152d984 7052 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 7053{
136821d9
SM
7054 displaced_debug_printf ("copying undefined insn %.8lx",
7055 (unsigned long) insn);
cca44b1b
JB
7056
7057 dsc->modinsn[0] = insn;
7058
7059 return 0;
7060}
7061
34518530
YQ
7062static int
7063thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
1152d984 7064 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7065{
7066
136821d9
SM
7067 displaced_debug_printf ("copying undefined insn %.4x %.4x",
7068 (unsigned short) insn1, (unsigned short) insn2);
34518530
YQ
7069
7070 dsc->modinsn[0] = insn1;
7071 dsc->modinsn[1] = insn2;
7072 dsc->numinsns = 2;
7073
7074 return 0;
7075}
7076
cca44b1b
JB
7077/* Copy unpredictable instructions. */
7078
7079static int
7ff120b4 7080arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
1152d984 7081 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 7082{
136821d9
SM
7083 displaced_debug_printf ("copying unpredictable insn %.8lx",
7084 (unsigned long) insn);
cca44b1b
JB
7085
7086 dsc->modinsn[0] = insn;
7087
7088 return 0;
7089}
7090
7091/* The decode_* functions are instruction decoding helpers. They mostly follow
7092 the presentation in the ARM ARM. */
7093
7094static int
7ff120b4
YQ
7095arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
7096 struct regcache *regs,
1152d984 7097 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7098{
7099 unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
7100 unsigned int rn = bits (insn, 16, 19);
7101
2f924de6 7102 if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0x1) == 0x0)
7ff120b4 7103 return arm_copy_unmodified (gdbarch, insn, "cps", dsc);
2f924de6 7104 else if (op1 == 0x10 && op2 == 0x0 && (rn & 0x1) == 0x1)
7ff120b4 7105 return arm_copy_unmodified (gdbarch, insn, "setend", dsc);
cca44b1b 7106 else if ((op1 & 0x60) == 0x20)
7ff120b4 7107 return arm_copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
cca44b1b 7108 else if ((op1 & 0x71) == 0x40)
7ff120b4
YQ
7109 return arm_copy_unmodified (gdbarch, insn, "neon elt/struct load/store",
7110 dsc);
cca44b1b 7111 else if ((op1 & 0x77) == 0x41)
7ff120b4 7112 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 7113 else if ((op1 & 0x77) == 0x45)
7ff120b4 7114 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pli. */
cca44b1b
JB
7115 else if ((op1 & 0x77) == 0x51)
7116 {
7117 if (rn != 0xf)
7ff120b4 7118 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b 7119 else
7ff120b4 7120 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7121 }
7122 else if ((op1 & 0x77) == 0x55)
7ff120b4 7123 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b
JB
7124 else if (op1 == 0x57)
7125 switch (op2)
7126 {
7ff120b4
YQ
7127 case 0x1: return arm_copy_unmodified (gdbarch, insn, "clrex", dsc);
7128 case 0x4: return arm_copy_unmodified (gdbarch, insn, "dsb", dsc);
7129 case 0x5: return arm_copy_unmodified (gdbarch, insn, "dmb", dsc);
7130 case 0x6: return arm_copy_unmodified (gdbarch, insn, "isb", dsc);
7131 default: return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7132 }
7133 else if ((op1 & 0x63) == 0x43)
7ff120b4 7134 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7135 else if ((op2 & 0x1) == 0x0)
7136 switch (op1 & ~0x80)
7137 {
7138 case 0x61:
7ff120b4 7139 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 7140 case 0x65:
7ff120b4 7141 return arm_copy_preload_reg (gdbarch, insn, regs, dsc); /* pli reg. */
cca44b1b 7142 case 0x71: case 0x75:
dda83cd7 7143 /* pld/pldw reg. */
7ff120b4 7144 return arm_copy_preload_reg (gdbarch, insn, regs, dsc);
cca44b1b 7145 case 0x63: case 0x67: case 0x73: case 0x77:
7ff120b4 7146 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b 7147 default:
7ff120b4 7148 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7149 }
7150 else
7ff120b4 7151 return arm_copy_undef (gdbarch, insn, dsc); /* Probably unreachable. */
cca44b1b
JB
7152}
7153
7154static int
7ff120b4
YQ
7155arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
7156 struct regcache *regs,
1152d984 7157 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7158{
7159 if (bit (insn, 27) == 0)
7ff120b4 7160 return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
cca44b1b
JB
7161 /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx. */
7162 else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
7163 {
7164 case 0x0: case 0x2:
7ff120b4 7165 return arm_copy_unmodified (gdbarch, insn, "srs", dsc);
cca44b1b
JB
7166
7167 case 0x1: case 0x3:
7ff120b4 7168 return arm_copy_unmodified (gdbarch, insn, "rfe", dsc);
cca44b1b
JB
7169
7170 case 0x4: case 0x5: case 0x6: case 0x7:
7ff120b4 7171 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b
JB
7172
7173 case 0x8:
7174 switch ((insn & 0xe00000) >> 21)
7175 {
7176 case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
7177 /* stc/stc2. */
7ff120b4 7178 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7179
7180 case 0x2:
7ff120b4 7181 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b
JB
7182
7183 default:
7ff120b4 7184 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7185 }
7186
7187 case 0x9:
7188 {
7189 int rn_f = (bits (insn, 16, 19) == 0xf);
7190 switch ((insn & 0xe00000) >> 21)
7191 {
7192 case 0x1: case 0x3:
7193 /* ldc/ldc2 imm (undefined for rn == pc). */
7ff120b4
YQ
7194 return rn_f ? arm_copy_undef (gdbarch, insn, dsc)
7195 : arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7196
7197 case 0x2:
7ff120b4 7198 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
7199
7200 case 0x4: case 0x5: case 0x6: case 0x7:
7201 /* ldc/ldc2 lit (undefined for rn != pc). */
7ff120b4
YQ
7202 return rn_f ? arm_copy_copro_load_store (gdbarch, insn, regs, dsc)
7203 : arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7204
7205 default:
7ff120b4 7206 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7207 }
7208 }
7209
7210 case 0xa:
7ff120b4 7211 return arm_copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
cca44b1b
JB
7212
7213 case 0xb:
7214 if (bits (insn, 16, 19) == 0xf)
dda83cd7 7215 /* ldc/ldc2 lit. */
7ff120b4 7216 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 7217 else
7ff120b4 7218 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7219
7220 case 0xc:
7221 if (bit (insn, 4))
7ff120b4 7222 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 7223 else
7ff120b4 7224 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7225
7226 case 0xd:
7227 if (bit (insn, 4))
7ff120b4 7228 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 7229 else
7ff120b4 7230 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7231
7232 default:
7ff120b4 7233 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7234 }
7235}
7236
7237/* Decode miscellaneous instructions in dp/misc encoding space. */
7238
7239static int
7ff120b4
YQ
7240arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
7241 struct regcache *regs,
1152d984 7242 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7243{
7244 unsigned int op2 = bits (insn, 4, 6);
7245 unsigned int op = bits (insn, 21, 22);
cca44b1b
JB
7246
7247 switch (op2)
7248 {
7249 case 0x0:
7ff120b4 7250 return arm_copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
cca44b1b
JB
7251
7252 case 0x1:
7253 if (op == 0x1) /* bx. */
7ff120b4 7254 return arm_copy_bx_blx_reg (gdbarch, insn, regs, dsc);
cca44b1b 7255 else if (op == 0x3)
7ff120b4 7256 return arm_copy_unmodified (gdbarch, insn, "clz", dsc);
cca44b1b 7257 else
7ff120b4 7258 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7259
7260 case 0x2:
7261 if (op == 0x1)
dda83cd7 7262 /* Not really supported. */
7ff120b4 7263 return arm_copy_unmodified (gdbarch, insn, "bxj", dsc);
cca44b1b 7264 else
7ff120b4 7265 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7266
7267 case 0x3:
7268 if (op == 0x1)
7ff120b4 7269 return arm_copy_bx_blx_reg (gdbarch, insn,
0963b4bd 7270 regs, dsc); /* blx register. */
cca44b1b 7271 else
7ff120b4 7272 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7273
7274 case 0x5:
7ff120b4 7275 return arm_copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
cca44b1b
JB
7276
7277 case 0x7:
7278 if (op == 0x1)
7ff120b4 7279 return arm_copy_unmodified (gdbarch, insn, "bkpt", dsc);
cca44b1b 7280 else if (op == 0x3)
dda83cd7 7281 /* Not really supported. */
7ff120b4 7282 return arm_copy_unmodified (gdbarch, insn, "smc", dsc);
86a73007 7283 /* Fall through. */
cca44b1b
JB
7284
7285 default:
7ff120b4 7286 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7287 }
7288}
7289
7290static int
7ff120b4
YQ
7291arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
7292 struct regcache *regs,
1152d984 7293 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7294{
7295 if (bit (insn, 25))
7296 switch (bits (insn, 20, 24))
7297 {
7298 case 0x10:
7ff120b4 7299 return arm_copy_unmodified (gdbarch, insn, "movw", dsc);
cca44b1b
JB
7300
7301 case 0x14:
7ff120b4 7302 return arm_copy_unmodified (gdbarch, insn, "movt", dsc);
cca44b1b
JB
7303
7304 case 0x12: case 0x16:
7ff120b4 7305 return arm_copy_unmodified (gdbarch, insn, "msr imm", dsc);
cca44b1b
JB
7306
7307 default:
7ff120b4 7308 return arm_copy_alu_imm (gdbarch, insn, regs, dsc);
cca44b1b
JB
7309 }
7310 else
7311 {
7312 uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
7313
7314 if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
7ff120b4 7315 return arm_copy_alu_reg (gdbarch, insn, regs, dsc);
cca44b1b 7316 else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
7ff120b4 7317 return arm_copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
cca44b1b 7318 else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
7ff120b4 7319 return arm_decode_miscellaneous (gdbarch, insn, regs, dsc);
cca44b1b 7320 else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
7ff120b4 7321 return arm_copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
cca44b1b 7322 else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
7ff120b4 7323 return arm_copy_unmodified (gdbarch, insn, "mul/mla", dsc);
cca44b1b 7324 else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
7ff120b4 7325 return arm_copy_unmodified (gdbarch, insn, "synch", dsc);
cca44b1b 7326 else if (op2 == 0xb || (op2 & 0xd) == 0xd)
550dc4e2 7327 /* 2nd arg means "unprivileged". */
7ff120b4
YQ
7328 return arm_copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
7329 dsc);
cca44b1b
JB
7330 }
7331
7332 /* Should be unreachable. */
7333 return 1;
7334}
7335
7336static int
7ff120b4
YQ
7337arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
7338 struct regcache *regs,
1152d984 7339 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7340{
7341 int a = bit (insn, 25), b = bit (insn, 4);
7342 uint32_t op1 = bits (insn, 20, 24);
cca44b1b
JB
7343
7344 if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
7345 || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
0f6f04ba 7346 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 0);
cca44b1b
JB
7347 else if ((!a && (op1 & 0x17) == 0x02)
7348 || (a && (op1 & 0x17) == 0x02 && !b))
0f6f04ba 7349 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 1);
cca44b1b
JB
7350 else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
7351 || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
0f6f04ba 7352 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 0);
cca44b1b
JB
7353 else if ((!a && (op1 & 0x17) == 0x03)
7354 || (a && (op1 & 0x17) == 0x03 && !b))
0f6f04ba 7355 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 1);
cca44b1b
JB
7356 else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
7357 || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
7ff120b4 7358 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
cca44b1b
JB
7359 else if ((!a && (op1 & 0x17) == 0x06)
7360 || (a && (op1 & 0x17) == 0x06 && !b))
7ff120b4 7361 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
cca44b1b
JB
7362 else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
7363 || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
7ff120b4 7364 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
cca44b1b
JB
7365 else if ((!a && (op1 & 0x17) == 0x07)
7366 || (a && (op1 & 0x17) == 0x07 && !b))
7ff120b4 7367 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
cca44b1b
JB
7368
7369 /* Should be unreachable. */
7370 return 1;
7371}
7372
7373static int
7ff120b4 7374arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
1152d984 7375 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7376{
7377 switch (bits (insn, 20, 24))
7378 {
7379 case 0x00: case 0x01: case 0x02: case 0x03:
7ff120b4 7380 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
cca44b1b
JB
7381
7382 case 0x04: case 0x05: case 0x06: case 0x07:
7ff120b4 7383 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
cca44b1b
JB
7384
7385 case 0x08: case 0x09: case 0x0a: case 0x0b:
7386 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
7ff120b4 7387 return arm_copy_unmodified (gdbarch, insn,
cca44b1b
JB
7388 "decode/pack/unpack/saturate/reverse", dsc);
7389
7390 case 0x18:
7391 if (bits (insn, 5, 7) == 0) /* op2. */
7392 {
7393 if (bits (insn, 12, 15) == 0xf)
7ff120b4 7394 return arm_copy_unmodified (gdbarch, insn, "usad8", dsc);
cca44b1b 7395 else
7ff120b4 7396 return arm_copy_unmodified (gdbarch, insn, "usada8", dsc);
cca44b1b
JB
7397 }
7398 else
7ff120b4 7399 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7400
7401 case 0x1a: case 0x1b:
7402 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 7403 return arm_copy_unmodified (gdbarch, insn, "sbfx", dsc);
cca44b1b 7404 else
7ff120b4 7405 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7406
7407 case 0x1c: case 0x1d:
7408 if (bits (insn, 5, 6) == 0x0) /* op2[1:0]. */
7409 {
7410 if (bits (insn, 0, 3) == 0xf)
7ff120b4 7411 return arm_copy_unmodified (gdbarch, insn, "bfc", dsc);
cca44b1b 7412 else
7ff120b4 7413 return arm_copy_unmodified (gdbarch, insn, "bfi", dsc);
cca44b1b
JB
7414 }
7415 else
7ff120b4 7416 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7417
7418 case 0x1e: case 0x1f:
7419 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 7420 return arm_copy_unmodified (gdbarch, insn, "ubfx", dsc);
cca44b1b 7421 else
7ff120b4 7422 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7423 }
7424
7425 /* Should be unreachable. */
7426 return 1;
7427}
7428
7429static int
615234c1 7430arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, uint32_t insn,
7ff120b4 7431 struct regcache *regs,
1152d984 7432 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7433{
7434 if (bit (insn, 25))
7ff120b4 7435 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b 7436 else
7ff120b4 7437 return arm_copy_block_xfer (gdbarch, insn, regs, dsc);
cca44b1b
JB
7438}
7439
7440static int
7ff120b4
YQ
7441arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
7442 struct regcache *regs,
1152d984 7443 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7444{
7445 unsigned int opcode = bits (insn, 20, 24);
7446
7447 switch (opcode)
7448 {
7449 case 0x04: case 0x05: /* VFP/Neon mrrc/mcrr. */
7ff120b4 7450 return arm_copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
cca44b1b
JB
7451
7452 case 0x08: case 0x0a: case 0x0c: case 0x0e:
7453 case 0x12: case 0x16:
7ff120b4 7454 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
cca44b1b
JB
7455
7456 case 0x09: case 0x0b: case 0x0d: case 0x0f:
7457 case 0x13: case 0x17:
7ff120b4 7458 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
cca44b1b
JB
7459
7460 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7461 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7462 /* Note: no writeback for these instructions. Bit 25 will always be
7463 zero though (via caller), so the following works OK. */
7ff120b4 7464 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7465 }
7466
7467 /* Should be unreachable. */
7468 return 1;
7469}
7470
34518530
YQ
7471/* Decode shifted register instructions. */
7472
7473static int
7474thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
7475 uint16_t insn2, struct regcache *regs,
1152d984 7476 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7477{
7478 /* PC is only allowed to be used in instruction MOV. */
7479
7480 unsigned int op = bits (insn1, 5, 8);
7481 unsigned int rn = bits (insn1, 0, 3);
7482
7483 if (op == 0x2 && rn == 0xf) /* MOV */
7484 return thumb2_copy_alu_imm (gdbarch, insn1, insn2, regs, dsc);
7485 else
7486 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7487 "dp (shift reg)", dsc);
7488}
7489
7490
7491/* Decode extension register load/store. Exactly the same as
7492 arm_decode_ext_reg_ld_st. */
7493
7494static int
7495thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
7496 uint16_t insn2, struct regcache *regs,
1152d984 7497 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7498{
7499 unsigned int opcode = bits (insn1, 4, 8);
7500
7501 switch (opcode)
7502 {
7503 case 0x04: case 0x05:
7504 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7505 "vfp/neon vmov", dsc);
7506
7507 case 0x08: case 0x0c: /* 01x00 */
7508 case 0x0a: case 0x0e: /* 01x10 */
7509 case 0x12: case 0x16: /* 10x10 */
7510 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7511 "vfp/neon vstm/vpush", dsc);
7512
7513 case 0x09: case 0x0d: /* 01x01 */
7514 case 0x0b: case 0x0f: /* 01x11 */
7515 case 0x13: case 0x17: /* 10x11 */
7516 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7517 "vfp/neon vldm/vpop", dsc);
7518
7519 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7520 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7521 "vstr", dsc);
7522 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7523 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2, regs, dsc);
7524 }
7525
7526 /* Should be unreachable. */
7527 return 1;
7528}
7529
cca44b1b 7530static int
12545665 7531arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn,
1152d984 7532 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7533{
7534 unsigned int op1 = bits (insn, 20, 25);
7535 int op = bit (insn, 4);
7536 unsigned int coproc = bits (insn, 8, 11);
cca44b1b
JB
7537
7538 if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
7ff120b4 7539 return arm_decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
cca44b1b
JB
7540 else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
7541 && (coproc & 0xe) != 0xa)
7542 /* stc/stc2. */
7ff120b4 7543 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7544 else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
7545 && (coproc & 0xe) != 0xa)
7546 /* ldc/ldc2 imm/lit. */
7ff120b4 7547 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 7548 else if ((op1 & 0x3e) == 0x00)
7ff120b4 7549 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b 7550 else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
7ff120b4 7551 return arm_copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
cca44b1b 7552 else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
7ff120b4 7553 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b 7554 else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
7ff120b4 7555 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
7556 else if ((op1 & 0x30) == 0x20 && !op)
7557 {
7558 if ((coproc & 0xe) == 0xa)
7ff120b4 7559 return arm_copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
cca44b1b 7560 else
7ff120b4 7561 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7562 }
7563 else if ((op1 & 0x30) == 0x20 && op)
7ff120b4 7564 return arm_copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
cca44b1b 7565 else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
7ff120b4 7566 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 7567 else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
7ff120b4 7568 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 7569 else if ((op1 & 0x30) == 0x30)
7ff120b4 7570 return arm_copy_svc (gdbarch, insn, regs, dsc);
cca44b1b 7571 else
7ff120b4 7572 return arm_copy_undef (gdbarch, insn, dsc); /* Possibly unreachable. */
cca44b1b
JB
7573}
7574
34518530
YQ
7575static int
7576thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
7577 uint16_t insn2, struct regcache *regs,
1152d984 7578 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7579{
7580 unsigned int coproc = bits (insn2, 8, 11);
34518530
YQ
7581 unsigned int bit_5_8 = bits (insn1, 5, 8);
7582 unsigned int bit_9 = bit (insn1, 9);
7583 unsigned int bit_4 = bit (insn1, 4);
34518530
YQ
7584
7585 if (bit_9 == 0)
7586 {
7587 if (bit_5_8 == 2)
7588 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7589 "neon 64bit xfer/mrrc/mrrc2/mcrr/mcrr2",
7590 dsc);
7591 else if (bit_5_8 == 0) /* UNDEFINED. */
7592 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
7593 else
7594 {
7595 /*coproc is 101x. SIMD/VFP, ext registers load/store. */
7596 if ((coproc & 0xe) == 0xa)
7597 return thumb2_decode_ext_reg_ld_st (gdbarch, insn1, insn2, regs,
7598 dsc);
7599 else /* coproc is not 101x. */
7600 {
7601 if (bit_4 == 0) /* STC/STC2. */
7602 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7603 "stc/stc2", dsc);
405feb71 7604 else /* LDC/LDC2 {literal, immediate}. */
34518530
YQ
7605 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2,
7606 regs, dsc);
7607 }
7608 }
7609 }
7610 else
7611 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "coproc", dsc);
7612
7613 return 0;
7614}
7615
7616static void
7617install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 7618 arm_displaced_step_copy_insn_closure *dsc, int rd)
34518530
YQ
7619{
7620 /* ADR Rd, #imm
7621
7622 Rewrite as:
7623
7624 Preparation: Rd <- PC
7625 Insn: ADD Rd, #imm
7626 Cleanup: Null.
7627 */
7628
7629 /* Rd <- PC */
7630 int val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
7631 displaced_write_reg (regs, dsc, rd, val, CANNOT_WRITE_PC);
7632}
7633
7634static int
7635thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 7636 arm_displaced_step_copy_insn_closure *dsc,
34518530
YQ
7637 int rd, unsigned int imm)
7638{
7639
7640 /* Encoding T2: ADDS Rd, #imm */
7641 dsc->modinsn[0] = (0x3000 | (rd << 8) | imm);
7642
7643 install_pc_relative (gdbarch, regs, dsc, rd);
7644
7645 return 0;
7646}
7647
7648static int
7649thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
7650 struct regcache *regs,
1152d984 7651 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7652{
7653 unsigned int rd = bits (insn, 8, 10);
7654 unsigned int imm8 = bits (insn, 0, 7);
7655
136821d9
SM
7656 displaced_debug_printf ("copying thumb adr r%d, #%d insn %.4x",
7657 rd, imm8, insn);
34518530
YQ
7658
7659 return thumb_copy_pc_relative_16bit (gdbarch, regs, dsc, rd, imm8);
7660}
7661
7662static int
7663thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
7664 uint16_t insn2, struct regcache *regs,
1152d984 7665 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7666{
7667 unsigned int rd = bits (insn2, 8, 11);
7668 /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
7669 extract raw immediate encoding rather than computing immediate. When
7670 generating ADD or SUB instruction, we can simply perform OR operation to
7671 set immediate into ADD. */
7672 unsigned int imm_3_8 = insn2 & 0x70ff;
7673 unsigned int imm_i = insn1 & 0x0400; /* Clear all bits except bit 10. */
7674
136821d9
SM
7675 displaced_debug_printf ("copying thumb adr r%d, #%d:%d insn %.4x%.4x",
7676 rd, imm_i, imm_3_8, insn1, insn2);
34518530
YQ
7677
7678 if (bit (insn1, 7)) /* Encoding T2 */
7679 {
7680 /* Encoding T3: SUB Rd, Rd, #imm */
7681 dsc->modinsn[0] = (0xf1a0 | rd | imm_i);
7682 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
7683 }
7684 else /* Encoding T3 */
7685 {
7686 /* Encoding T3: ADD Rd, Rd, #imm */
7687 dsc->modinsn[0] = (0xf100 | rd | imm_i);
7688 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
7689 }
7690 dsc->numinsns = 2;
7691
7692 install_pc_relative (gdbarch, regs, dsc, rd);
7693
7694 return 0;
7695}
7696
7697static int
615234c1 7698thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, uint16_t insn1,
34518530 7699 struct regcache *regs,
1152d984 7700 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7701{
7702 unsigned int rt = bits (insn1, 8, 10);
7703 unsigned int pc;
7704 int imm8 = (bits (insn1, 0, 7) << 2);
34518530
YQ
7705
7706 /* LDR Rd, #imm8
7707
7708 Rwrite as:
7709
7710 Preparation: tmp0 <- R0, tmp2 <- R2, tmp3 <- R3, R2 <- PC, R3 <- #imm8;
7711
7712 Insn: LDR R0, [R2, R3];
7713 Cleanup: R2 <- tmp2, R3 <- tmp3, Rd <- R0, R0 <- tmp0 */
7714
136821d9 7715 displaced_debug_printf ("copying thumb ldr r%d [pc #%d]", rt, imm8);
34518530
YQ
7716
7717 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
7718 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
7719 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
7720 pc = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
7721 /* The assembler calculates the required value of the offset from the
7722 Align(PC,4) value of this instruction to the label. */
7723 pc = pc & 0xfffffffc;
7724
7725 displaced_write_reg (regs, dsc, 2, pc, CANNOT_WRITE_PC);
7726 displaced_write_reg (regs, dsc, 3, imm8, CANNOT_WRITE_PC);
7727
7728 dsc->rd = rt;
7729 dsc->u.ldst.xfersize = 4;
7730 dsc->u.ldst.rn = 0;
7731 dsc->u.ldst.immed = 0;
7732 dsc->u.ldst.writeback = 0;
7733 dsc->u.ldst.restore_r4 = 0;
7734
7735 dsc->modinsn[0] = 0x58d0; /* ldr r0, [r2, r3]*/
7736
7737 dsc->cleanup = &cleanup_load;
7738
7739 return 0;
7740}
7741
405feb71 7742/* Copy Thumb cbnz/cbz instruction. */
34518530
YQ
7743
7744static int
7745thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
7746 struct regcache *regs,
1152d984 7747 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7748{
7749 int non_zero = bit (insn1, 11);
7750 unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
7751 CORE_ADDR from = dsc->insn_addr;
7752 int rn = bits (insn1, 0, 2);
7753 int rn_val = displaced_read_reg (regs, dsc, rn);
7754
7755 dsc->u.branch.cond = (rn_val && non_zero) || (!rn_val && !non_zero);
7756 /* CBNZ and CBZ do not affect the condition flags. If condition is true,
7757 set it INST_AL, so cleanup_branch will know branch is taken, otherwise,
7758 condition is false, let it be, cleanup_branch will do nothing. */
7759 if (dsc->u.branch.cond)
7760 {
7761 dsc->u.branch.cond = INST_AL;
7762 dsc->u.branch.dest = from + 4 + imm5;
7763 }
7764 else
7765 dsc->u.branch.dest = from + 2;
7766
7767 dsc->u.branch.link = 0;
7768 dsc->u.branch.exchange = 0;
7769
136821d9
SM
7770 displaced_debug_printf ("copying %s [r%d = 0x%x] insn %.4x to %.8lx",
7771 non_zero ? "cbnz" : "cbz",
7772 rn, rn_val, insn1, dsc->u.branch.dest);
34518530
YQ
7773
7774 dsc->modinsn[0] = THUMB_NOP;
7775
7776 dsc->cleanup = &cleanup_branch;
7777 return 0;
7778}
7779
7780/* Copy Table Branch Byte/Halfword */
7781static int
7782thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
7783 uint16_t insn2, struct regcache *regs,
1152d984 7784 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7785{
7786 ULONGEST rn_val, rm_val;
7787 int is_tbh = bit (insn2, 4);
7788 CORE_ADDR halfwords = 0;
7789 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7790
7791 rn_val = displaced_read_reg (regs, dsc, bits (insn1, 0, 3));
7792 rm_val = displaced_read_reg (regs, dsc, bits (insn2, 0, 3));
7793
7794 if (is_tbh)
7795 {
7796 gdb_byte buf[2];
7797
7798 target_read_memory (rn_val + 2 * rm_val, buf, 2);
7799 halfwords = extract_unsigned_integer (buf, 2, byte_order);
7800 }
7801 else
7802 {
7803 gdb_byte buf[1];
7804
7805 target_read_memory (rn_val + rm_val, buf, 1);
7806 halfwords = extract_unsigned_integer (buf, 1, byte_order);
7807 }
7808
136821d9
SM
7809 displaced_debug_printf ("%s base 0x%x offset 0x%x offset 0x%x",
7810 is_tbh ? "tbh" : "tbb",
7811 (unsigned int) rn_val, (unsigned int) rm_val,
7812 (unsigned int) halfwords);
34518530
YQ
7813
7814 dsc->u.branch.cond = INST_AL;
7815 dsc->u.branch.link = 0;
7816 dsc->u.branch.exchange = 0;
7817 dsc->u.branch.dest = dsc->insn_addr + 4 + 2 * halfwords;
7818
7819 dsc->cleanup = &cleanup_branch;
7820
7821 return 0;
7822}
7823
7824static void
7825cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 7826 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7827{
7828 /* PC <- r7 */
7829 int val = displaced_read_reg (regs, dsc, 7);
7830 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, val, BX_WRITE_PC);
7831
7832 /* r7 <- r8 */
7833 val = displaced_read_reg (regs, dsc, 8);
7834 displaced_write_reg (regs, dsc, 7, val, CANNOT_WRITE_PC);
7835
7836 /* r8 <- tmp[0] */
7837 displaced_write_reg (regs, dsc, 8, dsc->tmp[0], CANNOT_WRITE_PC);
7838
7839}
7840
7841static int
615234c1 7842thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, uint16_t insn1,
34518530 7843 struct regcache *regs,
1152d984 7844 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7845{
7846 dsc->u.block.regmask = insn1 & 0x00ff;
7847
7848 /* Rewrite instruction: POP {rX, rY, ...,rZ, PC}
7849 to :
7850
7851 (1) register list is full, that is, r0-r7 are used.
7852 Prepare: tmp[0] <- r8
7853
7854 POP {r0, r1, ...., r6, r7}; remove PC from reglist
7855 MOV r8, r7; Move value of r7 to r8;
7856 POP {r7}; Store PC value into r7.
7857
7858 Cleanup: PC <- r7, r7 <- r8, r8 <-tmp[0]
7859
7860 (2) register list is not full, supposing there are N registers in
7861 register list (except PC, 0 <= N <= 7).
7862 Prepare: for each i, 0 - N, tmp[i] <- ri.
7863
7864 POP {r0, r1, ...., rN};
7865
7866 Cleanup: Set registers in original reglist from r0 - rN. Restore r0 - rN
7867 from tmp[] properly.
7868 */
136821d9
SM
7869 displaced_debug_printf ("copying thumb pop {%.8x, pc} insn %.4x",
7870 dsc->u.block.regmask, insn1);
34518530
YQ
7871
7872 if (dsc->u.block.regmask == 0xff)
7873 {
7874 dsc->tmp[0] = displaced_read_reg (regs, dsc, 8);
7875
7876 dsc->modinsn[0] = (insn1 & 0xfeff); /* POP {r0,r1,...,r6, r7} */
7877 dsc->modinsn[1] = 0x46b8; /* MOV r8, r7 */
7878 dsc->modinsn[2] = 0xbc80; /* POP {r7} */
7879
7880 dsc->numinsns = 3;
7881 dsc->cleanup = &cleanup_pop_pc_16bit_all;
7882 }
7883 else
7884 {
5f661e03 7885 unsigned int num_in_list = count_one_bits (dsc->u.block.regmask);
bec2ab5a
SM
7886 unsigned int i;
7887 unsigned int new_regmask;
34518530
YQ
7888
7889 for (i = 0; i < num_in_list + 1; i++)
7890 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7891
7892 new_regmask = (1 << (num_in_list + 1)) - 1;
7893
136821d9
SM
7894 displaced_debug_printf ("POP {..., pc}: original reg list %.4x, "
7895 "modified list %.4x",
7896 (int) dsc->u.block.regmask, new_regmask);
34518530
YQ
7897
7898 dsc->u.block.regmask |= 0x8000;
7899 dsc->u.block.writeback = 0;
7900 dsc->u.block.cond = INST_AL;
7901
7902 dsc->modinsn[0] = (insn1 & ~0x1ff) | (new_regmask & 0xff);
7903
7904 dsc->cleanup = &cleanup_block_load_pc;
7905 }
7906
7907 return 0;
7908}
7909
7910static void
7911thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
7912 struct regcache *regs,
1152d984 7913 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7914{
7915 unsigned short op_bit_12_15 = bits (insn1, 12, 15);
7916 unsigned short op_bit_10_11 = bits (insn1, 10, 11);
7917 int err = 0;
7918
7919 /* 16-bit thumb instructions. */
7920 switch (op_bit_12_15)
7921 {
7922 /* Shift (imme), add, subtract, move and compare. */
7923 case 0: case 1: case 2: case 3:
7924 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
7925 "shift/add/sub/mov/cmp",
7926 dsc);
7927 break;
7928 case 4:
7929 switch (op_bit_10_11)
7930 {
7931 case 0: /* Data-processing */
7932 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
7933 "data-processing",
7934 dsc);
7935 break;
7936 case 1: /* Special data instructions and branch and exchange. */
7937 {
7938 unsigned short op = bits (insn1, 7, 9);
7939 if (op == 6 || op == 7) /* BX or BLX */
7940 err = thumb_copy_bx_blx_reg (gdbarch, insn1, regs, dsc);
7941 else if (bits (insn1, 6, 7) != 0) /* ADD/MOV/CMP high registers. */
7942 err = thumb_copy_alu_reg (gdbarch, insn1, regs, dsc);
7943 else
7944 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "special data",
7945 dsc);
7946 }
7947 break;
7948 default: /* LDR (literal) */
7949 err = thumb_copy_16bit_ldr_literal (gdbarch, insn1, regs, dsc);
7950 }
7951 break;
7952 case 5: case 6: case 7: case 8: case 9: /* Load/Store single data item */
7953 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldr/str", dsc);
7954 break;
7955 case 10:
7956 if (op_bit_10_11 < 2) /* Generate PC-relative address */
7957 err = thumb_decode_pc_relative_16bit (gdbarch, insn1, regs, dsc);
7958 else /* Generate SP-relative address */
7959 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "sp-relative", dsc);
7960 break;
7961 case 11: /* Misc 16-bit instructions */
7962 {
7963 switch (bits (insn1, 8, 11))
7964 {
7965 case 1: case 3: case 9: case 11: /* CBNZ, CBZ */
7966 err = thumb_copy_cbnz_cbz (gdbarch, insn1, regs, dsc);
7967 break;
7968 case 12: case 13: /* POP */
7969 if (bit (insn1, 8)) /* PC is in register list. */
7970 err = thumb_copy_pop_pc_16bit (gdbarch, insn1, regs, dsc);
7971 else
7972 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "pop", dsc);
7973 break;
7974 case 15: /* If-Then, and hints */
7975 if (bits (insn1, 0, 3))
7976 /* If-Then makes up to four following instructions conditional.
7977 IT instruction itself is not conditional, so handle it as a
7978 common unmodified instruction. */
7979 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "If-Then",
7980 dsc);
7981 else
7982 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "hints", dsc);
7983 break;
7984 default:
7985 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "misc", dsc);
7986 }
7987 }
7988 break;
7989 case 12:
7990 if (op_bit_10_11 < 2) /* Store multiple registers */
7991 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "stm", dsc);
7992 else /* Load multiple registers */
7993 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldm", dsc);
7994 break;
7995 case 13: /* Conditional branch and supervisor call */
7996 if (bits (insn1, 9, 11) != 7) /* conditional branch */
7997 err = thumb_copy_b (gdbarch, insn1, dsc);
7998 else
7999 err = thumb_copy_svc (gdbarch, insn1, regs, dsc);
8000 break;
8001 case 14: /* Unconditional branch */
8002 err = thumb_copy_b (gdbarch, insn1, dsc);
8003 break;
8004 default:
8005 err = 1;
8006 }
8007
8008 if (err)
8009 internal_error (__FILE__, __LINE__,
8010 _("thumb_process_displaced_16bit_insn: Instruction decode error"));
8011}
8012
8013static int
8014decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
8015 uint16_t insn1, uint16_t insn2,
8016 struct regcache *regs,
1152d984 8017 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
8018{
8019 int rt = bits (insn2, 12, 15);
8020 int rn = bits (insn1, 0, 3);
8021 int op1 = bits (insn1, 7, 8);
34518530
YQ
8022
8023 switch (bits (insn1, 5, 6))
8024 {
8025 case 0: /* Load byte and memory hints */
8026 if (rt == 0xf) /* PLD/PLI */
8027 {
8028 if (rn == 0xf)
8029 /* PLD literal or Encoding T3 of PLI(immediate, literal). */
8030 return thumb2_copy_preload (gdbarch, insn1, insn2, regs, dsc);
8031 else
8032 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8033 "pli/pld", dsc);
8034 }
8035 else
8036 {
8037 if (rn == 0xf) /* LDRB/LDRSB (literal) */
8038 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8039 1);
8040 else
8041 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8042 "ldrb{reg, immediate}/ldrbt",
8043 dsc);
8044 }
8045
8046 break;
8047 case 1: /* Load halfword and memory hints. */
8048 if (rt == 0xf) /* PLD{W} and Unalloc memory hint. */
8049 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8050 "pld/unalloc memhint", dsc);
8051 else
8052 {
8053 if (rn == 0xf)
8054 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8055 2);
8056 else
8057 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8058 "ldrh/ldrht", dsc);
8059 }
8060 break;
8061 case 2: /* Load word */
8062 {
8063 int insn2_bit_8_11 = bits (insn2, 8, 11);
8064
8065 if (rn == 0xf)
8066 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc, 4);
8067 else if (op1 == 0x1) /* Encoding T3 */
8068 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs, dsc,
8069 0, 1);
8070 else /* op1 == 0x0 */
8071 {
8072 if (insn2_bit_8_11 == 0xc || (insn2_bit_8_11 & 0x9) == 0x9)
8073 /* LDR (immediate) */
8074 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8075 dsc, bit (insn2, 8), 1);
8076 else if (insn2_bit_8_11 == 0xe) /* LDRT */
8077 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8078 "ldrt", dsc);
8079 else
8080 /* LDR (register) */
8081 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8082 dsc, 0, 0);
8083 }
8084 break;
8085 }
8086 default:
8087 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
8088 break;
8089 }
8090 return 0;
8091}
8092
8093static void
8094thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8095 uint16_t insn2, struct regcache *regs,
1152d984 8096 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
8097{
8098 int err = 0;
8099 unsigned short op = bit (insn2, 15);
8100 unsigned int op1 = bits (insn1, 11, 12);
8101
8102 switch (op1)
8103 {
8104 case 1:
8105 {
8106 switch (bits (insn1, 9, 10))
8107 {
8108 case 0:
8109 if (bit (insn1, 6))
8110 {
405feb71 8111 /* Load/store {dual, exclusive}, table branch. */
34518530
YQ
8112 if (bits (insn1, 7, 8) == 1 && bits (insn1, 4, 5) == 1
8113 && bits (insn2, 5, 7) == 0)
8114 err = thumb2_copy_table_branch (gdbarch, insn1, insn2, regs,
8115 dsc);
8116 else
8117 /* PC is not allowed to use in load/store {dual, exclusive}
8118 instructions. */
8119 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8120 "load/store dual/ex", dsc);
8121 }
8122 else /* load/store multiple */
8123 {
8124 switch (bits (insn1, 7, 8))
8125 {
8126 case 0: case 3: /* SRS, RFE */
8127 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8128 "srs/rfe", dsc);
8129 break;
8130 case 1: case 2: /* LDM/STM/PUSH/POP */
8131 err = thumb2_copy_block_xfer (gdbarch, insn1, insn2, regs, dsc);
8132 break;
8133 }
8134 }
8135 break;
8136
8137 case 1:
8138 /* Data-processing (shift register). */
8139 err = thumb2_decode_dp_shift_reg (gdbarch, insn1, insn2, regs,
8140 dsc);
8141 break;
8142 default: /* Coprocessor instructions. */
8143 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8144 break;
8145 }
8146 break;
8147 }
8148 case 2: /* op1 = 2 */
8149 if (op) /* Branch and misc control. */
8150 {
8151 if (bit (insn2, 14) /* BLX/BL */
8152 || bit (insn2, 12) /* Unconditional branch */
8153 || (bits (insn1, 7, 9) != 0x7)) /* Conditional branch */
8154 err = thumb2_copy_b_bl_blx (gdbarch, insn1, insn2, regs, dsc);
8155 else
8156 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8157 "misc ctrl", dsc);
8158 }
8159 else
8160 {
8161 if (bit (insn1, 9)) /* Data processing (plain binary imm). */
8162 {
b926417a 8163 int dp_op = bits (insn1, 4, 8);
34518530 8164 int rn = bits (insn1, 0, 3);
b926417a 8165 if ((dp_op == 0 || dp_op == 0xa) && rn == 0xf)
34518530
YQ
8166 err = thumb_copy_pc_relative_32bit (gdbarch, insn1, insn2,
8167 regs, dsc);
8168 else
8169 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8170 "dp/pb", dsc);
8171 }
405feb71 8172 else /* Data processing (modified immediate) */
34518530
YQ
8173 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8174 "dp/mi", dsc);
8175 }
8176 break;
8177 case 3: /* op1 = 3 */
8178 switch (bits (insn1, 9, 10))
8179 {
8180 case 0:
8181 if (bit (insn1, 4))
8182 err = decode_thumb_32bit_ld_mem_hints (gdbarch, insn1, insn2,
8183 regs, dsc);
8184 else /* NEON Load/Store and Store single data item */
8185 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8186 "neon elt/struct load/store",
8187 dsc);
8188 break;
8189 case 1: /* op1 = 3, bits (9, 10) == 1 */
8190 switch (bits (insn1, 7, 8))
8191 {
8192 case 0: case 1: /* Data processing (register) */
8193 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8194 "dp(reg)", dsc);
8195 break;
8196 case 2: /* Multiply and absolute difference */
8197 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8198 "mul/mua/diff", dsc);
8199 break;
8200 case 3: /* Long multiply and divide */
8201 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8202 "lmul/lmua", dsc);
8203 break;
8204 }
8205 break;
8206 default: /* Coprocessor instructions */
8207 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8208 break;
8209 }
8210 break;
8211 default:
8212 err = 1;
8213 }
8214
8215 if (err)
8216 internal_error (__FILE__, __LINE__,
8217 _("thumb_process_displaced_32bit_insn: Instruction decode error"));
8218
8219}
8220
b434a28f
YQ
8221static void
8222thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
12545665 8223 struct regcache *regs,
1152d984 8224 arm_displaced_step_copy_insn_closure *dsc)
b434a28f 8225{
34518530
YQ
8226 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8227 uint16_t insn1
8228 = read_memory_unsigned_integer (from, 2, byte_order_for_code);
8229
136821d9
SM
8230 displaced_debug_printf ("process thumb insn %.4x at %.8lx",
8231 insn1, (unsigned long) from);
34518530
YQ
8232
8233 dsc->is_thumb = 1;
8234 dsc->insn_size = thumb_insn_size (insn1);
8235 if (thumb_insn_size (insn1) == 4)
8236 {
8237 uint16_t insn2
8238 = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
8239 thumb_process_displaced_32bit_insn (gdbarch, insn1, insn2, regs, dsc);
8240 }
8241 else
8242 thumb_process_displaced_16bit_insn (gdbarch, insn1, regs, dsc);
b434a28f
YQ
8243}
8244
cca44b1b 8245void
b434a28f
YQ
8246arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8247 CORE_ADDR to, struct regcache *regs,
1152d984 8248 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
8249{
8250 int err = 0;
b434a28f
YQ
8251 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8252 uint32_t insn;
cca44b1b
JB
8253
8254 /* Most displaced instructions use a 1-instruction scratch space, so set this
8255 here and override below if/when necessary. */
8256 dsc->numinsns = 1;
8257 dsc->insn_addr = from;
8258 dsc->scratch_base = to;
8259 dsc->cleanup = NULL;
8260 dsc->wrote_to_pc = 0;
8261
b434a28f 8262 if (!displaced_in_arm_mode (regs))
12545665 8263 return thumb_process_displaced_insn (gdbarch, from, regs, dsc);
b434a28f 8264
4db71c0b
YQ
8265 dsc->is_thumb = 0;
8266 dsc->insn_size = 4;
b434a28f 8267 insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
136821d9
SM
8268 displaced_debug_printf ("stepping insn %.8lx at %.8lx",
8269 (unsigned long) insn, (unsigned long) from);
b434a28f 8270
cca44b1b 8271 if ((insn & 0xf0000000) == 0xf0000000)
7ff120b4 8272 err = arm_decode_unconditional (gdbarch, insn, regs, dsc);
cca44b1b
JB
8273 else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
8274 {
8275 case 0x0: case 0x1: case 0x2: case 0x3:
7ff120b4 8276 err = arm_decode_dp_misc (gdbarch, insn, regs, dsc);
cca44b1b
JB
8277 break;
8278
8279 case 0x4: case 0x5: case 0x6:
7ff120b4 8280 err = arm_decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
cca44b1b
JB
8281 break;
8282
8283 case 0x7:
7ff120b4 8284 err = arm_decode_media (gdbarch, insn, dsc);
cca44b1b
JB
8285 break;
8286
8287 case 0x8: case 0x9: case 0xa: case 0xb:
7ff120b4 8288 err = arm_decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
cca44b1b
JB
8289 break;
8290
8291 case 0xc: case 0xd: case 0xe: case 0xf:
12545665 8292 err = arm_decode_svc_copro (gdbarch, insn, regs, dsc);
cca44b1b
JB
8293 break;
8294 }
8295
8296 if (err)
8297 internal_error (__FILE__, __LINE__,
8298 _("arm_process_displaced_insn: Instruction decode error"));
8299}
8300
8301/* Actually set up the scratch space for a displaced instruction. */
8302
8303void
8304arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
1152d984
SM
8305 CORE_ADDR to,
8306 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 8307{
345bd07c 8308 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
4db71c0b 8309 unsigned int i, len, offset;
cca44b1b 8310 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4db71c0b 8311 int size = dsc->is_thumb? 2 : 4;
948f8e3d 8312 const gdb_byte *bkp_insn;
cca44b1b 8313
4db71c0b 8314 offset = 0;
cca44b1b
JB
8315 /* Poke modified instruction(s). */
8316 for (i = 0; i < dsc->numinsns; i++)
8317 {
136821d9
SM
8318 if (size == 4)
8319 displaced_debug_printf ("writing insn %.8lx at %.8lx",
8320 dsc->modinsn[i], (unsigned long) to + offset);
8321 else if (size == 2)
8322 displaced_debug_printf ("writing insn %.4x at %.8lx",
8323 (unsigned short) dsc->modinsn[i],
8324 (unsigned long) to + offset);
4db71c0b 8325
4db71c0b
YQ
8326 write_memory_unsigned_integer (to + offset, size,
8327 byte_order_for_code,
cca44b1b 8328 dsc->modinsn[i]);
4db71c0b
YQ
8329 offset += size;
8330 }
8331
8332 /* Choose the correct breakpoint instruction. */
8333 if (dsc->is_thumb)
8334 {
8335 bkp_insn = tdep->thumb_breakpoint;
8336 len = tdep->thumb_breakpoint_size;
8337 }
8338 else
8339 {
8340 bkp_insn = tdep->arm_breakpoint;
8341 len = tdep->arm_breakpoint_size;
cca44b1b
JB
8342 }
8343
8344 /* Put breakpoint afterwards. */
4db71c0b 8345 write_memory (to + offset, bkp_insn, len);
cca44b1b 8346
136821d9
SM
8347 displaced_debug_printf ("copy %s->%s", paddress (gdbarch, from),
8348 paddress (gdbarch, to));
cca44b1b
JB
8349}
8350
cca44b1b
JB
8351/* Entry point for cleaning things up after a displaced instruction has been
8352 single-stepped. */
8353
8354void
8355arm_displaced_step_fixup (struct gdbarch *gdbarch,
1152d984 8356 struct displaced_step_copy_insn_closure *dsc_,
cca44b1b
JB
8357 CORE_ADDR from, CORE_ADDR to,
8358 struct regcache *regs)
8359{
1152d984
SM
8360 arm_displaced_step_copy_insn_closure *dsc
8361 = (arm_displaced_step_copy_insn_closure *) dsc_;
cfba9872 8362
cca44b1b
JB
8363 if (dsc->cleanup)
8364 dsc->cleanup (gdbarch, regs, dsc);
8365
8366 if (!dsc->wrote_to_pc)
4db71c0b
YQ
8367 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
8368 dsc->insn_addr + dsc->insn_size);
8369
cca44b1b
JB
8370}
8371
8372#include "bfd-in2.h"
8373#include "libcoff.h"
8374
8375static int
8376gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
8377{
f0c2e3e0
AB
8378 gdb_disassemble_info *di
8379 = static_cast<gdb_disassemble_info *> (info->application_data);
e47ad6c0 8380 struct gdbarch *gdbarch = di->arch ();
9779414d
DJ
8381
8382 if (arm_pc_is_thumb (gdbarch, memaddr))
cca44b1b
JB
8383 {
8384 static asymbol *asym;
8385 static combined_entry_type ce;
8386 static struct coff_symbol_struct csym;
8387 static struct bfd fake_bfd;
8388 static bfd_target fake_target;
8389
8390 if (csym.native == NULL)
8391 {
8392 /* Create a fake symbol vector containing a Thumb symbol.
8393 This is solely so that the code in print_insn_little_arm()
8394 and print_insn_big_arm() in opcodes/arm-dis.c will detect
8395 the presence of a Thumb symbol and switch to decoding
8396 Thumb instructions. */
8397
8398 fake_target.flavour = bfd_target_coff_flavour;
8399 fake_bfd.xvec = &fake_target;
8400 ce.u.syment.n_sclass = C_THUMBEXTFUNC;
8401 csym.native = &ce;
8402 csym.symbol.the_bfd = &fake_bfd;
8403 csym.symbol.name = "fake";
8404 asym = (asymbol *) & csym;
8405 }
8406
8407 memaddr = UNMAKE_THUMB_ADDR (memaddr);
8408 info->symbols = &asym;
8409 }
8410 else
8411 info->symbols = NULL;
8412
e60eb288
YQ
8413 /* GDB is able to get bfd_mach from the exe_bfd, info->mach is
8414 accurate, so mark USER_SPECIFIED_MACHINE_TYPE bit. Otherwise,
8415 opcodes/arm-dis.c:print_insn reset info->mach, and it will trigger
7e10abd1
TT
8416 the assert on the mismatch of info->mach and
8417 bfd_get_mach (current_program_space->exec_bfd ()) in
8418 default_print_insn. */
3047c786
TV
8419 if (current_program_space->exec_bfd () != NULL
8420 && (current_program_space->exec_bfd ()->arch_info
8421 == gdbarch_bfd_arch_info (gdbarch)))
e60eb288
YQ
8422 info->flags |= USER_SPECIFIED_MACHINE_TYPE;
8423
6394c606 8424 return default_print_insn (memaddr, info);
cca44b1b
JB
8425}
8426
8427/* The following define instruction sequences that will cause ARM
8428 cpu's to take an undefined instruction trap. These are used to
8429 signal a breakpoint to GDB.
8430
8431 The newer ARMv4T cpu's are capable of operating in ARM or Thumb
8432 modes. A different instruction is required for each mode. The ARM
8433 cpu's can also be big or little endian. Thus four different
8434 instructions are needed to support all cases.
8435
8436 Note: ARMv4 defines several new instructions that will take the
8437 undefined instruction trap. ARM7TDMI is nominally ARMv4T, but does
8438 not in fact add the new instructions. The new undefined
8439 instructions in ARMv4 are all instructions that had no defined
8440 behaviour in earlier chips. There is no guarantee that they will
8441 raise an exception, but may be treated as NOP's. In practice, it
8442 may only safe to rely on instructions matching:
8443
8444 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
8445 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
8446 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
8447
0963b4bd 8448 Even this may only true if the condition predicate is true. The
cca44b1b
JB
8449 following use a condition predicate of ALWAYS so it is always TRUE.
8450
8451 There are other ways of forcing a breakpoint. GNU/Linux, RISC iX,
8452 and NetBSD all use a software interrupt rather than an undefined
8453 instruction to force a trap. This can be handled by by the
8454 abi-specific code during establishment of the gdbarch vector. */
8455
8456#define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
8457#define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
8458#define THUMB_LE_BREAKPOINT {0xbe,0xbe}
8459#define THUMB_BE_BREAKPOINT {0xbe,0xbe}
8460
948f8e3d
PA
8461static const gdb_byte arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
8462static const gdb_byte arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
8463static const gdb_byte arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
8464static const gdb_byte arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
cca44b1b 8465
cd6c3b4f
YQ
8466/* Implement the breakpoint_kind_from_pc gdbarch method. */
8467
d19280ad
YQ
8468static int
8469arm_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
cca44b1b 8470{
345bd07c 8471 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
177321bd 8472 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
cca44b1b 8473
9779414d 8474 if (arm_pc_is_thumb (gdbarch, *pcptr))
cca44b1b
JB
8475 {
8476 *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
177321bd
DJ
8477
8478 /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
8479 check whether we are replacing a 32-bit instruction. */
8480 if (tdep->thumb2_breakpoint != NULL)
8481 {
8482 gdb_byte buf[2];
d19280ad 8483
177321bd
DJ
8484 if (target_read_memory (*pcptr, buf, 2) == 0)
8485 {
8486 unsigned short inst1;
d19280ad 8487
177321bd 8488 inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
db24da6d 8489 if (thumb_insn_size (inst1) == 4)
d19280ad 8490 return ARM_BP_KIND_THUMB2;
177321bd
DJ
8491 }
8492 }
8493
d19280ad 8494 return ARM_BP_KIND_THUMB;
cca44b1b
JB
8495 }
8496 else
d19280ad
YQ
8497 return ARM_BP_KIND_ARM;
8498
8499}
8500
cd6c3b4f
YQ
8501/* Implement the sw_breakpoint_from_kind gdbarch method. */
8502
d19280ad
YQ
8503static const gdb_byte *
8504arm_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
8505{
345bd07c 8506 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
d19280ad
YQ
8507
8508 switch (kind)
cca44b1b 8509 {
d19280ad
YQ
8510 case ARM_BP_KIND_ARM:
8511 *size = tdep->arm_breakpoint_size;
cca44b1b 8512 return tdep->arm_breakpoint;
d19280ad
YQ
8513 case ARM_BP_KIND_THUMB:
8514 *size = tdep->thumb_breakpoint_size;
8515 return tdep->thumb_breakpoint;
8516 case ARM_BP_KIND_THUMB2:
8517 *size = tdep->thumb2_breakpoint_size;
8518 return tdep->thumb2_breakpoint;
8519 default:
8520 gdb_assert_not_reached ("unexpected arm breakpoint kind");
cca44b1b
JB
8521 }
8522}
8523
833b7ab5
YQ
8524/* Implement the breakpoint_kind_from_current_state gdbarch method. */
8525
8526static int
8527arm_breakpoint_kind_from_current_state (struct gdbarch *gdbarch,
8528 struct regcache *regcache,
8529 CORE_ADDR *pcptr)
8530{
8531 gdb_byte buf[4];
8532
8533 /* Check the memory pointed by PC is readable. */
8534 if (target_read_memory (regcache_read_pc (regcache), buf, 4) == 0)
8535 {
8536 struct arm_get_next_pcs next_pcs_ctx;
833b7ab5
YQ
8537
8538 arm_get_next_pcs_ctor (&next_pcs_ctx,
8539 &arm_get_next_pcs_ops,
8540 gdbarch_byte_order (gdbarch),
8541 gdbarch_byte_order_for_code (gdbarch),
8542 0,
8543 regcache);
8544
a0ff9e1a 8545 std::vector<CORE_ADDR> next_pcs = arm_get_next_pcs (&next_pcs_ctx);
833b7ab5
YQ
8546
8547 /* If MEMADDR is the next instruction of current pc, do the
8548 software single step computation, and get the thumb mode by
8549 the destination address. */
a0ff9e1a 8550 for (CORE_ADDR pc : next_pcs)
833b7ab5
YQ
8551 {
8552 if (UNMAKE_THUMB_ADDR (pc) == *pcptr)
8553 {
833b7ab5
YQ
8554 if (IS_THUMB_ADDR (pc))
8555 {
8556 *pcptr = MAKE_THUMB_ADDR (*pcptr);
8557 return arm_breakpoint_kind_from_pc (gdbarch, pcptr);
8558 }
8559 else
8560 return ARM_BP_KIND_ARM;
8561 }
8562 }
833b7ab5
YQ
8563 }
8564
8565 return arm_breakpoint_kind_from_pc (gdbarch, pcptr);
8566}
8567
cca44b1b
JB
8568/* Extract from an array REGBUF containing the (raw) register state a
8569 function return value of type TYPE, and copy that, in virtual
8570 format, into VALBUF. */
8571
8572static void
8573arm_extract_return_value (struct type *type, struct regcache *regs,
8574 gdb_byte *valbuf)
8575{
ac7936df 8576 struct gdbarch *gdbarch = regs->arch ();
cca44b1b 8577 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
345bd07c 8578 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
cca44b1b 8579
78134374 8580 if (TYPE_CODE_FLT == type->code ())
cca44b1b 8581 {
345bd07c 8582 switch (tdep->fp_model)
cca44b1b
JB
8583 {
8584 case ARM_FLOAT_FPA:
8585 {
8586 /* The value is in register F0 in internal format. We need to
8587 extract the raw value and then convert it to the desired
8588 internal type. */
f0452268 8589 bfd_byte tmpbuf[ARM_FP_REGISTER_SIZE];
cca44b1b 8590
dca08e1f 8591 regs->cooked_read (ARM_F0_REGNUM, tmpbuf);
3b2ca824
UW
8592 target_float_convert (tmpbuf, arm_ext_type (gdbarch),
8593 valbuf, type);
cca44b1b
JB
8594 }
8595 break;
8596
8597 case ARM_FLOAT_SOFT_FPA:
8598 case ARM_FLOAT_SOFT_VFP:
8599 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8600 not using the VFP ABI code. */
8601 case ARM_FLOAT_VFP:
dca08e1f 8602 regs->cooked_read (ARM_A1_REGNUM, valbuf);
cca44b1b 8603 if (TYPE_LENGTH (type) > 4)
f0452268
AH
8604 regs->cooked_read (ARM_A1_REGNUM + 1,
8605 valbuf + ARM_INT_REGISTER_SIZE);
cca44b1b
JB
8606 break;
8607
8608 default:
0963b4bd
MS
8609 internal_error (__FILE__, __LINE__,
8610 _("arm_extract_return_value: "
8611 "Floating point model not supported"));
cca44b1b
JB
8612 break;
8613 }
8614 }
78134374
SM
8615 else if (type->code () == TYPE_CODE_INT
8616 || type->code () == TYPE_CODE_CHAR
8617 || type->code () == TYPE_CODE_BOOL
8618 || type->code () == TYPE_CODE_PTR
aa006118 8619 || TYPE_IS_REFERENCE (type)
a6617193
JB
8620 || type->code () == TYPE_CODE_ENUM
8621 || is_fixed_point_type (type))
cca44b1b 8622 {
b021a221
MS
8623 /* If the type is a plain integer, then the access is
8624 straight-forward. Otherwise we have to play around a bit
8625 more. */
cca44b1b
JB
8626 int len = TYPE_LENGTH (type);
8627 int regno = ARM_A1_REGNUM;
8628 ULONGEST tmp;
8629
8630 while (len > 0)
8631 {
8632 /* By using store_unsigned_integer we avoid having to do
8633 anything special for small big-endian values. */
8634 regcache_cooked_read_unsigned (regs, regno++, &tmp);
8635 store_unsigned_integer (valbuf,
f0452268
AH
8636 (len > ARM_INT_REGISTER_SIZE
8637 ? ARM_INT_REGISTER_SIZE : len),
cca44b1b 8638 byte_order, tmp);
f0452268
AH
8639 len -= ARM_INT_REGISTER_SIZE;
8640 valbuf += ARM_INT_REGISTER_SIZE;
cca44b1b
JB
8641 }
8642 }
8643 else
8644 {
8645 /* For a structure or union the behaviour is as if the value had
dda83cd7
SM
8646 been stored to word-aligned memory and then loaded into
8647 registers with 32-bit load instruction(s). */
cca44b1b
JB
8648 int len = TYPE_LENGTH (type);
8649 int regno = ARM_A1_REGNUM;
f0452268 8650 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
cca44b1b
JB
8651
8652 while (len > 0)
8653 {
dca08e1f 8654 regs->cooked_read (regno++, tmpbuf);
cca44b1b 8655 memcpy (valbuf, tmpbuf,
f0452268
AH
8656 len > ARM_INT_REGISTER_SIZE ? ARM_INT_REGISTER_SIZE : len);
8657 len -= ARM_INT_REGISTER_SIZE;
8658 valbuf += ARM_INT_REGISTER_SIZE;
cca44b1b
JB
8659 }
8660 }
8661}
8662
8663
8664/* Will a function return an aggregate type in memory or in a
8665 register? Return 0 if an aggregate type can be returned in a
8666 register, 1 if it must be returned in memory. */
8667
8668static int
8669arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
8670{
cca44b1b
JB
8671 enum type_code code;
8672
f168693b 8673 type = check_typedef (type);
cca44b1b 8674
b13c8ab2
YQ
8675 /* Simple, non-aggregate types (ie not including vectors and
8676 complex) are always returned in a register (or registers). */
78134374 8677 code = type->code ();
b13c8ab2
YQ
8678 if (TYPE_CODE_STRUCT != code && TYPE_CODE_UNION != code
8679 && TYPE_CODE_ARRAY != code && TYPE_CODE_COMPLEX != code)
8680 return 0;
cca44b1b 8681
bd63c870 8682 if (TYPE_CODE_ARRAY == code && type->is_vector ())
c4312b19
YQ
8683 {
8684 /* Vector values should be returned using ARM registers if they
8685 are not over 16 bytes. */
8686 return (TYPE_LENGTH (type) > 16);
8687 }
8688
345bd07c
SM
8689 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
8690 if (tdep->arm_abi != ARM_ABI_APCS)
cca44b1b 8691 {
b13c8ab2
YQ
8692 /* The AAPCS says all aggregates not larger than a word are returned
8693 in a register. */
bab22d06
LM
8694 if (TYPE_LENGTH (type) <= ARM_INT_REGISTER_SIZE
8695 && language_pass_by_reference (type).trivially_copyable)
b13c8ab2
YQ
8696 return 0;
8697
cca44b1b
JB
8698 return 1;
8699 }
b13c8ab2
YQ
8700 else
8701 {
8702 int nRc;
cca44b1b 8703
b13c8ab2
YQ
8704 /* All aggregate types that won't fit in a register must be returned
8705 in memory. */
bab22d06
LM
8706 if (TYPE_LENGTH (type) > ARM_INT_REGISTER_SIZE
8707 || !language_pass_by_reference (type).trivially_copyable)
b13c8ab2 8708 return 1;
cca44b1b 8709
b13c8ab2
YQ
8710 /* In the ARM ABI, "integer" like aggregate types are returned in
8711 registers. For an aggregate type to be integer like, its size
f0452268 8712 must be less than or equal to ARM_INT_REGISTER_SIZE and the
b13c8ab2
YQ
8713 offset of each addressable subfield must be zero. Note that bit
8714 fields are not addressable, and all addressable subfields of
8715 unions always start at offset zero.
cca44b1b 8716
b13c8ab2
YQ
8717 This function is based on the behaviour of GCC 2.95.1.
8718 See: gcc/arm.c: arm_return_in_memory() for details.
cca44b1b 8719
b13c8ab2
YQ
8720 Note: All versions of GCC before GCC 2.95.2 do not set up the
8721 parameters correctly for a function returning the following
8722 structure: struct { float f;}; This should be returned in memory,
8723 not a register. Richard Earnshaw sent me a patch, but I do not
8724 know of any way to detect if a function like the above has been
8725 compiled with the correct calling convention. */
8726
8727 /* Assume all other aggregate types can be returned in a register.
8728 Run a check for structures, unions and arrays. */
8729 nRc = 0;
67255d04 8730
b13c8ab2
YQ
8731 if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
8732 {
8733 int i;
8734 /* Need to check if this struct/union is "integer" like. For
8735 this to be true, its size must be less than or equal to
f0452268 8736 ARM_INT_REGISTER_SIZE and the offset of each addressable
b13c8ab2
YQ
8737 subfield must be zero. Note that bit fields are not
8738 addressable, and unions always start at offset zero. If any
8739 of the subfields is a floating point type, the struct/union
8740 cannot be an integer type. */
8741
8742 /* For each field in the object, check:
8743 1) Is it FP? --> yes, nRc = 1;
8744 2) Is it addressable (bitpos != 0) and
8745 not packed (bitsize == 0)?
8746 --> yes, nRc = 1
8747 */
8748
1f704f76 8749 for (i = 0; i < type->num_fields (); i++)
67255d04 8750 {
b13c8ab2
YQ
8751 enum type_code field_type_code;
8752
8753 field_type_code
940da03e 8754 = check_typedef (type->field (i).type ())->code ();
b13c8ab2
YQ
8755
8756 /* Is it a floating point type field? */
8757 if (field_type_code == TYPE_CODE_FLT)
67255d04
RE
8758 {
8759 nRc = 1;
8760 break;
8761 }
b13c8ab2
YQ
8762
8763 /* If bitpos != 0, then we have to care about it. */
b610c045 8764 if (type->field (i).loc_bitpos () != 0)
b13c8ab2
YQ
8765 {
8766 /* Bitfields are not addressable. If the field bitsize is
8767 zero, then the field is not packed. Hence it cannot be
8768 a bitfield or any other packed type. */
8769 if (TYPE_FIELD_BITSIZE (type, i) == 0)
8770 {
8771 nRc = 1;
8772 break;
8773 }
8774 }
67255d04
RE
8775 }
8776 }
67255d04 8777
b13c8ab2
YQ
8778 return nRc;
8779 }
67255d04
RE
8780}
8781
34e8f22d
RE
8782/* Write into appropriate registers a function return value of type
8783 TYPE, given in virtual format. */
8784
8785static void
b508a996 8786arm_store_return_value (struct type *type, struct regcache *regs,
5238cf52 8787 const gdb_byte *valbuf)
34e8f22d 8788{
ac7936df 8789 struct gdbarch *gdbarch = regs->arch ();
e17a4113 8790 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
be8626e0 8791
78134374 8792 if (type->code () == TYPE_CODE_FLT)
34e8f22d 8793 {
f0452268 8794 gdb_byte buf[ARM_FP_REGISTER_SIZE];
345bd07c 8795 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
34e8f22d 8796
345bd07c 8797 switch (tdep->fp_model)
08216dd7
RE
8798 {
8799 case ARM_FLOAT_FPA:
8800
3b2ca824 8801 target_float_convert (valbuf, type, buf, arm_ext_type (gdbarch));
b66f5587 8802 regs->cooked_write (ARM_F0_REGNUM, buf);
08216dd7
RE
8803 break;
8804
fd50bc42 8805 case ARM_FLOAT_SOFT_FPA:
08216dd7 8806 case ARM_FLOAT_SOFT_VFP:
90445bd3
DJ
8807 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8808 not using the VFP ABI code. */
8809 case ARM_FLOAT_VFP:
b66f5587 8810 regs->cooked_write (ARM_A1_REGNUM, valbuf);
b508a996 8811 if (TYPE_LENGTH (type) > 4)
f0452268
AH
8812 regs->cooked_write (ARM_A1_REGNUM + 1,
8813 valbuf + ARM_INT_REGISTER_SIZE);
08216dd7
RE
8814 break;
8815
8816 default:
9b20d036
MS
8817 internal_error (__FILE__, __LINE__,
8818 _("arm_store_return_value: Floating "
8819 "point model not supported"));
08216dd7
RE
8820 break;
8821 }
34e8f22d 8822 }
78134374
SM
8823 else if (type->code () == TYPE_CODE_INT
8824 || type->code () == TYPE_CODE_CHAR
8825 || type->code () == TYPE_CODE_BOOL
8826 || type->code () == TYPE_CODE_PTR
aa006118 8827 || TYPE_IS_REFERENCE (type)
78134374 8828 || type->code () == TYPE_CODE_ENUM)
b508a996
RE
8829 {
8830 if (TYPE_LENGTH (type) <= 4)
8831 {
8832 /* Values of one word or less are zero/sign-extended and
8833 returned in r0. */
f0452268 8834 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
b508a996
RE
8835 LONGEST val = unpack_long (type, valbuf);
8836
f0452268 8837 store_signed_integer (tmpbuf, ARM_INT_REGISTER_SIZE, byte_order, val);
b66f5587 8838 regs->cooked_write (ARM_A1_REGNUM, tmpbuf);
b508a996
RE
8839 }
8840 else
8841 {
8842 /* Integral values greater than one word are stored in consecutive
8843 registers starting with r0. This will always be a multiple of
8844 the regiser size. */
8845 int len = TYPE_LENGTH (type);
8846 int regno = ARM_A1_REGNUM;
8847
8848 while (len > 0)
8849 {
b66f5587 8850 regs->cooked_write (regno++, valbuf);
f0452268
AH
8851 len -= ARM_INT_REGISTER_SIZE;
8852 valbuf += ARM_INT_REGISTER_SIZE;
b508a996
RE
8853 }
8854 }
8855 }
34e8f22d 8856 else
b508a996
RE
8857 {
8858 /* For a structure or union the behaviour is as if the value had
dda83cd7
SM
8859 been stored to word-aligned memory and then loaded into
8860 registers with 32-bit load instruction(s). */
b508a996
RE
8861 int len = TYPE_LENGTH (type);
8862 int regno = ARM_A1_REGNUM;
f0452268 8863 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
b508a996
RE
8864
8865 while (len > 0)
8866 {
8867 memcpy (tmpbuf, valbuf,
f0452268 8868 len > ARM_INT_REGISTER_SIZE ? ARM_INT_REGISTER_SIZE : len);
b66f5587 8869 regs->cooked_write (regno++, tmpbuf);
f0452268
AH
8870 len -= ARM_INT_REGISTER_SIZE;
8871 valbuf += ARM_INT_REGISTER_SIZE;
b508a996
RE
8872 }
8873 }
34e8f22d
RE
8874}
8875
2af48f68
PB
8876
8877/* Handle function return values. */
8878
8879static enum return_value_convention
6a3a010b 8880arm_return_value (struct gdbarch *gdbarch, struct value *function,
c055b101
CV
8881 struct type *valtype, struct regcache *regcache,
8882 gdb_byte *readbuf, const gdb_byte *writebuf)
2af48f68 8883{
345bd07c 8884 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
6a3a010b 8885 struct type *func_type = function ? value_type (function) : NULL;
90445bd3
DJ
8886 enum arm_vfp_cprc_base_type vfp_base_type;
8887 int vfp_base_count;
8888
8889 if (arm_vfp_abi_for_function (gdbarch, func_type)
8890 && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
8891 {
8892 int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
8893 int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
8894 int i;
8895 for (i = 0; i < vfp_base_count; i++)
8896 {
58d6951d
DJ
8897 if (reg_char == 'q')
8898 {
8899 if (writebuf)
8900 arm_neon_quad_write (gdbarch, regcache, i,
8901 writebuf + i * unit_length);
8902
8903 if (readbuf)
8904 arm_neon_quad_read (gdbarch, regcache, i,
8905 readbuf + i * unit_length);
8906 }
8907 else
8908 {
8909 char name_buf[4];
8910 int regnum;
8911
8c042590 8912 xsnprintf (name_buf, sizeof (name_buf), "%c%d", reg_char, i);
58d6951d
DJ
8913 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8914 strlen (name_buf));
8915 if (writebuf)
b66f5587 8916 regcache->cooked_write (regnum, writebuf + i * unit_length);
58d6951d 8917 if (readbuf)
dca08e1f 8918 regcache->cooked_read (regnum, readbuf + i * unit_length);
58d6951d 8919 }
90445bd3
DJ
8920 }
8921 return RETURN_VALUE_REGISTER_CONVENTION;
8922 }
7c00367c 8923
78134374
SM
8924 if (valtype->code () == TYPE_CODE_STRUCT
8925 || valtype->code () == TYPE_CODE_UNION
8926 || valtype->code () == TYPE_CODE_ARRAY)
2af48f68 8927 {
bab22d06
LM
8928 /* From the AAPCS document:
8929
8930 Result return:
8931
8932 A Composite Type larger than 4 bytes, or whose size cannot be
8933 determined statically by both caller and callee, is stored in memory
8934 at an address passed as an extra argument when the function was
8935 called (Parameter Passing, rule A.4). The memory to be used for the
8936 result may be modified at any point during the function call.
8937
8938 Parameter Passing:
8939
8940 A.4: If the subroutine is a function that returns a result in memory,
8941 then the address for the result is placed in r0 and the NCRN is set
8942 to r1. */
7c00367c
MK
8943 if (tdep->struct_return == pcc_struct_return
8944 || arm_return_in_memory (gdbarch, valtype))
bab22d06
LM
8945 {
8946 if (readbuf)
8947 {
8948 CORE_ADDR addr;
8949
8950 regcache->cooked_read (ARM_A1_REGNUM, &addr);
8951 read_memory (addr, readbuf, TYPE_LENGTH (valtype));
8952 }
8953 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
8954 }
2af48f68 8955 }
78134374 8956 else if (valtype->code () == TYPE_CODE_COMPLEX)
b13c8ab2
YQ
8957 {
8958 if (arm_return_in_memory (gdbarch, valtype))
8959 return RETURN_VALUE_STRUCT_CONVENTION;
8960 }
7052e42c 8961
2af48f68
PB
8962 if (writebuf)
8963 arm_store_return_value (valtype, regcache, writebuf);
8964
8965 if (readbuf)
8966 arm_extract_return_value (valtype, regcache, readbuf);
8967
8968 return RETURN_VALUE_REGISTER_CONVENTION;
8969}
8970
8971
9df628e0 8972static int
60ade65d 8973arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
9df628e0 8974{
e17a4113 8975 struct gdbarch *gdbarch = get_frame_arch (frame);
345bd07c 8976 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
e17a4113 8977 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
9df628e0 8978 CORE_ADDR jb_addr;
f0452268 8979 gdb_byte buf[ARM_INT_REGISTER_SIZE];
9df628e0 8980
60ade65d 8981 jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
9df628e0
RE
8982
8983 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
f0452268 8984 ARM_INT_REGISTER_SIZE))
9df628e0
RE
8985 return 0;
8986
f0452268 8987 *pc = extract_unsigned_integer (buf, ARM_INT_REGISTER_SIZE, byte_order);
9df628e0
RE
8988 return 1;
8989}
40eadf04
SP
8990/* A call to cmse secure entry function "foo" at "a" is modified by
8991 GNU ld as "b".
8992 a) bl xxxx <foo>
8993
8994 <foo>
8995 xxxx:
8996
8997 b) bl yyyy <__acle_se_foo>
8998
8999 section .gnu.sgstubs:
9000 <foo>
9001 yyyy: sg // secure gateway
9002 b.w xxxx <__acle_se_foo> // original_branch_dest
9003
9004 <__acle_se_foo>
9005 xxxx:
9006
9007 When the control at "b", the pc contains "yyyy" (sg address) which is a
9008 trampoline and does not exist in source code. This function returns the
9009 target pc "xxxx". For more details please refer to section 5.4
9010 (Entry functions) and section 3.4.4 (C level development flow of secure code)
9011 of "armv8-m-security-extensions-requirements-on-development-tools-engineering-specification"
9012 document on www.developer.arm.com. */
9013
9014static CORE_ADDR
9015arm_skip_cmse_entry (CORE_ADDR pc, const char *name, struct objfile *objfile)
9016{
9017 int target_len = strlen (name) + strlen ("__acle_se_") + 1;
9018 char *target_name = (char *) alloca (target_len);
9019 xsnprintf (target_name, target_len, "%s%s", "__acle_se_", name);
9020
9021 struct bound_minimal_symbol minsym
9022 = lookup_minimal_symbol (target_name, NULL, objfile);
9023
9024 if (minsym.minsym != nullptr)
4aeddc50 9025 return minsym.value_address ();
40eadf04
SP
9026
9027 return 0;
9028}
9029
9030/* Return true when SEC points to ".gnu.sgstubs" section. */
9031
9032static bool
9033arm_is_sgstubs_section (struct obj_section *sec)
9034{
9035 return (sec != nullptr
9036 && sec->the_bfd_section != nullptr
9037 && sec->the_bfd_section->name != nullptr
9038 && streq (sec->the_bfd_section->name, ".gnu.sgstubs"));
9039}
9df628e0 9040
faa95490
DJ
9041/* Recognize GCC and GNU ld's trampolines. If we are in a trampoline,
9042 return the target PC. Otherwise return 0. */
c906108c
SS
9043
9044CORE_ADDR
52f729a7 9045arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
c906108c 9046{
2c02bd72 9047 const char *name;
faa95490 9048 int namelen;
c906108c
SS
9049 CORE_ADDR start_addr;
9050
9051 /* Find the starting address and name of the function containing the PC. */
9052 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
80d8d390
YQ
9053 {
9054 /* Trampoline 'bx reg' doesn't belong to any functions. Do the
9055 check here. */
9056 start_addr = arm_skip_bx_reg (frame, pc);
9057 if (start_addr != 0)
9058 return start_addr;
9059
9060 return 0;
9061 }
c906108c 9062
faa95490
DJ
9063 /* If PC is in a Thumb call or return stub, return the address of the
9064 target PC, which is in a register. The thunk functions are called
9065 _call_via_xx, where x is the register name. The possible names
3d8d5e79
DJ
9066 are r0-r9, sl, fp, ip, sp, and lr. ARM RealView has similar
9067 functions, named __ARM_call_via_r[0-7]. */
61012eef
GB
9068 if (startswith (name, "_call_via_")
9069 || startswith (name, "__ARM_call_via_"))
c906108c 9070 {
ed9a39eb 9071 /* Use the name suffix to determine which register contains the
dda83cd7 9072 target PC. */
a121b7c1 9073 static const char *table[15] =
c5aa993b
JM
9074 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
9075 "r8", "r9", "sl", "fp", "ip", "sp", "lr"
9076 };
c906108c 9077 int regno;
faa95490 9078 int offset = strlen (name) - 2;
c906108c
SS
9079
9080 for (regno = 0; regno <= 14; regno++)
faa95490 9081 if (strcmp (&name[offset], table[regno]) == 0)
52f729a7 9082 return get_frame_register_unsigned (frame, regno);
c906108c 9083 }
ed9a39eb 9084
faa95490
DJ
9085 /* GNU ld generates __foo_from_arm or __foo_from_thumb for
9086 non-interworking calls to foo. We could decode the stubs
9087 to find the target but it's easier to use the symbol table. */
9088 namelen = strlen (name);
9089 if (name[0] == '_' && name[1] == '_'
9090 && ((namelen > 2 + strlen ("_from_thumb")
61012eef 9091 && startswith (name + namelen - strlen ("_from_thumb"), "_from_thumb"))
faa95490 9092 || (namelen > 2 + strlen ("_from_arm")
61012eef 9093 && startswith (name + namelen - strlen ("_from_arm"), "_from_arm"))))
faa95490
DJ
9094 {
9095 char *target_name;
9096 int target_len = namelen - 2;
3b7344d5 9097 struct bound_minimal_symbol minsym;
faa95490
DJ
9098 struct objfile *objfile;
9099 struct obj_section *sec;
9100
9101 if (name[namelen - 1] == 'b')
9102 target_len -= strlen ("_from_thumb");
9103 else
9104 target_len -= strlen ("_from_arm");
9105
224c3ddb 9106 target_name = (char *) alloca (target_len + 1);
faa95490
DJ
9107 memcpy (target_name, name + 2, target_len);
9108 target_name[target_len] = '\0';
9109
9110 sec = find_pc_section (pc);
9111 objfile = (sec == NULL) ? NULL : sec->objfile;
9112 minsym = lookup_minimal_symbol (target_name, NULL, objfile);
3b7344d5 9113 if (minsym.minsym != NULL)
4aeddc50 9114 return minsym.value_address ();
faa95490
DJ
9115 else
9116 return 0;
9117 }
9118
40eadf04
SP
9119 struct obj_section *section = find_pc_section (pc);
9120
9121 /* Check whether SECTION points to the ".gnu.sgstubs" section. */
9122 if (arm_is_sgstubs_section (section))
9123 return arm_skip_cmse_entry (pc, name, section->objfile);
9124
c5aa993b 9125 return 0; /* not a stub */
c906108c
SS
9126}
9127
28e97307
DJ
9128static void
9129arm_update_current_architecture (void)
fd50bc42 9130{
28e97307 9131 /* If the current architecture is not ARM, we have nothing to do. */
f5656ead 9132 if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_arm)
28e97307 9133 return;
fd50bc42 9134
28e97307 9135 /* Update the architecture. */
b447dd03 9136 gdbarch_info info;
28e97307 9137 if (!gdbarch_update_p (info))
9b20d036 9138 internal_error (__FILE__, __LINE__, _("could not update architecture"));
fd50bc42
RE
9139}
9140
9141static void
eb4c3f4a 9142set_fp_model_sfunc (const char *args, int from_tty,
fd50bc42
RE
9143 struct cmd_list_element *c)
9144{
570dc176 9145 int fp_model;
fd50bc42
RE
9146
9147 for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
9148 if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
9149 {
aead7601 9150 arm_fp_model = (enum arm_float_model) fp_model;
fd50bc42
RE
9151 break;
9152 }
9153
9154 if (fp_model == ARM_FLOAT_LAST)
edefbb7c 9155 internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
fd50bc42
RE
9156 current_fp_model);
9157
28e97307 9158 arm_update_current_architecture ();
fd50bc42
RE
9159}
9160
9161static void
08546159
AC
9162show_fp_model (struct ui_file *file, int from_tty,
9163 struct cmd_list_element *c, const char *value)
fd50bc42 9164{
345bd07c
SM
9165 arm_gdbarch_tdep *tdep
9166 = (arm_gdbarch_tdep *) gdbarch_tdep (target_gdbarch ());
fd50bc42 9167
28e97307 9168 if (arm_fp_model == ARM_FLOAT_AUTO
f5656ead 9169 && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
6cb06a8c 9170 gdb_printf (file, _("\
28e97307 9171The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
6cb06a8c 9172 fp_model_strings[tdep->fp_model]);
28e97307 9173 else
6cb06a8c 9174 gdb_printf (file, _("\
28e97307 9175The current ARM floating point model is \"%s\".\n"),
6cb06a8c 9176 fp_model_strings[arm_fp_model]);
28e97307
DJ
9177}
9178
9179static void
eb4c3f4a 9180arm_set_abi (const char *args, int from_tty,
28e97307
DJ
9181 struct cmd_list_element *c)
9182{
570dc176 9183 int arm_abi;
28e97307
DJ
9184
9185 for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
9186 if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
9187 {
aead7601 9188 arm_abi_global = (enum arm_abi_kind) arm_abi;
28e97307
DJ
9189 break;
9190 }
9191
9192 if (arm_abi == ARM_ABI_LAST)
9193 internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
9194 arm_abi_string);
9195
9196 arm_update_current_architecture ();
9197}
9198
9199static void
9200arm_show_abi (struct ui_file *file, int from_tty,
9201 struct cmd_list_element *c, const char *value)
9202{
345bd07c
SM
9203 arm_gdbarch_tdep *tdep
9204 = (arm_gdbarch_tdep *) gdbarch_tdep (target_gdbarch ());
28e97307
DJ
9205
9206 if (arm_abi_global == ARM_ABI_AUTO
f5656ead 9207 && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
6cb06a8c 9208 gdb_printf (file, _("\
28e97307 9209The current ARM ABI is \"auto\" (currently \"%s\").\n"),
6cb06a8c 9210 arm_abi_strings[tdep->arm_abi]);
28e97307 9211 else
6cb06a8c
TT
9212 gdb_printf (file, _("The current ARM ABI is \"%s\".\n"),
9213 arm_abi_string);
fd50bc42
RE
9214}
9215
0428b8f5
DJ
9216static void
9217arm_show_fallback_mode (struct ui_file *file, int from_tty,
9218 struct cmd_list_element *c, const char *value)
9219{
6cb06a8c
TT
9220 gdb_printf (file,
9221 _("The current execution mode assumed "
9222 "(when symbols are unavailable) is \"%s\".\n"),
9223 arm_fallback_mode_string);
0428b8f5
DJ
9224}
9225
9226static void
9227arm_show_force_mode (struct ui_file *file, int from_tty,
9228 struct cmd_list_element *c, const char *value)
9229{
6cb06a8c
TT
9230 gdb_printf (file,
9231 _("The current execution mode assumed "
9232 "(even when symbols are available) is \"%s\".\n"),
9233 arm_force_mode_string);
0428b8f5
DJ
9234}
9235
ef273377
CL
9236static void
9237arm_show_unwind_secure_frames (struct ui_file *file, int from_tty,
9238 struct cmd_list_element *c, const char *value)
9239{
9240 gdb_printf (file,
9241 _("Usage of non-secure to secure exception stack unwinding is %s.\n"),
9242 arm_unwind_secure_frames ? "on" : "off");
9243}
9244
afd7eef0
RE
9245/* If the user changes the register disassembly style used for info
9246 register and other commands, we have to also switch the style used
9247 in opcodes for disassembly output. This function is run in the "set
9248 arm disassembly" command, and does that. */
bc90b915
FN
9249
9250static void
eb4c3f4a 9251set_disassembly_style_sfunc (const char *args, int from_tty,
65b48a81 9252 struct cmd_list_element *c)
bc90b915 9253{
65b48a81
PB
9254 /* Convert the short style name into the long style name (eg, reg-names-*)
9255 before calling the generic set_disassembler_options() function. */
9256 std::string long_name = std::string ("reg-names-") + disassembly_style;
9257 set_disassembler_options (&long_name[0]);
9258}
9259
9260static void
9261show_disassembly_style_sfunc (struct ui_file *file, int from_tty,
9262 struct cmd_list_element *c, const char *value)
9263{
9264 struct gdbarch *gdbarch = get_current_arch ();
9265 char *options = get_disassembler_options (gdbarch);
9266 const char *style = "";
9267 int len = 0;
f995bbe8 9268 const char *opt;
65b48a81
PB
9269
9270 FOR_EACH_DISASSEMBLER_OPTION (opt, options)
08dedd66 9271 if (startswith (opt, "reg-names-"))
65b48a81
PB
9272 {
9273 style = &opt[strlen ("reg-names-")];
9274 len = strcspn (style, ",");
9275 }
9276
6cb06a8c 9277 gdb_printf (file, "The disassembly style is \"%.*s\".\n", len, style);
bc90b915
FN
9278}
9279\f
966fbf70 9280/* Return the ARM register name corresponding to register I. */
a208b0cb 9281static const char *
d93859e2 9282arm_register_name (struct gdbarch *gdbarch, int i)
966fbf70 9283{
345bd07c 9284 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
58d6951d 9285
ecbf5d4f 9286 if (is_s_pseudo (gdbarch, i))
58d6951d 9287 {
ecbf5d4f 9288 static const char *const s_pseudo_names[] = {
58d6951d
DJ
9289 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
9290 "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
9291 "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
9292 "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
9293 };
9294
ecbf5d4f 9295 return s_pseudo_names[i - tdep->s_pseudo_base];
58d6951d
DJ
9296 }
9297
ecbf5d4f 9298 if (is_q_pseudo (gdbarch, i))
58d6951d 9299 {
ecbf5d4f 9300 static const char *const q_pseudo_names[] = {
58d6951d
DJ
9301 "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
9302 "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
9303 };
9304
ecbf5d4f 9305 return q_pseudo_names[i - tdep->q_pseudo_base];
58d6951d
DJ
9306 }
9307
ae66a8f1
SP
9308 if (is_mve_pseudo (gdbarch, i))
9309 return "p0";
9310
a01567f4
LM
9311 /* RA_AUTH_CODE is used for unwinding only. Do not assign it a name. */
9312 if (is_pacbti_pseudo (gdbarch, i))
9313 return "";
9314
ff6f572f
DJ
9315 if (i >= ARRAY_SIZE (arm_register_names))
9316 /* These registers are only supported on targets which supply
9317 an XML description. */
9318 return "";
9319
ecbf5d4f 9320 /* Non-pseudo registers. */
966fbf70
RE
9321 return arm_register_names[i];
9322}
9323
082fc60d
RE
9324/* Test whether the coff symbol specific value corresponds to a Thumb
9325 function. */
9326
9327static int
9328coff_sym_is_thumb (int val)
9329{
f8bf5763
PM
9330 return (val == C_THUMBEXT
9331 || val == C_THUMBSTAT
9332 || val == C_THUMBEXTFUNC
9333 || val == C_THUMBSTATFUNC
9334 || val == C_THUMBLABEL);
082fc60d
RE
9335}
9336
9337/* arm_coff_make_msymbol_special()
9338 arm_elf_make_msymbol_special()
9339
9340 These functions test whether the COFF or ELF symbol corresponds to
9341 an address in thumb code, and set a "special" bit in a minimal
9342 symbol to indicate that it does. */
9343
34e8f22d 9344static void
082fc60d
RE
9345arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
9346{
39d911fc
TP
9347 elf_symbol_type *elfsym = (elf_symbol_type *) sym;
9348
9349 if (ARM_GET_SYM_BRANCH_TYPE (elfsym->internal_elf_sym.st_target_internal)
467d42c4 9350 == ST_BRANCH_TO_THUMB)
082fc60d
RE
9351 MSYMBOL_SET_SPECIAL (msym);
9352}
9353
34e8f22d 9354static void
082fc60d
RE
9355arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
9356{
9357 if (coff_sym_is_thumb (val))
9358 MSYMBOL_SET_SPECIAL (msym);
9359}
9360
60c5725c
DJ
9361static void
9362arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
9363 asymbol *sym)
9364{
9365 const char *name = bfd_asymbol_name (sym);
bd5766ec 9366 struct arm_per_bfd *data;
60c5725c
DJ
9367 struct arm_mapping_symbol new_map_sym;
9368
9369 gdb_assert (name[0] == '$');
9370 if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
9371 return;
9372
bd5766ec 9373 data = arm_bfd_data_key.get (objfile->obfd);
60c5725c 9374 if (data == NULL)
bd5766ec
LM
9375 data = arm_bfd_data_key.emplace (objfile->obfd,
9376 objfile->obfd->section_count);
54cc7474 9377 arm_mapping_symbol_vec &map
e6f7f6d1 9378 = data->section_maps[bfd_asymbol_section (sym)->index];
60c5725c
DJ
9379
9380 new_map_sym.value = sym->value;
9381 new_map_sym.type = name[1];
9382
4838e44c
SM
9383 /* Insert at the end, the vector will be sorted on first use. */
9384 map.push_back (new_map_sym);
60c5725c
DJ
9385}
9386
756fe439 9387static void
61a1198a 9388arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
756fe439 9389{
ac7936df 9390 struct gdbarch *gdbarch = regcache->arch ();
61a1198a 9391 regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
756fe439
DJ
9392
9393 /* If necessary, set the T bit. */
9394 if (arm_apcs_32)
9395 {
9779414d 9396 ULONGEST val, t_bit;
61a1198a 9397 regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
9779414d
DJ
9398 t_bit = arm_psr_thumb_bit (gdbarch);
9399 if (arm_pc_is_thumb (gdbarch, pc))
9400 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9401 val | t_bit);
756fe439 9402 else
61a1198a 9403 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9779414d 9404 val & ~t_bit);
756fe439
DJ
9405 }
9406}
123dc839 9407
58d6951d
DJ
9408/* Read the contents of a NEON quad register, by reading from two
9409 double registers. This is used to implement the quad pseudo
9410 registers, and for argument passing in case the quad registers are
9411 missing; vectors are passed in quad registers when using the VFP
9412 ABI, even if a NEON unit is not present. REGNUM is the index of
9413 the quad register, in [0, 15]. */
9414
05d1431c 9415static enum register_status
849d0ba8 9416arm_neon_quad_read (struct gdbarch *gdbarch, readable_regcache *regcache,
58d6951d
DJ
9417 int regnum, gdb_byte *buf)
9418{
9419 char name_buf[4];
9420 gdb_byte reg_buf[8];
9421 int offset, double_regnum;
05d1431c 9422 enum register_status status;
58d6951d 9423
8c042590 9424 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
58d6951d
DJ
9425 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9426 strlen (name_buf));
9427
9428 /* d0 is always the least significant half of q0. */
9429 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9430 offset = 8;
9431 else
9432 offset = 0;
9433
03f50fc8 9434 status = regcache->raw_read (double_regnum, reg_buf);
05d1431c
PA
9435 if (status != REG_VALID)
9436 return status;
58d6951d
DJ
9437 memcpy (buf + offset, reg_buf, 8);
9438
9439 offset = 8 - offset;
03f50fc8 9440 status = regcache->raw_read (double_regnum + 1, reg_buf);
05d1431c
PA
9441 if (status != REG_VALID)
9442 return status;
58d6951d 9443 memcpy (buf + offset, reg_buf, 8);
05d1431c
PA
9444
9445 return REG_VALID;
58d6951d
DJ
9446}
9447
ae66a8f1
SP
9448/* Read the contents of the MVE pseudo register REGNUM and store it
9449 in BUF. */
9450
9451static enum register_status
9452arm_mve_pseudo_read (struct gdbarch *gdbarch, readable_regcache *regcache,
9453 int regnum, gdb_byte *buf)
9454{
345bd07c 9455 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
ae66a8f1
SP
9456
9457 /* P0 is the first 16 bits of VPR. */
9458 return regcache->raw_read_part (tdep->mve_vpr_regnum, 0, 2, buf);
9459}
9460
05d1431c 9461static enum register_status
849d0ba8 9462arm_pseudo_read (struct gdbarch *gdbarch, readable_regcache *regcache,
58d6951d
DJ
9463 int regnum, gdb_byte *buf)
9464{
9465 const int num_regs = gdbarch_num_regs (gdbarch);
9466 char name_buf[4];
9467 gdb_byte reg_buf[8];
9468 int offset, double_regnum;
345bd07c 9469 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
58d6951d
DJ
9470
9471 gdb_assert (regnum >= num_regs);
58d6951d 9472
ecbf5d4f
LM
9473 if (is_q_pseudo (gdbarch, regnum))
9474 {
9475 /* Quad-precision register. */
9476 return arm_neon_quad_read (gdbarch, regcache,
9477 regnum - tdep->q_pseudo_base, buf);
9478 }
ae66a8f1
SP
9479 else if (is_mve_pseudo (gdbarch, regnum))
9480 return arm_mve_pseudo_read (gdbarch, regcache, regnum, buf);
58d6951d
DJ
9481 else
9482 {
05d1431c
PA
9483 enum register_status status;
9484
ecbf5d4f 9485 regnum -= tdep->s_pseudo_base;
58d6951d
DJ
9486 /* Single-precision register. */
9487 gdb_assert (regnum < 32);
9488
9489 /* s0 is always the least significant half of d0. */
9490 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9491 offset = (regnum & 1) ? 0 : 4;
9492 else
9493 offset = (regnum & 1) ? 4 : 0;
9494
8c042590 9495 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
58d6951d
DJ
9496 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9497 strlen (name_buf));
9498
03f50fc8 9499 status = regcache->raw_read (double_regnum, reg_buf);
05d1431c
PA
9500 if (status == REG_VALID)
9501 memcpy (buf, reg_buf + offset, 4);
9502 return status;
58d6951d
DJ
9503 }
9504}
9505
9506/* Store the contents of BUF to a NEON quad register, by writing to
9507 two double registers. This is used to implement the quad pseudo
9508 registers, and for argument passing in case the quad registers are
9509 missing; vectors are passed in quad registers when using the VFP
9510 ABI, even if a NEON unit is not present. REGNUM is the index
9511 of the quad register, in [0, 15]. */
9512
9513static void
9514arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
9515 int regnum, const gdb_byte *buf)
9516{
9517 char name_buf[4];
58d6951d
DJ
9518 int offset, double_regnum;
9519
8c042590 9520 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
58d6951d
DJ
9521 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9522 strlen (name_buf));
9523
9524 /* d0 is always the least significant half of q0. */
9525 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9526 offset = 8;
9527 else
9528 offset = 0;
9529
10eaee5f 9530 regcache->raw_write (double_regnum, buf + offset);
58d6951d 9531 offset = 8 - offset;
10eaee5f 9532 regcache->raw_write (double_regnum + 1, buf + offset);
58d6951d
DJ
9533}
9534
ae66a8f1
SP
9535/* Store the contents of BUF to the MVE pseudo register REGNUM. */
9536
9537static void
9538arm_mve_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
9539 int regnum, const gdb_byte *buf)
9540{
345bd07c 9541 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
ae66a8f1
SP
9542
9543 /* P0 is the first 16 bits of VPR. */
9544 regcache->raw_write_part (tdep->mve_vpr_regnum, 0, 2, buf);
9545}
9546
58d6951d
DJ
9547static void
9548arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
9549 int regnum, const gdb_byte *buf)
9550{
9551 const int num_regs = gdbarch_num_regs (gdbarch);
9552 char name_buf[4];
9553 gdb_byte reg_buf[8];
9554 int offset, double_regnum;
345bd07c 9555 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
58d6951d
DJ
9556
9557 gdb_assert (regnum >= num_regs);
58d6951d 9558
ecbf5d4f
LM
9559 if (is_q_pseudo (gdbarch, regnum))
9560 {
9561 /* Quad-precision register. */
9562 arm_neon_quad_write (gdbarch, regcache,
9563 regnum - tdep->q_pseudo_base, buf);
9564 }
ae66a8f1
SP
9565 else if (is_mve_pseudo (gdbarch, regnum))
9566 arm_mve_pseudo_write (gdbarch, regcache, regnum, buf);
58d6951d
DJ
9567 else
9568 {
ecbf5d4f 9569 regnum -= tdep->s_pseudo_base;
58d6951d
DJ
9570 /* Single-precision register. */
9571 gdb_assert (regnum < 32);
9572
9573 /* s0 is always the least significant half of d0. */
9574 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9575 offset = (regnum & 1) ? 0 : 4;
9576 else
9577 offset = (regnum & 1) ? 4 : 0;
9578
8c042590 9579 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
58d6951d
DJ
9580 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9581 strlen (name_buf));
9582
0b883586 9583 regcache->raw_read (double_regnum, reg_buf);
58d6951d 9584 memcpy (reg_buf + offset, buf, 4);
10eaee5f 9585 regcache->raw_write (double_regnum, reg_buf);
58d6951d
DJ
9586 }
9587}
9588
123dc839
DJ
9589static struct value *
9590value_of_arm_user_reg (struct frame_info *frame, const void *baton)
9591{
9a3c8263 9592 const int *reg_p = (const int *) baton;
123dc839
DJ
9593 return value_of_register (*reg_p, frame);
9594}
97e03143 9595\f
70f80edf
JT
9596static enum gdb_osabi
9597arm_elf_osabi_sniffer (bfd *abfd)
97e03143 9598{
2af48f68 9599 unsigned int elfosabi;
70f80edf 9600 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
97e03143 9601
70f80edf 9602 elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
97e03143 9603
28e97307
DJ
9604 if (elfosabi == ELFOSABI_ARM)
9605 /* GNU tools use this value. Check note sections in this case,
9606 as well. */
b35c1d1c
TT
9607 {
9608 for (asection *sect : gdb_bfd_sections (abfd))
9609 generic_elf_osabi_sniff_abi_tag_sections (abfd, sect, &osabi);
9610 }
97e03143 9611
28e97307 9612 /* Anything else will be handled by the generic ELF sniffer. */
70f80edf 9613 return osabi;
97e03143
RE
9614}
9615
54483882
YQ
9616static int
9617arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
dbf5d61b 9618 const struct reggroup *group)
54483882 9619{
2c291032
YQ
9620 /* FPS register's type is INT, but belongs to float_reggroup. Beside
9621 this, FPS register belongs to save_regroup, restore_reggroup, and
9622 all_reggroup, of course. */
54483882 9623 if (regnum == ARM_FPS_REGNUM)
2c291032
YQ
9624 return (group == float_reggroup
9625 || group == save_reggroup
9626 || group == restore_reggroup
9627 || group == all_reggroup);
54483882
YQ
9628 else
9629 return default_register_reggroup_p (gdbarch, regnum, group);
9630}
9631
25f8c692
JL
9632/* For backward-compatibility we allow two 'g' packet lengths with
9633 the remote protocol depending on whether FPA registers are
9634 supplied. M-profile targets do not have FPA registers, but some
9635 stubs already exist in the wild which use a 'g' packet which
9636 supplies them albeit with dummy values. The packet format which
9637 includes FPA registers should be considered deprecated for
9638 M-profile targets. */
9639
9640static void
9641arm_register_g_packet_guesses (struct gdbarch *gdbarch)
9642{
345bd07c
SM
9643 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
9644
9645 if (tdep->is_m)
25f8c692 9646 {
d105cce5
AH
9647 const target_desc *tdesc;
9648
25f8c692
JL
9649 /* If we know from the executable this is an M-profile target,
9650 cater for remote targets whose register set layout is the
9651 same as the FPA layout. */
d105cce5 9652 tdesc = arm_read_mprofile_description (ARM_M_TYPE_WITH_FPA);
25f8c692 9653 register_remote_g_packet_guess (gdbarch,
350fab54 9654 ARM_CORE_REGS_SIZE + ARM_FP_REGS_SIZE,
d105cce5 9655 tdesc);
25f8c692
JL
9656
9657 /* The regular M-profile layout. */
d105cce5 9658 tdesc = arm_read_mprofile_description (ARM_M_TYPE_M_PROFILE);
350fab54 9659 register_remote_g_packet_guess (gdbarch, ARM_CORE_REGS_SIZE,
d105cce5 9660 tdesc);
3184d3f9
JL
9661
9662 /* M-profile plus M4F VFP. */
d105cce5 9663 tdesc = arm_read_mprofile_description (ARM_M_TYPE_VFP_D16);
3184d3f9 9664 register_remote_g_packet_guess (gdbarch,
350fab54 9665 ARM_CORE_REGS_SIZE + ARM_VFP2_REGS_SIZE,
d105cce5 9666 tdesc);
ae66a8f1
SP
9667 /* M-profile plus MVE. */
9668 tdesc = arm_read_mprofile_description (ARM_M_TYPE_MVE);
9669 register_remote_g_packet_guess (gdbarch, ARM_CORE_REGS_SIZE
9670 + ARM_VFP2_REGS_SIZE
9671 + ARM_INT_REGISTER_SIZE, tdesc);
9074667a
CL
9672
9673 /* M-profile system (stack pointers). */
9674 tdesc = arm_read_mprofile_description (ARM_M_TYPE_SYSTEM);
9675 register_remote_g_packet_guess (gdbarch, 2 * ARM_INT_REGISTER_SIZE, tdesc);
25f8c692
JL
9676 }
9677
9678 /* Otherwise we don't have a useful guess. */
9679}
9680
7eb89530
YQ
9681/* Implement the code_of_frame_writable gdbarch method. */
9682
9683static int
9684arm_code_of_frame_writable (struct gdbarch *gdbarch, struct frame_info *frame)
9685{
345bd07c
SM
9686 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
9687
9688 if (tdep->is_m && get_frame_type (frame) == SIGTRAMP_FRAME)
7eb89530
YQ
9689 {
9690 /* M-profile exception frames return to some magic PCs, where
9691 isn't writable at all. */
9692 return 0;
9693 }
9694 else
9695 return 1;
9696}
9697
3426ae57
AH
9698/* Implement gdbarch_gnu_triplet_regexp. If the arch name is arm then allow it
9699 to be postfixed by a version (eg armv7hl). */
9700
9701static const char *
9702arm_gnu_triplet_regexp (struct gdbarch *gdbarch)
9703{
9704 if (strcmp (gdbarch_bfd_arch_info (gdbarch)->arch_name, "arm") == 0)
9705 return "arm(v[^- ]*)?";
9706 return gdbarch_bfd_arch_info (gdbarch)->arch_name;
9707}
9708
a01567f4
LM
9709/* Implement the "get_pc_address_flags" gdbarch method. */
9710
9711static std::string
9712arm_get_pc_address_flags (frame_info *frame, CORE_ADDR pc)
9713{
9714 if (get_frame_pc_masked (frame))
9715 return "PAC";
9716
9717 return "";
9718}
9719
da3c6d4a
MS
9720/* Initialize the current architecture based on INFO. If possible,
9721 re-use an architecture from ARCHES, which is a list of
9722 architectures already created during this debugging session.
97e03143 9723
da3c6d4a
MS
9724 Called e.g. at program startup, when reading a core file, and when
9725 reading a binary file. */
97e03143 9726
39bbf761
RE
9727static struct gdbarch *
9728arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
9729{
9730 struct gdbarch *gdbarch;
28e97307
DJ
9731 struct gdbarch_list *best_arch;
9732 enum arm_abi_kind arm_abi = arm_abi_global;
9733 enum arm_float_model fp_model = arm_fp_model;
c1e1314d 9734 tdesc_arch_data_up tdesc_data;
7559c217
CB
9735 int i;
9736 bool is_m = false;
ae7e2f45 9737 bool have_sec_ext = false;
7559c217 9738 int vfp_register_count = 0;
ecbf5d4f 9739 bool have_s_pseudos = false, have_q_pseudos = false;
7559c217
CB
9740 bool have_wmmx_registers = false;
9741 bool have_neon = false;
9742 bool have_fpa_registers = true;
9779414d 9743 const struct target_desc *tdesc = info.target_desc;
ae66a8f1
SP
9744 bool have_vfp = false;
9745 bool have_mve = false;
a01567f4 9746 bool have_pacbti = false;
ae66a8f1 9747 int mve_vpr_regnum = -1;
ecbf5d4f 9748 int register_count = ARM_NUM_REGS;
9074667a
CL
9749 bool have_m_profile_msp = false;
9750 int m_profile_msp_regnum = -1;
9751 int m_profile_psp_regnum = -1;
ae7e2f45
CL
9752 int m_profile_msp_ns_regnum = -1;
9753 int m_profile_psp_ns_regnum = -1;
9754 int m_profile_msp_s_regnum = -1;
9755 int m_profile_psp_s_regnum = -1;
92d48a1e 9756 int tls_regnum = 0;
9779414d
DJ
9757
9758 /* If we have an object to base this architecture on, try to determine
9759 its ABI. */
9760
9761 if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
9762 {
9763 int ei_osabi, e_flags;
9764
9765 switch (bfd_get_flavour (info.abfd))
9766 {
9779414d
DJ
9767 case bfd_target_coff_flavour:
9768 /* Assume it's an old APCS-style ABI. */
9769 /* XXX WinCE? */
9770 arm_abi = ARM_ABI_APCS;
9771 break;
9772
9773 case bfd_target_elf_flavour:
9774 ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
9775 e_flags = elf_elfheader (info.abfd)->e_flags;
9776
9777 if (ei_osabi == ELFOSABI_ARM)
9778 {
9779 /* GNU tools used to use this value, but do not for EABI
9780 objects. There's nowhere to tag an EABI version
9781 anyway, so assume APCS. */
9782 arm_abi = ARM_ABI_APCS;
9783 }
d403db27 9784 else if (ei_osabi == ELFOSABI_NONE || ei_osabi == ELFOSABI_GNU)
9779414d
DJ
9785 {
9786 int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
9779414d
DJ
9787
9788 switch (eabi_ver)
9789 {
9790 case EF_ARM_EABI_UNKNOWN:
9791 /* Assume GNU tools. */
9792 arm_abi = ARM_ABI_APCS;
9793 break;
9794
9795 case EF_ARM_EABI_VER4:
9796 case EF_ARM_EABI_VER5:
9797 arm_abi = ARM_ABI_AAPCS;
9798 /* EABI binaries default to VFP float ordering.
9799 They may also contain build attributes that can
9800 be used to identify if the VFP argument-passing
9801 ABI is in use. */
9802 if (fp_model == ARM_FLOAT_AUTO)
9803 {
9804#ifdef HAVE_ELF
9805 switch (bfd_elf_get_obj_attr_int (info.abfd,
9806 OBJ_ATTR_PROC,
9807 Tag_ABI_VFP_args))
9808 {
b35b0298 9809 case AEABI_VFP_args_base:
9779414d
DJ
9810 /* "The user intended FP parameter/result
9811 passing to conform to AAPCS, base
9812 variant". */
9813 fp_model = ARM_FLOAT_SOFT_VFP;
9814 break;
b35b0298 9815 case AEABI_VFP_args_vfp:
9779414d
DJ
9816 /* "The user intended FP parameter/result
9817 passing to conform to AAPCS, VFP
9818 variant". */
9819 fp_model = ARM_FLOAT_VFP;
9820 break;
b35b0298 9821 case AEABI_VFP_args_toolchain:
9779414d
DJ
9822 /* "The user intended FP parameter/result
9823 passing to conform to tool chain-specific
9824 conventions" - we don't know any such
9825 conventions, so leave it as "auto". */
9826 break;
b35b0298 9827 case AEABI_VFP_args_compatible:
5c294fee
TG
9828 /* "Code is compatible with both the base
9829 and VFP variants; the user did not permit
9830 non-variadic functions to pass FP
9831 parameters/results" - leave it as
9832 "auto". */
9833 break;
9779414d
DJ
9834 default:
9835 /* Attribute value not mentioned in the
5c294fee 9836 November 2012 ABI, so leave it as
9779414d
DJ
9837 "auto". */
9838 break;
9839 }
9840#else
9841 fp_model = ARM_FLOAT_SOFT_VFP;
9842#endif
9843 }
9844 break;
9845
9846 default:
9847 /* Leave it as "auto". */
9848 warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
9849 break;
9850 }
9851
9852#ifdef HAVE_ELF
9853 /* Detect M-profile programs. This only works if the
9854 executable file includes build attributes; GCC does
9855 copy them to the executable, but e.g. RealView does
9856 not. */
17cbafdb
SM
9857 int attr_arch
9858 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9859 Tag_CPU_arch);
9860 int attr_profile
9861 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9862 Tag_CPU_arch_profile);
9863
9779414d
DJ
9864 /* GCC specifies the profile for v6-M; RealView only
9865 specifies the profile for architectures starting with
9866 V7 (as opposed to architectures with a tag
9867 numerically greater than TAG_CPU_ARCH_V7). */
9868 if (!tdesc_has_registers (tdesc)
9869 && (attr_arch == TAG_CPU_ARCH_V6_M
9870 || attr_arch == TAG_CPU_ARCH_V6S_M
131a355f
LM
9871 || attr_arch == TAG_CPU_ARCH_V7E_M
9872 || attr_arch == TAG_CPU_ARCH_V8M_BASE
9873 || attr_arch == TAG_CPU_ARCH_V8M_MAIN
ae66a8f1 9874 || attr_arch == TAG_CPU_ARCH_V8_1M_MAIN
9779414d 9875 || attr_profile == 'M'))
7559c217 9876 is_m = true;
a01567f4
LM
9877
9878 /* Look for attributes that indicate support for ARMv8.1-m
9879 PACBTI. */
9880 if (!tdesc_has_registers (tdesc) && is_m)
9881 {
9882 int attr_pac_extension
9883 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9884 Tag_PAC_extension);
9885
9886 int attr_bti_extension
9887 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9888 Tag_BTI_extension);
9889
9890 int attr_pacret_use
9891 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9892 Tag_PACRET_use);
9893
9894 int attr_bti_use
9895 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9896 Tag_BTI_use);
9897
9898 if (attr_pac_extension != 0 || attr_bti_extension != 0
9899 || attr_pacret_use != 0 || attr_bti_use != 0)
9900 have_pacbti = true;
9901 }
9779414d
DJ
9902#endif
9903 }
9904
9905 if (fp_model == ARM_FLOAT_AUTO)
9906 {
9779414d
DJ
9907 switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
9908 {
9909 case 0:
9910 /* Leave it as "auto". Strictly speaking this case
9911 means FPA, but almost nobody uses that now, and
9912 many toolchains fail to set the appropriate bits
9913 for the floating-point model they use. */
9914 break;
9915 case EF_ARM_SOFT_FLOAT:
9916 fp_model = ARM_FLOAT_SOFT_FPA;
9917 break;
9918 case EF_ARM_VFP_FLOAT:
9919 fp_model = ARM_FLOAT_VFP;
9920 break;
9921 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
9922 fp_model = ARM_FLOAT_SOFT_VFP;
9923 break;
9924 }
9925 }
9926
9927 if (e_flags & EF_ARM_BE8)
9928 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
9929
9930 break;
9931
9932 default:
9933 /* Leave it as "auto". */
9934 break;
9935 }
9936 }
123dc839
DJ
9937
9938 /* Check any target description for validity. */
9779414d 9939 if (tdesc_has_registers (tdesc))
123dc839
DJ
9940 {
9941 /* For most registers we require GDB's default names; but also allow
9942 the numeric names for sp / lr / pc, as a convenience. */
9943 static const char *const arm_sp_names[] = { "r13", "sp", NULL };
9944 static const char *const arm_lr_names[] = { "r14", "lr", NULL };
9945 static const char *const arm_pc_names[] = { "r15", "pc", NULL };
9946
9947 const struct tdesc_feature *feature;
58d6951d 9948 int valid_p;
123dc839 9949
9779414d 9950 feature = tdesc_find_feature (tdesc,
123dc839
DJ
9951 "org.gnu.gdb.arm.core");
9952 if (feature == NULL)
9779414d
DJ
9953 {
9954 feature = tdesc_find_feature (tdesc,
9955 "org.gnu.gdb.arm.m-profile");
9956 if (feature == NULL)
9957 return NULL;
9958 else
7559c217 9959 is_m = true;
9779414d 9960 }
123dc839
DJ
9961
9962 tdesc_data = tdesc_data_alloc ();
9963
9964 valid_p = 1;
9965 for (i = 0; i < ARM_SP_REGNUM; i++)
c1e1314d 9966 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
123dc839 9967 arm_register_names[i]);
c1e1314d 9968 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
123dc839
DJ
9969 ARM_SP_REGNUM,
9970 arm_sp_names);
c1e1314d 9971 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
123dc839
DJ
9972 ARM_LR_REGNUM,
9973 arm_lr_names);
c1e1314d 9974 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
123dc839
DJ
9975 ARM_PC_REGNUM,
9976 arm_pc_names);
9779414d 9977 if (is_m)
c1e1314d 9978 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
9779414d
DJ
9979 ARM_PS_REGNUM, "xpsr");
9980 else
c1e1314d 9981 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
9779414d 9982 ARM_PS_REGNUM, "cpsr");
123dc839
DJ
9983
9984 if (!valid_p)
c1e1314d 9985 return NULL;
123dc839 9986
9074667a
CL
9987 if (is_m)
9988 {
9989 feature = tdesc_find_feature (tdesc,
9990 "org.gnu.gdb.arm.m-system");
9991 if (feature != nullptr)
9992 {
9993 /* MSP */
9994 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
9995 register_count, "msp");
9996 if (!valid_p)
9997 {
9998 warning (_("M-profile m-system feature is missing required register msp."));
9999 return nullptr;
10000 }
10001 have_m_profile_msp = true;
10002 m_profile_msp_regnum = register_count++;
10003
10004 /* PSP */
10005 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10006 register_count, "psp");
10007 if (!valid_p)
10008 {
10009 warning (_("M-profile m-system feature is missing required register psp."));
10010 return nullptr;
10011 }
10012 m_profile_psp_regnum = register_count++;
10013 }
10014 }
10015
9779414d 10016 feature = tdesc_find_feature (tdesc,
123dc839
DJ
10017 "org.gnu.gdb.arm.fpa");
10018 if (feature != NULL)
10019 {
10020 valid_p = 1;
10021 for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
c1e1314d 10022 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
123dc839
DJ
10023 arm_register_names[i]);
10024 if (!valid_p)
c1e1314d 10025 return NULL;
123dc839 10026 }
ff6f572f 10027 else
7559c217 10028 have_fpa_registers = false;
ff6f572f 10029
9779414d 10030 feature = tdesc_find_feature (tdesc,
ff6f572f
DJ
10031 "org.gnu.gdb.xscale.iwmmxt");
10032 if (feature != NULL)
10033 {
10034 static const char *const iwmmxt_names[] = {
10035 "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
10036 "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
10037 "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
10038 "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
10039 };
10040
10041 valid_p = 1;
10042 for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
10043 valid_p
c1e1314d 10044 &= tdesc_numbered_register (feature, tdesc_data.get (), i,
ff6f572f
DJ
10045 iwmmxt_names[i - ARM_WR0_REGNUM]);
10046
10047 /* Check for the control registers, but do not fail if they
10048 are missing. */
10049 for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
c1e1314d 10050 tdesc_numbered_register (feature, tdesc_data.get (), i,
ff6f572f
DJ
10051 iwmmxt_names[i - ARM_WR0_REGNUM]);
10052
10053 for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
10054 valid_p
c1e1314d 10055 &= tdesc_numbered_register (feature, tdesc_data.get (), i,
ff6f572f
DJ
10056 iwmmxt_names[i - ARM_WR0_REGNUM]);
10057
10058 if (!valid_p)
c1e1314d 10059 return NULL;
a56cc1ce 10060
7559c217 10061 have_wmmx_registers = true;
ff6f572f 10062 }
58d6951d
DJ
10063
10064 /* If we have a VFP unit, check whether the single precision registers
10065 are present. If not, then we will synthesize them as pseudo
10066 registers. */
9779414d 10067 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
10068 "org.gnu.gdb.arm.vfp");
10069 if (feature != NULL)
10070 {
10071 static const char *const vfp_double_names[] = {
10072 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
10073 "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
10074 "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
10075 "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
10076 };
10077
10078 /* Require the double precision registers. There must be either
10079 16 or 32. */
10080 valid_p = 1;
10081 for (i = 0; i < 32; i++)
10082 {
c1e1314d 10083 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
58d6951d
DJ
10084 ARM_D0_REGNUM + i,
10085 vfp_double_names[i]);
10086 if (!valid_p)
10087 break;
10088 }
2b9e5ea6
UW
10089 if (!valid_p && i == 16)
10090 valid_p = 1;
58d6951d 10091
2b9e5ea6 10092 /* Also require FPSCR. */
c1e1314d 10093 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
2b9e5ea6
UW
10094 ARM_FPSCR_REGNUM, "fpscr");
10095 if (!valid_p)
c1e1314d 10096 return NULL;
58d6951d 10097
ae66a8f1
SP
10098 have_vfp = true;
10099
58d6951d 10100 if (tdesc_unnumbered_register (feature, "s0") == 0)
ecbf5d4f 10101 have_s_pseudos = true;
58d6951d 10102
330c6ca9 10103 vfp_register_count = i;
58d6951d
DJ
10104
10105 /* If we have VFP, also check for NEON. The architecture allows
10106 NEON without VFP (integer vector operations only), but GDB
10107 does not support that. */
9779414d 10108 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
10109 "org.gnu.gdb.arm.neon");
10110 if (feature != NULL)
10111 {
10112 /* NEON requires 32 double-precision registers. */
10113 if (i != 32)
c1e1314d 10114 return NULL;
58d6951d
DJ
10115
10116 /* If there are quad registers defined by the stub, use
10117 their type; otherwise (normally) provide them with
10118 the default type. */
10119 if (tdesc_unnumbered_register (feature, "q0") == 0)
ecbf5d4f 10120 have_q_pseudos = true;
ae66a8f1
SP
10121 }
10122 }
10123
92d48a1e
JB
10124 /* Check for the TLS register feature. */
10125 feature = tdesc_find_feature (tdesc, "org.gnu.gdb.arm.tls");
10126 if (feature != nullptr)
10127 {
10128 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10129 register_count, "tpidruro");
10130 if (!valid_p)
10131 return nullptr;
10132
10133 tls_regnum = register_count;
10134 register_count++;
10135 }
10136
ae66a8f1
SP
10137 /* Check for MVE after all the checks for GPR's, VFP and Neon.
10138 MVE (Helium) is an M-profile extension. */
10139 if (is_m)
10140 {
10141 /* Do we have the MVE feature? */
10142 feature = tdesc_find_feature (tdesc,"org.gnu.gdb.arm.m-profile-mve");
10143
10144 if (feature != nullptr)
10145 {
10146 /* If we have MVE, we must always have the VPR register. */
10147 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10148 register_count, "vpr");
10149 if (!valid_p)
10150 {
10151 warning (_("MVE feature is missing required register vpr."));
10152 return nullptr;
10153 }
58d6951d 10154
ae66a8f1
SP
10155 have_mve = true;
10156 mve_vpr_regnum = register_count;
10157 register_count++;
10158
10159 /* We can't have Q pseudo registers available here, as that
10160 would mean we have NEON features, and that is only available
10161 on A and R profiles. */
10162 gdb_assert (!have_q_pseudos);
10163
10164 /* Given we have a M-profile target description, if MVE is
10165 enabled and there are VFP registers, we should have Q
10166 pseudo registers (Q0 ~ Q7). */
10167 if (have_vfp)
10168 have_q_pseudos = true;
58d6951d 10169 }
a01567f4
LM
10170
10171 /* Do we have the ARMv8.1-m PACBTI feature? */
10172 feature = tdesc_find_feature (tdesc,
10173 "org.gnu.gdb.arm.m-profile-pacbti");
10174 if (feature != nullptr)
10175 {
10176 /* By advertising this feature, the target acknowledges the
10177 presence of the ARMv8.1-m PACBTI extensions.
10178
10179 We don't care for any particular registers in this group, so
10180 the target is free to include whatever it deems appropriate.
10181
10182 The expectation is for this feature to include the PAC
10183 keys. */
10184 have_pacbti = true;
10185 }
ae7e2f45
CL
10186
10187 /* Do we have the Security extension? */
10188 feature = tdesc_find_feature (tdesc,
10189 "org.gnu.gdb.arm.secext");
10190 if (feature != nullptr)
10191 {
10192 /* Secure/Non-secure stack pointers. */
10193 /* MSP_NS */
10194 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10195 register_count, "msp_ns");
10196 if (!valid_p)
10197 {
10198 warning (_("M-profile secext feature is missing required register msp_ns."));
10199 return nullptr;
10200 }
10201 m_profile_msp_ns_regnum = register_count++;
10202
10203 /* PSP_NS */
10204 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10205 register_count, "psp_ns");
10206 if (!valid_p)
10207 {
10208 warning (_("M-profile secext feature is missing required register psp_ns."));
10209 return nullptr;
10210 }
10211 m_profile_psp_ns_regnum = register_count++;
10212
10213 /* MSP_S */
10214 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10215 register_count, "msp_s");
10216 if (!valid_p)
10217 {
10218 warning (_("M-profile secext feature is missing required register msp_s."));
10219 return nullptr;
10220 }
10221 m_profile_msp_s_regnum = register_count++;
10222
10223 /* PSP_S */
10224 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10225 register_count, "psp_s");
10226 if (!valid_p)
10227 {
10228 warning (_("M-profile secext feature is missing required register psp_s."));
10229 return nullptr;
10230 }
10231 m_profile_psp_s_regnum = register_count++;
10232
10233 have_sec_ext = true;
10234 }
10235
58d6951d 10236 }
123dc839 10237 }
39bbf761 10238
28e97307
DJ
10239 /* If there is already a candidate, use it. */
10240 for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
10241 best_arch != NULL;
10242 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
10243 {
345bd07c
SM
10244 arm_gdbarch_tdep *tdep
10245 = (arm_gdbarch_tdep *) gdbarch_tdep (best_arch->gdbarch);
10246
10247 if (arm_abi != ARM_ABI_AUTO && arm_abi != tdep->arm_abi)
28e97307
DJ
10248 continue;
10249
345bd07c 10250 if (fp_model != ARM_FLOAT_AUTO && fp_model != tdep->fp_model)
28e97307
DJ
10251 continue;
10252
58d6951d
DJ
10253 /* There are various other properties in tdep that we do not
10254 need to check here: those derived from a target description,
10255 since gdbarches with a different target description are
10256 automatically disqualified. */
10257
9779414d 10258 /* Do check is_m, though, since it might come from the binary. */
345bd07c 10259 if (is_m != tdep->is_m)
9779414d
DJ
10260 continue;
10261
a01567f4
LM
10262 /* Also check for ARMv8.1-m PACBTI support, since it might come from
10263 the binary. */
10264 if (have_pacbti != tdep->have_pacbti)
10265 continue;
10266
28e97307
DJ
10267 /* Found a match. */
10268 break;
10269 }
97e03143 10270
28e97307 10271 if (best_arch != NULL)
c1e1314d 10272 return best_arch->gdbarch;
28e97307 10273
345bd07c 10274 arm_gdbarch_tdep *tdep = new arm_gdbarch_tdep;
97e03143
RE
10275 gdbarch = gdbarch_alloc (&info, tdep);
10276
28e97307
DJ
10277 /* Record additional information about the architecture we are defining.
10278 These are gdbarch discriminators, like the OSABI. */
10279 tdep->arm_abi = arm_abi;
10280 tdep->fp_model = fp_model;
9779414d 10281 tdep->is_m = is_m;
ae7e2f45 10282 tdep->have_sec_ext = have_sec_ext;
ff6f572f 10283 tdep->have_fpa_registers = have_fpa_registers;
a56cc1ce 10284 tdep->have_wmmx_registers = have_wmmx_registers;
330c6ca9
YQ
10285 gdb_assert (vfp_register_count == 0
10286 || vfp_register_count == 16
10287 || vfp_register_count == 32);
10288 tdep->vfp_register_count = vfp_register_count;
ecbf5d4f
LM
10289 tdep->have_s_pseudos = have_s_pseudos;
10290 tdep->have_q_pseudos = have_q_pseudos;
58d6951d 10291 tdep->have_neon = have_neon;
92d48a1e 10292 tdep->tls_regnum = tls_regnum;
08216dd7 10293
ae66a8f1
SP
10294 /* Adjust the MVE feature settings. */
10295 if (have_mve)
10296 {
10297 tdep->have_mve = true;
10298 tdep->mve_vpr_regnum = mve_vpr_regnum;
10299 }
10300
a01567f4
LM
10301 /* Adjust the PACBTI feature settings. */
10302 tdep->have_pacbti = have_pacbti;
10303
9074667a
CL
10304 /* Adjust the M-profile stack pointers settings. */
10305 if (have_m_profile_msp)
10306 {
10307 tdep->m_profile_msp_regnum = m_profile_msp_regnum;
10308 tdep->m_profile_psp_regnum = m_profile_psp_regnum;
ae7e2f45
CL
10309 tdep->m_profile_msp_ns_regnum = m_profile_msp_ns_regnum;
10310 tdep->m_profile_psp_ns_regnum = m_profile_psp_ns_regnum;
10311 tdep->m_profile_msp_s_regnum = m_profile_msp_s_regnum;
10312 tdep->m_profile_psp_s_regnum = m_profile_psp_s_regnum;
9074667a
CL
10313 }
10314
25f8c692
JL
10315 arm_register_g_packet_guesses (gdbarch);
10316
08216dd7 10317 /* Breakpoints. */
9d4fde75 10318 switch (info.byte_order_for_code)
67255d04
RE
10319 {
10320 case BFD_ENDIAN_BIG:
66e810cd
RE
10321 tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
10322 tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
10323 tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
10324 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
10325
67255d04
RE
10326 break;
10327
10328 case BFD_ENDIAN_LITTLE:
66e810cd
RE
10329 tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
10330 tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
10331 tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
10332 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
10333
67255d04
RE
10334 break;
10335
10336 default:
10337 internal_error (__FILE__, __LINE__,
edefbb7c 10338 _("arm_gdbarch_init: bad byte order for float format"));
67255d04
RE
10339 }
10340
d7b486e7
RE
10341 /* On ARM targets char defaults to unsigned. */
10342 set_gdbarch_char_signed (gdbarch, 0);
10343
53375380
PA
10344 /* wchar_t is unsigned under the AAPCS. */
10345 if (tdep->arm_abi == ARM_ABI_AAPCS)
10346 set_gdbarch_wchar_signed (gdbarch, 0);
10347 else
10348 set_gdbarch_wchar_signed (gdbarch, 1);
53375380 10349
030197b4
AB
10350 /* Compute type alignment. */
10351 set_gdbarch_type_align (gdbarch, arm_type_align);
10352
cca44b1b
JB
10353 /* Note: for displaced stepping, this includes the breakpoint, and one word
10354 of additional scratch space. This setting isn't used for anything beside
10355 displaced stepping at present. */
e935475c 10356 set_gdbarch_max_insn_length (gdbarch, 4 * ARM_DISPLACED_MODIFIED_INSNS);
cca44b1b 10357
9df628e0 10358 /* This should be low enough for everything. */
97e03143 10359 tdep->lowest_pc = 0x20;
94c30b78 10360 tdep->jb_pc = -1; /* Longjump support not enabled by default. */
97e03143 10361
7c00367c
MK
10362 /* The default, for both APCS and AAPCS, is to return small
10363 structures in registers. */
10364 tdep->struct_return = reg_struct_return;
10365
2dd604e7 10366 set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
f53f0d0b 10367 set_gdbarch_frame_align (gdbarch, arm_frame_align);
39bbf761 10368
7eb89530
YQ
10369 if (is_m)
10370 set_gdbarch_code_of_frame_writable (gdbarch, arm_code_of_frame_writable);
10371
756fe439
DJ
10372 set_gdbarch_write_pc (gdbarch, arm_write_pc);
10373
eb5492fa 10374 frame_base_set_default (gdbarch, &arm_normal_base);
148754e5 10375
34e8f22d 10376 /* Address manipulation. */
34e8f22d
RE
10377 set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
10378
34e8f22d
RE
10379 /* Advance PC across function entry code. */
10380 set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
10381
c9cf6e20
MG
10382 /* Detect whether PC is at a point where the stack has been destroyed. */
10383 set_gdbarch_stack_frame_destroyed_p (gdbarch, arm_stack_frame_destroyed_p);
4024ca99 10384
190dce09
UW
10385 /* Skip trampolines. */
10386 set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
10387
34e8f22d
RE
10388 /* The stack grows downward. */
10389 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
10390
10391 /* Breakpoint manipulation. */
04180708
YQ
10392 set_gdbarch_breakpoint_kind_from_pc (gdbarch, arm_breakpoint_kind_from_pc);
10393 set_gdbarch_sw_breakpoint_from_kind (gdbarch, arm_sw_breakpoint_from_kind);
833b7ab5
YQ
10394 set_gdbarch_breakpoint_kind_from_current_state (gdbarch,
10395 arm_breakpoint_kind_from_current_state);
34e8f22d
RE
10396
10397 /* Information about registers, etc. */
34e8f22d
RE
10398 set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
10399 set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
ecbf5d4f 10400 set_gdbarch_num_regs (gdbarch, register_count);
7a5ea0d4 10401 set_gdbarch_register_type (gdbarch, arm_register_type);
54483882 10402 set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);
34e8f22d 10403
ff6f572f
DJ
10404 /* This "info float" is FPA-specific. Use the generic version if we
10405 do not have FPA. */
345bd07c 10406 if (tdep->have_fpa_registers)
ff6f572f
DJ
10407 set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
10408
26216b98 10409 /* Internal <-> external register number maps. */
ff6f572f 10410 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
26216b98
AC
10411 set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
10412
34e8f22d
RE
10413 set_gdbarch_register_name (gdbarch, arm_register_name);
10414
10415 /* Returning results. */
2af48f68 10416 set_gdbarch_return_value (gdbarch, arm_return_value);
34e8f22d 10417
03d48a7d
RE
10418 /* Disassembly. */
10419 set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
10420
34e8f22d
RE
10421 /* Minsymbol frobbing. */
10422 set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
10423 set_gdbarch_coff_make_msymbol_special (gdbarch,
10424 arm_coff_make_msymbol_special);
60c5725c 10425 set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
34e8f22d 10426
f9d67f43
DJ
10427 /* Thumb-2 IT block support. */
10428 set_gdbarch_adjust_breakpoint_address (gdbarch,
10429 arm_adjust_breakpoint_address);
10430
0d5de010
DJ
10431 /* Virtual tables. */
10432 set_gdbarch_vbit_in_delta (gdbarch, 1);
10433
97e03143 10434 /* Hook in the ABI-specific overrides, if they have been registered. */
4be87837 10435 gdbarch_init_osabi (info, gdbarch);
97e03143 10436
b39cc962
DJ
10437 dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
10438
eb5492fa 10439 /* Add some default predicates. */
2ae28aa9
YQ
10440 if (is_m)
10441 frame_unwind_append_unwinder (gdbarch, &arm_m_exception_unwind);
a262aec2
DJ
10442 frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
10443 dwarf2_append_unwinders (gdbarch);
0e9e9abd 10444 frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
779aa56f 10445 frame_unwind_append_unwinder (gdbarch, &arm_epilogue_frame_unwind);
a262aec2 10446 frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
eb5492fa 10447
97e03143
RE
10448 /* Now we have tuned the configuration, set a few final things,
10449 based on what the OS ABI has told us. */
10450
b8926edc
DJ
10451 /* If the ABI is not otherwise marked, assume the old GNU APCS. EABI
10452 binaries are always marked. */
10453 if (tdep->arm_abi == ARM_ABI_AUTO)
10454 tdep->arm_abi = ARM_ABI_APCS;
10455
e3039479
UW
10456 /* Watchpoints are not steppable. */
10457 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
10458
b8926edc
DJ
10459 /* We used to default to FPA for generic ARM, but almost nobody
10460 uses that now, and we now provide a way for the user to force
10461 the model. So default to the most useful variant. */
10462 if (tdep->fp_model == ARM_FLOAT_AUTO)
10463 tdep->fp_model = ARM_FLOAT_SOFT_FPA;
10464
9df628e0
RE
10465 if (tdep->jb_pc >= 0)
10466 set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
10467
08216dd7 10468 /* Floating point sizes and format. */
8da61cc4 10469 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
b8926edc 10470 if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
08216dd7 10471 {
8da61cc4
DJ
10472 set_gdbarch_double_format
10473 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10474 set_gdbarch_long_double_format
10475 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10476 }
10477 else
10478 {
10479 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
10480 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
08216dd7
RE
10481 }
10482
a01567f4
LM
10483 /* Hook used to decorate frames with signed return addresses, only available
10484 for ARMv8.1-m PACBTI. */
10485 if (is_m && have_pacbti)
10486 set_gdbarch_get_pc_address_flags (gdbarch, arm_get_pc_address_flags);
10487
dc22c61a
LM
10488 if (tdesc_data != nullptr)
10489 {
10490 set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
10491
10492 tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
ecbf5d4f 10493 register_count = gdbarch_num_regs (gdbarch);
dc22c61a
LM
10494
10495 /* Override tdesc_register_type to adjust the types of VFP
10496 registers for NEON. */
10497 set_gdbarch_register_type (gdbarch, arm_register_type);
10498 }
10499
ecbf5d4f 10500 /* Initialize the pseudo register data. */
ae66a8f1 10501 int num_pseudos = 0;
ecbf5d4f 10502 if (tdep->have_s_pseudos)
58d6951d 10503 {
ecbf5d4f
LM
10504 /* VFP single precision pseudo registers (S0~S31). */
10505 tdep->s_pseudo_base = register_count;
10506 tdep->s_pseudo_count = 32;
ae66a8f1 10507 num_pseudos += tdep->s_pseudo_count;
ecbf5d4f
LM
10508
10509 if (tdep->have_q_pseudos)
10510 {
10511 /* NEON quad precision pseudo registers (Q0~Q15). */
10512 tdep->q_pseudo_base = register_count + num_pseudos;
ae66a8f1
SP
10513
10514 if (have_neon)
10515 tdep->q_pseudo_count = 16;
10516 else if (have_mve)
10517 tdep->q_pseudo_count = ARM_MVE_NUM_Q_REGS;
10518
ecbf5d4f
LM
10519 num_pseudos += tdep->q_pseudo_count;
10520 }
ae66a8f1
SP
10521 }
10522
10523 /* Do we have any MVE pseudo registers? */
10524 if (have_mve)
10525 {
10526 tdep->mve_pseudo_base = register_count + num_pseudos;
10527 tdep->mve_pseudo_count = 1;
10528 num_pseudos += tdep->mve_pseudo_count;
10529 }
58d6951d 10530
a01567f4
LM
10531 /* Do we have any ARMv8.1-m PACBTI pseudo registers. */
10532 if (have_pacbti)
10533 {
10534 tdep->pacbti_pseudo_base = register_count + num_pseudos;
10535 tdep->pacbti_pseudo_count = 1;
10536 num_pseudos += tdep->pacbti_pseudo_count;
10537 }
10538
ae66a8f1 10539 /* Set some pseudo register hooks, if we have pseudo registers. */
a01567f4 10540 if (tdep->have_s_pseudos || have_mve || have_pacbti)
ae66a8f1 10541 {
58d6951d
DJ
10542 set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
10543 set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
10544 set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
10545 }
10546
123dc839 10547 /* Add standard register aliases. We add aliases even for those
85102364 10548 names which are used by the current architecture - it's simpler,
123dc839
DJ
10549 and does no harm, since nothing ever lists user registers. */
10550 for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
10551 user_reg_add (gdbarch, arm_register_aliases[i].name,
10552 value_of_arm_user_reg, &arm_register_aliases[i].regnum);
10553
65b48a81
PB
10554 set_gdbarch_disassembler_options (gdbarch, &arm_disassembler_options);
10555 set_gdbarch_valid_disassembler_options (gdbarch, disassembler_options_arm ());
10556
3426ae57
AH
10557 set_gdbarch_gnu_triplet_regexp (gdbarch, arm_gnu_triplet_regexp);
10558
39bbf761
RE
10559 return gdbarch;
10560}
10561
97e03143 10562static void
2af46ca0 10563arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
97e03143 10564{
345bd07c 10565 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
97e03143
RE
10566
10567 if (tdep == NULL)
10568 return;
10569
6cb06a8c
TT
10570 gdb_printf (file, _("arm_dump_tdep: fp_model = %i\n"),
10571 (int) tdep->fp_model);
10572 gdb_printf (file, _("arm_dump_tdep: have_fpa_registers = %i\n"),
10573 (int) tdep->have_fpa_registers);
10574 gdb_printf (file, _("arm_dump_tdep: have_wmmx_registers = %i\n"),
10575 (int) tdep->have_wmmx_registers);
10576 gdb_printf (file, _("arm_dump_tdep: vfp_register_count = %i\n"),
10577 (int) tdep->vfp_register_count);
10578 gdb_printf (file, _("arm_dump_tdep: have_s_pseudos = %s\n"),
10579 tdep->have_s_pseudos? "true" : "false");
10580 gdb_printf (file, _("arm_dump_tdep: s_pseudo_base = %i\n"),
10581 (int) tdep->s_pseudo_base);
10582 gdb_printf (file, _("arm_dump_tdep: s_pseudo_count = %i\n"),
10583 (int) tdep->s_pseudo_count);
10584 gdb_printf (file, _("arm_dump_tdep: have_q_pseudos = %s\n"),
10585 tdep->have_q_pseudos? "true" : "false");
10586 gdb_printf (file, _("arm_dump_tdep: q_pseudo_base = %i\n"),
10587 (int) tdep->q_pseudo_base);
10588 gdb_printf (file, _("arm_dump_tdep: q_pseudo_count = %i\n"),
10589 (int) tdep->q_pseudo_count);
10590 gdb_printf (file, _("arm_dump_tdep: have_neon = %i\n"),
10591 (int) tdep->have_neon);
10592 gdb_printf (file, _("arm_dump_tdep: have_mve = %s\n"),
10593 tdep->have_mve? "yes" : "no");
10594 gdb_printf (file, _("arm_dump_tdep: mve_vpr_regnum = %i\n"),
10595 tdep->mve_vpr_regnum);
10596 gdb_printf (file, _("arm_dump_tdep: mve_pseudo_base = %i\n"),
10597 tdep->mve_pseudo_base);
10598 gdb_printf (file, _("arm_dump_tdep: mve_pseudo_count = %i\n"),
10599 tdep->mve_pseudo_count);
9074667a
CL
10600 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_regnum = %i\n"),
10601 tdep->m_profile_msp_regnum);
10602 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_regnum = %i\n"),
10603 tdep->m_profile_psp_regnum);
ae7e2f45
CL
10604 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_ns_regnum = %i\n"),
10605 tdep->m_profile_msp_ns_regnum);
10606 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_ns_regnum = %i\n"),
10607 tdep->m_profile_psp_ns_regnum);
10608 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_s_regnum = %i\n"),
10609 tdep->m_profile_msp_s_regnum);
10610 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_s_regnum = %i\n"),
10611 tdep->m_profile_psp_s_regnum);
6cb06a8c
TT
10612 gdb_printf (file, _("arm_dump_tdep: Lowest pc = 0x%lx\n"),
10613 (unsigned long) tdep->lowest_pc);
a01567f4
LM
10614 gdb_printf (file, _("arm_dump_tdep: have_pacbti = %s\n"),
10615 tdep->have_pacbti? "yes" : "no");
10616 gdb_printf (file, _("arm_dump_tdep: pacbti_pseudo_base = %i\n"),
10617 tdep->pacbti_pseudo_base);
10618 gdb_printf (file, _("arm_dump_tdep: pacbti_pseudo_count = %i\n"),
10619 tdep->pacbti_pseudo_count);
10620 gdb_printf (file, _("arm_dump_tdep: is_m = %s\n"),
10621 tdep->is_m? "yes" : "no");
97e03143
RE
10622}
10623
0d4c07af 10624#if GDB_SELF_TEST
b121eeb9
YQ
10625namespace selftests
10626{
10627static void arm_record_test (void);
9ecab40c 10628static void arm_analyze_prologue_test ();
b121eeb9 10629}
0d4c07af 10630#endif
b121eeb9 10631
6c265988 10632void _initialize_arm_tdep ();
c906108c 10633void
6c265988 10634_initialize_arm_tdep ()
c906108c 10635{
bc90b915 10636 long length;
65b48a81 10637 int i, j;
edefbb7c
AC
10638 char regdesc[1024], *rdptr = regdesc;
10639 size_t rest = sizeof (regdesc);
085dd6e6 10640
42cf1509 10641 gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
97e03143 10642
0e9e9abd 10643 /* Add ourselves to objfile event chain. */
c90e7d63 10644 gdb::observers::new_objfile.attach (arm_exidx_new_objfile, "arm-tdep");
0e9e9abd 10645
70f80edf
JT
10646 /* Register an ELF OS ABI sniffer for ARM binaries. */
10647 gdbarch_register_osabi_sniffer (bfd_arch_arm,
10648 bfd_target_elf_flavour,
10649 arm_elf_osabi_sniffer);
10650
afd7eef0 10651 /* Add root prefix command for all "set arm"/"show arm" commands. */
f54bdb6d
SM
10652 add_setshow_prefix_cmd ("arm", no_class,
10653 _("Various ARM-specific commands."),
10654 _("Various ARM-specific commands."),
10655 &setarmcmdlist, &showarmcmdlist,
10656 &setlist, &showlist);
c5aa993b 10657
65b48a81 10658 arm_disassembler_options = xstrdup ("reg-names-std");
471b9d15
MR
10659 const disasm_options_t *disasm_options
10660 = &disassembler_options_arm ()->options;
65b48a81
PB
10661 int num_disassembly_styles = 0;
10662 for (i = 0; disasm_options->name[i] != NULL; i++)
08dedd66 10663 if (startswith (disasm_options->name[i], "reg-names-"))
65b48a81
PB
10664 num_disassembly_styles++;
10665
10666 /* Initialize the array that will be passed to add_setshow_enum_cmd(). */
8d749320 10667 valid_disassembly_styles = XNEWVEC (const char *,
65b48a81
PB
10668 num_disassembly_styles + 1);
10669 for (i = j = 0; disasm_options->name[i] != NULL; i++)
08dedd66 10670 if (startswith (disasm_options->name[i], "reg-names-"))
65b48a81
PB
10671 {
10672 size_t offset = strlen ("reg-names-");
10673 const char *style = disasm_options->name[i];
10674 valid_disassembly_styles[j++] = &style[offset];
dedb7102
TT
10675 if (strcmp (&style[offset], "std") == 0)
10676 disassembly_style = &style[offset];
65b48a81
PB
10677 length = snprintf (rdptr, rest, "%s - %s\n", &style[offset],
10678 disasm_options->description[i]);
10679 rdptr += length;
10680 rest -= length;
10681 }
94c30b78 10682 /* Mark the end of valid options. */
65b48a81 10683 valid_disassembly_styles[num_disassembly_styles] = NULL;
c906108c 10684
edefbb7c 10685 /* Create the help text. */
d7e74731
PA
10686 std::string helptext = string_printf ("%s%s%s",
10687 _("The valid values are:\n"),
10688 regdesc,
10689 _("The default is \"std\"."));
ed9a39eb 10690
edefbb7c
AC
10691 add_setshow_enum_cmd("disassembler", no_class,
10692 valid_disassembly_styles, &disassembly_style,
10693 _("Set the disassembly style."),
10694 _("Show the disassembly style."),
09b0e4b0 10695 helptext.c_str (),
2c5b56ce 10696 set_disassembly_style_sfunc,
65b48a81 10697 show_disassembly_style_sfunc,
7376b4c2 10698 &setarmcmdlist, &showarmcmdlist);
edefbb7c
AC
10699
10700 add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
10701 _("Set usage of ARM 32-bit mode."),
10702 _("Show usage of ARM 32-bit mode."),
10703 _("When off, a 26-bit PC will be used."),
2c5b56ce 10704 NULL,
0963b4bd
MS
10705 NULL, /* FIXME: i18n: Usage of ARM 32-bit
10706 mode is %s. */
26304000 10707 &setarmcmdlist, &showarmcmdlist);
c906108c 10708
fd50bc42 10709 /* Add a command to allow the user to force the FPU model. */
edefbb7c
AC
10710 add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
10711 _("Set the floating point type."),
10712 _("Show the floating point type."),
10713 _("auto - Determine the FP typefrom the OS-ABI.\n\
10714softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
10715fpa - FPA co-processor (GCC compiled).\n\
10716softvfp - Software FP with pure-endian doubles.\n\
10717vfp - VFP co-processor."),
edefbb7c 10718 set_fp_model_sfunc, show_fp_model,
7376b4c2 10719 &setarmcmdlist, &showarmcmdlist);
fd50bc42 10720
28e97307
DJ
10721 /* Add a command to allow the user to force the ABI. */
10722 add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
10723 _("Set the ABI."),
10724 _("Show the ABI."),
10725 NULL, arm_set_abi, arm_show_abi,
10726 &setarmcmdlist, &showarmcmdlist);
10727
0428b8f5
DJ
10728 /* Add two commands to allow the user to force the assumed
10729 execution mode. */
10730 add_setshow_enum_cmd ("fallback-mode", class_support,
10731 arm_mode_strings, &arm_fallback_mode_string,
10732 _("Set the mode assumed when symbols are unavailable."),
10733 _("Show the mode assumed when symbols are unavailable."),
10734 NULL, NULL, arm_show_fallback_mode,
10735 &setarmcmdlist, &showarmcmdlist);
10736 add_setshow_enum_cmd ("force-mode", class_support,
10737 arm_mode_strings, &arm_force_mode_string,
10738 _("Set the mode assumed even when symbols are available."),
10739 _("Show the mode assumed even when symbols are available."),
10740 NULL, NULL, arm_show_force_mode,
10741 &setarmcmdlist, &showarmcmdlist);
10742
ef273377
CL
10743 /* Add a command to stop triggering security exceptions when
10744 unwinding exception stacks. */
10745 add_setshow_boolean_cmd ("unwind-secure-frames", no_class, &arm_unwind_secure_frames,
10746 _("Set usage of non-secure to secure exception stack unwinding."),
10747 _("Show usage of non-secure to secure exception stack unwinding."),
10748 _("When on, the debugger can trigger memory access traps."),
10749 NULL, arm_show_unwind_secure_frames,
10750 &setarmcmdlist, &showarmcmdlist);
10751
6529d2dd 10752 /* Debugging flag. */
edefbb7c
AC
10753 add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
10754 _("Set ARM debugging."),
10755 _("Show ARM debugging."),
10756 _("When on, arm-specific debugging is enabled."),
2c5b56ce 10757 NULL,
7915a72c 10758 NULL, /* FIXME: i18n: "ARM debugging is %s. */
26304000 10759 &setdebuglist, &showdebuglist);
b121eeb9
YQ
10760
10761#if GDB_SELF_TEST
1526853e 10762 selftests::register_test ("arm-record", selftests::arm_record_test);
9ecab40c 10763 selftests::register_test ("arm_analyze_prologue", selftests::arm_analyze_prologue_test);
b121eeb9
YQ
10764#endif
10765
c906108c 10766}
72508ac0
PO
10767
10768/* ARM-reversible process record data structures. */
10769
10770#define ARM_INSN_SIZE_BYTES 4
10771#define THUMB_INSN_SIZE_BYTES 2
10772#define THUMB2_INSN_SIZE_BYTES 4
10773
10774
71e396f9
LM
10775/* Position of the bit within a 32-bit ARM instruction
10776 that defines whether the instruction is a load or store. */
72508ac0
PO
10777#define INSN_S_L_BIT_NUM 20
10778
10779#define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
dda83cd7
SM
10780 do \
10781 { \
10782 unsigned int reg_len = LENGTH; \
10783 if (reg_len) \
10784 { \
10785 REGS = XNEWVEC (uint32_t, reg_len); \
10786 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
10787 } \
10788 } \
10789 while (0)
72508ac0
PO
10790
10791#define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
dda83cd7
SM
10792 do \
10793 { \
10794 unsigned int mem_len = LENGTH; \
10795 if (mem_len) \
01add95b
SM
10796 { \
10797 MEMS = XNEWVEC (struct arm_mem_r, mem_len); \
10798 memcpy(&MEMS->len, &RECORD_BUF[0], \
10799 sizeof(struct arm_mem_r) * LENGTH); \
10800 } \
dda83cd7
SM
10801 } \
10802 while (0)
72508ac0
PO
10803
10804/* Checks whether insn is already recorded or yet to be decoded. (boolean expression). */
10805#define INSN_RECORDED(ARM_RECORD) \
dda83cd7 10806 (0 != (ARM_RECORD)->reg_rec_count || 0 != (ARM_RECORD)->mem_rec_count)
72508ac0
PO
10807
10808/* ARM memory record structure. */
10809struct arm_mem_r
10810{
10811 uint32_t len; /* Record length. */
bfbbec00 10812 uint32_t addr; /* Memory address. */
72508ac0
PO
10813};
10814
10815/* ARM instruction record contains opcode of current insn
10816 and execution state (before entry to decode_insn()),
10817 contains list of to-be-modified registers and
10818 memory blocks (on return from decode_insn()). */
10819
4748a9be 10820struct arm_insn_decode_record
72508ac0
PO
10821{
10822 struct gdbarch *gdbarch;
10823 struct regcache *regcache;
10824 CORE_ADDR this_addr; /* Address of the insn being decoded. */
10825 uint32_t arm_insn; /* Should accommodate thumb. */
10826 uint32_t cond; /* Condition code. */
10827 uint32_t opcode; /* Insn opcode. */
10828 uint32_t decode; /* Insn decode bits. */
10829 uint32_t mem_rec_count; /* No of mem records. */
10830 uint32_t reg_rec_count; /* No of reg records. */
10831 uint32_t *arm_regs; /* Registers to be saved for this record. */
10832 struct arm_mem_r *arm_mems; /* Memory to be saved for this record. */
4748a9be 10833};
72508ac0
PO
10834
10835
10836/* Checks ARM SBZ and SBO mandatory fields. */
10837
10838static int
10839sbo_sbz (uint32_t insn, uint32_t bit_num, uint32_t len, uint32_t sbo)
10840{
10841 uint32_t ones = bits (insn, bit_num - 1, (bit_num -1) + (len - 1));
10842
10843 if (!len)
10844 return 1;
10845
10846 if (!sbo)
10847 ones = ~ones;
10848
10849 while (ones)
10850 {
10851 if (!(ones & sbo))
dda83cd7
SM
10852 {
10853 return 0;
10854 }
72508ac0
PO
10855 ones = ones >> 1;
10856 }
10857 return 1;
10858}
10859
c6ec2b30
OJ
10860enum arm_record_result
10861{
10862 ARM_RECORD_SUCCESS = 0,
10863 ARM_RECORD_FAILURE = 1
10864};
10865
0d1703b8 10866enum arm_record_strx_t
72508ac0
PO
10867{
10868 ARM_RECORD_STRH=1,
10869 ARM_RECORD_STRD
0d1703b8 10870};
72508ac0 10871
0d1703b8 10872enum record_type_t
72508ac0
PO
10873{
10874 ARM_RECORD=1,
10875 THUMB_RECORD,
10876 THUMB2_RECORD
0d1703b8 10877};
72508ac0
PO
10878
10879
10880static int
4748a9be 10881arm_record_strx (arm_insn_decode_record *arm_insn_r, uint32_t *record_buf,
dda83cd7 10882 uint32_t *record_buf_mem, arm_record_strx_t str_type)
72508ac0
PO
10883{
10884
10885 struct regcache *reg_cache = arm_insn_r->regcache;
10886 ULONGEST u_regval[2]= {0};
10887
10888 uint32_t reg_src1 = 0, reg_src2 = 0;
10889 uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
72508ac0
PO
10890
10891 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10892 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
72508ac0
PO
10893
10894 if (14 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
10895 {
10896 /* 1) Handle misc store, immediate offset. */
10897 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
10898 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
10899 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10900 regcache_raw_read_unsigned (reg_cache, reg_src1,
dda83cd7 10901 &u_regval[0]);
72508ac0 10902 if (ARM_PC_REGNUM == reg_src1)
dda83cd7
SM
10903 {
10904 /* If R15 was used as Rn, hence current PC+8. */
10905 u_regval[0] = u_regval[0] + 8;
10906 }
72508ac0
PO
10907 offset_8 = (immed_high << 4) | immed_low;
10908 /* Calculate target store address. */
10909 if (14 == arm_insn_r->opcode)
dda83cd7
SM
10910 {
10911 tgt_mem_addr = u_regval[0] + offset_8;
10912 }
72508ac0 10913 else
dda83cd7
SM
10914 {
10915 tgt_mem_addr = u_regval[0] - offset_8;
10916 }
72508ac0 10917 if (ARM_RECORD_STRH == str_type)
dda83cd7
SM
10918 {
10919 record_buf_mem[0] = 2;
10920 record_buf_mem[1] = tgt_mem_addr;
10921 arm_insn_r->mem_rec_count = 1;
10922 }
72508ac0 10923 else if (ARM_RECORD_STRD == str_type)
dda83cd7
SM
10924 {
10925 record_buf_mem[0] = 4;
10926 record_buf_mem[1] = tgt_mem_addr;
10927 record_buf_mem[2] = 4;
10928 record_buf_mem[3] = tgt_mem_addr + 4;
10929 arm_insn_r->mem_rec_count = 2;
10930 }
72508ac0
PO
10931 }
10932 else if (12 == arm_insn_r->opcode || 8 == arm_insn_r->opcode)
10933 {
10934 /* 2) Store, register offset. */
10935 /* Get Rm. */
10936 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10937 /* Get Rn. */
10938 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10939 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10940 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10941 if (15 == reg_src2)
dda83cd7
SM
10942 {
10943 /* If R15 was used as Rn, hence current PC+8. */
10944 u_regval[0] = u_regval[0] + 8;
10945 }
72508ac0
PO
10946 /* Calculate target store address, Rn +/- Rm, register offset. */
10947 if (12 == arm_insn_r->opcode)
dda83cd7
SM
10948 {
10949 tgt_mem_addr = u_regval[0] + u_regval[1];
10950 }
72508ac0 10951 else
dda83cd7
SM
10952 {
10953 tgt_mem_addr = u_regval[1] - u_regval[0];
10954 }
72508ac0 10955 if (ARM_RECORD_STRH == str_type)
dda83cd7
SM
10956 {
10957 record_buf_mem[0] = 2;
10958 record_buf_mem[1] = tgt_mem_addr;
10959 arm_insn_r->mem_rec_count = 1;
10960 }
72508ac0 10961 else if (ARM_RECORD_STRD == str_type)
dda83cd7
SM
10962 {
10963 record_buf_mem[0] = 4;
10964 record_buf_mem[1] = tgt_mem_addr;
10965 record_buf_mem[2] = 4;
10966 record_buf_mem[3] = tgt_mem_addr + 4;
10967 arm_insn_r->mem_rec_count = 2;
10968 }
72508ac0
PO
10969 }
10970 else if (11 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
dda83cd7 10971 || 2 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
72508ac0
PO
10972 {
10973 /* 3) Store, immediate pre-indexed. */
10974 /* 5) Store, immediate post-indexed. */
10975 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
10976 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
10977 offset_8 = (immed_high << 4) | immed_low;
10978 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10979 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10980 /* Calculate target store address, Rn +/- Rm, register offset. */
10981 if (15 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
dda83cd7
SM
10982 {
10983 tgt_mem_addr = u_regval[0] + offset_8;
10984 }
72508ac0 10985 else
dda83cd7
SM
10986 {
10987 tgt_mem_addr = u_regval[0] - offset_8;
10988 }
72508ac0 10989 if (ARM_RECORD_STRH == str_type)
dda83cd7
SM
10990 {
10991 record_buf_mem[0] = 2;
10992 record_buf_mem[1] = tgt_mem_addr;
10993 arm_insn_r->mem_rec_count = 1;
10994 }
72508ac0 10995 else if (ARM_RECORD_STRD == str_type)
dda83cd7
SM
10996 {
10997 record_buf_mem[0] = 4;
10998 record_buf_mem[1] = tgt_mem_addr;
10999 record_buf_mem[2] = 4;
11000 record_buf_mem[3] = tgt_mem_addr + 4;
11001 arm_insn_r->mem_rec_count = 2;
11002 }
72508ac0
PO
11003 /* Record Rn also as it changes. */
11004 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
11005 arm_insn_r->reg_rec_count = 1;
11006 }
11007 else if (9 == arm_insn_r->opcode || 13 == arm_insn_r->opcode
dda83cd7 11008 || 0 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
72508ac0
PO
11009 {
11010 /* 4) Store, register pre-indexed. */
11011 /* 6) Store, register post -indexed. */
11012 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11013 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11014 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11015 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11016 /* Calculate target store address, Rn +/- Rm, register offset. */
11017 if (13 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
dda83cd7
SM
11018 {
11019 tgt_mem_addr = u_regval[0] + u_regval[1];
11020 }
72508ac0 11021 else
dda83cd7
SM
11022 {
11023 tgt_mem_addr = u_regval[1] - u_regval[0];
11024 }
72508ac0 11025 if (ARM_RECORD_STRH == str_type)
dda83cd7
SM
11026 {
11027 record_buf_mem[0] = 2;
11028 record_buf_mem[1] = tgt_mem_addr;
11029 arm_insn_r->mem_rec_count = 1;
11030 }
72508ac0 11031 else if (ARM_RECORD_STRD == str_type)
dda83cd7
SM
11032 {
11033 record_buf_mem[0] = 4;
11034 record_buf_mem[1] = tgt_mem_addr;
11035 record_buf_mem[2] = 4;
11036 record_buf_mem[3] = tgt_mem_addr + 4;
11037 arm_insn_r->mem_rec_count = 2;
11038 }
72508ac0
PO
11039 /* Record Rn also as it changes. */
11040 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
11041 arm_insn_r->reg_rec_count = 1;
11042 }
11043 return 0;
11044}
11045
11046/* Handling ARM extension space insns. */
11047
11048static int
4748a9be 11049arm_record_extension_space (arm_insn_decode_record *arm_insn_r)
72508ac0 11050{
df95a9cf 11051 int ret = 0; /* Return value: -1:record failure ; 0:success */
72508ac0
PO
11052 uint32_t opcode1 = 0, opcode2 = 0, insn_op1 = 0;
11053 uint32_t record_buf[8], record_buf_mem[8];
11054 uint32_t reg_src1 = 0;
72508ac0
PO
11055 struct regcache *reg_cache = arm_insn_r->regcache;
11056 ULONGEST u_regval = 0;
11057
11058 gdb_assert (!INSN_RECORDED(arm_insn_r));
11059 /* Handle unconditional insn extension space. */
11060
11061 opcode1 = bits (arm_insn_r->arm_insn, 20, 27);
11062 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
11063 if (arm_insn_r->cond)
11064 {
11065 /* PLD has no affect on architectural state, it just affects
dda83cd7 11066 the caches. */
72508ac0 11067 if (5 == ((opcode1 & 0xE0) >> 5))
dda83cd7
SM
11068 {
11069 /* BLX(1) */
11070 record_buf[0] = ARM_PS_REGNUM;
11071 record_buf[1] = ARM_LR_REGNUM;
11072 arm_insn_r->reg_rec_count = 2;
11073 }
72508ac0
PO
11074 /* STC2, LDC2, MCR2, MRC2, CDP2: <TBD>, co-processor insn. */
11075 }
11076
11077
11078 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
11079 if (3 == opcode1 && bit (arm_insn_r->arm_insn, 4))
11080 {
11081 ret = -1;
11082 /* Undefined instruction on ARM V5; need to handle if later
dda83cd7 11083 versions define it. */
72508ac0
PO
11084 }
11085
11086 opcode1 = bits (arm_insn_r->arm_insn, 24, 27);
11087 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
11088 insn_op1 = bits (arm_insn_r->arm_insn, 20, 23);
11089
11090 /* Handle arithmetic insn extension space. */
11091 if (!opcode1 && 9 == opcode2 && 1 != arm_insn_r->cond
11092 && !INSN_RECORDED(arm_insn_r))
11093 {
11094 /* Handle MLA(S) and MUL(S). */
b020ff80 11095 if (in_inclusive_range (insn_op1, 0U, 3U))
01add95b
SM
11096 {
11097 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11098 record_buf[1] = ARM_PS_REGNUM;
11099 arm_insn_r->reg_rec_count = 2;
11100 }
b020ff80 11101 else if (in_inclusive_range (insn_op1, 4U, 15U))
01add95b
SM
11102 {
11103 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
11104 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11105 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11106 record_buf[2] = ARM_PS_REGNUM;
11107 arm_insn_r->reg_rec_count = 3;
11108 }
72508ac0
PO
11109 }
11110
11111 opcode1 = bits (arm_insn_r->arm_insn, 26, 27);
11112 opcode2 = bits (arm_insn_r->arm_insn, 23, 24);
11113 insn_op1 = bits (arm_insn_r->arm_insn, 21, 22);
11114
11115 /* Handle control insn extension space. */
11116
11117 if (!opcode1 && 2 == opcode2 && !bit (arm_insn_r->arm_insn, 20)
11118 && 1 != arm_insn_r->cond && !INSN_RECORDED(arm_insn_r))
11119 {
11120 if (!bit (arm_insn_r->arm_insn,25))
dda83cd7
SM
11121 {
11122 if (!bits (arm_insn_r->arm_insn, 4, 7))
11123 {
11124 if ((0 == insn_op1) || (2 == insn_op1))
11125 {
11126 /* MRS. */
11127 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11128 arm_insn_r->reg_rec_count = 1;
11129 }
11130 else if (1 == insn_op1)
11131 {
11132 /* CSPR is going to be changed. */
11133 record_buf[0] = ARM_PS_REGNUM;
11134 arm_insn_r->reg_rec_count = 1;
11135 }
11136 else if (3 == insn_op1)
11137 {
11138 /* SPSR is going to be changed. */
11139 /* We need to get SPSR value, which is yet to be done. */
11140 return -1;
11141 }
11142 }
11143 else if (1 == bits (arm_insn_r->arm_insn, 4, 7))
11144 {
11145 if (1 == insn_op1)
11146 {
11147 /* BX. */
11148 record_buf[0] = ARM_PS_REGNUM;
11149 arm_insn_r->reg_rec_count = 1;
11150 }
11151 else if (3 == insn_op1)
11152 {
11153 /* CLZ. */
11154 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11155 arm_insn_r->reg_rec_count = 1;
11156 }
11157 }
11158 else if (3 == bits (arm_insn_r->arm_insn, 4, 7))
11159 {
11160 /* BLX. */
11161 record_buf[0] = ARM_PS_REGNUM;
11162 record_buf[1] = ARM_LR_REGNUM;
11163 arm_insn_r->reg_rec_count = 2;
11164 }
11165 else if (5 == bits (arm_insn_r->arm_insn, 4, 7))
11166 {
11167 /* QADD, QSUB, QDADD, QDSUB */
11168 record_buf[0] = ARM_PS_REGNUM;
11169 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11170 arm_insn_r->reg_rec_count = 2;
11171 }
11172 else if (7 == bits (arm_insn_r->arm_insn, 4, 7))
11173 {
11174 /* BKPT. */
11175 record_buf[0] = ARM_PS_REGNUM;
11176 record_buf[1] = ARM_LR_REGNUM;
11177 arm_insn_r->reg_rec_count = 2;
11178
11179 /* Save SPSR also;how? */
11180 return -1;
11181 }
11182 else if(8 == bits (arm_insn_r->arm_insn, 4, 7)
11183 || 10 == bits (arm_insn_r->arm_insn, 4, 7)
11184 || 12 == bits (arm_insn_r->arm_insn, 4, 7)
11185 || 14 == bits (arm_insn_r->arm_insn, 4, 7)
11186 )
11187 {
11188 if (0 == insn_op1 || 1 == insn_op1)
11189 {
11190 /* SMLA<x><y>, SMLAW<y>, SMULW<y>. */
11191 /* We dont do optimization for SMULW<y> where we
11192 need only Rd. */
11193 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11194 record_buf[1] = ARM_PS_REGNUM;
11195 arm_insn_r->reg_rec_count = 2;
11196 }
11197 else if (2 == insn_op1)
11198 {
11199 /* SMLAL<x><y>. */
11200 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11201 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
11202 arm_insn_r->reg_rec_count = 2;
11203 }
11204 else if (3 == insn_op1)
11205 {
11206 /* SMUL<x><y>. */
11207 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11208 arm_insn_r->reg_rec_count = 1;
11209 }
11210 }
11211 }
72508ac0 11212 else
dda83cd7
SM
11213 {
11214 /* MSR : immediate form. */
11215 if (1 == insn_op1)
11216 {
11217 /* CSPR is going to be changed. */
11218 record_buf[0] = ARM_PS_REGNUM;
11219 arm_insn_r->reg_rec_count = 1;
11220 }
11221 else if (3 == insn_op1)
11222 {
11223 /* SPSR is going to be changed. */
11224 /* we need to get SPSR value, which is yet to be done */
11225 return -1;
11226 }
11227 }
72508ac0
PO
11228 }
11229
11230 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
11231 opcode2 = bits (arm_insn_r->arm_insn, 20, 24);
11232 insn_op1 = bits (arm_insn_r->arm_insn, 5, 6);
11233
11234 /* Handle load/store insn extension space. */
11235
11236 if (!opcode1 && bit (arm_insn_r->arm_insn, 7)
11237 && bit (arm_insn_r->arm_insn, 4) && 1 != arm_insn_r->cond
11238 && !INSN_RECORDED(arm_insn_r))
11239 {
11240 /* SWP/SWPB. */
11241 if (0 == insn_op1)
dda83cd7
SM
11242 {
11243 /* These insn, changes register and memory as well. */
11244 /* SWP or SWPB insn. */
11245 /* Get memory address given by Rn. */
11246 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11247 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11248 /* SWP insn ?, swaps word. */
11249 if (8 == arm_insn_r->opcode)
11250 {
11251 record_buf_mem[0] = 4;
11252 }
11253 else
11254 {
11255 /* SWPB insn, swaps only byte. */
11256 record_buf_mem[0] = 1;
11257 }
11258 record_buf_mem[1] = u_regval;
11259 arm_insn_r->mem_rec_count = 1;
11260 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11261 arm_insn_r->reg_rec_count = 1;
11262 }
72508ac0 11263 else if (1 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
dda83cd7
SM
11264 {
11265 /* STRH. */
11266 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11267 ARM_RECORD_STRH);
11268 }
72508ac0 11269 else if (2 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
dda83cd7
SM
11270 {
11271 /* LDRD. */
11272 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11273 record_buf[1] = record_buf[0] + 1;
11274 arm_insn_r->reg_rec_count = 2;
11275 }
72508ac0 11276 else if (3 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
dda83cd7
SM
11277 {
11278 /* STRD. */
11279 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11280 ARM_RECORD_STRD);
11281 }
72508ac0 11282 else if (bit (arm_insn_r->arm_insn, 20) && insn_op1 <= 3)
dda83cd7
SM
11283 {
11284 /* LDRH, LDRSB, LDRSH. */
11285 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11286 arm_insn_r->reg_rec_count = 1;
11287 }
72508ac0
PO
11288
11289 }
11290
11291 opcode1 = bits (arm_insn_r->arm_insn, 23, 27);
11292 if (24 == opcode1 && bit (arm_insn_r->arm_insn, 21)
11293 && !INSN_RECORDED(arm_insn_r))
11294 {
11295 ret = -1;
11296 /* Handle coprocessor insn extension space. */
11297 }
11298
11299 /* To be done for ARMv5 and later; as of now we return -1. */
11300 if (-1 == ret)
ca92db2d 11301 return ret;
72508ac0
PO
11302
11303 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11304 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11305
11306 return ret;
11307}
11308
11309/* Handling opcode 000 insns. */
11310
11311static int
4748a9be 11312arm_record_data_proc_misc_ld_str (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
11313{
11314 struct regcache *reg_cache = arm_insn_r->regcache;
11315 uint32_t record_buf[8], record_buf_mem[8];
11316 ULONGEST u_regval[2] = {0};
11317
8d49165d 11318 uint32_t reg_src1 = 0;
72508ac0
PO
11319 uint32_t opcode1 = 0;
11320
11321 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11322 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11323 opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
11324
2d9e6acb 11325 if (!((opcode1 & 0x19) == 0x10))
72508ac0 11326 {
2d9e6acb
YQ
11327 /* Data-processing (register) and Data-processing (register-shifted
11328 register */
11329 /* Out of 11 shifter operands mode, all the insn modifies destination
11330 register, which is specified by 13-16 decode. */
11331 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11332 record_buf[1] = ARM_PS_REGNUM;
11333 arm_insn_r->reg_rec_count = 2;
72508ac0 11334 }
2d9e6acb 11335 else if ((arm_insn_r->decode < 8) && ((opcode1 & 0x19) == 0x10))
72508ac0 11336 {
2d9e6acb
YQ
11337 /* Miscellaneous instructions */
11338
11339 if (3 == arm_insn_r->decode && 0x12 == opcode1
11340 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11341 {
11342 /* Handle BLX, branch and link/exchange. */
11343 if (9 == arm_insn_r->opcode)
11344 {
11345 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm,
11346 and R14 stores the return address. */
11347 record_buf[0] = ARM_PS_REGNUM;
11348 record_buf[1] = ARM_LR_REGNUM;
11349 arm_insn_r->reg_rec_count = 2;
11350 }
11351 }
11352 else if (7 == arm_insn_r->decode && 0x12 == opcode1)
11353 {
11354 /* Handle enhanced software breakpoint insn, BKPT. */
11355 /* CPSR is changed to be executed in ARM state, disabling normal
11356 interrupts, entering abort mode. */
11357 /* According to high vector configuration PC is set. */
11358 /* user hit breakpoint and type reverse, in
11359 that case, we need to go back with previous CPSR and
11360 Program Counter. */
11361 record_buf[0] = ARM_PS_REGNUM;
11362 record_buf[1] = ARM_LR_REGNUM;
11363 arm_insn_r->reg_rec_count = 2;
11364
11365 /* Save SPSR also; how? */
11366 return -1;
11367 }
11368 else if (1 == arm_insn_r->decode && 0x12 == opcode1
11369 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11370 {
11371 /* Handle BX, branch and link/exchange. */
11372 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm. */
11373 record_buf[0] = ARM_PS_REGNUM;
11374 arm_insn_r->reg_rec_count = 1;
11375 }
11376 else if (1 == arm_insn_r->decode && 0x16 == opcode1
11377 && sbo_sbz (arm_insn_r->arm_insn, 9, 4, 1)
11378 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1))
11379 {
11380 /* Count leading zeros: CLZ. */
11381 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11382 arm_insn_r->reg_rec_count = 1;
11383 }
11384 else if (!bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11385 && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11386 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1)
11387 && sbo_sbz (arm_insn_r->arm_insn, 1, 12, 0))
11388 {
11389 /* Handle MRS insn. */
11390 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11391 arm_insn_r->reg_rec_count = 1;
11392 }
72508ac0 11393 }
2d9e6acb 11394 else if (9 == arm_insn_r->decode && opcode1 < 0x10)
72508ac0 11395 {
2d9e6acb
YQ
11396 /* Multiply and multiply-accumulate */
11397
11398 /* Handle multiply instructions. */
11399 /* MLA, MUL, SMLAL, SMULL, UMLAL, UMULL. */
11400 if (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)
f2a883a8
SM
11401 {
11402 /* Handle MLA and MUL. */
11403 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11404 record_buf[1] = ARM_PS_REGNUM;
11405 arm_insn_r->reg_rec_count = 2;
11406 }
11407 else if (4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11408 {
11409 /* Handle SMLAL, SMULL, UMLAL, UMULL. */
11410 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11411 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11412 record_buf[2] = ARM_PS_REGNUM;
11413 arm_insn_r->reg_rec_count = 3;
11414 }
2d9e6acb
YQ
11415 }
11416 else if (9 == arm_insn_r->decode && opcode1 > 0x10)
11417 {
11418 /* Synchronization primitives */
11419
72508ac0
PO
11420 /* Handling SWP, SWPB. */
11421 /* These insn, changes register and memory as well. */
11422 /* SWP or SWPB insn. */
11423
11424 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11425 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11426 /* SWP insn ?, swaps word. */
11427 if (8 == arm_insn_r->opcode)
2d9e6acb
YQ
11428 {
11429 record_buf_mem[0] = 4;
11430 }
11431 else
11432 {
11433 /* SWPB insn, swaps only byte. */
11434 record_buf_mem[0] = 1;
11435 }
72508ac0
PO
11436 record_buf_mem[1] = u_regval[0];
11437 arm_insn_r->mem_rec_count = 1;
11438 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11439 arm_insn_r->reg_rec_count = 1;
11440 }
2d9e6acb
YQ
11441 else if (11 == arm_insn_r->decode || 13 == arm_insn_r->decode
11442 || 15 == arm_insn_r->decode)
72508ac0 11443 {
2d9e6acb
YQ
11444 if ((opcode1 & 0x12) == 2)
11445 {
11446 /* Extra load/store (unprivileged) */
11447 return -1;
11448 }
11449 else
11450 {
11451 /* Extra load/store */
11452 switch (bits (arm_insn_r->arm_insn, 5, 6))
11453 {
11454 case 1:
11455 if ((opcode1 & 0x05) == 0x0 || (opcode1 & 0x05) == 0x4)
11456 {
11457 /* STRH (register), STRH (immediate) */
11458 arm_record_strx (arm_insn_r, &record_buf[0],
11459 &record_buf_mem[0], ARM_RECORD_STRH);
11460 }
11461 else if ((opcode1 & 0x05) == 0x1)
11462 {
11463 /* LDRH (register) */
11464 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11465 arm_insn_r->reg_rec_count = 1;
72508ac0 11466
2d9e6acb
YQ
11467 if (bit (arm_insn_r->arm_insn, 21))
11468 {
11469 /* Write back to Rn. */
11470 record_buf[arm_insn_r->reg_rec_count++]
11471 = bits (arm_insn_r->arm_insn, 16, 19);
11472 }
11473 }
11474 else if ((opcode1 & 0x05) == 0x5)
11475 {
11476 /* LDRH (immediate), LDRH (literal) */
11477 int rn = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0 11478
2d9e6acb
YQ
11479 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11480 arm_insn_r->reg_rec_count = 1;
11481
11482 if (rn != 15)
11483 {
11484 /*LDRH (immediate) */
11485 if (bit (arm_insn_r->arm_insn, 21))
11486 {
11487 /* Write back to Rn. */
11488 record_buf[arm_insn_r->reg_rec_count++] = rn;
11489 }
11490 }
11491 }
11492 else
11493 return -1;
11494 break;
11495 case 2:
11496 if ((opcode1 & 0x05) == 0x0)
11497 {
11498 /* LDRD (register) */
11499 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11500 record_buf[1] = record_buf[0] + 1;
11501 arm_insn_r->reg_rec_count = 2;
11502
11503 if (bit (arm_insn_r->arm_insn, 21))
11504 {
11505 /* Write back to Rn. */
11506 record_buf[arm_insn_r->reg_rec_count++]
11507 = bits (arm_insn_r->arm_insn, 16, 19);
11508 }
11509 }
11510 else if ((opcode1 & 0x05) == 0x1)
11511 {
11512 /* LDRSB (register) */
11513 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11514 arm_insn_r->reg_rec_count = 1;
11515
11516 if (bit (arm_insn_r->arm_insn, 21))
11517 {
11518 /* Write back to Rn. */
11519 record_buf[arm_insn_r->reg_rec_count++]
11520 = bits (arm_insn_r->arm_insn, 16, 19);
11521 }
11522 }
11523 else if ((opcode1 & 0x05) == 0x4 || (opcode1 & 0x05) == 0x5)
11524 {
11525 /* LDRD (immediate), LDRD (literal), LDRSB (immediate),
11526 LDRSB (literal) */
11527 int rn = bits (arm_insn_r->arm_insn, 16, 19);
11528
11529 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11530 arm_insn_r->reg_rec_count = 1;
11531
11532 if (rn != 15)
11533 {
11534 /*LDRD (immediate), LDRSB (immediate) */
11535 if (bit (arm_insn_r->arm_insn, 21))
11536 {
11537 /* Write back to Rn. */
11538 record_buf[arm_insn_r->reg_rec_count++] = rn;
11539 }
11540 }
11541 }
11542 else
11543 return -1;
11544 break;
11545 case 3:
11546 if ((opcode1 & 0x05) == 0x0)
11547 {
11548 /* STRD (register) */
11549 arm_record_strx (arm_insn_r, &record_buf[0],
11550 &record_buf_mem[0], ARM_RECORD_STRD);
11551 }
11552 else if ((opcode1 & 0x05) == 0x1)
11553 {
11554 /* LDRSH (register) */
11555 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11556 arm_insn_r->reg_rec_count = 1;
11557
11558 if (bit (arm_insn_r->arm_insn, 21))
11559 {
11560 /* Write back to Rn. */
11561 record_buf[arm_insn_r->reg_rec_count++]
11562 = bits (arm_insn_r->arm_insn, 16, 19);
11563 }
11564 }
11565 else if ((opcode1 & 0x05) == 0x4)
11566 {
11567 /* STRD (immediate) */
11568 arm_record_strx (arm_insn_r, &record_buf[0],
11569 &record_buf_mem[0], ARM_RECORD_STRD);
11570 }
11571 else if ((opcode1 & 0x05) == 0x5)
11572 {
11573 /* LDRSH (immediate), LDRSH (literal) */
11574 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11575 arm_insn_r->reg_rec_count = 1;
11576
11577 if (bit (arm_insn_r->arm_insn, 21))
11578 {
11579 /* Write back to Rn. */
11580 record_buf[arm_insn_r->reg_rec_count++]
11581 = bits (arm_insn_r->arm_insn, 16, 19);
11582 }
11583 }
11584 else
11585 return -1;
11586 break;
11587 default:
11588 return -1;
11589 }
11590 }
72508ac0
PO
11591 }
11592 else
11593 {
11594 return -1;
11595 }
11596
11597 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11598 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11599 return 0;
11600}
11601
11602/* Handling opcode 001 insns. */
11603
11604static int
4748a9be 11605arm_record_data_proc_imm (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
11606{
11607 uint32_t record_buf[8], record_buf_mem[8];
11608
11609 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11610 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11611
11612 if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11613 && 2 == bits (arm_insn_r->arm_insn, 20, 21)
11614 && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
11615 )
11616 {
11617 /* Handle MSR insn. */
11618 if (9 == arm_insn_r->opcode)
dda83cd7
SM
11619 {
11620 /* CSPR is going to be changed. */
11621 record_buf[0] = ARM_PS_REGNUM;
11622 arm_insn_r->reg_rec_count = 1;
11623 }
72508ac0 11624 else
dda83cd7
SM
11625 {
11626 /* SPSR is going to be changed. */
11627 }
72508ac0
PO
11628 }
11629 else if (arm_insn_r->opcode <= 15)
11630 {
11631 /* Normal data processing insns. */
11632 /* Out of 11 shifter operands mode, all the insn modifies destination
dda83cd7 11633 register, which is specified by 13-16 decode. */
72508ac0
PO
11634 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11635 record_buf[1] = ARM_PS_REGNUM;
11636 arm_insn_r->reg_rec_count = 2;
11637 }
11638 else
11639 {
11640 return -1;
11641 }
11642
11643 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11644 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11645 return 0;
11646}
11647
c55978a6 11648static int
4748a9be 11649arm_record_media (arm_insn_decode_record *arm_insn_r)
c55978a6
YQ
11650{
11651 uint32_t record_buf[8];
11652
11653 switch (bits (arm_insn_r->arm_insn, 22, 24))
11654 {
11655 case 0:
11656 /* Parallel addition and subtraction, signed */
11657 case 1:
11658 /* Parallel addition and subtraction, unsigned */
11659 case 2:
11660 case 3:
11661 /* Packing, unpacking, saturation and reversal */
11662 {
11663 int rd = bits (arm_insn_r->arm_insn, 12, 15);
11664
11665 record_buf[arm_insn_r->reg_rec_count++] = rd;
11666 }
11667 break;
11668
11669 case 4:
11670 case 5:
11671 /* Signed multiplies */
11672 {
11673 int rd = bits (arm_insn_r->arm_insn, 16, 19);
11674 unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 22);
11675
11676 record_buf[arm_insn_r->reg_rec_count++] = rd;
11677 if (op1 == 0x0)
11678 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
11679 else if (op1 == 0x4)
11680 record_buf[arm_insn_r->reg_rec_count++]
11681 = bits (arm_insn_r->arm_insn, 12, 15);
11682 }
11683 break;
11684
11685 case 6:
11686 {
11687 if (bit (arm_insn_r->arm_insn, 21)
11688 && bits (arm_insn_r->arm_insn, 5, 6) == 0x2)
11689 {
11690 /* SBFX */
11691 record_buf[arm_insn_r->reg_rec_count++]
11692 = bits (arm_insn_r->arm_insn, 12, 15);
11693 }
11694 else if (bits (arm_insn_r->arm_insn, 20, 21) == 0x0
11695 && bits (arm_insn_r->arm_insn, 5, 7) == 0x0)
11696 {
11697 /* USAD8 and USADA8 */
11698 record_buf[arm_insn_r->reg_rec_count++]
11699 = bits (arm_insn_r->arm_insn, 16, 19);
11700 }
11701 }
11702 break;
11703
11704 case 7:
11705 {
11706 if (bits (arm_insn_r->arm_insn, 20, 21) == 0x3
11707 && bits (arm_insn_r->arm_insn, 5, 7) == 0x7)
11708 {
11709 /* Permanently UNDEFINED */
11710 return -1;
11711 }
11712 else
11713 {
11714 /* BFC, BFI and UBFX */
11715 record_buf[arm_insn_r->reg_rec_count++]
11716 = bits (arm_insn_r->arm_insn, 12, 15);
11717 }
11718 }
11719 break;
11720
11721 default:
11722 return -1;
11723 }
11724
11725 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11726
11727 return 0;
11728}
11729
71e396f9 11730/* Handle ARM mode instructions with opcode 010. */
72508ac0
PO
11731
11732static int
4748a9be 11733arm_record_ld_st_imm_offset (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
11734{
11735 struct regcache *reg_cache = arm_insn_r->regcache;
11736
71e396f9
LM
11737 uint32_t reg_base , reg_dest;
11738 uint32_t offset_12, tgt_mem_addr;
72508ac0 11739 uint32_t record_buf[8], record_buf_mem[8];
71e396f9
LM
11740 unsigned char wback;
11741 ULONGEST u_regval;
72508ac0 11742
71e396f9
LM
11743 /* Calculate wback. */
11744 wback = (bit (arm_insn_r->arm_insn, 24) == 0)
11745 || (bit (arm_insn_r->arm_insn, 21) == 1);
72508ac0 11746
71e396f9
LM
11747 arm_insn_r->reg_rec_count = 0;
11748 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0
PO
11749
11750 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11751 {
71e396f9
LM
11752 /* LDR (immediate), LDR (literal), LDRB (immediate), LDRB (literal), LDRBT
11753 and LDRT. */
11754
72508ac0 11755 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
71e396f9
LM
11756 record_buf[arm_insn_r->reg_rec_count++] = reg_dest;
11757
11758 /* The LDR instruction is capable of doing branching. If MOV LR, PC
11759 preceeds a LDR instruction having R15 as reg_base, it
11760 emulates a branch and link instruction, and hence we need to save
11761 CPSR and PC as well. */
11762 if (ARM_PC_REGNUM == reg_dest)
11763 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
11764
11765 /* If wback is true, also save the base register, which is going to be
11766 written to. */
11767 if (wback)
11768 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
11769 }
11770 else
11771 {
71e396f9
LM
11772 /* STR (immediate), STRB (immediate), STRBT and STRT. */
11773
72508ac0 11774 offset_12 = bits (arm_insn_r->arm_insn, 0, 11);
71e396f9
LM
11775 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
11776
11777 /* Handle bit U. */
72508ac0 11778 if (bit (arm_insn_r->arm_insn, 23))
71e396f9
LM
11779 {
11780 /* U == 1: Add the offset. */
11781 tgt_mem_addr = (uint32_t) u_regval + offset_12;
11782 }
72508ac0 11783 else
71e396f9
LM
11784 {
11785 /* U == 0: subtract the offset. */
11786 tgt_mem_addr = (uint32_t) u_regval - offset_12;
11787 }
11788
11789 /* Bit 22 tells us whether the store instruction writes 1 byte or 4
11790 bytes. */
11791 if (bit (arm_insn_r->arm_insn, 22))
11792 {
11793 /* STRB and STRBT: 1 byte. */
11794 record_buf_mem[0] = 1;
11795 }
11796 else
11797 {
11798 /* STR and STRT: 4 bytes. */
11799 record_buf_mem[0] = 4;
11800 }
11801
11802 /* Handle bit P. */
11803 if (bit (arm_insn_r->arm_insn, 24))
11804 record_buf_mem[1] = tgt_mem_addr;
11805 else
11806 record_buf_mem[1] = (uint32_t) u_regval;
72508ac0 11807
72508ac0
PO
11808 arm_insn_r->mem_rec_count = 1;
11809
71e396f9
LM
11810 /* If wback is true, also save the base register, which is going to be
11811 written to. */
11812 if (wback)
11813 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
11814 }
11815
11816 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11817 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11818 return 0;
11819}
11820
11821/* Handling opcode 011 insns. */
11822
11823static int
4748a9be 11824arm_record_ld_st_reg_offset (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
11825{
11826 struct regcache *reg_cache = arm_insn_r->regcache;
11827
11828 uint32_t shift_imm = 0;
11829 uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
11830 uint32_t offset_12 = 0, tgt_mem_addr = 0;
11831 uint32_t record_buf[8], record_buf_mem[8];
11832
11833 LONGEST s_word;
11834 ULONGEST u_regval[2];
11835
c55978a6
YQ
11836 if (bit (arm_insn_r->arm_insn, 4))
11837 return arm_record_media (arm_insn_r);
11838
72508ac0
PO
11839 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11840 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11841
11842 /* Handle enhanced store insns and LDRD DSP insn,
11843 order begins according to addressing modes for store insns
11844 STRH insn. */
11845
11846 /* LDR or STR? */
11847 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11848 {
11849 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11850 /* LDR insn has a capability to do branching, if
dda83cd7
SM
11851 MOV LR, PC is preceded by LDR insn having Rn as R15
11852 in that case, it emulates branch and link insn, and hence we
11853 need to save CSPR and PC as well. */
72508ac0 11854 if (15 != reg_dest)
dda83cd7
SM
11855 {
11856 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11857 arm_insn_r->reg_rec_count = 1;
11858 }
72508ac0 11859 else
dda83cd7
SM
11860 {
11861 record_buf[0] = reg_dest;
11862 record_buf[1] = ARM_PS_REGNUM;
11863 arm_insn_r->reg_rec_count = 2;
11864 }
72508ac0
PO
11865 }
11866 else
11867 {
11868 if (! bits (arm_insn_r->arm_insn, 4, 11))
dda83cd7
SM
11869 {
11870 /* Store insn, register offset and register pre-indexed,
11871 register post-indexed. */
11872 /* Get Rm. */
11873 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11874 /* Get Rn. */
11875 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11876 regcache_raw_read_unsigned (reg_cache, reg_src1
11877 , &u_regval[0]);
11878 regcache_raw_read_unsigned (reg_cache, reg_src2
11879 , &u_regval[1]);
11880 if (15 == reg_src2)
11881 {
11882 /* If R15 was used as Rn, hence current PC+8. */
11883 /* Pre-indexed mode doesnt reach here ; illegal insn. */
11884 u_regval[0] = u_regval[0] + 8;
11885 }
11886 /* Calculate target store address, Rn +/- Rm, register offset. */
11887 /* U == 1. */
11888 if (bit (arm_insn_r->arm_insn, 23))
11889 {
11890 tgt_mem_addr = u_regval[0] + u_regval[1];
11891 }
11892 else
11893 {
11894 tgt_mem_addr = u_regval[1] - u_regval[0];
11895 }
11896
11897 switch (arm_insn_r->opcode)
11898 {
11899 /* STR. */
11900 case 8:
11901 case 12:
11902 /* STR. */
11903 case 9:
11904 case 13:
11905 /* STRT. */
11906 case 1:
11907 case 5:
11908 /* STR. */
11909 case 0:
11910 case 4:
11911 record_buf_mem[0] = 4;
11912 break;
11913
11914 /* STRB. */
11915 case 10:
11916 case 14:
11917 /* STRB. */
11918 case 11:
11919 case 15:
11920 /* STRBT. */
11921 case 3:
11922 case 7:
11923 /* STRB. */
11924 case 2:
11925 case 6:
11926 record_buf_mem[0] = 1;
11927 break;
11928
11929 default:
11930 gdb_assert_not_reached ("no decoding pattern found");
11931 break;
11932 }
11933 record_buf_mem[1] = tgt_mem_addr;
11934 arm_insn_r->mem_rec_count = 1;
11935
11936 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11937 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11938 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11939 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11940 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11941 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11942 )
11943 {
11944 /* Rn is going to be changed in pre-indexed mode and
11945 post-indexed mode as well. */
11946 record_buf[0] = reg_src2;
11947 arm_insn_r->reg_rec_count = 1;
11948 }
11949 }
72508ac0 11950 else
dda83cd7
SM
11951 {
11952 /* Store insn, scaled register offset; scaled pre-indexed. */
11953 offset_12 = bits (arm_insn_r->arm_insn, 5, 6);
11954 /* Get Rm. */
11955 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11956 /* Get Rn. */
11957 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11958 /* Get shift_imm. */
11959 shift_imm = bits (arm_insn_r->arm_insn, 7, 11);
11960 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11961 regcache_raw_read_signed (reg_cache, reg_src1, &s_word);
11962 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11963 /* Offset_12 used as shift. */
11964 switch (offset_12)
11965 {
11966 case 0:
11967 /* Offset_12 used as index. */
11968 offset_12 = u_regval[0] << shift_imm;
11969 break;
11970
11971 case 1:
11972 offset_12 = (!shift_imm)?0:u_regval[0] >> shift_imm;
11973 break;
11974
11975 case 2:
11976 if (!shift_imm)
11977 {
11978 if (bit (u_regval[0], 31))
11979 {
11980 offset_12 = 0xFFFFFFFF;
11981 }
11982 else
11983 {
11984 offset_12 = 0;
11985 }
11986 }
11987 else
11988 {
11989 /* This is arithmetic shift. */
11990 offset_12 = s_word >> shift_imm;
11991 }
11992 break;
11993
11994 case 3:
11995 if (!shift_imm)
11996 {
11997 regcache_raw_read_unsigned (reg_cache, ARM_PS_REGNUM,
11998 &u_regval[1]);
11999 /* Get C flag value and shift it by 31. */
12000 offset_12 = (((bit (u_regval[1], 29)) << 31) \
12001 | (u_regval[0]) >> 1);
12002 }
12003 else
12004 {
12005 offset_12 = (u_regval[0] >> shift_imm) \
12006 | (u_regval[0] <<
12007 (sizeof(uint32_t) - shift_imm));
12008 }
12009 break;
12010
12011 default:
12012 gdb_assert_not_reached ("no decoding pattern found");
12013 break;
12014 }
12015
12016 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
12017 /* bit U set. */
12018 if (bit (arm_insn_r->arm_insn, 23))
12019 {
12020 tgt_mem_addr = u_regval[1] + offset_12;
12021 }
12022 else
12023 {
12024 tgt_mem_addr = u_regval[1] - offset_12;
12025 }
12026
12027 switch (arm_insn_r->opcode)
12028 {
12029 /* STR. */
12030 case 8:
12031 case 12:
12032 /* STR. */
12033 case 9:
12034 case 13:
12035 /* STRT. */
12036 case 1:
12037 case 5:
12038 /* STR. */
12039 case 0:
12040 case 4:
12041 record_buf_mem[0] = 4;
12042 break;
12043
12044 /* STRB. */
12045 case 10:
12046 case 14:
12047 /* STRB. */
12048 case 11:
12049 case 15:
12050 /* STRBT. */
12051 case 3:
12052 case 7:
12053 /* STRB. */
12054 case 2:
12055 case 6:
12056 record_buf_mem[0] = 1;
12057 break;
12058
12059 default:
12060 gdb_assert_not_reached ("no decoding pattern found");
12061 break;
12062 }
12063 record_buf_mem[1] = tgt_mem_addr;
12064 arm_insn_r->mem_rec_count = 1;
12065
12066 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
12067 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
12068 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
12069 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
12070 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
12071 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
12072 )
12073 {
12074 /* Rn is going to be changed in register scaled pre-indexed
12075 mode,and scaled post indexed mode. */
12076 record_buf[0] = reg_src2;
12077 arm_insn_r->reg_rec_count = 1;
12078 }
12079 }
72508ac0
PO
12080 }
12081
12082 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12083 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12084 return 0;
12085}
12086
71e396f9 12087/* Handle ARM mode instructions with opcode 100. */
72508ac0
PO
12088
12089static int
4748a9be 12090arm_record_ld_st_multiple (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
12091{
12092 struct regcache *reg_cache = arm_insn_r->regcache;
71e396f9
LM
12093 uint32_t register_count = 0, register_bits;
12094 uint32_t reg_base, addr_mode;
72508ac0 12095 uint32_t record_buf[24], record_buf_mem[48];
71e396f9
LM
12096 uint32_t wback;
12097 ULONGEST u_regval;
72508ac0 12098
71e396f9
LM
12099 /* Fetch the list of registers. */
12100 register_bits = bits (arm_insn_r->arm_insn, 0, 15);
12101 arm_insn_r->reg_rec_count = 0;
12102
12103 /* Fetch the base register that contains the address we are loading data
12104 to. */
12105 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0 12106
71e396f9
LM
12107 /* Calculate wback. */
12108 wback = (bit (arm_insn_r->arm_insn, 21) == 1);
72508ac0
PO
12109
12110 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12111 {
71e396f9 12112 /* LDM/LDMIA/LDMFD, LDMDA/LDMFA, LDMDB and LDMIB. */
72508ac0 12113
71e396f9 12114 /* Find out which registers are going to be loaded from memory. */
72508ac0 12115 while (register_bits)
71e396f9
LM
12116 {
12117 if (register_bits & 0x00000001)
12118 record_buf[arm_insn_r->reg_rec_count++] = register_count;
12119 register_bits = register_bits >> 1;
12120 register_count++;
12121 }
72508ac0 12122
71e396f9
LM
12123
12124 /* If wback is true, also save the base register, which is going to be
12125 written to. */
12126 if (wback)
12127 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
12128
12129 /* Save the CPSR register. */
12130 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
72508ac0
PO
12131 }
12132 else
12133 {
71e396f9 12134 /* STM (STMIA, STMEA), STMDA (STMED), STMDB (STMFD) and STMIB (STMFA). */
72508ac0 12135
71e396f9
LM
12136 addr_mode = bits (arm_insn_r->arm_insn, 23, 24);
12137
12138 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
12139
12140 /* Find out how many registers are going to be stored to memory. */
72508ac0 12141 while (register_bits)
71e396f9
LM
12142 {
12143 if (register_bits & 0x00000001)
12144 register_count++;
12145 register_bits = register_bits >> 1;
12146 }
72508ac0
PO
12147
12148 switch (addr_mode)
71e396f9
LM
12149 {
12150 /* STMDA (STMED): Decrement after. */
12151 case 0:
12152 record_buf_mem[1] = (uint32_t) u_regval
f0452268 12153 - register_count * ARM_INT_REGISTER_SIZE + 4;
71e396f9
LM
12154 break;
12155 /* STM (STMIA, STMEA): Increment after. */
12156 case 1:
12157 record_buf_mem[1] = (uint32_t) u_regval;
12158 break;
12159 /* STMDB (STMFD): Decrement before. */
12160 case 2:
12161 record_buf_mem[1] = (uint32_t) u_regval
f0452268 12162 - register_count * ARM_INT_REGISTER_SIZE;
71e396f9
LM
12163 break;
12164 /* STMIB (STMFA): Increment before. */
12165 case 3:
f0452268 12166 record_buf_mem[1] = (uint32_t) u_regval + ARM_INT_REGISTER_SIZE;
71e396f9
LM
12167 break;
12168 default:
12169 gdb_assert_not_reached ("no decoding pattern found");
12170 break;
12171 }
72508ac0 12172
f0452268 12173 record_buf_mem[0] = register_count * ARM_INT_REGISTER_SIZE;
71e396f9
LM
12174 arm_insn_r->mem_rec_count = 1;
12175
12176 /* If wback is true, also save the base register, which is going to be
12177 written to. */
12178 if (wback)
12179 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
12180 }
12181
12182 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12183 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12184 return 0;
12185}
12186
12187/* Handling opcode 101 insns. */
12188
12189static int
4748a9be 12190arm_record_b_bl (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
12191{
12192 uint32_t record_buf[8];
12193
12194 /* Handle B, BL, BLX(1) insns. */
12195 /* B simply branches so we do nothing here. */
12196 /* Note: BLX(1) doesnt fall here but instead it falls into
12197 extension space. */
12198 if (bit (arm_insn_r->arm_insn, 24))
01add95b
SM
12199 {
12200 record_buf[0] = ARM_LR_REGNUM;
12201 arm_insn_r->reg_rec_count = 1;
12202 }
72508ac0
PO
12203
12204 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12205
12206 return 0;
12207}
12208
72508ac0 12209static int
4748a9be 12210arm_record_unsupported_insn (arm_insn_decode_record *arm_insn_r)
72508ac0 12211{
6cb06a8c
TT
12212 gdb_printf (gdb_stderr,
12213 _("Process record does not support instruction "
12214 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
12215 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
72508ac0
PO
12216
12217 return -1;
12218}
12219
5a578da5
OJ
12220/* Record handler for vector data transfer instructions. */
12221
12222static int
4748a9be 12223arm_record_vdata_transfer_insn (arm_insn_decode_record *arm_insn_r)
5a578da5
OJ
12224{
12225 uint32_t bits_a, bit_c, bit_l, reg_t, reg_v;
12226 uint32_t record_buf[4];
12227
5a578da5
OJ
12228 reg_t = bits (arm_insn_r->arm_insn, 12, 15);
12229 reg_v = bits (arm_insn_r->arm_insn, 21, 23);
12230 bits_a = bits (arm_insn_r->arm_insn, 21, 23);
12231 bit_l = bit (arm_insn_r->arm_insn, 20);
12232 bit_c = bit (arm_insn_r->arm_insn, 8);
12233
12234 /* Handle VMOV instruction. */
12235 if (bit_l && bit_c)
12236 {
12237 record_buf[0] = reg_t;
12238 arm_insn_r->reg_rec_count = 1;
12239 }
12240 else if (bit_l && !bit_c)
12241 {
12242 /* Handle VMOV instruction. */
12243 if (bits_a == 0x00)
dda83cd7 12244 {
f1771dce 12245 record_buf[0] = reg_t;
dda83cd7
SM
12246 arm_insn_r->reg_rec_count = 1;
12247 }
5a578da5
OJ
12248 /* Handle VMRS instruction. */
12249 else if (bits_a == 0x07)
dda83cd7
SM
12250 {
12251 if (reg_t == 15)
12252 reg_t = ARM_PS_REGNUM;
5a578da5 12253
dda83cd7
SM
12254 record_buf[0] = reg_t;
12255 arm_insn_r->reg_rec_count = 1;
12256 }
5a578da5
OJ
12257 }
12258 else if (!bit_l && !bit_c)
12259 {
12260 /* Handle VMOV instruction. */
12261 if (bits_a == 0x00)
dda83cd7 12262 {
f1771dce 12263 record_buf[0] = ARM_D0_REGNUM + reg_v;
5a578da5 12264
dda83cd7
SM
12265 arm_insn_r->reg_rec_count = 1;
12266 }
5a578da5
OJ
12267 /* Handle VMSR instruction. */
12268 else if (bits_a == 0x07)
dda83cd7
SM
12269 {
12270 record_buf[0] = ARM_FPSCR_REGNUM;
12271 arm_insn_r->reg_rec_count = 1;
12272 }
5a578da5
OJ
12273 }
12274 else if (!bit_l && bit_c)
12275 {
12276 /* Handle VMOV instruction. */
12277 if (!(bits_a & 0x04))
dda83cd7
SM
12278 {
12279 record_buf[0] = (reg_v | (bit (arm_insn_r->arm_insn, 7) << 4))
12280 + ARM_D0_REGNUM;
12281 arm_insn_r->reg_rec_count = 1;
12282 }
5a578da5
OJ
12283 /* Handle VDUP instruction. */
12284 else
dda83cd7
SM
12285 {
12286 if (bit (arm_insn_r->arm_insn, 21))
12287 {
12288 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
12289 record_buf[0] = reg_v + ARM_D0_REGNUM;
12290 record_buf[1] = reg_v + ARM_D0_REGNUM + 1;
12291 arm_insn_r->reg_rec_count = 2;
12292 }
12293 else
12294 {
12295 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
12296 record_buf[0] = reg_v + ARM_D0_REGNUM;
12297 arm_insn_r->reg_rec_count = 1;
12298 }
12299 }
12300 }
12301
12302 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12303 return 0;
12304}
12305
f20f80dd
OJ
12306/* Record handler for extension register load/store instructions. */
12307
12308static int
4748a9be 12309arm_record_exreg_ld_st_insn (arm_insn_decode_record *arm_insn_r)
f20f80dd
OJ
12310{
12311 uint32_t opcode, single_reg;
12312 uint8_t op_vldm_vstm;
12313 uint32_t record_buf[8], record_buf_mem[128];
12314 ULONGEST u_regval = 0;
12315
12316 struct regcache *reg_cache = arm_insn_r->regcache;
f20f80dd
OJ
12317
12318 opcode = bits (arm_insn_r->arm_insn, 20, 24);
9fde51ed 12319 single_reg = !bit (arm_insn_r->arm_insn, 8);
f20f80dd
OJ
12320 op_vldm_vstm = opcode & 0x1b;
12321
12322 /* Handle VMOV instructions. */
12323 if ((opcode & 0x1e) == 0x04)
12324 {
9fde51ed 12325 if (bit (arm_insn_r->arm_insn, 20)) /* to_arm_registers bit 20? */
01e57735
YQ
12326 {
12327 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12328 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
12329 arm_insn_r->reg_rec_count = 2;
12330 }
f20f80dd 12331 else
01e57735 12332 {
9fde51ed
YQ
12333 uint8_t reg_m = bits (arm_insn_r->arm_insn, 0, 3);
12334 uint8_t bit_m = bit (arm_insn_r->arm_insn, 5);
f20f80dd 12335
9fde51ed 12336 if (single_reg)
01e57735 12337 {
9fde51ed
YQ
12338 /* The first S register number m is REG_M:M (M is bit 5),
12339 the corresponding D register number is REG_M:M / 2, which
12340 is REG_M. */
12341 record_buf[arm_insn_r->reg_rec_count++] = ARM_D0_REGNUM + reg_m;
12342 /* The second S register number is REG_M:M + 1, the
12343 corresponding D register number is (REG_M:M + 1) / 2.
12344 IOW, if bit M is 1, the first and second S registers
12345 are mapped to different D registers, otherwise, they are
12346 in the same D register. */
12347 if (bit_m)
12348 {
12349 record_buf[arm_insn_r->reg_rec_count++]
12350 = ARM_D0_REGNUM + reg_m + 1;
12351 }
01e57735
YQ
12352 }
12353 else
12354 {
9fde51ed 12355 record_buf[0] = ((bit_m << 4) + reg_m + ARM_D0_REGNUM);
01e57735
YQ
12356 arm_insn_r->reg_rec_count = 1;
12357 }
12358 }
f20f80dd
OJ
12359 }
12360 /* Handle VSTM and VPUSH instructions. */
12361 else if (op_vldm_vstm == 0x08 || op_vldm_vstm == 0x0a
01e57735 12362 || op_vldm_vstm == 0x12)
f20f80dd
OJ
12363 {
12364 uint32_t start_address, reg_rn, imm_off32, imm_off8, memory_count;
12365 uint32_t memory_index = 0;
12366
12367 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
12368 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12369 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
9fde51ed 12370 imm_off32 = imm_off8 << 2;
f20f80dd
OJ
12371 memory_count = imm_off8;
12372
12373 if (bit (arm_insn_r->arm_insn, 23))
01e57735 12374 start_address = u_regval;
f20f80dd 12375 else
01e57735 12376 start_address = u_regval - imm_off32;
f20f80dd
OJ
12377
12378 if (bit (arm_insn_r->arm_insn, 21))
01e57735
YQ
12379 {
12380 record_buf[0] = reg_rn;
12381 arm_insn_r->reg_rec_count = 1;
12382 }
f20f80dd
OJ
12383
12384 while (memory_count > 0)
01e57735 12385 {
9fde51ed 12386 if (single_reg)
01e57735 12387 {
9fde51ed
YQ
12388 record_buf_mem[memory_index] = 4;
12389 record_buf_mem[memory_index + 1] = start_address;
01e57735
YQ
12390 start_address = start_address + 4;
12391 memory_index = memory_index + 2;
12392 }
12393 else
12394 {
9fde51ed
YQ
12395 record_buf_mem[memory_index] = 4;
12396 record_buf_mem[memory_index + 1] = start_address;
12397 record_buf_mem[memory_index + 2] = 4;
12398 record_buf_mem[memory_index + 3] = start_address + 4;
01e57735
YQ
12399 start_address = start_address + 8;
12400 memory_index = memory_index + 4;
12401 }
12402 memory_count--;
12403 }
f20f80dd
OJ
12404 arm_insn_r->mem_rec_count = (memory_index >> 1);
12405 }
12406 /* Handle VLDM instructions. */
12407 else if (op_vldm_vstm == 0x09 || op_vldm_vstm == 0x0b
01e57735 12408 || op_vldm_vstm == 0x13)
f20f80dd
OJ
12409 {
12410 uint32_t reg_count, reg_vd;
12411 uint32_t reg_index = 0;
9fde51ed 12412 uint32_t bit_d = bit (arm_insn_r->arm_insn, 22);
f20f80dd
OJ
12413
12414 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12415 reg_count = bits (arm_insn_r->arm_insn, 0, 7);
12416
9fde51ed
YQ
12417 /* REG_VD is the first D register number. If the instruction
12418 loads memory to S registers (SINGLE_REG is TRUE), the register
12419 number is (REG_VD << 1 | bit D), so the corresponding D
12420 register number is (REG_VD << 1 | bit D) / 2 = REG_VD. */
12421 if (!single_reg)
12422 reg_vd = reg_vd | (bit_d << 4);
f20f80dd 12423
9fde51ed 12424 if (bit (arm_insn_r->arm_insn, 21) /* write back */)
01e57735 12425 record_buf[reg_index++] = bits (arm_insn_r->arm_insn, 16, 19);
f20f80dd 12426
9fde51ed
YQ
12427 /* If the instruction loads memory to D register, REG_COUNT should
12428 be divided by 2, according to the ARM Architecture Reference
12429 Manual. If the instruction loads memory to S register, divide by
12430 2 as well because two S registers are mapped to D register. */
12431 reg_count = reg_count / 2;
12432 if (single_reg && bit_d)
01e57735 12433 {
9fde51ed
YQ
12434 /* Increase the register count if S register list starts from
12435 an odd number (bit d is one). */
12436 reg_count++;
12437 }
f20f80dd 12438
9fde51ed
YQ
12439 while (reg_count > 0)
12440 {
12441 record_buf[reg_index++] = ARM_D0_REGNUM + reg_vd + reg_count - 1;
01e57735
YQ
12442 reg_count--;
12443 }
f20f80dd
OJ
12444 arm_insn_r->reg_rec_count = reg_index;
12445 }
12446 /* VSTR Vector store register. */
12447 else if ((opcode & 0x13) == 0x10)
12448 {
bec2ab5a 12449 uint32_t start_address, reg_rn, imm_off32, imm_off8;
f20f80dd
OJ
12450 uint32_t memory_index = 0;
12451
12452 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
12453 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12454 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
9fde51ed 12455 imm_off32 = imm_off8 << 2;
f20f80dd
OJ
12456
12457 if (bit (arm_insn_r->arm_insn, 23))
01e57735 12458 start_address = u_regval + imm_off32;
f20f80dd 12459 else
01e57735 12460 start_address = u_regval - imm_off32;
f20f80dd
OJ
12461
12462 if (single_reg)
01e57735 12463 {
9fde51ed
YQ
12464 record_buf_mem[memory_index] = 4;
12465 record_buf_mem[memory_index + 1] = start_address;
01e57735
YQ
12466 arm_insn_r->mem_rec_count = 1;
12467 }
f20f80dd 12468 else
01e57735 12469 {
9fde51ed
YQ
12470 record_buf_mem[memory_index] = 4;
12471 record_buf_mem[memory_index + 1] = start_address;
12472 record_buf_mem[memory_index + 2] = 4;
12473 record_buf_mem[memory_index + 3] = start_address + 4;
01e57735
YQ
12474 arm_insn_r->mem_rec_count = 2;
12475 }
f20f80dd
OJ
12476 }
12477 /* VLDR Vector load register. */
12478 else if ((opcode & 0x13) == 0x11)
12479 {
12480 uint32_t reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12481
12482 if (!single_reg)
01e57735
YQ
12483 {
12484 reg_vd = reg_vd | (bit (arm_insn_r->arm_insn, 22) << 4);
12485 record_buf[0] = ARM_D0_REGNUM + reg_vd;
12486 }
f20f80dd 12487 else
01e57735
YQ
12488 {
12489 reg_vd = (reg_vd << 1) | bit (arm_insn_r->arm_insn, 22);
9fde51ed
YQ
12490 /* Record register D rather than pseudo register S. */
12491 record_buf[0] = ARM_D0_REGNUM + reg_vd / 2;
01e57735 12492 }
f20f80dd
OJ
12493 arm_insn_r->reg_rec_count = 1;
12494 }
12495
12496 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12497 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12498 return 0;
12499}
12500
851f26ae
OJ
12501/* Record handler for arm/thumb mode VFP data processing instructions. */
12502
12503static int
4748a9be 12504arm_record_vfp_data_proc_insn (arm_insn_decode_record *arm_insn_r)
851f26ae
OJ
12505{
12506 uint32_t opc1, opc2, opc3, dp_op_sz, bit_d, reg_vd;
12507 uint32_t record_buf[4];
12508 enum insn_types {INSN_T0, INSN_T1, INSN_T2, INSN_T3, INSN_INV};
12509 enum insn_types curr_insn_type = INSN_INV;
12510
12511 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12512 opc1 = bits (arm_insn_r->arm_insn, 20, 23);
12513 opc2 = bits (arm_insn_r->arm_insn, 16, 19);
12514 opc3 = bits (arm_insn_r->arm_insn, 6, 7);
12515 dp_op_sz = bit (arm_insn_r->arm_insn, 8);
12516 bit_d = bit (arm_insn_r->arm_insn, 22);
ce887586
TT
12517 /* Mask off the "D" bit. */
12518 opc1 = opc1 & ~0x04;
851f26ae
OJ
12519
12520 /* Handle VMLA, VMLS. */
12521 if (opc1 == 0x00)
12522 {
12523 if (bit (arm_insn_r->arm_insn, 10))
dda83cd7
SM
12524 {
12525 if (bit (arm_insn_r->arm_insn, 6))
12526 curr_insn_type = INSN_T0;
12527 else
12528 curr_insn_type = INSN_T1;
12529 }
851f26ae 12530 else
dda83cd7
SM
12531 {
12532 if (dp_op_sz)
12533 curr_insn_type = INSN_T1;
12534 else
12535 curr_insn_type = INSN_T2;
12536 }
851f26ae
OJ
12537 }
12538 /* Handle VNMLA, VNMLS, VNMUL. */
12539 else if (opc1 == 0x01)
12540 {
12541 if (dp_op_sz)
dda83cd7 12542 curr_insn_type = INSN_T1;
851f26ae 12543 else
dda83cd7 12544 curr_insn_type = INSN_T2;
851f26ae
OJ
12545 }
12546 /* Handle VMUL. */
12547 else if (opc1 == 0x02 && !(opc3 & 0x01))
12548 {
12549 if (bit (arm_insn_r->arm_insn, 10))
dda83cd7
SM
12550 {
12551 if (bit (arm_insn_r->arm_insn, 6))
12552 curr_insn_type = INSN_T0;
12553 else
12554 curr_insn_type = INSN_T1;
12555 }
851f26ae 12556 else
dda83cd7
SM
12557 {
12558 if (dp_op_sz)
12559 curr_insn_type = INSN_T1;
12560 else
12561 curr_insn_type = INSN_T2;
12562 }
851f26ae
OJ
12563 }
12564 /* Handle VADD, VSUB. */
12565 else if (opc1 == 0x03)
12566 {
12567 if (!bit (arm_insn_r->arm_insn, 9))
dda83cd7
SM
12568 {
12569 if (bit (arm_insn_r->arm_insn, 6))
12570 curr_insn_type = INSN_T0;
12571 else
12572 curr_insn_type = INSN_T1;
12573 }
851f26ae 12574 else
dda83cd7
SM
12575 {
12576 if (dp_op_sz)
12577 curr_insn_type = INSN_T1;
12578 else
12579 curr_insn_type = INSN_T2;
12580 }
851f26ae
OJ
12581 }
12582 /* Handle VDIV. */
ce887586 12583 else if (opc1 == 0x08)
851f26ae
OJ
12584 {
12585 if (dp_op_sz)
dda83cd7 12586 curr_insn_type = INSN_T1;
851f26ae 12587 else
dda83cd7 12588 curr_insn_type = INSN_T2;
851f26ae
OJ
12589 }
12590 /* Handle all other vfp data processing instructions. */
12591 else if (opc1 == 0x0b)
12592 {
12593 /* Handle VMOV. */
12594 if (!(opc3 & 0x01) || (opc2 == 0x00 && opc3 == 0x01))
dda83cd7
SM
12595 {
12596 if (bit (arm_insn_r->arm_insn, 4))
12597 {
12598 if (bit (arm_insn_r->arm_insn, 6))
12599 curr_insn_type = INSN_T0;
12600 else
12601 curr_insn_type = INSN_T1;
12602 }
12603 else
12604 {
12605 if (dp_op_sz)
12606 curr_insn_type = INSN_T1;
12607 else
12608 curr_insn_type = INSN_T2;
12609 }
12610 }
851f26ae
OJ
12611 /* Handle VNEG and VABS. */
12612 else if ((opc2 == 0x01 && opc3 == 0x01)
dda83cd7
SM
12613 || (opc2 == 0x00 && opc3 == 0x03))
12614 {
12615 if (!bit (arm_insn_r->arm_insn, 11))
12616 {
12617 if (bit (arm_insn_r->arm_insn, 6))
12618 curr_insn_type = INSN_T0;
12619 else
12620 curr_insn_type = INSN_T1;
12621 }
12622 else
12623 {
12624 if (dp_op_sz)
12625 curr_insn_type = INSN_T1;
12626 else
12627 curr_insn_type = INSN_T2;
12628 }
12629 }
851f26ae
OJ
12630 /* Handle VSQRT. */
12631 else if (opc2 == 0x01 && opc3 == 0x03)
dda83cd7
SM
12632 {
12633 if (dp_op_sz)
12634 curr_insn_type = INSN_T1;
12635 else
12636 curr_insn_type = INSN_T2;
12637 }
851f26ae
OJ
12638 /* Handle VCVT. */
12639 else if (opc2 == 0x07 && opc3 == 0x03)
dda83cd7
SM
12640 {
12641 if (!dp_op_sz)
12642 curr_insn_type = INSN_T1;
12643 else
12644 curr_insn_type = INSN_T2;
12645 }
851f26ae 12646 else if (opc3 & 0x01)
dda83cd7
SM
12647 {
12648 /* Handle VCVT. */
12649 if ((opc2 == 0x08) || (opc2 & 0x0e) == 0x0c)
12650 {
12651 if (!bit (arm_insn_r->arm_insn, 18))
12652 curr_insn_type = INSN_T2;
12653 else
12654 {
12655 if (dp_op_sz)
12656 curr_insn_type = INSN_T1;
12657 else
12658 curr_insn_type = INSN_T2;
12659 }
12660 }
12661 /* Handle VCVT. */
12662 else if ((opc2 & 0x0e) == 0x0a || (opc2 & 0x0e) == 0x0e)
12663 {
12664 if (dp_op_sz)
12665 curr_insn_type = INSN_T1;
12666 else
12667 curr_insn_type = INSN_T2;
12668 }
12669 /* Handle VCVTB, VCVTT. */
12670 else if ((opc2 & 0x0e) == 0x02)
12671 curr_insn_type = INSN_T2;
12672 /* Handle VCMP, VCMPE. */
12673 else if ((opc2 & 0x0e) == 0x04)
12674 curr_insn_type = INSN_T3;
12675 }
851f26ae
OJ
12676 }
12677
12678 switch (curr_insn_type)
12679 {
12680 case INSN_T0:
dda83cd7
SM
12681 reg_vd = reg_vd | (bit_d << 4);
12682 record_buf[0] = reg_vd + ARM_D0_REGNUM;
12683 record_buf[1] = reg_vd + ARM_D0_REGNUM + 1;
12684 arm_insn_r->reg_rec_count = 2;
12685 break;
851f26ae
OJ
12686
12687 case INSN_T1:
dda83cd7
SM
12688 reg_vd = reg_vd | (bit_d << 4);
12689 record_buf[0] = reg_vd + ARM_D0_REGNUM;
12690 arm_insn_r->reg_rec_count = 1;
12691 break;
851f26ae
OJ
12692
12693 case INSN_T2:
dda83cd7
SM
12694 reg_vd = (reg_vd << 1) | bit_d;
12695 record_buf[0] = reg_vd + ARM_D0_REGNUM;
12696 arm_insn_r->reg_rec_count = 1;
12697 break;
851f26ae
OJ
12698
12699 case INSN_T3:
dda83cd7
SM
12700 record_buf[0] = ARM_FPSCR_REGNUM;
12701 arm_insn_r->reg_rec_count = 1;
12702 break;
851f26ae
OJ
12703
12704 default:
dda83cd7
SM
12705 gdb_assert_not_reached ("no decoding pattern found");
12706 break;
851f26ae
OJ
12707 }
12708
12709 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12710 return 0;
12711}
12712
60cc5e93
OJ
12713/* Handling opcode 110 insns. */
12714
12715static int
4748a9be 12716arm_record_asimd_vfp_coproc (arm_insn_decode_record *arm_insn_r)
60cc5e93 12717{
bec2ab5a 12718 uint32_t op1, op1_ebit, coproc;
60cc5e93
OJ
12719
12720 coproc = bits (arm_insn_r->arm_insn, 8, 11);
12721 op1 = bits (arm_insn_r->arm_insn, 20, 25);
12722 op1_ebit = bit (arm_insn_r->arm_insn, 20);
12723
12724 if ((coproc & 0x0e) == 0x0a)
12725 {
12726 /* Handle extension register ld/st instructions. */
12727 if (!(op1 & 0x20))
dda83cd7 12728 return arm_record_exreg_ld_st_insn (arm_insn_r);
60cc5e93
OJ
12729
12730 /* 64-bit transfers between arm core and extension registers. */
12731 if ((op1 & 0x3e) == 0x04)
dda83cd7 12732 return arm_record_exreg_ld_st_insn (arm_insn_r);
60cc5e93
OJ
12733 }
12734 else
12735 {
12736 /* Handle coprocessor ld/st instructions. */
12737 if (!(op1 & 0x3a))
dda83cd7
SM
12738 {
12739 /* Store. */
12740 if (!op1_ebit)
12741 return arm_record_unsupported_insn (arm_insn_r);
12742 else
12743 /* Load. */
12744 return arm_record_unsupported_insn (arm_insn_r);
12745 }
60cc5e93
OJ
12746
12747 /* Move to coprocessor from two arm core registers. */
12748 if (op1 == 0x4)
dda83cd7 12749 return arm_record_unsupported_insn (arm_insn_r);
60cc5e93
OJ
12750
12751 /* Move to two arm core registers from coprocessor. */
12752 if (op1 == 0x5)
dda83cd7
SM
12753 {
12754 uint32_t reg_t[2];
60cc5e93 12755
dda83cd7
SM
12756 reg_t[0] = bits (arm_insn_r->arm_insn, 12, 15);
12757 reg_t[1] = bits (arm_insn_r->arm_insn, 16, 19);
12758 arm_insn_r->reg_rec_count = 2;
60cc5e93 12759
dda83cd7
SM
12760 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, reg_t);
12761 return 0;
60cc5e93
OJ
12762 }
12763 }
12764 return arm_record_unsupported_insn (arm_insn_r);
12765}
12766
72508ac0
PO
12767/* Handling opcode 111 insns. */
12768
12769static int
4748a9be 12770arm_record_coproc_data_proc (arm_insn_decode_record *arm_insn_r)
72508ac0 12771{
2d9e6acb 12772 uint32_t op, op1_ebit, coproc, bits_24_25;
345bd07c
SM
12773 arm_gdbarch_tdep *tdep
12774 = (arm_gdbarch_tdep *) gdbarch_tdep (arm_insn_r->gdbarch);
72508ac0 12775 struct regcache *reg_cache = arm_insn_r->regcache;
72508ac0
PO
12776
12777 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 24, 27);
60cc5e93 12778 coproc = bits (arm_insn_r->arm_insn, 8, 11);
60cc5e93
OJ
12779 op1_ebit = bit (arm_insn_r->arm_insn, 20);
12780 op = bit (arm_insn_r->arm_insn, 4);
2d9e6acb 12781 bits_24_25 = bits (arm_insn_r->arm_insn, 24, 25);
97dfe206
OJ
12782
12783 /* Handle arm SWI/SVC system call instructions. */
2d9e6acb 12784 if (bits_24_25 == 0x3)
97dfe206
OJ
12785 {
12786 if (tdep->arm_syscall_record != NULL)
dda83cd7
SM
12787 {
12788 ULONGEST svc_operand, svc_number;
97dfe206 12789
dda83cd7 12790 svc_operand = (0x00ffffff & arm_insn_r->arm_insn);
97dfe206 12791
dda83cd7
SM
12792 if (svc_operand) /* OABI. */
12793 svc_number = svc_operand - 0x900000;
12794 else /* EABI. */
12795 regcache_raw_read_unsigned (reg_cache, 7, &svc_number);
97dfe206 12796
dda83cd7
SM
12797 return tdep->arm_syscall_record (reg_cache, svc_number);
12798 }
97dfe206 12799 else
dda83cd7 12800 {
6cb06a8c 12801 gdb_printf (gdb_stderr, _("no syscall record support\n"));
dda83cd7
SM
12802 return -1;
12803 }
97dfe206 12804 }
2d9e6acb 12805 else if (bits_24_25 == 0x02)
60cc5e93 12806 {
2d9e6acb
YQ
12807 if (op)
12808 {
12809 if ((coproc & 0x0e) == 0x0a)
12810 {
12811 /* 8, 16, and 32-bit transfer */
12812 return arm_record_vdata_transfer_insn (arm_insn_r);
12813 }
12814 else
12815 {
12816 if (op1_ebit)
12817 {
12818 /* MRC, MRC2 */
12819 uint32_t record_buf[1];
12820
12821 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12822 if (record_buf[0] == 15)
12823 record_buf[0] = ARM_PS_REGNUM;
60cc5e93 12824
2d9e6acb
YQ
12825 arm_insn_r->reg_rec_count = 1;
12826 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count,
12827 record_buf);
12828 return 0;
12829 }
12830 else
12831 {
12832 /* MCR, MCR2 */
12833 return -1;
12834 }
12835 }
12836 }
12837 else
12838 {
12839 if ((coproc & 0x0e) == 0x0a)
12840 {
12841 /* VFP data-processing instructions. */
12842 return arm_record_vfp_data_proc_insn (arm_insn_r);
12843 }
12844 else
12845 {
12846 /* CDP, CDP2 */
12847 return -1;
12848 }
12849 }
60cc5e93 12850 }
97dfe206
OJ
12851 else
12852 {
2d9e6acb 12853 unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 25);
60cc5e93 12854
2d9e6acb
YQ
12855 if (op1 == 5)
12856 {
12857 if ((coproc & 0x0e) != 0x0a)
12858 {
12859 /* MRRC, MRRC2 */
12860 return -1;
12861 }
12862 }
12863 else if (op1 == 4 || op1 == 5)
12864 {
12865 if ((coproc & 0x0e) == 0x0a)
12866 {
12867 /* 64-bit transfers between ARM core and extension */
12868 return -1;
12869 }
12870 else if (op1 == 4)
12871 {
12872 /* MCRR, MCRR2 */
12873 return -1;
12874 }
12875 }
12876 else if (op1 == 0 || op1 == 1)
12877 {
12878 /* UNDEFINED */
12879 return -1;
12880 }
12881 else
12882 {
12883 if ((coproc & 0x0e) == 0x0a)
12884 {
12885 /* Extension register load/store */
12886 }
12887 else
12888 {
12889 /* STC, STC2, LDC, LDC2 */
12890 }
12891 return -1;
12892 }
97dfe206 12893 }
72508ac0 12894
2d9e6acb 12895 return -1;
72508ac0
PO
12896}
12897
12898/* Handling opcode 000 insns. */
12899
12900static int
4748a9be 12901thumb_record_shift_add_sub (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
12902{
12903 uint32_t record_buf[8];
12904 uint32_t reg_src1 = 0;
12905
12906 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12907
12908 record_buf[0] = ARM_PS_REGNUM;
12909 record_buf[1] = reg_src1;
12910 thumb_insn_r->reg_rec_count = 2;
12911
12912 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12913
12914 return 0;
12915}
12916
12917
12918/* Handling opcode 001 insns. */
12919
12920static int
4748a9be 12921thumb_record_add_sub_cmp_mov (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
12922{
12923 uint32_t record_buf[8];
12924 uint32_t reg_src1 = 0;
12925
12926 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12927
12928 record_buf[0] = ARM_PS_REGNUM;
12929 record_buf[1] = reg_src1;
12930 thumb_insn_r->reg_rec_count = 2;
12931
12932 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12933
12934 return 0;
12935}
12936
12937/* Handling opcode 010 insns. */
12938
12939static int
4748a9be 12940thumb_record_ld_st_reg_offset (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
12941{
12942 struct regcache *reg_cache = thumb_insn_r->regcache;
12943 uint32_t record_buf[8], record_buf_mem[8];
12944
12945 uint32_t reg_src1 = 0, reg_src2 = 0;
12946 uint32_t opcode1 = 0, opcode2 = 0, opcode3 = 0;
12947
12948 ULONGEST u_regval[2] = {0};
12949
12950 opcode1 = bits (thumb_insn_r->arm_insn, 10, 12);
12951
12952 if (bit (thumb_insn_r->arm_insn, 12))
12953 {
12954 /* Handle load/store register offset. */
b121eeb9
YQ
12955 uint32_t opB = bits (thumb_insn_r->arm_insn, 9, 11);
12956
b020ff80 12957 if (in_inclusive_range (opB, 4U, 7U))
dda83cd7
SM
12958 {
12959 /* LDR(2), LDRB(2) , LDRH(2), LDRSB, LDRSH. */
12960 reg_src1 = bits (thumb_insn_r->arm_insn,0, 2);
12961 record_buf[0] = reg_src1;
12962 thumb_insn_r->reg_rec_count = 1;
12963 }
b020ff80 12964 else if (in_inclusive_range (opB, 0U, 2U))
dda83cd7
SM
12965 {
12966 /* STR(2), STRB(2), STRH(2) . */
12967 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12968 reg_src2 = bits (thumb_insn_r->arm_insn, 6, 8);
12969 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
12970 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
12971 if (0 == opB)
12972 record_buf_mem[0] = 4; /* STR (2). */
12973 else if (2 == opB)
12974 record_buf_mem[0] = 1; /* STRB (2). */
12975 else if (1 == opB)
12976 record_buf_mem[0] = 2; /* STRH (2). */
12977 record_buf_mem[1] = u_regval[0] + u_regval[1];
12978 thumb_insn_r->mem_rec_count = 1;
12979 }
72508ac0
PO
12980 }
12981 else if (bit (thumb_insn_r->arm_insn, 11))
12982 {
12983 /* Handle load from literal pool. */
12984 /* LDR(3). */
12985 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12986 record_buf[0] = reg_src1;
12987 thumb_insn_r->reg_rec_count = 1;
12988 }
12989 else if (opcode1)
12990 {
b121eeb9 12991 /* Special data instructions and branch and exchange */
72508ac0
PO
12992 opcode2 = bits (thumb_insn_r->arm_insn, 8, 9);
12993 opcode3 = bits (thumb_insn_r->arm_insn, 0, 2);
12994 if ((3 == opcode2) && (!opcode3))
dda83cd7
SM
12995 {
12996 /* Branch with exchange. */
12997 record_buf[0] = ARM_PS_REGNUM;
12998 thumb_insn_r->reg_rec_count = 1;
12999 }
72508ac0 13000 else
dda83cd7 13001 {
1f33efec
YQ
13002 /* Format 8; special data processing insns. */
13003 record_buf[0] = ARM_PS_REGNUM;
13004 record_buf[1] = (bit (thumb_insn_r->arm_insn, 7) << 3
13005 | bits (thumb_insn_r->arm_insn, 0, 2));
dda83cd7
SM
13006 thumb_insn_r->reg_rec_count = 2;
13007 }
72508ac0
PO
13008 }
13009 else
13010 {
13011 /* Format 5; data processing insns. */
13012 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13013 if (bit (thumb_insn_r->arm_insn, 7))
dda83cd7
SM
13014 {
13015 reg_src1 = reg_src1 + 8;
13016 }
72508ac0
PO
13017 record_buf[0] = ARM_PS_REGNUM;
13018 record_buf[1] = reg_src1;
13019 thumb_insn_r->reg_rec_count = 2;
13020 }
13021
13022 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13023 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13024 record_buf_mem);
72508ac0
PO
13025
13026 return 0;
13027}
13028
13029/* Handling opcode 001 insns. */
13030
13031static int
4748a9be 13032thumb_record_ld_st_imm_offset (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13033{
13034 struct regcache *reg_cache = thumb_insn_r->regcache;
13035 uint32_t record_buf[8], record_buf_mem[8];
13036
13037 uint32_t reg_src1 = 0;
13038 uint32_t opcode = 0, immed_5 = 0;
13039
13040 ULONGEST u_regval = 0;
13041
13042 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
13043
13044 if (opcode)
13045 {
13046 /* LDR(1). */
13047 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13048 record_buf[0] = reg_src1;
13049 thumb_insn_r->reg_rec_count = 1;
13050 }
13051 else
13052 {
13053 /* STR(1). */
13054 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
13055 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
13056 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
13057 record_buf_mem[0] = 4;
13058 record_buf_mem[1] = u_regval + (immed_5 * 4);
13059 thumb_insn_r->mem_rec_count = 1;
13060 }
13061
13062 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13063 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13064 record_buf_mem);
72508ac0
PO
13065
13066 return 0;
13067}
13068
13069/* Handling opcode 100 insns. */
13070
13071static int
4748a9be 13072thumb_record_ld_st_stack (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13073{
13074 struct regcache *reg_cache = thumb_insn_r->regcache;
13075 uint32_t record_buf[8], record_buf_mem[8];
13076
13077 uint32_t reg_src1 = 0;
13078 uint32_t opcode = 0, immed_8 = 0, immed_5 = 0;
13079
13080 ULONGEST u_regval = 0;
13081
13082 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
13083
13084 if (3 == opcode)
13085 {
13086 /* LDR(4). */
13087 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13088 record_buf[0] = reg_src1;
13089 thumb_insn_r->reg_rec_count = 1;
13090 }
13091 else if (1 == opcode)
13092 {
13093 /* LDRH(1). */
13094 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13095 record_buf[0] = reg_src1;
13096 thumb_insn_r->reg_rec_count = 1;
13097 }
13098 else if (2 == opcode)
13099 {
13100 /* STR(3). */
13101 immed_8 = bits (thumb_insn_r->arm_insn, 0, 7);
13102 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
13103 record_buf_mem[0] = 4;
13104 record_buf_mem[1] = u_regval + (immed_8 * 4);
13105 thumb_insn_r->mem_rec_count = 1;
13106 }
13107 else if (0 == opcode)
13108 {
13109 /* STRH(1). */
13110 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
13111 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
13112 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
13113 record_buf_mem[0] = 2;
13114 record_buf_mem[1] = u_regval + (immed_5 * 2);
13115 thumb_insn_r->mem_rec_count = 1;
13116 }
13117
13118 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13119 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13120 record_buf_mem);
72508ac0
PO
13121
13122 return 0;
13123}
13124
13125/* Handling opcode 101 insns. */
13126
13127static int
4748a9be 13128thumb_record_misc (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13129{
13130 struct regcache *reg_cache = thumb_insn_r->regcache;
13131
b121eeb9 13132 uint32_t opcode = 0;
72508ac0 13133 uint32_t register_bits = 0, register_count = 0;
bec2ab5a 13134 uint32_t index = 0, start_address = 0;
72508ac0
PO
13135 uint32_t record_buf[24], record_buf_mem[48];
13136 uint32_t reg_src1;
13137
13138 ULONGEST u_regval = 0;
13139
13140 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
72508ac0 13141
b121eeb9 13142 if (opcode == 0 || opcode == 1)
72508ac0 13143 {
b121eeb9
YQ
13144 /* ADR and ADD (SP plus immediate) */
13145
72508ac0
PO
13146 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13147 record_buf[0] = reg_src1;
13148 thumb_insn_r->reg_rec_count = 1;
13149 }
b121eeb9 13150 else
72508ac0 13151 {
b121eeb9
YQ
13152 /* Miscellaneous 16-bit instructions */
13153 uint32_t opcode2 = bits (thumb_insn_r->arm_insn, 8, 11);
13154
13155 switch (opcode2)
13156 {
13157 case 6:
13158 /* SETEND and CPS */
13159 break;
13160 case 0:
13161 /* ADD/SUB (SP plus immediate) */
13162 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13163 record_buf[0] = ARM_SP_REGNUM;
13164 thumb_insn_r->reg_rec_count = 1;
13165 break;
13166 case 1: /* fall through */
13167 case 3: /* fall through */
13168 case 9: /* fall through */
13169 case 11:
13170 /* CBNZ, CBZ */
b121eeb9
YQ
13171 break;
13172 case 2:
13173 /* SXTH, SXTB, UXTH, UXTB */
13174 record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
13175 thumb_insn_r->reg_rec_count = 1;
13176 break;
13177 case 4: /* fall through */
13178 case 5:
13179 /* PUSH. */
13180 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13181 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
13182 while (register_bits)
13183 {
13184 if (register_bits & 0x00000001)
13185 register_count++;
13186 register_bits = register_bits >> 1;
13187 }
13188 start_address = u_regval - \
13189 (4 * (bit (thumb_insn_r->arm_insn, 8) + register_count));
13190 thumb_insn_r->mem_rec_count = register_count;
13191 while (register_count)
13192 {
13193 record_buf_mem[(register_count * 2) - 1] = start_address;
13194 record_buf_mem[(register_count * 2) - 2] = 4;
13195 start_address = start_address + 4;
13196 register_count--;
13197 }
13198 record_buf[0] = ARM_SP_REGNUM;
13199 thumb_insn_r->reg_rec_count = 1;
13200 break;
13201 case 10:
13202 /* REV, REV16, REVSH */
ba14f379
YQ
13203 record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
13204 thumb_insn_r->reg_rec_count = 1;
b121eeb9
YQ
13205 break;
13206 case 12: /* fall through */
13207 case 13:
13208 /* POP. */
13209 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13210 while (register_bits)
13211 {
13212 if (register_bits & 0x00000001)
13213 record_buf[index++] = register_count;
13214 register_bits = register_bits >> 1;
13215 register_count++;
13216 }
13217 record_buf[index++] = ARM_PS_REGNUM;
13218 record_buf[index++] = ARM_SP_REGNUM;
13219 thumb_insn_r->reg_rec_count = index;
13220 break;
13221 case 0xe:
13222 /* BKPT insn. */
13223 /* Handle enhanced software breakpoint insn, BKPT. */
13224 /* CPSR is changed to be executed in ARM state, disabling normal
13225 interrupts, entering abort mode. */
13226 /* According to high vector configuration PC is set. */
13227 /* User hits breakpoint and type reverse, in that case, we need to go back with
13228 previous CPSR and Program Counter. */
13229 record_buf[0] = ARM_PS_REGNUM;
13230 record_buf[1] = ARM_LR_REGNUM;
13231 thumb_insn_r->reg_rec_count = 2;
13232 /* We need to save SPSR value, which is not yet done. */
6cb06a8c
TT
13233 gdb_printf (gdb_stderr,
13234 _("Process record does not support instruction "
13235 "0x%0x at address %s.\n"),
13236 thumb_insn_r->arm_insn,
13237 paddress (thumb_insn_r->gdbarch,
13238 thumb_insn_r->this_addr));
b121eeb9
YQ
13239 return -1;
13240
13241 case 0xf:
13242 /* If-Then, and hints */
13243 break;
13244 default:
13245 return -1;
13246 };
72508ac0
PO
13247 }
13248
13249 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13250 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13251 record_buf_mem);
72508ac0
PO
13252
13253 return 0;
13254}
13255
13256/* Handling opcode 110 insns. */
13257
13258static int
4748a9be 13259thumb_record_ldm_stm_swi (arm_insn_decode_record *thumb_insn_r)
72508ac0 13260{
345bd07c
SM
13261 arm_gdbarch_tdep *tdep
13262 = (arm_gdbarch_tdep *) gdbarch_tdep (thumb_insn_r->gdbarch);
72508ac0
PO
13263 struct regcache *reg_cache = thumb_insn_r->regcache;
13264
13265 uint32_t ret = 0; /* function return value: -1:record failure ; 0:success */
13266 uint32_t reg_src1 = 0;
13267 uint32_t opcode1 = 0, opcode2 = 0, register_bits = 0, register_count = 0;
bec2ab5a 13268 uint32_t index = 0, start_address = 0;
72508ac0
PO
13269 uint32_t record_buf[24], record_buf_mem[48];
13270
13271 ULONGEST u_regval = 0;
13272
13273 opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
13274 opcode2 = bits (thumb_insn_r->arm_insn, 11, 12);
13275
13276 if (1 == opcode2)
13277 {
13278
13279 /* LDMIA. */
13280 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13281 /* Get Rn. */
13282 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13283 while (register_bits)
dda83cd7
SM
13284 {
13285 if (register_bits & 0x00000001)
13286 record_buf[index++] = register_count;
13287 register_bits = register_bits >> 1;
13288 register_count++;
13289 }
f969241e
OJ
13290 record_buf[index++] = reg_src1;
13291 thumb_insn_r->reg_rec_count = index;
72508ac0
PO
13292 }
13293 else if (0 == opcode2)
13294 {
13295 /* It handles both STMIA. */
13296 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13297 /* Get Rn. */
13298 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13299 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
13300 while (register_bits)
dda83cd7
SM
13301 {
13302 if (register_bits & 0x00000001)
13303 register_count++;
13304 register_bits = register_bits >> 1;
13305 }
72508ac0
PO
13306 start_address = u_regval;
13307 thumb_insn_r->mem_rec_count = register_count;
13308 while (register_count)
dda83cd7
SM
13309 {
13310 record_buf_mem[(register_count * 2) - 1] = start_address;
13311 record_buf_mem[(register_count * 2) - 2] = 4;
13312 start_address = start_address + 4;
13313 register_count--;
13314 }
72508ac0
PO
13315 }
13316 else if (0x1F == opcode1)
13317 {
dda83cd7
SM
13318 /* Handle arm syscall insn. */
13319 if (tdep->arm_syscall_record != NULL)
13320 {
13321 regcache_raw_read_unsigned (reg_cache, 7, &u_regval);
13322 ret = tdep->arm_syscall_record (reg_cache, u_regval);
13323 }
13324 else
13325 {
6cb06a8c 13326 gdb_printf (gdb_stderr, _("no syscall record support\n"));
dda83cd7
SM
13327 return -1;
13328 }
72508ac0
PO
13329 }
13330
13331 /* B (1), conditional branch is automatically taken care in process_record,
13332 as PC is saved there. */
13333
13334 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13335 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13336 record_buf_mem);
72508ac0
PO
13337
13338 return ret;
13339}
13340
13341/* Handling opcode 111 insns. */
13342
13343static int
4748a9be 13344thumb_record_branch (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13345{
13346 uint32_t record_buf[8];
13347 uint32_t bits_h = 0;
13348
13349 bits_h = bits (thumb_insn_r->arm_insn, 11, 12);
13350
13351 if (2 == bits_h || 3 == bits_h)
13352 {
13353 /* BL */
13354 record_buf[0] = ARM_LR_REGNUM;
13355 thumb_insn_r->reg_rec_count = 1;
13356 }
13357 else if (1 == bits_h)
13358 {
13359 /* BLX(1). */
13360 record_buf[0] = ARM_PS_REGNUM;
13361 record_buf[1] = ARM_LR_REGNUM;
13362 thumb_insn_r->reg_rec_count = 2;
13363 }
13364
13365 /* B(2) is automatically taken care in process_record, as PC is
13366 saved there. */
13367
13368 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13369
13370 return 0;
13371}
13372
c6ec2b30
OJ
13373/* Handler for thumb2 load/store multiple instructions. */
13374
13375static int
4748a9be 13376thumb2_record_ld_st_multiple (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13377{
13378 struct regcache *reg_cache = thumb2_insn_r->regcache;
13379
13380 uint32_t reg_rn, op;
13381 uint32_t register_bits = 0, register_count = 0;
13382 uint32_t index = 0, start_address = 0;
13383 uint32_t record_buf[24], record_buf_mem[48];
13384
13385 ULONGEST u_regval = 0;
13386
13387 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13388 op = bits (thumb2_insn_r->arm_insn, 23, 24);
13389
13390 if (0 == op || 3 == op)
13391 {
13392 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
dda83cd7
SM
13393 {
13394 /* Handle RFE instruction. */
13395 record_buf[0] = ARM_PS_REGNUM;
13396 thumb2_insn_r->reg_rec_count = 1;
13397 }
c6ec2b30 13398 else
dda83cd7
SM
13399 {
13400 /* Handle SRS instruction after reading banked SP. */
13401 return arm_record_unsupported_insn (thumb2_insn_r);
13402 }
c6ec2b30
OJ
13403 }
13404 else if (1 == op || 2 == op)
13405 {
13406 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
dda83cd7
SM
13407 {
13408 /* Handle LDM/LDMIA/LDMFD and LDMDB/LDMEA instructions. */
13409 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
13410 while (register_bits)
13411 {
13412 if (register_bits & 0x00000001)
13413 record_buf[index++] = register_count;
13414
13415 register_count++;
13416 register_bits = register_bits >> 1;
13417 }
13418 record_buf[index++] = reg_rn;
13419 record_buf[index++] = ARM_PS_REGNUM;
13420 thumb2_insn_r->reg_rec_count = index;
13421 }
c6ec2b30 13422 else
dda83cd7
SM
13423 {
13424 /* Handle STM/STMIA/STMEA and STMDB/STMFD. */
13425 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
13426 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
13427 while (register_bits)
13428 {
13429 if (register_bits & 0x00000001)
13430 register_count++;
13431
13432 register_bits = register_bits >> 1;
13433 }
13434
13435 if (1 == op)
13436 {
13437 /* Start address calculation for LDMDB/LDMEA. */
13438 start_address = u_regval;
13439 }
13440 else if (2 == op)
13441 {
13442 /* Start address calculation for LDMDB/LDMEA. */
13443 start_address = u_regval - register_count * 4;
13444 }
13445
13446 thumb2_insn_r->mem_rec_count = register_count;
13447 while (register_count)
13448 {
13449 record_buf_mem[register_count * 2 - 1] = start_address;
13450 record_buf_mem[register_count * 2 - 2] = 4;
13451 start_address = start_address + 4;
13452 register_count--;
13453 }
13454 record_buf[0] = reg_rn;
13455 record_buf[1] = ARM_PS_REGNUM;
13456 thumb2_insn_r->reg_rec_count = 2;
13457 }
c6ec2b30
OJ
13458 }
13459
13460 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
dda83cd7 13461 record_buf_mem);
c6ec2b30 13462 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13463 record_buf);
c6ec2b30
OJ
13464 return ARM_RECORD_SUCCESS;
13465}
13466
13467/* Handler for thumb2 load/store (dual/exclusive) and table branch
13468 instructions. */
13469
13470static int
4748a9be 13471thumb2_record_ld_st_dual_ex_tbb (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13472{
13473 struct regcache *reg_cache = thumb2_insn_r->regcache;
13474
13475 uint32_t reg_rd, reg_rn, offset_imm;
13476 uint32_t reg_dest1, reg_dest2;
13477 uint32_t address, offset_addr;
13478 uint32_t record_buf[8], record_buf_mem[8];
13479 uint32_t op1, op2, op3;
c6ec2b30
OJ
13480
13481 ULONGEST u_regval[2];
13482
13483 op1 = bits (thumb2_insn_r->arm_insn, 23, 24);
13484 op2 = bits (thumb2_insn_r->arm_insn, 20, 21);
13485 op3 = bits (thumb2_insn_r->arm_insn, 4, 7);
13486
13487 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
13488 {
13489 if(!(1 == op1 && 1 == op2 && (0 == op3 || 1 == op3)))
dda83cd7
SM
13490 {
13491 reg_dest1 = bits (thumb2_insn_r->arm_insn, 12, 15);
13492 record_buf[0] = reg_dest1;
13493 record_buf[1] = ARM_PS_REGNUM;
13494 thumb2_insn_r->reg_rec_count = 2;
13495 }
c6ec2b30
OJ
13496
13497 if (3 == op2 || (op1 & 2) || (1 == op1 && 1 == op2 && 7 == op3))
dda83cd7
SM
13498 {
13499 reg_dest2 = bits (thumb2_insn_r->arm_insn, 8, 11);
13500 record_buf[2] = reg_dest2;
13501 thumb2_insn_r->reg_rec_count = 3;
13502 }
c6ec2b30
OJ
13503 }
13504 else
13505 {
13506 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13507 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
13508
13509 if (0 == op1 && 0 == op2)
dda83cd7
SM
13510 {
13511 /* Handle STREX. */
13512 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13513 address = u_regval[0] + (offset_imm * 4);
13514 record_buf_mem[0] = 4;
13515 record_buf_mem[1] = address;
13516 thumb2_insn_r->mem_rec_count = 1;
13517 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
13518 record_buf[0] = reg_rd;
13519 thumb2_insn_r->reg_rec_count = 1;
13520 }
c6ec2b30 13521 else if (1 == op1 && 0 == op2)
dda83cd7
SM
13522 {
13523 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
13524 record_buf[0] = reg_rd;
13525 thumb2_insn_r->reg_rec_count = 1;
13526 address = u_regval[0];
13527 record_buf_mem[1] = address;
13528
13529 if (4 == op3)
13530 {
13531 /* Handle STREXB. */
13532 record_buf_mem[0] = 1;
13533 thumb2_insn_r->mem_rec_count = 1;
13534 }
13535 else if (5 == op3)
13536 {
13537 /* Handle STREXH. */
13538 record_buf_mem[0] = 2 ;
13539 thumb2_insn_r->mem_rec_count = 1;
13540 }
13541 else if (7 == op3)
13542 {
13543 /* Handle STREXD. */
13544 address = u_regval[0];
13545 record_buf_mem[0] = 4;
13546 record_buf_mem[2] = 4;
13547 record_buf_mem[3] = address + 4;
13548 thumb2_insn_r->mem_rec_count = 2;
13549 }
13550 }
c6ec2b30 13551 else
dda83cd7
SM
13552 {
13553 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13554
13555 if (bit (thumb2_insn_r->arm_insn, 24))
13556 {
13557 if (bit (thumb2_insn_r->arm_insn, 23))
13558 offset_addr = u_regval[0] + (offset_imm * 4);
13559 else
13560 offset_addr = u_regval[0] - (offset_imm * 4);
13561
13562 address = offset_addr;
13563 }
13564 else
13565 address = u_regval[0];
13566
13567 record_buf_mem[0] = 4;
13568 record_buf_mem[1] = address;
13569 record_buf_mem[2] = 4;
13570 record_buf_mem[3] = address + 4;
13571 thumb2_insn_r->mem_rec_count = 2;
13572 record_buf[0] = reg_rn;
13573 thumb2_insn_r->reg_rec_count = 1;
13574 }
c6ec2b30
OJ
13575 }
13576
13577 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13578 record_buf);
c6ec2b30 13579 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
dda83cd7 13580 record_buf_mem);
c6ec2b30
OJ
13581 return ARM_RECORD_SUCCESS;
13582}
13583
13584/* Handler for thumb2 data processing (shift register and modified immediate)
13585 instructions. */
13586
13587static int
4748a9be 13588thumb2_record_data_proc_sreg_mimm (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13589{
13590 uint32_t reg_rd, op;
13591 uint32_t record_buf[8];
13592
13593 op = bits (thumb2_insn_r->arm_insn, 21, 24);
13594 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
13595
13596 if ((0 == op || 4 == op || 8 == op || 13 == op) && 15 == reg_rd)
13597 {
13598 record_buf[0] = ARM_PS_REGNUM;
13599 thumb2_insn_r->reg_rec_count = 1;
13600 }
13601 else
13602 {
13603 record_buf[0] = reg_rd;
13604 record_buf[1] = ARM_PS_REGNUM;
13605 thumb2_insn_r->reg_rec_count = 2;
13606 }
13607
13608 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13609 record_buf);
c6ec2b30
OJ
13610 return ARM_RECORD_SUCCESS;
13611}
13612
13613/* Generic handler for thumb2 instructions which effect destination and PS
13614 registers. */
13615
13616static int
4748a9be 13617thumb2_record_ps_dest_generic (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13618{
13619 uint32_t reg_rd;
13620 uint32_t record_buf[8];
13621
13622 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
13623
13624 record_buf[0] = reg_rd;
13625 record_buf[1] = ARM_PS_REGNUM;
13626 thumb2_insn_r->reg_rec_count = 2;
13627
13628 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13629 record_buf);
c6ec2b30
OJ
13630 return ARM_RECORD_SUCCESS;
13631}
13632
13633/* Handler for thumb2 branch and miscellaneous control instructions. */
13634
13635static int
4748a9be 13636thumb2_record_branch_misc_cntrl (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13637{
13638 uint32_t op, op1, op2;
13639 uint32_t record_buf[8];
13640
13641 op = bits (thumb2_insn_r->arm_insn, 20, 26);
13642 op1 = bits (thumb2_insn_r->arm_insn, 12, 14);
13643 op2 = bits (thumb2_insn_r->arm_insn, 8, 11);
13644
13645 /* Handle MSR insn. */
13646 if (!(op1 & 0x2) && 0x38 == op)
13647 {
13648 if (!(op2 & 0x3))
dda83cd7
SM
13649 {
13650 /* CPSR is going to be changed. */
13651 record_buf[0] = ARM_PS_REGNUM;
13652 thumb2_insn_r->reg_rec_count = 1;
13653 }
c6ec2b30 13654 else
dda83cd7
SM
13655 {
13656 arm_record_unsupported_insn(thumb2_insn_r);
13657 return -1;
13658 }
c6ec2b30
OJ
13659 }
13660 else if (4 == (op1 & 0x5) || 5 == (op1 & 0x5))
13661 {
13662 /* BLX. */
13663 record_buf[0] = ARM_PS_REGNUM;
13664 record_buf[1] = ARM_LR_REGNUM;
13665 thumb2_insn_r->reg_rec_count = 2;
13666 }
13667
13668 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13669 record_buf);
c6ec2b30
OJ
13670 return ARM_RECORD_SUCCESS;
13671}
13672
13673/* Handler for thumb2 store single data item instructions. */
13674
13675static int
4748a9be 13676thumb2_record_str_single_data (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13677{
13678 struct regcache *reg_cache = thumb2_insn_r->regcache;
13679
13680 uint32_t reg_rn, reg_rm, offset_imm, shift_imm;
13681 uint32_t address, offset_addr;
13682 uint32_t record_buf[8], record_buf_mem[8];
13683 uint32_t op1, op2;
13684
13685 ULONGEST u_regval[2];
13686
13687 op1 = bits (thumb2_insn_r->arm_insn, 21, 23);
13688 op2 = bits (thumb2_insn_r->arm_insn, 6, 11);
13689 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13690 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
13691
13692 if (bit (thumb2_insn_r->arm_insn, 23))
13693 {
13694 /* T2 encoding. */
13695 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 11);
13696 offset_addr = u_regval[0] + offset_imm;
13697 address = offset_addr;
13698 }
13699 else
13700 {
13701 /* T3 encoding. */
13702 if ((0 == op1 || 1 == op1 || 2 == op1) && !(op2 & 0x20))
dda83cd7
SM
13703 {
13704 /* Handle STRB (register). */
13705 reg_rm = bits (thumb2_insn_r->arm_insn, 0, 3);
13706 regcache_raw_read_unsigned (reg_cache, reg_rm, &u_regval[1]);
13707 shift_imm = bits (thumb2_insn_r->arm_insn, 4, 5);
13708 offset_addr = u_regval[1] << shift_imm;
13709 address = u_regval[0] + offset_addr;
13710 }
c6ec2b30 13711 else
dda83cd7
SM
13712 {
13713 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13714 if (bit (thumb2_insn_r->arm_insn, 10))
13715 {
13716 if (bit (thumb2_insn_r->arm_insn, 9))
13717 offset_addr = u_regval[0] + offset_imm;
13718 else
13719 offset_addr = u_regval[0] - offset_imm;
13720
13721 address = offset_addr;
13722 }
13723 else
13724 address = u_regval[0];
13725 }
c6ec2b30
OJ
13726 }
13727
13728 switch (op1)
13729 {
13730 /* Store byte instructions. */
13731 case 4:
13732 case 0:
dda83cd7
SM
13733 record_buf_mem[0] = 1;
13734 break;
c6ec2b30
OJ
13735 /* Store half word instructions. */
13736 case 1:
13737 case 5:
dda83cd7
SM
13738 record_buf_mem[0] = 2;
13739 break;
c6ec2b30
OJ
13740 /* Store word instructions. */
13741 case 2:
13742 case 6:
dda83cd7
SM
13743 record_buf_mem[0] = 4;
13744 break;
c6ec2b30
OJ
13745
13746 default:
dda83cd7
SM
13747 gdb_assert_not_reached ("no decoding pattern found");
13748 break;
c6ec2b30
OJ
13749 }
13750
13751 record_buf_mem[1] = address;
13752 thumb2_insn_r->mem_rec_count = 1;
13753 record_buf[0] = reg_rn;
13754 thumb2_insn_r->reg_rec_count = 1;
13755
13756 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13757 record_buf);
c6ec2b30 13758 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
dda83cd7 13759 record_buf_mem);
c6ec2b30
OJ
13760 return ARM_RECORD_SUCCESS;
13761}
13762
13763/* Handler for thumb2 load memory hints instructions. */
13764
13765static int
4748a9be 13766thumb2_record_ld_mem_hints (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13767{
13768 uint32_t record_buf[8];
13769 uint32_t reg_rt, reg_rn;
13770
13771 reg_rt = bits (thumb2_insn_r->arm_insn, 12, 15);
13772 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13773
13774 if (ARM_PC_REGNUM != reg_rt)
13775 {
13776 record_buf[0] = reg_rt;
13777 record_buf[1] = reg_rn;
13778 record_buf[2] = ARM_PS_REGNUM;
13779 thumb2_insn_r->reg_rec_count = 3;
13780
13781 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13782 record_buf);
c6ec2b30
OJ
13783 return ARM_RECORD_SUCCESS;
13784 }
13785
13786 return ARM_RECORD_FAILURE;
13787}
13788
13789/* Handler for thumb2 load word instructions. */
13790
13791static int
4748a9be 13792thumb2_record_ld_word (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30 13793{
c6ec2b30
OJ
13794 uint32_t record_buf[8];
13795
13796 record_buf[0] = bits (thumb2_insn_r->arm_insn, 12, 15);
13797 record_buf[1] = ARM_PS_REGNUM;
13798 thumb2_insn_r->reg_rec_count = 2;
13799
13800 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13801 record_buf);
c6ec2b30
OJ
13802 return ARM_RECORD_SUCCESS;
13803}
13804
13805/* Handler for thumb2 long multiply, long multiply accumulate, and
13806 divide instructions. */
13807
13808static int
4748a9be 13809thumb2_record_lmul_lmla_div (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13810{
13811 uint32_t opcode1 = 0, opcode2 = 0;
13812 uint32_t record_buf[8];
c6ec2b30
OJ
13813
13814 opcode1 = bits (thumb2_insn_r->arm_insn, 20, 22);
13815 opcode2 = bits (thumb2_insn_r->arm_insn, 4, 7);
13816
13817 if (0 == opcode1 || 2 == opcode1 || (opcode1 >= 4 && opcode1 <= 6))
13818 {
13819 /* Handle SMULL, UMULL, SMULAL. */
13820 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
13821 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
13822 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
13823 record_buf[2] = ARM_PS_REGNUM;
13824 thumb2_insn_r->reg_rec_count = 3;
13825 }
13826 else if (1 == opcode1 || 3 == opcode2)
13827 {
13828 /* Handle SDIV and UDIV. */
13829 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
13830 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
13831 record_buf[2] = ARM_PS_REGNUM;
13832 thumb2_insn_r->reg_rec_count = 3;
13833 }
13834 else
13835 return ARM_RECORD_FAILURE;
13836
13837 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13838 record_buf);
c6ec2b30
OJ
13839 return ARM_RECORD_SUCCESS;
13840}
13841
60cc5e93
OJ
13842/* Record handler for thumb32 coprocessor instructions. */
13843
13844static int
4748a9be 13845thumb2_record_coproc_insn (arm_insn_decode_record *thumb2_insn_r)
60cc5e93
OJ
13846{
13847 if (bit (thumb2_insn_r->arm_insn, 25))
13848 return arm_record_coproc_data_proc (thumb2_insn_r);
13849 else
13850 return arm_record_asimd_vfp_coproc (thumb2_insn_r);
13851}
13852
1e1b6563
OJ
13853/* Record handler for advance SIMD structure load/store instructions. */
13854
13855static int
4748a9be 13856thumb2_record_asimd_struct_ld_st (arm_insn_decode_record *thumb2_insn_r)
1e1b6563
OJ
13857{
13858 struct regcache *reg_cache = thumb2_insn_r->regcache;
13859 uint32_t l_bit, a_bit, b_bits;
13860 uint32_t record_buf[128], record_buf_mem[128];
bec2ab5a 13861 uint32_t reg_rn, reg_vd, address, f_elem;
1e1b6563
OJ
13862 uint32_t index_r = 0, index_e = 0, bf_regs = 0, index_m = 0, loop_t = 0;
13863 uint8_t f_ebytes;
13864
13865 l_bit = bit (thumb2_insn_r->arm_insn, 21);
13866 a_bit = bit (thumb2_insn_r->arm_insn, 23);
13867 b_bits = bits (thumb2_insn_r->arm_insn, 8, 11);
13868 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13869 reg_vd = bits (thumb2_insn_r->arm_insn, 12, 15);
13870 reg_vd = (bit (thumb2_insn_r->arm_insn, 22) << 4) | reg_vd;
13871 f_ebytes = (1 << bits (thumb2_insn_r->arm_insn, 6, 7));
1e1b6563
OJ
13872 f_elem = 8 / f_ebytes;
13873
13874 if (!l_bit)
13875 {
13876 ULONGEST u_regval = 0;
13877 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
13878 address = u_regval;
13879
13880 if (!a_bit)
dda83cd7
SM
13881 {
13882 /* Handle VST1. */
13883 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
13884 {
13885 if (b_bits == 0x07)
13886 bf_regs = 1;
13887 else if (b_bits == 0x0a)
13888 bf_regs = 2;
13889 else if (b_bits == 0x06)
13890 bf_regs = 3;
13891 else if (b_bits == 0x02)
13892 bf_regs = 4;
13893 else
13894 bf_regs = 0;
13895
13896 for (index_r = 0; index_r < bf_regs; index_r++)
13897 {
13898 for (index_e = 0; index_e < f_elem; index_e++)
13899 {
13900 record_buf_mem[index_m++] = f_ebytes;
13901 record_buf_mem[index_m++] = address;
13902 address = address + f_ebytes;
13903 thumb2_insn_r->mem_rec_count += 1;
13904 }
13905 }
13906 }
13907 /* Handle VST2. */
13908 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
13909 {
13910 if (b_bits == 0x09 || b_bits == 0x08)
13911 bf_regs = 1;
13912 else if (b_bits == 0x03)
13913 bf_regs = 2;
13914 else
13915 bf_regs = 0;
13916
13917 for (index_r = 0; index_r < bf_regs; index_r++)
13918 for (index_e = 0; index_e < f_elem; index_e++)
13919 {
13920 for (loop_t = 0; loop_t < 2; loop_t++)
13921 {
13922 record_buf_mem[index_m++] = f_ebytes;
13923 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
13924 thumb2_insn_r->mem_rec_count += 1;
13925 }
13926 address = address + (2 * f_ebytes);
13927 }
13928 }
13929 /* Handle VST3. */
13930 else if ((b_bits & 0x0e) == 0x04)
13931 {
13932 for (index_e = 0; index_e < f_elem; index_e++)
13933 {
13934 for (loop_t = 0; loop_t < 3; loop_t++)
13935 {
13936 record_buf_mem[index_m++] = f_ebytes;
13937 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
13938 thumb2_insn_r->mem_rec_count += 1;
13939 }
13940 address = address + (3 * f_ebytes);
13941 }
13942 }
13943 /* Handle VST4. */
13944 else if (!(b_bits & 0x0e))
13945 {
13946 for (index_e = 0; index_e < f_elem; index_e++)
13947 {
13948 for (loop_t = 0; loop_t < 4; loop_t++)
13949 {
13950 record_buf_mem[index_m++] = f_ebytes;
13951 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
13952 thumb2_insn_r->mem_rec_count += 1;
13953 }
13954 address = address + (4 * f_ebytes);
13955 }
13956 }
13957 }
1e1b6563 13958 else
dda83cd7
SM
13959 {
13960 uint8_t bft_size = bits (thumb2_insn_r->arm_insn, 10, 11);
13961
13962 if (bft_size == 0x00)
13963 f_ebytes = 1;
13964 else if (bft_size == 0x01)
13965 f_ebytes = 2;
13966 else if (bft_size == 0x02)
13967 f_ebytes = 4;
13968 else
13969 f_ebytes = 0;
13970
13971 /* Handle VST1. */
13972 if (!(b_bits & 0x0b) || b_bits == 0x08)
13973 thumb2_insn_r->mem_rec_count = 1;
13974 /* Handle VST2. */
13975 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09)
13976 thumb2_insn_r->mem_rec_count = 2;
13977 /* Handle VST3. */
13978 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a)
13979 thumb2_insn_r->mem_rec_count = 3;
13980 /* Handle VST4. */
13981 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b)
13982 thumb2_insn_r->mem_rec_count = 4;
13983
13984 for (index_m = 0; index_m < thumb2_insn_r->mem_rec_count; index_m++)
13985 {
13986 record_buf_mem[index_m] = f_ebytes;
13987 record_buf_mem[index_m] = address + (index_m * f_ebytes);
13988 }
13989 }
1e1b6563
OJ
13990 }
13991 else
13992 {
13993 if (!a_bit)
dda83cd7
SM
13994 {
13995 /* Handle VLD1. */
13996 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
13997 thumb2_insn_r->reg_rec_count = 1;
13998 /* Handle VLD2. */
13999 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
14000 thumb2_insn_r->reg_rec_count = 2;
14001 /* Handle VLD3. */
14002 else if ((b_bits & 0x0e) == 0x04)
14003 thumb2_insn_r->reg_rec_count = 3;
14004 /* Handle VLD4. */
14005 else if (!(b_bits & 0x0e))
14006 thumb2_insn_r->reg_rec_count = 4;
14007 }
1e1b6563 14008 else
dda83cd7
SM
14009 {
14010 /* Handle VLD1. */
14011 if (!(b_bits & 0x0b) || b_bits == 0x08 || b_bits == 0x0c)
14012 thumb2_insn_r->reg_rec_count = 1;
14013 /* Handle VLD2. */
14014 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09 || b_bits == 0x0d)
14015 thumb2_insn_r->reg_rec_count = 2;
14016 /* Handle VLD3. */
14017 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a || b_bits == 0x0e)
14018 thumb2_insn_r->reg_rec_count = 3;
14019 /* Handle VLD4. */
14020 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b || b_bits == 0x0f)
14021 thumb2_insn_r->reg_rec_count = 4;
14022
14023 for (index_r = 0; index_r < thumb2_insn_r->reg_rec_count; index_r++)
14024 record_buf[index_r] = reg_vd + ARM_D0_REGNUM + index_r;
14025 }
1e1b6563
OJ
14026 }
14027
14028 if (bits (thumb2_insn_r->arm_insn, 0, 3) != 15)
14029 {
14030 record_buf[index_r] = reg_rn;
14031 thumb2_insn_r->reg_rec_count += 1;
14032 }
14033
14034 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 14035 record_buf);
1e1b6563 14036 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
dda83cd7 14037 record_buf_mem);
1e1b6563
OJ
14038 return 0;
14039}
14040
c6ec2b30
OJ
14041/* Decodes thumb2 instruction type and invokes its record handler. */
14042
14043static unsigned int
4748a9be 14044thumb2_record_decode_insn_handler (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
14045{
14046 uint32_t op, op1, op2;
14047
14048 op = bit (thumb2_insn_r->arm_insn, 15);
14049 op1 = bits (thumb2_insn_r->arm_insn, 27, 28);
14050 op2 = bits (thumb2_insn_r->arm_insn, 20, 26);
14051
14052 if (op1 == 0x01)
14053 {
14054 if (!(op2 & 0x64 ))
dda83cd7
SM
14055 {
14056 /* Load/store multiple instruction. */
14057 return thumb2_record_ld_st_multiple (thumb2_insn_r);
14058 }
b121eeb9 14059 else if ((op2 & 0x64) == 0x4)
dda83cd7
SM
14060 {
14061 /* Load/store (dual/exclusive) and table branch instruction. */
14062 return thumb2_record_ld_st_dual_ex_tbb (thumb2_insn_r);
14063 }
b121eeb9 14064 else if ((op2 & 0x60) == 0x20)
dda83cd7
SM
14065 {
14066 /* Data-processing (shifted register). */
14067 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
14068 }
c6ec2b30 14069 else if (op2 & 0x40)
dda83cd7
SM
14070 {
14071 /* Co-processor instructions. */
14072 return thumb2_record_coproc_insn (thumb2_insn_r);
14073 }
c6ec2b30
OJ
14074 }
14075 else if (op1 == 0x02)
14076 {
14077 if (op)
dda83cd7
SM
14078 {
14079 /* Branches and miscellaneous control instructions. */
14080 return thumb2_record_branch_misc_cntrl (thumb2_insn_r);
14081 }
c6ec2b30 14082 else if (op2 & 0x20)
dda83cd7
SM
14083 {
14084 /* Data-processing (plain binary immediate) instruction. */
14085 return thumb2_record_ps_dest_generic (thumb2_insn_r);
14086 }
c6ec2b30 14087 else
dda83cd7
SM
14088 {
14089 /* Data-processing (modified immediate). */
14090 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
14091 }
c6ec2b30
OJ
14092 }
14093 else if (op1 == 0x03)
14094 {
14095 if (!(op2 & 0x71 ))
dda83cd7
SM
14096 {
14097 /* Store single data item. */
14098 return thumb2_record_str_single_data (thumb2_insn_r);
14099 }
c6ec2b30 14100 else if (!((op2 & 0x71) ^ 0x10))
dda83cd7
SM
14101 {
14102 /* Advanced SIMD or structure load/store instructions. */
14103 return thumb2_record_asimd_struct_ld_st (thumb2_insn_r);
14104 }
c6ec2b30 14105 else if (!((op2 & 0x67) ^ 0x01))
dda83cd7
SM
14106 {
14107 /* Load byte, memory hints instruction. */
14108 return thumb2_record_ld_mem_hints (thumb2_insn_r);
14109 }
c6ec2b30 14110 else if (!((op2 & 0x67) ^ 0x03))
dda83cd7
SM
14111 {
14112 /* Load halfword, memory hints instruction. */
14113 return thumb2_record_ld_mem_hints (thumb2_insn_r);
14114 }
c6ec2b30 14115 else if (!((op2 & 0x67) ^ 0x05))
dda83cd7
SM
14116 {
14117 /* Load word instruction. */
14118 return thumb2_record_ld_word (thumb2_insn_r);
14119 }
c6ec2b30 14120 else if (!((op2 & 0x70) ^ 0x20))
dda83cd7
SM
14121 {
14122 /* Data-processing (register) instruction. */
14123 return thumb2_record_ps_dest_generic (thumb2_insn_r);
14124 }
c6ec2b30 14125 else if (!((op2 & 0x78) ^ 0x30))
dda83cd7
SM
14126 {
14127 /* Multiply, multiply accumulate, abs diff instruction. */
14128 return thumb2_record_ps_dest_generic (thumb2_insn_r);
14129 }
c6ec2b30 14130 else if (!((op2 & 0x78) ^ 0x38))
dda83cd7
SM
14131 {
14132 /* Long multiply, long multiply accumulate, and divide. */
14133 return thumb2_record_lmul_lmla_div (thumb2_insn_r);
14134 }
c6ec2b30 14135 else if (op2 & 0x40)
dda83cd7
SM
14136 {
14137 /* Co-processor instructions. */
14138 return thumb2_record_coproc_insn (thumb2_insn_r);
14139 }
c6ec2b30
OJ
14140 }
14141
14142 return -1;
14143}
72508ac0 14144
ffdbe864 14145namespace {
728a7913
YQ
14146/* Abstract memory reader. */
14147
14148class abstract_memory_reader
14149{
14150public:
14151 /* Read LEN bytes of target memory at address MEMADDR, placing the
14152 results in GDB's memory at BUF. Return true on success. */
14153
14154 virtual bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) = 0;
14155};
14156
14157/* Instruction reader from real target. */
14158
14159class instruction_reader : public abstract_memory_reader
14160{
14161 public:
632e107b 14162 bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) override
728a7913
YQ
14163 {
14164 if (target_read_memory (memaddr, buf, len))
14165 return false;
14166 else
14167 return true;
14168 }
14169};
14170
ffdbe864
YQ
14171} // namespace
14172
72508ac0 14173/* Extracts arm/thumb/thumb2 insn depending on the size, and returns 0 on success
85102364 14174and positive val on failure. */
72508ac0
PO
14175
14176static int
728a7913 14177extract_arm_insn (abstract_memory_reader& reader,
4748a9be 14178 arm_insn_decode_record *insn_record, uint32_t insn_size)
72508ac0
PO
14179{
14180 gdb_byte buf[insn_size];
14181
14182 memset (&buf[0], 0, insn_size);
14183
728a7913 14184 if (!reader.read (insn_record->this_addr, buf, insn_size))
72508ac0
PO
14185 return 1;
14186 insn_record->arm_insn = (uint32_t) extract_unsigned_integer (&buf[0],
dda83cd7 14187 insn_size,
2959fed9 14188 gdbarch_byte_order_for_code (insn_record->gdbarch));
72508ac0
PO
14189 return 0;
14190}
14191
4748a9be 14192typedef int (*sti_arm_hdl_fp_t) (arm_insn_decode_record*);
72508ac0
PO
14193
14194/* Decode arm/thumb insn depending on condition cods and opcodes; and
14195 dispatch it. */
14196
14197static int
4748a9be
TT
14198decode_insn (abstract_memory_reader &reader,
14199 arm_insn_decode_record *arm_record,
728a7913 14200 record_type_t record_type, uint32_t insn_size)
72508ac0
PO
14201{
14202
01e57735
YQ
14203 /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm
14204 instruction. */
0fa9c223 14205 static const sti_arm_hdl_fp_t arm_handle_insn[8] =
72508ac0
PO
14206 {
14207 arm_record_data_proc_misc_ld_str, /* 000. */
14208 arm_record_data_proc_imm, /* 001. */
14209 arm_record_ld_st_imm_offset, /* 010. */
14210 arm_record_ld_st_reg_offset, /* 011. */
14211 arm_record_ld_st_multiple, /* 100. */
14212 arm_record_b_bl, /* 101. */
60cc5e93 14213 arm_record_asimd_vfp_coproc, /* 110. */
72508ac0
PO
14214 arm_record_coproc_data_proc /* 111. */
14215 };
14216
01e57735
YQ
14217 /* (Starting from numerical 0); bits 13,14,15 decodes type of thumb
14218 instruction. */
0fa9c223 14219 static const sti_arm_hdl_fp_t thumb_handle_insn[8] =
72508ac0
PO
14220 { \
14221 thumb_record_shift_add_sub, /* 000. */
14222 thumb_record_add_sub_cmp_mov, /* 001. */
14223 thumb_record_ld_st_reg_offset, /* 010. */
14224 thumb_record_ld_st_imm_offset, /* 011. */
14225 thumb_record_ld_st_stack, /* 100. */
14226 thumb_record_misc, /* 101. */
14227 thumb_record_ldm_stm_swi, /* 110. */
14228 thumb_record_branch /* 111. */
14229 };
14230
14231 uint32_t ret = 0; /* return value: negative:failure 0:success. */
14232 uint32_t insn_id = 0;
14233
728a7913 14234 if (extract_arm_insn (reader, arm_record, insn_size))
72508ac0
PO
14235 {
14236 if (record_debug)
01e57735 14237 {
6cb06a8c
TT
14238 gdb_printf (gdb_stdlog,
14239 _("Process record: error reading memory at "
14240 "addr %s len = %d.\n"),
14241 paddress (arm_record->gdbarch,
14242 arm_record->this_addr), insn_size);
01e57735 14243 }
72508ac0
PO
14244 return -1;
14245 }
14246 else if (ARM_RECORD == record_type)
14247 {
14248 arm_record->cond = bits (arm_record->arm_insn, 28, 31);
14249 insn_id = bits (arm_record->arm_insn, 25, 27);
ca92db2d
YQ
14250
14251 if (arm_record->cond == 0xf)
14252 ret = arm_record_extension_space (arm_record);
14253 else
01e57735 14254 {
ca92db2d
YQ
14255 /* If this insn has fallen into extension space
14256 then we need not decode it anymore. */
01e57735
YQ
14257 ret = arm_handle_insn[insn_id] (arm_record);
14258 }
ca92db2d
YQ
14259 if (ret != ARM_RECORD_SUCCESS)
14260 {
14261 arm_record_unsupported_insn (arm_record);
14262 ret = -1;
14263 }
72508ac0
PO
14264 }
14265 else if (THUMB_RECORD == record_type)
14266 {
14267 /* As thumb does not have condition codes, we set negative. */
14268 arm_record->cond = -1;
14269 insn_id = bits (arm_record->arm_insn, 13, 15);
14270 ret = thumb_handle_insn[insn_id] (arm_record);
ca92db2d
YQ
14271 if (ret != ARM_RECORD_SUCCESS)
14272 {
14273 arm_record_unsupported_insn (arm_record);
14274 ret = -1;
14275 }
72508ac0
PO
14276 }
14277 else if (THUMB2_RECORD == record_type)
14278 {
c6ec2b30
OJ
14279 /* As thumb does not have condition codes, we set negative. */
14280 arm_record->cond = -1;
14281
14282 /* Swap first half of 32bit thumb instruction with second half. */
14283 arm_record->arm_insn
01e57735 14284 = (arm_record->arm_insn >> 16) | (arm_record->arm_insn << 16);
c6ec2b30 14285
ca92db2d 14286 ret = thumb2_record_decode_insn_handler (arm_record);
c6ec2b30 14287
ca92db2d 14288 if (ret != ARM_RECORD_SUCCESS)
01e57735
YQ
14289 {
14290 arm_record_unsupported_insn (arm_record);
14291 ret = -1;
14292 }
72508ac0
PO
14293 }
14294 else
14295 {
14296 /* Throw assertion. */
14297 gdb_assert_not_reached ("not a valid instruction, could not decode");
14298 }
14299
14300 return ret;
14301}
14302
b121eeb9
YQ
14303#if GDB_SELF_TEST
14304namespace selftests {
14305
14306/* Provide both 16-bit and 32-bit thumb instructions. */
14307
14308class instruction_reader_thumb : public abstract_memory_reader
14309{
14310public:
14311 template<size_t SIZE>
14312 instruction_reader_thumb (enum bfd_endian endian,
14313 const uint16_t (&insns)[SIZE])
14314 : m_endian (endian), m_insns (insns), m_insns_size (SIZE)
14315 {}
14316
632e107b 14317 bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) override
b121eeb9
YQ
14318 {
14319 SELF_CHECK (len == 4 || len == 2);
14320 SELF_CHECK (memaddr % 2 == 0);
14321 SELF_CHECK ((memaddr / 2) < m_insns_size);
14322
14323 store_unsigned_integer (buf, 2, m_endian, m_insns[memaddr / 2]);
14324 if (len == 4)
14325 {
14326 store_unsigned_integer (&buf[2], 2, m_endian,
14327 m_insns[memaddr / 2 + 1]);
14328 }
14329 return true;
14330 }
14331
14332private:
14333 enum bfd_endian m_endian;
14334 const uint16_t *m_insns;
14335 size_t m_insns_size;
14336};
14337
14338static void
14339arm_record_test (void)
14340{
14341 struct gdbarch_info info;
b121eeb9
YQ
14342 info.bfd_arch_info = bfd_scan_arch ("arm");
14343
14344 struct gdbarch *gdbarch = gdbarch_find_by_info (info);
14345
14346 SELF_CHECK (gdbarch != NULL);
14347
14348 /* 16-bit Thumb instructions. */
14349 {
4748a9be 14350 arm_insn_decode_record arm_record;
b121eeb9 14351
4748a9be 14352 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
b121eeb9
YQ
14353 arm_record.gdbarch = gdbarch;
14354
14355 static const uint16_t insns[] = {
14356 /* db b2 uxtb r3, r3 */
14357 0xb2db,
14358 /* cd 58 ldr r5, [r1, r3] */
14359 0x58cd,
14360 };
14361
14362 enum bfd_endian endian = gdbarch_byte_order_for_code (arm_record.gdbarch);
14363 instruction_reader_thumb reader (endian, insns);
14364 int ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14365 THUMB_INSN_SIZE_BYTES);
14366
14367 SELF_CHECK (ret == 0);
14368 SELF_CHECK (arm_record.mem_rec_count == 0);
14369 SELF_CHECK (arm_record.reg_rec_count == 1);
14370 SELF_CHECK (arm_record.arm_regs[0] == 3);
14371
14372 arm_record.this_addr += 2;
14373 ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14374 THUMB_INSN_SIZE_BYTES);
14375
14376 SELF_CHECK (ret == 0);
14377 SELF_CHECK (arm_record.mem_rec_count == 0);
14378 SELF_CHECK (arm_record.reg_rec_count == 1);
14379 SELF_CHECK (arm_record.arm_regs[0] == 5);
14380 }
14381
14382 /* 32-bit Thumb-2 instructions. */
14383 {
4748a9be 14384 arm_insn_decode_record arm_record;
b121eeb9 14385
4748a9be 14386 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
b121eeb9
YQ
14387 arm_record.gdbarch = gdbarch;
14388
14389 static const uint16_t insns[] = {
14390 /* 1d ee 70 7f mrc 15, 0, r7, cr13, cr0, {3} */
14391 0xee1d, 0x7f70,
14392 };
14393
14394 enum bfd_endian endian = gdbarch_byte_order_for_code (arm_record.gdbarch);
14395 instruction_reader_thumb reader (endian, insns);
14396 int ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
14397 THUMB2_INSN_SIZE_BYTES);
14398
14399 SELF_CHECK (ret == 0);
14400 SELF_CHECK (arm_record.mem_rec_count == 0);
14401 SELF_CHECK (arm_record.reg_rec_count == 1);
14402 SELF_CHECK (arm_record.arm_regs[0] == 7);
14403 }
14404}
9ecab40c
SM
14405
14406/* Instruction reader from manually cooked instruction sequences. */
14407
14408class test_arm_instruction_reader : public arm_instruction_reader
14409{
14410public:
14411 explicit test_arm_instruction_reader (gdb::array_view<const uint32_t> insns)
14412 : m_insns (insns)
14413 {}
14414
14415 uint32_t read (CORE_ADDR memaddr, enum bfd_endian byte_order) const override
14416 {
14417 SELF_CHECK (memaddr % 4 == 0);
14418 SELF_CHECK (memaddr / 4 < m_insns.size ());
14419
14420 return m_insns[memaddr / 4];
14421 }
14422
14423private:
14424 const gdb::array_view<const uint32_t> m_insns;
14425};
14426
14427static void
14428arm_analyze_prologue_test ()
14429{
14430 for (bfd_endian endianness : {BFD_ENDIAN_LITTLE, BFD_ENDIAN_BIG})
14431 {
14432 struct gdbarch_info info;
9ecab40c
SM
14433 info.byte_order = endianness;
14434 info.byte_order_for_code = endianness;
14435 info.bfd_arch_info = bfd_scan_arch ("arm");
14436
14437 struct gdbarch *gdbarch = gdbarch_find_by_info (info);
14438
14439 SELF_CHECK (gdbarch != NULL);
14440
14441 /* The "sub" instruction contains an immediate value rotate count of 0,
14442 which resulted in a 32-bit shift of a 32-bit value, caught by
14443 UBSan. */
14444 const uint32_t insns[] = {
14445 0xe92d4ff0, /* push {r4, r5, r6, r7, r8, r9, sl, fp, lr} */
14446 0xe1a05000, /* mov r5, r0 */
14447 0xe5903020, /* ldr r3, [r0, #32] */
14448 0xe24dd044, /* sub sp, sp, #68 ; 0x44 */
14449 };
14450
14451 test_arm_instruction_reader mem_reader (insns);
14452 arm_prologue_cache cache;
0824193f 14453 arm_cache_init (&cache, gdbarch);
9ecab40c
SM
14454
14455 arm_analyze_prologue (gdbarch, 0, sizeof (insns) - 1, &cache, mem_reader);
14456 }
14457}
14458
b121eeb9
YQ
14459} // namespace selftests
14460#endif /* GDB_SELF_TEST */
72508ac0
PO
14461
14462/* Cleans up local record registers and memory allocations. */
14463
14464static void
4748a9be 14465deallocate_reg_mem (arm_insn_decode_record *record)
72508ac0
PO
14466{
14467 xfree (record->arm_regs);
14468 xfree (record->arm_mems);
14469}
14470
14471
01e57735 14472/* Parse the current instruction and record the values of the registers and
72508ac0
PO
14473 memory that will be changed in current instruction to record_arch_list".
14474 Return -1 if something is wrong. */
14475
14476int
01e57735
YQ
14477arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
14478 CORE_ADDR insn_addr)
72508ac0
PO
14479{
14480
72508ac0
PO
14481 uint32_t no_of_rec = 0;
14482 uint32_t ret = 0; /* return value: -1:record failure ; 0:success */
14483 ULONGEST t_bit = 0, insn_id = 0;
14484
14485 ULONGEST u_regval = 0;
14486
4748a9be 14487 arm_insn_decode_record arm_record;
72508ac0 14488
4748a9be 14489 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
72508ac0
PO
14490 arm_record.regcache = regcache;
14491 arm_record.this_addr = insn_addr;
14492 arm_record.gdbarch = gdbarch;
14493
14494
14495 if (record_debug > 1)
14496 {
6cb06a8c
TT
14497 gdb_printf (gdb_stdlog, "Process record: arm_process_record "
14498 "addr = %s\n",
14499 paddress (gdbarch, arm_record.this_addr));
72508ac0
PO
14500 }
14501
728a7913
YQ
14502 instruction_reader reader;
14503 if (extract_arm_insn (reader, &arm_record, 2))
72508ac0
PO
14504 {
14505 if (record_debug)
01e57735 14506 {
6cb06a8c
TT
14507 gdb_printf (gdb_stdlog,
14508 _("Process record: error reading memory at "
14509 "addr %s len = %d.\n"),
14510 paddress (arm_record.gdbarch,
14511 arm_record.this_addr), 2);
01e57735 14512 }
72508ac0
PO
14513 return -1;
14514 }
14515
14516 /* Check the insn, whether it is thumb or arm one. */
14517
14518 t_bit = arm_psr_thumb_bit (arm_record.gdbarch);
14519 regcache_raw_read_unsigned (arm_record.regcache, ARM_PS_REGNUM, &u_regval);
14520
14521
14522 if (!(u_regval & t_bit))
14523 {
14524 /* We are decoding arm insn. */
728a7913 14525 ret = decode_insn (reader, &arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES);
72508ac0
PO
14526 }
14527 else
14528 {
14529 insn_id = bits (arm_record.arm_insn, 11, 15);
14530 /* is it thumb2 insn? */
14531 if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id))
01e57735 14532 {
728a7913 14533 ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
01e57735
YQ
14534 THUMB2_INSN_SIZE_BYTES);
14535 }
72508ac0 14536 else
01e57735
YQ
14537 {
14538 /* We are decoding thumb insn. */
728a7913
YQ
14539 ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14540 THUMB_INSN_SIZE_BYTES);
01e57735 14541 }
72508ac0
PO
14542 }
14543
14544 if (0 == ret)
14545 {
14546 /* Record registers. */
25ea693b 14547 record_full_arch_list_add_reg (arm_record.regcache, ARM_PC_REGNUM);
72508ac0 14548 if (arm_record.arm_regs)
01e57735
YQ
14549 {
14550 for (no_of_rec = 0; no_of_rec < arm_record.reg_rec_count; no_of_rec++)
14551 {
14552 if (record_full_arch_list_add_reg
25ea693b 14553 (arm_record.regcache , arm_record.arm_regs[no_of_rec]))
01e57735
YQ
14554 ret = -1;
14555 }
14556 }
72508ac0
PO
14557 /* Record memories. */
14558 if (arm_record.arm_mems)
01e57735
YQ
14559 {
14560 for (no_of_rec = 0; no_of_rec < arm_record.mem_rec_count; no_of_rec++)
14561 {
14562 if (record_full_arch_list_add_mem
14563 ((CORE_ADDR)arm_record.arm_mems[no_of_rec].addr,
25ea693b 14564 arm_record.arm_mems[no_of_rec].len))
01e57735
YQ
14565 ret = -1;
14566 }
14567 }
72508ac0 14568
25ea693b 14569 if (record_full_arch_list_add_end ())
01e57735 14570 ret = -1;
72508ac0
PO
14571 }
14572
14573
14574 deallocate_reg_mem (&arm_record);
14575
14576 return ret;
14577}
d105cce5
AH
14578
14579/* See arm-tdep.h. */
14580
14581const target_desc *
92d48a1e 14582arm_read_description (arm_fp_type fp_type, bool tls)
d105cce5 14583{
92d48a1e 14584 struct target_desc *tdesc = tdesc_arm_list[fp_type][tls];
d105cce5
AH
14585
14586 if (tdesc == nullptr)
14587 {
92d48a1e
JB
14588 tdesc = arm_create_target_description (fp_type, tls);
14589 tdesc_arm_list[fp_type][tls] = tdesc;
d105cce5
AH
14590 }
14591
14592 return tdesc;
14593}
14594
14595/* See arm-tdep.h. */
14596
14597const target_desc *
14598arm_read_mprofile_description (arm_m_profile_type m_type)
14599{
14600 struct target_desc *tdesc = tdesc_arm_mprofile_list[m_type];
14601
14602 if (tdesc == nullptr)
14603 {
14604 tdesc = arm_create_mprofile_target_description (m_type);
14605 tdesc_arm_mprofile_list[m_type] = tdesc;
14606 }
14607
14608 return tdesc;
14609}