* config/tc-arm.c (check_ldr_r15_aligned): New.
[deliverable/binutils-gdb.git] / gdb / arm-tdep.c
CommitLineData
ed9a39eb 1/* Common target dependent code for GDB on ARM systems.
0fd88904 2
6aba47ca 3 Copyright (C) 1988, 1989, 1991, 1992, 1993, 1995, 1996, 1998, 1999, 2000,
7b6bb8da 4 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
9b254dd1 5 Free Software Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 21
0963b4bd 22#include <ctype.h> /* XXX for isupper (). */
34e8f22d 23
c906108c
SS
24#include "defs.h"
25#include "frame.h"
26#include "inferior.h"
27#include "gdbcmd.h"
28#include "gdbcore.h"
c906108c 29#include "gdb_string.h"
0963b4bd 30#include "dis-asm.h" /* For register styles. */
4e052eda 31#include "regcache.h"
54483882 32#include "reggroups.h"
d16aafd8 33#include "doublest.h"
fd0407d6 34#include "value.h"
34e8f22d 35#include "arch-utils.h"
4be87837 36#include "osabi.h"
eb5492fa
DJ
37#include "frame-unwind.h"
38#include "frame-base.h"
39#include "trad-frame.h"
842e1f1e
DJ
40#include "objfiles.h"
41#include "dwarf2-frame.h"
e4c16157 42#include "gdbtypes.h"
29d73ae4 43#include "prologue-value.h"
123dc839
DJ
44#include "target-descriptions.h"
45#include "user-regs.h"
0e9e9abd 46#include "observer.h"
34e8f22d
RE
47
48#include "arm-tdep.h"
26216b98 49#include "gdb/sim-arm.h"
34e8f22d 50
082fc60d
RE
51#include "elf-bfd.h"
52#include "coff/internal.h"
97e03143 53#include "elf/arm.h"
c906108c 54
26216b98 55#include "gdb_assert.h"
60c5725c 56#include "vec.h"
26216b98 57
9779414d 58#include "features/arm-with-m.c"
ef7e8358
UW
59#include "features/arm-with-iwmmxt.c"
60#include "features/arm-with-vfpv2.c"
61#include "features/arm-with-vfpv3.c"
62#include "features/arm-with-neon.c"
9779414d 63
6529d2dd
AC
64static int arm_debug;
65
082fc60d
RE
66/* Macros for setting and testing a bit in a minimal symbol that marks
67 it as Thumb function. The MSB of the minimal symbol's "info" field
f594e5e9 68 is used for this purpose.
082fc60d
RE
69
70 MSYMBOL_SET_SPECIAL Actually sets the "special" bit.
f594e5e9 71 MSYMBOL_IS_SPECIAL Tests the "special" bit in a minimal symbol. */
082fc60d 72
0963b4bd 73#define MSYMBOL_SET_SPECIAL(msym) \
b887350f 74 MSYMBOL_TARGET_FLAG_1 (msym) = 1
082fc60d
RE
75
76#define MSYMBOL_IS_SPECIAL(msym) \
b887350f 77 MSYMBOL_TARGET_FLAG_1 (msym)
082fc60d 78
60c5725c
DJ
79/* Per-objfile data used for mapping symbols. */
80static const struct objfile_data *arm_objfile_data_key;
81
82struct arm_mapping_symbol
83{
84 bfd_vma value;
85 char type;
86};
87typedef struct arm_mapping_symbol arm_mapping_symbol_s;
88DEF_VEC_O(arm_mapping_symbol_s);
89
90struct arm_per_objfile
91{
92 VEC(arm_mapping_symbol_s) **section_maps;
93};
94
afd7eef0
RE
95/* The list of available "set arm ..." and "show arm ..." commands. */
96static struct cmd_list_element *setarmcmdlist = NULL;
97static struct cmd_list_element *showarmcmdlist = NULL;
98
fd50bc42
RE
99/* The type of floating-point to use. Keep this in sync with enum
100 arm_float_model, and the help string in _initialize_arm_tdep. */
101static const char *fp_model_strings[] =
102{
103 "auto",
104 "softfpa",
105 "fpa",
106 "softvfp",
28e97307
DJ
107 "vfp",
108 NULL
fd50bc42
RE
109};
110
111/* A variable that can be configured by the user. */
112static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
113static const char *current_fp_model = "auto";
114
28e97307
DJ
115/* The ABI to use. Keep this in sync with arm_abi_kind. */
116static const char *arm_abi_strings[] =
117{
118 "auto",
119 "APCS",
120 "AAPCS",
121 NULL
122};
123
124/* A variable that can be configured by the user. */
125static enum arm_abi_kind arm_abi_global = ARM_ABI_AUTO;
126static const char *arm_abi_string = "auto";
127
0428b8f5
DJ
128/* The execution mode to assume. */
129static const char *arm_mode_strings[] =
130 {
131 "auto",
132 "arm",
68770265
MGD
133 "thumb",
134 NULL
0428b8f5
DJ
135 };
136
137static const char *arm_fallback_mode_string = "auto";
138static const char *arm_force_mode_string = "auto";
139
18819fa6
UW
140/* Internal override of the execution mode. -1 means no override,
141 0 means override to ARM mode, 1 means override to Thumb mode.
142 The effect is the same as if arm_force_mode has been set by the
143 user (except the internal override has precedence over a user's
144 arm_force_mode override). */
145static int arm_override_mode = -1;
146
94c30b78 147/* Number of different reg name sets (options). */
afd7eef0 148static int num_disassembly_options;
bc90b915 149
f32bf4a4
YQ
150/* The standard register names, and all the valid aliases for them. Note
151 that `fp', `sp' and `pc' are not added in this alias list, because they
152 have been added as builtin user registers in
153 std-regs.c:_initialize_frame_reg. */
123dc839
DJ
154static const struct
155{
156 const char *name;
157 int regnum;
158} arm_register_aliases[] = {
159 /* Basic register numbers. */
160 { "r0", 0 },
161 { "r1", 1 },
162 { "r2", 2 },
163 { "r3", 3 },
164 { "r4", 4 },
165 { "r5", 5 },
166 { "r6", 6 },
167 { "r7", 7 },
168 { "r8", 8 },
169 { "r9", 9 },
170 { "r10", 10 },
171 { "r11", 11 },
172 { "r12", 12 },
173 { "r13", 13 },
174 { "r14", 14 },
175 { "r15", 15 },
176 /* Synonyms (argument and variable registers). */
177 { "a1", 0 },
178 { "a2", 1 },
179 { "a3", 2 },
180 { "a4", 3 },
181 { "v1", 4 },
182 { "v2", 5 },
183 { "v3", 6 },
184 { "v4", 7 },
185 { "v5", 8 },
186 { "v6", 9 },
187 { "v7", 10 },
188 { "v8", 11 },
189 /* Other platform-specific names for r9. */
190 { "sb", 9 },
191 { "tr", 9 },
192 /* Special names. */
193 { "ip", 12 },
123dc839 194 { "lr", 14 },
123dc839
DJ
195 /* Names used by GCC (not listed in the ARM EABI). */
196 { "sl", 10 },
123dc839
DJ
197 /* A special name from the older ATPCS. */
198 { "wr", 7 },
199};
bc90b915 200
123dc839 201static const char *const arm_register_names[] =
da59e081
JM
202{"r0", "r1", "r2", "r3", /* 0 1 2 3 */
203 "r4", "r5", "r6", "r7", /* 4 5 6 7 */
204 "r8", "r9", "r10", "r11", /* 8 9 10 11 */
205 "r12", "sp", "lr", "pc", /* 12 13 14 15 */
206 "f0", "f1", "f2", "f3", /* 16 17 18 19 */
207 "f4", "f5", "f6", "f7", /* 20 21 22 23 */
94c30b78 208 "fps", "cpsr" }; /* 24 25 */
ed9a39eb 209
afd7eef0
RE
210/* Valid register name styles. */
211static const char **valid_disassembly_styles;
ed9a39eb 212
afd7eef0
RE
213/* Disassembly style to use. Default to "std" register names. */
214static const char *disassembly_style;
96baa820 215
ed9a39eb 216/* This is used to keep the bfd arch_info in sync with the disassembly
afd7eef0
RE
217 style. */
218static void set_disassembly_style_sfunc(char *, int,
ed9a39eb 219 struct cmd_list_element *);
afd7eef0 220static void set_disassembly_style (void);
ed9a39eb 221
b508a996 222static void convert_from_extended (const struct floatformat *, const void *,
be8626e0 223 void *, int);
b508a996 224static void convert_to_extended (const struct floatformat *, void *,
be8626e0 225 const void *, int);
ed9a39eb 226
05d1431c
PA
227static enum register_status arm_neon_quad_read (struct gdbarch *gdbarch,
228 struct regcache *regcache,
229 int regnum, gdb_byte *buf);
58d6951d
DJ
230static void arm_neon_quad_write (struct gdbarch *gdbarch,
231 struct regcache *regcache,
232 int regnum, const gdb_byte *buf);
233
9b8d791a 234struct arm_prologue_cache
c3b4394c 235{
eb5492fa
DJ
236 /* The stack pointer at the time this frame was created; i.e. the
237 caller's stack pointer when this function was called. It is used
238 to identify this frame. */
239 CORE_ADDR prev_sp;
240
4be43953
DJ
241 /* The frame base for this frame is just prev_sp - frame size.
242 FRAMESIZE is the distance from the frame pointer to the
243 initial stack pointer. */
eb5492fa 244
c3b4394c 245 int framesize;
eb5492fa
DJ
246
247 /* The register used to hold the frame pointer for this frame. */
c3b4394c 248 int framereg;
eb5492fa
DJ
249
250 /* Saved register offsets. */
251 struct trad_frame_saved_reg *saved_regs;
c3b4394c 252};
ed9a39eb 253
0d39a070
DJ
254static CORE_ADDR arm_analyze_prologue (struct gdbarch *gdbarch,
255 CORE_ADDR prologue_start,
256 CORE_ADDR prologue_end,
257 struct arm_prologue_cache *cache);
258
cca44b1b
JB
259/* Architecture version for displaced stepping. This effects the behaviour of
260 certain instructions, and really should not be hard-wired. */
261
262#define DISPLACED_STEPPING_ARCH_VERSION 5
263
bc90b915
FN
264/* Addresses for calling Thumb functions have the bit 0 set.
265 Here are some macros to test, set, or clear bit 0 of addresses. */
266#define IS_THUMB_ADDR(addr) ((addr) & 1)
267#define MAKE_THUMB_ADDR(addr) ((addr) | 1)
268#define UNMAKE_THUMB_ADDR(addr) ((addr) & ~1)
269
94c30b78 270/* Set to true if the 32-bit mode is in use. */
c906108c
SS
271
272int arm_apcs_32 = 1;
273
9779414d
DJ
274/* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode. */
275
478fd957 276int
9779414d
DJ
277arm_psr_thumb_bit (struct gdbarch *gdbarch)
278{
279 if (gdbarch_tdep (gdbarch)->is_m)
280 return XPSR_T;
281 else
282 return CPSR_T;
283}
284
b39cc962
DJ
285/* Determine if FRAME is executing in Thumb mode. */
286
25b41d01 287int
b39cc962
DJ
288arm_frame_is_thumb (struct frame_info *frame)
289{
290 CORE_ADDR cpsr;
9779414d 291 ULONGEST t_bit = arm_psr_thumb_bit (get_frame_arch (frame));
b39cc962
DJ
292
293 /* Every ARM frame unwinder can unwind the T bit of the CPSR, either
294 directly (from a signal frame or dummy frame) or by interpreting
295 the saved LR (from a prologue or DWARF frame). So consult it and
296 trust the unwinders. */
297 cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
298
9779414d 299 return (cpsr & t_bit) != 0;
b39cc962
DJ
300}
301
60c5725c
DJ
302/* Callback for VEC_lower_bound. */
303
304static inline int
305arm_compare_mapping_symbols (const struct arm_mapping_symbol *lhs,
306 const struct arm_mapping_symbol *rhs)
307{
308 return lhs->value < rhs->value;
309}
310
f9d67f43
DJ
311/* Search for the mapping symbol covering MEMADDR. If one is found,
312 return its type. Otherwise, return 0. If START is non-NULL,
313 set *START to the location of the mapping symbol. */
c906108c 314
f9d67f43
DJ
315static char
316arm_find_mapping_symbol (CORE_ADDR memaddr, CORE_ADDR *start)
c906108c 317{
60c5725c 318 struct obj_section *sec;
0428b8f5 319
60c5725c
DJ
320 /* If there are mapping symbols, consult them. */
321 sec = find_pc_section (memaddr);
322 if (sec != NULL)
323 {
324 struct arm_per_objfile *data;
325 VEC(arm_mapping_symbol_s) *map;
aded6f54
PA
326 struct arm_mapping_symbol map_key = { memaddr - obj_section_addr (sec),
327 0 };
60c5725c
DJ
328 unsigned int idx;
329
330 data = objfile_data (sec->objfile, arm_objfile_data_key);
331 if (data != NULL)
332 {
333 map = data->section_maps[sec->the_bfd_section->index];
334 if (!VEC_empty (arm_mapping_symbol_s, map))
335 {
336 struct arm_mapping_symbol *map_sym;
337
338 idx = VEC_lower_bound (arm_mapping_symbol_s, map, &map_key,
339 arm_compare_mapping_symbols);
340
341 /* VEC_lower_bound finds the earliest ordered insertion
342 point. If the following symbol starts at this exact
343 address, we use that; otherwise, the preceding
344 mapping symbol covers this address. */
345 if (idx < VEC_length (arm_mapping_symbol_s, map))
346 {
347 map_sym = VEC_index (arm_mapping_symbol_s, map, idx);
348 if (map_sym->value == map_key.value)
f9d67f43
DJ
349 {
350 if (start)
351 *start = map_sym->value + obj_section_addr (sec);
352 return map_sym->type;
353 }
60c5725c
DJ
354 }
355
356 if (idx > 0)
357 {
358 map_sym = VEC_index (arm_mapping_symbol_s, map, idx - 1);
f9d67f43
DJ
359 if (start)
360 *start = map_sym->value + obj_section_addr (sec);
361 return map_sym->type;
60c5725c
DJ
362 }
363 }
364 }
365 }
366
f9d67f43
DJ
367 return 0;
368}
369
370/* Determine if the program counter specified in MEMADDR is in a Thumb
371 function. This function should be called for addresses unrelated to
372 any executing frame; otherwise, prefer arm_frame_is_thumb. */
373
e3039479 374int
9779414d 375arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
f9d67f43
DJ
376{
377 struct obj_section *sec;
378 struct minimal_symbol *sym;
379 char type;
a42244db
YQ
380 struct displaced_step_closure* dsc
381 = get_displaced_step_closure_by_addr(memaddr);
382
383 /* If checking the mode of displaced instruction in copy area, the mode
384 should be determined by instruction on the original address. */
385 if (dsc)
386 {
387 if (debug_displaced)
388 fprintf_unfiltered (gdb_stdlog,
389 "displaced: check mode of %.8lx instead of %.8lx\n",
390 (unsigned long) dsc->insn_addr,
391 (unsigned long) memaddr);
392 memaddr = dsc->insn_addr;
393 }
f9d67f43
DJ
394
395 /* If bit 0 of the address is set, assume this is a Thumb address. */
396 if (IS_THUMB_ADDR (memaddr))
397 return 1;
398
18819fa6
UW
399 /* Respect internal mode override if active. */
400 if (arm_override_mode != -1)
401 return arm_override_mode;
402
f9d67f43
DJ
403 /* If the user wants to override the symbol table, let him. */
404 if (strcmp (arm_force_mode_string, "arm") == 0)
405 return 0;
406 if (strcmp (arm_force_mode_string, "thumb") == 0)
407 return 1;
408
9779414d
DJ
409 /* ARM v6-M and v7-M are always in Thumb mode. */
410 if (gdbarch_tdep (gdbarch)->is_m)
411 return 1;
412
f9d67f43
DJ
413 /* If there are mapping symbols, consult them. */
414 type = arm_find_mapping_symbol (memaddr, NULL);
415 if (type)
416 return type == 't';
417
ed9a39eb 418 /* Thumb functions have a "special" bit set in minimal symbols. */
c906108c
SS
419 sym = lookup_minimal_symbol_by_pc (memaddr);
420 if (sym)
0428b8f5
DJ
421 return (MSYMBOL_IS_SPECIAL (sym));
422
423 /* If the user wants to override the fallback mode, let them. */
424 if (strcmp (arm_fallback_mode_string, "arm") == 0)
425 return 0;
426 if (strcmp (arm_fallback_mode_string, "thumb") == 0)
427 return 1;
428
429 /* If we couldn't find any symbol, but we're talking to a running
430 target, then trust the current value of $cpsr. This lets
431 "display/i $pc" always show the correct mode (though if there is
432 a symbol table we will not reach here, so it still may not be
18819fa6 433 displayed in the mode it will be executed). */
0428b8f5 434 if (target_has_registers)
18819fa6 435 return arm_frame_is_thumb (get_current_frame ());
0428b8f5
DJ
436
437 /* Otherwise we're out of luck; we assume ARM. */
438 return 0;
c906108c
SS
439}
440
181c1381 441/* Remove useless bits from addresses in a running program. */
34e8f22d 442static CORE_ADDR
24568a2c 443arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
c906108c 444{
a3a2ee65 445 if (arm_apcs_32)
dd6be234 446 return UNMAKE_THUMB_ADDR (val);
c906108c 447 else
a3a2ee65 448 return (val & 0x03fffffc);
c906108c
SS
449}
450
181c1381
RE
451/* When reading symbols, we need to zap the low bit of the address,
452 which may be set to 1 for Thumb functions. */
34e8f22d 453static CORE_ADDR
24568a2c 454arm_smash_text_address (struct gdbarch *gdbarch, CORE_ADDR val)
181c1381
RE
455{
456 return val & ~1;
457}
458
0d39a070 459/* Return 1 if PC is the start of a compiler helper function which
e0634ccf
UW
460 can be safely ignored during prologue skipping. IS_THUMB is true
461 if the function is known to be a Thumb function due to the way it
462 is being called. */
0d39a070 463static int
e0634ccf 464skip_prologue_function (struct gdbarch *gdbarch, CORE_ADDR pc, int is_thumb)
0d39a070 465{
e0634ccf 466 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
0d39a070 467 struct minimal_symbol *msym;
0d39a070
DJ
468
469 msym = lookup_minimal_symbol_by_pc (pc);
e0634ccf
UW
470 if (msym != NULL
471 && SYMBOL_VALUE_ADDRESS (msym) == pc
472 && SYMBOL_LINKAGE_NAME (msym) != NULL)
473 {
474 const char *name = SYMBOL_LINKAGE_NAME (msym);
0d39a070 475
e0634ccf
UW
476 /* The GNU linker's Thumb call stub to foo is named
477 __foo_from_thumb. */
478 if (strstr (name, "_from_thumb") != NULL)
479 name += 2;
0d39a070 480
e0634ccf
UW
481 /* On soft-float targets, __truncdfsf2 is called to convert promoted
482 arguments to their argument types in non-prototyped
483 functions. */
484 if (strncmp (name, "__truncdfsf2", strlen ("__truncdfsf2")) == 0)
485 return 1;
486 if (strncmp (name, "__aeabi_d2f", strlen ("__aeabi_d2f")) == 0)
487 return 1;
0d39a070 488
e0634ccf
UW
489 /* Internal functions related to thread-local storage. */
490 if (strncmp (name, "__tls_get_addr", strlen ("__tls_get_addr")) == 0)
491 return 1;
492 if (strncmp (name, "__aeabi_read_tp", strlen ("__aeabi_read_tp")) == 0)
493 return 1;
494 }
495 else
496 {
497 /* If we run against a stripped glibc, we may be unable to identify
498 special functions by name. Check for one important case,
499 __aeabi_read_tp, by comparing the *code* against the default
500 implementation (this is hand-written ARM assembler in glibc). */
501
502 if (!is_thumb
503 && read_memory_unsigned_integer (pc, 4, byte_order_for_code)
504 == 0xe3e00a0f /* mov r0, #0xffff0fff */
505 && read_memory_unsigned_integer (pc + 4, 4, byte_order_for_code)
506 == 0xe240f01f) /* sub pc, r0, #31 */
507 return 1;
508 }
ec3d575a 509
0d39a070
DJ
510 return 0;
511}
512
513/* Support routines for instruction parsing. */
514#define submask(x) ((1L << ((x) + 1)) - 1)
515#define bit(obj,st) (((obj) >> (st)) & 1)
516#define bits(obj,st,fn) (((obj) >> (st)) & submask ((fn) - (st)))
517#define sbits(obj,st,fn) \
518 ((long) (bits(obj,st,fn) | ((long) bit(obj,fn) * ~ submask (fn - st))))
519#define BranchDest(addr,instr) \
520 ((CORE_ADDR) (((long) (addr)) + 8 + (sbits (instr, 0, 23) << 2)))
521
621c6d5b
YQ
522/* Extract the immediate from instruction movw/movt of encoding T. INSN1 is
523 the first 16-bit of instruction, and INSN2 is the second 16-bit of
524 instruction. */
525#define EXTRACT_MOVW_MOVT_IMM_T(insn1, insn2) \
526 ((bits ((insn1), 0, 3) << 12) \
527 | (bits ((insn1), 10, 10) << 11) \
528 | (bits ((insn2), 12, 14) << 8) \
529 | bits ((insn2), 0, 7))
530
531/* Extract the immediate from instruction movw/movt of encoding A. INSN is
532 the 32-bit instruction. */
533#define EXTRACT_MOVW_MOVT_IMM_A(insn) \
534 ((bits ((insn), 16, 19) << 12) \
535 | bits ((insn), 0, 11))
536
ec3d575a
UW
537/* Decode immediate value; implements ThumbExpandImmediate pseudo-op. */
538
539static unsigned int
540thumb_expand_immediate (unsigned int imm)
541{
542 unsigned int count = imm >> 7;
543
544 if (count < 8)
545 switch (count / 2)
546 {
547 case 0:
548 return imm & 0xff;
549 case 1:
550 return (imm & 0xff) | ((imm & 0xff) << 16);
551 case 2:
552 return ((imm & 0xff) << 8) | ((imm & 0xff) << 24);
553 case 3:
554 return (imm & 0xff) | ((imm & 0xff) << 8)
555 | ((imm & 0xff) << 16) | ((imm & 0xff) << 24);
556 }
557
558 return (0x80 | (imm & 0x7f)) << (32 - count);
559}
560
561/* Return 1 if the 16-bit Thumb instruction INST might change
562 control flow, 0 otherwise. */
563
564static int
565thumb_instruction_changes_pc (unsigned short inst)
566{
567 if ((inst & 0xff00) == 0xbd00) /* pop {rlist, pc} */
568 return 1;
569
570 if ((inst & 0xf000) == 0xd000) /* conditional branch */
571 return 1;
572
573 if ((inst & 0xf800) == 0xe000) /* unconditional branch */
574 return 1;
575
576 if ((inst & 0xff00) == 0x4700) /* bx REG, blx REG */
577 return 1;
578
ad8b5167
UW
579 if ((inst & 0xff87) == 0x4687) /* mov pc, REG */
580 return 1;
581
ec3d575a
UW
582 if ((inst & 0xf500) == 0xb100) /* CBNZ or CBZ. */
583 return 1;
584
585 return 0;
586}
587
588/* Return 1 if the 32-bit Thumb instruction in INST1 and INST2
589 might change control flow, 0 otherwise. */
590
591static int
592thumb2_instruction_changes_pc (unsigned short inst1, unsigned short inst2)
593{
594 if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
595 {
596 /* Branches and miscellaneous control instructions. */
597
598 if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
599 {
600 /* B, BL, BLX. */
601 return 1;
602 }
603 else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
604 {
605 /* SUBS PC, LR, #imm8. */
606 return 1;
607 }
608 else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
609 {
610 /* Conditional branch. */
611 return 1;
612 }
613
614 return 0;
615 }
616
617 if ((inst1 & 0xfe50) == 0xe810)
618 {
619 /* Load multiple or RFE. */
620
621 if (bit (inst1, 7) && !bit (inst1, 8))
622 {
623 /* LDMIA or POP */
624 if (bit (inst2, 15))
625 return 1;
626 }
627 else if (!bit (inst1, 7) && bit (inst1, 8))
628 {
629 /* LDMDB */
630 if (bit (inst2, 15))
631 return 1;
632 }
633 else if (bit (inst1, 7) && bit (inst1, 8))
634 {
635 /* RFEIA */
636 return 1;
637 }
638 else if (!bit (inst1, 7) && !bit (inst1, 8))
639 {
640 /* RFEDB */
641 return 1;
642 }
643
644 return 0;
645 }
646
647 if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
648 {
649 /* MOV PC or MOVS PC. */
650 return 1;
651 }
652
653 if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
654 {
655 /* LDR PC. */
656 if (bits (inst1, 0, 3) == 15)
657 return 1;
658 if (bit (inst1, 7))
659 return 1;
660 if (bit (inst2, 11))
661 return 1;
662 if ((inst2 & 0x0fc0) == 0x0000)
663 return 1;
664
665 return 0;
666 }
667
668 if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
669 {
670 /* TBB. */
671 return 1;
672 }
673
674 if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
675 {
676 /* TBH. */
677 return 1;
678 }
679
680 return 0;
681}
682
29d73ae4
DJ
683/* Analyze a Thumb prologue, looking for a recognizable stack frame
684 and frame pointer. Scan until we encounter a store that could
0d39a070
DJ
685 clobber the stack frame unexpectedly, or an unknown instruction.
686 Return the last address which is definitely safe to skip for an
687 initial breakpoint. */
c906108c
SS
688
689static CORE_ADDR
29d73ae4
DJ
690thumb_analyze_prologue (struct gdbarch *gdbarch,
691 CORE_ADDR start, CORE_ADDR limit,
692 struct arm_prologue_cache *cache)
c906108c 693{
0d39a070 694 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
e17a4113 695 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
29d73ae4
DJ
696 int i;
697 pv_t regs[16];
698 struct pv_area *stack;
699 struct cleanup *back_to;
700 CORE_ADDR offset;
ec3d575a 701 CORE_ADDR unrecognized_pc = 0;
da3c6d4a 702
29d73ae4
DJ
703 for (i = 0; i < 16; i++)
704 regs[i] = pv_register (i, 0);
55f960e1 705 stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
29d73ae4
DJ
706 back_to = make_cleanup_free_pv_area (stack);
707
29d73ae4 708 while (start < limit)
c906108c 709 {
29d73ae4
DJ
710 unsigned short insn;
711
e17a4113 712 insn = read_memory_unsigned_integer (start, 2, byte_order_for_code);
9d4fde75 713
94c30b78 714 if ((insn & 0xfe00) == 0xb400) /* push { rlist } */
da59e081 715 {
29d73ae4
DJ
716 int regno;
717 int mask;
4be43953
DJ
718
719 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
720 break;
29d73ae4
DJ
721
722 /* Bits 0-7 contain a mask for registers R0-R7. Bit 8 says
723 whether to save LR (R14). */
724 mask = (insn & 0xff) | ((insn & 0x100) << 6);
725
726 /* Calculate offsets of saved R0-R7 and LR. */
727 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
728 if (mask & (1 << regno))
729 {
29d73ae4
DJ
730 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
731 -4);
732 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
733 }
da59e081 734 }
da3c6d4a
MS
735 else if ((insn & 0xff00) == 0xb000) /* add sp, #simm OR
736 sub sp, #simm */
da59e081 737 {
29d73ae4
DJ
738 offset = (insn & 0x7f) << 2; /* get scaled offset */
739 if (insn & 0x80) /* Check for SUB. */
740 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
741 -offset);
da59e081 742 else
29d73ae4
DJ
743 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
744 offset);
da59e081 745 }
0d39a070
DJ
746 else if ((insn & 0xf800) == 0xa800) /* add Rd, sp, #imm */
747 regs[bits (insn, 8, 10)] = pv_add_constant (regs[ARM_SP_REGNUM],
748 (insn & 0xff) << 2);
749 else if ((insn & 0xfe00) == 0x1c00 /* add Rd, Rn, #imm */
750 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
751 regs[bits (insn, 0, 2)] = pv_add_constant (regs[bits (insn, 3, 5)],
752 bits (insn, 6, 8));
753 else if ((insn & 0xf800) == 0x3000 /* add Rd, #imm */
754 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
755 regs[bits (insn, 8, 10)] = pv_add_constant (regs[bits (insn, 8, 10)],
756 bits (insn, 0, 7));
757 else if ((insn & 0xfe00) == 0x1800 /* add Rd, Rn, Rm */
758 && pv_is_register (regs[bits (insn, 6, 8)], ARM_SP_REGNUM)
759 && pv_is_constant (regs[bits (insn, 3, 5)]))
760 regs[bits (insn, 0, 2)] = pv_add (regs[bits (insn, 3, 5)],
761 regs[bits (insn, 6, 8)]);
762 else if ((insn & 0xff00) == 0x4400 /* add Rd, Rm */
763 && pv_is_constant (regs[bits (insn, 3, 6)]))
764 {
765 int rd = (bit (insn, 7) << 3) + bits (insn, 0, 2);
766 int rm = bits (insn, 3, 6);
767 regs[rd] = pv_add (regs[rd], regs[rm]);
768 }
29d73ae4 769 else if ((insn & 0xff00) == 0x4600) /* mov hi, lo or mov lo, hi */
da59e081 770 {
29d73ae4
DJ
771 int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
772 int src_reg = (insn & 0x78) >> 3;
773 regs[dst_reg] = regs[src_reg];
da59e081 774 }
29d73ae4 775 else if ((insn & 0xf800) == 0x9000) /* str rd, [sp, #off] */
da59e081 776 {
29d73ae4
DJ
777 /* Handle stores to the stack. Normally pushes are used,
778 but with GCC -mtpcs-frame, there may be other stores
779 in the prologue to create the frame. */
780 int regno = (insn >> 8) & 0x7;
781 pv_t addr;
782
783 offset = (insn & 0xff) << 2;
784 addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
785
786 if (pv_area_store_would_trash (stack, addr))
787 break;
788
789 pv_area_store (stack, addr, 4, regs[regno]);
da59e081 790 }
0d39a070
DJ
791 else if ((insn & 0xf800) == 0x6000) /* str rd, [rn, #off] */
792 {
793 int rd = bits (insn, 0, 2);
794 int rn = bits (insn, 3, 5);
795 pv_t addr;
796
797 offset = bits (insn, 6, 10) << 2;
798 addr = pv_add_constant (regs[rn], offset);
799
800 if (pv_area_store_would_trash (stack, addr))
801 break;
802
803 pv_area_store (stack, addr, 4, regs[rd]);
804 }
805 else if (((insn & 0xf800) == 0x7000 /* strb Rd, [Rn, #off] */
806 || (insn & 0xf800) == 0x8000) /* strh Rd, [Rn, #off] */
807 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
808 /* Ignore stores of argument registers to the stack. */
809 ;
810 else if ((insn & 0xf800) == 0xc800 /* ldmia Rn!, { registers } */
811 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
812 /* Ignore block loads from the stack, potentially copying
813 parameters from memory. */
814 ;
815 else if ((insn & 0xf800) == 0x9800 /* ldr Rd, [Rn, #immed] */
816 || ((insn & 0xf800) == 0x6800 /* ldr Rd, [sp, #immed] */
817 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM)))
818 /* Similarly ignore single loads from the stack. */
819 ;
820 else if ((insn & 0xffc0) == 0x0000 /* lsls Rd, Rm, #0 */
821 || (insn & 0xffc0) == 0x1c00) /* add Rd, Rn, #0 */
822 /* Skip register copies, i.e. saves to another register
823 instead of the stack. */
824 ;
825 else if ((insn & 0xf800) == 0x2000) /* movs Rd, #imm */
826 /* Recognize constant loads; even with small stacks these are necessary
827 on Thumb. */
828 regs[bits (insn, 8, 10)] = pv_constant (bits (insn, 0, 7));
829 else if ((insn & 0xf800) == 0x4800) /* ldr Rd, [pc, #imm] */
830 {
831 /* Constant pool loads, for the same reason. */
832 unsigned int constant;
833 CORE_ADDR loc;
834
835 loc = start + 4 + bits (insn, 0, 7) * 4;
836 constant = read_memory_unsigned_integer (loc, 4, byte_order);
837 regs[bits (insn, 8, 10)] = pv_constant (constant);
838 }
ec3d575a 839 else if ((insn & 0xe000) == 0xe000)
0d39a070 840 {
0d39a070
DJ
841 unsigned short inst2;
842
843 inst2 = read_memory_unsigned_integer (start + 2, 2,
844 byte_order_for_code);
845
846 if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
847 {
848 /* BL, BLX. Allow some special function calls when
849 skipping the prologue; GCC generates these before
850 storing arguments to the stack. */
851 CORE_ADDR nextpc;
852 int j1, j2, imm1, imm2;
853
854 imm1 = sbits (insn, 0, 10);
855 imm2 = bits (inst2, 0, 10);
856 j1 = bit (inst2, 13);
857 j2 = bit (inst2, 11);
858
859 offset = ((imm1 << 12) + (imm2 << 1));
860 offset ^= ((!j2) << 22) | ((!j1) << 23);
861
862 nextpc = start + 4 + offset;
863 /* For BLX make sure to clear the low bits. */
864 if (bit (inst2, 12) == 0)
865 nextpc = nextpc & 0xfffffffc;
866
e0634ccf
UW
867 if (!skip_prologue_function (gdbarch, nextpc,
868 bit (inst2, 12) != 0))
0d39a070
DJ
869 break;
870 }
ec3d575a 871
0963b4bd
MS
872 else if ((insn & 0xffd0) == 0xe900 /* stmdb Rn{!},
873 { registers } */
ec3d575a
UW
874 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
875 {
876 pv_t addr = regs[bits (insn, 0, 3)];
877 int regno;
878
879 if (pv_area_store_would_trash (stack, addr))
880 break;
881
882 /* Calculate offsets of saved registers. */
883 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
884 if (inst2 & (1 << regno))
885 {
886 addr = pv_add_constant (addr, -4);
887 pv_area_store (stack, addr, 4, regs[regno]);
888 }
889
890 if (insn & 0x0020)
891 regs[bits (insn, 0, 3)] = addr;
892 }
893
0963b4bd
MS
894 else if ((insn & 0xff50) == 0xe940 /* strd Rt, Rt2,
895 [Rn, #+/-imm]{!} */
ec3d575a
UW
896 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
897 {
898 int regno1 = bits (inst2, 12, 15);
899 int regno2 = bits (inst2, 8, 11);
900 pv_t addr = regs[bits (insn, 0, 3)];
901
902 offset = inst2 & 0xff;
903 if (insn & 0x0080)
904 addr = pv_add_constant (addr, offset);
905 else
906 addr = pv_add_constant (addr, -offset);
907
908 if (pv_area_store_would_trash (stack, addr))
909 break;
910
911 pv_area_store (stack, addr, 4, regs[regno1]);
912 pv_area_store (stack, pv_add_constant (addr, 4),
913 4, regs[regno2]);
914
915 if (insn & 0x0020)
916 regs[bits (insn, 0, 3)] = addr;
917 }
918
919 else if ((insn & 0xfff0) == 0xf8c0 /* str Rt,[Rn,+/-#imm]{!} */
920 && (inst2 & 0x0c00) == 0x0c00
921 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
922 {
923 int regno = bits (inst2, 12, 15);
924 pv_t addr = regs[bits (insn, 0, 3)];
925
926 offset = inst2 & 0xff;
927 if (inst2 & 0x0200)
928 addr = pv_add_constant (addr, offset);
929 else
930 addr = pv_add_constant (addr, -offset);
931
932 if (pv_area_store_would_trash (stack, addr))
933 break;
934
935 pv_area_store (stack, addr, 4, regs[regno]);
936
937 if (inst2 & 0x0100)
938 regs[bits (insn, 0, 3)] = addr;
939 }
940
941 else if ((insn & 0xfff0) == 0xf8c0 /* str.w Rt,[Rn,#imm] */
942 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
943 {
944 int regno = bits (inst2, 12, 15);
945 pv_t addr;
946
947 offset = inst2 & 0xfff;
948 addr = pv_add_constant (regs[bits (insn, 0, 3)], offset);
949
950 if (pv_area_store_would_trash (stack, addr))
951 break;
952
953 pv_area_store (stack, addr, 4, regs[regno]);
954 }
955
956 else if ((insn & 0xffd0) == 0xf880 /* str{bh}.w Rt,[Rn,#imm] */
0d39a070 957 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 958 /* Ignore stores of argument registers to the stack. */
0d39a070 959 ;
ec3d575a
UW
960
961 else if ((insn & 0xffd0) == 0xf800 /* str{bh} Rt,[Rn,#+/-imm] */
962 && (inst2 & 0x0d00) == 0x0c00
0d39a070 963 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 964 /* Ignore stores of argument registers to the stack. */
0d39a070 965 ;
ec3d575a 966
0963b4bd
MS
967 else if ((insn & 0xffd0) == 0xe890 /* ldmia Rn[!],
968 { registers } */
ec3d575a
UW
969 && (inst2 & 0x8000) == 0x0000
970 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
971 /* Ignore block loads from the stack, potentially copying
972 parameters from memory. */
0d39a070 973 ;
ec3d575a 974
0963b4bd
MS
975 else if ((insn & 0xffb0) == 0xe950 /* ldrd Rt, Rt2,
976 [Rn, #+/-imm] */
0d39a070 977 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 978 /* Similarly ignore dual loads from the stack. */
0d39a070 979 ;
ec3d575a
UW
980
981 else if ((insn & 0xfff0) == 0xf850 /* ldr Rt,[Rn,#+/-imm] */
982 && (inst2 & 0x0d00) == 0x0c00
0d39a070 983 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 984 /* Similarly ignore single loads from the stack. */
0d39a070 985 ;
ec3d575a
UW
986
987 else if ((insn & 0xfff0) == 0xf8d0 /* ldr.w Rt,[Rn,#imm] */
0d39a070 988 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 989 /* Similarly ignore single loads from the stack. */
0d39a070 990 ;
ec3d575a
UW
991
992 else if ((insn & 0xfbf0) == 0xf100 /* add.w Rd, Rn, #imm */
993 && (inst2 & 0x8000) == 0x0000)
994 {
995 unsigned int imm = ((bits (insn, 10, 10) << 11)
996 | (bits (inst2, 12, 14) << 8)
997 | bits (inst2, 0, 7));
998
999 regs[bits (inst2, 8, 11)]
1000 = pv_add_constant (regs[bits (insn, 0, 3)],
1001 thumb_expand_immediate (imm));
1002 }
1003
1004 else if ((insn & 0xfbf0) == 0xf200 /* addw Rd, Rn, #imm */
1005 && (inst2 & 0x8000) == 0x0000)
0d39a070 1006 {
ec3d575a
UW
1007 unsigned int imm = ((bits (insn, 10, 10) << 11)
1008 | (bits (inst2, 12, 14) << 8)
1009 | bits (inst2, 0, 7));
1010
1011 regs[bits (inst2, 8, 11)]
1012 = pv_add_constant (regs[bits (insn, 0, 3)], imm);
1013 }
1014
1015 else if ((insn & 0xfbf0) == 0xf1a0 /* sub.w Rd, Rn, #imm */
1016 && (inst2 & 0x8000) == 0x0000)
1017 {
1018 unsigned int imm = ((bits (insn, 10, 10) << 11)
1019 | (bits (inst2, 12, 14) << 8)
1020 | bits (inst2, 0, 7));
1021
1022 regs[bits (inst2, 8, 11)]
1023 = pv_add_constant (regs[bits (insn, 0, 3)],
1024 - (CORE_ADDR) thumb_expand_immediate (imm));
1025 }
1026
1027 else if ((insn & 0xfbf0) == 0xf2a0 /* subw Rd, Rn, #imm */
1028 && (inst2 & 0x8000) == 0x0000)
1029 {
1030 unsigned int imm = ((bits (insn, 10, 10) << 11)
1031 | (bits (inst2, 12, 14) << 8)
1032 | bits (inst2, 0, 7));
1033
1034 regs[bits (inst2, 8, 11)]
1035 = pv_add_constant (regs[bits (insn, 0, 3)], - (CORE_ADDR) imm);
1036 }
1037
1038 else if ((insn & 0xfbff) == 0xf04f) /* mov.w Rd, #const */
1039 {
1040 unsigned int imm = ((bits (insn, 10, 10) << 11)
1041 | (bits (inst2, 12, 14) << 8)
1042 | bits (inst2, 0, 7));
1043
1044 regs[bits (inst2, 8, 11)]
1045 = pv_constant (thumb_expand_immediate (imm));
1046 }
1047
1048 else if ((insn & 0xfbf0) == 0xf240) /* movw Rd, #const */
1049 {
621c6d5b
YQ
1050 unsigned int imm
1051 = EXTRACT_MOVW_MOVT_IMM_T (insn, inst2);
ec3d575a
UW
1052
1053 regs[bits (inst2, 8, 11)] = pv_constant (imm);
1054 }
1055
1056 else if (insn == 0xea5f /* mov.w Rd,Rm */
1057 && (inst2 & 0xf0f0) == 0)
1058 {
1059 int dst_reg = (inst2 & 0x0f00) >> 8;
1060 int src_reg = inst2 & 0xf;
1061 regs[dst_reg] = regs[src_reg];
1062 }
1063
1064 else if ((insn & 0xff7f) == 0xf85f) /* ldr.w Rt,<label> */
1065 {
1066 /* Constant pool loads. */
1067 unsigned int constant;
1068 CORE_ADDR loc;
1069
1070 offset = bits (insn, 0, 11);
1071 if (insn & 0x0080)
1072 loc = start + 4 + offset;
1073 else
1074 loc = start + 4 - offset;
1075
1076 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1077 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1078 }
1079
1080 else if ((insn & 0xff7f) == 0xe95f) /* ldrd Rt,Rt2,<label> */
1081 {
1082 /* Constant pool loads. */
1083 unsigned int constant;
1084 CORE_ADDR loc;
1085
1086 offset = bits (insn, 0, 7) << 2;
1087 if (insn & 0x0080)
1088 loc = start + 4 + offset;
1089 else
1090 loc = start + 4 - offset;
1091
1092 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1093 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1094
1095 constant = read_memory_unsigned_integer (loc + 4, 4, byte_order);
1096 regs[bits (inst2, 8, 11)] = pv_constant (constant);
1097 }
1098
1099 else if (thumb2_instruction_changes_pc (insn, inst2))
1100 {
1101 /* Don't scan past anything that might change control flow. */
0d39a070
DJ
1102 break;
1103 }
ec3d575a
UW
1104 else
1105 {
1106 /* The optimizer might shove anything into the prologue,
1107 so we just skip what we don't recognize. */
1108 unrecognized_pc = start;
1109 }
0d39a070
DJ
1110
1111 start += 2;
1112 }
ec3d575a 1113 else if (thumb_instruction_changes_pc (insn))
3d74b771 1114 {
ec3d575a 1115 /* Don't scan past anything that might change control flow. */
da3c6d4a 1116 break;
3d74b771 1117 }
ec3d575a
UW
1118 else
1119 {
1120 /* The optimizer might shove anything into the prologue,
1121 so we just skip what we don't recognize. */
1122 unrecognized_pc = start;
1123 }
29d73ae4
DJ
1124
1125 start += 2;
c906108c
SS
1126 }
1127
0d39a070
DJ
1128 if (arm_debug)
1129 fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1130 paddress (gdbarch, start));
1131
ec3d575a
UW
1132 if (unrecognized_pc == 0)
1133 unrecognized_pc = start;
1134
29d73ae4
DJ
1135 if (cache == NULL)
1136 {
1137 do_cleanups (back_to);
ec3d575a 1138 return unrecognized_pc;
29d73ae4
DJ
1139 }
1140
29d73ae4
DJ
1141 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1142 {
1143 /* Frame pointer is fp. Frame size is constant. */
1144 cache->framereg = ARM_FP_REGNUM;
1145 cache->framesize = -regs[ARM_FP_REGNUM].k;
1146 }
1147 else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
1148 {
1149 /* Frame pointer is r7. Frame size is constant. */
1150 cache->framereg = THUMB_FP_REGNUM;
1151 cache->framesize = -regs[THUMB_FP_REGNUM].k;
1152 }
1153 else if (pv_is_register (regs[ARM_SP_REGNUM], ARM_SP_REGNUM))
1154 {
1155 /* Try the stack pointer... this is a bit desperate. */
1156 cache->framereg = ARM_SP_REGNUM;
1157 cache->framesize = -regs[ARM_SP_REGNUM].k;
1158 }
1159 else
1160 {
1161 /* We're just out of luck. We don't know where the frame is. */
1162 cache->framereg = -1;
1163 cache->framesize = 0;
1164 }
1165
1166 for (i = 0; i < 16; i++)
1167 if (pv_area_find_reg (stack, gdbarch, i, &offset))
1168 cache->saved_regs[i].addr = offset;
1169
1170 do_cleanups (back_to);
ec3d575a 1171 return unrecognized_pc;
c906108c
SS
1172}
1173
621c6d5b
YQ
1174
1175/* Try to analyze the instructions starting from PC, which load symbol
1176 __stack_chk_guard. Return the address of instruction after loading this
1177 symbol, set the dest register number to *BASEREG, and set the size of
1178 instructions for loading symbol in OFFSET. Return 0 if instructions are
1179 not recognized. */
1180
1181static CORE_ADDR
1182arm_analyze_load_stack_chk_guard(CORE_ADDR pc, struct gdbarch *gdbarch,
1183 unsigned int *destreg, int *offset)
1184{
1185 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1186 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1187 unsigned int low, high, address;
1188
1189 address = 0;
1190 if (is_thumb)
1191 {
1192 unsigned short insn1
1193 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
1194
1195 if ((insn1 & 0xf800) == 0x4800) /* ldr Rd, #immed */
1196 {
1197 *destreg = bits (insn1, 8, 10);
1198 *offset = 2;
1199 address = bits (insn1, 0, 7);
1200 }
1201 else if ((insn1 & 0xfbf0) == 0xf240) /* movw Rd, #const */
1202 {
1203 unsigned short insn2
1204 = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);
1205
1206 low = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1207
1208 insn1
1209 = read_memory_unsigned_integer (pc + 4, 2, byte_order_for_code);
1210 insn2
1211 = read_memory_unsigned_integer (pc + 6, 2, byte_order_for_code);
1212
1213 /* movt Rd, #const */
1214 if ((insn1 & 0xfbc0) == 0xf2c0)
1215 {
1216 high = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1217 *destreg = bits (insn2, 8, 11);
1218 *offset = 8;
1219 address = (high << 16 | low);
1220 }
1221 }
1222 }
1223 else
1224 {
2e9e421f
UW
1225 unsigned int insn
1226 = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
1227
1228 if ((insn & 0x0e5f0000) == 0x041f0000) /* ldr Rd, #immed */
1229 {
1230 address = bits (insn, 0, 11);
1231 *destreg = bits (insn, 12, 15);
1232 *offset = 4;
1233 }
1234 else if ((insn & 0x0ff00000) == 0x03000000) /* movw Rd, #const */
1235 {
1236 low = EXTRACT_MOVW_MOVT_IMM_A (insn);
1237
1238 insn
1239 = read_memory_unsigned_integer (pc + 4, 4, byte_order_for_code);
1240
1241 if ((insn & 0x0ff00000) == 0x03400000) /* movt Rd, #const */
1242 {
1243 high = EXTRACT_MOVW_MOVT_IMM_A (insn);
1244 *destreg = bits (insn, 12, 15);
1245 *offset = 8;
1246 address = (high << 16 | low);
1247 }
1248 }
621c6d5b
YQ
1249 }
1250
1251 return address;
1252}
1253
1254/* Try to skip a sequence of instructions used for stack protector. If PC
0963b4bd
MS
1255 points to the first instruction of this sequence, return the address of
1256 first instruction after this sequence, otherwise, return original PC.
621c6d5b
YQ
1257
1258 On arm, this sequence of instructions is composed of mainly three steps,
1259 Step 1: load symbol __stack_chk_guard,
1260 Step 2: load from address of __stack_chk_guard,
1261 Step 3: store it to somewhere else.
1262
1263 Usually, instructions on step 2 and step 3 are the same on various ARM
1264 architectures. On step 2, it is one instruction 'ldr Rx, [Rn, #0]', and
1265 on step 3, it is also one instruction 'str Rx, [r7, #immd]'. However,
1266 instructions in step 1 vary from different ARM architectures. On ARMv7,
1267 they are,
1268
1269 movw Rn, #:lower16:__stack_chk_guard
1270 movt Rn, #:upper16:__stack_chk_guard
1271
1272 On ARMv5t, it is,
1273
1274 ldr Rn, .Label
1275 ....
1276 .Lable:
1277 .word __stack_chk_guard
1278
1279 Since ldr/str is a very popular instruction, we can't use them as
1280 'fingerprint' or 'signature' of stack protector sequence. Here we choose
1281 sequence {movw/movt, ldr}/ldr/str plus symbol __stack_chk_guard, if not
1282 stripped, as the 'fingerprint' of a stack protector cdoe sequence. */
1283
1284static CORE_ADDR
1285arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
1286{
1287 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1288 unsigned int address, basereg;
1289 struct minimal_symbol *stack_chk_guard;
1290 int offset;
1291 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1292 CORE_ADDR addr;
1293
1294 /* Try to parse the instructions in Step 1. */
1295 addr = arm_analyze_load_stack_chk_guard (pc, gdbarch,
1296 &basereg, &offset);
1297 if (!addr)
1298 return pc;
1299
1300 stack_chk_guard = lookup_minimal_symbol_by_pc (addr);
1301 /* If name of symbol doesn't start with '__stack_chk_guard', this
1302 instruction sequence is not for stack protector. If symbol is
1303 removed, we conservatively think this sequence is for stack protector. */
1304 if (stack_chk_guard
c1c2ab58
UW
1305 && strncmp (SYMBOL_LINKAGE_NAME (stack_chk_guard), "__stack_chk_guard",
1306 strlen ("__stack_chk_guard")) != 0)
621c6d5b
YQ
1307 return pc;
1308
1309 if (is_thumb)
1310 {
1311 unsigned int destreg;
1312 unsigned short insn
1313 = read_memory_unsigned_integer (pc + offset, 2, byte_order_for_code);
1314
1315 /* Step 2: ldr Rd, [Rn, #immed], encoding T1. */
1316 if ((insn & 0xf800) != 0x6800)
1317 return pc;
1318 if (bits (insn, 3, 5) != basereg)
1319 return pc;
1320 destreg = bits (insn, 0, 2);
1321
1322 insn = read_memory_unsigned_integer (pc + offset + 2, 2,
1323 byte_order_for_code);
1324 /* Step 3: str Rd, [Rn, #immed], encoding T1. */
1325 if ((insn & 0xf800) != 0x6000)
1326 return pc;
1327 if (destreg != bits (insn, 0, 2))
1328 return pc;
1329 }
1330 else
1331 {
1332 unsigned int destreg;
1333 unsigned int insn
1334 = read_memory_unsigned_integer (pc + offset, 4, byte_order_for_code);
1335
1336 /* Step 2: ldr Rd, [Rn, #immed], encoding A1. */
1337 if ((insn & 0x0e500000) != 0x04100000)
1338 return pc;
1339 if (bits (insn, 16, 19) != basereg)
1340 return pc;
1341 destreg = bits (insn, 12, 15);
1342 /* Step 3: str Rd, [Rn, #immed], encoding A1. */
1343 insn = read_memory_unsigned_integer (pc + offset + 4,
1344 4, byte_order_for_code);
1345 if ((insn & 0x0e500000) != 0x04000000)
1346 return pc;
1347 if (bits (insn, 12, 15) != destreg)
1348 return pc;
1349 }
1350 /* The size of total two instructions ldr/str is 4 on Thumb-2, while 8
1351 on arm. */
1352 if (is_thumb)
1353 return pc + offset + 4;
1354 else
1355 return pc + offset + 8;
1356}
1357
da3c6d4a
MS
1358/* Advance the PC across any function entry prologue instructions to
1359 reach some "real" code.
34e8f22d
RE
1360
1361 The APCS (ARM Procedure Call Standard) defines the following
ed9a39eb 1362 prologue:
c906108c 1363
c5aa993b
JM
1364 mov ip, sp
1365 [stmfd sp!, {a1,a2,a3,a4}]
1366 stmfd sp!, {...,fp,ip,lr,pc}
ed9a39eb
JM
1367 [stfe f7, [sp, #-12]!]
1368 [stfe f6, [sp, #-12]!]
1369 [stfe f5, [sp, #-12]!]
1370 [stfe f4, [sp, #-12]!]
0963b4bd 1371 sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn. */
c906108c 1372
34e8f22d 1373static CORE_ADDR
6093d2eb 1374arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
c906108c 1375{
e17a4113 1376 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
c906108c
SS
1377 unsigned long inst;
1378 CORE_ADDR skip_pc;
a89fea3c 1379 CORE_ADDR func_addr, limit_pc;
c906108c
SS
1380 struct symtab_and_line sal;
1381
a89fea3c
JL
1382 /* See if we can determine the end of the prologue via the symbol table.
1383 If so, then return either PC, or the PC after the prologue, whichever
1384 is greater. */
1385 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
c906108c 1386 {
d80b854b
UW
1387 CORE_ADDR post_prologue_pc
1388 = skip_prologue_using_sal (gdbarch, func_addr);
0d39a070
DJ
1389 struct symtab *s = find_pc_symtab (func_addr);
1390
621c6d5b
YQ
1391 if (post_prologue_pc)
1392 post_prologue_pc
1393 = arm_skip_stack_protector (post_prologue_pc, gdbarch);
1394
1395
0d39a070
DJ
1396 /* GCC always emits a line note before the prologue and another
1397 one after, even if the two are at the same address or on the
1398 same line. Take advantage of this so that we do not need to
1399 know every instruction that might appear in the prologue. We
1400 will have producer information for most binaries; if it is
1401 missing (e.g. for -gstabs), assuming the GNU tools. */
1402 if (post_prologue_pc
1403 && (s == NULL
1404 || s->producer == NULL
1405 || strncmp (s->producer, "GNU ", sizeof ("GNU ") - 1) == 0))
1406 return post_prologue_pc;
1407
a89fea3c 1408 if (post_prologue_pc != 0)
0d39a070
DJ
1409 {
1410 CORE_ADDR analyzed_limit;
1411
1412 /* For non-GCC compilers, make sure the entire line is an
1413 acceptable prologue; GDB will round this function's
1414 return value up to the end of the following line so we
1415 can not skip just part of a line (and we do not want to).
1416
1417 RealView does not treat the prologue specially, but does
1418 associate prologue code with the opening brace; so this
1419 lets us skip the first line if we think it is the opening
1420 brace. */
9779414d 1421 if (arm_pc_is_thumb (gdbarch, func_addr))
0d39a070
DJ
1422 analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
1423 post_prologue_pc, NULL);
1424 else
1425 analyzed_limit = arm_analyze_prologue (gdbarch, func_addr,
1426 post_prologue_pc, NULL);
1427
1428 if (analyzed_limit != post_prologue_pc)
1429 return func_addr;
1430
1431 return post_prologue_pc;
1432 }
c906108c
SS
1433 }
1434
a89fea3c
JL
1435 /* Can't determine prologue from the symbol table, need to examine
1436 instructions. */
c906108c 1437
a89fea3c
JL
1438 /* Find an upper limit on the function prologue using the debug
1439 information. If the debug information could not be used to provide
1440 that bound, then use an arbitrary large number as the upper bound. */
0963b4bd 1441 /* Like arm_scan_prologue, stop no later than pc + 64. */
d80b854b 1442 limit_pc = skip_prologue_using_sal (gdbarch, pc);
a89fea3c
JL
1443 if (limit_pc == 0)
1444 limit_pc = pc + 64; /* Magic. */
1445
c906108c 1446
29d73ae4 1447 /* Check if this is Thumb code. */
9779414d 1448 if (arm_pc_is_thumb (gdbarch, pc))
a89fea3c 1449 return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
29d73ae4 1450
a89fea3c 1451 for (skip_pc = pc; skip_pc < limit_pc; skip_pc += 4)
f43845b3 1452 {
e17a4113 1453 inst = read_memory_unsigned_integer (skip_pc, 4, byte_order_for_code);
9d4fde75 1454
b8d5e71d
MS
1455 /* "mov ip, sp" is no longer a required part of the prologue. */
1456 if (inst == 0xe1a0c00d) /* mov ip, sp */
1457 continue;
c906108c 1458
28cd8767
JG
1459 if ((inst & 0xfffff000) == 0xe28dc000) /* add ip, sp #n */
1460 continue;
1461
1462 if ((inst & 0xfffff000) == 0xe24dc000) /* sub ip, sp #n */
1463 continue;
1464
b8d5e71d
MS
1465 /* Some prologues begin with "str lr, [sp, #-4]!". */
1466 if (inst == 0xe52de004) /* str lr, [sp, #-4]! */
1467 continue;
c906108c 1468
b8d5e71d
MS
1469 if ((inst & 0xfffffff0) == 0xe92d0000) /* stmfd sp!,{a1,a2,a3,a4} */
1470 continue;
c906108c 1471
b8d5e71d
MS
1472 if ((inst & 0xfffff800) == 0xe92dd800) /* stmfd sp!,{fp,ip,lr,pc} */
1473 continue;
11d3b27d 1474
b8d5e71d
MS
1475 /* Any insns after this point may float into the code, if it makes
1476 for better instruction scheduling, so we skip them only if we
1477 find them, but still consider the function to be frame-ful. */
f43845b3 1478
b8d5e71d
MS
1479 /* We may have either one sfmfd instruction here, or several stfe
1480 insns, depending on the version of floating point code we
1481 support. */
1482 if ((inst & 0xffbf0fff) == 0xec2d0200) /* sfmfd fn, <cnt>, [sp]! */
1483 continue;
1484
1485 if ((inst & 0xffff8fff) == 0xed6d0103) /* stfe fn, [sp, #-12]! */
1486 continue;
1487
1488 if ((inst & 0xfffff000) == 0xe24cb000) /* sub fp, ip, #nn */
1489 continue;
1490
1491 if ((inst & 0xfffff000) == 0xe24dd000) /* sub sp, sp, #nn */
1492 continue;
1493
f8bf5763
PM
1494 if ((inst & 0xffffc000) == 0xe54b0000 /* strb r(0123),[r11,#-nn] */
1495 || (inst & 0xffffc0f0) == 0xe14b00b0 /* strh r(0123),[r11,#-nn] */
1496 || (inst & 0xffffc000) == 0xe50b0000) /* str r(0123),[r11,#-nn] */
b8d5e71d
MS
1497 continue;
1498
f8bf5763
PM
1499 if ((inst & 0xffffc000) == 0xe5cd0000 /* strb r(0123),[sp,#nn] */
1500 || (inst & 0xffffc0f0) == 0xe1cd00b0 /* strh r(0123),[sp,#nn] */
1501 || (inst & 0xffffc000) == 0xe58d0000) /* str r(0123),[sp,#nn] */
b8d5e71d
MS
1502 continue;
1503
1504 /* Un-recognized instruction; stop scanning. */
1505 break;
f43845b3 1506 }
c906108c 1507
0963b4bd 1508 return skip_pc; /* End of prologue. */
c906108c 1509}
94c30b78 1510
c5aa993b 1511/* *INDENT-OFF* */
c906108c
SS
1512/* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
1513 This function decodes a Thumb function prologue to determine:
1514 1) the size of the stack frame
1515 2) which registers are saved on it
1516 3) the offsets of saved regs
1517 4) the offset from the stack pointer to the frame pointer
c906108c 1518
da59e081
JM
1519 A typical Thumb function prologue would create this stack frame
1520 (offsets relative to FP)
c906108c
SS
1521 old SP -> 24 stack parameters
1522 20 LR
1523 16 R7
1524 R7 -> 0 local variables (16 bytes)
1525 SP -> -12 additional stack space (12 bytes)
1526 The frame size would thus be 36 bytes, and the frame offset would be
0963b4bd 1527 12 bytes. The frame register is R7.
da59e081 1528
da3c6d4a
MS
1529 The comments for thumb_skip_prolog() describe the algorithm we use
1530 to detect the end of the prolog. */
c5aa993b
JM
1531/* *INDENT-ON* */
1532
c906108c 1533static void
be8626e0 1534thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
b39cc962 1535 CORE_ADDR block_addr, struct arm_prologue_cache *cache)
c906108c
SS
1536{
1537 CORE_ADDR prologue_start;
1538 CORE_ADDR prologue_end;
1539 CORE_ADDR current_pc;
c906108c 1540
b39cc962
DJ
1541 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1542 &prologue_end))
c906108c 1543 {
ec3d575a
UW
1544 /* See comment in arm_scan_prologue for an explanation of
1545 this heuristics. */
1546 if (prologue_end > prologue_start + 64)
1547 {
1548 prologue_end = prologue_start + 64;
1549 }
c906108c
SS
1550 }
1551 else
f7060f85
DJ
1552 /* We're in the boondocks: we have no idea where the start of the
1553 function is. */
1554 return;
c906108c 1555
eb5492fa 1556 prologue_end = min (prologue_end, prev_pc);
c906108c 1557
be8626e0 1558 thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
1559}
1560
0d39a070 1561/* Return 1 if THIS_INSTR might change control flow, 0 otherwise. */
c906108c 1562
0d39a070
DJ
1563static int
1564arm_instruction_changes_pc (uint32_t this_instr)
c906108c 1565{
0d39a070
DJ
1566 if (bits (this_instr, 28, 31) == INST_NV)
1567 /* Unconditional instructions. */
1568 switch (bits (this_instr, 24, 27))
1569 {
1570 case 0xa:
1571 case 0xb:
1572 /* Branch with Link and change to Thumb. */
1573 return 1;
1574 case 0xc:
1575 case 0xd:
1576 case 0xe:
1577 /* Coprocessor register transfer. */
1578 if (bits (this_instr, 12, 15) == 15)
1579 error (_("Invalid update to pc in instruction"));
1580 return 0;
1581 default:
1582 return 0;
1583 }
1584 else
1585 switch (bits (this_instr, 25, 27))
1586 {
1587 case 0x0:
1588 if (bits (this_instr, 23, 24) == 2 && bit (this_instr, 20) == 0)
1589 {
1590 /* Multiplies and extra load/stores. */
1591 if (bit (this_instr, 4) == 1 && bit (this_instr, 7) == 1)
1592 /* Neither multiplies nor extension load/stores are allowed
1593 to modify PC. */
1594 return 0;
1595
1596 /* Otherwise, miscellaneous instructions. */
1597
1598 /* BX <reg>, BXJ <reg>, BLX <reg> */
1599 if (bits (this_instr, 4, 27) == 0x12fff1
1600 || bits (this_instr, 4, 27) == 0x12fff2
1601 || bits (this_instr, 4, 27) == 0x12fff3)
1602 return 1;
1603
1604 /* Other miscellaneous instructions are unpredictable if they
1605 modify PC. */
1606 return 0;
1607 }
1608 /* Data processing instruction. Fall through. */
c906108c 1609
0d39a070
DJ
1610 case 0x1:
1611 if (bits (this_instr, 12, 15) == 15)
1612 return 1;
1613 else
1614 return 0;
c906108c 1615
0d39a070
DJ
1616 case 0x2:
1617 case 0x3:
1618 /* Media instructions and architecturally undefined instructions. */
1619 if (bits (this_instr, 25, 27) == 3 && bit (this_instr, 4) == 1)
1620 return 0;
c906108c 1621
0d39a070
DJ
1622 /* Stores. */
1623 if (bit (this_instr, 20) == 0)
1624 return 0;
2a451106 1625
0d39a070
DJ
1626 /* Loads. */
1627 if (bits (this_instr, 12, 15) == ARM_PC_REGNUM)
1628 return 1;
1629 else
1630 return 0;
2a451106 1631
0d39a070
DJ
1632 case 0x4:
1633 /* Load/store multiple. */
1634 if (bit (this_instr, 20) == 1 && bit (this_instr, 15) == 1)
1635 return 1;
1636 else
1637 return 0;
2a451106 1638
0d39a070
DJ
1639 case 0x5:
1640 /* Branch and branch with link. */
1641 return 1;
2a451106 1642
0d39a070
DJ
1643 case 0x6:
1644 case 0x7:
1645 /* Coprocessor transfers or SWIs can not affect PC. */
1646 return 0;
eb5492fa 1647
0d39a070 1648 default:
9b20d036 1649 internal_error (__FILE__, __LINE__, _("bad value in switch"));
0d39a070
DJ
1650 }
1651}
c906108c 1652
0d39a070
DJ
1653/* Analyze an ARM mode prologue starting at PROLOGUE_START and
1654 continuing no further than PROLOGUE_END. If CACHE is non-NULL,
1655 fill it in. Return the first address not recognized as a prologue
1656 instruction.
eb5492fa 1657
0d39a070
DJ
1658 We recognize all the instructions typically found in ARM prologues,
1659 plus harmless instructions which can be skipped (either for analysis
1660 purposes, or a more restrictive set that can be skipped when finding
1661 the end of the prologue). */
1662
1663static CORE_ADDR
1664arm_analyze_prologue (struct gdbarch *gdbarch,
1665 CORE_ADDR prologue_start, CORE_ADDR prologue_end,
1666 struct arm_prologue_cache *cache)
1667{
1668 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1669 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1670 int regno;
1671 CORE_ADDR offset, current_pc;
1672 pv_t regs[ARM_FPS_REGNUM];
1673 struct pv_area *stack;
1674 struct cleanup *back_to;
1675 int framereg, framesize;
1676 CORE_ADDR unrecognized_pc = 0;
1677
1678 /* Search the prologue looking for instructions that set up the
96baa820 1679 frame pointer, adjust the stack pointer, and save registers.
ed9a39eb 1680
96baa820
JM
1681 Be careful, however, and if it doesn't look like a prologue,
1682 don't try to scan it. If, for instance, a frameless function
1683 begins with stmfd sp!, then we will tell ourselves there is
b8d5e71d 1684 a frame, which will confuse stack traceback, as well as "finish"
96baa820 1685 and other operations that rely on a knowledge of the stack
0d39a070 1686 traceback. */
d4473757 1687
4be43953
DJ
1688 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1689 regs[regno] = pv_register (regno, 0);
55f960e1 1690 stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
4be43953
DJ
1691 back_to = make_cleanup_free_pv_area (stack);
1692
94c30b78
MS
1693 for (current_pc = prologue_start;
1694 current_pc < prologue_end;
f43845b3 1695 current_pc += 4)
96baa820 1696 {
e17a4113
UW
1697 unsigned int insn
1698 = read_memory_unsigned_integer (current_pc, 4, byte_order_for_code);
9d4fde75 1699
94c30b78 1700 if (insn == 0xe1a0c00d) /* mov ip, sp */
f43845b3 1701 {
4be43953 1702 regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
28cd8767
JG
1703 continue;
1704 }
0d39a070
DJ
1705 else if ((insn & 0xfff00000) == 0xe2800000 /* add Rd, Rn, #n */
1706 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767
JG
1707 {
1708 unsigned imm = insn & 0xff; /* immediate value */
1709 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
0d39a070 1710 int rd = bits (insn, 12, 15);
28cd8767 1711 imm = (imm >> rot) | (imm << (32 - rot));
0d39a070 1712 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
28cd8767
JG
1713 continue;
1714 }
0d39a070
DJ
1715 else if ((insn & 0xfff00000) == 0xe2400000 /* sub Rd, Rn, #n */
1716 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767
JG
1717 {
1718 unsigned imm = insn & 0xff; /* immediate value */
1719 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
0d39a070 1720 int rd = bits (insn, 12, 15);
28cd8767 1721 imm = (imm >> rot) | (imm << (32 - rot));
0d39a070 1722 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
f43845b3
MS
1723 continue;
1724 }
0963b4bd
MS
1725 else if ((insn & 0xffff0fff) == 0xe52d0004) /* str Rd,
1726 [sp, #-4]! */
f43845b3 1727 {
4be43953
DJ
1728 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1729 break;
1730 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
0d39a070
DJ
1731 pv_area_store (stack, regs[ARM_SP_REGNUM], 4,
1732 regs[bits (insn, 12, 15)]);
f43845b3
MS
1733 continue;
1734 }
1735 else if ((insn & 0xffff0000) == 0xe92d0000)
d4473757
KB
1736 /* stmfd sp!, {..., fp, ip, lr, pc}
1737 or
1738 stmfd sp!, {a1, a2, a3, a4} */
c906108c 1739 {
d4473757 1740 int mask = insn & 0xffff;
ed9a39eb 1741
4be43953
DJ
1742 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1743 break;
1744
94c30b78 1745 /* Calculate offsets of saved registers. */
34e8f22d 1746 for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
d4473757
KB
1747 if (mask & (1 << regno))
1748 {
0963b4bd
MS
1749 regs[ARM_SP_REGNUM]
1750 = pv_add_constant (regs[ARM_SP_REGNUM], -4);
4be43953 1751 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
d4473757
KB
1752 }
1753 }
0d39a070
DJ
1754 else if ((insn & 0xffff0000) == 0xe54b0000 /* strb rx,[r11,#-n] */
1755 || (insn & 0xffff00f0) == 0xe14b00b0 /* strh rx,[r11,#-n] */
f8bf5763 1756 || (insn & 0xffffc000) == 0xe50b0000) /* str rx,[r11,#-n] */
b8d5e71d
MS
1757 {
1758 /* No need to add this to saved_regs -- it's just an arg reg. */
1759 continue;
1760 }
0d39a070
DJ
1761 else if ((insn & 0xffff0000) == 0xe5cd0000 /* strb rx,[sp,#n] */
1762 || (insn & 0xffff00f0) == 0xe1cd00b0 /* strh rx,[sp,#n] */
f8bf5763 1763 || (insn & 0xffffc000) == 0xe58d0000) /* str rx,[sp,#n] */
f43845b3
MS
1764 {
1765 /* No need to add this to saved_regs -- it's just an arg reg. */
1766 continue;
1767 }
0963b4bd
MS
1768 else if ((insn & 0xfff00000) == 0xe8800000 /* stm Rn,
1769 { registers } */
0d39a070
DJ
1770 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1771 {
1772 /* No need to add this to saved_regs -- it's just arg regs. */
1773 continue;
1774 }
d4473757
KB
1775 else if ((insn & 0xfffff000) == 0xe24cb000) /* sub fp, ip #n */
1776 {
94c30b78
MS
1777 unsigned imm = insn & 0xff; /* immediate value */
1778 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
d4473757 1779 imm = (imm >> rot) | (imm << (32 - rot));
4be43953 1780 regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
d4473757
KB
1781 }
1782 else if ((insn & 0xfffff000) == 0xe24dd000) /* sub sp, sp #n */
1783 {
94c30b78
MS
1784 unsigned imm = insn & 0xff; /* immediate value */
1785 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
d4473757 1786 imm = (imm >> rot) | (imm << (32 - rot));
4be43953 1787 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
d4473757 1788 }
0963b4bd
MS
1789 else if ((insn & 0xffff7fff) == 0xed6d0103 /* stfe f?,
1790 [sp, -#c]! */
2af46ca0 1791 && gdbarch_tdep (gdbarch)->have_fpa_registers)
d4473757 1792 {
4be43953
DJ
1793 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1794 break;
1795
1796 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
34e8f22d 1797 regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
4be43953 1798 pv_area_store (stack, regs[ARM_SP_REGNUM], 12, regs[regno]);
d4473757 1799 }
0963b4bd
MS
1800 else if ((insn & 0xffbf0fff) == 0xec2d0200 /* sfmfd f0, 4,
1801 [sp!] */
2af46ca0 1802 && gdbarch_tdep (gdbarch)->have_fpa_registers)
d4473757
KB
1803 {
1804 int n_saved_fp_regs;
1805 unsigned int fp_start_reg, fp_bound_reg;
1806
4be43953
DJ
1807 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1808 break;
1809
94c30b78 1810 if ((insn & 0x800) == 0x800) /* N0 is set */
96baa820 1811 {
d4473757
KB
1812 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1813 n_saved_fp_regs = 3;
1814 else
1815 n_saved_fp_regs = 1;
96baa820 1816 }
d4473757 1817 else
96baa820 1818 {
d4473757
KB
1819 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1820 n_saved_fp_regs = 2;
1821 else
1822 n_saved_fp_regs = 4;
96baa820 1823 }
d4473757 1824
34e8f22d 1825 fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
d4473757
KB
1826 fp_bound_reg = fp_start_reg + n_saved_fp_regs;
1827 for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
96baa820 1828 {
4be43953
DJ
1829 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
1830 pv_area_store (stack, regs[ARM_SP_REGNUM], 12,
1831 regs[fp_start_reg++]);
96baa820 1832 }
c906108c 1833 }
0d39a070
DJ
1834 else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
1835 {
1836 /* Allow some special function calls when skipping the
1837 prologue; GCC generates these before storing arguments to
1838 the stack. */
1839 CORE_ADDR dest = BranchDest (current_pc, insn);
1840
e0634ccf 1841 if (skip_prologue_function (gdbarch, dest, 0))
0d39a070
DJ
1842 continue;
1843 else
1844 break;
1845 }
d4473757 1846 else if ((insn & 0xf0000000) != 0xe0000000)
0963b4bd 1847 break; /* Condition not true, exit early. */
0d39a070
DJ
1848 else if (arm_instruction_changes_pc (insn))
1849 /* Don't scan past anything that might change control flow. */
1850 break;
d19f7eee
UW
1851 else if ((insn & 0xfe500000) == 0xe8100000 /* ldm */
1852 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1853 /* Ignore block loads from the stack, potentially copying
1854 parameters from memory. */
1855 continue;
1856 else if ((insn & 0xfc500000) == 0xe4100000
1857 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1858 /* Similarly ignore single loads from the stack. */
1859 continue;
0d39a070
DJ
1860 else if ((insn & 0xffff0ff0) == 0xe1a00000)
1861 /* MOV Rd, Rm. Skip register copies, i.e. saves to another
1862 register instead of the stack. */
d4473757 1863 continue;
0d39a070
DJ
1864 else
1865 {
1866 /* The optimizer might shove anything into the prologue,
1867 so we just skip what we don't recognize. */
1868 unrecognized_pc = current_pc;
1869 continue;
1870 }
c906108c
SS
1871 }
1872
0d39a070
DJ
1873 if (unrecognized_pc == 0)
1874 unrecognized_pc = current_pc;
1875
4be43953
DJ
1876 /* The frame size is just the distance from the frame register
1877 to the original stack pointer. */
1878 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1879 {
1880 /* Frame pointer is fp. */
0d39a070
DJ
1881 framereg = ARM_FP_REGNUM;
1882 framesize = -regs[ARM_FP_REGNUM].k;
4be43953
DJ
1883 }
1884 else if (pv_is_register (regs[ARM_SP_REGNUM], ARM_SP_REGNUM))
1885 {
1886 /* Try the stack pointer... this is a bit desperate. */
0d39a070
DJ
1887 framereg = ARM_SP_REGNUM;
1888 framesize = -regs[ARM_SP_REGNUM].k;
4be43953 1889 }
d4473757 1890 else
4be43953
DJ
1891 {
1892 /* We're just out of luck. We don't know where the frame is. */
0d39a070
DJ
1893 framereg = -1;
1894 framesize = 0;
4be43953
DJ
1895 }
1896
0d39a070
DJ
1897 if (cache)
1898 {
1899 cache->framereg = framereg;
1900 cache->framesize = framesize;
1901
1902 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1903 if (pv_area_find_reg (stack, gdbarch, regno, &offset))
1904 cache->saved_regs[regno].addr = offset;
1905 }
1906
1907 if (arm_debug)
1908 fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1909 paddress (gdbarch, unrecognized_pc));
4be43953
DJ
1910
1911 do_cleanups (back_to);
0d39a070
DJ
1912 return unrecognized_pc;
1913}
1914
1915static void
1916arm_scan_prologue (struct frame_info *this_frame,
1917 struct arm_prologue_cache *cache)
1918{
1919 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1920 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1921 int regno;
1922 CORE_ADDR prologue_start, prologue_end, current_pc;
1923 CORE_ADDR prev_pc = get_frame_pc (this_frame);
1924 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1925 pv_t regs[ARM_FPS_REGNUM];
1926 struct pv_area *stack;
1927 struct cleanup *back_to;
1928 CORE_ADDR offset;
1929
1930 /* Assume there is no frame until proven otherwise. */
1931 cache->framereg = ARM_SP_REGNUM;
1932 cache->framesize = 0;
1933
1934 /* Check for Thumb prologue. */
1935 if (arm_frame_is_thumb (this_frame))
1936 {
1937 thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
1938 return;
1939 }
1940
1941 /* Find the function prologue. If we can't find the function in
1942 the symbol table, peek in the stack frame to find the PC. */
1943 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1944 &prologue_end))
1945 {
1946 /* One way to find the end of the prologue (which works well
1947 for unoptimized code) is to do the following:
1948
1949 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
1950
1951 if (sal.line == 0)
1952 prologue_end = prev_pc;
1953 else if (sal.end < prologue_end)
1954 prologue_end = sal.end;
1955
1956 This mechanism is very accurate so long as the optimizer
1957 doesn't move any instructions from the function body into the
1958 prologue. If this happens, sal.end will be the last
1959 instruction in the first hunk of prologue code just before
1960 the first instruction that the scheduler has moved from
1961 the body to the prologue.
1962
1963 In order to make sure that we scan all of the prologue
1964 instructions, we use a slightly less accurate mechanism which
1965 may scan more than necessary. To help compensate for this
1966 lack of accuracy, the prologue scanning loop below contains
1967 several clauses which'll cause the loop to terminate early if
1968 an implausible prologue instruction is encountered.
1969
1970 The expression
1971
1972 prologue_start + 64
1973
1974 is a suitable endpoint since it accounts for the largest
1975 possible prologue plus up to five instructions inserted by
1976 the scheduler. */
1977
1978 if (prologue_end > prologue_start + 64)
1979 {
1980 prologue_end = prologue_start + 64; /* See above. */
1981 }
1982 }
1983 else
1984 {
1985 /* We have no symbol information. Our only option is to assume this
1986 function has a standard stack frame and the normal frame register.
1987 Then, we can find the value of our frame pointer on entrance to
1988 the callee (or at the present moment if this is the innermost frame).
1989 The value stored there should be the address of the stmfd + 8. */
1990 CORE_ADDR frame_loc;
1991 LONGEST return_value;
1992
1993 frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
1994 if (!safe_read_memory_integer (frame_loc, 4, byte_order, &return_value))
1995 return;
1996 else
1997 {
1998 prologue_start = gdbarch_addr_bits_remove
1999 (gdbarch, return_value) - 8;
2000 prologue_end = prologue_start + 64; /* See above. */
2001 }
2002 }
2003
2004 if (prev_pc < prologue_end)
2005 prologue_end = prev_pc;
2006
2007 arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
2008}
2009
eb5492fa 2010static struct arm_prologue_cache *
a262aec2 2011arm_make_prologue_cache (struct frame_info *this_frame)
c906108c 2012{
eb5492fa
DJ
2013 int reg;
2014 struct arm_prologue_cache *cache;
2015 CORE_ADDR unwound_fp;
c5aa993b 2016
35d5d4ee 2017 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
a262aec2 2018 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
c906108c 2019
a262aec2 2020 arm_scan_prologue (this_frame, cache);
848cfffb 2021
a262aec2 2022 unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
eb5492fa
DJ
2023 if (unwound_fp == 0)
2024 return cache;
c906108c 2025
4be43953 2026 cache->prev_sp = unwound_fp + cache->framesize;
c906108c 2027
eb5492fa
DJ
2028 /* Calculate actual addresses of saved registers using offsets
2029 determined by arm_scan_prologue. */
a262aec2 2030 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
e28a332c 2031 if (trad_frame_addr_p (cache->saved_regs, reg))
eb5492fa
DJ
2032 cache->saved_regs[reg].addr += cache->prev_sp;
2033
2034 return cache;
c906108c
SS
2035}
2036
eb5492fa
DJ
2037/* Our frame ID for a normal frame is the current function's starting PC
2038 and the caller's SP when we were called. */
c906108c 2039
148754e5 2040static void
a262aec2 2041arm_prologue_this_id (struct frame_info *this_frame,
eb5492fa
DJ
2042 void **this_cache,
2043 struct frame_id *this_id)
c906108c 2044{
eb5492fa
DJ
2045 struct arm_prologue_cache *cache;
2046 struct frame_id id;
2c404490 2047 CORE_ADDR pc, func;
f079148d 2048
eb5492fa 2049 if (*this_cache == NULL)
a262aec2 2050 *this_cache = arm_make_prologue_cache (this_frame);
eb5492fa 2051 cache = *this_cache;
2a451106 2052
2c404490
DJ
2053 /* This is meant to halt the backtrace at "_start". */
2054 pc = get_frame_pc (this_frame);
2055 if (pc <= gdbarch_tdep (get_frame_arch (this_frame))->lowest_pc)
eb5492fa 2056 return;
5a203e44 2057
eb5492fa
DJ
2058 /* If we've hit a wall, stop. */
2059 if (cache->prev_sp == 0)
2060 return;
24de872b 2061
0e9e9abd
UW
2062 /* Use function start address as part of the frame ID. If we cannot
2063 identify the start address (due to missing symbol information),
2064 fall back to just using the current PC. */
2c404490 2065 func = get_frame_func (this_frame);
0e9e9abd
UW
2066 if (!func)
2067 func = pc;
2068
eb5492fa 2069 id = frame_id_build (cache->prev_sp, func);
eb5492fa 2070 *this_id = id;
c906108c
SS
2071}
2072
a262aec2
DJ
2073static struct value *
2074arm_prologue_prev_register (struct frame_info *this_frame,
eb5492fa 2075 void **this_cache,
a262aec2 2076 int prev_regnum)
24de872b 2077{
24568a2c 2078 struct gdbarch *gdbarch = get_frame_arch (this_frame);
24de872b
DJ
2079 struct arm_prologue_cache *cache;
2080
eb5492fa 2081 if (*this_cache == NULL)
a262aec2 2082 *this_cache = arm_make_prologue_cache (this_frame);
eb5492fa 2083 cache = *this_cache;
24de872b 2084
eb5492fa 2085 /* If we are asked to unwind the PC, then we need to return the LR
b39cc962
DJ
2086 instead. The prologue may save PC, but it will point into this
2087 frame's prologue, not the next frame's resume location. Also
2088 strip the saved T bit. A valid LR may have the low bit set, but
2089 a valid PC never does. */
eb5492fa 2090 if (prev_regnum == ARM_PC_REGNUM)
b39cc962
DJ
2091 {
2092 CORE_ADDR lr;
2093
2094 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2095 return frame_unwind_got_constant (this_frame, prev_regnum,
24568a2c 2096 arm_addr_bits_remove (gdbarch, lr));
b39cc962 2097 }
24de872b 2098
eb5492fa 2099 /* SP is generally not saved to the stack, but this frame is
a262aec2 2100 identified by the next frame's stack pointer at the time of the call.
eb5492fa
DJ
2101 The value was already reconstructed into PREV_SP. */
2102 if (prev_regnum == ARM_SP_REGNUM)
a262aec2 2103 return frame_unwind_got_constant (this_frame, prev_regnum, cache->prev_sp);
eb5492fa 2104
b39cc962
DJ
2105 /* The CPSR may have been changed by the call instruction and by the
2106 called function. The only bit we can reconstruct is the T bit,
2107 by checking the low bit of LR as of the call. This is a reliable
2108 indicator of Thumb-ness except for some ARM v4T pre-interworking
2109 Thumb code, which could get away with a clear low bit as long as
2110 the called function did not use bx. Guess that all other
2111 bits are unchanged; the condition flags are presumably lost,
2112 but the processor status is likely valid. */
2113 if (prev_regnum == ARM_PS_REGNUM)
2114 {
2115 CORE_ADDR lr, cpsr;
9779414d 2116 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
b39cc962
DJ
2117
2118 cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
2119 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2120 if (IS_THUMB_ADDR (lr))
9779414d 2121 cpsr |= t_bit;
b39cc962 2122 else
9779414d 2123 cpsr &= ~t_bit;
b39cc962
DJ
2124 return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
2125 }
2126
a262aec2
DJ
2127 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
2128 prev_regnum);
eb5492fa
DJ
2129}
2130
2131struct frame_unwind arm_prologue_unwind = {
2132 NORMAL_FRAME,
8fbca658 2133 default_frame_unwind_stop_reason,
eb5492fa 2134 arm_prologue_this_id,
a262aec2
DJ
2135 arm_prologue_prev_register,
2136 NULL,
2137 default_frame_sniffer
eb5492fa
DJ
2138};
2139
0e9e9abd
UW
2140/* Maintain a list of ARM exception table entries per objfile, similar to the
2141 list of mapping symbols. We only cache entries for standard ARM-defined
2142 personality routines; the cache will contain only the frame unwinding
2143 instructions associated with the entry (not the descriptors). */
2144
2145static const struct objfile_data *arm_exidx_data_key;
2146
2147struct arm_exidx_entry
2148{
2149 bfd_vma addr;
2150 gdb_byte *entry;
2151};
2152typedef struct arm_exidx_entry arm_exidx_entry_s;
2153DEF_VEC_O(arm_exidx_entry_s);
2154
2155struct arm_exidx_data
2156{
2157 VEC(arm_exidx_entry_s) **section_maps;
2158};
2159
2160static void
2161arm_exidx_data_free (struct objfile *objfile, void *arg)
2162{
2163 struct arm_exidx_data *data = arg;
2164 unsigned int i;
2165
2166 for (i = 0; i < objfile->obfd->section_count; i++)
2167 VEC_free (arm_exidx_entry_s, data->section_maps[i]);
2168}
2169
2170static inline int
2171arm_compare_exidx_entries (const struct arm_exidx_entry *lhs,
2172 const struct arm_exidx_entry *rhs)
2173{
2174 return lhs->addr < rhs->addr;
2175}
2176
2177static struct obj_section *
2178arm_obj_section_from_vma (struct objfile *objfile, bfd_vma vma)
2179{
2180 struct obj_section *osect;
2181
2182 ALL_OBJFILE_OSECTIONS (objfile, osect)
2183 if (bfd_get_section_flags (objfile->obfd,
2184 osect->the_bfd_section) & SEC_ALLOC)
2185 {
2186 bfd_vma start, size;
2187 start = bfd_get_section_vma (objfile->obfd, osect->the_bfd_section);
2188 size = bfd_get_section_size (osect->the_bfd_section);
2189
2190 if (start <= vma && vma < start + size)
2191 return osect;
2192 }
2193
2194 return NULL;
2195}
2196
2197/* Parse contents of exception table and exception index sections
2198 of OBJFILE, and fill in the exception table entry cache.
2199
2200 For each entry that refers to a standard ARM-defined personality
2201 routine, extract the frame unwinding instructions (from either
2202 the index or the table section). The unwinding instructions
2203 are normalized by:
2204 - extracting them from the rest of the table data
2205 - converting to host endianness
2206 - appending the implicit 0xb0 ("Finish") code
2207
2208 The extracted and normalized instructions are stored for later
2209 retrieval by the arm_find_exidx_entry routine. */
2210
2211static void
2212arm_exidx_new_objfile (struct objfile *objfile)
2213{
3bb47e8b 2214 struct cleanup *cleanups;
0e9e9abd
UW
2215 struct arm_exidx_data *data;
2216 asection *exidx, *extab;
2217 bfd_vma exidx_vma = 0, extab_vma = 0;
2218 bfd_size_type exidx_size = 0, extab_size = 0;
2219 gdb_byte *exidx_data = NULL, *extab_data = NULL;
2220 LONGEST i;
2221
2222 /* If we've already touched this file, do nothing. */
2223 if (!objfile || objfile_data (objfile, arm_exidx_data_key) != NULL)
2224 return;
3bb47e8b 2225 cleanups = make_cleanup (null_cleanup, NULL);
0e9e9abd
UW
2226
2227 /* Read contents of exception table and index. */
2228 exidx = bfd_get_section_by_name (objfile->obfd, ".ARM.exidx");
2229 if (exidx)
2230 {
2231 exidx_vma = bfd_section_vma (objfile->obfd, exidx);
2232 exidx_size = bfd_get_section_size (exidx);
2233 exidx_data = xmalloc (exidx_size);
2234 make_cleanup (xfree, exidx_data);
2235
2236 if (!bfd_get_section_contents (objfile->obfd, exidx,
2237 exidx_data, 0, exidx_size))
2238 {
2239 do_cleanups (cleanups);
2240 return;
2241 }
2242 }
2243
2244 extab = bfd_get_section_by_name (objfile->obfd, ".ARM.extab");
2245 if (extab)
2246 {
2247 extab_vma = bfd_section_vma (objfile->obfd, extab);
2248 extab_size = bfd_get_section_size (extab);
2249 extab_data = xmalloc (extab_size);
2250 make_cleanup (xfree, extab_data);
2251
2252 if (!bfd_get_section_contents (objfile->obfd, extab,
2253 extab_data, 0, extab_size))
2254 {
2255 do_cleanups (cleanups);
2256 return;
2257 }
2258 }
2259
2260 /* Allocate exception table data structure. */
2261 data = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct arm_exidx_data);
2262 set_objfile_data (objfile, arm_exidx_data_key, data);
2263 data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
2264 objfile->obfd->section_count,
2265 VEC(arm_exidx_entry_s) *);
2266
2267 /* Fill in exception table. */
2268 for (i = 0; i < exidx_size / 8; i++)
2269 {
2270 struct arm_exidx_entry new_exidx_entry;
2271 bfd_vma idx = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8);
2272 bfd_vma val = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8 + 4);
2273 bfd_vma addr = 0, word = 0;
2274 int n_bytes = 0, n_words = 0;
2275 struct obj_section *sec;
2276 gdb_byte *entry = NULL;
2277
2278 /* Extract address of start of function. */
2279 idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2280 idx += exidx_vma + i * 8;
2281
2282 /* Find section containing function and compute section offset. */
2283 sec = arm_obj_section_from_vma (objfile, idx);
2284 if (sec == NULL)
2285 continue;
2286 idx -= bfd_get_section_vma (objfile->obfd, sec->the_bfd_section);
2287
2288 /* Determine address of exception table entry. */
2289 if (val == 1)
2290 {
2291 /* EXIDX_CANTUNWIND -- no exception table entry present. */
2292 }
2293 else if ((val & 0xff000000) == 0x80000000)
2294 {
2295 /* Exception table entry embedded in .ARM.exidx
2296 -- must be short form. */
2297 word = val;
2298 n_bytes = 3;
2299 }
2300 else if (!(val & 0x80000000))
2301 {
2302 /* Exception table entry in .ARM.extab. */
2303 addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2304 addr += exidx_vma + i * 8 + 4;
2305
2306 if (addr >= extab_vma && addr + 4 <= extab_vma + extab_size)
2307 {
2308 word = bfd_h_get_32 (objfile->obfd,
2309 extab_data + addr - extab_vma);
2310 addr += 4;
2311
2312 if ((word & 0xff000000) == 0x80000000)
2313 {
2314 /* Short form. */
2315 n_bytes = 3;
2316 }
2317 else if ((word & 0xff000000) == 0x81000000
2318 || (word & 0xff000000) == 0x82000000)
2319 {
2320 /* Long form. */
2321 n_bytes = 2;
2322 n_words = ((word >> 16) & 0xff);
2323 }
2324 else if (!(word & 0x80000000))
2325 {
2326 bfd_vma pers;
2327 struct obj_section *pers_sec;
2328 int gnu_personality = 0;
2329
2330 /* Custom personality routine. */
2331 pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2332 pers = UNMAKE_THUMB_ADDR (pers + addr - 4);
2333
2334 /* Check whether we've got one of the variants of the
2335 GNU personality routines. */
2336 pers_sec = arm_obj_section_from_vma (objfile, pers);
2337 if (pers_sec)
2338 {
2339 static const char *personality[] =
2340 {
2341 "__gcc_personality_v0",
2342 "__gxx_personality_v0",
2343 "__gcj_personality_v0",
2344 "__gnu_objc_personality_v0",
2345 NULL
2346 };
2347
2348 CORE_ADDR pc = pers + obj_section_offset (pers_sec);
2349 int k;
2350
2351 for (k = 0; personality[k]; k++)
2352 if (lookup_minimal_symbol_by_pc_name
2353 (pc, personality[k], objfile))
2354 {
2355 gnu_personality = 1;
2356 break;
2357 }
2358 }
2359
2360 /* If so, the next word contains a word count in the high
2361 byte, followed by the same unwind instructions as the
2362 pre-defined forms. */
2363 if (gnu_personality
2364 && addr + 4 <= extab_vma + extab_size)
2365 {
2366 word = bfd_h_get_32 (objfile->obfd,
2367 extab_data + addr - extab_vma);
2368 addr += 4;
2369 n_bytes = 3;
2370 n_words = ((word >> 24) & 0xff);
2371 }
2372 }
2373 }
2374 }
2375
2376 /* Sanity check address. */
2377 if (n_words)
2378 if (addr < extab_vma || addr + 4 * n_words > extab_vma + extab_size)
2379 n_words = n_bytes = 0;
2380
2381 /* The unwind instructions reside in WORD (only the N_BYTES least
2382 significant bytes are valid), followed by N_WORDS words in the
2383 extab section starting at ADDR. */
2384 if (n_bytes || n_words)
2385 {
2386 gdb_byte *p = entry = obstack_alloc (&objfile->objfile_obstack,
2387 n_bytes + n_words * 4 + 1);
2388
2389 while (n_bytes--)
2390 *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);
2391
2392 while (n_words--)
2393 {
2394 word = bfd_h_get_32 (objfile->obfd,
2395 extab_data + addr - extab_vma);
2396 addr += 4;
2397
2398 *p++ = (gdb_byte) ((word >> 24) & 0xff);
2399 *p++ = (gdb_byte) ((word >> 16) & 0xff);
2400 *p++ = (gdb_byte) ((word >> 8) & 0xff);
2401 *p++ = (gdb_byte) (word & 0xff);
2402 }
2403
2404 /* Implied "Finish" to terminate the list. */
2405 *p++ = 0xb0;
2406 }
2407
2408 /* Push entry onto vector. They are guaranteed to always
2409 appear in order of increasing addresses. */
2410 new_exidx_entry.addr = idx;
2411 new_exidx_entry.entry = entry;
2412 VEC_safe_push (arm_exidx_entry_s,
2413 data->section_maps[sec->the_bfd_section->index],
2414 &new_exidx_entry);
2415 }
2416
2417 do_cleanups (cleanups);
2418}
2419
2420/* Search for the exception table entry covering MEMADDR. If one is found,
2421 return a pointer to its data. Otherwise, return 0. If START is non-NULL,
2422 set *START to the start of the region covered by this entry. */
2423
2424static gdb_byte *
2425arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
2426{
2427 struct obj_section *sec;
2428
2429 sec = find_pc_section (memaddr);
2430 if (sec != NULL)
2431 {
2432 struct arm_exidx_data *data;
2433 VEC(arm_exidx_entry_s) *map;
2434 struct arm_exidx_entry map_key = { memaddr - obj_section_addr (sec), 0 };
2435 unsigned int idx;
2436
2437 data = objfile_data (sec->objfile, arm_exidx_data_key);
2438 if (data != NULL)
2439 {
2440 map = data->section_maps[sec->the_bfd_section->index];
2441 if (!VEC_empty (arm_exidx_entry_s, map))
2442 {
2443 struct arm_exidx_entry *map_sym;
2444
2445 idx = VEC_lower_bound (arm_exidx_entry_s, map, &map_key,
2446 arm_compare_exidx_entries);
2447
2448 /* VEC_lower_bound finds the earliest ordered insertion
2449 point. If the following symbol starts at this exact
2450 address, we use that; otherwise, the preceding
2451 exception table entry covers this address. */
2452 if (idx < VEC_length (arm_exidx_entry_s, map))
2453 {
2454 map_sym = VEC_index (arm_exidx_entry_s, map, idx);
2455 if (map_sym->addr == map_key.addr)
2456 {
2457 if (start)
2458 *start = map_sym->addr + obj_section_addr (sec);
2459 return map_sym->entry;
2460 }
2461 }
2462
2463 if (idx > 0)
2464 {
2465 map_sym = VEC_index (arm_exidx_entry_s, map, idx - 1);
2466 if (start)
2467 *start = map_sym->addr + obj_section_addr (sec);
2468 return map_sym->entry;
2469 }
2470 }
2471 }
2472 }
2473
2474 return NULL;
2475}
2476
2477/* Given the current frame THIS_FRAME, and its associated frame unwinding
2478 instruction list from the ARM exception table entry ENTRY, allocate and
2479 return a prologue cache structure describing how to unwind this frame.
2480
2481 Return NULL if the unwinding instruction list contains a "spare",
2482 "reserved" or "refuse to unwind" instruction as defined in section
2483 "9.3 Frame unwinding instructions" of the "Exception Handling ABI
2484 for the ARM Architecture" document. */
2485
2486static struct arm_prologue_cache *
2487arm_exidx_fill_cache (struct frame_info *this_frame, gdb_byte *entry)
2488{
2489 CORE_ADDR vsp = 0;
2490 int vsp_valid = 0;
2491
2492 struct arm_prologue_cache *cache;
2493 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2494 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2495
2496 for (;;)
2497 {
2498 gdb_byte insn;
2499
2500 /* Whenever we reload SP, we actually have to retrieve its
2501 actual value in the current frame. */
2502 if (!vsp_valid)
2503 {
2504 if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2505 {
2506 int reg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2507 vsp = get_frame_register_unsigned (this_frame, reg);
2508 }
2509 else
2510 {
2511 CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr;
2512 vsp = get_frame_memory_unsigned (this_frame, addr, 4);
2513 }
2514
2515 vsp_valid = 1;
2516 }
2517
2518 /* Decode next unwind instruction. */
2519 insn = *entry++;
2520
2521 if ((insn & 0xc0) == 0)
2522 {
2523 int offset = insn & 0x3f;
2524 vsp += (offset << 2) + 4;
2525 }
2526 else if ((insn & 0xc0) == 0x40)
2527 {
2528 int offset = insn & 0x3f;
2529 vsp -= (offset << 2) + 4;
2530 }
2531 else if ((insn & 0xf0) == 0x80)
2532 {
2533 int mask = ((insn & 0xf) << 8) | *entry++;
2534 int i;
2535
2536 /* The special case of an all-zero mask identifies
2537 "Refuse to unwind". We return NULL to fall back
2538 to the prologue analyzer. */
2539 if (mask == 0)
2540 return NULL;
2541
2542 /* Pop registers r4..r15 under mask. */
2543 for (i = 0; i < 12; i++)
2544 if (mask & (1 << i))
2545 {
2546 cache->saved_regs[4 + i].addr = vsp;
2547 vsp += 4;
2548 }
2549
2550 /* Special-case popping SP -- we need to reload vsp. */
2551 if (mask & (1 << (ARM_SP_REGNUM - 4)))
2552 vsp_valid = 0;
2553 }
2554 else if ((insn & 0xf0) == 0x90)
2555 {
2556 int reg = insn & 0xf;
2557
2558 /* Reserved cases. */
2559 if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
2560 return NULL;
2561
2562 /* Set SP from another register and mark VSP for reload. */
2563 cache->saved_regs[ARM_SP_REGNUM] = cache->saved_regs[reg];
2564 vsp_valid = 0;
2565 }
2566 else if ((insn & 0xf0) == 0xa0)
2567 {
2568 int count = insn & 0x7;
2569 int pop_lr = (insn & 0x8) != 0;
2570 int i;
2571
2572 /* Pop r4..r[4+count]. */
2573 for (i = 0; i <= count; i++)
2574 {
2575 cache->saved_regs[4 + i].addr = vsp;
2576 vsp += 4;
2577 }
2578
2579 /* If indicated by flag, pop LR as well. */
2580 if (pop_lr)
2581 {
2582 cache->saved_regs[ARM_LR_REGNUM].addr = vsp;
2583 vsp += 4;
2584 }
2585 }
2586 else if (insn == 0xb0)
2587 {
2588 /* We could only have updated PC by popping into it; if so, it
2589 will show up as address. Otherwise, copy LR into PC. */
2590 if (!trad_frame_addr_p (cache->saved_regs, ARM_PC_REGNUM))
2591 cache->saved_regs[ARM_PC_REGNUM]
2592 = cache->saved_regs[ARM_LR_REGNUM];
2593
2594 /* We're done. */
2595 break;
2596 }
2597 else if (insn == 0xb1)
2598 {
2599 int mask = *entry++;
2600 int i;
2601
2602 /* All-zero mask and mask >= 16 is "spare". */
2603 if (mask == 0 || mask >= 16)
2604 return NULL;
2605
2606 /* Pop r0..r3 under mask. */
2607 for (i = 0; i < 4; i++)
2608 if (mask & (1 << i))
2609 {
2610 cache->saved_regs[i].addr = vsp;
2611 vsp += 4;
2612 }
2613 }
2614 else if (insn == 0xb2)
2615 {
2616 ULONGEST offset = 0;
2617 unsigned shift = 0;
2618
2619 do
2620 {
2621 offset |= (*entry & 0x7f) << shift;
2622 shift += 7;
2623 }
2624 while (*entry++ & 0x80);
2625
2626 vsp += 0x204 + (offset << 2);
2627 }
2628 else if (insn == 0xb3)
2629 {
2630 int start = *entry >> 4;
2631 int count = (*entry++) & 0xf;
2632 int i;
2633
2634 /* Only registers D0..D15 are valid here. */
2635 if (start + count >= 16)
2636 return NULL;
2637
2638 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2639 for (i = 0; i <= count; i++)
2640 {
2641 cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2642 vsp += 8;
2643 }
2644
2645 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2646 vsp += 4;
2647 }
2648 else if ((insn & 0xf8) == 0xb8)
2649 {
2650 int count = insn & 0x7;
2651 int i;
2652
2653 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2654 for (i = 0; i <= count; i++)
2655 {
2656 cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2657 vsp += 8;
2658 }
2659
2660 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2661 vsp += 4;
2662 }
2663 else if (insn == 0xc6)
2664 {
2665 int start = *entry >> 4;
2666 int count = (*entry++) & 0xf;
2667 int i;
2668
2669 /* Only registers WR0..WR15 are valid. */
2670 if (start + count >= 16)
2671 return NULL;
2672
2673 /* Pop iwmmx registers WR[start]..WR[start+count]. */
2674 for (i = 0; i <= count; i++)
2675 {
2676 cache->saved_regs[ARM_WR0_REGNUM + start + i].addr = vsp;
2677 vsp += 8;
2678 }
2679 }
2680 else if (insn == 0xc7)
2681 {
2682 int mask = *entry++;
2683 int i;
2684
2685 /* All-zero mask and mask >= 16 is "spare". */
2686 if (mask == 0 || mask >= 16)
2687 return NULL;
2688
2689 /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask. */
2690 for (i = 0; i < 4; i++)
2691 if (mask & (1 << i))
2692 {
2693 cache->saved_regs[ARM_WCGR0_REGNUM + i].addr = vsp;
2694 vsp += 4;
2695 }
2696 }
2697 else if ((insn & 0xf8) == 0xc0)
2698 {
2699 int count = insn & 0x7;
2700 int i;
2701
2702 /* Pop iwmmx registers WR[10]..WR[10+count]. */
2703 for (i = 0; i <= count; i++)
2704 {
2705 cache->saved_regs[ARM_WR0_REGNUM + 10 + i].addr = vsp;
2706 vsp += 8;
2707 }
2708 }
2709 else if (insn == 0xc8)
2710 {
2711 int start = *entry >> 4;
2712 int count = (*entry++) & 0xf;
2713 int i;
2714
2715 /* Only registers D0..D31 are valid. */
2716 if (start + count >= 16)
2717 return NULL;
2718
2719 /* Pop VFP double-precision registers
2720 D[16+start]..D[16+start+count]. */
2721 for (i = 0; i <= count; i++)
2722 {
2723 cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].addr = vsp;
2724 vsp += 8;
2725 }
2726 }
2727 else if (insn == 0xc9)
2728 {
2729 int start = *entry >> 4;
2730 int count = (*entry++) & 0xf;
2731 int i;
2732
2733 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2734 for (i = 0; i <= count; i++)
2735 {
2736 cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2737 vsp += 8;
2738 }
2739 }
2740 else if ((insn & 0xf8) == 0xd0)
2741 {
2742 int count = insn & 0x7;
2743 int i;
2744
2745 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2746 for (i = 0; i <= count; i++)
2747 {
2748 cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2749 vsp += 8;
2750 }
2751 }
2752 else
2753 {
2754 /* Everything else is "spare". */
2755 return NULL;
2756 }
2757 }
2758
2759 /* If we restore SP from a register, assume this was the frame register.
2760 Otherwise just fall back to SP as frame register. */
2761 if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2762 cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2763 else
2764 cache->framereg = ARM_SP_REGNUM;
2765
2766 /* Determine offset to previous frame. */
2767 cache->framesize
2768 = vsp - get_frame_register_unsigned (this_frame, cache->framereg);
2769
2770 /* We already got the previous SP. */
2771 cache->prev_sp = vsp;
2772
2773 return cache;
2774}
2775
2776/* Unwinding via ARM exception table entries. Note that the sniffer
2777 already computes a filled-in prologue cache, which is then used
2778 with the same arm_prologue_this_id and arm_prologue_prev_register
2779 routines also used for prologue-parsing based unwinding. */
2780
2781static int
2782arm_exidx_unwind_sniffer (const struct frame_unwind *self,
2783 struct frame_info *this_frame,
2784 void **this_prologue_cache)
2785{
2786 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2787 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
2788 CORE_ADDR addr_in_block, exidx_region, func_start;
2789 struct arm_prologue_cache *cache;
2790 gdb_byte *entry;
2791
2792 /* See if we have an ARM exception table entry covering this address. */
2793 addr_in_block = get_frame_address_in_block (this_frame);
2794 entry = arm_find_exidx_entry (addr_in_block, &exidx_region);
2795 if (!entry)
2796 return 0;
2797
2798 /* The ARM exception table does not describe unwind information
2799 for arbitrary PC values, but is guaranteed to be correct only
2800 at call sites. We have to decide here whether we want to use
2801 ARM exception table information for this frame, or fall back
2802 to using prologue parsing. (Note that if we have DWARF CFI,
2803 this sniffer isn't even called -- CFI is always preferred.)
2804
2805 Before we make this decision, however, we check whether we
2806 actually have *symbol* information for the current frame.
2807 If not, prologue parsing would not work anyway, so we might
2808 as well use the exception table and hope for the best. */
2809 if (find_pc_partial_function (addr_in_block, NULL, &func_start, NULL))
2810 {
2811 int exc_valid = 0;
2812
2813 /* If the next frame is "normal", we are at a call site in this
2814 frame, so exception information is guaranteed to be valid. */
2815 if (get_next_frame (this_frame)
2816 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
2817 exc_valid = 1;
2818
2819 /* We also assume exception information is valid if we're currently
2820 blocked in a system call. The system library is supposed to
2821 ensure this, so that e.g. pthread cancellation works. */
2822 if (arm_frame_is_thumb (this_frame))
2823 {
2824 LONGEST insn;
2825
2826 if (safe_read_memory_integer (get_frame_pc (this_frame) - 2, 2,
2827 byte_order_for_code, &insn)
2828 && (insn & 0xff00) == 0xdf00 /* svc */)
2829 exc_valid = 1;
2830 }
2831 else
2832 {
2833 LONGEST insn;
2834
2835 if (safe_read_memory_integer (get_frame_pc (this_frame) - 4, 4,
2836 byte_order_for_code, &insn)
2837 && (insn & 0x0f000000) == 0x0f000000 /* svc */)
2838 exc_valid = 1;
2839 }
2840
2841 /* Bail out if we don't know that exception information is valid. */
2842 if (!exc_valid)
2843 return 0;
2844
2845 /* The ARM exception index does not mark the *end* of the region
2846 covered by the entry, and some functions will not have any entry.
2847 To correctly recognize the end of the covered region, the linker
2848 should have inserted dummy records with a CANTUNWIND marker.
2849
2850 Unfortunately, current versions of GNU ld do not reliably do
2851 this, and thus we may have found an incorrect entry above.
2852 As a (temporary) sanity check, we only use the entry if it
2853 lies *within* the bounds of the function. Note that this check
2854 might reject perfectly valid entries that just happen to cover
2855 multiple functions; therefore this check ought to be removed
2856 once the linker is fixed. */
2857 if (func_start > exidx_region)
2858 return 0;
2859 }
2860
2861 /* Decode the list of unwinding instructions into a prologue cache.
2862 Note that this may fail due to e.g. a "refuse to unwind" code. */
2863 cache = arm_exidx_fill_cache (this_frame, entry);
2864 if (!cache)
2865 return 0;
2866
2867 *this_prologue_cache = cache;
2868 return 1;
2869}
2870
2871struct frame_unwind arm_exidx_unwind = {
2872 NORMAL_FRAME,
8fbca658 2873 default_frame_unwind_stop_reason,
0e9e9abd
UW
2874 arm_prologue_this_id,
2875 arm_prologue_prev_register,
2876 NULL,
2877 arm_exidx_unwind_sniffer
2878};
2879
909cf6ea 2880static struct arm_prologue_cache *
a262aec2 2881arm_make_stub_cache (struct frame_info *this_frame)
909cf6ea 2882{
909cf6ea 2883 struct arm_prologue_cache *cache;
909cf6ea 2884
35d5d4ee 2885 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
a262aec2 2886 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
909cf6ea 2887
a262aec2 2888 cache->prev_sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
909cf6ea
DJ
2889
2890 return cache;
2891}
2892
2893/* Our frame ID for a stub frame is the current SP and LR. */
2894
2895static void
a262aec2 2896arm_stub_this_id (struct frame_info *this_frame,
909cf6ea
DJ
2897 void **this_cache,
2898 struct frame_id *this_id)
2899{
2900 struct arm_prologue_cache *cache;
2901
2902 if (*this_cache == NULL)
a262aec2 2903 *this_cache = arm_make_stub_cache (this_frame);
909cf6ea
DJ
2904 cache = *this_cache;
2905
a262aec2 2906 *this_id = frame_id_build (cache->prev_sp, get_frame_pc (this_frame));
909cf6ea
DJ
2907}
2908
a262aec2
DJ
2909static int
2910arm_stub_unwind_sniffer (const struct frame_unwind *self,
2911 struct frame_info *this_frame,
2912 void **this_prologue_cache)
909cf6ea 2913{
93d42b30 2914 CORE_ADDR addr_in_block;
909cf6ea
DJ
2915 char dummy[4];
2916
a262aec2 2917 addr_in_block = get_frame_address_in_block (this_frame);
93d42b30 2918 if (in_plt_section (addr_in_block, NULL)
fc36e839
DE
2919 /* We also use the stub winder if the target memory is unreadable
2920 to avoid having the prologue unwinder trying to read it. */
a262aec2
DJ
2921 || target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
2922 return 1;
909cf6ea 2923
a262aec2 2924 return 0;
909cf6ea
DJ
2925}
2926
a262aec2
DJ
2927struct frame_unwind arm_stub_unwind = {
2928 NORMAL_FRAME,
8fbca658 2929 default_frame_unwind_stop_reason,
a262aec2
DJ
2930 arm_stub_this_id,
2931 arm_prologue_prev_register,
2932 NULL,
2933 arm_stub_unwind_sniffer
2934};
2935
24de872b 2936static CORE_ADDR
a262aec2 2937arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
24de872b
DJ
2938{
2939 struct arm_prologue_cache *cache;
2940
eb5492fa 2941 if (*this_cache == NULL)
a262aec2 2942 *this_cache = arm_make_prologue_cache (this_frame);
eb5492fa
DJ
2943 cache = *this_cache;
2944
4be43953 2945 return cache->prev_sp - cache->framesize;
24de872b
DJ
2946}
2947
eb5492fa
DJ
2948struct frame_base arm_normal_base = {
2949 &arm_prologue_unwind,
2950 arm_normal_frame_base,
2951 arm_normal_frame_base,
2952 arm_normal_frame_base
2953};
2954
a262aec2 2955/* Assuming THIS_FRAME is a dummy, return the frame ID of that
eb5492fa
DJ
2956 dummy frame. The frame ID's base needs to match the TOS value
2957 saved by save_dummy_frame_tos() and returned from
2958 arm_push_dummy_call, and the PC needs to match the dummy frame's
2959 breakpoint. */
c906108c 2960
eb5492fa 2961static struct frame_id
a262aec2 2962arm_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
c906108c 2963{
0963b4bd
MS
2964 return frame_id_build (get_frame_register_unsigned (this_frame,
2965 ARM_SP_REGNUM),
a262aec2 2966 get_frame_pc (this_frame));
eb5492fa 2967}
c3b4394c 2968
eb5492fa
DJ
2969/* Given THIS_FRAME, find the previous frame's resume PC (which will
2970 be used to construct the previous frame's ID, after looking up the
2971 containing function). */
c3b4394c 2972
eb5492fa
DJ
2973static CORE_ADDR
2974arm_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
2975{
2976 CORE_ADDR pc;
2977 pc = frame_unwind_register_unsigned (this_frame, ARM_PC_REGNUM);
24568a2c 2978 return arm_addr_bits_remove (gdbarch, pc);
eb5492fa
DJ
2979}
2980
2981static CORE_ADDR
2982arm_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
2983{
2984 return frame_unwind_register_unsigned (this_frame, ARM_SP_REGNUM);
c906108c
SS
2985}
2986
b39cc962
DJ
2987static struct value *
2988arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
2989 int regnum)
2990{
24568a2c 2991 struct gdbarch * gdbarch = get_frame_arch (this_frame);
b39cc962 2992 CORE_ADDR lr, cpsr;
9779414d 2993 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
b39cc962
DJ
2994
2995 switch (regnum)
2996 {
2997 case ARM_PC_REGNUM:
2998 /* The PC is normally copied from the return column, which
2999 describes saves of LR. However, that version may have an
3000 extra bit set to indicate Thumb state. The bit is not
3001 part of the PC. */
3002 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3003 return frame_unwind_got_constant (this_frame, regnum,
24568a2c 3004 arm_addr_bits_remove (gdbarch, lr));
b39cc962
DJ
3005
3006 case ARM_PS_REGNUM:
3007 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
ca38c58e 3008 cpsr = get_frame_register_unsigned (this_frame, regnum);
b39cc962
DJ
3009 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3010 if (IS_THUMB_ADDR (lr))
9779414d 3011 cpsr |= t_bit;
b39cc962 3012 else
9779414d 3013 cpsr &= ~t_bit;
ca38c58e 3014 return frame_unwind_got_constant (this_frame, regnum, cpsr);
b39cc962
DJ
3015
3016 default:
3017 internal_error (__FILE__, __LINE__,
3018 _("Unexpected register %d"), regnum);
3019 }
3020}
3021
3022static void
3023arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
3024 struct dwarf2_frame_state_reg *reg,
3025 struct frame_info *this_frame)
3026{
3027 switch (regnum)
3028 {
3029 case ARM_PC_REGNUM:
3030 case ARM_PS_REGNUM:
3031 reg->how = DWARF2_FRAME_REG_FN;
3032 reg->loc.fn = arm_dwarf2_prev_register;
3033 break;
3034 case ARM_SP_REGNUM:
3035 reg->how = DWARF2_FRAME_REG_CFA;
3036 break;
3037 }
3038}
3039
4024ca99
UW
3040/* Return true if we are in the function's epilogue, i.e. after the
3041 instruction that destroyed the function's stack frame. */
3042
3043static int
3044thumb_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3045{
3046 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3047 unsigned int insn, insn2;
3048 int found_return = 0, found_stack_adjust = 0;
3049 CORE_ADDR func_start, func_end;
3050 CORE_ADDR scan_pc;
3051 gdb_byte buf[4];
3052
3053 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3054 return 0;
3055
3056 /* The epilogue is a sequence of instructions along the following lines:
3057
3058 - add stack frame size to SP or FP
3059 - [if frame pointer used] restore SP from FP
3060 - restore registers from SP [may include PC]
3061 - a return-type instruction [if PC wasn't already restored]
3062
3063 In a first pass, we scan forward from the current PC and verify the
3064 instructions we find as compatible with this sequence, ending in a
3065 return instruction.
3066
3067 However, this is not sufficient to distinguish indirect function calls
3068 within a function from indirect tail calls in the epilogue in some cases.
3069 Therefore, if we didn't already find any SP-changing instruction during
3070 forward scan, we add a backward scanning heuristic to ensure we actually
3071 are in the epilogue. */
3072
3073 scan_pc = pc;
3074 while (scan_pc < func_end && !found_return)
3075 {
3076 if (target_read_memory (scan_pc, buf, 2))
3077 break;
3078
3079 scan_pc += 2;
3080 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3081
3082 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
3083 found_return = 1;
3084 else if (insn == 0x46f7) /* mov pc, lr */
3085 found_return = 1;
3086 else if (insn == 0x46bd) /* mov sp, r7 */
3087 found_stack_adjust = 1;
3088 else if ((insn & 0xff00) == 0xb000) /* add sp, imm or sub sp, imm */
3089 found_stack_adjust = 1;
3090 else if ((insn & 0xfe00) == 0xbc00) /* pop <registers> */
3091 {
3092 found_stack_adjust = 1;
3093 if (insn & 0x0100) /* <registers> include PC. */
3094 found_return = 1;
3095 }
3096 else if ((insn & 0xe000) == 0xe000) /* 32-bit Thumb-2 instruction */
3097 {
3098 if (target_read_memory (scan_pc, buf, 2))
3099 break;
3100
3101 scan_pc += 2;
3102 insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3103
3104 if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3105 {
3106 found_stack_adjust = 1;
3107 if (insn2 & 0x8000) /* <registers> include PC. */
3108 found_return = 1;
3109 }
3110 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3111 && (insn2 & 0x0fff) == 0x0b04)
3112 {
3113 found_stack_adjust = 1;
3114 if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC. */
3115 found_return = 1;
3116 }
3117 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3118 && (insn2 & 0x0e00) == 0x0a00)
3119 found_stack_adjust = 1;
3120 else
3121 break;
3122 }
3123 else
3124 break;
3125 }
3126
3127 if (!found_return)
3128 return 0;
3129
3130 /* Since any instruction in the epilogue sequence, with the possible
3131 exception of return itself, updates the stack pointer, we need to
3132 scan backwards for at most one instruction. Try either a 16-bit or
3133 a 32-bit instruction. This is just a heuristic, so we do not worry
0963b4bd 3134 too much about false positives. */
4024ca99
UW
3135
3136 if (!found_stack_adjust)
3137 {
3138 if (pc - 4 < func_start)
3139 return 0;
3140 if (target_read_memory (pc - 4, buf, 4))
3141 return 0;
3142
3143 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3144 insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);
3145
3146 if (insn2 == 0x46bd) /* mov sp, r7 */
3147 found_stack_adjust = 1;
3148 else if ((insn2 & 0xff00) == 0xb000) /* add sp, imm or sub sp, imm */
3149 found_stack_adjust = 1;
3150 else if ((insn2 & 0xff00) == 0xbc00) /* pop <registers> without PC */
3151 found_stack_adjust = 1;
3152 else if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3153 found_stack_adjust = 1;
3154 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3155 && (insn2 & 0x0fff) == 0x0b04)
3156 found_stack_adjust = 1;
3157 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3158 && (insn2 & 0x0e00) == 0x0a00)
3159 found_stack_adjust = 1;
3160 }
3161
3162 return found_stack_adjust;
3163}
3164
3165/* Return true if we are in the function's epilogue, i.e. after the
3166 instruction that destroyed the function's stack frame. */
3167
3168static int
3169arm_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3170{
3171 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3172 unsigned int insn;
3173 int found_return, found_stack_adjust;
3174 CORE_ADDR func_start, func_end;
3175
3176 if (arm_pc_is_thumb (gdbarch, pc))
3177 return thumb_in_function_epilogue_p (gdbarch, pc);
3178
3179 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3180 return 0;
3181
3182 /* We are in the epilogue if the previous instruction was a stack
3183 adjustment and the next instruction is a possible return (bx, mov
3184 pc, or pop). We could have to scan backwards to find the stack
3185 adjustment, or forwards to find the return, but this is a decent
3186 approximation. First scan forwards. */
3187
3188 found_return = 0;
3189 insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
3190 if (bits (insn, 28, 31) != INST_NV)
3191 {
3192 if ((insn & 0x0ffffff0) == 0x012fff10)
3193 /* BX. */
3194 found_return = 1;
3195 else if ((insn & 0x0ffffff0) == 0x01a0f000)
3196 /* MOV PC. */
3197 found_return = 1;
3198 else if ((insn & 0x0fff0000) == 0x08bd0000
3199 && (insn & 0x0000c000) != 0)
3200 /* POP (LDMIA), including PC or LR. */
3201 found_return = 1;
3202 }
3203
3204 if (!found_return)
3205 return 0;
3206
3207 /* Scan backwards. This is just a heuristic, so do not worry about
3208 false positives from mode changes. */
3209
3210 if (pc < func_start + 4)
3211 return 0;
3212
73c964d6 3213 found_stack_adjust = 0;
4024ca99
UW
3214 insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
3215 if (bits (insn, 28, 31) != INST_NV)
3216 {
3217 if ((insn & 0x0df0f000) == 0x0080d000)
3218 /* ADD SP (register or immediate). */
3219 found_stack_adjust = 1;
3220 else if ((insn & 0x0df0f000) == 0x0040d000)
3221 /* SUB SP (register or immediate). */
3222 found_stack_adjust = 1;
3223 else if ((insn & 0x0ffffff0) == 0x01a0d000)
3224 /* MOV SP. */
77bc0675 3225 found_stack_adjust = 1;
4024ca99
UW
3226 else if ((insn & 0x0fff0000) == 0x08bd0000)
3227 /* POP (LDMIA). */
3228 found_stack_adjust = 1;
3229 }
3230
3231 if (found_stack_adjust)
3232 return 1;
3233
3234 return 0;
3235}
3236
3237
2dd604e7
RE
3238/* When arguments must be pushed onto the stack, they go on in reverse
3239 order. The code below implements a FILO (stack) to do this. */
3240
3241struct stack_item
3242{
3243 int len;
3244 struct stack_item *prev;
3245 void *data;
3246};
3247
3248static struct stack_item *
8c6363cf 3249push_stack_item (struct stack_item *prev, const void *contents, int len)
2dd604e7
RE
3250{
3251 struct stack_item *si;
3252 si = xmalloc (sizeof (struct stack_item));
226c7fbc 3253 si->data = xmalloc (len);
2dd604e7
RE
3254 si->len = len;
3255 si->prev = prev;
3256 memcpy (si->data, contents, len);
3257 return si;
3258}
3259
3260static struct stack_item *
3261pop_stack_item (struct stack_item *si)
3262{
3263 struct stack_item *dead = si;
3264 si = si->prev;
3265 xfree (dead->data);
3266 xfree (dead);
3267 return si;
3268}
3269
2af48f68
PB
3270
3271/* Return the alignment (in bytes) of the given type. */
3272
3273static int
3274arm_type_align (struct type *t)
3275{
3276 int n;
3277 int align;
3278 int falign;
3279
3280 t = check_typedef (t);
3281 switch (TYPE_CODE (t))
3282 {
3283 default:
3284 /* Should never happen. */
3285 internal_error (__FILE__, __LINE__, _("unknown type alignment"));
3286 return 4;
3287
3288 case TYPE_CODE_PTR:
3289 case TYPE_CODE_ENUM:
3290 case TYPE_CODE_INT:
3291 case TYPE_CODE_FLT:
3292 case TYPE_CODE_SET:
3293 case TYPE_CODE_RANGE:
3294 case TYPE_CODE_BITSTRING:
3295 case TYPE_CODE_REF:
3296 case TYPE_CODE_CHAR:
3297 case TYPE_CODE_BOOL:
3298 return TYPE_LENGTH (t);
3299
3300 case TYPE_CODE_ARRAY:
3301 case TYPE_CODE_COMPLEX:
3302 /* TODO: What about vector types? */
3303 return arm_type_align (TYPE_TARGET_TYPE (t));
3304
3305 case TYPE_CODE_STRUCT:
3306 case TYPE_CODE_UNION:
3307 align = 1;
3308 for (n = 0; n < TYPE_NFIELDS (t); n++)
3309 {
3310 falign = arm_type_align (TYPE_FIELD_TYPE (t, n));
3311 if (falign > align)
3312 align = falign;
3313 }
3314 return align;
3315 }
3316}
3317
90445bd3
DJ
3318/* Possible base types for a candidate for passing and returning in
3319 VFP registers. */
3320
3321enum arm_vfp_cprc_base_type
3322{
3323 VFP_CPRC_UNKNOWN,
3324 VFP_CPRC_SINGLE,
3325 VFP_CPRC_DOUBLE,
3326 VFP_CPRC_VEC64,
3327 VFP_CPRC_VEC128
3328};
3329
3330/* The length of one element of base type B. */
3331
3332static unsigned
3333arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
3334{
3335 switch (b)
3336 {
3337 case VFP_CPRC_SINGLE:
3338 return 4;
3339 case VFP_CPRC_DOUBLE:
3340 return 8;
3341 case VFP_CPRC_VEC64:
3342 return 8;
3343 case VFP_CPRC_VEC128:
3344 return 16;
3345 default:
3346 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3347 (int) b);
3348 }
3349}
3350
3351/* The character ('s', 'd' or 'q') for the type of VFP register used
3352 for passing base type B. */
3353
3354static int
3355arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
3356{
3357 switch (b)
3358 {
3359 case VFP_CPRC_SINGLE:
3360 return 's';
3361 case VFP_CPRC_DOUBLE:
3362 return 'd';
3363 case VFP_CPRC_VEC64:
3364 return 'd';
3365 case VFP_CPRC_VEC128:
3366 return 'q';
3367 default:
3368 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3369 (int) b);
3370 }
3371}
3372
3373/* Determine whether T may be part of a candidate for passing and
3374 returning in VFP registers, ignoring the limit on the total number
3375 of components. If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
3376 classification of the first valid component found; if it is not
3377 VFP_CPRC_UNKNOWN, all components must have the same classification
3378 as *BASE_TYPE. If it is found that T contains a type not permitted
3379 for passing and returning in VFP registers, a type differently
3380 classified from *BASE_TYPE, or two types differently classified
3381 from each other, return -1, otherwise return the total number of
3382 base-type elements found (possibly 0 in an empty structure or
3383 array). Vectors and complex types are not currently supported,
3384 matching the generic AAPCS support. */
3385
3386static int
3387arm_vfp_cprc_sub_candidate (struct type *t,
3388 enum arm_vfp_cprc_base_type *base_type)
3389{
3390 t = check_typedef (t);
3391 switch (TYPE_CODE (t))
3392 {
3393 case TYPE_CODE_FLT:
3394 switch (TYPE_LENGTH (t))
3395 {
3396 case 4:
3397 if (*base_type == VFP_CPRC_UNKNOWN)
3398 *base_type = VFP_CPRC_SINGLE;
3399 else if (*base_type != VFP_CPRC_SINGLE)
3400 return -1;
3401 return 1;
3402
3403 case 8:
3404 if (*base_type == VFP_CPRC_UNKNOWN)
3405 *base_type = VFP_CPRC_DOUBLE;
3406 else if (*base_type != VFP_CPRC_DOUBLE)
3407 return -1;
3408 return 1;
3409
3410 default:
3411 return -1;
3412 }
3413 break;
3414
3415 case TYPE_CODE_ARRAY:
3416 {
3417 int count;
3418 unsigned unitlen;
3419 count = arm_vfp_cprc_sub_candidate (TYPE_TARGET_TYPE (t), base_type);
3420 if (count == -1)
3421 return -1;
3422 if (TYPE_LENGTH (t) == 0)
3423 {
3424 gdb_assert (count == 0);
3425 return 0;
3426 }
3427 else if (count == 0)
3428 return -1;
3429 unitlen = arm_vfp_cprc_unit_length (*base_type);
3430 gdb_assert ((TYPE_LENGTH (t) % unitlen) == 0);
3431 return TYPE_LENGTH (t) / unitlen;
3432 }
3433 break;
3434
3435 case TYPE_CODE_STRUCT:
3436 {
3437 int count = 0;
3438 unsigned unitlen;
3439 int i;
3440 for (i = 0; i < TYPE_NFIELDS (t); i++)
3441 {
3442 int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3443 base_type);
3444 if (sub_count == -1)
3445 return -1;
3446 count += sub_count;
3447 }
3448 if (TYPE_LENGTH (t) == 0)
3449 {
3450 gdb_assert (count == 0);
3451 return 0;
3452 }
3453 else if (count == 0)
3454 return -1;
3455 unitlen = arm_vfp_cprc_unit_length (*base_type);
3456 if (TYPE_LENGTH (t) != unitlen * count)
3457 return -1;
3458 return count;
3459 }
3460
3461 case TYPE_CODE_UNION:
3462 {
3463 int count = 0;
3464 unsigned unitlen;
3465 int i;
3466 for (i = 0; i < TYPE_NFIELDS (t); i++)
3467 {
3468 int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3469 base_type);
3470 if (sub_count == -1)
3471 return -1;
3472 count = (count > sub_count ? count : sub_count);
3473 }
3474 if (TYPE_LENGTH (t) == 0)
3475 {
3476 gdb_assert (count == 0);
3477 return 0;
3478 }
3479 else if (count == 0)
3480 return -1;
3481 unitlen = arm_vfp_cprc_unit_length (*base_type);
3482 if (TYPE_LENGTH (t) != unitlen * count)
3483 return -1;
3484 return count;
3485 }
3486
3487 default:
3488 break;
3489 }
3490
3491 return -1;
3492}
3493
3494/* Determine whether T is a VFP co-processor register candidate (CPRC)
3495 if passed to or returned from a non-variadic function with the VFP
3496 ABI in effect. Return 1 if it is, 0 otherwise. If it is, set
3497 *BASE_TYPE to the base type for T and *COUNT to the number of
3498 elements of that base type before returning. */
3499
3500static int
3501arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
3502 int *count)
3503{
3504 enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
3505 int c = arm_vfp_cprc_sub_candidate (t, &b);
3506 if (c <= 0 || c > 4)
3507 return 0;
3508 *base_type = b;
3509 *count = c;
3510 return 1;
3511}
3512
3513/* Return 1 if the VFP ABI should be used for passing arguments to and
3514 returning values from a function of type FUNC_TYPE, 0
3515 otherwise. */
3516
3517static int
3518arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
3519{
3520 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3521 /* Variadic functions always use the base ABI. Assume that functions
3522 without debug info are not variadic. */
3523 if (func_type && TYPE_VARARGS (check_typedef (func_type)))
3524 return 0;
3525 /* The VFP ABI is only supported as a variant of AAPCS. */
3526 if (tdep->arm_abi != ARM_ABI_AAPCS)
3527 return 0;
3528 return gdbarch_tdep (gdbarch)->fp_model == ARM_FLOAT_VFP;
3529}
3530
3531/* We currently only support passing parameters in integer registers, which
3532 conforms with GCC's default model, and VFP argument passing following
3533 the VFP variant of AAPCS. Several other variants exist and
2dd604e7
RE
3534 we should probably support some of them based on the selected ABI. */
3535
3536static CORE_ADDR
7d9b040b 3537arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6a65450a
AC
3538 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
3539 struct value **args, CORE_ADDR sp, int struct_return,
3540 CORE_ADDR struct_addr)
2dd604e7 3541{
e17a4113 3542 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2dd604e7
RE
3543 int argnum;
3544 int argreg;
3545 int nstack;
3546 struct stack_item *si = NULL;
90445bd3
DJ
3547 int use_vfp_abi;
3548 struct type *ftype;
3549 unsigned vfp_regs_free = (1 << 16) - 1;
3550
3551 /* Determine the type of this function and whether the VFP ABI
3552 applies. */
3553 ftype = check_typedef (value_type (function));
3554 if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
3555 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
3556 use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
2dd604e7 3557
6a65450a
AC
3558 /* Set the return address. For the ARM, the return breakpoint is
3559 always at BP_ADDR. */
9779414d 3560 if (arm_pc_is_thumb (gdbarch, bp_addr))
9dca5578 3561 bp_addr |= 1;
6a65450a 3562 regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
2dd604e7
RE
3563
3564 /* Walk through the list of args and determine how large a temporary
3565 stack is required. Need to take care here as structs may be
7a9dd1b2 3566 passed on the stack, and we have to push them. */
2dd604e7
RE
3567 nstack = 0;
3568
3569 argreg = ARM_A1_REGNUM;
3570 nstack = 0;
3571
2dd604e7
RE
3572 /* The struct_return pointer occupies the first parameter
3573 passing register. */
3574 if (struct_return)
3575 {
3576 if (arm_debug)
5af949e3 3577 fprintf_unfiltered (gdb_stdlog, "struct return in %s = %s\n",
2af46ca0 3578 gdbarch_register_name (gdbarch, argreg),
5af949e3 3579 paddress (gdbarch, struct_addr));
2dd604e7
RE
3580 regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
3581 argreg++;
3582 }
3583
3584 for (argnum = 0; argnum < nargs; argnum++)
3585 {
3586 int len;
3587 struct type *arg_type;
3588 struct type *target_type;
3589 enum type_code typecode;
8c6363cf 3590 const bfd_byte *val;
2af48f68 3591 int align;
90445bd3
DJ
3592 enum arm_vfp_cprc_base_type vfp_base_type;
3593 int vfp_base_count;
3594 int may_use_core_reg = 1;
2dd604e7 3595
df407dfe 3596 arg_type = check_typedef (value_type (args[argnum]));
2dd604e7
RE
3597 len = TYPE_LENGTH (arg_type);
3598 target_type = TYPE_TARGET_TYPE (arg_type);
3599 typecode = TYPE_CODE (arg_type);
8c6363cf 3600 val = value_contents (args[argnum]);
2dd604e7 3601
2af48f68
PB
3602 align = arm_type_align (arg_type);
3603 /* Round alignment up to a whole number of words. */
3604 align = (align + INT_REGISTER_SIZE - 1) & ~(INT_REGISTER_SIZE - 1);
3605 /* Different ABIs have different maximum alignments. */
3606 if (gdbarch_tdep (gdbarch)->arm_abi == ARM_ABI_APCS)
3607 {
3608 /* The APCS ABI only requires word alignment. */
3609 align = INT_REGISTER_SIZE;
3610 }
3611 else
3612 {
3613 /* The AAPCS requires at most doubleword alignment. */
3614 if (align > INT_REGISTER_SIZE * 2)
3615 align = INT_REGISTER_SIZE * 2;
3616 }
3617
90445bd3
DJ
3618 if (use_vfp_abi
3619 && arm_vfp_call_candidate (arg_type, &vfp_base_type,
3620 &vfp_base_count))
3621 {
3622 int regno;
3623 int unit_length;
3624 int shift;
3625 unsigned mask;
3626
3627 /* Because this is a CPRC it cannot go in a core register or
3628 cause a core register to be skipped for alignment.
3629 Either it goes in VFP registers and the rest of this loop
3630 iteration is skipped for this argument, or it goes on the
3631 stack (and the stack alignment code is correct for this
3632 case). */
3633 may_use_core_reg = 0;
3634
3635 unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
3636 shift = unit_length / 4;
3637 mask = (1 << (shift * vfp_base_count)) - 1;
3638 for (regno = 0; regno < 16; regno += shift)
3639 if (((vfp_regs_free >> regno) & mask) == mask)
3640 break;
3641
3642 if (regno < 16)
3643 {
3644 int reg_char;
3645 int reg_scaled;
3646 int i;
3647
3648 vfp_regs_free &= ~(mask << regno);
3649 reg_scaled = regno / shift;
3650 reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
3651 for (i = 0; i < vfp_base_count; i++)
3652 {
3653 char name_buf[4];
3654 int regnum;
58d6951d
DJ
3655 if (reg_char == 'q')
3656 arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
90445bd3 3657 val + i * unit_length);
58d6951d
DJ
3658 else
3659 {
3660 sprintf (name_buf, "%c%d", reg_char, reg_scaled + i);
3661 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
3662 strlen (name_buf));
3663 regcache_cooked_write (regcache, regnum,
3664 val + i * unit_length);
3665 }
90445bd3
DJ
3666 }
3667 continue;
3668 }
3669 else
3670 {
3671 /* This CPRC could not go in VFP registers, so all VFP
3672 registers are now marked as used. */
3673 vfp_regs_free = 0;
3674 }
3675 }
3676
2af48f68
PB
3677 /* Push stack padding for dowubleword alignment. */
3678 if (nstack & (align - 1))
3679 {
3680 si = push_stack_item (si, val, INT_REGISTER_SIZE);
3681 nstack += INT_REGISTER_SIZE;
3682 }
3683
3684 /* Doubleword aligned quantities must go in even register pairs. */
90445bd3
DJ
3685 if (may_use_core_reg
3686 && argreg <= ARM_LAST_ARG_REGNUM
2af48f68
PB
3687 && align > INT_REGISTER_SIZE
3688 && argreg & 1)
3689 argreg++;
3690
2dd604e7
RE
3691 /* If the argument is a pointer to a function, and it is a
3692 Thumb function, create a LOCAL copy of the value and set
3693 the THUMB bit in it. */
3694 if (TYPE_CODE_PTR == typecode
3695 && target_type != NULL
f96b8fa0 3696 && TYPE_CODE_FUNC == TYPE_CODE (check_typedef (target_type)))
2dd604e7 3697 {
e17a4113 3698 CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
9779414d 3699 if (arm_pc_is_thumb (gdbarch, regval))
2dd604e7 3700 {
8c6363cf
TT
3701 bfd_byte *copy = alloca (len);
3702 store_unsigned_integer (copy, len, byte_order,
e17a4113 3703 MAKE_THUMB_ADDR (regval));
8c6363cf 3704 val = copy;
2dd604e7
RE
3705 }
3706 }
3707
3708 /* Copy the argument to general registers or the stack in
3709 register-sized pieces. Large arguments are split between
3710 registers and stack. */
3711 while (len > 0)
3712 {
f0c9063c 3713 int partial_len = len < INT_REGISTER_SIZE ? len : INT_REGISTER_SIZE;
2dd604e7 3714
90445bd3 3715 if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
2dd604e7
RE
3716 {
3717 /* The argument is being passed in a general purpose
3718 register. */
e17a4113
UW
3719 CORE_ADDR regval
3720 = extract_unsigned_integer (val, partial_len, byte_order);
3721 if (byte_order == BFD_ENDIAN_BIG)
8bf8793c 3722 regval <<= (INT_REGISTER_SIZE - partial_len) * 8;
2dd604e7
RE
3723 if (arm_debug)
3724 fprintf_unfiltered (gdb_stdlog, "arg %d in %s = 0x%s\n",
c9f4d572
UW
3725 argnum,
3726 gdbarch_register_name
2af46ca0 3727 (gdbarch, argreg),
f0c9063c 3728 phex (regval, INT_REGISTER_SIZE));
2dd604e7
RE
3729 regcache_cooked_write_unsigned (regcache, argreg, regval);
3730 argreg++;
3731 }
3732 else
3733 {
3734 /* Push the arguments onto the stack. */
3735 if (arm_debug)
3736 fprintf_unfiltered (gdb_stdlog, "arg %d @ sp + %d\n",
3737 argnum, nstack);
f0c9063c
UW
3738 si = push_stack_item (si, val, INT_REGISTER_SIZE);
3739 nstack += INT_REGISTER_SIZE;
2dd604e7
RE
3740 }
3741
3742 len -= partial_len;
3743 val += partial_len;
3744 }
3745 }
3746 /* If we have an odd number of words to push, then decrement the stack
3747 by one word now, so first stack argument will be dword aligned. */
3748 if (nstack & 4)
3749 sp -= 4;
3750
3751 while (si)
3752 {
3753 sp -= si->len;
3754 write_memory (sp, si->data, si->len);
3755 si = pop_stack_item (si);
3756 }
3757
3758 /* Finally, update teh SP register. */
3759 regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
3760
3761 return sp;
3762}
3763
f53f0d0b
PB
3764
3765/* Always align the frame to an 8-byte boundary. This is required on
3766 some platforms and harmless on the rest. */
3767
3768static CORE_ADDR
3769arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
3770{
3771 /* Align the stack to eight bytes. */
3772 return sp & ~ (CORE_ADDR) 7;
3773}
3774
c906108c 3775static void
ed9a39eb 3776print_fpu_flags (int flags)
c906108c 3777{
c5aa993b
JM
3778 if (flags & (1 << 0))
3779 fputs ("IVO ", stdout);
3780 if (flags & (1 << 1))
3781 fputs ("DVZ ", stdout);
3782 if (flags & (1 << 2))
3783 fputs ("OFL ", stdout);
3784 if (flags & (1 << 3))
3785 fputs ("UFL ", stdout);
3786 if (flags & (1 << 4))
3787 fputs ("INX ", stdout);
3788 putchar ('\n');
c906108c
SS
3789}
3790
5e74b15c
RE
3791/* Print interesting information about the floating point processor
3792 (if present) or emulator. */
34e8f22d 3793static void
d855c300 3794arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
23e3a7ac 3795 struct frame_info *frame, const char *args)
c906108c 3796{
9c9acae0 3797 unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
c5aa993b
JM
3798 int type;
3799
3800 type = (status >> 24) & 127;
edefbb7c
AC
3801 if (status & (1 << 31))
3802 printf (_("Hardware FPU type %d\n"), type);
3803 else
3804 printf (_("Software FPU type %d\n"), type);
3805 /* i18n: [floating point unit] mask */
3806 fputs (_("mask: "), stdout);
c5aa993b 3807 print_fpu_flags (status >> 16);
edefbb7c
AC
3808 /* i18n: [floating point unit] flags */
3809 fputs (_("flags: "), stdout);
c5aa993b 3810 print_fpu_flags (status);
c906108c
SS
3811}
3812
27067745
UW
3813/* Construct the ARM extended floating point type. */
3814static struct type *
3815arm_ext_type (struct gdbarch *gdbarch)
3816{
3817 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3818
3819 if (!tdep->arm_ext_type)
3820 tdep->arm_ext_type
e9bb382b 3821 = arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
27067745
UW
3822 floatformats_arm_ext);
3823
3824 return tdep->arm_ext_type;
3825}
3826
58d6951d
DJ
3827static struct type *
3828arm_neon_double_type (struct gdbarch *gdbarch)
3829{
3830 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3831
3832 if (tdep->neon_double_type == NULL)
3833 {
3834 struct type *t, *elem;
3835
3836 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
3837 TYPE_CODE_UNION);
3838 elem = builtin_type (gdbarch)->builtin_uint8;
3839 append_composite_type_field (t, "u8", init_vector_type (elem, 8));
3840 elem = builtin_type (gdbarch)->builtin_uint16;
3841 append_composite_type_field (t, "u16", init_vector_type (elem, 4));
3842 elem = builtin_type (gdbarch)->builtin_uint32;
3843 append_composite_type_field (t, "u32", init_vector_type (elem, 2));
3844 elem = builtin_type (gdbarch)->builtin_uint64;
3845 append_composite_type_field (t, "u64", elem);
3846 elem = builtin_type (gdbarch)->builtin_float;
3847 append_composite_type_field (t, "f32", init_vector_type (elem, 2));
3848 elem = builtin_type (gdbarch)->builtin_double;
3849 append_composite_type_field (t, "f64", elem);
3850
3851 TYPE_VECTOR (t) = 1;
3852 TYPE_NAME (t) = "neon_d";
3853 tdep->neon_double_type = t;
3854 }
3855
3856 return tdep->neon_double_type;
3857}
3858
3859/* FIXME: The vector types are not correctly ordered on big-endian
3860 targets. Just as s0 is the low bits of d0, d0[0] is also the low
3861 bits of d0 - regardless of what unit size is being held in d0. So
3862 the offset of the first uint8 in d0 is 7, but the offset of the
3863 first float is 4. This code works as-is for little-endian
3864 targets. */
3865
3866static struct type *
3867arm_neon_quad_type (struct gdbarch *gdbarch)
3868{
3869 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3870
3871 if (tdep->neon_quad_type == NULL)
3872 {
3873 struct type *t, *elem;
3874
3875 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
3876 TYPE_CODE_UNION);
3877 elem = builtin_type (gdbarch)->builtin_uint8;
3878 append_composite_type_field (t, "u8", init_vector_type (elem, 16));
3879 elem = builtin_type (gdbarch)->builtin_uint16;
3880 append_composite_type_field (t, "u16", init_vector_type (elem, 8));
3881 elem = builtin_type (gdbarch)->builtin_uint32;
3882 append_composite_type_field (t, "u32", init_vector_type (elem, 4));
3883 elem = builtin_type (gdbarch)->builtin_uint64;
3884 append_composite_type_field (t, "u64", init_vector_type (elem, 2));
3885 elem = builtin_type (gdbarch)->builtin_float;
3886 append_composite_type_field (t, "f32", init_vector_type (elem, 4));
3887 elem = builtin_type (gdbarch)->builtin_double;
3888 append_composite_type_field (t, "f64", init_vector_type (elem, 2));
3889
3890 TYPE_VECTOR (t) = 1;
3891 TYPE_NAME (t) = "neon_q";
3892 tdep->neon_quad_type = t;
3893 }
3894
3895 return tdep->neon_quad_type;
3896}
3897
34e8f22d
RE
3898/* Return the GDB type object for the "standard" data type of data in
3899 register N. */
3900
3901static struct type *
7a5ea0d4 3902arm_register_type (struct gdbarch *gdbarch, int regnum)
032758dc 3903{
58d6951d
DJ
3904 int num_regs = gdbarch_num_regs (gdbarch);
3905
3906 if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
3907 && regnum >= num_regs && regnum < num_regs + 32)
3908 return builtin_type (gdbarch)->builtin_float;
3909
3910 if (gdbarch_tdep (gdbarch)->have_neon_pseudos
3911 && regnum >= num_regs + 32 && regnum < num_regs + 32 + 16)
3912 return arm_neon_quad_type (gdbarch);
3913
3914 /* If the target description has register information, we are only
3915 in this function so that we can override the types of
3916 double-precision registers for NEON. */
3917 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
3918 {
3919 struct type *t = tdesc_register_type (gdbarch, regnum);
3920
3921 if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
3922 && TYPE_CODE (t) == TYPE_CODE_FLT
3923 && gdbarch_tdep (gdbarch)->have_neon)
3924 return arm_neon_double_type (gdbarch);
3925 else
3926 return t;
3927 }
3928
34e8f22d 3929 if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
58d6951d
DJ
3930 {
3931 if (!gdbarch_tdep (gdbarch)->have_fpa_registers)
3932 return builtin_type (gdbarch)->builtin_void;
3933
3934 return arm_ext_type (gdbarch);
3935 }
e4c16157 3936 else if (regnum == ARM_SP_REGNUM)
0dfff4cb 3937 return builtin_type (gdbarch)->builtin_data_ptr;
e4c16157 3938 else if (regnum == ARM_PC_REGNUM)
0dfff4cb 3939 return builtin_type (gdbarch)->builtin_func_ptr;
ff6f572f
DJ
3940 else if (regnum >= ARRAY_SIZE (arm_register_names))
3941 /* These registers are only supported on targets which supply
3942 an XML description. */
df4df182 3943 return builtin_type (gdbarch)->builtin_int0;
032758dc 3944 else
df4df182 3945 return builtin_type (gdbarch)->builtin_uint32;
032758dc
AC
3946}
3947
ff6f572f
DJ
3948/* Map a DWARF register REGNUM onto the appropriate GDB register
3949 number. */
3950
3951static int
d3f73121 3952arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
ff6f572f
DJ
3953{
3954 /* Core integer regs. */
3955 if (reg >= 0 && reg <= 15)
3956 return reg;
3957
3958 /* Legacy FPA encoding. These were once used in a way which
3959 overlapped with VFP register numbering, so their use is
3960 discouraged, but GDB doesn't support the ARM toolchain
3961 which used them for VFP. */
3962 if (reg >= 16 && reg <= 23)
3963 return ARM_F0_REGNUM + reg - 16;
3964
3965 /* New assignments for the FPA registers. */
3966 if (reg >= 96 && reg <= 103)
3967 return ARM_F0_REGNUM + reg - 96;
3968
3969 /* WMMX register assignments. */
3970 if (reg >= 104 && reg <= 111)
3971 return ARM_WCGR0_REGNUM + reg - 104;
3972
3973 if (reg >= 112 && reg <= 127)
3974 return ARM_WR0_REGNUM + reg - 112;
3975
3976 if (reg >= 192 && reg <= 199)
3977 return ARM_WC0_REGNUM + reg - 192;
3978
58d6951d
DJ
3979 /* VFP v2 registers. A double precision value is actually
3980 in d1 rather than s2, but the ABI only defines numbering
3981 for the single precision registers. This will "just work"
3982 in GDB for little endian targets (we'll read eight bytes,
3983 starting in s0 and then progressing to s1), but will be
3984 reversed on big endian targets with VFP. This won't
3985 be a problem for the new Neon quad registers; you're supposed
3986 to use DW_OP_piece for those. */
3987 if (reg >= 64 && reg <= 95)
3988 {
3989 char name_buf[4];
3990
3991 sprintf (name_buf, "s%d", reg - 64);
3992 return user_reg_map_name_to_regnum (gdbarch, name_buf,
3993 strlen (name_buf));
3994 }
3995
3996 /* VFP v3 / Neon registers. This range is also used for VFP v2
3997 registers, except that it now describes d0 instead of s0. */
3998 if (reg >= 256 && reg <= 287)
3999 {
4000 char name_buf[4];
4001
4002 sprintf (name_buf, "d%d", reg - 256);
4003 return user_reg_map_name_to_regnum (gdbarch, name_buf,
4004 strlen (name_buf));
4005 }
4006
ff6f572f
DJ
4007 return -1;
4008}
4009
26216b98
AC
4010/* Map GDB internal REGNUM onto the Arm simulator register numbers. */
4011static int
e7faf938 4012arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
26216b98
AC
4013{
4014 int reg = regnum;
e7faf938 4015 gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
26216b98 4016
ff6f572f
DJ
4017 if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
4018 return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
4019
4020 if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
4021 return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
4022
4023 if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
4024 return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
4025
26216b98
AC
4026 if (reg < NUM_GREGS)
4027 return SIM_ARM_R0_REGNUM + reg;
4028 reg -= NUM_GREGS;
4029
4030 if (reg < NUM_FREGS)
4031 return SIM_ARM_FP0_REGNUM + reg;
4032 reg -= NUM_FREGS;
4033
4034 if (reg < NUM_SREGS)
4035 return SIM_ARM_FPS_REGNUM + reg;
4036 reg -= NUM_SREGS;
4037
edefbb7c 4038 internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
26216b98 4039}
34e8f22d 4040
a37b3cc0
AC
4041/* NOTE: cagney/2001-08-20: Both convert_from_extended() and
4042 convert_to_extended() use floatformat_arm_ext_littlebyte_bigword.
4043 It is thought that this is is the floating-point register format on
4044 little-endian systems. */
c906108c 4045
ed9a39eb 4046static void
b508a996 4047convert_from_extended (const struct floatformat *fmt, const void *ptr,
be8626e0 4048 void *dbl, int endianess)
c906108c 4049{
a37b3cc0 4050 DOUBLEST d;
be8626e0
MD
4051
4052 if (endianess == BFD_ENDIAN_BIG)
a37b3cc0
AC
4053 floatformat_to_doublest (&floatformat_arm_ext_big, ptr, &d);
4054 else
4055 floatformat_to_doublest (&floatformat_arm_ext_littlebyte_bigword,
4056 ptr, &d);
b508a996 4057 floatformat_from_doublest (fmt, &d, dbl);
c906108c
SS
4058}
4059
34e8f22d 4060static void
be8626e0
MD
4061convert_to_extended (const struct floatformat *fmt, void *dbl, const void *ptr,
4062 int endianess)
c906108c 4063{
a37b3cc0 4064 DOUBLEST d;
be8626e0 4065
b508a996 4066 floatformat_to_doublest (fmt, ptr, &d);
be8626e0 4067 if (endianess == BFD_ENDIAN_BIG)
a37b3cc0
AC
4068 floatformat_from_doublest (&floatformat_arm_ext_big, &d, dbl);
4069 else
4070 floatformat_from_doublest (&floatformat_arm_ext_littlebyte_bigword,
4071 &d, dbl);
c906108c 4072}
ed9a39eb 4073
c906108c 4074static int
ed9a39eb 4075condition_true (unsigned long cond, unsigned long status_reg)
c906108c
SS
4076{
4077 if (cond == INST_AL || cond == INST_NV)
4078 return 1;
4079
4080 switch (cond)
4081 {
4082 case INST_EQ:
4083 return ((status_reg & FLAG_Z) != 0);
4084 case INST_NE:
4085 return ((status_reg & FLAG_Z) == 0);
4086 case INST_CS:
4087 return ((status_reg & FLAG_C) != 0);
4088 case INST_CC:
4089 return ((status_reg & FLAG_C) == 0);
4090 case INST_MI:
4091 return ((status_reg & FLAG_N) != 0);
4092 case INST_PL:
4093 return ((status_reg & FLAG_N) == 0);
4094 case INST_VS:
4095 return ((status_reg & FLAG_V) != 0);
4096 case INST_VC:
4097 return ((status_reg & FLAG_V) == 0);
4098 case INST_HI:
4099 return ((status_reg & (FLAG_C | FLAG_Z)) == FLAG_C);
4100 case INST_LS:
4101 return ((status_reg & (FLAG_C | FLAG_Z)) != FLAG_C);
4102 case INST_GE:
4103 return (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0));
4104 case INST_LT:
4105 return (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0));
4106 case INST_GT:
f8bf5763
PM
4107 return (((status_reg & FLAG_Z) == 0)
4108 && (((status_reg & FLAG_N) == 0)
4109 == ((status_reg & FLAG_V) == 0)));
c906108c 4110 case INST_LE:
f8bf5763
PM
4111 return (((status_reg & FLAG_Z) != 0)
4112 || (((status_reg & FLAG_N) == 0)
4113 != ((status_reg & FLAG_V) == 0)));
c906108c
SS
4114 }
4115 return 1;
4116}
4117
c906108c 4118static unsigned long
0b1b3e42
UW
4119shifted_reg_val (struct frame_info *frame, unsigned long inst, int carry,
4120 unsigned long pc_val, unsigned long status_reg)
c906108c
SS
4121{
4122 unsigned long res, shift;
4123 int rm = bits (inst, 0, 3);
4124 unsigned long shifttype = bits (inst, 5, 6);
c5aa993b
JM
4125
4126 if (bit (inst, 4))
c906108c
SS
4127 {
4128 int rs = bits (inst, 8, 11);
0b1b3e42
UW
4129 shift = (rs == 15 ? pc_val + 8
4130 : get_frame_register_unsigned (frame, rs)) & 0xFF;
c906108c
SS
4131 }
4132 else
4133 shift = bits (inst, 7, 11);
c5aa993b 4134
bf9f652a 4135 res = (rm == ARM_PC_REGNUM
0d39a070 4136 ? (pc_val + (bit (inst, 4) ? 12 : 8))
0b1b3e42 4137 : get_frame_register_unsigned (frame, rm));
c906108c
SS
4138
4139 switch (shifttype)
4140 {
c5aa993b 4141 case 0: /* LSL */
c906108c
SS
4142 res = shift >= 32 ? 0 : res << shift;
4143 break;
c5aa993b
JM
4144
4145 case 1: /* LSR */
c906108c
SS
4146 res = shift >= 32 ? 0 : res >> shift;
4147 break;
4148
c5aa993b
JM
4149 case 2: /* ASR */
4150 if (shift >= 32)
4151 shift = 31;
c906108c
SS
4152 res = ((res & 0x80000000L)
4153 ? ~((~res) >> shift) : res >> shift);
4154 break;
4155
c5aa993b 4156 case 3: /* ROR/RRX */
c906108c
SS
4157 shift &= 31;
4158 if (shift == 0)
4159 res = (res >> 1) | (carry ? 0x80000000L : 0);
4160 else
c5aa993b 4161 res = (res >> shift) | (res << (32 - shift));
c906108c
SS
4162 break;
4163 }
4164
4165 return res & 0xffffffff;
4166}
4167
c906108c
SS
4168/* Return number of 1-bits in VAL. */
4169
4170static int
ed9a39eb 4171bitcount (unsigned long val)
c906108c
SS
4172{
4173 int nbits;
4174 for (nbits = 0; val != 0; nbits++)
0963b4bd 4175 val &= val - 1; /* Delete rightmost 1-bit in val. */
c906108c
SS
4176 return nbits;
4177}
4178
177321bd
DJ
4179/* Return the size in bytes of the complete Thumb instruction whose
4180 first halfword is INST1. */
4181
4182static int
4183thumb_insn_size (unsigned short inst1)
4184{
4185 if ((inst1 & 0xe000) == 0xe000 && (inst1 & 0x1800) != 0)
4186 return 4;
4187 else
4188 return 2;
4189}
4190
4191static int
4192thumb_advance_itstate (unsigned int itstate)
4193{
4194 /* Preserve IT[7:5], the first three bits of the condition. Shift
4195 the upcoming condition flags left by one bit. */
4196 itstate = (itstate & 0xe0) | ((itstate << 1) & 0x1f);
4197
4198 /* If we have finished the IT block, clear the state. */
4199 if ((itstate & 0x0f) == 0)
4200 itstate = 0;
4201
4202 return itstate;
4203}
4204
4205/* Find the next PC after the current instruction executes. In some
4206 cases we can not statically determine the answer (see the IT state
4207 handling in this function); in that case, a breakpoint may be
4208 inserted in addition to the returned PC, which will be used to set
4209 another breakpoint by our caller. */
4210
ad527d2e 4211static CORE_ADDR
18819fa6 4212thumb_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc)
c906108c 4213{
2af46ca0 4214 struct gdbarch *gdbarch = get_frame_arch (frame);
177321bd 4215 struct address_space *aspace = get_frame_address_space (frame);
e17a4113
UW
4216 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4217 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
c5aa993b 4218 unsigned long pc_val = ((unsigned long) pc) + 4; /* PC after prefetch */
e17a4113 4219 unsigned short inst1;
0963b4bd 4220 CORE_ADDR nextpc = pc + 2; /* Default is next instruction. */
c906108c 4221 unsigned long offset;
177321bd 4222 ULONGEST status, itstate;
c906108c 4223
50e98be4
DJ
4224 nextpc = MAKE_THUMB_ADDR (nextpc);
4225 pc_val = MAKE_THUMB_ADDR (pc_val);
4226
e17a4113 4227 inst1 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
9d4fde75 4228
9dca5578
DJ
4229 /* Thumb-2 conditional execution support. There are eight bits in
4230 the CPSR which describe conditional execution state. Once
4231 reconstructed (they're in a funny order), the low five bits
4232 describe the low bit of the condition for each instruction and
4233 how many instructions remain. The high three bits describe the
4234 base condition. One of the low four bits will be set if an IT
4235 block is active. These bits read as zero on earlier
4236 processors. */
4237 status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
177321bd 4238 itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
9dca5578 4239
177321bd
DJ
4240 /* If-Then handling. On GNU/Linux, where this routine is used, we
4241 use an undefined instruction as a breakpoint. Unlike BKPT, IT
4242 can disable execution of the undefined instruction. So we might
4243 miss the breakpoint if we set it on a skipped conditional
4244 instruction. Because conditional instructions can change the
4245 flags, affecting the execution of further instructions, we may
4246 need to set two breakpoints. */
9dca5578 4247
177321bd
DJ
4248 if (gdbarch_tdep (gdbarch)->thumb2_breakpoint != NULL)
4249 {
4250 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
4251 {
4252 /* An IT instruction. Because this instruction does not
4253 modify the flags, we can accurately predict the next
4254 executed instruction. */
4255 itstate = inst1 & 0x00ff;
4256 pc += thumb_insn_size (inst1);
4257
4258 while (itstate != 0 && ! condition_true (itstate >> 4, status))
4259 {
0963b4bd
MS
4260 inst1 = read_memory_unsigned_integer (pc, 2,
4261 byte_order_for_code);
177321bd
DJ
4262 pc += thumb_insn_size (inst1);
4263 itstate = thumb_advance_itstate (itstate);
4264 }
4265
50e98be4 4266 return MAKE_THUMB_ADDR (pc);
177321bd
DJ
4267 }
4268 else if (itstate != 0)
4269 {
4270 /* We are in a conditional block. Check the condition. */
4271 if (! condition_true (itstate >> 4, status))
4272 {
4273 /* Advance to the next executed instruction. */
4274 pc += thumb_insn_size (inst1);
4275 itstate = thumb_advance_itstate (itstate);
4276
4277 while (itstate != 0 && ! condition_true (itstate >> 4, status))
4278 {
0963b4bd
MS
4279 inst1 = read_memory_unsigned_integer (pc, 2,
4280 byte_order_for_code);
177321bd
DJ
4281 pc += thumb_insn_size (inst1);
4282 itstate = thumb_advance_itstate (itstate);
4283 }
4284
50e98be4 4285 return MAKE_THUMB_ADDR (pc);
177321bd
DJ
4286 }
4287 else if ((itstate & 0x0f) == 0x08)
4288 {
4289 /* This is the last instruction of the conditional
4290 block, and it is executed. We can handle it normally
4291 because the following instruction is not conditional,
4292 and we must handle it normally because it is
4293 permitted to branch. Fall through. */
4294 }
4295 else
4296 {
4297 int cond_negated;
4298
4299 /* There are conditional instructions after this one.
4300 If this instruction modifies the flags, then we can
4301 not predict what the next executed instruction will
4302 be. Fortunately, this instruction is architecturally
4303 forbidden to branch; we know it will fall through.
4304 Start by skipping past it. */
4305 pc += thumb_insn_size (inst1);
4306 itstate = thumb_advance_itstate (itstate);
4307
4308 /* Set a breakpoint on the following instruction. */
4309 gdb_assert ((itstate & 0x0f) != 0);
18819fa6
UW
4310 arm_insert_single_step_breakpoint (gdbarch, aspace,
4311 MAKE_THUMB_ADDR (pc));
177321bd
DJ
4312 cond_negated = (itstate >> 4) & 1;
4313
4314 /* Skip all following instructions with the same
4315 condition. If there is a later instruction in the IT
4316 block with the opposite condition, set the other
4317 breakpoint there. If not, then set a breakpoint on
4318 the instruction after the IT block. */
4319 do
4320 {
0963b4bd
MS
4321 inst1 = read_memory_unsigned_integer (pc, 2,
4322 byte_order_for_code);
177321bd
DJ
4323 pc += thumb_insn_size (inst1);
4324 itstate = thumb_advance_itstate (itstate);
4325 }
4326 while (itstate != 0 && ((itstate >> 4) & 1) == cond_negated);
4327
50e98be4 4328 return MAKE_THUMB_ADDR (pc);
177321bd
DJ
4329 }
4330 }
4331 }
4332 else if (itstate & 0x0f)
9dca5578
DJ
4333 {
4334 /* We are in a conditional block. Check the condition. */
177321bd 4335 int cond = itstate >> 4;
9dca5578
DJ
4336
4337 if (! condition_true (cond, status))
4338 {
4339 /* Advance to the next instruction. All the 32-bit
4340 instructions share a common prefix. */
4341 if ((inst1 & 0xe000) == 0xe000 && (inst1 & 0x1800) != 0)
50e98be4 4342 return MAKE_THUMB_ADDR (pc + 4);
9dca5578 4343 else
50e98be4 4344 return MAKE_THUMB_ADDR (pc + 2);
9dca5578 4345 }
177321bd
DJ
4346
4347 /* Otherwise, handle the instruction normally. */
9dca5578
DJ
4348 }
4349
c906108c
SS
4350 if ((inst1 & 0xff00) == 0xbd00) /* pop {rlist, pc} */
4351 {
4352 CORE_ADDR sp;
4353
4354 /* Fetch the saved PC from the stack. It's stored above
4355 all of the other registers. */
f0c9063c 4356 offset = bitcount (bits (inst1, 0, 7)) * INT_REGISTER_SIZE;
0b1b3e42 4357 sp = get_frame_register_unsigned (frame, ARM_SP_REGNUM);
e17a4113 4358 nextpc = read_memory_unsigned_integer (sp + offset, 4, byte_order);
c906108c
SS
4359 }
4360 else if ((inst1 & 0xf000) == 0xd000) /* conditional branch */
4361 {
c5aa993b 4362 unsigned long cond = bits (inst1, 8, 11);
25b41d01
YQ
4363 if (cond == 0x0f) /* 0x0f = SWI */
4364 {
4365 struct gdbarch_tdep *tdep;
4366 tdep = gdbarch_tdep (gdbarch);
4367
4368 if (tdep->syscall_next_pc != NULL)
4369 nextpc = tdep->syscall_next_pc (frame);
4370
4371 }
4372 else if (cond != 0x0f && condition_true (cond, status))
c906108c
SS
4373 nextpc = pc_val + (sbits (inst1, 0, 7) << 1);
4374 }
4375 else if ((inst1 & 0xf800) == 0xe000) /* unconditional branch */
4376 {
4377 nextpc = pc_val + (sbits (inst1, 0, 10) << 1);
4378 }
9dca5578 4379 else if ((inst1 & 0xe000) == 0xe000) /* 32-bit instruction */
c906108c 4380 {
e17a4113
UW
4381 unsigned short inst2;
4382 inst2 = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);
9dca5578
DJ
4383
4384 /* Default to the next instruction. */
4385 nextpc = pc + 4;
50e98be4 4386 nextpc = MAKE_THUMB_ADDR (nextpc);
9dca5578
DJ
4387
4388 if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
4389 {
4390 /* Branches and miscellaneous control instructions. */
4391
4392 if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
4393 {
4394 /* B, BL, BLX. */
4395 int j1, j2, imm1, imm2;
4396
4397 imm1 = sbits (inst1, 0, 10);
4398 imm2 = bits (inst2, 0, 10);
4399 j1 = bit (inst2, 13);
4400 j2 = bit (inst2, 11);
4401
4402 offset = ((imm1 << 12) + (imm2 << 1));
4403 offset ^= ((!j2) << 22) | ((!j1) << 23);
4404
4405 nextpc = pc_val + offset;
4406 /* For BLX make sure to clear the low bits. */
4407 if (bit (inst2, 12) == 0)
4408 nextpc = nextpc & 0xfffffffc;
4409 }
4410 else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
4411 {
4412 /* SUBS PC, LR, #imm8. */
4413 nextpc = get_frame_register_unsigned (frame, ARM_LR_REGNUM);
4414 nextpc -= inst2 & 0x00ff;
4415 }
4069ebbe 4416 else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
9dca5578
DJ
4417 {
4418 /* Conditional branch. */
4419 if (condition_true (bits (inst1, 6, 9), status))
4420 {
4421 int sign, j1, j2, imm1, imm2;
4422
4423 sign = sbits (inst1, 10, 10);
4424 imm1 = bits (inst1, 0, 5);
4425 imm2 = bits (inst2, 0, 10);
4426 j1 = bit (inst2, 13);
4427 j2 = bit (inst2, 11);
4428
4429 offset = (sign << 20) + (j2 << 19) + (j1 << 18);
4430 offset += (imm1 << 12) + (imm2 << 1);
4431
4432 nextpc = pc_val + offset;
4433 }
4434 }
4435 }
4436 else if ((inst1 & 0xfe50) == 0xe810)
4437 {
4438 /* Load multiple or RFE. */
4439 int rn, offset, load_pc = 1;
4440
4441 rn = bits (inst1, 0, 3);
4442 if (bit (inst1, 7) && !bit (inst1, 8))
4443 {
4444 /* LDMIA or POP */
4445 if (!bit (inst2, 15))
4446 load_pc = 0;
4447 offset = bitcount (inst2) * 4 - 4;
4448 }
4449 else if (!bit (inst1, 7) && bit (inst1, 8))
4450 {
4451 /* LDMDB */
4452 if (!bit (inst2, 15))
4453 load_pc = 0;
4454 offset = -4;
4455 }
4456 else if (bit (inst1, 7) && bit (inst1, 8))
4457 {
4458 /* RFEIA */
4459 offset = 0;
4460 }
4461 else if (!bit (inst1, 7) && !bit (inst1, 8))
4462 {
4463 /* RFEDB */
4464 offset = -8;
4465 }
4466 else
4467 load_pc = 0;
4468
4469 if (load_pc)
4470 {
4471 CORE_ADDR addr = get_frame_register_unsigned (frame, rn);
4472 nextpc = get_frame_memory_unsigned (frame, addr + offset, 4);
4473 }
4474 }
4475 else if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
4476 {
4477 /* MOV PC or MOVS PC. */
4478 nextpc = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
50e98be4 4479 nextpc = MAKE_THUMB_ADDR (nextpc);
9dca5578
DJ
4480 }
4481 else if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
4482 {
4483 /* LDR PC. */
4484 CORE_ADDR base;
4485 int rn, load_pc = 1;
4486
4487 rn = bits (inst1, 0, 3);
4488 base = get_frame_register_unsigned (frame, rn);
bf9f652a 4489 if (rn == ARM_PC_REGNUM)
9dca5578
DJ
4490 {
4491 base = (base + 4) & ~(CORE_ADDR) 0x3;
4492 if (bit (inst1, 7))
4493 base += bits (inst2, 0, 11);
4494 else
4495 base -= bits (inst2, 0, 11);
4496 }
4497 else if (bit (inst1, 7))
4498 base += bits (inst2, 0, 11);
4499 else if (bit (inst2, 11))
4500 {
4501 if (bit (inst2, 10))
4502 {
4503 if (bit (inst2, 9))
4504 base += bits (inst2, 0, 7);
4505 else
4506 base -= bits (inst2, 0, 7);
4507 }
4508 }
4509 else if ((inst2 & 0x0fc0) == 0x0000)
4510 {
4511 int shift = bits (inst2, 4, 5), rm = bits (inst2, 0, 3);
4512 base += get_frame_register_unsigned (frame, rm) << shift;
4513 }
4514 else
4515 /* Reserved. */
4516 load_pc = 0;
4517
4518 if (load_pc)
4519 nextpc = get_frame_memory_unsigned (frame, base, 4);
4520 }
4521 else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
4522 {
4523 /* TBB. */
d476da0e
RE
4524 CORE_ADDR tbl_reg, table, offset, length;
4525
4526 tbl_reg = bits (inst1, 0, 3);
4527 if (tbl_reg == 0x0f)
4528 table = pc + 4; /* Regcache copy of PC isn't right yet. */
4529 else
4530 table = get_frame_register_unsigned (frame, tbl_reg);
9dca5578 4531
9dca5578
DJ
4532 offset = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
4533 length = 2 * get_frame_memory_unsigned (frame, table + offset, 1);
4534 nextpc = pc_val + length;
4535 }
d476da0e 4536 else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
9dca5578
DJ
4537 {
4538 /* TBH. */
d476da0e
RE
4539 CORE_ADDR tbl_reg, table, offset, length;
4540
4541 tbl_reg = bits (inst1, 0, 3);
4542 if (tbl_reg == 0x0f)
4543 table = pc + 4; /* Regcache copy of PC isn't right yet. */
4544 else
4545 table = get_frame_register_unsigned (frame, tbl_reg);
9dca5578 4546
9dca5578
DJ
4547 offset = 2 * get_frame_register_unsigned (frame, bits (inst2, 0, 3));
4548 length = 2 * get_frame_memory_unsigned (frame, table + offset, 2);
4549 nextpc = pc_val + length;
4550 }
c906108c 4551 }
aa17d93e 4552 else if ((inst1 & 0xff00) == 0x4700) /* bx REG, blx REG */
9498281f
DJ
4553 {
4554 if (bits (inst1, 3, 6) == 0x0f)
4555 nextpc = pc_val;
4556 else
0b1b3e42 4557 nextpc = get_frame_register_unsigned (frame, bits (inst1, 3, 6));
9498281f 4558 }
ad8b5167
UW
4559 else if ((inst1 & 0xff87) == 0x4687) /* mov pc, REG */
4560 {
4561 if (bits (inst1, 3, 6) == 0x0f)
4562 nextpc = pc_val;
4563 else
4564 nextpc = get_frame_register_unsigned (frame, bits (inst1, 3, 6));
4565
4566 nextpc = MAKE_THUMB_ADDR (nextpc);
4567 }
9dca5578
DJ
4568 else if ((inst1 & 0xf500) == 0xb100)
4569 {
4570 /* CBNZ or CBZ. */
4571 int imm = (bit (inst1, 9) << 6) + (bits (inst1, 3, 7) << 1);
4572 ULONGEST reg = get_frame_register_unsigned (frame, bits (inst1, 0, 2));
4573
4574 if (bit (inst1, 11) && reg != 0)
4575 nextpc = pc_val + imm;
4576 else if (!bit (inst1, 11) && reg == 0)
4577 nextpc = pc_val + imm;
4578 }
c906108c
SS
4579 return nextpc;
4580}
4581
50e98be4 4582/* Get the raw next address. PC is the current program counter, in
18819fa6 4583 FRAME, which is assumed to be executing in ARM mode.
50e98be4
DJ
4584
4585 The value returned has the execution state of the next instruction
4586 encoded in it. Use IS_THUMB_ADDR () to see whether the instruction is
4587 in Thumb-State, and gdbarch_addr_bits_remove () to get the plain memory
0963b4bd
MS
4588 address. */
4589
50e98be4 4590static CORE_ADDR
18819fa6 4591arm_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc)
c906108c 4592{
2af46ca0 4593 struct gdbarch *gdbarch = get_frame_arch (frame);
e17a4113
UW
4594 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4595 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
c906108c
SS
4596 unsigned long pc_val;
4597 unsigned long this_instr;
4598 unsigned long status;
4599 CORE_ADDR nextpc;
4600
c906108c 4601 pc_val = (unsigned long) pc;
e17a4113 4602 this_instr = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
9d4fde75 4603
0b1b3e42 4604 status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
c5aa993b 4605 nextpc = (CORE_ADDR) (pc_val + 4); /* Default case */
c906108c 4606
daddc3c1
DJ
4607 if (bits (this_instr, 28, 31) == INST_NV)
4608 switch (bits (this_instr, 24, 27))
4609 {
4610 case 0xa:
4611 case 0xb:
4612 {
4613 /* Branch with Link and change to Thumb. */
4614 nextpc = BranchDest (pc, this_instr);
4615 nextpc |= bit (this_instr, 24) << 1;
50e98be4 4616 nextpc = MAKE_THUMB_ADDR (nextpc);
daddc3c1
DJ
4617 break;
4618 }
4619 case 0xc:
4620 case 0xd:
4621 case 0xe:
4622 /* Coprocessor register transfer. */
4623 if (bits (this_instr, 12, 15) == 15)
4624 error (_("Invalid update to pc in instruction"));
4625 break;
4626 }
4627 else if (condition_true (bits (this_instr, 28, 31), status))
c906108c
SS
4628 {
4629 switch (bits (this_instr, 24, 27))
4630 {
c5aa993b 4631 case 0x0:
94c30b78 4632 case 0x1: /* data processing */
c5aa993b
JM
4633 case 0x2:
4634 case 0x3:
c906108c
SS
4635 {
4636 unsigned long operand1, operand2, result = 0;
4637 unsigned long rn;
4638 int c;
c5aa993b 4639
c906108c
SS
4640 if (bits (this_instr, 12, 15) != 15)
4641 break;
4642
4643 if (bits (this_instr, 22, 25) == 0
c5aa993b 4644 && bits (this_instr, 4, 7) == 9) /* multiply */
edefbb7c 4645 error (_("Invalid update to pc in instruction"));
c906108c 4646
9498281f 4647 /* BX <reg>, BLX <reg> */
e150acc7
PB
4648 if (bits (this_instr, 4, 27) == 0x12fff1
4649 || bits (this_instr, 4, 27) == 0x12fff3)
9498281f
DJ
4650 {
4651 rn = bits (this_instr, 0, 3);
bf9f652a
YQ
4652 nextpc = ((rn == ARM_PC_REGNUM)
4653 ? (pc_val + 8)
4654 : get_frame_register_unsigned (frame, rn));
4655
9498281f
DJ
4656 return nextpc;
4657 }
4658
0963b4bd 4659 /* Multiply into PC. */
c906108c
SS
4660 c = (status & FLAG_C) ? 1 : 0;
4661 rn = bits (this_instr, 16, 19);
bf9f652a
YQ
4662 operand1 = ((rn == ARM_PC_REGNUM)
4663 ? (pc_val + 8)
4664 : get_frame_register_unsigned (frame, rn));
c5aa993b 4665
c906108c
SS
4666 if (bit (this_instr, 25))
4667 {
4668 unsigned long immval = bits (this_instr, 0, 7);
4669 unsigned long rotate = 2 * bits (this_instr, 8, 11);
c5aa993b
JM
4670 operand2 = ((immval >> rotate) | (immval << (32 - rotate)))
4671 & 0xffffffff;
c906108c 4672 }
0963b4bd
MS
4673 else /* operand 2 is a shifted register. */
4674 operand2 = shifted_reg_val (frame, this_instr, c,
4675 pc_val, status);
c5aa993b 4676
c906108c
SS
4677 switch (bits (this_instr, 21, 24))
4678 {
c5aa993b 4679 case 0x0: /*and */
c906108c
SS
4680 result = operand1 & operand2;
4681 break;
4682
c5aa993b 4683 case 0x1: /*eor */
c906108c
SS
4684 result = operand1 ^ operand2;
4685 break;
4686
c5aa993b 4687 case 0x2: /*sub */
c906108c
SS
4688 result = operand1 - operand2;
4689 break;
4690
c5aa993b 4691 case 0x3: /*rsb */
c906108c
SS
4692 result = operand2 - operand1;
4693 break;
4694
c5aa993b 4695 case 0x4: /*add */
c906108c
SS
4696 result = operand1 + operand2;
4697 break;
4698
c5aa993b 4699 case 0x5: /*adc */
c906108c
SS
4700 result = operand1 + operand2 + c;
4701 break;
4702
c5aa993b 4703 case 0x6: /*sbc */
c906108c
SS
4704 result = operand1 - operand2 + c;
4705 break;
4706
c5aa993b 4707 case 0x7: /*rsc */
c906108c
SS
4708 result = operand2 - operand1 + c;
4709 break;
4710
c5aa993b
JM
4711 case 0x8:
4712 case 0x9:
4713 case 0xa:
4714 case 0xb: /* tst, teq, cmp, cmn */
c906108c
SS
4715 result = (unsigned long) nextpc;
4716 break;
4717
c5aa993b 4718 case 0xc: /*orr */
c906108c
SS
4719 result = operand1 | operand2;
4720 break;
4721
c5aa993b 4722 case 0xd: /*mov */
c906108c
SS
4723 /* Always step into a function. */
4724 result = operand2;
c5aa993b 4725 break;
c906108c 4726
c5aa993b 4727 case 0xe: /*bic */
c906108c
SS
4728 result = operand1 & ~operand2;
4729 break;
4730
c5aa993b 4731 case 0xf: /*mvn */
c906108c
SS
4732 result = ~operand2;
4733 break;
4734 }
c906108c 4735
50e98be4
DJ
4736 /* In 26-bit APCS the bottom two bits of the result are
4737 ignored, and we always end up in ARM state. */
4738 if (!arm_apcs_32)
4739 nextpc = arm_addr_bits_remove (gdbarch, result);
4740 else
4741 nextpc = result;
4742
c906108c
SS
4743 break;
4744 }
c5aa993b
JM
4745
4746 case 0x4:
4747 case 0x5: /* data transfer */
4748 case 0x6:
4749 case 0x7:
c906108c
SS
4750 if (bit (this_instr, 20))
4751 {
4752 /* load */
4753 if (bits (this_instr, 12, 15) == 15)
4754 {
4755 /* rd == pc */
c5aa993b 4756 unsigned long rn;
c906108c 4757 unsigned long base;
c5aa993b 4758
c906108c 4759 if (bit (this_instr, 22))
edefbb7c 4760 error (_("Invalid update to pc in instruction"));
c906108c
SS
4761
4762 /* byte write to PC */
4763 rn = bits (this_instr, 16, 19);
bf9f652a
YQ
4764 base = ((rn == ARM_PC_REGNUM)
4765 ? (pc_val + 8)
4766 : get_frame_register_unsigned (frame, rn));
4767
c906108c
SS
4768 if (bit (this_instr, 24))
4769 {
4770 /* pre-indexed */
4771 int c = (status & FLAG_C) ? 1 : 0;
4772 unsigned long offset =
c5aa993b 4773 (bit (this_instr, 25)
0b1b3e42 4774 ? shifted_reg_val (frame, this_instr, c, pc_val, status)
c5aa993b 4775 : bits (this_instr, 0, 11));
c906108c
SS
4776
4777 if (bit (this_instr, 23))
4778 base += offset;
4779 else
4780 base -= offset;
4781 }
51370a33
YQ
4782 nextpc =
4783 (CORE_ADDR) read_memory_unsigned_integer ((CORE_ADDR) base,
4784 4, byte_order);
c906108c
SS
4785 }
4786 }
4787 break;
c5aa993b
JM
4788
4789 case 0x8:
4790 case 0x9: /* block transfer */
c906108c
SS
4791 if (bit (this_instr, 20))
4792 {
4793 /* LDM */
4794 if (bit (this_instr, 15))
4795 {
4796 /* loading pc */
4797 int offset = 0;
51370a33
YQ
4798 unsigned long rn_val
4799 = get_frame_register_unsigned (frame,
4800 bits (this_instr, 16, 19));
c906108c
SS
4801
4802 if (bit (this_instr, 23))
4803 {
4804 /* up */
4805 unsigned long reglist = bits (this_instr, 0, 14);
4806 offset = bitcount (reglist) * 4;
c5aa993b 4807 if (bit (this_instr, 24)) /* pre */
c906108c
SS
4808 offset += 4;
4809 }
4810 else if (bit (this_instr, 24))
4811 offset = -4;
c5aa993b 4812
51370a33
YQ
4813 nextpc =
4814 (CORE_ADDR) read_memory_unsigned_integer ((CORE_ADDR)
4815 (rn_val + offset),
4816 4, byte_order);
c906108c
SS
4817 }
4818 }
4819 break;
c5aa993b
JM
4820
4821 case 0xb: /* branch & link */
4822 case 0xa: /* branch */
c906108c
SS
4823 {
4824 nextpc = BranchDest (pc, this_instr);
c906108c
SS
4825 break;
4826 }
c5aa993b
JM
4827
4828 case 0xc:
4829 case 0xd:
4830 case 0xe: /* coproc ops */
25b41d01 4831 break;
c5aa993b 4832 case 0xf: /* SWI */
25b41d01
YQ
4833 {
4834 struct gdbarch_tdep *tdep;
4835 tdep = gdbarch_tdep (gdbarch);
4836
4837 if (tdep->syscall_next_pc != NULL)
4838 nextpc = tdep->syscall_next_pc (frame);
4839
4840 }
c906108c
SS
4841 break;
4842
4843 default:
edefbb7c 4844 fprintf_filtered (gdb_stderr, _("Bad bit-field extraction\n"));
c906108c
SS
4845 return (pc);
4846 }
4847 }
4848
4849 return nextpc;
4850}
4851
18819fa6
UW
4852/* Determine next PC after current instruction executes. Will call either
4853 arm_get_next_pc_raw or thumb_get_next_pc_raw. Error out if infinite
4854 loop is detected. */
4855
50e98be4
DJ
4856CORE_ADDR
4857arm_get_next_pc (struct frame_info *frame, CORE_ADDR pc)
4858{
18819fa6
UW
4859 CORE_ADDR nextpc;
4860
4861 if (arm_frame_is_thumb (frame))
4862 {
4863 nextpc = thumb_get_next_pc_raw (frame, pc);
4864 if (nextpc == MAKE_THUMB_ADDR (pc))
4865 error (_("Infinite loop detected"));
4866 }
4867 else
4868 {
4869 nextpc = arm_get_next_pc_raw (frame, pc);
4870 if (nextpc == pc)
4871 error (_("Infinite loop detected"));
4872 }
4873
50e98be4
DJ
4874 return nextpc;
4875}
4876
18819fa6
UW
4877/* Like insert_single_step_breakpoint, but make sure we use a breakpoint
4878 of the appropriate mode (as encoded in the PC value), even if this
4879 differs from what would be expected according to the symbol tables. */
4880
4881void
4882arm_insert_single_step_breakpoint (struct gdbarch *gdbarch,
4883 struct address_space *aspace,
4884 CORE_ADDR pc)
4885{
4886 struct cleanup *old_chain
4887 = make_cleanup_restore_integer (&arm_override_mode);
4888
4889 arm_override_mode = IS_THUMB_ADDR (pc);
4890 pc = gdbarch_addr_bits_remove (gdbarch, pc);
4891
4892 insert_single_step_breakpoint (gdbarch, aspace, pc);
4893
4894 do_cleanups (old_chain);
4895}
4896
9512d7fd
FN
4897/* single_step() is called just before we want to resume the inferior,
4898 if we want to single-step it but there is no hardware or kernel
4899 single-step support. We find the target of the coming instruction
e0cd558a 4900 and breakpoint it. */
9512d7fd 4901
190dce09 4902int
0b1b3e42 4903arm_software_single_step (struct frame_info *frame)
9512d7fd 4904{
a6d9a66e 4905 struct gdbarch *gdbarch = get_frame_arch (frame);
6c95b8df 4906 struct address_space *aspace = get_frame_address_space (frame);
0b1b3e42 4907 CORE_ADDR next_pc = arm_get_next_pc (frame, get_frame_pc (frame));
18819fa6
UW
4908
4909 arm_insert_single_step_breakpoint (gdbarch, aspace, next_pc);
e6590a1b
UW
4910
4911 return 1;
9512d7fd 4912}
9512d7fd 4913
f9d67f43
DJ
4914/* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
4915 the buffer to be NEW_LEN bytes ending at ENDADDR. Return
4916 NULL if an error occurs. BUF is freed. */
4917
4918static gdb_byte *
4919extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
4920 int old_len, int new_len)
4921{
4922 gdb_byte *new_buf, *middle;
4923 int bytes_to_read = new_len - old_len;
4924
4925 new_buf = xmalloc (new_len);
4926 memcpy (new_buf + bytes_to_read, buf, old_len);
4927 xfree (buf);
4928 if (target_read_memory (endaddr - new_len, new_buf, bytes_to_read) != 0)
4929 {
4930 xfree (new_buf);
4931 return NULL;
4932 }
4933 return new_buf;
4934}
4935
4936/* An IT block is at most the 2-byte IT instruction followed by
4937 four 4-byte instructions. The furthest back we must search to
4938 find an IT block that affects the current instruction is thus
4939 2 + 3 * 4 == 14 bytes. */
4940#define MAX_IT_BLOCK_PREFIX 14
4941
4942/* Use a quick scan if there are more than this many bytes of
4943 code. */
4944#define IT_SCAN_THRESHOLD 32
4945
4946/* Adjust a breakpoint's address to move breakpoints out of IT blocks.
4947 A breakpoint in an IT block may not be hit, depending on the
4948 condition flags. */
4949static CORE_ADDR
4950arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
4951{
4952 gdb_byte *buf;
4953 char map_type;
4954 CORE_ADDR boundary, func_start;
4955 int buf_len, buf2_len;
4956 enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
4957 int i, any, last_it, last_it_count;
4958
4959 /* If we are using BKPT breakpoints, none of this is necessary. */
4960 if (gdbarch_tdep (gdbarch)->thumb2_breakpoint == NULL)
4961 return bpaddr;
4962
4963 /* ARM mode does not have this problem. */
9779414d 4964 if (!arm_pc_is_thumb (gdbarch, bpaddr))
f9d67f43
DJ
4965 return bpaddr;
4966
4967 /* We are setting a breakpoint in Thumb code that could potentially
4968 contain an IT block. The first step is to find how much Thumb
4969 code there is; we do not need to read outside of known Thumb
4970 sequences. */
4971 map_type = arm_find_mapping_symbol (bpaddr, &boundary);
4972 if (map_type == 0)
4973 /* Thumb-2 code must have mapping symbols to have a chance. */
4974 return bpaddr;
4975
4976 bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
4977
4978 if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
4979 && func_start > boundary)
4980 boundary = func_start;
4981
4982 /* Search for a candidate IT instruction. We have to do some fancy
4983 footwork to distinguish a real IT instruction from the second
4984 half of a 32-bit instruction, but there is no need for that if
4985 there's no candidate. */
4986 buf_len = min (bpaddr - boundary, MAX_IT_BLOCK_PREFIX);
4987 if (buf_len == 0)
4988 /* No room for an IT instruction. */
4989 return bpaddr;
4990
4991 buf = xmalloc (buf_len);
4992 if (target_read_memory (bpaddr - buf_len, buf, buf_len) != 0)
4993 return bpaddr;
4994 any = 0;
4995 for (i = 0; i < buf_len; i += 2)
4996 {
4997 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
4998 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
4999 {
5000 any = 1;
5001 break;
5002 }
5003 }
5004 if (any == 0)
5005 {
5006 xfree (buf);
5007 return bpaddr;
5008 }
5009
5010 /* OK, the code bytes before this instruction contain at least one
5011 halfword which resembles an IT instruction. We know that it's
5012 Thumb code, but there are still two possibilities. Either the
5013 halfword really is an IT instruction, or it is the second half of
5014 a 32-bit Thumb instruction. The only way we can tell is to
5015 scan forwards from a known instruction boundary. */
5016 if (bpaddr - boundary > IT_SCAN_THRESHOLD)
5017 {
5018 int definite;
5019
5020 /* There's a lot of code before this instruction. Start with an
5021 optimistic search; it's easy to recognize halfwords that can
5022 not be the start of a 32-bit instruction, and use that to
5023 lock on to the instruction boundaries. */
5024 buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
5025 if (buf == NULL)
5026 return bpaddr;
5027 buf_len = IT_SCAN_THRESHOLD;
5028
5029 definite = 0;
5030 for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
5031 {
5032 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5033 if (thumb_insn_size (inst1) == 2)
5034 {
5035 definite = 1;
5036 break;
5037 }
5038 }
5039
5040 /* At this point, if DEFINITE, BUF[I] is the first place we
5041 are sure that we know the instruction boundaries, and it is far
5042 enough from BPADDR that we could not miss an IT instruction
5043 affecting BPADDR. If ! DEFINITE, give up - start from a
5044 known boundary. */
5045 if (! definite)
5046 {
0963b4bd
MS
5047 buf = extend_buffer_earlier (buf, bpaddr, buf_len,
5048 bpaddr - boundary);
f9d67f43
DJ
5049 if (buf == NULL)
5050 return bpaddr;
5051 buf_len = bpaddr - boundary;
5052 i = 0;
5053 }
5054 }
5055 else
5056 {
5057 buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
5058 if (buf == NULL)
5059 return bpaddr;
5060 buf_len = bpaddr - boundary;
5061 i = 0;
5062 }
5063
5064 /* Scan forwards. Find the last IT instruction before BPADDR. */
5065 last_it = -1;
5066 last_it_count = 0;
5067 while (i < buf_len)
5068 {
5069 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5070 last_it_count--;
5071 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5072 {
5073 last_it = i;
5074 if (inst1 & 0x0001)
5075 last_it_count = 4;
5076 else if (inst1 & 0x0002)
5077 last_it_count = 3;
5078 else if (inst1 & 0x0004)
5079 last_it_count = 2;
5080 else
5081 last_it_count = 1;
5082 }
5083 i += thumb_insn_size (inst1);
5084 }
5085
5086 xfree (buf);
5087
5088 if (last_it == -1)
5089 /* There wasn't really an IT instruction after all. */
5090 return bpaddr;
5091
5092 if (last_it_count < 1)
5093 /* It was too far away. */
5094 return bpaddr;
5095
5096 /* This really is a trouble spot. Move the breakpoint to the IT
5097 instruction. */
5098 return bpaddr - buf_len + last_it;
5099}
5100
cca44b1b 5101/* ARM displaced stepping support.
c906108c 5102
cca44b1b 5103 Generally ARM displaced stepping works as follows:
c906108c 5104
cca44b1b
JB
5105 1. When an instruction is to be single-stepped, it is first decoded by
5106 arm_process_displaced_insn (called from arm_displaced_step_copy_insn).
5107 Depending on the type of instruction, it is then copied to a scratch
5108 location, possibly in a modified form. The copy_* set of functions
0963b4bd 5109 performs such modification, as necessary. A breakpoint is placed after
cca44b1b
JB
5110 the modified instruction in the scratch space to return control to GDB.
5111 Note in particular that instructions which modify the PC will no longer
5112 do so after modification.
c5aa993b 5113
cca44b1b
JB
5114 2. The instruction is single-stepped, by setting the PC to the scratch
5115 location address, and resuming. Control returns to GDB when the
5116 breakpoint is hit.
c5aa993b 5117
cca44b1b
JB
5118 3. A cleanup function (cleanup_*) is called corresponding to the copy_*
5119 function used for the current instruction. This function's job is to
5120 put the CPU/memory state back to what it would have been if the
5121 instruction had been executed unmodified in its original location. */
c5aa993b 5122
cca44b1b
JB
5123/* NOP instruction (mov r0, r0). */
5124#define ARM_NOP 0xe1a00000
34518530 5125#define THUMB_NOP 0x4600
cca44b1b
JB
5126
5127/* Helper for register reads for displaced stepping. In particular, this
5128 returns the PC as it would be seen by the instruction at its original
5129 location. */
5130
5131ULONGEST
36073a92
YQ
5132displaced_read_reg (struct regcache *regs, struct displaced_step_closure *dsc,
5133 int regno)
cca44b1b
JB
5134{
5135 ULONGEST ret;
36073a92 5136 CORE_ADDR from = dsc->insn_addr;
cca44b1b 5137
bf9f652a 5138 if (regno == ARM_PC_REGNUM)
cca44b1b 5139 {
4db71c0b
YQ
5140 /* Compute pipeline offset:
5141 - When executing an ARM instruction, PC reads as the address of the
5142 current instruction plus 8.
5143 - When executing a Thumb instruction, PC reads as the address of the
5144 current instruction plus 4. */
5145
36073a92 5146 if (!dsc->is_thumb)
4db71c0b
YQ
5147 from += 8;
5148 else
5149 from += 4;
5150
cca44b1b
JB
5151 if (debug_displaced)
5152 fprintf_unfiltered (gdb_stdlog, "displaced: read pc value %.8lx\n",
4db71c0b
YQ
5153 (unsigned long) from);
5154 return (ULONGEST) from;
cca44b1b 5155 }
c906108c 5156 else
cca44b1b
JB
5157 {
5158 regcache_cooked_read_unsigned (regs, regno, &ret);
5159 if (debug_displaced)
5160 fprintf_unfiltered (gdb_stdlog, "displaced: read r%d value %.8lx\n",
5161 regno, (unsigned long) ret);
5162 return ret;
5163 }
c906108c
SS
5164}
5165
cca44b1b
JB
5166static int
5167displaced_in_arm_mode (struct regcache *regs)
5168{
5169 ULONGEST ps;
9779414d 5170 ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
66e810cd 5171
cca44b1b 5172 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
66e810cd 5173
9779414d 5174 return (ps & t_bit) == 0;
cca44b1b 5175}
66e810cd 5176
cca44b1b 5177/* Write to the PC as from a branch instruction. */
c906108c 5178
cca44b1b 5179static void
36073a92
YQ
5180branch_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5181 ULONGEST val)
c906108c 5182{
36073a92 5183 if (!dsc->is_thumb)
cca44b1b
JB
5184 /* Note: If bits 0/1 are set, this branch would be unpredictable for
5185 architecture versions < 6. */
0963b4bd
MS
5186 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5187 val & ~(ULONGEST) 0x3);
cca44b1b 5188 else
0963b4bd
MS
5189 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5190 val & ~(ULONGEST) 0x1);
cca44b1b 5191}
66e810cd 5192
cca44b1b
JB
5193/* Write to the PC as from a branch-exchange instruction. */
5194
5195static void
5196bx_write_pc (struct regcache *regs, ULONGEST val)
5197{
5198 ULONGEST ps;
9779414d 5199 ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
cca44b1b
JB
5200
5201 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
5202
5203 if ((val & 1) == 1)
c906108c 5204 {
9779414d 5205 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
cca44b1b
JB
5206 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
5207 }
5208 else if ((val & 2) == 0)
5209 {
9779414d 5210 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 5211 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
c906108c
SS
5212 }
5213 else
5214 {
cca44b1b
JB
5215 /* Unpredictable behaviour. Try to do something sensible (switch to ARM
5216 mode, align dest to 4 bytes). */
5217 warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
9779414d 5218 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 5219 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
c906108c
SS
5220 }
5221}
ed9a39eb 5222
cca44b1b 5223/* Write to the PC as if from a load instruction. */
ed9a39eb 5224
34e8f22d 5225static void
36073a92
YQ
5226load_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5227 ULONGEST val)
ed9a39eb 5228{
cca44b1b
JB
5229 if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
5230 bx_write_pc (regs, val);
5231 else
36073a92 5232 branch_write_pc (regs, dsc, val);
cca44b1b 5233}
be8626e0 5234
cca44b1b
JB
5235/* Write to the PC as if from an ALU instruction. */
5236
5237static void
36073a92
YQ
5238alu_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5239 ULONGEST val)
cca44b1b 5240{
36073a92 5241 if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
cca44b1b
JB
5242 bx_write_pc (regs, val);
5243 else
36073a92 5244 branch_write_pc (regs, dsc, val);
cca44b1b
JB
5245}
5246
5247/* Helper for writing to registers for displaced stepping. Writing to the PC
5248 has a varying effects depending on the instruction which does the write:
5249 this is controlled by the WRITE_PC argument. */
5250
5251void
5252displaced_write_reg (struct regcache *regs, struct displaced_step_closure *dsc,
5253 int regno, ULONGEST val, enum pc_write_style write_pc)
5254{
bf9f652a 5255 if (regno == ARM_PC_REGNUM)
08216dd7 5256 {
cca44b1b
JB
5257 if (debug_displaced)
5258 fprintf_unfiltered (gdb_stdlog, "displaced: writing pc %.8lx\n",
5259 (unsigned long) val);
5260 switch (write_pc)
08216dd7 5261 {
cca44b1b 5262 case BRANCH_WRITE_PC:
36073a92 5263 branch_write_pc (regs, dsc, val);
08216dd7
RE
5264 break;
5265
cca44b1b
JB
5266 case BX_WRITE_PC:
5267 bx_write_pc (regs, val);
5268 break;
5269
5270 case LOAD_WRITE_PC:
36073a92 5271 load_write_pc (regs, dsc, val);
cca44b1b
JB
5272 break;
5273
5274 case ALU_WRITE_PC:
36073a92 5275 alu_write_pc (regs, dsc, val);
cca44b1b
JB
5276 break;
5277
5278 case CANNOT_WRITE_PC:
5279 warning (_("Instruction wrote to PC in an unexpected way when "
5280 "single-stepping"));
08216dd7
RE
5281 break;
5282
5283 default:
97b9747c
JB
5284 internal_error (__FILE__, __LINE__,
5285 _("Invalid argument to displaced_write_reg"));
08216dd7 5286 }
b508a996 5287
cca44b1b 5288 dsc->wrote_to_pc = 1;
b508a996 5289 }
ed9a39eb 5290 else
b508a996 5291 {
cca44b1b
JB
5292 if (debug_displaced)
5293 fprintf_unfiltered (gdb_stdlog, "displaced: writing r%d value %.8lx\n",
5294 regno, (unsigned long) val);
5295 regcache_cooked_write_unsigned (regs, regno, val);
b508a996 5296 }
34e8f22d
RE
5297}
5298
cca44b1b
JB
5299/* This function is used to concisely determine if an instruction INSN
5300 references PC. Register fields of interest in INSN should have the
0963b4bd
MS
5301 corresponding fields of BITMASK set to 0b1111. The function
5302 returns return 1 if any of these fields in INSN reference the PC
5303 (also 0b1111, r15), else it returns 0. */
67255d04
RE
5304
5305static int
cca44b1b 5306insn_references_pc (uint32_t insn, uint32_t bitmask)
67255d04 5307{
cca44b1b 5308 uint32_t lowbit = 1;
67255d04 5309
cca44b1b
JB
5310 while (bitmask != 0)
5311 {
5312 uint32_t mask;
44e1a9eb 5313
cca44b1b
JB
5314 for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
5315 ;
67255d04 5316
cca44b1b
JB
5317 if (!lowbit)
5318 break;
67255d04 5319
cca44b1b 5320 mask = lowbit * 0xf;
67255d04 5321
cca44b1b
JB
5322 if ((insn & mask) == mask)
5323 return 1;
5324
5325 bitmask &= ~mask;
67255d04
RE
5326 }
5327
cca44b1b
JB
5328 return 0;
5329}
2af48f68 5330
cca44b1b
JB
5331/* The simplest copy function. Many instructions have the same effect no
5332 matter what address they are executed at: in those cases, use this. */
67255d04 5333
cca44b1b 5334static int
7ff120b4
YQ
5335arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn,
5336 const char *iname, struct displaced_step_closure *dsc)
cca44b1b
JB
5337{
5338 if (debug_displaced)
5339 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx, "
5340 "opcode/class '%s' unmodified\n", (unsigned long) insn,
5341 iname);
67255d04 5342
cca44b1b 5343 dsc->modinsn[0] = insn;
67255d04 5344
cca44b1b
JB
5345 return 0;
5346}
5347
34518530
YQ
5348static int
5349thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
5350 uint16_t insn2, const char *iname,
5351 struct displaced_step_closure *dsc)
5352{
5353 if (debug_displaced)
5354 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x %.4x, "
5355 "opcode/class '%s' unmodified\n", insn1, insn2,
5356 iname);
5357
5358 dsc->modinsn[0] = insn1;
5359 dsc->modinsn[1] = insn2;
5360 dsc->numinsns = 2;
5361
5362 return 0;
5363}
5364
5365/* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
5366 modification. */
5367static int
5368thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, unsigned int insn,
5369 const char *iname,
5370 struct displaced_step_closure *dsc)
5371{
5372 if (debug_displaced)
5373 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x, "
5374 "opcode/class '%s' unmodified\n", insn,
5375 iname);
5376
5377 dsc->modinsn[0] = insn;
5378
5379 return 0;
5380}
5381
cca44b1b
JB
5382/* Preload instructions with immediate offset. */
5383
5384static void
6e39997a 5385cleanup_preload (struct gdbarch *gdbarch,
cca44b1b
JB
5386 struct regcache *regs, struct displaced_step_closure *dsc)
5387{
5388 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5389 if (!dsc->u.preload.immed)
5390 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5391}
5392
7ff120b4
YQ
5393static void
5394install_preload (struct gdbarch *gdbarch, struct regcache *regs,
5395 struct displaced_step_closure *dsc, unsigned int rn)
cca44b1b 5396{
cca44b1b 5397 ULONGEST rn_val;
cca44b1b
JB
5398 /* Preload instructions:
5399
5400 {pli/pld} [rn, #+/-imm]
5401 ->
5402 {pli/pld} [r0, #+/-imm]. */
5403
36073a92
YQ
5404 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5405 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 5406 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
cca44b1b
JB
5407 dsc->u.preload.immed = 1;
5408
cca44b1b 5409 dsc->cleanup = &cleanup_preload;
cca44b1b
JB
5410}
5411
cca44b1b 5412static int
7ff120b4 5413arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
cca44b1b
JB
5414 struct displaced_step_closure *dsc)
5415{
5416 unsigned int rn = bits (insn, 16, 19);
cca44b1b 5417
7ff120b4
YQ
5418 if (!insn_references_pc (insn, 0x000f0000ul))
5419 return arm_copy_unmodified (gdbarch, insn, "preload", dsc);
cca44b1b
JB
5420
5421 if (debug_displaced)
5422 fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
5423 (unsigned long) insn);
5424
7ff120b4
YQ
5425 dsc->modinsn[0] = insn & 0xfff0ffff;
5426
5427 install_preload (gdbarch, regs, dsc, rn);
5428
5429 return 0;
5430}
5431
34518530
YQ
5432static int
5433thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
5434 struct regcache *regs, struct displaced_step_closure *dsc)
5435{
5436 unsigned int rn = bits (insn1, 0, 3);
5437 unsigned int u_bit = bit (insn1, 7);
5438 int imm12 = bits (insn2, 0, 11);
5439 ULONGEST pc_val;
5440
5441 if (rn != ARM_PC_REGNUM)
5442 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);
5443
5444 /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
5445 PLD (literal) Encoding T1. */
5446 if (debug_displaced)
5447 fprintf_unfiltered (gdb_stdlog,
5448 "displaced: copying pld/pli pc (0x%x) %c imm12 %.4x\n",
5449 (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
5450 imm12);
5451
5452 if (!u_bit)
5453 imm12 = -1 * imm12;
5454
5455 /* Rewrite instruction {pli/pld} PC imm12 into:
5456 Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12
5457
5458 {pli/pld} [r0, r1]
5459
5460 Cleanup: r0 <- tmp[0], r1 <- tmp[1]. */
5461
5462 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5463 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5464
5465 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
5466
5467 displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
5468 displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
5469 dsc->u.preload.immed = 0;
5470
5471 /* {pli/pld} [r0, r1] */
5472 dsc->modinsn[0] = insn1 & 0xfff0;
5473 dsc->modinsn[1] = 0xf001;
5474 dsc->numinsns = 2;
5475
5476 dsc->cleanup = &cleanup_preload;
5477 return 0;
5478}
5479
7ff120b4
YQ
5480/* Preload instructions with register offset. */
5481
5482static void
5483install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
5484 struct displaced_step_closure *dsc, unsigned int rn,
5485 unsigned int rm)
5486{
5487 ULONGEST rn_val, rm_val;
5488
cca44b1b
JB
5489 /* Preload register-offset instructions:
5490
5491 {pli/pld} [rn, rm {, shift}]
5492 ->
5493 {pli/pld} [r0, r1 {, shift}]. */
5494
36073a92
YQ
5495 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5496 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5497 rn_val = displaced_read_reg (regs, dsc, rn);
5498 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5499 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5500 displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
cca44b1b
JB
5501 dsc->u.preload.immed = 0;
5502
cca44b1b 5503 dsc->cleanup = &cleanup_preload;
7ff120b4
YQ
5504}
5505
5506static int
5507arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
5508 struct regcache *regs,
5509 struct displaced_step_closure *dsc)
5510{
5511 unsigned int rn = bits (insn, 16, 19);
5512 unsigned int rm = bits (insn, 0, 3);
5513
5514
5515 if (!insn_references_pc (insn, 0x000f000ful))
5516 return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);
5517
5518 if (debug_displaced)
5519 fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
5520 (unsigned long) insn);
5521
5522 dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
cca44b1b 5523
7ff120b4 5524 install_preload_reg (gdbarch, regs, dsc, rn, rm);
cca44b1b
JB
5525 return 0;
5526}
5527
5528/* Copy/cleanup coprocessor load and store instructions. */
5529
5530static void
6e39997a 5531cleanup_copro_load_store (struct gdbarch *gdbarch,
cca44b1b
JB
5532 struct regcache *regs,
5533 struct displaced_step_closure *dsc)
5534{
36073a92 5535 ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5536
5537 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5538
5539 if (dsc->u.ldst.writeback)
5540 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
5541}
5542
7ff120b4
YQ
5543static void
5544install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
5545 struct displaced_step_closure *dsc,
5546 int writeback, unsigned int rn)
cca44b1b 5547{
cca44b1b 5548 ULONGEST rn_val;
cca44b1b 5549
cca44b1b
JB
5550 /* Coprocessor load/store instructions:
5551
5552 {stc/stc2} [<Rn>, #+/-imm] (and other immediate addressing modes)
5553 ->
5554 {stc/stc2} [r0, #+/-imm].
5555
5556 ldc/ldc2 are handled identically. */
5557
36073a92
YQ
5558 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5559 rn_val = displaced_read_reg (regs, dsc, rn);
2b16b2e3
YQ
5560 /* PC should be 4-byte aligned. */
5561 rn_val = rn_val & 0xfffffffc;
cca44b1b
JB
5562 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5563
7ff120b4 5564 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
5565 dsc->u.ldst.rn = rn;
5566
7ff120b4
YQ
5567 dsc->cleanup = &cleanup_copro_load_store;
5568}
5569
5570static int
5571arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
5572 struct regcache *regs,
5573 struct displaced_step_closure *dsc)
5574{
5575 unsigned int rn = bits (insn, 16, 19);
5576
5577 if (!insn_references_pc (insn, 0x000f0000ul))
5578 return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);
5579
5580 if (debug_displaced)
5581 fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
5582 "load/store insn %.8lx\n", (unsigned long) insn);
5583
cca44b1b
JB
5584 dsc->modinsn[0] = insn & 0xfff0ffff;
5585
7ff120b4 5586 install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);
cca44b1b
JB
5587
5588 return 0;
5589}
5590
34518530
YQ
5591static int
5592thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
5593 uint16_t insn2, struct regcache *regs,
5594 struct displaced_step_closure *dsc)
5595{
5596 unsigned int rn = bits (insn1, 0, 3);
5597
5598 if (rn != ARM_PC_REGNUM)
5599 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
5600 "copro load/store", dsc);
5601
5602 if (debug_displaced)
5603 fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
5604 "load/store insn %.4x%.4x\n", insn1, insn2);
5605
5606 dsc->modinsn[0] = insn1 & 0xfff0;
5607 dsc->modinsn[1] = insn2;
5608 dsc->numinsns = 2;
5609
5610 /* This function is called for copying instruction LDC/LDC2/VLDR, which
5611 doesn't support writeback, so pass 0. */
5612 install_copro_load_store (gdbarch, regs, dsc, 0, rn);
5613
5614 return 0;
5615}
5616
cca44b1b
JB
5617/* Clean up branch instructions (actually perform the branch, by setting
5618 PC). */
5619
5620static void
6e39997a 5621cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
5622 struct displaced_step_closure *dsc)
5623{
36073a92 5624 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
5625 int branch_taken = condition_true (dsc->u.branch.cond, status);
5626 enum pc_write_style write_pc = dsc->u.branch.exchange
5627 ? BX_WRITE_PC : BRANCH_WRITE_PC;
5628
5629 if (!branch_taken)
5630 return;
5631
5632 if (dsc->u.branch.link)
5633 {
8c8dba6d
YQ
5634 /* The value of LR should be the next insn of current one. In order
5635 not to confuse logic hanlding later insn `bx lr', if current insn mode
5636 is Thumb, the bit 0 of LR value should be set to 1. */
5637 ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;
5638
5639 if (dsc->is_thumb)
5640 next_insn_addr |= 0x1;
5641
5642 displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
5643 CANNOT_WRITE_PC);
cca44b1b
JB
5644 }
5645
bf9f652a 5646 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
cca44b1b
JB
5647}
5648
5649/* Copy B/BL/BLX instructions with immediate destinations. */
5650
7ff120b4
YQ
5651static void
5652install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
5653 struct displaced_step_closure *dsc,
5654 unsigned int cond, int exchange, int link, long offset)
5655{
5656 /* Implement "BL<cond> <label>" as:
5657
5658 Preparation: cond <- instruction condition
5659 Insn: mov r0, r0 (nop)
5660 Cleanup: if (condition true) { r14 <- pc; pc <- label }.
5661
5662 B<cond> similar, but don't set r14 in cleanup. */
5663
5664 dsc->u.branch.cond = cond;
5665 dsc->u.branch.link = link;
5666 dsc->u.branch.exchange = exchange;
5667
2b16b2e3
YQ
5668 dsc->u.branch.dest = dsc->insn_addr;
5669 if (link && exchange)
5670 /* For BLX, offset is computed from the Align (PC, 4). */
5671 dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;
5672
7ff120b4 5673 if (dsc->is_thumb)
2b16b2e3 5674 dsc->u.branch.dest += 4 + offset;
7ff120b4 5675 else
2b16b2e3 5676 dsc->u.branch.dest += 8 + offset;
7ff120b4
YQ
5677
5678 dsc->cleanup = &cleanup_branch;
5679}
cca44b1b 5680static int
7ff120b4
YQ
5681arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
5682 struct regcache *regs, struct displaced_step_closure *dsc)
cca44b1b
JB
5683{
5684 unsigned int cond = bits (insn, 28, 31);
5685 int exchange = (cond == 0xf);
5686 int link = exchange || bit (insn, 24);
cca44b1b
JB
5687 long offset;
5688
5689 if (debug_displaced)
5690 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s immediate insn "
5691 "%.8lx\n", (exchange) ? "blx" : (link) ? "bl" : "b",
5692 (unsigned long) insn);
cca44b1b
JB
5693 if (exchange)
5694 /* For BLX, set bit 0 of the destination. The cleanup_branch function will
5695 then arrange the switch into Thumb mode. */
5696 offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
5697 else
5698 offset = bits (insn, 0, 23) << 2;
5699
5700 if (bit (offset, 25))
5701 offset = offset | ~0x3ffffff;
5702
cca44b1b
JB
5703 dsc->modinsn[0] = ARM_NOP;
5704
7ff120b4 5705 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
cca44b1b
JB
5706 return 0;
5707}
5708
34518530
YQ
5709static int
5710thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
5711 uint16_t insn2, struct regcache *regs,
5712 struct displaced_step_closure *dsc)
5713{
5714 int link = bit (insn2, 14);
5715 int exchange = link && !bit (insn2, 12);
5716 int cond = INST_AL;
5717 long offset = 0;
5718 int j1 = bit (insn2, 13);
5719 int j2 = bit (insn2, 11);
5720 int s = sbits (insn1, 10, 10);
5721 int i1 = !(j1 ^ bit (insn1, 10));
5722 int i2 = !(j2 ^ bit (insn1, 10));
5723
5724 if (!link && !exchange) /* B */
5725 {
5726 offset = (bits (insn2, 0, 10) << 1);
5727 if (bit (insn2, 12)) /* Encoding T4 */
5728 {
5729 offset |= (bits (insn1, 0, 9) << 12)
5730 | (i2 << 22)
5731 | (i1 << 23)
5732 | (s << 24);
5733 cond = INST_AL;
5734 }
5735 else /* Encoding T3 */
5736 {
5737 offset |= (bits (insn1, 0, 5) << 12)
5738 | (j1 << 18)
5739 | (j2 << 19)
5740 | (s << 20);
5741 cond = bits (insn1, 6, 9);
5742 }
5743 }
5744 else
5745 {
5746 offset = (bits (insn1, 0, 9) << 12);
5747 offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
5748 offset |= exchange ?
5749 (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
5750 }
5751
5752 if (debug_displaced)
5753 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s insn "
5754 "%.4x %.4x with offset %.8lx\n",
5755 link ? (exchange) ? "blx" : "bl" : "b",
5756 insn1, insn2, offset);
5757
5758 dsc->modinsn[0] = THUMB_NOP;
5759
5760 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
5761 return 0;
5762}
5763
5764/* Copy B Thumb instructions. */
5765static int
5766thumb_copy_b (struct gdbarch *gdbarch, unsigned short insn,
5767 struct displaced_step_closure *dsc)
5768{
5769 unsigned int cond = 0;
5770 int offset = 0;
5771 unsigned short bit_12_15 = bits (insn, 12, 15);
5772 CORE_ADDR from = dsc->insn_addr;
5773
5774 if (bit_12_15 == 0xd)
5775 {
5776 /* offset = SignExtend (imm8:0, 32) */
5777 offset = sbits ((insn << 1), 0, 8);
5778 cond = bits (insn, 8, 11);
5779 }
5780 else if (bit_12_15 == 0xe) /* Encoding T2 */
5781 {
5782 offset = sbits ((insn << 1), 0, 11);
5783 cond = INST_AL;
5784 }
5785
5786 if (debug_displaced)
5787 fprintf_unfiltered (gdb_stdlog,
5788 "displaced: copying b immediate insn %.4x "
5789 "with offset %d\n", insn, offset);
5790
5791 dsc->u.branch.cond = cond;
5792 dsc->u.branch.link = 0;
5793 dsc->u.branch.exchange = 0;
5794 dsc->u.branch.dest = from + 4 + offset;
5795
5796 dsc->modinsn[0] = THUMB_NOP;
5797
5798 dsc->cleanup = &cleanup_branch;
5799
5800 return 0;
5801}
5802
cca44b1b
JB
5803/* Copy BX/BLX with register-specified destinations. */
5804
7ff120b4
YQ
5805static void
5806install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
5807 struct displaced_step_closure *dsc, int link,
5808 unsigned int cond, unsigned int rm)
cca44b1b 5809{
cca44b1b
JB
5810 /* Implement {BX,BLX}<cond> <reg>" as:
5811
5812 Preparation: cond <- instruction condition
5813 Insn: mov r0, r0 (nop)
5814 Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
5815
5816 Don't set r14 in cleanup for BX. */
5817
36073a92 5818 dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5819
5820 dsc->u.branch.cond = cond;
5821 dsc->u.branch.link = link;
cca44b1b 5822
7ff120b4 5823 dsc->u.branch.exchange = 1;
cca44b1b
JB
5824
5825 dsc->cleanup = &cleanup_branch;
7ff120b4 5826}
cca44b1b 5827
7ff120b4
YQ
5828static int
5829arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
5830 struct regcache *regs, struct displaced_step_closure *dsc)
5831{
5832 unsigned int cond = bits (insn, 28, 31);
5833 /* BX: x12xxx1x
5834 BLX: x12xxx3x. */
5835 int link = bit (insn, 5);
5836 unsigned int rm = bits (insn, 0, 3);
5837
5838 if (debug_displaced)
5839 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx",
5840 (unsigned long) insn);
5841
5842 dsc->modinsn[0] = ARM_NOP;
5843
5844 install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
cca44b1b
JB
5845 return 0;
5846}
5847
34518530
YQ
5848static int
5849thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
5850 struct regcache *regs,
5851 struct displaced_step_closure *dsc)
5852{
5853 int link = bit (insn, 7);
5854 unsigned int rm = bits (insn, 3, 6);
5855
5856 if (debug_displaced)
5857 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x",
5858 (unsigned short) insn);
5859
5860 dsc->modinsn[0] = THUMB_NOP;
5861
5862 install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);
5863
5864 return 0;
5865}
5866
5867
0963b4bd 5868/* Copy/cleanup arithmetic/logic instruction with immediate RHS. */
cca44b1b
JB
5869
5870static void
6e39997a 5871cleanup_alu_imm (struct gdbarch *gdbarch,
cca44b1b
JB
5872 struct regcache *regs, struct displaced_step_closure *dsc)
5873{
36073a92 5874 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5875 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5876 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5877 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
5878}
5879
5880static int
7ff120b4
YQ
5881arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
5882 struct displaced_step_closure *dsc)
cca44b1b
JB
5883{
5884 unsigned int rn = bits (insn, 16, 19);
5885 unsigned int rd = bits (insn, 12, 15);
5886 unsigned int op = bits (insn, 21, 24);
5887 int is_mov = (op == 0xd);
5888 ULONGEST rd_val, rn_val;
cca44b1b
JB
5889
5890 if (!insn_references_pc (insn, 0x000ff000ul))
7ff120b4 5891 return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
cca44b1b
JB
5892
5893 if (debug_displaced)
5894 fprintf_unfiltered (gdb_stdlog, "displaced: copying immediate %s insn "
5895 "%.8lx\n", is_mov ? "move" : "ALU",
5896 (unsigned long) insn);
5897
5898 /* Instruction is of form:
5899
5900 <op><cond> rd, [rn,] #imm
5901
5902 Rewrite as:
5903
5904 Preparation: tmp1, tmp2 <- r0, r1;
5905 r0, r1 <- rd, rn
5906 Insn: <op><cond> r0, r1, #imm
5907 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
5908 */
5909
36073a92
YQ
5910 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5911 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5912 rn_val = displaced_read_reg (regs, dsc, rn);
5913 rd_val = displaced_read_reg (regs, dsc, rd);
cca44b1b
JB
5914 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5915 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5916 dsc->rd = rd;
5917
5918 if (is_mov)
5919 dsc->modinsn[0] = insn & 0xfff00fff;
5920 else
5921 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
5922
5923 dsc->cleanup = &cleanup_alu_imm;
5924
5925 return 0;
5926}
5927
34518530
YQ
5928static int
5929thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
5930 uint16_t insn2, struct regcache *regs,
5931 struct displaced_step_closure *dsc)
5932{
5933 unsigned int op = bits (insn1, 5, 8);
5934 unsigned int rn, rm, rd;
5935 ULONGEST rd_val, rn_val;
5936
5937 rn = bits (insn1, 0, 3); /* Rn */
5938 rm = bits (insn2, 0, 3); /* Rm */
5939 rd = bits (insn2, 8, 11); /* Rd */
5940
5941 /* This routine is only called for instruction MOV. */
5942 gdb_assert (op == 0x2 && rn == 0xf);
5943
5944 if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
5945 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);
5946
5947 if (debug_displaced)
5948 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.4x%.4x\n",
5949 "ALU", insn1, insn2);
5950
5951 /* Instruction is of form:
5952
5953 <op><cond> rd, [rn,] #imm
5954
5955 Rewrite as:
5956
5957 Preparation: tmp1, tmp2 <- r0, r1;
5958 r0, r1 <- rd, rn
5959 Insn: <op><cond> r0, r1, #imm
5960 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
5961 */
5962
5963 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5964 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5965 rn_val = displaced_read_reg (regs, dsc, rn);
5966 rd_val = displaced_read_reg (regs, dsc, rd);
5967 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5968 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5969 dsc->rd = rd;
5970
5971 dsc->modinsn[0] = insn1;
5972 dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
5973 dsc->numinsns = 2;
5974
5975 dsc->cleanup = &cleanup_alu_imm;
5976
5977 return 0;
5978}
5979
cca44b1b
JB
5980/* Copy/cleanup arithmetic/logic insns with register RHS. */
5981
5982static void
6e39997a 5983cleanup_alu_reg (struct gdbarch *gdbarch,
cca44b1b
JB
5984 struct regcache *regs, struct displaced_step_closure *dsc)
5985{
5986 ULONGEST rd_val;
5987 int i;
5988
36073a92 5989 rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5990
5991 for (i = 0; i < 3; i++)
5992 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
5993
5994 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
5995}
5996
7ff120b4
YQ
5997static void
5998install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
5999 struct displaced_step_closure *dsc,
6000 unsigned int rd, unsigned int rn, unsigned int rm)
cca44b1b 6001{
cca44b1b 6002 ULONGEST rd_val, rn_val, rm_val;
cca44b1b 6003
cca44b1b
JB
6004 /* Instruction is of form:
6005
6006 <op><cond> rd, [rn,] rm [, <shift>]
6007
6008 Rewrite as:
6009
6010 Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
6011 r0, r1, r2 <- rd, rn, rm
6012 Insn: <op><cond> r0, r1, r2 [, <shift>]
6013 Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
6014 */
6015
36073a92
YQ
6016 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6017 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6018 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6019 rd_val = displaced_read_reg (regs, dsc, rd);
6020 rn_val = displaced_read_reg (regs, dsc, rn);
6021 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6022 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6023 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6024 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6025 dsc->rd = rd;
6026
7ff120b4
YQ
6027 dsc->cleanup = &cleanup_alu_reg;
6028}
6029
6030static int
6031arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
6032 struct displaced_step_closure *dsc)
6033{
6034 unsigned int op = bits (insn, 21, 24);
6035 int is_mov = (op == 0xd);
6036
6037 if (!insn_references_pc (insn, 0x000ff00ful))
6038 return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);
6039
6040 if (debug_displaced)
6041 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.8lx\n",
6042 is_mov ? "move" : "ALU", (unsigned long) insn);
6043
cca44b1b
JB
6044 if (is_mov)
6045 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
6046 else
6047 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
6048
7ff120b4
YQ
6049 install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
6050 bits (insn, 0, 3));
cca44b1b
JB
6051 return 0;
6052}
6053
34518530
YQ
6054static int
6055thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
6056 struct regcache *regs,
6057 struct displaced_step_closure *dsc)
6058{
6059 unsigned rn, rm, rd;
6060
6061 rd = bits (insn, 3, 6);
6062 rn = (bit (insn, 7) << 3) | bits (insn, 0, 2);
6063 rm = 2;
6064
6065 if (rd != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
6066 return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);
6067
6068 if (debug_displaced)
6069 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.4x\n",
6070 "ALU", (unsigned short) insn);
6071
6072 dsc->modinsn[0] = ((insn & 0xff00) | 0x08);
6073
6074 install_alu_reg (gdbarch, regs, dsc, rd, rn, rm);
6075
6076 return 0;
6077}
6078
cca44b1b
JB
6079/* Cleanup/copy arithmetic/logic insns with shifted register RHS. */
6080
6081static void
6e39997a 6082cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
cca44b1b
JB
6083 struct regcache *regs,
6084 struct displaced_step_closure *dsc)
6085{
36073a92 6086 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6087 int i;
6088
6089 for (i = 0; i < 4; i++)
6090 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6091
6092 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6093}
6094
7ff120b4
YQ
6095static void
6096install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
6097 struct displaced_step_closure *dsc,
6098 unsigned int rd, unsigned int rn, unsigned int rm,
6099 unsigned rs)
cca44b1b 6100{
7ff120b4 6101 int i;
cca44b1b 6102 ULONGEST rd_val, rn_val, rm_val, rs_val;
cca44b1b 6103
cca44b1b
JB
6104 /* Instruction is of form:
6105
6106 <op><cond> rd, [rn,] rm, <shift> rs
6107
6108 Rewrite as:
6109
6110 Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
6111 r0, r1, r2, r3 <- rd, rn, rm, rs
6112 Insn: <op><cond> r0, r1, r2, <shift> r3
6113 Cleanup: tmp5 <- r0
6114 r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
6115 rd <- tmp5
6116 */
6117
6118 for (i = 0; i < 4; i++)
36073a92 6119 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b 6120
36073a92
YQ
6121 rd_val = displaced_read_reg (regs, dsc, rd);
6122 rn_val = displaced_read_reg (regs, dsc, rn);
6123 rm_val = displaced_read_reg (regs, dsc, rm);
6124 rs_val = displaced_read_reg (regs, dsc, rs);
cca44b1b
JB
6125 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6126 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6127 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6128 displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
6129 dsc->rd = rd;
7ff120b4
YQ
6130 dsc->cleanup = &cleanup_alu_shifted_reg;
6131}
6132
6133static int
6134arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
6135 struct regcache *regs,
6136 struct displaced_step_closure *dsc)
6137{
6138 unsigned int op = bits (insn, 21, 24);
6139 int is_mov = (op == 0xd);
6140 unsigned int rd, rn, rm, rs;
6141
6142 if (!insn_references_pc (insn, 0x000fff0ful))
6143 return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
6144
6145 if (debug_displaced)
6146 fprintf_unfiltered (gdb_stdlog, "displaced: copying shifted reg %s insn "
6147 "%.8lx\n", is_mov ? "move" : "ALU",
6148 (unsigned long) insn);
6149
6150 rn = bits (insn, 16, 19);
6151 rm = bits (insn, 0, 3);
6152 rs = bits (insn, 8, 11);
6153 rd = bits (insn, 12, 15);
cca44b1b
JB
6154
6155 if (is_mov)
6156 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
6157 else
6158 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
6159
7ff120b4 6160 install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);
cca44b1b
JB
6161
6162 return 0;
6163}
6164
6165/* Clean up load instructions. */
6166
6167static void
6e39997a 6168cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
6169 struct displaced_step_closure *dsc)
6170{
6171 ULONGEST rt_val, rt_val2 = 0, rn_val;
cca44b1b 6172
36073a92 6173 rt_val = displaced_read_reg (regs, dsc, 0);
cca44b1b 6174 if (dsc->u.ldst.xfersize == 8)
36073a92
YQ
6175 rt_val2 = displaced_read_reg (regs, dsc, 1);
6176 rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
6177
6178 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6179 if (dsc->u.ldst.xfersize > 4)
6180 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6181 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6182 if (!dsc->u.ldst.immed)
6183 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6184
6185 /* Handle register writeback. */
6186 if (dsc->u.ldst.writeback)
6187 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6188 /* Put result in right place. */
6189 displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
6190 if (dsc->u.ldst.xfersize == 8)
6191 displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
6192}
6193
6194/* Clean up store instructions. */
6195
6196static void
6e39997a 6197cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
6198 struct displaced_step_closure *dsc)
6199{
36073a92 6200 ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
6201
6202 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6203 if (dsc->u.ldst.xfersize > 4)
6204 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6205 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6206 if (!dsc->u.ldst.immed)
6207 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6208 if (!dsc->u.ldst.restore_r4)
6209 displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
6210
6211 /* Writeback. */
6212 if (dsc->u.ldst.writeback)
6213 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6214}
6215
6216/* Copy "extra" load/store instructions. These are halfword/doubleword
6217 transfers, which have a different encoding to byte/word transfers. */
6218
6219static int
7ff120b4
YQ
6220arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unpriveleged,
6221 struct regcache *regs, struct displaced_step_closure *dsc)
cca44b1b
JB
6222{
6223 unsigned int op1 = bits (insn, 20, 24);
6224 unsigned int op2 = bits (insn, 5, 6);
6225 unsigned int rt = bits (insn, 12, 15);
6226 unsigned int rn = bits (insn, 16, 19);
6227 unsigned int rm = bits (insn, 0, 3);
6228 char load[12] = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
6229 char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
6230 int immed = (op1 & 0x4) != 0;
6231 int opcode;
6232 ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
cca44b1b
JB
6233
6234 if (!insn_references_pc (insn, 0x000ff00ful))
7ff120b4 6235 return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);
cca44b1b
JB
6236
6237 if (debug_displaced)
6238 fprintf_unfiltered (gdb_stdlog, "displaced: copying %sextra load/store "
6239 "insn %.8lx\n", unpriveleged ? "unpriveleged " : "",
6240 (unsigned long) insn);
6241
6242 opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
6243
6244 if (opcode < 0)
6245 internal_error (__FILE__, __LINE__,
6246 _("copy_extra_ld_st: instruction decode error"));
6247
36073a92
YQ
6248 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6249 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6250 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 6251 if (!immed)
36073a92 6252 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 6253
36073a92 6254 rt_val = displaced_read_reg (regs, dsc, rt);
cca44b1b 6255 if (bytesize[opcode] == 8)
36073a92
YQ
6256 rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
6257 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 6258 if (!immed)
36073a92 6259 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6260
6261 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6262 if (bytesize[opcode] == 8)
6263 displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
6264 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6265 if (!immed)
6266 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
6267
6268 dsc->rd = rt;
6269 dsc->u.ldst.xfersize = bytesize[opcode];
6270 dsc->u.ldst.rn = rn;
6271 dsc->u.ldst.immed = immed;
6272 dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
6273 dsc->u.ldst.restore_r4 = 0;
6274
6275 if (immed)
6276 /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
6277 ->
6278 {ldr,str}<width><cond> r0, [r1,] [r2, #imm]. */
6279 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6280 else
6281 /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
6282 ->
6283 {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3]. */
6284 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6285
6286 dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
6287
6288 return 0;
6289}
6290
0f6f04ba 6291/* Copy byte/half word/word loads and stores. */
cca44b1b 6292
7ff120b4 6293static void
0f6f04ba
YQ
6294install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
6295 struct displaced_step_closure *dsc, int load,
6296 int immed, int writeback, int size, int usermode,
6297 int rt, int rm, int rn)
cca44b1b 6298{
cca44b1b 6299 ULONGEST rt_val, rn_val, rm_val = 0;
cca44b1b 6300
36073a92
YQ
6301 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6302 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 6303 if (!immed)
36073a92 6304 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 6305 if (!load)
36073a92 6306 dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);
cca44b1b 6307
36073a92
YQ
6308 rt_val = displaced_read_reg (regs, dsc, rt);
6309 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 6310 if (!immed)
36073a92 6311 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6312
6313 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6314 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6315 if (!immed)
6316 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
cca44b1b 6317 dsc->rd = rt;
0f6f04ba 6318 dsc->u.ldst.xfersize = size;
cca44b1b
JB
6319 dsc->u.ldst.rn = rn;
6320 dsc->u.ldst.immed = immed;
7ff120b4 6321 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
6322
6323 /* To write PC we can do:
6324
494e194e
YQ
6325 Before this sequence of instructions:
6326 r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
6327 r2 is the Rn value got from dispalced_read_reg.
6328
6329 Insn1: push {pc} Write address of STR instruction + offset on stack
6330 Insn2: pop {r4} Read it back from stack, r4 = addr(Insn1) + offset
6331 Insn3: sub r4, r4, pc r4 = addr(Insn1) + offset - pc
6332 = addr(Insn1) + offset - addr(Insn3) - 8
6333 = offset - 16
6334 Insn4: add r4, r4, #8 r4 = offset - 8
6335 Insn5: add r0, r0, r4 r0 = from + 8 + offset - 8
6336 = from + offset
6337 Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])
cca44b1b
JB
6338
6339 Otherwise we don't know what value to write for PC, since the offset is
494e194e
YQ
6340 architecture-dependent (sometimes PC+8, sometimes PC+12). More details
6341 of this can be found in Section "Saving from r15" in
6342 http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */
cca44b1b 6343
7ff120b4
YQ
6344 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6345}
6346
34518530
YQ
6347
6348static int
6349thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
6350 uint16_t insn2, struct regcache *regs,
6351 struct displaced_step_closure *dsc, int size)
6352{
6353 unsigned int u_bit = bit (insn1, 7);
6354 unsigned int rt = bits (insn2, 12, 15);
6355 int imm12 = bits (insn2, 0, 11);
6356 ULONGEST pc_val;
6357
6358 if (debug_displaced)
6359 fprintf_unfiltered (gdb_stdlog,
6360 "displaced: copying ldr pc (0x%x) R%d %c imm12 %.4x\n",
6361 (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
6362 imm12);
6363
6364 if (!u_bit)
6365 imm12 = -1 * imm12;
6366
6367 /* Rewrite instruction LDR Rt imm12 into:
6368
6369 Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12
6370
6371 LDR R0, R2, R3,
6372
6373 Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2]. */
6374
6375
6376 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6377 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6378 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6379
6380 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6381
6382 pc_val = pc_val & 0xfffffffc;
6383
6384 displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
6385 displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);
6386
6387 dsc->rd = rt;
6388
6389 dsc->u.ldst.xfersize = size;
6390 dsc->u.ldst.immed = 0;
6391 dsc->u.ldst.writeback = 0;
6392 dsc->u.ldst.restore_r4 = 0;
6393
6394 /* LDR R0, R2, R3 */
6395 dsc->modinsn[0] = 0xf852;
6396 dsc->modinsn[1] = 0x3;
6397 dsc->numinsns = 2;
6398
6399 dsc->cleanup = &cleanup_load;
6400
6401 return 0;
6402}
6403
6404static int
6405thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
6406 uint16_t insn2, struct regcache *regs,
6407 struct displaced_step_closure *dsc,
6408 int writeback, int immed)
6409{
6410 unsigned int rt = bits (insn2, 12, 15);
6411 unsigned int rn = bits (insn1, 0, 3);
6412 unsigned int rm = bits (insn2, 0, 3); /* Only valid if !immed. */
6413 /* In LDR (register), there is also a register Rm, which is not allowed to
6414 be PC, so we don't have to check it. */
6415
6416 if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
6417 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
6418 dsc);
6419
6420 if (debug_displaced)
6421 fprintf_unfiltered (gdb_stdlog,
6422 "displaced: copying ldr r%d [r%d] insn %.4x%.4x\n",
6423 rt, rn, insn1, insn2);
6424
6425 install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
6426 0, rt, rm, rn);
6427
6428 dsc->u.ldst.restore_r4 = 0;
6429
6430 if (immed)
6431 /* ldr[b]<cond> rt, [rn, #imm], etc.
6432 ->
6433 ldr[b]<cond> r0, [r2, #imm]. */
6434 {
6435 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6436 dsc->modinsn[1] = insn2 & 0x0fff;
6437 }
6438 else
6439 /* ldr[b]<cond> rt, [rn, rm], etc.
6440 ->
6441 ldr[b]<cond> r0, [r2, r3]. */
6442 {
6443 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6444 dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
6445 }
6446
6447 dsc->numinsns = 2;
6448
6449 return 0;
6450}
6451
6452
7ff120b4
YQ
6453static int
6454arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
6455 struct regcache *regs,
6456 struct displaced_step_closure *dsc,
0f6f04ba 6457 int load, int size, int usermode)
7ff120b4
YQ
6458{
6459 int immed = !bit (insn, 25);
6460 int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
6461 unsigned int rt = bits (insn, 12, 15);
6462 unsigned int rn = bits (insn, 16, 19);
6463 unsigned int rm = bits (insn, 0, 3); /* Only valid if !immed. */
6464
6465 if (!insn_references_pc (insn, 0x000ff00ful))
6466 return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);
6467
6468 if (debug_displaced)
6469 fprintf_unfiltered (gdb_stdlog,
6470 "displaced: copying %s%s r%d [r%d] insn %.8lx\n",
0f6f04ba
YQ
6471 load ? (size == 1 ? "ldrb" : "ldr")
6472 : (size == 1 ? "strb" : "str"), usermode ? "t" : "",
7ff120b4
YQ
6473 rt, rn,
6474 (unsigned long) insn);
6475
0f6f04ba
YQ
6476 install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
6477 usermode, rt, rm, rn);
7ff120b4 6478
bf9f652a 6479 if (load || rt != ARM_PC_REGNUM)
cca44b1b
JB
6480 {
6481 dsc->u.ldst.restore_r4 = 0;
6482
6483 if (immed)
6484 /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
6485 ->
6486 {ldr,str}[b]<cond> r0, [r2, #imm]. */
6487 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6488 else
6489 /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
6490 ->
6491 {ldr,str}[b]<cond> r0, [r2, r3]. */
6492 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6493 }
6494 else
6495 {
6496 /* We need to use r4 as scratch. Make sure it's restored afterwards. */
6497 dsc->u.ldst.restore_r4 = 1;
494e194e
YQ
6498 dsc->modinsn[0] = 0xe92d8000; /* push {pc} */
6499 dsc->modinsn[1] = 0xe8bd0010; /* pop {r4} */
cca44b1b
JB
6500 dsc->modinsn[2] = 0xe044400f; /* sub r4, r4, pc. */
6501 dsc->modinsn[3] = 0xe2844008; /* add r4, r4, #8. */
6502 dsc->modinsn[4] = 0xe0800004; /* add r0, r0, r4. */
6503
6504 /* As above. */
6505 if (immed)
6506 dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
6507 else
6508 dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
6509
cca44b1b
JB
6510 dsc->numinsns = 6;
6511 }
6512
6513 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6514
6515 return 0;
6516}
6517
6518/* Cleanup LDM instructions with fully-populated register list. This is an
6519 unfortunate corner case: it's impossible to implement correctly by modifying
6520 the instruction. The issue is as follows: we have an instruction,
6521
6522 ldm rN, {r0-r15}
6523
6524 which we must rewrite to avoid loading PC. A possible solution would be to
6525 do the load in two halves, something like (with suitable cleanup
6526 afterwards):
6527
6528 mov r8, rN
6529 ldm[id][ab] r8!, {r0-r7}
6530 str r7, <temp>
6531 ldm[id][ab] r8, {r7-r14}
6532 <bkpt>
6533
6534 but at present there's no suitable place for <temp>, since the scratch space
6535 is overwritten before the cleanup routine is called. For now, we simply
6536 emulate the instruction. */
6537
6538static void
6539cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
6540 struct displaced_step_closure *dsc)
6541{
cca44b1b
JB
6542 int inc = dsc->u.block.increment;
6543 int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
6544 int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
6545 uint32_t regmask = dsc->u.block.regmask;
6546 int regno = inc ? 0 : 15;
6547 CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
6548 int exception_return = dsc->u.block.load && dsc->u.block.user
6549 && (regmask & 0x8000) != 0;
36073a92 6550 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
6551 int do_transfer = condition_true (dsc->u.block.cond, status);
6552 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6553
6554 if (!do_transfer)
6555 return;
6556
6557 /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
6558 sensible we can do here. Complain loudly. */
6559 if (exception_return)
6560 error (_("Cannot single-step exception return"));
6561
6562 /* We don't handle any stores here for now. */
6563 gdb_assert (dsc->u.block.load != 0);
6564
6565 if (debug_displaced)
6566 fprintf_unfiltered (gdb_stdlog, "displaced: emulating block transfer: "
6567 "%s %s %s\n", dsc->u.block.load ? "ldm" : "stm",
6568 dsc->u.block.increment ? "inc" : "dec",
6569 dsc->u.block.before ? "before" : "after");
6570
6571 while (regmask)
6572 {
6573 uint32_t memword;
6574
6575 if (inc)
bf9f652a 6576 while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
cca44b1b
JB
6577 regno++;
6578 else
6579 while (regno >= 0 && (regmask & (1 << regno)) == 0)
6580 regno--;
6581
6582 xfer_addr += bump_before;
6583
6584 memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
6585 displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
6586
6587 xfer_addr += bump_after;
6588
6589 regmask &= ~(1 << regno);
6590 }
6591
6592 if (dsc->u.block.writeback)
6593 displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
6594 CANNOT_WRITE_PC);
6595}
6596
6597/* Clean up an STM which included the PC in the register list. */
6598
6599static void
6600cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
6601 struct displaced_step_closure *dsc)
6602{
36073a92 6603 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
6604 int store_executed = condition_true (dsc->u.block.cond, status);
6605 CORE_ADDR pc_stored_at, transferred_regs = bitcount (dsc->u.block.regmask);
6606 CORE_ADDR stm_insn_addr;
6607 uint32_t pc_val;
6608 long offset;
6609 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6610
6611 /* If condition code fails, there's nothing else to do. */
6612 if (!store_executed)
6613 return;
6614
6615 if (dsc->u.block.increment)
6616 {
6617 pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
6618
6619 if (dsc->u.block.before)
6620 pc_stored_at += 4;
6621 }
6622 else
6623 {
6624 pc_stored_at = dsc->u.block.xfer_addr;
6625
6626 if (dsc->u.block.before)
6627 pc_stored_at -= 4;
6628 }
6629
6630 pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
6631 stm_insn_addr = dsc->scratch_base;
6632 offset = pc_val - stm_insn_addr;
6633
6634 if (debug_displaced)
6635 fprintf_unfiltered (gdb_stdlog, "displaced: detected PC offset %.8lx for "
6636 "STM instruction\n", offset);
6637
6638 /* Rewrite the stored PC to the proper value for the non-displaced original
6639 instruction. */
6640 write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
6641 dsc->insn_addr + offset);
6642}
6643
6644/* Clean up an LDM which includes the PC in the register list. We clumped all
6645 the registers in the transferred list into a contiguous range r0...rX (to
6646 avoid loading PC directly and losing control of the debugged program), so we
6647 must undo that here. */
6648
6649static void
6e39997a 6650cleanup_block_load_pc (struct gdbarch *gdbarch,
cca44b1b
JB
6651 struct regcache *regs,
6652 struct displaced_step_closure *dsc)
6653{
36073a92 6654 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b 6655 int load_executed = condition_true (dsc->u.block.cond, status), i;
bf9f652a 6656 unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
cca44b1b
JB
6657 unsigned int regs_loaded = bitcount (mask);
6658 unsigned int num_to_shuffle = regs_loaded, clobbered;
6659
6660 /* The method employed here will fail if the register list is fully populated
6661 (we need to avoid loading PC directly). */
6662 gdb_assert (num_to_shuffle < 16);
6663
6664 if (!load_executed)
6665 return;
6666
6667 clobbered = (1 << num_to_shuffle) - 1;
6668
6669 while (num_to_shuffle > 0)
6670 {
6671 if ((mask & (1 << write_reg)) != 0)
6672 {
6673 unsigned int read_reg = num_to_shuffle - 1;
6674
6675 if (read_reg != write_reg)
6676 {
36073a92 6677 ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
cca44b1b
JB
6678 displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
6679 if (debug_displaced)
6680 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: move "
6681 "loaded register r%d to r%d\n"), read_reg,
6682 write_reg);
6683 }
6684 else if (debug_displaced)
6685 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: register "
6686 "r%d already in the right place\n"),
6687 write_reg);
6688
6689 clobbered &= ~(1 << write_reg);
6690
6691 num_to_shuffle--;
6692 }
6693
6694 write_reg--;
6695 }
6696
6697 /* Restore any registers we scribbled over. */
6698 for (write_reg = 0; clobbered != 0; write_reg++)
6699 {
6700 if ((clobbered & (1 << write_reg)) != 0)
6701 {
6702 displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
6703 CANNOT_WRITE_PC);
6704 if (debug_displaced)
6705 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: restored "
6706 "clobbered register r%d\n"), write_reg);
6707 clobbered &= ~(1 << write_reg);
6708 }
6709 }
6710
6711 /* Perform register writeback manually. */
6712 if (dsc->u.block.writeback)
6713 {
6714 ULONGEST new_rn_val = dsc->u.block.xfer_addr;
6715
6716 if (dsc->u.block.increment)
6717 new_rn_val += regs_loaded * 4;
6718 else
6719 new_rn_val -= regs_loaded * 4;
6720
6721 displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
6722 CANNOT_WRITE_PC);
6723 }
6724}
6725
6726/* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
6727 in user-level code (in particular exception return, ldm rn, {...pc}^). */
6728
6729static int
7ff120b4
YQ
6730arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
6731 struct regcache *regs,
6732 struct displaced_step_closure *dsc)
cca44b1b
JB
6733{
6734 int load = bit (insn, 20);
6735 int user = bit (insn, 22);
6736 int increment = bit (insn, 23);
6737 int before = bit (insn, 24);
6738 int writeback = bit (insn, 21);
6739 int rn = bits (insn, 16, 19);
cca44b1b 6740
0963b4bd
MS
6741 /* Block transfers which don't mention PC can be run directly
6742 out-of-line. */
bf9f652a 6743 if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
7ff120b4 6744 return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
cca44b1b 6745
bf9f652a 6746 if (rn == ARM_PC_REGNUM)
cca44b1b 6747 {
0963b4bd
MS
6748 warning (_("displaced: Unpredictable LDM or STM with "
6749 "base register r15"));
7ff120b4 6750 return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
cca44b1b
JB
6751 }
6752
6753 if (debug_displaced)
6754 fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
6755 "%.8lx\n", (unsigned long) insn);
6756
36073a92 6757 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b
JB
6758 dsc->u.block.rn = rn;
6759
6760 dsc->u.block.load = load;
6761 dsc->u.block.user = user;
6762 dsc->u.block.increment = increment;
6763 dsc->u.block.before = before;
6764 dsc->u.block.writeback = writeback;
6765 dsc->u.block.cond = bits (insn, 28, 31);
6766
6767 dsc->u.block.regmask = insn & 0xffff;
6768
6769 if (load)
6770 {
6771 if ((insn & 0xffff) == 0xffff)
6772 {
6773 /* LDM with a fully-populated register list. This case is
6774 particularly tricky. Implement for now by fully emulating the
6775 instruction (which might not behave perfectly in all cases, but
6776 these instructions should be rare enough for that not to matter
6777 too much). */
6778 dsc->modinsn[0] = ARM_NOP;
6779
6780 dsc->cleanup = &cleanup_block_load_all;
6781 }
6782 else
6783 {
6784 /* LDM of a list of registers which includes PC. Implement by
6785 rewriting the list of registers to be transferred into a
6786 contiguous chunk r0...rX before doing the transfer, then shuffling
6787 registers into the correct places in the cleanup routine. */
6788 unsigned int regmask = insn & 0xffff;
6789 unsigned int num_in_list = bitcount (regmask), new_regmask, bit = 1;
6790 unsigned int to = 0, from = 0, i, new_rn;
6791
6792 for (i = 0; i < num_in_list; i++)
36073a92 6793 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b
JB
6794
6795 /* Writeback makes things complicated. We need to avoid clobbering
6796 the base register with one of the registers in our modified
6797 register list, but just using a different register can't work in
6798 all cases, e.g.:
6799
6800 ldm r14!, {r0-r13,pc}
6801
6802 which would need to be rewritten as:
6803
6804 ldm rN!, {r0-r14}
6805
6806 but that can't work, because there's no free register for N.
6807
6808 Solve this by turning off the writeback bit, and emulating
6809 writeback manually in the cleanup routine. */
6810
6811 if (writeback)
6812 insn &= ~(1 << 21);
6813
6814 new_regmask = (1 << num_in_list) - 1;
6815
6816 if (debug_displaced)
6817 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
6818 "{..., pc}: original reg list %.4x, modified "
6819 "list %.4x\n"), rn, writeback ? "!" : "",
6820 (int) insn & 0xffff, new_regmask);
6821
6822 dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
6823
6824 dsc->cleanup = &cleanup_block_load_pc;
6825 }
6826 }
6827 else
6828 {
6829 /* STM of a list of registers which includes PC. Run the instruction
6830 as-is, but out of line: this will store the wrong value for the PC,
6831 so we must manually fix up the memory in the cleanup routine.
6832 Doing things this way has the advantage that we can auto-detect
6833 the offset of the PC write (which is architecture-dependent) in
6834 the cleanup routine. */
6835 dsc->modinsn[0] = insn;
6836
6837 dsc->cleanup = &cleanup_block_store_pc;
6838 }
6839
6840 return 0;
6841}
6842
34518530
YQ
6843static int
6844thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
6845 struct regcache *regs,
6846 struct displaced_step_closure *dsc)
cca44b1b 6847{
34518530
YQ
6848 int rn = bits (insn1, 0, 3);
6849 int load = bit (insn1, 4);
6850 int writeback = bit (insn1, 5);
cca44b1b 6851
34518530
YQ
6852 /* Block transfers which don't mention PC can be run directly
6853 out-of-line. */
6854 if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
6855 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);
7ff120b4 6856
34518530
YQ
6857 if (rn == ARM_PC_REGNUM)
6858 {
6859 warning (_("displaced: Unpredictable LDM or STM with "
6860 "base register r15"));
6861 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6862 "unpredictable ldm/stm", dsc);
6863 }
cca44b1b
JB
6864
6865 if (debug_displaced)
34518530
YQ
6866 fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
6867 "%.4x%.4x\n", insn1, insn2);
cca44b1b 6868
34518530
YQ
6869 /* Clear bit 13, since it should be always zero. */
6870 dsc->u.block.regmask = (insn2 & 0xdfff);
6871 dsc->u.block.rn = rn;
cca44b1b 6872
34518530
YQ
6873 dsc->u.block.load = load;
6874 dsc->u.block.user = 0;
6875 dsc->u.block.increment = bit (insn1, 7);
6876 dsc->u.block.before = bit (insn1, 8);
6877 dsc->u.block.writeback = writeback;
6878 dsc->u.block.cond = INST_AL;
6879 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b 6880
34518530
YQ
6881 if (load)
6882 {
6883 if (dsc->u.block.regmask == 0xffff)
6884 {
6885 /* This branch is impossible to happen. */
6886 gdb_assert (0);
6887 }
6888 else
6889 {
6890 unsigned int regmask = dsc->u.block.regmask;
6891 unsigned int num_in_list = bitcount (regmask), new_regmask, bit = 1;
6892 unsigned int to = 0, from = 0, i, new_rn;
6893
6894 for (i = 0; i < num_in_list; i++)
6895 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
6896
6897 if (writeback)
6898 insn1 &= ~(1 << 5);
6899
6900 new_regmask = (1 << num_in_list) - 1;
6901
6902 if (debug_displaced)
6903 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
6904 "{..., pc}: original reg list %.4x, modified "
6905 "list %.4x\n"), rn, writeback ? "!" : "",
6906 (int) dsc->u.block.regmask, new_regmask);
6907
6908 dsc->modinsn[0] = insn1;
6909 dsc->modinsn[1] = (new_regmask & 0xffff);
6910 dsc->numinsns = 2;
6911
6912 dsc->cleanup = &cleanup_block_load_pc;
6913 }
6914 }
6915 else
6916 {
6917 dsc->modinsn[0] = insn1;
6918 dsc->modinsn[1] = insn2;
6919 dsc->numinsns = 2;
6920 dsc->cleanup = &cleanup_block_store_pc;
6921 }
6922 return 0;
6923}
6924
6925/* Cleanup/copy SVC (SWI) instructions. These two functions are overridden
6926 for Linux, where some SVC instructions must be treated specially. */
6927
6928static void
6929cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
6930 struct displaced_step_closure *dsc)
6931{
6932 CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
6933
6934 if (debug_displaced)
6935 fprintf_unfiltered (gdb_stdlog, "displaced: cleanup for svc, resume at "
6936 "%.8lx\n", (unsigned long) resume_addr);
6937
6938 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
6939}
6940
6941
6942/* Common copy routine for svc instruciton. */
6943
6944static int
6945install_svc (struct gdbarch *gdbarch, struct regcache *regs,
6946 struct displaced_step_closure *dsc)
6947{
6948 /* Preparation: none.
6949 Insn: unmodified svc.
6950 Cleanup: pc <- insn_addr + insn_size. */
6951
6952 /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
6953 instruction. */
6954 dsc->wrote_to_pc = 1;
6955
6956 /* Allow OS-specific code to override SVC handling. */
bd18283a
YQ
6957 if (dsc->u.svc.copy_svc_os)
6958 return dsc->u.svc.copy_svc_os (gdbarch, regs, dsc);
6959 else
6960 {
6961 dsc->cleanup = &cleanup_svc;
6962 return 0;
6963 }
34518530
YQ
6964}
6965
6966static int
6967arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
6968 struct regcache *regs, struct displaced_step_closure *dsc)
6969{
6970
6971 if (debug_displaced)
6972 fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.8lx\n",
6973 (unsigned long) insn);
6974
6975 dsc->modinsn[0] = insn;
6976
6977 return install_svc (gdbarch, regs, dsc);
6978}
6979
6980static int
6981thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
6982 struct regcache *regs, struct displaced_step_closure *dsc)
6983{
6984
6985 if (debug_displaced)
6986 fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.4x\n",
6987 insn);
bd18283a 6988
34518530
YQ
6989 dsc->modinsn[0] = insn;
6990
6991 return install_svc (gdbarch, regs, dsc);
cca44b1b
JB
6992}
6993
6994/* Copy undefined instructions. */
6995
6996static int
7ff120b4
YQ
6997arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
6998 struct displaced_step_closure *dsc)
cca44b1b
JB
6999{
7000 if (debug_displaced)
0963b4bd
MS
7001 fprintf_unfiltered (gdb_stdlog,
7002 "displaced: copying undefined insn %.8lx\n",
cca44b1b
JB
7003 (unsigned long) insn);
7004
7005 dsc->modinsn[0] = insn;
7006
7007 return 0;
7008}
7009
34518530
YQ
7010static int
7011thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
7012 struct displaced_step_closure *dsc)
7013{
7014
7015 if (debug_displaced)
7016 fprintf_unfiltered (gdb_stdlog, "displaced: copying undefined insn "
7017 "%.4x %.4x\n", (unsigned short) insn1,
7018 (unsigned short) insn2);
7019
7020 dsc->modinsn[0] = insn1;
7021 dsc->modinsn[1] = insn2;
7022 dsc->numinsns = 2;
7023
7024 return 0;
7025}
7026
cca44b1b
JB
7027/* Copy unpredictable instructions. */
7028
7029static int
7ff120b4
YQ
7030arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
7031 struct displaced_step_closure *dsc)
cca44b1b
JB
7032{
7033 if (debug_displaced)
7034 fprintf_unfiltered (gdb_stdlog, "displaced: copying unpredictable insn "
7035 "%.8lx\n", (unsigned long) insn);
7036
7037 dsc->modinsn[0] = insn;
7038
7039 return 0;
7040}
7041
7042/* The decode_* functions are instruction decoding helpers. They mostly follow
7043 the presentation in the ARM ARM. */
7044
7045static int
7ff120b4
YQ
7046arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
7047 struct regcache *regs,
7048 struct displaced_step_closure *dsc)
cca44b1b
JB
7049{
7050 unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
7051 unsigned int rn = bits (insn, 16, 19);
7052
7053 if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0xe) == 0x0)
7ff120b4 7054 return arm_copy_unmodified (gdbarch, insn, "cps", dsc);
cca44b1b 7055 else if (op1 == 0x10 && op2 == 0x0 && (rn & 0xe) == 0x1)
7ff120b4 7056 return arm_copy_unmodified (gdbarch, insn, "setend", dsc);
cca44b1b 7057 else if ((op1 & 0x60) == 0x20)
7ff120b4 7058 return arm_copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
cca44b1b 7059 else if ((op1 & 0x71) == 0x40)
7ff120b4
YQ
7060 return arm_copy_unmodified (gdbarch, insn, "neon elt/struct load/store",
7061 dsc);
cca44b1b 7062 else if ((op1 & 0x77) == 0x41)
7ff120b4 7063 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 7064 else if ((op1 & 0x77) == 0x45)
7ff120b4 7065 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pli. */
cca44b1b
JB
7066 else if ((op1 & 0x77) == 0x51)
7067 {
7068 if (rn != 0xf)
7ff120b4 7069 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b 7070 else
7ff120b4 7071 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7072 }
7073 else if ((op1 & 0x77) == 0x55)
7ff120b4 7074 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b
JB
7075 else if (op1 == 0x57)
7076 switch (op2)
7077 {
7ff120b4
YQ
7078 case 0x1: return arm_copy_unmodified (gdbarch, insn, "clrex", dsc);
7079 case 0x4: return arm_copy_unmodified (gdbarch, insn, "dsb", dsc);
7080 case 0x5: return arm_copy_unmodified (gdbarch, insn, "dmb", dsc);
7081 case 0x6: return arm_copy_unmodified (gdbarch, insn, "isb", dsc);
7082 default: return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7083 }
7084 else if ((op1 & 0x63) == 0x43)
7ff120b4 7085 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7086 else if ((op2 & 0x1) == 0x0)
7087 switch (op1 & ~0x80)
7088 {
7089 case 0x61:
7ff120b4 7090 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 7091 case 0x65:
7ff120b4 7092 return arm_copy_preload_reg (gdbarch, insn, regs, dsc); /* pli reg. */
cca44b1b
JB
7093 case 0x71: case 0x75:
7094 /* pld/pldw reg. */
7ff120b4 7095 return arm_copy_preload_reg (gdbarch, insn, regs, dsc);
cca44b1b 7096 case 0x63: case 0x67: case 0x73: case 0x77:
7ff120b4 7097 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b 7098 default:
7ff120b4 7099 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7100 }
7101 else
7ff120b4 7102 return arm_copy_undef (gdbarch, insn, dsc); /* Probably unreachable. */
cca44b1b
JB
7103}
7104
7105static int
7ff120b4
YQ
7106arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
7107 struct regcache *regs,
7108 struct displaced_step_closure *dsc)
cca44b1b
JB
7109{
7110 if (bit (insn, 27) == 0)
7ff120b4 7111 return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
cca44b1b
JB
7112 /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx. */
7113 else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
7114 {
7115 case 0x0: case 0x2:
7ff120b4 7116 return arm_copy_unmodified (gdbarch, insn, "srs", dsc);
cca44b1b
JB
7117
7118 case 0x1: case 0x3:
7ff120b4 7119 return arm_copy_unmodified (gdbarch, insn, "rfe", dsc);
cca44b1b
JB
7120
7121 case 0x4: case 0x5: case 0x6: case 0x7:
7ff120b4 7122 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b
JB
7123
7124 case 0x8:
7125 switch ((insn & 0xe00000) >> 21)
7126 {
7127 case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
7128 /* stc/stc2. */
7ff120b4 7129 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7130
7131 case 0x2:
7ff120b4 7132 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b
JB
7133
7134 default:
7ff120b4 7135 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7136 }
7137
7138 case 0x9:
7139 {
7140 int rn_f = (bits (insn, 16, 19) == 0xf);
7141 switch ((insn & 0xe00000) >> 21)
7142 {
7143 case 0x1: case 0x3:
7144 /* ldc/ldc2 imm (undefined for rn == pc). */
7ff120b4
YQ
7145 return rn_f ? arm_copy_undef (gdbarch, insn, dsc)
7146 : arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7147
7148 case 0x2:
7ff120b4 7149 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
7150
7151 case 0x4: case 0x5: case 0x6: case 0x7:
7152 /* ldc/ldc2 lit (undefined for rn != pc). */
7ff120b4
YQ
7153 return rn_f ? arm_copy_copro_load_store (gdbarch, insn, regs, dsc)
7154 : arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7155
7156 default:
7ff120b4 7157 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7158 }
7159 }
7160
7161 case 0xa:
7ff120b4 7162 return arm_copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
cca44b1b
JB
7163
7164 case 0xb:
7165 if (bits (insn, 16, 19) == 0xf)
7166 /* ldc/ldc2 lit. */
7ff120b4 7167 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 7168 else
7ff120b4 7169 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7170
7171 case 0xc:
7172 if (bit (insn, 4))
7ff120b4 7173 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 7174 else
7ff120b4 7175 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7176
7177 case 0xd:
7178 if (bit (insn, 4))
7ff120b4 7179 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 7180 else
7ff120b4 7181 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7182
7183 default:
7ff120b4 7184 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7185 }
7186}
7187
7188/* Decode miscellaneous instructions in dp/misc encoding space. */
7189
7190static int
7ff120b4
YQ
7191arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
7192 struct regcache *regs,
7193 struct displaced_step_closure *dsc)
cca44b1b
JB
7194{
7195 unsigned int op2 = bits (insn, 4, 6);
7196 unsigned int op = bits (insn, 21, 22);
7197 unsigned int op1 = bits (insn, 16, 19);
7198
7199 switch (op2)
7200 {
7201 case 0x0:
7ff120b4 7202 return arm_copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
cca44b1b
JB
7203
7204 case 0x1:
7205 if (op == 0x1) /* bx. */
7ff120b4 7206 return arm_copy_bx_blx_reg (gdbarch, insn, regs, dsc);
cca44b1b 7207 else if (op == 0x3)
7ff120b4 7208 return arm_copy_unmodified (gdbarch, insn, "clz", dsc);
cca44b1b 7209 else
7ff120b4 7210 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7211
7212 case 0x2:
7213 if (op == 0x1)
7214 /* Not really supported. */
7ff120b4 7215 return arm_copy_unmodified (gdbarch, insn, "bxj", dsc);
cca44b1b 7216 else
7ff120b4 7217 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7218
7219 case 0x3:
7220 if (op == 0x1)
7ff120b4 7221 return arm_copy_bx_blx_reg (gdbarch, insn,
0963b4bd 7222 regs, dsc); /* blx register. */
cca44b1b 7223 else
7ff120b4 7224 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7225
7226 case 0x5:
7ff120b4 7227 return arm_copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
cca44b1b
JB
7228
7229 case 0x7:
7230 if (op == 0x1)
7ff120b4 7231 return arm_copy_unmodified (gdbarch, insn, "bkpt", dsc);
cca44b1b
JB
7232 else if (op == 0x3)
7233 /* Not really supported. */
7ff120b4 7234 return arm_copy_unmodified (gdbarch, insn, "smc", dsc);
cca44b1b
JB
7235
7236 default:
7ff120b4 7237 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7238 }
7239}
7240
7241static int
7ff120b4
YQ
7242arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
7243 struct regcache *regs,
7244 struct displaced_step_closure *dsc)
cca44b1b
JB
7245{
7246 if (bit (insn, 25))
7247 switch (bits (insn, 20, 24))
7248 {
7249 case 0x10:
7ff120b4 7250 return arm_copy_unmodified (gdbarch, insn, "movw", dsc);
cca44b1b
JB
7251
7252 case 0x14:
7ff120b4 7253 return arm_copy_unmodified (gdbarch, insn, "movt", dsc);
cca44b1b
JB
7254
7255 case 0x12: case 0x16:
7ff120b4 7256 return arm_copy_unmodified (gdbarch, insn, "msr imm", dsc);
cca44b1b
JB
7257
7258 default:
7ff120b4 7259 return arm_copy_alu_imm (gdbarch, insn, regs, dsc);
cca44b1b
JB
7260 }
7261 else
7262 {
7263 uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
7264
7265 if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
7ff120b4 7266 return arm_copy_alu_reg (gdbarch, insn, regs, dsc);
cca44b1b 7267 else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
7ff120b4 7268 return arm_copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
cca44b1b 7269 else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
7ff120b4 7270 return arm_decode_miscellaneous (gdbarch, insn, regs, dsc);
cca44b1b 7271 else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
7ff120b4 7272 return arm_copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
cca44b1b 7273 else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
7ff120b4 7274 return arm_copy_unmodified (gdbarch, insn, "mul/mla", dsc);
cca44b1b 7275 else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
7ff120b4 7276 return arm_copy_unmodified (gdbarch, insn, "synch", dsc);
cca44b1b
JB
7277 else if (op2 == 0xb || (op2 & 0xd) == 0xd)
7278 /* 2nd arg means "unpriveleged". */
7ff120b4
YQ
7279 return arm_copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
7280 dsc);
cca44b1b
JB
7281 }
7282
7283 /* Should be unreachable. */
7284 return 1;
7285}
7286
7287static int
7ff120b4
YQ
7288arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
7289 struct regcache *regs,
7290 struct displaced_step_closure *dsc)
cca44b1b
JB
7291{
7292 int a = bit (insn, 25), b = bit (insn, 4);
7293 uint32_t op1 = bits (insn, 20, 24);
7294 int rn_f = bits (insn, 16, 19) == 0xf;
7295
7296 if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
7297 || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
0f6f04ba 7298 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 0);
cca44b1b
JB
7299 else if ((!a && (op1 & 0x17) == 0x02)
7300 || (a && (op1 & 0x17) == 0x02 && !b))
0f6f04ba 7301 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 1);
cca44b1b
JB
7302 else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
7303 || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
0f6f04ba 7304 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 0);
cca44b1b
JB
7305 else if ((!a && (op1 & 0x17) == 0x03)
7306 || (a && (op1 & 0x17) == 0x03 && !b))
0f6f04ba 7307 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 1);
cca44b1b
JB
7308 else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
7309 || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
7ff120b4 7310 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
cca44b1b
JB
7311 else if ((!a && (op1 & 0x17) == 0x06)
7312 || (a && (op1 & 0x17) == 0x06 && !b))
7ff120b4 7313 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
cca44b1b
JB
7314 else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
7315 || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
7ff120b4 7316 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
cca44b1b
JB
7317 else if ((!a && (op1 & 0x17) == 0x07)
7318 || (a && (op1 & 0x17) == 0x07 && !b))
7ff120b4 7319 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
cca44b1b
JB
7320
7321 /* Should be unreachable. */
7322 return 1;
7323}
7324
7325static int
7ff120b4
YQ
7326arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
7327 struct displaced_step_closure *dsc)
cca44b1b
JB
7328{
7329 switch (bits (insn, 20, 24))
7330 {
7331 case 0x00: case 0x01: case 0x02: case 0x03:
7ff120b4 7332 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
cca44b1b
JB
7333
7334 case 0x04: case 0x05: case 0x06: case 0x07:
7ff120b4 7335 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
cca44b1b
JB
7336
7337 case 0x08: case 0x09: case 0x0a: case 0x0b:
7338 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
7ff120b4 7339 return arm_copy_unmodified (gdbarch, insn,
cca44b1b
JB
7340 "decode/pack/unpack/saturate/reverse", dsc);
7341
7342 case 0x18:
7343 if (bits (insn, 5, 7) == 0) /* op2. */
7344 {
7345 if (bits (insn, 12, 15) == 0xf)
7ff120b4 7346 return arm_copy_unmodified (gdbarch, insn, "usad8", dsc);
cca44b1b 7347 else
7ff120b4 7348 return arm_copy_unmodified (gdbarch, insn, "usada8", dsc);
cca44b1b
JB
7349 }
7350 else
7ff120b4 7351 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7352
7353 case 0x1a: case 0x1b:
7354 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 7355 return arm_copy_unmodified (gdbarch, insn, "sbfx", dsc);
cca44b1b 7356 else
7ff120b4 7357 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7358
7359 case 0x1c: case 0x1d:
7360 if (bits (insn, 5, 6) == 0x0) /* op2[1:0]. */
7361 {
7362 if (bits (insn, 0, 3) == 0xf)
7ff120b4 7363 return arm_copy_unmodified (gdbarch, insn, "bfc", dsc);
cca44b1b 7364 else
7ff120b4 7365 return arm_copy_unmodified (gdbarch, insn, "bfi", dsc);
cca44b1b
JB
7366 }
7367 else
7ff120b4 7368 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7369
7370 case 0x1e: case 0x1f:
7371 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 7372 return arm_copy_unmodified (gdbarch, insn, "ubfx", dsc);
cca44b1b 7373 else
7ff120b4 7374 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7375 }
7376
7377 /* Should be unreachable. */
7378 return 1;
7379}
7380
7381static int
7ff120b4
YQ
7382arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, int32_t insn,
7383 struct regcache *regs,
7384 struct displaced_step_closure *dsc)
cca44b1b
JB
7385{
7386 if (bit (insn, 25))
7ff120b4 7387 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b 7388 else
7ff120b4 7389 return arm_copy_block_xfer (gdbarch, insn, regs, dsc);
cca44b1b
JB
7390}
7391
7392static int
7ff120b4
YQ
7393arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
7394 struct regcache *regs,
7395 struct displaced_step_closure *dsc)
cca44b1b
JB
7396{
7397 unsigned int opcode = bits (insn, 20, 24);
7398
7399 switch (opcode)
7400 {
7401 case 0x04: case 0x05: /* VFP/Neon mrrc/mcrr. */
7ff120b4 7402 return arm_copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
cca44b1b
JB
7403
7404 case 0x08: case 0x0a: case 0x0c: case 0x0e:
7405 case 0x12: case 0x16:
7ff120b4 7406 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
cca44b1b
JB
7407
7408 case 0x09: case 0x0b: case 0x0d: case 0x0f:
7409 case 0x13: case 0x17:
7ff120b4 7410 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
cca44b1b
JB
7411
7412 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7413 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7414 /* Note: no writeback for these instructions. Bit 25 will always be
7415 zero though (via caller), so the following works OK. */
7ff120b4 7416 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7417 }
7418
7419 /* Should be unreachable. */
7420 return 1;
7421}
7422
34518530
YQ
7423/* Decode shifted register instructions. */
7424
7425static int
7426thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
7427 uint16_t insn2, struct regcache *regs,
7428 struct displaced_step_closure *dsc)
7429{
7430 /* PC is only allowed to be used in instruction MOV. */
7431
7432 unsigned int op = bits (insn1, 5, 8);
7433 unsigned int rn = bits (insn1, 0, 3);
7434
7435 if (op == 0x2 && rn == 0xf) /* MOV */
7436 return thumb2_copy_alu_imm (gdbarch, insn1, insn2, regs, dsc);
7437 else
7438 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7439 "dp (shift reg)", dsc);
7440}
7441
7442
7443/* Decode extension register load/store. Exactly the same as
7444 arm_decode_ext_reg_ld_st. */
7445
7446static int
7447thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
7448 uint16_t insn2, struct regcache *regs,
7449 struct displaced_step_closure *dsc)
7450{
7451 unsigned int opcode = bits (insn1, 4, 8);
7452
7453 switch (opcode)
7454 {
7455 case 0x04: case 0x05:
7456 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7457 "vfp/neon vmov", dsc);
7458
7459 case 0x08: case 0x0c: /* 01x00 */
7460 case 0x0a: case 0x0e: /* 01x10 */
7461 case 0x12: case 0x16: /* 10x10 */
7462 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7463 "vfp/neon vstm/vpush", dsc);
7464
7465 case 0x09: case 0x0d: /* 01x01 */
7466 case 0x0b: case 0x0f: /* 01x11 */
7467 case 0x13: case 0x17: /* 10x11 */
7468 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7469 "vfp/neon vldm/vpop", dsc);
7470
7471 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7472 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7473 "vstr", dsc);
7474 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7475 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2, regs, dsc);
7476 }
7477
7478 /* Should be unreachable. */
7479 return 1;
7480}
7481
cca44b1b 7482static int
7ff120b4
YQ
7483arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn, CORE_ADDR to,
7484 struct regcache *regs, struct displaced_step_closure *dsc)
cca44b1b
JB
7485{
7486 unsigned int op1 = bits (insn, 20, 25);
7487 int op = bit (insn, 4);
7488 unsigned int coproc = bits (insn, 8, 11);
7489 unsigned int rn = bits (insn, 16, 19);
7490
7491 if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
7ff120b4 7492 return arm_decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
cca44b1b
JB
7493 else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
7494 && (coproc & 0xe) != 0xa)
7495 /* stc/stc2. */
7ff120b4 7496 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7497 else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
7498 && (coproc & 0xe) != 0xa)
7499 /* ldc/ldc2 imm/lit. */
7ff120b4 7500 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 7501 else if ((op1 & 0x3e) == 0x00)
7ff120b4 7502 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b 7503 else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
7ff120b4 7504 return arm_copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
cca44b1b 7505 else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
7ff120b4 7506 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b 7507 else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
7ff120b4 7508 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
7509 else if ((op1 & 0x30) == 0x20 && !op)
7510 {
7511 if ((coproc & 0xe) == 0xa)
7ff120b4 7512 return arm_copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
cca44b1b 7513 else
7ff120b4 7514 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7515 }
7516 else if ((op1 & 0x30) == 0x20 && op)
7ff120b4 7517 return arm_copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
cca44b1b 7518 else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
7ff120b4 7519 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 7520 else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
7ff120b4 7521 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 7522 else if ((op1 & 0x30) == 0x30)
7ff120b4 7523 return arm_copy_svc (gdbarch, insn, regs, dsc);
cca44b1b 7524 else
7ff120b4 7525 return arm_copy_undef (gdbarch, insn, dsc); /* Possibly unreachable. */
cca44b1b
JB
7526}
7527
34518530
YQ
7528static int
7529thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
7530 uint16_t insn2, struct regcache *regs,
7531 struct displaced_step_closure *dsc)
7532{
7533 unsigned int coproc = bits (insn2, 8, 11);
7534 unsigned int op1 = bits (insn1, 4, 9);
7535 unsigned int bit_5_8 = bits (insn1, 5, 8);
7536 unsigned int bit_9 = bit (insn1, 9);
7537 unsigned int bit_4 = bit (insn1, 4);
7538 unsigned int rn = bits (insn1, 0, 3);
7539
7540 if (bit_9 == 0)
7541 {
7542 if (bit_5_8 == 2)
7543 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7544 "neon 64bit xfer/mrrc/mrrc2/mcrr/mcrr2",
7545 dsc);
7546 else if (bit_5_8 == 0) /* UNDEFINED. */
7547 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
7548 else
7549 {
7550 /*coproc is 101x. SIMD/VFP, ext registers load/store. */
7551 if ((coproc & 0xe) == 0xa)
7552 return thumb2_decode_ext_reg_ld_st (gdbarch, insn1, insn2, regs,
7553 dsc);
7554 else /* coproc is not 101x. */
7555 {
7556 if (bit_4 == 0) /* STC/STC2. */
7557 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7558 "stc/stc2", dsc);
7559 else /* LDC/LDC2 {literal, immeidate}. */
7560 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2,
7561 regs, dsc);
7562 }
7563 }
7564 }
7565 else
7566 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "coproc", dsc);
7567
7568 return 0;
7569}
7570
7571static void
7572install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
7573 struct displaced_step_closure *dsc, int rd)
7574{
7575 /* ADR Rd, #imm
7576
7577 Rewrite as:
7578
7579 Preparation: Rd <- PC
7580 Insn: ADD Rd, #imm
7581 Cleanup: Null.
7582 */
7583
7584 /* Rd <- PC */
7585 int val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
7586 displaced_write_reg (regs, dsc, rd, val, CANNOT_WRITE_PC);
7587}
7588
7589static int
7590thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
7591 struct displaced_step_closure *dsc,
7592 int rd, unsigned int imm)
7593{
7594
7595 /* Encoding T2: ADDS Rd, #imm */
7596 dsc->modinsn[0] = (0x3000 | (rd << 8) | imm);
7597
7598 install_pc_relative (gdbarch, regs, dsc, rd);
7599
7600 return 0;
7601}
7602
7603static int
7604thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
7605 struct regcache *regs,
7606 struct displaced_step_closure *dsc)
7607{
7608 unsigned int rd = bits (insn, 8, 10);
7609 unsigned int imm8 = bits (insn, 0, 7);
7610
7611 if (debug_displaced)
7612 fprintf_unfiltered (gdb_stdlog,
7613 "displaced: copying thumb adr r%d, #%d insn %.4x\n",
7614 rd, imm8, insn);
7615
7616 return thumb_copy_pc_relative_16bit (gdbarch, regs, dsc, rd, imm8);
7617}
7618
7619static int
7620thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
7621 uint16_t insn2, struct regcache *regs,
7622 struct displaced_step_closure *dsc)
7623{
7624 unsigned int rd = bits (insn2, 8, 11);
7625 /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
7626 extract raw immediate encoding rather than computing immediate. When
7627 generating ADD or SUB instruction, we can simply perform OR operation to
7628 set immediate into ADD. */
7629 unsigned int imm_3_8 = insn2 & 0x70ff;
7630 unsigned int imm_i = insn1 & 0x0400; /* Clear all bits except bit 10. */
7631
7632 if (debug_displaced)
7633 fprintf_unfiltered (gdb_stdlog,
7634 "displaced: copying thumb adr r%d, #%d:%d insn %.4x%.4x\n",
7635 rd, imm_i, imm_3_8, insn1, insn2);
7636
7637 if (bit (insn1, 7)) /* Encoding T2 */
7638 {
7639 /* Encoding T3: SUB Rd, Rd, #imm */
7640 dsc->modinsn[0] = (0xf1a0 | rd | imm_i);
7641 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
7642 }
7643 else /* Encoding T3 */
7644 {
7645 /* Encoding T3: ADD Rd, Rd, #imm */
7646 dsc->modinsn[0] = (0xf100 | rd | imm_i);
7647 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
7648 }
7649 dsc->numinsns = 2;
7650
7651 install_pc_relative (gdbarch, regs, dsc, rd);
7652
7653 return 0;
7654}
7655
7656static int
7657thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, unsigned short insn1,
7658 struct regcache *regs,
7659 struct displaced_step_closure *dsc)
7660{
7661 unsigned int rt = bits (insn1, 8, 10);
7662 unsigned int pc;
7663 int imm8 = (bits (insn1, 0, 7) << 2);
7664 CORE_ADDR from = dsc->insn_addr;
7665
7666 /* LDR Rd, #imm8
7667
7668 Rwrite as:
7669
7670 Preparation: tmp0 <- R0, tmp2 <- R2, tmp3 <- R3, R2 <- PC, R3 <- #imm8;
7671
7672 Insn: LDR R0, [R2, R3];
7673 Cleanup: R2 <- tmp2, R3 <- tmp3, Rd <- R0, R0 <- tmp0 */
7674
7675 if (debug_displaced)
7676 fprintf_unfiltered (gdb_stdlog,
7677 "displaced: copying thumb ldr r%d [pc #%d]\n"
7678 , rt, imm8);
7679
7680 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
7681 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
7682 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
7683 pc = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
7684 /* The assembler calculates the required value of the offset from the
7685 Align(PC,4) value of this instruction to the label. */
7686 pc = pc & 0xfffffffc;
7687
7688 displaced_write_reg (regs, dsc, 2, pc, CANNOT_WRITE_PC);
7689 displaced_write_reg (regs, dsc, 3, imm8, CANNOT_WRITE_PC);
7690
7691 dsc->rd = rt;
7692 dsc->u.ldst.xfersize = 4;
7693 dsc->u.ldst.rn = 0;
7694 dsc->u.ldst.immed = 0;
7695 dsc->u.ldst.writeback = 0;
7696 dsc->u.ldst.restore_r4 = 0;
7697
7698 dsc->modinsn[0] = 0x58d0; /* ldr r0, [r2, r3]*/
7699
7700 dsc->cleanup = &cleanup_load;
7701
7702 return 0;
7703}
7704
7705/* Copy Thumb cbnz/cbz insruction. */
7706
7707static int
7708thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
7709 struct regcache *regs,
7710 struct displaced_step_closure *dsc)
7711{
7712 int non_zero = bit (insn1, 11);
7713 unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
7714 CORE_ADDR from = dsc->insn_addr;
7715 int rn = bits (insn1, 0, 2);
7716 int rn_val = displaced_read_reg (regs, dsc, rn);
7717
7718 dsc->u.branch.cond = (rn_val && non_zero) || (!rn_val && !non_zero);
7719 /* CBNZ and CBZ do not affect the condition flags. If condition is true,
7720 set it INST_AL, so cleanup_branch will know branch is taken, otherwise,
7721 condition is false, let it be, cleanup_branch will do nothing. */
7722 if (dsc->u.branch.cond)
7723 {
7724 dsc->u.branch.cond = INST_AL;
7725 dsc->u.branch.dest = from + 4 + imm5;
7726 }
7727 else
7728 dsc->u.branch.dest = from + 2;
7729
7730 dsc->u.branch.link = 0;
7731 dsc->u.branch.exchange = 0;
7732
7733 if (debug_displaced)
7734 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s [r%d = 0x%x]"
7735 " insn %.4x to %.8lx\n", non_zero ? "cbnz" : "cbz",
7736 rn, rn_val, insn1, dsc->u.branch.dest);
7737
7738 dsc->modinsn[0] = THUMB_NOP;
7739
7740 dsc->cleanup = &cleanup_branch;
7741 return 0;
7742}
7743
7744/* Copy Table Branch Byte/Halfword */
7745static int
7746thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
7747 uint16_t insn2, struct regcache *regs,
7748 struct displaced_step_closure *dsc)
7749{
7750 ULONGEST rn_val, rm_val;
7751 int is_tbh = bit (insn2, 4);
7752 CORE_ADDR halfwords = 0;
7753 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7754
7755 rn_val = displaced_read_reg (regs, dsc, bits (insn1, 0, 3));
7756 rm_val = displaced_read_reg (regs, dsc, bits (insn2, 0, 3));
7757
7758 if (is_tbh)
7759 {
7760 gdb_byte buf[2];
7761
7762 target_read_memory (rn_val + 2 * rm_val, buf, 2);
7763 halfwords = extract_unsigned_integer (buf, 2, byte_order);
7764 }
7765 else
7766 {
7767 gdb_byte buf[1];
7768
7769 target_read_memory (rn_val + rm_val, buf, 1);
7770 halfwords = extract_unsigned_integer (buf, 1, byte_order);
7771 }
7772
7773 if (debug_displaced)
7774 fprintf_unfiltered (gdb_stdlog, "displaced: %s base 0x%x offset 0x%x"
7775 " offset 0x%x\n", is_tbh ? "tbh" : "tbb",
7776 (unsigned int) rn_val, (unsigned int) rm_val,
7777 (unsigned int) halfwords);
7778
7779 dsc->u.branch.cond = INST_AL;
7780 dsc->u.branch.link = 0;
7781 dsc->u.branch.exchange = 0;
7782 dsc->u.branch.dest = dsc->insn_addr + 4 + 2 * halfwords;
7783
7784 dsc->cleanup = &cleanup_branch;
7785
7786 return 0;
7787}
7788
7789static void
7790cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
7791 struct displaced_step_closure *dsc)
7792{
7793 /* PC <- r7 */
7794 int val = displaced_read_reg (regs, dsc, 7);
7795 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, val, BX_WRITE_PC);
7796
7797 /* r7 <- r8 */
7798 val = displaced_read_reg (regs, dsc, 8);
7799 displaced_write_reg (regs, dsc, 7, val, CANNOT_WRITE_PC);
7800
7801 /* r8 <- tmp[0] */
7802 displaced_write_reg (regs, dsc, 8, dsc->tmp[0], CANNOT_WRITE_PC);
7803
7804}
7805
7806static int
7807thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, unsigned short insn1,
7808 struct regcache *regs,
7809 struct displaced_step_closure *dsc)
7810{
7811 dsc->u.block.regmask = insn1 & 0x00ff;
7812
7813 /* Rewrite instruction: POP {rX, rY, ...,rZ, PC}
7814 to :
7815
7816 (1) register list is full, that is, r0-r7 are used.
7817 Prepare: tmp[0] <- r8
7818
7819 POP {r0, r1, ...., r6, r7}; remove PC from reglist
7820 MOV r8, r7; Move value of r7 to r8;
7821 POP {r7}; Store PC value into r7.
7822
7823 Cleanup: PC <- r7, r7 <- r8, r8 <-tmp[0]
7824
7825 (2) register list is not full, supposing there are N registers in
7826 register list (except PC, 0 <= N <= 7).
7827 Prepare: for each i, 0 - N, tmp[i] <- ri.
7828
7829 POP {r0, r1, ...., rN};
7830
7831 Cleanup: Set registers in original reglist from r0 - rN. Restore r0 - rN
7832 from tmp[] properly.
7833 */
7834 if (debug_displaced)
7835 fprintf_unfiltered (gdb_stdlog,
7836 "displaced: copying thumb pop {%.8x, pc} insn %.4x\n",
7837 dsc->u.block.regmask, insn1);
7838
7839 if (dsc->u.block.regmask == 0xff)
7840 {
7841 dsc->tmp[0] = displaced_read_reg (regs, dsc, 8);
7842
7843 dsc->modinsn[0] = (insn1 & 0xfeff); /* POP {r0,r1,...,r6, r7} */
7844 dsc->modinsn[1] = 0x46b8; /* MOV r8, r7 */
7845 dsc->modinsn[2] = 0xbc80; /* POP {r7} */
7846
7847 dsc->numinsns = 3;
7848 dsc->cleanup = &cleanup_pop_pc_16bit_all;
7849 }
7850 else
7851 {
7852 unsigned int num_in_list = bitcount (dsc->u.block.regmask);
7853 unsigned int new_regmask, bit = 1;
7854 unsigned int to = 0, from = 0, i, new_rn;
7855
7856 for (i = 0; i < num_in_list + 1; i++)
7857 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7858
7859 new_regmask = (1 << (num_in_list + 1)) - 1;
7860
7861 if (debug_displaced)
7862 fprintf_unfiltered (gdb_stdlog, _("displaced: POP "
7863 "{..., pc}: original reg list %.4x,"
7864 " modified list %.4x\n"),
7865 (int) dsc->u.block.regmask, new_regmask);
7866
7867 dsc->u.block.regmask |= 0x8000;
7868 dsc->u.block.writeback = 0;
7869 dsc->u.block.cond = INST_AL;
7870
7871 dsc->modinsn[0] = (insn1 & ~0x1ff) | (new_regmask & 0xff);
7872
7873 dsc->cleanup = &cleanup_block_load_pc;
7874 }
7875
7876 return 0;
7877}
7878
7879static void
7880thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
7881 struct regcache *regs,
7882 struct displaced_step_closure *dsc)
7883{
7884 unsigned short op_bit_12_15 = bits (insn1, 12, 15);
7885 unsigned short op_bit_10_11 = bits (insn1, 10, 11);
7886 int err = 0;
7887
7888 /* 16-bit thumb instructions. */
7889 switch (op_bit_12_15)
7890 {
7891 /* Shift (imme), add, subtract, move and compare. */
7892 case 0: case 1: case 2: case 3:
7893 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
7894 "shift/add/sub/mov/cmp",
7895 dsc);
7896 break;
7897 case 4:
7898 switch (op_bit_10_11)
7899 {
7900 case 0: /* Data-processing */
7901 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
7902 "data-processing",
7903 dsc);
7904 break;
7905 case 1: /* Special data instructions and branch and exchange. */
7906 {
7907 unsigned short op = bits (insn1, 7, 9);
7908 if (op == 6 || op == 7) /* BX or BLX */
7909 err = thumb_copy_bx_blx_reg (gdbarch, insn1, regs, dsc);
7910 else if (bits (insn1, 6, 7) != 0) /* ADD/MOV/CMP high registers. */
7911 err = thumb_copy_alu_reg (gdbarch, insn1, regs, dsc);
7912 else
7913 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "special data",
7914 dsc);
7915 }
7916 break;
7917 default: /* LDR (literal) */
7918 err = thumb_copy_16bit_ldr_literal (gdbarch, insn1, regs, dsc);
7919 }
7920 break;
7921 case 5: case 6: case 7: case 8: case 9: /* Load/Store single data item */
7922 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldr/str", dsc);
7923 break;
7924 case 10:
7925 if (op_bit_10_11 < 2) /* Generate PC-relative address */
7926 err = thumb_decode_pc_relative_16bit (gdbarch, insn1, regs, dsc);
7927 else /* Generate SP-relative address */
7928 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "sp-relative", dsc);
7929 break;
7930 case 11: /* Misc 16-bit instructions */
7931 {
7932 switch (bits (insn1, 8, 11))
7933 {
7934 case 1: case 3: case 9: case 11: /* CBNZ, CBZ */
7935 err = thumb_copy_cbnz_cbz (gdbarch, insn1, regs, dsc);
7936 break;
7937 case 12: case 13: /* POP */
7938 if (bit (insn1, 8)) /* PC is in register list. */
7939 err = thumb_copy_pop_pc_16bit (gdbarch, insn1, regs, dsc);
7940 else
7941 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "pop", dsc);
7942 break;
7943 case 15: /* If-Then, and hints */
7944 if (bits (insn1, 0, 3))
7945 /* If-Then makes up to four following instructions conditional.
7946 IT instruction itself is not conditional, so handle it as a
7947 common unmodified instruction. */
7948 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "If-Then",
7949 dsc);
7950 else
7951 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "hints", dsc);
7952 break;
7953 default:
7954 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "misc", dsc);
7955 }
7956 }
7957 break;
7958 case 12:
7959 if (op_bit_10_11 < 2) /* Store multiple registers */
7960 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "stm", dsc);
7961 else /* Load multiple registers */
7962 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldm", dsc);
7963 break;
7964 case 13: /* Conditional branch and supervisor call */
7965 if (bits (insn1, 9, 11) != 7) /* conditional branch */
7966 err = thumb_copy_b (gdbarch, insn1, dsc);
7967 else
7968 err = thumb_copy_svc (gdbarch, insn1, regs, dsc);
7969 break;
7970 case 14: /* Unconditional branch */
7971 err = thumb_copy_b (gdbarch, insn1, dsc);
7972 break;
7973 default:
7974 err = 1;
7975 }
7976
7977 if (err)
7978 internal_error (__FILE__, __LINE__,
7979 _("thumb_process_displaced_16bit_insn: Instruction decode error"));
7980}
7981
7982static int
7983decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
7984 uint16_t insn1, uint16_t insn2,
7985 struct regcache *regs,
7986 struct displaced_step_closure *dsc)
7987{
7988 int rt = bits (insn2, 12, 15);
7989 int rn = bits (insn1, 0, 3);
7990 int op1 = bits (insn1, 7, 8);
7991 int err = 0;
7992
7993 switch (bits (insn1, 5, 6))
7994 {
7995 case 0: /* Load byte and memory hints */
7996 if (rt == 0xf) /* PLD/PLI */
7997 {
7998 if (rn == 0xf)
7999 /* PLD literal or Encoding T3 of PLI(immediate, literal). */
8000 return thumb2_copy_preload (gdbarch, insn1, insn2, regs, dsc);
8001 else
8002 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8003 "pli/pld", dsc);
8004 }
8005 else
8006 {
8007 if (rn == 0xf) /* LDRB/LDRSB (literal) */
8008 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8009 1);
8010 else
8011 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8012 "ldrb{reg, immediate}/ldrbt",
8013 dsc);
8014 }
8015
8016 break;
8017 case 1: /* Load halfword and memory hints. */
8018 if (rt == 0xf) /* PLD{W} and Unalloc memory hint. */
8019 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8020 "pld/unalloc memhint", dsc);
8021 else
8022 {
8023 if (rn == 0xf)
8024 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8025 2);
8026 else
8027 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8028 "ldrh/ldrht", dsc);
8029 }
8030 break;
8031 case 2: /* Load word */
8032 {
8033 int insn2_bit_8_11 = bits (insn2, 8, 11);
8034
8035 if (rn == 0xf)
8036 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc, 4);
8037 else if (op1 == 0x1) /* Encoding T3 */
8038 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs, dsc,
8039 0, 1);
8040 else /* op1 == 0x0 */
8041 {
8042 if (insn2_bit_8_11 == 0xc || (insn2_bit_8_11 & 0x9) == 0x9)
8043 /* LDR (immediate) */
8044 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8045 dsc, bit (insn2, 8), 1);
8046 else if (insn2_bit_8_11 == 0xe) /* LDRT */
8047 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8048 "ldrt", dsc);
8049 else
8050 /* LDR (register) */
8051 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8052 dsc, 0, 0);
8053 }
8054 break;
8055 }
8056 default:
8057 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
8058 break;
8059 }
8060 return 0;
8061}
8062
8063static void
8064thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8065 uint16_t insn2, struct regcache *regs,
8066 struct displaced_step_closure *dsc)
8067{
8068 int err = 0;
8069 unsigned short op = bit (insn2, 15);
8070 unsigned int op1 = bits (insn1, 11, 12);
8071
8072 switch (op1)
8073 {
8074 case 1:
8075 {
8076 switch (bits (insn1, 9, 10))
8077 {
8078 case 0:
8079 if (bit (insn1, 6))
8080 {
8081 /* Load/store {dual, execlusive}, table branch. */
8082 if (bits (insn1, 7, 8) == 1 && bits (insn1, 4, 5) == 1
8083 && bits (insn2, 5, 7) == 0)
8084 err = thumb2_copy_table_branch (gdbarch, insn1, insn2, regs,
8085 dsc);
8086 else
8087 /* PC is not allowed to use in load/store {dual, exclusive}
8088 instructions. */
8089 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8090 "load/store dual/ex", dsc);
8091 }
8092 else /* load/store multiple */
8093 {
8094 switch (bits (insn1, 7, 8))
8095 {
8096 case 0: case 3: /* SRS, RFE */
8097 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8098 "srs/rfe", dsc);
8099 break;
8100 case 1: case 2: /* LDM/STM/PUSH/POP */
8101 err = thumb2_copy_block_xfer (gdbarch, insn1, insn2, regs, dsc);
8102 break;
8103 }
8104 }
8105 break;
8106
8107 case 1:
8108 /* Data-processing (shift register). */
8109 err = thumb2_decode_dp_shift_reg (gdbarch, insn1, insn2, regs,
8110 dsc);
8111 break;
8112 default: /* Coprocessor instructions. */
8113 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8114 break;
8115 }
8116 break;
8117 }
8118 case 2: /* op1 = 2 */
8119 if (op) /* Branch and misc control. */
8120 {
8121 if (bit (insn2, 14) /* BLX/BL */
8122 || bit (insn2, 12) /* Unconditional branch */
8123 || (bits (insn1, 7, 9) != 0x7)) /* Conditional branch */
8124 err = thumb2_copy_b_bl_blx (gdbarch, insn1, insn2, regs, dsc);
8125 else
8126 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8127 "misc ctrl", dsc);
8128 }
8129 else
8130 {
8131 if (bit (insn1, 9)) /* Data processing (plain binary imm). */
8132 {
8133 int op = bits (insn1, 4, 8);
8134 int rn = bits (insn1, 0, 3);
8135 if ((op == 0 || op == 0xa) && rn == 0xf)
8136 err = thumb_copy_pc_relative_32bit (gdbarch, insn1, insn2,
8137 regs, dsc);
8138 else
8139 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8140 "dp/pb", dsc);
8141 }
8142 else /* Data processing (modified immeidate) */
8143 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8144 "dp/mi", dsc);
8145 }
8146 break;
8147 case 3: /* op1 = 3 */
8148 switch (bits (insn1, 9, 10))
8149 {
8150 case 0:
8151 if (bit (insn1, 4))
8152 err = decode_thumb_32bit_ld_mem_hints (gdbarch, insn1, insn2,
8153 regs, dsc);
8154 else /* NEON Load/Store and Store single data item */
8155 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8156 "neon elt/struct load/store",
8157 dsc);
8158 break;
8159 case 1: /* op1 = 3, bits (9, 10) == 1 */
8160 switch (bits (insn1, 7, 8))
8161 {
8162 case 0: case 1: /* Data processing (register) */
8163 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8164 "dp(reg)", dsc);
8165 break;
8166 case 2: /* Multiply and absolute difference */
8167 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8168 "mul/mua/diff", dsc);
8169 break;
8170 case 3: /* Long multiply and divide */
8171 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8172 "lmul/lmua", dsc);
8173 break;
8174 }
8175 break;
8176 default: /* Coprocessor instructions */
8177 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8178 break;
8179 }
8180 break;
8181 default:
8182 err = 1;
8183 }
8184
8185 if (err)
8186 internal_error (__FILE__, __LINE__,
8187 _("thumb_process_displaced_32bit_insn: Instruction decode error"));
8188
8189}
8190
b434a28f
YQ
8191static void
8192thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8193 CORE_ADDR to, struct regcache *regs,
8194 struct displaced_step_closure *dsc)
8195{
34518530
YQ
8196 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8197 uint16_t insn1
8198 = read_memory_unsigned_integer (from, 2, byte_order_for_code);
8199
8200 if (debug_displaced)
8201 fprintf_unfiltered (gdb_stdlog, "displaced: process thumb insn %.4x "
8202 "at %.8lx\n", insn1, (unsigned long) from);
8203
8204 dsc->is_thumb = 1;
8205 dsc->insn_size = thumb_insn_size (insn1);
8206 if (thumb_insn_size (insn1) == 4)
8207 {
8208 uint16_t insn2
8209 = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
8210 thumb_process_displaced_32bit_insn (gdbarch, insn1, insn2, regs, dsc);
8211 }
8212 else
8213 thumb_process_displaced_16bit_insn (gdbarch, insn1, regs, dsc);
b434a28f
YQ
8214}
8215
cca44b1b 8216void
b434a28f
YQ
8217arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8218 CORE_ADDR to, struct regcache *regs,
cca44b1b
JB
8219 struct displaced_step_closure *dsc)
8220{
8221 int err = 0;
b434a28f
YQ
8222 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8223 uint32_t insn;
cca44b1b
JB
8224
8225 /* Most displaced instructions use a 1-instruction scratch space, so set this
8226 here and override below if/when necessary. */
8227 dsc->numinsns = 1;
8228 dsc->insn_addr = from;
8229 dsc->scratch_base = to;
8230 dsc->cleanup = NULL;
8231 dsc->wrote_to_pc = 0;
8232
b434a28f
YQ
8233 if (!displaced_in_arm_mode (regs))
8234 return thumb_process_displaced_insn (gdbarch, from, to, regs, dsc);
8235
4db71c0b
YQ
8236 dsc->is_thumb = 0;
8237 dsc->insn_size = 4;
b434a28f
YQ
8238 insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
8239 if (debug_displaced)
8240 fprintf_unfiltered (gdb_stdlog, "displaced: stepping insn %.8lx "
8241 "at %.8lx\n", (unsigned long) insn,
8242 (unsigned long) from);
8243
cca44b1b 8244 if ((insn & 0xf0000000) == 0xf0000000)
7ff120b4 8245 err = arm_decode_unconditional (gdbarch, insn, regs, dsc);
cca44b1b
JB
8246 else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
8247 {
8248 case 0x0: case 0x1: case 0x2: case 0x3:
7ff120b4 8249 err = arm_decode_dp_misc (gdbarch, insn, regs, dsc);
cca44b1b
JB
8250 break;
8251
8252 case 0x4: case 0x5: case 0x6:
7ff120b4 8253 err = arm_decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
cca44b1b
JB
8254 break;
8255
8256 case 0x7:
7ff120b4 8257 err = arm_decode_media (gdbarch, insn, dsc);
cca44b1b
JB
8258 break;
8259
8260 case 0x8: case 0x9: case 0xa: case 0xb:
7ff120b4 8261 err = arm_decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
cca44b1b
JB
8262 break;
8263
8264 case 0xc: case 0xd: case 0xe: case 0xf:
7ff120b4 8265 err = arm_decode_svc_copro (gdbarch, insn, to, regs, dsc);
cca44b1b
JB
8266 break;
8267 }
8268
8269 if (err)
8270 internal_error (__FILE__, __LINE__,
8271 _("arm_process_displaced_insn: Instruction decode error"));
8272}
8273
8274/* Actually set up the scratch space for a displaced instruction. */
8275
8276void
8277arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
8278 CORE_ADDR to, struct displaced_step_closure *dsc)
8279{
8280 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4db71c0b 8281 unsigned int i, len, offset;
cca44b1b 8282 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4db71c0b
YQ
8283 int size = dsc->is_thumb? 2 : 4;
8284 const unsigned char *bkp_insn;
cca44b1b 8285
4db71c0b 8286 offset = 0;
cca44b1b
JB
8287 /* Poke modified instruction(s). */
8288 for (i = 0; i < dsc->numinsns; i++)
8289 {
8290 if (debug_displaced)
4db71c0b
YQ
8291 {
8292 fprintf_unfiltered (gdb_stdlog, "displaced: writing insn ");
8293 if (size == 4)
8294 fprintf_unfiltered (gdb_stdlog, "%.8lx",
8295 dsc->modinsn[i]);
8296 else if (size == 2)
8297 fprintf_unfiltered (gdb_stdlog, "%.4x",
8298 (unsigned short)dsc->modinsn[i]);
8299
8300 fprintf_unfiltered (gdb_stdlog, " at %.8lx\n",
8301 (unsigned long) to + offset);
8302
8303 }
8304 write_memory_unsigned_integer (to + offset, size,
8305 byte_order_for_code,
cca44b1b 8306 dsc->modinsn[i]);
4db71c0b
YQ
8307 offset += size;
8308 }
8309
8310 /* Choose the correct breakpoint instruction. */
8311 if (dsc->is_thumb)
8312 {
8313 bkp_insn = tdep->thumb_breakpoint;
8314 len = tdep->thumb_breakpoint_size;
8315 }
8316 else
8317 {
8318 bkp_insn = tdep->arm_breakpoint;
8319 len = tdep->arm_breakpoint_size;
cca44b1b
JB
8320 }
8321
8322 /* Put breakpoint afterwards. */
4db71c0b 8323 write_memory (to + offset, bkp_insn, len);
cca44b1b
JB
8324
8325 if (debug_displaced)
8326 fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
8327 paddress (gdbarch, from), paddress (gdbarch, to));
8328}
8329
8330/* Entry point for copying an instruction into scratch space for displaced
8331 stepping. */
8332
8333struct displaced_step_closure *
8334arm_displaced_step_copy_insn (struct gdbarch *gdbarch,
8335 CORE_ADDR from, CORE_ADDR to,
8336 struct regcache *regs)
8337{
8338 struct displaced_step_closure *dsc
8339 = xmalloc (sizeof (struct displaced_step_closure));
b434a28f 8340 arm_process_displaced_insn (gdbarch, from, to, regs, dsc);
cca44b1b
JB
8341 arm_displaced_init_closure (gdbarch, from, to, dsc);
8342
8343 return dsc;
8344}
8345
8346/* Entry point for cleaning things up after a displaced instruction has been
8347 single-stepped. */
8348
8349void
8350arm_displaced_step_fixup (struct gdbarch *gdbarch,
8351 struct displaced_step_closure *dsc,
8352 CORE_ADDR from, CORE_ADDR to,
8353 struct regcache *regs)
8354{
8355 if (dsc->cleanup)
8356 dsc->cleanup (gdbarch, regs, dsc);
8357
8358 if (!dsc->wrote_to_pc)
4db71c0b
YQ
8359 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
8360 dsc->insn_addr + dsc->insn_size);
8361
cca44b1b
JB
8362}
8363
8364#include "bfd-in2.h"
8365#include "libcoff.h"
8366
8367static int
8368gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
8369{
9779414d
DJ
8370 struct gdbarch *gdbarch = info->application_data;
8371
8372 if (arm_pc_is_thumb (gdbarch, memaddr))
cca44b1b
JB
8373 {
8374 static asymbol *asym;
8375 static combined_entry_type ce;
8376 static struct coff_symbol_struct csym;
8377 static struct bfd fake_bfd;
8378 static bfd_target fake_target;
8379
8380 if (csym.native == NULL)
8381 {
8382 /* Create a fake symbol vector containing a Thumb symbol.
8383 This is solely so that the code in print_insn_little_arm()
8384 and print_insn_big_arm() in opcodes/arm-dis.c will detect
8385 the presence of a Thumb symbol and switch to decoding
8386 Thumb instructions. */
8387
8388 fake_target.flavour = bfd_target_coff_flavour;
8389 fake_bfd.xvec = &fake_target;
8390 ce.u.syment.n_sclass = C_THUMBEXTFUNC;
8391 csym.native = &ce;
8392 csym.symbol.the_bfd = &fake_bfd;
8393 csym.symbol.name = "fake";
8394 asym = (asymbol *) & csym;
8395 }
8396
8397 memaddr = UNMAKE_THUMB_ADDR (memaddr);
8398 info->symbols = &asym;
8399 }
8400 else
8401 info->symbols = NULL;
8402
8403 if (info->endian == BFD_ENDIAN_BIG)
8404 return print_insn_big_arm (memaddr, info);
8405 else
8406 return print_insn_little_arm (memaddr, info);
8407}
8408
8409/* The following define instruction sequences that will cause ARM
8410 cpu's to take an undefined instruction trap. These are used to
8411 signal a breakpoint to GDB.
8412
8413 The newer ARMv4T cpu's are capable of operating in ARM or Thumb
8414 modes. A different instruction is required for each mode. The ARM
8415 cpu's can also be big or little endian. Thus four different
8416 instructions are needed to support all cases.
8417
8418 Note: ARMv4 defines several new instructions that will take the
8419 undefined instruction trap. ARM7TDMI is nominally ARMv4T, but does
8420 not in fact add the new instructions. The new undefined
8421 instructions in ARMv4 are all instructions that had no defined
8422 behaviour in earlier chips. There is no guarantee that they will
8423 raise an exception, but may be treated as NOP's. In practice, it
8424 may only safe to rely on instructions matching:
8425
8426 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
8427 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
8428 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
8429
0963b4bd 8430 Even this may only true if the condition predicate is true. The
cca44b1b
JB
8431 following use a condition predicate of ALWAYS so it is always TRUE.
8432
8433 There are other ways of forcing a breakpoint. GNU/Linux, RISC iX,
8434 and NetBSD all use a software interrupt rather than an undefined
8435 instruction to force a trap. This can be handled by by the
8436 abi-specific code during establishment of the gdbarch vector. */
8437
8438#define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
8439#define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
8440#define THUMB_LE_BREAKPOINT {0xbe,0xbe}
8441#define THUMB_BE_BREAKPOINT {0xbe,0xbe}
8442
8443static const char arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
8444static const char arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
8445static const char arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
8446static const char arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
8447
8448/* Determine the type and size of breakpoint to insert at PCPTR. Uses
8449 the program counter value to determine whether a 16-bit or 32-bit
8450 breakpoint should be used. It returns a pointer to a string of
8451 bytes that encode a breakpoint instruction, stores the length of
8452 the string to *lenptr, and adjusts the program counter (if
8453 necessary) to point to the actual memory location where the
8454 breakpoint should be inserted. */
8455
8456static const unsigned char *
8457arm_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenptr)
8458{
8459 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
177321bd 8460 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
cca44b1b 8461
9779414d 8462 if (arm_pc_is_thumb (gdbarch, *pcptr))
cca44b1b
JB
8463 {
8464 *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
177321bd
DJ
8465
8466 /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
8467 check whether we are replacing a 32-bit instruction. */
8468 if (tdep->thumb2_breakpoint != NULL)
8469 {
8470 gdb_byte buf[2];
8471 if (target_read_memory (*pcptr, buf, 2) == 0)
8472 {
8473 unsigned short inst1;
8474 inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
8475 if ((inst1 & 0xe000) == 0xe000 && (inst1 & 0x1800) != 0)
8476 {
8477 *lenptr = tdep->thumb2_breakpoint_size;
8478 return tdep->thumb2_breakpoint;
8479 }
8480 }
8481 }
8482
cca44b1b
JB
8483 *lenptr = tdep->thumb_breakpoint_size;
8484 return tdep->thumb_breakpoint;
8485 }
8486 else
8487 {
8488 *lenptr = tdep->arm_breakpoint_size;
8489 return tdep->arm_breakpoint;
8490 }
8491}
8492
177321bd
DJ
8493static void
8494arm_remote_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
8495 int *kindptr)
8496{
8497 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
8498
8499 arm_breakpoint_from_pc (gdbarch, pcptr, kindptr);
8500
9779414d 8501 if (arm_pc_is_thumb (gdbarch, *pcptr) && *kindptr == 4)
177321bd
DJ
8502 /* The documented magic value for a 32-bit Thumb-2 breakpoint, so
8503 that this is not confused with a 32-bit ARM breakpoint. */
8504 *kindptr = 3;
8505}
8506
cca44b1b
JB
8507/* Extract from an array REGBUF containing the (raw) register state a
8508 function return value of type TYPE, and copy that, in virtual
8509 format, into VALBUF. */
8510
8511static void
8512arm_extract_return_value (struct type *type, struct regcache *regs,
8513 gdb_byte *valbuf)
8514{
8515 struct gdbarch *gdbarch = get_regcache_arch (regs);
8516 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8517
8518 if (TYPE_CODE_FLT == TYPE_CODE (type))
8519 {
8520 switch (gdbarch_tdep (gdbarch)->fp_model)
8521 {
8522 case ARM_FLOAT_FPA:
8523 {
8524 /* The value is in register F0 in internal format. We need to
8525 extract the raw value and then convert it to the desired
8526 internal type. */
8527 bfd_byte tmpbuf[FP_REGISTER_SIZE];
8528
8529 regcache_cooked_read (regs, ARM_F0_REGNUM, tmpbuf);
8530 convert_from_extended (floatformat_from_type (type), tmpbuf,
8531 valbuf, gdbarch_byte_order (gdbarch));
8532 }
8533 break;
8534
8535 case ARM_FLOAT_SOFT_FPA:
8536 case ARM_FLOAT_SOFT_VFP:
8537 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8538 not using the VFP ABI code. */
8539 case ARM_FLOAT_VFP:
8540 regcache_cooked_read (regs, ARM_A1_REGNUM, valbuf);
8541 if (TYPE_LENGTH (type) > 4)
8542 regcache_cooked_read (regs, ARM_A1_REGNUM + 1,
8543 valbuf + INT_REGISTER_SIZE);
8544 break;
8545
8546 default:
0963b4bd
MS
8547 internal_error (__FILE__, __LINE__,
8548 _("arm_extract_return_value: "
8549 "Floating point model not supported"));
cca44b1b
JB
8550 break;
8551 }
8552 }
8553 else if (TYPE_CODE (type) == TYPE_CODE_INT
8554 || TYPE_CODE (type) == TYPE_CODE_CHAR
8555 || TYPE_CODE (type) == TYPE_CODE_BOOL
8556 || TYPE_CODE (type) == TYPE_CODE_PTR
8557 || TYPE_CODE (type) == TYPE_CODE_REF
8558 || TYPE_CODE (type) == TYPE_CODE_ENUM)
8559 {
b021a221
MS
8560 /* If the type is a plain integer, then the access is
8561 straight-forward. Otherwise we have to play around a bit
8562 more. */
cca44b1b
JB
8563 int len = TYPE_LENGTH (type);
8564 int regno = ARM_A1_REGNUM;
8565 ULONGEST tmp;
8566
8567 while (len > 0)
8568 {
8569 /* By using store_unsigned_integer we avoid having to do
8570 anything special for small big-endian values. */
8571 regcache_cooked_read_unsigned (regs, regno++, &tmp);
8572 store_unsigned_integer (valbuf,
8573 (len > INT_REGISTER_SIZE
8574 ? INT_REGISTER_SIZE : len),
8575 byte_order, tmp);
8576 len -= INT_REGISTER_SIZE;
8577 valbuf += INT_REGISTER_SIZE;
8578 }
8579 }
8580 else
8581 {
8582 /* For a structure or union the behaviour is as if the value had
8583 been stored to word-aligned memory and then loaded into
8584 registers with 32-bit load instruction(s). */
8585 int len = TYPE_LENGTH (type);
8586 int regno = ARM_A1_REGNUM;
8587 bfd_byte tmpbuf[INT_REGISTER_SIZE];
8588
8589 while (len > 0)
8590 {
8591 regcache_cooked_read (regs, regno++, tmpbuf);
8592 memcpy (valbuf, tmpbuf,
8593 len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
8594 len -= INT_REGISTER_SIZE;
8595 valbuf += INT_REGISTER_SIZE;
8596 }
8597 }
8598}
8599
8600
8601/* Will a function return an aggregate type in memory or in a
8602 register? Return 0 if an aggregate type can be returned in a
8603 register, 1 if it must be returned in memory. */
8604
8605static int
8606arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
8607{
8608 int nRc;
8609 enum type_code code;
8610
8611 CHECK_TYPEDEF (type);
8612
8613 /* In the ARM ABI, "integer" like aggregate types are returned in
8614 registers. For an aggregate type to be integer like, its size
8615 must be less than or equal to INT_REGISTER_SIZE and the
8616 offset of each addressable subfield must be zero. Note that bit
8617 fields are not addressable, and all addressable subfields of
8618 unions always start at offset zero.
8619
8620 This function is based on the behaviour of GCC 2.95.1.
8621 See: gcc/arm.c: arm_return_in_memory() for details.
8622
8623 Note: All versions of GCC before GCC 2.95.2 do not set up the
8624 parameters correctly for a function returning the following
8625 structure: struct { float f;}; This should be returned in memory,
8626 not a register. Richard Earnshaw sent me a patch, but I do not
8627 know of any way to detect if a function like the above has been
8628 compiled with the correct calling convention. */
8629
8630 /* All aggregate types that won't fit in a register must be returned
8631 in memory. */
8632 if (TYPE_LENGTH (type) > INT_REGISTER_SIZE)
8633 {
8634 return 1;
8635 }
8636
8637 /* The AAPCS says all aggregates not larger than a word are returned
8638 in a register. */
8639 if (gdbarch_tdep (gdbarch)->arm_abi != ARM_ABI_APCS)
8640 return 0;
8641
8642 /* The only aggregate types that can be returned in a register are
8643 structs and unions. Arrays must be returned in memory. */
8644 code = TYPE_CODE (type);
8645 if ((TYPE_CODE_STRUCT != code) && (TYPE_CODE_UNION != code))
8646 {
8647 return 1;
8648 }
8649
8650 /* Assume all other aggregate types can be returned in a register.
8651 Run a check for structures, unions and arrays. */
8652 nRc = 0;
8653
8654 if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
8655 {
8656 int i;
8657 /* Need to check if this struct/union is "integer" like. For
8658 this to be true, its size must be less than or equal to
8659 INT_REGISTER_SIZE and the offset of each addressable
8660 subfield must be zero. Note that bit fields are not
8661 addressable, and unions always start at offset zero. If any
8662 of the subfields is a floating point type, the struct/union
8663 cannot be an integer type. */
8664
8665 /* For each field in the object, check:
8666 1) Is it FP? --> yes, nRc = 1;
67255d04
RE
8667 2) Is it addressable (bitpos != 0) and
8668 not packed (bitsize == 0)?
8669 --> yes, nRc = 1
8670 */
8671
8672 for (i = 0; i < TYPE_NFIELDS (type); i++)
8673 {
8674 enum type_code field_type_code;
0963b4bd
MS
8675 field_type_code = TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type,
8676 i)));
67255d04
RE
8677
8678 /* Is it a floating point type field? */
8679 if (field_type_code == TYPE_CODE_FLT)
8680 {
8681 nRc = 1;
8682 break;
8683 }
8684
8685 /* If bitpos != 0, then we have to care about it. */
8686 if (TYPE_FIELD_BITPOS (type, i) != 0)
8687 {
8688 /* Bitfields are not addressable. If the field bitsize is
8689 zero, then the field is not packed. Hence it cannot be
8690 a bitfield or any other packed type. */
8691 if (TYPE_FIELD_BITSIZE (type, i) == 0)
8692 {
8693 nRc = 1;
8694 break;
8695 }
8696 }
8697 }
8698 }
8699
8700 return nRc;
8701}
8702
34e8f22d
RE
8703/* Write into appropriate registers a function return value of type
8704 TYPE, given in virtual format. */
8705
8706static void
b508a996 8707arm_store_return_value (struct type *type, struct regcache *regs,
5238cf52 8708 const gdb_byte *valbuf)
34e8f22d 8709{
be8626e0 8710 struct gdbarch *gdbarch = get_regcache_arch (regs);
e17a4113 8711 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
be8626e0 8712
34e8f22d
RE
8713 if (TYPE_CODE (type) == TYPE_CODE_FLT)
8714 {
7a5ea0d4 8715 char buf[MAX_REGISTER_SIZE];
34e8f22d 8716
be8626e0 8717 switch (gdbarch_tdep (gdbarch)->fp_model)
08216dd7
RE
8718 {
8719 case ARM_FLOAT_FPA:
8720
be8626e0
MD
8721 convert_to_extended (floatformat_from_type (type), buf, valbuf,
8722 gdbarch_byte_order (gdbarch));
b508a996 8723 regcache_cooked_write (regs, ARM_F0_REGNUM, buf);
08216dd7
RE
8724 break;
8725
fd50bc42 8726 case ARM_FLOAT_SOFT_FPA:
08216dd7 8727 case ARM_FLOAT_SOFT_VFP:
90445bd3
DJ
8728 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8729 not using the VFP ABI code. */
8730 case ARM_FLOAT_VFP:
b508a996
RE
8731 regcache_cooked_write (regs, ARM_A1_REGNUM, valbuf);
8732 if (TYPE_LENGTH (type) > 4)
8733 regcache_cooked_write (regs, ARM_A1_REGNUM + 1,
7a5ea0d4 8734 valbuf + INT_REGISTER_SIZE);
08216dd7
RE
8735 break;
8736
8737 default:
9b20d036
MS
8738 internal_error (__FILE__, __LINE__,
8739 _("arm_store_return_value: Floating "
8740 "point model not supported"));
08216dd7
RE
8741 break;
8742 }
34e8f22d 8743 }
b508a996
RE
8744 else if (TYPE_CODE (type) == TYPE_CODE_INT
8745 || TYPE_CODE (type) == TYPE_CODE_CHAR
8746 || TYPE_CODE (type) == TYPE_CODE_BOOL
8747 || TYPE_CODE (type) == TYPE_CODE_PTR
8748 || TYPE_CODE (type) == TYPE_CODE_REF
8749 || TYPE_CODE (type) == TYPE_CODE_ENUM)
8750 {
8751 if (TYPE_LENGTH (type) <= 4)
8752 {
8753 /* Values of one word or less are zero/sign-extended and
8754 returned in r0. */
7a5ea0d4 8755 bfd_byte tmpbuf[INT_REGISTER_SIZE];
b508a996
RE
8756 LONGEST val = unpack_long (type, valbuf);
8757
e17a4113 8758 store_signed_integer (tmpbuf, INT_REGISTER_SIZE, byte_order, val);
b508a996
RE
8759 regcache_cooked_write (regs, ARM_A1_REGNUM, tmpbuf);
8760 }
8761 else
8762 {
8763 /* Integral values greater than one word are stored in consecutive
8764 registers starting with r0. This will always be a multiple of
8765 the regiser size. */
8766 int len = TYPE_LENGTH (type);
8767 int regno = ARM_A1_REGNUM;
8768
8769 while (len > 0)
8770 {
8771 regcache_cooked_write (regs, regno++, valbuf);
7a5ea0d4
DJ
8772 len -= INT_REGISTER_SIZE;
8773 valbuf += INT_REGISTER_SIZE;
b508a996
RE
8774 }
8775 }
8776 }
34e8f22d 8777 else
b508a996
RE
8778 {
8779 /* For a structure or union the behaviour is as if the value had
8780 been stored to word-aligned memory and then loaded into
8781 registers with 32-bit load instruction(s). */
8782 int len = TYPE_LENGTH (type);
8783 int regno = ARM_A1_REGNUM;
7a5ea0d4 8784 bfd_byte tmpbuf[INT_REGISTER_SIZE];
b508a996
RE
8785
8786 while (len > 0)
8787 {
8788 memcpy (tmpbuf, valbuf,
7a5ea0d4 8789 len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
b508a996 8790 regcache_cooked_write (regs, regno++, tmpbuf);
7a5ea0d4
DJ
8791 len -= INT_REGISTER_SIZE;
8792 valbuf += INT_REGISTER_SIZE;
b508a996
RE
8793 }
8794 }
34e8f22d
RE
8795}
8796
2af48f68
PB
8797
8798/* Handle function return values. */
8799
8800static enum return_value_convention
c055b101
CV
8801arm_return_value (struct gdbarch *gdbarch, struct type *func_type,
8802 struct type *valtype, struct regcache *regcache,
8803 gdb_byte *readbuf, const gdb_byte *writebuf)
2af48f68 8804{
7c00367c 8805 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
90445bd3
DJ
8806 enum arm_vfp_cprc_base_type vfp_base_type;
8807 int vfp_base_count;
8808
8809 if (arm_vfp_abi_for_function (gdbarch, func_type)
8810 && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
8811 {
8812 int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
8813 int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
8814 int i;
8815 for (i = 0; i < vfp_base_count; i++)
8816 {
58d6951d
DJ
8817 if (reg_char == 'q')
8818 {
8819 if (writebuf)
8820 arm_neon_quad_write (gdbarch, regcache, i,
8821 writebuf + i * unit_length);
8822
8823 if (readbuf)
8824 arm_neon_quad_read (gdbarch, regcache, i,
8825 readbuf + i * unit_length);
8826 }
8827 else
8828 {
8829 char name_buf[4];
8830 int regnum;
8831
8832 sprintf (name_buf, "%c%d", reg_char, i);
8833 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8834 strlen (name_buf));
8835 if (writebuf)
8836 regcache_cooked_write (regcache, regnum,
8837 writebuf + i * unit_length);
8838 if (readbuf)
8839 regcache_cooked_read (regcache, regnum,
8840 readbuf + i * unit_length);
8841 }
90445bd3
DJ
8842 }
8843 return RETURN_VALUE_REGISTER_CONVENTION;
8844 }
7c00367c 8845
2af48f68
PB
8846 if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
8847 || TYPE_CODE (valtype) == TYPE_CODE_UNION
8848 || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
8849 {
7c00367c
MK
8850 if (tdep->struct_return == pcc_struct_return
8851 || arm_return_in_memory (gdbarch, valtype))
2af48f68
PB
8852 return RETURN_VALUE_STRUCT_CONVENTION;
8853 }
8854
8855 if (writebuf)
8856 arm_store_return_value (valtype, regcache, writebuf);
8857
8858 if (readbuf)
8859 arm_extract_return_value (valtype, regcache, readbuf);
8860
8861 return RETURN_VALUE_REGISTER_CONVENTION;
8862}
8863
8864
9df628e0 8865static int
60ade65d 8866arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
9df628e0 8867{
e17a4113
UW
8868 struct gdbarch *gdbarch = get_frame_arch (frame);
8869 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
8870 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
9df628e0 8871 CORE_ADDR jb_addr;
7a5ea0d4 8872 char buf[INT_REGISTER_SIZE];
9df628e0 8873
60ade65d 8874 jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
9df628e0
RE
8875
8876 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
7a5ea0d4 8877 INT_REGISTER_SIZE))
9df628e0
RE
8878 return 0;
8879
e17a4113 8880 *pc = extract_unsigned_integer (buf, INT_REGISTER_SIZE, byte_order);
9df628e0
RE
8881 return 1;
8882}
8883
faa95490
DJ
8884/* Recognize GCC and GNU ld's trampolines. If we are in a trampoline,
8885 return the target PC. Otherwise return 0. */
c906108c
SS
8886
8887CORE_ADDR
52f729a7 8888arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
c906108c 8889{
c5aa993b 8890 char *name;
faa95490 8891 int namelen;
c906108c
SS
8892 CORE_ADDR start_addr;
8893
8894 /* Find the starting address and name of the function containing the PC. */
8895 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
8896 return 0;
8897
faa95490
DJ
8898 /* If PC is in a Thumb call or return stub, return the address of the
8899 target PC, which is in a register. The thunk functions are called
8900 _call_via_xx, where x is the register name. The possible names
3d8d5e79
DJ
8901 are r0-r9, sl, fp, ip, sp, and lr. ARM RealView has similar
8902 functions, named __ARM_call_via_r[0-7]. */
8903 if (strncmp (name, "_call_via_", 10) == 0
8904 || strncmp (name, "__ARM_call_via_", strlen ("__ARM_call_via_")) == 0)
c906108c 8905 {
ed9a39eb
JM
8906 /* Use the name suffix to determine which register contains the
8907 target PC. */
c5aa993b
JM
8908 static char *table[15] =
8909 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
8910 "r8", "r9", "sl", "fp", "ip", "sp", "lr"
8911 };
c906108c 8912 int regno;
faa95490 8913 int offset = strlen (name) - 2;
c906108c
SS
8914
8915 for (regno = 0; regno <= 14; regno++)
faa95490 8916 if (strcmp (&name[offset], table[regno]) == 0)
52f729a7 8917 return get_frame_register_unsigned (frame, regno);
c906108c 8918 }
ed9a39eb 8919
faa95490
DJ
8920 /* GNU ld generates __foo_from_arm or __foo_from_thumb for
8921 non-interworking calls to foo. We could decode the stubs
8922 to find the target but it's easier to use the symbol table. */
8923 namelen = strlen (name);
8924 if (name[0] == '_' && name[1] == '_'
8925 && ((namelen > 2 + strlen ("_from_thumb")
8926 && strncmp (name + namelen - strlen ("_from_thumb"), "_from_thumb",
8927 strlen ("_from_thumb")) == 0)
8928 || (namelen > 2 + strlen ("_from_arm")
8929 && strncmp (name + namelen - strlen ("_from_arm"), "_from_arm",
8930 strlen ("_from_arm")) == 0)))
8931 {
8932 char *target_name;
8933 int target_len = namelen - 2;
8934 struct minimal_symbol *minsym;
8935 struct objfile *objfile;
8936 struct obj_section *sec;
8937
8938 if (name[namelen - 1] == 'b')
8939 target_len -= strlen ("_from_thumb");
8940 else
8941 target_len -= strlen ("_from_arm");
8942
8943 target_name = alloca (target_len + 1);
8944 memcpy (target_name, name + 2, target_len);
8945 target_name[target_len] = '\0';
8946
8947 sec = find_pc_section (pc);
8948 objfile = (sec == NULL) ? NULL : sec->objfile;
8949 minsym = lookup_minimal_symbol (target_name, NULL, objfile);
8950 if (minsym != NULL)
8951 return SYMBOL_VALUE_ADDRESS (minsym);
8952 else
8953 return 0;
8954 }
8955
c5aa993b 8956 return 0; /* not a stub */
c906108c
SS
8957}
8958
afd7eef0
RE
8959static void
8960set_arm_command (char *args, int from_tty)
8961{
edefbb7c
AC
8962 printf_unfiltered (_("\
8963\"set arm\" must be followed by an apporpriate subcommand.\n"));
afd7eef0
RE
8964 help_list (setarmcmdlist, "set arm ", all_commands, gdb_stdout);
8965}
8966
8967static void
8968show_arm_command (char *args, int from_tty)
8969{
26304000 8970 cmd_show_list (showarmcmdlist, from_tty, "");
afd7eef0
RE
8971}
8972
28e97307
DJ
8973static void
8974arm_update_current_architecture (void)
fd50bc42 8975{
28e97307 8976 struct gdbarch_info info;
fd50bc42 8977
28e97307 8978 /* If the current architecture is not ARM, we have nothing to do. */
1cf3db46 8979 if (gdbarch_bfd_arch_info (target_gdbarch)->arch != bfd_arch_arm)
28e97307 8980 return;
fd50bc42 8981
28e97307
DJ
8982 /* Update the architecture. */
8983 gdbarch_info_init (&info);
fd50bc42 8984
28e97307 8985 if (!gdbarch_update_p (info))
9b20d036 8986 internal_error (__FILE__, __LINE__, _("could not update architecture"));
fd50bc42
RE
8987}
8988
8989static void
8990set_fp_model_sfunc (char *args, int from_tty,
8991 struct cmd_list_element *c)
8992{
8993 enum arm_float_model fp_model;
8994
8995 for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
8996 if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
8997 {
8998 arm_fp_model = fp_model;
8999 break;
9000 }
9001
9002 if (fp_model == ARM_FLOAT_LAST)
edefbb7c 9003 internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
fd50bc42
RE
9004 current_fp_model);
9005
28e97307 9006 arm_update_current_architecture ();
fd50bc42
RE
9007}
9008
9009static void
08546159
AC
9010show_fp_model (struct ui_file *file, int from_tty,
9011 struct cmd_list_element *c, const char *value)
fd50bc42 9012{
1cf3db46 9013 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
fd50bc42 9014
28e97307 9015 if (arm_fp_model == ARM_FLOAT_AUTO
1cf3db46 9016 && gdbarch_bfd_arch_info (target_gdbarch)->arch == bfd_arch_arm)
28e97307
DJ
9017 fprintf_filtered (file, _("\
9018The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
9019 fp_model_strings[tdep->fp_model]);
9020 else
9021 fprintf_filtered (file, _("\
9022The current ARM floating point model is \"%s\".\n"),
9023 fp_model_strings[arm_fp_model]);
9024}
9025
9026static void
9027arm_set_abi (char *args, int from_tty,
9028 struct cmd_list_element *c)
9029{
9030 enum arm_abi_kind arm_abi;
9031
9032 for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
9033 if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
9034 {
9035 arm_abi_global = arm_abi;
9036 break;
9037 }
9038
9039 if (arm_abi == ARM_ABI_LAST)
9040 internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
9041 arm_abi_string);
9042
9043 arm_update_current_architecture ();
9044}
9045
9046static void
9047arm_show_abi (struct ui_file *file, int from_tty,
9048 struct cmd_list_element *c, const char *value)
9049{
1cf3db46 9050 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
28e97307
DJ
9051
9052 if (arm_abi_global == ARM_ABI_AUTO
1cf3db46 9053 && gdbarch_bfd_arch_info (target_gdbarch)->arch == bfd_arch_arm)
28e97307
DJ
9054 fprintf_filtered (file, _("\
9055The current ARM ABI is \"auto\" (currently \"%s\").\n"),
9056 arm_abi_strings[tdep->arm_abi]);
9057 else
9058 fprintf_filtered (file, _("The current ARM ABI is \"%s\".\n"),
9059 arm_abi_string);
fd50bc42
RE
9060}
9061
0428b8f5
DJ
9062static void
9063arm_show_fallback_mode (struct ui_file *file, int from_tty,
9064 struct cmd_list_element *c, const char *value)
9065{
1cf3db46 9066 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
0428b8f5 9067
0963b4bd
MS
9068 fprintf_filtered (file,
9069 _("The current execution mode assumed "
9070 "(when symbols are unavailable) is \"%s\".\n"),
0428b8f5
DJ
9071 arm_fallback_mode_string);
9072}
9073
9074static void
9075arm_show_force_mode (struct ui_file *file, int from_tty,
9076 struct cmd_list_element *c, const char *value)
9077{
1cf3db46 9078 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
0428b8f5 9079
0963b4bd
MS
9080 fprintf_filtered (file,
9081 _("The current execution mode assumed "
9082 "(even when symbols are available) is \"%s\".\n"),
0428b8f5
DJ
9083 arm_force_mode_string);
9084}
9085
afd7eef0
RE
9086/* If the user changes the register disassembly style used for info
9087 register and other commands, we have to also switch the style used
9088 in opcodes for disassembly output. This function is run in the "set
9089 arm disassembly" command, and does that. */
bc90b915
FN
9090
9091static void
afd7eef0 9092set_disassembly_style_sfunc (char *args, int from_tty,
bc90b915
FN
9093 struct cmd_list_element *c)
9094{
afd7eef0 9095 set_disassembly_style ();
bc90b915
FN
9096}
9097\f
966fbf70 9098/* Return the ARM register name corresponding to register I. */
a208b0cb 9099static const char *
d93859e2 9100arm_register_name (struct gdbarch *gdbarch, int i)
966fbf70 9101{
58d6951d
DJ
9102 const int num_regs = gdbarch_num_regs (gdbarch);
9103
9104 if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
9105 && i >= num_regs && i < num_regs + 32)
9106 {
9107 static const char *const vfp_pseudo_names[] = {
9108 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
9109 "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
9110 "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
9111 "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
9112 };
9113
9114 return vfp_pseudo_names[i - num_regs];
9115 }
9116
9117 if (gdbarch_tdep (gdbarch)->have_neon_pseudos
9118 && i >= num_regs + 32 && i < num_regs + 32 + 16)
9119 {
9120 static const char *const neon_pseudo_names[] = {
9121 "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
9122 "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
9123 };
9124
9125 return neon_pseudo_names[i - num_regs - 32];
9126 }
9127
ff6f572f
DJ
9128 if (i >= ARRAY_SIZE (arm_register_names))
9129 /* These registers are only supported on targets which supply
9130 an XML description. */
9131 return "";
9132
966fbf70
RE
9133 return arm_register_names[i];
9134}
9135
bc90b915 9136static void
afd7eef0 9137set_disassembly_style (void)
bc90b915 9138{
123dc839 9139 int current;
bc90b915 9140
123dc839
DJ
9141 /* Find the style that the user wants. */
9142 for (current = 0; current < num_disassembly_options; current++)
9143 if (disassembly_style == valid_disassembly_styles[current])
9144 break;
9145 gdb_assert (current < num_disassembly_options);
bc90b915 9146
94c30b78 9147 /* Synchronize the disassembler. */
bc90b915
FN
9148 set_arm_regname_option (current);
9149}
9150
082fc60d
RE
9151/* Test whether the coff symbol specific value corresponds to a Thumb
9152 function. */
9153
9154static int
9155coff_sym_is_thumb (int val)
9156{
f8bf5763
PM
9157 return (val == C_THUMBEXT
9158 || val == C_THUMBSTAT
9159 || val == C_THUMBEXTFUNC
9160 || val == C_THUMBSTATFUNC
9161 || val == C_THUMBLABEL);
082fc60d
RE
9162}
9163
9164/* arm_coff_make_msymbol_special()
9165 arm_elf_make_msymbol_special()
9166
9167 These functions test whether the COFF or ELF symbol corresponds to
9168 an address in thumb code, and set a "special" bit in a minimal
9169 symbol to indicate that it does. */
9170
34e8f22d 9171static void
082fc60d
RE
9172arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
9173{
467d42c4
UW
9174 if (ARM_SYM_BRANCH_TYPE (&((elf_symbol_type *)sym)->internal_elf_sym)
9175 == ST_BRANCH_TO_THUMB)
082fc60d
RE
9176 MSYMBOL_SET_SPECIAL (msym);
9177}
9178
34e8f22d 9179static void
082fc60d
RE
9180arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
9181{
9182 if (coff_sym_is_thumb (val))
9183 MSYMBOL_SET_SPECIAL (msym);
9184}
9185
60c5725c 9186static void
c1bd65d0 9187arm_objfile_data_free (struct objfile *objfile, void *arg)
60c5725c
DJ
9188{
9189 struct arm_per_objfile *data = arg;
9190 unsigned int i;
9191
9192 for (i = 0; i < objfile->obfd->section_count; i++)
9193 VEC_free (arm_mapping_symbol_s, data->section_maps[i]);
9194}
9195
9196static void
9197arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
9198 asymbol *sym)
9199{
9200 const char *name = bfd_asymbol_name (sym);
9201 struct arm_per_objfile *data;
9202 VEC(arm_mapping_symbol_s) **map_p;
9203 struct arm_mapping_symbol new_map_sym;
9204
9205 gdb_assert (name[0] == '$');
9206 if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
9207 return;
9208
9209 data = objfile_data (objfile, arm_objfile_data_key);
9210 if (data == NULL)
9211 {
9212 data = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9213 struct arm_per_objfile);
9214 set_objfile_data (objfile, arm_objfile_data_key, data);
9215 data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
9216 objfile->obfd->section_count,
9217 VEC(arm_mapping_symbol_s) *);
9218 }
9219 map_p = &data->section_maps[bfd_get_section (sym)->index];
9220
9221 new_map_sym.value = sym->value;
9222 new_map_sym.type = name[1];
9223
9224 /* Assume that most mapping symbols appear in order of increasing
9225 value. If they were randomly distributed, it would be faster to
9226 always push here and then sort at first use. */
9227 if (!VEC_empty (arm_mapping_symbol_s, *map_p))
9228 {
9229 struct arm_mapping_symbol *prev_map_sym;
9230
9231 prev_map_sym = VEC_last (arm_mapping_symbol_s, *map_p);
9232 if (prev_map_sym->value >= sym->value)
9233 {
9234 unsigned int idx;
9235 idx = VEC_lower_bound (arm_mapping_symbol_s, *map_p, &new_map_sym,
9236 arm_compare_mapping_symbols);
9237 VEC_safe_insert (arm_mapping_symbol_s, *map_p, idx, &new_map_sym);
9238 return;
9239 }
9240 }
9241
9242 VEC_safe_push (arm_mapping_symbol_s, *map_p, &new_map_sym);
9243}
9244
756fe439 9245static void
61a1198a 9246arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
756fe439 9247{
9779414d 9248 struct gdbarch *gdbarch = get_regcache_arch (regcache);
61a1198a 9249 regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
756fe439
DJ
9250
9251 /* If necessary, set the T bit. */
9252 if (arm_apcs_32)
9253 {
9779414d 9254 ULONGEST val, t_bit;
61a1198a 9255 regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
9779414d
DJ
9256 t_bit = arm_psr_thumb_bit (gdbarch);
9257 if (arm_pc_is_thumb (gdbarch, pc))
9258 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9259 val | t_bit);
756fe439 9260 else
61a1198a 9261 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9779414d 9262 val & ~t_bit);
756fe439
DJ
9263 }
9264}
123dc839 9265
58d6951d
DJ
9266/* Read the contents of a NEON quad register, by reading from two
9267 double registers. This is used to implement the quad pseudo
9268 registers, and for argument passing in case the quad registers are
9269 missing; vectors are passed in quad registers when using the VFP
9270 ABI, even if a NEON unit is not present. REGNUM is the index of
9271 the quad register, in [0, 15]. */
9272
05d1431c 9273static enum register_status
58d6951d
DJ
9274arm_neon_quad_read (struct gdbarch *gdbarch, struct regcache *regcache,
9275 int regnum, gdb_byte *buf)
9276{
9277 char name_buf[4];
9278 gdb_byte reg_buf[8];
9279 int offset, double_regnum;
05d1431c 9280 enum register_status status;
58d6951d
DJ
9281
9282 sprintf (name_buf, "d%d", regnum << 1);
9283 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9284 strlen (name_buf));
9285
9286 /* d0 is always the least significant half of q0. */
9287 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9288 offset = 8;
9289 else
9290 offset = 0;
9291
05d1431c
PA
9292 status = regcache_raw_read (regcache, double_regnum, reg_buf);
9293 if (status != REG_VALID)
9294 return status;
58d6951d
DJ
9295 memcpy (buf + offset, reg_buf, 8);
9296
9297 offset = 8 - offset;
05d1431c
PA
9298 status = regcache_raw_read (regcache, double_regnum + 1, reg_buf);
9299 if (status != REG_VALID)
9300 return status;
58d6951d 9301 memcpy (buf + offset, reg_buf, 8);
05d1431c
PA
9302
9303 return REG_VALID;
58d6951d
DJ
9304}
9305
05d1431c 9306static enum register_status
58d6951d
DJ
9307arm_pseudo_read (struct gdbarch *gdbarch, struct regcache *regcache,
9308 int regnum, gdb_byte *buf)
9309{
9310 const int num_regs = gdbarch_num_regs (gdbarch);
9311 char name_buf[4];
9312 gdb_byte reg_buf[8];
9313 int offset, double_regnum;
9314
9315 gdb_assert (regnum >= num_regs);
9316 regnum -= num_regs;
9317
9318 if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
9319 /* Quad-precision register. */
05d1431c 9320 return arm_neon_quad_read (gdbarch, regcache, regnum - 32, buf);
58d6951d
DJ
9321 else
9322 {
05d1431c
PA
9323 enum register_status status;
9324
58d6951d
DJ
9325 /* Single-precision register. */
9326 gdb_assert (regnum < 32);
9327
9328 /* s0 is always the least significant half of d0. */
9329 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9330 offset = (regnum & 1) ? 0 : 4;
9331 else
9332 offset = (regnum & 1) ? 4 : 0;
9333
9334 sprintf (name_buf, "d%d", regnum >> 1);
9335 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9336 strlen (name_buf));
9337
05d1431c
PA
9338 status = regcache_raw_read (regcache, double_regnum, reg_buf);
9339 if (status == REG_VALID)
9340 memcpy (buf, reg_buf + offset, 4);
9341 return status;
58d6951d
DJ
9342 }
9343}
9344
9345/* Store the contents of BUF to a NEON quad register, by writing to
9346 two double registers. This is used to implement the quad pseudo
9347 registers, and for argument passing in case the quad registers are
9348 missing; vectors are passed in quad registers when using the VFP
9349 ABI, even if a NEON unit is not present. REGNUM is the index
9350 of the quad register, in [0, 15]. */
9351
9352static void
9353arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
9354 int regnum, const gdb_byte *buf)
9355{
9356 char name_buf[4];
9357 gdb_byte reg_buf[8];
9358 int offset, double_regnum;
9359
9360 sprintf (name_buf, "d%d", regnum << 1);
9361 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9362 strlen (name_buf));
9363
9364 /* d0 is always the least significant half of q0. */
9365 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9366 offset = 8;
9367 else
9368 offset = 0;
9369
9370 regcache_raw_write (regcache, double_regnum, buf + offset);
9371 offset = 8 - offset;
9372 regcache_raw_write (regcache, double_regnum + 1, buf + offset);
9373}
9374
9375static void
9376arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
9377 int regnum, const gdb_byte *buf)
9378{
9379 const int num_regs = gdbarch_num_regs (gdbarch);
9380 char name_buf[4];
9381 gdb_byte reg_buf[8];
9382 int offset, double_regnum;
9383
9384 gdb_assert (regnum >= num_regs);
9385 regnum -= num_regs;
9386
9387 if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
9388 /* Quad-precision register. */
9389 arm_neon_quad_write (gdbarch, regcache, regnum - 32, buf);
9390 else
9391 {
9392 /* Single-precision register. */
9393 gdb_assert (regnum < 32);
9394
9395 /* s0 is always the least significant half of d0. */
9396 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9397 offset = (regnum & 1) ? 0 : 4;
9398 else
9399 offset = (regnum & 1) ? 4 : 0;
9400
9401 sprintf (name_buf, "d%d", regnum >> 1);
9402 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9403 strlen (name_buf));
9404
9405 regcache_raw_read (regcache, double_regnum, reg_buf);
9406 memcpy (reg_buf + offset, buf, 4);
9407 regcache_raw_write (regcache, double_regnum, reg_buf);
9408 }
9409}
9410
123dc839
DJ
9411static struct value *
9412value_of_arm_user_reg (struct frame_info *frame, const void *baton)
9413{
9414 const int *reg_p = baton;
9415 return value_of_register (*reg_p, frame);
9416}
97e03143 9417\f
70f80edf
JT
9418static enum gdb_osabi
9419arm_elf_osabi_sniffer (bfd *abfd)
97e03143 9420{
2af48f68 9421 unsigned int elfosabi;
70f80edf 9422 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
97e03143 9423
70f80edf 9424 elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
97e03143 9425
28e97307
DJ
9426 if (elfosabi == ELFOSABI_ARM)
9427 /* GNU tools use this value. Check note sections in this case,
9428 as well. */
9429 bfd_map_over_sections (abfd,
9430 generic_elf_osabi_sniff_abi_tag_sections,
9431 &osabi);
97e03143 9432
28e97307 9433 /* Anything else will be handled by the generic ELF sniffer. */
70f80edf 9434 return osabi;
97e03143
RE
9435}
9436
54483882
YQ
9437static int
9438arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
9439 struct reggroup *group)
9440{
2c291032
YQ
9441 /* FPS register's type is INT, but belongs to float_reggroup. Beside
9442 this, FPS register belongs to save_regroup, restore_reggroup, and
9443 all_reggroup, of course. */
54483882 9444 if (regnum == ARM_FPS_REGNUM)
2c291032
YQ
9445 return (group == float_reggroup
9446 || group == save_reggroup
9447 || group == restore_reggroup
9448 || group == all_reggroup);
54483882
YQ
9449 else
9450 return default_register_reggroup_p (gdbarch, regnum, group);
9451}
9452
70f80edf 9453\f
da3c6d4a
MS
9454/* Initialize the current architecture based on INFO. If possible,
9455 re-use an architecture from ARCHES, which is a list of
9456 architectures already created during this debugging session.
97e03143 9457
da3c6d4a
MS
9458 Called e.g. at program startup, when reading a core file, and when
9459 reading a binary file. */
97e03143 9460
39bbf761
RE
9461static struct gdbarch *
9462arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
9463{
97e03143 9464 struct gdbarch_tdep *tdep;
39bbf761 9465 struct gdbarch *gdbarch;
28e97307
DJ
9466 struct gdbarch_list *best_arch;
9467 enum arm_abi_kind arm_abi = arm_abi_global;
9468 enum arm_float_model fp_model = arm_fp_model;
123dc839 9469 struct tdesc_arch_data *tdesc_data = NULL;
9779414d 9470 int i, is_m = 0;
58d6951d
DJ
9471 int have_vfp_registers = 0, have_vfp_pseudos = 0, have_neon_pseudos = 0;
9472 int have_neon = 0;
ff6f572f 9473 int have_fpa_registers = 1;
9779414d
DJ
9474 const struct target_desc *tdesc = info.target_desc;
9475
9476 /* If we have an object to base this architecture on, try to determine
9477 its ABI. */
9478
9479 if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
9480 {
9481 int ei_osabi, e_flags;
9482
9483 switch (bfd_get_flavour (info.abfd))
9484 {
9485 case bfd_target_aout_flavour:
9486 /* Assume it's an old APCS-style ABI. */
9487 arm_abi = ARM_ABI_APCS;
9488 break;
9489
9490 case bfd_target_coff_flavour:
9491 /* Assume it's an old APCS-style ABI. */
9492 /* XXX WinCE? */
9493 arm_abi = ARM_ABI_APCS;
9494 break;
9495
9496 case bfd_target_elf_flavour:
9497 ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
9498 e_flags = elf_elfheader (info.abfd)->e_flags;
9499
9500 if (ei_osabi == ELFOSABI_ARM)
9501 {
9502 /* GNU tools used to use this value, but do not for EABI
9503 objects. There's nowhere to tag an EABI version
9504 anyway, so assume APCS. */
9505 arm_abi = ARM_ABI_APCS;
9506 }
9507 else if (ei_osabi == ELFOSABI_NONE)
9508 {
9509 int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
9510 int attr_arch, attr_profile;
9511
9512 switch (eabi_ver)
9513 {
9514 case EF_ARM_EABI_UNKNOWN:
9515 /* Assume GNU tools. */
9516 arm_abi = ARM_ABI_APCS;
9517 break;
9518
9519 case EF_ARM_EABI_VER4:
9520 case EF_ARM_EABI_VER5:
9521 arm_abi = ARM_ABI_AAPCS;
9522 /* EABI binaries default to VFP float ordering.
9523 They may also contain build attributes that can
9524 be used to identify if the VFP argument-passing
9525 ABI is in use. */
9526 if (fp_model == ARM_FLOAT_AUTO)
9527 {
9528#ifdef HAVE_ELF
9529 switch (bfd_elf_get_obj_attr_int (info.abfd,
9530 OBJ_ATTR_PROC,
9531 Tag_ABI_VFP_args))
9532 {
9533 case 0:
9534 /* "The user intended FP parameter/result
9535 passing to conform to AAPCS, base
9536 variant". */
9537 fp_model = ARM_FLOAT_SOFT_VFP;
9538 break;
9539 case 1:
9540 /* "The user intended FP parameter/result
9541 passing to conform to AAPCS, VFP
9542 variant". */
9543 fp_model = ARM_FLOAT_VFP;
9544 break;
9545 case 2:
9546 /* "The user intended FP parameter/result
9547 passing to conform to tool chain-specific
9548 conventions" - we don't know any such
9549 conventions, so leave it as "auto". */
9550 break;
9551 default:
9552 /* Attribute value not mentioned in the
9553 October 2008 ABI, so leave it as
9554 "auto". */
9555 break;
9556 }
9557#else
9558 fp_model = ARM_FLOAT_SOFT_VFP;
9559#endif
9560 }
9561 break;
9562
9563 default:
9564 /* Leave it as "auto". */
9565 warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
9566 break;
9567 }
9568
9569#ifdef HAVE_ELF
9570 /* Detect M-profile programs. This only works if the
9571 executable file includes build attributes; GCC does
9572 copy them to the executable, but e.g. RealView does
9573 not. */
9574 attr_arch = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9575 Tag_CPU_arch);
0963b4bd
MS
9576 attr_profile = bfd_elf_get_obj_attr_int (info.abfd,
9577 OBJ_ATTR_PROC,
9779414d
DJ
9578 Tag_CPU_arch_profile);
9579 /* GCC specifies the profile for v6-M; RealView only
9580 specifies the profile for architectures starting with
9581 V7 (as opposed to architectures with a tag
9582 numerically greater than TAG_CPU_ARCH_V7). */
9583 if (!tdesc_has_registers (tdesc)
9584 && (attr_arch == TAG_CPU_ARCH_V6_M
9585 || attr_arch == TAG_CPU_ARCH_V6S_M
9586 || attr_profile == 'M'))
9587 tdesc = tdesc_arm_with_m;
9588#endif
9589 }
9590
9591 if (fp_model == ARM_FLOAT_AUTO)
9592 {
9593 int e_flags = elf_elfheader (info.abfd)->e_flags;
9594
9595 switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
9596 {
9597 case 0:
9598 /* Leave it as "auto". Strictly speaking this case
9599 means FPA, but almost nobody uses that now, and
9600 many toolchains fail to set the appropriate bits
9601 for the floating-point model they use. */
9602 break;
9603 case EF_ARM_SOFT_FLOAT:
9604 fp_model = ARM_FLOAT_SOFT_FPA;
9605 break;
9606 case EF_ARM_VFP_FLOAT:
9607 fp_model = ARM_FLOAT_VFP;
9608 break;
9609 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
9610 fp_model = ARM_FLOAT_SOFT_VFP;
9611 break;
9612 }
9613 }
9614
9615 if (e_flags & EF_ARM_BE8)
9616 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
9617
9618 break;
9619
9620 default:
9621 /* Leave it as "auto". */
9622 break;
9623 }
9624 }
123dc839
DJ
9625
9626 /* Check any target description for validity. */
9779414d 9627 if (tdesc_has_registers (tdesc))
123dc839
DJ
9628 {
9629 /* For most registers we require GDB's default names; but also allow
9630 the numeric names for sp / lr / pc, as a convenience. */
9631 static const char *const arm_sp_names[] = { "r13", "sp", NULL };
9632 static const char *const arm_lr_names[] = { "r14", "lr", NULL };
9633 static const char *const arm_pc_names[] = { "r15", "pc", NULL };
9634
9635 const struct tdesc_feature *feature;
58d6951d 9636 int valid_p;
123dc839 9637
9779414d 9638 feature = tdesc_find_feature (tdesc,
123dc839
DJ
9639 "org.gnu.gdb.arm.core");
9640 if (feature == NULL)
9779414d
DJ
9641 {
9642 feature = tdesc_find_feature (tdesc,
9643 "org.gnu.gdb.arm.m-profile");
9644 if (feature == NULL)
9645 return NULL;
9646 else
9647 is_m = 1;
9648 }
123dc839
DJ
9649
9650 tdesc_data = tdesc_data_alloc ();
9651
9652 valid_p = 1;
9653 for (i = 0; i < ARM_SP_REGNUM; i++)
9654 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
9655 arm_register_names[i]);
9656 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
9657 ARM_SP_REGNUM,
9658 arm_sp_names);
9659 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
9660 ARM_LR_REGNUM,
9661 arm_lr_names);
9662 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
9663 ARM_PC_REGNUM,
9664 arm_pc_names);
9779414d
DJ
9665 if (is_m)
9666 valid_p &= tdesc_numbered_register (feature, tdesc_data,
9667 ARM_PS_REGNUM, "xpsr");
9668 else
9669 valid_p &= tdesc_numbered_register (feature, tdesc_data,
9670 ARM_PS_REGNUM, "cpsr");
123dc839
DJ
9671
9672 if (!valid_p)
9673 {
9674 tdesc_data_cleanup (tdesc_data);
9675 return NULL;
9676 }
9677
9779414d 9678 feature = tdesc_find_feature (tdesc,
123dc839
DJ
9679 "org.gnu.gdb.arm.fpa");
9680 if (feature != NULL)
9681 {
9682 valid_p = 1;
9683 for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
9684 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
9685 arm_register_names[i]);
9686 if (!valid_p)
9687 {
9688 tdesc_data_cleanup (tdesc_data);
9689 return NULL;
9690 }
9691 }
ff6f572f
DJ
9692 else
9693 have_fpa_registers = 0;
9694
9779414d 9695 feature = tdesc_find_feature (tdesc,
ff6f572f
DJ
9696 "org.gnu.gdb.xscale.iwmmxt");
9697 if (feature != NULL)
9698 {
9699 static const char *const iwmmxt_names[] = {
9700 "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
9701 "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
9702 "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
9703 "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
9704 };
9705
9706 valid_p = 1;
9707 for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
9708 valid_p
9709 &= tdesc_numbered_register (feature, tdesc_data, i,
9710 iwmmxt_names[i - ARM_WR0_REGNUM]);
9711
9712 /* Check for the control registers, but do not fail if they
9713 are missing. */
9714 for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
9715 tdesc_numbered_register (feature, tdesc_data, i,
9716 iwmmxt_names[i - ARM_WR0_REGNUM]);
9717
9718 for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
9719 valid_p
9720 &= tdesc_numbered_register (feature, tdesc_data, i,
9721 iwmmxt_names[i - ARM_WR0_REGNUM]);
9722
9723 if (!valid_p)
9724 {
9725 tdesc_data_cleanup (tdesc_data);
9726 return NULL;
9727 }
9728 }
58d6951d
DJ
9729
9730 /* If we have a VFP unit, check whether the single precision registers
9731 are present. If not, then we will synthesize them as pseudo
9732 registers. */
9779414d 9733 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
9734 "org.gnu.gdb.arm.vfp");
9735 if (feature != NULL)
9736 {
9737 static const char *const vfp_double_names[] = {
9738 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
9739 "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
9740 "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
9741 "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
9742 };
9743
9744 /* Require the double precision registers. There must be either
9745 16 or 32. */
9746 valid_p = 1;
9747 for (i = 0; i < 32; i++)
9748 {
9749 valid_p &= tdesc_numbered_register (feature, tdesc_data,
9750 ARM_D0_REGNUM + i,
9751 vfp_double_names[i]);
9752 if (!valid_p)
9753 break;
9754 }
2b9e5ea6
UW
9755 if (!valid_p && i == 16)
9756 valid_p = 1;
58d6951d 9757
2b9e5ea6
UW
9758 /* Also require FPSCR. */
9759 valid_p &= tdesc_numbered_register (feature, tdesc_data,
9760 ARM_FPSCR_REGNUM, "fpscr");
9761 if (!valid_p)
58d6951d
DJ
9762 {
9763 tdesc_data_cleanup (tdesc_data);
9764 return NULL;
9765 }
9766
9767 if (tdesc_unnumbered_register (feature, "s0") == 0)
9768 have_vfp_pseudos = 1;
9769
9770 have_vfp_registers = 1;
9771
9772 /* If we have VFP, also check for NEON. The architecture allows
9773 NEON without VFP (integer vector operations only), but GDB
9774 does not support that. */
9779414d 9775 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
9776 "org.gnu.gdb.arm.neon");
9777 if (feature != NULL)
9778 {
9779 /* NEON requires 32 double-precision registers. */
9780 if (i != 32)
9781 {
9782 tdesc_data_cleanup (tdesc_data);
9783 return NULL;
9784 }
9785
9786 /* If there are quad registers defined by the stub, use
9787 their type; otherwise (normally) provide them with
9788 the default type. */
9789 if (tdesc_unnumbered_register (feature, "q0") == 0)
9790 have_neon_pseudos = 1;
9791
9792 have_neon = 1;
9793 }
9794 }
123dc839 9795 }
39bbf761 9796
28e97307
DJ
9797 /* If there is already a candidate, use it. */
9798 for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
9799 best_arch != NULL;
9800 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
9801 {
b8926edc
DJ
9802 if (arm_abi != ARM_ABI_AUTO
9803 && arm_abi != gdbarch_tdep (best_arch->gdbarch)->arm_abi)
28e97307
DJ
9804 continue;
9805
b8926edc
DJ
9806 if (fp_model != ARM_FLOAT_AUTO
9807 && fp_model != gdbarch_tdep (best_arch->gdbarch)->fp_model)
28e97307
DJ
9808 continue;
9809
58d6951d
DJ
9810 /* There are various other properties in tdep that we do not
9811 need to check here: those derived from a target description,
9812 since gdbarches with a different target description are
9813 automatically disqualified. */
9814
9779414d
DJ
9815 /* Do check is_m, though, since it might come from the binary. */
9816 if (is_m != gdbarch_tdep (best_arch->gdbarch)->is_m)
9817 continue;
9818
28e97307
DJ
9819 /* Found a match. */
9820 break;
9821 }
97e03143 9822
28e97307 9823 if (best_arch != NULL)
123dc839
DJ
9824 {
9825 if (tdesc_data != NULL)
9826 tdesc_data_cleanup (tdesc_data);
9827 return best_arch->gdbarch;
9828 }
28e97307
DJ
9829
9830 tdep = xcalloc (1, sizeof (struct gdbarch_tdep));
97e03143
RE
9831 gdbarch = gdbarch_alloc (&info, tdep);
9832
28e97307
DJ
9833 /* Record additional information about the architecture we are defining.
9834 These are gdbarch discriminators, like the OSABI. */
9835 tdep->arm_abi = arm_abi;
9836 tdep->fp_model = fp_model;
9779414d 9837 tdep->is_m = is_m;
ff6f572f 9838 tdep->have_fpa_registers = have_fpa_registers;
58d6951d
DJ
9839 tdep->have_vfp_registers = have_vfp_registers;
9840 tdep->have_vfp_pseudos = have_vfp_pseudos;
9841 tdep->have_neon_pseudos = have_neon_pseudos;
9842 tdep->have_neon = have_neon;
08216dd7
RE
9843
9844 /* Breakpoints. */
9d4fde75 9845 switch (info.byte_order_for_code)
67255d04
RE
9846 {
9847 case BFD_ENDIAN_BIG:
66e810cd
RE
9848 tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
9849 tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
9850 tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
9851 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
9852
67255d04
RE
9853 break;
9854
9855 case BFD_ENDIAN_LITTLE:
66e810cd
RE
9856 tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
9857 tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
9858 tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
9859 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
9860
67255d04
RE
9861 break;
9862
9863 default:
9864 internal_error (__FILE__, __LINE__,
edefbb7c 9865 _("arm_gdbarch_init: bad byte order for float format"));
67255d04
RE
9866 }
9867
d7b486e7
RE
9868 /* On ARM targets char defaults to unsigned. */
9869 set_gdbarch_char_signed (gdbarch, 0);
9870
cca44b1b
JB
9871 /* Note: for displaced stepping, this includes the breakpoint, and one word
9872 of additional scratch space. This setting isn't used for anything beside
9873 displaced stepping at present. */
9874 set_gdbarch_max_insn_length (gdbarch, 4 * DISPLACED_MODIFIED_INSNS);
9875
9df628e0 9876 /* This should be low enough for everything. */
97e03143 9877 tdep->lowest_pc = 0x20;
94c30b78 9878 tdep->jb_pc = -1; /* Longjump support not enabled by default. */
97e03143 9879
7c00367c
MK
9880 /* The default, for both APCS and AAPCS, is to return small
9881 structures in registers. */
9882 tdep->struct_return = reg_struct_return;
9883
2dd604e7 9884 set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
f53f0d0b 9885 set_gdbarch_frame_align (gdbarch, arm_frame_align);
39bbf761 9886
756fe439
DJ
9887 set_gdbarch_write_pc (gdbarch, arm_write_pc);
9888
148754e5 9889 /* Frame handling. */
a262aec2 9890 set_gdbarch_dummy_id (gdbarch, arm_dummy_id);
eb5492fa
DJ
9891 set_gdbarch_unwind_pc (gdbarch, arm_unwind_pc);
9892 set_gdbarch_unwind_sp (gdbarch, arm_unwind_sp);
9893
eb5492fa 9894 frame_base_set_default (gdbarch, &arm_normal_base);
148754e5 9895
34e8f22d
RE
9896 /* Address manipulation. */
9897 set_gdbarch_smash_text_address (gdbarch, arm_smash_text_address);
9898 set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
9899
34e8f22d
RE
9900 /* Advance PC across function entry code. */
9901 set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
9902
4024ca99
UW
9903 /* Detect whether PC is in function epilogue. */
9904 set_gdbarch_in_function_epilogue_p (gdbarch, arm_in_function_epilogue_p);
9905
190dce09
UW
9906 /* Skip trampolines. */
9907 set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
9908
34e8f22d
RE
9909 /* The stack grows downward. */
9910 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
9911
9912 /* Breakpoint manipulation. */
9913 set_gdbarch_breakpoint_from_pc (gdbarch, arm_breakpoint_from_pc);
177321bd
DJ
9914 set_gdbarch_remote_breakpoint_from_pc (gdbarch,
9915 arm_remote_breakpoint_from_pc);
34e8f22d
RE
9916
9917 /* Information about registers, etc. */
34e8f22d
RE
9918 set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
9919 set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
ff6f572f 9920 set_gdbarch_num_regs (gdbarch, ARM_NUM_REGS);
7a5ea0d4 9921 set_gdbarch_register_type (gdbarch, arm_register_type);
54483882 9922 set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);
34e8f22d 9923
ff6f572f
DJ
9924 /* This "info float" is FPA-specific. Use the generic version if we
9925 do not have FPA. */
9926 if (gdbarch_tdep (gdbarch)->have_fpa_registers)
9927 set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
9928
26216b98 9929 /* Internal <-> external register number maps. */
ff6f572f 9930 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
26216b98
AC
9931 set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
9932
34e8f22d
RE
9933 set_gdbarch_register_name (gdbarch, arm_register_name);
9934
9935 /* Returning results. */
2af48f68 9936 set_gdbarch_return_value (gdbarch, arm_return_value);
34e8f22d 9937
03d48a7d
RE
9938 /* Disassembly. */
9939 set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
9940
34e8f22d
RE
9941 /* Minsymbol frobbing. */
9942 set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
9943 set_gdbarch_coff_make_msymbol_special (gdbarch,
9944 arm_coff_make_msymbol_special);
60c5725c 9945 set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
34e8f22d 9946
f9d67f43
DJ
9947 /* Thumb-2 IT block support. */
9948 set_gdbarch_adjust_breakpoint_address (gdbarch,
9949 arm_adjust_breakpoint_address);
9950
0d5de010
DJ
9951 /* Virtual tables. */
9952 set_gdbarch_vbit_in_delta (gdbarch, 1);
9953
97e03143 9954 /* Hook in the ABI-specific overrides, if they have been registered. */
4be87837 9955 gdbarch_init_osabi (info, gdbarch);
97e03143 9956
b39cc962
DJ
9957 dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
9958
eb5492fa 9959 /* Add some default predicates. */
a262aec2
DJ
9960 frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
9961 dwarf2_append_unwinders (gdbarch);
0e9e9abd 9962 frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
a262aec2 9963 frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
eb5492fa 9964
97e03143
RE
9965 /* Now we have tuned the configuration, set a few final things,
9966 based on what the OS ABI has told us. */
9967
b8926edc
DJ
9968 /* If the ABI is not otherwise marked, assume the old GNU APCS. EABI
9969 binaries are always marked. */
9970 if (tdep->arm_abi == ARM_ABI_AUTO)
9971 tdep->arm_abi = ARM_ABI_APCS;
9972
e3039479
UW
9973 /* Watchpoints are not steppable. */
9974 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
9975
b8926edc
DJ
9976 /* We used to default to FPA for generic ARM, but almost nobody
9977 uses that now, and we now provide a way for the user to force
9978 the model. So default to the most useful variant. */
9979 if (tdep->fp_model == ARM_FLOAT_AUTO)
9980 tdep->fp_model = ARM_FLOAT_SOFT_FPA;
9981
9df628e0
RE
9982 if (tdep->jb_pc >= 0)
9983 set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
9984
08216dd7 9985 /* Floating point sizes and format. */
8da61cc4 9986 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
b8926edc 9987 if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
08216dd7 9988 {
8da61cc4
DJ
9989 set_gdbarch_double_format
9990 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
9991 set_gdbarch_long_double_format
9992 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
9993 }
9994 else
9995 {
9996 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
9997 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
08216dd7
RE
9998 }
9999
58d6951d
DJ
10000 if (have_vfp_pseudos)
10001 {
10002 /* NOTE: These are the only pseudo registers used by
10003 the ARM target at the moment. If more are added, a
10004 little more care in numbering will be needed. */
10005
10006 int num_pseudos = 32;
10007 if (have_neon_pseudos)
10008 num_pseudos += 16;
10009 set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
10010 set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
10011 set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
10012 }
10013
123dc839 10014 if (tdesc_data)
58d6951d
DJ
10015 {
10016 set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
10017
9779414d 10018 tdesc_use_registers (gdbarch, tdesc, tdesc_data);
58d6951d
DJ
10019
10020 /* Override tdesc_register_type to adjust the types of VFP
10021 registers for NEON. */
10022 set_gdbarch_register_type (gdbarch, arm_register_type);
10023 }
123dc839
DJ
10024
10025 /* Add standard register aliases. We add aliases even for those
10026 nanes which are used by the current architecture - it's simpler,
10027 and does no harm, since nothing ever lists user registers. */
10028 for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
10029 user_reg_add (gdbarch, arm_register_aliases[i].name,
10030 value_of_arm_user_reg, &arm_register_aliases[i].regnum);
10031
39bbf761
RE
10032 return gdbarch;
10033}
10034
97e03143 10035static void
2af46ca0 10036arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
97e03143 10037{
2af46ca0 10038 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
97e03143
RE
10039
10040 if (tdep == NULL)
10041 return;
10042
edefbb7c 10043 fprintf_unfiltered (file, _("arm_dump_tdep: Lowest pc = 0x%lx"),
97e03143
RE
10044 (unsigned long) tdep->lowest_pc);
10045}
10046
a78f21af
AC
10047extern initialize_file_ftype _initialize_arm_tdep; /* -Wmissing-prototypes */
10048
c906108c 10049void
ed9a39eb 10050_initialize_arm_tdep (void)
c906108c 10051{
bc90b915
FN
10052 struct ui_file *stb;
10053 long length;
26304000 10054 struct cmd_list_element *new_set, *new_show;
53904c9e
AC
10055 const char *setname;
10056 const char *setdesc;
4bd7b427 10057 const char *const *regnames;
bc90b915
FN
10058 int numregs, i, j;
10059 static char *helptext;
edefbb7c
AC
10060 char regdesc[1024], *rdptr = regdesc;
10061 size_t rest = sizeof (regdesc);
085dd6e6 10062
42cf1509 10063 gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
97e03143 10064
60c5725c 10065 arm_objfile_data_key
c1bd65d0 10066 = register_objfile_data_with_cleanup (NULL, arm_objfile_data_free);
60c5725c 10067
0e9e9abd
UW
10068 /* Add ourselves to objfile event chain. */
10069 observer_attach_new_objfile (arm_exidx_new_objfile);
10070 arm_exidx_data_key
10071 = register_objfile_data_with_cleanup (NULL, arm_exidx_data_free);
10072
70f80edf
JT
10073 /* Register an ELF OS ABI sniffer for ARM binaries. */
10074 gdbarch_register_osabi_sniffer (bfd_arch_arm,
10075 bfd_target_elf_flavour,
10076 arm_elf_osabi_sniffer);
10077
9779414d
DJ
10078 /* Initialize the standard target descriptions. */
10079 initialize_tdesc_arm_with_m ();
ef7e8358
UW
10080 initialize_tdesc_arm_with_iwmmxt ();
10081 initialize_tdesc_arm_with_vfpv2 ();
10082 initialize_tdesc_arm_with_vfpv3 ();
10083 initialize_tdesc_arm_with_neon ();
9779414d 10084
94c30b78 10085 /* Get the number of possible sets of register names defined in opcodes. */
afd7eef0
RE
10086 num_disassembly_options = get_arm_regname_num_options ();
10087
10088 /* Add root prefix command for all "set arm"/"show arm" commands. */
10089 add_prefix_cmd ("arm", no_class, set_arm_command,
edefbb7c 10090 _("Various ARM-specific commands."),
afd7eef0
RE
10091 &setarmcmdlist, "set arm ", 0, &setlist);
10092
10093 add_prefix_cmd ("arm", no_class, show_arm_command,
edefbb7c 10094 _("Various ARM-specific commands."),
afd7eef0 10095 &showarmcmdlist, "show arm ", 0, &showlist);
bc90b915 10096
94c30b78 10097 /* Sync the opcode insn printer with our register viewer. */
bc90b915 10098 parse_arm_disassembler_option ("reg-names-std");
c5aa993b 10099
eefe576e
AC
10100 /* Initialize the array that will be passed to
10101 add_setshow_enum_cmd(). */
afd7eef0
RE
10102 valid_disassembly_styles
10103 = xmalloc ((num_disassembly_options + 1) * sizeof (char *));
10104 for (i = 0; i < num_disassembly_options; i++)
bc90b915
FN
10105 {
10106 numregs = get_arm_regnames (i, &setname, &setdesc, &regnames);
afd7eef0 10107 valid_disassembly_styles[i] = setname;
edefbb7c
AC
10108 length = snprintf (rdptr, rest, "%s - %s\n", setname, setdesc);
10109 rdptr += length;
10110 rest -= length;
123dc839
DJ
10111 /* When we find the default names, tell the disassembler to use
10112 them. */
bc90b915
FN
10113 if (!strcmp (setname, "std"))
10114 {
afd7eef0 10115 disassembly_style = setname;
bc90b915
FN
10116 set_arm_regname_option (i);
10117 }
10118 }
94c30b78 10119 /* Mark the end of valid options. */
afd7eef0 10120 valid_disassembly_styles[num_disassembly_options] = NULL;
c906108c 10121
edefbb7c
AC
10122 /* Create the help text. */
10123 stb = mem_fileopen ();
10124 fprintf_unfiltered (stb, "%s%s%s",
10125 _("The valid values are:\n"),
10126 regdesc,
10127 _("The default is \"std\"."));
759ef836 10128 helptext = ui_file_xstrdup (stb, NULL);
bc90b915 10129 ui_file_delete (stb);
ed9a39eb 10130
edefbb7c
AC
10131 add_setshow_enum_cmd("disassembler", no_class,
10132 valid_disassembly_styles, &disassembly_style,
10133 _("Set the disassembly style."),
10134 _("Show the disassembly style."),
10135 helptext,
2c5b56ce 10136 set_disassembly_style_sfunc,
0963b4bd
MS
10137 NULL, /* FIXME: i18n: The disassembly style is
10138 \"%s\". */
7376b4c2 10139 &setarmcmdlist, &showarmcmdlist);
edefbb7c
AC
10140
10141 add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
10142 _("Set usage of ARM 32-bit mode."),
10143 _("Show usage of ARM 32-bit mode."),
10144 _("When off, a 26-bit PC will be used."),
2c5b56ce 10145 NULL,
0963b4bd
MS
10146 NULL, /* FIXME: i18n: Usage of ARM 32-bit
10147 mode is %s. */
26304000 10148 &setarmcmdlist, &showarmcmdlist);
c906108c 10149
fd50bc42 10150 /* Add a command to allow the user to force the FPU model. */
edefbb7c
AC
10151 add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
10152 _("Set the floating point type."),
10153 _("Show the floating point type."),
10154 _("auto - Determine the FP typefrom the OS-ABI.\n\
10155softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
10156fpa - FPA co-processor (GCC compiled).\n\
10157softvfp - Software FP with pure-endian doubles.\n\
10158vfp - VFP co-processor."),
edefbb7c 10159 set_fp_model_sfunc, show_fp_model,
7376b4c2 10160 &setarmcmdlist, &showarmcmdlist);
fd50bc42 10161
28e97307
DJ
10162 /* Add a command to allow the user to force the ABI. */
10163 add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
10164 _("Set the ABI."),
10165 _("Show the ABI."),
10166 NULL, arm_set_abi, arm_show_abi,
10167 &setarmcmdlist, &showarmcmdlist);
10168
0428b8f5
DJ
10169 /* Add two commands to allow the user to force the assumed
10170 execution mode. */
10171 add_setshow_enum_cmd ("fallback-mode", class_support,
10172 arm_mode_strings, &arm_fallback_mode_string,
10173 _("Set the mode assumed when symbols are unavailable."),
10174 _("Show the mode assumed when symbols are unavailable."),
10175 NULL, NULL, arm_show_fallback_mode,
10176 &setarmcmdlist, &showarmcmdlist);
10177 add_setshow_enum_cmd ("force-mode", class_support,
10178 arm_mode_strings, &arm_force_mode_string,
10179 _("Set the mode assumed even when symbols are available."),
10180 _("Show the mode assumed even when symbols are available."),
10181 NULL, NULL, arm_show_force_mode,
10182 &setarmcmdlist, &showarmcmdlist);
10183
6529d2dd 10184 /* Debugging flag. */
edefbb7c
AC
10185 add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
10186 _("Set ARM debugging."),
10187 _("Show ARM debugging."),
10188 _("When on, arm-specific debugging is enabled."),
2c5b56ce 10189 NULL,
7915a72c 10190 NULL, /* FIXME: i18n: "ARM debugging is %s. */
26304000 10191 &setdebuglist, &showdebuglist);
c906108c 10192}
This page took 1.518684 seconds and 4 git commands to generate.