]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/arm-tdep.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / arm-tdep.c
CommitLineData
ed9a39eb 1/* Common target dependent code for GDB on ARM systems.
0fd88904 2
b811d2c2 3 Copyright (C) 1988-2020 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"
82ca8957 41#include "dwarf2/frame.h"
4de283e4
TT
42#include "gdbtypes.h"
43#include "prologue-value.h"
44#include "remote.h"
45#include "target-descriptions.h"
46#include "user-regs.h"
47#include "observable.h"
5f661e03 48#include "count-one-bits.h"
4de283e4 49
d55e5aa6 50#include "arch/arm.h"
4de283e4 51#include "arch/arm-get-next-pcs.h"
34e8f22d 52#include "arm-tdep.h"
4de283e4
TT
53#include "gdb/sim-arm.h"
54
d55e5aa6 55#include "elf-bfd.h"
4de283e4 56#include "coff/internal.h"
d55e5aa6 57#include "elf/arm.h"
4de283e4 58
4de283e4
TT
59#include "record.h"
60#include "record-full.h"
61#include <algorithm>
62
c2fd7fae
AKS
63#include "producer.h"
64
b121eeb9 65#if GDB_SELF_TEST
268a13a5 66#include "gdbsupport/selftest.h"
b121eeb9
YQ
67#endif
68
491144b5 69static bool arm_debug;
6529d2dd 70
082fc60d
RE
71/* Macros for setting and testing a bit in a minimal symbol that marks
72 it as Thumb function. The MSB of the minimal symbol's "info" field
f594e5e9 73 is used for this purpose.
082fc60d
RE
74
75 MSYMBOL_SET_SPECIAL Actually sets the "special" bit.
f594e5e9 76 MSYMBOL_IS_SPECIAL Tests the "special" bit in a minimal symbol. */
082fc60d 77
0963b4bd 78#define MSYMBOL_SET_SPECIAL(msym) \
b887350f 79 MSYMBOL_TARGET_FLAG_1 (msym) = 1
082fc60d
RE
80
81#define MSYMBOL_IS_SPECIAL(msym) \
b887350f 82 MSYMBOL_TARGET_FLAG_1 (msym)
082fc60d 83
60c5725c
DJ
84struct arm_mapping_symbol
85{
227031b2 86 CORE_ADDR value;
60c5725c 87 char type;
54cc7474
SM
88
89 bool operator< (const arm_mapping_symbol &other) const
90 { return this->value < other.value; }
60c5725c 91};
54cc7474
SM
92
93typedef std::vector<arm_mapping_symbol> arm_mapping_symbol_vec;
60c5725c 94
bd5766ec 95struct arm_per_bfd
60c5725c 96{
bd5766ec 97 explicit arm_per_bfd (size_t num_sections)
4838e44c
SM
98 : section_maps (new arm_mapping_symbol_vec[num_sections]),
99 section_maps_sorted (new bool[num_sections] ())
54cc7474
SM
100 {}
101
bd5766ec 102 DISABLE_COPY_AND_ASSIGN (arm_per_bfd);
54cc7474
SM
103
104 /* Information about mapping symbols ($a, $d, $t) in the objfile.
105
106 The format is an array of vectors of arm_mapping_symbols, there is one
107 vector for each section of the objfile (the array is index by BFD section
108 index).
109
110 For each section, the vector of arm_mapping_symbol is sorted by
111 symbol value (address). */
112 std::unique_ptr<arm_mapping_symbol_vec[]> section_maps;
4838e44c
SM
113
114 /* For each corresponding element of section_maps above, is this vector
115 sorted. */
116 std::unique_ptr<bool[]> section_maps_sorted;
60c5725c
DJ
117};
118
bd5766ec
LM
119/* Per-bfd data used for mapping symbols. */
120static bfd_key<arm_per_bfd> arm_bfd_data_key;
1b7f24cd 121
afd7eef0
RE
122/* The list of available "set arm ..." and "show arm ..." commands. */
123static struct cmd_list_element *setarmcmdlist = NULL;
124static struct cmd_list_element *showarmcmdlist = NULL;
125
fd50bc42
RE
126/* The type of floating-point to use. Keep this in sync with enum
127 arm_float_model, and the help string in _initialize_arm_tdep. */
40478521 128static const char *const fp_model_strings[] =
fd50bc42
RE
129{
130 "auto",
131 "softfpa",
132 "fpa",
133 "softvfp",
28e97307
DJ
134 "vfp",
135 NULL
fd50bc42
RE
136};
137
138/* A variable that can be configured by the user. */
139static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
140static const char *current_fp_model = "auto";
141
28e97307 142/* The ABI to use. Keep this in sync with arm_abi_kind. */
40478521 143static const char *const arm_abi_strings[] =
28e97307
DJ
144{
145 "auto",
146 "APCS",
147 "AAPCS",
148 NULL
149};
150
151/* A variable that can be configured by the user. */
152static enum arm_abi_kind arm_abi_global = ARM_ABI_AUTO;
153static const char *arm_abi_string = "auto";
154
0428b8f5 155/* The execution mode to assume. */
40478521 156static const char *const arm_mode_strings[] =
0428b8f5
DJ
157 {
158 "auto",
159 "arm",
68770265
MGD
160 "thumb",
161 NULL
0428b8f5
DJ
162 };
163
164static const char *arm_fallback_mode_string = "auto";
165static const char *arm_force_mode_string = "auto";
166
f32bf4a4
YQ
167/* The standard register names, and all the valid aliases for them. Note
168 that `fp', `sp' and `pc' are not added in this alias list, because they
169 have been added as builtin user registers in
170 std-regs.c:_initialize_frame_reg. */
123dc839
DJ
171static const struct
172{
173 const char *name;
174 int regnum;
175} arm_register_aliases[] = {
176 /* Basic register numbers. */
177 { "r0", 0 },
178 { "r1", 1 },
179 { "r2", 2 },
180 { "r3", 3 },
181 { "r4", 4 },
182 { "r5", 5 },
183 { "r6", 6 },
184 { "r7", 7 },
185 { "r8", 8 },
186 { "r9", 9 },
187 { "r10", 10 },
188 { "r11", 11 },
189 { "r12", 12 },
190 { "r13", 13 },
191 { "r14", 14 },
192 { "r15", 15 },
193 /* Synonyms (argument and variable registers). */
194 { "a1", 0 },
195 { "a2", 1 },
196 { "a3", 2 },
197 { "a4", 3 },
198 { "v1", 4 },
199 { "v2", 5 },
200 { "v3", 6 },
201 { "v4", 7 },
202 { "v5", 8 },
203 { "v6", 9 },
204 { "v7", 10 },
205 { "v8", 11 },
206 /* Other platform-specific names for r9. */
207 { "sb", 9 },
208 { "tr", 9 },
209 /* Special names. */
210 { "ip", 12 },
123dc839 211 { "lr", 14 },
123dc839
DJ
212 /* Names used by GCC (not listed in the ARM EABI). */
213 { "sl", 10 },
123dc839
DJ
214 /* A special name from the older ATPCS. */
215 { "wr", 7 },
216};
bc90b915 217
123dc839 218static const char *const arm_register_names[] =
da59e081
JM
219{"r0", "r1", "r2", "r3", /* 0 1 2 3 */
220 "r4", "r5", "r6", "r7", /* 4 5 6 7 */
221 "r8", "r9", "r10", "r11", /* 8 9 10 11 */
222 "r12", "sp", "lr", "pc", /* 12 13 14 15 */
223 "f0", "f1", "f2", "f3", /* 16 17 18 19 */
224 "f4", "f5", "f6", "f7", /* 20 21 22 23 */
94c30b78 225 "fps", "cpsr" }; /* 24 25 */
ed9a39eb 226
65b48a81
PB
227/* Holds the current set of options to be passed to the disassembler. */
228static char *arm_disassembler_options;
229
afd7eef0
RE
230/* Valid register name styles. */
231static const char **valid_disassembly_styles;
ed9a39eb 232
afd7eef0
RE
233/* Disassembly style to use. Default to "std" register names. */
234static const char *disassembly_style;
96baa820 235
d105cce5
AH
236/* All possible arm target descriptors. */
237static struct target_desc *tdesc_arm_list[ARM_FP_TYPE_INVALID];
238static struct target_desc *tdesc_arm_mprofile_list[ARM_M_TYPE_INVALID];
239
ed9a39eb 240/* This is used to keep the bfd arch_info in sync with the disassembly
afd7eef0 241 style. */
eb4c3f4a 242static void set_disassembly_style_sfunc (const char *, int,
ed9a39eb 243 struct cmd_list_element *);
65b48a81
PB
244static void show_disassembly_style_sfunc (struct ui_file *, int,
245 struct cmd_list_element *,
246 const char *);
ed9a39eb 247
05d1431c 248static enum register_status arm_neon_quad_read (struct gdbarch *gdbarch,
849d0ba8 249 readable_regcache *regcache,
05d1431c 250 int regnum, gdb_byte *buf);
58d6951d
DJ
251static void arm_neon_quad_write (struct gdbarch *gdbarch,
252 struct regcache *regcache,
253 int regnum, const gdb_byte *buf);
254
e7cf25a8 255static CORE_ADDR
553cb527 256 arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self);
e7cf25a8
YQ
257
258
d9311bfa
AT
259/* get_next_pcs operations. */
260static struct arm_get_next_pcs_ops arm_get_next_pcs_ops = {
261 arm_get_next_pcs_read_memory_unsigned_integer,
262 arm_get_next_pcs_syscall_next_pc,
263 arm_get_next_pcs_addr_bits_remove,
ed443b61
YQ
264 arm_get_next_pcs_is_thumb,
265 NULL,
d9311bfa
AT
266};
267
9b8d791a 268struct arm_prologue_cache
c3b4394c 269{
eb5492fa
DJ
270 /* The stack pointer at the time this frame was created; i.e. the
271 caller's stack pointer when this function was called. It is used
272 to identify this frame. */
273 CORE_ADDR prev_sp;
274
4be43953
DJ
275 /* The frame base for this frame is just prev_sp - frame size.
276 FRAMESIZE is the distance from the frame pointer to the
277 initial stack pointer. */
eb5492fa 278
c3b4394c 279 int framesize;
eb5492fa
DJ
280
281 /* The register used to hold the frame pointer for this frame. */
c3b4394c 282 int framereg;
eb5492fa
DJ
283
284 /* Saved register offsets. */
285 struct trad_frame_saved_reg *saved_regs;
c3b4394c 286};
ed9a39eb 287
0d39a070
DJ
288static CORE_ADDR arm_analyze_prologue (struct gdbarch *gdbarch,
289 CORE_ADDR prologue_start,
290 CORE_ADDR prologue_end,
291 struct arm_prologue_cache *cache);
292
cca44b1b
JB
293/* Architecture version for displaced stepping. This effects the behaviour of
294 certain instructions, and really should not be hard-wired. */
295
296#define DISPLACED_STEPPING_ARCH_VERSION 5
297
c7ae7675 298/* See arm-tdep.h. */
c906108c 299
491144b5 300bool arm_apcs_32 = true;
c906108c 301
9779414d
DJ
302/* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode. */
303
478fd957 304int
9779414d
DJ
305arm_psr_thumb_bit (struct gdbarch *gdbarch)
306{
307 if (gdbarch_tdep (gdbarch)->is_m)
308 return XPSR_T;
309 else
310 return CPSR_T;
311}
312
d0e59a68
AT
313/* Determine if the processor is currently executing in Thumb mode. */
314
315int
316arm_is_thumb (struct regcache *regcache)
317{
318 ULONGEST cpsr;
ac7936df 319 ULONGEST t_bit = arm_psr_thumb_bit (regcache->arch ());
d0e59a68
AT
320
321 cpsr = regcache_raw_get_unsigned (regcache, ARM_PS_REGNUM);
322
323 return (cpsr & t_bit) != 0;
324}
325
b39cc962
DJ
326/* Determine if FRAME is executing in Thumb mode. */
327
25b41d01 328int
b39cc962
DJ
329arm_frame_is_thumb (struct frame_info *frame)
330{
331 CORE_ADDR cpsr;
9779414d 332 ULONGEST t_bit = arm_psr_thumb_bit (get_frame_arch (frame));
b39cc962
DJ
333
334 /* Every ARM frame unwinder can unwind the T bit of the CPSR, either
335 directly (from a signal frame or dummy frame) or by interpreting
336 the saved LR (from a prologue or DWARF frame). So consult it and
337 trust the unwinders. */
338 cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
339
9779414d 340 return (cpsr & t_bit) != 0;
b39cc962
DJ
341}
342
f9d67f43
DJ
343/* Search for the mapping symbol covering MEMADDR. If one is found,
344 return its type. Otherwise, return 0. If START is non-NULL,
345 set *START to the location of the mapping symbol. */
c906108c 346
f9d67f43
DJ
347static char
348arm_find_mapping_symbol (CORE_ADDR memaddr, CORE_ADDR *start)
c906108c 349{
60c5725c 350 struct obj_section *sec;
0428b8f5 351
60c5725c
DJ
352 /* If there are mapping symbols, consult them. */
353 sec = find_pc_section (memaddr);
354 if (sec != NULL)
355 {
bd5766ec 356 arm_per_bfd *data = arm_bfd_data_key.get (sec->objfile->obfd);
60c5725c
DJ
357 if (data != NULL)
358 {
4838e44c
SM
359 unsigned int section_idx = sec->the_bfd_section->index;
360 arm_mapping_symbol_vec &map
361 = data->section_maps[section_idx];
362
363 /* Sort the vector on first use. */
364 if (!data->section_maps_sorted[section_idx])
365 {
366 std::sort (map.begin (), map.end ());
367 data->section_maps_sorted[section_idx] = true;
368 }
369
54cc7474
SM
370 struct arm_mapping_symbol map_key
371 = { memaddr - obj_section_addr (sec), 0 };
54cc7474
SM
372 arm_mapping_symbol_vec::const_iterator it
373 = std::lower_bound (map.begin (), map.end (), map_key);
374
375 /* std::lower_bound finds the earliest ordered insertion
376 point. If the symbol at this position starts at this exact
377 address, we use that; otherwise, the preceding
378 mapping symbol covers this address. */
379 if (it < map.end ())
60c5725c 380 {
54cc7474 381 if (it->value == map_key.value)
60c5725c 382 {
f9d67f43 383 if (start)
54cc7474
SM
384 *start = it->value + obj_section_addr (sec);
385 return it->type;
60c5725c
DJ
386 }
387 }
54cc7474
SM
388
389 if (it > map.begin ())
390 {
391 arm_mapping_symbol_vec::const_iterator prev_it
392 = it - 1;
393
394 if (start)
395 *start = prev_it->value + obj_section_addr (sec);
396 return prev_it->type;
397 }
60c5725c
DJ
398 }
399 }
400
f9d67f43
DJ
401 return 0;
402}
403
404/* Determine if the program counter specified in MEMADDR is in a Thumb
405 function. This function should be called for addresses unrelated to
406 any executing frame; otherwise, prefer arm_frame_is_thumb. */
407
e3039479 408int
9779414d 409arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
f9d67f43 410{
7cbd4a93 411 struct bound_minimal_symbol sym;
f9d67f43 412 char type;
cfba9872
SM
413 arm_displaced_step_closure *dsc
414 = ((arm_displaced_step_closure * )
415 get_displaced_step_closure_by_addr (memaddr));
a42244db
YQ
416
417 /* If checking the mode of displaced instruction in copy area, the mode
418 should be determined by instruction on the original address. */
419 if (dsc)
420 {
421 if (debug_displaced)
422 fprintf_unfiltered (gdb_stdlog,
423 "displaced: check mode of %.8lx instead of %.8lx\n",
424 (unsigned long) dsc->insn_addr,
425 (unsigned long) memaddr);
426 memaddr = dsc->insn_addr;
427 }
f9d67f43
DJ
428
429 /* If bit 0 of the address is set, assume this is a Thumb address. */
430 if (IS_THUMB_ADDR (memaddr))
431 return 1;
432
433 /* If the user wants to override the symbol table, let him. */
434 if (strcmp (arm_force_mode_string, "arm") == 0)
435 return 0;
436 if (strcmp (arm_force_mode_string, "thumb") == 0)
437 return 1;
438
9779414d
DJ
439 /* ARM v6-M and v7-M are always in Thumb mode. */
440 if (gdbarch_tdep (gdbarch)->is_m)
441 return 1;
442
f9d67f43
DJ
443 /* If there are mapping symbols, consult them. */
444 type = arm_find_mapping_symbol (memaddr, NULL);
445 if (type)
446 return type == 't';
447
ed9a39eb 448 /* Thumb functions have a "special" bit set in minimal symbols. */
c906108c 449 sym = lookup_minimal_symbol_by_pc (memaddr);
7cbd4a93
TT
450 if (sym.minsym)
451 return (MSYMBOL_IS_SPECIAL (sym.minsym));
0428b8f5
DJ
452
453 /* If the user wants to override the fallback mode, let them. */
454 if (strcmp (arm_fallback_mode_string, "arm") == 0)
455 return 0;
456 if (strcmp (arm_fallback_mode_string, "thumb") == 0)
457 return 1;
458
459 /* If we couldn't find any symbol, but we're talking to a running
460 target, then trust the current value of $cpsr. This lets
461 "display/i $pc" always show the correct mode (though if there is
462 a symbol table we will not reach here, so it still may not be
18819fa6 463 displayed in the mode it will be executed). */
0428b8f5 464 if (target_has_registers)
18819fa6 465 return arm_frame_is_thumb (get_current_frame ());
0428b8f5
DJ
466
467 /* Otherwise we're out of luck; we assume ARM. */
468 return 0;
c906108c
SS
469}
470
ca90e760 471/* Determine if the address specified equals any of these magic return
ba8b04a8 472 values, called EXC_RETURN, defined by the ARM v6-M, v7-M and v8-M
ca90e760
FH
473 architectures.
474
475 From ARMv6-M Reference Manual B1.5.8
476 Table B1-5 Exception return behavior
477
478 EXC_RETURN Return To Return Stack
479 0xFFFFFFF1 Handler mode Main
480 0xFFFFFFF9 Thread mode Main
481 0xFFFFFFFD Thread mode Process
482
483 From ARMv7-M Reference Manual B1.5.8
484 Table B1-8 EXC_RETURN definition of exception return behavior, no FP
485
486 EXC_RETURN Return To Return Stack
487 0xFFFFFFF1 Handler mode Main
488 0xFFFFFFF9 Thread mode Main
489 0xFFFFFFFD Thread mode Process
490
491 Table B1-9 EXC_RETURN definition of exception return behavior, with
492 FP
493
494 EXC_RETURN Return To Return Stack Frame Type
495 0xFFFFFFE1 Handler mode Main Extended
496 0xFFFFFFE9 Thread mode Main Extended
497 0xFFFFFFED Thread mode Process Extended
498 0xFFFFFFF1 Handler mode Main Basic
499 0xFFFFFFF9 Thread mode Main Basic
500 0xFFFFFFFD Thread mode Process Basic
501
502 For more details see "B1.5.8 Exception return behavior"
ba8b04a8
FH
503 in both ARMv6-M and ARMv7-M Architecture Reference Manuals.
504
505 In the ARMv8-M Architecture Technical Reference also adds
506 for implementations without the Security Extension:
507
508 EXC_RETURN Condition
509 0xFFFFFFB0 Return to Handler mode.
510 0xFFFFFFB8 Return to Thread mode using the main stack.
511 0xFFFFFFBC Return to Thread mode using the process stack. */
ca90e760
FH
512
513static int
514arm_m_addr_is_magic (CORE_ADDR addr)
515{
516 switch (addr)
517 {
ba8b04a8
FH
518 /* Values from ARMv8-M Architecture Technical Reference. */
519 case 0xffffffb0:
520 case 0xffffffb8:
521 case 0xffffffbc:
ca90e760
FH
522 /* Values from Tables in B1.5.8 the EXC_RETURN definitions of
523 the exception return behavior. */
524 case 0xffffffe1:
525 case 0xffffffe9:
526 case 0xffffffed:
527 case 0xfffffff1:
528 case 0xfffffff9:
529 case 0xfffffffd:
530 /* Address is magic. */
531 return 1;
532
533 default:
534 /* Address is not magic. */
535 return 0;
536 }
537}
538
181c1381 539/* Remove useless bits from addresses in a running program. */
34e8f22d 540static CORE_ADDR
24568a2c 541arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
c906108c 542{
2ae28aa9
YQ
543 /* On M-profile devices, do not strip the low bit from EXC_RETURN
544 (the magic exception return address). */
545 if (gdbarch_tdep (gdbarch)->is_m
ca90e760 546 && arm_m_addr_is_magic (val))
2ae28aa9
YQ
547 return val;
548
a3a2ee65 549 if (arm_apcs_32)
dd6be234 550 return UNMAKE_THUMB_ADDR (val);
c906108c 551 else
a3a2ee65 552 return (val & 0x03fffffc);
c906108c
SS
553}
554
0d39a070 555/* Return 1 if PC is the start of a compiler helper function which
e0634ccf
UW
556 can be safely ignored during prologue skipping. IS_THUMB is true
557 if the function is known to be a Thumb function due to the way it
558 is being called. */
0d39a070 559static int
e0634ccf 560skip_prologue_function (struct gdbarch *gdbarch, CORE_ADDR pc, int is_thumb)
0d39a070 561{
e0634ccf 562 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7cbd4a93 563 struct bound_minimal_symbol msym;
0d39a070
DJ
564
565 msym = lookup_minimal_symbol_by_pc (pc);
7cbd4a93 566 if (msym.minsym != NULL
77e371c0 567 && BMSYMBOL_VALUE_ADDRESS (msym) == pc
c9d95fa3 568 && msym.minsym->linkage_name () != NULL)
e0634ccf 569 {
c9d95fa3 570 const char *name = msym.minsym->linkage_name ();
0d39a070 571
e0634ccf
UW
572 /* The GNU linker's Thumb call stub to foo is named
573 __foo_from_thumb. */
574 if (strstr (name, "_from_thumb") != NULL)
575 name += 2;
0d39a070 576
e0634ccf
UW
577 /* On soft-float targets, __truncdfsf2 is called to convert promoted
578 arguments to their argument types in non-prototyped
579 functions. */
61012eef 580 if (startswith (name, "__truncdfsf2"))
e0634ccf 581 return 1;
61012eef 582 if (startswith (name, "__aeabi_d2f"))
e0634ccf 583 return 1;
0d39a070 584
e0634ccf 585 /* Internal functions related to thread-local storage. */
61012eef 586 if (startswith (name, "__tls_get_addr"))
e0634ccf 587 return 1;
61012eef 588 if (startswith (name, "__aeabi_read_tp"))
e0634ccf
UW
589 return 1;
590 }
591 else
592 {
593 /* If we run against a stripped glibc, we may be unable to identify
594 special functions by name. Check for one important case,
595 __aeabi_read_tp, by comparing the *code* against the default
596 implementation (this is hand-written ARM assembler in glibc). */
597
598 if (!is_thumb
198cd59d 599 && read_code_unsigned_integer (pc, 4, byte_order_for_code)
e0634ccf 600 == 0xe3e00a0f /* mov r0, #0xffff0fff */
198cd59d 601 && read_code_unsigned_integer (pc + 4, 4, byte_order_for_code)
e0634ccf
UW
602 == 0xe240f01f) /* sub pc, r0, #31 */
603 return 1;
604 }
ec3d575a 605
0d39a070
DJ
606 return 0;
607}
608
621c6d5b
YQ
609/* Extract the immediate from instruction movw/movt of encoding T. INSN1 is
610 the first 16-bit of instruction, and INSN2 is the second 16-bit of
611 instruction. */
612#define EXTRACT_MOVW_MOVT_IMM_T(insn1, insn2) \
613 ((bits ((insn1), 0, 3) << 12) \
614 | (bits ((insn1), 10, 10) << 11) \
615 | (bits ((insn2), 12, 14) << 8) \
616 | bits ((insn2), 0, 7))
617
618/* Extract the immediate from instruction movw/movt of encoding A. INSN is
619 the 32-bit instruction. */
620#define EXTRACT_MOVW_MOVT_IMM_A(insn) \
621 ((bits ((insn), 16, 19) << 12) \
622 | bits ((insn), 0, 11))
623
ec3d575a
UW
624/* Decode immediate value; implements ThumbExpandImmediate pseudo-op. */
625
626static unsigned int
627thumb_expand_immediate (unsigned int imm)
628{
629 unsigned int count = imm >> 7;
630
631 if (count < 8)
632 switch (count / 2)
633 {
634 case 0:
635 return imm & 0xff;
636 case 1:
637 return (imm & 0xff) | ((imm & 0xff) << 16);
638 case 2:
639 return ((imm & 0xff) << 8) | ((imm & 0xff) << 24);
640 case 3:
641 return (imm & 0xff) | ((imm & 0xff) << 8)
642 | ((imm & 0xff) << 16) | ((imm & 0xff) << 24);
643 }
644
645 return (0x80 | (imm & 0x7f)) << (32 - count);
646}
647
540314bd
YQ
648/* Return 1 if the 16-bit Thumb instruction INSN restores SP in
649 epilogue, 0 otherwise. */
650
651static int
652thumb_instruction_restores_sp (unsigned short insn)
653{
654 return (insn == 0x46bd /* mov sp, r7 */
655 || (insn & 0xff80) == 0xb000 /* add sp, imm */
656 || (insn & 0xfe00) == 0xbc00); /* pop <registers> */
657}
658
29d73ae4
DJ
659/* Analyze a Thumb prologue, looking for a recognizable stack frame
660 and frame pointer. Scan until we encounter a store that could
0d39a070
DJ
661 clobber the stack frame unexpectedly, or an unknown instruction.
662 Return the last address which is definitely safe to skip for an
663 initial breakpoint. */
c906108c
SS
664
665static CORE_ADDR
29d73ae4
DJ
666thumb_analyze_prologue (struct gdbarch *gdbarch,
667 CORE_ADDR start, CORE_ADDR limit,
668 struct arm_prologue_cache *cache)
c906108c 669{
0d39a070 670 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
e17a4113 671 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
29d73ae4
DJ
672 int i;
673 pv_t regs[16];
29d73ae4 674 CORE_ADDR offset;
ec3d575a 675 CORE_ADDR unrecognized_pc = 0;
da3c6d4a 676
29d73ae4
DJ
677 for (i = 0; i < 16; i++)
678 regs[i] = pv_register (i, 0);
f7b7ed97 679 pv_area stack (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
29d73ae4 680
29d73ae4 681 while (start < limit)
c906108c 682 {
29d73ae4
DJ
683 unsigned short insn;
684
198cd59d 685 insn = read_code_unsigned_integer (start, 2, byte_order_for_code);
9d4fde75 686
94c30b78 687 if ((insn & 0xfe00) == 0xb400) /* push { rlist } */
da59e081 688 {
29d73ae4
DJ
689 int regno;
690 int mask;
4be43953 691
f7b7ed97 692 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953 693 break;
29d73ae4
DJ
694
695 /* Bits 0-7 contain a mask for registers R0-R7. Bit 8 says
696 whether to save LR (R14). */
697 mask = (insn & 0xff) | ((insn & 0x100) << 6);
698
699 /* Calculate offsets of saved R0-R7 and LR. */
700 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
701 if (mask & (1 << regno))
702 {
29d73ae4
DJ
703 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
704 -4);
f7b7ed97 705 stack.store (regs[ARM_SP_REGNUM], 4, regs[regno]);
29d73ae4 706 }
da59e081 707 }
1db01f22 708 else if ((insn & 0xff80) == 0xb080) /* sub sp, #imm */
da59e081 709 {
29d73ae4 710 offset = (insn & 0x7f) << 2; /* get scaled offset */
1db01f22
YQ
711 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
712 -offset);
da59e081 713 }
808f7ab1
YQ
714 else if (thumb_instruction_restores_sp (insn))
715 {
716 /* Don't scan past the epilogue. */
717 break;
718 }
0d39a070
DJ
719 else if ((insn & 0xf800) == 0xa800) /* add Rd, sp, #imm */
720 regs[bits (insn, 8, 10)] = pv_add_constant (regs[ARM_SP_REGNUM],
721 (insn & 0xff) << 2);
722 else if ((insn & 0xfe00) == 0x1c00 /* add Rd, Rn, #imm */
723 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
724 regs[bits (insn, 0, 2)] = pv_add_constant (regs[bits (insn, 3, 5)],
725 bits (insn, 6, 8));
726 else if ((insn & 0xf800) == 0x3000 /* add Rd, #imm */
727 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
728 regs[bits (insn, 8, 10)] = pv_add_constant (regs[bits (insn, 8, 10)],
729 bits (insn, 0, 7));
730 else if ((insn & 0xfe00) == 0x1800 /* add Rd, Rn, Rm */
731 && pv_is_register (regs[bits (insn, 6, 8)], ARM_SP_REGNUM)
732 && pv_is_constant (regs[bits (insn, 3, 5)]))
733 regs[bits (insn, 0, 2)] = pv_add (regs[bits (insn, 3, 5)],
734 regs[bits (insn, 6, 8)]);
735 else if ((insn & 0xff00) == 0x4400 /* add Rd, Rm */
736 && pv_is_constant (regs[bits (insn, 3, 6)]))
737 {
738 int rd = (bit (insn, 7) << 3) + bits (insn, 0, 2);
739 int rm = bits (insn, 3, 6);
740 regs[rd] = pv_add (regs[rd], regs[rm]);
741 }
29d73ae4 742 else if ((insn & 0xff00) == 0x4600) /* mov hi, lo or mov lo, hi */
da59e081 743 {
29d73ae4
DJ
744 int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
745 int src_reg = (insn & 0x78) >> 3;
746 regs[dst_reg] = regs[src_reg];
da59e081 747 }
29d73ae4 748 else if ((insn & 0xf800) == 0x9000) /* str rd, [sp, #off] */
da59e081 749 {
29d73ae4
DJ
750 /* Handle stores to the stack. Normally pushes are used,
751 but with GCC -mtpcs-frame, there may be other stores
752 in the prologue to create the frame. */
753 int regno = (insn >> 8) & 0x7;
754 pv_t addr;
755
756 offset = (insn & 0xff) << 2;
757 addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
758
f7b7ed97 759 if (stack.store_would_trash (addr))
29d73ae4
DJ
760 break;
761
f7b7ed97 762 stack.store (addr, 4, regs[regno]);
da59e081 763 }
0d39a070
DJ
764 else if ((insn & 0xf800) == 0x6000) /* str rd, [rn, #off] */
765 {
766 int rd = bits (insn, 0, 2);
767 int rn = bits (insn, 3, 5);
768 pv_t addr;
769
770 offset = bits (insn, 6, 10) << 2;
771 addr = pv_add_constant (regs[rn], offset);
772
f7b7ed97 773 if (stack.store_would_trash (addr))
0d39a070
DJ
774 break;
775
f7b7ed97 776 stack.store (addr, 4, regs[rd]);
0d39a070
DJ
777 }
778 else if (((insn & 0xf800) == 0x7000 /* strb Rd, [Rn, #off] */
779 || (insn & 0xf800) == 0x8000) /* strh Rd, [Rn, #off] */
780 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
781 /* Ignore stores of argument registers to the stack. */
782 ;
783 else if ((insn & 0xf800) == 0xc800 /* ldmia Rn!, { registers } */
784 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
785 /* Ignore block loads from the stack, potentially copying
786 parameters from memory. */
787 ;
788 else if ((insn & 0xf800) == 0x9800 /* ldr Rd, [Rn, #immed] */
789 || ((insn & 0xf800) == 0x6800 /* ldr Rd, [sp, #immed] */
790 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM)))
791 /* Similarly ignore single loads from the stack. */
792 ;
793 else if ((insn & 0xffc0) == 0x0000 /* lsls Rd, Rm, #0 */
794 || (insn & 0xffc0) == 0x1c00) /* add Rd, Rn, #0 */
795 /* Skip register copies, i.e. saves to another register
796 instead of the stack. */
797 ;
798 else if ((insn & 0xf800) == 0x2000) /* movs Rd, #imm */
799 /* Recognize constant loads; even with small stacks these are necessary
800 on Thumb. */
801 regs[bits (insn, 8, 10)] = pv_constant (bits (insn, 0, 7));
802 else if ((insn & 0xf800) == 0x4800) /* ldr Rd, [pc, #imm] */
803 {
804 /* Constant pool loads, for the same reason. */
805 unsigned int constant;
806 CORE_ADDR loc;
807
808 loc = start + 4 + bits (insn, 0, 7) * 4;
809 constant = read_memory_unsigned_integer (loc, 4, byte_order);
810 regs[bits (insn, 8, 10)] = pv_constant (constant);
811 }
db24da6d 812 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instructions. */
0d39a070 813 {
0d39a070
DJ
814 unsigned short inst2;
815
198cd59d
YQ
816 inst2 = read_code_unsigned_integer (start + 2, 2,
817 byte_order_for_code);
0d39a070
DJ
818
819 if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
820 {
821 /* BL, BLX. Allow some special function calls when
822 skipping the prologue; GCC generates these before
823 storing arguments to the stack. */
824 CORE_ADDR nextpc;
825 int j1, j2, imm1, imm2;
826
827 imm1 = sbits (insn, 0, 10);
828 imm2 = bits (inst2, 0, 10);
829 j1 = bit (inst2, 13);
830 j2 = bit (inst2, 11);
831
832 offset = ((imm1 << 12) + (imm2 << 1));
833 offset ^= ((!j2) << 22) | ((!j1) << 23);
834
835 nextpc = start + 4 + offset;
836 /* For BLX make sure to clear the low bits. */
837 if (bit (inst2, 12) == 0)
838 nextpc = nextpc & 0xfffffffc;
839
e0634ccf
UW
840 if (!skip_prologue_function (gdbarch, nextpc,
841 bit (inst2, 12) != 0))
0d39a070
DJ
842 break;
843 }
ec3d575a 844
0963b4bd
MS
845 else if ((insn & 0xffd0) == 0xe900 /* stmdb Rn{!},
846 { registers } */
ec3d575a
UW
847 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
848 {
849 pv_t addr = regs[bits (insn, 0, 3)];
850 int regno;
851
f7b7ed97 852 if (stack.store_would_trash (addr))
ec3d575a
UW
853 break;
854
855 /* Calculate offsets of saved registers. */
856 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
857 if (inst2 & (1 << regno))
858 {
859 addr = pv_add_constant (addr, -4);
f7b7ed97 860 stack.store (addr, 4, regs[regno]);
ec3d575a
UW
861 }
862
863 if (insn & 0x0020)
864 regs[bits (insn, 0, 3)] = addr;
865 }
866
0963b4bd
MS
867 else if ((insn & 0xff50) == 0xe940 /* strd Rt, Rt2,
868 [Rn, #+/-imm]{!} */
ec3d575a
UW
869 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
870 {
871 int regno1 = bits (inst2, 12, 15);
872 int regno2 = bits (inst2, 8, 11);
873 pv_t addr = regs[bits (insn, 0, 3)];
874
875 offset = inst2 & 0xff;
876 if (insn & 0x0080)
877 addr = pv_add_constant (addr, offset);
878 else
879 addr = pv_add_constant (addr, -offset);
880
f7b7ed97 881 if (stack.store_would_trash (addr))
ec3d575a
UW
882 break;
883
f7b7ed97
TT
884 stack.store (addr, 4, regs[regno1]);
885 stack.store (pv_add_constant (addr, 4),
886 4, regs[regno2]);
ec3d575a
UW
887
888 if (insn & 0x0020)
889 regs[bits (insn, 0, 3)] = addr;
890 }
891
892 else if ((insn & 0xfff0) == 0xf8c0 /* str Rt,[Rn,+/-#imm]{!} */
893 && (inst2 & 0x0c00) == 0x0c00
894 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
895 {
896 int regno = bits (inst2, 12, 15);
897 pv_t addr = regs[bits (insn, 0, 3)];
898
899 offset = inst2 & 0xff;
900 if (inst2 & 0x0200)
901 addr = pv_add_constant (addr, offset);
902 else
903 addr = pv_add_constant (addr, -offset);
904
f7b7ed97 905 if (stack.store_would_trash (addr))
ec3d575a
UW
906 break;
907
f7b7ed97 908 stack.store (addr, 4, regs[regno]);
ec3d575a
UW
909
910 if (inst2 & 0x0100)
911 regs[bits (insn, 0, 3)] = addr;
912 }
913
914 else if ((insn & 0xfff0) == 0xf8c0 /* str.w Rt,[Rn,#imm] */
915 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
916 {
917 int regno = bits (inst2, 12, 15);
918 pv_t addr;
919
920 offset = inst2 & 0xfff;
921 addr = pv_add_constant (regs[bits (insn, 0, 3)], offset);
922
f7b7ed97 923 if (stack.store_would_trash (addr))
ec3d575a
UW
924 break;
925
f7b7ed97 926 stack.store (addr, 4, regs[regno]);
ec3d575a
UW
927 }
928
929 else if ((insn & 0xffd0) == 0xf880 /* str{bh}.w Rt,[Rn,#imm] */
0d39a070 930 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 931 /* Ignore stores of argument registers to the stack. */
0d39a070 932 ;
ec3d575a
UW
933
934 else if ((insn & 0xffd0) == 0xf800 /* str{bh} Rt,[Rn,#+/-imm] */
935 && (inst2 & 0x0d00) == 0x0c00
0d39a070 936 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 937 /* Ignore stores of argument registers to the stack. */
0d39a070 938 ;
ec3d575a 939
0963b4bd
MS
940 else if ((insn & 0xffd0) == 0xe890 /* ldmia Rn[!],
941 { registers } */
ec3d575a
UW
942 && (inst2 & 0x8000) == 0x0000
943 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
944 /* Ignore block loads from the stack, potentially copying
945 parameters from memory. */
0d39a070 946 ;
ec3d575a 947
f8c6d152 948 else if ((insn & 0xff70) == 0xe950 /* ldrd Rt, Rt2,
0963b4bd 949 [Rn, #+/-imm] */
0d39a070 950 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 951 /* Similarly ignore dual loads from the stack. */
0d39a070 952 ;
ec3d575a
UW
953
954 else if ((insn & 0xfff0) == 0xf850 /* ldr Rt,[Rn,#+/-imm] */
955 && (inst2 & 0x0d00) == 0x0c00
0d39a070 956 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 957 /* Similarly ignore single loads from the stack. */
0d39a070 958 ;
ec3d575a
UW
959
960 else if ((insn & 0xfff0) == 0xf8d0 /* ldr.w Rt,[Rn,#imm] */
0d39a070 961 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 962 /* Similarly ignore single loads from the stack. */
0d39a070 963 ;
ec3d575a
UW
964
965 else if ((insn & 0xfbf0) == 0xf100 /* add.w Rd, Rn, #imm */
966 && (inst2 & 0x8000) == 0x0000)
967 {
968 unsigned int imm = ((bits (insn, 10, 10) << 11)
969 | (bits (inst2, 12, 14) << 8)
970 | bits (inst2, 0, 7));
971
972 regs[bits (inst2, 8, 11)]
973 = pv_add_constant (regs[bits (insn, 0, 3)],
974 thumb_expand_immediate (imm));
975 }
976
977 else if ((insn & 0xfbf0) == 0xf200 /* addw Rd, Rn, #imm */
978 && (inst2 & 0x8000) == 0x0000)
0d39a070 979 {
ec3d575a
UW
980 unsigned int imm = ((bits (insn, 10, 10) << 11)
981 | (bits (inst2, 12, 14) << 8)
982 | bits (inst2, 0, 7));
983
984 regs[bits (inst2, 8, 11)]
985 = pv_add_constant (regs[bits (insn, 0, 3)], imm);
986 }
987
988 else if ((insn & 0xfbf0) == 0xf1a0 /* sub.w Rd, Rn, #imm */
989 && (inst2 & 0x8000) == 0x0000)
990 {
991 unsigned int imm = ((bits (insn, 10, 10) << 11)
992 | (bits (inst2, 12, 14) << 8)
993 | bits (inst2, 0, 7));
994
995 regs[bits (inst2, 8, 11)]
996 = pv_add_constant (regs[bits (insn, 0, 3)],
997 - (CORE_ADDR) thumb_expand_immediate (imm));
998 }
999
1000 else if ((insn & 0xfbf0) == 0xf2a0 /* subw Rd, Rn, #imm */
1001 && (inst2 & 0x8000) == 0x0000)
1002 {
1003 unsigned int imm = ((bits (insn, 10, 10) << 11)
1004 | (bits (inst2, 12, 14) << 8)
1005 | bits (inst2, 0, 7));
1006
1007 regs[bits (inst2, 8, 11)]
1008 = pv_add_constant (regs[bits (insn, 0, 3)], - (CORE_ADDR) imm);
1009 }
1010
1011 else if ((insn & 0xfbff) == 0xf04f) /* mov.w Rd, #const */
1012 {
1013 unsigned int imm = ((bits (insn, 10, 10) << 11)
1014 | (bits (inst2, 12, 14) << 8)
1015 | bits (inst2, 0, 7));
1016
1017 regs[bits (inst2, 8, 11)]
1018 = pv_constant (thumb_expand_immediate (imm));
1019 }
1020
1021 else if ((insn & 0xfbf0) == 0xf240) /* movw Rd, #const */
1022 {
621c6d5b
YQ
1023 unsigned int imm
1024 = EXTRACT_MOVW_MOVT_IMM_T (insn, inst2);
ec3d575a
UW
1025
1026 regs[bits (inst2, 8, 11)] = pv_constant (imm);
1027 }
1028
1029 else if (insn == 0xea5f /* mov.w Rd,Rm */
1030 && (inst2 & 0xf0f0) == 0)
1031 {
1032 int dst_reg = (inst2 & 0x0f00) >> 8;
1033 int src_reg = inst2 & 0xf;
1034 regs[dst_reg] = regs[src_reg];
1035 }
1036
1037 else if ((insn & 0xff7f) == 0xf85f) /* ldr.w Rt,<label> */
1038 {
1039 /* Constant pool loads. */
1040 unsigned int constant;
1041 CORE_ADDR loc;
1042
cac395ea 1043 offset = bits (inst2, 0, 11);
ec3d575a
UW
1044 if (insn & 0x0080)
1045 loc = start + 4 + offset;
1046 else
1047 loc = start + 4 - offset;
1048
1049 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1050 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1051 }
1052
1053 else if ((insn & 0xff7f) == 0xe95f) /* ldrd Rt,Rt2,<label> */
1054 {
1055 /* Constant pool loads. */
1056 unsigned int constant;
1057 CORE_ADDR loc;
1058
cac395ea 1059 offset = bits (inst2, 0, 7) << 2;
ec3d575a
UW
1060 if (insn & 0x0080)
1061 loc = start + 4 + offset;
1062 else
1063 loc = start + 4 - offset;
1064
1065 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1066 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1067
1068 constant = read_memory_unsigned_integer (loc + 4, 4, byte_order);
1069 regs[bits (inst2, 8, 11)] = pv_constant (constant);
1070 }
1071
1072 else if (thumb2_instruction_changes_pc (insn, inst2))
1073 {
1074 /* Don't scan past anything that might change control flow. */
0d39a070
DJ
1075 break;
1076 }
ec3d575a
UW
1077 else
1078 {
1079 /* The optimizer might shove anything into the prologue,
1080 so we just skip what we don't recognize. */
1081 unrecognized_pc = start;
1082 }
0d39a070
DJ
1083
1084 start += 2;
1085 }
ec3d575a 1086 else if (thumb_instruction_changes_pc (insn))
3d74b771 1087 {
ec3d575a 1088 /* Don't scan past anything that might change control flow. */
da3c6d4a 1089 break;
3d74b771 1090 }
ec3d575a
UW
1091 else
1092 {
1093 /* The optimizer might shove anything into the prologue,
1094 so we just skip what we don't recognize. */
1095 unrecognized_pc = start;
1096 }
29d73ae4
DJ
1097
1098 start += 2;
c906108c
SS
1099 }
1100
0d39a070
DJ
1101 if (arm_debug)
1102 fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1103 paddress (gdbarch, start));
1104
ec3d575a
UW
1105 if (unrecognized_pc == 0)
1106 unrecognized_pc = start;
1107
29d73ae4 1108 if (cache == NULL)
f7b7ed97 1109 return unrecognized_pc;
29d73ae4 1110
29d73ae4
DJ
1111 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1112 {
1113 /* Frame pointer is fp. Frame size is constant. */
1114 cache->framereg = ARM_FP_REGNUM;
1115 cache->framesize = -regs[ARM_FP_REGNUM].k;
1116 }
1117 else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
1118 {
1119 /* Frame pointer is r7. Frame size is constant. */
1120 cache->framereg = THUMB_FP_REGNUM;
1121 cache->framesize = -regs[THUMB_FP_REGNUM].k;
1122 }
72a2e3dc 1123 else
29d73ae4
DJ
1124 {
1125 /* Try the stack pointer... this is a bit desperate. */
1126 cache->framereg = ARM_SP_REGNUM;
1127 cache->framesize = -regs[ARM_SP_REGNUM].k;
1128 }
29d73ae4
DJ
1129
1130 for (i = 0; i < 16; i++)
f7b7ed97 1131 if (stack.find_reg (gdbarch, i, &offset))
29d73ae4
DJ
1132 cache->saved_regs[i].addr = offset;
1133
ec3d575a 1134 return unrecognized_pc;
c906108c
SS
1135}
1136
621c6d5b
YQ
1137
1138/* Try to analyze the instructions starting from PC, which load symbol
1139 __stack_chk_guard. Return the address of instruction after loading this
1140 symbol, set the dest register number to *BASEREG, and set the size of
1141 instructions for loading symbol in OFFSET. Return 0 if instructions are
1142 not recognized. */
1143
1144static CORE_ADDR
1145arm_analyze_load_stack_chk_guard(CORE_ADDR pc, struct gdbarch *gdbarch,
1146 unsigned int *destreg, int *offset)
1147{
1148 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1149 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1150 unsigned int low, high, address;
1151
1152 address = 0;
1153 if (is_thumb)
1154 {
1155 unsigned short insn1
198cd59d 1156 = read_code_unsigned_integer (pc, 2, byte_order_for_code);
621c6d5b
YQ
1157
1158 if ((insn1 & 0xf800) == 0x4800) /* ldr Rd, #immed */
1159 {
1160 *destreg = bits (insn1, 8, 10);
1161 *offset = 2;
6ae274b7
YQ
1162 address = (pc & 0xfffffffc) + 4 + (bits (insn1, 0, 7) << 2);
1163 address = read_memory_unsigned_integer (address, 4,
1164 byte_order_for_code);
621c6d5b
YQ
1165 }
1166 else if ((insn1 & 0xfbf0) == 0xf240) /* movw Rd, #const */
1167 {
1168 unsigned short insn2
198cd59d 1169 = read_code_unsigned_integer (pc + 2, 2, byte_order_for_code);
621c6d5b
YQ
1170
1171 low = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1172
1173 insn1
198cd59d 1174 = read_code_unsigned_integer (pc + 4, 2, byte_order_for_code);
621c6d5b 1175 insn2
198cd59d 1176 = read_code_unsigned_integer (pc + 6, 2, byte_order_for_code);
621c6d5b
YQ
1177
1178 /* movt Rd, #const */
1179 if ((insn1 & 0xfbc0) == 0xf2c0)
1180 {
1181 high = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1182 *destreg = bits (insn2, 8, 11);
1183 *offset = 8;
1184 address = (high << 16 | low);
1185 }
1186 }
1187 }
1188 else
1189 {
2e9e421f 1190 unsigned int insn
198cd59d 1191 = read_code_unsigned_integer (pc, 4, byte_order_for_code);
2e9e421f 1192
6ae274b7 1193 if ((insn & 0x0e5f0000) == 0x041f0000) /* ldr Rd, [PC, #immed] */
2e9e421f 1194 {
6ae274b7
YQ
1195 address = bits (insn, 0, 11) + pc + 8;
1196 address = read_memory_unsigned_integer (address, 4,
1197 byte_order_for_code);
1198
2e9e421f
UW
1199 *destreg = bits (insn, 12, 15);
1200 *offset = 4;
1201 }
1202 else if ((insn & 0x0ff00000) == 0x03000000) /* movw Rd, #const */
1203 {
1204 low = EXTRACT_MOVW_MOVT_IMM_A (insn);
1205
1206 insn
198cd59d 1207 = read_code_unsigned_integer (pc + 4, 4, byte_order_for_code);
2e9e421f
UW
1208
1209 if ((insn & 0x0ff00000) == 0x03400000) /* movt Rd, #const */
1210 {
1211 high = EXTRACT_MOVW_MOVT_IMM_A (insn);
1212 *destreg = bits (insn, 12, 15);
1213 *offset = 8;
1214 address = (high << 16 | low);
1215 }
1216 }
621c6d5b
YQ
1217 }
1218
1219 return address;
1220}
1221
1222/* Try to skip a sequence of instructions used for stack protector. If PC
0963b4bd
MS
1223 points to the first instruction of this sequence, return the address of
1224 first instruction after this sequence, otherwise, return original PC.
621c6d5b
YQ
1225
1226 On arm, this sequence of instructions is composed of mainly three steps,
1227 Step 1: load symbol __stack_chk_guard,
1228 Step 2: load from address of __stack_chk_guard,
1229 Step 3: store it to somewhere else.
1230
1231 Usually, instructions on step 2 and step 3 are the same on various ARM
1232 architectures. On step 2, it is one instruction 'ldr Rx, [Rn, #0]', and
1233 on step 3, it is also one instruction 'str Rx, [r7, #immd]'. However,
1234 instructions in step 1 vary from different ARM architectures. On ARMv7,
1235 they are,
1236
1237 movw Rn, #:lower16:__stack_chk_guard
1238 movt Rn, #:upper16:__stack_chk_guard
1239
1240 On ARMv5t, it is,
1241
1242 ldr Rn, .Label
1243 ....
1244 .Lable:
1245 .word __stack_chk_guard
1246
1247 Since ldr/str is a very popular instruction, we can't use them as
1248 'fingerprint' or 'signature' of stack protector sequence. Here we choose
1249 sequence {movw/movt, ldr}/ldr/str plus symbol __stack_chk_guard, if not
1250 stripped, as the 'fingerprint' of a stack protector cdoe sequence. */
1251
1252static CORE_ADDR
1253arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
1254{
1255 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
22e048c9 1256 unsigned int basereg;
7cbd4a93 1257 struct bound_minimal_symbol stack_chk_guard;
621c6d5b
YQ
1258 int offset;
1259 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1260 CORE_ADDR addr;
1261
1262 /* Try to parse the instructions in Step 1. */
1263 addr = arm_analyze_load_stack_chk_guard (pc, gdbarch,
1264 &basereg, &offset);
1265 if (!addr)
1266 return pc;
1267
1268 stack_chk_guard = lookup_minimal_symbol_by_pc (addr);
6041179a
JB
1269 /* ADDR must correspond to a symbol whose name is __stack_chk_guard.
1270 Otherwise, this sequence cannot be for stack protector. */
1271 if (stack_chk_guard.minsym == NULL
c9d95fa3 1272 || !startswith (stack_chk_guard.minsym->linkage_name (), "__stack_chk_guard"))
621c6d5b
YQ
1273 return pc;
1274
1275 if (is_thumb)
1276 {
1277 unsigned int destreg;
1278 unsigned short insn
198cd59d 1279 = read_code_unsigned_integer (pc + offset, 2, byte_order_for_code);
621c6d5b
YQ
1280
1281 /* Step 2: ldr Rd, [Rn, #immed], encoding T1. */
1282 if ((insn & 0xf800) != 0x6800)
1283 return pc;
1284 if (bits (insn, 3, 5) != basereg)
1285 return pc;
1286 destreg = bits (insn, 0, 2);
1287
198cd59d
YQ
1288 insn = read_code_unsigned_integer (pc + offset + 2, 2,
1289 byte_order_for_code);
621c6d5b
YQ
1290 /* Step 3: str Rd, [Rn, #immed], encoding T1. */
1291 if ((insn & 0xf800) != 0x6000)
1292 return pc;
1293 if (destreg != bits (insn, 0, 2))
1294 return pc;
1295 }
1296 else
1297 {
1298 unsigned int destreg;
1299 unsigned int insn
198cd59d 1300 = read_code_unsigned_integer (pc + offset, 4, byte_order_for_code);
621c6d5b
YQ
1301
1302 /* Step 2: ldr Rd, [Rn, #immed], encoding A1. */
1303 if ((insn & 0x0e500000) != 0x04100000)
1304 return pc;
1305 if (bits (insn, 16, 19) != basereg)
1306 return pc;
1307 destreg = bits (insn, 12, 15);
1308 /* Step 3: str Rd, [Rn, #immed], encoding A1. */
198cd59d 1309 insn = read_code_unsigned_integer (pc + offset + 4,
621c6d5b
YQ
1310 4, byte_order_for_code);
1311 if ((insn & 0x0e500000) != 0x04000000)
1312 return pc;
1313 if (bits (insn, 12, 15) != destreg)
1314 return pc;
1315 }
1316 /* The size of total two instructions ldr/str is 4 on Thumb-2, while 8
1317 on arm. */
1318 if (is_thumb)
1319 return pc + offset + 4;
1320 else
1321 return pc + offset + 8;
1322}
1323
da3c6d4a
MS
1324/* Advance the PC across any function entry prologue instructions to
1325 reach some "real" code.
34e8f22d
RE
1326
1327 The APCS (ARM Procedure Call Standard) defines the following
ed9a39eb 1328 prologue:
c906108c 1329
c5aa993b
JM
1330 mov ip, sp
1331 [stmfd sp!, {a1,a2,a3,a4}]
1332 stmfd sp!, {...,fp,ip,lr,pc}
ed9a39eb
JM
1333 [stfe f7, [sp, #-12]!]
1334 [stfe f6, [sp, #-12]!]
1335 [stfe f5, [sp, #-12]!]
1336 [stfe f4, [sp, #-12]!]
0963b4bd 1337 sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn. */
c906108c 1338
34e8f22d 1339static CORE_ADDR
6093d2eb 1340arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
c906108c 1341{
a89fea3c 1342 CORE_ADDR func_addr, limit_pc;
c906108c 1343
a89fea3c
JL
1344 /* See if we can determine the end of the prologue via the symbol table.
1345 If so, then return either PC, or the PC after the prologue, whichever
1346 is greater. */
1347 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
c906108c 1348 {
d80b854b
UW
1349 CORE_ADDR post_prologue_pc
1350 = skip_prologue_using_sal (gdbarch, func_addr);
43f3e411 1351 struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);
0d39a070 1352
621c6d5b
YQ
1353 if (post_prologue_pc)
1354 post_prologue_pc
1355 = arm_skip_stack_protector (post_prologue_pc, gdbarch);
1356
1357
0d39a070
DJ
1358 /* GCC always emits a line note before the prologue and another
1359 one after, even if the two are at the same address or on the
1360 same line. Take advantage of this so that we do not need to
1361 know every instruction that might appear in the prologue. We
1362 will have producer information for most binaries; if it is
1363 missing (e.g. for -gstabs), assuming the GNU tools. */
1364 if (post_prologue_pc
43f3e411
DE
1365 && (cust == NULL
1366 || COMPUNIT_PRODUCER (cust) == NULL
61012eef 1367 || startswith (COMPUNIT_PRODUCER (cust), "GNU ")
c2fd7fae 1368 || producer_is_llvm (COMPUNIT_PRODUCER (cust))))
0d39a070
DJ
1369 return post_prologue_pc;
1370
a89fea3c 1371 if (post_prologue_pc != 0)
0d39a070
DJ
1372 {
1373 CORE_ADDR analyzed_limit;
1374
1375 /* For non-GCC compilers, make sure the entire line is an
1376 acceptable prologue; GDB will round this function's
1377 return value up to the end of the following line so we
1378 can not skip just part of a line (and we do not want to).
1379
1380 RealView does not treat the prologue specially, but does
1381 associate prologue code with the opening brace; so this
1382 lets us skip the first line if we think it is the opening
1383 brace. */
9779414d 1384 if (arm_pc_is_thumb (gdbarch, func_addr))
0d39a070
DJ
1385 analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
1386 post_prologue_pc, NULL);
1387 else
1388 analyzed_limit = arm_analyze_prologue (gdbarch, func_addr,
1389 post_prologue_pc, NULL);
1390
1391 if (analyzed_limit != post_prologue_pc)
1392 return func_addr;
1393
1394 return post_prologue_pc;
1395 }
c906108c
SS
1396 }
1397
a89fea3c
JL
1398 /* Can't determine prologue from the symbol table, need to examine
1399 instructions. */
c906108c 1400
a89fea3c
JL
1401 /* Find an upper limit on the function prologue using the debug
1402 information. If the debug information could not be used to provide
1403 that bound, then use an arbitrary large number as the upper bound. */
0963b4bd 1404 /* Like arm_scan_prologue, stop no later than pc + 64. */
d80b854b 1405 limit_pc = skip_prologue_using_sal (gdbarch, pc);
a89fea3c
JL
1406 if (limit_pc == 0)
1407 limit_pc = pc + 64; /* Magic. */
1408
c906108c 1409
29d73ae4 1410 /* Check if this is Thumb code. */
9779414d 1411 if (arm_pc_is_thumb (gdbarch, pc))
a89fea3c 1412 return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
21daaaaf
YQ
1413 else
1414 return arm_analyze_prologue (gdbarch, pc, limit_pc, NULL);
c906108c 1415}
94c30b78 1416
c5aa993b 1417/* *INDENT-OFF* */
c906108c
SS
1418/* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
1419 This function decodes a Thumb function prologue to determine:
1420 1) the size of the stack frame
1421 2) which registers are saved on it
1422 3) the offsets of saved regs
1423 4) the offset from the stack pointer to the frame pointer
c906108c 1424
da59e081
JM
1425 A typical Thumb function prologue would create this stack frame
1426 (offsets relative to FP)
c906108c
SS
1427 old SP -> 24 stack parameters
1428 20 LR
1429 16 R7
1430 R7 -> 0 local variables (16 bytes)
1431 SP -> -12 additional stack space (12 bytes)
1432 The frame size would thus be 36 bytes, and the frame offset would be
0963b4bd 1433 12 bytes. The frame register is R7.
da59e081 1434
da3c6d4a
MS
1435 The comments for thumb_skip_prolog() describe the algorithm we use
1436 to detect the end of the prolog. */
c5aa993b
JM
1437/* *INDENT-ON* */
1438
c906108c 1439static void
be8626e0 1440thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
b39cc962 1441 CORE_ADDR block_addr, struct arm_prologue_cache *cache)
c906108c
SS
1442{
1443 CORE_ADDR prologue_start;
1444 CORE_ADDR prologue_end;
c906108c 1445
b39cc962
DJ
1446 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1447 &prologue_end))
c906108c 1448 {
ec3d575a
UW
1449 /* See comment in arm_scan_prologue for an explanation of
1450 this heuristics. */
1451 if (prologue_end > prologue_start + 64)
1452 {
1453 prologue_end = prologue_start + 64;
1454 }
c906108c
SS
1455 }
1456 else
f7060f85
DJ
1457 /* We're in the boondocks: we have no idea where the start of the
1458 function is. */
1459 return;
c906108c 1460
325fac50 1461 prologue_end = std::min (prologue_end, prev_pc);
c906108c 1462
be8626e0 1463 thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
1464}
1465
f303bc3e
YQ
1466/* Return 1 if the ARM instruction INSN restores SP in epilogue, 0
1467 otherwise. */
1468
1469static int
1470arm_instruction_restores_sp (unsigned int insn)
1471{
1472 if (bits (insn, 28, 31) != INST_NV)
1473 {
1474 if ((insn & 0x0df0f000) == 0x0080d000
1475 /* ADD SP (register or immediate). */
1476 || (insn & 0x0df0f000) == 0x0040d000
1477 /* SUB SP (register or immediate). */
1478 || (insn & 0x0ffffff0) == 0x01a0d000
1479 /* MOV SP. */
1480 || (insn & 0x0fff0000) == 0x08bd0000
1481 /* POP (LDMIA). */
1482 || (insn & 0x0fff0000) == 0x049d0000)
1483 /* POP of a single register. */
1484 return 1;
1485 }
1486
1487 return 0;
1488}
1489
0d39a070
DJ
1490/* Analyze an ARM mode prologue starting at PROLOGUE_START and
1491 continuing no further than PROLOGUE_END. If CACHE is non-NULL,
1492 fill it in. Return the first address not recognized as a prologue
1493 instruction.
eb5492fa 1494
0d39a070
DJ
1495 We recognize all the instructions typically found in ARM prologues,
1496 plus harmless instructions which can be skipped (either for analysis
1497 purposes, or a more restrictive set that can be skipped when finding
1498 the end of the prologue). */
1499
1500static CORE_ADDR
1501arm_analyze_prologue (struct gdbarch *gdbarch,
1502 CORE_ADDR prologue_start, CORE_ADDR prologue_end,
1503 struct arm_prologue_cache *cache)
1504{
0d39a070
DJ
1505 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1506 int regno;
1507 CORE_ADDR offset, current_pc;
1508 pv_t regs[ARM_FPS_REGNUM];
0d39a070
DJ
1509 CORE_ADDR unrecognized_pc = 0;
1510
1511 /* Search the prologue looking for instructions that set up the
96baa820 1512 frame pointer, adjust the stack pointer, and save registers.
ed9a39eb 1513
96baa820
JM
1514 Be careful, however, and if it doesn't look like a prologue,
1515 don't try to scan it. If, for instance, a frameless function
1516 begins with stmfd sp!, then we will tell ourselves there is
b8d5e71d 1517 a frame, which will confuse stack traceback, as well as "finish"
96baa820 1518 and other operations that rely on a knowledge of the stack
0d39a070 1519 traceback. */
d4473757 1520
4be43953
DJ
1521 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1522 regs[regno] = pv_register (regno, 0);
f7b7ed97 1523 pv_area stack (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
4be43953 1524
94c30b78
MS
1525 for (current_pc = prologue_start;
1526 current_pc < prologue_end;
f43845b3 1527 current_pc += 4)
96baa820 1528 {
e17a4113 1529 unsigned int insn
198cd59d 1530 = read_code_unsigned_integer (current_pc, 4, byte_order_for_code);
9d4fde75 1531
94c30b78 1532 if (insn == 0xe1a0c00d) /* mov ip, sp */
f43845b3 1533 {
4be43953 1534 regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
28cd8767
JG
1535 continue;
1536 }
0d39a070
DJ
1537 else if ((insn & 0xfff00000) == 0xe2800000 /* add Rd, Rn, #n */
1538 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767
JG
1539 {
1540 unsigned imm = insn & 0xff; /* immediate value */
1541 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
0d39a070 1542 int rd = bits (insn, 12, 15);
28cd8767 1543 imm = (imm >> rot) | (imm << (32 - rot));
0d39a070 1544 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
28cd8767
JG
1545 continue;
1546 }
0d39a070
DJ
1547 else if ((insn & 0xfff00000) == 0xe2400000 /* sub Rd, Rn, #n */
1548 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767
JG
1549 {
1550 unsigned imm = insn & 0xff; /* immediate value */
1551 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
0d39a070 1552 int rd = bits (insn, 12, 15);
28cd8767 1553 imm = (imm >> rot) | (imm << (32 - rot));
0d39a070 1554 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
f43845b3
MS
1555 continue;
1556 }
0963b4bd
MS
1557 else if ((insn & 0xffff0fff) == 0xe52d0004) /* str Rd,
1558 [sp, #-4]! */
f43845b3 1559 {
f7b7ed97 1560 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
1561 break;
1562 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
f7b7ed97
TT
1563 stack.store (regs[ARM_SP_REGNUM], 4,
1564 regs[bits (insn, 12, 15)]);
f43845b3
MS
1565 continue;
1566 }
1567 else if ((insn & 0xffff0000) == 0xe92d0000)
d4473757
KB
1568 /* stmfd sp!, {..., fp, ip, lr, pc}
1569 or
1570 stmfd sp!, {a1, a2, a3, a4} */
c906108c 1571 {
d4473757 1572 int mask = insn & 0xffff;
ed9a39eb 1573
f7b7ed97 1574 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
1575 break;
1576
94c30b78 1577 /* Calculate offsets of saved registers. */
34e8f22d 1578 for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
d4473757
KB
1579 if (mask & (1 << regno))
1580 {
0963b4bd
MS
1581 regs[ARM_SP_REGNUM]
1582 = pv_add_constant (regs[ARM_SP_REGNUM], -4);
f7b7ed97 1583 stack.store (regs[ARM_SP_REGNUM], 4, regs[regno]);
d4473757
KB
1584 }
1585 }
0d39a070
DJ
1586 else if ((insn & 0xffff0000) == 0xe54b0000 /* strb rx,[r11,#-n] */
1587 || (insn & 0xffff00f0) == 0xe14b00b0 /* strh rx,[r11,#-n] */
f8bf5763 1588 || (insn & 0xffffc000) == 0xe50b0000) /* str rx,[r11,#-n] */
b8d5e71d
MS
1589 {
1590 /* No need to add this to saved_regs -- it's just an arg reg. */
1591 continue;
1592 }
0d39a070
DJ
1593 else if ((insn & 0xffff0000) == 0xe5cd0000 /* strb rx,[sp,#n] */
1594 || (insn & 0xffff00f0) == 0xe1cd00b0 /* strh rx,[sp,#n] */
f8bf5763 1595 || (insn & 0xffffc000) == 0xe58d0000) /* str rx,[sp,#n] */
f43845b3
MS
1596 {
1597 /* No need to add this to saved_regs -- it's just an arg reg. */
1598 continue;
1599 }
0963b4bd
MS
1600 else if ((insn & 0xfff00000) == 0xe8800000 /* stm Rn,
1601 { registers } */
0d39a070
DJ
1602 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1603 {
1604 /* No need to add this to saved_regs -- it's just arg regs. */
1605 continue;
1606 }
d4473757
KB
1607 else if ((insn & 0xfffff000) == 0xe24cb000) /* sub fp, ip #n */
1608 {
94c30b78
MS
1609 unsigned imm = insn & 0xff; /* immediate value */
1610 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
d4473757 1611 imm = (imm >> rot) | (imm << (32 - rot));
4be43953 1612 regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
d4473757
KB
1613 }
1614 else if ((insn & 0xfffff000) == 0xe24dd000) /* sub sp, sp #n */
1615 {
94c30b78
MS
1616 unsigned imm = insn & 0xff; /* immediate value */
1617 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
d4473757 1618 imm = (imm >> rot) | (imm << (32 - rot));
4be43953 1619 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
d4473757 1620 }
0963b4bd
MS
1621 else if ((insn & 0xffff7fff) == 0xed6d0103 /* stfe f?,
1622 [sp, -#c]! */
2af46ca0 1623 && gdbarch_tdep (gdbarch)->have_fpa_registers)
d4473757 1624 {
f7b7ed97 1625 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
1626 break;
1627
1628 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
34e8f22d 1629 regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
f7b7ed97 1630 stack.store (regs[ARM_SP_REGNUM], 12, regs[regno]);
d4473757 1631 }
0963b4bd
MS
1632 else if ((insn & 0xffbf0fff) == 0xec2d0200 /* sfmfd f0, 4,
1633 [sp!] */
2af46ca0 1634 && gdbarch_tdep (gdbarch)->have_fpa_registers)
d4473757
KB
1635 {
1636 int n_saved_fp_regs;
1637 unsigned int fp_start_reg, fp_bound_reg;
1638
f7b7ed97 1639 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
1640 break;
1641
94c30b78 1642 if ((insn & 0x800) == 0x800) /* N0 is set */
96baa820 1643 {
d4473757
KB
1644 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1645 n_saved_fp_regs = 3;
1646 else
1647 n_saved_fp_regs = 1;
96baa820 1648 }
d4473757 1649 else
96baa820 1650 {
d4473757
KB
1651 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1652 n_saved_fp_regs = 2;
1653 else
1654 n_saved_fp_regs = 4;
96baa820 1655 }
d4473757 1656
34e8f22d 1657 fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
d4473757
KB
1658 fp_bound_reg = fp_start_reg + n_saved_fp_regs;
1659 for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
96baa820 1660 {
4be43953 1661 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
f7b7ed97
TT
1662 stack.store (regs[ARM_SP_REGNUM], 12,
1663 regs[fp_start_reg++]);
96baa820 1664 }
c906108c 1665 }
0d39a070
DJ
1666 else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
1667 {
1668 /* Allow some special function calls when skipping the
1669 prologue; GCC generates these before storing arguments to
1670 the stack. */
1671 CORE_ADDR dest = BranchDest (current_pc, insn);
1672
e0634ccf 1673 if (skip_prologue_function (gdbarch, dest, 0))
0d39a070
DJ
1674 continue;
1675 else
1676 break;
1677 }
d4473757 1678 else if ((insn & 0xf0000000) != 0xe0000000)
0963b4bd 1679 break; /* Condition not true, exit early. */
0d39a070
DJ
1680 else if (arm_instruction_changes_pc (insn))
1681 /* Don't scan past anything that might change control flow. */
1682 break;
f303bc3e
YQ
1683 else if (arm_instruction_restores_sp (insn))
1684 {
1685 /* Don't scan past the epilogue. */
1686 break;
1687 }
d19f7eee
UW
1688 else if ((insn & 0xfe500000) == 0xe8100000 /* ldm */
1689 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1690 /* Ignore block loads from the stack, potentially copying
1691 parameters from memory. */
1692 continue;
1693 else if ((insn & 0xfc500000) == 0xe4100000
1694 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1695 /* Similarly ignore single loads from the stack. */
1696 continue;
0d39a070
DJ
1697 else if ((insn & 0xffff0ff0) == 0xe1a00000)
1698 /* MOV Rd, Rm. Skip register copies, i.e. saves to another
1699 register instead of the stack. */
d4473757 1700 continue;
0d39a070
DJ
1701 else
1702 {
21daaaaf
YQ
1703 /* The optimizer might shove anything into the prologue, if
1704 we build up cache (cache != NULL) from scanning prologue,
1705 we just skip what we don't recognize and scan further to
1706 make cache as complete as possible. However, if we skip
1707 prologue, we'll stop immediately on unrecognized
1708 instruction. */
0d39a070 1709 unrecognized_pc = current_pc;
21daaaaf
YQ
1710 if (cache != NULL)
1711 continue;
1712 else
1713 break;
0d39a070 1714 }
c906108c
SS
1715 }
1716
0d39a070
DJ
1717 if (unrecognized_pc == 0)
1718 unrecognized_pc = current_pc;
1719
0d39a070
DJ
1720 if (cache)
1721 {
4072f920
YQ
1722 int framereg, framesize;
1723
1724 /* The frame size is just the distance from the frame register
1725 to the original stack pointer. */
1726 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1727 {
1728 /* Frame pointer is fp. */
1729 framereg = ARM_FP_REGNUM;
1730 framesize = -regs[ARM_FP_REGNUM].k;
1731 }
1732 else
1733 {
1734 /* Try the stack pointer... this is a bit desperate. */
1735 framereg = ARM_SP_REGNUM;
1736 framesize = -regs[ARM_SP_REGNUM].k;
1737 }
1738
0d39a070
DJ
1739 cache->framereg = framereg;
1740 cache->framesize = framesize;
1741
1742 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
f7b7ed97 1743 if (stack.find_reg (gdbarch, regno, &offset))
0d39a070
DJ
1744 cache->saved_regs[regno].addr = offset;
1745 }
1746
1747 if (arm_debug)
1748 fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1749 paddress (gdbarch, unrecognized_pc));
4be43953 1750
0d39a070
DJ
1751 return unrecognized_pc;
1752}
1753
1754static void
1755arm_scan_prologue (struct frame_info *this_frame,
1756 struct arm_prologue_cache *cache)
1757{
1758 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1759 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
bec2ab5a 1760 CORE_ADDR prologue_start, prologue_end;
0d39a070
DJ
1761 CORE_ADDR prev_pc = get_frame_pc (this_frame);
1762 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
0d39a070
DJ
1763
1764 /* Assume there is no frame until proven otherwise. */
1765 cache->framereg = ARM_SP_REGNUM;
1766 cache->framesize = 0;
1767
1768 /* Check for Thumb prologue. */
1769 if (arm_frame_is_thumb (this_frame))
1770 {
1771 thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
1772 return;
1773 }
1774
1775 /* Find the function prologue. If we can't find the function in
1776 the symbol table, peek in the stack frame to find the PC. */
1777 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1778 &prologue_end))
1779 {
1780 /* One way to find the end of the prologue (which works well
1781 for unoptimized code) is to do the following:
1782
1783 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
1784
1785 if (sal.line == 0)
1786 prologue_end = prev_pc;
1787 else if (sal.end < prologue_end)
1788 prologue_end = sal.end;
1789
1790 This mechanism is very accurate so long as the optimizer
1791 doesn't move any instructions from the function body into the
1792 prologue. If this happens, sal.end will be the last
1793 instruction in the first hunk of prologue code just before
1794 the first instruction that the scheduler has moved from
1795 the body to the prologue.
1796
1797 In order to make sure that we scan all of the prologue
1798 instructions, we use a slightly less accurate mechanism which
1799 may scan more than necessary. To help compensate for this
1800 lack of accuracy, the prologue scanning loop below contains
1801 several clauses which'll cause the loop to terminate early if
1802 an implausible prologue instruction is encountered.
1803
1804 The expression
1805
1806 prologue_start + 64
1807
1808 is a suitable endpoint since it accounts for the largest
1809 possible prologue plus up to five instructions inserted by
1810 the scheduler. */
1811
1812 if (prologue_end > prologue_start + 64)
1813 {
1814 prologue_end = prologue_start + 64; /* See above. */
1815 }
1816 }
1817 else
1818 {
1819 /* We have no symbol information. Our only option is to assume this
1820 function has a standard stack frame and the normal frame register.
1821 Then, we can find the value of our frame pointer on entrance to
1822 the callee (or at the present moment if this is the innermost frame).
1823 The value stored there should be the address of the stmfd + 8. */
1824 CORE_ADDR frame_loc;
7913a64c 1825 ULONGEST return_value;
0d39a070 1826
9e237747
MM
1827 /* AAPCS does not use a frame register, so we can abort here. */
1828 if (gdbarch_tdep (gdbarch)->arm_abi == ARM_ABI_AAPCS)
1829 return;
1830
0d39a070 1831 frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
7913a64c
YQ
1832 if (!safe_read_memory_unsigned_integer (frame_loc, 4, byte_order,
1833 &return_value))
0d39a070
DJ
1834 return;
1835 else
1836 {
1837 prologue_start = gdbarch_addr_bits_remove
1838 (gdbarch, return_value) - 8;
1839 prologue_end = prologue_start + 64; /* See above. */
1840 }
1841 }
1842
1843 if (prev_pc < prologue_end)
1844 prologue_end = prev_pc;
1845
1846 arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
1847}
1848
eb5492fa 1849static struct arm_prologue_cache *
a262aec2 1850arm_make_prologue_cache (struct frame_info *this_frame)
c906108c 1851{
eb5492fa
DJ
1852 int reg;
1853 struct arm_prologue_cache *cache;
1854 CORE_ADDR unwound_fp;
c5aa993b 1855
35d5d4ee 1856 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
a262aec2 1857 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
c906108c 1858
a262aec2 1859 arm_scan_prologue (this_frame, cache);
848cfffb 1860
a262aec2 1861 unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
eb5492fa
DJ
1862 if (unwound_fp == 0)
1863 return cache;
c906108c 1864
4be43953 1865 cache->prev_sp = unwound_fp + cache->framesize;
c906108c 1866
eb5492fa
DJ
1867 /* Calculate actual addresses of saved registers using offsets
1868 determined by arm_scan_prologue. */
a262aec2 1869 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
e28a332c 1870 if (trad_frame_addr_p (cache->saved_regs, reg))
eb5492fa
DJ
1871 cache->saved_regs[reg].addr += cache->prev_sp;
1872
1873 return cache;
c906108c
SS
1874}
1875
c1ee9414
LM
1876/* Implementation of the stop_reason hook for arm_prologue frames. */
1877
1878static enum unwind_stop_reason
1879arm_prologue_unwind_stop_reason (struct frame_info *this_frame,
1880 void **this_cache)
1881{
1882 struct arm_prologue_cache *cache;
1883 CORE_ADDR pc;
1884
1885 if (*this_cache == NULL)
1886 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 1887 cache = (struct arm_prologue_cache *) *this_cache;
c1ee9414
LM
1888
1889 /* This is meant to halt the backtrace at "_start". */
1890 pc = get_frame_pc (this_frame);
1891 if (pc <= gdbarch_tdep (get_frame_arch (this_frame))->lowest_pc)
1892 return UNWIND_OUTERMOST;
1893
1894 /* If we've hit a wall, stop. */
1895 if (cache->prev_sp == 0)
1896 return UNWIND_OUTERMOST;
1897
1898 return UNWIND_NO_REASON;
1899}
1900
eb5492fa
DJ
1901/* Our frame ID for a normal frame is the current function's starting PC
1902 and the caller's SP when we were called. */
c906108c 1903
148754e5 1904static void
a262aec2 1905arm_prologue_this_id (struct frame_info *this_frame,
eb5492fa
DJ
1906 void **this_cache,
1907 struct frame_id *this_id)
c906108c 1908{
eb5492fa
DJ
1909 struct arm_prologue_cache *cache;
1910 struct frame_id id;
2c404490 1911 CORE_ADDR pc, func;
f079148d 1912
eb5492fa 1913 if (*this_cache == NULL)
a262aec2 1914 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 1915 cache = (struct arm_prologue_cache *) *this_cache;
2a451106 1916
0e9e9abd
UW
1917 /* Use function start address as part of the frame ID. If we cannot
1918 identify the start address (due to missing symbol information),
1919 fall back to just using the current PC. */
c1ee9414 1920 pc = get_frame_pc (this_frame);
2c404490 1921 func = get_frame_func (this_frame);
0e9e9abd
UW
1922 if (!func)
1923 func = pc;
1924
eb5492fa 1925 id = frame_id_build (cache->prev_sp, func);
eb5492fa 1926 *this_id = id;
c906108c
SS
1927}
1928
a262aec2
DJ
1929static struct value *
1930arm_prologue_prev_register (struct frame_info *this_frame,
eb5492fa 1931 void **this_cache,
a262aec2 1932 int prev_regnum)
24de872b 1933{
24568a2c 1934 struct gdbarch *gdbarch = get_frame_arch (this_frame);
24de872b
DJ
1935 struct arm_prologue_cache *cache;
1936
eb5492fa 1937 if (*this_cache == NULL)
a262aec2 1938 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 1939 cache = (struct arm_prologue_cache *) *this_cache;
24de872b 1940
eb5492fa 1941 /* If we are asked to unwind the PC, then we need to return the LR
b39cc962
DJ
1942 instead. The prologue may save PC, but it will point into this
1943 frame's prologue, not the next frame's resume location. Also
1944 strip the saved T bit. A valid LR may have the low bit set, but
1945 a valid PC never does. */
eb5492fa 1946 if (prev_regnum == ARM_PC_REGNUM)
b39cc962
DJ
1947 {
1948 CORE_ADDR lr;
1949
1950 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
1951 return frame_unwind_got_constant (this_frame, prev_regnum,
24568a2c 1952 arm_addr_bits_remove (gdbarch, lr));
b39cc962 1953 }
24de872b 1954
eb5492fa 1955 /* SP is generally not saved to the stack, but this frame is
a262aec2 1956 identified by the next frame's stack pointer at the time of the call.
eb5492fa
DJ
1957 The value was already reconstructed into PREV_SP. */
1958 if (prev_regnum == ARM_SP_REGNUM)
a262aec2 1959 return frame_unwind_got_constant (this_frame, prev_regnum, cache->prev_sp);
eb5492fa 1960
b39cc962
DJ
1961 /* The CPSR may have been changed by the call instruction and by the
1962 called function. The only bit we can reconstruct is the T bit,
1963 by checking the low bit of LR as of the call. This is a reliable
1964 indicator of Thumb-ness except for some ARM v4T pre-interworking
1965 Thumb code, which could get away with a clear low bit as long as
1966 the called function did not use bx. Guess that all other
1967 bits are unchanged; the condition flags are presumably lost,
1968 but the processor status is likely valid. */
1969 if (prev_regnum == ARM_PS_REGNUM)
1970 {
1971 CORE_ADDR lr, cpsr;
9779414d 1972 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
b39cc962
DJ
1973
1974 cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
1975 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
1976 if (IS_THUMB_ADDR (lr))
9779414d 1977 cpsr |= t_bit;
b39cc962 1978 else
9779414d 1979 cpsr &= ~t_bit;
b39cc962
DJ
1980 return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
1981 }
1982
a262aec2
DJ
1983 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
1984 prev_regnum);
eb5492fa
DJ
1985}
1986
1987struct frame_unwind arm_prologue_unwind = {
1988 NORMAL_FRAME,
c1ee9414 1989 arm_prologue_unwind_stop_reason,
eb5492fa 1990 arm_prologue_this_id,
a262aec2
DJ
1991 arm_prologue_prev_register,
1992 NULL,
1993 default_frame_sniffer
eb5492fa
DJ
1994};
1995
0e9e9abd
UW
1996/* Maintain a list of ARM exception table entries per objfile, similar to the
1997 list of mapping symbols. We only cache entries for standard ARM-defined
1998 personality routines; the cache will contain only the frame unwinding
1999 instructions associated with the entry (not the descriptors). */
2000
0e9e9abd
UW
2001struct arm_exidx_entry
2002{
227031b2 2003 CORE_ADDR addr;
0e9e9abd 2004 gdb_byte *entry;
7a5d944b
TT
2005
2006 bool operator< (const arm_exidx_entry &other) const
2007 {
2008 return addr < other.addr;
2009 }
0e9e9abd 2010};
0e9e9abd
UW
2011
2012struct arm_exidx_data
2013{
7a5d944b 2014 std::vector<std::vector<arm_exidx_entry>> section_maps;
0e9e9abd
UW
2015};
2016
a2726d4f
LM
2017/* Per-BFD key to store exception handling information. */
2018static const struct bfd_key<arm_exidx_data> arm_exidx_data_key;
0e9e9abd
UW
2019
2020static struct obj_section *
2021arm_obj_section_from_vma (struct objfile *objfile, bfd_vma vma)
2022{
2023 struct obj_section *osect;
2024
2025 ALL_OBJFILE_OSECTIONS (objfile, osect)
fd361982 2026 if (bfd_section_flags (osect->the_bfd_section) & SEC_ALLOC)
0e9e9abd
UW
2027 {
2028 bfd_vma start, size;
fd361982
AM
2029 start = bfd_section_vma (osect->the_bfd_section);
2030 size = bfd_section_size (osect->the_bfd_section);
0e9e9abd
UW
2031
2032 if (start <= vma && vma < start + size)
2033 return osect;
2034 }
2035
2036 return NULL;
2037}
2038
2039/* Parse contents of exception table and exception index sections
2040 of OBJFILE, and fill in the exception table entry cache.
2041
2042 For each entry that refers to a standard ARM-defined personality
2043 routine, extract the frame unwinding instructions (from either
2044 the index or the table section). The unwinding instructions
2045 are normalized by:
2046 - extracting them from the rest of the table data
2047 - converting to host endianness
2048 - appending the implicit 0xb0 ("Finish") code
2049
2050 The extracted and normalized instructions are stored for later
2051 retrieval by the arm_find_exidx_entry routine. */
2052
2053static void
2054arm_exidx_new_objfile (struct objfile *objfile)
2055{
0e9e9abd
UW
2056 struct arm_exidx_data *data;
2057 asection *exidx, *extab;
2058 bfd_vma exidx_vma = 0, extab_vma = 0;
0e9e9abd
UW
2059 LONGEST i;
2060
2061 /* If we've already touched this file, do nothing. */
a2726d4f 2062 if (!objfile || arm_exidx_data_key.get (objfile->obfd) != NULL)
0e9e9abd
UW
2063 return;
2064
2065 /* Read contents of exception table and index. */
a5eda10c 2066 exidx = bfd_get_section_by_name (objfile->obfd, ELF_STRING_ARM_unwind);
984c7238 2067 gdb::byte_vector exidx_data;
0e9e9abd
UW
2068 if (exidx)
2069 {
fd361982
AM
2070 exidx_vma = bfd_section_vma (exidx);
2071 exidx_data.resize (bfd_section_size (exidx));
0e9e9abd
UW
2072
2073 if (!bfd_get_section_contents (objfile->obfd, exidx,
984c7238
TT
2074 exidx_data.data (), 0,
2075 exidx_data.size ()))
2076 return;
0e9e9abd
UW
2077 }
2078
2079 extab = bfd_get_section_by_name (objfile->obfd, ".ARM.extab");
984c7238 2080 gdb::byte_vector extab_data;
0e9e9abd
UW
2081 if (extab)
2082 {
fd361982
AM
2083 extab_vma = bfd_section_vma (extab);
2084 extab_data.resize (bfd_section_size (extab));
0e9e9abd
UW
2085
2086 if (!bfd_get_section_contents (objfile->obfd, extab,
984c7238
TT
2087 extab_data.data (), 0,
2088 extab_data.size ()))
2089 return;
0e9e9abd
UW
2090 }
2091
2092 /* Allocate exception table data structure. */
a2726d4f 2093 data = arm_exidx_data_key.emplace (objfile->obfd);
7a5d944b 2094 data->section_maps.resize (objfile->obfd->section_count);
0e9e9abd
UW
2095
2096 /* Fill in exception table. */
984c7238 2097 for (i = 0; i < exidx_data.size () / 8; i++)
0e9e9abd
UW
2098 {
2099 struct arm_exidx_entry new_exidx_entry;
984c7238
TT
2100 bfd_vma idx = bfd_h_get_32 (objfile->obfd, exidx_data.data () + i * 8);
2101 bfd_vma val = bfd_h_get_32 (objfile->obfd,
2102 exidx_data.data () + i * 8 + 4);
0e9e9abd
UW
2103 bfd_vma addr = 0, word = 0;
2104 int n_bytes = 0, n_words = 0;
2105 struct obj_section *sec;
2106 gdb_byte *entry = NULL;
2107
2108 /* Extract address of start of function. */
2109 idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2110 idx += exidx_vma + i * 8;
2111
2112 /* Find section containing function and compute section offset. */
2113 sec = arm_obj_section_from_vma (objfile, idx);
2114 if (sec == NULL)
2115 continue;
fd361982 2116 idx -= bfd_section_vma (sec->the_bfd_section);
0e9e9abd
UW
2117
2118 /* Determine address of exception table entry. */
2119 if (val == 1)
2120 {
2121 /* EXIDX_CANTUNWIND -- no exception table entry present. */
2122 }
2123 else if ((val & 0xff000000) == 0x80000000)
2124 {
2125 /* Exception table entry embedded in .ARM.exidx
2126 -- must be short form. */
2127 word = val;
2128 n_bytes = 3;
2129 }
2130 else if (!(val & 0x80000000))
2131 {
2132 /* Exception table entry in .ARM.extab. */
2133 addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2134 addr += exidx_vma + i * 8 + 4;
2135
984c7238 2136 if (addr >= extab_vma && addr + 4 <= extab_vma + extab_data.size ())
0e9e9abd
UW
2137 {
2138 word = bfd_h_get_32 (objfile->obfd,
984c7238 2139 extab_data.data () + addr - extab_vma);
0e9e9abd
UW
2140 addr += 4;
2141
2142 if ((word & 0xff000000) == 0x80000000)
2143 {
2144 /* Short form. */
2145 n_bytes = 3;
2146 }
2147 else if ((word & 0xff000000) == 0x81000000
2148 || (word & 0xff000000) == 0x82000000)
2149 {
2150 /* Long form. */
2151 n_bytes = 2;
2152 n_words = ((word >> 16) & 0xff);
2153 }
2154 else if (!(word & 0x80000000))
2155 {
2156 bfd_vma pers;
2157 struct obj_section *pers_sec;
2158 int gnu_personality = 0;
2159
2160 /* Custom personality routine. */
2161 pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2162 pers = UNMAKE_THUMB_ADDR (pers + addr - 4);
2163
2164 /* Check whether we've got one of the variants of the
2165 GNU personality routines. */
2166 pers_sec = arm_obj_section_from_vma (objfile, pers);
2167 if (pers_sec)
2168 {
2169 static const char *personality[] =
2170 {
2171 "__gcc_personality_v0",
2172 "__gxx_personality_v0",
2173 "__gcj_personality_v0",
2174 "__gnu_objc_personality_v0",
2175 NULL
2176 };
2177
2178 CORE_ADDR pc = pers + obj_section_offset (pers_sec);
2179 int k;
2180
2181 for (k = 0; personality[k]; k++)
2182 if (lookup_minimal_symbol_by_pc_name
2183 (pc, personality[k], objfile))
2184 {
2185 gnu_personality = 1;
2186 break;
2187 }
2188 }
2189
2190 /* If so, the next word contains a word count in the high
2191 byte, followed by the same unwind instructions as the
2192 pre-defined forms. */
2193 if (gnu_personality
984c7238 2194 && addr + 4 <= extab_vma + extab_data.size ())
0e9e9abd
UW
2195 {
2196 word = bfd_h_get_32 (objfile->obfd,
984c7238
TT
2197 (extab_data.data ()
2198 + addr - extab_vma));
0e9e9abd
UW
2199 addr += 4;
2200 n_bytes = 3;
2201 n_words = ((word >> 24) & 0xff);
2202 }
2203 }
2204 }
2205 }
2206
2207 /* Sanity check address. */
2208 if (n_words)
984c7238
TT
2209 if (addr < extab_vma
2210 || addr + 4 * n_words > extab_vma + extab_data.size ())
0e9e9abd
UW
2211 n_words = n_bytes = 0;
2212
2213 /* The unwind instructions reside in WORD (only the N_BYTES least
2214 significant bytes are valid), followed by N_WORDS words in the
2215 extab section starting at ADDR. */
2216 if (n_bytes || n_words)
2217 {
224c3ddb
SM
2218 gdb_byte *p = entry
2219 = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack,
2220 n_bytes + n_words * 4 + 1);
0e9e9abd
UW
2221
2222 while (n_bytes--)
2223 *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);
2224
2225 while (n_words--)
2226 {
2227 word = bfd_h_get_32 (objfile->obfd,
984c7238 2228 extab_data.data () + addr - extab_vma);
0e9e9abd
UW
2229 addr += 4;
2230
2231 *p++ = (gdb_byte) ((word >> 24) & 0xff);
2232 *p++ = (gdb_byte) ((word >> 16) & 0xff);
2233 *p++ = (gdb_byte) ((word >> 8) & 0xff);
2234 *p++ = (gdb_byte) (word & 0xff);
2235 }
2236
2237 /* Implied "Finish" to terminate the list. */
2238 *p++ = 0xb0;
2239 }
2240
2241 /* Push entry onto vector. They are guaranteed to always
2242 appear in order of increasing addresses. */
2243 new_exidx_entry.addr = idx;
2244 new_exidx_entry.entry = entry;
7a5d944b
TT
2245 data->section_maps[sec->the_bfd_section->index].push_back
2246 (new_exidx_entry);
0e9e9abd 2247 }
0e9e9abd
UW
2248}
2249
2250/* Search for the exception table entry covering MEMADDR. If one is found,
2251 return a pointer to its data. Otherwise, return 0. If START is non-NULL,
2252 set *START to the start of the region covered by this entry. */
2253
2254static gdb_byte *
2255arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
2256{
2257 struct obj_section *sec;
2258
2259 sec = find_pc_section (memaddr);
2260 if (sec != NULL)
2261 {
2262 struct arm_exidx_data *data;
0e9e9abd 2263 struct arm_exidx_entry map_key = { memaddr - obj_section_addr (sec), 0 };
0e9e9abd 2264
a2726d4f 2265 data = arm_exidx_data_key.get (sec->objfile->obfd);
0e9e9abd
UW
2266 if (data != NULL)
2267 {
7a5d944b
TT
2268 std::vector<arm_exidx_entry> &map
2269 = data->section_maps[sec->the_bfd_section->index];
2270 if (!map.empty ())
0e9e9abd 2271 {
7a5d944b 2272 auto idx = std::lower_bound (map.begin (), map.end (), map_key);
0e9e9abd 2273
7a5d944b 2274 /* std::lower_bound finds the earliest ordered insertion
0e9e9abd
UW
2275 point. If the following symbol starts at this exact
2276 address, we use that; otherwise, the preceding
2277 exception table entry covers this address. */
7a5d944b 2278 if (idx < map.end ())
0e9e9abd 2279 {
7a5d944b 2280 if (idx->addr == map_key.addr)
0e9e9abd
UW
2281 {
2282 if (start)
7a5d944b
TT
2283 *start = idx->addr + obj_section_addr (sec);
2284 return idx->entry;
0e9e9abd
UW
2285 }
2286 }
2287
7a5d944b 2288 if (idx > map.begin ())
0e9e9abd 2289 {
7a5d944b 2290 idx = idx - 1;
0e9e9abd 2291 if (start)
7a5d944b
TT
2292 *start = idx->addr + obj_section_addr (sec);
2293 return idx->entry;
0e9e9abd
UW
2294 }
2295 }
2296 }
2297 }
2298
2299 return NULL;
2300}
2301
2302/* Given the current frame THIS_FRAME, and its associated frame unwinding
2303 instruction list from the ARM exception table entry ENTRY, allocate and
2304 return a prologue cache structure describing how to unwind this frame.
2305
2306 Return NULL if the unwinding instruction list contains a "spare",
2307 "reserved" or "refuse to unwind" instruction as defined in section
2308 "9.3 Frame unwinding instructions" of the "Exception Handling ABI
2309 for the ARM Architecture" document. */
2310
2311static struct arm_prologue_cache *
2312arm_exidx_fill_cache (struct frame_info *this_frame, gdb_byte *entry)
2313{
2314 CORE_ADDR vsp = 0;
2315 int vsp_valid = 0;
2316
2317 struct arm_prologue_cache *cache;
2318 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2319 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2320
2321 for (;;)
2322 {
2323 gdb_byte insn;
2324
2325 /* Whenever we reload SP, we actually have to retrieve its
2326 actual value in the current frame. */
2327 if (!vsp_valid)
2328 {
2329 if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2330 {
2331 int reg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2332 vsp = get_frame_register_unsigned (this_frame, reg);
2333 }
2334 else
2335 {
2336 CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr;
2337 vsp = get_frame_memory_unsigned (this_frame, addr, 4);
2338 }
2339
2340 vsp_valid = 1;
2341 }
2342
2343 /* Decode next unwind instruction. */
2344 insn = *entry++;
2345
2346 if ((insn & 0xc0) == 0)
2347 {
2348 int offset = insn & 0x3f;
2349 vsp += (offset << 2) + 4;
2350 }
2351 else if ((insn & 0xc0) == 0x40)
2352 {
2353 int offset = insn & 0x3f;
2354 vsp -= (offset << 2) + 4;
2355 }
2356 else if ((insn & 0xf0) == 0x80)
2357 {
2358 int mask = ((insn & 0xf) << 8) | *entry++;
2359 int i;
2360
2361 /* The special case of an all-zero mask identifies
2362 "Refuse to unwind". We return NULL to fall back
2363 to the prologue analyzer. */
2364 if (mask == 0)
2365 return NULL;
2366
2367 /* Pop registers r4..r15 under mask. */
2368 for (i = 0; i < 12; i++)
2369 if (mask & (1 << i))
2370 {
2371 cache->saved_regs[4 + i].addr = vsp;
2372 vsp += 4;
2373 }
2374
2375 /* Special-case popping SP -- we need to reload vsp. */
2376 if (mask & (1 << (ARM_SP_REGNUM - 4)))
2377 vsp_valid = 0;
2378 }
2379 else if ((insn & 0xf0) == 0x90)
2380 {
2381 int reg = insn & 0xf;
2382
2383 /* Reserved cases. */
2384 if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
2385 return NULL;
2386
2387 /* Set SP from another register and mark VSP for reload. */
2388 cache->saved_regs[ARM_SP_REGNUM] = cache->saved_regs[reg];
2389 vsp_valid = 0;
2390 }
2391 else if ((insn & 0xf0) == 0xa0)
2392 {
2393 int count = insn & 0x7;
2394 int pop_lr = (insn & 0x8) != 0;
2395 int i;
2396
2397 /* Pop r4..r[4+count]. */
2398 for (i = 0; i <= count; i++)
2399 {
2400 cache->saved_regs[4 + i].addr = vsp;
2401 vsp += 4;
2402 }
2403
2404 /* If indicated by flag, pop LR as well. */
2405 if (pop_lr)
2406 {
2407 cache->saved_regs[ARM_LR_REGNUM].addr = vsp;
2408 vsp += 4;
2409 }
2410 }
2411 else if (insn == 0xb0)
2412 {
2413 /* We could only have updated PC by popping into it; if so, it
2414 will show up as address. Otherwise, copy LR into PC. */
2415 if (!trad_frame_addr_p (cache->saved_regs, ARM_PC_REGNUM))
2416 cache->saved_regs[ARM_PC_REGNUM]
2417 = cache->saved_regs[ARM_LR_REGNUM];
2418
2419 /* We're done. */
2420 break;
2421 }
2422 else if (insn == 0xb1)
2423 {
2424 int mask = *entry++;
2425 int i;
2426
2427 /* All-zero mask and mask >= 16 is "spare". */
2428 if (mask == 0 || mask >= 16)
2429 return NULL;
2430
2431 /* Pop r0..r3 under mask. */
2432 for (i = 0; i < 4; i++)
2433 if (mask & (1 << i))
2434 {
2435 cache->saved_regs[i].addr = vsp;
2436 vsp += 4;
2437 }
2438 }
2439 else if (insn == 0xb2)
2440 {
2441 ULONGEST offset = 0;
2442 unsigned shift = 0;
2443
2444 do
2445 {
2446 offset |= (*entry & 0x7f) << shift;
2447 shift += 7;
2448 }
2449 while (*entry++ & 0x80);
2450
2451 vsp += 0x204 + (offset << 2);
2452 }
2453 else if (insn == 0xb3)
2454 {
2455 int start = *entry >> 4;
2456 int count = (*entry++) & 0xf;
2457 int i;
2458
2459 /* Only registers D0..D15 are valid here. */
2460 if (start + count >= 16)
2461 return NULL;
2462
2463 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2464 for (i = 0; i <= count; i++)
2465 {
2466 cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2467 vsp += 8;
2468 }
2469
2470 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2471 vsp += 4;
2472 }
2473 else if ((insn & 0xf8) == 0xb8)
2474 {
2475 int count = insn & 0x7;
2476 int i;
2477
2478 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2479 for (i = 0; i <= count; i++)
2480 {
2481 cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2482 vsp += 8;
2483 }
2484
2485 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2486 vsp += 4;
2487 }
2488 else if (insn == 0xc6)
2489 {
2490 int start = *entry >> 4;
2491 int count = (*entry++) & 0xf;
2492 int i;
2493
2494 /* Only registers WR0..WR15 are valid. */
2495 if (start + count >= 16)
2496 return NULL;
2497
2498 /* Pop iwmmx registers WR[start]..WR[start+count]. */
2499 for (i = 0; i <= count; i++)
2500 {
2501 cache->saved_regs[ARM_WR0_REGNUM + start + i].addr = vsp;
2502 vsp += 8;
2503 }
2504 }
2505 else if (insn == 0xc7)
2506 {
2507 int mask = *entry++;
2508 int i;
2509
2510 /* All-zero mask and mask >= 16 is "spare". */
2511 if (mask == 0 || mask >= 16)
2512 return NULL;
2513
2514 /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask. */
2515 for (i = 0; i < 4; i++)
2516 if (mask & (1 << i))
2517 {
2518 cache->saved_regs[ARM_WCGR0_REGNUM + i].addr = vsp;
2519 vsp += 4;
2520 }
2521 }
2522 else if ((insn & 0xf8) == 0xc0)
2523 {
2524 int count = insn & 0x7;
2525 int i;
2526
2527 /* Pop iwmmx registers WR[10]..WR[10+count]. */
2528 for (i = 0; i <= count; i++)
2529 {
2530 cache->saved_regs[ARM_WR0_REGNUM + 10 + i].addr = vsp;
2531 vsp += 8;
2532 }
2533 }
2534 else if (insn == 0xc8)
2535 {
2536 int start = *entry >> 4;
2537 int count = (*entry++) & 0xf;
2538 int i;
2539
2540 /* Only registers D0..D31 are valid. */
2541 if (start + count >= 16)
2542 return NULL;
2543
2544 /* Pop VFP double-precision registers
2545 D[16+start]..D[16+start+count]. */
2546 for (i = 0; i <= count; i++)
2547 {
2548 cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].addr = vsp;
2549 vsp += 8;
2550 }
2551 }
2552 else if (insn == 0xc9)
2553 {
2554 int start = *entry >> 4;
2555 int count = (*entry++) & 0xf;
2556 int i;
2557
2558 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2559 for (i = 0; i <= count; i++)
2560 {
2561 cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2562 vsp += 8;
2563 }
2564 }
2565 else if ((insn & 0xf8) == 0xd0)
2566 {
2567 int count = insn & 0x7;
2568 int i;
2569
2570 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2571 for (i = 0; i <= count; i++)
2572 {
2573 cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2574 vsp += 8;
2575 }
2576 }
2577 else
2578 {
2579 /* Everything else is "spare". */
2580 return NULL;
2581 }
2582 }
2583
2584 /* If we restore SP from a register, assume this was the frame register.
2585 Otherwise just fall back to SP as frame register. */
2586 if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2587 cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2588 else
2589 cache->framereg = ARM_SP_REGNUM;
2590
2591 /* Determine offset to previous frame. */
2592 cache->framesize
2593 = vsp - get_frame_register_unsigned (this_frame, cache->framereg);
2594
2595 /* We already got the previous SP. */
2596 cache->prev_sp = vsp;
2597
2598 return cache;
2599}
2600
2601/* Unwinding via ARM exception table entries. Note that the sniffer
2602 already computes a filled-in prologue cache, which is then used
2603 with the same arm_prologue_this_id and arm_prologue_prev_register
2604 routines also used for prologue-parsing based unwinding. */
2605
2606static int
2607arm_exidx_unwind_sniffer (const struct frame_unwind *self,
2608 struct frame_info *this_frame,
2609 void **this_prologue_cache)
2610{
2611 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2612 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
2613 CORE_ADDR addr_in_block, exidx_region, func_start;
2614 struct arm_prologue_cache *cache;
2615 gdb_byte *entry;
2616
2617 /* See if we have an ARM exception table entry covering this address. */
2618 addr_in_block = get_frame_address_in_block (this_frame);
2619 entry = arm_find_exidx_entry (addr_in_block, &exidx_region);
2620 if (!entry)
2621 return 0;
2622
2623 /* The ARM exception table does not describe unwind information
2624 for arbitrary PC values, but is guaranteed to be correct only
2625 at call sites. We have to decide here whether we want to use
2626 ARM exception table information for this frame, or fall back
2627 to using prologue parsing. (Note that if we have DWARF CFI,
2628 this sniffer isn't even called -- CFI is always preferred.)
2629
2630 Before we make this decision, however, we check whether we
2631 actually have *symbol* information for the current frame.
2632 If not, prologue parsing would not work anyway, so we might
2633 as well use the exception table and hope for the best. */
2634 if (find_pc_partial_function (addr_in_block, NULL, &func_start, NULL))
2635 {
2636 int exc_valid = 0;
2637
2638 /* If the next frame is "normal", we are at a call site in this
2639 frame, so exception information is guaranteed to be valid. */
2640 if (get_next_frame (this_frame)
2641 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
2642 exc_valid = 1;
2643
2644 /* We also assume exception information is valid if we're currently
2645 blocked in a system call. The system library is supposed to
d9311bfa
AT
2646 ensure this, so that e.g. pthread cancellation works. */
2647 if (arm_frame_is_thumb (this_frame))
0e9e9abd 2648 {
7913a64c 2649 ULONGEST insn;
416dc9c6 2650
7913a64c
YQ
2651 if (safe_read_memory_unsigned_integer (get_frame_pc (this_frame) - 2,
2652 2, byte_order_for_code, &insn)
d9311bfa
AT
2653 && (insn & 0xff00) == 0xdf00 /* svc */)
2654 exc_valid = 1;
0e9e9abd 2655 }
d9311bfa
AT
2656 else
2657 {
7913a64c 2658 ULONGEST insn;
416dc9c6 2659
7913a64c
YQ
2660 if (safe_read_memory_unsigned_integer (get_frame_pc (this_frame) - 4,
2661 4, byte_order_for_code, &insn)
d9311bfa
AT
2662 && (insn & 0x0f000000) == 0x0f000000 /* svc */)
2663 exc_valid = 1;
2664 }
2665
0e9e9abd
UW
2666 /* Bail out if we don't know that exception information is valid. */
2667 if (!exc_valid)
2668 return 0;
2669
2670 /* The ARM exception index does not mark the *end* of the region
2671 covered by the entry, and some functions will not have any entry.
2672 To correctly recognize the end of the covered region, the linker
2673 should have inserted dummy records with a CANTUNWIND marker.
2674
2675 Unfortunately, current versions of GNU ld do not reliably do
2676 this, and thus we may have found an incorrect entry above.
2677 As a (temporary) sanity check, we only use the entry if it
2678 lies *within* the bounds of the function. Note that this check
2679 might reject perfectly valid entries that just happen to cover
2680 multiple functions; therefore this check ought to be removed
2681 once the linker is fixed. */
2682 if (func_start > exidx_region)
2683 return 0;
2684 }
2685
2686 /* Decode the list of unwinding instructions into a prologue cache.
2687 Note that this may fail due to e.g. a "refuse to unwind" code. */
2688 cache = arm_exidx_fill_cache (this_frame, entry);
2689 if (!cache)
2690 return 0;
2691
2692 *this_prologue_cache = cache;
2693 return 1;
2694}
2695
2696struct frame_unwind arm_exidx_unwind = {
2697 NORMAL_FRAME,
8fbca658 2698 default_frame_unwind_stop_reason,
0e9e9abd
UW
2699 arm_prologue_this_id,
2700 arm_prologue_prev_register,
2701 NULL,
2702 arm_exidx_unwind_sniffer
2703};
2704
779aa56f
YQ
2705static struct arm_prologue_cache *
2706arm_make_epilogue_frame_cache (struct frame_info *this_frame)
2707{
2708 struct arm_prologue_cache *cache;
779aa56f
YQ
2709 int reg;
2710
2711 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2712 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2713
2714 /* Still rely on the offset calculated from prologue. */
2715 arm_scan_prologue (this_frame, cache);
2716
2717 /* Since we are in epilogue, the SP has been restored. */
2718 cache->prev_sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
2719
2720 /* Calculate actual addresses of saved registers using offsets
2721 determined by arm_scan_prologue. */
2722 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
2723 if (trad_frame_addr_p (cache->saved_regs, reg))
2724 cache->saved_regs[reg].addr += cache->prev_sp;
2725
2726 return cache;
2727}
2728
2729/* Implementation of function hook 'this_id' in
2730 'struct frame_uwnind' for epilogue unwinder. */
2731
2732static void
2733arm_epilogue_frame_this_id (struct frame_info *this_frame,
2734 void **this_cache,
2735 struct frame_id *this_id)
2736{
2737 struct arm_prologue_cache *cache;
2738 CORE_ADDR pc, func;
2739
2740 if (*this_cache == NULL)
2741 *this_cache = arm_make_epilogue_frame_cache (this_frame);
2742 cache = (struct arm_prologue_cache *) *this_cache;
2743
2744 /* Use function start address as part of the frame ID. If we cannot
2745 identify the start address (due to missing symbol information),
2746 fall back to just using the current PC. */
2747 pc = get_frame_pc (this_frame);
2748 func = get_frame_func (this_frame);
fb3f3d25 2749 if (func == 0)
779aa56f
YQ
2750 func = pc;
2751
2752 (*this_id) = frame_id_build (cache->prev_sp, pc);
2753}
2754
2755/* Implementation of function hook 'prev_register' in
2756 'struct frame_uwnind' for epilogue unwinder. */
2757
2758static struct value *
2759arm_epilogue_frame_prev_register (struct frame_info *this_frame,
2760 void **this_cache, int regnum)
2761{
779aa56f
YQ
2762 if (*this_cache == NULL)
2763 *this_cache = arm_make_epilogue_frame_cache (this_frame);
779aa56f
YQ
2764
2765 return arm_prologue_prev_register (this_frame, this_cache, regnum);
2766}
2767
2768static int arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch,
2769 CORE_ADDR pc);
2770static int thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch,
2771 CORE_ADDR pc);
2772
2773/* Implementation of function hook 'sniffer' in
2774 'struct frame_uwnind' for epilogue unwinder. */
2775
2776static int
2777arm_epilogue_frame_sniffer (const struct frame_unwind *self,
2778 struct frame_info *this_frame,
2779 void **this_prologue_cache)
2780{
2781 if (frame_relative_level (this_frame) == 0)
2782 {
2783 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2784 CORE_ADDR pc = get_frame_pc (this_frame);
2785
2786 if (arm_frame_is_thumb (this_frame))
2787 return thumb_stack_frame_destroyed_p (gdbarch, pc);
2788 else
2789 return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
2790 }
2791 else
2792 return 0;
2793}
2794
2795/* Frame unwinder from epilogue. */
2796
2797static const struct frame_unwind arm_epilogue_frame_unwind =
2798{
2799 NORMAL_FRAME,
2800 default_frame_unwind_stop_reason,
2801 arm_epilogue_frame_this_id,
2802 arm_epilogue_frame_prev_register,
2803 NULL,
2804 arm_epilogue_frame_sniffer,
2805};
2806
80d8d390
YQ
2807/* Recognize GCC's trampoline for thumb call-indirect. If we are in a
2808 trampoline, return the target PC. Otherwise return 0.
2809
2810 void call0a (char c, short s, int i, long l) {}
2811
2812 int main (void)
2813 {
2814 (*pointer_to_call0a) (c, s, i, l);
2815 }
2816
2817 Instead of calling a stub library function _call_via_xx (xx is
2818 the register name), GCC may inline the trampoline in the object
2819 file as below (register r2 has the address of call0a).
2820
2821 .global main
2822 .type main, %function
2823 ...
2824 bl .L1
2825 ...
2826 .size main, .-main
2827
2828 .L1:
2829 bx r2
2830
2831 The trampoline 'bx r2' doesn't belong to main. */
2832
2833static CORE_ADDR
2834arm_skip_bx_reg (struct frame_info *frame, CORE_ADDR pc)
2835{
2836 /* The heuristics of recognizing such trampoline is that FRAME is
2837 executing in Thumb mode and the instruction on PC is 'bx Rm'. */
2838 if (arm_frame_is_thumb (frame))
2839 {
2840 gdb_byte buf[2];
2841
2842 if (target_read_memory (pc, buf, 2) == 0)
2843 {
2844 struct gdbarch *gdbarch = get_frame_arch (frame);
2845 enum bfd_endian byte_order_for_code
2846 = gdbarch_byte_order_for_code (gdbarch);
2847 uint16_t insn
2848 = extract_unsigned_integer (buf, 2, byte_order_for_code);
2849
2850 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
2851 {
2852 CORE_ADDR dest
2853 = get_frame_register_unsigned (frame, bits (insn, 3, 6));
2854
2855 /* Clear the LSB so that gdb core sets step-resume
2856 breakpoint at the right address. */
2857 return UNMAKE_THUMB_ADDR (dest);
2858 }
2859 }
2860 }
2861
2862 return 0;
2863}
2864
909cf6ea 2865static struct arm_prologue_cache *
a262aec2 2866arm_make_stub_cache (struct frame_info *this_frame)
909cf6ea 2867{
909cf6ea 2868 struct arm_prologue_cache *cache;
909cf6ea 2869
35d5d4ee 2870 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
a262aec2 2871 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
909cf6ea 2872
a262aec2 2873 cache->prev_sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
909cf6ea
DJ
2874
2875 return cache;
2876}
2877
2878/* Our frame ID for a stub frame is the current SP and LR. */
2879
2880static void
a262aec2 2881arm_stub_this_id (struct frame_info *this_frame,
909cf6ea
DJ
2882 void **this_cache,
2883 struct frame_id *this_id)
2884{
2885 struct arm_prologue_cache *cache;
2886
2887 if (*this_cache == NULL)
a262aec2 2888 *this_cache = arm_make_stub_cache (this_frame);
9a3c8263 2889 cache = (struct arm_prologue_cache *) *this_cache;
909cf6ea 2890
a262aec2 2891 *this_id = frame_id_build (cache->prev_sp, get_frame_pc (this_frame));
909cf6ea
DJ
2892}
2893
a262aec2
DJ
2894static int
2895arm_stub_unwind_sniffer (const struct frame_unwind *self,
2896 struct frame_info *this_frame,
2897 void **this_prologue_cache)
909cf6ea 2898{
93d42b30 2899 CORE_ADDR addr_in_block;
948f8e3d 2900 gdb_byte dummy[4];
18d18ac8
YQ
2901 CORE_ADDR pc, start_addr;
2902 const char *name;
909cf6ea 2903
a262aec2 2904 addr_in_block = get_frame_address_in_block (this_frame);
18d18ac8 2905 pc = get_frame_pc (this_frame);
3e5d3a5a 2906 if (in_plt_section (addr_in_block)
fc36e839
DE
2907 /* We also use the stub winder if the target memory is unreadable
2908 to avoid having the prologue unwinder trying to read it. */
18d18ac8
YQ
2909 || target_read_memory (pc, dummy, 4) != 0)
2910 return 1;
2911
2912 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0
2913 && arm_skip_bx_reg (this_frame, pc) != 0)
a262aec2 2914 return 1;
909cf6ea 2915
a262aec2 2916 return 0;
909cf6ea
DJ
2917}
2918
a262aec2
DJ
2919struct frame_unwind arm_stub_unwind = {
2920 NORMAL_FRAME,
8fbca658 2921 default_frame_unwind_stop_reason,
a262aec2
DJ
2922 arm_stub_this_id,
2923 arm_prologue_prev_register,
2924 NULL,
2925 arm_stub_unwind_sniffer
2926};
2927
2ae28aa9
YQ
2928/* Put here the code to store, into CACHE->saved_regs, the addresses
2929 of the saved registers of frame described by THIS_FRAME. CACHE is
2930 returned. */
2931
2932static struct arm_prologue_cache *
2933arm_m_exception_cache (struct frame_info *this_frame)
2934{
2935 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2936 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2937 struct arm_prologue_cache *cache;
ba8b04a8
FH
2938 CORE_ADDR lr;
2939 CORE_ADDR sp;
2ae28aa9
YQ
2940 CORE_ADDR unwound_sp;
2941 LONGEST xpsr;
ba8b04a8
FH
2942 uint32_t exc_return;
2943 uint32_t process_stack_used;
2944 uint32_t extended_frame_used;
2945 uint32_t secure_stack_used;
2ae28aa9
YQ
2946
2947 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2948 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2949
ba8b04a8
FH
2950 /* ARMv7-M Architecture Reference "B1.5.6 Exception entry behavior"
2951 describes which bits in LR that define which stack was used prior
2952 to the exception and if FPU is used (causing extended stack frame). */
2953
2954 lr = get_frame_register_unsigned (this_frame, ARM_LR_REGNUM);
2955 sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
2956
2957 /* Check EXC_RETURN indicator bits. */
2958 exc_return = (((lr >> 28) & 0xf) == 0xf);
2959
2960 /* Check EXC_RETURN bit SPSEL if Main or Thread (process) stack used. */
2961 process_stack_used = ((lr & (1 << 2)) != 0);
2962 if (exc_return && process_stack_used)
2963 {
2964 /* Thread (process) stack used.
2965 Potentially this could be other register defined by target, but PSP
2966 can be considered a standard name for the "Process Stack Pointer".
2967 To be fully aware of system registers like MSP and PSP, these could
2968 be added to a separate XML arm-m-system-profile that is valid for
2969 ARMv6-M and ARMv7-M architectures. Also to be able to debug eg a
2970 corefile off-line, then these registers must be defined by GDB,
2971 and also be included in the corefile regsets. */
2972
2973 int psp_regnum = user_reg_map_name_to_regnum (gdbarch, "psp", -1);
2974 if (psp_regnum == -1)
2975 {
2976 /* Thread (process) stack could not be fetched,
2977 give warning and exit. */
2978
2979 warning (_("no PSP thread stack unwinding supported."));
2980
2981 /* Terminate any further stack unwinding by refer to self. */
2982 cache->prev_sp = sp;
2983 return cache;
2984 }
2985 else
2986 {
2987 /* Thread (process) stack used, use PSP as SP. */
2988 unwound_sp = get_frame_register_unsigned (this_frame, psp_regnum);
2989 }
2990 }
2991 else
2992 {
2993 /* Main stack used, use MSP as SP. */
2994 unwound_sp = sp;
2995 }
2ae28aa9
YQ
2996
2997 /* The hardware saves eight 32-bit words, comprising xPSR,
2998 ReturnAddress, LR (R14), R12, R3, R2, R1, R0. See details in
2999 "B1.5.6 Exception entry behavior" in
3000 "ARMv7-M Architecture Reference Manual". */
3001 cache->saved_regs[0].addr = unwound_sp;
3002 cache->saved_regs[1].addr = unwound_sp + 4;
3003 cache->saved_regs[2].addr = unwound_sp + 8;
3004 cache->saved_regs[3].addr = unwound_sp + 12;
ba8b04a8
FH
3005 cache->saved_regs[ARM_IP_REGNUM].addr = unwound_sp + 16;
3006 cache->saved_regs[ARM_LR_REGNUM].addr = unwound_sp + 20;
3007 cache->saved_regs[ARM_PC_REGNUM].addr = unwound_sp + 24;
2ae28aa9
YQ
3008 cache->saved_regs[ARM_PS_REGNUM].addr = unwound_sp + 28;
3009
ba8b04a8
FH
3010 /* Check EXC_RETURN bit FTYPE if extended stack frame (FPU regs stored)
3011 type used. */
3012 extended_frame_used = ((lr & (1 << 4)) == 0);
3013 if (exc_return && extended_frame_used)
3014 {
3015 int i;
3016 int fpu_regs_stack_offset;
3017
3018 /* This code does not take into account the lazy stacking, see "Lazy
3019 context save of FP state", in B1.5.7, also ARM AN298, supported
3020 by Cortex-M4F architecture.
3021 To fully handle this the FPCCR register (Floating-point Context
3022 Control Register) needs to be read out and the bits ASPEN and LSPEN
3023 could be checked to setup correct lazy stacked FP registers.
3024 This register is located at address 0xE000EF34. */
3025
3026 /* Extended stack frame type used. */
3027 fpu_regs_stack_offset = unwound_sp + 0x20;
3028 for (i = 0; i < 16; i++)
3029 {
3030 cache->saved_regs[ARM_D0_REGNUM + i].addr = fpu_regs_stack_offset;
3031 fpu_regs_stack_offset += 4;
3032 }
3033 cache->saved_regs[ARM_FPSCR_REGNUM].addr = unwound_sp + 0x60;
3034
3035 /* Offset 0x64 is reserved. */
3036 cache->prev_sp = unwound_sp + 0x68;
3037 }
3038 else
3039 {
3040 /* Standard stack frame type used. */
3041 cache->prev_sp = unwound_sp + 0x20;
3042 }
3043
3044 /* Check EXC_RETURN bit S if Secure or Non-secure stack used. */
3045 secure_stack_used = ((lr & (1 << 6)) != 0);
3046 if (exc_return && secure_stack_used)
3047 {
3048 /* ARMv8-M Exception and interrupt handling is not considered here.
3049 In the ARMv8-M architecture also EXC_RETURN bit S is controlling if
3050 the Secure or Non-secure stack was used. To separate Secure and
3051 Non-secure stacks, processors that are based on the ARMv8-M
3052 architecture support 4 stack pointers: MSP_S, PSP_S, MSP_NS, PSP_NS.
3053 In addition, a stack limit feature is provided using stack limit
3054 registers (accessible using MSR and MRS instructions) in Privileged
3055 level. */
3056 }
3057
2ae28aa9
YQ
3058 /* If bit 9 of the saved xPSR is set, then there is a four-byte
3059 aligner between the top of the 32-byte stack frame and the
3060 previous context's stack pointer. */
2ae28aa9
YQ
3061 if (safe_read_memory_integer (unwound_sp + 28, 4, byte_order, &xpsr)
3062 && (xpsr & (1 << 9)) != 0)
3063 cache->prev_sp += 4;
3064
3065 return cache;
3066}
3067
3068/* Implementation of function hook 'this_id' in
3069 'struct frame_uwnind'. */
3070
3071static void
3072arm_m_exception_this_id (struct frame_info *this_frame,
3073 void **this_cache,
3074 struct frame_id *this_id)
3075{
3076 struct arm_prologue_cache *cache;
3077
3078 if (*this_cache == NULL)
3079 *this_cache = arm_m_exception_cache (this_frame);
9a3c8263 3080 cache = (struct arm_prologue_cache *) *this_cache;
2ae28aa9
YQ
3081
3082 /* Our frame ID for a stub frame is the current SP and LR. */
3083 *this_id = frame_id_build (cache->prev_sp,
3084 get_frame_pc (this_frame));
3085}
3086
3087/* Implementation of function hook 'prev_register' in
3088 'struct frame_uwnind'. */
3089
3090static struct value *
3091arm_m_exception_prev_register (struct frame_info *this_frame,
3092 void **this_cache,
3093 int prev_regnum)
3094{
2ae28aa9
YQ
3095 struct arm_prologue_cache *cache;
3096
3097 if (*this_cache == NULL)
3098 *this_cache = arm_m_exception_cache (this_frame);
9a3c8263 3099 cache = (struct arm_prologue_cache *) *this_cache;
2ae28aa9
YQ
3100
3101 /* The value was already reconstructed into PREV_SP. */
3102 if (prev_regnum == ARM_SP_REGNUM)
3103 return frame_unwind_got_constant (this_frame, prev_regnum,
3104 cache->prev_sp);
3105
3106 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
3107 prev_regnum);
3108}
3109
3110/* Implementation of function hook 'sniffer' in
3111 'struct frame_uwnind'. */
3112
3113static int
3114arm_m_exception_unwind_sniffer (const struct frame_unwind *self,
3115 struct frame_info *this_frame,
3116 void **this_prologue_cache)
3117{
3118 CORE_ADDR this_pc = get_frame_pc (this_frame);
3119
3120 /* No need to check is_m; this sniffer is only registered for
3121 M-profile architectures. */
3122
ca90e760
FH
3123 /* Check if exception frame returns to a magic PC value. */
3124 return arm_m_addr_is_magic (this_pc);
2ae28aa9
YQ
3125}
3126
3127/* Frame unwinder for M-profile exceptions. */
3128
3129struct frame_unwind arm_m_exception_unwind =
3130{
3131 SIGTRAMP_FRAME,
3132 default_frame_unwind_stop_reason,
3133 arm_m_exception_this_id,
3134 arm_m_exception_prev_register,
3135 NULL,
3136 arm_m_exception_unwind_sniffer
3137};
3138
24de872b 3139static CORE_ADDR
a262aec2 3140arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
24de872b
DJ
3141{
3142 struct arm_prologue_cache *cache;
3143
eb5492fa 3144 if (*this_cache == NULL)
a262aec2 3145 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 3146 cache = (struct arm_prologue_cache *) *this_cache;
eb5492fa 3147
4be43953 3148 return cache->prev_sp - cache->framesize;
24de872b
DJ
3149}
3150
eb5492fa
DJ
3151struct frame_base arm_normal_base = {
3152 &arm_prologue_unwind,
3153 arm_normal_frame_base,
3154 arm_normal_frame_base,
3155 arm_normal_frame_base
3156};
3157
b39cc962
DJ
3158static struct value *
3159arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
3160 int regnum)
3161{
24568a2c 3162 struct gdbarch * gdbarch = get_frame_arch (this_frame);
b39cc962 3163 CORE_ADDR lr, cpsr;
9779414d 3164 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
b39cc962
DJ
3165
3166 switch (regnum)
3167 {
3168 case ARM_PC_REGNUM:
3169 /* The PC is normally copied from the return column, which
3170 describes saves of LR. However, that version may have an
3171 extra bit set to indicate Thumb state. The bit is not
3172 part of the PC. */
3173 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3174 return frame_unwind_got_constant (this_frame, regnum,
24568a2c 3175 arm_addr_bits_remove (gdbarch, lr));
b39cc962
DJ
3176
3177 case ARM_PS_REGNUM:
3178 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
ca38c58e 3179 cpsr = get_frame_register_unsigned (this_frame, regnum);
b39cc962
DJ
3180 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3181 if (IS_THUMB_ADDR (lr))
9779414d 3182 cpsr |= t_bit;
b39cc962 3183 else
9779414d 3184 cpsr &= ~t_bit;
ca38c58e 3185 return frame_unwind_got_constant (this_frame, regnum, cpsr);
b39cc962
DJ
3186
3187 default:
3188 internal_error (__FILE__, __LINE__,
3189 _("Unexpected register %d"), regnum);
3190 }
3191}
3192
3193static void
3194arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
3195 struct dwarf2_frame_state_reg *reg,
3196 struct frame_info *this_frame)
3197{
3198 switch (regnum)
3199 {
3200 case ARM_PC_REGNUM:
3201 case ARM_PS_REGNUM:
3202 reg->how = DWARF2_FRAME_REG_FN;
3203 reg->loc.fn = arm_dwarf2_prev_register;
3204 break;
3205 case ARM_SP_REGNUM:
3206 reg->how = DWARF2_FRAME_REG_CFA;
3207 break;
3208 }
3209}
3210
c9cf6e20 3211/* Implement the stack_frame_destroyed_p gdbarch method. */
4024ca99
UW
3212
3213static int
c9cf6e20 3214thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
4024ca99
UW
3215{
3216 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3217 unsigned int insn, insn2;
3218 int found_return = 0, found_stack_adjust = 0;
3219 CORE_ADDR func_start, func_end;
3220 CORE_ADDR scan_pc;
3221 gdb_byte buf[4];
3222
3223 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3224 return 0;
3225
3226 /* The epilogue is a sequence of instructions along the following lines:
3227
3228 - add stack frame size to SP or FP
3229 - [if frame pointer used] restore SP from FP
3230 - restore registers from SP [may include PC]
3231 - a return-type instruction [if PC wasn't already restored]
3232
3233 In a first pass, we scan forward from the current PC and verify the
3234 instructions we find as compatible with this sequence, ending in a
3235 return instruction.
3236
3237 However, this is not sufficient to distinguish indirect function calls
3238 within a function from indirect tail calls in the epilogue in some cases.
3239 Therefore, if we didn't already find any SP-changing instruction during
3240 forward scan, we add a backward scanning heuristic to ensure we actually
3241 are in the epilogue. */
3242
3243 scan_pc = pc;
3244 while (scan_pc < func_end && !found_return)
3245 {
3246 if (target_read_memory (scan_pc, buf, 2))
3247 break;
3248
3249 scan_pc += 2;
3250 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3251
3252 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
3253 found_return = 1;
3254 else if (insn == 0x46f7) /* mov pc, lr */
3255 found_return = 1;
540314bd 3256 else if (thumb_instruction_restores_sp (insn))
4024ca99 3257 {
b7576e5c 3258 if ((insn & 0xff00) == 0xbd00) /* pop <registers, PC> */
4024ca99
UW
3259 found_return = 1;
3260 }
db24da6d 3261 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instruction */
4024ca99
UW
3262 {
3263 if (target_read_memory (scan_pc, buf, 2))
3264 break;
3265
3266 scan_pc += 2;
3267 insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3268
3269 if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3270 {
4024ca99
UW
3271 if (insn2 & 0x8000) /* <registers> include PC. */
3272 found_return = 1;
3273 }
3274 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3275 && (insn2 & 0x0fff) == 0x0b04)
3276 {
4024ca99
UW
3277 if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC. */
3278 found_return = 1;
3279 }
3280 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3281 && (insn2 & 0x0e00) == 0x0a00)
6b65d1b6 3282 ;
4024ca99
UW
3283 else
3284 break;
3285 }
3286 else
3287 break;
3288 }
3289
3290 if (!found_return)
3291 return 0;
3292
3293 /* Since any instruction in the epilogue sequence, with the possible
3294 exception of return itself, updates the stack pointer, we need to
3295 scan backwards for at most one instruction. Try either a 16-bit or
3296 a 32-bit instruction. This is just a heuristic, so we do not worry
0963b4bd 3297 too much about false positives. */
4024ca99 3298
6b65d1b6
YQ
3299 if (pc - 4 < func_start)
3300 return 0;
3301 if (target_read_memory (pc - 4, buf, 4))
3302 return 0;
4024ca99 3303
6b65d1b6
YQ
3304 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3305 insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);
3306
3307 if (thumb_instruction_restores_sp (insn2))
3308 found_stack_adjust = 1;
3309 else if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3310 found_stack_adjust = 1;
3311 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3312 && (insn2 & 0x0fff) == 0x0b04)
3313 found_stack_adjust = 1;
3314 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3315 && (insn2 & 0x0e00) == 0x0a00)
3316 found_stack_adjust = 1;
4024ca99
UW
3317
3318 return found_stack_adjust;
3319}
3320
4024ca99 3321static int
c58b006a 3322arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch, CORE_ADDR pc)
4024ca99
UW
3323{
3324 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3325 unsigned int insn;
f303bc3e 3326 int found_return;
4024ca99
UW
3327 CORE_ADDR func_start, func_end;
3328
4024ca99
UW
3329 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3330 return 0;
3331
3332 /* We are in the epilogue if the previous instruction was a stack
3333 adjustment and the next instruction is a possible return (bx, mov
3334 pc, or pop). We could have to scan backwards to find the stack
3335 adjustment, or forwards to find the return, but this is a decent
3336 approximation. First scan forwards. */
3337
3338 found_return = 0;
3339 insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
3340 if (bits (insn, 28, 31) != INST_NV)
3341 {
3342 if ((insn & 0x0ffffff0) == 0x012fff10)
3343 /* BX. */
3344 found_return = 1;
3345 else if ((insn & 0x0ffffff0) == 0x01a0f000)
3346 /* MOV PC. */
3347 found_return = 1;
3348 else if ((insn & 0x0fff0000) == 0x08bd0000
3349 && (insn & 0x0000c000) != 0)
3350 /* POP (LDMIA), including PC or LR. */
3351 found_return = 1;
3352 }
3353
3354 if (!found_return)
3355 return 0;
3356
3357 /* Scan backwards. This is just a heuristic, so do not worry about
3358 false positives from mode changes. */
3359
3360 if (pc < func_start + 4)
3361 return 0;
3362
3363 insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
f303bc3e 3364 if (arm_instruction_restores_sp (insn))
4024ca99
UW
3365 return 1;
3366
3367 return 0;
3368}
3369
c58b006a
YQ
3370/* Implement the stack_frame_destroyed_p gdbarch method. */
3371
3372static int
3373arm_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3374{
3375 if (arm_pc_is_thumb (gdbarch, pc))
3376 return thumb_stack_frame_destroyed_p (gdbarch, pc);
3377 else
3378 return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
3379}
4024ca99 3380
2dd604e7
RE
3381/* When arguments must be pushed onto the stack, they go on in reverse
3382 order. The code below implements a FILO (stack) to do this. */
3383
3384struct stack_item
3385{
3386 int len;
3387 struct stack_item *prev;
7c543f7b 3388 gdb_byte *data;
2dd604e7
RE
3389};
3390
3391static struct stack_item *
df3b6708 3392push_stack_item (struct stack_item *prev, const gdb_byte *contents, int len)
2dd604e7
RE
3393{
3394 struct stack_item *si;
8d749320 3395 si = XNEW (struct stack_item);
7c543f7b 3396 si->data = (gdb_byte *) xmalloc (len);
2dd604e7
RE
3397 si->len = len;
3398 si->prev = prev;
3399 memcpy (si->data, contents, len);
3400 return si;
3401}
3402
3403static struct stack_item *
3404pop_stack_item (struct stack_item *si)
3405{
3406 struct stack_item *dead = si;
3407 si = si->prev;
3408 xfree (dead->data);
3409 xfree (dead);
3410 return si;
3411}
3412
030197b4
AB
3413/* Implement the gdbarch type alignment method, overrides the generic
3414 alignment algorithm for anything that is arm specific. */
2af48f68 3415
030197b4
AB
3416static ULONGEST
3417arm_type_align (gdbarch *gdbarch, struct type *t)
2af48f68 3418{
2af48f68 3419 t = check_typedef (t);
78134374 3420 if (t->code () == TYPE_CODE_ARRAY && TYPE_VECTOR (t))
2af48f68 3421 {
030197b4
AB
3422 /* Use the natural alignment for vector types (the same for
3423 scalar type), but the maximum alignment is 64-bit. */
3424 if (TYPE_LENGTH (t) > 8)
3425 return 8;
c4312b19 3426 else
030197b4 3427 return TYPE_LENGTH (t);
2af48f68 3428 }
030197b4
AB
3429
3430 /* Allow the common code to calculate the alignment. */
3431 return 0;
2af48f68
PB
3432}
3433
90445bd3
DJ
3434/* Possible base types for a candidate for passing and returning in
3435 VFP registers. */
3436
3437enum arm_vfp_cprc_base_type
3438{
3439 VFP_CPRC_UNKNOWN,
3440 VFP_CPRC_SINGLE,
3441 VFP_CPRC_DOUBLE,
3442 VFP_CPRC_VEC64,
3443 VFP_CPRC_VEC128
3444};
3445
3446/* The length of one element of base type B. */
3447
3448static unsigned
3449arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
3450{
3451 switch (b)
3452 {
3453 case VFP_CPRC_SINGLE:
3454 return 4;
3455 case VFP_CPRC_DOUBLE:
3456 return 8;
3457 case VFP_CPRC_VEC64:
3458 return 8;
3459 case VFP_CPRC_VEC128:
3460 return 16;
3461 default:
3462 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3463 (int) b);
3464 }
3465}
3466
3467/* The character ('s', 'd' or 'q') for the type of VFP register used
3468 for passing base type B. */
3469
3470static int
3471arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
3472{
3473 switch (b)
3474 {
3475 case VFP_CPRC_SINGLE:
3476 return 's';
3477 case VFP_CPRC_DOUBLE:
3478 return 'd';
3479 case VFP_CPRC_VEC64:
3480 return 'd';
3481 case VFP_CPRC_VEC128:
3482 return 'q';
3483 default:
3484 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3485 (int) b);
3486 }
3487}
3488
3489/* Determine whether T may be part of a candidate for passing and
3490 returning in VFP registers, ignoring the limit on the total number
3491 of components. If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
3492 classification of the first valid component found; if it is not
3493 VFP_CPRC_UNKNOWN, all components must have the same classification
3494 as *BASE_TYPE. If it is found that T contains a type not permitted
3495 for passing and returning in VFP registers, a type differently
3496 classified from *BASE_TYPE, or two types differently classified
3497 from each other, return -1, otherwise return the total number of
3498 base-type elements found (possibly 0 in an empty structure or
817e0957
YQ
3499 array). Vector types are not currently supported, matching the
3500 generic AAPCS support. */
90445bd3
DJ
3501
3502static int
3503arm_vfp_cprc_sub_candidate (struct type *t,
3504 enum arm_vfp_cprc_base_type *base_type)
3505{
3506 t = check_typedef (t);
78134374 3507 switch (t->code ())
90445bd3
DJ
3508 {
3509 case TYPE_CODE_FLT:
3510 switch (TYPE_LENGTH (t))
3511 {
3512 case 4:
3513 if (*base_type == VFP_CPRC_UNKNOWN)
3514 *base_type = VFP_CPRC_SINGLE;
3515 else if (*base_type != VFP_CPRC_SINGLE)
3516 return -1;
3517 return 1;
3518
3519 case 8:
3520 if (*base_type == VFP_CPRC_UNKNOWN)
3521 *base_type = VFP_CPRC_DOUBLE;
3522 else if (*base_type != VFP_CPRC_DOUBLE)
3523 return -1;
3524 return 1;
3525
3526 default:
3527 return -1;
3528 }
3529 break;
3530
817e0957
YQ
3531 case TYPE_CODE_COMPLEX:
3532 /* Arguments of complex T where T is one of the types float or
3533 double get treated as if they are implemented as:
3534
3535 struct complexT
3536 {
3537 T real;
3538 T imag;
5f52445b
YQ
3539 };
3540
3541 */
817e0957
YQ
3542 switch (TYPE_LENGTH (t))
3543 {
3544 case 8:
3545 if (*base_type == VFP_CPRC_UNKNOWN)
3546 *base_type = VFP_CPRC_SINGLE;
3547 else if (*base_type != VFP_CPRC_SINGLE)
3548 return -1;
3549 return 2;
3550
3551 case 16:
3552 if (*base_type == VFP_CPRC_UNKNOWN)
3553 *base_type = VFP_CPRC_DOUBLE;
3554 else if (*base_type != VFP_CPRC_DOUBLE)
3555 return -1;
3556 return 2;
3557
3558 default:
3559 return -1;
3560 }
3561 break;
3562
90445bd3
DJ
3563 case TYPE_CODE_ARRAY:
3564 {
c4312b19 3565 if (TYPE_VECTOR (t))
90445bd3 3566 {
c4312b19
YQ
3567 /* A 64-bit or 128-bit containerized vector type are VFP
3568 CPRCs. */
3569 switch (TYPE_LENGTH (t))
3570 {
3571 case 8:
3572 if (*base_type == VFP_CPRC_UNKNOWN)
3573 *base_type = VFP_CPRC_VEC64;
3574 return 1;
3575 case 16:
3576 if (*base_type == VFP_CPRC_UNKNOWN)
3577 *base_type = VFP_CPRC_VEC128;
3578 return 1;
3579 default:
3580 return -1;
3581 }
3582 }
3583 else
3584 {
3585 int count;
3586 unsigned unitlen;
3587
3588 count = arm_vfp_cprc_sub_candidate (TYPE_TARGET_TYPE (t),
3589 base_type);
3590 if (count == -1)
3591 return -1;
3592 if (TYPE_LENGTH (t) == 0)
3593 {
3594 gdb_assert (count == 0);
3595 return 0;
3596 }
3597 else if (count == 0)
3598 return -1;
3599 unitlen = arm_vfp_cprc_unit_length (*base_type);
3600 gdb_assert ((TYPE_LENGTH (t) % unitlen) == 0);
3601 return TYPE_LENGTH (t) / unitlen;
90445bd3 3602 }
90445bd3
DJ
3603 }
3604 break;
3605
3606 case TYPE_CODE_STRUCT:
3607 {
3608 int count = 0;
3609 unsigned unitlen;
3610 int i;
1f704f76 3611 for (i = 0; i < t->num_fields (); i++)
90445bd3 3612 {
1040b979
YQ
3613 int sub_count = 0;
3614
ceacbf6e 3615 if (!field_is_static (&t->field (i)))
940da03e 3616 sub_count = arm_vfp_cprc_sub_candidate (t->field (i).type (),
1040b979 3617 base_type);
90445bd3
DJ
3618 if (sub_count == -1)
3619 return -1;
3620 count += sub_count;
3621 }
3622 if (TYPE_LENGTH (t) == 0)
3623 {
3624 gdb_assert (count == 0);
3625 return 0;
3626 }
3627 else if (count == 0)
3628 return -1;
3629 unitlen = arm_vfp_cprc_unit_length (*base_type);
3630 if (TYPE_LENGTH (t) != unitlen * count)
3631 return -1;
3632 return count;
3633 }
3634
3635 case TYPE_CODE_UNION:
3636 {
3637 int count = 0;
3638 unsigned unitlen;
3639 int i;
1f704f76 3640 for (i = 0; i < t->num_fields (); i++)
90445bd3 3641 {
940da03e 3642 int sub_count = arm_vfp_cprc_sub_candidate (t->field (i).type (),
90445bd3
DJ
3643 base_type);
3644 if (sub_count == -1)
3645 return -1;
3646 count = (count > sub_count ? count : sub_count);
3647 }
3648 if (TYPE_LENGTH (t) == 0)
3649 {
3650 gdb_assert (count == 0);
3651 return 0;
3652 }
3653 else if (count == 0)
3654 return -1;
3655 unitlen = arm_vfp_cprc_unit_length (*base_type);
3656 if (TYPE_LENGTH (t) != unitlen * count)
3657 return -1;
3658 return count;
3659 }
3660
3661 default:
3662 break;
3663 }
3664
3665 return -1;
3666}
3667
3668/* Determine whether T is a VFP co-processor register candidate (CPRC)
3669 if passed to or returned from a non-variadic function with the VFP
3670 ABI in effect. Return 1 if it is, 0 otherwise. If it is, set
3671 *BASE_TYPE to the base type for T and *COUNT to the number of
3672 elements of that base type before returning. */
3673
3674static int
3675arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
3676 int *count)
3677{
3678 enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
3679 int c = arm_vfp_cprc_sub_candidate (t, &b);
3680 if (c <= 0 || c > 4)
3681 return 0;
3682 *base_type = b;
3683 *count = c;
3684 return 1;
3685}
3686
3687/* Return 1 if the VFP ABI should be used for passing arguments to and
3688 returning values from a function of type FUNC_TYPE, 0
3689 otherwise. */
3690
3691static int
3692arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
3693{
3694 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3695 /* Variadic functions always use the base ABI. Assume that functions
3696 without debug info are not variadic. */
3697 if (func_type && TYPE_VARARGS (check_typedef (func_type)))
3698 return 0;
3699 /* The VFP ABI is only supported as a variant of AAPCS. */
3700 if (tdep->arm_abi != ARM_ABI_AAPCS)
3701 return 0;
3702 return gdbarch_tdep (gdbarch)->fp_model == ARM_FLOAT_VFP;
3703}
3704
3705/* We currently only support passing parameters in integer registers, which
3706 conforms with GCC's default model, and VFP argument passing following
3707 the VFP variant of AAPCS. Several other variants exist and
2dd604e7
RE
3708 we should probably support some of them based on the selected ABI. */
3709
3710static CORE_ADDR
7d9b040b 3711arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6a65450a 3712 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
cf84fa6b
AH
3713 struct value **args, CORE_ADDR sp,
3714 function_call_return_method return_method,
6a65450a 3715 CORE_ADDR struct_addr)
2dd604e7 3716{
e17a4113 3717 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2dd604e7
RE
3718 int argnum;
3719 int argreg;
3720 int nstack;
3721 struct stack_item *si = NULL;
90445bd3
DJ
3722 int use_vfp_abi;
3723 struct type *ftype;
3724 unsigned vfp_regs_free = (1 << 16) - 1;
3725
3726 /* Determine the type of this function and whether the VFP ABI
3727 applies. */
3728 ftype = check_typedef (value_type (function));
78134374 3729 if (ftype->code () == TYPE_CODE_PTR)
90445bd3
DJ
3730 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
3731 use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
2dd604e7 3732
6a65450a
AC
3733 /* Set the return address. For the ARM, the return breakpoint is
3734 always at BP_ADDR. */
9779414d 3735 if (arm_pc_is_thumb (gdbarch, bp_addr))
9dca5578 3736 bp_addr |= 1;
6a65450a 3737 regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
2dd604e7
RE
3738
3739 /* Walk through the list of args and determine how large a temporary
3740 stack is required. Need to take care here as structs may be
7a9dd1b2 3741 passed on the stack, and we have to push them. */
2dd604e7
RE
3742 nstack = 0;
3743
3744 argreg = ARM_A1_REGNUM;
3745 nstack = 0;
3746
2dd604e7
RE
3747 /* The struct_return pointer occupies the first parameter
3748 passing register. */
cf84fa6b 3749 if (return_method == return_method_struct)
2dd604e7
RE
3750 {
3751 if (arm_debug)
5af949e3 3752 fprintf_unfiltered (gdb_stdlog, "struct return in %s = %s\n",
2af46ca0 3753 gdbarch_register_name (gdbarch, argreg),
5af949e3 3754 paddress (gdbarch, struct_addr));
2dd604e7
RE
3755 regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
3756 argreg++;
3757 }
3758
3759 for (argnum = 0; argnum < nargs; argnum++)
3760 {
3761 int len;
3762 struct type *arg_type;
3763 struct type *target_type;
3764 enum type_code typecode;
8c6363cf 3765 const bfd_byte *val;
2af48f68 3766 int align;
90445bd3
DJ
3767 enum arm_vfp_cprc_base_type vfp_base_type;
3768 int vfp_base_count;
3769 int may_use_core_reg = 1;
2dd604e7 3770
df407dfe 3771 arg_type = check_typedef (value_type (args[argnum]));
2dd604e7
RE
3772 len = TYPE_LENGTH (arg_type);
3773 target_type = TYPE_TARGET_TYPE (arg_type);
78134374 3774 typecode = arg_type->code ();
8c6363cf 3775 val = value_contents (args[argnum]);
2dd604e7 3776
030197b4 3777 align = type_align (arg_type);
2af48f68 3778 /* Round alignment up to a whole number of words. */
f0452268
AH
3779 align = (align + ARM_INT_REGISTER_SIZE - 1)
3780 & ~(ARM_INT_REGISTER_SIZE - 1);
2af48f68
PB
3781 /* Different ABIs have different maximum alignments. */
3782 if (gdbarch_tdep (gdbarch)->arm_abi == ARM_ABI_APCS)
3783 {
3784 /* The APCS ABI only requires word alignment. */
f0452268 3785 align = ARM_INT_REGISTER_SIZE;
2af48f68
PB
3786 }
3787 else
3788 {
3789 /* The AAPCS requires at most doubleword alignment. */
f0452268
AH
3790 if (align > ARM_INT_REGISTER_SIZE * 2)
3791 align = ARM_INT_REGISTER_SIZE * 2;
2af48f68
PB
3792 }
3793
90445bd3
DJ
3794 if (use_vfp_abi
3795 && arm_vfp_call_candidate (arg_type, &vfp_base_type,
3796 &vfp_base_count))
3797 {
3798 int regno;
3799 int unit_length;
3800 int shift;
3801 unsigned mask;
3802
3803 /* Because this is a CPRC it cannot go in a core register or
3804 cause a core register to be skipped for alignment.
3805 Either it goes in VFP registers and the rest of this loop
3806 iteration is skipped for this argument, or it goes on the
3807 stack (and the stack alignment code is correct for this
3808 case). */
3809 may_use_core_reg = 0;
3810
3811 unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
3812 shift = unit_length / 4;
3813 mask = (1 << (shift * vfp_base_count)) - 1;
3814 for (regno = 0; regno < 16; regno += shift)
3815 if (((vfp_regs_free >> regno) & mask) == mask)
3816 break;
3817
3818 if (regno < 16)
3819 {
3820 int reg_char;
3821 int reg_scaled;
3822 int i;
3823
3824 vfp_regs_free &= ~(mask << regno);
3825 reg_scaled = regno / shift;
3826 reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
3827 for (i = 0; i < vfp_base_count; i++)
3828 {
3829 char name_buf[4];
3830 int regnum;
58d6951d
DJ
3831 if (reg_char == 'q')
3832 arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
90445bd3 3833 val + i * unit_length);
58d6951d
DJ
3834 else
3835 {
8c042590
PM
3836 xsnprintf (name_buf, sizeof (name_buf), "%c%d",
3837 reg_char, reg_scaled + i);
58d6951d
DJ
3838 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
3839 strlen (name_buf));
b66f5587 3840 regcache->cooked_write (regnum, val + i * unit_length);
58d6951d 3841 }
90445bd3
DJ
3842 }
3843 continue;
3844 }
3845 else
3846 {
3847 /* This CPRC could not go in VFP registers, so all VFP
3848 registers are now marked as used. */
3849 vfp_regs_free = 0;
3850 }
3851 }
3852
85102364 3853 /* Push stack padding for doubleword alignment. */
2af48f68
PB
3854 if (nstack & (align - 1))
3855 {
f0452268
AH
3856 si = push_stack_item (si, val, ARM_INT_REGISTER_SIZE);
3857 nstack += ARM_INT_REGISTER_SIZE;
2af48f68
PB
3858 }
3859
3860 /* Doubleword aligned quantities must go in even register pairs. */
90445bd3
DJ
3861 if (may_use_core_reg
3862 && argreg <= ARM_LAST_ARG_REGNUM
f0452268 3863 && align > ARM_INT_REGISTER_SIZE
2af48f68
PB
3864 && argreg & 1)
3865 argreg++;
3866
2dd604e7
RE
3867 /* If the argument is a pointer to a function, and it is a
3868 Thumb function, create a LOCAL copy of the value and set
3869 the THUMB bit in it. */
3870 if (TYPE_CODE_PTR == typecode
3871 && target_type != NULL
78134374 3872 && TYPE_CODE_FUNC == check_typedef (target_type)->code ())
2dd604e7 3873 {
e17a4113 3874 CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
9779414d 3875 if (arm_pc_is_thumb (gdbarch, regval))
2dd604e7 3876 {
224c3ddb 3877 bfd_byte *copy = (bfd_byte *) alloca (len);
8c6363cf 3878 store_unsigned_integer (copy, len, byte_order,
e17a4113 3879 MAKE_THUMB_ADDR (regval));
8c6363cf 3880 val = copy;
2dd604e7
RE
3881 }
3882 }
3883
3884 /* Copy the argument to general registers or the stack in
3885 register-sized pieces. Large arguments are split between
3886 registers and stack. */
3887 while (len > 0)
3888 {
f0452268
AH
3889 int partial_len = len < ARM_INT_REGISTER_SIZE
3890 ? len : ARM_INT_REGISTER_SIZE;
ef9bd0b8
YQ
3891 CORE_ADDR regval
3892 = extract_unsigned_integer (val, partial_len, byte_order);
2dd604e7 3893
90445bd3 3894 if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
2dd604e7
RE
3895 {
3896 /* The argument is being passed in a general purpose
3897 register. */
e17a4113 3898 if (byte_order == BFD_ENDIAN_BIG)
f0452268 3899 regval <<= (ARM_INT_REGISTER_SIZE - partial_len) * 8;
2dd604e7
RE
3900 if (arm_debug)
3901 fprintf_unfiltered (gdb_stdlog, "arg %d in %s = 0x%s\n",
c9f4d572
UW
3902 argnum,
3903 gdbarch_register_name
2af46ca0 3904 (gdbarch, argreg),
f0452268 3905 phex (regval, ARM_INT_REGISTER_SIZE));
2dd604e7
RE
3906 regcache_cooked_write_unsigned (regcache, argreg, regval);
3907 argreg++;
3908 }
3909 else
3910 {
f0452268 3911 gdb_byte buf[ARM_INT_REGISTER_SIZE];
ef9bd0b8
YQ
3912
3913 memset (buf, 0, sizeof (buf));
3914 store_unsigned_integer (buf, partial_len, byte_order, regval);
3915
2dd604e7
RE
3916 /* Push the arguments onto the stack. */
3917 if (arm_debug)
3918 fprintf_unfiltered (gdb_stdlog, "arg %d @ sp + %d\n",
3919 argnum, nstack);
f0452268
AH
3920 si = push_stack_item (si, buf, ARM_INT_REGISTER_SIZE);
3921 nstack += ARM_INT_REGISTER_SIZE;
2dd604e7
RE
3922 }
3923
3924 len -= partial_len;
3925 val += partial_len;
3926 }
3927 }
3928 /* If we have an odd number of words to push, then decrement the stack
3929 by one word now, so first stack argument will be dword aligned. */
3930 if (nstack & 4)
3931 sp -= 4;
3932
3933 while (si)
3934 {
3935 sp -= si->len;
3936 write_memory (sp, si->data, si->len);
3937 si = pop_stack_item (si);
3938 }
3939
3940 /* Finally, update teh SP register. */
3941 regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
3942
3943 return sp;
3944}
3945
f53f0d0b
PB
3946
3947/* Always align the frame to an 8-byte boundary. This is required on
3948 some platforms and harmless on the rest. */
3949
3950static CORE_ADDR
3951arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
3952{
3953 /* Align the stack to eight bytes. */
3954 return sp & ~ (CORE_ADDR) 7;
3955}
3956
c906108c 3957static void
12b27276 3958print_fpu_flags (struct ui_file *file, int flags)
c906108c 3959{
c5aa993b 3960 if (flags & (1 << 0))
12b27276 3961 fputs_filtered ("IVO ", file);
c5aa993b 3962 if (flags & (1 << 1))
12b27276 3963 fputs_filtered ("DVZ ", file);
c5aa993b 3964 if (flags & (1 << 2))
12b27276 3965 fputs_filtered ("OFL ", file);
c5aa993b 3966 if (flags & (1 << 3))
12b27276 3967 fputs_filtered ("UFL ", file);
c5aa993b 3968 if (flags & (1 << 4))
12b27276
WN
3969 fputs_filtered ("INX ", file);
3970 fputc_filtered ('\n', file);
c906108c
SS
3971}
3972
5e74b15c
RE
3973/* Print interesting information about the floating point processor
3974 (if present) or emulator. */
34e8f22d 3975static void
d855c300 3976arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
23e3a7ac 3977 struct frame_info *frame, const char *args)
c906108c 3978{
9c9acae0 3979 unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
c5aa993b
JM
3980 int type;
3981
3982 type = (status >> 24) & 127;
edefbb7c 3983 if (status & (1 << 31))
12b27276 3984 fprintf_filtered (file, _("Hardware FPU type %d\n"), type);
edefbb7c 3985 else
12b27276 3986 fprintf_filtered (file, _("Software FPU type %d\n"), type);
edefbb7c 3987 /* i18n: [floating point unit] mask */
12b27276
WN
3988 fputs_filtered (_("mask: "), file);
3989 print_fpu_flags (file, status >> 16);
edefbb7c 3990 /* i18n: [floating point unit] flags */
12b27276
WN
3991 fputs_filtered (_("flags: "), file);
3992 print_fpu_flags (file, status);
c906108c
SS
3993}
3994
27067745
UW
3995/* Construct the ARM extended floating point type. */
3996static struct type *
3997arm_ext_type (struct gdbarch *gdbarch)
3998{
3999 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4000
4001 if (!tdep->arm_ext_type)
4002 tdep->arm_ext_type
e9bb382b 4003 = arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
27067745
UW
4004 floatformats_arm_ext);
4005
4006 return tdep->arm_ext_type;
4007}
4008
58d6951d
DJ
4009static struct type *
4010arm_neon_double_type (struct gdbarch *gdbarch)
4011{
4012 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4013
4014 if (tdep->neon_double_type == NULL)
4015 {
4016 struct type *t, *elem;
4017
4018 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
4019 TYPE_CODE_UNION);
4020 elem = builtin_type (gdbarch)->builtin_uint8;
4021 append_composite_type_field (t, "u8", init_vector_type (elem, 8));
4022 elem = builtin_type (gdbarch)->builtin_uint16;
4023 append_composite_type_field (t, "u16", init_vector_type (elem, 4));
4024 elem = builtin_type (gdbarch)->builtin_uint32;
4025 append_composite_type_field (t, "u32", init_vector_type (elem, 2));
4026 elem = builtin_type (gdbarch)->builtin_uint64;
4027 append_composite_type_field (t, "u64", elem);
4028 elem = builtin_type (gdbarch)->builtin_float;
4029 append_composite_type_field (t, "f32", init_vector_type (elem, 2));
4030 elem = builtin_type (gdbarch)->builtin_double;
4031 append_composite_type_field (t, "f64", elem);
4032
4033 TYPE_VECTOR (t) = 1;
d0e39ea2 4034 t->set_name ("neon_d");
58d6951d
DJ
4035 tdep->neon_double_type = t;
4036 }
4037
4038 return tdep->neon_double_type;
4039}
4040
4041/* FIXME: The vector types are not correctly ordered on big-endian
4042 targets. Just as s0 is the low bits of d0, d0[0] is also the low
4043 bits of d0 - regardless of what unit size is being held in d0. So
4044 the offset of the first uint8 in d0 is 7, but the offset of the
4045 first float is 4. This code works as-is for little-endian
4046 targets. */
4047
4048static struct type *
4049arm_neon_quad_type (struct gdbarch *gdbarch)
4050{
4051 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4052
4053 if (tdep->neon_quad_type == NULL)
4054 {
4055 struct type *t, *elem;
4056
4057 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
4058 TYPE_CODE_UNION);
4059 elem = builtin_type (gdbarch)->builtin_uint8;
4060 append_composite_type_field (t, "u8", init_vector_type (elem, 16));
4061 elem = builtin_type (gdbarch)->builtin_uint16;
4062 append_composite_type_field (t, "u16", init_vector_type (elem, 8));
4063 elem = builtin_type (gdbarch)->builtin_uint32;
4064 append_composite_type_field (t, "u32", init_vector_type (elem, 4));
4065 elem = builtin_type (gdbarch)->builtin_uint64;
4066 append_composite_type_field (t, "u64", init_vector_type (elem, 2));
4067 elem = builtin_type (gdbarch)->builtin_float;
4068 append_composite_type_field (t, "f32", init_vector_type (elem, 4));
4069 elem = builtin_type (gdbarch)->builtin_double;
4070 append_composite_type_field (t, "f64", init_vector_type (elem, 2));
4071
4072 TYPE_VECTOR (t) = 1;
d0e39ea2 4073 t->set_name ("neon_q");
58d6951d
DJ
4074 tdep->neon_quad_type = t;
4075 }
4076
4077 return tdep->neon_quad_type;
4078}
4079
34e8f22d
RE
4080/* Return the GDB type object for the "standard" data type of data in
4081 register N. */
4082
4083static struct type *
7a5ea0d4 4084arm_register_type (struct gdbarch *gdbarch, int regnum)
032758dc 4085{
58d6951d
DJ
4086 int num_regs = gdbarch_num_regs (gdbarch);
4087
4088 if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
4089 && regnum >= num_regs && regnum < num_regs + 32)
4090 return builtin_type (gdbarch)->builtin_float;
4091
4092 if (gdbarch_tdep (gdbarch)->have_neon_pseudos
4093 && regnum >= num_regs + 32 && regnum < num_regs + 32 + 16)
4094 return arm_neon_quad_type (gdbarch);
4095
4096 /* If the target description has register information, we are only
4097 in this function so that we can override the types of
4098 double-precision registers for NEON. */
4099 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
4100 {
4101 struct type *t = tdesc_register_type (gdbarch, regnum);
4102
4103 if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
78134374 4104 && t->code () == TYPE_CODE_FLT
58d6951d
DJ
4105 && gdbarch_tdep (gdbarch)->have_neon)
4106 return arm_neon_double_type (gdbarch);
4107 else
4108 return t;
4109 }
4110
34e8f22d 4111 if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
58d6951d
DJ
4112 {
4113 if (!gdbarch_tdep (gdbarch)->have_fpa_registers)
4114 return builtin_type (gdbarch)->builtin_void;
4115
4116 return arm_ext_type (gdbarch);
4117 }
e4c16157 4118 else if (regnum == ARM_SP_REGNUM)
0dfff4cb 4119 return builtin_type (gdbarch)->builtin_data_ptr;
e4c16157 4120 else if (regnum == ARM_PC_REGNUM)
0dfff4cb 4121 return builtin_type (gdbarch)->builtin_func_ptr;
ff6f572f
DJ
4122 else if (regnum >= ARRAY_SIZE (arm_register_names))
4123 /* These registers are only supported on targets which supply
4124 an XML description. */
df4df182 4125 return builtin_type (gdbarch)->builtin_int0;
032758dc 4126 else
df4df182 4127 return builtin_type (gdbarch)->builtin_uint32;
032758dc
AC
4128}
4129
ff6f572f
DJ
4130/* Map a DWARF register REGNUM onto the appropriate GDB register
4131 number. */
4132
4133static int
d3f73121 4134arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
ff6f572f
DJ
4135{
4136 /* Core integer regs. */
4137 if (reg >= 0 && reg <= 15)
4138 return reg;
4139
4140 /* Legacy FPA encoding. These were once used in a way which
4141 overlapped with VFP register numbering, so their use is
4142 discouraged, but GDB doesn't support the ARM toolchain
4143 which used them for VFP. */
4144 if (reg >= 16 && reg <= 23)
4145 return ARM_F0_REGNUM + reg - 16;
4146
4147 /* New assignments for the FPA registers. */
4148 if (reg >= 96 && reg <= 103)
4149 return ARM_F0_REGNUM + reg - 96;
4150
4151 /* WMMX register assignments. */
4152 if (reg >= 104 && reg <= 111)
4153 return ARM_WCGR0_REGNUM + reg - 104;
4154
4155 if (reg >= 112 && reg <= 127)
4156 return ARM_WR0_REGNUM + reg - 112;
4157
4158 if (reg >= 192 && reg <= 199)
4159 return ARM_WC0_REGNUM + reg - 192;
4160
58d6951d
DJ
4161 /* VFP v2 registers. A double precision value is actually
4162 in d1 rather than s2, but the ABI only defines numbering
4163 for the single precision registers. This will "just work"
4164 in GDB for little endian targets (we'll read eight bytes,
4165 starting in s0 and then progressing to s1), but will be
4166 reversed on big endian targets with VFP. This won't
4167 be a problem for the new Neon quad registers; you're supposed
4168 to use DW_OP_piece for those. */
4169 if (reg >= 64 && reg <= 95)
4170 {
4171 char name_buf[4];
4172
8c042590 4173 xsnprintf (name_buf, sizeof (name_buf), "s%d", reg - 64);
58d6951d
DJ
4174 return user_reg_map_name_to_regnum (gdbarch, name_buf,
4175 strlen (name_buf));
4176 }
4177
4178 /* VFP v3 / Neon registers. This range is also used for VFP v2
4179 registers, except that it now describes d0 instead of s0. */
4180 if (reg >= 256 && reg <= 287)
4181 {
4182 char name_buf[4];
4183
8c042590 4184 xsnprintf (name_buf, sizeof (name_buf), "d%d", reg - 256);
58d6951d
DJ
4185 return user_reg_map_name_to_regnum (gdbarch, name_buf,
4186 strlen (name_buf));
4187 }
4188
ff6f572f
DJ
4189 return -1;
4190}
4191
26216b98
AC
4192/* Map GDB internal REGNUM onto the Arm simulator register numbers. */
4193static int
e7faf938 4194arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
26216b98
AC
4195{
4196 int reg = regnum;
e7faf938 4197 gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
26216b98 4198
ff6f572f
DJ
4199 if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
4200 return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
4201
4202 if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
4203 return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
4204
4205 if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
4206 return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
4207
26216b98
AC
4208 if (reg < NUM_GREGS)
4209 return SIM_ARM_R0_REGNUM + reg;
4210 reg -= NUM_GREGS;
4211
4212 if (reg < NUM_FREGS)
4213 return SIM_ARM_FP0_REGNUM + reg;
4214 reg -= NUM_FREGS;
4215
4216 if (reg < NUM_SREGS)
4217 return SIM_ARM_FPS_REGNUM + reg;
4218 reg -= NUM_SREGS;
4219
edefbb7c 4220 internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
26216b98 4221}
34e8f22d 4222
d9311bfa
AT
4223/* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
4224 the buffer to be NEW_LEN bytes ending at ENDADDR. Return
4225 NULL if an error occurs. BUF is freed. */
c906108c 4226
d9311bfa
AT
4227static gdb_byte *
4228extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
4229 int old_len, int new_len)
4230{
4231 gdb_byte *new_buf;
4232 int bytes_to_read = new_len - old_len;
c906108c 4233
d9311bfa
AT
4234 new_buf = (gdb_byte *) xmalloc (new_len);
4235 memcpy (new_buf + bytes_to_read, buf, old_len);
4236 xfree (buf);
198cd59d 4237 if (target_read_code (endaddr - new_len, new_buf, bytes_to_read) != 0)
d9311bfa
AT
4238 {
4239 xfree (new_buf);
4240 return NULL;
c906108c 4241 }
d9311bfa 4242 return new_buf;
c906108c
SS
4243}
4244
d9311bfa
AT
4245/* An IT block is at most the 2-byte IT instruction followed by
4246 four 4-byte instructions. The furthest back we must search to
4247 find an IT block that affects the current instruction is thus
4248 2 + 3 * 4 == 14 bytes. */
4249#define MAX_IT_BLOCK_PREFIX 14
177321bd 4250
d9311bfa
AT
4251/* Use a quick scan if there are more than this many bytes of
4252 code. */
4253#define IT_SCAN_THRESHOLD 32
177321bd 4254
d9311bfa
AT
4255/* Adjust a breakpoint's address to move breakpoints out of IT blocks.
4256 A breakpoint in an IT block may not be hit, depending on the
4257 condition flags. */
ad527d2e 4258static CORE_ADDR
d9311bfa 4259arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
c906108c 4260{
d9311bfa
AT
4261 gdb_byte *buf;
4262 char map_type;
4263 CORE_ADDR boundary, func_start;
4264 int buf_len;
4265 enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
4266 int i, any, last_it, last_it_count;
177321bd 4267
d9311bfa
AT
4268 /* If we are using BKPT breakpoints, none of this is necessary. */
4269 if (gdbarch_tdep (gdbarch)->thumb2_breakpoint == NULL)
4270 return bpaddr;
177321bd 4271
d9311bfa
AT
4272 /* ARM mode does not have this problem. */
4273 if (!arm_pc_is_thumb (gdbarch, bpaddr))
4274 return bpaddr;
177321bd 4275
d9311bfa
AT
4276 /* We are setting a breakpoint in Thumb code that could potentially
4277 contain an IT block. The first step is to find how much Thumb
4278 code there is; we do not need to read outside of known Thumb
4279 sequences. */
4280 map_type = arm_find_mapping_symbol (bpaddr, &boundary);
4281 if (map_type == 0)
4282 /* Thumb-2 code must have mapping symbols to have a chance. */
4283 return bpaddr;
9dca5578 4284
d9311bfa 4285 bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
177321bd 4286
d9311bfa
AT
4287 if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
4288 && func_start > boundary)
4289 boundary = func_start;
9dca5578 4290
d9311bfa
AT
4291 /* Search for a candidate IT instruction. We have to do some fancy
4292 footwork to distinguish a real IT instruction from the second
4293 half of a 32-bit instruction, but there is no need for that if
4294 there's no candidate. */
325fac50 4295 buf_len = std::min (bpaddr - boundary, (CORE_ADDR) MAX_IT_BLOCK_PREFIX);
d9311bfa
AT
4296 if (buf_len == 0)
4297 /* No room for an IT instruction. */
4298 return bpaddr;
c906108c 4299
d9311bfa 4300 buf = (gdb_byte *) xmalloc (buf_len);
198cd59d 4301 if (target_read_code (bpaddr - buf_len, buf, buf_len) != 0)
d9311bfa
AT
4302 return bpaddr;
4303 any = 0;
4304 for (i = 0; i < buf_len; i += 2)
c906108c 4305 {
d9311bfa
AT
4306 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
4307 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
25b41d01 4308 {
d9311bfa
AT
4309 any = 1;
4310 break;
25b41d01 4311 }
c906108c 4312 }
d9311bfa
AT
4313
4314 if (any == 0)
c906108c 4315 {
d9311bfa
AT
4316 xfree (buf);
4317 return bpaddr;
f9d67f43
DJ
4318 }
4319
4320 /* OK, the code bytes before this instruction contain at least one
4321 halfword which resembles an IT instruction. We know that it's
4322 Thumb code, but there are still two possibilities. Either the
4323 halfword really is an IT instruction, or it is the second half of
4324 a 32-bit Thumb instruction. The only way we can tell is to
4325 scan forwards from a known instruction boundary. */
4326 if (bpaddr - boundary > IT_SCAN_THRESHOLD)
4327 {
4328 int definite;
4329
4330 /* There's a lot of code before this instruction. Start with an
4331 optimistic search; it's easy to recognize halfwords that can
4332 not be the start of a 32-bit instruction, and use that to
4333 lock on to the instruction boundaries. */
4334 buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
4335 if (buf == NULL)
4336 return bpaddr;
4337 buf_len = IT_SCAN_THRESHOLD;
4338
4339 definite = 0;
4340 for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
4341 {
4342 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
4343 if (thumb_insn_size (inst1) == 2)
4344 {
4345 definite = 1;
4346 break;
4347 }
4348 }
4349
4350 /* At this point, if DEFINITE, BUF[I] is the first place we
4351 are sure that we know the instruction boundaries, and it is far
4352 enough from BPADDR that we could not miss an IT instruction
4353 affecting BPADDR. If ! DEFINITE, give up - start from a
4354 known boundary. */
4355 if (! definite)
4356 {
0963b4bd
MS
4357 buf = extend_buffer_earlier (buf, bpaddr, buf_len,
4358 bpaddr - boundary);
f9d67f43
DJ
4359 if (buf == NULL)
4360 return bpaddr;
4361 buf_len = bpaddr - boundary;
4362 i = 0;
4363 }
4364 }
4365 else
4366 {
4367 buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
4368 if (buf == NULL)
4369 return bpaddr;
4370 buf_len = bpaddr - boundary;
4371 i = 0;
4372 }
4373
4374 /* Scan forwards. Find the last IT instruction before BPADDR. */
4375 last_it = -1;
4376 last_it_count = 0;
4377 while (i < buf_len)
4378 {
4379 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
4380 last_it_count--;
4381 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
4382 {
4383 last_it = i;
4384 if (inst1 & 0x0001)
4385 last_it_count = 4;
4386 else if (inst1 & 0x0002)
4387 last_it_count = 3;
4388 else if (inst1 & 0x0004)
4389 last_it_count = 2;
4390 else
4391 last_it_count = 1;
4392 }
4393 i += thumb_insn_size (inst1);
4394 }
4395
4396 xfree (buf);
4397
4398 if (last_it == -1)
4399 /* There wasn't really an IT instruction after all. */
4400 return bpaddr;
4401
4402 if (last_it_count < 1)
4403 /* It was too far away. */
4404 return bpaddr;
4405
4406 /* This really is a trouble spot. Move the breakpoint to the IT
4407 instruction. */
4408 return bpaddr - buf_len + last_it;
4409}
4410
cca44b1b 4411/* ARM displaced stepping support.
c906108c 4412
cca44b1b 4413 Generally ARM displaced stepping works as follows:
c906108c 4414
cca44b1b 4415 1. When an instruction is to be single-stepped, it is first decoded by
2ba163c8
SM
4416 arm_process_displaced_insn. Depending on the type of instruction, it is
4417 then copied to a scratch location, possibly in a modified form. The
4418 copy_* set of functions performs such modification, as necessary. A
4419 breakpoint is placed after the modified instruction in the scratch space
4420 to return control to GDB. Note in particular that instructions which
4421 modify the PC will no longer do so after modification.
c5aa993b 4422
cca44b1b
JB
4423 2. The instruction is single-stepped, by setting the PC to the scratch
4424 location address, and resuming. Control returns to GDB when the
4425 breakpoint is hit.
c5aa993b 4426
cca44b1b
JB
4427 3. A cleanup function (cleanup_*) is called corresponding to the copy_*
4428 function used for the current instruction. This function's job is to
4429 put the CPU/memory state back to what it would have been if the
4430 instruction had been executed unmodified in its original location. */
c5aa993b 4431
cca44b1b
JB
4432/* NOP instruction (mov r0, r0). */
4433#define ARM_NOP 0xe1a00000
34518530 4434#define THUMB_NOP 0x4600
cca44b1b
JB
4435
4436/* Helper for register reads for displaced stepping. In particular, this
4437 returns the PC as it would be seen by the instruction at its original
4438 location. */
4439
4440ULONGEST
cfba9872 4441displaced_read_reg (struct regcache *regs, arm_displaced_step_closure *dsc,
36073a92 4442 int regno)
cca44b1b
JB
4443{
4444 ULONGEST ret;
36073a92 4445 CORE_ADDR from = dsc->insn_addr;
cca44b1b 4446
bf9f652a 4447 if (regno == ARM_PC_REGNUM)
cca44b1b 4448 {
4db71c0b
YQ
4449 /* Compute pipeline offset:
4450 - When executing an ARM instruction, PC reads as the address of the
4451 current instruction plus 8.
4452 - When executing a Thumb instruction, PC reads as the address of the
4453 current instruction plus 4. */
4454
36073a92 4455 if (!dsc->is_thumb)
4db71c0b
YQ
4456 from += 8;
4457 else
4458 from += 4;
4459
cca44b1b
JB
4460 if (debug_displaced)
4461 fprintf_unfiltered (gdb_stdlog, "displaced: read pc value %.8lx\n",
4db71c0b
YQ
4462 (unsigned long) from);
4463 return (ULONGEST) from;
cca44b1b 4464 }
c906108c 4465 else
cca44b1b
JB
4466 {
4467 regcache_cooked_read_unsigned (regs, regno, &ret);
4468 if (debug_displaced)
4469 fprintf_unfiltered (gdb_stdlog, "displaced: read r%d value %.8lx\n",
4470 regno, (unsigned long) ret);
4471 return ret;
4472 }
c906108c
SS
4473}
4474
cca44b1b
JB
4475static int
4476displaced_in_arm_mode (struct regcache *regs)
4477{
4478 ULONGEST ps;
ac7936df 4479 ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
66e810cd 4480
cca44b1b 4481 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
66e810cd 4482
9779414d 4483 return (ps & t_bit) == 0;
cca44b1b 4484}
66e810cd 4485
cca44b1b 4486/* Write to the PC as from a branch instruction. */
c906108c 4487
cca44b1b 4488static void
cfba9872 4489branch_write_pc (struct regcache *regs, arm_displaced_step_closure *dsc,
36073a92 4490 ULONGEST val)
c906108c 4491{
36073a92 4492 if (!dsc->is_thumb)
cca44b1b
JB
4493 /* Note: If bits 0/1 are set, this branch would be unpredictable for
4494 architecture versions < 6. */
0963b4bd
MS
4495 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
4496 val & ~(ULONGEST) 0x3);
cca44b1b 4497 else
0963b4bd
MS
4498 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
4499 val & ~(ULONGEST) 0x1);
cca44b1b 4500}
66e810cd 4501
cca44b1b
JB
4502/* Write to the PC as from a branch-exchange instruction. */
4503
4504static void
4505bx_write_pc (struct regcache *regs, ULONGEST val)
4506{
4507 ULONGEST ps;
ac7936df 4508 ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
cca44b1b
JB
4509
4510 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
4511
4512 if ((val & 1) == 1)
c906108c 4513 {
9779414d 4514 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
cca44b1b
JB
4515 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
4516 }
4517 else if ((val & 2) == 0)
4518 {
9779414d 4519 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 4520 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
c906108c
SS
4521 }
4522 else
4523 {
cca44b1b
JB
4524 /* Unpredictable behaviour. Try to do something sensible (switch to ARM
4525 mode, align dest to 4 bytes). */
4526 warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
9779414d 4527 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 4528 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
c906108c
SS
4529 }
4530}
ed9a39eb 4531
cca44b1b 4532/* Write to the PC as if from a load instruction. */
ed9a39eb 4533
34e8f22d 4534static void
cfba9872 4535load_write_pc (struct regcache *regs, arm_displaced_step_closure *dsc,
36073a92 4536 ULONGEST val)
ed9a39eb 4537{
cca44b1b
JB
4538 if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
4539 bx_write_pc (regs, val);
4540 else
36073a92 4541 branch_write_pc (regs, dsc, val);
cca44b1b 4542}
be8626e0 4543
cca44b1b
JB
4544/* Write to the PC as if from an ALU instruction. */
4545
4546static void
cfba9872 4547alu_write_pc (struct regcache *regs, arm_displaced_step_closure *dsc,
36073a92 4548 ULONGEST val)
cca44b1b 4549{
36073a92 4550 if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
cca44b1b
JB
4551 bx_write_pc (regs, val);
4552 else
36073a92 4553 branch_write_pc (regs, dsc, val);
cca44b1b
JB
4554}
4555
4556/* Helper for writing to registers for displaced stepping. Writing to the PC
4557 has a varying effects depending on the instruction which does the write:
4558 this is controlled by the WRITE_PC argument. */
4559
4560void
cfba9872 4561displaced_write_reg (struct regcache *regs, arm_displaced_step_closure *dsc,
cca44b1b
JB
4562 int regno, ULONGEST val, enum pc_write_style write_pc)
4563{
bf9f652a 4564 if (regno == ARM_PC_REGNUM)
08216dd7 4565 {
cca44b1b
JB
4566 if (debug_displaced)
4567 fprintf_unfiltered (gdb_stdlog, "displaced: writing pc %.8lx\n",
4568 (unsigned long) val);
4569 switch (write_pc)
08216dd7 4570 {
cca44b1b 4571 case BRANCH_WRITE_PC:
36073a92 4572 branch_write_pc (regs, dsc, val);
08216dd7
RE
4573 break;
4574
cca44b1b
JB
4575 case BX_WRITE_PC:
4576 bx_write_pc (regs, val);
4577 break;
4578
4579 case LOAD_WRITE_PC:
36073a92 4580 load_write_pc (regs, dsc, val);
cca44b1b
JB
4581 break;
4582
4583 case ALU_WRITE_PC:
36073a92 4584 alu_write_pc (regs, dsc, val);
cca44b1b
JB
4585 break;
4586
4587 case CANNOT_WRITE_PC:
4588 warning (_("Instruction wrote to PC in an unexpected way when "
4589 "single-stepping"));
08216dd7
RE
4590 break;
4591
4592 default:
97b9747c
JB
4593 internal_error (__FILE__, __LINE__,
4594 _("Invalid argument to displaced_write_reg"));
08216dd7 4595 }
b508a996 4596
cca44b1b 4597 dsc->wrote_to_pc = 1;
b508a996 4598 }
ed9a39eb 4599 else
b508a996 4600 {
cca44b1b
JB
4601 if (debug_displaced)
4602 fprintf_unfiltered (gdb_stdlog, "displaced: writing r%d value %.8lx\n",
4603 regno, (unsigned long) val);
4604 regcache_cooked_write_unsigned (regs, regno, val);
b508a996 4605 }
34e8f22d
RE
4606}
4607
cca44b1b
JB
4608/* This function is used to concisely determine if an instruction INSN
4609 references PC. Register fields of interest in INSN should have the
0963b4bd
MS
4610 corresponding fields of BITMASK set to 0b1111. The function
4611 returns return 1 if any of these fields in INSN reference the PC
4612 (also 0b1111, r15), else it returns 0. */
67255d04
RE
4613
4614static int
cca44b1b 4615insn_references_pc (uint32_t insn, uint32_t bitmask)
67255d04 4616{
cca44b1b 4617 uint32_t lowbit = 1;
67255d04 4618
cca44b1b
JB
4619 while (bitmask != 0)
4620 {
4621 uint32_t mask;
44e1a9eb 4622
cca44b1b
JB
4623 for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
4624 ;
67255d04 4625
cca44b1b
JB
4626 if (!lowbit)
4627 break;
67255d04 4628
cca44b1b 4629 mask = lowbit * 0xf;
67255d04 4630
cca44b1b
JB
4631 if ((insn & mask) == mask)
4632 return 1;
4633
4634 bitmask &= ~mask;
67255d04
RE
4635 }
4636
cca44b1b
JB
4637 return 0;
4638}
2af48f68 4639
cca44b1b
JB
4640/* The simplest copy function. Many instructions have the same effect no
4641 matter what address they are executed at: in those cases, use this. */
67255d04 4642
cca44b1b 4643static int
7ff120b4 4644arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn,
cfba9872 4645 const char *iname, arm_displaced_step_closure *dsc)
cca44b1b
JB
4646{
4647 if (debug_displaced)
4648 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx, "
4649 "opcode/class '%s' unmodified\n", (unsigned long) insn,
4650 iname);
67255d04 4651
cca44b1b 4652 dsc->modinsn[0] = insn;
67255d04 4653
cca44b1b
JB
4654 return 0;
4655}
4656
34518530
YQ
4657static int
4658thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
4659 uint16_t insn2, const char *iname,
cfba9872 4660 arm_displaced_step_closure *dsc)
34518530
YQ
4661{
4662 if (debug_displaced)
4663 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x %.4x, "
4664 "opcode/class '%s' unmodified\n", insn1, insn2,
4665 iname);
4666
4667 dsc->modinsn[0] = insn1;
4668 dsc->modinsn[1] = insn2;
4669 dsc->numinsns = 2;
4670
4671 return 0;
4672}
4673
4674/* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
4675 modification. */
4676static int
615234c1 4677thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, uint16_t insn,
34518530 4678 const char *iname,
cfba9872 4679 arm_displaced_step_closure *dsc)
34518530
YQ
4680{
4681 if (debug_displaced)
4682 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x, "
4683 "opcode/class '%s' unmodified\n", insn,
4684 iname);
4685
4686 dsc->modinsn[0] = insn;
4687
4688 return 0;
4689}
4690
cca44b1b
JB
4691/* Preload instructions with immediate offset. */
4692
4693static void
6e39997a 4694cleanup_preload (struct gdbarch *gdbarch,
cfba9872 4695 struct regcache *regs, arm_displaced_step_closure *dsc)
cca44b1b
JB
4696{
4697 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
4698 if (!dsc->u.preload.immed)
4699 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
4700}
4701
7ff120b4
YQ
4702static void
4703install_preload (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 4704 arm_displaced_step_closure *dsc, unsigned int rn)
cca44b1b 4705{
cca44b1b 4706 ULONGEST rn_val;
cca44b1b
JB
4707 /* Preload instructions:
4708
4709 {pli/pld} [rn, #+/-imm]
4710 ->
4711 {pli/pld} [r0, #+/-imm]. */
4712
36073a92
YQ
4713 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
4714 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 4715 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
cca44b1b
JB
4716 dsc->u.preload.immed = 1;
4717
cca44b1b 4718 dsc->cleanup = &cleanup_preload;
cca44b1b
JB
4719}
4720
cca44b1b 4721static int
7ff120b4 4722arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
cfba9872 4723 arm_displaced_step_closure *dsc)
cca44b1b
JB
4724{
4725 unsigned int rn = bits (insn, 16, 19);
cca44b1b 4726
7ff120b4
YQ
4727 if (!insn_references_pc (insn, 0x000f0000ul))
4728 return arm_copy_unmodified (gdbarch, insn, "preload", dsc);
cca44b1b
JB
4729
4730 if (debug_displaced)
4731 fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
4732 (unsigned long) insn);
4733
7ff120b4
YQ
4734 dsc->modinsn[0] = insn & 0xfff0ffff;
4735
4736 install_preload (gdbarch, regs, dsc, rn);
4737
4738 return 0;
4739}
4740
34518530
YQ
4741static int
4742thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
cfba9872 4743 struct regcache *regs, arm_displaced_step_closure *dsc)
34518530
YQ
4744{
4745 unsigned int rn = bits (insn1, 0, 3);
4746 unsigned int u_bit = bit (insn1, 7);
4747 int imm12 = bits (insn2, 0, 11);
4748 ULONGEST pc_val;
4749
4750 if (rn != ARM_PC_REGNUM)
4751 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);
4752
4753 /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
4754 PLD (literal) Encoding T1. */
4755 if (debug_displaced)
4756 fprintf_unfiltered (gdb_stdlog,
4757 "displaced: copying pld/pli pc (0x%x) %c imm12 %.4x\n",
4758 (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
4759 imm12);
4760
4761 if (!u_bit)
4762 imm12 = -1 * imm12;
4763
4764 /* Rewrite instruction {pli/pld} PC imm12 into:
4765 Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12
4766
4767 {pli/pld} [r0, r1]
4768
4769 Cleanup: r0 <- tmp[0], r1 <- tmp[1]. */
4770
4771 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
4772 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
4773
4774 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
4775
4776 displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
4777 displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
4778 dsc->u.preload.immed = 0;
4779
4780 /* {pli/pld} [r0, r1] */
4781 dsc->modinsn[0] = insn1 & 0xfff0;
4782 dsc->modinsn[1] = 0xf001;
4783 dsc->numinsns = 2;
4784
4785 dsc->cleanup = &cleanup_preload;
4786 return 0;
4787}
4788
7ff120b4
YQ
4789/* Preload instructions with register offset. */
4790
4791static void
4792install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 4793 arm_displaced_step_closure *dsc, unsigned int rn,
7ff120b4
YQ
4794 unsigned int rm)
4795{
4796 ULONGEST rn_val, rm_val;
4797
cca44b1b
JB
4798 /* Preload register-offset instructions:
4799
4800 {pli/pld} [rn, rm {, shift}]
4801 ->
4802 {pli/pld} [r0, r1 {, shift}]. */
4803
36073a92
YQ
4804 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
4805 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
4806 rn_val = displaced_read_reg (regs, dsc, rn);
4807 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
4808 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
4809 displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
cca44b1b
JB
4810 dsc->u.preload.immed = 0;
4811
cca44b1b 4812 dsc->cleanup = &cleanup_preload;
7ff120b4
YQ
4813}
4814
4815static int
4816arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
4817 struct regcache *regs,
cfba9872 4818 arm_displaced_step_closure *dsc)
7ff120b4
YQ
4819{
4820 unsigned int rn = bits (insn, 16, 19);
4821 unsigned int rm = bits (insn, 0, 3);
4822
4823
4824 if (!insn_references_pc (insn, 0x000f000ful))
4825 return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);
4826
4827 if (debug_displaced)
4828 fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
4829 (unsigned long) insn);
4830
4831 dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
cca44b1b 4832
7ff120b4 4833 install_preload_reg (gdbarch, regs, dsc, rn, rm);
cca44b1b
JB
4834 return 0;
4835}
4836
4837/* Copy/cleanup coprocessor load and store instructions. */
4838
4839static void
6e39997a 4840cleanup_copro_load_store (struct gdbarch *gdbarch,
cca44b1b 4841 struct regcache *regs,
cfba9872 4842 arm_displaced_step_closure *dsc)
cca44b1b 4843{
36073a92 4844 ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
4845
4846 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
4847
4848 if (dsc->u.ldst.writeback)
4849 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
4850}
4851
7ff120b4
YQ
4852static void
4853install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 4854 arm_displaced_step_closure *dsc,
7ff120b4 4855 int writeback, unsigned int rn)
cca44b1b 4856{
cca44b1b 4857 ULONGEST rn_val;
cca44b1b 4858
cca44b1b
JB
4859 /* Coprocessor load/store instructions:
4860
4861 {stc/stc2} [<Rn>, #+/-imm] (and other immediate addressing modes)
4862 ->
4863 {stc/stc2} [r0, #+/-imm].
4864
4865 ldc/ldc2 are handled identically. */
4866
36073a92
YQ
4867 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
4868 rn_val = displaced_read_reg (regs, dsc, rn);
2b16b2e3
YQ
4869 /* PC should be 4-byte aligned. */
4870 rn_val = rn_val & 0xfffffffc;
cca44b1b
JB
4871 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
4872
7ff120b4 4873 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
4874 dsc->u.ldst.rn = rn;
4875
7ff120b4
YQ
4876 dsc->cleanup = &cleanup_copro_load_store;
4877}
4878
4879static int
4880arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
4881 struct regcache *regs,
cfba9872 4882 arm_displaced_step_closure *dsc)
7ff120b4
YQ
4883{
4884 unsigned int rn = bits (insn, 16, 19);
4885
4886 if (!insn_references_pc (insn, 0x000f0000ul))
4887 return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);
4888
4889 if (debug_displaced)
4890 fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
4891 "load/store insn %.8lx\n", (unsigned long) insn);
4892
cca44b1b
JB
4893 dsc->modinsn[0] = insn & 0xfff0ffff;
4894
7ff120b4 4895 install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);
cca44b1b
JB
4896
4897 return 0;
4898}
4899
34518530
YQ
4900static int
4901thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
4902 uint16_t insn2, struct regcache *regs,
cfba9872 4903 arm_displaced_step_closure *dsc)
34518530
YQ
4904{
4905 unsigned int rn = bits (insn1, 0, 3);
4906
4907 if (rn != ARM_PC_REGNUM)
4908 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
4909 "copro load/store", dsc);
4910
4911 if (debug_displaced)
4912 fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
4913 "load/store insn %.4x%.4x\n", insn1, insn2);
4914
4915 dsc->modinsn[0] = insn1 & 0xfff0;
4916 dsc->modinsn[1] = insn2;
4917 dsc->numinsns = 2;
4918
4919 /* This function is called for copying instruction LDC/LDC2/VLDR, which
4920 doesn't support writeback, so pass 0. */
4921 install_copro_load_store (gdbarch, regs, dsc, 0, rn);
4922
4923 return 0;
4924}
4925
cca44b1b
JB
4926/* Clean up branch instructions (actually perform the branch, by setting
4927 PC). */
4928
4929static void
6e39997a 4930cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 4931 arm_displaced_step_closure *dsc)
cca44b1b 4932{
36073a92 4933 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
4934 int branch_taken = condition_true (dsc->u.branch.cond, status);
4935 enum pc_write_style write_pc = dsc->u.branch.exchange
4936 ? BX_WRITE_PC : BRANCH_WRITE_PC;
4937
4938 if (!branch_taken)
4939 return;
4940
4941 if (dsc->u.branch.link)
4942 {
8c8dba6d 4943 /* The value of LR should be the next insn of current one. In order
85102364 4944 not to confuse logic handling later insn `bx lr', if current insn mode
8c8dba6d
YQ
4945 is Thumb, the bit 0 of LR value should be set to 1. */
4946 ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;
4947
4948 if (dsc->is_thumb)
4949 next_insn_addr |= 0x1;
4950
4951 displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
4952 CANNOT_WRITE_PC);
cca44b1b
JB
4953 }
4954
bf9f652a 4955 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
cca44b1b
JB
4956}
4957
4958/* Copy B/BL/BLX instructions with immediate destinations. */
4959
7ff120b4
YQ
4960static void
4961install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 4962 arm_displaced_step_closure *dsc,
7ff120b4
YQ
4963 unsigned int cond, int exchange, int link, long offset)
4964{
4965 /* Implement "BL<cond> <label>" as:
4966
4967 Preparation: cond <- instruction condition
4968 Insn: mov r0, r0 (nop)
4969 Cleanup: if (condition true) { r14 <- pc; pc <- label }.
4970
4971 B<cond> similar, but don't set r14 in cleanup. */
4972
4973 dsc->u.branch.cond = cond;
4974 dsc->u.branch.link = link;
4975 dsc->u.branch.exchange = exchange;
4976
2b16b2e3
YQ
4977 dsc->u.branch.dest = dsc->insn_addr;
4978 if (link && exchange)
4979 /* For BLX, offset is computed from the Align (PC, 4). */
4980 dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;
4981
7ff120b4 4982 if (dsc->is_thumb)
2b16b2e3 4983 dsc->u.branch.dest += 4 + offset;
7ff120b4 4984 else
2b16b2e3 4985 dsc->u.branch.dest += 8 + offset;
7ff120b4
YQ
4986
4987 dsc->cleanup = &cleanup_branch;
4988}
cca44b1b 4989static int
7ff120b4 4990arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
cfba9872 4991 struct regcache *regs, arm_displaced_step_closure *dsc)
cca44b1b
JB
4992{
4993 unsigned int cond = bits (insn, 28, 31);
4994 int exchange = (cond == 0xf);
4995 int link = exchange || bit (insn, 24);
cca44b1b
JB
4996 long offset;
4997
4998 if (debug_displaced)
4999 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s immediate insn "
5000 "%.8lx\n", (exchange) ? "blx" : (link) ? "bl" : "b",
5001 (unsigned long) insn);
cca44b1b
JB
5002 if (exchange)
5003 /* For BLX, set bit 0 of the destination. The cleanup_branch function will
5004 then arrange the switch into Thumb mode. */
5005 offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
5006 else
5007 offset = bits (insn, 0, 23) << 2;
5008
5009 if (bit (offset, 25))
5010 offset = offset | ~0x3ffffff;
5011
cca44b1b
JB
5012 dsc->modinsn[0] = ARM_NOP;
5013
7ff120b4 5014 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
cca44b1b
JB
5015 return 0;
5016}
5017
34518530
YQ
5018static int
5019thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
5020 uint16_t insn2, struct regcache *regs,
cfba9872 5021 arm_displaced_step_closure *dsc)
34518530
YQ
5022{
5023 int link = bit (insn2, 14);
5024 int exchange = link && !bit (insn2, 12);
5025 int cond = INST_AL;
5026 long offset = 0;
5027 int j1 = bit (insn2, 13);
5028 int j2 = bit (insn2, 11);
5029 int s = sbits (insn1, 10, 10);
5030 int i1 = !(j1 ^ bit (insn1, 10));
5031 int i2 = !(j2 ^ bit (insn1, 10));
5032
5033 if (!link && !exchange) /* B */
5034 {
5035 offset = (bits (insn2, 0, 10) << 1);
5036 if (bit (insn2, 12)) /* Encoding T4 */
5037 {
5038 offset |= (bits (insn1, 0, 9) << 12)
5039 | (i2 << 22)
5040 | (i1 << 23)
5041 | (s << 24);
5042 cond = INST_AL;
5043 }
5044 else /* Encoding T3 */
5045 {
5046 offset |= (bits (insn1, 0, 5) << 12)
5047 | (j1 << 18)
5048 | (j2 << 19)
5049 | (s << 20);
5050 cond = bits (insn1, 6, 9);
5051 }
5052 }
5053 else
5054 {
5055 offset = (bits (insn1, 0, 9) << 12);
5056 offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
5057 offset |= exchange ?
5058 (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
5059 }
5060
5061 if (debug_displaced)
5062 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s insn "
5063 "%.4x %.4x with offset %.8lx\n",
5064 link ? (exchange) ? "blx" : "bl" : "b",
5065 insn1, insn2, offset);
5066
5067 dsc->modinsn[0] = THUMB_NOP;
5068
5069 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
5070 return 0;
5071}
5072
5073/* Copy B Thumb instructions. */
5074static int
615234c1 5075thumb_copy_b (struct gdbarch *gdbarch, uint16_t insn,
cfba9872 5076 arm_displaced_step_closure *dsc)
34518530
YQ
5077{
5078 unsigned int cond = 0;
5079 int offset = 0;
5080 unsigned short bit_12_15 = bits (insn, 12, 15);
5081 CORE_ADDR from = dsc->insn_addr;
5082
5083 if (bit_12_15 == 0xd)
5084 {
5085 /* offset = SignExtend (imm8:0, 32) */
5086 offset = sbits ((insn << 1), 0, 8);
5087 cond = bits (insn, 8, 11);
5088 }
5089 else if (bit_12_15 == 0xe) /* Encoding T2 */
5090 {
5091 offset = sbits ((insn << 1), 0, 11);
5092 cond = INST_AL;
5093 }
5094
5095 if (debug_displaced)
5096 fprintf_unfiltered (gdb_stdlog,
5097 "displaced: copying b immediate insn %.4x "
5098 "with offset %d\n", insn, offset);
5099
5100 dsc->u.branch.cond = cond;
5101 dsc->u.branch.link = 0;
5102 dsc->u.branch.exchange = 0;
5103 dsc->u.branch.dest = from + 4 + offset;
5104
5105 dsc->modinsn[0] = THUMB_NOP;
5106
5107 dsc->cleanup = &cleanup_branch;
5108
5109 return 0;
5110}
5111
cca44b1b
JB
5112/* Copy BX/BLX with register-specified destinations. */
5113
7ff120b4
YQ
5114static void
5115install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 5116 arm_displaced_step_closure *dsc, int link,
7ff120b4 5117 unsigned int cond, unsigned int rm)
cca44b1b 5118{
cca44b1b
JB
5119 /* Implement {BX,BLX}<cond> <reg>" as:
5120
5121 Preparation: cond <- instruction condition
5122 Insn: mov r0, r0 (nop)
5123 Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
5124
5125 Don't set r14 in cleanup for BX. */
5126
36073a92 5127 dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5128
5129 dsc->u.branch.cond = cond;
5130 dsc->u.branch.link = link;
cca44b1b 5131
7ff120b4 5132 dsc->u.branch.exchange = 1;
cca44b1b
JB
5133
5134 dsc->cleanup = &cleanup_branch;
7ff120b4 5135}
cca44b1b 5136
7ff120b4
YQ
5137static int
5138arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
cfba9872 5139 struct regcache *regs, arm_displaced_step_closure *dsc)
7ff120b4
YQ
5140{
5141 unsigned int cond = bits (insn, 28, 31);
5142 /* BX: x12xxx1x
5143 BLX: x12xxx3x. */
5144 int link = bit (insn, 5);
5145 unsigned int rm = bits (insn, 0, 3);
5146
5147 if (debug_displaced)
5148 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx",
5149 (unsigned long) insn);
5150
5151 dsc->modinsn[0] = ARM_NOP;
5152
5153 install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
cca44b1b
JB
5154 return 0;
5155}
5156
34518530
YQ
5157static int
5158thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
5159 struct regcache *regs,
cfba9872 5160 arm_displaced_step_closure *dsc)
34518530
YQ
5161{
5162 int link = bit (insn, 7);
5163 unsigned int rm = bits (insn, 3, 6);
5164
5165 if (debug_displaced)
5166 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x",
5167 (unsigned short) insn);
5168
5169 dsc->modinsn[0] = THUMB_NOP;
5170
5171 install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);
5172
5173 return 0;
5174}
5175
5176
0963b4bd 5177/* Copy/cleanup arithmetic/logic instruction with immediate RHS. */
cca44b1b
JB
5178
5179static void
6e39997a 5180cleanup_alu_imm (struct gdbarch *gdbarch,
cfba9872 5181 struct regcache *regs, arm_displaced_step_closure *dsc)
cca44b1b 5182{
36073a92 5183 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5184 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5185 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5186 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
5187}
5188
5189static int
7ff120b4 5190arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
cfba9872 5191 arm_displaced_step_closure *dsc)
cca44b1b
JB
5192{
5193 unsigned int rn = bits (insn, 16, 19);
5194 unsigned int rd = bits (insn, 12, 15);
5195 unsigned int op = bits (insn, 21, 24);
5196 int is_mov = (op == 0xd);
5197 ULONGEST rd_val, rn_val;
cca44b1b
JB
5198
5199 if (!insn_references_pc (insn, 0x000ff000ul))
7ff120b4 5200 return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
cca44b1b
JB
5201
5202 if (debug_displaced)
5203 fprintf_unfiltered (gdb_stdlog, "displaced: copying immediate %s insn "
5204 "%.8lx\n", is_mov ? "move" : "ALU",
5205 (unsigned long) insn);
5206
5207 /* Instruction is of form:
5208
5209 <op><cond> rd, [rn,] #imm
5210
5211 Rewrite as:
5212
5213 Preparation: tmp1, tmp2 <- r0, r1;
5214 r0, r1 <- rd, rn
5215 Insn: <op><cond> r0, r1, #imm
5216 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
5217 */
5218
36073a92
YQ
5219 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5220 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5221 rn_val = displaced_read_reg (regs, dsc, rn);
5222 rd_val = displaced_read_reg (regs, dsc, rd);
cca44b1b
JB
5223 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5224 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5225 dsc->rd = rd;
5226
5227 if (is_mov)
5228 dsc->modinsn[0] = insn & 0xfff00fff;
5229 else
5230 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
5231
5232 dsc->cleanup = &cleanup_alu_imm;
5233
5234 return 0;
5235}
5236
34518530
YQ
5237static int
5238thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
5239 uint16_t insn2, struct regcache *regs,
cfba9872 5240 arm_displaced_step_closure *dsc)
34518530
YQ
5241{
5242 unsigned int op = bits (insn1, 5, 8);
5243 unsigned int rn, rm, rd;
5244 ULONGEST rd_val, rn_val;
5245
5246 rn = bits (insn1, 0, 3); /* Rn */
5247 rm = bits (insn2, 0, 3); /* Rm */
5248 rd = bits (insn2, 8, 11); /* Rd */
5249
5250 /* This routine is only called for instruction MOV. */
5251 gdb_assert (op == 0x2 && rn == 0xf);
5252
5253 if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
5254 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);
5255
5256 if (debug_displaced)
5257 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.4x%.4x\n",
5258 "ALU", insn1, insn2);
5259
5260 /* Instruction is of form:
5261
5262 <op><cond> rd, [rn,] #imm
5263
5264 Rewrite as:
5265
5266 Preparation: tmp1, tmp2 <- r0, r1;
5267 r0, r1 <- rd, rn
5268 Insn: <op><cond> r0, r1, #imm
5269 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
5270 */
5271
5272 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5273 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5274 rn_val = displaced_read_reg (regs, dsc, rn);
5275 rd_val = displaced_read_reg (regs, dsc, rd);
5276 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5277 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5278 dsc->rd = rd;
5279
5280 dsc->modinsn[0] = insn1;
5281 dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
5282 dsc->numinsns = 2;
5283
5284 dsc->cleanup = &cleanup_alu_imm;
5285
5286 return 0;
5287}
5288
cca44b1b
JB
5289/* Copy/cleanup arithmetic/logic insns with register RHS. */
5290
5291static void
6e39997a 5292cleanup_alu_reg (struct gdbarch *gdbarch,
cfba9872 5293 struct regcache *regs, arm_displaced_step_closure *dsc)
cca44b1b
JB
5294{
5295 ULONGEST rd_val;
5296 int i;
5297
36073a92 5298 rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5299
5300 for (i = 0; i < 3; i++)
5301 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
5302
5303 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
5304}
5305
7ff120b4
YQ
5306static void
5307install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 5308 arm_displaced_step_closure *dsc,
7ff120b4 5309 unsigned int rd, unsigned int rn, unsigned int rm)
cca44b1b 5310{
cca44b1b 5311 ULONGEST rd_val, rn_val, rm_val;
cca44b1b 5312
cca44b1b
JB
5313 /* Instruction is of form:
5314
5315 <op><cond> rd, [rn,] rm [, <shift>]
5316
5317 Rewrite as:
5318
5319 Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
5320 r0, r1, r2 <- rd, rn, rm
ef713951 5321 Insn: <op><cond> r0, [r1,] r2 [, <shift>]
cca44b1b
JB
5322 Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
5323 */
5324
36073a92
YQ
5325 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5326 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5327 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
5328 rd_val = displaced_read_reg (regs, dsc, rd);
5329 rn_val = displaced_read_reg (regs, dsc, rn);
5330 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5331 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5332 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5333 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
5334 dsc->rd = rd;
5335
7ff120b4
YQ
5336 dsc->cleanup = &cleanup_alu_reg;
5337}
5338
5339static int
5340arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
cfba9872 5341 arm_displaced_step_closure *dsc)
7ff120b4
YQ
5342{
5343 unsigned int op = bits (insn, 21, 24);
5344 int is_mov = (op == 0xd);
5345
5346 if (!insn_references_pc (insn, 0x000ff00ful))
5347 return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);
5348
5349 if (debug_displaced)
5350 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.8lx\n",
5351 is_mov ? "move" : "ALU", (unsigned long) insn);
5352
cca44b1b
JB
5353 if (is_mov)
5354 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
5355 else
5356 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
5357
7ff120b4
YQ
5358 install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
5359 bits (insn, 0, 3));
cca44b1b
JB
5360 return 0;
5361}
5362
34518530
YQ
5363static int
5364thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
5365 struct regcache *regs,
cfba9872 5366 arm_displaced_step_closure *dsc)
34518530 5367{
ef713951 5368 unsigned rm, rd;
34518530 5369
ef713951
YQ
5370 rm = bits (insn, 3, 6);
5371 rd = (bit (insn, 7) << 3) | bits (insn, 0, 2);
34518530 5372
ef713951 5373 if (rd != ARM_PC_REGNUM && rm != ARM_PC_REGNUM)
34518530
YQ
5374 return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);
5375
5376 if (debug_displaced)
ef713951
YQ
5377 fprintf_unfiltered (gdb_stdlog, "displaced: copying ALU reg insn %.4x\n",
5378 (unsigned short) insn);
34518530 5379
ef713951 5380 dsc->modinsn[0] = ((insn & 0xff00) | 0x10);
34518530 5381
ef713951 5382 install_alu_reg (gdbarch, regs, dsc, rd, rd, rm);
34518530
YQ
5383
5384 return 0;
5385}
5386
cca44b1b
JB
5387/* Cleanup/copy arithmetic/logic insns with shifted register RHS. */
5388
5389static void
6e39997a 5390cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
cca44b1b 5391 struct regcache *regs,
cfba9872 5392 arm_displaced_step_closure *dsc)
cca44b1b 5393{
36073a92 5394 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5395 int i;
5396
5397 for (i = 0; i < 4; i++)
5398 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
5399
5400 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
5401}
5402
7ff120b4
YQ
5403static void
5404install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 5405 arm_displaced_step_closure *dsc,
7ff120b4
YQ
5406 unsigned int rd, unsigned int rn, unsigned int rm,
5407 unsigned rs)
cca44b1b 5408{
7ff120b4 5409 int i;
cca44b1b 5410 ULONGEST rd_val, rn_val, rm_val, rs_val;
cca44b1b 5411
cca44b1b
JB
5412 /* Instruction is of form:
5413
5414 <op><cond> rd, [rn,] rm, <shift> rs
5415
5416 Rewrite as:
5417
5418 Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
5419 r0, r1, r2, r3 <- rd, rn, rm, rs
5420 Insn: <op><cond> r0, r1, r2, <shift> r3
5421 Cleanup: tmp5 <- r0
5422 r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
5423 rd <- tmp5
5424 */
5425
5426 for (i = 0; i < 4; i++)
36073a92 5427 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b 5428
36073a92
YQ
5429 rd_val = displaced_read_reg (regs, dsc, rd);
5430 rn_val = displaced_read_reg (regs, dsc, rn);
5431 rm_val = displaced_read_reg (regs, dsc, rm);
5432 rs_val = displaced_read_reg (regs, dsc, rs);
cca44b1b
JB
5433 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5434 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5435 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
5436 displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
5437 dsc->rd = rd;
7ff120b4
YQ
5438 dsc->cleanup = &cleanup_alu_shifted_reg;
5439}
5440
5441static int
5442arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
5443 struct regcache *regs,
cfba9872 5444 arm_displaced_step_closure *dsc)
7ff120b4
YQ
5445{
5446 unsigned int op = bits (insn, 21, 24);
5447 int is_mov = (op == 0xd);
5448 unsigned int rd, rn, rm, rs;
5449
5450 if (!insn_references_pc (insn, 0x000fff0ful))
5451 return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
5452
5453 if (debug_displaced)
5454 fprintf_unfiltered (gdb_stdlog, "displaced: copying shifted reg %s insn "
5455 "%.8lx\n", is_mov ? "move" : "ALU",
5456 (unsigned long) insn);
5457
5458 rn = bits (insn, 16, 19);
5459 rm = bits (insn, 0, 3);
5460 rs = bits (insn, 8, 11);
5461 rd = bits (insn, 12, 15);
cca44b1b
JB
5462
5463 if (is_mov)
5464 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
5465 else
5466 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
5467
7ff120b4 5468 install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);
cca44b1b
JB
5469
5470 return 0;
5471}
5472
5473/* Clean up load instructions. */
5474
5475static void
6e39997a 5476cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 5477 arm_displaced_step_closure *dsc)
cca44b1b
JB
5478{
5479 ULONGEST rt_val, rt_val2 = 0, rn_val;
cca44b1b 5480
36073a92 5481 rt_val = displaced_read_reg (regs, dsc, 0);
cca44b1b 5482 if (dsc->u.ldst.xfersize == 8)
36073a92
YQ
5483 rt_val2 = displaced_read_reg (regs, dsc, 1);
5484 rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
5485
5486 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5487 if (dsc->u.ldst.xfersize > 4)
5488 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5489 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
5490 if (!dsc->u.ldst.immed)
5491 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
5492
5493 /* Handle register writeback. */
5494 if (dsc->u.ldst.writeback)
5495 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
5496 /* Put result in right place. */
5497 displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
5498 if (dsc->u.ldst.xfersize == 8)
5499 displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
5500}
5501
5502/* Clean up store instructions. */
5503
5504static void
6e39997a 5505cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 5506 arm_displaced_step_closure *dsc)
cca44b1b 5507{
36073a92 5508 ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
5509
5510 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5511 if (dsc->u.ldst.xfersize > 4)
5512 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5513 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
5514 if (!dsc->u.ldst.immed)
5515 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
5516 if (!dsc->u.ldst.restore_r4)
5517 displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
5518
5519 /* Writeback. */
5520 if (dsc->u.ldst.writeback)
5521 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
5522}
5523
5524/* Copy "extra" load/store instructions. These are halfword/doubleword
5525 transfers, which have a different encoding to byte/word transfers. */
5526
5527static int
550dc4e2 5528arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unprivileged,
cfba9872 5529 struct regcache *regs, arm_displaced_step_closure *dsc)
cca44b1b
JB
5530{
5531 unsigned int op1 = bits (insn, 20, 24);
5532 unsigned int op2 = bits (insn, 5, 6);
5533 unsigned int rt = bits (insn, 12, 15);
5534 unsigned int rn = bits (insn, 16, 19);
5535 unsigned int rm = bits (insn, 0, 3);
5536 char load[12] = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
5537 char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
5538 int immed = (op1 & 0x4) != 0;
5539 int opcode;
5540 ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
cca44b1b
JB
5541
5542 if (!insn_references_pc (insn, 0x000ff00ful))
7ff120b4 5543 return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);
cca44b1b
JB
5544
5545 if (debug_displaced)
5546 fprintf_unfiltered (gdb_stdlog, "displaced: copying %sextra load/store "
550dc4e2 5547 "insn %.8lx\n", unprivileged ? "unprivileged " : "",
cca44b1b
JB
5548 (unsigned long) insn);
5549
5550 opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
5551
5552 if (opcode < 0)
5553 internal_error (__FILE__, __LINE__,
5554 _("copy_extra_ld_st: instruction decode error"));
5555
36073a92
YQ
5556 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5557 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5558 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 5559 if (!immed)
36073a92 5560 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 5561
36073a92 5562 rt_val = displaced_read_reg (regs, dsc, rt);
cca44b1b 5563 if (bytesize[opcode] == 8)
36073a92
YQ
5564 rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
5565 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 5566 if (!immed)
36073a92 5567 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5568
5569 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
5570 if (bytesize[opcode] == 8)
5571 displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
5572 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
5573 if (!immed)
5574 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
5575
5576 dsc->rd = rt;
5577 dsc->u.ldst.xfersize = bytesize[opcode];
5578 dsc->u.ldst.rn = rn;
5579 dsc->u.ldst.immed = immed;
5580 dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
5581 dsc->u.ldst.restore_r4 = 0;
5582
5583 if (immed)
5584 /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
5585 ->
5586 {ldr,str}<width><cond> r0, [r1,] [r2, #imm]. */
5587 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
5588 else
5589 /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
5590 ->
5591 {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3]. */
5592 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
5593
5594 dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
5595
5596 return 0;
5597}
5598
0f6f04ba 5599/* Copy byte/half word/word loads and stores. */
cca44b1b 5600
7ff120b4 5601static void
0f6f04ba 5602install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 5603 arm_displaced_step_closure *dsc, int load,
0f6f04ba
YQ
5604 int immed, int writeback, int size, int usermode,
5605 int rt, int rm, int rn)
cca44b1b 5606{
cca44b1b 5607 ULONGEST rt_val, rn_val, rm_val = 0;
cca44b1b 5608
36073a92
YQ
5609 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5610 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 5611 if (!immed)
36073a92 5612 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 5613 if (!load)
36073a92 5614 dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);
cca44b1b 5615
36073a92
YQ
5616 rt_val = displaced_read_reg (regs, dsc, rt);
5617 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 5618 if (!immed)
36073a92 5619 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5620
5621 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
5622 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
5623 if (!immed)
5624 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
cca44b1b 5625 dsc->rd = rt;
0f6f04ba 5626 dsc->u.ldst.xfersize = size;
cca44b1b
JB
5627 dsc->u.ldst.rn = rn;
5628 dsc->u.ldst.immed = immed;
7ff120b4 5629 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
5630
5631 /* To write PC we can do:
5632
494e194e
YQ
5633 Before this sequence of instructions:
5634 r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
85102364 5635 r2 is the Rn value got from displaced_read_reg.
494e194e
YQ
5636
5637 Insn1: push {pc} Write address of STR instruction + offset on stack
5638 Insn2: pop {r4} Read it back from stack, r4 = addr(Insn1) + offset
5639 Insn3: sub r4, r4, pc r4 = addr(Insn1) + offset - pc
5640 = addr(Insn1) + offset - addr(Insn3) - 8
5641 = offset - 16
5642 Insn4: add r4, r4, #8 r4 = offset - 8
5643 Insn5: add r0, r0, r4 r0 = from + 8 + offset - 8
5644 = from + offset
5645 Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])
cca44b1b
JB
5646
5647 Otherwise we don't know what value to write for PC, since the offset is
494e194e
YQ
5648 architecture-dependent (sometimes PC+8, sometimes PC+12). More details
5649 of this can be found in Section "Saving from r15" in
5650 http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */
cca44b1b 5651
7ff120b4
YQ
5652 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
5653}
5654
34518530
YQ
5655
5656static int
5657thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
5658 uint16_t insn2, struct regcache *regs,
cfba9872 5659 arm_displaced_step_closure *dsc, int size)
34518530
YQ
5660{
5661 unsigned int u_bit = bit (insn1, 7);
5662 unsigned int rt = bits (insn2, 12, 15);
5663 int imm12 = bits (insn2, 0, 11);
5664 ULONGEST pc_val;
5665
5666 if (debug_displaced)
5667 fprintf_unfiltered (gdb_stdlog,
5668 "displaced: copying ldr pc (0x%x) R%d %c imm12 %.4x\n",
5669 (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
5670 imm12);
5671
5672 if (!u_bit)
5673 imm12 = -1 * imm12;
5674
5675 /* Rewrite instruction LDR Rt imm12 into:
5676
5677 Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12
5678
5679 LDR R0, R2, R3,
5680
5681 Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2]. */
5682
5683
5684 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5685 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
5686 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
5687
5688 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
5689
5690 pc_val = pc_val & 0xfffffffc;
5691
5692 displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
5693 displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);
5694
5695 dsc->rd = rt;
5696
5697 dsc->u.ldst.xfersize = size;
5698 dsc->u.ldst.immed = 0;
5699 dsc->u.ldst.writeback = 0;
5700 dsc->u.ldst.restore_r4 = 0;
5701
5702 /* LDR R0, R2, R3 */
5703 dsc->modinsn[0] = 0xf852;
5704 dsc->modinsn[1] = 0x3;
5705 dsc->numinsns = 2;
5706
5707 dsc->cleanup = &cleanup_load;
5708
5709 return 0;
5710}
5711
5712static int
5713thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
5714 uint16_t insn2, struct regcache *regs,
cfba9872 5715 arm_displaced_step_closure *dsc,
34518530
YQ
5716 int writeback, int immed)
5717{
5718 unsigned int rt = bits (insn2, 12, 15);
5719 unsigned int rn = bits (insn1, 0, 3);
5720 unsigned int rm = bits (insn2, 0, 3); /* Only valid if !immed. */
5721 /* In LDR (register), there is also a register Rm, which is not allowed to
5722 be PC, so we don't have to check it. */
5723
5724 if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
5725 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
5726 dsc);
5727
5728 if (debug_displaced)
5729 fprintf_unfiltered (gdb_stdlog,
5730 "displaced: copying ldr r%d [r%d] insn %.4x%.4x\n",
5731 rt, rn, insn1, insn2);
5732
5733 install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
5734 0, rt, rm, rn);
5735
5736 dsc->u.ldst.restore_r4 = 0;
5737
5738 if (immed)
5739 /* ldr[b]<cond> rt, [rn, #imm], etc.
5740 ->
5741 ldr[b]<cond> r0, [r2, #imm]. */
5742 {
5743 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
5744 dsc->modinsn[1] = insn2 & 0x0fff;
5745 }
5746 else
5747 /* ldr[b]<cond> rt, [rn, rm], etc.
5748 ->
5749 ldr[b]<cond> r0, [r2, r3]. */
5750 {
5751 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
5752 dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
5753 }
5754
5755 dsc->numinsns = 2;
5756
5757 return 0;
5758}
5759
5760
7ff120b4
YQ
5761static int
5762arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
5763 struct regcache *regs,
cfba9872 5764 arm_displaced_step_closure *dsc,
0f6f04ba 5765 int load, int size, int usermode)
7ff120b4
YQ
5766{
5767 int immed = !bit (insn, 25);
5768 int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
5769 unsigned int rt = bits (insn, 12, 15);
5770 unsigned int rn = bits (insn, 16, 19);
5771 unsigned int rm = bits (insn, 0, 3); /* Only valid if !immed. */
5772
5773 if (!insn_references_pc (insn, 0x000ff00ful))
5774 return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);
5775
5776 if (debug_displaced)
5777 fprintf_unfiltered (gdb_stdlog,
5778 "displaced: copying %s%s r%d [r%d] insn %.8lx\n",
0f6f04ba
YQ
5779 load ? (size == 1 ? "ldrb" : "ldr")
5780 : (size == 1 ? "strb" : "str"), usermode ? "t" : "",
7ff120b4
YQ
5781 rt, rn,
5782 (unsigned long) insn);
5783
0f6f04ba
YQ
5784 install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
5785 usermode, rt, rm, rn);
7ff120b4 5786
bf9f652a 5787 if (load || rt != ARM_PC_REGNUM)
cca44b1b
JB
5788 {
5789 dsc->u.ldst.restore_r4 = 0;
5790
5791 if (immed)
5792 /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
5793 ->
5794 {ldr,str}[b]<cond> r0, [r2, #imm]. */
5795 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
5796 else
5797 /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
5798 ->
5799 {ldr,str}[b]<cond> r0, [r2, r3]. */
5800 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
5801 }
5802 else
5803 {
5804 /* We need to use r4 as scratch. Make sure it's restored afterwards. */
5805 dsc->u.ldst.restore_r4 = 1;
494e194e
YQ
5806 dsc->modinsn[0] = 0xe92d8000; /* push {pc} */
5807 dsc->modinsn[1] = 0xe8bd0010; /* pop {r4} */
cca44b1b
JB
5808 dsc->modinsn[2] = 0xe044400f; /* sub r4, r4, pc. */
5809 dsc->modinsn[3] = 0xe2844008; /* add r4, r4, #8. */
5810 dsc->modinsn[4] = 0xe0800004; /* add r0, r0, r4. */
5811
5812 /* As above. */
5813 if (immed)
5814 dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
5815 else
5816 dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
5817
cca44b1b
JB
5818 dsc->numinsns = 6;
5819 }
5820
5821 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
5822
5823 return 0;
5824}
5825
5826/* Cleanup LDM instructions with fully-populated register list. This is an
5827 unfortunate corner case: it's impossible to implement correctly by modifying
5828 the instruction. The issue is as follows: we have an instruction,
5829
5830 ldm rN, {r0-r15}
5831
5832 which we must rewrite to avoid loading PC. A possible solution would be to
5833 do the load in two halves, something like (with suitable cleanup
5834 afterwards):
5835
5836 mov r8, rN
5837 ldm[id][ab] r8!, {r0-r7}
5838 str r7, <temp>
5839 ldm[id][ab] r8, {r7-r14}
5840 <bkpt>
5841
5842 but at present there's no suitable place for <temp>, since the scratch space
5843 is overwritten before the cleanup routine is called. For now, we simply
5844 emulate the instruction. */
5845
5846static void
5847cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 5848 arm_displaced_step_closure *dsc)
cca44b1b 5849{
cca44b1b
JB
5850 int inc = dsc->u.block.increment;
5851 int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
5852 int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
5853 uint32_t regmask = dsc->u.block.regmask;
5854 int regno = inc ? 0 : 15;
5855 CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
5856 int exception_return = dsc->u.block.load && dsc->u.block.user
5857 && (regmask & 0x8000) != 0;
36073a92 5858 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
5859 int do_transfer = condition_true (dsc->u.block.cond, status);
5860 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5861
5862 if (!do_transfer)
5863 return;
5864
5865 /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
5866 sensible we can do here. Complain loudly. */
5867 if (exception_return)
5868 error (_("Cannot single-step exception return"));
5869
5870 /* We don't handle any stores here for now. */
5871 gdb_assert (dsc->u.block.load != 0);
5872
5873 if (debug_displaced)
5874 fprintf_unfiltered (gdb_stdlog, "displaced: emulating block transfer: "
5875 "%s %s %s\n", dsc->u.block.load ? "ldm" : "stm",
5876 dsc->u.block.increment ? "inc" : "dec",
5877 dsc->u.block.before ? "before" : "after");
5878
5879 while (regmask)
5880 {
5881 uint32_t memword;
5882
5883 if (inc)
bf9f652a 5884 while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
cca44b1b
JB
5885 regno++;
5886 else
5887 while (regno >= 0 && (regmask & (1 << regno)) == 0)
5888 regno--;
5889
5890 xfer_addr += bump_before;
5891
5892 memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
5893 displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
5894
5895 xfer_addr += bump_after;
5896
5897 regmask &= ~(1 << regno);
5898 }
5899
5900 if (dsc->u.block.writeback)
5901 displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
5902 CANNOT_WRITE_PC);
5903}
5904
5905/* Clean up an STM which included the PC in the register list. */
5906
5907static void
5908cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 5909 arm_displaced_step_closure *dsc)
cca44b1b 5910{
36073a92 5911 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b 5912 int store_executed = condition_true (dsc->u.block.cond, status);
5f661e03
SM
5913 CORE_ADDR pc_stored_at, transferred_regs
5914 = count_one_bits (dsc->u.block.regmask);
cca44b1b
JB
5915 CORE_ADDR stm_insn_addr;
5916 uint32_t pc_val;
5917 long offset;
5918 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5919
5920 /* If condition code fails, there's nothing else to do. */
5921 if (!store_executed)
5922 return;
5923
5924 if (dsc->u.block.increment)
5925 {
5926 pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
5927
5928 if (dsc->u.block.before)
5929 pc_stored_at += 4;
5930 }
5931 else
5932 {
5933 pc_stored_at = dsc->u.block.xfer_addr;
5934
5935 if (dsc->u.block.before)
5936 pc_stored_at -= 4;
5937 }
5938
5939 pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
5940 stm_insn_addr = dsc->scratch_base;
5941 offset = pc_val - stm_insn_addr;
5942
5943 if (debug_displaced)
5944 fprintf_unfiltered (gdb_stdlog, "displaced: detected PC offset %.8lx for "
5945 "STM instruction\n", offset);
5946
5947 /* Rewrite the stored PC to the proper value for the non-displaced original
5948 instruction. */
5949 write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
5950 dsc->insn_addr + offset);
5951}
5952
5953/* Clean up an LDM which includes the PC in the register list. We clumped all
5954 the registers in the transferred list into a contiguous range r0...rX (to
5955 avoid loading PC directly and losing control of the debugged program), so we
5956 must undo that here. */
5957
5958static void
6e39997a 5959cleanup_block_load_pc (struct gdbarch *gdbarch,
cca44b1b 5960 struct regcache *regs,
cfba9872 5961 arm_displaced_step_closure *dsc)
cca44b1b 5962{
36073a92 5963 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
22e048c9 5964 int load_executed = condition_true (dsc->u.block.cond, status);
bf9f652a 5965 unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
5f661e03 5966 unsigned int regs_loaded = count_one_bits (mask);
cca44b1b
JB
5967 unsigned int num_to_shuffle = regs_loaded, clobbered;
5968
5969 /* The method employed here will fail if the register list is fully populated
5970 (we need to avoid loading PC directly). */
5971 gdb_assert (num_to_shuffle < 16);
5972
5973 if (!load_executed)
5974 return;
5975
5976 clobbered = (1 << num_to_shuffle) - 1;
5977
5978 while (num_to_shuffle > 0)
5979 {
5980 if ((mask & (1 << write_reg)) != 0)
5981 {
5982 unsigned int read_reg = num_to_shuffle - 1;
5983
5984 if (read_reg != write_reg)
5985 {
36073a92 5986 ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
cca44b1b
JB
5987 displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
5988 if (debug_displaced)
5989 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: move "
5990 "loaded register r%d to r%d\n"), read_reg,
5991 write_reg);
5992 }
5993 else if (debug_displaced)
5994 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: register "
5995 "r%d already in the right place\n"),
5996 write_reg);
5997
5998 clobbered &= ~(1 << write_reg);
5999
6000 num_to_shuffle--;
6001 }
6002
6003 write_reg--;
6004 }
6005
6006 /* Restore any registers we scribbled over. */
6007 for (write_reg = 0; clobbered != 0; write_reg++)
6008 {
6009 if ((clobbered & (1 << write_reg)) != 0)
6010 {
6011 displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
6012 CANNOT_WRITE_PC);
6013 if (debug_displaced)
6014 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: restored "
6015 "clobbered register r%d\n"), write_reg);
6016 clobbered &= ~(1 << write_reg);
6017 }
6018 }
6019
6020 /* Perform register writeback manually. */
6021 if (dsc->u.block.writeback)
6022 {
6023 ULONGEST new_rn_val = dsc->u.block.xfer_addr;
6024
6025 if (dsc->u.block.increment)
6026 new_rn_val += regs_loaded * 4;
6027 else
6028 new_rn_val -= regs_loaded * 4;
6029
6030 displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
6031 CANNOT_WRITE_PC);
6032 }
6033}
6034
6035/* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
6036 in user-level code (in particular exception return, ldm rn, {...pc}^). */
6037
6038static int
7ff120b4
YQ
6039arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
6040 struct regcache *regs,
cfba9872 6041 arm_displaced_step_closure *dsc)
cca44b1b
JB
6042{
6043 int load = bit (insn, 20);
6044 int user = bit (insn, 22);
6045 int increment = bit (insn, 23);
6046 int before = bit (insn, 24);
6047 int writeback = bit (insn, 21);
6048 int rn = bits (insn, 16, 19);
cca44b1b 6049
0963b4bd
MS
6050 /* Block transfers which don't mention PC can be run directly
6051 out-of-line. */
bf9f652a 6052 if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
7ff120b4 6053 return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
cca44b1b 6054
bf9f652a 6055 if (rn == ARM_PC_REGNUM)
cca44b1b 6056 {
0963b4bd
MS
6057 warning (_("displaced: Unpredictable LDM or STM with "
6058 "base register r15"));
7ff120b4 6059 return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
cca44b1b
JB
6060 }
6061
6062 if (debug_displaced)
6063 fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
6064 "%.8lx\n", (unsigned long) insn);
6065
36073a92 6066 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b
JB
6067 dsc->u.block.rn = rn;
6068
6069 dsc->u.block.load = load;
6070 dsc->u.block.user = user;
6071 dsc->u.block.increment = increment;
6072 dsc->u.block.before = before;
6073 dsc->u.block.writeback = writeback;
6074 dsc->u.block.cond = bits (insn, 28, 31);
6075
6076 dsc->u.block.regmask = insn & 0xffff;
6077
6078 if (load)
6079 {
6080 if ((insn & 0xffff) == 0xffff)
6081 {
6082 /* LDM with a fully-populated register list. This case is
6083 particularly tricky. Implement for now by fully emulating the
6084 instruction (which might not behave perfectly in all cases, but
6085 these instructions should be rare enough for that not to matter
6086 too much). */
6087 dsc->modinsn[0] = ARM_NOP;
6088
6089 dsc->cleanup = &cleanup_block_load_all;
6090 }
6091 else
6092 {
6093 /* LDM of a list of registers which includes PC. Implement by
6094 rewriting the list of registers to be transferred into a
6095 contiguous chunk r0...rX before doing the transfer, then shuffling
6096 registers into the correct places in the cleanup routine. */
6097 unsigned int regmask = insn & 0xffff;
5f661e03 6098 unsigned int num_in_list = count_one_bits (regmask), new_regmask;
bec2ab5a 6099 unsigned int i;
cca44b1b
JB
6100
6101 for (i = 0; i < num_in_list; i++)
36073a92 6102 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b
JB
6103
6104 /* Writeback makes things complicated. We need to avoid clobbering
6105 the base register with one of the registers in our modified
6106 register list, but just using a different register can't work in
6107 all cases, e.g.:
6108
6109 ldm r14!, {r0-r13,pc}
6110
6111 which would need to be rewritten as:
6112
6113 ldm rN!, {r0-r14}
6114
6115 but that can't work, because there's no free register for N.
6116
6117 Solve this by turning off the writeback bit, and emulating
6118 writeback manually in the cleanup routine. */
6119
6120 if (writeback)
6121 insn &= ~(1 << 21);
6122
6123 new_regmask = (1 << num_in_list) - 1;
6124
6125 if (debug_displaced)
6126 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
6127 "{..., pc}: original reg list %.4x, modified "
6128 "list %.4x\n"), rn, writeback ? "!" : "",
6129 (int) insn & 0xffff, new_regmask);
6130
6131 dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
6132
6133 dsc->cleanup = &cleanup_block_load_pc;
6134 }
6135 }
6136 else
6137 {
6138 /* STM of a list of registers which includes PC. Run the instruction
6139 as-is, but out of line: this will store the wrong value for the PC,
6140 so we must manually fix up the memory in the cleanup routine.
6141 Doing things this way has the advantage that we can auto-detect
6142 the offset of the PC write (which is architecture-dependent) in
6143 the cleanup routine. */
6144 dsc->modinsn[0] = insn;
6145
6146 dsc->cleanup = &cleanup_block_store_pc;
6147 }
6148
6149 return 0;
6150}
6151
34518530
YQ
6152static int
6153thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
6154 struct regcache *regs,
cfba9872 6155 arm_displaced_step_closure *dsc)
cca44b1b 6156{
34518530
YQ
6157 int rn = bits (insn1, 0, 3);
6158 int load = bit (insn1, 4);
6159 int writeback = bit (insn1, 5);
cca44b1b 6160
34518530
YQ
6161 /* Block transfers which don't mention PC can be run directly
6162 out-of-line. */
6163 if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
6164 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);
7ff120b4 6165
34518530
YQ
6166 if (rn == ARM_PC_REGNUM)
6167 {
6168 warning (_("displaced: Unpredictable LDM or STM with "
6169 "base register r15"));
6170 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6171 "unpredictable ldm/stm", dsc);
6172 }
cca44b1b
JB
6173
6174 if (debug_displaced)
34518530
YQ
6175 fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
6176 "%.4x%.4x\n", insn1, insn2);
cca44b1b 6177
34518530
YQ
6178 /* Clear bit 13, since it should be always zero. */
6179 dsc->u.block.regmask = (insn2 & 0xdfff);
6180 dsc->u.block.rn = rn;
cca44b1b 6181
34518530
YQ
6182 dsc->u.block.load = load;
6183 dsc->u.block.user = 0;
6184 dsc->u.block.increment = bit (insn1, 7);
6185 dsc->u.block.before = bit (insn1, 8);
6186 dsc->u.block.writeback = writeback;
6187 dsc->u.block.cond = INST_AL;
6188 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b 6189
34518530
YQ
6190 if (load)
6191 {
6192 if (dsc->u.block.regmask == 0xffff)
6193 {
6194 /* This branch is impossible to happen. */
6195 gdb_assert (0);
6196 }
6197 else
6198 {
6199 unsigned int regmask = dsc->u.block.regmask;
5f661e03 6200 unsigned int num_in_list = count_one_bits (regmask), new_regmask;
bec2ab5a 6201 unsigned int i;
34518530
YQ
6202
6203 for (i = 0; i < num_in_list; i++)
6204 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
6205
6206 if (writeback)
6207 insn1 &= ~(1 << 5);
6208
6209 new_regmask = (1 << num_in_list) - 1;
6210
6211 if (debug_displaced)
6212 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
6213 "{..., pc}: original reg list %.4x, modified "
6214 "list %.4x\n"), rn, writeback ? "!" : "",
6215 (int) dsc->u.block.regmask, new_regmask);
6216
6217 dsc->modinsn[0] = insn1;
6218 dsc->modinsn[1] = (new_regmask & 0xffff);
6219 dsc->numinsns = 2;
6220
6221 dsc->cleanup = &cleanup_block_load_pc;
6222 }
6223 }
6224 else
6225 {
6226 dsc->modinsn[0] = insn1;
6227 dsc->modinsn[1] = insn2;
6228 dsc->numinsns = 2;
6229 dsc->cleanup = &cleanup_block_store_pc;
6230 }
6231 return 0;
6232}
6233
d9311bfa
AT
6234/* Wrapper over read_memory_unsigned_integer for use in arm_get_next_pcs.
6235 This is used to avoid a dependency on BFD's bfd_endian enum. */
6236
6237ULONGEST
6238arm_get_next_pcs_read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
6239 int byte_order)
6240{
5f2dfcfd
AT
6241 return read_memory_unsigned_integer (memaddr, len,
6242 (enum bfd_endian) byte_order);
d9311bfa
AT
6243}
6244
6245/* Wrapper over gdbarch_addr_bits_remove for use in arm_get_next_pcs. */
6246
6247CORE_ADDR
6248arm_get_next_pcs_addr_bits_remove (struct arm_get_next_pcs *self,
6249 CORE_ADDR val)
6250{
ac7936df 6251 return gdbarch_addr_bits_remove (self->regcache->arch (), val);
d9311bfa
AT
6252}
6253
6254/* Wrapper over syscall_next_pc for use in get_next_pcs. */
6255
e7cf25a8 6256static CORE_ADDR
553cb527 6257arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self)
d9311bfa 6258{
d9311bfa
AT
6259 return 0;
6260}
6261
6262/* Wrapper over arm_is_thumb for use in arm_get_next_pcs. */
6263
6264int
6265arm_get_next_pcs_is_thumb (struct arm_get_next_pcs *self)
6266{
6267 return arm_is_thumb (self->regcache);
6268}
6269
6270/* single_step() is called just before we want to resume the inferior,
6271 if we want to single-step it but there is no hardware or kernel
6272 single-step support. We find the target of the coming instructions
6273 and breakpoint them. */
6274
a0ff9e1a 6275std::vector<CORE_ADDR>
f5ea389a 6276arm_software_single_step (struct regcache *regcache)
d9311bfa 6277{
ac7936df 6278 struct gdbarch *gdbarch = regcache->arch ();
d9311bfa 6279 struct arm_get_next_pcs next_pcs_ctx;
d9311bfa
AT
6280
6281 arm_get_next_pcs_ctor (&next_pcs_ctx,
6282 &arm_get_next_pcs_ops,
6283 gdbarch_byte_order (gdbarch),
6284 gdbarch_byte_order_for_code (gdbarch),
1b451dda 6285 0,
d9311bfa
AT
6286 regcache);
6287
a0ff9e1a 6288 std::vector<CORE_ADDR> next_pcs = arm_get_next_pcs (&next_pcs_ctx);
d9311bfa 6289
a0ff9e1a
SM
6290 for (CORE_ADDR &pc_ref : next_pcs)
6291 pc_ref = gdbarch_addr_bits_remove (gdbarch, pc_ref);
d9311bfa 6292
93f9a11f 6293 return next_pcs;
d9311bfa
AT
6294}
6295
34518530
YQ
6296/* Cleanup/copy SVC (SWI) instructions. These two functions are overridden
6297 for Linux, where some SVC instructions must be treated specially. */
6298
6299static void
6300cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 6301 arm_displaced_step_closure *dsc)
34518530
YQ
6302{
6303 CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
6304
6305 if (debug_displaced)
6306 fprintf_unfiltered (gdb_stdlog, "displaced: cleanup for svc, resume at "
6307 "%.8lx\n", (unsigned long) resume_addr);
6308
6309 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
6310}
6311
6312
85102364 6313/* Common copy routine for svc instruction. */
34518530
YQ
6314
6315static int
6316install_svc (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 6317 arm_displaced_step_closure *dsc)
34518530
YQ
6318{
6319 /* Preparation: none.
6320 Insn: unmodified svc.
6321 Cleanup: pc <- insn_addr + insn_size. */
6322
6323 /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
6324 instruction. */
6325 dsc->wrote_to_pc = 1;
6326
6327 /* Allow OS-specific code to override SVC handling. */
bd18283a
YQ
6328 if (dsc->u.svc.copy_svc_os)
6329 return dsc->u.svc.copy_svc_os (gdbarch, regs, dsc);
6330 else
6331 {
6332 dsc->cleanup = &cleanup_svc;
6333 return 0;
6334 }
34518530
YQ
6335}
6336
6337static int
6338arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
cfba9872 6339 struct regcache *regs, arm_displaced_step_closure *dsc)
34518530
YQ
6340{
6341
6342 if (debug_displaced)
6343 fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.8lx\n",
6344 (unsigned long) insn);
6345
6346 dsc->modinsn[0] = insn;
6347
6348 return install_svc (gdbarch, regs, dsc);
6349}
6350
6351static int
6352thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
cfba9872 6353 struct regcache *regs, arm_displaced_step_closure *dsc)
34518530
YQ
6354{
6355
6356 if (debug_displaced)
6357 fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.4x\n",
6358 insn);
bd18283a 6359
34518530
YQ
6360 dsc->modinsn[0] = insn;
6361
6362 return install_svc (gdbarch, regs, dsc);
cca44b1b
JB
6363}
6364
6365/* Copy undefined instructions. */
6366
6367static int
7ff120b4 6368arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
cfba9872 6369 arm_displaced_step_closure *dsc)
cca44b1b
JB
6370{
6371 if (debug_displaced)
0963b4bd
MS
6372 fprintf_unfiltered (gdb_stdlog,
6373 "displaced: copying undefined insn %.8lx\n",
cca44b1b
JB
6374 (unsigned long) insn);
6375
6376 dsc->modinsn[0] = insn;
6377
6378 return 0;
6379}
6380
34518530
YQ
6381static int
6382thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
cfba9872 6383 arm_displaced_step_closure *dsc)
34518530
YQ
6384{
6385
6386 if (debug_displaced)
6387 fprintf_unfiltered (gdb_stdlog, "displaced: copying undefined insn "
6388 "%.4x %.4x\n", (unsigned short) insn1,
6389 (unsigned short) insn2);
6390
6391 dsc->modinsn[0] = insn1;
6392 dsc->modinsn[1] = insn2;
6393 dsc->numinsns = 2;
6394
6395 return 0;
6396}
6397
cca44b1b
JB
6398/* Copy unpredictable instructions. */
6399
6400static int
7ff120b4 6401arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
cfba9872 6402 arm_displaced_step_closure *dsc)
cca44b1b
JB
6403{
6404 if (debug_displaced)
6405 fprintf_unfiltered (gdb_stdlog, "displaced: copying unpredictable insn "
6406 "%.8lx\n", (unsigned long) insn);
6407
6408 dsc->modinsn[0] = insn;
6409
6410 return 0;
6411}
6412
6413/* The decode_* functions are instruction decoding helpers. They mostly follow
6414 the presentation in the ARM ARM. */
6415
6416static int
7ff120b4
YQ
6417arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
6418 struct regcache *regs,
cfba9872 6419 arm_displaced_step_closure *dsc)
cca44b1b
JB
6420{
6421 unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
6422 unsigned int rn = bits (insn, 16, 19);
6423
2f924de6 6424 if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0x1) == 0x0)
7ff120b4 6425 return arm_copy_unmodified (gdbarch, insn, "cps", dsc);
2f924de6 6426 else if (op1 == 0x10 && op2 == 0x0 && (rn & 0x1) == 0x1)
7ff120b4 6427 return arm_copy_unmodified (gdbarch, insn, "setend", dsc);
cca44b1b 6428 else if ((op1 & 0x60) == 0x20)
7ff120b4 6429 return arm_copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
cca44b1b 6430 else if ((op1 & 0x71) == 0x40)
7ff120b4
YQ
6431 return arm_copy_unmodified (gdbarch, insn, "neon elt/struct load/store",
6432 dsc);
cca44b1b 6433 else if ((op1 & 0x77) == 0x41)
7ff120b4 6434 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 6435 else if ((op1 & 0x77) == 0x45)
7ff120b4 6436 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pli. */
cca44b1b
JB
6437 else if ((op1 & 0x77) == 0x51)
6438 {
6439 if (rn != 0xf)
7ff120b4 6440 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b 6441 else
7ff120b4 6442 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
6443 }
6444 else if ((op1 & 0x77) == 0x55)
7ff120b4 6445 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b
JB
6446 else if (op1 == 0x57)
6447 switch (op2)
6448 {
7ff120b4
YQ
6449 case 0x1: return arm_copy_unmodified (gdbarch, insn, "clrex", dsc);
6450 case 0x4: return arm_copy_unmodified (gdbarch, insn, "dsb", dsc);
6451 case 0x5: return arm_copy_unmodified (gdbarch, insn, "dmb", dsc);
6452 case 0x6: return arm_copy_unmodified (gdbarch, insn, "isb", dsc);
6453 default: return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
6454 }
6455 else if ((op1 & 0x63) == 0x43)
7ff120b4 6456 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
6457 else if ((op2 & 0x1) == 0x0)
6458 switch (op1 & ~0x80)
6459 {
6460 case 0x61:
7ff120b4 6461 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 6462 case 0x65:
7ff120b4 6463 return arm_copy_preload_reg (gdbarch, insn, regs, dsc); /* pli reg. */
cca44b1b
JB
6464 case 0x71: case 0x75:
6465 /* pld/pldw reg. */
7ff120b4 6466 return arm_copy_preload_reg (gdbarch, insn, regs, dsc);
cca44b1b 6467 case 0x63: case 0x67: case 0x73: case 0x77:
7ff120b4 6468 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b 6469 default:
7ff120b4 6470 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6471 }
6472 else
7ff120b4 6473 return arm_copy_undef (gdbarch, insn, dsc); /* Probably unreachable. */
cca44b1b
JB
6474}
6475
6476static int
7ff120b4
YQ
6477arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
6478 struct regcache *regs,
cfba9872 6479 arm_displaced_step_closure *dsc)
cca44b1b
JB
6480{
6481 if (bit (insn, 27) == 0)
7ff120b4 6482 return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
cca44b1b
JB
6483 /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx. */
6484 else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
6485 {
6486 case 0x0: case 0x2:
7ff120b4 6487 return arm_copy_unmodified (gdbarch, insn, "srs", dsc);
cca44b1b
JB
6488
6489 case 0x1: case 0x3:
7ff120b4 6490 return arm_copy_unmodified (gdbarch, insn, "rfe", dsc);
cca44b1b
JB
6491
6492 case 0x4: case 0x5: case 0x6: case 0x7:
7ff120b4 6493 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b
JB
6494
6495 case 0x8:
6496 switch ((insn & 0xe00000) >> 21)
6497 {
6498 case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
6499 /* stc/stc2. */
7ff120b4 6500 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
6501
6502 case 0x2:
7ff120b4 6503 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b
JB
6504
6505 default:
7ff120b4 6506 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6507 }
6508
6509 case 0x9:
6510 {
6511 int rn_f = (bits (insn, 16, 19) == 0xf);
6512 switch ((insn & 0xe00000) >> 21)
6513 {
6514 case 0x1: case 0x3:
6515 /* ldc/ldc2 imm (undefined for rn == pc). */
7ff120b4
YQ
6516 return rn_f ? arm_copy_undef (gdbarch, insn, dsc)
6517 : arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
6518
6519 case 0x2:
7ff120b4 6520 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
6521
6522 case 0x4: case 0x5: case 0x6: case 0x7:
6523 /* ldc/ldc2 lit (undefined for rn != pc). */
7ff120b4
YQ
6524 return rn_f ? arm_copy_copro_load_store (gdbarch, insn, regs, dsc)
6525 : arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6526
6527 default:
7ff120b4 6528 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6529 }
6530 }
6531
6532 case 0xa:
7ff120b4 6533 return arm_copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
cca44b1b
JB
6534
6535 case 0xb:
6536 if (bits (insn, 16, 19) == 0xf)
6537 /* ldc/ldc2 lit. */
7ff120b4 6538 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 6539 else
7ff120b4 6540 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6541
6542 case 0xc:
6543 if (bit (insn, 4))
7ff120b4 6544 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 6545 else
7ff120b4 6546 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
6547
6548 case 0xd:
6549 if (bit (insn, 4))
7ff120b4 6550 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 6551 else
7ff120b4 6552 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
6553
6554 default:
7ff120b4 6555 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6556 }
6557}
6558
6559/* Decode miscellaneous instructions in dp/misc encoding space. */
6560
6561static int
7ff120b4
YQ
6562arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
6563 struct regcache *regs,
cfba9872 6564 arm_displaced_step_closure *dsc)
cca44b1b
JB
6565{
6566 unsigned int op2 = bits (insn, 4, 6);
6567 unsigned int op = bits (insn, 21, 22);
cca44b1b
JB
6568
6569 switch (op2)
6570 {
6571 case 0x0:
7ff120b4 6572 return arm_copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
cca44b1b
JB
6573
6574 case 0x1:
6575 if (op == 0x1) /* bx. */
7ff120b4 6576 return arm_copy_bx_blx_reg (gdbarch, insn, regs, dsc);
cca44b1b 6577 else if (op == 0x3)
7ff120b4 6578 return arm_copy_unmodified (gdbarch, insn, "clz", dsc);
cca44b1b 6579 else
7ff120b4 6580 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6581
6582 case 0x2:
6583 if (op == 0x1)
6584 /* Not really supported. */
7ff120b4 6585 return arm_copy_unmodified (gdbarch, insn, "bxj", dsc);
cca44b1b 6586 else
7ff120b4 6587 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6588
6589 case 0x3:
6590 if (op == 0x1)
7ff120b4 6591 return arm_copy_bx_blx_reg (gdbarch, insn,
0963b4bd 6592 regs, dsc); /* blx register. */
cca44b1b 6593 else
7ff120b4 6594 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6595
6596 case 0x5:
7ff120b4 6597 return arm_copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
cca44b1b
JB
6598
6599 case 0x7:
6600 if (op == 0x1)
7ff120b4 6601 return arm_copy_unmodified (gdbarch, insn, "bkpt", dsc);
cca44b1b
JB
6602 else if (op == 0x3)
6603 /* Not really supported. */
7ff120b4 6604 return arm_copy_unmodified (gdbarch, insn, "smc", dsc);
86a73007 6605 /* Fall through. */
cca44b1b
JB
6606
6607 default:
7ff120b4 6608 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6609 }
6610}
6611
6612static int
7ff120b4
YQ
6613arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
6614 struct regcache *regs,
cfba9872 6615 arm_displaced_step_closure *dsc)
cca44b1b
JB
6616{
6617 if (bit (insn, 25))
6618 switch (bits (insn, 20, 24))
6619 {
6620 case 0x10:
7ff120b4 6621 return arm_copy_unmodified (gdbarch, insn, "movw", dsc);
cca44b1b
JB
6622
6623 case 0x14:
7ff120b4 6624 return arm_copy_unmodified (gdbarch, insn, "movt", dsc);
cca44b1b
JB
6625
6626 case 0x12: case 0x16:
7ff120b4 6627 return arm_copy_unmodified (gdbarch, insn, "msr imm", dsc);
cca44b1b
JB
6628
6629 default:
7ff120b4 6630 return arm_copy_alu_imm (gdbarch, insn, regs, dsc);
cca44b1b
JB
6631 }
6632 else
6633 {
6634 uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
6635
6636 if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
7ff120b4 6637 return arm_copy_alu_reg (gdbarch, insn, regs, dsc);
cca44b1b 6638 else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
7ff120b4 6639 return arm_copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
cca44b1b 6640 else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
7ff120b4 6641 return arm_decode_miscellaneous (gdbarch, insn, regs, dsc);
cca44b1b 6642 else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
7ff120b4 6643 return arm_copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
cca44b1b 6644 else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
7ff120b4 6645 return arm_copy_unmodified (gdbarch, insn, "mul/mla", dsc);
cca44b1b 6646 else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
7ff120b4 6647 return arm_copy_unmodified (gdbarch, insn, "synch", dsc);
cca44b1b 6648 else if (op2 == 0xb || (op2 & 0xd) == 0xd)
550dc4e2 6649 /* 2nd arg means "unprivileged". */
7ff120b4
YQ
6650 return arm_copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
6651 dsc);
cca44b1b
JB
6652 }
6653
6654 /* Should be unreachable. */
6655 return 1;
6656}
6657
6658static int
7ff120b4
YQ
6659arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
6660 struct regcache *regs,
cfba9872 6661 arm_displaced_step_closure *dsc)
cca44b1b
JB
6662{
6663 int a = bit (insn, 25), b = bit (insn, 4);
6664 uint32_t op1 = bits (insn, 20, 24);
cca44b1b
JB
6665
6666 if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
6667 || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
0f6f04ba 6668 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 0);
cca44b1b
JB
6669 else if ((!a && (op1 & 0x17) == 0x02)
6670 || (a && (op1 & 0x17) == 0x02 && !b))
0f6f04ba 6671 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 1);
cca44b1b
JB
6672 else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
6673 || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
0f6f04ba 6674 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 0);
cca44b1b
JB
6675 else if ((!a && (op1 & 0x17) == 0x03)
6676 || (a && (op1 & 0x17) == 0x03 && !b))
0f6f04ba 6677 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 1);
cca44b1b
JB
6678 else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
6679 || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
7ff120b4 6680 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
cca44b1b
JB
6681 else if ((!a && (op1 & 0x17) == 0x06)
6682 || (a && (op1 & 0x17) == 0x06 && !b))
7ff120b4 6683 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
cca44b1b
JB
6684 else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
6685 || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
7ff120b4 6686 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
cca44b1b
JB
6687 else if ((!a && (op1 & 0x17) == 0x07)
6688 || (a && (op1 & 0x17) == 0x07 && !b))
7ff120b4 6689 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
cca44b1b
JB
6690
6691 /* Should be unreachable. */
6692 return 1;
6693}
6694
6695static int
7ff120b4 6696arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
cfba9872 6697 arm_displaced_step_closure *dsc)
cca44b1b
JB
6698{
6699 switch (bits (insn, 20, 24))
6700 {
6701 case 0x00: case 0x01: case 0x02: case 0x03:
7ff120b4 6702 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
cca44b1b
JB
6703
6704 case 0x04: case 0x05: case 0x06: case 0x07:
7ff120b4 6705 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
cca44b1b
JB
6706
6707 case 0x08: case 0x09: case 0x0a: case 0x0b:
6708 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
7ff120b4 6709 return arm_copy_unmodified (gdbarch, insn,
cca44b1b
JB
6710 "decode/pack/unpack/saturate/reverse", dsc);
6711
6712 case 0x18:
6713 if (bits (insn, 5, 7) == 0) /* op2. */
6714 {
6715 if (bits (insn, 12, 15) == 0xf)
7ff120b4 6716 return arm_copy_unmodified (gdbarch, insn, "usad8", dsc);
cca44b1b 6717 else
7ff120b4 6718 return arm_copy_unmodified (gdbarch, insn, "usada8", dsc);
cca44b1b
JB
6719 }
6720 else
7ff120b4 6721 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6722
6723 case 0x1a: case 0x1b:
6724 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 6725 return arm_copy_unmodified (gdbarch, insn, "sbfx", dsc);
cca44b1b 6726 else
7ff120b4 6727 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6728
6729 case 0x1c: case 0x1d:
6730 if (bits (insn, 5, 6) == 0x0) /* op2[1:0]. */
6731 {
6732 if (bits (insn, 0, 3) == 0xf)
7ff120b4 6733 return arm_copy_unmodified (gdbarch, insn, "bfc", dsc);
cca44b1b 6734 else
7ff120b4 6735 return arm_copy_unmodified (gdbarch, insn, "bfi", dsc);
cca44b1b
JB
6736 }
6737 else
7ff120b4 6738 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6739
6740 case 0x1e: case 0x1f:
6741 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 6742 return arm_copy_unmodified (gdbarch, insn, "ubfx", dsc);
cca44b1b 6743 else
7ff120b4 6744 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6745 }
6746
6747 /* Should be unreachable. */
6748 return 1;
6749}
6750
6751static int
615234c1 6752arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, uint32_t insn,
7ff120b4 6753 struct regcache *regs,
cfba9872 6754 arm_displaced_step_closure *dsc)
cca44b1b
JB
6755{
6756 if (bit (insn, 25))
7ff120b4 6757 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b 6758 else
7ff120b4 6759 return arm_copy_block_xfer (gdbarch, insn, regs, dsc);
cca44b1b
JB
6760}
6761
6762static int
7ff120b4
YQ
6763arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
6764 struct regcache *regs,
cfba9872 6765 arm_displaced_step_closure *dsc)
cca44b1b
JB
6766{
6767 unsigned int opcode = bits (insn, 20, 24);
6768
6769 switch (opcode)
6770 {
6771 case 0x04: case 0x05: /* VFP/Neon mrrc/mcrr. */
7ff120b4 6772 return arm_copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
cca44b1b
JB
6773
6774 case 0x08: case 0x0a: case 0x0c: case 0x0e:
6775 case 0x12: case 0x16:
7ff120b4 6776 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
cca44b1b
JB
6777
6778 case 0x09: case 0x0b: case 0x0d: case 0x0f:
6779 case 0x13: case 0x17:
7ff120b4 6780 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
cca44b1b
JB
6781
6782 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
6783 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
6784 /* Note: no writeback for these instructions. Bit 25 will always be
6785 zero though (via caller), so the following works OK. */
7ff120b4 6786 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
6787 }
6788
6789 /* Should be unreachable. */
6790 return 1;
6791}
6792
34518530
YQ
6793/* Decode shifted register instructions. */
6794
6795static int
6796thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
6797 uint16_t insn2, struct regcache *regs,
cfba9872 6798 arm_displaced_step_closure *dsc)
34518530
YQ
6799{
6800 /* PC is only allowed to be used in instruction MOV. */
6801
6802 unsigned int op = bits (insn1, 5, 8);
6803 unsigned int rn = bits (insn1, 0, 3);
6804
6805 if (op == 0x2 && rn == 0xf) /* MOV */
6806 return thumb2_copy_alu_imm (gdbarch, insn1, insn2, regs, dsc);
6807 else
6808 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6809 "dp (shift reg)", dsc);
6810}
6811
6812
6813/* Decode extension register load/store. Exactly the same as
6814 arm_decode_ext_reg_ld_st. */
6815
6816static int
6817thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
6818 uint16_t insn2, struct regcache *regs,
cfba9872 6819 arm_displaced_step_closure *dsc)
34518530
YQ
6820{
6821 unsigned int opcode = bits (insn1, 4, 8);
6822
6823 switch (opcode)
6824 {
6825 case 0x04: case 0x05:
6826 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6827 "vfp/neon vmov", dsc);
6828
6829 case 0x08: case 0x0c: /* 01x00 */
6830 case 0x0a: case 0x0e: /* 01x10 */
6831 case 0x12: case 0x16: /* 10x10 */
6832 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6833 "vfp/neon vstm/vpush", dsc);
6834
6835 case 0x09: case 0x0d: /* 01x01 */
6836 case 0x0b: case 0x0f: /* 01x11 */
6837 case 0x13: case 0x17: /* 10x11 */
6838 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6839 "vfp/neon vldm/vpop", dsc);
6840
6841 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
6842 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6843 "vstr", dsc);
6844 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
6845 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2, regs, dsc);
6846 }
6847
6848 /* Should be unreachable. */
6849 return 1;
6850}
6851
cca44b1b 6852static int
12545665 6853arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn,
cfba9872 6854 struct regcache *regs, arm_displaced_step_closure *dsc)
cca44b1b
JB
6855{
6856 unsigned int op1 = bits (insn, 20, 25);
6857 int op = bit (insn, 4);
6858 unsigned int coproc = bits (insn, 8, 11);
cca44b1b
JB
6859
6860 if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
7ff120b4 6861 return arm_decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
cca44b1b
JB
6862 else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
6863 && (coproc & 0xe) != 0xa)
6864 /* stc/stc2. */
7ff120b4 6865 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
6866 else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
6867 && (coproc & 0xe) != 0xa)
6868 /* ldc/ldc2 imm/lit. */
7ff120b4 6869 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 6870 else if ((op1 & 0x3e) == 0x00)
7ff120b4 6871 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b 6872 else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
7ff120b4 6873 return arm_copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
cca44b1b 6874 else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
7ff120b4 6875 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b 6876 else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
7ff120b4 6877 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
6878 else if ((op1 & 0x30) == 0x20 && !op)
6879 {
6880 if ((coproc & 0xe) == 0xa)
7ff120b4 6881 return arm_copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
cca44b1b 6882 else
7ff120b4 6883 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
6884 }
6885 else if ((op1 & 0x30) == 0x20 && op)
7ff120b4 6886 return arm_copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
cca44b1b 6887 else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
7ff120b4 6888 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 6889 else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
7ff120b4 6890 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 6891 else if ((op1 & 0x30) == 0x30)
7ff120b4 6892 return arm_copy_svc (gdbarch, insn, regs, dsc);
cca44b1b 6893 else
7ff120b4 6894 return arm_copy_undef (gdbarch, insn, dsc); /* Possibly unreachable. */
cca44b1b
JB
6895}
6896
34518530
YQ
6897static int
6898thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
6899 uint16_t insn2, struct regcache *regs,
cfba9872 6900 arm_displaced_step_closure *dsc)
34518530
YQ
6901{
6902 unsigned int coproc = bits (insn2, 8, 11);
34518530
YQ
6903 unsigned int bit_5_8 = bits (insn1, 5, 8);
6904 unsigned int bit_9 = bit (insn1, 9);
6905 unsigned int bit_4 = bit (insn1, 4);
34518530
YQ
6906
6907 if (bit_9 == 0)
6908 {
6909 if (bit_5_8 == 2)
6910 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6911 "neon 64bit xfer/mrrc/mrrc2/mcrr/mcrr2",
6912 dsc);
6913 else if (bit_5_8 == 0) /* UNDEFINED. */
6914 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
6915 else
6916 {
6917 /*coproc is 101x. SIMD/VFP, ext registers load/store. */
6918 if ((coproc & 0xe) == 0xa)
6919 return thumb2_decode_ext_reg_ld_st (gdbarch, insn1, insn2, regs,
6920 dsc);
6921 else /* coproc is not 101x. */
6922 {
6923 if (bit_4 == 0) /* STC/STC2. */
6924 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6925 "stc/stc2", dsc);
405feb71 6926 else /* LDC/LDC2 {literal, immediate}. */
34518530
YQ
6927 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2,
6928 regs, dsc);
6929 }
6930 }
6931 }
6932 else
6933 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "coproc", dsc);
6934
6935 return 0;
6936}
6937
6938static void
6939install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 6940 arm_displaced_step_closure *dsc, int rd)
34518530
YQ
6941{
6942 /* ADR Rd, #imm
6943
6944 Rewrite as:
6945
6946 Preparation: Rd <- PC
6947 Insn: ADD Rd, #imm
6948 Cleanup: Null.
6949 */
6950
6951 /* Rd <- PC */
6952 int val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6953 displaced_write_reg (regs, dsc, rd, val, CANNOT_WRITE_PC);
6954}
6955
6956static int
6957thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 6958 arm_displaced_step_closure *dsc,
34518530
YQ
6959 int rd, unsigned int imm)
6960{
6961
6962 /* Encoding T2: ADDS Rd, #imm */
6963 dsc->modinsn[0] = (0x3000 | (rd << 8) | imm);
6964
6965 install_pc_relative (gdbarch, regs, dsc, rd);
6966
6967 return 0;
6968}
6969
6970static int
6971thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
6972 struct regcache *regs,
cfba9872 6973 arm_displaced_step_closure *dsc)
34518530
YQ
6974{
6975 unsigned int rd = bits (insn, 8, 10);
6976 unsigned int imm8 = bits (insn, 0, 7);
6977
6978 if (debug_displaced)
6979 fprintf_unfiltered (gdb_stdlog,
6980 "displaced: copying thumb adr r%d, #%d insn %.4x\n",
6981 rd, imm8, insn);
6982
6983 return thumb_copy_pc_relative_16bit (gdbarch, regs, dsc, rd, imm8);
6984}
6985
6986static int
6987thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
6988 uint16_t insn2, struct regcache *regs,
cfba9872 6989 arm_displaced_step_closure *dsc)
34518530
YQ
6990{
6991 unsigned int rd = bits (insn2, 8, 11);
6992 /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
6993 extract raw immediate encoding rather than computing immediate. When
6994 generating ADD or SUB instruction, we can simply perform OR operation to
6995 set immediate into ADD. */
6996 unsigned int imm_3_8 = insn2 & 0x70ff;
6997 unsigned int imm_i = insn1 & 0x0400; /* Clear all bits except bit 10. */
6998
6999 if (debug_displaced)
7000 fprintf_unfiltered (gdb_stdlog,
7001 "displaced: copying thumb adr r%d, #%d:%d insn %.4x%.4x\n",
7002 rd, imm_i, imm_3_8, insn1, insn2);
7003
7004 if (bit (insn1, 7)) /* Encoding T2 */
7005 {
7006 /* Encoding T3: SUB Rd, Rd, #imm */
7007 dsc->modinsn[0] = (0xf1a0 | rd | imm_i);
7008 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
7009 }
7010 else /* Encoding T3 */
7011 {
7012 /* Encoding T3: ADD Rd, Rd, #imm */
7013 dsc->modinsn[0] = (0xf100 | rd | imm_i);
7014 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
7015 }
7016 dsc->numinsns = 2;
7017
7018 install_pc_relative (gdbarch, regs, dsc, rd);
7019
7020 return 0;
7021}
7022
7023static int
615234c1 7024thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, uint16_t insn1,
34518530 7025 struct regcache *regs,
cfba9872 7026 arm_displaced_step_closure *dsc)
34518530
YQ
7027{
7028 unsigned int rt = bits (insn1, 8, 10);
7029 unsigned int pc;
7030 int imm8 = (bits (insn1, 0, 7) << 2);
34518530
YQ
7031
7032 /* LDR Rd, #imm8
7033
7034 Rwrite as:
7035
7036 Preparation: tmp0 <- R0, tmp2 <- R2, tmp3 <- R3, R2 <- PC, R3 <- #imm8;
7037
7038 Insn: LDR R0, [R2, R3];
7039 Cleanup: R2 <- tmp2, R3 <- tmp3, Rd <- R0, R0 <- tmp0 */
7040
7041 if (debug_displaced)
7042 fprintf_unfiltered (gdb_stdlog,
7043 "displaced: copying thumb ldr r%d [pc #%d]\n"
7044 , rt, imm8);
7045
7046 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
7047 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
7048 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
7049 pc = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
7050 /* The assembler calculates the required value of the offset from the
7051 Align(PC,4) value of this instruction to the label. */
7052 pc = pc & 0xfffffffc;
7053
7054 displaced_write_reg (regs, dsc, 2, pc, CANNOT_WRITE_PC);
7055 displaced_write_reg (regs, dsc, 3, imm8, CANNOT_WRITE_PC);
7056
7057 dsc->rd = rt;
7058 dsc->u.ldst.xfersize = 4;
7059 dsc->u.ldst.rn = 0;
7060 dsc->u.ldst.immed = 0;
7061 dsc->u.ldst.writeback = 0;
7062 dsc->u.ldst.restore_r4 = 0;
7063
7064 dsc->modinsn[0] = 0x58d0; /* ldr r0, [r2, r3]*/
7065
7066 dsc->cleanup = &cleanup_load;
7067
7068 return 0;
7069}
7070
405feb71 7071/* Copy Thumb cbnz/cbz instruction. */
34518530
YQ
7072
7073static int
7074thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
7075 struct regcache *regs,
cfba9872 7076 arm_displaced_step_closure *dsc)
34518530
YQ
7077{
7078 int non_zero = bit (insn1, 11);
7079 unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
7080 CORE_ADDR from = dsc->insn_addr;
7081 int rn = bits (insn1, 0, 2);
7082 int rn_val = displaced_read_reg (regs, dsc, rn);
7083
7084 dsc->u.branch.cond = (rn_val && non_zero) || (!rn_val && !non_zero);
7085 /* CBNZ and CBZ do not affect the condition flags. If condition is true,
7086 set it INST_AL, so cleanup_branch will know branch is taken, otherwise,
7087 condition is false, let it be, cleanup_branch will do nothing. */
7088 if (dsc->u.branch.cond)
7089 {
7090 dsc->u.branch.cond = INST_AL;
7091 dsc->u.branch.dest = from + 4 + imm5;
7092 }
7093 else
7094 dsc->u.branch.dest = from + 2;
7095
7096 dsc->u.branch.link = 0;
7097 dsc->u.branch.exchange = 0;
7098
7099 if (debug_displaced)
7100 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s [r%d = 0x%x]"
7101 " insn %.4x to %.8lx\n", non_zero ? "cbnz" : "cbz",
7102 rn, rn_val, insn1, dsc->u.branch.dest);
7103
7104 dsc->modinsn[0] = THUMB_NOP;
7105
7106 dsc->cleanup = &cleanup_branch;
7107 return 0;
7108}
7109
7110/* Copy Table Branch Byte/Halfword */
7111static int
7112thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
7113 uint16_t insn2, struct regcache *regs,
cfba9872 7114 arm_displaced_step_closure *dsc)
34518530
YQ
7115{
7116 ULONGEST rn_val, rm_val;
7117 int is_tbh = bit (insn2, 4);
7118 CORE_ADDR halfwords = 0;
7119 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7120
7121 rn_val = displaced_read_reg (regs, dsc, bits (insn1, 0, 3));
7122 rm_val = displaced_read_reg (regs, dsc, bits (insn2, 0, 3));
7123
7124 if (is_tbh)
7125 {
7126 gdb_byte buf[2];
7127
7128 target_read_memory (rn_val + 2 * rm_val, buf, 2);
7129 halfwords = extract_unsigned_integer (buf, 2, byte_order);
7130 }
7131 else
7132 {
7133 gdb_byte buf[1];
7134
7135 target_read_memory (rn_val + rm_val, buf, 1);
7136 halfwords = extract_unsigned_integer (buf, 1, byte_order);
7137 }
7138
7139 if (debug_displaced)
7140 fprintf_unfiltered (gdb_stdlog, "displaced: %s base 0x%x offset 0x%x"
7141 " offset 0x%x\n", is_tbh ? "tbh" : "tbb",
7142 (unsigned int) rn_val, (unsigned int) rm_val,
7143 (unsigned int) halfwords);
7144
7145 dsc->u.branch.cond = INST_AL;
7146 dsc->u.branch.link = 0;
7147 dsc->u.branch.exchange = 0;
7148 dsc->u.branch.dest = dsc->insn_addr + 4 + 2 * halfwords;
7149
7150 dsc->cleanup = &cleanup_branch;
7151
7152 return 0;
7153}
7154
7155static void
7156cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 7157 arm_displaced_step_closure *dsc)
34518530
YQ
7158{
7159 /* PC <- r7 */
7160 int val = displaced_read_reg (regs, dsc, 7);
7161 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, val, BX_WRITE_PC);
7162
7163 /* r7 <- r8 */
7164 val = displaced_read_reg (regs, dsc, 8);
7165 displaced_write_reg (regs, dsc, 7, val, CANNOT_WRITE_PC);
7166
7167 /* r8 <- tmp[0] */
7168 displaced_write_reg (regs, dsc, 8, dsc->tmp[0], CANNOT_WRITE_PC);
7169
7170}
7171
7172static int
615234c1 7173thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, uint16_t insn1,
34518530 7174 struct regcache *regs,
cfba9872 7175 arm_displaced_step_closure *dsc)
34518530
YQ
7176{
7177 dsc->u.block.regmask = insn1 & 0x00ff;
7178
7179 /* Rewrite instruction: POP {rX, rY, ...,rZ, PC}
7180 to :
7181
7182 (1) register list is full, that is, r0-r7 are used.
7183 Prepare: tmp[0] <- r8
7184
7185 POP {r0, r1, ...., r6, r7}; remove PC from reglist
7186 MOV r8, r7; Move value of r7 to r8;
7187 POP {r7}; Store PC value into r7.
7188
7189 Cleanup: PC <- r7, r7 <- r8, r8 <-tmp[0]
7190
7191 (2) register list is not full, supposing there are N registers in
7192 register list (except PC, 0 <= N <= 7).
7193 Prepare: for each i, 0 - N, tmp[i] <- ri.
7194
7195 POP {r0, r1, ...., rN};
7196
7197 Cleanup: Set registers in original reglist from r0 - rN. Restore r0 - rN
7198 from tmp[] properly.
7199 */
7200 if (debug_displaced)
7201 fprintf_unfiltered (gdb_stdlog,
7202 "displaced: copying thumb pop {%.8x, pc} insn %.4x\n",
7203 dsc->u.block.regmask, insn1);
7204
7205 if (dsc->u.block.regmask == 0xff)
7206 {
7207 dsc->tmp[0] = displaced_read_reg (regs, dsc, 8);
7208
7209 dsc->modinsn[0] = (insn1 & 0xfeff); /* POP {r0,r1,...,r6, r7} */
7210 dsc->modinsn[1] = 0x46b8; /* MOV r8, r7 */
7211 dsc->modinsn[2] = 0xbc80; /* POP {r7} */
7212
7213 dsc->numinsns = 3;
7214 dsc->cleanup = &cleanup_pop_pc_16bit_all;
7215 }
7216 else
7217 {
5f661e03 7218 unsigned int num_in_list = count_one_bits (dsc->u.block.regmask);
bec2ab5a
SM
7219 unsigned int i;
7220 unsigned int new_regmask;
34518530
YQ
7221
7222 for (i = 0; i < num_in_list + 1; i++)
7223 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7224
7225 new_regmask = (1 << (num_in_list + 1)) - 1;
7226
7227 if (debug_displaced)
7228 fprintf_unfiltered (gdb_stdlog, _("displaced: POP "
7229 "{..., pc}: original reg list %.4x,"
7230 " modified list %.4x\n"),
7231 (int) dsc->u.block.regmask, new_regmask);
7232
7233 dsc->u.block.regmask |= 0x8000;
7234 dsc->u.block.writeback = 0;
7235 dsc->u.block.cond = INST_AL;
7236
7237 dsc->modinsn[0] = (insn1 & ~0x1ff) | (new_regmask & 0xff);
7238
7239 dsc->cleanup = &cleanup_block_load_pc;
7240 }
7241
7242 return 0;
7243}
7244
7245static void
7246thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
7247 struct regcache *regs,
cfba9872 7248 arm_displaced_step_closure *dsc)
34518530
YQ
7249{
7250 unsigned short op_bit_12_15 = bits (insn1, 12, 15);
7251 unsigned short op_bit_10_11 = bits (insn1, 10, 11);
7252 int err = 0;
7253
7254 /* 16-bit thumb instructions. */
7255 switch (op_bit_12_15)
7256 {
7257 /* Shift (imme), add, subtract, move and compare. */
7258 case 0: case 1: case 2: case 3:
7259 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
7260 "shift/add/sub/mov/cmp",
7261 dsc);
7262 break;
7263 case 4:
7264 switch (op_bit_10_11)
7265 {
7266 case 0: /* Data-processing */
7267 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
7268 "data-processing",
7269 dsc);
7270 break;
7271 case 1: /* Special data instructions and branch and exchange. */
7272 {
7273 unsigned short op = bits (insn1, 7, 9);
7274 if (op == 6 || op == 7) /* BX or BLX */
7275 err = thumb_copy_bx_blx_reg (gdbarch, insn1, regs, dsc);
7276 else if (bits (insn1, 6, 7) != 0) /* ADD/MOV/CMP high registers. */
7277 err = thumb_copy_alu_reg (gdbarch, insn1, regs, dsc);
7278 else
7279 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "special data",
7280 dsc);
7281 }
7282 break;
7283 default: /* LDR (literal) */
7284 err = thumb_copy_16bit_ldr_literal (gdbarch, insn1, regs, dsc);
7285 }
7286 break;
7287 case 5: case 6: case 7: case 8: case 9: /* Load/Store single data item */
7288 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldr/str", dsc);
7289 break;
7290 case 10:
7291 if (op_bit_10_11 < 2) /* Generate PC-relative address */
7292 err = thumb_decode_pc_relative_16bit (gdbarch, insn1, regs, dsc);
7293 else /* Generate SP-relative address */
7294 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "sp-relative", dsc);
7295 break;
7296 case 11: /* Misc 16-bit instructions */
7297 {
7298 switch (bits (insn1, 8, 11))
7299 {
7300 case 1: case 3: case 9: case 11: /* CBNZ, CBZ */
7301 err = thumb_copy_cbnz_cbz (gdbarch, insn1, regs, dsc);
7302 break;
7303 case 12: case 13: /* POP */
7304 if (bit (insn1, 8)) /* PC is in register list. */
7305 err = thumb_copy_pop_pc_16bit (gdbarch, insn1, regs, dsc);
7306 else
7307 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "pop", dsc);
7308 break;
7309 case 15: /* If-Then, and hints */
7310 if (bits (insn1, 0, 3))
7311 /* If-Then makes up to four following instructions conditional.
7312 IT instruction itself is not conditional, so handle it as a
7313 common unmodified instruction. */
7314 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "If-Then",
7315 dsc);
7316 else
7317 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "hints", dsc);
7318 break;
7319 default:
7320 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "misc", dsc);
7321 }
7322 }
7323 break;
7324 case 12:
7325 if (op_bit_10_11 < 2) /* Store multiple registers */
7326 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "stm", dsc);
7327 else /* Load multiple registers */
7328 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldm", dsc);
7329 break;
7330 case 13: /* Conditional branch and supervisor call */
7331 if (bits (insn1, 9, 11) != 7) /* conditional branch */
7332 err = thumb_copy_b (gdbarch, insn1, dsc);
7333 else
7334 err = thumb_copy_svc (gdbarch, insn1, regs, dsc);
7335 break;
7336 case 14: /* Unconditional branch */
7337 err = thumb_copy_b (gdbarch, insn1, dsc);
7338 break;
7339 default:
7340 err = 1;
7341 }
7342
7343 if (err)
7344 internal_error (__FILE__, __LINE__,
7345 _("thumb_process_displaced_16bit_insn: Instruction decode error"));
7346}
7347
7348static int
7349decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
7350 uint16_t insn1, uint16_t insn2,
7351 struct regcache *regs,
cfba9872 7352 arm_displaced_step_closure *dsc)
34518530
YQ
7353{
7354 int rt = bits (insn2, 12, 15);
7355 int rn = bits (insn1, 0, 3);
7356 int op1 = bits (insn1, 7, 8);
34518530
YQ
7357
7358 switch (bits (insn1, 5, 6))
7359 {
7360 case 0: /* Load byte and memory hints */
7361 if (rt == 0xf) /* PLD/PLI */
7362 {
7363 if (rn == 0xf)
7364 /* PLD literal or Encoding T3 of PLI(immediate, literal). */
7365 return thumb2_copy_preload (gdbarch, insn1, insn2, regs, dsc);
7366 else
7367 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7368 "pli/pld", dsc);
7369 }
7370 else
7371 {
7372 if (rn == 0xf) /* LDRB/LDRSB (literal) */
7373 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
7374 1);
7375 else
7376 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7377 "ldrb{reg, immediate}/ldrbt",
7378 dsc);
7379 }
7380
7381 break;
7382 case 1: /* Load halfword and memory hints. */
7383 if (rt == 0xf) /* PLD{W} and Unalloc memory hint. */
7384 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7385 "pld/unalloc memhint", dsc);
7386 else
7387 {
7388 if (rn == 0xf)
7389 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
7390 2);
7391 else
7392 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7393 "ldrh/ldrht", dsc);
7394 }
7395 break;
7396 case 2: /* Load word */
7397 {
7398 int insn2_bit_8_11 = bits (insn2, 8, 11);
7399
7400 if (rn == 0xf)
7401 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc, 4);
7402 else if (op1 == 0x1) /* Encoding T3 */
7403 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs, dsc,
7404 0, 1);
7405 else /* op1 == 0x0 */
7406 {
7407 if (insn2_bit_8_11 == 0xc || (insn2_bit_8_11 & 0x9) == 0x9)
7408 /* LDR (immediate) */
7409 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
7410 dsc, bit (insn2, 8), 1);
7411 else if (insn2_bit_8_11 == 0xe) /* LDRT */
7412 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7413 "ldrt", dsc);
7414 else
7415 /* LDR (register) */
7416 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
7417 dsc, 0, 0);
7418 }
7419 break;
7420 }
7421 default:
7422 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
7423 break;
7424 }
7425 return 0;
7426}
7427
7428static void
7429thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
7430 uint16_t insn2, struct regcache *regs,
cfba9872 7431 arm_displaced_step_closure *dsc)
34518530
YQ
7432{
7433 int err = 0;
7434 unsigned short op = bit (insn2, 15);
7435 unsigned int op1 = bits (insn1, 11, 12);
7436
7437 switch (op1)
7438 {
7439 case 1:
7440 {
7441 switch (bits (insn1, 9, 10))
7442 {
7443 case 0:
7444 if (bit (insn1, 6))
7445 {
405feb71 7446 /* Load/store {dual, exclusive}, table branch. */
34518530
YQ
7447 if (bits (insn1, 7, 8) == 1 && bits (insn1, 4, 5) == 1
7448 && bits (insn2, 5, 7) == 0)
7449 err = thumb2_copy_table_branch (gdbarch, insn1, insn2, regs,
7450 dsc);
7451 else
7452 /* PC is not allowed to use in load/store {dual, exclusive}
7453 instructions. */
7454 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7455 "load/store dual/ex", dsc);
7456 }
7457 else /* load/store multiple */
7458 {
7459 switch (bits (insn1, 7, 8))
7460 {
7461 case 0: case 3: /* SRS, RFE */
7462 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7463 "srs/rfe", dsc);
7464 break;
7465 case 1: case 2: /* LDM/STM/PUSH/POP */
7466 err = thumb2_copy_block_xfer (gdbarch, insn1, insn2, regs, dsc);
7467 break;
7468 }
7469 }
7470 break;
7471
7472 case 1:
7473 /* Data-processing (shift register). */
7474 err = thumb2_decode_dp_shift_reg (gdbarch, insn1, insn2, regs,
7475 dsc);
7476 break;
7477 default: /* Coprocessor instructions. */
7478 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
7479 break;
7480 }
7481 break;
7482 }
7483 case 2: /* op1 = 2 */
7484 if (op) /* Branch and misc control. */
7485 {
7486 if (bit (insn2, 14) /* BLX/BL */
7487 || bit (insn2, 12) /* Unconditional branch */
7488 || (bits (insn1, 7, 9) != 0x7)) /* Conditional branch */
7489 err = thumb2_copy_b_bl_blx (gdbarch, insn1, insn2, regs, dsc);
7490 else
7491 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7492 "misc ctrl", dsc);
7493 }
7494 else
7495 {
7496 if (bit (insn1, 9)) /* Data processing (plain binary imm). */
7497 {
b926417a 7498 int dp_op = bits (insn1, 4, 8);
34518530 7499 int rn = bits (insn1, 0, 3);
b926417a 7500 if ((dp_op == 0 || dp_op == 0xa) && rn == 0xf)
34518530
YQ
7501 err = thumb_copy_pc_relative_32bit (gdbarch, insn1, insn2,
7502 regs, dsc);
7503 else
7504 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7505 "dp/pb", dsc);
7506 }
405feb71 7507 else /* Data processing (modified immediate) */
34518530
YQ
7508 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7509 "dp/mi", dsc);
7510 }
7511 break;
7512 case 3: /* op1 = 3 */
7513 switch (bits (insn1, 9, 10))
7514 {
7515 case 0:
7516 if (bit (insn1, 4))
7517 err = decode_thumb_32bit_ld_mem_hints (gdbarch, insn1, insn2,
7518 regs, dsc);
7519 else /* NEON Load/Store and Store single data item */
7520 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7521 "neon elt/struct load/store",
7522 dsc);
7523 break;
7524 case 1: /* op1 = 3, bits (9, 10) == 1 */
7525 switch (bits (insn1, 7, 8))
7526 {
7527 case 0: case 1: /* Data processing (register) */
7528 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7529 "dp(reg)", dsc);
7530 break;
7531 case 2: /* Multiply and absolute difference */
7532 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7533 "mul/mua/diff", dsc);
7534 break;
7535 case 3: /* Long multiply and divide */
7536 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7537 "lmul/lmua", dsc);
7538 break;
7539 }
7540 break;
7541 default: /* Coprocessor instructions */
7542 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
7543 break;
7544 }
7545 break;
7546 default:
7547 err = 1;
7548 }
7549
7550 if (err)
7551 internal_error (__FILE__, __LINE__,
7552 _("thumb_process_displaced_32bit_insn: Instruction decode error"));
7553
7554}
7555
b434a28f
YQ
7556static void
7557thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
12545665 7558 struct regcache *regs,
cfba9872 7559 arm_displaced_step_closure *dsc)
b434a28f 7560{
34518530
YQ
7561 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7562 uint16_t insn1
7563 = read_memory_unsigned_integer (from, 2, byte_order_for_code);
7564
7565 if (debug_displaced)
7566 fprintf_unfiltered (gdb_stdlog, "displaced: process thumb insn %.4x "
7567 "at %.8lx\n", insn1, (unsigned long) from);
7568
7569 dsc->is_thumb = 1;
7570 dsc->insn_size = thumb_insn_size (insn1);
7571 if (thumb_insn_size (insn1) == 4)
7572 {
7573 uint16_t insn2
7574 = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
7575 thumb_process_displaced_32bit_insn (gdbarch, insn1, insn2, regs, dsc);
7576 }
7577 else
7578 thumb_process_displaced_16bit_insn (gdbarch, insn1, regs, dsc);
b434a28f
YQ
7579}
7580
cca44b1b 7581void
b434a28f
YQ
7582arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
7583 CORE_ADDR to, struct regcache *regs,
cfba9872 7584 arm_displaced_step_closure *dsc)
cca44b1b
JB
7585{
7586 int err = 0;
b434a28f
YQ
7587 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7588 uint32_t insn;
cca44b1b
JB
7589
7590 /* Most displaced instructions use a 1-instruction scratch space, so set this
7591 here and override below if/when necessary. */
7592 dsc->numinsns = 1;
7593 dsc->insn_addr = from;
7594 dsc->scratch_base = to;
7595 dsc->cleanup = NULL;
7596 dsc->wrote_to_pc = 0;
7597
b434a28f 7598 if (!displaced_in_arm_mode (regs))
12545665 7599 return thumb_process_displaced_insn (gdbarch, from, regs, dsc);
b434a28f 7600
4db71c0b
YQ
7601 dsc->is_thumb = 0;
7602 dsc->insn_size = 4;
b434a28f
YQ
7603 insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
7604 if (debug_displaced)
7605 fprintf_unfiltered (gdb_stdlog, "displaced: stepping insn %.8lx "
7606 "at %.8lx\n", (unsigned long) insn,
7607 (unsigned long) from);
7608
cca44b1b 7609 if ((insn & 0xf0000000) == 0xf0000000)
7ff120b4 7610 err = arm_decode_unconditional (gdbarch, insn, regs, dsc);
cca44b1b
JB
7611 else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
7612 {
7613 case 0x0: case 0x1: case 0x2: case 0x3:
7ff120b4 7614 err = arm_decode_dp_misc (gdbarch, insn, regs, dsc);
cca44b1b
JB
7615 break;
7616
7617 case 0x4: case 0x5: case 0x6:
7ff120b4 7618 err = arm_decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
cca44b1b
JB
7619 break;
7620
7621 case 0x7:
7ff120b4 7622 err = arm_decode_media (gdbarch, insn, dsc);
cca44b1b
JB
7623 break;
7624
7625 case 0x8: case 0x9: case 0xa: case 0xb:
7ff120b4 7626 err = arm_decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
cca44b1b
JB
7627 break;
7628
7629 case 0xc: case 0xd: case 0xe: case 0xf:
12545665 7630 err = arm_decode_svc_copro (gdbarch, insn, regs, dsc);
cca44b1b
JB
7631 break;
7632 }
7633
7634 if (err)
7635 internal_error (__FILE__, __LINE__,
7636 _("arm_process_displaced_insn: Instruction decode error"));
7637}
7638
7639/* Actually set up the scratch space for a displaced instruction. */
7640
7641void
7642arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
cfba9872 7643 CORE_ADDR to, arm_displaced_step_closure *dsc)
cca44b1b
JB
7644{
7645 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4db71c0b 7646 unsigned int i, len, offset;
cca44b1b 7647 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4db71c0b 7648 int size = dsc->is_thumb? 2 : 4;
948f8e3d 7649 const gdb_byte *bkp_insn;
cca44b1b 7650
4db71c0b 7651 offset = 0;
cca44b1b
JB
7652 /* Poke modified instruction(s). */
7653 for (i = 0; i < dsc->numinsns; i++)
7654 {
7655 if (debug_displaced)
4db71c0b
YQ
7656 {
7657 fprintf_unfiltered (gdb_stdlog, "displaced: writing insn ");
7658 if (size == 4)
7659 fprintf_unfiltered (gdb_stdlog, "%.8lx",
7660 dsc->modinsn[i]);
7661 else if (size == 2)
7662 fprintf_unfiltered (gdb_stdlog, "%.4x",
7663 (unsigned short)dsc->modinsn[i]);
7664
7665 fprintf_unfiltered (gdb_stdlog, " at %.8lx\n",
7666 (unsigned long) to + offset);
7667
7668 }
7669 write_memory_unsigned_integer (to + offset, size,
7670 byte_order_for_code,
cca44b1b 7671 dsc->modinsn[i]);
4db71c0b
YQ
7672 offset += size;
7673 }
7674
7675 /* Choose the correct breakpoint instruction. */
7676 if (dsc->is_thumb)
7677 {
7678 bkp_insn = tdep->thumb_breakpoint;
7679 len = tdep->thumb_breakpoint_size;
7680 }
7681 else
7682 {
7683 bkp_insn = tdep->arm_breakpoint;
7684 len = tdep->arm_breakpoint_size;
cca44b1b
JB
7685 }
7686
7687 /* Put breakpoint afterwards. */
4db71c0b 7688 write_memory (to + offset, bkp_insn, len);
cca44b1b
JB
7689
7690 if (debug_displaced)
7691 fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
7692 paddress (gdbarch, from), paddress (gdbarch, to));
7693}
7694
cca44b1b
JB
7695/* Entry point for cleaning things up after a displaced instruction has been
7696 single-stepped. */
7697
7698void
7699arm_displaced_step_fixup (struct gdbarch *gdbarch,
cfba9872 7700 struct displaced_step_closure *dsc_,
cca44b1b
JB
7701 CORE_ADDR from, CORE_ADDR to,
7702 struct regcache *regs)
7703{
cfba9872
SM
7704 arm_displaced_step_closure *dsc = (arm_displaced_step_closure *) dsc_;
7705
cca44b1b
JB
7706 if (dsc->cleanup)
7707 dsc->cleanup (gdbarch, regs, dsc);
7708
7709 if (!dsc->wrote_to_pc)
4db71c0b
YQ
7710 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
7711 dsc->insn_addr + dsc->insn_size);
7712
cca44b1b
JB
7713}
7714
7715#include "bfd-in2.h"
7716#include "libcoff.h"
7717
7718static int
7719gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
7720{
e47ad6c0
YQ
7721 gdb_disassembler *di
7722 = static_cast<gdb_disassembler *>(info->application_data);
7723 struct gdbarch *gdbarch = di->arch ();
9779414d
DJ
7724
7725 if (arm_pc_is_thumb (gdbarch, memaddr))
cca44b1b
JB
7726 {
7727 static asymbol *asym;
7728 static combined_entry_type ce;
7729 static struct coff_symbol_struct csym;
7730 static struct bfd fake_bfd;
7731 static bfd_target fake_target;
7732
7733 if (csym.native == NULL)
7734 {
7735 /* Create a fake symbol vector containing a Thumb symbol.
7736 This is solely so that the code in print_insn_little_arm()
7737 and print_insn_big_arm() in opcodes/arm-dis.c will detect
7738 the presence of a Thumb symbol and switch to decoding
7739 Thumb instructions. */
7740
7741 fake_target.flavour = bfd_target_coff_flavour;
7742 fake_bfd.xvec = &fake_target;
7743 ce.u.syment.n_sclass = C_THUMBEXTFUNC;
7744 csym.native = &ce;
7745 csym.symbol.the_bfd = &fake_bfd;
7746 csym.symbol.name = "fake";
7747 asym = (asymbol *) & csym;
7748 }
7749
7750 memaddr = UNMAKE_THUMB_ADDR (memaddr);
7751 info->symbols = &asym;
7752 }
7753 else
7754 info->symbols = NULL;
7755
e60eb288
YQ
7756 /* GDB is able to get bfd_mach from the exe_bfd, info->mach is
7757 accurate, so mark USER_SPECIFIED_MACHINE_TYPE bit. Otherwise,
7758 opcodes/arm-dis.c:print_insn reset info->mach, and it will trigger
7759 the assert on the mismatch of info->mach and bfd_get_mach (exec_bfd)
7760 in default_print_insn. */
7761 if (exec_bfd != NULL)
7762 info->flags |= USER_SPECIFIED_MACHINE_TYPE;
7763
6394c606 7764 return default_print_insn (memaddr, info);
cca44b1b
JB
7765}
7766
7767/* The following define instruction sequences that will cause ARM
7768 cpu's to take an undefined instruction trap. These are used to
7769 signal a breakpoint to GDB.
7770
7771 The newer ARMv4T cpu's are capable of operating in ARM or Thumb
7772 modes. A different instruction is required for each mode. The ARM
7773 cpu's can also be big or little endian. Thus four different
7774 instructions are needed to support all cases.
7775
7776 Note: ARMv4 defines several new instructions that will take the
7777 undefined instruction trap. ARM7TDMI is nominally ARMv4T, but does
7778 not in fact add the new instructions. The new undefined
7779 instructions in ARMv4 are all instructions that had no defined
7780 behaviour in earlier chips. There is no guarantee that they will
7781 raise an exception, but may be treated as NOP's. In practice, it
7782 may only safe to rely on instructions matching:
7783
7784 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
7785 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
7786 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
7787
0963b4bd 7788 Even this may only true if the condition predicate is true. The
cca44b1b
JB
7789 following use a condition predicate of ALWAYS so it is always TRUE.
7790
7791 There are other ways of forcing a breakpoint. GNU/Linux, RISC iX,
7792 and NetBSD all use a software interrupt rather than an undefined
7793 instruction to force a trap. This can be handled by by the
7794 abi-specific code during establishment of the gdbarch vector. */
7795
7796#define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
7797#define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
7798#define THUMB_LE_BREAKPOINT {0xbe,0xbe}
7799#define THUMB_BE_BREAKPOINT {0xbe,0xbe}
7800
948f8e3d
PA
7801static const gdb_byte arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
7802static const gdb_byte arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
7803static const gdb_byte arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
7804static const gdb_byte arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
cca44b1b 7805
cd6c3b4f
YQ
7806/* Implement the breakpoint_kind_from_pc gdbarch method. */
7807
d19280ad
YQ
7808static int
7809arm_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
cca44b1b
JB
7810{
7811 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
177321bd 7812 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
cca44b1b 7813
9779414d 7814 if (arm_pc_is_thumb (gdbarch, *pcptr))
cca44b1b
JB
7815 {
7816 *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
177321bd
DJ
7817
7818 /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
7819 check whether we are replacing a 32-bit instruction. */
7820 if (tdep->thumb2_breakpoint != NULL)
7821 {
7822 gdb_byte buf[2];
d19280ad 7823
177321bd
DJ
7824 if (target_read_memory (*pcptr, buf, 2) == 0)
7825 {
7826 unsigned short inst1;
d19280ad 7827
177321bd 7828 inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
db24da6d 7829 if (thumb_insn_size (inst1) == 4)
d19280ad 7830 return ARM_BP_KIND_THUMB2;
177321bd
DJ
7831 }
7832 }
7833
d19280ad 7834 return ARM_BP_KIND_THUMB;
cca44b1b
JB
7835 }
7836 else
d19280ad
YQ
7837 return ARM_BP_KIND_ARM;
7838
7839}
7840
cd6c3b4f
YQ
7841/* Implement the sw_breakpoint_from_kind gdbarch method. */
7842
d19280ad
YQ
7843static const gdb_byte *
7844arm_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
7845{
7846 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
7847
7848 switch (kind)
cca44b1b 7849 {
d19280ad
YQ
7850 case ARM_BP_KIND_ARM:
7851 *size = tdep->arm_breakpoint_size;
cca44b1b 7852 return tdep->arm_breakpoint;
d19280ad
YQ
7853 case ARM_BP_KIND_THUMB:
7854 *size = tdep->thumb_breakpoint_size;
7855 return tdep->thumb_breakpoint;
7856 case ARM_BP_KIND_THUMB2:
7857 *size = tdep->thumb2_breakpoint_size;
7858 return tdep->thumb2_breakpoint;
7859 default:
7860 gdb_assert_not_reached ("unexpected arm breakpoint kind");
cca44b1b
JB
7861 }
7862}
7863
833b7ab5
YQ
7864/* Implement the breakpoint_kind_from_current_state gdbarch method. */
7865
7866static int
7867arm_breakpoint_kind_from_current_state (struct gdbarch *gdbarch,
7868 struct regcache *regcache,
7869 CORE_ADDR *pcptr)
7870{
7871 gdb_byte buf[4];
7872
7873 /* Check the memory pointed by PC is readable. */
7874 if (target_read_memory (regcache_read_pc (regcache), buf, 4) == 0)
7875 {
7876 struct arm_get_next_pcs next_pcs_ctx;
833b7ab5
YQ
7877
7878 arm_get_next_pcs_ctor (&next_pcs_ctx,
7879 &arm_get_next_pcs_ops,
7880 gdbarch_byte_order (gdbarch),
7881 gdbarch_byte_order_for_code (gdbarch),
7882 0,
7883 regcache);
7884
a0ff9e1a 7885 std::vector<CORE_ADDR> next_pcs = arm_get_next_pcs (&next_pcs_ctx);
833b7ab5
YQ
7886
7887 /* If MEMADDR is the next instruction of current pc, do the
7888 software single step computation, and get the thumb mode by
7889 the destination address. */
a0ff9e1a 7890 for (CORE_ADDR pc : next_pcs)
833b7ab5
YQ
7891 {
7892 if (UNMAKE_THUMB_ADDR (pc) == *pcptr)
7893 {
833b7ab5
YQ
7894 if (IS_THUMB_ADDR (pc))
7895 {
7896 *pcptr = MAKE_THUMB_ADDR (*pcptr);
7897 return arm_breakpoint_kind_from_pc (gdbarch, pcptr);
7898 }
7899 else
7900 return ARM_BP_KIND_ARM;
7901 }
7902 }
833b7ab5
YQ
7903 }
7904
7905 return arm_breakpoint_kind_from_pc (gdbarch, pcptr);
7906}
7907
cca44b1b
JB
7908/* Extract from an array REGBUF containing the (raw) register state a
7909 function return value of type TYPE, and copy that, in virtual
7910 format, into VALBUF. */
7911
7912static void
7913arm_extract_return_value (struct type *type, struct regcache *regs,
7914 gdb_byte *valbuf)
7915{
ac7936df 7916 struct gdbarch *gdbarch = regs->arch ();
cca44b1b
JB
7917 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7918
78134374 7919 if (TYPE_CODE_FLT == type->code ())
cca44b1b
JB
7920 {
7921 switch (gdbarch_tdep (gdbarch)->fp_model)
7922 {
7923 case ARM_FLOAT_FPA:
7924 {
7925 /* The value is in register F0 in internal format. We need to
7926 extract the raw value and then convert it to the desired
7927 internal type. */
f0452268 7928 bfd_byte tmpbuf[ARM_FP_REGISTER_SIZE];
cca44b1b 7929
dca08e1f 7930 regs->cooked_read (ARM_F0_REGNUM, tmpbuf);
3b2ca824
UW
7931 target_float_convert (tmpbuf, arm_ext_type (gdbarch),
7932 valbuf, type);
cca44b1b
JB
7933 }
7934 break;
7935
7936 case ARM_FLOAT_SOFT_FPA:
7937 case ARM_FLOAT_SOFT_VFP:
7938 /* ARM_FLOAT_VFP can arise if this is a variadic function so
7939 not using the VFP ABI code. */
7940 case ARM_FLOAT_VFP:
dca08e1f 7941 regs->cooked_read (ARM_A1_REGNUM, valbuf);
cca44b1b 7942 if (TYPE_LENGTH (type) > 4)
f0452268
AH
7943 regs->cooked_read (ARM_A1_REGNUM + 1,
7944 valbuf + ARM_INT_REGISTER_SIZE);
cca44b1b
JB
7945 break;
7946
7947 default:
0963b4bd
MS
7948 internal_error (__FILE__, __LINE__,
7949 _("arm_extract_return_value: "
7950 "Floating point model not supported"));
cca44b1b
JB
7951 break;
7952 }
7953 }
78134374
SM
7954 else if (type->code () == TYPE_CODE_INT
7955 || type->code () == TYPE_CODE_CHAR
7956 || type->code () == TYPE_CODE_BOOL
7957 || type->code () == TYPE_CODE_PTR
aa006118 7958 || TYPE_IS_REFERENCE (type)
78134374 7959 || type->code () == TYPE_CODE_ENUM)
cca44b1b 7960 {
b021a221
MS
7961 /* If the type is a plain integer, then the access is
7962 straight-forward. Otherwise we have to play around a bit
7963 more. */
cca44b1b
JB
7964 int len = TYPE_LENGTH (type);
7965 int regno = ARM_A1_REGNUM;
7966 ULONGEST tmp;
7967
7968 while (len > 0)
7969 {
7970 /* By using store_unsigned_integer we avoid having to do
7971 anything special for small big-endian values. */
7972 regcache_cooked_read_unsigned (regs, regno++, &tmp);
7973 store_unsigned_integer (valbuf,
f0452268
AH
7974 (len > ARM_INT_REGISTER_SIZE
7975 ? ARM_INT_REGISTER_SIZE : len),
cca44b1b 7976 byte_order, tmp);
f0452268
AH
7977 len -= ARM_INT_REGISTER_SIZE;
7978 valbuf += ARM_INT_REGISTER_SIZE;
cca44b1b
JB
7979 }
7980 }
7981 else
7982 {
7983 /* For a structure or union the behaviour is as if the value had
7984 been stored to word-aligned memory and then loaded into
7985 registers with 32-bit load instruction(s). */
7986 int len = TYPE_LENGTH (type);
7987 int regno = ARM_A1_REGNUM;
f0452268 7988 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
cca44b1b
JB
7989
7990 while (len > 0)
7991 {
dca08e1f 7992 regs->cooked_read (regno++, tmpbuf);
cca44b1b 7993 memcpy (valbuf, tmpbuf,
f0452268
AH
7994 len > ARM_INT_REGISTER_SIZE ? ARM_INT_REGISTER_SIZE : len);
7995 len -= ARM_INT_REGISTER_SIZE;
7996 valbuf += ARM_INT_REGISTER_SIZE;
cca44b1b
JB
7997 }
7998 }
7999}
8000
8001
8002/* Will a function return an aggregate type in memory or in a
8003 register? Return 0 if an aggregate type can be returned in a
8004 register, 1 if it must be returned in memory. */
8005
8006static int
8007arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
8008{
cca44b1b
JB
8009 enum type_code code;
8010
f168693b 8011 type = check_typedef (type);
cca44b1b 8012
b13c8ab2
YQ
8013 /* Simple, non-aggregate types (ie not including vectors and
8014 complex) are always returned in a register (or registers). */
78134374 8015 code = type->code ();
b13c8ab2
YQ
8016 if (TYPE_CODE_STRUCT != code && TYPE_CODE_UNION != code
8017 && TYPE_CODE_ARRAY != code && TYPE_CODE_COMPLEX != code)
8018 return 0;
cca44b1b 8019
c4312b19
YQ
8020 if (TYPE_CODE_ARRAY == code && TYPE_VECTOR (type))
8021 {
8022 /* Vector values should be returned using ARM registers if they
8023 are not over 16 bytes. */
8024 return (TYPE_LENGTH (type) > 16);
8025 }
8026
b13c8ab2 8027 if (gdbarch_tdep (gdbarch)->arm_abi != ARM_ABI_APCS)
cca44b1b 8028 {
b13c8ab2
YQ
8029 /* The AAPCS says all aggregates not larger than a word are returned
8030 in a register. */
f0452268 8031 if (TYPE_LENGTH (type) <= ARM_INT_REGISTER_SIZE)
b13c8ab2
YQ
8032 return 0;
8033
cca44b1b
JB
8034 return 1;
8035 }
b13c8ab2
YQ
8036 else
8037 {
8038 int nRc;
cca44b1b 8039
b13c8ab2
YQ
8040 /* All aggregate types that won't fit in a register must be returned
8041 in memory. */
f0452268 8042 if (TYPE_LENGTH (type) > ARM_INT_REGISTER_SIZE)
b13c8ab2 8043 return 1;
cca44b1b 8044
b13c8ab2
YQ
8045 /* In the ARM ABI, "integer" like aggregate types are returned in
8046 registers. For an aggregate type to be integer like, its size
f0452268 8047 must be less than or equal to ARM_INT_REGISTER_SIZE and the
b13c8ab2
YQ
8048 offset of each addressable subfield must be zero. Note that bit
8049 fields are not addressable, and all addressable subfields of
8050 unions always start at offset zero.
cca44b1b 8051
b13c8ab2
YQ
8052 This function is based on the behaviour of GCC 2.95.1.
8053 See: gcc/arm.c: arm_return_in_memory() for details.
cca44b1b 8054
b13c8ab2
YQ
8055 Note: All versions of GCC before GCC 2.95.2 do not set up the
8056 parameters correctly for a function returning the following
8057 structure: struct { float f;}; This should be returned in memory,
8058 not a register. Richard Earnshaw sent me a patch, but I do not
8059 know of any way to detect if a function like the above has been
8060 compiled with the correct calling convention. */
8061
8062 /* Assume all other aggregate types can be returned in a register.
8063 Run a check for structures, unions and arrays. */
8064 nRc = 0;
67255d04 8065
b13c8ab2
YQ
8066 if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
8067 {
8068 int i;
8069 /* Need to check if this struct/union is "integer" like. For
8070 this to be true, its size must be less than or equal to
f0452268 8071 ARM_INT_REGISTER_SIZE and the offset of each addressable
b13c8ab2
YQ
8072 subfield must be zero. Note that bit fields are not
8073 addressable, and unions always start at offset zero. If any
8074 of the subfields is a floating point type, the struct/union
8075 cannot be an integer type. */
8076
8077 /* For each field in the object, check:
8078 1) Is it FP? --> yes, nRc = 1;
8079 2) Is it addressable (bitpos != 0) and
8080 not packed (bitsize == 0)?
8081 --> yes, nRc = 1
8082 */
8083
1f704f76 8084 for (i = 0; i < type->num_fields (); i++)
67255d04 8085 {
b13c8ab2
YQ
8086 enum type_code field_type_code;
8087
8088 field_type_code
940da03e 8089 = check_typedef (type->field (i).type ())->code ();
b13c8ab2
YQ
8090
8091 /* Is it a floating point type field? */
8092 if (field_type_code == TYPE_CODE_FLT)
67255d04
RE
8093 {
8094 nRc = 1;
8095 break;
8096 }
b13c8ab2
YQ
8097
8098 /* If bitpos != 0, then we have to care about it. */
8099 if (TYPE_FIELD_BITPOS (type, i) != 0)
8100 {
8101 /* Bitfields are not addressable. If the field bitsize is
8102 zero, then the field is not packed. Hence it cannot be
8103 a bitfield or any other packed type. */
8104 if (TYPE_FIELD_BITSIZE (type, i) == 0)
8105 {
8106 nRc = 1;
8107 break;
8108 }
8109 }
67255d04
RE
8110 }
8111 }
67255d04 8112
b13c8ab2
YQ
8113 return nRc;
8114 }
67255d04
RE
8115}
8116
34e8f22d
RE
8117/* Write into appropriate registers a function return value of type
8118 TYPE, given in virtual format. */
8119
8120static void
b508a996 8121arm_store_return_value (struct type *type, struct regcache *regs,
5238cf52 8122 const gdb_byte *valbuf)
34e8f22d 8123{
ac7936df 8124 struct gdbarch *gdbarch = regs->arch ();
e17a4113 8125 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
be8626e0 8126
78134374 8127 if (type->code () == TYPE_CODE_FLT)
34e8f22d 8128 {
f0452268 8129 gdb_byte buf[ARM_FP_REGISTER_SIZE];
34e8f22d 8130
be8626e0 8131 switch (gdbarch_tdep (gdbarch)->fp_model)
08216dd7
RE
8132 {
8133 case ARM_FLOAT_FPA:
8134
3b2ca824 8135 target_float_convert (valbuf, type, buf, arm_ext_type (gdbarch));
b66f5587 8136 regs->cooked_write (ARM_F0_REGNUM, buf);
08216dd7
RE
8137 break;
8138
fd50bc42 8139 case ARM_FLOAT_SOFT_FPA:
08216dd7 8140 case ARM_FLOAT_SOFT_VFP:
90445bd3
DJ
8141 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8142 not using the VFP ABI code. */
8143 case ARM_FLOAT_VFP:
b66f5587 8144 regs->cooked_write (ARM_A1_REGNUM, valbuf);
b508a996 8145 if (TYPE_LENGTH (type) > 4)
f0452268
AH
8146 regs->cooked_write (ARM_A1_REGNUM + 1,
8147 valbuf + ARM_INT_REGISTER_SIZE);
08216dd7
RE
8148 break;
8149
8150 default:
9b20d036
MS
8151 internal_error (__FILE__, __LINE__,
8152 _("arm_store_return_value: Floating "
8153 "point model not supported"));
08216dd7
RE
8154 break;
8155 }
34e8f22d 8156 }
78134374
SM
8157 else if (type->code () == TYPE_CODE_INT
8158 || type->code () == TYPE_CODE_CHAR
8159 || type->code () == TYPE_CODE_BOOL
8160 || type->code () == TYPE_CODE_PTR
aa006118 8161 || TYPE_IS_REFERENCE (type)
78134374 8162 || type->code () == TYPE_CODE_ENUM)
b508a996
RE
8163 {
8164 if (TYPE_LENGTH (type) <= 4)
8165 {
8166 /* Values of one word or less are zero/sign-extended and
8167 returned in r0. */
f0452268 8168 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
b508a996
RE
8169 LONGEST val = unpack_long (type, valbuf);
8170
f0452268 8171 store_signed_integer (tmpbuf, ARM_INT_REGISTER_SIZE, byte_order, val);
b66f5587 8172 regs->cooked_write (ARM_A1_REGNUM, tmpbuf);
b508a996
RE
8173 }
8174 else
8175 {
8176 /* Integral values greater than one word are stored in consecutive
8177 registers starting with r0. This will always be a multiple of
8178 the regiser size. */
8179 int len = TYPE_LENGTH (type);
8180 int regno = ARM_A1_REGNUM;
8181
8182 while (len > 0)
8183 {
b66f5587 8184 regs->cooked_write (regno++, valbuf);
f0452268
AH
8185 len -= ARM_INT_REGISTER_SIZE;
8186 valbuf += ARM_INT_REGISTER_SIZE;
b508a996
RE
8187 }
8188 }
8189 }
34e8f22d 8190 else
b508a996
RE
8191 {
8192 /* For a structure or union the behaviour is as if the value had
8193 been stored to word-aligned memory and then loaded into
8194 registers with 32-bit load instruction(s). */
8195 int len = TYPE_LENGTH (type);
8196 int regno = ARM_A1_REGNUM;
f0452268 8197 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
b508a996
RE
8198
8199 while (len > 0)
8200 {
8201 memcpy (tmpbuf, valbuf,
f0452268 8202 len > ARM_INT_REGISTER_SIZE ? ARM_INT_REGISTER_SIZE : len);
b66f5587 8203 regs->cooked_write (regno++, tmpbuf);
f0452268
AH
8204 len -= ARM_INT_REGISTER_SIZE;
8205 valbuf += ARM_INT_REGISTER_SIZE;
b508a996
RE
8206 }
8207 }
34e8f22d
RE
8208}
8209
2af48f68
PB
8210
8211/* Handle function return values. */
8212
8213static enum return_value_convention
6a3a010b 8214arm_return_value (struct gdbarch *gdbarch, struct value *function,
c055b101
CV
8215 struct type *valtype, struct regcache *regcache,
8216 gdb_byte *readbuf, const gdb_byte *writebuf)
2af48f68 8217{
7c00367c 8218 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
6a3a010b 8219 struct type *func_type = function ? value_type (function) : NULL;
90445bd3
DJ
8220 enum arm_vfp_cprc_base_type vfp_base_type;
8221 int vfp_base_count;
8222
8223 if (arm_vfp_abi_for_function (gdbarch, func_type)
8224 && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
8225 {
8226 int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
8227 int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
8228 int i;
8229 for (i = 0; i < vfp_base_count; i++)
8230 {
58d6951d
DJ
8231 if (reg_char == 'q')
8232 {
8233 if (writebuf)
8234 arm_neon_quad_write (gdbarch, regcache, i,
8235 writebuf + i * unit_length);
8236
8237 if (readbuf)
8238 arm_neon_quad_read (gdbarch, regcache, i,
8239 readbuf + i * unit_length);
8240 }
8241 else
8242 {
8243 char name_buf[4];
8244 int regnum;
8245
8c042590 8246 xsnprintf (name_buf, sizeof (name_buf), "%c%d", reg_char, i);
58d6951d
DJ
8247 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8248 strlen (name_buf));
8249 if (writebuf)
b66f5587 8250 regcache->cooked_write (regnum, writebuf + i * unit_length);
58d6951d 8251 if (readbuf)
dca08e1f 8252 regcache->cooked_read (regnum, readbuf + i * unit_length);
58d6951d 8253 }
90445bd3
DJ
8254 }
8255 return RETURN_VALUE_REGISTER_CONVENTION;
8256 }
7c00367c 8257
78134374
SM
8258 if (valtype->code () == TYPE_CODE_STRUCT
8259 || valtype->code () == TYPE_CODE_UNION
8260 || valtype->code () == TYPE_CODE_ARRAY)
2af48f68 8261 {
7c00367c
MK
8262 if (tdep->struct_return == pcc_struct_return
8263 || arm_return_in_memory (gdbarch, valtype))
2af48f68
PB
8264 return RETURN_VALUE_STRUCT_CONVENTION;
8265 }
78134374 8266 else if (valtype->code () == TYPE_CODE_COMPLEX)
b13c8ab2
YQ
8267 {
8268 if (arm_return_in_memory (gdbarch, valtype))
8269 return RETURN_VALUE_STRUCT_CONVENTION;
8270 }
7052e42c 8271
2af48f68
PB
8272 if (writebuf)
8273 arm_store_return_value (valtype, regcache, writebuf);
8274
8275 if (readbuf)
8276 arm_extract_return_value (valtype, regcache, readbuf);
8277
8278 return RETURN_VALUE_REGISTER_CONVENTION;
8279}
8280
8281
9df628e0 8282static int
60ade65d 8283arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
9df628e0 8284{
e17a4113
UW
8285 struct gdbarch *gdbarch = get_frame_arch (frame);
8286 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
8287 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
9df628e0 8288 CORE_ADDR jb_addr;
f0452268 8289 gdb_byte buf[ARM_INT_REGISTER_SIZE];
9df628e0 8290
60ade65d 8291 jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
9df628e0
RE
8292
8293 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
f0452268 8294 ARM_INT_REGISTER_SIZE))
9df628e0
RE
8295 return 0;
8296
f0452268 8297 *pc = extract_unsigned_integer (buf, ARM_INT_REGISTER_SIZE, byte_order);
9df628e0
RE
8298 return 1;
8299}
40eadf04
SP
8300/* A call to cmse secure entry function "foo" at "a" is modified by
8301 GNU ld as "b".
8302 a) bl xxxx <foo>
8303
8304 <foo>
8305 xxxx:
8306
8307 b) bl yyyy <__acle_se_foo>
8308
8309 section .gnu.sgstubs:
8310 <foo>
8311 yyyy: sg // secure gateway
8312 b.w xxxx <__acle_se_foo> // original_branch_dest
8313
8314 <__acle_se_foo>
8315 xxxx:
8316
8317 When the control at "b", the pc contains "yyyy" (sg address) which is a
8318 trampoline and does not exist in source code. This function returns the
8319 target pc "xxxx". For more details please refer to section 5.4
8320 (Entry functions) and section 3.4.4 (C level development flow of secure code)
8321 of "armv8-m-security-extensions-requirements-on-development-tools-engineering-specification"
8322 document on www.developer.arm.com. */
8323
8324static CORE_ADDR
8325arm_skip_cmse_entry (CORE_ADDR pc, const char *name, struct objfile *objfile)
8326{
8327 int target_len = strlen (name) + strlen ("__acle_se_") + 1;
8328 char *target_name = (char *) alloca (target_len);
8329 xsnprintf (target_name, target_len, "%s%s", "__acle_se_", name);
8330
8331 struct bound_minimal_symbol minsym
8332 = lookup_minimal_symbol (target_name, NULL, objfile);
8333
8334 if (minsym.minsym != nullptr)
8335 return BMSYMBOL_VALUE_ADDRESS (minsym);
8336
8337 return 0;
8338}
8339
8340/* Return true when SEC points to ".gnu.sgstubs" section. */
8341
8342static bool
8343arm_is_sgstubs_section (struct obj_section *sec)
8344{
8345 return (sec != nullptr
8346 && sec->the_bfd_section != nullptr
8347 && sec->the_bfd_section->name != nullptr
8348 && streq (sec->the_bfd_section->name, ".gnu.sgstubs"));
8349}
9df628e0 8350
faa95490
DJ
8351/* Recognize GCC and GNU ld's trampolines. If we are in a trampoline,
8352 return the target PC. Otherwise return 0. */
c906108c
SS
8353
8354CORE_ADDR
52f729a7 8355arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
c906108c 8356{
2c02bd72 8357 const char *name;
faa95490 8358 int namelen;
c906108c
SS
8359 CORE_ADDR start_addr;
8360
8361 /* Find the starting address and name of the function containing the PC. */
8362 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
80d8d390
YQ
8363 {
8364 /* Trampoline 'bx reg' doesn't belong to any functions. Do the
8365 check here. */
8366 start_addr = arm_skip_bx_reg (frame, pc);
8367 if (start_addr != 0)
8368 return start_addr;
8369
8370 return 0;
8371 }
c906108c 8372
faa95490
DJ
8373 /* If PC is in a Thumb call or return stub, return the address of the
8374 target PC, which is in a register. The thunk functions are called
8375 _call_via_xx, where x is the register name. The possible names
3d8d5e79
DJ
8376 are r0-r9, sl, fp, ip, sp, and lr. ARM RealView has similar
8377 functions, named __ARM_call_via_r[0-7]. */
61012eef
GB
8378 if (startswith (name, "_call_via_")
8379 || startswith (name, "__ARM_call_via_"))
c906108c 8380 {
ed9a39eb
JM
8381 /* Use the name suffix to determine which register contains the
8382 target PC. */
a121b7c1 8383 static const char *table[15] =
c5aa993b
JM
8384 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
8385 "r8", "r9", "sl", "fp", "ip", "sp", "lr"
8386 };
c906108c 8387 int regno;
faa95490 8388 int offset = strlen (name) - 2;
c906108c
SS
8389
8390 for (regno = 0; regno <= 14; regno++)
faa95490 8391 if (strcmp (&name[offset], table[regno]) == 0)
52f729a7 8392 return get_frame_register_unsigned (frame, regno);
c906108c 8393 }
ed9a39eb 8394
faa95490
DJ
8395 /* GNU ld generates __foo_from_arm or __foo_from_thumb for
8396 non-interworking calls to foo. We could decode the stubs
8397 to find the target but it's easier to use the symbol table. */
8398 namelen = strlen (name);
8399 if (name[0] == '_' && name[1] == '_'
8400 && ((namelen > 2 + strlen ("_from_thumb")
61012eef 8401 && startswith (name + namelen - strlen ("_from_thumb"), "_from_thumb"))
faa95490 8402 || (namelen > 2 + strlen ("_from_arm")
61012eef 8403 && startswith (name + namelen - strlen ("_from_arm"), "_from_arm"))))
faa95490
DJ
8404 {
8405 char *target_name;
8406 int target_len = namelen - 2;
3b7344d5 8407 struct bound_minimal_symbol minsym;
faa95490
DJ
8408 struct objfile *objfile;
8409 struct obj_section *sec;
8410
8411 if (name[namelen - 1] == 'b')
8412 target_len -= strlen ("_from_thumb");
8413 else
8414 target_len -= strlen ("_from_arm");
8415
224c3ddb 8416 target_name = (char *) alloca (target_len + 1);
faa95490
DJ
8417 memcpy (target_name, name + 2, target_len);
8418 target_name[target_len] = '\0';
8419
8420 sec = find_pc_section (pc);
8421 objfile = (sec == NULL) ? NULL : sec->objfile;
8422 minsym = lookup_minimal_symbol (target_name, NULL, objfile);
3b7344d5 8423 if (minsym.minsym != NULL)
77e371c0 8424 return BMSYMBOL_VALUE_ADDRESS (minsym);
faa95490
DJ
8425 else
8426 return 0;
8427 }
8428
40eadf04
SP
8429 struct obj_section *section = find_pc_section (pc);
8430
8431 /* Check whether SECTION points to the ".gnu.sgstubs" section. */
8432 if (arm_is_sgstubs_section (section))
8433 return arm_skip_cmse_entry (pc, name, section->objfile);
8434
c5aa993b 8435 return 0; /* not a stub */
c906108c
SS
8436}
8437
28e97307
DJ
8438static void
8439arm_update_current_architecture (void)
fd50bc42 8440{
28e97307 8441 struct gdbarch_info info;
fd50bc42 8442
28e97307 8443 /* If the current architecture is not ARM, we have nothing to do. */
f5656ead 8444 if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_arm)
28e97307 8445 return;
fd50bc42 8446
28e97307
DJ
8447 /* Update the architecture. */
8448 gdbarch_info_init (&info);
fd50bc42 8449
28e97307 8450 if (!gdbarch_update_p (info))
9b20d036 8451 internal_error (__FILE__, __LINE__, _("could not update architecture"));
fd50bc42
RE
8452}
8453
8454static void
eb4c3f4a 8455set_fp_model_sfunc (const char *args, int from_tty,
fd50bc42
RE
8456 struct cmd_list_element *c)
8457{
570dc176 8458 int fp_model;
fd50bc42
RE
8459
8460 for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
8461 if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
8462 {
aead7601 8463 arm_fp_model = (enum arm_float_model) fp_model;
fd50bc42
RE
8464 break;
8465 }
8466
8467 if (fp_model == ARM_FLOAT_LAST)
edefbb7c 8468 internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
fd50bc42
RE
8469 current_fp_model);
8470
28e97307 8471 arm_update_current_architecture ();
fd50bc42
RE
8472}
8473
8474static void
08546159
AC
8475show_fp_model (struct ui_file *file, int from_tty,
8476 struct cmd_list_element *c, const char *value)
fd50bc42 8477{
f5656ead 8478 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
fd50bc42 8479
28e97307 8480 if (arm_fp_model == ARM_FLOAT_AUTO
f5656ead 8481 && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
28e97307
DJ
8482 fprintf_filtered (file, _("\
8483The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
8484 fp_model_strings[tdep->fp_model]);
8485 else
8486 fprintf_filtered (file, _("\
8487The current ARM floating point model is \"%s\".\n"),
8488 fp_model_strings[arm_fp_model]);
8489}
8490
8491static void
eb4c3f4a 8492arm_set_abi (const char *args, int from_tty,
28e97307
DJ
8493 struct cmd_list_element *c)
8494{
570dc176 8495 int arm_abi;
28e97307
DJ
8496
8497 for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
8498 if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
8499 {
aead7601 8500 arm_abi_global = (enum arm_abi_kind) arm_abi;
28e97307
DJ
8501 break;
8502 }
8503
8504 if (arm_abi == ARM_ABI_LAST)
8505 internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
8506 arm_abi_string);
8507
8508 arm_update_current_architecture ();
8509}
8510
8511static void
8512arm_show_abi (struct ui_file *file, int from_tty,
8513 struct cmd_list_element *c, const char *value)
8514{
f5656ead 8515 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
28e97307
DJ
8516
8517 if (arm_abi_global == ARM_ABI_AUTO
f5656ead 8518 && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
28e97307
DJ
8519 fprintf_filtered (file, _("\
8520The current ARM ABI is \"auto\" (currently \"%s\").\n"),
8521 arm_abi_strings[tdep->arm_abi]);
8522 else
8523 fprintf_filtered (file, _("The current ARM ABI is \"%s\".\n"),
8524 arm_abi_string);
fd50bc42
RE
8525}
8526
0428b8f5
DJ
8527static void
8528arm_show_fallback_mode (struct ui_file *file, int from_tty,
8529 struct cmd_list_element *c, const char *value)
8530{
0963b4bd
MS
8531 fprintf_filtered (file,
8532 _("The current execution mode assumed "
8533 "(when symbols are unavailable) is \"%s\".\n"),
0428b8f5
DJ
8534 arm_fallback_mode_string);
8535}
8536
8537static void
8538arm_show_force_mode (struct ui_file *file, int from_tty,
8539 struct cmd_list_element *c, const char *value)
8540{
0963b4bd
MS
8541 fprintf_filtered (file,
8542 _("The current execution mode assumed "
8543 "(even when symbols are available) is \"%s\".\n"),
0428b8f5
DJ
8544 arm_force_mode_string);
8545}
8546
afd7eef0
RE
8547/* If the user changes the register disassembly style used for info
8548 register and other commands, we have to also switch the style used
8549 in opcodes for disassembly output. This function is run in the "set
8550 arm disassembly" command, and does that. */
bc90b915
FN
8551
8552static void
eb4c3f4a 8553set_disassembly_style_sfunc (const char *args, int from_tty,
65b48a81 8554 struct cmd_list_element *c)
bc90b915 8555{
65b48a81
PB
8556 /* Convert the short style name into the long style name (eg, reg-names-*)
8557 before calling the generic set_disassembler_options() function. */
8558 std::string long_name = std::string ("reg-names-") + disassembly_style;
8559 set_disassembler_options (&long_name[0]);
8560}
8561
8562static void
8563show_disassembly_style_sfunc (struct ui_file *file, int from_tty,
8564 struct cmd_list_element *c, const char *value)
8565{
8566 struct gdbarch *gdbarch = get_current_arch ();
8567 char *options = get_disassembler_options (gdbarch);
8568 const char *style = "";
8569 int len = 0;
f995bbe8 8570 const char *opt;
65b48a81
PB
8571
8572 FOR_EACH_DISASSEMBLER_OPTION (opt, options)
8573 if (CONST_STRNEQ (opt, "reg-names-"))
8574 {
8575 style = &opt[strlen ("reg-names-")];
8576 len = strcspn (style, ",");
8577 }
8578
8579 fprintf_unfiltered (file, "The disassembly style is \"%.*s\".\n", len, style);
bc90b915
FN
8580}
8581\f
966fbf70 8582/* Return the ARM register name corresponding to register I. */
a208b0cb 8583static const char *
d93859e2 8584arm_register_name (struct gdbarch *gdbarch, int i)
966fbf70 8585{
58d6951d
DJ
8586 const int num_regs = gdbarch_num_regs (gdbarch);
8587
8588 if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
8589 && i >= num_regs && i < num_regs + 32)
8590 {
8591 static const char *const vfp_pseudo_names[] = {
8592 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
8593 "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
8594 "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
8595 "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
8596 };
8597
8598 return vfp_pseudo_names[i - num_regs];
8599 }
8600
8601 if (gdbarch_tdep (gdbarch)->have_neon_pseudos
8602 && i >= num_regs + 32 && i < num_regs + 32 + 16)
8603 {
8604 static const char *const neon_pseudo_names[] = {
8605 "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
8606 "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
8607 };
8608
8609 return neon_pseudo_names[i - num_regs - 32];
8610 }
8611
ff6f572f
DJ
8612 if (i >= ARRAY_SIZE (arm_register_names))
8613 /* These registers are only supported on targets which supply
8614 an XML description. */
8615 return "";
8616
966fbf70
RE
8617 return arm_register_names[i];
8618}
8619
082fc60d
RE
8620/* Test whether the coff symbol specific value corresponds to a Thumb
8621 function. */
8622
8623static int
8624coff_sym_is_thumb (int val)
8625{
f8bf5763
PM
8626 return (val == C_THUMBEXT
8627 || val == C_THUMBSTAT
8628 || val == C_THUMBEXTFUNC
8629 || val == C_THUMBSTATFUNC
8630 || val == C_THUMBLABEL);
082fc60d
RE
8631}
8632
8633/* arm_coff_make_msymbol_special()
8634 arm_elf_make_msymbol_special()
8635
8636 These functions test whether the COFF or ELF symbol corresponds to
8637 an address in thumb code, and set a "special" bit in a minimal
8638 symbol to indicate that it does. */
8639
34e8f22d 8640static void
082fc60d
RE
8641arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
8642{
39d911fc
TP
8643 elf_symbol_type *elfsym = (elf_symbol_type *) sym;
8644
8645 if (ARM_GET_SYM_BRANCH_TYPE (elfsym->internal_elf_sym.st_target_internal)
467d42c4 8646 == ST_BRANCH_TO_THUMB)
082fc60d
RE
8647 MSYMBOL_SET_SPECIAL (msym);
8648}
8649
34e8f22d 8650static void
082fc60d
RE
8651arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
8652{
8653 if (coff_sym_is_thumb (val))
8654 MSYMBOL_SET_SPECIAL (msym);
8655}
8656
60c5725c
DJ
8657static void
8658arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
8659 asymbol *sym)
8660{
8661 const char *name = bfd_asymbol_name (sym);
bd5766ec 8662 struct arm_per_bfd *data;
60c5725c
DJ
8663 struct arm_mapping_symbol new_map_sym;
8664
8665 gdb_assert (name[0] == '$');
8666 if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
8667 return;
8668
bd5766ec 8669 data = arm_bfd_data_key.get (objfile->obfd);
60c5725c 8670 if (data == NULL)
bd5766ec
LM
8671 data = arm_bfd_data_key.emplace (objfile->obfd,
8672 objfile->obfd->section_count);
54cc7474 8673 arm_mapping_symbol_vec &map
e6f7f6d1 8674 = data->section_maps[bfd_asymbol_section (sym)->index];
60c5725c
DJ
8675
8676 new_map_sym.value = sym->value;
8677 new_map_sym.type = name[1];
8678
4838e44c
SM
8679 /* Insert at the end, the vector will be sorted on first use. */
8680 map.push_back (new_map_sym);
60c5725c
DJ
8681}
8682
756fe439 8683static void
61a1198a 8684arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
756fe439 8685{
ac7936df 8686 struct gdbarch *gdbarch = regcache->arch ();
61a1198a 8687 regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
756fe439
DJ
8688
8689 /* If necessary, set the T bit. */
8690 if (arm_apcs_32)
8691 {
9779414d 8692 ULONGEST val, t_bit;
61a1198a 8693 regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
9779414d
DJ
8694 t_bit = arm_psr_thumb_bit (gdbarch);
8695 if (arm_pc_is_thumb (gdbarch, pc))
8696 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
8697 val | t_bit);
756fe439 8698 else
61a1198a 8699 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9779414d 8700 val & ~t_bit);
756fe439
DJ
8701 }
8702}
123dc839 8703
58d6951d
DJ
8704/* Read the contents of a NEON quad register, by reading from two
8705 double registers. This is used to implement the quad pseudo
8706 registers, and for argument passing in case the quad registers are
8707 missing; vectors are passed in quad registers when using the VFP
8708 ABI, even if a NEON unit is not present. REGNUM is the index of
8709 the quad register, in [0, 15]. */
8710
05d1431c 8711static enum register_status
849d0ba8 8712arm_neon_quad_read (struct gdbarch *gdbarch, readable_regcache *regcache,
58d6951d
DJ
8713 int regnum, gdb_byte *buf)
8714{
8715 char name_buf[4];
8716 gdb_byte reg_buf[8];
8717 int offset, double_regnum;
05d1431c 8718 enum register_status status;
58d6951d 8719
8c042590 8720 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
58d6951d
DJ
8721 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8722 strlen (name_buf));
8723
8724 /* d0 is always the least significant half of q0. */
8725 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
8726 offset = 8;
8727 else
8728 offset = 0;
8729
03f50fc8 8730 status = regcache->raw_read (double_regnum, reg_buf);
05d1431c
PA
8731 if (status != REG_VALID)
8732 return status;
58d6951d
DJ
8733 memcpy (buf + offset, reg_buf, 8);
8734
8735 offset = 8 - offset;
03f50fc8 8736 status = regcache->raw_read (double_regnum + 1, reg_buf);
05d1431c
PA
8737 if (status != REG_VALID)
8738 return status;
58d6951d 8739 memcpy (buf + offset, reg_buf, 8);
05d1431c
PA
8740
8741 return REG_VALID;
58d6951d
DJ
8742}
8743
05d1431c 8744static enum register_status
849d0ba8 8745arm_pseudo_read (struct gdbarch *gdbarch, readable_regcache *regcache,
58d6951d
DJ
8746 int regnum, gdb_byte *buf)
8747{
8748 const int num_regs = gdbarch_num_regs (gdbarch);
8749 char name_buf[4];
8750 gdb_byte reg_buf[8];
8751 int offset, double_regnum;
8752
8753 gdb_assert (regnum >= num_regs);
8754 regnum -= num_regs;
8755
8756 if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
8757 /* Quad-precision register. */
05d1431c 8758 return arm_neon_quad_read (gdbarch, regcache, regnum - 32, buf);
58d6951d
DJ
8759 else
8760 {
05d1431c
PA
8761 enum register_status status;
8762
58d6951d
DJ
8763 /* Single-precision register. */
8764 gdb_assert (regnum < 32);
8765
8766 /* s0 is always the least significant half of d0. */
8767 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
8768 offset = (regnum & 1) ? 0 : 4;
8769 else
8770 offset = (regnum & 1) ? 4 : 0;
8771
8c042590 8772 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
58d6951d
DJ
8773 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8774 strlen (name_buf));
8775
03f50fc8 8776 status = regcache->raw_read (double_regnum, reg_buf);
05d1431c
PA
8777 if (status == REG_VALID)
8778 memcpy (buf, reg_buf + offset, 4);
8779 return status;
58d6951d
DJ
8780 }
8781}
8782
8783/* Store the contents of BUF to a NEON quad register, by writing to
8784 two double registers. This is used to implement the quad pseudo
8785 registers, and for argument passing in case the quad registers are
8786 missing; vectors are passed in quad registers when using the VFP
8787 ABI, even if a NEON unit is not present. REGNUM is the index
8788 of the quad register, in [0, 15]. */
8789
8790static void
8791arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
8792 int regnum, const gdb_byte *buf)
8793{
8794 char name_buf[4];
58d6951d
DJ
8795 int offset, double_regnum;
8796
8c042590 8797 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
58d6951d
DJ
8798 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8799 strlen (name_buf));
8800
8801 /* d0 is always the least significant half of q0. */
8802 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
8803 offset = 8;
8804 else
8805 offset = 0;
8806
10eaee5f 8807 regcache->raw_write (double_regnum, buf + offset);
58d6951d 8808 offset = 8 - offset;
10eaee5f 8809 regcache->raw_write (double_regnum + 1, buf + offset);
58d6951d
DJ
8810}
8811
8812static void
8813arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
8814 int regnum, const gdb_byte *buf)
8815{
8816 const int num_regs = gdbarch_num_regs (gdbarch);
8817 char name_buf[4];
8818 gdb_byte reg_buf[8];
8819 int offset, double_regnum;
8820
8821 gdb_assert (regnum >= num_regs);
8822 regnum -= num_regs;
8823
8824 if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
8825 /* Quad-precision register. */
8826 arm_neon_quad_write (gdbarch, regcache, regnum - 32, buf);
8827 else
8828 {
8829 /* Single-precision register. */
8830 gdb_assert (regnum < 32);
8831
8832 /* s0 is always the least significant half of d0. */
8833 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
8834 offset = (regnum & 1) ? 0 : 4;
8835 else
8836 offset = (regnum & 1) ? 4 : 0;
8837
8c042590 8838 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
58d6951d
DJ
8839 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8840 strlen (name_buf));
8841
0b883586 8842 regcache->raw_read (double_regnum, reg_buf);
58d6951d 8843 memcpy (reg_buf + offset, buf, 4);
10eaee5f 8844 regcache->raw_write (double_regnum, reg_buf);
58d6951d
DJ
8845 }
8846}
8847
123dc839
DJ
8848static struct value *
8849value_of_arm_user_reg (struct frame_info *frame, const void *baton)
8850{
9a3c8263 8851 const int *reg_p = (const int *) baton;
123dc839
DJ
8852 return value_of_register (*reg_p, frame);
8853}
97e03143 8854\f
70f80edf
JT
8855static enum gdb_osabi
8856arm_elf_osabi_sniffer (bfd *abfd)
97e03143 8857{
2af48f68 8858 unsigned int elfosabi;
70f80edf 8859 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
97e03143 8860
70f80edf 8861 elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
97e03143 8862
28e97307
DJ
8863 if (elfosabi == ELFOSABI_ARM)
8864 /* GNU tools use this value. Check note sections in this case,
8865 as well. */
8866 bfd_map_over_sections (abfd,
8867 generic_elf_osabi_sniff_abi_tag_sections,
8868 &osabi);
97e03143 8869
28e97307 8870 /* Anything else will be handled by the generic ELF sniffer. */
70f80edf 8871 return osabi;
97e03143
RE
8872}
8873
54483882
YQ
8874static int
8875arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
8876 struct reggroup *group)
8877{
2c291032
YQ
8878 /* FPS register's type is INT, but belongs to float_reggroup. Beside
8879 this, FPS register belongs to save_regroup, restore_reggroup, and
8880 all_reggroup, of course. */
54483882 8881 if (regnum == ARM_FPS_REGNUM)
2c291032
YQ
8882 return (group == float_reggroup
8883 || group == save_reggroup
8884 || group == restore_reggroup
8885 || group == all_reggroup);
54483882
YQ
8886 else
8887 return default_register_reggroup_p (gdbarch, regnum, group);
8888}
8889
25f8c692
JL
8890/* For backward-compatibility we allow two 'g' packet lengths with
8891 the remote protocol depending on whether FPA registers are
8892 supplied. M-profile targets do not have FPA registers, but some
8893 stubs already exist in the wild which use a 'g' packet which
8894 supplies them albeit with dummy values. The packet format which
8895 includes FPA registers should be considered deprecated for
8896 M-profile targets. */
8897
8898static void
8899arm_register_g_packet_guesses (struct gdbarch *gdbarch)
8900{
8901 if (gdbarch_tdep (gdbarch)->is_m)
8902 {
d105cce5
AH
8903 const target_desc *tdesc;
8904
25f8c692
JL
8905 /* If we know from the executable this is an M-profile target,
8906 cater for remote targets whose register set layout is the
8907 same as the FPA layout. */
d105cce5 8908 tdesc = arm_read_mprofile_description (ARM_M_TYPE_WITH_FPA);
25f8c692 8909 register_remote_g_packet_guess (gdbarch,
350fab54 8910 ARM_CORE_REGS_SIZE + ARM_FP_REGS_SIZE,
d105cce5 8911 tdesc);
25f8c692
JL
8912
8913 /* The regular M-profile layout. */
d105cce5 8914 tdesc = arm_read_mprofile_description (ARM_M_TYPE_M_PROFILE);
350fab54 8915 register_remote_g_packet_guess (gdbarch, ARM_CORE_REGS_SIZE,
d105cce5 8916 tdesc);
3184d3f9
JL
8917
8918 /* M-profile plus M4F VFP. */
d105cce5 8919 tdesc = arm_read_mprofile_description (ARM_M_TYPE_VFP_D16);
3184d3f9 8920 register_remote_g_packet_guess (gdbarch,
350fab54 8921 ARM_CORE_REGS_SIZE + ARM_VFP2_REGS_SIZE,
d105cce5 8922 tdesc);
25f8c692
JL
8923 }
8924
8925 /* Otherwise we don't have a useful guess. */
8926}
8927
7eb89530
YQ
8928/* Implement the code_of_frame_writable gdbarch method. */
8929
8930static int
8931arm_code_of_frame_writable (struct gdbarch *gdbarch, struct frame_info *frame)
8932{
8933 if (gdbarch_tdep (gdbarch)->is_m
8934 && get_frame_type (frame) == SIGTRAMP_FRAME)
8935 {
8936 /* M-profile exception frames return to some magic PCs, where
8937 isn't writable at all. */
8938 return 0;
8939 }
8940 else
8941 return 1;
8942}
8943
3426ae57
AH
8944/* Implement gdbarch_gnu_triplet_regexp. If the arch name is arm then allow it
8945 to be postfixed by a version (eg armv7hl). */
8946
8947static const char *
8948arm_gnu_triplet_regexp (struct gdbarch *gdbarch)
8949{
8950 if (strcmp (gdbarch_bfd_arch_info (gdbarch)->arch_name, "arm") == 0)
8951 return "arm(v[^- ]*)?";
8952 return gdbarch_bfd_arch_info (gdbarch)->arch_name;
8953}
8954
da3c6d4a
MS
8955/* Initialize the current architecture based on INFO. If possible,
8956 re-use an architecture from ARCHES, which is a list of
8957 architectures already created during this debugging session.
97e03143 8958
da3c6d4a
MS
8959 Called e.g. at program startup, when reading a core file, and when
8960 reading a binary file. */
97e03143 8961
39bbf761
RE
8962static struct gdbarch *
8963arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
8964{
97e03143 8965 struct gdbarch_tdep *tdep;
39bbf761 8966 struct gdbarch *gdbarch;
28e97307
DJ
8967 struct gdbarch_list *best_arch;
8968 enum arm_abi_kind arm_abi = arm_abi_global;
8969 enum arm_float_model fp_model = arm_fp_model;
123dc839 8970 struct tdesc_arch_data *tdesc_data = NULL;
7559c217
CB
8971 int i;
8972 bool is_m = false;
8973 int vfp_register_count = 0;
8974 bool have_vfp_pseudos = false, have_neon_pseudos = false;
8975 bool have_wmmx_registers = false;
8976 bool have_neon = false;
8977 bool have_fpa_registers = true;
9779414d
DJ
8978 const struct target_desc *tdesc = info.target_desc;
8979
8980 /* If we have an object to base this architecture on, try to determine
8981 its ABI. */
8982
8983 if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
8984 {
8985 int ei_osabi, e_flags;
8986
8987 switch (bfd_get_flavour (info.abfd))
8988 {
9779414d
DJ
8989 case bfd_target_coff_flavour:
8990 /* Assume it's an old APCS-style ABI. */
8991 /* XXX WinCE? */
8992 arm_abi = ARM_ABI_APCS;
8993 break;
8994
8995 case bfd_target_elf_flavour:
8996 ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
8997 e_flags = elf_elfheader (info.abfd)->e_flags;
8998
8999 if (ei_osabi == ELFOSABI_ARM)
9000 {
9001 /* GNU tools used to use this value, but do not for EABI
9002 objects. There's nowhere to tag an EABI version
9003 anyway, so assume APCS. */
9004 arm_abi = ARM_ABI_APCS;
9005 }
d403db27 9006 else if (ei_osabi == ELFOSABI_NONE || ei_osabi == ELFOSABI_GNU)
9779414d
DJ
9007 {
9008 int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
9779414d
DJ
9009
9010 switch (eabi_ver)
9011 {
9012 case EF_ARM_EABI_UNKNOWN:
9013 /* Assume GNU tools. */
9014 arm_abi = ARM_ABI_APCS;
9015 break;
9016
9017 case EF_ARM_EABI_VER4:
9018 case EF_ARM_EABI_VER5:
9019 arm_abi = ARM_ABI_AAPCS;
9020 /* EABI binaries default to VFP float ordering.
9021 They may also contain build attributes that can
9022 be used to identify if the VFP argument-passing
9023 ABI is in use. */
9024 if (fp_model == ARM_FLOAT_AUTO)
9025 {
9026#ifdef HAVE_ELF
9027 switch (bfd_elf_get_obj_attr_int (info.abfd,
9028 OBJ_ATTR_PROC,
9029 Tag_ABI_VFP_args))
9030 {
b35b0298 9031 case AEABI_VFP_args_base:
9779414d
DJ
9032 /* "The user intended FP parameter/result
9033 passing to conform to AAPCS, base
9034 variant". */
9035 fp_model = ARM_FLOAT_SOFT_VFP;
9036 break;
b35b0298 9037 case AEABI_VFP_args_vfp:
9779414d
DJ
9038 /* "The user intended FP parameter/result
9039 passing to conform to AAPCS, VFP
9040 variant". */
9041 fp_model = ARM_FLOAT_VFP;
9042 break;
b35b0298 9043 case AEABI_VFP_args_toolchain:
9779414d
DJ
9044 /* "The user intended FP parameter/result
9045 passing to conform to tool chain-specific
9046 conventions" - we don't know any such
9047 conventions, so leave it as "auto". */
9048 break;
b35b0298 9049 case AEABI_VFP_args_compatible:
5c294fee
TG
9050 /* "Code is compatible with both the base
9051 and VFP variants; the user did not permit
9052 non-variadic functions to pass FP
9053 parameters/results" - leave it as
9054 "auto". */
9055 break;
9779414d
DJ
9056 default:
9057 /* Attribute value not mentioned in the
5c294fee 9058 November 2012 ABI, so leave it as
9779414d
DJ
9059 "auto". */
9060 break;
9061 }
9062#else
9063 fp_model = ARM_FLOAT_SOFT_VFP;
9064#endif
9065 }
9066 break;
9067
9068 default:
9069 /* Leave it as "auto". */
9070 warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
9071 break;
9072 }
9073
9074#ifdef HAVE_ELF
9075 /* Detect M-profile programs. This only works if the
9076 executable file includes build attributes; GCC does
9077 copy them to the executable, but e.g. RealView does
9078 not. */
17cbafdb
SM
9079 int attr_arch
9080 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9081 Tag_CPU_arch);
9082 int attr_profile
9083 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9084 Tag_CPU_arch_profile);
9085
9779414d
DJ
9086 /* GCC specifies the profile for v6-M; RealView only
9087 specifies the profile for architectures starting with
9088 V7 (as opposed to architectures with a tag
9089 numerically greater than TAG_CPU_ARCH_V7). */
9090 if (!tdesc_has_registers (tdesc)
9091 && (attr_arch == TAG_CPU_ARCH_V6_M
9092 || attr_arch == TAG_CPU_ARCH_V6S_M
9093 || attr_profile == 'M'))
7559c217 9094 is_m = true;
9779414d
DJ
9095#endif
9096 }
9097
9098 if (fp_model == ARM_FLOAT_AUTO)
9099 {
9779414d
DJ
9100 switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
9101 {
9102 case 0:
9103 /* Leave it as "auto". Strictly speaking this case
9104 means FPA, but almost nobody uses that now, and
9105 many toolchains fail to set the appropriate bits
9106 for the floating-point model they use. */
9107 break;
9108 case EF_ARM_SOFT_FLOAT:
9109 fp_model = ARM_FLOAT_SOFT_FPA;
9110 break;
9111 case EF_ARM_VFP_FLOAT:
9112 fp_model = ARM_FLOAT_VFP;
9113 break;
9114 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
9115 fp_model = ARM_FLOAT_SOFT_VFP;
9116 break;
9117 }
9118 }
9119
9120 if (e_flags & EF_ARM_BE8)
9121 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
9122
9123 break;
9124
9125 default:
9126 /* Leave it as "auto". */
9127 break;
9128 }
9129 }
123dc839
DJ
9130
9131 /* Check any target description for validity. */
9779414d 9132 if (tdesc_has_registers (tdesc))
123dc839
DJ
9133 {
9134 /* For most registers we require GDB's default names; but also allow
9135 the numeric names for sp / lr / pc, as a convenience. */
9136 static const char *const arm_sp_names[] = { "r13", "sp", NULL };
9137 static const char *const arm_lr_names[] = { "r14", "lr", NULL };
9138 static const char *const arm_pc_names[] = { "r15", "pc", NULL };
9139
9140 const struct tdesc_feature *feature;
58d6951d 9141 int valid_p;
123dc839 9142
9779414d 9143 feature = tdesc_find_feature (tdesc,
123dc839
DJ
9144 "org.gnu.gdb.arm.core");
9145 if (feature == NULL)
9779414d
DJ
9146 {
9147 feature = tdesc_find_feature (tdesc,
9148 "org.gnu.gdb.arm.m-profile");
9149 if (feature == NULL)
9150 return NULL;
9151 else
7559c217 9152 is_m = true;
9779414d 9153 }
123dc839
DJ
9154
9155 tdesc_data = tdesc_data_alloc ();
9156
9157 valid_p = 1;
9158 for (i = 0; i < ARM_SP_REGNUM; i++)
9159 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
9160 arm_register_names[i]);
9161 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
9162 ARM_SP_REGNUM,
9163 arm_sp_names);
9164 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
9165 ARM_LR_REGNUM,
9166 arm_lr_names);
9167 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
9168 ARM_PC_REGNUM,
9169 arm_pc_names);
9779414d
DJ
9170 if (is_m)
9171 valid_p &= tdesc_numbered_register (feature, tdesc_data,
9172 ARM_PS_REGNUM, "xpsr");
9173 else
9174 valid_p &= tdesc_numbered_register (feature, tdesc_data,
9175 ARM_PS_REGNUM, "cpsr");
123dc839
DJ
9176
9177 if (!valid_p)
9178 {
9179 tdesc_data_cleanup (tdesc_data);
9180 return NULL;
9181 }
9182
9779414d 9183 feature = tdesc_find_feature (tdesc,
123dc839
DJ
9184 "org.gnu.gdb.arm.fpa");
9185 if (feature != NULL)
9186 {
9187 valid_p = 1;
9188 for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
9189 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
9190 arm_register_names[i]);
9191 if (!valid_p)
9192 {
9193 tdesc_data_cleanup (tdesc_data);
9194 return NULL;
9195 }
9196 }
ff6f572f 9197 else
7559c217 9198 have_fpa_registers = false;
ff6f572f 9199
9779414d 9200 feature = tdesc_find_feature (tdesc,
ff6f572f
DJ
9201 "org.gnu.gdb.xscale.iwmmxt");
9202 if (feature != NULL)
9203 {
9204 static const char *const iwmmxt_names[] = {
9205 "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
9206 "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
9207 "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
9208 "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
9209 };
9210
9211 valid_p = 1;
9212 for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
9213 valid_p
9214 &= tdesc_numbered_register (feature, tdesc_data, i,
9215 iwmmxt_names[i - ARM_WR0_REGNUM]);
9216
9217 /* Check for the control registers, but do not fail if they
9218 are missing. */
9219 for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
9220 tdesc_numbered_register (feature, tdesc_data, i,
9221 iwmmxt_names[i - ARM_WR0_REGNUM]);
9222
9223 for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
9224 valid_p
9225 &= tdesc_numbered_register (feature, tdesc_data, i,
9226 iwmmxt_names[i - ARM_WR0_REGNUM]);
9227
9228 if (!valid_p)
9229 {
9230 tdesc_data_cleanup (tdesc_data);
9231 return NULL;
9232 }
a56cc1ce 9233
7559c217 9234 have_wmmx_registers = true;
ff6f572f 9235 }
58d6951d
DJ
9236
9237 /* If we have a VFP unit, check whether the single precision registers
9238 are present. If not, then we will synthesize them as pseudo
9239 registers. */
9779414d 9240 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
9241 "org.gnu.gdb.arm.vfp");
9242 if (feature != NULL)
9243 {
9244 static const char *const vfp_double_names[] = {
9245 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
9246 "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
9247 "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
9248 "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
9249 };
9250
9251 /* Require the double precision registers. There must be either
9252 16 or 32. */
9253 valid_p = 1;
9254 for (i = 0; i < 32; i++)
9255 {
9256 valid_p &= tdesc_numbered_register (feature, tdesc_data,
9257 ARM_D0_REGNUM + i,
9258 vfp_double_names[i]);
9259 if (!valid_p)
9260 break;
9261 }
2b9e5ea6
UW
9262 if (!valid_p && i == 16)
9263 valid_p = 1;
58d6951d 9264
2b9e5ea6
UW
9265 /* Also require FPSCR. */
9266 valid_p &= tdesc_numbered_register (feature, tdesc_data,
9267 ARM_FPSCR_REGNUM, "fpscr");
9268 if (!valid_p)
58d6951d
DJ
9269 {
9270 tdesc_data_cleanup (tdesc_data);
9271 return NULL;
9272 }
9273
9274 if (tdesc_unnumbered_register (feature, "s0") == 0)
7559c217 9275 have_vfp_pseudos = true;
58d6951d 9276
330c6ca9 9277 vfp_register_count = i;
58d6951d
DJ
9278
9279 /* If we have VFP, also check for NEON. The architecture allows
9280 NEON without VFP (integer vector operations only), but GDB
9281 does not support that. */
9779414d 9282 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
9283 "org.gnu.gdb.arm.neon");
9284 if (feature != NULL)
9285 {
9286 /* NEON requires 32 double-precision registers. */
9287 if (i != 32)
9288 {
9289 tdesc_data_cleanup (tdesc_data);
9290 return NULL;
9291 }
9292
9293 /* If there are quad registers defined by the stub, use
9294 their type; otherwise (normally) provide them with
9295 the default type. */
9296 if (tdesc_unnumbered_register (feature, "q0") == 0)
7559c217 9297 have_neon_pseudos = true;
58d6951d 9298
7559c217 9299 have_neon = true;
58d6951d
DJ
9300 }
9301 }
123dc839 9302 }
39bbf761 9303
28e97307
DJ
9304 /* If there is already a candidate, use it. */
9305 for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
9306 best_arch != NULL;
9307 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
9308 {
b8926edc
DJ
9309 if (arm_abi != ARM_ABI_AUTO
9310 && arm_abi != gdbarch_tdep (best_arch->gdbarch)->arm_abi)
28e97307
DJ
9311 continue;
9312
b8926edc
DJ
9313 if (fp_model != ARM_FLOAT_AUTO
9314 && fp_model != gdbarch_tdep (best_arch->gdbarch)->fp_model)
28e97307
DJ
9315 continue;
9316
58d6951d
DJ
9317 /* There are various other properties in tdep that we do not
9318 need to check here: those derived from a target description,
9319 since gdbarches with a different target description are
9320 automatically disqualified. */
9321
9779414d
DJ
9322 /* Do check is_m, though, since it might come from the binary. */
9323 if (is_m != gdbarch_tdep (best_arch->gdbarch)->is_m)
9324 continue;
9325
28e97307
DJ
9326 /* Found a match. */
9327 break;
9328 }
97e03143 9329
28e97307 9330 if (best_arch != NULL)
123dc839
DJ
9331 {
9332 if (tdesc_data != NULL)
9333 tdesc_data_cleanup (tdesc_data);
9334 return best_arch->gdbarch;
9335 }
28e97307 9336
8d749320 9337 tdep = XCNEW (struct gdbarch_tdep);
97e03143
RE
9338 gdbarch = gdbarch_alloc (&info, tdep);
9339
28e97307
DJ
9340 /* Record additional information about the architecture we are defining.
9341 These are gdbarch discriminators, like the OSABI. */
9342 tdep->arm_abi = arm_abi;
9343 tdep->fp_model = fp_model;
9779414d 9344 tdep->is_m = is_m;
ff6f572f 9345 tdep->have_fpa_registers = have_fpa_registers;
a56cc1ce 9346 tdep->have_wmmx_registers = have_wmmx_registers;
330c6ca9
YQ
9347 gdb_assert (vfp_register_count == 0
9348 || vfp_register_count == 16
9349 || vfp_register_count == 32);
9350 tdep->vfp_register_count = vfp_register_count;
58d6951d
DJ
9351 tdep->have_vfp_pseudos = have_vfp_pseudos;
9352 tdep->have_neon_pseudos = have_neon_pseudos;
9353 tdep->have_neon = have_neon;
08216dd7 9354
25f8c692
JL
9355 arm_register_g_packet_guesses (gdbarch);
9356
08216dd7 9357 /* Breakpoints. */
9d4fde75 9358 switch (info.byte_order_for_code)
67255d04
RE
9359 {
9360 case BFD_ENDIAN_BIG:
66e810cd
RE
9361 tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
9362 tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
9363 tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
9364 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
9365
67255d04
RE
9366 break;
9367
9368 case BFD_ENDIAN_LITTLE:
66e810cd
RE
9369 tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
9370 tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
9371 tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
9372 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
9373
67255d04
RE
9374 break;
9375
9376 default:
9377 internal_error (__FILE__, __LINE__,
edefbb7c 9378 _("arm_gdbarch_init: bad byte order for float format"));
67255d04
RE
9379 }
9380
d7b486e7
RE
9381 /* On ARM targets char defaults to unsigned. */
9382 set_gdbarch_char_signed (gdbarch, 0);
9383
53375380
PA
9384 /* wchar_t is unsigned under the AAPCS. */
9385 if (tdep->arm_abi == ARM_ABI_AAPCS)
9386 set_gdbarch_wchar_signed (gdbarch, 0);
9387 else
9388 set_gdbarch_wchar_signed (gdbarch, 1);
53375380 9389
030197b4
AB
9390 /* Compute type alignment. */
9391 set_gdbarch_type_align (gdbarch, arm_type_align);
9392
cca44b1b
JB
9393 /* Note: for displaced stepping, this includes the breakpoint, and one word
9394 of additional scratch space. This setting isn't used for anything beside
9395 displaced stepping at present. */
e935475c 9396 set_gdbarch_max_insn_length (gdbarch, 4 * ARM_DISPLACED_MODIFIED_INSNS);
cca44b1b 9397
9df628e0 9398 /* This should be low enough for everything. */
97e03143 9399 tdep->lowest_pc = 0x20;
94c30b78 9400 tdep->jb_pc = -1; /* Longjump support not enabled by default. */
97e03143 9401
7c00367c
MK
9402 /* The default, for both APCS and AAPCS, is to return small
9403 structures in registers. */
9404 tdep->struct_return = reg_struct_return;
9405
2dd604e7 9406 set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
f53f0d0b 9407 set_gdbarch_frame_align (gdbarch, arm_frame_align);
39bbf761 9408
7eb89530
YQ
9409 if (is_m)
9410 set_gdbarch_code_of_frame_writable (gdbarch, arm_code_of_frame_writable);
9411
756fe439
DJ
9412 set_gdbarch_write_pc (gdbarch, arm_write_pc);
9413
eb5492fa 9414 frame_base_set_default (gdbarch, &arm_normal_base);
148754e5 9415
34e8f22d 9416 /* Address manipulation. */
34e8f22d
RE
9417 set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
9418
34e8f22d
RE
9419 /* Advance PC across function entry code. */
9420 set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
9421
c9cf6e20
MG
9422 /* Detect whether PC is at a point where the stack has been destroyed. */
9423 set_gdbarch_stack_frame_destroyed_p (gdbarch, arm_stack_frame_destroyed_p);
4024ca99 9424
190dce09
UW
9425 /* Skip trampolines. */
9426 set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
9427
34e8f22d
RE
9428 /* The stack grows downward. */
9429 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
9430
9431 /* Breakpoint manipulation. */
04180708
YQ
9432 set_gdbarch_breakpoint_kind_from_pc (gdbarch, arm_breakpoint_kind_from_pc);
9433 set_gdbarch_sw_breakpoint_from_kind (gdbarch, arm_sw_breakpoint_from_kind);
833b7ab5
YQ
9434 set_gdbarch_breakpoint_kind_from_current_state (gdbarch,
9435 arm_breakpoint_kind_from_current_state);
34e8f22d
RE
9436
9437 /* Information about registers, etc. */
34e8f22d
RE
9438 set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
9439 set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
ff6f572f 9440 set_gdbarch_num_regs (gdbarch, ARM_NUM_REGS);
7a5ea0d4 9441 set_gdbarch_register_type (gdbarch, arm_register_type);
54483882 9442 set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);
34e8f22d 9443
ff6f572f
DJ
9444 /* This "info float" is FPA-specific. Use the generic version if we
9445 do not have FPA. */
9446 if (gdbarch_tdep (gdbarch)->have_fpa_registers)
9447 set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
9448
26216b98 9449 /* Internal <-> external register number maps. */
ff6f572f 9450 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
26216b98
AC
9451 set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
9452
34e8f22d
RE
9453 set_gdbarch_register_name (gdbarch, arm_register_name);
9454
9455 /* Returning results. */
2af48f68 9456 set_gdbarch_return_value (gdbarch, arm_return_value);
34e8f22d 9457
03d48a7d
RE
9458 /* Disassembly. */
9459 set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
9460
34e8f22d
RE
9461 /* Minsymbol frobbing. */
9462 set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
9463 set_gdbarch_coff_make_msymbol_special (gdbarch,
9464 arm_coff_make_msymbol_special);
60c5725c 9465 set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
34e8f22d 9466
f9d67f43
DJ
9467 /* Thumb-2 IT block support. */
9468 set_gdbarch_adjust_breakpoint_address (gdbarch,
9469 arm_adjust_breakpoint_address);
9470
0d5de010
DJ
9471 /* Virtual tables. */
9472 set_gdbarch_vbit_in_delta (gdbarch, 1);
9473
97e03143 9474 /* Hook in the ABI-specific overrides, if they have been registered. */
4be87837 9475 gdbarch_init_osabi (info, gdbarch);
97e03143 9476
b39cc962
DJ
9477 dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
9478
eb5492fa 9479 /* Add some default predicates. */
2ae28aa9
YQ
9480 if (is_m)
9481 frame_unwind_append_unwinder (gdbarch, &arm_m_exception_unwind);
a262aec2
DJ
9482 frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
9483 dwarf2_append_unwinders (gdbarch);
0e9e9abd 9484 frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
779aa56f 9485 frame_unwind_append_unwinder (gdbarch, &arm_epilogue_frame_unwind);
a262aec2 9486 frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
eb5492fa 9487
97e03143
RE
9488 /* Now we have tuned the configuration, set a few final things,
9489 based on what the OS ABI has told us. */
9490
b8926edc
DJ
9491 /* If the ABI is not otherwise marked, assume the old GNU APCS. EABI
9492 binaries are always marked. */
9493 if (tdep->arm_abi == ARM_ABI_AUTO)
9494 tdep->arm_abi = ARM_ABI_APCS;
9495
e3039479
UW
9496 /* Watchpoints are not steppable. */
9497 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
9498
b8926edc
DJ
9499 /* We used to default to FPA for generic ARM, but almost nobody
9500 uses that now, and we now provide a way for the user to force
9501 the model. So default to the most useful variant. */
9502 if (tdep->fp_model == ARM_FLOAT_AUTO)
9503 tdep->fp_model = ARM_FLOAT_SOFT_FPA;
9504
9df628e0
RE
9505 if (tdep->jb_pc >= 0)
9506 set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
9507
08216dd7 9508 /* Floating point sizes and format. */
8da61cc4 9509 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
b8926edc 9510 if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
08216dd7 9511 {
8da61cc4
DJ
9512 set_gdbarch_double_format
9513 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
9514 set_gdbarch_long_double_format
9515 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
9516 }
9517 else
9518 {
9519 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
9520 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
08216dd7
RE
9521 }
9522
58d6951d
DJ
9523 if (have_vfp_pseudos)
9524 {
9525 /* NOTE: These are the only pseudo registers used by
9526 the ARM target at the moment. If more are added, a
9527 little more care in numbering will be needed. */
9528
9529 int num_pseudos = 32;
9530 if (have_neon_pseudos)
9531 num_pseudos += 16;
9532 set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
9533 set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
9534 set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
9535 }
9536
123dc839 9537 if (tdesc_data)
58d6951d
DJ
9538 {
9539 set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
9540
9779414d 9541 tdesc_use_registers (gdbarch, tdesc, tdesc_data);
58d6951d
DJ
9542
9543 /* Override tdesc_register_type to adjust the types of VFP
9544 registers for NEON. */
9545 set_gdbarch_register_type (gdbarch, arm_register_type);
9546 }
123dc839
DJ
9547
9548 /* Add standard register aliases. We add aliases even for those
85102364 9549 names which are used by the current architecture - it's simpler,
123dc839
DJ
9550 and does no harm, since nothing ever lists user registers. */
9551 for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
9552 user_reg_add (gdbarch, arm_register_aliases[i].name,
9553 value_of_arm_user_reg, &arm_register_aliases[i].regnum);
9554
65b48a81
PB
9555 set_gdbarch_disassembler_options (gdbarch, &arm_disassembler_options);
9556 set_gdbarch_valid_disassembler_options (gdbarch, disassembler_options_arm ());
9557
3426ae57
AH
9558 set_gdbarch_gnu_triplet_regexp (gdbarch, arm_gnu_triplet_regexp);
9559
39bbf761
RE
9560 return gdbarch;
9561}
9562
97e03143 9563static void
2af46ca0 9564arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
97e03143 9565{
2af46ca0 9566 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
97e03143
RE
9567
9568 if (tdep == NULL)
9569 return;
9570
aeefc73c
CB
9571 fprintf_unfiltered (file, _("arm_dump_tdep: fp_model = %i\n"),
9572 (int) tdep->fp_model);
9573 fprintf_unfiltered (file, _("arm_dump_tdep: have_fpa_registers = %i\n"),
9574 (int) tdep->have_fpa_registers);
9575 fprintf_unfiltered (file, _("arm_dump_tdep: have_wmmx_registers = %i\n"),
9576 (int) tdep->have_wmmx_registers);
9577 fprintf_unfiltered (file, _("arm_dump_tdep: vfp_register_count = %i\n"),
9578 (int) tdep->vfp_register_count);
9579 fprintf_unfiltered (file, _("arm_dump_tdep: have_vfp_pseudos = %i\n"),
9580 (int) tdep->have_vfp_pseudos);
9581 fprintf_unfiltered (file, _("arm_dump_tdep: have_neon_pseudos = %i\n"),
9582 (int) tdep->have_neon_pseudos);
9583 fprintf_unfiltered (file, _("arm_dump_tdep: have_neon = %i\n"),
9584 (int) tdep->have_neon);
754e1564 9585 fprintf_unfiltered (file, _("arm_dump_tdep: Lowest pc = 0x%lx\n"),
97e03143
RE
9586 (unsigned long) tdep->lowest_pc);
9587}
9588
0d4c07af 9589#if GDB_SELF_TEST
b121eeb9
YQ
9590namespace selftests
9591{
9592static void arm_record_test (void);
9593}
0d4c07af 9594#endif
b121eeb9 9595
6c265988 9596void _initialize_arm_tdep ();
c906108c 9597void
6c265988 9598_initialize_arm_tdep ()
c906108c 9599{
bc90b915 9600 long length;
65b48a81 9601 int i, j;
edefbb7c
AC
9602 char regdesc[1024], *rdptr = regdesc;
9603 size_t rest = sizeof (regdesc);
085dd6e6 9604
42cf1509 9605 gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
97e03143 9606
0e9e9abd 9607 /* Add ourselves to objfile event chain. */
76727919 9608 gdb::observers::new_objfile.attach (arm_exidx_new_objfile);
0e9e9abd 9609
70f80edf
JT
9610 /* Register an ELF OS ABI sniffer for ARM binaries. */
9611 gdbarch_register_osabi_sniffer (bfd_arch_arm,
9612 bfd_target_elf_flavour,
9613 arm_elf_osabi_sniffer);
9614
afd7eef0 9615 /* Add root prefix command for all "set arm"/"show arm" commands. */
0743fc83
TT
9616 add_basic_prefix_cmd ("arm", no_class,
9617 _("Various ARM-specific commands."),
9618 &setarmcmdlist, "set arm ", 0, &setlist);
afd7eef0 9619
0743fc83
TT
9620 add_show_prefix_cmd ("arm", no_class,
9621 _("Various ARM-specific commands."),
9622 &showarmcmdlist, "show arm ", 0, &showlist);
bc90b915 9623
c5aa993b 9624
65b48a81 9625 arm_disassembler_options = xstrdup ("reg-names-std");
471b9d15
MR
9626 const disasm_options_t *disasm_options
9627 = &disassembler_options_arm ()->options;
65b48a81
PB
9628 int num_disassembly_styles = 0;
9629 for (i = 0; disasm_options->name[i] != NULL; i++)
9630 if (CONST_STRNEQ (disasm_options->name[i], "reg-names-"))
9631 num_disassembly_styles++;
9632
9633 /* Initialize the array that will be passed to add_setshow_enum_cmd(). */
8d749320 9634 valid_disassembly_styles = XNEWVEC (const char *,
65b48a81
PB
9635 num_disassembly_styles + 1);
9636 for (i = j = 0; disasm_options->name[i] != NULL; i++)
9637 if (CONST_STRNEQ (disasm_options->name[i], "reg-names-"))
9638 {
9639 size_t offset = strlen ("reg-names-");
9640 const char *style = disasm_options->name[i];
9641 valid_disassembly_styles[j++] = &style[offset];
9642 length = snprintf (rdptr, rest, "%s - %s\n", &style[offset],
9643 disasm_options->description[i]);
9644 rdptr += length;
9645 rest -= length;
9646 }
94c30b78 9647 /* Mark the end of valid options. */
65b48a81 9648 valid_disassembly_styles[num_disassembly_styles] = NULL;
c906108c 9649
edefbb7c 9650 /* Create the help text. */
d7e74731
PA
9651 std::string helptext = string_printf ("%s%s%s",
9652 _("The valid values are:\n"),
9653 regdesc,
9654 _("The default is \"std\"."));
ed9a39eb 9655
edefbb7c
AC
9656 add_setshow_enum_cmd("disassembler", no_class,
9657 valid_disassembly_styles, &disassembly_style,
9658 _("Set the disassembly style."),
9659 _("Show the disassembly style."),
09b0e4b0 9660 helptext.c_str (),
2c5b56ce 9661 set_disassembly_style_sfunc,
65b48a81 9662 show_disassembly_style_sfunc,
7376b4c2 9663 &setarmcmdlist, &showarmcmdlist);
edefbb7c
AC
9664
9665 add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
9666 _("Set usage of ARM 32-bit mode."),
9667 _("Show usage of ARM 32-bit mode."),
9668 _("When off, a 26-bit PC will be used."),
2c5b56ce 9669 NULL,
0963b4bd
MS
9670 NULL, /* FIXME: i18n: Usage of ARM 32-bit
9671 mode is %s. */
26304000 9672 &setarmcmdlist, &showarmcmdlist);
c906108c 9673
fd50bc42 9674 /* Add a command to allow the user to force the FPU model. */
edefbb7c
AC
9675 add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
9676 _("Set the floating point type."),
9677 _("Show the floating point type."),
9678 _("auto - Determine the FP typefrom the OS-ABI.\n\
9679softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
9680fpa - FPA co-processor (GCC compiled).\n\
9681softvfp - Software FP with pure-endian doubles.\n\
9682vfp - VFP co-processor."),
edefbb7c 9683 set_fp_model_sfunc, show_fp_model,
7376b4c2 9684 &setarmcmdlist, &showarmcmdlist);
fd50bc42 9685
28e97307
DJ
9686 /* Add a command to allow the user to force the ABI. */
9687 add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
9688 _("Set the ABI."),
9689 _("Show the ABI."),
9690 NULL, arm_set_abi, arm_show_abi,
9691 &setarmcmdlist, &showarmcmdlist);
9692
0428b8f5
DJ
9693 /* Add two commands to allow the user to force the assumed
9694 execution mode. */
9695 add_setshow_enum_cmd ("fallback-mode", class_support,
9696 arm_mode_strings, &arm_fallback_mode_string,
9697 _("Set the mode assumed when symbols are unavailable."),
9698 _("Show the mode assumed when symbols are unavailable."),
9699 NULL, NULL, arm_show_fallback_mode,
9700 &setarmcmdlist, &showarmcmdlist);
9701 add_setshow_enum_cmd ("force-mode", class_support,
9702 arm_mode_strings, &arm_force_mode_string,
9703 _("Set the mode assumed even when symbols are available."),
9704 _("Show the mode assumed even when symbols are available."),
9705 NULL, NULL, arm_show_force_mode,
9706 &setarmcmdlist, &showarmcmdlist);
9707
6529d2dd 9708 /* Debugging flag. */
edefbb7c
AC
9709 add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
9710 _("Set ARM debugging."),
9711 _("Show ARM debugging."),
9712 _("When on, arm-specific debugging is enabled."),
2c5b56ce 9713 NULL,
7915a72c 9714 NULL, /* FIXME: i18n: "ARM debugging is %s. */
26304000 9715 &setdebuglist, &showdebuglist);
b121eeb9
YQ
9716
9717#if GDB_SELF_TEST
1526853e 9718 selftests::register_test ("arm-record", selftests::arm_record_test);
b121eeb9
YQ
9719#endif
9720
c906108c 9721}
72508ac0
PO
9722
9723/* ARM-reversible process record data structures. */
9724
9725#define ARM_INSN_SIZE_BYTES 4
9726#define THUMB_INSN_SIZE_BYTES 2
9727#define THUMB2_INSN_SIZE_BYTES 4
9728
9729
71e396f9
LM
9730/* Position of the bit within a 32-bit ARM instruction
9731 that defines whether the instruction is a load or store. */
72508ac0
PO
9732#define INSN_S_L_BIT_NUM 20
9733
9734#define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
9735 do \
9736 { \
9737 unsigned int reg_len = LENGTH; \
9738 if (reg_len) \
9739 { \
9740 REGS = XNEWVEC (uint32_t, reg_len); \
9741 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
9742 } \
9743 } \
9744 while (0)
9745
9746#define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
9747 do \
9748 { \
9749 unsigned int mem_len = LENGTH; \
9750 if (mem_len) \
9751 { \
9752 MEMS = XNEWVEC (struct arm_mem_r, mem_len); \
9753 memcpy(&MEMS->len, &RECORD_BUF[0], \
9754 sizeof(struct arm_mem_r) * LENGTH); \
9755 } \
9756 } \
9757 while (0)
9758
9759/* Checks whether insn is already recorded or yet to be decoded. (boolean expression). */
9760#define INSN_RECORDED(ARM_RECORD) \
9761 (0 != (ARM_RECORD)->reg_rec_count || 0 != (ARM_RECORD)->mem_rec_count)
9762
9763/* ARM memory record structure. */
9764struct arm_mem_r
9765{
9766 uint32_t len; /* Record length. */
bfbbec00 9767 uint32_t addr; /* Memory address. */
72508ac0
PO
9768};
9769
9770/* ARM instruction record contains opcode of current insn
9771 and execution state (before entry to decode_insn()),
9772 contains list of to-be-modified registers and
9773 memory blocks (on return from decode_insn()). */
9774
9775typedef struct insn_decode_record_t
9776{
9777 struct gdbarch *gdbarch;
9778 struct regcache *regcache;
9779 CORE_ADDR this_addr; /* Address of the insn being decoded. */
9780 uint32_t arm_insn; /* Should accommodate thumb. */
9781 uint32_t cond; /* Condition code. */
9782 uint32_t opcode; /* Insn opcode. */
9783 uint32_t decode; /* Insn decode bits. */
9784 uint32_t mem_rec_count; /* No of mem records. */
9785 uint32_t reg_rec_count; /* No of reg records. */
9786 uint32_t *arm_regs; /* Registers to be saved for this record. */
9787 struct arm_mem_r *arm_mems; /* Memory to be saved for this record. */
9788} insn_decode_record;
9789
9790
9791/* Checks ARM SBZ and SBO mandatory fields. */
9792
9793static int
9794sbo_sbz (uint32_t insn, uint32_t bit_num, uint32_t len, uint32_t sbo)
9795{
9796 uint32_t ones = bits (insn, bit_num - 1, (bit_num -1) + (len - 1));
9797
9798 if (!len)
9799 return 1;
9800
9801 if (!sbo)
9802 ones = ~ones;
9803
9804 while (ones)
9805 {
9806 if (!(ones & sbo))
9807 {
9808 return 0;
9809 }
9810 ones = ones >> 1;
9811 }
9812 return 1;
9813}
9814
c6ec2b30
OJ
9815enum arm_record_result
9816{
9817 ARM_RECORD_SUCCESS = 0,
9818 ARM_RECORD_FAILURE = 1
9819};
9820
72508ac0
PO
9821typedef enum
9822{
9823 ARM_RECORD_STRH=1,
9824 ARM_RECORD_STRD
9825} arm_record_strx_t;
9826
9827typedef enum
9828{
9829 ARM_RECORD=1,
9830 THUMB_RECORD,
9831 THUMB2_RECORD
9832} record_type_t;
9833
9834
9835static int
9836arm_record_strx (insn_decode_record *arm_insn_r, uint32_t *record_buf,
9837 uint32_t *record_buf_mem, arm_record_strx_t str_type)
9838{
9839
9840 struct regcache *reg_cache = arm_insn_r->regcache;
9841 ULONGEST u_regval[2]= {0};
9842
9843 uint32_t reg_src1 = 0, reg_src2 = 0;
9844 uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
72508ac0
PO
9845
9846 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
9847 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
72508ac0
PO
9848
9849 if (14 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
9850 {
9851 /* 1) Handle misc store, immediate offset. */
9852 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
9853 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
9854 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
9855 regcache_raw_read_unsigned (reg_cache, reg_src1,
9856 &u_regval[0]);
9857 if (ARM_PC_REGNUM == reg_src1)
9858 {
9859 /* If R15 was used as Rn, hence current PC+8. */
9860 u_regval[0] = u_regval[0] + 8;
9861 }
9862 offset_8 = (immed_high << 4) | immed_low;
9863 /* Calculate target store address. */
9864 if (14 == arm_insn_r->opcode)
9865 {
9866 tgt_mem_addr = u_regval[0] + offset_8;
9867 }
9868 else
9869 {
9870 tgt_mem_addr = u_regval[0] - offset_8;
9871 }
9872 if (ARM_RECORD_STRH == str_type)
9873 {
9874 record_buf_mem[0] = 2;
9875 record_buf_mem[1] = tgt_mem_addr;
9876 arm_insn_r->mem_rec_count = 1;
9877 }
9878 else if (ARM_RECORD_STRD == str_type)
9879 {
9880 record_buf_mem[0] = 4;
9881 record_buf_mem[1] = tgt_mem_addr;
9882 record_buf_mem[2] = 4;
9883 record_buf_mem[3] = tgt_mem_addr + 4;
9884 arm_insn_r->mem_rec_count = 2;
9885 }
9886 }
9887 else if (12 == arm_insn_r->opcode || 8 == arm_insn_r->opcode)
9888 {
9889 /* 2) Store, register offset. */
9890 /* Get Rm. */
9891 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
9892 /* Get Rn. */
9893 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
9894 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
9895 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
9896 if (15 == reg_src2)
9897 {
9898 /* If R15 was used as Rn, hence current PC+8. */
9899 u_regval[0] = u_regval[0] + 8;
9900 }
9901 /* Calculate target store address, Rn +/- Rm, register offset. */
9902 if (12 == arm_insn_r->opcode)
9903 {
9904 tgt_mem_addr = u_regval[0] + u_regval[1];
9905 }
9906 else
9907 {
9908 tgt_mem_addr = u_regval[1] - u_regval[0];
9909 }
9910 if (ARM_RECORD_STRH == str_type)
9911 {
9912 record_buf_mem[0] = 2;
9913 record_buf_mem[1] = tgt_mem_addr;
9914 arm_insn_r->mem_rec_count = 1;
9915 }
9916 else if (ARM_RECORD_STRD == str_type)
9917 {
9918 record_buf_mem[0] = 4;
9919 record_buf_mem[1] = tgt_mem_addr;
9920 record_buf_mem[2] = 4;
9921 record_buf_mem[3] = tgt_mem_addr + 4;
9922 arm_insn_r->mem_rec_count = 2;
9923 }
9924 }
9925 else if (11 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
9926 || 2 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
9927 {
9928 /* 3) Store, immediate pre-indexed. */
9929 /* 5) Store, immediate post-indexed. */
9930 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
9931 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
9932 offset_8 = (immed_high << 4) | immed_low;
9933 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
9934 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
9935 /* Calculate target store address, Rn +/- Rm, register offset. */
9936 if (15 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
9937 {
9938 tgt_mem_addr = u_regval[0] + offset_8;
9939 }
9940 else
9941 {
9942 tgt_mem_addr = u_regval[0] - offset_8;
9943 }
9944 if (ARM_RECORD_STRH == str_type)
9945 {
9946 record_buf_mem[0] = 2;
9947 record_buf_mem[1] = tgt_mem_addr;
9948 arm_insn_r->mem_rec_count = 1;
9949 }
9950 else if (ARM_RECORD_STRD == str_type)
9951 {
9952 record_buf_mem[0] = 4;
9953 record_buf_mem[1] = tgt_mem_addr;
9954 record_buf_mem[2] = 4;
9955 record_buf_mem[3] = tgt_mem_addr + 4;
9956 arm_insn_r->mem_rec_count = 2;
9957 }
9958 /* Record Rn also as it changes. */
9959 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
9960 arm_insn_r->reg_rec_count = 1;
9961 }
9962 else if (9 == arm_insn_r->opcode || 13 == arm_insn_r->opcode
9963 || 0 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
9964 {
9965 /* 4) Store, register pre-indexed. */
9966 /* 6) Store, register post -indexed. */
9967 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
9968 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
9969 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
9970 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
9971 /* Calculate target store address, Rn +/- Rm, register offset. */
9972 if (13 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
9973 {
9974 tgt_mem_addr = u_regval[0] + u_regval[1];
9975 }
9976 else
9977 {
9978 tgt_mem_addr = u_regval[1] - u_regval[0];
9979 }
9980 if (ARM_RECORD_STRH == str_type)
9981 {
9982 record_buf_mem[0] = 2;
9983 record_buf_mem[1] = tgt_mem_addr;
9984 arm_insn_r->mem_rec_count = 1;
9985 }
9986 else if (ARM_RECORD_STRD == str_type)
9987 {
9988 record_buf_mem[0] = 4;
9989 record_buf_mem[1] = tgt_mem_addr;
9990 record_buf_mem[2] = 4;
9991 record_buf_mem[3] = tgt_mem_addr + 4;
9992 arm_insn_r->mem_rec_count = 2;
9993 }
9994 /* Record Rn also as it changes. */
9995 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
9996 arm_insn_r->reg_rec_count = 1;
9997 }
9998 return 0;
9999}
10000
10001/* Handling ARM extension space insns. */
10002
10003static int
10004arm_record_extension_space (insn_decode_record *arm_insn_r)
10005{
df95a9cf 10006 int ret = 0; /* Return value: -1:record failure ; 0:success */
72508ac0
PO
10007 uint32_t opcode1 = 0, opcode2 = 0, insn_op1 = 0;
10008 uint32_t record_buf[8], record_buf_mem[8];
10009 uint32_t reg_src1 = 0;
72508ac0
PO
10010 struct regcache *reg_cache = arm_insn_r->regcache;
10011 ULONGEST u_regval = 0;
10012
10013 gdb_assert (!INSN_RECORDED(arm_insn_r));
10014 /* Handle unconditional insn extension space. */
10015
10016 opcode1 = bits (arm_insn_r->arm_insn, 20, 27);
10017 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
10018 if (arm_insn_r->cond)
10019 {
10020 /* PLD has no affect on architectural state, it just affects
10021 the caches. */
10022 if (5 == ((opcode1 & 0xE0) >> 5))
10023 {
10024 /* BLX(1) */
10025 record_buf[0] = ARM_PS_REGNUM;
10026 record_buf[1] = ARM_LR_REGNUM;
10027 arm_insn_r->reg_rec_count = 2;
10028 }
10029 /* STC2, LDC2, MCR2, MRC2, CDP2: <TBD>, co-processor insn. */
10030 }
10031
10032
10033 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
10034 if (3 == opcode1 && bit (arm_insn_r->arm_insn, 4))
10035 {
10036 ret = -1;
10037 /* Undefined instruction on ARM V5; need to handle if later
10038 versions define it. */
10039 }
10040
10041 opcode1 = bits (arm_insn_r->arm_insn, 24, 27);
10042 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
10043 insn_op1 = bits (arm_insn_r->arm_insn, 20, 23);
10044
10045 /* Handle arithmetic insn extension space. */
10046 if (!opcode1 && 9 == opcode2 && 1 != arm_insn_r->cond
10047 && !INSN_RECORDED(arm_insn_r))
10048 {
10049 /* Handle MLA(S) and MUL(S). */
b020ff80 10050 if (in_inclusive_range (insn_op1, 0U, 3U))
72508ac0
PO
10051 {
10052 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10053 record_buf[1] = ARM_PS_REGNUM;
10054 arm_insn_r->reg_rec_count = 2;
10055 }
b020ff80 10056 else if (in_inclusive_range (insn_op1, 4U, 15U))
72508ac0
PO
10057 {
10058 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
10059 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
10060 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
10061 record_buf[2] = ARM_PS_REGNUM;
10062 arm_insn_r->reg_rec_count = 3;
10063 }
10064 }
10065
10066 opcode1 = bits (arm_insn_r->arm_insn, 26, 27);
10067 opcode2 = bits (arm_insn_r->arm_insn, 23, 24);
10068 insn_op1 = bits (arm_insn_r->arm_insn, 21, 22);
10069
10070 /* Handle control insn extension space. */
10071
10072 if (!opcode1 && 2 == opcode2 && !bit (arm_insn_r->arm_insn, 20)
10073 && 1 != arm_insn_r->cond && !INSN_RECORDED(arm_insn_r))
10074 {
10075 if (!bit (arm_insn_r->arm_insn,25))
10076 {
10077 if (!bits (arm_insn_r->arm_insn, 4, 7))
10078 {
10079 if ((0 == insn_op1) || (2 == insn_op1))
10080 {
10081 /* MRS. */
10082 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10083 arm_insn_r->reg_rec_count = 1;
10084 }
10085 else if (1 == insn_op1)
10086 {
10087 /* CSPR is going to be changed. */
10088 record_buf[0] = ARM_PS_REGNUM;
10089 arm_insn_r->reg_rec_count = 1;
10090 }
10091 else if (3 == insn_op1)
10092 {
10093 /* SPSR is going to be changed. */
10094 /* We need to get SPSR value, which is yet to be done. */
72508ac0
PO
10095 return -1;
10096 }
10097 }
10098 else if (1 == bits (arm_insn_r->arm_insn, 4, 7))
10099 {
10100 if (1 == insn_op1)
10101 {
10102 /* BX. */
10103 record_buf[0] = ARM_PS_REGNUM;
10104 arm_insn_r->reg_rec_count = 1;
10105 }
10106 else if (3 == insn_op1)
10107 {
10108 /* CLZ. */
10109 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10110 arm_insn_r->reg_rec_count = 1;
10111 }
10112 }
10113 else if (3 == bits (arm_insn_r->arm_insn, 4, 7))
10114 {
10115 /* BLX. */
10116 record_buf[0] = ARM_PS_REGNUM;
10117 record_buf[1] = ARM_LR_REGNUM;
10118 arm_insn_r->reg_rec_count = 2;
10119 }
10120 else if (5 == bits (arm_insn_r->arm_insn, 4, 7))
10121 {
10122 /* QADD, QSUB, QDADD, QDSUB */
10123 record_buf[0] = ARM_PS_REGNUM;
10124 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
10125 arm_insn_r->reg_rec_count = 2;
10126 }
10127 else if (7 == bits (arm_insn_r->arm_insn, 4, 7))
10128 {
10129 /* BKPT. */
10130 record_buf[0] = ARM_PS_REGNUM;
10131 record_buf[1] = ARM_LR_REGNUM;
10132 arm_insn_r->reg_rec_count = 2;
10133
10134 /* Save SPSR also;how? */
72508ac0
PO
10135 return -1;
10136 }
10137 else if(8 == bits (arm_insn_r->arm_insn, 4, 7)
10138 || 10 == bits (arm_insn_r->arm_insn, 4, 7)
10139 || 12 == bits (arm_insn_r->arm_insn, 4, 7)
10140 || 14 == bits (arm_insn_r->arm_insn, 4, 7)
10141 )
10142 {
10143 if (0 == insn_op1 || 1 == insn_op1)
10144 {
10145 /* SMLA<x><y>, SMLAW<y>, SMULW<y>. */
10146 /* We dont do optimization for SMULW<y> where we
10147 need only Rd. */
10148 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10149 record_buf[1] = ARM_PS_REGNUM;
10150 arm_insn_r->reg_rec_count = 2;
10151 }
10152 else if (2 == insn_op1)
10153 {
10154 /* SMLAL<x><y>. */
10155 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10156 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
10157 arm_insn_r->reg_rec_count = 2;
10158 }
10159 else if (3 == insn_op1)
10160 {
10161 /* SMUL<x><y>. */
10162 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10163 arm_insn_r->reg_rec_count = 1;
10164 }
10165 }
10166 }
10167 else
10168 {
10169 /* MSR : immediate form. */
10170 if (1 == insn_op1)
10171 {
10172 /* CSPR is going to be changed. */
10173 record_buf[0] = ARM_PS_REGNUM;
10174 arm_insn_r->reg_rec_count = 1;
10175 }
10176 else if (3 == insn_op1)
10177 {
10178 /* SPSR is going to be changed. */
10179 /* we need to get SPSR value, which is yet to be done */
72508ac0
PO
10180 return -1;
10181 }
10182 }
10183 }
10184
10185 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
10186 opcode2 = bits (arm_insn_r->arm_insn, 20, 24);
10187 insn_op1 = bits (arm_insn_r->arm_insn, 5, 6);
10188
10189 /* Handle load/store insn extension space. */
10190
10191 if (!opcode1 && bit (arm_insn_r->arm_insn, 7)
10192 && bit (arm_insn_r->arm_insn, 4) && 1 != arm_insn_r->cond
10193 && !INSN_RECORDED(arm_insn_r))
10194 {
10195 /* SWP/SWPB. */
10196 if (0 == insn_op1)
10197 {
10198 /* These insn, changes register and memory as well. */
10199 /* SWP or SWPB insn. */
10200 /* Get memory address given by Rn. */
10201 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10202 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
10203 /* SWP insn ?, swaps word. */
10204 if (8 == arm_insn_r->opcode)
10205 {
10206 record_buf_mem[0] = 4;
10207 }
10208 else
10209 {
10210 /* SWPB insn, swaps only byte. */
10211 record_buf_mem[0] = 1;
10212 }
10213 record_buf_mem[1] = u_regval;
10214 arm_insn_r->mem_rec_count = 1;
10215 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10216 arm_insn_r->reg_rec_count = 1;
10217 }
10218 else if (1 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
10219 {
10220 /* STRH. */
10221 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
10222 ARM_RECORD_STRH);
10223 }
10224 else if (2 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
10225 {
10226 /* LDRD. */
10227 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10228 record_buf[1] = record_buf[0] + 1;
10229 arm_insn_r->reg_rec_count = 2;
10230 }
10231 else if (3 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
10232 {
10233 /* STRD. */
10234 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
10235 ARM_RECORD_STRD);
10236 }
10237 else if (bit (arm_insn_r->arm_insn, 20) && insn_op1 <= 3)
10238 {
10239 /* LDRH, LDRSB, LDRSH. */
10240 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10241 arm_insn_r->reg_rec_count = 1;
10242 }
10243
10244 }
10245
10246 opcode1 = bits (arm_insn_r->arm_insn, 23, 27);
10247 if (24 == opcode1 && bit (arm_insn_r->arm_insn, 21)
10248 && !INSN_RECORDED(arm_insn_r))
10249 {
10250 ret = -1;
10251 /* Handle coprocessor insn extension space. */
10252 }
10253
10254 /* To be done for ARMv5 and later; as of now we return -1. */
10255 if (-1 == ret)
ca92db2d 10256 return ret;
72508ac0
PO
10257
10258 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10259 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10260
10261 return ret;
10262}
10263
10264/* Handling opcode 000 insns. */
10265
10266static int
10267arm_record_data_proc_misc_ld_str (insn_decode_record *arm_insn_r)
10268{
10269 struct regcache *reg_cache = arm_insn_r->regcache;
10270 uint32_t record_buf[8], record_buf_mem[8];
10271 ULONGEST u_regval[2] = {0};
10272
8d49165d 10273 uint32_t reg_src1 = 0;
72508ac0
PO
10274 uint32_t opcode1 = 0;
10275
10276 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10277 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10278 opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
10279
2d9e6acb 10280 if (!((opcode1 & 0x19) == 0x10))
72508ac0 10281 {
2d9e6acb
YQ
10282 /* Data-processing (register) and Data-processing (register-shifted
10283 register */
10284 /* Out of 11 shifter operands mode, all the insn modifies destination
10285 register, which is specified by 13-16 decode. */
10286 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10287 record_buf[1] = ARM_PS_REGNUM;
10288 arm_insn_r->reg_rec_count = 2;
72508ac0 10289 }
2d9e6acb 10290 else if ((arm_insn_r->decode < 8) && ((opcode1 & 0x19) == 0x10))
72508ac0 10291 {
2d9e6acb
YQ
10292 /* Miscellaneous instructions */
10293
10294 if (3 == arm_insn_r->decode && 0x12 == opcode1
10295 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
10296 {
10297 /* Handle BLX, branch and link/exchange. */
10298 if (9 == arm_insn_r->opcode)
10299 {
10300 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm,
10301 and R14 stores the return address. */
10302 record_buf[0] = ARM_PS_REGNUM;
10303 record_buf[1] = ARM_LR_REGNUM;
10304 arm_insn_r->reg_rec_count = 2;
10305 }
10306 }
10307 else if (7 == arm_insn_r->decode && 0x12 == opcode1)
10308 {
10309 /* Handle enhanced software breakpoint insn, BKPT. */
10310 /* CPSR is changed to be executed in ARM state, disabling normal
10311 interrupts, entering abort mode. */
10312 /* According to high vector configuration PC is set. */
10313 /* user hit breakpoint and type reverse, in
10314 that case, we need to go back with previous CPSR and
10315 Program Counter. */
10316 record_buf[0] = ARM_PS_REGNUM;
10317 record_buf[1] = ARM_LR_REGNUM;
10318 arm_insn_r->reg_rec_count = 2;
10319
10320 /* Save SPSR also; how? */
10321 return -1;
10322 }
10323 else if (1 == arm_insn_r->decode && 0x12 == opcode1
10324 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
10325 {
10326 /* Handle BX, branch and link/exchange. */
10327 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm. */
10328 record_buf[0] = ARM_PS_REGNUM;
10329 arm_insn_r->reg_rec_count = 1;
10330 }
10331 else if (1 == arm_insn_r->decode && 0x16 == opcode1
10332 && sbo_sbz (arm_insn_r->arm_insn, 9, 4, 1)
10333 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1))
10334 {
10335 /* Count leading zeros: CLZ. */
10336 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10337 arm_insn_r->reg_rec_count = 1;
10338 }
10339 else if (!bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
10340 && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
10341 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1)
10342 && sbo_sbz (arm_insn_r->arm_insn, 1, 12, 0))
10343 {
10344 /* Handle MRS insn. */
10345 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10346 arm_insn_r->reg_rec_count = 1;
10347 }
72508ac0 10348 }
2d9e6acb 10349 else if (9 == arm_insn_r->decode && opcode1 < 0x10)
72508ac0 10350 {
2d9e6acb
YQ
10351 /* Multiply and multiply-accumulate */
10352
10353 /* Handle multiply instructions. */
10354 /* MLA, MUL, SMLAL, SMULL, UMLAL, UMULL. */
10355 if (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)
10356 {
10357 /* Handle MLA and MUL. */
10358 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
10359 record_buf[1] = ARM_PS_REGNUM;
10360 arm_insn_r->reg_rec_count = 2;
10361 }
10362 else if (4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
10363 {
10364 /* Handle SMLAL, SMULL, UMLAL, UMULL. */
10365 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
10366 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
10367 record_buf[2] = ARM_PS_REGNUM;
10368 arm_insn_r->reg_rec_count = 3;
10369 }
10370 }
10371 else if (9 == arm_insn_r->decode && opcode1 > 0x10)
10372 {
10373 /* Synchronization primitives */
10374
72508ac0
PO
10375 /* Handling SWP, SWPB. */
10376 /* These insn, changes register and memory as well. */
10377 /* SWP or SWPB insn. */
10378
10379 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10380 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10381 /* SWP insn ?, swaps word. */
10382 if (8 == arm_insn_r->opcode)
2d9e6acb
YQ
10383 {
10384 record_buf_mem[0] = 4;
10385 }
10386 else
10387 {
10388 /* SWPB insn, swaps only byte. */
10389 record_buf_mem[0] = 1;
10390 }
72508ac0
PO
10391 record_buf_mem[1] = u_regval[0];
10392 arm_insn_r->mem_rec_count = 1;
10393 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10394 arm_insn_r->reg_rec_count = 1;
10395 }
2d9e6acb
YQ
10396 else if (11 == arm_insn_r->decode || 13 == arm_insn_r->decode
10397 || 15 == arm_insn_r->decode)
72508ac0 10398 {
2d9e6acb
YQ
10399 if ((opcode1 & 0x12) == 2)
10400 {
10401 /* Extra load/store (unprivileged) */
10402 return -1;
10403 }
10404 else
10405 {
10406 /* Extra load/store */
10407 switch (bits (arm_insn_r->arm_insn, 5, 6))
10408 {
10409 case 1:
10410 if ((opcode1 & 0x05) == 0x0 || (opcode1 & 0x05) == 0x4)
10411 {
10412 /* STRH (register), STRH (immediate) */
10413 arm_record_strx (arm_insn_r, &record_buf[0],
10414 &record_buf_mem[0], ARM_RECORD_STRH);
10415 }
10416 else if ((opcode1 & 0x05) == 0x1)
10417 {
10418 /* LDRH (register) */
10419 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10420 arm_insn_r->reg_rec_count = 1;
72508ac0 10421
2d9e6acb
YQ
10422 if (bit (arm_insn_r->arm_insn, 21))
10423 {
10424 /* Write back to Rn. */
10425 record_buf[arm_insn_r->reg_rec_count++]
10426 = bits (arm_insn_r->arm_insn, 16, 19);
10427 }
10428 }
10429 else if ((opcode1 & 0x05) == 0x5)
10430 {
10431 /* LDRH (immediate), LDRH (literal) */
10432 int rn = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0 10433
2d9e6acb
YQ
10434 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10435 arm_insn_r->reg_rec_count = 1;
10436
10437 if (rn != 15)
10438 {
10439 /*LDRH (immediate) */
10440 if (bit (arm_insn_r->arm_insn, 21))
10441 {
10442 /* Write back to Rn. */
10443 record_buf[arm_insn_r->reg_rec_count++] = rn;
10444 }
10445 }
10446 }
10447 else
10448 return -1;
10449 break;
10450 case 2:
10451 if ((opcode1 & 0x05) == 0x0)
10452 {
10453 /* LDRD (register) */
10454 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10455 record_buf[1] = record_buf[0] + 1;
10456 arm_insn_r->reg_rec_count = 2;
10457
10458 if (bit (arm_insn_r->arm_insn, 21))
10459 {
10460 /* Write back to Rn. */
10461 record_buf[arm_insn_r->reg_rec_count++]
10462 = bits (arm_insn_r->arm_insn, 16, 19);
10463 }
10464 }
10465 else if ((opcode1 & 0x05) == 0x1)
10466 {
10467 /* LDRSB (register) */
10468 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10469 arm_insn_r->reg_rec_count = 1;
10470
10471 if (bit (arm_insn_r->arm_insn, 21))
10472 {
10473 /* Write back to Rn. */
10474 record_buf[arm_insn_r->reg_rec_count++]
10475 = bits (arm_insn_r->arm_insn, 16, 19);
10476 }
10477 }
10478 else if ((opcode1 & 0x05) == 0x4 || (opcode1 & 0x05) == 0x5)
10479 {
10480 /* LDRD (immediate), LDRD (literal), LDRSB (immediate),
10481 LDRSB (literal) */
10482 int rn = bits (arm_insn_r->arm_insn, 16, 19);
10483
10484 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10485 arm_insn_r->reg_rec_count = 1;
10486
10487 if (rn != 15)
10488 {
10489 /*LDRD (immediate), LDRSB (immediate) */
10490 if (bit (arm_insn_r->arm_insn, 21))
10491 {
10492 /* Write back to Rn. */
10493 record_buf[arm_insn_r->reg_rec_count++] = rn;
10494 }
10495 }
10496 }
10497 else
10498 return -1;
10499 break;
10500 case 3:
10501 if ((opcode1 & 0x05) == 0x0)
10502 {
10503 /* STRD (register) */
10504 arm_record_strx (arm_insn_r, &record_buf[0],
10505 &record_buf_mem[0], ARM_RECORD_STRD);
10506 }
10507 else if ((opcode1 & 0x05) == 0x1)
10508 {
10509 /* LDRSH (register) */
10510 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10511 arm_insn_r->reg_rec_count = 1;
10512
10513 if (bit (arm_insn_r->arm_insn, 21))
10514 {
10515 /* Write back to Rn. */
10516 record_buf[arm_insn_r->reg_rec_count++]
10517 = bits (arm_insn_r->arm_insn, 16, 19);
10518 }
10519 }
10520 else if ((opcode1 & 0x05) == 0x4)
10521 {
10522 /* STRD (immediate) */
10523 arm_record_strx (arm_insn_r, &record_buf[0],
10524 &record_buf_mem[0], ARM_RECORD_STRD);
10525 }
10526 else if ((opcode1 & 0x05) == 0x5)
10527 {
10528 /* LDRSH (immediate), LDRSH (literal) */
10529 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10530 arm_insn_r->reg_rec_count = 1;
10531
10532 if (bit (arm_insn_r->arm_insn, 21))
10533 {
10534 /* Write back to Rn. */
10535 record_buf[arm_insn_r->reg_rec_count++]
10536 = bits (arm_insn_r->arm_insn, 16, 19);
10537 }
10538 }
10539 else
10540 return -1;
10541 break;
10542 default:
10543 return -1;
10544 }
10545 }
72508ac0
PO
10546 }
10547 else
10548 {
10549 return -1;
10550 }
10551
10552 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10553 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10554 return 0;
10555}
10556
10557/* Handling opcode 001 insns. */
10558
10559static int
10560arm_record_data_proc_imm (insn_decode_record *arm_insn_r)
10561{
10562 uint32_t record_buf[8], record_buf_mem[8];
10563
10564 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10565 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10566
10567 if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
10568 && 2 == bits (arm_insn_r->arm_insn, 20, 21)
10569 && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
10570 )
10571 {
10572 /* Handle MSR insn. */
10573 if (9 == arm_insn_r->opcode)
10574 {
10575 /* CSPR is going to be changed. */
10576 record_buf[0] = ARM_PS_REGNUM;
10577 arm_insn_r->reg_rec_count = 1;
10578 }
10579 else
10580 {
10581 /* SPSR is going to be changed. */
10582 }
10583 }
10584 else if (arm_insn_r->opcode <= 15)
10585 {
10586 /* Normal data processing insns. */
10587 /* Out of 11 shifter operands mode, all the insn modifies destination
10588 register, which is specified by 13-16 decode. */
10589 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10590 record_buf[1] = ARM_PS_REGNUM;
10591 arm_insn_r->reg_rec_count = 2;
10592 }
10593 else
10594 {
10595 return -1;
10596 }
10597
10598 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10599 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10600 return 0;
10601}
10602
c55978a6
YQ
10603static int
10604arm_record_media (insn_decode_record *arm_insn_r)
10605{
10606 uint32_t record_buf[8];
10607
10608 switch (bits (arm_insn_r->arm_insn, 22, 24))
10609 {
10610 case 0:
10611 /* Parallel addition and subtraction, signed */
10612 case 1:
10613 /* Parallel addition and subtraction, unsigned */
10614 case 2:
10615 case 3:
10616 /* Packing, unpacking, saturation and reversal */
10617 {
10618 int rd = bits (arm_insn_r->arm_insn, 12, 15);
10619
10620 record_buf[arm_insn_r->reg_rec_count++] = rd;
10621 }
10622 break;
10623
10624 case 4:
10625 case 5:
10626 /* Signed multiplies */
10627 {
10628 int rd = bits (arm_insn_r->arm_insn, 16, 19);
10629 unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 22);
10630
10631 record_buf[arm_insn_r->reg_rec_count++] = rd;
10632 if (op1 == 0x0)
10633 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
10634 else if (op1 == 0x4)
10635 record_buf[arm_insn_r->reg_rec_count++]
10636 = bits (arm_insn_r->arm_insn, 12, 15);
10637 }
10638 break;
10639
10640 case 6:
10641 {
10642 if (bit (arm_insn_r->arm_insn, 21)
10643 && bits (arm_insn_r->arm_insn, 5, 6) == 0x2)
10644 {
10645 /* SBFX */
10646 record_buf[arm_insn_r->reg_rec_count++]
10647 = bits (arm_insn_r->arm_insn, 12, 15);
10648 }
10649 else if (bits (arm_insn_r->arm_insn, 20, 21) == 0x0
10650 && bits (arm_insn_r->arm_insn, 5, 7) == 0x0)
10651 {
10652 /* USAD8 and USADA8 */
10653 record_buf[arm_insn_r->reg_rec_count++]
10654 = bits (arm_insn_r->arm_insn, 16, 19);
10655 }
10656 }
10657 break;
10658
10659 case 7:
10660 {
10661 if (bits (arm_insn_r->arm_insn, 20, 21) == 0x3
10662 && bits (arm_insn_r->arm_insn, 5, 7) == 0x7)
10663 {
10664 /* Permanently UNDEFINED */
10665 return -1;
10666 }
10667 else
10668 {
10669 /* BFC, BFI and UBFX */
10670 record_buf[arm_insn_r->reg_rec_count++]
10671 = bits (arm_insn_r->arm_insn, 12, 15);
10672 }
10673 }
10674 break;
10675
10676 default:
10677 return -1;
10678 }
10679
10680 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10681
10682 return 0;
10683}
10684
71e396f9 10685/* Handle ARM mode instructions with opcode 010. */
72508ac0
PO
10686
10687static int
10688arm_record_ld_st_imm_offset (insn_decode_record *arm_insn_r)
10689{
10690 struct regcache *reg_cache = arm_insn_r->regcache;
10691
71e396f9
LM
10692 uint32_t reg_base , reg_dest;
10693 uint32_t offset_12, tgt_mem_addr;
72508ac0 10694 uint32_t record_buf[8], record_buf_mem[8];
71e396f9
LM
10695 unsigned char wback;
10696 ULONGEST u_regval;
72508ac0 10697
71e396f9
LM
10698 /* Calculate wback. */
10699 wback = (bit (arm_insn_r->arm_insn, 24) == 0)
10700 || (bit (arm_insn_r->arm_insn, 21) == 1);
72508ac0 10701
71e396f9
LM
10702 arm_insn_r->reg_rec_count = 0;
10703 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0
PO
10704
10705 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
10706 {
71e396f9
LM
10707 /* LDR (immediate), LDR (literal), LDRB (immediate), LDRB (literal), LDRBT
10708 and LDRT. */
10709
72508ac0 10710 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
71e396f9
LM
10711 record_buf[arm_insn_r->reg_rec_count++] = reg_dest;
10712
10713 /* The LDR instruction is capable of doing branching. If MOV LR, PC
10714 preceeds a LDR instruction having R15 as reg_base, it
10715 emulates a branch and link instruction, and hence we need to save
10716 CPSR and PC as well. */
10717 if (ARM_PC_REGNUM == reg_dest)
10718 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
10719
10720 /* If wback is true, also save the base register, which is going to be
10721 written to. */
10722 if (wback)
10723 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
10724 }
10725 else
10726 {
71e396f9
LM
10727 /* STR (immediate), STRB (immediate), STRBT and STRT. */
10728
72508ac0 10729 offset_12 = bits (arm_insn_r->arm_insn, 0, 11);
71e396f9
LM
10730 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
10731
10732 /* Handle bit U. */
72508ac0 10733 if (bit (arm_insn_r->arm_insn, 23))
71e396f9
LM
10734 {
10735 /* U == 1: Add the offset. */
10736 tgt_mem_addr = (uint32_t) u_regval + offset_12;
10737 }
72508ac0 10738 else
71e396f9
LM
10739 {
10740 /* U == 0: subtract the offset. */
10741 tgt_mem_addr = (uint32_t) u_regval - offset_12;
10742 }
10743
10744 /* Bit 22 tells us whether the store instruction writes 1 byte or 4
10745 bytes. */
10746 if (bit (arm_insn_r->arm_insn, 22))
10747 {
10748 /* STRB and STRBT: 1 byte. */
10749 record_buf_mem[0] = 1;
10750 }
10751 else
10752 {
10753 /* STR and STRT: 4 bytes. */
10754 record_buf_mem[0] = 4;
10755 }
10756
10757 /* Handle bit P. */
10758 if (bit (arm_insn_r->arm_insn, 24))
10759 record_buf_mem[1] = tgt_mem_addr;
10760 else
10761 record_buf_mem[1] = (uint32_t) u_regval;
72508ac0 10762
72508ac0
PO
10763 arm_insn_r->mem_rec_count = 1;
10764
71e396f9
LM
10765 /* If wback is true, also save the base register, which is going to be
10766 written to. */
10767 if (wback)
10768 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
10769 }
10770
10771 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10772 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10773 return 0;
10774}
10775
10776/* Handling opcode 011 insns. */
10777
10778static int
10779arm_record_ld_st_reg_offset (insn_decode_record *arm_insn_r)
10780{
10781 struct regcache *reg_cache = arm_insn_r->regcache;
10782
10783 uint32_t shift_imm = 0;
10784 uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
10785 uint32_t offset_12 = 0, tgt_mem_addr = 0;
10786 uint32_t record_buf[8], record_buf_mem[8];
10787
10788 LONGEST s_word;
10789 ULONGEST u_regval[2];
10790
c55978a6
YQ
10791 if (bit (arm_insn_r->arm_insn, 4))
10792 return arm_record_media (arm_insn_r);
10793
72508ac0
PO
10794 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10795 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10796
10797 /* Handle enhanced store insns and LDRD DSP insn,
10798 order begins according to addressing modes for store insns
10799 STRH insn. */
10800
10801 /* LDR or STR? */
10802 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
10803 {
10804 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
10805 /* LDR insn has a capability to do branching, if
85102364 10806 MOV LR, PC is preceded by LDR insn having Rn as R15
72508ac0
PO
10807 in that case, it emulates branch and link insn, and hence we
10808 need to save CSPR and PC as well. */
10809 if (15 != reg_dest)
10810 {
10811 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10812 arm_insn_r->reg_rec_count = 1;
10813 }
10814 else
10815 {
10816 record_buf[0] = reg_dest;
10817 record_buf[1] = ARM_PS_REGNUM;
10818 arm_insn_r->reg_rec_count = 2;
10819 }
10820 }
10821 else
10822 {
10823 if (! bits (arm_insn_r->arm_insn, 4, 11))
10824 {
10825 /* Store insn, register offset and register pre-indexed,
10826 register post-indexed. */
10827 /* Get Rm. */
10828 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10829 /* Get Rn. */
10830 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10831 regcache_raw_read_unsigned (reg_cache, reg_src1
10832 , &u_regval[0]);
10833 regcache_raw_read_unsigned (reg_cache, reg_src2
10834 , &u_regval[1]);
10835 if (15 == reg_src2)
10836 {
10837 /* If R15 was used as Rn, hence current PC+8. */
10838 /* Pre-indexed mode doesnt reach here ; illegal insn. */
10839 u_regval[0] = u_regval[0] + 8;
10840 }
10841 /* Calculate target store address, Rn +/- Rm, register offset. */
10842 /* U == 1. */
10843 if (bit (arm_insn_r->arm_insn, 23))
10844 {
10845 tgt_mem_addr = u_regval[0] + u_regval[1];
10846 }
10847 else
10848 {
10849 tgt_mem_addr = u_regval[1] - u_regval[0];
10850 }
10851
10852 switch (arm_insn_r->opcode)
10853 {
10854 /* STR. */
10855 case 8:
10856 case 12:
10857 /* STR. */
10858 case 9:
10859 case 13:
10860 /* STRT. */
10861 case 1:
10862 case 5:
10863 /* STR. */
10864 case 0:
10865 case 4:
10866 record_buf_mem[0] = 4;
10867 break;
10868
10869 /* STRB. */
10870 case 10:
10871 case 14:
10872 /* STRB. */
10873 case 11:
10874 case 15:
10875 /* STRBT. */
10876 case 3:
10877 case 7:
10878 /* STRB. */
10879 case 2:
10880 case 6:
10881 record_buf_mem[0] = 1;
10882 break;
10883
10884 default:
10885 gdb_assert_not_reached ("no decoding pattern found");
10886 break;
10887 }
10888 record_buf_mem[1] = tgt_mem_addr;
10889 arm_insn_r->mem_rec_count = 1;
10890
10891 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
10892 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
10893 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
10894 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
10895 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
10896 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
10897 )
10898 {
10899 /* Rn is going to be changed in pre-indexed mode and
10900 post-indexed mode as well. */
10901 record_buf[0] = reg_src2;
10902 arm_insn_r->reg_rec_count = 1;
10903 }
10904 }
10905 else
10906 {
10907 /* Store insn, scaled register offset; scaled pre-indexed. */
10908 offset_12 = bits (arm_insn_r->arm_insn, 5, 6);
10909 /* Get Rm. */
10910 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10911 /* Get Rn. */
10912 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10913 /* Get shift_imm. */
10914 shift_imm = bits (arm_insn_r->arm_insn, 7, 11);
10915 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10916 regcache_raw_read_signed (reg_cache, reg_src1, &s_word);
10917 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10918 /* Offset_12 used as shift. */
10919 switch (offset_12)
10920 {
10921 case 0:
10922 /* Offset_12 used as index. */
10923 offset_12 = u_regval[0] << shift_imm;
10924 break;
10925
10926 case 1:
10927 offset_12 = (!shift_imm)?0:u_regval[0] >> shift_imm;
10928 break;
10929
10930 case 2:
10931 if (!shift_imm)
10932 {
10933 if (bit (u_regval[0], 31))
10934 {
10935 offset_12 = 0xFFFFFFFF;
10936 }
10937 else
10938 {
10939 offset_12 = 0;
10940 }
10941 }
10942 else
10943 {
10944 /* This is arithmetic shift. */
10945 offset_12 = s_word >> shift_imm;
10946 }
10947 break;
10948
10949 case 3:
10950 if (!shift_imm)
10951 {
10952 regcache_raw_read_unsigned (reg_cache, ARM_PS_REGNUM,
10953 &u_regval[1]);
10954 /* Get C flag value and shift it by 31. */
10955 offset_12 = (((bit (u_regval[1], 29)) << 31) \
10956 | (u_regval[0]) >> 1);
10957 }
10958 else
10959 {
10960 offset_12 = (u_regval[0] >> shift_imm) \
10961 | (u_regval[0] <<
10962 (sizeof(uint32_t) - shift_imm));
10963 }
10964 break;
10965
10966 default:
10967 gdb_assert_not_reached ("no decoding pattern found");
10968 break;
10969 }
10970
10971 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10972 /* bit U set. */
10973 if (bit (arm_insn_r->arm_insn, 23))
10974 {
10975 tgt_mem_addr = u_regval[1] + offset_12;
10976 }
10977 else
10978 {
10979 tgt_mem_addr = u_regval[1] - offset_12;
10980 }
10981
10982 switch (arm_insn_r->opcode)
10983 {
10984 /* STR. */
10985 case 8:
10986 case 12:
10987 /* STR. */
10988 case 9:
10989 case 13:
10990 /* STRT. */
10991 case 1:
10992 case 5:
10993 /* STR. */
10994 case 0:
10995 case 4:
10996 record_buf_mem[0] = 4;
10997 break;
10998
10999 /* STRB. */
11000 case 10:
11001 case 14:
11002 /* STRB. */
11003 case 11:
11004 case 15:
11005 /* STRBT. */
11006 case 3:
11007 case 7:
11008 /* STRB. */
11009 case 2:
11010 case 6:
11011 record_buf_mem[0] = 1;
11012 break;
11013
11014 default:
11015 gdb_assert_not_reached ("no decoding pattern found");
11016 break;
11017 }
11018 record_buf_mem[1] = tgt_mem_addr;
11019 arm_insn_r->mem_rec_count = 1;
11020
11021 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11022 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11023 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11024 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11025 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11026 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11027 )
11028 {
11029 /* Rn is going to be changed in register scaled pre-indexed
11030 mode,and scaled post indexed mode. */
11031 record_buf[0] = reg_src2;
11032 arm_insn_r->reg_rec_count = 1;
11033 }
11034 }
11035 }
11036
11037 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11038 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11039 return 0;
11040}
11041
71e396f9 11042/* Handle ARM mode instructions with opcode 100. */
72508ac0
PO
11043
11044static int
11045arm_record_ld_st_multiple (insn_decode_record *arm_insn_r)
11046{
11047 struct regcache *reg_cache = arm_insn_r->regcache;
71e396f9
LM
11048 uint32_t register_count = 0, register_bits;
11049 uint32_t reg_base, addr_mode;
72508ac0 11050 uint32_t record_buf[24], record_buf_mem[48];
71e396f9
LM
11051 uint32_t wback;
11052 ULONGEST u_regval;
72508ac0 11053
71e396f9
LM
11054 /* Fetch the list of registers. */
11055 register_bits = bits (arm_insn_r->arm_insn, 0, 15);
11056 arm_insn_r->reg_rec_count = 0;
11057
11058 /* Fetch the base register that contains the address we are loading data
11059 to. */
11060 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0 11061
71e396f9
LM
11062 /* Calculate wback. */
11063 wback = (bit (arm_insn_r->arm_insn, 21) == 1);
72508ac0
PO
11064
11065 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11066 {
71e396f9 11067 /* LDM/LDMIA/LDMFD, LDMDA/LDMFA, LDMDB and LDMIB. */
72508ac0 11068
71e396f9 11069 /* Find out which registers are going to be loaded from memory. */
72508ac0 11070 while (register_bits)
71e396f9
LM
11071 {
11072 if (register_bits & 0x00000001)
11073 record_buf[arm_insn_r->reg_rec_count++] = register_count;
11074 register_bits = register_bits >> 1;
11075 register_count++;
11076 }
72508ac0 11077
71e396f9
LM
11078
11079 /* If wback is true, also save the base register, which is going to be
11080 written to. */
11081 if (wback)
11082 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
11083
11084 /* Save the CPSR register. */
11085 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
72508ac0
PO
11086 }
11087 else
11088 {
71e396f9 11089 /* STM (STMIA, STMEA), STMDA (STMED), STMDB (STMFD) and STMIB (STMFA). */
72508ac0 11090
71e396f9
LM
11091 addr_mode = bits (arm_insn_r->arm_insn, 23, 24);
11092
11093 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
11094
11095 /* Find out how many registers are going to be stored to memory. */
72508ac0 11096 while (register_bits)
71e396f9
LM
11097 {
11098 if (register_bits & 0x00000001)
11099 register_count++;
11100 register_bits = register_bits >> 1;
11101 }
72508ac0
PO
11102
11103 switch (addr_mode)
71e396f9
LM
11104 {
11105 /* STMDA (STMED): Decrement after. */
11106 case 0:
11107 record_buf_mem[1] = (uint32_t) u_regval
f0452268 11108 - register_count * ARM_INT_REGISTER_SIZE + 4;
71e396f9
LM
11109 break;
11110 /* STM (STMIA, STMEA): Increment after. */
11111 case 1:
11112 record_buf_mem[1] = (uint32_t) u_regval;
11113 break;
11114 /* STMDB (STMFD): Decrement before. */
11115 case 2:
11116 record_buf_mem[1] = (uint32_t) u_regval
f0452268 11117 - register_count * ARM_INT_REGISTER_SIZE;
71e396f9
LM
11118 break;
11119 /* STMIB (STMFA): Increment before. */
11120 case 3:
f0452268 11121 record_buf_mem[1] = (uint32_t) u_regval + ARM_INT_REGISTER_SIZE;
71e396f9
LM
11122 break;
11123 default:
11124 gdb_assert_not_reached ("no decoding pattern found");
11125 break;
11126 }
72508ac0 11127
f0452268 11128 record_buf_mem[0] = register_count * ARM_INT_REGISTER_SIZE;
71e396f9
LM
11129 arm_insn_r->mem_rec_count = 1;
11130
11131 /* If wback is true, also save the base register, which is going to be
11132 written to. */
11133 if (wback)
11134 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
11135 }
11136
11137 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11138 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11139 return 0;
11140}
11141
11142/* Handling opcode 101 insns. */
11143
11144static int
11145arm_record_b_bl (insn_decode_record *arm_insn_r)
11146{
11147 uint32_t record_buf[8];
11148
11149 /* Handle B, BL, BLX(1) insns. */
11150 /* B simply branches so we do nothing here. */
11151 /* Note: BLX(1) doesnt fall here but instead it falls into
11152 extension space. */
11153 if (bit (arm_insn_r->arm_insn, 24))
11154 {
11155 record_buf[0] = ARM_LR_REGNUM;
11156 arm_insn_r->reg_rec_count = 1;
11157 }
11158
11159 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11160
11161 return 0;
11162}
11163
72508ac0 11164static int
c6ec2b30 11165arm_record_unsupported_insn (insn_decode_record *arm_insn_r)
72508ac0
PO
11166{
11167 printf_unfiltered (_("Process record does not support instruction "
01e57735
YQ
11168 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
11169 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
72508ac0
PO
11170
11171 return -1;
11172}
11173
5a578da5
OJ
11174/* Record handler for vector data transfer instructions. */
11175
11176static int
11177arm_record_vdata_transfer_insn (insn_decode_record *arm_insn_r)
11178{
11179 uint32_t bits_a, bit_c, bit_l, reg_t, reg_v;
11180 uint32_t record_buf[4];
11181
5a578da5
OJ
11182 reg_t = bits (arm_insn_r->arm_insn, 12, 15);
11183 reg_v = bits (arm_insn_r->arm_insn, 21, 23);
11184 bits_a = bits (arm_insn_r->arm_insn, 21, 23);
11185 bit_l = bit (arm_insn_r->arm_insn, 20);
11186 bit_c = bit (arm_insn_r->arm_insn, 8);
11187
11188 /* Handle VMOV instruction. */
11189 if (bit_l && bit_c)
11190 {
11191 record_buf[0] = reg_t;
11192 arm_insn_r->reg_rec_count = 1;
11193 }
11194 else if (bit_l && !bit_c)
11195 {
11196 /* Handle VMOV instruction. */
11197 if (bits_a == 0x00)
11198 {
f1771dce 11199 record_buf[0] = reg_t;
5a578da5
OJ
11200 arm_insn_r->reg_rec_count = 1;
11201 }
11202 /* Handle VMRS instruction. */
11203 else if (bits_a == 0x07)
11204 {
11205 if (reg_t == 15)
11206 reg_t = ARM_PS_REGNUM;
11207
11208 record_buf[0] = reg_t;
11209 arm_insn_r->reg_rec_count = 1;
11210 }
11211 }
11212 else if (!bit_l && !bit_c)
11213 {
11214 /* Handle VMOV instruction. */
11215 if (bits_a == 0x00)
11216 {
f1771dce 11217 record_buf[0] = ARM_D0_REGNUM + reg_v;
5a578da5
OJ
11218
11219 arm_insn_r->reg_rec_count = 1;
11220 }
11221 /* Handle VMSR instruction. */
11222 else if (bits_a == 0x07)
11223 {
11224 record_buf[0] = ARM_FPSCR_REGNUM;
11225 arm_insn_r->reg_rec_count = 1;
11226 }
11227 }
11228 else if (!bit_l && bit_c)
11229 {
11230 /* Handle VMOV instruction. */
11231 if (!(bits_a & 0x04))
11232 {
11233 record_buf[0] = (reg_v | (bit (arm_insn_r->arm_insn, 7) << 4))
11234 + ARM_D0_REGNUM;
11235 arm_insn_r->reg_rec_count = 1;
11236 }
11237 /* Handle VDUP instruction. */
11238 else
11239 {
11240 if (bit (arm_insn_r->arm_insn, 21))
11241 {
11242 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
11243 record_buf[0] = reg_v + ARM_D0_REGNUM;
11244 record_buf[1] = reg_v + ARM_D0_REGNUM + 1;
11245 arm_insn_r->reg_rec_count = 2;
11246 }
11247 else
11248 {
11249 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
11250 record_buf[0] = reg_v + ARM_D0_REGNUM;
11251 arm_insn_r->reg_rec_count = 1;
11252 }
11253 }
11254 }
11255
11256 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11257 return 0;
11258}
11259
f20f80dd
OJ
11260/* Record handler for extension register load/store instructions. */
11261
11262static int
11263arm_record_exreg_ld_st_insn (insn_decode_record *arm_insn_r)
11264{
11265 uint32_t opcode, single_reg;
11266 uint8_t op_vldm_vstm;
11267 uint32_t record_buf[8], record_buf_mem[128];
11268 ULONGEST u_regval = 0;
11269
11270 struct regcache *reg_cache = arm_insn_r->regcache;
f20f80dd
OJ
11271
11272 opcode = bits (arm_insn_r->arm_insn, 20, 24);
9fde51ed 11273 single_reg = !bit (arm_insn_r->arm_insn, 8);
f20f80dd
OJ
11274 op_vldm_vstm = opcode & 0x1b;
11275
11276 /* Handle VMOV instructions. */
11277 if ((opcode & 0x1e) == 0x04)
11278 {
9fde51ed 11279 if (bit (arm_insn_r->arm_insn, 20)) /* to_arm_registers bit 20? */
01e57735
YQ
11280 {
11281 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11282 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
11283 arm_insn_r->reg_rec_count = 2;
11284 }
f20f80dd 11285 else
01e57735 11286 {
9fde51ed
YQ
11287 uint8_t reg_m = bits (arm_insn_r->arm_insn, 0, 3);
11288 uint8_t bit_m = bit (arm_insn_r->arm_insn, 5);
f20f80dd 11289
9fde51ed 11290 if (single_reg)
01e57735 11291 {
9fde51ed
YQ
11292 /* The first S register number m is REG_M:M (M is bit 5),
11293 the corresponding D register number is REG_M:M / 2, which
11294 is REG_M. */
11295 record_buf[arm_insn_r->reg_rec_count++] = ARM_D0_REGNUM + reg_m;
11296 /* The second S register number is REG_M:M + 1, the
11297 corresponding D register number is (REG_M:M + 1) / 2.
11298 IOW, if bit M is 1, the first and second S registers
11299 are mapped to different D registers, otherwise, they are
11300 in the same D register. */
11301 if (bit_m)
11302 {
11303 record_buf[arm_insn_r->reg_rec_count++]
11304 = ARM_D0_REGNUM + reg_m + 1;
11305 }
01e57735
YQ
11306 }
11307 else
11308 {
9fde51ed 11309 record_buf[0] = ((bit_m << 4) + reg_m + ARM_D0_REGNUM);
01e57735
YQ
11310 arm_insn_r->reg_rec_count = 1;
11311 }
11312 }
f20f80dd
OJ
11313 }
11314 /* Handle VSTM and VPUSH instructions. */
11315 else if (op_vldm_vstm == 0x08 || op_vldm_vstm == 0x0a
01e57735 11316 || op_vldm_vstm == 0x12)
f20f80dd
OJ
11317 {
11318 uint32_t start_address, reg_rn, imm_off32, imm_off8, memory_count;
11319 uint32_t memory_index = 0;
11320
11321 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
11322 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
11323 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
9fde51ed 11324 imm_off32 = imm_off8 << 2;
f20f80dd
OJ
11325 memory_count = imm_off8;
11326
11327 if (bit (arm_insn_r->arm_insn, 23))
01e57735 11328 start_address = u_regval;
f20f80dd 11329 else
01e57735 11330 start_address = u_regval - imm_off32;
f20f80dd
OJ
11331
11332 if (bit (arm_insn_r->arm_insn, 21))
01e57735
YQ
11333 {
11334 record_buf[0] = reg_rn;
11335 arm_insn_r->reg_rec_count = 1;
11336 }
f20f80dd
OJ
11337
11338 while (memory_count > 0)
01e57735 11339 {
9fde51ed 11340 if (single_reg)
01e57735 11341 {
9fde51ed
YQ
11342 record_buf_mem[memory_index] = 4;
11343 record_buf_mem[memory_index + 1] = start_address;
01e57735
YQ
11344 start_address = start_address + 4;
11345 memory_index = memory_index + 2;
11346 }
11347 else
11348 {
9fde51ed
YQ
11349 record_buf_mem[memory_index] = 4;
11350 record_buf_mem[memory_index + 1] = start_address;
11351 record_buf_mem[memory_index + 2] = 4;
11352 record_buf_mem[memory_index + 3] = start_address + 4;
01e57735
YQ
11353 start_address = start_address + 8;
11354 memory_index = memory_index + 4;
11355 }
11356 memory_count--;
11357 }
f20f80dd
OJ
11358 arm_insn_r->mem_rec_count = (memory_index >> 1);
11359 }
11360 /* Handle VLDM instructions. */
11361 else if (op_vldm_vstm == 0x09 || op_vldm_vstm == 0x0b
01e57735 11362 || op_vldm_vstm == 0x13)
f20f80dd
OJ
11363 {
11364 uint32_t reg_count, reg_vd;
11365 uint32_t reg_index = 0;
9fde51ed 11366 uint32_t bit_d = bit (arm_insn_r->arm_insn, 22);
f20f80dd
OJ
11367
11368 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
11369 reg_count = bits (arm_insn_r->arm_insn, 0, 7);
11370
9fde51ed
YQ
11371 /* REG_VD is the first D register number. If the instruction
11372 loads memory to S registers (SINGLE_REG is TRUE), the register
11373 number is (REG_VD << 1 | bit D), so the corresponding D
11374 register number is (REG_VD << 1 | bit D) / 2 = REG_VD. */
11375 if (!single_reg)
11376 reg_vd = reg_vd | (bit_d << 4);
f20f80dd 11377
9fde51ed 11378 if (bit (arm_insn_r->arm_insn, 21) /* write back */)
01e57735 11379 record_buf[reg_index++] = bits (arm_insn_r->arm_insn, 16, 19);
f20f80dd 11380
9fde51ed
YQ
11381 /* If the instruction loads memory to D register, REG_COUNT should
11382 be divided by 2, according to the ARM Architecture Reference
11383 Manual. If the instruction loads memory to S register, divide by
11384 2 as well because two S registers are mapped to D register. */
11385 reg_count = reg_count / 2;
11386 if (single_reg && bit_d)
01e57735 11387 {
9fde51ed
YQ
11388 /* Increase the register count if S register list starts from
11389 an odd number (bit d is one). */
11390 reg_count++;
11391 }
f20f80dd 11392
9fde51ed
YQ
11393 while (reg_count > 0)
11394 {
11395 record_buf[reg_index++] = ARM_D0_REGNUM + reg_vd + reg_count - 1;
01e57735
YQ
11396 reg_count--;
11397 }
f20f80dd
OJ
11398 arm_insn_r->reg_rec_count = reg_index;
11399 }
11400 /* VSTR Vector store register. */
11401 else if ((opcode & 0x13) == 0x10)
11402 {
bec2ab5a 11403 uint32_t start_address, reg_rn, imm_off32, imm_off8;
f20f80dd
OJ
11404 uint32_t memory_index = 0;
11405
11406 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
11407 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
11408 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
9fde51ed 11409 imm_off32 = imm_off8 << 2;
f20f80dd
OJ
11410
11411 if (bit (arm_insn_r->arm_insn, 23))
01e57735 11412 start_address = u_regval + imm_off32;
f20f80dd 11413 else
01e57735 11414 start_address = u_regval - imm_off32;
f20f80dd
OJ
11415
11416 if (single_reg)
01e57735 11417 {
9fde51ed
YQ
11418 record_buf_mem[memory_index] = 4;
11419 record_buf_mem[memory_index + 1] = start_address;
01e57735
YQ
11420 arm_insn_r->mem_rec_count = 1;
11421 }
f20f80dd 11422 else
01e57735 11423 {
9fde51ed
YQ
11424 record_buf_mem[memory_index] = 4;
11425 record_buf_mem[memory_index + 1] = start_address;
11426 record_buf_mem[memory_index + 2] = 4;
11427 record_buf_mem[memory_index + 3] = start_address + 4;
01e57735
YQ
11428 arm_insn_r->mem_rec_count = 2;
11429 }
f20f80dd
OJ
11430 }
11431 /* VLDR Vector load register. */
11432 else if ((opcode & 0x13) == 0x11)
11433 {
11434 uint32_t reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
11435
11436 if (!single_reg)
01e57735
YQ
11437 {
11438 reg_vd = reg_vd | (bit (arm_insn_r->arm_insn, 22) << 4);
11439 record_buf[0] = ARM_D0_REGNUM + reg_vd;
11440 }
f20f80dd 11441 else
01e57735
YQ
11442 {
11443 reg_vd = (reg_vd << 1) | bit (arm_insn_r->arm_insn, 22);
9fde51ed
YQ
11444 /* Record register D rather than pseudo register S. */
11445 record_buf[0] = ARM_D0_REGNUM + reg_vd / 2;
01e57735 11446 }
f20f80dd
OJ
11447 arm_insn_r->reg_rec_count = 1;
11448 }
11449
11450 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11451 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11452 return 0;
11453}
11454
851f26ae
OJ
11455/* Record handler for arm/thumb mode VFP data processing instructions. */
11456
11457static int
11458arm_record_vfp_data_proc_insn (insn_decode_record *arm_insn_r)
11459{
11460 uint32_t opc1, opc2, opc3, dp_op_sz, bit_d, reg_vd;
11461 uint32_t record_buf[4];
11462 enum insn_types {INSN_T0, INSN_T1, INSN_T2, INSN_T3, INSN_INV};
11463 enum insn_types curr_insn_type = INSN_INV;
11464
11465 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
11466 opc1 = bits (arm_insn_r->arm_insn, 20, 23);
11467 opc2 = bits (arm_insn_r->arm_insn, 16, 19);
11468 opc3 = bits (arm_insn_r->arm_insn, 6, 7);
11469 dp_op_sz = bit (arm_insn_r->arm_insn, 8);
11470 bit_d = bit (arm_insn_r->arm_insn, 22);
ce887586
TT
11471 /* Mask off the "D" bit. */
11472 opc1 = opc1 & ~0x04;
851f26ae
OJ
11473
11474 /* Handle VMLA, VMLS. */
11475 if (opc1 == 0x00)
11476 {
11477 if (bit (arm_insn_r->arm_insn, 10))
11478 {
11479 if (bit (arm_insn_r->arm_insn, 6))
11480 curr_insn_type = INSN_T0;
11481 else
11482 curr_insn_type = INSN_T1;
11483 }
11484 else
11485 {
11486 if (dp_op_sz)
11487 curr_insn_type = INSN_T1;
11488 else
11489 curr_insn_type = INSN_T2;
11490 }
11491 }
11492 /* Handle VNMLA, VNMLS, VNMUL. */
11493 else if (opc1 == 0x01)
11494 {
11495 if (dp_op_sz)
11496 curr_insn_type = INSN_T1;
11497 else
11498 curr_insn_type = INSN_T2;
11499 }
11500 /* Handle VMUL. */
11501 else if (opc1 == 0x02 && !(opc3 & 0x01))
11502 {
11503 if (bit (arm_insn_r->arm_insn, 10))
11504 {
11505 if (bit (arm_insn_r->arm_insn, 6))
11506 curr_insn_type = INSN_T0;
11507 else
11508 curr_insn_type = INSN_T1;
11509 }
11510 else
11511 {
11512 if (dp_op_sz)
11513 curr_insn_type = INSN_T1;
11514 else
11515 curr_insn_type = INSN_T2;
11516 }
11517 }
11518 /* Handle VADD, VSUB. */
11519 else if (opc1 == 0x03)
11520 {
11521 if (!bit (arm_insn_r->arm_insn, 9))
11522 {
11523 if (bit (arm_insn_r->arm_insn, 6))
11524 curr_insn_type = INSN_T0;
11525 else
11526 curr_insn_type = INSN_T1;
11527 }
11528 else
11529 {
11530 if (dp_op_sz)
11531 curr_insn_type = INSN_T1;
11532 else
11533 curr_insn_type = INSN_T2;
11534 }
11535 }
11536 /* Handle VDIV. */
ce887586 11537 else if (opc1 == 0x08)
851f26ae
OJ
11538 {
11539 if (dp_op_sz)
11540 curr_insn_type = INSN_T1;
11541 else
11542 curr_insn_type = INSN_T2;
11543 }
11544 /* Handle all other vfp data processing instructions. */
11545 else if (opc1 == 0x0b)
11546 {
11547 /* Handle VMOV. */
11548 if (!(opc3 & 0x01) || (opc2 == 0x00 && opc3 == 0x01))
11549 {
11550 if (bit (arm_insn_r->arm_insn, 4))
11551 {
11552 if (bit (arm_insn_r->arm_insn, 6))
11553 curr_insn_type = INSN_T0;
11554 else
11555 curr_insn_type = INSN_T1;
11556 }
11557 else
11558 {
11559 if (dp_op_sz)
11560 curr_insn_type = INSN_T1;
11561 else
11562 curr_insn_type = INSN_T2;
11563 }
11564 }
11565 /* Handle VNEG and VABS. */
11566 else if ((opc2 == 0x01 && opc3 == 0x01)
11567 || (opc2 == 0x00 && opc3 == 0x03))
11568 {
11569 if (!bit (arm_insn_r->arm_insn, 11))
11570 {
11571 if (bit (arm_insn_r->arm_insn, 6))
11572 curr_insn_type = INSN_T0;
11573 else
11574 curr_insn_type = INSN_T1;
11575 }
11576 else
11577 {
11578 if (dp_op_sz)
11579 curr_insn_type = INSN_T1;
11580 else
11581 curr_insn_type = INSN_T2;
11582 }
11583 }
11584 /* Handle VSQRT. */
11585 else if (opc2 == 0x01 && opc3 == 0x03)
11586 {
11587 if (dp_op_sz)
11588 curr_insn_type = INSN_T1;
11589 else
11590 curr_insn_type = INSN_T2;
11591 }
11592 /* Handle VCVT. */
11593 else if (opc2 == 0x07 && opc3 == 0x03)
11594 {
11595 if (!dp_op_sz)
11596 curr_insn_type = INSN_T1;
11597 else
11598 curr_insn_type = INSN_T2;
11599 }
11600 else if (opc3 & 0x01)
11601 {
11602 /* Handle VCVT. */
11603 if ((opc2 == 0x08) || (opc2 & 0x0e) == 0x0c)
11604 {
11605 if (!bit (arm_insn_r->arm_insn, 18))
11606 curr_insn_type = INSN_T2;
11607 else
11608 {
11609 if (dp_op_sz)
11610 curr_insn_type = INSN_T1;
11611 else
11612 curr_insn_type = INSN_T2;
11613 }
11614 }
11615 /* Handle VCVT. */
11616 else if ((opc2 & 0x0e) == 0x0a || (opc2 & 0x0e) == 0x0e)
11617 {
11618 if (dp_op_sz)
11619 curr_insn_type = INSN_T1;
11620 else
11621 curr_insn_type = INSN_T2;
11622 }
11623 /* Handle VCVTB, VCVTT. */
11624 else if ((opc2 & 0x0e) == 0x02)
11625 curr_insn_type = INSN_T2;
11626 /* Handle VCMP, VCMPE. */
11627 else if ((opc2 & 0x0e) == 0x04)
11628 curr_insn_type = INSN_T3;
11629 }
11630 }
11631
11632 switch (curr_insn_type)
11633 {
11634 case INSN_T0:
11635 reg_vd = reg_vd | (bit_d << 4);
11636 record_buf[0] = reg_vd + ARM_D0_REGNUM;
11637 record_buf[1] = reg_vd + ARM_D0_REGNUM + 1;
11638 arm_insn_r->reg_rec_count = 2;
11639 break;
11640
11641 case INSN_T1:
11642 reg_vd = reg_vd | (bit_d << 4);
11643 record_buf[0] = reg_vd + ARM_D0_REGNUM;
11644 arm_insn_r->reg_rec_count = 1;
11645 break;
11646
11647 case INSN_T2:
11648 reg_vd = (reg_vd << 1) | bit_d;
11649 record_buf[0] = reg_vd + ARM_D0_REGNUM;
11650 arm_insn_r->reg_rec_count = 1;
11651 break;
11652
11653 case INSN_T3:
11654 record_buf[0] = ARM_FPSCR_REGNUM;
11655 arm_insn_r->reg_rec_count = 1;
11656 break;
11657
11658 default:
11659 gdb_assert_not_reached ("no decoding pattern found");
11660 break;
11661 }
11662
11663 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11664 return 0;
11665}
11666
60cc5e93
OJ
11667/* Handling opcode 110 insns. */
11668
11669static int
11670arm_record_asimd_vfp_coproc (insn_decode_record *arm_insn_r)
11671{
bec2ab5a 11672 uint32_t op1, op1_ebit, coproc;
60cc5e93
OJ
11673
11674 coproc = bits (arm_insn_r->arm_insn, 8, 11);
11675 op1 = bits (arm_insn_r->arm_insn, 20, 25);
11676 op1_ebit = bit (arm_insn_r->arm_insn, 20);
11677
11678 if ((coproc & 0x0e) == 0x0a)
11679 {
11680 /* Handle extension register ld/st instructions. */
11681 if (!(op1 & 0x20))
f20f80dd 11682 return arm_record_exreg_ld_st_insn (arm_insn_r);
60cc5e93
OJ
11683
11684 /* 64-bit transfers between arm core and extension registers. */
11685 if ((op1 & 0x3e) == 0x04)
f20f80dd 11686 return arm_record_exreg_ld_st_insn (arm_insn_r);
60cc5e93
OJ
11687 }
11688 else
11689 {
11690 /* Handle coprocessor ld/st instructions. */
11691 if (!(op1 & 0x3a))
11692 {
11693 /* Store. */
11694 if (!op1_ebit)
11695 return arm_record_unsupported_insn (arm_insn_r);
11696 else
11697 /* Load. */
11698 return arm_record_unsupported_insn (arm_insn_r);
11699 }
11700
11701 /* Move to coprocessor from two arm core registers. */
11702 if (op1 == 0x4)
11703 return arm_record_unsupported_insn (arm_insn_r);
11704
11705 /* Move to two arm core registers from coprocessor. */
11706 if (op1 == 0x5)
11707 {
11708 uint32_t reg_t[2];
11709
11710 reg_t[0] = bits (arm_insn_r->arm_insn, 12, 15);
11711 reg_t[1] = bits (arm_insn_r->arm_insn, 16, 19);
11712 arm_insn_r->reg_rec_count = 2;
11713
11714 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, reg_t);
11715 return 0;
11716 }
11717 }
11718 return arm_record_unsupported_insn (arm_insn_r);
11719}
11720
72508ac0
PO
11721/* Handling opcode 111 insns. */
11722
11723static int
11724arm_record_coproc_data_proc (insn_decode_record *arm_insn_r)
11725{
2d9e6acb 11726 uint32_t op, op1_ebit, coproc, bits_24_25;
72508ac0
PO
11727 struct gdbarch_tdep *tdep = gdbarch_tdep (arm_insn_r->gdbarch);
11728 struct regcache *reg_cache = arm_insn_r->regcache;
72508ac0
PO
11729
11730 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 24, 27);
60cc5e93 11731 coproc = bits (arm_insn_r->arm_insn, 8, 11);
60cc5e93
OJ
11732 op1_ebit = bit (arm_insn_r->arm_insn, 20);
11733 op = bit (arm_insn_r->arm_insn, 4);
2d9e6acb 11734 bits_24_25 = bits (arm_insn_r->arm_insn, 24, 25);
97dfe206
OJ
11735
11736 /* Handle arm SWI/SVC system call instructions. */
2d9e6acb 11737 if (bits_24_25 == 0x3)
97dfe206
OJ
11738 {
11739 if (tdep->arm_syscall_record != NULL)
11740 {
11741 ULONGEST svc_operand, svc_number;
11742
11743 svc_operand = (0x00ffffff & arm_insn_r->arm_insn);
11744
11745 if (svc_operand) /* OABI. */
11746 svc_number = svc_operand - 0x900000;
11747 else /* EABI. */
11748 regcache_raw_read_unsigned (reg_cache, 7, &svc_number);
11749
60cc5e93 11750 return tdep->arm_syscall_record (reg_cache, svc_number);
97dfe206
OJ
11751 }
11752 else
11753 {
11754 printf_unfiltered (_("no syscall record support\n"));
60cc5e93 11755 return -1;
97dfe206
OJ
11756 }
11757 }
2d9e6acb 11758 else if (bits_24_25 == 0x02)
60cc5e93 11759 {
2d9e6acb
YQ
11760 if (op)
11761 {
11762 if ((coproc & 0x0e) == 0x0a)
11763 {
11764 /* 8, 16, and 32-bit transfer */
11765 return arm_record_vdata_transfer_insn (arm_insn_r);
11766 }
11767 else
11768 {
11769 if (op1_ebit)
11770 {
11771 /* MRC, MRC2 */
11772 uint32_t record_buf[1];
11773
11774 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11775 if (record_buf[0] == 15)
11776 record_buf[0] = ARM_PS_REGNUM;
60cc5e93 11777
2d9e6acb
YQ
11778 arm_insn_r->reg_rec_count = 1;
11779 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count,
11780 record_buf);
11781 return 0;
11782 }
11783 else
11784 {
11785 /* MCR, MCR2 */
11786 return -1;
11787 }
11788 }
11789 }
11790 else
11791 {
11792 if ((coproc & 0x0e) == 0x0a)
11793 {
11794 /* VFP data-processing instructions. */
11795 return arm_record_vfp_data_proc_insn (arm_insn_r);
11796 }
11797 else
11798 {
11799 /* CDP, CDP2 */
11800 return -1;
11801 }
11802 }
60cc5e93 11803 }
97dfe206
OJ
11804 else
11805 {
2d9e6acb 11806 unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 25);
60cc5e93 11807
2d9e6acb
YQ
11808 if (op1 == 5)
11809 {
11810 if ((coproc & 0x0e) != 0x0a)
11811 {
11812 /* MRRC, MRRC2 */
11813 return -1;
11814 }
11815 }
11816 else if (op1 == 4 || op1 == 5)
11817 {
11818 if ((coproc & 0x0e) == 0x0a)
11819 {
11820 /* 64-bit transfers between ARM core and extension */
11821 return -1;
11822 }
11823 else if (op1 == 4)
11824 {
11825 /* MCRR, MCRR2 */
11826 return -1;
11827 }
11828 }
11829 else if (op1 == 0 || op1 == 1)
11830 {
11831 /* UNDEFINED */
11832 return -1;
11833 }
11834 else
11835 {
11836 if ((coproc & 0x0e) == 0x0a)
11837 {
11838 /* Extension register load/store */
11839 }
11840 else
11841 {
11842 /* STC, STC2, LDC, LDC2 */
11843 }
11844 return -1;
11845 }
97dfe206 11846 }
72508ac0 11847
2d9e6acb 11848 return -1;
72508ac0
PO
11849}
11850
11851/* Handling opcode 000 insns. */
11852
11853static int
11854thumb_record_shift_add_sub (insn_decode_record *thumb_insn_r)
11855{
11856 uint32_t record_buf[8];
11857 uint32_t reg_src1 = 0;
11858
11859 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11860
11861 record_buf[0] = ARM_PS_REGNUM;
11862 record_buf[1] = reg_src1;
11863 thumb_insn_r->reg_rec_count = 2;
11864
11865 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11866
11867 return 0;
11868}
11869
11870
11871/* Handling opcode 001 insns. */
11872
11873static int
11874thumb_record_add_sub_cmp_mov (insn_decode_record *thumb_insn_r)
11875{
11876 uint32_t record_buf[8];
11877 uint32_t reg_src1 = 0;
11878
11879 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11880
11881 record_buf[0] = ARM_PS_REGNUM;
11882 record_buf[1] = reg_src1;
11883 thumb_insn_r->reg_rec_count = 2;
11884
11885 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11886
11887 return 0;
11888}
11889
11890/* Handling opcode 010 insns. */
11891
11892static int
11893thumb_record_ld_st_reg_offset (insn_decode_record *thumb_insn_r)
11894{
11895 struct regcache *reg_cache = thumb_insn_r->regcache;
11896 uint32_t record_buf[8], record_buf_mem[8];
11897
11898 uint32_t reg_src1 = 0, reg_src2 = 0;
11899 uint32_t opcode1 = 0, opcode2 = 0, opcode3 = 0;
11900
11901 ULONGEST u_regval[2] = {0};
11902
11903 opcode1 = bits (thumb_insn_r->arm_insn, 10, 12);
11904
11905 if (bit (thumb_insn_r->arm_insn, 12))
11906 {
11907 /* Handle load/store register offset. */
b121eeb9
YQ
11908 uint32_t opB = bits (thumb_insn_r->arm_insn, 9, 11);
11909
b020ff80 11910 if (in_inclusive_range (opB, 4U, 7U))
72508ac0
PO
11911 {
11912 /* LDR(2), LDRB(2) , LDRH(2), LDRSB, LDRSH. */
11913 reg_src1 = bits (thumb_insn_r->arm_insn,0, 2);
11914 record_buf[0] = reg_src1;
11915 thumb_insn_r->reg_rec_count = 1;
11916 }
b020ff80 11917 else if (in_inclusive_range (opB, 0U, 2U))
72508ac0
PO
11918 {
11919 /* STR(2), STRB(2), STRH(2) . */
11920 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
11921 reg_src2 = bits (thumb_insn_r->arm_insn, 6, 8);
11922 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11923 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
b121eeb9 11924 if (0 == opB)
72508ac0 11925 record_buf_mem[0] = 4; /* STR (2). */
b121eeb9 11926 else if (2 == opB)
72508ac0 11927 record_buf_mem[0] = 1; /* STRB (2). */
b121eeb9 11928 else if (1 == opB)
72508ac0
PO
11929 record_buf_mem[0] = 2; /* STRH (2). */
11930 record_buf_mem[1] = u_regval[0] + u_regval[1];
11931 thumb_insn_r->mem_rec_count = 1;
11932 }
11933 }
11934 else if (bit (thumb_insn_r->arm_insn, 11))
11935 {
11936 /* Handle load from literal pool. */
11937 /* LDR(3). */
11938 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11939 record_buf[0] = reg_src1;
11940 thumb_insn_r->reg_rec_count = 1;
11941 }
11942 else if (opcode1)
11943 {
b121eeb9 11944 /* Special data instructions and branch and exchange */
72508ac0
PO
11945 opcode2 = bits (thumb_insn_r->arm_insn, 8, 9);
11946 opcode3 = bits (thumb_insn_r->arm_insn, 0, 2);
11947 if ((3 == opcode2) && (!opcode3))
11948 {
11949 /* Branch with exchange. */
11950 record_buf[0] = ARM_PS_REGNUM;
11951 thumb_insn_r->reg_rec_count = 1;
11952 }
11953 else
11954 {
1f33efec
YQ
11955 /* Format 8; special data processing insns. */
11956 record_buf[0] = ARM_PS_REGNUM;
11957 record_buf[1] = (bit (thumb_insn_r->arm_insn, 7) << 3
11958 | bits (thumb_insn_r->arm_insn, 0, 2));
72508ac0
PO
11959 thumb_insn_r->reg_rec_count = 2;
11960 }
11961 }
11962 else
11963 {
11964 /* Format 5; data processing insns. */
11965 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11966 if (bit (thumb_insn_r->arm_insn, 7))
11967 {
11968 reg_src1 = reg_src1 + 8;
11969 }
11970 record_buf[0] = ARM_PS_REGNUM;
11971 record_buf[1] = reg_src1;
11972 thumb_insn_r->reg_rec_count = 2;
11973 }
11974
11975 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11976 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
11977 record_buf_mem);
11978
11979 return 0;
11980}
11981
11982/* Handling opcode 001 insns. */
11983
11984static int
11985thumb_record_ld_st_imm_offset (insn_decode_record *thumb_insn_r)
11986{
11987 struct regcache *reg_cache = thumb_insn_r->regcache;
11988 uint32_t record_buf[8], record_buf_mem[8];
11989
11990 uint32_t reg_src1 = 0;
11991 uint32_t opcode = 0, immed_5 = 0;
11992
11993 ULONGEST u_regval = 0;
11994
11995 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
11996
11997 if (opcode)
11998 {
11999 /* LDR(1). */
12000 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12001 record_buf[0] = reg_src1;
12002 thumb_insn_r->reg_rec_count = 1;
12003 }
12004 else
12005 {
12006 /* STR(1). */
12007 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12008 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
12009 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12010 record_buf_mem[0] = 4;
12011 record_buf_mem[1] = u_regval + (immed_5 * 4);
12012 thumb_insn_r->mem_rec_count = 1;
12013 }
12014
12015 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12016 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12017 record_buf_mem);
12018
12019 return 0;
12020}
12021
12022/* Handling opcode 100 insns. */
12023
12024static int
12025thumb_record_ld_st_stack (insn_decode_record *thumb_insn_r)
12026{
12027 struct regcache *reg_cache = thumb_insn_r->regcache;
12028 uint32_t record_buf[8], record_buf_mem[8];
12029
12030 uint32_t reg_src1 = 0;
12031 uint32_t opcode = 0, immed_8 = 0, immed_5 = 0;
12032
12033 ULONGEST u_regval = 0;
12034
12035 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12036
12037 if (3 == opcode)
12038 {
12039 /* LDR(4). */
12040 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12041 record_buf[0] = reg_src1;
12042 thumb_insn_r->reg_rec_count = 1;
12043 }
12044 else if (1 == opcode)
12045 {
12046 /* LDRH(1). */
12047 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12048 record_buf[0] = reg_src1;
12049 thumb_insn_r->reg_rec_count = 1;
12050 }
12051 else if (2 == opcode)
12052 {
12053 /* STR(3). */
12054 immed_8 = bits (thumb_insn_r->arm_insn, 0, 7);
12055 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
12056 record_buf_mem[0] = 4;
12057 record_buf_mem[1] = u_regval + (immed_8 * 4);
12058 thumb_insn_r->mem_rec_count = 1;
12059 }
12060 else if (0 == opcode)
12061 {
12062 /* STRH(1). */
12063 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
12064 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12065 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12066 record_buf_mem[0] = 2;
12067 record_buf_mem[1] = u_regval + (immed_5 * 2);
12068 thumb_insn_r->mem_rec_count = 1;
12069 }
12070
12071 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12072 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12073 record_buf_mem);
12074
12075 return 0;
12076}
12077
12078/* Handling opcode 101 insns. */
12079
12080static int
12081thumb_record_misc (insn_decode_record *thumb_insn_r)
12082{
12083 struct regcache *reg_cache = thumb_insn_r->regcache;
12084
b121eeb9 12085 uint32_t opcode = 0;
72508ac0 12086 uint32_t register_bits = 0, register_count = 0;
bec2ab5a 12087 uint32_t index = 0, start_address = 0;
72508ac0
PO
12088 uint32_t record_buf[24], record_buf_mem[48];
12089 uint32_t reg_src1;
12090
12091 ULONGEST u_regval = 0;
12092
12093 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
72508ac0 12094
b121eeb9 12095 if (opcode == 0 || opcode == 1)
72508ac0 12096 {
b121eeb9
YQ
12097 /* ADR and ADD (SP plus immediate) */
12098
72508ac0
PO
12099 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12100 record_buf[0] = reg_src1;
12101 thumb_insn_r->reg_rec_count = 1;
12102 }
b121eeb9 12103 else
72508ac0 12104 {
b121eeb9
YQ
12105 /* Miscellaneous 16-bit instructions */
12106 uint32_t opcode2 = bits (thumb_insn_r->arm_insn, 8, 11);
12107
12108 switch (opcode2)
12109 {
12110 case 6:
12111 /* SETEND and CPS */
12112 break;
12113 case 0:
12114 /* ADD/SUB (SP plus immediate) */
12115 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12116 record_buf[0] = ARM_SP_REGNUM;
12117 thumb_insn_r->reg_rec_count = 1;
12118 break;
12119 case 1: /* fall through */
12120 case 3: /* fall through */
12121 case 9: /* fall through */
12122 case 11:
12123 /* CBNZ, CBZ */
b121eeb9
YQ
12124 break;
12125 case 2:
12126 /* SXTH, SXTB, UXTH, UXTB */
12127 record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
12128 thumb_insn_r->reg_rec_count = 1;
12129 break;
12130 case 4: /* fall through */
12131 case 5:
12132 /* PUSH. */
12133 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12134 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
12135 while (register_bits)
12136 {
12137 if (register_bits & 0x00000001)
12138 register_count++;
12139 register_bits = register_bits >> 1;
12140 }
12141 start_address = u_regval - \
12142 (4 * (bit (thumb_insn_r->arm_insn, 8) + register_count));
12143 thumb_insn_r->mem_rec_count = register_count;
12144 while (register_count)
12145 {
12146 record_buf_mem[(register_count * 2) - 1] = start_address;
12147 record_buf_mem[(register_count * 2) - 2] = 4;
12148 start_address = start_address + 4;
12149 register_count--;
12150 }
12151 record_buf[0] = ARM_SP_REGNUM;
12152 thumb_insn_r->reg_rec_count = 1;
12153 break;
12154 case 10:
12155 /* REV, REV16, REVSH */
ba14f379
YQ
12156 record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
12157 thumb_insn_r->reg_rec_count = 1;
b121eeb9
YQ
12158 break;
12159 case 12: /* fall through */
12160 case 13:
12161 /* POP. */
12162 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12163 while (register_bits)
12164 {
12165 if (register_bits & 0x00000001)
12166 record_buf[index++] = register_count;
12167 register_bits = register_bits >> 1;
12168 register_count++;
12169 }
12170 record_buf[index++] = ARM_PS_REGNUM;
12171 record_buf[index++] = ARM_SP_REGNUM;
12172 thumb_insn_r->reg_rec_count = index;
12173 break;
12174 case 0xe:
12175 /* BKPT insn. */
12176 /* Handle enhanced software breakpoint insn, BKPT. */
12177 /* CPSR is changed to be executed in ARM state, disabling normal
12178 interrupts, entering abort mode. */
12179 /* According to high vector configuration PC is set. */
12180 /* User hits breakpoint and type reverse, in that case, we need to go back with
12181 previous CPSR and Program Counter. */
12182 record_buf[0] = ARM_PS_REGNUM;
12183 record_buf[1] = ARM_LR_REGNUM;
12184 thumb_insn_r->reg_rec_count = 2;
12185 /* We need to save SPSR value, which is not yet done. */
12186 printf_unfiltered (_("Process record does not support instruction "
12187 "0x%0x at address %s.\n"),
12188 thumb_insn_r->arm_insn,
12189 paddress (thumb_insn_r->gdbarch,
12190 thumb_insn_r->this_addr));
12191 return -1;
12192
12193 case 0xf:
12194 /* If-Then, and hints */
12195 break;
12196 default:
12197 return -1;
12198 };
72508ac0
PO
12199 }
12200
12201 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12202 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12203 record_buf_mem);
12204
12205 return 0;
12206}
12207
12208/* Handling opcode 110 insns. */
12209
12210static int
12211thumb_record_ldm_stm_swi (insn_decode_record *thumb_insn_r)
12212{
12213 struct gdbarch_tdep *tdep = gdbarch_tdep (thumb_insn_r->gdbarch);
12214 struct regcache *reg_cache = thumb_insn_r->regcache;
12215
12216 uint32_t ret = 0; /* function return value: -1:record failure ; 0:success */
12217 uint32_t reg_src1 = 0;
12218 uint32_t opcode1 = 0, opcode2 = 0, register_bits = 0, register_count = 0;
bec2ab5a 12219 uint32_t index = 0, start_address = 0;
72508ac0
PO
12220 uint32_t record_buf[24], record_buf_mem[48];
12221
12222 ULONGEST u_regval = 0;
12223
12224 opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
12225 opcode2 = bits (thumb_insn_r->arm_insn, 11, 12);
12226
12227 if (1 == opcode2)
12228 {
12229
12230 /* LDMIA. */
12231 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12232 /* Get Rn. */
12233 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12234 while (register_bits)
12235 {
12236 if (register_bits & 0x00000001)
f969241e 12237 record_buf[index++] = register_count;
72508ac0 12238 register_bits = register_bits >> 1;
f969241e 12239 register_count++;
72508ac0 12240 }
f969241e
OJ
12241 record_buf[index++] = reg_src1;
12242 thumb_insn_r->reg_rec_count = index;
72508ac0
PO
12243 }
12244 else if (0 == opcode2)
12245 {
12246 /* It handles both STMIA. */
12247 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12248 /* Get Rn. */
12249 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12250 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12251 while (register_bits)
12252 {
12253 if (register_bits & 0x00000001)
12254 register_count++;
12255 register_bits = register_bits >> 1;
12256 }
12257 start_address = u_regval;
12258 thumb_insn_r->mem_rec_count = register_count;
12259 while (register_count)
12260 {
12261 record_buf_mem[(register_count * 2) - 1] = start_address;
12262 record_buf_mem[(register_count * 2) - 2] = 4;
12263 start_address = start_address + 4;
12264 register_count--;
12265 }
12266 }
12267 else if (0x1F == opcode1)
12268 {
12269 /* Handle arm syscall insn. */
97dfe206 12270 if (tdep->arm_syscall_record != NULL)
72508ac0 12271 {
97dfe206
OJ
12272 regcache_raw_read_unsigned (reg_cache, 7, &u_regval);
12273 ret = tdep->arm_syscall_record (reg_cache, u_regval);
72508ac0
PO
12274 }
12275 else
12276 {
12277 printf_unfiltered (_("no syscall record support\n"));
12278 return -1;
12279 }
12280 }
12281
12282 /* B (1), conditional branch is automatically taken care in process_record,
12283 as PC is saved there. */
12284
12285 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12286 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12287 record_buf_mem);
12288
12289 return ret;
12290}
12291
12292/* Handling opcode 111 insns. */
12293
12294static int
12295thumb_record_branch (insn_decode_record *thumb_insn_r)
12296{
12297 uint32_t record_buf[8];
12298 uint32_t bits_h = 0;
12299
12300 bits_h = bits (thumb_insn_r->arm_insn, 11, 12);
12301
12302 if (2 == bits_h || 3 == bits_h)
12303 {
12304 /* BL */
12305 record_buf[0] = ARM_LR_REGNUM;
12306 thumb_insn_r->reg_rec_count = 1;
12307 }
12308 else if (1 == bits_h)
12309 {
12310 /* BLX(1). */
12311 record_buf[0] = ARM_PS_REGNUM;
12312 record_buf[1] = ARM_LR_REGNUM;
12313 thumb_insn_r->reg_rec_count = 2;
12314 }
12315
12316 /* B(2) is automatically taken care in process_record, as PC is
12317 saved there. */
12318
12319 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12320
12321 return 0;
12322}
12323
c6ec2b30
OJ
12324/* Handler for thumb2 load/store multiple instructions. */
12325
12326static int
12327thumb2_record_ld_st_multiple (insn_decode_record *thumb2_insn_r)
12328{
12329 struct regcache *reg_cache = thumb2_insn_r->regcache;
12330
12331 uint32_t reg_rn, op;
12332 uint32_t register_bits = 0, register_count = 0;
12333 uint32_t index = 0, start_address = 0;
12334 uint32_t record_buf[24], record_buf_mem[48];
12335
12336 ULONGEST u_regval = 0;
12337
12338 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12339 op = bits (thumb2_insn_r->arm_insn, 23, 24);
12340
12341 if (0 == op || 3 == op)
12342 {
12343 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12344 {
12345 /* Handle RFE instruction. */
12346 record_buf[0] = ARM_PS_REGNUM;
12347 thumb2_insn_r->reg_rec_count = 1;
12348 }
12349 else
12350 {
12351 /* Handle SRS instruction after reading banked SP. */
12352 return arm_record_unsupported_insn (thumb2_insn_r);
12353 }
12354 }
12355 else if (1 == op || 2 == op)
12356 {
12357 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12358 {
12359 /* Handle LDM/LDMIA/LDMFD and LDMDB/LDMEA instructions. */
12360 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
12361 while (register_bits)
12362 {
12363 if (register_bits & 0x00000001)
12364 record_buf[index++] = register_count;
12365
12366 register_count++;
12367 register_bits = register_bits >> 1;
12368 }
12369 record_buf[index++] = reg_rn;
12370 record_buf[index++] = ARM_PS_REGNUM;
12371 thumb2_insn_r->reg_rec_count = index;
12372 }
12373 else
12374 {
12375 /* Handle STM/STMIA/STMEA and STMDB/STMFD. */
12376 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
12377 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12378 while (register_bits)
12379 {
12380 if (register_bits & 0x00000001)
12381 register_count++;
12382
12383 register_bits = register_bits >> 1;
12384 }
12385
12386 if (1 == op)
12387 {
12388 /* Start address calculation for LDMDB/LDMEA. */
12389 start_address = u_regval;
12390 }
12391 else if (2 == op)
12392 {
12393 /* Start address calculation for LDMDB/LDMEA. */
12394 start_address = u_regval - register_count * 4;
12395 }
12396
12397 thumb2_insn_r->mem_rec_count = register_count;
12398 while (register_count)
12399 {
12400 record_buf_mem[register_count * 2 - 1] = start_address;
12401 record_buf_mem[register_count * 2 - 2] = 4;
12402 start_address = start_address + 4;
12403 register_count--;
12404 }
12405 record_buf[0] = reg_rn;
12406 record_buf[1] = ARM_PS_REGNUM;
12407 thumb2_insn_r->reg_rec_count = 2;
12408 }
12409 }
12410
12411 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
12412 record_buf_mem);
12413 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12414 record_buf);
12415 return ARM_RECORD_SUCCESS;
12416}
12417
12418/* Handler for thumb2 load/store (dual/exclusive) and table branch
12419 instructions. */
12420
12421static int
12422thumb2_record_ld_st_dual_ex_tbb (insn_decode_record *thumb2_insn_r)
12423{
12424 struct regcache *reg_cache = thumb2_insn_r->regcache;
12425
12426 uint32_t reg_rd, reg_rn, offset_imm;
12427 uint32_t reg_dest1, reg_dest2;
12428 uint32_t address, offset_addr;
12429 uint32_t record_buf[8], record_buf_mem[8];
12430 uint32_t op1, op2, op3;
c6ec2b30
OJ
12431
12432 ULONGEST u_regval[2];
12433
12434 op1 = bits (thumb2_insn_r->arm_insn, 23, 24);
12435 op2 = bits (thumb2_insn_r->arm_insn, 20, 21);
12436 op3 = bits (thumb2_insn_r->arm_insn, 4, 7);
12437
12438 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12439 {
12440 if(!(1 == op1 && 1 == op2 && (0 == op3 || 1 == op3)))
12441 {
12442 reg_dest1 = bits (thumb2_insn_r->arm_insn, 12, 15);
12443 record_buf[0] = reg_dest1;
12444 record_buf[1] = ARM_PS_REGNUM;
12445 thumb2_insn_r->reg_rec_count = 2;
12446 }
12447
12448 if (3 == op2 || (op1 & 2) || (1 == op1 && 1 == op2 && 7 == op3))
12449 {
12450 reg_dest2 = bits (thumb2_insn_r->arm_insn, 8, 11);
12451 record_buf[2] = reg_dest2;
12452 thumb2_insn_r->reg_rec_count = 3;
12453 }
12454 }
12455 else
12456 {
12457 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12458 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
12459
12460 if (0 == op1 && 0 == op2)
12461 {
12462 /* Handle STREX. */
12463 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
12464 address = u_regval[0] + (offset_imm * 4);
12465 record_buf_mem[0] = 4;
12466 record_buf_mem[1] = address;
12467 thumb2_insn_r->mem_rec_count = 1;
12468 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
12469 record_buf[0] = reg_rd;
12470 thumb2_insn_r->reg_rec_count = 1;
12471 }
12472 else if (1 == op1 && 0 == op2)
12473 {
12474 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
12475 record_buf[0] = reg_rd;
12476 thumb2_insn_r->reg_rec_count = 1;
12477 address = u_regval[0];
12478 record_buf_mem[1] = address;
12479
12480 if (4 == op3)
12481 {
12482 /* Handle STREXB. */
12483 record_buf_mem[0] = 1;
12484 thumb2_insn_r->mem_rec_count = 1;
12485 }
12486 else if (5 == op3)
12487 {
12488 /* Handle STREXH. */
12489 record_buf_mem[0] = 2 ;
12490 thumb2_insn_r->mem_rec_count = 1;
12491 }
12492 else if (7 == op3)
12493 {
12494 /* Handle STREXD. */
12495 address = u_regval[0];
12496 record_buf_mem[0] = 4;
12497 record_buf_mem[2] = 4;
12498 record_buf_mem[3] = address + 4;
12499 thumb2_insn_r->mem_rec_count = 2;
12500 }
12501 }
12502 else
12503 {
12504 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
12505
12506 if (bit (thumb2_insn_r->arm_insn, 24))
12507 {
12508 if (bit (thumb2_insn_r->arm_insn, 23))
12509 offset_addr = u_regval[0] + (offset_imm * 4);
12510 else
12511 offset_addr = u_regval[0] - (offset_imm * 4);
12512
12513 address = offset_addr;
12514 }
12515 else
12516 address = u_regval[0];
12517
12518 record_buf_mem[0] = 4;
12519 record_buf_mem[1] = address;
12520 record_buf_mem[2] = 4;
12521 record_buf_mem[3] = address + 4;
12522 thumb2_insn_r->mem_rec_count = 2;
12523 record_buf[0] = reg_rn;
12524 thumb2_insn_r->reg_rec_count = 1;
12525 }
12526 }
12527
12528 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12529 record_buf);
12530 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
12531 record_buf_mem);
12532 return ARM_RECORD_SUCCESS;
12533}
12534
12535/* Handler for thumb2 data processing (shift register and modified immediate)
12536 instructions. */
12537
12538static int
12539thumb2_record_data_proc_sreg_mimm (insn_decode_record *thumb2_insn_r)
12540{
12541 uint32_t reg_rd, op;
12542 uint32_t record_buf[8];
12543
12544 op = bits (thumb2_insn_r->arm_insn, 21, 24);
12545 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
12546
12547 if ((0 == op || 4 == op || 8 == op || 13 == op) && 15 == reg_rd)
12548 {
12549 record_buf[0] = ARM_PS_REGNUM;
12550 thumb2_insn_r->reg_rec_count = 1;
12551 }
12552 else
12553 {
12554 record_buf[0] = reg_rd;
12555 record_buf[1] = ARM_PS_REGNUM;
12556 thumb2_insn_r->reg_rec_count = 2;
12557 }
12558
12559 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12560 record_buf);
12561 return ARM_RECORD_SUCCESS;
12562}
12563
12564/* Generic handler for thumb2 instructions which effect destination and PS
12565 registers. */
12566
12567static int
12568thumb2_record_ps_dest_generic (insn_decode_record *thumb2_insn_r)
12569{
12570 uint32_t reg_rd;
12571 uint32_t record_buf[8];
12572
12573 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
12574
12575 record_buf[0] = reg_rd;
12576 record_buf[1] = ARM_PS_REGNUM;
12577 thumb2_insn_r->reg_rec_count = 2;
12578
12579 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12580 record_buf);
12581 return ARM_RECORD_SUCCESS;
12582}
12583
12584/* Handler for thumb2 branch and miscellaneous control instructions. */
12585
12586static int
12587thumb2_record_branch_misc_cntrl (insn_decode_record *thumb2_insn_r)
12588{
12589 uint32_t op, op1, op2;
12590 uint32_t record_buf[8];
12591
12592 op = bits (thumb2_insn_r->arm_insn, 20, 26);
12593 op1 = bits (thumb2_insn_r->arm_insn, 12, 14);
12594 op2 = bits (thumb2_insn_r->arm_insn, 8, 11);
12595
12596 /* Handle MSR insn. */
12597 if (!(op1 & 0x2) && 0x38 == op)
12598 {
12599 if (!(op2 & 0x3))
12600 {
12601 /* CPSR is going to be changed. */
12602 record_buf[0] = ARM_PS_REGNUM;
12603 thumb2_insn_r->reg_rec_count = 1;
12604 }
12605 else
12606 {
12607 arm_record_unsupported_insn(thumb2_insn_r);
12608 return -1;
12609 }
12610 }
12611 else if (4 == (op1 & 0x5) || 5 == (op1 & 0x5))
12612 {
12613 /* BLX. */
12614 record_buf[0] = ARM_PS_REGNUM;
12615 record_buf[1] = ARM_LR_REGNUM;
12616 thumb2_insn_r->reg_rec_count = 2;
12617 }
12618
12619 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12620 record_buf);
12621 return ARM_RECORD_SUCCESS;
12622}
12623
12624/* Handler for thumb2 store single data item instructions. */
12625
12626static int
12627thumb2_record_str_single_data (insn_decode_record *thumb2_insn_r)
12628{
12629 struct regcache *reg_cache = thumb2_insn_r->regcache;
12630
12631 uint32_t reg_rn, reg_rm, offset_imm, shift_imm;
12632 uint32_t address, offset_addr;
12633 uint32_t record_buf[8], record_buf_mem[8];
12634 uint32_t op1, op2;
12635
12636 ULONGEST u_regval[2];
12637
12638 op1 = bits (thumb2_insn_r->arm_insn, 21, 23);
12639 op2 = bits (thumb2_insn_r->arm_insn, 6, 11);
12640 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12641 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
12642
12643 if (bit (thumb2_insn_r->arm_insn, 23))
12644 {
12645 /* T2 encoding. */
12646 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 11);
12647 offset_addr = u_regval[0] + offset_imm;
12648 address = offset_addr;
12649 }
12650 else
12651 {
12652 /* T3 encoding. */
12653 if ((0 == op1 || 1 == op1 || 2 == op1) && !(op2 & 0x20))
12654 {
12655 /* Handle STRB (register). */
12656 reg_rm = bits (thumb2_insn_r->arm_insn, 0, 3);
12657 regcache_raw_read_unsigned (reg_cache, reg_rm, &u_regval[1]);
12658 shift_imm = bits (thumb2_insn_r->arm_insn, 4, 5);
12659 offset_addr = u_regval[1] << shift_imm;
12660 address = u_regval[0] + offset_addr;
12661 }
12662 else
12663 {
12664 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
12665 if (bit (thumb2_insn_r->arm_insn, 10))
12666 {
12667 if (bit (thumb2_insn_r->arm_insn, 9))
12668 offset_addr = u_regval[0] + offset_imm;
12669 else
12670 offset_addr = u_regval[0] - offset_imm;
12671
12672 address = offset_addr;
12673 }
12674 else
12675 address = u_regval[0];
12676 }
12677 }
12678
12679 switch (op1)
12680 {
12681 /* Store byte instructions. */
12682 case 4:
12683 case 0:
12684 record_buf_mem[0] = 1;
12685 break;
12686 /* Store half word instructions. */
12687 case 1:
12688 case 5:
12689 record_buf_mem[0] = 2;
12690 break;
12691 /* Store word instructions. */
12692 case 2:
12693 case 6:
12694 record_buf_mem[0] = 4;
12695 break;
12696
12697 default:
12698 gdb_assert_not_reached ("no decoding pattern found");
12699 break;
12700 }
12701
12702 record_buf_mem[1] = address;
12703 thumb2_insn_r->mem_rec_count = 1;
12704 record_buf[0] = reg_rn;
12705 thumb2_insn_r->reg_rec_count = 1;
12706
12707 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12708 record_buf);
12709 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
12710 record_buf_mem);
12711 return ARM_RECORD_SUCCESS;
12712}
12713
12714/* Handler for thumb2 load memory hints instructions. */
12715
12716static int
12717thumb2_record_ld_mem_hints (insn_decode_record *thumb2_insn_r)
12718{
12719 uint32_t record_buf[8];
12720 uint32_t reg_rt, reg_rn;
12721
12722 reg_rt = bits (thumb2_insn_r->arm_insn, 12, 15);
12723 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12724
12725 if (ARM_PC_REGNUM != reg_rt)
12726 {
12727 record_buf[0] = reg_rt;
12728 record_buf[1] = reg_rn;
12729 record_buf[2] = ARM_PS_REGNUM;
12730 thumb2_insn_r->reg_rec_count = 3;
12731
12732 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12733 record_buf);
12734 return ARM_RECORD_SUCCESS;
12735 }
12736
12737 return ARM_RECORD_FAILURE;
12738}
12739
12740/* Handler for thumb2 load word instructions. */
12741
12742static int
12743thumb2_record_ld_word (insn_decode_record *thumb2_insn_r)
12744{
c6ec2b30
OJ
12745 uint32_t record_buf[8];
12746
12747 record_buf[0] = bits (thumb2_insn_r->arm_insn, 12, 15);
12748 record_buf[1] = ARM_PS_REGNUM;
12749 thumb2_insn_r->reg_rec_count = 2;
12750
12751 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12752 record_buf);
12753 return ARM_RECORD_SUCCESS;
12754}
12755
12756/* Handler for thumb2 long multiply, long multiply accumulate, and
12757 divide instructions. */
12758
12759static int
12760thumb2_record_lmul_lmla_div (insn_decode_record *thumb2_insn_r)
12761{
12762 uint32_t opcode1 = 0, opcode2 = 0;
12763 uint32_t record_buf[8];
c6ec2b30
OJ
12764
12765 opcode1 = bits (thumb2_insn_r->arm_insn, 20, 22);
12766 opcode2 = bits (thumb2_insn_r->arm_insn, 4, 7);
12767
12768 if (0 == opcode1 || 2 == opcode1 || (opcode1 >= 4 && opcode1 <= 6))
12769 {
12770 /* Handle SMULL, UMULL, SMULAL. */
12771 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
12772 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
12773 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
12774 record_buf[2] = ARM_PS_REGNUM;
12775 thumb2_insn_r->reg_rec_count = 3;
12776 }
12777 else if (1 == opcode1 || 3 == opcode2)
12778 {
12779 /* Handle SDIV and UDIV. */
12780 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
12781 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
12782 record_buf[2] = ARM_PS_REGNUM;
12783 thumb2_insn_r->reg_rec_count = 3;
12784 }
12785 else
12786 return ARM_RECORD_FAILURE;
12787
12788 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12789 record_buf);
12790 return ARM_RECORD_SUCCESS;
12791}
12792
60cc5e93
OJ
12793/* Record handler for thumb32 coprocessor instructions. */
12794
12795static int
12796thumb2_record_coproc_insn (insn_decode_record *thumb2_insn_r)
12797{
12798 if (bit (thumb2_insn_r->arm_insn, 25))
12799 return arm_record_coproc_data_proc (thumb2_insn_r);
12800 else
12801 return arm_record_asimd_vfp_coproc (thumb2_insn_r);
12802}
12803
1e1b6563
OJ
12804/* Record handler for advance SIMD structure load/store instructions. */
12805
12806static int
12807thumb2_record_asimd_struct_ld_st (insn_decode_record *thumb2_insn_r)
12808{
12809 struct regcache *reg_cache = thumb2_insn_r->regcache;
12810 uint32_t l_bit, a_bit, b_bits;
12811 uint32_t record_buf[128], record_buf_mem[128];
bec2ab5a 12812 uint32_t reg_rn, reg_vd, address, f_elem;
1e1b6563
OJ
12813 uint32_t index_r = 0, index_e = 0, bf_regs = 0, index_m = 0, loop_t = 0;
12814 uint8_t f_ebytes;
12815
12816 l_bit = bit (thumb2_insn_r->arm_insn, 21);
12817 a_bit = bit (thumb2_insn_r->arm_insn, 23);
12818 b_bits = bits (thumb2_insn_r->arm_insn, 8, 11);
12819 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12820 reg_vd = bits (thumb2_insn_r->arm_insn, 12, 15);
12821 reg_vd = (bit (thumb2_insn_r->arm_insn, 22) << 4) | reg_vd;
12822 f_ebytes = (1 << bits (thumb2_insn_r->arm_insn, 6, 7));
1e1b6563
OJ
12823 f_elem = 8 / f_ebytes;
12824
12825 if (!l_bit)
12826 {
12827 ULONGEST u_regval = 0;
12828 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12829 address = u_regval;
12830
12831 if (!a_bit)
12832 {
12833 /* Handle VST1. */
12834 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
12835 {
12836 if (b_bits == 0x07)
12837 bf_regs = 1;
12838 else if (b_bits == 0x0a)
12839 bf_regs = 2;
12840 else if (b_bits == 0x06)
12841 bf_regs = 3;
12842 else if (b_bits == 0x02)
12843 bf_regs = 4;
12844 else
12845 bf_regs = 0;
12846
12847 for (index_r = 0; index_r < bf_regs; index_r++)
12848 {
12849 for (index_e = 0; index_e < f_elem; index_e++)
12850 {
12851 record_buf_mem[index_m++] = f_ebytes;
12852 record_buf_mem[index_m++] = address;
12853 address = address + f_ebytes;
12854 thumb2_insn_r->mem_rec_count += 1;
12855 }
12856 }
12857 }
12858 /* Handle VST2. */
12859 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
12860 {
12861 if (b_bits == 0x09 || b_bits == 0x08)
12862 bf_regs = 1;
12863 else if (b_bits == 0x03)
12864 bf_regs = 2;
12865 else
12866 bf_regs = 0;
12867
12868 for (index_r = 0; index_r < bf_regs; index_r++)
12869 for (index_e = 0; index_e < f_elem; index_e++)
12870 {
12871 for (loop_t = 0; loop_t < 2; loop_t++)
12872 {
12873 record_buf_mem[index_m++] = f_ebytes;
12874 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
12875 thumb2_insn_r->mem_rec_count += 1;
12876 }
12877 address = address + (2 * f_ebytes);
12878 }
12879 }
12880 /* Handle VST3. */
12881 else if ((b_bits & 0x0e) == 0x04)
12882 {
12883 for (index_e = 0; index_e < f_elem; index_e++)
12884 {
12885 for (loop_t = 0; loop_t < 3; loop_t++)
12886 {
12887 record_buf_mem[index_m++] = f_ebytes;
12888 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
12889 thumb2_insn_r->mem_rec_count += 1;
12890 }
12891 address = address + (3 * f_ebytes);
12892 }
12893 }
12894 /* Handle VST4. */
12895 else if (!(b_bits & 0x0e))
12896 {
12897 for (index_e = 0; index_e < f_elem; index_e++)
12898 {
12899 for (loop_t = 0; loop_t < 4; loop_t++)
12900 {
12901 record_buf_mem[index_m++] = f_ebytes;
12902 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
12903 thumb2_insn_r->mem_rec_count += 1;
12904 }
12905 address = address + (4 * f_ebytes);
12906 }
12907 }
12908 }
12909 else
12910 {
12911 uint8_t bft_size = bits (thumb2_insn_r->arm_insn, 10, 11);
12912
12913 if (bft_size == 0x00)
12914 f_ebytes = 1;
12915 else if (bft_size == 0x01)
12916 f_ebytes = 2;
12917 else if (bft_size == 0x02)
12918 f_ebytes = 4;
12919 else
12920 f_ebytes = 0;
12921
12922 /* Handle VST1. */
12923 if (!(b_bits & 0x0b) || b_bits == 0x08)
12924 thumb2_insn_r->mem_rec_count = 1;
12925 /* Handle VST2. */
12926 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09)
12927 thumb2_insn_r->mem_rec_count = 2;
12928 /* Handle VST3. */
12929 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a)
12930 thumb2_insn_r->mem_rec_count = 3;
12931 /* Handle VST4. */
12932 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b)
12933 thumb2_insn_r->mem_rec_count = 4;
12934
12935 for (index_m = 0; index_m < thumb2_insn_r->mem_rec_count; index_m++)
12936 {
12937 record_buf_mem[index_m] = f_ebytes;
12938 record_buf_mem[index_m] = address + (index_m * f_ebytes);
12939 }
12940 }
12941 }
12942 else
12943 {
12944 if (!a_bit)
12945 {
12946 /* Handle VLD1. */
12947 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
12948 thumb2_insn_r->reg_rec_count = 1;
12949 /* Handle VLD2. */
12950 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
12951 thumb2_insn_r->reg_rec_count = 2;
12952 /* Handle VLD3. */
12953 else if ((b_bits & 0x0e) == 0x04)
12954 thumb2_insn_r->reg_rec_count = 3;
12955 /* Handle VLD4. */
12956 else if (!(b_bits & 0x0e))
12957 thumb2_insn_r->reg_rec_count = 4;
12958 }
12959 else
12960 {
12961 /* Handle VLD1. */
12962 if (!(b_bits & 0x0b) || b_bits == 0x08 || b_bits == 0x0c)
12963 thumb2_insn_r->reg_rec_count = 1;
12964 /* Handle VLD2. */
12965 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09 || b_bits == 0x0d)
12966 thumb2_insn_r->reg_rec_count = 2;
12967 /* Handle VLD3. */
12968 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a || b_bits == 0x0e)
12969 thumb2_insn_r->reg_rec_count = 3;
12970 /* Handle VLD4. */
12971 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b || b_bits == 0x0f)
12972 thumb2_insn_r->reg_rec_count = 4;
12973
12974 for (index_r = 0; index_r < thumb2_insn_r->reg_rec_count; index_r++)
12975 record_buf[index_r] = reg_vd + ARM_D0_REGNUM + index_r;
12976 }
12977 }
12978
12979 if (bits (thumb2_insn_r->arm_insn, 0, 3) != 15)
12980 {
12981 record_buf[index_r] = reg_rn;
12982 thumb2_insn_r->reg_rec_count += 1;
12983 }
12984
12985 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12986 record_buf);
12987 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
12988 record_buf_mem);
12989 return 0;
12990}
12991
c6ec2b30
OJ
12992/* Decodes thumb2 instruction type and invokes its record handler. */
12993
12994static unsigned int
12995thumb2_record_decode_insn_handler (insn_decode_record *thumb2_insn_r)
12996{
12997 uint32_t op, op1, op2;
12998
12999 op = bit (thumb2_insn_r->arm_insn, 15);
13000 op1 = bits (thumb2_insn_r->arm_insn, 27, 28);
13001 op2 = bits (thumb2_insn_r->arm_insn, 20, 26);
13002
13003 if (op1 == 0x01)
13004 {
13005 if (!(op2 & 0x64 ))
13006 {
13007 /* Load/store multiple instruction. */
13008 return thumb2_record_ld_st_multiple (thumb2_insn_r);
13009 }
b121eeb9 13010 else if ((op2 & 0x64) == 0x4)
c6ec2b30
OJ
13011 {
13012 /* Load/store (dual/exclusive) and table branch instruction. */
13013 return thumb2_record_ld_st_dual_ex_tbb (thumb2_insn_r);
13014 }
b121eeb9 13015 else if ((op2 & 0x60) == 0x20)
c6ec2b30
OJ
13016 {
13017 /* Data-processing (shifted register). */
13018 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
13019 }
13020 else if (op2 & 0x40)
13021 {
13022 /* Co-processor instructions. */
60cc5e93 13023 return thumb2_record_coproc_insn (thumb2_insn_r);
c6ec2b30
OJ
13024 }
13025 }
13026 else if (op1 == 0x02)
13027 {
13028 if (op)
13029 {
13030 /* Branches and miscellaneous control instructions. */
13031 return thumb2_record_branch_misc_cntrl (thumb2_insn_r);
13032 }
13033 else if (op2 & 0x20)
13034 {
13035 /* Data-processing (plain binary immediate) instruction. */
13036 return thumb2_record_ps_dest_generic (thumb2_insn_r);
13037 }
13038 else
13039 {
13040 /* Data-processing (modified immediate). */
13041 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
13042 }
13043 }
13044 else if (op1 == 0x03)
13045 {
13046 if (!(op2 & 0x71 ))
13047 {
13048 /* Store single data item. */
13049 return thumb2_record_str_single_data (thumb2_insn_r);
13050 }
13051 else if (!((op2 & 0x71) ^ 0x10))
13052 {
13053 /* Advanced SIMD or structure load/store instructions. */
1e1b6563 13054 return thumb2_record_asimd_struct_ld_st (thumb2_insn_r);
c6ec2b30
OJ
13055 }
13056 else if (!((op2 & 0x67) ^ 0x01))
13057 {
13058 /* Load byte, memory hints instruction. */
13059 return thumb2_record_ld_mem_hints (thumb2_insn_r);
13060 }
13061 else if (!((op2 & 0x67) ^ 0x03))
13062 {
13063 /* Load halfword, memory hints instruction. */
13064 return thumb2_record_ld_mem_hints (thumb2_insn_r);
13065 }
13066 else if (!((op2 & 0x67) ^ 0x05))
13067 {
13068 /* Load word instruction. */
13069 return thumb2_record_ld_word (thumb2_insn_r);
13070 }
13071 else if (!((op2 & 0x70) ^ 0x20))
13072 {
13073 /* Data-processing (register) instruction. */
13074 return thumb2_record_ps_dest_generic (thumb2_insn_r);
13075 }
13076 else if (!((op2 & 0x78) ^ 0x30))
13077 {
13078 /* Multiply, multiply accumulate, abs diff instruction. */
13079 return thumb2_record_ps_dest_generic (thumb2_insn_r);
13080 }
13081 else if (!((op2 & 0x78) ^ 0x38))
13082 {
13083 /* Long multiply, long multiply accumulate, and divide. */
13084 return thumb2_record_lmul_lmla_div (thumb2_insn_r);
13085 }
13086 else if (op2 & 0x40)
13087 {
13088 /* Co-processor instructions. */
60cc5e93 13089 return thumb2_record_coproc_insn (thumb2_insn_r);
c6ec2b30
OJ
13090 }
13091 }
13092
13093 return -1;
13094}
72508ac0 13095
ffdbe864 13096namespace {
728a7913
YQ
13097/* Abstract memory reader. */
13098
13099class abstract_memory_reader
13100{
13101public:
13102 /* Read LEN bytes of target memory at address MEMADDR, placing the
13103 results in GDB's memory at BUF. Return true on success. */
13104
13105 virtual bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) = 0;
13106};
13107
13108/* Instruction reader from real target. */
13109
13110class instruction_reader : public abstract_memory_reader
13111{
13112 public:
632e107b 13113 bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) override
728a7913
YQ
13114 {
13115 if (target_read_memory (memaddr, buf, len))
13116 return false;
13117 else
13118 return true;
13119 }
13120};
13121
ffdbe864
YQ
13122} // namespace
13123
72508ac0 13124/* Extracts arm/thumb/thumb2 insn depending on the size, and returns 0 on success
85102364 13125and positive val on failure. */
72508ac0
PO
13126
13127static int
728a7913
YQ
13128extract_arm_insn (abstract_memory_reader& reader,
13129 insn_decode_record *insn_record, uint32_t insn_size)
72508ac0
PO
13130{
13131 gdb_byte buf[insn_size];
13132
13133 memset (&buf[0], 0, insn_size);
13134
728a7913 13135 if (!reader.read (insn_record->this_addr, buf, insn_size))
72508ac0
PO
13136 return 1;
13137 insn_record->arm_insn = (uint32_t) extract_unsigned_integer (&buf[0],
13138 insn_size,
2959fed9 13139 gdbarch_byte_order_for_code (insn_record->gdbarch));
72508ac0
PO
13140 return 0;
13141}
13142
13143typedef int (*sti_arm_hdl_fp_t) (insn_decode_record*);
13144
13145/* Decode arm/thumb insn depending on condition cods and opcodes; and
13146 dispatch it. */
13147
13148static int
728a7913
YQ
13149decode_insn (abstract_memory_reader &reader, insn_decode_record *arm_record,
13150 record_type_t record_type, uint32_t insn_size)
72508ac0
PO
13151{
13152
01e57735
YQ
13153 /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm
13154 instruction. */
0fa9c223 13155 static const sti_arm_hdl_fp_t arm_handle_insn[8] =
72508ac0
PO
13156 {
13157 arm_record_data_proc_misc_ld_str, /* 000. */
13158 arm_record_data_proc_imm, /* 001. */
13159 arm_record_ld_st_imm_offset, /* 010. */
13160 arm_record_ld_st_reg_offset, /* 011. */
13161 arm_record_ld_st_multiple, /* 100. */
13162 arm_record_b_bl, /* 101. */
60cc5e93 13163 arm_record_asimd_vfp_coproc, /* 110. */
72508ac0
PO
13164 arm_record_coproc_data_proc /* 111. */
13165 };
13166
01e57735
YQ
13167 /* (Starting from numerical 0); bits 13,14,15 decodes type of thumb
13168 instruction. */
0fa9c223 13169 static const sti_arm_hdl_fp_t thumb_handle_insn[8] =
72508ac0
PO
13170 { \
13171 thumb_record_shift_add_sub, /* 000. */
13172 thumb_record_add_sub_cmp_mov, /* 001. */
13173 thumb_record_ld_st_reg_offset, /* 010. */
13174 thumb_record_ld_st_imm_offset, /* 011. */
13175 thumb_record_ld_st_stack, /* 100. */
13176 thumb_record_misc, /* 101. */
13177 thumb_record_ldm_stm_swi, /* 110. */
13178 thumb_record_branch /* 111. */
13179 };
13180
13181 uint32_t ret = 0; /* return value: negative:failure 0:success. */
13182 uint32_t insn_id = 0;
13183
728a7913 13184 if (extract_arm_insn (reader, arm_record, insn_size))
72508ac0
PO
13185 {
13186 if (record_debug)
01e57735
YQ
13187 {
13188 printf_unfiltered (_("Process record: error reading memory at "
13189 "addr %s len = %d.\n"),
13190 paddress (arm_record->gdbarch,
13191 arm_record->this_addr), insn_size);
13192 }
72508ac0
PO
13193 return -1;
13194 }
13195 else if (ARM_RECORD == record_type)
13196 {
13197 arm_record->cond = bits (arm_record->arm_insn, 28, 31);
13198 insn_id = bits (arm_record->arm_insn, 25, 27);
ca92db2d
YQ
13199
13200 if (arm_record->cond == 0xf)
13201 ret = arm_record_extension_space (arm_record);
13202 else
01e57735 13203 {
ca92db2d
YQ
13204 /* If this insn has fallen into extension space
13205 then we need not decode it anymore. */
01e57735
YQ
13206 ret = arm_handle_insn[insn_id] (arm_record);
13207 }
ca92db2d
YQ
13208 if (ret != ARM_RECORD_SUCCESS)
13209 {
13210 arm_record_unsupported_insn (arm_record);
13211 ret = -1;
13212 }
72508ac0
PO
13213 }
13214 else if (THUMB_RECORD == record_type)
13215 {
13216 /* As thumb does not have condition codes, we set negative. */
13217 arm_record->cond = -1;
13218 insn_id = bits (arm_record->arm_insn, 13, 15);
13219 ret = thumb_handle_insn[insn_id] (arm_record);
ca92db2d
YQ
13220 if (ret != ARM_RECORD_SUCCESS)
13221 {
13222 arm_record_unsupported_insn (arm_record);
13223 ret = -1;
13224 }
72508ac0
PO
13225 }
13226 else if (THUMB2_RECORD == record_type)
13227 {
c6ec2b30
OJ
13228 /* As thumb does not have condition codes, we set negative. */
13229 arm_record->cond = -1;
13230
13231 /* Swap first half of 32bit thumb instruction with second half. */
13232 arm_record->arm_insn
01e57735 13233 = (arm_record->arm_insn >> 16) | (arm_record->arm_insn << 16);
c6ec2b30 13234
ca92db2d 13235 ret = thumb2_record_decode_insn_handler (arm_record);
c6ec2b30 13236
ca92db2d 13237 if (ret != ARM_RECORD_SUCCESS)
01e57735
YQ
13238 {
13239 arm_record_unsupported_insn (arm_record);
13240 ret = -1;
13241 }
72508ac0
PO
13242 }
13243 else
13244 {
13245 /* Throw assertion. */
13246 gdb_assert_not_reached ("not a valid instruction, could not decode");
13247 }
13248
13249 return ret;
13250}
13251
b121eeb9
YQ
13252#if GDB_SELF_TEST
13253namespace selftests {
13254
13255/* Provide both 16-bit and 32-bit thumb instructions. */
13256
13257class instruction_reader_thumb : public abstract_memory_reader
13258{
13259public:
13260 template<size_t SIZE>
13261 instruction_reader_thumb (enum bfd_endian endian,
13262 const uint16_t (&insns)[SIZE])
13263 : m_endian (endian), m_insns (insns), m_insns_size (SIZE)
13264 {}
13265
632e107b 13266 bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) override
b121eeb9
YQ
13267 {
13268 SELF_CHECK (len == 4 || len == 2);
13269 SELF_CHECK (memaddr % 2 == 0);
13270 SELF_CHECK ((memaddr / 2) < m_insns_size);
13271
13272 store_unsigned_integer (buf, 2, m_endian, m_insns[memaddr / 2]);
13273 if (len == 4)
13274 {
13275 store_unsigned_integer (&buf[2], 2, m_endian,
13276 m_insns[memaddr / 2 + 1]);
13277 }
13278 return true;
13279 }
13280
13281private:
13282 enum bfd_endian m_endian;
13283 const uint16_t *m_insns;
13284 size_t m_insns_size;
13285};
13286
13287static void
13288arm_record_test (void)
13289{
13290 struct gdbarch_info info;
13291 gdbarch_info_init (&info);
13292 info.bfd_arch_info = bfd_scan_arch ("arm");
13293
13294 struct gdbarch *gdbarch = gdbarch_find_by_info (info);
13295
13296 SELF_CHECK (gdbarch != NULL);
13297
13298 /* 16-bit Thumb instructions. */
13299 {
13300 insn_decode_record arm_record;
13301
13302 memset (&arm_record, 0, sizeof (insn_decode_record));
13303 arm_record.gdbarch = gdbarch;
13304
13305 static const uint16_t insns[] = {
13306 /* db b2 uxtb r3, r3 */
13307 0xb2db,
13308 /* cd 58 ldr r5, [r1, r3] */
13309 0x58cd,
13310 };
13311
13312 enum bfd_endian endian = gdbarch_byte_order_for_code (arm_record.gdbarch);
13313 instruction_reader_thumb reader (endian, insns);
13314 int ret = decode_insn (reader, &arm_record, THUMB_RECORD,
13315 THUMB_INSN_SIZE_BYTES);
13316
13317 SELF_CHECK (ret == 0);
13318 SELF_CHECK (arm_record.mem_rec_count == 0);
13319 SELF_CHECK (arm_record.reg_rec_count == 1);
13320 SELF_CHECK (arm_record.arm_regs[0] == 3);
13321
13322 arm_record.this_addr += 2;
13323 ret = decode_insn (reader, &arm_record, THUMB_RECORD,
13324 THUMB_INSN_SIZE_BYTES);
13325
13326 SELF_CHECK (ret == 0);
13327 SELF_CHECK (arm_record.mem_rec_count == 0);
13328 SELF_CHECK (arm_record.reg_rec_count == 1);
13329 SELF_CHECK (arm_record.arm_regs[0] == 5);
13330 }
13331
13332 /* 32-bit Thumb-2 instructions. */
13333 {
13334 insn_decode_record arm_record;
13335
13336 memset (&arm_record, 0, sizeof (insn_decode_record));
13337 arm_record.gdbarch = gdbarch;
13338
13339 static const uint16_t insns[] = {
13340 /* 1d ee 70 7f mrc 15, 0, r7, cr13, cr0, {3} */
13341 0xee1d, 0x7f70,
13342 };
13343
13344 enum bfd_endian endian = gdbarch_byte_order_for_code (arm_record.gdbarch);
13345 instruction_reader_thumb reader (endian, insns);
13346 int ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
13347 THUMB2_INSN_SIZE_BYTES);
13348
13349 SELF_CHECK (ret == 0);
13350 SELF_CHECK (arm_record.mem_rec_count == 0);
13351 SELF_CHECK (arm_record.reg_rec_count == 1);
13352 SELF_CHECK (arm_record.arm_regs[0] == 7);
13353 }
13354}
13355} // namespace selftests
13356#endif /* GDB_SELF_TEST */
72508ac0
PO
13357
13358/* Cleans up local record registers and memory allocations. */
13359
13360static void
13361deallocate_reg_mem (insn_decode_record *record)
13362{
13363 xfree (record->arm_regs);
13364 xfree (record->arm_mems);
13365}
13366
13367
01e57735 13368/* Parse the current instruction and record the values of the registers and
72508ac0
PO
13369 memory that will be changed in current instruction to record_arch_list".
13370 Return -1 if something is wrong. */
13371
13372int
01e57735
YQ
13373arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
13374 CORE_ADDR insn_addr)
72508ac0
PO
13375{
13376
72508ac0
PO
13377 uint32_t no_of_rec = 0;
13378 uint32_t ret = 0; /* return value: -1:record failure ; 0:success */
13379 ULONGEST t_bit = 0, insn_id = 0;
13380
13381 ULONGEST u_regval = 0;
13382
13383 insn_decode_record arm_record;
13384
13385 memset (&arm_record, 0, sizeof (insn_decode_record));
13386 arm_record.regcache = regcache;
13387 arm_record.this_addr = insn_addr;
13388 arm_record.gdbarch = gdbarch;
13389
13390
13391 if (record_debug > 1)
13392 {
13393 fprintf_unfiltered (gdb_stdlog, "Process record: arm_process_record "
01e57735 13394 "addr = %s\n",
72508ac0
PO
13395 paddress (gdbarch, arm_record.this_addr));
13396 }
13397
728a7913
YQ
13398 instruction_reader reader;
13399 if (extract_arm_insn (reader, &arm_record, 2))
72508ac0
PO
13400 {
13401 if (record_debug)
01e57735
YQ
13402 {
13403 printf_unfiltered (_("Process record: error reading memory at "
13404 "addr %s len = %d.\n"),
13405 paddress (arm_record.gdbarch,
13406 arm_record.this_addr), 2);
13407 }
72508ac0
PO
13408 return -1;
13409 }
13410
13411 /* Check the insn, whether it is thumb or arm one. */
13412
13413 t_bit = arm_psr_thumb_bit (arm_record.gdbarch);
13414 regcache_raw_read_unsigned (arm_record.regcache, ARM_PS_REGNUM, &u_regval);
13415
13416
13417 if (!(u_regval & t_bit))
13418 {
13419 /* We are decoding arm insn. */
728a7913 13420 ret = decode_insn (reader, &arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES);
72508ac0
PO
13421 }
13422 else
13423 {
13424 insn_id = bits (arm_record.arm_insn, 11, 15);
13425 /* is it thumb2 insn? */
13426 if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id))
01e57735 13427 {
728a7913 13428 ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
01e57735
YQ
13429 THUMB2_INSN_SIZE_BYTES);
13430 }
72508ac0 13431 else
01e57735
YQ
13432 {
13433 /* We are decoding thumb insn. */
728a7913
YQ
13434 ret = decode_insn (reader, &arm_record, THUMB_RECORD,
13435 THUMB_INSN_SIZE_BYTES);
01e57735 13436 }
72508ac0
PO
13437 }
13438
13439 if (0 == ret)
13440 {
13441 /* Record registers. */
25ea693b 13442 record_full_arch_list_add_reg (arm_record.regcache, ARM_PC_REGNUM);
72508ac0 13443 if (arm_record.arm_regs)
01e57735
YQ
13444 {
13445 for (no_of_rec = 0; no_of_rec < arm_record.reg_rec_count; no_of_rec++)
13446 {
13447 if (record_full_arch_list_add_reg
25ea693b 13448 (arm_record.regcache , arm_record.arm_regs[no_of_rec]))
01e57735
YQ
13449 ret = -1;
13450 }
13451 }
72508ac0
PO
13452 /* Record memories. */
13453 if (arm_record.arm_mems)
01e57735
YQ
13454 {
13455 for (no_of_rec = 0; no_of_rec < arm_record.mem_rec_count; no_of_rec++)
13456 {
13457 if (record_full_arch_list_add_mem
13458 ((CORE_ADDR)arm_record.arm_mems[no_of_rec].addr,
25ea693b 13459 arm_record.arm_mems[no_of_rec].len))
01e57735
YQ
13460 ret = -1;
13461 }
13462 }
72508ac0 13463
25ea693b 13464 if (record_full_arch_list_add_end ())
01e57735 13465 ret = -1;
72508ac0
PO
13466 }
13467
13468
13469 deallocate_reg_mem (&arm_record);
13470
13471 return ret;
13472}
d105cce5
AH
13473
13474/* See arm-tdep.h. */
13475
13476const target_desc *
13477arm_read_description (arm_fp_type fp_type)
13478{
13479 struct target_desc *tdesc = tdesc_arm_list[fp_type];
13480
13481 if (tdesc == nullptr)
13482 {
13483 tdesc = arm_create_target_description (fp_type);
13484 tdesc_arm_list[fp_type] = tdesc;
13485 }
13486
13487 return tdesc;
13488}
13489
13490/* See arm-tdep.h. */
13491
13492const target_desc *
13493arm_read_mprofile_description (arm_m_profile_type m_type)
13494{
13495 struct target_desc *tdesc = tdesc_arm_mprofile_list[m_type];
13496
13497 if (tdesc == nullptr)
13498 {
13499 tdesc = arm_create_mprofile_target_description (m_type);
13500 tdesc_arm_mprofile_list[m_type] = tdesc;
13501 }
13502
13503 return tdesc;
13504}