Automatic date update in version.in
[deliverable/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"
45741a9c 26#include "infrun.h"
c906108c
SS
27#include "gdbcmd.h"
28#include "gdbcore.h"
0963b4bd 29#include "dis-asm.h" /* For register styles. */
4e052eda 30#include "regcache.h"
54483882 31#include "reggroups.h"
d16aafd8 32#include "doublest.h"
fd0407d6 33#include "value.h"
34e8f22d 34#include "arch-utils.h"
4be87837 35#include "osabi.h"
eb5492fa
DJ
36#include "frame-unwind.h"
37#include "frame-base.h"
38#include "trad-frame.h"
842e1f1e
DJ
39#include "objfiles.h"
40#include "dwarf2-frame.h"
e4c16157 41#include "gdbtypes.h"
29d73ae4 42#include "prologue-value.h"
25f8c692 43#include "remote.h"
123dc839
DJ
44#include "target-descriptions.h"
45#include "user-regs.h"
0e9e9abd 46#include "observer.h"
34e8f22d
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
60c5725c 55#include "vec.h"
26216b98 56
72508ac0 57#include "record.h"
d02ed0bb 58#include "record-full.h"
72508ac0 59
9779414d 60#include "features/arm-with-m.c"
25f8c692 61#include "features/arm-with-m-fpa-layout.c"
3184d3f9 62#include "features/arm-with-m-vfp-d16.c"
ef7e8358
UW
63#include "features/arm-with-iwmmxt.c"
64#include "features/arm-with-vfpv2.c"
65#include "features/arm-with-vfpv3.c"
66#include "features/arm-with-neon.c"
9779414d 67
6529d2dd
AC
68static int arm_debug;
69
082fc60d
RE
70/* Macros for setting and testing a bit in a minimal symbol that marks
71 it as Thumb function. The MSB of the minimal symbol's "info" field
f594e5e9 72 is used for this purpose.
082fc60d
RE
73
74 MSYMBOL_SET_SPECIAL Actually sets the "special" bit.
f594e5e9 75 MSYMBOL_IS_SPECIAL Tests the "special" bit in a minimal symbol. */
082fc60d 76
0963b4bd 77#define MSYMBOL_SET_SPECIAL(msym) \
b887350f 78 MSYMBOL_TARGET_FLAG_1 (msym) = 1
082fc60d
RE
79
80#define MSYMBOL_IS_SPECIAL(msym) \
b887350f 81 MSYMBOL_TARGET_FLAG_1 (msym)
082fc60d 82
60c5725c
DJ
83/* Per-objfile data used for mapping symbols. */
84static const struct objfile_data *arm_objfile_data_key;
85
86struct arm_mapping_symbol
87{
88 bfd_vma value;
89 char type;
90};
91typedef struct arm_mapping_symbol arm_mapping_symbol_s;
92DEF_VEC_O(arm_mapping_symbol_s);
93
94struct arm_per_objfile
95{
96 VEC(arm_mapping_symbol_s) **section_maps;
97};
98
afd7eef0
RE
99/* The list of available "set arm ..." and "show arm ..." commands. */
100static struct cmd_list_element *setarmcmdlist = NULL;
101static struct cmd_list_element *showarmcmdlist = NULL;
102
fd50bc42
RE
103/* The type of floating-point to use. Keep this in sync with enum
104 arm_float_model, and the help string in _initialize_arm_tdep. */
40478521 105static const char *const fp_model_strings[] =
fd50bc42
RE
106{
107 "auto",
108 "softfpa",
109 "fpa",
110 "softvfp",
28e97307
DJ
111 "vfp",
112 NULL
fd50bc42
RE
113};
114
115/* A variable that can be configured by the user. */
116static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
117static const char *current_fp_model = "auto";
118
28e97307 119/* The ABI to use. Keep this in sync with arm_abi_kind. */
40478521 120static const char *const arm_abi_strings[] =
28e97307
DJ
121{
122 "auto",
123 "APCS",
124 "AAPCS",
125 NULL
126};
127
128/* A variable that can be configured by the user. */
129static enum arm_abi_kind arm_abi_global = ARM_ABI_AUTO;
130static const char *arm_abi_string = "auto";
131
0428b8f5 132/* The execution mode to assume. */
40478521 133static const char *const arm_mode_strings[] =
0428b8f5
DJ
134 {
135 "auto",
136 "arm",
68770265
MGD
137 "thumb",
138 NULL
0428b8f5
DJ
139 };
140
141static const char *arm_fallback_mode_string = "auto";
142static const char *arm_force_mode_string = "auto";
143
18819fa6
UW
144/* Internal override of the execution mode. -1 means no override,
145 0 means override to ARM mode, 1 means override to Thumb mode.
146 The effect is the same as if arm_force_mode has been set by the
147 user (except the internal override has precedence over a user's
148 arm_force_mode override). */
149static int arm_override_mode = -1;
150
94c30b78 151/* Number of different reg name sets (options). */
afd7eef0 152static int num_disassembly_options;
bc90b915 153
f32bf4a4
YQ
154/* The standard register names, and all the valid aliases for them. Note
155 that `fp', `sp' and `pc' are not added in this alias list, because they
156 have been added as builtin user registers in
157 std-regs.c:_initialize_frame_reg. */
123dc839
DJ
158static const struct
159{
160 const char *name;
161 int regnum;
162} arm_register_aliases[] = {
163 /* Basic register numbers. */
164 { "r0", 0 },
165 { "r1", 1 },
166 { "r2", 2 },
167 { "r3", 3 },
168 { "r4", 4 },
169 { "r5", 5 },
170 { "r6", 6 },
171 { "r7", 7 },
172 { "r8", 8 },
173 { "r9", 9 },
174 { "r10", 10 },
175 { "r11", 11 },
176 { "r12", 12 },
177 { "r13", 13 },
178 { "r14", 14 },
179 { "r15", 15 },
180 /* Synonyms (argument and variable registers). */
181 { "a1", 0 },
182 { "a2", 1 },
183 { "a3", 2 },
184 { "a4", 3 },
185 { "v1", 4 },
186 { "v2", 5 },
187 { "v3", 6 },
188 { "v4", 7 },
189 { "v5", 8 },
190 { "v6", 9 },
191 { "v7", 10 },
192 { "v8", 11 },
193 /* Other platform-specific names for r9. */
194 { "sb", 9 },
195 { "tr", 9 },
196 /* Special names. */
197 { "ip", 12 },
123dc839 198 { "lr", 14 },
123dc839
DJ
199 /* Names used by GCC (not listed in the ARM EABI). */
200 { "sl", 10 },
123dc839
DJ
201 /* A special name from the older ATPCS. */
202 { "wr", 7 },
203};
bc90b915 204
123dc839 205static const char *const arm_register_names[] =
da59e081
JM
206{"r0", "r1", "r2", "r3", /* 0 1 2 3 */
207 "r4", "r5", "r6", "r7", /* 4 5 6 7 */
208 "r8", "r9", "r10", "r11", /* 8 9 10 11 */
209 "r12", "sp", "lr", "pc", /* 12 13 14 15 */
210 "f0", "f1", "f2", "f3", /* 16 17 18 19 */
211 "f4", "f5", "f6", "f7", /* 20 21 22 23 */
94c30b78 212 "fps", "cpsr" }; /* 24 25 */
ed9a39eb 213
afd7eef0
RE
214/* Valid register name styles. */
215static const char **valid_disassembly_styles;
ed9a39eb 216
afd7eef0
RE
217/* Disassembly style to use. Default to "std" register names. */
218static const char *disassembly_style;
96baa820 219
ed9a39eb 220/* This is used to keep the bfd arch_info in sync with the disassembly
afd7eef0
RE
221 style. */
222static void set_disassembly_style_sfunc(char *, int,
ed9a39eb 223 struct cmd_list_element *);
afd7eef0 224static void set_disassembly_style (void);
ed9a39eb 225
b508a996 226static void convert_from_extended (const struct floatformat *, const void *,
be8626e0 227 void *, int);
b508a996 228static void convert_to_extended (const struct floatformat *, void *,
be8626e0 229 const void *, int);
ed9a39eb 230
05d1431c
PA
231static enum register_status arm_neon_quad_read (struct gdbarch *gdbarch,
232 struct regcache *regcache,
233 int regnum, gdb_byte *buf);
58d6951d
DJ
234static void arm_neon_quad_write (struct gdbarch *gdbarch,
235 struct regcache *regcache,
236 int regnum, const gdb_byte *buf);
237
db24da6d
YQ
238static int thumb_insn_size (unsigned short inst1);
239
9b8d791a 240struct arm_prologue_cache
c3b4394c 241{
eb5492fa
DJ
242 /* The stack pointer at the time this frame was created; i.e. the
243 caller's stack pointer when this function was called. It is used
244 to identify this frame. */
245 CORE_ADDR prev_sp;
246
4be43953
DJ
247 /* The frame base for this frame is just prev_sp - frame size.
248 FRAMESIZE is the distance from the frame pointer to the
249 initial stack pointer. */
eb5492fa 250
c3b4394c 251 int framesize;
eb5492fa
DJ
252
253 /* The register used to hold the frame pointer for this frame. */
c3b4394c 254 int framereg;
eb5492fa
DJ
255
256 /* Saved register offsets. */
257 struct trad_frame_saved_reg *saved_regs;
c3b4394c 258};
ed9a39eb 259
0d39a070
DJ
260static CORE_ADDR arm_analyze_prologue (struct gdbarch *gdbarch,
261 CORE_ADDR prologue_start,
262 CORE_ADDR prologue_end,
263 struct arm_prologue_cache *cache);
264
cca44b1b
JB
265/* Architecture version for displaced stepping. This effects the behaviour of
266 certain instructions, and really should not be hard-wired. */
267
268#define DISPLACED_STEPPING_ARCH_VERSION 5
269
bc90b915
FN
270/* Addresses for calling Thumb functions have the bit 0 set.
271 Here are some macros to test, set, or clear bit 0 of addresses. */
272#define IS_THUMB_ADDR(addr) ((addr) & 1)
273#define MAKE_THUMB_ADDR(addr) ((addr) | 1)
274#define UNMAKE_THUMB_ADDR(addr) ((addr) & ~1)
275
94c30b78 276/* Set to true if the 32-bit mode is in use. */
c906108c
SS
277
278int arm_apcs_32 = 1;
279
9779414d
DJ
280/* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode. */
281
478fd957 282int
9779414d
DJ
283arm_psr_thumb_bit (struct gdbarch *gdbarch)
284{
285 if (gdbarch_tdep (gdbarch)->is_m)
286 return XPSR_T;
287 else
288 return CPSR_T;
289}
290
b39cc962
DJ
291/* Determine if FRAME is executing in Thumb mode. */
292
25b41d01 293int
b39cc962
DJ
294arm_frame_is_thumb (struct frame_info *frame)
295{
296 CORE_ADDR cpsr;
9779414d 297 ULONGEST t_bit = arm_psr_thumb_bit (get_frame_arch (frame));
b39cc962
DJ
298
299 /* Every ARM frame unwinder can unwind the T bit of the CPSR, either
300 directly (from a signal frame or dummy frame) or by interpreting
301 the saved LR (from a prologue or DWARF frame). So consult it and
302 trust the unwinders. */
303 cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
304
9779414d 305 return (cpsr & t_bit) != 0;
b39cc962
DJ
306}
307
60c5725c
DJ
308/* Callback for VEC_lower_bound. */
309
310static inline int
311arm_compare_mapping_symbols (const struct arm_mapping_symbol *lhs,
312 const struct arm_mapping_symbol *rhs)
313{
314 return lhs->value < rhs->value;
315}
316
f9d67f43
DJ
317/* Search for the mapping symbol covering MEMADDR. If one is found,
318 return its type. Otherwise, return 0. If START is non-NULL,
319 set *START to the location of the mapping symbol. */
c906108c 320
f9d67f43
DJ
321static char
322arm_find_mapping_symbol (CORE_ADDR memaddr, CORE_ADDR *start)
c906108c 323{
60c5725c 324 struct obj_section *sec;
0428b8f5 325
60c5725c
DJ
326 /* If there are mapping symbols, consult them. */
327 sec = find_pc_section (memaddr);
328 if (sec != NULL)
329 {
330 struct arm_per_objfile *data;
331 VEC(arm_mapping_symbol_s) *map;
aded6f54
PA
332 struct arm_mapping_symbol map_key = { memaddr - obj_section_addr (sec),
333 0 };
60c5725c
DJ
334 unsigned int idx;
335
336 data = objfile_data (sec->objfile, arm_objfile_data_key);
337 if (data != NULL)
338 {
339 map = data->section_maps[sec->the_bfd_section->index];
340 if (!VEC_empty (arm_mapping_symbol_s, map))
341 {
342 struct arm_mapping_symbol *map_sym;
343
344 idx = VEC_lower_bound (arm_mapping_symbol_s, map, &map_key,
345 arm_compare_mapping_symbols);
346
347 /* VEC_lower_bound finds the earliest ordered insertion
348 point. If the following symbol starts at this exact
349 address, we use that; otherwise, the preceding
350 mapping symbol covers this address. */
351 if (idx < VEC_length (arm_mapping_symbol_s, map))
352 {
353 map_sym = VEC_index (arm_mapping_symbol_s, map, idx);
354 if (map_sym->value == map_key.value)
f9d67f43
DJ
355 {
356 if (start)
357 *start = map_sym->value + obj_section_addr (sec);
358 return map_sym->type;
359 }
60c5725c
DJ
360 }
361
362 if (idx > 0)
363 {
364 map_sym = VEC_index (arm_mapping_symbol_s, map, idx - 1);
f9d67f43
DJ
365 if (start)
366 *start = map_sym->value + obj_section_addr (sec);
367 return map_sym->type;
60c5725c
DJ
368 }
369 }
370 }
371 }
372
f9d67f43
DJ
373 return 0;
374}
375
376/* Determine if the program counter specified in MEMADDR is in a Thumb
377 function. This function should be called for addresses unrelated to
378 any executing frame; otherwise, prefer arm_frame_is_thumb. */
379
e3039479 380int
9779414d 381arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
f9d67f43 382{
7cbd4a93 383 struct bound_minimal_symbol sym;
f9d67f43 384 char type;
a42244db
YQ
385 struct displaced_step_closure* dsc
386 = get_displaced_step_closure_by_addr(memaddr);
387
388 /* If checking the mode of displaced instruction in copy area, the mode
389 should be determined by instruction on the original address. */
390 if (dsc)
391 {
392 if (debug_displaced)
393 fprintf_unfiltered (gdb_stdlog,
394 "displaced: check mode of %.8lx instead of %.8lx\n",
395 (unsigned long) dsc->insn_addr,
396 (unsigned long) memaddr);
397 memaddr = dsc->insn_addr;
398 }
f9d67f43
DJ
399
400 /* If bit 0 of the address is set, assume this is a Thumb address. */
401 if (IS_THUMB_ADDR (memaddr))
402 return 1;
403
18819fa6
UW
404 /* Respect internal mode override if active. */
405 if (arm_override_mode != -1)
406 return arm_override_mode;
407
f9d67f43
DJ
408 /* If the user wants to override the symbol table, let him. */
409 if (strcmp (arm_force_mode_string, "arm") == 0)
410 return 0;
411 if (strcmp (arm_force_mode_string, "thumb") == 0)
412 return 1;
413
9779414d
DJ
414 /* ARM v6-M and v7-M are always in Thumb mode. */
415 if (gdbarch_tdep (gdbarch)->is_m)
416 return 1;
417
f9d67f43
DJ
418 /* If there are mapping symbols, consult them. */
419 type = arm_find_mapping_symbol (memaddr, NULL);
420 if (type)
421 return type == 't';
422
ed9a39eb 423 /* Thumb functions have a "special" bit set in minimal symbols. */
c906108c 424 sym = lookup_minimal_symbol_by_pc (memaddr);
7cbd4a93
TT
425 if (sym.minsym)
426 return (MSYMBOL_IS_SPECIAL (sym.minsym));
0428b8f5
DJ
427
428 /* If the user wants to override the fallback mode, let them. */
429 if (strcmp (arm_fallback_mode_string, "arm") == 0)
430 return 0;
431 if (strcmp (arm_fallback_mode_string, "thumb") == 0)
432 return 1;
433
434 /* If we couldn't find any symbol, but we're talking to a running
435 target, then trust the current value of $cpsr. This lets
436 "display/i $pc" always show the correct mode (though if there is
437 a symbol table we will not reach here, so it still may not be
18819fa6 438 displayed in the mode it will be executed). */
0428b8f5 439 if (target_has_registers)
18819fa6 440 return arm_frame_is_thumb (get_current_frame ());
0428b8f5
DJ
441
442 /* Otherwise we're out of luck; we assume ARM. */
443 return 0;
c906108c
SS
444}
445
181c1381 446/* Remove useless bits from addresses in a running program. */
34e8f22d 447static CORE_ADDR
24568a2c 448arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
c906108c 449{
2ae28aa9
YQ
450 /* On M-profile devices, do not strip the low bit from EXC_RETURN
451 (the magic exception return address). */
452 if (gdbarch_tdep (gdbarch)->is_m
453 && (val & 0xfffffff0) == 0xfffffff0)
454 return val;
455
a3a2ee65 456 if (arm_apcs_32)
dd6be234 457 return UNMAKE_THUMB_ADDR (val);
c906108c 458 else
a3a2ee65 459 return (val & 0x03fffffc);
c906108c
SS
460}
461
0d39a070 462/* Return 1 if PC is the start of a compiler helper function which
e0634ccf
UW
463 can be safely ignored during prologue skipping. IS_THUMB is true
464 if the function is known to be a Thumb function due to the way it
465 is being called. */
0d39a070 466static int
e0634ccf 467skip_prologue_function (struct gdbarch *gdbarch, CORE_ADDR pc, int is_thumb)
0d39a070 468{
e0634ccf 469 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7cbd4a93 470 struct bound_minimal_symbol msym;
0d39a070
DJ
471
472 msym = lookup_minimal_symbol_by_pc (pc);
7cbd4a93 473 if (msym.minsym != NULL
77e371c0 474 && BMSYMBOL_VALUE_ADDRESS (msym) == pc
efd66ac6 475 && MSYMBOL_LINKAGE_NAME (msym.minsym) != NULL)
e0634ccf 476 {
efd66ac6 477 const char *name = MSYMBOL_LINKAGE_NAME (msym.minsym);
0d39a070 478
e0634ccf
UW
479 /* The GNU linker's Thumb call stub to foo is named
480 __foo_from_thumb. */
481 if (strstr (name, "_from_thumb") != NULL)
482 name += 2;
0d39a070 483
e0634ccf
UW
484 /* On soft-float targets, __truncdfsf2 is called to convert promoted
485 arguments to their argument types in non-prototyped
486 functions. */
487 if (strncmp (name, "__truncdfsf2", strlen ("__truncdfsf2")) == 0)
488 return 1;
489 if (strncmp (name, "__aeabi_d2f", strlen ("__aeabi_d2f")) == 0)
490 return 1;
0d39a070 491
e0634ccf
UW
492 /* Internal functions related to thread-local storage. */
493 if (strncmp (name, "__tls_get_addr", strlen ("__tls_get_addr")) == 0)
494 return 1;
495 if (strncmp (name, "__aeabi_read_tp", strlen ("__aeabi_read_tp")) == 0)
496 return 1;
497 }
498 else
499 {
500 /* If we run against a stripped glibc, we may be unable to identify
501 special functions by name. Check for one important case,
502 __aeabi_read_tp, by comparing the *code* against the default
503 implementation (this is hand-written ARM assembler in glibc). */
504
505 if (!is_thumb
506 && read_memory_unsigned_integer (pc, 4, byte_order_for_code)
507 == 0xe3e00a0f /* mov r0, #0xffff0fff */
508 && read_memory_unsigned_integer (pc + 4, 4, byte_order_for_code)
509 == 0xe240f01f) /* sub pc, r0, #31 */
510 return 1;
511 }
ec3d575a 512
0d39a070
DJ
513 return 0;
514}
515
516/* Support routines for instruction parsing. */
517#define submask(x) ((1L << ((x) + 1)) - 1)
518#define bit(obj,st) (((obj) >> (st)) & 1)
519#define bits(obj,st,fn) (((obj) >> (st)) & submask ((fn) - (st)))
520#define sbits(obj,st,fn) \
521 ((long) (bits(obj,st,fn) | ((long) bit(obj,fn) * ~ submask (fn - st))))
522#define BranchDest(addr,instr) \
9991b207 523 ((CORE_ADDR) (((unsigned long) (addr)) + 8 + (sbits (instr, 0, 23) << 2)))
0d39a070 524
621c6d5b
YQ
525/* Extract the immediate from instruction movw/movt of encoding T. INSN1 is
526 the first 16-bit of instruction, and INSN2 is the second 16-bit of
527 instruction. */
528#define EXTRACT_MOVW_MOVT_IMM_T(insn1, insn2) \
529 ((bits ((insn1), 0, 3) << 12) \
530 | (bits ((insn1), 10, 10) << 11) \
531 | (bits ((insn2), 12, 14) << 8) \
532 | bits ((insn2), 0, 7))
533
534/* Extract the immediate from instruction movw/movt of encoding A. INSN is
535 the 32-bit instruction. */
536#define EXTRACT_MOVW_MOVT_IMM_A(insn) \
537 ((bits ((insn), 16, 19) << 12) \
538 | bits ((insn), 0, 11))
539
ec3d575a
UW
540/* Decode immediate value; implements ThumbExpandImmediate pseudo-op. */
541
542static unsigned int
543thumb_expand_immediate (unsigned int imm)
544{
545 unsigned int count = imm >> 7;
546
547 if (count < 8)
548 switch (count / 2)
549 {
550 case 0:
551 return imm & 0xff;
552 case 1:
553 return (imm & 0xff) | ((imm & 0xff) << 16);
554 case 2:
555 return ((imm & 0xff) << 8) | ((imm & 0xff) << 24);
556 case 3:
557 return (imm & 0xff) | ((imm & 0xff) << 8)
558 | ((imm & 0xff) << 16) | ((imm & 0xff) << 24);
559 }
560
561 return (0x80 | (imm & 0x7f)) << (32 - count);
562}
563
564/* Return 1 if the 16-bit Thumb instruction INST might change
565 control flow, 0 otherwise. */
566
567static int
568thumb_instruction_changes_pc (unsigned short inst)
569{
570 if ((inst & 0xff00) == 0xbd00) /* pop {rlist, pc} */
571 return 1;
572
573 if ((inst & 0xf000) == 0xd000) /* conditional branch */
574 return 1;
575
576 if ((inst & 0xf800) == 0xe000) /* unconditional branch */
577 return 1;
578
579 if ((inst & 0xff00) == 0x4700) /* bx REG, blx REG */
580 return 1;
581
ad8b5167
UW
582 if ((inst & 0xff87) == 0x4687) /* mov pc, REG */
583 return 1;
584
ec3d575a
UW
585 if ((inst & 0xf500) == 0xb100) /* CBNZ or CBZ. */
586 return 1;
587
588 return 0;
589}
590
591/* Return 1 if the 32-bit Thumb instruction in INST1 and INST2
592 might change control flow, 0 otherwise. */
593
594static int
595thumb2_instruction_changes_pc (unsigned short inst1, unsigned short inst2)
596{
597 if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
598 {
599 /* Branches and miscellaneous control instructions. */
600
601 if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
602 {
603 /* B, BL, BLX. */
604 return 1;
605 }
606 else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
607 {
608 /* SUBS PC, LR, #imm8. */
609 return 1;
610 }
611 else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
612 {
613 /* Conditional branch. */
614 return 1;
615 }
616
617 return 0;
618 }
619
620 if ((inst1 & 0xfe50) == 0xe810)
621 {
622 /* Load multiple or RFE. */
623
624 if (bit (inst1, 7) && !bit (inst1, 8))
625 {
626 /* LDMIA or POP */
627 if (bit (inst2, 15))
628 return 1;
629 }
630 else if (!bit (inst1, 7) && bit (inst1, 8))
631 {
632 /* LDMDB */
633 if (bit (inst2, 15))
634 return 1;
635 }
636 else if (bit (inst1, 7) && bit (inst1, 8))
637 {
638 /* RFEIA */
639 return 1;
640 }
641 else if (!bit (inst1, 7) && !bit (inst1, 8))
642 {
643 /* RFEDB */
644 return 1;
645 }
646
647 return 0;
648 }
649
650 if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
651 {
652 /* MOV PC or MOVS PC. */
653 return 1;
654 }
655
656 if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
657 {
658 /* LDR PC. */
659 if (bits (inst1, 0, 3) == 15)
660 return 1;
661 if (bit (inst1, 7))
662 return 1;
663 if (bit (inst2, 11))
664 return 1;
665 if ((inst2 & 0x0fc0) == 0x0000)
666 return 1;
667
668 return 0;
669 }
670
671 if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
672 {
673 /* TBB. */
674 return 1;
675 }
676
677 if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
678 {
679 /* TBH. */
680 return 1;
681 }
682
683 return 0;
684}
685
540314bd
YQ
686/* Return 1 if the 16-bit Thumb instruction INSN restores SP in
687 epilogue, 0 otherwise. */
688
689static int
690thumb_instruction_restores_sp (unsigned short insn)
691{
692 return (insn == 0x46bd /* mov sp, r7 */
693 || (insn & 0xff80) == 0xb000 /* add sp, imm */
694 || (insn & 0xfe00) == 0xbc00); /* pop <registers> */
695}
696
29d73ae4
DJ
697/* Analyze a Thumb prologue, looking for a recognizable stack frame
698 and frame pointer. Scan until we encounter a store that could
0d39a070
DJ
699 clobber the stack frame unexpectedly, or an unknown instruction.
700 Return the last address which is definitely safe to skip for an
701 initial breakpoint. */
c906108c
SS
702
703static CORE_ADDR
29d73ae4
DJ
704thumb_analyze_prologue (struct gdbarch *gdbarch,
705 CORE_ADDR start, CORE_ADDR limit,
706 struct arm_prologue_cache *cache)
c906108c 707{
0d39a070 708 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
e17a4113 709 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
29d73ae4
DJ
710 int i;
711 pv_t regs[16];
712 struct pv_area *stack;
713 struct cleanup *back_to;
714 CORE_ADDR offset;
ec3d575a 715 CORE_ADDR unrecognized_pc = 0;
da3c6d4a 716
29d73ae4
DJ
717 for (i = 0; i < 16; i++)
718 regs[i] = pv_register (i, 0);
55f960e1 719 stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
29d73ae4
DJ
720 back_to = make_cleanup_free_pv_area (stack);
721
29d73ae4 722 while (start < limit)
c906108c 723 {
29d73ae4
DJ
724 unsigned short insn;
725
e17a4113 726 insn = read_memory_unsigned_integer (start, 2, byte_order_for_code);
9d4fde75 727
94c30b78 728 if ((insn & 0xfe00) == 0xb400) /* push { rlist } */
da59e081 729 {
29d73ae4
DJ
730 int regno;
731 int mask;
4be43953
DJ
732
733 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
734 break;
29d73ae4
DJ
735
736 /* Bits 0-7 contain a mask for registers R0-R7. Bit 8 says
737 whether to save LR (R14). */
738 mask = (insn & 0xff) | ((insn & 0x100) << 6);
739
740 /* Calculate offsets of saved R0-R7 and LR. */
741 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
742 if (mask & (1 << regno))
743 {
29d73ae4
DJ
744 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
745 -4);
746 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
747 }
da59e081 748 }
1db01f22 749 else if ((insn & 0xff80) == 0xb080) /* sub sp, #imm */
da59e081 750 {
29d73ae4 751 offset = (insn & 0x7f) << 2; /* get scaled offset */
1db01f22
YQ
752 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
753 -offset);
da59e081 754 }
808f7ab1
YQ
755 else if (thumb_instruction_restores_sp (insn))
756 {
757 /* Don't scan past the epilogue. */
758 break;
759 }
0d39a070
DJ
760 else if ((insn & 0xf800) == 0xa800) /* add Rd, sp, #imm */
761 regs[bits (insn, 8, 10)] = pv_add_constant (regs[ARM_SP_REGNUM],
762 (insn & 0xff) << 2);
763 else if ((insn & 0xfe00) == 0x1c00 /* add Rd, Rn, #imm */
764 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
765 regs[bits (insn, 0, 2)] = pv_add_constant (regs[bits (insn, 3, 5)],
766 bits (insn, 6, 8));
767 else if ((insn & 0xf800) == 0x3000 /* add Rd, #imm */
768 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
769 regs[bits (insn, 8, 10)] = pv_add_constant (regs[bits (insn, 8, 10)],
770 bits (insn, 0, 7));
771 else if ((insn & 0xfe00) == 0x1800 /* add Rd, Rn, Rm */
772 && pv_is_register (regs[bits (insn, 6, 8)], ARM_SP_REGNUM)
773 && pv_is_constant (regs[bits (insn, 3, 5)]))
774 regs[bits (insn, 0, 2)] = pv_add (regs[bits (insn, 3, 5)],
775 regs[bits (insn, 6, 8)]);
776 else if ((insn & 0xff00) == 0x4400 /* add Rd, Rm */
777 && pv_is_constant (regs[bits (insn, 3, 6)]))
778 {
779 int rd = (bit (insn, 7) << 3) + bits (insn, 0, 2);
780 int rm = bits (insn, 3, 6);
781 regs[rd] = pv_add (regs[rd], regs[rm]);
782 }
29d73ae4 783 else if ((insn & 0xff00) == 0x4600) /* mov hi, lo or mov lo, hi */
da59e081 784 {
29d73ae4
DJ
785 int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
786 int src_reg = (insn & 0x78) >> 3;
787 regs[dst_reg] = regs[src_reg];
da59e081 788 }
29d73ae4 789 else if ((insn & 0xf800) == 0x9000) /* str rd, [sp, #off] */
da59e081 790 {
29d73ae4
DJ
791 /* Handle stores to the stack. Normally pushes are used,
792 but with GCC -mtpcs-frame, there may be other stores
793 in the prologue to create the frame. */
794 int regno = (insn >> 8) & 0x7;
795 pv_t addr;
796
797 offset = (insn & 0xff) << 2;
798 addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
799
800 if (pv_area_store_would_trash (stack, addr))
801 break;
802
803 pv_area_store (stack, addr, 4, regs[regno]);
da59e081 804 }
0d39a070
DJ
805 else if ((insn & 0xf800) == 0x6000) /* str rd, [rn, #off] */
806 {
807 int rd = bits (insn, 0, 2);
808 int rn = bits (insn, 3, 5);
809 pv_t addr;
810
811 offset = bits (insn, 6, 10) << 2;
812 addr = pv_add_constant (regs[rn], offset);
813
814 if (pv_area_store_would_trash (stack, addr))
815 break;
816
817 pv_area_store (stack, addr, 4, regs[rd]);
818 }
819 else if (((insn & 0xf800) == 0x7000 /* strb Rd, [Rn, #off] */
820 || (insn & 0xf800) == 0x8000) /* strh Rd, [Rn, #off] */
821 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
822 /* Ignore stores of argument registers to the stack. */
823 ;
824 else if ((insn & 0xf800) == 0xc800 /* ldmia Rn!, { registers } */
825 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
826 /* Ignore block loads from the stack, potentially copying
827 parameters from memory. */
828 ;
829 else if ((insn & 0xf800) == 0x9800 /* ldr Rd, [Rn, #immed] */
830 || ((insn & 0xf800) == 0x6800 /* ldr Rd, [sp, #immed] */
831 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM)))
832 /* Similarly ignore single loads from the stack. */
833 ;
834 else if ((insn & 0xffc0) == 0x0000 /* lsls Rd, Rm, #0 */
835 || (insn & 0xffc0) == 0x1c00) /* add Rd, Rn, #0 */
836 /* Skip register copies, i.e. saves to another register
837 instead of the stack. */
838 ;
839 else if ((insn & 0xf800) == 0x2000) /* movs Rd, #imm */
840 /* Recognize constant loads; even with small stacks these are necessary
841 on Thumb. */
842 regs[bits (insn, 8, 10)] = pv_constant (bits (insn, 0, 7));
843 else if ((insn & 0xf800) == 0x4800) /* ldr Rd, [pc, #imm] */
844 {
845 /* Constant pool loads, for the same reason. */
846 unsigned int constant;
847 CORE_ADDR loc;
848
849 loc = start + 4 + bits (insn, 0, 7) * 4;
850 constant = read_memory_unsigned_integer (loc, 4, byte_order);
851 regs[bits (insn, 8, 10)] = pv_constant (constant);
852 }
db24da6d 853 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instructions. */
0d39a070 854 {
0d39a070
DJ
855 unsigned short inst2;
856
857 inst2 = read_memory_unsigned_integer (start + 2, 2,
858 byte_order_for_code);
859
860 if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
861 {
862 /* BL, BLX. Allow some special function calls when
863 skipping the prologue; GCC generates these before
864 storing arguments to the stack. */
865 CORE_ADDR nextpc;
866 int j1, j2, imm1, imm2;
867
868 imm1 = sbits (insn, 0, 10);
869 imm2 = bits (inst2, 0, 10);
870 j1 = bit (inst2, 13);
871 j2 = bit (inst2, 11);
872
873 offset = ((imm1 << 12) + (imm2 << 1));
874 offset ^= ((!j2) << 22) | ((!j1) << 23);
875
876 nextpc = start + 4 + offset;
877 /* For BLX make sure to clear the low bits. */
878 if (bit (inst2, 12) == 0)
879 nextpc = nextpc & 0xfffffffc;
880
e0634ccf
UW
881 if (!skip_prologue_function (gdbarch, nextpc,
882 bit (inst2, 12) != 0))
0d39a070
DJ
883 break;
884 }
ec3d575a 885
0963b4bd
MS
886 else if ((insn & 0xffd0) == 0xe900 /* stmdb Rn{!},
887 { registers } */
ec3d575a
UW
888 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
889 {
890 pv_t addr = regs[bits (insn, 0, 3)];
891 int regno;
892
893 if (pv_area_store_would_trash (stack, addr))
894 break;
895
896 /* Calculate offsets of saved registers. */
897 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
898 if (inst2 & (1 << regno))
899 {
900 addr = pv_add_constant (addr, -4);
901 pv_area_store (stack, addr, 4, regs[regno]);
902 }
903
904 if (insn & 0x0020)
905 regs[bits (insn, 0, 3)] = addr;
906 }
907
0963b4bd
MS
908 else if ((insn & 0xff50) == 0xe940 /* strd Rt, Rt2,
909 [Rn, #+/-imm]{!} */
ec3d575a
UW
910 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
911 {
912 int regno1 = bits (inst2, 12, 15);
913 int regno2 = bits (inst2, 8, 11);
914 pv_t addr = regs[bits (insn, 0, 3)];
915
916 offset = inst2 & 0xff;
917 if (insn & 0x0080)
918 addr = pv_add_constant (addr, offset);
919 else
920 addr = pv_add_constant (addr, -offset);
921
922 if (pv_area_store_would_trash (stack, addr))
923 break;
924
925 pv_area_store (stack, addr, 4, regs[regno1]);
926 pv_area_store (stack, pv_add_constant (addr, 4),
927 4, regs[regno2]);
928
929 if (insn & 0x0020)
930 regs[bits (insn, 0, 3)] = addr;
931 }
932
933 else if ((insn & 0xfff0) == 0xf8c0 /* str Rt,[Rn,+/-#imm]{!} */
934 && (inst2 & 0x0c00) == 0x0c00
935 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
936 {
937 int regno = bits (inst2, 12, 15);
938 pv_t addr = regs[bits (insn, 0, 3)];
939
940 offset = inst2 & 0xff;
941 if (inst2 & 0x0200)
942 addr = pv_add_constant (addr, offset);
943 else
944 addr = pv_add_constant (addr, -offset);
945
946 if (pv_area_store_would_trash (stack, addr))
947 break;
948
949 pv_area_store (stack, addr, 4, regs[regno]);
950
951 if (inst2 & 0x0100)
952 regs[bits (insn, 0, 3)] = addr;
953 }
954
955 else if ((insn & 0xfff0) == 0xf8c0 /* str.w Rt,[Rn,#imm] */
956 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
957 {
958 int regno = bits (inst2, 12, 15);
959 pv_t addr;
960
961 offset = inst2 & 0xfff;
962 addr = pv_add_constant (regs[bits (insn, 0, 3)], offset);
963
964 if (pv_area_store_would_trash (stack, addr))
965 break;
966
967 pv_area_store (stack, addr, 4, regs[regno]);
968 }
969
970 else if ((insn & 0xffd0) == 0xf880 /* str{bh}.w Rt,[Rn,#imm] */
0d39a070 971 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 972 /* Ignore stores of argument registers to the stack. */
0d39a070 973 ;
ec3d575a
UW
974
975 else if ((insn & 0xffd0) == 0xf800 /* str{bh} Rt,[Rn,#+/-imm] */
976 && (inst2 & 0x0d00) == 0x0c00
0d39a070 977 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 978 /* Ignore stores of argument registers to the stack. */
0d39a070 979 ;
ec3d575a 980
0963b4bd
MS
981 else if ((insn & 0xffd0) == 0xe890 /* ldmia Rn[!],
982 { registers } */
ec3d575a
UW
983 && (inst2 & 0x8000) == 0x0000
984 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
985 /* Ignore block loads from the stack, potentially copying
986 parameters from memory. */
0d39a070 987 ;
ec3d575a 988
0963b4bd
MS
989 else if ((insn & 0xffb0) == 0xe950 /* ldrd Rt, Rt2,
990 [Rn, #+/-imm] */
0d39a070 991 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 992 /* Similarly ignore dual loads from the stack. */
0d39a070 993 ;
ec3d575a
UW
994
995 else if ((insn & 0xfff0) == 0xf850 /* ldr Rt,[Rn,#+/-imm] */
996 && (inst2 & 0x0d00) == 0x0c00
0d39a070 997 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 998 /* Similarly ignore single loads from the stack. */
0d39a070 999 ;
ec3d575a
UW
1000
1001 else if ((insn & 0xfff0) == 0xf8d0 /* ldr.w Rt,[Rn,#imm] */
0d39a070 1002 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 1003 /* Similarly ignore single loads from the stack. */
0d39a070 1004 ;
ec3d575a
UW
1005
1006 else if ((insn & 0xfbf0) == 0xf100 /* add.w Rd, Rn, #imm */
1007 && (inst2 & 0x8000) == 0x0000)
1008 {
1009 unsigned int imm = ((bits (insn, 10, 10) << 11)
1010 | (bits (inst2, 12, 14) << 8)
1011 | bits (inst2, 0, 7));
1012
1013 regs[bits (inst2, 8, 11)]
1014 = pv_add_constant (regs[bits (insn, 0, 3)],
1015 thumb_expand_immediate (imm));
1016 }
1017
1018 else if ((insn & 0xfbf0) == 0xf200 /* addw Rd, Rn, #imm */
1019 && (inst2 & 0x8000) == 0x0000)
0d39a070 1020 {
ec3d575a
UW
1021 unsigned int imm = ((bits (insn, 10, 10) << 11)
1022 | (bits (inst2, 12, 14) << 8)
1023 | bits (inst2, 0, 7));
1024
1025 regs[bits (inst2, 8, 11)]
1026 = pv_add_constant (regs[bits (insn, 0, 3)], imm);
1027 }
1028
1029 else if ((insn & 0xfbf0) == 0xf1a0 /* sub.w Rd, Rn, #imm */
1030 && (inst2 & 0x8000) == 0x0000)
1031 {
1032 unsigned int imm = ((bits (insn, 10, 10) << 11)
1033 | (bits (inst2, 12, 14) << 8)
1034 | bits (inst2, 0, 7));
1035
1036 regs[bits (inst2, 8, 11)]
1037 = pv_add_constant (regs[bits (insn, 0, 3)],
1038 - (CORE_ADDR) thumb_expand_immediate (imm));
1039 }
1040
1041 else if ((insn & 0xfbf0) == 0xf2a0 /* subw Rd, Rn, #imm */
1042 && (inst2 & 0x8000) == 0x0000)
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_add_constant (regs[bits (insn, 0, 3)], - (CORE_ADDR) imm);
1050 }
1051
1052 else if ((insn & 0xfbff) == 0xf04f) /* mov.w Rd, #const */
1053 {
1054 unsigned int imm = ((bits (insn, 10, 10) << 11)
1055 | (bits (inst2, 12, 14) << 8)
1056 | bits (inst2, 0, 7));
1057
1058 regs[bits (inst2, 8, 11)]
1059 = pv_constant (thumb_expand_immediate (imm));
1060 }
1061
1062 else if ((insn & 0xfbf0) == 0xf240) /* movw Rd, #const */
1063 {
621c6d5b
YQ
1064 unsigned int imm
1065 = EXTRACT_MOVW_MOVT_IMM_T (insn, inst2);
ec3d575a
UW
1066
1067 regs[bits (inst2, 8, 11)] = pv_constant (imm);
1068 }
1069
1070 else if (insn == 0xea5f /* mov.w Rd,Rm */
1071 && (inst2 & 0xf0f0) == 0)
1072 {
1073 int dst_reg = (inst2 & 0x0f00) >> 8;
1074 int src_reg = inst2 & 0xf;
1075 regs[dst_reg] = regs[src_reg];
1076 }
1077
1078 else if ((insn & 0xff7f) == 0xf85f) /* ldr.w Rt,<label> */
1079 {
1080 /* Constant pool loads. */
1081 unsigned int constant;
1082 CORE_ADDR loc;
1083
cac395ea 1084 offset = bits (inst2, 0, 11);
ec3d575a
UW
1085 if (insn & 0x0080)
1086 loc = start + 4 + offset;
1087 else
1088 loc = start + 4 - offset;
1089
1090 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1091 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1092 }
1093
1094 else if ((insn & 0xff7f) == 0xe95f) /* ldrd Rt,Rt2,<label> */
1095 {
1096 /* Constant pool loads. */
1097 unsigned int constant;
1098 CORE_ADDR loc;
1099
cac395ea 1100 offset = bits (inst2, 0, 7) << 2;
ec3d575a
UW
1101 if (insn & 0x0080)
1102 loc = start + 4 + offset;
1103 else
1104 loc = start + 4 - offset;
1105
1106 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1107 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1108
1109 constant = read_memory_unsigned_integer (loc + 4, 4, byte_order);
1110 regs[bits (inst2, 8, 11)] = pv_constant (constant);
1111 }
1112
1113 else if (thumb2_instruction_changes_pc (insn, inst2))
1114 {
1115 /* Don't scan past anything that might change control flow. */
0d39a070
DJ
1116 break;
1117 }
ec3d575a
UW
1118 else
1119 {
1120 /* The optimizer might shove anything into the prologue,
1121 so we just skip what we don't recognize. */
1122 unrecognized_pc = start;
1123 }
0d39a070
DJ
1124
1125 start += 2;
1126 }
ec3d575a 1127 else if (thumb_instruction_changes_pc (insn))
3d74b771 1128 {
ec3d575a 1129 /* Don't scan past anything that might change control flow. */
da3c6d4a 1130 break;
3d74b771 1131 }
ec3d575a
UW
1132 else
1133 {
1134 /* The optimizer might shove anything into the prologue,
1135 so we just skip what we don't recognize. */
1136 unrecognized_pc = start;
1137 }
29d73ae4
DJ
1138
1139 start += 2;
c906108c
SS
1140 }
1141
0d39a070
DJ
1142 if (arm_debug)
1143 fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1144 paddress (gdbarch, start));
1145
ec3d575a
UW
1146 if (unrecognized_pc == 0)
1147 unrecognized_pc = start;
1148
29d73ae4
DJ
1149 if (cache == NULL)
1150 {
1151 do_cleanups (back_to);
ec3d575a 1152 return unrecognized_pc;
29d73ae4
DJ
1153 }
1154
29d73ae4
DJ
1155 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1156 {
1157 /* Frame pointer is fp. Frame size is constant. */
1158 cache->framereg = ARM_FP_REGNUM;
1159 cache->framesize = -regs[ARM_FP_REGNUM].k;
1160 }
1161 else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
1162 {
1163 /* Frame pointer is r7. Frame size is constant. */
1164 cache->framereg = THUMB_FP_REGNUM;
1165 cache->framesize = -regs[THUMB_FP_REGNUM].k;
1166 }
72a2e3dc 1167 else
29d73ae4
DJ
1168 {
1169 /* Try the stack pointer... this is a bit desperate. */
1170 cache->framereg = ARM_SP_REGNUM;
1171 cache->framesize = -regs[ARM_SP_REGNUM].k;
1172 }
29d73ae4
DJ
1173
1174 for (i = 0; i < 16; i++)
1175 if (pv_area_find_reg (stack, gdbarch, i, &offset))
1176 cache->saved_regs[i].addr = offset;
1177
1178 do_cleanups (back_to);
ec3d575a 1179 return unrecognized_pc;
c906108c
SS
1180}
1181
621c6d5b
YQ
1182
1183/* Try to analyze the instructions starting from PC, which load symbol
1184 __stack_chk_guard. Return the address of instruction after loading this
1185 symbol, set the dest register number to *BASEREG, and set the size of
1186 instructions for loading symbol in OFFSET. Return 0 if instructions are
1187 not recognized. */
1188
1189static CORE_ADDR
1190arm_analyze_load_stack_chk_guard(CORE_ADDR pc, struct gdbarch *gdbarch,
1191 unsigned int *destreg, int *offset)
1192{
1193 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1194 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1195 unsigned int low, high, address;
1196
1197 address = 0;
1198 if (is_thumb)
1199 {
1200 unsigned short insn1
1201 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
1202
1203 if ((insn1 & 0xf800) == 0x4800) /* ldr Rd, #immed */
1204 {
1205 *destreg = bits (insn1, 8, 10);
1206 *offset = 2;
6ae274b7
YQ
1207 address = (pc & 0xfffffffc) + 4 + (bits (insn1, 0, 7) << 2);
1208 address = read_memory_unsigned_integer (address, 4,
1209 byte_order_for_code);
621c6d5b
YQ
1210 }
1211 else if ((insn1 & 0xfbf0) == 0xf240) /* movw Rd, #const */
1212 {
1213 unsigned short insn2
1214 = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);
1215
1216 low = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1217
1218 insn1
1219 = read_memory_unsigned_integer (pc + 4, 2, byte_order_for_code);
1220 insn2
1221 = read_memory_unsigned_integer (pc + 6, 2, byte_order_for_code);
1222
1223 /* movt Rd, #const */
1224 if ((insn1 & 0xfbc0) == 0xf2c0)
1225 {
1226 high = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1227 *destreg = bits (insn2, 8, 11);
1228 *offset = 8;
1229 address = (high << 16 | low);
1230 }
1231 }
1232 }
1233 else
1234 {
2e9e421f
UW
1235 unsigned int insn
1236 = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
1237
6ae274b7 1238 if ((insn & 0x0e5f0000) == 0x041f0000) /* ldr Rd, [PC, #immed] */
2e9e421f 1239 {
6ae274b7
YQ
1240 address = bits (insn, 0, 11) + pc + 8;
1241 address = read_memory_unsigned_integer (address, 4,
1242 byte_order_for_code);
1243
2e9e421f
UW
1244 *destreg = bits (insn, 12, 15);
1245 *offset = 4;
1246 }
1247 else if ((insn & 0x0ff00000) == 0x03000000) /* movw Rd, #const */
1248 {
1249 low = EXTRACT_MOVW_MOVT_IMM_A (insn);
1250
1251 insn
1252 = read_memory_unsigned_integer (pc + 4, 4, byte_order_for_code);
1253
1254 if ((insn & 0x0ff00000) == 0x03400000) /* movt Rd, #const */
1255 {
1256 high = EXTRACT_MOVW_MOVT_IMM_A (insn);
1257 *destreg = bits (insn, 12, 15);
1258 *offset = 8;
1259 address = (high << 16 | low);
1260 }
1261 }
621c6d5b
YQ
1262 }
1263
1264 return address;
1265}
1266
1267/* Try to skip a sequence of instructions used for stack protector. If PC
0963b4bd
MS
1268 points to the first instruction of this sequence, return the address of
1269 first instruction after this sequence, otherwise, return original PC.
621c6d5b
YQ
1270
1271 On arm, this sequence of instructions is composed of mainly three steps,
1272 Step 1: load symbol __stack_chk_guard,
1273 Step 2: load from address of __stack_chk_guard,
1274 Step 3: store it to somewhere else.
1275
1276 Usually, instructions on step 2 and step 3 are the same on various ARM
1277 architectures. On step 2, it is one instruction 'ldr Rx, [Rn, #0]', and
1278 on step 3, it is also one instruction 'str Rx, [r7, #immd]'. However,
1279 instructions in step 1 vary from different ARM architectures. On ARMv7,
1280 they are,
1281
1282 movw Rn, #:lower16:__stack_chk_guard
1283 movt Rn, #:upper16:__stack_chk_guard
1284
1285 On ARMv5t, it is,
1286
1287 ldr Rn, .Label
1288 ....
1289 .Lable:
1290 .word __stack_chk_guard
1291
1292 Since ldr/str is a very popular instruction, we can't use them as
1293 'fingerprint' or 'signature' of stack protector sequence. Here we choose
1294 sequence {movw/movt, ldr}/ldr/str plus symbol __stack_chk_guard, if not
1295 stripped, as the 'fingerprint' of a stack protector cdoe sequence. */
1296
1297static CORE_ADDR
1298arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
1299{
1300 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
22e048c9 1301 unsigned int basereg;
7cbd4a93 1302 struct bound_minimal_symbol stack_chk_guard;
621c6d5b
YQ
1303 int offset;
1304 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1305 CORE_ADDR addr;
1306
1307 /* Try to parse the instructions in Step 1. */
1308 addr = arm_analyze_load_stack_chk_guard (pc, gdbarch,
1309 &basereg, &offset);
1310 if (!addr)
1311 return pc;
1312
1313 stack_chk_guard = lookup_minimal_symbol_by_pc (addr);
6041179a
JB
1314 /* ADDR must correspond to a symbol whose name is __stack_chk_guard.
1315 Otherwise, this sequence cannot be for stack protector. */
1316 if (stack_chk_guard.minsym == NULL
1317 || strncmp (MSYMBOL_LINKAGE_NAME (stack_chk_guard.minsym),
7cbd4a93 1318 "__stack_chk_guard",
c1c2ab58 1319 strlen ("__stack_chk_guard")) != 0)
621c6d5b
YQ
1320 return pc;
1321
1322 if (is_thumb)
1323 {
1324 unsigned int destreg;
1325 unsigned short insn
1326 = read_memory_unsigned_integer (pc + offset, 2, byte_order_for_code);
1327
1328 /* Step 2: ldr Rd, [Rn, #immed], encoding T1. */
1329 if ((insn & 0xf800) != 0x6800)
1330 return pc;
1331 if (bits (insn, 3, 5) != basereg)
1332 return pc;
1333 destreg = bits (insn, 0, 2);
1334
1335 insn = read_memory_unsigned_integer (pc + offset + 2, 2,
1336 byte_order_for_code);
1337 /* Step 3: str Rd, [Rn, #immed], encoding T1. */
1338 if ((insn & 0xf800) != 0x6000)
1339 return pc;
1340 if (destreg != bits (insn, 0, 2))
1341 return pc;
1342 }
1343 else
1344 {
1345 unsigned int destreg;
1346 unsigned int insn
1347 = read_memory_unsigned_integer (pc + offset, 4, byte_order_for_code);
1348
1349 /* Step 2: ldr Rd, [Rn, #immed], encoding A1. */
1350 if ((insn & 0x0e500000) != 0x04100000)
1351 return pc;
1352 if (bits (insn, 16, 19) != basereg)
1353 return pc;
1354 destreg = bits (insn, 12, 15);
1355 /* Step 3: str Rd, [Rn, #immed], encoding A1. */
1356 insn = read_memory_unsigned_integer (pc + offset + 4,
1357 4, byte_order_for_code);
1358 if ((insn & 0x0e500000) != 0x04000000)
1359 return pc;
1360 if (bits (insn, 12, 15) != destreg)
1361 return pc;
1362 }
1363 /* The size of total two instructions ldr/str is 4 on Thumb-2, while 8
1364 on arm. */
1365 if (is_thumb)
1366 return pc + offset + 4;
1367 else
1368 return pc + offset + 8;
1369}
1370
da3c6d4a
MS
1371/* Advance the PC across any function entry prologue instructions to
1372 reach some "real" code.
34e8f22d
RE
1373
1374 The APCS (ARM Procedure Call Standard) defines the following
ed9a39eb 1375 prologue:
c906108c 1376
c5aa993b
JM
1377 mov ip, sp
1378 [stmfd sp!, {a1,a2,a3,a4}]
1379 stmfd sp!, {...,fp,ip,lr,pc}
ed9a39eb
JM
1380 [stfe f7, [sp, #-12]!]
1381 [stfe f6, [sp, #-12]!]
1382 [stfe f5, [sp, #-12]!]
1383 [stfe f4, [sp, #-12]!]
0963b4bd 1384 sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn. */
c906108c 1385
34e8f22d 1386static CORE_ADDR
6093d2eb 1387arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
c906108c 1388{
e17a4113 1389 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
c906108c
SS
1390 unsigned long inst;
1391 CORE_ADDR skip_pc;
a89fea3c 1392 CORE_ADDR func_addr, limit_pc;
c906108c 1393
a89fea3c
JL
1394 /* See if we can determine the end of the prologue via the symbol table.
1395 If so, then return either PC, or the PC after the prologue, whichever
1396 is greater. */
1397 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
c906108c 1398 {
d80b854b
UW
1399 CORE_ADDR post_prologue_pc
1400 = skip_prologue_using_sal (gdbarch, func_addr);
43f3e411 1401 struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);
0d39a070 1402
621c6d5b
YQ
1403 if (post_prologue_pc)
1404 post_prologue_pc
1405 = arm_skip_stack_protector (post_prologue_pc, gdbarch);
1406
1407
0d39a070
DJ
1408 /* GCC always emits a line note before the prologue and another
1409 one after, even if the two are at the same address or on the
1410 same line. Take advantage of this so that we do not need to
1411 know every instruction that might appear in the prologue. We
1412 will have producer information for most binaries; if it is
1413 missing (e.g. for -gstabs), assuming the GNU tools. */
1414 if (post_prologue_pc
43f3e411
DE
1415 && (cust == NULL
1416 || COMPUNIT_PRODUCER (cust) == NULL
1417 || strncmp (COMPUNIT_PRODUCER (cust), "GNU ",
1418 sizeof ("GNU ") - 1) == 0
1419 || strncmp (COMPUNIT_PRODUCER (cust), "clang ",
1420 sizeof ("clang ") - 1) == 0))
0d39a070
DJ
1421 return post_prologue_pc;
1422
a89fea3c 1423 if (post_prologue_pc != 0)
0d39a070
DJ
1424 {
1425 CORE_ADDR analyzed_limit;
1426
1427 /* For non-GCC compilers, make sure the entire line is an
1428 acceptable prologue; GDB will round this function's
1429 return value up to the end of the following line so we
1430 can not skip just part of a line (and we do not want to).
1431
1432 RealView does not treat the prologue specially, but does
1433 associate prologue code with the opening brace; so this
1434 lets us skip the first line if we think it is the opening
1435 brace. */
9779414d 1436 if (arm_pc_is_thumb (gdbarch, func_addr))
0d39a070
DJ
1437 analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
1438 post_prologue_pc, NULL);
1439 else
1440 analyzed_limit = arm_analyze_prologue (gdbarch, func_addr,
1441 post_prologue_pc, NULL);
1442
1443 if (analyzed_limit != post_prologue_pc)
1444 return func_addr;
1445
1446 return post_prologue_pc;
1447 }
c906108c
SS
1448 }
1449
a89fea3c
JL
1450 /* Can't determine prologue from the symbol table, need to examine
1451 instructions. */
c906108c 1452
a89fea3c
JL
1453 /* Find an upper limit on the function prologue using the debug
1454 information. If the debug information could not be used to provide
1455 that bound, then use an arbitrary large number as the upper bound. */
0963b4bd 1456 /* Like arm_scan_prologue, stop no later than pc + 64. */
d80b854b 1457 limit_pc = skip_prologue_using_sal (gdbarch, pc);
a89fea3c
JL
1458 if (limit_pc == 0)
1459 limit_pc = pc + 64; /* Magic. */
1460
c906108c 1461
29d73ae4 1462 /* Check if this is Thumb code. */
9779414d 1463 if (arm_pc_is_thumb (gdbarch, pc))
a89fea3c 1464 return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
29d73ae4 1465
a89fea3c 1466 for (skip_pc = pc; skip_pc < limit_pc; skip_pc += 4)
f43845b3 1467 {
e17a4113 1468 inst = read_memory_unsigned_integer (skip_pc, 4, byte_order_for_code);
9d4fde75 1469
b8d5e71d
MS
1470 /* "mov ip, sp" is no longer a required part of the prologue. */
1471 if (inst == 0xe1a0c00d) /* mov ip, sp */
1472 continue;
c906108c 1473
28cd8767
JG
1474 if ((inst & 0xfffff000) == 0xe28dc000) /* add ip, sp #n */
1475 continue;
1476
1477 if ((inst & 0xfffff000) == 0xe24dc000) /* sub ip, sp #n */
1478 continue;
1479
b8d5e71d
MS
1480 /* Some prologues begin with "str lr, [sp, #-4]!". */
1481 if (inst == 0xe52de004) /* str lr, [sp, #-4]! */
1482 continue;
c906108c 1483
b8d5e71d
MS
1484 if ((inst & 0xfffffff0) == 0xe92d0000) /* stmfd sp!,{a1,a2,a3,a4} */
1485 continue;
c906108c 1486
b8d5e71d
MS
1487 if ((inst & 0xfffff800) == 0xe92dd800) /* stmfd sp!,{fp,ip,lr,pc} */
1488 continue;
11d3b27d 1489
b8d5e71d
MS
1490 /* Any insns after this point may float into the code, if it makes
1491 for better instruction scheduling, so we skip them only if we
1492 find them, but still consider the function to be frame-ful. */
f43845b3 1493
b8d5e71d
MS
1494 /* We may have either one sfmfd instruction here, or several stfe
1495 insns, depending on the version of floating point code we
1496 support. */
1497 if ((inst & 0xffbf0fff) == 0xec2d0200) /* sfmfd fn, <cnt>, [sp]! */
1498 continue;
1499
1500 if ((inst & 0xffff8fff) == 0xed6d0103) /* stfe fn, [sp, #-12]! */
1501 continue;
1502
1503 if ((inst & 0xfffff000) == 0xe24cb000) /* sub fp, ip, #nn */
1504 continue;
1505
1506 if ((inst & 0xfffff000) == 0xe24dd000) /* sub sp, sp, #nn */
1507 continue;
1508
f8bf5763
PM
1509 if ((inst & 0xffffc000) == 0xe54b0000 /* strb r(0123),[r11,#-nn] */
1510 || (inst & 0xffffc0f0) == 0xe14b00b0 /* strh r(0123),[r11,#-nn] */
1511 || (inst & 0xffffc000) == 0xe50b0000) /* str r(0123),[r11,#-nn] */
b8d5e71d
MS
1512 continue;
1513
f8bf5763
PM
1514 if ((inst & 0xffffc000) == 0xe5cd0000 /* strb r(0123),[sp,#nn] */
1515 || (inst & 0xffffc0f0) == 0xe1cd00b0 /* strh r(0123),[sp,#nn] */
1516 || (inst & 0xffffc000) == 0xe58d0000) /* str r(0123),[sp,#nn] */
b8d5e71d
MS
1517 continue;
1518
1519 /* Un-recognized instruction; stop scanning. */
1520 break;
f43845b3 1521 }
c906108c 1522
0963b4bd 1523 return skip_pc; /* End of prologue. */
c906108c 1524}
94c30b78 1525
c5aa993b 1526/* *INDENT-OFF* */
c906108c
SS
1527/* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
1528 This function decodes a Thumb function prologue to determine:
1529 1) the size of the stack frame
1530 2) which registers are saved on it
1531 3) the offsets of saved regs
1532 4) the offset from the stack pointer to the frame pointer
c906108c 1533
da59e081
JM
1534 A typical Thumb function prologue would create this stack frame
1535 (offsets relative to FP)
c906108c
SS
1536 old SP -> 24 stack parameters
1537 20 LR
1538 16 R7
1539 R7 -> 0 local variables (16 bytes)
1540 SP -> -12 additional stack space (12 bytes)
1541 The frame size would thus be 36 bytes, and the frame offset would be
0963b4bd 1542 12 bytes. The frame register is R7.
da59e081 1543
da3c6d4a
MS
1544 The comments for thumb_skip_prolog() describe the algorithm we use
1545 to detect the end of the prolog. */
c5aa993b
JM
1546/* *INDENT-ON* */
1547
c906108c 1548static void
be8626e0 1549thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
b39cc962 1550 CORE_ADDR block_addr, struct arm_prologue_cache *cache)
c906108c
SS
1551{
1552 CORE_ADDR prologue_start;
1553 CORE_ADDR prologue_end;
c906108c 1554
b39cc962
DJ
1555 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1556 &prologue_end))
c906108c 1557 {
ec3d575a
UW
1558 /* See comment in arm_scan_prologue for an explanation of
1559 this heuristics. */
1560 if (prologue_end > prologue_start + 64)
1561 {
1562 prologue_end = prologue_start + 64;
1563 }
c906108c
SS
1564 }
1565 else
f7060f85
DJ
1566 /* We're in the boondocks: we have no idea where the start of the
1567 function is. */
1568 return;
c906108c 1569
eb5492fa 1570 prologue_end = min (prologue_end, prev_pc);
c906108c 1571
be8626e0 1572 thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
1573}
1574
0d39a070 1575/* Return 1 if THIS_INSTR might change control flow, 0 otherwise. */
c906108c 1576
0d39a070
DJ
1577static int
1578arm_instruction_changes_pc (uint32_t this_instr)
c906108c 1579{
0d39a070
DJ
1580 if (bits (this_instr, 28, 31) == INST_NV)
1581 /* Unconditional instructions. */
1582 switch (bits (this_instr, 24, 27))
1583 {
1584 case 0xa:
1585 case 0xb:
1586 /* Branch with Link and change to Thumb. */
1587 return 1;
1588 case 0xc:
1589 case 0xd:
1590 case 0xe:
1591 /* Coprocessor register transfer. */
1592 if (bits (this_instr, 12, 15) == 15)
1593 error (_("Invalid update to pc in instruction"));
1594 return 0;
1595 default:
1596 return 0;
1597 }
1598 else
1599 switch (bits (this_instr, 25, 27))
1600 {
1601 case 0x0:
1602 if (bits (this_instr, 23, 24) == 2 && bit (this_instr, 20) == 0)
1603 {
1604 /* Multiplies and extra load/stores. */
1605 if (bit (this_instr, 4) == 1 && bit (this_instr, 7) == 1)
1606 /* Neither multiplies nor extension load/stores are allowed
1607 to modify PC. */
1608 return 0;
1609
1610 /* Otherwise, miscellaneous instructions. */
1611
1612 /* BX <reg>, BXJ <reg>, BLX <reg> */
1613 if (bits (this_instr, 4, 27) == 0x12fff1
1614 || bits (this_instr, 4, 27) == 0x12fff2
1615 || bits (this_instr, 4, 27) == 0x12fff3)
1616 return 1;
1617
1618 /* Other miscellaneous instructions are unpredictable if they
1619 modify PC. */
1620 return 0;
1621 }
1622 /* Data processing instruction. Fall through. */
c906108c 1623
0d39a070
DJ
1624 case 0x1:
1625 if (bits (this_instr, 12, 15) == 15)
1626 return 1;
1627 else
1628 return 0;
c906108c 1629
0d39a070
DJ
1630 case 0x2:
1631 case 0x3:
1632 /* Media instructions and architecturally undefined instructions. */
1633 if (bits (this_instr, 25, 27) == 3 && bit (this_instr, 4) == 1)
1634 return 0;
c906108c 1635
0d39a070
DJ
1636 /* Stores. */
1637 if (bit (this_instr, 20) == 0)
1638 return 0;
2a451106 1639
0d39a070
DJ
1640 /* Loads. */
1641 if (bits (this_instr, 12, 15) == ARM_PC_REGNUM)
1642 return 1;
1643 else
1644 return 0;
2a451106 1645
0d39a070
DJ
1646 case 0x4:
1647 /* Load/store multiple. */
1648 if (bit (this_instr, 20) == 1 && bit (this_instr, 15) == 1)
1649 return 1;
1650 else
1651 return 0;
2a451106 1652
0d39a070
DJ
1653 case 0x5:
1654 /* Branch and branch with link. */
1655 return 1;
2a451106 1656
0d39a070
DJ
1657 case 0x6:
1658 case 0x7:
1659 /* Coprocessor transfers or SWIs can not affect PC. */
1660 return 0;
eb5492fa 1661
0d39a070 1662 default:
9b20d036 1663 internal_error (__FILE__, __LINE__, _("bad value in switch"));
0d39a070
DJ
1664 }
1665}
c906108c 1666
0d39a070
DJ
1667/* Analyze an ARM mode prologue starting at PROLOGUE_START and
1668 continuing no further than PROLOGUE_END. If CACHE is non-NULL,
1669 fill it in. Return the first address not recognized as a prologue
1670 instruction.
eb5492fa 1671
0d39a070
DJ
1672 We recognize all the instructions typically found in ARM prologues,
1673 plus harmless instructions which can be skipped (either for analysis
1674 purposes, or a more restrictive set that can be skipped when finding
1675 the end of the prologue). */
1676
1677static CORE_ADDR
1678arm_analyze_prologue (struct gdbarch *gdbarch,
1679 CORE_ADDR prologue_start, CORE_ADDR prologue_end,
1680 struct arm_prologue_cache *cache)
1681{
1682 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1683 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1684 int regno;
1685 CORE_ADDR offset, current_pc;
1686 pv_t regs[ARM_FPS_REGNUM];
1687 struct pv_area *stack;
1688 struct cleanup *back_to;
0d39a070
DJ
1689 CORE_ADDR unrecognized_pc = 0;
1690
1691 /* Search the prologue looking for instructions that set up the
96baa820 1692 frame pointer, adjust the stack pointer, and save registers.
ed9a39eb 1693
96baa820
JM
1694 Be careful, however, and if it doesn't look like a prologue,
1695 don't try to scan it. If, for instance, a frameless function
1696 begins with stmfd sp!, then we will tell ourselves there is
b8d5e71d 1697 a frame, which will confuse stack traceback, as well as "finish"
96baa820 1698 and other operations that rely on a knowledge of the stack
0d39a070 1699 traceback. */
d4473757 1700
4be43953
DJ
1701 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1702 regs[regno] = pv_register (regno, 0);
55f960e1 1703 stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
4be43953
DJ
1704 back_to = make_cleanup_free_pv_area (stack);
1705
94c30b78
MS
1706 for (current_pc = prologue_start;
1707 current_pc < prologue_end;
f43845b3 1708 current_pc += 4)
96baa820 1709 {
e17a4113
UW
1710 unsigned int insn
1711 = read_memory_unsigned_integer (current_pc, 4, byte_order_for_code);
9d4fde75 1712
94c30b78 1713 if (insn == 0xe1a0c00d) /* mov ip, sp */
f43845b3 1714 {
4be43953 1715 regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
28cd8767
JG
1716 continue;
1717 }
0d39a070
DJ
1718 else if ((insn & 0xfff00000) == 0xe2800000 /* add Rd, Rn, #n */
1719 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767
JG
1720 {
1721 unsigned imm = insn & 0xff; /* immediate value */
1722 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
0d39a070 1723 int rd = bits (insn, 12, 15);
28cd8767 1724 imm = (imm >> rot) | (imm << (32 - rot));
0d39a070 1725 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
28cd8767
JG
1726 continue;
1727 }
0d39a070
DJ
1728 else if ((insn & 0xfff00000) == 0xe2400000 /* sub Rd, Rn, #n */
1729 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767
JG
1730 {
1731 unsigned imm = insn & 0xff; /* immediate value */
1732 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
0d39a070 1733 int rd = bits (insn, 12, 15);
28cd8767 1734 imm = (imm >> rot) | (imm << (32 - rot));
0d39a070 1735 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
f43845b3
MS
1736 continue;
1737 }
0963b4bd
MS
1738 else if ((insn & 0xffff0fff) == 0xe52d0004) /* str Rd,
1739 [sp, #-4]! */
f43845b3 1740 {
4be43953
DJ
1741 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1742 break;
1743 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
0d39a070
DJ
1744 pv_area_store (stack, regs[ARM_SP_REGNUM], 4,
1745 regs[bits (insn, 12, 15)]);
f43845b3
MS
1746 continue;
1747 }
1748 else if ((insn & 0xffff0000) == 0xe92d0000)
d4473757
KB
1749 /* stmfd sp!, {..., fp, ip, lr, pc}
1750 or
1751 stmfd sp!, {a1, a2, a3, a4} */
c906108c 1752 {
d4473757 1753 int mask = insn & 0xffff;
ed9a39eb 1754
4be43953
DJ
1755 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1756 break;
1757
94c30b78 1758 /* Calculate offsets of saved registers. */
34e8f22d 1759 for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
d4473757
KB
1760 if (mask & (1 << regno))
1761 {
0963b4bd
MS
1762 regs[ARM_SP_REGNUM]
1763 = pv_add_constant (regs[ARM_SP_REGNUM], -4);
4be43953 1764 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
d4473757
KB
1765 }
1766 }
0d39a070
DJ
1767 else if ((insn & 0xffff0000) == 0xe54b0000 /* strb rx,[r11,#-n] */
1768 || (insn & 0xffff00f0) == 0xe14b00b0 /* strh rx,[r11,#-n] */
f8bf5763 1769 || (insn & 0xffffc000) == 0xe50b0000) /* str rx,[r11,#-n] */
b8d5e71d
MS
1770 {
1771 /* No need to add this to saved_regs -- it's just an arg reg. */
1772 continue;
1773 }
0d39a070
DJ
1774 else if ((insn & 0xffff0000) == 0xe5cd0000 /* strb rx,[sp,#n] */
1775 || (insn & 0xffff00f0) == 0xe1cd00b0 /* strh rx,[sp,#n] */
f8bf5763 1776 || (insn & 0xffffc000) == 0xe58d0000) /* str rx,[sp,#n] */
f43845b3
MS
1777 {
1778 /* No need to add this to saved_regs -- it's just an arg reg. */
1779 continue;
1780 }
0963b4bd
MS
1781 else if ((insn & 0xfff00000) == 0xe8800000 /* stm Rn,
1782 { registers } */
0d39a070
DJ
1783 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1784 {
1785 /* No need to add this to saved_regs -- it's just arg regs. */
1786 continue;
1787 }
d4473757
KB
1788 else if ((insn & 0xfffff000) == 0xe24cb000) /* sub fp, ip #n */
1789 {
94c30b78
MS
1790 unsigned imm = insn & 0xff; /* immediate value */
1791 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
d4473757 1792 imm = (imm >> rot) | (imm << (32 - rot));
4be43953 1793 regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
d4473757
KB
1794 }
1795 else if ((insn & 0xfffff000) == 0xe24dd000) /* sub sp, sp #n */
1796 {
94c30b78
MS
1797 unsigned imm = insn & 0xff; /* immediate value */
1798 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
d4473757 1799 imm = (imm >> rot) | (imm << (32 - rot));
4be43953 1800 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
d4473757 1801 }
0963b4bd
MS
1802 else if ((insn & 0xffff7fff) == 0xed6d0103 /* stfe f?,
1803 [sp, -#c]! */
2af46ca0 1804 && gdbarch_tdep (gdbarch)->have_fpa_registers)
d4473757 1805 {
4be43953
DJ
1806 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1807 break;
1808
1809 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
34e8f22d 1810 regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
4be43953 1811 pv_area_store (stack, regs[ARM_SP_REGNUM], 12, regs[regno]);
d4473757 1812 }
0963b4bd
MS
1813 else if ((insn & 0xffbf0fff) == 0xec2d0200 /* sfmfd f0, 4,
1814 [sp!] */
2af46ca0 1815 && gdbarch_tdep (gdbarch)->have_fpa_registers)
d4473757
KB
1816 {
1817 int n_saved_fp_regs;
1818 unsigned int fp_start_reg, fp_bound_reg;
1819
4be43953
DJ
1820 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1821 break;
1822
94c30b78 1823 if ((insn & 0x800) == 0x800) /* N0 is set */
96baa820 1824 {
d4473757
KB
1825 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1826 n_saved_fp_regs = 3;
1827 else
1828 n_saved_fp_regs = 1;
96baa820 1829 }
d4473757 1830 else
96baa820 1831 {
d4473757
KB
1832 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1833 n_saved_fp_regs = 2;
1834 else
1835 n_saved_fp_regs = 4;
96baa820 1836 }
d4473757 1837
34e8f22d 1838 fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
d4473757
KB
1839 fp_bound_reg = fp_start_reg + n_saved_fp_regs;
1840 for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
96baa820 1841 {
4be43953
DJ
1842 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
1843 pv_area_store (stack, regs[ARM_SP_REGNUM], 12,
1844 regs[fp_start_reg++]);
96baa820 1845 }
c906108c 1846 }
0d39a070
DJ
1847 else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
1848 {
1849 /* Allow some special function calls when skipping the
1850 prologue; GCC generates these before storing arguments to
1851 the stack. */
1852 CORE_ADDR dest = BranchDest (current_pc, insn);
1853
e0634ccf 1854 if (skip_prologue_function (gdbarch, dest, 0))
0d39a070
DJ
1855 continue;
1856 else
1857 break;
1858 }
d4473757 1859 else if ((insn & 0xf0000000) != 0xe0000000)
0963b4bd 1860 break; /* Condition not true, exit early. */
0d39a070
DJ
1861 else if (arm_instruction_changes_pc (insn))
1862 /* Don't scan past anything that might change control flow. */
1863 break;
d19f7eee
UW
1864 else if ((insn & 0xfe500000) == 0xe8100000 /* ldm */
1865 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1866 /* Ignore block loads from the stack, potentially copying
1867 parameters from memory. */
1868 continue;
1869 else if ((insn & 0xfc500000) == 0xe4100000
1870 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1871 /* Similarly ignore single loads from the stack. */
1872 continue;
0d39a070
DJ
1873 else if ((insn & 0xffff0ff0) == 0xe1a00000)
1874 /* MOV Rd, Rm. Skip register copies, i.e. saves to another
1875 register instead of the stack. */
d4473757 1876 continue;
0d39a070
DJ
1877 else
1878 {
1879 /* The optimizer might shove anything into the prologue,
1880 so we just skip what we don't recognize. */
1881 unrecognized_pc = current_pc;
1882 continue;
1883 }
c906108c
SS
1884 }
1885
0d39a070
DJ
1886 if (unrecognized_pc == 0)
1887 unrecognized_pc = current_pc;
1888
0d39a070
DJ
1889 if (cache)
1890 {
4072f920
YQ
1891 int framereg, framesize;
1892
1893 /* The frame size is just the distance from the frame register
1894 to the original stack pointer. */
1895 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1896 {
1897 /* Frame pointer is fp. */
1898 framereg = ARM_FP_REGNUM;
1899 framesize = -regs[ARM_FP_REGNUM].k;
1900 }
1901 else
1902 {
1903 /* Try the stack pointer... this is a bit desperate. */
1904 framereg = ARM_SP_REGNUM;
1905 framesize = -regs[ARM_SP_REGNUM].k;
1906 }
1907
0d39a070
DJ
1908 cache->framereg = framereg;
1909 cache->framesize = framesize;
1910
1911 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1912 if (pv_area_find_reg (stack, gdbarch, regno, &offset))
1913 cache->saved_regs[regno].addr = offset;
1914 }
1915
1916 if (arm_debug)
1917 fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1918 paddress (gdbarch, unrecognized_pc));
4be43953
DJ
1919
1920 do_cleanups (back_to);
0d39a070
DJ
1921 return unrecognized_pc;
1922}
1923
1924static void
1925arm_scan_prologue (struct frame_info *this_frame,
1926 struct arm_prologue_cache *cache)
1927{
1928 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1929 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1930 int regno;
1931 CORE_ADDR prologue_start, prologue_end, current_pc;
1932 CORE_ADDR prev_pc = get_frame_pc (this_frame);
1933 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1934 pv_t regs[ARM_FPS_REGNUM];
1935 struct pv_area *stack;
1936 struct cleanup *back_to;
1937 CORE_ADDR offset;
1938
1939 /* Assume there is no frame until proven otherwise. */
1940 cache->framereg = ARM_SP_REGNUM;
1941 cache->framesize = 0;
1942
1943 /* Check for Thumb prologue. */
1944 if (arm_frame_is_thumb (this_frame))
1945 {
1946 thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
1947 return;
1948 }
1949
1950 /* Find the function prologue. If we can't find the function in
1951 the symbol table, peek in the stack frame to find the PC. */
1952 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1953 &prologue_end))
1954 {
1955 /* One way to find the end of the prologue (which works well
1956 for unoptimized code) is to do the following:
1957
1958 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
1959
1960 if (sal.line == 0)
1961 prologue_end = prev_pc;
1962 else if (sal.end < prologue_end)
1963 prologue_end = sal.end;
1964
1965 This mechanism is very accurate so long as the optimizer
1966 doesn't move any instructions from the function body into the
1967 prologue. If this happens, sal.end will be the last
1968 instruction in the first hunk of prologue code just before
1969 the first instruction that the scheduler has moved from
1970 the body to the prologue.
1971
1972 In order to make sure that we scan all of the prologue
1973 instructions, we use a slightly less accurate mechanism which
1974 may scan more than necessary. To help compensate for this
1975 lack of accuracy, the prologue scanning loop below contains
1976 several clauses which'll cause the loop to terminate early if
1977 an implausible prologue instruction is encountered.
1978
1979 The expression
1980
1981 prologue_start + 64
1982
1983 is a suitable endpoint since it accounts for the largest
1984 possible prologue plus up to five instructions inserted by
1985 the scheduler. */
1986
1987 if (prologue_end > prologue_start + 64)
1988 {
1989 prologue_end = prologue_start + 64; /* See above. */
1990 }
1991 }
1992 else
1993 {
1994 /* We have no symbol information. Our only option is to assume this
1995 function has a standard stack frame and the normal frame register.
1996 Then, we can find the value of our frame pointer on entrance to
1997 the callee (or at the present moment if this is the innermost frame).
1998 The value stored there should be the address of the stmfd + 8. */
1999 CORE_ADDR frame_loc;
2000 LONGEST return_value;
2001
2002 frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
2003 if (!safe_read_memory_integer (frame_loc, 4, byte_order, &return_value))
2004 return;
2005 else
2006 {
2007 prologue_start = gdbarch_addr_bits_remove
2008 (gdbarch, return_value) - 8;
2009 prologue_end = prologue_start + 64; /* See above. */
2010 }
2011 }
2012
2013 if (prev_pc < prologue_end)
2014 prologue_end = prev_pc;
2015
2016 arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
2017}
2018
eb5492fa 2019static struct arm_prologue_cache *
a262aec2 2020arm_make_prologue_cache (struct frame_info *this_frame)
c906108c 2021{
eb5492fa
DJ
2022 int reg;
2023 struct arm_prologue_cache *cache;
2024 CORE_ADDR unwound_fp;
c5aa993b 2025
35d5d4ee 2026 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
a262aec2 2027 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
c906108c 2028
a262aec2 2029 arm_scan_prologue (this_frame, cache);
848cfffb 2030
a262aec2 2031 unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
eb5492fa
DJ
2032 if (unwound_fp == 0)
2033 return cache;
c906108c 2034
4be43953 2035 cache->prev_sp = unwound_fp + cache->framesize;
c906108c 2036
eb5492fa
DJ
2037 /* Calculate actual addresses of saved registers using offsets
2038 determined by arm_scan_prologue. */
a262aec2 2039 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
e28a332c 2040 if (trad_frame_addr_p (cache->saved_regs, reg))
eb5492fa
DJ
2041 cache->saved_regs[reg].addr += cache->prev_sp;
2042
2043 return cache;
c906108c
SS
2044}
2045
eb5492fa
DJ
2046/* Our frame ID for a normal frame is the current function's starting PC
2047 and the caller's SP when we were called. */
c906108c 2048
148754e5 2049static void
a262aec2 2050arm_prologue_this_id (struct frame_info *this_frame,
eb5492fa
DJ
2051 void **this_cache,
2052 struct frame_id *this_id)
c906108c 2053{
eb5492fa
DJ
2054 struct arm_prologue_cache *cache;
2055 struct frame_id id;
2c404490 2056 CORE_ADDR pc, func;
f079148d 2057
eb5492fa 2058 if (*this_cache == NULL)
a262aec2 2059 *this_cache = arm_make_prologue_cache (this_frame);
eb5492fa 2060 cache = *this_cache;
2a451106 2061
2c404490
DJ
2062 /* This is meant to halt the backtrace at "_start". */
2063 pc = get_frame_pc (this_frame);
2064 if (pc <= gdbarch_tdep (get_frame_arch (this_frame))->lowest_pc)
eb5492fa 2065 return;
5a203e44 2066
eb5492fa
DJ
2067 /* If we've hit a wall, stop. */
2068 if (cache->prev_sp == 0)
2069 return;
24de872b 2070
0e9e9abd
UW
2071 /* Use function start address as part of the frame ID. If we cannot
2072 identify the start address (due to missing symbol information),
2073 fall back to just using the current PC. */
2c404490 2074 func = get_frame_func (this_frame);
0e9e9abd
UW
2075 if (!func)
2076 func = pc;
2077
eb5492fa 2078 id = frame_id_build (cache->prev_sp, func);
eb5492fa 2079 *this_id = id;
c906108c
SS
2080}
2081
a262aec2
DJ
2082static struct value *
2083arm_prologue_prev_register (struct frame_info *this_frame,
eb5492fa 2084 void **this_cache,
a262aec2 2085 int prev_regnum)
24de872b 2086{
24568a2c 2087 struct gdbarch *gdbarch = get_frame_arch (this_frame);
24de872b
DJ
2088 struct arm_prologue_cache *cache;
2089
eb5492fa 2090 if (*this_cache == NULL)
a262aec2 2091 *this_cache = arm_make_prologue_cache (this_frame);
eb5492fa 2092 cache = *this_cache;
24de872b 2093
eb5492fa 2094 /* If we are asked to unwind the PC, then we need to return the LR
b39cc962
DJ
2095 instead. The prologue may save PC, but it will point into this
2096 frame's prologue, not the next frame's resume location. Also
2097 strip the saved T bit. A valid LR may have the low bit set, but
2098 a valid PC never does. */
eb5492fa 2099 if (prev_regnum == ARM_PC_REGNUM)
b39cc962
DJ
2100 {
2101 CORE_ADDR lr;
2102
2103 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2104 return frame_unwind_got_constant (this_frame, prev_regnum,
24568a2c 2105 arm_addr_bits_remove (gdbarch, lr));
b39cc962 2106 }
24de872b 2107
eb5492fa 2108 /* SP is generally not saved to the stack, but this frame is
a262aec2 2109 identified by the next frame's stack pointer at the time of the call.
eb5492fa
DJ
2110 The value was already reconstructed into PREV_SP. */
2111 if (prev_regnum == ARM_SP_REGNUM)
a262aec2 2112 return frame_unwind_got_constant (this_frame, prev_regnum, cache->prev_sp);
eb5492fa 2113
b39cc962
DJ
2114 /* The CPSR may have been changed by the call instruction and by the
2115 called function. The only bit we can reconstruct is the T bit,
2116 by checking the low bit of LR as of the call. This is a reliable
2117 indicator of Thumb-ness except for some ARM v4T pre-interworking
2118 Thumb code, which could get away with a clear low bit as long as
2119 the called function did not use bx. Guess that all other
2120 bits are unchanged; the condition flags are presumably lost,
2121 but the processor status is likely valid. */
2122 if (prev_regnum == ARM_PS_REGNUM)
2123 {
2124 CORE_ADDR lr, cpsr;
9779414d 2125 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
b39cc962
DJ
2126
2127 cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
2128 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2129 if (IS_THUMB_ADDR (lr))
9779414d 2130 cpsr |= t_bit;
b39cc962 2131 else
9779414d 2132 cpsr &= ~t_bit;
b39cc962
DJ
2133 return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
2134 }
2135
a262aec2
DJ
2136 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
2137 prev_regnum);
eb5492fa
DJ
2138}
2139
2140struct frame_unwind arm_prologue_unwind = {
2141 NORMAL_FRAME,
8fbca658 2142 default_frame_unwind_stop_reason,
eb5492fa 2143 arm_prologue_this_id,
a262aec2
DJ
2144 arm_prologue_prev_register,
2145 NULL,
2146 default_frame_sniffer
eb5492fa
DJ
2147};
2148
0e9e9abd
UW
2149/* Maintain a list of ARM exception table entries per objfile, similar to the
2150 list of mapping symbols. We only cache entries for standard ARM-defined
2151 personality routines; the cache will contain only the frame unwinding
2152 instructions associated with the entry (not the descriptors). */
2153
2154static const struct objfile_data *arm_exidx_data_key;
2155
2156struct arm_exidx_entry
2157{
2158 bfd_vma addr;
2159 gdb_byte *entry;
2160};
2161typedef struct arm_exidx_entry arm_exidx_entry_s;
2162DEF_VEC_O(arm_exidx_entry_s);
2163
2164struct arm_exidx_data
2165{
2166 VEC(arm_exidx_entry_s) **section_maps;
2167};
2168
2169static void
2170arm_exidx_data_free (struct objfile *objfile, void *arg)
2171{
2172 struct arm_exidx_data *data = arg;
2173 unsigned int i;
2174
2175 for (i = 0; i < objfile->obfd->section_count; i++)
2176 VEC_free (arm_exidx_entry_s, data->section_maps[i]);
2177}
2178
2179static inline int
2180arm_compare_exidx_entries (const struct arm_exidx_entry *lhs,
2181 const struct arm_exidx_entry *rhs)
2182{
2183 return lhs->addr < rhs->addr;
2184}
2185
2186static struct obj_section *
2187arm_obj_section_from_vma (struct objfile *objfile, bfd_vma vma)
2188{
2189 struct obj_section *osect;
2190
2191 ALL_OBJFILE_OSECTIONS (objfile, osect)
2192 if (bfd_get_section_flags (objfile->obfd,
2193 osect->the_bfd_section) & SEC_ALLOC)
2194 {
2195 bfd_vma start, size;
2196 start = bfd_get_section_vma (objfile->obfd, osect->the_bfd_section);
2197 size = bfd_get_section_size (osect->the_bfd_section);
2198
2199 if (start <= vma && vma < start + size)
2200 return osect;
2201 }
2202
2203 return NULL;
2204}
2205
2206/* Parse contents of exception table and exception index sections
2207 of OBJFILE, and fill in the exception table entry cache.
2208
2209 For each entry that refers to a standard ARM-defined personality
2210 routine, extract the frame unwinding instructions (from either
2211 the index or the table section). The unwinding instructions
2212 are normalized by:
2213 - extracting them from the rest of the table data
2214 - converting to host endianness
2215 - appending the implicit 0xb0 ("Finish") code
2216
2217 The extracted and normalized instructions are stored for later
2218 retrieval by the arm_find_exidx_entry routine. */
2219
2220static void
2221arm_exidx_new_objfile (struct objfile *objfile)
2222{
3bb47e8b 2223 struct cleanup *cleanups;
0e9e9abd
UW
2224 struct arm_exidx_data *data;
2225 asection *exidx, *extab;
2226 bfd_vma exidx_vma = 0, extab_vma = 0;
2227 bfd_size_type exidx_size = 0, extab_size = 0;
2228 gdb_byte *exidx_data = NULL, *extab_data = NULL;
2229 LONGEST i;
2230
2231 /* If we've already touched this file, do nothing. */
2232 if (!objfile || objfile_data (objfile, arm_exidx_data_key) != NULL)
2233 return;
3bb47e8b 2234 cleanups = make_cleanup (null_cleanup, NULL);
0e9e9abd
UW
2235
2236 /* Read contents of exception table and index. */
2237 exidx = bfd_get_section_by_name (objfile->obfd, ".ARM.exidx");
2238 if (exidx)
2239 {
2240 exidx_vma = bfd_section_vma (objfile->obfd, exidx);
2241 exidx_size = bfd_get_section_size (exidx);
2242 exidx_data = xmalloc (exidx_size);
2243 make_cleanup (xfree, exidx_data);
2244
2245 if (!bfd_get_section_contents (objfile->obfd, exidx,
2246 exidx_data, 0, exidx_size))
2247 {
2248 do_cleanups (cleanups);
2249 return;
2250 }
2251 }
2252
2253 extab = bfd_get_section_by_name (objfile->obfd, ".ARM.extab");
2254 if (extab)
2255 {
2256 extab_vma = bfd_section_vma (objfile->obfd, extab);
2257 extab_size = bfd_get_section_size (extab);
2258 extab_data = xmalloc (extab_size);
2259 make_cleanup (xfree, extab_data);
2260
2261 if (!bfd_get_section_contents (objfile->obfd, extab,
2262 extab_data, 0, extab_size))
2263 {
2264 do_cleanups (cleanups);
2265 return;
2266 }
2267 }
2268
2269 /* Allocate exception table data structure. */
2270 data = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct arm_exidx_data);
2271 set_objfile_data (objfile, arm_exidx_data_key, data);
2272 data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
2273 objfile->obfd->section_count,
2274 VEC(arm_exidx_entry_s) *);
2275
2276 /* Fill in exception table. */
2277 for (i = 0; i < exidx_size / 8; i++)
2278 {
2279 struct arm_exidx_entry new_exidx_entry;
2280 bfd_vma idx = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8);
2281 bfd_vma val = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8 + 4);
2282 bfd_vma addr = 0, word = 0;
2283 int n_bytes = 0, n_words = 0;
2284 struct obj_section *sec;
2285 gdb_byte *entry = NULL;
2286
2287 /* Extract address of start of function. */
2288 idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2289 idx += exidx_vma + i * 8;
2290
2291 /* Find section containing function and compute section offset. */
2292 sec = arm_obj_section_from_vma (objfile, idx);
2293 if (sec == NULL)
2294 continue;
2295 idx -= bfd_get_section_vma (objfile->obfd, sec->the_bfd_section);
2296
2297 /* Determine address of exception table entry. */
2298 if (val == 1)
2299 {
2300 /* EXIDX_CANTUNWIND -- no exception table entry present. */
2301 }
2302 else if ((val & 0xff000000) == 0x80000000)
2303 {
2304 /* Exception table entry embedded in .ARM.exidx
2305 -- must be short form. */
2306 word = val;
2307 n_bytes = 3;
2308 }
2309 else if (!(val & 0x80000000))
2310 {
2311 /* Exception table entry in .ARM.extab. */
2312 addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2313 addr += exidx_vma + i * 8 + 4;
2314
2315 if (addr >= extab_vma && addr + 4 <= extab_vma + extab_size)
2316 {
2317 word = bfd_h_get_32 (objfile->obfd,
2318 extab_data + addr - extab_vma);
2319 addr += 4;
2320
2321 if ((word & 0xff000000) == 0x80000000)
2322 {
2323 /* Short form. */
2324 n_bytes = 3;
2325 }
2326 else if ((word & 0xff000000) == 0x81000000
2327 || (word & 0xff000000) == 0x82000000)
2328 {
2329 /* Long form. */
2330 n_bytes = 2;
2331 n_words = ((word >> 16) & 0xff);
2332 }
2333 else if (!(word & 0x80000000))
2334 {
2335 bfd_vma pers;
2336 struct obj_section *pers_sec;
2337 int gnu_personality = 0;
2338
2339 /* Custom personality routine. */
2340 pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2341 pers = UNMAKE_THUMB_ADDR (pers + addr - 4);
2342
2343 /* Check whether we've got one of the variants of the
2344 GNU personality routines. */
2345 pers_sec = arm_obj_section_from_vma (objfile, pers);
2346 if (pers_sec)
2347 {
2348 static const char *personality[] =
2349 {
2350 "__gcc_personality_v0",
2351 "__gxx_personality_v0",
2352 "__gcj_personality_v0",
2353 "__gnu_objc_personality_v0",
2354 NULL
2355 };
2356
2357 CORE_ADDR pc = pers + obj_section_offset (pers_sec);
2358 int k;
2359
2360 for (k = 0; personality[k]; k++)
2361 if (lookup_minimal_symbol_by_pc_name
2362 (pc, personality[k], objfile))
2363 {
2364 gnu_personality = 1;
2365 break;
2366 }
2367 }
2368
2369 /* If so, the next word contains a word count in the high
2370 byte, followed by the same unwind instructions as the
2371 pre-defined forms. */
2372 if (gnu_personality
2373 && addr + 4 <= extab_vma + extab_size)
2374 {
2375 word = bfd_h_get_32 (objfile->obfd,
2376 extab_data + addr - extab_vma);
2377 addr += 4;
2378 n_bytes = 3;
2379 n_words = ((word >> 24) & 0xff);
2380 }
2381 }
2382 }
2383 }
2384
2385 /* Sanity check address. */
2386 if (n_words)
2387 if (addr < extab_vma || addr + 4 * n_words > extab_vma + extab_size)
2388 n_words = n_bytes = 0;
2389
2390 /* The unwind instructions reside in WORD (only the N_BYTES least
2391 significant bytes are valid), followed by N_WORDS words in the
2392 extab section starting at ADDR. */
2393 if (n_bytes || n_words)
2394 {
2395 gdb_byte *p = entry = obstack_alloc (&objfile->objfile_obstack,
2396 n_bytes + n_words * 4 + 1);
2397
2398 while (n_bytes--)
2399 *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);
2400
2401 while (n_words--)
2402 {
2403 word = bfd_h_get_32 (objfile->obfd,
2404 extab_data + addr - extab_vma);
2405 addr += 4;
2406
2407 *p++ = (gdb_byte) ((word >> 24) & 0xff);
2408 *p++ = (gdb_byte) ((word >> 16) & 0xff);
2409 *p++ = (gdb_byte) ((word >> 8) & 0xff);
2410 *p++ = (gdb_byte) (word & 0xff);
2411 }
2412
2413 /* Implied "Finish" to terminate the list. */
2414 *p++ = 0xb0;
2415 }
2416
2417 /* Push entry onto vector. They are guaranteed to always
2418 appear in order of increasing addresses. */
2419 new_exidx_entry.addr = idx;
2420 new_exidx_entry.entry = entry;
2421 VEC_safe_push (arm_exidx_entry_s,
2422 data->section_maps[sec->the_bfd_section->index],
2423 &new_exidx_entry);
2424 }
2425
2426 do_cleanups (cleanups);
2427}
2428
2429/* Search for the exception table entry covering MEMADDR. If one is found,
2430 return a pointer to its data. Otherwise, return 0. If START is non-NULL,
2431 set *START to the start of the region covered by this entry. */
2432
2433static gdb_byte *
2434arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
2435{
2436 struct obj_section *sec;
2437
2438 sec = find_pc_section (memaddr);
2439 if (sec != NULL)
2440 {
2441 struct arm_exidx_data *data;
2442 VEC(arm_exidx_entry_s) *map;
2443 struct arm_exidx_entry map_key = { memaddr - obj_section_addr (sec), 0 };
2444 unsigned int idx;
2445
2446 data = objfile_data (sec->objfile, arm_exidx_data_key);
2447 if (data != NULL)
2448 {
2449 map = data->section_maps[sec->the_bfd_section->index];
2450 if (!VEC_empty (arm_exidx_entry_s, map))
2451 {
2452 struct arm_exidx_entry *map_sym;
2453
2454 idx = VEC_lower_bound (arm_exidx_entry_s, map, &map_key,
2455 arm_compare_exidx_entries);
2456
2457 /* VEC_lower_bound finds the earliest ordered insertion
2458 point. If the following symbol starts at this exact
2459 address, we use that; otherwise, the preceding
2460 exception table entry covers this address. */
2461 if (idx < VEC_length (arm_exidx_entry_s, map))
2462 {
2463 map_sym = VEC_index (arm_exidx_entry_s, map, idx);
2464 if (map_sym->addr == map_key.addr)
2465 {
2466 if (start)
2467 *start = map_sym->addr + obj_section_addr (sec);
2468 return map_sym->entry;
2469 }
2470 }
2471
2472 if (idx > 0)
2473 {
2474 map_sym = VEC_index (arm_exidx_entry_s, map, idx - 1);
2475 if (start)
2476 *start = map_sym->addr + obj_section_addr (sec);
2477 return map_sym->entry;
2478 }
2479 }
2480 }
2481 }
2482
2483 return NULL;
2484}
2485
2486/* Given the current frame THIS_FRAME, and its associated frame unwinding
2487 instruction list from the ARM exception table entry ENTRY, allocate and
2488 return a prologue cache structure describing how to unwind this frame.
2489
2490 Return NULL if the unwinding instruction list contains a "spare",
2491 "reserved" or "refuse to unwind" instruction as defined in section
2492 "9.3 Frame unwinding instructions" of the "Exception Handling ABI
2493 for the ARM Architecture" document. */
2494
2495static struct arm_prologue_cache *
2496arm_exidx_fill_cache (struct frame_info *this_frame, gdb_byte *entry)
2497{
2498 CORE_ADDR vsp = 0;
2499 int vsp_valid = 0;
2500
2501 struct arm_prologue_cache *cache;
2502 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2503 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2504
2505 for (;;)
2506 {
2507 gdb_byte insn;
2508
2509 /* Whenever we reload SP, we actually have to retrieve its
2510 actual value in the current frame. */
2511 if (!vsp_valid)
2512 {
2513 if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2514 {
2515 int reg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2516 vsp = get_frame_register_unsigned (this_frame, reg);
2517 }
2518 else
2519 {
2520 CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr;
2521 vsp = get_frame_memory_unsigned (this_frame, addr, 4);
2522 }
2523
2524 vsp_valid = 1;
2525 }
2526
2527 /* Decode next unwind instruction. */
2528 insn = *entry++;
2529
2530 if ((insn & 0xc0) == 0)
2531 {
2532 int offset = insn & 0x3f;
2533 vsp += (offset << 2) + 4;
2534 }
2535 else if ((insn & 0xc0) == 0x40)
2536 {
2537 int offset = insn & 0x3f;
2538 vsp -= (offset << 2) + 4;
2539 }
2540 else if ((insn & 0xf0) == 0x80)
2541 {
2542 int mask = ((insn & 0xf) << 8) | *entry++;
2543 int i;
2544
2545 /* The special case of an all-zero mask identifies
2546 "Refuse to unwind". We return NULL to fall back
2547 to the prologue analyzer. */
2548 if (mask == 0)
2549 return NULL;
2550
2551 /* Pop registers r4..r15 under mask. */
2552 for (i = 0; i < 12; i++)
2553 if (mask & (1 << i))
2554 {
2555 cache->saved_regs[4 + i].addr = vsp;
2556 vsp += 4;
2557 }
2558
2559 /* Special-case popping SP -- we need to reload vsp. */
2560 if (mask & (1 << (ARM_SP_REGNUM - 4)))
2561 vsp_valid = 0;
2562 }
2563 else if ((insn & 0xf0) == 0x90)
2564 {
2565 int reg = insn & 0xf;
2566
2567 /* Reserved cases. */
2568 if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
2569 return NULL;
2570
2571 /* Set SP from another register and mark VSP for reload. */
2572 cache->saved_regs[ARM_SP_REGNUM] = cache->saved_regs[reg];
2573 vsp_valid = 0;
2574 }
2575 else if ((insn & 0xf0) == 0xa0)
2576 {
2577 int count = insn & 0x7;
2578 int pop_lr = (insn & 0x8) != 0;
2579 int i;
2580
2581 /* Pop r4..r[4+count]. */
2582 for (i = 0; i <= count; i++)
2583 {
2584 cache->saved_regs[4 + i].addr = vsp;
2585 vsp += 4;
2586 }
2587
2588 /* If indicated by flag, pop LR as well. */
2589 if (pop_lr)
2590 {
2591 cache->saved_regs[ARM_LR_REGNUM].addr = vsp;
2592 vsp += 4;
2593 }
2594 }
2595 else if (insn == 0xb0)
2596 {
2597 /* We could only have updated PC by popping into it; if so, it
2598 will show up as address. Otherwise, copy LR into PC. */
2599 if (!trad_frame_addr_p (cache->saved_regs, ARM_PC_REGNUM))
2600 cache->saved_regs[ARM_PC_REGNUM]
2601 = cache->saved_regs[ARM_LR_REGNUM];
2602
2603 /* We're done. */
2604 break;
2605 }
2606 else if (insn == 0xb1)
2607 {
2608 int mask = *entry++;
2609 int i;
2610
2611 /* All-zero mask and mask >= 16 is "spare". */
2612 if (mask == 0 || mask >= 16)
2613 return NULL;
2614
2615 /* Pop r0..r3 under mask. */
2616 for (i = 0; i < 4; i++)
2617 if (mask & (1 << i))
2618 {
2619 cache->saved_regs[i].addr = vsp;
2620 vsp += 4;
2621 }
2622 }
2623 else if (insn == 0xb2)
2624 {
2625 ULONGEST offset = 0;
2626 unsigned shift = 0;
2627
2628 do
2629 {
2630 offset |= (*entry & 0x7f) << shift;
2631 shift += 7;
2632 }
2633 while (*entry++ & 0x80);
2634
2635 vsp += 0x204 + (offset << 2);
2636 }
2637 else if (insn == 0xb3)
2638 {
2639 int start = *entry >> 4;
2640 int count = (*entry++) & 0xf;
2641 int i;
2642
2643 /* Only registers D0..D15 are valid here. */
2644 if (start + count >= 16)
2645 return NULL;
2646
2647 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2648 for (i = 0; i <= count; i++)
2649 {
2650 cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2651 vsp += 8;
2652 }
2653
2654 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2655 vsp += 4;
2656 }
2657 else if ((insn & 0xf8) == 0xb8)
2658 {
2659 int count = insn & 0x7;
2660 int i;
2661
2662 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2663 for (i = 0; i <= count; i++)
2664 {
2665 cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2666 vsp += 8;
2667 }
2668
2669 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2670 vsp += 4;
2671 }
2672 else if (insn == 0xc6)
2673 {
2674 int start = *entry >> 4;
2675 int count = (*entry++) & 0xf;
2676 int i;
2677
2678 /* Only registers WR0..WR15 are valid. */
2679 if (start + count >= 16)
2680 return NULL;
2681
2682 /* Pop iwmmx registers WR[start]..WR[start+count]. */
2683 for (i = 0; i <= count; i++)
2684 {
2685 cache->saved_regs[ARM_WR0_REGNUM + start + i].addr = vsp;
2686 vsp += 8;
2687 }
2688 }
2689 else if (insn == 0xc7)
2690 {
2691 int mask = *entry++;
2692 int i;
2693
2694 /* All-zero mask and mask >= 16 is "spare". */
2695 if (mask == 0 || mask >= 16)
2696 return NULL;
2697
2698 /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask. */
2699 for (i = 0; i < 4; i++)
2700 if (mask & (1 << i))
2701 {
2702 cache->saved_regs[ARM_WCGR0_REGNUM + i].addr = vsp;
2703 vsp += 4;
2704 }
2705 }
2706 else if ((insn & 0xf8) == 0xc0)
2707 {
2708 int count = insn & 0x7;
2709 int i;
2710
2711 /* Pop iwmmx registers WR[10]..WR[10+count]. */
2712 for (i = 0; i <= count; i++)
2713 {
2714 cache->saved_regs[ARM_WR0_REGNUM + 10 + i].addr = vsp;
2715 vsp += 8;
2716 }
2717 }
2718 else if (insn == 0xc8)
2719 {
2720 int start = *entry >> 4;
2721 int count = (*entry++) & 0xf;
2722 int i;
2723
2724 /* Only registers D0..D31 are valid. */
2725 if (start + count >= 16)
2726 return NULL;
2727
2728 /* Pop VFP double-precision registers
2729 D[16+start]..D[16+start+count]. */
2730 for (i = 0; i <= count; i++)
2731 {
2732 cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].addr = vsp;
2733 vsp += 8;
2734 }
2735 }
2736 else if (insn == 0xc9)
2737 {
2738 int start = *entry >> 4;
2739 int count = (*entry++) & 0xf;
2740 int i;
2741
2742 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2743 for (i = 0; i <= count; i++)
2744 {
2745 cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2746 vsp += 8;
2747 }
2748 }
2749 else if ((insn & 0xf8) == 0xd0)
2750 {
2751 int count = insn & 0x7;
2752 int i;
2753
2754 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2755 for (i = 0; i <= count; i++)
2756 {
2757 cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2758 vsp += 8;
2759 }
2760 }
2761 else
2762 {
2763 /* Everything else is "spare". */
2764 return NULL;
2765 }
2766 }
2767
2768 /* If we restore SP from a register, assume this was the frame register.
2769 Otherwise just fall back to SP as frame register. */
2770 if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2771 cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2772 else
2773 cache->framereg = ARM_SP_REGNUM;
2774
2775 /* Determine offset to previous frame. */
2776 cache->framesize
2777 = vsp - get_frame_register_unsigned (this_frame, cache->framereg);
2778
2779 /* We already got the previous SP. */
2780 cache->prev_sp = vsp;
2781
2782 return cache;
2783}
2784
2785/* Unwinding via ARM exception table entries. Note that the sniffer
2786 already computes a filled-in prologue cache, which is then used
2787 with the same arm_prologue_this_id and arm_prologue_prev_register
2788 routines also used for prologue-parsing based unwinding. */
2789
2790static int
2791arm_exidx_unwind_sniffer (const struct frame_unwind *self,
2792 struct frame_info *this_frame,
2793 void **this_prologue_cache)
2794{
2795 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2796 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
2797 CORE_ADDR addr_in_block, exidx_region, func_start;
2798 struct arm_prologue_cache *cache;
2799 gdb_byte *entry;
2800
2801 /* See if we have an ARM exception table entry covering this address. */
2802 addr_in_block = get_frame_address_in_block (this_frame);
2803 entry = arm_find_exidx_entry (addr_in_block, &exidx_region);
2804 if (!entry)
2805 return 0;
2806
2807 /* The ARM exception table does not describe unwind information
2808 for arbitrary PC values, but is guaranteed to be correct only
2809 at call sites. We have to decide here whether we want to use
2810 ARM exception table information for this frame, or fall back
2811 to using prologue parsing. (Note that if we have DWARF CFI,
2812 this sniffer isn't even called -- CFI is always preferred.)
2813
2814 Before we make this decision, however, we check whether we
2815 actually have *symbol* information for the current frame.
2816 If not, prologue parsing would not work anyway, so we might
2817 as well use the exception table and hope for the best. */
2818 if (find_pc_partial_function (addr_in_block, NULL, &func_start, NULL))
2819 {
2820 int exc_valid = 0;
2821
2822 /* If the next frame is "normal", we are at a call site in this
2823 frame, so exception information is guaranteed to be valid. */
2824 if (get_next_frame (this_frame)
2825 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
2826 exc_valid = 1;
2827
2828 /* We also assume exception information is valid if we're currently
2829 blocked in a system call. The system library is supposed to
2830 ensure this, so that e.g. pthread cancellation works. */
2831 if (arm_frame_is_thumb (this_frame))
2832 {
2833 LONGEST insn;
2834
2835 if (safe_read_memory_integer (get_frame_pc (this_frame) - 2, 2,
2836 byte_order_for_code, &insn)
2837 && (insn & 0xff00) == 0xdf00 /* svc */)
2838 exc_valid = 1;
2839 }
2840 else
2841 {
2842 LONGEST insn;
2843
2844 if (safe_read_memory_integer (get_frame_pc (this_frame) - 4, 4,
2845 byte_order_for_code, &insn)
2846 && (insn & 0x0f000000) == 0x0f000000 /* svc */)
2847 exc_valid = 1;
2848 }
2849
2850 /* Bail out if we don't know that exception information is valid. */
2851 if (!exc_valid)
2852 return 0;
2853
2854 /* The ARM exception index does not mark the *end* of the region
2855 covered by the entry, and some functions will not have any entry.
2856 To correctly recognize the end of the covered region, the linker
2857 should have inserted dummy records with a CANTUNWIND marker.
2858
2859 Unfortunately, current versions of GNU ld do not reliably do
2860 this, and thus we may have found an incorrect entry above.
2861 As a (temporary) sanity check, we only use the entry if it
2862 lies *within* the bounds of the function. Note that this check
2863 might reject perfectly valid entries that just happen to cover
2864 multiple functions; therefore this check ought to be removed
2865 once the linker is fixed. */
2866 if (func_start > exidx_region)
2867 return 0;
2868 }
2869
2870 /* Decode the list of unwinding instructions into a prologue cache.
2871 Note that this may fail due to e.g. a "refuse to unwind" code. */
2872 cache = arm_exidx_fill_cache (this_frame, entry);
2873 if (!cache)
2874 return 0;
2875
2876 *this_prologue_cache = cache;
2877 return 1;
2878}
2879
2880struct frame_unwind arm_exidx_unwind = {
2881 NORMAL_FRAME,
8fbca658 2882 default_frame_unwind_stop_reason,
0e9e9abd
UW
2883 arm_prologue_this_id,
2884 arm_prologue_prev_register,
2885 NULL,
2886 arm_exidx_unwind_sniffer
2887};
2888
80d8d390
YQ
2889/* Recognize GCC's trampoline for thumb call-indirect. If we are in a
2890 trampoline, return the target PC. Otherwise return 0.
2891
2892 void call0a (char c, short s, int i, long l) {}
2893
2894 int main (void)
2895 {
2896 (*pointer_to_call0a) (c, s, i, l);
2897 }
2898
2899 Instead of calling a stub library function _call_via_xx (xx is
2900 the register name), GCC may inline the trampoline in the object
2901 file as below (register r2 has the address of call0a).
2902
2903 .global main
2904 .type main, %function
2905 ...
2906 bl .L1
2907 ...
2908 .size main, .-main
2909
2910 .L1:
2911 bx r2
2912
2913 The trampoline 'bx r2' doesn't belong to main. */
2914
2915static CORE_ADDR
2916arm_skip_bx_reg (struct frame_info *frame, CORE_ADDR pc)
2917{
2918 /* The heuristics of recognizing such trampoline is that FRAME is
2919 executing in Thumb mode and the instruction on PC is 'bx Rm'. */
2920 if (arm_frame_is_thumb (frame))
2921 {
2922 gdb_byte buf[2];
2923
2924 if (target_read_memory (pc, buf, 2) == 0)
2925 {
2926 struct gdbarch *gdbarch = get_frame_arch (frame);
2927 enum bfd_endian byte_order_for_code
2928 = gdbarch_byte_order_for_code (gdbarch);
2929 uint16_t insn
2930 = extract_unsigned_integer (buf, 2, byte_order_for_code);
2931
2932 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
2933 {
2934 CORE_ADDR dest
2935 = get_frame_register_unsigned (frame, bits (insn, 3, 6));
2936
2937 /* Clear the LSB so that gdb core sets step-resume
2938 breakpoint at the right address. */
2939 return UNMAKE_THUMB_ADDR (dest);
2940 }
2941 }
2942 }
2943
2944 return 0;
2945}
2946
909cf6ea 2947static struct arm_prologue_cache *
a262aec2 2948arm_make_stub_cache (struct frame_info *this_frame)
909cf6ea 2949{
909cf6ea 2950 struct arm_prologue_cache *cache;
909cf6ea 2951
35d5d4ee 2952 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
a262aec2 2953 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
909cf6ea 2954
a262aec2 2955 cache->prev_sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
909cf6ea
DJ
2956
2957 return cache;
2958}
2959
2960/* Our frame ID for a stub frame is the current SP and LR. */
2961
2962static void
a262aec2 2963arm_stub_this_id (struct frame_info *this_frame,
909cf6ea
DJ
2964 void **this_cache,
2965 struct frame_id *this_id)
2966{
2967 struct arm_prologue_cache *cache;
2968
2969 if (*this_cache == NULL)
a262aec2 2970 *this_cache = arm_make_stub_cache (this_frame);
909cf6ea
DJ
2971 cache = *this_cache;
2972
a262aec2 2973 *this_id = frame_id_build (cache->prev_sp, get_frame_pc (this_frame));
909cf6ea
DJ
2974}
2975
a262aec2
DJ
2976static int
2977arm_stub_unwind_sniffer (const struct frame_unwind *self,
2978 struct frame_info *this_frame,
2979 void **this_prologue_cache)
909cf6ea 2980{
93d42b30 2981 CORE_ADDR addr_in_block;
948f8e3d 2982 gdb_byte dummy[4];
18d18ac8
YQ
2983 CORE_ADDR pc, start_addr;
2984 const char *name;
909cf6ea 2985
a262aec2 2986 addr_in_block = get_frame_address_in_block (this_frame);
18d18ac8 2987 pc = get_frame_pc (this_frame);
3e5d3a5a 2988 if (in_plt_section (addr_in_block)
fc36e839
DE
2989 /* We also use the stub winder if the target memory is unreadable
2990 to avoid having the prologue unwinder trying to read it. */
18d18ac8
YQ
2991 || target_read_memory (pc, dummy, 4) != 0)
2992 return 1;
2993
2994 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0
2995 && arm_skip_bx_reg (this_frame, pc) != 0)
a262aec2 2996 return 1;
909cf6ea 2997
a262aec2 2998 return 0;
909cf6ea
DJ
2999}
3000
a262aec2
DJ
3001struct frame_unwind arm_stub_unwind = {
3002 NORMAL_FRAME,
8fbca658 3003 default_frame_unwind_stop_reason,
a262aec2
DJ
3004 arm_stub_this_id,
3005 arm_prologue_prev_register,
3006 NULL,
3007 arm_stub_unwind_sniffer
3008};
3009
2ae28aa9
YQ
3010/* Put here the code to store, into CACHE->saved_regs, the addresses
3011 of the saved registers of frame described by THIS_FRAME. CACHE is
3012 returned. */
3013
3014static struct arm_prologue_cache *
3015arm_m_exception_cache (struct frame_info *this_frame)
3016{
3017 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3018 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3019 struct arm_prologue_cache *cache;
3020 CORE_ADDR unwound_sp;
3021 LONGEST xpsr;
3022
3023 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
3024 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
3025
3026 unwound_sp = get_frame_register_unsigned (this_frame,
3027 ARM_SP_REGNUM);
3028
3029 /* The hardware saves eight 32-bit words, comprising xPSR,
3030 ReturnAddress, LR (R14), R12, R3, R2, R1, R0. See details in
3031 "B1.5.6 Exception entry behavior" in
3032 "ARMv7-M Architecture Reference Manual". */
3033 cache->saved_regs[0].addr = unwound_sp;
3034 cache->saved_regs[1].addr = unwound_sp + 4;
3035 cache->saved_regs[2].addr = unwound_sp + 8;
3036 cache->saved_regs[3].addr = unwound_sp + 12;
3037 cache->saved_regs[12].addr = unwound_sp + 16;
3038 cache->saved_regs[14].addr = unwound_sp + 20;
3039 cache->saved_regs[15].addr = unwound_sp + 24;
3040 cache->saved_regs[ARM_PS_REGNUM].addr = unwound_sp + 28;
3041
3042 /* If bit 9 of the saved xPSR is set, then there is a four-byte
3043 aligner between the top of the 32-byte stack frame and the
3044 previous context's stack pointer. */
3045 cache->prev_sp = unwound_sp + 32;
3046 if (safe_read_memory_integer (unwound_sp + 28, 4, byte_order, &xpsr)
3047 && (xpsr & (1 << 9)) != 0)
3048 cache->prev_sp += 4;
3049
3050 return cache;
3051}
3052
3053/* Implementation of function hook 'this_id' in
3054 'struct frame_uwnind'. */
3055
3056static void
3057arm_m_exception_this_id (struct frame_info *this_frame,
3058 void **this_cache,
3059 struct frame_id *this_id)
3060{
3061 struct arm_prologue_cache *cache;
3062
3063 if (*this_cache == NULL)
3064 *this_cache = arm_m_exception_cache (this_frame);
3065 cache = *this_cache;
3066
3067 /* Our frame ID for a stub frame is the current SP and LR. */
3068 *this_id = frame_id_build (cache->prev_sp,
3069 get_frame_pc (this_frame));
3070}
3071
3072/* Implementation of function hook 'prev_register' in
3073 'struct frame_uwnind'. */
3074
3075static struct value *
3076arm_m_exception_prev_register (struct frame_info *this_frame,
3077 void **this_cache,
3078 int prev_regnum)
3079{
3080 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3081 struct arm_prologue_cache *cache;
3082
3083 if (*this_cache == NULL)
3084 *this_cache = arm_m_exception_cache (this_frame);
3085 cache = *this_cache;
3086
3087 /* The value was already reconstructed into PREV_SP. */
3088 if (prev_regnum == ARM_SP_REGNUM)
3089 return frame_unwind_got_constant (this_frame, prev_regnum,
3090 cache->prev_sp);
3091
3092 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
3093 prev_regnum);
3094}
3095
3096/* Implementation of function hook 'sniffer' in
3097 'struct frame_uwnind'. */
3098
3099static int
3100arm_m_exception_unwind_sniffer (const struct frame_unwind *self,
3101 struct frame_info *this_frame,
3102 void **this_prologue_cache)
3103{
3104 CORE_ADDR this_pc = get_frame_pc (this_frame);
3105
3106 /* No need to check is_m; this sniffer is only registered for
3107 M-profile architectures. */
3108
3109 /* Exception frames return to one of these magic PCs. Other values
3110 are not defined as of v7-M. See details in "B1.5.8 Exception
3111 return behavior" in "ARMv7-M Architecture Reference Manual". */
3112 if (this_pc == 0xfffffff1 || this_pc == 0xfffffff9
3113 || this_pc == 0xfffffffd)
3114 return 1;
3115
3116 return 0;
3117}
3118
3119/* Frame unwinder for M-profile exceptions. */
3120
3121struct frame_unwind arm_m_exception_unwind =
3122{
3123 SIGTRAMP_FRAME,
3124 default_frame_unwind_stop_reason,
3125 arm_m_exception_this_id,
3126 arm_m_exception_prev_register,
3127 NULL,
3128 arm_m_exception_unwind_sniffer
3129};
3130
24de872b 3131static CORE_ADDR
a262aec2 3132arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
24de872b
DJ
3133{
3134 struct arm_prologue_cache *cache;
3135
eb5492fa 3136 if (*this_cache == NULL)
a262aec2 3137 *this_cache = arm_make_prologue_cache (this_frame);
eb5492fa
DJ
3138 cache = *this_cache;
3139
4be43953 3140 return cache->prev_sp - cache->framesize;
24de872b
DJ
3141}
3142
eb5492fa
DJ
3143struct frame_base arm_normal_base = {
3144 &arm_prologue_unwind,
3145 arm_normal_frame_base,
3146 arm_normal_frame_base,
3147 arm_normal_frame_base
3148};
3149
a262aec2 3150/* Assuming THIS_FRAME is a dummy, return the frame ID of that
eb5492fa
DJ
3151 dummy frame. The frame ID's base needs to match the TOS value
3152 saved by save_dummy_frame_tos() and returned from
3153 arm_push_dummy_call, and the PC needs to match the dummy frame's
3154 breakpoint. */
c906108c 3155
eb5492fa 3156static struct frame_id
a262aec2 3157arm_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
c906108c 3158{
0963b4bd
MS
3159 return frame_id_build (get_frame_register_unsigned (this_frame,
3160 ARM_SP_REGNUM),
a262aec2 3161 get_frame_pc (this_frame));
eb5492fa 3162}
c3b4394c 3163
eb5492fa
DJ
3164/* Given THIS_FRAME, find the previous frame's resume PC (which will
3165 be used to construct the previous frame's ID, after looking up the
3166 containing function). */
c3b4394c 3167
eb5492fa
DJ
3168static CORE_ADDR
3169arm_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
3170{
3171 CORE_ADDR pc;
3172 pc = frame_unwind_register_unsigned (this_frame, ARM_PC_REGNUM);
24568a2c 3173 return arm_addr_bits_remove (gdbarch, pc);
eb5492fa
DJ
3174}
3175
3176static CORE_ADDR
3177arm_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
3178{
3179 return frame_unwind_register_unsigned (this_frame, ARM_SP_REGNUM);
c906108c
SS
3180}
3181
b39cc962
DJ
3182static struct value *
3183arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
3184 int regnum)
3185{
24568a2c 3186 struct gdbarch * gdbarch = get_frame_arch (this_frame);
b39cc962 3187 CORE_ADDR lr, cpsr;
9779414d 3188 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
b39cc962
DJ
3189
3190 switch (regnum)
3191 {
3192 case ARM_PC_REGNUM:
3193 /* The PC is normally copied from the return column, which
3194 describes saves of LR. However, that version may have an
3195 extra bit set to indicate Thumb state. The bit is not
3196 part of the PC. */
3197 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3198 return frame_unwind_got_constant (this_frame, regnum,
24568a2c 3199 arm_addr_bits_remove (gdbarch, lr));
b39cc962
DJ
3200
3201 case ARM_PS_REGNUM:
3202 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
ca38c58e 3203 cpsr = get_frame_register_unsigned (this_frame, regnum);
b39cc962
DJ
3204 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3205 if (IS_THUMB_ADDR (lr))
9779414d 3206 cpsr |= t_bit;
b39cc962 3207 else
9779414d 3208 cpsr &= ~t_bit;
ca38c58e 3209 return frame_unwind_got_constant (this_frame, regnum, cpsr);
b39cc962
DJ
3210
3211 default:
3212 internal_error (__FILE__, __LINE__,
3213 _("Unexpected register %d"), regnum);
3214 }
3215}
3216
3217static void
3218arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
3219 struct dwarf2_frame_state_reg *reg,
3220 struct frame_info *this_frame)
3221{
3222 switch (regnum)
3223 {
3224 case ARM_PC_REGNUM:
3225 case ARM_PS_REGNUM:
3226 reg->how = DWARF2_FRAME_REG_FN;
3227 reg->loc.fn = arm_dwarf2_prev_register;
3228 break;
3229 case ARM_SP_REGNUM:
3230 reg->how = DWARF2_FRAME_REG_CFA;
3231 break;
3232 }
3233}
3234
4024ca99
UW
3235/* Return true if we are in the function's epilogue, i.e. after the
3236 instruction that destroyed the function's stack frame. */
3237
3238static int
3239thumb_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3240{
3241 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3242 unsigned int insn, insn2;
3243 int found_return = 0, found_stack_adjust = 0;
3244 CORE_ADDR func_start, func_end;
3245 CORE_ADDR scan_pc;
3246 gdb_byte buf[4];
3247
3248 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3249 return 0;
3250
3251 /* The epilogue is a sequence of instructions along the following lines:
3252
3253 - add stack frame size to SP or FP
3254 - [if frame pointer used] restore SP from FP
3255 - restore registers from SP [may include PC]
3256 - a return-type instruction [if PC wasn't already restored]
3257
3258 In a first pass, we scan forward from the current PC and verify the
3259 instructions we find as compatible with this sequence, ending in a
3260 return instruction.
3261
3262 However, this is not sufficient to distinguish indirect function calls
3263 within a function from indirect tail calls in the epilogue in some cases.
3264 Therefore, if we didn't already find any SP-changing instruction during
3265 forward scan, we add a backward scanning heuristic to ensure we actually
3266 are in the epilogue. */
3267
3268 scan_pc = pc;
3269 while (scan_pc < func_end && !found_return)
3270 {
3271 if (target_read_memory (scan_pc, buf, 2))
3272 break;
3273
3274 scan_pc += 2;
3275 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3276
3277 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
3278 found_return = 1;
3279 else if (insn == 0x46f7) /* mov pc, lr */
3280 found_return = 1;
540314bd 3281 else if (thumb_instruction_restores_sp (insn))
4024ca99 3282 {
b7576e5c 3283 if ((insn & 0xff00) == 0xbd00) /* pop <registers, PC> */
4024ca99
UW
3284 found_return = 1;
3285 }
db24da6d 3286 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instruction */
4024ca99
UW
3287 {
3288 if (target_read_memory (scan_pc, buf, 2))
3289 break;
3290
3291 scan_pc += 2;
3292 insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3293
3294 if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3295 {
4024ca99
UW
3296 if (insn2 & 0x8000) /* <registers> include PC. */
3297 found_return = 1;
3298 }
3299 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3300 && (insn2 & 0x0fff) == 0x0b04)
3301 {
4024ca99
UW
3302 if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC. */
3303 found_return = 1;
3304 }
3305 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3306 && (insn2 & 0x0e00) == 0x0a00)
6b65d1b6 3307 ;
4024ca99
UW
3308 else
3309 break;
3310 }
3311 else
3312 break;
3313 }
3314
3315 if (!found_return)
3316 return 0;
3317
3318 /* Since any instruction in the epilogue sequence, with the possible
3319 exception of return itself, updates the stack pointer, we need to
3320 scan backwards for at most one instruction. Try either a 16-bit or
3321 a 32-bit instruction. This is just a heuristic, so we do not worry
0963b4bd 3322 too much about false positives. */
4024ca99 3323
6b65d1b6
YQ
3324 if (pc - 4 < func_start)
3325 return 0;
3326 if (target_read_memory (pc - 4, buf, 4))
3327 return 0;
4024ca99 3328
6b65d1b6
YQ
3329 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3330 insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);
3331
3332 if (thumb_instruction_restores_sp (insn2))
3333 found_stack_adjust = 1;
3334 else if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3335 found_stack_adjust = 1;
3336 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3337 && (insn2 & 0x0fff) == 0x0b04)
3338 found_stack_adjust = 1;
3339 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3340 && (insn2 & 0x0e00) == 0x0a00)
3341 found_stack_adjust = 1;
4024ca99
UW
3342
3343 return found_stack_adjust;
3344}
3345
3346/* Return true if we are in the function's epilogue, i.e. after the
3347 instruction that destroyed the function's stack frame. */
3348
3349static int
3350arm_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3351{
3352 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3353 unsigned int insn;
3354 int found_return, found_stack_adjust;
3355 CORE_ADDR func_start, func_end;
3356
3357 if (arm_pc_is_thumb (gdbarch, pc))
3358 return thumb_in_function_epilogue_p (gdbarch, pc);
3359
3360 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3361 return 0;
3362
3363 /* We are in the epilogue if the previous instruction was a stack
3364 adjustment and the next instruction is a possible return (bx, mov
3365 pc, or pop). We could have to scan backwards to find the stack
3366 adjustment, or forwards to find the return, but this is a decent
3367 approximation. First scan forwards. */
3368
3369 found_return = 0;
3370 insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
3371 if (bits (insn, 28, 31) != INST_NV)
3372 {
3373 if ((insn & 0x0ffffff0) == 0x012fff10)
3374 /* BX. */
3375 found_return = 1;
3376 else if ((insn & 0x0ffffff0) == 0x01a0f000)
3377 /* MOV PC. */
3378 found_return = 1;
3379 else if ((insn & 0x0fff0000) == 0x08bd0000
3380 && (insn & 0x0000c000) != 0)
3381 /* POP (LDMIA), including PC or LR. */
3382 found_return = 1;
3383 }
3384
3385 if (!found_return)
3386 return 0;
3387
3388 /* Scan backwards. This is just a heuristic, so do not worry about
3389 false positives from mode changes. */
3390
3391 if (pc < func_start + 4)
3392 return 0;
3393
73c964d6 3394 found_stack_adjust = 0;
4024ca99
UW
3395 insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
3396 if (bits (insn, 28, 31) != INST_NV)
3397 {
3398 if ((insn & 0x0df0f000) == 0x0080d000)
3399 /* ADD SP (register or immediate). */
3400 found_stack_adjust = 1;
3401 else if ((insn & 0x0df0f000) == 0x0040d000)
3402 /* SUB SP (register or immediate). */
3403 found_stack_adjust = 1;
3404 else if ((insn & 0x0ffffff0) == 0x01a0d000)
3405 /* MOV SP. */
77bc0675 3406 found_stack_adjust = 1;
4024ca99
UW
3407 else if ((insn & 0x0fff0000) == 0x08bd0000)
3408 /* POP (LDMIA). */
3409 found_stack_adjust = 1;
fc51cce1
MGD
3410 else if ((insn & 0x0fff0000) == 0x049d0000)
3411 /* POP of a single register. */
3412 found_stack_adjust = 1;
4024ca99
UW
3413 }
3414
3415 if (found_stack_adjust)
3416 return 1;
3417
3418 return 0;
3419}
3420
3421
2dd604e7
RE
3422/* When arguments must be pushed onto the stack, they go on in reverse
3423 order. The code below implements a FILO (stack) to do this. */
3424
3425struct stack_item
3426{
3427 int len;
3428 struct stack_item *prev;
3429 void *data;
3430};
3431
3432static struct stack_item *
8c6363cf 3433push_stack_item (struct stack_item *prev, const void *contents, int len)
2dd604e7
RE
3434{
3435 struct stack_item *si;
3436 si = xmalloc (sizeof (struct stack_item));
226c7fbc 3437 si->data = xmalloc (len);
2dd604e7
RE
3438 si->len = len;
3439 si->prev = prev;
3440 memcpy (si->data, contents, len);
3441 return si;
3442}
3443
3444static struct stack_item *
3445pop_stack_item (struct stack_item *si)
3446{
3447 struct stack_item *dead = si;
3448 si = si->prev;
3449 xfree (dead->data);
3450 xfree (dead);
3451 return si;
3452}
3453
2af48f68
PB
3454
3455/* Return the alignment (in bytes) of the given type. */
3456
3457static int
3458arm_type_align (struct type *t)
3459{
3460 int n;
3461 int align;
3462 int falign;
3463
3464 t = check_typedef (t);
3465 switch (TYPE_CODE (t))
3466 {
3467 default:
3468 /* Should never happen. */
3469 internal_error (__FILE__, __LINE__, _("unknown type alignment"));
3470 return 4;
3471
3472 case TYPE_CODE_PTR:
3473 case TYPE_CODE_ENUM:
3474 case TYPE_CODE_INT:
3475 case TYPE_CODE_FLT:
3476 case TYPE_CODE_SET:
3477 case TYPE_CODE_RANGE:
2af48f68
PB
3478 case TYPE_CODE_REF:
3479 case TYPE_CODE_CHAR:
3480 case TYPE_CODE_BOOL:
3481 return TYPE_LENGTH (t);
3482
3483 case TYPE_CODE_ARRAY:
3484 case TYPE_CODE_COMPLEX:
3485 /* TODO: What about vector types? */
3486 return arm_type_align (TYPE_TARGET_TYPE (t));
3487
3488 case TYPE_CODE_STRUCT:
3489 case TYPE_CODE_UNION:
3490 align = 1;
3491 for (n = 0; n < TYPE_NFIELDS (t); n++)
3492 {
3493 falign = arm_type_align (TYPE_FIELD_TYPE (t, n));
3494 if (falign > align)
3495 align = falign;
3496 }
3497 return align;
3498 }
3499}
3500
90445bd3
DJ
3501/* Possible base types for a candidate for passing and returning in
3502 VFP registers. */
3503
3504enum arm_vfp_cprc_base_type
3505{
3506 VFP_CPRC_UNKNOWN,
3507 VFP_CPRC_SINGLE,
3508 VFP_CPRC_DOUBLE,
3509 VFP_CPRC_VEC64,
3510 VFP_CPRC_VEC128
3511};
3512
3513/* The length of one element of base type B. */
3514
3515static unsigned
3516arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
3517{
3518 switch (b)
3519 {
3520 case VFP_CPRC_SINGLE:
3521 return 4;
3522 case VFP_CPRC_DOUBLE:
3523 return 8;
3524 case VFP_CPRC_VEC64:
3525 return 8;
3526 case VFP_CPRC_VEC128:
3527 return 16;
3528 default:
3529 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3530 (int) b);
3531 }
3532}
3533
3534/* The character ('s', 'd' or 'q') for the type of VFP register used
3535 for passing base type B. */
3536
3537static int
3538arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
3539{
3540 switch (b)
3541 {
3542 case VFP_CPRC_SINGLE:
3543 return 's';
3544 case VFP_CPRC_DOUBLE:
3545 return 'd';
3546 case VFP_CPRC_VEC64:
3547 return 'd';
3548 case VFP_CPRC_VEC128:
3549 return 'q';
3550 default:
3551 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3552 (int) b);
3553 }
3554}
3555
3556/* Determine whether T may be part of a candidate for passing and
3557 returning in VFP registers, ignoring the limit on the total number
3558 of components. If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
3559 classification of the first valid component found; if it is not
3560 VFP_CPRC_UNKNOWN, all components must have the same classification
3561 as *BASE_TYPE. If it is found that T contains a type not permitted
3562 for passing and returning in VFP registers, a type differently
3563 classified from *BASE_TYPE, or two types differently classified
3564 from each other, return -1, otherwise return the total number of
3565 base-type elements found (possibly 0 in an empty structure or
817e0957
YQ
3566 array). Vector types are not currently supported, matching the
3567 generic AAPCS support. */
90445bd3
DJ
3568
3569static int
3570arm_vfp_cprc_sub_candidate (struct type *t,
3571 enum arm_vfp_cprc_base_type *base_type)
3572{
3573 t = check_typedef (t);
3574 switch (TYPE_CODE (t))
3575 {
3576 case TYPE_CODE_FLT:
3577 switch (TYPE_LENGTH (t))
3578 {
3579 case 4:
3580 if (*base_type == VFP_CPRC_UNKNOWN)
3581 *base_type = VFP_CPRC_SINGLE;
3582 else if (*base_type != VFP_CPRC_SINGLE)
3583 return -1;
3584 return 1;
3585
3586 case 8:
3587 if (*base_type == VFP_CPRC_UNKNOWN)
3588 *base_type = VFP_CPRC_DOUBLE;
3589 else if (*base_type != VFP_CPRC_DOUBLE)
3590 return -1;
3591 return 1;
3592
3593 default:
3594 return -1;
3595 }
3596 break;
3597
817e0957
YQ
3598 case TYPE_CODE_COMPLEX:
3599 /* Arguments of complex T where T is one of the types float or
3600 double get treated as if they are implemented as:
3601
3602 struct complexT
3603 {
3604 T real;
3605 T imag;
5f52445b
YQ
3606 };
3607
3608 */
817e0957
YQ
3609 switch (TYPE_LENGTH (t))
3610 {
3611 case 8:
3612 if (*base_type == VFP_CPRC_UNKNOWN)
3613 *base_type = VFP_CPRC_SINGLE;
3614 else if (*base_type != VFP_CPRC_SINGLE)
3615 return -1;
3616 return 2;
3617
3618 case 16:
3619 if (*base_type == VFP_CPRC_UNKNOWN)
3620 *base_type = VFP_CPRC_DOUBLE;
3621 else if (*base_type != VFP_CPRC_DOUBLE)
3622 return -1;
3623 return 2;
3624
3625 default:
3626 return -1;
3627 }
3628 break;
3629
90445bd3
DJ
3630 case TYPE_CODE_ARRAY:
3631 {
3632 int count;
3633 unsigned unitlen;
3634 count = arm_vfp_cprc_sub_candidate (TYPE_TARGET_TYPE (t), base_type);
3635 if (count == -1)
3636 return -1;
3637 if (TYPE_LENGTH (t) == 0)
3638 {
3639 gdb_assert (count == 0);
3640 return 0;
3641 }
3642 else if (count == 0)
3643 return -1;
3644 unitlen = arm_vfp_cprc_unit_length (*base_type);
3645 gdb_assert ((TYPE_LENGTH (t) % unitlen) == 0);
3646 return TYPE_LENGTH (t) / unitlen;
3647 }
3648 break;
3649
3650 case TYPE_CODE_STRUCT:
3651 {
3652 int count = 0;
3653 unsigned unitlen;
3654 int i;
3655 for (i = 0; i < TYPE_NFIELDS (t); i++)
3656 {
3657 int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3658 base_type);
3659 if (sub_count == -1)
3660 return -1;
3661 count += sub_count;
3662 }
3663 if (TYPE_LENGTH (t) == 0)
3664 {
3665 gdb_assert (count == 0);
3666 return 0;
3667 }
3668 else if (count == 0)
3669 return -1;
3670 unitlen = arm_vfp_cprc_unit_length (*base_type);
3671 if (TYPE_LENGTH (t) != unitlen * count)
3672 return -1;
3673 return count;
3674 }
3675
3676 case TYPE_CODE_UNION:
3677 {
3678 int count = 0;
3679 unsigned unitlen;
3680 int i;
3681 for (i = 0; i < TYPE_NFIELDS (t); i++)
3682 {
3683 int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3684 base_type);
3685 if (sub_count == -1)
3686 return -1;
3687 count = (count > sub_count ? count : sub_count);
3688 }
3689 if (TYPE_LENGTH (t) == 0)
3690 {
3691 gdb_assert (count == 0);
3692 return 0;
3693 }
3694 else if (count == 0)
3695 return -1;
3696 unitlen = arm_vfp_cprc_unit_length (*base_type);
3697 if (TYPE_LENGTH (t) != unitlen * count)
3698 return -1;
3699 return count;
3700 }
3701
3702 default:
3703 break;
3704 }
3705
3706 return -1;
3707}
3708
3709/* Determine whether T is a VFP co-processor register candidate (CPRC)
3710 if passed to or returned from a non-variadic function with the VFP
3711 ABI in effect. Return 1 if it is, 0 otherwise. If it is, set
3712 *BASE_TYPE to the base type for T and *COUNT to the number of
3713 elements of that base type before returning. */
3714
3715static int
3716arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
3717 int *count)
3718{
3719 enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
3720 int c = arm_vfp_cprc_sub_candidate (t, &b);
3721 if (c <= 0 || c > 4)
3722 return 0;
3723 *base_type = b;
3724 *count = c;
3725 return 1;
3726}
3727
3728/* Return 1 if the VFP ABI should be used for passing arguments to and
3729 returning values from a function of type FUNC_TYPE, 0
3730 otherwise. */
3731
3732static int
3733arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
3734{
3735 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3736 /* Variadic functions always use the base ABI. Assume that functions
3737 without debug info are not variadic. */
3738 if (func_type && TYPE_VARARGS (check_typedef (func_type)))
3739 return 0;
3740 /* The VFP ABI is only supported as a variant of AAPCS. */
3741 if (tdep->arm_abi != ARM_ABI_AAPCS)
3742 return 0;
3743 return gdbarch_tdep (gdbarch)->fp_model == ARM_FLOAT_VFP;
3744}
3745
3746/* We currently only support passing parameters in integer registers, which
3747 conforms with GCC's default model, and VFP argument passing following
3748 the VFP variant of AAPCS. Several other variants exist and
2dd604e7
RE
3749 we should probably support some of them based on the selected ABI. */
3750
3751static CORE_ADDR
7d9b040b 3752arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6a65450a
AC
3753 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
3754 struct value **args, CORE_ADDR sp, int struct_return,
3755 CORE_ADDR struct_addr)
2dd604e7 3756{
e17a4113 3757 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2dd604e7
RE
3758 int argnum;
3759 int argreg;
3760 int nstack;
3761 struct stack_item *si = NULL;
90445bd3
DJ
3762 int use_vfp_abi;
3763 struct type *ftype;
3764 unsigned vfp_regs_free = (1 << 16) - 1;
3765
3766 /* Determine the type of this function and whether the VFP ABI
3767 applies. */
3768 ftype = check_typedef (value_type (function));
3769 if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
3770 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
3771 use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
2dd604e7 3772
6a65450a
AC
3773 /* Set the return address. For the ARM, the return breakpoint is
3774 always at BP_ADDR. */
9779414d 3775 if (arm_pc_is_thumb (gdbarch, bp_addr))
9dca5578 3776 bp_addr |= 1;
6a65450a 3777 regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
2dd604e7
RE
3778
3779 /* Walk through the list of args and determine how large a temporary
3780 stack is required. Need to take care here as structs may be
7a9dd1b2 3781 passed on the stack, and we have to push them. */
2dd604e7
RE
3782 nstack = 0;
3783
3784 argreg = ARM_A1_REGNUM;
3785 nstack = 0;
3786
2dd604e7
RE
3787 /* The struct_return pointer occupies the first parameter
3788 passing register. */
3789 if (struct_return)
3790 {
3791 if (arm_debug)
5af949e3 3792 fprintf_unfiltered (gdb_stdlog, "struct return in %s = %s\n",
2af46ca0 3793 gdbarch_register_name (gdbarch, argreg),
5af949e3 3794 paddress (gdbarch, struct_addr));
2dd604e7
RE
3795 regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
3796 argreg++;
3797 }
3798
3799 for (argnum = 0; argnum < nargs; argnum++)
3800 {
3801 int len;
3802 struct type *arg_type;
3803 struct type *target_type;
3804 enum type_code typecode;
8c6363cf 3805 const bfd_byte *val;
2af48f68 3806 int align;
90445bd3
DJ
3807 enum arm_vfp_cprc_base_type vfp_base_type;
3808 int vfp_base_count;
3809 int may_use_core_reg = 1;
2dd604e7 3810
df407dfe 3811 arg_type = check_typedef (value_type (args[argnum]));
2dd604e7
RE
3812 len = TYPE_LENGTH (arg_type);
3813 target_type = TYPE_TARGET_TYPE (arg_type);
3814 typecode = TYPE_CODE (arg_type);
8c6363cf 3815 val = value_contents (args[argnum]);
2dd604e7 3816
2af48f68
PB
3817 align = arm_type_align (arg_type);
3818 /* Round alignment up to a whole number of words. */
3819 align = (align + INT_REGISTER_SIZE - 1) & ~(INT_REGISTER_SIZE - 1);
3820 /* Different ABIs have different maximum alignments. */
3821 if (gdbarch_tdep (gdbarch)->arm_abi == ARM_ABI_APCS)
3822 {
3823 /* The APCS ABI only requires word alignment. */
3824 align = INT_REGISTER_SIZE;
3825 }
3826 else
3827 {
3828 /* The AAPCS requires at most doubleword alignment. */
3829 if (align > INT_REGISTER_SIZE * 2)
3830 align = INT_REGISTER_SIZE * 2;
3831 }
3832
90445bd3
DJ
3833 if (use_vfp_abi
3834 && arm_vfp_call_candidate (arg_type, &vfp_base_type,
3835 &vfp_base_count))
3836 {
3837 int regno;
3838 int unit_length;
3839 int shift;
3840 unsigned mask;
3841
3842 /* Because this is a CPRC it cannot go in a core register or
3843 cause a core register to be skipped for alignment.
3844 Either it goes in VFP registers and the rest of this loop
3845 iteration is skipped for this argument, or it goes on the
3846 stack (and the stack alignment code is correct for this
3847 case). */
3848 may_use_core_reg = 0;
3849
3850 unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
3851 shift = unit_length / 4;
3852 mask = (1 << (shift * vfp_base_count)) - 1;
3853 for (regno = 0; regno < 16; regno += shift)
3854 if (((vfp_regs_free >> regno) & mask) == mask)
3855 break;
3856
3857 if (regno < 16)
3858 {
3859 int reg_char;
3860 int reg_scaled;
3861 int i;
3862
3863 vfp_regs_free &= ~(mask << regno);
3864 reg_scaled = regno / shift;
3865 reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
3866 for (i = 0; i < vfp_base_count; i++)
3867 {
3868 char name_buf[4];
3869 int regnum;
58d6951d
DJ
3870 if (reg_char == 'q')
3871 arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
90445bd3 3872 val + i * unit_length);
58d6951d
DJ
3873 else
3874 {
8c042590
PM
3875 xsnprintf (name_buf, sizeof (name_buf), "%c%d",
3876 reg_char, reg_scaled + i);
58d6951d
DJ
3877 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
3878 strlen (name_buf));
3879 regcache_cooked_write (regcache, regnum,
3880 val + i * unit_length);
3881 }
90445bd3
DJ
3882 }
3883 continue;
3884 }
3885 else
3886 {
3887 /* This CPRC could not go in VFP registers, so all VFP
3888 registers are now marked as used. */
3889 vfp_regs_free = 0;
3890 }
3891 }
3892
2af48f68
PB
3893 /* Push stack padding for dowubleword alignment. */
3894 if (nstack & (align - 1))
3895 {
3896 si = push_stack_item (si, val, INT_REGISTER_SIZE);
3897 nstack += INT_REGISTER_SIZE;
3898 }
3899
3900 /* Doubleword aligned quantities must go in even register pairs. */
90445bd3
DJ
3901 if (may_use_core_reg
3902 && argreg <= ARM_LAST_ARG_REGNUM
2af48f68
PB
3903 && align > INT_REGISTER_SIZE
3904 && argreg & 1)
3905 argreg++;
3906
2dd604e7
RE
3907 /* If the argument is a pointer to a function, and it is a
3908 Thumb function, create a LOCAL copy of the value and set
3909 the THUMB bit in it. */
3910 if (TYPE_CODE_PTR == typecode
3911 && target_type != NULL
f96b8fa0 3912 && TYPE_CODE_FUNC == TYPE_CODE (check_typedef (target_type)))
2dd604e7 3913 {
e17a4113 3914 CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
9779414d 3915 if (arm_pc_is_thumb (gdbarch, regval))
2dd604e7 3916 {
8c6363cf
TT
3917 bfd_byte *copy = alloca (len);
3918 store_unsigned_integer (copy, len, byte_order,
e17a4113 3919 MAKE_THUMB_ADDR (regval));
8c6363cf 3920 val = copy;
2dd604e7
RE
3921 }
3922 }
3923
3924 /* Copy the argument to general registers or the stack in
3925 register-sized pieces. Large arguments are split between
3926 registers and stack. */
3927 while (len > 0)
3928 {
f0c9063c 3929 int partial_len = len < INT_REGISTER_SIZE ? len : INT_REGISTER_SIZE;
2dd604e7 3930
90445bd3 3931 if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
2dd604e7
RE
3932 {
3933 /* The argument is being passed in a general purpose
3934 register. */
e17a4113
UW
3935 CORE_ADDR regval
3936 = extract_unsigned_integer (val, partial_len, byte_order);
3937 if (byte_order == BFD_ENDIAN_BIG)
8bf8793c 3938 regval <<= (INT_REGISTER_SIZE - partial_len) * 8;
2dd604e7
RE
3939 if (arm_debug)
3940 fprintf_unfiltered (gdb_stdlog, "arg %d in %s = 0x%s\n",
c9f4d572
UW
3941 argnum,
3942 gdbarch_register_name
2af46ca0 3943 (gdbarch, argreg),
f0c9063c 3944 phex (regval, INT_REGISTER_SIZE));
2dd604e7
RE
3945 regcache_cooked_write_unsigned (regcache, argreg, regval);
3946 argreg++;
3947 }
3948 else
3949 {
3950 /* Push the arguments onto the stack. */
3951 if (arm_debug)
3952 fprintf_unfiltered (gdb_stdlog, "arg %d @ sp + %d\n",
3953 argnum, nstack);
f0c9063c
UW
3954 si = push_stack_item (si, val, INT_REGISTER_SIZE);
3955 nstack += INT_REGISTER_SIZE;
2dd604e7
RE
3956 }
3957
3958 len -= partial_len;
3959 val += partial_len;
3960 }
3961 }
3962 /* If we have an odd number of words to push, then decrement the stack
3963 by one word now, so first stack argument will be dword aligned. */
3964 if (nstack & 4)
3965 sp -= 4;
3966
3967 while (si)
3968 {
3969 sp -= si->len;
3970 write_memory (sp, si->data, si->len);
3971 si = pop_stack_item (si);
3972 }
3973
3974 /* Finally, update teh SP register. */
3975 regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
3976
3977 return sp;
3978}
3979
f53f0d0b
PB
3980
3981/* Always align the frame to an 8-byte boundary. This is required on
3982 some platforms and harmless on the rest. */
3983
3984static CORE_ADDR
3985arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
3986{
3987 /* Align the stack to eight bytes. */
3988 return sp & ~ (CORE_ADDR) 7;
3989}
3990
c906108c 3991static void
12b27276 3992print_fpu_flags (struct ui_file *file, int flags)
c906108c 3993{
c5aa993b 3994 if (flags & (1 << 0))
12b27276 3995 fputs_filtered ("IVO ", file);
c5aa993b 3996 if (flags & (1 << 1))
12b27276 3997 fputs_filtered ("DVZ ", file);
c5aa993b 3998 if (flags & (1 << 2))
12b27276 3999 fputs_filtered ("OFL ", file);
c5aa993b 4000 if (flags & (1 << 3))
12b27276 4001 fputs_filtered ("UFL ", file);
c5aa993b 4002 if (flags & (1 << 4))
12b27276
WN
4003 fputs_filtered ("INX ", file);
4004 fputc_filtered ('\n', file);
c906108c
SS
4005}
4006
5e74b15c
RE
4007/* Print interesting information about the floating point processor
4008 (if present) or emulator. */
34e8f22d 4009static void
d855c300 4010arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
23e3a7ac 4011 struct frame_info *frame, const char *args)
c906108c 4012{
9c9acae0 4013 unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
c5aa993b
JM
4014 int type;
4015
4016 type = (status >> 24) & 127;
edefbb7c 4017 if (status & (1 << 31))
12b27276 4018 fprintf_filtered (file, _("Hardware FPU type %d\n"), type);
edefbb7c 4019 else
12b27276 4020 fprintf_filtered (file, _("Software FPU type %d\n"), type);
edefbb7c 4021 /* i18n: [floating point unit] mask */
12b27276
WN
4022 fputs_filtered (_("mask: "), file);
4023 print_fpu_flags (file, status >> 16);
edefbb7c 4024 /* i18n: [floating point unit] flags */
12b27276
WN
4025 fputs_filtered (_("flags: "), file);
4026 print_fpu_flags (file, status);
c906108c
SS
4027}
4028
27067745
UW
4029/* Construct the ARM extended floating point type. */
4030static struct type *
4031arm_ext_type (struct gdbarch *gdbarch)
4032{
4033 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4034
4035 if (!tdep->arm_ext_type)
4036 tdep->arm_ext_type
e9bb382b 4037 = arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
27067745
UW
4038 floatformats_arm_ext);
4039
4040 return tdep->arm_ext_type;
4041}
4042
58d6951d
DJ
4043static struct type *
4044arm_neon_double_type (struct gdbarch *gdbarch)
4045{
4046 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4047
4048 if (tdep->neon_double_type == NULL)
4049 {
4050 struct type *t, *elem;
4051
4052 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
4053 TYPE_CODE_UNION);
4054 elem = builtin_type (gdbarch)->builtin_uint8;
4055 append_composite_type_field (t, "u8", init_vector_type (elem, 8));
4056 elem = builtin_type (gdbarch)->builtin_uint16;
4057 append_composite_type_field (t, "u16", init_vector_type (elem, 4));
4058 elem = builtin_type (gdbarch)->builtin_uint32;
4059 append_composite_type_field (t, "u32", init_vector_type (elem, 2));
4060 elem = builtin_type (gdbarch)->builtin_uint64;
4061 append_composite_type_field (t, "u64", elem);
4062 elem = builtin_type (gdbarch)->builtin_float;
4063 append_composite_type_field (t, "f32", init_vector_type (elem, 2));
4064 elem = builtin_type (gdbarch)->builtin_double;
4065 append_composite_type_field (t, "f64", elem);
4066
4067 TYPE_VECTOR (t) = 1;
4068 TYPE_NAME (t) = "neon_d";
4069 tdep->neon_double_type = t;
4070 }
4071
4072 return tdep->neon_double_type;
4073}
4074
4075/* FIXME: The vector types are not correctly ordered on big-endian
4076 targets. Just as s0 is the low bits of d0, d0[0] is also the low
4077 bits of d0 - regardless of what unit size is being held in d0. So
4078 the offset of the first uint8 in d0 is 7, but the offset of the
4079 first float is 4. This code works as-is for little-endian
4080 targets. */
4081
4082static struct type *
4083arm_neon_quad_type (struct gdbarch *gdbarch)
4084{
4085 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4086
4087 if (tdep->neon_quad_type == NULL)
4088 {
4089 struct type *t, *elem;
4090
4091 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
4092 TYPE_CODE_UNION);
4093 elem = builtin_type (gdbarch)->builtin_uint8;
4094 append_composite_type_field (t, "u8", init_vector_type (elem, 16));
4095 elem = builtin_type (gdbarch)->builtin_uint16;
4096 append_composite_type_field (t, "u16", init_vector_type (elem, 8));
4097 elem = builtin_type (gdbarch)->builtin_uint32;
4098 append_composite_type_field (t, "u32", init_vector_type (elem, 4));
4099 elem = builtin_type (gdbarch)->builtin_uint64;
4100 append_composite_type_field (t, "u64", init_vector_type (elem, 2));
4101 elem = builtin_type (gdbarch)->builtin_float;
4102 append_composite_type_field (t, "f32", init_vector_type (elem, 4));
4103 elem = builtin_type (gdbarch)->builtin_double;
4104 append_composite_type_field (t, "f64", init_vector_type (elem, 2));
4105
4106 TYPE_VECTOR (t) = 1;
4107 TYPE_NAME (t) = "neon_q";
4108 tdep->neon_quad_type = t;
4109 }
4110
4111 return tdep->neon_quad_type;
4112}
4113
34e8f22d
RE
4114/* Return the GDB type object for the "standard" data type of data in
4115 register N. */
4116
4117static struct type *
7a5ea0d4 4118arm_register_type (struct gdbarch *gdbarch, int regnum)
032758dc 4119{
58d6951d
DJ
4120 int num_regs = gdbarch_num_regs (gdbarch);
4121
4122 if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
4123 && regnum >= num_regs && regnum < num_regs + 32)
4124 return builtin_type (gdbarch)->builtin_float;
4125
4126 if (gdbarch_tdep (gdbarch)->have_neon_pseudos
4127 && regnum >= num_regs + 32 && regnum < num_regs + 32 + 16)
4128 return arm_neon_quad_type (gdbarch);
4129
4130 /* If the target description has register information, we are only
4131 in this function so that we can override the types of
4132 double-precision registers for NEON. */
4133 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
4134 {
4135 struct type *t = tdesc_register_type (gdbarch, regnum);
4136
4137 if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
4138 && TYPE_CODE (t) == TYPE_CODE_FLT
4139 && gdbarch_tdep (gdbarch)->have_neon)
4140 return arm_neon_double_type (gdbarch);
4141 else
4142 return t;
4143 }
4144
34e8f22d 4145 if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
58d6951d
DJ
4146 {
4147 if (!gdbarch_tdep (gdbarch)->have_fpa_registers)
4148 return builtin_type (gdbarch)->builtin_void;
4149
4150 return arm_ext_type (gdbarch);
4151 }
e4c16157 4152 else if (regnum == ARM_SP_REGNUM)
0dfff4cb 4153 return builtin_type (gdbarch)->builtin_data_ptr;
e4c16157 4154 else if (regnum == ARM_PC_REGNUM)
0dfff4cb 4155 return builtin_type (gdbarch)->builtin_func_ptr;
ff6f572f
DJ
4156 else if (regnum >= ARRAY_SIZE (arm_register_names))
4157 /* These registers are only supported on targets which supply
4158 an XML description. */
df4df182 4159 return builtin_type (gdbarch)->builtin_int0;
032758dc 4160 else
df4df182 4161 return builtin_type (gdbarch)->builtin_uint32;
032758dc
AC
4162}
4163
ff6f572f
DJ
4164/* Map a DWARF register REGNUM onto the appropriate GDB register
4165 number. */
4166
4167static int
d3f73121 4168arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
ff6f572f
DJ
4169{
4170 /* Core integer regs. */
4171 if (reg >= 0 && reg <= 15)
4172 return reg;
4173
4174 /* Legacy FPA encoding. These were once used in a way which
4175 overlapped with VFP register numbering, so their use is
4176 discouraged, but GDB doesn't support the ARM toolchain
4177 which used them for VFP. */
4178 if (reg >= 16 && reg <= 23)
4179 return ARM_F0_REGNUM + reg - 16;
4180
4181 /* New assignments for the FPA registers. */
4182 if (reg >= 96 && reg <= 103)
4183 return ARM_F0_REGNUM + reg - 96;
4184
4185 /* WMMX register assignments. */
4186 if (reg >= 104 && reg <= 111)
4187 return ARM_WCGR0_REGNUM + reg - 104;
4188
4189 if (reg >= 112 && reg <= 127)
4190 return ARM_WR0_REGNUM + reg - 112;
4191
4192 if (reg >= 192 && reg <= 199)
4193 return ARM_WC0_REGNUM + reg - 192;
4194
58d6951d
DJ
4195 /* VFP v2 registers. A double precision value is actually
4196 in d1 rather than s2, but the ABI only defines numbering
4197 for the single precision registers. This will "just work"
4198 in GDB for little endian targets (we'll read eight bytes,
4199 starting in s0 and then progressing to s1), but will be
4200 reversed on big endian targets with VFP. This won't
4201 be a problem for the new Neon quad registers; you're supposed
4202 to use DW_OP_piece for those. */
4203 if (reg >= 64 && reg <= 95)
4204 {
4205 char name_buf[4];
4206
8c042590 4207 xsnprintf (name_buf, sizeof (name_buf), "s%d", reg - 64);
58d6951d
DJ
4208 return user_reg_map_name_to_regnum (gdbarch, name_buf,
4209 strlen (name_buf));
4210 }
4211
4212 /* VFP v3 / Neon registers. This range is also used for VFP v2
4213 registers, except that it now describes d0 instead of s0. */
4214 if (reg >= 256 && reg <= 287)
4215 {
4216 char name_buf[4];
4217
8c042590 4218 xsnprintf (name_buf, sizeof (name_buf), "d%d", reg - 256);
58d6951d
DJ
4219 return user_reg_map_name_to_regnum (gdbarch, name_buf,
4220 strlen (name_buf));
4221 }
4222
ff6f572f
DJ
4223 return -1;
4224}
4225
26216b98
AC
4226/* Map GDB internal REGNUM onto the Arm simulator register numbers. */
4227static int
e7faf938 4228arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
26216b98
AC
4229{
4230 int reg = regnum;
e7faf938 4231 gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
26216b98 4232
ff6f572f
DJ
4233 if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
4234 return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
4235
4236 if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
4237 return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
4238
4239 if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
4240 return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
4241
26216b98
AC
4242 if (reg < NUM_GREGS)
4243 return SIM_ARM_R0_REGNUM + reg;
4244 reg -= NUM_GREGS;
4245
4246 if (reg < NUM_FREGS)
4247 return SIM_ARM_FP0_REGNUM + reg;
4248 reg -= NUM_FREGS;
4249
4250 if (reg < NUM_SREGS)
4251 return SIM_ARM_FPS_REGNUM + reg;
4252 reg -= NUM_SREGS;
4253
edefbb7c 4254 internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
26216b98 4255}
34e8f22d 4256
a37b3cc0
AC
4257/* NOTE: cagney/2001-08-20: Both convert_from_extended() and
4258 convert_to_extended() use floatformat_arm_ext_littlebyte_bigword.
4259 It is thought that this is is the floating-point register format on
4260 little-endian systems. */
c906108c 4261
ed9a39eb 4262static void
b508a996 4263convert_from_extended (const struct floatformat *fmt, const void *ptr,
be8626e0 4264 void *dbl, int endianess)
c906108c 4265{
a37b3cc0 4266 DOUBLEST d;
be8626e0
MD
4267
4268 if (endianess == BFD_ENDIAN_BIG)
a37b3cc0
AC
4269 floatformat_to_doublest (&floatformat_arm_ext_big, ptr, &d);
4270 else
4271 floatformat_to_doublest (&floatformat_arm_ext_littlebyte_bigword,
4272 ptr, &d);
b508a996 4273 floatformat_from_doublest (fmt, &d, dbl);
c906108c
SS
4274}
4275
34e8f22d 4276static void
be8626e0
MD
4277convert_to_extended (const struct floatformat *fmt, void *dbl, const void *ptr,
4278 int endianess)
c906108c 4279{
a37b3cc0 4280 DOUBLEST d;
be8626e0 4281
b508a996 4282 floatformat_to_doublest (fmt, ptr, &d);
be8626e0 4283 if (endianess == BFD_ENDIAN_BIG)
a37b3cc0
AC
4284 floatformat_from_doublest (&floatformat_arm_ext_big, &d, dbl);
4285 else
4286 floatformat_from_doublest (&floatformat_arm_ext_littlebyte_bigword,
4287 &d, dbl);
c906108c 4288}
ed9a39eb 4289
c906108c 4290static int
ed9a39eb 4291condition_true (unsigned long cond, unsigned long status_reg)
c906108c
SS
4292{
4293 if (cond == INST_AL || cond == INST_NV)
4294 return 1;
4295
4296 switch (cond)
4297 {
4298 case INST_EQ:
4299 return ((status_reg & FLAG_Z) != 0);
4300 case INST_NE:
4301 return ((status_reg & FLAG_Z) == 0);
4302 case INST_CS:
4303 return ((status_reg & FLAG_C) != 0);
4304 case INST_CC:
4305 return ((status_reg & FLAG_C) == 0);
4306 case INST_MI:
4307 return ((status_reg & FLAG_N) != 0);
4308 case INST_PL:
4309 return ((status_reg & FLAG_N) == 0);
4310 case INST_VS:
4311 return ((status_reg & FLAG_V) != 0);
4312 case INST_VC:
4313 return ((status_reg & FLAG_V) == 0);
4314 case INST_HI:
4315 return ((status_reg & (FLAG_C | FLAG_Z)) == FLAG_C);
4316 case INST_LS:
4317 return ((status_reg & (FLAG_C | FLAG_Z)) != FLAG_C);
4318 case INST_GE:
4319 return (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0));
4320 case INST_LT:
4321 return (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0));
4322 case INST_GT:
f8bf5763
PM
4323 return (((status_reg & FLAG_Z) == 0)
4324 && (((status_reg & FLAG_N) == 0)
4325 == ((status_reg & FLAG_V) == 0)));
c906108c 4326 case INST_LE:
f8bf5763
PM
4327 return (((status_reg & FLAG_Z) != 0)
4328 || (((status_reg & FLAG_N) == 0)
4329 != ((status_reg & FLAG_V) == 0)));
c906108c
SS
4330 }
4331 return 1;
4332}
4333
c906108c 4334static unsigned long
0b1b3e42
UW
4335shifted_reg_val (struct frame_info *frame, unsigned long inst, int carry,
4336 unsigned long pc_val, unsigned long status_reg)
c906108c
SS
4337{
4338 unsigned long res, shift;
4339 int rm = bits (inst, 0, 3);
4340 unsigned long shifttype = bits (inst, 5, 6);
c5aa993b
JM
4341
4342 if (bit (inst, 4))
c906108c
SS
4343 {
4344 int rs = bits (inst, 8, 11);
0b1b3e42
UW
4345 shift = (rs == 15 ? pc_val + 8
4346 : get_frame_register_unsigned (frame, rs)) & 0xFF;
c906108c
SS
4347 }
4348 else
4349 shift = bits (inst, 7, 11);
c5aa993b 4350
bf9f652a 4351 res = (rm == ARM_PC_REGNUM
0d39a070 4352 ? (pc_val + (bit (inst, 4) ? 12 : 8))
0b1b3e42 4353 : get_frame_register_unsigned (frame, rm));
c906108c
SS
4354
4355 switch (shifttype)
4356 {
c5aa993b 4357 case 0: /* LSL */
c906108c
SS
4358 res = shift >= 32 ? 0 : res << shift;
4359 break;
c5aa993b
JM
4360
4361 case 1: /* LSR */
c906108c
SS
4362 res = shift >= 32 ? 0 : res >> shift;
4363 break;
4364
c5aa993b
JM
4365 case 2: /* ASR */
4366 if (shift >= 32)
4367 shift = 31;
c906108c
SS
4368 res = ((res & 0x80000000L)
4369 ? ~((~res) >> shift) : res >> shift);
4370 break;
4371
c5aa993b 4372 case 3: /* ROR/RRX */
c906108c
SS
4373 shift &= 31;
4374 if (shift == 0)
4375 res = (res >> 1) | (carry ? 0x80000000L : 0);
4376 else
c5aa993b 4377 res = (res >> shift) | (res << (32 - shift));
c906108c
SS
4378 break;
4379 }
4380
4381 return res & 0xffffffff;
4382}
4383
c906108c
SS
4384/* Return number of 1-bits in VAL. */
4385
4386static int
ed9a39eb 4387bitcount (unsigned long val)
c906108c
SS
4388{
4389 int nbits;
4390 for (nbits = 0; val != 0; nbits++)
0963b4bd 4391 val &= val - 1; /* Delete rightmost 1-bit in val. */
c906108c
SS
4392 return nbits;
4393}
4394
177321bd
DJ
4395/* Return the size in bytes of the complete Thumb instruction whose
4396 first halfword is INST1. */
4397
4398static int
4399thumb_insn_size (unsigned short inst1)
4400{
4401 if ((inst1 & 0xe000) == 0xe000 && (inst1 & 0x1800) != 0)
4402 return 4;
4403 else
4404 return 2;
4405}
4406
4407static int
4408thumb_advance_itstate (unsigned int itstate)
4409{
4410 /* Preserve IT[7:5], the first three bits of the condition. Shift
4411 the upcoming condition flags left by one bit. */
4412 itstate = (itstate & 0xe0) | ((itstate << 1) & 0x1f);
4413
4414 /* If we have finished the IT block, clear the state. */
4415 if ((itstate & 0x0f) == 0)
4416 itstate = 0;
4417
4418 return itstate;
4419}
4420
4421/* Find the next PC after the current instruction executes. In some
4422 cases we can not statically determine the answer (see the IT state
4423 handling in this function); in that case, a breakpoint may be
4424 inserted in addition to the returned PC, which will be used to set
4425 another breakpoint by our caller. */
4426
ad527d2e 4427static CORE_ADDR
18819fa6 4428thumb_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc)
c906108c 4429{
2af46ca0 4430 struct gdbarch *gdbarch = get_frame_arch (frame);
177321bd 4431 struct address_space *aspace = get_frame_address_space (frame);
e17a4113
UW
4432 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4433 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
c5aa993b 4434 unsigned long pc_val = ((unsigned long) pc) + 4; /* PC after prefetch */
e17a4113 4435 unsigned short inst1;
0963b4bd 4436 CORE_ADDR nextpc = pc + 2; /* Default is next instruction. */
c906108c 4437 unsigned long offset;
177321bd 4438 ULONGEST status, itstate;
c906108c 4439
50e98be4
DJ
4440 nextpc = MAKE_THUMB_ADDR (nextpc);
4441 pc_val = MAKE_THUMB_ADDR (pc_val);
4442
e17a4113 4443 inst1 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
9d4fde75 4444
9dca5578
DJ
4445 /* Thumb-2 conditional execution support. There are eight bits in
4446 the CPSR which describe conditional execution state. Once
4447 reconstructed (they're in a funny order), the low five bits
4448 describe the low bit of the condition for each instruction and
4449 how many instructions remain. The high three bits describe the
4450 base condition. One of the low four bits will be set if an IT
4451 block is active. These bits read as zero on earlier
4452 processors. */
4453 status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
177321bd 4454 itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
9dca5578 4455
177321bd
DJ
4456 /* If-Then handling. On GNU/Linux, where this routine is used, we
4457 use an undefined instruction as a breakpoint. Unlike BKPT, IT
4458 can disable execution of the undefined instruction. So we might
4459 miss the breakpoint if we set it on a skipped conditional
4460 instruction. Because conditional instructions can change the
4461 flags, affecting the execution of further instructions, we may
4462 need to set two breakpoints. */
9dca5578 4463
177321bd
DJ
4464 if (gdbarch_tdep (gdbarch)->thumb2_breakpoint != NULL)
4465 {
4466 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
4467 {
4468 /* An IT instruction. Because this instruction does not
4469 modify the flags, we can accurately predict the next
4470 executed instruction. */
4471 itstate = inst1 & 0x00ff;
4472 pc += thumb_insn_size (inst1);
4473
4474 while (itstate != 0 && ! condition_true (itstate >> 4, status))
4475 {
0963b4bd
MS
4476 inst1 = read_memory_unsigned_integer (pc, 2,
4477 byte_order_for_code);
177321bd
DJ
4478 pc += thumb_insn_size (inst1);
4479 itstate = thumb_advance_itstate (itstate);
4480 }
4481
50e98be4 4482 return MAKE_THUMB_ADDR (pc);
177321bd
DJ
4483 }
4484 else if (itstate != 0)
4485 {
4486 /* We are in a conditional block. Check the condition. */
4487 if (! condition_true (itstate >> 4, status))
4488 {
4489 /* Advance to the next executed instruction. */
4490 pc += thumb_insn_size (inst1);
4491 itstate = thumb_advance_itstate (itstate);
4492
4493 while (itstate != 0 && ! condition_true (itstate >> 4, status))
4494 {
0963b4bd
MS
4495 inst1 = read_memory_unsigned_integer (pc, 2,
4496 byte_order_for_code);
177321bd
DJ
4497 pc += thumb_insn_size (inst1);
4498 itstate = thumb_advance_itstate (itstate);
4499 }
4500
50e98be4 4501 return MAKE_THUMB_ADDR (pc);
177321bd
DJ
4502 }
4503 else if ((itstate & 0x0f) == 0x08)
4504 {
4505 /* This is the last instruction of the conditional
4506 block, and it is executed. We can handle it normally
4507 because the following instruction is not conditional,
4508 and we must handle it normally because it is
4509 permitted to branch. Fall through. */
4510 }
4511 else
4512 {
4513 int cond_negated;
4514
4515 /* There are conditional instructions after this one.
4516 If this instruction modifies the flags, then we can
4517 not predict what the next executed instruction will
4518 be. Fortunately, this instruction is architecturally
4519 forbidden to branch; we know it will fall through.
4520 Start by skipping past it. */
4521 pc += thumb_insn_size (inst1);
4522 itstate = thumb_advance_itstate (itstate);
4523
4524 /* Set a breakpoint on the following instruction. */
4525 gdb_assert ((itstate & 0x0f) != 0);
18819fa6
UW
4526 arm_insert_single_step_breakpoint (gdbarch, aspace,
4527 MAKE_THUMB_ADDR (pc));
177321bd
DJ
4528 cond_negated = (itstate >> 4) & 1;
4529
4530 /* Skip all following instructions with the same
4531 condition. If there is a later instruction in the IT
4532 block with the opposite condition, set the other
4533 breakpoint there. If not, then set a breakpoint on
4534 the instruction after the IT block. */
4535 do
4536 {
0963b4bd
MS
4537 inst1 = read_memory_unsigned_integer (pc, 2,
4538 byte_order_for_code);
177321bd
DJ
4539 pc += thumb_insn_size (inst1);
4540 itstate = thumb_advance_itstate (itstate);
4541 }
4542 while (itstate != 0 && ((itstate >> 4) & 1) == cond_negated);
4543
50e98be4 4544 return MAKE_THUMB_ADDR (pc);
177321bd
DJ
4545 }
4546 }
4547 }
4548 else if (itstate & 0x0f)
9dca5578
DJ
4549 {
4550 /* We are in a conditional block. Check the condition. */
177321bd 4551 int cond = itstate >> 4;
9dca5578
DJ
4552
4553 if (! condition_true (cond, status))
db24da6d
YQ
4554 /* Advance to the next instruction. All the 32-bit
4555 instructions share a common prefix. */
4556 return MAKE_THUMB_ADDR (pc + thumb_insn_size (inst1));
177321bd
DJ
4557
4558 /* Otherwise, handle the instruction normally. */
9dca5578
DJ
4559 }
4560
c906108c
SS
4561 if ((inst1 & 0xff00) == 0xbd00) /* pop {rlist, pc} */
4562 {
4563 CORE_ADDR sp;
4564
4565 /* Fetch the saved PC from the stack. It's stored above
4566 all of the other registers. */
f0c9063c 4567 offset = bitcount (bits (inst1, 0, 7)) * INT_REGISTER_SIZE;
0b1b3e42 4568 sp = get_frame_register_unsigned (frame, ARM_SP_REGNUM);
e17a4113 4569 nextpc = read_memory_unsigned_integer (sp + offset, 4, byte_order);
c906108c
SS
4570 }
4571 else if ((inst1 & 0xf000) == 0xd000) /* conditional branch */
4572 {
c5aa993b 4573 unsigned long cond = bits (inst1, 8, 11);
25b41d01
YQ
4574 if (cond == 0x0f) /* 0x0f = SWI */
4575 {
4576 struct gdbarch_tdep *tdep;
4577 tdep = gdbarch_tdep (gdbarch);
4578
4579 if (tdep->syscall_next_pc != NULL)
4580 nextpc = tdep->syscall_next_pc (frame);
4581
4582 }
4583 else if (cond != 0x0f && condition_true (cond, status))
c906108c
SS
4584 nextpc = pc_val + (sbits (inst1, 0, 7) << 1);
4585 }
4586 else if ((inst1 & 0xf800) == 0xe000) /* unconditional branch */
4587 {
4588 nextpc = pc_val + (sbits (inst1, 0, 10) << 1);
4589 }
db24da6d 4590 else if (thumb_insn_size (inst1) == 4) /* 32-bit instruction */
c906108c 4591 {
e17a4113
UW
4592 unsigned short inst2;
4593 inst2 = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);
9dca5578
DJ
4594
4595 /* Default to the next instruction. */
4596 nextpc = pc + 4;
50e98be4 4597 nextpc = MAKE_THUMB_ADDR (nextpc);
9dca5578
DJ
4598
4599 if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
4600 {
4601 /* Branches and miscellaneous control instructions. */
4602
4603 if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
4604 {
4605 /* B, BL, BLX. */
4606 int j1, j2, imm1, imm2;
4607
4608 imm1 = sbits (inst1, 0, 10);
4609 imm2 = bits (inst2, 0, 10);
4610 j1 = bit (inst2, 13);
4611 j2 = bit (inst2, 11);
4612
4613 offset = ((imm1 << 12) + (imm2 << 1));
4614 offset ^= ((!j2) << 22) | ((!j1) << 23);
4615
4616 nextpc = pc_val + offset;
4617 /* For BLX make sure to clear the low bits. */
4618 if (bit (inst2, 12) == 0)
4619 nextpc = nextpc & 0xfffffffc;
4620 }
4621 else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
4622 {
4623 /* SUBS PC, LR, #imm8. */
4624 nextpc = get_frame_register_unsigned (frame, ARM_LR_REGNUM);
4625 nextpc -= inst2 & 0x00ff;
4626 }
4069ebbe 4627 else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
9dca5578
DJ
4628 {
4629 /* Conditional branch. */
4630 if (condition_true (bits (inst1, 6, 9), status))
4631 {
4632 int sign, j1, j2, imm1, imm2;
4633
4634 sign = sbits (inst1, 10, 10);
4635 imm1 = bits (inst1, 0, 5);
4636 imm2 = bits (inst2, 0, 10);
4637 j1 = bit (inst2, 13);
4638 j2 = bit (inst2, 11);
4639
4640 offset = (sign << 20) + (j2 << 19) + (j1 << 18);
4641 offset += (imm1 << 12) + (imm2 << 1);
4642
4643 nextpc = pc_val + offset;
4644 }
4645 }
4646 }
4647 else if ((inst1 & 0xfe50) == 0xe810)
4648 {
4649 /* Load multiple or RFE. */
4650 int rn, offset, load_pc = 1;
4651
4652 rn = bits (inst1, 0, 3);
4653 if (bit (inst1, 7) && !bit (inst1, 8))
4654 {
4655 /* LDMIA or POP */
4656 if (!bit (inst2, 15))
4657 load_pc = 0;
4658 offset = bitcount (inst2) * 4 - 4;
4659 }
4660 else if (!bit (inst1, 7) && bit (inst1, 8))
4661 {
4662 /* LDMDB */
4663 if (!bit (inst2, 15))
4664 load_pc = 0;
4665 offset = -4;
4666 }
4667 else if (bit (inst1, 7) && bit (inst1, 8))
4668 {
4669 /* RFEIA */
4670 offset = 0;
4671 }
4672 else if (!bit (inst1, 7) && !bit (inst1, 8))
4673 {
4674 /* RFEDB */
4675 offset = -8;
4676 }
4677 else
4678 load_pc = 0;
4679
4680 if (load_pc)
4681 {
4682 CORE_ADDR addr = get_frame_register_unsigned (frame, rn);
4683 nextpc = get_frame_memory_unsigned (frame, addr + offset, 4);
4684 }
4685 }
4686 else if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
4687 {
4688 /* MOV PC or MOVS PC. */
4689 nextpc = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
50e98be4 4690 nextpc = MAKE_THUMB_ADDR (nextpc);
9dca5578
DJ
4691 }
4692 else if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
4693 {
4694 /* LDR PC. */
4695 CORE_ADDR base;
4696 int rn, load_pc = 1;
4697
4698 rn = bits (inst1, 0, 3);
4699 base = get_frame_register_unsigned (frame, rn);
bf9f652a 4700 if (rn == ARM_PC_REGNUM)
9dca5578
DJ
4701 {
4702 base = (base + 4) & ~(CORE_ADDR) 0x3;
4703 if (bit (inst1, 7))
4704 base += bits (inst2, 0, 11);
4705 else
4706 base -= bits (inst2, 0, 11);
4707 }
4708 else if (bit (inst1, 7))
4709 base += bits (inst2, 0, 11);
4710 else if (bit (inst2, 11))
4711 {
4712 if (bit (inst2, 10))
4713 {
4714 if (bit (inst2, 9))
4715 base += bits (inst2, 0, 7);
4716 else
4717 base -= bits (inst2, 0, 7);
4718 }
4719 }
4720 else if ((inst2 & 0x0fc0) == 0x0000)
4721 {
4722 int shift = bits (inst2, 4, 5), rm = bits (inst2, 0, 3);
4723 base += get_frame_register_unsigned (frame, rm) << shift;
4724 }
4725 else
4726 /* Reserved. */
4727 load_pc = 0;
4728
4729 if (load_pc)
4730 nextpc = get_frame_memory_unsigned (frame, base, 4);
4731 }
4732 else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
4733 {
4734 /* TBB. */
d476da0e
RE
4735 CORE_ADDR tbl_reg, table, offset, length;
4736
4737 tbl_reg = bits (inst1, 0, 3);
4738 if (tbl_reg == 0x0f)
4739 table = pc + 4; /* Regcache copy of PC isn't right yet. */
4740 else
4741 table = get_frame_register_unsigned (frame, tbl_reg);
9dca5578 4742
9dca5578
DJ
4743 offset = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
4744 length = 2 * get_frame_memory_unsigned (frame, table + offset, 1);
4745 nextpc = pc_val + length;
4746 }
d476da0e 4747 else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
9dca5578
DJ
4748 {
4749 /* TBH. */
d476da0e
RE
4750 CORE_ADDR tbl_reg, table, offset, length;
4751
4752 tbl_reg = bits (inst1, 0, 3);
4753 if (tbl_reg == 0x0f)
4754 table = pc + 4; /* Regcache copy of PC isn't right yet. */
4755 else
4756 table = get_frame_register_unsigned (frame, tbl_reg);
9dca5578 4757
9dca5578
DJ
4758 offset = 2 * get_frame_register_unsigned (frame, bits (inst2, 0, 3));
4759 length = 2 * get_frame_memory_unsigned (frame, table + offset, 2);
4760 nextpc = pc_val + length;
4761 }
c906108c 4762 }
aa17d93e 4763 else if ((inst1 & 0xff00) == 0x4700) /* bx REG, blx REG */
9498281f
DJ
4764 {
4765 if (bits (inst1, 3, 6) == 0x0f)
6ca1b147 4766 nextpc = UNMAKE_THUMB_ADDR (pc_val);
9498281f 4767 else
0b1b3e42 4768 nextpc = get_frame_register_unsigned (frame, bits (inst1, 3, 6));
9498281f 4769 }
ad8b5167
UW
4770 else if ((inst1 & 0xff87) == 0x4687) /* mov pc, REG */
4771 {
4772 if (bits (inst1, 3, 6) == 0x0f)
4773 nextpc = pc_val;
4774 else
4775 nextpc = get_frame_register_unsigned (frame, bits (inst1, 3, 6));
4776
4777 nextpc = MAKE_THUMB_ADDR (nextpc);
4778 }
9dca5578
DJ
4779 else if ((inst1 & 0xf500) == 0xb100)
4780 {
4781 /* CBNZ or CBZ. */
4782 int imm = (bit (inst1, 9) << 6) + (bits (inst1, 3, 7) << 1);
4783 ULONGEST reg = get_frame_register_unsigned (frame, bits (inst1, 0, 2));
4784
4785 if (bit (inst1, 11) && reg != 0)
4786 nextpc = pc_val + imm;
4787 else if (!bit (inst1, 11) && reg == 0)
4788 nextpc = pc_val + imm;
4789 }
c906108c
SS
4790 return nextpc;
4791}
4792
50e98be4 4793/* Get the raw next address. PC is the current program counter, in
18819fa6 4794 FRAME, which is assumed to be executing in ARM mode.
50e98be4
DJ
4795
4796 The value returned has the execution state of the next instruction
4797 encoded in it. Use IS_THUMB_ADDR () to see whether the instruction is
4798 in Thumb-State, and gdbarch_addr_bits_remove () to get the plain memory
0963b4bd
MS
4799 address. */
4800
50e98be4 4801static CORE_ADDR
18819fa6 4802arm_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc)
c906108c 4803{
2af46ca0 4804 struct gdbarch *gdbarch = get_frame_arch (frame);
e17a4113
UW
4805 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4806 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
c906108c
SS
4807 unsigned long pc_val;
4808 unsigned long this_instr;
4809 unsigned long status;
4810 CORE_ADDR nextpc;
4811
c906108c 4812 pc_val = (unsigned long) pc;
e17a4113 4813 this_instr = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
9d4fde75 4814
0b1b3e42 4815 status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
c5aa993b 4816 nextpc = (CORE_ADDR) (pc_val + 4); /* Default case */
c906108c 4817
daddc3c1
DJ
4818 if (bits (this_instr, 28, 31) == INST_NV)
4819 switch (bits (this_instr, 24, 27))
4820 {
4821 case 0xa:
4822 case 0xb:
4823 {
4824 /* Branch with Link and change to Thumb. */
4825 nextpc = BranchDest (pc, this_instr);
4826 nextpc |= bit (this_instr, 24) << 1;
50e98be4 4827 nextpc = MAKE_THUMB_ADDR (nextpc);
daddc3c1
DJ
4828 break;
4829 }
4830 case 0xc:
4831 case 0xd:
4832 case 0xe:
4833 /* Coprocessor register transfer. */
4834 if (bits (this_instr, 12, 15) == 15)
4835 error (_("Invalid update to pc in instruction"));
4836 break;
4837 }
4838 else if (condition_true (bits (this_instr, 28, 31), status))
c906108c
SS
4839 {
4840 switch (bits (this_instr, 24, 27))
4841 {
c5aa993b 4842 case 0x0:
94c30b78 4843 case 0x1: /* data processing */
c5aa993b
JM
4844 case 0x2:
4845 case 0x3:
c906108c
SS
4846 {
4847 unsigned long operand1, operand2, result = 0;
4848 unsigned long rn;
4849 int c;
c5aa993b 4850
c906108c
SS
4851 if (bits (this_instr, 12, 15) != 15)
4852 break;
4853
4854 if (bits (this_instr, 22, 25) == 0
c5aa993b 4855 && bits (this_instr, 4, 7) == 9) /* multiply */
edefbb7c 4856 error (_("Invalid update to pc in instruction"));
c906108c 4857
9498281f 4858 /* BX <reg>, BLX <reg> */
e150acc7
PB
4859 if (bits (this_instr, 4, 27) == 0x12fff1
4860 || bits (this_instr, 4, 27) == 0x12fff3)
9498281f
DJ
4861 {
4862 rn = bits (this_instr, 0, 3);
bf9f652a
YQ
4863 nextpc = ((rn == ARM_PC_REGNUM)
4864 ? (pc_val + 8)
4865 : get_frame_register_unsigned (frame, rn));
4866
9498281f
DJ
4867 return nextpc;
4868 }
4869
0963b4bd 4870 /* Multiply into PC. */
c906108c
SS
4871 c = (status & FLAG_C) ? 1 : 0;
4872 rn = bits (this_instr, 16, 19);
bf9f652a
YQ
4873 operand1 = ((rn == ARM_PC_REGNUM)
4874 ? (pc_val + 8)
4875 : get_frame_register_unsigned (frame, rn));
c5aa993b 4876
c906108c
SS
4877 if (bit (this_instr, 25))
4878 {
4879 unsigned long immval = bits (this_instr, 0, 7);
4880 unsigned long rotate = 2 * bits (this_instr, 8, 11);
c5aa993b
JM
4881 operand2 = ((immval >> rotate) | (immval << (32 - rotate)))
4882 & 0xffffffff;
c906108c 4883 }
0963b4bd
MS
4884 else /* operand 2 is a shifted register. */
4885 operand2 = shifted_reg_val (frame, this_instr, c,
4886 pc_val, status);
c5aa993b 4887
c906108c
SS
4888 switch (bits (this_instr, 21, 24))
4889 {
c5aa993b 4890 case 0x0: /*and */
c906108c
SS
4891 result = operand1 & operand2;
4892 break;
4893
c5aa993b 4894 case 0x1: /*eor */
c906108c
SS
4895 result = operand1 ^ operand2;
4896 break;
4897
c5aa993b 4898 case 0x2: /*sub */
c906108c
SS
4899 result = operand1 - operand2;
4900 break;
4901
c5aa993b 4902 case 0x3: /*rsb */
c906108c
SS
4903 result = operand2 - operand1;
4904 break;
4905
c5aa993b 4906 case 0x4: /*add */
c906108c
SS
4907 result = operand1 + operand2;
4908 break;
4909
c5aa993b 4910 case 0x5: /*adc */
c906108c
SS
4911 result = operand1 + operand2 + c;
4912 break;
4913
c5aa993b 4914 case 0x6: /*sbc */
c906108c
SS
4915 result = operand1 - operand2 + c;
4916 break;
4917
c5aa993b 4918 case 0x7: /*rsc */
c906108c
SS
4919 result = operand2 - operand1 + c;
4920 break;
4921
c5aa993b
JM
4922 case 0x8:
4923 case 0x9:
4924 case 0xa:
4925 case 0xb: /* tst, teq, cmp, cmn */
c906108c
SS
4926 result = (unsigned long) nextpc;
4927 break;
4928
c5aa993b 4929 case 0xc: /*orr */
c906108c
SS
4930 result = operand1 | operand2;
4931 break;
4932
c5aa993b 4933 case 0xd: /*mov */
c906108c
SS
4934 /* Always step into a function. */
4935 result = operand2;
c5aa993b 4936 break;
c906108c 4937
c5aa993b 4938 case 0xe: /*bic */
c906108c
SS
4939 result = operand1 & ~operand2;
4940 break;
4941
c5aa993b 4942 case 0xf: /*mvn */
c906108c
SS
4943 result = ~operand2;
4944 break;
4945 }
c906108c 4946
50e98be4
DJ
4947 /* In 26-bit APCS the bottom two bits of the result are
4948 ignored, and we always end up in ARM state. */
4949 if (!arm_apcs_32)
4950 nextpc = arm_addr_bits_remove (gdbarch, result);
4951 else
4952 nextpc = result;
4953
c906108c
SS
4954 break;
4955 }
c5aa993b
JM
4956
4957 case 0x4:
4958 case 0x5: /* data transfer */
4959 case 0x6:
4960 case 0x7:
c906108c
SS
4961 if (bit (this_instr, 20))
4962 {
4963 /* load */
4964 if (bits (this_instr, 12, 15) == 15)
4965 {
4966 /* rd == pc */
c5aa993b 4967 unsigned long rn;
c906108c 4968 unsigned long base;
c5aa993b 4969
c906108c 4970 if (bit (this_instr, 22))
edefbb7c 4971 error (_("Invalid update to pc in instruction"));
c906108c
SS
4972
4973 /* byte write to PC */
4974 rn = bits (this_instr, 16, 19);
bf9f652a
YQ
4975 base = ((rn == ARM_PC_REGNUM)
4976 ? (pc_val + 8)
4977 : get_frame_register_unsigned (frame, rn));
4978
c906108c
SS
4979 if (bit (this_instr, 24))
4980 {
4981 /* pre-indexed */
4982 int c = (status & FLAG_C) ? 1 : 0;
4983 unsigned long offset =
c5aa993b 4984 (bit (this_instr, 25)
0b1b3e42 4985 ? shifted_reg_val (frame, this_instr, c, pc_val, status)
c5aa993b 4986 : bits (this_instr, 0, 11));
c906108c
SS
4987
4988 if (bit (this_instr, 23))
4989 base += offset;
4990 else
4991 base -= offset;
4992 }
51370a33
YQ
4993 nextpc =
4994 (CORE_ADDR) read_memory_unsigned_integer ((CORE_ADDR) base,
4995 4, byte_order);
c906108c
SS
4996 }
4997 }
4998 break;
c5aa993b
JM
4999
5000 case 0x8:
5001 case 0x9: /* block transfer */
c906108c
SS
5002 if (bit (this_instr, 20))
5003 {
5004 /* LDM */
5005 if (bit (this_instr, 15))
5006 {
5007 /* loading pc */
5008 int offset = 0;
51370a33
YQ
5009 unsigned long rn_val
5010 = get_frame_register_unsigned (frame,
5011 bits (this_instr, 16, 19));
c906108c
SS
5012
5013 if (bit (this_instr, 23))
5014 {
5015 /* up */
5016 unsigned long reglist = bits (this_instr, 0, 14);
5017 offset = bitcount (reglist) * 4;
c5aa993b 5018 if (bit (this_instr, 24)) /* pre */
c906108c
SS
5019 offset += 4;
5020 }
5021 else if (bit (this_instr, 24))
5022 offset = -4;
c5aa993b 5023
51370a33
YQ
5024 nextpc =
5025 (CORE_ADDR) read_memory_unsigned_integer ((CORE_ADDR)
5026 (rn_val + offset),
5027 4, byte_order);
c906108c
SS
5028 }
5029 }
5030 break;
c5aa993b
JM
5031
5032 case 0xb: /* branch & link */
5033 case 0xa: /* branch */
c906108c
SS
5034 {
5035 nextpc = BranchDest (pc, this_instr);
c906108c
SS
5036 break;
5037 }
c5aa993b
JM
5038
5039 case 0xc:
5040 case 0xd:
5041 case 0xe: /* coproc ops */
25b41d01 5042 break;
c5aa993b 5043 case 0xf: /* SWI */
25b41d01
YQ
5044 {
5045 struct gdbarch_tdep *tdep;
5046 tdep = gdbarch_tdep (gdbarch);
5047
5048 if (tdep->syscall_next_pc != NULL)
5049 nextpc = tdep->syscall_next_pc (frame);
5050
5051 }
c906108c
SS
5052 break;
5053
5054 default:
edefbb7c 5055 fprintf_filtered (gdb_stderr, _("Bad bit-field extraction\n"));
c906108c
SS
5056 return (pc);
5057 }
5058 }
5059
5060 return nextpc;
5061}
5062
18819fa6
UW
5063/* Determine next PC after current instruction executes. Will call either
5064 arm_get_next_pc_raw or thumb_get_next_pc_raw. Error out if infinite
5065 loop is detected. */
5066
50e98be4
DJ
5067CORE_ADDR
5068arm_get_next_pc (struct frame_info *frame, CORE_ADDR pc)
5069{
18819fa6
UW
5070 CORE_ADDR nextpc;
5071
5072 if (arm_frame_is_thumb (frame))
2b59118e 5073 nextpc = thumb_get_next_pc_raw (frame, pc);
18819fa6 5074 else
2b59118e 5075 nextpc = arm_get_next_pc_raw (frame, pc);
18819fa6 5076
50e98be4
DJ
5077 return nextpc;
5078}
5079
18819fa6
UW
5080/* Like insert_single_step_breakpoint, but make sure we use a breakpoint
5081 of the appropriate mode (as encoded in the PC value), even if this
5082 differs from what would be expected according to the symbol tables. */
5083
5084void
5085arm_insert_single_step_breakpoint (struct gdbarch *gdbarch,
5086 struct address_space *aspace,
5087 CORE_ADDR pc)
5088{
5089 struct cleanup *old_chain
5090 = make_cleanup_restore_integer (&arm_override_mode);
5091
5092 arm_override_mode = IS_THUMB_ADDR (pc);
5093 pc = gdbarch_addr_bits_remove (gdbarch, pc);
5094
5095 insert_single_step_breakpoint (gdbarch, aspace, pc);
5096
5097 do_cleanups (old_chain);
5098}
5099
35f73cfc
UW
5100/* Checks for an atomic sequence of instructions beginning with a LDREX{,B,H,D}
5101 instruction and ending with a STREX{,B,H,D} instruction. If such a sequence
5102 is found, attempt to step through it. A breakpoint is placed at the end of
5103 the sequence. */
5104
5105static int
5106thumb_deal_with_atomic_sequence_raw (struct frame_info *frame)
5107{
5108 struct gdbarch *gdbarch = get_frame_arch (frame);
5109 struct address_space *aspace = get_frame_address_space (frame);
5110 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
5111 CORE_ADDR pc = get_frame_pc (frame);
5112 CORE_ADDR breaks[2] = {-1, -1};
5113 CORE_ADDR loc = pc;
5114 unsigned short insn1, insn2;
5115 int insn_count;
5116 int index;
5117 int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed). */
5118 const int atomic_sequence_length = 16; /* Instruction sequence length. */
5119 ULONGEST status, itstate;
5120
5121 /* We currently do not support atomic sequences within an IT block. */
5122 status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
5123 itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
5124 if (itstate & 0x0f)
5125 return 0;
5126
5127 /* Assume all atomic sequences start with a ldrex{,b,h,d} instruction. */
5128 insn1 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
5129 loc += 2;
5130 if (thumb_insn_size (insn1) != 4)
5131 return 0;
5132
5133 insn2 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
5134 loc += 2;
5135 if (!((insn1 & 0xfff0) == 0xe850
5136 || ((insn1 & 0xfff0) == 0xe8d0 && (insn2 & 0x00c0) == 0x0040)))
5137 return 0;
5138
5139 /* Assume that no atomic sequence is longer than "atomic_sequence_length"
5140 instructions. */
5141 for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
5142 {
5143 insn1 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
5144 loc += 2;
5145
5146 if (thumb_insn_size (insn1) != 4)
5147 {
5148 /* Assume that there is at most one conditional branch in the
5149 atomic sequence. If a conditional branch is found, put a
5150 breakpoint in its destination address. */
5151 if ((insn1 & 0xf000) == 0xd000 && bits (insn1, 8, 11) != 0x0f)
5152 {
5153 if (last_breakpoint > 0)
5154 return 0; /* More than one conditional branch found,
5155 fallback to the standard code. */
5156
5157 breaks[1] = loc + 2 + (sbits (insn1, 0, 7) << 1);
5158 last_breakpoint++;
5159 }
5160
5161 /* We do not support atomic sequences that use any *other*
5162 instructions but conditional branches to change the PC.
5163 Fall back to standard code to avoid losing control of
5164 execution. */
5165 else if (thumb_instruction_changes_pc (insn1))
5166 return 0;
5167 }
5168 else
5169 {
5170 insn2 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
5171 loc += 2;
5172
5173 /* Assume that there is at most one conditional branch in the
5174 atomic sequence. If a conditional branch is found, put a
5175 breakpoint in its destination address. */
5176 if ((insn1 & 0xf800) == 0xf000
5177 && (insn2 & 0xd000) == 0x8000
5178 && (insn1 & 0x0380) != 0x0380)
5179 {
5180 int sign, j1, j2, imm1, imm2;
5181 unsigned int offset;
5182
5183 sign = sbits (insn1, 10, 10);
5184 imm1 = bits (insn1, 0, 5);
5185 imm2 = bits (insn2, 0, 10);
5186 j1 = bit (insn2, 13);
5187 j2 = bit (insn2, 11);
5188
5189 offset = (sign << 20) + (j2 << 19) + (j1 << 18);
5190 offset += (imm1 << 12) + (imm2 << 1);
5191
5192 if (last_breakpoint > 0)
5193 return 0; /* More than one conditional branch found,
5194 fallback to the standard code. */
5195
5196 breaks[1] = loc + offset;
5197 last_breakpoint++;
5198 }
5199
5200 /* We do not support atomic sequences that use any *other*
5201 instructions but conditional branches to change the PC.
5202 Fall back to standard code to avoid losing control of
5203 execution. */
5204 else if (thumb2_instruction_changes_pc (insn1, insn2))
5205 return 0;
5206
5207 /* If we find a strex{,b,h,d}, we're done. */
5208 if ((insn1 & 0xfff0) == 0xe840
5209 || ((insn1 & 0xfff0) == 0xe8c0 && (insn2 & 0x00c0) == 0x0040))
5210 break;
5211 }
5212 }
5213
5214 /* If we didn't find the strex{,b,h,d}, we cannot handle the sequence. */
5215 if (insn_count == atomic_sequence_length)
5216 return 0;
5217
5218 /* Insert a breakpoint right after the end of the atomic sequence. */
5219 breaks[0] = loc;
5220
5221 /* Check for duplicated breakpoints. Check also for a breakpoint
5222 placed (branch instruction's destination) anywhere in sequence. */
5223 if (last_breakpoint
5224 && (breaks[1] == breaks[0]
5225 || (breaks[1] >= pc && breaks[1] < loc)))
5226 last_breakpoint = 0;
5227
5228 /* Effectively inserts the breakpoints. */
5229 for (index = 0; index <= last_breakpoint; index++)
5230 arm_insert_single_step_breakpoint (gdbarch, aspace,
5231 MAKE_THUMB_ADDR (breaks[index]));
5232
5233 return 1;
5234}
5235
5236static int
5237arm_deal_with_atomic_sequence_raw (struct frame_info *frame)
5238{
5239 struct gdbarch *gdbarch = get_frame_arch (frame);
5240 struct address_space *aspace = get_frame_address_space (frame);
5241 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
5242 CORE_ADDR pc = get_frame_pc (frame);
5243 CORE_ADDR breaks[2] = {-1, -1};
5244 CORE_ADDR loc = pc;
5245 unsigned int insn;
5246 int insn_count;
5247 int index;
5248 int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed). */
5249 const int atomic_sequence_length = 16; /* Instruction sequence length. */
5250
5251 /* Assume all atomic sequences start with a ldrex{,b,h,d} instruction.
5252 Note that we do not currently support conditionally executed atomic
5253 instructions. */
5254 insn = read_memory_unsigned_integer (loc, 4, byte_order_for_code);
5255 loc += 4;
5256 if ((insn & 0xff9000f0) != 0xe1900090)
5257 return 0;
5258
5259 /* Assume that no atomic sequence is longer than "atomic_sequence_length"
5260 instructions. */
5261 for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
5262 {
5263 insn = read_memory_unsigned_integer (loc, 4, byte_order_for_code);
5264 loc += 4;
5265
5266 /* Assume that there is at most one conditional branch in the atomic
5267 sequence. If a conditional branch is found, put a breakpoint in
5268 its destination address. */
5269 if (bits (insn, 24, 27) == 0xa)
5270 {
5271 if (last_breakpoint > 0)
5272 return 0; /* More than one conditional branch found, fallback
5273 to the standard single-step code. */
5274
5275 breaks[1] = BranchDest (loc - 4, insn);
5276 last_breakpoint++;
5277 }
5278
5279 /* We do not support atomic sequences that use any *other* instructions
5280 but conditional branches to change the PC. Fall back to standard
5281 code to avoid losing control of execution. */
5282 else if (arm_instruction_changes_pc (insn))
5283 return 0;
5284
5285 /* If we find a strex{,b,h,d}, we're done. */
5286 if ((insn & 0xff9000f0) == 0xe1800090)
5287 break;
5288 }
5289
5290 /* If we didn't find the strex{,b,h,d}, we cannot handle the sequence. */
5291 if (insn_count == atomic_sequence_length)
5292 return 0;
5293
5294 /* Insert a breakpoint right after the end of the atomic sequence. */
5295 breaks[0] = loc;
5296
5297 /* Check for duplicated breakpoints. Check also for a breakpoint
5298 placed (branch instruction's destination) anywhere in sequence. */
5299 if (last_breakpoint
5300 && (breaks[1] == breaks[0]
5301 || (breaks[1] >= pc && breaks[1] < loc)))
5302 last_breakpoint = 0;
5303
5304 /* Effectively inserts the breakpoints. */
5305 for (index = 0; index <= last_breakpoint; index++)
5306 arm_insert_single_step_breakpoint (gdbarch, aspace, breaks[index]);
5307
5308 return 1;
5309}
5310
5311int
5312arm_deal_with_atomic_sequence (struct frame_info *frame)
5313{
5314 if (arm_frame_is_thumb (frame))
5315 return thumb_deal_with_atomic_sequence_raw (frame);
5316 else
5317 return arm_deal_with_atomic_sequence_raw (frame);
5318}
5319
9512d7fd
FN
5320/* single_step() is called just before we want to resume the inferior,
5321 if we want to single-step it but there is no hardware or kernel
5322 single-step support. We find the target of the coming instruction
e0cd558a 5323 and breakpoint it. */
9512d7fd 5324
190dce09 5325int
0b1b3e42 5326arm_software_single_step (struct frame_info *frame)
9512d7fd 5327{
a6d9a66e 5328 struct gdbarch *gdbarch = get_frame_arch (frame);
6c95b8df 5329 struct address_space *aspace = get_frame_address_space (frame);
35f73cfc
UW
5330 CORE_ADDR next_pc;
5331
5332 if (arm_deal_with_atomic_sequence (frame))
5333 return 1;
18819fa6 5334
35f73cfc 5335 next_pc = arm_get_next_pc (frame, get_frame_pc (frame));
18819fa6 5336 arm_insert_single_step_breakpoint (gdbarch, aspace, next_pc);
e6590a1b
UW
5337
5338 return 1;
9512d7fd 5339}
9512d7fd 5340
f9d67f43
DJ
5341/* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
5342 the buffer to be NEW_LEN bytes ending at ENDADDR. Return
5343 NULL if an error occurs. BUF is freed. */
5344
5345static gdb_byte *
5346extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
5347 int old_len, int new_len)
5348{
22e048c9 5349 gdb_byte *new_buf;
f9d67f43
DJ
5350 int bytes_to_read = new_len - old_len;
5351
5352 new_buf = xmalloc (new_len);
5353 memcpy (new_buf + bytes_to_read, buf, old_len);
5354 xfree (buf);
5355 if (target_read_memory (endaddr - new_len, new_buf, bytes_to_read) != 0)
5356 {
5357 xfree (new_buf);
5358 return NULL;
5359 }
5360 return new_buf;
5361}
5362
5363/* An IT block is at most the 2-byte IT instruction followed by
5364 four 4-byte instructions. The furthest back we must search to
5365 find an IT block that affects the current instruction is thus
5366 2 + 3 * 4 == 14 bytes. */
5367#define MAX_IT_BLOCK_PREFIX 14
5368
5369/* Use a quick scan if there are more than this many bytes of
5370 code. */
5371#define IT_SCAN_THRESHOLD 32
5372
5373/* Adjust a breakpoint's address to move breakpoints out of IT blocks.
5374 A breakpoint in an IT block may not be hit, depending on the
5375 condition flags. */
5376static CORE_ADDR
5377arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
5378{
5379 gdb_byte *buf;
5380 char map_type;
5381 CORE_ADDR boundary, func_start;
22e048c9 5382 int buf_len;
f9d67f43
DJ
5383 enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
5384 int i, any, last_it, last_it_count;
5385
5386 /* If we are using BKPT breakpoints, none of this is necessary. */
5387 if (gdbarch_tdep (gdbarch)->thumb2_breakpoint == NULL)
5388 return bpaddr;
5389
5390 /* ARM mode does not have this problem. */
9779414d 5391 if (!arm_pc_is_thumb (gdbarch, bpaddr))
f9d67f43
DJ
5392 return bpaddr;
5393
5394 /* We are setting a breakpoint in Thumb code that could potentially
5395 contain an IT block. The first step is to find how much Thumb
5396 code there is; we do not need to read outside of known Thumb
5397 sequences. */
5398 map_type = arm_find_mapping_symbol (bpaddr, &boundary);
5399 if (map_type == 0)
5400 /* Thumb-2 code must have mapping symbols to have a chance. */
5401 return bpaddr;
5402
5403 bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
5404
5405 if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
5406 && func_start > boundary)
5407 boundary = func_start;
5408
5409 /* Search for a candidate IT instruction. We have to do some fancy
5410 footwork to distinguish a real IT instruction from the second
5411 half of a 32-bit instruction, but there is no need for that if
5412 there's no candidate. */
5413 buf_len = min (bpaddr - boundary, MAX_IT_BLOCK_PREFIX);
5414 if (buf_len == 0)
5415 /* No room for an IT instruction. */
5416 return bpaddr;
5417
5418 buf = xmalloc (buf_len);
5419 if (target_read_memory (bpaddr - buf_len, buf, buf_len) != 0)
5420 return bpaddr;
5421 any = 0;
5422 for (i = 0; i < buf_len; i += 2)
5423 {
5424 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5425 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5426 {
5427 any = 1;
5428 break;
5429 }
5430 }
5431 if (any == 0)
5432 {
5433 xfree (buf);
5434 return bpaddr;
5435 }
5436
5437 /* OK, the code bytes before this instruction contain at least one
5438 halfword which resembles an IT instruction. We know that it's
5439 Thumb code, but there are still two possibilities. Either the
5440 halfword really is an IT instruction, or it is the second half of
5441 a 32-bit Thumb instruction. The only way we can tell is to
5442 scan forwards from a known instruction boundary. */
5443 if (bpaddr - boundary > IT_SCAN_THRESHOLD)
5444 {
5445 int definite;
5446
5447 /* There's a lot of code before this instruction. Start with an
5448 optimistic search; it's easy to recognize halfwords that can
5449 not be the start of a 32-bit instruction, and use that to
5450 lock on to the instruction boundaries. */
5451 buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
5452 if (buf == NULL)
5453 return bpaddr;
5454 buf_len = IT_SCAN_THRESHOLD;
5455
5456 definite = 0;
5457 for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
5458 {
5459 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5460 if (thumb_insn_size (inst1) == 2)
5461 {
5462 definite = 1;
5463 break;
5464 }
5465 }
5466
5467 /* At this point, if DEFINITE, BUF[I] is the first place we
5468 are sure that we know the instruction boundaries, and it is far
5469 enough from BPADDR that we could not miss an IT instruction
5470 affecting BPADDR. If ! DEFINITE, give up - start from a
5471 known boundary. */
5472 if (! definite)
5473 {
0963b4bd
MS
5474 buf = extend_buffer_earlier (buf, bpaddr, buf_len,
5475 bpaddr - boundary);
f9d67f43
DJ
5476 if (buf == NULL)
5477 return bpaddr;
5478 buf_len = bpaddr - boundary;
5479 i = 0;
5480 }
5481 }
5482 else
5483 {
5484 buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
5485 if (buf == NULL)
5486 return bpaddr;
5487 buf_len = bpaddr - boundary;
5488 i = 0;
5489 }
5490
5491 /* Scan forwards. Find the last IT instruction before BPADDR. */
5492 last_it = -1;
5493 last_it_count = 0;
5494 while (i < buf_len)
5495 {
5496 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5497 last_it_count--;
5498 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5499 {
5500 last_it = i;
5501 if (inst1 & 0x0001)
5502 last_it_count = 4;
5503 else if (inst1 & 0x0002)
5504 last_it_count = 3;
5505 else if (inst1 & 0x0004)
5506 last_it_count = 2;
5507 else
5508 last_it_count = 1;
5509 }
5510 i += thumb_insn_size (inst1);
5511 }
5512
5513 xfree (buf);
5514
5515 if (last_it == -1)
5516 /* There wasn't really an IT instruction after all. */
5517 return bpaddr;
5518
5519 if (last_it_count < 1)
5520 /* It was too far away. */
5521 return bpaddr;
5522
5523 /* This really is a trouble spot. Move the breakpoint to the IT
5524 instruction. */
5525 return bpaddr - buf_len + last_it;
5526}
5527
cca44b1b 5528/* ARM displaced stepping support.
c906108c 5529
cca44b1b 5530 Generally ARM displaced stepping works as follows:
c906108c 5531
cca44b1b
JB
5532 1. When an instruction is to be single-stepped, it is first decoded by
5533 arm_process_displaced_insn (called from arm_displaced_step_copy_insn).
5534 Depending on the type of instruction, it is then copied to a scratch
5535 location, possibly in a modified form. The copy_* set of functions
0963b4bd 5536 performs such modification, as necessary. A breakpoint is placed after
cca44b1b
JB
5537 the modified instruction in the scratch space to return control to GDB.
5538 Note in particular that instructions which modify the PC will no longer
5539 do so after modification.
c5aa993b 5540
cca44b1b
JB
5541 2. The instruction is single-stepped, by setting the PC to the scratch
5542 location address, and resuming. Control returns to GDB when the
5543 breakpoint is hit.
c5aa993b 5544
cca44b1b
JB
5545 3. A cleanup function (cleanup_*) is called corresponding to the copy_*
5546 function used for the current instruction. This function's job is to
5547 put the CPU/memory state back to what it would have been if the
5548 instruction had been executed unmodified in its original location. */
c5aa993b 5549
cca44b1b
JB
5550/* NOP instruction (mov r0, r0). */
5551#define ARM_NOP 0xe1a00000
34518530 5552#define THUMB_NOP 0x4600
cca44b1b
JB
5553
5554/* Helper for register reads for displaced stepping. In particular, this
5555 returns the PC as it would be seen by the instruction at its original
5556 location. */
5557
5558ULONGEST
36073a92
YQ
5559displaced_read_reg (struct regcache *regs, struct displaced_step_closure *dsc,
5560 int regno)
cca44b1b
JB
5561{
5562 ULONGEST ret;
36073a92 5563 CORE_ADDR from = dsc->insn_addr;
cca44b1b 5564
bf9f652a 5565 if (regno == ARM_PC_REGNUM)
cca44b1b 5566 {
4db71c0b
YQ
5567 /* Compute pipeline offset:
5568 - When executing an ARM instruction, PC reads as the address of the
5569 current instruction plus 8.
5570 - When executing a Thumb instruction, PC reads as the address of the
5571 current instruction plus 4. */
5572
36073a92 5573 if (!dsc->is_thumb)
4db71c0b
YQ
5574 from += 8;
5575 else
5576 from += 4;
5577
cca44b1b
JB
5578 if (debug_displaced)
5579 fprintf_unfiltered (gdb_stdlog, "displaced: read pc value %.8lx\n",
4db71c0b
YQ
5580 (unsigned long) from);
5581 return (ULONGEST) from;
cca44b1b 5582 }
c906108c 5583 else
cca44b1b
JB
5584 {
5585 regcache_cooked_read_unsigned (regs, regno, &ret);
5586 if (debug_displaced)
5587 fprintf_unfiltered (gdb_stdlog, "displaced: read r%d value %.8lx\n",
5588 regno, (unsigned long) ret);
5589 return ret;
5590 }
c906108c
SS
5591}
5592
cca44b1b
JB
5593static int
5594displaced_in_arm_mode (struct regcache *regs)
5595{
5596 ULONGEST ps;
9779414d 5597 ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
66e810cd 5598
cca44b1b 5599 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
66e810cd 5600
9779414d 5601 return (ps & t_bit) == 0;
cca44b1b 5602}
66e810cd 5603
cca44b1b 5604/* Write to the PC as from a branch instruction. */
c906108c 5605
cca44b1b 5606static void
36073a92
YQ
5607branch_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5608 ULONGEST val)
c906108c 5609{
36073a92 5610 if (!dsc->is_thumb)
cca44b1b
JB
5611 /* Note: If bits 0/1 are set, this branch would be unpredictable for
5612 architecture versions < 6. */
0963b4bd
MS
5613 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5614 val & ~(ULONGEST) 0x3);
cca44b1b 5615 else
0963b4bd
MS
5616 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5617 val & ~(ULONGEST) 0x1);
cca44b1b 5618}
66e810cd 5619
cca44b1b
JB
5620/* Write to the PC as from a branch-exchange instruction. */
5621
5622static void
5623bx_write_pc (struct regcache *regs, ULONGEST val)
5624{
5625 ULONGEST ps;
9779414d 5626 ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
cca44b1b
JB
5627
5628 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
5629
5630 if ((val & 1) == 1)
c906108c 5631 {
9779414d 5632 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
cca44b1b
JB
5633 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
5634 }
5635 else if ((val & 2) == 0)
5636 {
9779414d 5637 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 5638 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
c906108c
SS
5639 }
5640 else
5641 {
cca44b1b
JB
5642 /* Unpredictable behaviour. Try to do something sensible (switch to ARM
5643 mode, align dest to 4 bytes). */
5644 warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
9779414d 5645 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 5646 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
c906108c
SS
5647 }
5648}
ed9a39eb 5649
cca44b1b 5650/* Write to the PC as if from a load instruction. */
ed9a39eb 5651
34e8f22d 5652static void
36073a92
YQ
5653load_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5654 ULONGEST val)
ed9a39eb 5655{
cca44b1b
JB
5656 if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
5657 bx_write_pc (regs, val);
5658 else
36073a92 5659 branch_write_pc (regs, dsc, val);
cca44b1b 5660}
be8626e0 5661
cca44b1b
JB
5662/* Write to the PC as if from an ALU instruction. */
5663
5664static void
36073a92
YQ
5665alu_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5666 ULONGEST val)
cca44b1b 5667{
36073a92 5668 if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
cca44b1b
JB
5669 bx_write_pc (regs, val);
5670 else
36073a92 5671 branch_write_pc (regs, dsc, val);
cca44b1b
JB
5672}
5673
5674/* Helper for writing to registers for displaced stepping. Writing to the PC
5675 has a varying effects depending on the instruction which does the write:
5676 this is controlled by the WRITE_PC argument. */
5677
5678void
5679displaced_write_reg (struct regcache *regs, struct displaced_step_closure *dsc,
5680 int regno, ULONGEST val, enum pc_write_style write_pc)
5681{
bf9f652a 5682 if (regno == ARM_PC_REGNUM)
08216dd7 5683 {
cca44b1b
JB
5684 if (debug_displaced)
5685 fprintf_unfiltered (gdb_stdlog, "displaced: writing pc %.8lx\n",
5686 (unsigned long) val);
5687 switch (write_pc)
08216dd7 5688 {
cca44b1b 5689 case BRANCH_WRITE_PC:
36073a92 5690 branch_write_pc (regs, dsc, val);
08216dd7
RE
5691 break;
5692
cca44b1b
JB
5693 case BX_WRITE_PC:
5694 bx_write_pc (regs, val);
5695 break;
5696
5697 case LOAD_WRITE_PC:
36073a92 5698 load_write_pc (regs, dsc, val);
cca44b1b
JB
5699 break;
5700
5701 case ALU_WRITE_PC:
36073a92 5702 alu_write_pc (regs, dsc, val);
cca44b1b
JB
5703 break;
5704
5705 case CANNOT_WRITE_PC:
5706 warning (_("Instruction wrote to PC in an unexpected way when "
5707 "single-stepping"));
08216dd7
RE
5708 break;
5709
5710 default:
97b9747c
JB
5711 internal_error (__FILE__, __LINE__,
5712 _("Invalid argument to displaced_write_reg"));
08216dd7 5713 }
b508a996 5714
cca44b1b 5715 dsc->wrote_to_pc = 1;
b508a996 5716 }
ed9a39eb 5717 else
b508a996 5718 {
cca44b1b
JB
5719 if (debug_displaced)
5720 fprintf_unfiltered (gdb_stdlog, "displaced: writing r%d value %.8lx\n",
5721 regno, (unsigned long) val);
5722 regcache_cooked_write_unsigned (regs, regno, val);
b508a996 5723 }
34e8f22d
RE
5724}
5725
cca44b1b
JB
5726/* This function is used to concisely determine if an instruction INSN
5727 references PC. Register fields of interest in INSN should have the
0963b4bd
MS
5728 corresponding fields of BITMASK set to 0b1111. The function
5729 returns return 1 if any of these fields in INSN reference the PC
5730 (also 0b1111, r15), else it returns 0. */
67255d04
RE
5731
5732static int
cca44b1b 5733insn_references_pc (uint32_t insn, uint32_t bitmask)
67255d04 5734{
cca44b1b 5735 uint32_t lowbit = 1;
67255d04 5736
cca44b1b
JB
5737 while (bitmask != 0)
5738 {
5739 uint32_t mask;
44e1a9eb 5740
cca44b1b
JB
5741 for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
5742 ;
67255d04 5743
cca44b1b
JB
5744 if (!lowbit)
5745 break;
67255d04 5746
cca44b1b 5747 mask = lowbit * 0xf;
67255d04 5748
cca44b1b
JB
5749 if ((insn & mask) == mask)
5750 return 1;
5751
5752 bitmask &= ~mask;
67255d04
RE
5753 }
5754
cca44b1b
JB
5755 return 0;
5756}
2af48f68 5757
cca44b1b
JB
5758/* The simplest copy function. Many instructions have the same effect no
5759 matter what address they are executed at: in those cases, use this. */
67255d04 5760
cca44b1b 5761static int
7ff120b4
YQ
5762arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn,
5763 const char *iname, struct displaced_step_closure *dsc)
cca44b1b
JB
5764{
5765 if (debug_displaced)
5766 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx, "
5767 "opcode/class '%s' unmodified\n", (unsigned long) insn,
5768 iname);
67255d04 5769
cca44b1b 5770 dsc->modinsn[0] = insn;
67255d04 5771
cca44b1b
JB
5772 return 0;
5773}
5774
34518530
YQ
5775static int
5776thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
5777 uint16_t insn2, const char *iname,
5778 struct displaced_step_closure *dsc)
5779{
5780 if (debug_displaced)
5781 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x %.4x, "
5782 "opcode/class '%s' unmodified\n", insn1, insn2,
5783 iname);
5784
5785 dsc->modinsn[0] = insn1;
5786 dsc->modinsn[1] = insn2;
5787 dsc->numinsns = 2;
5788
5789 return 0;
5790}
5791
5792/* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
5793 modification. */
5794static int
5795thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, unsigned int insn,
5796 const char *iname,
5797 struct displaced_step_closure *dsc)
5798{
5799 if (debug_displaced)
5800 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x, "
5801 "opcode/class '%s' unmodified\n", insn,
5802 iname);
5803
5804 dsc->modinsn[0] = insn;
5805
5806 return 0;
5807}
5808
cca44b1b
JB
5809/* Preload instructions with immediate offset. */
5810
5811static void
6e39997a 5812cleanup_preload (struct gdbarch *gdbarch,
cca44b1b
JB
5813 struct regcache *regs, struct displaced_step_closure *dsc)
5814{
5815 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5816 if (!dsc->u.preload.immed)
5817 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5818}
5819
7ff120b4
YQ
5820static void
5821install_preload (struct gdbarch *gdbarch, struct regcache *regs,
5822 struct displaced_step_closure *dsc, unsigned int rn)
cca44b1b 5823{
cca44b1b 5824 ULONGEST rn_val;
cca44b1b
JB
5825 /* Preload instructions:
5826
5827 {pli/pld} [rn, #+/-imm]
5828 ->
5829 {pli/pld} [r0, #+/-imm]. */
5830
36073a92
YQ
5831 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5832 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 5833 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
cca44b1b
JB
5834 dsc->u.preload.immed = 1;
5835
cca44b1b 5836 dsc->cleanup = &cleanup_preload;
cca44b1b
JB
5837}
5838
cca44b1b 5839static int
7ff120b4 5840arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
cca44b1b
JB
5841 struct displaced_step_closure *dsc)
5842{
5843 unsigned int rn = bits (insn, 16, 19);
cca44b1b 5844
7ff120b4
YQ
5845 if (!insn_references_pc (insn, 0x000f0000ul))
5846 return arm_copy_unmodified (gdbarch, insn, "preload", dsc);
cca44b1b
JB
5847
5848 if (debug_displaced)
5849 fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
5850 (unsigned long) insn);
5851
7ff120b4
YQ
5852 dsc->modinsn[0] = insn & 0xfff0ffff;
5853
5854 install_preload (gdbarch, regs, dsc, rn);
5855
5856 return 0;
5857}
5858
34518530
YQ
5859static int
5860thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
5861 struct regcache *regs, struct displaced_step_closure *dsc)
5862{
5863 unsigned int rn = bits (insn1, 0, 3);
5864 unsigned int u_bit = bit (insn1, 7);
5865 int imm12 = bits (insn2, 0, 11);
5866 ULONGEST pc_val;
5867
5868 if (rn != ARM_PC_REGNUM)
5869 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);
5870
5871 /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
5872 PLD (literal) Encoding T1. */
5873 if (debug_displaced)
5874 fprintf_unfiltered (gdb_stdlog,
5875 "displaced: copying pld/pli pc (0x%x) %c imm12 %.4x\n",
5876 (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
5877 imm12);
5878
5879 if (!u_bit)
5880 imm12 = -1 * imm12;
5881
5882 /* Rewrite instruction {pli/pld} PC imm12 into:
5883 Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12
5884
5885 {pli/pld} [r0, r1]
5886
5887 Cleanup: r0 <- tmp[0], r1 <- tmp[1]. */
5888
5889 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5890 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5891
5892 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
5893
5894 displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
5895 displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
5896 dsc->u.preload.immed = 0;
5897
5898 /* {pli/pld} [r0, r1] */
5899 dsc->modinsn[0] = insn1 & 0xfff0;
5900 dsc->modinsn[1] = 0xf001;
5901 dsc->numinsns = 2;
5902
5903 dsc->cleanup = &cleanup_preload;
5904 return 0;
5905}
5906
7ff120b4
YQ
5907/* Preload instructions with register offset. */
5908
5909static void
5910install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
5911 struct displaced_step_closure *dsc, unsigned int rn,
5912 unsigned int rm)
5913{
5914 ULONGEST rn_val, rm_val;
5915
cca44b1b
JB
5916 /* Preload register-offset instructions:
5917
5918 {pli/pld} [rn, rm {, shift}]
5919 ->
5920 {pli/pld} [r0, r1 {, shift}]. */
5921
36073a92
YQ
5922 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5923 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5924 rn_val = displaced_read_reg (regs, dsc, rn);
5925 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5926 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5927 displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
cca44b1b
JB
5928 dsc->u.preload.immed = 0;
5929
cca44b1b 5930 dsc->cleanup = &cleanup_preload;
7ff120b4
YQ
5931}
5932
5933static int
5934arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
5935 struct regcache *regs,
5936 struct displaced_step_closure *dsc)
5937{
5938 unsigned int rn = bits (insn, 16, 19);
5939 unsigned int rm = bits (insn, 0, 3);
5940
5941
5942 if (!insn_references_pc (insn, 0x000f000ful))
5943 return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);
5944
5945 if (debug_displaced)
5946 fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
5947 (unsigned long) insn);
5948
5949 dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
cca44b1b 5950
7ff120b4 5951 install_preload_reg (gdbarch, regs, dsc, rn, rm);
cca44b1b
JB
5952 return 0;
5953}
5954
5955/* Copy/cleanup coprocessor load and store instructions. */
5956
5957static void
6e39997a 5958cleanup_copro_load_store (struct gdbarch *gdbarch,
cca44b1b
JB
5959 struct regcache *regs,
5960 struct displaced_step_closure *dsc)
5961{
36073a92 5962 ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5963
5964 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5965
5966 if (dsc->u.ldst.writeback)
5967 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
5968}
5969
7ff120b4
YQ
5970static void
5971install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
5972 struct displaced_step_closure *dsc,
5973 int writeback, unsigned int rn)
cca44b1b 5974{
cca44b1b 5975 ULONGEST rn_val;
cca44b1b 5976
cca44b1b
JB
5977 /* Coprocessor load/store instructions:
5978
5979 {stc/stc2} [<Rn>, #+/-imm] (and other immediate addressing modes)
5980 ->
5981 {stc/stc2} [r0, #+/-imm].
5982
5983 ldc/ldc2 are handled identically. */
5984
36073a92
YQ
5985 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5986 rn_val = displaced_read_reg (regs, dsc, rn);
2b16b2e3
YQ
5987 /* PC should be 4-byte aligned. */
5988 rn_val = rn_val & 0xfffffffc;
cca44b1b
JB
5989 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5990
7ff120b4 5991 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
5992 dsc->u.ldst.rn = rn;
5993
7ff120b4
YQ
5994 dsc->cleanup = &cleanup_copro_load_store;
5995}
5996
5997static int
5998arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
5999 struct regcache *regs,
6000 struct displaced_step_closure *dsc)
6001{
6002 unsigned int rn = bits (insn, 16, 19);
6003
6004 if (!insn_references_pc (insn, 0x000f0000ul))
6005 return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);
6006
6007 if (debug_displaced)
6008 fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
6009 "load/store insn %.8lx\n", (unsigned long) insn);
6010
cca44b1b
JB
6011 dsc->modinsn[0] = insn & 0xfff0ffff;
6012
7ff120b4 6013 install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);
cca44b1b
JB
6014
6015 return 0;
6016}
6017
34518530
YQ
6018static int
6019thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
6020 uint16_t insn2, struct regcache *regs,
6021 struct displaced_step_closure *dsc)
6022{
6023 unsigned int rn = bits (insn1, 0, 3);
6024
6025 if (rn != ARM_PC_REGNUM)
6026 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6027 "copro load/store", dsc);
6028
6029 if (debug_displaced)
6030 fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
6031 "load/store insn %.4x%.4x\n", insn1, insn2);
6032
6033 dsc->modinsn[0] = insn1 & 0xfff0;
6034 dsc->modinsn[1] = insn2;
6035 dsc->numinsns = 2;
6036
6037 /* This function is called for copying instruction LDC/LDC2/VLDR, which
6038 doesn't support writeback, so pass 0. */
6039 install_copro_load_store (gdbarch, regs, dsc, 0, rn);
6040
6041 return 0;
6042}
6043
cca44b1b
JB
6044/* Clean up branch instructions (actually perform the branch, by setting
6045 PC). */
6046
6047static void
6e39997a 6048cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
6049 struct displaced_step_closure *dsc)
6050{
36073a92 6051 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
6052 int branch_taken = condition_true (dsc->u.branch.cond, status);
6053 enum pc_write_style write_pc = dsc->u.branch.exchange
6054 ? BX_WRITE_PC : BRANCH_WRITE_PC;
6055
6056 if (!branch_taken)
6057 return;
6058
6059 if (dsc->u.branch.link)
6060 {
8c8dba6d
YQ
6061 /* The value of LR should be the next insn of current one. In order
6062 not to confuse logic hanlding later insn `bx lr', if current insn mode
6063 is Thumb, the bit 0 of LR value should be set to 1. */
6064 ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;
6065
6066 if (dsc->is_thumb)
6067 next_insn_addr |= 0x1;
6068
6069 displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
6070 CANNOT_WRITE_PC);
cca44b1b
JB
6071 }
6072
bf9f652a 6073 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
cca44b1b
JB
6074}
6075
6076/* Copy B/BL/BLX instructions with immediate destinations. */
6077
7ff120b4
YQ
6078static void
6079install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
6080 struct displaced_step_closure *dsc,
6081 unsigned int cond, int exchange, int link, long offset)
6082{
6083 /* Implement "BL<cond> <label>" as:
6084
6085 Preparation: cond <- instruction condition
6086 Insn: mov r0, r0 (nop)
6087 Cleanup: if (condition true) { r14 <- pc; pc <- label }.
6088
6089 B<cond> similar, but don't set r14 in cleanup. */
6090
6091 dsc->u.branch.cond = cond;
6092 dsc->u.branch.link = link;
6093 dsc->u.branch.exchange = exchange;
6094
2b16b2e3
YQ
6095 dsc->u.branch.dest = dsc->insn_addr;
6096 if (link && exchange)
6097 /* For BLX, offset is computed from the Align (PC, 4). */
6098 dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;
6099
7ff120b4 6100 if (dsc->is_thumb)
2b16b2e3 6101 dsc->u.branch.dest += 4 + offset;
7ff120b4 6102 else
2b16b2e3 6103 dsc->u.branch.dest += 8 + offset;
7ff120b4
YQ
6104
6105 dsc->cleanup = &cleanup_branch;
6106}
cca44b1b 6107static int
7ff120b4
YQ
6108arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
6109 struct regcache *regs, struct displaced_step_closure *dsc)
cca44b1b
JB
6110{
6111 unsigned int cond = bits (insn, 28, 31);
6112 int exchange = (cond == 0xf);
6113 int link = exchange || bit (insn, 24);
cca44b1b
JB
6114 long offset;
6115
6116 if (debug_displaced)
6117 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s immediate insn "
6118 "%.8lx\n", (exchange) ? "blx" : (link) ? "bl" : "b",
6119 (unsigned long) insn);
cca44b1b
JB
6120 if (exchange)
6121 /* For BLX, set bit 0 of the destination. The cleanup_branch function will
6122 then arrange the switch into Thumb mode. */
6123 offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
6124 else
6125 offset = bits (insn, 0, 23) << 2;
6126
6127 if (bit (offset, 25))
6128 offset = offset | ~0x3ffffff;
6129
cca44b1b
JB
6130 dsc->modinsn[0] = ARM_NOP;
6131
7ff120b4 6132 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
cca44b1b
JB
6133 return 0;
6134}
6135
34518530
YQ
6136static int
6137thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
6138 uint16_t insn2, struct regcache *regs,
6139 struct displaced_step_closure *dsc)
6140{
6141 int link = bit (insn2, 14);
6142 int exchange = link && !bit (insn2, 12);
6143 int cond = INST_AL;
6144 long offset = 0;
6145 int j1 = bit (insn2, 13);
6146 int j2 = bit (insn2, 11);
6147 int s = sbits (insn1, 10, 10);
6148 int i1 = !(j1 ^ bit (insn1, 10));
6149 int i2 = !(j2 ^ bit (insn1, 10));
6150
6151 if (!link && !exchange) /* B */
6152 {
6153 offset = (bits (insn2, 0, 10) << 1);
6154 if (bit (insn2, 12)) /* Encoding T4 */
6155 {
6156 offset |= (bits (insn1, 0, 9) << 12)
6157 | (i2 << 22)
6158 | (i1 << 23)
6159 | (s << 24);
6160 cond = INST_AL;
6161 }
6162 else /* Encoding T3 */
6163 {
6164 offset |= (bits (insn1, 0, 5) << 12)
6165 | (j1 << 18)
6166 | (j2 << 19)
6167 | (s << 20);
6168 cond = bits (insn1, 6, 9);
6169 }
6170 }
6171 else
6172 {
6173 offset = (bits (insn1, 0, 9) << 12);
6174 offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
6175 offset |= exchange ?
6176 (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
6177 }
6178
6179 if (debug_displaced)
6180 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s insn "
6181 "%.4x %.4x with offset %.8lx\n",
6182 link ? (exchange) ? "blx" : "bl" : "b",
6183 insn1, insn2, offset);
6184
6185 dsc->modinsn[0] = THUMB_NOP;
6186
6187 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
6188 return 0;
6189}
6190
6191/* Copy B Thumb instructions. */
6192static int
6193thumb_copy_b (struct gdbarch *gdbarch, unsigned short insn,
6194 struct displaced_step_closure *dsc)
6195{
6196 unsigned int cond = 0;
6197 int offset = 0;
6198 unsigned short bit_12_15 = bits (insn, 12, 15);
6199 CORE_ADDR from = dsc->insn_addr;
6200
6201 if (bit_12_15 == 0xd)
6202 {
6203 /* offset = SignExtend (imm8:0, 32) */
6204 offset = sbits ((insn << 1), 0, 8);
6205 cond = bits (insn, 8, 11);
6206 }
6207 else if (bit_12_15 == 0xe) /* Encoding T2 */
6208 {
6209 offset = sbits ((insn << 1), 0, 11);
6210 cond = INST_AL;
6211 }
6212
6213 if (debug_displaced)
6214 fprintf_unfiltered (gdb_stdlog,
6215 "displaced: copying b immediate insn %.4x "
6216 "with offset %d\n", insn, offset);
6217
6218 dsc->u.branch.cond = cond;
6219 dsc->u.branch.link = 0;
6220 dsc->u.branch.exchange = 0;
6221 dsc->u.branch.dest = from + 4 + offset;
6222
6223 dsc->modinsn[0] = THUMB_NOP;
6224
6225 dsc->cleanup = &cleanup_branch;
6226
6227 return 0;
6228}
6229
cca44b1b
JB
6230/* Copy BX/BLX with register-specified destinations. */
6231
7ff120b4
YQ
6232static void
6233install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
6234 struct displaced_step_closure *dsc, int link,
6235 unsigned int cond, unsigned int rm)
cca44b1b 6236{
cca44b1b
JB
6237 /* Implement {BX,BLX}<cond> <reg>" as:
6238
6239 Preparation: cond <- instruction condition
6240 Insn: mov r0, r0 (nop)
6241 Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
6242
6243 Don't set r14 in cleanup for BX. */
6244
36073a92 6245 dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6246
6247 dsc->u.branch.cond = cond;
6248 dsc->u.branch.link = link;
cca44b1b 6249
7ff120b4 6250 dsc->u.branch.exchange = 1;
cca44b1b
JB
6251
6252 dsc->cleanup = &cleanup_branch;
7ff120b4 6253}
cca44b1b 6254
7ff120b4
YQ
6255static int
6256arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
6257 struct regcache *regs, struct displaced_step_closure *dsc)
6258{
6259 unsigned int cond = bits (insn, 28, 31);
6260 /* BX: x12xxx1x
6261 BLX: x12xxx3x. */
6262 int link = bit (insn, 5);
6263 unsigned int rm = bits (insn, 0, 3);
6264
6265 if (debug_displaced)
6266 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx",
6267 (unsigned long) insn);
6268
6269 dsc->modinsn[0] = ARM_NOP;
6270
6271 install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
cca44b1b
JB
6272 return 0;
6273}
6274
34518530
YQ
6275static int
6276thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
6277 struct regcache *regs,
6278 struct displaced_step_closure *dsc)
6279{
6280 int link = bit (insn, 7);
6281 unsigned int rm = bits (insn, 3, 6);
6282
6283 if (debug_displaced)
6284 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x",
6285 (unsigned short) insn);
6286
6287 dsc->modinsn[0] = THUMB_NOP;
6288
6289 install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);
6290
6291 return 0;
6292}
6293
6294
0963b4bd 6295/* Copy/cleanup arithmetic/logic instruction with immediate RHS. */
cca44b1b
JB
6296
6297static void
6e39997a 6298cleanup_alu_imm (struct gdbarch *gdbarch,
cca44b1b
JB
6299 struct regcache *regs, struct displaced_step_closure *dsc)
6300{
36073a92 6301 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6302 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6303 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6304 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6305}
6306
6307static int
7ff120b4
YQ
6308arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
6309 struct displaced_step_closure *dsc)
cca44b1b
JB
6310{
6311 unsigned int rn = bits (insn, 16, 19);
6312 unsigned int rd = bits (insn, 12, 15);
6313 unsigned int op = bits (insn, 21, 24);
6314 int is_mov = (op == 0xd);
6315 ULONGEST rd_val, rn_val;
cca44b1b
JB
6316
6317 if (!insn_references_pc (insn, 0x000ff000ul))
7ff120b4 6318 return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
cca44b1b
JB
6319
6320 if (debug_displaced)
6321 fprintf_unfiltered (gdb_stdlog, "displaced: copying immediate %s insn "
6322 "%.8lx\n", is_mov ? "move" : "ALU",
6323 (unsigned long) insn);
6324
6325 /* Instruction is of form:
6326
6327 <op><cond> rd, [rn,] #imm
6328
6329 Rewrite as:
6330
6331 Preparation: tmp1, tmp2 <- r0, r1;
6332 r0, r1 <- rd, rn
6333 Insn: <op><cond> r0, r1, #imm
6334 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6335 */
6336
36073a92
YQ
6337 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6338 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6339 rn_val = displaced_read_reg (regs, dsc, rn);
6340 rd_val = displaced_read_reg (regs, dsc, rd);
cca44b1b
JB
6341 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6342 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6343 dsc->rd = rd;
6344
6345 if (is_mov)
6346 dsc->modinsn[0] = insn & 0xfff00fff;
6347 else
6348 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
6349
6350 dsc->cleanup = &cleanup_alu_imm;
6351
6352 return 0;
6353}
6354
34518530
YQ
6355static int
6356thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
6357 uint16_t insn2, struct regcache *regs,
6358 struct displaced_step_closure *dsc)
6359{
6360 unsigned int op = bits (insn1, 5, 8);
6361 unsigned int rn, rm, rd;
6362 ULONGEST rd_val, rn_val;
6363
6364 rn = bits (insn1, 0, 3); /* Rn */
6365 rm = bits (insn2, 0, 3); /* Rm */
6366 rd = bits (insn2, 8, 11); /* Rd */
6367
6368 /* This routine is only called for instruction MOV. */
6369 gdb_assert (op == 0x2 && rn == 0xf);
6370
6371 if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
6372 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);
6373
6374 if (debug_displaced)
6375 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.4x%.4x\n",
6376 "ALU", insn1, insn2);
6377
6378 /* Instruction is of form:
6379
6380 <op><cond> rd, [rn,] #imm
6381
6382 Rewrite as:
6383
6384 Preparation: tmp1, tmp2 <- r0, r1;
6385 r0, r1 <- rd, rn
6386 Insn: <op><cond> r0, r1, #imm
6387 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6388 */
6389
6390 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6391 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6392 rn_val = displaced_read_reg (regs, dsc, rn);
6393 rd_val = displaced_read_reg (regs, dsc, rd);
6394 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6395 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6396 dsc->rd = rd;
6397
6398 dsc->modinsn[0] = insn1;
6399 dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
6400 dsc->numinsns = 2;
6401
6402 dsc->cleanup = &cleanup_alu_imm;
6403
6404 return 0;
6405}
6406
cca44b1b
JB
6407/* Copy/cleanup arithmetic/logic insns with register RHS. */
6408
6409static void
6e39997a 6410cleanup_alu_reg (struct gdbarch *gdbarch,
cca44b1b
JB
6411 struct regcache *regs, struct displaced_step_closure *dsc)
6412{
6413 ULONGEST rd_val;
6414 int i;
6415
36073a92 6416 rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6417
6418 for (i = 0; i < 3; i++)
6419 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6420
6421 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6422}
6423
7ff120b4
YQ
6424static void
6425install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
6426 struct displaced_step_closure *dsc,
6427 unsigned int rd, unsigned int rn, unsigned int rm)
cca44b1b 6428{
cca44b1b 6429 ULONGEST rd_val, rn_val, rm_val;
cca44b1b 6430
cca44b1b
JB
6431 /* Instruction is of form:
6432
6433 <op><cond> rd, [rn,] rm [, <shift>]
6434
6435 Rewrite as:
6436
6437 Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
6438 r0, r1, r2 <- rd, rn, rm
6439 Insn: <op><cond> r0, r1, r2 [, <shift>]
6440 Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
6441 */
6442
36073a92
YQ
6443 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6444 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6445 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6446 rd_val = displaced_read_reg (regs, dsc, rd);
6447 rn_val = displaced_read_reg (regs, dsc, rn);
6448 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6449 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6450 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6451 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6452 dsc->rd = rd;
6453
7ff120b4
YQ
6454 dsc->cleanup = &cleanup_alu_reg;
6455}
6456
6457static int
6458arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
6459 struct displaced_step_closure *dsc)
6460{
6461 unsigned int op = bits (insn, 21, 24);
6462 int is_mov = (op == 0xd);
6463
6464 if (!insn_references_pc (insn, 0x000ff00ful))
6465 return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);
6466
6467 if (debug_displaced)
6468 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.8lx\n",
6469 is_mov ? "move" : "ALU", (unsigned long) insn);
6470
cca44b1b
JB
6471 if (is_mov)
6472 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
6473 else
6474 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
6475
7ff120b4
YQ
6476 install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
6477 bits (insn, 0, 3));
cca44b1b
JB
6478 return 0;
6479}
6480
34518530
YQ
6481static int
6482thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
6483 struct regcache *regs,
6484 struct displaced_step_closure *dsc)
6485{
6486 unsigned rn, rm, rd;
6487
6488 rd = bits (insn, 3, 6);
6489 rn = (bit (insn, 7) << 3) | bits (insn, 0, 2);
6490 rm = 2;
6491
6492 if (rd != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
6493 return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);
6494
6495 if (debug_displaced)
6496 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.4x\n",
6497 "ALU", (unsigned short) insn);
6498
6499 dsc->modinsn[0] = ((insn & 0xff00) | 0x08);
6500
6501 install_alu_reg (gdbarch, regs, dsc, rd, rn, rm);
6502
6503 return 0;
6504}
6505
cca44b1b
JB
6506/* Cleanup/copy arithmetic/logic insns with shifted register RHS. */
6507
6508static void
6e39997a 6509cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
cca44b1b
JB
6510 struct regcache *regs,
6511 struct displaced_step_closure *dsc)
6512{
36073a92 6513 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6514 int i;
6515
6516 for (i = 0; i < 4; i++)
6517 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6518
6519 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6520}
6521
7ff120b4
YQ
6522static void
6523install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
6524 struct displaced_step_closure *dsc,
6525 unsigned int rd, unsigned int rn, unsigned int rm,
6526 unsigned rs)
cca44b1b 6527{
7ff120b4 6528 int i;
cca44b1b 6529 ULONGEST rd_val, rn_val, rm_val, rs_val;
cca44b1b 6530
cca44b1b
JB
6531 /* Instruction is of form:
6532
6533 <op><cond> rd, [rn,] rm, <shift> rs
6534
6535 Rewrite as:
6536
6537 Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
6538 r0, r1, r2, r3 <- rd, rn, rm, rs
6539 Insn: <op><cond> r0, r1, r2, <shift> r3
6540 Cleanup: tmp5 <- r0
6541 r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
6542 rd <- tmp5
6543 */
6544
6545 for (i = 0; i < 4; i++)
36073a92 6546 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b 6547
36073a92
YQ
6548 rd_val = displaced_read_reg (regs, dsc, rd);
6549 rn_val = displaced_read_reg (regs, dsc, rn);
6550 rm_val = displaced_read_reg (regs, dsc, rm);
6551 rs_val = displaced_read_reg (regs, dsc, rs);
cca44b1b
JB
6552 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6553 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6554 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6555 displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
6556 dsc->rd = rd;
7ff120b4
YQ
6557 dsc->cleanup = &cleanup_alu_shifted_reg;
6558}
6559
6560static int
6561arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
6562 struct regcache *regs,
6563 struct displaced_step_closure *dsc)
6564{
6565 unsigned int op = bits (insn, 21, 24);
6566 int is_mov = (op == 0xd);
6567 unsigned int rd, rn, rm, rs;
6568
6569 if (!insn_references_pc (insn, 0x000fff0ful))
6570 return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
6571
6572 if (debug_displaced)
6573 fprintf_unfiltered (gdb_stdlog, "displaced: copying shifted reg %s insn "
6574 "%.8lx\n", is_mov ? "move" : "ALU",
6575 (unsigned long) insn);
6576
6577 rn = bits (insn, 16, 19);
6578 rm = bits (insn, 0, 3);
6579 rs = bits (insn, 8, 11);
6580 rd = bits (insn, 12, 15);
cca44b1b
JB
6581
6582 if (is_mov)
6583 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
6584 else
6585 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
6586
7ff120b4 6587 install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);
cca44b1b
JB
6588
6589 return 0;
6590}
6591
6592/* Clean up load instructions. */
6593
6594static void
6e39997a 6595cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
6596 struct displaced_step_closure *dsc)
6597{
6598 ULONGEST rt_val, rt_val2 = 0, rn_val;
cca44b1b 6599
36073a92 6600 rt_val = displaced_read_reg (regs, dsc, 0);
cca44b1b 6601 if (dsc->u.ldst.xfersize == 8)
36073a92
YQ
6602 rt_val2 = displaced_read_reg (regs, dsc, 1);
6603 rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
6604
6605 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6606 if (dsc->u.ldst.xfersize > 4)
6607 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6608 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6609 if (!dsc->u.ldst.immed)
6610 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6611
6612 /* Handle register writeback. */
6613 if (dsc->u.ldst.writeback)
6614 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6615 /* Put result in right place. */
6616 displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
6617 if (dsc->u.ldst.xfersize == 8)
6618 displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
6619}
6620
6621/* Clean up store instructions. */
6622
6623static void
6e39997a 6624cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
6625 struct displaced_step_closure *dsc)
6626{
36073a92 6627 ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
6628
6629 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6630 if (dsc->u.ldst.xfersize > 4)
6631 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6632 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6633 if (!dsc->u.ldst.immed)
6634 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6635 if (!dsc->u.ldst.restore_r4)
6636 displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
6637
6638 /* Writeback. */
6639 if (dsc->u.ldst.writeback)
6640 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6641}
6642
6643/* Copy "extra" load/store instructions. These are halfword/doubleword
6644 transfers, which have a different encoding to byte/word transfers. */
6645
6646static int
7ff120b4
YQ
6647arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unpriveleged,
6648 struct regcache *regs, struct displaced_step_closure *dsc)
cca44b1b
JB
6649{
6650 unsigned int op1 = bits (insn, 20, 24);
6651 unsigned int op2 = bits (insn, 5, 6);
6652 unsigned int rt = bits (insn, 12, 15);
6653 unsigned int rn = bits (insn, 16, 19);
6654 unsigned int rm = bits (insn, 0, 3);
6655 char load[12] = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
6656 char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
6657 int immed = (op1 & 0x4) != 0;
6658 int opcode;
6659 ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
cca44b1b
JB
6660
6661 if (!insn_references_pc (insn, 0x000ff00ful))
7ff120b4 6662 return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);
cca44b1b
JB
6663
6664 if (debug_displaced)
6665 fprintf_unfiltered (gdb_stdlog, "displaced: copying %sextra load/store "
6666 "insn %.8lx\n", unpriveleged ? "unpriveleged " : "",
6667 (unsigned long) insn);
6668
6669 opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
6670
6671 if (opcode < 0)
6672 internal_error (__FILE__, __LINE__,
6673 _("copy_extra_ld_st: instruction decode error"));
6674
36073a92
YQ
6675 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6676 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6677 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 6678 if (!immed)
36073a92 6679 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 6680
36073a92 6681 rt_val = displaced_read_reg (regs, dsc, rt);
cca44b1b 6682 if (bytesize[opcode] == 8)
36073a92
YQ
6683 rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
6684 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 6685 if (!immed)
36073a92 6686 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6687
6688 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6689 if (bytesize[opcode] == 8)
6690 displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
6691 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6692 if (!immed)
6693 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
6694
6695 dsc->rd = rt;
6696 dsc->u.ldst.xfersize = bytesize[opcode];
6697 dsc->u.ldst.rn = rn;
6698 dsc->u.ldst.immed = immed;
6699 dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
6700 dsc->u.ldst.restore_r4 = 0;
6701
6702 if (immed)
6703 /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
6704 ->
6705 {ldr,str}<width><cond> r0, [r1,] [r2, #imm]. */
6706 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6707 else
6708 /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
6709 ->
6710 {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3]. */
6711 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6712
6713 dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
6714
6715 return 0;
6716}
6717
0f6f04ba 6718/* Copy byte/half word/word loads and stores. */
cca44b1b 6719
7ff120b4 6720static void
0f6f04ba
YQ
6721install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
6722 struct displaced_step_closure *dsc, int load,
6723 int immed, int writeback, int size, int usermode,
6724 int rt, int rm, int rn)
cca44b1b 6725{
cca44b1b 6726 ULONGEST rt_val, rn_val, rm_val = 0;
cca44b1b 6727
36073a92
YQ
6728 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6729 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 6730 if (!immed)
36073a92 6731 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 6732 if (!load)
36073a92 6733 dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);
cca44b1b 6734
36073a92
YQ
6735 rt_val = displaced_read_reg (regs, dsc, rt);
6736 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 6737 if (!immed)
36073a92 6738 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6739
6740 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6741 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6742 if (!immed)
6743 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
cca44b1b 6744 dsc->rd = rt;
0f6f04ba 6745 dsc->u.ldst.xfersize = size;
cca44b1b
JB
6746 dsc->u.ldst.rn = rn;
6747 dsc->u.ldst.immed = immed;
7ff120b4 6748 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
6749
6750 /* To write PC we can do:
6751
494e194e
YQ
6752 Before this sequence of instructions:
6753 r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
6754 r2 is the Rn value got from dispalced_read_reg.
6755
6756 Insn1: push {pc} Write address of STR instruction + offset on stack
6757 Insn2: pop {r4} Read it back from stack, r4 = addr(Insn1) + offset
6758 Insn3: sub r4, r4, pc r4 = addr(Insn1) + offset - pc
6759 = addr(Insn1) + offset - addr(Insn3) - 8
6760 = offset - 16
6761 Insn4: add r4, r4, #8 r4 = offset - 8
6762 Insn5: add r0, r0, r4 r0 = from + 8 + offset - 8
6763 = from + offset
6764 Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])
cca44b1b
JB
6765
6766 Otherwise we don't know what value to write for PC, since the offset is
494e194e
YQ
6767 architecture-dependent (sometimes PC+8, sometimes PC+12). More details
6768 of this can be found in Section "Saving from r15" in
6769 http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */
cca44b1b 6770
7ff120b4
YQ
6771 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6772}
6773
34518530
YQ
6774
6775static int
6776thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
6777 uint16_t insn2, struct regcache *regs,
6778 struct displaced_step_closure *dsc, int size)
6779{
6780 unsigned int u_bit = bit (insn1, 7);
6781 unsigned int rt = bits (insn2, 12, 15);
6782 int imm12 = bits (insn2, 0, 11);
6783 ULONGEST pc_val;
6784
6785 if (debug_displaced)
6786 fprintf_unfiltered (gdb_stdlog,
6787 "displaced: copying ldr pc (0x%x) R%d %c imm12 %.4x\n",
6788 (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
6789 imm12);
6790
6791 if (!u_bit)
6792 imm12 = -1 * imm12;
6793
6794 /* Rewrite instruction LDR Rt imm12 into:
6795
6796 Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12
6797
6798 LDR R0, R2, R3,
6799
6800 Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2]. */
6801
6802
6803 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6804 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6805 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6806
6807 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6808
6809 pc_val = pc_val & 0xfffffffc;
6810
6811 displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
6812 displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);
6813
6814 dsc->rd = rt;
6815
6816 dsc->u.ldst.xfersize = size;
6817 dsc->u.ldst.immed = 0;
6818 dsc->u.ldst.writeback = 0;
6819 dsc->u.ldst.restore_r4 = 0;
6820
6821 /* LDR R0, R2, R3 */
6822 dsc->modinsn[0] = 0xf852;
6823 dsc->modinsn[1] = 0x3;
6824 dsc->numinsns = 2;
6825
6826 dsc->cleanup = &cleanup_load;
6827
6828 return 0;
6829}
6830
6831static int
6832thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
6833 uint16_t insn2, struct regcache *regs,
6834 struct displaced_step_closure *dsc,
6835 int writeback, int immed)
6836{
6837 unsigned int rt = bits (insn2, 12, 15);
6838 unsigned int rn = bits (insn1, 0, 3);
6839 unsigned int rm = bits (insn2, 0, 3); /* Only valid if !immed. */
6840 /* In LDR (register), there is also a register Rm, which is not allowed to
6841 be PC, so we don't have to check it. */
6842
6843 if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
6844 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
6845 dsc);
6846
6847 if (debug_displaced)
6848 fprintf_unfiltered (gdb_stdlog,
6849 "displaced: copying ldr r%d [r%d] insn %.4x%.4x\n",
6850 rt, rn, insn1, insn2);
6851
6852 install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
6853 0, rt, rm, rn);
6854
6855 dsc->u.ldst.restore_r4 = 0;
6856
6857 if (immed)
6858 /* ldr[b]<cond> rt, [rn, #imm], etc.
6859 ->
6860 ldr[b]<cond> r0, [r2, #imm]. */
6861 {
6862 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6863 dsc->modinsn[1] = insn2 & 0x0fff;
6864 }
6865 else
6866 /* ldr[b]<cond> rt, [rn, rm], etc.
6867 ->
6868 ldr[b]<cond> r0, [r2, r3]. */
6869 {
6870 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6871 dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
6872 }
6873
6874 dsc->numinsns = 2;
6875
6876 return 0;
6877}
6878
6879
7ff120b4
YQ
6880static int
6881arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
6882 struct regcache *regs,
6883 struct displaced_step_closure *dsc,
0f6f04ba 6884 int load, int size, int usermode)
7ff120b4
YQ
6885{
6886 int immed = !bit (insn, 25);
6887 int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
6888 unsigned int rt = bits (insn, 12, 15);
6889 unsigned int rn = bits (insn, 16, 19);
6890 unsigned int rm = bits (insn, 0, 3); /* Only valid if !immed. */
6891
6892 if (!insn_references_pc (insn, 0x000ff00ful))
6893 return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);
6894
6895 if (debug_displaced)
6896 fprintf_unfiltered (gdb_stdlog,
6897 "displaced: copying %s%s r%d [r%d] insn %.8lx\n",
0f6f04ba
YQ
6898 load ? (size == 1 ? "ldrb" : "ldr")
6899 : (size == 1 ? "strb" : "str"), usermode ? "t" : "",
7ff120b4
YQ
6900 rt, rn,
6901 (unsigned long) insn);
6902
0f6f04ba
YQ
6903 install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
6904 usermode, rt, rm, rn);
7ff120b4 6905
bf9f652a 6906 if (load || rt != ARM_PC_REGNUM)
cca44b1b
JB
6907 {
6908 dsc->u.ldst.restore_r4 = 0;
6909
6910 if (immed)
6911 /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
6912 ->
6913 {ldr,str}[b]<cond> r0, [r2, #imm]. */
6914 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6915 else
6916 /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
6917 ->
6918 {ldr,str}[b]<cond> r0, [r2, r3]. */
6919 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6920 }
6921 else
6922 {
6923 /* We need to use r4 as scratch. Make sure it's restored afterwards. */
6924 dsc->u.ldst.restore_r4 = 1;
494e194e
YQ
6925 dsc->modinsn[0] = 0xe92d8000; /* push {pc} */
6926 dsc->modinsn[1] = 0xe8bd0010; /* pop {r4} */
cca44b1b
JB
6927 dsc->modinsn[2] = 0xe044400f; /* sub r4, r4, pc. */
6928 dsc->modinsn[3] = 0xe2844008; /* add r4, r4, #8. */
6929 dsc->modinsn[4] = 0xe0800004; /* add r0, r0, r4. */
6930
6931 /* As above. */
6932 if (immed)
6933 dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
6934 else
6935 dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
6936
cca44b1b
JB
6937 dsc->numinsns = 6;
6938 }
6939
6940 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6941
6942 return 0;
6943}
6944
6945/* Cleanup LDM instructions with fully-populated register list. This is an
6946 unfortunate corner case: it's impossible to implement correctly by modifying
6947 the instruction. The issue is as follows: we have an instruction,
6948
6949 ldm rN, {r0-r15}
6950
6951 which we must rewrite to avoid loading PC. A possible solution would be to
6952 do the load in two halves, something like (with suitable cleanup
6953 afterwards):
6954
6955 mov r8, rN
6956 ldm[id][ab] r8!, {r0-r7}
6957 str r7, <temp>
6958 ldm[id][ab] r8, {r7-r14}
6959 <bkpt>
6960
6961 but at present there's no suitable place for <temp>, since the scratch space
6962 is overwritten before the cleanup routine is called. For now, we simply
6963 emulate the instruction. */
6964
6965static void
6966cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
6967 struct displaced_step_closure *dsc)
6968{
cca44b1b
JB
6969 int inc = dsc->u.block.increment;
6970 int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
6971 int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
6972 uint32_t regmask = dsc->u.block.regmask;
6973 int regno = inc ? 0 : 15;
6974 CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
6975 int exception_return = dsc->u.block.load && dsc->u.block.user
6976 && (regmask & 0x8000) != 0;
36073a92 6977 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
6978 int do_transfer = condition_true (dsc->u.block.cond, status);
6979 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6980
6981 if (!do_transfer)
6982 return;
6983
6984 /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
6985 sensible we can do here. Complain loudly. */
6986 if (exception_return)
6987 error (_("Cannot single-step exception return"));
6988
6989 /* We don't handle any stores here for now. */
6990 gdb_assert (dsc->u.block.load != 0);
6991
6992 if (debug_displaced)
6993 fprintf_unfiltered (gdb_stdlog, "displaced: emulating block transfer: "
6994 "%s %s %s\n", dsc->u.block.load ? "ldm" : "stm",
6995 dsc->u.block.increment ? "inc" : "dec",
6996 dsc->u.block.before ? "before" : "after");
6997
6998 while (regmask)
6999 {
7000 uint32_t memword;
7001
7002 if (inc)
bf9f652a 7003 while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
cca44b1b
JB
7004 regno++;
7005 else
7006 while (regno >= 0 && (regmask & (1 << regno)) == 0)
7007 regno--;
7008
7009 xfer_addr += bump_before;
7010
7011 memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
7012 displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
7013
7014 xfer_addr += bump_after;
7015
7016 regmask &= ~(1 << regno);
7017 }
7018
7019 if (dsc->u.block.writeback)
7020 displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
7021 CANNOT_WRITE_PC);
7022}
7023
7024/* Clean up an STM which included the PC in the register list. */
7025
7026static void
7027cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
7028 struct displaced_step_closure *dsc)
7029{
36073a92 7030 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
7031 int store_executed = condition_true (dsc->u.block.cond, status);
7032 CORE_ADDR pc_stored_at, transferred_regs = bitcount (dsc->u.block.regmask);
7033 CORE_ADDR stm_insn_addr;
7034 uint32_t pc_val;
7035 long offset;
7036 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7037
7038 /* If condition code fails, there's nothing else to do. */
7039 if (!store_executed)
7040 return;
7041
7042 if (dsc->u.block.increment)
7043 {
7044 pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
7045
7046 if (dsc->u.block.before)
7047 pc_stored_at += 4;
7048 }
7049 else
7050 {
7051 pc_stored_at = dsc->u.block.xfer_addr;
7052
7053 if (dsc->u.block.before)
7054 pc_stored_at -= 4;
7055 }
7056
7057 pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
7058 stm_insn_addr = dsc->scratch_base;
7059 offset = pc_val - stm_insn_addr;
7060
7061 if (debug_displaced)
7062 fprintf_unfiltered (gdb_stdlog, "displaced: detected PC offset %.8lx for "
7063 "STM instruction\n", offset);
7064
7065 /* Rewrite the stored PC to the proper value for the non-displaced original
7066 instruction. */
7067 write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
7068 dsc->insn_addr + offset);
7069}
7070
7071/* Clean up an LDM which includes the PC in the register list. We clumped all
7072 the registers in the transferred list into a contiguous range r0...rX (to
7073 avoid loading PC directly and losing control of the debugged program), so we
7074 must undo that here. */
7075
7076static void
6e39997a 7077cleanup_block_load_pc (struct gdbarch *gdbarch,
cca44b1b
JB
7078 struct regcache *regs,
7079 struct displaced_step_closure *dsc)
7080{
36073a92 7081 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
22e048c9 7082 int load_executed = condition_true (dsc->u.block.cond, status);
bf9f652a 7083 unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
cca44b1b
JB
7084 unsigned int regs_loaded = bitcount (mask);
7085 unsigned int num_to_shuffle = regs_loaded, clobbered;
7086
7087 /* The method employed here will fail if the register list is fully populated
7088 (we need to avoid loading PC directly). */
7089 gdb_assert (num_to_shuffle < 16);
7090
7091 if (!load_executed)
7092 return;
7093
7094 clobbered = (1 << num_to_shuffle) - 1;
7095
7096 while (num_to_shuffle > 0)
7097 {
7098 if ((mask & (1 << write_reg)) != 0)
7099 {
7100 unsigned int read_reg = num_to_shuffle - 1;
7101
7102 if (read_reg != write_reg)
7103 {
36073a92 7104 ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
cca44b1b
JB
7105 displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
7106 if (debug_displaced)
7107 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: move "
7108 "loaded register r%d to r%d\n"), read_reg,
7109 write_reg);
7110 }
7111 else if (debug_displaced)
7112 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: register "
7113 "r%d already in the right place\n"),
7114 write_reg);
7115
7116 clobbered &= ~(1 << write_reg);
7117
7118 num_to_shuffle--;
7119 }
7120
7121 write_reg--;
7122 }
7123
7124 /* Restore any registers we scribbled over. */
7125 for (write_reg = 0; clobbered != 0; write_reg++)
7126 {
7127 if ((clobbered & (1 << write_reg)) != 0)
7128 {
7129 displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
7130 CANNOT_WRITE_PC);
7131 if (debug_displaced)
7132 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: restored "
7133 "clobbered register r%d\n"), write_reg);
7134 clobbered &= ~(1 << write_reg);
7135 }
7136 }
7137
7138 /* Perform register writeback manually. */
7139 if (dsc->u.block.writeback)
7140 {
7141 ULONGEST new_rn_val = dsc->u.block.xfer_addr;
7142
7143 if (dsc->u.block.increment)
7144 new_rn_val += regs_loaded * 4;
7145 else
7146 new_rn_val -= regs_loaded * 4;
7147
7148 displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
7149 CANNOT_WRITE_PC);
7150 }
7151}
7152
7153/* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
7154 in user-level code (in particular exception return, ldm rn, {...pc}^). */
7155
7156static int
7ff120b4
YQ
7157arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
7158 struct regcache *regs,
7159 struct displaced_step_closure *dsc)
cca44b1b
JB
7160{
7161 int load = bit (insn, 20);
7162 int user = bit (insn, 22);
7163 int increment = bit (insn, 23);
7164 int before = bit (insn, 24);
7165 int writeback = bit (insn, 21);
7166 int rn = bits (insn, 16, 19);
cca44b1b 7167
0963b4bd
MS
7168 /* Block transfers which don't mention PC can be run directly
7169 out-of-line. */
bf9f652a 7170 if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
7ff120b4 7171 return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
cca44b1b 7172
bf9f652a 7173 if (rn == ARM_PC_REGNUM)
cca44b1b 7174 {
0963b4bd
MS
7175 warning (_("displaced: Unpredictable LDM or STM with "
7176 "base register r15"));
7ff120b4 7177 return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
cca44b1b
JB
7178 }
7179
7180 if (debug_displaced)
7181 fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
7182 "%.8lx\n", (unsigned long) insn);
7183
36073a92 7184 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b
JB
7185 dsc->u.block.rn = rn;
7186
7187 dsc->u.block.load = load;
7188 dsc->u.block.user = user;
7189 dsc->u.block.increment = increment;
7190 dsc->u.block.before = before;
7191 dsc->u.block.writeback = writeback;
7192 dsc->u.block.cond = bits (insn, 28, 31);
7193
7194 dsc->u.block.regmask = insn & 0xffff;
7195
7196 if (load)
7197 {
7198 if ((insn & 0xffff) == 0xffff)
7199 {
7200 /* LDM with a fully-populated register list. This case is
7201 particularly tricky. Implement for now by fully emulating the
7202 instruction (which might not behave perfectly in all cases, but
7203 these instructions should be rare enough for that not to matter
7204 too much). */
7205 dsc->modinsn[0] = ARM_NOP;
7206
7207 dsc->cleanup = &cleanup_block_load_all;
7208 }
7209 else
7210 {
7211 /* LDM of a list of registers which includes PC. Implement by
7212 rewriting the list of registers to be transferred into a
7213 contiguous chunk r0...rX before doing the transfer, then shuffling
7214 registers into the correct places in the cleanup routine. */
7215 unsigned int regmask = insn & 0xffff;
7216 unsigned int num_in_list = bitcount (regmask), new_regmask, bit = 1;
7217 unsigned int to = 0, from = 0, i, new_rn;
7218
7219 for (i = 0; i < num_in_list; i++)
36073a92 7220 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b
JB
7221
7222 /* Writeback makes things complicated. We need to avoid clobbering
7223 the base register with one of the registers in our modified
7224 register list, but just using a different register can't work in
7225 all cases, e.g.:
7226
7227 ldm r14!, {r0-r13,pc}
7228
7229 which would need to be rewritten as:
7230
7231 ldm rN!, {r0-r14}
7232
7233 but that can't work, because there's no free register for N.
7234
7235 Solve this by turning off the writeback bit, and emulating
7236 writeback manually in the cleanup routine. */
7237
7238 if (writeback)
7239 insn &= ~(1 << 21);
7240
7241 new_regmask = (1 << num_in_list) - 1;
7242
7243 if (debug_displaced)
7244 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
7245 "{..., pc}: original reg list %.4x, modified "
7246 "list %.4x\n"), rn, writeback ? "!" : "",
7247 (int) insn & 0xffff, new_regmask);
7248
7249 dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
7250
7251 dsc->cleanup = &cleanup_block_load_pc;
7252 }
7253 }
7254 else
7255 {
7256 /* STM of a list of registers which includes PC. Run the instruction
7257 as-is, but out of line: this will store the wrong value for the PC,
7258 so we must manually fix up the memory in the cleanup routine.
7259 Doing things this way has the advantage that we can auto-detect
7260 the offset of the PC write (which is architecture-dependent) in
7261 the cleanup routine. */
7262 dsc->modinsn[0] = insn;
7263
7264 dsc->cleanup = &cleanup_block_store_pc;
7265 }
7266
7267 return 0;
7268}
7269
34518530
YQ
7270static int
7271thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
7272 struct regcache *regs,
7273 struct displaced_step_closure *dsc)
cca44b1b 7274{
34518530
YQ
7275 int rn = bits (insn1, 0, 3);
7276 int load = bit (insn1, 4);
7277 int writeback = bit (insn1, 5);
cca44b1b 7278
34518530
YQ
7279 /* Block transfers which don't mention PC can be run directly
7280 out-of-line. */
7281 if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
7282 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);
7ff120b4 7283
34518530
YQ
7284 if (rn == ARM_PC_REGNUM)
7285 {
7286 warning (_("displaced: Unpredictable LDM or STM with "
7287 "base register r15"));
7288 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7289 "unpredictable ldm/stm", dsc);
7290 }
cca44b1b
JB
7291
7292 if (debug_displaced)
34518530
YQ
7293 fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
7294 "%.4x%.4x\n", insn1, insn2);
cca44b1b 7295
34518530
YQ
7296 /* Clear bit 13, since it should be always zero. */
7297 dsc->u.block.regmask = (insn2 & 0xdfff);
7298 dsc->u.block.rn = rn;
cca44b1b 7299
34518530
YQ
7300 dsc->u.block.load = load;
7301 dsc->u.block.user = 0;
7302 dsc->u.block.increment = bit (insn1, 7);
7303 dsc->u.block.before = bit (insn1, 8);
7304 dsc->u.block.writeback = writeback;
7305 dsc->u.block.cond = INST_AL;
7306 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b 7307
34518530
YQ
7308 if (load)
7309 {
7310 if (dsc->u.block.regmask == 0xffff)
7311 {
7312 /* This branch is impossible to happen. */
7313 gdb_assert (0);
7314 }
7315 else
7316 {
7317 unsigned int regmask = dsc->u.block.regmask;
7318 unsigned int num_in_list = bitcount (regmask), new_regmask, bit = 1;
7319 unsigned int to = 0, from = 0, i, new_rn;
7320
7321 for (i = 0; i < num_in_list; i++)
7322 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7323
7324 if (writeback)
7325 insn1 &= ~(1 << 5);
7326
7327 new_regmask = (1 << num_in_list) - 1;
7328
7329 if (debug_displaced)
7330 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
7331 "{..., pc}: original reg list %.4x, modified "
7332 "list %.4x\n"), rn, writeback ? "!" : "",
7333 (int) dsc->u.block.regmask, new_regmask);
7334
7335 dsc->modinsn[0] = insn1;
7336 dsc->modinsn[1] = (new_regmask & 0xffff);
7337 dsc->numinsns = 2;
7338
7339 dsc->cleanup = &cleanup_block_load_pc;
7340 }
7341 }
7342 else
7343 {
7344 dsc->modinsn[0] = insn1;
7345 dsc->modinsn[1] = insn2;
7346 dsc->numinsns = 2;
7347 dsc->cleanup = &cleanup_block_store_pc;
7348 }
7349 return 0;
7350}
7351
7352/* Cleanup/copy SVC (SWI) instructions. These two functions are overridden
7353 for Linux, where some SVC instructions must be treated specially. */
7354
7355static void
7356cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
7357 struct displaced_step_closure *dsc)
7358{
7359 CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
7360
7361 if (debug_displaced)
7362 fprintf_unfiltered (gdb_stdlog, "displaced: cleanup for svc, resume at "
7363 "%.8lx\n", (unsigned long) resume_addr);
7364
7365 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
7366}
7367
7368
7369/* Common copy routine for svc instruciton. */
7370
7371static int
7372install_svc (struct gdbarch *gdbarch, struct regcache *regs,
7373 struct displaced_step_closure *dsc)
7374{
7375 /* Preparation: none.
7376 Insn: unmodified svc.
7377 Cleanup: pc <- insn_addr + insn_size. */
7378
7379 /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
7380 instruction. */
7381 dsc->wrote_to_pc = 1;
7382
7383 /* Allow OS-specific code to override SVC handling. */
bd18283a
YQ
7384 if (dsc->u.svc.copy_svc_os)
7385 return dsc->u.svc.copy_svc_os (gdbarch, regs, dsc);
7386 else
7387 {
7388 dsc->cleanup = &cleanup_svc;
7389 return 0;
7390 }
34518530
YQ
7391}
7392
7393static int
7394arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
7395 struct regcache *regs, struct displaced_step_closure *dsc)
7396{
7397
7398 if (debug_displaced)
7399 fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.8lx\n",
7400 (unsigned long) insn);
7401
7402 dsc->modinsn[0] = insn;
7403
7404 return install_svc (gdbarch, regs, dsc);
7405}
7406
7407static int
7408thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
7409 struct regcache *regs, struct displaced_step_closure *dsc)
7410{
7411
7412 if (debug_displaced)
7413 fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.4x\n",
7414 insn);
bd18283a 7415
34518530
YQ
7416 dsc->modinsn[0] = insn;
7417
7418 return install_svc (gdbarch, regs, dsc);
cca44b1b
JB
7419}
7420
7421/* Copy undefined instructions. */
7422
7423static int
7ff120b4
YQ
7424arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
7425 struct displaced_step_closure *dsc)
cca44b1b
JB
7426{
7427 if (debug_displaced)
0963b4bd
MS
7428 fprintf_unfiltered (gdb_stdlog,
7429 "displaced: copying undefined insn %.8lx\n",
cca44b1b
JB
7430 (unsigned long) insn);
7431
7432 dsc->modinsn[0] = insn;
7433
7434 return 0;
7435}
7436
34518530
YQ
7437static int
7438thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
7439 struct displaced_step_closure *dsc)
7440{
7441
7442 if (debug_displaced)
7443 fprintf_unfiltered (gdb_stdlog, "displaced: copying undefined insn "
7444 "%.4x %.4x\n", (unsigned short) insn1,
7445 (unsigned short) insn2);
7446
7447 dsc->modinsn[0] = insn1;
7448 dsc->modinsn[1] = insn2;
7449 dsc->numinsns = 2;
7450
7451 return 0;
7452}
7453
cca44b1b
JB
7454/* Copy unpredictable instructions. */
7455
7456static int
7ff120b4
YQ
7457arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
7458 struct displaced_step_closure *dsc)
cca44b1b
JB
7459{
7460 if (debug_displaced)
7461 fprintf_unfiltered (gdb_stdlog, "displaced: copying unpredictable insn "
7462 "%.8lx\n", (unsigned long) insn);
7463
7464 dsc->modinsn[0] = insn;
7465
7466 return 0;
7467}
7468
7469/* The decode_* functions are instruction decoding helpers. They mostly follow
7470 the presentation in the ARM ARM. */
7471
7472static int
7ff120b4
YQ
7473arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
7474 struct regcache *regs,
7475 struct displaced_step_closure *dsc)
cca44b1b
JB
7476{
7477 unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
7478 unsigned int rn = bits (insn, 16, 19);
7479
7480 if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0xe) == 0x0)
7ff120b4 7481 return arm_copy_unmodified (gdbarch, insn, "cps", dsc);
cca44b1b 7482 else if (op1 == 0x10 && op2 == 0x0 && (rn & 0xe) == 0x1)
7ff120b4 7483 return arm_copy_unmodified (gdbarch, insn, "setend", dsc);
cca44b1b 7484 else if ((op1 & 0x60) == 0x20)
7ff120b4 7485 return arm_copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
cca44b1b 7486 else if ((op1 & 0x71) == 0x40)
7ff120b4
YQ
7487 return arm_copy_unmodified (gdbarch, insn, "neon elt/struct load/store",
7488 dsc);
cca44b1b 7489 else if ((op1 & 0x77) == 0x41)
7ff120b4 7490 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 7491 else if ((op1 & 0x77) == 0x45)
7ff120b4 7492 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pli. */
cca44b1b
JB
7493 else if ((op1 & 0x77) == 0x51)
7494 {
7495 if (rn != 0xf)
7ff120b4 7496 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b 7497 else
7ff120b4 7498 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7499 }
7500 else if ((op1 & 0x77) == 0x55)
7ff120b4 7501 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b
JB
7502 else if (op1 == 0x57)
7503 switch (op2)
7504 {
7ff120b4
YQ
7505 case 0x1: return arm_copy_unmodified (gdbarch, insn, "clrex", dsc);
7506 case 0x4: return arm_copy_unmodified (gdbarch, insn, "dsb", dsc);
7507 case 0x5: return arm_copy_unmodified (gdbarch, insn, "dmb", dsc);
7508 case 0x6: return arm_copy_unmodified (gdbarch, insn, "isb", dsc);
7509 default: return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7510 }
7511 else if ((op1 & 0x63) == 0x43)
7ff120b4 7512 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7513 else if ((op2 & 0x1) == 0x0)
7514 switch (op1 & ~0x80)
7515 {
7516 case 0x61:
7ff120b4 7517 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 7518 case 0x65:
7ff120b4 7519 return arm_copy_preload_reg (gdbarch, insn, regs, dsc); /* pli reg. */
cca44b1b
JB
7520 case 0x71: case 0x75:
7521 /* pld/pldw reg. */
7ff120b4 7522 return arm_copy_preload_reg (gdbarch, insn, regs, dsc);
cca44b1b 7523 case 0x63: case 0x67: case 0x73: case 0x77:
7ff120b4 7524 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b 7525 default:
7ff120b4 7526 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7527 }
7528 else
7ff120b4 7529 return arm_copy_undef (gdbarch, insn, dsc); /* Probably unreachable. */
cca44b1b
JB
7530}
7531
7532static int
7ff120b4
YQ
7533arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
7534 struct regcache *regs,
7535 struct displaced_step_closure *dsc)
cca44b1b
JB
7536{
7537 if (bit (insn, 27) == 0)
7ff120b4 7538 return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
cca44b1b
JB
7539 /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx. */
7540 else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
7541 {
7542 case 0x0: case 0x2:
7ff120b4 7543 return arm_copy_unmodified (gdbarch, insn, "srs", dsc);
cca44b1b
JB
7544
7545 case 0x1: case 0x3:
7ff120b4 7546 return arm_copy_unmodified (gdbarch, insn, "rfe", dsc);
cca44b1b
JB
7547
7548 case 0x4: case 0x5: case 0x6: case 0x7:
7ff120b4 7549 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b
JB
7550
7551 case 0x8:
7552 switch ((insn & 0xe00000) >> 21)
7553 {
7554 case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
7555 /* stc/stc2. */
7ff120b4 7556 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7557
7558 case 0x2:
7ff120b4 7559 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b
JB
7560
7561 default:
7ff120b4 7562 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7563 }
7564
7565 case 0x9:
7566 {
7567 int rn_f = (bits (insn, 16, 19) == 0xf);
7568 switch ((insn & 0xe00000) >> 21)
7569 {
7570 case 0x1: case 0x3:
7571 /* ldc/ldc2 imm (undefined for rn == pc). */
7ff120b4
YQ
7572 return rn_f ? arm_copy_undef (gdbarch, insn, dsc)
7573 : arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7574
7575 case 0x2:
7ff120b4 7576 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
7577
7578 case 0x4: case 0x5: case 0x6: case 0x7:
7579 /* ldc/ldc2 lit (undefined for rn != pc). */
7ff120b4
YQ
7580 return rn_f ? arm_copy_copro_load_store (gdbarch, insn, regs, dsc)
7581 : arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7582
7583 default:
7ff120b4 7584 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7585 }
7586 }
7587
7588 case 0xa:
7ff120b4 7589 return arm_copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
cca44b1b
JB
7590
7591 case 0xb:
7592 if (bits (insn, 16, 19) == 0xf)
7593 /* ldc/ldc2 lit. */
7ff120b4 7594 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 7595 else
7ff120b4 7596 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7597
7598 case 0xc:
7599 if (bit (insn, 4))
7ff120b4 7600 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 7601 else
7ff120b4 7602 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7603
7604 case 0xd:
7605 if (bit (insn, 4))
7ff120b4 7606 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 7607 else
7ff120b4 7608 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7609
7610 default:
7ff120b4 7611 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7612 }
7613}
7614
7615/* Decode miscellaneous instructions in dp/misc encoding space. */
7616
7617static int
7ff120b4
YQ
7618arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
7619 struct regcache *regs,
7620 struct displaced_step_closure *dsc)
cca44b1b
JB
7621{
7622 unsigned int op2 = bits (insn, 4, 6);
7623 unsigned int op = bits (insn, 21, 22);
7624 unsigned int op1 = bits (insn, 16, 19);
7625
7626 switch (op2)
7627 {
7628 case 0x0:
7ff120b4 7629 return arm_copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
cca44b1b
JB
7630
7631 case 0x1:
7632 if (op == 0x1) /* bx. */
7ff120b4 7633 return arm_copy_bx_blx_reg (gdbarch, insn, regs, dsc);
cca44b1b 7634 else if (op == 0x3)
7ff120b4 7635 return arm_copy_unmodified (gdbarch, insn, "clz", dsc);
cca44b1b 7636 else
7ff120b4 7637 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7638
7639 case 0x2:
7640 if (op == 0x1)
7641 /* Not really supported. */
7ff120b4 7642 return arm_copy_unmodified (gdbarch, insn, "bxj", dsc);
cca44b1b 7643 else
7ff120b4 7644 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7645
7646 case 0x3:
7647 if (op == 0x1)
7ff120b4 7648 return arm_copy_bx_blx_reg (gdbarch, insn,
0963b4bd 7649 regs, dsc); /* blx register. */
cca44b1b 7650 else
7ff120b4 7651 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7652
7653 case 0x5:
7ff120b4 7654 return arm_copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
cca44b1b
JB
7655
7656 case 0x7:
7657 if (op == 0x1)
7ff120b4 7658 return arm_copy_unmodified (gdbarch, insn, "bkpt", dsc);
cca44b1b
JB
7659 else if (op == 0x3)
7660 /* Not really supported. */
7ff120b4 7661 return arm_copy_unmodified (gdbarch, insn, "smc", dsc);
cca44b1b
JB
7662
7663 default:
7ff120b4 7664 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7665 }
7666}
7667
7668static int
7ff120b4
YQ
7669arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
7670 struct regcache *regs,
7671 struct displaced_step_closure *dsc)
cca44b1b
JB
7672{
7673 if (bit (insn, 25))
7674 switch (bits (insn, 20, 24))
7675 {
7676 case 0x10:
7ff120b4 7677 return arm_copy_unmodified (gdbarch, insn, "movw", dsc);
cca44b1b
JB
7678
7679 case 0x14:
7ff120b4 7680 return arm_copy_unmodified (gdbarch, insn, "movt", dsc);
cca44b1b
JB
7681
7682 case 0x12: case 0x16:
7ff120b4 7683 return arm_copy_unmodified (gdbarch, insn, "msr imm", dsc);
cca44b1b
JB
7684
7685 default:
7ff120b4 7686 return arm_copy_alu_imm (gdbarch, insn, regs, dsc);
cca44b1b
JB
7687 }
7688 else
7689 {
7690 uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
7691
7692 if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
7ff120b4 7693 return arm_copy_alu_reg (gdbarch, insn, regs, dsc);
cca44b1b 7694 else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
7ff120b4 7695 return arm_copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
cca44b1b 7696 else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
7ff120b4 7697 return arm_decode_miscellaneous (gdbarch, insn, regs, dsc);
cca44b1b 7698 else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
7ff120b4 7699 return arm_copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
cca44b1b 7700 else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
7ff120b4 7701 return arm_copy_unmodified (gdbarch, insn, "mul/mla", dsc);
cca44b1b 7702 else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
7ff120b4 7703 return arm_copy_unmodified (gdbarch, insn, "synch", dsc);
cca44b1b
JB
7704 else if (op2 == 0xb || (op2 & 0xd) == 0xd)
7705 /* 2nd arg means "unpriveleged". */
7ff120b4
YQ
7706 return arm_copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
7707 dsc);
cca44b1b
JB
7708 }
7709
7710 /* Should be unreachable. */
7711 return 1;
7712}
7713
7714static int
7ff120b4
YQ
7715arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
7716 struct regcache *regs,
7717 struct displaced_step_closure *dsc)
cca44b1b
JB
7718{
7719 int a = bit (insn, 25), b = bit (insn, 4);
7720 uint32_t op1 = bits (insn, 20, 24);
7721 int rn_f = bits (insn, 16, 19) == 0xf;
7722
7723 if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
7724 || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
0f6f04ba 7725 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 0);
cca44b1b
JB
7726 else if ((!a && (op1 & 0x17) == 0x02)
7727 || (a && (op1 & 0x17) == 0x02 && !b))
0f6f04ba 7728 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 1);
cca44b1b
JB
7729 else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
7730 || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
0f6f04ba 7731 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 0);
cca44b1b
JB
7732 else if ((!a && (op1 & 0x17) == 0x03)
7733 || (a && (op1 & 0x17) == 0x03 && !b))
0f6f04ba 7734 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 1);
cca44b1b
JB
7735 else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
7736 || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
7ff120b4 7737 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
cca44b1b
JB
7738 else if ((!a && (op1 & 0x17) == 0x06)
7739 || (a && (op1 & 0x17) == 0x06 && !b))
7ff120b4 7740 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
cca44b1b
JB
7741 else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
7742 || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
7ff120b4 7743 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
cca44b1b
JB
7744 else if ((!a && (op1 & 0x17) == 0x07)
7745 || (a && (op1 & 0x17) == 0x07 && !b))
7ff120b4 7746 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
cca44b1b
JB
7747
7748 /* Should be unreachable. */
7749 return 1;
7750}
7751
7752static int
7ff120b4
YQ
7753arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
7754 struct displaced_step_closure *dsc)
cca44b1b
JB
7755{
7756 switch (bits (insn, 20, 24))
7757 {
7758 case 0x00: case 0x01: case 0x02: case 0x03:
7ff120b4 7759 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
cca44b1b
JB
7760
7761 case 0x04: case 0x05: case 0x06: case 0x07:
7ff120b4 7762 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
cca44b1b
JB
7763
7764 case 0x08: case 0x09: case 0x0a: case 0x0b:
7765 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
7ff120b4 7766 return arm_copy_unmodified (gdbarch, insn,
cca44b1b
JB
7767 "decode/pack/unpack/saturate/reverse", dsc);
7768
7769 case 0x18:
7770 if (bits (insn, 5, 7) == 0) /* op2. */
7771 {
7772 if (bits (insn, 12, 15) == 0xf)
7ff120b4 7773 return arm_copy_unmodified (gdbarch, insn, "usad8", dsc);
cca44b1b 7774 else
7ff120b4 7775 return arm_copy_unmodified (gdbarch, insn, "usada8", dsc);
cca44b1b
JB
7776 }
7777 else
7ff120b4 7778 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7779
7780 case 0x1a: case 0x1b:
7781 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 7782 return arm_copy_unmodified (gdbarch, insn, "sbfx", dsc);
cca44b1b 7783 else
7ff120b4 7784 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7785
7786 case 0x1c: case 0x1d:
7787 if (bits (insn, 5, 6) == 0x0) /* op2[1:0]. */
7788 {
7789 if (bits (insn, 0, 3) == 0xf)
7ff120b4 7790 return arm_copy_unmodified (gdbarch, insn, "bfc", dsc);
cca44b1b 7791 else
7ff120b4 7792 return arm_copy_unmodified (gdbarch, insn, "bfi", dsc);
cca44b1b
JB
7793 }
7794 else
7ff120b4 7795 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7796
7797 case 0x1e: case 0x1f:
7798 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 7799 return arm_copy_unmodified (gdbarch, insn, "ubfx", dsc);
cca44b1b 7800 else
7ff120b4 7801 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7802 }
7803
7804 /* Should be unreachable. */
7805 return 1;
7806}
7807
7808static int
7ff120b4
YQ
7809arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, int32_t insn,
7810 struct regcache *regs,
7811 struct displaced_step_closure *dsc)
cca44b1b
JB
7812{
7813 if (bit (insn, 25))
7ff120b4 7814 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b 7815 else
7ff120b4 7816 return arm_copy_block_xfer (gdbarch, insn, regs, dsc);
cca44b1b
JB
7817}
7818
7819static int
7ff120b4
YQ
7820arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
7821 struct regcache *regs,
7822 struct displaced_step_closure *dsc)
cca44b1b
JB
7823{
7824 unsigned int opcode = bits (insn, 20, 24);
7825
7826 switch (opcode)
7827 {
7828 case 0x04: case 0x05: /* VFP/Neon mrrc/mcrr. */
7ff120b4 7829 return arm_copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
cca44b1b
JB
7830
7831 case 0x08: case 0x0a: case 0x0c: case 0x0e:
7832 case 0x12: case 0x16:
7ff120b4 7833 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
cca44b1b
JB
7834
7835 case 0x09: case 0x0b: case 0x0d: case 0x0f:
7836 case 0x13: case 0x17:
7ff120b4 7837 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
cca44b1b
JB
7838
7839 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7840 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7841 /* Note: no writeback for these instructions. Bit 25 will always be
7842 zero though (via caller), so the following works OK. */
7ff120b4 7843 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7844 }
7845
7846 /* Should be unreachable. */
7847 return 1;
7848}
7849
34518530
YQ
7850/* Decode shifted register instructions. */
7851
7852static int
7853thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
7854 uint16_t insn2, struct regcache *regs,
7855 struct displaced_step_closure *dsc)
7856{
7857 /* PC is only allowed to be used in instruction MOV. */
7858
7859 unsigned int op = bits (insn1, 5, 8);
7860 unsigned int rn = bits (insn1, 0, 3);
7861
7862 if (op == 0x2 && rn == 0xf) /* MOV */
7863 return thumb2_copy_alu_imm (gdbarch, insn1, insn2, regs, dsc);
7864 else
7865 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7866 "dp (shift reg)", dsc);
7867}
7868
7869
7870/* Decode extension register load/store. Exactly the same as
7871 arm_decode_ext_reg_ld_st. */
7872
7873static int
7874thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
7875 uint16_t insn2, struct regcache *regs,
7876 struct displaced_step_closure *dsc)
7877{
7878 unsigned int opcode = bits (insn1, 4, 8);
7879
7880 switch (opcode)
7881 {
7882 case 0x04: case 0x05:
7883 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7884 "vfp/neon vmov", dsc);
7885
7886 case 0x08: case 0x0c: /* 01x00 */
7887 case 0x0a: case 0x0e: /* 01x10 */
7888 case 0x12: case 0x16: /* 10x10 */
7889 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7890 "vfp/neon vstm/vpush", dsc);
7891
7892 case 0x09: case 0x0d: /* 01x01 */
7893 case 0x0b: case 0x0f: /* 01x11 */
7894 case 0x13: case 0x17: /* 10x11 */
7895 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7896 "vfp/neon vldm/vpop", dsc);
7897
7898 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7899 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7900 "vstr", dsc);
7901 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7902 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2, regs, dsc);
7903 }
7904
7905 /* Should be unreachable. */
7906 return 1;
7907}
7908
cca44b1b 7909static int
7ff120b4
YQ
7910arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn, CORE_ADDR to,
7911 struct regcache *regs, struct displaced_step_closure *dsc)
cca44b1b
JB
7912{
7913 unsigned int op1 = bits (insn, 20, 25);
7914 int op = bit (insn, 4);
7915 unsigned int coproc = bits (insn, 8, 11);
7916 unsigned int rn = bits (insn, 16, 19);
7917
7918 if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
7ff120b4 7919 return arm_decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
cca44b1b
JB
7920 else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
7921 && (coproc & 0xe) != 0xa)
7922 /* stc/stc2. */
7ff120b4 7923 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7924 else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
7925 && (coproc & 0xe) != 0xa)
7926 /* ldc/ldc2 imm/lit. */
7ff120b4 7927 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 7928 else if ((op1 & 0x3e) == 0x00)
7ff120b4 7929 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b 7930 else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
7ff120b4 7931 return arm_copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
cca44b1b 7932 else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
7ff120b4 7933 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b 7934 else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
7ff120b4 7935 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
7936 else if ((op1 & 0x30) == 0x20 && !op)
7937 {
7938 if ((coproc & 0xe) == 0xa)
7ff120b4 7939 return arm_copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
cca44b1b 7940 else
7ff120b4 7941 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7942 }
7943 else if ((op1 & 0x30) == 0x20 && op)
7ff120b4 7944 return arm_copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
cca44b1b 7945 else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
7ff120b4 7946 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 7947 else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
7ff120b4 7948 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 7949 else if ((op1 & 0x30) == 0x30)
7ff120b4 7950 return arm_copy_svc (gdbarch, insn, regs, dsc);
cca44b1b 7951 else
7ff120b4 7952 return arm_copy_undef (gdbarch, insn, dsc); /* Possibly unreachable. */
cca44b1b
JB
7953}
7954
34518530
YQ
7955static int
7956thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
7957 uint16_t insn2, struct regcache *regs,
7958 struct displaced_step_closure *dsc)
7959{
7960 unsigned int coproc = bits (insn2, 8, 11);
7961 unsigned int op1 = bits (insn1, 4, 9);
7962 unsigned int bit_5_8 = bits (insn1, 5, 8);
7963 unsigned int bit_9 = bit (insn1, 9);
7964 unsigned int bit_4 = bit (insn1, 4);
7965 unsigned int rn = bits (insn1, 0, 3);
7966
7967 if (bit_9 == 0)
7968 {
7969 if (bit_5_8 == 2)
7970 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7971 "neon 64bit xfer/mrrc/mrrc2/mcrr/mcrr2",
7972 dsc);
7973 else if (bit_5_8 == 0) /* UNDEFINED. */
7974 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
7975 else
7976 {
7977 /*coproc is 101x. SIMD/VFP, ext registers load/store. */
7978 if ((coproc & 0xe) == 0xa)
7979 return thumb2_decode_ext_reg_ld_st (gdbarch, insn1, insn2, regs,
7980 dsc);
7981 else /* coproc is not 101x. */
7982 {
7983 if (bit_4 == 0) /* STC/STC2. */
7984 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7985 "stc/stc2", dsc);
7986 else /* LDC/LDC2 {literal, immeidate}. */
7987 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2,
7988 regs, dsc);
7989 }
7990 }
7991 }
7992 else
7993 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "coproc", dsc);
7994
7995 return 0;
7996}
7997
7998static void
7999install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
8000 struct displaced_step_closure *dsc, int rd)
8001{
8002 /* ADR Rd, #imm
8003
8004 Rewrite as:
8005
8006 Preparation: Rd <- PC
8007 Insn: ADD Rd, #imm
8008 Cleanup: Null.
8009 */
8010
8011 /* Rd <- PC */
8012 int val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
8013 displaced_write_reg (regs, dsc, rd, val, CANNOT_WRITE_PC);
8014}
8015
8016static int
8017thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
8018 struct displaced_step_closure *dsc,
8019 int rd, unsigned int imm)
8020{
8021
8022 /* Encoding T2: ADDS Rd, #imm */
8023 dsc->modinsn[0] = (0x3000 | (rd << 8) | imm);
8024
8025 install_pc_relative (gdbarch, regs, dsc, rd);
8026
8027 return 0;
8028}
8029
8030static int
8031thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
8032 struct regcache *regs,
8033 struct displaced_step_closure *dsc)
8034{
8035 unsigned int rd = bits (insn, 8, 10);
8036 unsigned int imm8 = bits (insn, 0, 7);
8037
8038 if (debug_displaced)
8039 fprintf_unfiltered (gdb_stdlog,
8040 "displaced: copying thumb adr r%d, #%d insn %.4x\n",
8041 rd, imm8, insn);
8042
8043 return thumb_copy_pc_relative_16bit (gdbarch, regs, dsc, rd, imm8);
8044}
8045
8046static int
8047thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
8048 uint16_t insn2, struct regcache *regs,
8049 struct displaced_step_closure *dsc)
8050{
8051 unsigned int rd = bits (insn2, 8, 11);
8052 /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
8053 extract raw immediate encoding rather than computing immediate. When
8054 generating ADD or SUB instruction, we can simply perform OR operation to
8055 set immediate into ADD. */
8056 unsigned int imm_3_8 = insn2 & 0x70ff;
8057 unsigned int imm_i = insn1 & 0x0400; /* Clear all bits except bit 10. */
8058
8059 if (debug_displaced)
8060 fprintf_unfiltered (gdb_stdlog,
8061 "displaced: copying thumb adr r%d, #%d:%d insn %.4x%.4x\n",
8062 rd, imm_i, imm_3_8, insn1, insn2);
8063
8064 if (bit (insn1, 7)) /* Encoding T2 */
8065 {
8066 /* Encoding T3: SUB Rd, Rd, #imm */
8067 dsc->modinsn[0] = (0xf1a0 | rd | imm_i);
8068 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
8069 }
8070 else /* Encoding T3 */
8071 {
8072 /* Encoding T3: ADD Rd, Rd, #imm */
8073 dsc->modinsn[0] = (0xf100 | rd | imm_i);
8074 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
8075 }
8076 dsc->numinsns = 2;
8077
8078 install_pc_relative (gdbarch, regs, dsc, rd);
8079
8080 return 0;
8081}
8082
8083static int
8084thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, unsigned short insn1,
8085 struct regcache *regs,
8086 struct displaced_step_closure *dsc)
8087{
8088 unsigned int rt = bits (insn1, 8, 10);
8089 unsigned int pc;
8090 int imm8 = (bits (insn1, 0, 7) << 2);
8091 CORE_ADDR from = dsc->insn_addr;
8092
8093 /* LDR Rd, #imm8
8094
8095 Rwrite as:
8096
8097 Preparation: tmp0 <- R0, tmp2 <- R2, tmp3 <- R3, R2 <- PC, R3 <- #imm8;
8098
8099 Insn: LDR R0, [R2, R3];
8100 Cleanup: R2 <- tmp2, R3 <- tmp3, Rd <- R0, R0 <- tmp0 */
8101
8102 if (debug_displaced)
8103 fprintf_unfiltered (gdb_stdlog,
8104 "displaced: copying thumb ldr r%d [pc #%d]\n"
8105 , rt, imm8);
8106
8107 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
8108 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
8109 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
8110 pc = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
8111 /* The assembler calculates the required value of the offset from the
8112 Align(PC,4) value of this instruction to the label. */
8113 pc = pc & 0xfffffffc;
8114
8115 displaced_write_reg (regs, dsc, 2, pc, CANNOT_WRITE_PC);
8116 displaced_write_reg (regs, dsc, 3, imm8, CANNOT_WRITE_PC);
8117
8118 dsc->rd = rt;
8119 dsc->u.ldst.xfersize = 4;
8120 dsc->u.ldst.rn = 0;
8121 dsc->u.ldst.immed = 0;
8122 dsc->u.ldst.writeback = 0;
8123 dsc->u.ldst.restore_r4 = 0;
8124
8125 dsc->modinsn[0] = 0x58d0; /* ldr r0, [r2, r3]*/
8126
8127 dsc->cleanup = &cleanup_load;
8128
8129 return 0;
8130}
8131
8132/* Copy Thumb cbnz/cbz insruction. */
8133
8134static int
8135thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
8136 struct regcache *regs,
8137 struct displaced_step_closure *dsc)
8138{
8139 int non_zero = bit (insn1, 11);
8140 unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
8141 CORE_ADDR from = dsc->insn_addr;
8142 int rn = bits (insn1, 0, 2);
8143 int rn_val = displaced_read_reg (regs, dsc, rn);
8144
8145 dsc->u.branch.cond = (rn_val && non_zero) || (!rn_val && !non_zero);
8146 /* CBNZ and CBZ do not affect the condition flags. If condition is true,
8147 set it INST_AL, so cleanup_branch will know branch is taken, otherwise,
8148 condition is false, let it be, cleanup_branch will do nothing. */
8149 if (dsc->u.branch.cond)
8150 {
8151 dsc->u.branch.cond = INST_AL;
8152 dsc->u.branch.dest = from + 4 + imm5;
8153 }
8154 else
8155 dsc->u.branch.dest = from + 2;
8156
8157 dsc->u.branch.link = 0;
8158 dsc->u.branch.exchange = 0;
8159
8160 if (debug_displaced)
8161 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s [r%d = 0x%x]"
8162 " insn %.4x to %.8lx\n", non_zero ? "cbnz" : "cbz",
8163 rn, rn_val, insn1, dsc->u.branch.dest);
8164
8165 dsc->modinsn[0] = THUMB_NOP;
8166
8167 dsc->cleanup = &cleanup_branch;
8168 return 0;
8169}
8170
8171/* Copy Table Branch Byte/Halfword */
8172static int
8173thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
8174 uint16_t insn2, struct regcache *regs,
8175 struct displaced_step_closure *dsc)
8176{
8177 ULONGEST rn_val, rm_val;
8178 int is_tbh = bit (insn2, 4);
8179 CORE_ADDR halfwords = 0;
8180 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8181
8182 rn_val = displaced_read_reg (regs, dsc, bits (insn1, 0, 3));
8183 rm_val = displaced_read_reg (regs, dsc, bits (insn2, 0, 3));
8184
8185 if (is_tbh)
8186 {
8187 gdb_byte buf[2];
8188
8189 target_read_memory (rn_val + 2 * rm_val, buf, 2);
8190 halfwords = extract_unsigned_integer (buf, 2, byte_order);
8191 }
8192 else
8193 {
8194 gdb_byte buf[1];
8195
8196 target_read_memory (rn_val + rm_val, buf, 1);
8197 halfwords = extract_unsigned_integer (buf, 1, byte_order);
8198 }
8199
8200 if (debug_displaced)
8201 fprintf_unfiltered (gdb_stdlog, "displaced: %s base 0x%x offset 0x%x"
8202 " offset 0x%x\n", is_tbh ? "tbh" : "tbb",
8203 (unsigned int) rn_val, (unsigned int) rm_val,
8204 (unsigned int) halfwords);
8205
8206 dsc->u.branch.cond = INST_AL;
8207 dsc->u.branch.link = 0;
8208 dsc->u.branch.exchange = 0;
8209 dsc->u.branch.dest = dsc->insn_addr + 4 + 2 * halfwords;
8210
8211 dsc->cleanup = &cleanup_branch;
8212
8213 return 0;
8214}
8215
8216static void
8217cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
8218 struct displaced_step_closure *dsc)
8219{
8220 /* PC <- r7 */
8221 int val = displaced_read_reg (regs, dsc, 7);
8222 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, val, BX_WRITE_PC);
8223
8224 /* r7 <- r8 */
8225 val = displaced_read_reg (regs, dsc, 8);
8226 displaced_write_reg (regs, dsc, 7, val, CANNOT_WRITE_PC);
8227
8228 /* r8 <- tmp[0] */
8229 displaced_write_reg (regs, dsc, 8, dsc->tmp[0], CANNOT_WRITE_PC);
8230
8231}
8232
8233static int
8234thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, unsigned short insn1,
8235 struct regcache *regs,
8236 struct displaced_step_closure *dsc)
8237{
8238 dsc->u.block.regmask = insn1 & 0x00ff;
8239
8240 /* Rewrite instruction: POP {rX, rY, ...,rZ, PC}
8241 to :
8242
8243 (1) register list is full, that is, r0-r7 are used.
8244 Prepare: tmp[0] <- r8
8245
8246 POP {r0, r1, ...., r6, r7}; remove PC from reglist
8247 MOV r8, r7; Move value of r7 to r8;
8248 POP {r7}; Store PC value into r7.
8249
8250 Cleanup: PC <- r7, r7 <- r8, r8 <-tmp[0]
8251
8252 (2) register list is not full, supposing there are N registers in
8253 register list (except PC, 0 <= N <= 7).
8254 Prepare: for each i, 0 - N, tmp[i] <- ri.
8255
8256 POP {r0, r1, ...., rN};
8257
8258 Cleanup: Set registers in original reglist from r0 - rN. Restore r0 - rN
8259 from tmp[] properly.
8260 */
8261 if (debug_displaced)
8262 fprintf_unfiltered (gdb_stdlog,
8263 "displaced: copying thumb pop {%.8x, pc} insn %.4x\n",
8264 dsc->u.block.regmask, insn1);
8265
8266 if (dsc->u.block.regmask == 0xff)
8267 {
8268 dsc->tmp[0] = displaced_read_reg (regs, dsc, 8);
8269
8270 dsc->modinsn[0] = (insn1 & 0xfeff); /* POP {r0,r1,...,r6, r7} */
8271 dsc->modinsn[1] = 0x46b8; /* MOV r8, r7 */
8272 dsc->modinsn[2] = 0xbc80; /* POP {r7} */
8273
8274 dsc->numinsns = 3;
8275 dsc->cleanup = &cleanup_pop_pc_16bit_all;
8276 }
8277 else
8278 {
8279 unsigned int num_in_list = bitcount (dsc->u.block.regmask);
8280 unsigned int new_regmask, bit = 1;
8281 unsigned int to = 0, from = 0, i, new_rn;
8282
8283 for (i = 0; i < num_in_list + 1; i++)
8284 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
8285
8286 new_regmask = (1 << (num_in_list + 1)) - 1;
8287
8288 if (debug_displaced)
8289 fprintf_unfiltered (gdb_stdlog, _("displaced: POP "
8290 "{..., pc}: original reg list %.4x,"
8291 " modified list %.4x\n"),
8292 (int) dsc->u.block.regmask, new_regmask);
8293
8294 dsc->u.block.regmask |= 0x8000;
8295 dsc->u.block.writeback = 0;
8296 dsc->u.block.cond = INST_AL;
8297
8298 dsc->modinsn[0] = (insn1 & ~0x1ff) | (new_regmask & 0xff);
8299
8300 dsc->cleanup = &cleanup_block_load_pc;
8301 }
8302
8303 return 0;
8304}
8305
8306static void
8307thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8308 struct regcache *regs,
8309 struct displaced_step_closure *dsc)
8310{
8311 unsigned short op_bit_12_15 = bits (insn1, 12, 15);
8312 unsigned short op_bit_10_11 = bits (insn1, 10, 11);
8313 int err = 0;
8314
8315 /* 16-bit thumb instructions. */
8316 switch (op_bit_12_15)
8317 {
8318 /* Shift (imme), add, subtract, move and compare. */
8319 case 0: case 1: case 2: case 3:
8320 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8321 "shift/add/sub/mov/cmp",
8322 dsc);
8323 break;
8324 case 4:
8325 switch (op_bit_10_11)
8326 {
8327 case 0: /* Data-processing */
8328 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8329 "data-processing",
8330 dsc);
8331 break;
8332 case 1: /* Special data instructions and branch and exchange. */
8333 {
8334 unsigned short op = bits (insn1, 7, 9);
8335 if (op == 6 || op == 7) /* BX or BLX */
8336 err = thumb_copy_bx_blx_reg (gdbarch, insn1, regs, dsc);
8337 else if (bits (insn1, 6, 7) != 0) /* ADD/MOV/CMP high registers. */
8338 err = thumb_copy_alu_reg (gdbarch, insn1, regs, dsc);
8339 else
8340 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "special data",
8341 dsc);
8342 }
8343 break;
8344 default: /* LDR (literal) */
8345 err = thumb_copy_16bit_ldr_literal (gdbarch, insn1, regs, dsc);
8346 }
8347 break;
8348 case 5: case 6: case 7: case 8: case 9: /* Load/Store single data item */
8349 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldr/str", dsc);
8350 break;
8351 case 10:
8352 if (op_bit_10_11 < 2) /* Generate PC-relative address */
8353 err = thumb_decode_pc_relative_16bit (gdbarch, insn1, regs, dsc);
8354 else /* Generate SP-relative address */
8355 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "sp-relative", dsc);
8356 break;
8357 case 11: /* Misc 16-bit instructions */
8358 {
8359 switch (bits (insn1, 8, 11))
8360 {
8361 case 1: case 3: case 9: case 11: /* CBNZ, CBZ */
8362 err = thumb_copy_cbnz_cbz (gdbarch, insn1, regs, dsc);
8363 break;
8364 case 12: case 13: /* POP */
8365 if (bit (insn1, 8)) /* PC is in register list. */
8366 err = thumb_copy_pop_pc_16bit (gdbarch, insn1, regs, dsc);
8367 else
8368 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "pop", dsc);
8369 break;
8370 case 15: /* If-Then, and hints */
8371 if (bits (insn1, 0, 3))
8372 /* If-Then makes up to four following instructions conditional.
8373 IT instruction itself is not conditional, so handle it as a
8374 common unmodified instruction. */
8375 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "If-Then",
8376 dsc);
8377 else
8378 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "hints", dsc);
8379 break;
8380 default:
8381 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "misc", dsc);
8382 }
8383 }
8384 break;
8385 case 12:
8386 if (op_bit_10_11 < 2) /* Store multiple registers */
8387 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "stm", dsc);
8388 else /* Load multiple registers */
8389 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldm", dsc);
8390 break;
8391 case 13: /* Conditional branch and supervisor call */
8392 if (bits (insn1, 9, 11) != 7) /* conditional branch */
8393 err = thumb_copy_b (gdbarch, insn1, dsc);
8394 else
8395 err = thumb_copy_svc (gdbarch, insn1, regs, dsc);
8396 break;
8397 case 14: /* Unconditional branch */
8398 err = thumb_copy_b (gdbarch, insn1, dsc);
8399 break;
8400 default:
8401 err = 1;
8402 }
8403
8404 if (err)
8405 internal_error (__FILE__, __LINE__,
8406 _("thumb_process_displaced_16bit_insn: Instruction decode error"));
8407}
8408
8409static int
8410decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
8411 uint16_t insn1, uint16_t insn2,
8412 struct regcache *regs,
8413 struct displaced_step_closure *dsc)
8414{
8415 int rt = bits (insn2, 12, 15);
8416 int rn = bits (insn1, 0, 3);
8417 int op1 = bits (insn1, 7, 8);
8418 int err = 0;
8419
8420 switch (bits (insn1, 5, 6))
8421 {
8422 case 0: /* Load byte and memory hints */
8423 if (rt == 0xf) /* PLD/PLI */
8424 {
8425 if (rn == 0xf)
8426 /* PLD literal or Encoding T3 of PLI(immediate, literal). */
8427 return thumb2_copy_preload (gdbarch, insn1, insn2, regs, dsc);
8428 else
8429 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8430 "pli/pld", dsc);
8431 }
8432 else
8433 {
8434 if (rn == 0xf) /* LDRB/LDRSB (literal) */
8435 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8436 1);
8437 else
8438 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8439 "ldrb{reg, immediate}/ldrbt",
8440 dsc);
8441 }
8442
8443 break;
8444 case 1: /* Load halfword and memory hints. */
8445 if (rt == 0xf) /* PLD{W} and Unalloc memory hint. */
8446 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8447 "pld/unalloc memhint", dsc);
8448 else
8449 {
8450 if (rn == 0xf)
8451 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8452 2);
8453 else
8454 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8455 "ldrh/ldrht", dsc);
8456 }
8457 break;
8458 case 2: /* Load word */
8459 {
8460 int insn2_bit_8_11 = bits (insn2, 8, 11);
8461
8462 if (rn == 0xf)
8463 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc, 4);
8464 else if (op1 == 0x1) /* Encoding T3 */
8465 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs, dsc,
8466 0, 1);
8467 else /* op1 == 0x0 */
8468 {
8469 if (insn2_bit_8_11 == 0xc || (insn2_bit_8_11 & 0x9) == 0x9)
8470 /* LDR (immediate) */
8471 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8472 dsc, bit (insn2, 8), 1);
8473 else if (insn2_bit_8_11 == 0xe) /* LDRT */
8474 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8475 "ldrt", dsc);
8476 else
8477 /* LDR (register) */
8478 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8479 dsc, 0, 0);
8480 }
8481 break;
8482 }
8483 default:
8484 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
8485 break;
8486 }
8487 return 0;
8488}
8489
8490static void
8491thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8492 uint16_t insn2, struct regcache *regs,
8493 struct displaced_step_closure *dsc)
8494{
8495 int err = 0;
8496 unsigned short op = bit (insn2, 15);
8497 unsigned int op1 = bits (insn1, 11, 12);
8498
8499 switch (op1)
8500 {
8501 case 1:
8502 {
8503 switch (bits (insn1, 9, 10))
8504 {
8505 case 0:
8506 if (bit (insn1, 6))
8507 {
8508 /* Load/store {dual, execlusive}, table branch. */
8509 if (bits (insn1, 7, 8) == 1 && bits (insn1, 4, 5) == 1
8510 && bits (insn2, 5, 7) == 0)
8511 err = thumb2_copy_table_branch (gdbarch, insn1, insn2, regs,
8512 dsc);
8513 else
8514 /* PC is not allowed to use in load/store {dual, exclusive}
8515 instructions. */
8516 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8517 "load/store dual/ex", dsc);
8518 }
8519 else /* load/store multiple */
8520 {
8521 switch (bits (insn1, 7, 8))
8522 {
8523 case 0: case 3: /* SRS, RFE */
8524 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8525 "srs/rfe", dsc);
8526 break;
8527 case 1: case 2: /* LDM/STM/PUSH/POP */
8528 err = thumb2_copy_block_xfer (gdbarch, insn1, insn2, regs, dsc);
8529 break;
8530 }
8531 }
8532 break;
8533
8534 case 1:
8535 /* Data-processing (shift register). */
8536 err = thumb2_decode_dp_shift_reg (gdbarch, insn1, insn2, regs,
8537 dsc);
8538 break;
8539 default: /* Coprocessor instructions. */
8540 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8541 break;
8542 }
8543 break;
8544 }
8545 case 2: /* op1 = 2 */
8546 if (op) /* Branch and misc control. */
8547 {
8548 if (bit (insn2, 14) /* BLX/BL */
8549 || bit (insn2, 12) /* Unconditional branch */
8550 || (bits (insn1, 7, 9) != 0x7)) /* Conditional branch */
8551 err = thumb2_copy_b_bl_blx (gdbarch, insn1, insn2, regs, dsc);
8552 else
8553 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8554 "misc ctrl", dsc);
8555 }
8556 else
8557 {
8558 if (bit (insn1, 9)) /* Data processing (plain binary imm). */
8559 {
8560 int op = bits (insn1, 4, 8);
8561 int rn = bits (insn1, 0, 3);
8562 if ((op == 0 || op == 0xa) && rn == 0xf)
8563 err = thumb_copy_pc_relative_32bit (gdbarch, insn1, insn2,
8564 regs, dsc);
8565 else
8566 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8567 "dp/pb", dsc);
8568 }
8569 else /* Data processing (modified immeidate) */
8570 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8571 "dp/mi", dsc);
8572 }
8573 break;
8574 case 3: /* op1 = 3 */
8575 switch (bits (insn1, 9, 10))
8576 {
8577 case 0:
8578 if (bit (insn1, 4))
8579 err = decode_thumb_32bit_ld_mem_hints (gdbarch, insn1, insn2,
8580 regs, dsc);
8581 else /* NEON Load/Store and Store single data item */
8582 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8583 "neon elt/struct load/store",
8584 dsc);
8585 break;
8586 case 1: /* op1 = 3, bits (9, 10) == 1 */
8587 switch (bits (insn1, 7, 8))
8588 {
8589 case 0: case 1: /* Data processing (register) */
8590 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8591 "dp(reg)", dsc);
8592 break;
8593 case 2: /* Multiply and absolute difference */
8594 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8595 "mul/mua/diff", dsc);
8596 break;
8597 case 3: /* Long multiply and divide */
8598 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8599 "lmul/lmua", dsc);
8600 break;
8601 }
8602 break;
8603 default: /* Coprocessor instructions */
8604 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8605 break;
8606 }
8607 break;
8608 default:
8609 err = 1;
8610 }
8611
8612 if (err)
8613 internal_error (__FILE__, __LINE__,
8614 _("thumb_process_displaced_32bit_insn: Instruction decode error"));
8615
8616}
8617
b434a28f
YQ
8618static void
8619thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8620 CORE_ADDR to, struct regcache *regs,
8621 struct displaced_step_closure *dsc)
8622{
34518530
YQ
8623 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8624 uint16_t insn1
8625 = read_memory_unsigned_integer (from, 2, byte_order_for_code);
8626
8627 if (debug_displaced)
8628 fprintf_unfiltered (gdb_stdlog, "displaced: process thumb insn %.4x "
8629 "at %.8lx\n", insn1, (unsigned long) from);
8630
8631 dsc->is_thumb = 1;
8632 dsc->insn_size = thumb_insn_size (insn1);
8633 if (thumb_insn_size (insn1) == 4)
8634 {
8635 uint16_t insn2
8636 = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
8637 thumb_process_displaced_32bit_insn (gdbarch, insn1, insn2, regs, dsc);
8638 }
8639 else
8640 thumb_process_displaced_16bit_insn (gdbarch, insn1, regs, dsc);
b434a28f
YQ
8641}
8642
cca44b1b 8643void
b434a28f
YQ
8644arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8645 CORE_ADDR to, struct regcache *regs,
cca44b1b
JB
8646 struct displaced_step_closure *dsc)
8647{
8648 int err = 0;
b434a28f
YQ
8649 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8650 uint32_t insn;
cca44b1b
JB
8651
8652 /* Most displaced instructions use a 1-instruction scratch space, so set this
8653 here and override below if/when necessary. */
8654 dsc->numinsns = 1;
8655 dsc->insn_addr = from;
8656 dsc->scratch_base = to;
8657 dsc->cleanup = NULL;
8658 dsc->wrote_to_pc = 0;
8659
b434a28f
YQ
8660 if (!displaced_in_arm_mode (regs))
8661 return thumb_process_displaced_insn (gdbarch, from, to, regs, dsc);
8662
4db71c0b
YQ
8663 dsc->is_thumb = 0;
8664 dsc->insn_size = 4;
b434a28f
YQ
8665 insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
8666 if (debug_displaced)
8667 fprintf_unfiltered (gdb_stdlog, "displaced: stepping insn %.8lx "
8668 "at %.8lx\n", (unsigned long) insn,
8669 (unsigned long) from);
8670
cca44b1b 8671 if ((insn & 0xf0000000) == 0xf0000000)
7ff120b4 8672 err = arm_decode_unconditional (gdbarch, insn, regs, dsc);
cca44b1b
JB
8673 else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
8674 {
8675 case 0x0: case 0x1: case 0x2: case 0x3:
7ff120b4 8676 err = arm_decode_dp_misc (gdbarch, insn, regs, dsc);
cca44b1b
JB
8677 break;
8678
8679 case 0x4: case 0x5: case 0x6:
7ff120b4 8680 err = arm_decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
cca44b1b
JB
8681 break;
8682
8683 case 0x7:
7ff120b4 8684 err = arm_decode_media (gdbarch, insn, dsc);
cca44b1b
JB
8685 break;
8686
8687 case 0x8: case 0x9: case 0xa: case 0xb:
7ff120b4 8688 err = arm_decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
cca44b1b
JB
8689 break;
8690
8691 case 0xc: case 0xd: case 0xe: case 0xf:
7ff120b4 8692 err = arm_decode_svc_copro (gdbarch, insn, to, regs, dsc);
cca44b1b
JB
8693 break;
8694 }
8695
8696 if (err)
8697 internal_error (__FILE__, __LINE__,
8698 _("arm_process_displaced_insn: Instruction decode error"));
8699}
8700
8701/* Actually set up the scratch space for a displaced instruction. */
8702
8703void
8704arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
8705 CORE_ADDR to, struct displaced_step_closure *dsc)
8706{
8707 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4db71c0b 8708 unsigned int i, len, offset;
cca44b1b 8709 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4db71c0b 8710 int size = dsc->is_thumb? 2 : 4;
948f8e3d 8711 const gdb_byte *bkp_insn;
cca44b1b 8712
4db71c0b 8713 offset = 0;
cca44b1b
JB
8714 /* Poke modified instruction(s). */
8715 for (i = 0; i < dsc->numinsns; i++)
8716 {
8717 if (debug_displaced)
4db71c0b
YQ
8718 {
8719 fprintf_unfiltered (gdb_stdlog, "displaced: writing insn ");
8720 if (size == 4)
8721 fprintf_unfiltered (gdb_stdlog, "%.8lx",
8722 dsc->modinsn[i]);
8723 else if (size == 2)
8724 fprintf_unfiltered (gdb_stdlog, "%.4x",
8725 (unsigned short)dsc->modinsn[i]);
8726
8727 fprintf_unfiltered (gdb_stdlog, " at %.8lx\n",
8728 (unsigned long) to + offset);
8729
8730 }
8731 write_memory_unsigned_integer (to + offset, size,
8732 byte_order_for_code,
cca44b1b 8733 dsc->modinsn[i]);
4db71c0b
YQ
8734 offset += size;
8735 }
8736
8737 /* Choose the correct breakpoint instruction. */
8738 if (dsc->is_thumb)
8739 {
8740 bkp_insn = tdep->thumb_breakpoint;
8741 len = tdep->thumb_breakpoint_size;
8742 }
8743 else
8744 {
8745 bkp_insn = tdep->arm_breakpoint;
8746 len = tdep->arm_breakpoint_size;
cca44b1b
JB
8747 }
8748
8749 /* Put breakpoint afterwards. */
4db71c0b 8750 write_memory (to + offset, bkp_insn, len);
cca44b1b
JB
8751
8752 if (debug_displaced)
8753 fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
8754 paddress (gdbarch, from), paddress (gdbarch, to));
8755}
8756
8757/* Entry point for copying an instruction into scratch space for displaced
8758 stepping. */
8759
8760struct displaced_step_closure *
8761arm_displaced_step_copy_insn (struct gdbarch *gdbarch,
8762 CORE_ADDR from, CORE_ADDR to,
8763 struct regcache *regs)
8764{
8765 struct displaced_step_closure *dsc
8766 = xmalloc (sizeof (struct displaced_step_closure));
b434a28f 8767 arm_process_displaced_insn (gdbarch, from, to, regs, dsc);
cca44b1b
JB
8768 arm_displaced_init_closure (gdbarch, from, to, dsc);
8769
8770 return dsc;
8771}
8772
8773/* Entry point for cleaning things up after a displaced instruction has been
8774 single-stepped. */
8775
8776void
8777arm_displaced_step_fixup (struct gdbarch *gdbarch,
8778 struct displaced_step_closure *dsc,
8779 CORE_ADDR from, CORE_ADDR to,
8780 struct regcache *regs)
8781{
8782 if (dsc->cleanup)
8783 dsc->cleanup (gdbarch, regs, dsc);
8784
8785 if (!dsc->wrote_to_pc)
4db71c0b
YQ
8786 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
8787 dsc->insn_addr + dsc->insn_size);
8788
cca44b1b
JB
8789}
8790
8791#include "bfd-in2.h"
8792#include "libcoff.h"
8793
8794static int
8795gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
8796{
9779414d
DJ
8797 struct gdbarch *gdbarch = info->application_data;
8798
8799 if (arm_pc_is_thumb (gdbarch, memaddr))
cca44b1b
JB
8800 {
8801 static asymbol *asym;
8802 static combined_entry_type ce;
8803 static struct coff_symbol_struct csym;
8804 static struct bfd fake_bfd;
8805 static bfd_target fake_target;
8806
8807 if (csym.native == NULL)
8808 {
8809 /* Create a fake symbol vector containing a Thumb symbol.
8810 This is solely so that the code in print_insn_little_arm()
8811 and print_insn_big_arm() in opcodes/arm-dis.c will detect
8812 the presence of a Thumb symbol and switch to decoding
8813 Thumb instructions. */
8814
8815 fake_target.flavour = bfd_target_coff_flavour;
8816 fake_bfd.xvec = &fake_target;
8817 ce.u.syment.n_sclass = C_THUMBEXTFUNC;
8818 csym.native = &ce;
8819 csym.symbol.the_bfd = &fake_bfd;
8820 csym.symbol.name = "fake";
8821 asym = (asymbol *) & csym;
8822 }
8823
8824 memaddr = UNMAKE_THUMB_ADDR (memaddr);
8825 info->symbols = &asym;
8826 }
8827 else
8828 info->symbols = NULL;
8829
8830 if (info->endian == BFD_ENDIAN_BIG)
8831 return print_insn_big_arm (memaddr, info);
8832 else
8833 return print_insn_little_arm (memaddr, info);
8834}
8835
8836/* The following define instruction sequences that will cause ARM
8837 cpu's to take an undefined instruction trap. These are used to
8838 signal a breakpoint to GDB.
8839
8840 The newer ARMv4T cpu's are capable of operating in ARM or Thumb
8841 modes. A different instruction is required for each mode. The ARM
8842 cpu's can also be big or little endian. Thus four different
8843 instructions are needed to support all cases.
8844
8845 Note: ARMv4 defines several new instructions that will take the
8846 undefined instruction trap. ARM7TDMI is nominally ARMv4T, but does
8847 not in fact add the new instructions. The new undefined
8848 instructions in ARMv4 are all instructions that had no defined
8849 behaviour in earlier chips. There is no guarantee that they will
8850 raise an exception, but may be treated as NOP's. In practice, it
8851 may only safe to rely on instructions matching:
8852
8853 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
8854 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
8855 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
8856
0963b4bd 8857 Even this may only true if the condition predicate is true. The
cca44b1b
JB
8858 following use a condition predicate of ALWAYS so it is always TRUE.
8859
8860 There are other ways of forcing a breakpoint. GNU/Linux, RISC iX,
8861 and NetBSD all use a software interrupt rather than an undefined
8862 instruction to force a trap. This can be handled by by the
8863 abi-specific code during establishment of the gdbarch vector. */
8864
8865#define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
8866#define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
8867#define THUMB_LE_BREAKPOINT {0xbe,0xbe}
8868#define THUMB_BE_BREAKPOINT {0xbe,0xbe}
8869
948f8e3d
PA
8870static const gdb_byte arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
8871static const gdb_byte arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
8872static const gdb_byte arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
8873static const gdb_byte arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
cca44b1b
JB
8874
8875/* Determine the type and size of breakpoint to insert at PCPTR. Uses
8876 the program counter value to determine whether a 16-bit or 32-bit
8877 breakpoint should be used. It returns a pointer to a string of
8878 bytes that encode a breakpoint instruction, stores the length of
8879 the string to *lenptr, and adjusts the program counter (if
8880 necessary) to point to the actual memory location where the
8881 breakpoint should be inserted. */
8882
8883static const unsigned char *
8884arm_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenptr)
8885{
8886 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
177321bd 8887 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
cca44b1b 8888
9779414d 8889 if (arm_pc_is_thumb (gdbarch, *pcptr))
cca44b1b
JB
8890 {
8891 *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
177321bd
DJ
8892
8893 /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
8894 check whether we are replacing a 32-bit instruction. */
8895 if (tdep->thumb2_breakpoint != NULL)
8896 {
8897 gdb_byte buf[2];
8898 if (target_read_memory (*pcptr, buf, 2) == 0)
8899 {
8900 unsigned short inst1;
8901 inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
db24da6d 8902 if (thumb_insn_size (inst1) == 4)
177321bd
DJ
8903 {
8904 *lenptr = tdep->thumb2_breakpoint_size;
8905 return tdep->thumb2_breakpoint;
8906 }
8907 }
8908 }
8909
cca44b1b
JB
8910 *lenptr = tdep->thumb_breakpoint_size;
8911 return tdep->thumb_breakpoint;
8912 }
8913 else
8914 {
8915 *lenptr = tdep->arm_breakpoint_size;
8916 return tdep->arm_breakpoint;
8917 }
8918}
8919
177321bd
DJ
8920static void
8921arm_remote_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
8922 int *kindptr)
8923{
177321bd
DJ
8924 arm_breakpoint_from_pc (gdbarch, pcptr, kindptr);
8925
9779414d 8926 if (arm_pc_is_thumb (gdbarch, *pcptr) && *kindptr == 4)
177321bd
DJ
8927 /* The documented magic value for a 32-bit Thumb-2 breakpoint, so
8928 that this is not confused with a 32-bit ARM breakpoint. */
8929 *kindptr = 3;
8930}
8931
cca44b1b
JB
8932/* Extract from an array REGBUF containing the (raw) register state a
8933 function return value of type TYPE, and copy that, in virtual
8934 format, into VALBUF. */
8935
8936static void
8937arm_extract_return_value (struct type *type, struct regcache *regs,
8938 gdb_byte *valbuf)
8939{
8940 struct gdbarch *gdbarch = get_regcache_arch (regs);
8941 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8942
8943 if (TYPE_CODE_FLT == TYPE_CODE (type))
8944 {
8945 switch (gdbarch_tdep (gdbarch)->fp_model)
8946 {
8947 case ARM_FLOAT_FPA:
8948 {
8949 /* The value is in register F0 in internal format. We need to
8950 extract the raw value and then convert it to the desired
8951 internal type. */
8952 bfd_byte tmpbuf[FP_REGISTER_SIZE];
8953
8954 regcache_cooked_read (regs, ARM_F0_REGNUM, tmpbuf);
8955 convert_from_extended (floatformat_from_type (type), tmpbuf,
8956 valbuf, gdbarch_byte_order (gdbarch));
8957 }
8958 break;
8959
8960 case ARM_FLOAT_SOFT_FPA:
8961 case ARM_FLOAT_SOFT_VFP:
8962 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8963 not using the VFP ABI code. */
8964 case ARM_FLOAT_VFP:
8965 regcache_cooked_read (regs, ARM_A1_REGNUM, valbuf);
8966 if (TYPE_LENGTH (type) > 4)
8967 regcache_cooked_read (regs, ARM_A1_REGNUM + 1,
8968 valbuf + INT_REGISTER_SIZE);
8969 break;
8970
8971 default:
0963b4bd
MS
8972 internal_error (__FILE__, __LINE__,
8973 _("arm_extract_return_value: "
8974 "Floating point model not supported"));
cca44b1b
JB
8975 break;
8976 }
8977 }
8978 else if (TYPE_CODE (type) == TYPE_CODE_INT
8979 || TYPE_CODE (type) == TYPE_CODE_CHAR
8980 || TYPE_CODE (type) == TYPE_CODE_BOOL
8981 || TYPE_CODE (type) == TYPE_CODE_PTR
8982 || TYPE_CODE (type) == TYPE_CODE_REF
8983 || TYPE_CODE (type) == TYPE_CODE_ENUM)
8984 {
b021a221
MS
8985 /* If the type is a plain integer, then the access is
8986 straight-forward. Otherwise we have to play around a bit
8987 more. */
cca44b1b
JB
8988 int len = TYPE_LENGTH (type);
8989 int regno = ARM_A1_REGNUM;
8990 ULONGEST tmp;
8991
8992 while (len > 0)
8993 {
8994 /* By using store_unsigned_integer we avoid having to do
8995 anything special for small big-endian values. */
8996 regcache_cooked_read_unsigned (regs, regno++, &tmp);
8997 store_unsigned_integer (valbuf,
8998 (len > INT_REGISTER_SIZE
8999 ? INT_REGISTER_SIZE : len),
9000 byte_order, tmp);
9001 len -= INT_REGISTER_SIZE;
9002 valbuf += INT_REGISTER_SIZE;
9003 }
9004 }
9005 else
9006 {
9007 /* For a structure or union the behaviour is as if the value had
9008 been stored to word-aligned memory and then loaded into
9009 registers with 32-bit load instruction(s). */
9010 int len = TYPE_LENGTH (type);
9011 int regno = ARM_A1_REGNUM;
9012 bfd_byte tmpbuf[INT_REGISTER_SIZE];
9013
9014 while (len > 0)
9015 {
9016 regcache_cooked_read (regs, regno++, tmpbuf);
9017 memcpy (valbuf, tmpbuf,
9018 len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
9019 len -= INT_REGISTER_SIZE;
9020 valbuf += INT_REGISTER_SIZE;
9021 }
9022 }
9023}
9024
9025
9026/* Will a function return an aggregate type in memory or in a
9027 register? Return 0 if an aggregate type can be returned in a
9028 register, 1 if it must be returned in memory. */
9029
9030static int
9031arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
9032{
9033 int nRc;
9034 enum type_code code;
9035
9036 CHECK_TYPEDEF (type);
9037
9038 /* In the ARM ABI, "integer" like aggregate types are returned in
9039 registers. For an aggregate type to be integer like, its size
9040 must be less than or equal to INT_REGISTER_SIZE and the
9041 offset of each addressable subfield must be zero. Note that bit
9042 fields are not addressable, and all addressable subfields of
9043 unions always start at offset zero.
9044
9045 This function is based on the behaviour of GCC 2.95.1.
9046 See: gcc/arm.c: arm_return_in_memory() for details.
9047
9048 Note: All versions of GCC before GCC 2.95.2 do not set up the
9049 parameters correctly for a function returning the following
9050 structure: struct { float f;}; This should be returned in memory,
9051 not a register. Richard Earnshaw sent me a patch, but I do not
9052 know of any way to detect if a function like the above has been
9053 compiled with the correct calling convention. */
9054
9055 /* All aggregate types that won't fit in a register must be returned
9056 in memory. */
9057 if (TYPE_LENGTH (type) > INT_REGISTER_SIZE)
9058 {
9059 return 1;
9060 }
9061
9062 /* The AAPCS says all aggregates not larger than a word are returned
9063 in a register. */
9064 if (gdbarch_tdep (gdbarch)->arm_abi != ARM_ABI_APCS)
9065 return 0;
9066
9067 /* The only aggregate types that can be returned in a register are
9068 structs and unions. Arrays must be returned in memory. */
9069 code = TYPE_CODE (type);
9070 if ((TYPE_CODE_STRUCT != code) && (TYPE_CODE_UNION != code))
9071 {
9072 return 1;
9073 }
9074
9075 /* Assume all other aggregate types can be returned in a register.
9076 Run a check for structures, unions and arrays. */
9077 nRc = 0;
9078
9079 if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
9080 {
9081 int i;
9082 /* Need to check if this struct/union is "integer" like. For
9083 this to be true, its size must be less than or equal to
9084 INT_REGISTER_SIZE and the offset of each addressable
9085 subfield must be zero. Note that bit fields are not
9086 addressable, and unions always start at offset zero. If any
9087 of the subfields is a floating point type, the struct/union
9088 cannot be an integer type. */
9089
9090 /* For each field in the object, check:
9091 1) Is it FP? --> yes, nRc = 1;
67255d04
RE
9092 2) Is it addressable (bitpos != 0) and
9093 not packed (bitsize == 0)?
9094 --> yes, nRc = 1
9095 */
9096
9097 for (i = 0; i < TYPE_NFIELDS (type); i++)
9098 {
9099 enum type_code field_type_code;
0963b4bd
MS
9100 field_type_code = TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type,
9101 i)));
67255d04
RE
9102
9103 /* Is it a floating point type field? */
9104 if (field_type_code == TYPE_CODE_FLT)
9105 {
9106 nRc = 1;
9107 break;
9108 }
9109
9110 /* If bitpos != 0, then we have to care about it. */
9111 if (TYPE_FIELD_BITPOS (type, i) != 0)
9112 {
9113 /* Bitfields are not addressable. If the field bitsize is
9114 zero, then the field is not packed. Hence it cannot be
9115 a bitfield or any other packed type. */
9116 if (TYPE_FIELD_BITSIZE (type, i) == 0)
9117 {
9118 nRc = 1;
9119 break;
9120 }
9121 }
9122 }
9123 }
9124
9125 return nRc;
9126}
9127
34e8f22d
RE
9128/* Write into appropriate registers a function return value of type
9129 TYPE, given in virtual format. */
9130
9131static void
b508a996 9132arm_store_return_value (struct type *type, struct regcache *regs,
5238cf52 9133 const gdb_byte *valbuf)
34e8f22d 9134{
be8626e0 9135 struct gdbarch *gdbarch = get_regcache_arch (regs);
e17a4113 9136 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
be8626e0 9137
34e8f22d
RE
9138 if (TYPE_CODE (type) == TYPE_CODE_FLT)
9139 {
e362b510 9140 gdb_byte buf[MAX_REGISTER_SIZE];
34e8f22d 9141
be8626e0 9142 switch (gdbarch_tdep (gdbarch)->fp_model)
08216dd7
RE
9143 {
9144 case ARM_FLOAT_FPA:
9145
be8626e0
MD
9146 convert_to_extended (floatformat_from_type (type), buf, valbuf,
9147 gdbarch_byte_order (gdbarch));
b508a996 9148 regcache_cooked_write (regs, ARM_F0_REGNUM, buf);
08216dd7
RE
9149 break;
9150
fd50bc42 9151 case ARM_FLOAT_SOFT_FPA:
08216dd7 9152 case ARM_FLOAT_SOFT_VFP:
90445bd3
DJ
9153 /* ARM_FLOAT_VFP can arise if this is a variadic function so
9154 not using the VFP ABI code. */
9155 case ARM_FLOAT_VFP:
b508a996
RE
9156 regcache_cooked_write (regs, ARM_A1_REGNUM, valbuf);
9157 if (TYPE_LENGTH (type) > 4)
9158 regcache_cooked_write (regs, ARM_A1_REGNUM + 1,
7a5ea0d4 9159 valbuf + INT_REGISTER_SIZE);
08216dd7
RE
9160 break;
9161
9162 default:
9b20d036
MS
9163 internal_error (__FILE__, __LINE__,
9164 _("arm_store_return_value: Floating "
9165 "point model not supported"));
08216dd7
RE
9166 break;
9167 }
34e8f22d 9168 }
b508a996
RE
9169 else if (TYPE_CODE (type) == TYPE_CODE_INT
9170 || TYPE_CODE (type) == TYPE_CODE_CHAR
9171 || TYPE_CODE (type) == TYPE_CODE_BOOL
9172 || TYPE_CODE (type) == TYPE_CODE_PTR
9173 || TYPE_CODE (type) == TYPE_CODE_REF
9174 || TYPE_CODE (type) == TYPE_CODE_ENUM)
9175 {
9176 if (TYPE_LENGTH (type) <= 4)
9177 {
9178 /* Values of one word or less are zero/sign-extended and
9179 returned in r0. */
7a5ea0d4 9180 bfd_byte tmpbuf[INT_REGISTER_SIZE];
b508a996
RE
9181 LONGEST val = unpack_long (type, valbuf);
9182
e17a4113 9183 store_signed_integer (tmpbuf, INT_REGISTER_SIZE, byte_order, val);
b508a996
RE
9184 regcache_cooked_write (regs, ARM_A1_REGNUM, tmpbuf);
9185 }
9186 else
9187 {
9188 /* Integral values greater than one word are stored in consecutive
9189 registers starting with r0. This will always be a multiple of
9190 the regiser size. */
9191 int len = TYPE_LENGTH (type);
9192 int regno = ARM_A1_REGNUM;
9193
9194 while (len > 0)
9195 {
9196 regcache_cooked_write (regs, regno++, valbuf);
7a5ea0d4
DJ
9197 len -= INT_REGISTER_SIZE;
9198 valbuf += INT_REGISTER_SIZE;
b508a996
RE
9199 }
9200 }
9201 }
34e8f22d 9202 else
b508a996
RE
9203 {
9204 /* For a structure or union the behaviour is as if the value had
9205 been stored to word-aligned memory and then loaded into
9206 registers with 32-bit load instruction(s). */
9207 int len = TYPE_LENGTH (type);
9208 int regno = ARM_A1_REGNUM;
7a5ea0d4 9209 bfd_byte tmpbuf[INT_REGISTER_SIZE];
b508a996
RE
9210
9211 while (len > 0)
9212 {
9213 memcpy (tmpbuf, valbuf,
7a5ea0d4 9214 len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
b508a996 9215 regcache_cooked_write (regs, regno++, tmpbuf);
7a5ea0d4
DJ
9216 len -= INT_REGISTER_SIZE;
9217 valbuf += INT_REGISTER_SIZE;
b508a996
RE
9218 }
9219 }
34e8f22d
RE
9220}
9221
2af48f68
PB
9222
9223/* Handle function return values. */
9224
9225static enum return_value_convention
6a3a010b 9226arm_return_value (struct gdbarch *gdbarch, struct value *function,
c055b101
CV
9227 struct type *valtype, struct regcache *regcache,
9228 gdb_byte *readbuf, const gdb_byte *writebuf)
2af48f68 9229{
7c00367c 9230 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
6a3a010b 9231 struct type *func_type = function ? value_type (function) : NULL;
90445bd3
DJ
9232 enum arm_vfp_cprc_base_type vfp_base_type;
9233 int vfp_base_count;
9234
9235 if (arm_vfp_abi_for_function (gdbarch, func_type)
9236 && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
9237 {
9238 int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
9239 int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
9240 int i;
9241 for (i = 0; i < vfp_base_count; i++)
9242 {
58d6951d
DJ
9243 if (reg_char == 'q')
9244 {
9245 if (writebuf)
9246 arm_neon_quad_write (gdbarch, regcache, i,
9247 writebuf + i * unit_length);
9248
9249 if (readbuf)
9250 arm_neon_quad_read (gdbarch, regcache, i,
9251 readbuf + i * unit_length);
9252 }
9253 else
9254 {
9255 char name_buf[4];
9256 int regnum;
9257
8c042590 9258 xsnprintf (name_buf, sizeof (name_buf), "%c%d", reg_char, i);
58d6951d
DJ
9259 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9260 strlen (name_buf));
9261 if (writebuf)
9262 regcache_cooked_write (regcache, regnum,
9263 writebuf + i * unit_length);
9264 if (readbuf)
9265 regcache_cooked_read (regcache, regnum,
9266 readbuf + i * unit_length);
9267 }
90445bd3
DJ
9268 }
9269 return RETURN_VALUE_REGISTER_CONVENTION;
9270 }
7c00367c 9271
2af48f68
PB
9272 if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
9273 || TYPE_CODE (valtype) == TYPE_CODE_UNION
9274 || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
9275 {
7c00367c
MK
9276 if (tdep->struct_return == pcc_struct_return
9277 || arm_return_in_memory (gdbarch, valtype))
2af48f68
PB
9278 return RETURN_VALUE_STRUCT_CONVENTION;
9279 }
9280
7052e42c
UW
9281 /* AAPCS returns complex types longer than a register in memory. */
9282 if (tdep->arm_abi != ARM_ABI_APCS
9283 && TYPE_CODE (valtype) == TYPE_CODE_COMPLEX
9284 && TYPE_LENGTH (valtype) > INT_REGISTER_SIZE)
9285 return RETURN_VALUE_STRUCT_CONVENTION;
9286
2af48f68
PB
9287 if (writebuf)
9288 arm_store_return_value (valtype, regcache, writebuf);
9289
9290 if (readbuf)
9291 arm_extract_return_value (valtype, regcache, readbuf);
9292
9293 return RETURN_VALUE_REGISTER_CONVENTION;
9294}
9295
9296
9df628e0 9297static int
60ade65d 9298arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
9df628e0 9299{
e17a4113
UW
9300 struct gdbarch *gdbarch = get_frame_arch (frame);
9301 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
9302 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
9df628e0 9303 CORE_ADDR jb_addr;
e362b510 9304 gdb_byte buf[INT_REGISTER_SIZE];
9df628e0 9305
60ade65d 9306 jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
9df628e0
RE
9307
9308 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
7a5ea0d4 9309 INT_REGISTER_SIZE))
9df628e0
RE
9310 return 0;
9311
e17a4113 9312 *pc = extract_unsigned_integer (buf, INT_REGISTER_SIZE, byte_order);
9df628e0
RE
9313 return 1;
9314}
9315
faa95490
DJ
9316/* Recognize GCC and GNU ld's trampolines. If we are in a trampoline,
9317 return the target PC. Otherwise return 0. */
c906108c
SS
9318
9319CORE_ADDR
52f729a7 9320arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
c906108c 9321{
2c02bd72 9322 const char *name;
faa95490 9323 int namelen;
c906108c
SS
9324 CORE_ADDR start_addr;
9325
9326 /* Find the starting address and name of the function containing the PC. */
9327 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
80d8d390
YQ
9328 {
9329 /* Trampoline 'bx reg' doesn't belong to any functions. Do the
9330 check here. */
9331 start_addr = arm_skip_bx_reg (frame, pc);
9332 if (start_addr != 0)
9333 return start_addr;
9334
9335 return 0;
9336 }
c906108c 9337
faa95490
DJ
9338 /* If PC is in a Thumb call or return stub, return the address of the
9339 target PC, which is in a register. The thunk functions are called
9340 _call_via_xx, where x is the register name. The possible names
3d8d5e79
DJ
9341 are r0-r9, sl, fp, ip, sp, and lr. ARM RealView has similar
9342 functions, named __ARM_call_via_r[0-7]. */
9343 if (strncmp (name, "_call_via_", 10) == 0
9344 || strncmp (name, "__ARM_call_via_", strlen ("__ARM_call_via_")) == 0)
c906108c 9345 {
ed9a39eb
JM
9346 /* Use the name suffix to determine which register contains the
9347 target PC. */
c5aa993b
JM
9348 static char *table[15] =
9349 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
9350 "r8", "r9", "sl", "fp", "ip", "sp", "lr"
9351 };
c906108c 9352 int regno;
faa95490 9353 int offset = strlen (name) - 2;
c906108c
SS
9354
9355 for (regno = 0; regno <= 14; regno++)
faa95490 9356 if (strcmp (&name[offset], table[regno]) == 0)
52f729a7 9357 return get_frame_register_unsigned (frame, regno);
c906108c 9358 }
ed9a39eb 9359
faa95490
DJ
9360 /* GNU ld generates __foo_from_arm or __foo_from_thumb for
9361 non-interworking calls to foo. We could decode the stubs
9362 to find the target but it's easier to use the symbol table. */
9363 namelen = strlen (name);
9364 if (name[0] == '_' && name[1] == '_'
9365 && ((namelen > 2 + strlen ("_from_thumb")
9366 && strncmp (name + namelen - strlen ("_from_thumb"), "_from_thumb",
9367 strlen ("_from_thumb")) == 0)
9368 || (namelen > 2 + strlen ("_from_arm")
9369 && strncmp (name + namelen - strlen ("_from_arm"), "_from_arm",
9370 strlen ("_from_arm")) == 0)))
9371 {
9372 char *target_name;
9373 int target_len = namelen - 2;
3b7344d5 9374 struct bound_minimal_symbol minsym;
faa95490
DJ
9375 struct objfile *objfile;
9376 struct obj_section *sec;
9377
9378 if (name[namelen - 1] == 'b')
9379 target_len -= strlen ("_from_thumb");
9380 else
9381 target_len -= strlen ("_from_arm");
9382
9383 target_name = alloca (target_len + 1);
9384 memcpy (target_name, name + 2, target_len);
9385 target_name[target_len] = '\0';
9386
9387 sec = find_pc_section (pc);
9388 objfile = (sec == NULL) ? NULL : sec->objfile;
9389 minsym = lookup_minimal_symbol (target_name, NULL, objfile);
3b7344d5 9390 if (minsym.minsym != NULL)
77e371c0 9391 return BMSYMBOL_VALUE_ADDRESS (minsym);
faa95490
DJ
9392 else
9393 return 0;
9394 }
9395
c5aa993b 9396 return 0; /* not a stub */
c906108c
SS
9397}
9398
afd7eef0
RE
9399static void
9400set_arm_command (char *args, int from_tty)
9401{
edefbb7c
AC
9402 printf_unfiltered (_("\
9403\"set arm\" must be followed by an apporpriate subcommand.\n"));
afd7eef0
RE
9404 help_list (setarmcmdlist, "set arm ", all_commands, gdb_stdout);
9405}
9406
9407static void
9408show_arm_command (char *args, int from_tty)
9409{
26304000 9410 cmd_show_list (showarmcmdlist, from_tty, "");
afd7eef0
RE
9411}
9412
28e97307
DJ
9413static void
9414arm_update_current_architecture (void)
fd50bc42 9415{
28e97307 9416 struct gdbarch_info info;
fd50bc42 9417
28e97307 9418 /* If the current architecture is not ARM, we have nothing to do. */
f5656ead 9419 if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_arm)
28e97307 9420 return;
fd50bc42 9421
28e97307
DJ
9422 /* Update the architecture. */
9423 gdbarch_info_init (&info);
fd50bc42 9424
28e97307 9425 if (!gdbarch_update_p (info))
9b20d036 9426 internal_error (__FILE__, __LINE__, _("could not update architecture"));
fd50bc42
RE
9427}
9428
9429static void
9430set_fp_model_sfunc (char *args, int from_tty,
9431 struct cmd_list_element *c)
9432{
9433 enum arm_float_model fp_model;
9434
9435 for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
9436 if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
9437 {
9438 arm_fp_model = fp_model;
9439 break;
9440 }
9441
9442 if (fp_model == ARM_FLOAT_LAST)
edefbb7c 9443 internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
fd50bc42
RE
9444 current_fp_model);
9445
28e97307 9446 arm_update_current_architecture ();
fd50bc42
RE
9447}
9448
9449static void
08546159
AC
9450show_fp_model (struct ui_file *file, int from_tty,
9451 struct cmd_list_element *c, const char *value)
fd50bc42 9452{
f5656ead 9453 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
fd50bc42 9454
28e97307 9455 if (arm_fp_model == ARM_FLOAT_AUTO
f5656ead 9456 && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
28e97307
DJ
9457 fprintf_filtered (file, _("\
9458The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
9459 fp_model_strings[tdep->fp_model]);
9460 else
9461 fprintf_filtered (file, _("\
9462The current ARM floating point model is \"%s\".\n"),
9463 fp_model_strings[arm_fp_model]);
9464}
9465
9466static void
9467arm_set_abi (char *args, int from_tty,
9468 struct cmd_list_element *c)
9469{
9470 enum arm_abi_kind arm_abi;
9471
9472 for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
9473 if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
9474 {
9475 arm_abi_global = arm_abi;
9476 break;
9477 }
9478
9479 if (arm_abi == ARM_ABI_LAST)
9480 internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
9481 arm_abi_string);
9482
9483 arm_update_current_architecture ();
9484}
9485
9486static void
9487arm_show_abi (struct ui_file *file, int from_tty,
9488 struct cmd_list_element *c, const char *value)
9489{
f5656ead 9490 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
28e97307
DJ
9491
9492 if (arm_abi_global == ARM_ABI_AUTO
f5656ead 9493 && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
28e97307
DJ
9494 fprintf_filtered (file, _("\
9495The current ARM ABI is \"auto\" (currently \"%s\").\n"),
9496 arm_abi_strings[tdep->arm_abi]);
9497 else
9498 fprintf_filtered (file, _("The current ARM ABI is \"%s\".\n"),
9499 arm_abi_string);
fd50bc42
RE
9500}
9501
0428b8f5
DJ
9502static void
9503arm_show_fallback_mode (struct ui_file *file, int from_tty,
9504 struct cmd_list_element *c, const char *value)
9505{
0963b4bd
MS
9506 fprintf_filtered (file,
9507 _("The current execution mode assumed "
9508 "(when symbols are unavailable) is \"%s\".\n"),
0428b8f5
DJ
9509 arm_fallback_mode_string);
9510}
9511
9512static void
9513arm_show_force_mode (struct ui_file *file, int from_tty,
9514 struct cmd_list_element *c, const char *value)
9515{
f5656ead 9516 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
0428b8f5 9517
0963b4bd
MS
9518 fprintf_filtered (file,
9519 _("The current execution mode assumed "
9520 "(even when symbols are available) is \"%s\".\n"),
0428b8f5
DJ
9521 arm_force_mode_string);
9522}
9523
afd7eef0
RE
9524/* If the user changes the register disassembly style used for info
9525 register and other commands, we have to also switch the style used
9526 in opcodes for disassembly output. This function is run in the "set
9527 arm disassembly" command, and does that. */
bc90b915
FN
9528
9529static void
afd7eef0 9530set_disassembly_style_sfunc (char *args, int from_tty,
bc90b915
FN
9531 struct cmd_list_element *c)
9532{
afd7eef0 9533 set_disassembly_style ();
bc90b915
FN
9534}
9535\f
966fbf70 9536/* Return the ARM register name corresponding to register I. */
a208b0cb 9537static const char *
d93859e2 9538arm_register_name (struct gdbarch *gdbarch, int i)
966fbf70 9539{
58d6951d
DJ
9540 const int num_regs = gdbarch_num_regs (gdbarch);
9541
9542 if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
9543 && i >= num_regs && i < num_regs + 32)
9544 {
9545 static const char *const vfp_pseudo_names[] = {
9546 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
9547 "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
9548 "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
9549 "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
9550 };
9551
9552 return vfp_pseudo_names[i - num_regs];
9553 }
9554
9555 if (gdbarch_tdep (gdbarch)->have_neon_pseudos
9556 && i >= num_regs + 32 && i < num_regs + 32 + 16)
9557 {
9558 static const char *const neon_pseudo_names[] = {
9559 "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
9560 "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
9561 };
9562
9563 return neon_pseudo_names[i - num_regs - 32];
9564 }
9565
ff6f572f
DJ
9566 if (i >= ARRAY_SIZE (arm_register_names))
9567 /* These registers are only supported on targets which supply
9568 an XML description. */
9569 return "";
9570
966fbf70
RE
9571 return arm_register_names[i];
9572}
9573
bc90b915 9574static void
afd7eef0 9575set_disassembly_style (void)
bc90b915 9576{
123dc839 9577 int current;
bc90b915 9578
123dc839
DJ
9579 /* Find the style that the user wants. */
9580 for (current = 0; current < num_disassembly_options; current++)
9581 if (disassembly_style == valid_disassembly_styles[current])
9582 break;
9583 gdb_assert (current < num_disassembly_options);
bc90b915 9584
94c30b78 9585 /* Synchronize the disassembler. */
bc90b915
FN
9586 set_arm_regname_option (current);
9587}
9588
082fc60d
RE
9589/* Test whether the coff symbol specific value corresponds to a Thumb
9590 function. */
9591
9592static int
9593coff_sym_is_thumb (int val)
9594{
f8bf5763
PM
9595 return (val == C_THUMBEXT
9596 || val == C_THUMBSTAT
9597 || val == C_THUMBEXTFUNC
9598 || val == C_THUMBSTATFUNC
9599 || val == C_THUMBLABEL);
082fc60d
RE
9600}
9601
9602/* arm_coff_make_msymbol_special()
9603 arm_elf_make_msymbol_special()
9604
9605 These functions test whether the COFF or ELF symbol corresponds to
9606 an address in thumb code, and set a "special" bit in a minimal
9607 symbol to indicate that it does. */
9608
34e8f22d 9609static void
082fc60d
RE
9610arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
9611{
467d42c4
UW
9612 if (ARM_SYM_BRANCH_TYPE (&((elf_symbol_type *)sym)->internal_elf_sym)
9613 == ST_BRANCH_TO_THUMB)
082fc60d
RE
9614 MSYMBOL_SET_SPECIAL (msym);
9615}
9616
34e8f22d 9617static void
082fc60d
RE
9618arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
9619{
9620 if (coff_sym_is_thumb (val))
9621 MSYMBOL_SET_SPECIAL (msym);
9622}
9623
60c5725c 9624static void
c1bd65d0 9625arm_objfile_data_free (struct objfile *objfile, void *arg)
60c5725c
DJ
9626{
9627 struct arm_per_objfile *data = arg;
9628 unsigned int i;
9629
9630 for (i = 0; i < objfile->obfd->section_count; i++)
9631 VEC_free (arm_mapping_symbol_s, data->section_maps[i]);
9632}
9633
9634static void
9635arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
9636 asymbol *sym)
9637{
9638 const char *name = bfd_asymbol_name (sym);
9639 struct arm_per_objfile *data;
9640 VEC(arm_mapping_symbol_s) **map_p;
9641 struct arm_mapping_symbol new_map_sym;
9642
9643 gdb_assert (name[0] == '$');
9644 if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
9645 return;
9646
9647 data = objfile_data (objfile, arm_objfile_data_key);
9648 if (data == NULL)
9649 {
9650 data = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9651 struct arm_per_objfile);
9652 set_objfile_data (objfile, arm_objfile_data_key, data);
9653 data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
9654 objfile->obfd->section_count,
9655 VEC(arm_mapping_symbol_s) *);
9656 }
9657 map_p = &data->section_maps[bfd_get_section (sym)->index];
9658
9659 new_map_sym.value = sym->value;
9660 new_map_sym.type = name[1];
9661
9662 /* Assume that most mapping symbols appear in order of increasing
9663 value. If they were randomly distributed, it would be faster to
9664 always push here and then sort at first use. */
9665 if (!VEC_empty (arm_mapping_symbol_s, *map_p))
9666 {
9667 struct arm_mapping_symbol *prev_map_sym;
9668
9669 prev_map_sym = VEC_last (arm_mapping_symbol_s, *map_p);
9670 if (prev_map_sym->value >= sym->value)
9671 {
9672 unsigned int idx;
9673 idx = VEC_lower_bound (arm_mapping_symbol_s, *map_p, &new_map_sym,
9674 arm_compare_mapping_symbols);
9675 VEC_safe_insert (arm_mapping_symbol_s, *map_p, idx, &new_map_sym);
9676 return;
9677 }
9678 }
9679
9680 VEC_safe_push (arm_mapping_symbol_s, *map_p, &new_map_sym);
9681}
9682
756fe439 9683static void
61a1198a 9684arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
756fe439 9685{
9779414d 9686 struct gdbarch *gdbarch = get_regcache_arch (regcache);
61a1198a 9687 regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
756fe439
DJ
9688
9689 /* If necessary, set the T bit. */
9690 if (arm_apcs_32)
9691 {
9779414d 9692 ULONGEST val, t_bit;
61a1198a 9693 regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
9779414d
DJ
9694 t_bit = arm_psr_thumb_bit (gdbarch);
9695 if (arm_pc_is_thumb (gdbarch, pc))
9696 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9697 val | t_bit);
756fe439 9698 else
61a1198a 9699 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9779414d 9700 val & ~t_bit);
756fe439
DJ
9701 }
9702}
123dc839 9703
58d6951d
DJ
9704/* Read the contents of a NEON quad register, by reading from two
9705 double registers. This is used to implement the quad pseudo
9706 registers, and for argument passing in case the quad registers are
9707 missing; vectors are passed in quad registers when using the VFP
9708 ABI, even if a NEON unit is not present. REGNUM is the index of
9709 the quad register, in [0, 15]. */
9710
05d1431c 9711static enum register_status
58d6951d
DJ
9712arm_neon_quad_read (struct gdbarch *gdbarch, struct regcache *regcache,
9713 int regnum, gdb_byte *buf)
9714{
9715 char name_buf[4];
9716 gdb_byte reg_buf[8];
9717 int offset, double_regnum;
05d1431c 9718 enum register_status status;
58d6951d 9719
8c042590 9720 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
58d6951d
DJ
9721 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9722 strlen (name_buf));
9723
9724 /* d0 is always the least significant half of q0. */
9725 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9726 offset = 8;
9727 else
9728 offset = 0;
9729
05d1431c
PA
9730 status = regcache_raw_read (regcache, double_regnum, reg_buf);
9731 if (status != REG_VALID)
9732 return status;
58d6951d
DJ
9733 memcpy (buf + offset, reg_buf, 8);
9734
9735 offset = 8 - offset;
05d1431c
PA
9736 status = regcache_raw_read (regcache, double_regnum + 1, reg_buf);
9737 if (status != REG_VALID)
9738 return status;
58d6951d 9739 memcpy (buf + offset, reg_buf, 8);
05d1431c
PA
9740
9741 return REG_VALID;
58d6951d
DJ
9742}
9743
05d1431c 9744static enum register_status
58d6951d
DJ
9745arm_pseudo_read (struct gdbarch *gdbarch, struct regcache *regcache,
9746 int regnum, gdb_byte *buf)
9747{
9748 const int num_regs = gdbarch_num_regs (gdbarch);
9749 char name_buf[4];
9750 gdb_byte reg_buf[8];
9751 int offset, double_regnum;
9752
9753 gdb_assert (regnum >= num_regs);
9754 regnum -= num_regs;
9755
9756 if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
9757 /* Quad-precision register. */
05d1431c 9758 return arm_neon_quad_read (gdbarch, regcache, regnum - 32, buf);
58d6951d
DJ
9759 else
9760 {
05d1431c
PA
9761 enum register_status status;
9762
58d6951d
DJ
9763 /* Single-precision register. */
9764 gdb_assert (regnum < 32);
9765
9766 /* s0 is always the least significant half of d0. */
9767 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9768 offset = (regnum & 1) ? 0 : 4;
9769 else
9770 offset = (regnum & 1) ? 4 : 0;
9771
8c042590 9772 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
58d6951d
DJ
9773 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9774 strlen (name_buf));
9775
05d1431c
PA
9776 status = regcache_raw_read (regcache, double_regnum, reg_buf);
9777 if (status == REG_VALID)
9778 memcpy (buf, reg_buf + offset, 4);
9779 return status;
58d6951d
DJ
9780 }
9781}
9782
9783/* Store the contents of BUF to a NEON quad register, by writing to
9784 two double registers. This is used to implement the quad pseudo
9785 registers, and for argument passing in case the quad registers are
9786 missing; vectors are passed in quad registers when using the VFP
9787 ABI, even if a NEON unit is not present. REGNUM is the index
9788 of the quad register, in [0, 15]. */
9789
9790static void
9791arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
9792 int regnum, const gdb_byte *buf)
9793{
9794 char name_buf[4];
58d6951d
DJ
9795 int offset, double_regnum;
9796
8c042590 9797 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
58d6951d
DJ
9798 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9799 strlen (name_buf));
9800
9801 /* d0 is always the least significant half of q0. */
9802 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9803 offset = 8;
9804 else
9805 offset = 0;
9806
9807 regcache_raw_write (regcache, double_regnum, buf + offset);
9808 offset = 8 - offset;
9809 regcache_raw_write (regcache, double_regnum + 1, buf + offset);
9810}
9811
9812static void
9813arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
9814 int regnum, const gdb_byte *buf)
9815{
9816 const int num_regs = gdbarch_num_regs (gdbarch);
9817 char name_buf[4];
9818 gdb_byte reg_buf[8];
9819 int offset, double_regnum;
9820
9821 gdb_assert (regnum >= num_regs);
9822 regnum -= num_regs;
9823
9824 if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
9825 /* Quad-precision register. */
9826 arm_neon_quad_write (gdbarch, regcache, regnum - 32, buf);
9827 else
9828 {
9829 /* Single-precision register. */
9830 gdb_assert (regnum < 32);
9831
9832 /* s0 is always the least significant half of d0. */
9833 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9834 offset = (regnum & 1) ? 0 : 4;
9835 else
9836 offset = (regnum & 1) ? 4 : 0;
9837
8c042590 9838 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
58d6951d
DJ
9839 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9840 strlen (name_buf));
9841
9842 regcache_raw_read (regcache, double_regnum, reg_buf);
9843 memcpy (reg_buf + offset, buf, 4);
9844 regcache_raw_write (regcache, double_regnum, reg_buf);
9845 }
9846}
9847
123dc839
DJ
9848static struct value *
9849value_of_arm_user_reg (struct frame_info *frame, const void *baton)
9850{
9851 const int *reg_p = baton;
9852 return value_of_register (*reg_p, frame);
9853}
97e03143 9854\f
70f80edf
JT
9855static enum gdb_osabi
9856arm_elf_osabi_sniffer (bfd *abfd)
97e03143 9857{
2af48f68 9858 unsigned int elfosabi;
70f80edf 9859 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
97e03143 9860
70f80edf 9861 elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
97e03143 9862
28e97307
DJ
9863 if (elfosabi == ELFOSABI_ARM)
9864 /* GNU tools use this value. Check note sections in this case,
9865 as well. */
9866 bfd_map_over_sections (abfd,
9867 generic_elf_osabi_sniff_abi_tag_sections,
9868 &osabi);
97e03143 9869
28e97307 9870 /* Anything else will be handled by the generic ELF sniffer. */
70f80edf 9871 return osabi;
97e03143
RE
9872}
9873
54483882
YQ
9874static int
9875arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
9876 struct reggroup *group)
9877{
2c291032
YQ
9878 /* FPS register's type is INT, but belongs to float_reggroup. Beside
9879 this, FPS register belongs to save_regroup, restore_reggroup, and
9880 all_reggroup, of course. */
54483882 9881 if (regnum == ARM_FPS_REGNUM)
2c291032
YQ
9882 return (group == float_reggroup
9883 || group == save_reggroup
9884 || group == restore_reggroup
9885 || group == all_reggroup);
54483882
YQ
9886 else
9887 return default_register_reggroup_p (gdbarch, regnum, group);
9888}
9889
25f8c692
JL
9890\f
9891/* For backward-compatibility we allow two 'g' packet lengths with
9892 the remote protocol depending on whether FPA registers are
9893 supplied. M-profile targets do not have FPA registers, but some
9894 stubs already exist in the wild which use a 'g' packet which
9895 supplies them albeit with dummy values. The packet format which
9896 includes FPA registers should be considered deprecated for
9897 M-profile targets. */
9898
9899static void
9900arm_register_g_packet_guesses (struct gdbarch *gdbarch)
9901{
9902 if (gdbarch_tdep (gdbarch)->is_m)
9903 {
9904 /* If we know from the executable this is an M-profile target,
9905 cater for remote targets whose register set layout is the
9906 same as the FPA layout. */
9907 register_remote_g_packet_guess (gdbarch,
03145bf4 9908 /* r0-r12,sp,lr,pc; f0-f7; fps,xpsr */
25f8c692
JL
9909 (16 * INT_REGISTER_SIZE)
9910 + (8 * FP_REGISTER_SIZE)
9911 + (2 * INT_REGISTER_SIZE),
9912 tdesc_arm_with_m_fpa_layout);
9913
9914 /* The regular M-profile layout. */
9915 register_remote_g_packet_guess (gdbarch,
9916 /* r0-r12,sp,lr,pc; xpsr */
9917 (16 * INT_REGISTER_SIZE)
9918 + INT_REGISTER_SIZE,
9919 tdesc_arm_with_m);
3184d3f9
JL
9920
9921 /* M-profile plus M4F VFP. */
9922 register_remote_g_packet_guess (gdbarch,
9923 /* r0-r12,sp,lr,pc; d0-d15; fpscr,xpsr */
9924 (16 * INT_REGISTER_SIZE)
9925 + (16 * VFP_REGISTER_SIZE)
9926 + (2 * INT_REGISTER_SIZE),
9927 tdesc_arm_with_m_vfp_d16);
25f8c692
JL
9928 }
9929
9930 /* Otherwise we don't have a useful guess. */
9931}
9932
70f80edf 9933\f
da3c6d4a
MS
9934/* Initialize the current architecture based on INFO. If possible,
9935 re-use an architecture from ARCHES, which is a list of
9936 architectures already created during this debugging session.
97e03143 9937
da3c6d4a
MS
9938 Called e.g. at program startup, when reading a core file, and when
9939 reading a binary file. */
97e03143 9940
39bbf761
RE
9941static struct gdbarch *
9942arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
9943{
97e03143 9944 struct gdbarch_tdep *tdep;
39bbf761 9945 struct gdbarch *gdbarch;
28e97307
DJ
9946 struct gdbarch_list *best_arch;
9947 enum arm_abi_kind arm_abi = arm_abi_global;
9948 enum arm_float_model fp_model = arm_fp_model;
123dc839 9949 struct tdesc_arch_data *tdesc_data = NULL;
9779414d 9950 int i, is_m = 0;
58d6951d
DJ
9951 int have_vfp_registers = 0, have_vfp_pseudos = 0, have_neon_pseudos = 0;
9952 int have_neon = 0;
ff6f572f 9953 int have_fpa_registers = 1;
9779414d
DJ
9954 const struct target_desc *tdesc = info.target_desc;
9955
9956 /* If we have an object to base this architecture on, try to determine
9957 its ABI. */
9958
9959 if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
9960 {
9961 int ei_osabi, e_flags;
9962
9963 switch (bfd_get_flavour (info.abfd))
9964 {
9965 case bfd_target_aout_flavour:
9966 /* Assume it's an old APCS-style ABI. */
9967 arm_abi = ARM_ABI_APCS;
9968 break;
9969
9970 case bfd_target_coff_flavour:
9971 /* Assume it's an old APCS-style ABI. */
9972 /* XXX WinCE? */
9973 arm_abi = ARM_ABI_APCS;
9974 break;
9975
9976 case bfd_target_elf_flavour:
9977 ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
9978 e_flags = elf_elfheader (info.abfd)->e_flags;
9979
9980 if (ei_osabi == ELFOSABI_ARM)
9981 {
9982 /* GNU tools used to use this value, but do not for EABI
9983 objects. There's nowhere to tag an EABI version
9984 anyway, so assume APCS. */
9985 arm_abi = ARM_ABI_APCS;
9986 }
9987 else if (ei_osabi == ELFOSABI_NONE)
9988 {
9989 int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
9990 int attr_arch, attr_profile;
9991
9992 switch (eabi_ver)
9993 {
9994 case EF_ARM_EABI_UNKNOWN:
9995 /* Assume GNU tools. */
9996 arm_abi = ARM_ABI_APCS;
9997 break;
9998
9999 case EF_ARM_EABI_VER4:
10000 case EF_ARM_EABI_VER5:
10001 arm_abi = ARM_ABI_AAPCS;
10002 /* EABI binaries default to VFP float ordering.
10003 They may also contain build attributes that can
10004 be used to identify if the VFP argument-passing
10005 ABI is in use. */
10006 if (fp_model == ARM_FLOAT_AUTO)
10007 {
10008#ifdef HAVE_ELF
10009 switch (bfd_elf_get_obj_attr_int (info.abfd,
10010 OBJ_ATTR_PROC,
10011 Tag_ABI_VFP_args))
10012 {
10013 case 0:
10014 /* "The user intended FP parameter/result
10015 passing to conform to AAPCS, base
10016 variant". */
10017 fp_model = ARM_FLOAT_SOFT_VFP;
10018 break;
10019 case 1:
10020 /* "The user intended FP parameter/result
10021 passing to conform to AAPCS, VFP
10022 variant". */
10023 fp_model = ARM_FLOAT_VFP;
10024 break;
10025 case 2:
10026 /* "The user intended FP parameter/result
10027 passing to conform to tool chain-specific
10028 conventions" - we don't know any such
10029 conventions, so leave it as "auto". */
10030 break;
10031 default:
10032 /* Attribute value not mentioned in the
10033 October 2008 ABI, so leave it as
10034 "auto". */
10035 break;
10036 }
10037#else
10038 fp_model = ARM_FLOAT_SOFT_VFP;
10039#endif
10040 }
10041 break;
10042
10043 default:
10044 /* Leave it as "auto". */
10045 warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
10046 break;
10047 }
10048
10049#ifdef HAVE_ELF
10050 /* Detect M-profile programs. This only works if the
10051 executable file includes build attributes; GCC does
10052 copy them to the executable, but e.g. RealView does
10053 not. */
10054 attr_arch = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10055 Tag_CPU_arch);
0963b4bd
MS
10056 attr_profile = bfd_elf_get_obj_attr_int (info.abfd,
10057 OBJ_ATTR_PROC,
9779414d
DJ
10058 Tag_CPU_arch_profile);
10059 /* GCC specifies the profile for v6-M; RealView only
10060 specifies the profile for architectures starting with
10061 V7 (as opposed to architectures with a tag
10062 numerically greater than TAG_CPU_ARCH_V7). */
10063 if (!tdesc_has_registers (tdesc)
10064 && (attr_arch == TAG_CPU_ARCH_V6_M
10065 || attr_arch == TAG_CPU_ARCH_V6S_M
10066 || attr_profile == 'M'))
25f8c692 10067 is_m = 1;
9779414d
DJ
10068#endif
10069 }
10070
10071 if (fp_model == ARM_FLOAT_AUTO)
10072 {
10073 int e_flags = elf_elfheader (info.abfd)->e_flags;
10074
10075 switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
10076 {
10077 case 0:
10078 /* Leave it as "auto". Strictly speaking this case
10079 means FPA, but almost nobody uses that now, and
10080 many toolchains fail to set the appropriate bits
10081 for the floating-point model they use. */
10082 break;
10083 case EF_ARM_SOFT_FLOAT:
10084 fp_model = ARM_FLOAT_SOFT_FPA;
10085 break;
10086 case EF_ARM_VFP_FLOAT:
10087 fp_model = ARM_FLOAT_VFP;
10088 break;
10089 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
10090 fp_model = ARM_FLOAT_SOFT_VFP;
10091 break;
10092 }
10093 }
10094
10095 if (e_flags & EF_ARM_BE8)
10096 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
10097
10098 break;
10099
10100 default:
10101 /* Leave it as "auto". */
10102 break;
10103 }
10104 }
123dc839
DJ
10105
10106 /* Check any target description for validity. */
9779414d 10107 if (tdesc_has_registers (tdesc))
123dc839
DJ
10108 {
10109 /* For most registers we require GDB's default names; but also allow
10110 the numeric names for sp / lr / pc, as a convenience. */
10111 static const char *const arm_sp_names[] = { "r13", "sp", NULL };
10112 static const char *const arm_lr_names[] = { "r14", "lr", NULL };
10113 static const char *const arm_pc_names[] = { "r15", "pc", NULL };
10114
10115 const struct tdesc_feature *feature;
58d6951d 10116 int valid_p;
123dc839 10117
9779414d 10118 feature = tdesc_find_feature (tdesc,
123dc839
DJ
10119 "org.gnu.gdb.arm.core");
10120 if (feature == NULL)
9779414d
DJ
10121 {
10122 feature = tdesc_find_feature (tdesc,
10123 "org.gnu.gdb.arm.m-profile");
10124 if (feature == NULL)
10125 return NULL;
10126 else
10127 is_m = 1;
10128 }
123dc839
DJ
10129
10130 tdesc_data = tdesc_data_alloc ();
10131
10132 valid_p = 1;
10133 for (i = 0; i < ARM_SP_REGNUM; i++)
10134 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
10135 arm_register_names[i]);
10136 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
10137 ARM_SP_REGNUM,
10138 arm_sp_names);
10139 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
10140 ARM_LR_REGNUM,
10141 arm_lr_names);
10142 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
10143 ARM_PC_REGNUM,
10144 arm_pc_names);
9779414d
DJ
10145 if (is_m)
10146 valid_p &= tdesc_numbered_register (feature, tdesc_data,
10147 ARM_PS_REGNUM, "xpsr");
10148 else
10149 valid_p &= tdesc_numbered_register (feature, tdesc_data,
10150 ARM_PS_REGNUM, "cpsr");
123dc839
DJ
10151
10152 if (!valid_p)
10153 {
10154 tdesc_data_cleanup (tdesc_data);
10155 return NULL;
10156 }
10157
9779414d 10158 feature = tdesc_find_feature (tdesc,
123dc839
DJ
10159 "org.gnu.gdb.arm.fpa");
10160 if (feature != NULL)
10161 {
10162 valid_p = 1;
10163 for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
10164 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
10165 arm_register_names[i]);
10166 if (!valid_p)
10167 {
10168 tdesc_data_cleanup (tdesc_data);
10169 return NULL;
10170 }
10171 }
ff6f572f
DJ
10172 else
10173 have_fpa_registers = 0;
10174
9779414d 10175 feature = tdesc_find_feature (tdesc,
ff6f572f
DJ
10176 "org.gnu.gdb.xscale.iwmmxt");
10177 if (feature != NULL)
10178 {
10179 static const char *const iwmmxt_names[] = {
10180 "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
10181 "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
10182 "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
10183 "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
10184 };
10185
10186 valid_p = 1;
10187 for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
10188 valid_p
10189 &= tdesc_numbered_register (feature, tdesc_data, i,
10190 iwmmxt_names[i - ARM_WR0_REGNUM]);
10191
10192 /* Check for the control registers, but do not fail if they
10193 are missing. */
10194 for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
10195 tdesc_numbered_register (feature, tdesc_data, i,
10196 iwmmxt_names[i - ARM_WR0_REGNUM]);
10197
10198 for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
10199 valid_p
10200 &= tdesc_numbered_register (feature, tdesc_data, i,
10201 iwmmxt_names[i - ARM_WR0_REGNUM]);
10202
10203 if (!valid_p)
10204 {
10205 tdesc_data_cleanup (tdesc_data);
10206 return NULL;
10207 }
10208 }
58d6951d
DJ
10209
10210 /* If we have a VFP unit, check whether the single precision registers
10211 are present. If not, then we will synthesize them as pseudo
10212 registers. */
9779414d 10213 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
10214 "org.gnu.gdb.arm.vfp");
10215 if (feature != NULL)
10216 {
10217 static const char *const vfp_double_names[] = {
10218 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
10219 "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
10220 "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
10221 "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
10222 };
10223
10224 /* Require the double precision registers. There must be either
10225 16 or 32. */
10226 valid_p = 1;
10227 for (i = 0; i < 32; i++)
10228 {
10229 valid_p &= tdesc_numbered_register (feature, tdesc_data,
10230 ARM_D0_REGNUM + i,
10231 vfp_double_names[i]);
10232 if (!valid_p)
10233 break;
10234 }
2b9e5ea6
UW
10235 if (!valid_p && i == 16)
10236 valid_p = 1;
58d6951d 10237
2b9e5ea6
UW
10238 /* Also require FPSCR. */
10239 valid_p &= tdesc_numbered_register (feature, tdesc_data,
10240 ARM_FPSCR_REGNUM, "fpscr");
10241 if (!valid_p)
58d6951d
DJ
10242 {
10243 tdesc_data_cleanup (tdesc_data);
10244 return NULL;
10245 }
10246
10247 if (tdesc_unnumbered_register (feature, "s0") == 0)
10248 have_vfp_pseudos = 1;
10249
10250 have_vfp_registers = 1;
10251
10252 /* If we have VFP, also check for NEON. The architecture allows
10253 NEON without VFP (integer vector operations only), but GDB
10254 does not support that. */
9779414d 10255 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
10256 "org.gnu.gdb.arm.neon");
10257 if (feature != NULL)
10258 {
10259 /* NEON requires 32 double-precision registers. */
10260 if (i != 32)
10261 {
10262 tdesc_data_cleanup (tdesc_data);
10263 return NULL;
10264 }
10265
10266 /* If there are quad registers defined by the stub, use
10267 their type; otherwise (normally) provide them with
10268 the default type. */
10269 if (tdesc_unnumbered_register (feature, "q0") == 0)
10270 have_neon_pseudos = 1;
10271
10272 have_neon = 1;
10273 }
10274 }
123dc839 10275 }
39bbf761 10276
28e97307
DJ
10277 /* If there is already a candidate, use it. */
10278 for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
10279 best_arch != NULL;
10280 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
10281 {
b8926edc
DJ
10282 if (arm_abi != ARM_ABI_AUTO
10283 && arm_abi != gdbarch_tdep (best_arch->gdbarch)->arm_abi)
28e97307
DJ
10284 continue;
10285
b8926edc
DJ
10286 if (fp_model != ARM_FLOAT_AUTO
10287 && fp_model != gdbarch_tdep (best_arch->gdbarch)->fp_model)
28e97307
DJ
10288 continue;
10289
58d6951d
DJ
10290 /* There are various other properties in tdep that we do not
10291 need to check here: those derived from a target description,
10292 since gdbarches with a different target description are
10293 automatically disqualified. */
10294
9779414d
DJ
10295 /* Do check is_m, though, since it might come from the binary. */
10296 if (is_m != gdbarch_tdep (best_arch->gdbarch)->is_m)
10297 continue;
10298
28e97307
DJ
10299 /* Found a match. */
10300 break;
10301 }
97e03143 10302
28e97307 10303 if (best_arch != NULL)
123dc839
DJ
10304 {
10305 if (tdesc_data != NULL)
10306 tdesc_data_cleanup (tdesc_data);
10307 return best_arch->gdbarch;
10308 }
28e97307
DJ
10309
10310 tdep = xcalloc (1, sizeof (struct gdbarch_tdep));
97e03143
RE
10311 gdbarch = gdbarch_alloc (&info, tdep);
10312
28e97307
DJ
10313 /* Record additional information about the architecture we are defining.
10314 These are gdbarch discriminators, like the OSABI. */
10315 tdep->arm_abi = arm_abi;
10316 tdep->fp_model = fp_model;
9779414d 10317 tdep->is_m = is_m;
ff6f572f 10318 tdep->have_fpa_registers = have_fpa_registers;
58d6951d
DJ
10319 tdep->have_vfp_registers = have_vfp_registers;
10320 tdep->have_vfp_pseudos = have_vfp_pseudos;
10321 tdep->have_neon_pseudos = have_neon_pseudos;
10322 tdep->have_neon = have_neon;
08216dd7 10323
25f8c692
JL
10324 arm_register_g_packet_guesses (gdbarch);
10325
08216dd7 10326 /* Breakpoints. */
9d4fde75 10327 switch (info.byte_order_for_code)
67255d04
RE
10328 {
10329 case BFD_ENDIAN_BIG:
66e810cd
RE
10330 tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
10331 tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
10332 tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
10333 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
10334
67255d04
RE
10335 break;
10336
10337 case BFD_ENDIAN_LITTLE:
66e810cd
RE
10338 tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
10339 tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
10340 tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
10341 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
10342
67255d04
RE
10343 break;
10344
10345 default:
10346 internal_error (__FILE__, __LINE__,
edefbb7c 10347 _("arm_gdbarch_init: bad byte order for float format"));
67255d04
RE
10348 }
10349
d7b486e7
RE
10350 /* On ARM targets char defaults to unsigned. */
10351 set_gdbarch_char_signed (gdbarch, 0);
10352
cca44b1b
JB
10353 /* Note: for displaced stepping, this includes the breakpoint, and one word
10354 of additional scratch space. This setting isn't used for anything beside
10355 displaced stepping at present. */
10356 set_gdbarch_max_insn_length (gdbarch, 4 * DISPLACED_MODIFIED_INSNS);
10357
9df628e0 10358 /* This should be low enough for everything. */
97e03143 10359 tdep->lowest_pc = 0x20;
94c30b78 10360 tdep->jb_pc = -1; /* Longjump support not enabled by default. */
97e03143 10361
7c00367c
MK
10362 /* The default, for both APCS and AAPCS, is to return small
10363 structures in registers. */
10364 tdep->struct_return = reg_struct_return;
10365
2dd604e7 10366 set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
f53f0d0b 10367 set_gdbarch_frame_align (gdbarch, arm_frame_align);
39bbf761 10368
756fe439
DJ
10369 set_gdbarch_write_pc (gdbarch, arm_write_pc);
10370
148754e5 10371 /* Frame handling. */
a262aec2 10372 set_gdbarch_dummy_id (gdbarch, arm_dummy_id);
eb5492fa
DJ
10373 set_gdbarch_unwind_pc (gdbarch, arm_unwind_pc);
10374 set_gdbarch_unwind_sp (gdbarch, arm_unwind_sp);
10375
eb5492fa 10376 frame_base_set_default (gdbarch, &arm_normal_base);
148754e5 10377
34e8f22d 10378 /* Address manipulation. */
34e8f22d
RE
10379 set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
10380
34e8f22d
RE
10381 /* Advance PC across function entry code. */
10382 set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
10383
4024ca99
UW
10384 /* Detect whether PC is in function epilogue. */
10385 set_gdbarch_in_function_epilogue_p (gdbarch, arm_in_function_epilogue_p);
10386
190dce09
UW
10387 /* Skip trampolines. */
10388 set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
10389
34e8f22d
RE
10390 /* The stack grows downward. */
10391 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
10392
10393 /* Breakpoint manipulation. */
10394 set_gdbarch_breakpoint_from_pc (gdbarch, arm_breakpoint_from_pc);
177321bd
DJ
10395 set_gdbarch_remote_breakpoint_from_pc (gdbarch,
10396 arm_remote_breakpoint_from_pc);
34e8f22d
RE
10397
10398 /* Information about registers, etc. */
34e8f22d
RE
10399 set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
10400 set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
ff6f572f 10401 set_gdbarch_num_regs (gdbarch, ARM_NUM_REGS);
7a5ea0d4 10402 set_gdbarch_register_type (gdbarch, arm_register_type);
54483882 10403 set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);
34e8f22d 10404
ff6f572f
DJ
10405 /* This "info float" is FPA-specific. Use the generic version if we
10406 do not have FPA. */
10407 if (gdbarch_tdep (gdbarch)->have_fpa_registers)
10408 set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
10409
26216b98 10410 /* Internal <-> external register number maps. */
ff6f572f 10411 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
26216b98
AC
10412 set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
10413
34e8f22d
RE
10414 set_gdbarch_register_name (gdbarch, arm_register_name);
10415
10416 /* Returning results. */
2af48f68 10417 set_gdbarch_return_value (gdbarch, arm_return_value);
34e8f22d 10418
03d48a7d
RE
10419 /* Disassembly. */
10420 set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
10421
34e8f22d
RE
10422 /* Minsymbol frobbing. */
10423 set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
10424 set_gdbarch_coff_make_msymbol_special (gdbarch,
10425 arm_coff_make_msymbol_special);
60c5725c 10426 set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
34e8f22d 10427
f9d67f43
DJ
10428 /* Thumb-2 IT block support. */
10429 set_gdbarch_adjust_breakpoint_address (gdbarch,
10430 arm_adjust_breakpoint_address);
10431
0d5de010
DJ
10432 /* Virtual tables. */
10433 set_gdbarch_vbit_in_delta (gdbarch, 1);
10434
97e03143 10435 /* Hook in the ABI-specific overrides, if they have been registered. */
4be87837 10436 gdbarch_init_osabi (info, gdbarch);
97e03143 10437
b39cc962
DJ
10438 dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
10439
eb5492fa 10440 /* Add some default predicates. */
2ae28aa9
YQ
10441 if (is_m)
10442 frame_unwind_append_unwinder (gdbarch, &arm_m_exception_unwind);
a262aec2
DJ
10443 frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
10444 dwarf2_append_unwinders (gdbarch);
0e9e9abd 10445 frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
a262aec2 10446 frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
eb5492fa 10447
97e03143
RE
10448 /* Now we have tuned the configuration, set a few final things,
10449 based on what the OS ABI has told us. */
10450
b8926edc
DJ
10451 /* If the ABI is not otherwise marked, assume the old GNU APCS. EABI
10452 binaries are always marked. */
10453 if (tdep->arm_abi == ARM_ABI_AUTO)
10454 tdep->arm_abi = ARM_ABI_APCS;
10455
e3039479
UW
10456 /* Watchpoints are not steppable. */
10457 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
10458
b8926edc
DJ
10459 /* We used to default to FPA for generic ARM, but almost nobody
10460 uses that now, and we now provide a way for the user to force
10461 the model. So default to the most useful variant. */
10462 if (tdep->fp_model == ARM_FLOAT_AUTO)
10463 tdep->fp_model = ARM_FLOAT_SOFT_FPA;
10464
9df628e0
RE
10465 if (tdep->jb_pc >= 0)
10466 set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
10467
08216dd7 10468 /* Floating point sizes and format. */
8da61cc4 10469 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
b8926edc 10470 if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
08216dd7 10471 {
8da61cc4
DJ
10472 set_gdbarch_double_format
10473 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10474 set_gdbarch_long_double_format
10475 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10476 }
10477 else
10478 {
10479 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
10480 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
08216dd7
RE
10481 }
10482
58d6951d
DJ
10483 if (have_vfp_pseudos)
10484 {
10485 /* NOTE: These are the only pseudo registers used by
10486 the ARM target at the moment. If more are added, a
10487 little more care in numbering will be needed. */
10488
10489 int num_pseudos = 32;
10490 if (have_neon_pseudos)
10491 num_pseudos += 16;
10492 set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
10493 set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
10494 set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
10495 }
10496
123dc839 10497 if (tdesc_data)
58d6951d
DJ
10498 {
10499 set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
10500
9779414d 10501 tdesc_use_registers (gdbarch, tdesc, tdesc_data);
58d6951d
DJ
10502
10503 /* Override tdesc_register_type to adjust the types of VFP
10504 registers for NEON. */
10505 set_gdbarch_register_type (gdbarch, arm_register_type);
10506 }
123dc839
DJ
10507
10508 /* Add standard register aliases. We add aliases even for those
10509 nanes which are used by the current architecture - it's simpler,
10510 and does no harm, since nothing ever lists user registers. */
10511 for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
10512 user_reg_add (gdbarch, arm_register_aliases[i].name,
10513 value_of_arm_user_reg, &arm_register_aliases[i].regnum);
10514
39bbf761
RE
10515 return gdbarch;
10516}
10517
97e03143 10518static void
2af46ca0 10519arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
97e03143 10520{
2af46ca0 10521 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
97e03143
RE
10522
10523 if (tdep == NULL)
10524 return;
10525
edefbb7c 10526 fprintf_unfiltered (file, _("arm_dump_tdep: Lowest pc = 0x%lx"),
97e03143
RE
10527 (unsigned long) tdep->lowest_pc);
10528}
10529
a78f21af
AC
10530extern initialize_file_ftype _initialize_arm_tdep; /* -Wmissing-prototypes */
10531
c906108c 10532void
ed9a39eb 10533_initialize_arm_tdep (void)
c906108c 10534{
bc90b915
FN
10535 struct ui_file *stb;
10536 long length;
26304000 10537 struct cmd_list_element *new_set, *new_show;
53904c9e
AC
10538 const char *setname;
10539 const char *setdesc;
4bd7b427 10540 const char *const *regnames;
bc90b915
FN
10541 int numregs, i, j;
10542 static char *helptext;
edefbb7c
AC
10543 char regdesc[1024], *rdptr = regdesc;
10544 size_t rest = sizeof (regdesc);
085dd6e6 10545
42cf1509 10546 gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
97e03143 10547
60c5725c 10548 arm_objfile_data_key
c1bd65d0 10549 = register_objfile_data_with_cleanup (NULL, arm_objfile_data_free);
60c5725c 10550
0e9e9abd
UW
10551 /* Add ourselves to objfile event chain. */
10552 observer_attach_new_objfile (arm_exidx_new_objfile);
10553 arm_exidx_data_key
10554 = register_objfile_data_with_cleanup (NULL, arm_exidx_data_free);
10555
70f80edf
JT
10556 /* Register an ELF OS ABI sniffer for ARM binaries. */
10557 gdbarch_register_osabi_sniffer (bfd_arch_arm,
10558 bfd_target_elf_flavour,
10559 arm_elf_osabi_sniffer);
10560
9779414d
DJ
10561 /* Initialize the standard target descriptions. */
10562 initialize_tdesc_arm_with_m ();
25f8c692 10563 initialize_tdesc_arm_with_m_fpa_layout ();
3184d3f9 10564 initialize_tdesc_arm_with_m_vfp_d16 ();
ef7e8358
UW
10565 initialize_tdesc_arm_with_iwmmxt ();
10566 initialize_tdesc_arm_with_vfpv2 ();
10567 initialize_tdesc_arm_with_vfpv3 ();
10568 initialize_tdesc_arm_with_neon ();
9779414d 10569
94c30b78 10570 /* Get the number of possible sets of register names defined in opcodes. */
afd7eef0
RE
10571 num_disassembly_options = get_arm_regname_num_options ();
10572
10573 /* Add root prefix command for all "set arm"/"show arm" commands. */
10574 add_prefix_cmd ("arm", no_class, set_arm_command,
edefbb7c 10575 _("Various ARM-specific commands."),
afd7eef0
RE
10576 &setarmcmdlist, "set arm ", 0, &setlist);
10577
10578 add_prefix_cmd ("arm", no_class, show_arm_command,
edefbb7c 10579 _("Various ARM-specific commands."),
afd7eef0 10580 &showarmcmdlist, "show arm ", 0, &showlist);
bc90b915 10581
94c30b78 10582 /* Sync the opcode insn printer with our register viewer. */
bc90b915 10583 parse_arm_disassembler_option ("reg-names-std");
c5aa993b 10584
eefe576e
AC
10585 /* Initialize the array that will be passed to
10586 add_setshow_enum_cmd(). */
afd7eef0
RE
10587 valid_disassembly_styles
10588 = xmalloc ((num_disassembly_options + 1) * sizeof (char *));
10589 for (i = 0; i < num_disassembly_options; i++)
bc90b915
FN
10590 {
10591 numregs = get_arm_regnames (i, &setname, &setdesc, &regnames);
afd7eef0 10592 valid_disassembly_styles[i] = setname;
edefbb7c
AC
10593 length = snprintf (rdptr, rest, "%s - %s\n", setname, setdesc);
10594 rdptr += length;
10595 rest -= length;
123dc839
DJ
10596 /* When we find the default names, tell the disassembler to use
10597 them. */
bc90b915
FN
10598 if (!strcmp (setname, "std"))
10599 {
afd7eef0 10600 disassembly_style = setname;
bc90b915
FN
10601 set_arm_regname_option (i);
10602 }
10603 }
94c30b78 10604 /* Mark the end of valid options. */
afd7eef0 10605 valid_disassembly_styles[num_disassembly_options] = NULL;
c906108c 10606
edefbb7c
AC
10607 /* Create the help text. */
10608 stb = mem_fileopen ();
10609 fprintf_unfiltered (stb, "%s%s%s",
10610 _("The valid values are:\n"),
10611 regdesc,
10612 _("The default is \"std\"."));
759ef836 10613 helptext = ui_file_xstrdup (stb, NULL);
bc90b915 10614 ui_file_delete (stb);
ed9a39eb 10615
edefbb7c
AC
10616 add_setshow_enum_cmd("disassembler", no_class,
10617 valid_disassembly_styles, &disassembly_style,
10618 _("Set the disassembly style."),
10619 _("Show the disassembly style."),
10620 helptext,
2c5b56ce 10621 set_disassembly_style_sfunc,
0963b4bd
MS
10622 NULL, /* FIXME: i18n: The disassembly style is
10623 \"%s\". */
7376b4c2 10624 &setarmcmdlist, &showarmcmdlist);
edefbb7c
AC
10625
10626 add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
10627 _("Set usage of ARM 32-bit mode."),
10628 _("Show usage of ARM 32-bit mode."),
10629 _("When off, a 26-bit PC will be used."),
2c5b56ce 10630 NULL,
0963b4bd
MS
10631 NULL, /* FIXME: i18n: Usage of ARM 32-bit
10632 mode is %s. */
26304000 10633 &setarmcmdlist, &showarmcmdlist);
c906108c 10634
fd50bc42 10635 /* Add a command to allow the user to force the FPU model. */
edefbb7c
AC
10636 add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
10637 _("Set the floating point type."),
10638 _("Show the floating point type."),
10639 _("auto - Determine the FP typefrom the OS-ABI.\n\
10640softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
10641fpa - FPA co-processor (GCC compiled).\n\
10642softvfp - Software FP with pure-endian doubles.\n\
10643vfp - VFP co-processor."),
edefbb7c 10644 set_fp_model_sfunc, show_fp_model,
7376b4c2 10645 &setarmcmdlist, &showarmcmdlist);
fd50bc42 10646
28e97307
DJ
10647 /* Add a command to allow the user to force the ABI. */
10648 add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
10649 _("Set the ABI."),
10650 _("Show the ABI."),
10651 NULL, arm_set_abi, arm_show_abi,
10652 &setarmcmdlist, &showarmcmdlist);
10653
0428b8f5
DJ
10654 /* Add two commands to allow the user to force the assumed
10655 execution mode. */
10656 add_setshow_enum_cmd ("fallback-mode", class_support,
10657 arm_mode_strings, &arm_fallback_mode_string,
10658 _("Set the mode assumed when symbols are unavailable."),
10659 _("Show the mode assumed when symbols are unavailable."),
10660 NULL, NULL, arm_show_fallback_mode,
10661 &setarmcmdlist, &showarmcmdlist);
10662 add_setshow_enum_cmd ("force-mode", class_support,
10663 arm_mode_strings, &arm_force_mode_string,
10664 _("Set the mode assumed even when symbols are available."),
10665 _("Show the mode assumed even when symbols are available."),
10666 NULL, NULL, arm_show_force_mode,
10667 &setarmcmdlist, &showarmcmdlist);
10668
6529d2dd 10669 /* Debugging flag. */
edefbb7c
AC
10670 add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
10671 _("Set ARM debugging."),
10672 _("Show ARM debugging."),
10673 _("When on, arm-specific debugging is enabled."),
2c5b56ce 10674 NULL,
7915a72c 10675 NULL, /* FIXME: i18n: "ARM debugging is %s. */
26304000 10676 &setdebuglist, &showdebuglist);
c906108c 10677}
72508ac0
PO
10678
10679/* ARM-reversible process record data structures. */
10680
10681#define ARM_INSN_SIZE_BYTES 4
10682#define THUMB_INSN_SIZE_BYTES 2
10683#define THUMB2_INSN_SIZE_BYTES 4
10684
10685
71e396f9
LM
10686/* Position of the bit within a 32-bit ARM instruction
10687 that defines whether the instruction is a load or store. */
72508ac0
PO
10688#define INSN_S_L_BIT_NUM 20
10689
10690#define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
10691 do \
10692 { \
10693 unsigned int reg_len = LENGTH; \
10694 if (reg_len) \
10695 { \
10696 REGS = XNEWVEC (uint32_t, reg_len); \
10697 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
10698 } \
10699 } \
10700 while (0)
10701
10702#define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
10703 do \
10704 { \
10705 unsigned int mem_len = LENGTH; \
10706 if (mem_len) \
10707 { \
10708 MEMS = XNEWVEC (struct arm_mem_r, mem_len); \
10709 memcpy(&MEMS->len, &RECORD_BUF[0], \
10710 sizeof(struct arm_mem_r) * LENGTH); \
10711 } \
10712 } \
10713 while (0)
10714
10715/* Checks whether insn is already recorded or yet to be decoded. (boolean expression). */
10716#define INSN_RECORDED(ARM_RECORD) \
10717 (0 != (ARM_RECORD)->reg_rec_count || 0 != (ARM_RECORD)->mem_rec_count)
10718
10719/* ARM memory record structure. */
10720struct arm_mem_r
10721{
10722 uint32_t len; /* Record length. */
bfbbec00 10723 uint32_t addr; /* Memory address. */
72508ac0
PO
10724};
10725
10726/* ARM instruction record contains opcode of current insn
10727 and execution state (before entry to decode_insn()),
10728 contains list of to-be-modified registers and
10729 memory blocks (on return from decode_insn()). */
10730
10731typedef struct insn_decode_record_t
10732{
10733 struct gdbarch *gdbarch;
10734 struct regcache *regcache;
10735 CORE_ADDR this_addr; /* Address of the insn being decoded. */
10736 uint32_t arm_insn; /* Should accommodate thumb. */
10737 uint32_t cond; /* Condition code. */
10738 uint32_t opcode; /* Insn opcode. */
10739 uint32_t decode; /* Insn decode bits. */
10740 uint32_t mem_rec_count; /* No of mem records. */
10741 uint32_t reg_rec_count; /* No of reg records. */
10742 uint32_t *arm_regs; /* Registers to be saved for this record. */
10743 struct arm_mem_r *arm_mems; /* Memory to be saved for this record. */
10744} insn_decode_record;
10745
10746
10747/* Checks ARM SBZ and SBO mandatory fields. */
10748
10749static int
10750sbo_sbz (uint32_t insn, uint32_t bit_num, uint32_t len, uint32_t sbo)
10751{
10752 uint32_t ones = bits (insn, bit_num - 1, (bit_num -1) + (len - 1));
10753
10754 if (!len)
10755 return 1;
10756
10757 if (!sbo)
10758 ones = ~ones;
10759
10760 while (ones)
10761 {
10762 if (!(ones & sbo))
10763 {
10764 return 0;
10765 }
10766 ones = ones >> 1;
10767 }
10768 return 1;
10769}
10770
c6ec2b30
OJ
10771enum arm_record_result
10772{
10773 ARM_RECORD_SUCCESS = 0,
10774 ARM_RECORD_FAILURE = 1
10775};
10776
72508ac0
PO
10777typedef enum
10778{
10779 ARM_RECORD_STRH=1,
10780 ARM_RECORD_STRD
10781} arm_record_strx_t;
10782
10783typedef enum
10784{
10785 ARM_RECORD=1,
10786 THUMB_RECORD,
10787 THUMB2_RECORD
10788} record_type_t;
10789
10790
10791static int
10792arm_record_strx (insn_decode_record *arm_insn_r, uint32_t *record_buf,
10793 uint32_t *record_buf_mem, arm_record_strx_t str_type)
10794{
10795
10796 struct regcache *reg_cache = arm_insn_r->regcache;
10797 ULONGEST u_regval[2]= {0};
10798
10799 uint32_t reg_src1 = 0, reg_src2 = 0;
10800 uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
10801 uint32_t opcode1 = 0;
10802
10803 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10804 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10805 opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
10806
10807
10808 if (14 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
10809 {
10810 /* 1) Handle misc store, immediate offset. */
10811 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
10812 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
10813 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10814 regcache_raw_read_unsigned (reg_cache, reg_src1,
10815 &u_regval[0]);
10816 if (ARM_PC_REGNUM == reg_src1)
10817 {
10818 /* If R15 was used as Rn, hence current PC+8. */
10819 u_regval[0] = u_regval[0] + 8;
10820 }
10821 offset_8 = (immed_high << 4) | immed_low;
10822 /* Calculate target store address. */
10823 if (14 == arm_insn_r->opcode)
10824 {
10825 tgt_mem_addr = u_regval[0] + offset_8;
10826 }
10827 else
10828 {
10829 tgt_mem_addr = u_regval[0] - offset_8;
10830 }
10831 if (ARM_RECORD_STRH == str_type)
10832 {
10833 record_buf_mem[0] = 2;
10834 record_buf_mem[1] = tgt_mem_addr;
10835 arm_insn_r->mem_rec_count = 1;
10836 }
10837 else if (ARM_RECORD_STRD == str_type)
10838 {
10839 record_buf_mem[0] = 4;
10840 record_buf_mem[1] = tgt_mem_addr;
10841 record_buf_mem[2] = 4;
10842 record_buf_mem[3] = tgt_mem_addr + 4;
10843 arm_insn_r->mem_rec_count = 2;
10844 }
10845 }
10846 else if (12 == arm_insn_r->opcode || 8 == arm_insn_r->opcode)
10847 {
10848 /* 2) Store, register offset. */
10849 /* Get Rm. */
10850 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10851 /* Get Rn. */
10852 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10853 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10854 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10855 if (15 == reg_src2)
10856 {
10857 /* If R15 was used as Rn, hence current PC+8. */
10858 u_regval[0] = u_regval[0] + 8;
10859 }
10860 /* Calculate target store address, Rn +/- Rm, register offset. */
10861 if (12 == arm_insn_r->opcode)
10862 {
10863 tgt_mem_addr = u_regval[0] + u_regval[1];
10864 }
10865 else
10866 {
10867 tgt_mem_addr = u_regval[1] - u_regval[0];
10868 }
10869 if (ARM_RECORD_STRH == str_type)
10870 {
10871 record_buf_mem[0] = 2;
10872 record_buf_mem[1] = tgt_mem_addr;
10873 arm_insn_r->mem_rec_count = 1;
10874 }
10875 else if (ARM_RECORD_STRD == str_type)
10876 {
10877 record_buf_mem[0] = 4;
10878 record_buf_mem[1] = tgt_mem_addr;
10879 record_buf_mem[2] = 4;
10880 record_buf_mem[3] = tgt_mem_addr + 4;
10881 arm_insn_r->mem_rec_count = 2;
10882 }
10883 }
10884 else if (11 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
10885 || 2 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
10886 {
10887 /* 3) Store, immediate pre-indexed. */
10888 /* 5) Store, immediate post-indexed. */
10889 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
10890 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
10891 offset_8 = (immed_high << 4) | immed_low;
10892 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10893 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10894 /* Calculate target store address, Rn +/- Rm, register offset. */
10895 if (15 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
10896 {
10897 tgt_mem_addr = u_regval[0] + offset_8;
10898 }
10899 else
10900 {
10901 tgt_mem_addr = u_regval[0] - offset_8;
10902 }
10903 if (ARM_RECORD_STRH == str_type)
10904 {
10905 record_buf_mem[0] = 2;
10906 record_buf_mem[1] = tgt_mem_addr;
10907 arm_insn_r->mem_rec_count = 1;
10908 }
10909 else if (ARM_RECORD_STRD == str_type)
10910 {
10911 record_buf_mem[0] = 4;
10912 record_buf_mem[1] = tgt_mem_addr;
10913 record_buf_mem[2] = 4;
10914 record_buf_mem[3] = tgt_mem_addr + 4;
10915 arm_insn_r->mem_rec_count = 2;
10916 }
10917 /* Record Rn also as it changes. */
10918 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
10919 arm_insn_r->reg_rec_count = 1;
10920 }
10921 else if (9 == arm_insn_r->opcode || 13 == arm_insn_r->opcode
10922 || 0 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
10923 {
10924 /* 4) Store, register pre-indexed. */
10925 /* 6) Store, register post -indexed. */
10926 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10927 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10928 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10929 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10930 /* Calculate target store address, Rn +/- Rm, register offset. */
10931 if (13 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
10932 {
10933 tgt_mem_addr = u_regval[0] + u_regval[1];
10934 }
10935 else
10936 {
10937 tgt_mem_addr = u_regval[1] - u_regval[0];
10938 }
10939 if (ARM_RECORD_STRH == str_type)
10940 {
10941 record_buf_mem[0] = 2;
10942 record_buf_mem[1] = tgt_mem_addr;
10943 arm_insn_r->mem_rec_count = 1;
10944 }
10945 else if (ARM_RECORD_STRD == str_type)
10946 {
10947 record_buf_mem[0] = 4;
10948 record_buf_mem[1] = tgt_mem_addr;
10949 record_buf_mem[2] = 4;
10950 record_buf_mem[3] = tgt_mem_addr + 4;
10951 arm_insn_r->mem_rec_count = 2;
10952 }
10953 /* Record Rn also as it changes. */
10954 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
10955 arm_insn_r->reg_rec_count = 1;
10956 }
10957 return 0;
10958}
10959
10960/* Handling ARM extension space insns. */
10961
10962static int
10963arm_record_extension_space (insn_decode_record *arm_insn_r)
10964{
10965 uint32_t ret = 0; /* Return value: -1:record failure ; 0:success */
10966 uint32_t opcode1 = 0, opcode2 = 0, insn_op1 = 0;
10967 uint32_t record_buf[8], record_buf_mem[8];
10968 uint32_t reg_src1 = 0;
10969 uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
10970 struct regcache *reg_cache = arm_insn_r->regcache;
10971 ULONGEST u_regval = 0;
10972
10973 gdb_assert (!INSN_RECORDED(arm_insn_r));
10974 /* Handle unconditional insn extension space. */
10975
10976 opcode1 = bits (arm_insn_r->arm_insn, 20, 27);
10977 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
10978 if (arm_insn_r->cond)
10979 {
10980 /* PLD has no affect on architectural state, it just affects
10981 the caches. */
10982 if (5 == ((opcode1 & 0xE0) >> 5))
10983 {
10984 /* BLX(1) */
10985 record_buf[0] = ARM_PS_REGNUM;
10986 record_buf[1] = ARM_LR_REGNUM;
10987 arm_insn_r->reg_rec_count = 2;
10988 }
10989 /* STC2, LDC2, MCR2, MRC2, CDP2: <TBD>, co-processor insn. */
10990 }
10991
10992
10993 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
10994 if (3 == opcode1 && bit (arm_insn_r->arm_insn, 4))
10995 {
10996 ret = -1;
10997 /* Undefined instruction on ARM V5; need to handle if later
10998 versions define it. */
10999 }
11000
11001 opcode1 = bits (arm_insn_r->arm_insn, 24, 27);
11002 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
11003 insn_op1 = bits (arm_insn_r->arm_insn, 20, 23);
11004
11005 /* Handle arithmetic insn extension space. */
11006 if (!opcode1 && 9 == opcode2 && 1 != arm_insn_r->cond
11007 && !INSN_RECORDED(arm_insn_r))
11008 {
11009 /* Handle MLA(S) and MUL(S). */
11010 if (0 <= insn_op1 && 3 >= insn_op1)
11011 {
11012 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11013 record_buf[1] = ARM_PS_REGNUM;
11014 arm_insn_r->reg_rec_count = 2;
11015 }
11016 else if (4 <= insn_op1 && 15 >= insn_op1)
11017 {
11018 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
11019 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11020 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11021 record_buf[2] = ARM_PS_REGNUM;
11022 arm_insn_r->reg_rec_count = 3;
11023 }
11024 }
11025
11026 opcode1 = bits (arm_insn_r->arm_insn, 26, 27);
11027 opcode2 = bits (arm_insn_r->arm_insn, 23, 24);
11028 insn_op1 = bits (arm_insn_r->arm_insn, 21, 22);
11029
11030 /* Handle control insn extension space. */
11031
11032 if (!opcode1 && 2 == opcode2 && !bit (arm_insn_r->arm_insn, 20)
11033 && 1 != arm_insn_r->cond && !INSN_RECORDED(arm_insn_r))
11034 {
11035 if (!bit (arm_insn_r->arm_insn,25))
11036 {
11037 if (!bits (arm_insn_r->arm_insn, 4, 7))
11038 {
11039 if ((0 == insn_op1) || (2 == insn_op1))
11040 {
11041 /* MRS. */
11042 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11043 arm_insn_r->reg_rec_count = 1;
11044 }
11045 else if (1 == insn_op1)
11046 {
11047 /* CSPR is going to be changed. */
11048 record_buf[0] = ARM_PS_REGNUM;
11049 arm_insn_r->reg_rec_count = 1;
11050 }
11051 else if (3 == insn_op1)
11052 {
11053 /* SPSR is going to be changed. */
11054 /* We need to get SPSR value, which is yet to be done. */
11055 printf_unfiltered (_("Process record does not support "
11056 "instruction 0x%0x at address %s.\n"),
11057 arm_insn_r->arm_insn,
11058 paddress (arm_insn_r->gdbarch,
11059 arm_insn_r->this_addr));
11060 return -1;
11061 }
11062 }
11063 else if (1 == bits (arm_insn_r->arm_insn, 4, 7))
11064 {
11065 if (1 == insn_op1)
11066 {
11067 /* BX. */
11068 record_buf[0] = ARM_PS_REGNUM;
11069 arm_insn_r->reg_rec_count = 1;
11070 }
11071 else if (3 == insn_op1)
11072 {
11073 /* CLZ. */
11074 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11075 arm_insn_r->reg_rec_count = 1;
11076 }
11077 }
11078 else if (3 == bits (arm_insn_r->arm_insn, 4, 7))
11079 {
11080 /* BLX. */
11081 record_buf[0] = ARM_PS_REGNUM;
11082 record_buf[1] = ARM_LR_REGNUM;
11083 arm_insn_r->reg_rec_count = 2;
11084 }
11085 else if (5 == bits (arm_insn_r->arm_insn, 4, 7))
11086 {
11087 /* QADD, QSUB, QDADD, QDSUB */
11088 record_buf[0] = ARM_PS_REGNUM;
11089 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11090 arm_insn_r->reg_rec_count = 2;
11091 }
11092 else if (7 == bits (arm_insn_r->arm_insn, 4, 7))
11093 {
11094 /* BKPT. */
11095 record_buf[0] = ARM_PS_REGNUM;
11096 record_buf[1] = ARM_LR_REGNUM;
11097 arm_insn_r->reg_rec_count = 2;
11098
11099 /* Save SPSR also;how? */
11100 printf_unfiltered (_("Process record does not support "
11101 "instruction 0x%0x at address %s.\n"),
11102 arm_insn_r->arm_insn,
11103 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11104 return -1;
11105 }
11106 else if(8 == bits (arm_insn_r->arm_insn, 4, 7)
11107 || 10 == bits (arm_insn_r->arm_insn, 4, 7)
11108 || 12 == bits (arm_insn_r->arm_insn, 4, 7)
11109 || 14 == bits (arm_insn_r->arm_insn, 4, 7)
11110 )
11111 {
11112 if (0 == insn_op1 || 1 == insn_op1)
11113 {
11114 /* SMLA<x><y>, SMLAW<y>, SMULW<y>. */
11115 /* We dont do optimization for SMULW<y> where we
11116 need only Rd. */
11117 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11118 record_buf[1] = ARM_PS_REGNUM;
11119 arm_insn_r->reg_rec_count = 2;
11120 }
11121 else if (2 == insn_op1)
11122 {
11123 /* SMLAL<x><y>. */
11124 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11125 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
11126 arm_insn_r->reg_rec_count = 2;
11127 }
11128 else if (3 == insn_op1)
11129 {
11130 /* SMUL<x><y>. */
11131 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11132 arm_insn_r->reg_rec_count = 1;
11133 }
11134 }
11135 }
11136 else
11137 {
11138 /* MSR : immediate form. */
11139 if (1 == insn_op1)
11140 {
11141 /* CSPR is going to be changed. */
11142 record_buf[0] = ARM_PS_REGNUM;
11143 arm_insn_r->reg_rec_count = 1;
11144 }
11145 else if (3 == insn_op1)
11146 {
11147 /* SPSR is going to be changed. */
11148 /* we need to get SPSR value, which is yet to be done */
11149 printf_unfiltered (_("Process record does not support "
11150 "instruction 0x%0x at address %s.\n"),
11151 arm_insn_r->arm_insn,
11152 paddress (arm_insn_r->gdbarch,
11153 arm_insn_r->this_addr));
11154 return -1;
11155 }
11156 }
11157 }
11158
11159 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
11160 opcode2 = bits (arm_insn_r->arm_insn, 20, 24);
11161 insn_op1 = bits (arm_insn_r->arm_insn, 5, 6);
11162
11163 /* Handle load/store insn extension space. */
11164
11165 if (!opcode1 && bit (arm_insn_r->arm_insn, 7)
11166 && bit (arm_insn_r->arm_insn, 4) && 1 != arm_insn_r->cond
11167 && !INSN_RECORDED(arm_insn_r))
11168 {
11169 /* SWP/SWPB. */
11170 if (0 == insn_op1)
11171 {
11172 /* These insn, changes register and memory as well. */
11173 /* SWP or SWPB insn. */
11174 /* Get memory address given by Rn. */
11175 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11176 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11177 /* SWP insn ?, swaps word. */
11178 if (8 == arm_insn_r->opcode)
11179 {
11180 record_buf_mem[0] = 4;
11181 }
11182 else
11183 {
11184 /* SWPB insn, swaps only byte. */
11185 record_buf_mem[0] = 1;
11186 }
11187 record_buf_mem[1] = u_regval;
11188 arm_insn_r->mem_rec_count = 1;
11189 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11190 arm_insn_r->reg_rec_count = 1;
11191 }
11192 else if (1 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11193 {
11194 /* STRH. */
11195 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11196 ARM_RECORD_STRH);
11197 }
11198 else if (2 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11199 {
11200 /* LDRD. */
11201 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11202 record_buf[1] = record_buf[0] + 1;
11203 arm_insn_r->reg_rec_count = 2;
11204 }
11205 else if (3 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11206 {
11207 /* STRD. */
11208 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11209 ARM_RECORD_STRD);
11210 }
11211 else if (bit (arm_insn_r->arm_insn, 20) && insn_op1 <= 3)
11212 {
11213 /* LDRH, LDRSB, LDRSH. */
11214 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11215 arm_insn_r->reg_rec_count = 1;
11216 }
11217
11218 }
11219
11220 opcode1 = bits (arm_insn_r->arm_insn, 23, 27);
11221 if (24 == opcode1 && bit (arm_insn_r->arm_insn, 21)
11222 && !INSN_RECORDED(arm_insn_r))
11223 {
11224 ret = -1;
11225 /* Handle coprocessor insn extension space. */
11226 }
11227
11228 /* To be done for ARMv5 and later; as of now we return -1. */
11229 if (-1 == ret)
11230 printf_unfiltered (_("Process record does not support instruction x%0x "
11231 "at address %s.\n"),arm_insn_r->arm_insn,
11232 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11233
11234
11235 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11236 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11237
11238 return ret;
11239}
11240
11241/* Handling opcode 000 insns. */
11242
11243static int
11244arm_record_data_proc_misc_ld_str (insn_decode_record *arm_insn_r)
11245{
11246 struct regcache *reg_cache = arm_insn_r->regcache;
11247 uint32_t record_buf[8], record_buf_mem[8];
11248 ULONGEST u_regval[2] = {0};
11249
11250 uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
11251 uint32_t immed_high = 0, immed_low = 0, offset_8 = 0, tgt_mem_addr = 0;
11252 uint32_t opcode1 = 0;
11253
11254 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11255 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11256 opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
11257
11258 /* Data processing insn /multiply insn. */
11259 if (9 == arm_insn_r->decode
11260 && ((4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11261 || (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)))
11262 {
11263 /* Handle multiply instructions. */
11264 /* MLA, MUL, SMLAL, SMULL, UMLAL, UMULL. */
11265 if (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)
11266 {
11267 /* Handle MLA and MUL. */
11268 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11269 record_buf[1] = ARM_PS_REGNUM;
11270 arm_insn_r->reg_rec_count = 2;
11271 }
11272 else if (4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11273 {
11274 /* Handle SMLAL, SMULL, UMLAL, UMULL. */
11275 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11276 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11277 record_buf[2] = ARM_PS_REGNUM;
11278 arm_insn_r->reg_rec_count = 3;
11279 }
11280 }
11281 else if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11282 && (11 == arm_insn_r->decode || 13 == arm_insn_r->decode))
11283 {
11284 /* Handle misc load insns, as 20th bit (L = 1). */
11285 /* LDR insn has a capability to do branching, if
11286 MOV LR, PC is precceded by LDR insn having Rn as R15
11287 in that case, it emulates branch and link insn, and hence we
11288 need to save CSPR and PC as well. I am not sure this is right
11289 place; as opcode = 010 LDR insn make this happen, if R15 was
11290 used. */
11291 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11292 if (15 != reg_dest)
11293 {
11294 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11295 arm_insn_r->reg_rec_count = 1;
11296 }
11297 else
11298 {
11299 record_buf[0] = reg_dest;
11300 record_buf[1] = ARM_PS_REGNUM;
11301 arm_insn_r->reg_rec_count = 2;
11302 }
11303 }
11304 else if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11305 && sbo_sbz (arm_insn_r->arm_insn, 5, 12, 0)
11306 && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
11307 && 2 == bits (arm_insn_r->arm_insn, 20, 21))
11308 {
11309 /* Handle MSR insn. */
11310 if (9 == arm_insn_r->opcode)
11311 {
11312 /* CSPR is going to be changed. */
11313 record_buf[0] = ARM_PS_REGNUM;
11314 arm_insn_r->reg_rec_count = 1;
11315 }
11316 else
11317 {
11318 /* SPSR is going to be changed. */
11319 /* How to read SPSR value? */
11320 printf_unfiltered (_("Process record does not support instruction "
11321 "0x%0x at address %s.\n"),
11322 arm_insn_r->arm_insn,
11323 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11324 return -1;
11325 }
11326 }
11327 else if (9 == arm_insn_r->decode
11328 && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11329 && !bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11330 {
11331 /* Handling SWP, SWPB. */
11332 /* These insn, changes register and memory as well. */
11333 /* SWP or SWPB insn. */
11334
11335 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11336 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11337 /* SWP insn ?, swaps word. */
11338 if (8 == arm_insn_r->opcode)
11339 {
11340 record_buf_mem[0] = 4;
11341 }
11342 else
11343 {
11344 /* SWPB insn, swaps only byte. */
11345 record_buf_mem[0] = 1;
11346 }
11347 record_buf_mem[1] = u_regval[0];
11348 arm_insn_r->mem_rec_count = 1;
11349 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11350 arm_insn_r->reg_rec_count = 1;
11351 }
11352 else if (3 == arm_insn_r->decode && 0x12 == opcode1
11353 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11354 {
11355 /* Handle BLX, branch and link/exchange. */
11356 if (9 == arm_insn_r->opcode)
11357 {
11358 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm,
11359 and R14 stores the return address. */
11360 record_buf[0] = ARM_PS_REGNUM;
11361 record_buf[1] = ARM_LR_REGNUM;
11362 arm_insn_r->reg_rec_count = 2;
11363 }
11364 }
11365 else if (7 == arm_insn_r->decode && 0x12 == opcode1)
11366 {
11367 /* Handle enhanced software breakpoint insn, BKPT. */
11368 /* CPSR is changed to be executed in ARM state, disabling normal
11369 interrupts, entering abort mode. */
11370 /* According to high vector configuration PC is set. */
11371 /* user hit breakpoint and type reverse, in
11372 that case, we need to go back with previous CPSR and
11373 Program Counter. */
11374 record_buf[0] = ARM_PS_REGNUM;
11375 record_buf[1] = ARM_LR_REGNUM;
11376 arm_insn_r->reg_rec_count = 2;
11377
11378 /* Save SPSR also; how? */
11379 printf_unfiltered (_("Process record does not support instruction "
11380 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
11381 paddress (arm_insn_r->gdbarch,
11382 arm_insn_r->this_addr));
11383 return -1;
11384 }
11385 else if (11 == arm_insn_r->decode
11386 && !bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11387 {
11388 /* Handle enhanced store insns and DSP insns (e.g. LDRD). */
11389
11390 /* Handle str(x) insn */
11391 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11392 ARM_RECORD_STRH);
11393 }
11394 else if (1 == arm_insn_r->decode && 0x12 == opcode1
11395 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11396 {
11397 /* Handle BX, branch and link/exchange. */
11398 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm. */
11399 record_buf[0] = ARM_PS_REGNUM;
11400 arm_insn_r->reg_rec_count = 1;
11401 }
11402 else if (1 == arm_insn_r->decode && 0x16 == opcode1
11403 && sbo_sbz (arm_insn_r->arm_insn, 9, 4, 1)
11404 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1))
11405 {
11406 /* Count leading zeros: CLZ. */
11407 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11408 arm_insn_r->reg_rec_count = 1;
11409 }
11410 else if (!bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11411 && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11412 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1)
11413 && sbo_sbz (arm_insn_r->arm_insn, 1, 12, 0)
11414 )
11415 {
11416 /* Handle MRS insn. */
11417 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11418 arm_insn_r->reg_rec_count = 1;
11419 }
11420 else if (arm_insn_r->opcode <= 15)
11421 {
11422 /* Normal data processing insns. */
11423 /* Out of 11 shifter operands mode, all the insn modifies destination
11424 register, which is specified by 13-16 decode. */
11425 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11426 record_buf[1] = ARM_PS_REGNUM;
11427 arm_insn_r->reg_rec_count = 2;
11428 }
11429 else
11430 {
11431 return -1;
11432 }
11433
11434 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11435 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11436 return 0;
11437}
11438
11439/* Handling opcode 001 insns. */
11440
11441static int
11442arm_record_data_proc_imm (insn_decode_record *arm_insn_r)
11443{
11444 uint32_t record_buf[8], record_buf_mem[8];
11445
11446 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11447 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11448
11449 if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11450 && 2 == bits (arm_insn_r->arm_insn, 20, 21)
11451 && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
11452 )
11453 {
11454 /* Handle MSR insn. */
11455 if (9 == arm_insn_r->opcode)
11456 {
11457 /* CSPR is going to be changed. */
11458 record_buf[0] = ARM_PS_REGNUM;
11459 arm_insn_r->reg_rec_count = 1;
11460 }
11461 else
11462 {
11463 /* SPSR is going to be changed. */
11464 }
11465 }
11466 else if (arm_insn_r->opcode <= 15)
11467 {
11468 /* Normal data processing insns. */
11469 /* Out of 11 shifter operands mode, all the insn modifies destination
11470 register, which is specified by 13-16 decode. */
11471 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11472 record_buf[1] = ARM_PS_REGNUM;
11473 arm_insn_r->reg_rec_count = 2;
11474 }
11475 else
11476 {
11477 return -1;
11478 }
11479
11480 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11481 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11482 return 0;
11483}
11484
71e396f9 11485/* Handle ARM mode instructions with opcode 010. */
72508ac0
PO
11486
11487static int
11488arm_record_ld_st_imm_offset (insn_decode_record *arm_insn_r)
11489{
11490 struct regcache *reg_cache = arm_insn_r->regcache;
11491
71e396f9
LM
11492 uint32_t reg_base , reg_dest;
11493 uint32_t offset_12, tgt_mem_addr;
72508ac0 11494 uint32_t record_buf[8], record_buf_mem[8];
71e396f9
LM
11495 unsigned char wback;
11496 ULONGEST u_regval;
72508ac0 11497
71e396f9
LM
11498 /* Calculate wback. */
11499 wback = (bit (arm_insn_r->arm_insn, 24) == 0)
11500 || (bit (arm_insn_r->arm_insn, 21) == 1);
72508ac0 11501
71e396f9
LM
11502 arm_insn_r->reg_rec_count = 0;
11503 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0
PO
11504
11505 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11506 {
71e396f9
LM
11507 /* LDR (immediate), LDR (literal), LDRB (immediate), LDRB (literal), LDRBT
11508 and LDRT. */
11509
72508ac0 11510 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
71e396f9
LM
11511 record_buf[arm_insn_r->reg_rec_count++] = reg_dest;
11512
11513 /* The LDR instruction is capable of doing branching. If MOV LR, PC
11514 preceeds a LDR instruction having R15 as reg_base, it
11515 emulates a branch and link instruction, and hence we need to save
11516 CPSR and PC as well. */
11517 if (ARM_PC_REGNUM == reg_dest)
11518 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
11519
11520 /* If wback is true, also save the base register, which is going to be
11521 written to. */
11522 if (wback)
11523 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
11524 }
11525 else
11526 {
71e396f9
LM
11527 /* STR (immediate), STRB (immediate), STRBT and STRT. */
11528
72508ac0 11529 offset_12 = bits (arm_insn_r->arm_insn, 0, 11);
71e396f9
LM
11530 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
11531
11532 /* Handle bit U. */
72508ac0 11533 if (bit (arm_insn_r->arm_insn, 23))
71e396f9
LM
11534 {
11535 /* U == 1: Add the offset. */
11536 tgt_mem_addr = (uint32_t) u_regval + offset_12;
11537 }
72508ac0 11538 else
71e396f9
LM
11539 {
11540 /* U == 0: subtract the offset. */
11541 tgt_mem_addr = (uint32_t) u_regval - offset_12;
11542 }
11543
11544 /* Bit 22 tells us whether the store instruction writes 1 byte or 4
11545 bytes. */
11546 if (bit (arm_insn_r->arm_insn, 22))
11547 {
11548 /* STRB and STRBT: 1 byte. */
11549 record_buf_mem[0] = 1;
11550 }
11551 else
11552 {
11553 /* STR and STRT: 4 bytes. */
11554 record_buf_mem[0] = 4;
11555 }
11556
11557 /* Handle bit P. */
11558 if (bit (arm_insn_r->arm_insn, 24))
11559 record_buf_mem[1] = tgt_mem_addr;
11560 else
11561 record_buf_mem[1] = (uint32_t) u_regval;
72508ac0 11562
72508ac0
PO
11563 arm_insn_r->mem_rec_count = 1;
11564
71e396f9
LM
11565 /* If wback is true, also save the base register, which is going to be
11566 written to. */
11567 if (wback)
11568 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
11569 }
11570
11571 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11572 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11573 return 0;
11574}
11575
11576/* Handling opcode 011 insns. */
11577
11578static int
11579arm_record_ld_st_reg_offset (insn_decode_record *arm_insn_r)
11580{
11581 struct regcache *reg_cache = arm_insn_r->regcache;
11582
11583 uint32_t shift_imm = 0;
11584 uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
11585 uint32_t offset_12 = 0, tgt_mem_addr = 0;
11586 uint32_t record_buf[8], record_buf_mem[8];
11587
11588 LONGEST s_word;
11589 ULONGEST u_regval[2];
11590
11591 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11592 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11593
11594 /* Handle enhanced store insns and LDRD DSP insn,
11595 order begins according to addressing modes for store insns
11596 STRH insn. */
11597
11598 /* LDR or STR? */
11599 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11600 {
11601 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11602 /* LDR insn has a capability to do branching, if
11603 MOV LR, PC is precedded by LDR insn having Rn as R15
11604 in that case, it emulates branch and link insn, and hence we
11605 need to save CSPR and PC as well. */
11606 if (15 != reg_dest)
11607 {
11608 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11609 arm_insn_r->reg_rec_count = 1;
11610 }
11611 else
11612 {
11613 record_buf[0] = reg_dest;
11614 record_buf[1] = ARM_PS_REGNUM;
11615 arm_insn_r->reg_rec_count = 2;
11616 }
11617 }
11618 else
11619 {
11620 if (! bits (arm_insn_r->arm_insn, 4, 11))
11621 {
11622 /* Store insn, register offset and register pre-indexed,
11623 register post-indexed. */
11624 /* Get Rm. */
11625 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11626 /* Get Rn. */
11627 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11628 regcache_raw_read_unsigned (reg_cache, reg_src1
11629 , &u_regval[0]);
11630 regcache_raw_read_unsigned (reg_cache, reg_src2
11631 , &u_regval[1]);
11632 if (15 == reg_src2)
11633 {
11634 /* If R15 was used as Rn, hence current PC+8. */
11635 /* Pre-indexed mode doesnt reach here ; illegal insn. */
11636 u_regval[0] = u_regval[0] + 8;
11637 }
11638 /* Calculate target store address, Rn +/- Rm, register offset. */
11639 /* U == 1. */
11640 if (bit (arm_insn_r->arm_insn, 23))
11641 {
11642 tgt_mem_addr = u_regval[0] + u_regval[1];
11643 }
11644 else
11645 {
11646 tgt_mem_addr = u_regval[1] - u_regval[0];
11647 }
11648
11649 switch (arm_insn_r->opcode)
11650 {
11651 /* STR. */
11652 case 8:
11653 case 12:
11654 /* STR. */
11655 case 9:
11656 case 13:
11657 /* STRT. */
11658 case 1:
11659 case 5:
11660 /* STR. */
11661 case 0:
11662 case 4:
11663 record_buf_mem[0] = 4;
11664 break;
11665
11666 /* STRB. */
11667 case 10:
11668 case 14:
11669 /* STRB. */
11670 case 11:
11671 case 15:
11672 /* STRBT. */
11673 case 3:
11674 case 7:
11675 /* STRB. */
11676 case 2:
11677 case 6:
11678 record_buf_mem[0] = 1;
11679 break;
11680
11681 default:
11682 gdb_assert_not_reached ("no decoding pattern found");
11683 break;
11684 }
11685 record_buf_mem[1] = tgt_mem_addr;
11686 arm_insn_r->mem_rec_count = 1;
11687
11688 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11689 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11690 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11691 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11692 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11693 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11694 )
11695 {
11696 /* Rn is going to be changed in pre-indexed mode and
11697 post-indexed mode as well. */
11698 record_buf[0] = reg_src2;
11699 arm_insn_r->reg_rec_count = 1;
11700 }
11701 }
11702 else
11703 {
11704 /* Store insn, scaled register offset; scaled pre-indexed. */
11705 offset_12 = bits (arm_insn_r->arm_insn, 5, 6);
11706 /* Get Rm. */
11707 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11708 /* Get Rn. */
11709 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11710 /* Get shift_imm. */
11711 shift_imm = bits (arm_insn_r->arm_insn, 7, 11);
11712 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11713 regcache_raw_read_signed (reg_cache, reg_src1, &s_word);
11714 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11715 /* Offset_12 used as shift. */
11716 switch (offset_12)
11717 {
11718 case 0:
11719 /* Offset_12 used as index. */
11720 offset_12 = u_regval[0] << shift_imm;
11721 break;
11722
11723 case 1:
11724 offset_12 = (!shift_imm)?0:u_regval[0] >> shift_imm;
11725 break;
11726
11727 case 2:
11728 if (!shift_imm)
11729 {
11730 if (bit (u_regval[0], 31))
11731 {
11732 offset_12 = 0xFFFFFFFF;
11733 }
11734 else
11735 {
11736 offset_12 = 0;
11737 }
11738 }
11739 else
11740 {
11741 /* This is arithmetic shift. */
11742 offset_12 = s_word >> shift_imm;
11743 }
11744 break;
11745
11746 case 3:
11747 if (!shift_imm)
11748 {
11749 regcache_raw_read_unsigned (reg_cache, ARM_PS_REGNUM,
11750 &u_regval[1]);
11751 /* Get C flag value and shift it by 31. */
11752 offset_12 = (((bit (u_regval[1], 29)) << 31) \
11753 | (u_regval[0]) >> 1);
11754 }
11755 else
11756 {
11757 offset_12 = (u_regval[0] >> shift_imm) \
11758 | (u_regval[0] <<
11759 (sizeof(uint32_t) - shift_imm));
11760 }
11761 break;
11762
11763 default:
11764 gdb_assert_not_reached ("no decoding pattern found");
11765 break;
11766 }
11767
11768 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11769 /* bit U set. */
11770 if (bit (arm_insn_r->arm_insn, 23))
11771 {
11772 tgt_mem_addr = u_regval[1] + offset_12;
11773 }
11774 else
11775 {
11776 tgt_mem_addr = u_regval[1] - offset_12;
11777 }
11778
11779 switch (arm_insn_r->opcode)
11780 {
11781 /* STR. */
11782 case 8:
11783 case 12:
11784 /* STR. */
11785 case 9:
11786 case 13:
11787 /* STRT. */
11788 case 1:
11789 case 5:
11790 /* STR. */
11791 case 0:
11792 case 4:
11793 record_buf_mem[0] = 4;
11794 break;
11795
11796 /* STRB. */
11797 case 10:
11798 case 14:
11799 /* STRB. */
11800 case 11:
11801 case 15:
11802 /* STRBT. */
11803 case 3:
11804 case 7:
11805 /* STRB. */
11806 case 2:
11807 case 6:
11808 record_buf_mem[0] = 1;
11809 break;
11810
11811 default:
11812 gdb_assert_not_reached ("no decoding pattern found");
11813 break;
11814 }
11815 record_buf_mem[1] = tgt_mem_addr;
11816 arm_insn_r->mem_rec_count = 1;
11817
11818 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11819 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11820 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11821 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11822 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11823 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11824 )
11825 {
11826 /* Rn is going to be changed in register scaled pre-indexed
11827 mode,and scaled post indexed mode. */
11828 record_buf[0] = reg_src2;
11829 arm_insn_r->reg_rec_count = 1;
11830 }
11831 }
11832 }
11833
11834 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11835 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11836 return 0;
11837}
11838
71e396f9 11839/* Handle ARM mode instructions with opcode 100. */
72508ac0
PO
11840
11841static int
11842arm_record_ld_st_multiple (insn_decode_record *arm_insn_r)
11843{
11844 struct regcache *reg_cache = arm_insn_r->regcache;
71e396f9
LM
11845 uint32_t register_count = 0, register_bits;
11846 uint32_t reg_base, addr_mode;
72508ac0 11847 uint32_t record_buf[24], record_buf_mem[48];
71e396f9
LM
11848 uint32_t wback;
11849 ULONGEST u_regval;
72508ac0 11850
71e396f9
LM
11851 /* Fetch the list of registers. */
11852 register_bits = bits (arm_insn_r->arm_insn, 0, 15);
11853 arm_insn_r->reg_rec_count = 0;
11854
11855 /* Fetch the base register that contains the address we are loading data
11856 to. */
11857 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0 11858
71e396f9
LM
11859 /* Calculate wback. */
11860 wback = (bit (arm_insn_r->arm_insn, 21) == 1);
72508ac0
PO
11861
11862 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11863 {
71e396f9 11864 /* LDM/LDMIA/LDMFD, LDMDA/LDMFA, LDMDB and LDMIB. */
72508ac0 11865
71e396f9 11866 /* Find out which registers are going to be loaded from memory. */
72508ac0 11867 while (register_bits)
71e396f9
LM
11868 {
11869 if (register_bits & 0x00000001)
11870 record_buf[arm_insn_r->reg_rec_count++] = register_count;
11871 register_bits = register_bits >> 1;
11872 register_count++;
11873 }
72508ac0 11874
71e396f9
LM
11875
11876 /* If wback is true, also save the base register, which is going to be
11877 written to. */
11878 if (wback)
11879 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
11880
11881 /* Save the CPSR register. */
11882 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
72508ac0
PO
11883 }
11884 else
11885 {
71e396f9 11886 /* STM (STMIA, STMEA), STMDA (STMED), STMDB (STMFD) and STMIB (STMFA). */
72508ac0 11887
71e396f9
LM
11888 addr_mode = bits (arm_insn_r->arm_insn, 23, 24);
11889
11890 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
11891
11892 /* Find out how many registers are going to be stored to memory. */
72508ac0 11893 while (register_bits)
71e396f9
LM
11894 {
11895 if (register_bits & 0x00000001)
11896 register_count++;
11897 register_bits = register_bits >> 1;
11898 }
72508ac0
PO
11899
11900 switch (addr_mode)
71e396f9
LM
11901 {
11902 /* STMDA (STMED): Decrement after. */
11903 case 0:
11904 record_buf_mem[1] = (uint32_t) u_regval
11905 - register_count * INT_REGISTER_SIZE + 4;
11906 break;
11907 /* STM (STMIA, STMEA): Increment after. */
11908 case 1:
11909 record_buf_mem[1] = (uint32_t) u_regval;
11910 break;
11911 /* STMDB (STMFD): Decrement before. */
11912 case 2:
11913 record_buf_mem[1] = (uint32_t) u_regval
11914 - register_count * INT_REGISTER_SIZE;
11915 break;
11916 /* STMIB (STMFA): Increment before. */
11917 case 3:
11918 record_buf_mem[1] = (uint32_t) u_regval + INT_REGISTER_SIZE;
11919 break;
11920 default:
11921 gdb_assert_not_reached ("no decoding pattern found");
11922 break;
11923 }
72508ac0 11924
71e396f9
LM
11925 record_buf_mem[0] = register_count * INT_REGISTER_SIZE;
11926 arm_insn_r->mem_rec_count = 1;
11927
11928 /* If wback is true, also save the base register, which is going to be
11929 written to. */
11930 if (wback)
11931 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
11932 }
11933
11934 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11935 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11936 return 0;
11937}
11938
11939/* Handling opcode 101 insns. */
11940
11941static int
11942arm_record_b_bl (insn_decode_record *arm_insn_r)
11943{
11944 uint32_t record_buf[8];
11945
11946 /* Handle B, BL, BLX(1) insns. */
11947 /* B simply branches so we do nothing here. */
11948 /* Note: BLX(1) doesnt fall here but instead it falls into
11949 extension space. */
11950 if (bit (arm_insn_r->arm_insn, 24))
11951 {
11952 record_buf[0] = ARM_LR_REGNUM;
11953 arm_insn_r->reg_rec_count = 1;
11954 }
11955
11956 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11957
11958 return 0;
11959}
11960
11961/* Handling opcode 110 insns. */
11962
11963static int
c6ec2b30 11964arm_record_unsupported_insn (insn_decode_record *arm_insn_r)
72508ac0
PO
11965{
11966 printf_unfiltered (_("Process record does not support instruction "
11967 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
11968 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11969
11970 return -1;
11971}
11972
5a578da5
OJ
11973/* Record handler for vector data transfer instructions. */
11974
11975static int
11976arm_record_vdata_transfer_insn (insn_decode_record *arm_insn_r)
11977{
11978 uint32_t bits_a, bit_c, bit_l, reg_t, reg_v;
11979 uint32_t record_buf[4];
11980
11981 const int num_regs = gdbarch_num_regs (arm_insn_r->gdbarch);
11982 reg_t = bits (arm_insn_r->arm_insn, 12, 15);
11983 reg_v = bits (arm_insn_r->arm_insn, 21, 23);
11984 bits_a = bits (arm_insn_r->arm_insn, 21, 23);
11985 bit_l = bit (arm_insn_r->arm_insn, 20);
11986 bit_c = bit (arm_insn_r->arm_insn, 8);
11987
11988 /* Handle VMOV instruction. */
11989 if (bit_l && bit_c)
11990 {
11991 record_buf[0] = reg_t;
11992 arm_insn_r->reg_rec_count = 1;
11993 }
11994 else if (bit_l && !bit_c)
11995 {
11996 /* Handle VMOV instruction. */
11997 if (bits_a == 0x00)
11998 {
11999 if (bit (arm_insn_r->arm_insn, 20))
12000 record_buf[0] = reg_t;
12001 else
12002 record_buf[0] = num_regs + (bit (arm_insn_r->arm_insn, 7) |
12003 (reg_v << 1));
12004
12005 arm_insn_r->reg_rec_count = 1;
12006 }
12007 /* Handle VMRS instruction. */
12008 else if (bits_a == 0x07)
12009 {
12010 if (reg_t == 15)
12011 reg_t = ARM_PS_REGNUM;
12012
12013 record_buf[0] = reg_t;
12014 arm_insn_r->reg_rec_count = 1;
12015 }
12016 }
12017 else if (!bit_l && !bit_c)
12018 {
12019 /* Handle VMOV instruction. */
12020 if (bits_a == 0x00)
12021 {
12022 if (bit (arm_insn_r->arm_insn, 20))
12023 record_buf[0] = reg_t;
12024 else
12025 record_buf[0] = num_regs + (bit (arm_insn_r->arm_insn, 7) |
12026 (reg_v << 1));
12027
12028 arm_insn_r->reg_rec_count = 1;
12029 }
12030 /* Handle VMSR instruction. */
12031 else if (bits_a == 0x07)
12032 {
12033 record_buf[0] = ARM_FPSCR_REGNUM;
12034 arm_insn_r->reg_rec_count = 1;
12035 }
12036 }
12037 else if (!bit_l && bit_c)
12038 {
12039 /* Handle VMOV instruction. */
12040 if (!(bits_a & 0x04))
12041 {
12042 record_buf[0] = (reg_v | (bit (arm_insn_r->arm_insn, 7) << 4))
12043 + ARM_D0_REGNUM;
12044 arm_insn_r->reg_rec_count = 1;
12045 }
12046 /* Handle VDUP instruction. */
12047 else
12048 {
12049 if (bit (arm_insn_r->arm_insn, 21))
12050 {
12051 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
12052 record_buf[0] = reg_v + ARM_D0_REGNUM;
12053 record_buf[1] = reg_v + ARM_D0_REGNUM + 1;
12054 arm_insn_r->reg_rec_count = 2;
12055 }
12056 else
12057 {
12058 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
12059 record_buf[0] = reg_v + ARM_D0_REGNUM;
12060 arm_insn_r->reg_rec_count = 1;
12061 }
12062 }
12063 }
12064
12065 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12066 return 0;
12067}
12068
f20f80dd
OJ
12069/* Record handler for extension register load/store instructions. */
12070
12071static int
12072arm_record_exreg_ld_st_insn (insn_decode_record *arm_insn_r)
12073{
12074 uint32_t opcode, single_reg;
12075 uint8_t op_vldm_vstm;
12076 uint32_t record_buf[8], record_buf_mem[128];
12077 ULONGEST u_regval = 0;
12078
12079 struct regcache *reg_cache = arm_insn_r->regcache;
12080 const int num_regs = gdbarch_num_regs (arm_insn_r->gdbarch);
12081
12082 opcode = bits (arm_insn_r->arm_insn, 20, 24);
12083 single_reg = bit (arm_insn_r->arm_insn, 8);
12084 op_vldm_vstm = opcode & 0x1b;
12085
12086 /* Handle VMOV instructions. */
12087 if ((opcode & 0x1e) == 0x04)
12088 {
12089 if (bit (arm_insn_r->arm_insn, 4))
12090 {
12091 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12092 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
12093 arm_insn_r->reg_rec_count = 2;
12094 }
12095 else
12096 {
12097 uint8_t reg_m = (bits (arm_insn_r->arm_insn, 0, 3) << 1)
12098 | bit (arm_insn_r->arm_insn, 5);
12099
12100 if (!single_reg)
12101 {
12102 record_buf[0] = num_regs + reg_m;
12103 record_buf[1] = num_regs + reg_m + 1;
12104 arm_insn_r->reg_rec_count = 2;
12105 }
12106 else
12107 {
12108 record_buf[0] = reg_m + ARM_D0_REGNUM;
12109 arm_insn_r->reg_rec_count = 1;
12110 }
12111 }
12112 }
12113 /* Handle VSTM and VPUSH instructions. */
12114 else if (op_vldm_vstm == 0x08 || op_vldm_vstm == 0x0a
12115 || op_vldm_vstm == 0x12)
12116 {
12117 uint32_t start_address, reg_rn, imm_off32, imm_off8, memory_count;
12118 uint32_t memory_index = 0;
12119
12120 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
12121 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12122 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
12123 imm_off32 = imm_off8 << 24;
12124 memory_count = imm_off8;
12125
12126 if (bit (arm_insn_r->arm_insn, 23))
12127 start_address = u_regval;
12128 else
12129 start_address = u_regval - imm_off32;
12130
12131 if (bit (arm_insn_r->arm_insn, 21))
12132 {
12133 record_buf[0] = reg_rn;
12134 arm_insn_r->reg_rec_count = 1;
12135 }
12136
12137 while (memory_count > 0)
12138 {
12139 if (!single_reg)
12140 {
12141 record_buf_mem[memory_index] = start_address;
12142 record_buf_mem[memory_index + 1] = 4;
12143 start_address = start_address + 4;
12144 memory_index = memory_index + 2;
12145 }
12146 else
12147 {
12148 record_buf_mem[memory_index] = start_address;
12149 record_buf_mem[memory_index + 1] = 4;
12150 record_buf_mem[memory_index + 2] = start_address + 4;
12151 record_buf_mem[memory_index + 3] = 4;
12152 start_address = start_address + 8;
12153 memory_index = memory_index + 4;
12154 }
12155 memory_count--;
12156 }
12157 arm_insn_r->mem_rec_count = (memory_index >> 1);
12158 }
12159 /* Handle VLDM instructions. */
12160 else if (op_vldm_vstm == 0x09 || op_vldm_vstm == 0x0b
12161 || op_vldm_vstm == 0x13)
12162 {
12163 uint32_t reg_count, reg_vd;
12164 uint32_t reg_index = 0;
12165
12166 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12167 reg_count = bits (arm_insn_r->arm_insn, 0, 7);
12168
12169 if (single_reg)
12170 reg_vd = reg_vd | (bit (arm_insn_r->arm_insn, 22) << 4);
12171 else
12172 reg_vd = (reg_vd << 1) | bit (arm_insn_r->arm_insn, 22);
12173
12174 if (bit (arm_insn_r->arm_insn, 21))
12175 record_buf[reg_index++] = bits (arm_insn_r->arm_insn, 16, 19);
12176
12177 while (reg_count > 0)
12178 {
12179 if (single_reg)
12180 record_buf[reg_index++] = num_regs + reg_vd + reg_count - 1;
12181 else
12182 record_buf[reg_index++] = ARM_D0_REGNUM + reg_vd + reg_count - 1;
12183
12184 reg_count--;
12185 }
12186 arm_insn_r->reg_rec_count = reg_index;
12187 }
12188 /* VSTR Vector store register. */
12189 else if ((opcode & 0x13) == 0x10)
12190 {
12191 uint32_t start_address, reg_rn, imm_off32, imm_off8, memory_count;
12192 uint32_t memory_index = 0;
12193
12194 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
12195 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12196 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
12197 imm_off32 = imm_off8 << 24;
12198 memory_count = imm_off8;
12199
12200 if (bit (arm_insn_r->arm_insn, 23))
12201 start_address = u_regval + imm_off32;
12202 else
12203 start_address = u_regval - imm_off32;
12204
12205 if (single_reg)
12206 {
12207 record_buf_mem[memory_index] = start_address;
12208 record_buf_mem[memory_index + 1] = 4;
12209 arm_insn_r->mem_rec_count = 1;
12210 }
12211 else
12212 {
12213 record_buf_mem[memory_index] = start_address;
12214 record_buf_mem[memory_index + 1] = 4;
12215 record_buf_mem[memory_index + 2] = start_address + 4;
12216 record_buf_mem[memory_index + 3] = 4;
12217 arm_insn_r->mem_rec_count = 2;
12218 }
12219 }
12220 /* VLDR Vector load register. */
12221 else if ((opcode & 0x13) == 0x11)
12222 {
12223 uint32_t reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12224
12225 if (!single_reg)
12226 {
12227 reg_vd = reg_vd | (bit (arm_insn_r->arm_insn, 22) << 4);
12228 record_buf[0] = ARM_D0_REGNUM + reg_vd;
12229 }
12230 else
12231 {
12232 reg_vd = (reg_vd << 1) | bit (arm_insn_r->arm_insn, 22);
12233 record_buf[0] = num_regs + reg_vd;
12234 }
12235 arm_insn_r->reg_rec_count = 1;
12236 }
12237
12238 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12239 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12240 return 0;
12241}
12242
851f26ae
OJ
12243/* Record handler for arm/thumb mode VFP data processing instructions. */
12244
12245static int
12246arm_record_vfp_data_proc_insn (insn_decode_record *arm_insn_r)
12247{
12248 uint32_t opc1, opc2, opc3, dp_op_sz, bit_d, reg_vd;
12249 uint32_t record_buf[4];
12250 enum insn_types {INSN_T0, INSN_T1, INSN_T2, INSN_T3, INSN_INV};
12251 enum insn_types curr_insn_type = INSN_INV;
12252
12253 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12254 opc1 = bits (arm_insn_r->arm_insn, 20, 23);
12255 opc2 = bits (arm_insn_r->arm_insn, 16, 19);
12256 opc3 = bits (arm_insn_r->arm_insn, 6, 7);
12257 dp_op_sz = bit (arm_insn_r->arm_insn, 8);
12258 bit_d = bit (arm_insn_r->arm_insn, 22);
12259 opc1 = opc1 & 0x04;
12260
12261 /* Handle VMLA, VMLS. */
12262 if (opc1 == 0x00)
12263 {
12264 if (bit (arm_insn_r->arm_insn, 10))
12265 {
12266 if (bit (arm_insn_r->arm_insn, 6))
12267 curr_insn_type = INSN_T0;
12268 else
12269 curr_insn_type = INSN_T1;
12270 }
12271 else
12272 {
12273 if (dp_op_sz)
12274 curr_insn_type = INSN_T1;
12275 else
12276 curr_insn_type = INSN_T2;
12277 }
12278 }
12279 /* Handle VNMLA, VNMLS, VNMUL. */
12280 else if (opc1 == 0x01)
12281 {
12282 if (dp_op_sz)
12283 curr_insn_type = INSN_T1;
12284 else
12285 curr_insn_type = INSN_T2;
12286 }
12287 /* Handle VMUL. */
12288 else if (opc1 == 0x02 && !(opc3 & 0x01))
12289 {
12290 if (bit (arm_insn_r->arm_insn, 10))
12291 {
12292 if (bit (arm_insn_r->arm_insn, 6))
12293 curr_insn_type = INSN_T0;
12294 else
12295 curr_insn_type = INSN_T1;
12296 }
12297 else
12298 {
12299 if (dp_op_sz)
12300 curr_insn_type = INSN_T1;
12301 else
12302 curr_insn_type = INSN_T2;
12303 }
12304 }
12305 /* Handle VADD, VSUB. */
12306 else if (opc1 == 0x03)
12307 {
12308 if (!bit (arm_insn_r->arm_insn, 9))
12309 {
12310 if (bit (arm_insn_r->arm_insn, 6))
12311 curr_insn_type = INSN_T0;
12312 else
12313 curr_insn_type = INSN_T1;
12314 }
12315 else
12316 {
12317 if (dp_op_sz)
12318 curr_insn_type = INSN_T1;
12319 else
12320 curr_insn_type = INSN_T2;
12321 }
12322 }
12323 /* Handle VDIV. */
12324 else if (opc1 == 0x0b)
12325 {
12326 if (dp_op_sz)
12327 curr_insn_type = INSN_T1;
12328 else
12329 curr_insn_type = INSN_T2;
12330 }
12331 /* Handle all other vfp data processing instructions. */
12332 else if (opc1 == 0x0b)
12333 {
12334 /* Handle VMOV. */
12335 if (!(opc3 & 0x01) || (opc2 == 0x00 && opc3 == 0x01))
12336 {
12337 if (bit (arm_insn_r->arm_insn, 4))
12338 {
12339 if (bit (arm_insn_r->arm_insn, 6))
12340 curr_insn_type = INSN_T0;
12341 else
12342 curr_insn_type = INSN_T1;
12343 }
12344 else
12345 {
12346 if (dp_op_sz)
12347 curr_insn_type = INSN_T1;
12348 else
12349 curr_insn_type = INSN_T2;
12350 }
12351 }
12352 /* Handle VNEG and VABS. */
12353 else if ((opc2 == 0x01 && opc3 == 0x01)
12354 || (opc2 == 0x00 && opc3 == 0x03))
12355 {
12356 if (!bit (arm_insn_r->arm_insn, 11))
12357 {
12358 if (bit (arm_insn_r->arm_insn, 6))
12359 curr_insn_type = INSN_T0;
12360 else
12361 curr_insn_type = INSN_T1;
12362 }
12363 else
12364 {
12365 if (dp_op_sz)
12366 curr_insn_type = INSN_T1;
12367 else
12368 curr_insn_type = INSN_T2;
12369 }
12370 }
12371 /* Handle VSQRT. */
12372 else if (opc2 == 0x01 && opc3 == 0x03)
12373 {
12374 if (dp_op_sz)
12375 curr_insn_type = INSN_T1;
12376 else
12377 curr_insn_type = INSN_T2;
12378 }
12379 /* Handle VCVT. */
12380 else if (opc2 == 0x07 && opc3 == 0x03)
12381 {
12382 if (!dp_op_sz)
12383 curr_insn_type = INSN_T1;
12384 else
12385 curr_insn_type = INSN_T2;
12386 }
12387 else if (opc3 & 0x01)
12388 {
12389 /* Handle VCVT. */
12390 if ((opc2 == 0x08) || (opc2 & 0x0e) == 0x0c)
12391 {
12392 if (!bit (arm_insn_r->arm_insn, 18))
12393 curr_insn_type = INSN_T2;
12394 else
12395 {
12396 if (dp_op_sz)
12397 curr_insn_type = INSN_T1;
12398 else
12399 curr_insn_type = INSN_T2;
12400 }
12401 }
12402 /* Handle VCVT. */
12403 else if ((opc2 & 0x0e) == 0x0a || (opc2 & 0x0e) == 0x0e)
12404 {
12405 if (dp_op_sz)
12406 curr_insn_type = INSN_T1;
12407 else
12408 curr_insn_type = INSN_T2;
12409 }
12410 /* Handle VCVTB, VCVTT. */
12411 else if ((opc2 & 0x0e) == 0x02)
12412 curr_insn_type = INSN_T2;
12413 /* Handle VCMP, VCMPE. */
12414 else if ((opc2 & 0x0e) == 0x04)
12415 curr_insn_type = INSN_T3;
12416 }
12417 }
12418
12419 switch (curr_insn_type)
12420 {
12421 case INSN_T0:
12422 reg_vd = reg_vd | (bit_d << 4);
12423 record_buf[0] = reg_vd + ARM_D0_REGNUM;
12424 record_buf[1] = reg_vd + ARM_D0_REGNUM + 1;
12425 arm_insn_r->reg_rec_count = 2;
12426 break;
12427
12428 case INSN_T1:
12429 reg_vd = reg_vd | (bit_d << 4);
12430 record_buf[0] = reg_vd + ARM_D0_REGNUM;
12431 arm_insn_r->reg_rec_count = 1;
12432 break;
12433
12434 case INSN_T2:
12435 reg_vd = (reg_vd << 1) | bit_d;
12436 record_buf[0] = reg_vd + ARM_D0_REGNUM;
12437 arm_insn_r->reg_rec_count = 1;
12438 break;
12439
12440 case INSN_T3:
12441 record_buf[0] = ARM_FPSCR_REGNUM;
12442 arm_insn_r->reg_rec_count = 1;
12443 break;
12444
12445 default:
12446 gdb_assert_not_reached ("no decoding pattern found");
12447 break;
12448 }
12449
12450 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12451 return 0;
12452}
12453
60cc5e93
OJ
12454/* Handling opcode 110 insns. */
12455
12456static int
12457arm_record_asimd_vfp_coproc (insn_decode_record *arm_insn_r)
12458{
12459 uint32_t op, op1, op1_sbit, op1_ebit, coproc;
12460
12461 coproc = bits (arm_insn_r->arm_insn, 8, 11);
12462 op1 = bits (arm_insn_r->arm_insn, 20, 25);
12463 op1_ebit = bit (arm_insn_r->arm_insn, 20);
12464
12465 if ((coproc & 0x0e) == 0x0a)
12466 {
12467 /* Handle extension register ld/st instructions. */
12468 if (!(op1 & 0x20))
f20f80dd 12469 return arm_record_exreg_ld_st_insn (arm_insn_r);
60cc5e93
OJ
12470
12471 /* 64-bit transfers between arm core and extension registers. */
12472 if ((op1 & 0x3e) == 0x04)
f20f80dd 12473 return arm_record_exreg_ld_st_insn (arm_insn_r);
60cc5e93
OJ
12474 }
12475 else
12476 {
12477 /* Handle coprocessor ld/st instructions. */
12478 if (!(op1 & 0x3a))
12479 {
12480 /* Store. */
12481 if (!op1_ebit)
12482 return arm_record_unsupported_insn (arm_insn_r);
12483 else
12484 /* Load. */
12485 return arm_record_unsupported_insn (arm_insn_r);
12486 }
12487
12488 /* Move to coprocessor from two arm core registers. */
12489 if (op1 == 0x4)
12490 return arm_record_unsupported_insn (arm_insn_r);
12491
12492 /* Move to two arm core registers from coprocessor. */
12493 if (op1 == 0x5)
12494 {
12495 uint32_t reg_t[2];
12496
12497 reg_t[0] = bits (arm_insn_r->arm_insn, 12, 15);
12498 reg_t[1] = bits (arm_insn_r->arm_insn, 16, 19);
12499 arm_insn_r->reg_rec_count = 2;
12500
12501 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, reg_t);
12502 return 0;
12503 }
12504 }
12505 return arm_record_unsupported_insn (arm_insn_r);
12506}
12507
72508ac0
PO
12508/* Handling opcode 111 insns. */
12509
12510static int
12511arm_record_coproc_data_proc (insn_decode_record *arm_insn_r)
12512{
60cc5e93 12513 uint32_t op, op1_sbit, op1_ebit, coproc;
72508ac0
PO
12514 struct gdbarch_tdep *tdep = gdbarch_tdep (arm_insn_r->gdbarch);
12515 struct regcache *reg_cache = arm_insn_r->regcache;
97dfe206 12516 ULONGEST u_regval = 0;
72508ac0
PO
12517
12518 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 24, 27);
60cc5e93
OJ
12519 coproc = bits (arm_insn_r->arm_insn, 8, 11);
12520 op1_sbit = bit (arm_insn_r->arm_insn, 24);
12521 op1_ebit = bit (arm_insn_r->arm_insn, 20);
12522 op = bit (arm_insn_r->arm_insn, 4);
97dfe206
OJ
12523
12524 /* Handle arm SWI/SVC system call instructions. */
60cc5e93 12525 if (op1_sbit)
97dfe206
OJ
12526 {
12527 if (tdep->arm_syscall_record != NULL)
12528 {
12529 ULONGEST svc_operand, svc_number;
12530
12531 svc_operand = (0x00ffffff & arm_insn_r->arm_insn);
12532
12533 if (svc_operand) /* OABI. */
12534 svc_number = svc_operand - 0x900000;
12535 else /* EABI. */
12536 regcache_raw_read_unsigned (reg_cache, 7, &svc_number);
12537
60cc5e93 12538 return tdep->arm_syscall_record (reg_cache, svc_number);
97dfe206
OJ
12539 }
12540 else
12541 {
12542 printf_unfiltered (_("no syscall record support\n"));
60cc5e93 12543 return -1;
97dfe206
OJ
12544 }
12545 }
60cc5e93
OJ
12546
12547 if ((coproc & 0x0e) == 0x0a)
12548 {
12549 /* VFP data-processing instructions. */
12550 if (!op1_sbit && !op)
851f26ae 12551 return arm_record_vfp_data_proc_insn (arm_insn_r);
60cc5e93
OJ
12552
12553 /* Advanced SIMD, VFP instructions. */
12554 if (!op1_sbit && op)
5a578da5 12555 return arm_record_vdata_transfer_insn (arm_insn_r);
60cc5e93 12556 }
97dfe206
OJ
12557 else
12558 {
60cc5e93
OJ
12559 /* Coprocessor data operations. */
12560 if (!op1_sbit && !op)
12561 return arm_record_unsupported_insn (arm_insn_r);
12562
12563 /* Move to Coprocessor from ARM core register. */
12564 if (!op1_sbit && !op1_ebit && op)
12565 return arm_record_unsupported_insn (arm_insn_r);
12566
12567 /* Move to arm core register from coprocessor. */
12568 if (!op1_sbit && op1_ebit && op)
12569 {
12570 uint32_t record_buf[1];
12571
12572 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12573 if (record_buf[0] == 15)
12574 record_buf[0] = ARM_PS_REGNUM;
12575
12576 arm_insn_r->reg_rec_count = 1;
12577 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count,
12578 record_buf);
12579 return 0;
12580 }
97dfe206 12581 }
72508ac0 12582
60cc5e93 12583 return arm_record_unsupported_insn (arm_insn_r);
72508ac0
PO
12584}
12585
12586/* Handling opcode 000 insns. */
12587
12588static int
12589thumb_record_shift_add_sub (insn_decode_record *thumb_insn_r)
12590{
12591 uint32_t record_buf[8];
12592 uint32_t reg_src1 = 0;
12593
12594 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12595
12596 record_buf[0] = ARM_PS_REGNUM;
12597 record_buf[1] = reg_src1;
12598 thumb_insn_r->reg_rec_count = 2;
12599
12600 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12601
12602 return 0;
12603}
12604
12605
12606/* Handling opcode 001 insns. */
12607
12608static int
12609thumb_record_add_sub_cmp_mov (insn_decode_record *thumb_insn_r)
12610{
12611 uint32_t record_buf[8];
12612 uint32_t reg_src1 = 0;
12613
12614 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12615
12616 record_buf[0] = ARM_PS_REGNUM;
12617 record_buf[1] = reg_src1;
12618 thumb_insn_r->reg_rec_count = 2;
12619
12620 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12621
12622 return 0;
12623}
12624
12625/* Handling opcode 010 insns. */
12626
12627static int
12628thumb_record_ld_st_reg_offset (insn_decode_record *thumb_insn_r)
12629{
12630 struct regcache *reg_cache = thumb_insn_r->regcache;
12631 uint32_t record_buf[8], record_buf_mem[8];
12632
12633 uint32_t reg_src1 = 0, reg_src2 = 0;
12634 uint32_t opcode1 = 0, opcode2 = 0, opcode3 = 0;
12635
12636 ULONGEST u_regval[2] = {0};
12637
12638 opcode1 = bits (thumb_insn_r->arm_insn, 10, 12);
12639
12640 if (bit (thumb_insn_r->arm_insn, 12))
12641 {
12642 /* Handle load/store register offset. */
12643 opcode2 = bits (thumb_insn_r->arm_insn, 9, 10);
12644 if (opcode2 >= 12 && opcode2 <= 15)
12645 {
12646 /* LDR(2), LDRB(2) , LDRH(2), LDRSB, LDRSH. */
12647 reg_src1 = bits (thumb_insn_r->arm_insn,0, 2);
12648 record_buf[0] = reg_src1;
12649 thumb_insn_r->reg_rec_count = 1;
12650 }
12651 else if (opcode2 >= 8 && opcode2 <= 10)
12652 {
12653 /* STR(2), STRB(2), STRH(2) . */
12654 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12655 reg_src2 = bits (thumb_insn_r->arm_insn, 6, 8);
12656 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
12657 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
12658 if (8 == opcode2)
12659 record_buf_mem[0] = 4; /* STR (2). */
12660 else if (10 == opcode2)
12661 record_buf_mem[0] = 1; /* STRB (2). */
12662 else if (9 == opcode2)
12663 record_buf_mem[0] = 2; /* STRH (2). */
12664 record_buf_mem[1] = u_regval[0] + u_regval[1];
12665 thumb_insn_r->mem_rec_count = 1;
12666 }
12667 }
12668 else if (bit (thumb_insn_r->arm_insn, 11))
12669 {
12670 /* Handle load from literal pool. */
12671 /* LDR(3). */
12672 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12673 record_buf[0] = reg_src1;
12674 thumb_insn_r->reg_rec_count = 1;
12675 }
12676 else if (opcode1)
12677 {
12678 opcode2 = bits (thumb_insn_r->arm_insn, 8, 9);
12679 opcode3 = bits (thumb_insn_r->arm_insn, 0, 2);
12680 if ((3 == opcode2) && (!opcode3))
12681 {
12682 /* Branch with exchange. */
12683 record_buf[0] = ARM_PS_REGNUM;
12684 thumb_insn_r->reg_rec_count = 1;
12685 }
12686 else
12687 {
12688 /* Format 8; special data processing insns. */
12689 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12690 record_buf[0] = ARM_PS_REGNUM;
12691 record_buf[1] = reg_src1;
12692 thumb_insn_r->reg_rec_count = 2;
12693 }
12694 }
12695 else
12696 {
12697 /* Format 5; data processing insns. */
12698 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12699 if (bit (thumb_insn_r->arm_insn, 7))
12700 {
12701 reg_src1 = reg_src1 + 8;
12702 }
12703 record_buf[0] = ARM_PS_REGNUM;
12704 record_buf[1] = reg_src1;
12705 thumb_insn_r->reg_rec_count = 2;
12706 }
12707
12708 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12709 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12710 record_buf_mem);
12711
12712 return 0;
12713}
12714
12715/* Handling opcode 001 insns. */
12716
12717static int
12718thumb_record_ld_st_imm_offset (insn_decode_record *thumb_insn_r)
12719{
12720 struct regcache *reg_cache = thumb_insn_r->regcache;
12721 uint32_t record_buf[8], record_buf_mem[8];
12722
12723 uint32_t reg_src1 = 0;
12724 uint32_t opcode = 0, immed_5 = 0;
12725
12726 ULONGEST u_regval = 0;
12727
12728 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12729
12730 if (opcode)
12731 {
12732 /* LDR(1). */
12733 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12734 record_buf[0] = reg_src1;
12735 thumb_insn_r->reg_rec_count = 1;
12736 }
12737 else
12738 {
12739 /* STR(1). */
12740 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12741 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
12742 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12743 record_buf_mem[0] = 4;
12744 record_buf_mem[1] = u_regval + (immed_5 * 4);
12745 thumb_insn_r->mem_rec_count = 1;
12746 }
12747
12748 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12749 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12750 record_buf_mem);
12751
12752 return 0;
12753}
12754
12755/* Handling opcode 100 insns. */
12756
12757static int
12758thumb_record_ld_st_stack (insn_decode_record *thumb_insn_r)
12759{
12760 struct regcache *reg_cache = thumb_insn_r->regcache;
12761 uint32_t record_buf[8], record_buf_mem[8];
12762
12763 uint32_t reg_src1 = 0;
12764 uint32_t opcode = 0, immed_8 = 0, immed_5 = 0;
12765
12766 ULONGEST u_regval = 0;
12767
12768 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12769
12770 if (3 == opcode)
12771 {
12772 /* LDR(4). */
12773 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12774 record_buf[0] = reg_src1;
12775 thumb_insn_r->reg_rec_count = 1;
12776 }
12777 else if (1 == opcode)
12778 {
12779 /* LDRH(1). */
12780 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12781 record_buf[0] = reg_src1;
12782 thumb_insn_r->reg_rec_count = 1;
12783 }
12784 else if (2 == opcode)
12785 {
12786 /* STR(3). */
12787 immed_8 = bits (thumb_insn_r->arm_insn, 0, 7);
12788 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
12789 record_buf_mem[0] = 4;
12790 record_buf_mem[1] = u_regval + (immed_8 * 4);
12791 thumb_insn_r->mem_rec_count = 1;
12792 }
12793 else if (0 == opcode)
12794 {
12795 /* STRH(1). */
12796 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
12797 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12798 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12799 record_buf_mem[0] = 2;
12800 record_buf_mem[1] = u_regval + (immed_5 * 2);
12801 thumb_insn_r->mem_rec_count = 1;
12802 }
12803
12804 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12805 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12806 record_buf_mem);
12807
12808 return 0;
12809}
12810
12811/* Handling opcode 101 insns. */
12812
12813static int
12814thumb_record_misc (insn_decode_record *thumb_insn_r)
12815{
12816 struct regcache *reg_cache = thumb_insn_r->regcache;
12817
12818 uint32_t opcode = 0, opcode1 = 0, opcode2 = 0;
12819 uint32_t register_bits = 0, register_count = 0;
12820 uint32_t register_list[8] = {0}, index = 0, start_address = 0;
12821 uint32_t record_buf[24], record_buf_mem[48];
12822 uint32_t reg_src1;
12823
12824 ULONGEST u_regval = 0;
12825
12826 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12827 opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
12828 opcode2 = bits (thumb_insn_r->arm_insn, 9, 12);
12829
12830 if (14 == opcode2)
12831 {
12832 /* POP. */
12833 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12834 while (register_bits)
f969241e
OJ
12835 {
12836 if (register_bits & 0x00000001)
12837 record_buf[index++] = register_count;
12838 register_bits = register_bits >> 1;
12839 register_count++;
12840 }
12841 record_buf[index++] = ARM_PS_REGNUM;
12842 record_buf[index++] = ARM_SP_REGNUM;
12843 thumb_insn_r->reg_rec_count = index;
72508ac0
PO
12844 }
12845 else if (10 == opcode2)
12846 {
12847 /* PUSH. */
12848 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
9904a494 12849 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
72508ac0
PO
12850 while (register_bits)
12851 {
12852 if (register_bits & 0x00000001)
12853 register_count++;
12854 register_bits = register_bits >> 1;
12855 }
12856 start_address = u_regval - \
12857 (4 * (bit (thumb_insn_r->arm_insn, 8) + register_count));
12858 thumb_insn_r->mem_rec_count = register_count;
12859 while (register_count)
12860 {
12861 record_buf_mem[(register_count * 2) - 1] = start_address;
12862 record_buf_mem[(register_count * 2) - 2] = 4;
12863 start_address = start_address + 4;
12864 register_count--;
12865 }
12866 record_buf[0] = ARM_SP_REGNUM;
12867 thumb_insn_r->reg_rec_count = 1;
12868 }
12869 else if (0x1E == opcode1)
12870 {
12871 /* BKPT insn. */
12872 /* Handle enhanced software breakpoint insn, BKPT. */
12873 /* CPSR is changed to be executed in ARM state, disabling normal
12874 interrupts, entering abort mode. */
12875 /* According to high vector configuration PC is set. */
12876 /* User hits breakpoint and type reverse, in that case, we need to go back with
12877 previous CPSR and Program Counter. */
12878 record_buf[0] = ARM_PS_REGNUM;
12879 record_buf[1] = ARM_LR_REGNUM;
12880 thumb_insn_r->reg_rec_count = 2;
12881 /* We need to save SPSR value, which is not yet done. */
12882 printf_unfiltered (_("Process record does not support instruction "
12883 "0x%0x at address %s.\n"),
12884 thumb_insn_r->arm_insn,
12885 paddress (thumb_insn_r->gdbarch,
12886 thumb_insn_r->this_addr));
12887 return -1;
12888 }
12889 else if ((0 == opcode) || (1 == opcode))
12890 {
12891 /* ADD(5), ADD(6). */
12892 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12893 record_buf[0] = reg_src1;
12894 thumb_insn_r->reg_rec_count = 1;
12895 }
12896 else if (2 == opcode)
12897 {
12898 /* ADD(7), SUB(4). */
12899 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12900 record_buf[0] = ARM_SP_REGNUM;
12901 thumb_insn_r->reg_rec_count = 1;
12902 }
12903
12904 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12905 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12906 record_buf_mem);
12907
12908 return 0;
12909}
12910
12911/* Handling opcode 110 insns. */
12912
12913static int
12914thumb_record_ldm_stm_swi (insn_decode_record *thumb_insn_r)
12915{
12916 struct gdbarch_tdep *tdep = gdbarch_tdep (thumb_insn_r->gdbarch);
12917 struct regcache *reg_cache = thumb_insn_r->regcache;
12918
12919 uint32_t ret = 0; /* function return value: -1:record failure ; 0:success */
12920 uint32_t reg_src1 = 0;
12921 uint32_t opcode1 = 0, opcode2 = 0, register_bits = 0, register_count = 0;
12922 uint32_t register_list[8] = {0}, index = 0, start_address = 0;
12923 uint32_t record_buf[24], record_buf_mem[48];
12924
12925 ULONGEST u_regval = 0;
12926
12927 opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
12928 opcode2 = bits (thumb_insn_r->arm_insn, 11, 12);
12929
12930 if (1 == opcode2)
12931 {
12932
12933 /* LDMIA. */
12934 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12935 /* Get Rn. */
12936 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12937 while (register_bits)
12938 {
12939 if (register_bits & 0x00000001)
f969241e 12940 record_buf[index++] = register_count;
72508ac0 12941 register_bits = register_bits >> 1;
f969241e 12942 register_count++;
72508ac0 12943 }
f969241e
OJ
12944 record_buf[index++] = reg_src1;
12945 thumb_insn_r->reg_rec_count = index;
72508ac0
PO
12946 }
12947 else if (0 == opcode2)
12948 {
12949 /* It handles both STMIA. */
12950 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12951 /* Get Rn. */
12952 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12953 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12954 while (register_bits)
12955 {
12956 if (register_bits & 0x00000001)
12957 register_count++;
12958 register_bits = register_bits >> 1;
12959 }
12960 start_address = u_regval;
12961 thumb_insn_r->mem_rec_count = register_count;
12962 while (register_count)
12963 {
12964 record_buf_mem[(register_count * 2) - 1] = start_address;
12965 record_buf_mem[(register_count * 2) - 2] = 4;
12966 start_address = start_address + 4;
12967 register_count--;
12968 }
12969 }
12970 else if (0x1F == opcode1)
12971 {
12972 /* Handle arm syscall insn. */
97dfe206 12973 if (tdep->arm_syscall_record != NULL)
72508ac0 12974 {
97dfe206
OJ
12975 regcache_raw_read_unsigned (reg_cache, 7, &u_regval);
12976 ret = tdep->arm_syscall_record (reg_cache, u_regval);
72508ac0
PO
12977 }
12978 else
12979 {
12980 printf_unfiltered (_("no syscall record support\n"));
12981 return -1;
12982 }
12983 }
12984
12985 /* B (1), conditional branch is automatically taken care in process_record,
12986 as PC is saved there. */
12987
12988 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12989 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12990 record_buf_mem);
12991
12992 return ret;
12993}
12994
12995/* Handling opcode 111 insns. */
12996
12997static int
12998thumb_record_branch (insn_decode_record *thumb_insn_r)
12999{
13000 uint32_t record_buf[8];
13001 uint32_t bits_h = 0;
13002
13003 bits_h = bits (thumb_insn_r->arm_insn, 11, 12);
13004
13005 if (2 == bits_h || 3 == bits_h)
13006 {
13007 /* BL */
13008 record_buf[0] = ARM_LR_REGNUM;
13009 thumb_insn_r->reg_rec_count = 1;
13010 }
13011 else if (1 == bits_h)
13012 {
13013 /* BLX(1). */
13014 record_buf[0] = ARM_PS_REGNUM;
13015 record_buf[1] = ARM_LR_REGNUM;
13016 thumb_insn_r->reg_rec_count = 2;
13017 }
13018
13019 /* B(2) is automatically taken care in process_record, as PC is
13020 saved there. */
13021
13022 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13023
13024 return 0;
13025}
13026
c6ec2b30
OJ
13027/* Handler for thumb2 load/store multiple instructions. */
13028
13029static int
13030thumb2_record_ld_st_multiple (insn_decode_record *thumb2_insn_r)
13031{
13032 struct regcache *reg_cache = thumb2_insn_r->regcache;
13033
13034 uint32_t reg_rn, op;
13035 uint32_t register_bits = 0, register_count = 0;
13036 uint32_t index = 0, start_address = 0;
13037 uint32_t record_buf[24], record_buf_mem[48];
13038
13039 ULONGEST u_regval = 0;
13040
13041 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13042 op = bits (thumb2_insn_r->arm_insn, 23, 24);
13043
13044 if (0 == op || 3 == op)
13045 {
13046 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
13047 {
13048 /* Handle RFE instruction. */
13049 record_buf[0] = ARM_PS_REGNUM;
13050 thumb2_insn_r->reg_rec_count = 1;
13051 }
13052 else
13053 {
13054 /* Handle SRS instruction after reading banked SP. */
13055 return arm_record_unsupported_insn (thumb2_insn_r);
13056 }
13057 }
13058 else if (1 == op || 2 == op)
13059 {
13060 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
13061 {
13062 /* Handle LDM/LDMIA/LDMFD and LDMDB/LDMEA instructions. */
13063 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
13064 while (register_bits)
13065 {
13066 if (register_bits & 0x00000001)
13067 record_buf[index++] = register_count;
13068
13069 register_count++;
13070 register_bits = register_bits >> 1;
13071 }
13072 record_buf[index++] = reg_rn;
13073 record_buf[index++] = ARM_PS_REGNUM;
13074 thumb2_insn_r->reg_rec_count = index;
13075 }
13076 else
13077 {
13078 /* Handle STM/STMIA/STMEA and STMDB/STMFD. */
13079 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
13080 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
13081 while (register_bits)
13082 {
13083 if (register_bits & 0x00000001)
13084 register_count++;
13085
13086 register_bits = register_bits >> 1;
13087 }
13088
13089 if (1 == op)
13090 {
13091 /* Start address calculation for LDMDB/LDMEA. */
13092 start_address = u_regval;
13093 }
13094 else if (2 == op)
13095 {
13096 /* Start address calculation for LDMDB/LDMEA. */
13097 start_address = u_regval - register_count * 4;
13098 }
13099
13100 thumb2_insn_r->mem_rec_count = register_count;
13101 while (register_count)
13102 {
13103 record_buf_mem[register_count * 2 - 1] = start_address;
13104 record_buf_mem[register_count * 2 - 2] = 4;
13105 start_address = start_address + 4;
13106 register_count--;
13107 }
13108 record_buf[0] = reg_rn;
13109 record_buf[1] = ARM_PS_REGNUM;
13110 thumb2_insn_r->reg_rec_count = 2;
13111 }
13112 }
13113
13114 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
13115 record_buf_mem);
13116 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13117 record_buf);
13118 return ARM_RECORD_SUCCESS;
13119}
13120
13121/* Handler for thumb2 load/store (dual/exclusive) and table branch
13122 instructions. */
13123
13124static int
13125thumb2_record_ld_st_dual_ex_tbb (insn_decode_record *thumb2_insn_r)
13126{
13127 struct regcache *reg_cache = thumb2_insn_r->regcache;
13128
13129 uint32_t reg_rd, reg_rn, offset_imm;
13130 uint32_t reg_dest1, reg_dest2;
13131 uint32_t address, offset_addr;
13132 uint32_t record_buf[8], record_buf_mem[8];
13133 uint32_t op1, op2, op3;
13134 LONGEST s_word;
13135
13136 ULONGEST u_regval[2];
13137
13138 op1 = bits (thumb2_insn_r->arm_insn, 23, 24);
13139 op2 = bits (thumb2_insn_r->arm_insn, 20, 21);
13140 op3 = bits (thumb2_insn_r->arm_insn, 4, 7);
13141
13142 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
13143 {
13144 if(!(1 == op1 && 1 == op2 && (0 == op3 || 1 == op3)))
13145 {
13146 reg_dest1 = bits (thumb2_insn_r->arm_insn, 12, 15);
13147 record_buf[0] = reg_dest1;
13148 record_buf[1] = ARM_PS_REGNUM;
13149 thumb2_insn_r->reg_rec_count = 2;
13150 }
13151
13152 if (3 == op2 || (op1 & 2) || (1 == op1 && 1 == op2 && 7 == op3))
13153 {
13154 reg_dest2 = bits (thumb2_insn_r->arm_insn, 8, 11);
13155 record_buf[2] = reg_dest2;
13156 thumb2_insn_r->reg_rec_count = 3;
13157 }
13158 }
13159 else
13160 {
13161 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13162 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
13163
13164 if (0 == op1 && 0 == op2)
13165 {
13166 /* Handle STREX. */
13167 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13168 address = u_regval[0] + (offset_imm * 4);
13169 record_buf_mem[0] = 4;
13170 record_buf_mem[1] = address;
13171 thumb2_insn_r->mem_rec_count = 1;
13172 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
13173 record_buf[0] = reg_rd;
13174 thumb2_insn_r->reg_rec_count = 1;
13175 }
13176 else if (1 == op1 && 0 == op2)
13177 {
13178 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
13179 record_buf[0] = reg_rd;
13180 thumb2_insn_r->reg_rec_count = 1;
13181 address = u_regval[0];
13182 record_buf_mem[1] = address;
13183
13184 if (4 == op3)
13185 {
13186 /* Handle STREXB. */
13187 record_buf_mem[0] = 1;
13188 thumb2_insn_r->mem_rec_count = 1;
13189 }
13190 else if (5 == op3)
13191 {
13192 /* Handle STREXH. */
13193 record_buf_mem[0] = 2 ;
13194 thumb2_insn_r->mem_rec_count = 1;
13195 }
13196 else if (7 == op3)
13197 {
13198 /* Handle STREXD. */
13199 address = u_regval[0];
13200 record_buf_mem[0] = 4;
13201 record_buf_mem[2] = 4;
13202 record_buf_mem[3] = address + 4;
13203 thumb2_insn_r->mem_rec_count = 2;
13204 }
13205 }
13206 else
13207 {
13208 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13209
13210 if (bit (thumb2_insn_r->arm_insn, 24))
13211 {
13212 if (bit (thumb2_insn_r->arm_insn, 23))
13213 offset_addr = u_regval[0] + (offset_imm * 4);
13214 else
13215 offset_addr = u_regval[0] - (offset_imm * 4);
13216
13217 address = offset_addr;
13218 }
13219 else
13220 address = u_regval[0];
13221
13222 record_buf_mem[0] = 4;
13223 record_buf_mem[1] = address;
13224 record_buf_mem[2] = 4;
13225 record_buf_mem[3] = address + 4;
13226 thumb2_insn_r->mem_rec_count = 2;
13227 record_buf[0] = reg_rn;
13228 thumb2_insn_r->reg_rec_count = 1;
13229 }
13230 }
13231
13232 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13233 record_buf);
13234 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
13235 record_buf_mem);
13236 return ARM_RECORD_SUCCESS;
13237}
13238
13239/* Handler for thumb2 data processing (shift register and modified immediate)
13240 instructions. */
13241
13242static int
13243thumb2_record_data_proc_sreg_mimm (insn_decode_record *thumb2_insn_r)
13244{
13245 uint32_t reg_rd, op;
13246 uint32_t record_buf[8];
13247
13248 op = bits (thumb2_insn_r->arm_insn, 21, 24);
13249 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
13250
13251 if ((0 == op || 4 == op || 8 == op || 13 == op) && 15 == reg_rd)
13252 {
13253 record_buf[0] = ARM_PS_REGNUM;
13254 thumb2_insn_r->reg_rec_count = 1;
13255 }
13256 else
13257 {
13258 record_buf[0] = reg_rd;
13259 record_buf[1] = ARM_PS_REGNUM;
13260 thumb2_insn_r->reg_rec_count = 2;
13261 }
13262
13263 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13264 record_buf);
13265 return ARM_RECORD_SUCCESS;
13266}
13267
13268/* Generic handler for thumb2 instructions which effect destination and PS
13269 registers. */
13270
13271static int
13272thumb2_record_ps_dest_generic (insn_decode_record *thumb2_insn_r)
13273{
13274 uint32_t reg_rd;
13275 uint32_t record_buf[8];
13276
13277 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
13278
13279 record_buf[0] = reg_rd;
13280 record_buf[1] = ARM_PS_REGNUM;
13281 thumb2_insn_r->reg_rec_count = 2;
13282
13283 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13284 record_buf);
13285 return ARM_RECORD_SUCCESS;
13286}
13287
13288/* Handler for thumb2 branch and miscellaneous control instructions. */
13289
13290static int
13291thumb2_record_branch_misc_cntrl (insn_decode_record *thumb2_insn_r)
13292{
13293 uint32_t op, op1, op2;
13294 uint32_t record_buf[8];
13295
13296 op = bits (thumb2_insn_r->arm_insn, 20, 26);
13297 op1 = bits (thumb2_insn_r->arm_insn, 12, 14);
13298 op2 = bits (thumb2_insn_r->arm_insn, 8, 11);
13299
13300 /* Handle MSR insn. */
13301 if (!(op1 & 0x2) && 0x38 == op)
13302 {
13303 if (!(op2 & 0x3))
13304 {
13305 /* CPSR is going to be changed. */
13306 record_buf[0] = ARM_PS_REGNUM;
13307 thumb2_insn_r->reg_rec_count = 1;
13308 }
13309 else
13310 {
13311 arm_record_unsupported_insn(thumb2_insn_r);
13312 return -1;
13313 }
13314 }
13315 else if (4 == (op1 & 0x5) || 5 == (op1 & 0x5))
13316 {
13317 /* BLX. */
13318 record_buf[0] = ARM_PS_REGNUM;
13319 record_buf[1] = ARM_LR_REGNUM;
13320 thumb2_insn_r->reg_rec_count = 2;
13321 }
13322
13323 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13324 record_buf);
13325 return ARM_RECORD_SUCCESS;
13326}
13327
13328/* Handler for thumb2 store single data item instructions. */
13329
13330static int
13331thumb2_record_str_single_data (insn_decode_record *thumb2_insn_r)
13332{
13333 struct regcache *reg_cache = thumb2_insn_r->regcache;
13334
13335 uint32_t reg_rn, reg_rm, offset_imm, shift_imm;
13336 uint32_t address, offset_addr;
13337 uint32_t record_buf[8], record_buf_mem[8];
13338 uint32_t op1, op2;
13339
13340 ULONGEST u_regval[2];
13341
13342 op1 = bits (thumb2_insn_r->arm_insn, 21, 23);
13343 op2 = bits (thumb2_insn_r->arm_insn, 6, 11);
13344 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13345 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
13346
13347 if (bit (thumb2_insn_r->arm_insn, 23))
13348 {
13349 /* T2 encoding. */
13350 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 11);
13351 offset_addr = u_regval[0] + offset_imm;
13352 address = offset_addr;
13353 }
13354 else
13355 {
13356 /* T3 encoding. */
13357 if ((0 == op1 || 1 == op1 || 2 == op1) && !(op2 & 0x20))
13358 {
13359 /* Handle STRB (register). */
13360 reg_rm = bits (thumb2_insn_r->arm_insn, 0, 3);
13361 regcache_raw_read_unsigned (reg_cache, reg_rm, &u_regval[1]);
13362 shift_imm = bits (thumb2_insn_r->arm_insn, 4, 5);
13363 offset_addr = u_regval[1] << shift_imm;
13364 address = u_regval[0] + offset_addr;
13365 }
13366 else
13367 {
13368 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13369 if (bit (thumb2_insn_r->arm_insn, 10))
13370 {
13371 if (bit (thumb2_insn_r->arm_insn, 9))
13372 offset_addr = u_regval[0] + offset_imm;
13373 else
13374 offset_addr = u_regval[0] - offset_imm;
13375
13376 address = offset_addr;
13377 }
13378 else
13379 address = u_regval[0];
13380 }
13381 }
13382
13383 switch (op1)
13384 {
13385 /* Store byte instructions. */
13386 case 4:
13387 case 0:
13388 record_buf_mem[0] = 1;
13389 break;
13390 /* Store half word instructions. */
13391 case 1:
13392 case 5:
13393 record_buf_mem[0] = 2;
13394 break;
13395 /* Store word instructions. */
13396 case 2:
13397 case 6:
13398 record_buf_mem[0] = 4;
13399 break;
13400
13401 default:
13402 gdb_assert_not_reached ("no decoding pattern found");
13403 break;
13404 }
13405
13406 record_buf_mem[1] = address;
13407 thumb2_insn_r->mem_rec_count = 1;
13408 record_buf[0] = reg_rn;
13409 thumb2_insn_r->reg_rec_count = 1;
13410
13411 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13412 record_buf);
13413 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
13414 record_buf_mem);
13415 return ARM_RECORD_SUCCESS;
13416}
13417
13418/* Handler for thumb2 load memory hints instructions. */
13419
13420static int
13421thumb2_record_ld_mem_hints (insn_decode_record *thumb2_insn_r)
13422{
13423 uint32_t record_buf[8];
13424 uint32_t reg_rt, reg_rn;
13425
13426 reg_rt = bits (thumb2_insn_r->arm_insn, 12, 15);
13427 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13428
13429 if (ARM_PC_REGNUM != reg_rt)
13430 {
13431 record_buf[0] = reg_rt;
13432 record_buf[1] = reg_rn;
13433 record_buf[2] = ARM_PS_REGNUM;
13434 thumb2_insn_r->reg_rec_count = 3;
13435
13436 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13437 record_buf);
13438 return ARM_RECORD_SUCCESS;
13439 }
13440
13441 return ARM_RECORD_FAILURE;
13442}
13443
13444/* Handler for thumb2 load word instructions. */
13445
13446static int
13447thumb2_record_ld_word (insn_decode_record *thumb2_insn_r)
13448{
13449 uint32_t opcode1 = 0, opcode2 = 0;
13450 uint32_t record_buf[8];
13451
13452 record_buf[0] = bits (thumb2_insn_r->arm_insn, 12, 15);
13453 record_buf[1] = ARM_PS_REGNUM;
13454 thumb2_insn_r->reg_rec_count = 2;
13455
13456 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13457 record_buf);
13458 return ARM_RECORD_SUCCESS;
13459}
13460
13461/* Handler for thumb2 long multiply, long multiply accumulate, and
13462 divide instructions. */
13463
13464static int
13465thumb2_record_lmul_lmla_div (insn_decode_record *thumb2_insn_r)
13466{
13467 uint32_t opcode1 = 0, opcode2 = 0;
13468 uint32_t record_buf[8];
13469 uint32_t reg_src1 = 0;
13470
13471 opcode1 = bits (thumb2_insn_r->arm_insn, 20, 22);
13472 opcode2 = bits (thumb2_insn_r->arm_insn, 4, 7);
13473
13474 if (0 == opcode1 || 2 == opcode1 || (opcode1 >= 4 && opcode1 <= 6))
13475 {
13476 /* Handle SMULL, UMULL, SMULAL. */
13477 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
13478 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
13479 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
13480 record_buf[2] = ARM_PS_REGNUM;
13481 thumb2_insn_r->reg_rec_count = 3;
13482 }
13483 else if (1 == opcode1 || 3 == opcode2)
13484 {
13485 /* Handle SDIV and UDIV. */
13486 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
13487 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
13488 record_buf[2] = ARM_PS_REGNUM;
13489 thumb2_insn_r->reg_rec_count = 3;
13490 }
13491 else
13492 return ARM_RECORD_FAILURE;
13493
13494 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13495 record_buf);
13496 return ARM_RECORD_SUCCESS;
13497}
13498
60cc5e93
OJ
13499/* Record handler for thumb32 coprocessor instructions. */
13500
13501static int
13502thumb2_record_coproc_insn (insn_decode_record *thumb2_insn_r)
13503{
13504 if (bit (thumb2_insn_r->arm_insn, 25))
13505 return arm_record_coproc_data_proc (thumb2_insn_r);
13506 else
13507 return arm_record_asimd_vfp_coproc (thumb2_insn_r);
13508}
13509
1e1b6563
OJ
13510/* Record handler for advance SIMD structure load/store instructions. */
13511
13512static int
13513thumb2_record_asimd_struct_ld_st (insn_decode_record *thumb2_insn_r)
13514{
13515 struct regcache *reg_cache = thumb2_insn_r->regcache;
13516 uint32_t l_bit, a_bit, b_bits;
13517 uint32_t record_buf[128], record_buf_mem[128];
13518 uint32_t reg_rn, reg_vd, address, f_esize, f_elem;
13519 uint32_t index_r = 0, index_e = 0, bf_regs = 0, index_m = 0, loop_t = 0;
13520 uint8_t f_ebytes;
13521
13522 l_bit = bit (thumb2_insn_r->arm_insn, 21);
13523 a_bit = bit (thumb2_insn_r->arm_insn, 23);
13524 b_bits = bits (thumb2_insn_r->arm_insn, 8, 11);
13525 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13526 reg_vd = bits (thumb2_insn_r->arm_insn, 12, 15);
13527 reg_vd = (bit (thumb2_insn_r->arm_insn, 22) << 4) | reg_vd;
13528 f_ebytes = (1 << bits (thumb2_insn_r->arm_insn, 6, 7));
13529 f_esize = 8 * f_ebytes;
13530 f_elem = 8 / f_ebytes;
13531
13532 if (!l_bit)
13533 {
13534 ULONGEST u_regval = 0;
13535 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
13536 address = u_regval;
13537
13538 if (!a_bit)
13539 {
13540 /* Handle VST1. */
13541 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
13542 {
13543 if (b_bits == 0x07)
13544 bf_regs = 1;
13545 else if (b_bits == 0x0a)
13546 bf_regs = 2;
13547 else if (b_bits == 0x06)
13548 bf_regs = 3;
13549 else if (b_bits == 0x02)
13550 bf_regs = 4;
13551 else
13552 bf_regs = 0;
13553
13554 for (index_r = 0; index_r < bf_regs; index_r++)
13555 {
13556 for (index_e = 0; index_e < f_elem; index_e++)
13557 {
13558 record_buf_mem[index_m++] = f_ebytes;
13559 record_buf_mem[index_m++] = address;
13560 address = address + f_ebytes;
13561 thumb2_insn_r->mem_rec_count += 1;
13562 }
13563 }
13564 }
13565 /* Handle VST2. */
13566 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
13567 {
13568 if (b_bits == 0x09 || b_bits == 0x08)
13569 bf_regs = 1;
13570 else if (b_bits == 0x03)
13571 bf_regs = 2;
13572 else
13573 bf_regs = 0;
13574
13575 for (index_r = 0; index_r < bf_regs; index_r++)
13576 for (index_e = 0; index_e < f_elem; index_e++)
13577 {
13578 for (loop_t = 0; loop_t < 2; loop_t++)
13579 {
13580 record_buf_mem[index_m++] = f_ebytes;
13581 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
13582 thumb2_insn_r->mem_rec_count += 1;
13583 }
13584 address = address + (2 * f_ebytes);
13585 }
13586 }
13587 /* Handle VST3. */
13588 else if ((b_bits & 0x0e) == 0x04)
13589 {
13590 for (index_e = 0; index_e < f_elem; index_e++)
13591 {
13592 for (loop_t = 0; loop_t < 3; loop_t++)
13593 {
13594 record_buf_mem[index_m++] = f_ebytes;
13595 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
13596 thumb2_insn_r->mem_rec_count += 1;
13597 }
13598 address = address + (3 * f_ebytes);
13599 }
13600 }
13601 /* Handle VST4. */
13602 else if (!(b_bits & 0x0e))
13603 {
13604 for (index_e = 0; index_e < f_elem; index_e++)
13605 {
13606 for (loop_t = 0; loop_t < 4; loop_t++)
13607 {
13608 record_buf_mem[index_m++] = f_ebytes;
13609 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
13610 thumb2_insn_r->mem_rec_count += 1;
13611 }
13612 address = address + (4 * f_ebytes);
13613 }
13614 }
13615 }
13616 else
13617 {
13618 uint8_t bft_size = bits (thumb2_insn_r->arm_insn, 10, 11);
13619
13620 if (bft_size == 0x00)
13621 f_ebytes = 1;
13622 else if (bft_size == 0x01)
13623 f_ebytes = 2;
13624 else if (bft_size == 0x02)
13625 f_ebytes = 4;
13626 else
13627 f_ebytes = 0;
13628
13629 /* Handle VST1. */
13630 if (!(b_bits & 0x0b) || b_bits == 0x08)
13631 thumb2_insn_r->mem_rec_count = 1;
13632 /* Handle VST2. */
13633 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09)
13634 thumb2_insn_r->mem_rec_count = 2;
13635 /* Handle VST3. */
13636 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a)
13637 thumb2_insn_r->mem_rec_count = 3;
13638 /* Handle VST4. */
13639 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b)
13640 thumb2_insn_r->mem_rec_count = 4;
13641
13642 for (index_m = 0; index_m < thumb2_insn_r->mem_rec_count; index_m++)
13643 {
13644 record_buf_mem[index_m] = f_ebytes;
13645 record_buf_mem[index_m] = address + (index_m * f_ebytes);
13646 }
13647 }
13648 }
13649 else
13650 {
13651 if (!a_bit)
13652 {
13653 /* Handle VLD1. */
13654 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
13655 thumb2_insn_r->reg_rec_count = 1;
13656 /* Handle VLD2. */
13657 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
13658 thumb2_insn_r->reg_rec_count = 2;
13659 /* Handle VLD3. */
13660 else if ((b_bits & 0x0e) == 0x04)
13661 thumb2_insn_r->reg_rec_count = 3;
13662 /* Handle VLD4. */
13663 else if (!(b_bits & 0x0e))
13664 thumb2_insn_r->reg_rec_count = 4;
13665 }
13666 else
13667 {
13668 /* Handle VLD1. */
13669 if (!(b_bits & 0x0b) || b_bits == 0x08 || b_bits == 0x0c)
13670 thumb2_insn_r->reg_rec_count = 1;
13671 /* Handle VLD2. */
13672 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09 || b_bits == 0x0d)
13673 thumb2_insn_r->reg_rec_count = 2;
13674 /* Handle VLD3. */
13675 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a || b_bits == 0x0e)
13676 thumb2_insn_r->reg_rec_count = 3;
13677 /* Handle VLD4. */
13678 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b || b_bits == 0x0f)
13679 thumb2_insn_r->reg_rec_count = 4;
13680
13681 for (index_r = 0; index_r < thumb2_insn_r->reg_rec_count; index_r++)
13682 record_buf[index_r] = reg_vd + ARM_D0_REGNUM + index_r;
13683 }
13684 }
13685
13686 if (bits (thumb2_insn_r->arm_insn, 0, 3) != 15)
13687 {
13688 record_buf[index_r] = reg_rn;
13689 thumb2_insn_r->reg_rec_count += 1;
13690 }
13691
13692 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13693 record_buf);
13694 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
13695 record_buf_mem);
13696 return 0;
13697}
13698
c6ec2b30
OJ
13699/* Decodes thumb2 instruction type and invokes its record handler. */
13700
13701static unsigned int
13702thumb2_record_decode_insn_handler (insn_decode_record *thumb2_insn_r)
13703{
13704 uint32_t op, op1, op2;
13705
13706 op = bit (thumb2_insn_r->arm_insn, 15);
13707 op1 = bits (thumb2_insn_r->arm_insn, 27, 28);
13708 op2 = bits (thumb2_insn_r->arm_insn, 20, 26);
13709
13710 if (op1 == 0x01)
13711 {
13712 if (!(op2 & 0x64 ))
13713 {
13714 /* Load/store multiple instruction. */
13715 return thumb2_record_ld_st_multiple (thumb2_insn_r);
13716 }
13717 else if (!((op2 & 0x64) ^ 0x04))
13718 {
13719 /* Load/store (dual/exclusive) and table branch instruction. */
13720 return thumb2_record_ld_st_dual_ex_tbb (thumb2_insn_r);
13721 }
13722 else if (!((op2 & 0x20) ^ 0x20))
13723 {
13724 /* Data-processing (shifted register). */
13725 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
13726 }
13727 else if (op2 & 0x40)
13728 {
13729 /* Co-processor instructions. */
60cc5e93 13730 return thumb2_record_coproc_insn (thumb2_insn_r);
c6ec2b30
OJ
13731 }
13732 }
13733 else if (op1 == 0x02)
13734 {
13735 if (op)
13736 {
13737 /* Branches and miscellaneous control instructions. */
13738 return thumb2_record_branch_misc_cntrl (thumb2_insn_r);
13739 }
13740 else if (op2 & 0x20)
13741 {
13742 /* Data-processing (plain binary immediate) instruction. */
13743 return thumb2_record_ps_dest_generic (thumb2_insn_r);
13744 }
13745 else
13746 {
13747 /* Data-processing (modified immediate). */
13748 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
13749 }
13750 }
13751 else if (op1 == 0x03)
13752 {
13753 if (!(op2 & 0x71 ))
13754 {
13755 /* Store single data item. */
13756 return thumb2_record_str_single_data (thumb2_insn_r);
13757 }
13758 else if (!((op2 & 0x71) ^ 0x10))
13759 {
13760 /* Advanced SIMD or structure load/store instructions. */
1e1b6563 13761 return thumb2_record_asimd_struct_ld_st (thumb2_insn_r);
c6ec2b30
OJ
13762 }
13763 else if (!((op2 & 0x67) ^ 0x01))
13764 {
13765 /* Load byte, memory hints instruction. */
13766 return thumb2_record_ld_mem_hints (thumb2_insn_r);
13767 }
13768 else if (!((op2 & 0x67) ^ 0x03))
13769 {
13770 /* Load halfword, memory hints instruction. */
13771 return thumb2_record_ld_mem_hints (thumb2_insn_r);
13772 }
13773 else if (!((op2 & 0x67) ^ 0x05))
13774 {
13775 /* Load word instruction. */
13776 return thumb2_record_ld_word (thumb2_insn_r);
13777 }
13778 else if (!((op2 & 0x70) ^ 0x20))
13779 {
13780 /* Data-processing (register) instruction. */
13781 return thumb2_record_ps_dest_generic (thumb2_insn_r);
13782 }
13783 else if (!((op2 & 0x78) ^ 0x30))
13784 {
13785 /* Multiply, multiply accumulate, abs diff instruction. */
13786 return thumb2_record_ps_dest_generic (thumb2_insn_r);
13787 }
13788 else if (!((op2 & 0x78) ^ 0x38))
13789 {
13790 /* Long multiply, long multiply accumulate, and divide. */
13791 return thumb2_record_lmul_lmla_div (thumb2_insn_r);
13792 }
13793 else if (op2 & 0x40)
13794 {
13795 /* Co-processor instructions. */
60cc5e93 13796 return thumb2_record_coproc_insn (thumb2_insn_r);
c6ec2b30
OJ
13797 }
13798 }
13799
13800 return -1;
13801}
72508ac0
PO
13802
13803/* Extracts arm/thumb/thumb2 insn depending on the size, and returns 0 on success
13804and positive val on fauilure. */
13805
13806static int
13807extract_arm_insn (insn_decode_record *insn_record, uint32_t insn_size)
13808{
13809 gdb_byte buf[insn_size];
13810
13811 memset (&buf[0], 0, insn_size);
13812
13813 if (target_read_memory (insn_record->this_addr, &buf[0], insn_size))
13814 return 1;
13815 insn_record->arm_insn = (uint32_t) extract_unsigned_integer (&buf[0],
13816 insn_size,
2959fed9 13817 gdbarch_byte_order_for_code (insn_record->gdbarch));
72508ac0
PO
13818 return 0;
13819}
13820
13821typedef int (*sti_arm_hdl_fp_t) (insn_decode_record*);
13822
13823/* Decode arm/thumb insn depending on condition cods and opcodes; and
13824 dispatch it. */
13825
13826static int
13827decode_insn (insn_decode_record *arm_record, record_type_t record_type,
13828 uint32_t insn_size)
13829{
13830
13831 /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm instruction. */
13832 static const sti_arm_hdl_fp_t const arm_handle_insn[8] =
13833 {
13834 arm_record_data_proc_misc_ld_str, /* 000. */
13835 arm_record_data_proc_imm, /* 001. */
13836 arm_record_ld_st_imm_offset, /* 010. */
13837 arm_record_ld_st_reg_offset, /* 011. */
13838 arm_record_ld_st_multiple, /* 100. */
13839 arm_record_b_bl, /* 101. */
60cc5e93 13840 arm_record_asimd_vfp_coproc, /* 110. */
72508ac0
PO
13841 arm_record_coproc_data_proc /* 111. */
13842 };
13843
13844 /* (Starting from numerical 0); bits 13,14,15 decodes type of thumb instruction. */
13845 static const sti_arm_hdl_fp_t const thumb_handle_insn[8] =
13846 { \
13847 thumb_record_shift_add_sub, /* 000. */
13848 thumb_record_add_sub_cmp_mov, /* 001. */
13849 thumb_record_ld_st_reg_offset, /* 010. */
13850 thumb_record_ld_st_imm_offset, /* 011. */
13851 thumb_record_ld_st_stack, /* 100. */
13852 thumb_record_misc, /* 101. */
13853 thumb_record_ldm_stm_swi, /* 110. */
13854 thumb_record_branch /* 111. */
13855 };
13856
13857 uint32_t ret = 0; /* return value: negative:failure 0:success. */
13858 uint32_t insn_id = 0;
13859
13860 if (extract_arm_insn (arm_record, insn_size))
13861 {
13862 if (record_debug)
13863 {
13864 printf_unfiltered (_("Process record: error reading memory at "
13865 "addr %s len = %d.\n"),
13866 paddress (arm_record->gdbarch, arm_record->this_addr), insn_size);
13867 }
13868 return -1;
13869 }
13870 else if (ARM_RECORD == record_type)
13871 {
13872 arm_record->cond = bits (arm_record->arm_insn, 28, 31);
13873 insn_id = bits (arm_record->arm_insn, 25, 27);
13874 ret = arm_record_extension_space (arm_record);
13875 /* If this insn has fallen into extension space
13876 then we need not decode it anymore. */
13877 if (ret != -1 && !INSN_RECORDED(arm_record))
13878 {
13879 ret = arm_handle_insn[insn_id] (arm_record);
13880 }
13881 }
13882 else if (THUMB_RECORD == record_type)
13883 {
13884 /* As thumb does not have condition codes, we set negative. */
13885 arm_record->cond = -1;
13886 insn_id = bits (arm_record->arm_insn, 13, 15);
13887 ret = thumb_handle_insn[insn_id] (arm_record);
13888 }
13889 else if (THUMB2_RECORD == record_type)
13890 {
c6ec2b30
OJ
13891 /* As thumb does not have condition codes, we set negative. */
13892 arm_record->cond = -1;
13893
13894 /* Swap first half of 32bit thumb instruction with second half. */
13895 arm_record->arm_insn
13896 = (arm_record->arm_insn >> 16) | (arm_record->arm_insn << 16);
13897
13898 insn_id = thumb2_record_decode_insn_handler (arm_record);
13899
13900 if (insn_id != ARM_RECORD_SUCCESS)
13901 {
13902 arm_record_unsupported_insn (arm_record);
13903 ret = -1;
13904 }
72508ac0
PO
13905 }
13906 else
13907 {
13908 /* Throw assertion. */
13909 gdb_assert_not_reached ("not a valid instruction, could not decode");
13910 }
13911
13912 return ret;
13913}
13914
13915
13916/* Cleans up local record registers and memory allocations. */
13917
13918static void
13919deallocate_reg_mem (insn_decode_record *record)
13920{
13921 xfree (record->arm_regs);
13922 xfree (record->arm_mems);
13923}
13924
13925
13926/* Parse the current instruction and record the values of the registers and
13927 memory that will be changed in current instruction to record_arch_list".
13928 Return -1 if something is wrong. */
13929
13930int
13931arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
13932 CORE_ADDR insn_addr)
13933{
13934
13935 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
13936 uint32_t no_of_rec = 0;
13937 uint32_t ret = 0; /* return value: -1:record failure ; 0:success */
13938 ULONGEST t_bit = 0, insn_id = 0;
13939
13940 ULONGEST u_regval = 0;
13941
13942 insn_decode_record arm_record;
13943
13944 memset (&arm_record, 0, sizeof (insn_decode_record));
13945 arm_record.regcache = regcache;
13946 arm_record.this_addr = insn_addr;
13947 arm_record.gdbarch = gdbarch;
13948
13949
13950 if (record_debug > 1)
13951 {
13952 fprintf_unfiltered (gdb_stdlog, "Process record: arm_process_record "
13953 "addr = %s\n",
13954 paddress (gdbarch, arm_record.this_addr));
13955 }
13956
13957 if (extract_arm_insn (&arm_record, 2))
13958 {
13959 if (record_debug)
13960 {
13961 printf_unfiltered (_("Process record: error reading memory at "
13962 "addr %s len = %d.\n"),
13963 paddress (arm_record.gdbarch,
13964 arm_record.this_addr), 2);
13965 }
13966 return -1;
13967 }
13968
13969 /* Check the insn, whether it is thumb or arm one. */
13970
13971 t_bit = arm_psr_thumb_bit (arm_record.gdbarch);
13972 regcache_raw_read_unsigned (arm_record.regcache, ARM_PS_REGNUM, &u_regval);
13973
13974
13975 if (!(u_regval & t_bit))
13976 {
13977 /* We are decoding arm insn. */
13978 ret = decode_insn (&arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES);
13979 }
13980 else
13981 {
13982 insn_id = bits (arm_record.arm_insn, 11, 15);
13983 /* is it thumb2 insn? */
13984 if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id))
13985 {
13986 ret = decode_insn (&arm_record, THUMB2_RECORD,
13987 THUMB2_INSN_SIZE_BYTES);
13988 }
13989 else
13990 {
13991 /* We are decoding thumb insn. */
13992 ret = decode_insn (&arm_record, THUMB_RECORD, THUMB_INSN_SIZE_BYTES);
13993 }
13994 }
13995
13996 if (0 == ret)
13997 {
13998 /* Record registers. */
25ea693b 13999 record_full_arch_list_add_reg (arm_record.regcache, ARM_PC_REGNUM);
72508ac0
PO
14000 if (arm_record.arm_regs)
14001 {
14002 for (no_of_rec = 0; no_of_rec < arm_record.reg_rec_count; no_of_rec++)
14003 {
25ea693b
MM
14004 if (record_full_arch_list_add_reg
14005 (arm_record.regcache , arm_record.arm_regs[no_of_rec]))
72508ac0
PO
14006 ret = -1;
14007 }
14008 }
14009 /* Record memories. */
14010 if (arm_record.arm_mems)
14011 {
14012 for (no_of_rec = 0; no_of_rec < arm_record.mem_rec_count; no_of_rec++)
14013 {
25ea693b 14014 if (record_full_arch_list_add_mem
72508ac0 14015 ((CORE_ADDR)arm_record.arm_mems[no_of_rec].addr,
25ea693b 14016 arm_record.arm_mems[no_of_rec].len))
72508ac0
PO
14017 ret = -1;
14018 }
14019 }
14020
25ea693b 14021 if (record_full_arch_list_add_end ())
72508ac0
PO
14022 ret = -1;
14023 }
14024
14025
14026 deallocate_reg_mem (&arm_record);
14027
14028 return ret;
14029}
14030
This page took 1.688909 seconds and 4 git commands to generate.