PR 19051: support of inferior call with gnu vector support on ARM
[deliverable/binutils-gdb.git] / gdb / arm-tdep.c
CommitLineData
ed9a39eb 1/* Common target dependent code for GDB on ARM systems.
0fd88904 2
32d0add0 3 Copyright (C) 1988-2015 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. */
4e052eda 30#include "regcache.h"
54483882 31#include "reggroups.h"
d16aafd8 32#include "doublest.h"
fd0407d6 33#include "value.h"
34e8f22d 34#include "arch-utils.h"
4be87837 35#include "osabi.h"
eb5492fa
DJ
36#include "frame-unwind.h"
37#include "frame-base.h"
38#include "trad-frame.h"
842e1f1e
DJ
39#include "objfiles.h"
40#include "dwarf2-frame.h"
e4c16157 41#include "gdbtypes.h"
29d73ae4 42#include "prologue-value.h"
25f8c692 43#include "remote.h"
123dc839
DJ
44#include "target-descriptions.h"
45#include "user-regs.h"
0e9e9abd 46#include "observer.h"
34e8f22d 47
8689682c 48#include "arch/arm.h"
34e8f22d 49#include "arm-tdep.h"
26216b98 50#include "gdb/sim-arm.h"
34e8f22d 51
082fc60d
RE
52#include "elf-bfd.h"
53#include "coff/internal.h"
97e03143 54#include "elf/arm.h"
c906108c 55
60c5725c 56#include "vec.h"
26216b98 57
72508ac0 58#include "record.h"
d02ed0bb 59#include "record-full.h"
72508ac0 60
9779414d 61#include "features/arm-with-m.c"
25f8c692 62#include "features/arm-with-m-fpa-layout.c"
3184d3f9 63#include "features/arm-with-m-vfp-d16.c"
ef7e8358
UW
64#include "features/arm-with-iwmmxt.c"
65#include "features/arm-with-vfpv2.c"
66#include "features/arm-with-vfpv3.c"
67#include "features/arm-with-neon.c"
9779414d 68
6529d2dd
AC
69static int arm_debug;
70
082fc60d
RE
71/* Macros for setting and testing a bit in a minimal symbol that marks
72 it as Thumb function. The MSB of the minimal symbol's "info" field
f594e5e9 73 is used for this purpose.
082fc60d
RE
74
75 MSYMBOL_SET_SPECIAL Actually sets the "special" bit.
f594e5e9 76 MSYMBOL_IS_SPECIAL Tests the "special" bit in a minimal symbol. */
082fc60d 77
0963b4bd 78#define MSYMBOL_SET_SPECIAL(msym) \
b887350f 79 MSYMBOL_TARGET_FLAG_1 (msym) = 1
082fc60d
RE
80
81#define MSYMBOL_IS_SPECIAL(msym) \
b887350f 82 MSYMBOL_TARGET_FLAG_1 (msym)
082fc60d 83
60c5725c
DJ
84/* Per-objfile data used for mapping symbols. */
85static const struct objfile_data *arm_objfile_data_key;
86
87struct arm_mapping_symbol
88{
89 bfd_vma value;
90 char type;
91};
92typedef struct arm_mapping_symbol arm_mapping_symbol_s;
93DEF_VEC_O(arm_mapping_symbol_s);
94
95struct arm_per_objfile
96{
97 VEC(arm_mapping_symbol_s) **section_maps;
98};
99
afd7eef0
RE
100/* The list of available "set arm ..." and "show arm ..." commands. */
101static struct cmd_list_element *setarmcmdlist = NULL;
102static struct cmd_list_element *showarmcmdlist = NULL;
103
fd50bc42
RE
104/* The type of floating-point to use. Keep this in sync with enum
105 arm_float_model, and the help string in _initialize_arm_tdep. */
40478521 106static const char *const fp_model_strings[] =
fd50bc42
RE
107{
108 "auto",
109 "softfpa",
110 "fpa",
111 "softvfp",
28e97307
DJ
112 "vfp",
113 NULL
fd50bc42
RE
114};
115
116/* A variable that can be configured by the user. */
117static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
118static const char *current_fp_model = "auto";
119
28e97307 120/* The ABI to use. Keep this in sync with arm_abi_kind. */
40478521 121static const char *const arm_abi_strings[] =
28e97307
DJ
122{
123 "auto",
124 "APCS",
125 "AAPCS",
126 NULL
127};
128
129/* A variable that can be configured by the user. */
130static enum arm_abi_kind arm_abi_global = ARM_ABI_AUTO;
131static const char *arm_abi_string = "auto";
132
0428b8f5 133/* The execution mode to assume. */
40478521 134static const char *const arm_mode_strings[] =
0428b8f5
DJ
135 {
136 "auto",
137 "arm",
68770265
MGD
138 "thumb",
139 NULL
0428b8f5
DJ
140 };
141
142static const char *arm_fallback_mode_string = "auto";
143static const char *arm_force_mode_string = "auto";
144
18819fa6
UW
145/* Internal override of the execution mode. -1 means no override,
146 0 means override to ARM mode, 1 means override to Thumb mode.
147 The effect is the same as if arm_force_mode has been set by the
148 user (except the internal override has precedence over a user's
149 arm_force_mode override). */
150static int arm_override_mode = -1;
151
94c30b78 152/* Number of different reg name sets (options). */
afd7eef0 153static int num_disassembly_options;
bc90b915 154
f32bf4a4
YQ
155/* The standard register names, and all the valid aliases for them. Note
156 that `fp', `sp' and `pc' are not added in this alias list, because they
157 have been added as builtin user registers in
158 std-regs.c:_initialize_frame_reg. */
123dc839
DJ
159static const struct
160{
161 const char *name;
162 int regnum;
163} arm_register_aliases[] = {
164 /* Basic register numbers. */
165 { "r0", 0 },
166 { "r1", 1 },
167 { "r2", 2 },
168 { "r3", 3 },
169 { "r4", 4 },
170 { "r5", 5 },
171 { "r6", 6 },
172 { "r7", 7 },
173 { "r8", 8 },
174 { "r9", 9 },
175 { "r10", 10 },
176 { "r11", 11 },
177 { "r12", 12 },
178 { "r13", 13 },
179 { "r14", 14 },
180 { "r15", 15 },
181 /* Synonyms (argument and variable registers). */
182 { "a1", 0 },
183 { "a2", 1 },
184 { "a3", 2 },
185 { "a4", 3 },
186 { "v1", 4 },
187 { "v2", 5 },
188 { "v3", 6 },
189 { "v4", 7 },
190 { "v5", 8 },
191 { "v6", 9 },
192 { "v7", 10 },
193 { "v8", 11 },
194 /* Other platform-specific names for r9. */
195 { "sb", 9 },
196 { "tr", 9 },
197 /* Special names. */
198 { "ip", 12 },
123dc839 199 { "lr", 14 },
123dc839
DJ
200 /* Names used by GCC (not listed in the ARM EABI). */
201 { "sl", 10 },
123dc839
DJ
202 /* A special name from the older ATPCS. */
203 { "wr", 7 },
204};
bc90b915 205
123dc839 206static const char *const arm_register_names[] =
da59e081
JM
207{"r0", "r1", "r2", "r3", /* 0 1 2 3 */
208 "r4", "r5", "r6", "r7", /* 4 5 6 7 */
209 "r8", "r9", "r10", "r11", /* 8 9 10 11 */
210 "r12", "sp", "lr", "pc", /* 12 13 14 15 */
211 "f0", "f1", "f2", "f3", /* 16 17 18 19 */
212 "f4", "f5", "f6", "f7", /* 20 21 22 23 */
94c30b78 213 "fps", "cpsr" }; /* 24 25 */
ed9a39eb 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
RE
222 style. */
223static void set_disassembly_style_sfunc(char *, int,
ed9a39eb 224 struct cmd_list_element *);
afd7eef0 225static void set_disassembly_style (void);
ed9a39eb 226
b508a996 227static void convert_from_extended (const struct floatformat *, const void *,
be8626e0 228 void *, int);
b508a996 229static void convert_to_extended (const struct floatformat *, void *,
be8626e0 230 const void *, int);
ed9a39eb 231
05d1431c
PA
232static enum register_status arm_neon_quad_read (struct gdbarch *gdbarch,
233 struct regcache *regcache,
234 int regnum, gdb_byte *buf);
58d6951d
DJ
235static void arm_neon_quad_write (struct gdbarch *gdbarch,
236 struct regcache *regcache,
237 int regnum, const gdb_byte *buf);
238
9b8d791a 239struct arm_prologue_cache
c3b4394c 240{
eb5492fa
DJ
241 /* The stack pointer at the time this frame was created; i.e. the
242 caller's stack pointer when this function was called. It is used
243 to identify this frame. */
244 CORE_ADDR prev_sp;
245
4be43953
DJ
246 /* The frame base for this frame is just prev_sp - frame size.
247 FRAMESIZE is the distance from the frame pointer to the
248 initial stack pointer. */
eb5492fa 249
c3b4394c 250 int framesize;
eb5492fa
DJ
251
252 /* The register used to hold the frame pointer for this frame. */
c3b4394c 253 int framereg;
eb5492fa
DJ
254
255 /* Saved register offsets. */
256 struct trad_frame_saved_reg *saved_regs;
c3b4394c 257};
ed9a39eb 258
0d39a070
DJ
259static CORE_ADDR arm_analyze_prologue (struct gdbarch *gdbarch,
260 CORE_ADDR prologue_start,
261 CORE_ADDR prologue_end,
262 struct arm_prologue_cache *cache);
263
cca44b1b
JB
264/* Architecture version for displaced stepping. This effects the behaviour of
265 certain instructions, and really should not be hard-wired. */
266
267#define DISPLACED_STEPPING_ARCH_VERSION 5
268
94c30b78 269/* Set to true if the 32-bit mode is in use. */
c906108c
SS
270
271int arm_apcs_32 = 1;
272
9779414d
DJ
273/* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode. */
274
478fd957 275int
9779414d
DJ
276arm_psr_thumb_bit (struct gdbarch *gdbarch)
277{
278 if (gdbarch_tdep (gdbarch)->is_m)
279 return XPSR_T;
280 else
281 return CPSR_T;
282}
283
b39cc962
DJ
284/* Determine if FRAME is executing in Thumb mode. */
285
25b41d01 286int
b39cc962
DJ
287arm_frame_is_thumb (struct frame_info *frame)
288{
289 CORE_ADDR cpsr;
9779414d 290 ULONGEST t_bit = arm_psr_thumb_bit (get_frame_arch (frame));
b39cc962
DJ
291
292 /* Every ARM frame unwinder can unwind the T bit of the CPSR, either
293 directly (from a signal frame or dummy frame) or by interpreting
294 the saved LR (from a prologue or DWARF frame). So consult it and
295 trust the unwinders. */
296 cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
297
9779414d 298 return (cpsr & t_bit) != 0;
b39cc962
DJ
299}
300
60c5725c
DJ
301/* Callback for VEC_lower_bound. */
302
303static inline int
304arm_compare_mapping_symbols (const struct arm_mapping_symbol *lhs,
305 const struct arm_mapping_symbol *rhs)
306{
307 return lhs->value < rhs->value;
308}
309
f9d67f43
DJ
310/* Search for the mapping symbol covering MEMADDR. If one is found,
311 return its type. Otherwise, return 0. If START is non-NULL,
312 set *START to the location of the mapping symbol. */
c906108c 313
f9d67f43
DJ
314static char
315arm_find_mapping_symbol (CORE_ADDR memaddr, CORE_ADDR *start)
c906108c 316{
60c5725c 317 struct obj_section *sec;
0428b8f5 318
60c5725c
DJ
319 /* If there are mapping symbols, consult them. */
320 sec = find_pc_section (memaddr);
321 if (sec != NULL)
322 {
323 struct arm_per_objfile *data;
324 VEC(arm_mapping_symbol_s) *map;
aded6f54
PA
325 struct arm_mapping_symbol map_key = { memaddr - obj_section_addr (sec),
326 0 };
60c5725c
DJ
327 unsigned int idx;
328
9a3c8263
SM
329 data = (struct arm_per_objfile *) objfile_data (sec->objfile,
330 arm_objfile_data_key);
60c5725c
DJ
331 if (data != NULL)
332 {
333 map = data->section_maps[sec->the_bfd_section->index];
334 if (!VEC_empty (arm_mapping_symbol_s, map))
335 {
336 struct arm_mapping_symbol *map_sym;
337
338 idx = VEC_lower_bound (arm_mapping_symbol_s, map, &map_key,
339 arm_compare_mapping_symbols);
340
341 /* VEC_lower_bound finds the earliest ordered insertion
342 point. If the following symbol starts at this exact
343 address, we use that; otherwise, the preceding
344 mapping symbol covers this address. */
345 if (idx < VEC_length (arm_mapping_symbol_s, map))
346 {
347 map_sym = VEC_index (arm_mapping_symbol_s, map, idx);
348 if (map_sym->value == map_key.value)
f9d67f43
DJ
349 {
350 if (start)
351 *start = map_sym->value + obj_section_addr (sec);
352 return map_sym->type;
353 }
60c5725c
DJ
354 }
355
356 if (idx > 0)
357 {
358 map_sym = VEC_index (arm_mapping_symbol_s, map, idx - 1);
f9d67f43
DJ
359 if (start)
360 *start = map_sym->value + obj_section_addr (sec);
361 return map_sym->type;
60c5725c
DJ
362 }
363 }
364 }
365 }
366
f9d67f43
DJ
367 return 0;
368}
369
370/* Determine if the program counter specified in MEMADDR is in a Thumb
371 function. This function should be called for addresses unrelated to
372 any executing frame; otherwise, prefer arm_frame_is_thumb. */
373
e3039479 374int
9779414d 375arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
f9d67f43 376{
7cbd4a93 377 struct bound_minimal_symbol sym;
f9d67f43 378 char type;
a42244db
YQ
379 struct displaced_step_closure* dsc
380 = get_displaced_step_closure_by_addr(memaddr);
381
382 /* If checking the mode of displaced instruction in copy area, the mode
383 should be determined by instruction on the original address. */
384 if (dsc)
385 {
386 if (debug_displaced)
387 fprintf_unfiltered (gdb_stdlog,
388 "displaced: check mode of %.8lx instead of %.8lx\n",
389 (unsigned long) dsc->insn_addr,
390 (unsigned long) memaddr);
391 memaddr = dsc->insn_addr;
392 }
f9d67f43
DJ
393
394 /* If bit 0 of the address is set, assume this is a Thumb address. */
395 if (IS_THUMB_ADDR (memaddr))
396 return 1;
397
18819fa6
UW
398 /* Respect internal mode override if active. */
399 if (arm_override_mode != -1)
400 return arm_override_mode;
401
f9d67f43
DJ
402 /* If the user wants to override the symbol table, let him. */
403 if (strcmp (arm_force_mode_string, "arm") == 0)
404 return 0;
405 if (strcmp (arm_force_mode_string, "thumb") == 0)
406 return 1;
407
9779414d
DJ
408 /* ARM v6-M and v7-M are always in Thumb mode. */
409 if (gdbarch_tdep (gdbarch)->is_m)
410 return 1;
411
f9d67f43
DJ
412 /* If there are mapping symbols, consult them. */
413 type = arm_find_mapping_symbol (memaddr, NULL);
414 if (type)
415 return type == 't';
416
ed9a39eb 417 /* Thumb functions have a "special" bit set in minimal symbols. */
c906108c 418 sym = lookup_minimal_symbol_by_pc (memaddr);
7cbd4a93
TT
419 if (sym.minsym)
420 return (MSYMBOL_IS_SPECIAL (sym.minsym));
0428b8f5
DJ
421
422 /* If the user wants to override the fallback mode, let them. */
423 if (strcmp (arm_fallback_mode_string, "arm") == 0)
424 return 0;
425 if (strcmp (arm_fallback_mode_string, "thumb") == 0)
426 return 1;
427
428 /* If we couldn't find any symbol, but we're talking to a running
429 target, then trust the current value of $cpsr. This lets
430 "display/i $pc" always show the correct mode (though if there is
431 a symbol table we will not reach here, so it still may not be
18819fa6 432 displayed in the mode it will be executed). */
0428b8f5 433 if (target_has_registers)
18819fa6 434 return arm_frame_is_thumb (get_current_frame ());
0428b8f5
DJ
435
436 /* Otherwise we're out of luck; we assume ARM. */
437 return 0;
c906108c
SS
438}
439
181c1381 440/* Remove useless bits from addresses in a running program. */
34e8f22d 441static CORE_ADDR
24568a2c 442arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
c906108c 443{
2ae28aa9
YQ
444 /* On M-profile devices, do not strip the low bit from EXC_RETURN
445 (the magic exception return address). */
446 if (gdbarch_tdep (gdbarch)->is_m
447 && (val & 0xfffffff0) == 0xfffffff0)
448 return val;
449
a3a2ee65 450 if (arm_apcs_32)
dd6be234 451 return UNMAKE_THUMB_ADDR (val);
c906108c 452 else
a3a2ee65 453 return (val & 0x03fffffc);
c906108c
SS
454}
455
0d39a070 456/* Return 1 if PC is the start of a compiler helper function which
e0634ccf
UW
457 can be safely ignored during prologue skipping. IS_THUMB is true
458 if the function is known to be a Thumb function due to the way it
459 is being called. */
0d39a070 460static int
e0634ccf 461skip_prologue_function (struct gdbarch *gdbarch, CORE_ADDR pc, int is_thumb)
0d39a070 462{
e0634ccf 463 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7cbd4a93 464 struct bound_minimal_symbol msym;
0d39a070
DJ
465
466 msym = lookup_minimal_symbol_by_pc (pc);
7cbd4a93 467 if (msym.minsym != NULL
77e371c0 468 && BMSYMBOL_VALUE_ADDRESS (msym) == pc
efd66ac6 469 && MSYMBOL_LINKAGE_NAME (msym.minsym) != NULL)
e0634ccf 470 {
efd66ac6 471 const char *name = MSYMBOL_LINKAGE_NAME (msym.minsym);
0d39a070 472
e0634ccf
UW
473 /* The GNU linker's Thumb call stub to foo is named
474 __foo_from_thumb. */
475 if (strstr (name, "_from_thumb") != NULL)
476 name += 2;
0d39a070 477
e0634ccf
UW
478 /* On soft-float targets, __truncdfsf2 is called to convert promoted
479 arguments to their argument types in non-prototyped
480 functions. */
61012eef 481 if (startswith (name, "__truncdfsf2"))
e0634ccf 482 return 1;
61012eef 483 if (startswith (name, "__aeabi_d2f"))
e0634ccf 484 return 1;
0d39a070 485
e0634ccf 486 /* Internal functions related to thread-local storage. */
61012eef 487 if (startswith (name, "__tls_get_addr"))
e0634ccf 488 return 1;
61012eef 489 if (startswith (name, "__aeabi_read_tp"))
e0634ccf
UW
490 return 1;
491 }
492 else
493 {
494 /* If we run against a stripped glibc, we may be unable to identify
495 special functions by name. Check for one important case,
496 __aeabi_read_tp, by comparing the *code* against the default
497 implementation (this is hand-written ARM assembler in glibc). */
498
499 if (!is_thumb
500 && read_memory_unsigned_integer (pc, 4, byte_order_for_code)
501 == 0xe3e00a0f /* mov r0, #0xffff0fff */
502 && read_memory_unsigned_integer (pc + 4, 4, byte_order_for_code)
503 == 0xe240f01f) /* sub pc, r0, #31 */
504 return 1;
505 }
ec3d575a 506
0d39a070
DJ
507 return 0;
508}
509
510/* Support routines for instruction parsing. */
511#define submask(x) ((1L << ((x) + 1)) - 1)
512#define bit(obj,st) (((obj) >> (st)) & 1)
513#define bits(obj,st,fn) (((obj) >> (st)) & submask ((fn) - (st)))
514#define sbits(obj,st,fn) \
515 ((long) (bits(obj,st,fn) | ((long) bit(obj,fn) * ~ submask (fn - st))))
516#define BranchDest(addr,instr) \
9991b207 517 ((CORE_ADDR) (((unsigned long) (addr)) + 8 + (sbits (instr, 0, 23) << 2)))
0d39a070 518
621c6d5b
YQ
519/* Extract the immediate from instruction movw/movt of encoding T. INSN1 is
520 the first 16-bit of instruction, and INSN2 is the second 16-bit of
521 instruction. */
522#define EXTRACT_MOVW_MOVT_IMM_T(insn1, insn2) \
523 ((bits ((insn1), 0, 3) << 12) \
524 | (bits ((insn1), 10, 10) << 11) \
525 | (bits ((insn2), 12, 14) << 8) \
526 | bits ((insn2), 0, 7))
527
528/* Extract the immediate from instruction movw/movt of encoding A. INSN is
529 the 32-bit instruction. */
530#define EXTRACT_MOVW_MOVT_IMM_A(insn) \
531 ((bits ((insn), 16, 19) << 12) \
532 | bits ((insn), 0, 11))
533
ec3d575a
UW
534/* Decode immediate value; implements ThumbExpandImmediate pseudo-op. */
535
536static unsigned int
537thumb_expand_immediate (unsigned int imm)
538{
539 unsigned int count = imm >> 7;
540
541 if (count < 8)
542 switch (count / 2)
543 {
544 case 0:
545 return imm & 0xff;
546 case 1:
547 return (imm & 0xff) | ((imm & 0xff) << 16);
548 case 2:
549 return ((imm & 0xff) << 8) | ((imm & 0xff) << 24);
550 case 3:
551 return (imm & 0xff) | ((imm & 0xff) << 8)
552 | ((imm & 0xff) << 16) | ((imm & 0xff) << 24);
553 }
554
555 return (0x80 | (imm & 0x7f)) << (32 - count);
556}
557
558/* Return 1 if the 16-bit Thumb instruction INST might change
559 control flow, 0 otherwise. */
560
561static int
562thumb_instruction_changes_pc (unsigned short inst)
563{
564 if ((inst & 0xff00) == 0xbd00) /* pop {rlist, pc} */
565 return 1;
566
567 if ((inst & 0xf000) == 0xd000) /* conditional branch */
568 return 1;
569
570 if ((inst & 0xf800) == 0xe000) /* unconditional branch */
571 return 1;
572
573 if ((inst & 0xff00) == 0x4700) /* bx REG, blx REG */
574 return 1;
575
ad8b5167
UW
576 if ((inst & 0xff87) == 0x4687) /* mov pc, REG */
577 return 1;
578
ec3d575a
UW
579 if ((inst & 0xf500) == 0xb100) /* CBNZ or CBZ. */
580 return 1;
581
582 return 0;
583}
584
585/* Return 1 if the 32-bit Thumb instruction in INST1 and INST2
586 might change control flow, 0 otherwise. */
587
588static int
589thumb2_instruction_changes_pc (unsigned short inst1, unsigned short inst2)
590{
591 if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
592 {
593 /* Branches and miscellaneous control instructions. */
594
595 if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
596 {
597 /* B, BL, BLX. */
598 return 1;
599 }
600 else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
601 {
602 /* SUBS PC, LR, #imm8. */
603 return 1;
604 }
605 else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
606 {
607 /* Conditional branch. */
608 return 1;
609 }
610
611 return 0;
612 }
613
614 if ((inst1 & 0xfe50) == 0xe810)
615 {
616 /* Load multiple or RFE. */
617
618 if (bit (inst1, 7) && !bit (inst1, 8))
619 {
620 /* LDMIA or POP */
621 if (bit (inst2, 15))
622 return 1;
623 }
624 else if (!bit (inst1, 7) && bit (inst1, 8))
625 {
626 /* LDMDB */
627 if (bit (inst2, 15))
628 return 1;
629 }
630 else if (bit (inst1, 7) && bit (inst1, 8))
631 {
632 /* RFEIA */
633 return 1;
634 }
635 else if (!bit (inst1, 7) && !bit (inst1, 8))
636 {
637 /* RFEDB */
638 return 1;
639 }
640
641 return 0;
642 }
643
644 if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
645 {
646 /* MOV PC or MOVS PC. */
647 return 1;
648 }
649
650 if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
651 {
652 /* LDR PC. */
653 if (bits (inst1, 0, 3) == 15)
654 return 1;
655 if (bit (inst1, 7))
656 return 1;
657 if (bit (inst2, 11))
658 return 1;
659 if ((inst2 & 0x0fc0) == 0x0000)
660 return 1;
661
662 return 0;
663 }
664
665 if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
666 {
667 /* TBB. */
668 return 1;
669 }
670
671 if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
672 {
673 /* TBH. */
674 return 1;
675 }
676
677 return 0;
678}
679
540314bd
YQ
680/* Return 1 if the 16-bit Thumb instruction INSN restores SP in
681 epilogue, 0 otherwise. */
682
683static int
684thumb_instruction_restores_sp (unsigned short insn)
685{
686 return (insn == 0x46bd /* mov sp, r7 */
687 || (insn & 0xff80) == 0xb000 /* add sp, imm */
688 || (insn & 0xfe00) == 0xbc00); /* pop <registers> */
689}
690
29d73ae4
DJ
691/* Analyze a Thumb prologue, looking for a recognizable stack frame
692 and frame pointer. Scan until we encounter a store that could
0d39a070
DJ
693 clobber the stack frame unexpectedly, or an unknown instruction.
694 Return the last address which is definitely safe to skip for an
695 initial breakpoint. */
c906108c
SS
696
697static CORE_ADDR
29d73ae4
DJ
698thumb_analyze_prologue (struct gdbarch *gdbarch,
699 CORE_ADDR start, CORE_ADDR limit,
700 struct arm_prologue_cache *cache)
c906108c 701{
0d39a070 702 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
e17a4113 703 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
29d73ae4
DJ
704 int i;
705 pv_t regs[16];
706 struct pv_area *stack;
707 struct cleanup *back_to;
708 CORE_ADDR offset;
ec3d575a 709 CORE_ADDR unrecognized_pc = 0;
da3c6d4a 710
29d73ae4
DJ
711 for (i = 0; i < 16; i++)
712 regs[i] = pv_register (i, 0);
55f960e1 713 stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
29d73ae4
DJ
714 back_to = make_cleanup_free_pv_area (stack);
715
29d73ae4 716 while (start < limit)
c906108c 717 {
29d73ae4
DJ
718 unsigned short insn;
719
e17a4113 720 insn = read_memory_unsigned_integer (start, 2, byte_order_for_code);
9d4fde75 721
94c30b78 722 if ((insn & 0xfe00) == 0xb400) /* push { rlist } */
da59e081 723 {
29d73ae4
DJ
724 int regno;
725 int mask;
4be43953
DJ
726
727 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
728 break;
29d73ae4
DJ
729
730 /* Bits 0-7 contain a mask for registers R0-R7. Bit 8 says
731 whether to save LR (R14). */
732 mask = (insn & 0xff) | ((insn & 0x100) << 6);
733
734 /* Calculate offsets of saved R0-R7 and LR. */
735 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
736 if (mask & (1 << regno))
737 {
29d73ae4
DJ
738 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
739 -4);
740 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
741 }
da59e081 742 }
1db01f22 743 else if ((insn & 0xff80) == 0xb080) /* sub sp, #imm */
da59e081 744 {
29d73ae4 745 offset = (insn & 0x7f) << 2; /* get scaled offset */
1db01f22
YQ
746 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
747 -offset);
da59e081 748 }
808f7ab1
YQ
749 else if (thumb_instruction_restores_sp (insn))
750 {
751 /* Don't scan past the epilogue. */
752 break;
753 }
0d39a070
DJ
754 else if ((insn & 0xf800) == 0xa800) /* add Rd, sp, #imm */
755 regs[bits (insn, 8, 10)] = pv_add_constant (regs[ARM_SP_REGNUM],
756 (insn & 0xff) << 2);
757 else if ((insn & 0xfe00) == 0x1c00 /* add Rd, Rn, #imm */
758 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
759 regs[bits (insn, 0, 2)] = pv_add_constant (regs[bits (insn, 3, 5)],
760 bits (insn, 6, 8));
761 else if ((insn & 0xf800) == 0x3000 /* add Rd, #imm */
762 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
763 regs[bits (insn, 8, 10)] = pv_add_constant (regs[bits (insn, 8, 10)],
764 bits (insn, 0, 7));
765 else if ((insn & 0xfe00) == 0x1800 /* add Rd, Rn, Rm */
766 && pv_is_register (regs[bits (insn, 6, 8)], ARM_SP_REGNUM)
767 && pv_is_constant (regs[bits (insn, 3, 5)]))
768 regs[bits (insn, 0, 2)] = pv_add (regs[bits (insn, 3, 5)],
769 regs[bits (insn, 6, 8)]);
770 else if ((insn & 0xff00) == 0x4400 /* add Rd, Rm */
771 && pv_is_constant (regs[bits (insn, 3, 6)]))
772 {
773 int rd = (bit (insn, 7) << 3) + bits (insn, 0, 2);
774 int rm = bits (insn, 3, 6);
775 regs[rd] = pv_add (regs[rd], regs[rm]);
776 }
29d73ae4 777 else if ((insn & 0xff00) == 0x4600) /* mov hi, lo or mov lo, hi */
da59e081 778 {
29d73ae4
DJ
779 int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
780 int src_reg = (insn & 0x78) >> 3;
781 regs[dst_reg] = regs[src_reg];
da59e081 782 }
29d73ae4 783 else if ((insn & 0xf800) == 0x9000) /* str rd, [sp, #off] */
da59e081 784 {
29d73ae4
DJ
785 /* Handle stores to the stack. Normally pushes are used,
786 but with GCC -mtpcs-frame, there may be other stores
787 in the prologue to create the frame. */
788 int regno = (insn >> 8) & 0x7;
789 pv_t addr;
790
791 offset = (insn & 0xff) << 2;
792 addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
793
794 if (pv_area_store_would_trash (stack, addr))
795 break;
796
797 pv_area_store (stack, addr, 4, regs[regno]);
da59e081 798 }
0d39a070
DJ
799 else if ((insn & 0xf800) == 0x6000) /* str rd, [rn, #off] */
800 {
801 int rd = bits (insn, 0, 2);
802 int rn = bits (insn, 3, 5);
803 pv_t addr;
804
805 offset = bits (insn, 6, 10) << 2;
806 addr = pv_add_constant (regs[rn], offset);
807
808 if (pv_area_store_would_trash (stack, addr))
809 break;
810
811 pv_area_store (stack, addr, 4, regs[rd]);
812 }
813 else if (((insn & 0xf800) == 0x7000 /* strb Rd, [Rn, #off] */
814 || (insn & 0xf800) == 0x8000) /* strh Rd, [Rn, #off] */
815 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
816 /* Ignore stores of argument registers to the stack. */
817 ;
818 else if ((insn & 0xf800) == 0xc800 /* ldmia Rn!, { registers } */
819 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
820 /* Ignore block loads from the stack, potentially copying
821 parameters from memory. */
822 ;
823 else if ((insn & 0xf800) == 0x9800 /* ldr Rd, [Rn, #immed] */
824 || ((insn & 0xf800) == 0x6800 /* ldr Rd, [sp, #immed] */
825 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM)))
826 /* Similarly ignore single loads from the stack. */
827 ;
828 else if ((insn & 0xffc0) == 0x0000 /* lsls Rd, Rm, #0 */
829 || (insn & 0xffc0) == 0x1c00) /* add Rd, Rn, #0 */
830 /* Skip register copies, i.e. saves to another register
831 instead of the stack. */
832 ;
833 else if ((insn & 0xf800) == 0x2000) /* movs Rd, #imm */
834 /* Recognize constant loads; even with small stacks these are necessary
835 on Thumb. */
836 regs[bits (insn, 8, 10)] = pv_constant (bits (insn, 0, 7));
837 else if ((insn & 0xf800) == 0x4800) /* ldr Rd, [pc, #imm] */
838 {
839 /* Constant pool loads, for the same reason. */
840 unsigned int constant;
841 CORE_ADDR loc;
842
843 loc = start + 4 + bits (insn, 0, 7) * 4;
844 constant = read_memory_unsigned_integer (loc, 4, byte_order);
845 regs[bits (insn, 8, 10)] = pv_constant (constant);
846 }
db24da6d 847 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instructions. */
0d39a070 848 {
0d39a070
DJ
849 unsigned short inst2;
850
851 inst2 = read_memory_unsigned_integer (start + 2, 2,
852 byte_order_for_code);
853
854 if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
855 {
856 /* BL, BLX. Allow some special function calls when
857 skipping the prologue; GCC generates these before
858 storing arguments to the stack. */
859 CORE_ADDR nextpc;
860 int j1, j2, imm1, imm2;
861
862 imm1 = sbits (insn, 0, 10);
863 imm2 = bits (inst2, 0, 10);
864 j1 = bit (inst2, 13);
865 j2 = bit (inst2, 11);
866
867 offset = ((imm1 << 12) + (imm2 << 1));
868 offset ^= ((!j2) << 22) | ((!j1) << 23);
869
870 nextpc = start + 4 + offset;
871 /* For BLX make sure to clear the low bits. */
872 if (bit (inst2, 12) == 0)
873 nextpc = nextpc & 0xfffffffc;
874
e0634ccf
UW
875 if (!skip_prologue_function (gdbarch, nextpc,
876 bit (inst2, 12) != 0))
0d39a070
DJ
877 break;
878 }
ec3d575a 879
0963b4bd
MS
880 else if ((insn & 0xffd0) == 0xe900 /* stmdb Rn{!},
881 { registers } */
ec3d575a
UW
882 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
883 {
884 pv_t addr = regs[bits (insn, 0, 3)];
885 int regno;
886
887 if (pv_area_store_would_trash (stack, addr))
888 break;
889
890 /* Calculate offsets of saved registers. */
891 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
892 if (inst2 & (1 << regno))
893 {
894 addr = pv_add_constant (addr, -4);
895 pv_area_store (stack, addr, 4, regs[regno]);
896 }
897
898 if (insn & 0x0020)
899 regs[bits (insn, 0, 3)] = addr;
900 }
901
0963b4bd
MS
902 else if ((insn & 0xff50) == 0xe940 /* strd Rt, Rt2,
903 [Rn, #+/-imm]{!} */
ec3d575a
UW
904 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
905 {
906 int regno1 = bits (inst2, 12, 15);
907 int regno2 = bits (inst2, 8, 11);
908 pv_t addr = regs[bits (insn, 0, 3)];
909
910 offset = inst2 & 0xff;
911 if (insn & 0x0080)
912 addr = pv_add_constant (addr, offset);
913 else
914 addr = pv_add_constant (addr, -offset);
915
916 if (pv_area_store_would_trash (stack, addr))
917 break;
918
919 pv_area_store (stack, addr, 4, regs[regno1]);
920 pv_area_store (stack, pv_add_constant (addr, 4),
921 4, regs[regno2]);
922
923 if (insn & 0x0020)
924 regs[bits (insn, 0, 3)] = addr;
925 }
926
927 else if ((insn & 0xfff0) == 0xf8c0 /* str Rt,[Rn,+/-#imm]{!} */
928 && (inst2 & 0x0c00) == 0x0c00
929 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
930 {
931 int regno = bits (inst2, 12, 15);
932 pv_t addr = regs[bits (insn, 0, 3)];
933
934 offset = inst2 & 0xff;
935 if (inst2 & 0x0200)
936 addr = pv_add_constant (addr, offset);
937 else
938 addr = pv_add_constant (addr, -offset);
939
940 if (pv_area_store_would_trash (stack, addr))
941 break;
942
943 pv_area_store (stack, addr, 4, regs[regno]);
944
945 if (inst2 & 0x0100)
946 regs[bits (insn, 0, 3)] = addr;
947 }
948
949 else if ((insn & 0xfff0) == 0xf8c0 /* str.w Rt,[Rn,#imm] */
950 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
951 {
952 int regno = bits (inst2, 12, 15);
953 pv_t addr;
954
955 offset = inst2 & 0xfff;
956 addr = pv_add_constant (regs[bits (insn, 0, 3)], offset);
957
958 if (pv_area_store_would_trash (stack, addr))
959 break;
960
961 pv_area_store (stack, addr, 4, regs[regno]);
962 }
963
964 else if ((insn & 0xffd0) == 0xf880 /* str{bh}.w Rt,[Rn,#imm] */
0d39a070 965 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 966 /* Ignore stores of argument registers to the stack. */
0d39a070 967 ;
ec3d575a
UW
968
969 else if ((insn & 0xffd0) == 0xf800 /* str{bh} Rt,[Rn,#+/-imm] */
970 && (inst2 & 0x0d00) == 0x0c00
0d39a070 971 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 972 /* Ignore stores of argument registers to the stack. */
0d39a070 973 ;
ec3d575a 974
0963b4bd
MS
975 else if ((insn & 0xffd0) == 0xe890 /* ldmia Rn[!],
976 { registers } */
ec3d575a
UW
977 && (inst2 & 0x8000) == 0x0000
978 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
979 /* Ignore block loads from the stack, potentially copying
980 parameters from memory. */
0d39a070 981 ;
ec3d575a 982
0963b4bd
MS
983 else if ((insn & 0xffb0) == 0xe950 /* ldrd Rt, Rt2,
984 [Rn, #+/-imm] */
0d39a070 985 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 986 /* Similarly ignore dual loads from the stack. */
0d39a070 987 ;
ec3d575a
UW
988
989 else if ((insn & 0xfff0) == 0xf850 /* ldr Rt,[Rn,#+/-imm] */
990 && (inst2 & 0x0d00) == 0x0c00
0d39a070 991 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 992 /* Similarly ignore single loads from the stack. */
0d39a070 993 ;
ec3d575a
UW
994
995 else if ((insn & 0xfff0) == 0xf8d0 /* ldr.w Rt,[Rn,#imm] */
0d39a070 996 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 997 /* Similarly ignore single loads from the stack. */
0d39a070 998 ;
ec3d575a
UW
999
1000 else if ((insn & 0xfbf0) == 0xf100 /* add.w Rd, Rn, #imm */
1001 && (inst2 & 0x8000) == 0x0000)
1002 {
1003 unsigned int imm = ((bits (insn, 10, 10) << 11)
1004 | (bits (inst2, 12, 14) << 8)
1005 | bits (inst2, 0, 7));
1006
1007 regs[bits (inst2, 8, 11)]
1008 = pv_add_constant (regs[bits (insn, 0, 3)],
1009 thumb_expand_immediate (imm));
1010 }
1011
1012 else if ((insn & 0xfbf0) == 0xf200 /* addw Rd, Rn, #imm */
1013 && (inst2 & 0x8000) == 0x0000)
0d39a070 1014 {
ec3d575a
UW
1015 unsigned int imm = ((bits (insn, 10, 10) << 11)
1016 | (bits (inst2, 12, 14) << 8)
1017 | bits (inst2, 0, 7));
1018
1019 regs[bits (inst2, 8, 11)]
1020 = pv_add_constant (regs[bits (insn, 0, 3)], imm);
1021 }
1022
1023 else if ((insn & 0xfbf0) == 0xf1a0 /* sub.w Rd, Rn, #imm */
1024 && (inst2 & 0x8000) == 0x0000)
1025 {
1026 unsigned int imm = ((bits (insn, 10, 10) << 11)
1027 | (bits (inst2, 12, 14) << 8)
1028 | bits (inst2, 0, 7));
1029
1030 regs[bits (inst2, 8, 11)]
1031 = pv_add_constant (regs[bits (insn, 0, 3)],
1032 - (CORE_ADDR) thumb_expand_immediate (imm));
1033 }
1034
1035 else if ((insn & 0xfbf0) == 0xf2a0 /* subw Rd, Rn, #imm */
1036 && (inst2 & 0x8000) == 0x0000)
1037 {
1038 unsigned int imm = ((bits (insn, 10, 10) << 11)
1039 | (bits (inst2, 12, 14) << 8)
1040 | bits (inst2, 0, 7));
1041
1042 regs[bits (inst2, 8, 11)]
1043 = pv_add_constant (regs[bits (insn, 0, 3)], - (CORE_ADDR) imm);
1044 }
1045
1046 else if ((insn & 0xfbff) == 0xf04f) /* mov.w Rd, #const */
1047 {
1048 unsigned int imm = ((bits (insn, 10, 10) << 11)
1049 | (bits (inst2, 12, 14) << 8)
1050 | bits (inst2, 0, 7));
1051
1052 regs[bits (inst2, 8, 11)]
1053 = pv_constant (thumb_expand_immediate (imm));
1054 }
1055
1056 else if ((insn & 0xfbf0) == 0xf240) /* movw Rd, #const */
1057 {
621c6d5b
YQ
1058 unsigned int imm
1059 = EXTRACT_MOVW_MOVT_IMM_T (insn, inst2);
ec3d575a
UW
1060
1061 regs[bits (inst2, 8, 11)] = pv_constant (imm);
1062 }
1063
1064 else if (insn == 0xea5f /* mov.w Rd,Rm */
1065 && (inst2 & 0xf0f0) == 0)
1066 {
1067 int dst_reg = (inst2 & 0x0f00) >> 8;
1068 int src_reg = inst2 & 0xf;
1069 regs[dst_reg] = regs[src_reg];
1070 }
1071
1072 else if ((insn & 0xff7f) == 0xf85f) /* ldr.w Rt,<label> */
1073 {
1074 /* Constant pool loads. */
1075 unsigned int constant;
1076 CORE_ADDR loc;
1077
cac395ea 1078 offset = bits (inst2, 0, 11);
ec3d575a
UW
1079 if (insn & 0x0080)
1080 loc = start + 4 + offset;
1081 else
1082 loc = start + 4 - offset;
1083
1084 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1085 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1086 }
1087
1088 else if ((insn & 0xff7f) == 0xe95f) /* ldrd Rt,Rt2,<label> */
1089 {
1090 /* Constant pool loads. */
1091 unsigned int constant;
1092 CORE_ADDR loc;
1093
cac395ea 1094 offset = bits (inst2, 0, 7) << 2;
ec3d575a
UW
1095 if (insn & 0x0080)
1096 loc = start + 4 + offset;
1097 else
1098 loc = start + 4 - offset;
1099
1100 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1101 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1102
1103 constant = read_memory_unsigned_integer (loc + 4, 4, byte_order);
1104 regs[bits (inst2, 8, 11)] = pv_constant (constant);
1105 }
1106
1107 else if (thumb2_instruction_changes_pc (insn, inst2))
1108 {
1109 /* Don't scan past anything that might change control flow. */
0d39a070
DJ
1110 break;
1111 }
ec3d575a
UW
1112 else
1113 {
1114 /* The optimizer might shove anything into the prologue,
1115 so we just skip what we don't recognize. */
1116 unrecognized_pc = start;
1117 }
0d39a070
DJ
1118
1119 start += 2;
1120 }
ec3d575a 1121 else if (thumb_instruction_changes_pc (insn))
3d74b771 1122 {
ec3d575a 1123 /* Don't scan past anything that might change control flow. */
da3c6d4a 1124 break;
3d74b771 1125 }
ec3d575a
UW
1126 else
1127 {
1128 /* The optimizer might shove anything into the prologue,
1129 so we just skip what we don't recognize. */
1130 unrecognized_pc = start;
1131 }
29d73ae4
DJ
1132
1133 start += 2;
c906108c
SS
1134 }
1135
0d39a070
DJ
1136 if (arm_debug)
1137 fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1138 paddress (gdbarch, start));
1139
ec3d575a
UW
1140 if (unrecognized_pc == 0)
1141 unrecognized_pc = start;
1142
29d73ae4
DJ
1143 if (cache == NULL)
1144 {
1145 do_cleanups (back_to);
ec3d575a 1146 return unrecognized_pc;
29d73ae4
DJ
1147 }
1148
29d73ae4
DJ
1149 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1150 {
1151 /* Frame pointer is fp. Frame size is constant. */
1152 cache->framereg = ARM_FP_REGNUM;
1153 cache->framesize = -regs[ARM_FP_REGNUM].k;
1154 }
1155 else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
1156 {
1157 /* Frame pointer is r7. Frame size is constant. */
1158 cache->framereg = THUMB_FP_REGNUM;
1159 cache->framesize = -regs[THUMB_FP_REGNUM].k;
1160 }
72a2e3dc 1161 else
29d73ae4
DJ
1162 {
1163 /* Try the stack pointer... this is a bit desperate. */
1164 cache->framereg = ARM_SP_REGNUM;
1165 cache->framesize = -regs[ARM_SP_REGNUM].k;
1166 }
29d73ae4
DJ
1167
1168 for (i = 0; i < 16; i++)
1169 if (pv_area_find_reg (stack, gdbarch, i, &offset))
1170 cache->saved_regs[i].addr = offset;
1171
1172 do_cleanups (back_to);
ec3d575a 1173 return unrecognized_pc;
c906108c
SS
1174}
1175
621c6d5b
YQ
1176
1177/* Try to analyze the instructions starting from PC, which load symbol
1178 __stack_chk_guard. Return the address of instruction after loading this
1179 symbol, set the dest register number to *BASEREG, and set the size of
1180 instructions for loading symbol in OFFSET. Return 0 if instructions are
1181 not recognized. */
1182
1183static CORE_ADDR
1184arm_analyze_load_stack_chk_guard(CORE_ADDR pc, struct gdbarch *gdbarch,
1185 unsigned int *destreg, int *offset)
1186{
1187 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1188 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1189 unsigned int low, high, address;
1190
1191 address = 0;
1192 if (is_thumb)
1193 {
1194 unsigned short insn1
1195 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
1196
1197 if ((insn1 & 0xf800) == 0x4800) /* ldr Rd, #immed */
1198 {
1199 *destreg = bits (insn1, 8, 10);
1200 *offset = 2;
6ae274b7
YQ
1201 address = (pc & 0xfffffffc) + 4 + (bits (insn1, 0, 7) << 2);
1202 address = read_memory_unsigned_integer (address, 4,
1203 byte_order_for_code);
621c6d5b
YQ
1204 }
1205 else if ((insn1 & 0xfbf0) == 0xf240) /* movw Rd, #const */
1206 {
1207 unsigned short insn2
1208 = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);
1209
1210 low = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1211
1212 insn1
1213 = read_memory_unsigned_integer (pc + 4, 2, byte_order_for_code);
1214 insn2
1215 = read_memory_unsigned_integer (pc + 6, 2, byte_order_for_code);
1216
1217 /* movt Rd, #const */
1218 if ((insn1 & 0xfbc0) == 0xf2c0)
1219 {
1220 high = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1221 *destreg = bits (insn2, 8, 11);
1222 *offset = 8;
1223 address = (high << 16 | low);
1224 }
1225 }
1226 }
1227 else
1228 {
2e9e421f
UW
1229 unsigned int insn
1230 = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
1231
6ae274b7 1232 if ((insn & 0x0e5f0000) == 0x041f0000) /* ldr Rd, [PC, #immed] */
2e9e421f 1233 {
6ae274b7
YQ
1234 address = bits (insn, 0, 11) + pc + 8;
1235 address = read_memory_unsigned_integer (address, 4,
1236 byte_order_for_code);
1237
2e9e421f
UW
1238 *destreg = bits (insn, 12, 15);
1239 *offset = 4;
1240 }
1241 else if ((insn & 0x0ff00000) == 0x03000000) /* movw Rd, #const */
1242 {
1243 low = EXTRACT_MOVW_MOVT_IMM_A (insn);
1244
1245 insn
1246 = read_memory_unsigned_integer (pc + 4, 4, byte_order_for_code);
1247
1248 if ((insn & 0x0ff00000) == 0x03400000) /* movt Rd, #const */
1249 {
1250 high = EXTRACT_MOVW_MOVT_IMM_A (insn);
1251 *destreg = bits (insn, 12, 15);
1252 *offset = 8;
1253 address = (high << 16 | low);
1254 }
1255 }
621c6d5b
YQ
1256 }
1257
1258 return address;
1259}
1260
1261/* Try to skip a sequence of instructions used for stack protector. If PC
0963b4bd
MS
1262 points to the first instruction of this sequence, return the address of
1263 first instruction after this sequence, otherwise, return original PC.
621c6d5b
YQ
1264
1265 On arm, this sequence of instructions is composed of mainly three steps,
1266 Step 1: load symbol __stack_chk_guard,
1267 Step 2: load from address of __stack_chk_guard,
1268 Step 3: store it to somewhere else.
1269
1270 Usually, instructions on step 2 and step 3 are the same on various ARM
1271 architectures. On step 2, it is one instruction 'ldr Rx, [Rn, #0]', and
1272 on step 3, it is also one instruction 'str Rx, [r7, #immd]'. However,
1273 instructions in step 1 vary from different ARM architectures. On ARMv7,
1274 they are,
1275
1276 movw Rn, #:lower16:__stack_chk_guard
1277 movt Rn, #:upper16:__stack_chk_guard
1278
1279 On ARMv5t, it is,
1280
1281 ldr Rn, .Label
1282 ....
1283 .Lable:
1284 .word __stack_chk_guard
1285
1286 Since ldr/str is a very popular instruction, we can't use them as
1287 'fingerprint' or 'signature' of stack protector sequence. Here we choose
1288 sequence {movw/movt, ldr}/ldr/str plus symbol __stack_chk_guard, if not
1289 stripped, as the 'fingerprint' of a stack protector cdoe sequence. */
1290
1291static CORE_ADDR
1292arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
1293{
1294 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
22e048c9 1295 unsigned int basereg;
7cbd4a93 1296 struct bound_minimal_symbol stack_chk_guard;
621c6d5b
YQ
1297 int offset;
1298 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1299 CORE_ADDR addr;
1300
1301 /* Try to parse the instructions in Step 1. */
1302 addr = arm_analyze_load_stack_chk_guard (pc, gdbarch,
1303 &basereg, &offset);
1304 if (!addr)
1305 return pc;
1306
1307 stack_chk_guard = lookup_minimal_symbol_by_pc (addr);
6041179a
JB
1308 /* ADDR must correspond to a symbol whose name is __stack_chk_guard.
1309 Otherwise, this sequence cannot be for stack protector. */
1310 if (stack_chk_guard.minsym == NULL
61012eef 1311 || !startswith (MSYMBOL_LINKAGE_NAME (stack_chk_guard.minsym), "__stack_chk_guard"))
621c6d5b
YQ
1312 return pc;
1313
1314 if (is_thumb)
1315 {
1316 unsigned int destreg;
1317 unsigned short insn
1318 = read_memory_unsigned_integer (pc + offset, 2, byte_order_for_code);
1319
1320 /* Step 2: ldr Rd, [Rn, #immed], encoding T1. */
1321 if ((insn & 0xf800) != 0x6800)
1322 return pc;
1323 if (bits (insn, 3, 5) != basereg)
1324 return pc;
1325 destreg = bits (insn, 0, 2);
1326
1327 insn = read_memory_unsigned_integer (pc + offset + 2, 2,
1328 byte_order_for_code);
1329 /* Step 3: str Rd, [Rn, #immed], encoding T1. */
1330 if ((insn & 0xf800) != 0x6000)
1331 return pc;
1332 if (destreg != bits (insn, 0, 2))
1333 return pc;
1334 }
1335 else
1336 {
1337 unsigned int destreg;
1338 unsigned int insn
1339 = read_memory_unsigned_integer (pc + offset, 4, byte_order_for_code);
1340
1341 /* Step 2: ldr Rd, [Rn, #immed], encoding A1. */
1342 if ((insn & 0x0e500000) != 0x04100000)
1343 return pc;
1344 if (bits (insn, 16, 19) != basereg)
1345 return pc;
1346 destreg = bits (insn, 12, 15);
1347 /* Step 3: str Rd, [Rn, #immed], encoding A1. */
1348 insn = read_memory_unsigned_integer (pc + offset + 4,
1349 4, byte_order_for_code);
1350 if ((insn & 0x0e500000) != 0x04000000)
1351 return pc;
1352 if (bits (insn, 12, 15) != destreg)
1353 return pc;
1354 }
1355 /* The size of total two instructions ldr/str is 4 on Thumb-2, while 8
1356 on arm. */
1357 if (is_thumb)
1358 return pc + offset + 4;
1359 else
1360 return pc + offset + 8;
1361}
1362
da3c6d4a
MS
1363/* Advance the PC across any function entry prologue instructions to
1364 reach some "real" code.
34e8f22d
RE
1365
1366 The APCS (ARM Procedure Call Standard) defines the following
ed9a39eb 1367 prologue:
c906108c 1368
c5aa993b
JM
1369 mov ip, sp
1370 [stmfd sp!, {a1,a2,a3,a4}]
1371 stmfd sp!, {...,fp,ip,lr,pc}
ed9a39eb
JM
1372 [stfe f7, [sp, #-12]!]
1373 [stfe f6, [sp, #-12]!]
1374 [stfe f5, [sp, #-12]!]
1375 [stfe f4, [sp, #-12]!]
0963b4bd 1376 sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn. */
c906108c 1377
34e8f22d 1378static CORE_ADDR
6093d2eb 1379arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
c906108c 1380{
e17a4113 1381 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
c906108c 1382 unsigned long inst;
a89fea3c 1383 CORE_ADDR func_addr, limit_pc;
c906108c 1384
a89fea3c
JL
1385 /* See if we can determine the end of the prologue via the symbol table.
1386 If so, then return either PC, or the PC after the prologue, whichever
1387 is greater. */
1388 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
c906108c 1389 {
d80b854b
UW
1390 CORE_ADDR post_prologue_pc
1391 = skip_prologue_using_sal (gdbarch, func_addr);
43f3e411 1392 struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);
0d39a070 1393
621c6d5b
YQ
1394 if (post_prologue_pc)
1395 post_prologue_pc
1396 = arm_skip_stack_protector (post_prologue_pc, gdbarch);
1397
1398
0d39a070
DJ
1399 /* GCC always emits a line note before the prologue and another
1400 one after, even if the two are at the same address or on the
1401 same line. Take advantage of this so that we do not need to
1402 know every instruction that might appear in the prologue. We
1403 will have producer information for most binaries; if it is
1404 missing (e.g. for -gstabs), assuming the GNU tools. */
1405 if (post_prologue_pc
43f3e411
DE
1406 && (cust == NULL
1407 || COMPUNIT_PRODUCER (cust) == NULL
61012eef
GB
1408 || startswith (COMPUNIT_PRODUCER (cust), "GNU ")
1409 || startswith (COMPUNIT_PRODUCER (cust), "clang ")))
0d39a070
DJ
1410 return post_prologue_pc;
1411
a89fea3c 1412 if (post_prologue_pc != 0)
0d39a070
DJ
1413 {
1414 CORE_ADDR analyzed_limit;
1415
1416 /* For non-GCC compilers, make sure the entire line is an
1417 acceptable prologue; GDB will round this function's
1418 return value up to the end of the following line so we
1419 can not skip just part of a line (and we do not want to).
1420
1421 RealView does not treat the prologue specially, but does
1422 associate prologue code with the opening brace; so this
1423 lets us skip the first line if we think it is the opening
1424 brace. */
9779414d 1425 if (arm_pc_is_thumb (gdbarch, func_addr))
0d39a070
DJ
1426 analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
1427 post_prologue_pc, NULL);
1428 else
1429 analyzed_limit = arm_analyze_prologue (gdbarch, func_addr,
1430 post_prologue_pc, NULL);
1431
1432 if (analyzed_limit != post_prologue_pc)
1433 return func_addr;
1434
1435 return post_prologue_pc;
1436 }
c906108c
SS
1437 }
1438
a89fea3c
JL
1439 /* Can't determine prologue from the symbol table, need to examine
1440 instructions. */
c906108c 1441
a89fea3c
JL
1442 /* Find an upper limit on the function prologue using the debug
1443 information. If the debug information could not be used to provide
1444 that bound, then use an arbitrary large number as the upper bound. */
0963b4bd 1445 /* Like arm_scan_prologue, stop no later than pc + 64. */
d80b854b 1446 limit_pc = skip_prologue_using_sal (gdbarch, pc);
a89fea3c
JL
1447 if (limit_pc == 0)
1448 limit_pc = pc + 64; /* Magic. */
1449
c906108c 1450
29d73ae4 1451 /* Check if this is Thumb code. */
9779414d 1452 if (arm_pc_is_thumb (gdbarch, pc))
a89fea3c 1453 return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
21daaaaf
YQ
1454 else
1455 return arm_analyze_prologue (gdbarch, pc, limit_pc, NULL);
c906108c 1456}
94c30b78 1457
c5aa993b 1458/* *INDENT-OFF* */
c906108c
SS
1459/* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
1460 This function decodes a Thumb function prologue to determine:
1461 1) the size of the stack frame
1462 2) which registers are saved on it
1463 3) the offsets of saved regs
1464 4) the offset from the stack pointer to the frame pointer
c906108c 1465
da59e081
JM
1466 A typical Thumb function prologue would create this stack frame
1467 (offsets relative to FP)
c906108c
SS
1468 old SP -> 24 stack parameters
1469 20 LR
1470 16 R7
1471 R7 -> 0 local variables (16 bytes)
1472 SP -> -12 additional stack space (12 bytes)
1473 The frame size would thus be 36 bytes, and the frame offset would be
0963b4bd 1474 12 bytes. The frame register is R7.
da59e081 1475
da3c6d4a
MS
1476 The comments for thumb_skip_prolog() describe the algorithm we use
1477 to detect the end of the prolog. */
c5aa993b
JM
1478/* *INDENT-ON* */
1479
c906108c 1480static void
be8626e0 1481thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
b39cc962 1482 CORE_ADDR block_addr, struct arm_prologue_cache *cache)
c906108c
SS
1483{
1484 CORE_ADDR prologue_start;
1485 CORE_ADDR prologue_end;
c906108c 1486
b39cc962
DJ
1487 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1488 &prologue_end))
c906108c 1489 {
ec3d575a
UW
1490 /* See comment in arm_scan_prologue for an explanation of
1491 this heuristics. */
1492 if (prologue_end > prologue_start + 64)
1493 {
1494 prologue_end = prologue_start + 64;
1495 }
c906108c
SS
1496 }
1497 else
f7060f85
DJ
1498 /* We're in the boondocks: we have no idea where the start of the
1499 function is. */
1500 return;
c906108c 1501
eb5492fa 1502 prologue_end = min (prologue_end, prev_pc);
c906108c 1503
be8626e0 1504 thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
1505}
1506
0d39a070 1507/* Return 1 if THIS_INSTR might change control flow, 0 otherwise. */
c906108c 1508
0d39a070
DJ
1509static int
1510arm_instruction_changes_pc (uint32_t this_instr)
c906108c 1511{
0d39a070
DJ
1512 if (bits (this_instr, 28, 31) == INST_NV)
1513 /* Unconditional instructions. */
1514 switch (bits (this_instr, 24, 27))
1515 {
1516 case 0xa:
1517 case 0xb:
1518 /* Branch with Link and change to Thumb. */
1519 return 1;
1520 case 0xc:
1521 case 0xd:
1522 case 0xe:
1523 /* Coprocessor register transfer. */
1524 if (bits (this_instr, 12, 15) == 15)
1525 error (_("Invalid update to pc in instruction"));
1526 return 0;
1527 default:
1528 return 0;
1529 }
1530 else
1531 switch (bits (this_instr, 25, 27))
1532 {
1533 case 0x0:
1534 if (bits (this_instr, 23, 24) == 2 && bit (this_instr, 20) == 0)
1535 {
1536 /* Multiplies and extra load/stores. */
1537 if (bit (this_instr, 4) == 1 && bit (this_instr, 7) == 1)
1538 /* Neither multiplies nor extension load/stores are allowed
1539 to modify PC. */
1540 return 0;
1541
1542 /* Otherwise, miscellaneous instructions. */
1543
1544 /* BX <reg>, BXJ <reg>, BLX <reg> */
1545 if (bits (this_instr, 4, 27) == 0x12fff1
1546 || bits (this_instr, 4, 27) == 0x12fff2
1547 || bits (this_instr, 4, 27) == 0x12fff3)
1548 return 1;
1549
1550 /* Other miscellaneous instructions are unpredictable if they
1551 modify PC. */
1552 return 0;
1553 }
1554 /* Data processing instruction. Fall through. */
c906108c 1555
0d39a070
DJ
1556 case 0x1:
1557 if (bits (this_instr, 12, 15) == 15)
1558 return 1;
1559 else
1560 return 0;
c906108c 1561
0d39a070
DJ
1562 case 0x2:
1563 case 0x3:
1564 /* Media instructions and architecturally undefined instructions. */
1565 if (bits (this_instr, 25, 27) == 3 && bit (this_instr, 4) == 1)
1566 return 0;
c906108c 1567
0d39a070
DJ
1568 /* Stores. */
1569 if (bit (this_instr, 20) == 0)
1570 return 0;
2a451106 1571
0d39a070
DJ
1572 /* Loads. */
1573 if (bits (this_instr, 12, 15) == ARM_PC_REGNUM)
1574 return 1;
1575 else
1576 return 0;
2a451106 1577
0d39a070
DJ
1578 case 0x4:
1579 /* Load/store multiple. */
1580 if (bit (this_instr, 20) == 1 && bit (this_instr, 15) == 1)
1581 return 1;
1582 else
1583 return 0;
2a451106 1584
0d39a070
DJ
1585 case 0x5:
1586 /* Branch and branch with link. */
1587 return 1;
2a451106 1588
0d39a070
DJ
1589 case 0x6:
1590 case 0x7:
1591 /* Coprocessor transfers or SWIs can not affect PC. */
1592 return 0;
eb5492fa 1593
0d39a070 1594 default:
9b20d036 1595 internal_error (__FILE__, __LINE__, _("bad value in switch"));
0d39a070
DJ
1596 }
1597}
c906108c 1598
f303bc3e
YQ
1599/* Return 1 if the ARM instruction INSN restores SP in epilogue, 0
1600 otherwise. */
1601
1602static int
1603arm_instruction_restores_sp (unsigned int insn)
1604{
1605 if (bits (insn, 28, 31) != INST_NV)
1606 {
1607 if ((insn & 0x0df0f000) == 0x0080d000
1608 /* ADD SP (register or immediate). */
1609 || (insn & 0x0df0f000) == 0x0040d000
1610 /* SUB SP (register or immediate). */
1611 || (insn & 0x0ffffff0) == 0x01a0d000
1612 /* MOV SP. */
1613 || (insn & 0x0fff0000) == 0x08bd0000
1614 /* POP (LDMIA). */
1615 || (insn & 0x0fff0000) == 0x049d0000)
1616 /* POP of a single register. */
1617 return 1;
1618 }
1619
1620 return 0;
1621}
1622
0d39a070
DJ
1623/* Analyze an ARM mode prologue starting at PROLOGUE_START and
1624 continuing no further than PROLOGUE_END. If CACHE is non-NULL,
1625 fill it in. Return the first address not recognized as a prologue
1626 instruction.
eb5492fa 1627
0d39a070
DJ
1628 We recognize all the instructions typically found in ARM prologues,
1629 plus harmless instructions which can be skipped (either for analysis
1630 purposes, or a more restrictive set that can be skipped when finding
1631 the end of the prologue). */
1632
1633static CORE_ADDR
1634arm_analyze_prologue (struct gdbarch *gdbarch,
1635 CORE_ADDR prologue_start, CORE_ADDR prologue_end,
1636 struct arm_prologue_cache *cache)
1637{
1638 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1639 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1640 int regno;
1641 CORE_ADDR offset, current_pc;
1642 pv_t regs[ARM_FPS_REGNUM];
1643 struct pv_area *stack;
1644 struct cleanup *back_to;
0d39a070
DJ
1645 CORE_ADDR unrecognized_pc = 0;
1646
1647 /* Search the prologue looking for instructions that set up the
96baa820 1648 frame pointer, adjust the stack pointer, and save registers.
ed9a39eb 1649
96baa820
JM
1650 Be careful, however, and if it doesn't look like a prologue,
1651 don't try to scan it. If, for instance, a frameless function
1652 begins with stmfd sp!, then we will tell ourselves there is
b8d5e71d 1653 a frame, which will confuse stack traceback, as well as "finish"
96baa820 1654 and other operations that rely on a knowledge of the stack
0d39a070 1655 traceback. */
d4473757 1656
4be43953
DJ
1657 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1658 regs[regno] = pv_register (regno, 0);
55f960e1 1659 stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
4be43953
DJ
1660 back_to = make_cleanup_free_pv_area (stack);
1661
94c30b78
MS
1662 for (current_pc = prologue_start;
1663 current_pc < prologue_end;
f43845b3 1664 current_pc += 4)
96baa820 1665 {
e17a4113
UW
1666 unsigned int insn
1667 = read_memory_unsigned_integer (current_pc, 4, byte_order_for_code);
9d4fde75 1668
94c30b78 1669 if (insn == 0xe1a0c00d) /* mov ip, sp */
f43845b3 1670 {
4be43953 1671 regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
28cd8767
JG
1672 continue;
1673 }
0d39a070
DJ
1674 else if ((insn & 0xfff00000) == 0xe2800000 /* add Rd, Rn, #n */
1675 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767
JG
1676 {
1677 unsigned imm = insn & 0xff; /* immediate value */
1678 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
0d39a070 1679 int rd = bits (insn, 12, 15);
28cd8767 1680 imm = (imm >> rot) | (imm << (32 - rot));
0d39a070 1681 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
28cd8767
JG
1682 continue;
1683 }
0d39a070
DJ
1684 else if ((insn & 0xfff00000) == 0xe2400000 /* sub Rd, Rn, #n */
1685 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767
JG
1686 {
1687 unsigned imm = insn & 0xff; /* immediate value */
1688 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
0d39a070 1689 int rd = bits (insn, 12, 15);
28cd8767 1690 imm = (imm >> rot) | (imm << (32 - rot));
0d39a070 1691 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
f43845b3
MS
1692 continue;
1693 }
0963b4bd
MS
1694 else if ((insn & 0xffff0fff) == 0xe52d0004) /* str Rd,
1695 [sp, #-4]! */
f43845b3 1696 {
4be43953
DJ
1697 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1698 break;
1699 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
0d39a070
DJ
1700 pv_area_store (stack, regs[ARM_SP_REGNUM], 4,
1701 regs[bits (insn, 12, 15)]);
f43845b3
MS
1702 continue;
1703 }
1704 else if ((insn & 0xffff0000) == 0xe92d0000)
d4473757
KB
1705 /* stmfd sp!, {..., fp, ip, lr, pc}
1706 or
1707 stmfd sp!, {a1, a2, a3, a4} */
c906108c 1708 {
d4473757 1709 int mask = insn & 0xffff;
ed9a39eb 1710
4be43953
DJ
1711 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1712 break;
1713
94c30b78 1714 /* Calculate offsets of saved registers. */
34e8f22d 1715 for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
d4473757
KB
1716 if (mask & (1 << regno))
1717 {
0963b4bd
MS
1718 regs[ARM_SP_REGNUM]
1719 = pv_add_constant (regs[ARM_SP_REGNUM], -4);
4be43953 1720 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
d4473757
KB
1721 }
1722 }
0d39a070
DJ
1723 else if ((insn & 0xffff0000) == 0xe54b0000 /* strb rx,[r11,#-n] */
1724 || (insn & 0xffff00f0) == 0xe14b00b0 /* strh rx,[r11,#-n] */
f8bf5763 1725 || (insn & 0xffffc000) == 0xe50b0000) /* str rx,[r11,#-n] */
b8d5e71d
MS
1726 {
1727 /* No need to add this to saved_regs -- it's just an arg reg. */
1728 continue;
1729 }
0d39a070
DJ
1730 else if ((insn & 0xffff0000) == 0xe5cd0000 /* strb rx,[sp,#n] */
1731 || (insn & 0xffff00f0) == 0xe1cd00b0 /* strh rx,[sp,#n] */
f8bf5763 1732 || (insn & 0xffffc000) == 0xe58d0000) /* str rx,[sp,#n] */
f43845b3
MS
1733 {
1734 /* No need to add this to saved_regs -- it's just an arg reg. */
1735 continue;
1736 }
0963b4bd
MS
1737 else if ((insn & 0xfff00000) == 0xe8800000 /* stm Rn,
1738 { registers } */
0d39a070
DJ
1739 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1740 {
1741 /* No need to add this to saved_regs -- it's just arg regs. */
1742 continue;
1743 }
d4473757
KB
1744 else if ((insn & 0xfffff000) == 0xe24cb000) /* sub fp, ip #n */
1745 {
94c30b78
MS
1746 unsigned imm = insn & 0xff; /* immediate value */
1747 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
d4473757 1748 imm = (imm >> rot) | (imm << (32 - rot));
4be43953 1749 regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
d4473757
KB
1750 }
1751 else if ((insn & 0xfffff000) == 0xe24dd000) /* sub sp, sp #n */
1752 {
94c30b78
MS
1753 unsigned imm = insn & 0xff; /* immediate value */
1754 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
d4473757 1755 imm = (imm >> rot) | (imm << (32 - rot));
4be43953 1756 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
d4473757 1757 }
0963b4bd
MS
1758 else if ((insn & 0xffff7fff) == 0xed6d0103 /* stfe f?,
1759 [sp, -#c]! */
2af46ca0 1760 && gdbarch_tdep (gdbarch)->have_fpa_registers)
d4473757 1761 {
4be43953
DJ
1762 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1763 break;
1764
1765 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
34e8f22d 1766 regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
4be43953 1767 pv_area_store (stack, regs[ARM_SP_REGNUM], 12, regs[regno]);
d4473757 1768 }
0963b4bd
MS
1769 else if ((insn & 0xffbf0fff) == 0xec2d0200 /* sfmfd f0, 4,
1770 [sp!] */
2af46ca0 1771 && gdbarch_tdep (gdbarch)->have_fpa_registers)
d4473757
KB
1772 {
1773 int n_saved_fp_regs;
1774 unsigned int fp_start_reg, fp_bound_reg;
1775
4be43953
DJ
1776 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1777 break;
1778
94c30b78 1779 if ((insn & 0x800) == 0x800) /* N0 is set */
96baa820 1780 {
d4473757
KB
1781 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1782 n_saved_fp_regs = 3;
1783 else
1784 n_saved_fp_regs = 1;
96baa820 1785 }
d4473757 1786 else
96baa820 1787 {
d4473757
KB
1788 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1789 n_saved_fp_regs = 2;
1790 else
1791 n_saved_fp_regs = 4;
96baa820 1792 }
d4473757 1793
34e8f22d 1794 fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
d4473757
KB
1795 fp_bound_reg = fp_start_reg + n_saved_fp_regs;
1796 for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
96baa820 1797 {
4be43953
DJ
1798 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
1799 pv_area_store (stack, regs[ARM_SP_REGNUM], 12,
1800 regs[fp_start_reg++]);
96baa820 1801 }
c906108c 1802 }
0d39a070
DJ
1803 else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
1804 {
1805 /* Allow some special function calls when skipping the
1806 prologue; GCC generates these before storing arguments to
1807 the stack. */
1808 CORE_ADDR dest = BranchDest (current_pc, insn);
1809
e0634ccf 1810 if (skip_prologue_function (gdbarch, dest, 0))
0d39a070
DJ
1811 continue;
1812 else
1813 break;
1814 }
d4473757 1815 else if ((insn & 0xf0000000) != 0xe0000000)
0963b4bd 1816 break; /* Condition not true, exit early. */
0d39a070
DJ
1817 else if (arm_instruction_changes_pc (insn))
1818 /* Don't scan past anything that might change control flow. */
1819 break;
f303bc3e
YQ
1820 else if (arm_instruction_restores_sp (insn))
1821 {
1822 /* Don't scan past the epilogue. */
1823 break;
1824 }
d19f7eee
UW
1825 else if ((insn & 0xfe500000) == 0xe8100000 /* ldm */
1826 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1827 /* Ignore block loads from the stack, potentially copying
1828 parameters from memory. */
1829 continue;
1830 else if ((insn & 0xfc500000) == 0xe4100000
1831 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1832 /* Similarly ignore single loads from the stack. */
1833 continue;
0d39a070
DJ
1834 else if ((insn & 0xffff0ff0) == 0xe1a00000)
1835 /* MOV Rd, Rm. Skip register copies, i.e. saves to another
1836 register instead of the stack. */
d4473757 1837 continue;
0d39a070
DJ
1838 else
1839 {
21daaaaf
YQ
1840 /* The optimizer might shove anything into the prologue, if
1841 we build up cache (cache != NULL) from scanning prologue,
1842 we just skip what we don't recognize and scan further to
1843 make cache as complete as possible. However, if we skip
1844 prologue, we'll stop immediately on unrecognized
1845 instruction. */
0d39a070 1846 unrecognized_pc = current_pc;
21daaaaf
YQ
1847 if (cache != NULL)
1848 continue;
1849 else
1850 break;
0d39a070 1851 }
c906108c
SS
1852 }
1853
0d39a070
DJ
1854 if (unrecognized_pc == 0)
1855 unrecognized_pc = current_pc;
1856
0d39a070
DJ
1857 if (cache)
1858 {
4072f920
YQ
1859 int framereg, framesize;
1860
1861 /* The frame size is just the distance from the frame register
1862 to the original stack pointer. */
1863 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1864 {
1865 /* Frame pointer is fp. */
1866 framereg = ARM_FP_REGNUM;
1867 framesize = -regs[ARM_FP_REGNUM].k;
1868 }
1869 else
1870 {
1871 /* Try the stack pointer... this is a bit desperate. */
1872 framereg = ARM_SP_REGNUM;
1873 framesize = -regs[ARM_SP_REGNUM].k;
1874 }
1875
0d39a070
DJ
1876 cache->framereg = framereg;
1877 cache->framesize = framesize;
1878
1879 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1880 if (pv_area_find_reg (stack, gdbarch, regno, &offset))
1881 cache->saved_regs[regno].addr = offset;
1882 }
1883
1884 if (arm_debug)
1885 fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1886 paddress (gdbarch, unrecognized_pc));
4be43953
DJ
1887
1888 do_cleanups (back_to);
0d39a070
DJ
1889 return unrecognized_pc;
1890}
1891
1892static void
1893arm_scan_prologue (struct frame_info *this_frame,
1894 struct arm_prologue_cache *cache)
1895{
1896 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1897 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1898 int regno;
1899 CORE_ADDR prologue_start, prologue_end, current_pc;
1900 CORE_ADDR prev_pc = get_frame_pc (this_frame);
1901 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1902 pv_t regs[ARM_FPS_REGNUM];
1903 struct pv_area *stack;
1904 struct cleanup *back_to;
1905 CORE_ADDR offset;
1906
1907 /* Assume there is no frame until proven otherwise. */
1908 cache->framereg = ARM_SP_REGNUM;
1909 cache->framesize = 0;
1910
1911 /* Check for Thumb prologue. */
1912 if (arm_frame_is_thumb (this_frame))
1913 {
1914 thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
1915 return;
1916 }
1917
1918 /* Find the function prologue. If we can't find the function in
1919 the symbol table, peek in the stack frame to find the PC. */
1920 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1921 &prologue_end))
1922 {
1923 /* One way to find the end of the prologue (which works well
1924 for unoptimized code) is to do the following:
1925
1926 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
1927
1928 if (sal.line == 0)
1929 prologue_end = prev_pc;
1930 else if (sal.end < prologue_end)
1931 prologue_end = sal.end;
1932
1933 This mechanism is very accurate so long as the optimizer
1934 doesn't move any instructions from the function body into the
1935 prologue. If this happens, sal.end will be the last
1936 instruction in the first hunk of prologue code just before
1937 the first instruction that the scheduler has moved from
1938 the body to the prologue.
1939
1940 In order to make sure that we scan all of the prologue
1941 instructions, we use a slightly less accurate mechanism which
1942 may scan more than necessary. To help compensate for this
1943 lack of accuracy, the prologue scanning loop below contains
1944 several clauses which'll cause the loop to terminate early if
1945 an implausible prologue instruction is encountered.
1946
1947 The expression
1948
1949 prologue_start + 64
1950
1951 is a suitable endpoint since it accounts for the largest
1952 possible prologue plus up to five instructions inserted by
1953 the scheduler. */
1954
1955 if (prologue_end > prologue_start + 64)
1956 {
1957 prologue_end = prologue_start + 64; /* See above. */
1958 }
1959 }
1960 else
1961 {
1962 /* We have no symbol information. Our only option is to assume this
1963 function has a standard stack frame and the normal frame register.
1964 Then, we can find the value of our frame pointer on entrance to
1965 the callee (or at the present moment if this is the innermost frame).
1966 The value stored there should be the address of the stmfd + 8. */
1967 CORE_ADDR frame_loc;
1968 LONGEST return_value;
1969
1970 frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
1971 if (!safe_read_memory_integer (frame_loc, 4, byte_order, &return_value))
1972 return;
1973 else
1974 {
1975 prologue_start = gdbarch_addr_bits_remove
1976 (gdbarch, return_value) - 8;
1977 prologue_end = prologue_start + 64; /* See above. */
1978 }
1979 }
1980
1981 if (prev_pc < prologue_end)
1982 prologue_end = prev_pc;
1983
1984 arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
1985}
1986
eb5492fa 1987static struct arm_prologue_cache *
a262aec2 1988arm_make_prologue_cache (struct frame_info *this_frame)
c906108c 1989{
eb5492fa
DJ
1990 int reg;
1991 struct arm_prologue_cache *cache;
1992 CORE_ADDR unwound_fp;
c5aa993b 1993
35d5d4ee 1994 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
a262aec2 1995 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
c906108c 1996
a262aec2 1997 arm_scan_prologue (this_frame, cache);
848cfffb 1998
a262aec2 1999 unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
eb5492fa
DJ
2000 if (unwound_fp == 0)
2001 return cache;
c906108c 2002
4be43953 2003 cache->prev_sp = unwound_fp + cache->framesize;
c906108c 2004
eb5492fa
DJ
2005 /* Calculate actual addresses of saved registers using offsets
2006 determined by arm_scan_prologue. */
a262aec2 2007 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
e28a332c 2008 if (trad_frame_addr_p (cache->saved_regs, reg))
eb5492fa
DJ
2009 cache->saved_regs[reg].addr += cache->prev_sp;
2010
2011 return cache;
c906108c
SS
2012}
2013
c1ee9414
LM
2014/* Implementation of the stop_reason hook for arm_prologue frames. */
2015
2016static enum unwind_stop_reason
2017arm_prologue_unwind_stop_reason (struct frame_info *this_frame,
2018 void **this_cache)
2019{
2020 struct arm_prologue_cache *cache;
2021 CORE_ADDR pc;
2022
2023 if (*this_cache == NULL)
2024 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 2025 cache = (struct arm_prologue_cache *) *this_cache;
c1ee9414
LM
2026
2027 /* This is meant to halt the backtrace at "_start". */
2028 pc = get_frame_pc (this_frame);
2029 if (pc <= gdbarch_tdep (get_frame_arch (this_frame))->lowest_pc)
2030 return UNWIND_OUTERMOST;
2031
2032 /* If we've hit a wall, stop. */
2033 if (cache->prev_sp == 0)
2034 return UNWIND_OUTERMOST;
2035
2036 return UNWIND_NO_REASON;
2037}
2038
eb5492fa
DJ
2039/* Our frame ID for a normal frame is the current function's starting PC
2040 and the caller's SP when we were called. */
c906108c 2041
148754e5 2042static void
a262aec2 2043arm_prologue_this_id (struct frame_info *this_frame,
eb5492fa
DJ
2044 void **this_cache,
2045 struct frame_id *this_id)
c906108c 2046{
eb5492fa
DJ
2047 struct arm_prologue_cache *cache;
2048 struct frame_id id;
2c404490 2049 CORE_ADDR pc, func;
f079148d 2050
eb5492fa 2051 if (*this_cache == NULL)
a262aec2 2052 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 2053 cache = (struct arm_prologue_cache *) *this_cache;
2a451106 2054
0e9e9abd
UW
2055 /* Use function start address as part of the frame ID. If we cannot
2056 identify the start address (due to missing symbol information),
2057 fall back to just using the current PC. */
c1ee9414 2058 pc = get_frame_pc (this_frame);
2c404490 2059 func = get_frame_func (this_frame);
0e9e9abd
UW
2060 if (!func)
2061 func = pc;
2062
eb5492fa 2063 id = frame_id_build (cache->prev_sp, func);
eb5492fa 2064 *this_id = id;
c906108c
SS
2065}
2066
a262aec2
DJ
2067static struct value *
2068arm_prologue_prev_register (struct frame_info *this_frame,
eb5492fa 2069 void **this_cache,
a262aec2 2070 int prev_regnum)
24de872b 2071{
24568a2c 2072 struct gdbarch *gdbarch = get_frame_arch (this_frame);
24de872b
DJ
2073 struct arm_prologue_cache *cache;
2074
eb5492fa 2075 if (*this_cache == NULL)
a262aec2 2076 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 2077 cache = (struct arm_prologue_cache *) *this_cache;
24de872b 2078
eb5492fa 2079 /* If we are asked to unwind the PC, then we need to return the LR
b39cc962
DJ
2080 instead. The prologue may save PC, but it will point into this
2081 frame's prologue, not the next frame's resume location. Also
2082 strip the saved T bit. A valid LR may have the low bit set, but
2083 a valid PC never does. */
eb5492fa 2084 if (prev_regnum == ARM_PC_REGNUM)
b39cc962
DJ
2085 {
2086 CORE_ADDR lr;
2087
2088 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2089 return frame_unwind_got_constant (this_frame, prev_regnum,
24568a2c 2090 arm_addr_bits_remove (gdbarch, lr));
b39cc962 2091 }
24de872b 2092
eb5492fa 2093 /* SP is generally not saved to the stack, but this frame is
a262aec2 2094 identified by the next frame's stack pointer at the time of the call.
eb5492fa
DJ
2095 The value was already reconstructed into PREV_SP. */
2096 if (prev_regnum == ARM_SP_REGNUM)
a262aec2 2097 return frame_unwind_got_constant (this_frame, prev_regnum, cache->prev_sp);
eb5492fa 2098
b39cc962
DJ
2099 /* The CPSR may have been changed by the call instruction and by the
2100 called function. The only bit we can reconstruct is the T bit,
2101 by checking the low bit of LR as of the call. This is a reliable
2102 indicator of Thumb-ness except for some ARM v4T pre-interworking
2103 Thumb code, which could get away with a clear low bit as long as
2104 the called function did not use bx. Guess that all other
2105 bits are unchanged; the condition flags are presumably lost,
2106 but the processor status is likely valid. */
2107 if (prev_regnum == ARM_PS_REGNUM)
2108 {
2109 CORE_ADDR lr, cpsr;
9779414d 2110 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
b39cc962
DJ
2111
2112 cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
2113 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2114 if (IS_THUMB_ADDR (lr))
9779414d 2115 cpsr |= t_bit;
b39cc962 2116 else
9779414d 2117 cpsr &= ~t_bit;
b39cc962
DJ
2118 return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
2119 }
2120
a262aec2
DJ
2121 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
2122 prev_regnum);
eb5492fa
DJ
2123}
2124
2125struct frame_unwind arm_prologue_unwind = {
2126 NORMAL_FRAME,
c1ee9414 2127 arm_prologue_unwind_stop_reason,
eb5492fa 2128 arm_prologue_this_id,
a262aec2
DJ
2129 arm_prologue_prev_register,
2130 NULL,
2131 default_frame_sniffer
eb5492fa
DJ
2132};
2133
0e9e9abd
UW
2134/* Maintain a list of ARM exception table entries per objfile, similar to the
2135 list of mapping symbols. We only cache entries for standard ARM-defined
2136 personality routines; the cache will contain only the frame unwinding
2137 instructions associated with the entry (not the descriptors). */
2138
2139static const struct objfile_data *arm_exidx_data_key;
2140
2141struct arm_exidx_entry
2142{
2143 bfd_vma addr;
2144 gdb_byte *entry;
2145};
2146typedef struct arm_exidx_entry arm_exidx_entry_s;
2147DEF_VEC_O(arm_exidx_entry_s);
2148
2149struct arm_exidx_data
2150{
2151 VEC(arm_exidx_entry_s) **section_maps;
2152};
2153
2154static void
2155arm_exidx_data_free (struct objfile *objfile, void *arg)
2156{
9a3c8263 2157 struct arm_exidx_data *data = (struct arm_exidx_data *) arg;
0e9e9abd
UW
2158 unsigned int i;
2159
2160 for (i = 0; i < objfile->obfd->section_count; i++)
2161 VEC_free (arm_exidx_entry_s, data->section_maps[i]);
2162}
2163
2164static inline int
2165arm_compare_exidx_entries (const struct arm_exidx_entry *lhs,
2166 const struct arm_exidx_entry *rhs)
2167{
2168 return lhs->addr < rhs->addr;
2169}
2170
2171static struct obj_section *
2172arm_obj_section_from_vma (struct objfile *objfile, bfd_vma vma)
2173{
2174 struct obj_section *osect;
2175
2176 ALL_OBJFILE_OSECTIONS (objfile, osect)
2177 if (bfd_get_section_flags (objfile->obfd,
2178 osect->the_bfd_section) & SEC_ALLOC)
2179 {
2180 bfd_vma start, size;
2181 start = bfd_get_section_vma (objfile->obfd, osect->the_bfd_section);
2182 size = bfd_get_section_size (osect->the_bfd_section);
2183
2184 if (start <= vma && vma < start + size)
2185 return osect;
2186 }
2187
2188 return NULL;
2189}
2190
2191/* Parse contents of exception table and exception index sections
2192 of OBJFILE, and fill in the exception table entry cache.
2193
2194 For each entry that refers to a standard ARM-defined personality
2195 routine, extract the frame unwinding instructions (from either
2196 the index or the table section). The unwinding instructions
2197 are normalized by:
2198 - extracting them from the rest of the table data
2199 - converting to host endianness
2200 - appending the implicit 0xb0 ("Finish") code
2201
2202 The extracted and normalized instructions are stored for later
2203 retrieval by the arm_find_exidx_entry routine. */
2204
2205static void
2206arm_exidx_new_objfile (struct objfile *objfile)
2207{
3bb47e8b 2208 struct cleanup *cleanups;
0e9e9abd
UW
2209 struct arm_exidx_data *data;
2210 asection *exidx, *extab;
2211 bfd_vma exidx_vma = 0, extab_vma = 0;
2212 bfd_size_type exidx_size = 0, extab_size = 0;
2213 gdb_byte *exidx_data = NULL, *extab_data = NULL;
2214 LONGEST i;
2215
2216 /* If we've already touched this file, do nothing. */
2217 if (!objfile || objfile_data (objfile, arm_exidx_data_key) != NULL)
2218 return;
3bb47e8b 2219 cleanups = make_cleanup (null_cleanup, NULL);
0e9e9abd
UW
2220
2221 /* Read contents of exception table and index. */
a5eda10c 2222 exidx = bfd_get_section_by_name (objfile->obfd, ELF_STRING_ARM_unwind);
0e9e9abd
UW
2223 if (exidx)
2224 {
2225 exidx_vma = bfd_section_vma (objfile->obfd, exidx);
2226 exidx_size = bfd_get_section_size (exidx);
224c3ddb 2227 exidx_data = (gdb_byte *) xmalloc (exidx_size);
0e9e9abd
UW
2228 make_cleanup (xfree, exidx_data);
2229
2230 if (!bfd_get_section_contents (objfile->obfd, exidx,
2231 exidx_data, 0, exidx_size))
2232 {
2233 do_cleanups (cleanups);
2234 return;
2235 }
2236 }
2237
2238 extab = bfd_get_section_by_name (objfile->obfd, ".ARM.extab");
2239 if (extab)
2240 {
2241 extab_vma = bfd_section_vma (objfile->obfd, extab);
2242 extab_size = bfd_get_section_size (extab);
224c3ddb 2243 extab_data = (gdb_byte *) xmalloc (extab_size);
0e9e9abd
UW
2244 make_cleanup (xfree, extab_data);
2245
2246 if (!bfd_get_section_contents (objfile->obfd, extab,
2247 extab_data, 0, extab_size))
2248 {
2249 do_cleanups (cleanups);
2250 return;
2251 }
2252 }
2253
2254 /* Allocate exception table data structure. */
2255 data = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct arm_exidx_data);
2256 set_objfile_data (objfile, arm_exidx_data_key, data);
2257 data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
2258 objfile->obfd->section_count,
2259 VEC(arm_exidx_entry_s) *);
2260
2261 /* Fill in exception table. */
2262 for (i = 0; i < exidx_size / 8; i++)
2263 {
2264 struct arm_exidx_entry new_exidx_entry;
2265 bfd_vma idx = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8);
2266 bfd_vma val = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8 + 4);
2267 bfd_vma addr = 0, word = 0;
2268 int n_bytes = 0, n_words = 0;
2269 struct obj_section *sec;
2270 gdb_byte *entry = NULL;
2271
2272 /* Extract address of start of function. */
2273 idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2274 idx += exidx_vma + i * 8;
2275
2276 /* Find section containing function and compute section offset. */
2277 sec = arm_obj_section_from_vma (objfile, idx);
2278 if (sec == NULL)
2279 continue;
2280 idx -= bfd_get_section_vma (objfile->obfd, sec->the_bfd_section);
2281
2282 /* Determine address of exception table entry. */
2283 if (val == 1)
2284 {
2285 /* EXIDX_CANTUNWIND -- no exception table entry present. */
2286 }
2287 else if ((val & 0xff000000) == 0x80000000)
2288 {
2289 /* Exception table entry embedded in .ARM.exidx
2290 -- must be short form. */
2291 word = val;
2292 n_bytes = 3;
2293 }
2294 else if (!(val & 0x80000000))
2295 {
2296 /* Exception table entry in .ARM.extab. */
2297 addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2298 addr += exidx_vma + i * 8 + 4;
2299
2300 if (addr >= extab_vma && addr + 4 <= extab_vma + extab_size)
2301 {
2302 word = bfd_h_get_32 (objfile->obfd,
2303 extab_data + addr - extab_vma);
2304 addr += 4;
2305
2306 if ((word & 0xff000000) == 0x80000000)
2307 {
2308 /* Short form. */
2309 n_bytes = 3;
2310 }
2311 else if ((word & 0xff000000) == 0x81000000
2312 || (word & 0xff000000) == 0x82000000)
2313 {
2314 /* Long form. */
2315 n_bytes = 2;
2316 n_words = ((word >> 16) & 0xff);
2317 }
2318 else if (!(word & 0x80000000))
2319 {
2320 bfd_vma pers;
2321 struct obj_section *pers_sec;
2322 int gnu_personality = 0;
2323
2324 /* Custom personality routine. */
2325 pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2326 pers = UNMAKE_THUMB_ADDR (pers + addr - 4);
2327
2328 /* Check whether we've got one of the variants of the
2329 GNU personality routines. */
2330 pers_sec = arm_obj_section_from_vma (objfile, pers);
2331 if (pers_sec)
2332 {
2333 static const char *personality[] =
2334 {
2335 "__gcc_personality_v0",
2336 "__gxx_personality_v0",
2337 "__gcj_personality_v0",
2338 "__gnu_objc_personality_v0",
2339 NULL
2340 };
2341
2342 CORE_ADDR pc = pers + obj_section_offset (pers_sec);
2343 int k;
2344
2345 for (k = 0; personality[k]; k++)
2346 if (lookup_minimal_symbol_by_pc_name
2347 (pc, personality[k], objfile))
2348 {
2349 gnu_personality = 1;
2350 break;
2351 }
2352 }
2353
2354 /* If so, the next word contains a word count in the high
2355 byte, followed by the same unwind instructions as the
2356 pre-defined forms. */
2357 if (gnu_personality
2358 && addr + 4 <= extab_vma + extab_size)
2359 {
2360 word = bfd_h_get_32 (objfile->obfd,
2361 extab_data + addr - extab_vma);
2362 addr += 4;
2363 n_bytes = 3;
2364 n_words = ((word >> 24) & 0xff);
2365 }
2366 }
2367 }
2368 }
2369
2370 /* Sanity check address. */
2371 if (n_words)
2372 if (addr < extab_vma || addr + 4 * n_words > extab_vma + extab_size)
2373 n_words = n_bytes = 0;
2374
2375 /* The unwind instructions reside in WORD (only the N_BYTES least
2376 significant bytes are valid), followed by N_WORDS words in the
2377 extab section starting at ADDR. */
2378 if (n_bytes || n_words)
2379 {
224c3ddb
SM
2380 gdb_byte *p = entry
2381 = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack,
2382 n_bytes + n_words * 4 + 1);
0e9e9abd
UW
2383
2384 while (n_bytes--)
2385 *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);
2386
2387 while (n_words--)
2388 {
2389 word = bfd_h_get_32 (objfile->obfd,
2390 extab_data + addr - extab_vma);
2391 addr += 4;
2392
2393 *p++ = (gdb_byte) ((word >> 24) & 0xff);
2394 *p++ = (gdb_byte) ((word >> 16) & 0xff);
2395 *p++ = (gdb_byte) ((word >> 8) & 0xff);
2396 *p++ = (gdb_byte) (word & 0xff);
2397 }
2398
2399 /* Implied "Finish" to terminate the list. */
2400 *p++ = 0xb0;
2401 }
2402
2403 /* Push entry onto vector. They are guaranteed to always
2404 appear in order of increasing addresses. */
2405 new_exidx_entry.addr = idx;
2406 new_exidx_entry.entry = entry;
2407 VEC_safe_push (arm_exidx_entry_s,
2408 data->section_maps[sec->the_bfd_section->index],
2409 &new_exidx_entry);
2410 }
2411
2412 do_cleanups (cleanups);
2413}
2414
2415/* Search for the exception table entry covering MEMADDR. If one is found,
2416 return a pointer to its data. Otherwise, return 0. If START is non-NULL,
2417 set *START to the start of the region covered by this entry. */
2418
2419static gdb_byte *
2420arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
2421{
2422 struct obj_section *sec;
2423
2424 sec = find_pc_section (memaddr);
2425 if (sec != NULL)
2426 {
2427 struct arm_exidx_data *data;
2428 VEC(arm_exidx_entry_s) *map;
2429 struct arm_exidx_entry map_key = { memaddr - obj_section_addr (sec), 0 };
2430 unsigned int idx;
2431
9a3c8263
SM
2432 data = ((struct arm_exidx_data *)
2433 objfile_data (sec->objfile, arm_exidx_data_key));
0e9e9abd
UW
2434 if (data != NULL)
2435 {
2436 map = data->section_maps[sec->the_bfd_section->index];
2437 if (!VEC_empty (arm_exidx_entry_s, map))
2438 {
2439 struct arm_exidx_entry *map_sym;
2440
2441 idx = VEC_lower_bound (arm_exidx_entry_s, map, &map_key,
2442 arm_compare_exidx_entries);
2443
2444 /* VEC_lower_bound finds the earliest ordered insertion
2445 point. If the following symbol starts at this exact
2446 address, we use that; otherwise, the preceding
2447 exception table entry covers this address. */
2448 if (idx < VEC_length (arm_exidx_entry_s, map))
2449 {
2450 map_sym = VEC_index (arm_exidx_entry_s, map, idx);
2451 if (map_sym->addr == map_key.addr)
2452 {
2453 if (start)
2454 *start = map_sym->addr + obj_section_addr (sec);
2455 return map_sym->entry;
2456 }
2457 }
2458
2459 if (idx > 0)
2460 {
2461 map_sym = VEC_index (arm_exidx_entry_s, map, idx - 1);
2462 if (start)
2463 *start = map_sym->addr + obj_section_addr (sec);
2464 return map_sym->entry;
2465 }
2466 }
2467 }
2468 }
2469
2470 return NULL;
2471}
2472
2473/* Given the current frame THIS_FRAME, and its associated frame unwinding
2474 instruction list from the ARM exception table entry ENTRY, allocate and
2475 return a prologue cache structure describing how to unwind this frame.
2476
2477 Return NULL if the unwinding instruction list contains a "spare",
2478 "reserved" or "refuse to unwind" instruction as defined in section
2479 "9.3 Frame unwinding instructions" of the "Exception Handling ABI
2480 for the ARM Architecture" document. */
2481
2482static struct arm_prologue_cache *
2483arm_exidx_fill_cache (struct frame_info *this_frame, gdb_byte *entry)
2484{
2485 CORE_ADDR vsp = 0;
2486 int vsp_valid = 0;
2487
2488 struct arm_prologue_cache *cache;
2489 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2490 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2491
2492 for (;;)
2493 {
2494 gdb_byte insn;
2495
2496 /* Whenever we reload SP, we actually have to retrieve its
2497 actual value in the current frame. */
2498 if (!vsp_valid)
2499 {
2500 if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2501 {
2502 int reg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2503 vsp = get_frame_register_unsigned (this_frame, reg);
2504 }
2505 else
2506 {
2507 CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr;
2508 vsp = get_frame_memory_unsigned (this_frame, addr, 4);
2509 }
2510
2511 vsp_valid = 1;
2512 }
2513
2514 /* Decode next unwind instruction. */
2515 insn = *entry++;
2516
2517 if ((insn & 0xc0) == 0)
2518 {
2519 int offset = insn & 0x3f;
2520 vsp += (offset << 2) + 4;
2521 }
2522 else if ((insn & 0xc0) == 0x40)
2523 {
2524 int offset = insn & 0x3f;
2525 vsp -= (offset << 2) + 4;
2526 }
2527 else if ((insn & 0xf0) == 0x80)
2528 {
2529 int mask = ((insn & 0xf) << 8) | *entry++;
2530 int i;
2531
2532 /* The special case of an all-zero mask identifies
2533 "Refuse to unwind". We return NULL to fall back
2534 to the prologue analyzer. */
2535 if (mask == 0)
2536 return NULL;
2537
2538 /* Pop registers r4..r15 under mask. */
2539 for (i = 0; i < 12; i++)
2540 if (mask & (1 << i))
2541 {
2542 cache->saved_regs[4 + i].addr = vsp;
2543 vsp += 4;
2544 }
2545
2546 /* Special-case popping SP -- we need to reload vsp. */
2547 if (mask & (1 << (ARM_SP_REGNUM - 4)))
2548 vsp_valid = 0;
2549 }
2550 else if ((insn & 0xf0) == 0x90)
2551 {
2552 int reg = insn & 0xf;
2553
2554 /* Reserved cases. */
2555 if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
2556 return NULL;
2557
2558 /* Set SP from another register and mark VSP for reload. */
2559 cache->saved_regs[ARM_SP_REGNUM] = cache->saved_regs[reg];
2560 vsp_valid = 0;
2561 }
2562 else if ((insn & 0xf0) == 0xa0)
2563 {
2564 int count = insn & 0x7;
2565 int pop_lr = (insn & 0x8) != 0;
2566 int i;
2567
2568 /* Pop r4..r[4+count]. */
2569 for (i = 0; i <= count; i++)
2570 {
2571 cache->saved_regs[4 + i].addr = vsp;
2572 vsp += 4;
2573 }
2574
2575 /* If indicated by flag, pop LR as well. */
2576 if (pop_lr)
2577 {
2578 cache->saved_regs[ARM_LR_REGNUM].addr = vsp;
2579 vsp += 4;
2580 }
2581 }
2582 else if (insn == 0xb0)
2583 {
2584 /* We could only have updated PC by popping into it; if so, it
2585 will show up as address. Otherwise, copy LR into PC. */
2586 if (!trad_frame_addr_p (cache->saved_regs, ARM_PC_REGNUM))
2587 cache->saved_regs[ARM_PC_REGNUM]
2588 = cache->saved_regs[ARM_LR_REGNUM];
2589
2590 /* We're done. */
2591 break;
2592 }
2593 else if (insn == 0xb1)
2594 {
2595 int mask = *entry++;
2596 int i;
2597
2598 /* All-zero mask and mask >= 16 is "spare". */
2599 if (mask == 0 || mask >= 16)
2600 return NULL;
2601
2602 /* Pop r0..r3 under mask. */
2603 for (i = 0; i < 4; i++)
2604 if (mask & (1 << i))
2605 {
2606 cache->saved_regs[i].addr = vsp;
2607 vsp += 4;
2608 }
2609 }
2610 else if (insn == 0xb2)
2611 {
2612 ULONGEST offset = 0;
2613 unsigned shift = 0;
2614
2615 do
2616 {
2617 offset |= (*entry & 0x7f) << shift;
2618 shift += 7;
2619 }
2620 while (*entry++ & 0x80);
2621
2622 vsp += 0x204 + (offset << 2);
2623 }
2624 else if (insn == 0xb3)
2625 {
2626 int start = *entry >> 4;
2627 int count = (*entry++) & 0xf;
2628 int i;
2629
2630 /* Only registers D0..D15 are valid here. */
2631 if (start + count >= 16)
2632 return NULL;
2633
2634 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2635 for (i = 0; i <= count; i++)
2636 {
2637 cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2638 vsp += 8;
2639 }
2640
2641 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2642 vsp += 4;
2643 }
2644 else if ((insn & 0xf8) == 0xb8)
2645 {
2646 int count = insn & 0x7;
2647 int i;
2648
2649 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2650 for (i = 0; i <= count; i++)
2651 {
2652 cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2653 vsp += 8;
2654 }
2655
2656 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2657 vsp += 4;
2658 }
2659 else if (insn == 0xc6)
2660 {
2661 int start = *entry >> 4;
2662 int count = (*entry++) & 0xf;
2663 int i;
2664
2665 /* Only registers WR0..WR15 are valid. */
2666 if (start + count >= 16)
2667 return NULL;
2668
2669 /* Pop iwmmx registers WR[start]..WR[start+count]. */
2670 for (i = 0; i <= count; i++)
2671 {
2672 cache->saved_regs[ARM_WR0_REGNUM + start + i].addr = vsp;
2673 vsp += 8;
2674 }
2675 }
2676 else if (insn == 0xc7)
2677 {
2678 int mask = *entry++;
2679 int i;
2680
2681 /* All-zero mask and mask >= 16 is "spare". */
2682 if (mask == 0 || mask >= 16)
2683 return NULL;
2684
2685 /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask. */
2686 for (i = 0; i < 4; i++)
2687 if (mask & (1 << i))
2688 {
2689 cache->saved_regs[ARM_WCGR0_REGNUM + i].addr = vsp;
2690 vsp += 4;
2691 }
2692 }
2693 else if ((insn & 0xf8) == 0xc0)
2694 {
2695 int count = insn & 0x7;
2696 int i;
2697
2698 /* Pop iwmmx registers WR[10]..WR[10+count]. */
2699 for (i = 0; i <= count; i++)
2700 {
2701 cache->saved_regs[ARM_WR0_REGNUM + 10 + i].addr = vsp;
2702 vsp += 8;
2703 }
2704 }
2705 else if (insn == 0xc8)
2706 {
2707 int start = *entry >> 4;
2708 int count = (*entry++) & 0xf;
2709 int i;
2710
2711 /* Only registers D0..D31 are valid. */
2712 if (start + count >= 16)
2713 return NULL;
2714
2715 /* Pop VFP double-precision registers
2716 D[16+start]..D[16+start+count]. */
2717 for (i = 0; i <= count; i++)
2718 {
2719 cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].addr = vsp;
2720 vsp += 8;
2721 }
2722 }
2723 else if (insn == 0xc9)
2724 {
2725 int start = *entry >> 4;
2726 int count = (*entry++) & 0xf;
2727 int i;
2728
2729 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2730 for (i = 0; i <= count; i++)
2731 {
2732 cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2733 vsp += 8;
2734 }
2735 }
2736 else if ((insn & 0xf8) == 0xd0)
2737 {
2738 int count = insn & 0x7;
2739 int i;
2740
2741 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2742 for (i = 0; i <= count; i++)
2743 {
2744 cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2745 vsp += 8;
2746 }
2747 }
2748 else
2749 {
2750 /* Everything else is "spare". */
2751 return NULL;
2752 }
2753 }
2754
2755 /* If we restore SP from a register, assume this was the frame register.
2756 Otherwise just fall back to SP as frame register. */
2757 if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2758 cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2759 else
2760 cache->framereg = ARM_SP_REGNUM;
2761
2762 /* Determine offset to previous frame. */
2763 cache->framesize
2764 = vsp - get_frame_register_unsigned (this_frame, cache->framereg);
2765
2766 /* We already got the previous SP. */
2767 cache->prev_sp = vsp;
2768
2769 return cache;
2770}
2771
2772/* Unwinding via ARM exception table entries. Note that the sniffer
2773 already computes a filled-in prologue cache, which is then used
2774 with the same arm_prologue_this_id and arm_prologue_prev_register
2775 routines also used for prologue-parsing based unwinding. */
2776
2777static int
2778arm_exidx_unwind_sniffer (const struct frame_unwind *self,
2779 struct frame_info *this_frame,
2780 void **this_prologue_cache)
2781{
2782 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2783 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
2784 CORE_ADDR addr_in_block, exidx_region, func_start;
2785 struct arm_prologue_cache *cache;
2786 gdb_byte *entry;
2787
2788 /* See if we have an ARM exception table entry covering this address. */
2789 addr_in_block = get_frame_address_in_block (this_frame);
2790 entry = arm_find_exidx_entry (addr_in_block, &exidx_region);
2791 if (!entry)
2792 return 0;
2793
2794 /* The ARM exception table does not describe unwind information
2795 for arbitrary PC values, but is guaranteed to be correct only
2796 at call sites. We have to decide here whether we want to use
2797 ARM exception table information for this frame, or fall back
2798 to using prologue parsing. (Note that if we have DWARF CFI,
2799 this sniffer isn't even called -- CFI is always preferred.)
2800
2801 Before we make this decision, however, we check whether we
2802 actually have *symbol* information for the current frame.
2803 If not, prologue parsing would not work anyway, so we might
2804 as well use the exception table and hope for the best. */
2805 if (find_pc_partial_function (addr_in_block, NULL, &func_start, NULL))
2806 {
2807 int exc_valid = 0;
2808
2809 /* If the next frame is "normal", we are at a call site in this
2810 frame, so exception information is guaranteed to be valid. */
2811 if (get_next_frame (this_frame)
2812 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
2813 exc_valid = 1;
2814
2815 /* We also assume exception information is valid if we're currently
2816 blocked in a system call. The system library is supposed to
2817 ensure this, so that e.g. pthread cancellation works. */
2818 if (arm_frame_is_thumb (this_frame))
2819 {
2820 LONGEST insn;
2821
2822 if (safe_read_memory_integer (get_frame_pc (this_frame) - 2, 2,
2823 byte_order_for_code, &insn)
2824 && (insn & 0xff00) == 0xdf00 /* svc */)
2825 exc_valid = 1;
2826 }
2827 else
2828 {
2829 LONGEST insn;
2830
2831 if (safe_read_memory_integer (get_frame_pc (this_frame) - 4, 4,
2832 byte_order_for_code, &insn)
2833 && (insn & 0x0f000000) == 0x0f000000 /* svc */)
2834 exc_valid = 1;
2835 }
2836
2837 /* Bail out if we don't know that exception information is valid. */
2838 if (!exc_valid)
2839 return 0;
2840
2841 /* The ARM exception index does not mark the *end* of the region
2842 covered by the entry, and some functions will not have any entry.
2843 To correctly recognize the end of the covered region, the linker
2844 should have inserted dummy records with a CANTUNWIND marker.
2845
2846 Unfortunately, current versions of GNU ld do not reliably do
2847 this, and thus we may have found an incorrect entry above.
2848 As a (temporary) sanity check, we only use the entry if it
2849 lies *within* the bounds of the function. Note that this check
2850 might reject perfectly valid entries that just happen to cover
2851 multiple functions; therefore this check ought to be removed
2852 once the linker is fixed. */
2853 if (func_start > exidx_region)
2854 return 0;
2855 }
2856
2857 /* Decode the list of unwinding instructions into a prologue cache.
2858 Note that this may fail due to e.g. a "refuse to unwind" code. */
2859 cache = arm_exidx_fill_cache (this_frame, entry);
2860 if (!cache)
2861 return 0;
2862
2863 *this_prologue_cache = cache;
2864 return 1;
2865}
2866
2867struct frame_unwind arm_exidx_unwind = {
2868 NORMAL_FRAME,
8fbca658 2869 default_frame_unwind_stop_reason,
0e9e9abd
UW
2870 arm_prologue_this_id,
2871 arm_prologue_prev_register,
2872 NULL,
2873 arm_exidx_unwind_sniffer
2874};
2875
80d8d390
YQ
2876/* Recognize GCC's trampoline for thumb call-indirect. If we are in a
2877 trampoline, return the target PC. Otherwise return 0.
2878
2879 void call0a (char c, short s, int i, long l) {}
2880
2881 int main (void)
2882 {
2883 (*pointer_to_call0a) (c, s, i, l);
2884 }
2885
2886 Instead of calling a stub library function _call_via_xx (xx is
2887 the register name), GCC may inline the trampoline in the object
2888 file as below (register r2 has the address of call0a).
2889
2890 .global main
2891 .type main, %function
2892 ...
2893 bl .L1
2894 ...
2895 .size main, .-main
2896
2897 .L1:
2898 bx r2
2899
2900 The trampoline 'bx r2' doesn't belong to main. */
2901
2902static CORE_ADDR
2903arm_skip_bx_reg (struct frame_info *frame, CORE_ADDR pc)
2904{
2905 /* The heuristics of recognizing such trampoline is that FRAME is
2906 executing in Thumb mode and the instruction on PC is 'bx Rm'. */
2907 if (arm_frame_is_thumb (frame))
2908 {
2909 gdb_byte buf[2];
2910
2911 if (target_read_memory (pc, buf, 2) == 0)
2912 {
2913 struct gdbarch *gdbarch = get_frame_arch (frame);
2914 enum bfd_endian byte_order_for_code
2915 = gdbarch_byte_order_for_code (gdbarch);
2916 uint16_t insn
2917 = extract_unsigned_integer (buf, 2, byte_order_for_code);
2918
2919 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
2920 {
2921 CORE_ADDR dest
2922 = get_frame_register_unsigned (frame, bits (insn, 3, 6));
2923
2924 /* Clear the LSB so that gdb core sets step-resume
2925 breakpoint at the right address. */
2926 return UNMAKE_THUMB_ADDR (dest);
2927 }
2928 }
2929 }
2930
2931 return 0;
2932}
2933
909cf6ea 2934static struct arm_prologue_cache *
a262aec2 2935arm_make_stub_cache (struct frame_info *this_frame)
909cf6ea 2936{
909cf6ea 2937 struct arm_prologue_cache *cache;
909cf6ea 2938
35d5d4ee 2939 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
a262aec2 2940 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
909cf6ea 2941
a262aec2 2942 cache->prev_sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
909cf6ea
DJ
2943
2944 return cache;
2945}
2946
2947/* Our frame ID for a stub frame is the current SP and LR. */
2948
2949static void
a262aec2 2950arm_stub_this_id (struct frame_info *this_frame,
909cf6ea
DJ
2951 void **this_cache,
2952 struct frame_id *this_id)
2953{
2954 struct arm_prologue_cache *cache;
2955
2956 if (*this_cache == NULL)
a262aec2 2957 *this_cache = arm_make_stub_cache (this_frame);
9a3c8263 2958 cache = (struct arm_prologue_cache *) *this_cache;
909cf6ea 2959
a262aec2 2960 *this_id = frame_id_build (cache->prev_sp, get_frame_pc (this_frame));
909cf6ea
DJ
2961}
2962
a262aec2
DJ
2963static int
2964arm_stub_unwind_sniffer (const struct frame_unwind *self,
2965 struct frame_info *this_frame,
2966 void **this_prologue_cache)
909cf6ea 2967{
93d42b30 2968 CORE_ADDR addr_in_block;
948f8e3d 2969 gdb_byte dummy[4];
18d18ac8
YQ
2970 CORE_ADDR pc, start_addr;
2971 const char *name;
909cf6ea 2972
a262aec2 2973 addr_in_block = get_frame_address_in_block (this_frame);
18d18ac8 2974 pc = get_frame_pc (this_frame);
3e5d3a5a 2975 if (in_plt_section (addr_in_block)
fc36e839
DE
2976 /* We also use the stub winder if the target memory is unreadable
2977 to avoid having the prologue unwinder trying to read it. */
18d18ac8
YQ
2978 || target_read_memory (pc, dummy, 4) != 0)
2979 return 1;
2980
2981 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0
2982 && arm_skip_bx_reg (this_frame, pc) != 0)
a262aec2 2983 return 1;
909cf6ea 2984
a262aec2 2985 return 0;
909cf6ea
DJ
2986}
2987
a262aec2
DJ
2988struct frame_unwind arm_stub_unwind = {
2989 NORMAL_FRAME,
8fbca658 2990 default_frame_unwind_stop_reason,
a262aec2
DJ
2991 arm_stub_this_id,
2992 arm_prologue_prev_register,
2993 NULL,
2994 arm_stub_unwind_sniffer
2995};
2996
2ae28aa9
YQ
2997/* Put here the code to store, into CACHE->saved_regs, the addresses
2998 of the saved registers of frame described by THIS_FRAME. CACHE is
2999 returned. */
3000
3001static struct arm_prologue_cache *
3002arm_m_exception_cache (struct frame_info *this_frame)
3003{
3004 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3005 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3006 struct arm_prologue_cache *cache;
3007 CORE_ADDR unwound_sp;
3008 LONGEST xpsr;
3009
3010 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
3011 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
3012
3013 unwound_sp = get_frame_register_unsigned (this_frame,
3014 ARM_SP_REGNUM);
3015
3016 /* The hardware saves eight 32-bit words, comprising xPSR,
3017 ReturnAddress, LR (R14), R12, R3, R2, R1, R0. See details in
3018 "B1.5.6 Exception entry behavior" in
3019 "ARMv7-M Architecture Reference Manual". */
3020 cache->saved_regs[0].addr = unwound_sp;
3021 cache->saved_regs[1].addr = unwound_sp + 4;
3022 cache->saved_regs[2].addr = unwound_sp + 8;
3023 cache->saved_regs[3].addr = unwound_sp + 12;
3024 cache->saved_regs[12].addr = unwound_sp + 16;
3025 cache->saved_regs[14].addr = unwound_sp + 20;
3026 cache->saved_regs[15].addr = unwound_sp + 24;
3027 cache->saved_regs[ARM_PS_REGNUM].addr = unwound_sp + 28;
3028
3029 /* If bit 9 of the saved xPSR is set, then there is a four-byte
3030 aligner between the top of the 32-byte stack frame and the
3031 previous context's stack pointer. */
3032 cache->prev_sp = unwound_sp + 32;
3033 if (safe_read_memory_integer (unwound_sp + 28, 4, byte_order, &xpsr)
3034 && (xpsr & (1 << 9)) != 0)
3035 cache->prev_sp += 4;
3036
3037 return cache;
3038}
3039
3040/* Implementation of function hook 'this_id' in
3041 'struct frame_uwnind'. */
3042
3043static void
3044arm_m_exception_this_id (struct frame_info *this_frame,
3045 void **this_cache,
3046 struct frame_id *this_id)
3047{
3048 struct arm_prologue_cache *cache;
3049
3050 if (*this_cache == NULL)
3051 *this_cache = arm_m_exception_cache (this_frame);
9a3c8263 3052 cache = (struct arm_prologue_cache *) *this_cache;
2ae28aa9
YQ
3053
3054 /* Our frame ID for a stub frame is the current SP and LR. */
3055 *this_id = frame_id_build (cache->prev_sp,
3056 get_frame_pc (this_frame));
3057}
3058
3059/* Implementation of function hook 'prev_register' in
3060 'struct frame_uwnind'. */
3061
3062static struct value *
3063arm_m_exception_prev_register (struct frame_info *this_frame,
3064 void **this_cache,
3065 int prev_regnum)
3066{
3067 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3068 struct arm_prologue_cache *cache;
3069
3070 if (*this_cache == NULL)
3071 *this_cache = arm_m_exception_cache (this_frame);
9a3c8263 3072 cache = (struct arm_prologue_cache *) *this_cache;
2ae28aa9
YQ
3073
3074 /* The value was already reconstructed into PREV_SP. */
3075 if (prev_regnum == ARM_SP_REGNUM)
3076 return frame_unwind_got_constant (this_frame, prev_regnum,
3077 cache->prev_sp);
3078
3079 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
3080 prev_regnum);
3081}
3082
3083/* Implementation of function hook 'sniffer' in
3084 'struct frame_uwnind'. */
3085
3086static int
3087arm_m_exception_unwind_sniffer (const struct frame_unwind *self,
3088 struct frame_info *this_frame,
3089 void **this_prologue_cache)
3090{
3091 CORE_ADDR this_pc = get_frame_pc (this_frame);
3092
3093 /* No need to check is_m; this sniffer is only registered for
3094 M-profile architectures. */
3095
3096 /* Exception frames return to one of these magic PCs. Other values
3097 are not defined as of v7-M. See details in "B1.5.8 Exception
3098 return behavior" in "ARMv7-M Architecture Reference Manual". */
3099 if (this_pc == 0xfffffff1 || this_pc == 0xfffffff9
3100 || this_pc == 0xfffffffd)
3101 return 1;
3102
3103 return 0;
3104}
3105
3106/* Frame unwinder for M-profile exceptions. */
3107
3108struct frame_unwind arm_m_exception_unwind =
3109{
3110 SIGTRAMP_FRAME,
3111 default_frame_unwind_stop_reason,
3112 arm_m_exception_this_id,
3113 arm_m_exception_prev_register,
3114 NULL,
3115 arm_m_exception_unwind_sniffer
3116};
3117
24de872b 3118static CORE_ADDR
a262aec2 3119arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
24de872b
DJ
3120{
3121 struct arm_prologue_cache *cache;
3122
eb5492fa 3123 if (*this_cache == NULL)
a262aec2 3124 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 3125 cache = (struct arm_prologue_cache *) *this_cache;
eb5492fa 3126
4be43953 3127 return cache->prev_sp - cache->framesize;
24de872b
DJ
3128}
3129
eb5492fa
DJ
3130struct frame_base arm_normal_base = {
3131 &arm_prologue_unwind,
3132 arm_normal_frame_base,
3133 arm_normal_frame_base,
3134 arm_normal_frame_base
3135};
3136
a262aec2 3137/* Assuming THIS_FRAME is a dummy, return the frame ID of that
eb5492fa
DJ
3138 dummy frame. The frame ID's base needs to match the TOS value
3139 saved by save_dummy_frame_tos() and returned from
3140 arm_push_dummy_call, and the PC needs to match the dummy frame's
3141 breakpoint. */
c906108c 3142
eb5492fa 3143static struct frame_id
a262aec2 3144arm_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
c906108c 3145{
0963b4bd
MS
3146 return frame_id_build (get_frame_register_unsigned (this_frame,
3147 ARM_SP_REGNUM),
a262aec2 3148 get_frame_pc (this_frame));
eb5492fa 3149}
c3b4394c 3150
eb5492fa
DJ
3151/* Given THIS_FRAME, find the previous frame's resume PC (which will
3152 be used to construct the previous frame's ID, after looking up the
3153 containing function). */
c3b4394c 3154
eb5492fa
DJ
3155static CORE_ADDR
3156arm_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
3157{
3158 CORE_ADDR pc;
3159 pc = frame_unwind_register_unsigned (this_frame, ARM_PC_REGNUM);
24568a2c 3160 return arm_addr_bits_remove (gdbarch, pc);
eb5492fa
DJ
3161}
3162
3163static CORE_ADDR
3164arm_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
3165{
3166 return frame_unwind_register_unsigned (this_frame, ARM_SP_REGNUM);
c906108c
SS
3167}
3168
b39cc962
DJ
3169static struct value *
3170arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
3171 int regnum)
3172{
24568a2c 3173 struct gdbarch * gdbarch = get_frame_arch (this_frame);
b39cc962 3174 CORE_ADDR lr, cpsr;
9779414d 3175 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
b39cc962
DJ
3176
3177 switch (regnum)
3178 {
3179 case ARM_PC_REGNUM:
3180 /* The PC is normally copied from the return column, which
3181 describes saves of LR. However, that version may have an
3182 extra bit set to indicate Thumb state. The bit is not
3183 part of the PC. */
3184 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3185 return frame_unwind_got_constant (this_frame, regnum,
24568a2c 3186 arm_addr_bits_remove (gdbarch, lr));
b39cc962
DJ
3187
3188 case ARM_PS_REGNUM:
3189 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
ca38c58e 3190 cpsr = get_frame_register_unsigned (this_frame, regnum);
b39cc962
DJ
3191 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3192 if (IS_THUMB_ADDR (lr))
9779414d 3193 cpsr |= t_bit;
b39cc962 3194 else
9779414d 3195 cpsr &= ~t_bit;
ca38c58e 3196 return frame_unwind_got_constant (this_frame, regnum, cpsr);
b39cc962
DJ
3197
3198 default:
3199 internal_error (__FILE__, __LINE__,
3200 _("Unexpected register %d"), regnum);
3201 }
3202}
3203
3204static void
3205arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
3206 struct dwarf2_frame_state_reg *reg,
3207 struct frame_info *this_frame)
3208{
3209 switch (regnum)
3210 {
3211 case ARM_PC_REGNUM:
3212 case ARM_PS_REGNUM:
3213 reg->how = DWARF2_FRAME_REG_FN;
3214 reg->loc.fn = arm_dwarf2_prev_register;
3215 break;
3216 case ARM_SP_REGNUM:
3217 reg->how = DWARF2_FRAME_REG_CFA;
3218 break;
3219 }
3220}
3221
c9cf6e20 3222/* Implement the stack_frame_destroyed_p gdbarch method. */
4024ca99
UW
3223
3224static int
c9cf6e20 3225thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
4024ca99
UW
3226{
3227 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3228 unsigned int insn, insn2;
3229 int found_return = 0, found_stack_adjust = 0;
3230 CORE_ADDR func_start, func_end;
3231 CORE_ADDR scan_pc;
3232 gdb_byte buf[4];
3233
3234 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3235 return 0;
3236
3237 /* The epilogue is a sequence of instructions along the following lines:
3238
3239 - add stack frame size to SP or FP
3240 - [if frame pointer used] restore SP from FP
3241 - restore registers from SP [may include PC]
3242 - a return-type instruction [if PC wasn't already restored]
3243
3244 In a first pass, we scan forward from the current PC and verify the
3245 instructions we find as compatible with this sequence, ending in a
3246 return instruction.
3247
3248 However, this is not sufficient to distinguish indirect function calls
3249 within a function from indirect tail calls in the epilogue in some cases.
3250 Therefore, if we didn't already find any SP-changing instruction during
3251 forward scan, we add a backward scanning heuristic to ensure we actually
3252 are in the epilogue. */
3253
3254 scan_pc = pc;
3255 while (scan_pc < func_end && !found_return)
3256 {
3257 if (target_read_memory (scan_pc, buf, 2))
3258 break;
3259
3260 scan_pc += 2;
3261 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3262
3263 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
3264 found_return = 1;
3265 else if (insn == 0x46f7) /* mov pc, lr */
3266 found_return = 1;
540314bd 3267 else if (thumb_instruction_restores_sp (insn))
4024ca99 3268 {
b7576e5c 3269 if ((insn & 0xff00) == 0xbd00) /* pop <registers, PC> */
4024ca99
UW
3270 found_return = 1;
3271 }
db24da6d 3272 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instruction */
4024ca99
UW
3273 {
3274 if (target_read_memory (scan_pc, buf, 2))
3275 break;
3276
3277 scan_pc += 2;
3278 insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3279
3280 if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3281 {
4024ca99
UW
3282 if (insn2 & 0x8000) /* <registers> include PC. */
3283 found_return = 1;
3284 }
3285 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3286 && (insn2 & 0x0fff) == 0x0b04)
3287 {
4024ca99
UW
3288 if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC. */
3289 found_return = 1;
3290 }
3291 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3292 && (insn2 & 0x0e00) == 0x0a00)
6b65d1b6 3293 ;
4024ca99
UW
3294 else
3295 break;
3296 }
3297 else
3298 break;
3299 }
3300
3301 if (!found_return)
3302 return 0;
3303
3304 /* Since any instruction in the epilogue sequence, with the possible
3305 exception of return itself, updates the stack pointer, we need to
3306 scan backwards for at most one instruction. Try either a 16-bit or
3307 a 32-bit instruction. This is just a heuristic, so we do not worry
0963b4bd 3308 too much about false positives. */
4024ca99 3309
6b65d1b6
YQ
3310 if (pc - 4 < func_start)
3311 return 0;
3312 if (target_read_memory (pc - 4, buf, 4))
3313 return 0;
4024ca99 3314
6b65d1b6
YQ
3315 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3316 insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);
3317
3318 if (thumb_instruction_restores_sp (insn2))
3319 found_stack_adjust = 1;
3320 else if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3321 found_stack_adjust = 1;
3322 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3323 && (insn2 & 0x0fff) == 0x0b04)
3324 found_stack_adjust = 1;
3325 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3326 && (insn2 & 0x0e00) == 0x0a00)
3327 found_stack_adjust = 1;
4024ca99
UW
3328
3329 return found_stack_adjust;
3330}
3331
c9cf6e20 3332/* Implement the stack_frame_destroyed_p gdbarch method. */
4024ca99
UW
3333
3334static int
c9cf6e20 3335arm_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
4024ca99
UW
3336{
3337 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3338 unsigned int insn;
f303bc3e 3339 int found_return;
4024ca99
UW
3340 CORE_ADDR func_start, func_end;
3341
3342 if (arm_pc_is_thumb (gdbarch, pc))
c9cf6e20 3343 return thumb_stack_frame_destroyed_p (gdbarch, pc);
4024ca99
UW
3344
3345 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3346 return 0;
3347
3348 /* We are in the epilogue if the previous instruction was a stack
3349 adjustment and the next instruction is a possible return (bx, mov
3350 pc, or pop). We could have to scan backwards to find the stack
3351 adjustment, or forwards to find the return, but this is a decent
3352 approximation. First scan forwards. */
3353
3354 found_return = 0;
3355 insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
3356 if (bits (insn, 28, 31) != INST_NV)
3357 {
3358 if ((insn & 0x0ffffff0) == 0x012fff10)
3359 /* BX. */
3360 found_return = 1;
3361 else if ((insn & 0x0ffffff0) == 0x01a0f000)
3362 /* MOV PC. */
3363 found_return = 1;
3364 else if ((insn & 0x0fff0000) == 0x08bd0000
3365 && (insn & 0x0000c000) != 0)
3366 /* POP (LDMIA), including PC or LR. */
3367 found_return = 1;
3368 }
3369
3370 if (!found_return)
3371 return 0;
3372
3373 /* Scan backwards. This is just a heuristic, so do not worry about
3374 false positives from mode changes. */
3375
3376 if (pc < func_start + 4)
3377 return 0;
3378
3379 insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
f303bc3e 3380 if (arm_instruction_restores_sp (insn))
4024ca99
UW
3381 return 1;
3382
3383 return 0;
3384}
3385
3386
2dd604e7
RE
3387/* When arguments must be pushed onto the stack, they go on in reverse
3388 order. The code below implements a FILO (stack) to do this. */
3389
3390struct stack_item
3391{
3392 int len;
3393 struct stack_item *prev;
7c543f7b 3394 gdb_byte *data;
2dd604e7
RE
3395};
3396
3397static struct stack_item *
df3b6708 3398push_stack_item (struct stack_item *prev, const gdb_byte *contents, int len)
2dd604e7
RE
3399{
3400 struct stack_item *si;
8d749320 3401 si = XNEW (struct stack_item);
7c543f7b 3402 si->data = (gdb_byte *) xmalloc (len);
2dd604e7
RE
3403 si->len = len;
3404 si->prev = prev;
3405 memcpy (si->data, contents, len);
3406 return si;
3407}
3408
3409static struct stack_item *
3410pop_stack_item (struct stack_item *si)
3411{
3412 struct stack_item *dead = si;
3413 si = si->prev;
3414 xfree (dead->data);
3415 xfree (dead);
3416 return si;
3417}
3418
2af48f68
PB
3419
3420/* Return the alignment (in bytes) of the given type. */
3421
3422static int
3423arm_type_align (struct type *t)
3424{
3425 int n;
3426 int align;
3427 int falign;
3428
3429 t = check_typedef (t);
3430 switch (TYPE_CODE (t))
3431 {
3432 default:
3433 /* Should never happen. */
3434 internal_error (__FILE__, __LINE__, _("unknown type alignment"));
3435 return 4;
3436
3437 case TYPE_CODE_PTR:
3438 case TYPE_CODE_ENUM:
3439 case TYPE_CODE_INT:
3440 case TYPE_CODE_FLT:
3441 case TYPE_CODE_SET:
3442 case TYPE_CODE_RANGE:
2af48f68
PB
3443 case TYPE_CODE_REF:
3444 case TYPE_CODE_CHAR:
3445 case TYPE_CODE_BOOL:
3446 return TYPE_LENGTH (t);
3447
3448 case TYPE_CODE_ARRAY:
c4312b19
YQ
3449 if (TYPE_VECTOR (t))
3450 {
3451 /* Use the natural alignment for vector types (the same for
3452 scalar type), but the maximum alignment is 64-bit. */
3453 if (TYPE_LENGTH (t) > 8)
3454 return 8;
3455 else
3456 return TYPE_LENGTH (t);
3457 }
3458 else
3459 return arm_type_align (TYPE_TARGET_TYPE (t));
2af48f68 3460 case TYPE_CODE_COMPLEX:
2af48f68
PB
3461 return arm_type_align (TYPE_TARGET_TYPE (t));
3462
3463 case TYPE_CODE_STRUCT:
3464 case TYPE_CODE_UNION:
3465 align = 1;
3466 for (n = 0; n < TYPE_NFIELDS (t); n++)
3467 {
3468 falign = arm_type_align (TYPE_FIELD_TYPE (t, n));
3469 if (falign > align)
3470 align = falign;
3471 }
3472 return align;
3473 }
3474}
3475
90445bd3
DJ
3476/* Possible base types for a candidate for passing and returning in
3477 VFP registers. */
3478
3479enum arm_vfp_cprc_base_type
3480{
3481 VFP_CPRC_UNKNOWN,
3482 VFP_CPRC_SINGLE,
3483 VFP_CPRC_DOUBLE,
3484 VFP_CPRC_VEC64,
3485 VFP_CPRC_VEC128
3486};
3487
3488/* The length of one element of base type B. */
3489
3490static unsigned
3491arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
3492{
3493 switch (b)
3494 {
3495 case VFP_CPRC_SINGLE:
3496 return 4;
3497 case VFP_CPRC_DOUBLE:
3498 return 8;
3499 case VFP_CPRC_VEC64:
3500 return 8;
3501 case VFP_CPRC_VEC128:
3502 return 16;
3503 default:
3504 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3505 (int) b);
3506 }
3507}
3508
3509/* The character ('s', 'd' or 'q') for the type of VFP register used
3510 for passing base type B. */
3511
3512static int
3513arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
3514{
3515 switch (b)
3516 {
3517 case VFP_CPRC_SINGLE:
3518 return 's';
3519 case VFP_CPRC_DOUBLE:
3520 return 'd';
3521 case VFP_CPRC_VEC64:
3522 return 'd';
3523 case VFP_CPRC_VEC128:
3524 return 'q';
3525 default:
3526 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3527 (int) b);
3528 }
3529}
3530
3531/* Determine whether T may be part of a candidate for passing and
3532 returning in VFP registers, ignoring the limit on the total number
3533 of components. If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
3534 classification of the first valid component found; if it is not
3535 VFP_CPRC_UNKNOWN, all components must have the same classification
3536 as *BASE_TYPE. If it is found that T contains a type not permitted
3537 for passing and returning in VFP registers, a type differently
3538 classified from *BASE_TYPE, or two types differently classified
3539 from each other, return -1, otherwise return the total number of
3540 base-type elements found (possibly 0 in an empty structure or
817e0957
YQ
3541 array). Vector types are not currently supported, matching the
3542 generic AAPCS support. */
90445bd3
DJ
3543
3544static int
3545arm_vfp_cprc_sub_candidate (struct type *t,
3546 enum arm_vfp_cprc_base_type *base_type)
3547{
3548 t = check_typedef (t);
3549 switch (TYPE_CODE (t))
3550 {
3551 case TYPE_CODE_FLT:
3552 switch (TYPE_LENGTH (t))
3553 {
3554 case 4:
3555 if (*base_type == VFP_CPRC_UNKNOWN)
3556 *base_type = VFP_CPRC_SINGLE;
3557 else if (*base_type != VFP_CPRC_SINGLE)
3558 return -1;
3559 return 1;
3560
3561 case 8:
3562 if (*base_type == VFP_CPRC_UNKNOWN)
3563 *base_type = VFP_CPRC_DOUBLE;
3564 else if (*base_type != VFP_CPRC_DOUBLE)
3565 return -1;
3566 return 1;
3567
3568 default:
3569 return -1;
3570 }
3571 break;
3572
817e0957
YQ
3573 case TYPE_CODE_COMPLEX:
3574 /* Arguments of complex T where T is one of the types float or
3575 double get treated as if they are implemented as:
3576
3577 struct complexT
3578 {
3579 T real;
3580 T imag;
5f52445b
YQ
3581 };
3582
3583 */
817e0957
YQ
3584 switch (TYPE_LENGTH (t))
3585 {
3586 case 8:
3587 if (*base_type == VFP_CPRC_UNKNOWN)
3588 *base_type = VFP_CPRC_SINGLE;
3589 else if (*base_type != VFP_CPRC_SINGLE)
3590 return -1;
3591 return 2;
3592
3593 case 16:
3594 if (*base_type == VFP_CPRC_UNKNOWN)
3595 *base_type = VFP_CPRC_DOUBLE;
3596 else if (*base_type != VFP_CPRC_DOUBLE)
3597 return -1;
3598 return 2;
3599
3600 default:
3601 return -1;
3602 }
3603 break;
3604
90445bd3
DJ
3605 case TYPE_CODE_ARRAY:
3606 {
c4312b19 3607 if (TYPE_VECTOR (t))
90445bd3 3608 {
c4312b19
YQ
3609 /* A 64-bit or 128-bit containerized vector type are VFP
3610 CPRCs. */
3611 switch (TYPE_LENGTH (t))
3612 {
3613 case 8:
3614 if (*base_type == VFP_CPRC_UNKNOWN)
3615 *base_type = VFP_CPRC_VEC64;
3616 return 1;
3617 case 16:
3618 if (*base_type == VFP_CPRC_UNKNOWN)
3619 *base_type = VFP_CPRC_VEC128;
3620 return 1;
3621 default:
3622 return -1;
3623 }
3624 }
3625 else
3626 {
3627 int count;
3628 unsigned unitlen;
3629
3630 count = arm_vfp_cprc_sub_candidate (TYPE_TARGET_TYPE (t),
3631 base_type);
3632 if (count == -1)
3633 return -1;
3634 if (TYPE_LENGTH (t) == 0)
3635 {
3636 gdb_assert (count == 0);
3637 return 0;
3638 }
3639 else if (count == 0)
3640 return -1;
3641 unitlen = arm_vfp_cprc_unit_length (*base_type);
3642 gdb_assert ((TYPE_LENGTH (t) % unitlen) == 0);
3643 return TYPE_LENGTH (t) / unitlen;
90445bd3 3644 }
90445bd3
DJ
3645 }
3646 break;
3647
3648 case TYPE_CODE_STRUCT:
3649 {
3650 int count = 0;
3651 unsigned unitlen;
3652 int i;
3653 for (i = 0; i < TYPE_NFIELDS (t); i++)
3654 {
3655 int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3656 base_type);
3657 if (sub_count == -1)
3658 return -1;
3659 count += sub_count;
3660 }
3661 if (TYPE_LENGTH (t) == 0)
3662 {
3663 gdb_assert (count == 0);
3664 return 0;
3665 }
3666 else if (count == 0)
3667 return -1;
3668 unitlen = arm_vfp_cprc_unit_length (*base_type);
3669 if (TYPE_LENGTH (t) != unitlen * count)
3670 return -1;
3671 return count;
3672 }
3673
3674 case TYPE_CODE_UNION:
3675 {
3676 int count = 0;
3677 unsigned unitlen;
3678 int i;
3679 for (i = 0; i < TYPE_NFIELDS (t); i++)
3680 {
3681 int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3682 base_type);
3683 if (sub_count == -1)
3684 return -1;
3685 count = (count > sub_count ? count : sub_count);
3686 }
3687 if (TYPE_LENGTH (t) == 0)
3688 {
3689 gdb_assert (count == 0);
3690 return 0;
3691 }
3692 else if (count == 0)
3693 return -1;
3694 unitlen = arm_vfp_cprc_unit_length (*base_type);
3695 if (TYPE_LENGTH (t) != unitlen * count)
3696 return -1;
3697 return count;
3698 }
3699
3700 default:
3701 break;
3702 }
3703
3704 return -1;
3705}
3706
3707/* Determine whether T is a VFP co-processor register candidate (CPRC)
3708 if passed to or returned from a non-variadic function with the VFP
3709 ABI in effect. Return 1 if it is, 0 otherwise. If it is, set
3710 *BASE_TYPE to the base type for T and *COUNT to the number of
3711 elements of that base type before returning. */
3712
3713static int
3714arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
3715 int *count)
3716{
3717 enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
3718 int c = arm_vfp_cprc_sub_candidate (t, &b);
3719 if (c <= 0 || c > 4)
3720 return 0;
3721 *base_type = b;
3722 *count = c;
3723 return 1;
3724}
3725
3726/* Return 1 if the VFP ABI should be used for passing arguments to and
3727 returning values from a function of type FUNC_TYPE, 0
3728 otherwise. */
3729
3730static int
3731arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
3732{
3733 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3734 /* Variadic functions always use the base ABI. Assume that functions
3735 without debug info are not variadic. */
3736 if (func_type && TYPE_VARARGS (check_typedef (func_type)))
3737 return 0;
3738 /* The VFP ABI is only supported as a variant of AAPCS. */
3739 if (tdep->arm_abi != ARM_ABI_AAPCS)
3740 return 0;
3741 return gdbarch_tdep (gdbarch)->fp_model == ARM_FLOAT_VFP;
3742}
3743
3744/* We currently only support passing parameters in integer registers, which
3745 conforms with GCC's default model, and VFP argument passing following
3746 the VFP variant of AAPCS. Several other variants exist and
2dd604e7
RE
3747 we should probably support some of them based on the selected ABI. */
3748
3749static CORE_ADDR
7d9b040b 3750arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6a65450a
AC
3751 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
3752 struct value **args, CORE_ADDR sp, int struct_return,
3753 CORE_ADDR struct_addr)
2dd604e7 3754{
e17a4113 3755 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2dd604e7
RE
3756 int argnum;
3757 int argreg;
3758 int nstack;
3759 struct stack_item *si = NULL;
90445bd3
DJ
3760 int use_vfp_abi;
3761 struct type *ftype;
3762 unsigned vfp_regs_free = (1 << 16) - 1;
3763
3764 /* Determine the type of this function and whether the VFP ABI
3765 applies. */
3766 ftype = check_typedef (value_type (function));
3767 if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
3768 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
3769 use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
2dd604e7 3770
6a65450a
AC
3771 /* Set the return address. For the ARM, the return breakpoint is
3772 always at BP_ADDR. */
9779414d 3773 if (arm_pc_is_thumb (gdbarch, bp_addr))
9dca5578 3774 bp_addr |= 1;
6a65450a 3775 regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
2dd604e7
RE
3776
3777 /* Walk through the list of args and determine how large a temporary
3778 stack is required. Need to take care here as structs may be
7a9dd1b2 3779 passed on the stack, and we have to push them. */
2dd604e7
RE
3780 nstack = 0;
3781
3782 argreg = ARM_A1_REGNUM;
3783 nstack = 0;
3784
2dd604e7
RE
3785 /* The struct_return pointer occupies the first parameter
3786 passing register. */
3787 if (struct_return)
3788 {
3789 if (arm_debug)
5af949e3 3790 fprintf_unfiltered (gdb_stdlog, "struct return in %s = %s\n",
2af46ca0 3791 gdbarch_register_name (gdbarch, argreg),
5af949e3 3792 paddress (gdbarch, struct_addr));
2dd604e7
RE
3793 regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
3794 argreg++;
3795 }
3796
3797 for (argnum = 0; argnum < nargs; argnum++)
3798 {
3799 int len;
3800 struct type *arg_type;
3801 struct type *target_type;
3802 enum type_code typecode;
8c6363cf 3803 const bfd_byte *val;
2af48f68 3804 int align;
90445bd3
DJ
3805 enum arm_vfp_cprc_base_type vfp_base_type;
3806 int vfp_base_count;
3807 int may_use_core_reg = 1;
2dd604e7 3808
df407dfe 3809 arg_type = check_typedef (value_type (args[argnum]));
2dd604e7
RE
3810 len = TYPE_LENGTH (arg_type);
3811 target_type = TYPE_TARGET_TYPE (arg_type);
3812 typecode = TYPE_CODE (arg_type);
8c6363cf 3813 val = value_contents (args[argnum]);
2dd604e7 3814
2af48f68
PB
3815 align = arm_type_align (arg_type);
3816 /* Round alignment up to a whole number of words. */
3817 align = (align + INT_REGISTER_SIZE - 1) & ~(INT_REGISTER_SIZE - 1);
3818 /* Different ABIs have different maximum alignments. */
3819 if (gdbarch_tdep (gdbarch)->arm_abi == ARM_ABI_APCS)
3820 {
3821 /* The APCS ABI only requires word alignment. */
3822 align = INT_REGISTER_SIZE;
3823 }
3824 else
3825 {
3826 /* The AAPCS requires at most doubleword alignment. */
3827 if (align > INT_REGISTER_SIZE * 2)
3828 align = INT_REGISTER_SIZE * 2;
3829 }
3830
90445bd3
DJ
3831 if (use_vfp_abi
3832 && arm_vfp_call_candidate (arg_type, &vfp_base_type,
3833 &vfp_base_count))
3834 {
3835 int regno;
3836 int unit_length;
3837 int shift;
3838 unsigned mask;
3839
3840 /* Because this is a CPRC it cannot go in a core register or
3841 cause a core register to be skipped for alignment.
3842 Either it goes in VFP registers and the rest of this loop
3843 iteration is skipped for this argument, or it goes on the
3844 stack (and the stack alignment code is correct for this
3845 case). */
3846 may_use_core_reg = 0;
3847
3848 unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
3849 shift = unit_length / 4;
3850 mask = (1 << (shift * vfp_base_count)) - 1;
3851 for (regno = 0; regno < 16; regno += shift)
3852 if (((vfp_regs_free >> regno) & mask) == mask)
3853 break;
3854
3855 if (regno < 16)
3856 {
3857 int reg_char;
3858 int reg_scaled;
3859 int i;
3860
3861 vfp_regs_free &= ~(mask << regno);
3862 reg_scaled = regno / shift;
3863 reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
3864 for (i = 0; i < vfp_base_count; i++)
3865 {
3866 char name_buf[4];
3867 int regnum;
58d6951d
DJ
3868 if (reg_char == 'q')
3869 arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
90445bd3 3870 val + i * unit_length);
58d6951d
DJ
3871 else
3872 {
8c042590
PM
3873 xsnprintf (name_buf, sizeof (name_buf), "%c%d",
3874 reg_char, reg_scaled + i);
58d6951d
DJ
3875 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
3876 strlen (name_buf));
3877 regcache_cooked_write (regcache, regnum,
3878 val + i * unit_length);
3879 }
90445bd3
DJ
3880 }
3881 continue;
3882 }
3883 else
3884 {
3885 /* This CPRC could not go in VFP registers, so all VFP
3886 registers are now marked as used. */
3887 vfp_regs_free = 0;
3888 }
3889 }
3890
2af48f68
PB
3891 /* Push stack padding for dowubleword alignment. */
3892 if (nstack & (align - 1))
3893 {
3894 si = push_stack_item (si, val, INT_REGISTER_SIZE);
3895 nstack += INT_REGISTER_SIZE;
3896 }
3897
3898 /* Doubleword aligned quantities must go in even register pairs. */
90445bd3
DJ
3899 if (may_use_core_reg
3900 && argreg <= ARM_LAST_ARG_REGNUM
2af48f68
PB
3901 && align > INT_REGISTER_SIZE
3902 && argreg & 1)
3903 argreg++;
3904
2dd604e7
RE
3905 /* If the argument is a pointer to a function, and it is a
3906 Thumb function, create a LOCAL copy of the value and set
3907 the THUMB bit in it. */
3908 if (TYPE_CODE_PTR == typecode
3909 && target_type != NULL
f96b8fa0 3910 && TYPE_CODE_FUNC == TYPE_CODE (check_typedef (target_type)))
2dd604e7 3911 {
e17a4113 3912 CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
9779414d 3913 if (arm_pc_is_thumb (gdbarch, regval))
2dd604e7 3914 {
224c3ddb 3915 bfd_byte *copy = (bfd_byte *) alloca (len);
8c6363cf 3916 store_unsigned_integer (copy, len, byte_order,
e17a4113 3917 MAKE_THUMB_ADDR (regval));
8c6363cf 3918 val = copy;
2dd604e7
RE
3919 }
3920 }
3921
3922 /* Copy the argument to general registers or the stack in
3923 register-sized pieces. Large arguments are split between
3924 registers and stack. */
3925 while (len > 0)
3926 {
f0c9063c 3927 int partial_len = len < INT_REGISTER_SIZE ? len : INT_REGISTER_SIZE;
2dd604e7 3928
90445bd3 3929 if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
2dd604e7
RE
3930 {
3931 /* The argument is being passed in a general purpose
3932 register. */
e17a4113
UW
3933 CORE_ADDR regval
3934 = extract_unsigned_integer (val, partial_len, byte_order);
3935 if (byte_order == BFD_ENDIAN_BIG)
8bf8793c 3936 regval <<= (INT_REGISTER_SIZE - partial_len) * 8;
2dd604e7
RE
3937 if (arm_debug)
3938 fprintf_unfiltered (gdb_stdlog, "arg %d in %s = 0x%s\n",
c9f4d572
UW
3939 argnum,
3940 gdbarch_register_name
2af46ca0 3941 (gdbarch, argreg),
f0c9063c 3942 phex (regval, INT_REGISTER_SIZE));
2dd604e7
RE
3943 regcache_cooked_write_unsigned (regcache, argreg, regval);
3944 argreg++;
3945 }
3946 else
3947 {
3948 /* Push the arguments onto the stack. */
3949 if (arm_debug)
3950 fprintf_unfiltered (gdb_stdlog, "arg %d @ sp + %d\n",
3951 argnum, nstack);
f0c9063c
UW
3952 si = push_stack_item (si, val, INT_REGISTER_SIZE);
3953 nstack += INT_REGISTER_SIZE;
2dd604e7
RE
3954 }
3955
3956 len -= partial_len;
3957 val += partial_len;
3958 }
3959 }
3960 /* If we have an odd number of words to push, then decrement the stack
3961 by one word now, so first stack argument will be dword aligned. */
3962 if (nstack & 4)
3963 sp -= 4;
3964
3965 while (si)
3966 {
3967 sp -= si->len;
3968 write_memory (sp, si->data, si->len);
3969 si = pop_stack_item (si);
3970 }
3971
3972 /* Finally, update teh SP register. */
3973 regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
3974
3975 return sp;
3976}
3977
f53f0d0b
PB
3978
3979/* Always align the frame to an 8-byte boundary. This is required on
3980 some platforms and harmless on the rest. */
3981
3982static CORE_ADDR
3983arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
3984{
3985 /* Align the stack to eight bytes. */
3986 return sp & ~ (CORE_ADDR) 7;
3987}
3988
c906108c 3989static void
12b27276 3990print_fpu_flags (struct ui_file *file, int flags)
c906108c 3991{
c5aa993b 3992 if (flags & (1 << 0))
12b27276 3993 fputs_filtered ("IVO ", file);
c5aa993b 3994 if (flags & (1 << 1))
12b27276 3995 fputs_filtered ("DVZ ", file);
c5aa993b 3996 if (flags & (1 << 2))
12b27276 3997 fputs_filtered ("OFL ", file);
c5aa993b 3998 if (flags & (1 << 3))
12b27276 3999 fputs_filtered ("UFL ", file);
c5aa993b 4000 if (flags & (1 << 4))
12b27276
WN
4001 fputs_filtered ("INX ", file);
4002 fputc_filtered ('\n', file);
c906108c
SS
4003}
4004
5e74b15c
RE
4005/* Print interesting information about the floating point processor
4006 (if present) or emulator. */
34e8f22d 4007static void
d855c300 4008arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
23e3a7ac 4009 struct frame_info *frame, const char *args)
c906108c 4010{
9c9acae0 4011 unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
c5aa993b
JM
4012 int type;
4013
4014 type = (status >> 24) & 127;
edefbb7c 4015 if (status & (1 << 31))
12b27276 4016 fprintf_filtered (file, _("Hardware FPU type %d\n"), type);
edefbb7c 4017 else
12b27276 4018 fprintf_filtered (file, _("Software FPU type %d\n"), type);
edefbb7c 4019 /* i18n: [floating point unit] mask */
12b27276
WN
4020 fputs_filtered (_("mask: "), file);
4021 print_fpu_flags (file, status >> 16);
edefbb7c 4022 /* i18n: [floating point unit] flags */
12b27276
WN
4023 fputs_filtered (_("flags: "), file);
4024 print_fpu_flags (file, status);
c906108c
SS
4025}
4026
27067745
UW
4027/* Construct the ARM extended floating point type. */
4028static struct type *
4029arm_ext_type (struct gdbarch *gdbarch)
4030{
4031 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4032
4033 if (!tdep->arm_ext_type)
4034 tdep->arm_ext_type
e9bb382b 4035 = arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
27067745
UW
4036 floatformats_arm_ext);
4037
4038 return tdep->arm_ext_type;
4039}
4040
58d6951d
DJ
4041static struct type *
4042arm_neon_double_type (struct gdbarch *gdbarch)
4043{
4044 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4045
4046 if (tdep->neon_double_type == NULL)
4047 {
4048 struct type *t, *elem;
4049
4050 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
4051 TYPE_CODE_UNION);
4052 elem = builtin_type (gdbarch)->builtin_uint8;
4053 append_composite_type_field (t, "u8", init_vector_type (elem, 8));
4054 elem = builtin_type (gdbarch)->builtin_uint16;
4055 append_composite_type_field (t, "u16", init_vector_type (elem, 4));
4056 elem = builtin_type (gdbarch)->builtin_uint32;
4057 append_composite_type_field (t, "u32", init_vector_type (elem, 2));
4058 elem = builtin_type (gdbarch)->builtin_uint64;
4059 append_composite_type_field (t, "u64", elem);
4060 elem = builtin_type (gdbarch)->builtin_float;
4061 append_composite_type_field (t, "f32", init_vector_type (elem, 2));
4062 elem = builtin_type (gdbarch)->builtin_double;
4063 append_composite_type_field (t, "f64", elem);
4064
4065 TYPE_VECTOR (t) = 1;
4066 TYPE_NAME (t) = "neon_d";
4067 tdep->neon_double_type = t;
4068 }
4069
4070 return tdep->neon_double_type;
4071}
4072
4073/* FIXME: The vector types are not correctly ordered on big-endian
4074 targets. Just as s0 is the low bits of d0, d0[0] is also the low
4075 bits of d0 - regardless of what unit size is being held in d0. So
4076 the offset of the first uint8 in d0 is 7, but the offset of the
4077 first float is 4. This code works as-is for little-endian
4078 targets. */
4079
4080static struct type *
4081arm_neon_quad_type (struct gdbarch *gdbarch)
4082{
4083 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4084
4085 if (tdep->neon_quad_type == NULL)
4086 {
4087 struct type *t, *elem;
4088
4089 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
4090 TYPE_CODE_UNION);
4091 elem = builtin_type (gdbarch)->builtin_uint8;
4092 append_composite_type_field (t, "u8", init_vector_type (elem, 16));
4093 elem = builtin_type (gdbarch)->builtin_uint16;
4094 append_composite_type_field (t, "u16", init_vector_type (elem, 8));
4095 elem = builtin_type (gdbarch)->builtin_uint32;
4096 append_composite_type_field (t, "u32", init_vector_type (elem, 4));
4097 elem = builtin_type (gdbarch)->builtin_uint64;
4098 append_composite_type_field (t, "u64", init_vector_type (elem, 2));
4099 elem = builtin_type (gdbarch)->builtin_float;
4100 append_composite_type_field (t, "f32", init_vector_type (elem, 4));
4101 elem = builtin_type (gdbarch)->builtin_double;
4102 append_composite_type_field (t, "f64", init_vector_type (elem, 2));
4103
4104 TYPE_VECTOR (t) = 1;
4105 TYPE_NAME (t) = "neon_q";
4106 tdep->neon_quad_type = t;
4107 }
4108
4109 return tdep->neon_quad_type;
4110}
4111
34e8f22d
RE
4112/* Return the GDB type object for the "standard" data type of data in
4113 register N. */
4114
4115static struct type *
7a5ea0d4 4116arm_register_type (struct gdbarch *gdbarch, int regnum)
032758dc 4117{
58d6951d
DJ
4118 int num_regs = gdbarch_num_regs (gdbarch);
4119
4120 if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
4121 && regnum >= num_regs && regnum < num_regs + 32)
4122 return builtin_type (gdbarch)->builtin_float;
4123
4124 if (gdbarch_tdep (gdbarch)->have_neon_pseudos
4125 && regnum >= num_regs + 32 && regnum < num_regs + 32 + 16)
4126 return arm_neon_quad_type (gdbarch);
4127
4128 /* If the target description has register information, we are only
4129 in this function so that we can override the types of
4130 double-precision registers for NEON. */
4131 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
4132 {
4133 struct type *t = tdesc_register_type (gdbarch, regnum);
4134
4135 if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
4136 && TYPE_CODE (t) == TYPE_CODE_FLT
4137 && gdbarch_tdep (gdbarch)->have_neon)
4138 return arm_neon_double_type (gdbarch);
4139 else
4140 return t;
4141 }
4142
34e8f22d 4143 if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
58d6951d
DJ
4144 {
4145 if (!gdbarch_tdep (gdbarch)->have_fpa_registers)
4146 return builtin_type (gdbarch)->builtin_void;
4147
4148 return arm_ext_type (gdbarch);
4149 }
e4c16157 4150 else if (regnum == ARM_SP_REGNUM)
0dfff4cb 4151 return builtin_type (gdbarch)->builtin_data_ptr;
e4c16157 4152 else if (regnum == ARM_PC_REGNUM)
0dfff4cb 4153 return builtin_type (gdbarch)->builtin_func_ptr;
ff6f572f
DJ
4154 else if (regnum >= ARRAY_SIZE (arm_register_names))
4155 /* These registers are only supported on targets which supply
4156 an XML description. */
df4df182 4157 return builtin_type (gdbarch)->builtin_int0;
032758dc 4158 else
df4df182 4159 return builtin_type (gdbarch)->builtin_uint32;
032758dc
AC
4160}
4161
ff6f572f
DJ
4162/* Map a DWARF register REGNUM onto the appropriate GDB register
4163 number. */
4164
4165static int
d3f73121 4166arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
ff6f572f
DJ
4167{
4168 /* Core integer regs. */
4169 if (reg >= 0 && reg <= 15)
4170 return reg;
4171
4172 /* Legacy FPA encoding. These were once used in a way which
4173 overlapped with VFP register numbering, so their use is
4174 discouraged, but GDB doesn't support the ARM toolchain
4175 which used them for VFP. */
4176 if (reg >= 16 && reg <= 23)
4177 return ARM_F0_REGNUM + reg - 16;
4178
4179 /* New assignments for the FPA registers. */
4180 if (reg >= 96 && reg <= 103)
4181 return ARM_F0_REGNUM + reg - 96;
4182
4183 /* WMMX register assignments. */
4184 if (reg >= 104 && reg <= 111)
4185 return ARM_WCGR0_REGNUM + reg - 104;
4186
4187 if (reg >= 112 && reg <= 127)
4188 return ARM_WR0_REGNUM + reg - 112;
4189
4190 if (reg >= 192 && reg <= 199)
4191 return ARM_WC0_REGNUM + reg - 192;
4192
58d6951d
DJ
4193 /* VFP v2 registers. A double precision value is actually
4194 in d1 rather than s2, but the ABI only defines numbering
4195 for the single precision registers. This will "just work"
4196 in GDB for little endian targets (we'll read eight bytes,
4197 starting in s0 and then progressing to s1), but will be
4198 reversed on big endian targets with VFP. This won't
4199 be a problem for the new Neon quad registers; you're supposed
4200 to use DW_OP_piece for those. */
4201 if (reg >= 64 && reg <= 95)
4202 {
4203 char name_buf[4];
4204
8c042590 4205 xsnprintf (name_buf, sizeof (name_buf), "s%d", reg - 64);
58d6951d
DJ
4206 return user_reg_map_name_to_regnum (gdbarch, name_buf,
4207 strlen (name_buf));
4208 }
4209
4210 /* VFP v3 / Neon registers. This range is also used for VFP v2
4211 registers, except that it now describes d0 instead of s0. */
4212 if (reg >= 256 && reg <= 287)
4213 {
4214 char name_buf[4];
4215
8c042590 4216 xsnprintf (name_buf, sizeof (name_buf), "d%d", reg - 256);
58d6951d
DJ
4217 return user_reg_map_name_to_regnum (gdbarch, name_buf,
4218 strlen (name_buf));
4219 }
4220
ff6f572f
DJ
4221 return -1;
4222}
4223
26216b98
AC
4224/* Map GDB internal REGNUM onto the Arm simulator register numbers. */
4225static int
e7faf938 4226arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
26216b98
AC
4227{
4228 int reg = regnum;
e7faf938 4229 gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
26216b98 4230
ff6f572f
DJ
4231 if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
4232 return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
4233
4234 if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
4235 return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
4236
4237 if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
4238 return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
4239
26216b98
AC
4240 if (reg < NUM_GREGS)
4241 return SIM_ARM_R0_REGNUM + reg;
4242 reg -= NUM_GREGS;
4243
4244 if (reg < NUM_FREGS)
4245 return SIM_ARM_FP0_REGNUM + reg;
4246 reg -= NUM_FREGS;
4247
4248 if (reg < NUM_SREGS)
4249 return SIM_ARM_FPS_REGNUM + reg;
4250 reg -= NUM_SREGS;
4251
edefbb7c 4252 internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
26216b98 4253}
34e8f22d 4254
a37b3cc0
AC
4255/* NOTE: cagney/2001-08-20: Both convert_from_extended() and
4256 convert_to_extended() use floatformat_arm_ext_littlebyte_bigword.
4257 It is thought that this is is the floating-point register format on
4258 little-endian systems. */
c906108c 4259
ed9a39eb 4260static void
b508a996 4261convert_from_extended (const struct floatformat *fmt, const void *ptr,
be8626e0 4262 void *dbl, int endianess)
c906108c 4263{
a37b3cc0 4264 DOUBLEST d;
be8626e0
MD
4265
4266 if (endianess == BFD_ENDIAN_BIG)
a37b3cc0
AC
4267 floatformat_to_doublest (&floatformat_arm_ext_big, ptr, &d);
4268 else
4269 floatformat_to_doublest (&floatformat_arm_ext_littlebyte_bigword,
4270 ptr, &d);
b508a996 4271 floatformat_from_doublest (fmt, &d, dbl);
c906108c
SS
4272}
4273
34e8f22d 4274static void
be8626e0
MD
4275convert_to_extended (const struct floatformat *fmt, void *dbl, const void *ptr,
4276 int endianess)
c906108c 4277{
a37b3cc0 4278 DOUBLEST d;
be8626e0 4279
b508a996 4280 floatformat_to_doublest (fmt, ptr, &d);
be8626e0 4281 if (endianess == BFD_ENDIAN_BIG)
a37b3cc0
AC
4282 floatformat_from_doublest (&floatformat_arm_ext_big, &d, dbl);
4283 else
4284 floatformat_from_doublest (&floatformat_arm_ext_littlebyte_bigword,
4285 &d, dbl);
c906108c 4286}
ed9a39eb 4287
c906108c 4288static int
ed9a39eb 4289condition_true (unsigned long cond, unsigned long status_reg)
c906108c
SS
4290{
4291 if (cond == INST_AL || cond == INST_NV)
4292 return 1;
4293
4294 switch (cond)
4295 {
4296 case INST_EQ:
4297 return ((status_reg & FLAG_Z) != 0);
4298 case INST_NE:
4299 return ((status_reg & FLAG_Z) == 0);
4300 case INST_CS:
4301 return ((status_reg & FLAG_C) != 0);
4302 case INST_CC:
4303 return ((status_reg & FLAG_C) == 0);
4304 case INST_MI:
4305 return ((status_reg & FLAG_N) != 0);
4306 case INST_PL:
4307 return ((status_reg & FLAG_N) == 0);
4308 case INST_VS:
4309 return ((status_reg & FLAG_V) != 0);
4310 case INST_VC:
4311 return ((status_reg & FLAG_V) == 0);
4312 case INST_HI:
4313 return ((status_reg & (FLAG_C | FLAG_Z)) == FLAG_C);
4314 case INST_LS:
4315 return ((status_reg & (FLAG_C | FLAG_Z)) != FLAG_C);
4316 case INST_GE:
4317 return (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0));
4318 case INST_LT:
4319 return (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0));
4320 case INST_GT:
f8bf5763
PM
4321 return (((status_reg & FLAG_Z) == 0)
4322 && (((status_reg & FLAG_N) == 0)
4323 == ((status_reg & FLAG_V) == 0)));
c906108c 4324 case INST_LE:
f8bf5763
PM
4325 return (((status_reg & FLAG_Z) != 0)
4326 || (((status_reg & FLAG_N) == 0)
4327 != ((status_reg & FLAG_V) == 0)));
c906108c
SS
4328 }
4329 return 1;
4330}
4331
c906108c 4332static unsigned long
0b1b3e42
UW
4333shifted_reg_val (struct frame_info *frame, unsigned long inst, int carry,
4334 unsigned long pc_val, unsigned long status_reg)
c906108c
SS
4335{
4336 unsigned long res, shift;
4337 int rm = bits (inst, 0, 3);
4338 unsigned long shifttype = bits (inst, 5, 6);
c5aa993b
JM
4339
4340 if (bit (inst, 4))
c906108c
SS
4341 {
4342 int rs = bits (inst, 8, 11);
0b1b3e42
UW
4343 shift = (rs == 15 ? pc_val + 8
4344 : get_frame_register_unsigned (frame, rs)) & 0xFF;
c906108c
SS
4345 }
4346 else
4347 shift = bits (inst, 7, 11);
c5aa993b 4348
bf9f652a 4349 res = (rm == ARM_PC_REGNUM
0d39a070 4350 ? (pc_val + (bit (inst, 4) ? 12 : 8))
0b1b3e42 4351 : get_frame_register_unsigned (frame, rm));
c906108c
SS
4352
4353 switch (shifttype)
4354 {
c5aa993b 4355 case 0: /* LSL */
c906108c
SS
4356 res = shift >= 32 ? 0 : res << shift;
4357 break;
c5aa993b
JM
4358
4359 case 1: /* LSR */
c906108c
SS
4360 res = shift >= 32 ? 0 : res >> shift;
4361 break;
4362
c5aa993b
JM
4363 case 2: /* ASR */
4364 if (shift >= 32)
4365 shift = 31;
c906108c
SS
4366 res = ((res & 0x80000000L)
4367 ? ~((~res) >> shift) : res >> shift);
4368 break;
4369
c5aa993b 4370 case 3: /* ROR/RRX */
c906108c
SS
4371 shift &= 31;
4372 if (shift == 0)
4373 res = (res >> 1) | (carry ? 0x80000000L : 0);
4374 else
c5aa993b 4375 res = (res >> shift) | (res << (32 - shift));
c906108c
SS
4376 break;
4377 }
4378
4379 return res & 0xffffffff;
4380}
4381
c906108c
SS
4382/* Return number of 1-bits in VAL. */
4383
4384static int
ed9a39eb 4385bitcount (unsigned long val)
c906108c
SS
4386{
4387 int nbits;
4388 for (nbits = 0; val != 0; nbits++)
0963b4bd 4389 val &= val - 1; /* Delete rightmost 1-bit in val. */
c906108c
SS
4390 return nbits;
4391}
4392
177321bd
DJ
4393static int
4394thumb_advance_itstate (unsigned int itstate)
4395{
4396 /* Preserve IT[7:5], the first three bits of the condition. Shift
4397 the upcoming condition flags left by one bit. */
4398 itstate = (itstate & 0xe0) | ((itstate << 1) & 0x1f);
4399
4400 /* If we have finished the IT block, clear the state. */
4401 if ((itstate & 0x0f) == 0)
4402 itstate = 0;
4403
4404 return itstate;
4405}
4406
4407/* Find the next PC after the current instruction executes. In some
4408 cases we can not statically determine the answer (see the IT state
4409 handling in this function); in that case, a breakpoint may be
4410 inserted in addition to the returned PC, which will be used to set
4411 another breakpoint by our caller. */
4412
ad527d2e 4413static CORE_ADDR
18819fa6 4414thumb_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc)
c906108c 4415{
2af46ca0 4416 struct gdbarch *gdbarch = get_frame_arch (frame);
177321bd 4417 struct address_space *aspace = get_frame_address_space (frame);
e17a4113
UW
4418 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4419 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
c5aa993b 4420 unsigned long pc_val = ((unsigned long) pc) + 4; /* PC after prefetch */
e17a4113 4421 unsigned short inst1;
0963b4bd 4422 CORE_ADDR nextpc = pc + 2; /* Default is next instruction. */
c906108c 4423 unsigned long offset;
177321bd 4424 ULONGEST status, itstate;
c906108c 4425
50e98be4
DJ
4426 nextpc = MAKE_THUMB_ADDR (nextpc);
4427 pc_val = MAKE_THUMB_ADDR (pc_val);
4428
e17a4113 4429 inst1 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
9d4fde75 4430
9dca5578
DJ
4431 /* Thumb-2 conditional execution support. There are eight bits in
4432 the CPSR which describe conditional execution state. Once
4433 reconstructed (they're in a funny order), the low five bits
4434 describe the low bit of the condition for each instruction and
4435 how many instructions remain. The high three bits describe the
4436 base condition. One of the low four bits will be set if an IT
4437 block is active. These bits read as zero on earlier
4438 processors. */
4439 status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
177321bd 4440 itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
9dca5578 4441
177321bd
DJ
4442 /* If-Then handling. On GNU/Linux, where this routine is used, we
4443 use an undefined instruction as a breakpoint. Unlike BKPT, IT
4444 can disable execution of the undefined instruction. So we might
4445 miss the breakpoint if we set it on a skipped conditional
4446 instruction. Because conditional instructions can change the
4447 flags, affecting the execution of further instructions, we may
4448 need to set two breakpoints. */
9dca5578 4449
177321bd
DJ
4450 if (gdbarch_tdep (gdbarch)->thumb2_breakpoint != NULL)
4451 {
4452 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
4453 {
4454 /* An IT instruction. Because this instruction does not
4455 modify the flags, we can accurately predict the next
4456 executed instruction. */
4457 itstate = inst1 & 0x00ff;
4458 pc += thumb_insn_size (inst1);
4459
4460 while (itstate != 0 && ! condition_true (itstate >> 4, status))
4461 {
0963b4bd
MS
4462 inst1 = read_memory_unsigned_integer (pc, 2,
4463 byte_order_for_code);
177321bd
DJ
4464 pc += thumb_insn_size (inst1);
4465 itstate = thumb_advance_itstate (itstate);
4466 }
4467
50e98be4 4468 return MAKE_THUMB_ADDR (pc);
177321bd
DJ
4469 }
4470 else if (itstate != 0)
4471 {
4472 /* We are in a conditional block. Check the condition. */
4473 if (! condition_true (itstate >> 4, status))
4474 {
4475 /* Advance to the next executed instruction. */
4476 pc += thumb_insn_size (inst1);
4477 itstate = thumb_advance_itstate (itstate);
4478
4479 while (itstate != 0 && ! condition_true (itstate >> 4, status))
4480 {
0963b4bd
MS
4481 inst1 = read_memory_unsigned_integer (pc, 2,
4482 byte_order_for_code);
177321bd
DJ
4483 pc += thumb_insn_size (inst1);
4484 itstate = thumb_advance_itstate (itstate);
4485 }
4486
50e98be4 4487 return MAKE_THUMB_ADDR (pc);
177321bd
DJ
4488 }
4489 else if ((itstate & 0x0f) == 0x08)
4490 {
4491 /* This is the last instruction of the conditional
4492 block, and it is executed. We can handle it normally
4493 because the following instruction is not conditional,
4494 and we must handle it normally because it is
4495 permitted to branch. Fall through. */
4496 }
4497 else
4498 {
4499 int cond_negated;
4500
4501 /* There are conditional instructions after this one.
4502 If this instruction modifies the flags, then we can
4503 not predict what the next executed instruction will
4504 be. Fortunately, this instruction is architecturally
4505 forbidden to branch; we know it will fall through.
4506 Start by skipping past it. */
4507 pc += thumb_insn_size (inst1);
4508 itstate = thumb_advance_itstate (itstate);
4509
4510 /* Set a breakpoint on the following instruction. */
4511 gdb_assert ((itstate & 0x0f) != 0);
18819fa6
UW
4512 arm_insert_single_step_breakpoint (gdbarch, aspace,
4513 MAKE_THUMB_ADDR (pc));
177321bd
DJ
4514 cond_negated = (itstate >> 4) & 1;
4515
4516 /* Skip all following instructions with the same
4517 condition. If there is a later instruction in the IT
4518 block with the opposite condition, set the other
4519 breakpoint there. If not, then set a breakpoint on
4520 the instruction after the IT block. */
4521 do
4522 {
0963b4bd
MS
4523 inst1 = read_memory_unsigned_integer (pc, 2,
4524 byte_order_for_code);
177321bd
DJ
4525 pc += thumb_insn_size (inst1);
4526 itstate = thumb_advance_itstate (itstate);
4527 }
4528 while (itstate != 0 && ((itstate >> 4) & 1) == cond_negated);
4529
50e98be4 4530 return MAKE_THUMB_ADDR (pc);
177321bd
DJ
4531 }
4532 }
4533 }
4534 else if (itstate & 0x0f)
9dca5578
DJ
4535 {
4536 /* We are in a conditional block. Check the condition. */
177321bd 4537 int cond = itstate >> 4;
9dca5578
DJ
4538
4539 if (! condition_true (cond, status))
db24da6d
YQ
4540 /* Advance to the next instruction. All the 32-bit
4541 instructions share a common prefix. */
4542 return MAKE_THUMB_ADDR (pc + thumb_insn_size (inst1));
177321bd
DJ
4543
4544 /* Otherwise, handle the instruction normally. */
9dca5578
DJ
4545 }
4546
c906108c
SS
4547 if ((inst1 & 0xff00) == 0xbd00) /* pop {rlist, pc} */
4548 {
4549 CORE_ADDR sp;
4550
4551 /* Fetch the saved PC from the stack. It's stored above
4552 all of the other registers. */
f0c9063c 4553 offset = bitcount (bits (inst1, 0, 7)) * INT_REGISTER_SIZE;
0b1b3e42 4554 sp = get_frame_register_unsigned (frame, ARM_SP_REGNUM);
e17a4113 4555 nextpc = read_memory_unsigned_integer (sp + offset, 4, byte_order);
c906108c
SS
4556 }
4557 else if ((inst1 & 0xf000) == 0xd000) /* conditional branch */
4558 {
c5aa993b 4559 unsigned long cond = bits (inst1, 8, 11);
25b41d01
YQ
4560 if (cond == 0x0f) /* 0x0f = SWI */
4561 {
4562 struct gdbarch_tdep *tdep;
4563 tdep = gdbarch_tdep (gdbarch);
4564
4565 if (tdep->syscall_next_pc != NULL)
4566 nextpc = tdep->syscall_next_pc (frame);
4567
4568 }
4569 else if (cond != 0x0f && condition_true (cond, status))
c906108c
SS
4570 nextpc = pc_val + (sbits (inst1, 0, 7) << 1);
4571 }
4572 else if ((inst1 & 0xf800) == 0xe000) /* unconditional branch */
4573 {
4574 nextpc = pc_val + (sbits (inst1, 0, 10) << 1);
4575 }
db24da6d 4576 else if (thumb_insn_size (inst1) == 4) /* 32-bit instruction */
c906108c 4577 {
e17a4113
UW
4578 unsigned short inst2;
4579 inst2 = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);
9dca5578
DJ
4580
4581 /* Default to the next instruction. */
4582 nextpc = pc + 4;
50e98be4 4583 nextpc = MAKE_THUMB_ADDR (nextpc);
9dca5578
DJ
4584
4585 if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
4586 {
4587 /* Branches and miscellaneous control instructions. */
4588
4589 if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
4590 {
4591 /* B, BL, BLX. */
4592 int j1, j2, imm1, imm2;
4593
4594 imm1 = sbits (inst1, 0, 10);
4595 imm2 = bits (inst2, 0, 10);
4596 j1 = bit (inst2, 13);
4597 j2 = bit (inst2, 11);
4598
4599 offset = ((imm1 << 12) + (imm2 << 1));
4600 offset ^= ((!j2) << 22) | ((!j1) << 23);
4601
4602 nextpc = pc_val + offset;
4603 /* For BLX make sure to clear the low bits. */
4604 if (bit (inst2, 12) == 0)
4605 nextpc = nextpc & 0xfffffffc;
4606 }
4607 else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
4608 {
4609 /* SUBS PC, LR, #imm8. */
4610 nextpc = get_frame_register_unsigned (frame, ARM_LR_REGNUM);
4611 nextpc -= inst2 & 0x00ff;
4612 }
4069ebbe 4613 else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
9dca5578
DJ
4614 {
4615 /* Conditional branch. */
4616 if (condition_true (bits (inst1, 6, 9), status))
4617 {
4618 int sign, j1, j2, imm1, imm2;
4619
4620 sign = sbits (inst1, 10, 10);
4621 imm1 = bits (inst1, 0, 5);
4622 imm2 = bits (inst2, 0, 10);
4623 j1 = bit (inst2, 13);
4624 j2 = bit (inst2, 11);
4625
4626 offset = (sign << 20) + (j2 << 19) + (j1 << 18);
4627 offset += (imm1 << 12) + (imm2 << 1);
4628
4629 nextpc = pc_val + offset;
4630 }
4631 }
4632 }
4633 else if ((inst1 & 0xfe50) == 0xe810)
4634 {
4635 /* Load multiple or RFE. */
4636 int rn, offset, load_pc = 1;
4637
4638 rn = bits (inst1, 0, 3);
4639 if (bit (inst1, 7) && !bit (inst1, 8))
4640 {
4641 /* LDMIA or POP */
4642 if (!bit (inst2, 15))
4643 load_pc = 0;
4644 offset = bitcount (inst2) * 4 - 4;
4645 }
4646 else if (!bit (inst1, 7) && bit (inst1, 8))
4647 {
4648 /* LDMDB */
4649 if (!bit (inst2, 15))
4650 load_pc = 0;
4651 offset = -4;
4652 }
4653 else if (bit (inst1, 7) && bit (inst1, 8))
4654 {
4655 /* RFEIA */
4656 offset = 0;
4657 }
4658 else if (!bit (inst1, 7) && !bit (inst1, 8))
4659 {
4660 /* RFEDB */
4661 offset = -8;
4662 }
4663 else
4664 load_pc = 0;
4665
4666 if (load_pc)
4667 {
4668 CORE_ADDR addr = get_frame_register_unsigned (frame, rn);
4669 nextpc = get_frame_memory_unsigned (frame, addr + offset, 4);
4670 }
4671 }
4672 else if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
4673 {
4674 /* MOV PC or MOVS PC. */
4675 nextpc = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
50e98be4 4676 nextpc = MAKE_THUMB_ADDR (nextpc);
9dca5578
DJ
4677 }
4678 else if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
4679 {
4680 /* LDR PC. */
4681 CORE_ADDR base;
4682 int rn, load_pc = 1;
4683
4684 rn = bits (inst1, 0, 3);
4685 base = get_frame_register_unsigned (frame, rn);
bf9f652a 4686 if (rn == ARM_PC_REGNUM)
9dca5578
DJ
4687 {
4688 base = (base + 4) & ~(CORE_ADDR) 0x3;
4689 if (bit (inst1, 7))
4690 base += bits (inst2, 0, 11);
4691 else
4692 base -= bits (inst2, 0, 11);
4693 }
4694 else if (bit (inst1, 7))
4695 base += bits (inst2, 0, 11);
4696 else if (bit (inst2, 11))
4697 {
4698 if (bit (inst2, 10))
4699 {
4700 if (bit (inst2, 9))
4701 base += bits (inst2, 0, 7);
4702 else
4703 base -= bits (inst2, 0, 7);
4704 }
4705 }
4706 else if ((inst2 & 0x0fc0) == 0x0000)
4707 {
4708 int shift = bits (inst2, 4, 5), rm = bits (inst2, 0, 3);
4709 base += get_frame_register_unsigned (frame, rm) << shift;
4710 }
4711 else
4712 /* Reserved. */
4713 load_pc = 0;
4714
4715 if (load_pc)
4716 nextpc = get_frame_memory_unsigned (frame, base, 4);
4717 }
4718 else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
4719 {
4720 /* TBB. */
d476da0e
RE
4721 CORE_ADDR tbl_reg, table, offset, length;
4722
4723 tbl_reg = bits (inst1, 0, 3);
4724 if (tbl_reg == 0x0f)
4725 table = pc + 4; /* Regcache copy of PC isn't right yet. */
4726 else
4727 table = get_frame_register_unsigned (frame, tbl_reg);
9dca5578 4728
9dca5578
DJ
4729 offset = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
4730 length = 2 * get_frame_memory_unsigned (frame, table + offset, 1);
4731 nextpc = pc_val + length;
4732 }
d476da0e 4733 else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
9dca5578
DJ
4734 {
4735 /* TBH. */
d476da0e
RE
4736 CORE_ADDR tbl_reg, table, offset, length;
4737
4738 tbl_reg = bits (inst1, 0, 3);
4739 if (tbl_reg == 0x0f)
4740 table = pc + 4; /* Regcache copy of PC isn't right yet. */
4741 else
4742 table = get_frame_register_unsigned (frame, tbl_reg);
9dca5578 4743
9dca5578
DJ
4744 offset = 2 * get_frame_register_unsigned (frame, bits (inst2, 0, 3));
4745 length = 2 * get_frame_memory_unsigned (frame, table + offset, 2);
4746 nextpc = pc_val + length;
4747 }
c906108c 4748 }
aa17d93e 4749 else if ((inst1 & 0xff00) == 0x4700) /* bx REG, blx REG */
9498281f
DJ
4750 {
4751 if (bits (inst1, 3, 6) == 0x0f)
6ca1b147 4752 nextpc = UNMAKE_THUMB_ADDR (pc_val);
9498281f 4753 else
0b1b3e42 4754 nextpc = get_frame_register_unsigned (frame, bits (inst1, 3, 6));
9498281f 4755 }
ad8b5167
UW
4756 else if ((inst1 & 0xff87) == 0x4687) /* mov pc, REG */
4757 {
4758 if (bits (inst1, 3, 6) == 0x0f)
4759 nextpc = pc_val;
4760 else
4761 nextpc = get_frame_register_unsigned (frame, bits (inst1, 3, 6));
4762
4763 nextpc = MAKE_THUMB_ADDR (nextpc);
4764 }
9dca5578
DJ
4765 else if ((inst1 & 0xf500) == 0xb100)
4766 {
4767 /* CBNZ or CBZ. */
4768 int imm = (bit (inst1, 9) << 6) + (bits (inst1, 3, 7) << 1);
4769 ULONGEST reg = get_frame_register_unsigned (frame, bits (inst1, 0, 2));
4770
4771 if (bit (inst1, 11) && reg != 0)
4772 nextpc = pc_val + imm;
4773 else if (!bit (inst1, 11) && reg == 0)
4774 nextpc = pc_val + imm;
4775 }
c906108c
SS
4776 return nextpc;
4777}
4778
50e98be4 4779/* Get the raw next address. PC is the current program counter, in
18819fa6 4780 FRAME, which is assumed to be executing in ARM mode.
50e98be4
DJ
4781
4782 The value returned has the execution state of the next instruction
4783 encoded in it. Use IS_THUMB_ADDR () to see whether the instruction is
4784 in Thumb-State, and gdbarch_addr_bits_remove () to get the plain memory
0963b4bd
MS
4785 address. */
4786
50e98be4 4787static CORE_ADDR
18819fa6 4788arm_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc)
c906108c 4789{
2af46ca0 4790 struct gdbarch *gdbarch = get_frame_arch (frame);
e17a4113
UW
4791 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4792 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
c906108c
SS
4793 unsigned long pc_val;
4794 unsigned long this_instr;
4795 unsigned long status;
4796 CORE_ADDR nextpc;
4797
c906108c 4798 pc_val = (unsigned long) pc;
e17a4113 4799 this_instr = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
9d4fde75 4800
0b1b3e42 4801 status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
c5aa993b 4802 nextpc = (CORE_ADDR) (pc_val + 4); /* Default case */
c906108c 4803
daddc3c1
DJ
4804 if (bits (this_instr, 28, 31) == INST_NV)
4805 switch (bits (this_instr, 24, 27))
4806 {
4807 case 0xa:
4808 case 0xb:
4809 {
4810 /* Branch with Link and change to Thumb. */
4811 nextpc = BranchDest (pc, this_instr);
4812 nextpc |= bit (this_instr, 24) << 1;
50e98be4 4813 nextpc = MAKE_THUMB_ADDR (nextpc);
daddc3c1
DJ
4814 break;
4815 }
4816 case 0xc:
4817 case 0xd:
4818 case 0xe:
4819 /* Coprocessor register transfer. */
4820 if (bits (this_instr, 12, 15) == 15)
4821 error (_("Invalid update to pc in instruction"));
4822 break;
4823 }
4824 else if (condition_true (bits (this_instr, 28, 31), status))
c906108c
SS
4825 {
4826 switch (bits (this_instr, 24, 27))
4827 {
c5aa993b 4828 case 0x0:
94c30b78 4829 case 0x1: /* data processing */
c5aa993b
JM
4830 case 0x2:
4831 case 0x3:
c906108c
SS
4832 {
4833 unsigned long operand1, operand2, result = 0;
4834 unsigned long rn;
4835 int c;
c5aa993b 4836
c906108c
SS
4837 if (bits (this_instr, 12, 15) != 15)
4838 break;
4839
4840 if (bits (this_instr, 22, 25) == 0
c5aa993b 4841 && bits (this_instr, 4, 7) == 9) /* multiply */
edefbb7c 4842 error (_("Invalid update to pc in instruction"));
c906108c 4843
9498281f 4844 /* BX <reg>, BLX <reg> */
e150acc7
PB
4845 if (bits (this_instr, 4, 27) == 0x12fff1
4846 || bits (this_instr, 4, 27) == 0x12fff3)
9498281f
DJ
4847 {
4848 rn = bits (this_instr, 0, 3);
bf9f652a
YQ
4849 nextpc = ((rn == ARM_PC_REGNUM)
4850 ? (pc_val + 8)
4851 : get_frame_register_unsigned (frame, rn));
4852
9498281f
DJ
4853 return nextpc;
4854 }
4855
0963b4bd 4856 /* Multiply into PC. */
c906108c
SS
4857 c = (status & FLAG_C) ? 1 : 0;
4858 rn = bits (this_instr, 16, 19);
bf9f652a
YQ
4859 operand1 = ((rn == ARM_PC_REGNUM)
4860 ? (pc_val + 8)
4861 : get_frame_register_unsigned (frame, rn));
c5aa993b 4862
c906108c
SS
4863 if (bit (this_instr, 25))
4864 {
4865 unsigned long immval = bits (this_instr, 0, 7);
4866 unsigned long rotate = 2 * bits (this_instr, 8, 11);
c5aa993b
JM
4867 operand2 = ((immval >> rotate) | (immval << (32 - rotate)))
4868 & 0xffffffff;
c906108c 4869 }
0963b4bd
MS
4870 else /* operand 2 is a shifted register. */
4871 operand2 = shifted_reg_val (frame, this_instr, c,
4872 pc_val, status);
c5aa993b 4873
c906108c
SS
4874 switch (bits (this_instr, 21, 24))
4875 {
c5aa993b 4876 case 0x0: /*and */
c906108c
SS
4877 result = operand1 & operand2;
4878 break;
4879
c5aa993b 4880 case 0x1: /*eor */
c906108c
SS
4881 result = operand1 ^ operand2;
4882 break;
4883
c5aa993b 4884 case 0x2: /*sub */
c906108c
SS
4885 result = operand1 - operand2;
4886 break;
4887
c5aa993b 4888 case 0x3: /*rsb */
c906108c
SS
4889 result = operand2 - operand1;
4890 break;
4891
c5aa993b 4892 case 0x4: /*add */
c906108c
SS
4893 result = operand1 + operand2;
4894 break;
4895
c5aa993b 4896 case 0x5: /*adc */
c906108c
SS
4897 result = operand1 + operand2 + c;
4898 break;
4899
c5aa993b 4900 case 0x6: /*sbc */
c906108c
SS
4901 result = operand1 - operand2 + c;
4902 break;
4903
c5aa993b 4904 case 0x7: /*rsc */
c906108c
SS
4905 result = operand2 - operand1 + c;
4906 break;
4907
c5aa993b
JM
4908 case 0x8:
4909 case 0x9:
4910 case 0xa:
4911 case 0xb: /* tst, teq, cmp, cmn */
c906108c
SS
4912 result = (unsigned long) nextpc;
4913 break;
4914
c5aa993b 4915 case 0xc: /*orr */
c906108c
SS
4916 result = operand1 | operand2;
4917 break;
4918
c5aa993b 4919 case 0xd: /*mov */
c906108c
SS
4920 /* Always step into a function. */
4921 result = operand2;
c5aa993b 4922 break;
c906108c 4923
c5aa993b 4924 case 0xe: /*bic */
c906108c
SS
4925 result = operand1 & ~operand2;
4926 break;
4927
c5aa993b 4928 case 0xf: /*mvn */
c906108c
SS
4929 result = ~operand2;
4930 break;
4931 }
c906108c 4932
50e98be4
DJ
4933 /* In 26-bit APCS the bottom two bits of the result are
4934 ignored, and we always end up in ARM state. */
4935 if (!arm_apcs_32)
4936 nextpc = arm_addr_bits_remove (gdbarch, result);
4937 else
4938 nextpc = result;
4939
c906108c
SS
4940 break;
4941 }
c5aa993b
JM
4942
4943 case 0x4:
4944 case 0x5: /* data transfer */
4945 case 0x6:
4946 case 0x7:
7b9be803
YQ
4947 if (bits (this_instr, 25, 27) == 0x3 && bit (this_instr, 4) == 1)
4948 {
4949 /* Media instructions and architecturally undefined
4950 instructions. */
4951 break;
4952 }
4953
c906108c
SS
4954 if (bit (this_instr, 20))
4955 {
4956 /* load */
4957 if (bits (this_instr, 12, 15) == 15)
4958 {
4959 /* rd == pc */
c5aa993b 4960 unsigned long rn;
c906108c 4961 unsigned long base;
c5aa993b 4962
c906108c 4963 if (bit (this_instr, 22))
edefbb7c 4964 error (_("Invalid update to pc in instruction"));
c906108c
SS
4965
4966 /* byte write to PC */
4967 rn = bits (this_instr, 16, 19);
bf9f652a
YQ
4968 base = ((rn == ARM_PC_REGNUM)
4969 ? (pc_val + 8)
4970 : get_frame_register_unsigned (frame, rn));
4971
c906108c
SS
4972 if (bit (this_instr, 24))
4973 {
4974 /* pre-indexed */
4975 int c = (status & FLAG_C) ? 1 : 0;
4976 unsigned long offset =
c5aa993b 4977 (bit (this_instr, 25)
0b1b3e42 4978 ? shifted_reg_val (frame, this_instr, c, pc_val, status)
c5aa993b 4979 : bits (this_instr, 0, 11));
c906108c
SS
4980
4981 if (bit (this_instr, 23))
4982 base += offset;
4983 else
4984 base -= offset;
4985 }
51370a33
YQ
4986 nextpc =
4987 (CORE_ADDR) read_memory_unsigned_integer ((CORE_ADDR) base,
4988 4, byte_order);
c906108c
SS
4989 }
4990 }
4991 break;
c5aa993b
JM
4992
4993 case 0x8:
4994 case 0x9: /* block transfer */
c906108c
SS
4995 if (bit (this_instr, 20))
4996 {
4997 /* LDM */
4998 if (bit (this_instr, 15))
4999 {
5000 /* loading pc */
5001 int offset = 0;
51370a33
YQ
5002 unsigned long rn_val
5003 = get_frame_register_unsigned (frame,
5004 bits (this_instr, 16, 19));
c906108c
SS
5005
5006 if (bit (this_instr, 23))
5007 {
5008 /* up */
5009 unsigned long reglist = bits (this_instr, 0, 14);
5010 offset = bitcount (reglist) * 4;
c5aa993b 5011 if (bit (this_instr, 24)) /* pre */
c906108c
SS
5012 offset += 4;
5013 }
5014 else if (bit (this_instr, 24))
5015 offset = -4;
c5aa993b 5016
51370a33
YQ
5017 nextpc =
5018 (CORE_ADDR) read_memory_unsigned_integer ((CORE_ADDR)
5019 (rn_val + offset),
5020 4, byte_order);
c906108c
SS
5021 }
5022 }
5023 break;
c5aa993b
JM
5024
5025 case 0xb: /* branch & link */
5026 case 0xa: /* branch */
c906108c
SS
5027 {
5028 nextpc = BranchDest (pc, this_instr);
c906108c
SS
5029 break;
5030 }
c5aa993b
JM
5031
5032 case 0xc:
5033 case 0xd:
5034 case 0xe: /* coproc ops */
25b41d01 5035 break;
c5aa993b 5036 case 0xf: /* SWI */
25b41d01
YQ
5037 {
5038 struct gdbarch_tdep *tdep;
5039 tdep = gdbarch_tdep (gdbarch);
5040
5041 if (tdep->syscall_next_pc != NULL)
5042 nextpc = tdep->syscall_next_pc (frame);
5043
5044 }
c906108c
SS
5045 break;
5046
5047 default:
edefbb7c 5048 fprintf_filtered (gdb_stderr, _("Bad bit-field extraction\n"));
c906108c
SS
5049 return (pc);
5050 }
5051 }
5052
5053 return nextpc;
5054}
5055
18819fa6
UW
5056/* Determine next PC after current instruction executes. Will call either
5057 arm_get_next_pc_raw or thumb_get_next_pc_raw. Error out if infinite
5058 loop is detected. */
5059
50e98be4
DJ
5060CORE_ADDR
5061arm_get_next_pc (struct frame_info *frame, CORE_ADDR pc)
5062{
18819fa6
UW
5063 CORE_ADDR nextpc;
5064
5065 if (arm_frame_is_thumb (frame))
2b59118e 5066 nextpc = thumb_get_next_pc_raw (frame, pc);
18819fa6 5067 else
2b59118e 5068 nextpc = arm_get_next_pc_raw (frame, pc);
18819fa6 5069
50e98be4
DJ
5070 return nextpc;
5071}
5072
18819fa6
UW
5073/* Like insert_single_step_breakpoint, but make sure we use a breakpoint
5074 of the appropriate mode (as encoded in the PC value), even if this
5075 differs from what would be expected according to the symbol tables. */
5076
5077void
5078arm_insert_single_step_breakpoint (struct gdbarch *gdbarch,
5079 struct address_space *aspace,
5080 CORE_ADDR pc)
5081{
5082 struct cleanup *old_chain
5083 = make_cleanup_restore_integer (&arm_override_mode);
5084
5085 arm_override_mode = IS_THUMB_ADDR (pc);
5086 pc = gdbarch_addr_bits_remove (gdbarch, pc);
5087
5088 insert_single_step_breakpoint (gdbarch, aspace, pc);
5089
5090 do_cleanups (old_chain);
5091}
5092
35f73cfc
UW
5093/* Checks for an atomic sequence of instructions beginning with a LDREX{,B,H,D}
5094 instruction and ending with a STREX{,B,H,D} instruction. If such a sequence
5095 is found, attempt to step through it. A breakpoint is placed at the end of
5096 the sequence. */
5097
5098static int
5099thumb_deal_with_atomic_sequence_raw (struct frame_info *frame)
5100{
5101 struct gdbarch *gdbarch = get_frame_arch (frame);
5102 struct address_space *aspace = get_frame_address_space (frame);
5103 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
5104 CORE_ADDR pc = get_frame_pc (frame);
5105 CORE_ADDR breaks[2] = {-1, -1};
5106 CORE_ADDR loc = pc;
5107 unsigned short insn1, insn2;
5108 int insn_count;
5109 int index;
5110 int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed). */
5111 const int atomic_sequence_length = 16; /* Instruction sequence length. */
5112 ULONGEST status, itstate;
5113
5114 /* We currently do not support atomic sequences within an IT block. */
5115 status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
5116 itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
5117 if (itstate & 0x0f)
5118 return 0;
5119
5120 /* Assume all atomic sequences start with a ldrex{,b,h,d} instruction. */
5121 insn1 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
5122 loc += 2;
5123 if (thumb_insn_size (insn1) != 4)
5124 return 0;
5125
5126 insn2 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
5127 loc += 2;
5128 if (!((insn1 & 0xfff0) == 0xe850
5129 || ((insn1 & 0xfff0) == 0xe8d0 && (insn2 & 0x00c0) == 0x0040)))
5130 return 0;
5131
5132 /* Assume that no atomic sequence is longer than "atomic_sequence_length"
5133 instructions. */
5134 for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
5135 {
5136 insn1 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
5137 loc += 2;
5138
5139 if (thumb_insn_size (insn1) != 4)
5140 {
5141 /* Assume that there is at most one conditional branch in the
5142 atomic sequence. If a conditional branch is found, put a
5143 breakpoint in its destination address. */
5144 if ((insn1 & 0xf000) == 0xd000 && bits (insn1, 8, 11) != 0x0f)
5145 {
5146 if (last_breakpoint > 0)
5147 return 0; /* More than one conditional branch found,
5148 fallback to the standard code. */
5149
5150 breaks[1] = loc + 2 + (sbits (insn1, 0, 7) << 1);
5151 last_breakpoint++;
5152 }
5153
5154 /* We do not support atomic sequences that use any *other*
5155 instructions but conditional branches to change the PC.
5156 Fall back to standard code to avoid losing control of
5157 execution. */
5158 else if (thumb_instruction_changes_pc (insn1))
5159 return 0;
5160 }
5161 else
5162 {
5163 insn2 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
5164 loc += 2;
5165
5166 /* Assume that there is at most one conditional branch in the
5167 atomic sequence. If a conditional branch is found, put a
5168 breakpoint in its destination address. */
5169 if ((insn1 & 0xf800) == 0xf000
5170 && (insn2 & 0xd000) == 0x8000
5171 && (insn1 & 0x0380) != 0x0380)
5172 {
5173 int sign, j1, j2, imm1, imm2;
5174 unsigned int offset;
5175
5176 sign = sbits (insn1, 10, 10);
5177 imm1 = bits (insn1, 0, 5);
5178 imm2 = bits (insn2, 0, 10);
5179 j1 = bit (insn2, 13);
5180 j2 = bit (insn2, 11);
5181
5182 offset = (sign << 20) + (j2 << 19) + (j1 << 18);
5183 offset += (imm1 << 12) + (imm2 << 1);
5184
5185 if (last_breakpoint > 0)
5186 return 0; /* More than one conditional branch found,
5187 fallback to the standard code. */
5188
5189 breaks[1] = loc + offset;
5190 last_breakpoint++;
5191 }
5192
5193 /* We do not support atomic sequences that use any *other*
5194 instructions but conditional branches to change the PC.
5195 Fall back to standard code to avoid losing control of
5196 execution. */
5197 else if (thumb2_instruction_changes_pc (insn1, insn2))
5198 return 0;
5199
5200 /* If we find a strex{,b,h,d}, we're done. */
5201 if ((insn1 & 0xfff0) == 0xe840
5202 || ((insn1 & 0xfff0) == 0xe8c0 && (insn2 & 0x00c0) == 0x0040))
5203 break;
5204 }
5205 }
5206
5207 /* If we didn't find the strex{,b,h,d}, we cannot handle the sequence. */
5208 if (insn_count == atomic_sequence_length)
5209 return 0;
5210
5211 /* Insert a breakpoint right after the end of the atomic sequence. */
5212 breaks[0] = loc;
5213
5214 /* Check for duplicated breakpoints. Check also for a breakpoint
5215 placed (branch instruction's destination) anywhere in sequence. */
5216 if (last_breakpoint
5217 && (breaks[1] == breaks[0]
5218 || (breaks[1] >= pc && breaks[1] < loc)))
5219 last_breakpoint = 0;
5220
5221 /* Effectively inserts the breakpoints. */
5222 for (index = 0; index <= last_breakpoint; index++)
5223 arm_insert_single_step_breakpoint (gdbarch, aspace,
5224 MAKE_THUMB_ADDR (breaks[index]));
5225
5226 return 1;
5227}
5228
5229static int
5230arm_deal_with_atomic_sequence_raw (struct frame_info *frame)
5231{
5232 struct gdbarch *gdbarch = get_frame_arch (frame);
5233 struct address_space *aspace = get_frame_address_space (frame);
5234 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
5235 CORE_ADDR pc = get_frame_pc (frame);
5236 CORE_ADDR breaks[2] = {-1, -1};
5237 CORE_ADDR loc = pc;
5238 unsigned int insn;
5239 int insn_count;
5240 int index;
5241 int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed). */
5242 const int atomic_sequence_length = 16; /* Instruction sequence length. */
5243
5244 /* Assume all atomic sequences start with a ldrex{,b,h,d} instruction.
5245 Note that we do not currently support conditionally executed atomic
5246 instructions. */
5247 insn = read_memory_unsigned_integer (loc, 4, byte_order_for_code);
5248 loc += 4;
5249 if ((insn & 0xff9000f0) != 0xe1900090)
5250 return 0;
5251
5252 /* Assume that no atomic sequence is longer than "atomic_sequence_length"
5253 instructions. */
5254 for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
5255 {
5256 insn = read_memory_unsigned_integer (loc, 4, byte_order_for_code);
5257 loc += 4;
5258
5259 /* Assume that there is at most one conditional branch in the atomic
5260 sequence. If a conditional branch is found, put a breakpoint in
5261 its destination address. */
5262 if (bits (insn, 24, 27) == 0xa)
5263 {
5264 if (last_breakpoint > 0)
5265 return 0; /* More than one conditional branch found, fallback
5266 to the standard single-step code. */
5267
5268 breaks[1] = BranchDest (loc - 4, insn);
5269 last_breakpoint++;
5270 }
5271
5272 /* We do not support atomic sequences that use any *other* instructions
5273 but conditional branches to change the PC. Fall back to standard
5274 code to avoid losing control of execution. */
5275 else if (arm_instruction_changes_pc (insn))
5276 return 0;
5277
5278 /* If we find a strex{,b,h,d}, we're done. */
5279 if ((insn & 0xff9000f0) == 0xe1800090)
5280 break;
5281 }
5282
5283 /* If we didn't find the strex{,b,h,d}, we cannot handle the sequence. */
5284 if (insn_count == atomic_sequence_length)
5285 return 0;
5286
5287 /* Insert a breakpoint right after the end of the atomic sequence. */
5288 breaks[0] = loc;
5289
5290 /* Check for duplicated breakpoints. Check also for a breakpoint
5291 placed (branch instruction's destination) anywhere in sequence. */
5292 if (last_breakpoint
5293 && (breaks[1] == breaks[0]
5294 || (breaks[1] >= pc && breaks[1] < loc)))
5295 last_breakpoint = 0;
5296
5297 /* Effectively inserts the breakpoints. */
5298 for (index = 0; index <= last_breakpoint; index++)
5299 arm_insert_single_step_breakpoint (gdbarch, aspace, breaks[index]);
5300
5301 return 1;
5302}
5303
5304int
5305arm_deal_with_atomic_sequence (struct frame_info *frame)
5306{
5307 if (arm_frame_is_thumb (frame))
5308 return thumb_deal_with_atomic_sequence_raw (frame);
5309 else
5310 return arm_deal_with_atomic_sequence_raw (frame);
5311}
5312
9512d7fd
FN
5313/* single_step() is called just before we want to resume the inferior,
5314 if we want to single-step it but there is no hardware or kernel
5315 single-step support. We find the target of the coming instruction
e0cd558a 5316 and breakpoint it. */
9512d7fd 5317
190dce09 5318int
0b1b3e42 5319arm_software_single_step (struct frame_info *frame)
9512d7fd 5320{
a6d9a66e 5321 struct gdbarch *gdbarch = get_frame_arch (frame);
6c95b8df 5322 struct address_space *aspace = get_frame_address_space (frame);
35f73cfc
UW
5323 CORE_ADDR next_pc;
5324
5325 if (arm_deal_with_atomic_sequence (frame))
5326 return 1;
18819fa6 5327
35f73cfc 5328 next_pc = arm_get_next_pc (frame, get_frame_pc (frame));
18819fa6 5329 arm_insert_single_step_breakpoint (gdbarch, aspace, next_pc);
e6590a1b
UW
5330
5331 return 1;
9512d7fd 5332}
9512d7fd 5333
f9d67f43
DJ
5334/* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
5335 the buffer to be NEW_LEN bytes ending at ENDADDR. Return
5336 NULL if an error occurs. BUF is freed. */
5337
5338static gdb_byte *
5339extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
5340 int old_len, int new_len)
5341{
22e048c9 5342 gdb_byte *new_buf;
f9d67f43
DJ
5343 int bytes_to_read = new_len - old_len;
5344
224c3ddb 5345 new_buf = (gdb_byte *) xmalloc (new_len);
f9d67f43
DJ
5346 memcpy (new_buf + bytes_to_read, buf, old_len);
5347 xfree (buf);
5348 if (target_read_memory (endaddr - new_len, new_buf, bytes_to_read) != 0)
5349 {
5350 xfree (new_buf);
5351 return NULL;
5352 }
5353 return new_buf;
5354}
5355
5356/* An IT block is at most the 2-byte IT instruction followed by
5357 four 4-byte instructions. The furthest back we must search to
5358 find an IT block that affects the current instruction is thus
5359 2 + 3 * 4 == 14 bytes. */
5360#define MAX_IT_BLOCK_PREFIX 14
5361
5362/* Use a quick scan if there are more than this many bytes of
5363 code. */
5364#define IT_SCAN_THRESHOLD 32
5365
5366/* Adjust a breakpoint's address to move breakpoints out of IT blocks.
5367 A breakpoint in an IT block may not be hit, depending on the
5368 condition flags. */
5369static CORE_ADDR
5370arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
5371{
5372 gdb_byte *buf;
5373 char map_type;
5374 CORE_ADDR boundary, func_start;
22e048c9 5375 int buf_len;
f9d67f43
DJ
5376 enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
5377 int i, any, last_it, last_it_count;
5378
5379 /* If we are using BKPT breakpoints, none of this is necessary. */
5380 if (gdbarch_tdep (gdbarch)->thumb2_breakpoint == NULL)
5381 return bpaddr;
5382
5383 /* ARM mode does not have this problem. */
9779414d 5384 if (!arm_pc_is_thumb (gdbarch, bpaddr))
f9d67f43
DJ
5385 return bpaddr;
5386
5387 /* We are setting a breakpoint in Thumb code that could potentially
5388 contain an IT block. The first step is to find how much Thumb
5389 code there is; we do not need to read outside of known Thumb
5390 sequences. */
5391 map_type = arm_find_mapping_symbol (bpaddr, &boundary);
5392 if (map_type == 0)
5393 /* Thumb-2 code must have mapping symbols to have a chance. */
5394 return bpaddr;
5395
5396 bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
5397
5398 if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
5399 && func_start > boundary)
5400 boundary = func_start;
5401
5402 /* Search for a candidate IT instruction. We have to do some fancy
5403 footwork to distinguish a real IT instruction from the second
5404 half of a 32-bit instruction, but there is no need for that if
5405 there's no candidate. */
5406 buf_len = min (bpaddr - boundary, MAX_IT_BLOCK_PREFIX);
5407 if (buf_len == 0)
5408 /* No room for an IT instruction. */
5409 return bpaddr;
5410
224c3ddb 5411 buf = (gdb_byte *) xmalloc (buf_len);
f9d67f43
DJ
5412 if (target_read_memory (bpaddr - buf_len, buf, buf_len) != 0)
5413 return bpaddr;
5414 any = 0;
5415 for (i = 0; i < buf_len; i += 2)
5416 {
5417 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5418 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5419 {
5420 any = 1;
5421 break;
5422 }
5423 }
5424 if (any == 0)
5425 {
5426 xfree (buf);
5427 return bpaddr;
5428 }
5429
5430 /* OK, the code bytes before this instruction contain at least one
5431 halfword which resembles an IT instruction. We know that it's
5432 Thumb code, but there are still two possibilities. Either the
5433 halfword really is an IT instruction, or it is the second half of
5434 a 32-bit Thumb instruction. The only way we can tell is to
5435 scan forwards from a known instruction boundary. */
5436 if (bpaddr - boundary > IT_SCAN_THRESHOLD)
5437 {
5438 int definite;
5439
5440 /* There's a lot of code before this instruction. Start with an
5441 optimistic search; it's easy to recognize halfwords that can
5442 not be the start of a 32-bit instruction, and use that to
5443 lock on to the instruction boundaries. */
5444 buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
5445 if (buf == NULL)
5446 return bpaddr;
5447 buf_len = IT_SCAN_THRESHOLD;
5448
5449 definite = 0;
5450 for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
5451 {
5452 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5453 if (thumb_insn_size (inst1) == 2)
5454 {
5455 definite = 1;
5456 break;
5457 }
5458 }
5459
5460 /* At this point, if DEFINITE, BUF[I] is the first place we
5461 are sure that we know the instruction boundaries, and it is far
5462 enough from BPADDR that we could not miss an IT instruction
5463 affecting BPADDR. If ! DEFINITE, give up - start from a
5464 known boundary. */
5465 if (! definite)
5466 {
0963b4bd
MS
5467 buf = extend_buffer_earlier (buf, bpaddr, buf_len,
5468 bpaddr - boundary);
f9d67f43
DJ
5469 if (buf == NULL)
5470 return bpaddr;
5471 buf_len = bpaddr - boundary;
5472 i = 0;
5473 }
5474 }
5475 else
5476 {
5477 buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
5478 if (buf == NULL)
5479 return bpaddr;
5480 buf_len = bpaddr - boundary;
5481 i = 0;
5482 }
5483
5484 /* Scan forwards. Find the last IT instruction before BPADDR. */
5485 last_it = -1;
5486 last_it_count = 0;
5487 while (i < buf_len)
5488 {
5489 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5490 last_it_count--;
5491 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5492 {
5493 last_it = i;
5494 if (inst1 & 0x0001)
5495 last_it_count = 4;
5496 else if (inst1 & 0x0002)
5497 last_it_count = 3;
5498 else if (inst1 & 0x0004)
5499 last_it_count = 2;
5500 else
5501 last_it_count = 1;
5502 }
5503 i += thumb_insn_size (inst1);
5504 }
5505
5506 xfree (buf);
5507
5508 if (last_it == -1)
5509 /* There wasn't really an IT instruction after all. */
5510 return bpaddr;
5511
5512 if (last_it_count < 1)
5513 /* It was too far away. */
5514 return bpaddr;
5515
5516 /* This really is a trouble spot. Move the breakpoint to the IT
5517 instruction. */
5518 return bpaddr - buf_len + last_it;
5519}
5520
cca44b1b 5521/* ARM displaced stepping support.
c906108c 5522
cca44b1b 5523 Generally ARM displaced stepping works as follows:
c906108c 5524
cca44b1b
JB
5525 1. When an instruction is to be single-stepped, it is first decoded by
5526 arm_process_displaced_insn (called from arm_displaced_step_copy_insn).
5527 Depending on the type of instruction, it is then copied to a scratch
5528 location, possibly in a modified form. The copy_* set of functions
0963b4bd 5529 performs such modification, as necessary. A breakpoint is placed after
cca44b1b
JB
5530 the modified instruction in the scratch space to return control to GDB.
5531 Note in particular that instructions which modify the PC will no longer
5532 do so after modification.
c5aa993b 5533
cca44b1b
JB
5534 2. The instruction is single-stepped, by setting the PC to the scratch
5535 location address, and resuming. Control returns to GDB when the
5536 breakpoint is hit.
c5aa993b 5537
cca44b1b
JB
5538 3. A cleanup function (cleanup_*) is called corresponding to the copy_*
5539 function used for the current instruction. This function's job is to
5540 put the CPU/memory state back to what it would have been if the
5541 instruction had been executed unmodified in its original location. */
c5aa993b 5542
cca44b1b
JB
5543/* NOP instruction (mov r0, r0). */
5544#define ARM_NOP 0xe1a00000
34518530 5545#define THUMB_NOP 0x4600
cca44b1b
JB
5546
5547/* Helper for register reads for displaced stepping. In particular, this
5548 returns the PC as it would be seen by the instruction at its original
5549 location. */
5550
5551ULONGEST
36073a92
YQ
5552displaced_read_reg (struct regcache *regs, struct displaced_step_closure *dsc,
5553 int regno)
cca44b1b
JB
5554{
5555 ULONGEST ret;
36073a92 5556 CORE_ADDR from = dsc->insn_addr;
cca44b1b 5557
bf9f652a 5558 if (regno == ARM_PC_REGNUM)
cca44b1b 5559 {
4db71c0b
YQ
5560 /* Compute pipeline offset:
5561 - When executing an ARM instruction, PC reads as the address of the
5562 current instruction plus 8.
5563 - When executing a Thumb instruction, PC reads as the address of the
5564 current instruction plus 4. */
5565
36073a92 5566 if (!dsc->is_thumb)
4db71c0b
YQ
5567 from += 8;
5568 else
5569 from += 4;
5570
cca44b1b
JB
5571 if (debug_displaced)
5572 fprintf_unfiltered (gdb_stdlog, "displaced: read pc value %.8lx\n",
4db71c0b
YQ
5573 (unsigned long) from);
5574 return (ULONGEST) from;
cca44b1b 5575 }
c906108c 5576 else
cca44b1b
JB
5577 {
5578 regcache_cooked_read_unsigned (regs, regno, &ret);
5579 if (debug_displaced)
5580 fprintf_unfiltered (gdb_stdlog, "displaced: read r%d value %.8lx\n",
5581 regno, (unsigned long) ret);
5582 return ret;
5583 }
c906108c
SS
5584}
5585
cca44b1b
JB
5586static int
5587displaced_in_arm_mode (struct regcache *regs)
5588{
5589 ULONGEST ps;
9779414d 5590 ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
66e810cd 5591
cca44b1b 5592 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
66e810cd 5593
9779414d 5594 return (ps & t_bit) == 0;
cca44b1b 5595}
66e810cd 5596
cca44b1b 5597/* Write to the PC as from a branch instruction. */
c906108c 5598
cca44b1b 5599static void
36073a92
YQ
5600branch_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5601 ULONGEST val)
c906108c 5602{
36073a92 5603 if (!dsc->is_thumb)
cca44b1b
JB
5604 /* Note: If bits 0/1 are set, this branch would be unpredictable for
5605 architecture versions < 6. */
0963b4bd
MS
5606 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5607 val & ~(ULONGEST) 0x3);
cca44b1b 5608 else
0963b4bd
MS
5609 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5610 val & ~(ULONGEST) 0x1);
cca44b1b 5611}
66e810cd 5612
cca44b1b
JB
5613/* Write to the PC as from a branch-exchange instruction. */
5614
5615static void
5616bx_write_pc (struct regcache *regs, ULONGEST val)
5617{
5618 ULONGEST ps;
9779414d 5619 ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
cca44b1b
JB
5620
5621 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
5622
5623 if ((val & 1) == 1)
c906108c 5624 {
9779414d 5625 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
cca44b1b
JB
5626 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
5627 }
5628 else if ((val & 2) == 0)
5629 {
9779414d 5630 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 5631 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
c906108c
SS
5632 }
5633 else
5634 {
cca44b1b
JB
5635 /* Unpredictable behaviour. Try to do something sensible (switch to ARM
5636 mode, align dest to 4 bytes). */
5637 warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
9779414d 5638 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 5639 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
c906108c
SS
5640 }
5641}
ed9a39eb 5642
cca44b1b 5643/* Write to the PC as if from a load instruction. */
ed9a39eb 5644
34e8f22d 5645static void
36073a92
YQ
5646load_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5647 ULONGEST val)
ed9a39eb 5648{
cca44b1b
JB
5649 if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
5650 bx_write_pc (regs, val);
5651 else
36073a92 5652 branch_write_pc (regs, dsc, val);
cca44b1b 5653}
be8626e0 5654
cca44b1b
JB
5655/* Write to the PC as if from an ALU instruction. */
5656
5657static void
36073a92
YQ
5658alu_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5659 ULONGEST val)
cca44b1b 5660{
36073a92 5661 if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
cca44b1b
JB
5662 bx_write_pc (regs, val);
5663 else
36073a92 5664 branch_write_pc (regs, dsc, val);
cca44b1b
JB
5665}
5666
5667/* Helper for writing to registers for displaced stepping. Writing to the PC
5668 has a varying effects depending on the instruction which does the write:
5669 this is controlled by the WRITE_PC argument. */
5670
5671void
5672displaced_write_reg (struct regcache *regs, struct displaced_step_closure *dsc,
5673 int regno, ULONGEST val, enum pc_write_style write_pc)
5674{
bf9f652a 5675 if (regno == ARM_PC_REGNUM)
08216dd7 5676 {
cca44b1b
JB
5677 if (debug_displaced)
5678 fprintf_unfiltered (gdb_stdlog, "displaced: writing pc %.8lx\n",
5679 (unsigned long) val);
5680 switch (write_pc)
08216dd7 5681 {
cca44b1b 5682 case BRANCH_WRITE_PC:
36073a92 5683 branch_write_pc (regs, dsc, val);
08216dd7
RE
5684 break;
5685
cca44b1b
JB
5686 case BX_WRITE_PC:
5687 bx_write_pc (regs, val);
5688 break;
5689
5690 case LOAD_WRITE_PC:
36073a92 5691 load_write_pc (regs, dsc, val);
cca44b1b
JB
5692 break;
5693
5694 case ALU_WRITE_PC:
36073a92 5695 alu_write_pc (regs, dsc, val);
cca44b1b
JB
5696 break;
5697
5698 case CANNOT_WRITE_PC:
5699 warning (_("Instruction wrote to PC in an unexpected way when "
5700 "single-stepping"));
08216dd7
RE
5701 break;
5702
5703 default:
97b9747c
JB
5704 internal_error (__FILE__, __LINE__,
5705 _("Invalid argument to displaced_write_reg"));
08216dd7 5706 }
b508a996 5707
cca44b1b 5708 dsc->wrote_to_pc = 1;
b508a996 5709 }
ed9a39eb 5710 else
b508a996 5711 {
cca44b1b
JB
5712 if (debug_displaced)
5713 fprintf_unfiltered (gdb_stdlog, "displaced: writing r%d value %.8lx\n",
5714 regno, (unsigned long) val);
5715 regcache_cooked_write_unsigned (regs, regno, val);
b508a996 5716 }
34e8f22d
RE
5717}
5718
cca44b1b
JB
5719/* This function is used to concisely determine if an instruction INSN
5720 references PC. Register fields of interest in INSN should have the
0963b4bd
MS
5721 corresponding fields of BITMASK set to 0b1111. The function
5722 returns return 1 if any of these fields in INSN reference the PC
5723 (also 0b1111, r15), else it returns 0. */
67255d04
RE
5724
5725static int
cca44b1b 5726insn_references_pc (uint32_t insn, uint32_t bitmask)
67255d04 5727{
cca44b1b 5728 uint32_t lowbit = 1;
67255d04 5729
cca44b1b
JB
5730 while (bitmask != 0)
5731 {
5732 uint32_t mask;
44e1a9eb 5733
cca44b1b
JB
5734 for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
5735 ;
67255d04 5736
cca44b1b
JB
5737 if (!lowbit)
5738 break;
67255d04 5739
cca44b1b 5740 mask = lowbit * 0xf;
67255d04 5741
cca44b1b
JB
5742 if ((insn & mask) == mask)
5743 return 1;
5744
5745 bitmask &= ~mask;
67255d04
RE
5746 }
5747
cca44b1b
JB
5748 return 0;
5749}
2af48f68 5750
cca44b1b
JB
5751/* The simplest copy function. Many instructions have the same effect no
5752 matter what address they are executed at: in those cases, use this. */
67255d04 5753
cca44b1b 5754static int
7ff120b4
YQ
5755arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn,
5756 const char *iname, struct displaced_step_closure *dsc)
cca44b1b
JB
5757{
5758 if (debug_displaced)
5759 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx, "
5760 "opcode/class '%s' unmodified\n", (unsigned long) insn,
5761 iname);
67255d04 5762
cca44b1b 5763 dsc->modinsn[0] = insn;
67255d04 5764
cca44b1b
JB
5765 return 0;
5766}
5767
34518530
YQ
5768static int
5769thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
5770 uint16_t insn2, const char *iname,
5771 struct displaced_step_closure *dsc)
5772{
5773 if (debug_displaced)
5774 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x %.4x, "
5775 "opcode/class '%s' unmodified\n", insn1, insn2,
5776 iname);
5777
5778 dsc->modinsn[0] = insn1;
5779 dsc->modinsn[1] = insn2;
5780 dsc->numinsns = 2;
5781
5782 return 0;
5783}
5784
5785/* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
5786 modification. */
5787static int
5788thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, unsigned int insn,
5789 const char *iname,
5790 struct displaced_step_closure *dsc)
5791{
5792 if (debug_displaced)
5793 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x, "
5794 "opcode/class '%s' unmodified\n", insn,
5795 iname);
5796
5797 dsc->modinsn[0] = insn;
5798
5799 return 0;
5800}
5801
cca44b1b
JB
5802/* Preload instructions with immediate offset. */
5803
5804static void
6e39997a 5805cleanup_preload (struct gdbarch *gdbarch,
cca44b1b
JB
5806 struct regcache *regs, struct displaced_step_closure *dsc)
5807{
5808 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5809 if (!dsc->u.preload.immed)
5810 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5811}
5812
7ff120b4
YQ
5813static void
5814install_preload (struct gdbarch *gdbarch, struct regcache *regs,
5815 struct displaced_step_closure *dsc, unsigned int rn)
cca44b1b 5816{
cca44b1b 5817 ULONGEST rn_val;
cca44b1b
JB
5818 /* Preload instructions:
5819
5820 {pli/pld} [rn, #+/-imm]
5821 ->
5822 {pli/pld} [r0, #+/-imm]. */
5823
36073a92
YQ
5824 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5825 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 5826 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
cca44b1b
JB
5827 dsc->u.preload.immed = 1;
5828
cca44b1b 5829 dsc->cleanup = &cleanup_preload;
cca44b1b
JB
5830}
5831
cca44b1b 5832static int
7ff120b4 5833arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
cca44b1b
JB
5834 struct displaced_step_closure *dsc)
5835{
5836 unsigned int rn = bits (insn, 16, 19);
cca44b1b 5837
7ff120b4
YQ
5838 if (!insn_references_pc (insn, 0x000f0000ul))
5839 return arm_copy_unmodified (gdbarch, insn, "preload", dsc);
cca44b1b
JB
5840
5841 if (debug_displaced)
5842 fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
5843 (unsigned long) insn);
5844
7ff120b4
YQ
5845 dsc->modinsn[0] = insn & 0xfff0ffff;
5846
5847 install_preload (gdbarch, regs, dsc, rn);
5848
5849 return 0;
5850}
5851
34518530
YQ
5852static int
5853thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
5854 struct regcache *regs, struct displaced_step_closure *dsc)
5855{
5856 unsigned int rn = bits (insn1, 0, 3);
5857 unsigned int u_bit = bit (insn1, 7);
5858 int imm12 = bits (insn2, 0, 11);
5859 ULONGEST pc_val;
5860
5861 if (rn != ARM_PC_REGNUM)
5862 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);
5863
5864 /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
5865 PLD (literal) Encoding T1. */
5866 if (debug_displaced)
5867 fprintf_unfiltered (gdb_stdlog,
5868 "displaced: copying pld/pli pc (0x%x) %c imm12 %.4x\n",
5869 (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
5870 imm12);
5871
5872 if (!u_bit)
5873 imm12 = -1 * imm12;
5874
5875 /* Rewrite instruction {pli/pld} PC imm12 into:
5876 Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12
5877
5878 {pli/pld} [r0, r1]
5879
5880 Cleanup: r0 <- tmp[0], r1 <- tmp[1]. */
5881
5882 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5883 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5884
5885 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
5886
5887 displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
5888 displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
5889 dsc->u.preload.immed = 0;
5890
5891 /* {pli/pld} [r0, r1] */
5892 dsc->modinsn[0] = insn1 & 0xfff0;
5893 dsc->modinsn[1] = 0xf001;
5894 dsc->numinsns = 2;
5895
5896 dsc->cleanup = &cleanup_preload;
5897 return 0;
5898}
5899
7ff120b4
YQ
5900/* Preload instructions with register offset. */
5901
5902static void
5903install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
5904 struct displaced_step_closure *dsc, unsigned int rn,
5905 unsigned int rm)
5906{
5907 ULONGEST rn_val, rm_val;
5908
cca44b1b
JB
5909 /* Preload register-offset instructions:
5910
5911 {pli/pld} [rn, rm {, shift}]
5912 ->
5913 {pli/pld} [r0, r1 {, shift}]. */
5914
36073a92
YQ
5915 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5916 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5917 rn_val = displaced_read_reg (regs, dsc, rn);
5918 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5919 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5920 displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
cca44b1b
JB
5921 dsc->u.preload.immed = 0;
5922
cca44b1b 5923 dsc->cleanup = &cleanup_preload;
7ff120b4
YQ
5924}
5925
5926static int
5927arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
5928 struct regcache *regs,
5929 struct displaced_step_closure *dsc)
5930{
5931 unsigned int rn = bits (insn, 16, 19);
5932 unsigned int rm = bits (insn, 0, 3);
5933
5934
5935 if (!insn_references_pc (insn, 0x000f000ful))
5936 return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);
5937
5938 if (debug_displaced)
5939 fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
5940 (unsigned long) insn);
5941
5942 dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
cca44b1b 5943
7ff120b4 5944 install_preload_reg (gdbarch, regs, dsc, rn, rm);
cca44b1b
JB
5945 return 0;
5946}
5947
5948/* Copy/cleanup coprocessor load and store instructions. */
5949
5950static void
6e39997a 5951cleanup_copro_load_store (struct gdbarch *gdbarch,
cca44b1b
JB
5952 struct regcache *regs,
5953 struct displaced_step_closure *dsc)
5954{
36073a92 5955 ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5956
5957 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5958
5959 if (dsc->u.ldst.writeback)
5960 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
5961}
5962
7ff120b4
YQ
5963static void
5964install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
5965 struct displaced_step_closure *dsc,
5966 int writeback, unsigned int rn)
cca44b1b 5967{
cca44b1b 5968 ULONGEST rn_val;
cca44b1b 5969
cca44b1b
JB
5970 /* Coprocessor load/store instructions:
5971
5972 {stc/stc2} [<Rn>, #+/-imm] (and other immediate addressing modes)
5973 ->
5974 {stc/stc2} [r0, #+/-imm].
5975
5976 ldc/ldc2 are handled identically. */
5977
36073a92
YQ
5978 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5979 rn_val = displaced_read_reg (regs, dsc, rn);
2b16b2e3
YQ
5980 /* PC should be 4-byte aligned. */
5981 rn_val = rn_val & 0xfffffffc;
cca44b1b
JB
5982 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5983
7ff120b4 5984 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
5985 dsc->u.ldst.rn = rn;
5986
7ff120b4
YQ
5987 dsc->cleanup = &cleanup_copro_load_store;
5988}
5989
5990static int
5991arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
5992 struct regcache *regs,
5993 struct displaced_step_closure *dsc)
5994{
5995 unsigned int rn = bits (insn, 16, 19);
5996
5997 if (!insn_references_pc (insn, 0x000f0000ul))
5998 return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);
5999
6000 if (debug_displaced)
6001 fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
6002 "load/store insn %.8lx\n", (unsigned long) insn);
6003
cca44b1b
JB
6004 dsc->modinsn[0] = insn & 0xfff0ffff;
6005
7ff120b4 6006 install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);
cca44b1b
JB
6007
6008 return 0;
6009}
6010
34518530
YQ
6011static int
6012thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
6013 uint16_t insn2, struct regcache *regs,
6014 struct displaced_step_closure *dsc)
6015{
6016 unsigned int rn = bits (insn1, 0, 3);
6017
6018 if (rn != ARM_PC_REGNUM)
6019 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6020 "copro load/store", dsc);
6021
6022 if (debug_displaced)
6023 fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
6024 "load/store insn %.4x%.4x\n", insn1, insn2);
6025
6026 dsc->modinsn[0] = insn1 & 0xfff0;
6027 dsc->modinsn[1] = insn2;
6028 dsc->numinsns = 2;
6029
6030 /* This function is called for copying instruction LDC/LDC2/VLDR, which
6031 doesn't support writeback, so pass 0. */
6032 install_copro_load_store (gdbarch, regs, dsc, 0, rn);
6033
6034 return 0;
6035}
6036
cca44b1b
JB
6037/* Clean up branch instructions (actually perform the branch, by setting
6038 PC). */
6039
6040static void
6e39997a 6041cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
6042 struct displaced_step_closure *dsc)
6043{
36073a92 6044 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
6045 int branch_taken = condition_true (dsc->u.branch.cond, status);
6046 enum pc_write_style write_pc = dsc->u.branch.exchange
6047 ? BX_WRITE_PC : BRANCH_WRITE_PC;
6048
6049 if (!branch_taken)
6050 return;
6051
6052 if (dsc->u.branch.link)
6053 {
8c8dba6d
YQ
6054 /* The value of LR should be the next insn of current one. In order
6055 not to confuse logic hanlding later insn `bx lr', if current insn mode
6056 is Thumb, the bit 0 of LR value should be set to 1. */
6057 ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;
6058
6059 if (dsc->is_thumb)
6060 next_insn_addr |= 0x1;
6061
6062 displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
6063 CANNOT_WRITE_PC);
cca44b1b
JB
6064 }
6065
bf9f652a 6066 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
cca44b1b
JB
6067}
6068
6069/* Copy B/BL/BLX instructions with immediate destinations. */
6070
7ff120b4
YQ
6071static void
6072install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
6073 struct displaced_step_closure *dsc,
6074 unsigned int cond, int exchange, int link, long offset)
6075{
6076 /* Implement "BL<cond> <label>" as:
6077
6078 Preparation: cond <- instruction condition
6079 Insn: mov r0, r0 (nop)
6080 Cleanup: if (condition true) { r14 <- pc; pc <- label }.
6081
6082 B<cond> similar, but don't set r14 in cleanup. */
6083
6084 dsc->u.branch.cond = cond;
6085 dsc->u.branch.link = link;
6086 dsc->u.branch.exchange = exchange;
6087
2b16b2e3
YQ
6088 dsc->u.branch.dest = dsc->insn_addr;
6089 if (link && exchange)
6090 /* For BLX, offset is computed from the Align (PC, 4). */
6091 dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;
6092
7ff120b4 6093 if (dsc->is_thumb)
2b16b2e3 6094 dsc->u.branch.dest += 4 + offset;
7ff120b4 6095 else
2b16b2e3 6096 dsc->u.branch.dest += 8 + offset;
7ff120b4
YQ
6097
6098 dsc->cleanup = &cleanup_branch;
6099}
cca44b1b 6100static int
7ff120b4
YQ
6101arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
6102 struct regcache *regs, struct displaced_step_closure *dsc)
cca44b1b
JB
6103{
6104 unsigned int cond = bits (insn, 28, 31);
6105 int exchange = (cond == 0xf);
6106 int link = exchange || bit (insn, 24);
cca44b1b
JB
6107 long offset;
6108
6109 if (debug_displaced)
6110 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s immediate insn "
6111 "%.8lx\n", (exchange) ? "blx" : (link) ? "bl" : "b",
6112 (unsigned long) insn);
cca44b1b
JB
6113 if (exchange)
6114 /* For BLX, set bit 0 of the destination. The cleanup_branch function will
6115 then arrange the switch into Thumb mode. */
6116 offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
6117 else
6118 offset = bits (insn, 0, 23) << 2;
6119
6120 if (bit (offset, 25))
6121 offset = offset | ~0x3ffffff;
6122
cca44b1b
JB
6123 dsc->modinsn[0] = ARM_NOP;
6124
7ff120b4 6125 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
cca44b1b
JB
6126 return 0;
6127}
6128
34518530
YQ
6129static int
6130thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
6131 uint16_t insn2, struct regcache *regs,
6132 struct displaced_step_closure *dsc)
6133{
6134 int link = bit (insn2, 14);
6135 int exchange = link && !bit (insn2, 12);
6136 int cond = INST_AL;
6137 long offset = 0;
6138 int j1 = bit (insn2, 13);
6139 int j2 = bit (insn2, 11);
6140 int s = sbits (insn1, 10, 10);
6141 int i1 = !(j1 ^ bit (insn1, 10));
6142 int i2 = !(j2 ^ bit (insn1, 10));
6143
6144 if (!link && !exchange) /* B */
6145 {
6146 offset = (bits (insn2, 0, 10) << 1);
6147 if (bit (insn2, 12)) /* Encoding T4 */
6148 {
6149 offset |= (bits (insn1, 0, 9) << 12)
6150 | (i2 << 22)
6151 | (i1 << 23)
6152 | (s << 24);
6153 cond = INST_AL;
6154 }
6155 else /* Encoding T3 */
6156 {
6157 offset |= (bits (insn1, 0, 5) << 12)
6158 | (j1 << 18)
6159 | (j2 << 19)
6160 | (s << 20);
6161 cond = bits (insn1, 6, 9);
6162 }
6163 }
6164 else
6165 {
6166 offset = (bits (insn1, 0, 9) << 12);
6167 offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
6168 offset |= exchange ?
6169 (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
6170 }
6171
6172 if (debug_displaced)
6173 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s insn "
6174 "%.4x %.4x with offset %.8lx\n",
6175 link ? (exchange) ? "blx" : "bl" : "b",
6176 insn1, insn2, offset);
6177
6178 dsc->modinsn[0] = THUMB_NOP;
6179
6180 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
6181 return 0;
6182}
6183
6184/* Copy B Thumb instructions. */
6185static int
6186thumb_copy_b (struct gdbarch *gdbarch, unsigned short insn,
6187 struct displaced_step_closure *dsc)
6188{
6189 unsigned int cond = 0;
6190 int offset = 0;
6191 unsigned short bit_12_15 = bits (insn, 12, 15);
6192 CORE_ADDR from = dsc->insn_addr;
6193
6194 if (bit_12_15 == 0xd)
6195 {
6196 /* offset = SignExtend (imm8:0, 32) */
6197 offset = sbits ((insn << 1), 0, 8);
6198 cond = bits (insn, 8, 11);
6199 }
6200 else if (bit_12_15 == 0xe) /* Encoding T2 */
6201 {
6202 offset = sbits ((insn << 1), 0, 11);
6203 cond = INST_AL;
6204 }
6205
6206 if (debug_displaced)
6207 fprintf_unfiltered (gdb_stdlog,
6208 "displaced: copying b immediate insn %.4x "
6209 "with offset %d\n", insn, offset);
6210
6211 dsc->u.branch.cond = cond;
6212 dsc->u.branch.link = 0;
6213 dsc->u.branch.exchange = 0;
6214 dsc->u.branch.dest = from + 4 + offset;
6215
6216 dsc->modinsn[0] = THUMB_NOP;
6217
6218 dsc->cleanup = &cleanup_branch;
6219
6220 return 0;
6221}
6222
cca44b1b
JB
6223/* Copy BX/BLX with register-specified destinations. */
6224
7ff120b4
YQ
6225static void
6226install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
6227 struct displaced_step_closure *dsc, int link,
6228 unsigned int cond, unsigned int rm)
cca44b1b 6229{
cca44b1b
JB
6230 /* Implement {BX,BLX}<cond> <reg>" as:
6231
6232 Preparation: cond <- instruction condition
6233 Insn: mov r0, r0 (nop)
6234 Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
6235
6236 Don't set r14 in cleanup for BX. */
6237
36073a92 6238 dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6239
6240 dsc->u.branch.cond = cond;
6241 dsc->u.branch.link = link;
cca44b1b 6242
7ff120b4 6243 dsc->u.branch.exchange = 1;
cca44b1b
JB
6244
6245 dsc->cleanup = &cleanup_branch;
7ff120b4 6246}
cca44b1b 6247
7ff120b4
YQ
6248static int
6249arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
6250 struct regcache *regs, struct displaced_step_closure *dsc)
6251{
6252 unsigned int cond = bits (insn, 28, 31);
6253 /* BX: x12xxx1x
6254 BLX: x12xxx3x. */
6255 int link = bit (insn, 5);
6256 unsigned int rm = bits (insn, 0, 3);
6257
6258 if (debug_displaced)
6259 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx",
6260 (unsigned long) insn);
6261
6262 dsc->modinsn[0] = ARM_NOP;
6263
6264 install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
cca44b1b
JB
6265 return 0;
6266}
6267
34518530
YQ
6268static int
6269thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
6270 struct regcache *regs,
6271 struct displaced_step_closure *dsc)
6272{
6273 int link = bit (insn, 7);
6274 unsigned int rm = bits (insn, 3, 6);
6275
6276 if (debug_displaced)
6277 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x",
6278 (unsigned short) insn);
6279
6280 dsc->modinsn[0] = THUMB_NOP;
6281
6282 install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);
6283
6284 return 0;
6285}
6286
6287
0963b4bd 6288/* Copy/cleanup arithmetic/logic instruction with immediate RHS. */
cca44b1b
JB
6289
6290static void
6e39997a 6291cleanup_alu_imm (struct gdbarch *gdbarch,
cca44b1b
JB
6292 struct regcache *regs, struct displaced_step_closure *dsc)
6293{
36073a92 6294 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6295 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6296 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6297 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6298}
6299
6300static int
7ff120b4
YQ
6301arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
6302 struct displaced_step_closure *dsc)
cca44b1b
JB
6303{
6304 unsigned int rn = bits (insn, 16, 19);
6305 unsigned int rd = bits (insn, 12, 15);
6306 unsigned int op = bits (insn, 21, 24);
6307 int is_mov = (op == 0xd);
6308 ULONGEST rd_val, rn_val;
cca44b1b
JB
6309
6310 if (!insn_references_pc (insn, 0x000ff000ul))
7ff120b4 6311 return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
cca44b1b
JB
6312
6313 if (debug_displaced)
6314 fprintf_unfiltered (gdb_stdlog, "displaced: copying immediate %s insn "
6315 "%.8lx\n", is_mov ? "move" : "ALU",
6316 (unsigned long) insn);
6317
6318 /* Instruction is of form:
6319
6320 <op><cond> rd, [rn,] #imm
6321
6322 Rewrite as:
6323
6324 Preparation: tmp1, tmp2 <- r0, r1;
6325 r0, r1 <- rd, rn
6326 Insn: <op><cond> r0, r1, #imm
6327 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6328 */
6329
36073a92
YQ
6330 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6331 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6332 rn_val = displaced_read_reg (regs, dsc, rn);
6333 rd_val = displaced_read_reg (regs, dsc, rd);
cca44b1b
JB
6334 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6335 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6336 dsc->rd = rd;
6337
6338 if (is_mov)
6339 dsc->modinsn[0] = insn & 0xfff00fff;
6340 else
6341 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
6342
6343 dsc->cleanup = &cleanup_alu_imm;
6344
6345 return 0;
6346}
6347
34518530
YQ
6348static int
6349thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
6350 uint16_t insn2, struct regcache *regs,
6351 struct displaced_step_closure *dsc)
6352{
6353 unsigned int op = bits (insn1, 5, 8);
6354 unsigned int rn, rm, rd;
6355 ULONGEST rd_val, rn_val;
6356
6357 rn = bits (insn1, 0, 3); /* Rn */
6358 rm = bits (insn2, 0, 3); /* Rm */
6359 rd = bits (insn2, 8, 11); /* Rd */
6360
6361 /* This routine is only called for instruction MOV. */
6362 gdb_assert (op == 0x2 && rn == 0xf);
6363
6364 if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
6365 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);
6366
6367 if (debug_displaced)
6368 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.4x%.4x\n",
6369 "ALU", insn1, insn2);
6370
6371 /* Instruction is of form:
6372
6373 <op><cond> rd, [rn,] #imm
6374
6375 Rewrite as:
6376
6377 Preparation: tmp1, tmp2 <- r0, r1;
6378 r0, r1 <- rd, rn
6379 Insn: <op><cond> r0, r1, #imm
6380 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6381 */
6382
6383 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6384 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6385 rn_val = displaced_read_reg (regs, dsc, rn);
6386 rd_val = displaced_read_reg (regs, dsc, rd);
6387 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6388 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6389 dsc->rd = rd;
6390
6391 dsc->modinsn[0] = insn1;
6392 dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
6393 dsc->numinsns = 2;
6394
6395 dsc->cleanup = &cleanup_alu_imm;
6396
6397 return 0;
6398}
6399
cca44b1b
JB
6400/* Copy/cleanup arithmetic/logic insns with register RHS. */
6401
6402static void
6e39997a 6403cleanup_alu_reg (struct gdbarch *gdbarch,
cca44b1b
JB
6404 struct regcache *regs, struct displaced_step_closure *dsc)
6405{
6406 ULONGEST rd_val;
6407 int i;
6408
36073a92 6409 rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6410
6411 for (i = 0; i < 3; i++)
6412 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6413
6414 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6415}
6416
7ff120b4
YQ
6417static void
6418install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
6419 struct displaced_step_closure *dsc,
6420 unsigned int rd, unsigned int rn, unsigned int rm)
cca44b1b 6421{
cca44b1b 6422 ULONGEST rd_val, rn_val, rm_val;
cca44b1b 6423
cca44b1b
JB
6424 /* Instruction is of form:
6425
6426 <op><cond> rd, [rn,] rm [, <shift>]
6427
6428 Rewrite as:
6429
6430 Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
6431 r0, r1, r2 <- rd, rn, rm
ef713951 6432 Insn: <op><cond> r0, [r1,] r2 [, <shift>]
cca44b1b
JB
6433 Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
6434 */
6435
36073a92
YQ
6436 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6437 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6438 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6439 rd_val = displaced_read_reg (regs, dsc, rd);
6440 rn_val = displaced_read_reg (regs, dsc, rn);
6441 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6442 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6443 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6444 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6445 dsc->rd = rd;
6446
7ff120b4
YQ
6447 dsc->cleanup = &cleanup_alu_reg;
6448}
6449
6450static int
6451arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
6452 struct displaced_step_closure *dsc)
6453{
6454 unsigned int op = bits (insn, 21, 24);
6455 int is_mov = (op == 0xd);
6456
6457 if (!insn_references_pc (insn, 0x000ff00ful))
6458 return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);
6459
6460 if (debug_displaced)
6461 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.8lx\n",
6462 is_mov ? "move" : "ALU", (unsigned long) insn);
6463
cca44b1b
JB
6464 if (is_mov)
6465 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
6466 else
6467 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
6468
7ff120b4
YQ
6469 install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
6470 bits (insn, 0, 3));
cca44b1b
JB
6471 return 0;
6472}
6473
34518530
YQ
6474static int
6475thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
6476 struct regcache *regs,
6477 struct displaced_step_closure *dsc)
6478{
ef713951 6479 unsigned rm, rd;
34518530 6480
ef713951
YQ
6481 rm = bits (insn, 3, 6);
6482 rd = (bit (insn, 7) << 3) | bits (insn, 0, 2);
34518530 6483
ef713951 6484 if (rd != ARM_PC_REGNUM && rm != ARM_PC_REGNUM)
34518530
YQ
6485 return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);
6486
6487 if (debug_displaced)
ef713951
YQ
6488 fprintf_unfiltered (gdb_stdlog, "displaced: copying ALU reg insn %.4x\n",
6489 (unsigned short) insn);
34518530 6490
ef713951 6491 dsc->modinsn[0] = ((insn & 0xff00) | 0x10);
34518530 6492
ef713951 6493 install_alu_reg (gdbarch, regs, dsc, rd, rd, rm);
34518530
YQ
6494
6495 return 0;
6496}
6497
cca44b1b
JB
6498/* Cleanup/copy arithmetic/logic insns with shifted register RHS. */
6499
6500static void
6e39997a 6501cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
cca44b1b
JB
6502 struct regcache *regs,
6503 struct displaced_step_closure *dsc)
6504{
36073a92 6505 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6506 int i;
6507
6508 for (i = 0; i < 4; i++)
6509 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6510
6511 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6512}
6513
7ff120b4
YQ
6514static void
6515install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
6516 struct displaced_step_closure *dsc,
6517 unsigned int rd, unsigned int rn, unsigned int rm,
6518 unsigned rs)
cca44b1b 6519{
7ff120b4 6520 int i;
cca44b1b 6521 ULONGEST rd_val, rn_val, rm_val, rs_val;
cca44b1b 6522
cca44b1b
JB
6523 /* Instruction is of form:
6524
6525 <op><cond> rd, [rn,] rm, <shift> rs
6526
6527 Rewrite as:
6528
6529 Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
6530 r0, r1, r2, r3 <- rd, rn, rm, rs
6531 Insn: <op><cond> r0, r1, r2, <shift> r3
6532 Cleanup: tmp5 <- r0
6533 r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
6534 rd <- tmp5
6535 */
6536
6537 for (i = 0; i < 4; i++)
36073a92 6538 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b 6539
36073a92
YQ
6540 rd_val = displaced_read_reg (regs, dsc, rd);
6541 rn_val = displaced_read_reg (regs, dsc, rn);
6542 rm_val = displaced_read_reg (regs, dsc, rm);
6543 rs_val = displaced_read_reg (regs, dsc, rs);
cca44b1b
JB
6544 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6545 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6546 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6547 displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
6548 dsc->rd = rd;
7ff120b4
YQ
6549 dsc->cleanup = &cleanup_alu_shifted_reg;
6550}
6551
6552static int
6553arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
6554 struct regcache *regs,
6555 struct displaced_step_closure *dsc)
6556{
6557 unsigned int op = bits (insn, 21, 24);
6558 int is_mov = (op == 0xd);
6559 unsigned int rd, rn, rm, rs;
6560
6561 if (!insn_references_pc (insn, 0x000fff0ful))
6562 return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
6563
6564 if (debug_displaced)
6565 fprintf_unfiltered (gdb_stdlog, "displaced: copying shifted reg %s insn "
6566 "%.8lx\n", is_mov ? "move" : "ALU",
6567 (unsigned long) insn);
6568
6569 rn = bits (insn, 16, 19);
6570 rm = bits (insn, 0, 3);
6571 rs = bits (insn, 8, 11);
6572 rd = bits (insn, 12, 15);
cca44b1b
JB
6573
6574 if (is_mov)
6575 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
6576 else
6577 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
6578
7ff120b4 6579 install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);
cca44b1b
JB
6580
6581 return 0;
6582}
6583
6584/* Clean up load instructions. */
6585
6586static void
6e39997a 6587cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
6588 struct displaced_step_closure *dsc)
6589{
6590 ULONGEST rt_val, rt_val2 = 0, rn_val;
cca44b1b 6591
36073a92 6592 rt_val = displaced_read_reg (regs, dsc, 0);
cca44b1b 6593 if (dsc->u.ldst.xfersize == 8)
36073a92
YQ
6594 rt_val2 = displaced_read_reg (regs, dsc, 1);
6595 rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
6596
6597 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6598 if (dsc->u.ldst.xfersize > 4)
6599 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6600 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6601 if (!dsc->u.ldst.immed)
6602 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6603
6604 /* Handle register writeback. */
6605 if (dsc->u.ldst.writeback)
6606 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6607 /* Put result in right place. */
6608 displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
6609 if (dsc->u.ldst.xfersize == 8)
6610 displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
6611}
6612
6613/* Clean up store instructions. */
6614
6615static void
6e39997a 6616cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
6617 struct displaced_step_closure *dsc)
6618{
36073a92 6619 ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
6620
6621 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6622 if (dsc->u.ldst.xfersize > 4)
6623 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6624 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6625 if (!dsc->u.ldst.immed)
6626 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6627 if (!dsc->u.ldst.restore_r4)
6628 displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
6629
6630 /* Writeback. */
6631 if (dsc->u.ldst.writeback)
6632 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6633}
6634
6635/* Copy "extra" load/store instructions. These are halfword/doubleword
6636 transfers, which have a different encoding to byte/word transfers. */
6637
6638static int
7ff120b4
YQ
6639arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unpriveleged,
6640 struct regcache *regs, struct displaced_step_closure *dsc)
cca44b1b
JB
6641{
6642 unsigned int op1 = bits (insn, 20, 24);
6643 unsigned int op2 = bits (insn, 5, 6);
6644 unsigned int rt = bits (insn, 12, 15);
6645 unsigned int rn = bits (insn, 16, 19);
6646 unsigned int rm = bits (insn, 0, 3);
6647 char load[12] = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
6648 char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
6649 int immed = (op1 & 0x4) != 0;
6650 int opcode;
6651 ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
cca44b1b
JB
6652
6653 if (!insn_references_pc (insn, 0x000ff00ful))
7ff120b4 6654 return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);
cca44b1b
JB
6655
6656 if (debug_displaced)
6657 fprintf_unfiltered (gdb_stdlog, "displaced: copying %sextra load/store "
6658 "insn %.8lx\n", unpriveleged ? "unpriveleged " : "",
6659 (unsigned long) insn);
6660
6661 opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
6662
6663 if (opcode < 0)
6664 internal_error (__FILE__, __LINE__,
6665 _("copy_extra_ld_st: instruction decode error"));
6666
36073a92
YQ
6667 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6668 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6669 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 6670 if (!immed)
36073a92 6671 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 6672
36073a92 6673 rt_val = displaced_read_reg (regs, dsc, rt);
cca44b1b 6674 if (bytesize[opcode] == 8)
36073a92
YQ
6675 rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
6676 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 6677 if (!immed)
36073a92 6678 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6679
6680 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6681 if (bytesize[opcode] == 8)
6682 displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
6683 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6684 if (!immed)
6685 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
6686
6687 dsc->rd = rt;
6688 dsc->u.ldst.xfersize = bytesize[opcode];
6689 dsc->u.ldst.rn = rn;
6690 dsc->u.ldst.immed = immed;
6691 dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
6692 dsc->u.ldst.restore_r4 = 0;
6693
6694 if (immed)
6695 /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
6696 ->
6697 {ldr,str}<width><cond> r0, [r1,] [r2, #imm]. */
6698 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6699 else
6700 /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
6701 ->
6702 {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3]. */
6703 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6704
6705 dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
6706
6707 return 0;
6708}
6709
0f6f04ba 6710/* Copy byte/half word/word loads and stores. */
cca44b1b 6711
7ff120b4 6712static void
0f6f04ba
YQ
6713install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
6714 struct displaced_step_closure *dsc, int load,
6715 int immed, int writeback, int size, int usermode,
6716 int rt, int rm, int rn)
cca44b1b 6717{
cca44b1b 6718 ULONGEST rt_val, rn_val, rm_val = 0;
cca44b1b 6719
36073a92
YQ
6720 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6721 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 6722 if (!immed)
36073a92 6723 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 6724 if (!load)
36073a92 6725 dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);
cca44b1b 6726
36073a92
YQ
6727 rt_val = displaced_read_reg (regs, dsc, rt);
6728 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 6729 if (!immed)
36073a92 6730 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6731
6732 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6733 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6734 if (!immed)
6735 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
cca44b1b 6736 dsc->rd = rt;
0f6f04ba 6737 dsc->u.ldst.xfersize = size;
cca44b1b
JB
6738 dsc->u.ldst.rn = rn;
6739 dsc->u.ldst.immed = immed;
7ff120b4 6740 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
6741
6742 /* To write PC we can do:
6743
494e194e
YQ
6744 Before this sequence of instructions:
6745 r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
6746 r2 is the Rn value got from dispalced_read_reg.
6747
6748 Insn1: push {pc} Write address of STR instruction + offset on stack
6749 Insn2: pop {r4} Read it back from stack, r4 = addr(Insn1) + offset
6750 Insn3: sub r4, r4, pc r4 = addr(Insn1) + offset - pc
6751 = addr(Insn1) + offset - addr(Insn3) - 8
6752 = offset - 16
6753 Insn4: add r4, r4, #8 r4 = offset - 8
6754 Insn5: add r0, r0, r4 r0 = from + 8 + offset - 8
6755 = from + offset
6756 Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])
cca44b1b
JB
6757
6758 Otherwise we don't know what value to write for PC, since the offset is
494e194e
YQ
6759 architecture-dependent (sometimes PC+8, sometimes PC+12). More details
6760 of this can be found in Section "Saving from r15" in
6761 http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */
cca44b1b 6762
7ff120b4
YQ
6763 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6764}
6765
34518530
YQ
6766
6767static int
6768thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
6769 uint16_t insn2, struct regcache *regs,
6770 struct displaced_step_closure *dsc, int size)
6771{
6772 unsigned int u_bit = bit (insn1, 7);
6773 unsigned int rt = bits (insn2, 12, 15);
6774 int imm12 = bits (insn2, 0, 11);
6775 ULONGEST pc_val;
6776
6777 if (debug_displaced)
6778 fprintf_unfiltered (gdb_stdlog,
6779 "displaced: copying ldr pc (0x%x) R%d %c imm12 %.4x\n",
6780 (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
6781 imm12);
6782
6783 if (!u_bit)
6784 imm12 = -1 * imm12;
6785
6786 /* Rewrite instruction LDR Rt imm12 into:
6787
6788 Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12
6789
6790 LDR R0, R2, R3,
6791
6792 Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2]. */
6793
6794
6795 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6796 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6797 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6798
6799 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6800
6801 pc_val = pc_val & 0xfffffffc;
6802
6803 displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
6804 displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);
6805
6806 dsc->rd = rt;
6807
6808 dsc->u.ldst.xfersize = size;
6809 dsc->u.ldst.immed = 0;
6810 dsc->u.ldst.writeback = 0;
6811 dsc->u.ldst.restore_r4 = 0;
6812
6813 /* LDR R0, R2, R3 */
6814 dsc->modinsn[0] = 0xf852;
6815 dsc->modinsn[1] = 0x3;
6816 dsc->numinsns = 2;
6817
6818 dsc->cleanup = &cleanup_load;
6819
6820 return 0;
6821}
6822
6823static int
6824thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
6825 uint16_t insn2, struct regcache *regs,
6826 struct displaced_step_closure *dsc,
6827 int writeback, int immed)
6828{
6829 unsigned int rt = bits (insn2, 12, 15);
6830 unsigned int rn = bits (insn1, 0, 3);
6831 unsigned int rm = bits (insn2, 0, 3); /* Only valid if !immed. */
6832 /* In LDR (register), there is also a register Rm, which is not allowed to
6833 be PC, so we don't have to check it. */
6834
6835 if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
6836 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
6837 dsc);
6838
6839 if (debug_displaced)
6840 fprintf_unfiltered (gdb_stdlog,
6841 "displaced: copying ldr r%d [r%d] insn %.4x%.4x\n",
6842 rt, rn, insn1, insn2);
6843
6844 install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
6845 0, rt, rm, rn);
6846
6847 dsc->u.ldst.restore_r4 = 0;
6848
6849 if (immed)
6850 /* ldr[b]<cond> rt, [rn, #imm], etc.
6851 ->
6852 ldr[b]<cond> r0, [r2, #imm]. */
6853 {
6854 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6855 dsc->modinsn[1] = insn2 & 0x0fff;
6856 }
6857 else
6858 /* ldr[b]<cond> rt, [rn, rm], etc.
6859 ->
6860 ldr[b]<cond> r0, [r2, r3]. */
6861 {
6862 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6863 dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
6864 }
6865
6866 dsc->numinsns = 2;
6867
6868 return 0;
6869}
6870
6871
7ff120b4
YQ
6872static int
6873arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
6874 struct regcache *regs,
6875 struct displaced_step_closure *dsc,
0f6f04ba 6876 int load, int size, int usermode)
7ff120b4
YQ
6877{
6878 int immed = !bit (insn, 25);
6879 int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
6880 unsigned int rt = bits (insn, 12, 15);
6881 unsigned int rn = bits (insn, 16, 19);
6882 unsigned int rm = bits (insn, 0, 3); /* Only valid if !immed. */
6883
6884 if (!insn_references_pc (insn, 0x000ff00ful))
6885 return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);
6886
6887 if (debug_displaced)
6888 fprintf_unfiltered (gdb_stdlog,
6889 "displaced: copying %s%s r%d [r%d] insn %.8lx\n",
0f6f04ba
YQ
6890 load ? (size == 1 ? "ldrb" : "ldr")
6891 : (size == 1 ? "strb" : "str"), usermode ? "t" : "",
7ff120b4
YQ
6892 rt, rn,
6893 (unsigned long) insn);
6894
0f6f04ba
YQ
6895 install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
6896 usermode, rt, rm, rn);
7ff120b4 6897
bf9f652a 6898 if (load || rt != ARM_PC_REGNUM)
cca44b1b
JB
6899 {
6900 dsc->u.ldst.restore_r4 = 0;
6901
6902 if (immed)
6903 /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
6904 ->
6905 {ldr,str}[b]<cond> r0, [r2, #imm]. */
6906 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6907 else
6908 /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
6909 ->
6910 {ldr,str}[b]<cond> r0, [r2, r3]. */
6911 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6912 }
6913 else
6914 {
6915 /* We need to use r4 as scratch. Make sure it's restored afterwards. */
6916 dsc->u.ldst.restore_r4 = 1;
494e194e
YQ
6917 dsc->modinsn[0] = 0xe92d8000; /* push {pc} */
6918 dsc->modinsn[1] = 0xe8bd0010; /* pop {r4} */
cca44b1b
JB
6919 dsc->modinsn[2] = 0xe044400f; /* sub r4, r4, pc. */
6920 dsc->modinsn[3] = 0xe2844008; /* add r4, r4, #8. */
6921 dsc->modinsn[4] = 0xe0800004; /* add r0, r0, r4. */
6922
6923 /* As above. */
6924 if (immed)
6925 dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
6926 else
6927 dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
6928
cca44b1b
JB
6929 dsc->numinsns = 6;
6930 }
6931
6932 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6933
6934 return 0;
6935}
6936
6937/* Cleanup LDM instructions with fully-populated register list. This is an
6938 unfortunate corner case: it's impossible to implement correctly by modifying
6939 the instruction. The issue is as follows: we have an instruction,
6940
6941 ldm rN, {r0-r15}
6942
6943 which we must rewrite to avoid loading PC. A possible solution would be to
6944 do the load in two halves, something like (with suitable cleanup
6945 afterwards):
6946
6947 mov r8, rN
6948 ldm[id][ab] r8!, {r0-r7}
6949 str r7, <temp>
6950 ldm[id][ab] r8, {r7-r14}
6951 <bkpt>
6952
6953 but at present there's no suitable place for <temp>, since the scratch space
6954 is overwritten before the cleanup routine is called. For now, we simply
6955 emulate the instruction. */
6956
6957static void
6958cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
6959 struct displaced_step_closure *dsc)
6960{
cca44b1b
JB
6961 int inc = dsc->u.block.increment;
6962 int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
6963 int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
6964 uint32_t regmask = dsc->u.block.regmask;
6965 int regno = inc ? 0 : 15;
6966 CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
6967 int exception_return = dsc->u.block.load && dsc->u.block.user
6968 && (regmask & 0x8000) != 0;
36073a92 6969 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
6970 int do_transfer = condition_true (dsc->u.block.cond, status);
6971 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6972
6973 if (!do_transfer)
6974 return;
6975
6976 /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
6977 sensible we can do here. Complain loudly. */
6978 if (exception_return)
6979 error (_("Cannot single-step exception return"));
6980
6981 /* We don't handle any stores here for now. */
6982 gdb_assert (dsc->u.block.load != 0);
6983
6984 if (debug_displaced)
6985 fprintf_unfiltered (gdb_stdlog, "displaced: emulating block transfer: "
6986 "%s %s %s\n", dsc->u.block.load ? "ldm" : "stm",
6987 dsc->u.block.increment ? "inc" : "dec",
6988 dsc->u.block.before ? "before" : "after");
6989
6990 while (regmask)
6991 {
6992 uint32_t memword;
6993
6994 if (inc)
bf9f652a 6995 while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
cca44b1b
JB
6996 regno++;
6997 else
6998 while (regno >= 0 && (regmask & (1 << regno)) == 0)
6999 regno--;
7000
7001 xfer_addr += bump_before;
7002
7003 memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
7004 displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
7005
7006 xfer_addr += bump_after;
7007
7008 regmask &= ~(1 << regno);
7009 }
7010
7011 if (dsc->u.block.writeback)
7012 displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
7013 CANNOT_WRITE_PC);
7014}
7015
7016/* Clean up an STM which included the PC in the register list. */
7017
7018static void
7019cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
7020 struct displaced_step_closure *dsc)
7021{
36073a92 7022 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
7023 int store_executed = condition_true (dsc->u.block.cond, status);
7024 CORE_ADDR pc_stored_at, transferred_regs = bitcount (dsc->u.block.regmask);
7025 CORE_ADDR stm_insn_addr;
7026 uint32_t pc_val;
7027 long offset;
7028 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7029
7030 /* If condition code fails, there's nothing else to do. */
7031 if (!store_executed)
7032 return;
7033
7034 if (dsc->u.block.increment)
7035 {
7036 pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
7037
7038 if (dsc->u.block.before)
7039 pc_stored_at += 4;
7040 }
7041 else
7042 {
7043 pc_stored_at = dsc->u.block.xfer_addr;
7044
7045 if (dsc->u.block.before)
7046 pc_stored_at -= 4;
7047 }
7048
7049 pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
7050 stm_insn_addr = dsc->scratch_base;
7051 offset = pc_val - stm_insn_addr;
7052
7053 if (debug_displaced)
7054 fprintf_unfiltered (gdb_stdlog, "displaced: detected PC offset %.8lx for "
7055 "STM instruction\n", offset);
7056
7057 /* Rewrite the stored PC to the proper value for the non-displaced original
7058 instruction. */
7059 write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
7060 dsc->insn_addr + offset);
7061}
7062
7063/* Clean up an LDM which includes the PC in the register list. We clumped all
7064 the registers in the transferred list into a contiguous range r0...rX (to
7065 avoid loading PC directly and losing control of the debugged program), so we
7066 must undo that here. */
7067
7068static void
6e39997a 7069cleanup_block_load_pc (struct gdbarch *gdbarch,
cca44b1b
JB
7070 struct regcache *regs,
7071 struct displaced_step_closure *dsc)
7072{
36073a92 7073 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
22e048c9 7074 int load_executed = condition_true (dsc->u.block.cond, status);
bf9f652a 7075 unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
cca44b1b
JB
7076 unsigned int regs_loaded = bitcount (mask);
7077 unsigned int num_to_shuffle = regs_loaded, clobbered;
7078
7079 /* The method employed here will fail if the register list is fully populated
7080 (we need to avoid loading PC directly). */
7081 gdb_assert (num_to_shuffle < 16);
7082
7083 if (!load_executed)
7084 return;
7085
7086 clobbered = (1 << num_to_shuffle) - 1;
7087
7088 while (num_to_shuffle > 0)
7089 {
7090 if ((mask & (1 << write_reg)) != 0)
7091 {
7092 unsigned int read_reg = num_to_shuffle - 1;
7093
7094 if (read_reg != write_reg)
7095 {
36073a92 7096 ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
cca44b1b
JB
7097 displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
7098 if (debug_displaced)
7099 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: move "
7100 "loaded register r%d to r%d\n"), read_reg,
7101 write_reg);
7102 }
7103 else if (debug_displaced)
7104 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: register "
7105 "r%d already in the right place\n"),
7106 write_reg);
7107
7108 clobbered &= ~(1 << write_reg);
7109
7110 num_to_shuffle--;
7111 }
7112
7113 write_reg--;
7114 }
7115
7116 /* Restore any registers we scribbled over. */
7117 for (write_reg = 0; clobbered != 0; write_reg++)
7118 {
7119 if ((clobbered & (1 << write_reg)) != 0)
7120 {
7121 displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
7122 CANNOT_WRITE_PC);
7123 if (debug_displaced)
7124 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: restored "
7125 "clobbered register r%d\n"), write_reg);
7126 clobbered &= ~(1 << write_reg);
7127 }
7128 }
7129
7130 /* Perform register writeback manually. */
7131 if (dsc->u.block.writeback)
7132 {
7133 ULONGEST new_rn_val = dsc->u.block.xfer_addr;
7134
7135 if (dsc->u.block.increment)
7136 new_rn_val += regs_loaded * 4;
7137 else
7138 new_rn_val -= regs_loaded * 4;
7139
7140 displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
7141 CANNOT_WRITE_PC);
7142 }
7143}
7144
7145/* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
7146 in user-level code (in particular exception return, ldm rn, {...pc}^). */
7147
7148static int
7ff120b4
YQ
7149arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
7150 struct regcache *regs,
7151 struct displaced_step_closure *dsc)
cca44b1b
JB
7152{
7153 int load = bit (insn, 20);
7154 int user = bit (insn, 22);
7155 int increment = bit (insn, 23);
7156 int before = bit (insn, 24);
7157 int writeback = bit (insn, 21);
7158 int rn = bits (insn, 16, 19);
cca44b1b 7159
0963b4bd
MS
7160 /* Block transfers which don't mention PC can be run directly
7161 out-of-line. */
bf9f652a 7162 if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
7ff120b4 7163 return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
cca44b1b 7164
bf9f652a 7165 if (rn == ARM_PC_REGNUM)
cca44b1b 7166 {
0963b4bd
MS
7167 warning (_("displaced: Unpredictable LDM or STM with "
7168 "base register r15"));
7ff120b4 7169 return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
cca44b1b
JB
7170 }
7171
7172 if (debug_displaced)
7173 fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
7174 "%.8lx\n", (unsigned long) insn);
7175
36073a92 7176 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b
JB
7177 dsc->u.block.rn = rn;
7178
7179 dsc->u.block.load = load;
7180 dsc->u.block.user = user;
7181 dsc->u.block.increment = increment;
7182 dsc->u.block.before = before;
7183 dsc->u.block.writeback = writeback;
7184 dsc->u.block.cond = bits (insn, 28, 31);
7185
7186 dsc->u.block.regmask = insn & 0xffff;
7187
7188 if (load)
7189 {
7190 if ((insn & 0xffff) == 0xffff)
7191 {
7192 /* LDM with a fully-populated register list. This case is
7193 particularly tricky. Implement for now by fully emulating the
7194 instruction (which might not behave perfectly in all cases, but
7195 these instructions should be rare enough for that not to matter
7196 too much). */
7197 dsc->modinsn[0] = ARM_NOP;
7198
7199 dsc->cleanup = &cleanup_block_load_all;
7200 }
7201 else
7202 {
7203 /* LDM of a list of registers which includes PC. Implement by
7204 rewriting the list of registers to be transferred into a
7205 contiguous chunk r0...rX before doing the transfer, then shuffling
7206 registers into the correct places in the cleanup routine. */
7207 unsigned int regmask = insn & 0xffff;
7208 unsigned int num_in_list = bitcount (regmask), new_regmask, bit = 1;
7209 unsigned int to = 0, from = 0, i, new_rn;
7210
7211 for (i = 0; i < num_in_list; i++)
36073a92 7212 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b
JB
7213
7214 /* Writeback makes things complicated. We need to avoid clobbering
7215 the base register with one of the registers in our modified
7216 register list, but just using a different register can't work in
7217 all cases, e.g.:
7218
7219 ldm r14!, {r0-r13,pc}
7220
7221 which would need to be rewritten as:
7222
7223 ldm rN!, {r0-r14}
7224
7225 but that can't work, because there's no free register for N.
7226
7227 Solve this by turning off the writeback bit, and emulating
7228 writeback manually in the cleanup routine. */
7229
7230 if (writeback)
7231 insn &= ~(1 << 21);
7232
7233 new_regmask = (1 << num_in_list) - 1;
7234
7235 if (debug_displaced)
7236 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
7237 "{..., pc}: original reg list %.4x, modified "
7238 "list %.4x\n"), rn, writeback ? "!" : "",
7239 (int) insn & 0xffff, new_regmask);
7240
7241 dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
7242
7243 dsc->cleanup = &cleanup_block_load_pc;
7244 }
7245 }
7246 else
7247 {
7248 /* STM of a list of registers which includes PC. Run the instruction
7249 as-is, but out of line: this will store the wrong value for the PC,
7250 so we must manually fix up the memory in the cleanup routine.
7251 Doing things this way has the advantage that we can auto-detect
7252 the offset of the PC write (which is architecture-dependent) in
7253 the cleanup routine. */
7254 dsc->modinsn[0] = insn;
7255
7256 dsc->cleanup = &cleanup_block_store_pc;
7257 }
7258
7259 return 0;
7260}
7261
34518530
YQ
7262static int
7263thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
7264 struct regcache *regs,
7265 struct displaced_step_closure *dsc)
cca44b1b 7266{
34518530
YQ
7267 int rn = bits (insn1, 0, 3);
7268 int load = bit (insn1, 4);
7269 int writeback = bit (insn1, 5);
cca44b1b 7270
34518530
YQ
7271 /* Block transfers which don't mention PC can be run directly
7272 out-of-line. */
7273 if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
7274 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);
7ff120b4 7275
34518530
YQ
7276 if (rn == ARM_PC_REGNUM)
7277 {
7278 warning (_("displaced: Unpredictable LDM or STM with "
7279 "base register r15"));
7280 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7281 "unpredictable ldm/stm", dsc);
7282 }
cca44b1b
JB
7283
7284 if (debug_displaced)
34518530
YQ
7285 fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
7286 "%.4x%.4x\n", insn1, insn2);
cca44b1b 7287
34518530
YQ
7288 /* Clear bit 13, since it should be always zero. */
7289 dsc->u.block.regmask = (insn2 & 0xdfff);
7290 dsc->u.block.rn = rn;
cca44b1b 7291
34518530
YQ
7292 dsc->u.block.load = load;
7293 dsc->u.block.user = 0;
7294 dsc->u.block.increment = bit (insn1, 7);
7295 dsc->u.block.before = bit (insn1, 8);
7296 dsc->u.block.writeback = writeback;
7297 dsc->u.block.cond = INST_AL;
7298 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b 7299
34518530
YQ
7300 if (load)
7301 {
7302 if (dsc->u.block.regmask == 0xffff)
7303 {
7304 /* This branch is impossible to happen. */
7305 gdb_assert (0);
7306 }
7307 else
7308 {
7309 unsigned int regmask = dsc->u.block.regmask;
7310 unsigned int num_in_list = bitcount (regmask), new_regmask, bit = 1;
7311 unsigned int to = 0, from = 0, i, new_rn;
7312
7313 for (i = 0; i < num_in_list; i++)
7314 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7315
7316 if (writeback)
7317 insn1 &= ~(1 << 5);
7318
7319 new_regmask = (1 << num_in_list) - 1;
7320
7321 if (debug_displaced)
7322 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
7323 "{..., pc}: original reg list %.4x, modified "
7324 "list %.4x\n"), rn, writeback ? "!" : "",
7325 (int) dsc->u.block.regmask, new_regmask);
7326
7327 dsc->modinsn[0] = insn1;
7328 dsc->modinsn[1] = (new_regmask & 0xffff);
7329 dsc->numinsns = 2;
7330
7331 dsc->cleanup = &cleanup_block_load_pc;
7332 }
7333 }
7334 else
7335 {
7336 dsc->modinsn[0] = insn1;
7337 dsc->modinsn[1] = insn2;
7338 dsc->numinsns = 2;
7339 dsc->cleanup = &cleanup_block_store_pc;
7340 }
7341 return 0;
7342}
7343
7344/* Cleanup/copy SVC (SWI) instructions. These two functions are overridden
7345 for Linux, where some SVC instructions must be treated specially. */
7346
7347static void
7348cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
7349 struct displaced_step_closure *dsc)
7350{
7351 CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
7352
7353 if (debug_displaced)
7354 fprintf_unfiltered (gdb_stdlog, "displaced: cleanup for svc, resume at "
7355 "%.8lx\n", (unsigned long) resume_addr);
7356
7357 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
7358}
7359
7360
7361/* Common copy routine for svc instruciton. */
7362
7363static int
7364install_svc (struct gdbarch *gdbarch, struct regcache *regs,
7365 struct displaced_step_closure *dsc)
7366{
7367 /* Preparation: none.
7368 Insn: unmodified svc.
7369 Cleanup: pc <- insn_addr + insn_size. */
7370
7371 /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
7372 instruction. */
7373 dsc->wrote_to_pc = 1;
7374
7375 /* Allow OS-specific code to override SVC handling. */
bd18283a
YQ
7376 if (dsc->u.svc.copy_svc_os)
7377 return dsc->u.svc.copy_svc_os (gdbarch, regs, dsc);
7378 else
7379 {
7380 dsc->cleanup = &cleanup_svc;
7381 return 0;
7382 }
34518530
YQ
7383}
7384
7385static int
7386arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
7387 struct regcache *regs, struct displaced_step_closure *dsc)
7388{
7389
7390 if (debug_displaced)
7391 fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.8lx\n",
7392 (unsigned long) insn);
7393
7394 dsc->modinsn[0] = insn;
7395
7396 return install_svc (gdbarch, regs, dsc);
7397}
7398
7399static int
7400thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
7401 struct regcache *regs, struct displaced_step_closure *dsc)
7402{
7403
7404 if (debug_displaced)
7405 fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.4x\n",
7406 insn);
bd18283a 7407
34518530
YQ
7408 dsc->modinsn[0] = insn;
7409
7410 return install_svc (gdbarch, regs, dsc);
cca44b1b
JB
7411}
7412
7413/* Copy undefined instructions. */
7414
7415static int
7ff120b4
YQ
7416arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
7417 struct displaced_step_closure *dsc)
cca44b1b
JB
7418{
7419 if (debug_displaced)
0963b4bd
MS
7420 fprintf_unfiltered (gdb_stdlog,
7421 "displaced: copying undefined insn %.8lx\n",
cca44b1b
JB
7422 (unsigned long) insn);
7423
7424 dsc->modinsn[0] = insn;
7425
7426 return 0;
7427}
7428
34518530
YQ
7429static int
7430thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
7431 struct displaced_step_closure *dsc)
7432{
7433
7434 if (debug_displaced)
7435 fprintf_unfiltered (gdb_stdlog, "displaced: copying undefined insn "
7436 "%.4x %.4x\n", (unsigned short) insn1,
7437 (unsigned short) insn2);
7438
7439 dsc->modinsn[0] = insn1;
7440 dsc->modinsn[1] = insn2;
7441 dsc->numinsns = 2;
7442
7443 return 0;
7444}
7445
cca44b1b
JB
7446/* Copy unpredictable instructions. */
7447
7448static int
7ff120b4
YQ
7449arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
7450 struct displaced_step_closure *dsc)
cca44b1b
JB
7451{
7452 if (debug_displaced)
7453 fprintf_unfiltered (gdb_stdlog, "displaced: copying unpredictable insn "
7454 "%.8lx\n", (unsigned long) insn);
7455
7456 dsc->modinsn[0] = insn;
7457
7458 return 0;
7459}
7460
7461/* The decode_* functions are instruction decoding helpers. They mostly follow
7462 the presentation in the ARM ARM. */
7463
7464static int
7ff120b4
YQ
7465arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
7466 struct regcache *regs,
7467 struct displaced_step_closure *dsc)
cca44b1b
JB
7468{
7469 unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
7470 unsigned int rn = bits (insn, 16, 19);
7471
7472 if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0xe) == 0x0)
7ff120b4 7473 return arm_copy_unmodified (gdbarch, insn, "cps", dsc);
cca44b1b 7474 else if (op1 == 0x10 && op2 == 0x0 && (rn & 0xe) == 0x1)
7ff120b4 7475 return arm_copy_unmodified (gdbarch, insn, "setend", dsc);
cca44b1b 7476 else if ((op1 & 0x60) == 0x20)
7ff120b4 7477 return arm_copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
cca44b1b 7478 else if ((op1 & 0x71) == 0x40)
7ff120b4
YQ
7479 return arm_copy_unmodified (gdbarch, insn, "neon elt/struct load/store",
7480 dsc);
cca44b1b 7481 else if ((op1 & 0x77) == 0x41)
7ff120b4 7482 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 7483 else if ((op1 & 0x77) == 0x45)
7ff120b4 7484 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pli. */
cca44b1b
JB
7485 else if ((op1 & 0x77) == 0x51)
7486 {
7487 if (rn != 0xf)
7ff120b4 7488 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b 7489 else
7ff120b4 7490 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7491 }
7492 else if ((op1 & 0x77) == 0x55)
7ff120b4 7493 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b
JB
7494 else if (op1 == 0x57)
7495 switch (op2)
7496 {
7ff120b4
YQ
7497 case 0x1: return arm_copy_unmodified (gdbarch, insn, "clrex", dsc);
7498 case 0x4: return arm_copy_unmodified (gdbarch, insn, "dsb", dsc);
7499 case 0x5: return arm_copy_unmodified (gdbarch, insn, "dmb", dsc);
7500 case 0x6: return arm_copy_unmodified (gdbarch, insn, "isb", dsc);
7501 default: return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7502 }
7503 else if ((op1 & 0x63) == 0x43)
7ff120b4 7504 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7505 else if ((op2 & 0x1) == 0x0)
7506 switch (op1 & ~0x80)
7507 {
7508 case 0x61:
7ff120b4 7509 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 7510 case 0x65:
7ff120b4 7511 return arm_copy_preload_reg (gdbarch, insn, regs, dsc); /* pli reg. */
cca44b1b
JB
7512 case 0x71: case 0x75:
7513 /* pld/pldw reg. */
7ff120b4 7514 return arm_copy_preload_reg (gdbarch, insn, regs, dsc);
cca44b1b 7515 case 0x63: case 0x67: case 0x73: case 0x77:
7ff120b4 7516 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b 7517 default:
7ff120b4 7518 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7519 }
7520 else
7ff120b4 7521 return arm_copy_undef (gdbarch, insn, dsc); /* Probably unreachable. */
cca44b1b
JB
7522}
7523
7524static int
7ff120b4
YQ
7525arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
7526 struct regcache *regs,
7527 struct displaced_step_closure *dsc)
cca44b1b
JB
7528{
7529 if (bit (insn, 27) == 0)
7ff120b4 7530 return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
cca44b1b
JB
7531 /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx. */
7532 else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
7533 {
7534 case 0x0: case 0x2:
7ff120b4 7535 return arm_copy_unmodified (gdbarch, insn, "srs", dsc);
cca44b1b
JB
7536
7537 case 0x1: case 0x3:
7ff120b4 7538 return arm_copy_unmodified (gdbarch, insn, "rfe", dsc);
cca44b1b
JB
7539
7540 case 0x4: case 0x5: case 0x6: case 0x7:
7ff120b4 7541 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b
JB
7542
7543 case 0x8:
7544 switch ((insn & 0xe00000) >> 21)
7545 {
7546 case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
7547 /* stc/stc2. */
7ff120b4 7548 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7549
7550 case 0x2:
7ff120b4 7551 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b
JB
7552
7553 default:
7ff120b4 7554 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7555 }
7556
7557 case 0x9:
7558 {
7559 int rn_f = (bits (insn, 16, 19) == 0xf);
7560 switch ((insn & 0xe00000) >> 21)
7561 {
7562 case 0x1: case 0x3:
7563 /* ldc/ldc2 imm (undefined for rn == pc). */
7ff120b4
YQ
7564 return rn_f ? arm_copy_undef (gdbarch, insn, dsc)
7565 : arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7566
7567 case 0x2:
7ff120b4 7568 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
7569
7570 case 0x4: case 0x5: case 0x6: case 0x7:
7571 /* ldc/ldc2 lit (undefined for rn != pc). */
7ff120b4
YQ
7572 return rn_f ? arm_copy_copro_load_store (gdbarch, insn, regs, dsc)
7573 : arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7574
7575 default:
7ff120b4 7576 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7577 }
7578 }
7579
7580 case 0xa:
7ff120b4 7581 return arm_copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
cca44b1b
JB
7582
7583 case 0xb:
7584 if (bits (insn, 16, 19) == 0xf)
7585 /* ldc/ldc2 lit. */
7ff120b4 7586 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 7587 else
7ff120b4 7588 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7589
7590 case 0xc:
7591 if (bit (insn, 4))
7ff120b4 7592 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 7593 else
7ff120b4 7594 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7595
7596 case 0xd:
7597 if (bit (insn, 4))
7ff120b4 7598 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 7599 else
7ff120b4 7600 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7601
7602 default:
7ff120b4 7603 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7604 }
7605}
7606
7607/* Decode miscellaneous instructions in dp/misc encoding space. */
7608
7609static int
7ff120b4
YQ
7610arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
7611 struct regcache *regs,
7612 struct displaced_step_closure *dsc)
cca44b1b
JB
7613{
7614 unsigned int op2 = bits (insn, 4, 6);
7615 unsigned int op = bits (insn, 21, 22);
7616 unsigned int op1 = bits (insn, 16, 19);
7617
7618 switch (op2)
7619 {
7620 case 0x0:
7ff120b4 7621 return arm_copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
cca44b1b
JB
7622
7623 case 0x1:
7624 if (op == 0x1) /* bx. */
7ff120b4 7625 return arm_copy_bx_blx_reg (gdbarch, insn, regs, dsc);
cca44b1b 7626 else if (op == 0x3)
7ff120b4 7627 return arm_copy_unmodified (gdbarch, insn, "clz", dsc);
cca44b1b 7628 else
7ff120b4 7629 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7630
7631 case 0x2:
7632 if (op == 0x1)
7633 /* Not really supported. */
7ff120b4 7634 return arm_copy_unmodified (gdbarch, insn, "bxj", dsc);
cca44b1b 7635 else
7ff120b4 7636 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7637
7638 case 0x3:
7639 if (op == 0x1)
7ff120b4 7640 return arm_copy_bx_blx_reg (gdbarch, insn,
0963b4bd 7641 regs, dsc); /* blx register. */
cca44b1b 7642 else
7ff120b4 7643 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7644
7645 case 0x5:
7ff120b4 7646 return arm_copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
cca44b1b
JB
7647
7648 case 0x7:
7649 if (op == 0x1)
7ff120b4 7650 return arm_copy_unmodified (gdbarch, insn, "bkpt", dsc);
cca44b1b
JB
7651 else if (op == 0x3)
7652 /* Not really supported. */
7ff120b4 7653 return arm_copy_unmodified (gdbarch, insn, "smc", dsc);
cca44b1b
JB
7654
7655 default:
7ff120b4 7656 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7657 }
7658}
7659
7660static int
7ff120b4
YQ
7661arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
7662 struct regcache *regs,
7663 struct displaced_step_closure *dsc)
cca44b1b
JB
7664{
7665 if (bit (insn, 25))
7666 switch (bits (insn, 20, 24))
7667 {
7668 case 0x10:
7ff120b4 7669 return arm_copy_unmodified (gdbarch, insn, "movw", dsc);
cca44b1b
JB
7670
7671 case 0x14:
7ff120b4 7672 return arm_copy_unmodified (gdbarch, insn, "movt", dsc);
cca44b1b
JB
7673
7674 case 0x12: case 0x16:
7ff120b4 7675 return arm_copy_unmodified (gdbarch, insn, "msr imm", dsc);
cca44b1b
JB
7676
7677 default:
7ff120b4 7678 return arm_copy_alu_imm (gdbarch, insn, regs, dsc);
cca44b1b
JB
7679 }
7680 else
7681 {
7682 uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
7683
7684 if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
7ff120b4 7685 return arm_copy_alu_reg (gdbarch, insn, regs, dsc);
cca44b1b 7686 else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
7ff120b4 7687 return arm_copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
cca44b1b 7688 else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
7ff120b4 7689 return arm_decode_miscellaneous (gdbarch, insn, regs, dsc);
cca44b1b 7690 else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
7ff120b4 7691 return arm_copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
cca44b1b 7692 else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
7ff120b4 7693 return arm_copy_unmodified (gdbarch, insn, "mul/mla", dsc);
cca44b1b 7694 else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
7ff120b4 7695 return arm_copy_unmodified (gdbarch, insn, "synch", dsc);
cca44b1b
JB
7696 else if (op2 == 0xb || (op2 & 0xd) == 0xd)
7697 /* 2nd arg means "unpriveleged". */
7ff120b4
YQ
7698 return arm_copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
7699 dsc);
cca44b1b
JB
7700 }
7701
7702 /* Should be unreachable. */
7703 return 1;
7704}
7705
7706static int
7ff120b4
YQ
7707arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
7708 struct regcache *regs,
7709 struct displaced_step_closure *dsc)
cca44b1b
JB
7710{
7711 int a = bit (insn, 25), b = bit (insn, 4);
7712 uint32_t op1 = bits (insn, 20, 24);
7713 int rn_f = bits (insn, 16, 19) == 0xf;
7714
7715 if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
7716 || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
0f6f04ba 7717 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 0);
cca44b1b
JB
7718 else if ((!a && (op1 & 0x17) == 0x02)
7719 || (a && (op1 & 0x17) == 0x02 && !b))
0f6f04ba 7720 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 1);
cca44b1b
JB
7721 else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
7722 || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
0f6f04ba 7723 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 0);
cca44b1b
JB
7724 else if ((!a && (op1 & 0x17) == 0x03)
7725 || (a && (op1 & 0x17) == 0x03 && !b))
0f6f04ba 7726 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 1);
cca44b1b
JB
7727 else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
7728 || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
7ff120b4 7729 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
cca44b1b
JB
7730 else if ((!a && (op1 & 0x17) == 0x06)
7731 || (a && (op1 & 0x17) == 0x06 && !b))
7ff120b4 7732 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
cca44b1b
JB
7733 else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
7734 || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
7ff120b4 7735 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
cca44b1b
JB
7736 else if ((!a && (op1 & 0x17) == 0x07)
7737 || (a && (op1 & 0x17) == 0x07 && !b))
7ff120b4 7738 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
cca44b1b
JB
7739
7740 /* Should be unreachable. */
7741 return 1;
7742}
7743
7744static int
7ff120b4
YQ
7745arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
7746 struct displaced_step_closure *dsc)
cca44b1b
JB
7747{
7748 switch (bits (insn, 20, 24))
7749 {
7750 case 0x00: case 0x01: case 0x02: case 0x03:
7ff120b4 7751 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
cca44b1b
JB
7752
7753 case 0x04: case 0x05: case 0x06: case 0x07:
7ff120b4 7754 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
cca44b1b
JB
7755
7756 case 0x08: case 0x09: case 0x0a: case 0x0b:
7757 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
7ff120b4 7758 return arm_copy_unmodified (gdbarch, insn,
cca44b1b
JB
7759 "decode/pack/unpack/saturate/reverse", dsc);
7760
7761 case 0x18:
7762 if (bits (insn, 5, 7) == 0) /* op2. */
7763 {
7764 if (bits (insn, 12, 15) == 0xf)
7ff120b4 7765 return arm_copy_unmodified (gdbarch, insn, "usad8", dsc);
cca44b1b 7766 else
7ff120b4 7767 return arm_copy_unmodified (gdbarch, insn, "usada8", dsc);
cca44b1b
JB
7768 }
7769 else
7ff120b4 7770 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7771
7772 case 0x1a: case 0x1b:
7773 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 7774 return arm_copy_unmodified (gdbarch, insn, "sbfx", dsc);
cca44b1b 7775 else
7ff120b4 7776 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7777
7778 case 0x1c: case 0x1d:
7779 if (bits (insn, 5, 6) == 0x0) /* op2[1:0]. */
7780 {
7781 if (bits (insn, 0, 3) == 0xf)
7ff120b4 7782 return arm_copy_unmodified (gdbarch, insn, "bfc", dsc);
cca44b1b 7783 else
7ff120b4 7784 return arm_copy_unmodified (gdbarch, insn, "bfi", dsc);
cca44b1b
JB
7785 }
7786 else
7ff120b4 7787 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7788
7789 case 0x1e: case 0x1f:
7790 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 7791 return arm_copy_unmodified (gdbarch, insn, "ubfx", dsc);
cca44b1b 7792 else
7ff120b4 7793 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7794 }
7795
7796 /* Should be unreachable. */
7797 return 1;
7798}
7799
7800static int
7ff120b4
YQ
7801arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, int32_t insn,
7802 struct regcache *regs,
7803 struct displaced_step_closure *dsc)
cca44b1b
JB
7804{
7805 if (bit (insn, 25))
7ff120b4 7806 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b 7807 else
7ff120b4 7808 return arm_copy_block_xfer (gdbarch, insn, regs, dsc);
cca44b1b
JB
7809}
7810
7811static int
7ff120b4
YQ
7812arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
7813 struct regcache *regs,
7814 struct displaced_step_closure *dsc)
cca44b1b
JB
7815{
7816 unsigned int opcode = bits (insn, 20, 24);
7817
7818 switch (opcode)
7819 {
7820 case 0x04: case 0x05: /* VFP/Neon mrrc/mcrr. */
7ff120b4 7821 return arm_copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
cca44b1b
JB
7822
7823 case 0x08: case 0x0a: case 0x0c: case 0x0e:
7824 case 0x12: case 0x16:
7ff120b4 7825 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
cca44b1b
JB
7826
7827 case 0x09: case 0x0b: case 0x0d: case 0x0f:
7828 case 0x13: case 0x17:
7ff120b4 7829 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
cca44b1b
JB
7830
7831 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7832 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7833 /* Note: no writeback for these instructions. Bit 25 will always be
7834 zero though (via caller), so the following works OK. */
7ff120b4 7835 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7836 }
7837
7838 /* Should be unreachable. */
7839 return 1;
7840}
7841
34518530
YQ
7842/* Decode shifted register instructions. */
7843
7844static int
7845thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
7846 uint16_t insn2, struct regcache *regs,
7847 struct displaced_step_closure *dsc)
7848{
7849 /* PC is only allowed to be used in instruction MOV. */
7850
7851 unsigned int op = bits (insn1, 5, 8);
7852 unsigned int rn = bits (insn1, 0, 3);
7853
7854 if (op == 0x2 && rn == 0xf) /* MOV */
7855 return thumb2_copy_alu_imm (gdbarch, insn1, insn2, regs, dsc);
7856 else
7857 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7858 "dp (shift reg)", dsc);
7859}
7860
7861
7862/* Decode extension register load/store. Exactly the same as
7863 arm_decode_ext_reg_ld_st. */
7864
7865static int
7866thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
7867 uint16_t insn2, struct regcache *regs,
7868 struct displaced_step_closure *dsc)
7869{
7870 unsigned int opcode = bits (insn1, 4, 8);
7871
7872 switch (opcode)
7873 {
7874 case 0x04: case 0x05:
7875 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7876 "vfp/neon vmov", dsc);
7877
7878 case 0x08: case 0x0c: /* 01x00 */
7879 case 0x0a: case 0x0e: /* 01x10 */
7880 case 0x12: case 0x16: /* 10x10 */
7881 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7882 "vfp/neon vstm/vpush", dsc);
7883
7884 case 0x09: case 0x0d: /* 01x01 */
7885 case 0x0b: case 0x0f: /* 01x11 */
7886 case 0x13: case 0x17: /* 10x11 */
7887 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7888 "vfp/neon vldm/vpop", dsc);
7889
7890 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7891 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7892 "vstr", dsc);
7893 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7894 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2, regs, dsc);
7895 }
7896
7897 /* Should be unreachable. */
7898 return 1;
7899}
7900
cca44b1b 7901static int
7ff120b4
YQ
7902arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn, CORE_ADDR to,
7903 struct regcache *regs, struct displaced_step_closure *dsc)
cca44b1b
JB
7904{
7905 unsigned int op1 = bits (insn, 20, 25);
7906 int op = bit (insn, 4);
7907 unsigned int coproc = bits (insn, 8, 11);
7908 unsigned int rn = bits (insn, 16, 19);
7909
7910 if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
7ff120b4 7911 return arm_decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
cca44b1b
JB
7912 else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
7913 && (coproc & 0xe) != 0xa)
7914 /* stc/stc2. */
7ff120b4 7915 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7916 else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
7917 && (coproc & 0xe) != 0xa)
7918 /* ldc/ldc2 imm/lit. */
7ff120b4 7919 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 7920 else if ((op1 & 0x3e) == 0x00)
7ff120b4 7921 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b 7922 else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
7ff120b4 7923 return arm_copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
cca44b1b 7924 else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
7ff120b4 7925 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b 7926 else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
7ff120b4 7927 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
7928 else if ((op1 & 0x30) == 0x20 && !op)
7929 {
7930 if ((coproc & 0xe) == 0xa)
7ff120b4 7931 return arm_copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
cca44b1b 7932 else
7ff120b4 7933 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7934 }
7935 else if ((op1 & 0x30) == 0x20 && op)
7ff120b4 7936 return arm_copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
cca44b1b 7937 else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
7ff120b4 7938 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 7939 else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
7ff120b4 7940 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 7941 else if ((op1 & 0x30) == 0x30)
7ff120b4 7942 return arm_copy_svc (gdbarch, insn, regs, dsc);
cca44b1b 7943 else
7ff120b4 7944 return arm_copy_undef (gdbarch, insn, dsc); /* Possibly unreachable. */
cca44b1b
JB
7945}
7946
34518530
YQ
7947static int
7948thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
7949 uint16_t insn2, struct regcache *regs,
7950 struct displaced_step_closure *dsc)
7951{
7952 unsigned int coproc = bits (insn2, 8, 11);
7953 unsigned int op1 = bits (insn1, 4, 9);
7954 unsigned int bit_5_8 = bits (insn1, 5, 8);
7955 unsigned int bit_9 = bit (insn1, 9);
7956 unsigned int bit_4 = bit (insn1, 4);
7957 unsigned int rn = bits (insn1, 0, 3);
7958
7959 if (bit_9 == 0)
7960 {
7961 if (bit_5_8 == 2)
7962 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7963 "neon 64bit xfer/mrrc/mrrc2/mcrr/mcrr2",
7964 dsc);
7965 else if (bit_5_8 == 0) /* UNDEFINED. */
7966 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
7967 else
7968 {
7969 /*coproc is 101x. SIMD/VFP, ext registers load/store. */
7970 if ((coproc & 0xe) == 0xa)
7971 return thumb2_decode_ext_reg_ld_st (gdbarch, insn1, insn2, regs,
7972 dsc);
7973 else /* coproc is not 101x. */
7974 {
7975 if (bit_4 == 0) /* STC/STC2. */
7976 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7977 "stc/stc2", dsc);
7978 else /* LDC/LDC2 {literal, immeidate}. */
7979 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2,
7980 regs, dsc);
7981 }
7982 }
7983 }
7984 else
7985 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "coproc", dsc);
7986
7987 return 0;
7988}
7989
7990static void
7991install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
7992 struct displaced_step_closure *dsc, int rd)
7993{
7994 /* ADR Rd, #imm
7995
7996 Rewrite as:
7997
7998 Preparation: Rd <- PC
7999 Insn: ADD Rd, #imm
8000 Cleanup: Null.
8001 */
8002
8003 /* Rd <- PC */
8004 int val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
8005 displaced_write_reg (regs, dsc, rd, val, CANNOT_WRITE_PC);
8006}
8007
8008static int
8009thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
8010 struct displaced_step_closure *dsc,
8011 int rd, unsigned int imm)
8012{
8013
8014 /* Encoding T2: ADDS Rd, #imm */
8015 dsc->modinsn[0] = (0x3000 | (rd << 8) | imm);
8016
8017 install_pc_relative (gdbarch, regs, dsc, rd);
8018
8019 return 0;
8020}
8021
8022static int
8023thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
8024 struct regcache *regs,
8025 struct displaced_step_closure *dsc)
8026{
8027 unsigned int rd = bits (insn, 8, 10);
8028 unsigned int imm8 = bits (insn, 0, 7);
8029
8030 if (debug_displaced)
8031 fprintf_unfiltered (gdb_stdlog,
8032 "displaced: copying thumb adr r%d, #%d insn %.4x\n",
8033 rd, imm8, insn);
8034
8035 return thumb_copy_pc_relative_16bit (gdbarch, regs, dsc, rd, imm8);
8036}
8037
8038static int
8039thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
8040 uint16_t insn2, struct regcache *regs,
8041 struct displaced_step_closure *dsc)
8042{
8043 unsigned int rd = bits (insn2, 8, 11);
8044 /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
8045 extract raw immediate encoding rather than computing immediate. When
8046 generating ADD or SUB instruction, we can simply perform OR operation to
8047 set immediate into ADD. */
8048 unsigned int imm_3_8 = insn2 & 0x70ff;
8049 unsigned int imm_i = insn1 & 0x0400; /* Clear all bits except bit 10. */
8050
8051 if (debug_displaced)
8052 fprintf_unfiltered (gdb_stdlog,
8053 "displaced: copying thumb adr r%d, #%d:%d insn %.4x%.4x\n",
8054 rd, imm_i, imm_3_8, insn1, insn2);
8055
8056 if (bit (insn1, 7)) /* Encoding T2 */
8057 {
8058 /* Encoding T3: SUB Rd, Rd, #imm */
8059 dsc->modinsn[0] = (0xf1a0 | rd | imm_i);
8060 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
8061 }
8062 else /* Encoding T3 */
8063 {
8064 /* Encoding T3: ADD Rd, Rd, #imm */
8065 dsc->modinsn[0] = (0xf100 | rd | imm_i);
8066 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
8067 }
8068 dsc->numinsns = 2;
8069
8070 install_pc_relative (gdbarch, regs, dsc, rd);
8071
8072 return 0;
8073}
8074
8075static int
8076thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, unsigned short insn1,
8077 struct regcache *regs,
8078 struct displaced_step_closure *dsc)
8079{
8080 unsigned int rt = bits (insn1, 8, 10);
8081 unsigned int pc;
8082 int imm8 = (bits (insn1, 0, 7) << 2);
8083 CORE_ADDR from = dsc->insn_addr;
8084
8085 /* LDR Rd, #imm8
8086
8087 Rwrite as:
8088
8089 Preparation: tmp0 <- R0, tmp2 <- R2, tmp3 <- R3, R2 <- PC, R3 <- #imm8;
8090
8091 Insn: LDR R0, [R2, R3];
8092 Cleanup: R2 <- tmp2, R3 <- tmp3, Rd <- R0, R0 <- tmp0 */
8093
8094 if (debug_displaced)
8095 fprintf_unfiltered (gdb_stdlog,
8096 "displaced: copying thumb ldr r%d [pc #%d]\n"
8097 , rt, imm8);
8098
8099 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
8100 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
8101 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
8102 pc = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
8103 /* The assembler calculates the required value of the offset from the
8104 Align(PC,4) value of this instruction to the label. */
8105 pc = pc & 0xfffffffc;
8106
8107 displaced_write_reg (regs, dsc, 2, pc, CANNOT_WRITE_PC);
8108 displaced_write_reg (regs, dsc, 3, imm8, CANNOT_WRITE_PC);
8109
8110 dsc->rd = rt;
8111 dsc->u.ldst.xfersize = 4;
8112 dsc->u.ldst.rn = 0;
8113 dsc->u.ldst.immed = 0;
8114 dsc->u.ldst.writeback = 0;
8115 dsc->u.ldst.restore_r4 = 0;
8116
8117 dsc->modinsn[0] = 0x58d0; /* ldr r0, [r2, r3]*/
8118
8119 dsc->cleanup = &cleanup_load;
8120
8121 return 0;
8122}
8123
8124/* Copy Thumb cbnz/cbz insruction. */
8125
8126static int
8127thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
8128 struct regcache *regs,
8129 struct displaced_step_closure *dsc)
8130{
8131 int non_zero = bit (insn1, 11);
8132 unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
8133 CORE_ADDR from = dsc->insn_addr;
8134 int rn = bits (insn1, 0, 2);
8135 int rn_val = displaced_read_reg (regs, dsc, rn);
8136
8137 dsc->u.branch.cond = (rn_val && non_zero) || (!rn_val && !non_zero);
8138 /* CBNZ and CBZ do not affect the condition flags. If condition is true,
8139 set it INST_AL, so cleanup_branch will know branch is taken, otherwise,
8140 condition is false, let it be, cleanup_branch will do nothing. */
8141 if (dsc->u.branch.cond)
8142 {
8143 dsc->u.branch.cond = INST_AL;
8144 dsc->u.branch.dest = from + 4 + imm5;
8145 }
8146 else
8147 dsc->u.branch.dest = from + 2;
8148
8149 dsc->u.branch.link = 0;
8150 dsc->u.branch.exchange = 0;
8151
8152 if (debug_displaced)
8153 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s [r%d = 0x%x]"
8154 " insn %.4x to %.8lx\n", non_zero ? "cbnz" : "cbz",
8155 rn, rn_val, insn1, dsc->u.branch.dest);
8156
8157 dsc->modinsn[0] = THUMB_NOP;
8158
8159 dsc->cleanup = &cleanup_branch;
8160 return 0;
8161}
8162
8163/* Copy Table Branch Byte/Halfword */
8164static int
8165thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
8166 uint16_t insn2, struct regcache *regs,
8167 struct displaced_step_closure *dsc)
8168{
8169 ULONGEST rn_val, rm_val;
8170 int is_tbh = bit (insn2, 4);
8171 CORE_ADDR halfwords = 0;
8172 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8173
8174 rn_val = displaced_read_reg (regs, dsc, bits (insn1, 0, 3));
8175 rm_val = displaced_read_reg (regs, dsc, bits (insn2, 0, 3));
8176
8177 if (is_tbh)
8178 {
8179 gdb_byte buf[2];
8180
8181 target_read_memory (rn_val + 2 * rm_val, buf, 2);
8182 halfwords = extract_unsigned_integer (buf, 2, byte_order);
8183 }
8184 else
8185 {
8186 gdb_byte buf[1];
8187
8188 target_read_memory (rn_val + rm_val, buf, 1);
8189 halfwords = extract_unsigned_integer (buf, 1, byte_order);
8190 }
8191
8192 if (debug_displaced)
8193 fprintf_unfiltered (gdb_stdlog, "displaced: %s base 0x%x offset 0x%x"
8194 " offset 0x%x\n", is_tbh ? "tbh" : "tbb",
8195 (unsigned int) rn_val, (unsigned int) rm_val,
8196 (unsigned int) halfwords);
8197
8198 dsc->u.branch.cond = INST_AL;
8199 dsc->u.branch.link = 0;
8200 dsc->u.branch.exchange = 0;
8201 dsc->u.branch.dest = dsc->insn_addr + 4 + 2 * halfwords;
8202
8203 dsc->cleanup = &cleanup_branch;
8204
8205 return 0;
8206}
8207
8208static void
8209cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
8210 struct displaced_step_closure *dsc)
8211{
8212 /* PC <- r7 */
8213 int val = displaced_read_reg (regs, dsc, 7);
8214 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, val, BX_WRITE_PC);
8215
8216 /* r7 <- r8 */
8217 val = displaced_read_reg (regs, dsc, 8);
8218 displaced_write_reg (regs, dsc, 7, val, CANNOT_WRITE_PC);
8219
8220 /* r8 <- tmp[0] */
8221 displaced_write_reg (regs, dsc, 8, dsc->tmp[0], CANNOT_WRITE_PC);
8222
8223}
8224
8225static int
8226thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, unsigned short insn1,
8227 struct regcache *regs,
8228 struct displaced_step_closure *dsc)
8229{
8230 dsc->u.block.regmask = insn1 & 0x00ff;
8231
8232 /* Rewrite instruction: POP {rX, rY, ...,rZ, PC}
8233 to :
8234
8235 (1) register list is full, that is, r0-r7 are used.
8236 Prepare: tmp[0] <- r8
8237
8238 POP {r0, r1, ...., r6, r7}; remove PC from reglist
8239 MOV r8, r7; Move value of r7 to r8;
8240 POP {r7}; Store PC value into r7.
8241
8242 Cleanup: PC <- r7, r7 <- r8, r8 <-tmp[0]
8243
8244 (2) register list is not full, supposing there are N registers in
8245 register list (except PC, 0 <= N <= 7).
8246 Prepare: for each i, 0 - N, tmp[i] <- ri.
8247
8248 POP {r0, r1, ...., rN};
8249
8250 Cleanup: Set registers in original reglist from r0 - rN. Restore r0 - rN
8251 from tmp[] properly.
8252 */
8253 if (debug_displaced)
8254 fprintf_unfiltered (gdb_stdlog,
8255 "displaced: copying thumb pop {%.8x, pc} insn %.4x\n",
8256 dsc->u.block.regmask, insn1);
8257
8258 if (dsc->u.block.regmask == 0xff)
8259 {
8260 dsc->tmp[0] = displaced_read_reg (regs, dsc, 8);
8261
8262 dsc->modinsn[0] = (insn1 & 0xfeff); /* POP {r0,r1,...,r6, r7} */
8263 dsc->modinsn[1] = 0x46b8; /* MOV r8, r7 */
8264 dsc->modinsn[2] = 0xbc80; /* POP {r7} */
8265
8266 dsc->numinsns = 3;
8267 dsc->cleanup = &cleanup_pop_pc_16bit_all;
8268 }
8269 else
8270 {
8271 unsigned int num_in_list = bitcount (dsc->u.block.regmask);
8272 unsigned int new_regmask, bit = 1;
8273 unsigned int to = 0, from = 0, i, new_rn;
8274
8275 for (i = 0; i < num_in_list + 1; i++)
8276 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
8277
8278 new_regmask = (1 << (num_in_list + 1)) - 1;
8279
8280 if (debug_displaced)
8281 fprintf_unfiltered (gdb_stdlog, _("displaced: POP "
8282 "{..., pc}: original reg list %.4x,"
8283 " modified list %.4x\n"),
8284 (int) dsc->u.block.regmask, new_regmask);
8285
8286 dsc->u.block.regmask |= 0x8000;
8287 dsc->u.block.writeback = 0;
8288 dsc->u.block.cond = INST_AL;
8289
8290 dsc->modinsn[0] = (insn1 & ~0x1ff) | (new_regmask & 0xff);
8291
8292 dsc->cleanup = &cleanup_block_load_pc;
8293 }
8294
8295 return 0;
8296}
8297
8298static void
8299thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8300 struct regcache *regs,
8301 struct displaced_step_closure *dsc)
8302{
8303 unsigned short op_bit_12_15 = bits (insn1, 12, 15);
8304 unsigned short op_bit_10_11 = bits (insn1, 10, 11);
8305 int err = 0;
8306
8307 /* 16-bit thumb instructions. */
8308 switch (op_bit_12_15)
8309 {
8310 /* Shift (imme), add, subtract, move and compare. */
8311 case 0: case 1: case 2: case 3:
8312 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8313 "shift/add/sub/mov/cmp",
8314 dsc);
8315 break;
8316 case 4:
8317 switch (op_bit_10_11)
8318 {
8319 case 0: /* Data-processing */
8320 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8321 "data-processing",
8322 dsc);
8323 break;
8324 case 1: /* Special data instructions and branch and exchange. */
8325 {
8326 unsigned short op = bits (insn1, 7, 9);
8327 if (op == 6 || op == 7) /* BX or BLX */
8328 err = thumb_copy_bx_blx_reg (gdbarch, insn1, regs, dsc);
8329 else if (bits (insn1, 6, 7) != 0) /* ADD/MOV/CMP high registers. */
8330 err = thumb_copy_alu_reg (gdbarch, insn1, regs, dsc);
8331 else
8332 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "special data",
8333 dsc);
8334 }
8335 break;
8336 default: /* LDR (literal) */
8337 err = thumb_copy_16bit_ldr_literal (gdbarch, insn1, regs, dsc);
8338 }
8339 break;
8340 case 5: case 6: case 7: case 8: case 9: /* Load/Store single data item */
8341 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldr/str", dsc);
8342 break;
8343 case 10:
8344 if (op_bit_10_11 < 2) /* Generate PC-relative address */
8345 err = thumb_decode_pc_relative_16bit (gdbarch, insn1, regs, dsc);
8346 else /* Generate SP-relative address */
8347 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "sp-relative", dsc);
8348 break;
8349 case 11: /* Misc 16-bit instructions */
8350 {
8351 switch (bits (insn1, 8, 11))
8352 {
8353 case 1: case 3: case 9: case 11: /* CBNZ, CBZ */
8354 err = thumb_copy_cbnz_cbz (gdbarch, insn1, regs, dsc);
8355 break;
8356 case 12: case 13: /* POP */
8357 if (bit (insn1, 8)) /* PC is in register list. */
8358 err = thumb_copy_pop_pc_16bit (gdbarch, insn1, regs, dsc);
8359 else
8360 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "pop", dsc);
8361 break;
8362 case 15: /* If-Then, and hints */
8363 if (bits (insn1, 0, 3))
8364 /* If-Then makes up to four following instructions conditional.
8365 IT instruction itself is not conditional, so handle it as a
8366 common unmodified instruction. */
8367 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "If-Then",
8368 dsc);
8369 else
8370 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "hints", dsc);
8371 break;
8372 default:
8373 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "misc", dsc);
8374 }
8375 }
8376 break;
8377 case 12:
8378 if (op_bit_10_11 < 2) /* Store multiple registers */
8379 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "stm", dsc);
8380 else /* Load multiple registers */
8381 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldm", dsc);
8382 break;
8383 case 13: /* Conditional branch and supervisor call */
8384 if (bits (insn1, 9, 11) != 7) /* conditional branch */
8385 err = thumb_copy_b (gdbarch, insn1, dsc);
8386 else
8387 err = thumb_copy_svc (gdbarch, insn1, regs, dsc);
8388 break;
8389 case 14: /* Unconditional branch */
8390 err = thumb_copy_b (gdbarch, insn1, dsc);
8391 break;
8392 default:
8393 err = 1;
8394 }
8395
8396 if (err)
8397 internal_error (__FILE__, __LINE__,
8398 _("thumb_process_displaced_16bit_insn: Instruction decode error"));
8399}
8400
8401static int
8402decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
8403 uint16_t insn1, uint16_t insn2,
8404 struct regcache *regs,
8405 struct displaced_step_closure *dsc)
8406{
8407 int rt = bits (insn2, 12, 15);
8408 int rn = bits (insn1, 0, 3);
8409 int op1 = bits (insn1, 7, 8);
8410 int err = 0;
8411
8412 switch (bits (insn1, 5, 6))
8413 {
8414 case 0: /* Load byte and memory hints */
8415 if (rt == 0xf) /* PLD/PLI */
8416 {
8417 if (rn == 0xf)
8418 /* PLD literal or Encoding T3 of PLI(immediate, literal). */
8419 return thumb2_copy_preload (gdbarch, insn1, insn2, regs, dsc);
8420 else
8421 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8422 "pli/pld", dsc);
8423 }
8424 else
8425 {
8426 if (rn == 0xf) /* LDRB/LDRSB (literal) */
8427 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8428 1);
8429 else
8430 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8431 "ldrb{reg, immediate}/ldrbt",
8432 dsc);
8433 }
8434
8435 break;
8436 case 1: /* Load halfword and memory hints. */
8437 if (rt == 0xf) /* PLD{W} and Unalloc memory hint. */
8438 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8439 "pld/unalloc memhint", dsc);
8440 else
8441 {
8442 if (rn == 0xf)
8443 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8444 2);
8445 else
8446 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8447 "ldrh/ldrht", dsc);
8448 }
8449 break;
8450 case 2: /* Load word */
8451 {
8452 int insn2_bit_8_11 = bits (insn2, 8, 11);
8453
8454 if (rn == 0xf)
8455 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc, 4);
8456 else if (op1 == 0x1) /* Encoding T3 */
8457 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs, dsc,
8458 0, 1);
8459 else /* op1 == 0x0 */
8460 {
8461 if (insn2_bit_8_11 == 0xc || (insn2_bit_8_11 & 0x9) == 0x9)
8462 /* LDR (immediate) */
8463 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8464 dsc, bit (insn2, 8), 1);
8465 else if (insn2_bit_8_11 == 0xe) /* LDRT */
8466 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8467 "ldrt", dsc);
8468 else
8469 /* LDR (register) */
8470 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8471 dsc, 0, 0);
8472 }
8473 break;
8474 }
8475 default:
8476 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
8477 break;
8478 }
8479 return 0;
8480}
8481
8482static void
8483thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8484 uint16_t insn2, struct regcache *regs,
8485 struct displaced_step_closure *dsc)
8486{
8487 int err = 0;
8488 unsigned short op = bit (insn2, 15);
8489 unsigned int op1 = bits (insn1, 11, 12);
8490
8491 switch (op1)
8492 {
8493 case 1:
8494 {
8495 switch (bits (insn1, 9, 10))
8496 {
8497 case 0:
8498 if (bit (insn1, 6))
8499 {
8500 /* Load/store {dual, execlusive}, table branch. */
8501 if (bits (insn1, 7, 8) == 1 && bits (insn1, 4, 5) == 1
8502 && bits (insn2, 5, 7) == 0)
8503 err = thumb2_copy_table_branch (gdbarch, insn1, insn2, regs,
8504 dsc);
8505 else
8506 /* PC is not allowed to use in load/store {dual, exclusive}
8507 instructions. */
8508 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8509 "load/store dual/ex", dsc);
8510 }
8511 else /* load/store multiple */
8512 {
8513 switch (bits (insn1, 7, 8))
8514 {
8515 case 0: case 3: /* SRS, RFE */
8516 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8517 "srs/rfe", dsc);
8518 break;
8519 case 1: case 2: /* LDM/STM/PUSH/POP */
8520 err = thumb2_copy_block_xfer (gdbarch, insn1, insn2, regs, dsc);
8521 break;
8522 }
8523 }
8524 break;
8525
8526 case 1:
8527 /* Data-processing (shift register). */
8528 err = thumb2_decode_dp_shift_reg (gdbarch, insn1, insn2, regs,
8529 dsc);
8530 break;
8531 default: /* Coprocessor instructions. */
8532 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8533 break;
8534 }
8535 break;
8536 }
8537 case 2: /* op1 = 2 */
8538 if (op) /* Branch and misc control. */
8539 {
8540 if (bit (insn2, 14) /* BLX/BL */
8541 || bit (insn2, 12) /* Unconditional branch */
8542 || (bits (insn1, 7, 9) != 0x7)) /* Conditional branch */
8543 err = thumb2_copy_b_bl_blx (gdbarch, insn1, insn2, regs, dsc);
8544 else
8545 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8546 "misc ctrl", dsc);
8547 }
8548 else
8549 {
8550 if (bit (insn1, 9)) /* Data processing (plain binary imm). */
8551 {
8552 int op = bits (insn1, 4, 8);
8553 int rn = bits (insn1, 0, 3);
8554 if ((op == 0 || op == 0xa) && rn == 0xf)
8555 err = thumb_copy_pc_relative_32bit (gdbarch, insn1, insn2,
8556 regs, dsc);
8557 else
8558 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8559 "dp/pb", dsc);
8560 }
8561 else /* Data processing (modified immeidate) */
8562 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8563 "dp/mi", dsc);
8564 }
8565 break;
8566 case 3: /* op1 = 3 */
8567 switch (bits (insn1, 9, 10))
8568 {
8569 case 0:
8570 if (bit (insn1, 4))
8571 err = decode_thumb_32bit_ld_mem_hints (gdbarch, insn1, insn2,
8572 regs, dsc);
8573 else /* NEON Load/Store and Store single data item */
8574 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8575 "neon elt/struct load/store",
8576 dsc);
8577 break;
8578 case 1: /* op1 = 3, bits (9, 10) == 1 */
8579 switch (bits (insn1, 7, 8))
8580 {
8581 case 0: case 1: /* Data processing (register) */
8582 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8583 "dp(reg)", dsc);
8584 break;
8585 case 2: /* Multiply and absolute difference */
8586 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8587 "mul/mua/diff", dsc);
8588 break;
8589 case 3: /* Long multiply and divide */
8590 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8591 "lmul/lmua", dsc);
8592 break;
8593 }
8594 break;
8595 default: /* Coprocessor instructions */
8596 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8597 break;
8598 }
8599 break;
8600 default:
8601 err = 1;
8602 }
8603
8604 if (err)
8605 internal_error (__FILE__, __LINE__,
8606 _("thumb_process_displaced_32bit_insn: Instruction decode error"));
8607
8608}
8609
b434a28f
YQ
8610static void
8611thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8612 CORE_ADDR to, struct regcache *regs,
8613 struct displaced_step_closure *dsc)
8614{
34518530
YQ
8615 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8616 uint16_t insn1
8617 = read_memory_unsigned_integer (from, 2, byte_order_for_code);
8618
8619 if (debug_displaced)
8620 fprintf_unfiltered (gdb_stdlog, "displaced: process thumb insn %.4x "
8621 "at %.8lx\n", insn1, (unsigned long) from);
8622
8623 dsc->is_thumb = 1;
8624 dsc->insn_size = thumb_insn_size (insn1);
8625 if (thumb_insn_size (insn1) == 4)
8626 {
8627 uint16_t insn2
8628 = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
8629 thumb_process_displaced_32bit_insn (gdbarch, insn1, insn2, regs, dsc);
8630 }
8631 else
8632 thumb_process_displaced_16bit_insn (gdbarch, insn1, regs, dsc);
b434a28f
YQ
8633}
8634
cca44b1b 8635void
b434a28f
YQ
8636arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8637 CORE_ADDR to, struct regcache *regs,
cca44b1b
JB
8638 struct displaced_step_closure *dsc)
8639{
8640 int err = 0;
b434a28f
YQ
8641 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8642 uint32_t insn;
cca44b1b
JB
8643
8644 /* Most displaced instructions use a 1-instruction scratch space, so set this
8645 here and override below if/when necessary. */
8646 dsc->numinsns = 1;
8647 dsc->insn_addr = from;
8648 dsc->scratch_base = to;
8649 dsc->cleanup = NULL;
8650 dsc->wrote_to_pc = 0;
8651
b434a28f
YQ
8652 if (!displaced_in_arm_mode (regs))
8653 return thumb_process_displaced_insn (gdbarch, from, to, regs, dsc);
8654
4db71c0b
YQ
8655 dsc->is_thumb = 0;
8656 dsc->insn_size = 4;
b434a28f
YQ
8657 insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
8658 if (debug_displaced)
8659 fprintf_unfiltered (gdb_stdlog, "displaced: stepping insn %.8lx "
8660 "at %.8lx\n", (unsigned long) insn,
8661 (unsigned long) from);
8662
cca44b1b 8663 if ((insn & 0xf0000000) == 0xf0000000)
7ff120b4 8664 err = arm_decode_unconditional (gdbarch, insn, regs, dsc);
cca44b1b
JB
8665 else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
8666 {
8667 case 0x0: case 0x1: case 0x2: case 0x3:
7ff120b4 8668 err = arm_decode_dp_misc (gdbarch, insn, regs, dsc);
cca44b1b
JB
8669 break;
8670
8671 case 0x4: case 0x5: case 0x6:
7ff120b4 8672 err = arm_decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
cca44b1b
JB
8673 break;
8674
8675 case 0x7:
7ff120b4 8676 err = arm_decode_media (gdbarch, insn, dsc);
cca44b1b
JB
8677 break;
8678
8679 case 0x8: case 0x9: case 0xa: case 0xb:
7ff120b4 8680 err = arm_decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
cca44b1b
JB
8681 break;
8682
8683 case 0xc: case 0xd: case 0xe: case 0xf:
7ff120b4 8684 err = arm_decode_svc_copro (gdbarch, insn, to, regs, dsc);
cca44b1b
JB
8685 break;
8686 }
8687
8688 if (err)
8689 internal_error (__FILE__, __LINE__,
8690 _("arm_process_displaced_insn: Instruction decode error"));
8691}
8692
8693/* Actually set up the scratch space for a displaced instruction. */
8694
8695void
8696arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
8697 CORE_ADDR to, struct displaced_step_closure *dsc)
8698{
8699 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4db71c0b 8700 unsigned int i, len, offset;
cca44b1b 8701 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4db71c0b 8702 int size = dsc->is_thumb? 2 : 4;
948f8e3d 8703 const gdb_byte *bkp_insn;
cca44b1b 8704
4db71c0b 8705 offset = 0;
cca44b1b
JB
8706 /* Poke modified instruction(s). */
8707 for (i = 0; i < dsc->numinsns; i++)
8708 {
8709 if (debug_displaced)
4db71c0b
YQ
8710 {
8711 fprintf_unfiltered (gdb_stdlog, "displaced: writing insn ");
8712 if (size == 4)
8713 fprintf_unfiltered (gdb_stdlog, "%.8lx",
8714 dsc->modinsn[i]);
8715 else if (size == 2)
8716 fprintf_unfiltered (gdb_stdlog, "%.4x",
8717 (unsigned short)dsc->modinsn[i]);
8718
8719 fprintf_unfiltered (gdb_stdlog, " at %.8lx\n",
8720 (unsigned long) to + offset);
8721
8722 }
8723 write_memory_unsigned_integer (to + offset, size,
8724 byte_order_for_code,
cca44b1b 8725 dsc->modinsn[i]);
4db71c0b
YQ
8726 offset += size;
8727 }
8728
8729 /* Choose the correct breakpoint instruction. */
8730 if (dsc->is_thumb)
8731 {
8732 bkp_insn = tdep->thumb_breakpoint;
8733 len = tdep->thumb_breakpoint_size;
8734 }
8735 else
8736 {
8737 bkp_insn = tdep->arm_breakpoint;
8738 len = tdep->arm_breakpoint_size;
cca44b1b
JB
8739 }
8740
8741 /* Put breakpoint afterwards. */
4db71c0b 8742 write_memory (to + offset, bkp_insn, len);
cca44b1b
JB
8743
8744 if (debug_displaced)
8745 fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
8746 paddress (gdbarch, from), paddress (gdbarch, to));
8747}
8748
8749/* Entry point for copying an instruction into scratch space for displaced
8750 stepping. */
8751
8752struct displaced_step_closure *
8753arm_displaced_step_copy_insn (struct gdbarch *gdbarch,
8754 CORE_ADDR from, CORE_ADDR to,
8755 struct regcache *regs)
8756{
8d749320
SM
8757 struct displaced_step_closure *dsc = XNEW (struct displaced_step_closure);
8758
b434a28f 8759 arm_process_displaced_insn (gdbarch, from, to, regs, dsc);
cca44b1b
JB
8760 arm_displaced_init_closure (gdbarch, from, to, dsc);
8761
8762 return dsc;
8763}
8764
8765/* Entry point for cleaning things up after a displaced instruction has been
8766 single-stepped. */
8767
8768void
8769arm_displaced_step_fixup (struct gdbarch *gdbarch,
8770 struct displaced_step_closure *dsc,
8771 CORE_ADDR from, CORE_ADDR to,
8772 struct regcache *regs)
8773{
8774 if (dsc->cleanup)
8775 dsc->cleanup (gdbarch, regs, dsc);
8776
8777 if (!dsc->wrote_to_pc)
4db71c0b
YQ
8778 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
8779 dsc->insn_addr + dsc->insn_size);
8780
cca44b1b
JB
8781}
8782
8783#include "bfd-in2.h"
8784#include "libcoff.h"
8785
8786static int
8787gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
8788{
9a3c8263 8789 struct gdbarch *gdbarch = (struct gdbarch *) info->application_data;
9779414d
DJ
8790
8791 if (arm_pc_is_thumb (gdbarch, memaddr))
cca44b1b
JB
8792 {
8793 static asymbol *asym;
8794 static combined_entry_type ce;
8795 static struct coff_symbol_struct csym;
8796 static struct bfd fake_bfd;
8797 static bfd_target fake_target;
8798
8799 if (csym.native == NULL)
8800 {
8801 /* Create a fake symbol vector containing a Thumb symbol.
8802 This is solely so that the code in print_insn_little_arm()
8803 and print_insn_big_arm() in opcodes/arm-dis.c will detect
8804 the presence of a Thumb symbol and switch to decoding
8805 Thumb instructions. */
8806
8807 fake_target.flavour = bfd_target_coff_flavour;
8808 fake_bfd.xvec = &fake_target;
8809 ce.u.syment.n_sclass = C_THUMBEXTFUNC;
8810 csym.native = &ce;
8811 csym.symbol.the_bfd = &fake_bfd;
8812 csym.symbol.name = "fake";
8813 asym = (asymbol *) & csym;
8814 }
8815
8816 memaddr = UNMAKE_THUMB_ADDR (memaddr);
8817 info->symbols = &asym;
8818 }
8819 else
8820 info->symbols = NULL;
8821
8822 if (info->endian == BFD_ENDIAN_BIG)
8823 return print_insn_big_arm (memaddr, info);
8824 else
8825 return print_insn_little_arm (memaddr, info);
8826}
8827
8828/* The following define instruction sequences that will cause ARM
8829 cpu's to take an undefined instruction trap. These are used to
8830 signal a breakpoint to GDB.
8831
8832 The newer ARMv4T cpu's are capable of operating in ARM or Thumb
8833 modes. A different instruction is required for each mode. The ARM
8834 cpu's can also be big or little endian. Thus four different
8835 instructions are needed to support all cases.
8836
8837 Note: ARMv4 defines several new instructions that will take the
8838 undefined instruction trap. ARM7TDMI is nominally ARMv4T, but does
8839 not in fact add the new instructions. The new undefined
8840 instructions in ARMv4 are all instructions that had no defined
8841 behaviour in earlier chips. There is no guarantee that they will
8842 raise an exception, but may be treated as NOP's. In practice, it
8843 may only safe to rely on instructions matching:
8844
8845 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
8846 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
8847 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
8848
0963b4bd 8849 Even this may only true if the condition predicate is true. The
cca44b1b
JB
8850 following use a condition predicate of ALWAYS so it is always TRUE.
8851
8852 There are other ways of forcing a breakpoint. GNU/Linux, RISC iX,
8853 and NetBSD all use a software interrupt rather than an undefined
8854 instruction to force a trap. This can be handled by by the
8855 abi-specific code during establishment of the gdbarch vector. */
8856
8857#define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
8858#define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
8859#define THUMB_LE_BREAKPOINT {0xbe,0xbe}
8860#define THUMB_BE_BREAKPOINT {0xbe,0xbe}
8861
948f8e3d
PA
8862static const gdb_byte arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
8863static const gdb_byte arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
8864static const gdb_byte arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
8865static const gdb_byte arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
cca44b1b
JB
8866
8867/* Determine the type and size of breakpoint to insert at PCPTR. Uses
8868 the program counter value to determine whether a 16-bit or 32-bit
8869 breakpoint should be used. It returns a pointer to a string of
8870 bytes that encode a breakpoint instruction, stores the length of
8871 the string to *lenptr, and adjusts the program counter (if
8872 necessary) to point to the actual memory location where the
8873 breakpoint should be inserted. */
8874
8875static const unsigned char *
8876arm_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenptr)
8877{
8878 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
177321bd 8879 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
cca44b1b 8880
9779414d 8881 if (arm_pc_is_thumb (gdbarch, *pcptr))
cca44b1b
JB
8882 {
8883 *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
177321bd
DJ
8884
8885 /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
8886 check whether we are replacing a 32-bit instruction. */
8887 if (tdep->thumb2_breakpoint != NULL)
8888 {
8889 gdb_byte buf[2];
8890 if (target_read_memory (*pcptr, buf, 2) == 0)
8891 {
8892 unsigned short inst1;
8893 inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
db24da6d 8894 if (thumb_insn_size (inst1) == 4)
177321bd
DJ
8895 {
8896 *lenptr = tdep->thumb2_breakpoint_size;
8897 return tdep->thumb2_breakpoint;
8898 }
8899 }
8900 }
8901
cca44b1b
JB
8902 *lenptr = tdep->thumb_breakpoint_size;
8903 return tdep->thumb_breakpoint;
8904 }
8905 else
8906 {
8907 *lenptr = tdep->arm_breakpoint_size;
8908 return tdep->arm_breakpoint;
8909 }
8910}
8911
177321bd
DJ
8912static void
8913arm_remote_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
8914 int *kindptr)
8915{
177321bd
DJ
8916 arm_breakpoint_from_pc (gdbarch, pcptr, kindptr);
8917
9779414d 8918 if (arm_pc_is_thumb (gdbarch, *pcptr) && *kindptr == 4)
177321bd
DJ
8919 /* The documented magic value for a 32-bit Thumb-2 breakpoint, so
8920 that this is not confused with a 32-bit ARM breakpoint. */
8921 *kindptr = 3;
8922}
8923
cca44b1b
JB
8924/* Extract from an array REGBUF containing the (raw) register state a
8925 function return value of type TYPE, and copy that, in virtual
8926 format, into VALBUF. */
8927
8928static void
8929arm_extract_return_value (struct type *type, struct regcache *regs,
8930 gdb_byte *valbuf)
8931{
8932 struct gdbarch *gdbarch = get_regcache_arch (regs);
8933 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8934
8935 if (TYPE_CODE_FLT == TYPE_CODE (type))
8936 {
8937 switch (gdbarch_tdep (gdbarch)->fp_model)
8938 {
8939 case ARM_FLOAT_FPA:
8940 {
8941 /* The value is in register F0 in internal format. We need to
8942 extract the raw value and then convert it to the desired
8943 internal type. */
8944 bfd_byte tmpbuf[FP_REGISTER_SIZE];
8945
8946 regcache_cooked_read (regs, ARM_F0_REGNUM, tmpbuf);
8947 convert_from_extended (floatformat_from_type (type), tmpbuf,
8948 valbuf, gdbarch_byte_order (gdbarch));
8949 }
8950 break;
8951
8952 case ARM_FLOAT_SOFT_FPA:
8953 case ARM_FLOAT_SOFT_VFP:
8954 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8955 not using the VFP ABI code. */
8956 case ARM_FLOAT_VFP:
8957 regcache_cooked_read (regs, ARM_A1_REGNUM, valbuf);
8958 if (TYPE_LENGTH (type) > 4)
8959 regcache_cooked_read (regs, ARM_A1_REGNUM + 1,
8960 valbuf + INT_REGISTER_SIZE);
8961 break;
8962
8963 default:
0963b4bd
MS
8964 internal_error (__FILE__, __LINE__,
8965 _("arm_extract_return_value: "
8966 "Floating point model not supported"));
cca44b1b
JB
8967 break;
8968 }
8969 }
8970 else if (TYPE_CODE (type) == TYPE_CODE_INT
8971 || TYPE_CODE (type) == TYPE_CODE_CHAR
8972 || TYPE_CODE (type) == TYPE_CODE_BOOL
8973 || TYPE_CODE (type) == TYPE_CODE_PTR
8974 || TYPE_CODE (type) == TYPE_CODE_REF
8975 || TYPE_CODE (type) == TYPE_CODE_ENUM)
8976 {
b021a221
MS
8977 /* If the type is a plain integer, then the access is
8978 straight-forward. Otherwise we have to play around a bit
8979 more. */
cca44b1b
JB
8980 int len = TYPE_LENGTH (type);
8981 int regno = ARM_A1_REGNUM;
8982 ULONGEST tmp;
8983
8984 while (len > 0)
8985 {
8986 /* By using store_unsigned_integer we avoid having to do
8987 anything special for small big-endian values. */
8988 regcache_cooked_read_unsigned (regs, regno++, &tmp);
8989 store_unsigned_integer (valbuf,
8990 (len > INT_REGISTER_SIZE
8991 ? INT_REGISTER_SIZE : len),
8992 byte_order, tmp);
8993 len -= INT_REGISTER_SIZE;
8994 valbuf += INT_REGISTER_SIZE;
8995 }
8996 }
8997 else
8998 {
8999 /* For a structure or union the behaviour is as if the value had
9000 been stored to word-aligned memory and then loaded into
9001 registers with 32-bit load instruction(s). */
9002 int len = TYPE_LENGTH (type);
9003 int regno = ARM_A1_REGNUM;
9004 bfd_byte tmpbuf[INT_REGISTER_SIZE];
9005
9006 while (len > 0)
9007 {
9008 regcache_cooked_read (regs, regno++, tmpbuf);
9009 memcpy (valbuf, tmpbuf,
9010 len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
9011 len -= INT_REGISTER_SIZE;
9012 valbuf += INT_REGISTER_SIZE;
9013 }
9014 }
9015}
9016
9017
9018/* Will a function return an aggregate type in memory or in a
9019 register? Return 0 if an aggregate type can be returned in a
9020 register, 1 if it must be returned in memory. */
9021
9022static int
9023arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
9024{
cca44b1b
JB
9025 enum type_code code;
9026
f168693b 9027 type = check_typedef (type);
cca44b1b 9028
b13c8ab2
YQ
9029 /* Simple, non-aggregate types (ie not including vectors and
9030 complex) are always returned in a register (or registers). */
9031 code = TYPE_CODE (type);
9032 if (TYPE_CODE_STRUCT != code && TYPE_CODE_UNION != code
9033 && TYPE_CODE_ARRAY != code && TYPE_CODE_COMPLEX != code)
9034 return 0;
cca44b1b 9035
c4312b19
YQ
9036 if (TYPE_CODE_ARRAY == code && TYPE_VECTOR (type))
9037 {
9038 /* Vector values should be returned using ARM registers if they
9039 are not over 16 bytes. */
9040 return (TYPE_LENGTH (type) > 16);
9041 }
9042
b13c8ab2 9043 if (gdbarch_tdep (gdbarch)->arm_abi != ARM_ABI_APCS)
cca44b1b 9044 {
b13c8ab2
YQ
9045 /* The AAPCS says all aggregates not larger than a word are returned
9046 in a register. */
9047 if (TYPE_LENGTH (type) <= INT_REGISTER_SIZE)
9048 return 0;
9049
cca44b1b
JB
9050 return 1;
9051 }
b13c8ab2
YQ
9052 else
9053 {
9054 int nRc;
cca44b1b 9055
b13c8ab2
YQ
9056 /* All aggregate types that won't fit in a register must be returned
9057 in memory. */
9058 if (TYPE_LENGTH (type) > INT_REGISTER_SIZE)
9059 return 1;
cca44b1b 9060
b13c8ab2
YQ
9061 /* In the ARM ABI, "integer" like aggregate types are returned in
9062 registers. For an aggregate type to be integer like, its size
9063 must be less than or equal to INT_REGISTER_SIZE and the
9064 offset of each addressable subfield must be zero. Note that bit
9065 fields are not addressable, and all addressable subfields of
9066 unions always start at offset zero.
cca44b1b 9067
b13c8ab2
YQ
9068 This function is based on the behaviour of GCC 2.95.1.
9069 See: gcc/arm.c: arm_return_in_memory() for details.
cca44b1b 9070
b13c8ab2
YQ
9071 Note: All versions of GCC before GCC 2.95.2 do not set up the
9072 parameters correctly for a function returning the following
9073 structure: struct { float f;}; This should be returned in memory,
9074 not a register. Richard Earnshaw sent me a patch, but I do not
9075 know of any way to detect if a function like the above has been
9076 compiled with the correct calling convention. */
9077
9078 /* Assume all other aggregate types can be returned in a register.
9079 Run a check for structures, unions and arrays. */
9080 nRc = 0;
67255d04 9081
b13c8ab2
YQ
9082 if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
9083 {
9084 int i;
9085 /* Need to check if this struct/union is "integer" like. For
9086 this to be true, its size must be less than or equal to
9087 INT_REGISTER_SIZE and the offset of each addressable
9088 subfield must be zero. Note that bit fields are not
9089 addressable, and unions always start at offset zero. If any
9090 of the subfields is a floating point type, the struct/union
9091 cannot be an integer type. */
9092
9093 /* For each field in the object, check:
9094 1) Is it FP? --> yes, nRc = 1;
9095 2) Is it addressable (bitpos != 0) and
9096 not packed (bitsize == 0)?
9097 --> yes, nRc = 1
9098 */
9099
9100 for (i = 0; i < TYPE_NFIELDS (type); i++)
67255d04 9101 {
b13c8ab2
YQ
9102 enum type_code field_type_code;
9103
9104 field_type_code
9105 = TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type,
9106 i)));
9107
9108 /* Is it a floating point type field? */
9109 if (field_type_code == TYPE_CODE_FLT)
67255d04
RE
9110 {
9111 nRc = 1;
9112 break;
9113 }
b13c8ab2
YQ
9114
9115 /* If bitpos != 0, then we have to care about it. */
9116 if (TYPE_FIELD_BITPOS (type, i) != 0)
9117 {
9118 /* Bitfields are not addressable. If the field bitsize is
9119 zero, then the field is not packed. Hence it cannot be
9120 a bitfield or any other packed type. */
9121 if (TYPE_FIELD_BITSIZE (type, i) == 0)
9122 {
9123 nRc = 1;
9124 break;
9125 }
9126 }
67255d04
RE
9127 }
9128 }
67255d04 9129
b13c8ab2
YQ
9130 return nRc;
9131 }
67255d04
RE
9132}
9133
34e8f22d
RE
9134/* Write into appropriate registers a function return value of type
9135 TYPE, given in virtual format. */
9136
9137static void
b508a996 9138arm_store_return_value (struct type *type, struct regcache *regs,
5238cf52 9139 const gdb_byte *valbuf)
34e8f22d 9140{
be8626e0 9141 struct gdbarch *gdbarch = get_regcache_arch (regs);
e17a4113 9142 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
be8626e0 9143
34e8f22d
RE
9144 if (TYPE_CODE (type) == TYPE_CODE_FLT)
9145 {
e362b510 9146 gdb_byte buf[MAX_REGISTER_SIZE];
34e8f22d 9147
be8626e0 9148 switch (gdbarch_tdep (gdbarch)->fp_model)
08216dd7
RE
9149 {
9150 case ARM_FLOAT_FPA:
9151
be8626e0
MD
9152 convert_to_extended (floatformat_from_type (type), buf, valbuf,
9153 gdbarch_byte_order (gdbarch));
b508a996 9154 regcache_cooked_write (regs, ARM_F0_REGNUM, buf);
08216dd7
RE
9155 break;
9156
fd50bc42 9157 case ARM_FLOAT_SOFT_FPA:
08216dd7 9158 case ARM_FLOAT_SOFT_VFP:
90445bd3
DJ
9159 /* ARM_FLOAT_VFP can arise if this is a variadic function so
9160 not using the VFP ABI code. */
9161 case ARM_FLOAT_VFP:
b508a996
RE
9162 regcache_cooked_write (regs, ARM_A1_REGNUM, valbuf);
9163 if (TYPE_LENGTH (type) > 4)
9164 regcache_cooked_write (regs, ARM_A1_REGNUM + 1,
7a5ea0d4 9165 valbuf + INT_REGISTER_SIZE);
08216dd7
RE
9166 break;
9167
9168 default:
9b20d036
MS
9169 internal_error (__FILE__, __LINE__,
9170 _("arm_store_return_value: Floating "
9171 "point model not supported"));
08216dd7
RE
9172 break;
9173 }
34e8f22d 9174 }
b508a996
RE
9175 else if (TYPE_CODE (type) == TYPE_CODE_INT
9176 || TYPE_CODE (type) == TYPE_CODE_CHAR
9177 || TYPE_CODE (type) == TYPE_CODE_BOOL
9178 || TYPE_CODE (type) == TYPE_CODE_PTR
9179 || TYPE_CODE (type) == TYPE_CODE_REF
9180 || TYPE_CODE (type) == TYPE_CODE_ENUM)
9181 {
9182 if (TYPE_LENGTH (type) <= 4)
9183 {
9184 /* Values of one word or less are zero/sign-extended and
9185 returned in r0. */
7a5ea0d4 9186 bfd_byte tmpbuf[INT_REGISTER_SIZE];
b508a996
RE
9187 LONGEST val = unpack_long (type, valbuf);
9188
e17a4113 9189 store_signed_integer (tmpbuf, INT_REGISTER_SIZE, byte_order, val);
b508a996
RE
9190 regcache_cooked_write (regs, ARM_A1_REGNUM, tmpbuf);
9191 }
9192 else
9193 {
9194 /* Integral values greater than one word are stored in consecutive
9195 registers starting with r0. This will always be a multiple of
9196 the regiser size. */
9197 int len = TYPE_LENGTH (type);
9198 int regno = ARM_A1_REGNUM;
9199
9200 while (len > 0)
9201 {
9202 regcache_cooked_write (regs, regno++, valbuf);
7a5ea0d4
DJ
9203 len -= INT_REGISTER_SIZE;
9204 valbuf += INT_REGISTER_SIZE;
b508a996
RE
9205 }
9206 }
9207 }
34e8f22d 9208 else
b508a996
RE
9209 {
9210 /* For a structure or union the behaviour is as if the value had
9211 been stored to word-aligned memory and then loaded into
9212 registers with 32-bit load instruction(s). */
9213 int len = TYPE_LENGTH (type);
9214 int regno = ARM_A1_REGNUM;
7a5ea0d4 9215 bfd_byte tmpbuf[INT_REGISTER_SIZE];
b508a996
RE
9216
9217 while (len > 0)
9218 {
9219 memcpy (tmpbuf, valbuf,
7a5ea0d4 9220 len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
b508a996 9221 regcache_cooked_write (regs, regno++, tmpbuf);
7a5ea0d4
DJ
9222 len -= INT_REGISTER_SIZE;
9223 valbuf += INT_REGISTER_SIZE;
b508a996
RE
9224 }
9225 }
34e8f22d
RE
9226}
9227
2af48f68
PB
9228
9229/* Handle function return values. */
9230
9231static enum return_value_convention
6a3a010b 9232arm_return_value (struct gdbarch *gdbarch, struct value *function,
c055b101
CV
9233 struct type *valtype, struct regcache *regcache,
9234 gdb_byte *readbuf, const gdb_byte *writebuf)
2af48f68 9235{
7c00367c 9236 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
6a3a010b 9237 struct type *func_type = function ? value_type (function) : NULL;
90445bd3
DJ
9238 enum arm_vfp_cprc_base_type vfp_base_type;
9239 int vfp_base_count;
9240
9241 if (arm_vfp_abi_for_function (gdbarch, func_type)
9242 && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
9243 {
9244 int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
9245 int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
9246 int i;
9247 for (i = 0; i < vfp_base_count; i++)
9248 {
58d6951d
DJ
9249 if (reg_char == 'q')
9250 {
9251 if (writebuf)
9252 arm_neon_quad_write (gdbarch, regcache, i,
9253 writebuf + i * unit_length);
9254
9255 if (readbuf)
9256 arm_neon_quad_read (gdbarch, regcache, i,
9257 readbuf + i * unit_length);
9258 }
9259 else
9260 {
9261 char name_buf[4];
9262 int regnum;
9263
8c042590 9264 xsnprintf (name_buf, sizeof (name_buf), "%c%d", reg_char, i);
58d6951d
DJ
9265 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9266 strlen (name_buf));
9267 if (writebuf)
9268 regcache_cooked_write (regcache, regnum,
9269 writebuf + i * unit_length);
9270 if (readbuf)
9271 regcache_cooked_read (regcache, regnum,
9272 readbuf + i * unit_length);
9273 }
90445bd3
DJ
9274 }
9275 return RETURN_VALUE_REGISTER_CONVENTION;
9276 }
7c00367c 9277
2af48f68
PB
9278 if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
9279 || TYPE_CODE (valtype) == TYPE_CODE_UNION
9280 || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
9281 {
7c00367c
MK
9282 if (tdep->struct_return == pcc_struct_return
9283 || arm_return_in_memory (gdbarch, valtype))
2af48f68
PB
9284 return RETURN_VALUE_STRUCT_CONVENTION;
9285 }
b13c8ab2
YQ
9286 else if (TYPE_CODE (valtype) == TYPE_CODE_COMPLEX)
9287 {
9288 if (arm_return_in_memory (gdbarch, valtype))
9289 return RETURN_VALUE_STRUCT_CONVENTION;
9290 }
7052e42c 9291
2af48f68
PB
9292 if (writebuf)
9293 arm_store_return_value (valtype, regcache, writebuf);
9294
9295 if (readbuf)
9296 arm_extract_return_value (valtype, regcache, readbuf);
9297
9298 return RETURN_VALUE_REGISTER_CONVENTION;
9299}
9300
9301
9df628e0 9302static int
60ade65d 9303arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
9df628e0 9304{
e17a4113
UW
9305 struct gdbarch *gdbarch = get_frame_arch (frame);
9306 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
9307 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
9df628e0 9308 CORE_ADDR jb_addr;
e362b510 9309 gdb_byte buf[INT_REGISTER_SIZE];
9df628e0 9310
60ade65d 9311 jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
9df628e0
RE
9312
9313 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
7a5ea0d4 9314 INT_REGISTER_SIZE))
9df628e0
RE
9315 return 0;
9316
e17a4113 9317 *pc = extract_unsigned_integer (buf, INT_REGISTER_SIZE, byte_order);
9df628e0
RE
9318 return 1;
9319}
9320
faa95490
DJ
9321/* Recognize GCC and GNU ld's trampolines. If we are in a trampoline,
9322 return the target PC. Otherwise return 0. */
c906108c
SS
9323
9324CORE_ADDR
52f729a7 9325arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
c906108c 9326{
2c02bd72 9327 const char *name;
faa95490 9328 int namelen;
c906108c
SS
9329 CORE_ADDR start_addr;
9330
9331 /* Find the starting address and name of the function containing the PC. */
9332 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
80d8d390
YQ
9333 {
9334 /* Trampoline 'bx reg' doesn't belong to any functions. Do the
9335 check here. */
9336 start_addr = arm_skip_bx_reg (frame, pc);
9337 if (start_addr != 0)
9338 return start_addr;
9339
9340 return 0;
9341 }
c906108c 9342
faa95490
DJ
9343 /* If PC is in a Thumb call or return stub, return the address of the
9344 target PC, which is in a register. The thunk functions are called
9345 _call_via_xx, where x is the register name. The possible names
3d8d5e79
DJ
9346 are r0-r9, sl, fp, ip, sp, and lr. ARM RealView has similar
9347 functions, named __ARM_call_via_r[0-7]. */
61012eef
GB
9348 if (startswith (name, "_call_via_")
9349 || startswith (name, "__ARM_call_via_"))
c906108c 9350 {
ed9a39eb
JM
9351 /* Use the name suffix to determine which register contains the
9352 target PC. */
c5aa993b
JM
9353 static char *table[15] =
9354 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
9355 "r8", "r9", "sl", "fp", "ip", "sp", "lr"
9356 };
c906108c 9357 int regno;
faa95490 9358 int offset = strlen (name) - 2;
c906108c
SS
9359
9360 for (regno = 0; regno <= 14; regno++)
faa95490 9361 if (strcmp (&name[offset], table[regno]) == 0)
52f729a7 9362 return get_frame_register_unsigned (frame, regno);
c906108c 9363 }
ed9a39eb 9364
faa95490
DJ
9365 /* GNU ld generates __foo_from_arm or __foo_from_thumb for
9366 non-interworking calls to foo. We could decode the stubs
9367 to find the target but it's easier to use the symbol table. */
9368 namelen = strlen (name);
9369 if (name[0] == '_' && name[1] == '_'
9370 && ((namelen > 2 + strlen ("_from_thumb")
61012eef 9371 && startswith (name + namelen - strlen ("_from_thumb"), "_from_thumb"))
faa95490 9372 || (namelen > 2 + strlen ("_from_arm")
61012eef 9373 && startswith (name + namelen - strlen ("_from_arm"), "_from_arm"))))
faa95490
DJ
9374 {
9375 char *target_name;
9376 int target_len = namelen - 2;
3b7344d5 9377 struct bound_minimal_symbol minsym;
faa95490
DJ
9378 struct objfile *objfile;
9379 struct obj_section *sec;
9380
9381 if (name[namelen - 1] == 'b')
9382 target_len -= strlen ("_from_thumb");
9383 else
9384 target_len -= strlen ("_from_arm");
9385
224c3ddb 9386 target_name = (char *) alloca (target_len + 1);
faa95490
DJ
9387 memcpy (target_name, name + 2, target_len);
9388 target_name[target_len] = '\0';
9389
9390 sec = find_pc_section (pc);
9391 objfile = (sec == NULL) ? NULL : sec->objfile;
9392 minsym = lookup_minimal_symbol (target_name, NULL, objfile);
3b7344d5 9393 if (minsym.minsym != NULL)
77e371c0 9394 return BMSYMBOL_VALUE_ADDRESS (minsym);
faa95490
DJ
9395 else
9396 return 0;
9397 }
9398
c5aa993b 9399 return 0; /* not a stub */
c906108c
SS
9400}
9401
afd7eef0
RE
9402static void
9403set_arm_command (char *args, int from_tty)
9404{
edefbb7c
AC
9405 printf_unfiltered (_("\
9406\"set arm\" must be followed by an apporpriate subcommand.\n"));
afd7eef0
RE
9407 help_list (setarmcmdlist, "set arm ", all_commands, gdb_stdout);
9408}
9409
9410static void
9411show_arm_command (char *args, int from_tty)
9412{
26304000 9413 cmd_show_list (showarmcmdlist, from_tty, "");
afd7eef0
RE
9414}
9415
28e97307
DJ
9416static void
9417arm_update_current_architecture (void)
fd50bc42 9418{
28e97307 9419 struct gdbarch_info info;
fd50bc42 9420
28e97307 9421 /* If the current architecture is not ARM, we have nothing to do. */
f5656ead 9422 if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_arm)
28e97307 9423 return;
fd50bc42 9424
28e97307
DJ
9425 /* Update the architecture. */
9426 gdbarch_info_init (&info);
fd50bc42 9427
28e97307 9428 if (!gdbarch_update_p (info))
9b20d036 9429 internal_error (__FILE__, __LINE__, _("could not update architecture"));
fd50bc42
RE
9430}
9431
9432static void
9433set_fp_model_sfunc (char *args, int from_tty,
9434 struct cmd_list_element *c)
9435{
570dc176 9436 int fp_model;
fd50bc42
RE
9437
9438 for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
9439 if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
9440 {
aead7601 9441 arm_fp_model = (enum arm_float_model) fp_model;
fd50bc42
RE
9442 break;
9443 }
9444
9445 if (fp_model == ARM_FLOAT_LAST)
edefbb7c 9446 internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
fd50bc42
RE
9447 current_fp_model);
9448
28e97307 9449 arm_update_current_architecture ();
fd50bc42
RE
9450}
9451
9452static void
08546159
AC
9453show_fp_model (struct ui_file *file, int from_tty,
9454 struct cmd_list_element *c, const char *value)
fd50bc42 9455{
f5656ead 9456 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
fd50bc42 9457
28e97307 9458 if (arm_fp_model == ARM_FLOAT_AUTO
f5656ead 9459 && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
28e97307
DJ
9460 fprintf_filtered (file, _("\
9461The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
9462 fp_model_strings[tdep->fp_model]);
9463 else
9464 fprintf_filtered (file, _("\
9465The current ARM floating point model is \"%s\".\n"),
9466 fp_model_strings[arm_fp_model]);
9467}
9468
9469static void
9470arm_set_abi (char *args, int from_tty,
9471 struct cmd_list_element *c)
9472{
570dc176 9473 int arm_abi;
28e97307
DJ
9474
9475 for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
9476 if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
9477 {
aead7601 9478 arm_abi_global = (enum arm_abi_kind) arm_abi;
28e97307
DJ
9479 break;
9480 }
9481
9482 if (arm_abi == ARM_ABI_LAST)
9483 internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
9484 arm_abi_string);
9485
9486 arm_update_current_architecture ();
9487}
9488
9489static void
9490arm_show_abi (struct ui_file *file, int from_tty,
9491 struct cmd_list_element *c, const char *value)
9492{
f5656ead 9493 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
28e97307
DJ
9494
9495 if (arm_abi_global == ARM_ABI_AUTO
f5656ead 9496 && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
28e97307
DJ
9497 fprintf_filtered (file, _("\
9498The current ARM ABI is \"auto\" (currently \"%s\").\n"),
9499 arm_abi_strings[tdep->arm_abi]);
9500 else
9501 fprintf_filtered (file, _("The current ARM ABI is \"%s\".\n"),
9502 arm_abi_string);
fd50bc42
RE
9503}
9504
0428b8f5
DJ
9505static void
9506arm_show_fallback_mode (struct ui_file *file, int from_tty,
9507 struct cmd_list_element *c, const char *value)
9508{
0963b4bd
MS
9509 fprintf_filtered (file,
9510 _("The current execution mode assumed "
9511 "(when symbols are unavailable) is \"%s\".\n"),
0428b8f5
DJ
9512 arm_fallback_mode_string);
9513}
9514
9515static void
9516arm_show_force_mode (struct ui_file *file, int from_tty,
9517 struct cmd_list_element *c, const char *value)
9518{
f5656ead 9519 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
0428b8f5 9520
0963b4bd
MS
9521 fprintf_filtered (file,
9522 _("The current execution mode assumed "
9523 "(even when symbols are available) is \"%s\".\n"),
0428b8f5
DJ
9524 arm_force_mode_string);
9525}
9526
afd7eef0
RE
9527/* If the user changes the register disassembly style used for info
9528 register and other commands, we have to also switch the style used
9529 in opcodes for disassembly output. This function is run in the "set
9530 arm disassembly" command, and does that. */
bc90b915
FN
9531
9532static void
afd7eef0 9533set_disassembly_style_sfunc (char *args, int from_tty,
bc90b915
FN
9534 struct cmd_list_element *c)
9535{
afd7eef0 9536 set_disassembly_style ();
bc90b915
FN
9537}
9538\f
966fbf70 9539/* Return the ARM register name corresponding to register I. */
a208b0cb 9540static const char *
d93859e2 9541arm_register_name (struct gdbarch *gdbarch, int i)
966fbf70 9542{
58d6951d
DJ
9543 const int num_regs = gdbarch_num_regs (gdbarch);
9544
9545 if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
9546 && i >= num_regs && i < num_regs + 32)
9547 {
9548 static const char *const vfp_pseudo_names[] = {
9549 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
9550 "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
9551 "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
9552 "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
9553 };
9554
9555 return vfp_pseudo_names[i - num_regs];
9556 }
9557
9558 if (gdbarch_tdep (gdbarch)->have_neon_pseudos
9559 && i >= num_regs + 32 && i < num_regs + 32 + 16)
9560 {
9561 static const char *const neon_pseudo_names[] = {
9562 "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
9563 "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
9564 };
9565
9566 return neon_pseudo_names[i - num_regs - 32];
9567 }
9568
ff6f572f
DJ
9569 if (i >= ARRAY_SIZE (arm_register_names))
9570 /* These registers are only supported on targets which supply
9571 an XML description. */
9572 return "";
9573
966fbf70
RE
9574 return arm_register_names[i];
9575}
9576
bc90b915 9577static void
afd7eef0 9578set_disassembly_style (void)
bc90b915 9579{
123dc839 9580 int current;
bc90b915 9581
123dc839
DJ
9582 /* Find the style that the user wants. */
9583 for (current = 0; current < num_disassembly_options; current++)
9584 if (disassembly_style == valid_disassembly_styles[current])
9585 break;
9586 gdb_assert (current < num_disassembly_options);
bc90b915 9587
94c30b78 9588 /* Synchronize the disassembler. */
bc90b915
FN
9589 set_arm_regname_option (current);
9590}
9591
082fc60d
RE
9592/* Test whether the coff symbol specific value corresponds to a Thumb
9593 function. */
9594
9595static int
9596coff_sym_is_thumb (int val)
9597{
f8bf5763
PM
9598 return (val == C_THUMBEXT
9599 || val == C_THUMBSTAT
9600 || val == C_THUMBEXTFUNC
9601 || val == C_THUMBSTATFUNC
9602 || val == C_THUMBLABEL);
082fc60d
RE
9603}
9604
9605/* arm_coff_make_msymbol_special()
9606 arm_elf_make_msymbol_special()
9607
9608 These functions test whether the COFF or ELF symbol corresponds to
9609 an address in thumb code, and set a "special" bit in a minimal
9610 symbol to indicate that it does. */
9611
34e8f22d 9612static void
082fc60d
RE
9613arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
9614{
467d42c4
UW
9615 if (ARM_SYM_BRANCH_TYPE (&((elf_symbol_type *)sym)->internal_elf_sym)
9616 == ST_BRANCH_TO_THUMB)
082fc60d
RE
9617 MSYMBOL_SET_SPECIAL (msym);
9618}
9619
34e8f22d 9620static void
082fc60d
RE
9621arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
9622{
9623 if (coff_sym_is_thumb (val))
9624 MSYMBOL_SET_SPECIAL (msym);
9625}
9626
60c5725c 9627static void
c1bd65d0 9628arm_objfile_data_free (struct objfile *objfile, void *arg)
60c5725c 9629{
9a3c8263 9630 struct arm_per_objfile *data = (struct arm_per_objfile *) arg;
60c5725c
DJ
9631 unsigned int i;
9632
9633 for (i = 0; i < objfile->obfd->section_count; i++)
9634 VEC_free (arm_mapping_symbol_s, data->section_maps[i]);
9635}
9636
9637static void
9638arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
9639 asymbol *sym)
9640{
9641 const char *name = bfd_asymbol_name (sym);
9642 struct arm_per_objfile *data;
9643 VEC(arm_mapping_symbol_s) **map_p;
9644 struct arm_mapping_symbol new_map_sym;
9645
9646 gdb_assert (name[0] == '$');
9647 if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
9648 return;
9649
9a3c8263
SM
9650 data = (struct arm_per_objfile *) objfile_data (objfile,
9651 arm_objfile_data_key);
60c5725c
DJ
9652 if (data == NULL)
9653 {
9654 data = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9655 struct arm_per_objfile);
9656 set_objfile_data (objfile, arm_objfile_data_key, data);
9657 data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
9658 objfile->obfd->section_count,
9659 VEC(arm_mapping_symbol_s) *);
9660 }
9661 map_p = &data->section_maps[bfd_get_section (sym)->index];
9662
9663 new_map_sym.value = sym->value;
9664 new_map_sym.type = name[1];
9665
9666 /* Assume that most mapping symbols appear in order of increasing
9667 value. If they were randomly distributed, it would be faster to
9668 always push here and then sort at first use. */
9669 if (!VEC_empty (arm_mapping_symbol_s, *map_p))
9670 {
9671 struct arm_mapping_symbol *prev_map_sym;
9672
9673 prev_map_sym = VEC_last (arm_mapping_symbol_s, *map_p);
9674 if (prev_map_sym->value >= sym->value)
9675 {
9676 unsigned int idx;
9677 idx = VEC_lower_bound (arm_mapping_symbol_s, *map_p, &new_map_sym,
9678 arm_compare_mapping_symbols);
9679 VEC_safe_insert (arm_mapping_symbol_s, *map_p, idx, &new_map_sym);
9680 return;
9681 }
9682 }
9683
9684 VEC_safe_push (arm_mapping_symbol_s, *map_p, &new_map_sym);
9685}
9686
756fe439 9687static void
61a1198a 9688arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
756fe439 9689{
9779414d 9690 struct gdbarch *gdbarch = get_regcache_arch (regcache);
61a1198a 9691 regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
756fe439
DJ
9692
9693 /* If necessary, set the T bit. */
9694 if (arm_apcs_32)
9695 {
9779414d 9696 ULONGEST val, t_bit;
61a1198a 9697 regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
9779414d
DJ
9698 t_bit = arm_psr_thumb_bit (gdbarch);
9699 if (arm_pc_is_thumb (gdbarch, pc))
9700 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9701 val | t_bit);
756fe439 9702 else
61a1198a 9703 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9779414d 9704 val & ~t_bit);
756fe439
DJ
9705 }
9706}
123dc839 9707
58d6951d
DJ
9708/* Read the contents of a NEON quad register, by reading from two
9709 double registers. This is used to implement the quad pseudo
9710 registers, and for argument passing in case the quad registers are
9711 missing; vectors are passed in quad registers when using the VFP
9712 ABI, even if a NEON unit is not present. REGNUM is the index of
9713 the quad register, in [0, 15]. */
9714
05d1431c 9715static enum register_status
58d6951d
DJ
9716arm_neon_quad_read (struct gdbarch *gdbarch, struct regcache *regcache,
9717 int regnum, gdb_byte *buf)
9718{
9719 char name_buf[4];
9720 gdb_byte reg_buf[8];
9721 int offset, double_regnum;
05d1431c 9722 enum register_status status;
58d6951d 9723
8c042590 9724 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
58d6951d
DJ
9725 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9726 strlen (name_buf));
9727
9728 /* d0 is always the least significant half of q0. */
9729 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9730 offset = 8;
9731 else
9732 offset = 0;
9733
05d1431c
PA
9734 status = regcache_raw_read (regcache, double_regnum, reg_buf);
9735 if (status != REG_VALID)
9736 return status;
58d6951d
DJ
9737 memcpy (buf + offset, reg_buf, 8);
9738
9739 offset = 8 - offset;
05d1431c
PA
9740 status = regcache_raw_read (regcache, double_regnum + 1, reg_buf);
9741 if (status != REG_VALID)
9742 return status;
58d6951d 9743 memcpy (buf + offset, reg_buf, 8);
05d1431c
PA
9744
9745 return REG_VALID;
58d6951d
DJ
9746}
9747
05d1431c 9748static enum register_status
58d6951d
DJ
9749arm_pseudo_read (struct gdbarch *gdbarch, struct regcache *regcache,
9750 int regnum, gdb_byte *buf)
9751{
9752 const int num_regs = gdbarch_num_regs (gdbarch);
9753 char name_buf[4];
9754 gdb_byte reg_buf[8];
9755 int offset, double_regnum;
9756
9757 gdb_assert (regnum >= num_regs);
9758 regnum -= num_regs;
9759
9760 if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
9761 /* Quad-precision register. */
05d1431c 9762 return arm_neon_quad_read (gdbarch, regcache, regnum - 32, buf);
58d6951d
DJ
9763 else
9764 {
05d1431c
PA
9765 enum register_status status;
9766
58d6951d
DJ
9767 /* Single-precision register. */
9768 gdb_assert (regnum < 32);
9769
9770 /* s0 is always the least significant half of d0. */
9771 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9772 offset = (regnum & 1) ? 0 : 4;
9773 else
9774 offset = (regnum & 1) ? 4 : 0;
9775
8c042590 9776 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
58d6951d
DJ
9777 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9778 strlen (name_buf));
9779
05d1431c
PA
9780 status = regcache_raw_read (regcache, double_regnum, reg_buf);
9781 if (status == REG_VALID)
9782 memcpy (buf, reg_buf + offset, 4);
9783 return status;
58d6951d
DJ
9784 }
9785}
9786
9787/* Store the contents of BUF to a NEON quad register, by writing to
9788 two double registers. This is used to implement the quad pseudo
9789 registers, and for argument passing in case the quad registers are
9790 missing; vectors are passed in quad registers when using the VFP
9791 ABI, even if a NEON unit is not present. REGNUM is the index
9792 of the quad register, in [0, 15]. */
9793
9794static void
9795arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
9796 int regnum, const gdb_byte *buf)
9797{
9798 char name_buf[4];
58d6951d
DJ
9799 int offset, double_regnum;
9800
8c042590 9801 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
58d6951d
DJ
9802 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9803 strlen (name_buf));
9804
9805 /* d0 is always the least significant half of q0. */
9806 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9807 offset = 8;
9808 else
9809 offset = 0;
9810
9811 regcache_raw_write (regcache, double_regnum, buf + offset);
9812 offset = 8 - offset;
9813 regcache_raw_write (regcache, double_regnum + 1, buf + offset);
9814}
9815
9816static void
9817arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
9818 int regnum, const gdb_byte *buf)
9819{
9820 const int num_regs = gdbarch_num_regs (gdbarch);
9821 char name_buf[4];
9822 gdb_byte reg_buf[8];
9823 int offset, double_regnum;
9824
9825 gdb_assert (regnum >= num_regs);
9826 regnum -= num_regs;
9827
9828 if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
9829 /* Quad-precision register. */
9830 arm_neon_quad_write (gdbarch, regcache, regnum - 32, buf);
9831 else
9832 {
9833 /* Single-precision register. */
9834 gdb_assert (regnum < 32);
9835
9836 /* s0 is always the least significant half of d0. */
9837 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9838 offset = (regnum & 1) ? 0 : 4;
9839 else
9840 offset = (regnum & 1) ? 4 : 0;
9841
8c042590 9842 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
58d6951d
DJ
9843 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9844 strlen (name_buf));
9845
9846 regcache_raw_read (regcache, double_regnum, reg_buf);
9847 memcpy (reg_buf + offset, buf, 4);
9848 regcache_raw_write (regcache, double_regnum, reg_buf);
9849 }
9850}
9851
123dc839
DJ
9852static struct value *
9853value_of_arm_user_reg (struct frame_info *frame, const void *baton)
9854{
9a3c8263 9855 const int *reg_p = (const int *) baton;
123dc839
DJ
9856 return value_of_register (*reg_p, frame);
9857}
97e03143 9858\f
70f80edf
JT
9859static enum gdb_osabi
9860arm_elf_osabi_sniffer (bfd *abfd)
97e03143 9861{
2af48f68 9862 unsigned int elfosabi;
70f80edf 9863 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
97e03143 9864
70f80edf 9865 elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
97e03143 9866
28e97307
DJ
9867 if (elfosabi == ELFOSABI_ARM)
9868 /* GNU tools use this value. Check note sections in this case,
9869 as well. */
9870 bfd_map_over_sections (abfd,
9871 generic_elf_osabi_sniff_abi_tag_sections,
9872 &osabi);
97e03143 9873
28e97307 9874 /* Anything else will be handled by the generic ELF sniffer. */
70f80edf 9875 return osabi;
97e03143
RE
9876}
9877
54483882
YQ
9878static int
9879arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
9880 struct reggroup *group)
9881{
2c291032
YQ
9882 /* FPS register's type is INT, but belongs to float_reggroup. Beside
9883 this, FPS register belongs to save_regroup, restore_reggroup, and
9884 all_reggroup, of course. */
54483882 9885 if (regnum == ARM_FPS_REGNUM)
2c291032
YQ
9886 return (group == float_reggroup
9887 || group == save_reggroup
9888 || group == restore_reggroup
9889 || group == all_reggroup);
54483882
YQ
9890 else
9891 return default_register_reggroup_p (gdbarch, regnum, group);
9892}
9893
25f8c692
JL
9894\f
9895/* For backward-compatibility we allow two 'g' packet lengths with
9896 the remote protocol depending on whether FPA registers are
9897 supplied. M-profile targets do not have FPA registers, but some
9898 stubs already exist in the wild which use a 'g' packet which
9899 supplies them albeit with dummy values. The packet format which
9900 includes FPA registers should be considered deprecated for
9901 M-profile targets. */
9902
9903static void
9904arm_register_g_packet_guesses (struct gdbarch *gdbarch)
9905{
9906 if (gdbarch_tdep (gdbarch)->is_m)
9907 {
9908 /* If we know from the executable this is an M-profile target,
9909 cater for remote targets whose register set layout is the
9910 same as the FPA layout. */
9911 register_remote_g_packet_guess (gdbarch,
03145bf4 9912 /* r0-r12,sp,lr,pc; f0-f7; fps,xpsr */
25f8c692
JL
9913 (16 * INT_REGISTER_SIZE)
9914 + (8 * FP_REGISTER_SIZE)
9915 + (2 * INT_REGISTER_SIZE),
9916 tdesc_arm_with_m_fpa_layout);
9917
9918 /* The regular M-profile layout. */
9919 register_remote_g_packet_guess (gdbarch,
9920 /* r0-r12,sp,lr,pc; xpsr */
9921 (16 * INT_REGISTER_SIZE)
9922 + INT_REGISTER_SIZE,
9923 tdesc_arm_with_m);
3184d3f9
JL
9924
9925 /* M-profile plus M4F VFP. */
9926 register_remote_g_packet_guess (gdbarch,
9927 /* r0-r12,sp,lr,pc; d0-d15; fpscr,xpsr */
9928 (16 * INT_REGISTER_SIZE)
9929 + (16 * VFP_REGISTER_SIZE)
9930 + (2 * INT_REGISTER_SIZE),
9931 tdesc_arm_with_m_vfp_d16);
25f8c692
JL
9932 }
9933
9934 /* Otherwise we don't have a useful guess. */
9935}
9936
70f80edf 9937\f
da3c6d4a
MS
9938/* Initialize the current architecture based on INFO. If possible,
9939 re-use an architecture from ARCHES, which is a list of
9940 architectures already created during this debugging session.
97e03143 9941
da3c6d4a
MS
9942 Called e.g. at program startup, when reading a core file, and when
9943 reading a binary file. */
97e03143 9944
39bbf761
RE
9945static struct gdbarch *
9946arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
9947{
97e03143 9948 struct gdbarch_tdep *tdep;
39bbf761 9949 struct gdbarch *gdbarch;
28e97307
DJ
9950 struct gdbarch_list *best_arch;
9951 enum arm_abi_kind arm_abi = arm_abi_global;
9952 enum arm_float_model fp_model = arm_fp_model;
123dc839 9953 struct tdesc_arch_data *tdesc_data = NULL;
9779414d 9954 int i, is_m = 0;
330c6ca9 9955 int vfp_register_count = 0, have_vfp_pseudos = 0, have_neon_pseudos = 0;
a56cc1ce 9956 int have_wmmx_registers = 0;
58d6951d 9957 int have_neon = 0;
ff6f572f 9958 int have_fpa_registers = 1;
9779414d
DJ
9959 const struct target_desc *tdesc = info.target_desc;
9960
9961 /* If we have an object to base this architecture on, try to determine
9962 its ABI. */
9963
9964 if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
9965 {
9966 int ei_osabi, e_flags;
9967
9968 switch (bfd_get_flavour (info.abfd))
9969 {
9970 case bfd_target_aout_flavour:
9971 /* Assume it's an old APCS-style ABI. */
9972 arm_abi = ARM_ABI_APCS;
9973 break;
9974
9975 case bfd_target_coff_flavour:
9976 /* Assume it's an old APCS-style ABI. */
9977 /* XXX WinCE? */
9978 arm_abi = ARM_ABI_APCS;
9979 break;
9980
9981 case bfd_target_elf_flavour:
9982 ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
9983 e_flags = elf_elfheader (info.abfd)->e_flags;
9984
9985 if (ei_osabi == ELFOSABI_ARM)
9986 {
9987 /* GNU tools used to use this value, but do not for EABI
9988 objects. There's nowhere to tag an EABI version
9989 anyway, so assume APCS. */
9990 arm_abi = ARM_ABI_APCS;
9991 }
d403db27 9992 else if (ei_osabi == ELFOSABI_NONE || ei_osabi == ELFOSABI_GNU)
9779414d
DJ
9993 {
9994 int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
9995 int attr_arch, attr_profile;
9996
9997 switch (eabi_ver)
9998 {
9999 case EF_ARM_EABI_UNKNOWN:
10000 /* Assume GNU tools. */
10001 arm_abi = ARM_ABI_APCS;
10002 break;
10003
10004 case EF_ARM_EABI_VER4:
10005 case EF_ARM_EABI_VER5:
10006 arm_abi = ARM_ABI_AAPCS;
10007 /* EABI binaries default to VFP float ordering.
10008 They may also contain build attributes that can
10009 be used to identify if the VFP argument-passing
10010 ABI is in use. */
10011 if (fp_model == ARM_FLOAT_AUTO)
10012 {
10013#ifdef HAVE_ELF
10014 switch (bfd_elf_get_obj_attr_int (info.abfd,
10015 OBJ_ATTR_PROC,
10016 Tag_ABI_VFP_args))
10017 {
b35b0298 10018 case AEABI_VFP_args_base:
9779414d
DJ
10019 /* "The user intended FP parameter/result
10020 passing to conform to AAPCS, base
10021 variant". */
10022 fp_model = ARM_FLOAT_SOFT_VFP;
10023 break;
b35b0298 10024 case AEABI_VFP_args_vfp:
9779414d
DJ
10025 /* "The user intended FP parameter/result
10026 passing to conform to AAPCS, VFP
10027 variant". */
10028 fp_model = ARM_FLOAT_VFP;
10029 break;
b35b0298 10030 case AEABI_VFP_args_toolchain:
9779414d
DJ
10031 /* "The user intended FP parameter/result
10032 passing to conform to tool chain-specific
10033 conventions" - we don't know any such
10034 conventions, so leave it as "auto". */
10035 break;
b35b0298 10036 case AEABI_VFP_args_compatible:
5c294fee
TG
10037 /* "Code is compatible with both the base
10038 and VFP variants; the user did not permit
10039 non-variadic functions to pass FP
10040 parameters/results" - leave it as
10041 "auto". */
10042 break;
9779414d
DJ
10043 default:
10044 /* Attribute value not mentioned in the
5c294fee 10045 November 2012 ABI, so leave it as
9779414d
DJ
10046 "auto". */
10047 break;
10048 }
10049#else
10050 fp_model = ARM_FLOAT_SOFT_VFP;
10051#endif
10052 }
10053 break;
10054
10055 default:
10056 /* Leave it as "auto". */
10057 warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
10058 break;
10059 }
10060
10061#ifdef HAVE_ELF
10062 /* Detect M-profile programs. This only works if the
10063 executable file includes build attributes; GCC does
10064 copy them to the executable, but e.g. RealView does
10065 not. */
10066 attr_arch = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10067 Tag_CPU_arch);
0963b4bd
MS
10068 attr_profile = bfd_elf_get_obj_attr_int (info.abfd,
10069 OBJ_ATTR_PROC,
9779414d
DJ
10070 Tag_CPU_arch_profile);
10071 /* GCC specifies the profile for v6-M; RealView only
10072 specifies the profile for architectures starting with
10073 V7 (as opposed to architectures with a tag
10074 numerically greater than TAG_CPU_ARCH_V7). */
10075 if (!tdesc_has_registers (tdesc)
10076 && (attr_arch == TAG_CPU_ARCH_V6_M
10077 || attr_arch == TAG_CPU_ARCH_V6S_M
10078 || attr_profile == 'M'))
25f8c692 10079 is_m = 1;
9779414d
DJ
10080#endif
10081 }
10082
10083 if (fp_model == ARM_FLOAT_AUTO)
10084 {
10085 int e_flags = elf_elfheader (info.abfd)->e_flags;
10086
10087 switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
10088 {
10089 case 0:
10090 /* Leave it as "auto". Strictly speaking this case
10091 means FPA, but almost nobody uses that now, and
10092 many toolchains fail to set the appropriate bits
10093 for the floating-point model they use. */
10094 break;
10095 case EF_ARM_SOFT_FLOAT:
10096 fp_model = ARM_FLOAT_SOFT_FPA;
10097 break;
10098 case EF_ARM_VFP_FLOAT:
10099 fp_model = ARM_FLOAT_VFP;
10100 break;
10101 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
10102 fp_model = ARM_FLOAT_SOFT_VFP;
10103 break;
10104 }
10105 }
10106
10107 if (e_flags & EF_ARM_BE8)
10108 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
10109
10110 break;
10111
10112 default:
10113 /* Leave it as "auto". */
10114 break;
10115 }
10116 }
123dc839
DJ
10117
10118 /* Check any target description for validity. */
9779414d 10119 if (tdesc_has_registers (tdesc))
123dc839
DJ
10120 {
10121 /* For most registers we require GDB's default names; but also allow
10122 the numeric names for sp / lr / pc, as a convenience. */
10123 static const char *const arm_sp_names[] = { "r13", "sp", NULL };
10124 static const char *const arm_lr_names[] = { "r14", "lr", NULL };
10125 static const char *const arm_pc_names[] = { "r15", "pc", NULL };
10126
10127 const struct tdesc_feature *feature;
58d6951d 10128 int valid_p;
123dc839 10129
9779414d 10130 feature = tdesc_find_feature (tdesc,
123dc839
DJ
10131 "org.gnu.gdb.arm.core");
10132 if (feature == NULL)
9779414d
DJ
10133 {
10134 feature = tdesc_find_feature (tdesc,
10135 "org.gnu.gdb.arm.m-profile");
10136 if (feature == NULL)
10137 return NULL;
10138 else
10139 is_m = 1;
10140 }
123dc839
DJ
10141
10142 tdesc_data = tdesc_data_alloc ();
10143
10144 valid_p = 1;
10145 for (i = 0; i < ARM_SP_REGNUM; i++)
10146 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
10147 arm_register_names[i]);
10148 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
10149 ARM_SP_REGNUM,
10150 arm_sp_names);
10151 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
10152 ARM_LR_REGNUM,
10153 arm_lr_names);
10154 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
10155 ARM_PC_REGNUM,
10156 arm_pc_names);
9779414d
DJ
10157 if (is_m)
10158 valid_p &= tdesc_numbered_register (feature, tdesc_data,
10159 ARM_PS_REGNUM, "xpsr");
10160 else
10161 valid_p &= tdesc_numbered_register (feature, tdesc_data,
10162 ARM_PS_REGNUM, "cpsr");
123dc839
DJ
10163
10164 if (!valid_p)
10165 {
10166 tdesc_data_cleanup (tdesc_data);
10167 return NULL;
10168 }
10169
9779414d 10170 feature = tdesc_find_feature (tdesc,
123dc839
DJ
10171 "org.gnu.gdb.arm.fpa");
10172 if (feature != NULL)
10173 {
10174 valid_p = 1;
10175 for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
10176 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
10177 arm_register_names[i]);
10178 if (!valid_p)
10179 {
10180 tdesc_data_cleanup (tdesc_data);
10181 return NULL;
10182 }
10183 }
ff6f572f
DJ
10184 else
10185 have_fpa_registers = 0;
10186
9779414d 10187 feature = tdesc_find_feature (tdesc,
ff6f572f
DJ
10188 "org.gnu.gdb.xscale.iwmmxt");
10189 if (feature != NULL)
10190 {
10191 static const char *const iwmmxt_names[] = {
10192 "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
10193 "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
10194 "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
10195 "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
10196 };
10197
10198 valid_p = 1;
10199 for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
10200 valid_p
10201 &= tdesc_numbered_register (feature, tdesc_data, i,
10202 iwmmxt_names[i - ARM_WR0_REGNUM]);
10203
10204 /* Check for the control registers, but do not fail if they
10205 are missing. */
10206 for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
10207 tdesc_numbered_register (feature, tdesc_data, i,
10208 iwmmxt_names[i - ARM_WR0_REGNUM]);
10209
10210 for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
10211 valid_p
10212 &= tdesc_numbered_register (feature, tdesc_data, i,
10213 iwmmxt_names[i - ARM_WR0_REGNUM]);
10214
10215 if (!valid_p)
10216 {
10217 tdesc_data_cleanup (tdesc_data);
10218 return NULL;
10219 }
a56cc1ce
YQ
10220
10221 have_wmmx_registers = 1;
ff6f572f 10222 }
58d6951d
DJ
10223
10224 /* If we have a VFP unit, check whether the single precision registers
10225 are present. If not, then we will synthesize them as pseudo
10226 registers. */
9779414d 10227 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
10228 "org.gnu.gdb.arm.vfp");
10229 if (feature != NULL)
10230 {
10231 static const char *const vfp_double_names[] = {
10232 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
10233 "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
10234 "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
10235 "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
10236 };
10237
10238 /* Require the double precision registers. There must be either
10239 16 or 32. */
10240 valid_p = 1;
10241 for (i = 0; i < 32; i++)
10242 {
10243 valid_p &= tdesc_numbered_register (feature, tdesc_data,
10244 ARM_D0_REGNUM + i,
10245 vfp_double_names[i]);
10246 if (!valid_p)
10247 break;
10248 }
2b9e5ea6
UW
10249 if (!valid_p && i == 16)
10250 valid_p = 1;
58d6951d 10251
2b9e5ea6
UW
10252 /* Also require FPSCR. */
10253 valid_p &= tdesc_numbered_register (feature, tdesc_data,
10254 ARM_FPSCR_REGNUM, "fpscr");
10255 if (!valid_p)
58d6951d
DJ
10256 {
10257 tdesc_data_cleanup (tdesc_data);
10258 return NULL;
10259 }
10260
10261 if (tdesc_unnumbered_register (feature, "s0") == 0)
10262 have_vfp_pseudos = 1;
10263
330c6ca9 10264 vfp_register_count = i;
58d6951d
DJ
10265
10266 /* If we have VFP, also check for NEON. The architecture allows
10267 NEON without VFP (integer vector operations only), but GDB
10268 does not support that. */
9779414d 10269 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
10270 "org.gnu.gdb.arm.neon");
10271 if (feature != NULL)
10272 {
10273 /* NEON requires 32 double-precision registers. */
10274 if (i != 32)
10275 {
10276 tdesc_data_cleanup (tdesc_data);
10277 return NULL;
10278 }
10279
10280 /* If there are quad registers defined by the stub, use
10281 their type; otherwise (normally) provide them with
10282 the default type. */
10283 if (tdesc_unnumbered_register (feature, "q0") == 0)
10284 have_neon_pseudos = 1;
10285
10286 have_neon = 1;
10287 }
10288 }
123dc839 10289 }
39bbf761 10290
28e97307
DJ
10291 /* If there is already a candidate, use it. */
10292 for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
10293 best_arch != NULL;
10294 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
10295 {
b8926edc
DJ
10296 if (arm_abi != ARM_ABI_AUTO
10297 && arm_abi != gdbarch_tdep (best_arch->gdbarch)->arm_abi)
28e97307
DJ
10298 continue;
10299
b8926edc
DJ
10300 if (fp_model != ARM_FLOAT_AUTO
10301 && fp_model != gdbarch_tdep (best_arch->gdbarch)->fp_model)
28e97307
DJ
10302 continue;
10303
58d6951d
DJ
10304 /* There are various other properties in tdep that we do not
10305 need to check here: those derived from a target description,
10306 since gdbarches with a different target description are
10307 automatically disqualified. */
10308
9779414d
DJ
10309 /* Do check is_m, though, since it might come from the binary. */
10310 if (is_m != gdbarch_tdep (best_arch->gdbarch)->is_m)
10311 continue;
10312
28e97307
DJ
10313 /* Found a match. */
10314 break;
10315 }
97e03143 10316
28e97307 10317 if (best_arch != NULL)
123dc839
DJ
10318 {
10319 if (tdesc_data != NULL)
10320 tdesc_data_cleanup (tdesc_data);
10321 return best_arch->gdbarch;
10322 }
28e97307 10323
8d749320 10324 tdep = XCNEW (struct gdbarch_tdep);
97e03143
RE
10325 gdbarch = gdbarch_alloc (&info, tdep);
10326
28e97307
DJ
10327 /* Record additional information about the architecture we are defining.
10328 These are gdbarch discriminators, like the OSABI. */
10329 tdep->arm_abi = arm_abi;
10330 tdep->fp_model = fp_model;
9779414d 10331 tdep->is_m = is_m;
ff6f572f 10332 tdep->have_fpa_registers = have_fpa_registers;
a56cc1ce 10333 tdep->have_wmmx_registers = have_wmmx_registers;
330c6ca9
YQ
10334 gdb_assert (vfp_register_count == 0
10335 || vfp_register_count == 16
10336 || vfp_register_count == 32);
10337 tdep->vfp_register_count = vfp_register_count;
58d6951d
DJ
10338 tdep->have_vfp_pseudos = have_vfp_pseudos;
10339 tdep->have_neon_pseudos = have_neon_pseudos;
10340 tdep->have_neon = have_neon;
08216dd7 10341
25f8c692
JL
10342 arm_register_g_packet_guesses (gdbarch);
10343
08216dd7 10344 /* Breakpoints. */
9d4fde75 10345 switch (info.byte_order_for_code)
67255d04
RE
10346 {
10347 case BFD_ENDIAN_BIG:
66e810cd
RE
10348 tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
10349 tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
10350 tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
10351 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
10352
67255d04
RE
10353 break;
10354
10355 case BFD_ENDIAN_LITTLE:
66e810cd
RE
10356 tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
10357 tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
10358 tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
10359 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
10360
67255d04
RE
10361 break;
10362
10363 default:
10364 internal_error (__FILE__, __LINE__,
edefbb7c 10365 _("arm_gdbarch_init: bad byte order for float format"));
67255d04
RE
10366 }
10367
d7b486e7
RE
10368 /* On ARM targets char defaults to unsigned. */
10369 set_gdbarch_char_signed (gdbarch, 0);
10370
cca44b1b
JB
10371 /* Note: for displaced stepping, this includes the breakpoint, and one word
10372 of additional scratch space. This setting isn't used for anything beside
10373 displaced stepping at present. */
10374 set_gdbarch_max_insn_length (gdbarch, 4 * DISPLACED_MODIFIED_INSNS);
10375
9df628e0 10376 /* This should be low enough for everything. */
97e03143 10377 tdep->lowest_pc = 0x20;
94c30b78 10378 tdep->jb_pc = -1; /* Longjump support not enabled by default. */
97e03143 10379
7c00367c
MK
10380 /* The default, for both APCS and AAPCS, is to return small
10381 structures in registers. */
10382 tdep->struct_return = reg_struct_return;
10383
2dd604e7 10384 set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
f53f0d0b 10385 set_gdbarch_frame_align (gdbarch, arm_frame_align);
39bbf761 10386
756fe439
DJ
10387 set_gdbarch_write_pc (gdbarch, arm_write_pc);
10388
148754e5 10389 /* Frame handling. */
a262aec2 10390 set_gdbarch_dummy_id (gdbarch, arm_dummy_id);
eb5492fa
DJ
10391 set_gdbarch_unwind_pc (gdbarch, arm_unwind_pc);
10392 set_gdbarch_unwind_sp (gdbarch, arm_unwind_sp);
10393
eb5492fa 10394 frame_base_set_default (gdbarch, &arm_normal_base);
148754e5 10395
34e8f22d 10396 /* Address manipulation. */
34e8f22d
RE
10397 set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
10398
34e8f22d
RE
10399 /* Advance PC across function entry code. */
10400 set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
10401
c9cf6e20
MG
10402 /* Detect whether PC is at a point where the stack has been destroyed. */
10403 set_gdbarch_stack_frame_destroyed_p (gdbarch, arm_stack_frame_destroyed_p);
4024ca99 10404
190dce09
UW
10405 /* Skip trampolines. */
10406 set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
10407
34e8f22d
RE
10408 /* The stack grows downward. */
10409 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
10410
10411 /* Breakpoint manipulation. */
10412 set_gdbarch_breakpoint_from_pc (gdbarch, arm_breakpoint_from_pc);
177321bd
DJ
10413 set_gdbarch_remote_breakpoint_from_pc (gdbarch,
10414 arm_remote_breakpoint_from_pc);
34e8f22d
RE
10415
10416 /* Information about registers, etc. */
34e8f22d
RE
10417 set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
10418 set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
ff6f572f 10419 set_gdbarch_num_regs (gdbarch, ARM_NUM_REGS);
7a5ea0d4 10420 set_gdbarch_register_type (gdbarch, arm_register_type);
54483882 10421 set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);
34e8f22d 10422
ff6f572f
DJ
10423 /* This "info float" is FPA-specific. Use the generic version if we
10424 do not have FPA. */
10425 if (gdbarch_tdep (gdbarch)->have_fpa_registers)
10426 set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
10427
26216b98 10428 /* Internal <-> external register number maps. */
ff6f572f 10429 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
26216b98
AC
10430 set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
10431
34e8f22d
RE
10432 set_gdbarch_register_name (gdbarch, arm_register_name);
10433
10434 /* Returning results. */
2af48f68 10435 set_gdbarch_return_value (gdbarch, arm_return_value);
34e8f22d 10436
03d48a7d
RE
10437 /* Disassembly. */
10438 set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
10439
34e8f22d
RE
10440 /* Minsymbol frobbing. */
10441 set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
10442 set_gdbarch_coff_make_msymbol_special (gdbarch,
10443 arm_coff_make_msymbol_special);
60c5725c 10444 set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
34e8f22d 10445
f9d67f43
DJ
10446 /* Thumb-2 IT block support. */
10447 set_gdbarch_adjust_breakpoint_address (gdbarch,
10448 arm_adjust_breakpoint_address);
10449
0d5de010
DJ
10450 /* Virtual tables. */
10451 set_gdbarch_vbit_in_delta (gdbarch, 1);
10452
97e03143 10453 /* Hook in the ABI-specific overrides, if they have been registered. */
4be87837 10454 gdbarch_init_osabi (info, gdbarch);
97e03143 10455
b39cc962
DJ
10456 dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
10457
eb5492fa 10458 /* Add some default predicates. */
2ae28aa9
YQ
10459 if (is_m)
10460 frame_unwind_append_unwinder (gdbarch, &arm_m_exception_unwind);
a262aec2
DJ
10461 frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
10462 dwarf2_append_unwinders (gdbarch);
0e9e9abd 10463 frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
a262aec2 10464 frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
eb5492fa 10465
97e03143
RE
10466 /* Now we have tuned the configuration, set a few final things,
10467 based on what the OS ABI has told us. */
10468
b8926edc
DJ
10469 /* If the ABI is not otherwise marked, assume the old GNU APCS. EABI
10470 binaries are always marked. */
10471 if (tdep->arm_abi == ARM_ABI_AUTO)
10472 tdep->arm_abi = ARM_ABI_APCS;
10473
e3039479
UW
10474 /* Watchpoints are not steppable. */
10475 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
10476
b8926edc
DJ
10477 /* We used to default to FPA for generic ARM, but almost nobody
10478 uses that now, and we now provide a way for the user to force
10479 the model. So default to the most useful variant. */
10480 if (tdep->fp_model == ARM_FLOAT_AUTO)
10481 tdep->fp_model = ARM_FLOAT_SOFT_FPA;
10482
9df628e0
RE
10483 if (tdep->jb_pc >= 0)
10484 set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
10485
08216dd7 10486 /* Floating point sizes and format. */
8da61cc4 10487 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
b8926edc 10488 if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
08216dd7 10489 {
8da61cc4
DJ
10490 set_gdbarch_double_format
10491 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10492 set_gdbarch_long_double_format
10493 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10494 }
10495 else
10496 {
10497 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
10498 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
08216dd7
RE
10499 }
10500
58d6951d
DJ
10501 if (have_vfp_pseudos)
10502 {
10503 /* NOTE: These are the only pseudo registers used by
10504 the ARM target at the moment. If more are added, a
10505 little more care in numbering will be needed. */
10506
10507 int num_pseudos = 32;
10508 if (have_neon_pseudos)
10509 num_pseudos += 16;
10510 set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
10511 set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
10512 set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
10513 }
10514
123dc839 10515 if (tdesc_data)
58d6951d
DJ
10516 {
10517 set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
10518
9779414d 10519 tdesc_use_registers (gdbarch, tdesc, tdesc_data);
58d6951d
DJ
10520
10521 /* Override tdesc_register_type to adjust the types of VFP
10522 registers for NEON. */
10523 set_gdbarch_register_type (gdbarch, arm_register_type);
10524 }
123dc839
DJ
10525
10526 /* Add standard register aliases. We add aliases even for those
10527 nanes which are used by the current architecture - it's simpler,
10528 and does no harm, since nothing ever lists user registers. */
10529 for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
10530 user_reg_add (gdbarch, arm_register_aliases[i].name,
10531 value_of_arm_user_reg, &arm_register_aliases[i].regnum);
10532
39bbf761
RE
10533 return gdbarch;
10534}
10535
97e03143 10536static void
2af46ca0 10537arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
97e03143 10538{
2af46ca0 10539 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
97e03143
RE
10540
10541 if (tdep == NULL)
10542 return;
10543
edefbb7c 10544 fprintf_unfiltered (file, _("arm_dump_tdep: Lowest pc = 0x%lx"),
97e03143
RE
10545 (unsigned long) tdep->lowest_pc);
10546}
10547
a78f21af
AC
10548extern initialize_file_ftype _initialize_arm_tdep; /* -Wmissing-prototypes */
10549
c906108c 10550void
ed9a39eb 10551_initialize_arm_tdep (void)
c906108c 10552{
bc90b915
FN
10553 struct ui_file *stb;
10554 long length;
26304000 10555 struct cmd_list_element *new_set, *new_show;
53904c9e
AC
10556 const char *setname;
10557 const char *setdesc;
4bd7b427 10558 const char *const *regnames;
bc90b915
FN
10559 int numregs, i, j;
10560 static char *helptext;
edefbb7c
AC
10561 char regdesc[1024], *rdptr = regdesc;
10562 size_t rest = sizeof (regdesc);
085dd6e6 10563
42cf1509 10564 gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
97e03143 10565
60c5725c 10566 arm_objfile_data_key
c1bd65d0 10567 = register_objfile_data_with_cleanup (NULL, arm_objfile_data_free);
60c5725c 10568
0e9e9abd
UW
10569 /* Add ourselves to objfile event chain. */
10570 observer_attach_new_objfile (arm_exidx_new_objfile);
10571 arm_exidx_data_key
10572 = register_objfile_data_with_cleanup (NULL, arm_exidx_data_free);
10573
70f80edf
JT
10574 /* Register an ELF OS ABI sniffer for ARM binaries. */
10575 gdbarch_register_osabi_sniffer (bfd_arch_arm,
10576 bfd_target_elf_flavour,
10577 arm_elf_osabi_sniffer);
10578
9779414d
DJ
10579 /* Initialize the standard target descriptions. */
10580 initialize_tdesc_arm_with_m ();
25f8c692 10581 initialize_tdesc_arm_with_m_fpa_layout ();
3184d3f9 10582 initialize_tdesc_arm_with_m_vfp_d16 ();
ef7e8358
UW
10583 initialize_tdesc_arm_with_iwmmxt ();
10584 initialize_tdesc_arm_with_vfpv2 ();
10585 initialize_tdesc_arm_with_vfpv3 ();
10586 initialize_tdesc_arm_with_neon ();
9779414d 10587
94c30b78 10588 /* Get the number of possible sets of register names defined in opcodes. */
afd7eef0
RE
10589 num_disassembly_options = get_arm_regname_num_options ();
10590
10591 /* Add root prefix command for all "set arm"/"show arm" commands. */
10592 add_prefix_cmd ("arm", no_class, set_arm_command,
edefbb7c 10593 _("Various ARM-specific commands."),
afd7eef0
RE
10594 &setarmcmdlist, "set arm ", 0, &setlist);
10595
10596 add_prefix_cmd ("arm", no_class, show_arm_command,
edefbb7c 10597 _("Various ARM-specific commands."),
afd7eef0 10598 &showarmcmdlist, "show arm ", 0, &showlist);
bc90b915 10599
94c30b78 10600 /* Sync the opcode insn printer with our register viewer. */
bc90b915 10601 parse_arm_disassembler_option ("reg-names-std");
c5aa993b 10602
eefe576e
AC
10603 /* Initialize the array that will be passed to
10604 add_setshow_enum_cmd(). */
8d749320
SM
10605 valid_disassembly_styles = XNEWVEC (const char *,
10606 num_disassembly_options + 1);
afd7eef0 10607 for (i = 0; i < num_disassembly_options; i++)
bc90b915
FN
10608 {
10609 numregs = get_arm_regnames (i, &setname, &setdesc, &regnames);
afd7eef0 10610 valid_disassembly_styles[i] = setname;
edefbb7c
AC
10611 length = snprintf (rdptr, rest, "%s - %s\n", setname, setdesc);
10612 rdptr += length;
10613 rest -= length;
123dc839
DJ
10614 /* When we find the default names, tell the disassembler to use
10615 them. */
bc90b915
FN
10616 if (!strcmp (setname, "std"))
10617 {
afd7eef0 10618 disassembly_style = setname;
bc90b915
FN
10619 set_arm_regname_option (i);
10620 }
10621 }
94c30b78 10622 /* Mark the end of valid options. */
afd7eef0 10623 valid_disassembly_styles[num_disassembly_options] = NULL;
c906108c 10624
edefbb7c
AC
10625 /* Create the help text. */
10626 stb = mem_fileopen ();
10627 fprintf_unfiltered (stb, "%s%s%s",
10628 _("The valid values are:\n"),
10629 regdesc,
10630 _("The default is \"std\"."));
759ef836 10631 helptext = ui_file_xstrdup (stb, NULL);
bc90b915 10632 ui_file_delete (stb);
ed9a39eb 10633
edefbb7c
AC
10634 add_setshow_enum_cmd("disassembler", no_class,
10635 valid_disassembly_styles, &disassembly_style,
10636 _("Set the disassembly style."),
10637 _("Show the disassembly style."),
10638 helptext,
2c5b56ce 10639 set_disassembly_style_sfunc,
0963b4bd
MS
10640 NULL, /* FIXME: i18n: The disassembly style is
10641 \"%s\". */
7376b4c2 10642 &setarmcmdlist, &showarmcmdlist);
edefbb7c
AC
10643
10644 add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
10645 _("Set usage of ARM 32-bit mode."),
10646 _("Show usage of ARM 32-bit mode."),
10647 _("When off, a 26-bit PC will be used."),
2c5b56ce 10648 NULL,
0963b4bd
MS
10649 NULL, /* FIXME: i18n: Usage of ARM 32-bit
10650 mode is %s. */
26304000 10651 &setarmcmdlist, &showarmcmdlist);
c906108c 10652
fd50bc42 10653 /* Add a command to allow the user to force the FPU model. */
edefbb7c
AC
10654 add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
10655 _("Set the floating point type."),
10656 _("Show the floating point type."),
10657 _("auto - Determine the FP typefrom the OS-ABI.\n\
10658softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
10659fpa - FPA co-processor (GCC compiled).\n\
10660softvfp - Software FP with pure-endian doubles.\n\
10661vfp - VFP co-processor."),
edefbb7c 10662 set_fp_model_sfunc, show_fp_model,
7376b4c2 10663 &setarmcmdlist, &showarmcmdlist);
fd50bc42 10664
28e97307
DJ
10665 /* Add a command to allow the user to force the ABI. */
10666 add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
10667 _("Set the ABI."),
10668 _("Show the ABI."),
10669 NULL, arm_set_abi, arm_show_abi,
10670 &setarmcmdlist, &showarmcmdlist);
10671
0428b8f5
DJ
10672 /* Add two commands to allow the user to force the assumed
10673 execution mode. */
10674 add_setshow_enum_cmd ("fallback-mode", class_support,
10675 arm_mode_strings, &arm_fallback_mode_string,
10676 _("Set the mode assumed when symbols are unavailable."),
10677 _("Show the mode assumed when symbols are unavailable."),
10678 NULL, NULL, arm_show_fallback_mode,
10679 &setarmcmdlist, &showarmcmdlist);
10680 add_setshow_enum_cmd ("force-mode", class_support,
10681 arm_mode_strings, &arm_force_mode_string,
10682 _("Set the mode assumed even when symbols are available."),
10683 _("Show the mode assumed even when symbols are available."),
10684 NULL, NULL, arm_show_force_mode,
10685 &setarmcmdlist, &showarmcmdlist);
10686
6529d2dd 10687 /* Debugging flag. */
edefbb7c
AC
10688 add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
10689 _("Set ARM debugging."),
10690 _("Show ARM debugging."),
10691 _("When on, arm-specific debugging is enabled."),
2c5b56ce 10692 NULL,
7915a72c 10693 NULL, /* FIXME: i18n: "ARM debugging is %s. */
26304000 10694 &setdebuglist, &showdebuglist);
c906108c 10695}
72508ac0
PO
10696
10697/* ARM-reversible process record data structures. */
10698
10699#define ARM_INSN_SIZE_BYTES 4
10700#define THUMB_INSN_SIZE_BYTES 2
10701#define THUMB2_INSN_SIZE_BYTES 4
10702
10703
71e396f9
LM
10704/* Position of the bit within a 32-bit ARM instruction
10705 that defines whether the instruction is a load or store. */
72508ac0
PO
10706#define INSN_S_L_BIT_NUM 20
10707
10708#define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
10709 do \
10710 { \
10711 unsigned int reg_len = LENGTH; \
10712 if (reg_len) \
10713 { \
10714 REGS = XNEWVEC (uint32_t, reg_len); \
10715 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
10716 } \
10717 } \
10718 while (0)
10719
10720#define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
10721 do \
10722 { \
10723 unsigned int mem_len = LENGTH; \
10724 if (mem_len) \
10725 { \
10726 MEMS = XNEWVEC (struct arm_mem_r, mem_len); \
10727 memcpy(&MEMS->len, &RECORD_BUF[0], \
10728 sizeof(struct arm_mem_r) * LENGTH); \
10729 } \
10730 } \
10731 while (0)
10732
10733/* Checks whether insn is already recorded or yet to be decoded. (boolean expression). */
10734#define INSN_RECORDED(ARM_RECORD) \
10735 (0 != (ARM_RECORD)->reg_rec_count || 0 != (ARM_RECORD)->mem_rec_count)
10736
10737/* ARM memory record structure. */
10738struct arm_mem_r
10739{
10740 uint32_t len; /* Record length. */
bfbbec00 10741 uint32_t addr; /* Memory address. */
72508ac0
PO
10742};
10743
10744/* ARM instruction record contains opcode of current insn
10745 and execution state (before entry to decode_insn()),
10746 contains list of to-be-modified registers and
10747 memory blocks (on return from decode_insn()). */
10748
10749typedef struct insn_decode_record_t
10750{
10751 struct gdbarch *gdbarch;
10752 struct regcache *regcache;
10753 CORE_ADDR this_addr; /* Address of the insn being decoded. */
10754 uint32_t arm_insn; /* Should accommodate thumb. */
10755 uint32_t cond; /* Condition code. */
10756 uint32_t opcode; /* Insn opcode. */
10757 uint32_t decode; /* Insn decode bits. */
10758 uint32_t mem_rec_count; /* No of mem records. */
10759 uint32_t reg_rec_count; /* No of reg records. */
10760 uint32_t *arm_regs; /* Registers to be saved for this record. */
10761 struct arm_mem_r *arm_mems; /* Memory to be saved for this record. */
10762} insn_decode_record;
10763
10764
10765/* Checks ARM SBZ and SBO mandatory fields. */
10766
10767static int
10768sbo_sbz (uint32_t insn, uint32_t bit_num, uint32_t len, uint32_t sbo)
10769{
10770 uint32_t ones = bits (insn, bit_num - 1, (bit_num -1) + (len - 1));
10771
10772 if (!len)
10773 return 1;
10774
10775 if (!sbo)
10776 ones = ~ones;
10777
10778 while (ones)
10779 {
10780 if (!(ones & sbo))
10781 {
10782 return 0;
10783 }
10784 ones = ones >> 1;
10785 }
10786 return 1;
10787}
10788
c6ec2b30
OJ
10789enum arm_record_result
10790{
10791 ARM_RECORD_SUCCESS = 0,
10792 ARM_RECORD_FAILURE = 1
10793};
10794
72508ac0
PO
10795typedef enum
10796{
10797 ARM_RECORD_STRH=1,
10798 ARM_RECORD_STRD
10799} arm_record_strx_t;
10800
10801typedef enum
10802{
10803 ARM_RECORD=1,
10804 THUMB_RECORD,
10805 THUMB2_RECORD
10806} record_type_t;
10807
10808
10809static int
10810arm_record_strx (insn_decode_record *arm_insn_r, uint32_t *record_buf,
10811 uint32_t *record_buf_mem, arm_record_strx_t str_type)
10812{
10813
10814 struct regcache *reg_cache = arm_insn_r->regcache;
10815 ULONGEST u_regval[2]= {0};
10816
10817 uint32_t reg_src1 = 0, reg_src2 = 0;
10818 uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
10819 uint32_t opcode1 = 0;
10820
10821 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10822 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10823 opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
10824
10825
10826 if (14 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
10827 {
10828 /* 1) Handle misc store, immediate offset. */
10829 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
10830 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
10831 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10832 regcache_raw_read_unsigned (reg_cache, reg_src1,
10833 &u_regval[0]);
10834 if (ARM_PC_REGNUM == reg_src1)
10835 {
10836 /* If R15 was used as Rn, hence current PC+8. */
10837 u_regval[0] = u_regval[0] + 8;
10838 }
10839 offset_8 = (immed_high << 4) | immed_low;
10840 /* Calculate target store address. */
10841 if (14 == arm_insn_r->opcode)
10842 {
10843 tgt_mem_addr = u_regval[0] + offset_8;
10844 }
10845 else
10846 {
10847 tgt_mem_addr = u_regval[0] - offset_8;
10848 }
10849 if (ARM_RECORD_STRH == str_type)
10850 {
10851 record_buf_mem[0] = 2;
10852 record_buf_mem[1] = tgt_mem_addr;
10853 arm_insn_r->mem_rec_count = 1;
10854 }
10855 else if (ARM_RECORD_STRD == str_type)
10856 {
10857 record_buf_mem[0] = 4;
10858 record_buf_mem[1] = tgt_mem_addr;
10859 record_buf_mem[2] = 4;
10860 record_buf_mem[3] = tgt_mem_addr + 4;
10861 arm_insn_r->mem_rec_count = 2;
10862 }
10863 }
10864 else if (12 == arm_insn_r->opcode || 8 == arm_insn_r->opcode)
10865 {
10866 /* 2) Store, register offset. */
10867 /* Get Rm. */
10868 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10869 /* Get Rn. */
10870 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10871 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10872 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10873 if (15 == reg_src2)
10874 {
10875 /* If R15 was used as Rn, hence current PC+8. */
10876 u_regval[0] = u_regval[0] + 8;
10877 }
10878 /* Calculate target store address, Rn +/- Rm, register offset. */
10879 if (12 == arm_insn_r->opcode)
10880 {
10881 tgt_mem_addr = u_regval[0] + u_regval[1];
10882 }
10883 else
10884 {
10885 tgt_mem_addr = u_regval[1] - u_regval[0];
10886 }
10887 if (ARM_RECORD_STRH == str_type)
10888 {
10889 record_buf_mem[0] = 2;
10890 record_buf_mem[1] = tgt_mem_addr;
10891 arm_insn_r->mem_rec_count = 1;
10892 }
10893 else if (ARM_RECORD_STRD == str_type)
10894 {
10895 record_buf_mem[0] = 4;
10896 record_buf_mem[1] = tgt_mem_addr;
10897 record_buf_mem[2] = 4;
10898 record_buf_mem[3] = tgt_mem_addr + 4;
10899 arm_insn_r->mem_rec_count = 2;
10900 }
10901 }
10902 else if (11 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
10903 || 2 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
10904 {
10905 /* 3) Store, immediate pre-indexed. */
10906 /* 5) Store, immediate post-indexed. */
10907 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
10908 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
10909 offset_8 = (immed_high << 4) | immed_low;
10910 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10911 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10912 /* Calculate target store address, Rn +/- Rm, register offset. */
10913 if (15 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
10914 {
10915 tgt_mem_addr = u_regval[0] + offset_8;
10916 }
10917 else
10918 {
10919 tgt_mem_addr = u_regval[0] - offset_8;
10920 }
10921 if (ARM_RECORD_STRH == str_type)
10922 {
10923 record_buf_mem[0] = 2;
10924 record_buf_mem[1] = tgt_mem_addr;
10925 arm_insn_r->mem_rec_count = 1;
10926 }
10927 else if (ARM_RECORD_STRD == str_type)
10928 {
10929 record_buf_mem[0] = 4;
10930 record_buf_mem[1] = tgt_mem_addr;
10931 record_buf_mem[2] = 4;
10932 record_buf_mem[3] = tgt_mem_addr + 4;
10933 arm_insn_r->mem_rec_count = 2;
10934 }
10935 /* Record Rn also as it changes. */
10936 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
10937 arm_insn_r->reg_rec_count = 1;
10938 }
10939 else if (9 == arm_insn_r->opcode || 13 == arm_insn_r->opcode
10940 || 0 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
10941 {
10942 /* 4) Store, register pre-indexed. */
10943 /* 6) Store, register post -indexed. */
10944 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10945 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10946 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10947 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10948 /* Calculate target store address, Rn +/- Rm, register offset. */
10949 if (13 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
10950 {
10951 tgt_mem_addr = u_regval[0] + u_regval[1];
10952 }
10953 else
10954 {
10955 tgt_mem_addr = u_regval[1] - u_regval[0];
10956 }
10957 if (ARM_RECORD_STRH == str_type)
10958 {
10959 record_buf_mem[0] = 2;
10960 record_buf_mem[1] = tgt_mem_addr;
10961 arm_insn_r->mem_rec_count = 1;
10962 }
10963 else if (ARM_RECORD_STRD == str_type)
10964 {
10965 record_buf_mem[0] = 4;
10966 record_buf_mem[1] = tgt_mem_addr;
10967 record_buf_mem[2] = 4;
10968 record_buf_mem[3] = tgt_mem_addr + 4;
10969 arm_insn_r->mem_rec_count = 2;
10970 }
10971 /* Record Rn also as it changes. */
10972 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
10973 arm_insn_r->reg_rec_count = 1;
10974 }
10975 return 0;
10976}
10977
10978/* Handling ARM extension space insns. */
10979
10980static int
10981arm_record_extension_space (insn_decode_record *arm_insn_r)
10982{
10983 uint32_t ret = 0; /* Return value: -1:record failure ; 0:success */
10984 uint32_t opcode1 = 0, opcode2 = 0, insn_op1 = 0;
10985 uint32_t record_buf[8], record_buf_mem[8];
10986 uint32_t reg_src1 = 0;
10987 uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
10988 struct regcache *reg_cache = arm_insn_r->regcache;
10989 ULONGEST u_regval = 0;
10990
10991 gdb_assert (!INSN_RECORDED(arm_insn_r));
10992 /* Handle unconditional insn extension space. */
10993
10994 opcode1 = bits (arm_insn_r->arm_insn, 20, 27);
10995 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
10996 if (arm_insn_r->cond)
10997 {
10998 /* PLD has no affect on architectural state, it just affects
10999 the caches. */
11000 if (5 == ((opcode1 & 0xE0) >> 5))
11001 {
11002 /* BLX(1) */
11003 record_buf[0] = ARM_PS_REGNUM;
11004 record_buf[1] = ARM_LR_REGNUM;
11005 arm_insn_r->reg_rec_count = 2;
11006 }
11007 /* STC2, LDC2, MCR2, MRC2, CDP2: <TBD>, co-processor insn. */
11008 }
11009
11010
11011 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
11012 if (3 == opcode1 && bit (arm_insn_r->arm_insn, 4))
11013 {
11014 ret = -1;
11015 /* Undefined instruction on ARM V5; need to handle if later
11016 versions define it. */
11017 }
11018
11019 opcode1 = bits (arm_insn_r->arm_insn, 24, 27);
11020 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
11021 insn_op1 = bits (arm_insn_r->arm_insn, 20, 23);
11022
11023 /* Handle arithmetic insn extension space. */
11024 if (!opcode1 && 9 == opcode2 && 1 != arm_insn_r->cond
11025 && !INSN_RECORDED(arm_insn_r))
11026 {
11027 /* Handle MLA(S) and MUL(S). */
11028 if (0 <= insn_op1 && 3 >= insn_op1)
11029 {
11030 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11031 record_buf[1] = ARM_PS_REGNUM;
11032 arm_insn_r->reg_rec_count = 2;
11033 }
11034 else if (4 <= insn_op1 && 15 >= insn_op1)
11035 {
11036 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
11037 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11038 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11039 record_buf[2] = ARM_PS_REGNUM;
11040 arm_insn_r->reg_rec_count = 3;
11041 }
11042 }
11043
11044 opcode1 = bits (arm_insn_r->arm_insn, 26, 27);
11045 opcode2 = bits (arm_insn_r->arm_insn, 23, 24);
11046 insn_op1 = bits (arm_insn_r->arm_insn, 21, 22);
11047
11048 /* Handle control insn extension space. */
11049
11050 if (!opcode1 && 2 == opcode2 && !bit (arm_insn_r->arm_insn, 20)
11051 && 1 != arm_insn_r->cond && !INSN_RECORDED(arm_insn_r))
11052 {
11053 if (!bit (arm_insn_r->arm_insn,25))
11054 {
11055 if (!bits (arm_insn_r->arm_insn, 4, 7))
11056 {
11057 if ((0 == insn_op1) || (2 == insn_op1))
11058 {
11059 /* MRS. */
11060 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11061 arm_insn_r->reg_rec_count = 1;
11062 }
11063 else if (1 == insn_op1)
11064 {
11065 /* CSPR is going to be changed. */
11066 record_buf[0] = ARM_PS_REGNUM;
11067 arm_insn_r->reg_rec_count = 1;
11068 }
11069 else if (3 == insn_op1)
11070 {
11071 /* SPSR is going to be changed. */
11072 /* We need to get SPSR value, which is yet to be done. */
11073 printf_unfiltered (_("Process record does not support "
11074 "instruction 0x%0x at address %s.\n"),
11075 arm_insn_r->arm_insn,
11076 paddress (arm_insn_r->gdbarch,
11077 arm_insn_r->this_addr));
11078 return -1;
11079 }
11080 }
11081 else if (1 == bits (arm_insn_r->arm_insn, 4, 7))
11082 {
11083 if (1 == insn_op1)
11084 {
11085 /* BX. */
11086 record_buf[0] = ARM_PS_REGNUM;
11087 arm_insn_r->reg_rec_count = 1;
11088 }
11089 else if (3 == insn_op1)
11090 {
11091 /* CLZ. */
11092 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11093 arm_insn_r->reg_rec_count = 1;
11094 }
11095 }
11096 else if (3 == bits (arm_insn_r->arm_insn, 4, 7))
11097 {
11098 /* BLX. */
11099 record_buf[0] = ARM_PS_REGNUM;
11100 record_buf[1] = ARM_LR_REGNUM;
11101 arm_insn_r->reg_rec_count = 2;
11102 }
11103 else if (5 == bits (arm_insn_r->arm_insn, 4, 7))
11104 {
11105 /* QADD, QSUB, QDADD, QDSUB */
11106 record_buf[0] = ARM_PS_REGNUM;
11107 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11108 arm_insn_r->reg_rec_count = 2;
11109 }
11110 else if (7 == bits (arm_insn_r->arm_insn, 4, 7))
11111 {
11112 /* BKPT. */
11113 record_buf[0] = ARM_PS_REGNUM;
11114 record_buf[1] = ARM_LR_REGNUM;
11115 arm_insn_r->reg_rec_count = 2;
11116
11117 /* Save SPSR also;how? */
11118 printf_unfiltered (_("Process record does not support "
11119 "instruction 0x%0x at address %s.\n"),
11120 arm_insn_r->arm_insn,
11121 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11122 return -1;
11123 }
11124 else if(8 == bits (arm_insn_r->arm_insn, 4, 7)
11125 || 10 == bits (arm_insn_r->arm_insn, 4, 7)
11126 || 12 == bits (arm_insn_r->arm_insn, 4, 7)
11127 || 14 == bits (arm_insn_r->arm_insn, 4, 7)
11128 )
11129 {
11130 if (0 == insn_op1 || 1 == insn_op1)
11131 {
11132 /* SMLA<x><y>, SMLAW<y>, SMULW<y>. */
11133 /* We dont do optimization for SMULW<y> where we
11134 need only Rd. */
11135 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11136 record_buf[1] = ARM_PS_REGNUM;
11137 arm_insn_r->reg_rec_count = 2;
11138 }
11139 else if (2 == insn_op1)
11140 {
11141 /* SMLAL<x><y>. */
11142 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11143 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
11144 arm_insn_r->reg_rec_count = 2;
11145 }
11146 else if (3 == insn_op1)
11147 {
11148 /* SMUL<x><y>. */
11149 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11150 arm_insn_r->reg_rec_count = 1;
11151 }
11152 }
11153 }
11154 else
11155 {
11156 /* MSR : immediate form. */
11157 if (1 == insn_op1)
11158 {
11159 /* CSPR is going to be changed. */
11160 record_buf[0] = ARM_PS_REGNUM;
11161 arm_insn_r->reg_rec_count = 1;
11162 }
11163 else if (3 == insn_op1)
11164 {
11165 /* SPSR is going to be changed. */
11166 /* we need to get SPSR value, which is yet to be done */
11167 printf_unfiltered (_("Process record does not support "
11168 "instruction 0x%0x at address %s.\n"),
11169 arm_insn_r->arm_insn,
11170 paddress (arm_insn_r->gdbarch,
11171 arm_insn_r->this_addr));
11172 return -1;
11173 }
11174 }
11175 }
11176
11177 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
11178 opcode2 = bits (arm_insn_r->arm_insn, 20, 24);
11179 insn_op1 = bits (arm_insn_r->arm_insn, 5, 6);
11180
11181 /* Handle load/store insn extension space. */
11182
11183 if (!opcode1 && bit (arm_insn_r->arm_insn, 7)
11184 && bit (arm_insn_r->arm_insn, 4) && 1 != arm_insn_r->cond
11185 && !INSN_RECORDED(arm_insn_r))
11186 {
11187 /* SWP/SWPB. */
11188 if (0 == insn_op1)
11189 {
11190 /* These insn, changes register and memory as well. */
11191 /* SWP or SWPB insn. */
11192 /* Get memory address given by Rn. */
11193 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11194 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11195 /* SWP insn ?, swaps word. */
11196 if (8 == arm_insn_r->opcode)
11197 {
11198 record_buf_mem[0] = 4;
11199 }
11200 else
11201 {
11202 /* SWPB insn, swaps only byte. */
11203 record_buf_mem[0] = 1;
11204 }
11205 record_buf_mem[1] = u_regval;
11206 arm_insn_r->mem_rec_count = 1;
11207 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11208 arm_insn_r->reg_rec_count = 1;
11209 }
11210 else if (1 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11211 {
11212 /* STRH. */
11213 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11214 ARM_RECORD_STRH);
11215 }
11216 else if (2 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11217 {
11218 /* LDRD. */
11219 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11220 record_buf[1] = record_buf[0] + 1;
11221 arm_insn_r->reg_rec_count = 2;
11222 }
11223 else if (3 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11224 {
11225 /* STRD. */
11226 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11227 ARM_RECORD_STRD);
11228 }
11229 else if (bit (arm_insn_r->arm_insn, 20) && insn_op1 <= 3)
11230 {
11231 /* LDRH, LDRSB, LDRSH. */
11232 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11233 arm_insn_r->reg_rec_count = 1;
11234 }
11235
11236 }
11237
11238 opcode1 = bits (arm_insn_r->arm_insn, 23, 27);
11239 if (24 == opcode1 && bit (arm_insn_r->arm_insn, 21)
11240 && !INSN_RECORDED(arm_insn_r))
11241 {
11242 ret = -1;
11243 /* Handle coprocessor insn extension space. */
11244 }
11245
11246 /* To be done for ARMv5 and later; as of now we return -1. */
11247 if (-1 == ret)
11248 printf_unfiltered (_("Process record does not support instruction x%0x "
11249 "at address %s.\n"),arm_insn_r->arm_insn,
11250 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11251
11252
11253 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11254 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11255
11256 return ret;
11257}
11258
11259/* Handling opcode 000 insns. */
11260
11261static int
11262arm_record_data_proc_misc_ld_str (insn_decode_record *arm_insn_r)
11263{
11264 struct regcache *reg_cache = arm_insn_r->regcache;
11265 uint32_t record_buf[8], record_buf_mem[8];
11266 ULONGEST u_regval[2] = {0};
11267
11268 uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
11269 uint32_t immed_high = 0, immed_low = 0, offset_8 = 0, tgt_mem_addr = 0;
11270 uint32_t opcode1 = 0;
11271
11272 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11273 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11274 opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
11275
11276 /* Data processing insn /multiply insn. */
11277 if (9 == arm_insn_r->decode
11278 && ((4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11279 || (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)))
11280 {
11281 /* Handle multiply instructions. */
11282 /* MLA, MUL, SMLAL, SMULL, UMLAL, UMULL. */
11283 if (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)
11284 {
11285 /* Handle MLA and MUL. */
11286 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11287 record_buf[1] = ARM_PS_REGNUM;
11288 arm_insn_r->reg_rec_count = 2;
11289 }
11290 else if (4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11291 {
11292 /* Handle SMLAL, SMULL, UMLAL, UMULL. */
11293 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11294 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11295 record_buf[2] = ARM_PS_REGNUM;
11296 arm_insn_r->reg_rec_count = 3;
11297 }
11298 }
11299 else if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11300 && (11 == arm_insn_r->decode || 13 == arm_insn_r->decode))
11301 {
11302 /* Handle misc load insns, as 20th bit (L = 1). */
11303 /* LDR insn has a capability to do branching, if
11304 MOV LR, PC is precceded by LDR insn having Rn as R15
11305 in that case, it emulates branch and link insn, and hence we
11306 need to save CSPR and PC as well. I am not sure this is right
11307 place; as opcode = 010 LDR insn make this happen, if R15 was
11308 used. */
11309 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11310 if (15 != reg_dest)
11311 {
11312 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11313 arm_insn_r->reg_rec_count = 1;
11314 }
11315 else
11316 {
11317 record_buf[0] = reg_dest;
11318 record_buf[1] = ARM_PS_REGNUM;
11319 arm_insn_r->reg_rec_count = 2;
11320 }
11321 }
11322 else if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11323 && sbo_sbz (arm_insn_r->arm_insn, 5, 12, 0)
11324 && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
11325 && 2 == bits (arm_insn_r->arm_insn, 20, 21))
11326 {
11327 /* Handle MSR insn. */
11328 if (9 == arm_insn_r->opcode)
11329 {
11330 /* CSPR is going to be changed. */
11331 record_buf[0] = ARM_PS_REGNUM;
11332 arm_insn_r->reg_rec_count = 1;
11333 }
11334 else
11335 {
11336 /* SPSR is going to be changed. */
11337 /* How to read SPSR value? */
11338 printf_unfiltered (_("Process record does not support instruction "
11339 "0x%0x at address %s.\n"),
11340 arm_insn_r->arm_insn,
11341 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11342 return -1;
11343 }
11344 }
11345 else if (9 == arm_insn_r->decode
11346 && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11347 && !bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11348 {
11349 /* Handling SWP, SWPB. */
11350 /* These insn, changes register and memory as well. */
11351 /* SWP or SWPB insn. */
11352
11353 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11354 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11355 /* SWP insn ?, swaps word. */
11356 if (8 == arm_insn_r->opcode)
11357 {
11358 record_buf_mem[0] = 4;
11359 }
11360 else
11361 {
11362 /* SWPB insn, swaps only byte. */
11363 record_buf_mem[0] = 1;
11364 }
11365 record_buf_mem[1] = u_regval[0];
11366 arm_insn_r->mem_rec_count = 1;
11367 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11368 arm_insn_r->reg_rec_count = 1;
11369 }
11370 else if (3 == arm_insn_r->decode && 0x12 == opcode1
11371 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11372 {
11373 /* Handle BLX, branch and link/exchange. */
11374 if (9 == arm_insn_r->opcode)
11375 {
11376 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm,
11377 and R14 stores the return address. */
11378 record_buf[0] = ARM_PS_REGNUM;
11379 record_buf[1] = ARM_LR_REGNUM;
11380 arm_insn_r->reg_rec_count = 2;
11381 }
11382 }
11383 else if (7 == arm_insn_r->decode && 0x12 == opcode1)
11384 {
11385 /* Handle enhanced software breakpoint insn, BKPT. */
11386 /* CPSR is changed to be executed in ARM state, disabling normal
11387 interrupts, entering abort mode. */
11388 /* According to high vector configuration PC is set. */
11389 /* user hit breakpoint and type reverse, in
11390 that case, we need to go back with previous CPSR and
11391 Program Counter. */
11392 record_buf[0] = ARM_PS_REGNUM;
11393 record_buf[1] = ARM_LR_REGNUM;
11394 arm_insn_r->reg_rec_count = 2;
11395
11396 /* Save SPSR also; how? */
11397 printf_unfiltered (_("Process record does not support instruction "
11398 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
11399 paddress (arm_insn_r->gdbarch,
11400 arm_insn_r->this_addr));
11401 return -1;
11402 }
11403 else if (11 == arm_insn_r->decode
11404 && !bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11405 {
11406 /* Handle enhanced store insns and DSP insns (e.g. LDRD). */
11407
11408 /* Handle str(x) insn */
11409 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11410 ARM_RECORD_STRH);
11411 }
11412 else if (1 == arm_insn_r->decode && 0x12 == opcode1
11413 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11414 {
11415 /* Handle BX, branch and link/exchange. */
11416 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm. */
11417 record_buf[0] = ARM_PS_REGNUM;
11418 arm_insn_r->reg_rec_count = 1;
11419 }
11420 else if (1 == arm_insn_r->decode && 0x16 == opcode1
11421 && sbo_sbz (arm_insn_r->arm_insn, 9, 4, 1)
11422 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1))
11423 {
11424 /* Count leading zeros: CLZ. */
11425 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11426 arm_insn_r->reg_rec_count = 1;
11427 }
11428 else if (!bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11429 && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11430 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1)
11431 && sbo_sbz (arm_insn_r->arm_insn, 1, 12, 0)
11432 )
11433 {
11434 /* Handle MRS insn. */
11435 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11436 arm_insn_r->reg_rec_count = 1;
11437 }
11438 else if (arm_insn_r->opcode <= 15)
11439 {
11440 /* Normal data processing insns. */
11441 /* Out of 11 shifter operands mode, all the insn modifies destination
11442 register, which is specified by 13-16 decode. */
11443 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11444 record_buf[1] = ARM_PS_REGNUM;
11445 arm_insn_r->reg_rec_count = 2;
11446 }
11447 else
11448 {
11449 return -1;
11450 }
11451
11452 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11453 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11454 return 0;
11455}
11456
11457/* Handling opcode 001 insns. */
11458
11459static int
11460arm_record_data_proc_imm (insn_decode_record *arm_insn_r)
11461{
11462 uint32_t record_buf[8], record_buf_mem[8];
11463
11464 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11465 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11466
11467 if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11468 && 2 == bits (arm_insn_r->arm_insn, 20, 21)
11469 && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
11470 )
11471 {
11472 /* Handle MSR insn. */
11473 if (9 == arm_insn_r->opcode)
11474 {
11475 /* CSPR is going to be changed. */
11476 record_buf[0] = ARM_PS_REGNUM;
11477 arm_insn_r->reg_rec_count = 1;
11478 }
11479 else
11480 {
11481 /* SPSR is going to be changed. */
11482 }
11483 }
11484 else if (arm_insn_r->opcode <= 15)
11485 {
11486 /* Normal data processing insns. */
11487 /* Out of 11 shifter operands mode, all the insn modifies destination
11488 register, which is specified by 13-16 decode. */
11489 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11490 record_buf[1] = ARM_PS_REGNUM;
11491 arm_insn_r->reg_rec_count = 2;
11492 }
11493 else
11494 {
11495 return -1;
11496 }
11497
11498 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11499 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11500 return 0;
11501}
11502
71e396f9 11503/* Handle ARM mode instructions with opcode 010. */
72508ac0
PO
11504
11505static int
11506arm_record_ld_st_imm_offset (insn_decode_record *arm_insn_r)
11507{
11508 struct regcache *reg_cache = arm_insn_r->regcache;
11509
71e396f9
LM
11510 uint32_t reg_base , reg_dest;
11511 uint32_t offset_12, tgt_mem_addr;
72508ac0 11512 uint32_t record_buf[8], record_buf_mem[8];
71e396f9
LM
11513 unsigned char wback;
11514 ULONGEST u_regval;
72508ac0 11515
71e396f9
LM
11516 /* Calculate wback. */
11517 wback = (bit (arm_insn_r->arm_insn, 24) == 0)
11518 || (bit (arm_insn_r->arm_insn, 21) == 1);
72508ac0 11519
71e396f9
LM
11520 arm_insn_r->reg_rec_count = 0;
11521 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0
PO
11522
11523 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11524 {
71e396f9
LM
11525 /* LDR (immediate), LDR (literal), LDRB (immediate), LDRB (literal), LDRBT
11526 and LDRT. */
11527
72508ac0 11528 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
71e396f9
LM
11529 record_buf[arm_insn_r->reg_rec_count++] = reg_dest;
11530
11531 /* The LDR instruction is capable of doing branching. If MOV LR, PC
11532 preceeds a LDR instruction having R15 as reg_base, it
11533 emulates a branch and link instruction, and hence we need to save
11534 CPSR and PC as well. */
11535 if (ARM_PC_REGNUM == reg_dest)
11536 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
11537
11538 /* If wback is true, also save the base register, which is going to be
11539 written to. */
11540 if (wback)
11541 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
11542 }
11543 else
11544 {
71e396f9
LM
11545 /* STR (immediate), STRB (immediate), STRBT and STRT. */
11546
72508ac0 11547 offset_12 = bits (arm_insn_r->arm_insn, 0, 11);
71e396f9
LM
11548 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
11549
11550 /* Handle bit U. */
72508ac0 11551 if (bit (arm_insn_r->arm_insn, 23))
71e396f9
LM
11552 {
11553 /* U == 1: Add the offset. */
11554 tgt_mem_addr = (uint32_t) u_regval + offset_12;
11555 }
72508ac0 11556 else
71e396f9
LM
11557 {
11558 /* U == 0: subtract the offset. */
11559 tgt_mem_addr = (uint32_t) u_regval - offset_12;
11560 }
11561
11562 /* Bit 22 tells us whether the store instruction writes 1 byte or 4
11563 bytes. */
11564 if (bit (arm_insn_r->arm_insn, 22))
11565 {
11566 /* STRB and STRBT: 1 byte. */
11567 record_buf_mem[0] = 1;
11568 }
11569 else
11570 {
11571 /* STR and STRT: 4 bytes. */
11572 record_buf_mem[0] = 4;
11573 }
11574
11575 /* Handle bit P. */
11576 if (bit (arm_insn_r->arm_insn, 24))
11577 record_buf_mem[1] = tgt_mem_addr;
11578 else
11579 record_buf_mem[1] = (uint32_t) u_regval;
72508ac0 11580
72508ac0
PO
11581 arm_insn_r->mem_rec_count = 1;
11582
71e396f9
LM
11583 /* If wback is true, also save the base register, which is going to be
11584 written to. */
11585 if (wback)
11586 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
11587 }
11588
11589 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11590 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11591 return 0;
11592}
11593
11594/* Handling opcode 011 insns. */
11595
11596static int
11597arm_record_ld_st_reg_offset (insn_decode_record *arm_insn_r)
11598{
11599 struct regcache *reg_cache = arm_insn_r->regcache;
11600
11601 uint32_t shift_imm = 0;
11602 uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
11603 uint32_t offset_12 = 0, tgt_mem_addr = 0;
11604 uint32_t record_buf[8], record_buf_mem[8];
11605
11606 LONGEST s_word;
11607 ULONGEST u_regval[2];
11608
11609 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11610 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11611
11612 /* Handle enhanced store insns and LDRD DSP insn,
11613 order begins according to addressing modes for store insns
11614 STRH insn. */
11615
11616 /* LDR or STR? */
11617 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11618 {
11619 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11620 /* LDR insn has a capability to do branching, if
11621 MOV LR, PC is precedded by LDR insn having Rn as R15
11622 in that case, it emulates branch and link insn, and hence we
11623 need to save CSPR and PC as well. */
11624 if (15 != reg_dest)
11625 {
11626 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11627 arm_insn_r->reg_rec_count = 1;
11628 }
11629 else
11630 {
11631 record_buf[0] = reg_dest;
11632 record_buf[1] = ARM_PS_REGNUM;
11633 arm_insn_r->reg_rec_count = 2;
11634 }
11635 }
11636 else
11637 {
11638 if (! bits (arm_insn_r->arm_insn, 4, 11))
11639 {
11640 /* Store insn, register offset and register pre-indexed,
11641 register post-indexed. */
11642 /* Get Rm. */
11643 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11644 /* Get Rn. */
11645 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11646 regcache_raw_read_unsigned (reg_cache, reg_src1
11647 , &u_regval[0]);
11648 regcache_raw_read_unsigned (reg_cache, reg_src2
11649 , &u_regval[1]);
11650 if (15 == reg_src2)
11651 {
11652 /* If R15 was used as Rn, hence current PC+8. */
11653 /* Pre-indexed mode doesnt reach here ; illegal insn. */
11654 u_regval[0] = u_regval[0] + 8;
11655 }
11656 /* Calculate target store address, Rn +/- Rm, register offset. */
11657 /* U == 1. */
11658 if (bit (arm_insn_r->arm_insn, 23))
11659 {
11660 tgt_mem_addr = u_regval[0] + u_regval[1];
11661 }
11662 else
11663 {
11664 tgt_mem_addr = u_regval[1] - u_regval[0];
11665 }
11666
11667 switch (arm_insn_r->opcode)
11668 {
11669 /* STR. */
11670 case 8:
11671 case 12:
11672 /* STR. */
11673 case 9:
11674 case 13:
11675 /* STRT. */
11676 case 1:
11677 case 5:
11678 /* STR. */
11679 case 0:
11680 case 4:
11681 record_buf_mem[0] = 4;
11682 break;
11683
11684 /* STRB. */
11685 case 10:
11686 case 14:
11687 /* STRB. */
11688 case 11:
11689 case 15:
11690 /* STRBT. */
11691 case 3:
11692 case 7:
11693 /* STRB. */
11694 case 2:
11695 case 6:
11696 record_buf_mem[0] = 1;
11697 break;
11698
11699 default:
11700 gdb_assert_not_reached ("no decoding pattern found");
11701 break;
11702 }
11703 record_buf_mem[1] = tgt_mem_addr;
11704 arm_insn_r->mem_rec_count = 1;
11705
11706 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11707 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11708 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11709 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11710 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11711 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11712 )
11713 {
11714 /* Rn is going to be changed in pre-indexed mode and
11715 post-indexed mode as well. */
11716 record_buf[0] = reg_src2;
11717 arm_insn_r->reg_rec_count = 1;
11718 }
11719 }
11720 else
11721 {
11722 /* Store insn, scaled register offset; scaled pre-indexed. */
11723 offset_12 = bits (arm_insn_r->arm_insn, 5, 6);
11724 /* Get Rm. */
11725 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11726 /* Get Rn. */
11727 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11728 /* Get shift_imm. */
11729 shift_imm = bits (arm_insn_r->arm_insn, 7, 11);
11730 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11731 regcache_raw_read_signed (reg_cache, reg_src1, &s_word);
11732 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11733 /* Offset_12 used as shift. */
11734 switch (offset_12)
11735 {
11736 case 0:
11737 /* Offset_12 used as index. */
11738 offset_12 = u_regval[0] << shift_imm;
11739 break;
11740
11741 case 1:
11742 offset_12 = (!shift_imm)?0:u_regval[0] >> shift_imm;
11743 break;
11744
11745 case 2:
11746 if (!shift_imm)
11747 {
11748 if (bit (u_regval[0], 31))
11749 {
11750 offset_12 = 0xFFFFFFFF;
11751 }
11752 else
11753 {
11754 offset_12 = 0;
11755 }
11756 }
11757 else
11758 {
11759 /* This is arithmetic shift. */
11760 offset_12 = s_word >> shift_imm;
11761 }
11762 break;
11763
11764 case 3:
11765 if (!shift_imm)
11766 {
11767 regcache_raw_read_unsigned (reg_cache, ARM_PS_REGNUM,
11768 &u_regval[1]);
11769 /* Get C flag value and shift it by 31. */
11770 offset_12 = (((bit (u_regval[1], 29)) << 31) \
11771 | (u_regval[0]) >> 1);
11772 }
11773 else
11774 {
11775 offset_12 = (u_regval[0] >> shift_imm) \
11776 | (u_regval[0] <<
11777 (sizeof(uint32_t) - shift_imm));
11778 }
11779 break;
11780
11781 default:
11782 gdb_assert_not_reached ("no decoding pattern found");
11783 break;
11784 }
11785
11786 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11787 /* bit U set. */
11788 if (bit (arm_insn_r->arm_insn, 23))
11789 {
11790 tgt_mem_addr = u_regval[1] + offset_12;
11791 }
11792 else
11793 {
11794 tgt_mem_addr = u_regval[1] - offset_12;
11795 }
11796
11797 switch (arm_insn_r->opcode)
11798 {
11799 /* STR. */
11800 case 8:
11801 case 12:
11802 /* STR. */
11803 case 9:
11804 case 13:
11805 /* STRT. */
11806 case 1:
11807 case 5:
11808 /* STR. */
11809 case 0:
11810 case 4:
11811 record_buf_mem[0] = 4;
11812 break;
11813
11814 /* STRB. */
11815 case 10:
11816 case 14:
11817 /* STRB. */
11818 case 11:
11819 case 15:
11820 /* STRBT. */
11821 case 3:
11822 case 7:
11823 /* STRB. */
11824 case 2:
11825 case 6:
11826 record_buf_mem[0] = 1;
11827 break;
11828
11829 default:
11830 gdb_assert_not_reached ("no decoding pattern found");
11831 break;
11832 }
11833 record_buf_mem[1] = tgt_mem_addr;
11834 arm_insn_r->mem_rec_count = 1;
11835
11836 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11837 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11838 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11839 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11840 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11841 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11842 )
11843 {
11844 /* Rn is going to be changed in register scaled pre-indexed
11845 mode,and scaled post indexed mode. */
11846 record_buf[0] = reg_src2;
11847 arm_insn_r->reg_rec_count = 1;
11848 }
11849 }
11850 }
11851
11852 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11853 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11854 return 0;
11855}
11856
71e396f9 11857/* Handle ARM mode instructions with opcode 100. */
72508ac0
PO
11858
11859static int
11860arm_record_ld_st_multiple (insn_decode_record *arm_insn_r)
11861{
11862 struct regcache *reg_cache = arm_insn_r->regcache;
71e396f9
LM
11863 uint32_t register_count = 0, register_bits;
11864 uint32_t reg_base, addr_mode;
72508ac0 11865 uint32_t record_buf[24], record_buf_mem[48];
71e396f9
LM
11866 uint32_t wback;
11867 ULONGEST u_regval;
72508ac0 11868
71e396f9
LM
11869 /* Fetch the list of registers. */
11870 register_bits = bits (arm_insn_r->arm_insn, 0, 15);
11871 arm_insn_r->reg_rec_count = 0;
11872
11873 /* Fetch the base register that contains the address we are loading data
11874 to. */
11875 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0 11876
71e396f9
LM
11877 /* Calculate wback. */
11878 wback = (bit (arm_insn_r->arm_insn, 21) == 1);
72508ac0
PO
11879
11880 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11881 {
71e396f9 11882 /* LDM/LDMIA/LDMFD, LDMDA/LDMFA, LDMDB and LDMIB. */
72508ac0 11883
71e396f9 11884 /* Find out which registers are going to be loaded from memory. */
72508ac0 11885 while (register_bits)
71e396f9
LM
11886 {
11887 if (register_bits & 0x00000001)
11888 record_buf[arm_insn_r->reg_rec_count++] = register_count;
11889 register_bits = register_bits >> 1;
11890 register_count++;
11891 }
72508ac0 11892
71e396f9
LM
11893
11894 /* If wback is true, also save the base register, which is going to be
11895 written to. */
11896 if (wback)
11897 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
11898
11899 /* Save the CPSR register. */
11900 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
72508ac0
PO
11901 }
11902 else
11903 {
71e396f9 11904 /* STM (STMIA, STMEA), STMDA (STMED), STMDB (STMFD) and STMIB (STMFA). */
72508ac0 11905
71e396f9
LM
11906 addr_mode = bits (arm_insn_r->arm_insn, 23, 24);
11907
11908 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
11909
11910 /* Find out how many registers are going to be stored to memory. */
72508ac0 11911 while (register_bits)
71e396f9
LM
11912 {
11913 if (register_bits & 0x00000001)
11914 register_count++;
11915 register_bits = register_bits >> 1;
11916 }
72508ac0
PO
11917
11918 switch (addr_mode)
71e396f9
LM
11919 {
11920 /* STMDA (STMED): Decrement after. */
11921 case 0:
11922 record_buf_mem[1] = (uint32_t) u_regval
11923 - register_count * INT_REGISTER_SIZE + 4;
11924 break;
11925 /* STM (STMIA, STMEA): Increment after. */
11926 case 1:
11927 record_buf_mem[1] = (uint32_t) u_regval;
11928 break;
11929 /* STMDB (STMFD): Decrement before. */
11930 case 2:
11931 record_buf_mem[1] = (uint32_t) u_regval
11932 - register_count * INT_REGISTER_SIZE;
11933 break;
11934 /* STMIB (STMFA): Increment before. */
11935 case 3:
11936 record_buf_mem[1] = (uint32_t) u_regval + INT_REGISTER_SIZE;
11937 break;
11938 default:
11939 gdb_assert_not_reached ("no decoding pattern found");
11940 break;
11941 }
72508ac0 11942
71e396f9
LM
11943 record_buf_mem[0] = register_count * INT_REGISTER_SIZE;
11944 arm_insn_r->mem_rec_count = 1;
11945
11946 /* If wback is true, also save the base register, which is going to be
11947 written to. */
11948 if (wback)
11949 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
11950 }
11951
11952 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11953 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11954 return 0;
11955}
11956
11957/* Handling opcode 101 insns. */
11958
11959static int
11960arm_record_b_bl (insn_decode_record *arm_insn_r)
11961{
11962 uint32_t record_buf[8];
11963
11964 /* Handle B, BL, BLX(1) insns. */
11965 /* B simply branches so we do nothing here. */
11966 /* Note: BLX(1) doesnt fall here but instead it falls into
11967 extension space. */
11968 if (bit (arm_insn_r->arm_insn, 24))
11969 {
11970 record_buf[0] = ARM_LR_REGNUM;
11971 arm_insn_r->reg_rec_count = 1;
11972 }
11973
11974 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11975
11976 return 0;
11977}
11978
11979/* Handling opcode 110 insns. */
11980
11981static int
c6ec2b30 11982arm_record_unsupported_insn (insn_decode_record *arm_insn_r)
72508ac0
PO
11983{
11984 printf_unfiltered (_("Process record does not support instruction "
11985 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
11986 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11987
11988 return -1;
11989}
11990
5a578da5
OJ
11991/* Record handler for vector data transfer instructions. */
11992
11993static int
11994arm_record_vdata_transfer_insn (insn_decode_record *arm_insn_r)
11995{
11996 uint32_t bits_a, bit_c, bit_l, reg_t, reg_v;
11997 uint32_t record_buf[4];
11998
11999 const int num_regs = gdbarch_num_regs (arm_insn_r->gdbarch);
12000 reg_t = bits (arm_insn_r->arm_insn, 12, 15);
12001 reg_v = bits (arm_insn_r->arm_insn, 21, 23);
12002 bits_a = bits (arm_insn_r->arm_insn, 21, 23);
12003 bit_l = bit (arm_insn_r->arm_insn, 20);
12004 bit_c = bit (arm_insn_r->arm_insn, 8);
12005
12006 /* Handle VMOV instruction. */
12007 if (bit_l && bit_c)
12008 {
12009 record_buf[0] = reg_t;
12010 arm_insn_r->reg_rec_count = 1;
12011 }
12012 else if (bit_l && !bit_c)
12013 {
12014 /* Handle VMOV instruction. */
12015 if (bits_a == 0x00)
12016 {
12017 if (bit (arm_insn_r->arm_insn, 20))
12018 record_buf[0] = reg_t;
12019 else
12020 record_buf[0] = num_regs + (bit (arm_insn_r->arm_insn, 7) |
12021 (reg_v << 1));
12022
12023 arm_insn_r->reg_rec_count = 1;
12024 }
12025 /* Handle VMRS instruction. */
12026 else if (bits_a == 0x07)
12027 {
12028 if (reg_t == 15)
12029 reg_t = ARM_PS_REGNUM;
12030
12031 record_buf[0] = reg_t;
12032 arm_insn_r->reg_rec_count = 1;
12033 }
12034 }
12035 else if (!bit_l && !bit_c)
12036 {
12037 /* Handle VMOV instruction. */
12038 if (bits_a == 0x00)
12039 {
12040 if (bit (arm_insn_r->arm_insn, 20))
12041 record_buf[0] = reg_t;
12042 else
12043 record_buf[0] = num_regs + (bit (arm_insn_r->arm_insn, 7) |
12044 (reg_v << 1));
12045
12046 arm_insn_r->reg_rec_count = 1;
12047 }
12048 /* Handle VMSR instruction. */
12049 else if (bits_a == 0x07)
12050 {
12051 record_buf[0] = ARM_FPSCR_REGNUM;
12052 arm_insn_r->reg_rec_count = 1;
12053 }
12054 }
12055 else if (!bit_l && bit_c)
12056 {
12057 /* Handle VMOV instruction. */
12058 if (!(bits_a & 0x04))
12059 {
12060 record_buf[0] = (reg_v | (bit (arm_insn_r->arm_insn, 7) << 4))
12061 + ARM_D0_REGNUM;
12062 arm_insn_r->reg_rec_count = 1;
12063 }
12064 /* Handle VDUP instruction. */
12065 else
12066 {
12067 if (bit (arm_insn_r->arm_insn, 21))
12068 {
12069 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
12070 record_buf[0] = reg_v + ARM_D0_REGNUM;
12071 record_buf[1] = reg_v + ARM_D0_REGNUM + 1;
12072 arm_insn_r->reg_rec_count = 2;
12073 }
12074 else
12075 {
12076 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
12077 record_buf[0] = reg_v + ARM_D0_REGNUM;
12078 arm_insn_r->reg_rec_count = 1;
12079 }
12080 }
12081 }
12082
12083 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12084 return 0;
12085}
12086
f20f80dd
OJ
12087/* Record handler for extension register load/store instructions. */
12088
12089static int
12090arm_record_exreg_ld_st_insn (insn_decode_record *arm_insn_r)
12091{
12092 uint32_t opcode, single_reg;
12093 uint8_t op_vldm_vstm;
12094 uint32_t record_buf[8], record_buf_mem[128];
12095 ULONGEST u_regval = 0;
12096
12097 struct regcache *reg_cache = arm_insn_r->regcache;
12098 const int num_regs = gdbarch_num_regs (arm_insn_r->gdbarch);
12099
12100 opcode = bits (arm_insn_r->arm_insn, 20, 24);
12101 single_reg = bit (arm_insn_r->arm_insn, 8);
12102 op_vldm_vstm = opcode & 0x1b;
12103
12104 /* Handle VMOV instructions. */
12105 if ((opcode & 0x1e) == 0x04)
12106 {
12107 if (bit (arm_insn_r->arm_insn, 4))
12108 {
12109 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12110 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
12111 arm_insn_r->reg_rec_count = 2;
12112 }
12113 else
12114 {
12115 uint8_t reg_m = (bits (arm_insn_r->arm_insn, 0, 3) << 1)
12116 | bit (arm_insn_r->arm_insn, 5);
12117
12118 if (!single_reg)
12119 {
12120 record_buf[0] = num_regs + reg_m;
12121 record_buf[1] = num_regs + reg_m + 1;
12122 arm_insn_r->reg_rec_count = 2;
12123 }
12124 else
12125 {
12126 record_buf[0] = reg_m + ARM_D0_REGNUM;
12127 arm_insn_r->reg_rec_count = 1;
12128 }
12129 }
12130 }
12131 /* Handle VSTM and VPUSH instructions. */
12132 else if (op_vldm_vstm == 0x08 || op_vldm_vstm == 0x0a
12133 || op_vldm_vstm == 0x12)
12134 {
12135 uint32_t start_address, reg_rn, imm_off32, imm_off8, memory_count;
12136 uint32_t memory_index = 0;
12137
12138 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
12139 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12140 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
12141 imm_off32 = imm_off8 << 24;
12142 memory_count = imm_off8;
12143
12144 if (bit (arm_insn_r->arm_insn, 23))
12145 start_address = u_regval;
12146 else
12147 start_address = u_regval - imm_off32;
12148
12149 if (bit (arm_insn_r->arm_insn, 21))
12150 {
12151 record_buf[0] = reg_rn;
12152 arm_insn_r->reg_rec_count = 1;
12153 }
12154
12155 while (memory_count > 0)
12156 {
12157 if (!single_reg)
12158 {
12159 record_buf_mem[memory_index] = start_address;
12160 record_buf_mem[memory_index + 1] = 4;
12161 start_address = start_address + 4;
12162 memory_index = memory_index + 2;
12163 }
12164 else
12165 {
12166 record_buf_mem[memory_index] = start_address;
12167 record_buf_mem[memory_index + 1] = 4;
12168 record_buf_mem[memory_index + 2] = start_address + 4;
12169 record_buf_mem[memory_index + 3] = 4;
12170 start_address = start_address + 8;
12171 memory_index = memory_index + 4;
12172 }
12173 memory_count--;
12174 }
12175 arm_insn_r->mem_rec_count = (memory_index >> 1);
12176 }
12177 /* Handle VLDM instructions. */
12178 else if (op_vldm_vstm == 0x09 || op_vldm_vstm == 0x0b
12179 || op_vldm_vstm == 0x13)
12180 {
12181 uint32_t reg_count, reg_vd;
12182 uint32_t reg_index = 0;
12183
12184 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12185 reg_count = bits (arm_insn_r->arm_insn, 0, 7);
12186
12187 if (single_reg)
12188 reg_vd = reg_vd | (bit (arm_insn_r->arm_insn, 22) << 4);
12189 else
12190 reg_vd = (reg_vd << 1) | bit (arm_insn_r->arm_insn, 22);
12191
12192 if (bit (arm_insn_r->arm_insn, 21))
12193 record_buf[reg_index++] = bits (arm_insn_r->arm_insn, 16, 19);
12194
12195 while (reg_count > 0)
12196 {
12197 if (single_reg)
12198 record_buf[reg_index++] = num_regs + reg_vd + reg_count - 1;
12199 else
12200 record_buf[reg_index++] = ARM_D0_REGNUM + reg_vd + reg_count - 1;
12201
12202 reg_count--;
12203 }
12204 arm_insn_r->reg_rec_count = reg_index;
12205 }
12206 /* VSTR Vector store register. */
12207 else if ((opcode & 0x13) == 0x10)
12208 {
12209 uint32_t start_address, reg_rn, imm_off32, imm_off8, memory_count;
12210 uint32_t memory_index = 0;
12211
12212 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
12213 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12214 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
12215 imm_off32 = imm_off8 << 24;
12216 memory_count = imm_off8;
12217
12218 if (bit (arm_insn_r->arm_insn, 23))
12219 start_address = u_regval + imm_off32;
12220 else
12221 start_address = u_regval - imm_off32;
12222
12223 if (single_reg)
12224 {
12225 record_buf_mem[memory_index] = start_address;
12226 record_buf_mem[memory_index + 1] = 4;
12227 arm_insn_r->mem_rec_count = 1;
12228 }
12229 else
12230 {
12231 record_buf_mem[memory_index] = start_address;
12232 record_buf_mem[memory_index + 1] = 4;
12233 record_buf_mem[memory_index + 2] = start_address + 4;
12234 record_buf_mem[memory_index + 3] = 4;
12235 arm_insn_r->mem_rec_count = 2;
12236 }
12237 }
12238 /* VLDR Vector load register. */
12239 else if ((opcode & 0x13) == 0x11)
12240 {
12241 uint32_t reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12242
12243 if (!single_reg)
12244 {
12245 reg_vd = reg_vd | (bit (arm_insn_r->arm_insn, 22) << 4);
12246 record_buf[0] = ARM_D0_REGNUM + reg_vd;
12247 }
12248 else
12249 {
12250 reg_vd = (reg_vd << 1) | bit (arm_insn_r->arm_insn, 22);
12251 record_buf[0] = num_regs + reg_vd;
12252 }
12253 arm_insn_r->reg_rec_count = 1;
12254 }
12255
12256 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12257 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12258 return 0;
12259}
12260
851f26ae
OJ
12261/* Record handler for arm/thumb mode VFP data processing instructions. */
12262
12263static int
12264arm_record_vfp_data_proc_insn (insn_decode_record *arm_insn_r)
12265{
12266 uint32_t opc1, opc2, opc3, dp_op_sz, bit_d, reg_vd;
12267 uint32_t record_buf[4];
12268 enum insn_types {INSN_T0, INSN_T1, INSN_T2, INSN_T3, INSN_INV};
12269 enum insn_types curr_insn_type = INSN_INV;
12270
12271 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12272 opc1 = bits (arm_insn_r->arm_insn, 20, 23);
12273 opc2 = bits (arm_insn_r->arm_insn, 16, 19);
12274 opc3 = bits (arm_insn_r->arm_insn, 6, 7);
12275 dp_op_sz = bit (arm_insn_r->arm_insn, 8);
12276 bit_d = bit (arm_insn_r->arm_insn, 22);
12277 opc1 = opc1 & 0x04;
12278
12279 /* Handle VMLA, VMLS. */
12280 if (opc1 == 0x00)
12281 {
12282 if (bit (arm_insn_r->arm_insn, 10))
12283 {
12284 if (bit (arm_insn_r->arm_insn, 6))
12285 curr_insn_type = INSN_T0;
12286 else
12287 curr_insn_type = INSN_T1;
12288 }
12289 else
12290 {
12291 if (dp_op_sz)
12292 curr_insn_type = INSN_T1;
12293 else
12294 curr_insn_type = INSN_T2;
12295 }
12296 }
12297 /* Handle VNMLA, VNMLS, VNMUL. */
12298 else if (opc1 == 0x01)
12299 {
12300 if (dp_op_sz)
12301 curr_insn_type = INSN_T1;
12302 else
12303 curr_insn_type = INSN_T2;
12304 }
12305 /* Handle VMUL. */
12306 else if (opc1 == 0x02 && !(opc3 & 0x01))
12307 {
12308 if (bit (arm_insn_r->arm_insn, 10))
12309 {
12310 if (bit (arm_insn_r->arm_insn, 6))
12311 curr_insn_type = INSN_T0;
12312 else
12313 curr_insn_type = INSN_T1;
12314 }
12315 else
12316 {
12317 if (dp_op_sz)
12318 curr_insn_type = INSN_T1;
12319 else
12320 curr_insn_type = INSN_T2;
12321 }
12322 }
12323 /* Handle VADD, VSUB. */
12324 else if (opc1 == 0x03)
12325 {
12326 if (!bit (arm_insn_r->arm_insn, 9))
12327 {
12328 if (bit (arm_insn_r->arm_insn, 6))
12329 curr_insn_type = INSN_T0;
12330 else
12331 curr_insn_type = INSN_T1;
12332 }
12333 else
12334 {
12335 if (dp_op_sz)
12336 curr_insn_type = INSN_T1;
12337 else
12338 curr_insn_type = INSN_T2;
12339 }
12340 }
12341 /* Handle VDIV. */
12342 else if (opc1 == 0x0b)
12343 {
12344 if (dp_op_sz)
12345 curr_insn_type = INSN_T1;
12346 else
12347 curr_insn_type = INSN_T2;
12348 }
12349 /* Handle all other vfp data processing instructions. */
12350 else if (opc1 == 0x0b)
12351 {
12352 /* Handle VMOV. */
12353 if (!(opc3 & 0x01) || (opc2 == 0x00 && opc3 == 0x01))
12354 {
12355 if (bit (arm_insn_r->arm_insn, 4))
12356 {
12357 if (bit (arm_insn_r->arm_insn, 6))
12358 curr_insn_type = INSN_T0;
12359 else
12360 curr_insn_type = INSN_T1;
12361 }
12362 else
12363 {
12364 if (dp_op_sz)
12365 curr_insn_type = INSN_T1;
12366 else
12367 curr_insn_type = INSN_T2;
12368 }
12369 }
12370 /* Handle VNEG and VABS. */
12371 else if ((opc2 == 0x01 && opc3 == 0x01)
12372 || (opc2 == 0x00 && opc3 == 0x03))
12373 {
12374 if (!bit (arm_insn_r->arm_insn, 11))
12375 {
12376 if (bit (arm_insn_r->arm_insn, 6))
12377 curr_insn_type = INSN_T0;
12378 else
12379 curr_insn_type = INSN_T1;
12380 }
12381 else
12382 {
12383 if (dp_op_sz)
12384 curr_insn_type = INSN_T1;
12385 else
12386 curr_insn_type = INSN_T2;
12387 }
12388 }
12389 /* Handle VSQRT. */
12390 else if (opc2 == 0x01 && opc3 == 0x03)
12391 {
12392 if (dp_op_sz)
12393 curr_insn_type = INSN_T1;
12394 else
12395 curr_insn_type = INSN_T2;
12396 }
12397 /* Handle VCVT. */
12398 else if (opc2 == 0x07 && opc3 == 0x03)
12399 {
12400 if (!dp_op_sz)
12401 curr_insn_type = INSN_T1;
12402 else
12403 curr_insn_type = INSN_T2;
12404 }
12405 else if (opc3 & 0x01)
12406 {
12407 /* Handle VCVT. */
12408 if ((opc2 == 0x08) || (opc2 & 0x0e) == 0x0c)
12409 {
12410 if (!bit (arm_insn_r->arm_insn, 18))
12411 curr_insn_type = INSN_T2;
12412 else
12413 {
12414 if (dp_op_sz)
12415 curr_insn_type = INSN_T1;
12416 else
12417 curr_insn_type = INSN_T2;
12418 }
12419 }
12420 /* Handle VCVT. */
12421 else if ((opc2 & 0x0e) == 0x0a || (opc2 & 0x0e) == 0x0e)
12422 {
12423 if (dp_op_sz)
12424 curr_insn_type = INSN_T1;
12425 else
12426 curr_insn_type = INSN_T2;
12427 }
12428 /* Handle VCVTB, VCVTT. */
12429 else if ((opc2 & 0x0e) == 0x02)
12430 curr_insn_type = INSN_T2;
12431 /* Handle VCMP, VCMPE. */
12432 else if ((opc2 & 0x0e) == 0x04)
12433 curr_insn_type = INSN_T3;
12434 }
12435 }
12436
12437 switch (curr_insn_type)
12438 {
12439 case INSN_T0:
12440 reg_vd = reg_vd | (bit_d << 4);
12441 record_buf[0] = reg_vd + ARM_D0_REGNUM;
12442 record_buf[1] = reg_vd + ARM_D0_REGNUM + 1;
12443 arm_insn_r->reg_rec_count = 2;
12444 break;
12445
12446 case INSN_T1:
12447 reg_vd = reg_vd | (bit_d << 4);
12448 record_buf[0] = reg_vd + ARM_D0_REGNUM;
12449 arm_insn_r->reg_rec_count = 1;
12450 break;
12451
12452 case INSN_T2:
12453 reg_vd = (reg_vd << 1) | bit_d;
12454 record_buf[0] = reg_vd + ARM_D0_REGNUM;
12455 arm_insn_r->reg_rec_count = 1;
12456 break;
12457
12458 case INSN_T3:
12459 record_buf[0] = ARM_FPSCR_REGNUM;
12460 arm_insn_r->reg_rec_count = 1;
12461 break;
12462
12463 default:
12464 gdb_assert_not_reached ("no decoding pattern found");
12465 break;
12466 }
12467
12468 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12469 return 0;
12470}
12471
60cc5e93
OJ
12472/* Handling opcode 110 insns. */
12473
12474static int
12475arm_record_asimd_vfp_coproc (insn_decode_record *arm_insn_r)
12476{
12477 uint32_t op, op1, op1_sbit, op1_ebit, coproc;
12478
12479 coproc = bits (arm_insn_r->arm_insn, 8, 11);
12480 op1 = bits (arm_insn_r->arm_insn, 20, 25);
12481 op1_ebit = bit (arm_insn_r->arm_insn, 20);
12482
12483 if ((coproc & 0x0e) == 0x0a)
12484 {
12485 /* Handle extension register ld/st instructions. */
12486 if (!(op1 & 0x20))
f20f80dd 12487 return arm_record_exreg_ld_st_insn (arm_insn_r);
60cc5e93
OJ
12488
12489 /* 64-bit transfers between arm core and extension registers. */
12490 if ((op1 & 0x3e) == 0x04)
f20f80dd 12491 return arm_record_exreg_ld_st_insn (arm_insn_r);
60cc5e93
OJ
12492 }
12493 else
12494 {
12495 /* Handle coprocessor ld/st instructions. */
12496 if (!(op1 & 0x3a))
12497 {
12498 /* Store. */
12499 if (!op1_ebit)
12500 return arm_record_unsupported_insn (arm_insn_r);
12501 else
12502 /* Load. */
12503 return arm_record_unsupported_insn (arm_insn_r);
12504 }
12505
12506 /* Move to coprocessor from two arm core registers. */
12507 if (op1 == 0x4)
12508 return arm_record_unsupported_insn (arm_insn_r);
12509
12510 /* Move to two arm core registers from coprocessor. */
12511 if (op1 == 0x5)
12512 {
12513 uint32_t reg_t[2];
12514
12515 reg_t[0] = bits (arm_insn_r->arm_insn, 12, 15);
12516 reg_t[1] = bits (arm_insn_r->arm_insn, 16, 19);
12517 arm_insn_r->reg_rec_count = 2;
12518
12519 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, reg_t);
12520 return 0;
12521 }
12522 }
12523 return arm_record_unsupported_insn (arm_insn_r);
12524}
12525
72508ac0
PO
12526/* Handling opcode 111 insns. */
12527
12528static int
12529arm_record_coproc_data_proc (insn_decode_record *arm_insn_r)
12530{
60cc5e93 12531 uint32_t op, op1_sbit, op1_ebit, coproc;
72508ac0
PO
12532 struct gdbarch_tdep *tdep = gdbarch_tdep (arm_insn_r->gdbarch);
12533 struct regcache *reg_cache = arm_insn_r->regcache;
97dfe206 12534 ULONGEST u_regval = 0;
72508ac0
PO
12535
12536 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 24, 27);
60cc5e93
OJ
12537 coproc = bits (arm_insn_r->arm_insn, 8, 11);
12538 op1_sbit = bit (arm_insn_r->arm_insn, 24);
12539 op1_ebit = bit (arm_insn_r->arm_insn, 20);
12540 op = bit (arm_insn_r->arm_insn, 4);
97dfe206
OJ
12541
12542 /* Handle arm SWI/SVC system call instructions. */
60cc5e93 12543 if (op1_sbit)
97dfe206
OJ
12544 {
12545 if (tdep->arm_syscall_record != NULL)
12546 {
12547 ULONGEST svc_operand, svc_number;
12548
12549 svc_operand = (0x00ffffff & arm_insn_r->arm_insn);
12550
12551 if (svc_operand) /* OABI. */
12552 svc_number = svc_operand - 0x900000;
12553 else /* EABI. */
12554 regcache_raw_read_unsigned (reg_cache, 7, &svc_number);
12555
60cc5e93 12556 return tdep->arm_syscall_record (reg_cache, svc_number);
97dfe206
OJ
12557 }
12558 else
12559 {
12560 printf_unfiltered (_("no syscall record support\n"));
60cc5e93 12561 return -1;
97dfe206
OJ
12562 }
12563 }
60cc5e93
OJ
12564
12565 if ((coproc & 0x0e) == 0x0a)
12566 {
12567 /* VFP data-processing instructions. */
12568 if (!op1_sbit && !op)
851f26ae 12569 return arm_record_vfp_data_proc_insn (arm_insn_r);
60cc5e93
OJ
12570
12571 /* Advanced SIMD, VFP instructions. */
12572 if (!op1_sbit && op)
5a578da5 12573 return arm_record_vdata_transfer_insn (arm_insn_r);
60cc5e93 12574 }
97dfe206
OJ
12575 else
12576 {
60cc5e93
OJ
12577 /* Coprocessor data operations. */
12578 if (!op1_sbit && !op)
12579 return arm_record_unsupported_insn (arm_insn_r);
12580
12581 /* Move to Coprocessor from ARM core register. */
12582 if (!op1_sbit && !op1_ebit && op)
12583 return arm_record_unsupported_insn (arm_insn_r);
12584
12585 /* Move to arm core register from coprocessor. */
12586 if (!op1_sbit && op1_ebit && op)
12587 {
12588 uint32_t record_buf[1];
12589
12590 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12591 if (record_buf[0] == 15)
12592 record_buf[0] = ARM_PS_REGNUM;
12593
12594 arm_insn_r->reg_rec_count = 1;
12595 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count,
12596 record_buf);
12597 return 0;
12598 }
97dfe206 12599 }
72508ac0 12600
60cc5e93 12601 return arm_record_unsupported_insn (arm_insn_r);
72508ac0
PO
12602}
12603
12604/* Handling opcode 000 insns. */
12605
12606static int
12607thumb_record_shift_add_sub (insn_decode_record *thumb_insn_r)
12608{
12609 uint32_t record_buf[8];
12610 uint32_t reg_src1 = 0;
12611
12612 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12613
12614 record_buf[0] = ARM_PS_REGNUM;
12615 record_buf[1] = reg_src1;
12616 thumb_insn_r->reg_rec_count = 2;
12617
12618 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12619
12620 return 0;
12621}
12622
12623
12624/* Handling opcode 001 insns. */
12625
12626static int
12627thumb_record_add_sub_cmp_mov (insn_decode_record *thumb_insn_r)
12628{
12629 uint32_t record_buf[8];
12630 uint32_t reg_src1 = 0;
12631
12632 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12633
12634 record_buf[0] = ARM_PS_REGNUM;
12635 record_buf[1] = reg_src1;
12636 thumb_insn_r->reg_rec_count = 2;
12637
12638 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12639
12640 return 0;
12641}
12642
12643/* Handling opcode 010 insns. */
12644
12645static int
12646thumb_record_ld_st_reg_offset (insn_decode_record *thumb_insn_r)
12647{
12648 struct regcache *reg_cache = thumb_insn_r->regcache;
12649 uint32_t record_buf[8], record_buf_mem[8];
12650
12651 uint32_t reg_src1 = 0, reg_src2 = 0;
12652 uint32_t opcode1 = 0, opcode2 = 0, opcode3 = 0;
12653
12654 ULONGEST u_regval[2] = {0};
12655
12656 opcode1 = bits (thumb_insn_r->arm_insn, 10, 12);
12657
12658 if (bit (thumb_insn_r->arm_insn, 12))
12659 {
12660 /* Handle load/store register offset. */
12661 opcode2 = bits (thumb_insn_r->arm_insn, 9, 10);
12662 if (opcode2 >= 12 && opcode2 <= 15)
12663 {
12664 /* LDR(2), LDRB(2) , LDRH(2), LDRSB, LDRSH. */
12665 reg_src1 = bits (thumb_insn_r->arm_insn,0, 2);
12666 record_buf[0] = reg_src1;
12667 thumb_insn_r->reg_rec_count = 1;
12668 }
12669 else if (opcode2 >= 8 && opcode2 <= 10)
12670 {
12671 /* STR(2), STRB(2), STRH(2) . */
12672 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12673 reg_src2 = bits (thumb_insn_r->arm_insn, 6, 8);
12674 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
12675 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
12676 if (8 == opcode2)
12677 record_buf_mem[0] = 4; /* STR (2). */
12678 else if (10 == opcode2)
12679 record_buf_mem[0] = 1; /* STRB (2). */
12680 else if (9 == opcode2)
12681 record_buf_mem[0] = 2; /* STRH (2). */
12682 record_buf_mem[1] = u_regval[0] + u_regval[1];
12683 thumb_insn_r->mem_rec_count = 1;
12684 }
12685 }
12686 else if (bit (thumb_insn_r->arm_insn, 11))
12687 {
12688 /* Handle load from literal pool. */
12689 /* LDR(3). */
12690 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12691 record_buf[0] = reg_src1;
12692 thumb_insn_r->reg_rec_count = 1;
12693 }
12694 else if (opcode1)
12695 {
12696 opcode2 = bits (thumb_insn_r->arm_insn, 8, 9);
12697 opcode3 = bits (thumb_insn_r->arm_insn, 0, 2);
12698 if ((3 == opcode2) && (!opcode3))
12699 {
12700 /* Branch with exchange. */
12701 record_buf[0] = ARM_PS_REGNUM;
12702 thumb_insn_r->reg_rec_count = 1;
12703 }
12704 else
12705 {
12706 /* Format 8; special data processing insns. */
12707 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12708 record_buf[0] = ARM_PS_REGNUM;
12709 record_buf[1] = reg_src1;
12710 thumb_insn_r->reg_rec_count = 2;
12711 }
12712 }
12713 else
12714 {
12715 /* Format 5; data processing insns. */
12716 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12717 if (bit (thumb_insn_r->arm_insn, 7))
12718 {
12719 reg_src1 = reg_src1 + 8;
12720 }
12721 record_buf[0] = ARM_PS_REGNUM;
12722 record_buf[1] = reg_src1;
12723 thumb_insn_r->reg_rec_count = 2;
12724 }
12725
12726 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12727 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12728 record_buf_mem);
12729
12730 return 0;
12731}
12732
12733/* Handling opcode 001 insns. */
12734
12735static int
12736thumb_record_ld_st_imm_offset (insn_decode_record *thumb_insn_r)
12737{
12738 struct regcache *reg_cache = thumb_insn_r->regcache;
12739 uint32_t record_buf[8], record_buf_mem[8];
12740
12741 uint32_t reg_src1 = 0;
12742 uint32_t opcode = 0, immed_5 = 0;
12743
12744 ULONGEST u_regval = 0;
12745
12746 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12747
12748 if (opcode)
12749 {
12750 /* LDR(1). */
12751 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12752 record_buf[0] = reg_src1;
12753 thumb_insn_r->reg_rec_count = 1;
12754 }
12755 else
12756 {
12757 /* STR(1). */
12758 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12759 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
12760 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12761 record_buf_mem[0] = 4;
12762 record_buf_mem[1] = u_regval + (immed_5 * 4);
12763 thumb_insn_r->mem_rec_count = 1;
12764 }
12765
12766 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12767 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12768 record_buf_mem);
12769
12770 return 0;
12771}
12772
12773/* Handling opcode 100 insns. */
12774
12775static int
12776thumb_record_ld_st_stack (insn_decode_record *thumb_insn_r)
12777{
12778 struct regcache *reg_cache = thumb_insn_r->regcache;
12779 uint32_t record_buf[8], record_buf_mem[8];
12780
12781 uint32_t reg_src1 = 0;
12782 uint32_t opcode = 0, immed_8 = 0, immed_5 = 0;
12783
12784 ULONGEST u_regval = 0;
12785
12786 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12787
12788 if (3 == opcode)
12789 {
12790 /* LDR(4). */
12791 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12792 record_buf[0] = reg_src1;
12793 thumb_insn_r->reg_rec_count = 1;
12794 }
12795 else if (1 == opcode)
12796 {
12797 /* LDRH(1). */
12798 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12799 record_buf[0] = reg_src1;
12800 thumb_insn_r->reg_rec_count = 1;
12801 }
12802 else if (2 == opcode)
12803 {
12804 /* STR(3). */
12805 immed_8 = bits (thumb_insn_r->arm_insn, 0, 7);
12806 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
12807 record_buf_mem[0] = 4;
12808 record_buf_mem[1] = u_regval + (immed_8 * 4);
12809 thumb_insn_r->mem_rec_count = 1;
12810 }
12811 else if (0 == opcode)
12812 {
12813 /* STRH(1). */
12814 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
12815 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12816 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12817 record_buf_mem[0] = 2;
12818 record_buf_mem[1] = u_regval + (immed_5 * 2);
12819 thumb_insn_r->mem_rec_count = 1;
12820 }
12821
12822 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12823 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12824 record_buf_mem);
12825
12826 return 0;
12827}
12828
12829/* Handling opcode 101 insns. */
12830
12831static int
12832thumb_record_misc (insn_decode_record *thumb_insn_r)
12833{
12834 struct regcache *reg_cache = thumb_insn_r->regcache;
12835
12836 uint32_t opcode = 0, opcode1 = 0, opcode2 = 0;
12837 uint32_t register_bits = 0, register_count = 0;
12838 uint32_t register_list[8] = {0}, index = 0, start_address = 0;
12839 uint32_t record_buf[24], record_buf_mem[48];
12840 uint32_t reg_src1;
12841
12842 ULONGEST u_regval = 0;
12843
12844 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12845 opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
12846 opcode2 = bits (thumb_insn_r->arm_insn, 9, 12);
12847
12848 if (14 == opcode2)
12849 {
12850 /* POP. */
12851 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12852 while (register_bits)
f969241e
OJ
12853 {
12854 if (register_bits & 0x00000001)
12855 record_buf[index++] = register_count;
12856 register_bits = register_bits >> 1;
12857 register_count++;
12858 }
12859 record_buf[index++] = ARM_PS_REGNUM;
12860 record_buf[index++] = ARM_SP_REGNUM;
12861 thumb_insn_r->reg_rec_count = index;
72508ac0
PO
12862 }
12863 else if (10 == opcode2)
12864 {
12865 /* PUSH. */
12866 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
9904a494 12867 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
72508ac0
PO
12868 while (register_bits)
12869 {
12870 if (register_bits & 0x00000001)
12871 register_count++;
12872 register_bits = register_bits >> 1;
12873 }
12874 start_address = u_regval - \
12875 (4 * (bit (thumb_insn_r->arm_insn, 8) + register_count));
12876 thumb_insn_r->mem_rec_count = register_count;
12877 while (register_count)
12878 {
12879 record_buf_mem[(register_count * 2) - 1] = start_address;
12880 record_buf_mem[(register_count * 2) - 2] = 4;
12881 start_address = start_address + 4;
12882 register_count--;
12883 }
12884 record_buf[0] = ARM_SP_REGNUM;
12885 thumb_insn_r->reg_rec_count = 1;
12886 }
12887 else if (0x1E == opcode1)
12888 {
12889 /* BKPT insn. */
12890 /* Handle enhanced software breakpoint insn, BKPT. */
12891 /* CPSR is changed to be executed in ARM state, disabling normal
12892 interrupts, entering abort mode. */
12893 /* According to high vector configuration PC is set. */
12894 /* User hits breakpoint and type reverse, in that case, we need to go back with
12895 previous CPSR and Program Counter. */
12896 record_buf[0] = ARM_PS_REGNUM;
12897 record_buf[1] = ARM_LR_REGNUM;
12898 thumb_insn_r->reg_rec_count = 2;
12899 /* We need to save SPSR value, which is not yet done. */
12900 printf_unfiltered (_("Process record does not support instruction "
12901 "0x%0x at address %s.\n"),
12902 thumb_insn_r->arm_insn,
12903 paddress (thumb_insn_r->gdbarch,
12904 thumb_insn_r->this_addr));
12905 return -1;
12906 }
12907 else if ((0 == opcode) || (1 == opcode))
12908 {
12909 /* ADD(5), ADD(6). */
12910 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12911 record_buf[0] = reg_src1;
12912 thumb_insn_r->reg_rec_count = 1;
12913 }
12914 else if (2 == opcode)
12915 {
12916 /* ADD(7), SUB(4). */
12917 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12918 record_buf[0] = ARM_SP_REGNUM;
12919 thumb_insn_r->reg_rec_count = 1;
12920 }
12921
12922 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12923 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12924 record_buf_mem);
12925
12926 return 0;
12927}
12928
12929/* Handling opcode 110 insns. */
12930
12931static int
12932thumb_record_ldm_stm_swi (insn_decode_record *thumb_insn_r)
12933{
12934 struct gdbarch_tdep *tdep = gdbarch_tdep (thumb_insn_r->gdbarch);
12935 struct regcache *reg_cache = thumb_insn_r->regcache;
12936
12937 uint32_t ret = 0; /* function return value: -1:record failure ; 0:success */
12938 uint32_t reg_src1 = 0;
12939 uint32_t opcode1 = 0, opcode2 = 0, register_bits = 0, register_count = 0;
12940 uint32_t register_list[8] = {0}, index = 0, start_address = 0;
12941 uint32_t record_buf[24], record_buf_mem[48];
12942
12943 ULONGEST u_regval = 0;
12944
12945 opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
12946 opcode2 = bits (thumb_insn_r->arm_insn, 11, 12);
12947
12948 if (1 == opcode2)
12949 {
12950
12951 /* LDMIA. */
12952 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12953 /* Get Rn. */
12954 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12955 while (register_bits)
12956 {
12957 if (register_bits & 0x00000001)
f969241e 12958 record_buf[index++] = register_count;
72508ac0 12959 register_bits = register_bits >> 1;
f969241e 12960 register_count++;
72508ac0 12961 }
f969241e
OJ
12962 record_buf[index++] = reg_src1;
12963 thumb_insn_r->reg_rec_count = index;
72508ac0
PO
12964 }
12965 else if (0 == opcode2)
12966 {
12967 /* It handles both STMIA. */
12968 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12969 /* Get Rn. */
12970 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12971 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12972 while (register_bits)
12973 {
12974 if (register_bits & 0x00000001)
12975 register_count++;
12976 register_bits = register_bits >> 1;
12977 }
12978 start_address = u_regval;
12979 thumb_insn_r->mem_rec_count = register_count;
12980 while (register_count)
12981 {
12982 record_buf_mem[(register_count * 2) - 1] = start_address;
12983 record_buf_mem[(register_count * 2) - 2] = 4;
12984 start_address = start_address + 4;
12985 register_count--;
12986 }
12987 }
12988 else if (0x1F == opcode1)
12989 {
12990 /* Handle arm syscall insn. */
97dfe206 12991 if (tdep->arm_syscall_record != NULL)
72508ac0 12992 {
97dfe206
OJ
12993 regcache_raw_read_unsigned (reg_cache, 7, &u_regval);
12994 ret = tdep->arm_syscall_record (reg_cache, u_regval);
72508ac0
PO
12995 }
12996 else
12997 {
12998 printf_unfiltered (_("no syscall record support\n"));
12999 return -1;
13000 }
13001 }
13002
13003 /* B (1), conditional branch is automatically taken care in process_record,
13004 as PC is saved there. */
13005
13006 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13007 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
13008 record_buf_mem);
13009
13010 return ret;
13011}
13012
13013/* Handling opcode 111 insns. */
13014
13015static int
13016thumb_record_branch (insn_decode_record *thumb_insn_r)
13017{
13018 uint32_t record_buf[8];
13019 uint32_t bits_h = 0;
13020
13021 bits_h = bits (thumb_insn_r->arm_insn, 11, 12);
13022
13023 if (2 == bits_h || 3 == bits_h)
13024 {
13025 /* BL */
13026 record_buf[0] = ARM_LR_REGNUM;
13027 thumb_insn_r->reg_rec_count = 1;
13028 }
13029 else if (1 == bits_h)
13030 {
13031 /* BLX(1). */
13032 record_buf[0] = ARM_PS_REGNUM;
13033 record_buf[1] = ARM_LR_REGNUM;
13034 thumb_insn_r->reg_rec_count = 2;
13035 }
13036
13037 /* B(2) is automatically taken care in process_record, as PC is
13038 saved there. */
13039
13040 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13041
13042 return 0;
13043}
13044
c6ec2b30
OJ
13045/* Handler for thumb2 load/store multiple instructions. */
13046
13047static int
13048thumb2_record_ld_st_multiple (insn_decode_record *thumb2_insn_r)
13049{
13050 struct regcache *reg_cache = thumb2_insn_r->regcache;
13051
13052 uint32_t reg_rn, op;
13053 uint32_t register_bits = 0, register_count = 0;
13054 uint32_t index = 0, start_address = 0;
13055 uint32_t record_buf[24], record_buf_mem[48];
13056
13057 ULONGEST u_regval = 0;
13058
13059 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13060 op = bits (thumb2_insn_r->arm_insn, 23, 24);
13061
13062 if (0 == op || 3 == op)
13063 {
13064 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
13065 {
13066 /* Handle RFE instruction. */
13067 record_buf[0] = ARM_PS_REGNUM;
13068 thumb2_insn_r->reg_rec_count = 1;
13069 }
13070 else
13071 {
13072 /* Handle SRS instruction after reading banked SP. */
13073 return arm_record_unsupported_insn (thumb2_insn_r);
13074 }
13075 }
13076 else if (1 == op || 2 == op)
13077 {
13078 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
13079 {
13080 /* Handle LDM/LDMIA/LDMFD and LDMDB/LDMEA instructions. */
13081 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
13082 while (register_bits)
13083 {
13084 if (register_bits & 0x00000001)
13085 record_buf[index++] = register_count;
13086
13087 register_count++;
13088 register_bits = register_bits >> 1;
13089 }
13090 record_buf[index++] = reg_rn;
13091 record_buf[index++] = ARM_PS_REGNUM;
13092 thumb2_insn_r->reg_rec_count = index;
13093 }
13094 else
13095 {
13096 /* Handle STM/STMIA/STMEA and STMDB/STMFD. */
13097 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
13098 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
13099 while (register_bits)
13100 {
13101 if (register_bits & 0x00000001)
13102 register_count++;
13103
13104 register_bits = register_bits >> 1;
13105 }
13106
13107 if (1 == op)
13108 {
13109 /* Start address calculation for LDMDB/LDMEA. */
13110 start_address = u_regval;
13111 }
13112 else if (2 == op)
13113 {
13114 /* Start address calculation for LDMDB/LDMEA. */
13115 start_address = u_regval - register_count * 4;
13116 }
13117
13118 thumb2_insn_r->mem_rec_count = register_count;
13119 while (register_count)
13120 {
13121 record_buf_mem[register_count * 2 - 1] = start_address;
13122 record_buf_mem[register_count * 2 - 2] = 4;
13123 start_address = start_address + 4;
13124 register_count--;
13125 }
13126 record_buf[0] = reg_rn;
13127 record_buf[1] = ARM_PS_REGNUM;
13128 thumb2_insn_r->reg_rec_count = 2;
13129 }
13130 }
13131
13132 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
13133 record_buf_mem);
13134 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13135 record_buf);
13136 return ARM_RECORD_SUCCESS;
13137}
13138
13139/* Handler for thumb2 load/store (dual/exclusive) and table branch
13140 instructions. */
13141
13142static int
13143thumb2_record_ld_st_dual_ex_tbb (insn_decode_record *thumb2_insn_r)
13144{
13145 struct regcache *reg_cache = thumb2_insn_r->regcache;
13146
13147 uint32_t reg_rd, reg_rn, offset_imm;
13148 uint32_t reg_dest1, reg_dest2;
13149 uint32_t address, offset_addr;
13150 uint32_t record_buf[8], record_buf_mem[8];
13151 uint32_t op1, op2, op3;
13152 LONGEST s_word;
13153
13154 ULONGEST u_regval[2];
13155
13156 op1 = bits (thumb2_insn_r->arm_insn, 23, 24);
13157 op2 = bits (thumb2_insn_r->arm_insn, 20, 21);
13158 op3 = bits (thumb2_insn_r->arm_insn, 4, 7);
13159
13160 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
13161 {
13162 if(!(1 == op1 && 1 == op2 && (0 == op3 || 1 == op3)))
13163 {
13164 reg_dest1 = bits (thumb2_insn_r->arm_insn, 12, 15);
13165 record_buf[0] = reg_dest1;
13166 record_buf[1] = ARM_PS_REGNUM;
13167 thumb2_insn_r->reg_rec_count = 2;
13168 }
13169
13170 if (3 == op2 || (op1 & 2) || (1 == op1 && 1 == op2 && 7 == op3))
13171 {
13172 reg_dest2 = bits (thumb2_insn_r->arm_insn, 8, 11);
13173 record_buf[2] = reg_dest2;
13174 thumb2_insn_r->reg_rec_count = 3;
13175 }
13176 }
13177 else
13178 {
13179 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13180 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
13181
13182 if (0 == op1 && 0 == op2)
13183 {
13184 /* Handle STREX. */
13185 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13186 address = u_regval[0] + (offset_imm * 4);
13187 record_buf_mem[0] = 4;
13188 record_buf_mem[1] = address;
13189 thumb2_insn_r->mem_rec_count = 1;
13190 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
13191 record_buf[0] = reg_rd;
13192 thumb2_insn_r->reg_rec_count = 1;
13193 }
13194 else if (1 == op1 && 0 == op2)
13195 {
13196 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
13197 record_buf[0] = reg_rd;
13198 thumb2_insn_r->reg_rec_count = 1;
13199 address = u_regval[0];
13200 record_buf_mem[1] = address;
13201
13202 if (4 == op3)
13203 {
13204 /* Handle STREXB. */
13205 record_buf_mem[0] = 1;
13206 thumb2_insn_r->mem_rec_count = 1;
13207 }
13208 else if (5 == op3)
13209 {
13210 /* Handle STREXH. */
13211 record_buf_mem[0] = 2 ;
13212 thumb2_insn_r->mem_rec_count = 1;
13213 }
13214 else if (7 == op3)
13215 {
13216 /* Handle STREXD. */
13217 address = u_regval[0];
13218 record_buf_mem[0] = 4;
13219 record_buf_mem[2] = 4;
13220 record_buf_mem[3] = address + 4;
13221 thumb2_insn_r->mem_rec_count = 2;
13222 }
13223 }
13224 else
13225 {
13226 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13227
13228 if (bit (thumb2_insn_r->arm_insn, 24))
13229 {
13230 if (bit (thumb2_insn_r->arm_insn, 23))
13231 offset_addr = u_regval[0] + (offset_imm * 4);
13232 else
13233 offset_addr = u_regval[0] - (offset_imm * 4);
13234
13235 address = offset_addr;
13236 }
13237 else
13238 address = u_regval[0];
13239
13240 record_buf_mem[0] = 4;
13241 record_buf_mem[1] = address;
13242 record_buf_mem[2] = 4;
13243 record_buf_mem[3] = address + 4;
13244 thumb2_insn_r->mem_rec_count = 2;
13245 record_buf[0] = reg_rn;
13246 thumb2_insn_r->reg_rec_count = 1;
13247 }
13248 }
13249
13250 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13251 record_buf);
13252 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
13253 record_buf_mem);
13254 return ARM_RECORD_SUCCESS;
13255}
13256
13257/* Handler for thumb2 data processing (shift register and modified immediate)
13258 instructions. */
13259
13260static int
13261thumb2_record_data_proc_sreg_mimm (insn_decode_record *thumb2_insn_r)
13262{
13263 uint32_t reg_rd, op;
13264 uint32_t record_buf[8];
13265
13266 op = bits (thumb2_insn_r->arm_insn, 21, 24);
13267 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
13268
13269 if ((0 == op || 4 == op || 8 == op || 13 == op) && 15 == reg_rd)
13270 {
13271 record_buf[0] = ARM_PS_REGNUM;
13272 thumb2_insn_r->reg_rec_count = 1;
13273 }
13274 else
13275 {
13276 record_buf[0] = reg_rd;
13277 record_buf[1] = ARM_PS_REGNUM;
13278 thumb2_insn_r->reg_rec_count = 2;
13279 }
13280
13281 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13282 record_buf);
13283 return ARM_RECORD_SUCCESS;
13284}
13285
13286/* Generic handler for thumb2 instructions which effect destination and PS
13287 registers. */
13288
13289static int
13290thumb2_record_ps_dest_generic (insn_decode_record *thumb2_insn_r)
13291{
13292 uint32_t reg_rd;
13293 uint32_t record_buf[8];
13294
13295 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
13296
13297 record_buf[0] = reg_rd;
13298 record_buf[1] = ARM_PS_REGNUM;
13299 thumb2_insn_r->reg_rec_count = 2;
13300
13301 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13302 record_buf);
13303 return ARM_RECORD_SUCCESS;
13304}
13305
13306/* Handler for thumb2 branch and miscellaneous control instructions. */
13307
13308static int
13309thumb2_record_branch_misc_cntrl (insn_decode_record *thumb2_insn_r)
13310{
13311 uint32_t op, op1, op2;
13312 uint32_t record_buf[8];
13313
13314 op = bits (thumb2_insn_r->arm_insn, 20, 26);
13315 op1 = bits (thumb2_insn_r->arm_insn, 12, 14);
13316 op2 = bits (thumb2_insn_r->arm_insn, 8, 11);
13317
13318 /* Handle MSR insn. */
13319 if (!(op1 & 0x2) && 0x38 == op)
13320 {
13321 if (!(op2 & 0x3))
13322 {
13323 /* CPSR is going to be changed. */
13324 record_buf[0] = ARM_PS_REGNUM;
13325 thumb2_insn_r->reg_rec_count = 1;
13326 }
13327 else
13328 {
13329 arm_record_unsupported_insn(thumb2_insn_r);
13330 return -1;
13331 }
13332 }
13333 else if (4 == (op1 & 0x5) || 5 == (op1 & 0x5))
13334 {
13335 /* BLX. */
13336 record_buf[0] = ARM_PS_REGNUM;
13337 record_buf[1] = ARM_LR_REGNUM;
13338 thumb2_insn_r->reg_rec_count = 2;
13339 }
13340
13341 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13342 record_buf);
13343 return ARM_RECORD_SUCCESS;
13344}
13345
13346/* Handler for thumb2 store single data item instructions. */
13347
13348static int
13349thumb2_record_str_single_data (insn_decode_record *thumb2_insn_r)
13350{
13351 struct regcache *reg_cache = thumb2_insn_r->regcache;
13352
13353 uint32_t reg_rn, reg_rm, offset_imm, shift_imm;
13354 uint32_t address, offset_addr;
13355 uint32_t record_buf[8], record_buf_mem[8];
13356 uint32_t op1, op2;
13357
13358 ULONGEST u_regval[2];
13359
13360 op1 = bits (thumb2_insn_r->arm_insn, 21, 23);
13361 op2 = bits (thumb2_insn_r->arm_insn, 6, 11);
13362 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13363 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
13364
13365 if (bit (thumb2_insn_r->arm_insn, 23))
13366 {
13367 /* T2 encoding. */
13368 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 11);
13369 offset_addr = u_regval[0] + offset_imm;
13370 address = offset_addr;
13371 }
13372 else
13373 {
13374 /* T3 encoding. */
13375 if ((0 == op1 || 1 == op1 || 2 == op1) && !(op2 & 0x20))
13376 {
13377 /* Handle STRB (register). */
13378 reg_rm = bits (thumb2_insn_r->arm_insn, 0, 3);
13379 regcache_raw_read_unsigned (reg_cache, reg_rm, &u_regval[1]);
13380 shift_imm = bits (thumb2_insn_r->arm_insn, 4, 5);
13381 offset_addr = u_regval[1] << shift_imm;
13382 address = u_regval[0] + offset_addr;
13383 }
13384 else
13385 {
13386 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13387 if (bit (thumb2_insn_r->arm_insn, 10))
13388 {
13389 if (bit (thumb2_insn_r->arm_insn, 9))
13390 offset_addr = u_regval[0] + offset_imm;
13391 else
13392 offset_addr = u_regval[0] - offset_imm;
13393
13394 address = offset_addr;
13395 }
13396 else
13397 address = u_regval[0];
13398 }
13399 }
13400
13401 switch (op1)
13402 {
13403 /* Store byte instructions. */
13404 case 4:
13405 case 0:
13406 record_buf_mem[0] = 1;
13407 break;
13408 /* Store half word instructions. */
13409 case 1:
13410 case 5:
13411 record_buf_mem[0] = 2;
13412 break;
13413 /* Store word instructions. */
13414 case 2:
13415 case 6:
13416 record_buf_mem[0] = 4;
13417 break;
13418
13419 default:
13420 gdb_assert_not_reached ("no decoding pattern found");
13421 break;
13422 }
13423
13424 record_buf_mem[1] = address;
13425 thumb2_insn_r->mem_rec_count = 1;
13426 record_buf[0] = reg_rn;
13427 thumb2_insn_r->reg_rec_count = 1;
13428
13429 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13430 record_buf);
13431 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
13432 record_buf_mem);
13433 return ARM_RECORD_SUCCESS;
13434}
13435
13436/* Handler for thumb2 load memory hints instructions. */
13437
13438static int
13439thumb2_record_ld_mem_hints (insn_decode_record *thumb2_insn_r)
13440{
13441 uint32_t record_buf[8];
13442 uint32_t reg_rt, reg_rn;
13443
13444 reg_rt = bits (thumb2_insn_r->arm_insn, 12, 15);
13445 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13446
13447 if (ARM_PC_REGNUM != reg_rt)
13448 {
13449 record_buf[0] = reg_rt;
13450 record_buf[1] = reg_rn;
13451 record_buf[2] = ARM_PS_REGNUM;
13452 thumb2_insn_r->reg_rec_count = 3;
13453
13454 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13455 record_buf);
13456 return ARM_RECORD_SUCCESS;
13457 }
13458
13459 return ARM_RECORD_FAILURE;
13460}
13461
13462/* Handler for thumb2 load word instructions. */
13463
13464static int
13465thumb2_record_ld_word (insn_decode_record *thumb2_insn_r)
13466{
13467 uint32_t opcode1 = 0, opcode2 = 0;
13468 uint32_t record_buf[8];
13469
13470 record_buf[0] = bits (thumb2_insn_r->arm_insn, 12, 15);
13471 record_buf[1] = ARM_PS_REGNUM;
13472 thumb2_insn_r->reg_rec_count = 2;
13473
13474 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13475 record_buf);
13476 return ARM_RECORD_SUCCESS;
13477}
13478
13479/* Handler for thumb2 long multiply, long multiply accumulate, and
13480 divide instructions. */
13481
13482static int
13483thumb2_record_lmul_lmla_div (insn_decode_record *thumb2_insn_r)
13484{
13485 uint32_t opcode1 = 0, opcode2 = 0;
13486 uint32_t record_buf[8];
13487 uint32_t reg_src1 = 0;
13488
13489 opcode1 = bits (thumb2_insn_r->arm_insn, 20, 22);
13490 opcode2 = bits (thumb2_insn_r->arm_insn, 4, 7);
13491
13492 if (0 == opcode1 || 2 == opcode1 || (opcode1 >= 4 && opcode1 <= 6))
13493 {
13494 /* Handle SMULL, UMULL, SMULAL. */
13495 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
13496 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
13497 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
13498 record_buf[2] = ARM_PS_REGNUM;
13499 thumb2_insn_r->reg_rec_count = 3;
13500 }
13501 else if (1 == opcode1 || 3 == opcode2)
13502 {
13503 /* Handle SDIV and UDIV. */
13504 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
13505 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
13506 record_buf[2] = ARM_PS_REGNUM;
13507 thumb2_insn_r->reg_rec_count = 3;
13508 }
13509 else
13510 return ARM_RECORD_FAILURE;
13511
13512 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13513 record_buf);
13514 return ARM_RECORD_SUCCESS;
13515}
13516
60cc5e93
OJ
13517/* Record handler for thumb32 coprocessor instructions. */
13518
13519static int
13520thumb2_record_coproc_insn (insn_decode_record *thumb2_insn_r)
13521{
13522 if (bit (thumb2_insn_r->arm_insn, 25))
13523 return arm_record_coproc_data_proc (thumb2_insn_r);
13524 else
13525 return arm_record_asimd_vfp_coproc (thumb2_insn_r);
13526}
13527
1e1b6563
OJ
13528/* Record handler for advance SIMD structure load/store instructions. */
13529
13530static int
13531thumb2_record_asimd_struct_ld_st (insn_decode_record *thumb2_insn_r)
13532{
13533 struct regcache *reg_cache = thumb2_insn_r->regcache;
13534 uint32_t l_bit, a_bit, b_bits;
13535 uint32_t record_buf[128], record_buf_mem[128];
13536 uint32_t reg_rn, reg_vd, address, f_esize, f_elem;
13537 uint32_t index_r = 0, index_e = 0, bf_regs = 0, index_m = 0, loop_t = 0;
13538 uint8_t f_ebytes;
13539
13540 l_bit = bit (thumb2_insn_r->arm_insn, 21);
13541 a_bit = bit (thumb2_insn_r->arm_insn, 23);
13542 b_bits = bits (thumb2_insn_r->arm_insn, 8, 11);
13543 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13544 reg_vd = bits (thumb2_insn_r->arm_insn, 12, 15);
13545 reg_vd = (bit (thumb2_insn_r->arm_insn, 22) << 4) | reg_vd;
13546 f_ebytes = (1 << bits (thumb2_insn_r->arm_insn, 6, 7));
13547 f_esize = 8 * f_ebytes;
13548 f_elem = 8 / f_ebytes;
13549
13550 if (!l_bit)
13551 {
13552 ULONGEST u_regval = 0;
13553 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
13554 address = u_regval;
13555
13556 if (!a_bit)
13557 {
13558 /* Handle VST1. */
13559 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
13560 {
13561 if (b_bits == 0x07)
13562 bf_regs = 1;
13563 else if (b_bits == 0x0a)
13564 bf_regs = 2;
13565 else if (b_bits == 0x06)
13566 bf_regs = 3;
13567 else if (b_bits == 0x02)
13568 bf_regs = 4;
13569 else
13570 bf_regs = 0;
13571
13572 for (index_r = 0; index_r < bf_regs; index_r++)
13573 {
13574 for (index_e = 0; index_e < f_elem; index_e++)
13575 {
13576 record_buf_mem[index_m++] = f_ebytes;
13577 record_buf_mem[index_m++] = address;
13578 address = address + f_ebytes;
13579 thumb2_insn_r->mem_rec_count += 1;
13580 }
13581 }
13582 }
13583 /* Handle VST2. */
13584 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
13585 {
13586 if (b_bits == 0x09 || b_bits == 0x08)
13587 bf_regs = 1;
13588 else if (b_bits == 0x03)
13589 bf_regs = 2;
13590 else
13591 bf_regs = 0;
13592
13593 for (index_r = 0; index_r < bf_regs; index_r++)
13594 for (index_e = 0; index_e < f_elem; index_e++)
13595 {
13596 for (loop_t = 0; loop_t < 2; loop_t++)
13597 {
13598 record_buf_mem[index_m++] = f_ebytes;
13599 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
13600 thumb2_insn_r->mem_rec_count += 1;
13601 }
13602 address = address + (2 * f_ebytes);
13603 }
13604 }
13605 /* Handle VST3. */
13606 else if ((b_bits & 0x0e) == 0x04)
13607 {
13608 for (index_e = 0; index_e < f_elem; index_e++)
13609 {
13610 for (loop_t = 0; loop_t < 3; loop_t++)
13611 {
13612 record_buf_mem[index_m++] = f_ebytes;
13613 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
13614 thumb2_insn_r->mem_rec_count += 1;
13615 }
13616 address = address + (3 * f_ebytes);
13617 }
13618 }
13619 /* Handle VST4. */
13620 else if (!(b_bits & 0x0e))
13621 {
13622 for (index_e = 0; index_e < f_elem; index_e++)
13623 {
13624 for (loop_t = 0; loop_t < 4; loop_t++)
13625 {
13626 record_buf_mem[index_m++] = f_ebytes;
13627 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
13628 thumb2_insn_r->mem_rec_count += 1;
13629 }
13630 address = address + (4 * f_ebytes);
13631 }
13632 }
13633 }
13634 else
13635 {
13636 uint8_t bft_size = bits (thumb2_insn_r->arm_insn, 10, 11);
13637
13638 if (bft_size == 0x00)
13639 f_ebytes = 1;
13640 else if (bft_size == 0x01)
13641 f_ebytes = 2;
13642 else if (bft_size == 0x02)
13643 f_ebytes = 4;
13644 else
13645 f_ebytes = 0;
13646
13647 /* Handle VST1. */
13648 if (!(b_bits & 0x0b) || b_bits == 0x08)
13649 thumb2_insn_r->mem_rec_count = 1;
13650 /* Handle VST2. */
13651 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09)
13652 thumb2_insn_r->mem_rec_count = 2;
13653 /* Handle VST3. */
13654 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a)
13655 thumb2_insn_r->mem_rec_count = 3;
13656 /* Handle VST4. */
13657 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b)
13658 thumb2_insn_r->mem_rec_count = 4;
13659
13660 for (index_m = 0; index_m < thumb2_insn_r->mem_rec_count; index_m++)
13661 {
13662 record_buf_mem[index_m] = f_ebytes;
13663 record_buf_mem[index_m] = address + (index_m * f_ebytes);
13664 }
13665 }
13666 }
13667 else
13668 {
13669 if (!a_bit)
13670 {
13671 /* Handle VLD1. */
13672 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
13673 thumb2_insn_r->reg_rec_count = 1;
13674 /* Handle VLD2. */
13675 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
13676 thumb2_insn_r->reg_rec_count = 2;
13677 /* Handle VLD3. */
13678 else if ((b_bits & 0x0e) == 0x04)
13679 thumb2_insn_r->reg_rec_count = 3;
13680 /* Handle VLD4. */
13681 else if (!(b_bits & 0x0e))
13682 thumb2_insn_r->reg_rec_count = 4;
13683 }
13684 else
13685 {
13686 /* Handle VLD1. */
13687 if (!(b_bits & 0x0b) || b_bits == 0x08 || b_bits == 0x0c)
13688 thumb2_insn_r->reg_rec_count = 1;
13689 /* Handle VLD2. */
13690 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09 || b_bits == 0x0d)
13691 thumb2_insn_r->reg_rec_count = 2;
13692 /* Handle VLD3. */
13693 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a || b_bits == 0x0e)
13694 thumb2_insn_r->reg_rec_count = 3;
13695 /* Handle VLD4. */
13696 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b || b_bits == 0x0f)
13697 thumb2_insn_r->reg_rec_count = 4;
13698
13699 for (index_r = 0; index_r < thumb2_insn_r->reg_rec_count; index_r++)
13700 record_buf[index_r] = reg_vd + ARM_D0_REGNUM + index_r;
13701 }
13702 }
13703
13704 if (bits (thumb2_insn_r->arm_insn, 0, 3) != 15)
13705 {
13706 record_buf[index_r] = reg_rn;
13707 thumb2_insn_r->reg_rec_count += 1;
13708 }
13709
13710 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13711 record_buf);
13712 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
13713 record_buf_mem);
13714 return 0;
13715}
13716
c6ec2b30
OJ
13717/* Decodes thumb2 instruction type and invokes its record handler. */
13718
13719static unsigned int
13720thumb2_record_decode_insn_handler (insn_decode_record *thumb2_insn_r)
13721{
13722 uint32_t op, op1, op2;
13723
13724 op = bit (thumb2_insn_r->arm_insn, 15);
13725 op1 = bits (thumb2_insn_r->arm_insn, 27, 28);
13726 op2 = bits (thumb2_insn_r->arm_insn, 20, 26);
13727
13728 if (op1 == 0x01)
13729 {
13730 if (!(op2 & 0x64 ))
13731 {
13732 /* Load/store multiple instruction. */
13733 return thumb2_record_ld_st_multiple (thumb2_insn_r);
13734 }
13735 else if (!((op2 & 0x64) ^ 0x04))
13736 {
13737 /* Load/store (dual/exclusive) and table branch instruction. */
13738 return thumb2_record_ld_st_dual_ex_tbb (thumb2_insn_r);
13739 }
13740 else if (!((op2 & 0x20) ^ 0x20))
13741 {
13742 /* Data-processing (shifted register). */
13743 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
13744 }
13745 else if (op2 & 0x40)
13746 {
13747 /* Co-processor instructions. */
60cc5e93 13748 return thumb2_record_coproc_insn (thumb2_insn_r);
c6ec2b30
OJ
13749 }
13750 }
13751 else if (op1 == 0x02)
13752 {
13753 if (op)
13754 {
13755 /* Branches and miscellaneous control instructions. */
13756 return thumb2_record_branch_misc_cntrl (thumb2_insn_r);
13757 }
13758 else if (op2 & 0x20)
13759 {
13760 /* Data-processing (plain binary immediate) instruction. */
13761 return thumb2_record_ps_dest_generic (thumb2_insn_r);
13762 }
13763 else
13764 {
13765 /* Data-processing (modified immediate). */
13766 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
13767 }
13768 }
13769 else if (op1 == 0x03)
13770 {
13771 if (!(op2 & 0x71 ))
13772 {
13773 /* Store single data item. */
13774 return thumb2_record_str_single_data (thumb2_insn_r);
13775 }
13776 else if (!((op2 & 0x71) ^ 0x10))
13777 {
13778 /* Advanced SIMD or structure load/store instructions. */
1e1b6563 13779 return thumb2_record_asimd_struct_ld_st (thumb2_insn_r);
c6ec2b30
OJ
13780 }
13781 else if (!((op2 & 0x67) ^ 0x01))
13782 {
13783 /* Load byte, memory hints instruction. */
13784 return thumb2_record_ld_mem_hints (thumb2_insn_r);
13785 }
13786 else if (!((op2 & 0x67) ^ 0x03))
13787 {
13788 /* Load halfword, memory hints instruction. */
13789 return thumb2_record_ld_mem_hints (thumb2_insn_r);
13790 }
13791 else if (!((op2 & 0x67) ^ 0x05))
13792 {
13793 /* Load word instruction. */
13794 return thumb2_record_ld_word (thumb2_insn_r);
13795 }
13796 else if (!((op2 & 0x70) ^ 0x20))
13797 {
13798 /* Data-processing (register) instruction. */
13799 return thumb2_record_ps_dest_generic (thumb2_insn_r);
13800 }
13801 else if (!((op2 & 0x78) ^ 0x30))
13802 {
13803 /* Multiply, multiply accumulate, abs diff instruction. */
13804 return thumb2_record_ps_dest_generic (thumb2_insn_r);
13805 }
13806 else if (!((op2 & 0x78) ^ 0x38))
13807 {
13808 /* Long multiply, long multiply accumulate, and divide. */
13809 return thumb2_record_lmul_lmla_div (thumb2_insn_r);
13810 }
13811 else if (op2 & 0x40)
13812 {
13813 /* Co-processor instructions. */
60cc5e93 13814 return thumb2_record_coproc_insn (thumb2_insn_r);
c6ec2b30
OJ
13815 }
13816 }
13817
13818 return -1;
13819}
72508ac0
PO
13820
13821/* Extracts arm/thumb/thumb2 insn depending on the size, and returns 0 on success
13822and positive val on fauilure. */
13823
13824static int
13825extract_arm_insn (insn_decode_record *insn_record, uint32_t insn_size)
13826{
13827 gdb_byte buf[insn_size];
13828
13829 memset (&buf[0], 0, insn_size);
13830
13831 if (target_read_memory (insn_record->this_addr, &buf[0], insn_size))
13832 return 1;
13833 insn_record->arm_insn = (uint32_t) extract_unsigned_integer (&buf[0],
13834 insn_size,
2959fed9 13835 gdbarch_byte_order_for_code (insn_record->gdbarch));
72508ac0
PO
13836 return 0;
13837}
13838
13839typedef int (*sti_arm_hdl_fp_t) (insn_decode_record*);
13840
13841/* Decode arm/thumb insn depending on condition cods and opcodes; and
13842 dispatch it. */
13843
13844static int
13845decode_insn (insn_decode_record *arm_record, record_type_t record_type,
13846 uint32_t insn_size)
13847{
13848
13849 /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm instruction. */
0fa9c223 13850 static const sti_arm_hdl_fp_t arm_handle_insn[8] =
72508ac0
PO
13851 {
13852 arm_record_data_proc_misc_ld_str, /* 000. */
13853 arm_record_data_proc_imm, /* 001. */
13854 arm_record_ld_st_imm_offset, /* 010. */
13855 arm_record_ld_st_reg_offset, /* 011. */
13856 arm_record_ld_st_multiple, /* 100. */
13857 arm_record_b_bl, /* 101. */
60cc5e93 13858 arm_record_asimd_vfp_coproc, /* 110. */
72508ac0
PO
13859 arm_record_coproc_data_proc /* 111. */
13860 };
13861
13862 /* (Starting from numerical 0); bits 13,14,15 decodes type of thumb instruction. */
0fa9c223 13863 static const sti_arm_hdl_fp_t thumb_handle_insn[8] =
72508ac0
PO
13864 { \
13865 thumb_record_shift_add_sub, /* 000. */
13866 thumb_record_add_sub_cmp_mov, /* 001. */
13867 thumb_record_ld_st_reg_offset, /* 010. */
13868 thumb_record_ld_st_imm_offset, /* 011. */
13869 thumb_record_ld_st_stack, /* 100. */
13870 thumb_record_misc, /* 101. */
13871 thumb_record_ldm_stm_swi, /* 110. */
13872 thumb_record_branch /* 111. */
13873 };
13874
13875 uint32_t ret = 0; /* return value: negative:failure 0:success. */
13876 uint32_t insn_id = 0;
13877
13878 if (extract_arm_insn (arm_record, insn_size))
13879 {
13880 if (record_debug)
13881 {
13882 printf_unfiltered (_("Process record: error reading memory at "
13883 "addr %s len = %d.\n"),
13884 paddress (arm_record->gdbarch, arm_record->this_addr), insn_size);
13885 }
13886 return -1;
13887 }
13888 else if (ARM_RECORD == record_type)
13889 {
13890 arm_record->cond = bits (arm_record->arm_insn, 28, 31);
13891 insn_id = bits (arm_record->arm_insn, 25, 27);
13892 ret = arm_record_extension_space (arm_record);
13893 /* If this insn has fallen into extension space
13894 then we need not decode it anymore. */
13895 if (ret != -1 && !INSN_RECORDED(arm_record))
13896 {
13897 ret = arm_handle_insn[insn_id] (arm_record);
13898 }
13899 }
13900 else if (THUMB_RECORD == record_type)
13901 {
13902 /* As thumb does not have condition codes, we set negative. */
13903 arm_record->cond = -1;
13904 insn_id = bits (arm_record->arm_insn, 13, 15);
13905 ret = thumb_handle_insn[insn_id] (arm_record);
13906 }
13907 else if (THUMB2_RECORD == record_type)
13908 {
c6ec2b30
OJ
13909 /* As thumb does not have condition codes, we set negative. */
13910 arm_record->cond = -1;
13911
13912 /* Swap first half of 32bit thumb instruction with second half. */
13913 arm_record->arm_insn
13914 = (arm_record->arm_insn >> 16) | (arm_record->arm_insn << 16);
13915
13916 insn_id = thumb2_record_decode_insn_handler (arm_record);
13917
13918 if (insn_id != ARM_RECORD_SUCCESS)
13919 {
13920 arm_record_unsupported_insn (arm_record);
13921 ret = -1;
13922 }
72508ac0
PO
13923 }
13924 else
13925 {
13926 /* Throw assertion. */
13927 gdb_assert_not_reached ("not a valid instruction, could not decode");
13928 }
13929
13930 return ret;
13931}
13932
13933
13934/* Cleans up local record registers and memory allocations. */
13935
13936static void
13937deallocate_reg_mem (insn_decode_record *record)
13938{
13939 xfree (record->arm_regs);
13940 xfree (record->arm_mems);
13941}
13942
13943
13944/* Parse the current instruction and record the values of the registers and
13945 memory that will be changed in current instruction to record_arch_list".
13946 Return -1 if something is wrong. */
13947
13948int
13949arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
13950 CORE_ADDR insn_addr)
13951{
13952
13953 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
13954 uint32_t no_of_rec = 0;
13955 uint32_t ret = 0; /* return value: -1:record failure ; 0:success */
13956 ULONGEST t_bit = 0, insn_id = 0;
13957
13958 ULONGEST u_regval = 0;
13959
13960 insn_decode_record arm_record;
13961
13962 memset (&arm_record, 0, sizeof (insn_decode_record));
13963 arm_record.regcache = regcache;
13964 arm_record.this_addr = insn_addr;
13965 arm_record.gdbarch = gdbarch;
13966
13967
13968 if (record_debug > 1)
13969 {
13970 fprintf_unfiltered (gdb_stdlog, "Process record: arm_process_record "
13971 "addr = %s\n",
13972 paddress (gdbarch, arm_record.this_addr));
13973 }
13974
13975 if (extract_arm_insn (&arm_record, 2))
13976 {
13977 if (record_debug)
13978 {
13979 printf_unfiltered (_("Process record: error reading memory at "
13980 "addr %s len = %d.\n"),
13981 paddress (arm_record.gdbarch,
13982 arm_record.this_addr), 2);
13983 }
13984 return -1;
13985 }
13986
13987 /* Check the insn, whether it is thumb or arm one. */
13988
13989 t_bit = arm_psr_thumb_bit (arm_record.gdbarch);
13990 regcache_raw_read_unsigned (arm_record.regcache, ARM_PS_REGNUM, &u_regval);
13991
13992
13993 if (!(u_regval & t_bit))
13994 {
13995 /* We are decoding arm insn. */
13996 ret = decode_insn (&arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES);
13997 }
13998 else
13999 {
14000 insn_id = bits (arm_record.arm_insn, 11, 15);
14001 /* is it thumb2 insn? */
14002 if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id))
14003 {
14004 ret = decode_insn (&arm_record, THUMB2_RECORD,
14005 THUMB2_INSN_SIZE_BYTES);
14006 }
14007 else
14008 {
14009 /* We are decoding thumb insn. */
14010 ret = decode_insn (&arm_record, THUMB_RECORD, THUMB_INSN_SIZE_BYTES);
14011 }
14012 }
14013
14014 if (0 == ret)
14015 {
14016 /* Record registers. */
25ea693b 14017 record_full_arch_list_add_reg (arm_record.regcache, ARM_PC_REGNUM);
72508ac0
PO
14018 if (arm_record.arm_regs)
14019 {
14020 for (no_of_rec = 0; no_of_rec < arm_record.reg_rec_count; no_of_rec++)
14021 {
25ea693b
MM
14022 if (record_full_arch_list_add_reg
14023 (arm_record.regcache , arm_record.arm_regs[no_of_rec]))
72508ac0
PO
14024 ret = -1;
14025 }
14026 }
14027 /* Record memories. */
14028 if (arm_record.arm_mems)
14029 {
14030 for (no_of_rec = 0; no_of_rec < arm_record.mem_rec_count; no_of_rec++)
14031 {
25ea693b 14032 if (record_full_arch_list_add_mem
72508ac0 14033 ((CORE_ADDR)arm_record.arm_mems[no_of_rec].addr,
25ea693b 14034 arm_record.arm_mems[no_of_rec].len))
72508ac0
PO
14035 ret = -1;
14036 }
14037 }
14038
25ea693b 14039 if (record_full_arch_list_add_end ())
72508ac0
PO
14040 ret = -1;
14041 }
14042
14043
14044 deallocate_reg_mem (&arm_record);
14045
14046 return ret;
14047}
14048
This page took 1.753701 seconds and 4 git commands to generate.