Don't print 0x for core_addr_to_string_nz
[deliverable/binutils-gdb.git] / gdb / arm-tdep.c
CommitLineData
ed9a39eb 1/* Common target dependent code for GDB on ARM systems.
0fd88904 2
618f726f 3 Copyright (C) 1988-2016 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 19
0baeab03
PA
20#include "defs.h"
21
0963b4bd 22#include <ctype.h> /* XXX for isupper (). */
34e8f22d 23
c906108c
SS
24#include "frame.h"
25#include "inferior.h"
45741a9c 26#include "infrun.h"
c906108c
SS
27#include "gdbcmd.h"
28#include "gdbcore.h"
0963b4bd 29#include "dis-asm.h" /* For register styles. */
4e052eda 30#include "regcache.h"
54483882 31#include "reggroups.h"
d16aafd8 32#include "doublest.h"
fd0407d6 33#include "value.h"
34e8f22d 34#include "arch-utils.h"
4be87837 35#include "osabi.h"
eb5492fa
DJ
36#include "frame-unwind.h"
37#include "frame-base.h"
38#include "trad-frame.h"
842e1f1e
DJ
39#include "objfiles.h"
40#include "dwarf2-frame.h"
e4c16157 41#include "gdbtypes.h"
29d73ae4 42#include "prologue-value.h"
25f8c692 43#include "remote.h"
123dc839
DJ
44#include "target-descriptions.h"
45#include "user-regs.h"
0e9e9abd 46#include "observer.h"
34e8f22d 47
8689682c 48#include "arch/arm.h"
d9311bfa 49#include "arch/arm-get-next-pcs.h"
34e8f22d 50#include "arm-tdep.h"
26216b98 51#include "gdb/sim-arm.h"
34e8f22d 52
082fc60d
RE
53#include "elf-bfd.h"
54#include "coff/internal.h"
97e03143 55#include "elf/arm.h"
c906108c 56
60c5725c 57#include "vec.h"
26216b98 58
72508ac0 59#include "record.h"
d02ed0bb 60#include "record-full.h"
72508ac0 61
9779414d 62#include "features/arm-with-m.c"
25f8c692 63#include "features/arm-with-m-fpa-layout.c"
3184d3f9 64#include "features/arm-with-m-vfp-d16.c"
ef7e8358
UW
65#include "features/arm-with-iwmmxt.c"
66#include "features/arm-with-vfpv2.c"
67#include "features/arm-with-vfpv3.c"
68#include "features/arm-with-neon.c"
9779414d 69
6529d2dd
AC
70static int arm_debug;
71
082fc60d
RE
72/* Macros for setting and testing a bit in a minimal symbol that marks
73 it as Thumb function. The MSB of the minimal symbol's "info" field
f594e5e9 74 is used for this purpose.
082fc60d
RE
75
76 MSYMBOL_SET_SPECIAL Actually sets the "special" bit.
f594e5e9 77 MSYMBOL_IS_SPECIAL Tests the "special" bit in a minimal symbol. */
082fc60d 78
0963b4bd 79#define MSYMBOL_SET_SPECIAL(msym) \
b887350f 80 MSYMBOL_TARGET_FLAG_1 (msym) = 1
082fc60d
RE
81
82#define MSYMBOL_IS_SPECIAL(msym) \
b887350f 83 MSYMBOL_TARGET_FLAG_1 (msym)
082fc60d 84
60c5725c
DJ
85/* Per-objfile data used for mapping symbols. */
86static const struct objfile_data *arm_objfile_data_key;
87
88struct arm_mapping_symbol
89{
90 bfd_vma value;
91 char type;
92};
93typedef struct arm_mapping_symbol arm_mapping_symbol_s;
94DEF_VEC_O(arm_mapping_symbol_s);
95
96struct arm_per_objfile
97{
98 VEC(arm_mapping_symbol_s) **section_maps;
99};
100
afd7eef0
RE
101/* The list of available "set arm ..." and "show arm ..." commands. */
102static struct cmd_list_element *setarmcmdlist = NULL;
103static struct cmd_list_element *showarmcmdlist = NULL;
104
fd50bc42
RE
105/* The type of floating-point to use. Keep this in sync with enum
106 arm_float_model, and the help string in _initialize_arm_tdep. */
40478521 107static const char *const fp_model_strings[] =
fd50bc42
RE
108{
109 "auto",
110 "softfpa",
111 "fpa",
112 "softvfp",
28e97307
DJ
113 "vfp",
114 NULL
fd50bc42
RE
115};
116
117/* A variable that can be configured by the user. */
118static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
119static const char *current_fp_model = "auto";
120
28e97307 121/* The ABI to use. Keep this in sync with arm_abi_kind. */
40478521 122static const char *const arm_abi_strings[] =
28e97307
DJ
123{
124 "auto",
125 "APCS",
126 "AAPCS",
127 NULL
128};
129
130/* A variable that can be configured by the user. */
131static enum arm_abi_kind arm_abi_global = ARM_ABI_AUTO;
132static const char *arm_abi_string = "auto";
133
0428b8f5 134/* The execution mode to assume. */
40478521 135static const char *const arm_mode_strings[] =
0428b8f5
DJ
136 {
137 "auto",
138 "arm",
68770265
MGD
139 "thumb",
140 NULL
0428b8f5
DJ
141 };
142
143static const char *arm_fallback_mode_string = "auto";
144static const char *arm_force_mode_string = "auto";
145
18819fa6
UW
146/* Internal override of the execution mode. -1 means no override,
147 0 means override to ARM mode, 1 means override to Thumb mode.
148 The effect is the same as if arm_force_mode has been set by the
149 user (except the internal override has precedence over a user's
150 arm_force_mode override). */
151static int arm_override_mode = -1;
152
94c30b78 153/* Number of different reg name sets (options). */
afd7eef0 154static int num_disassembly_options;
bc90b915 155
f32bf4a4
YQ
156/* The standard register names, and all the valid aliases for them. Note
157 that `fp', `sp' and `pc' are not added in this alias list, because they
158 have been added as builtin user registers in
159 std-regs.c:_initialize_frame_reg. */
123dc839
DJ
160static const struct
161{
162 const char *name;
163 int regnum;
164} arm_register_aliases[] = {
165 /* Basic register numbers. */
166 { "r0", 0 },
167 { "r1", 1 },
168 { "r2", 2 },
169 { "r3", 3 },
170 { "r4", 4 },
171 { "r5", 5 },
172 { "r6", 6 },
173 { "r7", 7 },
174 { "r8", 8 },
175 { "r9", 9 },
176 { "r10", 10 },
177 { "r11", 11 },
178 { "r12", 12 },
179 { "r13", 13 },
180 { "r14", 14 },
181 { "r15", 15 },
182 /* Synonyms (argument and variable registers). */
183 { "a1", 0 },
184 { "a2", 1 },
185 { "a3", 2 },
186 { "a4", 3 },
187 { "v1", 4 },
188 { "v2", 5 },
189 { "v3", 6 },
190 { "v4", 7 },
191 { "v5", 8 },
192 { "v6", 9 },
193 { "v7", 10 },
194 { "v8", 11 },
195 /* Other platform-specific names for r9. */
196 { "sb", 9 },
197 { "tr", 9 },
198 /* Special names. */
199 { "ip", 12 },
123dc839 200 { "lr", 14 },
123dc839
DJ
201 /* Names used by GCC (not listed in the ARM EABI). */
202 { "sl", 10 },
123dc839
DJ
203 /* A special name from the older ATPCS. */
204 { "wr", 7 },
205};
bc90b915 206
123dc839 207static const char *const arm_register_names[] =
da59e081
JM
208{"r0", "r1", "r2", "r3", /* 0 1 2 3 */
209 "r4", "r5", "r6", "r7", /* 4 5 6 7 */
210 "r8", "r9", "r10", "r11", /* 8 9 10 11 */
211 "r12", "sp", "lr", "pc", /* 12 13 14 15 */
212 "f0", "f1", "f2", "f3", /* 16 17 18 19 */
213 "f4", "f5", "f6", "f7", /* 20 21 22 23 */
94c30b78 214 "fps", "cpsr" }; /* 24 25 */
ed9a39eb 215
afd7eef0
RE
216/* Valid register name styles. */
217static const char **valid_disassembly_styles;
ed9a39eb 218
afd7eef0
RE
219/* Disassembly style to use. Default to "std" register names. */
220static const char *disassembly_style;
96baa820 221
ed9a39eb 222/* This is used to keep the bfd arch_info in sync with the disassembly
afd7eef0
RE
223 style. */
224static void set_disassembly_style_sfunc(char *, int,
ed9a39eb 225 struct cmd_list_element *);
afd7eef0 226static void set_disassembly_style (void);
ed9a39eb 227
b508a996 228static void convert_from_extended (const struct floatformat *, const void *,
be8626e0 229 void *, int);
b508a996 230static void convert_to_extended (const struct floatformat *, void *,
be8626e0 231 const void *, int);
ed9a39eb 232
05d1431c
PA
233static enum register_status arm_neon_quad_read (struct gdbarch *gdbarch,
234 struct regcache *regcache,
235 int regnum, gdb_byte *buf);
58d6951d
DJ
236static void arm_neon_quad_write (struct gdbarch *gdbarch,
237 struct regcache *regcache,
238 int regnum, const gdb_byte *buf);
239
e7cf25a8
YQ
240static CORE_ADDR
241 arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self,
242 CORE_ADDR pc);
243
244
d9311bfa
AT
245/* get_next_pcs operations. */
246static struct arm_get_next_pcs_ops arm_get_next_pcs_ops = {
247 arm_get_next_pcs_read_memory_unsigned_integer,
248 arm_get_next_pcs_syscall_next_pc,
249 arm_get_next_pcs_addr_bits_remove,
ed443b61
YQ
250 arm_get_next_pcs_is_thumb,
251 NULL,
d9311bfa
AT
252};
253
9b8d791a 254struct arm_prologue_cache
c3b4394c 255{
eb5492fa
DJ
256 /* The stack pointer at the time this frame was created; i.e. the
257 caller's stack pointer when this function was called. It is used
258 to identify this frame. */
259 CORE_ADDR prev_sp;
260
4be43953
DJ
261 /* The frame base for this frame is just prev_sp - frame size.
262 FRAMESIZE is the distance from the frame pointer to the
263 initial stack pointer. */
eb5492fa 264
c3b4394c 265 int framesize;
eb5492fa
DJ
266
267 /* The register used to hold the frame pointer for this frame. */
c3b4394c 268 int framereg;
eb5492fa
DJ
269
270 /* Saved register offsets. */
271 struct trad_frame_saved_reg *saved_regs;
c3b4394c 272};
ed9a39eb 273
0d39a070
DJ
274static CORE_ADDR arm_analyze_prologue (struct gdbarch *gdbarch,
275 CORE_ADDR prologue_start,
276 CORE_ADDR prologue_end,
277 struct arm_prologue_cache *cache);
278
cca44b1b
JB
279/* Architecture version for displaced stepping. This effects the behaviour of
280 certain instructions, and really should not be hard-wired. */
281
282#define DISPLACED_STEPPING_ARCH_VERSION 5
283
94c30b78 284/* Set to true if the 32-bit mode is in use. */
c906108c
SS
285
286int arm_apcs_32 = 1;
287
9779414d
DJ
288/* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode. */
289
478fd957 290int
9779414d
DJ
291arm_psr_thumb_bit (struct gdbarch *gdbarch)
292{
293 if (gdbarch_tdep (gdbarch)->is_m)
294 return XPSR_T;
295 else
296 return CPSR_T;
297}
298
d0e59a68
AT
299/* Determine if the processor is currently executing in Thumb mode. */
300
301int
302arm_is_thumb (struct regcache *regcache)
303{
304 ULONGEST cpsr;
305 ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regcache));
306
307 cpsr = regcache_raw_get_unsigned (regcache, ARM_PS_REGNUM);
308
309 return (cpsr & t_bit) != 0;
310}
311
b39cc962
DJ
312/* Determine if FRAME is executing in Thumb mode. */
313
25b41d01 314int
b39cc962
DJ
315arm_frame_is_thumb (struct frame_info *frame)
316{
317 CORE_ADDR cpsr;
9779414d 318 ULONGEST t_bit = arm_psr_thumb_bit (get_frame_arch (frame));
b39cc962
DJ
319
320 /* Every ARM frame unwinder can unwind the T bit of the CPSR, either
321 directly (from a signal frame or dummy frame) or by interpreting
322 the saved LR (from a prologue or DWARF frame). So consult it and
323 trust the unwinders. */
324 cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
325
9779414d 326 return (cpsr & t_bit) != 0;
b39cc962
DJ
327}
328
60c5725c
DJ
329/* Callback for VEC_lower_bound. */
330
331static inline int
332arm_compare_mapping_symbols (const struct arm_mapping_symbol *lhs,
333 const struct arm_mapping_symbol *rhs)
334{
335 return lhs->value < rhs->value;
336}
337
f9d67f43
DJ
338/* Search for the mapping symbol covering MEMADDR. If one is found,
339 return its type. Otherwise, return 0. If START is non-NULL,
340 set *START to the location of the mapping symbol. */
c906108c 341
f9d67f43
DJ
342static char
343arm_find_mapping_symbol (CORE_ADDR memaddr, CORE_ADDR *start)
c906108c 344{
60c5725c 345 struct obj_section *sec;
0428b8f5 346
60c5725c
DJ
347 /* If there are mapping symbols, consult them. */
348 sec = find_pc_section (memaddr);
349 if (sec != NULL)
350 {
351 struct arm_per_objfile *data;
352 VEC(arm_mapping_symbol_s) *map;
aded6f54
PA
353 struct arm_mapping_symbol map_key = { memaddr - obj_section_addr (sec),
354 0 };
60c5725c
DJ
355 unsigned int idx;
356
9a3c8263
SM
357 data = (struct arm_per_objfile *) objfile_data (sec->objfile,
358 arm_objfile_data_key);
60c5725c
DJ
359 if (data != NULL)
360 {
361 map = data->section_maps[sec->the_bfd_section->index];
362 if (!VEC_empty (arm_mapping_symbol_s, map))
363 {
364 struct arm_mapping_symbol *map_sym;
365
366 idx = VEC_lower_bound (arm_mapping_symbol_s, map, &map_key,
367 arm_compare_mapping_symbols);
368
369 /* VEC_lower_bound finds the earliest ordered insertion
370 point. If the following symbol starts at this exact
371 address, we use that; otherwise, the preceding
372 mapping symbol covers this address. */
373 if (idx < VEC_length (arm_mapping_symbol_s, map))
374 {
375 map_sym = VEC_index (arm_mapping_symbol_s, map, idx);
376 if (map_sym->value == map_key.value)
f9d67f43
DJ
377 {
378 if (start)
379 *start = map_sym->value + obj_section_addr (sec);
380 return map_sym->type;
381 }
60c5725c
DJ
382 }
383
384 if (idx > 0)
385 {
386 map_sym = VEC_index (arm_mapping_symbol_s, map, idx - 1);
f9d67f43
DJ
387 if (start)
388 *start = map_sym->value + obj_section_addr (sec);
389 return map_sym->type;
60c5725c
DJ
390 }
391 }
392 }
393 }
394
f9d67f43
DJ
395 return 0;
396}
397
398/* Determine if the program counter specified in MEMADDR is in a Thumb
399 function. This function should be called for addresses unrelated to
400 any executing frame; otherwise, prefer arm_frame_is_thumb. */
401
e3039479 402int
9779414d 403arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
f9d67f43 404{
7cbd4a93 405 struct bound_minimal_symbol sym;
f9d67f43 406 char type;
a42244db
YQ
407 struct displaced_step_closure* dsc
408 = get_displaced_step_closure_by_addr(memaddr);
409
410 /* If checking the mode of displaced instruction in copy area, the mode
411 should be determined by instruction on the original address. */
412 if (dsc)
413 {
414 if (debug_displaced)
415 fprintf_unfiltered (gdb_stdlog,
416 "displaced: check mode of %.8lx instead of %.8lx\n",
417 (unsigned long) dsc->insn_addr,
418 (unsigned long) memaddr);
419 memaddr = dsc->insn_addr;
420 }
f9d67f43
DJ
421
422 /* If bit 0 of the address is set, assume this is a Thumb address. */
423 if (IS_THUMB_ADDR (memaddr))
424 return 1;
425
18819fa6
UW
426 /* Respect internal mode override if active. */
427 if (arm_override_mode != -1)
428 return arm_override_mode;
429
f9d67f43
DJ
430 /* If the user wants to override the symbol table, let him. */
431 if (strcmp (arm_force_mode_string, "arm") == 0)
432 return 0;
433 if (strcmp (arm_force_mode_string, "thumb") == 0)
434 return 1;
435
9779414d
DJ
436 /* ARM v6-M and v7-M are always in Thumb mode. */
437 if (gdbarch_tdep (gdbarch)->is_m)
438 return 1;
439
f9d67f43
DJ
440 /* If there are mapping symbols, consult them. */
441 type = arm_find_mapping_symbol (memaddr, NULL);
442 if (type)
443 return type == 't';
444
ed9a39eb 445 /* Thumb functions have a "special" bit set in minimal symbols. */
c906108c 446 sym = lookup_minimal_symbol_by_pc (memaddr);
7cbd4a93
TT
447 if (sym.minsym)
448 return (MSYMBOL_IS_SPECIAL (sym.minsym));
0428b8f5
DJ
449
450 /* If the user wants to override the fallback mode, let them. */
451 if (strcmp (arm_fallback_mode_string, "arm") == 0)
452 return 0;
453 if (strcmp (arm_fallback_mode_string, "thumb") == 0)
454 return 1;
455
456 /* If we couldn't find any symbol, but we're talking to a running
457 target, then trust the current value of $cpsr. This lets
458 "display/i $pc" always show the correct mode (though if there is
459 a symbol table we will not reach here, so it still may not be
18819fa6 460 displayed in the mode it will be executed). */
0428b8f5 461 if (target_has_registers)
18819fa6 462 return arm_frame_is_thumb (get_current_frame ());
0428b8f5
DJ
463
464 /* Otherwise we're out of luck; we assume ARM. */
465 return 0;
c906108c
SS
466}
467
181c1381 468/* Remove useless bits from addresses in a running program. */
34e8f22d 469static CORE_ADDR
24568a2c 470arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
c906108c 471{
2ae28aa9
YQ
472 /* On M-profile devices, do not strip the low bit from EXC_RETURN
473 (the magic exception return address). */
474 if (gdbarch_tdep (gdbarch)->is_m
475 && (val & 0xfffffff0) == 0xfffffff0)
476 return val;
477
a3a2ee65 478 if (arm_apcs_32)
dd6be234 479 return UNMAKE_THUMB_ADDR (val);
c906108c 480 else
a3a2ee65 481 return (val & 0x03fffffc);
c906108c
SS
482}
483
0d39a070 484/* Return 1 if PC is the start of a compiler helper function which
e0634ccf
UW
485 can be safely ignored during prologue skipping. IS_THUMB is true
486 if the function is known to be a Thumb function due to the way it
487 is being called. */
0d39a070 488static int
e0634ccf 489skip_prologue_function (struct gdbarch *gdbarch, CORE_ADDR pc, int is_thumb)
0d39a070 490{
e0634ccf 491 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7cbd4a93 492 struct bound_minimal_symbol msym;
0d39a070
DJ
493
494 msym = lookup_minimal_symbol_by_pc (pc);
7cbd4a93 495 if (msym.minsym != NULL
77e371c0 496 && BMSYMBOL_VALUE_ADDRESS (msym) == pc
efd66ac6 497 && MSYMBOL_LINKAGE_NAME (msym.minsym) != NULL)
e0634ccf 498 {
efd66ac6 499 const char *name = MSYMBOL_LINKAGE_NAME (msym.minsym);
0d39a070 500
e0634ccf
UW
501 /* The GNU linker's Thumb call stub to foo is named
502 __foo_from_thumb. */
503 if (strstr (name, "_from_thumb") != NULL)
504 name += 2;
0d39a070 505
e0634ccf
UW
506 /* On soft-float targets, __truncdfsf2 is called to convert promoted
507 arguments to their argument types in non-prototyped
508 functions. */
61012eef 509 if (startswith (name, "__truncdfsf2"))
e0634ccf 510 return 1;
61012eef 511 if (startswith (name, "__aeabi_d2f"))
e0634ccf 512 return 1;
0d39a070 513
e0634ccf 514 /* Internal functions related to thread-local storage. */
61012eef 515 if (startswith (name, "__tls_get_addr"))
e0634ccf 516 return 1;
61012eef 517 if (startswith (name, "__aeabi_read_tp"))
e0634ccf
UW
518 return 1;
519 }
520 else
521 {
522 /* If we run against a stripped glibc, we may be unable to identify
523 special functions by name. Check for one important case,
524 __aeabi_read_tp, by comparing the *code* against the default
525 implementation (this is hand-written ARM assembler in glibc). */
526
527 if (!is_thumb
528 && read_memory_unsigned_integer (pc, 4, byte_order_for_code)
529 == 0xe3e00a0f /* mov r0, #0xffff0fff */
530 && read_memory_unsigned_integer (pc + 4, 4, byte_order_for_code)
531 == 0xe240f01f) /* sub pc, r0, #31 */
532 return 1;
533 }
ec3d575a 534
0d39a070
DJ
535 return 0;
536}
537
621c6d5b
YQ
538/* Extract the immediate from instruction movw/movt of encoding T. INSN1 is
539 the first 16-bit of instruction, and INSN2 is the second 16-bit of
540 instruction. */
541#define EXTRACT_MOVW_MOVT_IMM_T(insn1, insn2) \
542 ((bits ((insn1), 0, 3) << 12) \
543 | (bits ((insn1), 10, 10) << 11) \
544 | (bits ((insn2), 12, 14) << 8) \
545 | bits ((insn2), 0, 7))
546
547/* Extract the immediate from instruction movw/movt of encoding A. INSN is
548 the 32-bit instruction. */
549#define EXTRACT_MOVW_MOVT_IMM_A(insn) \
550 ((bits ((insn), 16, 19) << 12) \
551 | bits ((insn), 0, 11))
552
ec3d575a
UW
553/* Decode immediate value; implements ThumbExpandImmediate pseudo-op. */
554
555static unsigned int
556thumb_expand_immediate (unsigned int imm)
557{
558 unsigned int count = imm >> 7;
559
560 if (count < 8)
561 switch (count / 2)
562 {
563 case 0:
564 return imm & 0xff;
565 case 1:
566 return (imm & 0xff) | ((imm & 0xff) << 16);
567 case 2:
568 return ((imm & 0xff) << 8) | ((imm & 0xff) << 24);
569 case 3:
570 return (imm & 0xff) | ((imm & 0xff) << 8)
571 | ((imm & 0xff) << 16) | ((imm & 0xff) << 24);
572 }
573
574 return (0x80 | (imm & 0x7f)) << (32 - count);
575}
576
540314bd
YQ
577/* Return 1 if the 16-bit Thumb instruction INSN restores SP in
578 epilogue, 0 otherwise. */
579
580static int
581thumb_instruction_restores_sp (unsigned short insn)
582{
583 return (insn == 0x46bd /* mov sp, r7 */
584 || (insn & 0xff80) == 0xb000 /* add sp, imm */
585 || (insn & 0xfe00) == 0xbc00); /* pop <registers> */
586}
587
29d73ae4
DJ
588/* Analyze a Thumb prologue, looking for a recognizable stack frame
589 and frame pointer. Scan until we encounter a store that could
0d39a070
DJ
590 clobber the stack frame unexpectedly, or an unknown instruction.
591 Return the last address which is definitely safe to skip for an
592 initial breakpoint. */
c906108c
SS
593
594static CORE_ADDR
29d73ae4
DJ
595thumb_analyze_prologue (struct gdbarch *gdbarch,
596 CORE_ADDR start, CORE_ADDR limit,
597 struct arm_prologue_cache *cache)
c906108c 598{
0d39a070 599 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
e17a4113 600 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
29d73ae4
DJ
601 int i;
602 pv_t regs[16];
603 struct pv_area *stack;
604 struct cleanup *back_to;
605 CORE_ADDR offset;
ec3d575a 606 CORE_ADDR unrecognized_pc = 0;
da3c6d4a 607
29d73ae4
DJ
608 for (i = 0; i < 16; i++)
609 regs[i] = pv_register (i, 0);
55f960e1 610 stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
29d73ae4
DJ
611 back_to = make_cleanup_free_pv_area (stack);
612
29d73ae4 613 while (start < limit)
c906108c 614 {
29d73ae4
DJ
615 unsigned short insn;
616
e17a4113 617 insn = read_memory_unsigned_integer (start, 2, byte_order_for_code);
9d4fde75 618
94c30b78 619 if ((insn & 0xfe00) == 0xb400) /* push { rlist } */
da59e081 620 {
29d73ae4
DJ
621 int regno;
622 int mask;
4be43953
DJ
623
624 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
625 break;
29d73ae4
DJ
626
627 /* Bits 0-7 contain a mask for registers R0-R7. Bit 8 says
628 whether to save LR (R14). */
629 mask = (insn & 0xff) | ((insn & 0x100) << 6);
630
631 /* Calculate offsets of saved R0-R7 and LR. */
632 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
633 if (mask & (1 << regno))
634 {
29d73ae4
DJ
635 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
636 -4);
637 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
638 }
da59e081 639 }
1db01f22 640 else if ((insn & 0xff80) == 0xb080) /* sub sp, #imm */
da59e081 641 {
29d73ae4 642 offset = (insn & 0x7f) << 2; /* get scaled offset */
1db01f22
YQ
643 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
644 -offset);
da59e081 645 }
808f7ab1
YQ
646 else if (thumb_instruction_restores_sp (insn))
647 {
648 /* Don't scan past the epilogue. */
649 break;
650 }
0d39a070
DJ
651 else if ((insn & 0xf800) == 0xa800) /* add Rd, sp, #imm */
652 regs[bits (insn, 8, 10)] = pv_add_constant (regs[ARM_SP_REGNUM],
653 (insn & 0xff) << 2);
654 else if ((insn & 0xfe00) == 0x1c00 /* add Rd, Rn, #imm */
655 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
656 regs[bits (insn, 0, 2)] = pv_add_constant (regs[bits (insn, 3, 5)],
657 bits (insn, 6, 8));
658 else if ((insn & 0xf800) == 0x3000 /* add Rd, #imm */
659 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
660 regs[bits (insn, 8, 10)] = pv_add_constant (regs[bits (insn, 8, 10)],
661 bits (insn, 0, 7));
662 else if ((insn & 0xfe00) == 0x1800 /* add Rd, Rn, Rm */
663 && pv_is_register (regs[bits (insn, 6, 8)], ARM_SP_REGNUM)
664 && pv_is_constant (regs[bits (insn, 3, 5)]))
665 regs[bits (insn, 0, 2)] = pv_add (regs[bits (insn, 3, 5)],
666 regs[bits (insn, 6, 8)]);
667 else if ((insn & 0xff00) == 0x4400 /* add Rd, Rm */
668 && pv_is_constant (regs[bits (insn, 3, 6)]))
669 {
670 int rd = (bit (insn, 7) << 3) + bits (insn, 0, 2);
671 int rm = bits (insn, 3, 6);
672 regs[rd] = pv_add (regs[rd], regs[rm]);
673 }
29d73ae4 674 else if ((insn & 0xff00) == 0x4600) /* mov hi, lo or mov lo, hi */
da59e081 675 {
29d73ae4
DJ
676 int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
677 int src_reg = (insn & 0x78) >> 3;
678 regs[dst_reg] = regs[src_reg];
da59e081 679 }
29d73ae4 680 else if ((insn & 0xf800) == 0x9000) /* str rd, [sp, #off] */
da59e081 681 {
29d73ae4
DJ
682 /* Handle stores to the stack. Normally pushes are used,
683 but with GCC -mtpcs-frame, there may be other stores
684 in the prologue to create the frame. */
685 int regno = (insn >> 8) & 0x7;
686 pv_t addr;
687
688 offset = (insn & 0xff) << 2;
689 addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
690
691 if (pv_area_store_would_trash (stack, addr))
692 break;
693
694 pv_area_store (stack, addr, 4, regs[regno]);
da59e081 695 }
0d39a070
DJ
696 else if ((insn & 0xf800) == 0x6000) /* str rd, [rn, #off] */
697 {
698 int rd = bits (insn, 0, 2);
699 int rn = bits (insn, 3, 5);
700 pv_t addr;
701
702 offset = bits (insn, 6, 10) << 2;
703 addr = pv_add_constant (regs[rn], offset);
704
705 if (pv_area_store_would_trash (stack, addr))
706 break;
707
708 pv_area_store (stack, addr, 4, regs[rd]);
709 }
710 else if (((insn & 0xf800) == 0x7000 /* strb Rd, [Rn, #off] */
711 || (insn & 0xf800) == 0x8000) /* strh Rd, [Rn, #off] */
712 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
713 /* Ignore stores of argument registers to the stack. */
714 ;
715 else if ((insn & 0xf800) == 0xc800 /* ldmia Rn!, { registers } */
716 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
717 /* Ignore block loads from the stack, potentially copying
718 parameters from memory. */
719 ;
720 else if ((insn & 0xf800) == 0x9800 /* ldr Rd, [Rn, #immed] */
721 || ((insn & 0xf800) == 0x6800 /* ldr Rd, [sp, #immed] */
722 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM)))
723 /* Similarly ignore single loads from the stack. */
724 ;
725 else if ((insn & 0xffc0) == 0x0000 /* lsls Rd, Rm, #0 */
726 || (insn & 0xffc0) == 0x1c00) /* add Rd, Rn, #0 */
727 /* Skip register copies, i.e. saves to another register
728 instead of the stack. */
729 ;
730 else if ((insn & 0xf800) == 0x2000) /* movs Rd, #imm */
731 /* Recognize constant loads; even with small stacks these are necessary
732 on Thumb. */
733 regs[bits (insn, 8, 10)] = pv_constant (bits (insn, 0, 7));
734 else if ((insn & 0xf800) == 0x4800) /* ldr Rd, [pc, #imm] */
735 {
736 /* Constant pool loads, for the same reason. */
737 unsigned int constant;
738 CORE_ADDR loc;
739
740 loc = start + 4 + bits (insn, 0, 7) * 4;
741 constant = read_memory_unsigned_integer (loc, 4, byte_order);
742 regs[bits (insn, 8, 10)] = pv_constant (constant);
743 }
db24da6d 744 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instructions. */
0d39a070 745 {
0d39a070
DJ
746 unsigned short inst2;
747
748 inst2 = read_memory_unsigned_integer (start + 2, 2,
749 byte_order_for_code);
750
751 if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
752 {
753 /* BL, BLX. Allow some special function calls when
754 skipping the prologue; GCC generates these before
755 storing arguments to the stack. */
756 CORE_ADDR nextpc;
757 int j1, j2, imm1, imm2;
758
759 imm1 = sbits (insn, 0, 10);
760 imm2 = bits (inst2, 0, 10);
761 j1 = bit (inst2, 13);
762 j2 = bit (inst2, 11);
763
764 offset = ((imm1 << 12) + (imm2 << 1));
765 offset ^= ((!j2) << 22) | ((!j1) << 23);
766
767 nextpc = start + 4 + offset;
768 /* For BLX make sure to clear the low bits. */
769 if (bit (inst2, 12) == 0)
770 nextpc = nextpc & 0xfffffffc;
771
e0634ccf
UW
772 if (!skip_prologue_function (gdbarch, nextpc,
773 bit (inst2, 12) != 0))
0d39a070
DJ
774 break;
775 }
ec3d575a 776
0963b4bd
MS
777 else if ((insn & 0xffd0) == 0xe900 /* stmdb Rn{!},
778 { registers } */
ec3d575a
UW
779 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
780 {
781 pv_t addr = regs[bits (insn, 0, 3)];
782 int regno;
783
784 if (pv_area_store_would_trash (stack, addr))
785 break;
786
787 /* Calculate offsets of saved registers. */
788 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
789 if (inst2 & (1 << regno))
790 {
791 addr = pv_add_constant (addr, -4);
792 pv_area_store (stack, addr, 4, regs[regno]);
793 }
794
795 if (insn & 0x0020)
796 regs[bits (insn, 0, 3)] = addr;
797 }
798
0963b4bd
MS
799 else if ((insn & 0xff50) == 0xe940 /* strd Rt, Rt2,
800 [Rn, #+/-imm]{!} */
ec3d575a
UW
801 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
802 {
803 int regno1 = bits (inst2, 12, 15);
804 int regno2 = bits (inst2, 8, 11);
805 pv_t addr = regs[bits (insn, 0, 3)];
806
807 offset = inst2 & 0xff;
808 if (insn & 0x0080)
809 addr = pv_add_constant (addr, offset);
810 else
811 addr = pv_add_constant (addr, -offset);
812
813 if (pv_area_store_would_trash (stack, addr))
814 break;
815
816 pv_area_store (stack, addr, 4, regs[regno1]);
817 pv_area_store (stack, pv_add_constant (addr, 4),
818 4, regs[regno2]);
819
820 if (insn & 0x0020)
821 regs[bits (insn, 0, 3)] = addr;
822 }
823
824 else if ((insn & 0xfff0) == 0xf8c0 /* str Rt,[Rn,+/-#imm]{!} */
825 && (inst2 & 0x0c00) == 0x0c00
826 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
827 {
828 int regno = bits (inst2, 12, 15);
829 pv_t addr = regs[bits (insn, 0, 3)];
830
831 offset = inst2 & 0xff;
832 if (inst2 & 0x0200)
833 addr = pv_add_constant (addr, offset);
834 else
835 addr = pv_add_constant (addr, -offset);
836
837 if (pv_area_store_would_trash (stack, addr))
838 break;
839
840 pv_area_store (stack, addr, 4, regs[regno]);
841
842 if (inst2 & 0x0100)
843 regs[bits (insn, 0, 3)] = addr;
844 }
845
846 else if ((insn & 0xfff0) == 0xf8c0 /* str.w Rt,[Rn,#imm] */
847 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
848 {
849 int regno = bits (inst2, 12, 15);
850 pv_t addr;
851
852 offset = inst2 & 0xfff;
853 addr = pv_add_constant (regs[bits (insn, 0, 3)], offset);
854
855 if (pv_area_store_would_trash (stack, addr))
856 break;
857
858 pv_area_store (stack, addr, 4, regs[regno]);
859 }
860
861 else if ((insn & 0xffd0) == 0xf880 /* str{bh}.w Rt,[Rn,#imm] */
0d39a070 862 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 863 /* Ignore stores of argument registers to the stack. */
0d39a070 864 ;
ec3d575a
UW
865
866 else if ((insn & 0xffd0) == 0xf800 /* str{bh} Rt,[Rn,#+/-imm] */
867 && (inst2 & 0x0d00) == 0x0c00
0d39a070 868 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 869 /* Ignore stores of argument registers to the stack. */
0d39a070 870 ;
ec3d575a 871
0963b4bd
MS
872 else if ((insn & 0xffd0) == 0xe890 /* ldmia Rn[!],
873 { registers } */
ec3d575a
UW
874 && (inst2 & 0x8000) == 0x0000
875 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
876 /* Ignore block loads from the stack, potentially copying
877 parameters from memory. */
0d39a070 878 ;
ec3d575a 879
0963b4bd
MS
880 else if ((insn & 0xffb0) == 0xe950 /* ldrd Rt, Rt2,
881 [Rn, #+/-imm] */
0d39a070 882 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 883 /* Similarly ignore dual loads from the stack. */
0d39a070 884 ;
ec3d575a
UW
885
886 else if ((insn & 0xfff0) == 0xf850 /* ldr Rt,[Rn,#+/-imm] */
887 && (inst2 & 0x0d00) == 0x0c00
0d39a070 888 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 889 /* Similarly ignore single loads from the stack. */
0d39a070 890 ;
ec3d575a
UW
891
892 else if ((insn & 0xfff0) == 0xf8d0 /* ldr.w Rt,[Rn,#imm] */
0d39a070 893 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 894 /* Similarly ignore single loads from the stack. */
0d39a070 895 ;
ec3d575a
UW
896
897 else if ((insn & 0xfbf0) == 0xf100 /* add.w Rd, Rn, #imm */
898 && (inst2 & 0x8000) == 0x0000)
899 {
900 unsigned int imm = ((bits (insn, 10, 10) << 11)
901 | (bits (inst2, 12, 14) << 8)
902 | bits (inst2, 0, 7));
903
904 regs[bits (inst2, 8, 11)]
905 = pv_add_constant (regs[bits (insn, 0, 3)],
906 thumb_expand_immediate (imm));
907 }
908
909 else if ((insn & 0xfbf0) == 0xf200 /* addw Rd, Rn, #imm */
910 && (inst2 & 0x8000) == 0x0000)
0d39a070 911 {
ec3d575a
UW
912 unsigned int imm = ((bits (insn, 10, 10) << 11)
913 | (bits (inst2, 12, 14) << 8)
914 | bits (inst2, 0, 7));
915
916 regs[bits (inst2, 8, 11)]
917 = pv_add_constant (regs[bits (insn, 0, 3)], imm);
918 }
919
920 else if ((insn & 0xfbf0) == 0xf1a0 /* sub.w Rd, Rn, #imm */
921 && (inst2 & 0x8000) == 0x0000)
922 {
923 unsigned int imm = ((bits (insn, 10, 10) << 11)
924 | (bits (inst2, 12, 14) << 8)
925 | bits (inst2, 0, 7));
926
927 regs[bits (inst2, 8, 11)]
928 = pv_add_constant (regs[bits (insn, 0, 3)],
929 - (CORE_ADDR) thumb_expand_immediate (imm));
930 }
931
932 else if ((insn & 0xfbf0) == 0xf2a0 /* subw Rd, Rn, #imm */
933 && (inst2 & 0x8000) == 0x0000)
934 {
935 unsigned int imm = ((bits (insn, 10, 10) << 11)
936 | (bits (inst2, 12, 14) << 8)
937 | bits (inst2, 0, 7));
938
939 regs[bits (inst2, 8, 11)]
940 = pv_add_constant (regs[bits (insn, 0, 3)], - (CORE_ADDR) imm);
941 }
942
943 else if ((insn & 0xfbff) == 0xf04f) /* mov.w Rd, #const */
944 {
945 unsigned int imm = ((bits (insn, 10, 10) << 11)
946 | (bits (inst2, 12, 14) << 8)
947 | bits (inst2, 0, 7));
948
949 regs[bits (inst2, 8, 11)]
950 = pv_constant (thumb_expand_immediate (imm));
951 }
952
953 else if ((insn & 0xfbf0) == 0xf240) /* movw Rd, #const */
954 {
621c6d5b
YQ
955 unsigned int imm
956 = EXTRACT_MOVW_MOVT_IMM_T (insn, inst2);
ec3d575a
UW
957
958 regs[bits (inst2, 8, 11)] = pv_constant (imm);
959 }
960
961 else if (insn == 0xea5f /* mov.w Rd,Rm */
962 && (inst2 & 0xf0f0) == 0)
963 {
964 int dst_reg = (inst2 & 0x0f00) >> 8;
965 int src_reg = inst2 & 0xf;
966 regs[dst_reg] = regs[src_reg];
967 }
968
969 else if ((insn & 0xff7f) == 0xf85f) /* ldr.w Rt,<label> */
970 {
971 /* Constant pool loads. */
972 unsigned int constant;
973 CORE_ADDR loc;
974
cac395ea 975 offset = bits (inst2, 0, 11);
ec3d575a
UW
976 if (insn & 0x0080)
977 loc = start + 4 + offset;
978 else
979 loc = start + 4 - offset;
980
981 constant = read_memory_unsigned_integer (loc, 4, byte_order);
982 regs[bits (inst2, 12, 15)] = pv_constant (constant);
983 }
984
985 else if ((insn & 0xff7f) == 0xe95f) /* ldrd Rt,Rt2,<label> */
986 {
987 /* Constant pool loads. */
988 unsigned int constant;
989 CORE_ADDR loc;
990
cac395ea 991 offset = bits (inst2, 0, 7) << 2;
ec3d575a
UW
992 if (insn & 0x0080)
993 loc = start + 4 + offset;
994 else
995 loc = start + 4 - offset;
996
997 constant = read_memory_unsigned_integer (loc, 4, byte_order);
998 regs[bits (inst2, 12, 15)] = pv_constant (constant);
999
1000 constant = read_memory_unsigned_integer (loc + 4, 4, byte_order);
1001 regs[bits (inst2, 8, 11)] = pv_constant (constant);
1002 }
1003
1004 else if (thumb2_instruction_changes_pc (insn, inst2))
1005 {
1006 /* Don't scan past anything that might change control flow. */
0d39a070
DJ
1007 break;
1008 }
ec3d575a
UW
1009 else
1010 {
1011 /* The optimizer might shove anything into the prologue,
1012 so we just skip what we don't recognize. */
1013 unrecognized_pc = start;
1014 }
0d39a070
DJ
1015
1016 start += 2;
1017 }
ec3d575a 1018 else if (thumb_instruction_changes_pc (insn))
3d74b771 1019 {
ec3d575a 1020 /* Don't scan past anything that might change control flow. */
da3c6d4a 1021 break;
3d74b771 1022 }
ec3d575a
UW
1023 else
1024 {
1025 /* The optimizer might shove anything into the prologue,
1026 so we just skip what we don't recognize. */
1027 unrecognized_pc = start;
1028 }
29d73ae4
DJ
1029
1030 start += 2;
c906108c
SS
1031 }
1032
0d39a070
DJ
1033 if (arm_debug)
1034 fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1035 paddress (gdbarch, start));
1036
ec3d575a
UW
1037 if (unrecognized_pc == 0)
1038 unrecognized_pc = start;
1039
29d73ae4
DJ
1040 if (cache == NULL)
1041 {
1042 do_cleanups (back_to);
ec3d575a 1043 return unrecognized_pc;
29d73ae4
DJ
1044 }
1045
29d73ae4
DJ
1046 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1047 {
1048 /* Frame pointer is fp. Frame size is constant. */
1049 cache->framereg = ARM_FP_REGNUM;
1050 cache->framesize = -regs[ARM_FP_REGNUM].k;
1051 }
1052 else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
1053 {
1054 /* Frame pointer is r7. Frame size is constant. */
1055 cache->framereg = THUMB_FP_REGNUM;
1056 cache->framesize = -regs[THUMB_FP_REGNUM].k;
1057 }
72a2e3dc 1058 else
29d73ae4
DJ
1059 {
1060 /* Try the stack pointer... this is a bit desperate. */
1061 cache->framereg = ARM_SP_REGNUM;
1062 cache->framesize = -regs[ARM_SP_REGNUM].k;
1063 }
29d73ae4
DJ
1064
1065 for (i = 0; i < 16; i++)
1066 if (pv_area_find_reg (stack, gdbarch, i, &offset))
1067 cache->saved_regs[i].addr = offset;
1068
1069 do_cleanups (back_to);
ec3d575a 1070 return unrecognized_pc;
c906108c
SS
1071}
1072
621c6d5b
YQ
1073
1074/* Try to analyze the instructions starting from PC, which load symbol
1075 __stack_chk_guard. Return the address of instruction after loading this
1076 symbol, set the dest register number to *BASEREG, and set the size of
1077 instructions for loading symbol in OFFSET. Return 0 if instructions are
1078 not recognized. */
1079
1080static CORE_ADDR
1081arm_analyze_load_stack_chk_guard(CORE_ADDR pc, struct gdbarch *gdbarch,
1082 unsigned int *destreg, int *offset)
1083{
1084 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1085 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1086 unsigned int low, high, address;
1087
1088 address = 0;
1089 if (is_thumb)
1090 {
1091 unsigned short insn1
1092 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
1093
1094 if ((insn1 & 0xf800) == 0x4800) /* ldr Rd, #immed */
1095 {
1096 *destreg = bits (insn1, 8, 10);
1097 *offset = 2;
6ae274b7
YQ
1098 address = (pc & 0xfffffffc) + 4 + (bits (insn1, 0, 7) << 2);
1099 address = read_memory_unsigned_integer (address, 4,
1100 byte_order_for_code);
621c6d5b
YQ
1101 }
1102 else if ((insn1 & 0xfbf0) == 0xf240) /* movw Rd, #const */
1103 {
1104 unsigned short insn2
1105 = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);
1106
1107 low = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1108
1109 insn1
1110 = read_memory_unsigned_integer (pc + 4, 2, byte_order_for_code);
1111 insn2
1112 = read_memory_unsigned_integer (pc + 6, 2, byte_order_for_code);
1113
1114 /* movt Rd, #const */
1115 if ((insn1 & 0xfbc0) == 0xf2c0)
1116 {
1117 high = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1118 *destreg = bits (insn2, 8, 11);
1119 *offset = 8;
1120 address = (high << 16 | low);
1121 }
1122 }
1123 }
1124 else
1125 {
2e9e421f
UW
1126 unsigned int insn
1127 = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
1128
6ae274b7 1129 if ((insn & 0x0e5f0000) == 0x041f0000) /* ldr Rd, [PC, #immed] */
2e9e421f 1130 {
6ae274b7
YQ
1131 address = bits (insn, 0, 11) + pc + 8;
1132 address = read_memory_unsigned_integer (address, 4,
1133 byte_order_for_code);
1134
2e9e421f
UW
1135 *destreg = bits (insn, 12, 15);
1136 *offset = 4;
1137 }
1138 else if ((insn & 0x0ff00000) == 0x03000000) /* movw Rd, #const */
1139 {
1140 low = EXTRACT_MOVW_MOVT_IMM_A (insn);
1141
1142 insn
1143 = read_memory_unsigned_integer (pc + 4, 4, byte_order_for_code);
1144
1145 if ((insn & 0x0ff00000) == 0x03400000) /* movt Rd, #const */
1146 {
1147 high = EXTRACT_MOVW_MOVT_IMM_A (insn);
1148 *destreg = bits (insn, 12, 15);
1149 *offset = 8;
1150 address = (high << 16 | low);
1151 }
1152 }
621c6d5b
YQ
1153 }
1154
1155 return address;
1156}
1157
1158/* Try to skip a sequence of instructions used for stack protector. If PC
0963b4bd
MS
1159 points to the first instruction of this sequence, return the address of
1160 first instruction after this sequence, otherwise, return original PC.
621c6d5b
YQ
1161
1162 On arm, this sequence of instructions is composed of mainly three steps,
1163 Step 1: load symbol __stack_chk_guard,
1164 Step 2: load from address of __stack_chk_guard,
1165 Step 3: store it to somewhere else.
1166
1167 Usually, instructions on step 2 and step 3 are the same on various ARM
1168 architectures. On step 2, it is one instruction 'ldr Rx, [Rn, #0]', and
1169 on step 3, it is also one instruction 'str Rx, [r7, #immd]'. However,
1170 instructions in step 1 vary from different ARM architectures. On ARMv7,
1171 they are,
1172
1173 movw Rn, #:lower16:__stack_chk_guard
1174 movt Rn, #:upper16:__stack_chk_guard
1175
1176 On ARMv5t, it is,
1177
1178 ldr Rn, .Label
1179 ....
1180 .Lable:
1181 .word __stack_chk_guard
1182
1183 Since ldr/str is a very popular instruction, we can't use them as
1184 'fingerprint' or 'signature' of stack protector sequence. Here we choose
1185 sequence {movw/movt, ldr}/ldr/str plus symbol __stack_chk_guard, if not
1186 stripped, as the 'fingerprint' of a stack protector cdoe sequence. */
1187
1188static CORE_ADDR
1189arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
1190{
1191 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
22e048c9 1192 unsigned int basereg;
7cbd4a93 1193 struct bound_minimal_symbol stack_chk_guard;
621c6d5b
YQ
1194 int offset;
1195 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1196 CORE_ADDR addr;
1197
1198 /* Try to parse the instructions in Step 1. */
1199 addr = arm_analyze_load_stack_chk_guard (pc, gdbarch,
1200 &basereg, &offset);
1201 if (!addr)
1202 return pc;
1203
1204 stack_chk_guard = lookup_minimal_symbol_by_pc (addr);
6041179a
JB
1205 /* ADDR must correspond to a symbol whose name is __stack_chk_guard.
1206 Otherwise, this sequence cannot be for stack protector. */
1207 if (stack_chk_guard.minsym == NULL
61012eef 1208 || !startswith (MSYMBOL_LINKAGE_NAME (stack_chk_guard.minsym), "__stack_chk_guard"))
621c6d5b
YQ
1209 return pc;
1210
1211 if (is_thumb)
1212 {
1213 unsigned int destreg;
1214 unsigned short insn
1215 = read_memory_unsigned_integer (pc + offset, 2, byte_order_for_code);
1216
1217 /* Step 2: ldr Rd, [Rn, #immed], encoding T1. */
1218 if ((insn & 0xf800) != 0x6800)
1219 return pc;
1220 if (bits (insn, 3, 5) != basereg)
1221 return pc;
1222 destreg = bits (insn, 0, 2);
1223
1224 insn = read_memory_unsigned_integer (pc + offset + 2, 2,
1225 byte_order_for_code);
1226 /* Step 3: str Rd, [Rn, #immed], encoding T1. */
1227 if ((insn & 0xf800) != 0x6000)
1228 return pc;
1229 if (destreg != bits (insn, 0, 2))
1230 return pc;
1231 }
1232 else
1233 {
1234 unsigned int destreg;
1235 unsigned int insn
1236 = read_memory_unsigned_integer (pc + offset, 4, byte_order_for_code);
1237
1238 /* Step 2: ldr Rd, [Rn, #immed], encoding A1. */
1239 if ((insn & 0x0e500000) != 0x04100000)
1240 return pc;
1241 if (bits (insn, 16, 19) != basereg)
1242 return pc;
1243 destreg = bits (insn, 12, 15);
1244 /* Step 3: str Rd, [Rn, #immed], encoding A1. */
1245 insn = read_memory_unsigned_integer (pc + offset + 4,
1246 4, byte_order_for_code);
1247 if ((insn & 0x0e500000) != 0x04000000)
1248 return pc;
1249 if (bits (insn, 12, 15) != destreg)
1250 return pc;
1251 }
1252 /* The size of total two instructions ldr/str is 4 on Thumb-2, while 8
1253 on arm. */
1254 if (is_thumb)
1255 return pc + offset + 4;
1256 else
1257 return pc + offset + 8;
1258}
1259
da3c6d4a
MS
1260/* Advance the PC across any function entry prologue instructions to
1261 reach some "real" code.
34e8f22d
RE
1262
1263 The APCS (ARM Procedure Call Standard) defines the following
ed9a39eb 1264 prologue:
c906108c 1265
c5aa993b
JM
1266 mov ip, sp
1267 [stmfd sp!, {a1,a2,a3,a4}]
1268 stmfd sp!, {...,fp,ip,lr,pc}
ed9a39eb
JM
1269 [stfe f7, [sp, #-12]!]
1270 [stfe f6, [sp, #-12]!]
1271 [stfe f5, [sp, #-12]!]
1272 [stfe f4, [sp, #-12]!]
0963b4bd 1273 sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn. */
c906108c 1274
34e8f22d 1275static CORE_ADDR
6093d2eb 1276arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
c906108c 1277{
a89fea3c 1278 CORE_ADDR func_addr, limit_pc;
c906108c 1279
a89fea3c
JL
1280 /* See if we can determine the end of the prologue via the symbol table.
1281 If so, then return either PC, or the PC after the prologue, whichever
1282 is greater. */
1283 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
c906108c 1284 {
d80b854b
UW
1285 CORE_ADDR post_prologue_pc
1286 = skip_prologue_using_sal (gdbarch, func_addr);
43f3e411 1287 struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);
0d39a070 1288
621c6d5b
YQ
1289 if (post_prologue_pc)
1290 post_prologue_pc
1291 = arm_skip_stack_protector (post_prologue_pc, gdbarch);
1292
1293
0d39a070
DJ
1294 /* GCC always emits a line note before the prologue and another
1295 one after, even if the two are at the same address or on the
1296 same line. Take advantage of this so that we do not need to
1297 know every instruction that might appear in the prologue. We
1298 will have producer information for most binaries; if it is
1299 missing (e.g. for -gstabs), assuming the GNU tools. */
1300 if (post_prologue_pc
43f3e411
DE
1301 && (cust == NULL
1302 || COMPUNIT_PRODUCER (cust) == NULL
61012eef
GB
1303 || startswith (COMPUNIT_PRODUCER (cust), "GNU ")
1304 || startswith (COMPUNIT_PRODUCER (cust), "clang ")))
0d39a070
DJ
1305 return post_prologue_pc;
1306
a89fea3c 1307 if (post_prologue_pc != 0)
0d39a070
DJ
1308 {
1309 CORE_ADDR analyzed_limit;
1310
1311 /* For non-GCC compilers, make sure the entire line is an
1312 acceptable prologue; GDB will round this function's
1313 return value up to the end of the following line so we
1314 can not skip just part of a line (and we do not want to).
1315
1316 RealView does not treat the prologue specially, but does
1317 associate prologue code with the opening brace; so this
1318 lets us skip the first line if we think it is the opening
1319 brace. */
9779414d 1320 if (arm_pc_is_thumb (gdbarch, func_addr))
0d39a070
DJ
1321 analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
1322 post_prologue_pc, NULL);
1323 else
1324 analyzed_limit = arm_analyze_prologue (gdbarch, func_addr,
1325 post_prologue_pc, NULL);
1326
1327 if (analyzed_limit != post_prologue_pc)
1328 return func_addr;
1329
1330 return post_prologue_pc;
1331 }
c906108c
SS
1332 }
1333
a89fea3c
JL
1334 /* Can't determine prologue from the symbol table, need to examine
1335 instructions. */
c906108c 1336
a89fea3c
JL
1337 /* Find an upper limit on the function prologue using the debug
1338 information. If the debug information could not be used to provide
1339 that bound, then use an arbitrary large number as the upper bound. */
0963b4bd 1340 /* Like arm_scan_prologue, stop no later than pc + 64. */
d80b854b 1341 limit_pc = skip_prologue_using_sal (gdbarch, pc);
a89fea3c
JL
1342 if (limit_pc == 0)
1343 limit_pc = pc + 64; /* Magic. */
1344
c906108c 1345
29d73ae4 1346 /* Check if this is Thumb code. */
9779414d 1347 if (arm_pc_is_thumb (gdbarch, pc))
a89fea3c 1348 return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
21daaaaf
YQ
1349 else
1350 return arm_analyze_prologue (gdbarch, pc, limit_pc, NULL);
c906108c 1351}
94c30b78 1352
c5aa993b 1353/* *INDENT-OFF* */
c906108c
SS
1354/* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
1355 This function decodes a Thumb function prologue to determine:
1356 1) the size of the stack frame
1357 2) which registers are saved on it
1358 3) the offsets of saved regs
1359 4) the offset from the stack pointer to the frame pointer
c906108c 1360
da59e081
JM
1361 A typical Thumb function prologue would create this stack frame
1362 (offsets relative to FP)
c906108c
SS
1363 old SP -> 24 stack parameters
1364 20 LR
1365 16 R7
1366 R7 -> 0 local variables (16 bytes)
1367 SP -> -12 additional stack space (12 bytes)
1368 The frame size would thus be 36 bytes, and the frame offset would be
0963b4bd 1369 12 bytes. The frame register is R7.
da59e081 1370
da3c6d4a
MS
1371 The comments for thumb_skip_prolog() describe the algorithm we use
1372 to detect the end of the prolog. */
c5aa993b
JM
1373/* *INDENT-ON* */
1374
c906108c 1375static void
be8626e0 1376thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
b39cc962 1377 CORE_ADDR block_addr, struct arm_prologue_cache *cache)
c906108c
SS
1378{
1379 CORE_ADDR prologue_start;
1380 CORE_ADDR prologue_end;
c906108c 1381
b39cc962
DJ
1382 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1383 &prologue_end))
c906108c 1384 {
ec3d575a
UW
1385 /* See comment in arm_scan_prologue for an explanation of
1386 this heuristics. */
1387 if (prologue_end > prologue_start + 64)
1388 {
1389 prologue_end = prologue_start + 64;
1390 }
c906108c
SS
1391 }
1392 else
f7060f85
DJ
1393 /* We're in the boondocks: we have no idea where the start of the
1394 function is. */
1395 return;
c906108c 1396
eb5492fa 1397 prologue_end = min (prologue_end, prev_pc);
c906108c 1398
be8626e0 1399 thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
1400}
1401
f303bc3e
YQ
1402/* Return 1 if the ARM instruction INSN restores SP in epilogue, 0
1403 otherwise. */
1404
1405static int
1406arm_instruction_restores_sp (unsigned int insn)
1407{
1408 if (bits (insn, 28, 31) != INST_NV)
1409 {
1410 if ((insn & 0x0df0f000) == 0x0080d000
1411 /* ADD SP (register or immediate). */
1412 || (insn & 0x0df0f000) == 0x0040d000
1413 /* SUB SP (register or immediate). */
1414 || (insn & 0x0ffffff0) == 0x01a0d000
1415 /* MOV SP. */
1416 || (insn & 0x0fff0000) == 0x08bd0000
1417 /* POP (LDMIA). */
1418 || (insn & 0x0fff0000) == 0x049d0000)
1419 /* POP of a single register. */
1420 return 1;
1421 }
1422
1423 return 0;
1424}
1425
0d39a070
DJ
1426/* Analyze an ARM mode prologue starting at PROLOGUE_START and
1427 continuing no further than PROLOGUE_END. If CACHE is non-NULL,
1428 fill it in. Return the first address not recognized as a prologue
1429 instruction.
eb5492fa 1430
0d39a070
DJ
1431 We recognize all the instructions typically found in ARM prologues,
1432 plus harmless instructions which can be skipped (either for analysis
1433 purposes, or a more restrictive set that can be skipped when finding
1434 the end of the prologue). */
1435
1436static CORE_ADDR
1437arm_analyze_prologue (struct gdbarch *gdbarch,
1438 CORE_ADDR prologue_start, CORE_ADDR prologue_end,
1439 struct arm_prologue_cache *cache)
1440{
0d39a070
DJ
1441 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1442 int regno;
1443 CORE_ADDR offset, current_pc;
1444 pv_t regs[ARM_FPS_REGNUM];
1445 struct pv_area *stack;
1446 struct cleanup *back_to;
0d39a070
DJ
1447 CORE_ADDR unrecognized_pc = 0;
1448
1449 /* Search the prologue looking for instructions that set up the
96baa820 1450 frame pointer, adjust the stack pointer, and save registers.
ed9a39eb 1451
96baa820
JM
1452 Be careful, however, and if it doesn't look like a prologue,
1453 don't try to scan it. If, for instance, a frameless function
1454 begins with stmfd sp!, then we will tell ourselves there is
b8d5e71d 1455 a frame, which will confuse stack traceback, as well as "finish"
96baa820 1456 and other operations that rely on a knowledge of the stack
0d39a070 1457 traceback. */
d4473757 1458
4be43953
DJ
1459 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1460 regs[regno] = pv_register (regno, 0);
55f960e1 1461 stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
4be43953
DJ
1462 back_to = make_cleanup_free_pv_area (stack);
1463
94c30b78
MS
1464 for (current_pc = prologue_start;
1465 current_pc < prologue_end;
f43845b3 1466 current_pc += 4)
96baa820 1467 {
e17a4113
UW
1468 unsigned int insn
1469 = read_memory_unsigned_integer (current_pc, 4, byte_order_for_code);
9d4fde75 1470
94c30b78 1471 if (insn == 0xe1a0c00d) /* mov ip, sp */
f43845b3 1472 {
4be43953 1473 regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
28cd8767
JG
1474 continue;
1475 }
0d39a070
DJ
1476 else if ((insn & 0xfff00000) == 0xe2800000 /* add Rd, Rn, #n */
1477 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767
JG
1478 {
1479 unsigned imm = insn & 0xff; /* immediate value */
1480 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
0d39a070 1481 int rd = bits (insn, 12, 15);
28cd8767 1482 imm = (imm >> rot) | (imm << (32 - rot));
0d39a070 1483 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
28cd8767
JG
1484 continue;
1485 }
0d39a070
DJ
1486 else if ((insn & 0xfff00000) == 0xe2400000 /* sub Rd, Rn, #n */
1487 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767
JG
1488 {
1489 unsigned imm = insn & 0xff; /* immediate value */
1490 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
0d39a070 1491 int rd = bits (insn, 12, 15);
28cd8767 1492 imm = (imm >> rot) | (imm << (32 - rot));
0d39a070 1493 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
f43845b3
MS
1494 continue;
1495 }
0963b4bd
MS
1496 else if ((insn & 0xffff0fff) == 0xe52d0004) /* str Rd,
1497 [sp, #-4]! */
f43845b3 1498 {
4be43953
DJ
1499 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1500 break;
1501 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
0d39a070
DJ
1502 pv_area_store (stack, regs[ARM_SP_REGNUM], 4,
1503 regs[bits (insn, 12, 15)]);
f43845b3
MS
1504 continue;
1505 }
1506 else if ((insn & 0xffff0000) == 0xe92d0000)
d4473757
KB
1507 /* stmfd sp!, {..., fp, ip, lr, pc}
1508 or
1509 stmfd sp!, {a1, a2, a3, a4} */
c906108c 1510 {
d4473757 1511 int mask = insn & 0xffff;
ed9a39eb 1512
4be43953
DJ
1513 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1514 break;
1515
94c30b78 1516 /* Calculate offsets of saved registers. */
34e8f22d 1517 for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
d4473757
KB
1518 if (mask & (1 << regno))
1519 {
0963b4bd
MS
1520 regs[ARM_SP_REGNUM]
1521 = pv_add_constant (regs[ARM_SP_REGNUM], -4);
4be43953 1522 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
d4473757
KB
1523 }
1524 }
0d39a070
DJ
1525 else if ((insn & 0xffff0000) == 0xe54b0000 /* strb rx,[r11,#-n] */
1526 || (insn & 0xffff00f0) == 0xe14b00b0 /* strh rx,[r11,#-n] */
f8bf5763 1527 || (insn & 0xffffc000) == 0xe50b0000) /* str rx,[r11,#-n] */
b8d5e71d
MS
1528 {
1529 /* No need to add this to saved_regs -- it's just an arg reg. */
1530 continue;
1531 }
0d39a070
DJ
1532 else if ((insn & 0xffff0000) == 0xe5cd0000 /* strb rx,[sp,#n] */
1533 || (insn & 0xffff00f0) == 0xe1cd00b0 /* strh rx,[sp,#n] */
f8bf5763 1534 || (insn & 0xffffc000) == 0xe58d0000) /* str rx,[sp,#n] */
f43845b3
MS
1535 {
1536 /* No need to add this to saved_regs -- it's just an arg reg. */
1537 continue;
1538 }
0963b4bd
MS
1539 else if ((insn & 0xfff00000) == 0xe8800000 /* stm Rn,
1540 { registers } */
0d39a070
DJ
1541 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1542 {
1543 /* No need to add this to saved_regs -- it's just arg regs. */
1544 continue;
1545 }
d4473757
KB
1546 else if ((insn & 0xfffff000) == 0xe24cb000) /* sub fp, ip #n */
1547 {
94c30b78
MS
1548 unsigned imm = insn & 0xff; /* immediate value */
1549 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
d4473757 1550 imm = (imm >> rot) | (imm << (32 - rot));
4be43953 1551 regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
d4473757
KB
1552 }
1553 else if ((insn & 0xfffff000) == 0xe24dd000) /* sub sp, sp #n */
1554 {
94c30b78
MS
1555 unsigned imm = insn & 0xff; /* immediate value */
1556 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
d4473757 1557 imm = (imm >> rot) | (imm << (32 - rot));
4be43953 1558 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
d4473757 1559 }
0963b4bd
MS
1560 else if ((insn & 0xffff7fff) == 0xed6d0103 /* stfe f?,
1561 [sp, -#c]! */
2af46ca0 1562 && gdbarch_tdep (gdbarch)->have_fpa_registers)
d4473757 1563 {
4be43953
DJ
1564 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1565 break;
1566
1567 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
34e8f22d 1568 regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
4be43953 1569 pv_area_store (stack, regs[ARM_SP_REGNUM], 12, regs[regno]);
d4473757 1570 }
0963b4bd
MS
1571 else if ((insn & 0xffbf0fff) == 0xec2d0200 /* sfmfd f0, 4,
1572 [sp!] */
2af46ca0 1573 && gdbarch_tdep (gdbarch)->have_fpa_registers)
d4473757
KB
1574 {
1575 int n_saved_fp_regs;
1576 unsigned int fp_start_reg, fp_bound_reg;
1577
4be43953
DJ
1578 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1579 break;
1580
94c30b78 1581 if ((insn & 0x800) == 0x800) /* N0 is set */
96baa820 1582 {
d4473757
KB
1583 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1584 n_saved_fp_regs = 3;
1585 else
1586 n_saved_fp_regs = 1;
96baa820 1587 }
d4473757 1588 else
96baa820 1589 {
d4473757
KB
1590 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1591 n_saved_fp_regs = 2;
1592 else
1593 n_saved_fp_regs = 4;
96baa820 1594 }
d4473757 1595
34e8f22d 1596 fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
d4473757
KB
1597 fp_bound_reg = fp_start_reg + n_saved_fp_regs;
1598 for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
96baa820 1599 {
4be43953
DJ
1600 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
1601 pv_area_store (stack, regs[ARM_SP_REGNUM], 12,
1602 regs[fp_start_reg++]);
96baa820 1603 }
c906108c 1604 }
0d39a070
DJ
1605 else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
1606 {
1607 /* Allow some special function calls when skipping the
1608 prologue; GCC generates these before storing arguments to
1609 the stack. */
1610 CORE_ADDR dest = BranchDest (current_pc, insn);
1611
e0634ccf 1612 if (skip_prologue_function (gdbarch, dest, 0))
0d39a070
DJ
1613 continue;
1614 else
1615 break;
1616 }
d4473757 1617 else if ((insn & 0xf0000000) != 0xe0000000)
0963b4bd 1618 break; /* Condition not true, exit early. */
0d39a070
DJ
1619 else if (arm_instruction_changes_pc (insn))
1620 /* Don't scan past anything that might change control flow. */
1621 break;
f303bc3e
YQ
1622 else if (arm_instruction_restores_sp (insn))
1623 {
1624 /* Don't scan past the epilogue. */
1625 break;
1626 }
d19f7eee
UW
1627 else if ((insn & 0xfe500000) == 0xe8100000 /* ldm */
1628 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1629 /* Ignore block loads from the stack, potentially copying
1630 parameters from memory. */
1631 continue;
1632 else if ((insn & 0xfc500000) == 0xe4100000
1633 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1634 /* Similarly ignore single loads from the stack. */
1635 continue;
0d39a070
DJ
1636 else if ((insn & 0xffff0ff0) == 0xe1a00000)
1637 /* MOV Rd, Rm. Skip register copies, i.e. saves to another
1638 register instead of the stack. */
d4473757 1639 continue;
0d39a070
DJ
1640 else
1641 {
21daaaaf
YQ
1642 /* The optimizer might shove anything into the prologue, if
1643 we build up cache (cache != NULL) from scanning prologue,
1644 we just skip what we don't recognize and scan further to
1645 make cache as complete as possible. However, if we skip
1646 prologue, we'll stop immediately on unrecognized
1647 instruction. */
0d39a070 1648 unrecognized_pc = current_pc;
21daaaaf
YQ
1649 if (cache != NULL)
1650 continue;
1651 else
1652 break;
0d39a070 1653 }
c906108c
SS
1654 }
1655
0d39a070
DJ
1656 if (unrecognized_pc == 0)
1657 unrecognized_pc = current_pc;
1658
0d39a070
DJ
1659 if (cache)
1660 {
4072f920
YQ
1661 int framereg, framesize;
1662
1663 /* The frame size is just the distance from the frame register
1664 to the original stack pointer. */
1665 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1666 {
1667 /* Frame pointer is fp. */
1668 framereg = ARM_FP_REGNUM;
1669 framesize = -regs[ARM_FP_REGNUM].k;
1670 }
1671 else
1672 {
1673 /* Try the stack pointer... this is a bit desperate. */
1674 framereg = ARM_SP_REGNUM;
1675 framesize = -regs[ARM_SP_REGNUM].k;
1676 }
1677
0d39a070
DJ
1678 cache->framereg = framereg;
1679 cache->framesize = framesize;
1680
1681 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1682 if (pv_area_find_reg (stack, gdbarch, regno, &offset))
1683 cache->saved_regs[regno].addr = offset;
1684 }
1685
1686 if (arm_debug)
1687 fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1688 paddress (gdbarch, unrecognized_pc));
4be43953
DJ
1689
1690 do_cleanups (back_to);
0d39a070
DJ
1691 return unrecognized_pc;
1692}
1693
1694static void
1695arm_scan_prologue (struct frame_info *this_frame,
1696 struct arm_prologue_cache *cache)
1697{
1698 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1699 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
bec2ab5a 1700 CORE_ADDR prologue_start, prologue_end;
0d39a070
DJ
1701 CORE_ADDR prev_pc = get_frame_pc (this_frame);
1702 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
0d39a070
DJ
1703
1704 /* Assume there is no frame until proven otherwise. */
1705 cache->framereg = ARM_SP_REGNUM;
1706 cache->framesize = 0;
1707
1708 /* Check for Thumb prologue. */
1709 if (arm_frame_is_thumb (this_frame))
1710 {
1711 thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
1712 return;
1713 }
1714
1715 /* Find the function prologue. If we can't find the function in
1716 the symbol table, peek in the stack frame to find the PC. */
1717 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1718 &prologue_end))
1719 {
1720 /* One way to find the end of the prologue (which works well
1721 for unoptimized code) is to do the following:
1722
1723 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
1724
1725 if (sal.line == 0)
1726 prologue_end = prev_pc;
1727 else if (sal.end < prologue_end)
1728 prologue_end = sal.end;
1729
1730 This mechanism is very accurate so long as the optimizer
1731 doesn't move any instructions from the function body into the
1732 prologue. If this happens, sal.end will be the last
1733 instruction in the first hunk of prologue code just before
1734 the first instruction that the scheduler has moved from
1735 the body to the prologue.
1736
1737 In order to make sure that we scan all of the prologue
1738 instructions, we use a slightly less accurate mechanism which
1739 may scan more than necessary. To help compensate for this
1740 lack of accuracy, the prologue scanning loop below contains
1741 several clauses which'll cause the loop to terminate early if
1742 an implausible prologue instruction is encountered.
1743
1744 The expression
1745
1746 prologue_start + 64
1747
1748 is a suitable endpoint since it accounts for the largest
1749 possible prologue plus up to five instructions inserted by
1750 the scheduler. */
1751
1752 if (prologue_end > prologue_start + 64)
1753 {
1754 prologue_end = prologue_start + 64; /* See above. */
1755 }
1756 }
1757 else
1758 {
1759 /* We have no symbol information. Our only option is to assume this
1760 function has a standard stack frame and the normal frame register.
1761 Then, we can find the value of our frame pointer on entrance to
1762 the callee (or at the present moment if this is the innermost frame).
1763 The value stored there should be the address of the stmfd + 8. */
1764 CORE_ADDR frame_loc;
1765 LONGEST return_value;
1766
1767 frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
1768 if (!safe_read_memory_integer (frame_loc, 4, byte_order, &return_value))
1769 return;
1770 else
1771 {
1772 prologue_start = gdbarch_addr_bits_remove
1773 (gdbarch, return_value) - 8;
1774 prologue_end = prologue_start + 64; /* See above. */
1775 }
1776 }
1777
1778 if (prev_pc < prologue_end)
1779 prologue_end = prev_pc;
1780
1781 arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
1782}
1783
eb5492fa 1784static struct arm_prologue_cache *
a262aec2 1785arm_make_prologue_cache (struct frame_info *this_frame)
c906108c 1786{
eb5492fa
DJ
1787 int reg;
1788 struct arm_prologue_cache *cache;
1789 CORE_ADDR unwound_fp;
c5aa993b 1790
35d5d4ee 1791 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
a262aec2 1792 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
c906108c 1793
a262aec2 1794 arm_scan_prologue (this_frame, cache);
848cfffb 1795
a262aec2 1796 unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
eb5492fa
DJ
1797 if (unwound_fp == 0)
1798 return cache;
c906108c 1799
4be43953 1800 cache->prev_sp = unwound_fp + cache->framesize;
c906108c 1801
eb5492fa
DJ
1802 /* Calculate actual addresses of saved registers using offsets
1803 determined by arm_scan_prologue. */
a262aec2 1804 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
e28a332c 1805 if (trad_frame_addr_p (cache->saved_regs, reg))
eb5492fa
DJ
1806 cache->saved_regs[reg].addr += cache->prev_sp;
1807
1808 return cache;
c906108c
SS
1809}
1810
c1ee9414
LM
1811/* Implementation of the stop_reason hook for arm_prologue frames. */
1812
1813static enum unwind_stop_reason
1814arm_prologue_unwind_stop_reason (struct frame_info *this_frame,
1815 void **this_cache)
1816{
1817 struct arm_prologue_cache *cache;
1818 CORE_ADDR pc;
1819
1820 if (*this_cache == NULL)
1821 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 1822 cache = (struct arm_prologue_cache *) *this_cache;
c1ee9414
LM
1823
1824 /* This is meant to halt the backtrace at "_start". */
1825 pc = get_frame_pc (this_frame);
1826 if (pc <= gdbarch_tdep (get_frame_arch (this_frame))->lowest_pc)
1827 return UNWIND_OUTERMOST;
1828
1829 /* If we've hit a wall, stop. */
1830 if (cache->prev_sp == 0)
1831 return UNWIND_OUTERMOST;
1832
1833 return UNWIND_NO_REASON;
1834}
1835
eb5492fa
DJ
1836/* Our frame ID for a normal frame is the current function's starting PC
1837 and the caller's SP when we were called. */
c906108c 1838
148754e5 1839static void
a262aec2 1840arm_prologue_this_id (struct frame_info *this_frame,
eb5492fa
DJ
1841 void **this_cache,
1842 struct frame_id *this_id)
c906108c 1843{
eb5492fa
DJ
1844 struct arm_prologue_cache *cache;
1845 struct frame_id id;
2c404490 1846 CORE_ADDR pc, func;
f079148d 1847
eb5492fa 1848 if (*this_cache == NULL)
a262aec2 1849 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 1850 cache = (struct arm_prologue_cache *) *this_cache;
2a451106 1851
0e9e9abd
UW
1852 /* Use function start address as part of the frame ID. If we cannot
1853 identify the start address (due to missing symbol information),
1854 fall back to just using the current PC. */
c1ee9414 1855 pc = get_frame_pc (this_frame);
2c404490 1856 func = get_frame_func (this_frame);
0e9e9abd
UW
1857 if (!func)
1858 func = pc;
1859
eb5492fa 1860 id = frame_id_build (cache->prev_sp, func);
eb5492fa 1861 *this_id = id;
c906108c
SS
1862}
1863
a262aec2
DJ
1864static struct value *
1865arm_prologue_prev_register (struct frame_info *this_frame,
eb5492fa 1866 void **this_cache,
a262aec2 1867 int prev_regnum)
24de872b 1868{
24568a2c 1869 struct gdbarch *gdbarch = get_frame_arch (this_frame);
24de872b
DJ
1870 struct arm_prologue_cache *cache;
1871
eb5492fa 1872 if (*this_cache == NULL)
a262aec2 1873 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 1874 cache = (struct arm_prologue_cache *) *this_cache;
24de872b 1875
eb5492fa 1876 /* If we are asked to unwind the PC, then we need to return the LR
b39cc962
DJ
1877 instead. The prologue may save PC, but it will point into this
1878 frame's prologue, not the next frame's resume location. Also
1879 strip the saved T bit. A valid LR may have the low bit set, but
1880 a valid PC never does. */
eb5492fa 1881 if (prev_regnum == ARM_PC_REGNUM)
b39cc962
DJ
1882 {
1883 CORE_ADDR lr;
1884
1885 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
1886 return frame_unwind_got_constant (this_frame, prev_regnum,
24568a2c 1887 arm_addr_bits_remove (gdbarch, lr));
b39cc962 1888 }
24de872b 1889
eb5492fa 1890 /* SP is generally not saved to the stack, but this frame is
a262aec2 1891 identified by the next frame's stack pointer at the time of the call.
eb5492fa
DJ
1892 The value was already reconstructed into PREV_SP. */
1893 if (prev_regnum == ARM_SP_REGNUM)
a262aec2 1894 return frame_unwind_got_constant (this_frame, prev_regnum, cache->prev_sp);
eb5492fa 1895
b39cc962
DJ
1896 /* The CPSR may have been changed by the call instruction and by the
1897 called function. The only bit we can reconstruct is the T bit,
1898 by checking the low bit of LR as of the call. This is a reliable
1899 indicator of Thumb-ness except for some ARM v4T pre-interworking
1900 Thumb code, which could get away with a clear low bit as long as
1901 the called function did not use bx. Guess that all other
1902 bits are unchanged; the condition flags are presumably lost,
1903 but the processor status is likely valid. */
1904 if (prev_regnum == ARM_PS_REGNUM)
1905 {
1906 CORE_ADDR lr, cpsr;
9779414d 1907 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
b39cc962
DJ
1908
1909 cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
1910 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
1911 if (IS_THUMB_ADDR (lr))
9779414d 1912 cpsr |= t_bit;
b39cc962 1913 else
9779414d 1914 cpsr &= ~t_bit;
b39cc962
DJ
1915 return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
1916 }
1917
a262aec2
DJ
1918 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
1919 prev_regnum);
eb5492fa
DJ
1920}
1921
1922struct frame_unwind arm_prologue_unwind = {
1923 NORMAL_FRAME,
c1ee9414 1924 arm_prologue_unwind_stop_reason,
eb5492fa 1925 arm_prologue_this_id,
a262aec2
DJ
1926 arm_prologue_prev_register,
1927 NULL,
1928 default_frame_sniffer
eb5492fa
DJ
1929};
1930
0e9e9abd
UW
1931/* Maintain a list of ARM exception table entries per objfile, similar to the
1932 list of mapping symbols. We only cache entries for standard ARM-defined
1933 personality routines; the cache will contain only the frame unwinding
1934 instructions associated with the entry (not the descriptors). */
1935
1936static const struct objfile_data *arm_exidx_data_key;
1937
1938struct arm_exidx_entry
1939{
1940 bfd_vma addr;
1941 gdb_byte *entry;
1942};
1943typedef struct arm_exidx_entry arm_exidx_entry_s;
1944DEF_VEC_O(arm_exidx_entry_s);
1945
1946struct arm_exidx_data
1947{
1948 VEC(arm_exidx_entry_s) **section_maps;
1949};
1950
1951static void
1952arm_exidx_data_free (struct objfile *objfile, void *arg)
1953{
9a3c8263 1954 struct arm_exidx_data *data = (struct arm_exidx_data *) arg;
0e9e9abd
UW
1955 unsigned int i;
1956
1957 for (i = 0; i < objfile->obfd->section_count; i++)
1958 VEC_free (arm_exidx_entry_s, data->section_maps[i]);
1959}
1960
1961static inline int
1962arm_compare_exidx_entries (const struct arm_exidx_entry *lhs,
1963 const struct arm_exidx_entry *rhs)
1964{
1965 return lhs->addr < rhs->addr;
1966}
1967
1968static struct obj_section *
1969arm_obj_section_from_vma (struct objfile *objfile, bfd_vma vma)
1970{
1971 struct obj_section *osect;
1972
1973 ALL_OBJFILE_OSECTIONS (objfile, osect)
1974 if (bfd_get_section_flags (objfile->obfd,
1975 osect->the_bfd_section) & SEC_ALLOC)
1976 {
1977 bfd_vma start, size;
1978 start = bfd_get_section_vma (objfile->obfd, osect->the_bfd_section);
1979 size = bfd_get_section_size (osect->the_bfd_section);
1980
1981 if (start <= vma && vma < start + size)
1982 return osect;
1983 }
1984
1985 return NULL;
1986}
1987
1988/* Parse contents of exception table and exception index sections
1989 of OBJFILE, and fill in the exception table entry cache.
1990
1991 For each entry that refers to a standard ARM-defined personality
1992 routine, extract the frame unwinding instructions (from either
1993 the index or the table section). The unwinding instructions
1994 are normalized by:
1995 - extracting them from the rest of the table data
1996 - converting to host endianness
1997 - appending the implicit 0xb0 ("Finish") code
1998
1999 The extracted and normalized instructions are stored for later
2000 retrieval by the arm_find_exidx_entry routine. */
2001
2002static void
2003arm_exidx_new_objfile (struct objfile *objfile)
2004{
3bb47e8b 2005 struct cleanup *cleanups;
0e9e9abd
UW
2006 struct arm_exidx_data *data;
2007 asection *exidx, *extab;
2008 bfd_vma exidx_vma = 0, extab_vma = 0;
2009 bfd_size_type exidx_size = 0, extab_size = 0;
2010 gdb_byte *exidx_data = NULL, *extab_data = NULL;
2011 LONGEST i;
2012
2013 /* If we've already touched this file, do nothing. */
2014 if (!objfile || objfile_data (objfile, arm_exidx_data_key) != NULL)
2015 return;
3bb47e8b 2016 cleanups = make_cleanup (null_cleanup, NULL);
0e9e9abd
UW
2017
2018 /* Read contents of exception table and index. */
a5eda10c 2019 exidx = bfd_get_section_by_name (objfile->obfd, ELF_STRING_ARM_unwind);
0e9e9abd
UW
2020 if (exidx)
2021 {
2022 exidx_vma = bfd_section_vma (objfile->obfd, exidx);
2023 exidx_size = bfd_get_section_size (exidx);
224c3ddb 2024 exidx_data = (gdb_byte *) xmalloc (exidx_size);
0e9e9abd
UW
2025 make_cleanup (xfree, exidx_data);
2026
2027 if (!bfd_get_section_contents (objfile->obfd, exidx,
2028 exidx_data, 0, exidx_size))
2029 {
2030 do_cleanups (cleanups);
2031 return;
2032 }
2033 }
2034
2035 extab = bfd_get_section_by_name (objfile->obfd, ".ARM.extab");
2036 if (extab)
2037 {
2038 extab_vma = bfd_section_vma (objfile->obfd, extab);
2039 extab_size = bfd_get_section_size (extab);
224c3ddb 2040 extab_data = (gdb_byte *) xmalloc (extab_size);
0e9e9abd
UW
2041 make_cleanup (xfree, extab_data);
2042
2043 if (!bfd_get_section_contents (objfile->obfd, extab,
2044 extab_data, 0, extab_size))
2045 {
2046 do_cleanups (cleanups);
2047 return;
2048 }
2049 }
2050
2051 /* Allocate exception table data structure. */
2052 data = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct arm_exidx_data);
2053 set_objfile_data (objfile, arm_exidx_data_key, data);
2054 data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
2055 objfile->obfd->section_count,
2056 VEC(arm_exidx_entry_s) *);
2057
2058 /* Fill in exception table. */
2059 for (i = 0; i < exidx_size / 8; i++)
2060 {
2061 struct arm_exidx_entry new_exidx_entry;
2062 bfd_vma idx = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8);
2063 bfd_vma val = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8 + 4);
2064 bfd_vma addr = 0, word = 0;
2065 int n_bytes = 0, n_words = 0;
2066 struct obj_section *sec;
2067 gdb_byte *entry = NULL;
2068
2069 /* Extract address of start of function. */
2070 idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2071 idx += exidx_vma + i * 8;
2072
2073 /* Find section containing function and compute section offset. */
2074 sec = arm_obj_section_from_vma (objfile, idx);
2075 if (sec == NULL)
2076 continue;
2077 idx -= bfd_get_section_vma (objfile->obfd, sec->the_bfd_section);
2078
2079 /* Determine address of exception table entry. */
2080 if (val == 1)
2081 {
2082 /* EXIDX_CANTUNWIND -- no exception table entry present. */
2083 }
2084 else if ((val & 0xff000000) == 0x80000000)
2085 {
2086 /* Exception table entry embedded in .ARM.exidx
2087 -- must be short form. */
2088 word = val;
2089 n_bytes = 3;
2090 }
2091 else if (!(val & 0x80000000))
2092 {
2093 /* Exception table entry in .ARM.extab. */
2094 addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2095 addr += exidx_vma + i * 8 + 4;
2096
2097 if (addr >= extab_vma && addr + 4 <= extab_vma + extab_size)
2098 {
2099 word = bfd_h_get_32 (objfile->obfd,
2100 extab_data + addr - extab_vma);
2101 addr += 4;
2102
2103 if ((word & 0xff000000) == 0x80000000)
2104 {
2105 /* Short form. */
2106 n_bytes = 3;
2107 }
2108 else if ((word & 0xff000000) == 0x81000000
2109 || (word & 0xff000000) == 0x82000000)
2110 {
2111 /* Long form. */
2112 n_bytes = 2;
2113 n_words = ((word >> 16) & 0xff);
2114 }
2115 else if (!(word & 0x80000000))
2116 {
2117 bfd_vma pers;
2118 struct obj_section *pers_sec;
2119 int gnu_personality = 0;
2120
2121 /* Custom personality routine. */
2122 pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2123 pers = UNMAKE_THUMB_ADDR (pers + addr - 4);
2124
2125 /* Check whether we've got one of the variants of the
2126 GNU personality routines. */
2127 pers_sec = arm_obj_section_from_vma (objfile, pers);
2128 if (pers_sec)
2129 {
2130 static const char *personality[] =
2131 {
2132 "__gcc_personality_v0",
2133 "__gxx_personality_v0",
2134 "__gcj_personality_v0",
2135 "__gnu_objc_personality_v0",
2136 NULL
2137 };
2138
2139 CORE_ADDR pc = pers + obj_section_offset (pers_sec);
2140 int k;
2141
2142 for (k = 0; personality[k]; k++)
2143 if (lookup_minimal_symbol_by_pc_name
2144 (pc, personality[k], objfile))
2145 {
2146 gnu_personality = 1;
2147 break;
2148 }
2149 }
2150
2151 /* If so, the next word contains a word count in the high
2152 byte, followed by the same unwind instructions as the
2153 pre-defined forms. */
2154 if (gnu_personality
2155 && addr + 4 <= extab_vma + extab_size)
2156 {
2157 word = bfd_h_get_32 (objfile->obfd,
2158 extab_data + addr - extab_vma);
2159 addr += 4;
2160 n_bytes = 3;
2161 n_words = ((word >> 24) & 0xff);
2162 }
2163 }
2164 }
2165 }
2166
2167 /* Sanity check address. */
2168 if (n_words)
2169 if (addr < extab_vma || addr + 4 * n_words > extab_vma + extab_size)
2170 n_words = n_bytes = 0;
2171
2172 /* The unwind instructions reside in WORD (only the N_BYTES least
2173 significant bytes are valid), followed by N_WORDS words in the
2174 extab section starting at ADDR. */
2175 if (n_bytes || n_words)
2176 {
224c3ddb
SM
2177 gdb_byte *p = entry
2178 = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack,
2179 n_bytes + n_words * 4 + 1);
0e9e9abd
UW
2180
2181 while (n_bytes--)
2182 *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);
2183
2184 while (n_words--)
2185 {
2186 word = bfd_h_get_32 (objfile->obfd,
2187 extab_data + addr - extab_vma);
2188 addr += 4;
2189
2190 *p++ = (gdb_byte) ((word >> 24) & 0xff);
2191 *p++ = (gdb_byte) ((word >> 16) & 0xff);
2192 *p++ = (gdb_byte) ((word >> 8) & 0xff);
2193 *p++ = (gdb_byte) (word & 0xff);
2194 }
2195
2196 /* Implied "Finish" to terminate the list. */
2197 *p++ = 0xb0;
2198 }
2199
2200 /* Push entry onto vector. They are guaranteed to always
2201 appear in order of increasing addresses. */
2202 new_exidx_entry.addr = idx;
2203 new_exidx_entry.entry = entry;
2204 VEC_safe_push (arm_exidx_entry_s,
2205 data->section_maps[sec->the_bfd_section->index],
2206 &new_exidx_entry);
2207 }
2208
2209 do_cleanups (cleanups);
2210}
2211
2212/* Search for the exception table entry covering MEMADDR. If one is found,
2213 return a pointer to its data. Otherwise, return 0. If START is non-NULL,
2214 set *START to the start of the region covered by this entry. */
2215
2216static gdb_byte *
2217arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
2218{
2219 struct obj_section *sec;
2220
2221 sec = find_pc_section (memaddr);
2222 if (sec != NULL)
2223 {
2224 struct arm_exidx_data *data;
2225 VEC(arm_exidx_entry_s) *map;
2226 struct arm_exidx_entry map_key = { memaddr - obj_section_addr (sec), 0 };
2227 unsigned int idx;
2228
9a3c8263
SM
2229 data = ((struct arm_exidx_data *)
2230 objfile_data (sec->objfile, arm_exidx_data_key));
0e9e9abd
UW
2231 if (data != NULL)
2232 {
2233 map = data->section_maps[sec->the_bfd_section->index];
2234 if (!VEC_empty (arm_exidx_entry_s, map))
2235 {
2236 struct arm_exidx_entry *map_sym;
2237
2238 idx = VEC_lower_bound (arm_exidx_entry_s, map, &map_key,
2239 arm_compare_exidx_entries);
2240
2241 /* VEC_lower_bound finds the earliest ordered insertion
2242 point. If the following symbol starts at this exact
2243 address, we use that; otherwise, the preceding
2244 exception table entry covers this address. */
2245 if (idx < VEC_length (arm_exidx_entry_s, map))
2246 {
2247 map_sym = VEC_index (arm_exidx_entry_s, map, idx);
2248 if (map_sym->addr == map_key.addr)
2249 {
2250 if (start)
2251 *start = map_sym->addr + obj_section_addr (sec);
2252 return map_sym->entry;
2253 }
2254 }
2255
2256 if (idx > 0)
2257 {
2258 map_sym = VEC_index (arm_exidx_entry_s, map, idx - 1);
2259 if (start)
2260 *start = map_sym->addr + obj_section_addr (sec);
2261 return map_sym->entry;
2262 }
2263 }
2264 }
2265 }
2266
2267 return NULL;
2268}
2269
2270/* Given the current frame THIS_FRAME, and its associated frame unwinding
2271 instruction list from the ARM exception table entry ENTRY, allocate and
2272 return a prologue cache structure describing how to unwind this frame.
2273
2274 Return NULL if the unwinding instruction list contains a "spare",
2275 "reserved" or "refuse to unwind" instruction as defined in section
2276 "9.3 Frame unwinding instructions" of the "Exception Handling ABI
2277 for the ARM Architecture" document. */
2278
2279static struct arm_prologue_cache *
2280arm_exidx_fill_cache (struct frame_info *this_frame, gdb_byte *entry)
2281{
2282 CORE_ADDR vsp = 0;
2283 int vsp_valid = 0;
2284
2285 struct arm_prologue_cache *cache;
2286 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2287 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2288
2289 for (;;)
2290 {
2291 gdb_byte insn;
2292
2293 /* Whenever we reload SP, we actually have to retrieve its
2294 actual value in the current frame. */
2295 if (!vsp_valid)
2296 {
2297 if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2298 {
2299 int reg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2300 vsp = get_frame_register_unsigned (this_frame, reg);
2301 }
2302 else
2303 {
2304 CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr;
2305 vsp = get_frame_memory_unsigned (this_frame, addr, 4);
2306 }
2307
2308 vsp_valid = 1;
2309 }
2310
2311 /* Decode next unwind instruction. */
2312 insn = *entry++;
2313
2314 if ((insn & 0xc0) == 0)
2315 {
2316 int offset = insn & 0x3f;
2317 vsp += (offset << 2) + 4;
2318 }
2319 else if ((insn & 0xc0) == 0x40)
2320 {
2321 int offset = insn & 0x3f;
2322 vsp -= (offset << 2) + 4;
2323 }
2324 else if ((insn & 0xf0) == 0x80)
2325 {
2326 int mask = ((insn & 0xf) << 8) | *entry++;
2327 int i;
2328
2329 /* The special case of an all-zero mask identifies
2330 "Refuse to unwind". We return NULL to fall back
2331 to the prologue analyzer. */
2332 if (mask == 0)
2333 return NULL;
2334
2335 /* Pop registers r4..r15 under mask. */
2336 for (i = 0; i < 12; i++)
2337 if (mask & (1 << i))
2338 {
2339 cache->saved_regs[4 + i].addr = vsp;
2340 vsp += 4;
2341 }
2342
2343 /* Special-case popping SP -- we need to reload vsp. */
2344 if (mask & (1 << (ARM_SP_REGNUM - 4)))
2345 vsp_valid = 0;
2346 }
2347 else if ((insn & 0xf0) == 0x90)
2348 {
2349 int reg = insn & 0xf;
2350
2351 /* Reserved cases. */
2352 if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
2353 return NULL;
2354
2355 /* Set SP from another register and mark VSP for reload. */
2356 cache->saved_regs[ARM_SP_REGNUM] = cache->saved_regs[reg];
2357 vsp_valid = 0;
2358 }
2359 else if ((insn & 0xf0) == 0xa0)
2360 {
2361 int count = insn & 0x7;
2362 int pop_lr = (insn & 0x8) != 0;
2363 int i;
2364
2365 /* Pop r4..r[4+count]. */
2366 for (i = 0; i <= count; i++)
2367 {
2368 cache->saved_regs[4 + i].addr = vsp;
2369 vsp += 4;
2370 }
2371
2372 /* If indicated by flag, pop LR as well. */
2373 if (pop_lr)
2374 {
2375 cache->saved_regs[ARM_LR_REGNUM].addr = vsp;
2376 vsp += 4;
2377 }
2378 }
2379 else if (insn == 0xb0)
2380 {
2381 /* We could only have updated PC by popping into it; if so, it
2382 will show up as address. Otherwise, copy LR into PC. */
2383 if (!trad_frame_addr_p (cache->saved_regs, ARM_PC_REGNUM))
2384 cache->saved_regs[ARM_PC_REGNUM]
2385 = cache->saved_regs[ARM_LR_REGNUM];
2386
2387 /* We're done. */
2388 break;
2389 }
2390 else if (insn == 0xb1)
2391 {
2392 int mask = *entry++;
2393 int i;
2394
2395 /* All-zero mask and mask >= 16 is "spare". */
2396 if (mask == 0 || mask >= 16)
2397 return NULL;
2398
2399 /* Pop r0..r3 under mask. */
2400 for (i = 0; i < 4; i++)
2401 if (mask & (1 << i))
2402 {
2403 cache->saved_regs[i].addr = vsp;
2404 vsp += 4;
2405 }
2406 }
2407 else if (insn == 0xb2)
2408 {
2409 ULONGEST offset = 0;
2410 unsigned shift = 0;
2411
2412 do
2413 {
2414 offset |= (*entry & 0x7f) << shift;
2415 shift += 7;
2416 }
2417 while (*entry++ & 0x80);
2418
2419 vsp += 0x204 + (offset << 2);
2420 }
2421 else if (insn == 0xb3)
2422 {
2423 int start = *entry >> 4;
2424 int count = (*entry++) & 0xf;
2425 int i;
2426
2427 /* Only registers D0..D15 are valid here. */
2428 if (start + count >= 16)
2429 return NULL;
2430
2431 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2432 for (i = 0; i <= count; i++)
2433 {
2434 cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2435 vsp += 8;
2436 }
2437
2438 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2439 vsp += 4;
2440 }
2441 else if ((insn & 0xf8) == 0xb8)
2442 {
2443 int count = insn & 0x7;
2444 int i;
2445
2446 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2447 for (i = 0; i <= count; i++)
2448 {
2449 cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2450 vsp += 8;
2451 }
2452
2453 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2454 vsp += 4;
2455 }
2456 else if (insn == 0xc6)
2457 {
2458 int start = *entry >> 4;
2459 int count = (*entry++) & 0xf;
2460 int i;
2461
2462 /* Only registers WR0..WR15 are valid. */
2463 if (start + count >= 16)
2464 return NULL;
2465
2466 /* Pop iwmmx registers WR[start]..WR[start+count]. */
2467 for (i = 0; i <= count; i++)
2468 {
2469 cache->saved_regs[ARM_WR0_REGNUM + start + i].addr = vsp;
2470 vsp += 8;
2471 }
2472 }
2473 else if (insn == 0xc7)
2474 {
2475 int mask = *entry++;
2476 int i;
2477
2478 /* All-zero mask and mask >= 16 is "spare". */
2479 if (mask == 0 || mask >= 16)
2480 return NULL;
2481
2482 /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask. */
2483 for (i = 0; i < 4; i++)
2484 if (mask & (1 << i))
2485 {
2486 cache->saved_regs[ARM_WCGR0_REGNUM + i].addr = vsp;
2487 vsp += 4;
2488 }
2489 }
2490 else if ((insn & 0xf8) == 0xc0)
2491 {
2492 int count = insn & 0x7;
2493 int i;
2494
2495 /* Pop iwmmx registers WR[10]..WR[10+count]. */
2496 for (i = 0; i <= count; i++)
2497 {
2498 cache->saved_regs[ARM_WR0_REGNUM + 10 + i].addr = vsp;
2499 vsp += 8;
2500 }
2501 }
2502 else if (insn == 0xc8)
2503 {
2504 int start = *entry >> 4;
2505 int count = (*entry++) & 0xf;
2506 int i;
2507
2508 /* Only registers D0..D31 are valid. */
2509 if (start + count >= 16)
2510 return NULL;
2511
2512 /* Pop VFP double-precision registers
2513 D[16+start]..D[16+start+count]. */
2514 for (i = 0; i <= count; i++)
2515 {
2516 cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].addr = vsp;
2517 vsp += 8;
2518 }
2519 }
2520 else if (insn == 0xc9)
2521 {
2522 int start = *entry >> 4;
2523 int count = (*entry++) & 0xf;
2524 int i;
2525
2526 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2527 for (i = 0; i <= count; i++)
2528 {
2529 cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2530 vsp += 8;
2531 }
2532 }
2533 else if ((insn & 0xf8) == 0xd0)
2534 {
2535 int count = insn & 0x7;
2536 int i;
2537
2538 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2539 for (i = 0; i <= count; i++)
2540 {
2541 cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2542 vsp += 8;
2543 }
2544 }
2545 else
2546 {
2547 /* Everything else is "spare". */
2548 return NULL;
2549 }
2550 }
2551
2552 /* If we restore SP from a register, assume this was the frame register.
2553 Otherwise just fall back to SP as frame register. */
2554 if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2555 cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2556 else
2557 cache->framereg = ARM_SP_REGNUM;
2558
2559 /* Determine offset to previous frame. */
2560 cache->framesize
2561 = vsp - get_frame_register_unsigned (this_frame, cache->framereg);
2562
2563 /* We already got the previous SP. */
2564 cache->prev_sp = vsp;
2565
2566 return cache;
2567}
2568
2569/* Unwinding via ARM exception table entries. Note that the sniffer
2570 already computes a filled-in prologue cache, which is then used
2571 with the same arm_prologue_this_id and arm_prologue_prev_register
2572 routines also used for prologue-parsing based unwinding. */
2573
2574static int
2575arm_exidx_unwind_sniffer (const struct frame_unwind *self,
2576 struct frame_info *this_frame,
2577 void **this_prologue_cache)
2578{
2579 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2580 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
2581 CORE_ADDR addr_in_block, exidx_region, func_start;
2582 struct arm_prologue_cache *cache;
2583 gdb_byte *entry;
2584
2585 /* See if we have an ARM exception table entry covering this address. */
2586 addr_in_block = get_frame_address_in_block (this_frame);
2587 entry = arm_find_exidx_entry (addr_in_block, &exidx_region);
2588 if (!entry)
2589 return 0;
2590
2591 /* The ARM exception table does not describe unwind information
2592 for arbitrary PC values, but is guaranteed to be correct only
2593 at call sites. We have to decide here whether we want to use
2594 ARM exception table information for this frame, or fall back
2595 to using prologue parsing. (Note that if we have DWARF CFI,
2596 this sniffer isn't even called -- CFI is always preferred.)
2597
2598 Before we make this decision, however, we check whether we
2599 actually have *symbol* information for the current frame.
2600 If not, prologue parsing would not work anyway, so we might
2601 as well use the exception table and hope for the best. */
2602 if (find_pc_partial_function (addr_in_block, NULL, &func_start, NULL))
2603 {
2604 int exc_valid = 0;
2605
2606 /* If the next frame is "normal", we are at a call site in this
2607 frame, so exception information is guaranteed to be valid. */
2608 if (get_next_frame (this_frame)
2609 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
2610 exc_valid = 1;
2611
2612 /* We also assume exception information is valid if we're currently
2613 blocked in a system call. The system library is supposed to
d9311bfa
AT
2614 ensure this, so that e.g. pthread cancellation works. */
2615 if (arm_frame_is_thumb (this_frame))
0e9e9abd 2616 {
d9311bfa 2617 LONGEST insn;
416dc9c6 2618
d9311bfa
AT
2619 if (safe_read_memory_integer (get_frame_pc (this_frame) - 2, 2,
2620 byte_order_for_code, &insn)
2621 && (insn & 0xff00) == 0xdf00 /* svc */)
2622 exc_valid = 1;
0e9e9abd 2623 }
d9311bfa
AT
2624 else
2625 {
2626 LONGEST insn;
416dc9c6 2627
d9311bfa
AT
2628 if (safe_read_memory_integer (get_frame_pc (this_frame) - 4, 4,
2629 byte_order_for_code, &insn)
2630 && (insn & 0x0f000000) == 0x0f000000 /* svc */)
2631 exc_valid = 1;
2632 }
2633
0e9e9abd
UW
2634 /* Bail out if we don't know that exception information is valid. */
2635 if (!exc_valid)
2636 return 0;
2637
2638 /* The ARM exception index does not mark the *end* of the region
2639 covered by the entry, and some functions will not have any entry.
2640 To correctly recognize the end of the covered region, the linker
2641 should have inserted dummy records with a CANTUNWIND marker.
2642
2643 Unfortunately, current versions of GNU ld do not reliably do
2644 this, and thus we may have found an incorrect entry above.
2645 As a (temporary) sanity check, we only use the entry if it
2646 lies *within* the bounds of the function. Note that this check
2647 might reject perfectly valid entries that just happen to cover
2648 multiple functions; therefore this check ought to be removed
2649 once the linker is fixed. */
2650 if (func_start > exidx_region)
2651 return 0;
2652 }
2653
2654 /* Decode the list of unwinding instructions into a prologue cache.
2655 Note that this may fail due to e.g. a "refuse to unwind" code. */
2656 cache = arm_exidx_fill_cache (this_frame, entry);
2657 if (!cache)
2658 return 0;
2659
2660 *this_prologue_cache = cache;
2661 return 1;
2662}
2663
2664struct frame_unwind arm_exidx_unwind = {
2665 NORMAL_FRAME,
8fbca658 2666 default_frame_unwind_stop_reason,
0e9e9abd
UW
2667 arm_prologue_this_id,
2668 arm_prologue_prev_register,
2669 NULL,
2670 arm_exidx_unwind_sniffer
2671};
2672
80d8d390
YQ
2673/* Recognize GCC's trampoline for thumb call-indirect. If we are in a
2674 trampoline, return the target PC. Otherwise return 0.
2675
2676 void call0a (char c, short s, int i, long l) {}
2677
2678 int main (void)
2679 {
2680 (*pointer_to_call0a) (c, s, i, l);
2681 }
2682
2683 Instead of calling a stub library function _call_via_xx (xx is
2684 the register name), GCC may inline the trampoline in the object
2685 file as below (register r2 has the address of call0a).
2686
2687 .global main
2688 .type main, %function
2689 ...
2690 bl .L1
2691 ...
2692 .size main, .-main
2693
2694 .L1:
2695 bx r2
2696
2697 The trampoline 'bx r2' doesn't belong to main. */
2698
2699static CORE_ADDR
2700arm_skip_bx_reg (struct frame_info *frame, CORE_ADDR pc)
2701{
2702 /* The heuristics of recognizing such trampoline is that FRAME is
2703 executing in Thumb mode and the instruction on PC is 'bx Rm'. */
2704 if (arm_frame_is_thumb (frame))
2705 {
2706 gdb_byte buf[2];
2707
2708 if (target_read_memory (pc, buf, 2) == 0)
2709 {
2710 struct gdbarch *gdbarch = get_frame_arch (frame);
2711 enum bfd_endian byte_order_for_code
2712 = gdbarch_byte_order_for_code (gdbarch);
2713 uint16_t insn
2714 = extract_unsigned_integer (buf, 2, byte_order_for_code);
2715
2716 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
2717 {
2718 CORE_ADDR dest
2719 = get_frame_register_unsigned (frame, bits (insn, 3, 6));
2720
2721 /* Clear the LSB so that gdb core sets step-resume
2722 breakpoint at the right address. */
2723 return UNMAKE_THUMB_ADDR (dest);
2724 }
2725 }
2726 }
2727
2728 return 0;
2729}
2730
909cf6ea 2731static struct arm_prologue_cache *
a262aec2 2732arm_make_stub_cache (struct frame_info *this_frame)
909cf6ea 2733{
909cf6ea 2734 struct arm_prologue_cache *cache;
909cf6ea 2735
35d5d4ee 2736 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
a262aec2 2737 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
909cf6ea 2738
a262aec2 2739 cache->prev_sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
909cf6ea
DJ
2740
2741 return cache;
2742}
2743
2744/* Our frame ID for a stub frame is the current SP and LR. */
2745
2746static void
a262aec2 2747arm_stub_this_id (struct frame_info *this_frame,
909cf6ea
DJ
2748 void **this_cache,
2749 struct frame_id *this_id)
2750{
2751 struct arm_prologue_cache *cache;
2752
2753 if (*this_cache == NULL)
a262aec2 2754 *this_cache = arm_make_stub_cache (this_frame);
9a3c8263 2755 cache = (struct arm_prologue_cache *) *this_cache;
909cf6ea 2756
a262aec2 2757 *this_id = frame_id_build (cache->prev_sp, get_frame_pc (this_frame));
909cf6ea
DJ
2758}
2759
a262aec2
DJ
2760static int
2761arm_stub_unwind_sniffer (const struct frame_unwind *self,
2762 struct frame_info *this_frame,
2763 void **this_prologue_cache)
909cf6ea 2764{
93d42b30 2765 CORE_ADDR addr_in_block;
948f8e3d 2766 gdb_byte dummy[4];
18d18ac8
YQ
2767 CORE_ADDR pc, start_addr;
2768 const char *name;
909cf6ea 2769
a262aec2 2770 addr_in_block = get_frame_address_in_block (this_frame);
18d18ac8 2771 pc = get_frame_pc (this_frame);
3e5d3a5a 2772 if (in_plt_section (addr_in_block)
fc36e839
DE
2773 /* We also use the stub winder if the target memory is unreadable
2774 to avoid having the prologue unwinder trying to read it. */
18d18ac8
YQ
2775 || target_read_memory (pc, dummy, 4) != 0)
2776 return 1;
2777
2778 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0
2779 && arm_skip_bx_reg (this_frame, pc) != 0)
a262aec2 2780 return 1;
909cf6ea 2781
a262aec2 2782 return 0;
909cf6ea
DJ
2783}
2784
a262aec2
DJ
2785struct frame_unwind arm_stub_unwind = {
2786 NORMAL_FRAME,
8fbca658 2787 default_frame_unwind_stop_reason,
a262aec2
DJ
2788 arm_stub_this_id,
2789 arm_prologue_prev_register,
2790 NULL,
2791 arm_stub_unwind_sniffer
2792};
2793
2ae28aa9
YQ
2794/* Put here the code to store, into CACHE->saved_regs, the addresses
2795 of the saved registers of frame described by THIS_FRAME. CACHE is
2796 returned. */
2797
2798static struct arm_prologue_cache *
2799arm_m_exception_cache (struct frame_info *this_frame)
2800{
2801 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2802 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2803 struct arm_prologue_cache *cache;
2804 CORE_ADDR unwound_sp;
2805 LONGEST xpsr;
2806
2807 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2808 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2809
2810 unwound_sp = get_frame_register_unsigned (this_frame,
2811 ARM_SP_REGNUM);
2812
2813 /* The hardware saves eight 32-bit words, comprising xPSR,
2814 ReturnAddress, LR (R14), R12, R3, R2, R1, R0. See details in
2815 "B1.5.6 Exception entry behavior" in
2816 "ARMv7-M Architecture Reference Manual". */
2817 cache->saved_regs[0].addr = unwound_sp;
2818 cache->saved_regs[1].addr = unwound_sp + 4;
2819 cache->saved_regs[2].addr = unwound_sp + 8;
2820 cache->saved_regs[3].addr = unwound_sp + 12;
2821 cache->saved_regs[12].addr = unwound_sp + 16;
2822 cache->saved_regs[14].addr = unwound_sp + 20;
2823 cache->saved_regs[15].addr = unwound_sp + 24;
2824 cache->saved_regs[ARM_PS_REGNUM].addr = unwound_sp + 28;
2825
2826 /* If bit 9 of the saved xPSR is set, then there is a four-byte
2827 aligner between the top of the 32-byte stack frame and the
2828 previous context's stack pointer. */
2829 cache->prev_sp = unwound_sp + 32;
2830 if (safe_read_memory_integer (unwound_sp + 28, 4, byte_order, &xpsr)
2831 && (xpsr & (1 << 9)) != 0)
2832 cache->prev_sp += 4;
2833
2834 return cache;
2835}
2836
2837/* Implementation of function hook 'this_id' in
2838 'struct frame_uwnind'. */
2839
2840static void
2841arm_m_exception_this_id (struct frame_info *this_frame,
2842 void **this_cache,
2843 struct frame_id *this_id)
2844{
2845 struct arm_prologue_cache *cache;
2846
2847 if (*this_cache == NULL)
2848 *this_cache = arm_m_exception_cache (this_frame);
9a3c8263 2849 cache = (struct arm_prologue_cache *) *this_cache;
2ae28aa9
YQ
2850
2851 /* Our frame ID for a stub frame is the current SP and LR. */
2852 *this_id = frame_id_build (cache->prev_sp,
2853 get_frame_pc (this_frame));
2854}
2855
2856/* Implementation of function hook 'prev_register' in
2857 'struct frame_uwnind'. */
2858
2859static struct value *
2860arm_m_exception_prev_register (struct frame_info *this_frame,
2861 void **this_cache,
2862 int prev_regnum)
2863{
2ae28aa9
YQ
2864 struct arm_prologue_cache *cache;
2865
2866 if (*this_cache == NULL)
2867 *this_cache = arm_m_exception_cache (this_frame);
9a3c8263 2868 cache = (struct arm_prologue_cache *) *this_cache;
2ae28aa9
YQ
2869
2870 /* The value was already reconstructed into PREV_SP. */
2871 if (prev_regnum == ARM_SP_REGNUM)
2872 return frame_unwind_got_constant (this_frame, prev_regnum,
2873 cache->prev_sp);
2874
2875 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
2876 prev_regnum);
2877}
2878
2879/* Implementation of function hook 'sniffer' in
2880 'struct frame_uwnind'. */
2881
2882static int
2883arm_m_exception_unwind_sniffer (const struct frame_unwind *self,
2884 struct frame_info *this_frame,
2885 void **this_prologue_cache)
2886{
2887 CORE_ADDR this_pc = get_frame_pc (this_frame);
2888
2889 /* No need to check is_m; this sniffer is only registered for
2890 M-profile architectures. */
2891
2892 /* Exception frames return to one of these magic PCs. Other values
2893 are not defined as of v7-M. See details in "B1.5.8 Exception
2894 return behavior" in "ARMv7-M Architecture Reference Manual". */
2895 if (this_pc == 0xfffffff1 || this_pc == 0xfffffff9
2896 || this_pc == 0xfffffffd)
2897 return 1;
2898
2899 return 0;
2900}
2901
2902/* Frame unwinder for M-profile exceptions. */
2903
2904struct frame_unwind arm_m_exception_unwind =
2905{
2906 SIGTRAMP_FRAME,
2907 default_frame_unwind_stop_reason,
2908 arm_m_exception_this_id,
2909 arm_m_exception_prev_register,
2910 NULL,
2911 arm_m_exception_unwind_sniffer
2912};
2913
24de872b 2914static CORE_ADDR
a262aec2 2915arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
24de872b
DJ
2916{
2917 struct arm_prologue_cache *cache;
2918
eb5492fa 2919 if (*this_cache == NULL)
a262aec2 2920 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 2921 cache = (struct arm_prologue_cache *) *this_cache;
eb5492fa 2922
4be43953 2923 return cache->prev_sp - cache->framesize;
24de872b
DJ
2924}
2925
eb5492fa
DJ
2926struct frame_base arm_normal_base = {
2927 &arm_prologue_unwind,
2928 arm_normal_frame_base,
2929 arm_normal_frame_base,
2930 arm_normal_frame_base
2931};
2932
a262aec2 2933/* Assuming THIS_FRAME is a dummy, return the frame ID of that
eb5492fa
DJ
2934 dummy frame. The frame ID's base needs to match the TOS value
2935 saved by save_dummy_frame_tos() and returned from
2936 arm_push_dummy_call, and the PC needs to match the dummy frame's
2937 breakpoint. */
c906108c 2938
eb5492fa 2939static struct frame_id
a262aec2 2940arm_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
c906108c 2941{
0963b4bd
MS
2942 return frame_id_build (get_frame_register_unsigned (this_frame,
2943 ARM_SP_REGNUM),
a262aec2 2944 get_frame_pc (this_frame));
eb5492fa 2945}
c3b4394c 2946
eb5492fa
DJ
2947/* Given THIS_FRAME, find the previous frame's resume PC (which will
2948 be used to construct the previous frame's ID, after looking up the
2949 containing function). */
c3b4394c 2950
eb5492fa
DJ
2951static CORE_ADDR
2952arm_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
2953{
2954 CORE_ADDR pc;
2955 pc = frame_unwind_register_unsigned (this_frame, ARM_PC_REGNUM);
24568a2c 2956 return arm_addr_bits_remove (gdbarch, pc);
eb5492fa
DJ
2957}
2958
2959static CORE_ADDR
2960arm_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
2961{
2962 return frame_unwind_register_unsigned (this_frame, ARM_SP_REGNUM);
c906108c
SS
2963}
2964
b39cc962
DJ
2965static struct value *
2966arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
2967 int regnum)
2968{
24568a2c 2969 struct gdbarch * gdbarch = get_frame_arch (this_frame);
b39cc962 2970 CORE_ADDR lr, cpsr;
9779414d 2971 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
b39cc962
DJ
2972
2973 switch (regnum)
2974 {
2975 case ARM_PC_REGNUM:
2976 /* The PC is normally copied from the return column, which
2977 describes saves of LR. However, that version may have an
2978 extra bit set to indicate Thumb state. The bit is not
2979 part of the PC. */
2980 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2981 return frame_unwind_got_constant (this_frame, regnum,
24568a2c 2982 arm_addr_bits_remove (gdbarch, lr));
b39cc962
DJ
2983
2984 case ARM_PS_REGNUM:
2985 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
ca38c58e 2986 cpsr = get_frame_register_unsigned (this_frame, regnum);
b39cc962
DJ
2987 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2988 if (IS_THUMB_ADDR (lr))
9779414d 2989 cpsr |= t_bit;
b39cc962 2990 else
9779414d 2991 cpsr &= ~t_bit;
ca38c58e 2992 return frame_unwind_got_constant (this_frame, regnum, cpsr);
b39cc962
DJ
2993
2994 default:
2995 internal_error (__FILE__, __LINE__,
2996 _("Unexpected register %d"), regnum);
2997 }
2998}
2999
3000static void
3001arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
3002 struct dwarf2_frame_state_reg *reg,
3003 struct frame_info *this_frame)
3004{
3005 switch (regnum)
3006 {
3007 case ARM_PC_REGNUM:
3008 case ARM_PS_REGNUM:
3009 reg->how = DWARF2_FRAME_REG_FN;
3010 reg->loc.fn = arm_dwarf2_prev_register;
3011 break;
3012 case ARM_SP_REGNUM:
3013 reg->how = DWARF2_FRAME_REG_CFA;
3014 break;
3015 }
3016}
3017
c9cf6e20 3018/* Implement the stack_frame_destroyed_p gdbarch method. */
4024ca99
UW
3019
3020static int
c9cf6e20 3021thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
4024ca99
UW
3022{
3023 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3024 unsigned int insn, insn2;
3025 int found_return = 0, found_stack_adjust = 0;
3026 CORE_ADDR func_start, func_end;
3027 CORE_ADDR scan_pc;
3028 gdb_byte buf[4];
3029
3030 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3031 return 0;
3032
3033 /* The epilogue is a sequence of instructions along the following lines:
3034
3035 - add stack frame size to SP or FP
3036 - [if frame pointer used] restore SP from FP
3037 - restore registers from SP [may include PC]
3038 - a return-type instruction [if PC wasn't already restored]
3039
3040 In a first pass, we scan forward from the current PC and verify the
3041 instructions we find as compatible with this sequence, ending in a
3042 return instruction.
3043
3044 However, this is not sufficient to distinguish indirect function calls
3045 within a function from indirect tail calls in the epilogue in some cases.
3046 Therefore, if we didn't already find any SP-changing instruction during
3047 forward scan, we add a backward scanning heuristic to ensure we actually
3048 are in the epilogue. */
3049
3050 scan_pc = pc;
3051 while (scan_pc < func_end && !found_return)
3052 {
3053 if (target_read_memory (scan_pc, buf, 2))
3054 break;
3055
3056 scan_pc += 2;
3057 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3058
3059 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
3060 found_return = 1;
3061 else if (insn == 0x46f7) /* mov pc, lr */
3062 found_return = 1;
540314bd 3063 else if (thumb_instruction_restores_sp (insn))
4024ca99 3064 {
b7576e5c 3065 if ((insn & 0xff00) == 0xbd00) /* pop <registers, PC> */
4024ca99
UW
3066 found_return = 1;
3067 }
db24da6d 3068 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instruction */
4024ca99
UW
3069 {
3070 if (target_read_memory (scan_pc, buf, 2))
3071 break;
3072
3073 scan_pc += 2;
3074 insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3075
3076 if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3077 {
4024ca99
UW
3078 if (insn2 & 0x8000) /* <registers> include PC. */
3079 found_return = 1;
3080 }
3081 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3082 && (insn2 & 0x0fff) == 0x0b04)
3083 {
4024ca99
UW
3084 if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC. */
3085 found_return = 1;
3086 }
3087 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3088 && (insn2 & 0x0e00) == 0x0a00)
6b65d1b6 3089 ;
4024ca99
UW
3090 else
3091 break;
3092 }
3093 else
3094 break;
3095 }
3096
3097 if (!found_return)
3098 return 0;
3099
3100 /* Since any instruction in the epilogue sequence, with the possible
3101 exception of return itself, updates the stack pointer, we need to
3102 scan backwards for at most one instruction. Try either a 16-bit or
3103 a 32-bit instruction. This is just a heuristic, so we do not worry
0963b4bd 3104 too much about false positives. */
4024ca99 3105
6b65d1b6
YQ
3106 if (pc - 4 < func_start)
3107 return 0;
3108 if (target_read_memory (pc - 4, buf, 4))
3109 return 0;
4024ca99 3110
6b65d1b6
YQ
3111 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3112 insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);
3113
3114 if (thumb_instruction_restores_sp (insn2))
3115 found_stack_adjust = 1;
3116 else if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3117 found_stack_adjust = 1;
3118 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3119 && (insn2 & 0x0fff) == 0x0b04)
3120 found_stack_adjust = 1;
3121 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3122 && (insn2 & 0x0e00) == 0x0a00)
3123 found_stack_adjust = 1;
4024ca99
UW
3124
3125 return found_stack_adjust;
3126}
3127
c9cf6e20 3128/* Implement the stack_frame_destroyed_p gdbarch method. */
4024ca99
UW
3129
3130static int
c9cf6e20 3131arm_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
4024ca99
UW
3132{
3133 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3134 unsigned int insn;
f303bc3e 3135 int found_return;
4024ca99
UW
3136 CORE_ADDR func_start, func_end;
3137
3138 if (arm_pc_is_thumb (gdbarch, pc))
c9cf6e20 3139 return thumb_stack_frame_destroyed_p (gdbarch, pc);
4024ca99
UW
3140
3141 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3142 return 0;
3143
3144 /* We are in the epilogue if the previous instruction was a stack
3145 adjustment and the next instruction is a possible return (bx, mov
3146 pc, or pop). We could have to scan backwards to find the stack
3147 adjustment, or forwards to find the return, but this is a decent
3148 approximation. First scan forwards. */
3149
3150 found_return = 0;
3151 insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
3152 if (bits (insn, 28, 31) != INST_NV)
3153 {
3154 if ((insn & 0x0ffffff0) == 0x012fff10)
3155 /* BX. */
3156 found_return = 1;
3157 else if ((insn & 0x0ffffff0) == 0x01a0f000)
3158 /* MOV PC. */
3159 found_return = 1;
3160 else if ((insn & 0x0fff0000) == 0x08bd0000
3161 && (insn & 0x0000c000) != 0)
3162 /* POP (LDMIA), including PC or LR. */
3163 found_return = 1;
3164 }
3165
3166 if (!found_return)
3167 return 0;
3168
3169 /* Scan backwards. This is just a heuristic, so do not worry about
3170 false positives from mode changes. */
3171
3172 if (pc < func_start + 4)
3173 return 0;
3174
3175 insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
f303bc3e 3176 if (arm_instruction_restores_sp (insn))
4024ca99
UW
3177 return 1;
3178
3179 return 0;
3180}
3181
3182
2dd604e7
RE
3183/* When arguments must be pushed onto the stack, they go on in reverse
3184 order. The code below implements a FILO (stack) to do this. */
3185
3186struct stack_item
3187{
3188 int len;
3189 struct stack_item *prev;
7c543f7b 3190 gdb_byte *data;
2dd604e7
RE
3191};
3192
3193static struct stack_item *
df3b6708 3194push_stack_item (struct stack_item *prev, const gdb_byte *contents, int len)
2dd604e7
RE
3195{
3196 struct stack_item *si;
8d749320 3197 si = XNEW (struct stack_item);
7c543f7b 3198 si->data = (gdb_byte *) xmalloc (len);
2dd604e7
RE
3199 si->len = len;
3200 si->prev = prev;
3201 memcpy (si->data, contents, len);
3202 return si;
3203}
3204
3205static struct stack_item *
3206pop_stack_item (struct stack_item *si)
3207{
3208 struct stack_item *dead = si;
3209 si = si->prev;
3210 xfree (dead->data);
3211 xfree (dead);
3212 return si;
3213}
3214
2af48f68
PB
3215
3216/* Return the alignment (in bytes) of the given type. */
3217
3218static int
3219arm_type_align (struct type *t)
3220{
3221 int n;
3222 int align;
3223 int falign;
3224
3225 t = check_typedef (t);
3226 switch (TYPE_CODE (t))
3227 {
3228 default:
3229 /* Should never happen. */
3230 internal_error (__FILE__, __LINE__, _("unknown type alignment"));
3231 return 4;
3232
3233 case TYPE_CODE_PTR:
3234 case TYPE_CODE_ENUM:
3235 case TYPE_CODE_INT:
3236 case TYPE_CODE_FLT:
3237 case TYPE_CODE_SET:
3238 case TYPE_CODE_RANGE:
2af48f68
PB
3239 case TYPE_CODE_REF:
3240 case TYPE_CODE_CHAR:
3241 case TYPE_CODE_BOOL:
3242 return TYPE_LENGTH (t);
3243
3244 case TYPE_CODE_ARRAY:
c4312b19
YQ
3245 if (TYPE_VECTOR (t))
3246 {
3247 /* Use the natural alignment for vector types (the same for
3248 scalar type), but the maximum alignment is 64-bit. */
3249 if (TYPE_LENGTH (t) > 8)
3250 return 8;
3251 else
3252 return TYPE_LENGTH (t);
3253 }
3254 else
3255 return arm_type_align (TYPE_TARGET_TYPE (t));
2af48f68 3256 case TYPE_CODE_COMPLEX:
2af48f68
PB
3257 return arm_type_align (TYPE_TARGET_TYPE (t));
3258
3259 case TYPE_CODE_STRUCT:
3260 case TYPE_CODE_UNION:
3261 align = 1;
3262 for (n = 0; n < TYPE_NFIELDS (t); n++)
3263 {
3264 falign = arm_type_align (TYPE_FIELD_TYPE (t, n));
3265 if (falign > align)
3266 align = falign;
3267 }
3268 return align;
3269 }
3270}
3271
90445bd3
DJ
3272/* Possible base types for a candidate for passing and returning in
3273 VFP registers. */
3274
3275enum arm_vfp_cprc_base_type
3276{
3277 VFP_CPRC_UNKNOWN,
3278 VFP_CPRC_SINGLE,
3279 VFP_CPRC_DOUBLE,
3280 VFP_CPRC_VEC64,
3281 VFP_CPRC_VEC128
3282};
3283
3284/* The length of one element of base type B. */
3285
3286static unsigned
3287arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
3288{
3289 switch (b)
3290 {
3291 case VFP_CPRC_SINGLE:
3292 return 4;
3293 case VFP_CPRC_DOUBLE:
3294 return 8;
3295 case VFP_CPRC_VEC64:
3296 return 8;
3297 case VFP_CPRC_VEC128:
3298 return 16;
3299 default:
3300 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3301 (int) b);
3302 }
3303}
3304
3305/* The character ('s', 'd' or 'q') for the type of VFP register used
3306 for passing base type B. */
3307
3308static int
3309arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
3310{
3311 switch (b)
3312 {
3313 case VFP_CPRC_SINGLE:
3314 return 's';
3315 case VFP_CPRC_DOUBLE:
3316 return 'd';
3317 case VFP_CPRC_VEC64:
3318 return 'd';
3319 case VFP_CPRC_VEC128:
3320 return 'q';
3321 default:
3322 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3323 (int) b);
3324 }
3325}
3326
3327/* Determine whether T may be part of a candidate for passing and
3328 returning in VFP registers, ignoring the limit on the total number
3329 of components. If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
3330 classification of the first valid component found; if it is not
3331 VFP_CPRC_UNKNOWN, all components must have the same classification
3332 as *BASE_TYPE. If it is found that T contains a type not permitted
3333 for passing and returning in VFP registers, a type differently
3334 classified from *BASE_TYPE, or two types differently classified
3335 from each other, return -1, otherwise return the total number of
3336 base-type elements found (possibly 0 in an empty structure or
817e0957
YQ
3337 array). Vector types are not currently supported, matching the
3338 generic AAPCS support. */
90445bd3
DJ
3339
3340static int
3341arm_vfp_cprc_sub_candidate (struct type *t,
3342 enum arm_vfp_cprc_base_type *base_type)
3343{
3344 t = check_typedef (t);
3345 switch (TYPE_CODE (t))
3346 {
3347 case TYPE_CODE_FLT:
3348 switch (TYPE_LENGTH (t))
3349 {
3350 case 4:
3351 if (*base_type == VFP_CPRC_UNKNOWN)
3352 *base_type = VFP_CPRC_SINGLE;
3353 else if (*base_type != VFP_CPRC_SINGLE)
3354 return -1;
3355 return 1;
3356
3357 case 8:
3358 if (*base_type == VFP_CPRC_UNKNOWN)
3359 *base_type = VFP_CPRC_DOUBLE;
3360 else if (*base_type != VFP_CPRC_DOUBLE)
3361 return -1;
3362 return 1;
3363
3364 default:
3365 return -1;
3366 }
3367 break;
3368
817e0957
YQ
3369 case TYPE_CODE_COMPLEX:
3370 /* Arguments of complex T where T is one of the types float or
3371 double get treated as if they are implemented as:
3372
3373 struct complexT
3374 {
3375 T real;
3376 T imag;
5f52445b
YQ
3377 };
3378
3379 */
817e0957
YQ
3380 switch (TYPE_LENGTH (t))
3381 {
3382 case 8:
3383 if (*base_type == VFP_CPRC_UNKNOWN)
3384 *base_type = VFP_CPRC_SINGLE;
3385 else if (*base_type != VFP_CPRC_SINGLE)
3386 return -1;
3387 return 2;
3388
3389 case 16:
3390 if (*base_type == VFP_CPRC_UNKNOWN)
3391 *base_type = VFP_CPRC_DOUBLE;
3392 else if (*base_type != VFP_CPRC_DOUBLE)
3393 return -1;
3394 return 2;
3395
3396 default:
3397 return -1;
3398 }
3399 break;
3400
90445bd3
DJ
3401 case TYPE_CODE_ARRAY:
3402 {
c4312b19 3403 if (TYPE_VECTOR (t))
90445bd3 3404 {
c4312b19
YQ
3405 /* A 64-bit or 128-bit containerized vector type are VFP
3406 CPRCs. */
3407 switch (TYPE_LENGTH (t))
3408 {
3409 case 8:
3410 if (*base_type == VFP_CPRC_UNKNOWN)
3411 *base_type = VFP_CPRC_VEC64;
3412 return 1;
3413 case 16:
3414 if (*base_type == VFP_CPRC_UNKNOWN)
3415 *base_type = VFP_CPRC_VEC128;
3416 return 1;
3417 default:
3418 return -1;
3419 }
3420 }
3421 else
3422 {
3423 int count;
3424 unsigned unitlen;
3425
3426 count = arm_vfp_cprc_sub_candidate (TYPE_TARGET_TYPE (t),
3427 base_type);
3428 if (count == -1)
3429 return -1;
3430 if (TYPE_LENGTH (t) == 0)
3431 {
3432 gdb_assert (count == 0);
3433 return 0;
3434 }
3435 else if (count == 0)
3436 return -1;
3437 unitlen = arm_vfp_cprc_unit_length (*base_type);
3438 gdb_assert ((TYPE_LENGTH (t) % unitlen) == 0);
3439 return TYPE_LENGTH (t) / unitlen;
90445bd3 3440 }
90445bd3
DJ
3441 }
3442 break;
3443
3444 case TYPE_CODE_STRUCT:
3445 {
3446 int count = 0;
3447 unsigned unitlen;
3448 int i;
3449 for (i = 0; i < TYPE_NFIELDS (t); i++)
3450 {
3451 int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3452 base_type);
3453 if (sub_count == -1)
3454 return -1;
3455 count += sub_count;
3456 }
3457 if (TYPE_LENGTH (t) == 0)
3458 {
3459 gdb_assert (count == 0);
3460 return 0;
3461 }
3462 else if (count == 0)
3463 return -1;
3464 unitlen = arm_vfp_cprc_unit_length (*base_type);
3465 if (TYPE_LENGTH (t) != unitlen * count)
3466 return -1;
3467 return count;
3468 }
3469
3470 case TYPE_CODE_UNION:
3471 {
3472 int count = 0;
3473 unsigned unitlen;
3474 int i;
3475 for (i = 0; i < TYPE_NFIELDS (t); i++)
3476 {
3477 int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3478 base_type);
3479 if (sub_count == -1)
3480 return -1;
3481 count = (count > sub_count ? count : sub_count);
3482 }
3483 if (TYPE_LENGTH (t) == 0)
3484 {
3485 gdb_assert (count == 0);
3486 return 0;
3487 }
3488 else if (count == 0)
3489 return -1;
3490 unitlen = arm_vfp_cprc_unit_length (*base_type);
3491 if (TYPE_LENGTH (t) != unitlen * count)
3492 return -1;
3493 return count;
3494 }
3495
3496 default:
3497 break;
3498 }
3499
3500 return -1;
3501}
3502
3503/* Determine whether T is a VFP co-processor register candidate (CPRC)
3504 if passed to or returned from a non-variadic function with the VFP
3505 ABI in effect. Return 1 if it is, 0 otherwise. If it is, set
3506 *BASE_TYPE to the base type for T and *COUNT to the number of
3507 elements of that base type before returning. */
3508
3509static int
3510arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
3511 int *count)
3512{
3513 enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
3514 int c = arm_vfp_cprc_sub_candidate (t, &b);
3515 if (c <= 0 || c > 4)
3516 return 0;
3517 *base_type = b;
3518 *count = c;
3519 return 1;
3520}
3521
3522/* Return 1 if the VFP ABI should be used for passing arguments to and
3523 returning values from a function of type FUNC_TYPE, 0
3524 otherwise. */
3525
3526static int
3527arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
3528{
3529 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3530 /* Variadic functions always use the base ABI. Assume that functions
3531 without debug info are not variadic. */
3532 if (func_type && TYPE_VARARGS (check_typedef (func_type)))
3533 return 0;
3534 /* The VFP ABI is only supported as a variant of AAPCS. */
3535 if (tdep->arm_abi != ARM_ABI_AAPCS)
3536 return 0;
3537 return gdbarch_tdep (gdbarch)->fp_model == ARM_FLOAT_VFP;
3538}
3539
3540/* We currently only support passing parameters in integer registers, which
3541 conforms with GCC's default model, and VFP argument passing following
3542 the VFP variant of AAPCS. Several other variants exist and
2dd604e7
RE
3543 we should probably support some of them based on the selected ABI. */
3544
3545static CORE_ADDR
7d9b040b 3546arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6a65450a
AC
3547 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
3548 struct value **args, CORE_ADDR sp, int struct_return,
3549 CORE_ADDR struct_addr)
2dd604e7 3550{
e17a4113 3551 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2dd604e7
RE
3552 int argnum;
3553 int argreg;
3554 int nstack;
3555 struct stack_item *si = NULL;
90445bd3
DJ
3556 int use_vfp_abi;
3557 struct type *ftype;
3558 unsigned vfp_regs_free = (1 << 16) - 1;
3559
3560 /* Determine the type of this function and whether the VFP ABI
3561 applies. */
3562 ftype = check_typedef (value_type (function));
3563 if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
3564 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
3565 use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
2dd604e7 3566
6a65450a
AC
3567 /* Set the return address. For the ARM, the return breakpoint is
3568 always at BP_ADDR. */
9779414d 3569 if (arm_pc_is_thumb (gdbarch, bp_addr))
9dca5578 3570 bp_addr |= 1;
6a65450a 3571 regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
2dd604e7
RE
3572
3573 /* Walk through the list of args and determine how large a temporary
3574 stack is required. Need to take care here as structs may be
7a9dd1b2 3575 passed on the stack, and we have to push them. */
2dd604e7
RE
3576 nstack = 0;
3577
3578 argreg = ARM_A1_REGNUM;
3579 nstack = 0;
3580
2dd604e7
RE
3581 /* The struct_return pointer occupies the first parameter
3582 passing register. */
3583 if (struct_return)
3584 {
3585 if (arm_debug)
5af949e3 3586 fprintf_unfiltered (gdb_stdlog, "struct return in %s = %s\n",
2af46ca0 3587 gdbarch_register_name (gdbarch, argreg),
5af949e3 3588 paddress (gdbarch, struct_addr));
2dd604e7
RE
3589 regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
3590 argreg++;
3591 }
3592
3593 for (argnum = 0; argnum < nargs; argnum++)
3594 {
3595 int len;
3596 struct type *arg_type;
3597 struct type *target_type;
3598 enum type_code typecode;
8c6363cf 3599 const bfd_byte *val;
2af48f68 3600 int align;
90445bd3
DJ
3601 enum arm_vfp_cprc_base_type vfp_base_type;
3602 int vfp_base_count;
3603 int may_use_core_reg = 1;
2dd604e7 3604
df407dfe 3605 arg_type = check_typedef (value_type (args[argnum]));
2dd604e7
RE
3606 len = TYPE_LENGTH (arg_type);
3607 target_type = TYPE_TARGET_TYPE (arg_type);
3608 typecode = TYPE_CODE (arg_type);
8c6363cf 3609 val = value_contents (args[argnum]);
2dd604e7 3610
2af48f68
PB
3611 align = arm_type_align (arg_type);
3612 /* Round alignment up to a whole number of words. */
3613 align = (align + INT_REGISTER_SIZE - 1) & ~(INT_REGISTER_SIZE - 1);
3614 /* Different ABIs have different maximum alignments. */
3615 if (gdbarch_tdep (gdbarch)->arm_abi == ARM_ABI_APCS)
3616 {
3617 /* The APCS ABI only requires word alignment. */
3618 align = INT_REGISTER_SIZE;
3619 }
3620 else
3621 {
3622 /* The AAPCS requires at most doubleword alignment. */
3623 if (align > INT_REGISTER_SIZE * 2)
3624 align = INT_REGISTER_SIZE * 2;
3625 }
3626
90445bd3
DJ
3627 if (use_vfp_abi
3628 && arm_vfp_call_candidate (arg_type, &vfp_base_type,
3629 &vfp_base_count))
3630 {
3631 int regno;
3632 int unit_length;
3633 int shift;
3634 unsigned mask;
3635
3636 /* Because this is a CPRC it cannot go in a core register or
3637 cause a core register to be skipped for alignment.
3638 Either it goes in VFP registers and the rest of this loop
3639 iteration is skipped for this argument, or it goes on the
3640 stack (and the stack alignment code is correct for this
3641 case). */
3642 may_use_core_reg = 0;
3643
3644 unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
3645 shift = unit_length / 4;
3646 mask = (1 << (shift * vfp_base_count)) - 1;
3647 for (regno = 0; regno < 16; regno += shift)
3648 if (((vfp_regs_free >> regno) & mask) == mask)
3649 break;
3650
3651 if (regno < 16)
3652 {
3653 int reg_char;
3654 int reg_scaled;
3655 int i;
3656
3657 vfp_regs_free &= ~(mask << regno);
3658 reg_scaled = regno / shift;
3659 reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
3660 for (i = 0; i < vfp_base_count; i++)
3661 {
3662 char name_buf[4];
3663 int regnum;
58d6951d
DJ
3664 if (reg_char == 'q')
3665 arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
90445bd3 3666 val + i * unit_length);
58d6951d
DJ
3667 else
3668 {
8c042590
PM
3669 xsnprintf (name_buf, sizeof (name_buf), "%c%d",
3670 reg_char, reg_scaled + i);
58d6951d
DJ
3671 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
3672 strlen (name_buf));
3673 regcache_cooked_write (regcache, regnum,
3674 val + i * unit_length);
3675 }
90445bd3
DJ
3676 }
3677 continue;
3678 }
3679 else
3680 {
3681 /* This CPRC could not go in VFP registers, so all VFP
3682 registers are now marked as used. */
3683 vfp_regs_free = 0;
3684 }
3685 }
3686
2af48f68
PB
3687 /* Push stack padding for dowubleword alignment. */
3688 if (nstack & (align - 1))
3689 {
3690 si = push_stack_item (si, val, INT_REGISTER_SIZE);
3691 nstack += INT_REGISTER_SIZE;
3692 }
3693
3694 /* Doubleword aligned quantities must go in even register pairs. */
90445bd3
DJ
3695 if (may_use_core_reg
3696 && argreg <= ARM_LAST_ARG_REGNUM
2af48f68
PB
3697 && align > INT_REGISTER_SIZE
3698 && argreg & 1)
3699 argreg++;
3700
2dd604e7
RE
3701 /* If the argument is a pointer to a function, and it is a
3702 Thumb function, create a LOCAL copy of the value and set
3703 the THUMB bit in it. */
3704 if (TYPE_CODE_PTR == typecode
3705 && target_type != NULL
f96b8fa0 3706 && TYPE_CODE_FUNC == TYPE_CODE (check_typedef (target_type)))
2dd604e7 3707 {
e17a4113 3708 CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
9779414d 3709 if (arm_pc_is_thumb (gdbarch, regval))
2dd604e7 3710 {
224c3ddb 3711 bfd_byte *copy = (bfd_byte *) alloca (len);
8c6363cf 3712 store_unsigned_integer (copy, len, byte_order,
e17a4113 3713 MAKE_THUMB_ADDR (regval));
8c6363cf 3714 val = copy;
2dd604e7
RE
3715 }
3716 }
3717
3718 /* Copy the argument to general registers or the stack in
3719 register-sized pieces. Large arguments are split between
3720 registers and stack. */
3721 while (len > 0)
3722 {
f0c9063c 3723 int partial_len = len < INT_REGISTER_SIZE ? len : INT_REGISTER_SIZE;
ef9bd0b8
YQ
3724 CORE_ADDR regval
3725 = extract_unsigned_integer (val, partial_len, byte_order);
2dd604e7 3726
90445bd3 3727 if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
2dd604e7
RE
3728 {
3729 /* The argument is being passed in a general purpose
3730 register. */
e17a4113 3731 if (byte_order == BFD_ENDIAN_BIG)
8bf8793c 3732 regval <<= (INT_REGISTER_SIZE - partial_len) * 8;
2dd604e7
RE
3733 if (arm_debug)
3734 fprintf_unfiltered (gdb_stdlog, "arg %d in %s = 0x%s\n",
c9f4d572
UW
3735 argnum,
3736 gdbarch_register_name
2af46ca0 3737 (gdbarch, argreg),
f0c9063c 3738 phex (regval, INT_REGISTER_SIZE));
2dd604e7
RE
3739 regcache_cooked_write_unsigned (regcache, argreg, regval);
3740 argreg++;
3741 }
3742 else
3743 {
ef9bd0b8
YQ
3744 gdb_byte buf[INT_REGISTER_SIZE];
3745
3746 memset (buf, 0, sizeof (buf));
3747 store_unsigned_integer (buf, partial_len, byte_order, regval);
3748
2dd604e7
RE
3749 /* Push the arguments onto the stack. */
3750 if (arm_debug)
3751 fprintf_unfiltered (gdb_stdlog, "arg %d @ sp + %d\n",
3752 argnum, nstack);
ef9bd0b8 3753 si = push_stack_item (si, buf, INT_REGISTER_SIZE);
f0c9063c 3754 nstack += INT_REGISTER_SIZE;
2dd604e7
RE
3755 }
3756
3757 len -= partial_len;
3758 val += partial_len;
3759 }
3760 }
3761 /* If we have an odd number of words to push, then decrement the stack
3762 by one word now, so first stack argument will be dword aligned. */
3763 if (nstack & 4)
3764 sp -= 4;
3765
3766 while (si)
3767 {
3768 sp -= si->len;
3769 write_memory (sp, si->data, si->len);
3770 si = pop_stack_item (si);
3771 }
3772
3773 /* Finally, update teh SP register. */
3774 regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
3775
3776 return sp;
3777}
3778
f53f0d0b
PB
3779
3780/* Always align the frame to an 8-byte boundary. This is required on
3781 some platforms and harmless on the rest. */
3782
3783static CORE_ADDR
3784arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
3785{
3786 /* Align the stack to eight bytes. */
3787 return sp & ~ (CORE_ADDR) 7;
3788}
3789
c906108c 3790static void
12b27276 3791print_fpu_flags (struct ui_file *file, int flags)
c906108c 3792{
c5aa993b 3793 if (flags & (1 << 0))
12b27276 3794 fputs_filtered ("IVO ", file);
c5aa993b 3795 if (flags & (1 << 1))
12b27276 3796 fputs_filtered ("DVZ ", file);
c5aa993b 3797 if (flags & (1 << 2))
12b27276 3798 fputs_filtered ("OFL ", file);
c5aa993b 3799 if (flags & (1 << 3))
12b27276 3800 fputs_filtered ("UFL ", file);
c5aa993b 3801 if (flags & (1 << 4))
12b27276
WN
3802 fputs_filtered ("INX ", file);
3803 fputc_filtered ('\n', file);
c906108c
SS
3804}
3805
5e74b15c
RE
3806/* Print interesting information about the floating point processor
3807 (if present) or emulator. */
34e8f22d 3808static void
d855c300 3809arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
23e3a7ac 3810 struct frame_info *frame, const char *args)
c906108c 3811{
9c9acae0 3812 unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
c5aa993b
JM
3813 int type;
3814
3815 type = (status >> 24) & 127;
edefbb7c 3816 if (status & (1 << 31))
12b27276 3817 fprintf_filtered (file, _("Hardware FPU type %d\n"), type);
edefbb7c 3818 else
12b27276 3819 fprintf_filtered (file, _("Software FPU type %d\n"), type);
edefbb7c 3820 /* i18n: [floating point unit] mask */
12b27276
WN
3821 fputs_filtered (_("mask: "), file);
3822 print_fpu_flags (file, status >> 16);
edefbb7c 3823 /* i18n: [floating point unit] flags */
12b27276
WN
3824 fputs_filtered (_("flags: "), file);
3825 print_fpu_flags (file, status);
c906108c
SS
3826}
3827
27067745
UW
3828/* Construct the ARM extended floating point type. */
3829static struct type *
3830arm_ext_type (struct gdbarch *gdbarch)
3831{
3832 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3833
3834 if (!tdep->arm_ext_type)
3835 tdep->arm_ext_type
e9bb382b 3836 = arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
27067745
UW
3837 floatformats_arm_ext);
3838
3839 return tdep->arm_ext_type;
3840}
3841
58d6951d
DJ
3842static struct type *
3843arm_neon_double_type (struct gdbarch *gdbarch)
3844{
3845 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3846
3847 if (tdep->neon_double_type == NULL)
3848 {
3849 struct type *t, *elem;
3850
3851 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
3852 TYPE_CODE_UNION);
3853 elem = builtin_type (gdbarch)->builtin_uint8;
3854 append_composite_type_field (t, "u8", init_vector_type (elem, 8));
3855 elem = builtin_type (gdbarch)->builtin_uint16;
3856 append_composite_type_field (t, "u16", init_vector_type (elem, 4));
3857 elem = builtin_type (gdbarch)->builtin_uint32;
3858 append_composite_type_field (t, "u32", init_vector_type (elem, 2));
3859 elem = builtin_type (gdbarch)->builtin_uint64;
3860 append_composite_type_field (t, "u64", elem);
3861 elem = builtin_type (gdbarch)->builtin_float;
3862 append_composite_type_field (t, "f32", init_vector_type (elem, 2));
3863 elem = builtin_type (gdbarch)->builtin_double;
3864 append_composite_type_field (t, "f64", elem);
3865
3866 TYPE_VECTOR (t) = 1;
3867 TYPE_NAME (t) = "neon_d";
3868 tdep->neon_double_type = t;
3869 }
3870
3871 return tdep->neon_double_type;
3872}
3873
3874/* FIXME: The vector types are not correctly ordered on big-endian
3875 targets. Just as s0 is the low bits of d0, d0[0] is also the low
3876 bits of d0 - regardless of what unit size is being held in d0. So
3877 the offset of the first uint8 in d0 is 7, but the offset of the
3878 first float is 4. This code works as-is for little-endian
3879 targets. */
3880
3881static struct type *
3882arm_neon_quad_type (struct gdbarch *gdbarch)
3883{
3884 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3885
3886 if (tdep->neon_quad_type == NULL)
3887 {
3888 struct type *t, *elem;
3889
3890 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
3891 TYPE_CODE_UNION);
3892 elem = builtin_type (gdbarch)->builtin_uint8;
3893 append_composite_type_field (t, "u8", init_vector_type (elem, 16));
3894 elem = builtin_type (gdbarch)->builtin_uint16;
3895 append_composite_type_field (t, "u16", init_vector_type (elem, 8));
3896 elem = builtin_type (gdbarch)->builtin_uint32;
3897 append_composite_type_field (t, "u32", init_vector_type (elem, 4));
3898 elem = builtin_type (gdbarch)->builtin_uint64;
3899 append_composite_type_field (t, "u64", init_vector_type (elem, 2));
3900 elem = builtin_type (gdbarch)->builtin_float;
3901 append_composite_type_field (t, "f32", init_vector_type (elem, 4));
3902 elem = builtin_type (gdbarch)->builtin_double;
3903 append_composite_type_field (t, "f64", init_vector_type (elem, 2));
3904
3905 TYPE_VECTOR (t) = 1;
3906 TYPE_NAME (t) = "neon_q";
3907 tdep->neon_quad_type = t;
3908 }
3909
3910 return tdep->neon_quad_type;
3911}
3912
34e8f22d
RE
3913/* Return the GDB type object for the "standard" data type of data in
3914 register N. */
3915
3916static struct type *
7a5ea0d4 3917arm_register_type (struct gdbarch *gdbarch, int regnum)
032758dc 3918{
58d6951d
DJ
3919 int num_regs = gdbarch_num_regs (gdbarch);
3920
3921 if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
3922 && regnum >= num_regs && regnum < num_regs + 32)
3923 return builtin_type (gdbarch)->builtin_float;
3924
3925 if (gdbarch_tdep (gdbarch)->have_neon_pseudos
3926 && regnum >= num_regs + 32 && regnum < num_regs + 32 + 16)
3927 return arm_neon_quad_type (gdbarch);
3928
3929 /* If the target description has register information, we are only
3930 in this function so that we can override the types of
3931 double-precision registers for NEON. */
3932 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
3933 {
3934 struct type *t = tdesc_register_type (gdbarch, regnum);
3935
3936 if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
3937 && TYPE_CODE (t) == TYPE_CODE_FLT
3938 && gdbarch_tdep (gdbarch)->have_neon)
3939 return arm_neon_double_type (gdbarch);
3940 else
3941 return t;
3942 }
3943
34e8f22d 3944 if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
58d6951d
DJ
3945 {
3946 if (!gdbarch_tdep (gdbarch)->have_fpa_registers)
3947 return builtin_type (gdbarch)->builtin_void;
3948
3949 return arm_ext_type (gdbarch);
3950 }
e4c16157 3951 else if (regnum == ARM_SP_REGNUM)
0dfff4cb 3952 return builtin_type (gdbarch)->builtin_data_ptr;
e4c16157 3953 else if (regnum == ARM_PC_REGNUM)
0dfff4cb 3954 return builtin_type (gdbarch)->builtin_func_ptr;
ff6f572f
DJ
3955 else if (regnum >= ARRAY_SIZE (arm_register_names))
3956 /* These registers are only supported on targets which supply
3957 an XML description. */
df4df182 3958 return builtin_type (gdbarch)->builtin_int0;
032758dc 3959 else
df4df182 3960 return builtin_type (gdbarch)->builtin_uint32;
032758dc
AC
3961}
3962
ff6f572f
DJ
3963/* Map a DWARF register REGNUM onto the appropriate GDB register
3964 number. */
3965
3966static int
d3f73121 3967arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
ff6f572f
DJ
3968{
3969 /* Core integer regs. */
3970 if (reg >= 0 && reg <= 15)
3971 return reg;
3972
3973 /* Legacy FPA encoding. These were once used in a way which
3974 overlapped with VFP register numbering, so their use is
3975 discouraged, but GDB doesn't support the ARM toolchain
3976 which used them for VFP. */
3977 if (reg >= 16 && reg <= 23)
3978 return ARM_F0_REGNUM + reg - 16;
3979
3980 /* New assignments for the FPA registers. */
3981 if (reg >= 96 && reg <= 103)
3982 return ARM_F0_REGNUM + reg - 96;
3983
3984 /* WMMX register assignments. */
3985 if (reg >= 104 && reg <= 111)
3986 return ARM_WCGR0_REGNUM + reg - 104;
3987
3988 if (reg >= 112 && reg <= 127)
3989 return ARM_WR0_REGNUM + reg - 112;
3990
3991 if (reg >= 192 && reg <= 199)
3992 return ARM_WC0_REGNUM + reg - 192;
3993
58d6951d
DJ
3994 /* VFP v2 registers. A double precision value is actually
3995 in d1 rather than s2, but the ABI only defines numbering
3996 for the single precision registers. This will "just work"
3997 in GDB for little endian targets (we'll read eight bytes,
3998 starting in s0 and then progressing to s1), but will be
3999 reversed on big endian targets with VFP. This won't
4000 be a problem for the new Neon quad registers; you're supposed
4001 to use DW_OP_piece for those. */
4002 if (reg >= 64 && reg <= 95)
4003 {
4004 char name_buf[4];
4005
8c042590 4006 xsnprintf (name_buf, sizeof (name_buf), "s%d", reg - 64);
58d6951d
DJ
4007 return user_reg_map_name_to_regnum (gdbarch, name_buf,
4008 strlen (name_buf));
4009 }
4010
4011 /* VFP v3 / Neon registers. This range is also used for VFP v2
4012 registers, except that it now describes d0 instead of s0. */
4013 if (reg >= 256 && reg <= 287)
4014 {
4015 char name_buf[4];
4016
8c042590 4017 xsnprintf (name_buf, sizeof (name_buf), "d%d", reg - 256);
58d6951d
DJ
4018 return user_reg_map_name_to_regnum (gdbarch, name_buf,
4019 strlen (name_buf));
4020 }
4021
ff6f572f
DJ
4022 return -1;
4023}
4024
26216b98
AC
4025/* Map GDB internal REGNUM onto the Arm simulator register numbers. */
4026static int
e7faf938 4027arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
26216b98
AC
4028{
4029 int reg = regnum;
e7faf938 4030 gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
26216b98 4031
ff6f572f
DJ
4032 if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
4033 return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
4034
4035 if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
4036 return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
4037
4038 if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
4039 return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
4040
26216b98
AC
4041 if (reg < NUM_GREGS)
4042 return SIM_ARM_R0_REGNUM + reg;
4043 reg -= NUM_GREGS;
4044
4045 if (reg < NUM_FREGS)
4046 return SIM_ARM_FP0_REGNUM + reg;
4047 reg -= NUM_FREGS;
4048
4049 if (reg < NUM_SREGS)
4050 return SIM_ARM_FPS_REGNUM + reg;
4051 reg -= NUM_SREGS;
4052
edefbb7c 4053 internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
26216b98 4054}
34e8f22d 4055
a37b3cc0
AC
4056/* NOTE: cagney/2001-08-20: Both convert_from_extended() and
4057 convert_to_extended() use floatformat_arm_ext_littlebyte_bigword.
4058 It is thought that this is is the floating-point register format on
4059 little-endian systems. */
c906108c 4060
ed9a39eb 4061static void
b508a996 4062convert_from_extended (const struct floatformat *fmt, const void *ptr,
be8626e0 4063 void *dbl, int endianess)
c906108c 4064{
a37b3cc0 4065 DOUBLEST d;
be8626e0
MD
4066
4067 if (endianess == BFD_ENDIAN_BIG)
a37b3cc0
AC
4068 floatformat_to_doublest (&floatformat_arm_ext_big, ptr, &d);
4069 else
4070 floatformat_to_doublest (&floatformat_arm_ext_littlebyte_bigword,
4071 ptr, &d);
b508a996 4072 floatformat_from_doublest (fmt, &d, dbl);
c906108c
SS
4073}
4074
34e8f22d 4075static void
be8626e0
MD
4076convert_to_extended (const struct floatformat *fmt, void *dbl, const void *ptr,
4077 int endianess)
c906108c 4078{
a37b3cc0 4079 DOUBLEST d;
be8626e0 4080
b508a996 4081 floatformat_to_doublest (fmt, ptr, &d);
be8626e0 4082 if (endianess == BFD_ENDIAN_BIG)
a37b3cc0
AC
4083 floatformat_from_doublest (&floatformat_arm_ext_big, &d, dbl);
4084 else
4085 floatformat_from_doublest (&floatformat_arm_ext_littlebyte_bigword,
4086 &d, dbl);
c906108c 4087}
ed9a39eb 4088
d9311bfa
AT
4089/* Like insert_single_step_breakpoint, but make sure we use a breakpoint
4090 of the appropriate mode (as encoded in the PC value), even if this
4091 differs from what would be expected according to the symbol tables. */
4092
4093void
4094arm_insert_single_step_breakpoint (struct gdbarch *gdbarch,
4095 struct address_space *aspace,
4096 CORE_ADDR pc)
c906108c 4097{
d9311bfa
AT
4098 struct cleanup *old_chain
4099 = make_cleanup_restore_integer (&arm_override_mode);
c5aa993b 4100
d9311bfa
AT
4101 arm_override_mode = IS_THUMB_ADDR (pc);
4102 pc = gdbarch_addr_bits_remove (gdbarch, pc);
c5aa993b 4103
d9311bfa 4104 insert_single_step_breakpoint (gdbarch, aspace, pc);
c906108c 4105
d9311bfa
AT
4106 do_cleanups (old_chain);
4107}
c5aa993b 4108
d9311bfa
AT
4109/* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
4110 the buffer to be NEW_LEN bytes ending at ENDADDR. Return
4111 NULL if an error occurs. BUF is freed. */
c906108c 4112
d9311bfa
AT
4113static gdb_byte *
4114extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
4115 int old_len, int new_len)
4116{
4117 gdb_byte *new_buf;
4118 int bytes_to_read = new_len - old_len;
c906108c 4119
d9311bfa
AT
4120 new_buf = (gdb_byte *) xmalloc (new_len);
4121 memcpy (new_buf + bytes_to_read, buf, old_len);
4122 xfree (buf);
4123 if (target_read_memory (endaddr - new_len, new_buf, bytes_to_read) != 0)
4124 {
4125 xfree (new_buf);
4126 return NULL;
c906108c 4127 }
d9311bfa 4128 return new_buf;
c906108c
SS
4129}
4130
d9311bfa
AT
4131/* An IT block is at most the 2-byte IT instruction followed by
4132 four 4-byte instructions. The furthest back we must search to
4133 find an IT block that affects the current instruction is thus
4134 2 + 3 * 4 == 14 bytes. */
4135#define MAX_IT_BLOCK_PREFIX 14
177321bd 4136
d9311bfa
AT
4137/* Use a quick scan if there are more than this many bytes of
4138 code. */
4139#define IT_SCAN_THRESHOLD 32
177321bd 4140
d9311bfa
AT
4141/* Adjust a breakpoint's address to move breakpoints out of IT blocks.
4142 A breakpoint in an IT block may not be hit, depending on the
4143 condition flags. */
ad527d2e 4144static CORE_ADDR
d9311bfa 4145arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
c906108c 4146{
d9311bfa
AT
4147 gdb_byte *buf;
4148 char map_type;
4149 CORE_ADDR boundary, func_start;
4150 int buf_len;
4151 enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
4152 int i, any, last_it, last_it_count;
177321bd 4153
d9311bfa
AT
4154 /* If we are using BKPT breakpoints, none of this is necessary. */
4155 if (gdbarch_tdep (gdbarch)->thumb2_breakpoint == NULL)
4156 return bpaddr;
177321bd 4157
d9311bfa
AT
4158 /* ARM mode does not have this problem. */
4159 if (!arm_pc_is_thumb (gdbarch, bpaddr))
4160 return bpaddr;
177321bd 4161
d9311bfa
AT
4162 /* We are setting a breakpoint in Thumb code that could potentially
4163 contain an IT block. The first step is to find how much Thumb
4164 code there is; we do not need to read outside of known Thumb
4165 sequences. */
4166 map_type = arm_find_mapping_symbol (bpaddr, &boundary);
4167 if (map_type == 0)
4168 /* Thumb-2 code must have mapping symbols to have a chance. */
4169 return bpaddr;
9dca5578 4170
d9311bfa 4171 bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
177321bd 4172
d9311bfa
AT
4173 if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
4174 && func_start > boundary)
4175 boundary = func_start;
9dca5578 4176
d9311bfa
AT
4177 /* Search for a candidate IT instruction. We have to do some fancy
4178 footwork to distinguish a real IT instruction from the second
4179 half of a 32-bit instruction, but there is no need for that if
4180 there's no candidate. */
4181 buf_len = min (bpaddr - boundary, MAX_IT_BLOCK_PREFIX);
4182 if (buf_len == 0)
4183 /* No room for an IT instruction. */
4184 return bpaddr;
c906108c 4185
d9311bfa
AT
4186 buf = (gdb_byte *) xmalloc (buf_len);
4187 if (target_read_memory (bpaddr - buf_len, buf, buf_len) != 0)
4188 return bpaddr;
4189 any = 0;
4190 for (i = 0; i < buf_len; i += 2)
c906108c 4191 {
d9311bfa
AT
4192 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
4193 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
25b41d01 4194 {
d9311bfa
AT
4195 any = 1;
4196 break;
25b41d01 4197 }
c906108c 4198 }
d9311bfa
AT
4199
4200 if (any == 0)
c906108c 4201 {
d9311bfa
AT
4202 xfree (buf);
4203 return bpaddr;
f9d67f43
DJ
4204 }
4205
4206 /* OK, the code bytes before this instruction contain at least one
4207 halfword which resembles an IT instruction. We know that it's
4208 Thumb code, but there are still two possibilities. Either the
4209 halfword really is an IT instruction, or it is the second half of
4210 a 32-bit Thumb instruction. The only way we can tell is to
4211 scan forwards from a known instruction boundary. */
4212 if (bpaddr - boundary > IT_SCAN_THRESHOLD)
4213 {
4214 int definite;
4215
4216 /* There's a lot of code before this instruction. Start with an
4217 optimistic search; it's easy to recognize halfwords that can
4218 not be the start of a 32-bit instruction, and use that to
4219 lock on to the instruction boundaries. */
4220 buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
4221 if (buf == NULL)
4222 return bpaddr;
4223 buf_len = IT_SCAN_THRESHOLD;
4224
4225 definite = 0;
4226 for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
4227 {
4228 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
4229 if (thumb_insn_size (inst1) == 2)
4230 {
4231 definite = 1;
4232 break;
4233 }
4234 }
4235
4236 /* At this point, if DEFINITE, BUF[I] is the first place we
4237 are sure that we know the instruction boundaries, and it is far
4238 enough from BPADDR that we could not miss an IT instruction
4239 affecting BPADDR. If ! DEFINITE, give up - start from a
4240 known boundary. */
4241 if (! definite)
4242 {
0963b4bd
MS
4243 buf = extend_buffer_earlier (buf, bpaddr, buf_len,
4244 bpaddr - boundary);
f9d67f43
DJ
4245 if (buf == NULL)
4246 return bpaddr;
4247 buf_len = bpaddr - boundary;
4248 i = 0;
4249 }
4250 }
4251 else
4252 {
4253 buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
4254 if (buf == NULL)
4255 return bpaddr;
4256 buf_len = bpaddr - boundary;
4257 i = 0;
4258 }
4259
4260 /* Scan forwards. Find the last IT instruction before BPADDR. */
4261 last_it = -1;
4262 last_it_count = 0;
4263 while (i < buf_len)
4264 {
4265 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
4266 last_it_count--;
4267 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
4268 {
4269 last_it = i;
4270 if (inst1 & 0x0001)
4271 last_it_count = 4;
4272 else if (inst1 & 0x0002)
4273 last_it_count = 3;
4274 else if (inst1 & 0x0004)
4275 last_it_count = 2;
4276 else
4277 last_it_count = 1;
4278 }
4279 i += thumb_insn_size (inst1);
4280 }
4281
4282 xfree (buf);
4283
4284 if (last_it == -1)
4285 /* There wasn't really an IT instruction after all. */
4286 return bpaddr;
4287
4288 if (last_it_count < 1)
4289 /* It was too far away. */
4290 return bpaddr;
4291
4292 /* This really is a trouble spot. Move the breakpoint to the IT
4293 instruction. */
4294 return bpaddr - buf_len + last_it;
4295}
4296
cca44b1b 4297/* ARM displaced stepping support.
c906108c 4298
cca44b1b 4299 Generally ARM displaced stepping works as follows:
c906108c 4300
cca44b1b 4301 1. When an instruction is to be single-stepped, it is first decoded by
2ba163c8
SM
4302 arm_process_displaced_insn. Depending on the type of instruction, it is
4303 then copied to a scratch location, possibly in a modified form. The
4304 copy_* set of functions performs such modification, as necessary. A
4305 breakpoint is placed after the modified instruction in the scratch space
4306 to return control to GDB. Note in particular that instructions which
4307 modify the PC will no longer do so after modification.
c5aa993b 4308
cca44b1b
JB
4309 2. The instruction is single-stepped, by setting the PC to the scratch
4310 location address, and resuming. Control returns to GDB when the
4311 breakpoint is hit.
c5aa993b 4312
cca44b1b
JB
4313 3. A cleanup function (cleanup_*) is called corresponding to the copy_*
4314 function used for the current instruction. This function's job is to
4315 put the CPU/memory state back to what it would have been if the
4316 instruction had been executed unmodified in its original location. */
c5aa993b 4317
cca44b1b
JB
4318/* NOP instruction (mov r0, r0). */
4319#define ARM_NOP 0xe1a00000
34518530 4320#define THUMB_NOP 0x4600
cca44b1b
JB
4321
4322/* Helper for register reads for displaced stepping. In particular, this
4323 returns the PC as it would be seen by the instruction at its original
4324 location. */
4325
4326ULONGEST
36073a92
YQ
4327displaced_read_reg (struct regcache *regs, struct displaced_step_closure *dsc,
4328 int regno)
cca44b1b
JB
4329{
4330 ULONGEST ret;
36073a92 4331 CORE_ADDR from = dsc->insn_addr;
cca44b1b 4332
bf9f652a 4333 if (regno == ARM_PC_REGNUM)
cca44b1b 4334 {
4db71c0b
YQ
4335 /* Compute pipeline offset:
4336 - When executing an ARM instruction, PC reads as the address of the
4337 current instruction plus 8.
4338 - When executing a Thumb instruction, PC reads as the address of the
4339 current instruction plus 4. */
4340
36073a92 4341 if (!dsc->is_thumb)
4db71c0b
YQ
4342 from += 8;
4343 else
4344 from += 4;
4345
cca44b1b
JB
4346 if (debug_displaced)
4347 fprintf_unfiltered (gdb_stdlog, "displaced: read pc value %.8lx\n",
4db71c0b
YQ
4348 (unsigned long) from);
4349 return (ULONGEST) from;
cca44b1b 4350 }
c906108c 4351 else
cca44b1b
JB
4352 {
4353 regcache_cooked_read_unsigned (regs, regno, &ret);
4354 if (debug_displaced)
4355 fprintf_unfiltered (gdb_stdlog, "displaced: read r%d value %.8lx\n",
4356 regno, (unsigned long) ret);
4357 return ret;
4358 }
c906108c
SS
4359}
4360
cca44b1b
JB
4361static int
4362displaced_in_arm_mode (struct regcache *regs)
4363{
4364 ULONGEST ps;
9779414d 4365 ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
66e810cd 4366
cca44b1b 4367 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
66e810cd 4368
9779414d 4369 return (ps & t_bit) == 0;
cca44b1b 4370}
66e810cd 4371
cca44b1b 4372/* Write to the PC as from a branch instruction. */
c906108c 4373
cca44b1b 4374static void
36073a92
YQ
4375branch_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
4376 ULONGEST val)
c906108c 4377{
36073a92 4378 if (!dsc->is_thumb)
cca44b1b
JB
4379 /* Note: If bits 0/1 are set, this branch would be unpredictable for
4380 architecture versions < 6. */
0963b4bd
MS
4381 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
4382 val & ~(ULONGEST) 0x3);
cca44b1b 4383 else
0963b4bd
MS
4384 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
4385 val & ~(ULONGEST) 0x1);
cca44b1b 4386}
66e810cd 4387
cca44b1b
JB
4388/* Write to the PC as from a branch-exchange instruction. */
4389
4390static void
4391bx_write_pc (struct regcache *regs, ULONGEST val)
4392{
4393 ULONGEST ps;
9779414d 4394 ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
cca44b1b
JB
4395
4396 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
4397
4398 if ((val & 1) == 1)
c906108c 4399 {
9779414d 4400 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
cca44b1b
JB
4401 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
4402 }
4403 else if ((val & 2) == 0)
4404 {
9779414d 4405 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 4406 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
c906108c
SS
4407 }
4408 else
4409 {
cca44b1b
JB
4410 /* Unpredictable behaviour. Try to do something sensible (switch to ARM
4411 mode, align dest to 4 bytes). */
4412 warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
9779414d 4413 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 4414 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
c906108c
SS
4415 }
4416}
ed9a39eb 4417
cca44b1b 4418/* Write to the PC as if from a load instruction. */
ed9a39eb 4419
34e8f22d 4420static void
36073a92
YQ
4421load_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
4422 ULONGEST val)
ed9a39eb 4423{
cca44b1b
JB
4424 if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
4425 bx_write_pc (regs, val);
4426 else
36073a92 4427 branch_write_pc (regs, dsc, val);
cca44b1b 4428}
be8626e0 4429
cca44b1b
JB
4430/* Write to the PC as if from an ALU instruction. */
4431
4432static void
36073a92
YQ
4433alu_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
4434 ULONGEST val)
cca44b1b 4435{
36073a92 4436 if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
cca44b1b
JB
4437 bx_write_pc (regs, val);
4438 else
36073a92 4439 branch_write_pc (regs, dsc, val);
cca44b1b
JB
4440}
4441
4442/* Helper for writing to registers for displaced stepping. Writing to the PC
4443 has a varying effects depending on the instruction which does the write:
4444 this is controlled by the WRITE_PC argument. */
4445
4446void
4447displaced_write_reg (struct regcache *regs, struct displaced_step_closure *dsc,
4448 int regno, ULONGEST val, enum pc_write_style write_pc)
4449{
bf9f652a 4450 if (regno == ARM_PC_REGNUM)
08216dd7 4451 {
cca44b1b
JB
4452 if (debug_displaced)
4453 fprintf_unfiltered (gdb_stdlog, "displaced: writing pc %.8lx\n",
4454 (unsigned long) val);
4455 switch (write_pc)
08216dd7 4456 {
cca44b1b 4457 case BRANCH_WRITE_PC:
36073a92 4458 branch_write_pc (regs, dsc, val);
08216dd7
RE
4459 break;
4460
cca44b1b
JB
4461 case BX_WRITE_PC:
4462 bx_write_pc (regs, val);
4463 break;
4464
4465 case LOAD_WRITE_PC:
36073a92 4466 load_write_pc (regs, dsc, val);
cca44b1b
JB
4467 break;
4468
4469 case ALU_WRITE_PC:
36073a92 4470 alu_write_pc (regs, dsc, val);
cca44b1b
JB
4471 break;
4472
4473 case CANNOT_WRITE_PC:
4474 warning (_("Instruction wrote to PC in an unexpected way when "
4475 "single-stepping"));
08216dd7
RE
4476 break;
4477
4478 default:
97b9747c
JB
4479 internal_error (__FILE__, __LINE__,
4480 _("Invalid argument to displaced_write_reg"));
08216dd7 4481 }
b508a996 4482
cca44b1b 4483 dsc->wrote_to_pc = 1;
b508a996 4484 }
ed9a39eb 4485 else
b508a996 4486 {
cca44b1b
JB
4487 if (debug_displaced)
4488 fprintf_unfiltered (gdb_stdlog, "displaced: writing r%d value %.8lx\n",
4489 regno, (unsigned long) val);
4490 regcache_cooked_write_unsigned (regs, regno, val);
b508a996 4491 }
34e8f22d
RE
4492}
4493
cca44b1b
JB
4494/* This function is used to concisely determine if an instruction INSN
4495 references PC. Register fields of interest in INSN should have the
0963b4bd
MS
4496 corresponding fields of BITMASK set to 0b1111. The function
4497 returns return 1 if any of these fields in INSN reference the PC
4498 (also 0b1111, r15), else it returns 0. */
67255d04
RE
4499
4500static int
cca44b1b 4501insn_references_pc (uint32_t insn, uint32_t bitmask)
67255d04 4502{
cca44b1b 4503 uint32_t lowbit = 1;
67255d04 4504
cca44b1b
JB
4505 while (bitmask != 0)
4506 {
4507 uint32_t mask;
44e1a9eb 4508
cca44b1b
JB
4509 for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
4510 ;
67255d04 4511
cca44b1b
JB
4512 if (!lowbit)
4513 break;
67255d04 4514
cca44b1b 4515 mask = lowbit * 0xf;
67255d04 4516
cca44b1b
JB
4517 if ((insn & mask) == mask)
4518 return 1;
4519
4520 bitmask &= ~mask;
67255d04
RE
4521 }
4522
cca44b1b
JB
4523 return 0;
4524}
2af48f68 4525
cca44b1b
JB
4526/* The simplest copy function. Many instructions have the same effect no
4527 matter what address they are executed at: in those cases, use this. */
67255d04 4528
cca44b1b 4529static int
7ff120b4
YQ
4530arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn,
4531 const char *iname, struct displaced_step_closure *dsc)
cca44b1b
JB
4532{
4533 if (debug_displaced)
4534 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx, "
4535 "opcode/class '%s' unmodified\n", (unsigned long) insn,
4536 iname);
67255d04 4537
cca44b1b 4538 dsc->modinsn[0] = insn;
67255d04 4539
cca44b1b
JB
4540 return 0;
4541}
4542
34518530
YQ
4543static int
4544thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
4545 uint16_t insn2, const char *iname,
4546 struct displaced_step_closure *dsc)
4547{
4548 if (debug_displaced)
4549 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x %.4x, "
4550 "opcode/class '%s' unmodified\n", insn1, insn2,
4551 iname);
4552
4553 dsc->modinsn[0] = insn1;
4554 dsc->modinsn[1] = insn2;
4555 dsc->numinsns = 2;
4556
4557 return 0;
4558}
4559
4560/* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
4561 modification. */
4562static int
615234c1 4563thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, uint16_t insn,
34518530
YQ
4564 const char *iname,
4565 struct displaced_step_closure *dsc)
4566{
4567 if (debug_displaced)
4568 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x, "
4569 "opcode/class '%s' unmodified\n", insn,
4570 iname);
4571
4572 dsc->modinsn[0] = insn;
4573
4574 return 0;
4575}
4576
cca44b1b
JB
4577/* Preload instructions with immediate offset. */
4578
4579static void
6e39997a 4580cleanup_preload (struct gdbarch *gdbarch,
cca44b1b
JB
4581 struct regcache *regs, struct displaced_step_closure *dsc)
4582{
4583 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
4584 if (!dsc->u.preload.immed)
4585 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
4586}
4587
7ff120b4
YQ
4588static void
4589install_preload (struct gdbarch *gdbarch, struct regcache *regs,
4590 struct displaced_step_closure *dsc, unsigned int rn)
cca44b1b 4591{
cca44b1b 4592 ULONGEST rn_val;
cca44b1b
JB
4593 /* Preload instructions:
4594
4595 {pli/pld} [rn, #+/-imm]
4596 ->
4597 {pli/pld} [r0, #+/-imm]. */
4598
36073a92
YQ
4599 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
4600 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 4601 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
cca44b1b
JB
4602 dsc->u.preload.immed = 1;
4603
cca44b1b 4604 dsc->cleanup = &cleanup_preload;
cca44b1b
JB
4605}
4606
cca44b1b 4607static int
7ff120b4 4608arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
cca44b1b
JB
4609 struct displaced_step_closure *dsc)
4610{
4611 unsigned int rn = bits (insn, 16, 19);
cca44b1b 4612
7ff120b4
YQ
4613 if (!insn_references_pc (insn, 0x000f0000ul))
4614 return arm_copy_unmodified (gdbarch, insn, "preload", dsc);
cca44b1b
JB
4615
4616 if (debug_displaced)
4617 fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
4618 (unsigned long) insn);
4619
7ff120b4
YQ
4620 dsc->modinsn[0] = insn & 0xfff0ffff;
4621
4622 install_preload (gdbarch, regs, dsc, rn);
4623
4624 return 0;
4625}
4626
34518530
YQ
4627static int
4628thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
4629 struct regcache *regs, struct displaced_step_closure *dsc)
4630{
4631 unsigned int rn = bits (insn1, 0, 3);
4632 unsigned int u_bit = bit (insn1, 7);
4633 int imm12 = bits (insn2, 0, 11);
4634 ULONGEST pc_val;
4635
4636 if (rn != ARM_PC_REGNUM)
4637 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);
4638
4639 /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
4640 PLD (literal) Encoding T1. */
4641 if (debug_displaced)
4642 fprintf_unfiltered (gdb_stdlog,
4643 "displaced: copying pld/pli pc (0x%x) %c imm12 %.4x\n",
4644 (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
4645 imm12);
4646
4647 if (!u_bit)
4648 imm12 = -1 * imm12;
4649
4650 /* Rewrite instruction {pli/pld} PC imm12 into:
4651 Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12
4652
4653 {pli/pld} [r0, r1]
4654
4655 Cleanup: r0 <- tmp[0], r1 <- tmp[1]. */
4656
4657 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
4658 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
4659
4660 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
4661
4662 displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
4663 displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
4664 dsc->u.preload.immed = 0;
4665
4666 /* {pli/pld} [r0, r1] */
4667 dsc->modinsn[0] = insn1 & 0xfff0;
4668 dsc->modinsn[1] = 0xf001;
4669 dsc->numinsns = 2;
4670
4671 dsc->cleanup = &cleanup_preload;
4672 return 0;
4673}
4674
7ff120b4
YQ
4675/* Preload instructions with register offset. */
4676
4677static void
4678install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
4679 struct displaced_step_closure *dsc, unsigned int rn,
4680 unsigned int rm)
4681{
4682 ULONGEST rn_val, rm_val;
4683
cca44b1b
JB
4684 /* Preload register-offset instructions:
4685
4686 {pli/pld} [rn, rm {, shift}]
4687 ->
4688 {pli/pld} [r0, r1 {, shift}]. */
4689
36073a92
YQ
4690 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
4691 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
4692 rn_val = displaced_read_reg (regs, dsc, rn);
4693 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
4694 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
4695 displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
cca44b1b
JB
4696 dsc->u.preload.immed = 0;
4697
cca44b1b 4698 dsc->cleanup = &cleanup_preload;
7ff120b4
YQ
4699}
4700
4701static int
4702arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
4703 struct regcache *regs,
4704 struct displaced_step_closure *dsc)
4705{
4706 unsigned int rn = bits (insn, 16, 19);
4707 unsigned int rm = bits (insn, 0, 3);
4708
4709
4710 if (!insn_references_pc (insn, 0x000f000ful))
4711 return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);
4712
4713 if (debug_displaced)
4714 fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
4715 (unsigned long) insn);
4716
4717 dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
cca44b1b 4718
7ff120b4 4719 install_preload_reg (gdbarch, regs, dsc, rn, rm);
cca44b1b
JB
4720 return 0;
4721}
4722
4723/* Copy/cleanup coprocessor load and store instructions. */
4724
4725static void
6e39997a 4726cleanup_copro_load_store (struct gdbarch *gdbarch,
cca44b1b
JB
4727 struct regcache *regs,
4728 struct displaced_step_closure *dsc)
4729{
36073a92 4730 ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
4731
4732 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
4733
4734 if (dsc->u.ldst.writeback)
4735 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
4736}
4737
7ff120b4
YQ
4738static void
4739install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
4740 struct displaced_step_closure *dsc,
4741 int writeback, unsigned int rn)
cca44b1b 4742{
cca44b1b 4743 ULONGEST rn_val;
cca44b1b 4744
cca44b1b
JB
4745 /* Coprocessor load/store instructions:
4746
4747 {stc/stc2} [<Rn>, #+/-imm] (and other immediate addressing modes)
4748 ->
4749 {stc/stc2} [r0, #+/-imm].
4750
4751 ldc/ldc2 are handled identically. */
4752
36073a92
YQ
4753 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
4754 rn_val = displaced_read_reg (regs, dsc, rn);
2b16b2e3
YQ
4755 /* PC should be 4-byte aligned. */
4756 rn_val = rn_val & 0xfffffffc;
cca44b1b
JB
4757 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
4758
7ff120b4 4759 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
4760 dsc->u.ldst.rn = rn;
4761
7ff120b4
YQ
4762 dsc->cleanup = &cleanup_copro_load_store;
4763}
4764
4765static int
4766arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
4767 struct regcache *regs,
4768 struct displaced_step_closure *dsc)
4769{
4770 unsigned int rn = bits (insn, 16, 19);
4771
4772 if (!insn_references_pc (insn, 0x000f0000ul))
4773 return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);
4774
4775 if (debug_displaced)
4776 fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
4777 "load/store insn %.8lx\n", (unsigned long) insn);
4778
cca44b1b
JB
4779 dsc->modinsn[0] = insn & 0xfff0ffff;
4780
7ff120b4 4781 install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);
cca44b1b
JB
4782
4783 return 0;
4784}
4785
34518530
YQ
4786static int
4787thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
4788 uint16_t insn2, struct regcache *regs,
4789 struct displaced_step_closure *dsc)
4790{
4791 unsigned int rn = bits (insn1, 0, 3);
4792
4793 if (rn != ARM_PC_REGNUM)
4794 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
4795 "copro load/store", dsc);
4796
4797 if (debug_displaced)
4798 fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
4799 "load/store insn %.4x%.4x\n", insn1, insn2);
4800
4801 dsc->modinsn[0] = insn1 & 0xfff0;
4802 dsc->modinsn[1] = insn2;
4803 dsc->numinsns = 2;
4804
4805 /* This function is called for copying instruction LDC/LDC2/VLDR, which
4806 doesn't support writeback, so pass 0. */
4807 install_copro_load_store (gdbarch, regs, dsc, 0, rn);
4808
4809 return 0;
4810}
4811
cca44b1b
JB
4812/* Clean up branch instructions (actually perform the branch, by setting
4813 PC). */
4814
4815static void
6e39997a 4816cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
4817 struct displaced_step_closure *dsc)
4818{
36073a92 4819 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
4820 int branch_taken = condition_true (dsc->u.branch.cond, status);
4821 enum pc_write_style write_pc = dsc->u.branch.exchange
4822 ? BX_WRITE_PC : BRANCH_WRITE_PC;
4823
4824 if (!branch_taken)
4825 return;
4826
4827 if (dsc->u.branch.link)
4828 {
8c8dba6d
YQ
4829 /* The value of LR should be the next insn of current one. In order
4830 not to confuse logic hanlding later insn `bx lr', if current insn mode
4831 is Thumb, the bit 0 of LR value should be set to 1. */
4832 ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;
4833
4834 if (dsc->is_thumb)
4835 next_insn_addr |= 0x1;
4836
4837 displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
4838 CANNOT_WRITE_PC);
cca44b1b
JB
4839 }
4840
bf9f652a 4841 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
cca44b1b
JB
4842}
4843
4844/* Copy B/BL/BLX instructions with immediate destinations. */
4845
7ff120b4
YQ
4846static void
4847install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
4848 struct displaced_step_closure *dsc,
4849 unsigned int cond, int exchange, int link, long offset)
4850{
4851 /* Implement "BL<cond> <label>" as:
4852
4853 Preparation: cond <- instruction condition
4854 Insn: mov r0, r0 (nop)
4855 Cleanup: if (condition true) { r14 <- pc; pc <- label }.
4856
4857 B<cond> similar, but don't set r14 in cleanup. */
4858
4859 dsc->u.branch.cond = cond;
4860 dsc->u.branch.link = link;
4861 dsc->u.branch.exchange = exchange;
4862
2b16b2e3
YQ
4863 dsc->u.branch.dest = dsc->insn_addr;
4864 if (link && exchange)
4865 /* For BLX, offset is computed from the Align (PC, 4). */
4866 dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;
4867
7ff120b4 4868 if (dsc->is_thumb)
2b16b2e3 4869 dsc->u.branch.dest += 4 + offset;
7ff120b4 4870 else
2b16b2e3 4871 dsc->u.branch.dest += 8 + offset;
7ff120b4
YQ
4872
4873 dsc->cleanup = &cleanup_branch;
4874}
cca44b1b 4875static int
7ff120b4
YQ
4876arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
4877 struct regcache *regs, struct displaced_step_closure *dsc)
cca44b1b
JB
4878{
4879 unsigned int cond = bits (insn, 28, 31);
4880 int exchange = (cond == 0xf);
4881 int link = exchange || bit (insn, 24);
cca44b1b
JB
4882 long offset;
4883
4884 if (debug_displaced)
4885 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s immediate insn "
4886 "%.8lx\n", (exchange) ? "blx" : (link) ? "bl" : "b",
4887 (unsigned long) insn);
cca44b1b
JB
4888 if (exchange)
4889 /* For BLX, set bit 0 of the destination. The cleanup_branch function will
4890 then arrange the switch into Thumb mode. */
4891 offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
4892 else
4893 offset = bits (insn, 0, 23) << 2;
4894
4895 if (bit (offset, 25))
4896 offset = offset | ~0x3ffffff;
4897
cca44b1b
JB
4898 dsc->modinsn[0] = ARM_NOP;
4899
7ff120b4 4900 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
cca44b1b
JB
4901 return 0;
4902}
4903
34518530
YQ
4904static int
4905thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
4906 uint16_t insn2, struct regcache *regs,
4907 struct displaced_step_closure *dsc)
4908{
4909 int link = bit (insn2, 14);
4910 int exchange = link && !bit (insn2, 12);
4911 int cond = INST_AL;
4912 long offset = 0;
4913 int j1 = bit (insn2, 13);
4914 int j2 = bit (insn2, 11);
4915 int s = sbits (insn1, 10, 10);
4916 int i1 = !(j1 ^ bit (insn1, 10));
4917 int i2 = !(j2 ^ bit (insn1, 10));
4918
4919 if (!link && !exchange) /* B */
4920 {
4921 offset = (bits (insn2, 0, 10) << 1);
4922 if (bit (insn2, 12)) /* Encoding T4 */
4923 {
4924 offset |= (bits (insn1, 0, 9) << 12)
4925 | (i2 << 22)
4926 | (i1 << 23)
4927 | (s << 24);
4928 cond = INST_AL;
4929 }
4930 else /* Encoding T3 */
4931 {
4932 offset |= (bits (insn1, 0, 5) << 12)
4933 | (j1 << 18)
4934 | (j2 << 19)
4935 | (s << 20);
4936 cond = bits (insn1, 6, 9);
4937 }
4938 }
4939 else
4940 {
4941 offset = (bits (insn1, 0, 9) << 12);
4942 offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
4943 offset |= exchange ?
4944 (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
4945 }
4946
4947 if (debug_displaced)
4948 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s insn "
4949 "%.4x %.4x with offset %.8lx\n",
4950 link ? (exchange) ? "blx" : "bl" : "b",
4951 insn1, insn2, offset);
4952
4953 dsc->modinsn[0] = THUMB_NOP;
4954
4955 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
4956 return 0;
4957}
4958
4959/* Copy B Thumb instructions. */
4960static int
615234c1 4961thumb_copy_b (struct gdbarch *gdbarch, uint16_t insn,
34518530
YQ
4962 struct displaced_step_closure *dsc)
4963{
4964 unsigned int cond = 0;
4965 int offset = 0;
4966 unsigned short bit_12_15 = bits (insn, 12, 15);
4967 CORE_ADDR from = dsc->insn_addr;
4968
4969 if (bit_12_15 == 0xd)
4970 {
4971 /* offset = SignExtend (imm8:0, 32) */
4972 offset = sbits ((insn << 1), 0, 8);
4973 cond = bits (insn, 8, 11);
4974 }
4975 else if (bit_12_15 == 0xe) /* Encoding T2 */
4976 {
4977 offset = sbits ((insn << 1), 0, 11);
4978 cond = INST_AL;
4979 }
4980
4981 if (debug_displaced)
4982 fprintf_unfiltered (gdb_stdlog,
4983 "displaced: copying b immediate insn %.4x "
4984 "with offset %d\n", insn, offset);
4985
4986 dsc->u.branch.cond = cond;
4987 dsc->u.branch.link = 0;
4988 dsc->u.branch.exchange = 0;
4989 dsc->u.branch.dest = from + 4 + offset;
4990
4991 dsc->modinsn[0] = THUMB_NOP;
4992
4993 dsc->cleanup = &cleanup_branch;
4994
4995 return 0;
4996}
4997
cca44b1b
JB
4998/* Copy BX/BLX with register-specified destinations. */
4999
7ff120b4
YQ
5000static void
5001install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
5002 struct displaced_step_closure *dsc, int link,
5003 unsigned int cond, unsigned int rm)
cca44b1b 5004{
cca44b1b
JB
5005 /* Implement {BX,BLX}<cond> <reg>" as:
5006
5007 Preparation: cond <- instruction condition
5008 Insn: mov r0, r0 (nop)
5009 Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
5010
5011 Don't set r14 in cleanup for BX. */
5012
36073a92 5013 dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5014
5015 dsc->u.branch.cond = cond;
5016 dsc->u.branch.link = link;
cca44b1b 5017
7ff120b4 5018 dsc->u.branch.exchange = 1;
cca44b1b
JB
5019
5020 dsc->cleanup = &cleanup_branch;
7ff120b4 5021}
cca44b1b 5022
7ff120b4
YQ
5023static int
5024arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
5025 struct regcache *regs, struct displaced_step_closure *dsc)
5026{
5027 unsigned int cond = bits (insn, 28, 31);
5028 /* BX: x12xxx1x
5029 BLX: x12xxx3x. */
5030 int link = bit (insn, 5);
5031 unsigned int rm = bits (insn, 0, 3);
5032
5033 if (debug_displaced)
5034 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx",
5035 (unsigned long) insn);
5036
5037 dsc->modinsn[0] = ARM_NOP;
5038
5039 install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
cca44b1b
JB
5040 return 0;
5041}
5042
34518530
YQ
5043static int
5044thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
5045 struct regcache *regs,
5046 struct displaced_step_closure *dsc)
5047{
5048 int link = bit (insn, 7);
5049 unsigned int rm = bits (insn, 3, 6);
5050
5051 if (debug_displaced)
5052 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x",
5053 (unsigned short) insn);
5054
5055 dsc->modinsn[0] = THUMB_NOP;
5056
5057 install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);
5058
5059 return 0;
5060}
5061
5062
0963b4bd 5063/* Copy/cleanup arithmetic/logic instruction with immediate RHS. */
cca44b1b
JB
5064
5065static void
6e39997a 5066cleanup_alu_imm (struct gdbarch *gdbarch,
cca44b1b
JB
5067 struct regcache *regs, struct displaced_step_closure *dsc)
5068{
36073a92 5069 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5070 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5071 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5072 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
5073}
5074
5075static int
7ff120b4
YQ
5076arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
5077 struct displaced_step_closure *dsc)
cca44b1b
JB
5078{
5079 unsigned int rn = bits (insn, 16, 19);
5080 unsigned int rd = bits (insn, 12, 15);
5081 unsigned int op = bits (insn, 21, 24);
5082 int is_mov = (op == 0xd);
5083 ULONGEST rd_val, rn_val;
cca44b1b
JB
5084
5085 if (!insn_references_pc (insn, 0x000ff000ul))
7ff120b4 5086 return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
cca44b1b
JB
5087
5088 if (debug_displaced)
5089 fprintf_unfiltered (gdb_stdlog, "displaced: copying immediate %s insn "
5090 "%.8lx\n", is_mov ? "move" : "ALU",
5091 (unsigned long) insn);
5092
5093 /* Instruction is of form:
5094
5095 <op><cond> rd, [rn,] #imm
5096
5097 Rewrite as:
5098
5099 Preparation: tmp1, tmp2 <- r0, r1;
5100 r0, r1 <- rd, rn
5101 Insn: <op><cond> r0, r1, #imm
5102 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
5103 */
5104
36073a92
YQ
5105 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5106 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5107 rn_val = displaced_read_reg (regs, dsc, rn);
5108 rd_val = displaced_read_reg (regs, dsc, rd);
cca44b1b
JB
5109 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5110 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5111 dsc->rd = rd;
5112
5113 if (is_mov)
5114 dsc->modinsn[0] = insn & 0xfff00fff;
5115 else
5116 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
5117
5118 dsc->cleanup = &cleanup_alu_imm;
5119
5120 return 0;
5121}
5122
34518530
YQ
5123static int
5124thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
5125 uint16_t insn2, struct regcache *regs,
5126 struct displaced_step_closure *dsc)
5127{
5128 unsigned int op = bits (insn1, 5, 8);
5129 unsigned int rn, rm, rd;
5130 ULONGEST rd_val, rn_val;
5131
5132 rn = bits (insn1, 0, 3); /* Rn */
5133 rm = bits (insn2, 0, 3); /* Rm */
5134 rd = bits (insn2, 8, 11); /* Rd */
5135
5136 /* This routine is only called for instruction MOV. */
5137 gdb_assert (op == 0x2 && rn == 0xf);
5138
5139 if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
5140 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);
5141
5142 if (debug_displaced)
5143 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.4x%.4x\n",
5144 "ALU", insn1, insn2);
5145
5146 /* Instruction is of form:
5147
5148 <op><cond> rd, [rn,] #imm
5149
5150 Rewrite as:
5151
5152 Preparation: tmp1, tmp2 <- r0, r1;
5153 r0, r1 <- rd, rn
5154 Insn: <op><cond> r0, r1, #imm
5155 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
5156 */
5157
5158 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5159 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5160 rn_val = displaced_read_reg (regs, dsc, rn);
5161 rd_val = displaced_read_reg (regs, dsc, rd);
5162 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5163 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5164 dsc->rd = rd;
5165
5166 dsc->modinsn[0] = insn1;
5167 dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
5168 dsc->numinsns = 2;
5169
5170 dsc->cleanup = &cleanup_alu_imm;
5171
5172 return 0;
5173}
5174
cca44b1b
JB
5175/* Copy/cleanup arithmetic/logic insns with register RHS. */
5176
5177static void
6e39997a 5178cleanup_alu_reg (struct gdbarch *gdbarch,
cca44b1b
JB
5179 struct regcache *regs, struct displaced_step_closure *dsc)
5180{
5181 ULONGEST rd_val;
5182 int i;
5183
36073a92 5184 rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5185
5186 for (i = 0; i < 3; i++)
5187 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
5188
5189 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
5190}
5191
7ff120b4
YQ
5192static void
5193install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
5194 struct displaced_step_closure *dsc,
5195 unsigned int rd, unsigned int rn, unsigned int rm)
cca44b1b 5196{
cca44b1b 5197 ULONGEST rd_val, rn_val, rm_val;
cca44b1b 5198
cca44b1b
JB
5199 /* Instruction is of form:
5200
5201 <op><cond> rd, [rn,] rm [, <shift>]
5202
5203 Rewrite as:
5204
5205 Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
5206 r0, r1, r2 <- rd, rn, rm
ef713951 5207 Insn: <op><cond> r0, [r1,] r2 [, <shift>]
cca44b1b
JB
5208 Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
5209 */
5210
36073a92
YQ
5211 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5212 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5213 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
5214 rd_val = displaced_read_reg (regs, dsc, rd);
5215 rn_val = displaced_read_reg (regs, dsc, rn);
5216 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5217 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5218 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5219 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
5220 dsc->rd = rd;
5221
7ff120b4
YQ
5222 dsc->cleanup = &cleanup_alu_reg;
5223}
5224
5225static int
5226arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
5227 struct displaced_step_closure *dsc)
5228{
5229 unsigned int op = bits (insn, 21, 24);
5230 int is_mov = (op == 0xd);
5231
5232 if (!insn_references_pc (insn, 0x000ff00ful))
5233 return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);
5234
5235 if (debug_displaced)
5236 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.8lx\n",
5237 is_mov ? "move" : "ALU", (unsigned long) insn);
5238
cca44b1b
JB
5239 if (is_mov)
5240 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
5241 else
5242 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
5243
7ff120b4
YQ
5244 install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
5245 bits (insn, 0, 3));
cca44b1b
JB
5246 return 0;
5247}
5248
34518530
YQ
5249static int
5250thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
5251 struct regcache *regs,
5252 struct displaced_step_closure *dsc)
5253{
ef713951 5254 unsigned rm, rd;
34518530 5255
ef713951
YQ
5256 rm = bits (insn, 3, 6);
5257 rd = (bit (insn, 7) << 3) | bits (insn, 0, 2);
34518530 5258
ef713951 5259 if (rd != ARM_PC_REGNUM && rm != ARM_PC_REGNUM)
34518530
YQ
5260 return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);
5261
5262 if (debug_displaced)
ef713951
YQ
5263 fprintf_unfiltered (gdb_stdlog, "displaced: copying ALU reg insn %.4x\n",
5264 (unsigned short) insn);
34518530 5265
ef713951 5266 dsc->modinsn[0] = ((insn & 0xff00) | 0x10);
34518530 5267
ef713951 5268 install_alu_reg (gdbarch, regs, dsc, rd, rd, rm);
34518530
YQ
5269
5270 return 0;
5271}
5272
cca44b1b
JB
5273/* Cleanup/copy arithmetic/logic insns with shifted register RHS. */
5274
5275static void
6e39997a 5276cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
cca44b1b
JB
5277 struct regcache *regs,
5278 struct displaced_step_closure *dsc)
5279{
36073a92 5280 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5281 int i;
5282
5283 for (i = 0; i < 4; i++)
5284 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
5285
5286 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
5287}
5288
7ff120b4
YQ
5289static void
5290install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
5291 struct displaced_step_closure *dsc,
5292 unsigned int rd, unsigned int rn, unsigned int rm,
5293 unsigned rs)
cca44b1b 5294{
7ff120b4 5295 int i;
cca44b1b 5296 ULONGEST rd_val, rn_val, rm_val, rs_val;
cca44b1b 5297
cca44b1b
JB
5298 /* Instruction is of form:
5299
5300 <op><cond> rd, [rn,] rm, <shift> rs
5301
5302 Rewrite as:
5303
5304 Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
5305 r0, r1, r2, r3 <- rd, rn, rm, rs
5306 Insn: <op><cond> r0, r1, r2, <shift> r3
5307 Cleanup: tmp5 <- r0
5308 r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
5309 rd <- tmp5
5310 */
5311
5312 for (i = 0; i < 4; i++)
36073a92 5313 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b 5314
36073a92
YQ
5315 rd_val = displaced_read_reg (regs, dsc, rd);
5316 rn_val = displaced_read_reg (regs, dsc, rn);
5317 rm_val = displaced_read_reg (regs, dsc, rm);
5318 rs_val = displaced_read_reg (regs, dsc, rs);
cca44b1b
JB
5319 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5320 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5321 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
5322 displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
5323 dsc->rd = rd;
7ff120b4
YQ
5324 dsc->cleanup = &cleanup_alu_shifted_reg;
5325}
5326
5327static int
5328arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
5329 struct regcache *regs,
5330 struct displaced_step_closure *dsc)
5331{
5332 unsigned int op = bits (insn, 21, 24);
5333 int is_mov = (op == 0xd);
5334 unsigned int rd, rn, rm, rs;
5335
5336 if (!insn_references_pc (insn, 0x000fff0ful))
5337 return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
5338
5339 if (debug_displaced)
5340 fprintf_unfiltered (gdb_stdlog, "displaced: copying shifted reg %s insn "
5341 "%.8lx\n", is_mov ? "move" : "ALU",
5342 (unsigned long) insn);
5343
5344 rn = bits (insn, 16, 19);
5345 rm = bits (insn, 0, 3);
5346 rs = bits (insn, 8, 11);
5347 rd = bits (insn, 12, 15);
cca44b1b
JB
5348
5349 if (is_mov)
5350 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
5351 else
5352 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
5353
7ff120b4 5354 install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);
cca44b1b
JB
5355
5356 return 0;
5357}
5358
5359/* Clean up load instructions. */
5360
5361static void
6e39997a 5362cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
5363 struct displaced_step_closure *dsc)
5364{
5365 ULONGEST rt_val, rt_val2 = 0, rn_val;
cca44b1b 5366
36073a92 5367 rt_val = displaced_read_reg (regs, dsc, 0);
cca44b1b 5368 if (dsc->u.ldst.xfersize == 8)
36073a92
YQ
5369 rt_val2 = displaced_read_reg (regs, dsc, 1);
5370 rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
5371
5372 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5373 if (dsc->u.ldst.xfersize > 4)
5374 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5375 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
5376 if (!dsc->u.ldst.immed)
5377 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
5378
5379 /* Handle register writeback. */
5380 if (dsc->u.ldst.writeback)
5381 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
5382 /* Put result in right place. */
5383 displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
5384 if (dsc->u.ldst.xfersize == 8)
5385 displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
5386}
5387
5388/* Clean up store instructions. */
5389
5390static void
6e39997a 5391cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
5392 struct displaced_step_closure *dsc)
5393{
36073a92 5394 ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
5395
5396 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5397 if (dsc->u.ldst.xfersize > 4)
5398 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5399 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
5400 if (!dsc->u.ldst.immed)
5401 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
5402 if (!dsc->u.ldst.restore_r4)
5403 displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
5404
5405 /* Writeback. */
5406 if (dsc->u.ldst.writeback)
5407 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
5408}
5409
5410/* Copy "extra" load/store instructions. These are halfword/doubleword
5411 transfers, which have a different encoding to byte/word transfers. */
5412
5413static int
550dc4e2 5414arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unprivileged,
7ff120b4 5415 struct regcache *regs, struct displaced_step_closure *dsc)
cca44b1b
JB
5416{
5417 unsigned int op1 = bits (insn, 20, 24);
5418 unsigned int op2 = bits (insn, 5, 6);
5419 unsigned int rt = bits (insn, 12, 15);
5420 unsigned int rn = bits (insn, 16, 19);
5421 unsigned int rm = bits (insn, 0, 3);
5422 char load[12] = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
5423 char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
5424 int immed = (op1 & 0x4) != 0;
5425 int opcode;
5426 ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
cca44b1b
JB
5427
5428 if (!insn_references_pc (insn, 0x000ff00ful))
7ff120b4 5429 return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);
cca44b1b
JB
5430
5431 if (debug_displaced)
5432 fprintf_unfiltered (gdb_stdlog, "displaced: copying %sextra load/store "
550dc4e2 5433 "insn %.8lx\n", unprivileged ? "unprivileged " : "",
cca44b1b
JB
5434 (unsigned long) insn);
5435
5436 opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
5437
5438 if (opcode < 0)
5439 internal_error (__FILE__, __LINE__,
5440 _("copy_extra_ld_st: instruction decode error"));
5441
36073a92
YQ
5442 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5443 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5444 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 5445 if (!immed)
36073a92 5446 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 5447
36073a92 5448 rt_val = displaced_read_reg (regs, dsc, rt);
cca44b1b 5449 if (bytesize[opcode] == 8)
36073a92
YQ
5450 rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
5451 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 5452 if (!immed)
36073a92 5453 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5454
5455 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
5456 if (bytesize[opcode] == 8)
5457 displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
5458 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
5459 if (!immed)
5460 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
5461
5462 dsc->rd = rt;
5463 dsc->u.ldst.xfersize = bytesize[opcode];
5464 dsc->u.ldst.rn = rn;
5465 dsc->u.ldst.immed = immed;
5466 dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
5467 dsc->u.ldst.restore_r4 = 0;
5468
5469 if (immed)
5470 /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
5471 ->
5472 {ldr,str}<width><cond> r0, [r1,] [r2, #imm]. */
5473 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
5474 else
5475 /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
5476 ->
5477 {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3]. */
5478 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
5479
5480 dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
5481
5482 return 0;
5483}
5484
0f6f04ba 5485/* Copy byte/half word/word loads and stores. */
cca44b1b 5486
7ff120b4 5487static void
0f6f04ba
YQ
5488install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
5489 struct displaced_step_closure *dsc, int load,
5490 int immed, int writeback, int size, int usermode,
5491 int rt, int rm, int rn)
cca44b1b 5492{
cca44b1b 5493 ULONGEST rt_val, rn_val, rm_val = 0;
cca44b1b 5494
36073a92
YQ
5495 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5496 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 5497 if (!immed)
36073a92 5498 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 5499 if (!load)
36073a92 5500 dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);
cca44b1b 5501
36073a92
YQ
5502 rt_val = displaced_read_reg (regs, dsc, rt);
5503 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 5504 if (!immed)
36073a92 5505 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5506
5507 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
5508 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
5509 if (!immed)
5510 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
cca44b1b 5511 dsc->rd = rt;
0f6f04ba 5512 dsc->u.ldst.xfersize = size;
cca44b1b
JB
5513 dsc->u.ldst.rn = rn;
5514 dsc->u.ldst.immed = immed;
7ff120b4 5515 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
5516
5517 /* To write PC we can do:
5518
494e194e
YQ
5519 Before this sequence of instructions:
5520 r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
5521 r2 is the Rn value got from dispalced_read_reg.
5522
5523 Insn1: push {pc} Write address of STR instruction + offset on stack
5524 Insn2: pop {r4} Read it back from stack, r4 = addr(Insn1) + offset
5525 Insn3: sub r4, r4, pc r4 = addr(Insn1) + offset - pc
5526 = addr(Insn1) + offset - addr(Insn3) - 8
5527 = offset - 16
5528 Insn4: add r4, r4, #8 r4 = offset - 8
5529 Insn5: add r0, r0, r4 r0 = from + 8 + offset - 8
5530 = from + offset
5531 Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])
cca44b1b
JB
5532
5533 Otherwise we don't know what value to write for PC, since the offset is
494e194e
YQ
5534 architecture-dependent (sometimes PC+8, sometimes PC+12). More details
5535 of this can be found in Section "Saving from r15" in
5536 http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */
cca44b1b 5537
7ff120b4
YQ
5538 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
5539}
5540
34518530
YQ
5541
5542static int
5543thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
5544 uint16_t insn2, struct regcache *regs,
5545 struct displaced_step_closure *dsc, int size)
5546{
5547 unsigned int u_bit = bit (insn1, 7);
5548 unsigned int rt = bits (insn2, 12, 15);
5549 int imm12 = bits (insn2, 0, 11);
5550 ULONGEST pc_val;
5551
5552 if (debug_displaced)
5553 fprintf_unfiltered (gdb_stdlog,
5554 "displaced: copying ldr pc (0x%x) R%d %c imm12 %.4x\n",
5555 (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
5556 imm12);
5557
5558 if (!u_bit)
5559 imm12 = -1 * imm12;
5560
5561 /* Rewrite instruction LDR Rt imm12 into:
5562
5563 Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12
5564
5565 LDR R0, R2, R3,
5566
5567 Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2]. */
5568
5569
5570 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5571 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
5572 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
5573
5574 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
5575
5576 pc_val = pc_val & 0xfffffffc;
5577
5578 displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
5579 displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);
5580
5581 dsc->rd = rt;
5582
5583 dsc->u.ldst.xfersize = size;
5584 dsc->u.ldst.immed = 0;
5585 dsc->u.ldst.writeback = 0;
5586 dsc->u.ldst.restore_r4 = 0;
5587
5588 /* LDR R0, R2, R3 */
5589 dsc->modinsn[0] = 0xf852;
5590 dsc->modinsn[1] = 0x3;
5591 dsc->numinsns = 2;
5592
5593 dsc->cleanup = &cleanup_load;
5594
5595 return 0;
5596}
5597
5598static int
5599thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
5600 uint16_t insn2, struct regcache *regs,
5601 struct displaced_step_closure *dsc,
5602 int writeback, int immed)
5603{
5604 unsigned int rt = bits (insn2, 12, 15);
5605 unsigned int rn = bits (insn1, 0, 3);
5606 unsigned int rm = bits (insn2, 0, 3); /* Only valid if !immed. */
5607 /* In LDR (register), there is also a register Rm, which is not allowed to
5608 be PC, so we don't have to check it. */
5609
5610 if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
5611 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
5612 dsc);
5613
5614 if (debug_displaced)
5615 fprintf_unfiltered (gdb_stdlog,
5616 "displaced: copying ldr r%d [r%d] insn %.4x%.4x\n",
5617 rt, rn, insn1, insn2);
5618
5619 install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
5620 0, rt, rm, rn);
5621
5622 dsc->u.ldst.restore_r4 = 0;
5623
5624 if (immed)
5625 /* ldr[b]<cond> rt, [rn, #imm], etc.
5626 ->
5627 ldr[b]<cond> r0, [r2, #imm]. */
5628 {
5629 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
5630 dsc->modinsn[1] = insn2 & 0x0fff;
5631 }
5632 else
5633 /* ldr[b]<cond> rt, [rn, rm], etc.
5634 ->
5635 ldr[b]<cond> r0, [r2, r3]. */
5636 {
5637 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
5638 dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
5639 }
5640
5641 dsc->numinsns = 2;
5642
5643 return 0;
5644}
5645
5646
7ff120b4
YQ
5647static int
5648arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
5649 struct regcache *regs,
5650 struct displaced_step_closure *dsc,
0f6f04ba 5651 int load, int size, int usermode)
7ff120b4
YQ
5652{
5653 int immed = !bit (insn, 25);
5654 int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
5655 unsigned int rt = bits (insn, 12, 15);
5656 unsigned int rn = bits (insn, 16, 19);
5657 unsigned int rm = bits (insn, 0, 3); /* Only valid if !immed. */
5658
5659 if (!insn_references_pc (insn, 0x000ff00ful))
5660 return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);
5661
5662 if (debug_displaced)
5663 fprintf_unfiltered (gdb_stdlog,
5664 "displaced: copying %s%s r%d [r%d] insn %.8lx\n",
0f6f04ba
YQ
5665 load ? (size == 1 ? "ldrb" : "ldr")
5666 : (size == 1 ? "strb" : "str"), usermode ? "t" : "",
7ff120b4
YQ
5667 rt, rn,
5668 (unsigned long) insn);
5669
0f6f04ba
YQ
5670 install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
5671 usermode, rt, rm, rn);
7ff120b4 5672
bf9f652a 5673 if (load || rt != ARM_PC_REGNUM)
cca44b1b
JB
5674 {
5675 dsc->u.ldst.restore_r4 = 0;
5676
5677 if (immed)
5678 /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
5679 ->
5680 {ldr,str}[b]<cond> r0, [r2, #imm]. */
5681 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
5682 else
5683 /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
5684 ->
5685 {ldr,str}[b]<cond> r0, [r2, r3]. */
5686 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
5687 }
5688 else
5689 {
5690 /* We need to use r4 as scratch. Make sure it's restored afterwards. */
5691 dsc->u.ldst.restore_r4 = 1;
494e194e
YQ
5692 dsc->modinsn[0] = 0xe92d8000; /* push {pc} */
5693 dsc->modinsn[1] = 0xe8bd0010; /* pop {r4} */
cca44b1b
JB
5694 dsc->modinsn[2] = 0xe044400f; /* sub r4, r4, pc. */
5695 dsc->modinsn[3] = 0xe2844008; /* add r4, r4, #8. */
5696 dsc->modinsn[4] = 0xe0800004; /* add r0, r0, r4. */
5697
5698 /* As above. */
5699 if (immed)
5700 dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
5701 else
5702 dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
5703
cca44b1b
JB
5704 dsc->numinsns = 6;
5705 }
5706
5707 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
5708
5709 return 0;
5710}
5711
5712/* Cleanup LDM instructions with fully-populated register list. This is an
5713 unfortunate corner case: it's impossible to implement correctly by modifying
5714 the instruction. The issue is as follows: we have an instruction,
5715
5716 ldm rN, {r0-r15}
5717
5718 which we must rewrite to avoid loading PC. A possible solution would be to
5719 do the load in two halves, something like (with suitable cleanup
5720 afterwards):
5721
5722 mov r8, rN
5723 ldm[id][ab] r8!, {r0-r7}
5724 str r7, <temp>
5725 ldm[id][ab] r8, {r7-r14}
5726 <bkpt>
5727
5728 but at present there's no suitable place for <temp>, since the scratch space
5729 is overwritten before the cleanup routine is called. For now, we simply
5730 emulate the instruction. */
5731
5732static void
5733cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
5734 struct displaced_step_closure *dsc)
5735{
cca44b1b
JB
5736 int inc = dsc->u.block.increment;
5737 int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
5738 int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
5739 uint32_t regmask = dsc->u.block.regmask;
5740 int regno = inc ? 0 : 15;
5741 CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
5742 int exception_return = dsc->u.block.load && dsc->u.block.user
5743 && (regmask & 0x8000) != 0;
36073a92 5744 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
5745 int do_transfer = condition_true (dsc->u.block.cond, status);
5746 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5747
5748 if (!do_transfer)
5749 return;
5750
5751 /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
5752 sensible we can do here. Complain loudly. */
5753 if (exception_return)
5754 error (_("Cannot single-step exception return"));
5755
5756 /* We don't handle any stores here for now. */
5757 gdb_assert (dsc->u.block.load != 0);
5758
5759 if (debug_displaced)
5760 fprintf_unfiltered (gdb_stdlog, "displaced: emulating block transfer: "
5761 "%s %s %s\n", dsc->u.block.load ? "ldm" : "stm",
5762 dsc->u.block.increment ? "inc" : "dec",
5763 dsc->u.block.before ? "before" : "after");
5764
5765 while (regmask)
5766 {
5767 uint32_t memword;
5768
5769 if (inc)
bf9f652a 5770 while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
cca44b1b
JB
5771 regno++;
5772 else
5773 while (regno >= 0 && (regmask & (1 << regno)) == 0)
5774 regno--;
5775
5776 xfer_addr += bump_before;
5777
5778 memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
5779 displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
5780
5781 xfer_addr += bump_after;
5782
5783 regmask &= ~(1 << regno);
5784 }
5785
5786 if (dsc->u.block.writeback)
5787 displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
5788 CANNOT_WRITE_PC);
5789}
5790
5791/* Clean up an STM which included the PC in the register list. */
5792
5793static void
5794cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
5795 struct displaced_step_closure *dsc)
5796{
36073a92 5797 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
5798 int store_executed = condition_true (dsc->u.block.cond, status);
5799 CORE_ADDR pc_stored_at, transferred_regs = bitcount (dsc->u.block.regmask);
5800 CORE_ADDR stm_insn_addr;
5801 uint32_t pc_val;
5802 long offset;
5803 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5804
5805 /* If condition code fails, there's nothing else to do. */
5806 if (!store_executed)
5807 return;
5808
5809 if (dsc->u.block.increment)
5810 {
5811 pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
5812
5813 if (dsc->u.block.before)
5814 pc_stored_at += 4;
5815 }
5816 else
5817 {
5818 pc_stored_at = dsc->u.block.xfer_addr;
5819
5820 if (dsc->u.block.before)
5821 pc_stored_at -= 4;
5822 }
5823
5824 pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
5825 stm_insn_addr = dsc->scratch_base;
5826 offset = pc_val - stm_insn_addr;
5827
5828 if (debug_displaced)
5829 fprintf_unfiltered (gdb_stdlog, "displaced: detected PC offset %.8lx for "
5830 "STM instruction\n", offset);
5831
5832 /* Rewrite the stored PC to the proper value for the non-displaced original
5833 instruction. */
5834 write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
5835 dsc->insn_addr + offset);
5836}
5837
5838/* Clean up an LDM which includes the PC in the register list. We clumped all
5839 the registers in the transferred list into a contiguous range r0...rX (to
5840 avoid loading PC directly and losing control of the debugged program), so we
5841 must undo that here. */
5842
5843static void
6e39997a 5844cleanup_block_load_pc (struct gdbarch *gdbarch,
cca44b1b
JB
5845 struct regcache *regs,
5846 struct displaced_step_closure *dsc)
5847{
36073a92 5848 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
22e048c9 5849 int load_executed = condition_true (dsc->u.block.cond, status);
bf9f652a 5850 unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
cca44b1b
JB
5851 unsigned int regs_loaded = bitcount (mask);
5852 unsigned int num_to_shuffle = regs_loaded, clobbered;
5853
5854 /* The method employed here will fail if the register list is fully populated
5855 (we need to avoid loading PC directly). */
5856 gdb_assert (num_to_shuffle < 16);
5857
5858 if (!load_executed)
5859 return;
5860
5861 clobbered = (1 << num_to_shuffle) - 1;
5862
5863 while (num_to_shuffle > 0)
5864 {
5865 if ((mask & (1 << write_reg)) != 0)
5866 {
5867 unsigned int read_reg = num_to_shuffle - 1;
5868
5869 if (read_reg != write_reg)
5870 {
36073a92 5871 ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
cca44b1b
JB
5872 displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
5873 if (debug_displaced)
5874 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: move "
5875 "loaded register r%d to r%d\n"), read_reg,
5876 write_reg);
5877 }
5878 else if (debug_displaced)
5879 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: register "
5880 "r%d already in the right place\n"),
5881 write_reg);
5882
5883 clobbered &= ~(1 << write_reg);
5884
5885 num_to_shuffle--;
5886 }
5887
5888 write_reg--;
5889 }
5890
5891 /* Restore any registers we scribbled over. */
5892 for (write_reg = 0; clobbered != 0; write_reg++)
5893 {
5894 if ((clobbered & (1 << write_reg)) != 0)
5895 {
5896 displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
5897 CANNOT_WRITE_PC);
5898 if (debug_displaced)
5899 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: restored "
5900 "clobbered register r%d\n"), write_reg);
5901 clobbered &= ~(1 << write_reg);
5902 }
5903 }
5904
5905 /* Perform register writeback manually. */
5906 if (dsc->u.block.writeback)
5907 {
5908 ULONGEST new_rn_val = dsc->u.block.xfer_addr;
5909
5910 if (dsc->u.block.increment)
5911 new_rn_val += regs_loaded * 4;
5912 else
5913 new_rn_val -= regs_loaded * 4;
5914
5915 displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
5916 CANNOT_WRITE_PC);
5917 }
5918}
5919
5920/* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
5921 in user-level code (in particular exception return, ldm rn, {...pc}^). */
5922
5923static int
7ff120b4
YQ
5924arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
5925 struct regcache *regs,
5926 struct displaced_step_closure *dsc)
cca44b1b
JB
5927{
5928 int load = bit (insn, 20);
5929 int user = bit (insn, 22);
5930 int increment = bit (insn, 23);
5931 int before = bit (insn, 24);
5932 int writeback = bit (insn, 21);
5933 int rn = bits (insn, 16, 19);
cca44b1b 5934
0963b4bd
MS
5935 /* Block transfers which don't mention PC can be run directly
5936 out-of-line. */
bf9f652a 5937 if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
7ff120b4 5938 return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
cca44b1b 5939
bf9f652a 5940 if (rn == ARM_PC_REGNUM)
cca44b1b 5941 {
0963b4bd
MS
5942 warning (_("displaced: Unpredictable LDM or STM with "
5943 "base register r15"));
7ff120b4 5944 return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
cca44b1b
JB
5945 }
5946
5947 if (debug_displaced)
5948 fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
5949 "%.8lx\n", (unsigned long) insn);
5950
36073a92 5951 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b
JB
5952 dsc->u.block.rn = rn;
5953
5954 dsc->u.block.load = load;
5955 dsc->u.block.user = user;
5956 dsc->u.block.increment = increment;
5957 dsc->u.block.before = before;
5958 dsc->u.block.writeback = writeback;
5959 dsc->u.block.cond = bits (insn, 28, 31);
5960
5961 dsc->u.block.regmask = insn & 0xffff;
5962
5963 if (load)
5964 {
5965 if ((insn & 0xffff) == 0xffff)
5966 {
5967 /* LDM with a fully-populated register list. This case is
5968 particularly tricky. Implement for now by fully emulating the
5969 instruction (which might not behave perfectly in all cases, but
5970 these instructions should be rare enough for that not to matter
5971 too much). */
5972 dsc->modinsn[0] = ARM_NOP;
5973
5974 dsc->cleanup = &cleanup_block_load_all;
5975 }
5976 else
5977 {
5978 /* LDM of a list of registers which includes PC. Implement by
5979 rewriting the list of registers to be transferred into a
5980 contiguous chunk r0...rX before doing the transfer, then shuffling
5981 registers into the correct places in the cleanup routine. */
5982 unsigned int regmask = insn & 0xffff;
bec2ab5a
SM
5983 unsigned int num_in_list = bitcount (regmask), new_regmask;
5984 unsigned int i;
cca44b1b
JB
5985
5986 for (i = 0; i < num_in_list; i++)
36073a92 5987 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b
JB
5988
5989 /* Writeback makes things complicated. We need to avoid clobbering
5990 the base register with one of the registers in our modified
5991 register list, but just using a different register can't work in
5992 all cases, e.g.:
5993
5994 ldm r14!, {r0-r13,pc}
5995
5996 which would need to be rewritten as:
5997
5998 ldm rN!, {r0-r14}
5999
6000 but that can't work, because there's no free register for N.
6001
6002 Solve this by turning off the writeback bit, and emulating
6003 writeback manually in the cleanup routine. */
6004
6005 if (writeback)
6006 insn &= ~(1 << 21);
6007
6008 new_regmask = (1 << num_in_list) - 1;
6009
6010 if (debug_displaced)
6011 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
6012 "{..., pc}: original reg list %.4x, modified "
6013 "list %.4x\n"), rn, writeback ? "!" : "",
6014 (int) insn & 0xffff, new_regmask);
6015
6016 dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
6017
6018 dsc->cleanup = &cleanup_block_load_pc;
6019 }
6020 }
6021 else
6022 {
6023 /* STM of a list of registers which includes PC. Run the instruction
6024 as-is, but out of line: this will store the wrong value for the PC,
6025 so we must manually fix up the memory in the cleanup routine.
6026 Doing things this way has the advantage that we can auto-detect
6027 the offset of the PC write (which is architecture-dependent) in
6028 the cleanup routine. */
6029 dsc->modinsn[0] = insn;
6030
6031 dsc->cleanup = &cleanup_block_store_pc;
6032 }
6033
6034 return 0;
6035}
6036
34518530
YQ
6037static int
6038thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
6039 struct regcache *regs,
6040 struct displaced_step_closure *dsc)
cca44b1b 6041{
34518530
YQ
6042 int rn = bits (insn1, 0, 3);
6043 int load = bit (insn1, 4);
6044 int writeback = bit (insn1, 5);
cca44b1b 6045
34518530
YQ
6046 /* Block transfers which don't mention PC can be run directly
6047 out-of-line. */
6048 if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
6049 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);
7ff120b4 6050
34518530
YQ
6051 if (rn == ARM_PC_REGNUM)
6052 {
6053 warning (_("displaced: Unpredictable LDM or STM with "
6054 "base register r15"));
6055 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6056 "unpredictable ldm/stm", dsc);
6057 }
cca44b1b
JB
6058
6059 if (debug_displaced)
34518530
YQ
6060 fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
6061 "%.4x%.4x\n", insn1, insn2);
cca44b1b 6062
34518530
YQ
6063 /* Clear bit 13, since it should be always zero. */
6064 dsc->u.block.regmask = (insn2 & 0xdfff);
6065 dsc->u.block.rn = rn;
cca44b1b 6066
34518530
YQ
6067 dsc->u.block.load = load;
6068 dsc->u.block.user = 0;
6069 dsc->u.block.increment = bit (insn1, 7);
6070 dsc->u.block.before = bit (insn1, 8);
6071 dsc->u.block.writeback = writeback;
6072 dsc->u.block.cond = INST_AL;
6073 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b 6074
34518530
YQ
6075 if (load)
6076 {
6077 if (dsc->u.block.regmask == 0xffff)
6078 {
6079 /* This branch is impossible to happen. */
6080 gdb_assert (0);
6081 }
6082 else
6083 {
6084 unsigned int regmask = dsc->u.block.regmask;
bec2ab5a
SM
6085 unsigned int num_in_list = bitcount (regmask), new_regmask;
6086 unsigned int i;
34518530
YQ
6087
6088 for (i = 0; i < num_in_list; i++)
6089 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
6090
6091 if (writeback)
6092 insn1 &= ~(1 << 5);
6093
6094 new_regmask = (1 << num_in_list) - 1;
6095
6096 if (debug_displaced)
6097 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
6098 "{..., pc}: original reg list %.4x, modified "
6099 "list %.4x\n"), rn, writeback ? "!" : "",
6100 (int) dsc->u.block.regmask, new_regmask);
6101
6102 dsc->modinsn[0] = insn1;
6103 dsc->modinsn[1] = (new_regmask & 0xffff);
6104 dsc->numinsns = 2;
6105
6106 dsc->cleanup = &cleanup_block_load_pc;
6107 }
6108 }
6109 else
6110 {
6111 dsc->modinsn[0] = insn1;
6112 dsc->modinsn[1] = insn2;
6113 dsc->numinsns = 2;
6114 dsc->cleanup = &cleanup_block_store_pc;
6115 }
6116 return 0;
6117}
6118
d9311bfa
AT
6119/* Wrapper over read_memory_unsigned_integer for use in arm_get_next_pcs.
6120 This is used to avoid a dependency on BFD's bfd_endian enum. */
6121
6122ULONGEST
6123arm_get_next_pcs_read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
6124 int byte_order)
6125{
5f2dfcfd
AT
6126 return read_memory_unsigned_integer (memaddr, len,
6127 (enum bfd_endian) byte_order);
d9311bfa
AT
6128}
6129
6130/* Wrapper over gdbarch_addr_bits_remove for use in arm_get_next_pcs. */
6131
6132CORE_ADDR
6133arm_get_next_pcs_addr_bits_remove (struct arm_get_next_pcs *self,
6134 CORE_ADDR val)
6135{
6136 return gdbarch_addr_bits_remove (get_regcache_arch (self->regcache), val);
6137}
6138
6139/* Wrapper over syscall_next_pc for use in get_next_pcs. */
6140
e7cf25a8
YQ
6141static CORE_ADDR
6142arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self,
6143 CORE_ADDR pc)
d9311bfa 6144{
d9311bfa
AT
6145 return 0;
6146}
6147
6148/* Wrapper over arm_is_thumb for use in arm_get_next_pcs. */
6149
6150int
6151arm_get_next_pcs_is_thumb (struct arm_get_next_pcs *self)
6152{
6153 return arm_is_thumb (self->regcache);
6154}
6155
6156/* single_step() is called just before we want to resume the inferior,
6157 if we want to single-step it but there is no hardware or kernel
6158 single-step support. We find the target of the coming instructions
6159 and breakpoint them. */
6160
6161int
6162arm_software_single_step (struct frame_info *frame)
6163{
6164 struct regcache *regcache = get_current_regcache ();
6165 struct gdbarch *gdbarch = get_regcache_arch (regcache);
6166 struct address_space *aspace = get_regcache_aspace (regcache);
6167 struct arm_get_next_pcs next_pcs_ctx;
6168 CORE_ADDR pc;
6169 int i;
6170 VEC (CORE_ADDR) *next_pcs = NULL;
6171 struct cleanup *old_chain = make_cleanup (VEC_cleanup (CORE_ADDR), &next_pcs);
6172
6173 arm_get_next_pcs_ctor (&next_pcs_ctx,
6174 &arm_get_next_pcs_ops,
6175 gdbarch_byte_order (gdbarch),
6176 gdbarch_byte_order_for_code (gdbarch),
1b451dda 6177 0,
d9311bfa
AT
6178 regcache);
6179
4d18591b 6180 next_pcs = arm_get_next_pcs (&next_pcs_ctx);
d9311bfa
AT
6181
6182 for (i = 0; VEC_iterate (CORE_ADDR, next_pcs, i, pc); i++)
6183 arm_insert_single_step_breakpoint (gdbarch, aspace, pc);
6184
6185 do_cleanups (old_chain);
6186
6187 return 1;
6188}
6189
34518530
YQ
6190/* Cleanup/copy SVC (SWI) instructions. These two functions are overridden
6191 for Linux, where some SVC instructions must be treated specially. */
6192
6193static void
6194cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
6195 struct displaced_step_closure *dsc)
6196{
6197 CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
6198
6199 if (debug_displaced)
6200 fprintf_unfiltered (gdb_stdlog, "displaced: cleanup for svc, resume at "
6201 "%.8lx\n", (unsigned long) resume_addr);
6202
6203 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
6204}
6205
6206
6207/* Common copy routine for svc instruciton. */
6208
6209static int
6210install_svc (struct gdbarch *gdbarch, struct regcache *regs,
6211 struct displaced_step_closure *dsc)
6212{
6213 /* Preparation: none.
6214 Insn: unmodified svc.
6215 Cleanup: pc <- insn_addr + insn_size. */
6216
6217 /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
6218 instruction. */
6219 dsc->wrote_to_pc = 1;
6220
6221 /* Allow OS-specific code to override SVC handling. */
bd18283a
YQ
6222 if (dsc->u.svc.copy_svc_os)
6223 return dsc->u.svc.copy_svc_os (gdbarch, regs, dsc);
6224 else
6225 {
6226 dsc->cleanup = &cleanup_svc;
6227 return 0;
6228 }
34518530
YQ
6229}
6230
6231static int
6232arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
6233 struct regcache *regs, struct displaced_step_closure *dsc)
6234{
6235
6236 if (debug_displaced)
6237 fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.8lx\n",
6238 (unsigned long) insn);
6239
6240 dsc->modinsn[0] = insn;
6241
6242 return install_svc (gdbarch, regs, dsc);
6243}
6244
6245static int
6246thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
6247 struct regcache *regs, struct displaced_step_closure *dsc)
6248{
6249
6250 if (debug_displaced)
6251 fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.4x\n",
6252 insn);
bd18283a 6253
34518530
YQ
6254 dsc->modinsn[0] = insn;
6255
6256 return install_svc (gdbarch, regs, dsc);
cca44b1b
JB
6257}
6258
6259/* Copy undefined instructions. */
6260
6261static int
7ff120b4
YQ
6262arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
6263 struct displaced_step_closure *dsc)
cca44b1b
JB
6264{
6265 if (debug_displaced)
0963b4bd
MS
6266 fprintf_unfiltered (gdb_stdlog,
6267 "displaced: copying undefined insn %.8lx\n",
cca44b1b
JB
6268 (unsigned long) insn);
6269
6270 dsc->modinsn[0] = insn;
6271
6272 return 0;
6273}
6274
34518530
YQ
6275static int
6276thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
6277 struct displaced_step_closure *dsc)
6278{
6279
6280 if (debug_displaced)
6281 fprintf_unfiltered (gdb_stdlog, "displaced: copying undefined insn "
6282 "%.4x %.4x\n", (unsigned short) insn1,
6283 (unsigned short) insn2);
6284
6285 dsc->modinsn[0] = insn1;
6286 dsc->modinsn[1] = insn2;
6287 dsc->numinsns = 2;
6288
6289 return 0;
6290}
6291
cca44b1b
JB
6292/* Copy unpredictable instructions. */
6293
6294static int
7ff120b4
YQ
6295arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
6296 struct displaced_step_closure *dsc)
cca44b1b
JB
6297{
6298 if (debug_displaced)
6299 fprintf_unfiltered (gdb_stdlog, "displaced: copying unpredictable insn "
6300 "%.8lx\n", (unsigned long) insn);
6301
6302 dsc->modinsn[0] = insn;
6303
6304 return 0;
6305}
6306
6307/* The decode_* functions are instruction decoding helpers. They mostly follow
6308 the presentation in the ARM ARM. */
6309
6310static int
7ff120b4
YQ
6311arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
6312 struct regcache *regs,
6313 struct displaced_step_closure *dsc)
cca44b1b
JB
6314{
6315 unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
6316 unsigned int rn = bits (insn, 16, 19);
6317
6318 if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0xe) == 0x0)
7ff120b4 6319 return arm_copy_unmodified (gdbarch, insn, "cps", dsc);
cca44b1b 6320 else if (op1 == 0x10 && op2 == 0x0 && (rn & 0xe) == 0x1)
7ff120b4 6321 return arm_copy_unmodified (gdbarch, insn, "setend", dsc);
cca44b1b 6322 else if ((op1 & 0x60) == 0x20)
7ff120b4 6323 return arm_copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
cca44b1b 6324 else if ((op1 & 0x71) == 0x40)
7ff120b4
YQ
6325 return arm_copy_unmodified (gdbarch, insn, "neon elt/struct load/store",
6326 dsc);
cca44b1b 6327 else if ((op1 & 0x77) == 0x41)
7ff120b4 6328 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 6329 else if ((op1 & 0x77) == 0x45)
7ff120b4 6330 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pli. */
cca44b1b
JB
6331 else if ((op1 & 0x77) == 0x51)
6332 {
6333 if (rn != 0xf)
7ff120b4 6334 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b 6335 else
7ff120b4 6336 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
6337 }
6338 else if ((op1 & 0x77) == 0x55)
7ff120b4 6339 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b
JB
6340 else if (op1 == 0x57)
6341 switch (op2)
6342 {
7ff120b4
YQ
6343 case 0x1: return arm_copy_unmodified (gdbarch, insn, "clrex", dsc);
6344 case 0x4: return arm_copy_unmodified (gdbarch, insn, "dsb", dsc);
6345 case 0x5: return arm_copy_unmodified (gdbarch, insn, "dmb", dsc);
6346 case 0x6: return arm_copy_unmodified (gdbarch, insn, "isb", dsc);
6347 default: return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
6348 }
6349 else if ((op1 & 0x63) == 0x43)
7ff120b4 6350 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
6351 else if ((op2 & 0x1) == 0x0)
6352 switch (op1 & ~0x80)
6353 {
6354 case 0x61:
7ff120b4 6355 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 6356 case 0x65:
7ff120b4 6357 return arm_copy_preload_reg (gdbarch, insn, regs, dsc); /* pli reg. */
cca44b1b
JB
6358 case 0x71: case 0x75:
6359 /* pld/pldw reg. */
7ff120b4 6360 return arm_copy_preload_reg (gdbarch, insn, regs, dsc);
cca44b1b 6361 case 0x63: case 0x67: case 0x73: case 0x77:
7ff120b4 6362 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b 6363 default:
7ff120b4 6364 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6365 }
6366 else
7ff120b4 6367 return arm_copy_undef (gdbarch, insn, dsc); /* Probably unreachable. */
cca44b1b
JB
6368}
6369
6370static int
7ff120b4
YQ
6371arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
6372 struct regcache *regs,
6373 struct displaced_step_closure *dsc)
cca44b1b
JB
6374{
6375 if (bit (insn, 27) == 0)
7ff120b4 6376 return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
cca44b1b
JB
6377 /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx. */
6378 else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
6379 {
6380 case 0x0: case 0x2:
7ff120b4 6381 return arm_copy_unmodified (gdbarch, insn, "srs", dsc);
cca44b1b
JB
6382
6383 case 0x1: case 0x3:
7ff120b4 6384 return arm_copy_unmodified (gdbarch, insn, "rfe", dsc);
cca44b1b
JB
6385
6386 case 0x4: case 0x5: case 0x6: case 0x7:
7ff120b4 6387 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b
JB
6388
6389 case 0x8:
6390 switch ((insn & 0xe00000) >> 21)
6391 {
6392 case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
6393 /* stc/stc2. */
7ff120b4 6394 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
6395
6396 case 0x2:
7ff120b4 6397 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b
JB
6398
6399 default:
7ff120b4 6400 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6401 }
6402
6403 case 0x9:
6404 {
6405 int rn_f = (bits (insn, 16, 19) == 0xf);
6406 switch ((insn & 0xe00000) >> 21)
6407 {
6408 case 0x1: case 0x3:
6409 /* ldc/ldc2 imm (undefined for rn == pc). */
7ff120b4
YQ
6410 return rn_f ? arm_copy_undef (gdbarch, insn, dsc)
6411 : arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
6412
6413 case 0x2:
7ff120b4 6414 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
6415
6416 case 0x4: case 0x5: case 0x6: case 0x7:
6417 /* ldc/ldc2 lit (undefined for rn != pc). */
7ff120b4
YQ
6418 return rn_f ? arm_copy_copro_load_store (gdbarch, insn, regs, dsc)
6419 : arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6420
6421 default:
7ff120b4 6422 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6423 }
6424 }
6425
6426 case 0xa:
7ff120b4 6427 return arm_copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
cca44b1b
JB
6428
6429 case 0xb:
6430 if (bits (insn, 16, 19) == 0xf)
6431 /* ldc/ldc2 lit. */
7ff120b4 6432 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 6433 else
7ff120b4 6434 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6435
6436 case 0xc:
6437 if (bit (insn, 4))
7ff120b4 6438 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 6439 else
7ff120b4 6440 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
6441
6442 case 0xd:
6443 if (bit (insn, 4))
7ff120b4 6444 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 6445 else
7ff120b4 6446 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
6447
6448 default:
7ff120b4 6449 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6450 }
6451}
6452
6453/* Decode miscellaneous instructions in dp/misc encoding space. */
6454
6455static int
7ff120b4
YQ
6456arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
6457 struct regcache *regs,
6458 struct displaced_step_closure *dsc)
cca44b1b
JB
6459{
6460 unsigned int op2 = bits (insn, 4, 6);
6461 unsigned int op = bits (insn, 21, 22);
cca44b1b
JB
6462
6463 switch (op2)
6464 {
6465 case 0x0:
7ff120b4 6466 return arm_copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
cca44b1b
JB
6467
6468 case 0x1:
6469 if (op == 0x1) /* bx. */
7ff120b4 6470 return arm_copy_bx_blx_reg (gdbarch, insn, regs, dsc);
cca44b1b 6471 else if (op == 0x3)
7ff120b4 6472 return arm_copy_unmodified (gdbarch, insn, "clz", dsc);
cca44b1b 6473 else
7ff120b4 6474 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6475
6476 case 0x2:
6477 if (op == 0x1)
6478 /* Not really supported. */
7ff120b4 6479 return arm_copy_unmodified (gdbarch, insn, "bxj", dsc);
cca44b1b 6480 else
7ff120b4 6481 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6482
6483 case 0x3:
6484 if (op == 0x1)
7ff120b4 6485 return arm_copy_bx_blx_reg (gdbarch, insn,
0963b4bd 6486 regs, dsc); /* blx register. */
cca44b1b 6487 else
7ff120b4 6488 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6489
6490 case 0x5:
7ff120b4 6491 return arm_copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
cca44b1b
JB
6492
6493 case 0x7:
6494 if (op == 0x1)
7ff120b4 6495 return arm_copy_unmodified (gdbarch, insn, "bkpt", dsc);
cca44b1b
JB
6496 else if (op == 0x3)
6497 /* Not really supported. */
7ff120b4 6498 return arm_copy_unmodified (gdbarch, insn, "smc", dsc);
cca44b1b
JB
6499
6500 default:
7ff120b4 6501 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6502 }
6503}
6504
6505static int
7ff120b4
YQ
6506arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
6507 struct regcache *regs,
6508 struct displaced_step_closure *dsc)
cca44b1b
JB
6509{
6510 if (bit (insn, 25))
6511 switch (bits (insn, 20, 24))
6512 {
6513 case 0x10:
7ff120b4 6514 return arm_copy_unmodified (gdbarch, insn, "movw", dsc);
cca44b1b
JB
6515
6516 case 0x14:
7ff120b4 6517 return arm_copy_unmodified (gdbarch, insn, "movt", dsc);
cca44b1b
JB
6518
6519 case 0x12: case 0x16:
7ff120b4 6520 return arm_copy_unmodified (gdbarch, insn, "msr imm", dsc);
cca44b1b
JB
6521
6522 default:
7ff120b4 6523 return arm_copy_alu_imm (gdbarch, insn, regs, dsc);
cca44b1b
JB
6524 }
6525 else
6526 {
6527 uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
6528
6529 if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
7ff120b4 6530 return arm_copy_alu_reg (gdbarch, insn, regs, dsc);
cca44b1b 6531 else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
7ff120b4 6532 return arm_copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
cca44b1b 6533 else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
7ff120b4 6534 return arm_decode_miscellaneous (gdbarch, insn, regs, dsc);
cca44b1b 6535 else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
7ff120b4 6536 return arm_copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
cca44b1b 6537 else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
7ff120b4 6538 return arm_copy_unmodified (gdbarch, insn, "mul/mla", dsc);
cca44b1b 6539 else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
7ff120b4 6540 return arm_copy_unmodified (gdbarch, insn, "synch", dsc);
cca44b1b 6541 else if (op2 == 0xb || (op2 & 0xd) == 0xd)
550dc4e2 6542 /* 2nd arg means "unprivileged". */
7ff120b4
YQ
6543 return arm_copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
6544 dsc);
cca44b1b
JB
6545 }
6546
6547 /* Should be unreachable. */
6548 return 1;
6549}
6550
6551static int
7ff120b4
YQ
6552arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
6553 struct regcache *regs,
6554 struct displaced_step_closure *dsc)
cca44b1b
JB
6555{
6556 int a = bit (insn, 25), b = bit (insn, 4);
6557 uint32_t op1 = bits (insn, 20, 24);
cca44b1b
JB
6558
6559 if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
6560 || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
0f6f04ba 6561 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 0);
cca44b1b
JB
6562 else if ((!a && (op1 & 0x17) == 0x02)
6563 || (a && (op1 & 0x17) == 0x02 && !b))
0f6f04ba 6564 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 1);
cca44b1b
JB
6565 else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
6566 || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
0f6f04ba 6567 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 0);
cca44b1b
JB
6568 else if ((!a && (op1 & 0x17) == 0x03)
6569 || (a && (op1 & 0x17) == 0x03 && !b))
0f6f04ba 6570 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 1);
cca44b1b
JB
6571 else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
6572 || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
7ff120b4 6573 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
cca44b1b
JB
6574 else if ((!a && (op1 & 0x17) == 0x06)
6575 || (a && (op1 & 0x17) == 0x06 && !b))
7ff120b4 6576 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
cca44b1b
JB
6577 else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
6578 || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
7ff120b4 6579 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
cca44b1b
JB
6580 else if ((!a && (op1 & 0x17) == 0x07)
6581 || (a && (op1 & 0x17) == 0x07 && !b))
7ff120b4 6582 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
cca44b1b
JB
6583
6584 /* Should be unreachable. */
6585 return 1;
6586}
6587
6588static int
7ff120b4
YQ
6589arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
6590 struct displaced_step_closure *dsc)
cca44b1b
JB
6591{
6592 switch (bits (insn, 20, 24))
6593 {
6594 case 0x00: case 0x01: case 0x02: case 0x03:
7ff120b4 6595 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
cca44b1b
JB
6596
6597 case 0x04: case 0x05: case 0x06: case 0x07:
7ff120b4 6598 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
cca44b1b
JB
6599
6600 case 0x08: case 0x09: case 0x0a: case 0x0b:
6601 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
7ff120b4 6602 return arm_copy_unmodified (gdbarch, insn,
cca44b1b
JB
6603 "decode/pack/unpack/saturate/reverse", dsc);
6604
6605 case 0x18:
6606 if (bits (insn, 5, 7) == 0) /* op2. */
6607 {
6608 if (bits (insn, 12, 15) == 0xf)
7ff120b4 6609 return arm_copy_unmodified (gdbarch, insn, "usad8", dsc);
cca44b1b 6610 else
7ff120b4 6611 return arm_copy_unmodified (gdbarch, insn, "usada8", dsc);
cca44b1b
JB
6612 }
6613 else
7ff120b4 6614 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6615
6616 case 0x1a: case 0x1b:
6617 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 6618 return arm_copy_unmodified (gdbarch, insn, "sbfx", dsc);
cca44b1b 6619 else
7ff120b4 6620 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6621
6622 case 0x1c: case 0x1d:
6623 if (bits (insn, 5, 6) == 0x0) /* op2[1:0]. */
6624 {
6625 if (bits (insn, 0, 3) == 0xf)
7ff120b4 6626 return arm_copy_unmodified (gdbarch, insn, "bfc", dsc);
cca44b1b 6627 else
7ff120b4 6628 return arm_copy_unmodified (gdbarch, insn, "bfi", dsc);
cca44b1b
JB
6629 }
6630 else
7ff120b4 6631 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6632
6633 case 0x1e: case 0x1f:
6634 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 6635 return arm_copy_unmodified (gdbarch, insn, "ubfx", dsc);
cca44b1b 6636 else
7ff120b4 6637 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
6638 }
6639
6640 /* Should be unreachable. */
6641 return 1;
6642}
6643
6644static int
615234c1 6645arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, uint32_t insn,
7ff120b4
YQ
6646 struct regcache *regs,
6647 struct displaced_step_closure *dsc)
cca44b1b
JB
6648{
6649 if (bit (insn, 25))
7ff120b4 6650 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b 6651 else
7ff120b4 6652 return arm_copy_block_xfer (gdbarch, insn, regs, dsc);
cca44b1b
JB
6653}
6654
6655static int
7ff120b4
YQ
6656arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
6657 struct regcache *regs,
6658 struct displaced_step_closure *dsc)
cca44b1b
JB
6659{
6660 unsigned int opcode = bits (insn, 20, 24);
6661
6662 switch (opcode)
6663 {
6664 case 0x04: case 0x05: /* VFP/Neon mrrc/mcrr. */
7ff120b4 6665 return arm_copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
cca44b1b
JB
6666
6667 case 0x08: case 0x0a: case 0x0c: case 0x0e:
6668 case 0x12: case 0x16:
7ff120b4 6669 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
cca44b1b
JB
6670
6671 case 0x09: case 0x0b: case 0x0d: case 0x0f:
6672 case 0x13: case 0x17:
7ff120b4 6673 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
cca44b1b
JB
6674
6675 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
6676 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
6677 /* Note: no writeback for these instructions. Bit 25 will always be
6678 zero though (via caller), so the following works OK. */
7ff120b4 6679 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
6680 }
6681
6682 /* Should be unreachable. */
6683 return 1;
6684}
6685
34518530
YQ
6686/* Decode shifted register instructions. */
6687
6688static int
6689thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
6690 uint16_t insn2, struct regcache *regs,
6691 struct displaced_step_closure *dsc)
6692{
6693 /* PC is only allowed to be used in instruction MOV. */
6694
6695 unsigned int op = bits (insn1, 5, 8);
6696 unsigned int rn = bits (insn1, 0, 3);
6697
6698 if (op == 0x2 && rn == 0xf) /* MOV */
6699 return thumb2_copy_alu_imm (gdbarch, insn1, insn2, regs, dsc);
6700 else
6701 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6702 "dp (shift reg)", dsc);
6703}
6704
6705
6706/* Decode extension register load/store. Exactly the same as
6707 arm_decode_ext_reg_ld_st. */
6708
6709static int
6710thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
6711 uint16_t insn2, struct regcache *regs,
6712 struct displaced_step_closure *dsc)
6713{
6714 unsigned int opcode = bits (insn1, 4, 8);
6715
6716 switch (opcode)
6717 {
6718 case 0x04: case 0x05:
6719 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6720 "vfp/neon vmov", dsc);
6721
6722 case 0x08: case 0x0c: /* 01x00 */
6723 case 0x0a: case 0x0e: /* 01x10 */
6724 case 0x12: case 0x16: /* 10x10 */
6725 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6726 "vfp/neon vstm/vpush", dsc);
6727
6728 case 0x09: case 0x0d: /* 01x01 */
6729 case 0x0b: case 0x0f: /* 01x11 */
6730 case 0x13: case 0x17: /* 10x11 */
6731 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6732 "vfp/neon vldm/vpop", dsc);
6733
6734 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
6735 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6736 "vstr", dsc);
6737 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
6738 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2, regs, dsc);
6739 }
6740
6741 /* Should be unreachable. */
6742 return 1;
6743}
6744
cca44b1b 6745static int
7ff120b4
YQ
6746arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn, CORE_ADDR to,
6747 struct regcache *regs, struct displaced_step_closure *dsc)
cca44b1b
JB
6748{
6749 unsigned int op1 = bits (insn, 20, 25);
6750 int op = bit (insn, 4);
6751 unsigned int coproc = bits (insn, 8, 11);
cca44b1b
JB
6752
6753 if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
7ff120b4 6754 return arm_decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
cca44b1b
JB
6755 else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
6756 && (coproc & 0xe) != 0xa)
6757 /* stc/stc2. */
7ff120b4 6758 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
6759 else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
6760 && (coproc & 0xe) != 0xa)
6761 /* ldc/ldc2 imm/lit. */
7ff120b4 6762 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 6763 else if ((op1 & 0x3e) == 0x00)
7ff120b4 6764 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b 6765 else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
7ff120b4 6766 return arm_copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
cca44b1b 6767 else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
7ff120b4 6768 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b 6769 else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
7ff120b4 6770 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
6771 else if ((op1 & 0x30) == 0x20 && !op)
6772 {
6773 if ((coproc & 0xe) == 0xa)
7ff120b4 6774 return arm_copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
cca44b1b 6775 else
7ff120b4 6776 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
6777 }
6778 else if ((op1 & 0x30) == 0x20 && op)
7ff120b4 6779 return arm_copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
cca44b1b 6780 else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
7ff120b4 6781 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 6782 else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
7ff120b4 6783 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 6784 else if ((op1 & 0x30) == 0x30)
7ff120b4 6785 return arm_copy_svc (gdbarch, insn, regs, dsc);
cca44b1b 6786 else
7ff120b4 6787 return arm_copy_undef (gdbarch, insn, dsc); /* Possibly unreachable. */
cca44b1b
JB
6788}
6789
34518530
YQ
6790static int
6791thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
6792 uint16_t insn2, struct regcache *regs,
6793 struct displaced_step_closure *dsc)
6794{
6795 unsigned int coproc = bits (insn2, 8, 11);
34518530
YQ
6796 unsigned int bit_5_8 = bits (insn1, 5, 8);
6797 unsigned int bit_9 = bit (insn1, 9);
6798 unsigned int bit_4 = bit (insn1, 4);
34518530
YQ
6799
6800 if (bit_9 == 0)
6801 {
6802 if (bit_5_8 == 2)
6803 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6804 "neon 64bit xfer/mrrc/mrrc2/mcrr/mcrr2",
6805 dsc);
6806 else if (bit_5_8 == 0) /* UNDEFINED. */
6807 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
6808 else
6809 {
6810 /*coproc is 101x. SIMD/VFP, ext registers load/store. */
6811 if ((coproc & 0xe) == 0xa)
6812 return thumb2_decode_ext_reg_ld_st (gdbarch, insn1, insn2, regs,
6813 dsc);
6814 else /* coproc is not 101x. */
6815 {
6816 if (bit_4 == 0) /* STC/STC2. */
6817 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6818 "stc/stc2", dsc);
6819 else /* LDC/LDC2 {literal, immeidate}. */
6820 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2,
6821 regs, dsc);
6822 }
6823 }
6824 }
6825 else
6826 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "coproc", dsc);
6827
6828 return 0;
6829}
6830
6831static void
6832install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
6833 struct displaced_step_closure *dsc, int rd)
6834{
6835 /* ADR Rd, #imm
6836
6837 Rewrite as:
6838
6839 Preparation: Rd <- PC
6840 Insn: ADD Rd, #imm
6841 Cleanup: Null.
6842 */
6843
6844 /* Rd <- PC */
6845 int val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6846 displaced_write_reg (regs, dsc, rd, val, CANNOT_WRITE_PC);
6847}
6848
6849static int
6850thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
6851 struct displaced_step_closure *dsc,
6852 int rd, unsigned int imm)
6853{
6854
6855 /* Encoding T2: ADDS Rd, #imm */
6856 dsc->modinsn[0] = (0x3000 | (rd << 8) | imm);
6857
6858 install_pc_relative (gdbarch, regs, dsc, rd);
6859
6860 return 0;
6861}
6862
6863static int
6864thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
6865 struct regcache *regs,
6866 struct displaced_step_closure *dsc)
6867{
6868 unsigned int rd = bits (insn, 8, 10);
6869 unsigned int imm8 = bits (insn, 0, 7);
6870
6871 if (debug_displaced)
6872 fprintf_unfiltered (gdb_stdlog,
6873 "displaced: copying thumb adr r%d, #%d insn %.4x\n",
6874 rd, imm8, insn);
6875
6876 return thumb_copy_pc_relative_16bit (gdbarch, regs, dsc, rd, imm8);
6877}
6878
6879static int
6880thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
6881 uint16_t insn2, struct regcache *regs,
6882 struct displaced_step_closure *dsc)
6883{
6884 unsigned int rd = bits (insn2, 8, 11);
6885 /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
6886 extract raw immediate encoding rather than computing immediate. When
6887 generating ADD or SUB instruction, we can simply perform OR operation to
6888 set immediate into ADD. */
6889 unsigned int imm_3_8 = insn2 & 0x70ff;
6890 unsigned int imm_i = insn1 & 0x0400; /* Clear all bits except bit 10. */
6891
6892 if (debug_displaced)
6893 fprintf_unfiltered (gdb_stdlog,
6894 "displaced: copying thumb adr r%d, #%d:%d insn %.4x%.4x\n",
6895 rd, imm_i, imm_3_8, insn1, insn2);
6896
6897 if (bit (insn1, 7)) /* Encoding T2 */
6898 {
6899 /* Encoding T3: SUB Rd, Rd, #imm */
6900 dsc->modinsn[0] = (0xf1a0 | rd | imm_i);
6901 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
6902 }
6903 else /* Encoding T3 */
6904 {
6905 /* Encoding T3: ADD Rd, Rd, #imm */
6906 dsc->modinsn[0] = (0xf100 | rd | imm_i);
6907 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
6908 }
6909 dsc->numinsns = 2;
6910
6911 install_pc_relative (gdbarch, regs, dsc, rd);
6912
6913 return 0;
6914}
6915
6916static int
615234c1 6917thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, uint16_t insn1,
34518530
YQ
6918 struct regcache *regs,
6919 struct displaced_step_closure *dsc)
6920{
6921 unsigned int rt = bits (insn1, 8, 10);
6922 unsigned int pc;
6923 int imm8 = (bits (insn1, 0, 7) << 2);
34518530
YQ
6924
6925 /* LDR Rd, #imm8
6926
6927 Rwrite as:
6928
6929 Preparation: tmp0 <- R0, tmp2 <- R2, tmp3 <- R3, R2 <- PC, R3 <- #imm8;
6930
6931 Insn: LDR R0, [R2, R3];
6932 Cleanup: R2 <- tmp2, R3 <- tmp3, Rd <- R0, R0 <- tmp0 */
6933
6934 if (debug_displaced)
6935 fprintf_unfiltered (gdb_stdlog,
6936 "displaced: copying thumb ldr r%d [pc #%d]\n"
6937 , rt, imm8);
6938
6939 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6940 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6941 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6942 pc = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6943 /* The assembler calculates the required value of the offset from the
6944 Align(PC,4) value of this instruction to the label. */
6945 pc = pc & 0xfffffffc;
6946
6947 displaced_write_reg (regs, dsc, 2, pc, CANNOT_WRITE_PC);
6948 displaced_write_reg (regs, dsc, 3, imm8, CANNOT_WRITE_PC);
6949
6950 dsc->rd = rt;
6951 dsc->u.ldst.xfersize = 4;
6952 dsc->u.ldst.rn = 0;
6953 dsc->u.ldst.immed = 0;
6954 dsc->u.ldst.writeback = 0;
6955 dsc->u.ldst.restore_r4 = 0;
6956
6957 dsc->modinsn[0] = 0x58d0; /* ldr r0, [r2, r3]*/
6958
6959 dsc->cleanup = &cleanup_load;
6960
6961 return 0;
6962}
6963
6964/* Copy Thumb cbnz/cbz insruction. */
6965
6966static int
6967thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
6968 struct regcache *regs,
6969 struct displaced_step_closure *dsc)
6970{
6971 int non_zero = bit (insn1, 11);
6972 unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
6973 CORE_ADDR from = dsc->insn_addr;
6974 int rn = bits (insn1, 0, 2);
6975 int rn_val = displaced_read_reg (regs, dsc, rn);
6976
6977 dsc->u.branch.cond = (rn_val && non_zero) || (!rn_val && !non_zero);
6978 /* CBNZ and CBZ do not affect the condition flags. If condition is true,
6979 set it INST_AL, so cleanup_branch will know branch is taken, otherwise,
6980 condition is false, let it be, cleanup_branch will do nothing. */
6981 if (dsc->u.branch.cond)
6982 {
6983 dsc->u.branch.cond = INST_AL;
6984 dsc->u.branch.dest = from + 4 + imm5;
6985 }
6986 else
6987 dsc->u.branch.dest = from + 2;
6988
6989 dsc->u.branch.link = 0;
6990 dsc->u.branch.exchange = 0;
6991
6992 if (debug_displaced)
6993 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s [r%d = 0x%x]"
6994 " insn %.4x to %.8lx\n", non_zero ? "cbnz" : "cbz",
6995 rn, rn_val, insn1, dsc->u.branch.dest);
6996
6997 dsc->modinsn[0] = THUMB_NOP;
6998
6999 dsc->cleanup = &cleanup_branch;
7000 return 0;
7001}
7002
7003/* Copy Table Branch Byte/Halfword */
7004static int
7005thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
7006 uint16_t insn2, struct regcache *regs,
7007 struct displaced_step_closure *dsc)
7008{
7009 ULONGEST rn_val, rm_val;
7010 int is_tbh = bit (insn2, 4);
7011 CORE_ADDR halfwords = 0;
7012 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7013
7014 rn_val = displaced_read_reg (regs, dsc, bits (insn1, 0, 3));
7015 rm_val = displaced_read_reg (regs, dsc, bits (insn2, 0, 3));
7016
7017 if (is_tbh)
7018 {
7019 gdb_byte buf[2];
7020
7021 target_read_memory (rn_val + 2 * rm_val, buf, 2);
7022 halfwords = extract_unsigned_integer (buf, 2, byte_order);
7023 }
7024 else
7025 {
7026 gdb_byte buf[1];
7027
7028 target_read_memory (rn_val + rm_val, buf, 1);
7029 halfwords = extract_unsigned_integer (buf, 1, byte_order);
7030 }
7031
7032 if (debug_displaced)
7033 fprintf_unfiltered (gdb_stdlog, "displaced: %s base 0x%x offset 0x%x"
7034 " offset 0x%x\n", is_tbh ? "tbh" : "tbb",
7035 (unsigned int) rn_val, (unsigned int) rm_val,
7036 (unsigned int) halfwords);
7037
7038 dsc->u.branch.cond = INST_AL;
7039 dsc->u.branch.link = 0;
7040 dsc->u.branch.exchange = 0;
7041 dsc->u.branch.dest = dsc->insn_addr + 4 + 2 * halfwords;
7042
7043 dsc->cleanup = &cleanup_branch;
7044
7045 return 0;
7046}
7047
7048static void
7049cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
7050 struct displaced_step_closure *dsc)
7051{
7052 /* PC <- r7 */
7053 int val = displaced_read_reg (regs, dsc, 7);
7054 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, val, BX_WRITE_PC);
7055
7056 /* r7 <- r8 */
7057 val = displaced_read_reg (regs, dsc, 8);
7058 displaced_write_reg (regs, dsc, 7, val, CANNOT_WRITE_PC);
7059
7060 /* r8 <- tmp[0] */
7061 displaced_write_reg (regs, dsc, 8, dsc->tmp[0], CANNOT_WRITE_PC);
7062
7063}
7064
7065static int
615234c1 7066thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, uint16_t insn1,
34518530
YQ
7067 struct regcache *regs,
7068 struct displaced_step_closure *dsc)
7069{
7070 dsc->u.block.regmask = insn1 & 0x00ff;
7071
7072 /* Rewrite instruction: POP {rX, rY, ...,rZ, PC}
7073 to :
7074
7075 (1) register list is full, that is, r0-r7 are used.
7076 Prepare: tmp[0] <- r8
7077
7078 POP {r0, r1, ...., r6, r7}; remove PC from reglist
7079 MOV r8, r7; Move value of r7 to r8;
7080 POP {r7}; Store PC value into r7.
7081
7082 Cleanup: PC <- r7, r7 <- r8, r8 <-tmp[0]
7083
7084 (2) register list is not full, supposing there are N registers in
7085 register list (except PC, 0 <= N <= 7).
7086 Prepare: for each i, 0 - N, tmp[i] <- ri.
7087
7088 POP {r0, r1, ...., rN};
7089
7090 Cleanup: Set registers in original reglist from r0 - rN. Restore r0 - rN
7091 from tmp[] properly.
7092 */
7093 if (debug_displaced)
7094 fprintf_unfiltered (gdb_stdlog,
7095 "displaced: copying thumb pop {%.8x, pc} insn %.4x\n",
7096 dsc->u.block.regmask, insn1);
7097
7098 if (dsc->u.block.regmask == 0xff)
7099 {
7100 dsc->tmp[0] = displaced_read_reg (regs, dsc, 8);
7101
7102 dsc->modinsn[0] = (insn1 & 0xfeff); /* POP {r0,r1,...,r6, r7} */
7103 dsc->modinsn[1] = 0x46b8; /* MOV r8, r7 */
7104 dsc->modinsn[2] = 0xbc80; /* POP {r7} */
7105
7106 dsc->numinsns = 3;
7107 dsc->cleanup = &cleanup_pop_pc_16bit_all;
7108 }
7109 else
7110 {
7111 unsigned int num_in_list = bitcount (dsc->u.block.regmask);
bec2ab5a
SM
7112 unsigned int i;
7113 unsigned int new_regmask;
34518530
YQ
7114
7115 for (i = 0; i < num_in_list + 1; i++)
7116 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7117
7118 new_regmask = (1 << (num_in_list + 1)) - 1;
7119
7120 if (debug_displaced)
7121 fprintf_unfiltered (gdb_stdlog, _("displaced: POP "
7122 "{..., pc}: original reg list %.4x,"
7123 " modified list %.4x\n"),
7124 (int) dsc->u.block.regmask, new_regmask);
7125
7126 dsc->u.block.regmask |= 0x8000;
7127 dsc->u.block.writeback = 0;
7128 dsc->u.block.cond = INST_AL;
7129
7130 dsc->modinsn[0] = (insn1 & ~0x1ff) | (new_regmask & 0xff);
7131
7132 dsc->cleanup = &cleanup_block_load_pc;
7133 }
7134
7135 return 0;
7136}
7137
7138static void
7139thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
7140 struct regcache *regs,
7141 struct displaced_step_closure *dsc)
7142{
7143 unsigned short op_bit_12_15 = bits (insn1, 12, 15);
7144 unsigned short op_bit_10_11 = bits (insn1, 10, 11);
7145 int err = 0;
7146
7147 /* 16-bit thumb instructions. */
7148 switch (op_bit_12_15)
7149 {
7150 /* Shift (imme), add, subtract, move and compare. */
7151 case 0: case 1: case 2: case 3:
7152 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
7153 "shift/add/sub/mov/cmp",
7154 dsc);
7155 break;
7156 case 4:
7157 switch (op_bit_10_11)
7158 {
7159 case 0: /* Data-processing */
7160 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
7161 "data-processing",
7162 dsc);
7163 break;
7164 case 1: /* Special data instructions and branch and exchange. */
7165 {
7166 unsigned short op = bits (insn1, 7, 9);
7167 if (op == 6 || op == 7) /* BX or BLX */
7168 err = thumb_copy_bx_blx_reg (gdbarch, insn1, regs, dsc);
7169 else if (bits (insn1, 6, 7) != 0) /* ADD/MOV/CMP high registers. */
7170 err = thumb_copy_alu_reg (gdbarch, insn1, regs, dsc);
7171 else
7172 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "special data",
7173 dsc);
7174 }
7175 break;
7176 default: /* LDR (literal) */
7177 err = thumb_copy_16bit_ldr_literal (gdbarch, insn1, regs, dsc);
7178 }
7179 break;
7180 case 5: case 6: case 7: case 8: case 9: /* Load/Store single data item */
7181 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldr/str", dsc);
7182 break;
7183 case 10:
7184 if (op_bit_10_11 < 2) /* Generate PC-relative address */
7185 err = thumb_decode_pc_relative_16bit (gdbarch, insn1, regs, dsc);
7186 else /* Generate SP-relative address */
7187 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "sp-relative", dsc);
7188 break;
7189 case 11: /* Misc 16-bit instructions */
7190 {
7191 switch (bits (insn1, 8, 11))
7192 {
7193 case 1: case 3: case 9: case 11: /* CBNZ, CBZ */
7194 err = thumb_copy_cbnz_cbz (gdbarch, insn1, regs, dsc);
7195 break;
7196 case 12: case 13: /* POP */
7197 if (bit (insn1, 8)) /* PC is in register list. */
7198 err = thumb_copy_pop_pc_16bit (gdbarch, insn1, regs, dsc);
7199 else
7200 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "pop", dsc);
7201 break;
7202 case 15: /* If-Then, and hints */
7203 if (bits (insn1, 0, 3))
7204 /* If-Then makes up to four following instructions conditional.
7205 IT instruction itself is not conditional, so handle it as a
7206 common unmodified instruction. */
7207 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "If-Then",
7208 dsc);
7209 else
7210 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "hints", dsc);
7211 break;
7212 default:
7213 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "misc", dsc);
7214 }
7215 }
7216 break;
7217 case 12:
7218 if (op_bit_10_11 < 2) /* Store multiple registers */
7219 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "stm", dsc);
7220 else /* Load multiple registers */
7221 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldm", dsc);
7222 break;
7223 case 13: /* Conditional branch and supervisor call */
7224 if (bits (insn1, 9, 11) != 7) /* conditional branch */
7225 err = thumb_copy_b (gdbarch, insn1, dsc);
7226 else
7227 err = thumb_copy_svc (gdbarch, insn1, regs, dsc);
7228 break;
7229 case 14: /* Unconditional branch */
7230 err = thumb_copy_b (gdbarch, insn1, dsc);
7231 break;
7232 default:
7233 err = 1;
7234 }
7235
7236 if (err)
7237 internal_error (__FILE__, __LINE__,
7238 _("thumb_process_displaced_16bit_insn: Instruction decode error"));
7239}
7240
7241static int
7242decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
7243 uint16_t insn1, uint16_t insn2,
7244 struct regcache *regs,
7245 struct displaced_step_closure *dsc)
7246{
7247 int rt = bits (insn2, 12, 15);
7248 int rn = bits (insn1, 0, 3);
7249 int op1 = bits (insn1, 7, 8);
34518530
YQ
7250
7251 switch (bits (insn1, 5, 6))
7252 {
7253 case 0: /* Load byte and memory hints */
7254 if (rt == 0xf) /* PLD/PLI */
7255 {
7256 if (rn == 0xf)
7257 /* PLD literal or Encoding T3 of PLI(immediate, literal). */
7258 return thumb2_copy_preload (gdbarch, insn1, insn2, regs, dsc);
7259 else
7260 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7261 "pli/pld", dsc);
7262 }
7263 else
7264 {
7265 if (rn == 0xf) /* LDRB/LDRSB (literal) */
7266 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
7267 1);
7268 else
7269 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7270 "ldrb{reg, immediate}/ldrbt",
7271 dsc);
7272 }
7273
7274 break;
7275 case 1: /* Load halfword and memory hints. */
7276 if (rt == 0xf) /* PLD{W} and Unalloc memory hint. */
7277 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7278 "pld/unalloc memhint", dsc);
7279 else
7280 {
7281 if (rn == 0xf)
7282 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
7283 2);
7284 else
7285 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7286 "ldrh/ldrht", dsc);
7287 }
7288 break;
7289 case 2: /* Load word */
7290 {
7291 int insn2_bit_8_11 = bits (insn2, 8, 11);
7292
7293 if (rn == 0xf)
7294 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc, 4);
7295 else if (op1 == 0x1) /* Encoding T3 */
7296 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs, dsc,
7297 0, 1);
7298 else /* op1 == 0x0 */
7299 {
7300 if (insn2_bit_8_11 == 0xc || (insn2_bit_8_11 & 0x9) == 0x9)
7301 /* LDR (immediate) */
7302 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
7303 dsc, bit (insn2, 8), 1);
7304 else if (insn2_bit_8_11 == 0xe) /* LDRT */
7305 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7306 "ldrt", dsc);
7307 else
7308 /* LDR (register) */
7309 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
7310 dsc, 0, 0);
7311 }
7312 break;
7313 }
7314 default:
7315 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
7316 break;
7317 }
7318 return 0;
7319}
7320
7321static void
7322thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
7323 uint16_t insn2, struct regcache *regs,
7324 struct displaced_step_closure *dsc)
7325{
7326 int err = 0;
7327 unsigned short op = bit (insn2, 15);
7328 unsigned int op1 = bits (insn1, 11, 12);
7329
7330 switch (op1)
7331 {
7332 case 1:
7333 {
7334 switch (bits (insn1, 9, 10))
7335 {
7336 case 0:
7337 if (bit (insn1, 6))
7338 {
7339 /* Load/store {dual, execlusive}, table branch. */
7340 if (bits (insn1, 7, 8) == 1 && bits (insn1, 4, 5) == 1
7341 && bits (insn2, 5, 7) == 0)
7342 err = thumb2_copy_table_branch (gdbarch, insn1, insn2, regs,
7343 dsc);
7344 else
7345 /* PC is not allowed to use in load/store {dual, exclusive}
7346 instructions. */
7347 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7348 "load/store dual/ex", dsc);
7349 }
7350 else /* load/store multiple */
7351 {
7352 switch (bits (insn1, 7, 8))
7353 {
7354 case 0: case 3: /* SRS, RFE */
7355 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7356 "srs/rfe", dsc);
7357 break;
7358 case 1: case 2: /* LDM/STM/PUSH/POP */
7359 err = thumb2_copy_block_xfer (gdbarch, insn1, insn2, regs, dsc);
7360 break;
7361 }
7362 }
7363 break;
7364
7365 case 1:
7366 /* Data-processing (shift register). */
7367 err = thumb2_decode_dp_shift_reg (gdbarch, insn1, insn2, regs,
7368 dsc);
7369 break;
7370 default: /* Coprocessor instructions. */
7371 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
7372 break;
7373 }
7374 break;
7375 }
7376 case 2: /* op1 = 2 */
7377 if (op) /* Branch and misc control. */
7378 {
7379 if (bit (insn2, 14) /* BLX/BL */
7380 || bit (insn2, 12) /* Unconditional branch */
7381 || (bits (insn1, 7, 9) != 0x7)) /* Conditional branch */
7382 err = thumb2_copy_b_bl_blx (gdbarch, insn1, insn2, regs, dsc);
7383 else
7384 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7385 "misc ctrl", dsc);
7386 }
7387 else
7388 {
7389 if (bit (insn1, 9)) /* Data processing (plain binary imm). */
7390 {
7391 int op = bits (insn1, 4, 8);
7392 int rn = bits (insn1, 0, 3);
7393 if ((op == 0 || op == 0xa) && rn == 0xf)
7394 err = thumb_copy_pc_relative_32bit (gdbarch, insn1, insn2,
7395 regs, dsc);
7396 else
7397 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7398 "dp/pb", dsc);
7399 }
7400 else /* Data processing (modified immeidate) */
7401 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7402 "dp/mi", dsc);
7403 }
7404 break;
7405 case 3: /* op1 = 3 */
7406 switch (bits (insn1, 9, 10))
7407 {
7408 case 0:
7409 if (bit (insn1, 4))
7410 err = decode_thumb_32bit_ld_mem_hints (gdbarch, insn1, insn2,
7411 regs, dsc);
7412 else /* NEON Load/Store and Store single data item */
7413 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7414 "neon elt/struct load/store",
7415 dsc);
7416 break;
7417 case 1: /* op1 = 3, bits (9, 10) == 1 */
7418 switch (bits (insn1, 7, 8))
7419 {
7420 case 0: case 1: /* Data processing (register) */
7421 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7422 "dp(reg)", dsc);
7423 break;
7424 case 2: /* Multiply and absolute difference */
7425 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7426 "mul/mua/diff", dsc);
7427 break;
7428 case 3: /* Long multiply and divide */
7429 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7430 "lmul/lmua", dsc);
7431 break;
7432 }
7433 break;
7434 default: /* Coprocessor instructions */
7435 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
7436 break;
7437 }
7438 break;
7439 default:
7440 err = 1;
7441 }
7442
7443 if (err)
7444 internal_error (__FILE__, __LINE__,
7445 _("thumb_process_displaced_32bit_insn: Instruction decode error"));
7446
7447}
7448
b434a28f
YQ
7449static void
7450thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
7451 CORE_ADDR to, struct regcache *regs,
7452 struct displaced_step_closure *dsc)
7453{
34518530
YQ
7454 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7455 uint16_t insn1
7456 = read_memory_unsigned_integer (from, 2, byte_order_for_code);
7457
7458 if (debug_displaced)
7459 fprintf_unfiltered (gdb_stdlog, "displaced: process thumb insn %.4x "
7460 "at %.8lx\n", insn1, (unsigned long) from);
7461
7462 dsc->is_thumb = 1;
7463 dsc->insn_size = thumb_insn_size (insn1);
7464 if (thumb_insn_size (insn1) == 4)
7465 {
7466 uint16_t insn2
7467 = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
7468 thumb_process_displaced_32bit_insn (gdbarch, insn1, insn2, regs, dsc);
7469 }
7470 else
7471 thumb_process_displaced_16bit_insn (gdbarch, insn1, regs, dsc);
b434a28f
YQ
7472}
7473
cca44b1b 7474void
b434a28f
YQ
7475arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
7476 CORE_ADDR to, struct regcache *regs,
cca44b1b
JB
7477 struct displaced_step_closure *dsc)
7478{
7479 int err = 0;
b434a28f
YQ
7480 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7481 uint32_t insn;
cca44b1b
JB
7482
7483 /* Most displaced instructions use a 1-instruction scratch space, so set this
7484 here and override below if/when necessary. */
7485 dsc->numinsns = 1;
7486 dsc->insn_addr = from;
7487 dsc->scratch_base = to;
7488 dsc->cleanup = NULL;
7489 dsc->wrote_to_pc = 0;
7490
b434a28f
YQ
7491 if (!displaced_in_arm_mode (regs))
7492 return thumb_process_displaced_insn (gdbarch, from, to, regs, dsc);
7493
4db71c0b
YQ
7494 dsc->is_thumb = 0;
7495 dsc->insn_size = 4;
b434a28f
YQ
7496 insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
7497 if (debug_displaced)
7498 fprintf_unfiltered (gdb_stdlog, "displaced: stepping insn %.8lx "
7499 "at %.8lx\n", (unsigned long) insn,
7500 (unsigned long) from);
7501
cca44b1b 7502 if ((insn & 0xf0000000) == 0xf0000000)
7ff120b4 7503 err = arm_decode_unconditional (gdbarch, insn, regs, dsc);
cca44b1b
JB
7504 else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
7505 {
7506 case 0x0: case 0x1: case 0x2: case 0x3:
7ff120b4 7507 err = arm_decode_dp_misc (gdbarch, insn, regs, dsc);
cca44b1b
JB
7508 break;
7509
7510 case 0x4: case 0x5: case 0x6:
7ff120b4 7511 err = arm_decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
cca44b1b
JB
7512 break;
7513
7514 case 0x7:
7ff120b4 7515 err = arm_decode_media (gdbarch, insn, dsc);
cca44b1b
JB
7516 break;
7517
7518 case 0x8: case 0x9: case 0xa: case 0xb:
7ff120b4 7519 err = arm_decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
cca44b1b
JB
7520 break;
7521
7522 case 0xc: case 0xd: case 0xe: case 0xf:
7ff120b4 7523 err = arm_decode_svc_copro (gdbarch, insn, to, regs, dsc);
cca44b1b
JB
7524 break;
7525 }
7526
7527 if (err)
7528 internal_error (__FILE__, __LINE__,
7529 _("arm_process_displaced_insn: Instruction decode error"));
7530}
7531
7532/* Actually set up the scratch space for a displaced instruction. */
7533
7534void
7535arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
7536 CORE_ADDR to, struct displaced_step_closure *dsc)
7537{
7538 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4db71c0b 7539 unsigned int i, len, offset;
cca44b1b 7540 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4db71c0b 7541 int size = dsc->is_thumb? 2 : 4;
948f8e3d 7542 const gdb_byte *bkp_insn;
cca44b1b 7543
4db71c0b 7544 offset = 0;
cca44b1b
JB
7545 /* Poke modified instruction(s). */
7546 for (i = 0; i < dsc->numinsns; i++)
7547 {
7548 if (debug_displaced)
4db71c0b
YQ
7549 {
7550 fprintf_unfiltered (gdb_stdlog, "displaced: writing insn ");
7551 if (size == 4)
7552 fprintf_unfiltered (gdb_stdlog, "%.8lx",
7553 dsc->modinsn[i]);
7554 else if (size == 2)
7555 fprintf_unfiltered (gdb_stdlog, "%.4x",
7556 (unsigned short)dsc->modinsn[i]);
7557
7558 fprintf_unfiltered (gdb_stdlog, " at %.8lx\n",
7559 (unsigned long) to + offset);
7560
7561 }
7562 write_memory_unsigned_integer (to + offset, size,
7563 byte_order_for_code,
cca44b1b 7564 dsc->modinsn[i]);
4db71c0b
YQ
7565 offset += size;
7566 }
7567
7568 /* Choose the correct breakpoint instruction. */
7569 if (dsc->is_thumb)
7570 {
7571 bkp_insn = tdep->thumb_breakpoint;
7572 len = tdep->thumb_breakpoint_size;
7573 }
7574 else
7575 {
7576 bkp_insn = tdep->arm_breakpoint;
7577 len = tdep->arm_breakpoint_size;
cca44b1b
JB
7578 }
7579
7580 /* Put breakpoint afterwards. */
4db71c0b 7581 write_memory (to + offset, bkp_insn, len);
cca44b1b
JB
7582
7583 if (debug_displaced)
7584 fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
7585 paddress (gdbarch, from), paddress (gdbarch, to));
7586}
7587
cca44b1b
JB
7588/* Entry point for cleaning things up after a displaced instruction has been
7589 single-stepped. */
7590
7591void
7592arm_displaced_step_fixup (struct gdbarch *gdbarch,
7593 struct displaced_step_closure *dsc,
7594 CORE_ADDR from, CORE_ADDR to,
7595 struct regcache *regs)
7596{
7597 if (dsc->cleanup)
7598 dsc->cleanup (gdbarch, regs, dsc);
7599
7600 if (!dsc->wrote_to_pc)
4db71c0b
YQ
7601 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
7602 dsc->insn_addr + dsc->insn_size);
7603
cca44b1b
JB
7604}
7605
7606#include "bfd-in2.h"
7607#include "libcoff.h"
7608
7609static int
7610gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
7611{
9a3c8263 7612 struct gdbarch *gdbarch = (struct gdbarch *) info->application_data;
9779414d
DJ
7613
7614 if (arm_pc_is_thumb (gdbarch, memaddr))
cca44b1b
JB
7615 {
7616 static asymbol *asym;
7617 static combined_entry_type ce;
7618 static struct coff_symbol_struct csym;
7619 static struct bfd fake_bfd;
7620 static bfd_target fake_target;
7621
7622 if (csym.native == NULL)
7623 {
7624 /* Create a fake symbol vector containing a Thumb symbol.
7625 This is solely so that the code in print_insn_little_arm()
7626 and print_insn_big_arm() in opcodes/arm-dis.c will detect
7627 the presence of a Thumb symbol and switch to decoding
7628 Thumb instructions. */
7629
7630 fake_target.flavour = bfd_target_coff_flavour;
7631 fake_bfd.xvec = &fake_target;
7632 ce.u.syment.n_sclass = C_THUMBEXTFUNC;
7633 csym.native = &ce;
7634 csym.symbol.the_bfd = &fake_bfd;
7635 csym.symbol.name = "fake";
7636 asym = (asymbol *) & csym;
7637 }
7638
7639 memaddr = UNMAKE_THUMB_ADDR (memaddr);
7640 info->symbols = &asym;
7641 }
7642 else
7643 info->symbols = NULL;
7644
7645 if (info->endian == BFD_ENDIAN_BIG)
7646 return print_insn_big_arm (memaddr, info);
7647 else
7648 return print_insn_little_arm (memaddr, info);
7649}
7650
7651/* The following define instruction sequences that will cause ARM
7652 cpu's to take an undefined instruction trap. These are used to
7653 signal a breakpoint to GDB.
7654
7655 The newer ARMv4T cpu's are capable of operating in ARM or Thumb
7656 modes. A different instruction is required for each mode. The ARM
7657 cpu's can also be big or little endian. Thus four different
7658 instructions are needed to support all cases.
7659
7660 Note: ARMv4 defines several new instructions that will take the
7661 undefined instruction trap. ARM7TDMI is nominally ARMv4T, but does
7662 not in fact add the new instructions. The new undefined
7663 instructions in ARMv4 are all instructions that had no defined
7664 behaviour in earlier chips. There is no guarantee that they will
7665 raise an exception, but may be treated as NOP's. In practice, it
7666 may only safe to rely on instructions matching:
7667
7668 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
7669 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
7670 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
7671
0963b4bd 7672 Even this may only true if the condition predicate is true. The
cca44b1b
JB
7673 following use a condition predicate of ALWAYS so it is always TRUE.
7674
7675 There are other ways of forcing a breakpoint. GNU/Linux, RISC iX,
7676 and NetBSD all use a software interrupt rather than an undefined
7677 instruction to force a trap. This can be handled by by the
7678 abi-specific code during establishment of the gdbarch vector. */
7679
7680#define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
7681#define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
7682#define THUMB_LE_BREAKPOINT {0xbe,0xbe}
7683#define THUMB_BE_BREAKPOINT {0xbe,0xbe}
7684
948f8e3d
PA
7685static const gdb_byte arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
7686static const gdb_byte arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
7687static const gdb_byte arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
7688static const gdb_byte arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
cca44b1b
JB
7689
7690/* Determine the type and size of breakpoint to insert at PCPTR. Uses
7691 the program counter value to determine whether a 16-bit or 32-bit
7692 breakpoint should be used. It returns a pointer to a string of
7693 bytes that encode a breakpoint instruction, stores the length of
7694 the string to *lenptr, and adjusts the program counter (if
7695 necessary) to point to the actual memory location where the
7696 breakpoint should be inserted. */
7697
7698static const unsigned char *
7699arm_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenptr)
7700{
7701 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
177321bd 7702 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
cca44b1b 7703
9779414d 7704 if (arm_pc_is_thumb (gdbarch, *pcptr))
cca44b1b
JB
7705 {
7706 *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
177321bd
DJ
7707
7708 /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
7709 check whether we are replacing a 32-bit instruction. */
7710 if (tdep->thumb2_breakpoint != NULL)
7711 {
7712 gdb_byte buf[2];
7713 if (target_read_memory (*pcptr, buf, 2) == 0)
7714 {
7715 unsigned short inst1;
7716 inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
db24da6d 7717 if (thumb_insn_size (inst1) == 4)
177321bd
DJ
7718 {
7719 *lenptr = tdep->thumb2_breakpoint_size;
7720 return tdep->thumb2_breakpoint;
7721 }
7722 }
7723 }
7724
cca44b1b
JB
7725 *lenptr = tdep->thumb_breakpoint_size;
7726 return tdep->thumb_breakpoint;
7727 }
7728 else
7729 {
7730 *lenptr = tdep->arm_breakpoint_size;
7731 return tdep->arm_breakpoint;
7732 }
7733}
7734
177321bd
DJ
7735static void
7736arm_remote_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
7737 int *kindptr)
7738{
177321bd
DJ
7739 arm_breakpoint_from_pc (gdbarch, pcptr, kindptr);
7740
9779414d 7741 if (arm_pc_is_thumb (gdbarch, *pcptr) && *kindptr == 4)
177321bd
DJ
7742 /* The documented magic value for a 32-bit Thumb-2 breakpoint, so
7743 that this is not confused with a 32-bit ARM breakpoint. */
7744 *kindptr = 3;
7745}
7746
cca44b1b
JB
7747/* Extract from an array REGBUF containing the (raw) register state a
7748 function return value of type TYPE, and copy that, in virtual
7749 format, into VALBUF. */
7750
7751static void
7752arm_extract_return_value (struct type *type, struct regcache *regs,
7753 gdb_byte *valbuf)
7754{
7755 struct gdbarch *gdbarch = get_regcache_arch (regs);
7756 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7757
7758 if (TYPE_CODE_FLT == TYPE_CODE (type))
7759 {
7760 switch (gdbarch_tdep (gdbarch)->fp_model)
7761 {
7762 case ARM_FLOAT_FPA:
7763 {
7764 /* The value is in register F0 in internal format. We need to
7765 extract the raw value and then convert it to the desired
7766 internal type. */
7767 bfd_byte tmpbuf[FP_REGISTER_SIZE];
7768
7769 regcache_cooked_read (regs, ARM_F0_REGNUM, tmpbuf);
7770 convert_from_extended (floatformat_from_type (type), tmpbuf,
7771 valbuf, gdbarch_byte_order (gdbarch));
7772 }
7773 break;
7774
7775 case ARM_FLOAT_SOFT_FPA:
7776 case ARM_FLOAT_SOFT_VFP:
7777 /* ARM_FLOAT_VFP can arise if this is a variadic function so
7778 not using the VFP ABI code. */
7779 case ARM_FLOAT_VFP:
7780 regcache_cooked_read (regs, ARM_A1_REGNUM, valbuf);
7781 if (TYPE_LENGTH (type) > 4)
7782 regcache_cooked_read (regs, ARM_A1_REGNUM + 1,
7783 valbuf + INT_REGISTER_SIZE);
7784 break;
7785
7786 default:
0963b4bd
MS
7787 internal_error (__FILE__, __LINE__,
7788 _("arm_extract_return_value: "
7789 "Floating point model not supported"));
cca44b1b
JB
7790 break;
7791 }
7792 }
7793 else if (TYPE_CODE (type) == TYPE_CODE_INT
7794 || TYPE_CODE (type) == TYPE_CODE_CHAR
7795 || TYPE_CODE (type) == TYPE_CODE_BOOL
7796 || TYPE_CODE (type) == TYPE_CODE_PTR
7797 || TYPE_CODE (type) == TYPE_CODE_REF
7798 || TYPE_CODE (type) == TYPE_CODE_ENUM)
7799 {
b021a221
MS
7800 /* If the type is a plain integer, then the access is
7801 straight-forward. Otherwise we have to play around a bit
7802 more. */
cca44b1b
JB
7803 int len = TYPE_LENGTH (type);
7804 int regno = ARM_A1_REGNUM;
7805 ULONGEST tmp;
7806
7807 while (len > 0)
7808 {
7809 /* By using store_unsigned_integer we avoid having to do
7810 anything special for small big-endian values. */
7811 regcache_cooked_read_unsigned (regs, regno++, &tmp);
7812 store_unsigned_integer (valbuf,
7813 (len > INT_REGISTER_SIZE
7814 ? INT_REGISTER_SIZE : len),
7815 byte_order, tmp);
7816 len -= INT_REGISTER_SIZE;
7817 valbuf += INT_REGISTER_SIZE;
7818 }
7819 }
7820 else
7821 {
7822 /* For a structure or union the behaviour is as if the value had
7823 been stored to word-aligned memory and then loaded into
7824 registers with 32-bit load instruction(s). */
7825 int len = TYPE_LENGTH (type);
7826 int regno = ARM_A1_REGNUM;
7827 bfd_byte tmpbuf[INT_REGISTER_SIZE];
7828
7829 while (len > 0)
7830 {
7831 regcache_cooked_read (regs, regno++, tmpbuf);
7832 memcpy (valbuf, tmpbuf,
7833 len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
7834 len -= INT_REGISTER_SIZE;
7835 valbuf += INT_REGISTER_SIZE;
7836 }
7837 }
7838}
7839
7840
7841/* Will a function return an aggregate type in memory or in a
7842 register? Return 0 if an aggregate type can be returned in a
7843 register, 1 if it must be returned in memory. */
7844
7845static int
7846arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
7847{
cca44b1b
JB
7848 enum type_code code;
7849
f168693b 7850 type = check_typedef (type);
cca44b1b 7851
b13c8ab2
YQ
7852 /* Simple, non-aggregate types (ie not including vectors and
7853 complex) are always returned in a register (or registers). */
7854 code = TYPE_CODE (type);
7855 if (TYPE_CODE_STRUCT != code && TYPE_CODE_UNION != code
7856 && TYPE_CODE_ARRAY != code && TYPE_CODE_COMPLEX != code)
7857 return 0;
cca44b1b 7858
c4312b19
YQ
7859 if (TYPE_CODE_ARRAY == code && TYPE_VECTOR (type))
7860 {
7861 /* Vector values should be returned using ARM registers if they
7862 are not over 16 bytes. */
7863 return (TYPE_LENGTH (type) > 16);
7864 }
7865
b13c8ab2 7866 if (gdbarch_tdep (gdbarch)->arm_abi != ARM_ABI_APCS)
cca44b1b 7867 {
b13c8ab2
YQ
7868 /* The AAPCS says all aggregates not larger than a word are returned
7869 in a register. */
7870 if (TYPE_LENGTH (type) <= INT_REGISTER_SIZE)
7871 return 0;
7872
cca44b1b
JB
7873 return 1;
7874 }
b13c8ab2
YQ
7875 else
7876 {
7877 int nRc;
cca44b1b 7878
b13c8ab2
YQ
7879 /* All aggregate types that won't fit in a register must be returned
7880 in memory. */
7881 if (TYPE_LENGTH (type) > INT_REGISTER_SIZE)
7882 return 1;
cca44b1b 7883
b13c8ab2
YQ
7884 /* In the ARM ABI, "integer" like aggregate types are returned in
7885 registers. For an aggregate type to be integer like, its size
7886 must be less than or equal to INT_REGISTER_SIZE and the
7887 offset of each addressable subfield must be zero. Note that bit
7888 fields are not addressable, and all addressable subfields of
7889 unions always start at offset zero.
cca44b1b 7890
b13c8ab2
YQ
7891 This function is based on the behaviour of GCC 2.95.1.
7892 See: gcc/arm.c: arm_return_in_memory() for details.
cca44b1b 7893
b13c8ab2
YQ
7894 Note: All versions of GCC before GCC 2.95.2 do not set up the
7895 parameters correctly for a function returning the following
7896 structure: struct { float f;}; This should be returned in memory,
7897 not a register. Richard Earnshaw sent me a patch, but I do not
7898 know of any way to detect if a function like the above has been
7899 compiled with the correct calling convention. */
7900
7901 /* Assume all other aggregate types can be returned in a register.
7902 Run a check for structures, unions and arrays. */
7903 nRc = 0;
67255d04 7904
b13c8ab2
YQ
7905 if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
7906 {
7907 int i;
7908 /* Need to check if this struct/union is "integer" like. For
7909 this to be true, its size must be less than or equal to
7910 INT_REGISTER_SIZE and the offset of each addressable
7911 subfield must be zero. Note that bit fields are not
7912 addressable, and unions always start at offset zero. If any
7913 of the subfields is a floating point type, the struct/union
7914 cannot be an integer type. */
7915
7916 /* For each field in the object, check:
7917 1) Is it FP? --> yes, nRc = 1;
7918 2) Is it addressable (bitpos != 0) and
7919 not packed (bitsize == 0)?
7920 --> yes, nRc = 1
7921 */
7922
7923 for (i = 0; i < TYPE_NFIELDS (type); i++)
67255d04 7924 {
b13c8ab2
YQ
7925 enum type_code field_type_code;
7926
7927 field_type_code
7928 = TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type,
7929 i)));
7930
7931 /* Is it a floating point type field? */
7932 if (field_type_code == TYPE_CODE_FLT)
67255d04
RE
7933 {
7934 nRc = 1;
7935 break;
7936 }
b13c8ab2
YQ
7937
7938 /* If bitpos != 0, then we have to care about it. */
7939 if (TYPE_FIELD_BITPOS (type, i) != 0)
7940 {
7941 /* Bitfields are not addressable. If the field bitsize is
7942 zero, then the field is not packed. Hence it cannot be
7943 a bitfield or any other packed type. */
7944 if (TYPE_FIELD_BITSIZE (type, i) == 0)
7945 {
7946 nRc = 1;
7947 break;
7948 }
7949 }
67255d04
RE
7950 }
7951 }
67255d04 7952
b13c8ab2
YQ
7953 return nRc;
7954 }
67255d04
RE
7955}
7956
34e8f22d
RE
7957/* Write into appropriate registers a function return value of type
7958 TYPE, given in virtual format. */
7959
7960static void
b508a996 7961arm_store_return_value (struct type *type, struct regcache *regs,
5238cf52 7962 const gdb_byte *valbuf)
34e8f22d 7963{
be8626e0 7964 struct gdbarch *gdbarch = get_regcache_arch (regs);
e17a4113 7965 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
be8626e0 7966
34e8f22d
RE
7967 if (TYPE_CODE (type) == TYPE_CODE_FLT)
7968 {
e362b510 7969 gdb_byte buf[MAX_REGISTER_SIZE];
34e8f22d 7970
be8626e0 7971 switch (gdbarch_tdep (gdbarch)->fp_model)
08216dd7
RE
7972 {
7973 case ARM_FLOAT_FPA:
7974
be8626e0
MD
7975 convert_to_extended (floatformat_from_type (type), buf, valbuf,
7976 gdbarch_byte_order (gdbarch));
b508a996 7977 regcache_cooked_write (regs, ARM_F0_REGNUM, buf);
08216dd7
RE
7978 break;
7979
fd50bc42 7980 case ARM_FLOAT_SOFT_FPA:
08216dd7 7981 case ARM_FLOAT_SOFT_VFP:
90445bd3
DJ
7982 /* ARM_FLOAT_VFP can arise if this is a variadic function so
7983 not using the VFP ABI code. */
7984 case ARM_FLOAT_VFP:
b508a996
RE
7985 regcache_cooked_write (regs, ARM_A1_REGNUM, valbuf);
7986 if (TYPE_LENGTH (type) > 4)
7987 regcache_cooked_write (regs, ARM_A1_REGNUM + 1,
7a5ea0d4 7988 valbuf + INT_REGISTER_SIZE);
08216dd7
RE
7989 break;
7990
7991 default:
9b20d036
MS
7992 internal_error (__FILE__, __LINE__,
7993 _("arm_store_return_value: Floating "
7994 "point model not supported"));
08216dd7
RE
7995 break;
7996 }
34e8f22d 7997 }
b508a996
RE
7998 else if (TYPE_CODE (type) == TYPE_CODE_INT
7999 || TYPE_CODE (type) == TYPE_CODE_CHAR
8000 || TYPE_CODE (type) == TYPE_CODE_BOOL
8001 || TYPE_CODE (type) == TYPE_CODE_PTR
8002 || TYPE_CODE (type) == TYPE_CODE_REF
8003 || TYPE_CODE (type) == TYPE_CODE_ENUM)
8004 {
8005 if (TYPE_LENGTH (type) <= 4)
8006 {
8007 /* Values of one word or less are zero/sign-extended and
8008 returned in r0. */
7a5ea0d4 8009 bfd_byte tmpbuf[INT_REGISTER_SIZE];
b508a996
RE
8010 LONGEST val = unpack_long (type, valbuf);
8011
e17a4113 8012 store_signed_integer (tmpbuf, INT_REGISTER_SIZE, byte_order, val);
b508a996
RE
8013 regcache_cooked_write (regs, ARM_A1_REGNUM, tmpbuf);
8014 }
8015 else
8016 {
8017 /* Integral values greater than one word are stored in consecutive
8018 registers starting with r0. This will always be a multiple of
8019 the regiser size. */
8020 int len = TYPE_LENGTH (type);
8021 int regno = ARM_A1_REGNUM;
8022
8023 while (len > 0)
8024 {
8025 regcache_cooked_write (regs, regno++, valbuf);
7a5ea0d4
DJ
8026 len -= INT_REGISTER_SIZE;
8027 valbuf += INT_REGISTER_SIZE;
b508a996
RE
8028 }
8029 }
8030 }
34e8f22d 8031 else
b508a996
RE
8032 {
8033 /* For a structure or union the behaviour is as if the value had
8034 been stored to word-aligned memory and then loaded into
8035 registers with 32-bit load instruction(s). */
8036 int len = TYPE_LENGTH (type);
8037 int regno = ARM_A1_REGNUM;
7a5ea0d4 8038 bfd_byte tmpbuf[INT_REGISTER_SIZE];
b508a996
RE
8039
8040 while (len > 0)
8041 {
8042 memcpy (tmpbuf, valbuf,
7a5ea0d4 8043 len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
b508a996 8044 regcache_cooked_write (regs, regno++, tmpbuf);
7a5ea0d4
DJ
8045 len -= INT_REGISTER_SIZE;
8046 valbuf += INT_REGISTER_SIZE;
b508a996
RE
8047 }
8048 }
34e8f22d
RE
8049}
8050
2af48f68
PB
8051
8052/* Handle function return values. */
8053
8054static enum return_value_convention
6a3a010b 8055arm_return_value (struct gdbarch *gdbarch, struct value *function,
c055b101
CV
8056 struct type *valtype, struct regcache *regcache,
8057 gdb_byte *readbuf, const gdb_byte *writebuf)
2af48f68 8058{
7c00367c 8059 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
6a3a010b 8060 struct type *func_type = function ? value_type (function) : NULL;
90445bd3
DJ
8061 enum arm_vfp_cprc_base_type vfp_base_type;
8062 int vfp_base_count;
8063
8064 if (arm_vfp_abi_for_function (gdbarch, func_type)
8065 && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
8066 {
8067 int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
8068 int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
8069 int i;
8070 for (i = 0; i < vfp_base_count; i++)
8071 {
58d6951d
DJ
8072 if (reg_char == 'q')
8073 {
8074 if (writebuf)
8075 arm_neon_quad_write (gdbarch, regcache, i,
8076 writebuf + i * unit_length);
8077
8078 if (readbuf)
8079 arm_neon_quad_read (gdbarch, regcache, i,
8080 readbuf + i * unit_length);
8081 }
8082 else
8083 {
8084 char name_buf[4];
8085 int regnum;
8086
8c042590 8087 xsnprintf (name_buf, sizeof (name_buf), "%c%d", reg_char, i);
58d6951d
DJ
8088 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8089 strlen (name_buf));
8090 if (writebuf)
8091 regcache_cooked_write (regcache, regnum,
8092 writebuf + i * unit_length);
8093 if (readbuf)
8094 regcache_cooked_read (regcache, regnum,
8095 readbuf + i * unit_length);
8096 }
90445bd3
DJ
8097 }
8098 return RETURN_VALUE_REGISTER_CONVENTION;
8099 }
7c00367c 8100
2af48f68
PB
8101 if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
8102 || TYPE_CODE (valtype) == TYPE_CODE_UNION
8103 || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
8104 {
7c00367c
MK
8105 if (tdep->struct_return == pcc_struct_return
8106 || arm_return_in_memory (gdbarch, valtype))
2af48f68
PB
8107 return RETURN_VALUE_STRUCT_CONVENTION;
8108 }
b13c8ab2
YQ
8109 else if (TYPE_CODE (valtype) == TYPE_CODE_COMPLEX)
8110 {
8111 if (arm_return_in_memory (gdbarch, valtype))
8112 return RETURN_VALUE_STRUCT_CONVENTION;
8113 }
7052e42c 8114
2af48f68
PB
8115 if (writebuf)
8116 arm_store_return_value (valtype, regcache, writebuf);
8117
8118 if (readbuf)
8119 arm_extract_return_value (valtype, regcache, readbuf);
8120
8121 return RETURN_VALUE_REGISTER_CONVENTION;
8122}
8123
8124
9df628e0 8125static int
60ade65d 8126arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
9df628e0 8127{
e17a4113
UW
8128 struct gdbarch *gdbarch = get_frame_arch (frame);
8129 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
8130 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
9df628e0 8131 CORE_ADDR jb_addr;
e362b510 8132 gdb_byte buf[INT_REGISTER_SIZE];
9df628e0 8133
60ade65d 8134 jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
9df628e0
RE
8135
8136 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
7a5ea0d4 8137 INT_REGISTER_SIZE))
9df628e0
RE
8138 return 0;
8139
e17a4113 8140 *pc = extract_unsigned_integer (buf, INT_REGISTER_SIZE, byte_order);
9df628e0
RE
8141 return 1;
8142}
8143
faa95490
DJ
8144/* Recognize GCC and GNU ld's trampolines. If we are in a trampoline,
8145 return the target PC. Otherwise return 0. */
c906108c
SS
8146
8147CORE_ADDR
52f729a7 8148arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
c906108c 8149{
2c02bd72 8150 const char *name;
faa95490 8151 int namelen;
c906108c
SS
8152 CORE_ADDR start_addr;
8153
8154 /* Find the starting address and name of the function containing the PC. */
8155 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
80d8d390
YQ
8156 {
8157 /* Trampoline 'bx reg' doesn't belong to any functions. Do the
8158 check here. */
8159 start_addr = arm_skip_bx_reg (frame, pc);
8160 if (start_addr != 0)
8161 return start_addr;
8162
8163 return 0;
8164 }
c906108c 8165
faa95490
DJ
8166 /* If PC is in a Thumb call or return stub, return the address of the
8167 target PC, which is in a register. The thunk functions are called
8168 _call_via_xx, where x is the register name. The possible names
3d8d5e79
DJ
8169 are r0-r9, sl, fp, ip, sp, and lr. ARM RealView has similar
8170 functions, named __ARM_call_via_r[0-7]. */
61012eef
GB
8171 if (startswith (name, "_call_via_")
8172 || startswith (name, "__ARM_call_via_"))
c906108c 8173 {
ed9a39eb
JM
8174 /* Use the name suffix to determine which register contains the
8175 target PC. */
c5aa993b
JM
8176 static char *table[15] =
8177 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
8178 "r8", "r9", "sl", "fp", "ip", "sp", "lr"
8179 };
c906108c 8180 int regno;
faa95490 8181 int offset = strlen (name) - 2;
c906108c
SS
8182
8183 for (regno = 0; regno <= 14; regno++)
faa95490 8184 if (strcmp (&name[offset], table[regno]) == 0)
52f729a7 8185 return get_frame_register_unsigned (frame, regno);
c906108c 8186 }
ed9a39eb 8187
faa95490
DJ
8188 /* GNU ld generates __foo_from_arm or __foo_from_thumb for
8189 non-interworking calls to foo. We could decode the stubs
8190 to find the target but it's easier to use the symbol table. */
8191 namelen = strlen (name);
8192 if (name[0] == '_' && name[1] == '_'
8193 && ((namelen > 2 + strlen ("_from_thumb")
61012eef 8194 && startswith (name + namelen - strlen ("_from_thumb"), "_from_thumb"))
faa95490 8195 || (namelen > 2 + strlen ("_from_arm")
61012eef 8196 && startswith (name + namelen - strlen ("_from_arm"), "_from_arm"))))
faa95490
DJ
8197 {
8198 char *target_name;
8199 int target_len = namelen - 2;
3b7344d5 8200 struct bound_minimal_symbol minsym;
faa95490
DJ
8201 struct objfile *objfile;
8202 struct obj_section *sec;
8203
8204 if (name[namelen - 1] == 'b')
8205 target_len -= strlen ("_from_thumb");
8206 else
8207 target_len -= strlen ("_from_arm");
8208
224c3ddb 8209 target_name = (char *) alloca (target_len + 1);
faa95490
DJ
8210 memcpy (target_name, name + 2, target_len);
8211 target_name[target_len] = '\0';
8212
8213 sec = find_pc_section (pc);
8214 objfile = (sec == NULL) ? NULL : sec->objfile;
8215 minsym = lookup_minimal_symbol (target_name, NULL, objfile);
3b7344d5 8216 if (minsym.minsym != NULL)
77e371c0 8217 return BMSYMBOL_VALUE_ADDRESS (minsym);
faa95490
DJ
8218 else
8219 return 0;
8220 }
8221
c5aa993b 8222 return 0; /* not a stub */
c906108c
SS
8223}
8224
afd7eef0
RE
8225static void
8226set_arm_command (char *args, int from_tty)
8227{
edefbb7c
AC
8228 printf_unfiltered (_("\
8229\"set arm\" must be followed by an apporpriate subcommand.\n"));
afd7eef0
RE
8230 help_list (setarmcmdlist, "set arm ", all_commands, gdb_stdout);
8231}
8232
8233static void
8234show_arm_command (char *args, int from_tty)
8235{
26304000 8236 cmd_show_list (showarmcmdlist, from_tty, "");
afd7eef0
RE
8237}
8238
28e97307
DJ
8239static void
8240arm_update_current_architecture (void)
fd50bc42 8241{
28e97307 8242 struct gdbarch_info info;
fd50bc42 8243
28e97307 8244 /* If the current architecture is not ARM, we have nothing to do. */
f5656ead 8245 if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_arm)
28e97307 8246 return;
fd50bc42 8247
28e97307
DJ
8248 /* Update the architecture. */
8249 gdbarch_info_init (&info);
fd50bc42 8250
28e97307 8251 if (!gdbarch_update_p (info))
9b20d036 8252 internal_error (__FILE__, __LINE__, _("could not update architecture"));
fd50bc42
RE
8253}
8254
8255static void
8256set_fp_model_sfunc (char *args, int from_tty,
8257 struct cmd_list_element *c)
8258{
570dc176 8259 int fp_model;
fd50bc42
RE
8260
8261 for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
8262 if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
8263 {
aead7601 8264 arm_fp_model = (enum arm_float_model) fp_model;
fd50bc42
RE
8265 break;
8266 }
8267
8268 if (fp_model == ARM_FLOAT_LAST)
edefbb7c 8269 internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
fd50bc42
RE
8270 current_fp_model);
8271
28e97307 8272 arm_update_current_architecture ();
fd50bc42
RE
8273}
8274
8275static void
08546159
AC
8276show_fp_model (struct ui_file *file, int from_tty,
8277 struct cmd_list_element *c, const char *value)
fd50bc42 8278{
f5656ead 8279 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
fd50bc42 8280
28e97307 8281 if (arm_fp_model == ARM_FLOAT_AUTO
f5656ead 8282 && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
28e97307
DJ
8283 fprintf_filtered (file, _("\
8284The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
8285 fp_model_strings[tdep->fp_model]);
8286 else
8287 fprintf_filtered (file, _("\
8288The current ARM floating point model is \"%s\".\n"),
8289 fp_model_strings[arm_fp_model]);
8290}
8291
8292static void
8293arm_set_abi (char *args, int from_tty,
8294 struct cmd_list_element *c)
8295{
570dc176 8296 int arm_abi;
28e97307
DJ
8297
8298 for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
8299 if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
8300 {
aead7601 8301 arm_abi_global = (enum arm_abi_kind) arm_abi;
28e97307
DJ
8302 break;
8303 }
8304
8305 if (arm_abi == ARM_ABI_LAST)
8306 internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
8307 arm_abi_string);
8308
8309 arm_update_current_architecture ();
8310}
8311
8312static void
8313arm_show_abi (struct ui_file *file, int from_tty,
8314 struct cmd_list_element *c, const char *value)
8315{
f5656ead 8316 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
28e97307
DJ
8317
8318 if (arm_abi_global == ARM_ABI_AUTO
f5656ead 8319 && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
28e97307
DJ
8320 fprintf_filtered (file, _("\
8321The current ARM ABI is \"auto\" (currently \"%s\").\n"),
8322 arm_abi_strings[tdep->arm_abi]);
8323 else
8324 fprintf_filtered (file, _("The current ARM ABI is \"%s\".\n"),
8325 arm_abi_string);
fd50bc42
RE
8326}
8327
0428b8f5
DJ
8328static void
8329arm_show_fallback_mode (struct ui_file *file, int from_tty,
8330 struct cmd_list_element *c, const char *value)
8331{
0963b4bd
MS
8332 fprintf_filtered (file,
8333 _("The current execution mode assumed "
8334 "(when symbols are unavailable) is \"%s\".\n"),
0428b8f5
DJ
8335 arm_fallback_mode_string);
8336}
8337
8338static void
8339arm_show_force_mode (struct ui_file *file, int from_tty,
8340 struct cmd_list_element *c, const char *value)
8341{
0963b4bd
MS
8342 fprintf_filtered (file,
8343 _("The current execution mode assumed "
8344 "(even when symbols are available) is \"%s\".\n"),
0428b8f5
DJ
8345 arm_force_mode_string);
8346}
8347
afd7eef0
RE
8348/* If the user changes the register disassembly style used for info
8349 register and other commands, we have to also switch the style used
8350 in opcodes for disassembly output. This function is run in the "set
8351 arm disassembly" command, and does that. */
bc90b915
FN
8352
8353static void
afd7eef0 8354set_disassembly_style_sfunc (char *args, int from_tty,
bc90b915
FN
8355 struct cmd_list_element *c)
8356{
afd7eef0 8357 set_disassembly_style ();
bc90b915
FN
8358}
8359\f
966fbf70 8360/* Return the ARM register name corresponding to register I. */
a208b0cb 8361static const char *
d93859e2 8362arm_register_name (struct gdbarch *gdbarch, int i)
966fbf70 8363{
58d6951d
DJ
8364 const int num_regs = gdbarch_num_regs (gdbarch);
8365
8366 if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
8367 && i >= num_regs && i < num_regs + 32)
8368 {
8369 static const char *const vfp_pseudo_names[] = {
8370 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
8371 "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
8372 "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
8373 "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
8374 };
8375
8376 return vfp_pseudo_names[i - num_regs];
8377 }
8378
8379 if (gdbarch_tdep (gdbarch)->have_neon_pseudos
8380 && i >= num_regs + 32 && i < num_regs + 32 + 16)
8381 {
8382 static const char *const neon_pseudo_names[] = {
8383 "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
8384 "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
8385 };
8386
8387 return neon_pseudo_names[i - num_regs - 32];
8388 }
8389
ff6f572f
DJ
8390 if (i >= ARRAY_SIZE (arm_register_names))
8391 /* These registers are only supported on targets which supply
8392 an XML description. */
8393 return "";
8394
966fbf70
RE
8395 return arm_register_names[i];
8396}
8397
bc90b915 8398static void
afd7eef0 8399set_disassembly_style (void)
bc90b915 8400{
123dc839 8401 int current;
bc90b915 8402
123dc839
DJ
8403 /* Find the style that the user wants. */
8404 for (current = 0; current < num_disassembly_options; current++)
8405 if (disassembly_style == valid_disassembly_styles[current])
8406 break;
8407 gdb_assert (current < num_disassembly_options);
bc90b915 8408
94c30b78 8409 /* Synchronize the disassembler. */
bc90b915
FN
8410 set_arm_regname_option (current);
8411}
8412
082fc60d
RE
8413/* Test whether the coff symbol specific value corresponds to a Thumb
8414 function. */
8415
8416static int
8417coff_sym_is_thumb (int val)
8418{
f8bf5763
PM
8419 return (val == C_THUMBEXT
8420 || val == C_THUMBSTAT
8421 || val == C_THUMBEXTFUNC
8422 || val == C_THUMBSTATFUNC
8423 || val == C_THUMBLABEL);
082fc60d
RE
8424}
8425
8426/* arm_coff_make_msymbol_special()
8427 arm_elf_make_msymbol_special()
8428
8429 These functions test whether the COFF or ELF symbol corresponds to
8430 an address in thumb code, and set a "special" bit in a minimal
8431 symbol to indicate that it does. */
8432
34e8f22d 8433static void
082fc60d
RE
8434arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
8435{
467d42c4
UW
8436 if (ARM_SYM_BRANCH_TYPE (&((elf_symbol_type *)sym)->internal_elf_sym)
8437 == ST_BRANCH_TO_THUMB)
082fc60d
RE
8438 MSYMBOL_SET_SPECIAL (msym);
8439}
8440
34e8f22d 8441static void
082fc60d
RE
8442arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
8443{
8444 if (coff_sym_is_thumb (val))
8445 MSYMBOL_SET_SPECIAL (msym);
8446}
8447
60c5725c 8448static void
c1bd65d0 8449arm_objfile_data_free (struct objfile *objfile, void *arg)
60c5725c 8450{
9a3c8263 8451 struct arm_per_objfile *data = (struct arm_per_objfile *) arg;
60c5725c
DJ
8452 unsigned int i;
8453
8454 for (i = 0; i < objfile->obfd->section_count; i++)
8455 VEC_free (arm_mapping_symbol_s, data->section_maps[i]);
8456}
8457
8458static void
8459arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
8460 asymbol *sym)
8461{
8462 const char *name = bfd_asymbol_name (sym);
8463 struct arm_per_objfile *data;
8464 VEC(arm_mapping_symbol_s) **map_p;
8465 struct arm_mapping_symbol new_map_sym;
8466
8467 gdb_assert (name[0] == '$');
8468 if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
8469 return;
8470
9a3c8263
SM
8471 data = (struct arm_per_objfile *) objfile_data (objfile,
8472 arm_objfile_data_key);
60c5725c
DJ
8473 if (data == NULL)
8474 {
8475 data = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8476 struct arm_per_objfile);
8477 set_objfile_data (objfile, arm_objfile_data_key, data);
8478 data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
8479 objfile->obfd->section_count,
8480 VEC(arm_mapping_symbol_s) *);
8481 }
8482 map_p = &data->section_maps[bfd_get_section (sym)->index];
8483
8484 new_map_sym.value = sym->value;
8485 new_map_sym.type = name[1];
8486
8487 /* Assume that most mapping symbols appear in order of increasing
8488 value. If they were randomly distributed, it would be faster to
8489 always push here and then sort at first use. */
8490 if (!VEC_empty (arm_mapping_symbol_s, *map_p))
8491 {
8492 struct arm_mapping_symbol *prev_map_sym;
8493
8494 prev_map_sym = VEC_last (arm_mapping_symbol_s, *map_p);
8495 if (prev_map_sym->value >= sym->value)
8496 {
8497 unsigned int idx;
8498 idx = VEC_lower_bound (arm_mapping_symbol_s, *map_p, &new_map_sym,
8499 arm_compare_mapping_symbols);
8500 VEC_safe_insert (arm_mapping_symbol_s, *map_p, idx, &new_map_sym);
8501 return;
8502 }
8503 }
8504
8505 VEC_safe_push (arm_mapping_symbol_s, *map_p, &new_map_sym);
8506}
8507
756fe439 8508static void
61a1198a 8509arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
756fe439 8510{
9779414d 8511 struct gdbarch *gdbarch = get_regcache_arch (regcache);
61a1198a 8512 regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
756fe439
DJ
8513
8514 /* If necessary, set the T bit. */
8515 if (arm_apcs_32)
8516 {
9779414d 8517 ULONGEST val, t_bit;
61a1198a 8518 regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
9779414d
DJ
8519 t_bit = arm_psr_thumb_bit (gdbarch);
8520 if (arm_pc_is_thumb (gdbarch, pc))
8521 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
8522 val | t_bit);
756fe439 8523 else
61a1198a 8524 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9779414d 8525 val & ~t_bit);
756fe439
DJ
8526 }
8527}
123dc839 8528
58d6951d
DJ
8529/* Read the contents of a NEON quad register, by reading from two
8530 double registers. This is used to implement the quad pseudo
8531 registers, and for argument passing in case the quad registers are
8532 missing; vectors are passed in quad registers when using the VFP
8533 ABI, even if a NEON unit is not present. REGNUM is the index of
8534 the quad register, in [0, 15]. */
8535
05d1431c 8536static enum register_status
58d6951d
DJ
8537arm_neon_quad_read (struct gdbarch *gdbarch, struct regcache *regcache,
8538 int regnum, gdb_byte *buf)
8539{
8540 char name_buf[4];
8541 gdb_byte reg_buf[8];
8542 int offset, double_regnum;
05d1431c 8543 enum register_status status;
58d6951d 8544
8c042590 8545 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
58d6951d
DJ
8546 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8547 strlen (name_buf));
8548
8549 /* d0 is always the least significant half of q0. */
8550 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
8551 offset = 8;
8552 else
8553 offset = 0;
8554
05d1431c
PA
8555 status = regcache_raw_read (regcache, double_regnum, reg_buf);
8556 if (status != REG_VALID)
8557 return status;
58d6951d
DJ
8558 memcpy (buf + offset, reg_buf, 8);
8559
8560 offset = 8 - offset;
05d1431c
PA
8561 status = regcache_raw_read (regcache, double_regnum + 1, reg_buf);
8562 if (status != REG_VALID)
8563 return status;
58d6951d 8564 memcpy (buf + offset, reg_buf, 8);
05d1431c
PA
8565
8566 return REG_VALID;
58d6951d
DJ
8567}
8568
05d1431c 8569static enum register_status
58d6951d
DJ
8570arm_pseudo_read (struct gdbarch *gdbarch, struct regcache *regcache,
8571 int regnum, gdb_byte *buf)
8572{
8573 const int num_regs = gdbarch_num_regs (gdbarch);
8574 char name_buf[4];
8575 gdb_byte reg_buf[8];
8576 int offset, double_regnum;
8577
8578 gdb_assert (regnum >= num_regs);
8579 regnum -= num_regs;
8580
8581 if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
8582 /* Quad-precision register. */
05d1431c 8583 return arm_neon_quad_read (gdbarch, regcache, regnum - 32, buf);
58d6951d
DJ
8584 else
8585 {
05d1431c
PA
8586 enum register_status status;
8587
58d6951d
DJ
8588 /* Single-precision register. */
8589 gdb_assert (regnum < 32);
8590
8591 /* s0 is always the least significant half of d0. */
8592 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
8593 offset = (regnum & 1) ? 0 : 4;
8594 else
8595 offset = (regnum & 1) ? 4 : 0;
8596
8c042590 8597 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
58d6951d
DJ
8598 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8599 strlen (name_buf));
8600
05d1431c
PA
8601 status = regcache_raw_read (regcache, double_regnum, reg_buf);
8602 if (status == REG_VALID)
8603 memcpy (buf, reg_buf + offset, 4);
8604 return status;
58d6951d
DJ
8605 }
8606}
8607
8608/* Store the contents of BUF to a NEON quad register, by writing to
8609 two double registers. This is used to implement the quad pseudo
8610 registers, and for argument passing in case the quad registers are
8611 missing; vectors are passed in quad registers when using the VFP
8612 ABI, even if a NEON unit is not present. REGNUM is the index
8613 of the quad register, in [0, 15]. */
8614
8615static void
8616arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
8617 int regnum, const gdb_byte *buf)
8618{
8619 char name_buf[4];
58d6951d
DJ
8620 int offset, double_regnum;
8621
8c042590 8622 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
58d6951d
DJ
8623 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8624 strlen (name_buf));
8625
8626 /* d0 is always the least significant half of q0. */
8627 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
8628 offset = 8;
8629 else
8630 offset = 0;
8631
8632 regcache_raw_write (regcache, double_regnum, buf + offset);
8633 offset = 8 - offset;
8634 regcache_raw_write (regcache, double_regnum + 1, buf + offset);
8635}
8636
8637static void
8638arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
8639 int regnum, const gdb_byte *buf)
8640{
8641 const int num_regs = gdbarch_num_regs (gdbarch);
8642 char name_buf[4];
8643 gdb_byte reg_buf[8];
8644 int offset, double_regnum;
8645
8646 gdb_assert (regnum >= num_regs);
8647 regnum -= num_regs;
8648
8649 if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
8650 /* Quad-precision register. */
8651 arm_neon_quad_write (gdbarch, regcache, regnum - 32, buf);
8652 else
8653 {
8654 /* Single-precision register. */
8655 gdb_assert (regnum < 32);
8656
8657 /* s0 is always the least significant half of d0. */
8658 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
8659 offset = (regnum & 1) ? 0 : 4;
8660 else
8661 offset = (regnum & 1) ? 4 : 0;
8662
8c042590 8663 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
58d6951d
DJ
8664 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8665 strlen (name_buf));
8666
8667 regcache_raw_read (regcache, double_regnum, reg_buf);
8668 memcpy (reg_buf + offset, buf, 4);
8669 regcache_raw_write (regcache, double_regnum, reg_buf);
8670 }
8671}
8672
123dc839
DJ
8673static struct value *
8674value_of_arm_user_reg (struct frame_info *frame, const void *baton)
8675{
9a3c8263 8676 const int *reg_p = (const int *) baton;
123dc839
DJ
8677 return value_of_register (*reg_p, frame);
8678}
97e03143 8679\f
70f80edf
JT
8680static enum gdb_osabi
8681arm_elf_osabi_sniffer (bfd *abfd)
97e03143 8682{
2af48f68 8683 unsigned int elfosabi;
70f80edf 8684 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
97e03143 8685
70f80edf 8686 elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
97e03143 8687
28e97307
DJ
8688 if (elfosabi == ELFOSABI_ARM)
8689 /* GNU tools use this value. Check note sections in this case,
8690 as well. */
8691 bfd_map_over_sections (abfd,
8692 generic_elf_osabi_sniff_abi_tag_sections,
8693 &osabi);
97e03143 8694
28e97307 8695 /* Anything else will be handled by the generic ELF sniffer. */
70f80edf 8696 return osabi;
97e03143
RE
8697}
8698
54483882
YQ
8699static int
8700arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
8701 struct reggroup *group)
8702{
2c291032
YQ
8703 /* FPS register's type is INT, but belongs to float_reggroup. Beside
8704 this, FPS register belongs to save_regroup, restore_reggroup, and
8705 all_reggroup, of course. */
54483882 8706 if (regnum == ARM_FPS_REGNUM)
2c291032
YQ
8707 return (group == float_reggroup
8708 || group == save_reggroup
8709 || group == restore_reggroup
8710 || group == all_reggroup);
54483882
YQ
8711 else
8712 return default_register_reggroup_p (gdbarch, regnum, group);
8713}
8714
25f8c692
JL
8715\f
8716/* For backward-compatibility we allow two 'g' packet lengths with
8717 the remote protocol depending on whether FPA registers are
8718 supplied. M-profile targets do not have FPA registers, but some
8719 stubs already exist in the wild which use a 'g' packet which
8720 supplies them albeit with dummy values. The packet format which
8721 includes FPA registers should be considered deprecated for
8722 M-profile targets. */
8723
8724static void
8725arm_register_g_packet_guesses (struct gdbarch *gdbarch)
8726{
8727 if (gdbarch_tdep (gdbarch)->is_m)
8728 {
8729 /* If we know from the executable this is an M-profile target,
8730 cater for remote targets whose register set layout is the
8731 same as the FPA layout. */
8732 register_remote_g_packet_guess (gdbarch,
03145bf4 8733 /* r0-r12,sp,lr,pc; f0-f7; fps,xpsr */
25f8c692
JL
8734 (16 * INT_REGISTER_SIZE)
8735 + (8 * FP_REGISTER_SIZE)
8736 + (2 * INT_REGISTER_SIZE),
8737 tdesc_arm_with_m_fpa_layout);
8738
8739 /* The regular M-profile layout. */
8740 register_remote_g_packet_guess (gdbarch,
8741 /* r0-r12,sp,lr,pc; xpsr */
8742 (16 * INT_REGISTER_SIZE)
8743 + INT_REGISTER_SIZE,
8744 tdesc_arm_with_m);
3184d3f9
JL
8745
8746 /* M-profile plus M4F VFP. */
8747 register_remote_g_packet_guess (gdbarch,
8748 /* r0-r12,sp,lr,pc; d0-d15; fpscr,xpsr */
8749 (16 * INT_REGISTER_SIZE)
8750 + (16 * VFP_REGISTER_SIZE)
8751 + (2 * INT_REGISTER_SIZE),
8752 tdesc_arm_with_m_vfp_d16);
25f8c692
JL
8753 }
8754
8755 /* Otherwise we don't have a useful guess. */
8756}
8757
70f80edf 8758\f
da3c6d4a
MS
8759/* Initialize the current architecture based on INFO. If possible,
8760 re-use an architecture from ARCHES, which is a list of
8761 architectures already created during this debugging session.
97e03143 8762
da3c6d4a
MS
8763 Called e.g. at program startup, when reading a core file, and when
8764 reading a binary file. */
97e03143 8765
39bbf761
RE
8766static struct gdbarch *
8767arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
8768{
97e03143 8769 struct gdbarch_tdep *tdep;
39bbf761 8770 struct gdbarch *gdbarch;
28e97307
DJ
8771 struct gdbarch_list *best_arch;
8772 enum arm_abi_kind arm_abi = arm_abi_global;
8773 enum arm_float_model fp_model = arm_fp_model;
123dc839 8774 struct tdesc_arch_data *tdesc_data = NULL;
9779414d 8775 int i, is_m = 0;
330c6ca9 8776 int vfp_register_count = 0, have_vfp_pseudos = 0, have_neon_pseudos = 0;
a56cc1ce 8777 int have_wmmx_registers = 0;
58d6951d 8778 int have_neon = 0;
ff6f572f 8779 int have_fpa_registers = 1;
9779414d
DJ
8780 const struct target_desc *tdesc = info.target_desc;
8781
8782 /* If we have an object to base this architecture on, try to determine
8783 its ABI. */
8784
8785 if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
8786 {
8787 int ei_osabi, e_flags;
8788
8789 switch (bfd_get_flavour (info.abfd))
8790 {
8791 case bfd_target_aout_flavour:
8792 /* Assume it's an old APCS-style ABI. */
8793 arm_abi = ARM_ABI_APCS;
8794 break;
8795
8796 case bfd_target_coff_flavour:
8797 /* Assume it's an old APCS-style ABI. */
8798 /* XXX WinCE? */
8799 arm_abi = ARM_ABI_APCS;
8800 break;
8801
8802 case bfd_target_elf_flavour:
8803 ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
8804 e_flags = elf_elfheader (info.abfd)->e_flags;
8805
8806 if (ei_osabi == ELFOSABI_ARM)
8807 {
8808 /* GNU tools used to use this value, but do not for EABI
8809 objects. There's nowhere to tag an EABI version
8810 anyway, so assume APCS. */
8811 arm_abi = ARM_ABI_APCS;
8812 }
d403db27 8813 else if (ei_osabi == ELFOSABI_NONE || ei_osabi == ELFOSABI_GNU)
9779414d
DJ
8814 {
8815 int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
8816 int attr_arch, attr_profile;
8817
8818 switch (eabi_ver)
8819 {
8820 case EF_ARM_EABI_UNKNOWN:
8821 /* Assume GNU tools. */
8822 arm_abi = ARM_ABI_APCS;
8823 break;
8824
8825 case EF_ARM_EABI_VER4:
8826 case EF_ARM_EABI_VER5:
8827 arm_abi = ARM_ABI_AAPCS;
8828 /* EABI binaries default to VFP float ordering.
8829 They may also contain build attributes that can
8830 be used to identify if the VFP argument-passing
8831 ABI is in use. */
8832 if (fp_model == ARM_FLOAT_AUTO)
8833 {
8834#ifdef HAVE_ELF
8835 switch (bfd_elf_get_obj_attr_int (info.abfd,
8836 OBJ_ATTR_PROC,
8837 Tag_ABI_VFP_args))
8838 {
b35b0298 8839 case AEABI_VFP_args_base:
9779414d
DJ
8840 /* "The user intended FP parameter/result
8841 passing to conform to AAPCS, base
8842 variant". */
8843 fp_model = ARM_FLOAT_SOFT_VFP;
8844 break;
b35b0298 8845 case AEABI_VFP_args_vfp:
9779414d
DJ
8846 /* "The user intended FP parameter/result
8847 passing to conform to AAPCS, VFP
8848 variant". */
8849 fp_model = ARM_FLOAT_VFP;
8850 break;
b35b0298 8851 case AEABI_VFP_args_toolchain:
9779414d
DJ
8852 /* "The user intended FP parameter/result
8853 passing to conform to tool chain-specific
8854 conventions" - we don't know any such
8855 conventions, so leave it as "auto". */
8856 break;
b35b0298 8857 case AEABI_VFP_args_compatible:
5c294fee
TG
8858 /* "Code is compatible with both the base
8859 and VFP variants; the user did not permit
8860 non-variadic functions to pass FP
8861 parameters/results" - leave it as
8862 "auto". */
8863 break;
9779414d
DJ
8864 default:
8865 /* Attribute value not mentioned in the
5c294fee 8866 November 2012 ABI, so leave it as
9779414d
DJ
8867 "auto". */
8868 break;
8869 }
8870#else
8871 fp_model = ARM_FLOAT_SOFT_VFP;
8872#endif
8873 }
8874 break;
8875
8876 default:
8877 /* Leave it as "auto". */
8878 warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
8879 break;
8880 }
8881
8882#ifdef HAVE_ELF
8883 /* Detect M-profile programs. This only works if the
8884 executable file includes build attributes; GCC does
8885 copy them to the executable, but e.g. RealView does
8886 not. */
8887 attr_arch = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
8888 Tag_CPU_arch);
0963b4bd
MS
8889 attr_profile = bfd_elf_get_obj_attr_int (info.abfd,
8890 OBJ_ATTR_PROC,
9779414d
DJ
8891 Tag_CPU_arch_profile);
8892 /* GCC specifies the profile for v6-M; RealView only
8893 specifies the profile for architectures starting with
8894 V7 (as opposed to architectures with a tag
8895 numerically greater than TAG_CPU_ARCH_V7). */
8896 if (!tdesc_has_registers (tdesc)
8897 && (attr_arch == TAG_CPU_ARCH_V6_M
8898 || attr_arch == TAG_CPU_ARCH_V6S_M
8899 || attr_profile == 'M'))
25f8c692 8900 is_m = 1;
9779414d
DJ
8901#endif
8902 }
8903
8904 if (fp_model == ARM_FLOAT_AUTO)
8905 {
8906 int e_flags = elf_elfheader (info.abfd)->e_flags;
8907
8908 switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
8909 {
8910 case 0:
8911 /* Leave it as "auto". Strictly speaking this case
8912 means FPA, but almost nobody uses that now, and
8913 many toolchains fail to set the appropriate bits
8914 for the floating-point model they use. */
8915 break;
8916 case EF_ARM_SOFT_FLOAT:
8917 fp_model = ARM_FLOAT_SOFT_FPA;
8918 break;
8919 case EF_ARM_VFP_FLOAT:
8920 fp_model = ARM_FLOAT_VFP;
8921 break;
8922 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
8923 fp_model = ARM_FLOAT_SOFT_VFP;
8924 break;
8925 }
8926 }
8927
8928 if (e_flags & EF_ARM_BE8)
8929 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
8930
8931 break;
8932
8933 default:
8934 /* Leave it as "auto". */
8935 break;
8936 }
8937 }
123dc839
DJ
8938
8939 /* Check any target description for validity. */
9779414d 8940 if (tdesc_has_registers (tdesc))
123dc839
DJ
8941 {
8942 /* For most registers we require GDB's default names; but also allow
8943 the numeric names for sp / lr / pc, as a convenience. */
8944 static const char *const arm_sp_names[] = { "r13", "sp", NULL };
8945 static const char *const arm_lr_names[] = { "r14", "lr", NULL };
8946 static const char *const arm_pc_names[] = { "r15", "pc", NULL };
8947
8948 const struct tdesc_feature *feature;
58d6951d 8949 int valid_p;
123dc839 8950
9779414d 8951 feature = tdesc_find_feature (tdesc,
123dc839
DJ
8952 "org.gnu.gdb.arm.core");
8953 if (feature == NULL)
9779414d
DJ
8954 {
8955 feature = tdesc_find_feature (tdesc,
8956 "org.gnu.gdb.arm.m-profile");
8957 if (feature == NULL)
8958 return NULL;
8959 else
8960 is_m = 1;
8961 }
123dc839
DJ
8962
8963 tdesc_data = tdesc_data_alloc ();
8964
8965 valid_p = 1;
8966 for (i = 0; i < ARM_SP_REGNUM; i++)
8967 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
8968 arm_register_names[i]);
8969 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
8970 ARM_SP_REGNUM,
8971 arm_sp_names);
8972 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
8973 ARM_LR_REGNUM,
8974 arm_lr_names);
8975 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
8976 ARM_PC_REGNUM,
8977 arm_pc_names);
9779414d
DJ
8978 if (is_m)
8979 valid_p &= tdesc_numbered_register (feature, tdesc_data,
8980 ARM_PS_REGNUM, "xpsr");
8981 else
8982 valid_p &= tdesc_numbered_register (feature, tdesc_data,
8983 ARM_PS_REGNUM, "cpsr");
123dc839
DJ
8984
8985 if (!valid_p)
8986 {
8987 tdesc_data_cleanup (tdesc_data);
8988 return NULL;
8989 }
8990
9779414d 8991 feature = tdesc_find_feature (tdesc,
123dc839
DJ
8992 "org.gnu.gdb.arm.fpa");
8993 if (feature != NULL)
8994 {
8995 valid_p = 1;
8996 for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
8997 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
8998 arm_register_names[i]);
8999 if (!valid_p)
9000 {
9001 tdesc_data_cleanup (tdesc_data);
9002 return NULL;
9003 }
9004 }
ff6f572f
DJ
9005 else
9006 have_fpa_registers = 0;
9007
9779414d 9008 feature = tdesc_find_feature (tdesc,
ff6f572f
DJ
9009 "org.gnu.gdb.xscale.iwmmxt");
9010 if (feature != NULL)
9011 {
9012 static const char *const iwmmxt_names[] = {
9013 "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
9014 "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
9015 "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
9016 "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
9017 };
9018
9019 valid_p = 1;
9020 for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
9021 valid_p
9022 &= tdesc_numbered_register (feature, tdesc_data, i,
9023 iwmmxt_names[i - ARM_WR0_REGNUM]);
9024
9025 /* Check for the control registers, but do not fail if they
9026 are missing. */
9027 for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
9028 tdesc_numbered_register (feature, tdesc_data, i,
9029 iwmmxt_names[i - ARM_WR0_REGNUM]);
9030
9031 for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
9032 valid_p
9033 &= tdesc_numbered_register (feature, tdesc_data, i,
9034 iwmmxt_names[i - ARM_WR0_REGNUM]);
9035
9036 if (!valid_p)
9037 {
9038 tdesc_data_cleanup (tdesc_data);
9039 return NULL;
9040 }
a56cc1ce
YQ
9041
9042 have_wmmx_registers = 1;
ff6f572f 9043 }
58d6951d
DJ
9044
9045 /* If we have a VFP unit, check whether the single precision registers
9046 are present. If not, then we will synthesize them as pseudo
9047 registers. */
9779414d 9048 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
9049 "org.gnu.gdb.arm.vfp");
9050 if (feature != NULL)
9051 {
9052 static const char *const vfp_double_names[] = {
9053 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
9054 "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
9055 "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
9056 "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
9057 };
9058
9059 /* Require the double precision registers. There must be either
9060 16 or 32. */
9061 valid_p = 1;
9062 for (i = 0; i < 32; i++)
9063 {
9064 valid_p &= tdesc_numbered_register (feature, tdesc_data,
9065 ARM_D0_REGNUM + i,
9066 vfp_double_names[i]);
9067 if (!valid_p)
9068 break;
9069 }
2b9e5ea6
UW
9070 if (!valid_p && i == 16)
9071 valid_p = 1;
58d6951d 9072
2b9e5ea6
UW
9073 /* Also require FPSCR. */
9074 valid_p &= tdesc_numbered_register (feature, tdesc_data,
9075 ARM_FPSCR_REGNUM, "fpscr");
9076 if (!valid_p)
58d6951d
DJ
9077 {
9078 tdesc_data_cleanup (tdesc_data);
9079 return NULL;
9080 }
9081
9082 if (tdesc_unnumbered_register (feature, "s0") == 0)
9083 have_vfp_pseudos = 1;
9084
330c6ca9 9085 vfp_register_count = i;
58d6951d
DJ
9086
9087 /* If we have VFP, also check for NEON. The architecture allows
9088 NEON without VFP (integer vector operations only), but GDB
9089 does not support that. */
9779414d 9090 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
9091 "org.gnu.gdb.arm.neon");
9092 if (feature != NULL)
9093 {
9094 /* NEON requires 32 double-precision registers. */
9095 if (i != 32)
9096 {
9097 tdesc_data_cleanup (tdesc_data);
9098 return NULL;
9099 }
9100
9101 /* If there are quad registers defined by the stub, use
9102 their type; otherwise (normally) provide them with
9103 the default type. */
9104 if (tdesc_unnumbered_register (feature, "q0") == 0)
9105 have_neon_pseudos = 1;
9106
9107 have_neon = 1;
9108 }
9109 }
123dc839 9110 }
39bbf761 9111
28e97307
DJ
9112 /* If there is already a candidate, use it. */
9113 for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
9114 best_arch != NULL;
9115 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
9116 {
b8926edc
DJ
9117 if (arm_abi != ARM_ABI_AUTO
9118 && arm_abi != gdbarch_tdep (best_arch->gdbarch)->arm_abi)
28e97307
DJ
9119 continue;
9120
b8926edc
DJ
9121 if (fp_model != ARM_FLOAT_AUTO
9122 && fp_model != gdbarch_tdep (best_arch->gdbarch)->fp_model)
28e97307
DJ
9123 continue;
9124
58d6951d
DJ
9125 /* There are various other properties in tdep that we do not
9126 need to check here: those derived from a target description,
9127 since gdbarches with a different target description are
9128 automatically disqualified. */
9129
9779414d
DJ
9130 /* Do check is_m, though, since it might come from the binary. */
9131 if (is_m != gdbarch_tdep (best_arch->gdbarch)->is_m)
9132 continue;
9133
28e97307
DJ
9134 /* Found a match. */
9135 break;
9136 }
97e03143 9137
28e97307 9138 if (best_arch != NULL)
123dc839
DJ
9139 {
9140 if (tdesc_data != NULL)
9141 tdesc_data_cleanup (tdesc_data);
9142 return best_arch->gdbarch;
9143 }
28e97307 9144
8d749320 9145 tdep = XCNEW (struct gdbarch_tdep);
97e03143
RE
9146 gdbarch = gdbarch_alloc (&info, tdep);
9147
28e97307
DJ
9148 /* Record additional information about the architecture we are defining.
9149 These are gdbarch discriminators, like the OSABI. */
9150 tdep->arm_abi = arm_abi;
9151 tdep->fp_model = fp_model;
9779414d 9152 tdep->is_m = is_m;
ff6f572f 9153 tdep->have_fpa_registers = have_fpa_registers;
a56cc1ce 9154 tdep->have_wmmx_registers = have_wmmx_registers;
330c6ca9
YQ
9155 gdb_assert (vfp_register_count == 0
9156 || vfp_register_count == 16
9157 || vfp_register_count == 32);
9158 tdep->vfp_register_count = vfp_register_count;
58d6951d
DJ
9159 tdep->have_vfp_pseudos = have_vfp_pseudos;
9160 tdep->have_neon_pseudos = have_neon_pseudos;
9161 tdep->have_neon = have_neon;
08216dd7 9162
25f8c692
JL
9163 arm_register_g_packet_guesses (gdbarch);
9164
08216dd7 9165 /* Breakpoints. */
9d4fde75 9166 switch (info.byte_order_for_code)
67255d04
RE
9167 {
9168 case BFD_ENDIAN_BIG:
66e810cd
RE
9169 tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
9170 tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
9171 tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
9172 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
9173
67255d04
RE
9174 break;
9175
9176 case BFD_ENDIAN_LITTLE:
66e810cd
RE
9177 tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
9178 tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
9179 tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
9180 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
9181
67255d04
RE
9182 break;
9183
9184 default:
9185 internal_error (__FILE__, __LINE__,
edefbb7c 9186 _("arm_gdbarch_init: bad byte order for float format"));
67255d04
RE
9187 }
9188
d7b486e7
RE
9189 /* On ARM targets char defaults to unsigned. */
9190 set_gdbarch_char_signed (gdbarch, 0);
9191
cca44b1b
JB
9192 /* Note: for displaced stepping, this includes the breakpoint, and one word
9193 of additional scratch space. This setting isn't used for anything beside
9194 displaced stepping at present. */
9195 set_gdbarch_max_insn_length (gdbarch, 4 * DISPLACED_MODIFIED_INSNS);
9196
9df628e0 9197 /* This should be low enough for everything. */
97e03143 9198 tdep->lowest_pc = 0x20;
94c30b78 9199 tdep->jb_pc = -1; /* Longjump support not enabled by default. */
97e03143 9200
7c00367c
MK
9201 /* The default, for both APCS and AAPCS, is to return small
9202 structures in registers. */
9203 tdep->struct_return = reg_struct_return;
9204
2dd604e7 9205 set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
f53f0d0b 9206 set_gdbarch_frame_align (gdbarch, arm_frame_align);
39bbf761 9207
756fe439
DJ
9208 set_gdbarch_write_pc (gdbarch, arm_write_pc);
9209
148754e5 9210 /* Frame handling. */
a262aec2 9211 set_gdbarch_dummy_id (gdbarch, arm_dummy_id);
eb5492fa
DJ
9212 set_gdbarch_unwind_pc (gdbarch, arm_unwind_pc);
9213 set_gdbarch_unwind_sp (gdbarch, arm_unwind_sp);
9214
eb5492fa 9215 frame_base_set_default (gdbarch, &arm_normal_base);
148754e5 9216
34e8f22d 9217 /* Address manipulation. */
34e8f22d
RE
9218 set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
9219
34e8f22d
RE
9220 /* Advance PC across function entry code. */
9221 set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
9222
c9cf6e20
MG
9223 /* Detect whether PC is at a point where the stack has been destroyed. */
9224 set_gdbarch_stack_frame_destroyed_p (gdbarch, arm_stack_frame_destroyed_p);
4024ca99 9225
190dce09
UW
9226 /* Skip trampolines. */
9227 set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
9228
34e8f22d
RE
9229 /* The stack grows downward. */
9230 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
9231
9232 /* Breakpoint manipulation. */
9233 set_gdbarch_breakpoint_from_pc (gdbarch, arm_breakpoint_from_pc);
177321bd
DJ
9234 set_gdbarch_remote_breakpoint_from_pc (gdbarch,
9235 arm_remote_breakpoint_from_pc);
34e8f22d
RE
9236
9237 /* Information about registers, etc. */
34e8f22d
RE
9238 set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
9239 set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
ff6f572f 9240 set_gdbarch_num_regs (gdbarch, ARM_NUM_REGS);
7a5ea0d4 9241 set_gdbarch_register_type (gdbarch, arm_register_type);
54483882 9242 set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);
34e8f22d 9243
ff6f572f
DJ
9244 /* This "info float" is FPA-specific. Use the generic version if we
9245 do not have FPA. */
9246 if (gdbarch_tdep (gdbarch)->have_fpa_registers)
9247 set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
9248
26216b98 9249 /* Internal <-> external register number maps. */
ff6f572f 9250 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
26216b98
AC
9251 set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
9252
34e8f22d
RE
9253 set_gdbarch_register_name (gdbarch, arm_register_name);
9254
9255 /* Returning results. */
2af48f68 9256 set_gdbarch_return_value (gdbarch, arm_return_value);
34e8f22d 9257
03d48a7d
RE
9258 /* Disassembly. */
9259 set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
9260
34e8f22d
RE
9261 /* Minsymbol frobbing. */
9262 set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
9263 set_gdbarch_coff_make_msymbol_special (gdbarch,
9264 arm_coff_make_msymbol_special);
60c5725c 9265 set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
34e8f22d 9266
f9d67f43
DJ
9267 /* Thumb-2 IT block support. */
9268 set_gdbarch_adjust_breakpoint_address (gdbarch,
9269 arm_adjust_breakpoint_address);
9270
0d5de010
DJ
9271 /* Virtual tables. */
9272 set_gdbarch_vbit_in_delta (gdbarch, 1);
9273
97e03143 9274 /* Hook in the ABI-specific overrides, if they have been registered. */
4be87837 9275 gdbarch_init_osabi (info, gdbarch);
97e03143 9276
b39cc962
DJ
9277 dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
9278
eb5492fa 9279 /* Add some default predicates. */
2ae28aa9
YQ
9280 if (is_m)
9281 frame_unwind_append_unwinder (gdbarch, &arm_m_exception_unwind);
a262aec2
DJ
9282 frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
9283 dwarf2_append_unwinders (gdbarch);
0e9e9abd 9284 frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
a262aec2 9285 frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
eb5492fa 9286
97e03143
RE
9287 /* Now we have tuned the configuration, set a few final things,
9288 based on what the OS ABI has told us. */
9289
b8926edc
DJ
9290 /* If the ABI is not otherwise marked, assume the old GNU APCS. EABI
9291 binaries are always marked. */
9292 if (tdep->arm_abi == ARM_ABI_AUTO)
9293 tdep->arm_abi = ARM_ABI_APCS;
9294
e3039479
UW
9295 /* Watchpoints are not steppable. */
9296 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
9297
b8926edc
DJ
9298 /* We used to default to FPA for generic ARM, but almost nobody
9299 uses that now, and we now provide a way for the user to force
9300 the model. So default to the most useful variant. */
9301 if (tdep->fp_model == ARM_FLOAT_AUTO)
9302 tdep->fp_model = ARM_FLOAT_SOFT_FPA;
9303
9df628e0
RE
9304 if (tdep->jb_pc >= 0)
9305 set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
9306
08216dd7 9307 /* Floating point sizes and format. */
8da61cc4 9308 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
b8926edc 9309 if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
08216dd7 9310 {
8da61cc4
DJ
9311 set_gdbarch_double_format
9312 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
9313 set_gdbarch_long_double_format
9314 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
9315 }
9316 else
9317 {
9318 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
9319 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
08216dd7
RE
9320 }
9321
58d6951d
DJ
9322 if (have_vfp_pseudos)
9323 {
9324 /* NOTE: These are the only pseudo registers used by
9325 the ARM target at the moment. If more are added, a
9326 little more care in numbering will be needed. */
9327
9328 int num_pseudos = 32;
9329 if (have_neon_pseudos)
9330 num_pseudos += 16;
9331 set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
9332 set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
9333 set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
9334 }
9335
123dc839 9336 if (tdesc_data)
58d6951d
DJ
9337 {
9338 set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
9339
9779414d 9340 tdesc_use_registers (gdbarch, tdesc, tdesc_data);
58d6951d
DJ
9341
9342 /* Override tdesc_register_type to adjust the types of VFP
9343 registers for NEON. */
9344 set_gdbarch_register_type (gdbarch, arm_register_type);
9345 }
123dc839
DJ
9346
9347 /* Add standard register aliases. We add aliases even for those
9348 nanes which are used by the current architecture - it's simpler,
9349 and does no harm, since nothing ever lists user registers. */
9350 for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
9351 user_reg_add (gdbarch, arm_register_aliases[i].name,
9352 value_of_arm_user_reg, &arm_register_aliases[i].regnum);
9353
39bbf761
RE
9354 return gdbarch;
9355}
9356
97e03143 9357static void
2af46ca0 9358arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
97e03143 9359{
2af46ca0 9360 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
97e03143
RE
9361
9362 if (tdep == NULL)
9363 return;
9364
edefbb7c 9365 fprintf_unfiltered (file, _("arm_dump_tdep: Lowest pc = 0x%lx"),
97e03143
RE
9366 (unsigned long) tdep->lowest_pc);
9367}
9368
a78f21af
AC
9369extern initialize_file_ftype _initialize_arm_tdep; /* -Wmissing-prototypes */
9370
c906108c 9371void
ed9a39eb 9372_initialize_arm_tdep (void)
c906108c 9373{
bc90b915
FN
9374 struct ui_file *stb;
9375 long length;
53904c9e
AC
9376 const char *setname;
9377 const char *setdesc;
4bd7b427 9378 const char *const *regnames;
bec2ab5a 9379 int i;
bc90b915 9380 static char *helptext;
edefbb7c
AC
9381 char regdesc[1024], *rdptr = regdesc;
9382 size_t rest = sizeof (regdesc);
085dd6e6 9383
42cf1509 9384 gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
97e03143 9385
60c5725c 9386 arm_objfile_data_key
c1bd65d0 9387 = register_objfile_data_with_cleanup (NULL, arm_objfile_data_free);
60c5725c 9388
0e9e9abd
UW
9389 /* Add ourselves to objfile event chain. */
9390 observer_attach_new_objfile (arm_exidx_new_objfile);
9391 arm_exidx_data_key
9392 = register_objfile_data_with_cleanup (NULL, arm_exidx_data_free);
9393
70f80edf
JT
9394 /* Register an ELF OS ABI sniffer for ARM binaries. */
9395 gdbarch_register_osabi_sniffer (bfd_arch_arm,
9396 bfd_target_elf_flavour,
9397 arm_elf_osabi_sniffer);
9398
9779414d
DJ
9399 /* Initialize the standard target descriptions. */
9400 initialize_tdesc_arm_with_m ();
25f8c692 9401 initialize_tdesc_arm_with_m_fpa_layout ();
3184d3f9 9402 initialize_tdesc_arm_with_m_vfp_d16 ();
ef7e8358
UW
9403 initialize_tdesc_arm_with_iwmmxt ();
9404 initialize_tdesc_arm_with_vfpv2 ();
9405 initialize_tdesc_arm_with_vfpv3 ();
9406 initialize_tdesc_arm_with_neon ();
9779414d 9407
94c30b78 9408 /* Get the number of possible sets of register names defined in opcodes. */
afd7eef0
RE
9409 num_disassembly_options = get_arm_regname_num_options ();
9410
9411 /* Add root prefix command for all "set arm"/"show arm" commands. */
9412 add_prefix_cmd ("arm", no_class, set_arm_command,
edefbb7c 9413 _("Various ARM-specific commands."),
afd7eef0
RE
9414 &setarmcmdlist, "set arm ", 0, &setlist);
9415
9416 add_prefix_cmd ("arm", no_class, show_arm_command,
edefbb7c 9417 _("Various ARM-specific commands."),
afd7eef0 9418 &showarmcmdlist, "show arm ", 0, &showlist);
bc90b915 9419
94c30b78 9420 /* Sync the opcode insn printer with our register viewer. */
bc90b915 9421 parse_arm_disassembler_option ("reg-names-std");
c5aa993b 9422
eefe576e
AC
9423 /* Initialize the array that will be passed to
9424 add_setshow_enum_cmd(). */
8d749320
SM
9425 valid_disassembly_styles = XNEWVEC (const char *,
9426 num_disassembly_options + 1);
afd7eef0 9427 for (i = 0; i < num_disassembly_options; i++)
bc90b915 9428 {
bec2ab5a 9429 get_arm_regnames (i, &setname, &setdesc, &regnames);
afd7eef0 9430 valid_disassembly_styles[i] = setname;
edefbb7c
AC
9431 length = snprintf (rdptr, rest, "%s - %s\n", setname, setdesc);
9432 rdptr += length;
9433 rest -= length;
123dc839
DJ
9434 /* When we find the default names, tell the disassembler to use
9435 them. */
bc90b915
FN
9436 if (!strcmp (setname, "std"))
9437 {
afd7eef0 9438 disassembly_style = setname;
bc90b915
FN
9439 set_arm_regname_option (i);
9440 }
9441 }
94c30b78 9442 /* Mark the end of valid options. */
afd7eef0 9443 valid_disassembly_styles[num_disassembly_options] = NULL;
c906108c 9444
edefbb7c
AC
9445 /* Create the help text. */
9446 stb = mem_fileopen ();
9447 fprintf_unfiltered (stb, "%s%s%s",
9448 _("The valid values are:\n"),
9449 regdesc,
9450 _("The default is \"std\"."));
759ef836 9451 helptext = ui_file_xstrdup (stb, NULL);
bc90b915 9452 ui_file_delete (stb);
ed9a39eb 9453
edefbb7c
AC
9454 add_setshow_enum_cmd("disassembler", no_class,
9455 valid_disassembly_styles, &disassembly_style,
9456 _("Set the disassembly style."),
9457 _("Show the disassembly style."),
9458 helptext,
2c5b56ce 9459 set_disassembly_style_sfunc,
0963b4bd
MS
9460 NULL, /* FIXME: i18n: The disassembly style is
9461 \"%s\". */
7376b4c2 9462 &setarmcmdlist, &showarmcmdlist);
edefbb7c
AC
9463
9464 add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
9465 _("Set usage of ARM 32-bit mode."),
9466 _("Show usage of ARM 32-bit mode."),
9467 _("When off, a 26-bit PC will be used."),
2c5b56ce 9468 NULL,
0963b4bd
MS
9469 NULL, /* FIXME: i18n: Usage of ARM 32-bit
9470 mode is %s. */
26304000 9471 &setarmcmdlist, &showarmcmdlist);
c906108c 9472
fd50bc42 9473 /* Add a command to allow the user to force the FPU model. */
edefbb7c
AC
9474 add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
9475 _("Set the floating point type."),
9476 _("Show the floating point type."),
9477 _("auto - Determine the FP typefrom the OS-ABI.\n\
9478softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
9479fpa - FPA co-processor (GCC compiled).\n\
9480softvfp - Software FP with pure-endian doubles.\n\
9481vfp - VFP co-processor."),
edefbb7c 9482 set_fp_model_sfunc, show_fp_model,
7376b4c2 9483 &setarmcmdlist, &showarmcmdlist);
fd50bc42 9484
28e97307
DJ
9485 /* Add a command to allow the user to force the ABI. */
9486 add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
9487 _("Set the ABI."),
9488 _("Show the ABI."),
9489 NULL, arm_set_abi, arm_show_abi,
9490 &setarmcmdlist, &showarmcmdlist);
9491
0428b8f5
DJ
9492 /* Add two commands to allow the user to force the assumed
9493 execution mode. */
9494 add_setshow_enum_cmd ("fallback-mode", class_support,
9495 arm_mode_strings, &arm_fallback_mode_string,
9496 _("Set the mode assumed when symbols are unavailable."),
9497 _("Show the mode assumed when symbols are unavailable."),
9498 NULL, NULL, arm_show_fallback_mode,
9499 &setarmcmdlist, &showarmcmdlist);
9500 add_setshow_enum_cmd ("force-mode", class_support,
9501 arm_mode_strings, &arm_force_mode_string,
9502 _("Set the mode assumed even when symbols are available."),
9503 _("Show the mode assumed even when symbols are available."),
9504 NULL, NULL, arm_show_force_mode,
9505 &setarmcmdlist, &showarmcmdlist);
9506
6529d2dd 9507 /* Debugging flag. */
edefbb7c
AC
9508 add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
9509 _("Set ARM debugging."),
9510 _("Show ARM debugging."),
9511 _("When on, arm-specific debugging is enabled."),
2c5b56ce 9512 NULL,
7915a72c 9513 NULL, /* FIXME: i18n: "ARM debugging is %s. */
26304000 9514 &setdebuglist, &showdebuglist);
c906108c 9515}
72508ac0
PO
9516
9517/* ARM-reversible process record data structures. */
9518
9519#define ARM_INSN_SIZE_BYTES 4
9520#define THUMB_INSN_SIZE_BYTES 2
9521#define THUMB2_INSN_SIZE_BYTES 4
9522
9523
71e396f9
LM
9524/* Position of the bit within a 32-bit ARM instruction
9525 that defines whether the instruction is a load or store. */
72508ac0
PO
9526#define INSN_S_L_BIT_NUM 20
9527
9528#define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
9529 do \
9530 { \
9531 unsigned int reg_len = LENGTH; \
9532 if (reg_len) \
9533 { \
9534 REGS = XNEWVEC (uint32_t, reg_len); \
9535 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
9536 } \
9537 } \
9538 while (0)
9539
9540#define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
9541 do \
9542 { \
9543 unsigned int mem_len = LENGTH; \
9544 if (mem_len) \
9545 { \
9546 MEMS = XNEWVEC (struct arm_mem_r, mem_len); \
9547 memcpy(&MEMS->len, &RECORD_BUF[0], \
9548 sizeof(struct arm_mem_r) * LENGTH); \
9549 } \
9550 } \
9551 while (0)
9552
9553/* Checks whether insn is already recorded or yet to be decoded. (boolean expression). */
9554#define INSN_RECORDED(ARM_RECORD) \
9555 (0 != (ARM_RECORD)->reg_rec_count || 0 != (ARM_RECORD)->mem_rec_count)
9556
9557/* ARM memory record structure. */
9558struct arm_mem_r
9559{
9560 uint32_t len; /* Record length. */
bfbbec00 9561 uint32_t addr; /* Memory address. */
72508ac0
PO
9562};
9563
9564/* ARM instruction record contains opcode of current insn
9565 and execution state (before entry to decode_insn()),
9566 contains list of to-be-modified registers and
9567 memory blocks (on return from decode_insn()). */
9568
9569typedef struct insn_decode_record_t
9570{
9571 struct gdbarch *gdbarch;
9572 struct regcache *regcache;
9573 CORE_ADDR this_addr; /* Address of the insn being decoded. */
9574 uint32_t arm_insn; /* Should accommodate thumb. */
9575 uint32_t cond; /* Condition code. */
9576 uint32_t opcode; /* Insn opcode. */
9577 uint32_t decode; /* Insn decode bits. */
9578 uint32_t mem_rec_count; /* No of mem records. */
9579 uint32_t reg_rec_count; /* No of reg records. */
9580 uint32_t *arm_regs; /* Registers to be saved for this record. */
9581 struct arm_mem_r *arm_mems; /* Memory to be saved for this record. */
9582} insn_decode_record;
9583
9584
9585/* Checks ARM SBZ and SBO mandatory fields. */
9586
9587static int
9588sbo_sbz (uint32_t insn, uint32_t bit_num, uint32_t len, uint32_t sbo)
9589{
9590 uint32_t ones = bits (insn, bit_num - 1, (bit_num -1) + (len - 1));
9591
9592 if (!len)
9593 return 1;
9594
9595 if (!sbo)
9596 ones = ~ones;
9597
9598 while (ones)
9599 {
9600 if (!(ones & sbo))
9601 {
9602 return 0;
9603 }
9604 ones = ones >> 1;
9605 }
9606 return 1;
9607}
9608
c6ec2b30
OJ
9609enum arm_record_result
9610{
9611 ARM_RECORD_SUCCESS = 0,
9612 ARM_RECORD_FAILURE = 1
9613};
9614
72508ac0
PO
9615typedef enum
9616{
9617 ARM_RECORD_STRH=1,
9618 ARM_RECORD_STRD
9619} arm_record_strx_t;
9620
9621typedef enum
9622{
9623 ARM_RECORD=1,
9624 THUMB_RECORD,
9625 THUMB2_RECORD
9626} record_type_t;
9627
9628
9629static int
9630arm_record_strx (insn_decode_record *arm_insn_r, uint32_t *record_buf,
9631 uint32_t *record_buf_mem, arm_record_strx_t str_type)
9632{
9633
9634 struct regcache *reg_cache = arm_insn_r->regcache;
9635 ULONGEST u_regval[2]= {0};
9636
9637 uint32_t reg_src1 = 0, reg_src2 = 0;
9638 uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
72508ac0
PO
9639
9640 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
9641 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
72508ac0
PO
9642
9643 if (14 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
9644 {
9645 /* 1) Handle misc store, immediate offset. */
9646 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
9647 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
9648 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
9649 regcache_raw_read_unsigned (reg_cache, reg_src1,
9650 &u_regval[0]);
9651 if (ARM_PC_REGNUM == reg_src1)
9652 {
9653 /* If R15 was used as Rn, hence current PC+8. */
9654 u_regval[0] = u_regval[0] + 8;
9655 }
9656 offset_8 = (immed_high << 4) | immed_low;
9657 /* Calculate target store address. */
9658 if (14 == arm_insn_r->opcode)
9659 {
9660 tgt_mem_addr = u_regval[0] + offset_8;
9661 }
9662 else
9663 {
9664 tgt_mem_addr = u_regval[0] - offset_8;
9665 }
9666 if (ARM_RECORD_STRH == str_type)
9667 {
9668 record_buf_mem[0] = 2;
9669 record_buf_mem[1] = tgt_mem_addr;
9670 arm_insn_r->mem_rec_count = 1;
9671 }
9672 else if (ARM_RECORD_STRD == str_type)
9673 {
9674 record_buf_mem[0] = 4;
9675 record_buf_mem[1] = tgt_mem_addr;
9676 record_buf_mem[2] = 4;
9677 record_buf_mem[3] = tgt_mem_addr + 4;
9678 arm_insn_r->mem_rec_count = 2;
9679 }
9680 }
9681 else if (12 == arm_insn_r->opcode || 8 == arm_insn_r->opcode)
9682 {
9683 /* 2) Store, register offset. */
9684 /* Get Rm. */
9685 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
9686 /* Get Rn. */
9687 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
9688 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
9689 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
9690 if (15 == reg_src2)
9691 {
9692 /* If R15 was used as Rn, hence current PC+8. */
9693 u_regval[0] = u_regval[0] + 8;
9694 }
9695 /* Calculate target store address, Rn +/- Rm, register offset. */
9696 if (12 == arm_insn_r->opcode)
9697 {
9698 tgt_mem_addr = u_regval[0] + u_regval[1];
9699 }
9700 else
9701 {
9702 tgt_mem_addr = u_regval[1] - u_regval[0];
9703 }
9704 if (ARM_RECORD_STRH == str_type)
9705 {
9706 record_buf_mem[0] = 2;
9707 record_buf_mem[1] = tgt_mem_addr;
9708 arm_insn_r->mem_rec_count = 1;
9709 }
9710 else if (ARM_RECORD_STRD == str_type)
9711 {
9712 record_buf_mem[0] = 4;
9713 record_buf_mem[1] = tgt_mem_addr;
9714 record_buf_mem[2] = 4;
9715 record_buf_mem[3] = tgt_mem_addr + 4;
9716 arm_insn_r->mem_rec_count = 2;
9717 }
9718 }
9719 else if (11 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
9720 || 2 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
9721 {
9722 /* 3) Store, immediate pre-indexed. */
9723 /* 5) Store, immediate post-indexed. */
9724 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
9725 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
9726 offset_8 = (immed_high << 4) | immed_low;
9727 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
9728 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
9729 /* Calculate target store address, Rn +/- Rm, register offset. */
9730 if (15 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
9731 {
9732 tgt_mem_addr = u_regval[0] + offset_8;
9733 }
9734 else
9735 {
9736 tgt_mem_addr = u_regval[0] - offset_8;
9737 }
9738 if (ARM_RECORD_STRH == str_type)
9739 {
9740 record_buf_mem[0] = 2;
9741 record_buf_mem[1] = tgt_mem_addr;
9742 arm_insn_r->mem_rec_count = 1;
9743 }
9744 else if (ARM_RECORD_STRD == str_type)
9745 {
9746 record_buf_mem[0] = 4;
9747 record_buf_mem[1] = tgt_mem_addr;
9748 record_buf_mem[2] = 4;
9749 record_buf_mem[3] = tgt_mem_addr + 4;
9750 arm_insn_r->mem_rec_count = 2;
9751 }
9752 /* Record Rn also as it changes. */
9753 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
9754 arm_insn_r->reg_rec_count = 1;
9755 }
9756 else if (9 == arm_insn_r->opcode || 13 == arm_insn_r->opcode
9757 || 0 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
9758 {
9759 /* 4) Store, register pre-indexed. */
9760 /* 6) Store, register post -indexed. */
9761 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
9762 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
9763 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
9764 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
9765 /* Calculate target store address, Rn +/- Rm, register offset. */
9766 if (13 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
9767 {
9768 tgt_mem_addr = u_regval[0] + u_regval[1];
9769 }
9770 else
9771 {
9772 tgt_mem_addr = u_regval[1] - u_regval[0];
9773 }
9774 if (ARM_RECORD_STRH == str_type)
9775 {
9776 record_buf_mem[0] = 2;
9777 record_buf_mem[1] = tgt_mem_addr;
9778 arm_insn_r->mem_rec_count = 1;
9779 }
9780 else if (ARM_RECORD_STRD == str_type)
9781 {
9782 record_buf_mem[0] = 4;
9783 record_buf_mem[1] = tgt_mem_addr;
9784 record_buf_mem[2] = 4;
9785 record_buf_mem[3] = tgt_mem_addr + 4;
9786 arm_insn_r->mem_rec_count = 2;
9787 }
9788 /* Record Rn also as it changes. */
9789 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
9790 arm_insn_r->reg_rec_count = 1;
9791 }
9792 return 0;
9793}
9794
9795/* Handling ARM extension space insns. */
9796
9797static int
9798arm_record_extension_space (insn_decode_record *arm_insn_r)
9799{
9800 uint32_t ret = 0; /* Return value: -1:record failure ; 0:success */
9801 uint32_t opcode1 = 0, opcode2 = 0, insn_op1 = 0;
9802 uint32_t record_buf[8], record_buf_mem[8];
9803 uint32_t reg_src1 = 0;
72508ac0
PO
9804 struct regcache *reg_cache = arm_insn_r->regcache;
9805 ULONGEST u_regval = 0;
9806
9807 gdb_assert (!INSN_RECORDED(arm_insn_r));
9808 /* Handle unconditional insn extension space. */
9809
9810 opcode1 = bits (arm_insn_r->arm_insn, 20, 27);
9811 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
9812 if (arm_insn_r->cond)
9813 {
9814 /* PLD has no affect on architectural state, it just affects
9815 the caches. */
9816 if (5 == ((opcode1 & 0xE0) >> 5))
9817 {
9818 /* BLX(1) */
9819 record_buf[0] = ARM_PS_REGNUM;
9820 record_buf[1] = ARM_LR_REGNUM;
9821 arm_insn_r->reg_rec_count = 2;
9822 }
9823 /* STC2, LDC2, MCR2, MRC2, CDP2: <TBD>, co-processor insn. */
9824 }
9825
9826
9827 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
9828 if (3 == opcode1 && bit (arm_insn_r->arm_insn, 4))
9829 {
9830 ret = -1;
9831 /* Undefined instruction on ARM V5; need to handle if later
9832 versions define it. */
9833 }
9834
9835 opcode1 = bits (arm_insn_r->arm_insn, 24, 27);
9836 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
9837 insn_op1 = bits (arm_insn_r->arm_insn, 20, 23);
9838
9839 /* Handle arithmetic insn extension space. */
9840 if (!opcode1 && 9 == opcode2 && 1 != arm_insn_r->cond
9841 && !INSN_RECORDED(arm_insn_r))
9842 {
9843 /* Handle MLA(S) and MUL(S). */
9844 if (0 <= insn_op1 && 3 >= insn_op1)
9845 {
9846 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
9847 record_buf[1] = ARM_PS_REGNUM;
9848 arm_insn_r->reg_rec_count = 2;
9849 }
9850 else if (4 <= insn_op1 && 15 >= insn_op1)
9851 {
9852 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
9853 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
9854 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
9855 record_buf[2] = ARM_PS_REGNUM;
9856 arm_insn_r->reg_rec_count = 3;
9857 }
9858 }
9859
9860 opcode1 = bits (arm_insn_r->arm_insn, 26, 27);
9861 opcode2 = bits (arm_insn_r->arm_insn, 23, 24);
9862 insn_op1 = bits (arm_insn_r->arm_insn, 21, 22);
9863
9864 /* Handle control insn extension space. */
9865
9866 if (!opcode1 && 2 == opcode2 && !bit (arm_insn_r->arm_insn, 20)
9867 && 1 != arm_insn_r->cond && !INSN_RECORDED(arm_insn_r))
9868 {
9869 if (!bit (arm_insn_r->arm_insn,25))
9870 {
9871 if (!bits (arm_insn_r->arm_insn, 4, 7))
9872 {
9873 if ((0 == insn_op1) || (2 == insn_op1))
9874 {
9875 /* MRS. */
9876 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
9877 arm_insn_r->reg_rec_count = 1;
9878 }
9879 else if (1 == insn_op1)
9880 {
9881 /* CSPR is going to be changed. */
9882 record_buf[0] = ARM_PS_REGNUM;
9883 arm_insn_r->reg_rec_count = 1;
9884 }
9885 else if (3 == insn_op1)
9886 {
9887 /* SPSR is going to be changed. */
9888 /* We need to get SPSR value, which is yet to be done. */
9889 printf_unfiltered (_("Process record does not support "
9890 "instruction 0x%0x at address %s.\n"),
9891 arm_insn_r->arm_insn,
9892 paddress (arm_insn_r->gdbarch,
9893 arm_insn_r->this_addr));
9894 return -1;
9895 }
9896 }
9897 else if (1 == bits (arm_insn_r->arm_insn, 4, 7))
9898 {
9899 if (1 == insn_op1)
9900 {
9901 /* BX. */
9902 record_buf[0] = ARM_PS_REGNUM;
9903 arm_insn_r->reg_rec_count = 1;
9904 }
9905 else if (3 == insn_op1)
9906 {
9907 /* CLZ. */
9908 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
9909 arm_insn_r->reg_rec_count = 1;
9910 }
9911 }
9912 else if (3 == bits (arm_insn_r->arm_insn, 4, 7))
9913 {
9914 /* BLX. */
9915 record_buf[0] = ARM_PS_REGNUM;
9916 record_buf[1] = ARM_LR_REGNUM;
9917 arm_insn_r->reg_rec_count = 2;
9918 }
9919 else if (5 == bits (arm_insn_r->arm_insn, 4, 7))
9920 {
9921 /* QADD, QSUB, QDADD, QDSUB */
9922 record_buf[0] = ARM_PS_REGNUM;
9923 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
9924 arm_insn_r->reg_rec_count = 2;
9925 }
9926 else if (7 == bits (arm_insn_r->arm_insn, 4, 7))
9927 {
9928 /* BKPT. */
9929 record_buf[0] = ARM_PS_REGNUM;
9930 record_buf[1] = ARM_LR_REGNUM;
9931 arm_insn_r->reg_rec_count = 2;
9932
9933 /* Save SPSR also;how? */
9934 printf_unfiltered (_("Process record does not support "
9935 "instruction 0x%0x at address %s.\n"),
9936 arm_insn_r->arm_insn,
9937 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
9938 return -1;
9939 }
9940 else if(8 == bits (arm_insn_r->arm_insn, 4, 7)
9941 || 10 == bits (arm_insn_r->arm_insn, 4, 7)
9942 || 12 == bits (arm_insn_r->arm_insn, 4, 7)
9943 || 14 == bits (arm_insn_r->arm_insn, 4, 7)
9944 )
9945 {
9946 if (0 == insn_op1 || 1 == insn_op1)
9947 {
9948 /* SMLA<x><y>, SMLAW<y>, SMULW<y>. */
9949 /* We dont do optimization for SMULW<y> where we
9950 need only Rd. */
9951 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
9952 record_buf[1] = ARM_PS_REGNUM;
9953 arm_insn_r->reg_rec_count = 2;
9954 }
9955 else if (2 == insn_op1)
9956 {
9957 /* SMLAL<x><y>. */
9958 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
9959 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
9960 arm_insn_r->reg_rec_count = 2;
9961 }
9962 else if (3 == insn_op1)
9963 {
9964 /* SMUL<x><y>. */
9965 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
9966 arm_insn_r->reg_rec_count = 1;
9967 }
9968 }
9969 }
9970 else
9971 {
9972 /* MSR : immediate form. */
9973 if (1 == insn_op1)
9974 {
9975 /* CSPR is going to be changed. */
9976 record_buf[0] = ARM_PS_REGNUM;
9977 arm_insn_r->reg_rec_count = 1;
9978 }
9979 else if (3 == insn_op1)
9980 {
9981 /* SPSR is going to be changed. */
9982 /* we need to get SPSR value, which is yet to be done */
9983 printf_unfiltered (_("Process record does not support "
9984 "instruction 0x%0x at address %s.\n"),
9985 arm_insn_r->arm_insn,
9986 paddress (arm_insn_r->gdbarch,
9987 arm_insn_r->this_addr));
9988 return -1;
9989 }
9990 }
9991 }
9992
9993 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
9994 opcode2 = bits (arm_insn_r->arm_insn, 20, 24);
9995 insn_op1 = bits (arm_insn_r->arm_insn, 5, 6);
9996
9997 /* Handle load/store insn extension space. */
9998
9999 if (!opcode1 && bit (arm_insn_r->arm_insn, 7)
10000 && bit (arm_insn_r->arm_insn, 4) && 1 != arm_insn_r->cond
10001 && !INSN_RECORDED(arm_insn_r))
10002 {
10003 /* SWP/SWPB. */
10004 if (0 == insn_op1)
10005 {
10006 /* These insn, changes register and memory as well. */
10007 /* SWP or SWPB insn. */
10008 /* Get memory address given by Rn. */
10009 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10010 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
10011 /* SWP insn ?, swaps word. */
10012 if (8 == arm_insn_r->opcode)
10013 {
10014 record_buf_mem[0] = 4;
10015 }
10016 else
10017 {
10018 /* SWPB insn, swaps only byte. */
10019 record_buf_mem[0] = 1;
10020 }
10021 record_buf_mem[1] = u_regval;
10022 arm_insn_r->mem_rec_count = 1;
10023 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10024 arm_insn_r->reg_rec_count = 1;
10025 }
10026 else if (1 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
10027 {
10028 /* STRH. */
10029 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
10030 ARM_RECORD_STRH);
10031 }
10032 else if (2 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
10033 {
10034 /* LDRD. */
10035 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10036 record_buf[1] = record_buf[0] + 1;
10037 arm_insn_r->reg_rec_count = 2;
10038 }
10039 else if (3 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
10040 {
10041 /* STRD. */
10042 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
10043 ARM_RECORD_STRD);
10044 }
10045 else if (bit (arm_insn_r->arm_insn, 20) && insn_op1 <= 3)
10046 {
10047 /* LDRH, LDRSB, LDRSH. */
10048 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10049 arm_insn_r->reg_rec_count = 1;
10050 }
10051
10052 }
10053
10054 opcode1 = bits (arm_insn_r->arm_insn, 23, 27);
10055 if (24 == opcode1 && bit (arm_insn_r->arm_insn, 21)
10056 && !INSN_RECORDED(arm_insn_r))
10057 {
10058 ret = -1;
10059 /* Handle coprocessor insn extension space. */
10060 }
10061
10062 /* To be done for ARMv5 and later; as of now we return -1. */
10063 if (-1 == ret)
10064 printf_unfiltered (_("Process record does not support instruction x%0x "
10065 "at address %s.\n"),arm_insn_r->arm_insn,
10066 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
10067
10068
10069 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10070 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10071
10072 return ret;
10073}
10074
10075/* Handling opcode 000 insns. */
10076
10077static int
10078arm_record_data_proc_misc_ld_str (insn_decode_record *arm_insn_r)
10079{
10080 struct regcache *reg_cache = arm_insn_r->regcache;
10081 uint32_t record_buf[8], record_buf_mem[8];
10082 ULONGEST u_regval[2] = {0};
10083
bec2ab5a 10084 uint32_t reg_src1 = 0, reg_dest = 0;
72508ac0
PO
10085 uint32_t opcode1 = 0;
10086
10087 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10088 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10089 opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
10090
10091 /* Data processing insn /multiply insn. */
10092 if (9 == arm_insn_r->decode
10093 && ((4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
10094 || (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)))
10095 {
10096 /* Handle multiply instructions. */
10097 /* MLA, MUL, SMLAL, SMULL, UMLAL, UMULL. */
10098 if (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)
10099 {
10100 /* Handle MLA and MUL. */
10101 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
10102 record_buf[1] = ARM_PS_REGNUM;
10103 arm_insn_r->reg_rec_count = 2;
10104 }
10105 else if (4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
10106 {
10107 /* Handle SMLAL, SMULL, UMLAL, UMULL. */
10108 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
10109 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
10110 record_buf[2] = ARM_PS_REGNUM;
10111 arm_insn_r->reg_rec_count = 3;
10112 }
10113 }
10114 else if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
10115 && (11 == arm_insn_r->decode || 13 == arm_insn_r->decode))
10116 {
10117 /* Handle misc load insns, as 20th bit (L = 1). */
10118 /* LDR insn has a capability to do branching, if
10119 MOV LR, PC is precceded by LDR insn having Rn as R15
10120 in that case, it emulates branch and link insn, and hence we
10121 need to save CSPR and PC as well. I am not sure this is right
10122 place; as opcode = 010 LDR insn make this happen, if R15 was
10123 used. */
10124 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
10125 if (15 != reg_dest)
10126 {
10127 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10128 arm_insn_r->reg_rec_count = 1;
10129 }
10130 else
10131 {
10132 record_buf[0] = reg_dest;
10133 record_buf[1] = ARM_PS_REGNUM;
10134 arm_insn_r->reg_rec_count = 2;
10135 }
10136 }
10137 else if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
10138 && sbo_sbz (arm_insn_r->arm_insn, 5, 12, 0)
10139 && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
10140 && 2 == bits (arm_insn_r->arm_insn, 20, 21))
10141 {
10142 /* Handle MSR insn. */
10143 if (9 == arm_insn_r->opcode)
10144 {
10145 /* CSPR is going to be changed. */
10146 record_buf[0] = ARM_PS_REGNUM;
10147 arm_insn_r->reg_rec_count = 1;
10148 }
10149 else
10150 {
10151 /* SPSR is going to be changed. */
10152 /* How to read SPSR value? */
10153 printf_unfiltered (_("Process record does not support instruction "
10154 "0x%0x at address %s.\n"),
10155 arm_insn_r->arm_insn,
10156 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
10157 return -1;
10158 }
10159 }
10160 else if (9 == arm_insn_r->decode
10161 && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
10162 && !bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
10163 {
10164 /* Handling SWP, SWPB. */
10165 /* These insn, changes register and memory as well. */
10166 /* SWP or SWPB insn. */
10167
10168 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10169 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10170 /* SWP insn ?, swaps word. */
10171 if (8 == arm_insn_r->opcode)
10172 {
10173 record_buf_mem[0] = 4;
10174 }
10175 else
10176 {
10177 /* SWPB insn, swaps only byte. */
10178 record_buf_mem[0] = 1;
10179 }
10180 record_buf_mem[1] = u_regval[0];
10181 arm_insn_r->mem_rec_count = 1;
10182 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10183 arm_insn_r->reg_rec_count = 1;
10184 }
10185 else if (3 == arm_insn_r->decode && 0x12 == opcode1
10186 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
10187 {
10188 /* Handle BLX, branch and link/exchange. */
10189 if (9 == arm_insn_r->opcode)
10190 {
10191 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm,
10192 and R14 stores the return address. */
10193 record_buf[0] = ARM_PS_REGNUM;
10194 record_buf[1] = ARM_LR_REGNUM;
10195 arm_insn_r->reg_rec_count = 2;
10196 }
10197 }
10198 else if (7 == arm_insn_r->decode && 0x12 == opcode1)
10199 {
10200 /* Handle enhanced software breakpoint insn, BKPT. */
10201 /* CPSR is changed to be executed in ARM state, disabling normal
10202 interrupts, entering abort mode. */
10203 /* According to high vector configuration PC is set. */
10204 /* user hit breakpoint and type reverse, in
10205 that case, we need to go back with previous CPSR and
10206 Program Counter. */
10207 record_buf[0] = ARM_PS_REGNUM;
10208 record_buf[1] = ARM_LR_REGNUM;
10209 arm_insn_r->reg_rec_count = 2;
10210
10211 /* Save SPSR also; how? */
10212 printf_unfiltered (_("Process record does not support instruction "
10213 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
10214 paddress (arm_insn_r->gdbarch,
10215 arm_insn_r->this_addr));
10216 return -1;
10217 }
10218 else if (11 == arm_insn_r->decode
10219 && !bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
10220 {
10221 /* Handle enhanced store insns and DSP insns (e.g. LDRD). */
10222
10223 /* Handle str(x) insn */
10224 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
10225 ARM_RECORD_STRH);
10226 }
10227 else if (1 == arm_insn_r->decode && 0x12 == opcode1
10228 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
10229 {
10230 /* Handle BX, branch and link/exchange. */
10231 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm. */
10232 record_buf[0] = ARM_PS_REGNUM;
10233 arm_insn_r->reg_rec_count = 1;
10234 }
10235 else if (1 == arm_insn_r->decode && 0x16 == opcode1
10236 && sbo_sbz (arm_insn_r->arm_insn, 9, 4, 1)
10237 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1))
10238 {
10239 /* Count leading zeros: CLZ. */
10240 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10241 arm_insn_r->reg_rec_count = 1;
10242 }
10243 else if (!bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
10244 && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
10245 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1)
10246 && sbo_sbz (arm_insn_r->arm_insn, 1, 12, 0)
10247 )
10248 {
10249 /* Handle MRS insn. */
10250 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10251 arm_insn_r->reg_rec_count = 1;
10252 }
10253 else if (arm_insn_r->opcode <= 15)
10254 {
10255 /* Normal data processing insns. */
10256 /* Out of 11 shifter operands mode, all the insn modifies destination
10257 register, which is specified by 13-16 decode. */
10258 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10259 record_buf[1] = ARM_PS_REGNUM;
10260 arm_insn_r->reg_rec_count = 2;
10261 }
10262 else
10263 {
10264 return -1;
10265 }
10266
10267 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10268 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10269 return 0;
10270}
10271
10272/* Handling opcode 001 insns. */
10273
10274static int
10275arm_record_data_proc_imm (insn_decode_record *arm_insn_r)
10276{
10277 uint32_t record_buf[8], record_buf_mem[8];
10278
10279 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10280 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10281
10282 if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
10283 && 2 == bits (arm_insn_r->arm_insn, 20, 21)
10284 && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
10285 )
10286 {
10287 /* Handle MSR insn. */
10288 if (9 == arm_insn_r->opcode)
10289 {
10290 /* CSPR is going to be changed. */
10291 record_buf[0] = ARM_PS_REGNUM;
10292 arm_insn_r->reg_rec_count = 1;
10293 }
10294 else
10295 {
10296 /* SPSR is going to be changed. */
10297 }
10298 }
10299 else if (arm_insn_r->opcode <= 15)
10300 {
10301 /* Normal data processing insns. */
10302 /* Out of 11 shifter operands mode, all the insn modifies destination
10303 register, which is specified by 13-16 decode. */
10304 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10305 record_buf[1] = ARM_PS_REGNUM;
10306 arm_insn_r->reg_rec_count = 2;
10307 }
10308 else
10309 {
10310 return -1;
10311 }
10312
10313 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10314 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10315 return 0;
10316}
10317
71e396f9 10318/* Handle ARM mode instructions with opcode 010. */
72508ac0
PO
10319
10320static int
10321arm_record_ld_st_imm_offset (insn_decode_record *arm_insn_r)
10322{
10323 struct regcache *reg_cache = arm_insn_r->regcache;
10324
71e396f9
LM
10325 uint32_t reg_base , reg_dest;
10326 uint32_t offset_12, tgt_mem_addr;
72508ac0 10327 uint32_t record_buf[8], record_buf_mem[8];
71e396f9
LM
10328 unsigned char wback;
10329 ULONGEST u_regval;
72508ac0 10330
71e396f9
LM
10331 /* Calculate wback. */
10332 wback = (bit (arm_insn_r->arm_insn, 24) == 0)
10333 || (bit (arm_insn_r->arm_insn, 21) == 1);
72508ac0 10334
71e396f9
LM
10335 arm_insn_r->reg_rec_count = 0;
10336 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0
PO
10337
10338 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
10339 {
71e396f9
LM
10340 /* LDR (immediate), LDR (literal), LDRB (immediate), LDRB (literal), LDRBT
10341 and LDRT. */
10342
72508ac0 10343 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
71e396f9
LM
10344 record_buf[arm_insn_r->reg_rec_count++] = reg_dest;
10345
10346 /* The LDR instruction is capable of doing branching. If MOV LR, PC
10347 preceeds a LDR instruction having R15 as reg_base, it
10348 emulates a branch and link instruction, and hence we need to save
10349 CPSR and PC as well. */
10350 if (ARM_PC_REGNUM == reg_dest)
10351 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
10352
10353 /* If wback is true, also save the base register, which is going to be
10354 written to. */
10355 if (wback)
10356 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
10357 }
10358 else
10359 {
71e396f9
LM
10360 /* STR (immediate), STRB (immediate), STRBT and STRT. */
10361
72508ac0 10362 offset_12 = bits (arm_insn_r->arm_insn, 0, 11);
71e396f9
LM
10363 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
10364
10365 /* Handle bit U. */
72508ac0 10366 if (bit (arm_insn_r->arm_insn, 23))
71e396f9
LM
10367 {
10368 /* U == 1: Add the offset. */
10369 tgt_mem_addr = (uint32_t) u_regval + offset_12;
10370 }
72508ac0 10371 else
71e396f9
LM
10372 {
10373 /* U == 0: subtract the offset. */
10374 tgt_mem_addr = (uint32_t) u_regval - offset_12;
10375 }
10376
10377 /* Bit 22 tells us whether the store instruction writes 1 byte or 4
10378 bytes. */
10379 if (bit (arm_insn_r->arm_insn, 22))
10380 {
10381 /* STRB and STRBT: 1 byte. */
10382 record_buf_mem[0] = 1;
10383 }
10384 else
10385 {
10386 /* STR and STRT: 4 bytes. */
10387 record_buf_mem[0] = 4;
10388 }
10389
10390 /* Handle bit P. */
10391 if (bit (arm_insn_r->arm_insn, 24))
10392 record_buf_mem[1] = tgt_mem_addr;
10393 else
10394 record_buf_mem[1] = (uint32_t) u_regval;
72508ac0 10395
72508ac0
PO
10396 arm_insn_r->mem_rec_count = 1;
10397
71e396f9
LM
10398 /* If wback is true, also save the base register, which is going to be
10399 written to. */
10400 if (wback)
10401 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
10402 }
10403
10404 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10405 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10406 return 0;
10407}
10408
10409/* Handling opcode 011 insns. */
10410
10411static int
10412arm_record_ld_st_reg_offset (insn_decode_record *arm_insn_r)
10413{
10414 struct regcache *reg_cache = arm_insn_r->regcache;
10415
10416 uint32_t shift_imm = 0;
10417 uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
10418 uint32_t offset_12 = 0, tgt_mem_addr = 0;
10419 uint32_t record_buf[8], record_buf_mem[8];
10420
10421 LONGEST s_word;
10422 ULONGEST u_regval[2];
10423
10424 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10425 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10426
10427 /* Handle enhanced store insns and LDRD DSP insn,
10428 order begins according to addressing modes for store insns
10429 STRH insn. */
10430
10431 /* LDR or STR? */
10432 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
10433 {
10434 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
10435 /* LDR insn has a capability to do branching, if
10436 MOV LR, PC is precedded by LDR insn having Rn as R15
10437 in that case, it emulates branch and link insn, and hence we
10438 need to save CSPR and PC as well. */
10439 if (15 != reg_dest)
10440 {
10441 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10442 arm_insn_r->reg_rec_count = 1;
10443 }
10444 else
10445 {
10446 record_buf[0] = reg_dest;
10447 record_buf[1] = ARM_PS_REGNUM;
10448 arm_insn_r->reg_rec_count = 2;
10449 }
10450 }
10451 else
10452 {
10453 if (! bits (arm_insn_r->arm_insn, 4, 11))
10454 {
10455 /* Store insn, register offset and register pre-indexed,
10456 register post-indexed. */
10457 /* Get Rm. */
10458 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10459 /* Get Rn. */
10460 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10461 regcache_raw_read_unsigned (reg_cache, reg_src1
10462 , &u_regval[0]);
10463 regcache_raw_read_unsigned (reg_cache, reg_src2
10464 , &u_regval[1]);
10465 if (15 == reg_src2)
10466 {
10467 /* If R15 was used as Rn, hence current PC+8. */
10468 /* Pre-indexed mode doesnt reach here ; illegal insn. */
10469 u_regval[0] = u_regval[0] + 8;
10470 }
10471 /* Calculate target store address, Rn +/- Rm, register offset. */
10472 /* U == 1. */
10473 if (bit (arm_insn_r->arm_insn, 23))
10474 {
10475 tgt_mem_addr = u_regval[0] + u_regval[1];
10476 }
10477 else
10478 {
10479 tgt_mem_addr = u_regval[1] - u_regval[0];
10480 }
10481
10482 switch (arm_insn_r->opcode)
10483 {
10484 /* STR. */
10485 case 8:
10486 case 12:
10487 /* STR. */
10488 case 9:
10489 case 13:
10490 /* STRT. */
10491 case 1:
10492 case 5:
10493 /* STR. */
10494 case 0:
10495 case 4:
10496 record_buf_mem[0] = 4;
10497 break;
10498
10499 /* STRB. */
10500 case 10:
10501 case 14:
10502 /* STRB. */
10503 case 11:
10504 case 15:
10505 /* STRBT. */
10506 case 3:
10507 case 7:
10508 /* STRB. */
10509 case 2:
10510 case 6:
10511 record_buf_mem[0] = 1;
10512 break;
10513
10514 default:
10515 gdb_assert_not_reached ("no decoding pattern found");
10516 break;
10517 }
10518 record_buf_mem[1] = tgt_mem_addr;
10519 arm_insn_r->mem_rec_count = 1;
10520
10521 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
10522 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
10523 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
10524 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
10525 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
10526 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
10527 )
10528 {
10529 /* Rn is going to be changed in pre-indexed mode and
10530 post-indexed mode as well. */
10531 record_buf[0] = reg_src2;
10532 arm_insn_r->reg_rec_count = 1;
10533 }
10534 }
10535 else
10536 {
10537 /* Store insn, scaled register offset; scaled pre-indexed. */
10538 offset_12 = bits (arm_insn_r->arm_insn, 5, 6);
10539 /* Get Rm. */
10540 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10541 /* Get Rn. */
10542 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10543 /* Get shift_imm. */
10544 shift_imm = bits (arm_insn_r->arm_insn, 7, 11);
10545 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10546 regcache_raw_read_signed (reg_cache, reg_src1, &s_word);
10547 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10548 /* Offset_12 used as shift. */
10549 switch (offset_12)
10550 {
10551 case 0:
10552 /* Offset_12 used as index. */
10553 offset_12 = u_regval[0] << shift_imm;
10554 break;
10555
10556 case 1:
10557 offset_12 = (!shift_imm)?0:u_regval[0] >> shift_imm;
10558 break;
10559
10560 case 2:
10561 if (!shift_imm)
10562 {
10563 if (bit (u_regval[0], 31))
10564 {
10565 offset_12 = 0xFFFFFFFF;
10566 }
10567 else
10568 {
10569 offset_12 = 0;
10570 }
10571 }
10572 else
10573 {
10574 /* This is arithmetic shift. */
10575 offset_12 = s_word >> shift_imm;
10576 }
10577 break;
10578
10579 case 3:
10580 if (!shift_imm)
10581 {
10582 regcache_raw_read_unsigned (reg_cache, ARM_PS_REGNUM,
10583 &u_regval[1]);
10584 /* Get C flag value and shift it by 31. */
10585 offset_12 = (((bit (u_regval[1], 29)) << 31) \
10586 | (u_regval[0]) >> 1);
10587 }
10588 else
10589 {
10590 offset_12 = (u_regval[0] >> shift_imm) \
10591 | (u_regval[0] <<
10592 (sizeof(uint32_t) - shift_imm));
10593 }
10594 break;
10595
10596 default:
10597 gdb_assert_not_reached ("no decoding pattern found");
10598 break;
10599 }
10600
10601 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10602 /* bit U set. */
10603 if (bit (arm_insn_r->arm_insn, 23))
10604 {
10605 tgt_mem_addr = u_regval[1] + offset_12;
10606 }
10607 else
10608 {
10609 tgt_mem_addr = u_regval[1] - offset_12;
10610 }
10611
10612 switch (arm_insn_r->opcode)
10613 {
10614 /* STR. */
10615 case 8:
10616 case 12:
10617 /* STR. */
10618 case 9:
10619 case 13:
10620 /* STRT. */
10621 case 1:
10622 case 5:
10623 /* STR. */
10624 case 0:
10625 case 4:
10626 record_buf_mem[0] = 4;
10627 break;
10628
10629 /* STRB. */
10630 case 10:
10631 case 14:
10632 /* STRB. */
10633 case 11:
10634 case 15:
10635 /* STRBT. */
10636 case 3:
10637 case 7:
10638 /* STRB. */
10639 case 2:
10640 case 6:
10641 record_buf_mem[0] = 1;
10642 break;
10643
10644 default:
10645 gdb_assert_not_reached ("no decoding pattern found");
10646 break;
10647 }
10648 record_buf_mem[1] = tgt_mem_addr;
10649 arm_insn_r->mem_rec_count = 1;
10650
10651 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
10652 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
10653 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
10654 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
10655 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
10656 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
10657 )
10658 {
10659 /* Rn is going to be changed in register scaled pre-indexed
10660 mode,and scaled post indexed mode. */
10661 record_buf[0] = reg_src2;
10662 arm_insn_r->reg_rec_count = 1;
10663 }
10664 }
10665 }
10666
10667 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10668 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10669 return 0;
10670}
10671
71e396f9 10672/* Handle ARM mode instructions with opcode 100. */
72508ac0
PO
10673
10674static int
10675arm_record_ld_st_multiple (insn_decode_record *arm_insn_r)
10676{
10677 struct regcache *reg_cache = arm_insn_r->regcache;
71e396f9
LM
10678 uint32_t register_count = 0, register_bits;
10679 uint32_t reg_base, addr_mode;
72508ac0 10680 uint32_t record_buf[24], record_buf_mem[48];
71e396f9
LM
10681 uint32_t wback;
10682 ULONGEST u_regval;
72508ac0 10683
71e396f9
LM
10684 /* Fetch the list of registers. */
10685 register_bits = bits (arm_insn_r->arm_insn, 0, 15);
10686 arm_insn_r->reg_rec_count = 0;
10687
10688 /* Fetch the base register that contains the address we are loading data
10689 to. */
10690 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0 10691
71e396f9
LM
10692 /* Calculate wback. */
10693 wback = (bit (arm_insn_r->arm_insn, 21) == 1);
72508ac0
PO
10694
10695 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
10696 {
71e396f9 10697 /* LDM/LDMIA/LDMFD, LDMDA/LDMFA, LDMDB and LDMIB. */
72508ac0 10698
71e396f9 10699 /* Find out which registers are going to be loaded from memory. */
72508ac0 10700 while (register_bits)
71e396f9
LM
10701 {
10702 if (register_bits & 0x00000001)
10703 record_buf[arm_insn_r->reg_rec_count++] = register_count;
10704 register_bits = register_bits >> 1;
10705 register_count++;
10706 }
72508ac0 10707
71e396f9
LM
10708
10709 /* If wback is true, also save the base register, which is going to be
10710 written to. */
10711 if (wback)
10712 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
10713
10714 /* Save the CPSR register. */
10715 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
72508ac0
PO
10716 }
10717 else
10718 {
71e396f9 10719 /* STM (STMIA, STMEA), STMDA (STMED), STMDB (STMFD) and STMIB (STMFA). */
72508ac0 10720
71e396f9
LM
10721 addr_mode = bits (arm_insn_r->arm_insn, 23, 24);
10722
10723 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
10724
10725 /* Find out how many registers are going to be stored to memory. */
72508ac0 10726 while (register_bits)
71e396f9
LM
10727 {
10728 if (register_bits & 0x00000001)
10729 register_count++;
10730 register_bits = register_bits >> 1;
10731 }
72508ac0
PO
10732
10733 switch (addr_mode)
71e396f9
LM
10734 {
10735 /* STMDA (STMED): Decrement after. */
10736 case 0:
10737 record_buf_mem[1] = (uint32_t) u_regval
10738 - register_count * INT_REGISTER_SIZE + 4;
10739 break;
10740 /* STM (STMIA, STMEA): Increment after. */
10741 case 1:
10742 record_buf_mem[1] = (uint32_t) u_regval;
10743 break;
10744 /* STMDB (STMFD): Decrement before. */
10745 case 2:
10746 record_buf_mem[1] = (uint32_t) u_regval
10747 - register_count * INT_REGISTER_SIZE;
10748 break;
10749 /* STMIB (STMFA): Increment before. */
10750 case 3:
10751 record_buf_mem[1] = (uint32_t) u_regval + INT_REGISTER_SIZE;
10752 break;
10753 default:
10754 gdb_assert_not_reached ("no decoding pattern found");
10755 break;
10756 }
72508ac0 10757
71e396f9
LM
10758 record_buf_mem[0] = register_count * INT_REGISTER_SIZE;
10759 arm_insn_r->mem_rec_count = 1;
10760
10761 /* If wback is true, also save the base register, which is going to be
10762 written to. */
10763 if (wback)
10764 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
10765 }
10766
10767 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10768 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10769 return 0;
10770}
10771
10772/* Handling opcode 101 insns. */
10773
10774static int
10775arm_record_b_bl (insn_decode_record *arm_insn_r)
10776{
10777 uint32_t record_buf[8];
10778
10779 /* Handle B, BL, BLX(1) insns. */
10780 /* B simply branches so we do nothing here. */
10781 /* Note: BLX(1) doesnt fall here but instead it falls into
10782 extension space. */
10783 if (bit (arm_insn_r->arm_insn, 24))
10784 {
10785 record_buf[0] = ARM_LR_REGNUM;
10786 arm_insn_r->reg_rec_count = 1;
10787 }
10788
10789 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10790
10791 return 0;
10792}
10793
10794/* Handling opcode 110 insns. */
10795
10796static int
c6ec2b30 10797arm_record_unsupported_insn (insn_decode_record *arm_insn_r)
72508ac0
PO
10798{
10799 printf_unfiltered (_("Process record does not support instruction "
10800 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
10801 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
10802
10803 return -1;
10804}
10805
5a578da5
OJ
10806/* Record handler for vector data transfer instructions. */
10807
10808static int
10809arm_record_vdata_transfer_insn (insn_decode_record *arm_insn_r)
10810{
10811 uint32_t bits_a, bit_c, bit_l, reg_t, reg_v;
10812 uint32_t record_buf[4];
10813
10814 const int num_regs = gdbarch_num_regs (arm_insn_r->gdbarch);
10815 reg_t = bits (arm_insn_r->arm_insn, 12, 15);
10816 reg_v = bits (arm_insn_r->arm_insn, 21, 23);
10817 bits_a = bits (arm_insn_r->arm_insn, 21, 23);
10818 bit_l = bit (arm_insn_r->arm_insn, 20);
10819 bit_c = bit (arm_insn_r->arm_insn, 8);
10820
10821 /* Handle VMOV instruction. */
10822 if (bit_l && bit_c)
10823 {
10824 record_buf[0] = reg_t;
10825 arm_insn_r->reg_rec_count = 1;
10826 }
10827 else if (bit_l && !bit_c)
10828 {
10829 /* Handle VMOV instruction. */
10830 if (bits_a == 0x00)
10831 {
10832 if (bit (arm_insn_r->arm_insn, 20))
10833 record_buf[0] = reg_t;
10834 else
10835 record_buf[0] = num_regs + (bit (arm_insn_r->arm_insn, 7) |
10836 (reg_v << 1));
10837
10838 arm_insn_r->reg_rec_count = 1;
10839 }
10840 /* Handle VMRS instruction. */
10841 else if (bits_a == 0x07)
10842 {
10843 if (reg_t == 15)
10844 reg_t = ARM_PS_REGNUM;
10845
10846 record_buf[0] = reg_t;
10847 arm_insn_r->reg_rec_count = 1;
10848 }
10849 }
10850 else if (!bit_l && !bit_c)
10851 {
10852 /* Handle VMOV instruction. */
10853 if (bits_a == 0x00)
10854 {
10855 if (bit (arm_insn_r->arm_insn, 20))
10856 record_buf[0] = reg_t;
10857 else
10858 record_buf[0] = num_regs + (bit (arm_insn_r->arm_insn, 7) |
10859 (reg_v << 1));
10860
10861 arm_insn_r->reg_rec_count = 1;
10862 }
10863 /* Handle VMSR instruction. */
10864 else if (bits_a == 0x07)
10865 {
10866 record_buf[0] = ARM_FPSCR_REGNUM;
10867 arm_insn_r->reg_rec_count = 1;
10868 }
10869 }
10870 else if (!bit_l && bit_c)
10871 {
10872 /* Handle VMOV instruction. */
10873 if (!(bits_a & 0x04))
10874 {
10875 record_buf[0] = (reg_v | (bit (arm_insn_r->arm_insn, 7) << 4))
10876 + ARM_D0_REGNUM;
10877 arm_insn_r->reg_rec_count = 1;
10878 }
10879 /* Handle VDUP instruction. */
10880 else
10881 {
10882 if (bit (arm_insn_r->arm_insn, 21))
10883 {
10884 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
10885 record_buf[0] = reg_v + ARM_D0_REGNUM;
10886 record_buf[1] = reg_v + ARM_D0_REGNUM + 1;
10887 arm_insn_r->reg_rec_count = 2;
10888 }
10889 else
10890 {
10891 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
10892 record_buf[0] = reg_v + ARM_D0_REGNUM;
10893 arm_insn_r->reg_rec_count = 1;
10894 }
10895 }
10896 }
10897
10898 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10899 return 0;
10900}
10901
f20f80dd
OJ
10902/* Record handler for extension register load/store instructions. */
10903
10904static int
10905arm_record_exreg_ld_st_insn (insn_decode_record *arm_insn_r)
10906{
10907 uint32_t opcode, single_reg;
10908 uint8_t op_vldm_vstm;
10909 uint32_t record_buf[8], record_buf_mem[128];
10910 ULONGEST u_regval = 0;
10911
10912 struct regcache *reg_cache = arm_insn_r->regcache;
10913 const int num_regs = gdbarch_num_regs (arm_insn_r->gdbarch);
10914
10915 opcode = bits (arm_insn_r->arm_insn, 20, 24);
10916 single_reg = bit (arm_insn_r->arm_insn, 8);
10917 op_vldm_vstm = opcode & 0x1b;
10918
10919 /* Handle VMOV instructions. */
10920 if ((opcode & 0x1e) == 0x04)
10921 {
10922 if (bit (arm_insn_r->arm_insn, 4))
10923 {
10924 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10925 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
10926 arm_insn_r->reg_rec_count = 2;
10927 }
10928 else
10929 {
10930 uint8_t reg_m = (bits (arm_insn_r->arm_insn, 0, 3) << 1)
10931 | bit (arm_insn_r->arm_insn, 5);
10932
10933 if (!single_reg)
10934 {
10935 record_buf[0] = num_regs + reg_m;
10936 record_buf[1] = num_regs + reg_m + 1;
10937 arm_insn_r->reg_rec_count = 2;
10938 }
10939 else
10940 {
10941 record_buf[0] = reg_m + ARM_D0_REGNUM;
10942 arm_insn_r->reg_rec_count = 1;
10943 }
10944 }
10945 }
10946 /* Handle VSTM and VPUSH instructions. */
10947 else if (op_vldm_vstm == 0x08 || op_vldm_vstm == 0x0a
10948 || op_vldm_vstm == 0x12)
10949 {
10950 uint32_t start_address, reg_rn, imm_off32, imm_off8, memory_count;
10951 uint32_t memory_index = 0;
10952
10953 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
10954 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
10955 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
10956 imm_off32 = imm_off8 << 24;
10957 memory_count = imm_off8;
10958
10959 if (bit (arm_insn_r->arm_insn, 23))
10960 start_address = u_regval;
10961 else
10962 start_address = u_regval - imm_off32;
10963
10964 if (bit (arm_insn_r->arm_insn, 21))
10965 {
10966 record_buf[0] = reg_rn;
10967 arm_insn_r->reg_rec_count = 1;
10968 }
10969
10970 while (memory_count > 0)
10971 {
10972 if (!single_reg)
10973 {
10974 record_buf_mem[memory_index] = start_address;
10975 record_buf_mem[memory_index + 1] = 4;
10976 start_address = start_address + 4;
10977 memory_index = memory_index + 2;
10978 }
10979 else
10980 {
10981 record_buf_mem[memory_index] = start_address;
10982 record_buf_mem[memory_index + 1] = 4;
10983 record_buf_mem[memory_index + 2] = start_address + 4;
10984 record_buf_mem[memory_index + 3] = 4;
10985 start_address = start_address + 8;
10986 memory_index = memory_index + 4;
10987 }
10988 memory_count--;
10989 }
10990 arm_insn_r->mem_rec_count = (memory_index >> 1);
10991 }
10992 /* Handle VLDM instructions. */
10993 else if (op_vldm_vstm == 0x09 || op_vldm_vstm == 0x0b
10994 || op_vldm_vstm == 0x13)
10995 {
10996 uint32_t reg_count, reg_vd;
10997 uint32_t reg_index = 0;
10998
10999 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
11000 reg_count = bits (arm_insn_r->arm_insn, 0, 7);
11001
11002 if (single_reg)
11003 reg_vd = reg_vd | (bit (arm_insn_r->arm_insn, 22) << 4);
11004 else
11005 reg_vd = (reg_vd << 1) | bit (arm_insn_r->arm_insn, 22);
11006
11007 if (bit (arm_insn_r->arm_insn, 21))
11008 record_buf[reg_index++] = bits (arm_insn_r->arm_insn, 16, 19);
11009
11010 while (reg_count > 0)
11011 {
11012 if (single_reg)
11013 record_buf[reg_index++] = num_regs + reg_vd + reg_count - 1;
11014 else
11015 record_buf[reg_index++] = ARM_D0_REGNUM + reg_vd + reg_count - 1;
11016
11017 reg_count--;
11018 }
11019 arm_insn_r->reg_rec_count = reg_index;
11020 }
11021 /* VSTR Vector store register. */
11022 else if ((opcode & 0x13) == 0x10)
11023 {
bec2ab5a 11024 uint32_t start_address, reg_rn, imm_off32, imm_off8;
f20f80dd
OJ
11025 uint32_t memory_index = 0;
11026
11027 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
11028 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
11029 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
11030 imm_off32 = imm_off8 << 24;
f20f80dd
OJ
11031
11032 if (bit (arm_insn_r->arm_insn, 23))
11033 start_address = u_regval + imm_off32;
11034 else
11035 start_address = u_regval - imm_off32;
11036
11037 if (single_reg)
11038 {
11039 record_buf_mem[memory_index] = start_address;
11040 record_buf_mem[memory_index + 1] = 4;
11041 arm_insn_r->mem_rec_count = 1;
11042 }
11043 else
11044 {
11045 record_buf_mem[memory_index] = start_address;
11046 record_buf_mem[memory_index + 1] = 4;
11047 record_buf_mem[memory_index + 2] = start_address + 4;
11048 record_buf_mem[memory_index + 3] = 4;
11049 arm_insn_r->mem_rec_count = 2;
11050 }
11051 }
11052 /* VLDR Vector load register. */
11053 else if ((opcode & 0x13) == 0x11)
11054 {
11055 uint32_t reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
11056
11057 if (!single_reg)
11058 {
11059 reg_vd = reg_vd | (bit (arm_insn_r->arm_insn, 22) << 4);
11060 record_buf[0] = ARM_D0_REGNUM + reg_vd;
11061 }
11062 else
11063 {
11064 reg_vd = (reg_vd << 1) | bit (arm_insn_r->arm_insn, 22);
11065 record_buf[0] = num_regs + reg_vd;
11066 }
11067 arm_insn_r->reg_rec_count = 1;
11068 }
11069
11070 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11071 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11072 return 0;
11073}
11074
851f26ae
OJ
11075/* Record handler for arm/thumb mode VFP data processing instructions. */
11076
11077static int
11078arm_record_vfp_data_proc_insn (insn_decode_record *arm_insn_r)
11079{
11080 uint32_t opc1, opc2, opc3, dp_op_sz, bit_d, reg_vd;
11081 uint32_t record_buf[4];
11082 enum insn_types {INSN_T0, INSN_T1, INSN_T2, INSN_T3, INSN_INV};
11083 enum insn_types curr_insn_type = INSN_INV;
11084
11085 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
11086 opc1 = bits (arm_insn_r->arm_insn, 20, 23);
11087 opc2 = bits (arm_insn_r->arm_insn, 16, 19);
11088 opc3 = bits (arm_insn_r->arm_insn, 6, 7);
11089 dp_op_sz = bit (arm_insn_r->arm_insn, 8);
11090 bit_d = bit (arm_insn_r->arm_insn, 22);
11091 opc1 = opc1 & 0x04;
11092
11093 /* Handle VMLA, VMLS. */
11094 if (opc1 == 0x00)
11095 {
11096 if (bit (arm_insn_r->arm_insn, 10))
11097 {
11098 if (bit (arm_insn_r->arm_insn, 6))
11099 curr_insn_type = INSN_T0;
11100 else
11101 curr_insn_type = INSN_T1;
11102 }
11103 else
11104 {
11105 if (dp_op_sz)
11106 curr_insn_type = INSN_T1;
11107 else
11108 curr_insn_type = INSN_T2;
11109 }
11110 }
11111 /* Handle VNMLA, VNMLS, VNMUL. */
11112 else if (opc1 == 0x01)
11113 {
11114 if (dp_op_sz)
11115 curr_insn_type = INSN_T1;
11116 else
11117 curr_insn_type = INSN_T2;
11118 }
11119 /* Handle VMUL. */
11120 else if (opc1 == 0x02 && !(opc3 & 0x01))
11121 {
11122 if (bit (arm_insn_r->arm_insn, 10))
11123 {
11124 if (bit (arm_insn_r->arm_insn, 6))
11125 curr_insn_type = INSN_T0;
11126 else
11127 curr_insn_type = INSN_T1;
11128 }
11129 else
11130 {
11131 if (dp_op_sz)
11132 curr_insn_type = INSN_T1;
11133 else
11134 curr_insn_type = INSN_T2;
11135 }
11136 }
11137 /* Handle VADD, VSUB. */
11138 else if (opc1 == 0x03)
11139 {
11140 if (!bit (arm_insn_r->arm_insn, 9))
11141 {
11142 if (bit (arm_insn_r->arm_insn, 6))
11143 curr_insn_type = INSN_T0;
11144 else
11145 curr_insn_type = INSN_T1;
11146 }
11147 else
11148 {
11149 if (dp_op_sz)
11150 curr_insn_type = INSN_T1;
11151 else
11152 curr_insn_type = INSN_T2;
11153 }
11154 }
11155 /* Handle VDIV. */
11156 else if (opc1 == 0x0b)
11157 {
11158 if (dp_op_sz)
11159 curr_insn_type = INSN_T1;
11160 else
11161 curr_insn_type = INSN_T2;
11162 }
11163 /* Handle all other vfp data processing instructions. */
11164 else if (opc1 == 0x0b)
11165 {
11166 /* Handle VMOV. */
11167 if (!(opc3 & 0x01) || (opc2 == 0x00 && opc3 == 0x01))
11168 {
11169 if (bit (arm_insn_r->arm_insn, 4))
11170 {
11171 if (bit (arm_insn_r->arm_insn, 6))
11172 curr_insn_type = INSN_T0;
11173 else
11174 curr_insn_type = INSN_T1;
11175 }
11176 else
11177 {
11178 if (dp_op_sz)
11179 curr_insn_type = INSN_T1;
11180 else
11181 curr_insn_type = INSN_T2;
11182 }
11183 }
11184 /* Handle VNEG and VABS. */
11185 else if ((opc2 == 0x01 && opc3 == 0x01)
11186 || (opc2 == 0x00 && opc3 == 0x03))
11187 {
11188 if (!bit (arm_insn_r->arm_insn, 11))
11189 {
11190 if (bit (arm_insn_r->arm_insn, 6))
11191 curr_insn_type = INSN_T0;
11192 else
11193 curr_insn_type = INSN_T1;
11194 }
11195 else
11196 {
11197 if (dp_op_sz)
11198 curr_insn_type = INSN_T1;
11199 else
11200 curr_insn_type = INSN_T2;
11201 }
11202 }
11203 /* Handle VSQRT. */
11204 else if (opc2 == 0x01 && opc3 == 0x03)
11205 {
11206 if (dp_op_sz)
11207 curr_insn_type = INSN_T1;
11208 else
11209 curr_insn_type = INSN_T2;
11210 }
11211 /* Handle VCVT. */
11212 else if (opc2 == 0x07 && opc3 == 0x03)
11213 {
11214 if (!dp_op_sz)
11215 curr_insn_type = INSN_T1;
11216 else
11217 curr_insn_type = INSN_T2;
11218 }
11219 else if (opc3 & 0x01)
11220 {
11221 /* Handle VCVT. */
11222 if ((opc2 == 0x08) || (opc2 & 0x0e) == 0x0c)
11223 {
11224 if (!bit (arm_insn_r->arm_insn, 18))
11225 curr_insn_type = INSN_T2;
11226 else
11227 {
11228 if (dp_op_sz)
11229 curr_insn_type = INSN_T1;
11230 else
11231 curr_insn_type = INSN_T2;
11232 }
11233 }
11234 /* Handle VCVT. */
11235 else if ((opc2 & 0x0e) == 0x0a || (opc2 & 0x0e) == 0x0e)
11236 {
11237 if (dp_op_sz)
11238 curr_insn_type = INSN_T1;
11239 else
11240 curr_insn_type = INSN_T2;
11241 }
11242 /* Handle VCVTB, VCVTT. */
11243 else if ((opc2 & 0x0e) == 0x02)
11244 curr_insn_type = INSN_T2;
11245 /* Handle VCMP, VCMPE. */
11246 else if ((opc2 & 0x0e) == 0x04)
11247 curr_insn_type = INSN_T3;
11248 }
11249 }
11250
11251 switch (curr_insn_type)
11252 {
11253 case INSN_T0:
11254 reg_vd = reg_vd | (bit_d << 4);
11255 record_buf[0] = reg_vd + ARM_D0_REGNUM;
11256 record_buf[1] = reg_vd + ARM_D0_REGNUM + 1;
11257 arm_insn_r->reg_rec_count = 2;
11258 break;
11259
11260 case INSN_T1:
11261 reg_vd = reg_vd | (bit_d << 4);
11262 record_buf[0] = reg_vd + ARM_D0_REGNUM;
11263 arm_insn_r->reg_rec_count = 1;
11264 break;
11265
11266 case INSN_T2:
11267 reg_vd = (reg_vd << 1) | bit_d;
11268 record_buf[0] = reg_vd + ARM_D0_REGNUM;
11269 arm_insn_r->reg_rec_count = 1;
11270 break;
11271
11272 case INSN_T3:
11273 record_buf[0] = ARM_FPSCR_REGNUM;
11274 arm_insn_r->reg_rec_count = 1;
11275 break;
11276
11277 default:
11278 gdb_assert_not_reached ("no decoding pattern found");
11279 break;
11280 }
11281
11282 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11283 return 0;
11284}
11285
60cc5e93
OJ
11286/* Handling opcode 110 insns. */
11287
11288static int
11289arm_record_asimd_vfp_coproc (insn_decode_record *arm_insn_r)
11290{
bec2ab5a 11291 uint32_t op1, op1_ebit, coproc;
60cc5e93
OJ
11292
11293 coproc = bits (arm_insn_r->arm_insn, 8, 11);
11294 op1 = bits (arm_insn_r->arm_insn, 20, 25);
11295 op1_ebit = bit (arm_insn_r->arm_insn, 20);
11296
11297 if ((coproc & 0x0e) == 0x0a)
11298 {
11299 /* Handle extension register ld/st instructions. */
11300 if (!(op1 & 0x20))
f20f80dd 11301 return arm_record_exreg_ld_st_insn (arm_insn_r);
60cc5e93
OJ
11302
11303 /* 64-bit transfers between arm core and extension registers. */
11304 if ((op1 & 0x3e) == 0x04)
f20f80dd 11305 return arm_record_exreg_ld_st_insn (arm_insn_r);
60cc5e93
OJ
11306 }
11307 else
11308 {
11309 /* Handle coprocessor ld/st instructions. */
11310 if (!(op1 & 0x3a))
11311 {
11312 /* Store. */
11313 if (!op1_ebit)
11314 return arm_record_unsupported_insn (arm_insn_r);
11315 else
11316 /* Load. */
11317 return arm_record_unsupported_insn (arm_insn_r);
11318 }
11319
11320 /* Move to coprocessor from two arm core registers. */
11321 if (op1 == 0x4)
11322 return arm_record_unsupported_insn (arm_insn_r);
11323
11324 /* Move to two arm core registers from coprocessor. */
11325 if (op1 == 0x5)
11326 {
11327 uint32_t reg_t[2];
11328
11329 reg_t[0] = bits (arm_insn_r->arm_insn, 12, 15);
11330 reg_t[1] = bits (arm_insn_r->arm_insn, 16, 19);
11331 arm_insn_r->reg_rec_count = 2;
11332
11333 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, reg_t);
11334 return 0;
11335 }
11336 }
11337 return arm_record_unsupported_insn (arm_insn_r);
11338}
11339
72508ac0
PO
11340/* Handling opcode 111 insns. */
11341
11342static int
11343arm_record_coproc_data_proc (insn_decode_record *arm_insn_r)
11344{
60cc5e93 11345 uint32_t op, op1_sbit, op1_ebit, coproc;
72508ac0
PO
11346 struct gdbarch_tdep *tdep = gdbarch_tdep (arm_insn_r->gdbarch);
11347 struct regcache *reg_cache = arm_insn_r->regcache;
72508ac0
PO
11348
11349 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 24, 27);
60cc5e93
OJ
11350 coproc = bits (arm_insn_r->arm_insn, 8, 11);
11351 op1_sbit = bit (arm_insn_r->arm_insn, 24);
11352 op1_ebit = bit (arm_insn_r->arm_insn, 20);
11353 op = bit (arm_insn_r->arm_insn, 4);
97dfe206
OJ
11354
11355 /* Handle arm SWI/SVC system call instructions. */
60cc5e93 11356 if (op1_sbit)
97dfe206
OJ
11357 {
11358 if (tdep->arm_syscall_record != NULL)
11359 {
11360 ULONGEST svc_operand, svc_number;
11361
11362 svc_operand = (0x00ffffff & arm_insn_r->arm_insn);
11363
11364 if (svc_operand) /* OABI. */
11365 svc_number = svc_operand - 0x900000;
11366 else /* EABI. */
11367 regcache_raw_read_unsigned (reg_cache, 7, &svc_number);
11368
60cc5e93 11369 return tdep->arm_syscall_record (reg_cache, svc_number);
97dfe206
OJ
11370 }
11371 else
11372 {
11373 printf_unfiltered (_("no syscall record support\n"));
60cc5e93 11374 return -1;
97dfe206
OJ
11375 }
11376 }
60cc5e93
OJ
11377
11378 if ((coproc & 0x0e) == 0x0a)
11379 {
11380 /* VFP data-processing instructions. */
11381 if (!op1_sbit && !op)
851f26ae 11382 return arm_record_vfp_data_proc_insn (arm_insn_r);
60cc5e93
OJ
11383
11384 /* Advanced SIMD, VFP instructions. */
11385 if (!op1_sbit && op)
5a578da5 11386 return arm_record_vdata_transfer_insn (arm_insn_r);
60cc5e93 11387 }
97dfe206
OJ
11388 else
11389 {
60cc5e93
OJ
11390 /* Coprocessor data operations. */
11391 if (!op1_sbit && !op)
11392 return arm_record_unsupported_insn (arm_insn_r);
11393
11394 /* Move to Coprocessor from ARM core register. */
11395 if (!op1_sbit && !op1_ebit && op)
11396 return arm_record_unsupported_insn (arm_insn_r);
11397
11398 /* Move to arm core register from coprocessor. */
11399 if (!op1_sbit && op1_ebit && op)
11400 {
11401 uint32_t record_buf[1];
11402
11403 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11404 if (record_buf[0] == 15)
11405 record_buf[0] = ARM_PS_REGNUM;
11406
11407 arm_insn_r->reg_rec_count = 1;
11408 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count,
11409 record_buf);
11410 return 0;
11411 }
97dfe206 11412 }
72508ac0 11413
60cc5e93 11414 return arm_record_unsupported_insn (arm_insn_r);
72508ac0
PO
11415}
11416
11417/* Handling opcode 000 insns. */
11418
11419static int
11420thumb_record_shift_add_sub (insn_decode_record *thumb_insn_r)
11421{
11422 uint32_t record_buf[8];
11423 uint32_t reg_src1 = 0;
11424
11425 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11426
11427 record_buf[0] = ARM_PS_REGNUM;
11428 record_buf[1] = reg_src1;
11429 thumb_insn_r->reg_rec_count = 2;
11430
11431 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11432
11433 return 0;
11434}
11435
11436
11437/* Handling opcode 001 insns. */
11438
11439static int
11440thumb_record_add_sub_cmp_mov (insn_decode_record *thumb_insn_r)
11441{
11442 uint32_t record_buf[8];
11443 uint32_t reg_src1 = 0;
11444
11445 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11446
11447 record_buf[0] = ARM_PS_REGNUM;
11448 record_buf[1] = reg_src1;
11449 thumb_insn_r->reg_rec_count = 2;
11450
11451 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11452
11453 return 0;
11454}
11455
11456/* Handling opcode 010 insns. */
11457
11458static int
11459thumb_record_ld_st_reg_offset (insn_decode_record *thumb_insn_r)
11460{
11461 struct regcache *reg_cache = thumb_insn_r->regcache;
11462 uint32_t record_buf[8], record_buf_mem[8];
11463
11464 uint32_t reg_src1 = 0, reg_src2 = 0;
11465 uint32_t opcode1 = 0, opcode2 = 0, opcode3 = 0;
11466
11467 ULONGEST u_regval[2] = {0};
11468
11469 opcode1 = bits (thumb_insn_r->arm_insn, 10, 12);
11470
11471 if (bit (thumb_insn_r->arm_insn, 12))
11472 {
11473 /* Handle load/store register offset. */
11474 opcode2 = bits (thumb_insn_r->arm_insn, 9, 10);
11475 if (opcode2 >= 12 && opcode2 <= 15)
11476 {
11477 /* LDR(2), LDRB(2) , LDRH(2), LDRSB, LDRSH. */
11478 reg_src1 = bits (thumb_insn_r->arm_insn,0, 2);
11479 record_buf[0] = reg_src1;
11480 thumb_insn_r->reg_rec_count = 1;
11481 }
11482 else if (opcode2 >= 8 && opcode2 <= 10)
11483 {
11484 /* STR(2), STRB(2), STRH(2) . */
11485 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
11486 reg_src2 = bits (thumb_insn_r->arm_insn, 6, 8);
11487 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11488 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11489 if (8 == opcode2)
11490 record_buf_mem[0] = 4; /* STR (2). */
11491 else if (10 == opcode2)
11492 record_buf_mem[0] = 1; /* STRB (2). */
11493 else if (9 == opcode2)
11494 record_buf_mem[0] = 2; /* STRH (2). */
11495 record_buf_mem[1] = u_regval[0] + u_regval[1];
11496 thumb_insn_r->mem_rec_count = 1;
11497 }
11498 }
11499 else if (bit (thumb_insn_r->arm_insn, 11))
11500 {
11501 /* Handle load from literal pool. */
11502 /* LDR(3). */
11503 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11504 record_buf[0] = reg_src1;
11505 thumb_insn_r->reg_rec_count = 1;
11506 }
11507 else if (opcode1)
11508 {
11509 opcode2 = bits (thumb_insn_r->arm_insn, 8, 9);
11510 opcode3 = bits (thumb_insn_r->arm_insn, 0, 2);
11511 if ((3 == opcode2) && (!opcode3))
11512 {
11513 /* Branch with exchange. */
11514 record_buf[0] = ARM_PS_REGNUM;
11515 thumb_insn_r->reg_rec_count = 1;
11516 }
11517 else
11518 {
11519 /* Format 8; special data processing insns. */
11520 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11521 record_buf[0] = ARM_PS_REGNUM;
11522 record_buf[1] = reg_src1;
11523 thumb_insn_r->reg_rec_count = 2;
11524 }
11525 }
11526 else
11527 {
11528 /* Format 5; data processing insns. */
11529 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11530 if (bit (thumb_insn_r->arm_insn, 7))
11531 {
11532 reg_src1 = reg_src1 + 8;
11533 }
11534 record_buf[0] = ARM_PS_REGNUM;
11535 record_buf[1] = reg_src1;
11536 thumb_insn_r->reg_rec_count = 2;
11537 }
11538
11539 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11540 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
11541 record_buf_mem);
11542
11543 return 0;
11544}
11545
11546/* Handling opcode 001 insns. */
11547
11548static int
11549thumb_record_ld_st_imm_offset (insn_decode_record *thumb_insn_r)
11550{
11551 struct regcache *reg_cache = thumb_insn_r->regcache;
11552 uint32_t record_buf[8], record_buf_mem[8];
11553
11554 uint32_t reg_src1 = 0;
11555 uint32_t opcode = 0, immed_5 = 0;
11556
11557 ULONGEST u_regval = 0;
11558
11559 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
11560
11561 if (opcode)
11562 {
11563 /* LDR(1). */
11564 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11565 record_buf[0] = reg_src1;
11566 thumb_insn_r->reg_rec_count = 1;
11567 }
11568 else
11569 {
11570 /* STR(1). */
11571 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
11572 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
11573 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11574 record_buf_mem[0] = 4;
11575 record_buf_mem[1] = u_regval + (immed_5 * 4);
11576 thumb_insn_r->mem_rec_count = 1;
11577 }
11578
11579 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11580 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
11581 record_buf_mem);
11582
11583 return 0;
11584}
11585
11586/* Handling opcode 100 insns. */
11587
11588static int
11589thumb_record_ld_st_stack (insn_decode_record *thumb_insn_r)
11590{
11591 struct regcache *reg_cache = thumb_insn_r->regcache;
11592 uint32_t record_buf[8], record_buf_mem[8];
11593
11594 uint32_t reg_src1 = 0;
11595 uint32_t opcode = 0, immed_8 = 0, immed_5 = 0;
11596
11597 ULONGEST u_regval = 0;
11598
11599 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
11600
11601 if (3 == opcode)
11602 {
11603 /* LDR(4). */
11604 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11605 record_buf[0] = reg_src1;
11606 thumb_insn_r->reg_rec_count = 1;
11607 }
11608 else if (1 == opcode)
11609 {
11610 /* LDRH(1). */
11611 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11612 record_buf[0] = reg_src1;
11613 thumb_insn_r->reg_rec_count = 1;
11614 }
11615 else if (2 == opcode)
11616 {
11617 /* STR(3). */
11618 immed_8 = bits (thumb_insn_r->arm_insn, 0, 7);
11619 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
11620 record_buf_mem[0] = 4;
11621 record_buf_mem[1] = u_regval + (immed_8 * 4);
11622 thumb_insn_r->mem_rec_count = 1;
11623 }
11624 else if (0 == opcode)
11625 {
11626 /* STRH(1). */
11627 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
11628 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
11629 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11630 record_buf_mem[0] = 2;
11631 record_buf_mem[1] = u_regval + (immed_5 * 2);
11632 thumb_insn_r->mem_rec_count = 1;
11633 }
11634
11635 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11636 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
11637 record_buf_mem);
11638
11639 return 0;
11640}
11641
11642/* Handling opcode 101 insns. */
11643
11644static int
11645thumb_record_misc (insn_decode_record *thumb_insn_r)
11646{
11647 struct regcache *reg_cache = thumb_insn_r->regcache;
11648
11649 uint32_t opcode = 0, opcode1 = 0, opcode2 = 0;
11650 uint32_t register_bits = 0, register_count = 0;
bec2ab5a 11651 uint32_t index = 0, start_address = 0;
72508ac0
PO
11652 uint32_t record_buf[24], record_buf_mem[48];
11653 uint32_t reg_src1;
11654
11655 ULONGEST u_regval = 0;
11656
11657 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
11658 opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
11659 opcode2 = bits (thumb_insn_r->arm_insn, 9, 12);
11660
11661 if (14 == opcode2)
11662 {
11663 /* POP. */
11664 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
11665 while (register_bits)
f969241e
OJ
11666 {
11667 if (register_bits & 0x00000001)
11668 record_buf[index++] = register_count;
11669 register_bits = register_bits >> 1;
11670 register_count++;
11671 }
11672 record_buf[index++] = ARM_PS_REGNUM;
11673 record_buf[index++] = ARM_SP_REGNUM;
11674 thumb_insn_r->reg_rec_count = index;
72508ac0
PO
11675 }
11676 else if (10 == opcode2)
11677 {
11678 /* PUSH. */
11679 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
9904a494 11680 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
72508ac0
PO
11681 while (register_bits)
11682 {
11683 if (register_bits & 0x00000001)
11684 register_count++;
11685 register_bits = register_bits >> 1;
11686 }
11687 start_address = u_regval - \
11688 (4 * (bit (thumb_insn_r->arm_insn, 8) + register_count));
11689 thumb_insn_r->mem_rec_count = register_count;
11690 while (register_count)
11691 {
11692 record_buf_mem[(register_count * 2) - 1] = start_address;
11693 record_buf_mem[(register_count * 2) - 2] = 4;
11694 start_address = start_address + 4;
11695 register_count--;
11696 }
11697 record_buf[0] = ARM_SP_REGNUM;
11698 thumb_insn_r->reg_rec_count = 1;
11699 }
11700 else if (0x1E == opcode1)
11701 {
11702 /* BKPT insn. */
11703 /* Handle enhanced software breakpoint insn, BKPT. */
11704 /* CPSR is changed to be executed in ARM state, disabling normal
11705 interrupts, entering abort mode. */
11706 /* According to high vector configuration PC is set. */
11707 /* User hits breakpoint and type reverse, in that case, we need to go back with
11708 previous CPSR and Program Counter. */
11709 record_buf[0] = ARM_PS_REGNUM;
11710 record_buf[1] = ARM_LR_REGNUM;
11711 thumb_insn_r->reg_rec_count = 2;
11712 /* We need to save SPSR value, which is not yet done. */
11713 printf_unfiltered (_("Process record does not support instruction "
11714 "0x%0x at address %s.\n"),
11715 thumb_insn_r->arm_insn,
11716 paddress (thumb_insn_r->gdbarch,
11717 thumb_insn_r->this_addr));
11718 return -1;
11719 }
11720 else if ((0 == opcode) || (1 == opcode))
11721 {
11722 /* ADD(5), ADD(6). */
11723 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11724 record_buf[0] = reg_src1;
11725 thumb_insn_r->reg_rec_count = 1;
11726 }
11727 else if (2 == opcode)
11728 {
11729 /* ADD(7), SUB(4). */
11730 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11731 record_buf[0] = ARM_SP_REGNUM;
11732 thumb_insn_r->reg_rec_count = 1;
11733 }
11734
11735 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11736 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
11737 record_buf_mem);
11738
11739 return 0;
11740}
11741
11742/* Handling opcode 110 insns. */
11743
11744static int
11745thumb_record_ldm_stm_swi (insn_decode_record *thumb_insn_r)
11746{
11747 struct gdbarch_tdep *tdep = gdbarch_tdep (thumb_insn_r->gdbarch);
11748 struct regcache *reg_cache = thumb_insn_r->regcache;
11749
11750 uint32_t ret = 0; /* function return value: -1:record failure ; 0:success */
11751 uint32_t reg_src1 = 0;
11752 uint32_t opcode1 = 0, opcode2 = 0, register_bits = 0, register_count = 0;
bec2ab5a 11753 uint32_t index = 0, start_address = 0;
72508ac0
PO
11754 uint32_t record_buf[24], record_buf_mem[48];
11755
11756 ULONGEST u_regval = 0;
11757
11758 opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
11759 opcode2 = bits (thumb_insn_r->arm_insn, 11, 12);
11760
11761 if (1 == opcode2)
11762 {
11763
11764 /* LDMIA. */
11765 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
11766 /* Get Rn. */
11767 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11768 while (register_bits)
11769 {
11770 if (register_bits & 0x00000001)
f969241e 11771 record_buf[index++] = register_count;
72508ac0 11772 register_bits = register_bits >> 1;
f969241e 11773 register_count++;
72508ac0 11774 }
f969241e
OJ
11775 record_buf[index++] = reg_src1;
11776 thumb_insn_r->reg_rec_count = index;
72508ac0
PO
11777 }
11778 else if (0 == opcode2)
11779 {
11780 /* It handles both STMIA. */
11781 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
11782 /* Get Rn. */
11783 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11784 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11785 while (register_bits)
11786 {
11787 if (register_bits & 0x00000001)
11788 register_count++;
11789 register_bits = register_bits >> 1;
11790 }
11791 start_address = u_regval;
11792 thumb_insn_r->mem_rec_count = register_count;
11793 while (register_count)
11794 {
11795 record_buf_mem[(register_count * 2) - 1] = start_address;
11796 record_buf_mem[(register_count * 2) - 2] = 4;
11797 start_address = start_address + 4;
11798 register_count--;
11799 }
11800 }
11801 else if (0x1F == opcode1)
11802 {
11803 /* Handle arm syscall insn. */
97dfe206 11804 if (tdep->arm_syscall_record != NULL)
72508ac0 11805 {
97dfe206
OJ
11806 regcache_raw_read_unsigned (reg_cache, 7, &u_regval);
11807 ret = tdep->arm_syscall_record (reg_cache, u_regval);
72508ac0
PO
11808 }
11809 else
11810 {
11811 printf_unfiltered (_("no syscall record support\n"));
11812 return -1;
11813 }
11814 }
11815
11816 /* B (1), conditional branch is automatically taken care in process_record,
11817 as PC is saved there. */
11818
11819 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11820 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
11821 record_buf_mem);
11822
11823 return ret;
11824}
11825
11826/* Handling opcode 111 insns. */
11827
11828static int
11829thumb_record_branch (insn_decode_record *thumb_insn_r)
11830{
11831 uint32_t record_buf[8];
11832 uint32_t bits_h = 0;
11833
11834 bits_h = bits (thumb_insn_r->arm_insn, 11, 12);
11835
11836 if (2 == bits_h || 3 == bits_h)
11837 {
11838 /* BL */
11839 record_buf[0] = ARM_LR_REGNUM;
11840 thumb_insn_r->reg_rec_count = 1;
11841 }
11842 else if (1 == bits_h)
11843 {
11844 /* BLX(1). */
11845 record_buf[0] = ARM_PS_REGNUM;
11846 record_buf[1] = ARM_LR_REGNUM;
11847 thumb_insn_r->reg_rec_count = 2;
11848 }
11849
11850 /* B(2) is automatically taken care in process_record, as PC is
11851 saved there. */
11852
11853 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11854
11855 return 0;
11856}
11857
c6ec2b30
OJ
11858/* Handler for thumb2 load/store multiple instructions. */
11859
11860static int
11861thumb2_record_ld_st_multiple (insn_decode_record *thumb2_insn_r)
11862{
11863 struct regcache *reg_cache = thumb2_insn_r->regcache;
11864
11865 uint32_t reg_rn, op;
11866 uint32_t register_bits = 0, register_count = 0;
11867 uint32_t index = 0, start_address = 0;
11868 uint32_t record_buf[24], record_buf_mem[48];
11869
11870 ULONGEST u_regval = 0;
11871
11872 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
11873 op = bits (thumb2_insn_r->arm_insn, 23, 24);
11874
11875 if (0 == op || 3 == op)
11876 {
11877 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11878 {
11879 /* Handle RFE instruction. */
11880 record_buf[0] = ARM_PS_REGNUM;
11881 thumb2_insn_r->reg_rec_count = 1;
11882 }
11883 else
11884 {
11885 /* Handle SRS instruction after reading banked SP. */
11886 return arm_record_unsupported_insn (thumb2_insn_r);
11887 }
11888 }
11889 else if (1 == op || 2 == op)
11890 {
11891 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11892 {
11893 /* Handle LDM/LDMIA/LDMFD and LDMDB/LDMEA instructions. */
11894 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
11895 while (register_bits)
11896 {
11897 if (register_bits & 0x00000001)
11898 record_buf[index++] = register_count;
11899
11900 register_count++;
11901 register_bits = register_bits >> 1;
11902 }
11903 record_buf[index++] = reg_rn;
11904 record_buf[index++] = ARM_PS_REGNUM;
11905 thumb2_insn_r->reg_rec_count = index;
11906 }
11907 else
11908 {
11909 /* Handle STM/STMIA/STMEA and STMDB/STMFD. */
11910 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
11911 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
11912 while (register_bits)
11913 {
11914 if (register_bits & 0x00000001)
11915 register_count++;
11916
11917 register_bits = register_bits >> 1;
11918 }
11919
11920 if (1 == op)
11921 {
11922 /* Start address calculation for LDMDB/LDMEA. */
11923 start_address = u_regval;
11924 }
11925 else if (2 == op)
11926 {
11927 /* Start address calculation for LDMDB/LDMEA. */
11928 start_address = u_regval - register_count * 4;
11929 }
11930
11931 thumb2_insn_r->mem_rec_count = register_count;
11932 while (register_count)
11933 {
11934 record_buf_mem[register_count * 2 - 1] = start_address;
11935 record_buf_mem[register_count * 2 - 2] = 4;
11936 start_address = start_address + 4;
11937 register_count--;
11938 }
11939 record_buf[0] = reg_rn;
11940 record_buf[1] = ARM_PS_REGNUM;
11941 thumb2_insn_r->reg_rec_count = 2;
11942 }
11943 }
11944
11945 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
11946 record_buf_mem);
11947 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
11948 record_buf);
11949 return ARM_RECORD_SUCCESS;
11950}
11951
11952/* Handler for thumb2 load/store (dual/exclusive) and table branch
11953 instructions. */
11954
11955static int
11956thumb2_record_ld_st_dual_ex_tbb (insn_decode_record *thumb2_insn_r)
11957{
11958 struct regcache *reg_cache = thumb2_insn_r->regcache;
11959
11960 uint32_t reg_rd, reg_rn, offset_imm;
11961 uint32_t reg_dest1, reg_dest2;
11962 uint32_t address, offset_addr;
11963 uint32_t record_buf[8], record_buf_mem[8];
11964 uint32_t op1, op2, op3;
c6ec2b30
OJ
11965
11966 ULONGEST u_regval[2];
11967
11968 op1 = bits (thumb2_insn_r->arm_insn, 23, 24);
11969 op2 = bits (thumb2_insn_r->arm_insn, 20, 21);
11970 op3 = bits (thumb2_insn_r->arm_insn, 4, 7);
11971
11972 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11973 {
11974 if(!(1 == op1 && 1 == op2 && (0 == op3 || 1 == op3)))
11975 {
11976 reg_dest1 = bits (thumb2_insn_r->arm_insn, 12, 15);
11977 record_buf[0] = reg_dest1;
11978 record_buf[1] = ARM_PS_REGNUM;
11979 thumb2_insn_r->reg_rec_count = 2;
11980 }
11981
11982 if (3 == op2 || (op1 & 2) || (1 == op1 && 1 == op2 && 7 == op3))
11983 {
11984 reg_dest2 = bits (thumb2_insn_r->arm_insn, 8, 11);
11985 record_buf[2] = reg_dest2;
11986 thumb2_insn_r->reg_rec_count = 3;
11987 }
11988 }
11989 else
11990 {
11991 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
11992 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
11993
11994 if (0 == op1 && 0 == op2)
11995 {
11996 /* Handle STREX. */
11997 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
11998 address = u_regval[0] + (offset_imm * 4);
11999 record_buf_mem[0] = 4;
12000 record_buf_mem[1] = address;
12001 thumb2_insn_r->mem_rec_count = 1;
12002 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
12003 record_buf[0] = reg_rd;
12004 thumb2_insn_r->reg_rec_count = 1;
12005 }
12006 else if (1 == op1 && 0 == op2)
12007 {
12008 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
12009 record_buf[0] = reg_rd;
12010 thumb2_insn_r->reg_rec_count = 1;
12011 address = u_regval[0];
12012 record_buf_mem[1] = address;
12013
12014 if (4 == op3)
12015 {
12016 /* Handle STREXB. */
12017 record_buf_mem[0] = 1;
12018 thumb2_insn_r->mem_rec_count = 1;
12019 }
12020 else if (5 == op3)
12021 {
12022 /* Handle STREXH. */
12023 record_buf_mem[0] = 2 ;
12024 thumb2_insn_r->mem_rec_count = 1;
12025 }
12026 else if (7 == op3)
12027 {
12028 /* Handle STREXD. */
12029 address = u_regval[0];
12030 record_buf_mem[0] = 4;
12031 record_buf_mem[2] = 4;
12032 record_buf_mem[3] = address + 4;
12033 thumb2_insn_r->mem_rec_count = 2;
12034 }
12035 }
12036 else
12037 {
12038 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
12039
12040 if (bit (thumb2_insn_r->arm_insn, 24))
12041 {
12042 if (bit (thumb2_insn_r->arm_insn, 23))
12043 offset_addr = u_regval[0] + (offset_imm * 4);
12044 else
12045 offset_addr = u_regval[0] - (offset_imm * 4);
12046
12047 address = offset_addr;
12048 }
12049 else
12050 address = u_regval[0];
12051
12052 record_buf_mem[0] = 4;
12053 record_buf_mem[1] = address;
12054 record_buf_mem[2] = 4;
12055 record_buf_mem[3] = address + 4;
12056 thumb2_insn_r->mem_rec_count = 2;
12057 record_buf[0] = reg_rn;
12058 thumb2_insn_r->reg_rec_count = 1;
12059 }
12060 }
12061
12062 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12063 record_buf);
12064 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
12065 record_buf_mem);
12066 return ARM_RECORD_SUCCESS;
12067}
12068
12069/* Handler for thumb2 data processing (shift register and modified immediate)
12070 instructions. */
12071
12072static int
12073thumb2_record_data_proc_sreg_mimm (insn_decode_record *thumb2_insn_r)
12074{
12075 uint32_t reg_rd, op;
12076 uint32_t record_buf[8];
12077
12078 op = bits (thumb2_insn_r->arm_insn, 21, 24);
12079 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
12080
12081 if ((0 == op || 4 == op || 8 == op || 13 == op) && 15 == reg_rd)
12082 {
12083 record_buf[0] = ARM_PS_REGNUM;
12084 thumb2_insn_r->reg_rec_count = 1;
12085 }
12086 else
12087 {
12088 record_buf[0] = reg_rd;
12089 record_buf[1] = ARM_PS_REGNUM;
12090 thumb2_insn_r->reg_rec_count = 2;
12091 }
12092
12093 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12094 record_buf);
12095 return ARM_RECORD_SUCCESS;
12096}
12097
12098/* Generic handler for thumb2 instructions which effect destination and PS
12099 registers. */
12100
12101static int
12102thumb2_record_ps_dest_generic (insn_decode_record *thumb2_insn_r)
12103{
12104 uint32_t reg_rd;
12105 uint32_t record_buf[8];
12106
12107 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
12108
12109 record_buf[0] = reg_rd;
12110 record_buf[1] = ARM_PS_REGNUM;
12111 thumb2_insn_r->reg_rec_count = 2;
12112
12113 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12114 record_buf);
12115 return ARM_RECORD_SUCCESS;
12116}
12117
12118/* Handler for thumb2 branch and miscellaneous control instructions. */
12119
12120static int
12121thumb2_record_branch_misc_cntrl (insn_decode_record *thumb2_insn_r)
12122{
12123 uint32_t op, op1, op2;
12124 uint32_t record_buf[8];
12125
12126 op = bits (thumb2_insn_r->arm_insn, 20, 26);
12127 op1 = bits (thumb2_insn_r->arm_insn, 12, 14);
12128 op2 = bits (thumb2_insn_r->arm_insn, 8, 11);
12129
12130 /* Handle MSR insn. */
12131 if (!(op1 & 0x2) && 0x38 == op)
12132 {
12133 if (!(op2 & 0x3))
12134 {
12135 /* CPSR is going to be changed. */
12136 record_buf[0] = ARM_PS_REGNUM;
12137 thumb2_insn_r->reg_rec_count = 1;
12138 }
12139 else
12140 {
12141 arm_record_unsupported_insn(thumb2_insn_r);
12142 return -1;
12143 }
12144 }
12145 else if (4 == (op1 & 0x5) || 5 == (op1 & 0x5))
12146 {
12147 /* BLX. */
12148 record_buf[0] = ARM_PS_REGNUM;
12149 record_buf[1] = ARM_LR_REGNUM;
12150 thumb2_insn_r->reg_rec_count = 2;
12151 }
12152
12153 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12154 record_buf);
12155 return ARM_RECORD_SUCCESS;
12156}
12157
12158/* Handler for thumb2 store single data item instructions. */
12159
12160static int
12161thumb2_record_str_single_data (insn_decode_record *thumb2_insn_r)
12162{
12163 struct regcache *reg_cache = thumb2_insn_r->regcache;
12164
12165 uint32_t reg_rn, reg_rm, offset_imm, shift_imm;
12166 uint32_t address, offset_addr;
12167 uint32_t record_buf[8], record_buf_mem[8];
12168 uint32_t op1, op2;
12169
12170 ULONGEST u_regval[2];
12171
12172 op1 = bits (thumb2_insn_r->arm_insn, 21, 23);
12173 op2 = bits (thumb2_insn_r->arm_insn, 6, 11);
12174 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12175 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
12176
12177 if (bit (thumb2_insn_r->arm_insn, 23))
12178 {
12179 /* T2 encoding. */
12180 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 11);
12181 offset_addr = u_regval[0] + offset_imm;
12182 address = offset_addr;
12183 }
12184 else
12185 {
12186 /* T3 encoding. */
12187 if ((0 == op1 || 1 == op1 || 2 == op1) && !(op2 & 0x20))
12188 {
12189 /* Handle STRB (register). */
12190 reg_rm = bits (thumb2_insn_r->arm_insn, 0, 3);
12191 regcache_raw_read_unsigned (reg_cache, reg_rm, &u_regval[1]);
12192 shift_imm = bits (thumb2_insn_r->arm_insn, 4, 5);
12193 offset_addr = u_regval[1] << shift_imm;
12194 address = u_regval[0] + offset_addr;
12195 }
12196 else
12197 {
12198 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
12199 if (bit (thumb2_insn_r->arm_insn, 10))
12200 {
12201 if (bit (thumb2_insn_r->arm_insn, 9))
12202 offset_addr = u_regval[0] + offset_imm;
12203 else
12204 offset_addr = u_regval[0] - offset_imm;
12205
12206 address = offset_addr;
12207 }
12208 else
12209 address = u_regval[0];
12210 }
12211 }
12212
12213 switch (op1)
12214 {
12215 /* Store byte instructions. */
12216 case 4:
12217 case 0:
12218 record_buf_mem[0] = 1;
12219 break;
12220 /* Store half word instructions. */
12221 case 1:
12222 case 5:
12223 record_buf_mem[0] = 2;
12224 break;
12225 /* Store word instructions. */
12226 case 2:
12227 case 6:
12228 record_buf_mem[0] = 4;
12229 break;
12230
12231 default:
12232 gdb_assert_not_reached ("no decoding pattern found");
12233 break;
12234 }
12235
12236 record_buf_mem[1] = address;
12237 thumb2_insn_r->mem_rec_count = 1;
12238 record_buf[0] = reg_rn;
12239 thumb2_insn_r->reg_rec_count = 1;
12240
12241 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12242 record_buf);
12243 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
12244 record_buf_mem);
12245 return ARM_RECORD_SUCCESS;
12246}
12247
12248/* Handler for thumb2 load memory hints instructions. */
12249
12250static int
12251thumb2_record_ld_mem_hints (insn_decode_record *thumb2_insn_r)
12252{
12253 uint32_t record_buf[8];
12254 uint32_t reg_rt, reg_rn;
12255
12256 reg_rt = bits (thumb2_insn_r->arm_insn, 12, 15);
12257 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12258
12259 if (ARM_PC_REGNUM != reg_rt)
12260 {
12261 record_buf[0] = reg_rt;
12262 record_buf[1] = reg_rn;
12263 record_buf[2] = ARM_PS_REGNUM;
12264 thumb2_insn_r->reg_rec_count = 3;
12265
12266 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12267 record_buf);
12268 return ARM_RECORD_SUCCESS;
12269 }
12270
12271 return ARM_RECORD_FAILURE;
12272}
12273
12274/* Handler for thumb2 load word instructions. */
12275
12276static int
12277thumb2_record_ld_word (insn_decode_record *thumb2_insn_r)
12278{
c6ec2b30
OJ
12279 uint32_t record_buf[8];
12280
12281 record_buf[0] = bits (thumb2_insn_r->arm_insn, 12, 15);
12282 record_buf[1] = ARM_PS_REGNUM;
12283 thumb2_insn_r->reg_rec_count = 2;
12284
12285 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12286 record_buf);
12287 return ARM_RECORD_SUCCESS;
12288}
12289
12290/* Handler for thumb2 long multiply, long multiply accumulate, and
12291 divide instructions. */
12292
12293static int
12294thumb2_record_lmul_lmla_div (insn_decode_record *thumb2_insn_r)
12295{
12296 uint32_t opcode1 = 0, opcode2 = 0;
12297 uint32_t record_buf[8];
c6ec2b30
OJ
12298
12299 opcode1 = bits (thumb2_insn_r->arm_insn, 20, 22);
12300 opcode2 = bits (thumb2_insn_r->arm_insn, 4, 7);
12301
12302 if (0 == opcode1 || 2 == opcode1 || (opcode1 >= 4 && opcode1 <= 6))
12303 {
12304 /* Handle SMULL, UMULL, SMULAL. */
12305 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
12306 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
12307 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
12308 record_buf[2] = ARM_PS_REGNUM;
12309 thumb2_insn_r->reg_rec_count = 3;
12310 }
12311 else if (1 == opcode1 || 3 == opcode2)
12312 {
12313 /* Handle SDIV and UDIV. */
12314 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
12315 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
12316 record_buf[2] = ARM_PS_REGNUM;
12317 thumb2_insn_r->reg_rec_count = 3;
12318 }
12319 else
12320 return ARM_RECORD_FAILURE;
12321
12322 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12323 record_buf);
12324 return ARM_RECORD_SUCCESS;
12325}
12326
60cc5e93
OJ
12327/* Record handler for thumb32 coprocessor instructions. */
12328
12329static int
12330thumb2_record_coproc_insn (insn_decode_record *thumb2_insn_r)
12331{
12332 if (bit (thumb2_insn_r->arm_insn, 25))
12333 return arm_record_coproc_data_proc (thumb2_insn_r);
12334 else
12335 return arm_record_asimd_vfp_coproc (thumb2_insn_r);
12336}
12337
1e1b6563
OJ
12338/* Record handler for advance SIMD structure load/store instructions. */
12339
12340static int
12341thumb2_record_asimd_struct_ld_st (insn_decode_record *thumb2_insn_r)
12342{
12343 struct regcache *reg_cache = thumb2_insn_r->regcache;
12344 uint32_t l_bit, a_bit, b_bits;
12345 uint32_t record_buf[128], record_buf_mem[128];
bec2ab5a 12346 uint32_t reg_rn, reg_vd, address, f_elem;
1e1b6563
OJ
12347 uint32_t index_r = 0, index_e = 0, bf_regs = 0, index_m = 0, loop_t = 0;
12348 uint8_t f_ebytes;
12349
12350 l_bit = bit (thumb2_insn_r->arm_insn, 21);
12351 a_bit = bit (thumb2_insn_r->arm_insn, 23);
12352 b_bits = bits (thumb2_insn_r->arm_insn, 8, 11);
12353 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12354 reg_vd = bits (thumb2_insn_r->arm_insn, 12, 15);
12355 reg_vd = (bit (thumb2_insn_r->arm_insn, 22) << 4) | reg_vd;
12356 f_ebytes = (1 << bits (thumb2_insn_r->arm_insn, 6, 7));
1e1b6563
OJ
12357 f_elem = 8 / f_ebytes;
12358
12359 if (!l_bit)
12360 {
12361 ULONGEST u_regval = 0;
12362 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12363 address = u_regval;
12364
12365 if (!a_bit)
12366 {
12367 /* Handle VST1. */
12368 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
12369 {
12370 if (b_bits == 0x07)
12371 bf_regs = 1;
12372 else if (b_bits == 0x0a)
12373 bf_regs = 2;
12374 else if (b_bits == 0x06)
12375 bf_regs = 3;
12376 else if (b_bits == 0x02)
12377 bf_regs = 4;
12378 else
12379 bf_regs = 0;
12380
12381 for (index_r = 0; index_r < bf_regs; index_r++)
12382 {
12383 for (index_e = 0; index_e < f_elem; index_e++)
12384 {
12385 record_buf_mem[index_m++] = f_ebytes;
12386 record_buf_mem[index_m++] = address;
12387 address = address + f_ebytes;
12388 thumb2_insn_r->mem_rec_count += 1;
12389 }
12390 }
12391 }
12392 /* Handle VST2. */
12393 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
12394 {
12395 if (b_bits == 0x09 || b_bits == 0x08)
12396 bf_regs = 1;
12397 else if (b_bits == 0x03)
12398 bf_regs = 2;
12399 else
12400 bf_regs = 0;
12401
12402 for (index_r = 0; index_r < bf_regs; index_r++)
12403 for (index_e = 0; index_e < f_elem; index_e++)
12404 {
12405 for (loop_t = 0; loop_t < 2; loop_t++)
12406 {
12407 record_buf_mem[index_m++] = f_ebytes;
12408 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
12409 thumb2_insn_r->mem_rec_count += 1;
12410 }
12411 address = address + (2 * f_ebytes);
12412 }
12413 }
12414 /* Handle VST3. */
12415 else if ((b_bits & 0x0e) == 0x04)
12416 {
12417 for (index_e = 0; index_e < f_elem; index_e++)
12418 {
12419 for (loop_t = 0; loop_t < 3; loop_t++)
12420 {
12421 record_buf_mem[index_m++] = f_ebytes;
12422 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
12423 thumb2_insn_r->mem_rec_count += 1;
12424 }
12425 address = address + (3 * f_ebytes);
12426 }
12427 }
12428 /* Handle VST4. */
12429 else if (!(b_bits & 0x0e))
12430 {
12431 for (index_e = 0; index_e < f_elem; index_e++)
12432 {
12433 for (loop_t = 0; loop_t < 4; loop_t++)
12434 {
12435 record_buf_mem[index_m++] = f_ebytes;
12436 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
12437 thumb2_insn_r->mem_rec_count += 1;
12438 }
12439 address = address + (4 * f_ebytes);
12440 }
12441 }
12442 }
12443 else
12444 {
12445 uint8_t bft_size = bits (thumb2_insn_r->arm_insn, 10, 11);
12446
12447 if (bft_size == 0x00)
12448 f_ebytes = 1;
12449 else if (bft_size == 0x01)
12450 f_ebytes = 2;
12451 else if (bft_size == 0x02)
12452 f_ebytes = 4;
12453 else
12454 f_ebytes = 0;
12455
12456 /* Handle VST1. */
12457 if (!(b_bits & 0x0b) || b_bits == 0x08)
12458 thumb2_insn_r->mem_rec_count = 1;
12459 /* Handle VST2. */
12460 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09)
12461 thumb2_insn_r->mem_rec_count = 2;
12462 /* Handle VST3. */
12463 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a)
12464 thumb2_insn_r->mem_rec_count = 3;
12465 /* Handle VST4. */
12466 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b)
12467 thumb2_insn_r->mem_rec_count = 4;
12468
12469 for (index_m = 0; index_m < thumb2_insn_r->mem_rec_count; index_m++)
12470 {
12471 record_buf_mem[index_m] = f_ebytes;
12472 record_buf_mem[index_m] = address + (index_m * f_ebytes);
12473 }
12474 }
12475 }
12476 else
12477 {
12478 if (!a_bit)
12479 {
12480 /* Handle VLD1. */
12481 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
12482 thumb2_insn_r->reg_rec_count = 1;
12483 /* Handle VLD2. */
12484 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
12485 thumb2_insn_r->reg_rec_count = 2;
12486 /* Handle VLD3. */
12487 else if ((b_bits & 0x0e) == 0x04)
12488 thumb2_insn_r->reg_rec_count = 3;
12489 /* Handle VLD4. */
12490 else if (!(b_bits & 0x0e))
12491 thumb2_insn_r->reg_rec_count = 4;
12492 }
12493 else
12494 {
12495 /* Handle VLD1. */
12496 if (!(b_bits & 0x0b) || b_bits == 0x08 || b_bits == 0x0c)
12497 thumb2_insn_r->reg_rec_count = 1;
12498 /* Handle VLD2. */
12499 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09 || b_bits == 0x0d)
12500 thumb2_insn_r->reg_rec_count = 2;
12501 /* Handle VLD3. */
12502 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a || b_bits == 0x0e)
12503 thumb2_insn_r->reg_rec_count = 3;
12504 /* Handle VLD4. */
12505 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b || b_bits == 0x0f)
12506 thumb2_insn_r->reg_rec_count = 4;
12507
12508 for (index_r = 0; index_r < thumb2_insn_r->reg_rec_count; index_r++)
12509 record_buf[index_r] = reg_vd + ARM_D0_REGNUM + index_r;
12510 }
12511 }
12512
12513 if (bits (thumb2_insn_r->arm_insn, 0, 3) != 15)
12514 {
12515 record_buf[index_r] = reg_rn;
12516 thumb2_insn_r->reg_rec_count += 1;
12517 }
12518
12519 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12520 record_buf);
12521 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
12522 record_buf_mem);
12523 return 0;
12524}
12525
c6ec2b30
OJ
12526/* Decodes thumb2 instruction type and invokes its record handler. */
12527
12528static unsigned int
12529thumb2_record_decode_insn_handler (insn_decode_record *thumb2_insn_r)
12530{
12531 uint32_t op, op1, op2;
12532
12533 op = bit (thumb2_insn_r->arm_insn, 15);
12534 op1 = bits (thumb2_insn_r->arm_insn, 27, 28);
12535 op2 = bits (thumb2_insn_r->arm_insn, 20, 26);
12536
12537 if (op1 == 0x01)
12538 {
12539 if (!(op2 & 0x64 ))
12540 {
12541 /* Load/store multiple instruction. */
12542 return thumb2_record_ld_st_multiple (thumb2_insn_r);
12543 }
12544 else if (!((op2 & 0x64) ^ 0x04))
12545 {
12546 /* Load/store (dual/exclusive) and table branch instruction. */
12547 return thumb2_record_ld_st_dual_ex_tbb (thumb2_insn_r);
12548 }
12549 else if (!((op2 & 0x20) ^ 0x20))
12550 {
12551 /* Data-processing (shifted register). */
12552 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
12553 }
12554 else if (op2 & 0x40)
12555 {
12556 /* Co-processor instructions. */
60cc5e93 12557 return thumb2_record_coproc_insn (thumb2_insn_r);
c6ec2b30
OJ
12558 }
12559 }
12560 else if (op1 == 0x02)
12561 {
12562 if (op)
12563 {
12564 /* Branches and miscellaneous control instructions. */
12565 return thumb2_record_branch_misc_cntrl (thumb2_insn_r);
12566 }
12567 else if (op2 & 0x20)
12568 {
12569 /* Data-processing (plain binary immediate) instruction. */
12570 return thumb2_record_ps_dest_generic (thumb2_insn_r);
12571 }
12572 else
12573 {
12574 /* Data-processing (modified immediate). */
12575 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
12576 }
12577 }
12578 else if (op1 == 0x03)
12579 {
12580 if (!(op2 & 0x71 ))
12581 {
12582 /* Store single data item. */
12583 return thumb2_record_str_single_data (thumb2_insn_r);
12584 }
12585 else if (!((op2 & 0x71) ^ 0x10))
12586 {
12587 /* Advanced SIMD or structure load/store instructions. */
1e1b6563 12588 return thumb2_record_asimd_struct_ld_st (thumb2_insn_r);
c6ec2b30
OJ
12589 }
12590 else if (!((op2 & 0x67) ^ 0x01))
12591 {
12592 /* Load byte, memory hints instruction. */
12593 return thumb2_record_ld_mem_hints (thumb2_insn_r);
12594 }
12595 else if (!((op2 & 0x67) ^ 0x03))
12596 {
12597 /* Load halfword, memory hints instruction. */
12598 return thumb2_record_ld_mem_hints (thumb2_insn_r);
12599 }
12600 else if (!((op2 & 0x67) ^ 0x05))
12601 {
12602 /* Load word instruction. */
12603 return thumb2_record_ld_word (thumb2_insn_r);
12604 }
12605 else if (!((op2 & 0x70) ^ 0x20))
12606 {
12607 /* Data-processing (register) instruction. */
12608 return thumb2_record_ps_dest_generic (thumb2_insn_r);
12609 }
12610 else if (!((op2 & 0x78) ^ 0x30))
12611 {
12612 /* Multiply, multiply accumulate, abs diff instruction. */
12613 return thumb2_record_ps_dest_generic (thumb2_insn_r);
12614 }
12615 else if (!((op2 & 0x78) ^ 0x38))
12616 {
12617 /* Long multiply, long multiply accumulate, and divide. */
12618 return thumb2_record_lmul_lmla_div (thumb2_insn_r);
12619 }
12620 else if (op2 & 0x40)
12621 {
12622 /* Co-processor instructions. */
60cc5e93 12623 return thumb2_record_coproc_insn (thumb2_insn_r);
c6ec2b30
OJ
12624 }
12625 }
12626
12627 return -1;
12628}
72508ac0
PO
12629
12630/* Extracts arm/thumb/thumb2 insn depending on the size, and returns 0 on success
12631and positive val on fauilure. */
12632
12633static int
12634extract_arm_insn (insn_decode_record *insn_record, uint32_t insn_size)
12635{
12636 gdb_byte buf[insn_size];
12637
12638 memset (&buf[0], 0, insn_size);
12639
12640 if (target_read_memory (insn_record->this_addr, &buf[0], insn_size))
12641 return 1;
12642 insn_record->arm_insn = (uint32_t) extract_unsigned_integer (&buf[0],
12643 insn_size,
2959fed9 12644 gdbarch_byte_order_for_code (insn_record->gdbarch));
72508ac0
PO
12645 return 0;
12646}
12647
12648typedef int (*sti_arm_hdl_fp_t) (insn_decode_record*);
12649
12650/* Decode arm/thumb insn depending on condition cods and opcodes; and
12651 dispatch it. */
12652
12653static int
12654decode_insn (insn_decode_record *arm_record, record_type_t record_type,
12655 uint32_t insn_size)
12656{
12657
12658 /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm instruction. */
0fa9c223 12659 static const sti_arm_hdl_fp_t arm_handle_insn[8] =
72508ac0
PO
12660 {
12661 arm_record_data_proc_misc_ld_str, /* 000. */
12662 arm_record_data_proc_imm, /* 001. */
12663 arm_record_ld_st_imm_offset, /* 010. */
12664 arm_record_ld_st_reg_offset, /* 011. */
12665 arm_record_ld_st_multiple, /* 100. */
12666 arm_record_b_bl, /* 101. */
60cc5e93 12667 arm_record_asimd_vfp_coproc, /* 110. */
72508ac0
PO
12668 arm_record_coproc_data_proc /* 111. */
12669 };
12670
12671 /* (Starting from numerical 0); bits 13,14,15 decodes type of thumb instruction. */
0fa9c223 12672 static const sti_arm_hdl_fp_t thumb_handle_insn[8] =
72508ac0
PO
12673 { \
12674 thumb_record_shift_add_sub, /* 000. */
12675 thumb_record_add_sub_cmp_mov, /* 001. */
12676 thumb_record_ld_st_reg_offset, /* 010. */
12677 thumb_record_ld_st_imm_offset, /* 011. */
12678 thumb_record_ld_st_stack, /* 100. */
12679 thumb_record_misc, /* 101. */
12680 thumb_record_ldm_stm_swi, /* 110. */
12681 thumb_record_branch /* 111. */
12682 };
12683
12684 uint32_t ret = 0; /* return value: negative:failure 0:success. */
12685 uint32_t insn_id = 0;
12686
12687 if (extract_arm_insn (arm_record, insn_size))
12688 {
12689 if (record_debug)
12690 {
12691 printf_unfiltered (_("Process record: error reading memory at "
12692 "addr %s len = %d.\n"),
12693 paddress (arm_record->gdbarch, arm_record->this_addr), insn_size);
12694 }
12695 return -1;
12696 }
12697 else if (ARM_RECORD == record_type)
12698 {
12699 arm_record->cond = bits (arm_record->arm_insn, 28, 31);
12700 insn_id = bits (arm_record->arm_insn, 25, 27);
12701 ret = arm_record_extension_space (arm_record);
12702 /* If this insn has fallen into extension space
12703 then we need not decode it anymore. */
12704 if (ret != -1 && !INSN_RECORDED(arm_record))
12705 {
12706 ret = arm_handle_insn[insn_id] (arm_record);
12707 }
12708 }
12709 else if (THUMB_RECORD == record_type)
12710 {
12711 /* As thumb does not have condition codes, we set negative. */
12712 arm_record->cond = -1;
12713 insn_id = bits (arm_record->arm_insn, 13, 15);
12714 ret = thumb_handle_insn[insn_id] (arm_record);
12715 }
12716 else if (THUMB2_RECORD == record_type)
12717 {
c6ec2b30
OJ
12718 /* As thumb does not have condition codes, we set negative. */
12719 arm_record->cond = -1;
12720
12721 /* Swap first half of 32bit thumb instruction with second half. */
12722 arm_record->arm_insn
12723 = (arm_record->arm_insn >> 16) | (arm_record->arm_insn << 16);
12724
12725 insn_id = thumb2_record_decode_insn_handler (arm_record);
12726
12727 if (insn_id != ARM_RECORD_SUCCESS)
12728 {
12729 arm_record_unsupported_insn (arm_record);
12730 ret = -1;
12731 }
72508ac0
PO
12732 }
12733 else
12734 {
12735 /* Throw assertion. */
12736 gdb_assert_not_reached ("not a valid instruction, could not decode");
12737 }
12738
12739 return ret;
12740}
12741
12742
12743/* Cleans up local record registers and memory allocations. */
12744
12745static void
12746deallocate_reg_mem (insn_decode_record *record)
12747{
12748 xfree (record->arm_regs);
12749 xfree (record->arm_mems);
12750}
12751
12752
12753/* Parse the current instruction and record the values of the registers and
12754 memory that will be changed in current instruction to record_arch_list".
12755 Return -1 if something is wrong. */
12756
12757int
12758arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
12759 CORE_ADDR insn_addr)
12760{
12761
72508ac0
PO
12762 uint32_t no_of_rec = 0;
12763 uint32_t ret = 0; /* return value: -1:record failure ; 0:success */
12764 ULONGEST t_bit = 0, insn_id = 0;
12765
12766 ULONGEST u_regval = 0;
12767
12768 insn_decode_record arm_record;
12769
12770 memset (&arm_record, 0, sizeof (insn_decode_record));
12771 arm_record.regcache = regcache;
12772 arm_record.this_addr = insn_addr;
12773 arm_record.gdbarch = gdbarch;
12774
12775
12776 if (record_debug > 1)
12777 {
12778 fprintf_unfiltered (gdb_stdlog, "Process record: arm_process_record "
12779 "addr = %s\n",
12780 paddress (gdbarch, arm_record.this_addr));
12781 }
12782
12783 if (extract_arm_insn (&arm_record, 2))
12784 {
12785 if (record_debug)
12786 {
12787 printf_unfiltered (_("Process record: error reading memory at "
12788 "addr %s len = %d.\n"),
12789 paddress (arm_record.gdbarch,
12790 arm_record.this_addr), 2);
12791 }
12792 return -1;
12793 }
12794
12795 /* Check the insn, whether it is thumb or arm one. */
12796
12797 t_bit = arm_psr_thumb_bit (arm_record.gdbarch);
12798 regcache_raw_read_unsigned (arm_record.regcache, ARM_PS_REGNUM, &u_regval);
12799
12800
12801 if (!(u_regval & t_bit))
12802 {
12803 /* We are decoding arm insn. */
12804 ret = decode_insn (&arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES);
12805 }
12806 else
12807 {
12808 insn_id = bits (arm_record.arm_insn, 11, 15);
12809 /* is it thumb2 insn? */
12810 if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id))
12811 {
12812 ret = decode_insn (&arm_record, THUMB2_RECORD,
12813 THUMB2_INSN_SIZE_BYTES);
12814 }
12815 else
12816 {
12817 /* We are decoding thumb insn. */
12818 ret = decode_insn (&arm_record, THUMB_RECORD, THUMB_INSN_SIZE_BYTES);
12819 }
12820 }
12821
12822 if (0 == ret)
12823 {
12824 /* Record registers. */
25ea693b 12825 record_full_arch_list_add_reg (arm_record.regcache, ARM_PC_REGNUM);
72508ac0
PO
12826 if (arm_record.arm_regs)
12827 {
12828 for (no_of_rec = 0; no_of_rec < arm_record.reg_rec_count; no_of_rec++)
12829 {
25ea693b
MM
12830 if (record_full_arch_list_add_reg
12831 (arm_record.regcache , arm_record.arm_regs[no_of_rec]))
72508ac0
PO
12832 ret = -1;
12833 }
12834 }
12835 /* Record memories. */
12836 if (arm_record.arm_mems)
12837 {
12838 for (no_of_rec = 0; no_of_rec < arm_record.mem_rec_count; no_of_rec++)
12839 {
25ea693b 12840 if (record_full_arch_list_add_mem
72508ac0 12841 ((CORE_ADDR)arm_record.arm_mems[no_of_rec].addr,
25ea693b 12842 arm_record.arm_mems[no_of_rec].len))
72508ac0
PO
12843 ret = -1;
12844 }
12845 }
12846
25ea693b 12847 if (record_full_arch_list_add_end ())
72508ac0
PO
12848 ret = -1;
12849 }
12850
12851
12852 deallocate_reg_mem (&arm_record);
12853
12854 return ret;
12855}
12856
This page took 2.212633 seconds and 4 git commands to generate.