Use @defvar to document gdb.pretty_printers
[deliverable/binutils-gdb.git] / gdb / arm-tdep.c
CommitLineData
ed9a39eb 1/* Common target dependent code for GDB on ARM systems.
0fd88904 2
42a4f53d 3 Copyright (C) 1988-2019 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
0963b4bd 22#include <ctype.h> /* XXX for isupper (). */
34e8f22d 23
c906108c
SS
24#include "frame.h"
25#include "inferior.h"
45741a9c 26#include "infrun.h"
c906108c
SS
27#include "gdbcmd.h"
28#include "gdbcore.h"
0963b4bd 29#include "dis-asm.h" /* For register styles. */
e47ad6c0 30#include "disasm.h"
4e052eda 31#include "regcache.h"
54483882 32#include "reggroups.h"
3b2ca824 33#include "target-float.h"
fd0407d6 34#include "value.h"
34e8f22d 35#include "arch-utils.h"
4be87837 36#include "osabi.h"
eb5492fa
DJ
37#include "frame-unwind.h"
38#include "frame-base.h"
39#include "trad-frame.h"
842e1f1e
DJ
40#include "objfiles.h"
41#include "dwarf2-frame.h"
e4c16157 42#include "gdbtypes.h"
29d73ae4 43#include "prologue-value.h"
25f8c692 44#include "remote.h"
123dc839
DJ
45#include "target-descriptions.h"
46#include "user-regs.h"
76727919 47#include "observable.h"
34e8f22d 48
8689682c 49#include "arch/arm.h"
d9311bfa 50#include "arch/arm-get-next-pcs.h"
34e8f22d 51#include "arm-tdep.h"
26216b98 52#include "gdb/sim-arm.h"
34e8f22d 53
082fc60d
RE
54#include "elf-bfd.h"
55#include "coff/internal.h"
97e03143 56#include "elf/arm.h"
c906108c 57
0747795c 58#include "common/vec.h"
26216b98 59
72508ac0 60#include "record.h"
d02ed0bb 61#include "record-full.h"
325fac50 62#include <algorithm>
72508ac0 63
0a69eedb
YQ
64#include "features/arm/arm-with-m.c"
65#include "features/arm/arm-with-m-fpa-layout.c"
66#include "features/arm/arm-with-m-vfp-d16.c"
67#include "features/arm/arm-with-iwmmxt.c"
68#include "features/arm/arm-with-vfpv2.c"
69#include "features/arm/arm-with-vfpv3.c"
70#include "features/arm/arm-with-neon.c"
9779414d 71
b121eeb9 72#if GDB_SELF_TEST
0747795c 73#include "common/selftest.h"
b121eeb9
YQ
74#endif
75
6529d2dd
AC
76static int arm_debug;
77
082fc60d
RE
78/* Macros for setting and testing a bit in a minimal symbol that marks
79 it as Thumb function. The MSB of the minimal symbol's "info" field
f594e5e9 80 is used for this purpose.
082fc60d
RE
81
82 MSYMBOL_SET_SPECIAL Actually sets the "special" bit.
f594e5e9 83 MSYMBOL_IS_SPECIAL Tests the "special" bit in a minimal symbol. */
082fc60d 84
0963b4bd 85#define MSYMBOL_SET_SPECIAL(msym) \
b887350f 86 MSYMBOL_TARGET_FLAG_1 (msym) = 1
082fc60d
RE
87
88#define MSYMBOL_IS_SPECIAL(msym) \
b887350f 89 MSYMBOL_TARGET_FLAG_1 (msym)
082fc60d 90
60c5725c
DJ
91/* Per-objfile data used for mapping symbols. */
92static const struct objfile_data *arm_objfile_data_key;
93
94struct arm_mapping_symbol
95{
96 bfd_vma value;
97 char type;
98};
99typedef struct arm_mapping_symbol arm_mapping_symbol_s;
100DEF_VEC_O(arm_mapping_symbol_s);
101
102struct arm_per_objfile
103{
104 VEC(arm_mapping_symbol_s) **section_maps;
105};
106
afd7eef0
RE
107/* The list of available "set arm ..." and "show arm ..." commands. */
108static struct cmd_list_element *setarmcmdlist = NULL;
109static struct cmd_list_element *showarmcmdlist = NULL;
110
fd50bc42
RE
111/* The type of floating-point to use. Keep this in sync with enum
112 arm_float_model, and the help string in _initialize_arm_tdep. */
40478521 113static const char *const fp_model_strings[] =
fd50bc42
RE
114{
115 "auto",
116 "softfpa",
117 "fpa",
118 "softvfp",
28e97307
DJ
119 "vfp",
120 NULL
fd50bc42
RE
121};
122
123/* A variable that can be configured by the user. */
124static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
125static const char *current_fp_model = "auto";
126
28e97307 127/* The ABI to use. Keep this in sync with arm_abi_kind. */
40478521 128static const char *const arm_abi_strings[] =
28e97307
DJ
129{
130 "auto",
131 "APCS",
132 "AAPCS",
133 NULL
134};
135
136/* A variable that can be configured by the user. */
137static enum arm_abi_kind arm_abi_global = ARM_ABI_AUTO;
138static const char *arm_abi_string = "auto";
139
0428b8f5 140/* The execution mode to assume. */
40478521 141static const char *const arm_mode_strings[] =
0428b8f5
DJ
142 {
143 "auto",
144 "arm",
68770265
MGD
145 "thumb",
146 NULL
0428b8f5
DJ
147 };
148
149static const char *arm_fallback_mode_string = "auto";
150static const char *arm_force_mode_string = "auto";
151
f32bf4a4
YQ
152/* The standard register names, and all the valid aliases for them. Note
153 that `fp', `sp' and `pc' are not added in this alias list, because they
154 have been added as builtin user registers in
155 std-regs.c:_initialize_frame_reg. */
123dc839
DJ
156static const struct
157{
158 const char *name;
159 int regnum;
160} arm_register_aliases[] = {
161 /* Basic register numbers. */
162 { "r0", 0 },
163 { "r1", 1 },
164 { "r2", 2 },
165 { "r3", 3 },
166 { "r4", 4 },
167 { "r5", 5 },
168 { "r6", 6 },
169 { "r7", 7 },
170 { "r8", 8 },
171 { "r9", 9 },
172 { "r10", 10 },
173 { "r11", 11 },
174 { "r12", 12 },
175 { "r13", 13 },
176 { "r14", 14 },
177 { "r15", 15 },
178 /* Synonyms (argument and variable registers). */
179 { "a1", 0 },
180 { "a2", 1 },
181 { "a3", 2 },
182 { "a4", 3 },
183 { "v1", 4 },
184 { "v2", 5 },
185 { "v3", 6 },
186 { "v4", 7 },
187 { "v5", 8 },
188 { "v6", 9 },
189 { "v7", 10 },
190 { "v8", 11 },
191 /* Other platform-specific names for r9. */
192 { "sb", 9 },
193 { "tr", 9 },
194 /* Special names. */
195 { "ip", 12 },
123dc839 196 { "lr", 14 },
123dc839
DJ
197 /* Names used by GCC (not listed in the ARM EABI). */
198 { "sl", 10 },
123dc839
DJ
199 /* A special name from the older ATPCS. */
200 { "wr", 7 },
201};
bc90b915 202
123dc839 203static const char *const arm_register_names[] =
da59e081
JM
204{"r0", "r1", "r2", "r3", /* 0 1 2 3 */
205 "r4", "r5", "r6", "r7", /* 4 5 6 7 */
206 "r8", "r9", "r10", "r11", /* 8 9 10 11 */
207 "r12", "sp", "lr", "pc", /* 12 13 14 15 */
208 "f0", "f1", "f2", "f3", /* 16 17 18 19 */
209 "f4", "f5", "f6", "f7", /* 20 21 22 23 */
94c30b78 210 "fps", "cpsr" }; /* 24 25 */
ed9a39eb 211
65b48a81
PB
212/* Holds the current set of options to be passed to the disassembler. */
213static char *arm_disassembler_options;
214
afd7eef0
RE
215/* Valid register name styles. */
216static const char **valid_disassembly_styles;
ed9a39eb 217
afd7eef0
RE
218/* Disassembly style to use. Default to "std" register names. */
219static const char *disassembly_style;
96baa820 220
ed9a39eb 221/* This is used to keep the bfd arch_info in sync with the disassembly
afd7eef0 222 style. */
eb4c3f4a 223static void set_disassembly_style_sfunc (const char *, int,
ed9a39eb 224 struct cmd_list_element *);
65b48a81
PB
225static void show_disassembly_style_sfunc (struct ui_file *, int,
226 struct cmd_list_element *,
227 const char *);
ed9a39eb 228
05d1431c 229static enum register_status arm_neon_quad_read (struct gdbarch *gdbarch,
849d0ba8 230 readable_regcache *regcache,
05d1431c 231 int regnum, gdb_byte *buf);
58d6951d
DJ
232static void arm_neon_quad_write (struct gdbarch *gdbarch,
233 struct regcache *regcache,
234 int regnum, const gdb_byte *buf);
235
e7cf25a8 236static CORE_ADDR
553cb527 237 arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self);
e7cf25a8
YQ
238
239
d9311bfa
AT
240/* get_next_pcs operations. */
241static struct arm_get_next_pcs_ops arm_get_next_pcs_ops = {
242 arm_get_next_pcs_read_memory_unsigned_integer,
243 arm_get_next_pcs_syscall_next_pc,
244 arm_get_next_pcs_addr_bits_remove,
ed443b61
YQ
245 arm_get_next_pcs_is_thumb,
246 NULL,
d9311bfa
AT
247};
248
9b8d791a 249struct arm_prologue_cache
c3b4394c 250{
eb5492fa
DJ
251 /* The stack pointer at the time this frame was created; i.e. the
252 caller's stack pointer when this function was called. It is used
253 to identify this frame. */
254 CORE_ADDR prev_sp;
255
4be43953
DJ
256 /* The frame base for this frame is just prev_sp - frame size.
257 FRAMESIZE is the distance from the frame pointer to the
258 initial stack pointer. */
eb5492fa 259
c3b4394c 260 int framesize;
eb5492fa
DJ
261
262 /* The register used to hold the frame pointer for this frame. */
c3b4394c 263 int framereg;
eb5492fa
DJ
264
265 /* Saved register offsets. */
266 struct trad_frame_saved_reg *saved_regs;
c3b4394c 267};
ed9a39eb 268
0d39a070
DJ
269static CORE_ADDR arm_analyze_prologue (struct gdbarch *gdbarch,
270 CORE_ADDR prologue_start,
271 CORE_ADDR prologue_end,
272 struct arm_prologue_cache *cache);
273
cca44b1b
JB
274/* Architecture version for displaced stepping. This effects the behaviour of
275 certain instructions, and really should not be hard-wired. */
276
277#define DISPLACED_STEPPING_ARCH_VERSION 5
278
94c30b78 279/* Set to true if the 32-bit mode is in use. */
c906108c
SS
280
281int arm_apcs_32 = 1;
282
9779414d
DJ
283/* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode. */
284
478fd957 285int
9779414d
DJ
286arm_psr_thumb_bit (struct gdbarch *gdbarch)
287{
288 if (gdbarch_tdep (gdbarch)->is_m)
289 return XPSR_T;
290 else
291 return CPSR_T;
292}
293
d0e59a68
AT
294/* Determine if the processor is currently executing in Thumb mode. */
295
296int
297arm_is_thumb (struct regcache *regcache)
298{
299 ULONGEST cpsr;
ac7936df 300 ULONGEST t_bit = arm_psr_thumb_bit (regcache->arch ());
d0e59a68
AT
301
302 cpsr = regcache_raw_get_unsigned (regcache, ARM_PS_REGNUM);
303
304 return (cpsr & t_bit) != 0;
305}
306
b39cc962
DJ
307/* Determine if FRAME is executing in Thumb mode. */
308
25b41d01 309int
b39cc962
DJ
310arm_frame_is_thumb (struct frame_info *frame)
311{
312 CORE_ADDR cpsr;
9779414d 313 ULONGEST t_bit = arm_psr_thumb_bit (get_frame_arch (frame));
b39cc962
DJ
314
315 /* Every ARM frame unwinder can unwind the T bit of the CPSR, either
316 directly (from a signal frame or dummy frame) or by interpreting
317 the saved LR (from a prologue or DWARF frame). So consult it and
318 trust the unwinders. */
319 cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
320
9779414d 321 return (cpsr & t_bit) != 0;
b39cc962
DJ
322}
323
60c5725c
DJ
324/* Callback for VEC_lower_bound. */
325
326static inline int
327arm_compare_mapping_symbols (const struct arm_mapping_symbol *lhs,
328 const struct arm_mapping_symbol *rhs)
329{
330 return lhs->value < rhs->value;
331}
332
f9d67f43
DJ
333/* Search for the mapping symbol covering MEMADDR. If one is found,
334 return its type. Otherwise, return 0. If START is non-NULL,
335 set *START to the location of the mapping symbol. */
c906108c 336
f9d67f43
DJ
337static char
338arm_find_mapping_symbol (CORE_ADDR memaddr, CORE_ADDR *start)
c906108c 339{
60c5725c 340 struct obj_section *sec;
0428b8f5 341
60c5725c
DJ
342 /* If there are mapping symbols, consult them. */
343 sec = find_pc_section (memaddr);
344 if (sec != NULL)
345 {
346 struct arm_per_objfile *data;
347 VEC(arm_mapping_symbol_s) *map;
aded6f54
PA
348 struct arm_mapping_symbol map_key = { memaddr - obj_section_addr (sec),
349 0 };
60c5725c
DJ
350 unsigned int idx;
351
9a3c8263
SM
352 data = (struct arm_per_objfile *) objfile_data (sec->objfile,
353 arm_objfile_data_key);
60c5725c
DJ
354 if (data != NULL)
355 {
356 map = data->section_maps[sec->the_bfd_section->index];
357 if (!VEC_empty (arm_mapping_symbol_s, map))
358 {
359 struct arm_mapping_symbol *map_sym;
360
361 idx = VEC_lower_bound (arm_mapping_symbol_s, map, &map_key,
362 arm_compare_mapping_symbols);
363
364 /* VEC_lower_bound finds the earliest ordered insertion
365 point. If the following symbol starts at this exact
366 address, we use that; otherwise, the preceding
367 mapping symbol covers this address. */
368 if (idx < VEC_length (arm_mapping_symbol_s, map))
369 {
370 map_sym = VEC_index (arm_mapping_symbol_s, map, idx);
371 if (map_sym->value == map_key.value)
f9d67f43
DJ
372 {
373 if (start)
374 *start = map_sym->value + obj_section_addr (sec);
375 return map_sym->type;
376 }
60c5725c
DJ
377 }
378
379 if (idx > 0)
380 {
381 map_sym = VEC_index (arm_mapping_symbol_s, map, idx - 1);
f9d67f43
DJ
382 if (start)
383 *start = map_sym->value + obj_section_addr (sec);
384 return map_sym->type;
60c5725c
DJ
385 }
386 }
387 }
388 }
389
f9d67f43
DJ
390 return 0;
391}
392
393/* Determine if the program counter specified in MEMADDR is in a Thumb
394 function. This function should be called for addresses unrelated to
395 any executing frame; otherwise, prefer arm_frame_is_thumb. */
396
e3039479 397int
9779414d 398arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
f9d67f43 399{
7cbd4a93 400 struct bound_minimal_symbol sym;
f9d67f43 401 char type;
cfba9872
SM
402 arm_displaced_step_closure *dsc
403 = ((arm_displaced_step_closure * )
404 get_displaced_step_closure_by_addr (memaddr));
a42244db
YQ
405
406 /* If checking the mode of displaced instruction in copy area, the mode
407 should be determined by instruction on the original address. */
408 if (dsc)
409 {
410 if (debug_displaced)
411 fprintf_unfiltered (gdb_stdlog,
412 "displaced: check mode of %.8lx instead of %.8lx\n",
413 (unsigned long) dsc->insn_addr,
414 (unsigned long) memaddr);
415 memaddr = dsc->insn_addr;
416 }
f9d67f43
DJ
417
418 /* If bit 0 of the address is set, assume this is a Thumb address. */
419 if (IS_THUMB_ADDR (memaddr))
420 return 1;
421
422 /* If the user wants to override the symbol table, let him. */
423 if (strcmp (arm_force_mode_string, "arm") == 0)
424 return 0;
425 if (strcmp (arm_force_mode_string, "thumb") == 0)
426 return 1;
427
9779414d
DJ
428 /* ARM v6-M and v7-M are always in Thumb mode. */
429 if (gdbarch_tdep (gdbarch)->is_m)
430 return 1;
431
f9d67f43
DJ
432 /* If there are mapping symbols, consult them. */
433 type = arm_find_mapping_symbol (memaddr, NULL);
434 if (type)
435 return type == 't';
436
ed9a39eb 437 /* Thumb functions have a "special" bit set in minimal symbols. */
c906108c 438 sym = lookup_minimal_symbol_by_pc (memaddr);
7cbd4a93
TT
439 if (sym.minsym)
440 return (MSYMBOL_IS_SPECIAL (sym.minsym));
0428b8f5
DJ
441
442 /* If the user wants to override the fallback mode, let them. */
443 if (strcmp (arm_fallback_mode_string, "arm") == 0)
444 return 0;
445 if (strcmp (arm_fallback_mode_string, "thumb") == 0)
446 return 1;
447
448 /* If we couldn't find any symbol, but we're talking to a running
449 target, then trust the current value of $cpsr. This lets
450 "display/i $pc" always show the correct mode (though if there is
451 a symbol table we will not reach here, so it still may not be
18819fa6 452 displayed in the mode it will be executed). */
0428b8f5 453 if (target_has_registers)
18819fa6 454 return arm_frame_is_thumb (get_current_frame ());
0428b8f5
DJ
455
456 /* Otherwise we're out of luck; we assume ARM. */
457 return 0;
c906108c
SS
458}
459
ca90e760
FH
460/* Determine if the address specified equals any of these magic return
461 values, called EXC_RETURN, defined by the ARM v6-M and v7-M
462 architectures.
463
464 From ARMv6-M Reference Manual B1.5.8
465 Table B1-5 Exception return behavior
466
467 EXC_RETURN Return To Return Stack
468 0xFFFFFFF1 Handler mode Main
469 0xFFFFFFF9 Thread mode Main
470 0xFFFFFFFD Thread mode Process
471
472 From ARMv7-M Reference Manual B1.5.8
473 Table B1-8 EXC_RETURN definition of exception return behavior, no FP
474
475 EXC_RETURN Return To Return Stack
476 0xFFFFFFF1 Handler mode Main
477 0xFFFFFFF9 Thread mode Main
478 0xFFFFFFFD Thread mode Process
479
480 Table B1-9 EXC_RETURN definition of exception return behavior, with
481 FP
482
483 EXC_RETURN Return To Return Stack Frame Type
484 0xFFFFFFE1 Handler mode Main Extended
485 0xFFFFFFE9 Thread mode Main Extended
486 0xFFFFFFED Thread mode Process Extended
487 0xFFFFFFF1 Handler mode Main Basic
488 0xFFFFFFF9 Thread mode Main Basic
489 0xFFFFFFFD Thread mode Process Basic
490
491 For more details see "B1.5.8 Exception return behavior"
492 in both ARMv6-M and ARMv7-M Architecture Reference Manuals. */
493
494static int
495arm_m_addr_is_magic (CORE_ADDR addr)
496{
497 switch (addr)
498 {
499 /* Values from Tables in B1.5.8 the EXC_RETURN definitions of
500 the exception return behavior. */
501 case 0xffffffe1:
502 case 0xffffffe9:
503 case 0xffffffed:
504 case 0xfffffff1:
505 case 0xfffffff9:
506 case 0xfffffffd:
507 /* Address is magic. */
508 return 1;
509
510 default:
511 /* Address is not magic. */
512 return 0;
513 }
514}
515
181c1381 516/* Remove useless bits from addresses in a running program. */
34e8f22d 517static CORE_ADDR
24568a2c 518arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
c906108c 519{
2ae28aa9
YQ
520 /* On M-profile devices, do not strip the low bit from EXC_RETURN
521 (the magic exception return address). */
522 if (gdbarch_tdep (gdbarch)->is_m
ca90e760 523 && arm_m_addr_is_magic (val))
2ae28aa9
YQ
524 return val;
525
a3a2ee65 526 if (arm_apcs_32)
dd6be234 527 return UNMAKE_THUMB_ADDR (val);
c906108c 528 else
a3a2ee65 529 return (val & 0x03fffffc);
c906108c
SS
530}
531
0d39a070 532/* Return 1 if PC is the start of a compiler helper function which
e0634ccf
UW
533 can be safely ignored during prologue skipping. IS_THUMB is true
534 if the function is known to be a Thumb function due to the way it
535 is being called. */
0d39a070 536static int
e0634ccf 537skip_prologue_function (struct gdbarch *gdbarch, CORE_ADDR pc, int is_thumb)
0d39a070 538{
e0634ccf 539 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7cbd4a93 540 struct bound_minimal_symbol msym;
0d39a070
DJ
541
542 msym = lookup_minimal_symbol_by_pc (pc);
7cbd4a93 543 if (msym.minsym != NULL
77e371c0 544 && BMSYMBOL_VALUE_ADDRESS (msym) == pc
efd66ac6 545 && MSYMBOL_LINKAGE_NAME (msym.minsym) != NULL)
e0634ccf 546 {
efd66ac6 547 const char *name = MSYMBOL_LINKAGE_NAME (msym.minsym);
0d39a070 548
e0634ccf
UW
549 /* The GNU linker's Thumb call stub to foo is named
550 __foo_from_thumb. */
551 if (strstr (name, "_from_thumb") != NULL)
552 name += 2;
0d39a070 553
e0634ccf
UW
554 /* On soft-float targets, __truncdfsf2 is called to convert promoted
555 arguments to their argument types in non-prototyped
556 functions. */
61012eef 557 if (startswith (name, "__truncdfsf2"))
e0634ccf 558 return 1;
61012eef 559 if (startswith (name, "__aeabi_d2f"))
e0634ccf 560 return 1;
0d39a070 561
e0634ccf 562 /* Internal functions related to thread-local storage. */
61012eef 563 if (startswith (name, "__tls_get_addr"))
e0634ccf 564 return 1;
61012eef 565 if (startswith (name, "__aeabi_read_tp"))
e0634ccf
UW
566 return 1;
567 }
568 else
569 {
570 /* If we run against a stripped glibc, we may be unable to identify
571 special functions by name. Check for one important case,
572 __aeabi_read_tp, by comparing the *code* against the default
573 implementation (this is hand-written ARM assembler in glibc). */
574
575 if (!is_thumb
198cd59d 576 && read_code_unsigned_integer (pc, 4, byte_order_for_code)
e0634ccf 577 == 0xe3e00a0f /* mov r0, #0xffff0fff */
198cd59d 578 && read_code_unsigned_integer (pc + 4, 4, byte_order_for_code)
e0634ccf
UW
579 == 0xe240f01f) /* sub pc, r0, #31 */
580 return 1;
581 }
ec3d575a 582
0d39a070
DJ
583 return 0;
584}
585
621c6d5b
YQ
586/* Extract the immediate from instruction movw/movt of encoding T. INSN1 is
587 the first 16-bit of instruction, and INSN2 is the second 16-bit of
588 instruction. */
589#define EXTRACT_MOVW_MOVT_IMM_T(insn1, insn2) \
590 ((bits ((insn1), 0, 3) << 12) \
591 | (bits ((insn1), 10, 10) << 11) \
592 | (bits ((insn2), 12, 14) << 8) \
593 | bits ((insn2), 0, 7))
594
595/* Extract the immediate from instruction movw/movt of encoding A. INSN is
596 the 32-bit instruction. */
597#define EXTRACT_MOVW_MOVT_IMM_A(insn) \
598 ((bits ((insn), 16, 19) << 12) \
599 | bits ((insn), 0, 11))
600
ec3d575a
UW
601/* Decode immediate value; implements ThumbExpandImmediate pseudo-op. */
602
603static unsigned int
604thumb_expand_immediate (unsigned int imm)
605{
606 unsigned int count = imm >> 7;
607
608 if (count < 8)
609 switch (count / 2)
610 {
611 case 0:
612 return imm & 0xff;
613 case 1:
614 return (imm & 0xff) | ((imm & 0xff) << 16);
615 case 2:
616 return ((imm & 0xff) << 8) | ((imm & 0xff) << 24);
617 case 3:
618 return (imm & 0xff) | ((imm & 0xff) << 8)
619 | ((imm & 0xff) << 16) | ((imm & 0xff) << 24);
620 }
621
622 return (0x80 | (imm & 0x7f)) << (32 - count);
623}
624
540314bd
YQ
625/* Return 1 if the 16-bit Thumb instruction INSN restores SP in
626 epilogue, 0 otherwise. */
627
628static int
629thumb_instruction_restores_sp (unsigned short insn)
630{
631 return (insn == 0x46bd /* mov sp, r7 */
632 || (insn & 0xff80) == 0xb000 /* add sp, imm */
633 || (insn & 0xfe00) == 0xbc00); /* pop <registers> */
634}
635
29d73ae4
DJ
636/* Analyze a Thumb prologue, looking for a recognizable stack frame
637 and frame pointer. Scan until we encounter a store that could
0d39a070
DJ
638 clobber the stack frame unexpectedly, or an unknown instruction.
639 Return the last address which is definitely safe to skip for an
640 initial breakpoint. */
c906108c
SS
641
642static CORE_ADDR
29d73ae4
DJ
643thumb_analyze_prologue (struct gdbarch *gdbarch,
644 CORE_ADDR start, CORE_ADDR limit,
645 struct arm_prologue_cache *cache)
c906108c 646{
0d39a070 647 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
e17a4113 648 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
29d73ae4
DJ
649 int i;
650 pv_t regs[16];
29d73ae4 651 CORE_ADDR offset;
ec3d575a 652 CORE_ADDR unrecognized_pc = 0;
da3c6d4a 653
29d73ae4
DJ
654 for (i = 0; i < 16; i++)
655 regs[i] = pv_register (i, 0);
f7b7ed97 656 pv_area stack (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
29d73ae4 657
29d73ae4 658 while (start < limit)
c906108c 659 {
29d73ae4
DJ
660 unsigned short insn;
661
198cd59d 662 insn = read_code_unsigned_integer (start, 2, byte_order_for_code);
9d4fde75 663
94c30b78 664 if ((insn & 0xfe00) == 0xb400) /* push { rlist } */
da59e081 665 {
29d73ae4
DJ
666 int regno;
667 int mask;
4be43953 668
f7b7ed97 669 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953 670 break;
29d73ae4
DJ
671
672 /* Bits 0-7 contain a mask for registers R0-R7. Bit 8 says
673 whether to save LR (R14). */
674 mask = (insn & 0xff) | ((insn & 0x100) << 6);
675
676 /* Calculate offsets of saved R0-R7 and LR. */
677 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
678 if (mask & (1 << regno))
679 {
29d73ae4
DJ
680 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
681 -4);
f7b7ed97 682 stack.store (regs[ARM_SP_REGNUM], 4, regs[regno]);
29d73ae4 683 }
da59e081 684 }
1db01f22 685 else if ((insn & 0xff80) == 0xb080) /* sub sp, #imm */
da59e081 686 {
29d73ae4 687 offset = (insn & 0x7f) << 2; /* get scaled offset */
1db01f22
YQ
688 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
689 -offset);
da59e081 690 }
808f7ab1
YQ
691 else if (thumb_instruction_restores_sp (insn))
692 {
693 /* Don't scan past the epilogue. */
694 break;
695 }
0d39a070
DJ
696 else if ((insn & 0xf800) == 0xa800) /* add Rd, sp, #imm */
697 regs[bits (insn, 8, 10)] = pv_add_constant (regs[ARM_SP_REGNUM],
698 (insn & 0xff) << 2);
699 else if ((insn & 0xfe00) == 0x1c00 /* add Rd, Rn, #imm */
700 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
701 regs[bits (insn, 0, 2)] = pv_add_constant (regs[bits (insn, 3, 5)],
702 bits (insn, 6, 8));
703 else if ((insn & 0xf800) == 0x3000 /* add Rd, #imm */
704 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
705 regs[bits (insn, 8, 10)] = pv_add_constant (regs[bits (insn, 8, 10)],
706 bits (insn, 0, 7));
707 else if ((insn & 0xfe00) == 0x1800 /* add Rd, Rn, Rm */
708 && pv_is_register (regs[bits (insn, 6, 8)], ARM_SP_REGNUM)
709 && pv_is_constant (regs[bits (insn, 3, 5)]))
710 regs[bits (insn, 0, 2)] = pv_add (regs[bits (insn, 3, 5)],
711 regs[bits (insn, 6, 8)]);
712 else if ((insn & 0xff00) == 0x4400 /* add Rd, Rm */
713 && pv_is_constant (regs[bits (insn, 3, 6)]))
714 {
715 int rd = (bit (insn, 7) << 3) + bits (insn, 0, 2);
716 int rm = bits (insn, 3, 6);
717 regs[rd] = pv_add (regs[rd], regs[rm]);
718 }
29d73ae4 719 else if ((insn & 0xff00) == 0x4600) /* mov hi, lo or mov lo, hi */
da59e081 720 {
29d73ae4
DJ
721 int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
722 int src_reg = (insn & 0x78) >> 3;
723 regs[dst_reg] = regs[src_reg];
da59e081 724 }
29d73ae4 725 else if ((insn & 0xf800) == 0x9000) /* str rd, [sp, #off] */
da59e081 726 {
29d73ae4
DJ
727 /* Handle stores to the stack. Normally pushes are used,
728 but with GCC -mtpcs-frame, there may be other stores
729 in the prologue to create the frame. */
730 int regno = (insn >> 8) & 0x7;
731 pv_t addr;
732
733 offset = (insn & 0xff) << 2;
734 addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
735
f7b7ed97 736 if (stack.store_would_trash (addr))
29d73ae4
DJ
737 break;
738
f7b7ed97 739 stack.store (addr, 4, regs[regno]);
da59e081 740 }
0d39a070
DJ
741 else if ((insn & 0xf800) == 0x6000) /* str rd, [rn, #off] */
742 {
743 int rd = bits (insn, 0, 2);
744 int rn = bits (insn, 3, 5);
745 pv_t addr;
746
747 offset = bits (insn, 6, 10) << 2;
748 addr = pv_add_constant (regs[rn], offset);
749
f7b7ed97 750 if (stack.store_would_trash (addr))
0d39a070
DJ
751 break;
752
f7b7ed97 753 stack.store (addr, 4, regs[rd]);
0d39a070
DJ
754 }
755 else if (((insn & 0xf800) == 0x7000 /* strb Rd, [Rn, #off] */
756 || (insn & 0xf800) == 0x8000) /* strh Rd, [Rn, #off] */
757 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
758 /* Ignore stores of argument registers to the stack. */
759 ;
760 else if ((insn & 0xf800) == 0xc800 /* ldmia Rn!, { registers } */
761 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
762 /* Ignore block loads from the stack, potentially copying
763 parameters from memory. */
764 ;
765 else if ((insn & 0xf800) == 0x9800 /* ldr Rd, [Rn, #immed] */
766 || ((insn & 0xf800) == 0x6800 /* ldr Rd, [sp, #immed] */
767 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM)))
768 /* Similarly ignore single loads from the stack. */
769 ;
770 else if ((insn & 0xffc0) == 0x0000 /* lsls Rd, Rm, #0 */
771 || (insn & 0xffc0) == 0x1c00) /* add Rd, Rn, #0 */
772 /* Skip register copies, i.e. saves to another register
773 instead of the stack. */
774 ;
775 else if ((insn & 0xf800) == 0x2000) /* movs Rd, #imm */
776 /* Recognize constant loads; even with small stacks these are necessary
777 on Thumb. */
778 regs[bits (insn, 8, 10)] = pv_constant (bits (insn, 0, 7));
779 else if ((insn & 0xf800) == 0x4800) /* ldr Rd, [pc, #imm] */
780 {
781 /* Constant pool loads, for the same reason. */
782 unsigned int constant;
783 CORE_ADDR loc;
784
785 loc = start + 4 + bits (insn, 0, 7) * 4;
786 constant = read_memory_unsigned_integer (loc, 4, byte_order);
787 regs[bits (insn, 8, 10)] = pv_constant (constant);
788 }
db24da6d 789 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instructions. */
0d39a070 790 {
0d39a070
DJ
791 unsigned short inst2;
792
198cd59d
YQ
793 inst2 = read_code_unsigned_integer (start + 2, 2,
794 byte_order_for_code);
0d39a070
DJ
795
796 if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
797 {
798 /* BL, BLX. Allow some special function calls when
799 skipping the prologue; GCC generates these before
800 storing arguments to the stack. */
801 CORE_ADDR nextpc;
802 int j1, j2, imm1, imm2;
803
804 imm1 = sbits (insn, 0, 10);
805 imm2 = bits (inst2, 0, 10);
806 j1 = bit (inst2, 13);
807 j2 = bit (inst2, 11);
808
809 offset = ((imm1 << 12) + (imm2 << 1));
810 offset ^= ((!j2) << 22) | ((!j1) << 23);
811
812 nextpc = start + 4 + offset;
813 /* For BLX make sure to clear the low bits. */
814 if (bit (inst2, 12) == 0)
815 nextpc = nextpc & 0xfffffffc;
816
e0634ccf
UW
817 if (!skip_prologue_function (gdbarch, nextpc,
818 bit (inst2, 12) != 0))
0d39a070
DJ
819 break;
820 }
ec3d575a 821
0963b4bd
MS
822 else if ((insn & 0xffd0) == 0xe900 /* stmdb Rn{!},
823 { registers } */
ec3d575a
UW
824 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
825 {
826 pv_t addr = regs[bits (insn, 0, 3)];
827 int regno;
828
f7b7ed97 829 if (stack.store_would_trash (addr))
ec3d575a
UW
830 break;
831
832 /* Calculate offsets of saved registers. */
833 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
834 if (inst2 & (1 << regno))
835 {
836 addr = pv_add_constant (addr, -4);
f7b7ed97 837 stack.store (addr, 4, regs[regno]);
ec3d575a
UW
838 }
839
840 if (insn & 0x0020)
841 regs[bits (insn, 0, 3)] = addr;
842 }
843
0963b4bd
MS
844 else if ((insn & 0xff50) == 0xe940 /* strd Rt, Rt2,
845 [Rn, #+/-imm]{!} */
ec3d575a
UW
846 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
847 {
848 int regno1 = bits (inst2, 12, 15);
849 int regno2 = bits (inst2, 8, 11);
850 pv_t addr = regs[bits (insn, 0, 3)];
851
852 offset = inst2 & 0xff;
853 if (insn & 0x0080)
854 addr = pv_add_constant (addr, offset);
855 else
856 addr = pv_add_constant (addr, -offset);
857
f7b7ed97 858 if (stack.store_would_trash (addr))
ec3d575a
UW
859 break;
860
f7b7ed97
TT
861 stack.store (addr, 4, regs[regno1]);
862 stack.store (pv_add_constant (addr, 4),
863 4, regs[regno2]);
ec3d575a
UW
864
865 if (insn & 0x0020)
866 regs[bits (insn, 0, 3)] = addr;
867 }
868
869 else if ((insn & 0xfff0) == 0xf8c0 /* str Rt,[Rn,+/-#imm]{!} */
870 && (inst2 & 0x0c00) == 0x0c00
871 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
872 {
873 int regno = bits (inst2, 12, 15);
874 pv_t addr = regs[bits (insn, 0, 3)];
875
876 offset = inst2 & 0xff;
877 if (inst2 & 0x0200)
878 addr = pv_add_constant (addr, offset);
879 else
880 addr = pv_add_constant (addr, -offset);
881
f7b7ed97 882 if (stack.store_would_trash (addr))
ec3d575a
UW
883 break;
884
f7b7ed97 885 stack.store (addr, 4, regs[regno]);
ec3d575a
UW
886
887 if (inst2 & 0x0100)
888 regs[bits (insn, 0, 3)] = addr;
889 }
890
891 else if ((insn & 0xfff0) == 0xf8c0 /* str.w Rt,[Rn,#imm] */
892 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
893 {
894 int regno = bits (inst2, 12, 15);
895 pv_t addr;
896
897 offset = inst2 & 0xfff;
898 addr = pv_add_constant (regs[bits (insn, 0, 3)], offset);
899
f7b7ed97 900 if (stack.store_would_trash (addr))
ec3d575a
UW
901 break;
902
f7b7ed97 903 stack.store (addr, 4, regs[regno]);
ec3d575a
UW
904 }
905
906 else if ((insn & 0xffd0) == 0xf880 /* str{bh}.w Rt,[Rn,#imm] */
0d39a070 907 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 908 /* Ignore stores of argument registers to the stack. */
0d39a070 909 ;
ec3d575a
UW
910
911 else if ((insn & 0xffd0) == 0xf800 /* str{bh} Rt,[Rn,#+/-imm] */
912 && (inst2 & 0x0d00) == 0x0c00
0d39a070 913 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 914 /* Ignore stores of argument registers to the stack. */
0d39a070 915 ;
ec3d575a 916
0963b4bd
MS
917 else if ((insn & 0xffd0) == 0xe890 /* ldmia Rn[!],
918 { registers } */
ec3d575a
UW
919 && (inst2 & 0x8000) == 0x0000
920 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
921 /* Ignore block loads from the stack, potentially copying
922 parameters from memory. */
0d39a070 923 ;
ec3d575a 924
0963b4bd
MS
925 else if ((insn & 0xffb0) == 0xe950 /* ldrd Rt, Rt2,
926 [Rn, #+/-imm] */
0d39a070 927 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 928 /* Similarly ignore dual loads from the stack. */
0d39a070 929 ;
ec3d575a
UW
930
931 else if ((insn & 0xfff0) == 0xf850 /* ldr Rt,[Rn,#+/-imm] */
932 && (inst2 & 0x0d00) == 0x0c00
0d39a070 933 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 934 /* Similarly ignore single loads from the stack. */
0d39a070 935 ;
ec3d575a
UW
936
937 else if ((insn & 0xfff0) == 0xf8d0 /* ldr.w Rt,[Rn,#imm] */
0d39a070 938 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 939 /* Similarly ignore single loads from the stack. */
0d39a070 940 ;
ec3d575a
UW
941
942 else if ((insn & 0xfbf0) == 0xf100 /* add.w Rd, Rn, #imm */
943 && (inst2 & 0x8000) == 0x0000)
944 {
945 unsigned int imm = ((bits (insn, 10, 10) << 11)
946 | (bits (inst2, 12, 14) << 8)
947 | bits (inst2, 0, 7));
948
949 regs[bits (inst2, 8, 11)]
950 = pv_add_constant (regs[bits (insn, 0, 3)],
951 thumb_expand_immediate (imm));
952 }
953
954 else if ((insn & 0xfbf0) == 0xf200 /* addw Rd, Rn, #imm */
955 && (inst2 & 0x8000) == 0x0000)
0d39a070 956 {
ec3d575a
UW
957 unsigned int imm = ((bits (insn, 10, 10) << 11)
958 | (bits (inst2, 12, 14) << 8)
959 | bits (inst2, 0, 7));
960
961 regs[bits (inst2, 8, 11)]
962 = pv_add_constant (regs[bits (insn, 0, 3)], imm);
963 }
964
965 else if ((insn & 0xfbf0) == 0xf1a0 /* sub.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 - (CORE_ADDR) thumb_expand_immediate (imm));
975 }
976
977 else if ((insn & 0xfbf0) == 0xf2a0 /* subw Rd, Rn, #imm */
978 && (inst2 & 0x8000) == 0x0000)
979 {
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)], - (CORE_ADDR) imm);
986 }
987
988 else if ((insn & 0xfbff) == 0xf04f) /* mov.w Rd, #const */
989 {
990 unsigned int imm = ((bits (insn, 10, 10) << 11)
991 | (bits (inst2, 12, 14) << 8)
992 | bits (inst2, 0, 7));
993
994 regs[bits (inst2, 8, 11)]
995 = pv_constant (thumb_expand_immediate (imm));
996 }
997
998 else if ((insn & 0xfbf0) == 0xf240) /* movw Rd, #const */
999 {
621c6d5b
YQ
1000 unsigned int imm
1001 = EXTRACT_MOVW_MOVT_IMM_T (insn, inst2);
ec3d575a
UW
1002
1003 regs[bits (inst2, 8, 11)] = pv_constant (imm);
1004 }
1005
1006 else if (insn == 0xea5f /* mov.w Rd,Rm */
1007 && (inst2 & 0xf0f0) == 0)
1008 {
1009 int dst_reg = (inst2 & 0x0f00) >> 8;
1010 int src_reg = inst2 & 0xf;
1011 regs[dst_reg] = regs[src_reg];
1012 }
1013
1014 else if ((insn & 0xff7f) == 0xf85f) /* ldr.w Rt,<label> */
1015 {
1016 /* Constant pool loads. */
1017 unsigned int constant;
1018 CORE_ADDR loc;
1019
cac395ea 1020 offset = bits (inst2, 0, 11);
ec3d575a
UW
1021 if (insn & 0x0080)
1022 loc = start + 4 + offset;
1023 else
1024 loc = start + 4 - offset;
1025
1026 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1027 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1028 }
1029
1030 else if ((insn & 0xff7f) == 0xe95f) /* ldrd Rt,Rt2,<label> */
1031 {
1032 /* Constant pool loads. */
1033 unsigned int constant;
1034 CORE_ADDR loc;
1035
cac395ea 1036 offset = bits (inst2, 0, 7) << 2;
ec3d575a
UW
1037 if (insn & 0x0080)
1038 loc = start + 4 + offset;
1039 else
1040 loc = start + 4 - offset;
1041
1042 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1043 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1044
1045 constant = read_memory_unsigned_integer (loc + 4, 4, byte_order);
1046 regs[bits (inst2, 8, 11)] = pv_constant (constant);
1047 }
1048
1049 else if (thumb2_instruction_changes_pc (insn, inst2))
1050 {
1051 /* Don't scan past anything that might change control flow. */
0d39a070
DJ
1052 break;
1053 }
ec3d575a
UW
1054 else
1055 {
1056 /* The optimizer might shove anything into the prologue,
1057 so we just skip what we don't recognize. */
1058 unrecognized_pc = start;
1059 }
0d39a070
DJ
1060
1061 start += 2;
1062 }
ec3d575a 1063 else if (thumb_instruction_changes_pc (insn))
3d74b771 1064 {
ec3d575a 1065 /* Don't scan past anything that might change control flow. */
da3c6d4a 1066 break;
3d74b771 1067 }
ec3d575a
UW
1068 else
1069 {
1070 /* The optimizer might shove anything into the prologue,
1071 so we just skip what we don't recognize. */
1072 unrecognized_pc = start;
1073 }
29d73ae4
DJ
1074
1075 start += 2;
c906108c
SS
1076 }
1077
0d39a070
DJ
1078 if (arm_debug)
1079 fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1080 paddress (gdbarch, start));
1081
ec3d575a
UW
1082 if (unrecognized_pc == 0)
1083 unrecognized_pc = start;
1084
29d73ae4 1085 if (cache == NULL)
f7b7ed97 1086 return unrecognized_pc;
29d73ae4 1087
29d73ae4
DJ
1088 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1089 {
1090 /* Frame pointer is fp. Frame size is constant. */
1091 cache->framereg = ARM_FP_REGNUM;
1092 cache->framesize = -regs[ARM_FP_REGNUM].k;
1093 }
1094 else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
1095 {
1096 /* Frame pointer is r7. Frame size is constant. */
1097 cache->framereg = THUMB_FP_REGNUM;
1098 cache->framesize = -regs[THUMB_FP_REGNUM].k;
1099 }
72a2e3dc 1100 else
29d73ae4
DJ
1101 {
1102 /* Try the stack pointer... this is a bit desperate. */
1103 cache->framereg = ARM_SP_REGNUM;
1104 cache->framesize = -regs[ARM_SP_REGNUM].k;
1105 }
29d73ae4
DJ
1106
1107 for (i = 0; i < 16; i++)
f7b7ed97 1108 if (stack.find_reg (gdbarch, i, &offset))
29d73ae4
DJ
1109 cache->saved_regs[i].addr = offset;
1110
ec3d575a 1111 return unrecognized_pc;
c906108c
SS
1112}
1113
621c6d5b
YQ
1114
1115/* Try to analyze the instructions starting from PC, which load symbol
1116 __stack_chk_guard. Return the address of instruction after loading this
1117 symbol, set the dest register number to *BASEREG, and set the size of
1118 instructions for loading symbol in OFFSET. Return 0 if instructions are
1119 not recognized. */
1120
1121static CORE_ADDR
1122arm_analyze_load_stack_chk_guard(CORE_ADDR pc, struct gdbarch *gdbarch,
1123 unsigned int *destreg, int *offset)
1124{
1125 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1126 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1127 unsigned int low, high, address;
1128
1129 address = 0;
1130 if (is_thumb)
1131 {
1132 unsigned short insn1
198cd59d 1133 = read_code_unsigned_integer (pc, 2, byte_order_for_code);
621c6d5b
YQ
1134
1135 if ((insn1 & 0xf800) == 0x4800) /* ldr Rd, #immed */
1136 {
1137 *destreg = bits (insn1, 8, 10);
1138 *offset = 2;
6ae274b7
YQ
1139 address = (pc & 0xfffffffc) + 4 + (bits (insn1, 0, 7) << 2);
1140 address = read_memory_unsigned_integer (address, 4,
1141 byte_order_for_code);
621c6d5b
YQ
1142 }
1143 else if ((insn1 & 0xfbf0) == 0xf240) /* movw Rd, #const */
1144 {
1145 unsigned short insn2
198cd59d 1146 = read_code_unsigned_integer (pc + 2, 2, byte_order_for_code);
621c6d5b
YQ
1147
1148 low = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1149
1150 insn1
198cd59d 1151 = read_code_unsigned_integer (pc + 4, 2, byte_order_for_code);
621c6d5b 1152 insn2
198cd59d 1153 = read_code_unsigned_integer (pc + 6, 2, byte_order_for_code);
621c6d5b
YQ
1154
1155 /* movt Rd, #const */
1156 if ((insn1 & 0xfbc0) == 0xf2c0)
1157 {
1158 high = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1159 *destreg = bits (insn2, 8, 11);
1160 *offset = 8;
1161 address = (high << 16 | low);
1162 }
1163 }
1164 }
1165 else
1166 {
2e9e421f 1167 unsigned int insn
198cd59d 1168 = read_code_unsigned_integer (pc, 4, byte_order_for_code);
2e9e421f 1169
6ae274b7 1170 if ((insn & 0x0e5f0000) == 0x041f0000) /* ldr Rd, [PC, #immed] */
2e9e421f 1171 {
6ae274b7
YQ
1172 address = bits (insn, 0, 11) + pc + 8;
1173 address = read_memory_unsigned_integer (address, 4,
1174 byte_order_for_code);
1175
2e9e421f
UW
1176 *destreg = bits (insn, 12, 15);
1177 *offset = 4;
1178 }
1179 else if ((insn & 0x0ff00000) == 0x03000000) /* movw Rd, #const */
1180 {
1181 low = EXTRACT_MOVW_MOVT_IMM_A (insn);
1182
1183 insn
198cd59d 1184 = read_code_unsigned_integer (pc + 4, 4, byte_order_for_code);
2e9e421f
UW
1185
1186 if ((insn & 0x0ff00000) == 0x03400000) /* movt Rd, #const */
1187 {
1188 high = EXTRACT_MOVW_MOVT_IMM_A (insn);
1189 *destreg = bits (insn, 12, 15);
1190 *offset = 8;
1191 address = (high << 16 | low);
1192 }
1193 }
621c6d5b
YQ
1194 }
1195
1196 return address;
1197}
1198
1199/* Try to skip a sequence of instructions used for stack protector. If PC
0963b4bd
MS
1200 points to the first instruction of this sequence, return the address of
1201 first instruction after this sequence, otherwise, return original PC.
621c6d5b
YQ
1202
1203 On arm, this sequence of instructions is composed of mainly three steps,
1204 Step 1: load symbol __stack_chk_guard,
1205 Step 2: load from address of __stack_chk_guard,
1206 Step 3: store it to somewhere else.
1207
1208 Usually, instructions on step 2 and step 3 are the same on various ARM
1209 architectures. On step 2, it is one instruction 'ldr Rx, [Rn, #0]', and
1210 on step 3, it is also one instruction 'str Rx, [r7, #immd]'. However,
1211 instructions in step 1 vary from different ARM architectures. On ARMv7,
1212 they are,
1213
1214 movw Rn, #:lower16:__stack_chk_guard
1215 movt Rn, #:upper16:__stack_chk_guard
1216
1217 On ARMv5t, it is,
1218
1219 ldr Rn, .Label
1220 ....
1221 .Lable:
1222 .word __stack_chk_guard
1223
1224 Since ldr/str is a very popular instruction, we can't use them as
1225 'fingerprint' or 'signature' of stack protector sequence. Here we choose
1226 sequence {movw/movt, ldr}/ldr/str plus symbol __stack_chk_guard, if not
1227 stripped, as the 'fingerprint' of a stack protector cdoe sequence. */
1228
1229static CORE_ADDR
1230arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
1231{
1232 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
22e048c9 1233 unsigned int basereg;
7cbd4a93 1234 struct bound_minimal_symbol stack_chk_guard;
621c6d5b
YQ
1235 int offset;
1236 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1237 CORE_ADDR addr;
1238
1239 /* Try to parse the instructions in Step 1. */
1240 addr = arm_analyze_load_stack_chk_guard (pc, gdbarch,
1241 &basereg, &offset);
1242 if (!addr)
1243 return pc;
1244
1245 stack_chk_guard = lookup_minimal_symbol_by_pc (addr);
6041179a
JB
1246 /* ADDR must correspond to a symbol whose name is __stack_chk_guard.
1247 Otherwise, this sequence cannot be for stack protector. */
1248 if (stack_chk_guard.minsym == NULL
61012eef 1249 || !startswith (MSYMBOL_LINKAGE_NAME (stack_chk_guard.minsym), "__stack_chk_guard"))
621c6d5b
YQ
1250 return pc;
1251
1252 if (is_thumb)
1253 {
1254 unsigned int destreg;
1255 unsigned short insn
198cd59d 1256 = read_code_unsigned_integer (pc + offset, 2, byte_order_for_code);
621c6d5b
YQ
1257
1258 /* Step 2: ldr Rd, [Rn, #immed], encoding T1. */
1259 if ((insn & 0xf800) != 0x6800)
1260 return pc;
1261 if (bits (insn, 3, 5) != basereg)
1262 return pc;
1263 destreg = bits (insn, 0, 2);
1264
198cd59d
YQ
1265 insn = read_code_unsigned_integer (pc + offset + 2, 2,
1266 byte_order_for_code);
621c6d5b
YQ
1267 /* Step 3: str Rd, [Rn, #immed], encoding T1. */
1268 if ((insn & 0xf800) != 0x6000)
1269 return pc;
1270 if (destreg != bits (insn, 0, 2))
1271 return pc;
1272 }
1273 else
1274 {
1275 unsigned int destreg;
1276 unsigned int insn
198cd59d 1277 = read_code_unsigned_integer (pc + offset, 4, byte_order_for_code);
621c6d5b
YQ
1278
1279 /* Step 2: ldr Rd, [Rn, #immed], encoding A1. */
1280 if ((insn & 0x0e500000) != 0x04100000)
1281 return pc;
1282 if (bits (insn, 16, 19) != basereg)
1283 return pc;
1284 destreg = bits (insn, 12, 15);
1285 /* Step 3: str Rd, [Rn, #immed], encoding A1. */
198cd59d 1286 insn = read_code_unsigned_integer (pc + offset + 4,
621c6d5b
YQ
1287 4, byte_order_for_code);
1288 if ((insn & 0x0e500000) != 0x04000000)
1289 return pc;
1290 if (bits (insn, 12, 15) != destreg)
1291 return pc;
1292 }
1293 /* The size of total two instructions ldr/str is 4 on Thumb-2, while 8
1294 on arm. */
1295 if (is_thumb)
1296 return pc + offset + 4;
1297 else
1298 return pc + offset + 8;
1299}
1300
da3c6d4a
MS
1301/* Advance the PC across any function entry prologue instructions to
1302 reach some "real" code.
34e8f22d
RE
1303
1304 The APCS (ARM Procedure Call Standard) defines the following
ed9a39eb 1305 prologue:
c906108c 1306
c5aa993b
JM
1307 mov ip, sp
1308 [stmfd sp!, {a1,a2,a3,a4}]
1309 stmfd sp!, {...,fp,ip,lr,pc}
ed9a39eb
JM
1310 [stfe f7, [sp, #-12]!]
1311 [stfe f6, [sp, #-12]!]
1312 [stfe f5, [sp, #-12]!]
1313 [stfe f4, [sp, #-12]!]
0963b4bd 1314 sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn. */
c906108c 1315
34e8f22d 1316static CORE_ADDR
6093d2eb 1317arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
c906108c 1318{
a89fea3c 1319 CORE_ADDR func_addr, limit_pc;
c906108c 1320
a89fea3c
JL
1321 /* See if we can determine the end of the prologue via the symbol table.
1322 If so, then return either PC, or the PC after the prologue, whichever
1323 is greater. */
1324 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
c906108c 1325 {
d80b854b
UW
1326 CORE_ADDR post_prologue_pc
1327 = skip_prologue_using_sal (gdbarch, func_addr);
43f3e411 1328 struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);
0d39a070 1329
621c6d5b
YQ
1330 if (post_prologue_pc)
1331 post_prologue_pc
1332 = arm_skip_stack_protector (post_prologue_pc, gdbarch);
1333
1334
0d39a070
DJ
1335 /* GCC always emits a line note before the prologue and another
1336 one after, even if the two are at the same address or on the
1337 same line. Take advantage of this so that we do not need to
1338 know every instruction that might appear in the prologue. We
1339 will have producer information for most binaries; if it is
1340 missing (e.g. for -gstabs), assuming the GNU tools. */
1341 if (post_prologue_pc
43f3e411
DE
1342 && (cust == NULL
1343 || COMPUNIT_PRODUCER (cust) == NULL
61012eef
GB
1344 || startswith (COMPUNIT_PRODUCER (cust), "GNU ")
1345 || startswith (COMPUNIT_PRODUCER (cust), "clang ")))
0d39a070
DJ
1346 return post_prologue_pc;
1347
a89fea3c 1348 if (post_prologue_pc != 0)
0d39a070
DJ
1349 {
1350 CORE_ADDR analyzed_limit;
1351
1352 /* For non-GCC compilers, make sure the entire line is an
1353 acceptable prologue; GDB will round this function's
1354 return value up to the end of the following line so we
1355 can not skip just part of a line (and we do not want to).
1356
1357 RealView does not treat the prologue specially, but does
1358 associate prologue code with the opening brace; so this
1359 lets us skip the first line if we think it is the opening
1360 brace. */
9779414d 1361 if (arm_pc_is_thumb (gdbarch, func_addr))
0d39a070
DJ
1362 analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
1363 post_prologue_pc, NULL);
1364 else
1365 analyzed_limit = arm_analyze_prologue (gdbarch, func_addr,
1366 post_prologue_pc, NULL);
1367
1368 if (analyzed_limit != post_prologue_pc)
1369 return func_addr;
1370
1371 return post_prologue_pc;
1372 }
c906108c
SS
1373 }
1374
a89fea3c
JL
1375 /* Can't determine prologue from the symbol table, need to examine
1376 instructions. */
c906108c 1377
a89fea3c
JL
1378 /* Find an upper limit on the function prologue using the debug
1379 information. If the debug information could not be used to provide
1380 that bound, then use an arbitrary large number as the upper bound. */
0963b4bd 1381 /* Like arm_scan_prologue, stop no later than pc + 64. */
d80b854b 1382 limit_pc = skip_prologue_using_sal (gdbarch, pc);
a89fea3c
JL
1383 if (limit_pc == 0)
1384 limit_pc = pc + 64; /* Magic. */
1385
c906108c 1386
29d73ae4 1387 /* Check if this is Thumb code. */
9779414d 1388 if (arm_pc_is_thumb (gdbarch, pc))
a89fea3c 1389 return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
21daaaaf
YQ
1390 else
1391 return arm_analyze_prologue (gdbarch, pc, limit_pc, NULL);
c906108c 1392}
94c30b78 1393
c5aa993b 1394/* *INDENT-OFF* */
c906108c
SS
1395/* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
1396 This function decodes a Thumb function prologue to determine:
1397 1) the size of the stack frame
1398 2) which registers are saved on it
1399 3) the offsets of saved regs
1400 4) the offset from the stack pointer to the frame pointer
c906108c 1401
da59e081
JM
1402 A typical Thumb function prologue would create this stack frame
1403 (offsets relative to FP)
c906108c
SS
1404 old SP -> 24 stack parameters
1405 20 LR
1406 16 R7
1407 R7 -> 0 local variables (16 bytes)
1408 SP -> -12 additional stack space (12 bytes)
1409 The frame size would thus be 36 bytes, and the frame offset would be
0963b4bd 1410 12 bytes. The frame register is R7.
da59e081 1411
da3c6d4a
MS
1412 The comments for thumb_skip_prolog() describe the algorithm we use
1413 to detect the end of the prolog. */
c5aa993b
JM
1414/* *INDENT-ON* */
1415
c906108c 1416static void
be8626e0 1417thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
b39cc962 1418 CORE_ADDR block_addr, struct arm_prologue_cache *cache)
c906108c
SS
1419{
1420 CORE_ADDR prologue_start;
1421 CORE_ADDR prologue_end;
c906108c 1422
b39cc962
DJ
1423 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1424 &prologue_end))
c906108c 1425 {
ec3d575a
UW
1426 /* See comment in arm_scan_prologue for an explanation of
1427 this heuristics. */
1428 if (prologue_end > prologue_start + 64)
1429 {
1430 prologue_end = prologue_start + 64;
1431 }
c906108c
SS
1432 }
1433 else
f7060f85
DJ
1434 /* We're in the boondocks: we have no idea where the start of the
1435 function is. */
1436 return;
c906108c 1437
325fac50 1438 prologue_end = std::min (prologue_end, prev_pc);
c906108c 1439
be8626e0 1440 thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
1441}
1442
f303bc3e
YQ
1443/* Return 1 if the ARM instruction INSN restores SP in epilogue, 0
1444 otherwise. */
1445
1446static int
1447arm_instruction_restores_sp (unsigned int insn)
1448{
1449 if (bits (insn, 28, 31) != INST_NV)
1450 {
1451 if ((insn & 0x0df0f000) == 0x0080d000
1452 /* ADD SP (register or immediate). */
1453 || (insn & 0x0df0f000) == 0x0040d000
1454 /* SUB SP (register or immediate). */
1455 || (insn & 0x0ffffff0) == 0x01a0d000
1456 /* MOV SP. */
1457 || (insn & 0x0fff0000) == 0x08bd0000
1458 /* POP (LDMIA). */
1459 || (insn & 0x0fff0000) == 0x049d0000)
1460 /* POP of a single register. */
1461 return 1;
1462 }
1463
1464 return 0;
1465}
1466
0d39a070
DJ
1467/* Analyze an ARM mode prologue starting at PROLOGUE_START and
1468 continuing no further than PROLOGUE_END. If CACHE is non-NULL,
1469 fill it in. Return the first address not recognized as a prologue
1470 instruction.
eb5492fa 1471
0d39a070
DJ
1472 We recognize all the instructions typically found in ARM prologues,
1473 plus harmless instructions which can be skipped (either for analysis
1474 purposes, or a more restrictive set that can be skipped when finding
1475 the end of the prologue). */
1476
1477static CORE_ADDR
1478arm_analyze_prologue (struct gdbarch *gdbarch,
1479 CORE_ADDR prologue_start, CORE_ADDR prologue_end,
1480 struct arm_prologue_cache *cache)
1481{
0d39a070
DJ
1482 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1483 int regno;
1484 CORE_ADDR offset, current_pc;
1485 pv_t regs[ARM_FPS_REGNUM];
0d39a070
DJ
1486 CORE_ADDR unrecognized_pc = 0;
1487
1488 /* Search the prologue looking for instructions that set up the
96baa820 1489 frame pointer, adjust the stack pointer, and save registers.
ed9a39eb 1490
96baa820
JM
1491 Be careful, however, and if it doesn't look like a prologue,
1492 don't try to scan it. If, for instance, a frameless function
1493 begins with stmfd sp!, then we will tell ourselves there is
b8d5e71d 1494 a frame, which will confuse stack traceback, as well as "finish"
96baa820 1495 and other operations that rely on a knowledge of the stack
0d39a070 1496 traceback. */
d4473757 1497
4be43953
DJ
1498 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1499 regs[regno] = pv_register (regno, 0);
f7b7ed97 1500 pv_area stack (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
4be43953 1501
94c30b78
MS
1502 for (current_pc = prologue_start;
1503 current_pc < prologue_end;
f43845b3 1504 current_pc += 4)
96baa820 1505 {
e17a4113 1506 unsigned int insn
198cd59d 1507 = read_code_unsigned_integer (current_pc, 4, byte_order_for_code);
9d4fde75 1508
94c30b78 1509 if (insn == 0xe1a0c00d) /* mov ip, sp */
f43845b3 1510 {
4be43953 1511 regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
28cd8767
JG
1512 continue;
1513 }
0d39a070
DJ
1514 else if ((insn & 0xfff00000) == 0xe2800000 /* add Rd, Rn, #n */
1515 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767
JG
1516 {
1517 unsigned imm = insn & 0xff; /* immediate value */
1518 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
0d39a070 1519 int rd = bits (insn, 12, 15);
28cd8767 1520 imm = (imm >> rot) | (imm << (32 - rot));
0d39a070 1521 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
28cd8767
JG
1522 continue;
1523 }
0d39a070
DJ
1524 else if ((insn & 0xfff00000) == 0xe2400000 /* sub Rd, Rn, #n */
1525 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767
JG
1526 {
1527 unsigned imm = insn & 0xff; /* immediate value */
1528 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
0d39a070 1529 int rd = bits (insn, 12, 15);
28cd8767 1530 imm = (imm >> rot) | (imm << (32 - rot));
0d39a070 1531 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
f43845b3
MS
1532 continue;
1533 }
0963b4bd
MS
1534 else if ((insn & 0xffff0fff) == 0xe52d0004) /* str Rd,
1535 [sp, #-4]! */
f43845b3 1536 {
f7b7ed97 1537 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
1538 break;
1539 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
f7b7ed97
TT
1540 stack.store (regs[ARM_SP_REGNUM], 4,
1541 regs[bits (insn, 12, 15)]);
f43845b3
MS
1542 continue;
1543 }
1544 else if ((insn & 0xffff0000) == 0xe92d0000)
d4473757
KB
1545 /* stmfd sp!, {..., fp, ip, lr, pc}
1546 or
1547 stmfd sp!, {a1, a2, a3, a4} */
c906108c 1548 {
d4473757 1549 int mask = insn & 0xffff;
ed9a39eb 1550
f7b7ed97 1551 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
1552 break;
1553
94c30b78 1554 /* Calculate offsets of saved registers. */
34e8f22d 1555 for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
d4473757
KB
1556 if (mask & (1 << regno))
1557 {
0963b4bd
MS
1558 regs[ARM_SP_REGNUM]
1559 = pv_add_constant (regs[ARM_SP_REGNUM], -4);
f7b7ed97 1560 stack.store (regs[ARM_SP_REGNUM], 4, regs[regno]);
d4473757
KB
1561 }
1562 }
0d39a070
DJ
1563 else if ((insn & 0xffff0000) == 0xe54b0000 /* strb rx,[r11,#-n] */
1564 || (insn & 0xffff00f0) == 0xe14b00b0 /* strh rx,[r11,#-n] */
f8bf5763 1565 || (insn & 0xffffc000) == 0xe50b0000) /* str rx,[r11,#-n] */
b8d5e71d
MS
1566 {
1567 /* No need to add this to saved_regs -- it's just an arg reg. */
1568 continue;
1569 }
0d39a070
DJ
1570 else if ((insn & 0xffff0000) == 0xe5cd0000 /* strb rx,[sp,#n] */
1571 || (insn & 0xffff00f0) == 0xe1cd00b0 /* strh rx,[sp,#n] */
f8bf5763 1572 || (insn & 0xffffc000) == 0xe58d0000) /* str rx,[sp,#n] */
f43845b3
MS
1573 {
1574 /* No need to add this to saved_regs -- it's just an arg reg. */
1575 continue;
1576 }
0963b4bd
MS
1577 else if ((insn & 0xfff00000) == 0xe8800000 /* stm Rn,
1578 { registers } */
0d39a070
DJ
1579 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1580 {
1581 /* No need to add this to saved_regs -- it's just arg regs. */
1582 continue;
1583 }
d4473757
KB
1584 else if ((insn & 0xfffff000) == 0xe24cb000) /* sub fp, ip #n */
1585 {
94c30b78
MS
1586 unsigned imm = insn & 0xff; /* immediate value */
1587 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
d4473757 1588 imm = (imm >> rot) | (imm << (32 - rot));
4be43953 1589 regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
d4473757
KB
1590 }
1591 else if ((insn & 0xfffff000) == 0xe24dd000) /* sub sp, sp #n */
1592 {
94c30b78
MS
1593 unsigned imm = insn & 0xff; /* immediate value */
1594 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
d4473757 1595 imm = (imm >> rot) | (imm << (32 - rot));
4be43953 1596 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
d4473757 1597 }
0963b4bd
MS
1598 else if ((insn & 0xffff7fff) == 0xed6d0103 /* stfe f?,
1599 [sp, -#c]! */
2af46ca0 1600 && gdbarch_tdep (gdbarch)->have_fpa_registers)
d4473757 1601 {
f7b7ed97 1602 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
1603 break;
1604
1605 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
34e8f22d 1606 regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
f7b7ed97 1607 stack.store (regs[ARM_SP_REGNUM], 12, regs[regno]);
d4473757 1608 }
0963b4bd
MS
1609 else if ((insn & 0xffbf0fff) == 0xec2d0200 /* sfmfd f0, 4,
1610 [sp!] */
2af46ca0 1611 && gdbarch_tdep (gdbarch)->have_fpa_registers)
d4473757
KB
1612 {
1613 int n_saved_fp_regs;
1614 unsigned int fp_start_reg, fp_bound_reg;
1615
f7b7ed97 1616 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
1617 break;
1618
94c30b78 1619 if ((insn & 0x800) == 0x800) /* N0 is set */
96baa820 1620 {
d4473757
KB
1621 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1622 n_saved_fp_regs = 3;
1623 else
1624 n_saved_fp_regs = 1;
96baa820 1625 }
d4473757 1626 else
96baa820 1627 {
d4473757
KB
1628 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1629 n_saved_fp_regs = 2;
1630 else
1631 n_saved_fp_regs = 4;
96baa820 1632 }
d4473757 1633
34e8f22d 1634 fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
d4473757
KB
1635 fp_bound_reg = fp_start_reg + n_saved_fp_regs;
1636 for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
96baa820 1637 {
4be43953 1638 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
f7b7ed97
TT
1639 stack.store (regs[ARM_SP_REGNUM], 12,
1640 regs[fp_start_reg++]);
96baa820 1641 }
c906108c 1642 }
0d39a070
DJ
1643 else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
1644 {
1645 /* Allow some special function calls when skipping the
1646 prologue; GCC generates these before storing arguments to
1647 the stack. */
1648 CORE_ADDR dest = BranchDest (current_pc, insn);
1649
e0634ccf 1650 if (skip_prologue_function (gdbarch, dest, 0))
0d39a070
DJ
1651 continue;
1652 else
1653 break;
1654 }
d4473757 1655 else if ((insn & 0xf0000000) != 0xe0000000)
0963b4bd 1656 break; /* Condition not true, exit early. */
0d39a070
DJ
1657 else if (arm_instruction_changes_pc (insn))
1658 /* Don't scan past anything that might change control flow. */
1659 break;
f303bc3e
YQ
1660 else if (arm_instruction_restores_sp (insn))
1661 {
1662 /* Don't scan past the epilogue. */
1663 break;
1664 }
d19f7eee
UW
1665 else if ((insn & 0xfe500000) == 0xe8100000 /* ldm */
1666 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1667 /* Ignore block loads from the stack, potentially copying
1668 parameters from memory. */
1669 continue;
1670 else if ((insn & 0xfc500000) == 0xe4100000
1671 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1672 /* Similarly ignore single loads from the stack. */
1673 continue;
0d39a070
DJ
1674 else if ((insn & 0xffff0ff0) == 0xe1a00000)
1675 /* MOV Rd, Rm. Skip register copies, i.e. saves to another
1676 register instead of the stack. */
d4473757 1677 continue;
0d39a070
DJ
1678 else
1679 {
21daaaaf
YQ
1680 /* The optimizer might shove anything into the prologue, if
1681 we build up cache (cache != NULL) from scanning prologue,
1682 we just skip what we don't recognize and scan further to
1683 make cache as complete as possible. However, if we skip
1684 prologue, we'll stop immediately on unrecognized
1685 instruction. */
0d39a070 1686 unrecognized_pc = current_pc;
21daaaaf
YQ
1687 if (cache != NULL)
1688 continue;
1689 else
1690 break;
0d39a070 1691 }
c906108c
SS
1692 }
1693
0d39a070
DJ
1694 if (unrecognized_pc == 0)
1695 unrecognized_pc = current_pc;
1696
0d39a070
DJ
1697 if (cache)
1698 {
4072f920
YQ
1699 int framereg, framesize;
1700
1701 /* The frame size is just the distance from the frame register
1702 to the original stack pointer. */
1703 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1704 {
1705 /* Frame pointer is fp. */
1706 framereg = ARM_FP_REGNUM;
1707 framesize = -regs[ARM_FP_REGNUM].k;
1708 }
1709 else
1710 {
1711 /* Try the stack pointer... this is a bit desperate. */
1712 framereg = ARM_SP_REGNUM;
1713 framesize = -regs[ARM_SP_REGNUM].k;
1714 }
1715
0d39a070
DJ
1716 cache->framereg = framereg;
1717 cache->framesize = framesize;
1718
1719 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
f7b7ed97 1720 if (stack.find_reg (gdbarch, regno, &offset))
0d39a070
DJ
1721 cache->saved_regs[regno].addr = offset;
1722 }
1723
1724 if (arm_debug)
1725 fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1726 paddress (gdbarch, unrecognized_pc));
4be43953 1727
0d39a070
DJ
1728 return unrecognized_pc;
1729}
1730
1731static void
1732arm_scan_prologue (struct frame_info *this_frame,
1733 struct arm_prologue_cache *cache)
1734{
1735 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1736 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
bec2ab5a 1737 CORE_ADDR prologue_start, prologue_end;
0d39a070
DJ
1738 CORE_ADDR prev_pc = get_frame_pc (this_frame);
1739 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
0d39a070
DJ
1740
1741 /* Assume there is no frame until proven otherwise. */
1742 cache->framereg = ARM_SP_REGNUM;
1743 cache->framesize = 0;
1744
1745 /* Check for Thumb prologue. */
1746 if (arm_frame_is_thumb (this_frame))
1747 {
1748 thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
1749 return;
1750 }
1751
1752 /* Find the function prologue. If we can't find the function in
1753 the symbol table, peek in the stack frame to find the PC. */
1754 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1755 &prologue_end))
1756 {
1757 /* One way to find the end of the prologue (which works well
1758 for unoptimized code) is to do the following:
1759
1760 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
1761
1762 if (sal.line == 0)
1763 prologue_end = prev_pc;
1764 else if (sal.end < prologue_end)
1765 prologue_end = sal.end;
1766
1767 This mechanism is very accurate so long as the optimizer
1768 doesn't move any instructions from the function body into the
1769 prologue. If this happens, sal.end will be the last
1770 instruction in the first hunk of prologue code just before
1771 the first instruction that the scheduler has moved from
1772 the body to the prologue.
1773
1774 In order to make sure that we scan all of the prologue
1775 instructions, we use a slightly less accurate mechanism which
1776 may scan more than necessary. To help compensate for this
1777 lack of accuracy, the prologue scanning loop below contains
1778 several clauses which'll cause the loop to terminate early if
1779 an implausible prologue instruction is encountered.
1780
1781 The expression
1782
1783 prologue_start + 64
1784
1785 is a suitable endpoint since it accounts for the largest
1786 possible prologue plus up to five instructions inserted by
1787 the scheduler. */
1788
1789 if (prologue_end > prologue_start + 64)
1790 {
1791 prologue_end = prologue_start + 64; /* See above. */
1792 }
1793 }
1794 else
1795 {
1796 /* We have no symbol information. Our only option is to assume this
1797 function has a standard stack frame and the normal frame register.
1798 Then, we can find the value of our frame pointer on entrance to
1799 the callee (or at the present moment if this is the innermost frame).
1800 The value stored there should be the address of the stmfd + 8. */
1801 CORE_ADDR frame_loc;
7913a64c 1802 ULONGEST return_value;
0d39a070 1803
9e237747
MM
1804 /* AAPCS does not use a frame register, so we can abort here. */
1805 if (gdbarch_tdep (gdbarch)->arm_abi == ARM_ABI_AAPCS)
1806 return;
1807
0d39a070 1808 frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
7913a64c
YQ
1809 if (!safe_read_memory_unsigned_integer (frame_loc, 4, byte_order,
1810 &return_value))
0d39a070
DJ
1811 return;
1812 else
1813 {
1814 prologue_start = gdbarch_addr_bits_remove
1815 (gdbarch, return_value) - 8;
1816 prologue_end = prologue_start + 64; /* See above. */
1817 }
1818 }
1819
1820 if (prev_pc < prologue_end)
1821 prologue_end = prev_pc;
1822
1823 arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
1824}
1825
eb5492fa 1826static struct arm_prologue_cache *
a262aec2 1827arm_make_prologue_cache (struct frame_info *this_frame)
c906108c 1828{
eb5492fa
DJ
1829 int reg;
1830 struct arm_prologue_cache *cache;
1831 CORE_ADDR unwound_fp;
c5aa993b 1832
35d5d4ee 1833 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
a262aec2 1834 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
c906108c 1835
a262aec2 1836 arm_scan_prologue (this_frame, cache);
848cfffb 1837
a262aec2 1838 unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
eb5492fa
DJ
1839 if (unwound_fp == 0)
1840 return cache;
c906108c 1841
4be43953 1842 cache->prev_sp = unwound_fp + cache->framesize;
c906108c 1843
eb5492fa
DJ
1844 /* Calculate actual addresses of saved registers using offsets
1845 determined by arm_scan_prologue. */
a262aec2 1846 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
e28a332c 1847 if (trad_frame_addr_p (cache->saved_regs, reg))
eb5492fa
DJ
1848 cache->saved_regs[reg].addr += cache->prev_sp;
1849
1850 return cache;
c906108c
SS
1851}
1852
c1ee9414
LM
1853/* Implementation of the stop_reason hook for arm_prologue frames. */
1854
1855static enum unwind_stop_reason
1856arm_prologue_unwind_stop_reason (struct frame_info *this_frame,
1857 void **this_cache)
1858{
1859 struct arm_prologue_cache *cache;
1860 CORE_ADDR pc;
1861
1862 if (*this_cache == NULL)
1863 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 1864 cache = (struct arm_prologue_cache *) *this_cache;
c1ee9414
LM
1865
1866 /* This is meant to halt the backtrace at "_start". */
1867 pc = get_frame_pc (this_frame);
1868 if (pc <= gdbarch_tdep (get_frame_arch (this_frame))->lowest_pc)
1869 return UNWIND_OUTERMOST;
1870
1871 /* If we've hit a wall, stop. */
1872 if (cache->prev_sp == 0)
1873 return UNWIND_OUTERMOST;
1874
1875 return UNWIND_NO_REASON;
1876}
1877
eb5492fa
DJ
1878/* Our frame ID for a normal frame is the current function's starting PC
1879 and the caller's SP when we were called. */
c906108c 1880
148754e5 1881static void
a262aec2 1882arm_prologue_this_id (struct frame_info *this_frame,
eb5492fa
DJ
1883 void **this_cache,
1884 struct frame_id *this_id)
c906108c 1885{
eb5492fa
DJ
1886 struct arm_prologue_cache *cache;
1887 struct frame_id id;
2c404490 1888 CORE_ADDR pc, func;
f079148d 1889
eb5492fa 1890 if (*this_cache == NULL)
a262aec2 1891 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 1892 cache = (struct arm_prologue_cache *) *this_cache;
2a451106 1893
0e9e9abd
UW
1894 /* Use function start address as part of the frame ID. If we cannot
1895 identify the start address (due to missing symbol information),
1896 fall back to just using the current PC. */
c1ee9414 1897 pc = get_frame_pc (this_frame);
2c404490 1898 func = get_frame_func (this_frame);
0e9e9abd
UW
1899 if (!func)
1900 func = pc;
1901
eb5492fa 1902 id = frame_id_build (cache->prev_sp, func);
eb5492fa 1903 *this_id = id;
c906108c
SS
1904}
1905
a262aec2
DJ
1906static struct value *
1907arm_prologue_prev_register (struct frame_info *this_frame,
eb5492fa 1908 void **this_cache,
a262aec2 1909 int prev_regnum)
24de872b 1910{
24568a2c 1911 struct gdbarch *gdbarch = get_frame_arch (this_frame);
24de872b
DJ
1912 struct arm_prologue_cache *cache;
1913
eb5492fa 1914 if (*this_cache == NULL)
a262aec2 1915 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 1916 cache = (struct arm_prologue_cache *) *this_cache;
24de872b 1917
eb5492fa 1918 /* If we are asked to unwind the PC, then we need to return the LR
b39cc962
DJ
1919 instead. The prologue may save PC, but it will point into this
1920 frame's prologue, not the next frame's resume location. Also
1921 strip the saved T bit. A valid LR may have the low bit set, but
1922 a valid PC never does. */
eb5492fa 1923 if (prev_regnum == ARM_PC_REGNUM)
b39cc962
DJ
1924 {
1925 CORE_ADDR lr;
1926
1927 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
1928 return frame_unwind_got_constant (this_frame, prev_regnum,
24568a2c 1929 arm_addr_bits_remove (gdbarch, lr));
b39cc962 1930 }
24de872b 1931
eb5492fa 1932 /* SP is generally not saved to the stack, but this frame is
a262aec2 1933 identified by the next frame's stack pointer at the time of the call.
eb5492fa
DJ
1934 The value was already reconstructed into PREV_SP. */
1935 if (prev_regnum == ARM_SP_REGNUM)
a262aec2 1936 return frame_unwind_got_constant (this_frame, prev_regnum, cache->prev_sp);
eb5492fa 1937
b39cc962
DJ
1938 /* The CPSR may have been changed by the call instruction and by the
1939 called function. The only bit we can reconstruct is the T bit,
1940 by checking the low bit of LR as of the call. This is a reliable
1941 indicator of Thumb-ness except for some ARM v4T pre-interworking
1942 Thumb code, which could get away with a clear low bit as long as
1943 the called function did not use bx. Guess that all other
1944 bits are unchanged; the condition flags are presumably lost,
1945 but the processor status is likely valid. */
1946 if (prev_regnum == ARM_PS_REGNUM)
1947 {
1948 CORE_ADDR lr, cpsr;
9779414d 1949 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
b39cc962
DJ
1950
1951 cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
1952 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
1953 if (IS_THUMB_ADDR (lr))
9779414d 1954 cpsr |= t_bit;
b39cc962 1955 else
9779414d 1956 cpsr &= ~t_bit;
b39cc962
DJ
1957 return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
1958 }
1959
a262aec2
DJ
1960 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
1961 prev_regnum);
eb5492fa
DJ
1962}
1963
1964struct frame_unwind arm_prologue_unwind = {
1965 NORMAL_FRAME,
c1ee9414 1966 arm_prologue_unwind_stop_reason,
eb5492fa 1967 arm_prologue_this_id,
a262aec2
DJ
1968 arm_prologue_prev_register,
1969 NULL,
1970 default_frame_sniffer
eb5492fa
DJ
1971};
1972
0e9e9abd
UW
1973/* Maintain a list of ARM exception table entries per objfile, similar to the
1974 list of mapping symbols. We only cache entries for standard ARM-defined
1975 personality routines; the cache will contain only the frame unwinding
1976 instructions associated with the entry (not the descriptors). */
1977
1978static const struct objfile_data *arm_exidx_data_key;
1979
1980struct arm_exidx_entry
1981{
1982 bfd_vma addr;
1983 gdb_byte *entry;
1984};
1985typedef struct arm_exidx_entry arm_exidx_entry_s;
1986DEF_VEC_O(arm_exidx_entry_s);
1987
1988struct arm_exidx_data
1989{
1990 VEC(arm_exidx_entry_s) **section_maps;
1991};
1992
1993static void
1994arm_exidx_data_free (struct objfile *objfile, void *arg)
1995{
9a3c8263 1996 struct arm_exidx_data *data = (struct arm_exidx_data *) arg;
0e9e9abd
UW
1997 unsigned int i;
1998
1999 for (i = 0; i < objfile->obfd->section_count; i++)
2000 VEC_free (arm_exidx_entry_s, data->section_maps[i]);
2001}
2002
2003static inline int
2004arm_compare_exidx_entries (const struct arm_exidx_entry *lhs,
2005 const struct arm_exidx_entry *rhs)
2006{
2007 return lhs->addr < rhs->addr;
2008}
2009
2010static struct obj_section *
2011arm_obj_section_from_vma (struct objfile *objfile, bfd_vma vma)
2012{
2013 struct obj_section *osect;
2014
2015 ALL_OBJFILE_OSECTIONS (objfile, osect)
2016 if (bfd_get_section_flags (objfile->obfd,
2017 osect->the_bfd_section) & SEC_ALLOC)
2018 {
2019 bfd_vma start, size;
2020 start = bfd_get_section_vma (objfile->obfd, osect->the_bfd_section);
2021 size = bfd_get_section_size (osect->the_bfd_section);
2022
2023 if (start <= vma && vma < start + size)
2024 return osect;
2025 }
2026
2027 return NULL;
2028}
2029
2030/* Parse contents of exception table and exception index sections
2031 of OBJFILE, and fill in the exception table entry cache.
2032
2033 For each entry that refers to a standard ARM-defined personality
2034 routine, extract the frame unwinding instructions (from either
2035 the index or the table section). The unwinding instructions
2036 are normalized by:
2037 - extracting them from the rest of the table data
2038 - converting to host endianness
2039 - appending the implicit 0xb0 ("Finish") code
2040
2041 The extracted and normalized instructions are stored for later
2042 retrieval by the arm_find_exidx_entry routine. */
2043
2044static void
2045arm_exidx_new_objfile (struct objfile *objfile)
2046{
0e9e9abd
UW
2047 struct arm_exidx_data *data;
2048 asection *exidx, *extab;
2049 bfd_vma exidx_vma = 0, extab_vma = 0;
0e9e9abd
UW
2050 LONGEST i;
2051
2052 /* If we've already touched this file, do nothing. */
2053 if (!objfile || objfile_data (objfile, arm_exidx_data_key) != NULL)
2054 return;
2055
2056 /* Read contents of exception table and index. */
a5eda10c 2057 exidx = bfd_get_section_by_name (objfile->obfd, ELF_STRING_ARM_unwind);
984c7238 2058 gdb::byte_vector exidx_data;
0e9e9abd
UW
2059 if (exidx)
2060 {
2061 exidx_vma = bfd_section_vma (objfile->obfd, exidx);
984c7238 2062 exidx_data.resize (bfd_get_section_size (exidx));
0e9e9abd
UW
2063
2064 if (!bfd_get_section_contents (objfile->obfd, exidx,
984c7238
TT
2065 exidx_data.data (), 0,
2066 exidx_data.size ()))
2067 return;
0e9e9abd
UW
2068 }
2069
2070 extab = bfd_get_section_by_name (objfile->obfd, ".ARM.extab");
984c7238 2071 gdb::byte_vector extab_data;
0e9e9abd
UW
2072 if (extab)
2073 {
2074 extab_vma = bfd_section_vma (objfile->obfd, extab);
984c7238 2075 extab_data.resize (bfd_get_section_size (extab));
0e9e9abd
UW
2076
2077 if (!bfd_get_section_contents (objfile->obfd, extab,
984c7238
TT
2078 extab_data.data (), 0,
2079 extab_data.size ()))
2080 return;
0e9e9abd
UW
2081 }
2082
2083 /* Allocate exception table data structure. */
2084 data = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct arm_exidx_data);
2085 set_objfile_data (objfile, arm_exidx_data_key, data);
2086 data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
2087 objfile->obfd->section_count,
2088 VEC(arm_exidx_entry_s) *);
2089
2090 /* Fill in exception table. */
984c7238 2091 for (i = 0; i < exidx_data.size () / 8; i++)
0e9e9abd
UW
2092 {
2093 struct arm_exidx_entry new_exidx_entry;
984c7238
TT
2094 bfd_vma idx = bfd_h_get_32 (objfile->obfd, exidx_data.data () + i * 8);
2095 bfd_vma val = bfd_h_get_32 (objfile->obfd,
2096 exidx_data.data () + i * 8 + 4);
0e9e9abd
UW
2097 bfd_vma addr = 0, word = 0;
2098 int n_bytes = 0, n_words = 0;
2099 struct obj_section *sec;
2100 gdb_byte *entry = NULL;
2101
2102 /* Extract address of start of function. */
2103 idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2104 idx += exidx_vma + i * 8;
2105
2106 /* Find section containing function and compute section offset. */
2107 sec = arm_obj_section_from_vma (objfile, idx);
2108 if (sec == NULL)
2109 continue;
2110 idx -= bfd_get_section_vma (objfile->obfd, sec->the_bfd_section);
2111
2112 /* Determine address of exception table entry. */
2113 if (val == 1)
2114 {
2115 /* EXIDX_CANTUNWIND -- no exception table entry present. */
2116 }
2117 else if ((val & 0xff000000) == 0x80000000)
2118 {
2119 /* Exception table entry embedded in .ARM.exidx
2120 -- must be short form. */
2121 word = val;
2122 n_bytes = 3;
2123 }
2124 else if (!(val & 0x80000000))
2125 {
2126 /* Exception table entry in .ARM.extab. */
2127 addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2128 addr += exidx_vma + i * 8 + 4;
2129
984c7238 2130 if (addr >= extab_vma && addr + 4 <= extab_vma + extab_data.size ())
0e9e9abd
UW
2131 {
2132 word = bfd_h_get_32 (objfile->obfd,
984c7238 2133 extab_data.data () + addr - extab_vma);
0e9e9abd
UW
2134 addr += 4;
2135
2136 if ((word & 0xff000000) == 0x80000000)
2137 {
2138 /* Short form. */
2139 n_bytes = 3;
2140 }
2141 else if ((word & 0xff000000) == 0x81000000
2142 || (word & 0xff000000) == 0x82000000)
2143 {
2144 /* Long form. */
2145 n_bytes = 2;
2146 n_words = ((word >> 16) & 0xff);
2147 }
2148 else if (!(word & 0x80000000))
2149 {
2150 bfd_vma pers;
2151 struct obj_section *pers_sec;
2152 int gnu_personality = 0;
2153
2154 /* Custom personality routine. */
2155 pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2156 pers = UNMAKE_THUMB_ADDR (pers + addr - 4);
2157
2158 /* Check whether we've got one of the variants of the
2159 GNU personality routines. */
2160 pers_sec = arm_obj_section_from_vma (objfile, pers);
2161 if (pers_sec)
2162 {
2163 static const char *personality[] =
2164 {
2165 "__gcc_personality_v0",
2166 "__gxx_personality_v0",
2167 "__gcj_personality_v0",
2168 "__gnu_objc_personality_v0",
2169 NULL
2170 };
2171
2172 CORE_ADDR pc = pers + obj_section_offset (pers_sec);
2173 int k;
2174
2175 for (k = 0; personality[k]; k++)
2176 if (lookup_minimal_symbol_by_pc_name
2177 (pc, personality[k], objfile))
2178 {
2179 gnu_personality = 1;
2180 break;
2181 }
2182 }
2183
2184 /* If so, the next word contains a word count in the high
2185 byte, followed by the same unwind instructions as the
2186 pre-defined forms. */
2187 if (gnu_personality
984c7238 2188 && addr + 4 <= extab_vma + extab_data.size ())
0e9e9abd
UW
2189 {
2190 word = bfd_h_get_32 (objfile->obfd,
984c7238
TT
2191 (extab_data.data ()
2192 + addr - extab_vma));
0e9e9abd
UW
2193 addr += 4;
2194 n_bytes = 3;
2195 n_words = ((word >> 24) & 0xff);
2196 }
2197 }
2198 }
2199 }
2200
2201 /* Sanity check address. */
2202 if (n_words)
984c7238
TT
2203 if (addr < extab_vma
2204 || addr + 4 * n_words > extab_vma + extab_data.size ())
0e9e9abd
UW
2205 n_words = n_bytes = 0;
2206
2207 /* The unwind instructions reside in WORD (only the N_BYTES least
2208 significant bytes are valid), followed by N_WORDS words in the
2209 extab section starting at ADDR. */
2210 if (n_bytes || n_words)
2211 {
224c3ddb
SM
2212 gdb_byte *p = entry
2213 = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack,
2214 n_bytes + n_words * 4 + 1);
0e9e9abd
UW
2215
2216 while (n_bytes--)
2217 *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);
2218
2219 while (n_words--)
2220 {
2221 word = bfd_h_get_32 (objfile->obfd,
984c7238 2222 extab_data.data () + addr - extab_vma);
0e9e9abd
UW
2223 addr += 4;
2224
2225 *p++ = (gdb_byte) ((word >> 24) & 0xff);
2226 *p++ = (gdb_byte) ((word >> 16) & 0xff);
2227 *p++ = (gdb_byte) ((word >> 8) & 0xff);
2228 *p++ = (gdb_byte) (word & 0xff);
2229 }
2230
2231 /* Implied "Finish" to terminate the list. */
2232 *p++ = 0xb0;
2233 }
2234
2235 /* Push entry onto vector. They are guaranteed to always
2236 appear in order of increasing addresses. */
2237 new_exidx_entry.addr = idx;
2238 new_exidx_entry.entry = entry;
2239 VEC_safe_push (arm_exidx_entry_s,
2240 data->section_maps[sec->the_bfd_section->index],
2241 &new_exidx_entry);
2242 }
0e9e9abd
UW
2243}
2244
2245/* Search for the exception table entry covering MEMADDR. If one is found,
2246 return a pointer to its data. Otherwise, return 0. If START is non-NULL,
2247 set *START to the start of the region covered by this entry. */
2248
2249static gdb_byte *
2250arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
2251{
2252 struct obj_section *sec;
2253
2254 sec = find_pc_section (memaddr);
2255 if (sec != NULL)
2256 {
2257 struct arm_exidx_data *data;
2258 VEC(arm_exidx_entry_s) *map;
2259 struct arm_exidx_entry map_key = { memaddr - obj_section_addr (sec), 0 };
2260 unsigned int idx;
2261
9a3c8263
SM
2262 data = ((struct arm_exidx_data *)
2263 objfile_data (sec->objfile, arm_exidx_data_key));
0e9e9abd
UW
2264 if (data != NULL)
2265 {
2266 map = data->section_maps[sec->the_bfd_section->index];
2267 if (!VEC_empty (arm_exidx_entry_s, map))
2268 {
2269 struct arm_exidx_entry *map_sym;
2270
2271 idx = VEC_lower_bound (arm_exidx_entry_s, map, &map_key,
2272 arm_compare_exidx_entries);
2273
2274 /* VEC_lower_bound finds the earliest ordered insertion
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. */
2278 if (idx < VEC_length (arm_exidx_entry_s, map))
2279 {
2280 map_sym = VEC_index (arm_exidx_entry_s, map, idx);
2281 if (map_sym->addr == map_key.addr)
2282 {
2283 if (start)
2284 *start = map_sym->addr + obj_section_addr (sec);
2285 return map_sym->entry;
2286 }
2287 }
2288
2289 if (idx > 0)
2290 {
2291 map_sym = VEC_index (arm_exidx_entry_s, map, idx - 1);
2292 if (start)
2293 *start = map_sym->addr + obj_section_addr (sec);
2294 return map_sym->entry;
2295 }
2296 }
2297 }
2298 }
2299
2300 return NULL;
2301}
2302
2303/* Given the current frame THIS_FRAME, and its associated frame unwinding
2304 instruction list from the ARM exception table entry ENTRY, allocate and
2305 return a prologue cache structure describing how to unwind this frame.
2306
2307 Return NULL if the unwinding instruction list contains a "spare",
2308 "reserved" or "refuse to unwind" instruction as defined in section
2309 "9.3 Frame unwinding instructions" of the "Exception Handling ABI
2310 for the ARM Architecture" document. */
2311
2312static struct arm_prologue_cache *
2313arm_exidx_fill_cache (struct frame_info *this_frame, gdb_byte *entry)
2314{
2315 CORE_ADDR vsp = 0;
2316 int vsp_valid = 0;
2317
2318 struct arm_prologue_cache *cache;
2319 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2320 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2321
2322 for (;;)
2323 {
2324 gdb_byte insn;
2325
2326 /* Whenever we reload SP, we actually have to retrieve its
2327 actual value in the current frame. */
2328 if (!vsp_valid)
2329 {
2330 if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2331 {
2332 int reg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2333 vsp = get_frame_register_unsigned (this_frame, reg);
2334 }
2335 else
2336 {
2337 CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr;
2338 vsp = get_frame_memory_unsigned (this_frame, addr, 4);
2339 }
2340
2341 vsp_valid = 1;
2342 }
2343
2344 /* Decode next unwind instruction. */
2345 insn = *entry++;
2346
2347 if ((insn & 0xc0) == 0)
2348 {
2349 int offset = insn & 0x3f;
2350 vsp += (offset << 2) + 4;
2351 }
2352 else if ((insn & 0xc0) == 0x40)
2353 {
2354 int offset = insn & 0x3f;
2355 vsp -= (offset << 2) + 4;
2356 }
2357 else if ((insn & 0xf0) == 0x80)
2358 {
2359 int mask = ((insn & 0xf) << 8) | *entry++;
2360 int i;
2361
2362 /* The special case of an all-zero mask identifies
2363 "Refuse to unwind". We return NULL to fall back
2364 to the prologue analyzer. */
2365 if (mask == 0)
2366 return NULL;
2367
2368 /* Pop registers r4..r15 under mask. */
2369 for (i = 0; i < 12; i++)
2370 if (mask & (1 << i))
2371 {
2372 cache->saved_regs[4 + i].addr = vsp;
2373 vsp += 4;
2374 }
2375
2376 /* Special-case popping SP -- we need to reload vsp. */
2377 if (mask & (1 << (ARM_SP_REGNUM - 4)))
2378 vsp_valid = 0;
2379 }
2380 else if ((insn & 0xf0) == 0x90)
2381 {
2382 int reg = insn & 0xf;
2383
2384 /* Reserved cases. */
2385 if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
2386 return NULL;
2387
2388 /* Set SP from another register and mark VSP for reload. */
2389 cache->saved_regs[ARM_SP_REGNUM] = cache->saved_regs[reg];
2390 vsp_valid = 0;
2391 }
2392 else if ((insn & 0xf0) == 0xa0)
2393 {
2394 int count = insn & 0x7;
2395 int pop_lr = (insn & 0x8) != 0;
2396 int i;
2397
2398 /* Pop r4..r[4+count]. */
2399 for (i = 0; i <= count; i++)
2400 {
2401 cache->saved_regs[4 + i].addr = vsp;
2402 vsp += 4;
2403 }
2404
2405 /* If indicated by flag, pop LR as well. */
2406 if (pop_lr)
2407 {
2408 cache->saved_regs[ARM_LR_REGNUM].addr = vsp;
2409 vsp += 4;
2410 }
2411 }
2412 else if (insn == 0xb0)
2413 {
2414 /* We could only have updated PC by popping into it; if so, it
2415 will show up as address. Otherwise, copy LR into PC. */
2416 if (!trad_frame_addr_p (cache->saved_regs, ARM_PC_REGNUM))
2417 cache->saved_regs[ARM_PC_REGNUM]
2418 = cache->saved_regs[ARM_LR_REGNUM];
2419
2420 /* We're done. */
2421 break;
2422 }
2423 else if (insn == 0xb1)
2424 {
2425 int mask = *entry++;
2426 int i;
2427
2428 /* All-zero mask and mask >= 16 is "spare". */
2429 if (mask == 0 || mask >= 16)
2430 return NULL;
2431
2432 /* Pop r0..r3 under mask. */
2433 for (i = 0; i < 4; i++)
2434 if (mask & (1 << i))
2435 {
2436 cache->saved_regs[i].addr = vsp;
2437 vsp += 4;
2438 }
2439 }
2440 else if (insn == 0xb2)
2441 {
2442 ULONGEST offset = 0;
2443 unsigned shift = 0;
2444
2445 do
2446 {
2447 offset |= (*entry & 0x7f) << shift;
2448 shift += 7;
2449 }
2450 while (*entry++ & 0x80);
2451
2452 vsp += 0x204 + (offset << 2);
2453 }
2454 else if (insn == 0xb3)
2455 {
2456 int start = *entry >> 4;
2457 int count = (*entry++) & 0xf;
2458 int i;
2459
2460 /* Only registers D0..D15 are valid here. */
2461 if (start + count >= 16)
2462 return NULL;
2463
2464 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2465 for (i = 0; i <= count; i++)
2466 {
2467 cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2468 vsp += 8;
2469 }
2470
2471 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2472 vsp += 4;
2473 }
2474 else if ((insn & 0xf8) == 0xb8)
2475 {
2476 int count = insn & 0x7;
2477 int i;
2478
2479 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2480 for (i = 0; i <= count; i++)
2481 {
2482 cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2483 vsp += 8;
2484 }
2485
2486 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2487 vsp += 4;
2488 }
2489 else if (insn == 0xc6)
2490 {
2491 int start = *entry >> 4;
2492 int count = (*entry++) & 0xf;
2493 int i;
2494
2495 /* Only registers WR0..WR15 are valid. */
2496 if (start + count >= 16)
2497 return NULL;
2498
2499 /* Pop iwmmx registers WR[start]..WR[start+count]. */
2500 for (i = 0; i <= count; i++)
2501 {
2502 cache->saved_regs[ARM_WR0_REGNUM + start + i].addr = vsp;
2503 vsp += 8;
2504 }
2505 }
2506 else if (insn == 0xc7)
2507 {
2508 int mask = *entry++;
2509 int i;
2510
2511 /* All-zero mask and mask >= 16 is "spare". */
2512 if (mask == 0 || mask >= 16)
2513 return NULL;
2514
2515 /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask. */
2516 for (i = 0; i < 4; i++)
2517 if (mask & (1 << i))
2518 {
2519 cache->saved_regs[ARM_WCGR0_REGNUM + i].addr = vsp;
2520 vsp += 4;
2521 }
2522 }
2523 else if ((insn & 0xf8) == 0xc0)
2524 {
2525 int count = insn & 0x7;
2526 int i;
2527
2528 /* Pop iwmmx registers WR[10]..WR[10+count]. */
2529 for (i = 0; i <= count; i++)
2530 {
2531 cache->saved_regs[ARM_WR0_REGNUM + 10 + i].addr = vsp;
2532 vsp += 8;
2533 }
2534 }
2535 else if (insn == 0xc8)
2536 {
2537 int start = *entry >> 4;
2538 int count = (*entry++) & 0xf;
2539 int i;
2540
2541 /* Only registers D0..D31 are valid. */
2542 if (start + count >= 16)
2543 return NULL;
2544
2545 /* Pop VFP double-precision registers
2546 D[16+start]..D[16+start+count]. */
2547 for (i = 0; i <= count; i++)
2548 {
2549 cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].addr = vsp;
2550 vsp += 8;
2551 }
2552 }
2553 else if (insn == 0xc9)
2554 {
2555 int start = *entry >> 4;
2556 int count = (*entry++) & 0xf;
2557 int i;
2558
2559 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2560 for (i = 0; i <= count; i++)
2561 {
2562 cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2563 vsp += 8;
2564 }
2565 }
2566 else if ((insn & 0xf8) == 0xd0)
2567 {
2568 int count = insn & 0x7;
2569 int i;
2570
2571 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2572 for (i = 0; i <= count; i++)
2573 {
2574 cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2575 vsp += 8;
2576 }
2577 }
2578 else
2579 {
2580 /* Everything else is "spare". */
2581 return NULL;
2582 }
2583 }
2584
2585 /* If we restore SP from a register, assume this was the frame register.
2586 Otherwise just fall back to SP as frame register. */
2587 if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2588 cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2589 else
2590 cache->framereg = ARM_SP_REGNUM;
2591
2592 /* Determine offset to previous frame. */
2593 cache->framesize
2594 = vsp - get_frame_register_unsigned (this_frame, cache->framereg);
2595
2596 /* We already got the previous SP. */
2597 cache->prev_sp = vsp;
2598
2599 return cache;
2600}
2601
2602/* Unwinding via ARM exception table entries. Note that the sniffer
2603 already computes a filled-in prologue cache, which is then used
2604 with the same arm_prologue_this_id and arm_prologue_prev_register
2605 routines also used for prologue-parsing based unwinding. */
2606
2607static int
2608arm_exidx_unwind_sniffer (const struct frame_unwind *self,
2609 struct frame_info *this_frame,
2610 void **this_prologue_cache)
2611{
2612 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2613 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
2614 CORE_ADDR addr_in_block, exidx_region, func_start;
2615 struct arm_prologue_cache *cache;
2616 gdb_byte *entry;
2617
2618 /* See if we have an ARM exception table entry covering this address. */
2619 addr_in_block = get_frame_address_in_block (this_frame);
2620 entry = arm_find_exidx_entry (addr_in_block, &exidx_region);
2621 if (!entry)
2622 return 0;
2623
2624 /* The ARM exception table does not describe unwind information
2625 for arbitrary PC values, but is guaranteed to be correct only
2626 at call sites. We have to decide here whether we want to use
2627 ARM exception table information for this frame, or fall back
2628 to using prologue parsing. (Note that if we have DWARF CFI,
2629 this sniffer isn't even called -- CFI is always preferred.)
2630
2631 Before we make this decision, however, we check whether we
2632 actually have *symbol* information for the current frame.
2633 If not, prologue parsing would not work anyway, so we might
2634 as well use the exception table and hope for the best. */
2635 if (find_pc_partial_function (addr_in_block, NULL, &func_start, NULL))
2636 {
2637 int exc_valid = 0;
2638
2639 /* If the next frame is "normal", we are at a call site in this
2640 frame, so exception information is guaranteed to be valid. */
2641 if (get_next_frame (this_frame)
2642 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
2643 exc_valid = 1;
2644
2645 /* We also assume exception information is valid if we're currently
2646 blocked in a system call. The system library is supposed to
d9311bfa
AT
2647 ensure this, so that e.g. pthread cancellation works. */
2648 if (arm_frame_is_thumb (this_frame))
0e9e9abd 2649 {
7913a64c 2650 ULONGEST insn;
416dc9c6 2651
7913a64c
YQ
2652 if (safe_read_memory_unsigned_integer (get_frame_pc (this_frame) - 2,
2653 2, byte_order_for_code, &insn)
d9311bfa
AT
2654 && (insn & 0xff00) == 0xdf00 /* svc */)
2655 exc_valid = 1;
0e9e9abd 2656 }
d9311bfa
AT
2657 else
2658 {
7913a64c 2659 ULONGEST insn;
416dc9c6 2660
7913a64c
YQ
2661 if (safe_read_memory_unsigned_integer (get_frame_pc (this_frame) - 4,
2662 4, byte_order_for_code, &insn)
d9311bfa
AT
2663 && (insn & 0x0f000000) == 0x0f000000 /* svc */)
2664 exc_valid = 1;
2665 }
2666
0e9e9abd
UW
2667 /* Bail out if we don't know that exception information is valid. */
2668 if (!exc_valid)
2669 return 0;
2670
2671 /* The ARM exception index does not mark the *end* of the region
2672 covered by the entry, and some functions will not have any entry.
2673 To correctly recognize the end of the covered region, the linker
2674 should have inserted dummy records with a CANTUNWIND marker.
2675
2676 Unfortunately, current versions of GNU ld do not reliably do
2677 this, and thus we may have found an incorrect entry above.
2678 As a (temporary) sanity check, we only use the entry if it
2679 lies *within* the bounds of the function. Note that this check
2680 might reject perfectly valid entries that just happen to cover
2681 multiple functions; therefore this check ought to be removed
2682 once the linker is fixed. */
2683 if (func_start > exidx_region)
2684 return 0;
2685 }
2686
2687 /* Decode the list of unwinding instructions into a prologue cache.
2688 Note that this may fail due to e.g. a "refuse to unwind" code. */
2689 cache = arm_exidx_fill_cache (this_frame, entry);
2690 if (!cache)
2691 return 0;
2692
2693 *this_prologue_cache = cache;
2694 return 1;
2695}
2696
2697struct frame_unwind arm_exidx_unwind = {
2698 NORMAL_FRAME,
8fbca658 2699 default_frame_unwind_stop_reason,
0e9e9abd
UW
2700 arm_prologue_this_id,
2701 arm_prologue_prev_register,
2702 NULL,
2703 arm_exidx_unwind_sniffer
2704};
2705
779aa56f
YQ
2706static struct arm_prologue_cache *
2707arm_make_epilogue_frame_cache (struct frame_info *this_frame)
2708{
2709 struct arm_prologue_cache *cache;
779aa56f
YQ
2710 int reg;
2711
2712 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2713 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2714
2715 /* Still rely on the offset calculated from prologue. */
2716 arm_scan_prologue (this_frame, cache);
2717
2718 /* Since we are in epilogue, the SP has been restored. */
2719 cache->prev_sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
2720
2721 /* Calculate actual addresses of saved registers using offsets
2722 determined by arm_scan_prologue. */
2723 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
2724 if (trad_frame_addr_p (cache->saved_regs, reg))
2725 cache->saved_regs[reg].addr += cache->prev_sp;
2726
2727 return cache;
2728}
2729
2730/* Implementation of function hook 'this_id' in
2731 'struct frame_uwnind' for epilogue unwinder. */
2732
2733static void
2734arm_epilogue_frame_this_id (struct frame_info *this_frame,
2735 void **this_cache,
2736 struct frame_id *this_id)
2737{
2738 struct arm_prologue_cache *cache;
2739 CORE_ADDR pc, func;
2740
2741 if (*this_cache == NULL)
2742 *this_cache = arm_make_epilogue_frame_cache (this_frame);
2743 cache = (struct arm_prologue_cache *) *this_cache;
2744
2745 /* Use function start address as part of the frame ID. If we cannot
2746 identify the start address (due to missing symbol information),
2747 fall back to just using the current PC. */
2748 pc = get_frame_pc (this_frame);
2749 func = get_frame_func (this_frame);
fb3f3d25 2750 if (func == 0)
779aa56f
YQ
2751 func = pc;
2752
2753 (*this_id) = frame_id_build (cache->prev_sp, pc);
2754}
2755
2756/* Implementation of function hook 'prev_register' in
2757 'struct frame_uwnind' for epilogue unwinder. */
2758
2759static struct value *
2760arm_epilogue_frame_prev_register (struct frame_info *this_frame,
2761 void **this_cache, int regnum)
2762{
779aa56f
YQ
2763 if (*this_cache == NULL)
2764 *this_cache = arm_make_epilogue_frame_cache (this_frame);
779aa56f
YQ
2765
2766 return arm_prologue_prev_register (this_frame, this_cache, regnum);
2767}
2768
2769static int arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch,
2770 CORE_ADDR pc);
2771static int thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch,
2772 CORE_ADDR pc);
2773
2774/* Implementation of function hook 'sniffer' in
2775 'struct frame_uwnind' for epilogue unwinder. */
2776
2777static int
2778arm_epilogue_frame_sniffer (const struct frame_unwind *self,
2779 struct frame_info *this_frame,
2780 void **this_prologue_cache)
2781{
2782 if (frame_relative_level (this_frame) == 0)
2783 {
2784 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2785 CORE_ADDR pc = get_frame_pc (this_frame);
2786
2787 if (arm_frame_is_thumb (this_frame))
2788 return thumb_stack_frame_destroyed_p (gdbarch, pc);
2789 else
2790 return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
2791 }
2792 else
2793 return 0;
2794}
2795
2796/* Frame unwinder from epilogue. */
2797
2798static const struct frame_unwind arm_epilogue_frame_unwind =
2799{
2800 NORMAL_FRAME,
2801 default_frame_unwind_stop_reason,
2802 arm_epilogue_frame_this_id,
2803 arm_epilogue_frame_prev_register,
2804 NULL,
2805 arm_epilogue_frame_sniffer,
2806};
2807
80d8d390
YQ
2808/* Recognize GCC's trampoline for thumb call-indirect. If we are in a
2809 trampoline, return the target PC. Otherwise return 0.
2810
2811 void call0a (char c, short s, int i, long l) {}
2812
2813 int main (void)
2814 {
2815 (*pointer_to_call0a) (c, s, i, l);
2816 }
2817
2818 Instead of calling a stub library function _call_via_xx (xx is
2819 the register name), GCC may inline the trampoline in the object
2820 file as below (register r2 has the address of call0a).
2821
2822 .global main
2823 .type main, %function
2824 ...
2825 bl .L1
2826 ...
2827 .size main, .-main
2828
2829 .L1:
2830 bx r2
2831
2832 The trampoline 'bx r2' doesn't belong to main. */
2833
2834static CORE_ADDR
2835arm_skip_bx_reg (struct frame_info *frame, CORE_ADDR pc)
2836{
2837 /* The heuristics of recognizing such trampoline is that FRAME is
2838 executing in Thumb mode and the instruction on PC is 'bx Rm'. */
2839 if (arm_frame_is_thumb (frame))
2840 {
2841 gdb_byte buf[2];
2842
2843 if (target_read_memory (pc, buf, 2) == 0)
2844 {
2845 struct gdbarch *gdbarch = get_frame_arch (frame);
2846 enum bfd_endian byte_order_for_code
2847 = gdbarch_byte_order_for_code (gdbarch);
2848 uint16_t insn
2849 = extract_unsigned_integer (buf, 2, byte_order_for_code);
2850
2851 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
2852 {
2853 CORE_ADDR dest
2854 = get_frame_register_unsigned (frame, bits (insn, 3, 6));
2855
2856 /* Clear the LSB so that gdb core sets step-resume
2857 breakpoint at the right address. */
2858 return UNMAKE_THUMB_ADDR (dest);
2859 }
2860 }
2861 }
2862
2863 return 0;
2864}
2865
909cf6ea 2866static struct arm_prologue_cache *
a262aec2 2867arm_make_stub_cache (struct frame_info *this_frame)
909cf6ea 2868{
909cf6ea 2869 struct arm_prologue_cache *cache;
909cf6ea 2870
35d5d4ee 2871 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
a262aec2 2872 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
909cf6ea 2873
a262aec2 2874 cache->prev_sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
909cf6ea
DJ
2875
2876 return cache;
2877}
2878
2879/* Our frame ID for a stub frame is the current SP and LR. */
2880
2881static void
a262aec2 2882arm_stub_this_id (struct frame_info *this_frame,
909cf6ea
DJ
2883 void **this_cache,
2884 struct frame_id *this_id)
2885{
2886 struct arm_prologue_cache *cache;
2887
2888 if (*this_cache == NULL)
a262aec2 2889 *this_cache = arm_make_stub_cache (this_frame);
9a3c8263 2890 cache = (struct arm_prologue_cache *) *this_cache;
909cf6ea 2891
a262aec2 2892 *this_id = frame_id_build (cache->prev_sp, get_frame_pc (this_frame));
909cf6ea
DJ
2893}
2894
a262aec2
DJ
2895static int
2896arm_stub_unwind_sniffer (const struct frame_unwind *self,
2897 struct frame_info *this_frame,
2898 void **this_prologue_cache)
909cf6ea 2899{
93d42b30 2900 CORE_ADDR addr_in_block;
948f8e3d 2901 gdb_byte dummy[4];
18d18ac8
YQ
2902 CORE_ADDR pc, start_addr;
2903 const char *name;
909cf6ea 2904
a262aec2 2905 addr_in_block = get_frame_address_in_block (this_frame);
18d18ac8 2906 pc = get_frame_pc (this_frame);
3e5d3a5a 2907 if (in_plt_section (addr_in_block)
fc36e839
DE
2908 /* We also use the stub winder if the target memory is unreadable
2909 to avoid having the prologue unwinder trying to read it. */
18d18ac8
YQ
2910 || target_read_memory (pc, dummy, 4) != 0)
2911 return 1;
2912
2913 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0
2914 && arm_skip_bx_reg (this_frame, pc) != 0)
a262aec2 2915 return 1;
909cf6ea 2916
a262aec2 2917 return 0;
909cf6ea
DJ
2918}
2919
a262aec2
DJ
2920struct frame_unwind arm_stub_unwind = {
2921 NORMAL_FRAME,
8fbca658 2922 default_frame_unwind_stop_reason,
a262aec2
DJ
2923 arm_stub_this_id,
2924 arm_prologue_prev_register,
2925 NULL,
2926 arm_stub_unwind_sniffer
2927};
2928
2ae28aa9
YQ
2929/* Put here the code to store, into CACHE->saved_regs, the addresses
2930 of the saved registers of frame described by THIS_FRAME. CACHE is
2931 returned. */
2932
2933static struct arm_prologue_cache *
2934arm_m_exception_cache (struct frame_info *this_frame)
2935{
2936 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2937 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2938 struct arm_prologue_cache *cache;
2939 CORE_ADDR unwound_sp;
2940 LONGEST xpsr;
2941
2942 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2943 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2944
2945 unwound_sp = get_frame_register_unsigned (this_frame,
2946 ARM_SP_REGNUM);
2947
2948 /* The hardware saves eight 32-bit words, comprising xPSR,
2949 ReturnAddress, LR (R14), R12, R3, R2, R1, R0. See details in
2950 "B1.5.6 Exception entry behavior" in
2951 "ARMv7-M Architecture Reference Manual". */
2952 cache->saved_regs[0].addr = unwound_sp;
2953 cache->saved_regs[1].addr = unwound_sp + 4;
2954 cache->saved_regs[2].addr = unwound_sp + 8;
2955 cache->saved_regs[3].addr = unwound_sp + 12;
2956 cache->saved_regs[12].addr = unwound_sp + 16;
2957 cache->saved_regs[14].addr = unwound_sp + 20;
2958 cache->saved_regs[15].addr = unwound_sp + 24;
2959 cache->saved_regs[ARM_PS_REGNUM].addr = unwound_sp + 28;
2960
2961 /* If bit 9 of the saved xPSR is set, then there is a four-byte
2962 aligner between the top of the 32-byte stack frame and the
2963 previous context's stack pointer. */
2964 cache->prev_sp = unwound_sp + 32;
2965 if (safe_read_memory_integer (unwound_sp + 28, 4, byte_order, &xpsr)
2966 && (xpsr & (1 << 9)) != 0)
2967 cache->prev_sp += 4;
2968
2969 return cache;
2970}
2971
2972/* Implementation of function hook 'this_id' in
2973 'struct frame_uwnind'. */
2974
2975static void
2976arm_m_exception_this_id (struct frame_info *this_frame,
2977 void **this_cache,
2978 struct frame_id *this_id)
2979{
2980 struct arm_prologue_cache *cache;
2981
2982 if (*this_cache == NULL)
2983 *this_cache = arm_m_exception_cache (this_frame);
9a3c8263 2984 cache = (struct arm_prologue_cache *) *this_cache;
2ae28aa9
YQ
2985
2986 /* Our frame ID for a stub frame is the current SP and LR. */
2987 *this_id = frame_id_build (cache->prev_sp,
2988 get_frame_pc (this_frame));
2989}
2990
2991/* Implementation of function hook 'prev_register' in
2992 'struct frame_uwnind'. */
2993
2994static struct value *
2995arm_m_exception_prev_register (struct frame_info *this_frame,
2996 void **this_cache,
2997 int prev_regnum)
2998{
2ae28aa9
YQ
2999 struct arm_prologue_cache *cache;
3000
3001 if (*this_cache == NULL)
3002 *this_cache = arm_m_exception_cache (this_frame);
9a3c8263 3003 cache = (struct arm_prologue_cache *) *this_cache;
2ae28aa9
YQ
3004
3005 /* The value was already reconstructed into PREV_SP. */
3006 if (prev_regnum == ARM_SP_REGNUM)
3007 return frame_unwind_got_constant (this_frame, prev_regnum,
3008 cache->prev_sp);
3009
3010 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
3011 prev_regnum);
3012}
3013
3014/* Implementation of function hook 'sniffer' in
3015 'struct frame_uwnind'. */
3016
3017static int
3018arm_m_exception_unwind_sniffer (const struct frame_unwind *self,
3019 struct frame_info *this_frame,
3020 void **this_prologue_cache)
3021{
3022 CORE_ADDR this_pc = get_frame_pc (this_frame);
3023
3024 /* No need to check is_m; this sniffer is only registered for
3025 M-profile architectures. */
3026
ca90e760
FH
3027 /* Check if exception frame returns to a magic PC value. */
3028 return arm_m_addr_is_magic (this_pc);
2ae28aa9
YQ
3029}
3030
3031/* Frame unwinder for M-profile exceptions. */
3032
3033struct frame_unwind arm_m_exception_unwind =
3034{
3035 SIGTRAMP_FRAME,
3036 default_frame_unwind_stop_reason,
3037 arm_m_exception_this_id,
3038 arm_m_exception_prev_register,
3039 NULL,
3040 arm_m_exception_unwind_sniffer
3041};
3042
24de872b 3043static CORE_ADDR
a262aec2 3044arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
24de872b
DJ
3045{
3046 struct arm_prologue_cache *cache;
3047
eb5492fa 3048 if (*this_cache == NULL)
a262aec2 3049 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 3050 cache = (struct arm_prologue_cache *) *this_cache;
eb5492fa 3051
4be43953 3052 return cache->prev_sp - cache->framesize;
24de872b
DJ
3053}
3054
eb5492fa
DJ
3055struct frame_base arm_normal_base = {
3056 &arm_prologue_unwind,
3057 arm_normal_frame_base,
3058 arm_normal_frame_base,
3059 arm_normal_frame_base
3060};
3061
b39cc962
DJ
3062static struct value *
3063arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
3064 int regnum)
3065{
24568a2c 3066 struct gdbarch * gdbarch = get_frame_arch (this_frame);
b39cc962 3067 CORE_ADDR lr, cpsr;
9779414d 3068 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
b39cc962
DJ
3069
3070 switch (regnum)
3071 {
3072 case ARM_PC_REGNUM:
3073 /* The PC is normally copied from the return column, which
3074 describes saves of LR. However, that version may have an
3075 extra bit set to indicate Thumb state. The bit is not
3076 part of the PC. */
3077 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3078 return frame_unwind_got_constant (this_frame, regnum,
24568a2c 3079 arm_addr_bits_remove (gdbarch, lr));
b39cc962
DJ
3080
3081 case ARM_PS_REGNUM:
3082 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
ca38c58e 3083 cpsr = get_frame_register_unsigned (this_frame, regnum);
b39cc962
DJ
3084 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3085 if (IS_THUMB_ADDR (lr))
9779414d 3086 cpsr |= t_bit;
b39cc962 3087 else
9779414d 3088 cpsr &= ~t_bit;
ca38c58e 3089 return frame_unwind_got_constant (this_frame, regnum, cpsr);
b39cc962
DJ
3090
3091 default:
3092 internal_error (__FILE__, __LINE__,
3093 _("Unexpected register %d"), regnum);
3094 }
3095}
3096
3097static void
3098arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
3099 struct dwarf2_frame_state_reg *reg,
3100 struct frame_info *this_frame)
3101{
3102 switch (regnum)
3103 {
3104 case ARM_PC_REGNUM:
3105 case ARM_PS_REGNUM:
3106 reg->how = DWARF2_FRAME_REG_FN;
3107 reg->loc.fn = arm_dwarf2_prev_register;
3108 break;
3109 case ARM_SP_REGNUM:
3110 reg->how = DWARF2_FRAME_REG_CFA;
3111 break;
3112 }
3113}
3114
c9cf6e20 3115/* Implement the stack_frame_destroyed_p gdbarch method. */
4024ca99
UW
3116
3117static int
c9cf6e20 3118thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
4024ca99
UW
3119{
3120 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3121 unsigned int insn, insn2;
3122 int found_return = 0, found_stack_adjust = 0;
3123 CORE_ADDR func_start, func_end;
3124 CORE_ADDR scan_pc;
3125 gdb_byte buf[4];
3126
3127 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3128 return 0;
3129
3130 /* The epilogue is a sequence of instructions along the following lines:
3131
3132 - add stack frame size to SP or FP
3133 - [if frame pointer used] restore SP from FP
3134 - restore registers from SP [may include PC]
3135 - a return-type instruction [if PC wasn't already restored]
3136
3137 In a first pass, we scan forward from the current PC and verify the
3138 instructions we find as compatible with this sequence, ending in a
3139 return instruction.
3140
3141 However, this is not sufficient to distinguish indirect function calls
3142 within a function from indirect tail calls in the epilogue in some cases.
3143 Therefore, if we didn't already find any SP-changing instruction during
3144 forward scan, we add a backward scanning heuristic to ensure we actually
3145 are in the epilogue. */
3146
3147 scan_pc = pc;
3148 while (scan_pc < func_end && !found_return)
3149 {
3150 if (target_read_memory (scan_pc, buf, 2))
3151 break;
3152
3153 scan_pc += 2;
3154 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3155
3156 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
3157 found_return = 1;
3158 else if (insn == 0x46f7) /* mov pc, lr */
3159 found_return = 1;
540314bd 3160 else if (thumb_instruction_restores_sp (insn))
4024ca99 3161 {
b7576e5c 3162 if ((insn & 0xff00) == 0xbd00) /* pop <registers, PC> */
4024ca99
UW
3163 found_return = 1;
3164 }
db24da6d 3165 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instruction */
4024ca99
UW
3166 {
3167 if (target_read_memory (scan_pc, buf, 2))
3168 break;
3169
3170 scan_pc += 2;
3171 insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3172
3173 if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3174 {
4024ca99
UW
3175 if (insn2 & 0x8000) /* <registers> include PC. */
3176 found_return = 1;
3177 }
3178 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3179 && (insn2 & 0x0fff) == 0x0b04)
3180 {
4024ca99
UW
3181 if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC. */
3182 found_return = 1;
3183 }
3184 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3185 && (insn2 & 0x0e00) == 0x0a00)
6b65d1b6 3186 ;
4024ca99
UW
3187 else
3188 break;
3189 }
3190 else
3191 break;
3192 }
3193
3194 if (!found_return)
3195 return 0;
3196
3197 /* Since any instruction in the epilogue sequence, with the possible
3198 exception of return itself, updates the stack pointer, we need to
3199 scan backwards for at most one instruction. Try either a 16-bit or
3200 a 32-bit instruction. This is just a heuristic, so we do not worry
0963b4bd 3201 too much about false positives. */
4024ca99 3202
6b65d1b6
YQ
3203 if (pc - 4 < func_start)
3204 return 0;
3205 if (target_read_memory (pc - 4, buf, 4))
3206 return 0;
4024ca99 3207
6b65d1b6
YQ
3208 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3209 insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);
3210
3211 if (thumb_instruction_restores_sp (insn2))
3212 found_stack_adjust = 1;
3213 else if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3214 found_stack_adjust = 1;
3215 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3216 && (insn2 & 0x0fff) == 0x0b04)
3217 found_stack_adjust = 1;
3218 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3219 && (insn2 & 0x0e00) == 0x0a00)
3220 found_stack_adjust = 1;
4024ca99
UW
3221
3222 return found_stack_adjust;
3223}
3224
4024ca99 3225static int
c58b006a 3226arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch, CORE_ADDR pc)
4024ca99
UW
3227{
3228 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3229 unsigned int insn;
f303bc3e 3230 int found_return;
4024ca99
UW
3231 CORE_ADDR func_start, func_end;
3232
4024ca99
UW
3233 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3234 return 0;
3235
3236 /* We are in the epilogue if the previous instruction was a stack
3237 adjustment and the next instruction is a possible return (bx, mov
3238 pc, or pop). We could have to scan backwards to find the stack
3239 adjustment, or forwards to find the return, but this is a decent
3240 approximation. First scan forwards. */
3241
3242 found_return = 0;
3243 insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
3244 if (bits (insn, 28, 31) != INST_NV)
3245 {
3246 if ((insn & 0x0ffffff0) == 0x012fff10)
3247 /* BX. */
3248 found_return = 1;
3249 else if ((insn & 0x0ffffff0) == 0x01a0f000)
3250 /* MOV PC. */
3251 found_return = 1;
3252 else if ((insn & 0x0fff0000) == 0x08bd0000
3253 && (insn & 0x0000c000) != 0)
3254 /* POP (LDMIA), including PC or LR. */
3255 found_return = 1;
3256 }
3257
3258 if (!found_return)
3259 return 0;
3260
3261 /* Scan backwards. This is just a heuristic, so do not worry about
3262 false positives from mode changes. */
3263
3264 if (pc < func_start + 4)
3265 return 0;
3266
3267 insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
f303bc3e 3268 if (arm_instruction_restores_sp (insn))
4024ca99
UW
3269 return 1;
3270
3271 return 0;
3272}
3273
c58b006a
YQ
3274/* Implement the stack_frame_destroyed_p gdbarch method. */
3275
3276static int
3277arm_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3278{
3279 if (arm_pc_is_thumb (gdbarch, pc))
3280 return thumb_stack_frame_destroyed_p (gdbarch, pc);
3281 else
3282 return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
3283}
4024ca99 3284
2dd604e7
RE
3285/* When arguments must be pushed onto the stack, they go on in reverse
3286 order. The code below implements a FILO (stack) to do this. */
3287
3288struct stack_item
3289{
3290 int len;
3291 struct stack_item *prev;
7c543f7b 3292 gdb_byte *data;
2dd604e7
RE
3293};
3294
3295static struct stack_item *
df3b6708 3296push_stack_item (struct stack_item *prev, const gdb_byte *contents, int len)
2dd604e7
RE
3297{
3298 struct stack_item *si;
8d749320 3299 si = XNEW (struct stack_item);
7c543f7b 3300 si->data = (gdb_byte *) xmalloc (len);
2dd604e7
RE
3301 si->len = len;
3302 si->prev = prev;
3303 memcpy (si->data, contents, len);
3304 return si;
3305}
3306
3307static struct stack_item *
3308pop_stack_item (struct stack_item *si)
3309{
3310 struct stack_item *dead = si;
3311 si = si->prev;
3312 xfree (dead->data);
3313 xfree (dead);
3314 return si;
3315}
3316
2af48f68
PB
3317
3318/* Return the alignment (in bytes) of the given type. */
3319
3320static int
3321arm_type_align (struct type *t)
3322{
3323 int n;
3324 int align;
3325 int falign;
3326
3327 t = check_typedef (t);
3328 switch (TYPE_CODE (t))
3329 {
3330 default:
3331 /* Should never happen. */
3332 internal_error (__FILE__, __LINE__, _("unknown type alignment"));
3333 return 4;
3334
3335 case TYPE_CODE_PTR:
3336 case TYPE_CODE_ENUM:
3337 case TYPE_CODE_INT:
3338 case TYPE_CODE_FLT:
3339 case TYPE_CODE_SET:
3340 case TYPE_CODE_RANGE:
2af48f68 3341 case TYPE_CODE_REF:
aa006118 3342 case TYPE_CODE_RVALUE_REF:
2af48f68
PB
3343 case TYPE_CODE_CHAR:
3344 case TYPE_CODE_BOOL:
3345 return TYPE_LENGTH (t);
3346
3347 case TYPE_CODE_ARRAY:
c4312b19
YQ
3348 if (TYPE_VECTOR (t))
3349 {
3350 /* Use the natural alignment for vector types (the same for
3351 scalar type), but the maximum alignment is 64-bit. */
3352 if (TYPE_LENGTH (t) > 8)
3353 return 8;
3354 else
3355 return TYPE_LENGTH (t);
3356 }
3357 else
3358 return arm_type_align (TYPE_TARGET_TYPE (t));
2af48f68 3359 case TYPE_CODE_COMPLEX:
2af48f68
PB
3360 return arm_type_align (TYPE_TARGET_TYPE (t));
3361
3362 case TYPE_CODE_STRUCT:
3363 case TYPE_CODE_UNION:
3364 align = 1;
3365 for (n = 0; n < TYPE_NFIELDS (t); n++)
3366 {
3367 falign = arm_type_align (TYPE_FIELD_TYPE (t, n));
3368 if (falign > align)
3369 align = falign;
3370 }
3371 return align;
3372 }
3373}
3374
90445bd3
DJ
3375/* Possible base types for a candidate for passing and returning in
3376 VFP registers. */
3377
3378enum arm_vfp_cprc_base_type
3379{
3380 VFP_CPRC_UNKNOWN,
3381 VFP_CPRC_SINGLE,
3382 VFP_CPRC_DOUBLE,
3383 VFP_CPRC_VEC64,
3384 VFP_CPRC_VEC128
3385};
3386
3387/* The length of one element of base type B. */
3388
3389static unsigned
3390arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
3391{
3392 switch (b)
3393 {
3394 case VFP_CPRC_SINGLE:
3395 return 4;
3396 case VFP_CPRC_DOUBLE:
3397 return 8;
3398 case VFP_CPRC_VEC64:
3399 return 8;
3400 case VFP_CPRC_VEC128:
3401 return 16;
3402 default:
3403 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3404 (int) b);
3405 }
3406}
3407
3408/* The character ('s', 'd' or 'q') for the type of VFP register used
3409 for passing base type B. */
3410
3411static int
3412arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
3413{
3414 switch (b)
3415 {
3416 case VFP_CPRC_SINGLE:
3417 return 's';
3418 case VFP_CPRC_DOUBLE:
3419 return 'd';
3420 case VFP_CPRC_VEC64:
3421 return 'd';
3422 case VFP_CPRC_VEC128:
3423 return 'q';
3424 default:
3425 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3426 (int) b);
3427 }
3428}
3429
3430/* Determine whether T may be part of a candidate for passing and
3431 returning in VFP registers, ignoring the limit on the total number
3432 of components. If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
3433 classification of the first valid component found; if it is not
3434 VFP_CPRC_UNKNOWN, all components must have the same classification
3435 as *BASE_TYPE. If it is found that T contains a type not permitted
3436 for passing and returning in VFP registers, a type differently
3437 classified from *BASE_TYPE, or two types differently classified
3438 from each other, return -1, otherwise return the total number of
3439 base-type elements found (possibly 0 in an empty structure or
817e0957
YQ
3440 array). Vector types are not currently supported, matching the
3441 generic AAPCS support. */
90445bd3
DJ
3442
3443static int
3444arm_vfp_cprc_sub_candidate (struct type *t,
3445 enum arm_vfp_cprc_base_type *base_type)
3446{
3447 t = check_typedef (t);
3448 switch (TYPE_CODE (t))
3449 {
3450 case TYPE_CODE_FLT:
3451 switch (TYPE_LENGTH (t))
3452 {
3453 case 4:
3454 if (*base_type == VFP_CPRC_UNKNOWN)
3455 *base_type = VFP_CPRC_SINGLE;
3456 else if (*base_type != VFP_CPRC_SINGLE)
3457 return -1;
3458 return 1;
3459
3460 case 8:
3461 if (*base_type == VFP_CPRC_UNKNOWN)
3462 *base_type = VFP_CPRC_DOUBLE;
3463 else if (*base_type != VFP_CPRC_DOUBLE)
3464 return -1;
3465 return 1;
3466
3467 default:
3468 return -1;
3469 }
3470 break;
3471
817e0957
YQ
3472 case TYPE_CODE_COMPLEX:
3473 /* Arguments of complex T where T is one of the types float or
3474 double get treated as if they are implemented as:
3475
3476 struct complexT
3477 {
3478 T real;
3479 T imag;
5f52445b
YQ
3480 };
3481
3482 */
817e0957
YQ
3483 switch (TYPE_LENGTH (t))
3484 {
3485 case 8:
3486 if (*base_type == VFP_CPRC_UNKNOWN)
3487 *base_type = VFP_CPRC_SINGLE;
3488 else if (*base_type != VFP_CPRC_SINGLE)
3489 return -1;
3490 return 2;
3491
3492 case 16:
3493 if (*base_type == VFP_CPRC_UNKNOWN)
3494 *base_type = VFP_CPRC_DOUBLE;
3495 else if (*base_type != VFP_CPRC_DOUBLE)
3496 return -1;
3497 return 2;
3498
3499 default:
3500 return -1;
3501 }
3502 break;
3503
90445bd3
DJ
3504 case TYPE_CODE_ARRAY:
3505 {
c4312b19 3506 if (TYPE_VECTOR (t))
90445bd3 3507 {
c4312b19
YQ
3508 /* A 64-bit or 128-bit containerized vector type are VFP
3509 CPRCs. */
3510 switch (TYPE_LENGTH (t))
3511 {
3512 case 8:
3513 if (*base_type == VFP_CPRC_UNKNOWN)
3514 *base_type = VFP_CPRC_VEC64;
3515 return 1;
3516 case 16:
3517 if (*base_type == VFP_CPRC_UNKNOWN)
3518 *base_type = VFP_CPRC_VEC128;
3519 return 1;
3520 default:
3521 return -1;
3522 }
3523 }
3524 else
3525 {
3526 int count;
3527 unsigned unitlen;
3528
3529 count = arm_vfp_cprc_sub_candidate (TYPE_TARGET_TYPE (t),
3530 base_type);
3531 if (count == -1)
3532 return -1;
3533 if (TYPE_LENGTH (t) == 0)
3534 {
3535 gdb_assert (count == 0);
3536 return 0;
3537 }
3538 else if (count == 0)
3539 return -1;
3540 unitlen = arm_vfp_cprc_unit_length (*base_type);
3541 gdb_assert ((TYPE_LENGTH (t) % unitlen) == 0);
3542 return TYPE_LENGTH (t) / unitlen;
90445bd3 3543 }
90445bd3
DJ
3544 }
3545 break;
3546
3547 case TYPE_CODE_STRUCT:
3548 {
3549 int count = 0;
3550 unsigned unitlen;
3551 int i;
3552 for (i = 0; i < TYPE_NFIELDS (t); i++)
3553 {
1040b979
YQ
3554 int sub_count = 0;
3555
3556 if (!field_is_static (&TYPE_FIELD (t, i)))
3557 sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3558 base_type);
90445bd3
DJ
3559 if (sub_count == -1)
3560 return -1;
3561 count += sub_count;
3562 }
3563 if (TYPE_LENGTH (t) == 0)
3564 {
3565 gdb_assert (count == 0);
3566 return 0;
3567 }
3568 else if (count == 0)
3569 return -1;
3570 unitlen = arm_vfp_cprc_unit_length (*base_type);
3571 if (TYPE_LENGTH (t) != unitlen * count)
3572 return -1;
3573 return count;
3574 }
3575
3576 case TYPE_CODE_UNION:
3577 {
3578 int count = 0;
3579 unsigned unitlen;
3580 int i;
3581 for (i = 0; i < TYPE_NFIELDS (t); i++)
3582 {
3583 int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3584 base_type);
3585 if (sub_count == -1)
3586 return -1;
3587 count = (count > sub_count ? count : sub_count);
3588 }
3589 if (TYPE_LENGTH (t) == 0)
3590 {
3591 gdb_assert (count == 0);
3592 return 0;
3593 }
3594 else if (count == 0)
3595 return -1;
3596 unitlen = arm_vfp_cprc_unit_length (*base_type);
3597 if (TYPE_LENGTH (t) != unitlen * count)
3598 return -1;
3599 return count;
3600 }
3601
3602 default:
3603 break;
3604 }
3605
3606 return -1;
3607}
3608
3609/* Determine whether T is a VFP co-processor register candidate (CPRC)
3610 if passed to or returned from a non-variadic function with the VFP
3611 ABI in effect. Return 1 if it is, 0 otherwise. If it is, set
3612 *BASE_TYPE to the base type for T and *COUNT to the number of
3613 elements of that base type before returning. */
3614
3615static int
3616arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
3617 int *count)
3618{
3619 enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
3620 int c = arm_vfp_cprc_sub_candidate (t, &b);
3621 if (c <= 0 || c > 4)
3622 return 0;
3623 *base_type = b;
3624 *count = c;
3625 return 1;
3626}
3627
3628/* Return 1 if the VFP ABI should be used for passing arguments to and
3629 returning values from a function of type FUNC_TYPE, 0
3630 otherwise. */
3631
3632static int
3633arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
3634{
3635 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3636 /* Variadic functions always use the base ABI. Assume that functions
3637 without debug info are not variadic. */
3638 if (func_type && TYPE_VARARGS (check_typedef (func_type)))
3639 return 0;
3640 /* The VFP ABI is only supported as a variant of AAPCS. */
3641 if (tdep->arm_abi != ARM_ABI_AAPCS)
3642 return 0;
3643 return gdbarch_tdep (gdbarch)->fp_model == ARM_FLOAT_VFP;
3644}
3645
3646/* We currently only support passing parameters in integer registers, which
3647 conforms with GCC's default model, and VFP argument passing following
3648 the VFP variant of AAPCS. Several other variants exist and
2dd604e7
RE
3649 we should probably support some of them based on the selected ABI. */
3650
3651static CORE_ADDR
7d9b040b 3652arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6a65450a 3653 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
cf84fa6b
AH
3654 struct value **args, CORE_ADDR sp,
3655 function_call_return_method return_method,
6a65450a 3656 CORE_ADDR struct_addr)
2dd604e7 3657{
e17a4113 3658 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2dd604e7
RE
3659 int argnum;
3660 int argreg;
3661 int nstack;
3662 struct stack_item *si = NULL;
90445bd3
DJ
3663 int use_vfp_abi;
3664 struct type *ftype;
3665 unsigned vfp_regs_free = (1 << 16) - 1;
3666
3667 /* Determine the type of this function and whether the VFP ABI
3668 applies. */
3669 ftype = check_typedef (value_type (function));
3670 if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
3671 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
3672 use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
2dd604e7 3673
6a65450a
AC
3674 /* Set the return address. For the ARM, the return breakpoint is
3675 always at BP_ADDR. */
9779414d 3676 if (arm_pc_is_thumb (gdbarch, bp_addr))
9dca5578 3677 bp_addr |= 1;
6a65450a 3678 regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
2dd604e7
RE
3679
3680 /* Walk through the list of args and determine how large a temporary
3681 stack is required. Need to take care here as structs may be
7a9dd1b2 3682 passed on the stack, and we have to push them. */
2dd604e7
RE
3683 nstack = 0;
3684
3685 argreg = ARM_A1_REGNUM;
3686 nstack = 0;
3687
2dd604e7
RE
3688 /* The struct_return pointer occupies the first parameter
3689 passing register. */
cf84fa6b 3690 if (return_method == return_method_struct)
2dd604e7
RE
3691 {
3692 if (arm_debug)
5af949e3 3693 fprintf_unfiltered (gdb_stdlog, "struct return in %s = %s\n",
2af46ca0 3694 gdbarch_register_name (gdbarch, argreg),
5af949e3 3695 paddress (gdbarch, struct_addr));
2dd604e7
RE
3696 regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
3697 argreg++;
3698 }
3699
3700 for (argnum = 0; argnum < nargs; argnum++)
3701 {
3702 int len;
3703 struct type *arg_type;
3704 struct type *target_type;
3705 enum type_code typecode;
8c6363cf 3706 const bfd_byte *val;
2af48f68 3707 int align;
90445bd3
DJ
3708 enum arm_vfp_cprc_base_type vfp_base_type;
3709 int vfp_base_count;
3710 int may_use_core_reg = 1;
2dd604e7 3711
df407dfe 3712 arg_type = check_typedef (value_type (args[argnum]));
2dd604e7
RE
3713 len = TYPE_LENGTH (arg_type);
3714 target_type = TYPE_TARGET_TYPE (arg_type);
3715 typecode = TYPE_CODE (arg_type);
8c6363cf 3716 val = value_contents (args[argnum]);
2dd604e7 3717
2af48f68
PB
3718 align = arm_type_align (arg_type);
3719 /* Round alignment up to a whole number of words. */
3720 align = (align + INT_REGISTER_SIZE - 1) & ~(INT_REGISTER_SIZE - 1);
3721 /* Different ABIs have different maximum alignments. */
3722 if (gdbarch_tdep (gdbarch)->arm_abi == ARM_ABI_APCS)
3723 {
3724 /* The APCS ABI only requires word alignment. */
3725 align = INT_REGISTER_SIZE;
3726 }
3727 else
3728 {
3729 /* The AAPCS requires at most doubleword alignment. */
3730 if (align > INT_REGISTER_SIZE * 2)
3731 align = INT_REGISTER_SIZE * 2;
3732 }
3733
90445bd3
DJ
3734 if (use_vfp_abi
3735 && arm_vfp_call_candidate (arg_type, &vfp_base_type,
3736 &vfp_base_count))
3737 {
3738 int regno;
3739 int unit_length;
3740 int shift;
3741 unsigned mask;
3742
3743 /* Because this is a CPRC it cannot go in a core register or
3744 cause a core register to be skipped for alignment.
3745 Either it goes in VFP registers and the rest of this loop
3746 iteration is skipped for this argument, or it goes on the
3747 stack (and the stack alignment code is correct for this
3748 case). */
3749 may_use_core_reg = 0;
3750
3751 unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
3752 shift = unit_length / 4;
3753 mask = (1 << (shift * vfp_base_count)) - 1;
3754 for (regno = 0; regno < 16; regno += shift)
3755 if (((vfp_regs_free >> regno) & mask) == mask)
3756 break;
3757
3758 if (regno < 16)
3759 {
3760 int reg_char;
3761 int reg_scaled;
3762 int i;
3763
3764 vfp_regs_free &= ~(mask << regno);
3765 reg_scaled = regno / shift;
3766 reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
3767 for (i = 0; i < vfp_base_count; i++)
3768 {
3769 char name_buf[4];
3770 int regnum;
58d6951d
DJ
3771 if (reg_char == 'q')
3772 arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
90445bd3 3773 val + i * unit_length);
58d6951d
DJ
3774 else
3775 {
8c042590
PM
3776 xsnprintf (name_buf, sizeof (name_buf), "%c%d",
3777 reg_char, reg_scaled + i);
58d6951d
DJ
3778 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
3779 strlen (name_buf));
b66f5587 3780 regcache->cooked_write (regnum, val + i * unit_length);
58d6951d 3781 }
90445bd3
DJ
3782 }
3783 continue;
3784 }
3785 else
3786 {
3787 /* This CPRC could not go in VFP registers, so all VFP
3788 registers are now marked as used. */
3789 vfp_regs_free = 0;
3790 }
3791 }
3792
2af48f68
PB
3793 /* Push stack padding for dowubleword alignment. */
3794 if (nstack & (align - 1))
3795 {
3796 si = push_stack_item (si, val, INT_REGISTER_SIZE);
3797 nstack += INT_REGISTER_SIZE;
3798 }
3799
3800 /* Doubleword aligned quantities must go in even register pairs. */
90445bd3
DJ
3801 if (may_use_core_reg
3802 && argreg <= ARM_LAST_ARG_REGNUM
2af48f68
PB
3803 && align > INT_REGISTER_SIZE
3804 && argreg & 1)
3805 argreg++;
3806
2dd604e7
RE
3807 /* If the argument is a pointer to a function, and it is a
3808 Thumb function, create a LOCAL copy of the value and set
3809 the THUMB bit in it. */
3810 if (TYPE_CODE_PTR == typecode
3811 && target_type != NULL
f96b8fa0 3812 && TYPE_CODE_FUNC == TYPE_CODE (check_typedef (target_type)))
2dd604e7 3813 {
e17a4113 3814 CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
9779414d 3815 if (arm_pc_is_thumb (gdbarch, regval))
2dd604e7 3816 {
224c3ddb 3817 bfd_byte *copy = (bfd_byte *) alloca (len);
8c6363cf 3818 store_unsigned_integer (copy, len, byte_order,
e17a4113 3819 MAKE_THUMB_ADDR (regval));
8c6363cf 3820 val = copy;
2dd604e7
RE
3821 }
3822 }
3823
3824 /* Copy the argument to general registers or the stack in
3825 register-sized pieces. Large arguments are split between
3826 registers and stack. */
3827 while (len > 0)
3828 {
f0c9063c 3829 int partial_len = len < INT_REGISTER_SIZE ? len : INT_REGISTER_SIZE;
ef9bd0b8
YQ
3830 CORE_ADDR regval
3831 = extract_unsigned_integer (val, partial_len, byte_order);
2dd604e7 3832
90445bd3 3833 if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
2dd604e7
RE
3834 {
3835 /* The argument is being passed in a general purpose
3836 register. */
e17a4113 3837 if (byte_order == BFD_ENDIAN_BIG)
8bf8793c 3838 regval <<= (INT_REGISTER_SIZE - partial_len) * 8;
2dd604e7
RE
3839 if (arm_debug)
3840 fprintf_unfiltered (gdb_stdlog, "arg %d in %s = 0x%s\n",
c9f4d572
UW
3841 argnum,
3842 gdbarch_register_name
2af46ca0 3843 (gdbarch, argreg),
f0c9063c 3844 phex (regval, INT_REGISTER_SIZE));
2dd604e7
RE
3845 regcache_cooked_write_unsigned (regcache, argreg, regval);
3846 argreg++;
3847 }
3848 else
3849 {
ef9bd0b8
YQ
3850 gdb_byte buf[INT_REGISTER_SIZE];
3851
3852 memset (buf, 0, sizeof (buf));
3853 store_unsigned_integer (buf, partial_len, byte_order, regval);
3854
2dd604e7
RE
3855 /* Push the arguments onto the stack. */
3856 if (arm_debug)
3857 fprintf_unfiltered (gdb_stdlog, "arg %d @ sp + %d\n",
3858 argnum, nstack);
ef9bd0b8 3859 si = push_stack_item (si, buf, INT_REGISTER_SIZE);
f0c9063c 3860 nstack += INT_REGISTER_SIZE;
2dd604e7
RE
3861 }
3862
3863 len -= partial_len;
3864 val += partial_len;
3865 }
3866 }
3867 /* If we have an odd number of words to push, then decrement the stack
3868 by one word now, so first stack argument will be dword aligned. */
3869 if (nstack & 4)
3870 sp -= 4;
3871
3872 while (si)
3873 {
3874 sp -= si->len;
3875 write_memory (sp, si->data, si->len);
3876 si = pop_stack_item (si);
3877 }
3878
3879 /* Finally, update teh SP register. */
3880 regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
3881
3882 return sp;
3883}
3884
f53f0d0b
PB
3885
3886/* Always align the frame to an 8-byte boundary. This is required on
3887 some platforms and harmless on the rest. */
3888
3889static CORE_ADDR
3890arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
3891{
3892 /* Align the stack to eight bytes. */
3893 return sp & ~ (CORE_ADDR) 7;
3894}
3895
c906108c 3896static void
12b27276 3897print_fpu_flags (struct ui_file *file, int flags)
c906108c 3898{
c5aa993b 3899 if (flags & (1 << 0))
12b27276 3900 fputs_filtered ("IVO ", file);
c5aa993b 3901 if (flags & (1 << 1))
12b27276 3902 fputs_filtered ("DVZ ", file);
c5aa993b 3903 if (flags & (1 << 2))
12b27276 3904 fputs_filtered ("OFL ", file);
c5aa993b 3905 if (flags & (1 << 3))
12b27276 3906 fputs_filtered ("UFL ", file);
c5aa993b 3907 if (flags & (1 << 4))
12b27276
WN
3908 fputs_filtered ("INX ", file);
3909 fputc_filtered ('\n', file);
c906108c
SS
3910}
3911
5e74b15c
RE
3912/* Print interesting information about the floating point processor
3913 (if present) or emulator. */
34e8f22d 3914static void
d855c300 3915arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
23e3a7ac 3916 struct frame_info *frame, const char *args)
c906108c 3917{
9c9acae0 3918 unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
c5aa993b
JM
3919 int type;
3920
3921 type = (status >> 24) & 127;
edefbb7c 3922 if (status & (1 << 31))
12b27276 3923 fprintf_filtered (file, _("Hardware FPU type %d\n"), type);
edefbb7c 3924 else
12b27276 3925 fprintf_filtered (file, _("Software FPU type %d\n"), type);
edefbb7c 3926 /* i18n: [floating point unit] mask */
12b27276
WN
3927 fputs_filtered (_("mask: "), file);
3928 print_fpu_flags (file, status >> 16);
edefbb7c 3929 /* i18n: [floating point unit] flags */
12b27276
WN
3930 fputs_filtered (_("flags: "), file);
3931 print_fpu_flags (file, status);
c906108c
SS
3932}
3933
27067745
UW
3934/* Construct the ARM extended floating point type. */
3935static struct type *
3936arm_ext_type (struct gdbarch *gdbarch)
3937{
3938 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3939
3940 if (!tdep->arm_ext_type)
3941 tdep->arm_ext_type
e9bb382b 3942 = arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
27067745
UW
3943 floatformats_arm_ext);
3944
3945 return tdep->arm_ext_type;
3946}
3947
58d6951d
DJ
3948static struct type *
3949arm_neon_double_type (struct gdbarch *gdbarch)
3950{
3951 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3952
3953 if (tdep->neon_double_type == NULL)
3954 {
3955 struct type *t, *elem;
3956
3957 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
3958 TYPE_CODE_UNION);
3959 elem = builtin_type (gdbarch)->builtin_uint8;
3960 append_composite_type_field (t, "u8", init_vector_type (elem, 8));
3961 elem = builtin_type (gdbarch)->builtin_uint16;
3962 append_composite_type_field (t, "u16", init_vector_type (elem, 4));
3963 elem = builtin_type (gdbarch)->builtin_uint32;
3964 append_composite_type_field (t, "u32", init_vector_type (elem, 2));
3965 elem = builtin_type (gdbarch)->builtin_uint64;
3966 append_composite_type_field (t, "u64", elem);
3967 elem = builtin_type (gdbarch)->builtin_float;
3968 append_composite_type_field (t, "f32", init_vector_type (elem, 2));
3969 elem = builtin_type (gdbarch)->builtin_double;
3970 append_composite_type_field (t, "f64", elem);
3971
3972 TYPE_VECTOR (t) = 1;
3973 TYPE_NAME (t) = "neon_d";
3974 tdep->neon_double_type = t;
3975 }
3976
3977 return tdep->neon_double_type;
3978}
3979
3980/* FIXME: The vector types are not correctly ordered on big-endian
3981 targets. Just as s0 is the low bits of d0, d0[0] is also the low
3982 bits of d0 - regardless of what unit size is being held in d0. So
3983 the offset of the first uint8 in d0 is 7, but the offset of the
3984 first float is 4. This code works as-is for little-endian
3985 targets. */
3986
3987static struct type *
3988arm_neon_quad_type (struct gdbarch *gdbarch)
3989{
3990 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3991
3992 if (tdep->neon_quad_type == NULL)
3993 {
3994 struct type *t, *elem;
3995
3996 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
3997 TYPE_CODE_UNION);
3998 elem = builtin_type (gdbarch)->builtin_uint8;
3999 append_composite_type_field (t, "u8", init_vector_type (elem, 16));
4000 elem = builtin_type (gdbarch)->builtin_uint16;
4001 append_composite_type_field (t, "u16", init_vector_type (elem, 8));
4002 elem = builtin_type (gdbarch)->builtin_uint32;
4003 append_composite_type_field (t, "u32", init_vector_type (elem, 4));
4004 elem = builtin_type (gdbarch)->builtin_uint64;
4005 append_composite_type_field (t, "u64", init_vector_type (elem, 2));
4006 elem = builtin_type (gdbarch)->builtin_float;
4007 append_composite_type_field (t, "f32", init_vector_type (elem, 4));
4008 elem = builtin_type (gdbarch)->builtin_double;
4009 append_composite_type_field (t, "f64", init_vector_type (elem, 2));
4010
4011 TYPE_VECTOR (t) = 1;
4012 TYPE_NAME (t) = "neon_q";
4013 tdep->neon_quad_type = t;
4014 }
4015
4016 return tdep->neon_quad_type;
4017}
4018
34e8f22d
RE
4019/* Return the GDB type object for the "standard" data type of data in
4020 register N. */
4021
4022static struct type *
7a5ea0d4 4023arm_register_type (struct gdbarch *gdbarch, int regnum)
032758dc 4024{
58d6951d
DJ
4025 int num_regs = gdbarch_num_regs (gdbarch);
4026
4027 if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
4028 && regnum >= num_regs && regnum < num_regs + 32)
4029 return builtin_type (gdbarch)->builtin_float;
4030
4031 if (gdbarch_tdep (gdbarch)->have_neon_pseudos
4032 && regnum >= num_regs + 32 && regnum < num_regs + 32 + 16)
4033 return arm_neon_quad_type (gdbarch);
4034
4035 /* If the target description has register information, we are only
4036 in this function so that we can override the types of
4037 double-precision registers for NEON. */
4038 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
4039 {
4040 struct type *t = tdesc_register_type (gdbarch, regnum);
4041
4042 if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
4043 && TYPE_CODE (t) == TYPE_CODE_FLT
4044 && gdbarch_tdep (gdbarch)->have_neon)
4045 return arm_neon_double_type (gdbarch);
4046 else
4047 return t;
4048 }
4049
34e8f22d 4050 if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
58d6951d
DJ
4051 {
4052 if (!gdbarch_tdep (gdbarch)->have_fpa_registers)
4053 return builtin_type (gdbarch)->builtin_void;
4054
4055 return arm_ext_type (gdbarch);
4056 }
e4c16157 4057 else if (regnum == ARM_SP_REGNUM)
0dfff4cb 4058 return builtin_type (gdbarch)->builtin_data_ptr;
e4c16157 4059 else if (regnum == ARM_PC_REGNUM)
0dfff4cb 4060 return builtin_type (gdbarch)->builtin_func_ptr;
ff6f572f
DJ
4061 else if (regnum >= ARRAY_SIZE (arm_register_names))
4062 /* These registers are only supported on targets which supply
4063 an XML description. */
df4df182 4064 return builtin_type (gdbarch)->builtin_int0;
032758dc 4065 else
df4df182 4066 return builtin_type (gdbarch)->builtin_uint32;
032758dc
AC
4067}
4068
ff6f572f
DJ
4069/* Map a DWARF register REGNUM onto the appropriate GDB register
4070 number. */
4071
4072static int
d3f73121 4073arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
ff6f572f
DJ
4074{
4075 /* Core integer regs. */
4076 if (reg >= 0 && reg <= 15)
4077 return reg;
4078
4079 /* Legacy FPA encoding. These were once used in a way which
4080 overlapped with VFP register numbering, so their use is
4081 discouraged, but GDB doesn't support the ARM toolchain
4082 which used them for VFP. */
4083 if (reg >= 16 && reg <= 23)
4084 return ARM_F0_REGNUM + reg - 16;
4085
4086 /* New assignments for the FPA registers. */
4087 if (reg >= 96 && reg <= 103)
4088 return ARM_F0_REGNUM + reg - 96;
4089
4090 /* WMMX register assignments. */
4091 if (reg >= 104 && reg <= 111)
4092 return ARM_WCGR0_REGNUM + reg - 104;
4093
4094 if (reg >= 112 && reg <= 127)
4095 return ARM_WR0_REGNUM + reg - 112;
4096
4097 if (reg >= 192 && reg <= 199)
4098 return ARM_WC0_REGNUM + reg - 192;
4099
58d6951d
DJ
4100 /* VFP v2 registers. A double precision value is actually
4101 in d1 rather than s2, but the ABI only defines numbering
4102 for the single precision registers. This will "just work"
4103 in GDB for little endian targets (we'll read eight bytes,
4104 starting in s0 and then progressing to s1), but will be
4105 reversed on big endian targets with VFP. This won't
4106 be a problem for the new Neon quad registers; you're supposed
4107 to use DW_OP_piece for those. */
4108 if (reg >= 64 && reg <= 95)
4109 {
4110 char name_buf[4];
4111
8c042590 4112 xsnprintf (name_buf, sizeof (name_buf), "s%d", reg - 64);
58d6951d
DJ
4113 return user_reg_map_name_to_regnum (gdbarch, name_buf,
4114 strlen (name_buf));
4115 }
4116
4117 /* VFP v3 / Neon registers. This range is also used for VFP v2
4118 registers, except that it now describes d0 instead of s0. */
4119 if (reg >= 256 && reg <= 287)
4120 {
4121 char name_buf[4];
4122
8c042590 4123 xsnprintf (name_buf, sizeof (name_buf), "d%d", reg - 256);
58d6951d
DJ
4124 return user_reg_map_name_to_regnum (gdbarch, name_buf,
4125 strlen (name_buf));
4126 }
4127
ff6f572f
DJ
4128 return -1;
4129}
4130
26216b98
AC
4131/* Map GDB internal REGNUM onto the Arm simulator register numbers. */
4132static int
e7faf938 4133arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
26216b98
AC
4134{
4135 int reg = regnum;
e7faf938 4136 gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
26216b98 4137
ff6f572f
DJ
4138 if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
4139 return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
4140
4141 if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
4142 return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
4143
4144 if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
4145 return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
4146
26216b98
AC
4147 if (reg < NUM_GREGS)
4148 return SIM_ARM_R0_REGNUM + reg;
4149 reg -= NUM_GREGS;
4150
4151 if (reg < NUM_FREGS)
4152 return SIM_ARM_FP0_REGNUM + reg;
4153 reg -= NUM_FREGS;
4154
4155 if (reg < NUM_SREGS)
4156 return SIM_ARM_FPS_REGNUM + reg;
4157 reg -= NUM_SREGS;
4158
edefbb7c 4159 internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
26216b98 4160}
34e8f22d 4161
d9311bfa
AT
4162/* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
4163 the buffer to be NEW_LEN bytes ending at ENDADDR. Return
4164 NULL if an error occurs. BUF is freed. */
c906108c 4165
d9311bfa
AT
4166static gdb_byte *
4167extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
4168 int old_len, int new_len)
4169{
4170 gdb_byte *new_buf;
4171 int bytes_to_read = new_len - old_len;
c906108c 4172
d9311bfa
AT
4173 new_buf = (gdb_byte *) xmalloc (new_len);
4174 memcpy (new_buf + bytes_to_read, buf, old_len);
4175 xfree (buf);
198cd59d 4176 if (target_read_code (endaddr - new_len, new_buf, bytes_to_read) != 0)
d9311bfa
AT
4177 {
4178 xfree (new_buf);
4179 return NULL;
c906108c 4180 }
d9311bfa 4181 return new_buf;
c906108c
SS
4182}
4183
d9311bfa
AT
4184/* An IT block is at most the 2-byte IT instruction followed by
4185 four 4-byte instructions. The furthest back we must search to
4186 find an IT block that affects the current instruction is thus
4187 2 + 3 * 4 == 14 bytes. */
4188#define MAX_IT_BLOCK_PREFIX 14
177321bd 4189
d9311bfa
AT
4190/* Use a quick scan if there are more than this many bytes of
4191 code. */
4192#define IT_SCAN_THRESHOLD 32
177321bd 4193
d9311bfa
AT
4194/* Adjust a breakpoint's address to move breakpoints out of IT blocks.
4195 A breakpoint in an IT block may not be hit, depending on the
4196 condition flags. */
ad527d2e 4197static CORE_ADDR
d9311bfa 4198arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
c906108c 4199{
d9311bfa
AT
4200 gdb_byte *buf;
4201 char map_type;
4202 CORE_ADDR boundary, func_start;
4203 int buf_len;
4204 enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
4205 int i, any, last_it, last_it_count;
177321bd 4206
d9311bfa
AT
4207 /* If we are using BKPT breakpoints, none of this is necessary. */
4208 if (gdbarch_tdep (gdbarch)->thumb2_breakpoint == NULL)
4209 return bpaddr;
177321bd 4210
d9311bfa
AT
4211 /* ARM mode does not have this problem. */
4212 if (!arm_pc_is_thumb (gdbarch, bpaddr))
4213 return bpaddr;
177321bd 4214
d9311bfa
AT
4215 /* We are setting a breakpoint in Thumb code that could potentially
4216 contain an IT block. The first step is to find how much Thumb
4217 code there is; we do not need to read outside of known Thumb
4218 sequences. */
4219 map_type = arm_find_mapping_symbol (bpaddr, &boundary);
4220 if (map_type == 0)
4221 /* Thumb-2 code must have mapping symbols to have a chance. */
4222 return bpaddr;
9dca5578 4223
d9311bfa 4224 bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
177321bd 4225
d9311bfa
AT
4226 if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
4227 && func_start > boundary)
4228 boundary = func_start;
9dca5578 4229
d9311bfa
AT
4230 /* Search for a candidate IT instruction. We have to do some fancy
4231 footwork to distinguish a real IT instruction from the second
4232 half of a 32-bit instruction, but there is no need for that if
4233 there's no candidate. */
325fac50 4234 buf_len = std::min (bpaddr - boundary, (CORE_ADDR) MAX_IT_BLOCK_PREFIX);
d9311bfa
AT
4235 if (buf_len == 0)
4236 /* No room for an IT instruction. */
4237 return bpaddr;
c906108c 4238
d9311bfa 4239 buf = (gdb_byte *) xmalloc (buf_len);
198cd59d 4240 if (target_read_code (bpaddr - buf_len, buf, buf_len) != 0)
d9311bfa
AT
4241 return bpaddr;
4242 any = 0;
4243 for (i = 0; i < buf_len; i += 2)
c906108c 4244 {
d9311bfa
AT
4245 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
4246 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
25b41d01 4247 {
d9311bfa
AT
4248 any = 1;
4249 break;
25b41d01 4250 }
c906108c 4251 }
d9311bfa
AT
4252
4253 if (any == 0)
c906108c 4254 {
d9311bfa
AT
4255 xfree (buf);
4256 return bpaddr;
f9d67f43
DJ
4257 }
4258
4259 /* OK, the code bytes before this instruction contain at least one
4260 halfword which resembles an IT instruction. We know that it's
4261 Thumb code, but there are still two possibilities. Either the
4262 halfword really is an IT instruction, or it is the second half of
4263 a 32-bit Thumb instruction. The only way we can tell is to
4264 scan forwards from a known instruction boundary. */
4265 if (bpaddr - boundary > IT_SCAN_THRESHOLD)
4266 {
4267 int definite;
4268
4269 /* There's a lot of code before this instruction. Start with an
4270 optimistic search; it's easy to recognize halfwords that can
4271 not be the start of a 32-bit instruction, and use that to
4272 lock on to the instruction boundaries. */
4273 buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
4274 if (buf == NULL)
4275 return bpaddr;
4276 buf_len = IT_SCAN_THRESHOLD;
4277
4278 definite = 0;
4279 for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
4280 {
4281 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
4282 if (thumb_insn_size (inst1) == 2)
4283 {
4284 definite = 1;
4285 break;
4286 }
4287 }
4288
4289 /* At this point, if DEFINITE, BUF[I] is the first place we
4290 are sure that we know the instruction boundaries, and it is far
4291 enough from BPADDR that we could not miss an IT instruction
4292 affecting BPADDR. If ! DEFINITE, give up - start from a
4293 known boundary. */
4294 if (! definite)
4295 {
0963b4bd
MS
4296 buf = extend_buffer_earlier (buf, bpaddr, buf_len,
4297 bpaddr - boundary);
f9d67f43
DJ
4298 if (buf == NULL)
4299 return bpaddr;
4300 buf_len = bpaddr - boundary;
4301 i = 0;
4302 }
4303 }
4304 else
4305 {
4306 buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
4307 if (buf == NULL)
4308 return bpaddr;
4309 buf_len = bpaddr - boundary;
4310 i = 0;
4311 }
4312
4313 /* Scan forwards. Find the last IT instruction before BPADDR. */
4314 last_it = -1;
4315 last_it_count = 0;
4316 while (i < buf_len)
4317 {
4318 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
4319 last_it_count--;
4320 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
4321 {
4322 last_it = i;
4323 if (inst1 & 0x0001)
4324 last_it_count = 4;
4325 else if (inst1 & 0x0002)
4326 last_it_count = 3;
4327 else if (inst1 & 0x0004)
4328 last_it_count = 2;
4329 else
4330 last_it_count = 1;
4331 }
4332 i += thumb_insn_size (inst1);
4333 }
4334
4335 xfree (buf);
4336
4337 if (last_it == -1)
4338 /* There wasn't really an IT instruction after all. */
4339 return bpaddr;
4340
4341 if (last_it_count < 1)
4342 /* It was too far away. */
4343 return bpaddr;
4344
4345 /* This really is a trouble spot. Move the breakpoint to the IT
4346 instruction. */
4347 return bpaddr - buf_len + last_it;
4348}
4349
cca44b1b 4350/* ARM displaced stepping support.
c906108c 4351
cca44b1b 4352 Generally ARM displaced stepping works as follows:
c906108c 4353
cca44b1b 4354 1. When an instruction is to be single-stepped, it is first decoded by
2ba163c8
SM
4355 arm_process_displaced_insn. Depending on the type of instruction, it is
4356 then copied to a scratch location, possibly in a modified form. The
4357 copy_* set of functions performs such modification, as necessary. A
4358 breakpoint is placed after the modified instruction in the scratch space
4359 to return control to GDB. Note in particular that instructions which
4360 modify the PC will no longer do so after modification.
c5aa993b 4361
cca44b1b
JB
4362 2. The instruction is single-stepped, by setting the PC to the scratch
4363 location address, and resuming. Control returns to GDB when the
4364 breakpoint is hit.
c5aa993b 4365
cca44b1b
JB
4366 3. A cleanup function (cleanup_*) is called corresponding to the copy_*
4367 function used for the current instruction. This function's job is to
4368 put the CPU/memory state back to what it would have been if the
4369 instruction had been executed unmodified in its original location. */
c5aa993b 4370
cca44b1b
JB
4371/* NOP instruction (mov r0, r0). */
4372#define ARM_NOP 0xe1a00000
34518530 4373#define THUMB_NOP 0x4600
cca44b1b
JB
4374
4375/* Helper for register reads for displaced stepping. In particular, this
4376 returns the PC as it would be seen by the instruction at its original
4377 location. */
4378
4379ULONGEST
cfba9872 4380displaced_read_reg (struct regcache *regs, arm_displaced_step_closure *dsc,
36073a92 4381 int regno)
cca44b1b
JB
4382{
4383 ULONGEST ret;
36073a92 4384 CORE_ADDR from = dsc->insn_addr;
cca44b1b 4385
bf9f652a 4386 if (regno == ARM_PC_REGNUM)
cca44b1b 4387 {
4db71c0b
YQ
4388 /* Compute pipeline offset:
4389 - When executing an ARM instruction, PC reads as the address of the
4390 current instruction plus 8.
4391 - When executing a Thumb instruction, PC reads as the address of the
4392 current instruction plus 4. */
4393
36073a92 4394 if (!dsc->is_thumb)
4db71c0b
YQ
4395 from += 8;
4396 else
4397 from += 4;
4398
cca44b1b
JB
4399 if (debug_displaced)
4400 fprintf_unfiltered (gdb_stdlog, "displaced: read pc value %.8lx\n",
4db71c0b
YQ
4401 (unsigned long) from);
4402 return (ULONGEST) from;
cca44b1b 4403 }
c906108c 4404 else
cca44b1b
JB
4405 {
4406 regcache_cooked_read_unsigned (regs, regno, &ret);
4407 if (debug_displaced)
4408 fprintf_unfiltered (gdb_stdlog, "displaced: read r%d value %.8lx\n",
4409 regno, (unsigned long) ret);
4410 return ret;
4411 }
c906108c
SS
4412}
4413
cca44b1b
JB
4414static int
4415displaced_in_arm_mode (struct regcache *regs)
4416{
4417 ULONGEST ps;
ac7936df 4418 ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
66e810cd 4419
cca44b1b 4420 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
66e810cd 4421
9779414d 4422 return (ps & t_bit) == 0;
cca44b1b 4423}
66e810cd 4424
cca44b1b 4425/* Write to the PC as from a branch instruction. */
c906108c 4426
cca44b1b 4427static void
cfba9872 4428branch_write_pc (struct regcache *regs, arm_displaced_step_closure *dsc,
36073a92 4429 ULONGEST val)
c906108c 4430{
36073a92 4431 if (!dsc->is_thumb)
cca44b1b
JB
4432 /* Note: If bits 0/1 are set, this branch would be unpredictable for
4433 architecture versions < 6. */
0963b4bd
MS
4434 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
4435 val & ~(ULONGEST) 0x3);
cca44b1b 4436 else
0963b4bd
MS
4437 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
4438 val & ~(ULONGEST) 0x1);
cca44b1b 4439}
66e810cd 4440
cca44b1b
JB
4441/* Write to the PC as from a branch-exchange instruction. */
4442
4443static void
4444bx_write_pc (struct regcache *regs, ULONGEST val)
4445{
4446 ULONGEST ps;
ac7936df 4447 ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
cca44b1b
JB
4448
4449 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
4450
4451 if ((val & 1) == 1)
c906108c 4452 {
9779414d 4453 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
cca44b1b
JB
4454 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
4455 }
4456 else if ((val & 2) == 0)
4457 {
9779414d 4458 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 4459 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
c906108c
SS
4460 }
4461 else
4462 {
cca44b1b
JB
4463 /* Unpredictable behaviour. Try to do something sensible (switch to ARM
4464 mode, align dest to 4 bytes). */
4465 warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
9779414d 4466 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 4467 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
c906108c
SS
4468 }
4469}
ed9a39eb 4470
cca44b1b 4471/* Write to the PC as if from a load instruction. */
ed9a39eb 4472
34e8f22d 4473static void
cfba9872 4474load_write_pc (struct regcache *regs, arm_displaced_step_closure *dsc,
36073a92 4475 ULONGEST val)
ed9a39eb 4476{
cca44b1b
JB
4477 if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
4478 bx_write_pc (regs, val);
4479 else
36073a92 4480 branch_write_pc (regs, dsc, val);
cca44b1b 4481}
be8626e0 4482
cca44b1b
JB
4483/* Write to the PC as if from an ALU instruction. */
4484
4485static void
cfba9872 4486alu_write_pc (struct regcache *regs, arm_displaced_step_closure *dsc,
36073a92 4487 ULONGEST val)
cca44b1b 4488{
36073a92 4489 if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
cca44b1b
JB
4490 bx_write_pc (regs, val);
4491 else
36073a92 4492 branch_write_pc (regs, dsc, val);
cca44b1b
JB
4493}
4494
4495/* Helper for writing to registers for displaced stepping. Writing to the PC
4496 has a varying effects depending on the instruction which does the write:
4497 this is controlled by the WRITE_PC argument. */
4498
4499void
cfba9872 4500displaced_write_reg (struct regcache *regs, arm_displaced_step_closure *dsc,
cca44b1b
JB
4501 int regno, ULONGEST val, enum pc_write_style write_pc)
4502{
bf9f652a 4503 if (regno == ARM_PC_REGNUM)
08216dd7 4504 {
cca44b1b
JB
4505 if (debug_displaced)
4506 fprintf_unfiltered (gdb_stdlog, "displaced: writing pc %.8lx\n",
4507 (unsigned long) val);
4508 switch (write_pc)
08216dd7 4509 {
cca44b1b 4510 case BRANCH_WRITE_PC:
36073a92 4511 branch_write_pc (regs, dsc, val);
08216dd7
RE
4512 break;
4513
cca44b1b
JB
4514 case BX_WRITE_PC:
4515 bx_write_pc (regs, val);
4516 break;
4517
4518 case LOAD_WRITE_PC:
36073a92 4519 load_write_pc (regs, dsc, val);
cca44b1b
JB
4520 break;
4521
4522 case ALU_WRITE_PC:
36073a92 4523 alu_write_pc (regs, dsc, val);
cca44b1b
JB
4524 break;
4525
4526 case CANNOT_WRITE_PC:
4527 warning (_("Instruction wrote to PC in an unexpected way when "
4528 "single-stepping"));
08216dd7
RE
4529 break;
4530
4531 default:
97b9747c
JB
4532 internal_error (__FILE__, __LINE__,
4533 _("Invalid argument to displaced_write_reg"));
08216dd7 4534 }
b508a996 4535
cca44b1b 4536 dsc->wrote_to_pc = 1;
b508a996 4537 }
ed9a39eb 4538 else
b508a996 4539 {
cca44b1b
JB
4540 if (debug_displaced)
4541 fprintf_unfiltered (gdb_stdlog, "displaced: writing r%d value %.8lx\n",
4542 regno, (unsigned long) val);
4543 regcache_cooked_write_unsigned (regs, regno, val);
b508a996 4544 }
34e8f22d
RE
4545}
4546
cca44b1b
JB
4547/* This function is used to concisely determine if an instruction INSN
4548 references PC. Register fields of interest in INSN should have the
0963b4bd
MS
4549 corresponding fields of BITMASK set to 0b1111. The function
4550 returns return 1 if any of these fields in INSN reference the PC
4551 (also 0b1111, r15), else it returns 0. */
67255d04
RE
4552
4553static int
cca44b1b 4554insn_references_pc (uint32_t insn, uint32_t bitmask)
67255d04 4555{
cca44b1b 4556 uint32_t lowbit = 1;
67255d04 4557
cca44b1b
JB
4558 while (bitmask != 0)
4559 {
4560 uint32_t mask;
44e1a9eb 4561
cca44b1b
JB
4562 for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
4563 ;
67255d04 4564
cca44b1b
JB
4565 if (!lowbit)
4566 break;
67255d04 4567
cca44b1b 4568 mask = lowbit * 0xf;
67255d04 4569
cca44b1b
JB
4570 if ((insn & mask) == mask)
4571 return 1;
4572
4573 bitmask &= ~mask;
67255d04
RE
4574 }
4575
cca44b1b
JB
4576 return 0;
4577}
2af48f68 4578
cca44b1b
JB
4579/* The simplest copy function. Many instructions have the same effect no
4580 matter what address they are executed at: in those cases, use this. */
67255d04 4581
cca44b1b 4582static int
7ff120b4 4583arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn,
cfba9872 4584 const char *iname, arm_displaced_step_closure *dsc)
cca44b1b
JB
4585{
4586 if (debug_displaced)
4587 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx, "
4588 "opcode/class '%s' unmodified\n", (unsigned long) insn,
4589 iname);
67255d04 4590
cca44b1b 4591 dsc->modinsn[0] = insn;
67255d04 4592
cca44b1b
JB
4593 return 0;
4594}
4595
34518530
YQ
4596static int
4597thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
4598 uint16_t insn2, const char *iname,
cfba9872 4599 arm_displaced_step_closure *dsc)
34518530
YQ
4600{
4601 if (debug_displaced)
4602 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x %.4x, "
4603 "opcode/class '%s' unmodified\n", insn1, insn2,
4604 iname);
4605
4606 dsc->modinsn[0] = insn1;
4607 dsc->modinsn[1] = insn2;
4608 dsc->numinsns = 2;
4609
4610 return 0;
4611}
4612
4613/* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
4614 modification. */
4615static int
615234c1 4616thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, uint16_t insn,
34518530 4617 const char *iname,
cfba9872 4618 arm_displaced_step_closure *dsc)
34518530
YQ
4619{
4620 if (debug_displaced)
4621 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x, "
4622 "opcode/class '%s' unmodified\n", insn,
4623 iname);
4624
4625 dsc->modinsn[0] = insn;
4626
4627 return 0;
4628}
4629
cca44b1b
JB
4630/* Preload instructions with immediate offset. */
4631
4632static void
6e39997a 4633cleanup_preload (struct gdbarch *gdbarch,
cfba9872 4634 struct regcache *regs, arm_displaced_step_closure *dsc)
cca44b1b
JB
4635{
4636 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
4637 if (!dsc->u.preload.immed)
4638 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
4639}
4640
7ff120b4
YQ
4641static void
4642install_preload (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 4643 arm_displaced_step_closure *dsc, unsigned int rn)
cca44b1b 4644{
cca44b1b 4645 ULONGEST rn_val;
cca44b1b
JB
4646 /* Preload instructions:
4647
4648 {pli/pld} [rn, #+/-imm]
4649 ->
4650 {pli/pld} [r0, #+/-imm]. */
4651
36073a92
YQ
4652 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
4653 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 4654 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
cca44b1b
JB
4655 dsc->u.preload.immed = 1;
4656
cca44b1b 4657 dsc->cleanup = &cleanup_preload;
cca44b1b
JB
4658}
4659
cca44b1b 4660static int
7ff120b4 4661arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
cfba9872 4662 arm_displaced_step_closure *dsc)
cca44b1b
JB
4663{
4664 unsigned int rn = bits (insn, 16, 19);
cca44b1b 4665
7ff120b4
YQ
4666 if (!insn_references_pc (insn, 0x000f0000ul))
4667 return arm_copy_unmodified (gdbarch, insn, "preload", dsc);
cca44b1b
JB
4668
4669 if (debug_displaced)
4670 fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
4671 (unsigned long) insn);
4672
7ff120b4
YQ
4673 dsc->modinsn[0] = insn & 0xfff0ffff;
4674
4675 install_preload (gdbarch, regs, dsc, rn);
4676
4677 return 0;
4678}
4679
34518530
YQ
4680static int
4681thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
cfba9872 4682 struct regcache *regs, arm_displaced_step_closure *dsc)
34518530
YQ
4683{
4684 unsigned int rn = bits (insn1, 0, 3);
4685 unsigned int u_bit = bit (insn1, 7);
4686 int imm12 = bits (insn2, 0, 11);
4687 ULONGEST pc_val;
4688
4689 if (rn != ARM_PC_REGNUM)
4690 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);
4691
4692 /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
4693 PLD (literal) Encoding T1. */
4694 if (debug_displaced)
4695 fprintf_unfiltered (gdb_stdlog,
4696 "displaced: copying pld/pli pc (0x%x) %c imm12 %.4x\n",
4697 (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
4698 imm12);
4699
4700 if (!u_bit)
4701 imm12 = -1 * imm12;
4702
4703 /* Rewrite instruction {pli/pld} PC imm12 into:
4704 Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12
4705
4706 {pli/pld} [r0, r1]
4707
4708 Cleanup: r0 <- tmp[0], r1 <- tmp[1]. */
4709
4710 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
4711 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
4712
4713 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
4714
4715 displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
4716 displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
4717 dsc->u.preload.immed = 0;
4718
4719 /* {pli/pld} [r0, r1] */
4720 dsc->modinsn[0] = insn1 & 0xfff0;
4721 dsc->modinsn[1] = 0xf001;
4722 dsc->numinsns = 2;
4723
4724 dsc->cleanup = &cleanup_preload;
4725 return 0;
4726}
4727
7ff120b4
YQ
4728/* Preload instructions with register offset. */
4729
4730static void
4731install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 4732 arm_displaced_step_closure *dsc, unsigned int rn,
7ff120b4
YQ
4733 unsigned int rm)
4734{
4735 ULONGEST rn_val, rm_val;
4736
cca44b1b
JB
4737 /* Preload register-offset instructions:
4738
4739 {pli/pld} [rn, rm {, shift}]
4740 ->
4741 {pli/pld} [r0, r1 {, shift}]. */
4742
36073a92
YQ
4743 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
4744 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
4745 rn_val = displaced_read_reg (regs, dsc, rn);
4746 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
4747 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
4748 displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
cca44b1b
JB
4749 dsc->u.preload.immed = 0;
4750
cca44b1b 4751 dsc->cleanup = &cleanup_preload;
7ff120b4
YQ
4752}
4753
4754static int
4755arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
4756 struct regcache *regs,
cfba9872 4757 arm_displaced_step_closure *dsc)
7ff120b4
YQ
4758{
4759 unsigned int rn = bits (insn, 16, 19);
4760 unsigned int rm = bits (insn, 0, 3);
4761
4762
4763 if (!insn_references_pc (insn, 0x000f000ful))
4764 return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);
4765
4766 if (debug_displaced)
4767 fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
4768 (unsigned long) insn);
4769
4770 dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
cca44b1b 4771
7ff120b4 4772 install_preload_reg (gdbarch, regs, dsc, rn, rm);
cca44b1b
JB
4773 return 0;
4774}
4775
4776/* Copy/cleanup coprocessor load and store instructions. */
4777
4778static void
6e39997a 4779cleanup_copro_load_store (struct gdbarch *gdbarch,
cca44b1b 4780 struct regcache *regs,
cfba9872 4781 arm_displaced_step_closure *dsc)
cca44b1b 4782{
36073a92 4783 ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
4784
4785 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
4786
4787 if (dsc->u.ldst.writeback)
4788 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
4789}
4790
7ff120b4
YQ
4791static void
4792install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 4793 arm_displaced_step_closure *dsc,
7ff120b4 4794 int writeback, unsigned int rn)
cca44b1b 4795{
cca44b1b 4796 ULONGEST rn_val;
cca44b1b 4797
cca44b1b
JB
4798 /* Coprocessor load/store instructions:
4799
4800 {stc/stc2} [<Rn>, #+/-imm] (and other immediate addressing modes)
4801 ->
4802 {stc/stc2} [r0, #+/-imm].
4803
4804 ldc/ldc2 are handled identically. */
4805
36073a92
YQ
4806 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
4807 rn_val = displaced_read_reg (regs, dsc, rn);
2b16b2e3
YQ
4808 /* PC should be 4-byte aligned. */
4809 rn_val = rn_val & 0xfffffffc;
cca44b1b
JB
4810 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
4811
7ff120b4 4812 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
4813 dsc->u.ldst.rn = rn;
4814
7ff120b4
YQ
4815 dsc->cleanup = &cleanup_copro_load_store;
4816}
4817
4818static int
4819arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
4820 struct regcache *regs,
cfba9872 4821 arm_displaced_step_closure *dsc)
7ff120b4
YQ
4822{
4823 unsigned int rn = bits (insn, 16, 19);
4824
4825 if (!insn_references_pc (insn, 0x000f0000ul))
4826 return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);
4827
4828 if (debug_displaced)
4829 fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
4830 "load/store insn %.8lx\n", (unsigned long) insn);
4831
cca44b1b
JB
4832 dsc->modinsn[0] = insn & 0xfff0ffff;
4833
7ff120b4 4834 install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);
cca44b1b
JB
4835
4836 return 0;
4837}
4838
34518530
YQ
4839static int
4840thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
4841 uint16_t insn2, struct regcache *regs,
cfba9872 4842 arm_displaced_step_closure *dsc)
34518530
YQ
4843{
4844 unsigned int rn = bits (insn1, 0, 3);
4845
4846 if (rn != ARM_PC_REGNUM)
4847 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
4848 "copro load/store", dsc);
4849
4850 if (debug_displaced)
4851 fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
4852 "load/store insn %.4x%.4x\n", insn1, insn2);
4853
4854 dsc->modinsn[0] = insn1 & 0xfff0;
4855 dsc->modinsn[1] = insn2;
4856 dsc->numinsns = 2;
4857
4858 /* This function is called for copying instruction LDC/LDC2/VLDR, which
4859 doesn't support writeback, so pass 0. */
4860 install_copro_load_store (gdbarch, regs, dsc, 0, rn);
4861
4862 return 0;
4863}
4864
cca44b1b
JB
4865/* Clean up branch instructions (actually perform the branch, by setting
4866 PC). */
4867
4868static void
6e39997a 4869cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 4870 arm_displaced_step_closure *dsc)
cca44b1b 4871{
36073a92 4872 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
4873 int branch_taken = condition_true (dsc->u.branch.cond, status);
4874 enum pc_write_style write_pc = dsc->u.branch.exchange
4875 ? BX_WRITE_PC : BRANCH_WRITE_PC;
4876
4877 if (!branch_taken)
4878 return;
4879
4880 if (dsc->u.branch.link)
4881 {
8c8dba6d
YQ
4882 /* The value of LR should be the next insn of current one. In order
4883 not to confuse logic hanlding later insn `bx lr', if current insn mode
4884 is Thumb, the bit 0 of LR value should be set to 1. */
4885 ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;
4886
4887 if (dsc->is_thumb)
4888 next_insn_addr |= 0x1;
4889
4890 displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
4891 CANNOT_WRITE_PC);
cca44b1b
JB
4892 }
4893
bf9f652a 4894 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
cca44b1b
JB
4895}
4896
4897/* Copy B/BL/BLX instructions with immediate destinations. */
4898
7ff120b4
YQ
4899static void
4900install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 4901 arm_displaced_step_closure *dsc,
7ff120b4
YQ
4902 unsigned int cond, int exchange, int link, long offset)
4903{
4904 /* Implement "BL<cond> <label>" as:
4905
4906 Preparation: cond <- instruction condition
4907 Insn: mov r0, r0 (nop)
4908 Cleanup: if (condition true) { r14 <- pc; pc <- label }.
4909
4910 B<cond> similar, but don't set r14 in cleanup. */
4911
4912 dsc->u.branch.cond = cond;
4913 dsc->u.branch.link = link;
4914 dsc->u.branch.exchange = exchange;
4915
2b16b2e3
YQ
4916 dsc->u.branch.dest = dsc->insn_addr;
4917 if (link && exchange)
4918 /* For BLX, offset is computed from the Align (PC, 4). */
4919 dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;
4920
7ff120b4 4921 if (dsc->is_thumb)
2b16b2e3 4922 dsc->u.branch.dest += 4 + offset;
7ff120b4 4923 else
2b16b2e3 4924 dsc->u.branch.dest += 8 + offset;
7ff120b4
YQ
4925
4926 dsc->cleanup = &cleanup_branch;
4927}
cca44b1b 4928static int
7ff120b4 4929arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
cfba9872 4930 struct regcache *regs, arm_displaced_step_closure *dsc)
cca44b1b
JB
4931{
4932 unsigned int cond = bits (insn, 28, 31);
4933 int exchange = (cond == 0xf);
4934 int link = exchange || bit (insn, 24);
cca44b1b
JB
4935 long offset;
4936
4937 if (debug_displaced)
4938 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s immediate insn "
4939 "%.8lx\n", (exchange) ? "blx" : (link) ? "bl" : "b",
4940 (unsigned long) insn);
cca44b1b
JB
4941 if (exchange)
4942 /* For BLX, set bit 0 of the destination. The cleanup_branch function will
4943 then arrange the switch into Thumb mode. */
4944 offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
4945 else
4946 offset = bits (insn, 0, 23) << 2;
4947
4948 if (bit (offset, 25))
4949 offset = offset | ~0x3ffffff;
4950
cca44b1b
JB
4951 dsc->modinsn[0] = ARM_NOP;
4952
7ff120b4 4953 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
cca44b1b
JB
4954 return 0;
4955}
4956
34518530
YQ
4957static int
4958thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
4959 uint16_t insn2, struct regcache *regs,
cfba9872 4960 arm_displaced_step_closure *dsc)
34518530
YQ
4961{
4962 int link = bit (insn2, 14);
4963 int exchange = link && !bit (insn2, 12);
4964 int cond = INST_AL;
4965 long offset = 0;
4966 int j1 = bit (insn2, 13);
4967 int j2 = bit (insn2, 11);
4968 int s = sbits (insn1, 10, 10);
4969 int i1 = !(j1 ^ bit (insn1, 10));
4970 int i2 = !(j2 ^ bit (insn1, 10));
4971
4972 if (!link && !exchange) /* B */
4973 {
4974 offset = (bits (insn2, 0, 10) << 1);
4975 if (bit (insn2, 12)) /* Encoding T4 */
4976 {
4977 offset |= (bits (insn1, 0, 9) << 12)
4978 | (i2 << 22)
4979 | (i1 << 23)
4980 | (s << 24);
4981 cond = INST_AL;
4982 }
4983 else /* Encoding T3 */
4984 {
4985 offset |= (bits (insn1, 0, 5) << 12)
4986 | (j1 << 18)
4987 | (j2 << 19)
4988 | (s << 20);
4989 cond = bits (insn1, 6, 9);
4990 }
4991 }
4992 else
4993 {
4994 offset = (bits (insn1, 0, 9) << 12);
4995 offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
4996 offset |= exchange ?
4997 (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
4998 }
4999
5000 if (debug_displaced)
5001 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s insn "
5002 "%.4x %.4x with offset %.8lx\n",
5003 link ? (exchange) ? "blx" : "bl" : "b",
5004 insn1, insn2, offset);
5005
5006 dsc->modinsn[0] = THUMB_NOP;
5007
5008 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
5009 return 0;
5010}
5011
5012/* Copy B Thumb instructions. */
5013static int
615234c1 5014thumb_copy_b (struct gdbarch *gdbarch, uint16_t insn,
cfba9872 5015 arm_displaced_step_closure *dsc)
34518530
YQ
5016{
5017 unsigned int cond = 0;
5018 int offset = 0;
5019 unsigned short bit_12_15 = bits (insn, 12, 15);
5020 CORE_ADDR from = dsc->insn_addr;
5021
5022 if (bit_12_15 == 0xd)
5023 {
5024 /* offset = SignExtend (imm8:0, 32) */
5025 offset = sbits ((insn << 1), 0, 8);
5026 cond = bits (insn, 8, 11);
5027 }
5028 else if (bit_12_15 == 0xe) /* Encoding T2 */
5029 {
5030 offset = sbits ((insn << 1), 0, 11);
5031 cond = INST_AL;
5032 }
5033
5034 if (debug_displaced)
5035 fprintf_unfiltered (gdb_stdlog,
5036 "displaced: copying b immediate insn %.4x "
5037 "with offset %d\n", insn, offset);
5038
5039 dsc->u.branch.cond = cond;
5040 dsc->u.branch.link = 0;
5041 dsc->u.branch.exchange = 0;
5042 dsc->u.branch.dest = from + 4 + offset;
5043
5044 dsc->modinsn[0] = THUMB_NOP;
5045
5046 dsc->cleanup = &cleanup_branch;
5047
5048 return 0;
5049}
5050
cca44b1b
JB
5051/* Copy BX/BLX with register-specified destinations. */
5052
7ff120b4
YQ
5053static void
5054install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 5055 arm_displaced_step_closure *dsc, int link,
7ff120b4 5056 unsigned int cond, unsigned int rm)
cca44b1b 5057{
cca44b1b
JB
5058 /* Implement {BX,BLX}<cond> <reg>" as:
5059
5060 Preparation: cond <- instruction condition
5061 Insn: mov r0, r0 (nop)
5062 Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
5063
5064 Don't set r14 in cleanup for BX. */
5065
36073a92 5066 dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5067
5068 dsc->u.branch.cond = cond;
5069 dsc->u.branch.link = link;
cca44b1b 5070
7ff120b4 5071 dsc->u.branch.exchange = 1;
cca44b1b
JB
5072
5073 dsc->cleanup = &cleanup_branch;
7ff120b4 5074}
cca44b1b 5075
7ff120b4
YQ
5076static int
5077arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
cfba9872 5078 struct regcache *regs, arm_displaced_step_closure *dsc)
7ff120b4
YQ
5079{
5080 unsigned int cond = bits (insn, 28, 31);
5081 /* BX: x12xxx1x
5082 BLX: x12xxx3x. */
5083 int link = bit (insn, 5);
5084 unsigned int rm = bits (insn, 0, 3);
5085
5086 if (debug_displaced)
5087 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx",
5088 (unsigned long) insn);
5089
5090 dsc->modinsn[0] = ARM_NOP;
5091
5092 install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
cca44b1b
JB
5093 return 0;
5094}
5095
34518530
YQ
5096static int
5097thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
5098 struct regcache *regs,
cfba9872 5099 arm_displaced_step_closure *dsc)
34518530
YQ
5100{
5101 int link = bit (insn, 7);
5102 unsigned int rm = bits (insn, 3, 6);
5103
5104 if (debug_displaced)
5105 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x",
5106 (unsigned short) insn);
5107
5108 dsc->modinsn[0] = THUMB_NOP;
5109
5110 install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);
5111
5112 return 0;
5113}
5114
5115
0963b4bd 5116/* Copy/cleanup arithmetic/logic instruction with immediate RHS. */
cca44b1b
JB
5117
5118static void
6e39997a 5119cleanup_alu_imm (struct gdbarch *gdbarch,
cfba9872 5120 struct regcache *regs, arm_displaced_step_closure *dsc)
cca44b1b 5121{
36073a92 5122 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5123 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5124 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5125 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
5126}
5127
5128static int
7ff120b4 5129arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
cfba9872 5130 arm_displaced_step_closure *dsc)
cca44b1b
JB
5131{
5132 unsigned int rn = bits (insn, 16, 19);
5133 unsigned int rd = bits (insn, 12, 15);
5134 unsigned int op = bits (insn, 21, 24);
5135 int is_mov = (op == 0xd);
5136 ULONGEST rd_val, rn_val;
cca44b1b
JB
5137
5138 if (!insn_references_pc (insn, 0x000ff000ul))
7ff120b4 5139 return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
cca44b1b
JB
5140
5141 if (debug_displaced)
5142 fprintf_unfiltered (gdb_stdlog, "displaced: copying immediate %s insn "
5143 "%.8lx\n", is_mov ? "move" : "ALU",
5144 (unsigned long) insn);
5145
5146 /* Instruction is of form:
5147
5148 <op><cond> rd, [rn,] #imm
5149
5150 Rewrite as:
5151
5152 Preparation: tmp1, tmp2 <- r0, r1;
5153 r0, r1 <- rd, rn
5154 Insn: <op><cond> r0, r1, #imm
5155 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
5156 */
5157
36073a92
YQ
5158 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5159 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5160 rn_val = displaced_read_reg (regs, dsc, rn);
5161 rd_val = displaced_read_reg (regs, dsc, rd);
cca44b1b
JB
5162 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5163 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5164 dsc->rd = rd;
5165
5166 if (is_mov)
5167 dsc->modinsn[0] = insn & 0xfff00fff;
5168 else
5169 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
5170
5171 dsc->cleanup = &cleanup_alu_imm;
5172
5173 return 0;
5174}
5175
34518530
YQ
5176static int
5177thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
5178 uint16_t insn2, struct regcache *regs,
cfba9872 5179 arm_displaced_step_closure *dsc)
34518530
YQ
5180{
5181 unsigned int op = bits (insn1, 5, 8);
5182 unsigned int rn, rm, rd;
5183 ULONGEST rd_val, rn_val;
5184
5185 rn = bits (insn1, 0, 3); /* Rn */
5186 rm = bits (insn2, 0, 3); /* Rm */
5187 rd = bits (insn2, 8, 11); /* Rd */
5188
5189 /* This routine is only called for instruction MOV. */
5190 gdb_assert (op == 0x2 && rn == 0xf);
5191
5192 if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
5193 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);
5194
5195 if (debug_displaced)
5196 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.4x%.4x\n",
5197 "ALU", insn1, insn2);
5198
5199 /* Instruction is of form:
5200
5201 <op><cond> rd, [rn,] #imm
5202
5203 Rewrite as:
5204
5205 Preparation: tmp1, tmp2 <- r0, r1;
5206 r0, r1 <- rd, rn
5207 Insn: <op><cond> r0, r1, #imm
5208 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
5209 */
5210
5211 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5212 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5213 rn_val = displaced_read_reg (regs, dsc, rn);
5214 rd_val = displaced_read_reg (regs, dsc, rd);
5215 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5216 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5217 dsc->rd = rd;
5218
5219 dsc->modinsn[0] = insn1;
5220 dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
5221 dsc->numinsns = 2;
5222
5223 dsc->cleanup = &cleanup_alu_imm;
5224
5225 return 0;
5226}
5227
cca44b1b
JB
5228/* Copy/cleanup arithmetic/logic insns with register RHS. */
5229
5230static void
6e39997a 5231cleanup_alu_reg (struct gdbarch *gdbarch,
cfba9872 5232 struct regcache *regs, arm_displaced_step_closure *dsc)
cca44b1b
JB
5233{
5234 ULONGEST rd_val;
5235 int i;
5236
36073a92 5237 rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5238
5239 for (i = 0; i < 3; i++)
5240 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
5241
5242 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
5243}
5244
7ff120b4
YQ
5245static void
5246install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 5247 arm_displaced_step_closure *dsc,
7ff120b4 5248 unsigned int rd, unsigned int rn, unsigned int rm)
cca44b1b 5249{
cca44b1b 5250 ULONGEST rd_val, rn_val, rm_val;
cca44b1b 5251
cca44b1b
JB
5252 /* Instruction is of form:
5253
5254 <op><cond> rd, [rn,] rm [, <shift>]
5255
5256 Rewrite as:
5257
5258 Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
5259 r0, r1, r2 <- rd, rn, rm
ef713951 5260 Insn: <op><cond> r0, [r1,] r2 [, <shift>]
cca44b1b
JB
5261 Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
5262 */
5263
36073a92
YQ
5264 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5265 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5266 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
5267 rd_val = displaced_read_reg (regs, dsc, rd);
5268 rn_val = displaced_read_reg (regs, dsc, rn);
5269 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5270 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5271 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5272 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
5273 dsc->rd = rd;
5274
7ff120b4
YQ
5275 dsc->cleanup = &cleanup_alu_reg;
5276}
5277
5278static int
5279arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
cfba9872 5280 arm_displaced_step_closure *dsc)
7ff120b4
YQ
5281{
5282 unsigned int op = bits (insn, 21, 24);
5283 int is_mov = (op == 0xd);
5284
5285 if (!insn_references_pc (insn, 0x000ff00ful))
5286 return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);
5287
5288 if (debug_displaced)
5289 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.8lx\n",
5290 is_mov ? "move" : "ALU", (unsigned long) insn);
5291
cca44b1b
JB
5292 if (is_mov)
5293 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
5294 else
5295 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
5296
7ff120b4
YQ
5297 install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
5298 bits (insn, 0, 3));
cca44b1b
JB
5299 return 0;
5300}
5301
34518530
YQ
5302static int
5303thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
5304 struct regcache *regs,
cfba9872 5305 arm_displaced_step_closure *dsc)
34518530 5306{
ef713951 5307 unsigned rm, rd;
34518530 5308
ef713951
YQ
5309 rm = bits (insn, 3, 6);
5310 rd = (bit (insn, 7) << 3) | bits (insn, 0, 2);
34518530 5311
ef713951 5312 if (rd != ARM_PC_REGNUM && rm != ARM_PC_REGNUM)
34518530
YQ
5313 return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);
5314
5315 if (debug_displaced)
ef713951
YQ
5316 fprintf_unfiltered (gdb_stdlog, "displaced: copying ALU reg insn %.4x\n",
5317 (unsigned short) insn);
34518530 5318
ef713951 5319 dsc->modinsn[0] = ((insn & 0xff00) | 0x10);
34518530 5320
ef713951 5321 install_alu_reg (gdbarch, regs, dsc, rd, rd, rm);
34518530
YQ
5322
5323 return 0;
5324}
5325
cca44b1b
JB
5326/* Cleanup/copy arithmetic/logic insns with shifted register RHS. */
5327
5328static void
6e39997a 5329cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
cca44b1b 5330 struct regcache *regs,
cfba9872 5331 arm_displaced_step_closure *dsc)
cca44b1b 5332{
36073a92 5333 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5334 int i;
5335
5336 for (i = 0; i < 4; i++)
5337 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
5338
5339 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
5340}
5341
7ff120b4
YQ
5342static void
5343install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 5344 arm_displaced_step_closure *dsc,
7ff120b4
YQ
5345 unsigned int rd, unsigned int rn, unsigned int rm,
5346 unsigned rs)
cca44b1b 5347{
7ff120b4 5348 int i;
cca44b1b 5349 ULONGEST rd_val, rn_val, rm_val, rs_val;
cca44b1b 5350
cca44b1b
JB
5351 /* Instruction is of form:
5352
5353 <op><cond> rd, [rn,] rm, <shift> rs
5354
5355 Rewrite as:
5356
5357 Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
5358 r0, r1, r2, r3 <- rd, rn, rm, rs
5359 Insn: <op><cond> r0, r1, r2, <shift> r3
5360 Cleanup: tmp5 <- r0
5361 r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
5362 rd <- tmp5
5363 */
5364
5365 for (i = 0; i < 4; i++)
36073a92 5366 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b 5367
36073a92
YQ
5368 rd_val = displaced_read_reg (regs, dsc, rd);
5369 rn_val = displaced_read_reg (regs, dsc, rn);
5370 rm_val = displaced_read_reg (regs, dsc, rm);
5371 rs_val = displaced_read_reg (regs, dsc, rs);
cca44b1b
JB
5372 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5373 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5374 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
5375 displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
5376 dsc->rd = rd;
7ff120b4
YQ
5377 dsc->cleanup = &cleanup_alu_shifted_reg;
5378}
5379
5380static int
5381arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
5382 struct regcache *regs,
cfba9872 5383 arm_displaced_step_closure *dsc)
7ff120b4
YQ
5384{
5385 unsigned int op = bits (insn, 21, 24);
5386 int is_mov = (op == 0xd);
5387 unsigned int rd, rn, rm, rs;
5388
5389 if (!insn_references_pc (insn, 0x000fff0ful))
5390 return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
5391
5392 if (debug_displaced)
5393 fprintf_unfiltered (gdb_stdlog, "displaced: copying shifted reg %s insn "
5394 "%.8lx\n", is_mov ? "move" : "ALU",
5395 (unsigned long) insn);
5396
5397 rn = bits (insn, 16, 19);
5398 rm = bits (insn, 0, 3);
5399 rs = bits (insn, 8, 11);
5400 rd = bits (insn, 12, 15);
cca44b1b
JB
5401
5402 if (is_mov)
5403 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
5404 else
5405 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
5406
7ff120b4 5407 install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);
cca44b1b
JB
5408
5409 return 0;
5410}
5411
5412/* Clean up load instructions. */
5413
5414static void
6e39997a 5415cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 5416 arm_displaced_step_closure *dsc)
cca44b1b
JB
5417{
5418 ULONGEST rt_val, rt_val2 = 0, rn_val;
cca44b1b 5419
36073a92 5420 rt_val = displaced_read_reg (regs, dsc, 0);
cca44b1b 5421 if (dsc->u.ldst.xfersize == 8)
36073a92
YQ
5422 rt_val2 = displaced_read_reg (regs, dsc, 1);
5423 rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
5424
5425 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5426 if (dsc->u.ldst.xfersize > 4)
5427 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5428 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
5429 if (!dsc->u.ldst.immed)
5430 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
5431
5432 /* Handle register writeback. */
5433 if (dsc->u.ldst.writeback)
5434 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
5435 /* Put result in right place. */
5436 displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
5437 if (dsc->u.ldst.xfersize == 8)
5438 displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
5439}
5440
5441/* Clean up store instructions. */
5442
5443static void
6e39997a 5444cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 5445 arm_displaced_step_closure *dsc)
cca44b1b 5446{
36073a92 5447 ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
5448
5449 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5450 if (dsc->u.ldst.xfersize > 4)
5451 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5452 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
5453 if (!dsc->u.ldst.immed)
5454 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
5455 if (!dsc->u.ldst.restore_r4)
5456 displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
5457
5458 /* Writeback. */
5459 if (dsc->u.ldst.writeback)
5460 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
5461}
5462
5463/* Copy "extra" load/store instructions. These are halfword/doubleword
5464 transfers, which have a different encoding to byte/word transfers. */
5465
5466static int
550dc4e2 5467arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unprivileged,
cfba9872 5468 struct regcache *regs, arm_displaced_step_closure *dsc)
cca44b1b
JB
5469{
5470 unsigned int op1 = bits (insn, 20, 24);
5471 unsigned int op2 = bits (insn, 5, 6);
5472 unsigned int rt = bits (insn, 12, 15);
5473 unsigned int rn = bits (insn, 16, 19);
5474 unsigned int rm = bits (insn, 0, 3);
5475 char load[12] = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
5476 char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
5477 int immed = (op1 & 0x4) != 0;
5478 int opcode;
5479 ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
cca44b1b
JB
5480
5481 if (!insn_references_pc (insn, 0x000ff00ful))
7ff120b4 5482 return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);
cca44b1b
JB
5483
5484 if (debug_displaced)
5485 fprintf_unfiltered (gdb_stdlog, "displaced: copying %sextra load/store "
550dc4e2 5486 "insn %.8lx\n", unprivileged ? "unprivileged " : "",
cca44b1b
JB
5487 (unsigned long) insn);
5488
5489 opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
5490
5491 if (opcode < 0)
5492 internal_error (__FILE__, __LINE__,
5493 _("copy_extra_ld_st: instruction decode error"));
5494
36073a92
YQ
5495 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5496 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5497 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 5498 if (!immed)
36073a92 5499 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 5500
36073a92 5501 rt_val = displaced_read_reg (regs, dsc, rt);
cca44b1b 5502 if (bytesize[opcode] == 8)
36073a92
YQ
5503 rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
5504 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 5505 if (!immed)
36073a92 5506 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5507
5508 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
5509 if (bytesize[opcode] == 8)
5510 displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
5511 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
5512 if (!immed)
5513 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
5514
5515 dsc->rd = rt;
5516 dsc->u.ldst.xfersize = bytesize[opcode];
5517 dsc->u.ldst.rn = rn;
5518 dsc->u.ldst.immed = immed;
5519 dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
5520 dsc->u.ldst.restore_r4 = 0;
5521
5522 if (immed)
5523 /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
5524 ->
5525 {ldr,str}<width><cond> r0, [r1,] [r2, #imm]. */
5526 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
5527 else
5528 /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
5529 ->
5530 {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3]. */
5531 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
5532
5533 dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
5534
5535 return 0;
5536}
5537
0f6f04ba 5538/* Copy byte/half word/word loads and stores. */
cca44b1b 5539
7ff120b4 5540static void
0f6f04ba 5541install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 5542 arm_displaced_step_closure *dsc, int load,
0f6f04ba
YQ
5543 int immed, int writeback, int size, int usermode,
5544 int rt, int rm, int rn)
cca44b1b 5545{
cca44b1b 5546 ULONGEST rt_val, rn_val, rm_val = 0;
cca44b1b 5547
36073a92
YQ
5548 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5549 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 5550 if (!immed)
36073a92 5551 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 5552 if (!load)
36073a92 5553 dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);
cca44b1b 5554
36073a92
YQ
5555 rt_val = displaced_read_reg (regs, dsc, rt);
5556 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 5557 if (!immed)
36073a92 5558 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5559
5560 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
5561 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
5562 if (!immed)
5563 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
cca44b1b 5564 dsc->rd = rt;
0f6f04ba 5565 dsc->u.ldst.xfersize = size;
cca44b1b
JB
5566 dsc->u.ldst.rn = rn;
5567 dsc->u.ldst.immed = immed;
7ff120b4 5568 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
5569
5570 /* To write PC we can do:
5571
494e194e
YQ
5572 Before this sequence of instructions:
5573 r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
5574 r2 is the Rn value got from dispalced_read_reg.
5575
5576 Insn1: push {pc} Write address of STR instruction + offset on stack
5577 Insn2: pop {r4} Read it back from stack, r4 = addr(Insn1) + offset
5578 Insn3: sub r4, r4, pc r4 = addr(Insn1) + offset - pc
5579 = addr(Insn1) + offset - addr(Insn3) - 8
5580 = offset - 16
5581 Insn4: add r4, r4, #8 r4 = offset - 8
5582 Insn5: add r0, r0, r4 r0 = from + 8 + offset - 8
5583 = from + offset
5584 Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])
cca44b1b
JB
5585
5586 Otherwise we don't know what value to write for PC, since the offset is
494e194e
YQ
5587 architecture-dependent (sometimes PC+8, sometimes PC+12). More details
5588 of this can be found in Section "Saving from r15" in
5589 http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */
cca44b1b 5590
7ff120b4
YQ
5591 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
5592}
5593
34518530
YQ
5594
5595static int
5596thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
5597 uint16_t insn2, struct regcache *regs,
cfba9872 5598 arm_displaced_step_closure *dsc, int size)
34518530
YQ
5599{
5600 unsigned int u_bit = bit (insn1, 7);
5601 unsigned int rt = bits (insn2, 12, 15);
5602 int imm12 = bits (insn2, 0, 11);
5603 ULONGEST pc_val;
5604
5605 if (debug_displaced)
5606 fprintf_unfiltered (gdb_stdlog,
5607 "displaced: copying ldr pc (0x%x) R%d %c imm12 %.4x\n",
5608 (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
5609 imm12);
5610
5611 if (!u_bit)
5612 imm12 = -1 * imm12;
5613
5614 /* Rewrite instruction LDR Rt imm12 into:
5615
5616 Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12
5617
5618 LDR R0, R2, R3,
5619
5620 Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2]. */
5621
5622
5623 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5624 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
5625 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
5626
5627 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
5628
5629 pc_val = pc_val & 0xfffffffc;
5630
5631 displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
5632 displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);
5633
5634 dsc->rd = rt;
5635
5636 dsc->u.ldst.xfersize = size;
5637 dsc->u.ldst.immed = 0;
5638 dsc->u.ldst.writeback = 0;
5639 dsc->u.ldst.restore_r4 = 0;
5640
5641 /* LDR R0, R2, R3 */
5642 dsc->modinsn[0] = 0xf852;
5643 dsc->modinsn[1] = 0x3;
5644 dsc->numinsns = 2;
5645
5646 dsc->cleanup = &cleanup_load;
5647
5648 return 0;
5649}
5650
5651static int
5652thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
5653 uint16_t insn2, struct regcache *regs,
cfba9872 5654 arm_displaced_step_closure *dsc,
34518530
YQ
5655 int writeback, int immed)
5656{
5657 unsigned int rt = bits (insn2, 12, 15);
5658 unsigned int rn = bits (insn1, 0, 3);
5659 unsigned int rm = bits (insn2, 0, 3); /* Only valid if !immed. */
5660 /* In LDR (register), there is also a register Rm, which is not allowed to
5661 be PC, so we don't have to check it. */
5662
5663 if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
5664 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
5665 dsc);
5666
5667 if (debug_displaced)
5668 fprintf_unfiltered (gdb_stdlog,
5669 "displaced: copying ldr r%d [r%d] insn %.4x%.4x\n",
5670 rt, rn, insn1, insn2);
5671
5672 install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
5673 0, rt, rm, rn);
5674
5675 dsc->u.ldst.restore_r4 = 0;
5676
5677 if (immed)
5678 /* ldr[b]<cond> rt, [rn, #imm], etc.
5679 ->
5680 ldr[b]<cond> r0, [r2, #imm]. */
5681 {
5682 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
5683 dsc->modinsn[1] = insn2 & 0x0fff;
5684 }
5685 else
5686 /* ldr[b]<cond> rt, [rn, rm], etc.
5687 ->
5688 ldr[b]<cond> r0, [r2, r3]. */
5689 {
5690 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
5691 dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
5692 }
5693
5694 dsc->numinsns = 2;
5695
5696 return 0;
5697}
5698
5699
7ff120b4
YQ
5700static int
5701arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
5702 struct regcache *regs,
cfba9872 5703 arm_displaced_step_closure *dsc,
0f6f04ba 5704 int load, int size, int usermode)
7ff120b4
YQ
5705{
5706 int immed = !bit (insn, 25);
5707 int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
5708 unsigned int rt = bits (insn, 12, 15);
5709 unsigned int rn = bits (insn, 16, 19);
5710 unsigned int rm = bits (insn, 0, 3); /* Only valid if !immed. */
5711
5712 if (!insn_references_pc (insn, 0x000ff00ful))
5713 return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);
5714
5715 if (debug_displaced)
5716 fprintf_unfiltered (gdb_stdlog,
5717 "displaced: copying %s%s r%d [r%d] insn %.8lx\n",
0f6f04ba
YQ
5718 load ? (size == 1 ? "ldrb" : "ldr")
5719 : (size == 1 ? "strb" : "str"), usermode ? "t" : "",
7ff120b4
YQ
5720 rt, rn,
5721 (unsigned long) insn);
5722
0f6f04ba
YQ
5723 install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
5724 usermode, rt, rm, rn);
7ff120b4 5725
bf9f652a 5726 if (load || rt != ARM_PC_REGNUM)
cca44b1b
JB
5727 {
5728 dsc->u.ldst.restore_r4 = 0;
5729
5730 if (immed)
5731 /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
5732 ->
5733 {ldr,str}[b]<cond> r0, [r2, #imm]. */
5734 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
5735 else
5736 /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
5737 ->
5738 {ldr,str}[b]<cond> r0, [r2, r3]. */
5739 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
5740 }
5741 else
5742 {
5743 /* We need to use r4 as scratch. Make sure it's restored afterwards. */
5744 dsc->u.ldst.restore_r4 = 1;
494e194e
YQ
5745 dsc->modinsn[0] = 0xe92d8000; /* push {pc} */
5746 dsc->modinsn[1] = 0xe8bd0010; /* pop {r4} */
cca44b1b
JB
5747 dsc->modinsn[2] = 0xe044400f; /* sub r4, r4, pc. */
5748 dsc->modinsn[3] = 0xe2844008; /* add r4, r4, #8. */
5749 dsc->modinsn[4] = 0xe0800004; /* add r0, r0, r4. */
5750
5751 /* As above. */
5752 if (immed)
5753 dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
5754 else
5755 dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
5756
cca44b1b
JB
5757 dsc->numinsns = 6;
5758 }
5759
5760 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
5761
5762 return 0;
5763}
5764
5765/* Cleanup LDM instructions with fully-populated register list. This is an
5766 unfortunate corner case: it's impossible to implement correctly by modifying
5767 the instruction. The issue is as follows: we have an instruction,
5768
5769 ldm rN, {r0-r15}
5770
5771 which we must rewrite to avoid loading PC. A possible solution would be to
5772 do the load in two halves, something like (with suitable cleanup
5773 afterwards):
5774
5775 mov r8, rN
5776 ldm[id][ab] r8!, {r0-r7}
5777 str r7, <temp>
5778 ldm[id][ab] r8, {r7-r14}
5779 <bkpt>
5780
5781 but at present there's no suitable place for <temp>, since the scratch space
5782 is overwritten before the cleanup routine is called. For now, we simply
5783 emulate the instruction. */
5784
5785static void
5786cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 5787 arm_displaced_step_closure *dsc)
cca44b1b 5788{
cca44b1b
JB
5789 int inc = dsc->u.block.increment;
5790 int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
5791 int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
5792 uint32_t regmask = dsc->u.block.regmask;
5793 int regno = inc ? 0 : 15;
5794 CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
5795 int exception_return = dsc->u.block.load && dsc->u.block.user
5796 && (regmask & 0x8000) != 0;
36073a92 5797 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
5798 int do_transfer = condition_true (dsc->u.block.cond, status);
5799 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5800
5801 if (!do_transfer)
5802 return;
5803
5804 /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
5805 sensible we can do here. Complain loudly. */
5806 if (exception_return)
5807 error (_("Cannot single-step exception return"));
5808
5809 /* We don't handle any stores here for now. */
5810 gdb_assert (dsc->u.block.load != 0);
5811
5812 if (debug_displaced)
5813 fprintf_unfiltered (gdb_stdlog, "displaced: emulating block transfer: "
5814 "%s %s %s\n", dsc->u.block.load ? "ldm" : "stm",
5815 dsc->u.block.increment ? "inc" : "dec",
5816 dsc->u.block.before ? "before" : "after");
5817
5818 while (regmask)
5819 {
5820 uint32_t memword;
5821
5822 if (inc)
bf9f652a 5823 while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
cca44b1b
JB
5824 regno++;
5825 else
5826 while (regno >= 0 && (regmask & (1 << regno)) == 0)
5827 regno--;
5828
5829 xfer_addr += bump_before;
5830
5831 memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
5832 displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
5833
5834 xfer_addr += bump_after;
5835
5836 regmask &= ~(1 << regno);
5837 }
5838
5839 if (dsc->u.block.writeback)
5840 displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
5841 CANNOT_WRITE_PC);
5842}
5843
5844/* Clean up an STM which included the PC in the register list. */
5845
5846static void
5847cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 5848 arm_displaced_step_closure *dsc)
cca44b1b 5849{
36073a92 5850 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
5851 int store_executed = condition_true (dsc->u.block.cond, status);
5852 CORE_ADDR pc_stored_at, transferred_regs = bitcount (dsc->u.block.regmask);
5853 CORE_ADDR stm_insn_addr;
5854 uint32_t pc_val;
5855 long offset;
5856 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5857
5858 /* If condition code fails, there's nothing else to do. */
5859 if (!store_executed)
5860 return;
5861
5862 if (dsc->u.block.increment)
5863 {
5864 pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
5865
5866 if (dsc->u.block.before)
5867 pc_stored_at += 4;
5868 }
5869 else
5870 {
5871 pc_stored_at = dsc->u.block.xfer_addr;
5872
5873 if (dsc->u.block.before)
5874 pc_stored_at -= 4;
5875 }
5876
5877 pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
5878 stm_insn_addr = dsc->scratch_base;
5879 offset = pc_val - stm_insn_addr;
5880
5881 if (debug_displaced)
5882 fprintf_unfiltered (gdb_stdlog, "displaced: detected PC offset %.8lx for "
5883 "STM instruction\n", offset);
5884
5885 /* Rewrite the stored PC to the proper value for the non-displaced original
5886 instruction. */
5887 write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
5888 dsc->insn_addr + offset);
5889}
5890
5891/* Clean up an LDM which includes the PC in the register list. We clumped all
5892 the registers in the transferred list into a contiguous range r0...rX (to
5893 avoid loading PC directly and losing control of the debugged program), so we
5894 must undo that here. */
5895
5896static void
6e39997a 5897cleanup_block_load_pc (struct gdbarch *gdbarch,
cca44b1b 5898 struct regcache *regs,
cfba9872 5899 arm_displaced_step_closure *dsc)
cca44b1b 5900{
36073a92 5901 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
22e048c9 5902 int load_executed = condition_true (dsc->u.block.cond, status);
bf9f652a 5903 unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
cca44b1b
JB
5904 unsigned int regs_loaded = bitcount (mask);
5905 unsigned int num_to_shuffle = regs_loaded, clobbered;
5906
5907 /* The method employed here will fail if the register list is fully populated
5908 (we need to avoid loading PC directly). */
5909 gdb_assert (num_to_shuffle < 16);
5910
5911 if (!load_executed)
5912 return;
5913
5914 clobbered = (1 << num_to_shuffle) - 1;
5915
5916 while (num_to_shuffle > 0)
5917 {
5918 if ((mask & (1 << write_reg)) != 0)
5919 {
5920 unsigned int read_reg = num_to_shuffle - 1;
5921
5922 if (read_reg != write_reg)
5923 {
36073a92 5924 ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
cca44b1b
JB
5925 displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
5926 if (debug_displaced)
5927 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: move "
5928 "loaded register r%d to r%d\n"), read_reg,
5929 write_reg);
5930 }
5931 else if (debug_displaced)
5932 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: register "
5933 "r%d already in the right place\n"),
5934 write_reg);
5935
5936 clobbered &= ~(1 << write_reg);
5937
5938 num_to_shuffle--;
5939 }
5940
5941 write_reg--;
5942 }
5943
5944 /* Restore any registers we scribbled over. */
5945 for (write_reg = 0; clobbered != 0; write_reg++)
5946 {
5947 if ((clobbered & (1 << write_reg)) != 0)
5948 {
5949 displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
5950 CANNOT_WRITE_PC);
5951 if (debug_displaced)
5952 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: restored "
5953 "clobbered register r%d\n"), write_reg);
5954 clobbered &= ~(1 << write_reg);
5955 }
5956 }
5957
5958 /* Perform register writeback manually. */
5959 if (dsc->u.block.writeback)
5960 {
5961 ULONGEST new_rn_val = dsc->u.block.xfer_addr;
5962
5963 if (dsc->u.block.increment)
5964 new_rn_val += regs_loaded * 4;
5965 else
5966 new_rn_val -= regs_loaded * 4;
5967
5968 displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
5969 CANNOT_WRITE_PC);
5970 }
5971}
5972
5973/* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
5974 in user-level code (in particular exception return, ldm rn, {...pc}^). */
5975
5976static int
7ff120b4
YQ
5977arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
5978 struct regcache *regs,
cfba9872 5979 arm_displaced_step_closure *dsc)
cca44b1b
JB
5980{
5981 int load = bit (insn, 20);
5982 int user = bit (insn, 22);
5983 int increment = bit (insn, 23);
5984 int before = bit (insn, 24);
5985 int writeback = bit (insn, 21);
5986 int rn = bits (insn, 16, 19);
cca44b1b 5987
0963b4bd
MS
5988 /* Block transfers which don't mention PC can be run directly
5989 out-of-line. */
bf9f652a 5990 if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
7ff120b4 5991 return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
cca44b1b 5992
bf9f652a 5993 if (rn == ARM_PC_REGNUM)
cca44b1b 5994 {
0963b4bd
MS
5995 warning (_("displaced: Unpredictable LDM or STM with "
5996 "base register r15"));
7ff120b4 5997 return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
cca44b1b
JB
5998 }
5999
6000 if (debug_displaced)
6001 fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
6002 "%.8lx\n", (unsigned long) insn);
6003
36073a92 6004 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b
JB
6005 dsc->u.block.rn = rn;
6006
6007 dsc->u.block.load = load;
6008 dsc->u.block.user = user;
6009 dsc->u.block.increment = increment;
6010 dsc->u.block.before = before;
6011 dsc->u.block.writeback = writeback;
6012 dsc->u.block.cond = bits (insn, 28, 31);
6013
6014 dsc->u.block.regmask = insn & 0xffff;
6015
6016 if (load)
6017 {
6018 if ((insn & 0xffff) == 0xffff)
6019 {
6020 /* LDM with a fully-populated register list. This case is
6021 particularly tricky. Implement for now by fully emulating the
6022 instruction (which might not behave perfectly in all cases, but
6023 these instructions should be rare enough for that not to matter
6024 too much). */
6025 dsc->modinsn[0] = ARM_NOP;
6026
6027 dsc->cleanup = &cleanup_block_load_all;
6028 }
6029 else
6030 {
6031 /* LDM of a list of registers which includes PC. Implement by
6032 rewriting the list of registers to be transferred into a
6033 contiguous chunk r0...rX before doing the transfer, then shuffling
6034 registers into the correct places in the cleanup routine. */
6035 unsigned int regmask = insn & 0xffff;
bec2ab5a
SM
6036 unsigned int num_in_list = bitcount (regmask), new_regmask;
6037 unsigned int i;
cca44b1b
JB
6038
6039 for (i = 0; i < num_in_list; i++)
36073a92 6040 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b
JB
6041
6042 /* Writeback makes things complicated. We need to avoid clobbering
6043 the base register with one of the registers in our modified
6044 register list, but just using a different register can't work in
6045 all cases, e.g.:
6046
6047 ldm r14!, {r0-r13,pc}
6048
6049 which would need to be rewritten as:
6050
6051 ldm rN!, {r0-r14}
6052
6053 but that can't work, because there's no free register for N.
6054
6055 Solve this by turning off the writeback bit, and emulating
6056 writeback manually in the cleanup routine. */
6057
6058 if (writeback)
6059 insn &= ~(1 << 21);
6060
6061 new_regmask = (1 << num_in_list) - 1;
6062
6063 if (debug_displaced)
6064 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
6065 "{..., pc}: original reg list %.4x, modified "
6066 "list %.4x\n"), rn, writeback ? "!" : "",
6067 (int) insn & 0xffff, new_regmask);
6068
6069 dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
6070
6071 dsc->cleanup = &cleanup_block_load_pc;
6072 }
6073 }
6074 else
6075 {
6076 /* STM of a list of registers which includes PC. Run the instruction
6077 as-is, but out of line: this will store the wrong value for the PC,
6078 so we must manually fix up the memory in the cleanup routine.
6079 Doing things this way has the advantage that we can auto-detect
6080 the offset of the PC write (which is architecture-dependent) in
6081 the cleanup routine. */
6082 dsc->modinsn[0] = insn;
6083
6084 dsc->cleanup = &cleanup_block_store_pc;
6085 }
6086
6087 return 0;
6088}
6089
34518530
YQ
6090static int
6091thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
6092 struct regcache *regs,
cfba9872 6093 arm_displaced_step_closure *dsc)
cca44b1b 6094{
34518530
YQ
6095 int rn = bits (insn1, 0, 3);
6096 int load = bit (insn1, 4);
6097 int writeback = bit (insn1, 5);
cca44b1b 6098
34518530
YQ
6099 /* Block transfers which don't mention PC can be run directly
6100 out-of-line. */
6101 if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
6102 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);
7ff120b4 6103
34518530
YQ
6104 if (rn == ARM_PC_REGNUM)
6105 {
6106 warning (_("displaced: Unpredictable LDM or STM with "
6107 "base register r15"));
6108 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6109 "unpredictable ldm/stm", dsc);
6110 }
cca44b1b
JB
6111
6112 if (debug_displaced)
34518530
YQ
6113 fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
6114 "%.4x%.4x\n", insn1, insn2);
cca44b1b 6115
34518530
YQ
6116 /* Clear bit 13, since it should be always zero. */
6117 dsc->u.block.regmask = (insn2 & 0xdfff);
6118 dsc->u.block.rn = rn;
cca44b1b 6119
34518530
YQ
6120 dsc->u.block.load = load;
6121 dsc->u.block.user = 0;
6122 dsc->u.block.increment = bit (insn1, 7);
6123 dsc->u.block.before = bit (insn1, 8);
6124 dsc->u.block.writeback = writeback;
6125 dsc->u.block.cond = INST_AL;
6126 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b 6127
34518530
YQ
6128 if (load)
6129 {
6130 if (dsc->u.block.regmask == 0xffff)
6131 {
6132 /* This branch is impossible to happen. */
6133 gdb_assert (0);
6134 }
6135 else
6136 {
6137 unsigned int regmask = dsc->u.block.regmask;
bec2ab5a
SM
6138 unsigned int num_in_list = bitcount (regmask), new_regmask;
6139 unsigned int i;
34518530
YQ
6140
6141 for (i = 0; i < num_in_list; i++)
6142 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
6143
6144 if (writeback)
6145 insn1 &= ~(1 << 5);
6146
6147 new_regmask = (1 << num_in_list) - 1;
6148
6149 if (debug_displaced)
6150 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
6151 "{..., pc}: original reg list %.4x, modified "
6152 "list %.4x\n"), rn, writeback ? "!" : "",
6153 (int) dsc->u.block.regmask, new_regmask);
6154
6155 dsc->modinsn[0] = insn1;
6156 dsc->modinsn[1] = (new_regmask & 0xffff);
6157 dsc->numinsns = 2;
6158
6159 dsc->cleanup = &cleanup_block_load_pc;
6160 }
6161 }
6162 else
6163 {
6164 dsc->modinsn[0] = insn1;
6165 dsc->modinsn[1] = insn2;
6166 dsc->numinsns = 2;
6167 dsc->cleanup = &cleanup_block_store_pc;
6168 }
6169 return 0;
6170}
6171
d9311bfa
AT
6172/* Wrapper over read_memory_unsigned_integer for use in arm_get_next_pcs.
6173 This is used to avoid a dependency on BFD's bfd_endian enum. */
6174
6175ULONGEST
6176arm_get_next_pcs_read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
6177 int byte_order)
6178{
5f2dfcfd
AT
6179 return read_memory_unsigned_integer (memaddr, len,
6180 (enum bfd_endian) byte_order);
d9311bfa
AT
6181}
6182
6183/* Wrapper over gdbarch_addr_bits_remove for use in arm_get_next_pcs. */
6184
6185CORE_ADDR
6186arm_get_next_pcs_addr_bits_remove (struct arm_get_next_pcs *self,
6187 CORE_ADDR val)
6188{
ac7936df 6189 return gdbarch_addr_bits_remove (self->regcache->arch (), val);
d9311bfa
AT
6190}
6191
6192/* Wrapper over syscall_next_pc for use in get_next_pcs. */
6193
e7cf25a8 6194static CORE_ADDR
553cb527 6195arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self)
d9311bfa 6196{
d9311bfa
AT
6197 return 0;
6198}
6199
6200/* Wrapper over arm_is_thumb for use in arm_get_next_pcs. */
6201
6202int
6203arm_get_next_pcs_is_thumb (struct arm_get_next_pcs *self)
6204{
6205 return arm_is_thumb (self->regcache);
6206}
6207
6208/* single_step() is called just before we want to resume the inferior,
6209 if we want to single-step it but there is no hardware or kernel
6210 single-step support. We find the target of the coming instructions
6211 and breakpoint them. */
6212
a0ff9e1a 6213std::vector<CORE_ADDR>
f5ea389a 6214arm_software_single_step (struct regcache *regcache)
d9311bfa 6215{
ac7936df 6216 struct gdbarch *gdbarch = regcache->arch ();
d9311bfa 6217 struct arm_get_next_pcs next_pcs_ctx;
d9311bfa
AT
6218
6219 arm_get_next_pcs_ctor (&next_pcs_ctx,
6220 &arm_get_next_pcs_ops,
6221 gdbarch_byte_order (gdbarch),
6222 gdbarch_byte_order_for_code (gdbarch),
1b451dda 6223 0,
d9311bfa
AT
6224 regcache);
6225
a0ff9e1a 6226 std::vector<CORE_ADDR> next_pcs = arm_get_next_pcs (&next_pcs_ctx);
d9311bfa 6227
a0ff9e1a
SM
6228 for (CORE_ADDR &pc_ref : next_pcs)
6229 pc_ref = gdbarch_addr_bits_remove (gdbarch, pc_ref);
d9311bfa 6230
93f9a11f 6231 return next_pcs;
d9311bfa
AT
6232}
6233
34518530
YQ
6234/* Cleanup/copy SVC (SWI) instructions. These two functions are overridden
6235 for Linux, where some SVC instructions must be treated specially. */
6236
6237static void
6238cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 6239 arm_displaced_step_closure *dsc)
34518530
YQ
6240{
6241 CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
6242
6243 if (debug_displaced)
6244 fprintf_unfiltered (gdb_stdlog, "displaced: cleanup for svc, resume at "
6245 "%.8lx\n", (unsigned long) resume_addr);
6246
6247 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
6248}
6249
6250
6251/* Common copy routine for svc instruciton. */
6252
6253static int
6254install_svc (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 6255 arm_displaced_step_closure *dsc)
34518530
YQ
6256{
6257 /* Preparation: none.
6258 Insn: unmodified svc.
6259 Cleanup: pc <- insn_addr + insn_size. */
6260
6261 /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
6262 instruction. */
6263 dsc->wrote_to_pc = 1;
6264
6265 /* Allow OS-specific code to override SVC handling. */
bd18283a
YQ
6266 if (dsc->u.svc.copy_svc_os)
6267 return dsc->u.svc.copy_svc_os (gdbarch, regs, dsc);
6268 else
6269 {
6270 dsc->cleanup = &cleanup_svc;
6271 return 0;
6272 }
34518530
YQ
6273}
6274
6275static int
6276arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
cfba9872 6277 struct regcache *regs, arm_displaced_step_closure *dsc)
34518530
YQ
6278{
6279
6280 if (debug_displaced)
6281 fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.8lx\n",
6282 (unsigned long) insn);
6283
6284 dsc->modinsn[0] = insn;
6285
6286 return install_svc (gdbarch, regs, dsc);
6287}
6288
6289static int
6290thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
cfba9872 6291 struct regcache *regs, arm_displaced_step_closure *dsc)
34518530
YQ
6292{
6293
6294 if (debug_displaced)
6295 fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.4x\n",
6296 insn);
bd18283a 6297
34518530
YQ
6298 dsc->modinsn[0] = insn;
6299
6300 return install_svc (gdbarch, regs, dsc);
cca44b1b
JB
6301}
6302
6303/* Copy undefined instructions. */
6304
6305static int
7ff120b4 6306arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
cfba9872 6307 arm_displaced_step_closure *dsc)
cca44b1b
JB
6308{
6309 if (debug_displaced)
0963b4bd
MS
6310 fprintf_unfiltered (gdb_stdlog,
6311 "displaced: copying undefined insn %.8lx\n",
cca44b1b
JB
6312 (unsigned long) insn);
6313
6314 dsc->modinsn[0] = insn;
6315
6316 return 0;
6317}
6318
34518530
YQ
6319static int
6320thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
cfba9872 6321 arm_displaced_step_closure *dsc)
34518530
YQ
6322{
6323
6324 if (debug_displaced)
6325 fprintf_unfiltered (gdb_stdlog, "displaced: copying undefined insn "
6326 "%.4x %.4x\n", (unsigned short) insn1,
6327 (unsigned short) insn2);
6328
6329 dsc->modinsn[0] = insn1;
6330 dsc->modinsn[1] = insn2;
6331 dsc->numinsns = 2;
6332
6333 return 0;
6334}
6335
cca44b1b
JB
6336/* Copy unpredictable instructions. */
6337
6338static int
7ff120b4 6339arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
cfba9872 6340 arm_displaced_step_closure *dsc)
cca44b1b
JB
6341{
6342 if (debug_displaced)
6343 fprintf_unfiltered (gdb_stdlog, "displaced: copying unpredictable insn "
6344 "%.8lx\n", (unsigned long) insn);
6345
6346 dsc->modinsn[0] = insn;
6347
6348 return 0;
6349}
6350
6351/* The decode_* functions are instruction decoding helpers. They mostly follow
6352 the presentation in the ARM ARM. */
6353
6354static int
7ff120b4
YQ
6355arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
6356 struct regcache *regs,
cfba9872 6357 arm_displaced_step_closure *dsc)
cca44b1b
JB
6358{
6359 unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
6360 unsigned int rn = bits (insn, 16, 19);
6361
2f924de6 6362 if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0x1) == 0x0)
7ff120b4 6363 return arm_copy_unmodified (gdbarch, insn, "cps", dsc);
2f924de6 6364 else if (op1 == 0x10 && op2 == 0x0 && (rn & 0x1) == 0x1)
7ff120b4 6365 return arm_copy_unmodified (gdbarch, insn, "setend", dsc);
cca44b1b 6366 else if ((op1 & 0x60) == 0x20)
7ff120b4 6367 return arm_copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
cca44b1b 6368 else if ((op1 & 0x71) == 0x40)
7ff120b4
YQ
6369 return arm_copy_unmodified (gdbarch, insn, "neon elt/struct load/store",
6370 dsc);
cca44b1b 6371 else if ((op1 & 0x77) == 0x41)
7ff120b4 6372 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 6373 else if ((op1 & 0x77) == 0x45)
7ff120b4 6374 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pli. */
cca44b1b
JB
6375 else if ((op1 & 0x77) == 0x51)
6376 {
6377 if (rn != 0xf)
7ff120b4 6378 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b 6379 else
7ff120b4 6380 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
6381 }
6382 else if ((op1 & 0x77) == 0x55)
7ff120b4 6383 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b
JB
6384 else if (op1 == 0x57)
6385 switch (op2)
6386 {
7ff120b4
YQ
6387 case 0x1: return arm_copy_unmodified (gdbarch, insn, "clrex", dsc);
6388 case 0x4: return arm_copy_unmodified (gdbarch, insn, "dsb", dsc);
6389 case 0x5: return arm_copy_unmodified (gdbarch, insn, "dmb", dsc);
6390 case 0x6: return arm_copy_unmodified (gdbarch, insn, "isb", dsc);
6391 default: return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
6392 }
6393 else if ((op1 & 0x63) == 0x43)
7ff120b4 6394 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
6395 else if ((op2 & 0x1) == 0x0)
6396 switch (op1 & ~0x80)
6397 {
6398 case 0x61:
7ff120b4 6399 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 6400 case 0x65:
7ff120b4 6401 return arm_copy_preload_reg (gdbarch, insn, regs, dsc); /* pli reg. */
cca44b1b
JB
6402 case 0x71: case 0x75:
6403 /* pld/pldw reg. */
7ff120b4 6404 return arm_copy_preload_reg (gdbarch, insn, regs, dsc);
cca44b1b 6405 case 0x63: case 0x67: case 0x73: case 0x77:
7ff120b4 6406 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b 6407 default:
7ff120b4 6408 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6409 }
6410 else
7ff120b4 6411 return arm_copy_undef (gdbarch, insn, dsc); /* Probably unreachable. */
cca44b1b
JB
6412}
6413
6414static int
7ff120b4
YQ
6415arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
6416 struct regcache *regs,
cfba9872 6417 arm_displaced_step_closure *dsc)
cca44b1b
JB
6418{
6419 if (bit (insn, 27) == 0)
7ff120b4 6420 return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
cca44b1b
JB
6421 /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx. */
6422 else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
6423 {
6424 case 0x0: case 0x2:
7ff120b4 6425 return arm_copy_unmodified (gdbarch, insn, "srs", dsc);
cca44b1b
JB
6426
6427 case 0x1: case 0x3:
7ff120b4 6428 return arm_copy_unmodified (gdbarch, insn, "rfe", dsc);
cca44b1b
JB
6429
6430 case 0x4: case 0x5: case 0x6: case 0x7:
7ff120b4 6431 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b
JB
6432
6433 case 0x8:
6434 switch ((insn & 0xe00000) >> 21)
6435 {
6436 case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
6437 /* stc/stc2. */
7ff120b4 6438 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
6439
6440 case 0x2:
7ff120b4 6441 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b
JB
6442
6443 default:
7ff120b4 6444 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6445 }
6446
6447 case 0x9:
6448 {
6449 int rn_f = (bits (insn, 16, 19) == 0xf);
6450 switch ((insn & 0xe00000) >> 21)
6451 {
6452 case 0x1: case 0x3:
6453 /* ldc/ldc2 imm (undefined for rn == pc). */
7ff120b4
YQ
6454 return rn_f ? arm_copy_undef (gdbarch, insn, dsc)
6455 : arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
6456
6457 case 0x2:
7ff120b4 6458 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
6459
6460 case 0x4: case 0x5: case 0x6: case 0x7:
6461 /* ldc/ldc2 lit (undefined for rn != pc). */
7ff120b4
YQ
6462 return rn_f ? arm_copy_copro_load_store (gdbarch, insn, regs, dsc)
6463 : arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6464
6465 default:
7ff120b4 6466 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6467 }
6468 }
6469
6470 case 0xa:
7ff120b4 6471 return arm_copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
cca44b1b
JB
6472
6473 case 0xb:
6474 if (bits (insn, 16, 19) == 0xf)
6475 /* ldc/ldc2 lit. */
7ff120b4 6476 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 6477 else
7ff120b4 6478 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6479
6480 case 0xc:
6481 if (bit (insn, 4))
7ff120b4 6482 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 6483 else
7ff120b4 6484 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
6485
6486 case 0xd:
6487 if (bit (insn, 4))
7ff120b4 6488 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 6489 else
7ff120b4 6490 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
6491
6492 default:
7ff120b4 6493 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6494 }
6495}
6496
6497/* Decode miscellaneous instructions in dp/misc encoding space. */
6498
6499static int
7ff120b4
YQ
6500arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
6501 struct regcache *regs,
cfba9872 6502 arm_displaced_step_closure *dsc)
cca44b1b
JB
6503{
6504 unsigned int op2 = bits (insn, 4, 6);
6505 unsigned int op = bits (insn, 21, 22);
cca44b1b
JB
6506
6507 switch (op2)
6508 {
6509 case 0x0:
7ff120b4 6510 return arm_copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
cca44b1b
JB
6511
6512 case 0x1:
6513 if (op == 0x1) /* bx. */
7ff120b4 6514 return arm_copy_bx_blx_reg (gdbarch, insn, regs, dsc);
cca44b1b 6515 else if (op == 0x3)
7ff120b4 6516 return arm_copy_unmodified (gdbarch, insn, "clz", dsc);
cca44b1b 6517 else
7ff120b4 6518 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6519
6520 case 0x2:
6521 if (op == 0x1)
6522 /* Not really supported. */
7ff120b4 6523 return arm_copy_unmodified (gdbarch, insn, "bxj", dsc);
cca44b1b 6524 else
7ff120b4 6525 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6526
6527 case 0x3:
6528 if (op == 0x1)
7ff120b4 6529 return arm_copy_bx_blx_reg (gdbarch, insn,
0963b4bd 6530 regs, dsc); /* blx register. */
cca44b1b 6531 else
7ff120b4 6532 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6533
6534 case 0x5:
7ff120b4 6535 return arm_copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
cca44b1b
JB
6536
6537 case 0x7:
6538 if (op == 0x1)
7ff120b4 6539 return arm_copy_unmodified (gdbarch, insn, "bkpt", dsc);
cca44b1b
JB
6540 else if (op == 0x3)
6541 /* Not really supported. */
7ff120b4 6542 return arm_copy_unmodified (gdbarch, insn, "smc", dsc);
86a73007 6543 /* Fall through. */
cca44b1b
JB
6544
6545 default:
7ff120b4 6546 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6547 }
6548}
6549
6550static int
7ff120b4
YQ
6551arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
6552 struct regcache *regs,
cfba9872 6553 arm_displaced_step_closure *dsc)
cca44b1b
JB
6554{
6555 if (bit (insn, 25))
6556 switch (bits (insn, 20, 24))
6557 {
6558 case 0x10:
7ff120b4 6559 return arm_copy_unmodified (gdbarch, insn, "movw", dsc);
cca44b1b
JB
6560
6561 case 0x14:
7ff120b4 6562 return arm_copy_unmodified (gdbarch, insn, "movt", dsc);
cca44b1b
JB
6563
6564 case 0x12: case 0x16:
7ff120b4 6565 return arm_copy_unmodified (gdbarch, insn, "msr imm", dsc);
cca44b1b
JB
6566
6567 default:
7ff120b4 6568 return arm_copy_alu_imm (gdbarch, insn, regs, dsc);
cca44b1b
JB
6569 }
6570 else
6571 {
6572 uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
6573
6574 if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
7ff120b4 6575 return arm_copy_alu_reg (gdbarch, insn, regs, dsc);
cca44b1b 6576 else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
7ff120b4 6577 return arm_copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
cca44b1b 6578 else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
7ff120b4 6579 return arm_decode_miscellaneous (gdbarch, insn, regs, dsc);
cca44b1b 6580 else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
7ff120b4 6581 return arm_copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
cca44b1b 6582 else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
7ff120b4 6583 return arm_copy_unmodified (gdbarch, insn, "mul/mla", dsc);
cca44b1b 6584 else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
7ff120b4 6585 return arm_copy_unmodified (gdbarch, insn, "synch", dsc);
cca44b1b 6586 else if (op2 == 0xb || (op2 & 0xd) == 0xd)
550dc4e2 6587 /* 2nd arg means "unprivileged". */
7ff120b4
YQ
6588 return arm_copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
6589 dsc);
cca44b1b
JB
6590 }
6591
6592 /* Should be unreachable. */
6593 return 1;
6594}
6595
6596static int
7ff120b4
YQ
6597arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
6598 struct regcache *regs,
cfba9872 6599 arm_displaced_step_closure *dsc)
cca44b1b
JB
6600{
6601 int a = bit (insn, 25), b = bit (insn, 4);
6602 uint32_t op1 = bits (insn, 20, 24);
cca44b1b
JB
6603
6604 if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
6605 || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
0f6f04ba 6606 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 0);
cca44b1b
JB
6607 else if ((!a && (op1 & 0x17) == 0x02)
6608 || (a && (op1 & 0x17) == 0x02 && !b))
0f6f04ba 6609 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 1);
cca44b1b
JB
6610 else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
6611 || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
0f6f04ba 6612 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 0);
cca44b1b
JB
6613 else if ((!a && (op1 & 0x17) == 0x03)
6614 || (a && (op1 & 0x17) == 0x03 && !b))
0f6f04ba 6615 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 1);
cca44b1b
JB
6616 else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
6617 || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
7ff120b4 6618 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
cca44b1b
JB
6619 else if ((!a && (op1 & 0x17) == 0x06)
6620 || (a && (op1 & 0x17) == 0x06 && !b))
7ff120b4 6621 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
cca44b1b
JB
6622 else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
6623 || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
7ff120b4 6624 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
cca44b1b
JB
6625 else if ((!a && (op1 & 0x17) == 0x07)
6626 || (a && (op1 & 0x17) == 0x07 && !b))
7ff120b4 6627 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
cca44b1b
JB
6628
6629 /* Should be unreachable. */
6630 return 1;
6631}
6632
6633static int
7ff120b4 6634arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
cfba9872 6635 arm_displaced_step_closure *dsc)
cca44b1b
JB
6636{
6637 switch (bits (insn, 20, 24))
6638 {
6639 case 0x00: case 0x01: case 0x02: case 0x03:
7ff120b4 6640 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
cca44b1b
JB
6641
6642 case 0x04: case 0x05: case 0x06: case 0x07:
7ff120b4 6643 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
cca44b1b
JB
6644
6645 case 0x08: case 0x09: case 0x0a: case 0x0b:
6646 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
7ff120b4 6647 return arm_copy_unmodified (gdbarch, insn,
cca44b1b
JB
6648 "decode/pack/unpack/saturate/reverse", dsc);
6649
6650 case 0x18:
6651 if (bits (insn, 5, 7) == 0) /* op2. */
6652 {
6653 if (bits (insn, 12, 15) == 0xf)
7ff120b4 6654 return arm_copy_unmodified (gdbarch, insn, "usad8", dsc);
cca44b1b 6655 else
7ff120b4 6656 return arm_copy_unmodified (gdbarch, insn, "usada8", dsc);
cca44b1b
JB
6657 }
6658 else
7ff120b4 6659 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6660
6661 case 0x1a: case 0x1b:
6662 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 6663 return arm_copy_unmodified (gdbarch, insn, "sbfx", dsc);
cca44b1b 6664 else
7ff120b4 6665 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6666
6667 case 0x1c: case 0x1d:
6668 if (bits (insn, 5, 6) == 0x0) /* op2[1:0]. */
6669 {
6670 if (bits (insn, 0, 3) == 0xf)
7ff120b4 6671 return arm_copy_unmodified (gdbarch, insn, "bfc", dsc);
cca44b1b 6672 else
7ff120b4 6673 return arm_copy_unmodified (gdbarch, insn, "bfi", dsc);
cca44b1b
JB
6674 }
6675 else
7ff120b4 6676 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6677
6678 case 0x1e: case 0x1f:
6679 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 6680 return arm_copy_unmodified (gdbarch, insn, "ubfx", dsc);
cca44b1b 6681 else
7ff120b4 6682 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6683 }
6684
6685 /* Should be unreachable. */
6686 return 1;
6687}
6688
6689static int
615234c1 6690arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, uint32_t insn,
7ff120b4 6691 struct regcache *regs,
cfba9872 6692 arm_displaced_step_closure *dsc)
cca44b1b
JB
6693{
6694 if (bit (insn, 25))
7ff120b4 6695 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b 6696 else
7ff120b4 6697 return arm_copy_block_xfer (gdbarch, insn, regs, dsc);
cca44b1b
JB
6698}
6699
6700static int
7ff120b4
YQ
6701arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
6702 struct regcache *regs,
cfba9872 6703 arm_displaced_step_closure *dsc)
cca44b1b
JB
6704{
6705 unsigned int opcode = bits (insn, 20, 24);
6706
6707 switch (opcode)
6708 {
6709 case 0x04: case 0x05: /* VFP/Neon mrrc/mcrr. */
7ff120b4 6710 return arm_copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
cca44b1b
JB
6711
6712 case 0x08: case 0x0a: case 0x0c: case 0x0e:
6713 case 0x12: case 0x16:
7ff120b4 6714 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
cca44b1b
JB
6715
6716 case 0x09: case 0x0b: case 0x0d: case 0x0f:
6717 case 0x13: case 0x17:
7ff120b4 6718 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
cca44b1b
JB
6719
6720 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
6721 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
6722 /* Note: no writeback for these instructions. Bit 25 will always be
6723 zero though (via caller), so the following works OK. */
7ff120b4 6724 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
6725 }
6726
6727 /* Should be unreachable. */
6728 return 1;
6729}
6730
34518530
YQ
6731/* Decode shifted register instructions. */
6732
6733static int
6734thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
6735 uint16_t insn2, struct regcache *regs,
cfba9872 6736 arm_displaced_step_closure *dsc)
34518530
YQ
6737{
6738 /* PC is only allowed to be used in instruction MOV. */
6739
6740 unsigned int op = bits (insn1, 5, 8);
6741 unsigned int rn = bits (insn1, 0, 3);
6742
6743 if (op == 0x2 && rn == 0xf) /* MOV */
6744 return thumb2_copy_alu_imm (gdbarch, insn1, insn2, regs, dsc);
6745 else
6746 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6747 "dp (shift reg)", dsc);
6748}
6749
6750
6751/* Decode extension register load/store. Exactly the same as
6752 arm_decode_ext_reg_ld_st. */
6753
6754static int
6755thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
6756 uint16_t insn2, struct regcache *regs,
cfba9872 6757 arm_displaced_step_closure *dsc)
34518530
YQ
6758{
6759 unsigned int opcode = bits (insn1, 4, 8);
6760
6761 switch (opcode)
6762 {
6763 case 0x04: case 0x05:
6764 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6765 "vfp/neon vmov", dsc);
6766
6767 case 0x08: case 0x0c: /* 01x00 */
6768 case 0x0a: case 0x0e: /* 01x10 */
6769 case 0x12: case 0x16: /* 10x10 */
6770 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6771 "vfp/neon vstm/vpush", dsc);
6772
6773 case 0x09: case 0x0d: /* 01x01 */
6774 case 0x0b: case 0x0f: /* 01x11 */
6775 case 0x13: case 0x17: /* 10x11 */
6776 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6777 "vfp/neon vldm/vpop", dsc);
6778
6779 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
6780 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6781 "vstr", dsc);
6782 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
6783 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2, regs, dsc);
6784 }
6785
6786 /* Should be unreachable. */
6787 return 1;
6788}
6789
cca44b1b 6790static int
12545665 6791arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn,
cfba9872 6792 struct regcache *regs, arm_displaced_step_closure *dsc)
cca44b1b
JB
6793{
6794 unsigned int op1 = bits (insn, 20, 25);
6795 int op = bit (insn, 4);
6796 unsigned int coproc = bits (insn, 8, 11);
cca44b1b
JB
6797
6798 if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
7ff120b4 6799 return arm_decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
cca44b1b
JB
6800 else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
6801 && (coproc & 0xe) != 0xa)
6802 /* stc/stc2. */
7ff120b4 6803 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
6804 else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
6805 && (coproc & 0xe) != 0xa)
6806 /* ldc/ldc2 imm/lit. */
7ff120b4 6807 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 6808 else if ((op1 & 0x3e) == 0x00)
7ff120b4 6809 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b 6810 else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
7ff120b4 6811 return arm_copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
cca44b1b 6812 else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
7ff120b4 6813 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b 6814 else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
7ff120b4 6815 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
6816 else if ((op1 & 0x30) == 0x20 && !op)
6817 {
6818 if ((coproc & 0xe) == 0xa)
7ff120b4 6819 return arm_copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
cca44b1b 6820 else
7ff120b4 6821 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
6822 }
6823 else if ((op1 & 0x30) == 0x20 && op)
7ff120b4 6824 return arm_copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
cca44b1b 6825 else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
7ff120b4 6826 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 6827 else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
7ff120b4 6828 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 6829 else if ((op1 & 0x30) == 0x30)
7ff120b4 6830 return arm_copy_svc (gdbarch, insn, regs, dsc);
cca44b1b 6831 else
7ff120b4 6832 return arm_copy_undef (gdbarch, insn, dsc); /* Possibly unreachable. */
cca44b1b
JB
6833}
6834
34518530
YQ
6835static int
6836thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
6837 uint16_t insn2, struct regcache *regs,
cfba9872 6838 arm_displaced_step_closure *dsc)
34518530
YQ
6839{
6840 unsigned int coproc = bits (insn2, 8, 11);
34518530
YQ
6841 unsigned int bit_5_8 = bits (insn1, 5, 8);
6842 unsigned int bit_9 = bit (insn1, 9);
6843 unsigned int bit_4 = bit (insn1, 4);
34518530
YQ
6844
6845 if (bit_9 == 0)
6846 {
6847 if (bit_5_8 == 2)
6848 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6849 "neon 64bit xfer/mrrc/mrrc2/mcrr/mcrr2",
6850 dsc);
6851 else if (bit_5_8 == 0) /* UNDEFINED. */
6852 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
6853 else
6854 {
6855 /*coproc is 101x. SIMD/VFP, ext registers load/store. */
6856 if ((coproc & 0xe) == 0xa)
6857 return thumb2_decode_ext_reg_ld_st (gdbarch, insn1, insn2, regs,
6858 dsc);
6859 else /* coproc is not 101x. */
6860 {
6861 if (bit_4 == 0) /* STC/STC2. */
6862 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6863 "stc/stc2", dsc);
6864 else /* LDC/LDC2 {literal, immeidate}. */
6865 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2,
6866 regs, dsc);
6867 }
6868 }
6869 }
6870 else
6871 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "coproc", dsc);
6872
6873 return 0;
6874}
6875
6876static void
6877install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 6878 arm_displaced_step_closure *dsc, int rd)
34518530
YQ
6879{
6880 /* ADR Rd, #imm
6881
6882 Rewrite as:
6883
6884 Preparation: Rd <- PC
6885 Insn: ADD Rd, #imm
6886 Cleanup: Null.
6887 */
6888
6889 /* Rd <- PC */
6890 int val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6891 displaced_write_reg (regs, dsc, rd, val, CANNOT_WRITE_PC);
6892}
6893
6894static int
6895thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 6896 arm_displaced_step_closure *dsc,
34518530
YQ
6897 int rd, unsigned int imm)
6898{
6899
6900 /* Encoding T2: ADDS Rd, #imm */
6901 dsc->modinsn[0] = (0x3000 | (rd << 8) | imm);
6902
6903 install_pc_relative (gdbarch, regs, dsc, rd);
6904
6905 return 0;
6906}
6907
6908static int
6909thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
6910 struct regcache *regs,
cfba9872 6911 arm_displaced_step_closure *dsc)
34518530
YQ
6912{
6913 unsigned int rd = bits (insn, 8, 10);
6914 unsigned int imm8 = bits (insn, 0, 7);
6915
6916 if (debug_displaced)
6917 fprintf_unfiltered (gdb_stdlog,
6918 "displaced: copying thumb adr r%d, #%d insn %.4x\n",
6919 rd, imm8, insn);
6920
6921 return thumb_copy_pc_relative_16bit (gdbarch, regs, dsc, rd, imm8);
6922}
6923
6924static int
6925thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
6926 uint16_t insn2, struct regcache *regs,
cfba9872 6927 arm_displaced_step_closure *dsc)
34518530
YQ
6928{
6929 unsigned int rd = bits (insn2, 8, 11);
6930 /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
6931 extract raw immediate encoding rather than computing immediate. When
6932 generating ADD or SUB instruction, we can simply perform OR operation to
6933 set immediate into ADD. */
6934 unsigned int imm_3_8 = insn2 & 0x70ff;
6935 unsigned int imm_i = insn1 & 0x0400; /* Clear all bits except bit 10. */
6936
6937 if (debug_displaced)
6938 fprintf_unfiltered (gdb_stdlog,
6939 "displaced: copying thumb adr r%d, #%d:%d insn %.4x%.4x\n",
6940 rd, imm_i, imm_3_8, insn1, insn2);
6941
6942 if (bit (insn1, 7)) /* Encoding T2 */
6943 {
6944 /* Encoding T3: SUB Rd, Rd, #imm */
6945 dsc->modinsn[0] = (0xf1a0 | rd | imm_i);
6946 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
6947 }
6948 else /* Encoding T3 */
6949 {
6950 /* Encoding T3: ADD Rd, Rd, #imm */
6951 dsc->modinsn[0] = (0xf100 | rd | imm_i);
6952 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
6953 }
6954 dsc->numinsns = 2;
6955
6956 install_pc_relative (gdbarch, regs, dsc, rd);
6957
6958 return 0;
6959}
6960
6961static int
615234c1 6962thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, uint16_t insn1,
34518530 6963 struct regcache *regs,
cfba9872 6964 arm_displaced_step_closure *dsc)
34518530
YQ
6965{
6966 unsigned int rt = bits (insn1, 8, 10);
6967 unsigned int pc;
6968 int imm8 = (bits (insn1, 0, 7) << 2);
34518530
YQ
6969
6970 /* LDR Rd, #imm8
6971
6972 Rwrite as:
6973
6974 Preparation: tmp0 <- R0, tmp2 <- R2, tmp3 <- R3, R2 <- PC, R3 <- #imm8;
6975
6976 Insn: LDR R0, [R2, R3];
6977 Cleanup: R2 <- tmp2, R3 <- tmp3, Rd <- R0, R0 <- tmp0 */
6978
6979 if (debug_displaced)
6980 fprintf_unfiltered (gdb_stdlog,
6981 "displaced: copying thumb ldr r%d [pc #%d]\n"
6982 , rt, imm8);
6983
6984 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6985 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6986 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6987 pc = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6988 /* The assembler calculates the required value of the offset from the
6989 Align(PC,4) value of this instruction to the label. */
6990 pc = pc & 0xfffffffc;
6991
6992 displaced_write_reg (regs, dsc, 2, pc, CANNOT_WRITE_PC);
6993 displaced_write_reg (regs, dsc, 3, imm8, CANNOT_WRITE_PC);
6994
6995 dsc->rd = rt;
6996 dsc->u.ldst.xfersize = 4;
6997 dsc->u.ldst.rn = 0;
6998 dsc->u.ldst.immed = 0;
6999 dsc->u.ldst.writeback = 0;
7000 dsc->u.ldst.restore_r4 = 0;
7001
7002 dsc->modinsn[0] = 0x58d0; /* ldr r0, [r2, r3]*/
7003
7004 dsc->cleanup = &cleanup_load;
7005
7006 return 0;
7007}
7008
7009/* Copy Thumb cbnz/cbz insruction. */
7010
7011static int
7012thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
7013 struct regcache *regs,
cfba9872 7014 arm_displaced_step_closure *dsc)
34518530
YQ
7015{
7016 int non_zero = bit (insn1, 11);
7017 unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
7018 CORE_ADDR from = dsc->insn_addr;
7019 int rn = bits (insn1, 0, 2);
7020 int rn_val = displaced_read_reg (regs, dsc, rn);
7021
7022 dsc->u.branch.cond = (rn_val && non_zero) || (!rn_val && !non_zero);
7023 /* CBNZ and CBZ do not affect the condition flags. If condition is true,
7024 set it INST_AL, so cleanup_branch will know branch is taken, otherwise,
7025 condition is false, let it be, cleanup_branch will do nothing. */
7026 if (dsc->u.branch.cond)
7027 {
7028 dsc->u.branch.cond = INST_AL;
7029 dsc->u.branch.dest = from + 4 + imm5;
7030 }
7031 else
7032 dsc->u.branch.dest = from + 2;
7033
7034 dsc->u.branch.link = 0;
7035 dsc->u.branch.exchange = 0;
7036
7037 if (debug_displaced)
7038 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s [r%d = 0x%x]"
7039 " insn %.4x to %.8lx\n", non_zero ? "cbnz" : "cbz",
7040 rn, rn_val, insn1, dsc->u.branch.dest);
7041
7042 dsc->modinsn[0] = THUMB_NOP;
7043
7044 dsc->cleanup = &cleanup_branch;
7045 return 0;
7046}
7047
7048/* Copy Table Branch Byte/Halfword */
7049static int
7050thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
7051 uint16_t insn2, struct regcache *regs,
cfba9872 7052 arm_displaced_step_closure *dsc)
34518530
YQ
7053{
7054 ULONGEST rn_val, rm_val;
7055 int is_tbh = bit (insn2, 4);
7056 CORE_ADDR halfwords = 0;
7057 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7058
7059 rn_val = displaced_read_reg (regs, dsc, bits (insn1, 0, 3));
7060 rm_val = displaced_read_reg (regs, dsc, bits (insn2, 0, 3));
7061
7062 if (is_tbh)
7063 {
7064 gdb_byte buf[2];
7065
7066 target_read_memory (rn_val + 2 * rm_val, buf, 2);
7067 halfwords = extract_unsigned_integer (buf, 2, byte_order);
7068 }
7069 else
7070 {
7071 gdb_byte buf[1];
7072
7073 target_read_memory (rn_val + rm_val, buf, 1);
7074 halfwords = extract_unsigned_integer (buf, 1, byte_order);
7075 }
7076
7077 if (debug_displaced)
7078 fprintf_unfiltered (gdb_stdlog, "displaced: %s base 0x%x offset 0x%x"
7079 " offset 0x%x\n", is_tbh ? "tbh" : "tbb",
7080 (unsigned int) rn_val, (unsigned int) rm_val,
7081 (unsigned int) halfwords);
7082
7083 dsc->u.branch.cond = INST_AL;
7084 dsc->u.branch.link = 0;
7085 dsc->u.branch.exchange = 0;
7086 dsc->u.branch.dest = dsc->insn_addr + 4 + 2 * halfwords;
7087
7088 dsc->cleanup = &cleanup_branch;
7089
7090 return 0;
7091}
7092
7093static void
7094cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
cfba9872 7095 arm_displaced_step_closure *dsc)
34518530
YQ
7096{
7097 /* PC <- r7 */
7098 int val = displaced_read_reg (regs, dsc, 7);
7099 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, val, BX_WRITE_PC);
7100
7101 /* r7 <- r8 */
7102 val = displaced_read_reg (regs, dsc, 8);
7103 displaced_write_reg (regs, dsc, 7, val, CANNOT_WRITE_PC);
7104
7105 /* r8 <- tmp[0] */
7106 displaced_write_reg (regs, dsc, 8, dsc->tmp[0], CANNOT_WRITE_PC);
7107
7108}
7109
7110static int
615234c1 7111thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, uint16_t insn1,
34518530 7112 struct regcache *regs,
cfba9872 7113 arm_displaced_step_closure *dsc)
34518530
YQ
7114{
7115 dsc->u.block.regmask = insn1 & 0x00ff;
7116
7117 /* Rewrite instruction: POP {rX, rY, ...,rZ, PC}
7118 to :
7119
7120 (1) register list is full, that is, r0-r7 are used.
7121 Prepare: tmp[0] <- r8
7122
7123 POP {r0, r1, ...., r6, r7}; remove PC from reglist
7124 MOV r8, r7; Move value of r7 to r8;
7125 POP {r7}; Store PC value into r7.
7126
7127 Cleanup: PC <- r7, r7 <- r8, r8 <-tmp[0]
7128
7129 (2) register list is not full, supposing there are N registers in
7130 register list (except PC, 0 <= N <= 7).
7131 Prepare: for each i, 0 - N, tmp[i] <- ri.
7132
7133 POP {r0, r1, ...., rN};
7134
7135 Cleanup: Set registers in original reglist from r0 - rN. Restore r0 - rN
7136 from tmp[] properly.
7137 */
7138 if (debug_displaced)
7139 fprintf_unfiltered (gdb_stdlog,
7140 "displaced: copying thumb pop {%.8x, pc} insn %.4x\n",
7141 dsc->u.block.regmask, insn1);
7142
7143 if (dsc->u.block.regmask == 0xff)
7144 {
7145 dsc->tmp[0] = displaced_read_reg (regs, dsc, 8);
7146
7147 dsc->modinsn[0] = (insn1 & 0xfeff); /* POP {r0,r1,...,r6, r7} */
7148 dsc->modinsn[1] = 0x46b8; /* MOV r8, r7 */
7149 dsc->modinsn[2] = 0xbc80; /* POP {r7} */
7150
7151 dsc->numinsns = 3;
7152 dsc->cleanup = &cleanup_pop_pc_16bit_all;
7153 }
7154 else
7155 {
7156 unsigned int num_in_list = bitcount (dsc->u.block.regmask);
bec2ab5a
SM
7157 unsigned int i;
7158 unsigned int new_regmask;
34518530
YQ
7159
7160 for (i = 0; i < num_in_list + 1; i++)
7161 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7162
7163 new_regmask = (1 << (num_in_list + 1)) - 1;
7164
7165 if (debug_displaced)
7166 fprintf_unfiltered (gdb_stdlog, _("displaced: POP "
7167 "{..., pc}: original reg list %.4x,"
7168 " modified list %.4x\n"),
7169 (int) dsc->u.block.regmask, new_regmask);
7170
7171 dsc->u.block.regmask |= 0x8000;
7172 dsc->u.block.writeback = 0;
7173 dsc->u.block.cond = INST_AL;
7174
7175 dsc->modinsn[0] = (insn1 & ~0x1ff) | (new_regmask & 0xff);
7176
7177 dsc->cleanup = &cleanup_block_load_pc;
7178 }
7179
7180 return 0;
7181}
7182
7183static void
7184thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
7185 struct regcache *regs,
cfba9872 7186 arm_displaced_step_closure *dsc)
34518530
YQ
7187{
7188 unsigned short op_bit_12_15 = bits (insn1, 12, 15);
7189 unsigned short op_bit_10_11 = bits (insn1, 10, 11);
7190 int err = 0;
7191
7192 /* 16-bit thumb instructions. */
7193 switch (op_bit_12_15)
7194 {
7195 /* Shift (imme), add, subtract, move and compare. */
7196 case 0: case 1: case 2: case 3:
7197 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
7198 "shift/add/sub/mov/cmp",
7199 dsc);
7200 break;
7201 case 4:
7202 switch (op_bit_10_11)
7203 {
7204 case 0: /* Data-processing */
7205 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
7206 "data-processing",
7207 dsc);
7208 break;
7209 case 1: /* Special data instructions and branch and exchange. */
7210 {
7211 unsigned short op = bits (insn1, 7, 9);
7212 if (op == 6 || op == 7) /* BX or BLX */
7213 err = thumb_copy_bx_blx_reg (gdbarch, insn1, regs, dsc);
7214 else if (bits (insn1, 6, 7) != 0) /* ADD/MOV/CMP high registers. */
7215 err = thumb_copy_alu_reg (gdbarch, insn1, regs, dsc);
7216 else
7217 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "special data",
7218 dsc);
7219 }
7220 break;
7221 default: /* LDR (literal) */
7222 err = thumb_copy_16bit_ldr_literal (gdbarch, insn1, regs, dsc);
7223 }
7224 break;
7225 case 5: case 6: case 7: case 8: case 9: /* Load/Store single data item */
7226 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldr/str", dsc);
7227 break;
7228 case 10:
7229 if (op_bit_10_11 < 2) /* Generate PC-relative address */
7230 err = thumb_decode_pc_relative_16bit (gdbarch, insn1, regs, dsc);
7231 else /* Generate SP-relative address */
7232 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "sp-relative", dsc);
7233 break;
7234 case 11: /* Misc 16-bit instructions */
7235 {
7236 switch (bits (insn1, 8, 11))
7237 {
7238 case 1: case 3: case 9: case 11: /* CBNZ, CBZ */
7239 err = thumb_copy_cbnz_cbz (gdbarch, insn1, regs, dsc);
7240 break;
7241 case 12: case 13: /* POP */
7242 if (bit (insn1, 8)) /* PC is in register list. */
7243 err = thumb_copy_pop_pc_16bit (gdbarch, insn1, regs, dsc);
7244 else
7245 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "pop", dsc);
7246 break;
7247 case 15: /* If-Then, and hints */
7248 if (bits (insn1, 0, 3))
7249 /* If-Then makes up to four following instructions conditional.
7250 IT instruction itself is not conditional, so handle it as a
7251 common unmodified instruction. */
7252 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "If-Then",
7253 dsc);
7254 else
7255 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "hints", dsc);
7256 break;
7257 default:
7258 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "misc", dsc);
7259 }
7260 }
7261 break;
7262 case 12:
7263 if (op_bit_10_11 < 2) /* Store multiple registers */
7264 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "stm", dsc);
7265 else /* Load multiple registers */
7266 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldm", dsc);
7267 break;
7268 case 13: /* Conditional branch and supervisor call */
7269 if (bits (insn1, 9, 11) != 7) /* conditional branch */
7270 err = thumb_copy_b (gdbarch, insn1, dsc);
7271 else
7272 err = thumb_copy_svc (gdbarch, insn1, regs, dsc);
7273 break;
7274 case 14: /* Unconditional branch */
7275 err = thumb_copy_b (gdbarch, insn1, dsc);
7276 break;
7277 default:
7278 err = 1;
7279 }
7280
7281 if (err)
7282 internal_error (__FILE__, __LINE__,
7283 _("thumb_process_displaced_16bit_insn: Instruction decode error"));
7284}
7285
7286static int
7287decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
7288 uint16_t insn1, uint16_t insn2,
7289 struct regcache *regs,
cfba9872 7290 arm_displaced_step_closure *dsc)
34518530
YQ
7291{
7292 int rt = bits (insn2, 12, 15);
7293 int rn = bits (insn1, 0, 3);
7294 int op1 = bits (insn1, 7, 8);
34518530
YQ
7295
7296 switch (bits (insn1, 5, 6))
7297 {
7298 case 0: /* Load byte and memory hints */
7299 if (rt == 0xf) /* PLD/PLI */
7300 {
7301 if (rn == 0xf)
7302 /* PLD literal or Encoding T3 of PLI(immediate, literal). */
7303 return thumb2_copy_preload (gdbarch, insn1, insn2, regs, dsc);
7304 else
7305 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7306 "pli/pld", dsc);
7307 }
7308 else
7309 {
7310 if (rn == 0xf) /* LDRB/LDRSB (literal) */
7311 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
7312 1);
7313 else
7314 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7315 "ldrb{reg, immediate}/ldrbt",
7316 dsc);
7317 }
7318
7319 break;
7320 case 1: /* Load halfword and memory hints. */
7321 if (rt == 0xf) /* PLD{W} and Unalloc memory hint. */
7322 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7323 "pld/unalloc memhint", dsc);
7324 else
7325 {
7326 if (rn == 0xf)
7327 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
7328 2);
7329 else
7330 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7331 "ldrh/ldrht", dsc);
7332 }
7333 break;
7334 case 2: /* Load word */
7335 {
7336 int insn2_bit_8_11 = bits (insn2, 8, 11);
7337
7338 if (rn == 0xf)
7339 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc, 4);
7340 else if (op1 == 0x1) /* Encoding T3 */
7341 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs, dsc,
7342 0, 1);
7343 else /* op1 == 0x0 */
7344 {
7345 if (insn2_bit_8_11 == 0xc || (insn2_bit_8_11 & 0x9) == 0x9)
7346 /* LDR (immediate) */
7347 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
7348 dsc, bit (insn2, 8), 1);
7349 else if (insn2_bit_8_11 == 0xe) /* LDRT */
7350 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7351 "ldrt", dsc);
7352 else
7353 /* LDR (register) */
7354 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
7355 dsc, 0, 0);
7356 }
7357 break;
7358 }
7359 default:
7360 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
7361 break;
7362 }
7363 return 0;
7364}
7365
7366static void
7367thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
7368 uint16_t insn2, struct regcache *regs,
cfba9872 7369 arm_displaced_step_closure *dsc)
34518530
YQ
7370{
7371 int err = 0;
7372 unsigned short op = bit (insn2, 15);
7373 unsigned int op1 = bits (insn1, 11, 12);
7374
7375 switch (op1)
7376 {
7377 case 1:
7378 {
7379 switch (bits (insn1, 9, 10))
7380 {
7381 case 0:
7382 if (bit (insn1, 6))
7383 {
7384 /* Load/store {dual, execlusive}, table branch. */
7385 if (bits (insn1, 7, 8) == 1 && bits (insn1, 4, 5) == 1
7386 && bits (insn2, 5, 7) == 0)
7387 err = thumb2_copy_table_branch (gdbarch, insn1, insn2, regs,
7388 dsc);
7389 else
7390 /* PC is not allowed to use in load/store {dual, exclusive}
7391 instructions. */
7392 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7393 "load/store dual/ex", dsc);
7394 }
7395 else /* load/store multiple */
7396 {
7397 switch (bits (insn1, 7, 8))
7398 {
7399 case 0: case 3: /* SRS, RFE */
7400 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7401 "srs/rfe", dsc);
7402 break;
7403 case 1: case 2: /* LDM/STM/PUSH/POP */
7404 err = thumb2_copy_block_xfer (gdbarch, insn1, insn2, regs, dsc);
7405 break;
7406 }
7407 }
7408 break;
7409
7410 case 1:
7411 /* Data-processing (shift register). */
7412 err = thumb2_decode_dp_shift_reg (gdbarch, insn1, insn2, regs,
7413 dsc);
7414 break;
7415 default: /* Coprocessor instructions. */
7416 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
7417 break;
7418 }
7419 break;
7420 }
7421 case 2: /* op1 = 2 */
7422 if (op) /* Branch and misc control. */
7423 {
7424 if (bit (insn2, 14) /* BLX/BL */
7425 || bit (insn2, 12) /* Unconditional branch */
7426 || (bits (insn1, 7, 9) != 0x7)) /* Conditional branch */
7427 err = thumb2_copy_b_bl_blx (gdbarch, insn1, insn2, regs, dsc);
7428 else
7429 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7430 "misc ctrl", dsc);
7431 }
7432 else
7433 {
7434 if (bit (insn1, 9)) /* Data processing (plain binary imm). */
7435 {
b926417a 7436 int dp_op = bits (insn1, 4, 8);
34518530 7437 int rn = bits (insn1, 0, 3);
b926417a 7438 if ((dp_op == 0 || dp_op == 0xa) && rn == 0xf)
34518530
YQ
7439 err = thumb_copy_pc_relative_32bit (gdbarch, insn1, insn2,
7440 regs, dsc);
7441 else
7442 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7443 "dp/pb", dsc);
7444 }
7445 else /* Data processing (modified immeidate) */
7446 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7447 "dp/mi", dsc);
7448 }
7449 break;
7450 case 3: /* op1 = 3 */
7451 switch (bits (insn1, 9, 10))
7452 {
7453 case 0:
7454 if (bit (insn1, 4))
7455 err = decode_thumb_32bit_ld_mem_hints (gdbarch, insn1, insn2,
7456 regs, dsc);
7457 else /* NEON Load/Store and Store single data item */
7458 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7459 "neon elt/struct load/store",
7460 dsc);
7461 break;
7462 case 1: /* op1 = 3, bits (9, 10) == 1 */
7463 switch (bits (insn1, 7, 8))
7464 {
7465 case 0: case 1: /* Data processing (register) */
7466 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7467 "dp(reg)", dsc);
7468 break;
7469 case 2: /* Multiply and absolute difference */
7470 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7471 "mul/mua/diff", dsc);
7472 break;
7473 case 3: /* Long multiply and divide */
7474 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7475 "lmul/lmua", dsc);
7476 break;
7477 }
7478 break;
7479 default: /* Coprocessor instructions */
7480 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
7481 break;
7482 }
7483 break;
7484 default:
7485 err = 1;
7486 }
7487
7488 if (err)
7489 internal_error (__FILE__, __LINE__,
7490 _("thumb_process_displaced_32bit_insn: Instruction decode error"));
7491
7492}
7493
b434a28f
YQ
7494static void
7495thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
12545665 7496 struct regcache *regs,
cfba9872 7497 arm_displaced_step_closure *dsc)
b434a28f 7498{
34518530
YQ
7499 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7500 uint16_t insn1
7501 = read_memory_unsigned_integer (from, 2, byte_order_for_code);
7502
7503 if (debug_displaced)
7504 fprintf_unfiltered (gdb_stdlog, "displaced: process thumb insn %.4x "
7505 "at %.8lx\n", insn1, (unsigned long) from);
7506
7507 dsc->is_thumb = 1;
7508 dsc->insn_size = thumb_insn_size (insn1);
7509 if (thumb_insn_size (insn1) == 4)
7510 {
7511 uint16_t insn2
7512 = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
7513 thumb_process_displaced_32bit_insn (gdbarch, insn1, insn2, regs, dsc);
7514 }
7515 else
7516 thumb_process_displaced_16bit_insn (gdbarch, insn1, regs, dsc);
b434a28f
YQ
7517}
7518
cca44b1b 7519void
b434a28f
YQ
7520arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
7521 CORE_ADDR to, struct regcache *regs,
cfba9872 7522 arm_displaced_step_closure *dsc)
cca44b1b
JB
7523{
7524 int err = 0;
b434a28f
YQ
7525 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7526 uint32_t insn;
cca44b1b
JB
7527
7528 /* Most displaced instructions use a 1-instruction scratch space, so set this
7529 here and override below if/when necessary. */
7530 dsc->numinsns = 1;
7531 dsc->insn_addr = from;
7532 dsc->scratch_base = to;
7533 dsc->cleanup = NULL;
7534 dsc->wrote_to_pc = 0;
7535
b434a28f 7536 if (!displaced_in_arm_mode (regs))
12545665 7537 return thumb_process_displaced_insn (gdbarch, from, regs, dsc);
b434a28f 7538
4db71c0b
YQ
7539 dsc->is_thumb = 0;
7540 dsc->insn_size = 4;
b434a28f
YQ
7541 insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
7542 if (debug_displaced)
7543 fprintf_unfiltered (gdb_stdlog, "displaced: stepping insn %.8lx "
7544 "at %.8lx\n", (unsigned long) insn,
7545 (unsigned long) from);
7546
cca44b1b 7547 if ((insn & 0xf0000000) == 0xf0000000)
7ff120b4 7548 err = arm_decode_unconditional (gdbarch, insn, regs, dsc);
cca44b1b
JB
7549 else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
7550 {
7551 case 0x0: case 0x1: case 0x2: case 0x3:
7ff120b4 7552 err = arm_decode_dp_misc (gdbarch, insn, regs, dsc);
cca44b1b
JB
7553 break;
7554
7555 case 0x4: case 0x5: case 0x6:
7ff120b4 7556 err = arm_decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
cca44b1b
JB
7557 break;
7558
7559 case 0x7:
7ff120b4 7560 err = arm_decode_media (gdbarch, insn, dsc);
cca44b1b
JB
7561 break;
7562
7563 case 0x8: case 0x9: case 0xa: case 0xb:
7ff120b4 7564 err = arm_decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
cca44b1b
JB
7565 break;
7566
7567 case 0xc: case 0xd: case 0xe: case 0xf:
12545665 7568 err = arm_decode_svc_copro (gdbarch, insn, regs, dsc);
cca44b1b
JB
7569 break;
7570 }
7571
7572 if (err)
7573 internal_error (__FILE__, __LINE__,
7574 _("arm_process_displaced_insn: Instruction decode error"));
7575}
7576
7577/* Actually set up the scratch space for a displaced instruction. */
7578
7579void
7580arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
cfba9872 7581 CORE_ADDR to, arm_displaced_step_closure *dsc)
cca44b1b
JB
7582{
7583 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4db71c0b 7584 unsigned int i, len, offset;
cca44b1b 7585 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4db71c0b 7586 int size = dsc->is_thumb? 2 : 4;
948f8e3d 7587 const gdb_byte *bkp_insn;
cca44b1b 7588
4db71c0b 7589 offset = 0;
cca44b1b
JB
7590 /* Poke modified instruction(s). */
7591 for (i = 0; i < dsc->numinsns; i++)
7592 {
7593 if (debug_displaced)
4db71c0b
YQ
7594 {
7595 fprintf_unfiltered (gdb_stdlog, "displaced: writing insn ");
7596 if (size == 4)
7597 fprintf_unfiltered (gdb_stdlog, "%.8lx",
7598 dsc->modinsn[i]);
7599 else if (size == 2)
7600 fprintf_unfiltered (gdb_stdlog, "%.4x",
7601 (unsigned short)dsc->modinsn[i]);
7602
7603 fprintf_unfiltered (gdb_stdlog, " at %.8lx\n",
7604 (unsigned long) to + offset);
7605
7606 }
7607 write_memory_unsigned_integer (to + offset, size,
7608 byte_order_for_code,
cca44b1b 7609 dsc->modinsn[i]);
4db71c0b
YQ
7610 offset += size;
7611 }
7612
7613 /* Choose the correct breakpoint instruction. */
7614 if (dsc->is_thumb)
7615 {
7616 bkp_insn = tdep->thumb_breakpoint;
7617 len = tdep->thumb_breakpoint_size;
7618 }
7619 else
7620 {
7621 bkp_insn = tdep->arm_breakpoint;
7622 len = tdep->arm_breakpoint_size;
cca44b1b
JB
7623 }
7624
7625 /* Put breakpoint afterwards. */
4db71c0b 7626 write_memory (to + offset, bkp_insn, len);
cca44b1b
JB
7627
7628 if (debug_displaced)
7629 fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
7630 paddress (gdbarch, from), paddress (gdbarch, to));
7631}
7632
cca44b1b
JB
7633/* Entry point for cleaning things up after a displaced instruction has been
7634 single-stepped. */
7635
7636void
7637arm_displaced_step_fixup (struct gdbarch *gdbarch,
cfba9872 7638 struct displaced_step_closure *dsc_,
cca44b1b
JB
7639 CORE_ADDR from, CORE_ADDR to,
7640 struct regcache *regs)
7641{
cfba9872
SM
7642 arm_displaced_step_closure *dsc = (arm_displaced_step_closure *) dsc_;
7643
cca44b1b
JB
7644 if (dsc->cleanup)
7645 dsc->cleanup (gdbarch, regs, dsc);
7646
7647 if (!dsc->wrote_to_pc)
4db71c0b
YQ
7648 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
7649 dsc->insn_addr + dsc->insn_size);
7650
cca44b1b
JB
7651}
7652
7653#include "bfd-in2.h"
7654#include "libcoff.h"
7655
7656static int
7657gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
7658{
e47ad6c0
YQ
7659 gdb_disassembler *di
7660 = static_cast<gdb_disassembler *>(info->application_data);
7661 struct gdbarch *gdbarch = di->arch ();
9779414d
DJ
7662
7663 if (arm_pc_is_thumb (gdbarch, memaddr))
cca44b1b
JB
7664 {
7665 static asymbol *asym;
7666 static combined_entry_type ce;
7667 static struct coff_symbol_struct csym;
7668 static struct bfd fake_bfd;
7669 static bfd_target fake_target;
7670
7671 if (csym.native == NULL)
7672 {
7673 /* Create a fake symbol vector containing a Thumb symbol.
7674 This is solely so that the code in print_insn_little_arm()
7675 and print_insn_big_arm() in opcodes/arm-dis.c will detect
7676 the presence of a Thumb symbol and switch to decoding
7677 Thumb instructions. */
7678
7679 fake_target.flavour = bfd_target_coff_flavour;
7680 fake_bfd.xvec = &fake_target;
7681 ce.u.syment.n_sclass = C_THUMBEXTFUNC;
7682 csym.native = &ce;
7683 csym.symbol.the_bfd = &fake_bfd;
7684 csym.symbol.name = "fake";
7685 asym = (asymbol *) & csym;
7686 }
7687
7688 memaddr = UNMAKE_THUMB_ADDR (memaddr);
7689 info->symbols = &asym;
7690 }
7691 else
7692 info->symbols = NULL;
7693
e60eb288
YQ
7694 /* GDB is able to get bfd_mach from the exe_bfd, info->mach is
7695 accurate, so mark USER_SPECIFIED_MACHINE_TYPE bit. Otherwise,
7696 opcodes/arm-dis.c:print_insn reset info->mach, and it will trigger
7697 the assert on the mismatch of info->mach and bfd_get_mach (exec_bfd)
7698 in default_print_insn. */
7699 if (exec_bfd != NULL)
7700 info->flags |= USER_SPECIFIED_MACHINE_TYPE;
7701
6394c606 7702 return default_print_insn (memaddr, info);
cca44b1b
JB
7703}
7704
7705/* The following define instruction sequences that will cause ARM
7706 cpu's to take an undefined instruction trap. These are used to
7707 signal a breakpoint to GDB.
7708
7709 The newer ARMv4T cpu's are capable of operating in ARM or Thumb
7710 modes. A different instruction is required for each mode. The ARM
7711 cpu's can also be big or little endian. Thus four different
7712 instructions are needed to support all cases.
7713
7714 Note: ARMv4 defines several new instructions that will take the
7715 undefined instruction trap. ARM7TDMI is nominally ARMv4T, but does
7716 not in fact add the new instructions. The new undefined
7717 instructions in ARMv4 are all instructions that had no defined
7718 behaviour in earlier chips. There is no guarantee that they will
7719 raise an exception, but may be treated as NOP's. In practice, it
7720 may only safe to rely on instructions matching:
7721
7722 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
7723 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
7724 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
7725
0963b4bd 7726 Even this may only true if the condition predicate is true. The
cca44b1b
JB
7727 following use a condition predicate of ALWAYS so it is always TRUE.
7728
7729 There are other ways of forcing a breakpoint. GNU/Linux, RISC iX,
7730 and NetBSD all use a software interrupt rather than an undefined
7731 instruction to force a trap. This can be handled by by the
7732 abi-specific code during establishment of the gdbarch vector. */
7733
7734#define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
7735#define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
7736#define THUMB_LE_BREAKPOINT {0xbe,0xbe}
7737#define THUMB_BE_BREAKPOINT {0xbe,0xbe}
7738
948f8e3d
PA
7739static const gdb_byte arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
7740static const gdb_byte arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
7741static const gdb_byte arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
7742static const gdb_byte arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
cca44b1b 7743
cd6c3b4f
YQ
7744/* Implement the breakpoint_kind_from_pc gdbarch method. */
7745
d19280ad
YQ
7746static int
7747arm_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
cca44b1b
JB
7748{
7749 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
177321bd 7750 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
cca44b1b 7751
9779414d 7752 if (arm_pc_is_thumb (gdbarch, *pcptr))
cca44b1b
JB
7753 {
7754 *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
177321bd
DJ
7755
7756 /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
7757 check whether we are replacing a 32-bit instruction. */
7758 if (tdep->thumb2_breakpoint != NULL)
7759 {
7760 gdb_byte buf[2];
d19280ad 7761
177321bd
DJ
7762 if (target_read_memory (*pcptr, buf, 2) == 0)
7763 {
7764 unsigned short inst1;
d19280ad 7765
177321bd 7766 inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
db24da6d 7767 if (thumb_insn_size (inst1) == 4)
d19280ad 7768 return ARM_BP_KIND_THUMB2;
177321bd
DJ
7769 }
7770 }
7771
d19280ad 7772 return ARM_BP_KIND_THUMB;
cca44b1b
JB
7773 }
7774 else
d19280ad
YQ
7775 return ARM_BP_KIND_ARM;
7776
7777}
7778
cd6c3b4f
YQ
7779/* Implement the sw_breakpoint_from_kind gdbarch method. */
7780
d19280ad
YQ
7781static const gdb_byte *
7782arm_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
7783{
7784 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
7785
7786 switch (kind)
cca44b1b 7787 {
d19280ad
YQ
7788 case ARM_BP_KIND_ARM:
7789 *size = tdep->arm_breakpoint_size;
cca44b1b 7790 return tdep->arm_breakpoint;
d19280ad
YQ
7791 case ARM_BP_KIND_THUMB:
7792 *size = tdep->thumb_breakpoint_size;
7793 return tdep->thumb_breakpoint;
7794 case ARM_BP_KIND_THUMB2:
7795 *size = tdep->thumb2_breakpoint_size;
7796 return tdep->thumb2_breakpoint;
7797 default:
7798 gdb_assert_not_reached ("unexpected arm breakpoint kind");
cca44b1b
JB
7799 }
7800}
7801
833b7ab5
YQ
7802/* Implement the breakpoint_kind_from_current_state gdbarch method. */
7803
7804static int
7805arm_breakpoint_kind_from_current_state (struct gdbarch *gdbarch,
7806 struct regcache *regcache,
7807 CORE_ADDR *pcptr)
7808{
7809 gdb_byte buf[4];
7810
7811 /* Check the memory pointed by PC is readable. */
7812 if (target_read_memory (regcache_read_pc (regcache), buf, 4) == 0)
7813 {
7814 struct arm_get_next_pcs next_pcs_ctx;
833b7ab5
YQ
7815
7816 arm_get_next_pcs_ctor (&next_pcs_ctx,
7817 &arm_get_next_pcs_ops,
7818 gdbarch_byte_order (gdbarch),
7819 gdbarch_byte_order_for_code (gdbarch),
7820 0,
7821 regcache);
7822
a0ff9e1a 7823 std::vector<CORE_ADDR> next_pcs = arm_get_next_pcs (&next_pcs_ctx);
833b7ab5
YQ
7824
7825 /* If MEMADDR is the next instruction of current pc, do the
7826 software single step computation, and get the thumb mode by
7827 the destination address. */
a0ff9e1a 7828 for (CORE_ADDR pc : next_pcs)
833b7ab5
YQ
7829 {
7830 if (UNMAKE_THUMB_ADDR (pc) == *pcptr)
7831 {
833b7ab5
YQ
7832 if (IS_THUMB_ADDR (pc))
7833 {
7834 *pcptr = MAKE_THUMB_ADDR (*pcptr);
7835 return arm_breakpoint_kind_from_pc (gdbarch, pcptr);
7836 }
7837 else
7838 return ARM_BP_KIND_ARM;
7839 }
7840 }
833b7ab5
YQ
7841 }
7842
7843 return arm_breakpoint_kind_from_pc (gdbarch, pcptr);
7844}
7845
cca44b1b
JB
7846/* Extract from an array REGBUF containing the (raw) register state a
7847 function return value of type TYPE, and copy that, in virtual
7848 format, into VALBUF. */
7849
7850static void
7851arm_extract_return_value (struct type *type, struct regcache *regs,
7852 gdb_byte *valbuf)
7853{
ac7936df 7854 struct gdbarch *gdbarch = regs->arch ();
cca44b1b
JB
7855 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7856
7857 if (TYPE_CODE_FLT == TYPE_CODE (type))
7858 {
7859 switch (gdbarch_tdep (gdbarch)->fp_model)
7860 {
7861 case ARM_FLOAT_FPA:
7862 {
7863 /* The value is in register F0 in internal format. We need to
7864 extract the raw value and then convert it to the desired
7865 internal type. */
7866 bfd_byte tmpbuf[FP_REGISTER_SIZE];
7867
dca08e1f 7868 regs->cooked_read (ARM_F0_REGNUM, tmpbuf);
3b2ca824
UW
7869 target_float_convert (tmpbuf, arm_ext_type (gdbarch),
7870 valbuf, type);
cca44b1b
JB
7871 }
7872 break;
7873
7874 case ARM_FLOAT_SOFT_FPA:
7875 case ARM_FLOAT_SOFT_VFP:
7876 /* ARM_FLOAT_VFP can arise if this is a variadic function so
7877 not using the VFP ABI code. */
7878 case ARM_FLOAT_VFP:
dca08e1f 7879 regs->cooked_read (ARM_A1_REGNUM, valbuf);
cca44b1b 7880 if (TYPE_LENGTH (type) > 4)
dca08e1f 7881 regs->cooked_read (ARM_A1_REGNUM + 1, valbuf + INT_REGISTER_SIZE);
cca44b1b
JB
7882 break;
7883
7884 default:
0963b4bd
MS
7885 internal_error (__FILE__, __LINE__,
7886 _("arm_extract_return_value: "
7887 "Floating point model not supported"));
cca44b1b
JB
7888 break;
7889 }
7890 }
7891 else if (TYPE_CODE (type) == TYPE_CODE_INT
7892 || TYPE_CODE (type) == TYPE_CODE_CHAR
7893 || TYPE_CODE (type) == TYPE_CODE_BOOL
7894 || TYPE_CODE (type) == TYPE_CODE_PTR
aa006118 7895 || TYPE_IS_REFERENCE (type)
cca44b1b
JB
7896 || TYPE_CODE (type) == TYPE_CODE_ENUM)
7897 {
b021a221
MS
7898 /* If the type is a plain integer, then the access is
7899 straight-forward. Otherwise we have to play around a bit
7900 more. */
cca44b1b
JB
7901 int len = TYPE_LENGTH (type);
7902 int regno = ARM_A1_REGNUM;
7903 ULONGEST tmp;
7904
7905 while (len > 0)
7906 {
7907 /* By using store_unsigned_integer we avoid having to do
7908 anything special for small big-endian values. */
7909 regcache_cooked_read_unsigned (regs, regno++, &tmp);
7910 store_unsigned_integer (valbuf,
7911 (len > INT_REGISTER_SIZE
7912 ? INT_REGISTER_SIZE : len),
7913 byte_order, tmp);
7914 len -= INT_REGISTER_SIZE;
7915 valbuf += INT_REGISTER_SIZE;
7916 }
7917 }
7918 else
7919 {
7920 /* For a structure or union the behaviour is as if the value had
7921 been stored to word-aligned memory and then loaded into
7922 registers with 32-bit load instruction(s). */
7923 int len = TYPE_LENGTH (type);
7924 int regno = ARM_A1_REGNUM;
7925 bfd_byte tmpbuf[INT_REGISTER_SIZE];
7926
7927 while (len > 0)
7928 {
dca08e1f 7929 regs->cooked_read (regno++, tmpbuf);
cca44b1b
JB
7930 memcpy (valbuf, tmpbuf,
7931 len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
7932 len -= INT_REGISTER_SIZE;
7933 valbuf += INT_REGISTER_SIZE;
7934 }
7935 }
7936}
7937
7938
7939/* Will a function return an aggregate type in memory or in a
7940 register? Return 0 if an aggregate type can be returned in a
7941 register, 1 if it must be returned in memory. */
7942
7943static int
7944arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
7945{
cca44b1b
JB
7946 enum type_code code;
7947
f168693b 7948 type = check_typedef (type);
cca44b1b 7949
b13c8ab2
YQ
7950 /* Simple, non-aggregate types (ie not including vectors and
7951 complex) are always returned in a register (or registers). */
7952 code = TYPE_CODE (type);
7953 if (TYPE_CODE_STRUCT != code && TYPE_CODE_UNION != code
7954 && TYPE_CODE_ARRAY != code && TYPE_CODE_COMPLEX != code)
7955 return 0;
cca44b1b 7956
c4312b19
YQ
7957 if (TYPE_CODE_ARRAY == code && TYPE_VECTOR (type))
7958 {
7959 /* Vector values should be returned using ARM registers if they
7960 are not over 16 bytes. */
7961 return (TYPE_LENGTH (type) > 16);
7962 }
7963
b13c8ab2 7964 if (gdbarch_tdep (gdbarch)->arm_abi != ARM_ABI_APCS)
cca44b1b 7965 {
b13c8ab2
YQ
7966 /* The AAPCS says all aggregates not larger than a word are returned
7967 in a register. */
7968 if (TYPE_LENGTH (type) <= INT_REGISTER_SIZE)
7969 return 0;
7970
cca44b1b
JB
7971 return 1;
7972 }
b13c8ab2
YQ
7973 else
7974 {
7975 int nRc;
cca44b1b 7976
b13c8ab2
YQ
7977 /* All aggregate types that won't fit in a register must be returned
7978 in memory. */
7979 if (TYPE_LENGTH (type) > INT_REGISTER_SIZE)
7980 return 1;
cca44b1b 7981
b13c8ab2
YQ
7982 /* In the ARM ABI, "integer" like aggregate types are returned in
7983 registers. For an aggregate type to be integer like, its size
7984 must be less than or equal to INT_REGISTER_SIZE and the
7985 offset of each addressable subfield must be zero. Note that bit
7986 fields are not addressable, and all addressable subfields of
7987 unions always start at offset zero.
cca44b1b 7988
b13c8ab2
YQ
7989 This function is based on the behaviour of GCC 2.95.1.
7990 See: gcc/arm.c: arm_return_in_memory() for details.
cca44b1b 7991
b13c8ab2
YQ
7992 Note: All versions of GCC before GCC 2.95.2 do not set up the
7993 parameters correctly for a function returning the following
7994 structure: struct { float f;}; This should be returned in memory,
7995 not a register. Richard Earnshaw sent me a patch, but I do not
7996 know of any way to detect if a function like the above has been
7997 compiled with the correct calling convention. */
7998
7999 /* Assume all other aggregate types can be returned in a register.
8000 Run a check for structures, unions and arrays. */
8001 nRc = 0;
67255d04 8002
b13c8ab2
YQ
8003 if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
8004 {
8005 int i;
8006 /* Need to check if this struct/union is "integer" like. For
8007 this to be true, its size must be less than or equal to
8008 INT_REGISTER_SIZE and the offset of each addressable
8009 subfield must be zero. Note that bit fields are not
8010 addressable, and unions always start at offset zero. If any
8011 of the subfields is a floating point type, the struct/union
8012 cannot be an integer type. */
8013
8014 /* For each field in the object, check:
8015 1) Is it FP? --> yes, nRc = 1;
8016 2) Is it addressable (bitpos != 0) and
8017 not packed (bitsize == 0)?
8018 --> yes, nRc = 1
8019 */
8020
8021 for (i = 0; i < TYPE_NFIELDS (type); i++)
67255d04 8022 {
b13c8ab2
YQ
8023 enum type_code field_type_code;
8024
8025 field_type_code
8026 = TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type,
8027 i)));
8028
8029 /* Is it a floating point type field? */
8030 if (field_type_code == TYPE_CODE_FLT)
67255d04
RE
8031 {
8032 nRc = 1;
8033 break;
8034 }
b13c8ab2
YQ
8035
8036 /* If bitpos != 0, then we have to care about it. */
8037 if (TYPE_FIELD_BITPOS (type, i) != 0)
8038 {
8039 /* Bitfields are not addressable. If the field bitsize is
8040 zero, then the field is not packed. Hence it cannot be
8041 a bitfield or any other packed type. */
8042 if (TYPE_FIELD_BITSIZE (type, i) == 0)
8043 {
8044 nRc = 1;
8045 break;
8046 }
8047 }
67255d04
RE
8048 }
8049 }
67255d04 8050
b13c8ab2
YQ
8051 return nRc;
8052 }
67255d04
RE
8053}
8054
34e8f22d
RE
8055/* Write into appropriate registers a function return value of type
8056 TYPE, given in virtual format. */
8057
8058static void
b508a996 8059arm_store_return_value (struct type *type, struct regcache *regs,
5238cf52 8060 const gdb_byte *valbuf)
34e8f22d 8061{
ac7936df 8062 struct gdbarch *gdbarch = regs->arch ();
e17a4113 8063 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
be8626e0 8064
34e8f22d
RE
8065 if (TYPE_CODE (type) == TYPE_CODE_FLT)
8066 {
64403bd1 8067 gdb_byte buf[FP_REGISTER_SIZE];
34e8f22d 8068
be8626e0 8069 switch (gdbarch_tdep (gdbarch)->fp_model)
08216dd7
RE
8070 {
8071 case ARM_FLOAT_FPA:
8072
3b2ca824 8073 target_float_convert (valbuf, type, buf, arm_ext_type (gdbarch));
b66f5587 8074 regs->cooked_write (ARM_F0_REGNUM, buf);
08216dd7
RE
8075 break;
8076
fd50bc42 8077 case ARM_FLOAT_SOFT_FPA:
08216dd7 8078 case ARM_FLOAT_SOFT_VFP:
90445bd3
DJ
8079 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8080 not using the VFP ABI code. */
8081 case ARM_FLOAT_VFP:
b66f5587 8082 regs->cooked_write (ARM_A1_REGNUM, valbuf);
b508a996 8083 if (TYPE_LENGTH (type) > 4)
b66f5587 8084 regs->cooked_write (ARM_A1_REGNUM + 1, valbuf + INT_REGISTER_SIZE);
08216dd7
RE
8085 break;
8086
8087 default:
9b20d036
MS
8088 internal_error (__FILE__, __LINE__,
8089 _("arm_store_return_value: Floating "
8090 "point model not supported"));
08216dd7
RE
8091 break;
8092 }
34e8f22d 8093 }
b508a996
RE
8094 else if (TYPE_CODE (type) == TYPE_CODE_INT
8095 || TYPE_CODE (type) == TYPE_CODE_CHAR
8096 || TYPE_CODE (type) == TYPE_CODE_BOOL
8097 || TYPE_CODE (type) == TYPE_CODE_PTR
aa006118 8098 || TYPE_IS_REFERENCE (type)
b508a996
RE
8099 || TYPE_CODE (type) == TYPE_CODE_ENUM)
8100 {
8101 if (TYPE_LENGTH (type) <= 4)
8102 {
8103 /* Values of one word or less are zero/sign-extended and
8104 returned in r0. */
7a5ea0d4 8105 bfd_byte tmpbuf[INT_REGISTER_SIZE];
b508a996
RE
8106 LONGEST val = unpack_long (type, valbuf);
8107
e17a4113 8108 store_signed_integer (tmpbuf, INT_REGISTER_SIZE, byte_order, val);
b66f5587 8109 regs->cooked_write (ARM_A1_REGNUM, tmpbuf);
b508a996
RE
8110 }
8111 else
8112 {
8113 /* Integral values greater than one word are stored in consecutive
8114 registers starting with r0. This will always be a multiple of
8115 the regiser size. */
8116 int len = TYPE_LENGTH (type);
8117 int regno = ARM_A1_REGNUM;
8118
8119 while (len > 0)
8120 {
b66f5587 8121 regs->cooked_write (regno++, valbuf);
7a5ea0d4
DJ
8122 len -= INT_REGISTER_SIZE;
8123 valbuf += INT_REGISTER_SIZE;
b508a996
RE
8124 }
8125 }
8126 }
34e8f22d 8127 else
b508a996
RE
8128 {
8129 /* For a structure or union the behaviour is as if the value had
8130 been stored to word-aligned memory and then loaded into
8131 registers with 32-bit load instruction(s). */
8132 int len = TYPE_LENGTH (type);
8133 int regno = ARM_A1_REGNUM;
7a5ea0d4 8134 bfd_byte tmpbuf[INT_REGISTER_SIZE];
b508a996
RE
8135
8136 while (len > 0)
8137 {
8138 memcpy (tmpbuf, valbuf,
7a5ea0d4 8139 len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
b66f5587 8140 regs->cooked_write (regno++, tmpbuf);
7a5ea0d4
DJ
8141 len -= INT_REGISTER_SIZE;
8142 valbuf += INT_REGISTER_SIZE;
b508a996
RE
8143 }
8144 }
34e8f22d
RE
8145}
8146
2af48f68
PB
8147
8148/* Handle function return values. */
8149
8150static enum return_value_convention
6a3a010b 8151arm_return_value (struct gdbarch *gdbarch, struct value *function,
c055b101
CV
8152 struct type *valtype, struct regcache *regcache,
8153 gdb_byte *readbuf, const gdb_byte *writebuf)
2af48f68 8154{
7c00367c 8155 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
6a3a010b 8156 struct type *func_type = function ? value_type (function) : NULL;
90445bd3
DJ
8157 enum arm_vfp_cprc_base_type vfp_base_type;
8158 int vfp_base_count;
8159
8160 if (arm_vfp_abi_for_function (gdbarch, func_type)
8161 && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
8162 {
8163 int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
8164 int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
8165 int i;
8166 for (i = 0; i < vfp_base_count; i++)
8167 {
58d6951d
DJ
8168 if (reg_char == 'q')
8169 {
8170 if (writebuf)
8171 arm_neon_quad_write (gdbarch, regcache, i,
8172 writebuf + i * unit_length);
8173
8174 if (readbuf)
8175 arm_neon_quad_read (gdbarch, regcache, i,
8176 readbuf + i * unit_length);
8177 }
8178 else
8179 {
8180 char name_buf[4];
8181 int regnum;
8182
8c042590 8183 xsnprintf (name_buf, sizeof (name_buf), "%c%d", reg_char, i);
58d6951d
DJ
8184 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8185 strlen (name_buf));
8186 if (writebuf)
b66f5587 8187 regcache->cooked_write (regnum, writebuf + i * unit_length);
58d6951d 8188 if (readbuf)
dca08e1f 8189 regcache->cooked_read (regnum, readbuf + i * unit_length);
58d6951d 8190 }
90445bd3
DJ
8191 }
8192 return RETURN_VALUE_REGISTER_CONVENTION;
8193 }
7c00367c 8194
2af48f68
PB
8195 if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
8196 || TYPE_CODE (valtype) == TYPE_CODE_UNION
8197 || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
8198 {
7c00367c
MK
8199 if (tdep->struct_return == pcc_struct_return
8200 || arm_return_in_memory (gdbarch, valtype))
2af48f68
PB
8201 return RETURN_VALUE_STRUCT_CONVENTION;
8202 }
b13c8ab2
YQ
8203 else if (TYPE_CODE (valtype) == TYPE_CODE_COMPLEX)
8204 {
8205 if (arm_return_in_memory (gdbarch, valtype))
8206 return RETURN_VALUE_STRUCT_CONVENTION;
8207 }
7052e42c 8208
2af48f68
PB
8209 if (writebuf)
8210 arm_store_return_value (valtype, regcache, writebuf);
8211
8212 if (readbuf)
8213 arm_extract_return_value (valtype, regcache, readbuf);
8214
8215 return RETURN_VALUE_REGISTER_CONVENTION;
8216}
8217
8218
9df628e0 8219static int
60ade65d 8220arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
9df628e0 8221{
e17a4113
UW
8222 struct gdbarch *gdbarch = get_frame_arch (frame);
8223 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
8224 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
9df628e0 8225 CORE_ADDR jb_addr;
e362b510 8226 gdb_byte buf[INT_REGISTER_SIZE];
9df628e0 8227
60ade65d 8228 jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
9df628e0
RE
8229
8230 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
7a5ea0d4 8231 INT_REGISTER_SIZE))
9df628e0
RE
8232 return 0;
8233
e17a4113 8234 *pc = extract_unsigned_integer (buf, INT_REGISTER_SIZE, byte_order);
9df628e0
RE
8235 return 1;
8236}
8237
faa95490
DJ
8238/* Recognize GCC and GNU ld's trampolines. If we are in a trampoline,
8239 return the target PC. Otherwise return 0. */
c906108c
SS
8240
8241CORE_ADDR
52f729a7 8242arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
c906108c 8243{
2c02bd72 8244 const char *name;
faa95490 8245 int namelen;
c906108c
SS
8246 CORE_ADDR start_addr;
8247
8248 /* Find the starting address and name of the function containing the PC. */
8249 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
80d8d390
YQ
8250 {
8251 /* Trampoline 'bx reg' doesn't belong to any functions. Do the
8252 check here. */
8253 start_addr = arm_skip_bx_reg (frame, pc);
8254 if (start_addr != 0)
8255 return start_addr;
8256
8257 return 0;
8258 }
c906108c 8259
faa95490
DJ
8260 /* If PC is in a Thumb call or return stub, return the address of the
8261 target PC, which is in a register. The thunk functions are called
8262 _call_via_xx, where x is the register name. The possible names
3d8d5e79
DJ
8263 are r0-r9, sl, fp, ip, sp, and lr. ARM RealView has similar
8264 functions, named __ARM_call_via_r[0-7]. */
61012eef
GB
8265 if (startswith (name, "_call_via_")
8266 || startswith (name, "__ARM_call_via_"))
c906108c 8267 {
ed9a39eb
JM
8268 /* Use the name suffix to determine which register contains the
8269 target PC. */
a121b7c1 8270 static const char *table[15] =
c5aa993b
JM
8271 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
8272 "r8", "r9", "sl", "fp", "ip", "sp", "lr"
8273 };
c906108c 8274 int regno;
faa95490 8275 int offset = strlen (name) - 2;
c906108c
SS
8276
8277 for (regno = 0; regno <= 14; regno++)
faa95490 8278 if (strcmp (&name[offset], table[regno]) == 0)
52f729a7 8279 return get_frame_register_unsigned (frame, regno);
c906108c 8280 }
ed9a39eb 8281
faa95490
DJ
8282 /* GNU ld generates __foo_from_arm or __foo_from_thumb for
8283 non-interworking calls to foo. We could decode the stubs
8284 to find the target but it's easier to use the symbol table. */
8285 namelen = strlen (name);
8286 if (name[0] == '_' && name[1] == '_'
8287 && ((namelen > 2 + strlen ("_from_thumb")
61012eef 8288 && startswith (name + namelen - strlen ("_from_thumb"), "_from_thumb"))
faa95490 8289 || (namelen > 2 + strlen ("_from_arm")
61012eef 8290 && startswith (name + namelen - strlen ("_from_arm"), "_from_arm"))))
faa95490
DJ
8291 {
8292 char *target_name;
8293 int target_len = namelen - 2;
3b7344d5 8294 struct bound_minimal_symbol minsym;
faa95490
DJ
8295 struct objfile *objfile;
8296 struct obj_section *sec;
8297
8298 if (name[namelen - 1] == 'b')
8299 target_len -= strlen ("_from_thumb");
8300 else
8301 target_len -= strlen ("_from_arm");
8302
224c3ddb 8303 target_name = (char *) alloca (target_len + 1);
faa95490
DJ
8304 memcpy (target_name, name + 2, target_len);
8305 target_name[target_len] = '\0';
8306
8307 sec = find_pc_section (pc);
8308 objfile = (sec == NULL) ? NULL : sec->objfile;
8309 minsym = lookup_minimal_symbol (target_name, NULL, objfile);
3b7344d5 8310 if (minsym.minsym != NULL)
77e371c0 8311 return BMSYMBOL_VALUE_ADDRESS (minsym);
faa95490
DJ
8312 else
8313 return 0;
8314 }
8315
c5aa993b 8316 return 0; /* not a stub */
c906108c
SS
8317}
8318
afd7eef0 8319static void
981a3fb3 8320set_arm_command (const char *args, int from_tty)
afd7eef0 8321{
edefbb7c
AC
8322 printf_unfiltered (_("\
8323\"set arm\" must be followed by an apporpriate subcommand.\n"));
afd7eef0
RE
8324 help_list (setarmcmdlist, "set arm ", all_commands, gdb_stdout);
8325}
8326
8327static void
981a3fb3 8328show_arm_command (const char *args, int from_tty)
afd7eef0 8329{
26304000 8330 cmd_show_list (showarmcmdlist, from_tty, "");
afd7eef0
RE
8331}
8332
28e97307
DJ
8333static void
8334arm_update_current_architecture (void)
fd50bc42 8335{
28e97307 8336 struct gdbarch_info info;
fd50bc42 8337
28e97307 8338 /* If the current architecture is not ARM, we have nothing to do. */
f5656ead 8339 if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_arm)
28e97307 8340 return;
fd50bc42 8341
28e97307
DJ
8342 /* Update the architecture. */
8343 gdbarch_info_init (&info);
fd50bc42 8344
28e97307 8345 if (!gdbarch_update_p (info))
9b20d036 8346 internal_error (__FILE__, __LINE__, _("could not update architecture"));
fd50bc42
RE
8347}
8348
8349static void
eb4c3f4a 8350set_fp_model_sfunc (const char *args, int from_tty,
fd50bc42
RE
8351 struct cmd_list_element *c)
8352{
570dc176 8353 int fp_model;
fd50bc42
RE
8354
8355 for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
8356 if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
8357 {
aead7601 8358 arm_fp_model = (enum arm_float_model) fp_model;
fd50bc42
RE
8359 break;
8360 }
8361
8362 if (fp_model == ARM_FLOAT_LAST)
edefbb7c 8363 internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
fd50bc42
RE
8364 current_fp_model);
8365
28e97307 8366 arm_update_current_architecture ();
fd50bc42
RE
8367}
8368
8369static void
08546159
AC
8370show_fp_model (struct ui_file *file, int from_tty,
8371 struct cmd_list_element *c, const char *value)
fd50bc42 8372{
f5656ead 8373 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
fd50bc42 8374
28e97307 8375 if (arm_fp_model == ARM_FLOAT_AUTO
f5656ead 8376 && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
28e97307
DJ
8377 fprintf_filtered (file, _("\
8378The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
8379 fp_model_strings[tdep->fp_model]);
8380 else
8381 fprintf_filtered (file, _("\
8382The current ARM floating point model is \"%s\".\n"),
8383 fp_model_strings[arm_fp_model]);
8384}
8385
8386static void
eb4c3f4a 8387arm_set_abi (const char *args, int from_tty,
28e97307
DJ
8388 struct cmd_list_element *c)
8389{
570dc176 8390 int arm_abi;
28e97307
DJ
8391
8392 for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
8393 if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
8394 {
aead7601 8395 arm_abi_global = (enum arm_abi_kind) arm_abi;
28e97307
DJ
8396 break;
8397 }
8398
8399 if (arm_abi == ARM_ABI_LAST)
8400 internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
8401 arm_abi_string);
8402
8403 arm_update_current_architecture ();
8404}
8405
8406static void
8407arm_show_abi (struct ui_file *file, int from_tty,
8408 struct cmd_list_element *c, const char *value)
8409{
f5656ead 8410 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
28e97307
DJ
8411
8412 if (arm_abi_global == ARM_ABI_AUTO
f5656ead 8413 && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
28e97307
DJ
8414 fprintf_filtered (file, _("\
8415The current ARM ABI is \"auto\" (currently \"%s\").\n"),
8416 arm_abi_strings[tdep->arm_abi]);
8417 else
8418 fprintf_filtered (file, _("The current ARM ABI is \"%s\".\n"),
8419 arm_abi_string);
fd50bc42
RE
8420}
8421
0428b8f5
DJ
8422static void
8423arm_show_fallback_mode (struct ui_file *file, int from_tty,
8424 struct cmd_list_element *c, const char *value)
8425{
0963b4bd
MS
8426 fprintf_filtered (file,
8427 _("The current execution mode assumed "
8428 "(when symbols are unavailable) is \"%s\".\n"),
0428b8f5
DJ
8429 arm_fallback_mode_string);
8430}
8431
8432static void
8433arm_show_force_mode (struct ui_file *file, int from_tty,
8434 struct cmd_list_element *c, const char *value)
8435{
0963b4bd
MS
8436 fprintf_filtered (file,
8437 _("The current execution mode assumed "
8438 "(even when symbols are available) is \"%s\".\n"),
0428b8f5
DJ
8439 arm_force_mode_string);
8440}
8441
afd7eef0
RE
8442/* If the user changes the register disassembly style used for info
8443 register and other commands, we have to also switch the style used
8444 in opcodes for disassembly output. This function is run in the "set
8445 arm disassembly" command, and does that. */
bc90b915
FN
8446
8447static void
eb4c3f4a 8448set_disassembly_style_sfunc (const char *args, int from_tty,
65b48a81 8449 struct cmd_list_element *c)
bc90b915 8450{
65b48a81
PB
8451 /* Convert the short style name into the long style name (eg, reg-names-*)
8452 before calling the generic set_disassembler_options() function. */
8453 std::string long_name = std::string ("reg-names-") + disassembly_style;
8454 set_disassembler_options (&long_name[0]);
8455}
8456
8457static void
8458show_disassembly_style_sfunc (struct ui_file *file, int from_tty,
8459 struct cmd_list_element *c, const char *value)
8460{
8461 struct gdbarch *gdbarch = get_current_arch ();
8462 char *options = get_disassembler_options (gdbarch);
8463 const char *style = "";
8464 int len = 0;
f995bbe8 8465 const char *opt;
65b48a81
PB
8466
8467 FOR_EACH_DISASSEMBLER_OPTION (opt, options)
8468 if (CONST_STRNEQ (opt, "reg-names-"))
8469 {
8470 style = &opt[strlen ("reg-names-")];
8471 len = strcspn (style, ",");
8472 }
8473
8474 fprintf_unfiltered (file, "The disassembly style is \"%.*s\".\n", len, style);
bc90b915
FN
8475}
8476\f
966fbf70 8477/* Return the ARM register name corresponding to register I. */
a208b0cb 8478static const char *
d93859e2 8479arm_register_name (struct gdbarch *gdbarch, int i)
966fbf70 8480{
58d6951d
DJ
8481 const int num_regs = gdbarch_num_regs (gdbarch);
8482
8483 if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
8484 && i >= num_regs && i < num_regs + 32)
8485 {
8486 static const char *const vfp_pseudo_names[] = {
8487 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
8488 "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
8489 "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
8490 "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
8491 };
8492
8493 return vfp_pseudo_names[i - num_regs];
8494 }
8495
8496 if (gdbarch_tdep (gdbarch)->have_neon_pseudos
8497 && i >= num_regs + 32 && i < num_regs + 32 + 16)
8498 {
8499 static const char *const neon_pseudo_names[] = {
8500 "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
8501 "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
8502 };
8503
8504 return neon_pseudo_names[i - num_regs - 32];
8505 }
8506
ff6f572f
DJ
8507 if (i >= ARRAY_SIZE (arm_register_names))
8508 /* These registers are only supported on targets which supply
8509 an XML description. */
8510 return "";
8511
966fbf70
RE
8512 return arm_register_names[i];
8513}
8514
082fc60d
RE
8515/* Test whether the coff symbol specific value corresponds to a Thumb
8516 function. */
8517
8518static int
8519coff_sym_is_thumb (int val)
8520{
f8bf5763
PM
8521 return (val == C_THUMBEXT
8522 || val == C_THUMBSTAT
8523 || val == C_THUMBEXTFUNC
8524 || val == C_THUMBSTATFUNC
8525 || val == C_THUMBLABEL);
082fc60d
RE
8526}
8527
8528/* arm_coff_make_msymbol_special()
8529 arm_elf_make_msymbol_special()
8530
8531 These functions test whether the COFF or ELF symbol corresponds to
8532 an address in thumb code, and set a "special" bit in a minimal
8533 symbol to indicate that it does. */
8534
34e8f22d 8535static void
082fc60d
RE
8536arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
8537{
39d911fc
TP
8538 elf_symbol_type *elfsym = (elf_symbol_type *) sym;
8539
8540 if (ARM_GET_SYM_BRANCH_TYPE (elfsym->internal_elf_sym.st_target_internal)
467d42c4 8541 == ST_BRANCH_TO_THUMB)
082fc60d
RE
8542 MSYMBOL_SET_SPECIAL (msym);
8543}
8544
34e8f22d 8545static void
082fc60d
RE
8546arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
8547{
8548 if (coff_sym_is_thumb (val))
8549 MSYMBOL_SET_SPECIAL (msym);
8550}
8551
60c5725c 8552static void
c1bd65d0 8553arm_objfile_data_free (struct objfile *objfile, void *arg)
60c5725c 8554{
9a3c8263 8555 struct arm_per_objfile *data = (struct arm_per_objfile *) arg;
60c5725c
DJ
8556 unsigned int i;
8557
8558 for (i = 0; i < objfile->obfd->section_count; i++)
8559 VEC_free (arm_mapping_symbol_s, data->section_maps[i]);
8560}
8561
8562static void
8563arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
8564 asymbol *sym)
8565{
8566 const char *name = bfd_asymbol_name (sym);
8567 struct arm_per_objfile *data;
8568 VEC(arm_mapping_symbol_s) **map_p;
8569 struct arm_mapping_symbol new_map_sym;
8570
8571 gdb_assert (name[0] == '$');
8572 if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
8573 return;
8574
9a3c8263
SM
8575 data = (struct arm_per_objfile *) objfile_data (objfile,
8576 arm_objfile_data_key);
60c5725c
DJ
8577 if (data == NULL)
8578 {
8579 data = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8580 struct arm_per_objfile);
8581 set_objfile_data (objfile, arm_objfile_data_key, data);
8582 data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
8583 objfile->obfd->section_count,
8584 VEC(arm_mapping_symbol_s) *);
8585 }
8586 map_p = &data->section_maps[bfd_get_section (sym)->index];
8587
8588 new_map_sym.value = sym->value;
8589 new_map_sym.type = name[1];
8590
8591 /* Assume that most mapping symbols appear in order of increasing
8592 value. If they were randomly distributed, it would be faster to
8593 always push here and then sort at first use. */
8594 if (!VEC_empty (arm_mapping_symbol_s, *map_p))
8595 {
8596 struct arm_mapping_symbol *prev_map_sym;
8597
8598 prev_map_sym = VEC_last (arm_mapping_symbol_s, *map_p);
8599 if (prev_map_sym->value >= sym->value)
8600 {
8601 unsigned int idx;
8602 idx = VEC_lower_bound (arm_mapping_symbol_s, *map_p, &new_map_sym,
8603 arm_compare_mapping_symbols);
8604 VEC_safe_insert (arm_mapping_symbol_s, *map_p, idx, &new_map_sym);
8605 return;
8606 }
8607 }
8608
8609 VEC_safe_push (arm_mapping_symbol_s, *map_p, &new_map_sym);
8610}
8611
756fe439 8612static void
61a1198a 8613arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
756fe439 8614{
ac7936df 8615 struct gdbarch *gdbarch = regcache->arch ();
61a1198a 8616 regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
756fe439
DJ
8617
8618 /* If necessary, set the T bit. */
8619 if (arm_apcs_32)
8620 {
9779414d 8621 ULONGEST val, t_bit;
61a1198a 8622 regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
9779414d
DJ
8623 t_bit = arm_psr_thumb_bit (gdbarch);
8624 if (arm_pc_is_thumb (gdbarch, pc))
8625 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
8626 val | t_bit);
756fe439 8627 else
61a1198a 8628 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9779414d 8629 val & ~t_bit);
756fe439
DJ
8630 }
8631}
123dc839 8632
58d6951d
DJ
8633/* Read the contents of a NEON quad register, by reading from two
8634 double registers. This is used to implement the quad pseudo
8635 registers, and for argument passing in case the quad registers are
8636 missing; vectors are passed in quad registers when using the VFP
8637 ABI, even if a NEON unit is not present. REGNUM is the index of
8638 the quad register, in [0, 15]. */
8639
05d1431c 8640static enum register_status
849d0ba8 8641arm_neon_quad_read (struct gdbarch *gdbarch, readable_regcache *regcache,
58d6951d
DJ
8642 int regnum, gdb_byte *buf)
8643{
8644 char name_buf[4];
8645 gdb_byte reg_buf[8];
8646 int offset, double_regnum;
05d1431c 8647 enum register_status status;
58d6951d 8648
8c042590 8649 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
58d6951d
DJ
8650 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8651 strlen (name_buf));
8652
8653 /* d0 is always the least significant half of q0. */
8654 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
8655 offset = 8;
8656 else
8657 offset = 0;
8658
03f50fc8 8659 status = regcache->raw_read (double_regnum, reg_buf);
05d1431c
PA
8660 if (status != REG_VALID)
8661 return status;
58d6951d
DJ
8662 memcpy (buf + offset, reg_buf, 8);
8663
8664 offset = 8 - offset;
03f50fc8 8665 status = regcache->raw_read (double_regnum + 1, reg_buf);
05d1431c
PA
8666 if (status != REG_VALID)
8667 return status;
58d6951d 8668 memcpy (buf + offset, reg_buf, 8);
05d1431c
PA
8669
8670 return REG_VALID;
58d6951d
DJ
8671}
8672
05d1431c 8673static enum register_status
849d0ba8 8674arm_pseudo_read (struct gdbarch *gdbarch, readable_regcache *regcache,
58d6951d
DJ
8675 int regnum, gdb_byte *buf)
8676{
8677 const int num_regs = gdbarch_num_regs (gdbarch);
8678 char name_buf[4];
8679 gdb_byte reg_buf[8];
8680 int offset, double_regnum;
8681
8682 gdb_assert (regnum >= num_regs);
8683 regnum -= num_regs;
8684
8685 if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
8686 /* Quad-precision register. */
05d1431c 8687 return arm_neon_quad_read (gdbarch, regcache, regnum - 32, buf);
58d6951d
DJ
8688 else
8689 {
05d1431c
PA
8690 enum register_status status;
8691
58d6951d
DJ
8692 /* Single-precision register. */
8693 gdb_assert (regnum < 32);
8694
8695 /* s0 is always the least significant half of d0. */
8696 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
8697 offset = (regnum & 1) ? 0 : 4;
8698 else
8699 offset = (regnum & 1) ? 4 : 0;
8700
8c042590 8701 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
58d6951d
DJ
8702 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8703 strlen (name_buf));
8704
03f50fc8 8705 status = regcache->raw_read (double_regnum, reg_buf);
05d1431c
PA
8706 if (status == REG_VALID)
8707 memcpy (buf, reg_buf + offset, 4);
8708 return status;
58d6951d
DJ
8709 }
8710}
8711
8712/* Store the contents of BUF to a NEON quad register, by writing to
8713 two double registers. This is used to implement the quad pseudo
8714 registers, and for argument passing in case the quad registers are
8715 missing; vectors are passed in quad registers when using the VFP
8716 ABI, even if a NEON unit is not present. REGNUM is the index
8717 of the quad register, in [0, 15]. */
8718
8719static void
8720arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
8721 int regnum, const gdb_byte *buf)
8722{
8723 char name_buf[4];
58d6951d
DJ
8724 int offset, double_regnum;
8725
8c042590 8726 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
58d6951d
DJ
8727 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8728 strlen (name_buf));
8729
8730 /* d0 is always the least significant half of q0. */
8731 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
8732 offset = 8;
8733 else
8734 offset = 0;
8735
10eaee5f 8736 regcache->raw_write (double_regnum, buf + offset);
58d6951d 8737 offset = 8 - offset;
10eaee5f 8738 regcache->raw_write (double_regnum + 1, buf + offset);
58d6951d
DJ
8739}
8740
8741static void
8742arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
8743 int regnum, const gdb_byte *buf)
8744{
8745 const int num_regs = gdbarch_num_regs (gdbarch);
8746 char name_buf[4];
8747 gdb_byte reg_buf[8];
8748 int offset, double_regnum;
8749
8750 gdb_assert (regnum >= num_regs);
8751 regnum -= num_regs;
8752
8753 if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
8754 /* Quad-precision register. */
8755 arm_neon_quad_write (gdbarch, regcache, regnum - 32, buf);
8756 else
8757 {
8758 /* Single-precision register. */
8759 gdb_assert (regnum < 32);
8760
8761 /* s0 is always the least significant half of d0. */
8762 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
8763 offset = (regnum & 1) ? 0 : 4;
8764 else
8765 offset = (regnum & 1) ? 4 : 0;
8766
8c042590 8767 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
58d6951d
DJ
8768 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8769 strlen (name_buf));
8770
0b883586 8771 regcache->raw_read (double_regnum, reg_buf);
58d6951d 8772 memcpy (reg_buf + offset, buf, 4);
10eaee5f 8773 regcache->raw_write (double_regnum, reg_buf);
58d6951d
DJ
8774 }
8775}
8776
123dc839
DJ
8777static struct value *
8778value_of_arm_user_reg (struct frame_info *frame, const void *baton)
8779{
9a3c8263 8780 const int *reg_p = (const int *) baton;
123dc839
DJ
8781 return value_of_register (*reg_p, frame);
8782}
97e03143 8783\f
70f80edf
JT
8784static enum gdb_osabi
8785arm_elf_osabi_sniffer (bfd *abfd)
97e03143 8786{
2af48f68 8787 unsigned int elfosabi;
70f80edf 8788 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
97e03143 8789
70f80edf 8790 elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
97e03143 8791
28e97307
DJ
8792 if (elfosabi == ELFOSABI_ARM)
8793 /* GNU tools use this value. Check note sections in this case,
8794 as well. */
8795 bfd_map_over_sections (abfd,
8796 generic_elf_osabi_sniff_abi_tag_sections,
8797 &osabi);
97e03143 8798
28e97307 8799 /* Anything else will be handled by the generic ELF sniffer. */
70f80edf 8800 return osabi;
97e03143
RE
8801}
8802
54483882
YQ
8803static int
8804arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
8805 struct reggroup *group)
8806{
2c291032
YQ
8807 /* FPS register's type is INT, but belongs to float_reggroup. Beside
8808 this, FPS register belongs to save_regroup, restore_reggroup, and
8809 all_reggroup, of course. */
54483882 8810 if (regnum == ARM_FPS_REGNUM)
2c291032
YQ
8811 return (group == float_reggroup
8812 || group == save_reggroup
8813 || group == restore_reggroup
8814 || group == all_reggroup);
54483882
YQ
8815 else
8816 return default_register_reggroup_p (gdbarch, regnum, group);
8817}
8818
25f8c692
JL
8819\f
8820/* For backward-compatibility we allow two 'g' packet lengths with
8821 the remote protocol depending on whether FPA registers are
8822 supplied. M-profile targets do not have FPA registers, but some
8823 stubs already exist in the wild which use a 'g' packet which
8824 supplies them albeit with dummy values. The packet format which
8825 includes FPA registers should be considered deprecated for
8826 M-profile targets. */
8827
8828static void
8829arm_register_g_packet_guesses (struct gdbarch *gdbarch)
8830{
8831 if (gdbarch_tdep (gdbarch)->is_m)
8832 {
8833 /* If we know from the executable this is an M-profile target,
8834 cater for remote targets whose register set layout is the
8835 same as the FPA layout. */
8836 register_remote_g_packet_guess (gdbarch,
03145bf4 8837 /* r0-r12,sp,lr,pc; f0-f7; fps,xpsr */
25f8c692
JL
8838 (16 * INT_REGISTER_SIZE)
8839 + (8 * FP_REGISTER_SIZE)
8840 + (2 * INT_REGISTER_SIZE),
8841 tdesc_arm_with_m_fpa_layout);
8842
8843 /* The regular M-profile layout. */
8844 register_remote_g_packet_guess (gdbarch,
8845 /* r0-r12,sp,lr,pc; xpsr */
8846 (16 * INT_REGISTER_SIZE)
8847 + INT_REGISTER_SIZE,
8848 tdesc_arm_with_m);
3184d3f9
JL
8849
8850 /* M-profile plus M4F VFP. */
8851 register_remote_g_packet_guess (gdbarch,
8852 /* r0-r12,sp,lr,pc; d0-d15; fpscr,xpsr */
8853 (16 * INT_REGISTER_SIZE)
8854 + (16 * VFP_REGISTER_SIZE)
8855 + (2 * INT_REGISTER_SIZE),
8856 tdesc_arm_with_m_vfp_d16);
25f8c692
JL
8857 }
8858
8859 /* Otherwise we don't have a useful guess. */
8860}
8861
7eb89530
YQ
8862/* Implement the code_of_frame_writable gdbarch method. */
8863
8864static int
8865arm_code_of_frame_writable (struct gdbarch *gdbarch, struct frame_info *frame)
8866{
8867 if (gdbarch_tdep (gdbarch)->is_m
8868 && get_frame_type (frame) == SIGTRAMP_FRAME)
8869 {
8870 /* M-profile exception frames return to some magic PCs, where
8871 isn't writable at all. */
8872 return 0;
8873 }
8874 else
8875 return 1;
8876}
8877
70f80edf 8878\f
da3c6d4a
MS
8879/* Initialize the current architecture based on INFO. If possible,
8880 re-use an architecture from ARCHES, which is a list of
8881 architectures already created during this debugging session.
97e03143 8882
da3c6d4a
MS
8883 Called e.g. at program startup, when reading a core file, and when
8884 reading a binary file. */
97e03143 8885
39bbf761
RE
8886static struct gdbarch *
8887arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
8888{
97e03143 8889 struct gdbarch_tdep *tdep;
39bbf761 8890 struct gdbarch *gdbarch;
28e97307
DJ
8891 struct gdbarch_list *best_arch;
8892 enum arm_abi_kind arm_abi = arm_abi_global;
8893 enum arm_float_model fp_model = arm_fp_model;
123dc839 8894 struct tdesc_arch_data *tdesc_data = NULL;
9779414d 8895 int i, is_m = 0;
330c6ca9 8896 int vfp_register_count = 0, have_vfp_pseudos = 0, have_neon_pseudos = 0;
a56cc1ce 8897 int have_wmmx_registers = 0;
58d6951d 8898 int have_neon = 0;
ff6f572f 8899 int have_fpa_registers = 1;
9779414d
DJ
8900 const struct target_desc *tdesc = info.target_desc;
8901
8902 /* If we have an object to base this architecture on, try to determine
8903 its ABI. */
8904
8905 if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
8906 {
8907 int ei_osabi, e_flags;
8908
8909 switch (bfd_get_flavour (info.abfd))
8910 {
9779414d
DJ
8911 case bfd_target_coff_flavour:
8912 /* Assume it's an old APCS-style ABI. */
8913 /* XXX WinCE? */
8914 arm_abi = ARM_ABI_APCS;
8915 break;
8916
8917 case bfd_target_elf_flavour:
8918 ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
8919 e_flags = elf_elfheader (info.abfd)->e_flags;
8920
8921 if (ei_osabi == ELFOSABI_ARM)
8922 {
8923 /* GNU tools used to use this value, but do not for EABI
8924 objects. There's nowhere to tag an EABI version
8925 anyway, so assume APCS. */
8926 arm_abi = ARM_ABI_APCS;
8927 }
d403db27 8928 else if (ei_osabi == ELFOSABI_NONE || ei_osabi == ELFOSABI_GNU)
9779414d
DJ
8929 {
8930 int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
9779414d
DJ
8931
8932 switch (eabi_ver)
8933 {
8934 case EF_ARM_EABI_UNKNOWN:
8935 /* Assume GNU tools. */
8936 arm_abi = ARM_ABI_APCS;
8937 break;
8938
8939 case EF_ARM_EABI_VER4:
8940 case EF_ARM_EABI_VER5:
8941 arm_abi = ARM_ABI_AAPCS;
8942 /* EABI binaries default to VFP float ordering.
8943 They may also contain build attributes that can
8944 be used to identify if the VFP argument-passing
8945 ABI is in use. */
8946 if (fp_model == ARM_FLOAT_AUTO)
8947 {
8948#ifdef HAVE_ELF
8949 switch (bfd_elf_get_obj_attr_int (info.abfd,
8950 OBJ_ATTR_PROC,
8951 Tag_ABI_VFP_args))
8952 {
b35b0298 8953 case AEABI_VFP_args_base:
9779414d
DJ
8954 /* "The user intended FP parameter/result
8955 passing to conform to AAPCS, base
8956 variant". */
8957 fp_model = ARM_FLOAT_SOFT_VFP;
8958 break;
b35b0298 8959 case AEABI_VFP_args_vfp:
9779414d
DJ
8960 /* "The user intended FP parameter/result
8961 passing to conform to AAPCS, VFP
8962 variant". */
8963 fp_model = ARM_FLOAT_VFP;
8964 break;
b35b0298 8965 case AEABI_VFP_args_toolchain:
9779414d
DJ
8966 /* "The user intended FP parameter/result
8967 passing to conform to tool chain-specific
8968 conventions" - we don't know any such
8969 conventions, so leave it as "auto". */
8970 break;
b35b0298 8971 case AEABI_VFP_args_compatible:
5c294fee
TG
8972 /* "Code is compatible with both the base
8973 and VFP variants; the user did not permit
8974 non-variadic functions to pass FP
8975 parameters/results" - leave it as
8976 "auto". */
8977 break;
9779414d
DJ
8978 default:
8979 /* Attribute value not mentioned in the
5c294fee 8980 November 2012 ABI, so leave it as
9779414d
DJ
8981 "auto". */
8982 break;
8983 }
8984#else
8985 fp_model = ARM_FLOAT_SOFT_VFP;
8986#endif
8987 }
8988 break;
8989
8990 default:
8991 /* Leave it as "auto". */
8992 warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
8993 break;
8994 }
8995
8996#ifdef HAVE_ELF
8997 /* Detect M-profile programs. This only works if the
8998 executable file includes build attributes; GCC does
8999 copy them to the executable, but e.g. RealView does
9000 not. */
17cbafdb
SM
9001 int attr_arch
9002 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9003 Tag_CPU_arch);
9004 int attr_profile
9005 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9006 Tag_CPU_arch_profile);
9007
9779414d
DJ
9008 /* GCC specifies the profile for v6-M; RealView only
9009 specifies the profile for architectures starting with
9010 V7 (as opposed to architectures with a tag
9011 numerically greater than TAG_CPU_ARCH_V7). */
9012 if (!tdesc_has_registers (tdesc)
9013 && (attr_arch == TAG_CPU_ARCH_V6_M
9014 || attr_arch == TAG_CPU_ARCH_V6S_M
9015 || attr_profile == 'M'))
25f8c692 9016 is_m = 1;
9779414d
DJ
9017#endif
9018 }
9019
9020 if (fp_model == ARM_FLOAT_AUTO)
9021 {
9779414d
DJ
9022 switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
9023 {
9024 case 0:
9025 /* Leave it as "auto". Strictly speaking this case
9026 means FPA, but almost nobody uses that now, and
9027 many toolchains fail to set the appropriate bits
9028 for the floating-point model they use. */
9029 break;
9030 case EF_ARM_SOFT_FLOAT:
9031 fp_model = ARM_FLOAT_SOFT_FPA;
9032 break;
9033 case EF_ARM_VFP_FLOAT:
9034 fp_model = ARM_FLOAT_VFP;
9035 break;
9036 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
9037 fp_model = ARM_FLOAT_SOFT_VFP;
9038 break;
9039 }
9040 }
9041
9042 if (e_flags & EF_ARM_BE8)
9043 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
9044
9045 break;
9046
9047 default:
9048 /* Leave it as "auto". */
9049 break;
9050 }
9051 }
123dc839
DJ
9052
9053 /* Check any target description for validity. */
9779414d 9054 if (tdesc_has_registers (tdesc))
123dc839
DJ
9055 {
9056 /* For most registers we require GDB's default names; but also allow
9057 the numeric names for sp / lr / pc, as a convenience. */
9058 static const char *const arm_sp_names[] = { "r13", "sp", NULL };
9059 static const char *const arm_lr_names[] = { "r14", "lr", NULL };
9060 static const char *const arm_pc_names[] = { "r15", "pc", NULL };
9061
9062 const struct tdesc_feature *feature;
58d6951d 9063 int valid_p;
123dc839 9064
9779414d 9065 feature = tdesc_find_feature (tdesc,
123dc839
DJ
9066 "org.gnu.gdb.arm.core");
9067 if (feature == NULL)
9779414d
DJ
9068 {
9069 feature = tdesc_find_feature (tdesc,
9070 "org.gnu.gdb.arm.m-profile");
9071 if (feature == NULL)
9072 return NULL;
9073 else
9074 is_m = 1;
9075 }
123dc839
DJ
9076
9077 tdesc_data = tdesc_data_alloc ();
9078
9079 valid_p = 1;
9080 for (i = 0; i < ARM_SP_REGNUM; i++)
9081 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
9082 arm_register_names[i]);
9083 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
9084 ARM_SP_REGNUM,
9085 arm_sp_names);
9086 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
9087 ARM_LR_REGNUM,
9088 arm_lr_names);
9089 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
9090 ARM_PC_REGNUM,
9091 arm_pc_names);
9779414d
DJ
9092 if (is_m)
9093 valid_p &= tdesc_numbered_register (feature, tdesc_data,
9094 ARM_PS_REGNUM, "xpsr");
9095 else
9096 valid_p &= tdesc_numbered_register (feature, tdesc_data,
9097 ARM_PS_REGNUM, "cpsr");
123dc839
DJ
9098
9099 if (!valid_p)
9100 {
9101 tdesc_data_cleanup (tdesc_data);
9102 return NULL;
9103 }
9104
9779414d 9105 feature = tdesc_find_feature (tdesc,
123dc839
DJ
9106 "org.gnu.gdb.arm.fpa");
9107 if (feature != NULL)
9108 {
9109 valid_p = 1;
9110 for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
9111 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
9112 arm_register_names[i]);
9113 if (!valid_p)
9114 {
9115 tdesc_data_cleanup (tdesc_data);
9116 return NULL;
9117 }
9118 }
ff6f572f
DJ
9119 else
9120 have_fpa_registers = 0;
9121
9779414d 9122 feature = tdesc_find_feature (tdesc,
ff6f572f
DJ
9123 "org.gnu.gdb.xscale.iwmmxt");
9124 if (feature != NULL)
9125 {
9126 static const char *const iwmmxt_names[] = {
9127 "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
9128 "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
9129 "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
9130 "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
9131 };
9132
9133 valid_p = 1;
9134 for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
9135 valid_p
9136 &= tdesc_numbered_register (feature, tdesc_data, i,
9137 iwmmxt_names[i - ARM_WR0_REGNUM]);
9138
9139 /* Check for the control registers, but do not fail if they
9140 are missing. */
9141 for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
9142 tdesc_numbered_register (feature, tdesc_data, i,
9143 iwmmxt_names[i - ARM_WR0_REGNUM]);
9144
9145 for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
9146 valid_p
9147 &= tdesc_numbered_register (feature, tdesc_data, i,
9148 iwmmxt_names[i - ARM_WR0_REGNUM]);
9149
9150 if (!valid_p)
9151 {
9152 tdesc_data_cleanup (tdesc_data);
9153 return NULL;
9154 }
a56cc1ce
YQ
9155
9156 have_wmmx_registers = 1;
ff6f572f 9157 }
58d6951d
DJ
9158
9159 /* If we have a VFP unit, check whether the single precision registers
9160 are present. If not, then we will synthesize them as pseudo
9161 registers. */
9779414d 9162 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
9163 "org.gnu.gdb.arm.vfp");
9164 if (feature != NULL)
9165 {
9166 static const char *const vfp_double_names[] = {
9167 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
9168 "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
9169 "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
9170 "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
9171 };
9172
9173 /* Require the double precision registers. There must be either
9174 16 or 32. */
9175 valid_p = 1;
9176 for (i = 0; i < 32; i++)
9177 {
9178 valid_p &= tdesc_numbered_register (feature, tdesc_data,
9179 ARM_D0_REGNUM + i,
9180 vfp_double_names[i]);
9181 if (!valid_p)
9182 break;
9183 }
2b9e5ea6
UW
9184 if (!valid_p && i == 16)
9185 valid_p = 1;
58d6951d 9186
2b9e5ea6
UW
9187 /* Also require FPSCR. */
9188 valid_p &= tdesc_numbered_register (feature, tdesc_data,
9189 ARM_FPSCR_REGNUM, "fpscr");
9190 if (!valid_p)
58d6951d
DJ
9191 {
9192 tdesc_data_cleanup (tdesc_data);
9193 return NULL;
9194 }
9195
9196 if (tdesc_unnumbered_register (feature, "s0") == 0)
9197 have_vfp_pseudos = 1;
9198
330c6ca9 9199 vfp_register_count = i;
58d6951d
DJ
9200
9201 /* If we have VFP, also check for NEON. The architecture allows
9202 NEON without VFP (integer vector operations only), but GDB
9203 does not support that. */
9779414d 9204 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
9205 "org.gnu.gdb.arm.neon");
9206 if (feature != NULL)
9207 {
9208 /* NEON requires 32 double-precision registers. */
9209 if (i != 32)
9210 {
9211 tdesc_data_cleanup (tdesc_data);
9212 return NULL;
9213 }
9214
9215 /* If there are quad registers defined by the stub, use
9216 their type; otherwise (normally) provide them with
9217 the default type. */
9218 if (tdesc_unnumbered_register (feature, "q0") == 0)
9219 have_neon_pseudos = 1;
9220
9221 have_neon = 1;
9222 }
9223 }
123dc839 9224 }
39bbf761 9225
28e97307
DJ
9226 /* If there is already a candidate, use it. */
9227 for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
9228 best_arch != NULL;
9229 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
9230 {
b8926edc
DJ
9231 if (arm_abi != ARM_ABI_AUTO
9232 && arm_abi != gdbarch_tdep (best_arch->gdbarch)->arm_abi)
28e97307
DJ
9233 continue;
9234
b8926edc
DJ
9235 if (fp_model != ARM_FLOAT_AUTO
9236 && fp_model != gdbarch_tdep (best_arch->gdbarch)->fp_model)
28e97307
DJ
9237 continue;
9238
58d6951d
DJ
9239 /* There are various other properties in tdep that we do not
9240 need to check here: those derived from a target description,
9241 since gdbarches with a different target description are
9242 automatically disqualified. */
9243
9779414d
DJ
9244 /* Do check is_m, though, since it might come from the binary. */
9245 if (is_m != gdbarch_tdep (best_arch->gdbarch)->is_m)
9246 continue;
9247
28e97307
DJ
9248 /* Found a match. */
9249 break;
9250 }
97e03143 9251
28e97307 9252 if (best_arch != NULL)
123dc839
DJ
9253 {
9254 if (tdesc_data != NULL)
9255 tdesc_data_cleanup (tdesc_data);
9256 return best_arch->gdbarch;
9257 }
28e97307 9258
8d749320 9259 tdep = XCNEW (struct gdbarch_tdep);
97e03143
RE
9260 gdbarch = gdbarch_alloc (&info, tdep);
9261
28e97307
DJ
9262 /* Record additional information about the architecture we are defining.
9263 These are gdbarch discriminators, like the OSABI. */
9264 tdep->arm_abi = arm_abi;
9265 tdep->fp_model = fp_model;
9779414d 9266 tdep->is_m = is_m;
ff6f572f 9267 tdep->have_fpa_registers = have_fpa_registers;
a56cc1ce 9268 tdep->have_wmmx_registers = have_wmmx_registers;
330c6ca9
YQ
9269 gdb_assert (vfp_register_count == 0
9270 || vfp_register_count == 16
9271 || vfp_register_count == 32);
9272 tdep->vfp_register_count = vfp_register_count;
58d6951d
DJ
9273 tdep->have_vfp_pseudos = have_vfp_pseudos;
9274 tdep->have_neon_pseudos = have_neon_pseudos;
9275 tdep->have_neon = have_neon;
08216dd7 9276
25f8c692
JL
9277 arm_register_g_packet_guesses (gdbarch);
9278
08216dd7 9279 /* Breakpoints. */
9d4fde75 9280 switch (info.byte_order_for_code)
67255d04
RE
9281 {
9282 case BFD_ENDIAN_BIG:
66e810cd
RE
9283 tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
9284 tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
9285 tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
9286 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
9287
67255d04
RE
9288 break;
9289
9290 case BFD_ENDIAN_LITTLE:
66e810cd
RE
9291 tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
9292 tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
9293 tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
9294 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
9295
67255d04
RE
9296 break;
9297
9298 default:
9299 internal_error (__FILE__, __LINE__,
edefbb7c 9300 _("arm_gdbarch_init: bad byte order for float format"));
67255d04
RE
9301 }
9302
d7b486e7
RE
9303 /* On ARM targets char defaults to unsigned. */
9304 set_gdbarch_char_signed (gdbarch, 0);
9305
53375380
PA
9306 /* wchar_t is unsigned under the AAPCS. */
9307 if (tdep->arm_abi == ARM_ABI_AAPCS)
9308 set_gdbarch_wchar_signed (gdbarch, 0);
9309 else
9310 set_gdbarch_wchar_signed (gdbarch, 1);
53375380 9311
cca44b1b
JB
9312 /* Note: for displaced stepping, this includes the breakpoint, and one word
9313 of additional scratch space. This setting isn't used for anything beside
9314 displaced stepping at present. */
9315 set_gdbarch_max_insn_length (gdbarch, 4 * DISPLACED_MODIFIED_INSNS);
9316
9df628e0 9317 /* This should be low enough for everything. */
97e03143 9318 tdep->lowest_pc = 0x20;
94c30b78 9319 tdep->jb_pc = -1; /* Longjump support not enabled by default. */
97e03143 9320
7c00367c
MK
9321 /* The default, for both APCS and AAPCS, is to return small
9322 structures in registers. */
9323 tdep->struct_return = reg_struct_return;
9324
2dd604e7 9325 set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
f53f0d0b 9326 set_gdbarch_frame_align (gdbarch, arm_frame_align);
39bbf761 9327
7eb89530
YQ
9328 if (is_m)
9329 set_gdbarch_code_of_frame_writable (gdbarch, arm_code_of_frame_writable);
9330
756fe439
DJ
9331 set_gdbarch_write_pc (gdbarch, arm_write_pc);
9332
eb5492fa 9333 frame_base_set_default (gdbarch, &arm_normal_base);
148754e5 9334
34e8f22d 9335 /* Address manipulation. */
34e8f22d
RE
9336 set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
9337
34e8f22d
RE
9338 /* Advance PC across function entry code. */
9339 set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
9340
c9cf6e20
MG
9341 /* Detect whether PC is at a point where the stack has been destroyed. */
9342 set_gdbarch_stack_frame_destroyed_p (gdbarch, arm_stack_frame_destroyed_p);
4024ca99 9343
190dce09
UW
9344 /* Skip trampolines. */
9345 set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
9346
34e8f22d
RE
9347 /* The stack grows downward. */
9348 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
9349
9350 /* Breakpoint manipulation. */
04180708
YQ
9351 set_gdbarch_breakpoint_kind_from_pc (gdbarch, arm_breakpoint_kind_from_pc);
9352 set_gdbarch_sw_breakpoint_from_kind (gdbarch, arm_sw_breakpoint_from_kind);
833b7ab5
YQ
9353 set_gdbarch_breakpoint_kind_from_current_state (gdbarch,
9354 arm_breakpoint_kind_from_current_state);
34e8f22d
RE
9355
9356 /* Information about registers, etc. */
34e8f22d
RE
9357 set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
9358 set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
ff6f572f 9359 set_gdbarch_num_regs (gdbarch, ARM_NUM_REGS);
7a5ea0d4 9360 set_gdbarch_register_type (gdbarch, arm_register_type);
54483882 9361 set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);
34e8f22d 9362
ff6f572f
DJ
9363 /* This "info float" is FPA-specific. Use the generic version if we
9364 do not have FPA. */
9365 if (gdbarch_tdep (gdbarch)->have_fpa_registers)
9366 set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
9367
26216b98 9368 /* Internal <-> external register number maps. */
ff6f572f 9369 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
26216b98
AC
9370 set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
9371
34e8f22d
RE
9372 set_gdbarch_register_name (gdbarch, arm_register_name);
9373
9374 /* Returning results. */
2af48f68 9375 set_gdbarch_return_value (gdbarch, arm_return_value);
34e8f22d 9376
03d48a7d
RE
9377 /* Disassembly. */
9378 set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
9379
34e8f22d
RE
9380 /* Minsymbol frobbing. */
9381 set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
9382 set_gdbarch_coff_make_msymbol_special (gdbarch,
9383 arm_coff_make_msymbol_special);
60c5725c 9384 set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
34e8f22d 9385
f9d67f43
DJ
9386 /* Thumb-2 IT block support. */
9387 set_gdbarch_adjust_breakpoint_address (gdbarch,
9388 arm_adjust_breakpoint_address);
9389
0d5de010
DJ
9390 /* Virtual tables. */
9391 set_gdbarch_vbit_in_delta (gdbarch, 1);
9392
97e03143 9393 /* Hook in the ABI-specific overrides, if they have been registered. */
4be87837 9394 gdbarch_init_osabi (info, gdbarch);
97e03143 9395
b39cc962
DJ
9396 dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
9397
eb5492fa 9398 /* Add some default predicates. */
2ae28aa9
YQ
9399 if (is_m)
9400 frame_unwind_append_unwinder (gdbarch, &arm_m_exception_unwind);
a262aec2
DJ
9401 frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
9402 dwarf2_append_unwinders (gdbarch);
0e9e9abd 9403 frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
779aa56f 9404 frame_unwind_append_unwinder (gdbarch, &arm_epilogue_frame_unwind);
a262aec2 9405 frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
eb5492fa 9406
97e03143
RE
9407 /* Now we have tuned the configuration, set a few final things,
9408 based on what the OS ABI has told us. */
9409
b8926edc
DJ
9410 /* If the ABI is not otherwise marked, assume the old GNU APCS. EABI
9411 binaries are always marked. */
9412 if (tdep->arm_abi == ARM_ABI_AUTO)
9413 tdep->arm_abi = ARM_ABI_APCS;
9414
e3039479
UW
9415 /* Watchpoints are not steppable. */
9416 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
9417
b8926edc
DJ
9418 /* We used to default to FPA for generic ARM, but almost nobody
9419 uses that now, and we now provide a way for the user to force
9420 the model. So default to the most useful variant. */
9421 if (tdep->fp_model == ARM_FLOAT_AUTO)
9422 tdep->fp_model = ARM_FLOAT_SOFT_FPA;
9423
9df628e0
RE
9424 if (tdep->jb_pc >= 0)
9425 set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
9426
08216dd7 9427 /* Floating point sizes and format. */
8da61cc4 9428 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
b8926edc 9429 if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
08216dd7 9430 {
8da61cc4
DJ
9431 set_gdbarch_double_format
9432 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
9433 set_gdbarch_long_double_format
9434 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
9435 }
9436 else
9437 {
9438 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
9439 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
08216dd7
RE
9440 }
9441
58d6951d
DJ
9442 if (have_vfp_pseudos)
9443 {
9444 /* NOTE: These are the only pseudo registers used by
9445 the ARM target at the moment. If more are added, a
9446 little more care in numbering will be needed. */
9447
9448 int num_pseudos = 32;
9449 if (have_neon_pseudos)
9450 num_pseudos += 16;
9451 set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
9452 set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
9453 set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
9454 }
9455
123dc839 9456 if (tdesc_data)
58d6951d
DJ
9457 {
9458 set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
9459
9779414d 9460 tdesc_use_registers (gdbarch, tdesc, tdesc_data);
58d6951d
DJ
9461
9462 /* Override tdesc_register_type to adjust the types of VFP
9463 registers for NEON. */
9464 set_gdbarch_register_type (gdbarch, arm_register_type);
9465 }
123dc839
DJ
9466
9467 /* Add standard register aliases. We add aliases even for those
9468 nanes which are used by the current architecture - it's simpler,
9469 and does no harm, since nothing ever lists user registers. */
9470 for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
9471 user_reg_add (gdbarch, arm_register_aliases[i].name,
9472 value_of_arm_user_reg, &arm_register_aliases[i].regnum);
9473
65b48a81
PB
9474 set_gdbarch_disassembler_options (gdbarch, &arm_disassembler_options);
9475 set_gdbarch_valid_disassembler_options (gdbarch, disassembler_options_arm ());
9476
39bbf761
RE
9477 return gdbarch;
9478}
9479
97e03143 9480static void
2af46ca0 9481arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
97e03143 9482{
2af46ca0 9483 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
97e03143
RE
9484
9485 if (tdep == NULL)
9486 return;
9487
edefbb7c 9488 fprintf_unfiltered (file, _("arm_dump_tdep: Lowest pc = 0x%lx"),
97e03143
RE
9489 (unsigned long) tdep->lowest_pc);
9490}
9491
0d4c07af 9492#if GDB_SELF_TEST
b121eeb9
YQ
9493namespace selftests
9494{
9495static void arm_record_test (void);
9496}
0d4c07af 9497#endif
b121eeb9 9498
c906108c 9499void
ed9a39eb 9500_initialize_arm_tdep (void)
c906108c 9501{
bc90b915 9502 long length;
65b48a81 9503 int i, j;
edefbb7c
AC
9504 char regdesc[1024], *rdptr = regdesc;
9505 size_t rest = sizeof (regdesc);
085dd6e6 9506
42cf1509 9507 gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
97e03143 9508
60c5725c 9509 arm_objfile_data_key
c1bd65d0 9510 = register_objfile_data_with_cleanup (NULL, arm_objfile_data_free);
60c5725c 9511
0e9e9abd 9512 /* Add ourselves to objfile event chain. */
76727919 9513 gdb::observers::new_objfile.attach (arm_exidx_new_objfile);
0e9e9abd
UW
9514 arm_exidx_data_key
9515 = register_objfile_data_with_cleanup (NULL, arm_exidx_data_free);
9516
70f80edf
JT
9517 /* Register an ELF OS ABI sniffer for ARM binaries. */
9518 gdbarch_register_osabi_sniffer (bfd_arch_arm,
9519 bfd_target_elf_flavour,
9520 arm_elf_osabi_sniffer);
9521
9779414d
DJ
9522 /* Initialize the standard target descriptions. */
9523 initialize_tdesc_arm_with_m ();
25f8c692 9524 initialize_tdesc_arm_with_m_fpa_layout ();
3184d3f9 9525 initialize_tdesc_arm_with_m_vfp_d16 ();
ef7e8358
UW
9526 initialize_tdesc_arm_with_iwmmxt ();
9527 initialize_tdesc_arm_with_vfpv2 ();
9528 initialize_tdesc_arm_with_vfpv3 ();
9529 initialize_tdesc_arm_with_neon ();
9779414d 9530
afd7eef0
RE
9531 /* Add root prefix command for all "set arm"/"show arm" commands. */
9532 add_prefix_cmd ("arm", no_class, set_arm_command,
edefbb7c 9533 _("Various ARM-specific commands."),
afd7eef0
RE
9534 &setarmcmdlist, "set arm ", 0, &setlist);
9535
9536 add_prefix_cmd ("arm", no_class, show_arm_command,
edefbb7c 9537 _("Various ARM-specific commands."),
afd7eef0 9538 &showarmcmdlist, "show arm ", 0, &showlist);
bc90b915 9539
c5aa993b 9540
65b48a81 9541 arm_disassembler_options = xstrdup ("reg-names-std");
471b9d15
MR
9542 const disasm_options_t *disasm_options
9543 = &disassembler_options_arm ()->options;
65b48a81
PB
9544 int num_disassembly_styles = 0;
9545 for (i = 0; disasm_options->name[i] != NULL; i++)
9546 if (CONST_STRNEQ (disasm_options->name[i], "reg-names-"))
9547 num_disassembly_styles++;
9548
9549 /* Initialize the array that will be passed to add_setshow_enum_cmd(). */
8d749320 9550 valid_disassembly_styles = XNEWVEC (const char *,
65b48a81
PB
9551 num_disassembly_styles + 1);
9552 for (i = j = 0; disasm_options->name[i] != NULL; i++)
9553 if (CONST_STRNEQ (disasm_options->name[i], "reg-names-"))
9554 {
9555 size_t offset = strlen ("reg-names-");
9556 const char *style = disasm_options->name[i];
9557 valid_disassembly_styles[j++] = &style[offset];
9558 length = snprintf (rdptr, rest, "%s - %s\n", &style[offset],
9559 disasm_options->description[i]);
9560 rdptr += length;
9561 rest -= length;
9562 }
94c30b78 9563 /* Mark the end of valid options. */
65b48a81 9564 valid_disassembly_styles[num_disassembly_styles] = NULL;
c906108c 9565
edefbb7c 9566 /* Create the help text. */
d7e74731
PA
9567 std::string helptext = string_printf ("%s%s%s",
9568 _("The valid values are:\n"),
9569 regdesc,
9570 _("The default is \"std\"."));
ed9a39eb 9571
edefbb7c
AC
9572 add_setshow_enum_cmd("disassembler", no_class,
9573 valid_disassembly_styles, &disassembly_style,
9574 _("Set the disassembly style."),
9575 _("Show the disassembly style."),
09b0e4b0 9576 helptext.c_str (),
2c5b56ce 9577 set_disassembly_style_sfunc,
65b48a81 9578 show_disassembly_style_sfunc,
7376b4c2 9579 &setarmcmdlist, &showarmcmdlist);
edefbb7c
AC
9580
9581 add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
9582 _("Set usage of ARM 32-bit mode."),
9583 _("Show usage of ARM 32-bit mode."),
9584 _("When off, a 26-bit PC will be used."),
2c5b56ce 9585 NULL,
0963b4bd
MS
9586 NULL, /* FIXME: i18n: Usage of ARM 32-bit
9587 mode is %s. */
26304000 9588 &setarmcmdlist, &showarmcmdlist);
c906108c 9589
fd50bc42 9590 /* Add a command to allow the user to force the FPU model. */
edefbb7c
AC
9591 add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
9592 _("Set the floating point type."),
9593 _("Show the floating point type."),
9594 _("auto - Determine the FP typefrom the OS-ABI.\n\
9595softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
9596fpa - FPA co-processor (GCC compiled).\n\
9597softvfp - Software FP with pure-endian doubles.\n\
9598vfp - VFP co-processor."),
edefbb7c 9599 set_fp_model_sfunc, show_fp_model,
7376b4c2 9600 &setarmcmdlist, &showarmcmdlist);
fd50bc42 9601
28e97307
DJ
9602 /* Add a command to allow the user to force the ABI. */
9603 add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
9604 _("Set the ABI."),
9605 _("Show the ABI."),
9606 NULL, arm_set_abi, arm_show_abi,
9607 &setarmcmdlist, &showarmcmdlist);
9608
0428b8f5
DJ
9609 /* Add two commands to allow the user to force the assumed
9610 execution mode. */
9611 add_setshow_enum_cmd ("fallback-mode", class_support,
9612 arm_mode_strings, &arm_fallback_mode_string,
9613 _("Set the mode assumed when symbols are unavailable."),
9614 _("Show the mode assumed when symbols are unavailable."),
9615 NULL, NULL, arm_show_fallback_mode,
9616 &setarmcmdlist, &showarmcmdlist);
9617 add_setshow_enum_cmd ("force-mode", class_support,
9618 arm_mode_strings, &arm_force_mode_string,
9619 _("Set the mode assumed even when symbols are available."),
9620 _("Show the mode assumed even when symbols are available."),
9621 NULL, NULL, arm_show_force_mode,
9622 &setarmcmdlist, &showarmcmdlist);
9623
6529d2dd 9624 /* Debugging flag. */
edefbb7c
AC
9625 add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
9626 _("Set ARM debugging."),
9627 _("Show ARM debugging."),
9628 _("When on, arm-specific debugging is enabled."),
2c5b56ce 9629 NULL,
7915a72c 9630 NULL, /* FIXME: i18n: "ARM debugging is %s. */
26304000 9631 &setdebuglist, &showdebuglist);
b121eeb9
YQ
9632
9633#if GDB_SELF_TEST
1526853e 9634 selftests::register_test ("arm-record", selftests::arm_record_test);
b121eeb9
YQ
9635#endif
9636
c906108c 9637}
72508ac0
PO
9638
9639/* ARM-reversible process record data structures. */
9640
9641#define ARM_INSN_SIZE_BYTES 4
9642#define THUMB_INSN_SIZE_BYTES 2
9643#define THUMB2_INSN_SIZE_BYTES 4
9644
9645
71e396f9
LM
9646/* Position of the bit within a 32-bit ARM instruction
9647 that defines whether the instruction is a load or store. */
72508ac0
PO
9648#define INSN_S_L_BIT_NUM 20
9649
9650#define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
9651 do \
9652 { \
9653 unsigned int reg_len = LENGTH; \
9654 if (reg_len) \
9655 { \
9656 REGS = XNEWVEC (uint32_t, reg_len); \
9657 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
9658 } \
9659 } \
9660 while (0)
9661
9662#define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
9663 do \
9664 { \
9665 unsigned int mem_len = LENGTH; \
9666 if (mem_len) \
9667 { \
9668 MEMS = XNEWVEC (struct arm_mem_r, mem_len); \
9669 memcpy(&MEMS->len, &RECORD_BUF[0], \
9670 sizeof(struct arm_mem_r) * LENGTH); \
9671 } \
9672 } \
9673 while (0)
9674
9675/* Checks whether insn is already recorded or yet to be decoded. (boolean expression). */
9676#define INSN_RECORDED(ARM_RECORD) \
9677 (0 != (ARM_RECORD)->reg_rec_count || 0 != (ARM_RECORD)->mem_rec_count)
9678
9679/* ARM memory record structure. */
9680struct arm_mem_r
9681{
9682 uint32_t len; /* Record length. */
bfbbec00 9683 uint32_t addr; /* Memory address. */
72508ac0
PO
9684};
9685
9686/* ARM instruction record contains opcode of current insn
9687 and execution state (before entry to decode_insn()),
9688 contains list of to-be-modified registers and
9689 memory blocks (on return from decode_insn()). */
9690
9691typedef struct insn_decode_record_t
9692{
9693 struct gdbarch *gdbarch;
9694 struct regcache *regcache;
9695 CORE_ADDR this_addr; /* Address of the insn being decoded. */
9696 uint32_t arm_insn; /* Should accommodate thumb. */
9697 uint32_t cond; /* Condition code. */
9698 uint32_t opcode; /* Insn opcode. */
9699 uint32_t decode; /* Insn decode bits. */
9700 uint32_t mem_rec_count; /* No of mem records. */
9701 uint32_t reg_rec_count; /* No of reg records. */
9702 uint32_t *arm_regs; /* Registers to be saved for this record. */
9703 struct arm_mem_r *arm_mems; /* Memory to be saved for this record. */
9704} insn_decode_record;
9705
9706
9707/* Checks ARM SBZ and SBO mandatory fields. */
9708
9709static int
9710sbo_sbz (uint32_t insn, uint32_t bit_num, uint32_t len, uint32_t sbo)
9711{
9712 uint32_t ones = bits (insn, bit_num - 1, (bit_num -1) + (len - 1));
9713
9714 if (!len)
9715 return 1;
9716
9717 if (!sbo)
9718 ones = ~ones;
9719
9720 while (ones)
9721 {
9722 if (!(ones & sbo))
9723 {
9724 return 0;
9725 }
9726 ones = ones >> 1;
9727 }
9728 return 1;
9729}
9730
c6ec2b30
OJ
9731enum arm_record_result
9732{
9733 ARM_RECORD_SUCCESS = 0,
9734 ARM_RECORD_FAILURE = 1
9735};
9736
72508ac0
PO
9737typedef enum
9738{
9739 ARM_RECORD_STRH=1,
9740 ARM_RECORD_STRD
9741} arm_record_strx_t;
9742
9743typedef enum
9744{
9745 ARM_RECORD=1,
9746 THUMB_RECORD,
9747 THUMB2_RECORD
9748} record_type_t;
9749
9750
9751static int
9752arm_record_strx (insn_decode_record *arm_insn_r, uint32_t *record_buf,
9753 uint32_t *record_buf_mem, arm_record_strx_t str_type)
9754{
9755
9756 struct regcache *reg_cache = arm_insn_r->regcache;
9757 ULONGEST u_regval[2]= {0};
9758
9759 uint32_t reg_src1 = 0, reg_src2 = 0;
9760 uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
72508ac0
PO
9761
9762 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
9763 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
72508ac0
PO
9764
9765 if (14 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
9766 {
9767 /* 1) Handle misc store, immediate offset. */
9768 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
9769 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
9770 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
9771 regcache_raw_read_unsigned (reg_cache, reg_src1,
9772 &u_regval[0]);
9773 if (ARM_PC_REGNUM == reg_src1)
9774 {
9775 /* If R15 was used as Rn, hence current PC+8. */
9776 u_regval[0] = u_regval[0] + 8;
9777 }
9778 offset_8 = (immed_high << 4) | immed_low;
9779 /* Calculate target store address. */
9780 if (14 == arm_insn_r->opcode)
9781 {
9782 tgt_mem_addr = u_regval[0] + offset_8;
9783 }
9784 else
9785 {
9786 tgt_mem_addr = u_regval[0] - offset_8;
9787 }
9788 if (ARM_RECORD_STRH == str_type)
9789 {
9790 record_buf_mem[0] = 2;
9791 record_buf_mem[1] = tgt_mem_addr;
9792 arm_insn_r->mem_rec_count = 1;
9793 }
9794 else if (ARM_RECORD_STRD == str_type)
9795 {
9796 record_buf_mem[0] = 4;
9797 record_buf_mem[1] = tgt_mem_addr;
9798 record_buf_mem[2] = 4;
9799 record_buf_mem[3] = tgt_mem_addr + 4;
9800 arm_insn_r->mem_rec_count = 2;
9801 }
9802 }
9803 else if (12 == arm_insn_r->opcode || 8 == arm_insn_r->opcode)
9804 {
9805 /* 2) Store, register offset. */
9806 /* Get Rm. */
9807 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
9808 /* Get Rn. */
9809 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
9810 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
9811 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
9812 if (15 == reg_src2)
9813 {
9814 /* If R15 was used as Rn, hence current PC+8. */
9815 u_regval[0] = u_regval[0] + 8;
9816 }
9817 /* Calculate target store address, Rn +/- Rm, register offset. */
9818 if (12 == arm_insn_r->opcode)
9819 {
9820 tgt_mem_addr = u_regval[0] + u_regval[1];
9821 }
9822 else
9823 {
9824 tgt_mem_addr = u_regval[1] - u_regval[0];
9825 }
9826 if (ARM_RECORD_STRH == str_type)
9827 {
9828 record_buf_mem[0] = 2;
9829 record_buf_mem[1] = tgt_mem_addr;
9830 arm_insn_r->mem_rec_count = 1;
9831 }
9832 else if (ARM_RECORD_STRD == str_type)
9833 {
9834 record_buf_mem[0] = 4;
9835 record_buf_mem[1] = tgt_mem_addr;
9836 record_buf_mem[2] = 4;
9837 record_buf_mem[3] = tgt_mem_addr + 4;
9838 arm_insn_r->mem_rec_count = 2;
9839 }
9840 }
9841 else if (11 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
9842 || 2 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
9843 {
9844 /* 3) Store, immediate pre-indexed. */
9845 /* 5) Store, immediate post-indexed. */
9846 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
9847 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
9848 offset_8 = (immed_high << 4) | immed_low;
9849 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
9850 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
9851 /* Calculate target store address, Rn +/- Rm, register offset. */
9852 if (15 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
9853 {
9854 tgt_mem_addr = u_regval[0] + offset_8;
9855 }
9856 else
9857 {
9858 tgt_mem_addr = u_regval[0] - offset_8;
9859 }
9860 if (ARM_RECORD_STRH == str_type)
9861 {
9862 record_buf_mem[0] = 2;
9863 record_buf_mem[1] = tgt_mem_addr;
9864 arm_insn_r->mem_rec_count = 1;
9865 }
9866 else if (ARM_RECORD_STRD == str_type)
9867 {
9868 record_buf_mem[0] = 4;
9869 record_buf_mem[1] = tgt_mem_addr;
9870 record_buf_mem[2] = 4;
9871 record_buf_mem[3] = tgt_mem_addr + 4;
9872 arm_insn_r->mem_rec_count = 2;
9873 }
9874 /* Record Rn also as it changes. */
9875 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
9876 arm_insn_r->reg_rec_count = 1;
9877 }
9878 else if (9 == arm_insn_r->opcode || 13 == arm_insn_r->opcode
9879 || 0 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
9880 {
9881 /* 4) Store, register pre-indexed. */
9882 /* 6) Store, register post -indexed. */
9883 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
9884 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
9885 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
9886 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
9887 /* Calculate target store address, Rn +/- Rm, register offset. */
9888 if (13 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
9889 {
9890 tgt_mem_addr = u_regval[0] + u_regval[1];
9891 }
9892 else
9893 {
9894 tgt_mem_addr = u_regval[1] - u_regval[0];
9895 }
9896 if (ARM_RECORD_STRH == str_type)
9897 {
9898 record_buf_mem[0] = 2;
9899 record_buf_mem[1] = tgt_mem_addr;
9900 arm_insn_r->mem_rec_count = 1;
9901 }
9902 else if (ARM_RECORD_STRD == str_type)
9903 {
9904 record_buf_mem[0] = 4;
9905 record_buf_mem[1] = tgt_mem_addr;
9906 record_buf_mem[2] = 4;
9907 record_buf_mem[3] = tgt_mem_addr + 4;
9908 arm_insn_r->mem_rec_count = 2;
9909 }
9910 /* Record Rn also as it changes. */
9911 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
9912 arm_insn_r->reg_rec_count = 1;
9913 }
9914 return 0;
9915}
9916
9917/* Handling ARM extension space insns. */
9918
9919static int
9920arm_record_extension_space (insn_decode_record *arm_insn_r)
9921{
df95a9cf 9922 int ret = 0; /* Return value: -1:record failure ; 0:success */
72508ac0
PO
9923 uint32_t opcode1 = 0, opcode2 = 0, insn_op1 = 0;
9924 uint32_t record_buf[8], record_buf_mem[8];
9925 uint32_t reg_src1 = 0;
72508ac0
PO
9926 struct regcache *reg_cache = arm_insn_r->regcache;
9927 ULONGEST u_regval = 0;
9928
9929 gdb_assert (!INSN_RECORDED(arm_insn_r));
9930 /* Handle unconditional insn extension space. */
9931
9932 opcode1 = bits (arm_insn_r->arm_insn, 20, 27);
9933 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
9934 if (arm_insn_r->cond)
9935 {
9936 /* PLD has no affect on architectural state, it just affects
9937 the caches. */
9938 if (5 == ((opcode1 & 0xE0) >> 5))
9939 {
9940 /* BLX(1) */
9941 record_buf[0] = ARM_PS_REGNUM;
9942 record_buf[1] = ARM_LR_REGNUM;
9943 arm_insn_r->reg_rec_count = 2;
9944 }
9945 /* STC2, LDC2, MCR2, MRC2, CDP2: <TBD>, co-processor insn. */
9946 }
9947
9948
9949 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
9950 if (3 == opcode1 && bit (arm_insn_r->arm_insn, 4))
9951 {
9952 ret = -1;
9953 /* Undefined instruction on ARM V5; need to handle if later
9954 versions define it. */
9955 }
9956
9957 opcode1 = bits (arm_insn_r->arm_insn, 24, 27);
9958 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
9959 insn_op1 = bits (arm_insn_r->arm_insn, 20, 23);
9960
9961 /* Handle arithmetic insn extension space. */
9962 if (!opcode1 && 9 == opcode2 && 1 != arm_insn_r->cond
9963 && !INSN_RECORDED(arm_insn_r))
9964 {
9965 /* Handle MLA(S) and MUL(S). */
b020ff80 9966 if (in_inclusive_range (insn_op1, 0U, 3U))
72508ac0
PO
9967 {
9968 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
9969 record_buf[1] = ARM_PS_REGNUM;
9970 arm_insn_r->reg_rec_count = 2;
9971 }
b020ff80 9972 else if (in_inclusive_range (insn_op1, 4U, 15U))
72508ac0
PO
9973 {
9974 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
9975 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
9976 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
9977 record_buf[2] = ARM_PS_REGNUM;
9978 arm_insn_r->reg_rec_count = 3;
9979 }
9980 }
9981
9982 opcode1 = bits (arm_insn_r->arm_insn, 26, 27);
9983 opcode2 = bits (arm_insn_r->arm_insn, 23, 24);
9984 insn_op1 = bits (arm_insn_r->arm_insn, 21, 22);
9985
9986 /* Handle control insn extension space. */
9987
9988 if (!opcode1 && 2 == opcode2 && !bit (arm_insn_r->arm_insn, 20)
9989 && 1 != arm_insn_r->cond && !INSN_RECORDED(arm_insn_r))
9990 {
9991 if (!bit (arm_insn_r->arm_insn,25))
9992 {
9993 if (!bits (arm_insn_r->arm_insn, 4, 7))
9994 {
9995 if ((0 == insn_op1) || (2 == insn_op1))
9996 {
9997 /* MRS. */
9998 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
9999 arm_insn_r->reg_rec_count = 1;
10000 }
10001 else if (1 == insn_op1)
10002 {
10003 /* CSPR is going to be changed. */
10004 record_buf[0] = ARM_PS_REGNUM;
10005 arm_insn_r->reg_rec_count = 1;
10006 }
10007 else if (3 == insn_op1)
10008 {
10009 /* SPSR is going to be changed. */
10010 /* We need to get SPSR value, which is yet to be done. */
72508ac0
PO
10011 return -1;
10012 }
10013 }
10014 else if (1 == bits (arm_insn_r->arm_insn, 4, 7))
10015 {
10016 if (1 == insn_op1)
10017 {
10018 /* BX. */
10019 record_buf[0] = ARM_PS_REGNUM;
10020 arm_insn_r->reg_rec_count = 1;
10021 }
10022 else if (3 == insn_op1)
10023 {
10024 /* CLZ. */
10025 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10026 arm_insn_r->reg_rec_count = 1;
10027 }
10028 }
10029 else if (3 == bits (arm_insn_r->arm_insn, 4, 7))
10030 {
10031 /* BLX. */
10032 record_buf[0] = ARM_PS_REGNUM;
10033 record_buf[1] = ARM_LR_REGNUM;
10034 arm_insn_r->reg_rec_count = 2;
10035 }
10036 else if (5 == bits (arm_insn_r->arm_insn, 4, 7))
10037 {
10038 /* QADD, QSUB, QDADD, QDSUB */
10039 record_buf[0] = ARM_PS_REGNUM;
10040 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
10041 arm_insn_r->reg_rec_count = 2;
10042 }
10043 else if (7 == bits (arm_insn_r->arm_insn, 4, 7))
10044 {
10045 /* BKPT. */
10046 record_buf[0] = ARM_PS_REGNUM;
10047 record_buf[1] = ARM_LR_REGNUM;
10048 arm_insn_r->reg_rec_count = 2;
10049
10050 /* Save SPSR also;how? */
72508ac0
PO
10051 return -1;
10052 }
10053 else if(8 == bits (arm_insn_r->arm_insn, 4, 7)
10054 || 10 == bits (arm_insn_r->arm_insn, 4, 7)
10055 || 12 == bits (arm_insn_r->arm_insn, 4, 7)
10056 || 14 == bits (arm_insn_r->arm_insn, 4, 7)
10057 )
10058 {
10059 if (0 == insn_op1 || 1 == insn_op1)
10060 {
10061 /* SMLA<x><y>, SMLAW<y>, SMULW<y>. */
10062 /* We dont do optimization for SMULW<y> where we
10063 need only Rd. */
10064 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10065 record_buf[1] = ARM_PS_REGNUM;
10066 arm_insn_r->reg_rec_count = 2;
10067 }
10068 else if (2 == insn_op1)
10069 {
10070 /* SMLAL<x><y>. */
10071 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10072 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
10073 arm_insn_r->reg_rec_count = 2;
10074 }
10075 else if (3 == insn_op1)
10076 {
10077 /* SMUL<x><y>. */
10078 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10079 arm_insn_r->reg_rec_count = 1;
10080 }
10081 }
10082 }
10083 else
10084 {
10085 /* MSR : immediate form. */
10086 if (1 == insn_op1)
10087 {
10088 /* CSPR is going to be changed. */
10089 record_buf[0] = ARM_PS_REGNUM;
10090 arm_insn_r->reg_rec_count = 1;
10091 }
10092 else if (3 == insn_op1)
10093 {
10094 /* SPSR is going to be changed. */
10095 /* we need to get SPSR value, which is yet to be done */
72508ac0
PO
10096 return -1;
10097 }
10098 }
10099 }
10100
10101 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
10102 opcode2 = bits (arm_insn_r->arm_insn, 20, 24);
10103 insn_op1 = bits (arm_insn_r->arm_insn, 5, 6);
10104
10105 /* Handle load/store insn extension space. */
10106
10107 if (!opcode1 && bit (arm_insn_r->arm_insn, 7)
10108 && bit (arm_insn_r->arm_insn, 4) && 1 != arm_insn_r->cond
10109 && !INSN_RECORDED(arm_insn_r))
10110 {
10111 /* SWP/SWPB. */
10112 if (0 == insn_op1)
10113 {
10114 /* These insn, changes register and memory as well. */
10115 /* SWP or SWPB insn. */
10116 /* Get memory address given by Rn. */
10117 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10118 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
10119 /* SWP insn ?, swaps word. */
10120 if (8 == arm_insn_r->opcode)
10121 {
10122 record_buf_mem[0] = 4;
10123 }
10124 else
10125 {
10126 /* SWPB insn, swaps only byte. */
10127 record_buf_mem[0] = 1;
10128 }
10129 record_buf_mem[1] = u_regval;
10130 arm_insn_r->mem_rec_count = 1;
10131 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10132 arm_insn_r->reg_rec_count = 1;
10133 }
10134 else if (1 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
10135 {
10136 /* STRH. */
10137 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
10138 ARM_RECORD_STRH);
10139 }
10140 else if (2 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
10141 {
10142 /* LDRD. */
10143 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10144 record_buf[1] = record_buf[0] + 1;
10145 arm_insn_r->reg_rec_count = 2;
10146 }
10147 else if (3 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
10148 {
10149 /* STRD. */
10150 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
10151 ARM_RECORD_STRD);
10152 }
10153 else if (bit (arm_insn_r->arm_insn, 20) && insn_op1 <= 3)
10154 {
10155 /* LDRH, LDRSB, LDRSH. */
10156 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10157 arm_insn_r->reg_rec_count = 1;
10158 }
10159
10160 }
10161
10162 opcode1 = bits (arm_insn_r->arm_insn, 23, 27);
10163 if (24 == opcode1 && bit (arm_insn_r->arm_insn, 21)
10164 && !INSN_RECORDED(arm_insn_r))
10165 {
10166 ret = -1;
10167 /* Handle coprocessor insn extension space. */
10168 }
10169
10170 /* To be done for ARMv5 and later; as of now we return -1. */
10171 if (-1 == ret)
ca92db2d 10172 return ret;
72508ac0
PO
10173
10174 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10175 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10176
10177 return ret;
10178}
10179
10180/* Handling opcode 000 insns. */
10181
10182static int
10183arm_record_data_proc_misc_ld_str (insn_decode_record *arm_insn_r)
10184{
10185 struct regcache *reg_cache = arm_insn_r->regcache;
10186 uint32_t record_buf[8], record_buf_mem[8];
10187 ULONGEST u_regval[2] = {0};
10188
8d49165d 10189 uint32_t reg_src1 = 0;
72508ac0
PO
10190 uint32_t opcode1 = 0;
10191
10192 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10193 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10194 opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
10195
2d9e6acb 10196 if (!((opcode1 & 0x19) == 0x10))
72508ac0 10197 {
2d9e6acb
YQ
10198 /* Data-processing (register) and Data-processing (register-shifted
10199 register */
10200 /* Out of 11 shifter operands mode, all the insn modifies destination
10201 register, which is specified by 13-16 decode. */
10202 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10203 record_buf[1] = ARM_PS_REGNUM;
10204 arm_insn_r->reg_rec_count = 2;
72508ac0 10205 }
2d9e6acb 10206 else if ((arm_insn_r->decode < 8) && ((opcode1 & 0x19) == 0x10))
72508ac0 10207 {
2d9e6acb
YQ
10208 /* Miscellaneous instructions */
10209
10210 if (3 == arm_insn_r->decode && 0x12 == opcode1
10211 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
10212 {
10213 /* Handle BLX, branch and link/exchange. */
10214 if (9 == arm_insn_r->opcode)
10215 {
10216 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm,
10217 and R14 stores the return address. */
10218 record_buf[0] = ARM_PS_REGNUM;
10219 record_buf[1] = ARM_LR_REGNUM;
10220 arm_insn_r->reg_rec_count = 2;
10221 }
10222 }
10223 else if (7 == arm_insn_r->decode && 0x12 == opcode1)
10224 {
10225 /* Handle enhanced software breakpoint insn, BKPT. */
10226 /* CPSR is changed to be executed in ARM state, disabling normal
10227 interrupts, entering abort mode. */
10228 /* According to high vector configuration PC is set. */
10229 /* user hit breakpoint and type reverse, in
10230 that case, we need to go back with previous CPSR and
10231 Program Counter. */
10232 record_buf[0] = ARM_PS_REGNUM;
10233 record_buf[1] = ARM_LR_REGNUM;
10234 arm_insn_r->reg_rec_count = 2;
10235
10236 /* Save SPSR also; how? */
10237 return -1;
10238 }
10239 else if (1 == arm_insn_r->decode && 0x12 == opcode1
10240 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
10241 {
10242 /* Handle BX, branch and link/exchange. */
10243 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm. */
10244 record_buf[0] = ARM_PS_REGNUM;
10245 arm_insn_r->reg_rec_count = 1;
10246 }
10247 else if (1 == arm_insn_r->decode && 0x16 == opcode1
10248 && sbo_sbz (arm_insn_r->arm_insn, 9, 4, 1)
10249 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1))
10250 {
10251 /* Count leading zeros: CLZ. */
10252 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10253 arm_insn_r->reg_rec_count = 1;
10254 }
10255 else if (!bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
10256 && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
10257 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1)
10258 && sbo_sbz (arm_insn_r->arm_insn, 1, 12, 0))
10259 {
10260 /* Handle MRS insn. */
10261 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10262 arm_insn_r->reg_rec_count = 1;
10263 }
72508ac0 10264 }
2d9e6acb 10265 else if (9 == arm_insn_r->decode && opcode1 < 0x10)
72508ac0 10266 {
2d9e6acb
YQ
10267 /* Multiply and multiply-accumulate */
10268
10269 /* Handle multiply instructions. */
10270 /* MLA, MUL, SMLAL, SMULL, UMLAL, UMULL. */
10271 if (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)
10272 {
10273 /* Handle MLA and MUL. */
10274 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
10275 record_buf[1] = ARM_PS_REGNUM;
10276 arm_insn_r->reg_rec_count = 2;
10277 }
10278 else if (4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
10279 {
10280 /* Handle SMLAL, SMULL, UMLAL, UMULL. */
10281 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
10282 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
10283 record_buf[2] = ARM_PS_REGNUM;
10284 arm_insn_r->reg_rec_count = 3;
10285 }
10286 }
10287 else if (9 == arm_insn_r->decode && opcode1 > 0x10)
10288 {
10289 /* Synchronization primitives */
10290
72508ac0
PO
10291 /* Handling SWP, SWPB. */
10292 /* These insn, changes register and memory as well. */
10293 /* SWP or SWPB insn. */
10294
10295 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10296 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10297 /* SWP insn ?, swaps word. */
10298 if (8 == arm_insn_r->opcode)
2d9e6acb
YQ
10299 {
10300 record_buf_mem[0] = 4;
10301 }
10302 else
10303 {
10304 /* SWPB insn, swaps only byte. */
10305 record_buf_mem[0] = 1;
10306 }
72508ac0
PO
10307 record_buf_mem[1] = u_regval[0];
10308 arm_insn_r->mem_rec_count = 1;
10309 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10310 arm_insn_r->reg_rec_count = 1;
10311 }
2d9e6acb
YQ
10312 else if (11 == arm_insn_r->decode || 13 == arm_insn_r->decode
10313 || 15 == arm_insn_r->decode)
72508ac0 10314 {
2d9e6acb
YQ
10315 if ((opcode1 & 0x12) == 2)
10316 {
10317 /* Extra load/store (unprivileged) */
10318 return -1;
10319 }
10320 else
10321 {
10322 /* Extra load/store */
10323 switch (bits (arm_insn_r->arm_insn, 5, 6))
10324 {
10325 case 1:
10326 if ((opcode1 & 0x05) == 0x0 || (opcode1 & 0x05) == 0x4)
10327 {
10328 /* STRH (register), STRH (immediate) */
10329 arm_record_strx (arm_insn_r, &record_buf[0],
10330 &record_buf_mem[0], ARM_RECORD_STRH);
10331 }
10332 else if ((opcode1 & 0x05) == 0x1)
10333 {
10334 /* LDRH (register) */
10335 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10336 arm_insn_r->reg_rec_count = 1;
72508ac0 10337
2d9e6acb
YQ
10338 if (bit (arm_insn_r->arm_insn, 21))
10339 {
10340 /* Write back to Rn. */
10341 record_buf[arm_insn_r->reg_rec_count++]
10342 = bits (arm_insn_r->arm_insn, 16, 19);
10343 }
10344 }
10345 else if ((opcode1 & 0x05) == 0x5)
10346 {
10347 /* LDRH (immediate), LDRH (literal) */
10348 int rn = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0 10349
2d9e6acb
YQ
10350 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10351 arm_insn_r->reg_rec_count = 1;
10352
10353 if (rn != 15)
10354 {
10355 /*LDRH (immediate) */
10356 if (bit (arm_insn_r->arm_insn, 21))
10357 {
10358 /* Write back to Rn. */
10359 record_buf[arm_insn_r->reg_rec_count++] = rn;
10360 }
10361 }
10362 }
10363 else
10364 return -1;
10365 break;
10366 case 2:
10367 if ((opcode1 & 0x05) == 0x0)
10368 {
10369 /* LDRD (register) */
10370 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10371 record_buf[1] = record_buf[0] + 1;
10372 arm_insn_r->reg_rec_count = 2;
10373
10374 if (bit (arm_insn_r->arm_insn, 21))
10375 {
10376 /* Write back to Rn. */
10377 record_buf[arm_insn_r->reg_rec_count++]
10378 = bits (arm_insn_r->arm_insn, 16, 19);
10379 }
10380 }
10381 else if ((opcode1 & 0x05) == 0x1)
10382 {
10383 /* LDRSB (register) */
10384 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10385 arm_insn_r->reg_rec_count = 1;
10386
10387 if (bit (arm_insn_r->arm_insn, 21))
10388 {
10389 /* Write back to Rn. */
10390 record_buf[arm_insn_r->reg_rec_count++]
10391 = bits (arm_insn_r->arm_insn, 16, 19);
10392 }
10393 }
10394 else if ((opcode1 & 0x05) == 0x4 || (opcode1 & 0x05) == 0x5)
10395 {
10396 /* LDRD (immediate), LDRD (literal), LDRSB (immediate),
10397 LDRSB (literal) */
10398 int rn = bits (arm_insn_r->arm_insn, 16, 19);
10399
10400 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10401 arm_insn_r->reg_rec_count = 1;
10402
10403 if (rn != 15)
10404 {
10405 /*LDRD (immediate), LDRSB (immediate) */
10406 if (bit (arm_insn_r->arm_insn, 21))
10407 {
10408 /* Write back to Rn. */
10409 record_buf[arm_insn_r->reg_rec_count++] = rn;
10410 }
10411 }
10412 }
10413 else
10414 return -1;
10415 break;
10416 case 3:
10417 if ((opcode1 & 0x05) == 0x0)
10418 {
10419 /* STRD (register) */
10420 arm_record_strx (arm_insn_r, &record_buf[0],
10421 &record_buf_mem[0], ARM_RECORD_STRD);
10422 }
10423 else if ((opcode1 & 0x05) == 0x1)
10424 {
10425 /* LDRSH (register) */
10426 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10427 arm_insn_r->reg_rec_count = 1;
10428
10429 if (bit (arm_insn_r->arm_insn, 21))
10430 {
10431 /* Write back to Rn. */
10432 record_buf[arm_insn_r->reg_rec_count++]
10433 = bits (arm_insn_r->arm_insn, 16, 19);
10434 }
10435 }
10436 else if ((opcode1 & 0x05) == 0x4)
10437 {
10438 /* STRD (immediate) */
10439 arm_record_strx (arm_insn_r, &record_buf[0],
10440 &record_buf_mem[0], ARM_RECORD_STRD);
10441 }
10442 else if ((opcode1 & 0x05) == 0x5)
10443 {
10444 /* LDRSH (immediate), LDRSH (literal) */
10445 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10446 arm_insn_r->reg_rec_count = 1;
10447
10448 if (bit (arm_insn_r->arm_insn, 21))
10449 {
10450 /* Write back to Rn. */
10451 record_buf[arm_insn_r->reg_rec_count++]
10452 = bits (arm_insn_r->arm_insn, 16, 19);
10453 }
10454 }
10455 else
10456 return -1;
10457 break;
10458 default:
10459 return -1;
10460 }
10461 }
72508ac0
PO
10462 }
10463 else
10464 {
10465 return -1;
10466 }
10467
10468 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10469 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10470 return 0;
10471}
10472
10473/* Handling opcode 001 insns. */
10474
10475static int
10476arm_record_data_proc_imm (insn_decode_record *arm_insn_r)
10477{
10478 uint32_t record_buf[8], record_buf_mem[8];
10479
10480 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10481 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10482
10483 if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
10484 && 2 == bits (arm_insn_r->arm_insn, 20, 21)
10485 && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
10486 )
10487 {
10488 /* Handle MSR insn. */
10489 if (9 == arm_insn_r->opcode)
10490 {
10491 /* CSPR is going to be changed. */
10492 record_buf[0] = ARM_PS_REGNUM;
10493 arm_insn_r->reg_rec_count = 1;
10494 }
10495 else
10496 {
10497 /* SPSR is going to be changed. */
10498 }
10499 }
10500 else if (arm_insn_r->opcode <= 15)
10501 {
10502 /* Normal data processing insns. */
10503 /* Out of 11 shifter operands mode, all the insn modifies destination
10504 register, which is specified by 13-16 decode. */
10505 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10506 record_buf[1] = ARM_PS_REGNUM;
10507 arm_insn_r->reg_rec_count = 2;
10508 }
10509 else
10510 {
10511 return -1;
10512 }
10513
10514 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10515 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10516 return 0;
10517}
10518
c55978a6
YQ
10519static int
10520arm_record_media (insn_decode_record *arm_insn_r)
10521{
10522 uint32_t record_buf[8];
10523
10524 switch (bits (arm_insn_r->arm_insn, 22, 24))
10525 {
10526 case 0:
10527 /* Parallel addition and subtraction, signed */
10528 case 1:
10529 /* Parallel addition and subtraction, unsigned */
10530 case 2:
10531 case 3:
10532 /* Packing, unpacking, saturation and reversal */
10533 {
10534 int rd = bits (arm_insn_r->arm_insn, 12, 15);
10535
10536 record_buf[arm_insn_r->reg_rec_count++] = rd;
10537 }
10538 break;
10539
10540 case 4:
10541 case 5:
10542 /* Signed multiplies */
10543 {
10544 int rd = bits (arm_insn_r->arm_insn, 16, 19);
10545 unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 22);
10546
10547 record_buf[arm_insn_r->reg_rec_count++] = rd;
10548 if (op1 == 0x0)
10549 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
10550 else if (op1 == 0x4)
10551 record_buf[arm_insn_r->reg_rec_count++]
10552 = bits (arm_insn_r->arm_insn, 12, 15);
10553 }
10554 break;
10555
10556 case 6:
10557 {
10558 if (bit (arm_insn_r->arm_insn, 21)
10559 && bits (arm_insn_r->arm_insn, 5, 6) == 0x2)
10560 {
10561 /* SBFX */
10562 record_buf[arm_insn_r->reg_rec_count++]
10563 = bits (arm_insn_r->arm_insn, 12, 15);
10564 }
10565 else if (bits (arm_insn_r->arm_insn, 20, 21) == 0x0
10566 && bits (arm_insn_r->arm_insn, 5, 7) == 0x0)
10567 {
10568 /* USAD8 and USADA8 */
10569 record_buf[arm_insn_r->reg_rec_count++]
10570 = bits (arm_insn_r->arm_insn, 16, 19);
10571 }
10572 }
10573 break;
10574
10575 case 7:
10576 {
10577 if (bits (arm_insn_r->arm_insn, 20, 21) == 0x3
10578 && bits (arm_insn_r->arm_insn, 5, 7) == 0x7)
10579 {
10580 /* Permanently UNDEFINED */
10581 return -1;
10582 }
10583 else
10584 {
10585 /* BFC, BFI and UBFX */
10586 record_buf[arm_insn_r->reg_rec_count++]
10587 = bits (arm_insn_r->arm_insn, 12, 15);
10588 }
10589 }
10590 break;
10591
10592 default:
10593 return -1;
10594 }
10595
10596 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10597
10598 return 0;
10599}
10600
71e396f9 10601/* Handle ARM mode instructions with opcode 010. */
72508ac0
PO
10602
10603static int
10604arm_record_ld_st_imm_offset (insn_decode_record *arm_insn_r)
10605{
10606 struct regcache *reg_cache = arm_insn_r->regcache;
10607
71e396f9
LM
10608 uint32_t reg_base , reg_dest;
10609 uint32_t offset_12, tgt_mem_addr;
72508ac0 10610 uint32_t record_buf[8], record_buf_mem[8];
71e396f9
LM
10611 unsigned char wback;
10612 ULONGEST u_regval;
72508ac0 10613
71e396f9
LM
10614 /* Calculate wback. */
10615 wback = (bit (arm_insn_r->arm_insn, 24) == 0)
10616 || (bit (arm_insn_r->arm_insn, 21) == 1);
72508ac0 10617
71e396f9
LM
10618 arm_insn_r->reg_rec_count = 0;
10619 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0
PO
10620
10621 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
10622 {
71e396f9
LM
10623 /* LDR (immediate), LDR (literal), LDRB (immediate), LDRB (literal), LDRBT
10624 and LDRT. */
10625
72508ac0 10626 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
71e396f9
LM
10627 record_buf[arm_insn_r->reg_rec_count++] = reg_dest;
10628
10629 /* The LDR instruction is capable of doing branching. If MOV LR, PC
10630 preceeds a LDR instruction having R15 as reg_base, it
10631 emulates a branch and link instruction, and hence we need to save
10632 CPSR and PC as well. */
10633 if (ARM_PC_REGNUM == reg_dest)
10634 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
10635
10636 /* If wback is true, also save the base register, which is going to be
10637 written to. */
10638 if (wback)
10639 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
10640 }
10641 else
10642 {
71e396f9
LM
10643 /* STR (immediate), STRB (immediate), STRBT and STRT. */
10644
72508ac0 10645 offset_12 = bits (arm_insn_r->arm_insn, 0, 11);
71e396f9
LM
10646 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
10647
10648 /* Handle bit U. */
72508ac0 10649 if (bit (arm_insn_r->arm_insn, 23))
71e396f9
LM
10650 {
10651 /* U == 1: Add the offset. */
10652 tgt_mem_addr = (uint32_t) u_regval + offset_12;
10653 }
72508ac0 10654 else
71e396f9
LM
10655 {
10656 /* U == 0: subtract the offset. */
10657 tgt_mem_addr = (uint32_t) u_regval - offset_12;
10658 }
10659
10660 /* Bit 22 tells us whether the store instruction writes 1 byte or 4
10661 bytes. */
10662 if (bit (arm_insn_r->arm_insn, 22))
10663 {
10664 /* STRB and STRBT: 1 byte. */
10665 record_buf_mem[0] = 1;
10666 }
10667 else
10668 {
10669 /* STR and STRT: 4 bytes. */
10670 record_buf_mem[0] = 4;
10671 }
10672
10673 /* Handle bit P. */
10674 if (bit (arm_insn_r->arm_insn, 24))
10675 record_buf_mem[1] = tgt_mem_addr;
10676 else
10677 record_buf_mem[1] = (uint32_t) u_regval;
72508ac0 10678
72508ac0
PO
10679 arm_insn_r->mem_rec_count = 1;
10680
71e396f9
LM
10681 /* If wback is true, also save the base register, which is going to be
10682 written to. */
10683 if (wback)
10684 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
10685 }
10686
10687 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10688 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10689 return 0;
10690}
10691
10692/* Handling opcode 011 insns. */
10693
10694static int
10695arm_record_ld_st_reg_offset (insn_decode_record *arm_insn_r)
10696{
10697 struct regcache *reg_cache = arm_insn_r->regcache;
10698
10699 uint32_t shift_imm = 0;
10700 uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
10701 uint32_t offset_12 = 0, tgt_mem_addr = 0;
10702 uint32_t record_buf[8], record_buf_mem[8];
10703
10704 LONGEST s_word;
10705 ULONGEST u_regval[2];
10706
c55978a6
YQ
10707 if (bit (arm_insn_r->arm_insn, 4))
10708 return arm_record_media (arm_insn_r);
10709
72508ac0
PO
10710 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10711 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10712
10713 /* Handle enhanced store insns and LDRD DSP insn,
10714 order begins according to addressing modes for store insns
10715 STRH insn. */
10716
10717 /* LDR or STR? */
10718 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
10719 {
10720 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
10721 /* LDR insn has a capability to do branching, if
10722 MOV LR, PC is precedded by LDR insn having Rn as R15
10723 in that case, it emulates branch and link insn, and hence we
10724 need to save CSPR and PC as well. */
10725 if (15 != reg_dest)
10726 {
10727 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10728 arm_insn_r->reg_rec_count = 1;
10729 }
10730 else
10731 {
10732 record_buf[0] = reg_dest;
10733 record_buf[1] = ARM_PS_REGNUM;
10734 arm_insn_r->reg_rec_count = 2;
10735 }
10736 }
10737 else
10738 {
10739 if (! bits (arm_insn_r->arm_insn, 4, 11))
10740 {
10741 /* Store insn, register offset and register pre-indexed,
10742 register post-indexed. */
10743 /* Get Rm. */
10744 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10745 /* Get Rn. */
10746 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10747 regcache_raw_read_unsigned (reg_cache, reg_src1
10748 , &u_regval[0]);
10749 regcache_raw_read_unsigned (reg_cache, reg_src2
10750 , &u_regval[1]);
10751 if (15 == reg_src2)
10752 {
10753 /* If R15 was used as Rn, hence current PC+8. */
10754 /* Pre-indexed mode doesnt reach here ; illegal insn. */
10755 u_regval[0] = u_regval[0] + 8;
10756 }
10757 /* Calculate target store address, Rn +/- Rm, register offset. */
10758 /* U == 1. */
10759 if (bit (arm_insn_r->arm_insn, 23))
10760 {
10761 tgt_mem_addr = u_regval[0] + u_regval[1];
10762 }
10763 else
10764 {
10765 tgt_mem_addr = u_regval[1] - u_regval[0];
10766 }
10767
10768 switch (arm_insn_r->opcode)
10769 {
10770 /* STR. */
10771 case 8:
10772 case 12:
10773 /* STR. */
10774 case 9:
10775 case 13:
10776 /* STRT. */
10777 case 1:
10778 case 5:
10779 /* STR. */
10780 case 0:
10781 case 4:
10782 record_buf_mem[0] = 4;
10783 break;
10784
10785 /* STRB. */
10786 case 10:
10787 case 14:
10788 /* STRB. */
10789 case 11:
10790 case 15:
10791 /* STRBT. */
10792 case 3:
10793 case 7:
10794 /* STRB. */
10795 case 2:
10796 case 6:
10797 record_buf_mem[0] = 1;
10798 break;
10799
10800 default:
10801 gdb_assert_not_reached ("no decoding pattern found");
10802 break;
10803 }
10804 record_buf_mem[1] = tgt_mem_addr;
10805 arm_insn_r->mem_rec_count = 1;
10806
10807 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
10808 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
10809 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
10810 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
10811 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
10812 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
10813 )
10814 {
10815 /* Rn is going to be changed in pre-indexed mode and
10816 post-indexed mode as well. */
10817 record_buf[0] = reg_src2;
10818 arm_insn_r->reg_rec_count = 1;
10819 }
10820 }
10821 else
10822 {
10823 /* Store insn, scaled register offset; scaled pre-indexed. */
10824 offset_12 = bits (arm_insn_r->arm_insn, 5, 6);
10825 /* Get Rm. */
10826 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10827 /* Get Rn. */
10828 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10829 /* Get shift_imm. */
10830 shift_imm = bits (arm_insn_r->arm_insn, 7, 11);
10831 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10832 regcache_raw_read_signed (reg_cache, reg_src1, &s_word);
10833 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10834 /* Offset_12 used as shift. */
10835 switch (offset_12)
10836 {
10837 case 0:
10838 /* Offset_12 used as index. */
10839 offset_12 = u_regval[0] << shift_imm;
10840 break;
10841
10842 case 1:
10843 offset_12 = (!shift_imm)?0:u_regval[0] >> shift_imm;
10844 break;
10845
10846 case 2:
10847 if (!shift_imm)
10848 {
10849 if (bit (u_regval[0], 31))
10850 {
10851 offset_12 = 0xFFFFFFFF;
10852 }
10853 else
10854 {
10855 offset_12 = 0;
10856 }
10857 }
10858 else
10859 {
10860 /* This is arithmetic shift. */
10861 offset_12 = s_word >> shift_imm;
10862 }
10863 break;
10864
10865 case 3:
10866 if (!shift_imm)
10867 {
10868 regcache_raw_read_unsigned (reg_cache, ARM_PS_REGNUM,
10869 &u_regval[1]);
10870 /* Get C flag value and shift it by 31. */
10871 offset_12 = (((bit (u_regval[1], 29)) << 31) \
10872 | (u_regval[0]) >> 1);
10873 }
10874 else
10875 {
10876 offset_12 = (u_regval[0] >> shift_imm) \
10877 | (u_regval[0] <<
10878 (sizeof(uint32_t) - shift_imm));
10879 }
10880 break;
10881
10882 default:
10883 gdb_assert_not_reached ("no decoding pattern found");
10884 break;
10885 }
10886
10887 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10888 /* bit U set. */
10889 if (bit (arm_insn_r->arm_insn, 23))
10890 {
10891 tgt_mem_addr = u_regval[1] + offset_12;
10892 }
10893 else
10894 {
10895 tgt_mem_addr = u_regval[1] - offset_12;
10896 }
10897
10898 switch (arm_insn_r->opcode)
10899 {
10900 /* STR. */
10901 case 8:
10902 case 12:
10903 /* STR. */
10904 case 9:
10905 case 13:
10906 /* STRT. */
10907 case 1:
10908 case 5:
10909 /* STR. */
10910 case 0:
10911 case 4:
10912 record_buf_mem[0] = 4;
10913 break;
10914
10915 /* STRB. */
10916 case 10:
10917 case 14:
10918 /* STRB. */
10919 case 11:
10920 case 15:
10921 /* STRBT. */
10922 case 3:
10923 case 7:
10924 /* STRB. */
10925 case 2:
10926 case 6:
10927 record_buf_mem[0] = 1;
10928 break;
10929
10930 default:
10931 gdb_assert_not_reached ("no decoding pattern found");
10932 break;
10933 }
10934 record_buf_mem[1] = tgt_mem_addr;
10935 arm_insn_r->mem_rec_count = 1;
10936
10937 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
10938 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
10939 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
10940 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
10941 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
10942 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
10943 )
10944 {
10945 /* Rn is going to be changed in register scaled pre-indexed
10946 mode,and scaled post indexed mode. */
10947 record_buf[0] = reg_src2;
10948 arm_insn_r->reg_rec_count = 1;
10949 }
10950 }
10951 }
10952
10953 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10954 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10955 return 0;
10956}
10957
71e396f9 10958/* Handle ARM mode instructions with opcode 100. */
72508ac0
PO
10959
10960static int
10961arm_record_ld_st_multiple (insn_decode_record *arm_insn_r)
10962{
10963 struct regcache *reg_cache = arm_insn_r->regcache;
71e396f9
LM
10964 uint32_t register_count = 0, register_bits;
10965 uint32_t reg_base, addr_mode;
72508ac0 10966 uint32_t record_buf[24], record_buf_mem[48];
71e396f9
LM
10967 uint32_t wback;
10968 ULONGEST u_regval;
72508ac0 10969
71e396f9
LM
10970 /* Fetch the list of registers. */
10971 register_bits = bits (arm_insn_r->arm_insn, 0, 15);
10972 arm_insn_r->reg_rec_count = 0;
10973
10974 /* Fetch the base register that contains the address we are loading data
10975 to. */
10976 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0 10977
71e396f9
LM
10978 /* Calculate wback. */
10979 wback = (bit (arm_insn_r->arm_insn, 21) == 1);
72508ac0
PO
10980
10981 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
10982 {
71e396f9 10983 /* LDM/LDMIA/LDMFD, LDMDA/LDMFA, LDMDB and LDMIB. */
72508ac0 10984
71e396f9 10985 /* Find out which registers are going to be loaded from memory. */
72508ac0 10986 while (register_bits)
71e396f9
LM
10987 {
10988 if (register_bits & 0x00000001)
10989 record_buf[arm_insn_r->reg_rec_count++] = register_count;
10990 register_bits = register_bits >> 1;
10991 register_count++;
10992 }
72508ac0 10993
71e396f9
LM
10994
10995 /* If wback is true, also save the base register, which is going to be
10996 written to. */
10997 if (wback)
10998 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
10999
11000 /* Save the CPSR register. */
11001 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
72508ac0
PO
11002 }
11003 else
11004 {
71e396f9 11005 /* STM (STMIA, STMEA), STMDA (STMED), STMDB (STMFD) and STMIB (STMFA). */
72508ac0 11006
71e396f9
LM
11007 addr_mode = bits (arm_insn_r->arm_insn, 23, 24);
11008
11009 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
11010
11011 /* Find out how many registers are going to be stored to memory. */
72508ac0 11012 while (register_bits)
71e396f9
LM
11013 {
11014 if (register_bits & 0x00000001)
11015 register_count++;
11016 register_bits = register_bits >> 1;
11017 }
72508ac0
PO
11018
11019 switch (addr_mode)
71e396f9
LM
11020 {
11021 /* STMDA (STMED): Decrement after. */
11022 case 0:
11023 record_buf_mem[1] = (uint32_t) u_regval
11024 - register_count * INT_REGISTER_SIZE + 4;
11025 break;
11026 /* STM (STMIA, STMEA): Increment after. */
11027 case 1:
11028 record_buf_mem[1] = (uint32_t) u_regval;
11029 break;
11030 /* STMDB (STMFD): Decrement before. */
11031 case 2:
11032 record_buf_mem[1] = (uint32_t) u_regval
11033 - register_count * INT_REGISTER_SIZE;
11034 break;
11035 /* STMIB (STMFA): Increment before. */
11036 case 3:
11037 record_buf_mem[1] = (uint32_t) u_regval + INT_REGISTER_SIZE;
11038 break;
11039 default:
11040 gdb_assert_not_reached ("no decoding pattern found");
11041 break;
11042 }
72508ac0 11043
71e396f9
LM
11044 record_buf_mem[0] = register_count * INT_REGISTER_SIZE;
11045 arm_insn_r->mem_rec_count = 1;
11046
11047 /* If wback is true, also save the base register, which is going to be
11048 written to. */
11049 if (wback)
11050 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
11051 }
11052
11053 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11054 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11055 return 0;
11056}
11057
11058/* Handling opcode 101 insns. */
11059
11060static int
11061arm_record_b_bl (insn_decode_record *arm_insn_r)
11062{
11063 uint32_t record_buf[8];
11064
11065 /* Handle B, BL, BLX(1) insns. */
11066 /* B simply branches so we do nothing here. */
11067 /* Note: BLX(1) doesnt fall here but instead it falls into
11068 extension space. */
11069 if (bit (arm_insn_r->arm_insn, 24))
11070 {
11071 record_buf[0] = ARM_LR_REGNUM;
11072 arm_insn_r->reg_rec_count = 1;
11073 }
11074
11075 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11076
11077 return 0;
11078}
11079
72508ac0 11080static int
c6ec2b30 11081arm_record_unsupported_insn (insn_decode_record *arm_insn_r)
72508ac0
PO
11082{
11083 printf_unfiltered (_("Process record does not support instruction "
01e57735
YQ
11084 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
11085 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
72508ac0
PO
11086
11087 return -1;
11088}
11089
5a578da5
OJ
11090/* Record handler for vector data transfer instructions. */
11091
11092static int
11093arm_record_vdata_transfer_insn (insn_decode_record *arm_insn_r)
11094{
11095 uint32_t bits_a, bit_c, bit_l, reg_t, reg_v;
11096 uint32_t record_buf[4];
11097
5a578da5
OJ
11098 reg_t = bits (arm_insn_r->arm_insn, 12, 15);
11099 reg_v = bits (arm_insn_r->arm_insn, 21, 23);
11100 bits_a = bits (arm_insn_r->arm_insn, 21, 23);
11101 bit_l = bit (arm_insn_r->arm_insn, 20);
11102 bit_c = bit (arm_insn_r->arm_insn, 8);
11103
11104 /* Handle VMOV instruction. */
11105 if (bit_l && bit_c)
11106 {
11107 record_buf[0] = reg_t;
11108 arm_insn_r->reg_rec_count = 1;
11109 }
11110 else if (bit_l && !bit_c)
11111 {
11112 /* Handle VMOV instruction. */
11113 if (bits_a == 0x00)
11114 {
f1771dce 11115 record_buf[0] = reg_t;
5a578da5
OJ
11116 arm_insn_r->reg_rec_count = 1;
11117 }
11118 /* Handle VMRS instruction. */
11119 else if (bits_a == 0x07)
11120 {
11121 if (reg_t == 15)
11122 reg_t = ARM_PS_REGNUM;
11123
11124 record_buf[0] = reg_t;
11125 arm_insn_r->reg_rec_count = 1;
11126 }
11127 }
11128 else if (!bit_l && !bit_c)
11129 {
11130 /* Handle VMOV instruction. */
11131 if (bits_a == 0x00)
11132 {
f1771dce 11133 record_buf[0] = ARM_D0_REGNUM + reg_v;
5a578da5
OJ
11134
11135 arm_insn_r->reg_rec_count = 1;
11136 }
11137 /* Handle VMSR instruction. */
11138 else if (bits_a == 0x07)
11139 {
11140 record_buf[0] = ARM_FPSCR_REGNUM;
11141 arm_insn_r->reg_rec_count = 1;
11142 }
11143 }
11144 else if (!bit_l && bit_c)
11145 {
11146 /* Handle VMOV instruction. */
11147 if (!(bits_a & 0x04))
11148 {
11149 record_buf[0] = (reg_v | (bit (arm_insn_r->arm_insn, 7) << 4))
11150 + ARM_D0_REGNUM;
11151 arm_insn_r->reg_rec_count = 1;
11152 }
11153 /* Handle VDUP instruction. */
11154 else
11155 {
11156 if (bit (arm_insn_r->arm_insn, 21))
11157 {
11158 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
11159 record_buf[0] = reg_v + ARM_D0_REGNUM;
11160 record_buf[1] = reg_v + ARM_D0_REGNUM + 1;
11161 arm_insn_r->reg_rec_count = 2;
11162 }
11163 else
11164 {
11165 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
11166 record_buf[0] = reg_v + ARM_D0_REGNUM;
11167 arm_insn_r->reg_rec_count = 1;
11168 }
11169 }
11170 }
11171
11172 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11173 return 0;
11174}
11175
f20f80dd
OJ
11176/* Record handler for extension register load/store instructions. */
11177
11178static int
11179arm_record_exreg_ld_st_insn (insn_decode_record *arm_insn_r)
11180{
11181 uint32_t opcode, single_reg;
11182 uint8_t op_vldm_vstm;
11183 uint32_t record_buf[8], record_buf_mem[128];
11184 ULONGEST u_regval = 0;
11185
11186 struct regcache *reg_cache = arm_insn_r->regcache;
f20f80dd
OJ
11187
11188 opcode = bits (arm_insn_r->arm_insn, 20, 24);
9fde51ed 11189 single_reg = !bit (arm_insn_r->arm_insn, 8);
f20f80dd
OJ
11190 op_vldm_vstm = opcode & 0x1b;
11191
11192 /* Handle VMOV instructions. */
11193 if ((opcode & 0x1e) == 0x04)
11194 {
9fde51ed 11195 if (bit (arm_insn_r->arm_insn, 20)) /* to_arm_registers bit 20? */
01e57735
YQ
11196 {
11197 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11198 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
11199 arm_insn_r->reg_rec_count = 2;
11200 }
f20f80dd 11201 else
01e57735 11202 {
9fde51ed
YQ
11203 uint8_t reg_m = bits (arm_insn_r->arm_insn, 0, 3);
11204 uint8_t bit_m = bit (arm_insn_r->arm_insn, 5);
f20f80dd 11205
9fde51ed 11206 if (single_reg)
01e57735 11207 {
9fde51ed
YQ
11208 /* The first S register number m is REG_M:M (M is bit 5),
11209 the corresponding D register number is REG_M:M / 2, which
11210 is REG_M. */
11211 record_buf[arm_insn_r->reg_rec_count++] = ARM_D0_REGNUM + reg_m;
11212 /* The second S register number is REG_M:M + 1, the
11213 corresponding D register number is (REG_M:M + 1) / 2.
11214 IOW, if bit M is 1, the first and second S registers
11215 are mapped to different D registers, otherwise, they are
11216 in the same D register. */
11217 if (bit_m)
11218 {
11219 record_buf[arm_insn_r->reg_rec_count++]
11220 = ARM_D0_REGNUM + reg_m + 1;
11221 }
01e57735
YQ
11222 }
11223 else
11224 {
9fde51ed 11225 record_buf[0] = ((bit_m << 4) + reg_m + ARM_D0_REGNUM);
01e57735
YQ
11226 arm_insn_r->reg_rec_count = 1;
11227 }
11228 }
f20f80dd
OJ
11229 }
11230 /* Handle VSTM and VPUSH instructions. */
11231 else if (op_vldm_vstm == 0x08 || op_vldm_vstm == 0x0a
01e57735 11232 || op_vldm_vstm == 0x12)
f20f80dd
OJ
11233 {
11234 uint32_t start_address, reg_rn, imm_off32, imm_off8, memory_count;
11235 uint32_t memory_index = 0;
11236
11237 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
11238 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
11239 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
9fde51ed 11240 imm_off32 = imm_off8 << 2;
f20f80dd
OJ
11241 memory_count = imm_off8;
11242
11243 if (bit (arm_insn_r->arm_insn, 23))
01e57735 11244 start_address = u_regval;
f20f80dd 11245 else
01e57735 11246 start_address = u_regval - imm_off32;
f20f80dd
OJ
11247
11248 if (bit (arm_insn_r->arm_insn, 21))
01e57735
YQ
11249 {
11250 record_buf[0] = reg_rn;
11251 arm_insn_r->reg_rec_count = 1;
11252 }
f20f80dd
OJ
11253
11254 while (memory_count > 0)
01e57735 11255 {
9fde51ed 11256 if (single_reg)
01e57735 11257 {
9fde51ed
YQ
11258 record_buf_mem[memory_index] = 4;
11259 record_buf_mem[memory_index + 1] = start_address;
01e57735
YQ
11260 start_address = start_address + 4;
11261 memory_index = memory_index + 2;
11262 }
11263 else
11264 {
9fde51ed
YQ
11265 record_buf_mem[memory_index] = 4;
11266 record_buf_mem[memory_index + 1] = start_address;
11267 record_buf_mem[memory_index + 2] = 4;
11268 record_buf_mem[memory_index + 3] = start_address + 4;
01e57735
YQ
11269 start_address = start_address + 8;
11270 memory_index = memory_index + 4;
11271 }
11272 memory_count--;
11273 }
f20f80dd
OJ
11274 arm_insn_r->mem_rec_count = (memory_index >> 1);
11275 }
11276 /* Handle VLDM instructions. */
11277 else if (op_vldm_vstm == 0x09 || op_vldm_vstm == 0x0b
01e57735 11278 || op_vldm_vstm == 0x13)
f20f80dd
OJ
11279 {
11280 uint32_t reg_count, reg_vd;
11281 uint32_t reg_index = 0;
9fde51ed 11282 uint32_t bit_d = bit (arm_insn_r->arm_insn, 22);
f20f80dd
OJ
11283
11284 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
11285 reg_count = bits (arm_insn_r->arm_insn, 0, 7);
11286
9fde51ed
YQ
11287 /* REG_VD is the first D register number. If the instruction
11288 loads memory to S registers (SINGLE_REG is TRUE), the register
11289 number is (REG_VD << 1 | bit D), so the corresponding D
11290 register number is (REG_VD << 1 | bit D) / 2 = REG_VD. */
11291 if (!single_reg)
11292 reg_vd = reg_vd | (bit_d << 4);
f20f80dd 11293
9fde51ed 11294 if (bit (arm_insn_r->arm_insn, 21) /* write back */)
01e57735 11295 record_buf[reg_index++] = bits (arm_insn_r->arm_insn, 16, 19);
f20f80dd 11296
9fde51ed
YQ
11297 /* If the instruction loads memory to D register, REG_COUNT should
11298 be divided by 2, according to the ARM Architecture Reference
11299 Manual. If the instruction loads memory to S register, divide by
11300 2 as well because two S registers are mapped to D register. */
11301 reg_count = reg_count / 2;
11302 if (single_reg && bit_d)
01e57735 11303 {
9fde51ed
YQ
11304 /* Increase the register count if S register list starts from
11305 an odd number (bit d is one). */
11306 reg_count++;
11307 }
f20f80dd 11308
9fde51ed
YQ
11309 while (reg_count > 0)
11310 {
11311 record_buf[reg_index++] = ARM_D0_REGNUM + reg_vd + reg_count - 1;
01e57735
YQ
11312 reg_count--;
11313 }
f20f80dd
OJ
11314 arm_insn_r->reg_rec_count = reg_index;
11315 }
11316 /* VSTR Vector store register. */
11317 else if ((opcode & 0x13) == 0x10)
11318 {
bec2ab5a 11319 uint32_t start_address, reg_rn, imm_off32, imm_off8;
f20f80dd
OJ
11320 uint32_t memory_index = 0;
11321
11322 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
11323 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
11324 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
9fde51ed 11325 imm_off32 = imm_off8 << 2;
f20f80dd
OJ
11326
11327 if (bit (arm_insn_r->arm_insn, 23))
01e57735 11328 start_address = u_regval + imm_off32;
f20f80dd 11329 else
01e57735 11330 start_address = u_regval - imm_off32;
f20f80dd
OJ
11331
11332 if (single_reg)
01e57735 11333 {
9fde51ed
YQ
11334 record_buf_mem[memory_index] = 4;
11335 record_buf_mem[memory_index + 1] = start_address;
01e57735
YQ
11336 arm_insn_r->mem_rec_count = 1;
11337 }
f20f80dd 11338 else
01e57735 11339 {
9fde51ed
YQ
11340 record_buf_mem[memory_index] = 4;
11341 record_buf_mem[memory_index + 1] = start_address;
11342 record_buf_mem[memory_index + 2] = 4;
11343 record_buf_mem[memory_index + 3] = start_address + 4;
01e57735
YQ
11344 arm_insn_r->mem_rec_count = 2;
11345 }
f20f80dd
OJ
11346 }
11347 /* VLDR Vector load register. */
11348 else if ((opcode & 0x13) == 0x11)
11349 {
11350 uint32_t reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
11351
11352 if (!single_reg)
01e57735
YQ
11353 {
11354 reg_vd = reg_vd | (bit (arm_insn_r->arm_insn, 22) << 4);
11355 record_buf[0] = ARM_D0_REGNUM + reg_vd;
11356 }
f20f80dd 11357 else
01e57735
YQ
11358 {
11359 reg_vd = (reg_vd << 1) | bit (arm_insn_r->arm_insn, 22);
9fde51ed
YQ
11360 /* Record register D rather than pseudo register S. */
11361 record_buf[0] = ARM_D0_REGNUM + reg_vd / 2;
01e57735 11362 }
f20f80dd
OJ
11363 arm_insn_r->reg_rec_count = 1;
11364 }
11365
11366 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11367 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11368 return 0;
11369}
11370
851f26ae
OJ
11371/* Record handler for arm/thumb mode VFP data processing instructions. */
11372
11373static int
11374arm_record_vfp_data_proc_insn (insn_decode_record *arm_insn_r)
11375{
11376 uint32_t opc1, opc2, opc3, dp_op_sz, bit_d, reg_vd;
11377 uint32_t record_buf[4];
11378 enum insn_types {INSN_T0, INSN_T1, INSN_T2, INSN_T3, INSN_INV};
11379 enum insn_types curr_insn_type = INSN_INV;
11380
11381 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
11382 opc1 = bits (arm_insn_r->arm_insn, 20, 23);
11383 opc2 = bits (arm_insn_r->arm_insn, 16, 19);
11384 opc3 = bits (arm_insn_r->arm_insn, 6, 7);
11385 dp_op_sz = bit (arm_insn_r->arm_insn, 8);
11386 bit_d = bit (arm_insn_r->arm_insn, 22);
ce887586
TT
11387 /* Mask off the "D" bit. */
11388 opc1 = opc1 & ~0x04;
851f26ae
OJ
11389
11390 /* Handle VMLA, VMLS. */
11391 if (opc1 == 0x00)
11392 {
11393 if (bit (arm_insn_r->arm_insn, 10))
11394 {
11395 if (bit (arm_insn_r->arm_insn, 6))
11396 curr_insn_type = INSN_T0;
11397 else
11398 curr_insn_type = INSN_T1;
11399 }
11400 else
11401 {
11402 if (dp_op_sz)
11403 curr_insn_type = INSN_T1;
11404 else
11405 curr_insn_type = INSN_T2;
11406 }
11407 }
11408 /* Handle VNMLA, VNMLS, VNMUL. */
11409 else if (opc1 == 0x01)
11410 {
11411 if (dp_op_sz)
11412 curr_insn_type = INSN_T1;
11413 else
11414 curr_insn_type = INSN_T2;
11415 }
11416 /* Handle VMUL. */
11417 else if (opc1 == 0x02 && !(opc3 & 0x01))
11418 {
11419 if (bit (arm_insn_r->arm_insn, 10))
11420 {
11421 if (bit (arm_insn_r->arm_insn, 6))
11422 curr_insn_type = INSN_T0;
11423 else
11424 curr_insn_type = INSN_T1;
11425 }
11426 else
11427 {
11428 if (dp_op_sz)
11429 curr_insn_type = INSN_T1;
11430 else
11431 curr_insn_type = INSN_T2;
11432 }
11433 }
11434 /* Handle VADD, VSUB. */
11435 else if (opc1 == 0x03)
11436 {
11437 if (!bit (arm_insn_r->arm_insn, 9))
11438 {
11439 if (bit (arm_insn_r->arm_insn, 6))
11440 curr_insn_type = INSN_T0;
11441 else
11442 curr_insn_type = INSN_T1;
11443 }
11444 else
11445 {
11446 if (dp_op_sz)
11447 curr_insn_type = INSN_T1;
11448 else
11449 curr_insn_type = INSN_T2;
11450 }
11451 }
11452 /* Handle VDIV. */
ce887586 11453 else if (opc1 == 0x08)
851f26ae
OJ
11454 {
11455 if (dp_op_sz)
11456 curr_insn_type = INSN_T1;
11457 else
11458 curr_insn_type = INSN_T2;
11459 }
11460 /* Handle all other vfp data processing instructions. */
11461 else if (opc1 == 0x0b)
11462 {
11463 /* Handle VMOV. */
11464 if (!(opc3 & 0x01) || (opc2 == 0x00 && opc3 == 0x01))
11465 {
11466 if (bit (arm_insn_r->arm_insn, 4))
11467 {
11468 if (bit (arm_insn_r->arm_insn, 6))
11469 curr_insn_type = INSN_T0;
11470 else
11471 curr_insn_type = INSN_T1;
11472 }
11473 else
11474 {
11475 if (dp_op_sz)
11476 curr_insn_type = INSN_T1;
11477 else
11478 curr_insn_type = INSN_T2;
11479 }
11480 }
11481 /* Handle VNEG and VABS. */
11482 else if ((opc2 == 0x01 && opc3 == 0x01)
11483 || (opc2 == 0x00 && opc3 == 0x03))
11484 {
11485 if (!bit (arm_insn_r->arm_insn, 11))
11486 {
11487 if (bit (arm_insn_r->arm_insn, 6))
11488 curr_insn_type = INSN_T0;
11489 else
11490 curr_insn_type = INSN_T1;
11491 }
11492 else
11493 {
11494 if (dp_op_sz)
11495 curr_insn_type = INSN_T1;
11496 else
11497 curr_insn_type = INSN_T2;
11498 }
11499 }
11500 /* Handle VSQRT. */
11501 else if (opc2 == 0x01 && opc3 == 0x03)
11502 {
11503 if (dp_op_sz)
11504 curr_insn_type = INSN_T1;
11505 else
11506 curr_insn_type = INSN_T2;
11507 }
11508 /* Handle VCVT. */
11509 else if (opc2 == 0x07 && opc3 == 0x03)
11510 {
11511 if (!dp_op_sz)
11512 curr_insn_type = INSN_T1;
11513 else
11514 curr_insn_type = INSN_T2;
11515 }
11516 else if (opc3 & 0x01)
11517 {
11518 /* Handle VCVT. */
11519 if ((opc2 == 0x08) || (opc2 & 0x0e) == 0x0c)
11520 {
11521 if (!bit (arm_insn_r->arm_insn, 18))
11522 curr_insn_type = INSN_T2;
11523 else
11524 {
11525 if (dp_op_sz)
11526 curr_insn_type = INSN_T1;
11527 else
11528 curr_insn_type = INSN_T2;
11529 }
11530 }
11531 /* Handle VCVT. */
11532 else if ((opc2 & 0x0e) == 0x0a || (opc2 & 0x0e) == 0x0e)
11533 {
11534 if (dp_op_sz)
11535 curr_insn_type = INSN_T1;
11536 else
11537 curr_insn_type = INSN_T2;
11538 }
11539 /* Handle VCVTB, VCVTT. */
11540 else if ((opc2 & 0x0e) == 0x02)
11541 curr_insn_type = INSN_T2;
11542 /* Handle VCMP, VCMPE. */
11543 else if ((opc2 & 0x0e) == 0x04)
11544 curr_insn_type = INSN_T3;
11545 }
11546 }
11547
11548 switch (curr_insn_type)
11549 {
11550 case INSN_T0:
11551 reg_vd = reg_vd | (bit_d << 4);
11552 record_buf[0] = reg_vd + ARM_D0_REGNUM;
11553 record_buf[1] = reg_vd + ARM_D0_REGNUM + 1;
11554 arm_insn_r->reg_rec_count = 2;
11555 break;
11556
11557 case INSN_T1:
11558 reg_vd = reg_vd | (bit_d << 4);
11559 record_buf[0] = reg_vd + ARM_D0_REGNUM;
11560 arm_insn_r->reg_rec_count = 1;
11561 break;
11562
11563 case INSN_T2:
11564 reg_vd = (reg_vd << 1) | bit_d;
11565 record_buf[0] = reg_vd + ARM_D0_REGNUM;
11566 arm_insn_r->reg_rec_count = 1;
11567 break;
11568
11569 case INSN_T3:
11570 record_buf[0] = ARM_FPSCR_REGNUM;
11571 arm_insn_r->reg_rec_count = 1;
11572 break;
11573
11574 default:
11575 gdb_assert_not_reached ("no decoding pattern found");
11576 break;
11577 }
11578
11579 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11580 return 0;
11581}
11582
60cc5e93
OJ
11583/* Handling opcode 110 insns. */
11584
11585static int
11586arm_record_asimd_vfp_coproc (insn_decode_record *arm_insn_r)
11587{
bec2ab5a 11588 uint32_t op1, op1_ebit, coproc;
60cc5e93
OJ
11589
11590 coproc = bits (arm_insn_r->arm_insn, 8, 11);
11591 op1 = bits (arm_insn_r->arm_insn, 20, 25);
11592 op1_ebit = bit (arm_insn_r->arm_insn, 20);
11593
11594 if ((coproc & 0x0e) == 0x0a)
11595 {
11596 /* Handle extension register ld/st instructions. */
11597 if (!(op1 & 0x20))
f20f80dd 11598 return arm_record_exreg_ld_st_insn (arm_insn_r);
60cc5e93
OJ
11599
11600 /* 64-bit transfers between arm core and extension registers. */
11601 if ((op1 & 0x3e) == 0x04)
f20f80dd 11602 return arm_record_exreg_ld_st_insn (arm_insn_r);
60cc5e93
OJ
11603 }
11604 else
11605 {
11606 /* Handle coprocessor ld/st instructions. */
11607 if (!(op1 & 0x3a))
11608 {
11609 /* Store. */
11610 if (!op1_ebit)
11611 return arm_record_unsupported_insn (arm_insn_r);
11612 else
11613 /* Load. */
11614 return arm_record_unsupported_insn (arm_insn_r);
11615 }
11616
11617 /* Move to coprocessor from two arm core registers. */
11618 if (op1 == 0x4)
11619 return arm_record_unsupported_insn (arm_insn_r);
11620
11621 /* Move to two arm core registers from coprocessor. */
11622 if (op1 == 0x5)
11623 {
11624 uint32_t reg_t[2];
11625
11626 reg_t[0] = bits (arm_insn_r->arm_insn, 12, 15);
11627 reg_t[1] = bits (arm_insn_r->arm_insn, 16, 19);
11628 arm_insn_r->reg_rec_count = 2;
11629
11630 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, reg_t);
11631 return 0;
11632 }
11633 }
11634 return arm_record_unsupported_insn (arm_insn_r);
11635}
11636
72508ac0
PO
11637/* Handling opcode 111 insns. */
11638
11639static int
11640arm_record_coproc_data_proc (insn_decode_record *arm_insn_r)
11641{
2d9e6acb 11642 uint32_t op, op1_ebit, coproc, bits_24_25;
72508ac0
PO
11643 struct gdbarch_tdep *tdep = gdbarch_tdep (arm_insn_r->gdbarch);
11644 struct regcache *reg_cache = arm_insn_r->regcache;
72508ac0
PO
11645
11646 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 24, 27);
60cc5e93 11647 coproc = bits (arm_insn_r->arm_insn, 8, 11);
60cc5e93
OJ
11648 op1_ebit = bit (arm_insn_r->arm_insn, 20);
11649 op = bit (arm_insn_r->arm_insn, 4);
2d9e6acb 11650 bits_24_25 = bits (arm_insn_r->arm_insn, 24, 25);
97dfe206
OJ
11651
11652 /* Handle arm SWI/SVC system call instructions. */
2d9e6acb 11653 if (bits_24_25 == 0x3)
97dfe206
OJ
11654 {
11655 if (tdep->arm_syscall_record != NULL)
11656 {
11657 ULONGEST svc_operand, svc_number;
11658
11659 svc_operand = (0x00ffffff & arm_insn_r->arm_insn);
11660
11661 if (svc_operand) /* OABI. */
11662 svc_number = svc_operand - 0x900000;
11663 else /* EABI. */
11664 regcache_raw_read_unsigned (reg_cache, 7, &svc_number);
11665
60cc5e93 11666 return tdep->arm_syscall_record (reg_cache, svc_number);
97dfe206
OJ
11667 }
11668 else
11669 {
11670 printf_unfiltered (_("no syscall record support\n"));
60cc5e93 11671 return -1;
97dfe206
OJ
11672 }
11673 }
2d9e6acb 11674 else if (bits_24_25 == 0x02)
60cc5e93 11675 {
2d9e6acb
YQ
11676 if (op)
11677 {
11678 if ((coproc & 0x0e) == 0x0a)
11679 {
11680 /* 8, 16, and 32-bit transfer */
11681 return arm_record_vdata_transfer_insn (arm_insn_r);
11682 }
11683 else
11684 {
11685 if (op1_ebit)
11686 {
11687 /* MRC, MRC2 */
11688 uint32_t record_buf[1];
11689
11690 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11691 if (record_buf[0] == 15)
11692 record_buf[0] = ARM_PS_REGNUM;
60cc5e93 11693
2d9e6acb
YQ
11694 arm_insn_r->reg_rec_count = 1;
11695 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count,
11696 record_buf);
11697 return 0;
11698 }
11699 else
11700 {
11701 /* MCR, MCR2 */
11702 return -1;
11703 }
11704 }
11705 }
11706 else
11707 {
11708 if ((coproc & 0x0e) == 0x0a)
11709 {
11710 /* VFP data-processing instructions. */
11711 return arm_record_vfp_data_proc_insn (arm_insn_r);
11712 }
11713 else
11714 {
11715 /* CDP, CDP2 */
11716 return -1;
11717 }
11718 }
60cc5e93 11719 }
97dfe206
OJ
11720 else
11721 {
2d9e6acb 11722 unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 25);
60cc5e93 11723
2d9e6acb
YQ
11724 if (op1 == 5)
11725 {
11726 if ((coproc & 0x0e) != 0x0a)
11727 {
11728 /* MRRC, MRRC2 */
11729 return -1;
11730 }
11731 }
11732 else if (op1 == 4 || op1 == 5)
11733 {
11734 if ((coproc & 0x0e) == 0x0a)
11735 {
11736 /* 64-bit transfers between ARM core and extension */
11737 return -1;
11738 }
11739 else if (op1 == 4)
11740 {
11741 /* MCRR, MCRR2 */
11742 return -1;
11743 }
11744 }
11745 else if (op1 == 0 || op1 == 1)
11746 {
11747 /* UNDEFINED */
11748 return -1;
11749 }
11750 else
11751 {
11752 if ((coproc & 0x0e) == 0x0a)
11753 {
11754 /* Extension register load/store */
11755 }
11756 else
11757 {
11758 /* STC, STC2, LDC, LDC2 */
11759 }
11760 return -1;
11761 }
97dfe206 11762 }
72508ac0 11763
2d9e6acb 11764 return -1;
72508ac0
PO
11765}
11766
11767/* Handling opcode 000 insns. */
11768
11769static int
11770thumb_record_shift_add_sub (insn_decode_record *thumb_insn_r)
11771{
11772 uint32_t record_buf[8];
11773 uint32_t reg_src1 = 0;
11774
11775 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11776
11777 record_buf[0] = ARM_PS_REGNUM;
11778 record_buf[1] = reg_src1;
11779 thumb_insn_r->reg_rec_count = 2;
11780
11781 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11782
11783 return 0;
11784}
11785
11786
11787/* Handling opcode 001 insns. */
11788
11789static int
11790thumb_record_add_sub_cmp_mov (insn_decode_record *thumb_insn_r)
11791{
11792 uint32_t record_buf[8];
11793 uint32_t reg_src1 = 0;
11794
11795 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11796
11797 record_buf[0] = ARM_PS_REGNUM;
11798 record_buf[1] = reg_src1;
11799 thumb_insn_r->reg_rec_count = 2;
11800
11801 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11802
11803 return 0;
11804}
11805
11806/* Handling opcode 010 insns. */
11807
11808static int
11809thumb_record_ld_st_reg_offset (insn_decode_record *thumb_insn_r)
11810{
11811 struct regcache *reg_cache = thumb_insn_r->regcache;
11812 uint32_t record_buf[8], record_buf_mem[8];
11813
11814 uint32_t reg_src1 = 0, reg_src2 = 0;
11815 uint32_t opcode1 = 0, opcode2 = 0, opcode3 = 0;
11816
11817 ULONGEST u_regval[2] = {0};
11818
11819 opcode1 = bits (thumb_insn_r->arm_insn, 10, 12);
11820
11821 if (bit (thumb_insn_r->arm_insn, 12))
11822 {
11823 /* Handle load/store register offset. */
b121eeb9
YQ
11824 uint32_t opB = bits (thumb_insn_r->arm_insn, 9, 11);
11825
b020ff80 11826 if (in_inclusive_range (opB, 4U, 7U))
72508ac0
PO
11827 {
11828 /* LDR(2), LDRB(2) , LDRH(2), LDRSB, LDRSH. */
11829 reg_src1 = bits (thumb_insn_r->arm_insn,0, 2);
11830 record_buf[0] = reg_src1;
11831 thumb_insn_r->reg_rec_count = 1;
11832 }
b020ff80 11833 else if (in_inclusive_range (opB, 0U, 2U))
72508ac0
PO
11834 {
11835 /* STR(2), STRB(2), STRH(2) . */
11836 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
11837 reg_src2 = bits (thumb_insn_r->arm_insn, 6, 8);
11838 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11839 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
b121eeb9 11840 if (0 == opB)
72508ac0 11841 record_buf_mem[0] = 4; /* STR (2). */
b121eeb9 11842 else if (2 == opB)
72508ac0 11843 record_buf_mem[0] = 1; /* STRB (2). */
b121eeb9 11844 else if (1 == opB)
72508ac0
PO
11845 record_buf_mem[0] = 2; /* STRH (2). */
11846 record_buf_mem[1] = u_regval[0] + u_regval[1];
11847 thumb_insn_r->mem_rec_count = 1;
11848 }
11849 }
11850 else if (bit (thumb_insn_r->arm_insn, 11))
11851 {
11852 /* Handle load from literal pool. */
11853 /* LDR(3). */
11854 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11855 record_buf[0] = reg_src1;
11856 thumb_insn_r->reg_rec_count = 1;
11857 }
11858 else if (opcode1)
11859 {
b121eeb9 11860 /* Special data instructions and branch and exchange */
72508ac0
PO
11861 opcode2 = bits (thumb_insn_r->arm_insn, 8, 9);
11862 opcode3 = bits (thumb_insn_r->arm_insn, 0, 2);
11863 if ((3 == opcode2) && (!opcode3))
11864 {
11865 /* Branch with exchange. */
11866 record_buf[0] = ARM_PS_REGNUM;
11867 thumb_insn_r->reg_rec_count = 1;
11868 }
11869 else
11870 {
1f33efec
YQ
11871 /* Format 8; special data processing insns. */
11872 record_buf[0] = ARM_PS_REGNUM;
11873 record_buf[1] = (bit (thumb_insn_r->arm_insn, 7) << 3
11874 | bits (thumb_insn_r->arm_insn, 0, 2));
72508ac0
PO
11875 thumb_insn_r->reg_rec_count = 2;
11876 }
11877 }
11878 else
11879 {
11880 /* Format 5; data processing insns. */
11881 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11882 if (bit (thumb_insn_r->arm_insn, 7))
11883 {
11884 reg_src1 = reg_src1 + 8;
11885 }
11886 record_buf[0] = ARM_PS_REGNUM;
11887 record_buf[1] = reg_src1;
11888 thumb_insn_r->reg_rec_count = 2;
11889 }
11890
11891 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11892 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
11893 record_buf_mem);
11894
11895 return 0;
11896}
11897
11898/* Handling opcode 001 insns. */
11899
11900static int
11901thumb_record_ld_st_imm_offset (insn_decode_record *thumb_insn_r)
11902{
11903 struct regcache *reg_cache = thumb_insn_r->regcache;
11904 uint32_t record_buf[8], record_buf_mem[8];
11905
11906 uint32_t reg_src1 = 0;
11907 uint32_t opcode = 0, immed_5 = 0;
11908
11909 ULONGEST u_regval = 0;
11910
11911 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
11912
11913 if (opcode)
11914 {
11915 /* LDR(1). */
11916 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11917 record_buf[0] = reg_src1;
11918 thumb_insn_r->reg_rec_count = 1;
11919 }
11920 else
11921 {
11922 /* STR(1). */
11923 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
11924 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
11925 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11926 record_buf_mem[0] = 4;
11927 record_buf_mem[1] = u_regval + (immed_5 * 4);
11928 thumb_insn_r->mem_rec_count = 1;
11929 }
11930
11931 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11932 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
11933 record_buf_mem);
11934
11935 return 0;
11936}
11937
11938/* Handling opcode 100 insns. */
11939
11940static int
11941thumb_record_ld_st_stack (insn_decode_record *thumb_insn_r)
11942{
11943 struct regcache *reg_cache = thumb_insn_r->regcache;
11944 uint32_t record_buf[8], record_buf_mem[8];
11945
11946 uint32_t reg_src1 = 0;
11947 uint32_t opcode = 0, immed_8 = 0, immed_5 = 0;
11948
11949 ULONGEST u_regval = 0;
11950
11951 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
11952
11953 if (3 == opcode)
11954 {
11955 /* LDR(4). */
11956 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11957 record_buf[0] = reg_src1;
11958 thumb_insn_r->reg_rec_count = 1;
11959 }
11960 else if (1 == opcode)
11961 {
11962 /* LDRH(1). */
11963 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11964 record_buf[0] = reg_src1;
11965 thumb_insn_r->reg_rec_count = 1;
11966 }
11967 else if (2 == opcode)
11968 {
11969 /* STR(3). */
11970 immed_8 = bits (thumb_insn_r->arm_insn, 0, 7);
11971 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
11972 record_buf_mem[0] = 4;
11973 record_buf_mem[1] = u_regval + (immed_8 * 4);
11974 thumb_insn_r->mem_rec_count = 1;
11975 }
11976 else if (0 == opcode)
11977 {
11978 /* STRH(1). */
11979 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
11980 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
11981 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11982 record_buf_mem[0] = 2;
11983 record_buf_mem[1] = u_regval + (immed_5 * 2);
11984 thumb_insn_r->mem_rec_count = 1;
11985 }
11986
11987 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11988 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
11989 record_buf_mem);
11990
11991 return 0;
11992}
11993
11994/* Handling opcode 101 insns. */
11995
11996static int
11997thumb_record_misc (insn_decode_record *thumb_insn_r)
11998{
11999 struct regcache *reg_cache = thumb_insn_r->regcache;
12000
b121eeb9 12001 uint32_t opcode = 0;
72508ac0 12002 uint32_t register_bits = 0, register_count = 0;
bec2ab5a 12003 uint32_t index = 0, start_address = 0;
72508ac0
PO
12004 uint32_t record_buf[24], record_buf_mem[48];
12005 uint32_t reg_src1;
12006
12007 ULONGEST u_regval = 0;
12008
12009 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
72508ac0 12010
b121eeb9 12011 if (opcode == 0 || opcode == 1)
72508ac0 12012 {
b121eeb9
YQ
12013 /* ADR and ADD (SP plus immediate) */
12014
72508ac0
PO
12015 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12016 record_buf[0] = reg_src1;
12017 thumb_insn_r->reg_rec_count = 1;
12018 }
b121eeb9 12019 else
72508ac0 12020 {
b121eeb9
YQ
12021 /* Miscellaneous 16-bit instructions */
12022 uint32_t opcode2 = bits (thumb_insn_r->arm_insn, 8, 11);
12023
12024 switch (opcode2)
12025 {
12026 case 6:
12027 /* SETEND and CPS */
12028 break;
12029 case 0:
12030 /* ADD/SUB (SP plus immediate) */
12031 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12032 record_buf[0] = ARM_SP_REGNUM;
12033 thumb_insn_r->reg_rec_count = 1;
12034 break;
12035 case 1: /* fall through */
12036 case 3: /* fall through */
12037 case 9: /* fall through */
12038 case 11:
12039 /* CBNZ, CBZ */
b121eeb9
YQ
12040 break;
12041 case 2:
12042 /* SXTH, SXTB, UXTH, UXTB */
12043 record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
12044 thumb_insn_r->reg_rec_count = 1;
12045 break;
12046 case 4: /* fall through */
12047 case 5:
12048 /* PUSH. */
12049 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12050 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
12051 while (register_bits)
12052 {
12053 if (register_bits & 0x00000001)
12054 register_count++;
12055 register_bits = register_bits >> 1;
12056 }
12057 start_address = u_regval - \
12058 (4 * (bit (thumb_insn_r->arm_insn, 8) + register_count));
12059 thumb_insn_r->mem_rec_count = register_count;
12060 while (register_count)
12061 {
12062 record_buf_mem[(register_count * 2) - 1] = start_address;
12063 record_buf_mem[(register_count * 2) - 2] = 4;
12064 start_address = start_address + 4;
12065 register_count--;
12066 }
12067 record_buf[0] = ARM_SP_REGNUM;
12068 thumb_insn_r->reg_rec_count = 1;
12069 break;
12070 case 10:
12071 /* REV, REV16, REVSH */
ba14f379
YQ
12072 record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
12073 thumb_insn_r->reg_rec_count = 1;
b121eeb9
YQ
12074 break;
12075 case 12: /* fall through */
12076 case 13:
12077 /* POP. */
12078 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12079 while (register_bits)
12080 {
12081 if (register_bits & 0x00000001)
12082 record_buf[index++] = register_count;
12083 register_bits = register_bits >> 1;
12084 register_count++;
12085 }
12086 record_buf[index++] = ARM_PS_REGNUM;
12087 record_buf[index++] = ARM_SP_REGNUM;
12088 thumb_insn_r->reg_rec_count = index;
12089 break;
12090 case 0xe:
12091 /* BKPT insn. */
12092 /* Handle enhanced software breakpoint insn, BKPT. */
12093 /* CPSR is changed to be executed in ARM state, disabling normal
12094 interrupts, entering abort mode. */
12095 /* According to high vector configuration PC is set. */
12096 /* User hits breakpoint and type reverse, in that case, we need to go back with
12097 previous CPSR and Program Counter. */
12098 record_buf[0] = ARM_PS_REGNUM;
12099 record_buf[1] = ARM_LR_REGNUM;
12100 thumb_insn_r->reg_rec_count = 2;
12101 /* We need to save SPSR value, which is not yet done. */
12102 printf_unfiltered (_("Process record does not support instruction "
12103 "0x%0x at address %s.\n"),
12104 thumb_insn_r->arm_insn,
12105 paddress (thumb_insn_r->gdbarch,
12106 thumb_insn_r->this_addr));
12107 return -1;
12108
12109 case 0xf:
12110 /* If-Then, and hints */
12111 break;
12112 default:
12113 return -1;
12114 };
72508ac0
PO
12115 }
12116
12117 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12118 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12119 record_buf_mem);
12120
12121 return 0;
12122}
12123
12124/* Handling opcode 110 insns. */
12125
12126static int
12127thumb_record_ldm_stm_swi (insn_decode_record *thumb_insn_r)
12128{
12129 struct gdbarch_tdep *tdep = gdbarch_tdep (thumb_insn_r->gdbarch);
12130 struct regcache *reg_cache = thumb_insn_r->regcache;
12131
12132 uint32_t ret = 0; /* function return value: -1:record failure ; 0:success */
12133 uint32_t reg_src1 = 0;
12134 uint32_t opcode1 = 0, opcode2 = 0, register_bits = 0, register_count = 0;
bec2ab5a 12135 uint32_t index = 0, start_address = 0;
72508ac0
PO
12136 uint32_t record_buf[24], record_buf_mem[48];
12137
12138 ULONGEST u_regval = 0;
12139
12140 opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
12141 opcode2 = bits (thumb_insn_r->arm_insn, 11, 12);
12142
12143 if (1 == opcode2)
12144 {
12145
12146 /* LDMIA. */
12147 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12148 /* Get Rn. */
12149 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12150 while (register_bits)
12151 {
12152 if (register_bits & 0x00000001)
f969241e 12153 record_buf[index++] = register_count;
72508ac0 12154 register_bits = register_bits >> 1;
f969241e 12155 register_count++;
72508ac0 12156 }
f969241e
OJ
12157 record_buf[index++] = reg_src1;
12158 thumb_insn_r->reg_rec_count = index;
72508ac0
PO
12159 }
12160 else if (0 == opcode2)
12161 {
12162 /* It handles both STMIA. */
12163 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12164 /* Get Rn. */
12165 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12166 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12167 while (register_bits)
12168 {
12169 if (register_bits & 0x00000001)
12170 register_count++;
12171 register_bits = register_bits >> 1;
12172 }
12173 start_address = u_regval;
12174 thumb_insn_r->mem_rec_count = register_count;
12175 while (register_count)
12176 {
12177 record_buf_mem[(register_count * 2) - 1] = start_address;
12178 record_buf_mem[(register_count * 2) - 2] = 4;
12179 start_address = start_address + 4;
12180 register_count--;
12181 }
12182 }
12183 else if (0x1F == opcode1)
12184 {
12185 /* Handle arm syscall insn. */
97dfe206 12186 if (tdep->arm_syscall_record != NULL)
72508ac0 12187 {
97dfe206
OJ
12188 regcache_raw_read_unsigned (reg_cache, 7, &u_regval);
12189 ret = tdep->arm_syscall_record (reg_cache, u_regval);
72508ac0
PO
12190 }
12191 else
12192 {
12193 printf_unfiltered (_("no syscall record support\n"));
12194 return -1;
12195 }
12196 }
12197
12198 /* B (1), conditional branch is automatically taken care in process_record,
12199 as PC is saved there. */
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 ret;
12206}
12207
12208/* Handling opcode 111 insns. */
12209
12210static int
12211thumb_record_branch (insn_decode_record *thumb_insn_r)
12212{
12213 uint32_t record_buf[8];
12214 uint32_t bits_h = 0;
12215
12216 bits_h = bits (thumb_insn_r->arm_insn, 11, 12);
12217
12218 if (2 == bits_h || 3 == bits_h)
12219 {
12220 /* BL */
12221 record_buf[0] = ARM_LR_REGNUM;
12222 thumb_insn_r->reg_rec_count = 1;
12223 }
12224 else if (1 == bits_h)
12225 {
12226 /* BLX(1). */
12227 record_buf[0] = ARM_PS_REGNUM;
12228 record_buf[1] = ARM_LR_REGNUM;
12229 thumb_insn_r->reg_rec_count = 2;
12230 }
12231
12232 /* B(2) is automatically taken care in process_record, as PC is
12233 saved there. */
12234
12235 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12236
12237 return 0;
12238}
12239
c6ec2b30
OJ
12240/* Handler for thumb2 load/store multiple instructions. */
12241
12242static int
12243thumb2_record_ld_st_multiple (insn_decode_record *thumb2_insn_r)
12244{
12245 struct regcache *reg_cache = thumb2_insn_r->regcache;
12246
12247 uint32_t reg_rn, op;
12248 uint32_t register_bits = 0, register_count = 0;
12249 uint32_t index = 0, start_address = 0;
12250 uint32_t record_buf[24], record_buf_mem[48];
12251
12252 ULONGEST u_regval = 0;
12253
12254 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12255 op = bits (thumb2_insn_r->arm_insn, 23, 24);
12256
12257 if (0 == op || 3 == op)
12258 {
12259 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12260 {
12261 /* Handle RFE instruction. */
12262 record_buf[0] = ARM_PS_REGNUM;
12263 thumb2_insn_r->reg_rec_count = 1;
12264 }
12265 else
12266 {
12267 /* Handle SRS instruction after reading banked SP. */
12268 return arm_record_unsupported_insn (thumb2_insn_r);
12269 }
12270 }
12271 else if (1 == op || 2 == op)
12272 {
12273 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12274 {
12275 /* Handle LDM/LDMIA/LDMFD and LDMDB/LDMEA instructions. */
12276 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
12277 while (register_bits)
12278 {
12279 if (register_bits & 0x00000001)
12280 record_buf[index++] = register_count;
12281
12282 register_count++;
12283 register_bits = register_bits >> 1;
12284 }
12285 record_buf[index++] = reg_rn;
12286 record_buf[index++] = ARM_PS_REGNUM;
12287 thumb2_insn_r->reg_rec_count = index;
12288 }
12289 else
12290 {
12291 /* Handle STM/STMIA/STMEA and STMDB/STMFD. */
12292 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
12293 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12294 while (register_bits)
12295 {
12296 if (register_bits & 0x00000001)
12297 register_count++;
12298
12299 register_bits = register_bits >> 1;
12300 }
12301
12302 if (1 == op)
12303 {
12304 /* Start address calculation for LDMDB/LDMEA. */
12305 start_address = u_regval;
12306 }
12307 else if (2 == op)
12308 {
12309 /* Start address calculation for LDMDB/LDMEA. */
12310 start_address = u_regval - register_count * 4;
12311 }
12312
12313 thumb2_insn_r->mem_rec_count = register_count;
12314 while (register_count)
12315 {
12316 record_buf_mem[register_count * 2 - 1] = start_address;
12317 record_buf_mem[register_count * 2 - 2] = 4;
12318 start_address = start_address + 4;
12319 register_count--;
12320 }
12321 record_buf[0] = reg_rn;
12322 record_buf[1] = ARM_PS_REGNUM;
12323 thumb2_insn_r->reg_rec_count = 2;
12324 }
12325 }
12326
12327 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
12328 record_buf_mem);
12329 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12330 record_buf);
12331 return ARM_RECORD_SUCCESS;
12332}
12333
12334/* Handler for thumb2 load/store (dual/exclusive) and table branch
12335 instructions. */
12336
12337static int
12338thumb2_record_ld_st_dual_ex_tbb (insn_decode_record *thumb2_insn_r)
12339{
12340 struct regcache *reg_cache = thumb2_insn_r->regcache;
12341
12342 uint32_t reg_rd, reg_rn, offset_imm;
12343 uint32_t reg_dest1, reg_dest2;
12344 uint32_t address, offset_addr;
12345 uint32_t record_buf[8], record_buf_mem[8];
12346 uint32_t op1, op2, op3;
c6ec2b30
OJ
12347
12348 ULONGEST u_regval[2];
12349
12350 op1 = bits (thumb2_insn_r->arm_insn, 23, 24);
12351 op2 = bits (thumb2_insn_r->arm_insn, 20, 21);
12352 op3 = bits (thumb2_insn_r->arm_insn, 4, 7);
12353
12354 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12355 {
12356 if(!(1 == op1 && 1 == op2 && (0 == op3 || 1 == op3)))
12357 {
12358 reg_dest1 = bits (thumb2_insn_r->arm_insn, 12, 15);
12359 record_buf[0] = reg_dest1;
12360 record_buf[1] = ARM_PS_REGNUM;
12361 thumb2_insn_r->reg_rec_count = 2;
12362 }
12363
12364 if (3 == op2 || (op1 & 2) || (1 == op1 && 1 == op2 && 7 == op3))
12365 {
12366 reg_dest2 = bits (thumb2_insn_r->arm_insn, 8, 11);
12367 record_buf[2] = reg_dest2;
12368 thumb2_insn_r->reg_rec_count = 3;
12369 }
12370 }
12371 else
12372 {
12373 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12374 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
12375
12376 if (0 == op1 && 0 == op2)
12377 {
12378 /* Handle STREX. */
12379 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
12380 address = u_regval[0] + (offset_imm * 4);
12381 record_buf_mem[0] = 4;
12382 record_buf_mem[1] = address;
12383 thumb2_insn_r->mem_rec_count = 1;
12384 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
12385 record_buf[0] = reg_rd;
12386 thumb2_insn_r->reg_rec_count = 1;
12387 }
12388 else if (1 == op1 && 0 == op2)
12389 {
12390 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
12391 record_buf[0] = reg_rd;
12392 thumb2_insn_r->reg_rec_count = 1;
12393 address = u_regval[0];
12394 record_buf_mem[1] = address;
12395
12396 if (4 == op3)
12397 {
12398 /* Handle STREXB. */
12399 record_buf_mem[0] = 1;
12400 thumb2_insn_r->mem_rec_count = 1;
12401 }
12402 else if (5 == op3)
12403 {
12404 /* Handle STREXH. */
12405 record_buf_mem[0] = 2 ;
12406 thumb2_insn_r->mem_rec_count = 1;
12407 }
12408 else if (7 == op3)
12409 {
12410 /* Handle STREXD. */
12411 address = u_regval[0];
12412 record_buf_mem[0] = 4;
12413 record_buf_mem[2] = 4;
12414 record_buf_mem[3] = address + 4;
12415 thumb2_insn_r->mem_rec_count = 2;
12416 }
12417 }
12418 else
12419 {
12420 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
12421
12422 if (bit (thumb2_insn_r->arm_insn, 24))
12423 {
12424 if (bit (thumb2_insn_r->arm_insn, 23))
12425 offset_addr = u_regval[0] + (offset_imm * 4);
12426 else
12427 offset_addr = u_regval[0] - (offset_imm * 4);
12428
12429 address = offset_addr;
12430 }
12431 else
12432 address = u_regval[0];
12433
12434 record_buf_mem[0] = 4;
12435 record_buf_mem[1] = address;
12436 record_buf_mem[2] = 4;
12437 record_buf_mem[3] = address + 4;
12438 thumb2_insn_r->mem_rec_count = 2;
12439 record_buf[0] = reg_rn;
12440 thumb2_insn_r->reg_rec_count = 1;
12441 }
12442 }
12443
12444 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12445 record_buf);
12446 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
12447 record_buf_mem);
12448 return ARM_RECORD_SUCCESS;
12449}
12450
12451/* Handler for thumb2 data processing (shift register and modified immediate)
12452 instructions. */
12453
12454static int
12455thumb2_record_data_proc_sreg_mimm (insn_decode_record *thumb2_insn_r)
12456{
12457 uint32_t reg_rd, op;
12458 uint32_t record_buf[8];
12459
12460 op = bits (thumb2_insn_r->arm_insn, 21, 24);
12461 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
12462
12463 if ((0 == op || 4 == op || 8 == op || 13 == op) && 15 == reg_rd)
12464 {
12465 record_buf[0] = ARM_PS_REGNUM;
12466 thumb2_insn_r->reg_rec_count = 1;
12467 }
12468 else
12469 {
12470 record_buf[0] = reg_rd;
12471 record_buf[1] = ARM_PS_REGNUM;
12472 thumb2_insn_r->reg_rec_count = 2;
12473 }
12474
12475 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12476 record_buf);
12477 return ARM_RECORD_SUCCESS;
12478}
12479
12480/* Generic handler for thumb2 instructions which effect destination and PS
12481 registers. */
12482
12483static int
12484thumb2_record_ps_dest_generic (insn_decode_record *thumb2_insn_r)
12485{
12486 uint32_t reg_rd;
12487 uint32_t record_buf[8];
12488
12489 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
12490
12491 record_buf[0] = reg_rd;
12492 record_buf[1] = ARM_PS_REGNUM;
12493 thumb2_insn_r->reg_rec_count = 2;
12494
12495 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12496 record_buf);
12497 return ARM_RECORD_SUCCESS;
12498}
12499
12500/* Handler for thumb2 branch and miscellaneous control instructions. */
12501
12502static int
12503thumb2_record_branch_misc_cntrl (insn_decode_record *thumb2_insn_r)
12504{
12505 uint32_t op, op1, op2;
12506 uint32_t record_buf[8];
12507
12508 op = bits (thumb2_insn_r->arm_insn, 20, 26);
12509 op1 = bits (thumb2_insn_r->arm_insn, 12, 14);
12510 op2 = bits (thumb2_insn_r->arm_insn, 8, 11);
12511
12512 /* Handle MSR insn. */
12513 if (!(op1 & 0x2) && 0x38 == op)
12514 {
12515 if (!(op2 & 0x3))
12516 {
12517 /* CPSR is going to be changed. */
12518 record_buf[0] = ARM_PS_REGNUM;
12519 thumb2_insn_r->reg_rec_count = 1;
12520 }
12521 else
12522 {
12523 arm_record_unsupported_insn(thumb2_insn_r);
12524 return -1;
12525 }
12526 }
12527 else if (4 == (op1 & 0x5) || 5 == (op1 & 0x5))
12528 {
12529 /* BLX. */
12530 record_buf[0] = ARM_PS_REGNUM;
12531 record_buf[1] = ARM_LR_REGNUM;
12532 thumb2_insn_r->reg_rec_count = 2;
12533 }
12534
12535 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12536 record_buf);
12537 return ARM_RECORD_SUCCESS;
12538}
12539
12540/* Handler for thumb2 store single data item instructions. */
12541
12542static int
12543thumb2_record_str_single_data (insn_decode_record *thumb2_insn_r)
12544{
12545 struct regcache *reg_cache = thumb2_insn_r->regcache;
12546
12547 uint32_t reg_rn, reg_rm, offset_imm, shift_imm;
12548 uint32_t address, offset_addr;
12549 uint32_t record_buf[8], record_buf_mem[8];
12550 uint32_t op1, op2;
12551
12552 ULONGEST u_regval[2];
12553
12554 op1 = bits (thumb2_insn_r->arm_insn, 21, 23);
12555 op2 = bits (thumb2_insn_r->arm_insn, 6, 11);
12556 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12557 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
12558
12559 if (bit (thumb2_insn_r->arm_insn, 23))
12560 {
12561 /* T2 encoding. */
12562 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 11);
12563 offset_addr = u_regval[0] + offset_imm;
12564 address = offset_addr;
12565 }
12566 else
12567 {
12568 /* T3 encoding. */
12569 if ((0 == op1 || 1 == op1 || 2 == op1) && !(op2 & 0x20))
12570 {
12571 /* Handle STRB (register). */
12572 reg_rm = bits (thumb2_insn_r->arm_insn, 0, 3);
12573 regcache_raw_read_unsigned (reg_cache, reg_rm, &u_regval[1]);
12574 shift_imm = bits (thumb2_insn_r->arm_insn, 4, 5);
12575 offset_addr = u_regval[1] << shift_imm;
12576 address = u_regval[0] + offset_addr;
12577 }
12578 else
12579 {
12580 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
12581 if (bit (thumb2_insn_r->arm_insn, 10))
12582 {
12583 if (bit (thumb2_insn_r->arm_insn, 9))
12584 offset_addr = u_regval[0] + offset_imm;
12585 else
12586 offset_addr = u_regval[0] - offset_imm;
12587
12588 address = offset_addr;
12589 }
12590 else
12591 address = u_regval[0];
12592 }
12593 }
12594
12595 switch (op1)
12596 {
12597 /* Store byte instructions. */
12598 case 4:
12599 case 0:
12600 record_buf_mem[0] = 1;
12601 break;
12602 /* Store half word instructions. */
12603 case 1:
12604 case 5:
12605 record_buf_mem[0] = 2;
12606 break;
12607 /* Store word instructions. */
12608 case 2:
12609 case 6:
12610 record_buf_mem[0] = 4;
12611 break;
12612
12613 default:
12614 gdb_assert_not_reached ("no decoding pattern found");
12615 break;
12616 }
12617
12618 record_buf_mem[1] = address;
12619 thumb2_insn_r->mem_rec_count = 1;
12620 record_buf[0] = reg_rn;
12621 thumb2_insn_r->reg_rec_count = 1;
12622
12623 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12624 record_buf);
12625 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
12626 record_buf_mem);
12627 return ARM_RECORD_SUCCESS;
12628}
12629
12630/* Handler for thumb2 load memory hints instructions. */
12631
12632static int
12633thumb2_record_ld_mem_hints (insn_decode_record *thumb2_insn_r)
12634{
12635 uint32_t record_buf[8];
12636 uint32_t reg_rt, reg_rn;
12637
12638 reg_rt = bits (thumb2_insn_r->arm_insn, 12, 15);
12639 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12640
12641 if (ARM_PC_REGNUM != reg_rt)
12642 {
12643 record_buf[0] = reg_rt;
12644 record_buf[1] = reg_rn;
12645 record_buf[2] = ARM_PS_REGNUM;
12646 thumb2_insn_r->reg_rec_count = 3;
12647
12648 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12649 record_buf);
12650 return ARM_RECORD_SUCCESS;
12651 }
12652
12653 return ARM_RECORD_FAILURE;
12654}
12655
12656/* Handler for thumb2 load word instructions. */
12657
12658static int
12659thumb2_record_ld_word (insn_decode_record *thumb2_insn_r)
12660{
c6ec2b30
OJ
12661 uint32_t record_buf[8];
12662
12663 record_buf[0] = bits (thumb2_insn_r->arm_insn, 12, 15);
12664 record_buf[1] = ARM_PS_REGNUM;
12665 thumb2_insn_r->reg_rec_count = 2;
12666
12667 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12668 record_buf);
12669 return ARM_RECORD_SUCCESS;
12670}
12671
12672/* Handler for thumb2 long multiply, long multiply accumulate, and
12673 divide instructions. */
12674
12675static int
12676thumb2_record_lmul_lmla_div (insn_decode_record *thumb2_insn_r)
12677{
12678 uint32_t opcode1 = 0, opcode2 = 0;
12679 uint32_t record_buf[8];
c6ec2b30
OJ
12680
12681 opcode1 = bits (thumb2_insn_r->arm_insn, 20, 22);
12682 opcode2 = bits (thumb2_insn_r->arm_insn, 4, 7);
12683
12684 if (0 == opcode1 || 2 == opcode1 || (opcode1 >= 4 && opcode1 <= 6))
12685 {
12686 /* Handle SMULL, UMULL, SMULAL. */
12687 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
12688 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
12689 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
12690 record_buf[2] = ARM_PS_REGNUM;
12691 thumb2_insn_r->reg_rec_count = 3;
12692 }
12693 else if (1 == opcode1 || 3 == opcode2)
12694 {
12695 /* Handle SDIV and UDIV. */
12696 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
12697 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
12698 record_buf[2] = ARM_PS_REGNUM;
12699 thumb2_insn_r->reg_rec_count = 3;
12700 }
12701 else
12702 return ARM_RECORD_FAILURE;
12703
12704 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12705 record_buf);
12706 return ARM_RECORD_SUCCESS;
12707}
12708
60cc5e93
OJ
12709/* Record handler for thumb32 coprocessor instructions. */
12710
12711static int
12712thumb2_record_coproc_insn (insn_decode_record *thumb2_insn_r)
12713{
12714 if (bit (thumb2_insn_r->arm_insn, 25))
12715 return arm_record_coproc_data_proc (thumb2_insn_r);
12716 else
12717 return arm_record_asimd_vfp_coproc (thumb2_insn_r);
12718}
12719
1e1b6563
OJ
12720/* Record handler for advance SIMD structure load/store instructions. */
12721
12722static int
12723thumb2_record_asimd_struct_ld_st (insn_decode_record *thumb2_insn_r)
12724{
12725 struct regcache *reg_cache = thumb2_insn_r->regcache;
12726 uint32_t l_bit, a_bit, b_bits;
12727 uint32_t record_buf[128], record_buf_mem[128];
bec2ab5a 12728 uint32_t reg_rn, reg_vd, address, f_elem;
1e1b6563
OJ
12729 uint32_t index_r = 0, index_e = 0, bf_regs = 0, index_m = 0, loop_t = 0;
12730 uint8_t f_ebytes;
12731
12732 l_bit = bit (thumb2_insn_r->arm_insn, 21);
12733 a_bit = bit (thumb2_insn_r->arm_insn, 23);
12734 b_bits = bits (thumb2_insn_r->arm_insn, 8, 11);
12735 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12736 reg_vd = bits (thumb2_insn_r->arm_insn, 12, 15);
12737 reg_vd = (bit (thumb2_insn_r->arm_insn, 22) << 4) | reg_vd;
12738 f_ebytes = (1 << bits (thumb2_insn_r->arm_insn, 6, 7));
1e1b6563
OJ
12739 f_elem = 8 / f_ebytes;
12740
12741 if (!l_bit)
12742 {
12743 ULONGEST u_regval = 0;
12744 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12745 address = u_regval;
12746
12747 if (!a_bit)
12748 {
12749 /* Handle VST1. */
12750 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
12751 {
12752 if (b_bits == 0x07)
12753 bf_regs = 1;
12754 else if (b_bits == 0x0a)
12755 bf_regs = 2;
12756 else if (b_bits == 0x06)
12757 bf_regs = 3;
12758 else if (b_bits == 0x02)
12759 bf_regs = 4;
12760 else
12761 bf_regs = 0;
12762
12763 for (index_r = 0; index_r < bf_regs; index_r++)
12764 {
12765 for (index_e = 0; index_e < f_elem; index_e++)
12766 {
12767 record_buf_mem[index_m++] = f_ebytes;
12768 record_buf_mem[index_m++] = address;
12769 address = address + f_ebytes;
12770 thumb2_insn_r->mem_rec_count += 1;
12771 }
12772 }
12773 }
12774 /* Handle VST2. */
12775 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
12776 {
12777 if (b_bits == 0x09 || b_bits == 0x08)
12778 bf_regs = 1;
12779 else if (b_bits == 0x03)
12780 bf_regs = 2;
12781 else
12782 bf_regs = 0;
12783
12784 for (index_r = 0; index_r < bf_regs; index_r++)
12785 for (index_e = 0; index_e < f_elem; index_e++)
12786 {
12787 for (loop_t = 0; loop_t < 2; loop_t++)
12788 {
12789 record_buf_mem[index_m++] = f_ebytes;
12790 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
12791 thumb2_insn_r->mem_rec_count += 1;
12792 }
12793 address = address + (2 * f_ebytes);
12794 }
12795 }
12796 /* Handle VST3. */
12797 else if ((b_bits & 0x0e) == 0x04)
12798 {
12799 for (index_e = 0; index_e < f_elem; index_e++)
12800 {
12801 for (loop_t = 0; loop_t < 3; loop_t++)
12802 {
12803 record_buf_mem[index_m++] = f_ebytes;
12804 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
12805 thumb2_insn_r->mem_rec_count += 1;
12806 }
12807 address = address + (3 * f_ebytes);
12808 }
12809 }
12810 /* Handle VST4. */
12811 else if (!(b_bits & 0x0e))
12812 {
12813 for (index_e = 0; index_e < f_elem; index_e++)
12814 {
12815 for (loop_t = 0; loop_t < 4; loop_t++)
12816 {
12817 record_buf_mem[index_m++] = f_ebytes;
12818 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
12819 thumb2_insn_r->mem_rec_count += 1;
12820 }
12821 address = address + (4 * f_ebytes);
12822 }
12823 }
12824 }
12825 else
12826 {
12827 uint8_t bft_size = bits (thumb2_insn_r->arm_insn, 10, 11);
12828
12829 if (bft_size == 0x00)
12830 f_ebytes = 1;
12831 else if (bft_size == 0x01)
12832 f_ebytes = 2;
12833 else if (bft_size == 0x02)
12834 f_ebytes = 4;
12835 else
12836 f_ebytes = 0;
12837
12838 /* Handle VST1. */
12839 if (!(b_bits & 0x0b) || b_bits == 0x08)
12840 thumb2_insn_r->mem_rec_count = 1;
12841 /* Handle VST2. */
12842 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09)
12843 thumb2_insn_r->mem_rec_count = 2;
12844 /* Handle VST3. */
12845 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a)
12846 thumb2_insn_r->mem_rec_count = 3;
12847 /* Handle VST4. */
12848 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b)
12849 thumb2_insn_r->mem_rec_count = 4;
12850
12851 for (index_m = 0; index_m < thumb2_insn_r->mem_rec_count; index_m++)
12852 {
12853 record_buf_mem[index_m] = f_ebytes;
12854 record_buf_mem[index_m] = address + (index_m * f_ebytes);
12855 }
12856 }
12857 }
12858 else
12859 {
12860 if (!a_bit)
12861 {
12862 /* Handle VLD1. */
12863 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
12864 thumb2_insn_r->reg_rec_count = 1;
12865 /* Handle VLD2. */
12866 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
12867 thumb2_insn_r->reg_rec_count = 2;
12868 /* Handle VLD3. */
12869 else if ((b_bits & 0x0e) == 0x04)
12870 thumb2_insn_r->reg_rec_count = 3;
12871 /* Handle VLD4. */
12872 else if (!(b_bits & 0x0e))
12873 thumb2_insn_r->reg_rec_count = 4;
12874 }
12875 else
12876 {
12877 /* Handle VLD1. */
12878 if (!(b_bits & 0x0b) || b_bits == 0x08 || b_bits == 0x0c)
12879 thumb2_insn_r->reg_rec_count = 1;
12880 /* Handle VLD2. */
12881 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09 || b_bits == 0x0d)
12882 thumb2_insn_r->reg_rec_count = 2;
12883 /* Handle VLD3. */
12884 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a || b_bits == 0x0e)
12885 thumb2_insn_r->reg_rec_count = 3;
12886 /* Handle VLD4. */
12887 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b || b_bits == 0x0f)
12888 thumb2_insn_r->reg_rec_count = 4;
12889
12890 for (index_r = 0; index_r < thumb2_insn_r->reg_rec_count; index_r++)
12891 record_buf[index_r] = reg_vd + ARM_D0_REGNUM + index_r;
12892 }
12893 }
12894
12895 if (bits (thumb2_insn_r->arm_insn, 0, 3) != 15)
12896 {
12897 record_buf[index_r] = reg_rn;
12898 thumb2_insn_r->reg_rec_count += 1;
12899 }
12900
12901 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12902 record_buf);
12903 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
12904 record_buf_mem);
12905 return 0;
12906}
12907
c6ec2b30
OJ
12908/* Decodes thumb2 instruction type and invokes its record handler. */
12909
12910static unsigned int
12911thumb2_record_decode_insn_handler (insn_decode_record *thumb2_insn_r)
12912{
12913 uint32_t op, op1, op2;
12914
12915 op = bit (thumb2_insn_r->arm_insn, 15);
12916 op1 = bits (thumb2_insn_r->arm_insn, 27, 28);
12917 op2 = bits (thumb2_insn_r->arm_insn, 20, 26);
12918
12919 if (op1 == 0x01)
12920 {
12921 if (!(op2 & 0x64 ))
12922 {
12923 /* Load/store multiple instruction. */
12924 return thumb2_record_ld_st_multiple (thumb2_insn_r);
12925 }
b121eeb9 12926 else if ((op2 & 0x64) == 0x4)
c6ec2b30
OJ
12927 {
12928 /* Load/store (dual/exclusive) and table branch instruction. */
12929 return thumb2_record_ld_st_dual_ex_tbb (thumb2_insn_r);
12930 }
b121eeb9 12931 else if ((op2 & 0x60) == 0x20)
c6ec2b30
OJ
12932 {
12933 /* Data-processing (shifted register). */
12934 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
12935 }
12936 else if (op2 & 0x40)
12937 {
12938 /* Co-processor instructions. */
60cc5e93 12939 return thumb2_record_coproc_insn (thumb2_insn_r);
c6ec2b30
OJ
12940 }
12941 }
12942 else if (op1 == 0x02)
12943 {
12944 if (op)
12945 {
12946 /* Branches and miscellaneous control instructions. */
12947 return thumb2_record_branch_misc_cntrl (thumb2_insn_r);
12948 }
12949 else if (op2 & 0x20)
12950 {
12951 /* Data-processing (plain binary immediate) instruction. */
12952 return thumb2_record_ps_dest_generic (thumb2_insn_r);
12953 }
12954 else
12955 {
12956 /* Data-processing (modified immediate). */
12957 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
12958 }
12959 }
12960 else if (op1 == 0x03)
12961 {
12962 if (!(op2 & 0x71 ))
12963 {
12964 /* Store single data item. */
12965 return thumb2_record_str_single_data (thumb2_insn_r);
12966 }
12967 else if (!((op2 & 0x71) ^ 0x10))
12968 {
12969 /* Advanced SIMD or structure load/store instructions. */
1e1b6563 12970 return thumb2_record_asimd_struct_ld_st (thumb2_insn_r);
c6ec2b30
OJ
12971 }
12972 else if (!((op2 & 0x67) ^ 0x01))
12973 {
12974 /* Load byte, memory hints instruction. */
12975 return thumb2_record_ld_mem_hints (thumb2_insn_r);
12976 }
12977 else if (!((op2 & 0x67) ^ 0x03))
12978 {
12979 /* Load halfword, memory hints instruction. */
12980 return thumb2_record_ld_mem_hints (thumb2_insn_r);
12981 }
12982 else if (!((op2 & 0x67) ^ 0x05))
12983 {
12984 /* Load word instruction. */
12985 return thumb2_record_ld_word (thumb2_insn_r);
12986 }
12987 else if (!((op2 & 0x70) ^ 0x20))
12988 {
12989 /* Data-processing (register) instruction. */
12990 return thumb2_record_ps_dest_generic (thumb2_insn_r);
12991 }
12992 else if (!((op2 & 0x78) ^ 0x30))
12993 {
12994 /* Multiply, multiply accumulate, abs diff instruction. */
12995 return thumb2_record_ps_dest_generic (thumb2_insn_r);
12996 }
12997 else if (!((op2 & 0x78) ^ 0x38))
12998 {
12999 /* Long multiply, long multiply accumulate, and divide. */
13000 return thumb2_record_lmul_lmla_div (thumb2_insn_r);
13001 }
13002 else if (op2 & 0x40)
13003 {
13004 /* Co-processor instructions. */
60cc5e93 13005 return thumb2_record_coproc_insn (thumb2_insn_r);
c6ec2b30
OJ
13006 }
13007 }
13008
13009 return -1;
13010}
72508ac0 13011
ffdbe864 13012namespace {
728a7913
YQ
13013/* Abstract memory reader. */
13014
13015class abstract_memory_reader
13016{
13017public:
13018 /* Read LEN bytes of target memory at address MEMADDR, placing the
13019 results in GDB's memory at BUF. Return true on success. */
13020
13021 virtual bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) = 0;
13022};
13023
13024/* Instruction reader from real target. */
13025
13026class instruction_reader : public abstract_memory_reader
13027{
13028 public:
632e107b 13029 bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) override
728a7913
YQ
13030 {
13031 if (target_read_memory (memaddr, buf, len))
13032 return false;
13033 else
13034 return true;
13035 }
13036};
13037
ffdbe864
YQ
13038} // namespace
13039
72508ac0
PO
13040/* Extracts arm/thumb/thumb2 insn depending on the size, and returns 0 on success
13041and positive val on fauilure. */
13042
13043static int
728a7913
YQ
13044extract_arm_insn (abstract_memory_reader& reader,
13045 insn_decode_record *insn_record, uint32_t insn_size)
72508ac0
PO
13046{
13047 gdb_byte buf[insn_size];
13048
13049 memset (&buf[0], 0, insn_size);
13050
728a7913 13051 if (!reader.read (insn_record->this_addr, buf, insn_size))
72508ac0
PO
13052 return 1;
13053 insn_record->arm_insn = (uint32_t) extract_unsigned_integer (&buf[0],
13054 insn_size,
2959fed9 13055 gdbarch_byte_order_for_code (insn_record->gdbarch));
72508ac0
PO
13056 return 0;
13057}
13058
13059typedef int (*sti_arm_hdl_fp_t) (insn_decode_record*);
13060
13061/* Decode arm/thumb insn depending on condition cods and opcodes; and
13062 dispatch it. */
13063
13064static int
728a7913
YQ
13065decode_insn (abstract_memory_reader &reader, insn_decode_record *arm_record,
13066 record_type_t record_type, uint32_t insn_size)
72508ac0
PO
13067{
13068
01e57735
YQ
13069 /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm
13070 instruction. */
0fa9c223 13071 static const sti_arm_hdl_fp_t arm_handle_insn[8] =
72508ac0
PO
13072 {
13073 arm_record_data_proc_misc_ld_str, /* 000. */
13074 arm_record_data_proc_imm, /* 001. */
13075 arm_record_ld_st_imm_offset, /* 010. */
13076 arm_record_ld_st_reg_offset, /* 011. */
13077 arm_record_ld_st_multiple, /* 100. */
13078 arm_record_b_bl, /* 101. */
60cc5e93 13079 arm_record_asimd_vfp_coproc, /* 110. */
72508ac0
PO
13080 arm_record_coproc_data_proc /* 111. */
13081 };
13082
01e57735
YQ
13083 /* (Starting from numerical 0); bits 13,14,15 decodes type of thumb
13084 instruction. */
0fa9c223 13085 static const sti_arm_hdl_fp_t thumb_handle_insn[8] =
72508ac0
PO
13086 { \
13087 thumb_record_shift_add_sub, /* 000. */
13088 thumb_record_add_sub_cmp_mov, /* 001. */
13089 thumb_record_ld_st_reg_offset, /* 010. */
13090 thumb_record_ld_st_imm_offset, /* 011. */
13091 thumb_record_ld_st_stack, /* 100. */
13092 thumb_record_misc, /* 101. */
13093 thumb_record_ldm_stm_swi, /* 110. */
13094 thumb_record_branch /* 111. */
13095 };
13096
13097 uint32_t ret = 0; /* return value: negative:failure 0:success. */
13098 uint32_t insn_id = 0;
13099
728a7913 13100 if (extract_arm_insn (reader, arm_record, insn_size))
72508ac0
PO
13101 {
13102 if (record_debug)
01e57735
YQ
13103 {
13104 printf_unfiltered (_("Process record: error reading memory at "
13105 "addr %s len = %d.\n"),
13106 paddress (arm_record->gdbarch,
13107 arm_record->this_addr), insn_size);
13108 }
72508ac0
PO
13109 return -1;
13110 }
13111 else if (ARM_RECORD == record_type)
13112 {
13113 arm_record->cond = bits (arm_record->arm_insn, 28, 31);
13114 insn_id = bits (arm_record->arm_insn, 25, 27);
ca92db2d
YQ
13115
13116 if (arm_record->cond == 0xf)
13117 ret = arm_record_extension_space (arm_record);
13118 else
01e57735 13119 {
ca92db2d
YQ
13120 /* If this insn has fallen into extension space
13121 then we need not decode it anymore. */
01e57735
YQ
13122 ret = arm_handle_insn[insn_id] (arm_record);
13123 }
ca92db2d
YQ
13124 if (ret != ARM_RECORD_SUCCESS)
13125 {
13126 arm_record_unsupported_insn (arm_record);
13127 ret = -1;
13128 }
72508ac0
PO
13129 }
13130 else if (THUMB_RECORD == record_type)
13131 {
13132 /* As thumb does not have condition codes, we set negative. */
13133 arm_record->cond = -1;
13134 insn_id = bits (arm_record->arm_insn, 13, 15);
13135 ret = thumb_handle_insn[insn_id] (arm_record);
ca92db2d
YQ
13136 if (ret != ARM_RECORD_SUCCESS)
13137 {
13138 arm_record_unsupported_insn (arm_record);
13139 ret = -1;
13140 }
72508ac0
PO
13141 }
13142 else if (THUMB2_RECORD == record_type)
13143 {
c6ec2b30
OJ
13144 /* As thumb does not have condition codes, we set negative. */
13145 arm_record->cond = -1;
13146
13147 /* Swap first half of 32bit thumb instruction with second half. */
13148 arm_record->arm_insn
01e57735 13149 = (arm_record->arm_insn >> 16) | (arm_record->arm_insn << 16);
c6ec2b30 13150
ca92db2d 13151 ret = thumb2_record_decode_insn_handler (arm_record);
c6ec2b30 13152
ca92db2d 13153 if (ret != ARM_RECORD_SUCCESS)
01e57735
YQ
13154 {
13155 arm_record_unsupported_insn (arm_record);
13156 ret = -1;
13157 }
72508ac0
PO
13158 }
13159 else
13160 {
13161 /* Throw assertion. */
13162 gdb_assert_not_reached ("not a valid instruction, could not decode");
13163 }
13164
13165 return ret;
13166}
13167
b121eeb9
YQ
13168#if GDB_SELF_TEST
13169namespace selftests {
13170
13171/* Provide both 16-bit and 32-bit thumb instructions. */
13172
13173class instruction_reader_thumb : public abstract_memory_reader
13174{
13175public:
13176 template<size_t SIZE>
13177 instruction_reader_thumb (enum bfd_endian endian,
13178 const uint16_t (&insns)[SIZE])
13179 : m_endian (endian), m_insns (insns), m_insns_size (SIZE)
13180 {}
13181
632e107b 13182 bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) override
b121eeb9
YQ
13183 {
13184 SELF_CHECK (len == 4 || len == 2);
13185 SELF_CHECK (memaddr % 2 == 0);
13186 SELF_CHECK ((memaddr / 2) < m_insns_size);
13187
13188 store_unsigned_integer (buf, 2, m_endian, m_insns[memaddr / 2]);
13189 if (len == 4)
13190 {
13191 store_unsigned_integer (&buf[2], 2, m_endian,
13192 m_insns[memaddr / 2 + 1]);
13193 }
13194 return true;
13195 }
13196
13197private:
13198 enum bfd_endian m_endian;
13199 const uint16_t *m_insns;
13200 size_t m_insns_size;
13201};
13202
13203static void
13204arm_record_test (void)
13205{
13206 struct gdbarch_info info;
13207 gdbarch_info_init (&info);
13208 info.bfd_arch_info = bfd_scan_arch ("arm");
13209
13210 struct gdbarch *gdbarch = gdbarch_find_by_info (info);
13211
13212 SELF_CHECK (gdbarch != NULL);
13213
13214 /* 16-bit Thumb instructions. */
13215 {
13216 insn_decode_record arm_record;
13217
13218 memset (&arm_record, 0, sizeof (insn_decode_record));
13219 arm_record.gdbarch = gdbarch;
13220
13221 static const uint16_t insns[] = {
13222 /* db b2 uxtb r3, r3 */
13223 0xb2db,
13224 /* cd 58 ldr r5, [r1, r3] */
13225 0x58cd,
13226 };
13227
13228 enum bfd_endian endian = gdbarch_byte_order_for_code (arm_record.gdbarch);
13229 instruction_reader_thumb reader (endian, insns);
13230 int ret = decode_insn (reader, &arm_record, THUMB_RECORD,
13231 THUMB_INSN_SIZE_BYTES);
13232
13233 SELF_CHECK (ret == 0);
13234 SELF_CHECK (arm_record.mem_rec_count == 0);
13235 SELF_CHECK (arm_record.reg_rec_count == 1);
13236 SELF_CHECK (arm_record.arm_regs[0] == 3);
13237
13238 arm_record.this_addr += 2;
13239 ret = decode_insn (reader, &arm_record, THUMB_RECORD,
13240 THUMB_INSN_SIZE_BYTES);
13241
13242 SELF_CHECK (ret == 0);
13243 SELF_CHECK (arm_record.mem_rec_count == 0);
13244 SELF_CHECK (arm_record.reg_rec_count == 1);
13245 SELF_CHECK (arm_record.arm_regs[0] == 5);
13246 }
13247
13248 /* 32-bit Thumb-2 instructions. */
13249 {
13250 insn_decode_record arm_record;
13251
13252 memset (&arm_record, 0, sizeof (insn_decode_record));
13253 arm_record.gdbarch = gdbarch;
13254
13255 static const uint16_t insns[] = {
13256 /* 1d ee 70 7f mrc 15, 0, r7, cr13, cr0, {3} */
13257 0xee1d, 0x7f70,
13258 };
13259
13260 enum bfd_endian endian = gdbarch_byte_order_for_code (arm_record.gdbarch);
13261 instruction_reader_thumb reader (endian, insns);
13262 int ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
13263 THUMB2_INSN_SIZE_BYTES);
13264
13265 SELF_CHECK (ret == 0);
13266 SELF_CHECK (arm_record.mem_rec_count == 0);
13267 SELF_CHECK (arm_record.reg_rec_count == 1);
13268 SELF_CHECK (arm_record.arm_regs[0] == 7);
13269 }
13270}
13271} // namespace selftests
13272#endif /* GDB_SELF_TEST */
72508ac0
PO
13273
13274/* Cleans up local record registers and memory allocations. */
13275
13276static void
13277deallocate_reg_mem (insn_decode_record *record)
13278{
13279 xfree (record->arm_regs);
13280 xfree (record->arm_mems);
13281}
13282
13283
01e57735 13284/* Parse the current instruction and record the values of the registers and
72508ac0
PO
13285 memory that will be changed in current instruction to record_arch_list".
13286 Return -1 if something is wrong. */
13287
13288int
01e57735
YQ
13289arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
13290 CORE_ADDR insn_addr)
72508ac0
PO
13291{
13292
72508ac0
PO
13293 uint32_t no_of_rec = 0;
13294 uint32_t ret = 0; /* return value: -1:record failure ; 0:success */
13295 ULONGEST t_bit = 0, insn_id = 0;
13296
13297 ULONGEST u_regval = 0;
13298
13299 insn_decode_record arm_record;
13300
13301 memset (&arm_record, 0, sizeof (insn_decode_record));
13302 arm_record.regcache = regcache;
13303 arm_record.this_addr = insn_addr;
13304 arm_record.gdbarch = gdbarch;
13305
13306
13307 if (record_debug > 1)
13308 {
13309 fprintf_unfiltered (gdb_stdlog, "Process record: arm_process_record "
01e57735 13310 "addr = %s\n",
72508ac0
PO
13311 paddress (gdbarch, arm_record.this_addr));
13312 }
13313
728a7913
YQ
13314 instruction_reader reader;
13315 if (extract_arm_insn (reader, &arm_record, 2))
72508ac0
PO
13316 {
13317 if (record_debug)
01e57735
YQ
13318 {
13319 printf_unfiltered (_("Process record: error reading memory at "
13320 "addr %s len = %d.\n"),
13321 paddress (arm_record.gdbarch,
13322 arm_record.this_addr), 2);
13323 }
72508ac0
PO
13324 return -1;
13325 }
13326
13327 /* Check the insn, whether it is thumb or arm one. */
13328
13329 t_bit = arm_psr_thumb_bit (arm_record.gdbarch);
13330 regcache_raw_read_unsigned (arm_record.regcache, ARM_PS_REGNUM, &u_regval);
13331
13332
13333 if (!(u_regval & t_bit))
13334 {
13335 /* We are decoding arm insn. */
728a7913 13336 ret = decode_insn (reader, &arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES);
72508ac0
PO
13337 }
13338 else
13339 {
13340 insn_id = bits (arm_record.arm_insn, 11, 15);
13341 /* is it thumb2 insn? */
13342 if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id))
01e57735 13343 {
728a7913 13344 ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
01e57735
YQ
13345 THUMB2_INSN_SIZE_BYTES);
13346 }
72508ac0 13347 else
01e57735
YQ
13348 {
13349 /* We are decoding thumb insn. */
728a7913
YQ
13350 ret = decode_insn (reader, &arm_record, THUMB_RECORD,
13351 THUMB_INSN_SIZE_BYTES);
01e57735 13352 }
72508ac0
PO
13353 }
13354
13355 if (0 == ret)
13356 {
13357 /* Record registers. */
25ea693b 13358 record_full_arch_list_add_reg (arm_record.regcache, ARM_PC_REGNUM);
72508ac0 13359 if (arm_record.arm_regs)
01e57735
YQ
13360 {
13361 for (no_of_rec = 0; no_of_rec < arm_record.reg_rec_count; no_of_rec++)
13362 {
13363 if (record_full_arch_list_add_reg
25ea693b 13364 (arm_record.regcache , arm_record.arm_regs[no_of_rec]))
01e57735
YQ
13365 ret = -1;
13366 }
13367 }
72508ac0
PO
13368 /* Record memories. */
13369 if (arm_record.arm_mems)
01e57735
YQ
13370 {
13371 for (no_of_rec = 0; no_of_rec < arm_record.mem_rec_count; no_of_rec++)
13372 {
13373 if (record_full_arch_list_add_mem
13374 ((CORE_ADDR)arm_record.arm_mems[no_of_rec].addr,
25ea693b 13375 arm_record.arm_mems[no_of_rec].len))
01e57735
YQ
13376 ret = -1;
13377 }
13378 }
72508ac0 13379
25ea693b 13380 if (record_full_arch_list_add_end ())
01e57735 13381 ret = -1;
72508ac0
PO
13382 }
13383
13384
13385 deallocate_reg_mem (&arm_record);
13386
13387 return ret;
13388}
This page took 2.453262 seconds and 4 git commands to generate.