]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/arm-tdep.c
Update Copyright year range in all files maintained by GDB.
[thirdparty/binutils-gdb.git] / gdb / arm-tdep.c
CommitLineData
ed9a39eb 1/* Common target dependent code for GDB on ARM systems.
0fd88904 2
ecd75fc8 3 Copyright (C) 1988-2014 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"
26#include "gdbcmd.h"
27#include "gdbcore.h"
0e9f083f 28#include <string.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
RE
47
48#include "arm-tdep.h"
26216b98 49#include "gdb/sim-arm.h"
34e8f22d 50
082fc60d
RE
51#include "elf-bfd.h"
52#include "coff/internal.h"
97e03143 53#include "elf/arm.h"
c906108c 54
26216b98 55#include "gdb_assert.h"
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
db24da6d
YQ
239static int thumb_insn_size (unsigned short inst1);
240
9b8d791a 241struct arm_prologue_cache
c3b4394c 242{
eb5492fa
DJ
243 /* The stack pointer at the time this frame was created; i.e. the
244 caller's stack pointer when this function was called. It is used
245 to identify this frame. */
246 CORE_ADDR prev_sp;
247
4be43953
DJ
248 /* The frame base for this frame is just prev_sp - frame size.
249 FRAMESIZE is the distance from the frame pointer to the
250 initial stack pointer. */
eb5492fa 251
c3b4394c 252 int framesize;
eb5492fa
DJ
253
254 /* The register used to hold the frame pointer for this frame. */
c3b4394c 255 int framereg;
eb5492fa
DJ
256
257 /* Saved register offsets. */
258 struct trad_frame_saved_reg *saved_regs;
c3b4394c 259};
ed9a39eb 260
0d39a070
DJ
261static CORE_ADDR arm_analyze_prologue (struct gdbarch *gdbarch,
262 CORE_ADDR prologue_start,
263 CORE_ADDR prologue_end,
264 struct arm_prologue_cache *cache);
265
cca44b1b
JB
266/* Architecture version for displaced stepping. This effects the behaviour of
267 certain instructions, and really should not be hard-wired. */
268
269#define DISPLACED_STEPPING_ARCH_VERSION 5
270
bc90b915
FN
271/* Addresses for calling Thumb functions have the bit 0 set.
272 Here are some macros to test, set, or clear bit 0 of addresses. */
273#define IS_THUMB_ADDR(addr) ((addr) & 1)
274#define MAKE_THUMB_ADDR(addr) ((addr) | 1)
275#define UNMAKE_THUMB_ADDR(addr) ((addr) & ~1)
276
94c30b78 277/* Set to true if the 32-bit mode is in use. */
c906108c
SS
278
279int arm_apcs_32 = 1;
280
9779414d
DJ
281/* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode. */
282
478fd957 283int
9779414d
DJ
284arm_psr_thumb_bit (struct gdbarch *gdbarch)
285{
286 if (gdbarch_tdep (gdbarch)->is_m)
287 return XPSR_T;
288 else
289 return CPSR_T;
290}
291
b39cc962
DJ
292/* Determine if FRAME is executing in Thumb mode. */
293
25b41d01 294int
b39cc962
DJ
295arm_frame_is_thumb (struct frame_info *frame)
296{
297 CORE_ADDR cpsr;
9779414d 298 ULONGEST t_bit = arm_psr_thumb_bit (get_frame_arch (frame));
b39cc962
DJ
299
300 /* Every ARM frame unwinder can unwind the T bit of the CPSR, either
301 directly (from a signal frame or dummy frame) or by interpreting
302 the saved LR (from a prologue or DWARF frame). So consult it and
303 trust the unwinders. */
304 cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
305
9779414d 306 return (cpsr & t_bit) != 0;
b39cc962
DJ
307}
308
60c5725c
DJ
309/* Callback for VEC_lower_bound. */
310
311static inline int
312arm_compare_mapping_symbols (const struct arm_mapping_symbol *lhs,
313 const struct arm_mapping_symbol *rhs)
314{
315 return lhs->value < rhs->value;
316}
317
f9d67f43
DJ
318/* Search for the mapping symbol covering MEMADDR. If one is found,
319 return its type. Otherwise, return 0. If START is non-NULL,
320 set *START to the location of the mapping symbol. */
c906108c 321
f9d67f43
DJ
322static char
323arm_find_mapping_symbol (CORE_ADDR memaddr, CORE_ADDR *start)
c906108c 324{
60c5725c 325 struct obj_section *sec;
0428b8f5 326
60c5725c
DJ
327 /* If there are mapping symbols, consult them. */
328 sec = find_pc_section (memaddr);
329 if (sec != NULL)
330 {
331 struct arm_per_objfile *data;
332 VEC(arm_mapping_symbol_s) *map;
aded6f54
PA
333 struct arm_mapping_symbol map_key = { memaddr - obj_section_addr (sec),
334 0 };
60c5725c
DJ
335 unsigned int idx;
336
337 data = objfile_data (sec->objfile, arm_objfile_data_key);
338 if (data != NULL)
339 {
340 map = data->section_maps[sec->the_bfd_section->index];
341 if (!VEC_empty (arm_mapping_symbol_s, map))
342 {
343 struct arm_mapping_symbol *map_sym;
344
345 idx = VEC_lower_bound (arm_mapping_symbol_s, map, &map_key,
346 arm_compare_mapping_symbols);
347
348 /* VEC_lower_bound finds the earliest ordered insertion
349 point. If the following symbol starts at this exact
350 address, we use that; otherwise, the preceding
351 mapping symbol covers this address. */
352 if (idx < VEC_length (arm_mapping_symbol_s, map))
353 {
354 map_sym = VEC_index (arm_mapping_symbol_s, map, idx);
355 if (map_sym->value == map_key.value)
f9d67f43
DJ
356 {
357 if (start)
358 *start = map_sym->value + obj_section_addr (sec);
359 return map_sym->type;
360 }
60c5725c
DJ
361 }
362
363 if (idx > 0)
364 {
365 map_sym = VEC_index (arm_mapping_symbol_s, map, idx - 1);
f9d67f43
DJ
366 if (start)
367 *start = map_sym->value + obj_section_addr (sec);
368 return map_sym->type;
60c5725c
DJ
369 }
370 }
371 }
372 }
373
f9d67f43
DJ
374 return 0;
375}
376
377/* Determine if the program counter specified in MEMADDR is in a Thumb
378 function. This function should be called for addresses unrelated to
379 any executing frame; otherwise, prefer arm_frame_is_thumb. */
380
e3039479 381int
9779414d 382arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
f9d67f43 383{
7cbd4a93 384 struct bound_minimal_symbol sym;
f9d67f43 385 char type;
a42244db
YQ
386 struct displaced_step_closure* dsc
387 = get_displaced_step_closure_by_addr(memaddr);
388
389 /* If checking the mode of displaced instruction in copy area, the mode
390 should be determined by instruction on the original address. */
391 if (dsc)
392 {
393 if (debug_displaced)
394 fprintf_unfiltered (gdb_stdlog,
395 "displaced: check mode of %.8lx instead of %.8lx\n",
396 (unsigned long) dsc->insn_addr,
397 (unsigned long) memaddr);
398 memaddr = dsc->insn_addr;
399 }
f9d67f43
DJ
400
401 /* If bit 0 of the address is set, assume this is a Thumb address. */
402 if (IS_THUMB_ADDR (memaddr))
403 return 1;
404
18819fa6
UW
405 /* Respect internal mode override if active. */
406 if (arm_override_mode != -1)
407 return arm_override_mode;
408
f9d67f43
DJ
409 /* If the user wants to override the symbol table, let him. */
410 if (strcmp (arm_force_mode_string, "arm") == 0)
411 return 0;
412 if (strcmp (arm_force_mode_string, "thumb") == 0)
413 return 1;
414
9779414d
DJ
415 /* ARM v6-M and v7-M are always in Thumb mode. */
416 if (gdbarch_tdep (gdbarch)->is_m)
417 return 1;
418
f9d67f43
DJ
419 /* If there are mapping symbols, consult them. */
420 type = arm_find_mapping_symbol (memaddr, NULL);
421 if (type)
422 return type == 't';
423
ed9a39eb 424 /* Thumb functions have a "special" bit set in minimal symbols. */
c906108c 425 sym = lookup_minimal_symbol_by_pc (memaddr);
7cbd4a93
TT
426 if (sym.minsym)
427 return (MSYMBOL_IS_SPECIAL (sym.minsym));
0428b8f5
DJ
428
429 /* If the user wants to override the fallback mode, let them. */
430 if (strcmp (arm_fallback_mode_string, "arm") == 0)
431 return 0;
432 if (strcmp (arm_fallback_mode_string, "thumb") == 0)
433 return 1;
434
435 /* If we couldn't find any symbol, but we're talking to a running
436 target, then trust the current value of $cpsr. This lets
437 "display/i $pc" always show the correct mode (though if there is
438 a symbol table we will not reach here, so it still may not be
18819fa6 439 displayed in the mode it will be executed). */
0428b8f5 440 if (target_has_registers)
18819fa6 441 return arm_frame_is_thumb (get_current_frame ());
0428b8f5
DJ
442
443 /* Otherwise we're out of luck; we assume ARM. */
444 return 0;
c906108c
SS
445}
446
181c1381 447/* Remove useless bits from addresses in a running program. */
34e8f22d 448static CORE_ADDR
24568a2c 449arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
c906108c 450{
2ae28aa9
YQ
451 /* On M-profile devices, do not strip the low bit from EXC_RETURN
452 (the magic exception return address). */
453 if (gdbarch_tdep (gdbarch)->is_m
454 && (val & 0xfffffff0) == 0xfffffff0)
455 return val;
456
a3a2ee65 457 if (arm_apcs_32)
dd6be234 458 return UNMAKE_THUMB_ADDR (val);
c906108c 459 else
a3a2ee65 460 return (val & 0x03fffffc);
c906108c
SS
461}
462
0d39a070 463/* Return 1 if PC is the start of a compiler helper function which
e0634ccf
UW
464 can be safely ignored during prologue skipping. IS_THUMB is true
465 if the function is known to be a Thumb function due to the way it
466 is being called. */
0d39a070 467static int
e0634ccf 468skip_prologue_function (struct gdbarch *gdbarch, CORE_ADDR pc, int is_thumb)
0d39a070 469{
e0634ccf 470 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7cbd4a93 471 struct bound_minimal_symbol msym;
0d39a070
DJ
472
473 msym = lookup_minimal_symbol_by_pc (pc);
7cbd4a93
TT
474 if (msym.minsym != NULL
475 && SYMBOL_VALUE_ADDRESS (msym.minsym) == pc
476 && SYMBOL_LINKAGE_NAME (msym.minsym) != NULL)
e0634ccf 477 {
7cbd4a93 478 const char *name = SYMBOL_LINKAGE_NAME (msym.minsym);
0d39a070 479
e0634ccf
UW
480 /* The GNU linker's Thumb call stub to foo is named
481 __foo_from_thumb. */
482 if (strstr (name, "_from_thumb") != NULL)
483 name += 2;
0d39a070 484
e0634ccf
UW
485 /* On soft-float targets, __truncdfsf2 is called to convert promoted
486 arguments to their argument types in non-prototyped
487 functions. */
488 if (strncmp (name, "__truncdfsf2", strlen ("__truncdfsf2")) == 0)
489 return 1;
490 if (strncmp (name, "__aeabi_d2f", strlen ("__aeabi_d2f")) == 0)
491 return 1;
0d39a070 492
e0634ccf
UW
493 /* Internal functions related to thread-local storage. */
494 if (strncmp (name, "__tls_get_addr", strlen ("__tls_get_addr")) == 0)
495 return 1;
496 if (strncmp (name, "__aeabi_read_tp", strlen ("__aeabi_read_tp")) == 0)
497 return 1;
498 }
499 else
500 {
501 /* If we run against a stripped glibc, we may be unable to identify
502 special functions by name. Check for one important case,
503 __aeabi_read_tp, by comparing the *code* against the default
504 implementation (this is hand-written ARM assembler in glibc). */
505
506 if (!is_thumb
507 && read_memory_unsigned_integer (pc, 4, byte_order_for_code)
508 == 0xe3e00a0f /* mov r0, #0xffff0fff */
509 && read_memory_unsigned_integer (pc + 4, 4, byte_order_for_code)
510 == 0xe240f01f) /* sub pc, r0, #31 */
511 return 1;
512 }
ec3d575a 513
0d39a070
DJ
514 return 0;
515}
516
517/* Support routines for instruction parsing. */
518#define submask(x) ((1L << ((x) + 1)) - 1)
519#define bit(obj,st) (((obj) >> (st)) & 1)
520#define bits(obj,st,fn) (((obj) >> (st)) & submask ((fn) - (st)))
521#define sbits(obj,st,fn) \
522 ((long) (bits(obj,st,fn) | ((long) bit(obj,fn) * ~ submask (fn - st))))
523#define BranchDest(addr,instr) \
9991b207 524 ((CORE_ADDR) (((unsigned long) (addr)) + 8 + (sbits (instr, 0, 23) << 2)))
0d39a070 525
621c6d5b
YQ
526/* Extract the immediate from instruction movw/movt of encoding T. INSN1 is
527 the first 16-bit of instruction, and INSN2 is the second 16-bit of
528 instruction. */
529#define EXTRACT_MOVW_MOVT_IMM_T(insn1, insn2) \
530 ((bits ((insn1), 0, 3) << 12) \
531 | (bits ((insn1), 10, 10) << 11) \
532 | (bits ((insn2), 12, 14) << 8) \
533 | bits ((insn2), 0, 7))
534
535/* Extract the immediate from instruction movw/movt of encoding A. INSN is
536 the 32-bit instruction. */
537#define EXTRACT_MOVW_MOVT_IMM_A(insn) \
538 ((bits ((insn), 16, 19) << 12) \
539 | bits ((insn), 0, 11))
540
ec3d575a
UW
541/* Decode immediate value; implements ThumbExpandImmediate pseudo-op. */
542
543static unsigned int
544thumb_expand_immediate (unsigned int imm)
545{
546 unsigned int count = imm >> 7;
547
548 if (count < 8)
549 switch (count / 2)
550 {
551 case 0:
552 return imm & 0xff;
553 case 1:
554 return (imm & 0xff) | ((imm & 0xff) << 16);
555 case 2:
556 return ((imm & 0xff) << 8) | ((imm & 0xff) << 24);
557 case 3:
558 return (imm & 0xff) | ((imm & 0xff) << 8)
559 | ((imm & 0xff) << 16) | ((imm & 0xff) << 24);
560 }
561
562 return (0x80 | (imm & 0x7f)) << (32 - count);
563}
564
565/* Return 1 if the 16-bit Thumb instruction INST might change
566 control flow, 0 otherwise. */
567
568static int
569thumb_instruction_changes_pc (unsigned short inst)
570{
571 if ((inst & 0xff00) == 0xbd00) /* pop {rlist, pc} */
572 return 1;
573
574 if ((inst & 0xf000) == 0xd000) /* conditional branch */
575 return 1;
576
577 if ((inst & 0xf800) == 0xe000) /* unconditional branch */
578 return 1;
579
580 if ((inst & 0xff00) == 0x4700) /* bx REG, blx REG */
581 return 1;
582
ad8b5167
UW
583 if ((inst & 0xff87) == 0x4687) /* mov pc, REG */
584 return 1;
585
ec3d575a
UW
586 if ((inst & 0xf500) == 0xb100) /* CBNZ or CBZ. */
587 return 1;
588
589 return 0;
590}
591
592/* Return 1 if the 32-bit Thumb instruction in INST1 and INST2
593 might change control flow, 0 otherwise. */
594
595static int
596thumb2_instruction_changes_pc (unsigned short inst1, unsigned short inst2)
597{
598 if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
599 {
600 /* Branches and miscellaneous control instructions. */
601
602 if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
603 {
604 /* B, BL, BLX. */
605 return 1;
606 }
607 else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
608 {
609 /* SUBS PC, LR, #imm8. */
610 return 1;
611 }
612 else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
613 {
614 /* Conditional branch. */
615 return 1;
616 }
617
618 return 0;
619 }
620
621 if ((inst1 & 0xfe50) == 0xe810)
622 {
623 /* Load multiple or RFE. */
624
625 if (bit (inst1, 7) && !bit (inst1, 8))
626 {
627 /* LDMIA or POP */
628 if (bit (inst2, 15))
629 return 1;
630 }
631 else if (!bit (inst1, 7) && bit (inst1, 8))
632 {
633 /* LDMDB */
634 if (bit (inst2, 15))
635 return 1;
636 }
637 else if (bit (inst1, 7) && bit (inst1, 8))
638 {
639 /* RFEIA */
640 return 1;
641 }
642 else if (!bit (inst1, 7) && !bit (inst1, 8))
643 {
644 /* RFEDB */
645 return 1;
646 }
647
648 return 0;
649 }
650
651 if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
652 {
653 /* MOV PC or MOVS PC. */
654 return 1;
655 }
656
657 if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
658 {
659 /* LDR PC. */
660 if (bits (inst1, 0, 3) == 15)
661 return 1;
662 if (bit (inst1, 7))
663 return 1;
664 if (bit (inst2, 11))
665 return 1;
666 if ((inst2 & 0x0fc0) == 0x0000)
667 return 1;
668
669 return 0;
670 }
671
672 if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
673 {
674 /* TBB. */
675 return 1;
676 }
677
678 if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
679 {
680 /* TBH. */
681 return 1;
682 }
683
684 return 0;
685}
686
29d73ae4
DJ
687/* Analyze a Thumb prologue, looking for a recognizable stack frame
688 and frame pointer. Scan until we encounter a store that could
0d39a070
DJ
689 clobber the stack frame unexpectedly, or an unknown instruction.
690 Return the last address which is definitely safe to skip for an
691 initial breakpoint. */
c906108c
SS
692
693static CORE_ADDR
29d73ae4
DJ
694thumb_analyze_prologue (struct gdbarch *gdbarch,
695 CORE_ADDR start, CORE_ADDR limit,
696 struct arm_prologue_cache *cache)
c906108c 697{
0d39a070 698 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
e17a4113 699 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
29d73ae4
DJ
700 int i;
701 pv_t regs[16];
702 struct pv_area *stack;
703 struct cleanup *back_to;
704 CORE_ADDR offset;
ec3d575a 705 CORE_ADDR unrecognized_pc = 0;
da3c6d4a 706
29d73ae4
DJ
707 for (i = 0; i < 16; i++)
708 regs[i] = pv_register (i, 0);
55f960e1 709 stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
29d73ae4
DJ
710 back_to = make_cleanup_free_pv_area (stack);
711
29d73ae4 712 while (start < limit)
c906108c 713 {
29d73ae4
DJ
714 unsigned short insn;
715
e17a4113 716 insn = read_memory_unsigned_integer (start, 2, byte_order_for_code);
9d4fde75 717
94c30b78 718 if ((insn & 0xfe00) == 0xb400) /* push { rlist } */
da59e081 719 {
29d73ae4
DJ
720 int regno;
721 int mask;
4be43953
DJ
722
723 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
724 break;
29d73ae4
DJ
725
726 /* Bits 0-7 contain a mask for registers R0-R7. Bit 8 says
727 whether to save LR (R14). */
728 mask = (insn & 0xff) | ((insn & 0x100) << 6);
729
730 /* Calculate offsets of saved R0-R7 and LR. */
731 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
732 if (mask & (1 << regno))
733 {
29d73ae4
DJ
734 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
735 -4);
736 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
737 }
da59e081 738 }
da3c6d4a
MS
739 else if ((insn & 0xff00) == 0xb000) /* add sp, #simm OR
740 sub sp, #simm */
da59e081 741 {
29d73ae4
DJ
742 offset = (insn & 0x7f) << 2; /* get scaled offset */
743 if (insn & 0x80) /* Check for SUB. */
744 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
745 -offset);
da59e081 746 else
29d73ae4
DJ
747 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
748 offset);
da59e081 749 }
0d39a070
DJ
750 else if ((insn & 0xf800) == 0xa800) /* add Rd, sp, #imm */
751 regs[bits (insn, 8, 10)] = pv_add_constant (regs[ARM_SP_REGNUM],
752 (insn & 0xff) << 2);
753 else if ((insn & 0xfe00) == 0x1c00 /* add Rd, Rn, #imm */
754 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
755 regs[bits (insn, 0, 2)] = pv_add_constant (regs[bits (insn, 3, 5)],
756 bits (insn, 6, 8));
757 else if ((insn & 0xf800) == 0x3000 /* add Rd, #imm */
758 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
759 regs[bits (insn, 8, 10)] = pv_add_constant (regs[bits (insn, 8, 10)],
760 bits (insn, 0, 7));
761 else if ((insn & 0xfe00) == 0x1800 /* add Rd, Rn, Rm */
762 && pv_is_register (regs[bits (insn, 6, 8)], ARM_SP_REGNUM)
763 && pv_is_constant (regs[bits (insn, 3, 5)]))
764 regs[bits (insn, 0, 2)] = pv_add (regs[bits (insn, 3, 5)],
765 regs[bits (insn, 6, 8)]);
766 else if ((insn & 0xff00) == 0x4400 /* add Rd, Rm */
767 && pv_is_constant (regs[bits (insn, 3, 6)]))
768 {
769 int rd = (bit (insn, 7) << 3) + bits (insn, 0, 2);
770 int rm = bits (insn, 3, 6);
771 regs[rd] = pv_add (regs[rd], regs[rm]);
772 }
29d73ae4 773 else if ((insn & 0xff00) == 0x4600) /* mov hi, lo or mov lo, hi */
da59e081 774 {
29d73ae4
DJ
775 int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
776 int src_reg = (insn & 0x78) >> 3;
777 regs[dst_reg] = regs[src_reg];
da59e081 778 }
29d73ae4 779 else if ((insn & 0xf800) == 0x9000) /* str rd, [sp, #off] */
da59e081 780 {
29d73ae4
DJ
781 /* Handle stores to the stack. Normally pushes are used,
782 but with GCC -mtpcs-frame, there may be other stores
783 in the prologue to create the frame. */
784 int regno = (insn >> 8) & 0x7;
785 pv_t addr;
786
787 offset = (insn & 0xff) << 2;
788 addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
789
790 if (pv_area_store_would_trash (stack, addr))
791 break;
792
793 pv_area_store (stack, addr, 4, regs[regno]);
da59e081 794 }
0d39a070
DJ
795 else if ((insn & 0xf800) == 0x6000) /* str rd, [rn, #off] */
796 {
797 int rd = bits (insn, 0, 2);
798 int rn = bits (insn, 3, 5);
799 pv_t addr;
800
801 offset = bits (insn, 6, 10) << 2;
802 addr = pv_add_constant (regs[rn], offset);
803
804 if (pv_area_store_would_trash (stack, addr))
805 break;
806
807 pv_area_store (stack, addr, 4, regs[rd]);
808 }
809 else if (((insn & 0xf800) == 0x7000 /* strb Rd, [Rn, #off] */
810 || (insn & 0xf800) == 0x8000) /* strh Rd, [Rn, #off] */
811 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
812 /* Ignore stores of argument registers to the stack. */
813 ;
814 else if ((insn & 0xf800) == 0xc800 /* ldmia Rn!, { registers } */
815 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
816 /* Ignore block loads from the stack, potentially copying
817 parameters from memory. */
818 ;
819 else if ((insn & 0xf800) == 0x9800 /* ldr Rd, [Rn, #immed] */
820 || ((insn & 0xf800) == 0x6800 /* ldr Rd, [sp, #immed] */
821 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM)))
822 /* Similarly ignore single loads from the stack. */
823 ;
824 else if ((insn & 0xffc0) == 0x0000 /* lsls Rd, Rm, #0 */
825 || (insn & 0xffc0) == 0x1c00) /* add Rd, Rn, #0 */
826 /* Skip register copies, i.e. saves to another register
827 instead of the stack. */
828 ;
829 else if ((insn & 0xf800) == 0x2000) /* movs Rd, #imm */
830 /* Recognize constant loads; even with small stacks these are necessary
831 on Thumb. */
832 regs[bits (insn, 8, 10)] = pv_constant (bits (insn, 0, 7));
833 else if ((insn & 0xf800) == 0x4800) /* ldr Rd, [pc, #imm] */
834 {
835 /* Constant pool loads, for the same reason. */
836 unsigned int constant;
837 CORE_ADDR loc;
838
839 loc = start + 4 + bits (insn, 0, 7) * 4;
840 constant = read_memory_unsigned_integer (loc, 4, byte_order);
841 regs[bits (insn, 8, 10)] = pv_constant (constant);
842 }
db24da6d 843 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instructions. */
0d39a070 844 {
0d39a070
DJ
845 unsigned short inst2;
846
847 inst2 = read_memory_unsigned_integer (start + 2, 2,
848 byte_order_for_code);
849
850 if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
851 {
852 /* BL, BLX. Allow some special function calls when
853 skipping the prologue; GCC generates these before
854 storing arguments to the stack. */
855 CORE_ADDR nextpc;
856 int j1, j2, imm1, imm2;
857
858 imm1 = sbits (insn, 0, 10);
859 imm2 = bits (inst2, 0, 10);
860 j1 = bit (inst2, 13);
861 j2 = bit (inst2, 11);
862
863 offset = ((imm1 << 12) + (imm2 << 1));
864 offset ^= ((!j2) << 22) | ((!j1) << 23);
865
866 nextpc = start + 4 + offset;
867 /* For BLX make sure to clear the low bits. */
868 if (bit (inst2, 12) == 0)
869 nextpc = nextpc & 0xfffffffc;
870
e0634ccf
UW
871 if (!skip_prologue_function (gdbarch, nextpc,
872 bit (inst2, 12) != 0))
0d39a070
DJ
873 break;
874 }
ec3d575a 875
0963b4bd
MS
876 else if ((insn & 0xffd0) == 0xe900 /* stmdb Rn{!},
877 { registers } */
ec3d575a
UW
878 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
879 {
880 pv_t addr = regs[bits (insn, 0, 3)];
881 int regno;
882
883 if (pv_area_store_would_trash (stack, addr))
884 break;
885
886 /* Calculate offsets of saved registers. */
887 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
888 if (inst2 & (1 << regno))
889 {
890 addr = pv_add_constant (addr, -4);
891 pv_area_store (stack, addr, 4, regs[regno]);
892 }
893
894 if (insn & 0x0020)
895 regs[bits (insn, 0, 3)] = addr;
896 }
897
0963b4bd
MS
898 else if ((insn & 0xff50) == 0xe940 /* strd Rt, Rt2,
899 [Rn, #+/-imm]{!} */
ec3d575a
UW
900 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
901 {
902 int regno1 = bits (inst2, 12, 15);
903 int regno2 = bits (inst2, 8, 11);
904 pv_t addr = regs[bits (insn, 0, 3)];
905
906 offset = inst2 & 0xff;
907 if (insn & 0x0080)
908 addr = pv_add_constant (addr, offset);
909 else
910 addr = pv_add_constant (addr, -offset);
911
912 if (pv_area_store_would_trash (stack, addr))
913 break;
914
915 pv_area_store (stack, addr, 4, regs[regno1]);
916 pv_area_store (stack, pv_add_constant (addr, 4),
917 4, regs[regno2]);
918
919 if (insn & 0x0020)
920 regs[bits (insn, 0, 3)] = addr;
921 }
922
923 else if ((insn & 0xfff0) == 0xf8c0 /* str Rt,[Rn,+/-#imm]{!} */
924 && (inst2 & 0x0c00) == 0x0c00
925 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
926 {
927 int regno = bits (inst2, 12, 15);
928 pv_t addr = regs[bits (insn, 0, 3)];
929
930 offset = inst2 & 0xff;
931 if (inst2 & 0x0200)
932 addr = pv_add_constant (addr, offset);
933 else
934 addr = pv_add_constant (addr, -offset);
935
936 if (pv_area_store_would_trash (stack, addr))
937 break;
938
939 pv_area_store (stack, addr, 4, regs[regno]);
940
941 if (inst2 & 0x0100)
942 regs[bits (insn, 0, 3)] = addr;
943 }
944
945 else if ((insn & 0xfff0) == 0xf8c0 /* str.w Rt,[Rn,#imm] */
946 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
947 {
948 int regno = bits (inst2, 12, 15);
949 pv_t addr;
950
951 offset = inst2 & 0xfff;
952 addr = pv_add_constant (regs[bits (insn, 0, 3)], offset);
953
954 if (pv_area_store_would_trash (stack, addr))
955 break;
956
957 pv_area_store (stack, addr, 4, regs[regno]);
958 }
959
960 else if ((insn & 0xffd0) == 0xf880 /* str{bh}.w Rt,[Rn,#imm] */
0d39a070 961 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 962 /* Ignore stores of argument registers to the stack. */
0d39a070 963 ;
ec3d575a
UW
964
965 else if ((insn & 0xffd0) == 0xf800 /* str{bh} Rt,[Rn,#+/-imm] */
966 && (inst2 & 0x0d00) == 0x0c00
0d39a070 967 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 968 /* Ignore stores of argument registers to the stack. */
0d39a070 969 ;
ec3d575a 970
0963b4bd
MS
971 else if ((insn & 0xffd0) == 0xe890 /* ldmia Rn[!],
972 { registers } */
ec3d575a
UW
973 && (inst2 & 0x8000) == 0x0000
974 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
975 /* Ignore block loads from the stack, potentially copying
976 parameters from memory. */
0d39a070 977 ;
ec3d575a 978
0963b4bd
MS
979 else if ((insn & 0xffb0) == 0xe950 /* ldrd Rt, Rt2,
980 [Rn, #+/-imm] */
0d39a070 981 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 982 /* Similarly ignore dual loads from the stack. */
0d39a070 983 ;
ec3d575a
UW
984
985 else if ((insn & 0xfff0) == 0xf850 /* ldr Rt,[Rn,#+/-imm] */
986 && (inst2 & 0x0d00) == 0x0c00
0d39a070 987 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 988 /* Similarly ignore single loads from the stack. */
0d39a070 989 ;
ec3d575a
UW
990
991 else if ((insn & 0xfff0) == 0xf8d0 /* ldr.w Rt,[Rn,#imm] */
0d39a070 992 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 993 /* Similarly ignore single loads from the stack. */
0d39a070 994 ;
ec3d575a
UW
995
996 else if ((insn & 0xfbf0) == 0xf100 /* add.w Rd, Rn, #imm */
997 && (inst2 & 0x8000) == 0x0000)
998 {
999 unsigned int imm = ((bits (insn, 10, 10) << 11)
1000 | (bits (inst2, 12, 14) << 8)
1001 | bits (inst2, 0, 7));
1002
1003 regs[bits (inst2, 8, 11)]
1004 = pv_add_constant (regs[bits (insn, 0, 3)],
1005 thumb_expand_immediate (imm));
1006 }
1007
1008 else if ((insn & 0xfbf0) == 0xf200 /* addw Rd, Rn, #imm */
1009 && (inst2 & 0x8000) == 0x0000)
0d39a070 1010 {
ec3d575a
UW
1011 unsigned int imm = ((bits (insn, 10, 10) << 11)
1012 | (bits (inst2, 12, 14) << 8)
1013 | bits (inst2, 0, 7));
1014
1015 regs[bits (inst2, 8, 11)]
1016 = pv_add_constant (regs[bits (insn, 0, 3)], imm);
1017 }
1018
1019 else if ((insn & 0xfbf0) == 0xf1a0 /* sub.w Rd, Rn, #imm */
1020 && (inst2 & 0x8000) == 0x0000)
1021 {
1022 unsigned int imm = ((bits (insn, 10, 10) << 11)
1023 | (bits (inst2, 12, 14) << 8)
1024 | bits (inst2, 0, 7));
1025
1026 regs[bits (inst2, 8, 11)]
1027 = pv_add_constant (regs[bits (insn, 0, 3)],
1028 - (CORE_ADDR) thumb_expand_immediate (imm));
1029 }
1030
1031 else if ((insn & 0xfbf0) == 0xf2a0 /* subw Rd, Rn, #imm */
1032 && (inst2 & 0x8000) == 0x0000)
1033 {
1034 unsigned int imm = ((bits (insn, 10, 10) << 11)
1035 | (bits (inst2, 12, 14) << 8)
1036 | bits (inst2, 0, 7));
1037
1038 regs[bits (inst2, 8, 11)]
1039 = pv_add_constant (regs[bits (insn, 0, 3)], - (CORE_ADDR) imm);
1040 }
1041
1042 else if ((insn & 0xfbff) == 0xf04f) /* mov.w Rd, #const */
1043 {
1044 unsigned int imm = ((bits (insn, 10, 10) << 11)
1045 | (bits (inst2, 12, 14) << 8)
1046 | bits (inst2, 0, 7));
1047
1048 regs[bits (inst2, 8, 11)]
1049 = pv_constant (thumb_expand_immediate (imm));
1050 }
1051
1052 else if ((insn & 0xfbf0) == 0xf240) /* movw Rd, #const */
1053 {
621c6d5b
YQ
1054 unsigned int imm
1055 = EXTRACT_MOVW_MOVT_IMM_T (insn, inst2);
ec3d575a
UW
1056
1057 regs[bits (inst2, 8, 11)] = pv_constant (imm);
1058 }
1059
1060 else if (insn == 0xea5f /* mov.w Rd,Rm */
1061 && (inst2 & 0xf0f0) == 0)
1062 {
1063 int dst_reg = (inst2 & 0x0f00) >> 8;
1064 int src_reg = inst2 & 0xf;
1065 regs[dst_reg] = regs[src_reg];
1066 }
1067
1068 else if ((insn & 0xff7f) == 0xf85f) /* ldr.w Rt,<label> */
1069 {
1070 /* Constant pool loads. */
1071 unsigned int constant;
1072 CORE_ADDR loc;
1073
1074 offset = bits (insn, 0, 11);
1075 if (insn & 0x0080)
1076 loc = start + 4 + offset;
1077 else
1078 loc = start + 4 - offset;
1079
1080 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1081 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1082 }
1083
1084 else if ((insn & 0xff7f) == 0xe95f) /* ldrd Rt,Rt2,<label> */
1085 {
1086 /* Constant pool loads. */
1087 unsigned int constant;
1088 CORE_ADDR loc;
1089
1090 offset = bits (insn, 0, 7) << 2;
1091 if (insn & 0x0080)
1092 loc = start + 4 + offset;
1093 else
1094 loc = start + 4 - offset;
1095
1096 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1097 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1098
1099 constant = read_memory_unsigned_integer (loc + 4, 4, byte_order);
1100 regs[bits (inst2, 8, 11)] = pv_constant (constant);
1101 }
1102
1103 else if (thumb2_instruction_changes_pc (insn, inst2))
1104 {
1105 /* Don't scan past anything that might change control flow. */
0d39a070
DJ
1106 break;
1107 }
ec3d575a
UW
1108 else
1109 {
1110 /* The optimizer might shove anything into the prologue,
1111 so we just skip what we don't recognize. */
1112 unrecognized_pc = start;
1113 }
0d39a070
DJ
1114
1115 start += 2;
1116 }
ec3d575a 1117 else if (thumb_instruction_changes_pc (insn))
3d74b771 1118 {
ec3d575a 1119 /* Don't scan past anything that might change control flow. */
da3c6d4a 1120 break;
3d74b771 1121 }
ec3d575a
UW
1122 else
1123 {
1124 /* The optimizer might shove anything into the prologue,
1125 so we just skip what we don't recognize. */
1126 unrecognized_pc = start;
1127 }
29d73ae4
DJ
1128
1129 start += 2;
c906108c
SS
1130 }
1131
0d39a070
DJ
1132 if (arm_debug)
1133 fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1134 paddress (gdbarch, start));
1135
ec3d575a
UW
1136 if (unrecognized_pc == 0)
1137 unrecognized_pc = start;
1138
29d73ae4
DJ
1139 if (cache == NULL)
1140 {
1141 do_cleanups (back_to);
ec3d575a 1142 return unrecognized_pc;
29d73ae4
DJ
1143 }
1144
29d73ae4
DJ
1145 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1146 {
1147 /* Frame pointer is fp. Frame size is constant. */
1148 cache->framereg = ARM_FP_REGNUM;
1149 cache->framesize = -regs[ARM_FP_REGNUM].k;
1150 }
1151 else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
1152 {
1153 /* Frame pointer is r7. Frame size is constant. */
1154 cache->framereg = THUMB_FP_REGNUM;
1155 cache->framesize = -regs[THUMB_FP_REGNUM].k;
1156 }
72a2e3dc 1157 else
29d73ae4
DJ
1158 {
1159 /* Try the stack pointer... this is a bit desperate. */
1160 cache->framereg = ARM_SP_REGNUM;
1161 cache->framesize = -regs[ARM_SP_REGNUM].k;
1162 }
29d73ae4
DJ
1163
1164 for (i = 0; i < 16; i++)
1165 if (pv_area_find_reg (stack, gdbarch, i, &offset))
1166 cache->saved_regs[i].addr = offset;
1167
1168 do_cleanups (back_to);
ec3d575a 1169 return unrecognized_pc;
c906108c
SS
1170}
1171
621c6d5b
YQ
1172
1173/* Try to analyze the instructions starting from PC, which load symbol
1174 __stack_chk_guard. Return the address of instruction after loading this
1175 symbol, set the dest register number to *BASEREG, and set the size of
1176 instructions for loading symbol in OFFSET. Return 0 if instructions are
1177 not recognized. */
1178
1179static CORE_ADDR
1180arm_analyze_load_stack_chk_guard(CORE_ADDR pc, struct gdbarch *gdbarch,
1181 unsigned int *destreg, int *offset)
1182{
1183 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1184 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1185 unsigned int low, high, address;
1186
1187 address = 0;
1188 if (is_thumb)
1189 {
1190 unsigned short insn1
1191 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
1192
1193 if ((insn1 & 0xf800) == 0x4800) /* ldr Rd, #immed */
1194 {
1195 *destreg = bits (insn1, 8, 10);
1196 *offset = 2;
1197 address = bits (insn1, 0, 7);
1198 }
1199 else if ((insn1 & 0xfbf0) == 0xf240) /* movw Rd, #const */
1200 {
1201 unsigned short insn2
1202 = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);
1203
1204 low = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1205
1206 insn1
1207 = read_memory_unsigned_integer (pc + 4, 2, byte_order_for_code);
1208 insn2
1209 = read_memory_unsigned_integer (pc + 6, 2, byte_order_for_code);
1210
1211 /* movt Rd, #const */
1212 if ((insn1 & 0xfbc0) == 0xf2c0)
1213 {
1214 high = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1215 *destreg = bits (insn2, 8, 11);
1216 *offset = 8;
1217 address = (high << 16 | low);
1218 }
1219 }
1220 }
1221 else
1222 {
2e9e421f
UW
1223 unsigned int insn
1224 = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
1225
1226 if ((insn & 0x0e5f0000) == 0x041f0000) /* ldr Rd, #immed */
1227 {
1228 address = bits (insn, 0, 11);
1229 *destreg = bits (insn, 12, 15);
1230 *offset = 4;
1231 }
1232 else if ((insn & 0x0ff00000) == 0x03000000) /* movw Rd, #const */
1233 {
1234 low = EXTRACT_MOVW_MOVT_IMM_A (insn);
1235
1236 insn
1237 = read_memory_unsigned_integer (pc + 4, 4, byte_order_for_code);
1238
1239 if ((insn & 0x0ff00000) == 0x03400000) /* movt Rd, #const */
1240 {
1241 high = EXTRACT_MOVW_MOVT_IMM_A (insn);
1242 *destreg = bits (insn, 12, 15);
1243 *offset = 8;
1244 address = (high << 16 | low);
1245 }
1246 }
621c6d5b
YQ
1247 }
1248
1249 return address;
1250}
1251
1252/* Try to skip a sequence of instructions used for stack protector. If PC
0963b4bd
MS
1253 points to the first instruction of this sequence, return the address of
1254 first instruction after this sequence, otherwise, return original PC.
621c6d5b
YQ
1255
1256 On arm, this sequence of instructions is composed of mainly three steps,
1257 Step 1: load symbol __stack_chk_guard,
1258 Step 2: load from address of __stack_chk_guard,
1259 Step 3: store it to somewhere else.
1260
1261 Usually, instructions on step 2 and step 3 are the same on various ARM
1262 architectures. On step 2, it is one instruction 'ldr Rx, [Rn, #0]', and
1263 on step 3, it is also one instruction 'str Rx, [r7, #immd]'. However,
1264 instructions in step 1 vary from different ARM architectures. On ARMv7,
1265 they are,
1266
1267 movw Rn, #:lower16:__stack_chk_guard
1268 movt Rn, #:upper16:__stack_chk_guard
1269
1270 On ARMv5t, it is,
1271
1272 ldr Rn, .Label
1273 ....
1274 .Lable:
1275 .word __stack_chk_guard
1276
1277 Since ldr/str is a very popular instruction, we can't use them as
1278 'fingerprint' or 'signature' of stack protector sequence. Here we choose
1279 sequence {movw/movt, ldr}/ldr/str plus symbol __stack_chk_guard, if not
1280 stripped, as the 'fingerprint' of a stack protector cdoe sequence. */
1281
1282static CORE_ADDR
1283arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
1284{
1285 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
22e048c9 1286 unsigned int basereg;
7cbd4a93 1287 struct bound_minimal_symbol stack_chk_guard;
621c6d5b
YQ
1288 int offset;
1289 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1290 CORE_ADDR addr;
1291
1292 /* Try to parse the instructions in Step 1. */
1293 addr = arm_analyze_load_stack_chk_guard (pc, gdbarch,
1294 &basereg, &offset);
1295 if (!addr)
1296 return pc;
1297
1298 stack_chk_guard = lookup_minimal_symbol_by_pc (addr);
1299 /* If name of symbol doesn't start with '__stack_chk_guard', this
1300 instruction sequence is not for stack protector. If symbol is
1301 removed, we conservatively think this sequence is for stack protector. */
7cbd4a93
TT
1302 if (stack_chk_guard.minsym
1303 && strncmp (SYMBOL_LINKAGE_NAME (stack_chk_guard.minsym),
1304 "__stack_chk_guard",
c1c2ab58 1305 strlen ("__stack_chk_guard")) != 0)
621c6d5b
YQ
1306 return pc;
1307
1308 if (is_thumb)
1309 {
1310 unsigned int destreg;
1311 unsigned short insn
1312 = read_memory_unsigned_integer (pc + offset, 2, byte_order_for_code);
1313
1314 /* Step 2: ldr Rd, [Rn, #immed], encoding T1. */
1315 if ((insn & 0xf800) != 0x6800)
1316 return pc;
1317 if (bits (insn, 3, 5) != basereg)
1318 return pc;
1319 destreg = bits (insn, 0, 2);
1320
1321 insn = read_memory_unsigned_integer (pc + offset + 2, 2,
1322 byte_order_for_code);
1323 /* Step 3: str Rd, [Rn, #immed], encoding T1. */
1324 if ((insn & 0xf800) != 0x6000)
1325 return pc;
1326 if (destreg != bits (insn, 0, 2))
1327 return pc;
1328 }
1329 else
1330 {
1331 unsigned int destreg;
1332 unsigned int insn
1333 = read_memory_unsigned_integer (pc + offset, 4, byte_order_for_code);
1334
1335 /* Step 2: ldr Rd, [Rn, #immed], encoding A1. */
1336 if ((insn & 0x0e500000) != 0x04100000)
1337 return pc;
1338 if (bits (insn, 16, 19) != basereg)
1339 return pc;
1340 destreg = bits (insn, 12, 15);
1341 /* Step 3: str Rd, [Rn, #immed], encoding A1. */
1342 insn = read_memory_unsigned_integer (pc + offset + 4,
1343 4, byte_order_for_code);
1344 if ((insn & 0x0e500000) != 0x04000000)
1345 return pc;
1346 if (bits (insn, 12, 15) != destreg)
1347 return pc;
1348 }
1349 /* The size of total two instructions ldr/str is 4 on Thumb-2, while 8
1350 on arm. */
1351 if (is_thumb)
1352 return pc + offset + 4;
1353 else
1354 return pc + offset + 8;
1355}
1356
da3c6d4a
MS
1357/* Advance the PC across any function entry prologue instructions to
1358 reach some "real" code.
34e8f22d
RE
1359
1360 The APCS (ARM Procedure Call Standard) defines the following
ed9a39eb 1361 prologue:
c906108c 1362
c5aa993b
JM
1363 mov ip, sp
1364 [stmfd sp!, {a1,a2,a3,a4}]
1365 stmfd sp!, {...,fp,ip,lr,pc}
ed9a39eb
JM
1366 [stfe f7, [sp, #-12]!]
1367 [stfe f6, [sp, #-12]!]
1368 [stfe f5, [sp, #-12]!]
1369 [stfe f4, [sp, #-12]!]
0963b4bd 1370 sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn. */
c906108c 1371
34e8f22d 1372static CORE_ADDR
6093d2eb 1373arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
c906108c 1374{
e17a4113 1375 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
c906108c
SS
1376 unsigned long inst;
1377 CORE_ADDR skip_pc;
a89fea3c 1378 CORE_ADDR func_addr, limit_pc;
c906108c 1379
a89fea3c
JL
1380 /* See if we can determine the end of the prologue via the symbol table.
1381 If so, then return either PC, or the PC after the prologue, whichever
1382 is greater. */
1383 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
c906108c 1384 {
d80b854b
UW
1385 CORE_ADDR post_prologue_pc
1386 = skip_prologue_using_sal (gdbarch, func_addr);
0d39a070
DJ
1387 struct symtab *s = find_pc_symtab (func_addr);
1388
621c6d5b
YQ
1389 if (post_prologue_pc)
1390 post_prologue_pc
1391 = arm_skip_stack_protector (post_prologue_pc, gdbarch);
1392
1393
0d39a070
DJ
1394 /* GCC always emits a line note before the prologue and another
1395 one after, even if the two are at the same address or on the
1396 same line. Take advantage of this so that we do not need to
1397 know every instruction that might appear in the prologue. We
1398 will have producer information for most binaries; if it is
1399 missing (e.g. for -gstabs), assuming the GNU tools. */
1400 if (post_prologue_pc
1401 && (s == NULL
1402 || s->producer == NULL
9ead7ae4
KB
1403 || strncmp (s->producer, "GNU ", sizeof ("GNU ") - 1) == 0
1404 || strncmp (s->producer, "clang ", sizeof ("clang ") - 1) == 0))
0d39a070
DJ
1405 return post_prologue_pc;
1406
a89fea3c 1407 if (post_prologue_pc != 0)
0d39a070
DJ
1408 {
1409 CORE_ADDR analyzed_limit;
1410
1411 /* For non-GCC compilers, make sure the entire line is an
1412 acceptable prologue; GDB will round this function's
1413 return value up to the end of the following line so we
1414 can not skip just part of a line (and we do not want to).
1415
1416 RealView does not treat the prologue specially, but does
1417 associate prologue code with the opening brace; so this
1418 lets us skip the first line if we think it is the opening
1419 brace. */
9779414d 1420 if (arm_pc_is_thumb (gdbarch, func_addr))
0d39a070
DJ
1421 analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
1422 post_prologue_pc, NULL);
1423 else
1424 analyzed_limit = arm_analyze_prologue (gdbarch, func_addr,
1425 post_prologue_pc, NULL);
1426
1427 if (analyzed_limit != post_prologue_pc)
1428 return func_addr;
1429
1430 return post_prologue_pc;
1431 }
c906108c
SS
1432 }
1433
a89fea3c
JL
1434 /* Can't determine prologue from the symbol table, need to examine
1435 instructions. */
c906108c 1436
a89fea3c
JL
1437 /* Find an upper limit on the function prologue using the debug
1438 information. If the debug information could not be used to provide
1439 that bound, then use an arbitrary large number as the upper bound. */
0963b4bd 1440 /* Like arm_scan_prologue, stop no later than pc + 64. */
d80b854b 1441 limit_pc = skip_prologue_using_sal (gdbarch, pc);
a89fea3c
JL
1442 if (limit_pc == 0)
1443 limit_pc = pc + 64; /* Magic. */
1444
c906108c 1445
29d73ae4 1446 /* Check if this is Thumb code. */
9779414d 1447 if (arm_pc_is_thumb (gdbarch, pc))
a89fea3c 1448 return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
29d73ae4 1449
a89fea3c 1450 for (skip_pc = pc; skip_pc < limit_pc; skip_pc += 4)
f43845b3 1451 {
e17a4113 1452 inst = read_memory_unsigned_integer (skip_pc, 4, byte_order_for_code);
9d4fde75 1453
b8d5e71d
MS
1454 /* "mov ip, sp" is no longer a required part of the prologue. */
1455 if (inst == 0xe1a0c00d) /* mov ip, sp */
1456 continue;
c906108c 1457
28cd8767
JG
1458 if ((inst & 0xfffff000) == 0xe28dc000) /* add ip, sp #n */
1459 continue;
1460
1461 if ((inst & 0xfffff000) == 0xe24dc000) /* sub ip, sp #n */
1462 continue;
1463
b8d5e71d
MS
1464 /* Some prologues begin with "str lr, [sp, #-4]!". */
1465 if (inst == 0xe52de004) /* str lr, [sp, #-4]! */
1466 continue;
c906108c 1467
b8d5e71d
MS
1468 if ((inst & 0xfffffff0) == 0xe92d0000) /* stmfd sp!,{a1,a2,a3,a4} */
1469 continue;
c906108c 1470
b8d5e71d
MS
1471 if ((inst & 0xfffff800) == 0xe92dd800) /* stmfd sp!,{fp,ip,lr,pc} */
1472 continue;
11d3b27d 1473
b8d5e71d
MS
1474 /* Any insns after this point may float into the code, if it makes
1475 for better instruction scheduling, so we skip them only if we
1476 find them, but still consider the function to be frame-ful. */
f43845b3 1477
b8d5e71d
MS
1478 /* We may have either one sfmfd instruction here, or several stfe
1479 insns, depending on the version of floating point code we
1480 support. */
1481 if ((inst & 0xffbf0fff) == 0xec2d0200) /* sfmfd fn, <cnt>, [sp]! */
1482 continue;
1483
1484 if ((inst & 0xffff8fff) == 0xed6d0103) /* stfe fn, [sp, #-12]! */
1485 continue;
1486
1487 if ((inst & 0xfffff000) == 0xe24cb000) /* sub fp, ip, #nn */
1488 continue;
1489
1490 if ((inst & 0xfffff000) == 0xe24dd000) /* sub sp, sp, #nn */
1491 continue;
1492
f8bf5763
PM
1493 if ((inst & 0xffffc000) == 0xe54b0000 /* strb r(0123),[r11,#-nn] */
1494 || (inst & 0xffffc0f0) == 0xe14b00b0 /* strh r(0123),[r11,#-nn] */
1495 || (inst & 0xffffc000) == 0xe50b0000) /* str r(0123),[r11,#-nn] */
b8d5e71d
MS
1496 continue;
1497
f8bf5763
PM
1498 if ((inst & 0xffffc000) == 0xe5cd0000 /* strb r(0123),[sp,#nn] */
1499 || (inst & 0xffffc0f0) == 0xe1cd00b0 /* strh r(0123),[sp,#nn] */
1500 || (inst & 0xffffc000) == 0xe58d0000) /* str r(0123),[sp,#nn] */
b8d5e71d
MS
1501 continue;
1502
1503 /* Un-recognized instruction; stop scanning. */
1504 break;
f43845b3 1505 }
c906108c 1506
0963b4bd 1507 return skip_pc; /* End of prologue. */
c906108c 1508}
94c30b78 1509
c5aa993b 1510/* *INDENT-OFF* */
c906108c
SS
1511/* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
1512 This function decodes a Thumb function prologue to determine:
1513 1) the size of the stack frame
1514 2) which registers are saved on it
1515 3) the offsets of saved regs
1516 4) the offset from the stack pointer to the frame pointer
c906108c 1517
da59e081
JM
1518 A typical Thumb function prologue would create this stack frame
1519 (offsets relative to FP)
c906108c
SS
1520 old SP -> 24 stack parameters
1521 20 LR
1522 16 R7
1523 R7 -> 0 local variables (16 bytes)
1524 SP -> -12 additional stack space (12 bytes)
1525 The frame size would thus be 36 bytes, and the frame offset would be
0963b4bd 1526 12 bytes. The frame register is R7.
da59e081 1527
da3c6d4a
MS
1528 The comments for thumb_skip_prolog() describe the algorithm we use
1529 to detect the end of the prolog. */
c5aa993b
JM
1530/* *INDENT-ON* */
1531
c906108c 1532static void
be8626e0 1533thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
b39cc962 1534 CORE_ADDR block_addr, struct arm_prologue_cache *cache)
c906108c
SS
1535{
1536 CORE_ADDR prologue_start;
1537 CORE_ADDR prologue_end;
c906108c 1538
b39cc962
DJ
1539 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1540 &prologue_end))
c906108c 1541 {
ec3d575a
UW
1542 /* See comment in arm_scan_prologue for an explanation of
1543 this heuristics. */
1544 if (prologue_end > prologue_start + 64)
1545 {
1546 prologue_end = prologue_start + 64;
1547 }
c906108c
SS
1548 }
1549 else
f7060f85
DJ
1550 /* We're in the boondocks: we have no idea where the start of the
1551 function is. */
1552 return;
c906108c 1553
eb5492fa 1554 prologue_end = min (prologue_end, prev_pc);
c906108c 1555
be8626e0 1556 thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
1557}
1558
0d39a070 1559/* Return 1 if THIS_INSTR might change control flow, 0 otherwise. */
c906108c 1560
0d39a070
DJ
1561static int
1562arm_instruction_changes_pc (uint32_t this_instr)
c906108c 1563{
0d39a070
DJ
1564 if (bits (this_instr, 28, 31) == INST_NV)
1565 /* Unconditional instructions. */
1566 switch (bits (this_instr, 24, 27))
1567 {
1568 case 0xa:
1569 case 0xb:
1570 /* Branch with Link and change to Thumb. */
1571 return 1;
1572 case 0xc:
1573 case 0xd:
1574 case 0xe:
1575 /* Coprocessor register transfer. */
1576 if (bits (this_instr, 12, 15) == 15)
1577 error (_("Invalid update to pc in instruction"));
1578 return 0;
1579 default:
1580 return 0;
1581 }
1582 else
1583 switch (bits (this_instr, 25, 27))
1584 {
1585 case 0x0:
1586 if (bits (this_instr, 23, 24) == 2 && bit (this_instr, 20) == 0)
1587 {
1588 /* Multiplies and extra load/stores. */
1589 if (bit (this_instr, 4) == 1 && bit (this_instr, 7) == 1)
1590 /* Neither multiplies nor extension load/stores are allowed
1591 to modify PC. */
1592 return 0;
1593
1594 /* Otherwise, miscellaneous instructions. */
1595
1596 /* BX <reg>, BXJ <reg>, BLX <reg> */
1597 if (bits (this_instr, 4, 27) == 0x12fff1
1598 || bits (this_instr, 4, 27) == 0x12fff2
1599 || bits (this_instr, 4, 27) == 0x12fff3)
1600 return 1;
1601
1602 /* Other miscellaneous instructions are unpredictable if they
1603 modify PC. */
1604 return 0;
1605 }
1606 /* Data processing instruction. Fall through. */
c906108c 1607
0d39a070
DJ
1608 case 0x1:
1609 if (bits (this_instr, 12, 15) == 15)
1610 return 1;
1611 else
1612 return 0;
c906108c 1613
0d39a070
DJ
1614 case 0x2:
1615 case 0x3:
1616 /* Media instructions and architecturally undefined instructions. */
1617 if (bits (this_instr, 25, 27) == 3 && bit (this_instr, 4) == 1)
1618 return 0;
c906108c 1619
0d39a070
DJ
1620 /* Stores. */
1621 if (bit (this_instr, 20) == 0)
1622 return 0;
2a451106 1623
0d39a070
DJ
1624 /* Loads. */
1625 if (bits (this_instr, 12, 15) == ARM_PC_REGNUM)
1626 return 1;
1627 else
1628 return 0;
2a451106 1629
0d39a070
DJ
1630 case 0x4:
1631 /* Load/store multiple. */
1632 if (bit (this_instr, 20) == 1 && bit (this_instr, 15) == 1)
1633 return 1;
1634 else
1635 return 0;
2a451106 1636
0d39a070
DJ
1637 case 0x5:
1638 /* Branch and branch with link. */
1639 return 1;
2a451106 1640
0d39a070
DJ
1641 case 0x6:
1642 case 0x7:
1643 /* Coprocessor transfers or SWIs can not affect PC. */
1644 return 0;
eb5492fa 1645
0d39a070 1646 default:
9b20d036 1647 internal_error (__FILE__, __LINE__, _("bad value in switch"));
0d39a070
DJ
1648 }
1649}
c906108c 1650
0d39a070
DJ
1651/* Analyze an ARM mode prologue starting at PROLOGUE_START and
1652 continuing no further than PROLOGUE_END. If CACHE is non-NULL,
1653 fill it in. Return the first address not recognized as a prologue
1654 instruction.
eb5492fa 1655
0d39a070
DJ
1656 We recognize all the instructions typically found in ARM prologues,
1657 plus harmless instructions which can be skipped (either for analysis
1658 purposes, or a more restrictive set that can be skipped when finding
1659 the end of the prologue). */
1660
1661static CORE_ADDR
1662arm_analyze_prologue (struct gdbarch *gdbarch,
1663 CORE_ADDR prologue_start, CORE_ADDR prologue_end,
1664 struct arm_prologue_cache *cache)
1665{
1666 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1667 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1668 int regno;
1669 CORE_ADDR offset, current_pc;
1670 pv_t regs[ARM_FPS_REGNUM];
1671 struct pv_area *stack;
1672 struct cleanup *back_to;
1673 int framereg, framesize;
1674 CORE_ADDR unrecognized_pc = 0;
1675
1676 /* Search the prologue looking for instructions that set up the
96baa820 1677 frame pointer, adjust the stack pointer, and save registers.
ed9a39eb 1678
96baa820
JM
1679 Be careful, however, and if it doesn't look like a prologue,
1680 don't try to scan it. If, for instance, a frameless function
1681 begins with stmfd sp!, then we will tell ourselves there is
b8d5e71d 1682 a frame, which will confuse stack traceback, as well as "finish"
96baa820 1683 and other operations that rely on a knowledge of the stack
0d39a070 1684 traceback. */
d4473757 1685
4be43953
DJ
1686 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1687 regs[regno] = pv_register (regno, 0);
55f960e1 1688 stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
4be43953
DJ
1689 back_to = make_cleanup_free_pv_area (stack);
1690
94c30b78
MS
1691 for (current_pc = prologue_start;
1692 current_pc < prologue_end;
f43845b3 1693 current_pc += 4)
96baa820 1694 {
e17a4113
UW
1695 unsigned int insn
1696 = read_memory_unsigned_integer (current_pc, 4, byte_order_for_code);
9d4fde75 1697
94c30b78 1698 if (insn == 0xe1a0c00d) /* mov ip, sp */
f43845b3 1699 {
4be43953 1700 regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
28cd8767
JG
1701 continue;
1702 }
0d39a070
DJ
1703 else if ((insn & 0xfff00000) == 0xe2800000 /* add Rd, Rn, #n */
1704 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767
JG
1705 {
1706 unsigned imm = insn & 0xff; /* immediate value */
1707 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
0d39a070 1708 int rd = bits (insn, 12, 15);
28cd8767 1709 imm = (imm >> rot) | (imm << (32 - rot));
0d39a070 1710 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
28cd8767
JG
1711 continue;
1712 }
0d39a070
DJ
1713 else if ((insn & 0xfff00000) == 0xe2400000 /* sub Rd, Rn, #n */
1714 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767
JG
1715 {
1716 unsigned imm = insn & 0xff; /* immediate value */
1717 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
0d39a070 1718 int rd = bits (insn, 12, 15);
28cd8767 1719 imm = (imm >> rot) | (imm << (32 - rot));
0d39a070 1720 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
f43845b3
MS
1721 continue;
1722 }
0963b4bd
MS
1723 else if ((insn & 0xffff0fff) == 0xe52d0004) /* str Rd,
1724 [sp, #-4]! */
f43845b3 1725 {
4be43953
DJ
1726 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1727 break;
1728 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
0d39a070
DJ
1729 pv_area_store (stack, regs[ARM_SP_REGNUM], 4,
1730 regs[bits (insn, 12, 15)]);
f43845b3
MS
1731 continue;
1732 }
1733 else if ((insn & 0xffff0000) == 0xe92d0000)
d4473757
KB
1734 /* stmfd sp!, {..., fp, ip, lr, pc}
1735 or
1736 stmfd sp!, {a1, a2, a3, a4} */
c906108c 1737 {
d4473757 1738 int mask = insn & 0xffff;
ed9a39eb 1739
4be43953
DJ
1740 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1741 break;
1742
94c30b78 1743 /* Calculate offsets of saved registers. */
34e8f22d 1744 for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
d4473757
KB
1745 if (mask & (1 << regno))
1746 {
0963b4bd
MS
1747 regs[ARM_SP_REGNUM]
1748 = pv_add_constant (regs[ARM_SP_REGNUM], -4);
4be43953 1749 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
d4473757
KB
1750 }
1751 }
0d39a070
DJ
1752 else if ((insn & 0xffff0000) == 0xe54b0000 /* strb rx,[r11,#-n] */
1753 || (insn & 0xffff00f0) == 0xe14b00b0 /* strh rx,[r11,#-n] */
f8bf5763 1754 || (insn & 0xffffc000) == 0xe50b0000) /* str rx,[r11,#-n] */
b8d5e71d
MS
1755 {
1756 /* No need to add this to saved_regs -- it's just an arg reg. */
1757 continue;
1758 }
0d39a070
DJ
1759 else if ((insn & 0xffff0000) == 0xe5cd0000 /* strb rx,[sp,#n] */
1760 || (insn & 0xffff00f0) == 0xe1cd00b0 /* strh rx,[sp,#n] */
f8bf5763 1761 || (insn & 0xffffc000) == 0xe58d0000) /* str rx,[sp,#n] */
f43845b3
MS
1762 {
1763 /* No need to add this to saved_regs -- it's just an arg reg. */
1764 continue;
1765 }
0963b4bd
MS
1766 else if ((insn & 0xfff00000) == 0xe8800000 /* stm Rn,
1767 { registers } */
0d39a070
DJ
1768 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1769 {
1770 /* No need to add this to saved_regs -- it's just arg regs. */
1771 continue;
1772 }
d4473757
KB
1773 else if ((insn & 0xfffff000) == 0xe24cb000) /* sub fp, ip #n */
1774 {
94c30b78
MS
1775 unsigned imm = insn & 0xff; /* immediate value */
1776 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
d4473757 1777 imm = (imm >> rot) | (imm << (32 - rot));
4be43953 1778 regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
d4473757
KB
1779 }
1780 else if ((insn & 0xfffff000) == 0xe24dd000) /* sub sp, sp #n */
1781 {
94c30b78
MS
1782 unsigned imm = insn & 0xff; /* immediate value */
1783 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
d4473757 1784 imm = (imm >> rot) | (imm << (32 - rot));
4be43953 1785 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
d4473757 1786 }
0963b4bd
MS
1787 else if ((insn & 0xffff7fff) == 0xed6d0103 /* stfe f?,
1788 [sp, -#c]! */
2af46ca0 1789 && gdbarch_tdep (gdbarch)->have_fpa_registers)
d4473757 1790 {
4be43953
DJ
1791 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1792 break;
1793
1794 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
34e8f22d 1795 regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
4be43953 1796 pv_area_store (stack, regs[ARM_SP_REGNUM], 12, regs[regno]);
d4473757 1797 }
0963b4bd
MS
1798 else if ((insn & 0xffbf0fff) == 0xec2d0200 /* sfmfd f0, 4,
1799 [sp!] */
2af46ca0 1800 && gdbarch_tdep (gdbarch)->have_fpa_registers)
d4473757
KB
1801 {
1802 int n_saved_fp_regs;
1803 unsigned int fp_start_reg, fp_bound_reg;
1804
4be43953
DJ
1805 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1806 break;
1807
94c30b78 1808 if ((insn & 0x800) == 0x800) /* N0 is set */
96baa820 1809 {
d4473757
KB
1810 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1811 n_saved_fp_regs = 3;
1812 else
1813 n_saved_fp_regs = 1;
96baa820 1814 }
d4473757 1815 else
96baa820 1816 {
d4473757
KB
1817 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1818 n_saved_fp_regs = 2;
1819 else
1820 n_saved_fp_regs = 4;
96baa820 1821 }
d4473757 1822
34e8f22d 1823 fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
d4473757
KB
1824 fp_bound_reg = fp_start_reg + n_saved_fp_regs;
1825 for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
96baa820 1826 {
4be43953
DJ
1827 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
1828 pv_area_store (stack, regs[ARM_SP_REGNUM], 12,
1829 regs[fp_start_reg++]);
96baa820 1830 }
c906108c 1831 }
0d39a070
DJ
1832 else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
1833 {
1834 /* Allow some special function calls when skipping the
1835 prologue; GCC generates these before storing arguments to
1836 the stack. */
1837 CORE_ADDR dest = BranchDest (current_pc, insn);
1838
e0634ccf 1839 if (skip_prologue_function (gdbarch, dest, 0))
0d39a070
DJ
1840 continue;
1841 else
1842 break;
1843 }
d4473757 1844 else if ((insn & 0xf0000000) != 0xe0000000)
0963b4bd 1845 break; /* Condition not true, exit early. */
0d39a070
DJ
1846 else if (arm_instruction_changes_pc (insn))
1847 /* Don't scan past anything that might change control flow. */
1848 break;
d19f7eee
UW
1849 else if ((insn & 0xfe500000) == 0xe8100000 /* ldm */
1850 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1851 /* Ignore block loads from the stack, potentially copying
1852 parameters from memory. */
1853 continue;
1854 else if ((insn & 0xfc500000) == 0xe4100000
1855 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1856 /* Similarly ignore single loads from the stack. */
1857 continue;
0d39a070
DJ
1858 else if ((insn & 0xffff0ff0) == 0xe1a00000)
1859 /* MOV Rd, Rm. Skip register copies, i.e. saves to another
1860 register instead of the stack. */
d4473757 1861 continue;
0d39a070
DJ
1862 else
1863 {
1864 /* The optimizer might shove anything into the prologue,
1865 so we just skip what we don't recognize. */
1866 unrecognized_pc = current_pc;
1867 continue;
1868 }
c906108c
SS
1869 }
1870
0d39a070
DJ
1871 if (unrecognized_pc == 0)
1872 unrecognized_pc = current_pc;
1873
4be43953
DJ
1874 /* The frame size is just the distance from the frame register
1875 to the original stack pointer. */
1876 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1877 {
1878 /* Frame pointer is fp. */
0d39a070
DJ
1879 framereg = ARM_FP_REGNUM;
1880 framesize = -regs[ARM_FP_REGNUM].k;
4be43953 1881 }
72a2e3dc 1882 else
4be43953
DJ
1883 {
1884 /* Try the stack pointer... this is a bit desperate. */
0d39a070
DJ
1885 framereg = ARM_SP_REGNUM;
1886 framesize = -regs[ARM_SP_REGNUM].k;
4be43953 1887 }
4be43953 1888
0d39a070
DJ
1889 if (cache)
1890 {
1891 cache->framereg = framereg;
1892 cache->framesize = framesize;
1893
1894 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1895 if (pv_area_find_reg (stack, gdbarch, regno, &offset))
1896 cache->saved_regs[regno].addr = offset;
1897 }
1898
1899 if (arm_debug)
1900 fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1901 paddress (gdbarch, unrecognized_pc));
4be43953
DJ
1902
1903 do_cleanups (back_to);
0d39a070
DJ
1904 return unrecognized_pc;
1905}
1906
1907static void
1908arm_scan_prologue (struct frame_info *this_frame,
1909 struct arm_prologue_cache *cache)
1910{
1911 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1912 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1913 int regno;
1914 CORE_ADDR prologue_start, prologue_end, current_pc;
1915 CORE_ADDR prev_pc = get_frame_pc (this_frame);
1916 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1917 pv_t regs[ARM_FPS_REGNUM];
1918 struct pv_area *stack;
1919 struct cleanup *back_to;
1920 CORE_ADDR offset;
1921
1922 /* Assume there is no frame until proven otherwise. */
1923 cache->framereg = ARM_SP_REGNUM;
1924 cache->framesize = 0;
1925
1926 /* Check for Thumb prologue. */
1927 if (arm_frame_is_thumb (this_frame))
1928 {
1929 thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
1930 return;
1931 }
1932
1933 /* Find the function prologue. If we can't find the function in
1934 the symbol table, peek in the stack frame to find the PC. */
1935 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1936 &prologue_end))
1937 {
1938 /* One way to find the end of the prologue (which works well
1939 for unoptimized code) is to do the following:
1940
1941 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
1942
1943 if (sal.line == 0)
1944 prologue_end = prev_pc;
1945 else if (sal.end < prologue_end)
1946 prologue_end = sal.end;
1947
1948 This mechanism is very accurate so long as the optimizer
1949 doesn't move any instructions from the function body into the
1950 prologue. If this happens, sal.end will be the last
1951 instruction in the first hunk of prologue code just before
1952 the first instruction that the scheduler has moved from
1953 the body to the prologue.
1954
1955 In order to make sure that we scan all of the prologue
1956 instructions, we use a slightly less accurate mechanism which
1957 may scan more than necessary. To help compensate for this
1958 lack of accuracy, the prologue scanning loop below contains
1959 several clauses which'll cause the loop to terminate early if
1960 an implausible prologue instruction is encountered.
1961
1962 The expression
1963
1964 prologue_start + 64
1965
1966 is a suitable endpoint since it accounts for the largest
1967 possible prologue plus up to five instructions inserted by
1968 the scheduler. */
1969
1970 if (prologue_end > prologue_start + 64)
1971 {
1972 prologue_end = prologue_start + 64; /* See above. */
1973 }
1974 }
1975 else
1976 {
1977 /* We have no symbol information. Our only option is to assume this
1978 function has a standard stack frame and the normal frame register.
1979 Then, we can find the value of our frame pointer on entrance to
1980 the callee (or at the present moment if this is the innermost frame).
1981 The value stored there should be the address of the stmfd + 8. */
1982 CORE_ADDR frame_loc;
1983 LONGEST return_value;
1984
1985 frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
1986 if (!safe_read_memory_integer (frame_loc, 4, byte_order, &return_value))
1987 return;
1988 else
1989 {
1990 prologue_start = gdbarch_addr_bits_remove
1991 (gdbarch, return_value) - 8;
1992 prologue_end = prologue_start + 64; /* See above. */
1993 }
1994 }
1995
1996 if (prev_pc < prologue_end)
1997 prologue_end = prev_pc;
1998
1999 arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
2000}
2001
eb5492fa 2002static struct arm_prologue_cache *
a262aec2 2003arm_make_prologue_cache (struct frame_info *this_frame)
c906108c 2004{
eb5492fa
DJ
2005 int reg;
2006 struct arm_prologue_cache *cache;
2007 CORE_ADDR unwound_fp;
c5aa993b 2008
35d5d4ee 2009 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
a262aec2 2010 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
c906108c 2011
a262aec2 2012 arm_scan_prologue (this_frame, cache);
848cfffb 2013
a262aec2 2014 unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
eb5492fa
DJ
2015 if (unwound_fp == 0)
2016 return cache;
c906108c 2017
4be43953 2018 cache->prev_sp = unwound_fp + cache->framesize;
c906108c 2019
eb5492fa
DJ
2020 /* Calculate actual addresses of saved registers using offsets
2021 determined by arm_scan_prologue. */
a262aec2 2022 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
e28a332c 2023 if (trad_frame_addr_p (cache->saved_regs, reg))
eb5492fa
DJ
2024 cache->saved_regs[reg].addr += cache->prev_sp;
2025
2026 return cache;
c906108c
SS
2027}
2028
eb5492fa
DJ
2029/* Our frame ID for a normal frame is the current function's starting PC
2030 and the caller's SP when we were called. */
c906108c 2031
148754e5 2032static void
a262aec2 2033arm_prologue_this_id (struct frame_info *this_frame,
eb5492fa
DJ
2034 void **this_cache,
2035 struct frame_id *this_id)
c906108c 2036{
eb5492fa
DJ
2037 struct arm_prologue_cache *cache;
2038 struct frame_id id;
2c404490 2039 CORE_ADDR pc, func;
f079148d 2040
eb5492fa 2041 if (*this_cache == NULL)
a262aec2 2042 *this_cache = arm_make_prologue_cache (this_frame);
eb5492fa 2043 cache = *this_cache;
2a451106 2044
2c404490
DJ
2045 /* This is meant to halt the backtrace at "_start". */
2046 pc = get_frame_pc (this_frame);
2047 if (pc <= gdbarch_tdep (get_frame_arch (this_frame))->lowest_pc)
eb5492fa 2048 return;
5a203e44 2049
eb5492fa
DJ
2050 /* If we've hit a wall, stop. */
2051 if (cache->prev_sp == 0)
2052 return;
24de872b 2053
0e9e9abd
UW
2054 /* Use function start address as part of the frame ID. If we cannot
2055 identify the start address (due to missing symbol information),
2056 fall back to just using the current PC. */
2c404490 2057 func = get_frame_func (this_frame);
0e9e9abd
UW
2058 if (!func)
2059 func = pc;
2060
eb5492fa 2061 id = frame_id_build (cache->prev_sp, func);
eb5492fa 2062 *this_id = id;
c906108c
SS
2063}
2064
a262aec2
DJ
2065static struct value *
2066arm_prologue_prev_register (struct frame_info *this_frame,
eb5492fa 2067 void **this_cache,
a262aec2 2068 int prev_regnum)
24de872b 2069{
24568a2c 2070 struct gdbarch *gdbarch = get_frame_arch (this_frame);
24de872b
DJ
2071 struct arm_prologue_cache *cache;
2072
eb5492fa 2073 if (*this_cache == NULL)
a262aec2 2074 *this_cache = arm_make_prologue_cache (this_frame);
eb5492fa 2075 cache = *this_cache;
24de872b 2076
eb5492fa 2077 /* If we are asked to unwind the PC, then we need to return the LR
b39cc962
DJ
2078 instead. The prologue may save PC, but it will point into this
2079 frame's prologue, not the next frame's resume location. Also
2080 strip the saved T bit. A valid LR may have the low bit set, but
2081 a valid PC never does. */
eb5492fa 2082 if (prev_regnum == ARM_PC_REGNUM)
b39cc962
DJ
2083 {
2084 CORE_ADDR lr;
2085
2086 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2087 return frame_unwind_got_constant (this_frame, prev_regnum,
24568a2c 2088 arm_addr_bits_remove (gdbarch, lr));
b39cc962 2089 }
24de872b 2090
eb5492fa 2091 /* SP is generally not saved to the stack, but this frame is
a262aec2 2092 identified by the next frame's stack pointer at the time of the call.
eb5492fa
DJ
2093 The value was already reconstructed into PREV_SP. */
2094 if (prev_regnum == ARM_SP_REGNUM)
a262aec2 2095 return frame_unwind_got_constant (this_frame, prev_regnum, cache->prev_sp);
eb5492fa 2096
b39cc962
DJ
2097 /* The CPSR may have been changed by the call instruction and by the
2098 called function. The only bit we can reconstruct is the T bit,
2099 by checking the low bit of LR as of the call. This is a reliable
2100 indicator of Thumb-ness except for some ARM v4T pre-interworking
2101 Thumb code, which could get away with a clear low bit as long as
2102 the called function did not use bx. Guess that all other
2103 bits are unchanged; the condition flags are presumably lost,
2104 but the processor status is likely valid. */
2105 if (prev_regnum == ARM_PS_REGNUM)
2106 {
2107 CORE_ADDR lr, cpsr;
9779414d 2108 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
b39cc962
DJ
2109
2110 cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
2111 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2112 if (IS_THUMB_ADDR (lr))
9779414d 2113 cpsr |= t_bit;
b39cc962 2114 else
9779414d 2115 cpsr &= ~t_bit;
b39cc962
DJ
2116 return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
2117 }
2118
a262aec2
DJ
2119 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
2120 prev_regnum);
eb5492fa
DJ
2121}
2122
2123struct frame_unwind arm_prologue_unwind = {
2124 NORMAL_FRAME,
8fbca658 2125 default_frame_unwind_stop_reason,
eb5492fa 2126 arm_prologue_this_id,
a262aec2
DJ
2127 arm_prologue_prev_register,
2128 NULL,
2129 default_frame_sniffer
eb5492fa
DJ
2130};
2131
0e9e9abd
UW
2132/* Maintain a list of ARM exception table entries per objfile, similar to the
2133 list of mapping symbols. We only cache entries for standard ARM-defined
2134 personality routines; the cache will contain only the frame unwinding
2135 instructions associated with the entry (not the descriptors). */
2136
2137static const struct objfile_data *arm_exidx_data_key;
2138
2139struct arm_exidx_entry
2140{
2141 bfd_vma addr;
2142 gdb_byte *entry;
2143};
2144typedef struct arm_exidx_entry arm_exidx_entry_s;
2145DEF_VEC_O(arm_exidx_entry_s);
2146
2147struct arm_exidx_data
2148{
2149 VEC(arm_exidx_entry_s) **section_maps;
2150};
2151
2152static void
2153arm_exidx_data_free (struct objfile *objfile, void *arg)
2154{
2155 struct arm_exidx_data *data = arg;
2156 unsigned int i;
2157
2158 for (i = 0; i < objfile->obfd->section_count; i++)
2159 VEC_free (arm_exidx_entry_s, data->section_maps[i]);
2160}
2161
2162static inline int
2163arm_compare_exidx_entries (const struct arm_exidx_entry *lhs,
2164 const struct arm_exidx_entry *rhs)
2165{
2166 return lhs->addr < rhs->addr;
2167}
2168
2169static struct obj_section *
2170arm_obj_section_from_vma (struct objfile *objfile, bfd_vma vma)
2171{
2172 struct obj_section *osect;
2173
2174 ALL_OBJFILE_OSECTIONS (objfile, osect)
2175 if (bfd_get_section_flags (objfile->obfd,
2176 osect->the_bfd_section) & SEC_ALLOC)
2177 {
2178 bfd_vma start, size;
2179 start = bfd_get_section_vma (objfile->obfd, osect->the_bfd_section);
2180 size = bfd_get_section_size (osect->the_bfd_section);
2181
2182 if (start <= vma && vma < start + size)
2183 return osect;
2184 }
2185
2186 return NULL;
2187}
2188
2189/* Parse contents of exception table and exception index sections
2190 of OBJFILE, and fill in the exception table entry cache.
2191
2192 For each entry that refers to a standard ARM-defined personality
2193 routine, extract the frame unwinding instructions (from either
2194 the index or the table section). The unwinding instructions
2195 are normalized by:
2196 - extracting them from the rest of the table data
2197 - converting to host endianness
2198 - appending the implicit 0xb0 ("Finish") code
2199
2200 The extracted and normalized instructions are stored for later
2201 retrieval by the arm_find_exidx_entry routine. */
2202
2203static void
2204arm_exidx_new_objfile (struct objfile *objfile)
2205{
3bb47e8b 2206 struct cleanup *cleanups;
0e9e9abd
UW
2207 struct arm_exidx_data *data;
2208 asection *exidx, *extab;
2209 bfd_vma exidx_vma = 0, extab_vma = 0;
2210 bfd_size_type exidx_size = 0, extab_size = 0;
2211 gdb_byte *exidx_data = NULL, *extab_data = NULL;
2212 LONGEST i;
2213
2214 /* If we've already touched this file, do nothing. */
2215 if (!objfile || objfile_data (objfile, arm_exidx_data_key) != NULL)
2216 return;
3bb47e8b 2217 cleanups = make_cleanup (null_cleanup, NULL);
0e9e9abd
UW
2218
2219 /* Read contents of exception table and index. */
2220 exidx = bfd_get_section_by_name (objfile->obfd, ".ARM.exidx");
2221 if (exidx)
2222 {
2223 exidx_vma = bfd_section_vma (objfile->obfd, exidx);
2224 exidx_size = bfd_get_section_size (exidx);
2225 exidx_data = xmalloc (exidx_size);
2226 make_cleanup (xfree, exidx_data);
2227
2228 if (!bfd_get_section_contents (objfile->obfd, exidx,
2229 exidx_data, 0, exidx_size))
2230 {
2231 do_cleanups (cleanups);
2232 return;
2233 }
2234 }
2235
2236 extab = bfd_get_section_by_name (objfile->obfd, ".ARM.extab");
2237 if (extab)
2238 {
2239 extab_vma = bfd_section_vma (objfile->obfd, extab);
2240 extab_size = bfd_get_section_size (extab);
2241 extab_data = xmalloc (extab_size);
2242 make_cleanup (xfree, extab_data);
2243
2244 if (!bfd_get_section_contents (objfile->obfd, extab,
2245 extab_data, 0, extab_size))
2246 {
2247 do_cleanups (cleanups);
2248 return;
2249 }
2250 }
2251
2252 /* Allocate exception table data structure. */
2253 data = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct arm_exidx_data);
2254 set_objfile_data (objfile, arm_exidx_data_key, data);
2255 data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
2256 objfile->obfd->section_count,
2257 VEC(arm_exidx_entry_s) *);
2258
2259 /* Fill in exception table. */
2260 for (i = 0; i < exidx_size / 8; i++)
2261 {
2262 struct arm_exidx_entry new_exidx_entry;
2263 bfd_vma idx = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8);
2264 bfd_vma val = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8 + 4);
2265 bfd_vma addr = 0, word = 0;
2266 int n_bytes = 0, n_words = 0;
2267 struct obj_section *sec;
2268 gdb_byte *entry = NULL;
2269
2270 /* Extract address of start of function. */
2271 idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2272 idx += exidx_vma + i * 8;
2273
2274 /* Find section containing function and compute section offset. */
2275 sec = arm_obj_section_from_vma (objfile, idx);
2276 if (sec == NULL)
2277 continue;
2278 idx -= bfd_get_section_vma (objfile->obfd, sec->the_bfd_section);
2279
2280 /* Determine address of exception table entry. */
2281 if (val == 1)
2282 {
2283 /* EXIDX_CANTUNWIND -- no exception table entry present. */
2284 }
2285 else if ((val & 0xff000000) == 0x80000000)
2286 {
2287 /* Exception table entry embedded in .ARM.exidx
2288 -- must be short form. */
2289 word = val;
2290 n_bytes = 3;
2291 }
2292 else if (!(val & 0x80000000))
2293 {
2294 /* Exception table entry in .ARM.extab. */
2295 addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2296 addr += exidx_vma + i * 8 + 4;
2297
2298 if (addr >= extab_vma && addr + 4 <= extab_vma + extab_size)
2299 {
2300 word = bfd_h_get_32 (objfile->obfd,
2301 extab_data + addr - extab_vma);
2302 addr += 4;
2303
2304 if ((word & 0xff000000) == 0x80000000)
2305 {
2306 /* Short form. */
2307 n_bytes = 3;
2308 }
2309 else if ((word & 0xff000000) == 0x81000000
2310 || (word & 0xff000000) == 0x82000000)
2311 {
2312 /* Long form. */
2313 n_bytes = 2;
2314 n_words = ((word >> 16) & 0xff);
2315 }
2316 else if (!(word & 0x80000000))
2317 {
2318 bfd_vma pers;
2319 struct obj_section *pers_sec;
2320 int gnu_personality = 0;
2321
2322 /* Custom personality routine. */
2323 pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2324 pers = UNMAKE_THUMB_ADDR (pers + addr - 4);
2325
2326 /* Check whether we've got one of the variants of the
2327 GNU personality routines. */
2328 pers_sec = arm_obj_section_from_vma (objfile, pers);
2329 if (pers_sec)
2330 {
2331 static const char *personality[] =
2332 {
2333 "__gcc_personality_v0",
2334 "__gxx_personality_v0",
2335 "__gcj_personality_v0",
2336 "__gnu_objc_personality_v0",
2337 NULL
2338 };
2339
2340 CORE_ADDR pc = pers + obj_section_offset (pers_sec);
2341 int k;
2342
2343 for (k = 0; personality[k]; k++)
2344 if (lookup_minimal_symbol_by_pc_name
2345 (pc, personality[k], objfile))
2346 {
2347 gnu_personality = 1;
2348 break;
2349 }
2350 }
2351
2352 /* If so, the next word contains a word count in the high
2353 byte, followed by the same unwind instructions as the
2354 pre-defined forms. */
2355 if (gnu_personality
2356 && addr + 4 <= extab_vma + extab_size)
2357 {
2358 word = bfd_h_get_32 (objfile->obfd,
2359 extab_data + addr - extab_vma);
2360 addr += 4;
2361 n_bytes = 3;
2362 n_words = ((word >> 24) & 0xff);
2363 }
2364 }
2365 }
2366 }
2367
2368 /* Sanity check address. */
2369 if (n_words)
2370 if (addr < extab_vma || addr + 4 * n_words > extab_vma + extab_size)
2371 n_words = n_bytes = 0;
2372
2373 /* The unwind instructions reside in WORD (only the N_BYTES least
2374 significant bytes are valid), followed by N_WORDS words in the
2375 extab section starting at ADDR. */
2376 if (n_bytes || n_words)
2377 {
2378 gdb_byte *p = entry = obstack_alloc (&objfile->objfile_obstack,
2379 n_bytes + n_words * 4 + 1);
2380
2381 while (n_bytes--)
2382 *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);
2383
2384 while (n_words--)
2385 {
2386 word = bfd_h_get_32 (objfile->obfd,
2387 extab_data + addr - extab_vma);
2388 addr += 4;
2389
2390 *p++ = (gdb_byte) ((word >> 24) & 0xff);
2391 *p++ = (gdb_byte) ((word >> 16) & 0xff);
2392 *p++ = (gdb_byte) ((word >> 8) & 0xff);
2393 *p++ = (gdb_byte) (word & 0xff);
2394 }
2395
2396 /* Implied "Finish" to terminate the list. */
2397 *p++ = 0xb0;
2398 }
2399
2400 /* Push entry onto vector. They are guaranteed to always
2401 appear in order of increasing addresses. */
2402 new_exidx_entry.addr = idx;
2403 new_exidx_entry.entry = entry;
2404 VEC_safe_push (arm_exidx_entry_s,
2405 data->section_maps[sec->the_bfd_section->index],
2406 &new_exidx_entry);
2407 }
2408
2409 do_cleanups (cleanups);
2410}
2411
2412/* Search for the exception table entry covering MEMADDR. If one is found,
2413 return a pointer to its data. Otherwise, return 0. If START is non-NULL,
2414 set *START to the start of the region covered by this entry. */
2415
2416static gdb_byte *
2417arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
2418{
2419 struct obj_section *sec;
2420
2421 sec = find_pc_section (memaddr);
2422 if (sec != NULL)
2423 {
2424 struct arm_exidx_data *data;
2425 VEC(arm_exidx_entry_s) *map;
2426 struct arm_exidx_entry map_key = { memaddr - obj_section_addr (sec), 0 };
2427 unsigned int idx;
2428
2429 data = objfile_data (sec->objfile, arm_exidx_data_key);
2430 if (data != NULL)
2431 {
2432 map = data->section_maps[sec->the_bfd_section->index];
2433 if (!VEC_empty (arm_exidx_entry_s, map))
2434 {
2435 struct arm_exidx_entry *map_sym;
2436
2437 idx = VEC_lower_bound (arm_exidx_entry_s, map, &map_key,
2438 arm_compare_exidx_entries);
2439
2440 /* VEC_lower_bound finds the earliest ordered insertion
2441 point. If the following symbol starts at this exact
2442 address, we use that; otherwise, the preceding
2443 exception table entry covers this address. */
2444 if (idx < VEC_length (arm_exidx_entry_s, map))
2445 {
2446 map_sym = VEC_index (arm_exidx_entry_s, map, idx);
2447 if (map_sym->addr == map_key.addr)
2448 {
2449 if (start)
2450 *start = map_sym->addr + obj_section_addr (sec);
2451 return map_sym->entry;
2452 }
2453 }
2454
2455 if (idx > 0)
2456 {
2457 map_sym = VEC_index (arm_exidx_entry_s, map, idx - 1);
2458 if (start)
2459 *start = map_sym->addr + obj_section_addr (sec);
2460 return map_sym->entry;
2461 }
2462 }
2463 }
2464 }
2465
2466 return NULL;
2467}
2468
2469/* Given the current frame THIS_FRAME, and its associated frame unwinding
2470 instruction list from the ARM exception table entry ENTRY, allocate and
2471 return a prologue cache structure describing how to unwind this frame.
2472
2473 Return NULL if the unwinding instruction list contains a "spare",
2474 "reserved" or "refuse to unwind" instruction as defined in section
2475 "9.3 Frame unwinding instructions" of the "Exception Handling ABI
2476 for the ARM Architecture" document. */
2477
2478static struct arm_prologue_cache *
2479arm_exidx_fill_cache (struct frame_info *this_frame, gdb_byte *entry)
2480{
2481 CORE_ADDR vsp = 0;
2482 int vsp_valid = 0;
2483
2484 struct arm_prologue_cache *cache;
2485 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2486 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2487
2488 for (;;)
2489 {
2490 gdb_byte insn;
2491
2492 /* Whenever we reload SP, we actually have to retrieve its
2493 actual value in the current frame. */
2494 if (!vsp_valid)
2495 {
2496 if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2497 {
2498 int reg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2499 vsp = get_frame_register_unsigned (this_frame, reg);
2500 }
2501 else
2502 {
2503 CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr;
2504 vsp = get_frame_memory_unsigned (this_frame, addr, 4);
2505 }
2506
2507 vsp_valid = 1;
2508 }
2509
2510 /* Decode next unwind instruction. */
2511 insn = *entry++;
2512
2513 if ((insn & 0xc0) == 0)
2514 {
2515 int offset = insn & 0x3f;
2516 vsp += (offset << 2) + 4;
2517 }
2518 else if ((insn & 0xc0) == 0x40)
2519 {
2520 int offset = insn & 0x3f;
2521 vsp -= (offset << 2) + 4;
2522 }
2523 else if ((insn & 0xf0) == 0x80)
2524 {
2525 int mask = ((insn & 0xf) << 8) | *entry++;
2526 int i;
2527
2528 /* The special case of an all-zero mask identifies
2529 "Refuse to unwind". We return NULL to fall back
2530 to the prologue analyzer. */
2531 if (mask == 0)
2532 return NULL;
2533
2534 /* Pop registers r4..r15 under mask. */
2535 for (i = 0; i < 12; i++)
2536 if (mask & (1 << i))
2537 {
2538 cache->saved_regs[4 + i].addr = vsp;
2539 vsp += 4;
2540 }
2541
2542 /* Special-case popping SP -- we need to reload vsp. */
2543 if (mask & (1 << (ARM_SP_REGNUM - 4)))
2544 vsp_valid = 0;
2545 }
2546 else if ((insn & 0xf0) == 0x90)
2547 {
2548 int reg = insn & 0xf;
2549
2550 /* Reserved cases. */
2551 if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
2552 return NULL;
2553
2554 /* Set SP from another register and mark VSP for reload. */
2555 cache->saved_regs[ARM_SP_REGNUM] = cache->saved_regs[reg];
2556 vsp_valid = 0;
2557 }
2558 else if ((insn & 0xf0) == 0xa0)
2559 {
2560 int count = insn & 0x7;
2561 int pop_lr = (insn & 0x8) != 0;
2562 int i;
2563
2564 /* Pop r4..r[4+count]. */
2565 for (i = 0; i <= count; i++)
2566 {
2567 cache->saved_regs[4 + i].addr = vsp;
2568 vsp += 4;
2569 }
2570
2571 /* If indicated by flag, pop LR as well. */
2572 if (pop_lr)
2573 {
2574 cache->saved_regs[ARM_LR_REGNUM].addr = vsp;
2575 vsp += 4;
2576 }
2577 }
2578 else if (insn == 0xb0)
2579 {
2580 /* We could only have updated PC by popping into it; if so, it
2581 will show up as address. Otherwise, copy LR into PC. */
2582 if (!trad_frame_addr_p (cache->saved_regs, ARM_PC_REGNUM))
2583 cache->saved_regs[ARM_PC_REGNUM]
2584 = cache->saved_regs[ARM_LR_REGNUM];
2585
2586 /* We're done. */
2587 break;
2588 }
2589 else if (insn == 0xb1)
2590 {
2591 int mask = *entry++;
2592 int i;
2593
2594 /* All-zero mask and mask >= 16 is "spare". */
2595 if (mask == 0 || mask >= 16)
2596 return NULL;
2597
2598 /* Pop r0..r3 under mask. */
2599 for (i = 0; i < 4; i++)
2600 if (mask & (1 << i))
2601 {
2602 cache->saved_regs[i].addr = vsp;
2603 vsp += 4;
2604 }
2605 }
2606 else if (insn == 0xb2)
2607 {
2608 ULONGEST offset = 0;
2609 unsigned shift = 0;
2610
2611 do
2612 {
2613 offset |= (*entry & 0x7f) << shift;
2614 shift += 7;
2615 }
2616 while (*entry++ & 0x80);
2617
2618 vsp += 0x204 + (offset << 2);
2619 }
2620 else if (insn == 0xb3)
2621 {
2622 int start = *entry >> 4;
2623 int count = (*entry++) & 0xf;
2624 int i;
2625
2626 /* Only registers D0..D15 are valid here. */
2627 if (start + count >= 16)
2628 return NULL;
2629
2630 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2631 for (i = 0; i <= count; i++)
2632 {
2633 cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2634 vsp += 8;
2635 }
2636
2637 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2638 vsp += 4;
2639 }
2640 else if ((insn & 0xf8) == 0xb8)
2641 {
2642 int count = insn & 0x7;
2643 int i;
2644
2645 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2646 for (i = 0; i <= count; i++)
2647 {
2648 cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2649 vsp += 8;
2650 }
2651
2652 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2653 vsp += 4;
2654 }
2655 else if (insn == 0xc6)
2656 {
2657 int start = *entry >> 4;
2658 int count = (*entry++) & 0xf;
2659 int i;
2660
2661 /* Only registers WR0..WR15 are valid. */
2662 if (start + count >= 16)
2663 return NULL;
2664
2665 /* Pop iwmmx registers WR[start]..WR[start+count]. */
2666 for (i = 0; i <= count; i++)
2667 {
2668 cache->saved_regs[ARM_WR0_REGNUM + start + i].addr = vsp;
2669 vsp += 8;
2670 }
2671 }
2672 else if (insn == 0xc7)
2673 {
2674 int mask = *entry++;
2675 int i;
2676
2677 /* All-zero mask and mask >= 16 is "spare". */
2678 if (mask == 0 || mask >= 16)
2679 return NULL;
2680
2681 /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask. */
2682 for (i = 0; i < 4; i++)
2683 if (mask & (1 << i))
2684 {
2685 cache->saved_regs[ARM_WCGR0_REGNUM + i].addr = vsp;
2686 vsp += 4;
2687 }
2688 }
2689 else if ((insn & 0xf8) == 0xc0)
2690 {
2691 int count = insn & 0x7;
2692 int i;
2693
2694 /* Pop iwmmx registers WR[10]..WR[10+count]. */
2695 for (i = 0; i <= count; i++)
2696 {
2697 cache->saved_regs[ARM_WR0_REGNUM + 10 + i].addr = vsp;
2698 vsp += 8;
2699 }
2700 }
2701 else if (insn == 0xc8)
2702 {
2703 int start = *entry >> 4;
2704 int count = (*entry++) & 0xf;
2705 int i;
2706
2707 /* Only registers D0..D31 are valid. */
2708 if (start + count >= 16)
2709 return NULL;
2710
2711 /* Pop VFP double-precision registers
2712 D[16+start]..D[16+start+count]. */
2713 for (i = 0; i <= count; i++)
2714 {
2715 cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].addr = vsp;
2716 vsp += 8;
2717 }
2718 }
2719 else if (insn == 0xc9)
2720 {
2721 int start = *entry >> 4;
2722 int count = (*entry++) & 0xf;
2723 int i;
2724
2725 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2726 for (i = 0; i <= count; i++)
2727 {
2728 cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2729 vsp += 8;
2730 }
2731 }
2732 else if ((insn & 0xf8) == 0xd0)
2733 {
2734 int count = insn & 0x7;
2735 int i;
2736
2737 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2738 for (i = 0; i <= count; i++)
2739 {
2740 cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2741 vsp += 8;
2742 }
2743 }
2744 else
2745 {
2746 /* Everything else is "spare". */
2747 return NULL;
2748 }
2749 }
2750
2751 /* If we restore SP from a register, assume this was the frame register.
2752 Otherwise just fall back to SP as frame register. */
2753 if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2754 cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2755 else
2756 cache->framereg = ARM_SP_REGNUM;
2757
2758 /* Determine offset to previous frame. */
2759 cache->framesize
2760 = vsp - get_frame_register_unsigned (this_frame, cache->framereg);
2761
2762 /* We already got the previous SP. */
2763 cache->prev_sp = vsp;
2764
2765 return cache;
2766}
2767
2768/* Unwinding via ARM exception table entries. Note that the sniffer
2769 already computes a filled-in prologue cache, which is then used
2770 with the same arm_prologue_this_id and arm_prologue_prev_register
2771 routines also used for prologue-parsing based unwinding. */
2772
2773static int
2774arm_exidx_unwind_sniffer (const struct frame_unwind *self,
2775 struct frame_info *this_frame,
2776 void **this_prologue_cache)
2777{
2778 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2779 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
2780 CORE_ADDR addr_in_block, exidx_region, func_start;
2781 struct arm_prologue_cache *cache;
2782 gdb_byte *entry;
2783
2784 /* See if we have an ARM exception table entry covering this address. */
2785 addr_in_block = get_frame_address_in_block (this_frame);
2786 entry = arm_find_exidx_entry (addr_in_block, &exidx_region);
2787 if (!entry)
2788 return 0;
2789
2790 /* The ARM exception table does not describe unwind information
2791 for arbitrary PC values, but is guaranteed to be correct only
2792 at call sites. We have to decide here whether we want to use
2793 ARM exception table information for this frame, or fall back
2794 to using prologue parsing. (Note that if we have DWARF CFI,
2795 this sniffer isn't even called -- CFI is always preferred.)
2796
2797 Before we make this decision, however, we check whether we
2798 actually have *symbol* information for the current frame.
2799 If not, prologue parsing would not work anyway, so we might
2800 as well use the exception table and hope for the best. */
2801 if (find_pc_partial_function (addr_in_block, NULL, &func_start, NULL))
2802 {
2803 int exc_valid = 0;
2804
2805 /* If the next frame is "normal", we are at a call site in this
2806 frame, so exception information is guaranteed to be valid. */
2807 if (get_next_frame (this_frame)
2808 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
2809 exc_valid = 1;
2810
2811 /* We also assume exception information is valid if we're currently
2812 blocked in a system call. The system library is supposed to
2813 ensure this, so that e.g. pthread cancellation works. */
2814 if (arm_frame_is_thumb (this_frame))
2815 {
2816 LONGEST insn;
2817
2818 if (safe_read_memory_integer (get_frame_pc (this_frame) - 2, 2,
2819 byte_order_for_code, &insn)
2820 && (insn & 0xff00) == 0xdf00 /* svc */)
2821 exc_valid = 1;
2822 }
2823 else
2824 {
2825 LONGEST insn;
2826
2827 if (safe_read_memory_integer (get_frame_pc (this_frame) - 4, 4,
2828 byte_order_for_code, &insn)
2829 && (insn & 0x0f000000) == 0x0f000000 /* svc */)
2830 exc_valid = 1;
2831 }
2832
2833 /* Bail out if we don't know that exception information is valid. */
2834 if (!exc_valid)
2835 return 0;
2836
2837 /* The ARM exception index does not mark the *end* of the region
2838 covered by the entry, and some functions will not have any entry.
2839 To correctly recognize the end of the covered region, the linker
2840 should have inserted dummy records with a CANTUNWIND marker.
2841
2842 Unfortunately, current versions of GNU ld do not reliably do
2843 this, and thus we may have found an incorrect entry above.
2844 As a (temporary) sanity check, we only use the entry if it
2845 lies *within* the bounds of the function. Note that this check
2846 might reject perfectly valid entries that just happen to cover
2847 multiple functions; therefore this check ought to be removed
2848 once the linker is fixed. */
2849 if (func_start > exidx_region)
2850 return 0;
2851 }
2852
2853 /* Decode the list of unwinding instructions into a prologue cache.
2854 Note that this may fail due to e.g. a "refuse to unwind" code. */
2855 cache = arm_exidx_fill_cache (this_frame, entry);
2856 if (!cache)
2857 return 0;
2858
2859 *this_prologue_cache = cache;
2860 return 1;
2861}
2862
2863struct frame_unwind arm_exidx_unwind = {
2864 NORMAL_FRAME,
8fbca658 2865 default_frame_unwind_stop_reason,
0e9e9abd
UW
2866 arm_prologue_this_id,
2867 arm_prologue_prev_register,
2868 NULL,
2869 arm_exidx_unwind_sniffer
2870};
2871
909cf6ea 2872static struct arm_prologue_cache *
a262aec2 2873arm_make_stub_cache (struct frame_info *this_frame)
909cf6ea 2874{
909cf6ea 2875 struct arm_prologue_cache *cache;
909cf6ea 2876
35d5d4ee 2877 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
a262aec2 2878 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
909cf6ea 2879
a262aec2 2880 cache->prev_sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
909cf6ea
DJ
2881
2882 return cache;
2883}
2884
2885/* Our frame ID for a stub frame is the current SP and LR. */
2886
2887static void
a262aec2 2888arm_stub_this_id (struct frame_info *this_frame,
909cf6ea
DJ
2889 void **this_cache,
2890 struct frame_id *this_id)
2891{
2892 struct arm_prologue_cache *cache;
2893
2894 if (*this_cache == NULL)
a262aec2 2895 *this_cache = arm_make_stub_cache (this_frame);
909cf6ea
DJ
2896 cache = *this_cache;
2897
a262aec2 2898 *this_id = frame_id_build (cache->prev_sp, get_frame_pc (this_frame));
909cf6ea
DJ
2899}
2900
a262aec2
DJ
2901static int
2902arm_stub_unwind_sniffer (const struct frame_unwind *self,
2903 struct frame_info *this_frame,
2904 void **this_prologue_cache)
909cf6ea 2905{
93d42b30 2906 CORE_ADDR addr_in_block;
948f8e3d 2907 gdb_byte dummy[4];
909cf6ea 2908
a262aec2 2909 addr_in_block = get_frame_address_in_block (this_frame);
3e5d3a5a 2910 if (in_plt_section (addr_in_block)
fc36e839
DE
2911 /* We also use the stub winder if the target memory is unreadable
2912 to avoid having the prologue unwinder trying to read it. */
a262aec2
DJ
2913 || target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
2914 return 1;
909cf6ea 2915
a262aec2 2916 return 0;
909cf6ea
DJ
2917}
2918
a262aec2
DJ
2919struct frame_unwind arm_stub_unwind = {
2920 NORMAL_FRAME,
8fbca658 2921 default_frame_unwind_stop_reason,
a262aec2
DJ
2922 arm_stub_this_id,
2923 arm_prologue_prev_register,
2924 NULL,
2925 arm_stub_unwind_sniffer
2926};
2927
2ae28aa9
YQ
2928/* Put here the code to store, into CACHE->saved_regs, the addresses
2929 of the saved registers of frame described by THIS_FRAME. CACHE is
2930 returned. */
2931
2932static struct arm_prologue_cache *
2933arm_m_exception_cache (struct frame_info *this_frame)
2934{
2935 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2936 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2937 struct arm_prologue_cache *cache;
2938 CORE_ADDR unwound_sp;
2939 LONGEST xpsr;
2940
2941 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2942 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2943
2944 unwound_sp = get_frame_register_unsigned (this_frame,
2945 ARM_SP_REGNUM);
2946
2947 /* The hardware saves eight 32-bit words, comprising xPSR,
2948 ReturnAddress, LR (R14), R12, R3, R2, R1, R0. See details in
2949 "B1.5.6 Exception entry behavior" in
2950 "ARMv7-M Architecture Reference Manual". */
2951 cache->saved_regs[0].addr = unwound_sp;
2952 cache->saved_regs[1].addr = unwound_sp + 4;
2953 cache->saved_regs[2].addr = unwound_sp + 8;
2954 cache->saved_regs[3].addr = unwound_sp + 12;
2955 cache->saved_regs[12].addr = unwound_sp + 16;
2956 cache->saved_regs[14].addr = unwound_sp + 20;
2957 cache->saved_regs[15].addr = unwound_sp + 24;
2958 cache->saved_regs[ARM_PS_REGNUM].addr = unwound_sp + 28;
2959
2960 /* If bit 9 of the saved xPSR is set, then there is a four-byte
2961 aligner between the top of the 32-byte stack frame and the
2962 previous context's stack pointer. */
2963 cache->prev_sp = unwound_sp + 32;
2964 if (safe_read_memory_integer (unwound_sp + 28, 4, byte_order, &xpsr)
2965 && (xpsr & (1 << 9)) != 0)
2966 cache->prev_sp += 4;
2967
2968 return cache;
2969}
2970
2971/* Implementation of function hook 'this_id' in
2972 'struct frame_uwnind'. */
2973
2974static void
2975arm_m_exception_this_id (struct frame_info *this_frame,
2976 void **this_cache,
2977 struct frame_id *this_id)
2978{
2979 struct arm_prologue_cache *cache;
2980
2981 if (*this_cache == NULL)
2982 *this_cache = arm_m_exception_cache (this_frame);
2983 cache = *this_cache;
2984
2985 /* Our frame ID for a stub frame is the current SP and LR. */
2986 *this_id = frame_id_build (cache->prev_sp,
2987 get_frame_pc (this_frame));
2988}
2989
2990/* Implementation of function hook 'prev_register' in
2991 'struct frame_uwnind'. */
2992
2993static struct value *
2994arm_m_exception_prev_register (struct frame_info *this_frame,
2995 void **this_cache,
2996 int prev_regnum)
2997{
2998 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2999 struct arm_prologue_cache *cache;
3000
3001 if (*this_cache == NULL)
3002 *this_cache = arm_m_exception_cache (this_frame);
3003 cache = *this_cache;
3004
3005 /* The value was already reconstructed into PREV_SP. */
3006 if (prev_regnum == ARM_SP_REGNUM)
3007 return frame_unwind_got_constant (this_frame, prev_regnum,
3008 cache->prev_sp);
3009
3010 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
3011 prev_regnum);
3012}
3013
3014/* Implementation of function hook 'sniffer' in
3015 'struct frame_uwnind'. */
3016
3017static int
3018arm_m_exception_unwind_sniffer (const struct frame_unwind *self,
3019 struct frame_info *this_frame,
3020 void **this_prologue_cache)
3021{
3022 CORE_ADDR this_pc = get_frame_pc (this_frame);
3023
3024 /* No need to check is_m; this sniffer is only registered for
3025 M-profile architectures. */
3026
3027 /* Exception frames return to one of these magic PCs. Other values
3028 are not defined as of v7-M. See details in "B1.5.8 Exception
3029 return behavior" in "ARMv7-M Architecture Reference Manual". */
3030 if (this_pc == 0xfffffff1 || this_pc == 0xfffffff9
3031 || this_pc == 0xfffffffd)
3032 return 1;
3033
3034 return 0;
3035}
3036
3037/* Frame unwinder for M-profile exceptions. */
3038
3039struct frame_unwind arm_m_exception_unwind =
3040{
3041 SIGTRAMP_FRAME,
3042 default_frame_unwind_stop_reason,
3043 arm_m_exception_this_id,
3044 arm_m_exception_prev_register,
3045 NULL,
3046 arm_m_exception_unwind_sniffer
3047};
3048
24de872b 3049static CORE_ADDR
a262aec2 3050arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
24de872b
DJ
3051{
3052 struct arm_prologue_cache *cache;
3053
eb5492fa 3054 if (*this_cache == NULL)
a262aec2 3055 *this_cache = arm_make_prologue_cache (this_frame);
eb5492fa
DJ
3056 cache = *this_cache;
3057
4be43953 3058 return cache->prev_sp - cache->framesize;
24de872b
DJ
3059}
3060
eb5492fa
DJ
3061struct frame_base arm_normal_base = {
3062 &arm_prologue_unwind,
3063 arm_normal_frame_base,
3064 arm_normal_frame_base,
3065 arm_normal_frame_base
3066};
3067
a262aec2 3068/* Assuming THIS_FRAME is a dummy, return the frame ID of that
eb5492fa
DJ
3069 dummy frame. The frame ID's base needs to match the TOS value
3070 saved by save_dummy_frame_tos() and returned from
3071 arm_push_dummy_call, and the PC needs to match the dummy frame's
3072 breakpoint. */
c906108c 3073
eb5492fa 3074static struct frame_id
a262aec2 3075arm_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
c906108c 3076{
0963b4bd
MS
3077 return frame_id_build (get_frame_register_unsigned (this_frame,
3078 ARM_SP_REGNUM),
a262aec2 3079 get_frame_pc (this_frame));
eb5492fa 3080}
c3b4394c 3081
eb5492fa
DJ
3082/* Given THIS_FRAME, find the previous frame's resume PC (which will
3083 be used to construct the previous frame's ID, after looking up the
3084 containing function). */
c3b4394c 3085
eb5492fa
DJ
3086static CORE_ADDR
3087arm_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
3088{
3089 CORE_ADDR pc;
3090 pc = frame_unwind_register_unsigned (this_frame, ARM_PC_REGNUM);
24568a2c 3091 return arm_addr_bits_remove (gdbarch, pc);
eb5492fa
DJ
3092}
3093
3094static CORE_ADDR
3095arm_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
3096{
3097 return frame_unwind_register_unsigned (this_frame, ARM_SP_REGNUM);
c906108c
SS
3098}
3099
b39cc962
DJ
3100static struct value *
3101arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
3102 int regnum)
3103{
24568a2c 3104 struct gdbarch * gdbarch = get_frame_arch (this_frame);
b39cc962 3105 CORE_ADDR lr, cpsr;
9779414d 3106 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
b39cc962
DJ
3107
3108 switch (regnum)
3109 {
3110 case ARM_PC_REGNUM:
3111 /* The PC is normally copied from the return column, which
3112 describes saves of LR. However, that version may have an
3113 extra bit set to indicate Thumb state. The bit is not
3114 part of the PC. */
3115 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3116 return frame_unwind_got_constant (this_frame, regnum,
24568a2c 3117 arm_addr_bits_remove (gdbarch, lr));
b39cc962
DJ
3118
3119 case ARM_PS_REGNUM:
3120 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
ca38c58e 3121 cpsr = get_frame_register_unsigned (this_frame, regnum);
b39cc962
DJ
3122 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3123 if (IS_THUMB_ADDR (lr))
9779414d 3124 cpsr |= t_bit;
b39cc962 3125 else
9779414d 3126 cpsr &= ~t_bit;
ca38c58e 3127 return frame_unwind_got_constant (this_frame, regnum, cpsr);
b39cc962
DJ
3128
3129 default:
3130 internal_error (__FILE__, __LINE__,
3131 _("Unexpected register %d"), regnum);
3132 }
3133}
3134
3135static void
3136arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
3137 struct dwarf2_frame_state_reg *reg,
3138 struct frame_info *this_frame)
3139{
3140 switch (regnum)
3141 {
3142 case ARM_PC_REGNUM:
3143 case ARM_PS_REGNUM:
3144 reg->how = DWARF2_FRAME_REG_FN;
3145 reg->loc.fn = arm_dwarf2_prev_register;
3146 break;
3147 case ARM_SP_REGNUM:
3148 reg->how = DWARF2_FRAME_REG_CFA;
3149 break;
3150 }
3151}
3152
4024ca99
UW
3153/* Return true if we are in the function's epilogue, i.e. after the
3154 instruction that destroyed the function's stack frame. */
3155
3156static int
3157thumb_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3158{
3159 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3160 unsigned int insn, insn2;
3161 int found_return = 0, found_stack_adjust = 0;
3162 CORE_ADDR func_start, func_end;
3163 CORE_ADDR scan_pc;
3164 gdb_byte buf[4];
3165
3166 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3167 return 0;
3168
3169 /* The epilogue is a sequence of instructions along the following lines:
3170
3171 - add stack frame size to SP or FP
3172 - [if frame pointer used] restore SP from FP
3173 - restore registers from SP [may include PC]
3174 - a return-type instruction [if PC wasn't already restored]
3175
3176 In a first pass, we scan forward from the current PC and verify the
3177 instructions we find as compatible with this sequence, ending in a
3178 return instruction.
3179
3180 However, this is not sufficient to distinguish indirect function calls
3181 within a function from indirect tail calls in the epilogue in some cases.
3182 Therefore, if we didn't already find any SP-changing instruction during
3183 forward scan, we add a backward scanning heuristic to ensure we actually
3184 are in the epilogue. */
3185
3186 scan_pc = pc;
3187 while (scan_pc < func_end && !found_return)
3188 {
3189 if (target_read_memory (scan_pc, buf, 2))
3190 break;
3191
3192 scan_pc += 2;
3193 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3194
3195 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
3196 found_return = 1;
3197 else if (insn == 0x46f7) /* mov pc, lr */
3198 found_return = 1;
3199 else if (insn == 0x46bd) /* mov sp, r7 */
3200 found_stack_adjust = 1;
3201 else if ((insn & 0xff00) == 0xb000) /* add sp, imm or sub sp, imm */
3202 found_stack_adjust = 1;
3203 else if ((insn & 0xfe00) == 0xbc00) /* pop <registers> */
3204 {
3205 found_stack_adjust = 1;
3206 if (insn & 0x0100) /* <registers> include PC. */
3207 found_return = 1;
3208 }
db24da6d 3209 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instruction */
4024ca99
UW
3210 {
3211 if (target_read_memory (scan_pc, buf, 2))
3212 break;
3213
3214 scan_pc += 2;
3215 insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3216
3217 if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3218 {
3219 found_stack_adjust = 1;
3220 if (insn2 & 0x8000) /* <registers> include PC. */
3221 found_return = 1;
3222 }
3223 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3224 && (insn2 & 0x0fff) == 0x0b04)
3225 {
3226 found_stack_adjust = 1;
3227 if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC. */
3228 found_return = 1;
3229 }
3230 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3231 && (insn2 & 0x0e00) == 0x0a00)
3232 found_stack_adjust = 1;
3233 else
3234 break;
3235 }
3236 else
3237 break;
3238 }
3239
3240 if (!found_return)
3241 return 0;
3242
3243 /* Since any instruction in the epilogue sequence, with the possible
3244 exception of return itself, updates the stack pointer, we need to
3245 scan backwards for at most one instruction. Try either a 16-bit or
3246 a 32-bit instruction. This is just a heuristic, so we do not worry
0963b4bd 3247 too much about false positives. */
4024ca99
UW
3248
3249 if (!found_stack_adjust)
3250 {
3251 if (pc - 4 < func_start)
3252 return 0;
3253 if (target_read_memory (pc - 4, buf, 4))
3254 return 0;
3255
3256 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3257 insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);
3258
3259 if (insn2 == 0x46bd) /* mov sp, r7 */
3260 found_stack_adjust = 1;
3261 else if ((insn2 & 0xff00) == 0xb000) /* add sp, imm or sub sp, imm */
3262 found_stack_adjust = 1;
3263 else if ((insn2 & 0xff00) == 0xbc00) /* pop <registers> without PC */
3264 found_stack_adjust = 1;
3265 else if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3266 found_stack_adjust = 1;
3267 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3268 && (insn2 & 0x0fff) == 0x0b04)
3269 found_stack_adjust = 1;
3270 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3271 && (insn2 & 0x0e00) == 0x0a00)
3272 found_stack_adjust = 1;
3273 }
3274
3275 return found_stack_adjust;
3276}
3277
3278/* Return true if we are in the function's epilogue, i.e. after the
3279 instruction that destroyed the function's stack frame. */
3280
3281static int
3282arm_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3283{
3284 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3285 unsigned int insn;
3286 int found_return, found_stack_adjust;
3287 CORE_ADDR func_start, func_end;
3288
3289 if (arm_pc_is_thumb (gdbarch, pc))
3290 return thumb_in_function_epilogue_p (gdbarch, pc);
3291
3292 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3293 return 0;
3294
3295 /* We are in the epilogue if the previous instruction was a stack
3296 adjustment and the next instruction is a possible return (bx, mov
3297 pc, or pop). We could have to scan backwards to find the stack
3298 adjustment, or forwards to find the return, but this is a decent
3299 approximation. First scan forwards. */
3300
3301 found_return = 0;
3302 insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
3303 if (bits (insn, 28, 31) != INST_NV)
3304 {
3305 if ((insn & 0x0ffffff0) == 0x012fff10)
3306 /* BX. */
3307 found_return = 1;
3308 else if ((insn & 0x0ffffff0) == 0x01a0f000)
3309 /* MOV PC. */
3310 found_return = 1;
3311 else if ((insn & 0x0fff0000) == 0x08bd0000
3312 && (insn & 0x0000c000) != 0)
3313 /* POP (LDMIA), including PC or LR. */
3314 found_return = 1;
3315 }
3316
3317 if (!found_return)
3318 return 0;
3319
3320 /* Scan backwards. This is just a heuristic, so do not worry about
3321 false positives from mode changes. */
3322
3323 if (pc < func_start + 4)
3324 return 0;
3325
73c964d6 3326 found_stack_adjust = 0;
4024ca99
UW
3327 insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
3328 if (bits (insn, 28, 31) != INST_NV)
3329 {
3330 if ((insn & 0x0df0f000) == 0x0080d000)
3331 /* ADD SP (register or immediate). */
3332 found_stack_adjust = 1;
3333 else if ((insn & 0x0df0f000) == 0x0040d000)
3334 /* SUB SP (register or immediate). */
3335 found_stack_adjust = 1;
3336 else if ((insn & 0x0ffffff0) == 0x01a0d000)
3337 /* MOV SP. */
77bc0675 3338 found_stack_adjust = 1;
4024ca99
UW
3339 else if ((insn & 0x0fff0000) == 0x08bd0000)
3340 /* POP (LDMIA). */
3341 found_stack_adjust = 1;
fc51cce1
MGD
3342 else if ((insn & 0x0fff0000) == 0x049d0000)
3343 /* POP of a single register. */
3344 found_stack_adjust = 1;
4024ca99
UW
3345 }
3346
3347 if (found_stack_adjust)
3348 return 1;
3349
3350 return 0;
3351}
3352
3353
2dd604e7
RE
3354/* When arguments must be pushed onto the stack, they go on in reverse
3355 order. The code below implements a FILO (stack) to do this. */
3356
3357struct stack_item
3358{
3359 int len;
3360 struct stack_item *prev;
3361 void *data;
3362};
3363
3364static struct stack_item *
8c6363cf 3365push_stack_item (struct stack_item *prev, const void *contents, int len)
2dd604e7
RE
3366{
3367 struct stack_item *si;
3368 si = xmalloc (sizeof (struct stack_item));
226c7fbc 3369 si->data = xmalloc (len);
2dd604e7
RE
3370 si->len = len;
3371 si->prev = prev;
3372 memcpy (si->data, contents, len);
3373 return si;
3374}
3375
3376static struct stack_item *
3377pop_stack_item (struct stack_item *si)
3378{
3379 struct stack_item *dead = si;
3380 si = si->prev;
3381 xfree (dead->data);
3382 xfree (dead);
3383 return si;
3384}
3385
2af48f68
PB
3386
3387/* Return the alignment (in bytes) of the given type. */
3388
3389static int
3390arm_type_align (struct type *t)
3391{
3392 int n;
3393 int align;
3394 int falign;
3395
3396 t = check_typedef (t);
3397 switch (TYPE_CODE (t))
3398 {
3399 default:
3400 /* Should never happen. */
3401 internal_error (__FILE__, __LINE__, _("unknown type alignment"));
3402 return 4;
3403
3404 case TYPE_CODE_PTR:
3405 case TYPE_CODE_ENUM:
3406 case TYPE_CODE_INT:
3407 case TYPE_CODE_FLT:
3408 case TYPE_CODE_SET:
3409 case TYPE_CODE_RANGE:
2af48f68
PB
3410 case TYPE_CODE_REF:
3411 case TYPE_CODE_CHAR:
3412 case TYPE_CODE_BOOL:
3413 return TYPE_LENGTH (t);
3414
3415 case TYPE_CODE_ARRAY:
3416 case TYPE_CODE_COMPLEX:
3417 /* TODO: What about vector types? */
3418 return arm_type_align (TYPE_TARGET_TYPE (t));
3419
3420 case TYPE_CODE_STRUCT:
3421 case TYPE_CODE_UNION:
3422 align = 1;
3423 for (n = 0; n < TYPE_NFIELDS (t); n++)
3424 {
3425 falign = arm_type_align (TYPE_FIELD_TYPE (t, n));
3426 if (falign > align)
3427 align = falign;
3428 }
3429 return align;
3430 }
3431}
3432
90445bd3
DJ
3433/* Possible base types for a candidate for passing and returning in
3434 VFP registers. */
3435
3436enum arm_vfp_cprc_base_type
3437{
3438 VFP_CPRC_UNKNOWN,
3439 VFP_CPRC_SINGLE,
3440 VFP_CPRC_DOUBLE,
3441 VFP_CPRC_VEC64,
3442 VFP_CPRC_VEC128
3443};
3444
3445/* The length of one element of base type B. */
3446
3447static unsigned
3448arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
3449{
3450 switch (b)
3451 {
3452 case VFP_CPRC_SINGLE:
3453 return 4;
3454 case VFP_CPRC_DOUBLE:
3455 return 8;
3456 case VFP_CPRC_VEC64:
3457 return 8;
3458 case VFP_CPRC_VEC128:
3459 return 16;
3460 default:
3461 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3462 (int) b);
3463 }
3464}
3465
3466/* The character ('s', 'd' or 'q') for the type of VFP register used
3467 for passing base type B. */
3468
3469static int
3470arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
3471{
3472 switch (b)
3473 {
3474 case VFP_CPRC_SINGLE:
3475 return 's';
3476 case VFP_CPRC_DOUBLE:
3477 return 'd';
3478 case VFP_CPRC_VEC64:
3479 return 'd';
3480 case VFP_CPRC_VEC128:
3481 return 'q';
3482 default:
3483 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3484 (int) b);
3485 }
3486}
3487
3488/* Determine whether T may be part of a candidate for passing and
3489 returning in VFP registers, ignoring the limit on the total number
3490 of components. If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
3491 classification of the first valid component found; if it is not
3492 VFP_CPRC_UNKNOWN, all components must have the same classification
3493 as *BASE_TYPE. If it is found that T contains a type not permitted
3494 for passing and returning in VFP registers, a type differently
3495 classified from *BASE_TYPE, or two types differently classified
3496 from each other, return -1, otherwise return the total number of
3497 base-type elements found (possibly 0 in an empty structure or
3498 array). Vectors and complex types are not currently supported,
3499 matching the generic AAPCS support. */
3500
3501static int
3502arm_vfp_cprc_sub_candidate (struct type *t,
3503 enum arm_vfp_cprc_base_type *base_type)
3504{
3505 t = check_typedef (t);
3506 switch (TYPE_CODE (t))
3507 {
3508 case TYPE_CODE_FLT:
3509 switch (TYPE_LENGTH (t))
3510 {
3511 case 4:
3512 if (*base_type == VFP_CPRC_UNKNOWN)
3513 *base_type = VFP_CPRC_SINGLE;
3514 else if (*base_type != VFP_CPRC_SINGLE)
3515 return -1;
3516 return 1;
3517
3518 case 8:
3519 if (*base_type == VFP_CPRC_UNKNOWN)
3520 *base_type = VFP_CPRC_DOUBLE;
3521 else if (*base_type != VFP_CPRC_DOUBLE)
3522 return -1;
3523 return 1;
3524
3525 default:
3526 return -1;
3527 }
3528 break;
3529
3530 case TYPE_CODE_ARRAY:
3531 {
3532 int count;
3533 unsigned unitlen;
3534 count = arm_vfp_cprc_sub_candidate (TYPE_TARGET_TYPE (t), base_type);
3535 if (count == -1)
3536 return -1;
3537 if (TYPE_LENGTH (t) == 0)
3538 {
3539 gdb_assert (count == 0);
3540 return 0;
3541 }
3542 else if (count == 0)
3543 return -1;
3544 unitlen = arm_vfp_cprc_unit_length (*base_type);
3545 gdb_assert ((TYPE_LENGTH (t) % unitlen) == 0);
3546 return TYPE_LENGTH (t) / unitlen;
3547 }
3548 break;
3549
3550 case TYPE_CODE_STRUCT:
3551 {
3552 int count = 0;
3553 unsigned unitlen;
3554 int i;
3555 for (i = 0; i < TYPE_NFIELDS (t); i++)
3556 {
3557 int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3558 base_type);
3559 if (sub_count == -1)
3560 return -1;
3561 count += sub_count;
3562 }
3563 if (TYPE_LENGTH (t) == 0)
3564 {
3565 gdb_assert (count == 0);
3566 return 0;
3567 }
3568 else if (count == 0)
3569 return -1;
3570 unitlen = arm_vfp_cprc_unit_length (*base_type);
3571 if (TYPE_LENGTH (t) != unitlen * count)
3572 return -1;
3573 return count;
3574 }
3575
3576 case TYPE_CODE_UNION:
3577 {
3578 int count = 0;
3579 unsigned unitlen;
3580 int i;
3581 for (i = 0; i < TYPE_NFIELDS (t); i++)
3582 {
3583 int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3584 base_type);
3585 if (sub_count == -1)
3586 return -1;
3587 count = (count > sub_count ? count : sub_count);
3588 }
3589 if (TYPE_LENGTH (t) == 0)
3590 {
3591 gdb_assert (count == 0);
3592 return 0;
3593 }
3594 else if (count == 0)
3595 return -1;
3596 unitlen = arm_vfp_cprc_unit_length (*base_type);
3597 if (TYPE_LENGTH (t) != unitlen * count)
3598 return -1;
3599 return count;
3600 }
3601
3602 default:
3603 break;
3604 }
3605
3606 return -1;
3607}
3608
3609/* Determine whether T is a VFP co-processor register candidate (CPRC)
3610 if passed to or returned from a non-variadic function with the VFP
3611 ABI in effect. Return 1 if it is, 0 otherwise. If it is, set
3612 *BASE_TYPE to the base type for T and *COUNT to the number of
3613 elements of that base type before returning. */
3614
3615static int
3616arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
3617 int *count)
3618{
3619 enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
3620 int c = arm_vfp_cprc_sub_candidate (t, &b);
3621 if (c <= 0 || c > 4)
3622 return 0;
3623 *base_type = b;
3624 *count = c;
3625 return 1;
3626}
3627
3628/* Return 1 if the VFP ABI should be used for passing arguments to and
3629 returning values from a function of type FUNC_TYPE, 0
3630 otherwise. */
3631
3632static int
3633arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
3634{
3635 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3636 /* Variadic functions always use the base ABI. Assume that functions
3637 without debug info are not variadic. */
3638 if (func_type && TYPE_VARARGS (check_typedef (func_type)))
3639 return 0;
3640 /* The VFP ABI is only supported as a variant of AAPCS. */
3641 if (tdep->arm_abi != ARM_ABI_AAPCS)
3642 return 0;
3643 return gdbarch_tdep (gdbarch)->fp_model == ARM_FLOAT_VFP;
3644}
3645
3646/* We currently only support passing parameters in integer registers, which
3647 conforms with GCC's default model, and VFP argument passing following
3648 the VFP variant of AAPCS. Several other variants exist and
2dd604e7
RE
3649 we should probably support some of them based on the selected ABI. */
3650
3651static CORE_ADDR
7d9b040b 3652arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6a65450a
AC
3653 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
3654 struct value **args, CORE_ADDR sp, int struct_return,
3655 CORE_ADDR struct_addr)
2dd604e7 3656{
e17a4113 3657 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2dd604e7
RE
3658 int argnum;
3659 int argreg;
3660 int nstack;
3661 struct stack_item *si = NULL;
90445bd3
DJ
3662 int use_vfp_abi;
3663 struct type *ftype;
3664 unsigned vfp_regs_free = (1 << 16) - 1;
3665
3666 /* Determine the type of this function and whether the VFP ABI
3667 applies. */
3668 ftype = check_typedef (value_type (function));
3669 if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
3670 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
3671 use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
2dd604e7 3672
6a65450a
AC
3673 /* Set the return address. For the ARM, the return breakpoint is
3674 always at BP_ADDR. */
9779414d 3675 if (arm_pc_is_thumb (gdbarch, bp_addr))
9dca5578 3676 bp_addr |= 1;
6a65450a 3677 regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
2dd604e7
RE
3678
3679 /* Walk through the list of args and determine how large a temporary
3680 stack is required. Need to take care here as structs may be
7a9dd1b2 3681 passed on the stack, and we have to push them. */
2dd604e7
RE
3682 nstack = 0;
3683
3684 argreg = ARM_A1_REGNUM;
3685 nstack = 0;
3686
2dd604e7
RE
3687 /* The struct_return pointer occupies the first parameter
3688 passing register. */
3689 if (struct_return)
3690 {
3691 if (arm_debug)
5af949e3 3692 fprintf_unfiltered (gdb_stdlog, "struct return in %s = %s\n",
2af46ca0 3693 gdbarch_register_name (gdbarch, argreg),
5af949e3 3694 paddress (gdbarch, struct_addr));
2dd604e7
RE
3695 regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
3696 argreg++;
3697 }
3698
3699 for (argnum = 0; argnum < nargs; argnum++)
3700 {
3701 int len;
3702 struct type *arg_type;
3703 struct type *target_type;
3704 enum type_code typecode;
8c6363cf 3705 const bfd_byte *val;
2af48f68 3706 int align;
90445bd3
DJ
3707 enum arm_vfp_cprc_base_type vfp_base_type;
3708 int vfp_base_count;
3709 int may_use_core_reg = 1;
2dd604e7 3710
df407dfe 3711 arg_type = check_typedef (value_type (args[argnum]));
2dd604e7
RE
3712 len = TYPE_LENGTH (arg_type);
3713 target_type = TYPE_TARGET_TYPE (arg_type);
3714 typecode = TYPE_CODE (arg_type);
8c6363cf 3715 val = value_contents (args[argnum]);
2dd604e7 3716
2af48f68
PB
3717 align = arm_type_align (arg_type);
3718 /* Round alignment up to a whole number of words. */
3719 align = (align + INT_REGISTER_SIZE - 1) & ~(INT_REGISTER_SIZE - 1);
3720 /* Different ABIs have different maximum alignments. */
3721 if (gdbarch_tdep (gdbarch)->arm_abi == ARM_ABI_APCS)
3722 {
3723 /* The APCS ABI only requires word alignment. */
3724 align = INT_REGISTER_SIZE;
3725 }
3726 else
3727 {
3728 /* The AAPCS requires at most doubleword alignment. */
3729 if (align > INT_REGISTER_SIZE * 2)
3730 align = INT_REGISTER_SIZE * 2;
3731 }
3732
90445bd3
DJ
3733 if (use_vfp_abi
3734 && arm_vfp_call_candidate (arg_type, &vfp_base_type,
3735 &vfp_base_count))
3736 {
3737 int regno;
3738 int unit_length;
3739 int shift;
3740 unsigned mask;
3741
3742 /* Because this is a CPRC it cannot go in a core register or
3743 cause a core register to be skipped for alignment.
3744 Either it goes in VFP registers and the rest of this loop
3745 iteration is skipped for this argument, or it goes on the
3746 stack (and the stack alignment code is correct for this
3747 case). */
3748 may_use_core_reg = 0;
3749
3750 unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
3751 shift = unit_length / 4;
3752 mask = (1 << (shift * vfp_base_count)) - 1;
3753 for (regno = 0; regno < 16; regno += shift)
3754 if (((vfp_regs_free >> regno) & mask) == mask)
3755 break;
3756
3757 if (regno < 16)
3758 {
3759 int reg_char;
3760 int reg_scaled;
3761 int i;
3762
3763 vfp_regs_free &= ~(mask << regno);
3764 reg_scaled = regno / shift;
3765 reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
3766 for (i = 0; i < vfp_base_count; i++)
3767 {
3768 char name_buf[4];
3769 int regnum;
58d6951d
DJ
3770 if (reg_char == 'q')
3771 arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
90445bd3 3772 val + i * unit_length);
58d6951d
DJ
3773 else
3774 {
8c042590
PM
3775 xsnprintf (name_buf, sizeof (name_buf), "%c%d",
3776 reg_char, reg_scaled + i);
58d6951d
DJ
3777 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
3778 strlen (name_buf));
3779 regcache_cooked_write (regcache, regnum,
3780 val + i * unit_length);
3781 }
90445bd3
DJ
3782 }
3783 continue;
3784 }
3785 else
3786 {
3787 /* This CPRC could not go in VFP registers, so all VFP
3788 registers are now marked as used. */
3789 vfp_regs_free = 0;
3790 }
3791 }
3792
2af48f68
PB
3793 /* Push stack padding for dowubleword alignment. */
3794 if (nstack & (align - 1))
3795 {
3796 si = push_stack_item (si, val, INT_REGISTER_SIZE);
3797 nstack += INT_REGISTER_SIZE;
3798 }
3799
3800 /* Doubleword aligned quantities must go in even register pairs. */
90445bd3
DJ
3801 if (may_use_core_reg
3802 && argreg <= ARM_LAST_ARG_REGNUM
2af48f68
PB
3803 && align > INT_REGISTER_SIZE
3804 && argreg & 1)
3805 argreg++;
3806
2dd604e7
RE
3807 /* If the argument is a pointer to a function, and it is a
3808 Thumb function, create a LOCAL copy of the value and set
3809 the THUMB bit in it. */
3810 if (TYPE_CODE_PTR == typecode
3811 && target_type != NULL
f96b8fa0 3812 && TYPE_CODE_FUNC == TYPE_CODE (check_typedef (target_type)))
2dd604e7 3813 {
e17a4113 3814 CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
9779414d 3815 if (arm_pc_is_thumb (gdbarch, regval))
2dd604e7 3816 {
8c6363cf
TT
3817 bfd_byte *copy = alloca (len);
3818 store_unsigned_integer (copy, len, byte_order,
e17a4113 3819 MAKE_THUMB_ADDR (regval));
8c6363cf 3820 val = copy;
2dd604e7
RE
3821 }
3822 }
3823
3824 /* Copy the argument to general registers or the stack in
3825 register-sized pieces. Large arguments are split between
3826 registers and stack. */
3827 while (len > 0)
3828 {
f0c9063c 3829 int partial_len = len < INT_REGISTER_SIZE ? len : INT_REGISTER_SIZE;
2dd604e7 3830
90445bd3 3831 if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
2dd604e7
RE
3832 {
3833 /* The argument is being passed in a general purpose
3834 register. */
e17a4113
UW
3835 CORE_ADDR regval
3836 = extract_unsigned_integer (val, partial_len, byte_order);
3837 if (byte_order == BFD_ENDIAN_BIG)
8bf8793c 3838 regval <<= (INT_REGISTER_SIZE - partial_len) * 8;
2dd604e7
RE
3839 if (arm_debug)
3840 fprintf_unfiltered (gdb_stdlog, "arg %d in %s = 0x%s\n",
c9f4d572
UW
3841 argnum,
3842 gdbarch_register_name
2af46ca0 3843 (gdbarch, argreg),
f0c9063c 3844 phex (regval, INT_REGISTER_SIZE));
2dd604e7
RE
3845 regcache_cooked_write_unsigned (regcache, argreg, regval);
3846 argreg++;
3847 }
3848 else
3849 {
3850 /* Push the arguments onto the stack. */
3851 if (arm_debug)
3852 fprintf_unfiltered (gdb_stdlog, "arg %d @ sp + %d\n",
3853 argnum, nstack);
f0c9063c
UW
3854 si = push_stack_item (si, val, INT_REGISTER_SIZE);
3855 nstack += INT_REGISTER_SIZE;
2dd604e7
RE
3856 }
3857
3858 len -= partial_len;
3859 val += partial_len;
3860 }
3861 }
3862 /* If we have an odd number of words to push, then decrement the stack
3863 by one word now, so first stack argument will be dword aligned. */
3864 if (nstack & 4)
3865 sp -= 4;
3866
3867 while (si)
3868 {
3869 sp -= si->len;
3870 write_memory (sp, si->data, si->len);
3871 si = pop_stack_item (si);
3872 }
3873
3874 /* Finally, update teh SP register. */
3875 regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
3876
3877 return sp;
3878}
3879
f53f0d0b
PB
3880
3881/* Always align the frame to an 8-byte boundary. This is required on
3882 some platforms and harmless on the rest. */
3883
3884static CORE_ADDR
3885arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
3886{
3887 /* Align the stack to eight bytes. */
3888 return sp & ~ (CORE_ADDR) 7;
3889}
3890
c906108c 3891static void
12b27276 3892print_fpu_flags (struct ui_file *file, int flags)
c906108c 3893{
c5aa993b 3894 if (flags & (1 << 0))
12b27276 3895 fputs_filtered ("IVO ", file);
c5aa993b 3896 if (flags & (1 << 1))
12b27276 3897 fputs_filtered ("DVZ ", file);
c5aa993b 3898 if (flags & (1 << 2))
12b27276 3899 fputs_filtered ("OFL ", file);
c5aa993b 3900 if (flags & (1 << 3))
12b27276 3901 fputs_filtered ("UFL ", file);
c5aa993b 3902 if (flags & (1 << 4))
12b27276
WN
3903 fputs_filtered ("INX ", file);
3904 fputc_filtered ('\n', file);
c906108c
SS
3905}
3906
5e74b15c
RE
3907/* Print interesting information about the floating point processor
3908 (if present) or emulator. */
34e8f22d 3909static void
d855c300 3910arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
23e3a7ac 3911 struct frame_info *frame, const char *args)
c906108c 3912{
9c9acae0 3913 unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
c5aa993b
JM
3914 int type;
3915
3916 type = (status >> 24) & 127;
edefbb7c 3917 if (status & (1 << 31))
12b27276 3918 fprintf_filtered (file, _("Hardware FPU type %d\n"), type);
edefbb7c 3919 else
12b27276 3920 fprintf_filtered (file, _("Software FPU type %d\n"), type);
edefbb7c 3921 /* i18n: [floating point unit] mask */
12b27276
WN
3922 fputs_filtered (_("mask: "), file);
3923 print_fpu_flags (file, status >> 16);
edefbb7c 3924 /* i18n: [floating point unit] flags */
12b27276
WN
3925 fputs_filtered (_("flags: "), file);
3926 print_fpu_flags (file, status);
c906108c
SS
3927}
3928
27067745
UW
3929/* Construct the ARM extended floating point type. */
3930static struct type *
3931arm_ext_type (struct gdbarch *gdbarch)
3932{
3933 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3934
3935 if (!tdep->arm_ext_type)
3936 tdep->arm_ext_type
e9bb382b 3937 = arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
27067745
UW
3938 floatformats_arm_ext);
3939
3940 return tdep->arm_ext_type;
3941}
3942
58d6951d
DJ
3943static struct type *
3944arm_neon_double_type (struct gdbarch *gdbarch)
3945{
3946 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3947
3948 if (tdep->neon_double_type == NULL)
3949 {
3950 struct type *t, *elem;
3951
3952 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
3953 TYPE_CODE_UNION);
3954 elem = builtin_type (gdbarch)->builtin_uint8;
3955 append_composite_type_field (t, "u8", init_vector_type (elem, 8));
3956 elem = builtin_type (gdbarch)->builtin_uint16;
3957 append_composite_type_field (t, "u16", init_vector_type (elem, 4));
3958 elem = builtin_type (gdbarch)->builtin_uint32;
3959 append_composite_type_field (t, "u32", init_vector_type (elem, 2));
3960 elem = builtin_type (gdbarch)->builtin_uint64;
3961 append_composite_type_field (t, "u64", elem);
3962 elem = builtin_type (gdbarch)->builtin_float;
3963 append_composite_type_field (t, "f32", init_vector_type (elem, 2));
3964 elem = builtin_type (gdbarch)->builtin_double;
3965 append_composite_type_field (t, "f64", elem);
3966
3967 TYPE_VECTOR (t) = 1;
3968 TYPE_NAME (t) = "neon_d";
3969 tdep->neon_double_type = t;
3970 }
3971
3972 return tdep->neon_double_type;
3973}
3974
3975/* FIXME: The vector types are not correctly ordered on big-endian
3976 targets. Just as s0 is the low bits of d0, d0[0] is also the low
3977 bits of d0 - regardless of what unit size is being held in d0. So
3978 the offset of the first uint8 in d0 is 7, but the offset of the
3979 first float is 4. This code works as-is for little-endian
3980 targets. */
3981
3982static struct type *
3983arm_neon_quad_type (struct gdbarch *gdbarch)
3984{
3985 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3986
3987 if (tdep->neon_quad_type == NULL)
3988 {
3989 struct type *t, *elem;
3990
3991 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
3992 TYPE_CODE_UNION);
3993 elem = builtin_type (gdbarch)->builtin_uint8;
3994 append_composite_type_field (t, "u8", init_vector_type (elem, 16));
3995 elem = builtin_type (gdbarch)->builtin_uint16;
3996 append_composite_type_field (t, "u16", init_vector_type (elem, 8));
3997 elem = builtin_type (gdbarch)->builtin_uint32;
3998 append_composite_type_field (t, "u32", init_vector_type (elem, 4));
3999 elem = builtin_type (gdbarch)->builtin_uint64;
4000 append_composite_type_field (t, "u64", init_vector_type (elem, 2));
4001 elem = builtin_type (gdbarch)->builtin_float;
4002 append_composite_type_field (t, "f32", init_vector_type (elem, 4));
4003 elem = builtin_type (gdbarch)->builtin_double;
4004 append_composite_type_field (t, "f64", init_vector_type (elem, 2));
4005
4006 TYPE_VECTOR (t) = 1;
4007 TYPE_NAME (t) = "neon_q";
4008 tdep->neon_quad_type = t;
4009 }
4010
4011 return tdep->neon_quad_type;
4012}
4013
34e8f22d
RE
4014/* Return the GDB type object for the "standard" data type of data in
4015 register N. */
4016
4017static struct type *
7a5ea0d4 4018arm_register_type (struct gdbarch *gdbarch, int regnum)
032758dc 4019{
58d6951d
DJ
4020 int num_regs = gdbarch_num_regs (gdbarch);
4021
4022 if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
4023 && regnum >= num_regs && regnum < num_regs + 32)
4024 return builtin_type (gdbarch)->builtin_float;
4025
4026 if (gdbarch_tdep (gdbarch)->have_neon_pseudos
4027 && regnum >= num_regs + 32 && regnum < num_regs + 32 + 16)
4028 return arm_neon_quad_type (gdbarch);
4029
4030 /* If the target description has register information, we are only
4031 in this function so that we can override the types of
4032 double-precision registers for NEON. */
4033 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
4034 {
4035 struct type *t = tdesc_register_type (gdbarch, regnum);
4036
4037 if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
4038 && TYPE_CODE (t) == TYPE_CODE_FLT
4039 && gdbarch_tdep (gdbarch)->have_neon)
4040 return arm_neon_double_type (gdbarch);
4041 else
4042 return t;
4043 }
4044
34e8f22d 4045 if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
58d6951d
DJ
4046 {
4047 if (!gdbarch_tdep (gdbarch)->have_fpa_registers)
4048 return builtin_type (gdbarch)->builtin_void;
4049
4050 return arm_ext_type (gdbarch);
4051 }
e4c16157 4052 else if (regnum == ARM_SP_REGNUM)
0dfff4cb 4053 return builtin_type (gdbarch)->builtin_data_ptr;
e4c16157 4054 else if (regnum == ARM_PC_REGNUM)
0dfff4cb 4055 return builtin_type (gdbarch)->builtin_func_ptr;
ff6f572f
DJ
4056 else if (regnum >= ARRAY_SIZE (arm_register_names))
4057 /* These registers are only supported on targets which supply
4058 an XML description. */
df4df182 4059 return builtin_type (gdbarch)->builtin_int0;
032758dc 4060 else
df4df182 4061 return builtin_type (gdbarch)->builtin_uint32;
032758dc
AC
4062}
4063
ff6f572f
DJ
4064/* Map a DWARF register REGNUM onto the appropriate GDB register
4065 number. */
4066
4067static int
d3f73121 4068arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
ff6f572f
DJ
4069{
4070 /* Core integer regs. */
4071 if (reg >= 0 && reg <= 15)
4072 return reg;
4073
4074 /* Legacy FPA encoding. These were once used in a way which
4075 overlapped with VFP register numbering, so their use is
4076 discouraged, but GDB doesn't support the ARM toolchain
4077 which used them for VFP. */
4078 if (reg >= 16 && reg <= 23)
4079 return ARM_F0_REGNUM + reg - 16;
4080
4081 /* New assignments for the FPA registers. */
4082 if (reg >= 96 && reg <= 103)
4083 return ARM_F0_REGNUM + reg - 96;
4084
4085 /* WMMX register assignments. */
4086 if (reg >= 104 && reg <= 111)
4087 return ARM_WCGR0_REGNUM + reg - 104;
4088
4089 if (reg >= 112 && reg <= 127)
4090 return ARM_WR0_REGNUM + reg - 112;
4091
4092 if (reg >= 192 && reg <= 199)
4093 return ARM_WC0_REGNUM + reg - 192;
4094
58d6951d
DJ
4095 /* VFP v2 registers. A double precision value is actually
4096 in d1 rather than s2, but the ABI only defines numbering
4097 for the single precision registers. This will "just work"
4098 in GDB for little endian targets (we'll read eight bytes,
4099 starting in s0 and then progressing to s1), but will be
4100 reversed on big endian targets with VFP. This won't
4101 be a problem for the new Neon quad registers; you're supposed
4102 to use DW_OP_piece for those. */
4103 if (reg >= 64 && reg <= 95)
4104 {
4105 char name_buf[4];
4106
8c042590 4107 xsnprintf (name_buf, sizeof (name_buf), "s%d", reg - 64);
58d6951d
DJ
4108 return user_reg_map_name_to_regnum (gdbarch, name_buf,
4109 strlen (name_buf));
4110 }
4111
4112 /* VFP v3 / Neon registers. This range is also used for VFP v2
4113 registers, except that it now describes d0 instead of s0. */
4114 if (reg >= 256 && reg <= 287)
4115 {
4116 char name_buf[4];
4117
8c042590 4118 xsnprintf (name_buf, sizeof (name_buf), "d%d", reg - 256);
58d6951d
DJ
4119 return user_reg_map_name_to_regnum (gdbarch, name_buf,
4120 strlen (name_buf));
4121 }
4122
ff6f572f
DJ
4123 return -1;
4124}
4125
26216b98
AC
4126/* Map GDB internal REGNUM onto the Arm simulator register numbers. */
4127static int
e7faf938 4128arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
26216b98
AC
4129{
4130 int reg = regnum;
e7faf938 4131 gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
26216b98 4132
ff6f572f
DJ
4133 if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
4134 return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
4135
4136 if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
4137 return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
4138
4139 if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
4140 return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
4141
26216b98
AC
4142 if (reg < NUM_GREGS)
4143 return SIM_ARM_R0_REGNUM + reg;
4144 reg -= NUM_GREGS;
4145
4146 if (reg < NUM_FREGS)
4147 return SIM_ARM_FP0_REGNUM + reg;
4148 reg -= NUM_FREGS;
4149
4150 if (reg < NUM_SREGS)
4151 return SIM_ARM_FPS_REGNUM + reg;
4152 reg -= NUM_SREGS;
4153
edefbb7c 4154 internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
26216b98 4155}
34e8f22d 4156
a37b3cc0
AC
4157/* NOTE: cagney/2001-08-20: Both convert_from_extended() and
4158 convert_to_extended() use floatformat_arm_ext_littlebyte_bigword.
4159 It is thought that this is is the floating-point register format on
4160 little-endian systems. */
c906108c 4161
ed9a39eb 4162static void
b508a996 4163convert_from_extended (const struct floatformat *fmt, const void *ptr,
be8626e0 4164 void *dbl, int endianess)
c906108c 4165{
a37b3cc0 4166 DOUBLEST d;
be8626e0
MD
4167
4168 if (endianess == BFD_ENDIAN_BIG)
a37b3cc0
AC
4169 floatformat_to_doublest (&floatformat_arm_ext_big, ptr, &d);
4170 else
4171 floatformat_to_doublest (&floatformat_arm_ext_littlebyte_bigword,
4172 ptr, &d);
b508a996 4173 floatformat_from_doublest (fmt, &d, dbl);
c906108c
SS
4174}
4175
34e8f22d 4176static void
be8626e0
MD
4177convert_to_extended (const struct floatformat *fmt, void *dbl, const void *ptr,
4178 int endianess)
c906108c 4179{
a37b3cc0 4180 DOUBLEST d;
be8626e0 4181
b508a996 4182 floatformat_to_doublest (fmt, ptr, &d);
be8626e0 4183 if (endianess == BFD_ENDIAN_BIG)
a37b3cc0
AC
4184 floatformat_from_doublest (&floatformat_arm_ext_big, &d, dbl);
4185 else
4186 floatformat_from_doublest (&floatformat_arm_ext_littlebyte_bigword,
4187 &d, dbl);
c906108c 4188}
ed9a39eb 4189
c906108c 4190static int
ed9a39eb 4191condition_true (unsigned long cond, unsigned long status_reg)
c906108c
SS
4192{
4193 if (cond == INST_AL || cond == INST_NV)
4194 return 1;
4195
4196 switch (cond)
4197 {
4198 case INST_EQ:
4199 return ((status_reg & FLAG_Z) != 0);
4200 case INST_NE:
4201 return ((status_reg & FLAG_Z) == 0);
4202 case INST_CS:
4203 return ((status_reg & FLAG_C) != 0);
4204 case INST_CC:
4205 return ((status_reg & FLAG_C) == 0);
4206 case INST_MI:
4207 return ((status_reg & FLAG_N) != 0);
4208 case INST_PL:
4209 return ((status_reg & FLAG_N) == 0);
4210 case INST_VS:
4211 return ((status_reg & FLAG_V) != 0);
4212 case INST_VC:
4213 return ((status_reg & FLAG_V) == 0);
4214 case INST_HI:
4215 return ((status_reg & (FLAG_C | FLAG_Z)) == FLAG_C);
4216 case INST_LS:
4217 return ((status_reg & (FLAG_C | FLAG_Z)) != FLAG_C);
4218 case INST_GE:
4219 return (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0));
4220 case INST_LT:
4221 return (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0));
4222 case INST_GT:
f8bf5763
PM
4223 return (((status_reg & FLAG_Z) == 0)
4224 && (((status_reg & FLAG_N) == 0)
4225 == ((status_reg & FLAG_V) == 0)));
c906108c 4226 case INST_LE:
f8bf5763
PM
4227 return (((status_reg & FLAG_Z) != 0)
4228 || (((status_reg & FLAG_N) == 0)
4229 != ((status_reg & FLAG_V) == 0)));
c906108c
SS
4230 }
4231 return 1;
4232}
4233
c906108c 4234static unsigned long
0b1b3e42
UW
4235shifted_reg_val (struct frame_info *frame, unsigned long inst, int carry,
4236 unsigned long pc_val, unsigned long status_reg)
c906108c
SS
4237{
4238 unsigned long res, shift;
4239 int rm = bits (inst, 0, 3);
4240 unsigned long shifttype = bits (inst, 5, 6);
c5aa993b
JM
4241
4242 if (bit (inst, 4))
c906108c
SS
4243 {
4244 int rs = bits (inst, 8, 11);
0b1b3e42
UW
4245 shift = (rs == 15 ? pc_val + 8
4246 : get_frame_register_unsigned (frame, rs)) & 0xFF;
c906108c
SS
4247 }
4248 else
4249 shift = bits (inst, 7, 11);
c5aa993b 4250
bf9f652a 4251 res = (rm == ARM_PC_REGNUM
0d39a070 4252 ? (pc_val + (bit (inst, 4) ? 12 : 8))
0b1b3e42 4253 : get_frame_register_unsigned (frame, rm));
c906108c
SS
4254
4255 switch (shifttype)
4256 {
c5aa993b 4257 case 0: /* LSL */
c906108c
SS
4258 res = shift >= 32 ? 0 : res << shift;
4259 break;
c5aa993b
JM
4260
4261 case 1: /* LSR */
c906108c
SS
4262 res = shift >= 32 ? 0 : res >> shift;
4263 break;
4264
c5aa993b
JM
4265 case 2: /* ASR */
4266 if (shift >= 32)
4267 shift = 31;
c906108c
SS
4268 res = ((res & 0x80000000L)
4269 ? ~((~res) >> shift) : res >> shift);
4270 break;
4271
c5aa993b 4272 case 3: /* ROR/RRX */
c906108c
SS
4273 shift &= 31;
4274 if (shift == 0)
4275 res = (res >> 1) | (carry ? 0x80000000L : 0);
4276 else
c5aa993b 4277 res = (res >> shift) | (res << (32 - shift));
c906108c
SS
4278 break;
4279 }
4280
4281 return res & 0xffffffff;
4282}
4283
c906108c
SS
4284/* Return number of 1-bits in VAL. */
4285
4286static int
ed9a39eb 4287bitcount (unsigned long val)
c906108c
SS
4288{
4289 int nbits;
4290 for (nbits = 0; val != 0; nbits++)
0963b4bd 4291 val &= val - 1; /* Delete rightmost 1-bit in val. */
c906108c
SS
4292 return nbits;
4293}
4294
177321bd
DJ
4295/* Return the size in bytes of the complete Thumb instruction whose
4296 first halfword is INST1. */
4297
4298static int
4299thumb_insn_size (unsigned short inst1)
4300{
4301 if ((inst1 & 0xe000) == 0xe000 && (inst1 & 0x1800) != 0)
4302 return 4;
4303 else
4304 return 2;
4305}
4306
4307static int
4308thumb_advance_itstate (unsigned int itstate)
4309{
4310 /* Preserve IT[7:5], the first three bits of the condition. Shift
4311 the upcoming condition flags left by one bit. */
4312 itstate = (itstate & 0xe0) | ((itstate << 1) & 0x1f);
4313
4314 /* If we have finished the IT block, clear the state. */
4315 if ((itstate & 0x0f) == 0)
4316 itstate = 0;
4317
4318 return itstate;
4319}
4320
4321/* Find the next PC after the current instruction executes. In some
4322 cases we can not statically determine the answer (see the IT state
4323 handling in this function); in that case, a breakpoint may be
4324 inserted in addition to the returned PC, which will be used to set
4325 another breakpoint by our caller. */
4326
ad527d2e 4327static CORE_ADDR
18819fa6 4328thumb_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc)
c906108c 4329{
2af46ca0 4330 struct gdbarch *gdbarch = get_frame_arch (frame);
177321bd 4331 struct address_space *aspace = get_frame_address_space (frame);
e17a4113
UW
4332 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4333 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
c5aa993b 4334 unsigned long pc_val = ((unsigned long) pc) + 4; /* PC after prefetch */
e17a4113 4335 unsigned short inst1;
0963b4bd 4336 CORE_ADDR nextpc = pc + 2; /* Default is next instruction. */
c906108c 4337 unsigned long offset;
177321bd 4338 ULONGEST status, itstate;
c906108c 4339
50e98be4
DJ
4340 nextpc = MAKE_THUMB_ADDR (nextpc);
4341 pc_val = MAKE_THUMB_ADDR (pc_val);
4342
e17a4113 4343 inst1 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
9d4fde75 4344
9dca5578
DJ
4345 /* Thumb-2 conditional execution support. There are eight bits in
4346 the CPSR which describe conditional execution state. Once
4347 reconstructed (they're in a funny order), the low five bits
4348 describe the low bit of the condition for each instruction and
4349 how many instructions remain. The high three bits describe the
4350 base condition. One of the low four bits will be set if an IT
4351 block is active. These bits read as zero on earlier
4352 processors. */
4353 status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
177321bd 4354 itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
9dca5578 4355
177321bd
DJ
4356 /* If-Then handling. On GNU/Linux, where this routine is used, we
4357 use an undefined instruction as a breakpoint. Unlike BKPT, IT
4358 can disable execution of the undefined instruction. So we might
4359 miss the breakpoint if we set it on a skipped conditional
4360 instruction. Because conditional instructions can change the
4361 flags, affecting the execution of further instructions, we may
4362 need to set two breakpoints. */
9dca5578 4363
177321bd
DJ
4364 if (gdbarch_tdep (gdbarch)->thumb2_breakpoint != NULL)
4365 {
4366 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
4367 {
4368 /* An IT instruction. Because this instruction does not
4369 modify the flags, we can accurately predict the next
4370 executed instruction. */
4371 itstate = inst1 & 0x00ff;
4372 pc += thumb_insn_size (inst1);
4373
4374 while (itstate != 0 && ! condition_true (itstate >> 4, status))
4375 {
0963b4bd
MS
4376 inst1 = read_memory_unsigned_integer (pc, 2,
4377 byte_order_for_code);
177321bd
DJ
4378 pc += thumb_insn_size (inst1);
4379 itstate = thumb_advance_itstate (itstate);
4380 }
4381
50e98be4 4382 return MAKE_THUMB_ADDR (pc);
177321bd
DJ
4383 }
4384 else if (itstate != 0)
4385 {
4386 /* We are in a conditional block. Check the condition. */
4387 if (! condition_true (itstate >> 4, status))
4388 {
4389 /* Advance to the next executed instruction. */
4390 pc += thumb_insn_size (inst1);
4391 itstate = thumb_advance_itstate (itstate);
4392
4393 while (itstate != 0 && ! condition_true (itstate >> 4, status))
4394 {
0963b4bd
MS
4395 inst1 = read_memory_unsigned_integer (pc, 2,
4396 byte_order_for_code);
177321bd
DJ
4397 pc += thumb_insn_size (inst1);
4398 itstate = thumb_advance_itstate (itstate);
4399 }
4400
50e98be4 4401 return MAKE_THUMB_ADDR (pc);
177321bd
DJ
4402 }
4403 else if ((itstate & 0x0f) == 0x08)
4404 {
4405 /* This is the last instruction of the conditional
4406 block, and it is executed. We can handle it normally
4407 because the following instruction is not conditional,
4408 and we must handle it normally because it is
4409 permitted to branch. Fall through. */
4410 }
4411 else
4412 {
4413 int cond_negated;
4414
4415 /* There are conditional instructions after this one.
4416 If this instruction modifies the flags, then we can
4417 not predict what the next executed instruction will
4418 be. Fortunately, this instruction is architecturally
4419 forbidden to branch; we know it will fall through.
4420 Start by skipping past it. */
4421 pc += thumb_insn_size (inst1);
4422 itstate = thumb_advance_itstate (itstate);
4423
4424 /* Set a breakpoint on the following instruction. */
4425 gdb_assert ((itstate & 0x0f) != 0);
18819fa6
UW
4426 arm_insert_single_step_breakpoint (gdbarch, aspace,
4427 MAKE_THUMB_ADDR (pc));
177321bd
DJ
4428 cond_negated = (itstate >> 4) & 1;
4429
4430 /* Skip all following instructions with the same
4431 condition. If there is a later instruction in the IT
4432 block with the opposite condition, set the other
4433 breakpoint there. If not, then set a breakpoint on
4434 the instruction after the IT block. */
4435 do
4436 {
0963b4bd
MS
4437 inst1 = read_memory_unsigned_integer (pc, 2,
4438 byte_order_for_code);
177321bd
DJ
4439 pc += thumb_insn_size (inst1);
4440 itstate = thumb_advance_itstate (itstate);
4441 }
4442 while (itstate != 0 && ((itstate >> 4) & 1) == cond_negated);
4443
50e98be4 4444 return MAKE_THUMB_ADDR (pc);
177321bd
DJ
4445 }
4446 }
4447 }
4448 else if (itstate & 0x0f)
9dca5578
DJ
4449 {
4450 /* We are in a conditional block. Check the condition. */
177321bd 4451 int cond = itstate >> 4;
9dca5578
DJ
4452
4453 if (! condition_true (cond, status))
db24da6d
YQ
4454 /* Advance to the next instruction. All the 32-bit
4455 instructions share a common prefix. */
4456 return MAKE_THUMB_ADDR (pc + thumb_insn_size (inst1));
177321bd
DJ
4457
4458 /* Otherwise, handle the instruction normally. */
9dca5578
DJ
4459 }
4460
c906108c
SS
4461 if ((inst1 & 0xff00) == 0xbd00) /* pop {rlist, pc} */
4462 {
4463 CORE_ADDR sp;
4464
4465 /* Fetch the saved PC from the stack. It's stored above
4466 all of the other registers. */
f0c9063c 4467 offset = bitcount (bits (inst1, 0, 7)) * INT_REGISTER_SIZE;
0b1b3e42 4468 sp = get_frame_register_unsigned (frame, ARM_SP_REGNUM);
e17a4113 4469 nextpc = read_memory_unsigned_integer (sp + offset, 4, byte_order);
c906108c
SS
4470 }
4471 else if ((inst1 & 0xf000) == 0xd000) /* conditional branch */
4472 {
c5aa993b 4473 unsigned long cond = bits (inst1, 8, 11);
25b41d01
YQ
4474 if (cond == 0x0f) /* 0x0f = SWI */
4475 {
4476 struct gdbarch_tdep *tdep;
4477 tdep = gdbarch_tdep (gdbarch);
4478
4479 if (tdep->syscall_next_pc != NULL)
4480 nextpc = tdep->syscall_next_pc (frame);
4481
4482 }
4483 else if (cond != 0x0f && condition_true (cond, status))
c906108c
SS
4484 nextpc = pc_val + (sbits (inst1, 0, 7) << 1);
4485 }
4486 else if ((inst1 & 0xf800) == 0xe000) /* unconditional branch */
4487 {
4488 nextpc = pc_val + (sbits (inst1, 0, 10) << 1);
4489 }
db24da6d 4490 else if (thumb_insn_size (inst1) == 4) /* 32-bit instruction */
c906108c 4491 {
e17a4113
UW
4492 unsigned short inst2;
4493 inst2 = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);
9dca5578
DJ
4494
4495 /* Default to the next instruction. */
4496 nextpc = pc + 4;
50e98be4 4497 nextpc = MAKE_THUMB_ADDR (nextpc);
9dca5578
DJ
4498
4499 if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
4500 {
4501 /* Branches and miscellaneous control instructions. */
4502
4503 if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
4504 {
4505 /* B, BL, BLX. */
4506 int j1, j2, imm1, imm2;
4507
4508 imm1 = sbits (inst1, 0, 10);
4509 imm2 = bits (inst2, 0, 10);
4510 j1 = bit (inst2, 13);
4511 j2 = bit (inst2, 11);
4512
4513 offset = ((imm1 << 12) + (imm2 << 1));
4514 offset ^= ((!j2) << 22) | ((!j1) << 23);
4515
4516 nextpc = pc_val + offset;
4517 /* For BLX make sure to clear the low bits. */
4518 if (bit (inst2, 12) == 0)
4519 nextpc = nextpc & 0xfffffffc;
4520 }
4521 else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
4522 {
4523 /* SUBS PC, LR, #imm8. */
4524 nextpc = get_frame_register_unsigned (frame, ARM_LR_REGNUM);
4525 nextpc -= inst2 & 0x00ff;
4526 }
4069ebbe 4527 else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
9dca5578
DJ
4528 {
4529 /* Conditional branch. */
4530 if (condition_true (bits (inst1, 6, 9), status))
4531 {
4532 int sign, j1, j2, imm1, imm2;
4533
4534 sign = sbits (inst1, 10, 10);
4535 imm1 = bits (inst1, 0, 5);
4536 imm2 = bits (inst2, 0, 10);
4537 j1 = bit (inst2, 13);
4538 j2 = bit (inst2, 11);
4539
4540 offset = (sign << 20) + (j2 << 19) + (j1 << 18);
4541 offset += (imm1 << 12) + (imm2 << 1);
4542
4543 nextpc = pc_val + offset;
4544 }
4545 }
4546 }
4547 else if ((inst1 & 0xfe50) == 0xe810)
4548 {
4549 /* Load multiple or RFE. */
4550 int rn, offset, load_pc = 1;
4551
4552 rn = bits (inst1, 0, 3);
4553 if (bit (inst1, 7) && !bit (inst1, 8))
4554 {
4555 /* LDMIA or POP */
4556 if (!bit (inst2, 15))
4557 load_pc = 0;
4558 offset = bitcount (inst2) * 4 - 4;
4559 }
4560 else if (!bit (inst1, 7) && bit (inst1, 8))
4561 {
4562 /* LDMDB */
4563 if (!bit (inst2, 15))
4564 load_pc = 0;
4565 offset = -4;
4566 }
4567 else if (bit (inst1, 7) && bit (inst1, 8))
4568 {
4569 /* RFEIA */
4570 offset = 0;
4571 }
4572 else if (!bit (inst1, 7) && !bit (inst1, 8))
4573 {
4574 /* RFEDB */
4575 offset = -8;
4576 }
4577 else
4578 load_pc = 0;
4579
4580 if (load_pc)
4581 {
4582 CORE_ADDR addr = get_frame_register_unsigned (frame, rn);
4583 nextpc = get_frame_memory_unsigned (frame, addr + offset, 4);
4584 }
4585 }
4586 else if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
4587 {
4588 /* MOV PC or MOVS PC. */
4589 nextpc = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
50e98be4 4590 nextpc = MAKE_THUMB_ADDR (nextpc);
9dca5578
DJ
4591 }
4592 else if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
4593 {
4594 /* LDR PC. */
4595 CORE_ADDR base;
4596 int rn, load_pc = 1;
4597
4598 rn = bits (inst1, 0, 3);
4599 base = get_frame_register_unsigned (frame, rn);
bf9f652a 4600 if (rn == ARM_PC_REGNUM)
9dca5578
DJ
4601 {
4602 base = (base + 4) & ~(CORE_ADDR) 0x3;
4603 if (bit (inst1, 7))
4604 base += bits (inst2, 0, 11);
4605 else
4606 base -= bits (inst2, 0, 11);
4607 }
4608 else if (bit (inst1, 7))
4609 base += bits (inst2, 0, 11);
4610 else if (bit (inst2, 11))
4611 {
4612 if (bit (inst2, 10))
4613 {
4614 if (bit (inst2, 9))
4615 base += bits (inst2, 0, 7);
4616 else
4617 base -= bits (inst2, 0, 7);
4618 }
4619 }
4620 else if ((inst2 & 0x0fc0) == 0x0000)
4621 {
4622 int shift = bits (inst2, 4, 5), rm = bits (inst2, 0, 3);
4623 base += get_frame_register_unsigned (frame, rm) << shift;
4624 }
4625 else
4626 /* Reserved. */
4627 load_pc = 0;
4628
4629 if (load_pc)
4630 nextpc = get_frame_memory_unsigned (frame, base, 4);
4631 }
4632 else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
4633 {
4634 /* TBB. */
d476da0e
RE
4635 CORE_ADDR tbl_reg, table, offset, length;
4636
4637 tbl_reg = bits (inst1, 0, 3);
4638 if (tbl_reg == 0x0f)
4639 table = pc + 4; /* Regcache copy of PC isn't right yet. */
4640 else
4641 table = get_frame_register_unsigned (frame, tbl_reg);
9dca5578 4642
9dca5578
DJ
4643 offset = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
4644 length = 2 * get_frame_memory_unsigned (frame, table + offset, 1);
4645 nextpc = pc_val + length;
4646 }
d476da0e 4647 else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
9dca5578
DJ
4648 {
4649 /* TBH. */
d476da0e
RE
4650 CORE_ADDR tbl_reg, table, offset, length;
4651
4652 tbl_reg = bits (inst1, 0, 3);
4653 if (tbl_reg == 0x0f)
4654 table = pc + 4; /* Regcache copy of PC isn't right yet. */
4655 else
4656 table = get_frame_register_unsigned (frame, tbl_reg);
9dca5578 4657
9dca5578
DJ
4658 offset = 2 * get_frame_register_unsigned (frame, bits (inst2, 0, 3));
4659 length = 2 * get_frame_memory_unsigned (frame, table + offset, 2);
4660 nextpc = pc_val + length;
4661 }
c906108c 4662 }
aa17d93e 4663 else if ((inst1 & 0xff00) == 0x4700) /* bx REG, blx REG */
9498281f
DJ
4664 {
4665 if (bits (inst1, 3, 6) == 0x0f)
6ca1b147 4666 nextpc = UNMAKE_THUMB_ADDR (pc_val);
9498281f 4667 else
0b1b3e42 4668 nextpc = get_frame_register_unsigned (frame, bits (inst1, 3, 6));
9498281f 4669 }
ad8b5167
UW
4670 else if ((inst1 & 0xff87) == 0x4687) /* mov pc, REG */
4671 {
4672 if (bits (inst1, 3, 6) == 0x0f)
4673 nextpc = pc_val;
4674 else
4675 nextpc = get_frame_register_unsigned (frame, bits (inst1, 3, 6));
4676
4677 nextpc = MAKE_THUMB_ADDR (nextpc);
4678 }
9dca5578
DJ
4679 else if ((inst1 & 0xf500) == 0xb100)
4680 {
4681 /* CBNZ or CBZ. */
4682 int imm = (bit (inst1, 9) << 6) + (bits (inst1, 3, 7) << 1);
4683 ULONGEST reg = get_frame_register_unsigned (frame, bits (inst1, 0, 2));
4684
4685 if (bit (inst1, 11) && reg != 0)
4686 nextpc = pc_val + imm;
4687 else if (!bit (inst1, 11) && reg == 0)
4688 nextpc = pc_val + imm;
4689 }
c906108c
SS
4690 return nextpc;
4691}
4692
50e98be4 4693/* Get the raw next address. PC is the current program counter, in
18819fa6 4694 FRAME, which is assumed to be executing in ARM mode.
50e98be4
DJ
4695
4696 The value returned has the execution state of the next instruction
4697 encoded in it. Use IS_THUMB_ADDR () to see whether the instruction is
4698 in Thumb-State, and gdbarch_addr_bits_remove () to get the plain memory
0963b4bd
MS
4699 address. */
4700
50e98be4 4701static CORE_ADDR
18819fa6 4702arm_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc)
c906108c 4703{
2af46ca0 4704 struct gdbarch *gdbarch = get_frame_arch (frame);
e17a4113
UW
4705 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4706 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
c906108c
SS
4707 unsigned long pc_val;
4708 unsigned long this_instr;
4709 unsigned long status;
4710 CORE_ADDR nextpc;
4711
c906108c 4712 pc_val = (unsigned long) pc;
e17a4113 4713 this_instr = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
9d4fde75 4714
0b1b3e42 4715 status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
c5aa993b 4716 nextpc = (CORE_ADDR) (pc_val + 4); /* Default case */
c906108c 4717
daddc3c1
DJ
4718 if (bits (this_instr, 28, 31) == INST_NV)
4719 switch (bits (this_instr, 24, 27))
4720 {
4721 case 0xa:
4722 case 0xb:
4723 {
4724 /* Branch with Link and change to Thumb. */
4725 nextpc = BranchDest (pc, this_instr);
4726 nextpc |= bit (this_instr, 24) << 1;
50e98be4 4727 nextpc = MAKE_THUMB_ADDR (nextpc);
daddc3c1
DJ
4728 break;
4729 }
4730 case 0xc:
4731 case 0xd:
4732 case 0xe:
4733 /* Coprocessor register transfer. */
4734 if (bits (this_instr, 12, 15) == 15)
4735 error (_("Invalid update to pc in instruction"));
4736 break;
4737 }
4738 else if (condition_true (bits (this_instr, 28, 31), status))
c906108c
SS
4739 {
4740 switch (bits (this_instr, 24, 27))
4741 {
c5aa993b 4742 case 0x0:
94c30b78 4743 case 0x1: /* data processing */
c5aa993b
JM
4744 case 0x2:
4745 case 0x3:
c906108c
SS
4746 {
4747 unsigned long operand1, operand2, result = 0;
4748 unsigned long rn;
4749 int c;
c5aa993b 4750
c906108c
SS
4751 if (bits (this_instr, 12, 15) != 15)
4752 break;
4753
4754 if (bits (this_instr, 22, 25) == 0
c5aa993b 4755 && bits (this_instr, 4, 7) == 9) /* multiply */
edefbb7c 4756 error (_("Invalid update to pc in instruction"));
c906108c 4757
9498281f 4758 /* BX <reg>, BLX <reg> */
e150acc7
PB
4759 if (bits (this_instr, 4, 27) == 0x12fff1
4760 || bits (this_instr, 4, 27) == 0x12fff3)
9498281f
DJ
4761 {
4762 rn = bits (this_instr, 0, 3);
bf9f652a
YQ
4763 nextpc = ((rn == ARM_PC_REGNUM)
4764 ? (pc_val + 8)
4765 : get_frame_register_unsigned (frame, rn));
4766
9498281f
DJ
4767 return nextpc;
4768 }
4769
0963b4bd 4770 /* Multiply into PC. */
c906108c
SS
4771 c = (status & FLAG_C) ? 1 : 0;
4772 rn = bits (this_instr, 16, 19);
bf9f652a
YQ
4773 operand1 = ((rn == ARM_PC_REGNUM)
4774 ? (pc_val + 8)
4775 : get_frame_register_unsigned (frame, rn));
c5aa993b 4776
c906108c
SS
4777 if (bit (this_instr, 25))
4778 {
4779 unsigned long immval = bits (this_instr, 0, 7);
4780 unsigned long rotate = 2 * bits (this_instr, 8, 11);
c5aa993b
JM
4781 operand2 = ((immval >> rotate) | (immval << (32 - rotate)))
4782 & 0xffffffff;
c906108c 4783 }
0963b4bd
MS
4784 else /* operand 2 is a shifted register. */
4785 operand2 = shifted_reg_val (frame, this_instr, c,
4786 pc_val, status);
c5aa993b 4787
c906108c
SS
4788 switch (bits (this_instr, 21, 24))
4789 {
c5aa993b 4790 case 0x0: /*and */
c906108c
SS
4791 result = operand1 & operand2;
4792 break;
4793
c5aa993b 4794 case 0x1: /*eor */
c906108c
SS
4795 result = operand1 ^ operand2;
4796 break;
4797
c5aa993b 4798 case 0x2: /*sub */
c906108c
SS
4799 result = operand1 - operand2;
4800 break;
4801
c5aa993b 4802 case 0x3: /*rsb */
c906108c
SS
4803 result = operand2 - operand1;
4804 break;
4805
c5aa993b 4806 case 0x4: /*add */
c906108c
SS
4807 result = operand1 + operand2;
4808 break;
4809
c5aa993b 4810 case 0x5: /*adc */
c906108c
SS
4811 result = operand1 + operand2 + c;
4812 break;
4813
c5aa993b 4814 case 0x6: /*sbc */
c906108c
SS
4815 result = operand1 - operand2 + c;
4816 break;
4817
c5aa993b 4818 case 0x7: /*rsc */
c906108c
SS
4819 result = operand2 - operand1 + c;
4820 break;
4821
c5aa993b
JM
4822 case 0x8:
4823 case 0x9:
4824 case 0xa:
4825 case 0xb: /* tst, teq, cmp, cmn */
c906108c
SS
4826 result = (unsigned long) nextpc;
4827 break;
4828
c5aa993b 4829 case 0xc: /*orr */
c906108c
SS
4830 result = operand1 | operand2;
4831 break;
4832
c5aa993b 4833 case 0xd: /*mov */
c906108c
SS
4834 /* Always step into a function. */
4835 result = operand2;
c5aa993b 4836 break;
c906108c 4837
c5aa993b 4838 case 0xe: /*bic */
c906108c
SS
4839 result = operand1 & ~operand2;
4840 break;
4841
c5aa993b 4842 case 0xf: /*mvn */
c906108c
SS
4843 result = ~operand2;
4844 break;
4845 }
c906108c 4846
50e98be4
DJ
4847 /* In 26-bit APCS the bottom two bits of the result are
4848 ignored, and we always end up in ARM state. */
4849 if (!arm_apcs_32)
4850 nextpc = arm_addr_bits_remove (gdbarch, result);
4851 else
4852 nextpc = result;
4853
c906108c
SS
4854 break;
4855 }
c5aa993b
JM
4856
4857 case 0x4:
4858 case 0x5: /* data transfer */
4859 case 0x6:
4860 case 0x7:
c906108c
SS
4861 if (bit (this_instr, 20))
4862 {
4863 /* load */
4864 if (bits (this_instr, 12, 15) == 15)
4865 {
4866 /* rd == pc */
c5aa993b 4867 unsigned long rn;
c906108c 4868 unsigned long base;
c5aa993b 4869
c906108c 4870 if (bit (this_instr, 22))
edefbb7c 4871 error (_("Invalid update to pc in instruction"));
c906108c
SS
4872
4873 /* byte write to PC */
4874 rn = bits (this_instr, 16, 19);
bf9f652a
YQ
4875 base = ((rn == ARM_PC_REGNUM)
4876 ? (pc_val + 8)
4877 : get_frame_register_unsigned (frame, rn));
4878
c906108c
SS
4879 if (bit (this_instr, 24))
4880 {
4881 /* pre-indexed */
4882 int c = (status & FLAG_C) ? 1 : 0;
4883 unsigned long offset =
c5aa993b 4884 (bit (this_instr, 25)
0b1b3e42 4885 ? shifted_reg_val (frame, this_instr, c, pc_val, status)
c5aa993b 4886 : bits (this_instr, 0, 11));
c906108c
SS
4887
4888 if (bit (this_instr, 23))
4889 base += offset;
4890 else
4891 base -= offset;
4892 }
51370a33
YQ
4893 nextpc =
4894 (CORE_ADDR) read_memory_unsigned_integer ((CORE_ADDR) base,
4895 4, byte_order);
c906108c
SS
4896 }
4897 }
4898 break;
c5aa993b
JM
4899
4900 case 0x8:
4901 case 0x9: /* block transfer */
c906108c
SS
4902 if (bit (this_instr, 20))
4903 {
4904 /* LDM */
4905 if (bit (this_instr, 15))
4906 {
4907 /* loading pc */
4908 int offset = 0;
51370a33
YQ
4909 unsigned long rn_val
4910 = get_frame_register_unsigned (frame,
4911 bits (this_instr, 16, 19));
c906108c
SS
4912
4913 if (bit (this_instr, 23))
4914 {
4915 /* up */
4916 unsigned long reglist = bits (this_instr, 0, 14);
4917 offset = bitcount (reglist) * 4;
c5aa993b 4918 if (bit (this_instr, 24)) /* pre */
c906108c
SS
4919 offset += 4;
4920 }
4921 else if (bit (this_instr, 24))
4922 offset = -4;
c5aa993b 4923
51370a33
YQ
4924 nextpc =
4925 (CORE_ADDR) read_memory_unsigned_integer ((CORE_ADDR)
4926 (rn_val + offset),
4927 4, byte_order);
c906108c
SS
4928 }
4929 }
4930 break;
c5aa993b
JM
4931
4932 case 0xb: /* branch & link */
4933 case 0xa: /* branch */
c906108c
SS
4934 {
4935 nextpc = BranchDest (pc, this_instr);
c906108c
SS
4936 break;
4937 }
c5aa993b
JM
4938
4939 case 0xc:
4940 case 0xd:
4941 case 0xe: /* coproc ops */
25b41d01 4942 break;
c5aa993b 4943 case 0xf: /* SWI */
25b41d01
YQ
4944 {
4945 struct gdbarch_tdep *tdep;
4946 tdep = gdbarch_tdep (gdbarch);
4947
4948 if (tdep->syscall_next_pc != NULL)
4949 nextpc = tdep->syscall_next_pc (frame);
4950
4951 }
c906108c
SS
4952 break;
4953
4954 default:
edefbb7c 4955 fprintf_filtered (gdb_stderr, _("Bad bit-field extraction\n"));
c906108c
SS
4956 return (pc);
4957 }
4958 }
4959
4960 return nextpc;
4961}
4962
18819fa6
UW
4963/* Determine next PC after current instruction executes. Will call either
4964 arm_get_next_pc_raw or thumb_get_next_pc_raw. Error out if infinite
4965 loop is detected. */
4966
50e98be4
DJ
4967CORE_ADDR
4968arm_get_next_pc (struct frame_info *frame, CORE_ADDR pc)
4969{
18819fa6
UW
4970 CORE_ADDR nextpc;
4971
4972 if (arm_frame_is_thumb (frame))
2b59118e 4973 nextpc = thumb_get_next_pc_raw (frame, pc);
18819fa6 4974 else
2b59118e 4975 nextpc = arm_get_next_pc_raw (frame, pc);
18819fa6 4976
50e98be4
DJ
4977 return nextpc;
4978}
4979
18819fa6
UW
4980/* Like insert_single_step_breakpoint, but make sure we use a breakpoint
4981 of the appropriate mode (as encoded in the PC value), even if this
4982 differs from what would be expected according to the symbol tables. */
4983
4984void
4985arm_insert_single_step_breakpoint (struct gdbarch *gdbarch,
4986 struct address_space *aspace,
4987 CORE_ADDR pc)
4988{
4989 struct cleanup *old_chain
4990 = make_cleanup_restore_integer (&arm_override_mode);
4991
4992 arm_override_mode = IS_THUMB_ADDR (pc);
4993 pc = gdbarch_addr_bits_remove (gdbarch, pc);
4994
4995 insert_single_step_breakpoint (gdbarch, aspace, pc);
4996
4997 do_cleanups (old_chain);
4998}
4999
35f73cfc
UW
5000/* Checks for an atomic sequence of instructions beginning with a LDREX{,B,H,D}
5001 instruction and ending with a STREX{,B,H,D} instruction. If such a sequence
5002 is found, attempt to step through it. A breakpoint is placed at the end of
5003 the sequence. */
5004
5005static int
5006thumb_deal_with_atomic_sequence_raw (struct frame_info *frame)
5007{
5008 struct gdbarch *gdbarch = get_frame_arch (frame);
5009 struct address_space *aspace = get_frame_address_space (frame);
5010 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
5011 CORE_ADDR pc = get_frame_pc (frame);
5012 CORE_ADDR breaks[2] = {-1, -1};
5013 CORE_ADDR loc = pc;
5014 unsigned short insn1, insn2;
5015 int insn_count;
5016 int index;
5017 int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed). */
5018 const int atomic_sequence_length = 16; /* Instruction sequence length. */
5019 ULONGEST status, itstate;
5020
5021 /* We currently do not support atomic sequences within an IT block. */
5022 status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
5023 itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
5024 if (itstate & 0x0f)
5025 return 0;
5026
5027 /* Assume all atomic sequences start with a ldrex{,b,h,d} instruction. */
5028 insn1 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
5029 loc += 2;
5030 if (thumb_insn_size (insn1) != 4)
5031 return 0;
5032
5033 insn2 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
5034 loc += 2;
5035 if (!((insn1 & 0xfff0) == 0xe850
5036 || ((insn1 & 0xfff0) == 0xe8d0 && (insn2 & 0x00c0) == 0x0040)))
5037 return 0;
5038
5039 /* Assume that no atomic sequence is longer than "atomic_sequence_length"
5040 instructions. */
5041 for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
5042 {
5043 insn1 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
5044 loc += 2;
5045
5046 if (thumb_insn_size (insn1) != 4)
5047 {
5048 /* Assume that there is at most one conditional branch in the
5049 atomic sequence. If a conditional branch is found, put a
5050 breakpoint in its destination address. */
5051 if ((insn1 & 0xf000) == 0xd000 && bits (insn1, 8, 11) != 0x0f)
5052 {
5053 if (last_breakpoint > 0)
5054 return 0; /* More than one conditional branch found,
5055 fallback to the standard code. */
5056
5057 breaks[1] = loc + 2 + (sbits (insn1, 0, 7) << 1);
5058 last_breakpoint++;
5059 }
5060
5061 /* We do not support atomic sequences that use any *other*
5062 instructions but conditional branches to change the PC.
5063 Fall back to standard code to avoid losing control of
5064 execution. */
5065 else if (thumb_instruction_changes_pc (insn1))
5066 return 0;
5067 }
5068 else
5069 {
5070 insn2 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
5071 loc += 2;
5072
5073 /* Assume that there is at most one conditional branch in the
5074 atomic sequence. If a conditional branch is found, put a
5075 breakpoint in its destination address. */
5076 if ((insn1 & 0xf800) == 0xf000
5077 && (insn2 & 0xd000) == 0x8000
5078 && (insn1 & 0x0380) != 0x0380)
5079 {
5080 int sign, j1, j2, imm1, imm2;
5081 unsigned int offset;
5082
5083 sign = sbits (insn1, 10, 10);
5084 imm1 = bits (insn1, 0, 5);
5085 imm2 = bits (insn2, 0, 10);
5086 j1 = bit (insn2, 13);
5087 j2 = bit (insn2, 11);
5088
5089 offset = (sign << 20) + (j2 << 19) + (j1 << 18);
5090 offset += (imm1 << 12) + (imm2 << 1);
5091
5092 if (last_breakpoint > 0)
5093 return 0; /* More than one conditional branch found,
5094 fallback to the standard code. */
5095
5096 breaks[1] = loc + offset;
5097 last_breakpoint++;
5098 }
5099
5100 /* We do not support atomic sequences that use any *other*
5101 instructions but conditional branches to change the PC.
5102 Fall back to standard code to avoid losing control of
5103 execution. */
5104 else if (thumb2_instruction_changes_pc (insn1, insn2))
5105 return 0;
5106
5107 /* If we find a strex{,b,h,d}, we're done. */
5108 if ((insn1 & 0xfff0) == 0xe840
5109 || ((insn1 & 0xfff0) == 0xe8c0 && (insn2 & 0x00c0) == 0x0040))
5110 break;
5111 }
5112 }
5113
5114 /* If we didn't find the strex{,b,h,d}, we cannot handle the sequence. */
5115 if (insn_count == atomic_sequence_length)
5116 return 0;
5117
5118 /* Insert a breakpoint right after the end of the atomic sequence. */
5119 breaks[0] = loc;
5120
5121 /* Check for duplicated breakpoints. Check also for a breakpoint
5122 placed (branch instruction's destination) anywhere in sequence. */
5123 if (last_breakpoint
5124 && (breaks[1] == breaks[0]
5125 || (breaks[1] >= pc && breaks[1] < loc)))
5126 last_breakpoint = 0;
5127
5128 /* Effectively inserts the breakpoints. */
5129 for (index = 0; index <= last_breakpoint; index++)
5130 arm_insert_single_step_breakpoint (gdbarch, aspace,
5131 MAKE_THUMB_ADDR (breaks[index]));
5132
5133 return 1;
5134}
5135
5136static int
5137arm_deal_with_atomic_sequence_raw (struct frame_info *frame)
5138{
5139 struct gdbarch *gdbarch = get_frame_arch (frame);
5140 struct address_space *aspace = get_frame_address_space (frame);
5141 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
5142 CORE_ADDR pc = get_frame_pc (frame);
5143 CORE_ADDR breaks[2] = {-1, -1};
5144 CORE_ADDR loc = pc;
5145 unsigned int insn;
5146 int insn_count;
5147 int index;
5148 int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed). */
5149 const int atomic_sequence_length = 16; /* Instruction sequence length. */
5150
5151 /* Assume all atomic sequences start with a ldrex{,b,h,d} instruction.
5152 Note that we do not currently support conditionally executed atomic
5153 instructions. */
5154 insn = read_memory_unsigned_integer (loc, 4, byte_order_for_code);
5155 loc += 4;
5156 if ((insn & 0xff9000f0) != 0xe1900090)
5157 return 0;
5158
5159 /* Assume that no atomic sequence is longer than "atomic_sequence_length"
5160 instructions. */
5161 for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
5162 {
5163 insn = read_memory_unsigned_integer (loc, 4, byte_order_for_code);
5164 loc += 4;
5165
5166 /* Assume that there is at most one conditional branch in the atomic
5167 sequence. If a conditional branch is found, put a breakpoint in
5168 its destination address. */
5169 if (bits (insn, 24, 27) == 0xa)
5170 {
5171 if (last_breakpoint > 0)
5172 return 0; /* More than one conditional branch found, fallback
5173 to the standard single-step code. */
5174
5175 breaks[1] = BranchDest (loc - 4, insn);
5176 last_breakpoint++;
5177 }
5178
5179 /* We do not support atomic sequences that use any *other* instructions
5180 but conditional branches to change the PC. Fall back to standard
5181 code to avoid losing control of execution. */
5182 else if (arm_instruction_changes_pc (insn))
5183 return 0;
5184
5185 /* If we find a strex{,b,h,d}, we're done. */
5186 if ((insn & 0xff9000f0) == 0xe1800090)
5187 break;
5188 }
5189
5190 /* If we didn't find the strex{,b,h,d}, we cannot handle the sequence. */
5191 if (insn_count == atomic_sequence_length)
5192 return 0;
5193
5194 /* Insert a breakpoint right after the end of the atomic sequence. */
5195 breaks[0] = loc;
5196
5197 /* Check for duplicated breakpoints. Check also for a breakpoint
5198 placed (branch instruction's destination) anywhere in sequence. */
5199 if (last_breakpoint
5200 && (breaks[1] == breaks[0]
5201 || (breaks[1] >= pc && breaks[1] < loc)))
5202 last_breakpoint = 0;
5203
5204 /* Effectively inserts the breakpoints. */
5205 for (index = 0; index <= last_breakpoint; index++)
5206 arm_insert_single_step_breakpoint (gdbarch, aspace, breaks[index]);
5207
5208 return 1;
5209}
5210
5211int
5212arm_deal_with_atomic_sequence (struct frame_info *frame)
5213{
5214 if (arm_frame_is_thumb (frame))
5215 return thumb_deal_with_atomic_sequence_raw (frame);
5216 else
5217 return arm_deal_with_atomic_sequence_raw (frame);
5218}
5219
9512d7fd
FN
5220/* single_step() is called just before we want to resume the inferior,
5221 if we want to single-step it but there is no hardware or kernel
5222 single-step support. We find the target of the coming instruction
e0cd558a 5223 and breakpoint it. */
9512d7fd 5224
190dce09 5225int
0b1b3e42 5226arm_software_single_step (struct frame_info *frame)
9512d7fd 5227{
a6d9a66e 5228 struct gdbarch *gdbarch = get_frame_arch (frame);
6c95b8df 5229 struct address_space *aspace = get_frame_address_space (frame);
35f73cfc
UW
5230 CORE_ADDR next_pc;
5231
5232 if (arm_deal_with_atomic_sequence (frame))
5233 return 1;
18819fa6 5234
35f73cfc 5235 next_pc = arm_get_next_pc (frame, get_frame_pc (frame));
18819fa6 5236 arm_insert_single_step_breakpoint (gdbarch, aspace, next_pc);
e6590a1b
UW
5237
5238 return 1;
9512d7fd 5239}
9512d7fd 5240
f9d67f43
DJ
5241/* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
5242 the buffer to be NEW_LEN bytes ending at ENDADDR. Return
5243 NULL if an error occurs. BUF is freed. */
5244
5245static gdb_byte *
5246extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
5247 int old_len, int new_len)
5248{
22e048c9 5249 gdb_byte *new_buf;
f9d67f43
DJ
5250 int bytes_to_read = new_len - old_len;
5251
5252 new_buf = xmalloc (new_len);
5253 memcpy (new_buf + bytes_to_read, buf, old_len);
5254 xfree (buf);
5255 if (target_read_memory (endaddr - new_len, new_buf, bytes_to_read) != 0)
5256 {
5257 xfree (new_buf);
5258 return NULL;
5259 }
5260 return new_buf;
5261}
5262
5263/* An IT block is at most the 2-byte IT instruction followed by
5264 four 4-byte instructions. The furthest back we must search to
5265 find an IT block that affects the current instruction is thus
5266 2 + 3 * 4 == 14 bytes. */
5267#define MAX_IT_BLOCK_PREFIX 14
5268
5269/* Use a quick scan if there are more than this many bytes of
5270 code. */
5271#define IT_SCAN_THRESHOLD 32
5272
5273/* Adjust a breakpoint's address to move breakpoints out of IT blocks.
5274 A breakpoint in an IT block may not be hit, depending on the
5275 condition flags. */
5276static CORE_ADDR
5277arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
5278{
5279 gdb_byte *buf;
5280 char map_type;
5281 CORE_ADDR boundary, func_start;
22e048c9 5282 int buf_len;
f9d67f43
DJ
5283 enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
5284 int i, any, last_it, last_it_count;
5285
5286 /* If we are using BKPT breakpoints, none of this is necessary. */
5287 if (gdbarch_tdep (gdbarch)->thumb2_breakpoint == NULL)
5288 return bpaddr;
5289
5290 /* ARM mode does not have this problem. */
9779414d 5291 if (!arm_pc_is_thumb (gdbarch, bpaddr))
f9d67f43
DJ
5292 return bpaddr;
5293
5294 /* We are setting a breakpoint in Thumb code that could potentially
5295 contain an IT block. The first step is to find how much Thumb
5296 code there is; we do not need to read outside of known Thumb
5297 sequences. */
5298 map_type = arm_find_mapping_symbol (bpaddr, &boundary);
5299 if (map_type == 0)
5300 /* Thumb-2 code must have mapping symbols to have a chance. */
5301 return bpaddr;
5302
5303 bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
5304
5305 if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
5306 && func_start > boundary)
5307 boundary = func_start;
5308
5309 /* Search for a candidate IT instruction. We have to do some fancy
5310 footwork to distinguish a real IT instruction from the second
5311 half of a 32-bit instruction, but there is no need for that if
5312 there's no candidate. */
5313 buf_len = min (bpaddr - boundary, MAX_IT_BLOCK_PREFIX);
5314 if (buf_len == 0)
5315 /* No room for an IT instruction. */
5316 return bpaddr;
5317
5318 buf = xmalloc (buf_len);
5319 if (target_read_memory (bpaddr - buf_len, buf, buf_len) != 0)
5320 return bpaddr;
5321 any = 0;
5322 for (i = 0; i < buf_len; i += 2)
5323 {
5324 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5325 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5326 {
5327 any = 1;
5328 break;
5329 }
5330 }
5331 if (any == 0)
5332 {
5333 xfree (buf);
5334 return bpaddr;
5335 }
5336
5337 /* OK, the code bytes before this instruction contain at least one
5338 halfword which resembles an IT instruction. We know that it's
5339 Thumb code, but there are still two possibilities. Either the
5340 halfword really is an IT instruction, or it is the second half of
5341 a 32-bit Thumb instruction. The only way we can tell is to
5342 scan forwards from a known instruction boundary. */
5343 if (bpaddr - boundary > IT_SCAN_THRESHOLD)
5344 {
5345 int definite;
5346
5347 /* There's a lot of code before this instruction. Start with an
5348 optimistic search; it's easy to recognize halfwords that can
5349 not be the start of a 32-bit instruction, and use that to
5350 lock on to the instruction boundaries. */
5351 buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
5352 if (buf == NULL)
5353 return bpaddr;
5354 buf_len = IT_SCAN_THRESHOLD;
5355
5356 definite = 0;
5357 for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
5358 {
5359 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5360 if (thumb_insn_size (inst1) == 2)
5361 {
5362 definite = 1;
5363 break;
5364 }
5365 }
5366
5367 /* At this point, if DEFINITE, BUF[I] is the first place we
5368 are sure that we know the instruction boundaries, and it is far
5369 enough from BPADDR that we could not miss an IT instruction
5370 affecting BPADDR. If ! DEFINITE, give up - start from a
5371 known boundary. */
5372 if (! definite)
5373 {
0963b4bd
MS
5374 buf = extend_buffer_earlier (buf, bpaddr, buf_len,
5375 bpaddr - boundary);
f9d67f43
DJ
5376 if (buf == NULL)
5377 return bpaddr;
5378 buf_len = bpaddr - boundary;
5379 i = 0;
5380 }
5381 }
5382 else
5383 {
5384 buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
5385 if (buf == NULL)
5386 return bpaddr;
5387 buf_len = bpaddr - boundary;
5388 i = 0;
5389 }
5390
5391 /* Scan forwards. Find the last IT instruction before BPADDR. */
5392 last_it = -1;
5393 last_it_count = 0;
5394 while (i < buf_len)
5395 {
5396 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5397 last_it_count--;
5398 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5399 {
5400 last_it = i;
5401 if (inst1 & 0x0001)
5402 last_it_count = 4;
5403 else if (inst1 & 0x0002)
5404 last_it_count = 3;
5405 else if (inst1 & 0x0004)
5406 last_it_count = 2;
5407 else
5408 last_it_count = 1;
5409 }
5410 i += thumb_insn_size (inst1);
5411 }
5412
5413 xfree (buf);
5414
5415 if (last_it == -1)
5416 /* There wasn't really an IT instruction after all. */
5417 return bpaddr;
5418
5419 if (last_it_count < 1)
5420 /* It was too far away. */
5421 return bpaddr;
5422
5423 /* This really is a trouble spot. Move the breakpoint to the IT
5424 instruction. */
5425 return bpaddr - buf_len + last_it;
5426}
5427
cca44b1b 5428/* ARM displaced stepping support.
c906108c 5429
cca44b1b 5430 Generally ARM displaced stepping works as follows:
c906108c 5431
cca44b1b
JB
5432 1. When an instruction is to be single-stepped, it is first decoded by
5433 arm_process_displaced_insn (called from arm_displaced_step_copy_insn).
5434 Depending on the type of instruction, it is then copied to a scratch
5435 location, possibly in a modified form. The copy_* set of functions
0963b4bd 5436 performs such modification, as necessary. A breakpoint is placed after
cca44b1b
JB
5437 the modified instruction in the scratch space to return control to GDB.
5438 Note in particular that instructions which modify the PC will no longer
5439 do so after modification.
c5aa993b 5440
cca44b1b
JB
5441 2. The instruction is single-stepped, by setting the PC to the scratch
5442 location address, and resuming. Control returns to GDB when the
5443 breakpoint is hit.
c5aa993b 5444
cca44b1b
JB
5445 3. A cleanup function (cleanup_*) is called corresponding to the copy_*
5446 function used for the current instruction. This function's job is to
5447 put the CPU/memory state back to what it would have been if the
5448 instruction had been executed unmodified in its original location. */
c5aa993b 5449
cca44b1b
JB
5450/* NOP instruction (mov r0, r0). */
5451#define ARM_NOP 0xe1a00000
34518530 5452#define THUMB_NOP 0x4600
cca44b1b
JB
5453
5454/* Helper for register reads for displaced stepping. In particular, this
5455 returns the PC as it would be seen by the instruction at its original
5456 location. */
5457
5458ULONGEST
36073a92
YQ
5459displaced_read_reg (struct regcache *regs, struct displaced_step_closure *dsc,
5460 int regno)
cca44b1b
JB
5461{
5462 ULONGEST ret;
36073a92 5463 CORE_ADDR from = dsc->insn_addr;
cca44b1b 5464
bf9f652a 5465 if (regno == ARM_PC_REGNUM)
cca44b1b 5466 {
4db71c0b
YQ
5467 /* Compute pipeline offset:
5468 - When executing an ARM instruction, PC reads as the address of the
5469 current instruction plus 8.
5470 - When executing a Thumb instruction, PC reads as the address of the
5471 current instruction plus 4. */
5472
36073a92 5473 if (!dsc->is_thumb)
4db71c0b
YQ
5474 from += 8;
5475 else
5476 from += 4;
5477
cca44b1b
JB
5478 if (debug_displaced)
5479 fprintf_unfiltered (gdb_stdlog, "displaced: read pc value %.8lx\n",
4db71c0b
YQ
5480 (unsigned long) from);
5481 return (ULONGEST) from;
cca44b1b 5482 }
c906108c 5483 else
cca44b1b
JB
5484 {
5485 regcache_cooked_read_unsigned (regs, regno, &ret);
5486 if (debug_displaced)
5487 fprintf_unfiltered (gdb_stdlog, "displaced: read r%d value %.8lx\n",
5488 regno, (unsigned long) ret);
5489 return ret;
5490 }
c906108c
SS
5491}
5492
cca44b1b
JB
5493static int
5494displaced_in_arm_mode (struct regcache *regs)
5495{
5496 ULONGEST ps;
9779414d 5497 ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
66e810cd 5498
cca44b1b 5499 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
66e810cd 5500
9779414d 5501 return (ps & t_bit) == 0;
cca44b1b 5502}
66e810cd 5503
cca44b1b 5504/* Write to the PC as from a branch instruction. */
c906108c 5505
cca44b1b 5506static void
36073a92
YQ
5507branch_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5508 ULONGEST val)
c906108c 5509{
36073a92 5510 if (!dsc->is_thumb)
cca44b1b
JB
5511 /* Note: If bits 0/1 are set, this branch would be unpredictable for
5512 architecture versions < 6. */
0963b4bd
MS
5513 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5514 val & ~(ULONGEST) 0x3);
cca44b1b 5515 else
0963b4bd
MS
5516 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5517 val & ~(ULONGEST) 0x1);
cca44b1b 5518}
66e810cd 5519
cca44b1b
JB
5520/* Write to the PC as from a branch-exchange instruction. */
5521
5522static void
5523bx_write_pc (struct regcache *regs, ULONGEST val)
5524{
5525 ULONGEST ps;
9779414d 5526 ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
cca44b1b
JB
5527
5528 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
5529
5530 if ((val & 1) == 1)
c906108c 5531 {
9779414d 5532 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
cca44b1b
JB
5533 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
5534 }
5535 else if ((val & 2) == 0)
5536 {
9779414d 5537 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 5538 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
c906108c
SS
5539 }
5540 else
5541 {
cca44b1b
JB
5542 /* Unpredictable behaviour. Try to do something sensible (switch to ARM
5543 mode, align dest to 4 bytes). */
5544 warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
9779414d 5545 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 5546 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
c906108c
SS
5547 }
5548}
ed9a39eb 5549
cca44b1b 5550/* Write to the PC as if from a load instruction. */
ed9a39eb 5551
34e8f22d 5552static void
36073a92
YQ
5553load_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5554 ULONGEST val)
ed9a39eb 5555{
cca44b1b
JB
5556 if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
5557 bx_write_pc (regs, val);
5558 else
36073a92 5559 branch_write_pc (regs, dsc, val);
cca44b1b 5560}
be8626e0 5561
cca44b1b
JB
5562/* Write to the PC as if from an ALU instruction. */
5563
5564static void
36073a92
YQ
5565alu_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5566 ULONGEST val)
cca44b1b 5567{
36073a92 5568 if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
cca44b1b
JB
5569 bx_write_pc (regs, val);
5570 else
36073a92 5571 branch_write_pc (regs, dsc, val);
cca44b1b
JB
5572}
5573
5574/* Helper for writing to registers for displaced stepping. Writing to the PC
5575 has a varying effects depending on the instruction which does the write:
5576 this is controlled by the WRITE_PC argument. */
5577
5578void
5579displaced_write_reg (struct regcache *regs, struct displaced_step_closure *dsc,
5580 int regno, ULONGEST val, enum pc_write_style write_pc)
5581{
bf9f652a 5582 if (regno == ARM_PC_REGNUM)
08216dd7 5583 {
cca44b1b
JB
5584 if (debug_displaced)
5585 fprintf_unfiltered (gdb_stdlog, "displaced: writing pc %.8lx\n",
5586 (unsigned long) val);
5587 switch (write_pc)
08216dd7 5588 {
cca44b1b 5589 case BRANCH_WRITE_PC:
36073a92 5590 branch_write_pc (regs, dsc, val);
08216dd7
RE
5591 break;
5592
cca44b1b
JB
5593 case BX_WRITE_PC:
5594 bx_write_pc (regs, val);
5595 break;
5596
5597 case LOAD_WRITE_PC:
36073a92 5598 load_write_pc (regs, dsc, val);
cca44b1b
JB
5599 break;
5600
5601 case ALU_WRITE_PC:
36073a92 5602 alu_write_pc (regs, dsc, val);
cca44b1b
JB
5603 break;
5604
5605 case CANNOT_WRITE_PC:
5606 warning (_("Instruction wrote to PC in an unexpected way when "
5607 "single-stepping"));
08216dd7
RE
5608 break;
5609
5610 default:
97b9747c
JB
5611 internal_error (__FILE__, __LINE__,
5612 _("Invalid argument to displaced_write_reg"));
08216dd7 5613 }
b508a996 5614
cca44b1b 5615 dsc->wrote_to_pc = 1;
b508a996 5616 }
ed9a39eb 5617 else
b508a996 5618 {
cca44b1b
JB
5619 if (debug_displaced)
5620 fprintf_unfiltered (gdb_stdlog, "displaced: writing r%d value %.8lx\n",
5621 regno, (unsigned long) val);
5622 regcache_cooked_write_unsigned (regs, regno, val);
b508a996 5623 }
34e8f22d
RE
5624}
5625
cca44b1b
JB
5626/* This function is used to concisely determine if an instruction INSN
5627 references PC. Register fields of interest in INSN should have the
0963b4bd
MS
5628 corresponding fields of BITMASK set to 0b1111. The function
5629 returns return 1 if any of these fields in INSN reference the PC
5630 (also 0b1111, r15), else it returns 0. */
67255d04
RE
5631
5632static int
cca44b1b 5633insn_references_pc (uint32_t insn, uint32_t bitmask)
67255d04 5634{
cca44b1b 5635 uint32_t lowbit = 1;
67255d04 5636
cca44b1b
JB
5637 while (bitmask != 0)
5638 {
5639 uint32_t mask;
44e1a9eb 5640
cca44b1b
JB
5641 for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
5642 ;
67255d04 5643
cca44b1b
JB
5644 if (!lowbit)
5645 break;
67255d04 5646
cca44b1b 5647 mask = lowbit * 0xf;
67255d04 5648
cca44b1b
JB
5649 if ((insn & mask) == mask)
5650 return 1;
5651
5652 bitmask &= ~mask;
67255d04
RE
5653 }
5654
cca44b1b
JB
5655 return 0;
5656}
2af48f68 5657
cca44b1b
JB
5658/* The simplest copy function. Many instructions have the same effect no
5659 matter what address they are executed at: in those cases, use this. */
67255d04 5660
cca44b1b 5661static int
7ff120b4
YQ
5662arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn,
5663 const char *iname, struct displaced_step_closure *dsc)
cca44b1b
JB
5664{
5665 if (debug_displaced)
5666 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx, "
5667 "opcode/class '%s' unmodified\n", (unsigned long) insn,
5668 iname);
67255d04 5669
cca44b1b 5670 dsc->modinsn[0] = insn;
67255d04 5671
cca44b1b
JB
5672 return 0;
5673}
5674
34518530
YQ
5675static int
5676thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
5677 uint16_t insn2, const char *iname,
5678 struct displaced_step_closure *dsc)
5679{
5680 if (debug_displaced)
5681 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x %.4x, "
5682 "opcode/class '%s' unmodified\n", insn1, insn2,
5683 iname);
5684
5685 dsc->modinsn[0] = insn1;
5686 dsc->modinsn[1] = insn2;
5687 dsc->numinsns = 2;
5688
5689 return 0;
5690}
5691
5692/* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
5693 modification. */
5694static int
5695thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, unsigned int insn,
5696 const char *iname,
5697 struct displaced_step_closure *dsc)
5698{
5699 if (debug_displaced)
5700 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x, "
5701 "opcode/class '%s' unmodified\n", insn,
5702 iname);
5703
5704 dsc->modinsn[0] = insn;
5705
5706 return 0;
5707}
5708
cca44b1b
JB
5709/* Preload instructions with immediate offset. */
5710
5711static void
6e39997a 5712cleanup_preload (struct gdbarch *gdbarch,
cca44b1b
JB
5713 struct regcache *regs, struct displaced_step_closure *dsc)
5714{
5715 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5716 if (!dsc->u.preload.immed)
5717 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5718}
5719
7ff120b4
YQ
5720static void
5721install_preload (struct gdbarch *gdbarch, struct regcache *regs,
5722 struct displaced_step_closure *dsc, unsigned int rn)
cca44b1b 5723{
cca44b1b 5724 ULONGEST rn_val;
cca44b1b
JB
5725 /* Preload instructions:
5726
5727 {pli/pld} [rn, #+/-imm]
5728 ->
5729 {pli/pld} [r0, #+/-imm]. */
5730
36073a92
YQ
5731 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5732 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 5733 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
cca44b1b
JB
5734 dsc->u.preload.immed = 1;
5735
cca44b1b 5736 dsc->cleanup = &cleanup_preload;
cca44b1b
JB
5737}
5738
cca44b1b 5739static int
7ff120b4 5740arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
cca44b1b
JB
5741 struct displaced_step_closure *dsc)
5742{
5743 unsigned int rn = bits (insn, 16, 19);
cca44b1b 5744
7ff120b4
YQ
5745 if (!insn_references_pc (insn, 0x000f0000ul))
5746 return arm_copy_unmodified (gdbarch, insn, "preload", dsc);
cca44b1b
JB
5747
5748 if (debug_displaced)
5749 fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
5750 (unsigned long) insn);
5751
7ff120b4
YQ
5752 dsc->modinsn[0] = insn & 0xfff0ffff;
5753
5754 install_preload (gdbarch, regs, dsc, rn);
5755
5756 return 0;
5757}
5758
34518530
YQ
5759static int
5760thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
5761 struct regcache *regs, struct displaced_step_closure *dsc)
5762{
5763 unsigned int rn = bits (insn1, 0, 3);
5764 unsigned int u_bit = bit (insn1, 7);
5765 int imm12 = bits (insn2, 0, 11);
5766 ULONGEST pc_val;
5767
5768 if (rn != ARM_PC_REGNUM)
5769 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);
5770
5771 /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
5772 PLD (literal) Encoding T1. */
5773 if (debug_displaced)
5774 fprintf_unfiltered (gdb_stdlog,
5775 "displaced: copying pld/pli pc (0x%x) %c imm12 %.4x\n",
5776 (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
5777 imm12);
5778
5779 if (!u_bit)
5780 imm12 = -1 * imm12;
5781
5782 /* Rewrite instruction {pli/pld} PC imm12 into:
5783 Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12
5784
5785 {pli/pld} [r0, r1]
5786
5787 Cleanup: r0 <- tmp[0], r1 <- tmp[1]. */
5788
5789 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5790 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5791
5792 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
5793
5794 displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
5795 displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
5796 dsc->u.preload.immed = 0;
5797
5798 /* {pli/pld} [r0, r1] */
5799 dsc->modinsn[0] = insn1 & 0xfff0;
5800 dsc->modinsn[1] = 0xf001;
5801 dsc->numinsns = 2;
5802
5803 dsc->cleanup = &cleanup_preload;
5804 return 0;
5805}
5806
7ff120b4
YQ
5807/* Preload instructions with register offset. */
5808
5809static void
5810install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
5811 struct displaced_step_closure *dsc, unsigned int rn,
5812 unsigned int rm)
5813{
5814 ULONGEST rn_val, rm_val;
5815
cca44b1b
JB
5816 /* Preload register-offset instructions:
5817
5818 {pli/pld} [rn, rm {, shift}]
5819 ->
5820 {pli/pld} [r0, r1 {, shift}]. */
5821
36073a92
YQ
5822 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5823 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5824 rn_val = displaced_read_reg (regs, dsc, rn);
5825 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5826 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5827 displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
cca44b1b
JB
5828 dsc->u.preload.immed = 0;
5829
cca44b1b 5830 dsc->cleanup = &cleanup_preload;
7ff120b4
YQ
5831}
5832
5833static int
5834arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
5835 struct regcache *regs,
5836 struct displaced_step_closure *dsc)
5837{
5838 unsigned int rn = bits (insn, 16, 19);
5839 unsigned int rm = bits (insn, 0, 3);
5840
5841
5842 if (!insn_references_pc (insn, 0x000f000ful))
5843 return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);
5844
5845 if (debug_displaced)
5846 fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
5847 (unsigned long) insn);
5848
5849 dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
cca44b1b 5850
7ff120b4 5851 install_preload_reg (gdbarch, regs, dsc, rn, rm);
cca44b1b
JB
5852 return 0;
5853}
5854
5855/* Copy/cleanup coprocessor load and store instructions. */
5856
5857static void
6e39997a 5858cleanup_copro_load_store (struct gdbarch *gdbarch,
cca44b1b
JB
5859 struct regcache *regs,
5860 struct displaced_step_closure *dsc)
5861{
36073a92 5862 ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5863
5864 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5865
5866 if (dsc->u.ldst.writeback)
5867 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
5868}
5869
7ff120b4
YQ
5870static void
5871install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
5872 struct displaced_step_closure *dsc,
5873 int writeback, unsigned int rn)
cca44b1b 5874{
cca44b1b 5875 ULONGEST rn_val;
cca44b1b 5876
cca44b1b
JB
5877 /* Coprocessor load/store instructions:
5878
5879 {stc/stc2} [<Rn>, #+/-imm] (and other immediate addressing modes)
5880 ->
5881 {stc/stc2} [r0, #+/-imm].
5882
5883 ldc/ldc2 are handled identically. */
5884
36073a92
YQ
5885 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5886 rn_val = displaced_read_reg (regs, dsc, rn);
2b16b2e3
YQ
5887 /* PC should be 4-byte aligned. */
5888 rn_val = rn_val & 0xfffffffc;
cca44b1b
JB
5889 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5890
7ff120b4 5891 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
5892 dsc->u.ldst.rn = rn;
5893
7ff120b4
YQ
5894 dsc->cleanup = &cleanup_copro_load_store;
5895}
5896
5897static int
5898arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
5899 struct regcache *regs,
5900 struct displaced_step_closure *dsc)
5901{
5902 unsigned int rn = bits (insn, 16, 19);
5903
5904 if (!insn_references_pc (insn, 0x000f0000ul))
5905 return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);
5906
5907 if (debug_displaced)
5908 fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
5909 "load/store insn %.8lx\n", (unsigned long) insn);
5910
cca44b1b
JB
5911 dsc->modinsn[0] = insn & 0xfff0ffff;
5912
7ff120b4 5913 install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);
cca44b1b
JB
5914
5915 return 0;
5916}
5917
34518530
YQ
5918static int
5919thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
5920 uint16_t insn2, struct regcache *regs,
5921 struct displaced_step_closure *dsc)
5922{
5923 unsigned int rn = bits (insn1, 0, 3);
5924
5925 if (rn != ARM_PC_REGNUM)
5926 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
5927 "copro load/store", dsc);
5928
5929 if (debug_displaced)
5930 fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
5931 "load/store insn %.4x%.4x\n", insn1, insn2);
5932
5933 dsc->modinsn[0] = insn1 & 0xfff0;
5934 dsc->modinsn[1] = insn2;
5935 dsc->numinsns = 2;
5936
5937 /* This function is called for copying instruction LDC/LDC2/VLDR, which
5938 doesn't support writeback, so pass 0. */
5939 install_copro_load_store (gdbarch, regs, dsc, 0, rn);
5940
5941 return 0;
5942}
5943
cca44b1b
JB
5944/* Clean up branch instructions (actually perform the branch, by setting
5945 PC). */
5946
5947static void
6e39997a 5948cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
5949 struct displaced_step_closure *dsc)
5950{
36073a92 5951 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
5952 int branch_taken = condition_true (dsc->u.branch.cond, status);
5953 enum pc_write_style write_pc = dsc->u.branch.exchange
5954 ? BX_WRITE_PC : BRANCH_WRITE_PC;
5955
5956 if (!branch_taken)
5957 return;
5958
5959 if (dsc->u.branch.link)
5960 {
8c8dba6d
YQ
5961 /* The value of LR should be the next insn of current one. In order
5962 not to confuse logic hanlding later insn `bx lr', if current insn mode
5963 is Thumb, the bit 0 of LR value should be set to 1. */
5964 ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;
5965
5966 if (dsc->is_thumb)
5967 next_insn_addr |= 0x1;
5968
5969 displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
5970 CANNOT_WRITE_PC);
cca44b1b
JB
5971 }
5972
bf9f652a 5973 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
cca44b1b
JB
5974}
5975
5976/* Copy B/BL/BLX instructions with immediate destinations. */
5977
7ff120b4
YQ
5978static void
5979install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
5980 struct displaced_step_closure *dsc,
5981 unsigned int cond, int exchange, int link, long offset)
5982{
5983 /* Implement "BL<cond> <label>" as:
5984
5985 Preparation: cond <- instruction condition
5986 Insn: mov r0, r0 (nop)
5987 Cleanup: if (condition true) { r14 <- pc; pc <- label }.
5988
5989 B<cond> similar, but don't set r14 in cleanup. */
5990
5991 dsc->u.branch.cond = cond;
5992 dsc->u.branch.link = link;
5993 dsc->u.branch.exchange = exchange;
5994
2b16b2e3
YQ
5995 dsc->u.branch.dest = dsc->insn_addr;
5996 if (link && exchange)
5997 /* For BLX, offset is computed from the Align (PC, 4). */
5998 dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;
5999
7ff120b4 6000 if (dsc->is_thumb)
2b16b2e3 6001 dsc->u.branch.dest += 4 + offset;
7ff120b4 6002 else
2b16b2e3 6003 dsc->u.branch.dest += 8 + offset;
7ff120b4
YQ
6004
6005 dsc->cleanup = &cleanup_branch;
6006}
cca44b1b 6007static int
7ff120b4
YQ
6008arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
6009 struct regcache *regs, struct displaced_step_closure *dsc)
cca44b1b
JB
6010{
6011 unsigned int cond = bits (insn, 28, 31);
6012 int exchange = (cond == 0xf);
6013 int link = exchange || bit (insn, 24);
cca44b1b
JB
6014 long offset;
6015
6016 if (debug_displaced)
6017 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s immediate insn "
6018 "%.8lx\n", (exchange) ? "blx" : (link) ? "bl" : "b",
6019 (unsigned long) insn);
cca44b1b
JB
6020 if (exchange)
6021 /* For BLX, set bit 0 of the destination. The cleanup_branch function will
6022 then arrange the switch into Thumb mode. */
6023 offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
6024 else
6025 offset = bits (insn, 0, 23) << 2;
6026
6027 if (bit (offset, 25))
6028 offset = offset | ~0x3ffffff;
6029
cca44b1b
JB
6030 dsc->modinsn[0] = ARM_NOP;
6031
7ff120b4 6032 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
cca44b1b
JB
6033 return 0;
6034}
6035
34518530
YQ
6036static int
6037thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
6038 uint16_t insn2, struct regcache *regs,
6039 struct displaced_step_closure *dsc)
6040{
6041 int link = bit (insn2, 14);
6042 int exchange = link && !bit (insn2, 12);
6043 int cond = INST_AL;
6044 long offset = 0;
6045 int j1 = bit (insn2, 13);
6046 int j2 = bit (insn2, 11);
6047 int s = sbits (insn1, 10, 10);
6048 int i1 = !(j1 ^ bit (insn1, 10));
6049 int i2 = !(j2 ^ bit (insn1, 10));
6050
6051 if (!link && !exchange) /* B */
6052 {
6053 offset = (bits (insn2, 0, 10) << 1);
6054 if (bit (insn2, 12)) /* Encoding T4 */
6055 {
6056 offset |= (bits (insn1, 0, 9) << 12)
6057 | (i2 << 22)
6058 | (i1 << 23)
6059 | (s << 24);
6060 cond = INST_AL;
6061 }
6062 else /* Encoding T3 */
6063 {
6064 offset |= (bits (insn1, 0, 5) << 12)
6065 | (j1 << 18)
6066 | (j2 << 19)
6067 | (s << 20);
6068 cond = bits (insn1, 6, 9);
6069 }
6070 }
6071 else
6072 {
6073 offset = (bits (insn1, 0, 9) << 12);
6074 offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
6075 offset |= exchange ?
6076 (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
6077 }
6078
6079 if (debug_displaced)
6080 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s insn "
6081 "%.4x %.4x with offset %.8lx\n",
6082 link ? (exchange) ? "blx" : "bl" : "b",
6083 insn1, insn2, offset);
6084
6085 dsc->modinsn[0] = THUMB_NOP;
6086
6087 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
6088 return 0;
6089}
6090
6091/* Copy B Thumb instructions. */
6092static int
6093thumb_copy_b (struct gdbarch *gdbarch, unsigned short insn,
6094 struct displaced_step_closure *dsc)
6095{
6096 unsigned int cond = 0;
6097 int offset = 0;
6098 unsigned short bit_12_15 = bits (insn, 12, 15);
6099 CORE_ADDR from = dsc->insn_addr;
6100
6101 if (bit_12_15 == 0xd)
6102 {
6103 /* offset = SignExtend (imm8:0, 32) */
6104 offset = sbits ((insn << 1), 0, 8);
6105 cond = bits (insn, 8, 11);
6106 }
6107 else if (bit_12_15 == 0xe) /* Encoding T2 */
6108 {
6109 offset = sbits ((insn << 1), 0, 11);
6110 cond = INST_AL;
6111 }
6112
6113 if (debug_displaced)
6114 fprintf_unfiltered (gdb_stdlog,
6115 "displaced: copying b immediate insn %.4x "
6116 "with offset %d\n", insn, offset);
6117
6118 dsc->u.branch.cond = cond;
6119 dsc->u.branch.link = 0;
6120 dsc->u.branch.exchange = 0;
6121 dsc->u.branch.dest = from + 4 + offset;
6122
6123 dsc->modinsn[0] = THUMB_NOP;
6124
6125 dsc->cleanup = &cleanup_branch;
6126
6127 return 0;
6128}
6129
cca44b1b
JB
6130/* Copy BX/BLX with register-specified destinations. */
6131
7ff120b4
YQ
6132static void
6133install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
6134 struct displaced_step_closure *dsc, int link,
6135 unsigned int cond, unsigned int rm)
cca44b1b 6136{
cca44b1b
JB
6137 /* Implement {BX,BLX}<cond> <reg>" as:
6138
6139 Preparation: cond <- instruction condition
6140 Insn: mov r0, r0 (nop)
6141 Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
6142
6143 Don't set r14 in cleanup for BX. */
6144
36073a92 6145 dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6146
6147 dsc->u.branch.cond = cond;
6148 dsc->u.branch.link = link;
cca44b1b 6149
7ff120b4 6150 dsc->u.branch.exchange = 1;
cca44b1b
JB
6151
6152 dsc->cleanup = &cleanup_branch;
7ff120b4 6153}
cca44b1b 6154
7ff120b4
YQ
6155static int
6156arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
6157 struct regcache *regs, struct displaced_step_closure *dsc)
6158{
6159 unsigned int cond = bits (insn, 28, 31);
6160 /* BX: x12xxx1x
6161 BLX: x12xxx3x. */
6162 int link = bit (insn, 5);
6163 unsigned int rm = bits (insn, 0, 3);
6164
6165 if (debug_displaced)
6166 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx",
6167 (unsigned long) insn);
6168
6169 dsc->modinsn[0] = ARM_NOP;
6170
6171 install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
cca44b1b
JB
6172 return 0;
6173}
6174
34518530
YQ
6175static int
6176thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
6177 struct regcache *regs,
6178 struct displaced_step_closure *dsc)
6179{
6180 int link = bit (insn, 7);
6181 unsigned int rm = bits (insn, 3, 6);
6182
6183 if (debug_displaced)
6184 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x",
6185 (unsigned short) insn);
6186
6187 dsc->modinsn[0] = THUMB_NOP;
6188
6189 install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);
6190
6191 return 0;
6192}
6193
6194
0963b4bd 6195/* Copy/cleanup arithmetic/logic instruction with immediate RHS. */
cca44b1b
JB
6196
6197static void
6e39997a 6198cleanup_alu_imm (struct gdbarch *gdbarch,
cca44b1b
JB
6199 struct regcache *regs, struct displaced_step_closure *dsc)
6200{
36073a92 6201 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6202 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6203 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6204 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6205}
6206
6207static int
7ff120b4
YQ
6208arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
6209 struct displaced_step_closure *dsc)
cca44b1b
JB
6210{
6211 unsigned int rn = bits (insn, 16, 19);
6212 unsigned int rd = bits (insn, 12, 15);
6213 unsigned int op = bits (insn, 21, 24);
6214 int is_mov = (op == 0xd);
6215 ULONGEST rd_val, rn_val;
cca44b1b
JB
6216
6217 if (!insn_references_pc (insn, 0x000ff000ul))
7ff120b4 6218 return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
cca44b1b
JB
6219
6220 if (debug_displaced)
6221 fprintf_unfiltered (gdb_stdlog, "displaced: copying immediate %s insn "
6222 "%.8lx\n", is_mov ? "move" : "ALU",
6223 (unsigned long) insn);
6224
6225 /* Instruction is of form:
6226
6227 <op><cond> rd, [rn,] #imm
6228
6229 Rewrite as:
6230
6231 Preparation: tmp1, tmp2 <- r0, r1;
6232 r0, r1 <- rd, rn
6233 Insn: <op><cond> r0, r1, #imm
6234 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6235 */
6236
36073a92
YQ
6237 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6238 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6239 rn_val = displaced_read_reg (regs, dsc, rn);
6240 rd_val = displaced_read_reg (regs, dsc, rd);
cca44b1b
JB
6241 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6242 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6243 dsc->rd = rd;
6244
6245 if (is_mov)
6246 dsc->modinsn[0] = insn & 0xfff00fff;
6247 else
6248 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
6249
6250 dsc->cleanup = &cleanup_alu_imm;
6251
6252 return 0;
6253}
6254
34518530
YQ
6255static int
6256thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
6257 uint16_t insn2, struct regcache *regs,
6258 struct displaced_step_closure *dsc)
6259{
6260 unsigned int op = bits (insn1, 5, 8);
6261 unsigned int rn, rm, rd;
6262 ULONGEST rd_val, rn_val;
6263
6264 rn = bits (insn1, 0, 3); /* Rn */
6265 rm = bits (insn2, 0, 3); /* Rm */
6266 rd = bits (insn2, 8, 11); /* Rd */
6267
6268 /* This routine is only called for instruction MOV. */
6269 gdb_assert (op == 0x2 && rn == 0xf);
6270
6271 if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
6272 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);
6273
6274 if (debug_displaced)
6275 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.4x%.4x\n",
6276 "ALU", insn1, insn2);
6277
6278 /* Instruction is of form:
6279
6280 <op><cond> rd, [rn,] #imm
6281
6282 Rewrite as:
6283
6284 Preparation: tmp1, tmp2 <- r0, r1;
6285 r0, r1 <- rd, rn
6286 Insn: <op><cond> r0, r1, #imm
6287 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6288 */
6289
6290 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6291 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6292 rn_val = displaced_read_reg (regs, dsc, rn);
6293 rd_val = displaced_read_reg (regs, dsc, rd);
6294 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6295 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6296 dsc->rd = rd;
6297
6298 dsc->modinsn[0] = insn1;
6299 dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
6300 dsc->numinsns = 2;
6301
6302 dsc->cleanup = &cleanup_alu_imm;
6303
6304 return 0;
6305}
6306
cca44b1b
JB
6307/* Copy/cleanup arithmetic/logic insns with register RHS. */
6308
6309static void
6e39997a 6310cleanup_alu_reg (struct gdbarch *gdbarch,
cca44b1b
JB
6311 struct regcache *regs, struct displaced_step_closure *dsc)
6312{
6313 ULONGEST rd_val;
6314 int i;
6315
36073a92 6316 rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6317
6318 for (i = 0; i < 3; i++)
6319 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6320
6321 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6322}
6323
7ff120b4
YQ
6324static void
6325install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
6326 struct displaced_step_closure *dsc,
6327 unsigned int rd, unsigned int rn, unsigned int rm)
cca44b1b 6328{
cca44b1b 6329 ULONGEST rd_val, rn_val, rm_val;
cca44b1b 6330
cca44b1b
JB
6331 /* Instruction is of form:
6332
6333 <op><cond> rd, [rn,] rm [, <shift>]
6334
6335 Rewrite as:
6336
6337 Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
6338 r0, r1, r2 <- rd, rn, rm
6339 Insn: <op><cond> r0, r1, r2 [, <shift>]
6340 Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
6341 */
6342
36073a92
YQ
6343 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6344 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6345 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6346 rd_val = displaced_read_reg (regs, dsc, rd);
6347 rn_val = displaced_read_reg (regs, dsc, rn);
6348 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6349 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6350 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6351 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6352 dsc->rd = rd;
6353
7ff120b4
YQ
6354 dsc->cleanup = &cleanup_alu_reg;
6355}
6356
6357static int
6358arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
6359 struct displaced_step_closure *dsc)
6360{
6361 unsigned int op = bits (insn, 21, 24);
6362 int is_mov = (op == 0xd);
6363
6364 if (!insn_references_pc (insn, 0x000ff00ful))
6365 return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);
6366
6367 if (debug_displaced)
6368 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.8lx\n",
6369 is_mov ? "move" : "ALU", (unsigned long) insn);
6370
cca44b1b
JB
6371 if (is_mov)
6372 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
6373 else
6374 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
6375
7ff120b4
YQ
6376 install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
6377 bits (insn, 0, 3));
cca44b1b
JB
6378 return 0;
6379}
6380
34518530
YQ
6381static int
6382thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
6383 struct regcache *regs,
6384 struct displaced_step_closure *dsc)
6385{
6386 unsigned rn, rm, rd;
6387
6388 rd = bits (insn, 3, 6);
6389 rn = (bit (insn, 7) << 3) | bits (insn, 0, 2);
6390 rm = 2;
6391
6392 if (rd != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
6393 return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);
6394
6395 if (debug_displaced)
6396 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.4x\n",
6397 "ALU", (unsigned short) insn);
6398
6399 dsc->modinsn[0] = ((insn & 0xff00) | 0x08);
6400
6401 install_alu_reg (gdbarch, regs, dsc, rd, rn, rm);
6402
6403 return 0;
6404}
6405
cca44b1b
JB
6406/* Cleanup/copy arithmetic/logic insns with shifted register RHS. */
6407
6408static void
6e39997a 6409cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
cca44b1b
JB
6410 struct regcache *regs,
6411 struct displaced_step_closure *dsc)
6412{
36073a92 6413 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6414 int i;
6415
6416 for (i = 0; i < 4; i++)
6417 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6418
6419 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6420}
6421
7ff120b4
YQ
6422static void
6423install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
6424 struct displaced_step_closure *dsc,
6425 unsigned int rd, unsigned int rn, unsigned int rm,
6426 unsigned rs)
cca44b1b 6427{
7ff120b4 6428 int i;
cca44b1b 6429 ULONGEST rd_val, rn_val, rm_val, rs_val;
cca44b1b 6430
cca44b1b
JB
6431 /* Instruction is of form:
6432
6433 <op><cond> rd, [rn,] rm, <shift> rs
6434
6435 Rewrite as:
6436
6437 Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
6438 r0, r1, r2, r3 <- rd, rn, rm, rs
6439 Insn: <op><cond> r0, r1, r2, <shift> r3
6440 Cleanup: tmp5 <- r0
6441 r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
6442 rd <- tmp5
6443 */
6444
6445 for (i = 0; i < 4; i++)
36073a92 6446 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b 6447
36073a92
YQ
6448 rd_val = displaced_read_reg (regs, dsc, rd);
6449 rn_val = displaced_read_reg (regs, dsc, rn);
6450 rm_val = displaced_read_reg (regs, dsc, rm);
6451 rs_val = displaced_read_reg (regs, dsc, rs);
cca44b1b
JB
6452 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6453 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6454 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6455 displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
6456 dsc->rd = rd;
7ff120b4
YQ
6457 dsc->cleanup = &cleanup_alu_shifted_reg;
6458}
6459
6460static int
6461arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
6462 struct regcache *regs,
6463 struct displaced_step_closure *dsc)
6464{
6465 unsigned int op = bits (insn, 21, 24);
6466 int is_mov = (op == 0xd);
6467 unsigned int rd, rn, rm, rs;
6468
6469 if (!insn_references_pc (insn, 0x000fff0ful))
6470 return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
6471
6472 if (debug_displaced)
6473 fprintf_unfiltered (gdb_stdlog, "displaced: copying shifted reg %s insn "
6474 "%.8lx\n", is_mov ? "move" : "ALU",
6475 (unsigned long) insn);
6476
6477 rn = bits (insn, 16, 19);
6478 rm = bits (insn, 0, 3);
6479 rs = bits (insn, 8, 11);
6480 rd = bits (insn, 12, 15);
cca44b1b
JB
6481
6482 if (is_mov)
6483 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
6484 else
6485 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
6486
7ff120b4 6487 install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);
cca44b1b
JB
6488
6489 return 0;
6490}
6491
6492/* Clean up load instructions. */
6493
6494static void
6e39997a 6495cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
6496 struct displaced_step_closure *dsc)
6497{
6498 ULONGEST rt_val, rt_val2 = 0, rn_val;
cca44b1b 6499
36073a92 6500 rt_val = displaced_read_reg (regs, dsc, 0);
cca44b1b 6501 if (dsc->u.ldst.xfersize == 8)
36073a92
YQ
6502 rt_val2 = displaced_read_reg (regs, dsc, 1);
6503 rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
6504
6505 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6506 if (dsc->u.ldst.xfersize > 4)
6507 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6508 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6509 if (!dsc->u.ldst.immed)
6510 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6511
6512 /* Handle register writeback. */
6513 if (dsc->u.ldst.writeback)
6514 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6515 /* Put result in right place. */
6516 displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
6517 if (dsc->u.ldst.xfersize == 8)
6518 displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
6519}
6520
6521/* Clean up store instructions. */
6522
6523static void
6e39997a 6524cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
6525 struct displaced_step_closure *dsc)
6526{
36073a92 6527 ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
6528
6529 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6530 if (dsc->u.ldst.xfersize > 4)
6531 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6532 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6533 if (!dsc->u.ldst.immed)
6534 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6535 if (!dsc->u.ldst.restore_r4)
6536 displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
6537
6538 /* Writeback. */
6539 if (dsc->u.ldst.writeback)
6540 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6541}
6542
6543/* Copy "extra" load/store instructions. These are halfword/doubleword
6544 transfers, which have a different encoding to byte/word transfers. */
6545
6546static int
7ff120b4
YQ
6547arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unpriveleged,
6548 struct regcache *regs, struct displaced_step_closure *dsc)
cca44b1b
JB
6549{
6550 unsigned int op1 = bits (insn, 20, 24);
6551 unsigned int op2 = bits (insn, 5, 6);
6552 unsigned int rt = bits (insn, 12, 15);
6553 unsigned int rn = bits (insn, 16, 19);
6554 unsigned int rm = bits (insn, 0, 3);
6555 char load[12] = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
6556 char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
6557 int immed = (op1 & 0x4) != 0;
6558 int opcode;
6559 ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
cca44b1b
JB
6560
6561 if (!insn_references_pc (insn, 0x000ff00ful))
7ff120b4 6562 return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);
cca44b1b
JB
6563
6564 if (debug_displaced)
6565 fprintf_unfiltered (gdb_stdlog, "displaced: copying %sextra load/store "
6566 "insn %.8lx\n", unpriveleged ? "unpriveleged " : "",
6567 (unsigned long) insn);
6568
6569 opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
6570
6571 if (opcode < 0)
6572 internal_error (__FILE__, __LINE__,
6573 _("copy_extra_ld_st: instruction decode error"));
6574
36073a92
YQ
6575 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6576 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6577 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 6578 if (!immed)
36073a92 6579 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 6580
36073a92 6581 rt_val = displaced_read_reg (regs, dsc, rt);
cca44b1b 6582 if (bytesize[opcode] == 8)
36073a92
YQ
6583 rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
6584 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 6585 if (!immed)
36073a92 6586 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6587
6588 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6589 if (bytesize[opcode] == 8)
6590 displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
6591 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6592 if (!immed)
6593 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
6594
6595 dsc->rd = rt;
6596 dsc->u.ldst.xfersize = bytesize[opcode];
6597 dsc->u.ldst.rn = rn;
6598 dsc->u.ldst.immed = immed;
6599 dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
6600 dsc->u.ldst.restore_r4 = 0;
6601
6602 if (immed)
6603 /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
6604 ->
6605 {ldr,str}<width><cond> r0, [r1,] [r2, #imm]. */
6606 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6607 else
6608 /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
6609 ->
6610 {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3]. */
6611 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6612
6613 dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
6614
6615 return 0;
6616}
6617
0f6f04ba 6618/* Copy byte/half word/word loads and stores. */
cca44b1b 6619
7ff120b4 6620static void
0f6f04ba
YQ
6621install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
6622 struct displaced_step_closure *dsc, int load,
6623 int immed, int writeback, int size, int usermode,
6624 int rt, int rm, int rn)
cca44b1b 6625{
cca44b1b 6626 ULONGEST rt_val, rn_val, rm_val = 0;
cca44b1b 6627
36073a92
YQ
6628 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6629 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 6630 if (!immed)
36073a92 6631 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 6632 if (!load)
36073a92 6633 dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);
cca44b1b 6634
36073a92
YQ
6635 rt_val = displaced_read_reg (regs, dsc, rt);
6636 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 6637 if (!immed)
36073a92 6638 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6639
6640 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6641 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6642 if (!immed)
6643 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
cca44b1b 6644 dsc->rd = rt;
0f6f04ba 6645 dsc->u.ldst.xfersize = size;
cca44b1b
JB
6646 dsc->u.ldst.rn = rn;
6647 dsc->u.ldst.immed = immed;
7ff120b4 6648 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
6649
6650 /* To write PC we can do:
6651
494e194e
YQ
6652 Before this sequence of instructions:
6653 r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
6654 r2 is the Rn value got from dispalced_read_reg.
6655
6656 Insn1: push {pc} Write address of STR instruction + offset on stack
6657 Insn2: pop {r4} Read it back from stack, r4 = addr(Insn1) + offset
6658 Insn3: sub r4, r4, pc r4 = addr(Insn1) + offset - pc
6659 = addr(Insn1) + offset - addr(Insn3) - 8
6660 = offset - 16
6661 Insn4: add r4, r4, #8 r4 = offset - 8
6662 Insn5: add r0, r0, r4 r0 = from + 8 + offset - 8
6663 = from + offset
6664 Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])
cca44b1b
JB
6665
6666 Otherwise we don't know what value to write for PC, since the offset is
494e194e
YQ
6667 architecture-dependent (sometimes PC+8, sometimes PC+12). More details
6668 of this can be found in Section "Saving from r15" in
6669 http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */
cca44b1b 6670
7ff120b4
YQ
6671 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6672}
6673
34518530
YQ
6674
6675static int
6676thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
6677 uint16_t insn2, struct regcache *regs,
6678 struct displaced_step_closure *dsc, int size)
6679{
6680 unsigned int u_bit = bit (insn1, 7);
6681 unsigned int rt = bits (insn2, 12, 15);
6682 int imm12 = bits (insn2, 0, 11);
6683 ULONGEST pc_val;
6684
6685 if (debug_displaced)
6686 fprintf_unfiltered (gdb_stdlog,
6687 "displaced: copying ldr pc (0x%x) R%d %c imm12 %.4x\n",
6688 (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
6689 imm12);
6690
6691 if (!u_bit)
6692 imm12 = -1 * imm12;
6693
6694 /* Rewrite instruction LDR Rt imm12 into:
6695
6696 Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12
6697
6698 LDR R0, R2, R3,
6699
6700 Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2]. */
6701
6702
6703 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6704 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6705 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6706
6707 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6708
6709 pc_val = pc_val & 0xfffffffc;
6710
6711 displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
6712 displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);
6713
6714 dsc->rd = rt;
6715
6716 dsc->u.ldst.xfersize = size;
6717 dsc->u.ldst.immed = 0;
6718 dsc->u.ldst.writeback = 0;
6719 dsc->u.ldst.restore_r4 = 0;
6720
6721 /* LDR R0, R2, R3 */
6722 dsc->modinsn[0] = 0xf852;
6723 dsc->modinsn[1] = 0x3;
6724 dsc->numinsns = 2;
6725
6726 dsc->cleanup = &cleanup_load;
6727
6728 return 0;
6729}
6730
6731static int
6732thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
6733 uint16_t insn2, struct regcache *regs,
6734 struct displaced_step_closure *dsc,
6735 int writeback, int immed)
6736{
6737 unsigned int rt = bits (insn2, 12, 15);
6738 unsigned int rn = bits (insn1, 0, 3);
6739 unsigned int rm = bits (insn2, 0, 3); /* Only valid if !immed. */
6740 /* In LDR (register), there is also a register Rm, which is not allowed to
6741 be PC, so we don't have to check it. */
6742
6743 if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
6744 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
6745 dsc);
6746
6747 if (debug_displaced)
6748 fprintf_unfiltered (gdb_stdlog,
6749 "displaced: copying ldr r%d [r%d] insn %.4x%.4x\n",
6750 rt, rn, insn1, insn2);
6751
6752 install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
6753 0, rt, rm, rn);
6754
6755 dsc->u.ldst.restore_r4 = 0;
6756
6757 if (immed)
6758 /* ldr[b]<cond> rt, [rn, #imm], etc.
6759 ->
6760 ldr[b]<cond> r0, [r2, #imm]. */
6761 {
6762 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6763 dsc->modinsn[1] = insn2 & 0x0fff;
6764 }
6765 else
6766 /* ldr[b]<cond> rt, [rn, rm], etc.
6767 ->
6768 ldr[b]<cond> r0, [r2, r3]. */
6769 {
6770 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6771 dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
6772 }
6773
6774 dsc->numinsns = 2;
6775
6776 return 0;
6777}
6778
6779
7ff120b4
YQ
6780static int
6781arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
6782 struct regcache *regs,
6783 struct displaced_step_closure *dsc,
0f6f04ba 6784 int load, int size, int usermode)
7ff120b4
YQ
6785{
6786 int immed = !bit (insn, 25);
6787 int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
6788 unsigned int rt = bits (insn, 12, 15);
6789 unsigned int rn = bits (insn, 16, 19);
6790 unsigned int rm = bits (insn, 0, 3); /* Only valid if !immed. */
6791
6792 if (!insn_references_pc (insn, 0x000ff00ful))
6793 return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);
6794
6795 if (debug_displaced)
6796 fprintf_unfiltered (gdb_stdlog,
6797 "displaced: copying %s%s r%d [r%d] insn %.8lx\n",
0f6f04ba
YQ
6798 load ? (size == 1 ? "ldrb" : "ldr")
6799 : (size == 1 ? "strb" : "str"), usermode ? "t" : "",
7ff120b4
YQ
6800 rt, rn,
6801 (unsigned long) insn);
6802
0f6f04ba
YQ
6803 install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
6804 usermode, rt, rm, rn);
7ff120b4 6805
bf9f652a 6806 if (load || rt != ARM_PC_REGNUM)
cca44b1b
JB
6807 {
6808 dsc->u.ldst.restore_r4 = 0;
6809
6810 if (immed)
6811 /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
6812 ->
6813 {ldr,str}[b]<cond> r0, [r2, #imm]. */
6814 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6815 else
6816 /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
6817 ->
6818 {ldr,str}[b]<cond> r0, [r2, r3]. */
6819 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6820 }
6821 else
6822 {
6823 /* We need to use r4 as scratch. Make sure it's restored afterwards. */
6824 dsc->u.ldst.restore_r4 = 1;
494e194e
YQ
6825 dsc->modinsn[0] = 0xe92d8000; /* push {pc} */
6826 dsc->modinsn[1] = 0xe8bd0010; /* pop {r4} */
cca44b1b
JB
6827 dsc->modinsn[2] = 0xe044400f; /* sub r4, r4, pc. */
6828 dsc->modinsn[3] = 0xe2844008; /* add r4, r4, #8. */
6829 dsc->modinsn[4] = 0xe0800004; /* add r0, r0, r4. */
6830
6831 /* As above. */
6832 if (immed)
6833 dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
6834 else
6835 dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
6836
cca44b1b
JB
6837 dsc->numinsns = 6;
6838 }
6839
6840 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6841
6842 return 0;
6843}
6844
6845/* Cleanup LDM instructions with fully-populated register list. This is an
6846 unfortunate corner case: it's impossible to implement correctly by modifying
6847 the instruction. The issue is as follows: we have an instruction,
6848
6849 ldm rN, {r0-r15}
6850
6851 which we must rewrite to avoid loading PC. A possible solution would be to
6852 do the load in two halves, something like (with suitable cleanup
6853 afterwards):
6854
6855 mov r8, rN
6856 ldm[id][ab] r8!, {r0-r7}
6857 str r7, <temp>
6858 ldm[id][ab] r8, {r7-r14}
6859 <bkpt>
6860
6861 but at present there's no suitable place for <temp>, since the scratch space
6862 is overwritten before the cleanup routine is called. For now, we simply
6863 emulate the instruction. */
6864
6865static void
6866cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
6867 struct displaced_step_closure *dsc)
6868{
cca44b1b
JB
6869 int inc = dsc->u.block.increment;
6870 int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
6871 int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
6872 uint32_t regmask = dsc->u.block.regmask;
6873 int regno = inc ? 0 : 15;
6874 CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
6875 int exception_return = dsc->u.block.load && dsc->u.block.user
6876 && (regmask & 0x8000) != 0;
36073a92 6877 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
6878 int do_transfer = condition_true (dsc->u.block.cond, status);
6879 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6880
6881 if (!do_transfer)
6882 return;
6883
6884 /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
6885 sensible we can do here. Complain loudly. */
6886 if (exception_return)
6887 error (_("Cannot single-step exception return"));
6888
6889 /* We don't handle any stores here for now. */
6890 gdb_assert (dsc->u.block.load != 0);
6891
6892 if (debug_displaced)
6893 fprintf_unfiltered (gdb_stdlog, "displaced: emulating block transfer: "
6894 "%s %s %s\n", dsc->u.block.load ? "ldm" : "stm",
6895 dsc->u.block.increment ? "inc" : "dec",
6896 dsc->u.block.before ? "before" : "after");
6897
6898 while (regmask)
6899 {
6900 uint32_t memword;
6901
6902 if (inc)
bf9f652a 6903 while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
cca44b1b
JB
6904 regno++;
6905 else
6906 while (regno >= 0 && (regmask & (1 << regno)) == 0)
6907 regno--;
6908
6909 xfer_addr += bump_before;
6910
6911 memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
6912 displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
6913
6914 xfer_addr += bump_after;
6915
6916 regmask &= ~(1 << regno);
6917 }
6918
6919 if (dsc->u.block.writeback)
6920 displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
6921 CANNOT_WRITE_PC);
6922}
6923
6924/* Clean up an STM which included the PC in the register list. */
6925
6926static void
6927cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
6928 struct displaced_step_closure *dsc)
6929{
36073a92 6930 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
6931 int store_executed = condition_true (dsc->u.block.cond, status);
6932 CORE_ADDR pc_stored_at, transferred_regs = bitcount (dsc->u.block.regmask);
6933 CORE_ADDR stm_insn_addr;
6934 uint32_t pc_val;
6935 long offset;
6936 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6937
6938 /* If condition code fails, there's nothing else to do. */
6939 if (!store_executed)
6940 return;
6941
6942 if (dsc->u.block.increment)
6943 {
6944 pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
6945
6946 if (dsc->u.block.before)
6947 pc_stored_at += 4;
6948 }
6949 else
6950 {
6951 pc_stored_at = dsc->u.block.xfer_addr;
6952
6953 if (dsc->u.block.before)
6954 pc_stored_at -= 4;
6955 }
6956
6957 pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
6958 stm_insn_addr = dsc->scratch_base;
6959 offset = pc_val - stm_insn_addr;
6960
6961 if (debug_displaced)
6962 fprintf_unfiltered (gdb_stdlog, "displaced: detected PC offset %.8lx for "
6963 "STM instruction\n", offset);
6964
6965 /* Rewrite the stored PC to the proper value for the non-displaced original
6966 instruction. */
6967 write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
6968 dsc->insn_addr + offset);
6969}
6970
6971/* Clean up an LDM which includes the PC in the register list. We clumped all
6972 the registers in the transferred list into a contiguous range r0...rX (to
6973 avoid loading PC directly and losing control of the debugged program), so we
6974 must undo that here. */
6975
6976static void
6e39997a 6977cleanup_block_load_pc (struct gdbarch *gdbarch,
cca44b1b
JB
6978 struct regcache *regs,
6979 struct displaced_step_closure *dsc)
6980{
36073a92 6981 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
22e048c9 6982 int load_executed = condition_true (dsc->u.block.cond, status);
bf9f652a 6983 unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
cca44b1b
JB
6984 unsigned int regs_loaded = bitcount (mask);
6985 unsigned int num_to_shuffle = regs_loaded, clobbered;
6986
6987 /* The method employed here will fail if the register list is fully populated
6988 (we need to avoid loading PC directly). */
6989 gdb_assert (num_to_shuffle < 16);
6990
6991 if (!load_executed)
6992 return;
6993
6994 clobbered = (1 << num_to_shuffle) - 1;
6995
6996 while (num_to_shuffle > 0)
6997 {
6998 if ((mask & (1 << write_reg)) != 0)
6999 {
7000 unsigned int read_reg = num_to_shuffle - 1;
7001
7002 if (read_reg != write_reg)
7003 {
36073a92 7004 ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
cca44b1b
JB
7005 displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
7006 if (debug_displaced)
7007 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: move "
7008 "loaded register r%d to r%d\n"), read_reg,
7009 write_reg);
7010 }
7011 else if (debug_displaced)
7012 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: register "
7013 "r%d already in the right place\n"),
7014 write_reg);
7015
7016 clobbered &= ~(1 << write_reg);
7017
7018 num_to_shuffle--;
7019 }
7020
7021 write_reg--;
7022 }
7023
7024 /* Restore any registers we scribbled over. */
7025 for (write_reg = 0; clobbered != 0; write_reg++)
7026 {
7027 if ((clobbered & (1 << write_reg)) != 0)
7028 {
7029 displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
7030 CANNOT_WRITE_PC);
7031 if (debug_displaced)
7032 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: restored "
7033 "clobbered register r%d\n"), write_reg);
7034 clobbered &= ~(1 << write_reg);
7035 }
7036 }
7037
7038 /* Perform register writeback manually. */
7039 if (dsc->u.block.writeback)
7040 {
7041 ULONGEST new_rn_val = dsc->u.block.xfer_addr;
7042
7043 if (dsc->u.block.increment)
7044 new_rn_val += regs_loaded * 4;
7045 else
7046 new_rn_val -= regs_loaded * 4;
7047
7048 displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
7049 CANNOT_WRITE_PC);
7050 }
7051}
7052
7053/* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
7054 in user-level code (in particular exception return, ldm rn, {...pc}^). */
7055
7056static int
7ff120b4
YQ
7057arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
7058 struct regcache *regs,
7059 struct displaced_step_closure *dsc)
cca44b1b
JB
7060{
7061 int load = bit (insn, 20);
7062 int user = bit (insn, 22);
7063 int increment = bit (insn, 23);
7064 int before = bit (insn, 24);
7065 int writeback = bit (insn, 21);
7066 int rn = bits (insn, 16, 19);
cca44b1b 7067
0963b4bd
MS
7068 /* Block transfers which don't mention PC can be run directly
7069 out-of-line. */
bf9f652a 7070 if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
7ff120b4 7071 return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
cca44b1b 7072
bf9f652a 7073 if (rn == ARM_PC_REGNUM)
cca44b1b 7074 {
0963b4bd
MS
7075 warning (_("displaced: Unpredictable LDM or STM with "
7076 "base register r15"));
7ff120b4 7077 return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
cca44b1b
JB
7078 }
7079
7080 if (debug_displaced)
7081 fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
7082 "%.8lx\n", (unsigned long) insn);
7083
36073a92 7084 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b
JB
7085 dsc->u.block.rn = rn;
7086
7087 dsc->u.block.load = load;
7088 dsc->u.block.user = user;
7089 dsc->u.block.increment = increment;
7090 dsc->u.block.before = before;
7091 dsc->u.block.writeback = writeback;
7092 dsc->u.block.cond = bits (insn, 28, 31);
7093
7094 dsc->u.block.regmask = insn & 0xffff;
7095
7096 if (load)
7097 {
7098 if ((insn & 0xffff) == 0xffff)
7099 {
7100 /* LDM with a fully-populated register list. This case is
7101 particularly tricky. Implement for now by fully emulating the
7102 instruction (which might not behave perfectly in all cases, but
7103 these instructions should be rare enough for that not to matter
7104 too much). */
7105 dsc->modinsn[0] = ARM_NOP;
7106
7107 dsc->cleanup = &cleanup_block_load_all;
7108 }
7109 else
7110 {
7111 /* LDM of a list of registers which includes PC. Implement by
7112 rewriting the list of registers to be transferred into a
7113 contiguous chunk r0...rX before doing the transfer, then shuffling
7114 registers into the correct places in the cleanup routine. */
7115 unsigned int regmask = insn & 0xffff;
7116 unsigned int num_in_list = bitcount (regmask), new_regmask, bit = 1;
7117 unsigned int to = 0, from = 0, i, new_rn;
7118
7119 for (i = 0; i < num_in_list; i++)
36073a92 7120 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b
JB
7121
7122 /* Writeback makes things complicated. We need to avoid clobbering
7123 the base register with one of the registers in our modified
7124 register list, but just using a different register can't work in
7125 all cases, e.g.:
7126
7127 ldm r14!, {r0-r13,pc}
7128
7129 which would need to be rewritten as:
7130
7131 ldm rN!, {r0-r14}
7132
7133 but that can't work, because there's no free register for N.
7134
7135 Solve this by turning off the writeback bit, and emulating
7136 writeback manually in the cleanup routine. */
7137
7138 if (writeback)
7139 insn &= ~(1 << 21);
7140
7141 new_regmask = (1 << num_in_list) - 1;
7142
7143 if (debug_displaced)
7144 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
7145 "{..., pc}: original reg list %.4x, modified "
7146 "list %.4x\n"), rn, writeback ? "!" : "",
7147 (int) insn & 0xffff, new_regmask);
7148
7149 dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
7150
7151 dsc->cleanup = &cleanup_block_load_pc;
7152 }
7153 }
7154 else
7155 {
7156 /* STM of a list of registers which includes PC. Run the instruction
7157 as-is, but out of line: this will store the wrong value for the PC,
7158 so we must manually fix up the memory in the cleanup routine.
7159 Doing things this way has the advantage that we can auto-detect
7160 the offset of the PC write (which is architecture-dependent) in
7161 the cleanup routine. */
7162 dsc->modinsn[0] = insn;
7163
7164 dsc->cleanup = &cleanup_block_store_pc;
7165 }
7166
7167 return 0;
7168}
7169
34518530
YQ
7170static int
7171thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
7172 struct regcache *regs,
7173 struct displaced_step_closure *dsc)
cca44b1b 7174{
34518530
YQ
7175 int rn = bits (insn1, 0, 3);
7176 int load = bit (insn1, 4);
7177 int writeback = bit (insn1, 5);
cca44b1b 7178
34518530
YQ
7179 /* Block transfers which don't mention PC can be run directly
7180 out-of-line. */
7181 if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
7182 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);
7ff120b4 7183
34518530
YQ
7184 if (rn == ARM_PC_REGNUM)
7185 {
7186 warning (_("displaced: Unpredictable LDM or STM with "
7187 "base register r15"));
7188 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7189 "unpredictable ldm/stm", dsc);
7190 }
cca44b1b
JB
7191
7192 if (debug_displaced)
34518530
YQ
7193 fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
7194 "%.4x%.4x\n", insn1, insn2);
cca44b1b 7195
34518530
YQ
7196 /* Clear bit 13, since it should be always zero. */
7197 dsc->u.block.regmask = (insn2 & 0xdfff);
7198 dsc->u.block.rn = rn;
cca44b1b 7199
34518530
YQ
7200 dsc->u.block.load = load;
7201 dsc->u.block.user = 0;
7202 dsc->u.block.increment = bit (insn1, 7);
7203 dsc->u.block.before = bit (insn1, 8);
7204 dsc->u.block.writeback = writeback;
7205 dsc->u.block.cond = INST_AL;
7206 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b 7207
34518530
YQ
7208 if (load)
7209 {
7210 if (dsc->u.block.regmask == 0xffff)
7211 {
7212 /* This branch is impossible to happen. */
7213 gdb_assert (0);
7214 }
7215 else
7216 {
7217 unsigned int regmask = dsc->u.block.regmask;
7218 unsigned int num_in_list = bitcount (regmask), new_regmask, bit = 1;
7219 unsigned int to = 0, from = 0, i, new_rn;
7220
7221 for (i = 0; i < num_in_list; i++)
7222 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7223
7224 if (writeback)
7225 insn1 &= ~(1 << 5);
7226
7227 new_regmask = (1 << num_in_list) - 1;
7228
7229 if (debug_displaced)
7230 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
7231 "{..., pc}: original reg list %.4x, modified "
7232 "list %.4x\n"), rn, writeback ? "!" : "",
7233 (int) dsc->u.block.regmask, new_regmask);
7234
7235 dsc->modinsn[0] = insn1;
7236 dsc->modinsn[1] = (new_regmask & 0xffff);
7237 dsc->numinsns = 2;
7238
7239 dsc->cleanup = &cleanup_block_load_pc;
7240 }
7241 }
7242 else
7243 {
7244 dsc->modinsn[0] = insn1;
7245 dsc->modinsn[1] = insn2;
7246 dsc->numinsns = 2;
7247 dsc->cleanup = &cleanup_block_store_pc;
7248 }
7249 return 0;
7250}
7251
7252/* Cleanup/copy SVC (SWI) instructions. These two functions are overridden
7253 for Linux, where some SVC instructions must be treated specially. */
7254
7255static void
7256cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
7257 struct displaced_step_closure *dsc)
7258{
7259 CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
7260
7261 if (debug_displaced)
7262 fprintf_unfiltered (gdb_stdlog, "displaced: cleanup for svc, resume at "
7263 "%.8lx\n", (unsigned long) resume_addr);
7264
7265 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
7266}
7267
7268
7269/* Common copy routine for svc instruciton. */
7270
7271static int
7272install_svc (struct gdbarch *gdbarch, struct regcache *regs,
7273 struct displaced_step_closure *dsc)
7274{
7275 /* Preparation: none.
7276 Insn: unmodified svc.
7277 Cleanup: pc <- insn_addr + insn_size. */
7278
7279 /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
7280 instruction. */
7281 dsc->wrote_to_pc = 1;
7282
7283 /* Allow OS-specific code to override SVC handling. */
bd18283a
YQ
7284 if (dsc->u.svc.copy_svc_os)
7285 return dsc->u.svc.copy_svc_os (gdbarch, regs, dsc);
7286 else
7287 {
7288 dsc->cleanup = &cleanup_svc;
7289 return 0;
7290 }
34518530
YQ
7291}
7292
7293static int
7294arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
7295 struct regcache *regs, struct displaced_step_closure *dsc)
7296{
7297
7298 if (debug_displaced)
7299 fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.8lx\n",
7300 (unsigned long) insn);
7301
7302 dsc->modinsn[0] = insn;
7303
7304 return install_svc (gdbarch, regs, dsc);
7305}
7306
7307static int
7308thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
7309 struct regcache *regs, struct displaced_step_closure *dsc)
7310{
7311
7312 if (debug_displaced)
7313 fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.4x\n",
7314 insn);
bd18283a 7315
34518530
YQ
7316 dsc->modinsn[0] = insn;
7317
7318 return install_svc (gdbarch, regs, dsc);
cca44b1b
JB
7319}
7320
7321/* Copy undefined instructions. */
7322
7323static int
7ff120b4
YQ
7324arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
7325 struct displaced_step_closure *dsc)
cca44b1b
JB
7326{
7327 if (debug_displaced)
0963b4bd
MS
7328 fprintf_unfiltered (gdb_stdlog,
7329 "displaced: copying undefined insn %.8lx\n",
cca44b1b
JB
7330 (unsigned long) insn);
7331
7332 dsc->modinsn[0] = insn;
7333
7334 return 0;
7335}
7336
34518530
YQ
7337static int
7338thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
7339 struct displaced_step_closure *dsc)
7340{
7341
7342 if (debug_displaced)
7343 fprintf_unfiltered (gdb_stdlog, "displaced: copying undefined insn "
7344 "%.4x %.4x\n", (unsigned short) insn1,
7345 (unsigned short) insn2);
7346
7347 dsc->modinsn[0] = insn1;
7348 dsc->modinsn[1] = insn2;
7349 dsc->numinsns = 2;
7350
7351 return 0;
7352}
7353
cca44b1b
JB
7354/* Copy unpredictable instructions. */
7355
7356static int
7ff120b4
YQ
7357arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
7358 struct displaced_step_closure *dsc)
cca44b1b
JB
7359{
7360 if (debug_displaced)
7361 fprintf_unfiltered (gdb_stdlog, "displaced: copying unpredictable insn "
7362 "%.8lx\n", (unsigned long) insn);
7363
7364 dsc->modinsn[0] = insn;
7365
7366 return 0;
7367}
7368
7369/* The decode_* functions are instruction decoding helpers. They mostly follow
7370 the presentation in the ARM ARM. */
7371
7372static int
7ff120b4
YQ
7373arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
7374 struct regcache *regs,
7375 struct displaced_step_closure *dsc)
cca44b1b
JB
7376{
7377 unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
7378 unsigned int rn = bits (insn, 16, 19);
7379
7380 if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0xe) == 0x0)
7ff120b4 7381 return arm_copy_unmodified (gdbarch, insn, "cps", dsc);
cca44b1b 7382 else if (op1 == 0x10 && op2 == 0x0 && (rn & 0xe) == 0x1)
7ff120b4 7383 return arm_copy_unmodified (gdbarch, insn, "setend", dsc);
cca44b1b 7384 else if ((op1 & 0x60) == 0x20)
7ff120b4 7385 return arm_copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
cca44b1b 7386 else if ((op1 & 0x71) == 0x40)
7ff120b4
YQ
7387 return arm_copy_unmodified (gdbarch, insn, "neon elt/struct load/store",
7388 dsc);
cca44b1b 7389 else if ((op1 & 0x77) == 0x41)
7ff120b4 7390 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 7391 else if ((op1 & 0x77) == 0x45)
7ff120b4 7392 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pli. */
cca44b1b
JB
7393 else if ((op1 & 0x77) == 0x51)
7394 {
7395 if (rn != 0xf)
7ff120b4 7396 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b 7397 else
7ff120b4 7398 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7399 }
7400 else if ((op1 & 0x77) == 0x55)
7ff120b4 7401 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b
JB
7402 else if (op1 == 0x57)
7403 switch (op2)
7404 {
7ff120b4
YQ
7405 case 0x1: return arm_copy_unmodified (gdbarch, insn, "clrex", dsc);
7406 case 0x4: return arm_copy_unmodified (gdbarch, insn, "dsb", dsc);
7407 case 0x5: return arm_copy_unmodified (gdbarch, insn, "dmb", dsc);
7408 case 0x6: return arm_copy_unmodified (gdbarch, insn, "isb", dsc);
7409 default: return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7410 }
7411 else if ((op1 & 0x63) == 0x43)
7ff120b4 7412 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7413 else if ((op2 & 0x1) == 0x0)
7414 switch (op1 & ~0x80)
7415 {
7416 case 0x61:
7ff120b4 7417 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 7418 case 0x65:
7ff120b4 7419 return arm_copy_preload_reg (gdbarch, insn, regs, dsc); /* pli reg. */
cca44b1b
JB
7420 case 0x71: case 0x75:
7421 /* pld/pldw reg. */
7ff120b4 7422 return arm_copy_preload_reg (gdbarch, insn, regs, dsc);
cca44b1b 7423 case 0x63: case 0x67: case 0x73: case 0x77:
7ff120b4 7424 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b 7425 default:
7ff120b4 7426 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7427 }
7428 else
7ff120b4 7429 return arm_copy_undef (gdbarch, insn, dsc); /* Probably unreachable. */
cca44b1b
JB
7430}
7431
7432static int
7ff120b4
YQ
7433arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
7434 struct regcache *regs,
7435 struct displaced_step_closure *dsc)
cca44b1b
JB
7436{
7437 if (bit (insn, 27) == 0)
7ff120b4 7438 return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
cca44b1b
JB
7439 /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx. */
7440 else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
7441 {
7442 case 0x0: case 0x2:
7ff120b4 7443 return arm_copy_unmodified (gdbarch, insn, "srs", dsc);
cca44b1b
JB
7444
7445 case 0x1: case 0x3:
7ff120b4 7446 return arm_copy_unmodified (gdbarch, insn, "rfe", dsc);
cca44b1b
JB
7447
7448 case 0x4: case 0x5: case 0x6: case 0x7:
7ff120b4 7449 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b
JB
7450
7451 case 0x8:
7452 switch ((insn & 0xe00000) >> 21)
7453 {
7454 case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
7455 /* stc/stc2. */
7ff120b4 7456 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7457
7458 case 0x2:
7ff120b4 7459 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b
JB
7460
7461 default:
7ff120b4 7462 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7463 }
7464
7465 case 0x9:
7466 {
7467 int rn_f = (bits (insn, 16, 19) == 0xf);
7468 switch ((insn & 0xe00000) >> 21)
7469 {
7470 case 0x1: case 0x3:
7471 /* ldc/ldc2 imm (undefined for rn == pc). */
7ff120b4
YQ
7472 return rn_f ? arm_copy_undef (gdbarch, insn, dsc)
7473 : arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7474
7475 case 0x2:
7ff120b4 7476 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
7477
7478 case 0x4: case 0x5: case 0x6: case 0x7:
7479 /* ldc/ldc2 lit (undefined for rn != pc). */
7ff120b4
YQ
7480 return rn_f ? arm_copy_copro_load_store (gdbarch, insn, regs, dsc)
7481 : arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7482
7483 default:
7ff120b4 7484 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7485 }
7486 }
7487
7488 case 0xa:
7ff120b4 7489 return arm_copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
cca44b1b
JB
7490
7491 case 0xb:
7492 if (bits (insn, 16, 19) == 0xf)
7493 /* ldc/ldc2 lit. */
7ff120b4 7494 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 7495 else
7ff120b4 7496 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7497
7498 case 0xc:
7499 if (bit (insn, 4))
7ff120b4 7500 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 7501 else
7ff120b4 7502 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7503
7504 case 0xd:
7505 if (bit (insn, 4))
7ff120b4 7506 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 7507 else
7ff120b4 7508 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7509
7510 default:
7ff120b4 7511 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7512 }
7513}
7514
7515/* Decode miscellaneous instructions in dp/misc encoding space. */
7516
7517static int
7ff120b4
YQ
7518arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
7519 struct regcache *regs,
7520 struct displaced_step_closure *dsc)
cca44b1b
JB
7521{
7522 unsigned int op2 = bits (insn, 4, 6);
7523 unsigned int op = bits (insn, 21, 22);
7524 unsigned int op1 = bits (insn, 16, 19);
7525
7526 switch (op2)
7527 {
7528 case 0x0:
7ff120b4 7529 return arm_copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
cca44b1b
JB
7530
7531 case 0x1:
7532 if (op == 0x1) /* bx. */
7ff120b4 7533 return arm_copy_bx_blx_reg (gdbarch, insn, regs, dsc);
cca44b1b 7534 else if (op == 0x3)
7ff120b4 7535 return arm_copy_unmodified (gdbarch, insn, "clz", dsc);
cca44b1b 7536 else
7ff120b4 7537 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7538
7539 case 0x2:
7540 if (op == 0x1)
7541 /* Not really supported. */
7ff120b4 7542 return arm_copy_unmodified (gdbarch, insn, "bxj", dsc);
cca44b1b 7543 else
7ff120b4 7544 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7545
7546 case 0x3:
7547 if (op == 0x1)
7ff120b4 7548 return arm_copy_bx_blx_reg (gdbarch, insn,
0963b4bd 7549 regs, dsc); /* blx register. */
cca44b1b 7550 else
7ff120b4 7551 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7552
7553 case 0x5:
7ff120b4 7554 return arm_copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
cca44b1b
JB
7555
7556 case 0x7:
7557 if (op == 0x1)
7ff120b4 7558 return arm_copy_unmodified (gdbarch, insn, "bkpt", dsc);
cca44b1b
JB
7559 else if (op == 0x3)
7560 /* Not really supported. */
7ff120b4 7561 return arm_copy_unmodified (gdbarch, insn, "smc", dsc);
cca44b1b
JB
7562
7563 default:
7ff120b4 7564 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7565 }
7566}
7567
7568static int
7ff120b4
YQ
7569arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
7570 struct regcache *regs,
7571 struct displaced_step_closure *dsc)
cca44b1b
JB
7572{
7573 if (bit (insn, 25))
7574 switch (bits (insn, 20, 24))
7575 {
7576 case 0x10:
7ff120b4 7577 return arm_copy_unmodified (gdbarch, insn, "movw", dsc);
cca44b1b
JB
7578
7579 case 0x14:
7ff120b4 7580 return arm_copy_unmodified (gdbarch, insn, "movt", dsc);
cca44b1b
JB
7581
7582 case 0x12: case 0x16:
7ff120b4 7583 return arm_copy_unmodified (gdbarch, insn, "msr imm", dsc);
cca44b1b
JB
7584
7585 default:
7ff120b4 7586 return arm_copy_alu_imm (gdbarch, insn, regs, dsc);
cca44b1b
JB
7587 }
7588 else
7589 {
7590 uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
7591
7592 if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
7ff120b4 7593 return arm_copy_alu_reg (gdbarch, insn, regs, dsc);
cca44b1b 7594 else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
7ff120b4 7595 return arm_copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
cca44b1b 7596 else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
7ff120b4 7597 return arm_decode_miscellaneous (gdbarch, insn, regs, dsc);
cca44b1b 7598 else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
7ff120b4 7599 return arm_copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
cca44b1b 7600 else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
7ff120b4 7601 return arm_copy_unmodified (gdbarch, insn, "mul/mla", dsc);
cca44b1b 7602 else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
7ff120b4 7603 return arm_copy_unmodified (gdbarch, insn, "synch", dsc);
cca44b1b
JB
7604 else if (op2 == 0xb || (op2 & 0xd) == 0xd)
7605 /* 2nd arg means "unpriveleged". */
7ff120b4
YQ
7606 return arm_copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
7607 dsc);
cca44b1b
JB
7608 }
7609
7610 /* Should be unreachable. */
7611 return 1;
7612}
7613
7614static int
7ff120b4
YQ
7615arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
7616 struct regcache *regs,
7617 struct displaced_step_closure *dsc)
cca44b1b
JB
7618{
7619 int a = bit (insn, 25), b = bit (insn, 4);
7620 uint32_t op1 = bits (insn, 20, 24);
7621 int rn_f = bits (insn, 16, 19) == 0xf;
7622
7623 if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
7624 || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
0f6f04ba 7625 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 0);
cca44b1b
JB
7626 else if ((!a && (op1 & 0x17) == 0x02)
7627 || (a && (op1 & 0x17) == 0x02 && !b))
0f6f04ba 7628 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 1);
cca44b1b
JB
7629 else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
7630 || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
0f6f04ba 7631 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 0);
cca44b1b
JB
7632 else if ((!a && (op1 & 0x17) == 0x03)
7633 || (a && (op1 & 0x17) == 0x03 && !b))
0f6f04ba 7634 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 1);
cca44b1b
JB
7635 else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
7636 || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
7ff120b4 7637 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
cca44b1b
JB
7638 else if ((!a && (op1 & 0x17) == 0x06)
7639 || (a && (op1 & 0x17) == 0x06 && !b))
7ff120b4 7640 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
cca44b1b
JB
7641 else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
7642 || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
7ff120b4 7643 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
cca44b1b
JB
7644 else if ((!a && (op1 & 0x17) == 0x07)
7645 || (a && (op1 & 0x17) == 0x07 && !b))
7ff120b4 7646 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
cca44b1b
JB
7647
7648 /* Should be unreachable. */
7649 return 1;
7650}
7651
7652static int
7ff120b4
YQ
7653arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
7654 struct displaced_step_closure *dsc)
cca44b1b
JB
7655{
7656 switch (bits (insn, 20, 24))
7657 {
7658 case 0x00: case 0x01: case 0x02: case 0x03:
7ff120b4 7659 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
cca44b1b
JB
7660
7661 case 0x04: case 0x05: case 0x06: case 0x07:
7ff120b4 7662 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
cca44b1b
JB
7663
7664 case 0x08: case 0x09: case 0x0a: case 0x0b:
7665 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
7ff120b4 7666 return arm_copy_unmodified (gdbarch, insn,
cca44b1b
JB
7667 "decode/pack/unpack/saturate/reverse", dsc);
7668
7669 case 0x18:
7670 if (bits (insn, 5, 7) == 0) /* op2. */
7671 {
7672 if (bits (insn, 12, 15) == 0xf)
7ff120b4 7673 return arm_copy_unmodified (gdbarch, insn, "usad8", dsc);
cca44b1b 7674 else
7ff120b4 7675 return arm_copy_unmodified (gdbarch, insn, "usada8", dsc);
cca44b1b
JB
7676 }
7677 else
7ff120b4 7678 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7679
7680 case 0x1a: case 0x1b:
7681 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 7682 return arm_copy_unmodified (gdbarch, insn, "sbfx", dsc);
cca44b1b 7683 else
7ff120b4 7684 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7685
7686 case 0x1c: case 0x1d:
7687 if (bits (insn, 5, 6) == 0x0) /* op2[1:0]. */
7688 {
7689 if (bits (insn, 0, 3) == 0xf)
7ff120b4 7690 return arm_copy_unmodified (gdbarch, insn, "bfc", dsc);
cca44b1b 7691 else
7ff120b4 7692 return arm_copy_unmodified (gdbarch, insn, "bfi", dsc);
cca44b1b
JB
7693 }
7694 else
7ff120b4 7695 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7696
7697 case 0x1e: case 0x1f:
7698 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 7699 return arm_copy_unmodified (gdbarch, insn, "ubfx", dsc);
cca44b1b 7700 else
7ff120b4 7701 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7702 }
7703
7704 /* Should be unreachable. */
7705 return 1;
7706}
7707
7708static int
7ff120b4
YQ
7709arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, int32_t insn,
7710 struct regcache *regs,
7711 struct displaced_step_closure *dsc)
cca44b1b
JB
7712{
7713 if (bit (insn, 25))
7ff120b4 7714 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b 7715 else
7ff120b4 7716 return arm_copy_block_xfer (gdbarch, insn, regs, dsc);
cca44b1b
JB
7717}
7718
7719static int
7ff120b4
YQ
7720arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
7721 struct regcache *regs,
7722 struct displaced_step_closure *dsc)
cca44b1b
JB
7723{
7724 unsigned int opcode = bits (insn, 20, 24);
7725
7726 switch (opcode)
7727 {
7728 case 0x04: case 0x05: /* VFP/Neon mrrc/mcrr. */
7ff120b4 7729 return arm_copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
cca44b1b
JB
7730
7731 case 0x08: case 0x0a: case 0x0c: case 0x0e:
7732 case 0x12: case 0x16:
7ff120b4 7733 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
cca44b1b
JB
7734
7735 case 0x09: case 0x0b: case 0x0d: case 0x0f:
7736 case 0x13: case 0x17:
7ff120b4 7737 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
cca44b1b
JB
7738
7739 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7740 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7741 /* Note: no writeback for these instructions. Bit 25 will always be
7742 zero though (via caller), so the following works OK. */
7ff120b4 7743 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7744 }
7745
7746 /* Should be unreachable. */
7747 return 1;
7748}
7749
34518530
YQ
7750/* Decode shifted register instructions. */
7751
7752static int
7753thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
7754 uint16_t insn2, struct regcache *regs,
7755 struct displaced_step_closure *dsc)
7756{
7757 /* PC is only allowed to be used in instruction MOV. */
7758
7759 unsigned int op = bits (insn1, 5, 8);
7760 unsigned int rn = bits (insn1, 0, 3);
7761
7762 if (op == 0x2 && rn == 0xf) /* MOV */
7763 return thumb2_copy_alu_imm (gdbarch, insn1, insn2, regs, dsc);
7764 else
7765 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7766 "dp (shift reg)", dsc);
7767}
7768
7769
7770/* Decode extension register load/store. Exactly the same as
7771 arm_decode_ext_reg_ld_st. */
7772
7773static int
7774thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
7775 uint16_t insn2, struct regcache *regs,
7776 struct displaced_step_closure *dsc)
7777{
7778 unsigned int opcode = bits (insn1, 4, 8);
7779
7780 switch (opcode)
7781 {
7782 case 0x04: case 0x05:
7783 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7784 "vfp/neon vmov", dsc);
7785
7786 case 0x08: case 0x0c: /* 01x00 */
7787 case 0x0a: case 0x0e: /* 01x10 */
7788 case 0x12: case 0x16: /* 10x10 */
7789 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7790 "vfp/neon vstm/vpush", dsc);
7791
7792 case 0x09: case 0x0d: /* 01x01 */
7793 case 0x0b: case 0x0f: /* 01x11 */
7794 case 0x13: case 0x17: /* 10x11 */
7795 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7796 "vfp/neon vldm/vpop", dsc);
7797
7798 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7799 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7800 "vstr", dsc);
7801 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7802 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2, regs, dsc);
7803 }
7804
7805 /* Should be unreachable. */
7806 return 1;
7807}
7808
cca44b1b 7809static int
7ff120b4
YQ
7810arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn, CORE_ADDR to,
7811 struct regcache *regs, struct displaced_step_closure *dsc)
cca44b1b
JB
7812{
7813 unsigned int op1 = bits (insn, 20, 25);
7814 int op = bit (insn, 4);
7815 unsigned int coproc = bits (insn, 8, 11);
7816 unsigned int rn = bits (insn, 16, 19);
7817
7818 if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
7ff120b4 7819 return arm_decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
cca44b1b
JB
7820 else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
7821 && (coproc & 0xe) != 0xa)
7822 /* stc/stc2. */
7ff120b4 7823 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7824 else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
7825 && (coproc & 0xe) != 0xa)
7826 /* ldc/ldc2 imm/lit. */
7ff120b4 7827 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 7828 else if ((op1 & 0x3e) == 0x00)
7ff120b4 7829 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b 7830 else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
7ff120b4 7831 return arm_copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
cca44b1b 7832 else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
7ff120b4 7833 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b 7834 else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
7ff120b4 7835 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
7836 else if ((op1 & 0x30) == 0x20 && !op)
7837 {
7838 if ((coproc & 0xe) == 0xa)
7ff120b4 7839 return arm_copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
cca44b1b 7840 else
7ff120b4 7841 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7842 }
7843 else if ((op1 & 0x30) == 0x20 && op)
7ff120b4 7844 return arm_copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
cca44b1b 7845 else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
7ff120b4 7846 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 7847 else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
7ff120b4 7848 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 7849 else if ((op1 & 0x30) == 0x30)
7ff120b4 7850 return arm_copy_svc (gdbarch, insn, regs, dsc);
cca44b1b 7851 else
7ff120b4 7852 return arm_copy_undef (gdbarch, insn, dsc); /* Possibly unreachable. */
cca44b1b
JB
7853}
7854
34518530
YQ
7855static int
7856thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
7857 uint16_t insn2, struct regcache *regs,
7858 struct displaced_step_closure *dsc)
7859{
7860 unsigned int coproc = bits (insn2, 8, 11);
7861 unsigned int op1 = bits (insn1, 4, 9);
7862 unsigned int bit_5_8 = bits (insn1, 5, 8);
7863 unsigned int bit_9 = bit (insn1, 9);
7864 unsigned int bit_4 = bit (insn1, 4);
7865 unsigned int rn = bits (insn1, 0, 3);
7866
7867 if (bit_9 == 0)
7868 {
7869 if (bit_5_8 == 2)
7870 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7871 "neon 64bit xfer/mrrc/mrrc2/mcrr/mcrr2",
7872 dsc);
7873 else if (bit_5_8 == 0) /* UNDEFINED. */
7874 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
7875 else
7876 {
7877 /*coproc is 101x. SIMD/VFP, ext registers load/store. */
7878 if ((coproc & 0xe) == 0xa)
7879 return thumb2_decode_ext_reg_ld_st (gdbarch, insn1, insn2, regs,
7880 dsc);
7881 else /* coproc is not 101x. */
7882 {
7883 if (bit_4 == 0) /* STC/STC2. */
7884 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7885 "stc/stc2", dsc);
7886 else /* LDC/LDC2 {literal, immeidate}. */
7887 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2,
7888 regs, dsc);
7889 }
7890 }
7891 }
7892 else
7893 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "coproc", dsc);
7894
7895 return 0;
7896}
7897
7898static void
7899install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
7900 struct displaced_step_closure *dsc, int rd)
7901{
7902 /* ADR Rd, #imm
7903
7904 Rewrite as:
7905
7906 Preparation: Rd <- PC
7907 Insn: ADD Rd, #imm
7908 Cleanup: Null.
7909 */
7910
7911 /* Rd <- PC */
7912 int val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
7913 displaced_write_reg (regs, dsc, rd, val, CANNOT_WRITE_PC);
7914}
7915
7916static int
7917thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
7918 struct displaced_step_closure *dsc,
7919 int rd, unsigned int imm)
7920{
7921
7922 /* Encoding T2: ADDS Rd, #imm */
7923 dsc->modinsn[0] = (0x3000 | (rd << 8) | imm);
7924
7925 install_pc_relative (gdbarch, regs, dsc, rd);
7926
7927 return 0;
7928}
7929
7930static int
7931thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
7932 struct regcache *regs,
7933 struct displaced_step_closure *dsc)
7934{
7935 unsigned int rd = bits (insn, 8, 10);
7936 unsigned int imm8 = bits (insn, 0, 7);
7937
7938 if (debug_displaced)
7939 fprintf_unfiltered (gdb_stdlog,
7940 "displaced: copying thumb adr r%d, #%d insn %.4x\n",
7941 rd, imm8, insn);
7942
7943 return thumb_copy_pc_relative_16bit (gdbarch, regs, dsc, rd, imm8);
7944}
7945
7946static int
7947thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
7948 uint16_t insn2, struct regcache *regs,
7949 struct displaced_step_closure *dsc)
7950{
7951 unsigned int rd = bits (insn2, 8, 11);
7952 /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
7953 extract raw immediate encoding rather than computing immediate. When
7954 generating ADD or SUB instruction, we can simply perform OR operation to
7955 set immediate into ADD. */
7956 unsigned int imm_3_8 = insn2 & 0x70ff;
7957 unsigned int imm_i = insn1 & 0x0400; /* Clear all bits except bit 10. */
7958
7959 if (debug_displaced)
7960 fprintf_unfiltered (gdb_stdlog,
7961 "displaced: copying thumb adr r%d, #%d:%d insn %.4x%.4x\n",
7962 rd, imm_i, imm_3_8, insn1, insn2);
7963
7964 if (bit (insn1, 7)) /* Encoding T2 */
7965 {
7966 /* Encoding T3: SUB Rd, Rd, #imm */
7967 dsc->modinsn[0] = (0xf1a0 | rd | imm_i);
7968 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
7969 }
7970 else /* Encoding T3 */
7971 {
7972 /* Encoding T3: ADD Rd, Rd, #imm */
7973 dsc->modinsn[0] = (0xf100 | rd | imm_i);
7974 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
7975 }
7976 dsc->numinsns = 2;
7977
7978 install_pc_relative (gdbarch, regs, dsc, rd);
7979
7980 return 0;
7981}
7982
7983static int
7984thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, unsigned short insn1,
7985 struct regcache *regs,
7986 struct displaced_step_closure *dsc)
7987{
7988 unsigned int rt = bits (insn1, 8, 10);
7989 unsigned int pc;
7990 int imm8 = (bits (insn1, 0, 7) << 2);
7991 CORE_ADDR from = dsc->insn_addr;
7992
7993 /* LDR Rd, #imm8
7994
7995 Rwrite as:
7996
7997 Preparation: tmp0 <- R0, tmp2 <- R2, tmp3 <- R3, R2 <- PC, R3 <- #imm8;
7998
7999 Insn: LDR R0, [R2, R3];
8000 Cleanup: R2 <- tmp2, R3 <- tmp3, Rd <- R0, R0 <- tmp0 */
8001
8002 if (debug_displaced)
8003 fprintf_unfiltered (gdb_stdlog,
8004 "displaced: copying thumb ldr r%d [pc #%d]\n"
8005 , rt, imm8);
8006
8007 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
8008 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
8009 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
8010 pc = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
8011 /* The assembler calculates the required value of the offset from the
8012 Align(PC,4) value of this instruction to the label. */
8013 pc = pc & 0xfffffffc;
8014
8015 displaced_write_reg (regs, dsc, 2, pc, CANNOT_WRITE_PC);
8016 displaced_write_reg (regs, dsc, 3, imm8, CANNOT_WRITE_PC);
8017
8018 dsc->rd = rt;
8019 dsc->u.ldst.xfersize = 4;
8020 dsc->u.ldst.rn = 0;
8021 dsc->u.ldst.immed = 0;
8022 dsc->u.ldst.writeback = 0;
8023 dsc->u.ldst.restore_r4 = 0;
8024
8025 dsc->modinsn[0] = 0x58d0; /* ldr r0, [r2, r3]*/
8026
8027 dsc->cleanup = &cleanup_load;
8028
8029 return 0;
8030}
8031
8032/* Copy Thumb cbnz/cbz insruction. */
8033
8034static int
8035thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
8036 struct regcache *regs,
8037 struct displaced_step_closure *dsc)
8038{
8039 int non_zero = bit (insn1, 11);
8040 unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
8041 CORE_ADDR from = dsc->insn_addr;
8042 int rn = bits (insn1, 0, 2);
8043 int rn_val = displaced_read_reg (regs, dsc, rn);
8044
8045 dsc->u.branch.cond = (rn_val && non_zero) || (!rn_val && !non_zero);
8046 /* CBNZ and CBZ do not affect the condition flags. If condition is true,
8047 set it INST_AL, so cleanup_branch will know branch is taken, otherwise,
8048 condition is false, let it be, cleanup_branch will do nothing. */
8049 if (dsc->u.branch.cond)
8050 {
8051 dsc->u.branch.cond = INST_AL;
8052 dsc->u.branch.dest = from + 4 + imm5;
8053 }
8054 else
8055 dsc->u.branch.dest = from + 2;
8056
8057 dsc->u.branch.link = 0;
8058 dsc->u.branch.exchange = 0;
8059
8060 if (debug_displaced)
8061 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s [r%d = 0x%x]"
8062 " insn %.4x to %.8lx\n", non_zero ? "cbnz" : "cbz",
8063 rn, rn_val, insn1, dsc->u.branch.dest);
8064
8065 dsc->modinsn[0] = THUMB_NOP;
8066
8067 dsc->cleanup = &cleanup_branch;
8068 return 0;
8069}
8070
8071/* Copy Table Branch Byte/Halfword */
8072static int
8073thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
8074 uint16_t insn2, struct regcache *regs,
8075 struct displaced_step_closure *dsc)
8076{
8077 ULONGEST rn_val, rm_val;
8078 int is_tbh = bit (insn2, 4);
8079 CORE_ADDR halfwords = 0;
8080 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8081
8082 rn_val = displaced_read_reg (regs, dsc, bits (insn1, 0, 3));
8083 rm_val = displaced_read_reg (regs, dsc, bits (insn2, 0, 3));
8084
8085 if (is_tbh)
8086 {
8087 gdb_byte buf[2];
8088
8089 target_read_memory (rn_val + 2 * rm_val, buf, 2);
8090 halfwords = extract_unsigned_integer (buf, 2, byte_order);
8091 }
8092 else
8093 {
8094 gdb_byte buf[1];
8095
8096 target_read_memory (rn_val + rm_val, buf, 1);
8097 halfwords = extract_unsigned_integer (buf, 1, byte_order);
8098 }
8099
8100 if (debug_displaced)
8101 fprintf_unfiltered (gdb_stdlog, "displaced: %s base 0x%x offset 0x%x"
8102 " offset 0x%x\n", is_tbh ? "tbh" : "tbb",
8103 (unsigned int) rn_val, (unsigned int) rm_val,
8104 (unsigned int) halfwords);
8105
8106 dsc->u.branch.cond = INST_AL;
8107 dsc->u.branch.link = 0;
8108 dsc->u.branch.exchange = 0;
8109 dsc->u.branch.dest = dsc->insn_addr + 4 + 2 * halfwords;
8110
8111 dsc->cleanup = &cleanup_branch;
8112
8113 return 0;
8114}
8115
8116static void
8117cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
8118 struct displaced_step_closure *dsc)
8119{
8120 /* PC <- r7 */
8121 int val = displaced_read_reg (regs, dsc, 7);
8122 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, val, BX_WRITE_PC);
8123
8124 /* r7 <- r8 */
8125 val = displaced_read_reg (regs, dsc, 8);
8126 displaced_write_reg (regs, dsc, 7, val, CANNOT_WRITE_PC);
8127
8128 /* r8 <- tmp[0] */
8129 displaced_write_reg (regs, dsc, 8, dsc->tmp[0], CANNOT_WRITE_PC);
8130
8131}
8132
8133static int
8134thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, unsigned short insn1,
8135 struct regcache *regs,
8136 struct displaced_step_closure *dsc)
8137{
8138 dsc->u.block.regmask = insn1 & 0x00ff;
8139
8140 /* Rewrite instruction: POP {rX, rY, ...,rZ, PC}
8141 to :
8142
8143 (1) register list is full, that is, r0-r7 are used.
8144 Prepare: tmp[0] <- r8
8145
8146 POP {r0, r1, ...., r6, r7}; remove PC from reglist
8147 MOV r8, r7; Move value of r7 to r8;
8148 POP {r7}; Store PC value into r7.
8149
8150 Cleanup: PC <- r7, r7 <- r8, r8 <-tmp[0]
8151
8152 (2) register list is not full, supposing there are N registers in
8153 register list (except PC, 0 <= N <= 7).
8154 Prepare: for each i, 0 - N, tmp[i] <- ri.
8155
8156 POP {r0, r1, ...., rN};
8157
8158 Cleanup: Set registers in original reglist from r0 - rN. Restore r0 - rN
8159 from tmp[] properly.
8160 */
8161 if (debug_displaced)
8162 fprintf_unfiltered (gdb_stdlog,
8163 "displaced: copying thumb pop {%.8x, pc} insn %.4x\n",
8164 dsc->u.block.regmask, insn1);
8165
8166 if (dsc->u.block.regmask == 0xff)
8167 {
8168 dsc->tmp[0] = displaced_read_reg (regs, dsc, 8);
8169
8170 dsc->modinsn[0] = (insn1 & 0xfeff); /* POP {r0,r1,...,r6, r7} */
8171 dsc->modinsn[1] = 0x46b8; /* MOV r8, r7 */
8172 dsc->modinsn[2] = 0xbc80; /* POP {r7} */
8173
8174 dsc->numinsns = 3;
8175 dsc->cleanup = &cleanup_pop_pc_16bit_all;
8176 }
8177 else
8178 {
8179 unsigned int num_in_list = bitcount (dsc->u.block.regmask);
8180 unsigned int new_regmask, bit = 1;
8181 unsigned int to = 0, from = 0, i, new_rn;
8182
8183 for (i = 0; i < num_in_list + 1; i++)
8184 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
8185
8186 new_regmask = (1 << (num_in_list + 1)) - 1;
8187
8188 if (debug_displaced)
8189 fprintf_unfiltered (gdb_stdlog, _("displaced: POP "
8190 "{..., pc}: original reg list %.4x,"
8191 " modified list %.4x\n"),
8192 (int) dsc->u.block.regmask, new_regmask);
8193
8194 dsc->u.block.regmask |= 0x8000;
8195 dsc->u.block.writeback = 0;
8196 dsc->u.block.cond = INST_AL;
8197
8198 dsc->modinsn[0] = (insn1 & ~0x1ff) | (new_regmask & 0xff);
8199
8200 dsc->cleanup = &cleanup_block_load_pc;
8201 }
8202
8203 return 0;
8204}
8205
8206static void
8207thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8208 struct regcache *regs,
8209 struct displaced_step_closure *dsc)
8210{
8211 unsigned short op_bit_12_15 = bits (insn1, 12, 15);
8212 unsigned short op_bit_10_11 = bits (insn1, 10, 11);
8213 int err = 0;
8214
8215 /* 16-bit thumb instructions. */
8216 switch (op_bit_12_15)
8217 {
8218 /* Shift (imme), add, subtract, move and compare. */
8219 case 0: case 1: case 2: case 3:
8220 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8221 "shift/add/sub/mov/cmp",
8222 dsc);
8223 break;
8224 case 4:
8225 switch (op_bit_10_11)
8226 {
8227 case 0: /* Data-processing */
8228 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8229 "data-processing",
8230 dsc);
8231 break;
8232 case 1: /* Special data instructions and branch and exchange. */
8233 {
8234 unsigned short op = bits (insn1, 7, 9);
8235 if (op == 6 || op == 7) /* BX or BLX */
8236 err = thumb_copy_bx_blx_reg (gdbarch, insn1, regs, dsc);
8237 else if (bits (insn1, 6, 7) != 0) /* ADD/MOV/CMP high registers. */
8238 err = thumb_copy_alu_reg (gdbarch, insn1, regs, dsc);
8239 else
8240 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "special data",
8241 dsc);
8242 }
8243 break;
8244 default: /* LDR (literal) */
8245 err = thumb_copy_16bit_ldr_literal (gdbarch, insn1, regs, dsc);
8246 }
8247 break;
8248 case 5: case 6: case 7: case 8: case 9: /* Load/Store single data item */
8249 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldr/str", dsc);
8250 break;
8251 case 10:
8252 if (op_bit_10_11 < 2) /* Generate PC-relative address */
8253 err = thumb_decode_pc_relative_16bit (gdbarch, insn1, regs, dsc);
8254 else /* Generate SP-relative address */
8255 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "sp-relative", dsc);
8256 break;
8257 case 11: /* Misc 16-bit instructions */
8258 {
8259 switch (bits (insn1, 8, 11))
8260 {
8261 case 1: case 3: case 9: case 11: /* CBNZ, CBZ */
8262 err = thumb_copy_cbnz_cbz (gdbarch, insn1, regs, dsc);
8263 break;
8264 case 12: case 13: /* POP */
8265 if (bit (insn1, 8)) /* PC is in register list. */
8266 err = thumb_copy_pop_pc_16bit (gdbarch, insn1, regs, dsc);
8267 else
8268 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "pop", dsc);
8269 break;
8270 case 15: /* If-Then, and hints */
8271 if (bits (insn1, 0, 3))
8272 /* If-Then makes up to four following instructions conditional.
8273 IT instruction itself is not conditional, so handle it as a
8274 common unmodified instruction. */
8275 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "If-Then",
8276 dsc);
8277 else
8278 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "hints", dsc);
8279 break;
8280 default:
8281 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "misc", dsc);
8282 }
8283 }
8284 break;
8285 case 12:
8286 if (op_bit_10_11 < 2) /* Store multiple registers */
8287 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "stm", dsc);
8288 else /* Load multiple registers */
8289 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldm", dsc);
8290 break;
8291 case 13: /* Conditional branch and supervisor call */
8292 if (bits (insn1, 9, 11) != 7) /* conditional branch */
8293 err = thumb_copy_b (gdbarch, insn1, dsc);
8294 else
8295 err = thumb_copy_svc (gdbarch, insn1, regs, dsc);
8296 break;
8297 case 14: /* Unconditional branch */
8298 err = thumb_copy_b (gdbarch, insn1, dsc);
8299 break;
8300 default:
8301 err = 1;
8302 }
8303
8304 if (err)
8305 internal_error (__FILE__, __LINE__,
8306 _("thumb_process_displaced_16bit_insn: Instruction decode error"));
8307}
8308
8309static int
8310decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
8311 uint16_t insn1, uint16_t insn2,
8312 struct regcache *regs,
8313 struct displaced_step_closure *dsc)
8314{
8315 int rt = bits (insn2, 12, 15);
8316 int rn = bits (insn1, 0, 3);
8317 int op1 = bits (insn1, 7, 8);
8318 int err = 0;
8319
8320 switch (bits (insn1, 5, 6))
8321 {
8322 case 0: /* Load byte and memory hints */
8323 if (rt == 0xf) /* PLD/PLI */
8324 {
8325 if (rn == 0xf)
8326 /* PLD literal or Encoding T3 of PLI(immediate, literal). */
8327 return thumb2_copy_preload (gdbarch, insn1, insn2, regs, dsc);
8328 else
8329 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8330 "pli/pld", dsc);
8331 }
8332 else
8333 {
8334 if (rn == 0xf) /* LDRB/LDRSB (literal) */
8335 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8336 1);
8337 else
8338 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8339 "ldrb{reg, immediate}/ldrbt",
8340 dsc);
8341 }
8342
8343 break;
8344 case 1: /* Load halfword and memory hints. */
8345 if (rt == 0xf) /* PLD{W} and Unalloc memory hint. */
8346 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8347 "pld/unalloc memhint", dsc);
8348 else
8349 {
8350 if (rn == 0xf)
8351 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8352 2);
8353 else
8354 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8355 "ldrh/ldrht", dsc);
8356 }
8357 break;
8358 case 2: /* Load word */
8359 {
8360 int insn2_bit_8_11 = bits (insn2, 8, 11);
8361
8362 if (rn == 0xf)
8363 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc, 4);
8364 else if (op1 == 0x1) /* Encoding T3 */
8365 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs, dsc,
8366 0, 1);
8367 else /* op1 == 0x0 */
8368 {
8369 if (insn2_bit_8_11 == 0xc || (insn2_bit_8_11 & 0x9) == 0x9)
8370 /* LDR (immediate) */
8371 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8372 dsc, bit (insn2, 8), 1);
8373 else if (insn2_bit_8_11 == 0xe) /* LDRT */
8374 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8375 "ldrt", dsc);
8376 else
8377 /* LDR (register) */
8378 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8379 dsc, 0, 0);
8380 }
8381 break;
8382 }
8383 default:
8384 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
8385 break;
8386 }
8387 return 0;
8388}
8389
8390static void
8391thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8392 uint16_t insn2, struct regcache *regs,
8393 struct displaced_step_closure *dsc)
8394{
8395 int err = 0;
8396 unsigned short op = bit (insn2, 15);
8397 unsigned int op1 = bits (insn1, 11, 12);
8398
8399 switch (op1)
8400 {
8401 case 1:
8402 {
8403 switch (bits (insn1, 9, 10))
8404 {
8405 case 0:
8406 if (bit (insn1, 6))
8407 {
8408 /* Load/store {dual, execlusive}, table branch. */
8409 if (bits (insn1, 7, 8) == 1 && bits (insn1, 4, 5) == 1
8410 && bits (insn2, 5, 7) == 0)
8411 err = thumb2_copy_table_branch (gdbarch, insn1, insn2, regs,
8412 dsc);
8413 else
8414 /* PC is not allowed to use in load/store {dual, exclusive}
8415 instructions. */
8416 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8417 "load/store dual/ex", dsc);
8418 }
8419 else /* load/store multiple */
8420 {
8421 switch (bits (insn1, 7, 8))
8422 {
8423 case 0: case 3: /* SRS, RFE */
8424 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8425 "srs/rfe", dsc);
8426 break;
8427 case 1: case 2: /* LDM/STM/PUSH/POP */
8428 err = thumb2_copy_block_xfer (gdbarch, insn1, insn2, regs, dsc);
8429 break;
8430 }
8431 }
8432 break;
8433
8434 case 1:
8435 /* Data-processing (shift register). */
8436 err = thumb2_decode_dp_shift_reg (gdbarch, insn1, insn2, regs,
8437 dsc);
8438 break;
8439 default: /* Coprocessor instructions. */
8440 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8441 break;
8442 }
8443 break;
8444 }
8445 case 2: /* op1 = 2 */
8446 if (op) /* Branch and misc control. */
8447 {
8448 if (bit (insn2, 14) /* BLX/BL */
8449 || bit (insn2, 12) /* Unconditional branch */
8450 || (bits (insn1, 7, 9) != 0x7)) /* Conditional branch */
8451 err = thumb2_copy_b_bl_blx (gdbarch, insn1, insn2, regs, dsc);
8452 else
8453 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8454 "misc ctrl", dsc);
8455 }
8456 else
8457 {
8458 if (bit (insn1, 9)) /* Data processing (plain binary imm). */
8459 {
8460 int op = bits (insn1, 4, 8);
8461 int rn = bits (insn1, 0, 3);
8462 if ((op == 0 || op == 0xa) && rn == 0xf)
8463 err = thumb_copy_pc_relative_32bit (gdbarch, insn1, insn2,
8464 regs, dsc);
8465 else
8466 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8467 "dp/pb", dsc);
8468 }
8469 else /* Data processing (modified immeidate) */
8470 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8471 "dp/mi", dsc);
8472 }
8473 break;
8474 case 3: /* op1 = 3 */
8475 switch (bits (insn1, 9, 10))
8476 {
8477 case 0:
8478 if (bit (insn1, 4))
8479 err = decode_thumb_32bit_ld_mem_hints (gdbarch, insn1, insn2,
8480 regs, dsc);
8481 else /* NEON Load/Store and Store single data item */
8482 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8483 "neon elt/struct load/store",
8484 dsc);
8485 break;
8486 case 1: /* op1 = 3, bits (9, 10) == 1 */
8487 switch (bits (insn1, 7, 8))
8488 {
8489 case 0: case 1: /* Data processing (register) */
8490 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8491 "dp(reg)", dsc);
8492 break;
8493 case 2: /* Multiply and absolute difference */
8494 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8495 "mul/mua/diff", dsc);
8496 break;
8497 case 3: /* Long multiply and divide */
8498 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8499 "lmul/lmua", dsc);
8500 break;
8501 }
8502 break;
8503 default: /* Coprocessor instructions */
8504 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8505 break;
8506 }
8507 break;
8508 default:
8509 err = 1;
8510 }
8511
8512 if (err)
8513 internal_error (__FILE__, __LINE__,
8514 _("thumb_process_displaced_32bit_insn: Instruction decode error"));
8515
8516}
8517
b434a28f
YQ
8518static void
8519thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8520 CORE_ADDR to, struct regcache *regs,
8521 struct displaced_step_closure *dsc)
8522{
34518530
YQ
8523 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8524 uint16_t insn1
8525 = read_memory_unsigned_integer (from, 2, byte_order_for_code);
8526
8527 if (debug_displaced)
8528 fprintf_unfiltered (gdb_stdlog, "displaced: process thumb insn %.4x "
8529 "at %.8lx\n", insn1, (unsigned long) from);
8530
8531 dsc->is_thumb = 1;
8532 dsc->insn_size = thumb_insn_size (insn1);
8533 if (thumb_insn_size (insn1) == 4)
8534 {
8535 uint16_t insn2
8536 = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
8537 thumb_process_displaced_32bit_insn (gdbarch, insn1, insn2, regs, dsc);
8538 }
8539 else
8540 thumb_process_displaced_16bit_insn (gdbarch, insn1, regs, dsc);
b434a28f
YQ
8541}
8542
cca44b1b 8543void
b434a28f
YQ
8544arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8545 CORE_ADDR to, struct regcache *regs,
cca44b1b
JB
8546 struct displaced_step_closure *dsc)
8547{
8548 int err = 0;
b434a28f
YQ
8549 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8550 uint32_t insn;
cca44b1b
JB
8551
8552 /* Most displaced instructions use a 1-instruction scratch space, so set this
8553 here and override below if/when necessary. */
8554 dsc->numinsns = 1;
8555 dsc->insn_addr = from;
8556 dsc->scratch_base = to;
8557 dsc->cleanup = NULL;
8558 dsc->wrote_to_pc = 0;
8559
b434a28f
YQ
8560 if (!displaced_in_arm_mode (regs))
8561 return thumb_process_displaced_insn (gdbarch, from, to, regs, dsc);
8562
4db71c0b
YQ
8563 dsc->is_thumb = 0;
8564 dsc->insn_size = 4;
b434a28f
YQ
8565 insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
8566 if (debug_displaced)
8567 fprintf_unfiltered (gdb_stdlog, "displaced: stepping insn %.8lx "
8568 "at %.8lx\n", (unsigned long) insn,
8569 (unsigned long) from);
8570
cca44b1b 8571 if ((insn & 0xf0000000) == 0xf0000000)
7ff120b4 8572 err = arm_decode_unconditional (gdbarch, insn, regs, dsc);
cca44b1b
JB
8573 else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
8574 {
8575 case 0x0: case 0x1: case 0x2: case 0x3:
7ff120b4 8576 err = arm_decode_dp_misc (gdbarch, insn, regs, dsc);
cca44b1b
JB
8577 break;
8578
8579 case 0x4: case 0x5: case 0x6:
7ff120b4 8580 err = arm_decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
cca44b1b
JB
8581 break;
8582
8583 case 0x7:
7ff120b4 8584 err = arm_decode_media (gdbarch, insn, dsc);
cca44b1b
JB
8585 break;
8586
8587 case 0x8: case 0x9: case 0xa: case 0xb:
7ff120b4 8588 err = arm_decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
cca44b1b
JB
8589 break;
8590
8591 case 0xc: case 0xd: case 0xe: case 0xf:
7ff120b4 8592 err = arm_decode_svc_copro (gdbarch, insn, to, regs, dsc);
cca44b1b
JB
8593 break;
8594 }
8595
8596 if (err)
8597 internal_error (__FILE__, __LINE__,
8598 _("arm_process_displaced_insn: Instruction decode error"));
8599}
8600
8601/* Actually set up the scratch space for a displaced instruction. */
8602
8603void
8604arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
8605 CORE_ADDR to, struct displaced_step_closure *dsc)
8606{
8607 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4db71c0b 8608 unsigned int i, len, offset;
cca44b1b 8609 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4db71c0b 8610 int size = dsc->is_thumb? 2 : 4;
948f8e3d 8611 const gdb_byte *bkp_insn;
cca44b1b 8612
4db71c0b 8613 offset = 0;
cca44b1b
JB
8614 /* Poke modified instruction(s). */
8615 for (i = 0; i < dsc->numinsns; i++)
8616 {
8617 if (debug_displaced)
4db71c0b
YQ
8618 {
8619 fprintf_unfiltered (gdb_stdlog, "displaced: writing insn ");
8620 if (size == 4)
8621 fprintf_unfiltered (gdb_stdlog, "%.8lx",
8622 dsc->modinsn[i]);
8623 else if (size == 2)
8624 fprintf_unfiltered (gdb_stdlog, "%.4x",
8625 (unsigned short)dsc->modinsn[i]);
8626
8627 fprintf_unfiltered (gdb_stdlog, " at %.8lx\n",
8628 (unsigned long) to + offset);
8629
8630 }
8631 write_memory_unsigned_integer (to + offset, size,
8632 byte_order_for_code,
cca44b1b 8633 dsc->modinsn[i]);
4db71c0b
YQ
8634 offset += size;
8635 }
8636
8637 /* Choose the correct breakpoint instruction. */
8638 if (dsc->is_thumb)
8639 {
8640 bkp_insn = tdep->thumb_breakpoint;
8641 len = tdep->thumb_breakpoint_size;
8642 }
8643 else
8644 {
8645 bkp_insn = tdep->arm_breakpoint;
8646 len = tdep->arm_breakpoint_size;
cca44b1b
JB
8647 }
8648
8649 /* Put breakpoint afterwards. */
4db71c0b 8650 write_memory (to + offset, bkp_insn, len);
cca44b1b
JB
8651
8652 if (debug_displaced)
8653 fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
8654 paddress (gdbarch, from), paddress (gdbarch, to));
8655}
8656
8657/* Entry point for copying an instruction into scratch space for displaced
8658 stepping. */
8659
8660struct displaced_step_closure *
8661arm_displaced_step_copy_insn (struct gdbarch *gdbarch,
8662 CORE_ADDR from, CORE_ADDR to,
8663 struct regcache *regs)
8664{
8665 struct displaced_step_closure *dsc
8666 = xmalloc (sizeof (struct displaced_step_closure));
b434a28f 8667 arm_process_displaced_insn (gdbarch, from, to, regs, dsc);
cca44b1b
JB
8668 arm_displaced_init_closure (gdbarch, from, to, dsc);
8669
8670 return dsc;
8671}
8672
8673/* Entry point for cleaning things up after a displaced instruction has been
8674 single-stepped. */
8675
8676void
8677arm_displaced_step_fixup (struct gdbarch *gdbarch,
8678 struct displaced_step_closure *dsc,
8679 CORE_ADDR from, CORE_ADDR to,
8680 struct regcache *regs)
8681{
8682 if (dsc->cleanup)
8683 dsc->cleanup (gdbarch, regs, dsc);
8684
8685 if (!dsc->wrote_to_pc)
4db71c0b
YQ
8686 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
8687 dsc->insn_addr + dsc->insn_size);
8688
cca44b1b
JB
8689}
8690
8691#include "bfd-in2.h"
8692#include "libcoff.h"
8693
8694static int
8695gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
8696{
9779414d
DJ
8697 struct gdbarch *gdbarch = info->application_data;
8698
8699 if (arm_pc_is_thumb (gdbarch, memaddr))
cca44b1b
JB
8700 {
8701 static asymbol *asym;
8702 static combined_entry_type ce;
8703 static struct coff_symbol_struct csym;
8704 static struct bfd fake_bfd;
8705 static bfd_target fake_target;
8706
8707 if (csym.native == NULL)
8708 {
8709 /* Create a fake symbol vector containing a Thumb symbol.
8710 This is solely so that the code in print_insn_little_arm()
8711 and print_insn_big_arm() in opcodes/arm-dis.c will detect
8712 the presence of a Thumb symbol and switch to decoding
8713 Thumb instructions. */
8714
8715 fake_target.flavour = bfd_target_coff_flavour;
8716 fake_bfd.xvec = &fake_target;
8717 ce.u.syment.n_sclass = C_THUMBEXTFUNC;
8718 csym.native = &ce;
8719 csym.symbol.the_bfd = &fake_bfd;
8720 csym.symbol.name = "fake";
8721 asym = (asymbol *) & csym;
8722 }
8723
8724 memaddr = UNMAKE_THUMB_ADDR (memaddr);
8725 info->symbols = &asym;
8726 }
8727 else
8728 info->symbols = NULL;
8729
8730 if (info->endian == BFD_ENDIAN_BIG)
8731 return print_insn_big_arm (memaddr, info);
8732 else
8733 return print_insn_little_arm (memaddr, info);
8734}
8735
8736/* The following define instruction sequences that will cause ARM
8737 cpu's to take an undefined instruction trap. These are used to
8738 signal a breakpoint to GDB.
8739
8740 The newer ARMv4T cpu's are capable of operating in ARM or Thumb
8741 modes. A different instruction is required for each mode. The ARM
8742 cpu's can also be big or little endian. Thus four different
8743 instructions are needed to support all cases.
8744
8745 Note: ARMv4 defines several new instructions that will take the
8746 undefined instruction trap. ARM7TDMI is nominally ARMv4T, but does
8747 not in fact add the new instructions. The new undefined
8748 instructions in ARMv4 are all instructions that had no defined
8749 behaviour in earlier chips. There is no guarantee that they will
8750 raise an exception, but may be treated as NOP's. In practice, it
8751 may only safe to rely on instructions matching:
8752
8753 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
8754 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
8755 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
8756
0963b4bd 8757 Even this may only true if the condition predicate is true. The
cca44b1b
JB
8758 following use a condition predicate of ALWAYS so it is always TRUE.
8759
8760 There are other ways of forcing a breakpoint. GNU/Linux, RISC iX,
8761 and NetBSD all use a software interrupt rather than an undefined
8762 instruction to force a trap. This can be handled by by the
8763 abi-specific code during establishment of the gdbarch vector. */
8764
8765#define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
8766#define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
8767#define THUMB_LE_BREAKPOINT {0xbe,0xbe}
8768#define THUMB_BE_BREAKPOINT {0xbe,0xbe}
8769
948f8e3d
PA
8770static const gdb_byte arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
8771static const gdb_byte arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
8772static const gdb_byte arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
8773static const gdb_byte arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
cca44b1b
JB
8774
8775/* Determine the type and size of breakpoint to insert at PCPTR. Uses
8776 the program counter value to determine whether a 16-bit or 32-bit
8777 breakpoint should be used. It returns a pointer to a string of
8778 bytes that encode a breakpoint instruction, stores the length of
8779 the string to *lenptr, and adjusts the program counter (if
8780 necessary) to point to the actual memory location where the
8781 breakpoint should be inserted. */
8782
8783static const unsigned char *
8784arm_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenptr)
8785{
8786 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
177321bd 8787 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
cca44b1b 8788
9779414d 8789 if (arm_pc_is_thumb (gdbarch, *pcptr))
cca44b1b
JB
8790 {
8791 *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
177321bd
DJ
8792
8793 /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
8794 check whether we are replacing a 32-bit instruction. */
8795 if (tdep->thumb2_breakpoint != NULL)
8796 {
8797 gdb_byte buf[2];
8798 if (target_read_memory (*pcptr, buf, 2) == 0)
8799 {
8800 unsigned short inst1;
8801 inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
db24da6d 8802 if (thumb_insn_size (inst1) == 4)
177321bd
DJ
8803 {
8804 *lenptr = tdep->thumb2_breakpoint_size;
8805 return tdep->thumb2_breakpoint;
8806 }
8807 }
8808 }
8809
cca44b1b
JB
8810 *lenptr = tdep->thumb_breakpoint_size;
8811 return tdep->thumb_breakpoint;
8812 }
8813 else
8814 {
8815 *lenptr = tdep->arm_breakpoint_size;
8816 return tdep->arm_breakpoint;
8817 }
8818}
8819
177321bd
DJ
8820static void
8821arm_remote_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
8822 int *kindptr)
8823{
177321bd
DJ
8824 arm_breakpoint_from_pc (gdbarch, pcptr, kindptr);
8825
9779414d 8826 if (arm_pc_is_thumb (gdbarch, *pcptr) && *kindptr == 4)
177321bd
DJ
8827 /* The documented magic value for a 32-bit Thumb-2 breakpoint, so
8828 that this is not confused with a 32-bit ARM breakpoint. */
8829 *kindptr = 3;
8830}
8831
cca44b1b
JB
8832/* Extract from an array REGBUF containing the (raw) register state a
8833 function return value of type TYPE, and copy that, in virtual
8834 format, into VALBUF. */
8835
8836static void
8837arm_extract_return_value (struct type *type, struct regcache *regs,
8838 gdb_byte *valbuf)
8839{
8840 struct gdbarch *gdbarch = get_regcache_arch (regs);
8841 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8842
8843 if (TYPE_CODE_FLT == TYPE_CODE (type))
8844 {
8845 switch (gdbarch_tdep (gdbarch)->fp_model)
8846 {
8847 case ARM_FLOAT_FPA:
8848 {
8849 /* The value is in register F0 in internal format. We need to
8850 extract the raw value and then convert it to the desired
8851 internal type. */
8852 bfd_byte tmpbuf[FP_REGISTER_SIZE];
8853
8854 regcache_cooked_read (regs, ARM_F0_REGNUM, tmpbuf);
8855 convert_from_extended (floatformat_from_type (type), tmpbuf,
8856 valbuf, gdbarch_byte_order (gdbarch));
8857 }
8858 break;
8859
8860 case ARM_FLOAT_SOFT_FPA:
8861 case ARM_FLOAT_SOFT_VFP:
8862 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8863 not using the VFP ABI code. */
8864 case ARM_FLOAT_VFP:
8865 regcache_cooked_read (regs, ARM_A1_REGNUM, valbuf);
8866 if (TYPE_LENGTH (type) > 4)
8867 regcache_cooked_read (regs, ARM_A1_REGNUM + 1,
8868 valbuf + INT_REGISTER_SIZE);
8869 break;
8870
8871 default:
0963b4bd
MS
8872 internal_error (__FILE__, __LINE__,
8873 _("arm_extract_return_value: "
8874 "Floating point model not supported"));
cca44b1b
JB
8875 break;
8876 }
8877 }
8878 else if (TYPE_CODE (type) == TYPE_CODE_INT
8879 || TYPE_CODE (type) == TYPE_CODE_CHAR
8880 || TYPE_CODE (type) == TYPE_CODE_BOOL
8881 || TYPE_CODE (type) == TYPE_CODE_PTR
8882 || TYPE_CODE (type) == TYPE_CODE_REF
8883 || TYPE_CODE (type) == TYPE_CODE_ENUM)
8884 {
b021a221
MS
8885 /* If the type is a plain integer, then the access is
8886 straight-forward. Otherwise we have to play around a bit
8887 more. */
cca44b1b
JB
8888 int len = TYPE_LENGTH (type);
8889 int regno = ARM_A1_REGNUM;
8890 ULONGEST tmp;
8891
8892 while (len > 0)
8893 {
8894 /* By using store_unsigned_integer we avoid having to do
8895 anything special for small big-endian values. */
8896 regcache_cooked_read_unsigned (regs, regno++, &tmp);
8897 store_unsigned_integer (valbuf,
8898 (len > INT_REGISTER_SIZE
8899 ? INT_REGISTER_SIZE : len),
8900 byte_order, tmp);
8901 len -= INT_REGISTER_SIZE;
8902 valbuf += INT_REGISTER_SIZE;
8903 }
8904 }
8905 else
8906 {
8907 /* For a structure or union the behaviour is as if the value had
8908 been stored to word-aligned memory and then loaded into
8909 registers with 32-bit load instruction(s). */
8910 int len = TYPE_LENGTH (type);
8911 int regno = ARM_A1_REGNUM;
8912 bfd_byte tmpbuf[INT_REGISTER_SIZE];
8913
8914 while (len > 0)
8915 {
8916 regcache_cooked_read (regs, regno++, tmpbuf);
8917 memcpy (valbuf, tmpbuf,
8918 len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
8919 len -= INT_REGISTER_SIZE;
8920 valbuf += INT_REGISTER_SIZE;
8921 }
8922 }
8923}
8924
8925
8926/* Will a function return an aggregate type in memory or in a
8927 register? Return 0 if an aggregate type can be returned in a
8928 register, 1 if it must be returned in memory. */
8929
8930static int
8931arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
8932{
8933 int nRc;
8934 enum type_code code;
8935
8936 CHECK_TYPEDEF (type);
8937
8938 /* In the ARM ABI, "integer" like aggregate types are returned in
8939 registers. For an aggregate type to be integer like, its size
8940 must be less than or equal to INT_REGISTER_SIZE and the
8941 offset of each addressable subfield must be zero. Note that bit
8942 fields are not addressable, and all addressable subfields of
8943 unions always start at offset zero.
8944
8945 This function is based on the behaviour of GCC 2.95.1.
8946 See: gcc/arm.c: arm_return_in_memory() for details.
8947
8948 Note: All versions of GCC before GCC 2.95.2 do not set up the
8949 parameters correctly for a function returning the following
8950 structure: struct { float f;}; This should be returned in memory,
8951 not a register. Richard Earnshaw sent me a patch, but I do not
8952 know of any way to detect if a function like the above has been
8953 compiled with the correct calling convention. */
8954
8955 /* All aggregate types that won't fit in a register must be returned
8956 in memory. */
8957 if (TYPE_LENGTH (type) > INT_REGISTER_SIZE)
8958 {
8959 return 1;
8960 }
8961
8962 /* The AAPCS says all aggregates not larger than a word are returned
8963 in a register. */
8964 if (gdbarch_tdep (gdbarch)->arm_abi != ARM_ABI_APCS)
8965 return 0;
8966
8967 /* The only aggregate types that can be returned in a register are
8968 structs and unions. Arrays must be returned in memory. */
8969 code = TYPE_CODE (type);
8970 if ((TYPE_CODE_STRUCT != code) && (TYPE_CODE_UNION != code))
8971 {
8972 return 1;
8973 }
8974
8975 /* Assume all other aggregate types can be returned in a register.
8976 Run a check for structures, unions and arrays. */
8977 nRc = 0;
8978
8979 if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
8980 {
8981 int i;
8982 /* Need to check if this struct/union is "integer" like. For
8983 this to be true, its size must be less than or equal to
8984 INT_REGISTER_SIZE and the offset of each addressable
8985 subfield must be zero. Note that bit fields are not
8986 addressable, and unions always start at offset zero. If any
8987 of the subfields is a floating point type, the struct/union
8988 cannot be an integer type. */
8989
8990 /* For each field in the object, check:
8991 1) Is it FP? --> yes, nRc = 1;
67255d04
RE
8992 2) Is it addressable (bitpos != 0) and
8993 not packed (bitsize == 0)?
8994 --> yes, nRc = 1
8995 */
8996
8997 for (i = 0; i < TYPE_NFIELDS (type); i++)
8998 {
8999 enum type_code field_type_code;
0963b4bd
MS
9000 field_type_code = TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type,
9001 i)));
67255d04
RE
9002
9003 /* Is it a floating point type field? */
9004 if (field_type_code == TYPE_CODE_FLT)
9005 {
9006 nRc = 1;
9007 break;
9008 }
9009
9010 /* If bitpos != 0, then we have to care about it. */
9011 if (TYPE_FIELD_BITPOS (type, i) != 0)
9012 {
9013 /* Bitfields are not addressable. If the field bitsize is
9014 zero, then the field is not packed. Hence it cannot be
9015 a bitfield or any other packed type. */
9016 if (TYPE_FIELD_BITSIZE (type, i) == 0)
9017 {
9018 nRc = 1;
9019 break;
9020 }
9021 }
9022 }
9023 }
9024
9025 return nRc;
9026}
9027
34e8f22d
RE
9028/* Write into appropriate registers a function return value of type
9029 TYPE, given in virtual format. */
9030
9031static void
b508a996 9032arm_store_return_value (struct type *type, struct regcache *regs,
5238cf52 9033 const gdb_byte *valbuf)
34e8f22d 9034{
be8626e0 9035 struct gdbarch *gdbarch = get_regcache_arch (regs);
e17a4113 9036 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
be8626e0 9037
34e8f22d
RE
9038 if (TYPE_CODE (type) == TYPE_CODE_FLT)
9039 {
e362b510 9040 gdb_byte buf[MAX_REGISTER_SIZE];
34e8f22d 9041
be8626e0 9042 switch (gdbarch_tdep (gdbarch)->fp_model)
08216dd7
RE
9043 {
9044 case ARM_FLOAT_FPA:
9045
be8626e0
MD
9046 convert_to_extended (floatformat_from_type (type), buf, valbuf,
9047 gdbarch_byte_order (gdbarch));
b508a996 9048 regcache_cooked_write (regs, ARM_F0_REGNUM, buf);
08216dd7
RE
9049 break;
9050
fd50bc42 9051 case ARM_FLOAT_SOFT_FPA:
08216dd7 9052 case ARM_FLOAT_SOFT_VFP:
90445bd3
DJ
9053 /* ARM_FLOAT_VFP can arise if this is a variadic function so
9054 not using the VFP ABI code. */
9055 case ARM_FLOAT_VFP:
b508a996
RE
9056 regcache_cooked_write (regs, ARM_A1_REGNUM, valbuf);
9057 if (TYPE_LENGTH (type) > 4)
9058 regcache_cooked_write (regs, ARM_A1_REGNUM + 1,
7a5ea0d4 9059 valbuf + INT_REGISTER_SIZE);
08216dd7
RE
9060 break;
9061
9062 default:
9b20d036
MS
9063 internal_error (__FILE__, __LINE__,
9064 _("arm_store_return_value: Floating "
9065 "point model not supported"));
08216dd7
RE
9066 break;
9067 }
34e8f22d 9068 }
b508a996
RE
9069 else if (TYPE_CODE (type) == TYPE_CODE_INT
9070 || TYPE_CODE (type) == TYPE_CODE_CHAR
9071 || TYPE_CODE (type) == TYPE_CODE_BOOL
9072 || TYPE_CODE (type) == TYPE_CODE_PTR
9073 || TYPE_CODE (type) == TYPE_CODE_REF
9074 || TYPE_CODE (type) == TYPE_CODE_ENUM)
9075 {
9076 if (TYPE_LENGTH (type) <= 4)
9077 {
9078 /* Values of one word or less are zero/sign-extended and
9079 returned in r0. */
7a5ea0d4 9080 bfd_byte tmpbuf[INT_REGISTER_SIZE];
b508a996
RE
9081 LONGEST val = unpack_long (type, valbuf);
9082
e17a4113 9083 store_signed_integer (tmpbuf, INT_REGISTER_SIZE, byte_order, val);
b508a996
RE
9084 regcache_cooked_write (regs, ARM_A1_REGNUM, tmpbuf);
9085 }
9086 else
9087 {
9088 /* Integral values greater than one word are stored in consecutive
9089 registers starting with r0. This will always be a multiple of
9090 the regiser size. */
9091 int len = TYPE_LENGTH (type);
9092 int regno = ARM_A1_REGNUM;
9093
9094 while (len > 0)
9095 {
9096 regcache_cooked_write (regs, regno++, valbuf);
7a5ea0d4
DJ
9097 len -= INT_REGISTER_SIZE;
9098 valbuf += INT_REGISTER_SIZE;
b508a996
RE
9099 }
9100 }
9101 }
34e8f22d 9102 else
b508a996
RE
9103 {
9104 /* For a structure or union the behaviour is as if the value had
9105 been stored to word-aligned memory and then loaded into
9106 registers with 32-bit load instruction(s). */
9107 int len = TYPE_LENGTH (type);
9108 int regno = ARM_A1_REGNUM;
7a5ea0d4 9109 bfd_byte tmpbuf[INT_REGISTER_SIZE];
b508a996
RE
9110
9111 while (len > 0)
9112 {
9113 memcpy (tmpbuf, valbuf,
7a5ea0d4 9114 len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
b508a996 9115 regcache_cooked_write (regs, regno++, tmpbuf);
7a5ea0d4
DJ
9116 len -= INT_REGISTER_SIZE;
9117 valbuf += INT_REGISTER_SIZE;
b508a996
RE
9118 }
9119 }
34e8f22d
RE
9120}
9121
2af48f68
PB
9122
9123/* Handle function return values. */
9124
9125static enum return_value_convention
6a3a010b 9126arm_return_value (struct gdbarch *gdbarch, struct value *function,
c055b101
CV
9127 struct type *valtype, struct regcache *regcache,
9128 gdb_byte *readbuf, const gdb_byte *writebuf)
2af48f68 9129{
7c00367c 9130 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
6a3a010b 9131 struct type *func_type = function ? value_type (function) : NULL;
90445bd3
DJ
9132 enum arm_vfp_cprc_base_type vfp_base_type;
9133 int vfp_base_count;
9134
9135 if (arm_vfp_abi_for_function (gdbarch, func_type)
9136 && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
9137 {
9138 int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
9139 int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
9140 int i;
9141 for (i = 0; i < vfp_base_count; i++)
9142 {
58d6951d
DJ
9143 if (reg_char == 'q')
9144 {
9145 if (writebuf)
9146 arm_neon_quad_write (gdbarch, regcache, i,
9147 writebuf + i * unit_length);
9148
9149 if (readbuf)
9150 arm_neon_quad_read (gdbarch, regcache, i,
9151 readbuf + i * unit_length);
9152 }
9153 else
9154 {
9155 char name_buf[4];
9156 int regnum;
9157
8c042590 9158 xsnprintf (name_buf, sizeof (name_buf), "%c%d", reg_char, i);
58d6951d
DJ
9159 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9160 strlen (name_buf));
9161 if (writebuf)
9162 regcache_cooked_write (regcache, regnum,
9163 writebuf + i * unit_length);
9164 if (readbuf)
9165 regcache_cooked_read (regcache, regnum,
9166 readbuf + i * unit_length);
9167 }
90445bd3
DJ
9168 }
9169 return RETURN_VALUE_REGISTER_CONVENTION;
9170 }
7c00367c 9171
2af48f68
PB
9172 if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
9173 || TYPE_CODE (valtype) == TYPE_CODE_UNION
9174 || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
9175 {
7c00367c
MK
9176 if (tdep->struct_return == pcc_struct_return
9177 || arm_return_in_memory (gdbarch, valtype))
2af48f68
PB
9178 return RETURN_VALUE_STRUCT_CONVENTION;
9179 }
9180
7052e42c
UW
9181 /* AAPCS returns complex types longer than a register in memory. */
9182 if (tdep->arm_abi != ARM_ABI_APCS
9183 && TYPE_CODE (valtype) == TYPE_CODE_COMPLEX
9184 && TYPE_LENGTH (valtype) > INT_REGISTER_SIZE)
9185 return RETURN_VALUE_STRUCT_CONVENTION;
9186
2af48f68
PB
9187 if (writebuf)
9188 arm_store_return_value (valtype, regcache, writebuf);
9189
9190 if (readbuf)
9191 arm_extract_return_value (valtype, regcache, readbuf);
9192
9193 return RETURN_VALUE_REGISTER_CONVENTION;
9194}
9195
9196
9df628e0 9197static int
60ade65d 9198arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
9df628e0 9199{
e17a4113
UW
9200 struct gdbarch *gdbarch = get_frame_arch (frame);
9201 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
9202 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
9df628e0 9203 CORE_ADDR jb_addr;
e362b510 9204 gdb_byte buf[INT_REGISTER_SIZE];
9df628e0 9205
60ade65d 9206 jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
9df628e0
RE
9207
9208 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
7a5ea0d4 9209 INT_REGISTER_SIZE))
9df628e0
RE
9210 return 0;
9211
e17a4113 9212 *pc = extract_unsigned_integer (buf, INT_REGISTER_SIZE, byte_order);
9df628e0
RE
9213 return 1;
9214}
9215
faa95490
DJ
9216/* Recognize GCC and GNU ld's trampolines. If we are in a trampoline,
9217 return the target PC. Otherwise return 0. */
c906108c
SS
9218
9219CORE_ADDR
52f729a7 9220arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
c906108c 9221{
2c02bd72 9222 const char *name;
faa95490 9223 int namelen;
c906108c
SS
9224 CORE_ADDR start_addr;
9225
9226 /* Find the starting address and name of the function containing the PC. */
9227 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
9228 return 0;
9229
faa95490
DJ
9230 /* If PC is in a Thumb call or return stub, return the address of the
9231 target PC, which is in a register. The thunk functions are called
9232 _call_via_xx, where x is the register name. The possible names
3d8d5e79
DJ
9233 are r0-r9, sl, fp, ip, sp, and lr. ARM RealView has similar
9234 functions, named __ARM_call_via_r[0-7]. */
9235 if (strncmp (name, "_call_via_", 10) == 0
9236 || strncmp (name, "__ARM_call_via_", strlen ("__ARM_call_via_")) == 0)
c906108c 9237 {
ed9a39eb
JM
9238 /* Use the name suffix to determine which register contains the
9239 target PC. */
c5aa993b
JM
9240 static char *table[15] =
9241 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
9242 "r8", "r9", "sl", "fp", "ip", "sp", "lr"
9243 };
c906108c 9244 int regno;
faa95490 9245 int offset = strlen (name) - 2;
c906108c
SS
9246
9247 for (regno = 0; regno <= 14; regno++)
faa95490 9248 if (strcmp (&name[offset], table[regno]) == 0)
52f729a7 9249 return get_frame_register_unsigned (frame, regno);
c906108c 9250 }
ed9a39eb 9251
faa95490
DJ
9252 /* GNU ld generates __foo_from_arm or __foo_from_thumb for
9253 non-interworking calls to foo. We could decode the stubs
9254 to find the target but it's easier to use the symbol table. */
9255 namelen = strlen (name);
9256 if (name[0] == '_' && name[1] == '_'
9257 && ((namelen > 2 + strlen ("_from_thumb")
9258 && strncmp (name + namelen - strlen ("_from_thumb"), "_from_thumb",
9259 strlen ("_from_thumb")) == 0)
9260 || (namelen > 2 + strlen ("_from_arm")
9261 && strncmp (name + namelen - strlen ("_from_arm"), "_from_arm",
9262 strlen ("_from_arm")) == 0)))
9263 {
9264 char *target_name;
9265 int target_len = namelen - 2;
9266 struct minimal_symbol *minsym;
9267 struct objfile *objfile;
9268 struct obj_section *sec;
9269
9270 if (name[namelen - 1] == 'b')
9271 target_len -= strlen ("_from_thumb");
9272 else
9273 target_len -= strlen ("_from_arm");
9274
9275 target_name = alloca (target_len + 1);
9276 memcpy (target_name, name + 2, target_len);
9277 target_name[target_len] = '\0';
9278
9279 sec = find_pc_section (pc);
9280 objfile = (sec == NULL) ? NULL : sec->objfile;
9281 minsym = lookup_minimal_symbol (target_name, NULL, objfile);
9282 if (minsym != NULL)
9283 return SYMBOL_VALUE_ADDRESS (minsym);
9284 else
9285 return 0;
9286 }
9287
c5aa993b 9288 return 0; /* not a stub */
c906108c
SS
9289}
9290
afd7eef0
RE
9291static void
9292set_arm_command (char *args, int from_tty)
9293{
edefbb7c
AC
9294 printf_unfiltered (_("\
9295\"set arm\" must be followed by an apporpriate subcommand.\n"));
afd7eef0
RE
9296 help_list (setarmcmdlist, "set arm ", all_commands, gdb_stdout);
9297}
9298
9299static void
9300show_arm_command (char *args, int from_tty)
9301{
26304000 9302 cmd_show_list (showarmcmdlist, from_tty, "");
afd7eef0
RE
9303}
9304
28e97307
DJ
9305static void
9306arm_update_current_architecture (void)
fd50bc42 9307{
28e97307 9308 struct gdbarch_info info;
fd50bc42 9309
28e97307 9310 /* If the current architecture is not ARM, we have nothing to do. */
f5656ead 9311 if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_arm)
28e97307 9312 return;
fd50bc42 9313
28e97307
DJ
9314 /* Update the architecture. */
9315 gdbarch_info_init (&info);
fd50bc42 9316
28e97307 9317 if (!gdbarch_update_p (info))
9b20d036 9318 internal_error (__FILE__, __LINE__, _("could not update architecture"));
fd50bc42
RE
9319}
9320
9321static void
9322set_fp_model_sfunc (char *args, int from_tty,
9323 struct cmd_list_element *c)
9324{
9325 enum arm_float_model fp_model;
9326
9327 for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
9328 if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
9329 {
9330 arm_fp_model = fp_model;
9331 break;
9332 }
9333
9334 if (fp_model == ARM_FLOAT_LAST)
edefbb7c 9335 internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
fd50bc42
RE
9336 current_fp_model);
9337
28e97307 9338 arm_update_current_architecture ();
fd50bc42
RE
9339}
9340
9341static void
08546159
AC
9342show_fp_model (struct ui_file *file, int from_tty,
9343 struct cmd_list_element *c, const char *value)
fd50bc42 9344{
f5656ead 9345 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
fd50bc42 9346
28e97307 9347 if (arm_fp_model == ARM_FLOAT_AUTO
f5656ead 9348 && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
28e97307
DJ
9349 fprintf_filtered (file, _("\
9350The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
9351 fp_model_strings[tdep->fp_model]);
9352 else
9353 fprintf_filtered (file, _("\
9354The current ARM floating point model is \"%s\".\n"),
9355 fp_model_strings[arm_fp_model]);
9356}
9357
9358static void
9359arm_set_abi (char *args, int from_tty,
9360 struct cmd_list_element *c)
9361{
9362 enum arm_abi_kind arm_abi;
9363
9364 for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
9365 if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
9366 {
9367 arm_abi_global = arm_abi;
9368 break;
9369 }
9370
9371 if (arm_abi == ARM_ABI_LAST)
9372 internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
9373 arm_abi_string);
9374
9375 arm_update_current_architecture ();
9376}
9377
9378static void
9379arm_show_abi (struct ui_file *file, int from_tty,
9380 struct cmd_list_element *c, const char *value)
9381{
f5656ead 9382 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
28e97307
DJ
9383
9384 if (arm_abi_global == ARM_ABI_AUTO
f5656ead 9385 && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
28e97307
DJ
9386 fprintf_filtered (file, _("\
9387The current ARM ABI is \"auto\" (currently \"%s\").\n"),
9388 arm_abi_strings[tdep->arm_abi]);
9389 else
9390 fprintf_filtered (file, _("The current ARM ABI is \"%s\".\n"),
9391 arm_abi_string);
fd50bc42
RE
9392}
9393
0428b8f5
DJ
9394static void
9395arm_show_fallback_mode (struct ui_file *file, int from_tty,
9396 struct cmd_list_element *c, const char *value)
9397{
0963b4bd
MS
9398 fprintf_filtered (file,
9399 _("The current execution mode assumed "
9400 "(when symbols are unavailable) is \"%s\".\n"),
0428b8f5
DJ
9401 arm_fallback_mode_string);
9402}
9403
9404static void
9405arm_show_force_mode (struct ui_file *file, int from_tty,
9406 struct cmd_list_element *c, const char *value)
9407{
f5656ead 9408 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
0428b8f5 9409
0963b4bd
MS
9410 fprintf_filtered (file,
9411 _("The current execution mode assumed "
9412 "(even when symbols are available) is \"%s\".\n"),
0428b8f5
DJ
9413 arm_force_mode_string);
9414}
9415
afd7eef0
RE
9416/* If the user changes the register disassembly style used for info
9417 register and other commands, we have to also switch the style used
9418 in opcodes for disassembly output. This function is run in the "set
9419 arm disassembly" command, and does that. */
bc90b915
FN
9420
9421static void
afd7eef0 9422set_disassembly_style_sfunc (char *args, int from_tty,
bc90b915
FN
9423 struct cmd_list_element *c)
9424{
afd7eef0 9425 set_disassembly_style ();
bc90b915
FN
9426}
9427\f
966fbf70 9428/* Return the ARM register name corresponding to register I. */
a208b0cb 9429static const char *
d93859e2 9430arm_register_name (struct gdbarch *gdbarch, int i)
966fbf70 9431{
58d6951d
DJ
9432 const int num_regs = gdbarch_num_regs (gdbarch);
9433
9434 if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
9435 && i >= num_regs && i < num_regs + 32)
9436 {
9437 static const char *const vfp_pseudo_names[] = {
9438 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
9439 "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
9440 "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
9441 "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
9442 };
9443
9444 return vfp_pseudo_names[i - num_regs];
9445 }
9446
9447 if (gdbarch_tdep (gdbarch)->have_neon_pseudos
9448 && i >= num_regs + 32 && i < num_regs + 32 + 16)
9449 {
9450 static const char *const neon_pseudo_names[] = {
9451 "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
9452 "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
9453 };
9454
9455 return neon_pseudo_names[i - num_regs - 32];
9456 }
9457
ff6f572f
DJ
9458 if (i >= ARRAY_SIZE (arm_register_names))
9459 /* These registers are only supported on targets which supply
9460 an XML description. */
9461 return "";
9462
966fbf70
RE
9463 return arm_register_names[i];
9464}
9465
bc90b915 9466static void
afd7eef0 9467set_disassembly_style (void)
bc90b915 9468{
123dc839 9469 int current;
bc90b915 9470
123dc839
DJ
9471 /* Find the style that the user wants. */
9472 for (current = 0; current < num_disassembly_options; current++)
9473 if (disassembly_style == valid_disassembly_styles[current])
9474 break;
9475 gdb_assert (current < num_disassembly_options);
bc90b915 9476
94c30b78 9477 /* Synchronize the disassembler. */
bc90b915
FN
9478 set_arm_regname_option (current);
9479}
9480
082fc60d
RE
9481/* Test whether the coff symbol specific value corresponds to a Thumb
9482 function. */
9483
9484static int
9485coff_sym_is_thumb (int val)
9486{
f8bf5763
PM
9487 return (val == C_THUMBEXT
9488 || val == C_THUMBSTAT
9489 || val == C_THUMBEXTFUNC
9490 || val == C_THUMBSTATFUNC
9491 || val == C_THUMBLABEL);
082fc60d
RE
9492}
9493
9494/* arm_coff_make_msymbol_special()
9495 arm_elf_make_msymbol_special()
9496
9497 These functions test whether the COFF or ELF symbol corresponds to
9498 an address in thumb code, and set a "special" bit in a minimal
9499 symbol to indicate that it does. */
9500
34e8f22d 9501static void
082fc60d
RE
9502arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
9503{
467d42c4
UW
9504 if (ARM_SYM_BRANCH_TYPE (&((elf_symbol_type *)sym)->internal_elf_sym)
9505 == ST_BRANCH_TO_THUMB)
082fc60d
RE
9506 MSYMBOL_SET_SPECIAL (msym);
9507}
9508
34e8f22d 9509static void
082fc60d
RE
9510arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
9511{
9512 if (coff_sym_is_thumb (val))
9513 MSYMBOL_SET_SPECIAL (msym);
9514}
9515
60c5725c 9516static void
c1bd65d0 9517arm_objfile_data_free (struct objfile *objfile, void *arg)
60c5725c
DJ
9518{
9519 struct arm_per_objfile *data = arg;
9520 unsigned int i;
9521
9522 for (i = 0; i < objfile->obfd->section_count; i++)
9523 VEC_free (arm_mapping_symbol_s, data->section_maps[i]);
9524}
9525
9526static void
9527arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
9528 asymbol *sym)
9529{
9530 const char *name = bfd_asymbol_name (sym);
9531 struct arm_per_objfile *data;
9532 VEC(arm_mapping_symbol_s) **map_p;
9533 struct arm_mapping_symbol new_map_sym;
9534
9535 gdb_assert (name[0] == '$');
9536 if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
9537 return;
9538
9539 data = objfile_data (objfile, arm_objfile_data_key);
9540 if (data == NULL)
9541 {
9542 data = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9543 struct arm_per_objfile);
9544 set_objfile_data (objfile, arm_objfile_data_key, data);
9545 data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
9546 objfile->obfd->section_count,
9547 VEC(arm_mapping_symbol_s) *);
9548 }
9549 map_p = &data->section_maps[bfd_get_section (sym)->index];
9550
9551 new_map_sym.value = sym->value;
9552 new_map_sym.type = name[1];
9553
9554 /* Assume that most mapping symbols appear in order of increasing
9555 value. If they were randomly distributed, it would be faster to
9556 always push here and then sort at first use. */
9557 if (!VEC_empty (arm_mapping_symbol_s, *map_p))
9558 {
9559 struct arm_mapping_symbol *prev_map_sym;
9560
9561 prev_map_sym = VEC_last (arm_mapping_symbol_s, *map_p);
9562 if (prev_map_sym->value >= sym->value)
9563 {
9564 unsigned int idx;
9565 idx = VEC_lower_bound (arm_mapping_symbol_s, *map_p, &new_map_sym,
9566 arm_compare_mapping_symbols);
9567 VEC_safe_insert (arm_mapping_symbol_s, *map_p, idx, &new_map_sym);
9568 return;
9569 }
9570 }
9571
9572 VEC_safe_push (arm_mapping_symbol_s, *map_p, &new_map_sym);
9573}
9574
756fe439 9575static void
61a1198a 9576arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
756fe439 9577{
9779414d 9578 struct gdbarch *gdbarch = get_regcache_arch (regcache);
61a1198a 9579 regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
756fe439
DJ
9580
9581 /* If necessary, set the T bit. */
9582 if (arm_apcs_32)
9583 {
9779414d 9584 ULONGEST val, t_bit;
61a1198a 9585 regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
9779414d
DJ
9586 t_bit = arm_psr_thumb_bit (gdbarch);
9587 if (arm_pc_is_thumb (gdbarch, pc))
9588 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9589 val | t_bit);
756fe439 9590 else
61a1198a 9591 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9779414d 9592 val & ~t_bit);
756fe439
DJ
9593 }
9594}
123dc839 9595
58d6951d
DJ
9596/* Read the contents of a NEON quad register, by reading from two
9597 double registers. This is used to implement the quad pseudo
9598 registers, and for argument passing in case the quad registers are
9599 missing; vectors are passed in quad registers when using the VFP
9600 ABI, even if a NEON unit is not present. REGNUM is the index of
9601 the quad register, in [0, 15]. */
9602
05d1431c 9603static enum register_status
58d6951d
DJ
9604arm_neon_quad_read (struct gdbarch *gdbarch, struct regcache *regcache,
9605 int regnum, gdb_byte *buf)
9606{
9607 char name_buf[4];
9608 gdb_byte reg_buf[8];
9609 int offset, double_regnum;
05d1431c 9610 enum register_status status;
58d6951d 9611
8c042590 9612 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
58d6951d
DJ
9613 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9614 strlen (name_buf));
9615
9616 /* d0 is always the least significant half of q0. */
9617 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9618 offset = 8;
9619 else
9620 offset = 0;
9621
05d1431c
PA
9622 status = regcache_raw_read (regcache, double_regnum, reg_buf);
9623 if (status != REG_VALID)
9624 return status;
58d6951d
DJ
9625 memcpy (buf + offset, reg_buf, 8);
9626
9627 offset = 8 - offset;
05d1431c
PA
9628 status = regcache_raw_read (regcache, double_regnum + 1, reg_buf);
9629 if (status != REG_VALID)
9630 return status;
58d6951d 9631 memcpy (buf + offset, reg_buf, 8);
05d1431c
PA
9632
9633 return REG_VALID;
58d6951d
DJ
9634}
9635
05d1431c 9636static enum register_status
58d6951d
DJ
9637arm_pseudo_read (struct gdbarch *gdbarch, struct regcache *regcache,
9638 int regnum, gdb_byte *buf)
9639{
9640 const int num_regs = gdbarch_num_regs (gdbarch);
9641 char name_buf[4];
9642 gdb_byte reg_buf[8];
9643 int offset, double_regnum;
9644
9645 gdb_assert (regnum >= num_regs);
9646 regnum -= num_regs;
9647
9648 if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
9649 /* Quad-precision register. */
05d1431c 9650 return arm_neon_quad_read (gdbarch, regcache, regnum - 32, buf);
58d6951d
DJ
9651 else
9652 {
05d1431c
PA
9653 enum register_status status;
9654
58d6951d
DJ
9655 /* Single-precision register. */
9656 gdb_assert (regnum < 32);
9657
9658 /* s0 is always the least significant half of d0. */
9659 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9660 offset = (regnum & 1) ? 0 : 4;
9661 else
9662 offset = (regnum & 1) ? 4 : 0;
9663
8c042590 9664 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
58d6951d
DJ
9665 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9666 strlen (name_buf));
9667
05d1431c
PA
9668 status = regcache_raw_read (regcache, double_regnum, reg_buf);
9669 if (status == REG_VALID)
9670 memcpy (buf, reg_buf + offset, 4);
9671 return status;
58d6951d
DJ
9672 }
9673}
9674
9675/* Store the contents of BUF to a NEON quad register, by writing to
9676 two double registers. This is used to implement the quad pseudo
9677 registers, and for argument passing in case the quad registers are
9678 missing; vectors are passed in quad registers when using the VFP
9679 ABI, even if a NEON unit is not present. REGNUM is the index
9680 of the quad register, in [0, 15]. */
9681
9682static void
9683arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
9684 int regnum, const gdb_byte *buf)
9685{
9686 char name_buf[4];
58d6951d
DJ
9687 int offset, double_regnum;
9688
8c042590 9689 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
58d6951d
DJ
9690 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9691 strlen (name_buf));
9692
9693 /* d0 is always the least significant half of q0. */
9694 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9695 offset = 8;
9696 else
9697 offset = 0;
9698
9699 regcache_raw_write (regcache, double_regnum, buf + offset);
9700 offset = 8 - offset;
9701 regcache_raw_write (regcache, double_regnum + 1, buf + offset);
9702}
9703
9704static void
9705arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
9706 int regnum, const gdb_byte *buf)
9707{
9708 const int num_regs = gdbarch_num_regs (gdbarch);
9709 char name_buf[4];
9710 gdb_byte reg_buf[8];
9711 int offset, double_regnum;
9712
9713 gdb_assert (regnum >= num_regs);
9714 regnum -= num_regs;
9715
9716 if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
9717 /* Quad-precision register. */
9718 arm_neon_quad_write (gdbarch, regcache, regnum - 32, buf);
9719 else
9720 {
9721 /* Single-precision register. */
9722 gdb_assert (regnum < 32);
9723
9724 /* s0 is always the least significant half of d0. */
9725 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9726 offset = (regnum & 1) ? 0 : 4;
9727 else
9728 offset = (regnum & 1) ? 4 : 0;
9729
8c042590 9730 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
58d6951d
DJ
9731 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9732 strlen (name_buf));
9733
9734 regcache_raw_read (regcache, double_regnum, reg_buf);
9735 memcpy (reg_buf + offset, buf, 4);
9736 regcache_raw_write (regcache, double_regnum, reg_buf);
9737 }
9738}
9739
123dc839
DJ
9740static struct value *
9741value_of_arm_user_reg (struct frame_info *frame, const void *baton)
9742{
9743 const int *reg_p = baton;
9744 return value_of_register (*reg_p, frame);
9745}
97e03143 9746\f
70f80edf
JT
9747static enum gdb_osabi
9748arm_elf_osabi_sniffer (bfd *abfd)
97e03143 9749{
2af48f68 9750 unsigned int elfosabi;
70f80edf 9751 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
97e03143 9752
70f80edf 9753 elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
97e03143 9754
28e97307
DJ
9755 if (elfosabi == ELFOSABI_ARM)
9756 /* GNU tools use this value. Check note sections in this case,
9757 as well. */
9758 bfd_map_over_sections (abfd,
9759 generic_elf_osabi_sniff_abi_tag_sections,
9760 &osabi);
97e03143 9761
28e97307 9762 /* Anything else will be handled by the generic ELF sniffer. */
70f80edf 9763 return osabi;
97e03143
RE
9764}
9765
54483882
YQ
9766static int
9767arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
9768 struct reggroup *group)
9769{
2c291032
YQ
9770 /* FPS register's type is INT, but belongs to float_reggroup. Beside
9771 this, FPS register belongs to save_regroup, restore_reggroup, and
9772 all_reggroup, of course. */
54483882 9773 if (regnum == ARM_FPS_REGNUM)
2c291032
YQ
9774 return (group == float_reggroup
9775 || group == save_reggroup
9776 || group == restore_reggroup
9777 || group == all_reggroup);
54483882
YQ
9778 else
9779 return default_register_reggroup_p (gdbarch, regnum, group);
9780}
9781
25f8c692
JL
9782\f
9783/* For backward-compatibility we allow two 'g' packet lengths with
9784 the remote protocol depending on whether FPA registers are
9785 supplied. M-profile targets do not have FPA registers, but some
9786 stubs already exist in the wild which use a 'g' packet which
9787 supplies them albeit with dummy values. The packet format which
9788 includes FPA registers should be considered deprecated for
9789 M-profile targets. */
9790
9791static void
9792arm_register_g_packet_guesses (struct gdbarch *gdbarch)
9793{
9794 if (gdbarch_tdep (gdbarch)->is_m)
9795 {
9796 /* If we know from the executable this is an M-profile target,
9797 cater for remote targets whose register set layout is the
9798 same as the FPA layout. */
9799 register_remote_g_packet_guess (gdbarch,
03145bf4 9800 /* r0-r12,sp,lr,pc; f0-f7; fps,xpsr */
25f8c692
JL
9801 (16 * INT_REGISTER_SIZE)
9802 + (8 * FP_REGISTER_SIZE)
9803 + (2 * INT_REGISTER_SIZE),
9804 tdesc_arm_with_m_fpa_layout);
9805
9806 /* The regular M-profile layout. */
9807 register_remote_g_packet_guess (gdbarch,
9808 /* r0-r12,sp,lr,pc; xpsr */
9809 (16 * INT_REGISTER_SIZE)
9810 + INT_REGISTER_SIZE,
9811 tdesc_arm_with_m);
3184d3f9
JL
9812
9813 /* M-profile plus M4F VFP. */
9814 register_remote_g_packet_guess (gdbarch,
9815 /* r0-r12,sp,lr,pc; d0-d15; fpscr,xpsr */
9816 (16 * INT_REGISTER_SIZE)
9817 + (16 * VFP_REGISTER_SIZE)
9818 + (2 * INT_REGISTER_SIZE),
9819 tdesc_arm_with_m_vfp_d16);
25f8c692
JL
9820 }
9821
9822 /* Otherwise we don't have a useful guess. */
9823}
9824
70f80edf 9825\f
da3c6d4a
MS
9826/* Initialize the current architecture based on INFO. If possible,
9827 re-use an architecture from ARCHES, which is a list of
9828 architectures already created during this debugging session.
97e03143 9829
da3c6d4a
MS
9830 Called e.g. at program startup, when reading a core file, and when
9831 reading a binary file. */
97e03143 9832
39bbf761
RE
9833static struct gdbarch *
9834arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
9835{
97e03143 9836 struct gdbarch_tdep *tdep;
39bbf761 9837 struct gdbarch *gdbarch;
28e97307
DJ
9838 struct gdbarch_list *best_arch;
9839 enum arm_abi_kind arm_abi = arm_abi_global;
9840 enum arm_float_model fp_model = arm_fp_model;
123dc839 9841 struct tdesc_arch_data *tdesc_data = NULL;
9779414d 9842 int i, is_m = 0;
58d6951d
DJ
9843 int have_vfp_registers = 0, have_vfp_pseudos = 0, have_neon_pseudos = 0;
9844 int have_neon = 0;
ff6f572f 9845 int have_fpa_registers = 1;
9779414d
DJ
9846 const struct target_desc *tdesc = info.target_desc;
9847
9848 /* If we have an object to base this architecture on, try to determine
9849 its ABI. */
9850
9851 if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
9852 {
9853 int ei_osabi, e_flags;
9854
9855 switch (bfd_get_flavour (info.abfd))
9856 {
9857 case bfd_target_aout_flavour:
9858 /* Assume it's an old APCS-style ABI. */
9859 arm_abi = ARM_ABI_APCS;
9860 break;
9861
9862 case bfd_target_coff_flavour:
9863 /* Assume it's an old APCS-style ABI. */
9864 /* XXX WinCE? */
9865 arm_abi = ARM_ABI_APCS;
9866 break;
9867
9868 case bfd_target_elf_flavour:
9869 ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
9870 e_flags = elf_elfheader (info.abfd)->e_flags;
9871
9872 if (ei_osabi == ELFOSABI_ARM)
9873 {
9874 /* GNU tools used to use this value, but do not for EABI
9875 objects. There's nowhere to tag an EABI version
9876 anyway, so assume APCS. */
9877 arm_abi = ARM_ABI_APCS;
9878 }
9879 else if (ei_osabi == ELFOSABI_NONE)
9880 {
9881 int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
9882 int attr_arch, attr_profile;
9883
9884 switch (eabi_ver)
9885 {
9886 case EF_ARM_EABI_UNKNOWN:
9887 /* Assume GNU tools. */
9888 arm_abi = ARM_ABI_APCS;
9889 break;
9890
9891 case EF_ARM_EABI_VER4:
9892 case EF_ARM_EABI_VER5:
9893 arm_abi = ARM_ABI_AAPCS;
9894 /* EABI binaries default to VFP float ordering.
9895 They may also contain build attributes that can
9896 be used to identify if the VFP argument-passing
9897 ABI is in use. */
9898 if (fp_model == ARM_FLOAT_AUTO)
9899 {
9900#ifdef HAVE_ELF
9901 switch (bfd_elf_get_obj_attr_int (info.abfd,
9902 OBJ_ATTR_PROC,
9903 Tag_ABI_VFP_args))
9904 {
9905 case 0:
9906 /* "The user intended FP parameter/result
9907 passing to conform to AAPCS, base
9908 variant". */
9909 fp_model = ARM_FLOAT_SOFT_VFP;
9910 break;
9911 case 1:
9912 /* "The user intended FP parameter/result
9913 passing to conform to AAPCS, VFP
9914 variant". */
9915 fp_model = ARM_FLOAT_VFP;
9916 break;
9917 case 2:
9918 /* "The user intended FP parameter/result
9919 passing to conform to tool chain-specific
9920 conventions" - we don't know any such
9921 conventions, so leave it as "auto". */
9922 break;
9923 default:
9924 /* Attribute value not mentioned in the
9925 October 2008 ABI, so leave it as
9926 "auto". */
9927 break;
9928 }
9929#else
9930 fp_model = ARM_FLOAT_SOFT_VFP;
9931#endif
9932 }
9933 break;
9934
9935 default:
9936 /* Leave it as "auto". */
9937 warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
9938 break;
9939 }
9940
9941#ifdef HAVE_ELF
9942 /* Detect M-profile programs. This only works if the
9943 executable file includes build attributes; GCC does
9944 copy them to the executable, but e.g. RealView does
9945 not. */
9946 attr_arch = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9947 Tag_CPU_arch);
0963b4bd
MS
9948 attr_profile = bfd_elf_get_obj_attr_int (info.abfd,
9949 OBJ_ATTR_PROC,
9779414d
DJ
9950 Tag_CPU_arch_profile);
9951 /* GCC specifies the profile for v6-M; RealView only
9952 specifies the profile for architectures starting with
9953 V7 (as opposed to architectures with a tag
9954 numerically greater than TAG_CPU_ARCH_V7). */
9955 if (!tdesc_has_registers (tdesc)
9956 && (attr_arch == TAG_CPU_ARCH_V6_M
9957 || attr_arch == TAG_CPU_ARCH_V6S_M
9958 || attr_profile == 'M'))
25f8c692 9959 is_m = 1;
9779414d
DJ
9960#endif
9961 }
9962
9963 if (fp_model == ARM_FLOAT_AUTO)
9964 {
9965 int e_flags = elf_elfheader (info.abfd)->e_flags;
9966
9967 switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
9968 {
9969 case 0:
9970 /* Leave it as "auto". Strictly speaking this case
9971 means FPA, but almost nobody uses that now, and
9972 many toolchains fail to set the appropriate bits
9973 for the floating-point model they use. */
9974 break;
9975 case EF_ARM_SOFT_FLOAT:
9976 fp_model = ARM_FLOAT_SOFT_FPA;
9977 break;
9978 case EF_ARM_VFP_FLOAT:
9979 fp_model = ARM_FLOAT_VFP;
9980 break;
9981 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
9982 fp_model = ARM_FLOAT_SOFT_VFP;
9983 break;
9984 }
9985 }
9986
9987 if (e_flags & EF_ARM_BE8)
9988 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
9989
9990 break;
9991
9992 default:
9993 /* Leave it as "auto". */
9994 break;
9995 }
9996 }
123dc839
DJ
9997
9998 /* Check any target description for validity. */
9779414d 9999 if (tdesc_has_registers (tdesc))
123dc839
DJ
10000 {
10001 /* For most registers we require GDB's default names; but also allow
10002 the numeric names for sp / lr / pc, as a convenience. */
10003 static const char *const arm_sp_names[] = { "r13", "sp", NULL };
10004 static const char *const arm_lr_names[] = { "r14", "lr", NULL };
10005 static const char *const arm_pc_names[] = { "r15", "pc", NULL };
10006
10007 const struct tdesc_feature *feature;
58d6951d 10008 int valid_p;
123dc839 10009
9779414d 10010 feature = tdesc_find_feature (tdesc,
123dc839
DJ
10011 "org.gnu.gdb.arm.core");
10012 if (feature == NULL)
9779414d
DJ
10013 {
10014 feature = tdesc_find_feature (tdesc,
10015 "org.gnu.gdb.arm.m-profile");
10016 if (feature == NULL)
10017 return NULL;
10018 else
10019 is_m = 1;
10020 }
123dc839
DJ
10021
10022 tdesc_data = tdesc_data_alloc ();
10023
10024 valid_p = 1;
10025 for (i = 0; i < ARM_SP_REGNUM; i++)
10026 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
10027 arm_register_names[i]);
10028 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
10029 ARM_SP_REGNUM,
10030 arm_sp_names);
10031 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
10032 ARM_LR_REGNUM,
10033 arm_lr_names);
10034 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
10035 ARM_PC_REGNUM,
10036 arm_pc_names);
9779414d
DJ
10037 if (is_m)
10038 valid_p &= tdesc_numbered_register (feature, tdesc_data,
10039 ARM_PS_REGNUM, "xpsr");
10040 else
10041 valid_p &= tdesc_numbered_register (feature, tdesc_data,
10042 ARM_PS_REGNUM, "cpsr");
123dc839
DJ
10043
10044 if (!valid_p)
10045 {
10046 tdesc_data_cleanup (tdesc_data);
10047 return NULL;
10048 }
10049
9779414d 10050 feature = tdesc_find_feature (tdesc,
123dc839
DJ
10051 "org.gnu.gdb.arm.fpa");
10052 if (feature != NULL)
10053 {
10054 valid_p = 1;
10055 for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
10056 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
10057 arm_register_names[i]);
10058 if (!valid_p)
10059 {
10060 tdesc_data_cleanup (tdesc_data);
10061 return NULL;
10062 }
10063 }
ff6f572f
DJ
10064 else
10065 have_fpa_registers = 0;
10066
9779414d 10067 feature = tdesc_find_feature (tdesc,
ff6f572f
DJ
10068 "org.gnu.gdb.xscale.iwmmxt");
10069 if (feature != NULL)
10070 {
10071 static const char *const iwmmxt_names[] = {
10072 "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
10073 "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
10074 "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
10075 "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
10076 };
10077
10078 valid_p = 1;
10079 for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
10080 valid_p
10081 &= tdesc_numbered_register (feature, tdesc_data, i,
10082 iwmmxt_names[i - ARM_WR0_REGNUM]);
10083
10084 /* Check for the control registers, but do not fail if they
10085 are missing. */
10086 for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
10087 tdesc_numbered_register (feature, tdesc_data, i,
10088 iwmmxt_names[i - ARM_WR0_REGNUM]);
10089
10090 for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
10091 valid_p
10092 &= tdesc_numbered_register (feature, tdesc_data, i,
10093 iwmmxt_names[i - ARM_WR0_REGNUM]);
10094
10095 if (!valid_p)
10096 {
10097 tdesc_data_cleanup (tdesc_data);
10098 return NULL;
10099 }
10100 }
58d6951d
DJ
10101
10102 /* If we have a VFP unit, check whether the single precision registers
10103 are present. If not, then we will synthesize them as pseudo
10104 registers. */
9779414d 10105 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
10106 "org.gnu.gdb.arm.vfp");
10107 if (feature != NULL)
10108 {
10109 static const char *const vfp_double_names[] = {
10110 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
10111 "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
10112 "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
10113 "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
10114 };
10115
10116 /* Require the double precision registers. There must be either
10117 16 or 32. */
10118 valid_p = 1;
10119 for (i = 0; i < 32; i++)
10120 {
10121 valid_p &= tdesc_numbered_register (feature, tdesc_data,
10122 ARM_D0_REGNUM + i,
10123 vfp_double_names[i]);
10124 if (!valid_p)
10125 break;
10126 }
2b9e5ea6
UW
10127 if (!valid_p && i == 16)
10128 valid_p = 1;
58d6951d 10129
2b9e5ea6
UW
10130 /* Also require FPSCR. */
10131 valid_p &= tdesc_numbered_register (feature, tdesc_data,
10132 ARM_FPSCR_REGNUM, "fpscr");
10133 if (!valid_p)
58d6951d
DJ
10134 {
10135 tdesc_data_cleanup (tdesc_data);
10136 return NULL;
10137 }
10138
10139 if (tdesc_unnumbered_register (feature, "s0") == 0)
10140 have_vfp_pseudos = 1;
10141
10142 have_vfp_registers = 1;
10143
10144 /* If we have VFP, also check for NEON. The architecture allows
10145 NEON without VFP (integer vector operations only), but GDB
10146 does not support that. */
9779414d 10147 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
10148 "org.gnu.gdb.arm.neon");
10149 if (feature != NULL)
10150 {
10151 /* NEON requires 32 double-precision registers. */
10152 if (i != 32)
10153 {
10154 tdesc_data_cleanup (tdesc_data);
10155 return NULL;
10156 }
10157
10158 /* If there are quad registers defined by the stub, use
10159 their type; otherwise (normally) provide them with
10160 the default type. */
10161 if (tdesc_unnumbered_register (feature, "q0") == 0)
10162 have_neon_pseudos = 1;
10163
10164 have_neon = 1;
10165 }
10166 }
123dc839 10167 }
39bbf761 10168
28e97307
DJ
10169 /* If there is already a candidate, use it. */
10170 for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
10171 best_arch != NULL;
10172 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
10173 {
b8926edc
DJ
10174 if (arm_abi != ARM_ABI_AUTO
10175 && arm_abi != gdbarch_tdep (best_arch->gdbarch)->arm_abi)
28e97307
DJ
10176 continue;
10177
b8926edc
DJ
10178 if (fp_model != ARM_FLOAT_AUTO
10179 && fp_model != gdbarch_tdep (best_arch->gdbarch)->fp_model)
28e97307
DJ
10180 continue;
10181
58d6951d
DJ
10182 /* There are various other properties in tdep that we do not
10183 need to check here: those derived from a target description,
10184 since gdbarches with a different target description are
10185 automatically disqualified. */
10186
9779414d
DJ
10187 /* Do check is_m, though, since it might come from the binary. */
10188 if (is_m != gdbarch_tdep (best_arch->gdbarch)->is_m)
10189 continue;
10190
28e97307
DJ
10191 /* Found a match. */
10192 break;
10193 }
97e03143 10194
28e97307 10195 if (best_arch != NULL)
123dc839
DJ
10196 {
10197 if (tdesc_data != NULL)
10198 tdesc_data_cleanup (tdesc_data);
10199 return best_arch->gdbarch;
10200 }
28e97307
DJ
10201
10202 tdep = xcalloc (1, sizeof (struct gdbarch_tdep));
97e03143
RE
10203 gdbarch = gdbarch_alloc (&info, tdep);
10204
28e97307
DJ
10205 /* Record additional information about the architecture we are defining.
10206 These are gdbarch discriminators, like the OSABI. */
10207 tdep->arm_abi = arm_abi;
10208 tdep->fp_model = fp_model;
9779414d 10209 tdep->is_m = is_m;
ff6f572f 10210 tdep->have_fpa_registers = have_fpa_registers;
58d6951d
DJ
10211 tdep->have_vfp_registers = have_vfp_registers;
10212 tdep->have_vfp_pseudos = have_vfp_pseudos;
10213 tdep->have_neon_pseudos = have_neon_pseudos;
10214 tdep->have_neon = have_neon;
08216dd7 10215
25f8c692
JL
10216 arm_register_g_packet_guesses (gdbarch);
10217
08216dd7 10218 /* Breakpoints. */
9d4fde75 10219 switch (info.byte_order_for_code)
67255d04
RE
10220 {
10221 case BFD_ENDIAN_BIG:
66e810cd
RE
10222 tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
10223 tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
10224 tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
10225 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
10226
67255d04
RE
10227 break;
10228
10229 case BFD_ENDIAN_LITTLE:
66e810cd
RE
10230 tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
10231 tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
10232 tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
10233 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
10234
67255d04
RE
10235 break;
10236
10237 default:
10238 internal_error (__FILE__, __LINE__,
edefbb7c 10239 _("arm_gdbarch_init: bad byte order for float format"));
67255d04
RE
10240 }
10241
d7b486e7
RE
10242 /* On ARM targets char defaults to unsigned. */
10243 set_gdbarch_char_signed (gdbarch, 0);
10244
cca44b1b
JB
10245 /* Note: for displaced stepping, this includes the breakpoint, and one word
10246 of additional scratch space. This setting isn't used for anything beside
10247 displaced stepping at present. */
10248 set_gdbarch_max_insn_length (gdbarch, 4 * DISPLACED_MODIFIED_INSNS);
10249
9df628e0 10250 /* This should be low enough for everything. */
97e03143 10251 tdep->lowest_pc = 0x20;
94c30b78 10252 tdep->jb_pc = -1; /* Longjump support not enabled by default. */
97e03143 10253
7c00367c
MK
10254 /* The default, for both APCS and AAPCS, is to return small
10255 structures in registers. */
10256 tdep->struct_return = reg_struct_return;
10257
2dd604e7 10258 set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
f53f0d0b 10259 set_gdbarch_frame_align (gdbarch, arm_frame_align);
39bbf761 10260
756fe439
DJ
10261 set_gdbarch_write_pc (gdbarch, arm_write_pc);
10262
148754e5 10263 /* Frame handling. */
a262aec2 10264 set_gdbarch_dummy_id (gdbarch, arm_dummy_id);
eb5492fa
DJ
10265 set_gdbarch_unwind_pc (gdbarch, arm_unwind_pc);
10266 set_gdbarch_unwind_sp (gdbarch, arm_unwind_sp);
10267
eb5492fa 10268 frame_base_set_default (gdbarch, &arm_normal_base);
148754e5 10269
34e8f22d 10270 /* Address manipulation. */
34e8f22d
RE
10271 set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
10272
34e8f22d
RE
10273 /* Advance PC across function entry code. */
10274 set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
10275
4024ca99
UW
10276 /* Detect whether PC is in function epilogue. */
10277 set_gdbarch_in_function_epilogue_p (gdbarch, arm_in_function_epilogue_p);
10278
190dce09
UW
10279 /* Skip trampolines. */
10280 set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
10281
34e8f22d
RE
10282 /* The stack grows downward. */
10283 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
10284
10285 /* Breakpoint manipulation. */
10286 set_gdbarch_breakpoint_from_pc (gdbarch, arm_breakpoint_from_pc);
177321bd
DJ
10287 set_gdbarch_remote_breakpoint_from_pc (gdbarch,
10288 arm_remote_breakpoint_from_pc);
34e8f22d
RE
10289
10290 /* Information about registers, etc. */
34e8f22d
RE
10291 set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
10292 set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
ff6f572f 10293 set_gdbarch_num_regs (gdbarch, ARM_NUM_REGS);
7a5ea0d4 10294 set_gdbarch_register_type (gdbarch, arm_register_type);
54483882 10295 set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);
34e8f22d 10296
ff6f572f
DJ
10297 /* This "info float" is FPA-specific. Use the generic version if we
10298 do not have FPA. */
10299 if (gdbarch_tdep (gdbarch)->have_fpa_registers)
10300 set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
10301
26216b98 10302 /* Internal <-> external register number maps. */
ff6f572f 10303 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
26216b98
AC
10304 set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
10305
34e8f22d
RE
10306 set_gdbarch_register_name (gdbarch, arm_register_name);
10307
10308 /* Returning results. */
2af48f68 10309 set_gdbarch_return_value (gdbarch, arm_return_value);
34e8f22d 10310
03d48a7d
RE
10311 /* Disassembly. */
10312 set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
10313
34e8f22d
RE
10314 /* Minsymbol frobbing. */
10315 set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
10316 set_gdbarch_coff_make_msymbol_special (gdbarch,
10317 arm_coff_make_msymbol_special);
60c5725c 10318 set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
34e8f22d 10319
f9d67f43
DJ
10320 /* Thumb-2 IT block support. */
10321 set_gdbarch_adjust_breakpoint_address (gdbarch,
10322 arm_adjust_breakpoint_address);
10323
0d5de010
DJ
10324 /* Virtual tables. */
10325 set_gdbarch_vbit_in_delta (gdbarch, 1);
10326
97e03143 10327 /* Hook in the ABI-specific overrides, if they have been registered. */
4be87837 10328 gdbarch_init_osabi (info, gdbarch);
97e03143 10329
b39cc962
DJ
10330 dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
10331
eb5492fa 10332 /* Add some default predicates. */
2ae28aa9
YQ
10333 if (is_m)
10334 frame_unwind_append_unwinder (gdbarch, &arm_m_exception_unwind);
a262aec2
DJ
10335 frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
10336 dwarf2_append_unwinders (gdbarch);
0e9e9abd 10337 frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
a262aec2 10338 frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
eb5492fa 10339
97e03143
RE
10340 /* Now we have tuned the configuration, set a few final things,
10341 based on what the OS ABI has told us. */
10342
b8926edc
DJ
10343 /* If the ABI is not otherwise marked, assume the old GNU APCS. EABI
10344 binaries are always marked. */
10345 if (tdep->arm_abi == ARM_ABI_AUTO)
10346 tdep->arm_abi = ARM_ABI_APCS;
10347
e3039479
UW
10348 /* Watchpoints are not steppable. */
10349 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
10350
b8926edc
DJ
10351 /* We used to default to FPA for generic ARM, but almost nobody
10352 uses that now, and we now provide a way for the user to force
10353 the model. So default to the most useful variant. */
10354 if (tdep->fp_model == ARM_FLOAT_AUTO)
10355 tdep->fp_model = ARM_FLOAT_SOFT_FPA;
10356
9df628e0
RE
10357 if (tdep->jb_pc >= 0)
10358 set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
10359
08216dd7 10360 /* Floating point sizes and format. */
8da61cc4 10361 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
b8926edc 10362 if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
08216dd7 10363 {
8da61cc4
DJ
10364 set_gdbarch_double_format
10365 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10366 set_gdbarch_long_double_format
10367 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10368 }
10369 else
10370 {
10371 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
10372 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
08216dd7
RE
10373 }
10374
58d6951d
DJ
10375 if (have_vfp_pseudos)
10376 {
10377 /* NOTE: These are the only pseudo registers used by
10378 the ARM target at the moment. If more are added, a
10379 little more care in numbering will be needed. */
10380
10381 int num_pseudos = 32;
10382 if (have_neon_pseudos)
10383 num_pseudos += 16;
10384 set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
10385 set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
10386 set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
10387 }
10388
123dc839 10389 if (tdesc_data)
58d6951d
DJ
10390 {
10391 set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
10392
9779414d 10393 tdesc_use_registers (gdbarch, tdesc, tdesc_data);
58d6951d
DJ
10394
10395 /* Override tdesc_register_type to adjust the types of VFP
10396 registers for NEON. */
10397 set_gdbarch_register_type (gdbarch, arm_register_type);
10398 }
123dc839
DJ
10399
10400 /* Add standard register aliases. We add aliases even for those
10401 nanes which are used by the current architecture - it's simpler,
10402 and does no harm, since nothing ever lists user registers. */
10403 for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
10404 user_reg_add (gdbarch, arm_register_aliases[i].name,
10405 value_of_arm_user_reg, &arm_register_aliases[i].regnum);
10406
39bbf761
RE
10407 return gdbarch;
10408}
10409
97e03143 10410static void
2af46ca0 10411arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
97e03143 10412{
2af46ca0 10413 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
97e03143
RE
10414
10415 if (tdep == NULL)
10416 return;
10417
edefbb7c 10418 fprintf_unfiltered (file, _("arm_dump_tdep: Lowest pc = 0x%lx"),
97e03143
RE
10419 (unsigned long) tdep->lowest_pc);
10420}
10421
a78f21af
AC
10422extern initialize_file_ftype _initialize_arm_tdep; /* -Wmissing-prototypes */
10423
c906108c 10424void
ed9a39eb 10425_initialize_arm_tdep (void)
c906108c 10426{
bc90b915
FN
10427 struct ui_file *stb;
10428 long length;
26304000 10429 struct cmd_list_element *new_set, *new_show;
53904c9e
AC
10430 const char *setname;
10431 const char *setdesc;
4bd7b427 10432 const char *const *regnames;
bc90b915
FN
10433 int numregs, i, j;
10434 static char *helptext;
edefbb7c
AC
10435 char regdesc[1024], *rdptr = regdesc;
10436 size_t rest = sizeof (regdesc);
085dd6e6 10437
42cf1509 10438 gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
97e03143 10439
60c5725c 10440 arm_objfile_data_key
c1bd65d0 10441 = register_objfile_data_with_cleanup (NULL, arm_objfile_data_free);
60c5725c 10442
0e9e9abd
UW
10443 /* Add ourselves to objfile event chain. */
10444 observer_attach_new_objfile (arm_exidx_new_objfile);
10445 arm_exidx_data_key
10446 = register_objfile_data_with_cleanup (NULL, arm_exidx_data_free);
10447
70f80edf
JT
10448 /* Register an ELF OS ABI sniffer for ARM binaries. */
10449 gdbarch_register_osabi_sniffer (bfd_arch_arm,
10450 bfd_target_elf_flavour,
10451 arm_elf_osabi_sniffer);
10452
9779414d
DJ
10453 /* Initialize the standard target descriptions. */
10454 initialize_tdesc_arm_with_m ();
25f8c692 10455 initialize_tdesc_arm_with_m_fpa_layout ();
3184d3f9 10456 initialize_tdesc_arm_with_m_vfp_d16 ();
ef7e8358
UW
10457 initialize_tdesc_arm_with_iwmmxt ();
10458 initialize_tdesc_arm_with_vfpv2 ();
10459 initialize_tdesc_arm_with_vfpv3 ();
10460 initialize_tdesc_arm_with_neon ();
9779414d 10461
94c30b78 10462 /* Get the number of possible sets of register names defined in opcodes. */
afd7eef0
RE
10463 num_disassembly_options = get_arm_regname_num_options ();
10464
10465 /* Add root prefix command for all "set arm"/"show arm" commands. */
10466 add_prefix_cmd ("arm", no_class, set_arm_command,
edefbb7c 10467 _("Various ARM-specific commands."),
afd7eef0
RE
10468 &setarmcmdlist, "set arm ", 0, &setlist);
10469
10470 add_prefix_cmd ("arm", no_class, show_arm_command,
edefbb7c 10471 _("Various ARM-specific commands."),
afd7eef0 10472 &showarmcmdlist, "show arm ", 0, &showlist);
bc90b915 10473
94c30b78 10474 /* Sync the opcode insn printer with our register viewer. */
bc90b915 10475 parse_arm_disassembler_option ("reg-names-std");
c5aa993b 10476
eefe576e
AC
10477 /* Initialize the array that will be passed to
10478 add_setshow_enum_cmd(). */
afd7eef0
RE
10479 valid_disassembly_styles
10480 = xmalloc ((num_disassembly_options + 1) * sizeof (char *));
10481 for (i = 0; i < num_disassembly_options; i++)
bc90b915
FN
10482 {
10483 numregs = get_arm_regnames (i, &setname, &setdesc, &regnames);
afd7eef0 10484 valid_disassembly_styles[i] = setname;
edefbb7c
AC
10485 length = snprintf (rdptr, rest, "%s - %s\n", setname, setdesc);
10486 rdptr += length;
10487 rest -= length;
123dc839
DJ
10488 /* When we find the default names, tell the disassembler to use
10489 them. */
bc90b915
FN
10490 if (!strcmp (setname, "std"))
10491 {
afd7eef0 10492 disassembly_style = setname;
bc90b915
FN
10493 set_arm_regname_option (i);
10494 }
10495 }
94c30b78 10496 /* Mark the end of valid options. */
afd7eef0 10497 valid_disassembly_styles[num_disassembly_options] = NULL;
c906108c 10498
edefbb7c
AC
10499 /* Create the help text. */
10500 stb = mem_fileopen ();
10501 fprintf_unfiltered (stb, "%s%s%s",
10502 _("The valid values are:\n"),
10503 regdesc,
10504 _("The default is \"std\"."));
759ef836 10505 helptext = ui_file_xstrdup (stb, NULL);
bc90b915 10506 ui_file_delete (stb);
ed9a39eb 10507
edefbb7c
AC
10508 add_setshow_enum_cmd("disassembler", no_class,
10509 valid_disassembly_styles, &disassembly_style,
10510 _("Set the disassembly style."),
10511 _("Show the disassembly style."),
10512 helptext,
2c5b56ce 10513 set_disassembly_style_sfunc,
0963b4bd
MS
10514 NULL, /* FIXME: i18n: The disassembly style is
10515 \"%s\". */
7376b4c2 10516 &setarmcmdlist, &showarmcmdlist);
edefbb7c
AC
10517
10518 add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
10519 _("Set usage of ARM 32-bit mode."),
10520 _("Show usage of ARM 32-bit mode."),
10521 _("When off, a 26-bit PC will be used."),
2c5b56ce 10522 NULL,
0963b4bd
MS
10523 NULL, /* FIXME: i18n: Usage of ARM 32-bit
10524 mode is %s. */
26304000 10525 &setarmcmdlist, &showarmcmdlist);
c906108c 10526
fd50bc42 10527 /* Add a command to allow the user to force the FPU model. */
edefbb7c
AC
10528 add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
10529 _("Set the floating point type."),
10530 _("Show the floating point type."),
10531 _("auto - Determine the FP typefrom the OS-ABI.\n\
10532softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
10533fpa - FPA co-processor (GCC compiled).\n\
10534softvfp - Software FP with pure-endian doubles.\n\
10535vfp - VFP co-processor."),
edefbb7c 10536 set_fp_model_sfunc, show_fp_model,
7376b4c2 10537 &setarmcmdlist, &showarmcmdlist);
fd50bc42 10538
28e97307
DJ
10539 /* Add a command to allow the user to force the ABI. */
10540 add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
10541 _("Set the ABI."),
10542 _("Show the ABI."),
10543 NULL, arm_set_abi, arm_show_abi,
10544 &setarmcmdlist, &showarmcmdlist);
10545
0428b8f5
DJ
10546 /* Add two commands to allow the user to force the assumed
10547 execution mode. */
10548 add_setshow_enum_cmd ("fallback-mode", class_support,
10549 arm_mode_strings, &arm_fallback_mode_string,
10550 _("Set the mode assumed when symbols are unavailable."),
10551 _("Show the mode assumed when symbols are unavailable."),
10552 NULL, NULL, arm_show_fallback_mode,
10553 &setarmcmdlist, &showarmcmdlist);
10554 add_setshow_enum_cmd ("force-mode", class_support,
10555 arm_mode_strings, &arm_force_mode_string,
10556 _("Set the mode assumed even when symbols are available."),
10557 _("Show the mode assumed even when symbols are available."),
10558 NULL, NULL, arm_show_force_mode,
10559 &setarmcmdlist, &showarmcmdlist);
10560
6529d2dd 10561 /* Debugging flag. */
edefbb7c
AC
10562 add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
10563 _("Set ARM debugging."),
10564 _("Show ARM debugging."),
10565 _("When on, arm-specific debugging is enabled."),
2c5b56ce 10566 NULL,
7915a72c 10567 NULL, /* FIXME: i18n: "ARM debugging is %s. */
26304000 10568 &setdebuglist, &showdebuglist);
c906108c 10569}
72508ac0
PO
10570
10571/* ARM-reversible process record data structures. */
10572
10573#define ARM_INSN_SIZE_BYTES 4
10574#define THUMB_INSN_SIZE_BYTES 2
10575#define THUMB2_INSN_SIZE_BYTES 4
10576
10577
10578#define INSN_S_L_BIT_NUM 20
10579
10580#define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
10581 do \
10582 { \
10583 unsigned int reg_len = LENGTH; \
10584 if (reg_len) \
10585 { \
10586 REGS = XNEWVEC (uint32_t, reg_len); \
10587 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
10588 } \
10589 } \
10590 while (0)
10591
10592#define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
10593 do \
10594 { \
10595 unsigned int mem_len = LENGTH; \
10596 if (mem_len) \
10597 { \
10598 MEMS = XNEWVEC (struct arm_mem_r, mem_len); \
10599 memcpy(&MEMS->len, &RECORD_BUF[0], \
10600 sizeof(struct arm_mem_r) * LENGTH); \
10601 } \
10602 } \
10603 while (0)
10604
10605/* Checks whether insn is already recorded or yet to be decoded. (boolean expression). */
10606#define INSN_RECORDED(ARM_RECORD) \
10607 (0 != (ARM_RECORD)->reg_rec_count || 0 != (ARM_RECORD)->mem_rec_count)
10608
10609/* ARM memory record structure. */
10610struct arm_mem_r
10611{
10612 uint32_t len; /* Record length. */
10613 CORE_ADDR addr; /* Memory address. */
10614};
10615
10616/* ARM instruction record contains opcode of current insn
10617 and execution state (before entry to decode_insn()),
10618 contains list of to-be-modified registers and
10619 memory blocks (on return from decode_insn()). */
10620
10621typedef struct insn_decode_record_t
10622{
10623 struct gdbarch *gdbarch;
10624 struct regcache *regcache;
10625 CORE_ADDR this_addr; /* Address of the insn being decoded. */
10626 uint32_t arm_insn; /* Should accommodate thumb. */
10627 uint32_t cond; /* Condition code. */
10628 uint32_t opcode; /* Insn opcode. */
10629 uint32_t decode; /* Insn decode bits. */
10630 uint32_t mem_rec_count; /* No of mem records. */
10631 uint32_t reg_rec_count; /* No of reg records. */
10632 uint32_t *arm_regs; /* Registers to be saved for this record. */
10633 struct arm_mem_r *arm_mems; /* Memory to be saved for this record. */
10634} insn_decode_record;
10635
10636
10637/* Checks ARM SBZ and SBO mandatory fields. */
10638
10639static int
10640sbo_sbz (uint32_t insn, uint32_t bit_num, uint32_t len, uint32_t sbo)
10641{
10642 uint32_t ones = bits (insn, bit_num - 1, (bit_num -1) + (len - 1));
10643
10644 if (!len)
10645 return 1;
10646
10647 if (!sbo)
10648 ones = ~ones;
10649
10650 while (ones)
10651 {
10652 if (!(ones & sbo))
10653 {
10654 return 0;
10655 }
10656 ones = ones >> 1;
10657 }
10658 return 1;
10659}
10660
10661typedef enum
10662{
10663 ARM_RECORD_STRH=1,
10664 ARM_RECORD_STRD
10665} arm_record_strx_t;
10666
10667typedef enum
10668{
10669 ARM_RECORD=1,
10670 THUMB_RECORD,
10671 THUMB2_RECORD
10672} record_type_t;
10673
10674
10675static int
10676arm_record_strx (insn_decode_record *arm_insn_r, uint32_t *record_buf,
10677 uint32_t *record_buf_mem, arm_record_strx_t str_type)
10678{
10679
10680 struct regcache *reg_cache = arm_insn_r->regcache;
10681 ULONGEST u_regval[2]= {0};
10682
10683 uint32_t reg_src1 = 0, reg_src2 = 0;
10684 uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
10685 uint32_t opcode1 = 0;
10686
10687 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10688 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10689 opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
10690
10691
10692 if (14 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
10693 {
10694 /* 1) Handle misc store, immediate offset. */
10695 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
10696 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
10697 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10698 regcache_raw_read_unsigned (reg_cache, reg_src1,
10699 &u_regval[0]);
10700 if (ARM_PC_REGNUM == reg_src1)
10701 {
10702 /* If R15 was used as Rn, hence current PC+8. */
10703 u_regval[0] = u_regval[0] + 8;
10704 }
10705 offset_8 = (immed_high << 4) | immed_low;
10706 /* Calculate target store address. */
10707 if (14 == arm_insn_r->opcode)
10708 {
10709 tgt_mem_addr = u_regval[0] + offset_8;
10710 }
10711 else
10712 {
10713 tgt_mem_addr = u_regval[0] - offset_8;
10714 }
10715 if (ARM_RECORD_STRH == str_type)
10716 {
10717 record_buf_mem[0] = 2;
10718 record_buf_mem[1] = tgt_mem_addr;
10719 arm_insn_r->mem_rec_count = 1;
10720 }
10721 else if (ARM_RECORD_STRD == str_type)
10722 {
10723 record_buf_mem[0] = 4;
10724 record_buf_mem[1] = tgt_mem_addr;
10725 record_buf_mem[2] = 4;
10726 record_buf_mem[3] = tgt_mem_addr + 4;
10727 arm_insn_r->mem_rec_count = 2;
10728 }
10729 }
10730 else if (12 == arm_insn_r->opcode || 8 == arm_insn_r->opcode)
10731 {
10732 /* 2) Store, register offset. */
10733 /* Get Rm. */
10734 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10735 /* Get Rn. */
10736 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10737 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10738 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10739 if (15 == reg_src2)
10740 {
10741 /* If R15 was used as Rn, hence current PC+8. */
10742 u_regval[0] = u_regval[0] + 8;
10743 }
10744 /* Calculate target store address, Rn +/- Rm, register offset. */
10745 if (12 == arm_insn_r->opcode)
10746 {
10747 tgt_mem_addr = u_regval[0] + u_regval[1];
10748 }
10749 else
10750 {
10751 tgt_mem_addr = u_regval[1] - u_regval[0];
10752 }
10753 if (ARM_RECORD_STRH == str_type)
10754 {
10755 record_buf_mem[0] = 2;
10756 record_buf_mem[1] = tgt_mem_addr;
10757 arm_insn_r->mem_rec_count = 1;
10758 }
10759 else if (ARM_RECORD_STRD == str_type)
10760 {
10761 record_buf_mem[0] = 4;
10762 record_buf_mem[1] = tgt_mem_addr;
10763 record_buf_mem[2] = 4;
10764 record_buf_mem[3] = tgt_mem_addr + 4;
10765 arm_insn_r->mem_rec_count = 2;
10766 }
10767 }
10768 else if (11 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
10769 || 2 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
10770 {
10771 /* 3) Store, immediate pre-indexed. */
10772 /* 5) Store, immediate post-indexed. */
10773 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
10774 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
10775 offset_8 = (immed_high << 4) | immed_low;
10776 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10777 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10778 /* Calculate target store address, Rn +/- Rm, register offset. */
10779 if (15 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
10780 {
10781 tgt_mem_addr = u_regval[0] + offset_8;
10782 }
10783 else
10784 {
10785 tgt_mem_addr = u_regval[0] - offset_8;
10786 }
10787 if (ARM_RECORD_STRH == str_type)
10788 {
10789 record_buf_mem[0] = 2;
10790 record_buf_mem[1] = tgt_mem_addr;
10791 arm_insn_r->mem_rec_count = 1;
10792 }
10793 else if (ARM_RECORD_STRD == str_type)
10794 {
10795 record_buf_mem[0] = 4;
10796 record_buf_mem[1] = tgt_mem_addr;
10797 record_buf_mem[2] = 4;
10798 record_buf_mem[3] = tgt_mem_addr + 4;
10799 arm_insn_r->mem_rec_count = 2;
10800 }
10801 /* Record Rn also as it changes. */
10802 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
10803 arm_insn_r->reg_rec_count = 1;
10804 }
10805 else if (9 == arm_insn_r->opcode || 13 == arm_insn_r->opcode
10806 || 0 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
10807 {
10808 /* 4) Store, register pre-indexed. */
10809 /* 6) Store, register post -indexed. */
10810 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10811 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10812 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10813 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10814 /* Calculate target store address, Rn +/- Rm, register offset. */
10815 if (13 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
10816 {
10817 tgt_mem_addr = u_regval[0] + u_regval[1];
10818 }
10819 else
10820 {
10821 tgt_mem_addr = u_regval[1] - u_regval[0];
10822 }
10823 if (ARM_RECORD_STRH == str_type)
10824 {
10825 record_buf_mem[0] = 2;
10826 record_buf_mem[1] = tgt_mem_addr;
10827 arm_insn_r->mem_rec_count = 1;
10828 }
10829 else if (ARM_RECORD_STRD == str_type)
10830 {
10831 record_buf_mem[0] = 4;
10832 record_buf_mem[1] = tgt_mem_addr;
10833 record_buf_mem[2] = 4;
10834 record_buf_mem[3] = tgt_mem_addr + 4;
10835 arm_insn_r->mem_rec_count = 2;
10836 }
10837 /* Record Rn also as it changes. */
10838 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
10839 arm_insn_r->reg_rec_count = 1;
10840 }
10841 return 0;
10842}
10843
10844/* Handling ARM extension space insns. */
10845
10846static int
10847arm_record_extension_space (insn_decode_record *arm_insn_r)
10848{
10849 uint32_t ret = 0; /* Return value: -1:record failure ; 0:success */
10850 uint32_t opcode1 = 0, opcode2 = 0, insn_op1 = 0;
10851 uint32_t record_buf[8], record_buf_mem[8];
10852 uint32_t reg_src1 = 0;
10853 uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
10854 struct regcache *reg_cache = arm_insn_r->regcache;
10855 ULONGEST u_regval = 0;
10856
10857 gdb_assert (!INSN_RECORDED(arm_insn_r));
10858 /* Handle unconditional insn extension space. */
10859
10860 opcode1 = bits (arm_insn_r->arm_insn, 20, 27);
10861 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
10862 if (arm_insn_r->cond)
10863 {
10864 /* PLD has no affect on architectural state, it just affects
10865 the caches. */
10866 if (5 == ((opcode1 & 0xE0) >> 5))
10867 {
10868 /* BLX(1) */
10869 record_buf[0] = ARM_PS_REGNUM;
10870 record_buf[1] = ARM_LR_REGNUM;
10871 arm_insn_r->reg_rec_count = 2;
10872 }
10873 /* STC2, LDC2, MCR2, MRC2, CDP2: <TBD>, co-processor insn. */
10874 }
10875
10876
10877 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
10878 if (3 == opcode1 && bit (arm_insn_r->arm_insn, 4))
10879 {
10880 ret = -1;
10881 /* Undefined instruction on ARM V5; need to handle if later
10882 versions define it. */
10883 }
10884
10885 opcode1 = bits (arm_insn_r->arm_insn, 24, 27);
10886 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
10887 insn_op1 = bits (arm_insn_r->arm_insn, 20, 23);
10888
10889 /* Handle arithmetic insn extension space. */
10890 if (!opcode1 && 9 == opcode2 && 1 != arm_insn_r->cond
10891 && !INSN_RECORDED(arm_insn_r))
10892 {
10893 /* Handle MLA(S) and MUL(S). */
10894 if (0 <= insn_op1 && 3 >= insn_op1)
10895 {
10896 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10897 record_buf[1] = ARM_PS_REGNUM;
10898 arm_insn_r->reg_rec_count = 2;
10899 }
10900 else if (4 <= insn_op1 && 15 >= insn_op1)
10901 {
10902 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
10903 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
10904 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
10905 record_buf[2] = ARM_PS_REGNUM;
10906 arm_insn_r->reg_rec_count = 3;
10907 }
10908 }
10909
10910 opcode1 = bits (arm_insn_r->arm_insn, 26, 27);
10911 opcode2 = bits (arm_insn_r->arm_insn, 23, 24);
10912 insn_op1 = bits (arm_insn_r->arm_insn, 21, 22);
10913
10914 /* Handle control insn extension space. */
10915
10916 if (!opcode1 && 2 == opcode2 && !bit (arm_insn_r->arm_insn, 20)
10917 && 1 != arm_insn_r->cond && !INSN_RECORDED(arm_insn_r))
10918 {
10919 if (!bit (arm_insn_r->arm_insn,25))
10920 {
10921 if (!bits (arm_insn_r->arm_insn, 4, 7))
10922 {
10923 if ((0 == insn_op1) || (2 == insn_op1))
10924 {
10925 /* MRS. */
10926 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10927 arm_insn_r->reg_rec_count = 1;
10928 }
10929 else if (1 == insn_op1)
10930 {
10931 /* CSPR is going to be changed. */
10932 record_buf[0] = ARM_PS_REGNUM;
10933 arm_insn_r->reg_rec_count = 1;
10934 }
10935 else if (3 == insn_op1)
10936 {
10937 /* SPSR is going to be changed. */
10938 /* We need to get SPSR value, which is yet to be done. */
10939 printf_unfiltered (_("Process record does not support "
10940 "instruction 0x%0x at address %s.\n"),
10941 arm_insn_r->arm_insn,
10942 paddress (arm_insn_r->gdbarch,
10943 arm_insn_r->this_addr));
10944 return -1;
10945 }
10946 }
10947 else if (1 == bits (arm_insn_r->arm_insn, 4, 7))
10948 {
10949 if (1 == insn_op1)
10950 {
10951 /* BX. */
10952 record_buf[0] = ARM_PS_REGNUM;
10953 arm_insn_r->reg_rec_count = 1;
10954 }
10955 else if (3 == insn_op1)
10956 {
10957 /* CLZ. */
10958 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10959 arm_insn_r->reg_rec_count = 1;
10960 }
10961 }
10962 else if (3 == bits (arm_insn_r->arm_insn, 4, 7))
10963 {
10964 /* BLX. */
10965 record_buf[0] = ARM_PS_REGNUM;
10966 record_buf[1] = ARM_LR_REGNUM;
10967 arm_insn_r->reg_rec_count = 2;
10968 }
10969 else if (5 == bits (arm_insn_r->arm_insn, 4, 7))
10970 {
10971 /* QADD, QSUB, QDADD, QDSUB */
10972 record_buf[0] = ARM_PS_REGNUM;
10973 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
10974 arm_insn_r->reg_rec_count = 2;
10975 }
10976 else if (7 == bits (arm_insn_r->arm_insn, 4, 7))
10977 {
10978 /* BKPT. */
10979 record_buf[0] = ARM_PS_REGNUM;
10980 record_buf[1] = ARM_LR_REGNUM;
10981 arm_insn_r->reg_rec_count = 2;
10982
10983 /* Save SPSR also;how? */
10984 printf_unfiltered (_("Process record does not support "
10985 "instruction 0x%0x at address %s.\n"),
10986 arm_insn_r->arm_insn,
10987 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
10988 return -1;
10989 }
10990 else if(8 == bits (arm_insn_r->arm_insn, 4, 7)
10991 || 10 == bits (arm_insn_r->arm_insn, 4, 7)
10992 || 12 == bits (arm_insn_r->arm_insn, 4, 7)
10993 || 14 == bits (arm_insn_r->arm_insn, 4, 7)
10994 )
10995 {
10996 if (0 == insn_op1 || 1 == insn_op1)
10997 {
10998 /* SMLA<x><y>, SMLAW<y>, SMULW<y>. */
10999 /* We dont do optimization for SMULW<y> where we
11000 need only Rd. */
11001 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11002 record_buf[1] = ARM_PS_REGNUM;
11003 arm_insn_r->reg_rec_count = 2;
11004 }
11005 else if (2 == insn_op1)
11006 {
11007 /* SMLAL<x><y>. */
11008 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11009 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
11010 arm_insn_r->reg_rec_count = 2;
11011 }
11012 else if (3 == insn_op1)
11013 {
11014 /* SMUL<x><y>. */
11015 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11016 arm_insn_r->reg_rec_count = 1;
11017 }
11018 }
11019 }
11020 else
11021 {
11022 /* MSR : immediate form. */
11023 if (1 == insn_op1)
11024 {
11025 /* CSPR is going to be changed. */
11026 record_buf[0] = ARM_PS_REGNUM;
11027 arm_insn_r->reg_rec_count = 1;
11028 }
11029 else if (3 == insn_op1)
11030 {
11031 /* SPSR is going to be changed. */
11032 /* we need to get SPSR value, which is yet to be done */
11033 printf_unfiltered (_("Process record does not support "
11034 "instruction 0x%0x at address %s.\n"),
11035 arm_insn_r->arm_insn,
11036 paddress (arm_insn_r->gdbarch,
11037 arm_insn_r->this_addr));
11038 return -1;
11039 }
11040 }
11041 }
11042
11043 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
11044 opcode2 = bits (arm_insn_r->arm_insn, 20, 24);
11045 insn_op1 = bits (arm_insn_r->arm_insn, 5, 6);
11046
11047 /* Handle load/store insn extension space. */
11048
11049 if (!opcode1 && bit (arm_insn_r->arm_insn, 7)
11050 && bit (arm_insn_r->arm_insn, 4) && 1 != arm_insn_r->cond
11051 && !INSN_RECORDED(arm_insn_r))
11052 {
11053 /* SWP/SWPB. */
11054 if (0 == insn_op1)
11055 {
11056 /* These insn, changes register and memory as well. */
11057 /* SWP or SWPB insn. */
11058 /* Get memory address given by Rn. */
11059 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11060 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11061 /* SWP insn ?, swaps word. */
11062 if (8 == arm_insn_r->opcode)
11063 {
11064 record_buf_mem[0] = 4;
11065 }
11066 else
11067 {
11068 /* SWPB insn, swaps only byte. */
11069 record_buf_mem[0] = 1;
11070 }
11071 record_buf_mem[1] = u_regval;
11072 arm_insn_r->mem_rec_count = 1;
11073 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11074 arm_insn_r->reg_rec_count = 1;
11075 }
11076 else if (1 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11077 {
11078 /* STRH. */
11079 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11080 ARM_RECORD_STRH);
11081 }
11082 else if (2 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11083 {
11084 /* LDRD. */
11085 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11086 record_buf[1] = record_buf[0] + 1;
11087 arm_insn_r->reg_rec_count = 2;
11088 }
11089 else if (3 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11090 {
11091 /* STRD. */
11092 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11093 ARM_RECORD_STRD);
11094 }
11095 else if (bit (arm_insn_r->arm_insn, 20) && insn_op1 <= 3)
11096 {
11097 /* LDRH, LDRSB, LDRSH. */
11098 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11099 arm_insn_r->reg_rec_count = 1;
11100 }
11101
11102 }
11103
11104 opcode1 = bits (arm_insn_r->arm_insn, 23, 27);
11105 if (24 == opcode1 && bit (arm_insn_r->arm_insn, 21)
11106 && !INSN_RECORDED(arm_insn_r))
11107 {
11108 ret = -1;
11109 /* Handle coprocessor insn extension space. */
11110 }
11111
11112 /* To be done for ARMv5 and later; as of now we return -1. */
11113 if (-1 == ret)
11114 printf_unfiltered (_("Process record does not support instruction x%0x "
11115 "at address %s.\n"),arm_insn_r->arm_insn,
11116 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11117
11118
11119 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11120 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11121
11122 return ret;
11123}
11124
11125/* Handling opcode 000 insns. */
11126
11127static int
11128arm_record_data_proc_misc_ld_str (insn_decode_record *arm_insn_r)
11129{
11130 struct regcache *reg_cache = arm_insn_r->regcache;
11131 uint32_t record_buf[8], record_buf_mem[8];
11132 ULONGEST u_regval[2] = {0};
11133
11134 uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
11135 uint32_t immed_high = 0, immed_low = 0, offset_8 = 0, tgt_mem_addr = 0;
11136 uint32_t opcode1 = 0;
11137
11138 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11139 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11140 opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
11141
11142 /* Data processing insn /multiply insn. */
11143 if (9 == arm_insn_r->decode
11144 && ((4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11145 || (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)))
11146 {
11147 /* Handle multiply instructions. */
11148 /* MLA, MUL, SMLAL, SMULL, UMLAL, UMULL. */
11149 if (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)
11150 {
11151 /* Handle MLA and MUL. */
11152 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11153 record_buf[1] = ARM_PS_REGNUM;
11154 arm_insn_r->reg_rec_count = 2;
11155 }
11156 else if (4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11157 {
11158 /* Handle SMLAL, SMULL, UMLAL, UMULL. */
11159 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11160 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11161 record_buf[2] = ARM_PS_REGNUM;
11162 arm_insn_r->reg_rec_count = 3;
11163 }
11164 }
11165 else if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11166 && (11 == arm_insn_r->decode || 13 == arm_insn_r->decode))
11167 {
11168 /* Handle misc load insns, as 20th bit (L = 1). */
11169 /* LDR insn has a capability to do branching, if
11170 MOV LR, PC is precceded by LDR insn having Rn as R15
11171 in that case, it emulates branch and link insn, and hence we
11172 need to save CSPR and PC as well. I am not sure this is right
11173 place; as opcode = 010 LDR insn make this happen, if R15 was
11174 used. */
11175 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11176 if (15 != reg_dest)
11177 {
11178 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11179 arm_insn_r->reg_rec_count = 1;
11180 }
11181 else
11182 {
11183 record_buf[0] = reg_dest;
11184 record_buf[1] = ARM_PS_REGNUM;
11185 arm_insn_r->reg_rec_count = 2;
11186 }
11187 }
11188 else if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11189 && sbo_sbz (arm_insn_r->arm_insn, 5, 12, 0)
11190 && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
11191 && 2 == bits (arm_insn_r->arm_insn, 20, 21))
11192 {
11193 /* Handle MSR insn. */
11194 if (9 == arm_insn_r->opcode)
11195 {
11196 /* CSPR is going to be changed. */
11197 record_buf[0] = ARM_PS_REGNUM;
11198 arm_insn_r->reg_rec_count = 1;
11199 }
11200 else
11201 {
11202 /* SPSR is going to be changed. */
11203 /* How to read SPSR value? */
11204 printf_unfiltered (_("Process record does not support instruction "
11205 "0x%0x at address %s.\n"),
11206 arm_insn_r->arm_insn,
11207 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11208 return -1;
11209 }
11210 }
11211 else if (9 == arm_insn_r->decode
11212 && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11213 && !bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11214 {
11215 /* Handling SWP, SWPB. */
11216 /* These insn, changes register and memory as well. */
11217 /* SWP or SWPB insn. */
11218
11219 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11220 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11221 /* SWP insn ?, swaps word. */
11222 if (8 == arm_insn_r->opcode)
11223 {
11224 record_buf_mem[0] = 4;
11225 }
11226 else
11227 {
11228 /* SWPB insn, swaps only byte. */
11229 record_buf_mem[0] = 1;
11230 }
11231 record_buf_mem[1] = u_regval[0];
11232 arm_insn_r->mem_rec_count = 1;
11233 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11234 arm_insn_r->reg_rec_count = 1;
11235 }
11236 else if (3 == arm_insn_r->decode && 0x12 == opcode1
11237 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11238 {
11239 /* Handle BLX, branch and link/exchange. */
11240 if (9 == arm_insn_r->opcode)
11241 {
11242 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm,
11243 and R14 stores the return address. */
11244 record_buf[0] = ARM_PS_REGNUM;
11245 record_buf[1] = ARM_LR_REGNUM;
11246 arm_insn_r->reg_rec_count = 2;
11247 }
11248 }
11249 else if (7 == arm_insn_r->decode && 0x12 == opcode1)
11250 {
11251 /* Handle enhanced software breakpoint insn, BKPT. */
11252 /* CPSR is changed to be executed in ARM state, disabling normal
11253 interrupts, entering abort mode. */
11254 /* According to high vector configuration PC is set. */
11255 /* user hit breakpoint and type reverse, in
11256 that case, we need to go back with previous CPSR and
11257 Program Counter. */
11258 record_buf[0] = ARM_PS_REGNUM;
11259 record_buf[1] = ARM_LR_REGNUM;
11260 arm_insn_r->reg_rec_count = 2;
11261
11262 /* Save SPSR also; how? */
11263 printf_unfiltered (_("Process record does not support instruction "
11264 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
11265 paddress (arm_insn_r->gdbarch,
11266 arm_insn_r->this_addr));
11267 return -1;
11268 }
11269 else if (11 == arm_insn_r->decode
11270 && !bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11271 {
11272 /* Handle enhanced store insns and DSP insns (e.g. LDRD). */
11273
11274 /* Handle str(x) insn */
11275 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11276 ARM_RECORD_STRH);
11277 }
11278 else if (1 == arm_insn_r->decode && 0x12 == opcode1
11279 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11280 {
11281 /* Handle BX, branch and link/exchange. */
11282 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm. */
11283 record_buf[0] = ARM_PS_REGNUM;
11284 arm_insn_r->reg_rec_count = 1;
11285 }
11286 else if (1 == arm_insn_r->decode && 0x16 == opcode1
11287 && sbo_sbz (arm_insn_r->arm_insn, 9, 4, 1)
11288 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1))
11289 {
11290 /* Count leading zeros: CLZ. */
11291 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11292 arm_insn_r->reg_rec_count = 1;
11293 }
11294 else if (!bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11295 && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11296 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1)
11297 && sbo_sbz (arm_insn_r->arm_insn, 1, 12, 0)
11298 )
11299 {
11300 /* Handle MRS insn. */
11301 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11302 arm_insn_r->reg_rec_count = 1;
11303 }
11304 else if (arm_insn_r->opcode <= 15)
11305 {
11306 /* Normal data processing insns. */
11307 /* Out of 11 shifter operands mode, all the insn modifies destination
11308 register, which is specified by 13-16 decode. */
11309 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11310 record_buf[1] = ARM_PS_REGNUM;
11311 arm_insn_r->reg_rec_count = 2;
11312 }
11313 else
11314 {
11315 return -1;
11316 }
11317
11318 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11319 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11320 return 0;
11321}
11322
11323/* Handling opcode 001 insns. */
11324
11325static int
11326arm_record_data_proc_imm (insn_decode_record *arm_insn_r)
11327{
11328 uint32_t record_buf[8], record_buf_mem[8];
11329
11330 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11331 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11332
11333 if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11334 && 2 == bits (arm_insn_r->arm_insn, 20, 21)
11335 && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
11336 )
11337 {
11338 /* Handle MSR insn. */
11339 if (9 == arm_insn_r->opcode)
11340 {
11341 /* CSPR is going to be changed. */
11342 record_buf[0] = ARM_PS_REGNUM;
11343 arm_insn_r->reg_rec_count = 1;
11344 }
11345 else
11346 {
11347 /* SPSR is going to be changed. */
11348 }
11349 }
11350 else if (arm_insn_r->opcode <= 15)
11351 {
11352 /* Normal data processing insns. */
11353 /* Out of 11 shifter operands mode, all the insn modifies destination
11354 register, which is specified by 13-16 decode. */
11355 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11356 record_buf[1] = ARM_PS_REGNUM;
11357 arm_insn_r->reg_rec_count = 2;
11358 }
11359 else
11360 {
11361 return -1;
11362 }
11363
11364 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11365 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11366 return 0;
11367}
11368
11369/* Handling opcode 010 insns. */
11370
11371static int
11372arm_record_ld_st_imm_offset (insn_decode_record *arm_insn_r)
11373{
11374 struct regcache *reg_cache = arm_insn_r->regcache;
11375
11376 uint32_t reg_src1 = 0 , reg_dest = 0;
11377 uint32_t offset_12 = 0, tgt_mem_addr = 0;
11378 uint32_t record_buf[8], record_buf_mem[8];
11379
11380 ULONGEST u_regval = 0;
11381
11382 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11383 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11384
11385 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11386 {
11387 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11388 /* LDR insn has a capability to do branching, if
11389 MOV LR, PC is precedded by LDR insn having Rn as R15
11390 in that case, it emulates branch and link insn, and hence we
11391 need to save CSPR and PC as well. */
11392 if (ARM_PC_REGNUM != reg_dest)
11393 {
11394 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11395 arm_insn_r->reg_rec_count = 1;
11396 }
11397 else
11398 {
11399 record_buf[0] = reg_dest;
11400 record_buf[1] = ARM_PS_REGNUM;
11401 arm_insn_r->reg_rec_count = 2;
11402 }
11403 }
11404 else
11405 {
11406 /* Store, immediate offset, immediate pre-indexed,
11407 immediate post-indexed. */
11408 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11409 offset_12 = bits (arm_insn_r->arm_insn, 0, 11);
11410 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11411 /* U == 1 */
11412 if (bit (arm_insn_r->arm_insn, 23))
11413 {
11414 tgt_mem_addr = u_regval + offset_12;
11415 }
11416 else
11417 {
11418 tgt_mem_addr = u_regval - offset_12;
11419 }
11420
11421 switch (arm_insn_r->opcode)
11422 {
11423 /* STR. */
11424 case 8:
11425 case 12:
11426 /* STR. */
11427 case 9:
11428 case 13:
11429 /* STRT. */
11430 case 1:
11431 case 5:
11432 /* STR. */
11433 case 4:
11434 case 0:
11435 record_buf_mem[0] = 4;
11436 break;
11437
11438 /* STRB. */
11439 case 10:
11440 case 14:
11441 /* STRB. */
11442 case 11:
11443 case 15:
11444 /* STRBT. */
11445 case 3:
11446 case 7:
11447 /* STRB. */
11448 case 2:
11449 case 6:
11450 record_buf_mem[0] = 1;
11451 break;
11452
11453 default:
11454 gdb_assert_not_reached ("no decoding pattern found");
11455 break;
11456 }
11457 record_buf_mem[1] = tgt_mem_addr;
11458 arm_insn_r->mem_rec_count = 1;
11459
11460 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11461 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11462 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11463 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11464 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11465 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11466 )
11467 {
11468 /* We are handling pre-indexed mode; post-indexed mode;
11469 where Rn is going to be changed. */
11470 record_buf[0] = reg_src1;
11471 arm_insn_r->reg_rec_count = 1;
11472 }
11473 }
11474
11475 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11476 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11477 return 0;
11478}
11479
11480/* Handling opcode 011 insns. */
11481
11482static int
11483arm_record_ld_st_reg_offset (insn_decode_record *arm_insn_r)
11484{
11485 struct regcache *reg_cache = arm_insn_r->regcache;
11486
11487 uint32_t shift_imm = 0;
11488 uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
11489 uint32_t offset_12 = 0, tgt_mem_addr = 0;
11490 uint32_t record_buf[8], record_buf_mem[8];
11491
11492 LONGEST s_word;
11493 ULONGEST u_regval[2];
11494
11495 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11496 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11497
11498 /* Handle enhanced store insns and LDRD DSP insn,
11499 order begins according to addressing modes for store insns
11500 STRH insn. */
11501
11502 /* LDR or STR? */
11503 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11504 {
11505 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11506 /* LDR insn has a capability to do branching, if
11507 MOV LR, PC is precedded by LDR insn having Rn as R15
11508 in that case, it emulates branch and link insn, and hence we
11509 need to save CSPR and PC as well. */
11510 if (15 != reg_dest)
11511 {
11512 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11513 arm_insn_r->reg_rec_count = 1;
11514 }
11515 else
11516 {
11517 record_buf[0] = reg_dest;
11518 record_buf[1] = ARM_PS_REGNUM;
11519 arm_insn_r->reg_rec_count = 2;
11520 }
11521 }
11522 else
11523 {
11524 if (! bits (arm_insn_r->arm_insn, 4, 11))
11525 {
11526 /* Store insn, register offset and register pre-indexed,
11527 register post-indexed. */
11528 /* Get Rm. */
11529 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11530 /* Get Rn. */
11531 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11532 regcache_raw_read_unsigned (reg_cache, reg_src1
11533 , &u_regval[0]);
11534 regcache_raw_read_unsigned (reg_cache, reg_src2
11535 , &u_regval[1]);
11536 if (15 == reg_src2)
11537 {
11538 /* If R15 was used as Rn, hence current PC+8. */
11539 /* Pre-indexed mode doesnt reach here ; illegal insn. */
11540 u_regval[0] = u_regval[0] + 8;
11541 }
11542 /* Calculate target store address, Rn +/- Rm, register offset. */
11543 /* U == 1. */
11544 if (bit (arm_insn_r->arm_insn, 23))
11545 {
11546 tgt_mem_addr = u_regval[0] + u_regval[1];
11547 }
11548 else
11549 {
11550 tgt_mem_addr = u_regval[1] - u_regval[0];
11551 }
11552
11553 switch (arm_insn_r->opcode)
11554 {
11555 /* STR. */
11556 case 8:
11557 case 12:
11558 /* STR. */
11559 case 9:
11560 case 13:
11561 /* STRT. */
11562 case 1:
11563 case 5:
11564 /* STR. */
11565 case 0:
11566 case 4:
11567 record_buf_mem[0] = 4;
11568 break;
11569
11570 /* STRB. */
11571 case 10:
11572 case 14:
11573 /* STRB. */
11574 case 11:
11575 case 15:
11576 /* STRBT. */
11577 case 3:
11578 case 7:
11579 /* STRB. */
11580 case 2:
11581 case 6:
11582 record_buf_mem[0] = 1;
11583 break;
11584
11585 default:
11586 gdb_assert_not_reached ("no decoding pattern found");
11587 break;
11588 }
11589 record_buf_mem[1] = tgt_mem_addr;
11590 arm_insn_r->mem_rec_count = 1;
11591
11592 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11593 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11594 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11595 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11596 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11597 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11598 )
11599 {
11600 /* Rn is going to be changed in pre-indexed mode and
11601 post-indexed mode as well. */
11602 record_buf[0] = reg_src2;
11603 arm_insn_r->reg_rec_count = 1;
11604 }
11605 }
11606 else
11607 {
11608 /* Store insn, scaled register offset; scaled pre-indexed. */
11609 offset_12 = bits (arm_insn_r->arm_insn, 5, 6);
11610 /* Get Rm. */
11611 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11612 /* Get Rn. */
11613 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11614 /* Get shift_imm. */
11615 shift_imm = bits (arm_insn_r->arm_insn, 7, 11);
11616 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11617 regcache_raw_read_signed (reg_cache, reg_src1, &s_word);
11618 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11619 /* Offset_12 used as shift. */
11620 switch (offset_12)
11621 {
11622 case 0:
11623 /* Offset_12 used as index. */
11624 offset_12 = u_regval[0] << shift_imm;
11625 break;
11626
11627 case 1:
11628 offset_12 = (!shift_imm)?0:u_regval[0] >> shift_imm;
11629 break;
11630
11631 case 2:
11632 if (!shift_imm)
11633 {
11634 if (bit (u_regval[0], 31))
11635 {
11636 offset_12 = 0xFFFFFFFF;
11637 }
11638 else
11639 {
11640 offset_12 = 0;
11641 }
11642 }
11643 else
11644 {
11645 /* This is arithmetic shift. */
11646 offset_12 = s_word >> shift_imm;
11647 }
11648 break;
11649
11650 case 3:
11651 if (!shift_imm)
11652 {
11653 regcache_raw_read_unsigned (reg_cache, ARM_PS_REGNUM,
11654 &u_regval[1]);
11655 /* Get C flag value and shift it by 31. */
11656 offset_12 = (((bit (u_regval[1], 29)) << 31) \
11657 | (u_regval[0]) >> 1);
11658 }
11659 else
11660 {
11661 offset_12 = (u_regval[0] >> shift_imm) \
11662 | (u_regval[0] <<
11663 (sizeof(uint32_t) - shift_imm));
11664 }
11665 break;
11666
11667 default:
11668 gdb_assert_not_reached ("no decoding pattern found");
11669 break;
11670 }
11671
11672 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11673 /* bit U set. */
11674 if (bit (arm_insn_r->arm_insn, 23))
11675 {
11676 tgt_mem_addr = u_regval[1] + offset_12;
11677 }
11678 else
11679 {
11680 tgt_mem_addr = u_regval[1] - offset_12;
11681 }
11682
11683 switch (arm_insn_r->opcode)
11684 {
11685 /* STR. */
11686 case 8:
11687 case 12:
11688 /* STR. */
11689 case 9:
11690 case 13:
11691 /* STRT. */
11692 case 1:
11693 case 5:
11694 /* STR. */
11695 case 0:
11696 case 4:
11697 record_buf_mem[0] = 4;
11698 break;
11699
11700 /* STRB. */
11701 case 10:
11702 case 14:
11703 /* STRB. */
11704 case 11:
11705 case 15:
11706 /* STRBT. */
11707 case 3:
11708 case 7:
11709 /* STRB. */
11710 case 2:
11711 case 6:
11712 record_buf_mem[0] = 1;
11713 break;
11714
11715 default:
11716 gdb_assert_not_reached ("no decoding pattern found");
11717 break;
11718 }
11719 record_buf_mem[1] = tgt_mem_addr;
11720 arm_insn_r->mem_rec_count = 1;
11721
11722 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11723 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11724 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11725 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11726 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11727 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11728 )
11729 {
11730 /* Rn is going to be changed in register scaled pre-indexed
11731 mode,and scaled post indexed mode. */
11732 record_buf[0] = reg_src2;
11733 arm_insn_r->reg_rec_count = 1;
11734 }
11735 }
11736 }
11737
11738 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11739 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11740 return 0;
11741}
11742
11743/* Handling opcode 100 insns. */
11744
11745static int
11746arm_record_ld_st_multiple (insn_decode_record *arm_insn_r)
11747{
11748 struct regcache *reg_cache = arm_insn_r->regcache;
11749
11750 uint32_t register_list[16] = {0}, register_count = 0, register_bits = 0;
11751 uint32_t reg_src1 = 0, addr_mode = 0, no_of_regs = 0;
11752 uint32_t start_address = 0, index = 0;
11753 uint32_t record_buf[24], record_buf_mem[48];
11754
11755 ULONGEST u_regval[2] = {0};
11756
11757 /* This mode is exclusively for load and store multiple. */
11758 /* Handle incremenrt after/before and decrment after.before mode;
11759 Rn is changing depending on W bit, but as of now we store Rn too
11760 without optimization. */
11761
11762 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11763 {
11764 /* LDM (1,2,3) where LDM (3) changes CPSR too. */
11765
11766 if (bit (arm_insn_r->arm_insn, 20) && !bit (arm_insn_r->arm_insn, 22))
11767 {
11768 register_bits = bits (arm_insn_r->arm_insn, 0, 15);
11769 no_of_regs = 15;
11770 }
11771 else
11772 {
11773 register_bits = bits (arm_insn_r->arm_insn, 0, 14);
11774 no_of_regs = 14;
11775 }
11776 /* Get Rn. */
11777 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11778 while (register_bits)
11779 {
11780 if (register_bits & 0x00000001)
11781 register_list[register_count++] = 1;
11782 register_bits = register_bits >> 1;
11783 }
11784
11785 /* Extra space for Base Register and CPSR; wihtout optimization. */
11786 record_buf[register_count] = reg_src1;
11787 record_buf[register_count + 1] = ARM_PS_REGNUM;
11788 arm_insn_r->reg_rec_count = register_count + 2;
11789
11790 for (register_count = 0; register_count < no_of_regs; register_count++)
11791 {
11792 if (register_list[register_count])
11793 {
11794 /* Register_count gives total no of registers
11795 and dually working as reg number. */
11796 record_buf[index] = register_count;
11797 index++;
11798 }
11799 }
11800
11801 }
11802 else
11803 {
11804 /* It handles both STM(1) and STM(2). */
11805 addr_mode = bits (arm_insn_r->arm_insn, 23, 24);
11806
11807 register_bits = bits (arm_insn_r->arm_insn, 0, 15);
11808 /* Get Rn. */
11809 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11810 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11811 while (register_bits)
11812 {
11813 if (register_bits & 0x00000001)
11814 register_count++;
11815 register_bits = register_bits >> 1;
11816 }
11817
11818 switch (addr_mode)
11819 {
11820 /* Decrement after. */
11821 case 0:
11822 start_address = (u_regval[0]) - (register_count * 4) + 4;
11823 arm_insn_r->mem_rec_count = register_count;
11824 while (register_count)
11825 {
11826 record_buf_mem[(register_count * 2) - 1] = start_address;
11827 record_buf_mem[(register_count * 2) - 2] = 4;
11828 start_address = start_address + 4;
11829 register_count--;
11830 }
11831 break;
11832
11833 /* Increment after. */
11834 case 1:
11835 start_address = u_regval[0];
11836 arm_insn_r->mem_rec_count = register_count;
11837 while (register_count)
11838 {
11839 record_buf_mem[(register_count * 2) - 1] = start_address;
11840 record_buf_mem[(register_count * 2) - 2] = 4;
11841 start_address = start_address + 4;
11842 register_count--;
11843 }
11844 break;
11845
11846 /* Decrement before. */
11847 case 2:
11848
11849 start_address = (u_regval[0]) - (register_count * 4);
11850 arm_insn_r->mem_rec_count = register_count;
11851 while (register_count)
11852 {
11853 record_buf_mem[(register_count * 2) - 1] = start_address;
11854 record_buf_mem[(register_count * 2) - 2] = 4;
11855 start_address = start_address + 4;
11856 register_count--;
11857 }
11858 break;
11859
11860 /* Increment before. */
11861 case 3:
11862 start_address = u_regval[0] + 4;
11863 arm_insn_r->mem_rec_count = register_count;
11864 while (register_count)
11865 {
11866 record_buf_mem[(register_count * 2) - 1] = start_address;
11867 record_buf_mem[(register_count * 2) - 2] = 4;
11868 start_address = start_address + 4;
11869 register_count--;
11870 }
11871 break;
11872
11873 default:
11874 gdb_assert_not_reached ("no decoding pattern found");
11875 break;
11876 }
11877
11878 /* Base register also changes; based on condition and W bit. */
11879 /* We save it anyway without optimization. */
11880 record_buf[0] = reg_src1;
11881 arm_insn_r->reg_rec_count = 1;
11882 }
11883
11884 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11885 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11886 return 0;
11887}
11888
11889/* Handling opcode 101 insns. */
11890
11891static int
11892arm_record_b_bl (insn_decode_record *arm_insn_r)
11893{
11894 uint32_t record_buf[8];
11895
11896 /* Handle B, BL, BLX(1) insns. */
11897 /* B simply branches so we do nothing here. */
11898 /* Note: BLX(1) doesnt fall here but instead it falls into
11899 extension space. */
11900 if (bit (arm_insn_r->arm_insn, 24))
11901 {
11902 record_buf[0] = ARM_LR_REGNUM;
11903 arm_insn_r->reg_rec_count = 1;
11904 }
11905
11906 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11907
11908 return 0;
11909}
11910
11911/* Handling opcode 110 insns. */
11912
11913static int
11914arm_record_coproc (insn_decode_record *arm_insn_r)
11915{
11916 printf_unfiltered (_("Process record does not support instruction "
11917 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
11918 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11919
11920 return -1;
11921}
11922
11923/* Handling opcode 111 insns. */
11924
11925static int
11926arm_record_coproc_data_proc (insn_decode_record *arm_insn_r)
11927{
11928 struct gdbarch_tdep *tdep = gdbarch_tdep (arm_insn_r->gdbarch);
11929 struct regcache *reg_cache = arm_insn_r->regcache;
11930 uint32_t ret = 0; /* function return value: -1:record failure ; 0:success */
11931
11932 /* Handle SWI insn; system call would be handled over here. */
11933
11934 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 24, 27);
11935 if (15 == arm_insn_r->opcode)
11936 {
11937 /* Handle arm syscall insn. */
11938 if (tdep->arm_swi_record != NULL)
11939 {
11940 ret = tdep->arm_swi_record(reg_cache);
11941 }
11942 else
11943 {
11944 printf_unfiltered (_("no syscall record support\n"));
11945 ret = -1;
11946 }
11947 }
11948
11949 printf_unfiltered (_("Process record does not support instruction "
11950 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
11951 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11952 return ret;
11953}
11954
11955/* Handling opcode 000 insns. */
11956
11957static int
11958thumb_record_shift_add_sub (insn_decode_record *thumb_insn_r)
11959{
11960 uint32_t record_buf[8];
11961 uint32_t reg_src1 = 0;
11962
11963 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11964
11965 record_buf[0] = ARM_PS_REGNUM;
11966 record_buf[1] = reg_src1;
11967 thumb_insn_r->reg_rec_count = 2;
11968
11969 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11970
11971 return 0;
11972}
11973
11974
11975/* Handling opcode 001 insns. */
11976
11977static int
11978thumb_record_add_sub_cmp_mov (insn_decode_record *thumb_insn_r)
11979{
11980 uint32_t record_buf[8];
11981 uint32_t reg_src1 = 0;
11982
11983 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11984
11985 record_buf[0] = ARM_PS_REGNUM;
11986 record_buf[1] = reg_src1;
11987 thumb_insn_r->reg_rec_count = 2;
11988
11989 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11990
11991 return 0;
11992}
11993
11994/* Handling opcode 010 insns. */
11995
11996static int
11997thumb_record_ld_st_reg_offset (insn_decode_record *thumb_insn_r)
11998{
11999 struct regcache *reg_cache = thumb_insn_r->regcache;
12000 uint32_t record_buf[8], record_buf_mem[8];
12001
12002 uint32_t reg_src1 = 0, reg_src2 = 0;
12003 uint32_t opcode1 = 0, opcode2 = 0, opcode3 = 0;
12004
12005 ULONGEST u_regval[2] = {0};
12006
12007 opcode1 = bits (thumb_insn_r->arm_insn, 10, 12);
12008
12009 if (bit (thumb_insn_r->arm_insn, 12))
12010 {
12011 /* Handle load/store register offset. */
12012 opcode2 = bits (thumb_insn_r->arm_insn, 9, 10);
12013 if (opcode2 >= 12 && opcode2 <= 15)
12014 {
12015 /* LDR(2), LDRB(2) , LDRH(2), LDRSB, LDRSH. */
12016 reg_src1 = bits (thumb_insn_r->arm_insn,0, 2);
12017 record_buf[0] = reg_src1;
12018 thumb_insn_r->reg_rec_count = 1;
12019 }
12020 else if (opcode2 >= 8 && opcode2 <= 10)
12021 {
12022 /* STR(2), STRB(2), STRH(2) . */
12023 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12024 reg_src2 = bits (thumb_insn_r->arm_insn, 6, 8);
12025 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
12026 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
12027 if (8 == opcode2)
12028 record_buf_mem[0] = 4; /* STR (2). */
12029 else if (10 == opcode2)
12030 record_buf_mem[0] = 1; /* STRB (2). */
12031 else if (9 == opcode2)
12032 record_buf_mem[0] = 2; /* STRH (2). */
12033 record_buf_mem[1] = u_regval[0] + u_regval[1];
12034 thumb_insn_r->mem_rec_count = 1;
12035 }
12036 }
12037 else if (bit (thumb_insn_r->arm_insn, 11))
12038 {
12039 /* Handle load from literal pool. */
12040 /* LDR(3). */
12041 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12042 record_buf[0] = reg_src1;
12043 thumb_insn_r->reg_rec_count = 1;
12044 }
12045 else if (opcode1)
12046 {
12047 opcode2 = bits (thumb_insn_r->arm_insn, 8, 9);
12048 opcode3 = bits (thumb_insn_r->arm_insn, 0, 2);
12049 if ((3 == opcode2) && (!opcode3))
12050 {
12051 /* Branch with exchange. */
12052 record_buf[0] = ARM_PS_REGNUM;
12053 thumb_insn_r->reg_rec_count = 1;
12054 }
12055 else
12056 {
12057 /* Format 8; special data processing insns. */
12058 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12059 record_buf[0] = ARM_PS_REGNUM;
12060 record_buf[1] = reg_src1;
12061 thumb_insn_r->reg_rec_count = 2;
12062 }
12063 }
12064 else
12065 {
12066 /* Format 5; data processing insns. */
12067 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12068 if (bit (thumb_insn_r->arm_insn, 7))
12069 {
12070 reg_src1 = reg_src1 + 8;
12071 }
12072 record_buf[0] = ARM_PS_REGNUM;
12073 record_buf[1] = reg_src1;
12074 thumb_insn_r->reg_rec_count = 2;
12075 }
12076
12077 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12078 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12079 record_buf_mem);
12080
12081 return 0;
12082}
12083
12084/* Handling opcode 001 insns. */
12085
12086static int
12087thumb_record_ld_st_imm_offset (insn_decode_record *thumb_insn_r)
12088{
12089 struct regcache *reg_cache = thumb_insn_r->regcache;
12090 uint32_t record_buf[8], record_buf_mem[8];
12091
12092 uint32_t reg_src1 = 0;
12093 uint32_t opcode = 0, immed_5 = 0;
12094
12095 ULONGEST u_regval = 0;
12096
12097 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12098
12099 if (opcode)
12100 {
12101 /* LDR(1). */
12102 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12103 record_buf[0] = reg_src1;
12104 thumb_insn_r->reg_rec_count = 1;
12105 }
12106 else
12107 {
12108 /* STR(1). */
12109 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12110 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
12111 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12112 record_buf_mem[0] = 4;
12113 record_buf_mem[1] = u_regval + (immed_5 * 4);
12114 thumb_insn_r->mem_rec_count = 1;
12115 }
12116
12117 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12118 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12119 record_buf_mem);
12120
12121 return 0;
12122}
12123
12124/* Handling opcode 100 insns. */
12125
12126static int
12127thumb_record_ld_st_stack (insn_decode_record *thumb_insn_r)
12128{
12129 struct regcache *reg_cache = thumb_insn_r->regcache;
12130 uint32_t record_buf[8], record_buf_mem[8];
12131
12132 uint32_t reg_src1 = 0;
12133 uint32_t opcode = 0, immed_8 = 0, immed_5 = 0;
12134
12135 ULONGEST u_regval = 0;
12136
12137 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12138
12139 if (3 == opcode)
12140 {
12141 /* LDR(4). */
12142 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12143 record_buf[0] = reg_src1;
12144 thumb_insn_r->reg_rec_count = 1;
12145 }
12146 else if (1 == opcode)
12147 {
12148 /* LDRH(1). */
12149 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12150 record_buf[0] = reg_src1;
12151 thumb_insn_r->reg_rec_count = 1;
12152 }
12153 else if (2 == opcode)
12154 {
12155 /* STR(3). */
12156 immed_8 = bits (thumb_insn_r->arm_insn, 0, 7);
12157 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
12158 record_buf_mem[0] = 4;
12159 record_buf_mem[1] = u_regval + (immed_8 * 4);
12160 thumb_insn_r->mem_rec_count = 1;
12161 }
12162 else if (0 == opcode)
12163 {
12164 /* STRH(1). */
12165 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
12166 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12167 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12168 record_buf_mem[0] = 2;
12169 record_buf_mem[1] = u_regval + (immed_5 * 2);
12170 thumb_insn_r->mem_rec_count = 1;
12171 }
12172
12173 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12174 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12175 record_buf_mem);
12176
12177 return 0;
12178}
12179
12180/* Handling opcode 101 insns. */
12181
12182static int
12183thumb_record_misc (insn_decode_record *thumb_insn_r)
12184{
12185 struct regcache *reg_cache = thumb_insn_r->regcache;
12186
12187 uint32_t opcode = 0, opcode1 = 0, opcode2 = 0;
12188 uint32_t register_bits = 0, register_count = 0;
12189 uint32_t register_list[8] = {0}, index = 0, start_address = 0;
12190 uint32_t record_buf[24], record_buf_mem[48];
12191 uint32_t reg_src1;
12192
12193 ULONGEST u_regval = 0;
12194
12195 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12196 opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
12197 opcode2 = bits (thumb_insn_r->arm_insn, 9, 12);
12198
12199 if (14 == opcode2)
12200 {
12201 /* POP. */
12202 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12203 while (register_bits)
12204 {
12205 if (register_bits & 0x00000001)
12206 register_list[register_count++] = 1;
12207 register_bits = register_bits >> 1;
12208 }
12209 record_buf[register_count] = ARM_PS_REGNUM;
12210 record_buf[register_count + 1] = ARM_SP_REGNUM;
12211 thumb_insn_r->reg_rec_count = register_count + 2;
12212 for (register_count = 0; register_count < 8; register_count++)
12213 {
12214 if (register_list[register_count])
12215 {
12216 record_buf[index] = register_count;
12217 index++;
12218 }
12219 }
12220 }
12221 else if (10 == opcode2)
12222 {
12223 /* PUSH. */
12224 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12225 regcache_raw_read_unsigned (reg_cache, ARM_PC_REGNUM, &u_regval);
12226 while (register_bits)
12227 {
12228 if (register_bits & 0x00000001)
12229 register_count++;
12230 register_bits = register_bits >> 1;
12231 }
12232 start_address = u_regval - \
12233 (4 * (bit (thumb_insn_r->arm_insn, 8) + register_count));
12234 thumb_insn_r->mem_rec_count = register_count;
12235 while (register_count)
12236 {
12237 record_buf_mem[(register_count * 2) - 1] = start_address;
12238 record_buf_mem[(register_count * 2) - 2] = 4;
12239 start_address = start_address + 4;
12240 register_count--;
12241 }
12242 record_buf[0] = ARM_SP_REGNUM;
12243 thumb_insn_r->reg_rec_count = 1;
12244 }
12245 else if (0x1E == opcode1)
12246 {
12247 /* BKPT insn. */
12248 /* Handle enhanced software breakpoint insn, BKPT. */
12249 /* CPSR is changed to be executed in ARM state, disabling normal
12250 interrupts, entering abort mode. */
12251 /* According to high vector configuration PC is set. */
12252 /* User hits breakpoint and type reverse, in that case, we need to go back with
12253 previous CPSR and Program Counter. */
12254 record_buf[0] = ARM_PS_REGNUM;
12255 record_buf[1] = ARM_LR_REGNUM;
12256 thumb_insn_r->reg_rec_count = 2;
12257 /* We need to save SPSR value, which is not yet done. */
12258 printf_unfiltered (_("Process record does not support instruction "
12259 "0x%0x at address %s.\n"),
12260 thumb_insn_r->arm_insn,
12261 paddress (thumb_insn_r->gdbarch,
12262 thumb_insn_r->this_addr));
12263 return -1;
12264 }
12265 else if ((0 == opcode) || (1 == opcode))
12266 {
12267 /* ADD(5), ADD(6). */
12268 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12269 record_buf[0] = reg_src1;
12270 thumb_insn_r->reg_rec_count = 1;
12271 }
12272 else if (2 == opcode)
12273 {
12274 /* ADD(7), SUB(4). */
12275 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12276 record_buf[0] = ARM_SP_REGNUM;
12277 thumb_insn_r->reg_rec_count = 1;
12278 }
12279
12280 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12281 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12282 record_buf_mem);
12283
12284 return 0;
12285}
12286
12287/* Handling opcode 110 insns. */
12288
12289static int
12290thumb_record_ldm_stm_swi (insn_decode_record *thumb_insn_r)
12291{
12292 struct gdbarch_tdep *tdep = gdbarch_tdep (thumb_insn_r->gdbarch);
12293 struct regcache *reg_cache = thumb_insn_r->regcache;
12294
12295 uint32_t ret = 0; /* function return value: -1:record failure ; 0:success */
12296 uint32_t reg_src1 = 0;
12297 uint32_t opcode1 = 0, opcode2 = 0, register_bits = 0, register_count = 0;
12298 uint32_t register_list[8] = {0}, index = 0, start_address = 0;
12299 uint32_t record_buf[24], record_buf_mem[48];
12300
12301 ULONGEST u_regval = 0;
12302
12303 opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
12304 opcode2 = bits (thumb_insn_r->arm_insn, 11, 12);
12305
12306 if (1 == opcode2)
12307 {
12308
12309 /* LDMIA. */
12310 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12311 /* Get Rn. */
12312 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12313 while (register_bits)
12314 {
12315 if (register_bits & 0x00000001)
12316 register_list[register_count++] = 1;
12317 register_bits = register_bits >> 1;
12318 }
12319 record_buf[register_count] = reg_src1;
12320 thumb_insn_r->reg_rec_count = register_count + 1;
12321 for (register_count = 0; register_count < 8; register_count++)
12322 {
12323 if (register_list[register_count])
12324 {
12325 record_buf[index] = register_count;
12326 index++;
12327 }
12328 }
12329 }
12330 else if (0 == opcode2)
12331 {
12332 /* It handles both STMIA. */
12333 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12334 /* Get Rn. */
12335 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12336 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12337 while (register_bits)
12338 {
12339 if (register_bits & 0x00000001)
12340 register_count++;
12341 register_bits = register_bits >> 1;
12342 }
12343 start_address = u_regval;
12344 thumb_insn_r->mem_rec_count = register_count;
12345 while (register_count)
12346 {
12347 record_buf_mem[(register_count * 2) - 1] = start_address;
12348 record_buf_mem[(register_count * 2) - 2] = 4;
12349 start_address = start_address + 4;
12350 register_count--;
12351 }
12352 }
12353 else if (0x1F == opcode1)
12354 {
12355 /* Handle arm syscall insn. */
12356 if (tdep->arm_swi_record != NULL)
12357 {
12358 ret = tdep->arm_swi_record(reg_cache);
12359 }
12360 else
12361 {
12362 printf_unfiltered (_("no syscall record support\n"));
12363 return -1;
12364 }
12365 }
12366
12367 /* B (1), conditional branch is automatically taken care in process_record,
12368 as PC is saved there. */
12369
12370 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12371 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12372 record_buf_mem);
12373
12374 return ret;
12375}
12376
12377/* Handling opcode 111 insns. */
12378
12379static int
12380thumb_record_branch (insn_decode_record *thumb_insn_r)
12381{
12382 uint32_t record_buf[8];
12383 uint32_t bits_h = 0;
12384
12385 bits_h = bits (thumb_insn_r->arm_insn, 11, 12);
12386
12387 if (2 == bits_h || 3 == bits_h)
12388 {
12389 /* BL */
12390 record_buf[0] = ARM_LR_REGNUM;
12391 thumb_insn_r->reg_rec_count = 1;
12392 }
12393 else if (1 == bits_h)
12394 {
12395 /* BLX(1). */
12396 record_buf[0] = ARM_PS_REGNUM;
12397 record_buf[1] = ARM_LR_REGNUM;
12398 thumb_insn_r->reg_rec_count = 2;
12399 }
12400
12401 /* B(2) is automatically taken care in process_record, as PC is
12402 saved there. */
12403
12404 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12405
12406 return 0;
12407}
12408
12409
12410/* Extracts arm/thumb/thumb2 insn depending on the size, and returns 0 on success
12411and positive val on fauilure. */
12412
12413static int
12414extract_arm_insn (insn_decode_record *insn_record, uint32_t insn_size)
12415{
12416 gdb_byte buf[insn_size];
12417
12418 memset (&buf[0], 0, insn_size);
12419
12420 if (target_read_memory (insn_record->this_addr, &buf[0], insn_size))
12421 return 1;
12422 insn_record->arm_insn = (uint32_t) extract_unsigned_integer (&buf[0],
12423 insn_size,
12424 gdbarch_byte_order (insn_record->gdbarch));
12425 return 0;
12426}
12427
12428typedef int (*sti_arm_hdl_fp_t) (insn_decode_record*);
12429
12430/* Decode arm/thumb insn depending on condition cods and opcodes; and
12431 dispatch it. */
12432
12433static int
12434decode_insn (insn_decode_record *arm_record, record_type_t record_type,
12435 uint32_t insn_size)
12436{
12437
12438 /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm instruction. */
12439 static const sti_arm_hdl_fp_t const arm_handle_insn[8] =
12440 {
12441 arm_record_data_proc_misc_ld_str, /* 000. */
12442 arm_record_data_proc_imm, /* 001. */
12443 arm_record_ld_st_imm_offset, /* 010. */
12444 arm_record_ld_st_reg_offset, /* 011. */
12445 arm_record_ld_st_multiple, /* 100. */
12446 arm_record_b_bl, /* 101. */
12447 arm_record_coproc, /* 110. */
12448 arm_record_coproc_data_proc /* 111. */
12449 };
12450
12451 /* (Starting from numerical 0); bits 13,14,15 decodes type of thumb instruction. */
12452 static const sti_arm_hdl_fp_t const thumb_handle_insn[8] =
12453 { \
12454 thumb_record_shift_add_sub, /* 000. */
12455 thumb_record_add_sub_cmp_mov, /* 001. */
12456 thumb_record_ld_st_reg_offset, /* 010. */
12457 thumb_record_ld_st_imm_offset, /* 011. */
12458 thumb_record_ld_st_stack, /* 100. */
12459 thumb_record_misc, /* 101. */
12460 thumb_record_ldm_stm_swi, /* 110. */
12461 thumb_record_branch /* 111. */
12462 };
12463
12464 uint32_t ret = 0; /* return value: negative:failure 0:success. */
12465 uint32_t insn_id = 0;
12466
12467 if (extract_arm_insn (arm_record, insn_size))
12468 {
12469 if (record_debug)
12470 {
12471 printf_unfiltered (_("Process record: error reading memory at "
12472 "addr %s len = %d.\n"),
12473 paddress (arm_record->gdbarch, arm_record->this_addr), insn_size);
12474 }
12475 return -1;
12476 }
12477 else if (ARM_RECORD == record_type)
12478 {
12479 arm_record->cond = bits (arm_record->arm_insn, 28, 31);
12480 insn_id = bits (arm_record->arm_insn, 25, 27);
12481 ret = arm_record_extension_space (arm_record);
12482 /* If this insn has fallen into extension space
12483 then we need not decode it anymore. */
12484 if (ret != -1 && !INSN_RECORDED(arm_record))
12485 {
12486 ret = arm_handle_insn[insn_id] (arm_record);
12487 }
12488 }
12489 else if (THUMB_RECORD == record_type)
12490 {
12491 /* As thumb does not have condition codes, we set negative. */
12492 arm_record->cond = -1;
12493 insn_id = bits (arm_record->arm_insn, 13, 15);
12494 ret = thumb_handle_insn[insn_id] (arm_record);
12495 }
12496 else if (THUMB2_RECORD == record_type)
12497 {
12498 printf_unfiltered (_("Process record doesnt support thumb32 instruction "
12499 "0x%0x at address %s.\n"),arm_record->arm_insn,
12500 paddress (arm_record->gdbarch,
12501 arm_record->this_addr));
12502 ret = -1;
12503 }
12504 else
12505 {
12506 /* Throw assertion. */
12507 gdb_assert_not_reached ("not a valid instruction, could not decode");
12508 }
12509
12510 return ret;
12511}
12512
12513
12514/* Cleans up local record registers and memory allocations. */
12515
12516static void
12517deallocate_reg_mem (insn_decode_record *record)
12518{
12519 xfree (record->arm_regs);
12520 xfree (record->arm_mems);
12521}
12522
12523
12524/* Parse the current instruction and record the values of the registers and
12525 memory that will be changed in current instruction to record_arch_list".
12526 Return -1 if something is wrong. */
12527
12528int
12529arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
12530 CORE_ADDR insn_addr)
12531{
12532
12533 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
12534 uint32_t no_of_rec = 0;
12535 uint32_t ret = 0; /* return value: -1:record failure ; 0:success */
12536 ULONGEST t_bit = 0, insn_id = 0;
12537
12538 ULONGEST u_regval = 0;
12539
12540 insn_decode_record arm_record;
12541
12542 memset (&arm_record, 0, sizeof (insn_decode_record));
12543 arm_record.regcache = regcache;
12544 arm_record.this_addr = insn_addr;
12545 arm_record.gdbarch = gdbarch;
12546
12547
12548 if (record_debug > 1)
12549 {
12550 fprintf_unfiltered (gdb_stdlog, "Process record: arm_process_record "
12551 "addr = %s\n",
12552 paddress (gdbarch, arm_record.this_addr));
12553 }
12554
12555 if (extract_arm_insn (&arm_record, 2))
12556 {
12557 if (record_debug)
12558 {
12559 printf_unfiltered (_("Process record: error reading memory at "
12560 "addr %s len = %d.\n"),
12561 paddress (arm_record.gdbarch,
12562 arm_record.this_addr), 2);
12563 }
12564 return -1;
12565 }
12566
12567 /* Check the insn, whether it is thumb or arm one. */
12568
12569 t_bit = arm_psr_thumb_bit (arm_record.gdbarch);
12570 regcache_raw_read_unsigned (arm_record.regcache, ARM_PS_REGNUM, &u_regval);
12571
12572
12573 if (!(u_regval & t_bit))
12574 {
12575 /* We are decoding arm insn. */
12576 ret = decode_insn (&arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES);
12577 }
12578 else
12579 {
12580 insn_id = bits (arm_record.arm_insn, 11, 15);
12581 /* is it thumb2 insn? */
12582 if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id))
12583 {
12584 ret = decode_insn (&arm_record, THUMB2_RECORD,
12585 THUMB2_INSN_SIZE_BYTES);
12586 }
12587 else
12588 {
12589 /* We are decoding thumb insn. */
12590 ret = decode_insn (&arm_record, THUMB_RECORD, THUMB_INSN_SIZE_BYTES);
12591 }
12592 }
12593
12594 if (0 == ret)
12595 {
12596 /* Record registers. */
25ea693b 12597 record_full_arch_list_add_reg (arm_record.regcache, ARM_PC_REGNUM);
72508ac0
PO
12598 if (arm_record.arm_regs)
12599 {
12600 for (no_of_rec = 0; no_of_rec < arm_record.reg_rec_count; no_of_rec++)
12601 {
25ea693b
MM
12602 if (record_full_arch_list_add_reg
12603 (arm_record.regcache , arm_record.arm_regs[no_of_rec]))
72508ac0
PO
12604 ret = -1;
12605 }
12606 }
12607 /* Record memories. */
12608 if (arm_record.arm_mems)
12609 {
12610 for (no_of_rec = 0; no_of_rec < arm_record.mem_rec_count; no_of_rec++)
12611 {
25ea693b 12612 if (record_full_arch_list_add_mem
72508ac0 12613 ((CORE_ADDR)arm_record.arm_mems[no_of_rec].addr,
25ea693b 12614 arm_record.arm_mems[no_of_rec].len))
72508ac0
PO
12615 ret = -1;
12616 }
12617 }
12618
25ea693b 12619 if (record_full_arch_list_add_end ())
72508ac0
PO
12620 ret = -1;
12621 }
12622
12623
12624 deallocate_reg_mem (&arm_record);
12625
12626 return ret;
12627}
12628