*** empty log message ***
[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,
4c38e0a4 4 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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
34e8f22d
RE
22#include <ctype.h> /* XXX for isupper () */
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"
afd7eef0 30#include "dis-asm.h" /* For register styles. */
4e052eda 31#include "regcache.h"
d16aafd8 32#include "doublest.h"
fd0407d6 33#include "value.h"
34e8f22d 34#include "arch-utils.h"
4be87837 35#include "osabi.h"
eb5492fa
DJ
36#include "frame-unwind.h"
37#include "frame-base.h"
38#include "trad-frame.h"
842e1f1e
DJ
39#include "objfiles.h"
40#include "dwarf2-frame.h"
e4c16157 41#include "gdbtypes.h"
29d73ae4 42#include "prologue-value.h"
123dc839
DJ
43#include "target-descriptions.h"
44#include "user-regs.h"
34e8f22d
RE
45
46#include "arm-tdep.h"
26216b98 47#include "gdb/sim-arm.h"
34e8f22d 48
082fc60d
RE
49#include "elf-bfd.h"
50#include "coff/internal.h"
97e03143 51#include "elf/arm.h"
c906108c 52
26216b98 53#include "gdb_assert.h"
60c5725c 54#include "vec.h"
26216b98 55
9779414d
DJ
56#include "features/arm-with-m.c"
57
6529d2dd
AC
58static int arm_debug;
59
082fc60d
RE
60/* Macros for setting and testing a bit in a minimal symbol that marks
61 it as Thumb function. The MSB of the minimal symbol's "info" field
f594e5e9 62 is used for this purpose.
082fc60d
RE
63
64 MSYMBOL_SET_SPECIAL Actually sets the "special" bit.
f594e5e9 65 MSYMBOL_IS_SPECIAL Tests the "special" bit in a minimal symbol. */
082fc60d
RE
66
67#define MSYMBOL_SET_SPECIAL(msym) \
b887350f 68 MSYMBOL_TARGET_FLAG_1 (msym) = 1
082fc60d
RE
69
70#define MSYMBOL_IS_SPECIAL(msym) \
b887350f 71 MSYMBOL_TARGET_FLAG_1 (msym)
082fc60d 72
60c5725c
DJ
73/* Per-objfile data used for mapping symbols. */
74static const struct objfile_data *arm_objfile_data_key;
75
76struct arm_mapping_symbol
77{
78 bfd_vma value;
79 char type;
80};
81typedef struct arm_mapping_symbol arm_mapping_symbol_s;
82DEF_VEC_O(arm_mapping_symbol_s);
83
84struct arm_per_objfile
85{
86 VEC(arm_mapping_symbol_s) **section_maps;
87};
88
afd7eef0
RE
89/* The list of available "set arm ..." and "show arm ..." commands. */
90static struct cmd_list_element *setarmcmdlist = NULL;
91static struct cmd_list_element *showarmcmdlist = NULL;
92
fd50bc42
RE
93/* The type of floating-point to use. Keep this in sync with enum
94 arm_float_model, and the help string in _initialize_arm_tdep. */
95static const char *fp_model_strings[] =
96{
97 "auto",
98 "softfpa",
99 "fpa",
100 "softvfp",
28e97307
DJ
101 "vfp",
102 NULL
fd50bc42
RE
103};
104
105/* A variable that can be configured by the user. */
106static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
107static const char *current_fp_model = "auto";
108
28e97307
DJ
109/* The ABI to use. Keep this in sync with arm_abi_kind. */
110static const char *arm_abi_strings[] =
111{
112 "auto",
113 "APCS",
114 "AAPCS",
115 NULL
116};
117
118/* A variable that can be configured by the user. */
119static enum arm_abi_kind arm_abi_global = ARM_ABI_AUTO;
120static const char *arm_abi_string = "auto";
121
0428b8f5
DJ
122/* The execution mode to assume. */
123static const char *arm_mode_strings[] =
124 {
125 "auto",
126 "arm",
127 "thumb"
128 };
129
130static const char *arm_fallback_mode_string = "auto";
131static const char *arm_force_mode_string = "auto";
132
94c30b78 133/* Number of different reg name sets (options). */
afd7eef0 134static int num_disassembly_options;
bc90b915 135
123dc839
DJ
136/* The standard register names, and all the valid aliases for them. */
137static const struct
138{
139 const char *name;
140 int regnum;
141} arm_register_aliases[] = {
142 /* Basic register numbers. */
143 { "r0", 0 },
144 { "r1", 1 },
145 { "r2", 2 },
146 { "r3", 3 },
147 { "r4", 4 },
148 { "r5", 5 },
149 { "r6", 6 },
150 { "r7", 7 },
151 { "r8", 8 },
152 { "r9", 9 },
153 { "r10", 10 },
154 { "r11", 11 },
155 { "r12", 12 },
156 { "r13", 13 },
157 { "r14", 14 },
158 { "r15", 15 },
159 /* Synonyms (argument and variable registers). */
160 { "a1", 0 },
161 { "a2", 1 },
162 { "a3", 2 },
163 { "a4", 3 },
164 { "v1", 4 },
165 { "v2", 5 },
166 { "v3", 6 },
167 { "v4", 7 },
168 { "v5", 8 },
169 { "v6", 9 },
170 { "v7", 10 },
171 { "v8", 11 },
172 /* Other platform-specific names for r9. */
173 { "sb", 9 },
174 { "tr", 9 },
175 /* Special names. */
176 { "ip", 12 },
177 { "sp", 13 },
178 { "lr", 14 },
179 { "pc", 15 },
180 /* Names used by GCC (not listed in the ARM EABI). */
181 { "sl", 10 },
182 { "fp", 11 },
183 /* A special name from the older ATPCS. */
184 { "wr", 7 },
185};
bc90b915 186
123dc839 187static const char *const arm_register_names[] =
da59e081
JM
188{"r0", "r1", "r2", "r3", /* 0 1 2 3 */
189 "r4", "r5", "r6", "r7", /* 4 5 6 7 */
190 "r8", "r9", "r10", "r11", /* 8 9 10 11 */
191 "r12", "sp", "lr", "pc", /* 12 13 14 15 */
192 "f0", "f1", "f2", "f3", /* 16 17 18 19 */
193 "f4", "f5", "f6", "f7", /* 20 21 22 23 */
94c30b78 194 "fps", "cpsr" }; /* 24 25 */
ed9a39eb 195
afd7eef0
RE
196/* Valid register name styles. */
197static const char **valid_disassembly_styles;
ed9a39eb 198
afd7eef0
RE
199/* Disassembly style to use. Default to "std" register names. */
200static const char *disassembly_style;
96baa820 201
ed9a39eb 202/* This is used to keep the bfd arch_info in sync with the disassembly
afd7eef0
RE
203 style. */
204static void set_disassembly_style_sfunc(char *, int,
ed9a39eb 205 struct cmd_list_element *);
afd7eef0 206static void set_disassembly_style (void);
ed9a39eb 207
b508a996 208static void convert_from_extended (const struct floatformat *, const void *,
be8626e0 209 void *, int);
b508a996 210static void convert_to_extended (const struct floatformat *, void *,
be8626e0 211 const void *, int);
ed9a39eb 212
58d6951d
DJ
213static void arm_neon_quad_read (struct gdbarch *gdbarch,
214 struct regcache *regcache,
215 int regnum, gdb_byte *buf);
216static void arm_neon_quad_write (struct gdbarch *gdbarch,
217 struct regcache *regcache,
218 int regnum, const gdb_byte *buf);
219
9b8d791a 220struct arm_prologue_cache
c3b4394c 221{
eb5492fa
DJ
222 /* The stack pointer at the time this frame was created; i.e. the
223 caller's stack pointer when this function was called. It is used
224 to identify this frame. */
225 CORE_ADDR prev_sp;
226
4be43953
DJ
227 /* The frame base for this frame is just prev_sp - frame size.
228 FRAMESIZE is the distance from the frame pointer to the
229 initial stack pointer. */
eb5492fa 230
c3b4394c 231 int framesize;
eb5492fa
DJ
232
233 /* The register used to hold the frame pointer for this frame. */
c3b4394c 234 int framereg;
eb5492fa
DJ
235
236 /* Saved register offsets. */
237 struct trad_frame_saved_reg *saved_regs;
c3b4394c 238};
ed9a39eb 239
0d39a070
DJ
240static CORE_ADDR arm_analyze_prologue (struct gdbarch *gdbarch,
241 CORE_ADDR prologue_start,
242 CORE_ADDR prologue_end,
243 struct arm_prologue_cache *cache);
244
cca44b1b
JB
245/* Architecture version for displaced stepping. This effects the behaviour of
246 certain instructions, and really should not be hard-wired. */
247
248#define DISPLACED_STEPPING_ARCH_VERSION 5
249
bc90b915
FN
250/* Addresses for calling Thumb functions have the bit 0 set.
251 Here are some macros to test, set, or clear bit 0 of addresses. */
252#define IS_THUMB_ADDR(addr) ((addr) & 1)
253#define MAKE_THUMB_ADDR(addr) ((addr) | 1)
254#define UNMAKE_THUMB_ADDR(addr) ((addr) & ~1)
255
94c30b78 256/* Set to true if the 32-bit mode is in use. */
c906108c
SS
257
258int arm_apcs_32 = 1;
259
9779414d
DJ
260/* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode. */
261
262static int
263arm_psr_thumb_bit (struct gdbarch *gdbarch)
264{
265 if (gdbarch_tdep (gdbarch)->is_m)
266 return XPSR_T;
267 else
268 return CPSR_T;
269}
270
b39cc962
DJ
271/* Determine if FRAME is executing in Thumb mode. */
272
25b41d01 273int
b39cc962
DJ
274arm_frame_is_thumb (struct frame_info *frame)
275{
276 CORE_ADDR cpsr;
9779414d 277 ULONGEST t_bit = arm_psr_thumb_bit (get_frame_arch (frame));
b39cc962
DJ
278
279 /* Every ARM frame unwinder can unwind the T bit of the CPSR, either
280 directly (from a signal frame or dummy frame) or by interpreting
281 the saved LR (from a prologue or DWARF frame). So consult it and
282 trust the unwinders. */
283 cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
284
9779414d 285 return (cpsr & t_bit) != 0;
b39cc962
DJ
286}
287
60c5725c
DJ
288/* Callback for VEC_lower_bound. */
289
290static inline int
291arm_compare_mapping_symbols (const struct arm_mapping_symbol *lhs,
292 const struct arm_mapping_symbol *rhs)
293{
294 return lhs->value < rhs->value;
295}
296
f9d67f43
DJ
297/* Search for the mapping symbol covering MEMADDR. If one is found,
298 return its type. Otherwise, return 0. If START is non-NULL,
299 set *START to the location of the mapping symbol. */
c906108c 300
f9d67f43
DJ
301static char
302arm_find_mapping_symbol (CORE_ADDR memaddr, CORE_ADDR *start)
c906108c 303{
60c5725c 304 struct obj_section *sec;
0428b8f5 305
60c5725c
DJ
306 /* If there are mapping symbols, consult them. */
307 sec = find_pc_section (memaddr);
308 if (sec != NULL)
309 {
310 struct arm_per_objfile *data;
311 VEC(arm_mapping_symbol_s) *map;
aded6f54
PA
312 struct arm_mapping_symbol map_key = { memaddr - obj_section_addr (sec),
313 0 };
60c5725c
DJ
314 unsigned int idx;
315
316 data = objfile_data (sec->objfile, arm_objfile_data_key);
317 if (data != NULL)
318 {
319 map = data->section_maps[sec->the_bfd_section->index];
320 if (!VEC_empty (arm_mapping_symbol_s, map))
321 {
322 struct arm_mapping_symbol *map_sym;
323
324 idx = VEC_lower_bound (arm_mapping_symbol_s, map, &map_key,
325 arm_compare_mapping_symbols);
326
327 /* VEC_lower_bound finds the earliest ordered insertion
328 point. If the following symbol starts at this exact
329 address, we use that; otherwise, the preceding
330 mapping symbol covers this address. */
331 if (idx < VEC_length (arm_mapping_symbol_s, map))
332 {
333 map_sym = VEC_index (arm_mapping_symbol_s, map, idx);
334 if (map_sym->value == map_key.value)
f9d67f43
DJ
335 {
336 if (start)
337 *start = map_sym->value + obj_section_addr (sec);
338 return map_sym->type;
339 }
60c5725c
DJ
340 }
341
342 if (idx > 0)
343 {
344 map_sym = VEC_index (arm_mapping_symbol_s, map, idx - 1);
f9d67f43
DJ
345 if (start)
346 *start = map_sym->value + obj_section_addr (sec);
347 return map_sym->type;
60c5725c
DJ
348 }
349 }
350 }
351 }
352
f9d67f43
DJ
353 return 0;
354}
355
50e98be4
DJ
356static CORE_ADDR arm_get_next_pc_raw (struct frame_info *frame,
357 CORE_ADDR pc, int insert_bkpt);
358
f9d67f43
DJ
359/* Determine if the program counter specified in MEMADDR is in a Thumb
360 function. This function should be called for addresses unrelated to
361 any executing frame; otherwise, prefer arm_frame_is_thumb. */
362
363static int
9779414d 364arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
f9d67f43
DJ
365{
366 struct obj_section *sec;
367 struct minimal_symbol *sym;
368 char type;
369
370 /* If bit 0 of the address is set, assume this is a Thumb address. */
371 if (IS_THUMB_ADDR (memaddr))
372 return 1;
373
374 /* If the user wants to override the symbol table, let him. */
375 if (strcmp (arm_force_mode_string, "arm") == 0)
376 return 0;
377 if (strcmp (arm_force_mode_string, "thumb") == 0)
378 return 1;
379
9779414d
DJ
380 /* ARM v6-M and v7-M are always in Thumb mode. */
381 if (gdbarch_tdep (gdbarch)->is_m)
382 return 1;
383
f9d67f43
DJ
384 /* If there are mapping symbols, consult them. */
385 type = arm_find_mapping_symbol (memaddr, NULL);
386 if (type)
387 return type == 't';
388
ed9a39eb 389 /* Thumb functions have a "special" bit set in minimal symbols. */
c906108c
SS
390 sym = lookup_minimal_symbol_by_pc (memaddr);
391 if (sym)
0428b8f5
DJ
392 return (MSYMBOL_IS_SPECIAL (sym));
393
394 /* If the user wants to override the fallback mode, let them. */
395 if (strcmp (arm_fallback_mode_string, "arm") == 0)
396 return 0;
397 if (strcmp (arm_fallback_mode_string, "thumb") == 0)
398 return 1;
399
400 /* If we couldn't find any symbol, but we're talking to a running
401 target, then trust the current value of $cpsr. This lets
402 "display/i $pc" always show the correct mode (though if there is
403 a symbol table we will not reach here, so it still may not be
50e98be4
DJ
404 displayed in the mode it will be executed).
405
406 As a further heuristic if we detect that we are doing a single-step we
407 see what state executing the current instruction ends up with us being
408 in. */
0428b8f5 409 if (target_has_registers)
50e98be4
DJ
410 {
411 struct frame_info *current_frame = get_current_frame ();
412 CORE_ADDR current_pc = get_frame_pc (current_frame);
413 int is_thumb = arm_frame_is_thumb (current_frame);
414 CORE_ADDR next_pc;
415 if (memaddr == current_pc)
416 return is_thumb;
417 else
418 {
419 struct gdbarch *gdbarch = get_frame_arch (current_frame);
420 next_pc = arm_get_next_pc_raw (current_frame, current_pc, FALSE);
421 if (memaddr == gdbarch_addr_bits_remove (gdbarch, next_pc))
422 return IS_THUMB_ADDR (next_pc);
423 else
424 return is_thumb;
425 }
426 }
0428b8f5
DJ
427
428 /* Otherwise we're out of luck; we assume ARM. */
429 return 0;
c906108c
SS
430}
431
181c1381 432/* Remove useless bits from addresses in a running program. */
34e8f22d 433static CORE_ADDR
24568a2c 434arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
c906108c 435{
a3a2ee65 436 if (arm_apcs_32)
dd6be234 437 return UNMAKE_THUMB_ADDR (val);
c906108c 438 else
a3a2ee65 439 return (val & 0x03fffffc);
c906108c
SS
440}
441
181c1381
RE
442/* When reading symbols, we need to zap the low bit of the address,
443 which may be set to 1 for Thumb functions. */
34e8f22d 444static CORE_ADDR
24568a2c 445arm_smash_text_address (struct gdbarch *gdbarch, CORE_ADDR val)
181c1381
RE
446{
447 return val & ~1;
448}
449
0d39a070
DJ
450/* Return 1 if PC is the start of a compiler helper function which
451 can be safely ignored during prologue skipping. */
452static int
453skip_prologue_function (CORE_ADDR pc)
454{
455 struct minimal_symbol *msym;
456 const char *name;
457
458 msym = lookup_minimal_symbol_by_pc (pc);
459 if (msym == NULL || SYMBOL_VALUE_ADDRESS (msym) != pc)
460 return 0;
461
462 name = SYMBOL_LINKAGE_NAME (msym);
463 if (name == NULL)
464 return 0;
465
466 /* The GNU linker's Thumb call stub to foo is named
467 __foo_from_thumb. */
468 if (strstr (name, "_from_thumb") != NULL)
469 name += 2;
470
471 /* On soft-float targets, __truncdfsf2 is called to convert promoted
472 arguments to their argument types in non-prototyped
473 functions. */
474 if (strncmp (name, "__truncdfsf2", strlen ("__truncdfsf2")) == 0)
475 return 1;
476 if (strncmp (name, "__aeabi_d2f", strlen ("__aeabi_d2f")) == 0)
477 return 1;
478
479 return 0;
480}
481
482/* Support routines for instruction parsing. */
483#define submask(x) ((1L << ((x) + 1)) - 1)
484#define bit(obj,st) (((obj) >> (st)) & 1)
485#define bits(obj,st,fn) (((obj) >> (st)) & submask ((fn) - (st)))
486#define sbits(obj,st,fn) \
487 ((long) (bits(obj,st,fn) | ((long) bit(obj,fn) * ~ submask (fn - st))))
488#define BranchDest(addr,instr) \
489 ((CORE_ADDR) (((long) (addr)) + 8 + (sbits (instr, 0, 23) << 2)))
490
29d73ae4
DJ
491/* Analyze a Thumb prologue, looking for a recognizable stack frame
492 and frame pointer. Scan until we encounter a store that could
0d39a070
DJ
493 clobber the stack frame unexpectedly, or an unknown instruction.
494 Return the last address which is definitely safe to skip for an
495 initial breakpoint. */
c906108c
SS
496
497static CORE_ADDR
29d73ae4
DJ
498thumb_analyze_prologue (struct gdbarch *gdbarch,
499 CORE_ADDR start, CORE_ADDR limit,
500 struct arm_prologue_cache *cache)
c906108c 501{
0d39a070 502 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
e17a4113 503 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
29d73ae4
DJ
504 int i;
505 pv_t regs[16];
506 struct pv_area *stack;
507 struct cleanup *back_to;
508 CORE_ADDR offset;
da3c6d4a 509
29d73ae4
DJ
510 for (i = 0; i < 16; i++)
511 regs[i] = pv_register (i, 0);
55f960e1 512 stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
29d73ae4
DJ
513 back_to = make_cleanup_free_pv_area (stack);
514
29d73ae4 515 while (start < limit)
c906108c 516 {
29d73ae4
DJ
517 unsigned short insn;
518
e17a4113 519 insn = read_memory_unsigned_integer (start, 2, byte_order_for_code);
9d4fde75 520
94c30b78 521 if ((insn & 0xfe00) == 0xb400) /* push { rlist } */
da59e081 522 {
29d73ae4
DJ
523 int regno;
524 int mask;
4be43953
DJ
525
526 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
527 break;
29d73ae4
DJ
528
529 /* Bits 0-7 contain a mask for registers R0-R7. Bit 8 says
530 whether to save LR (R14). */
531 mask = (insn & 0xff) | ((insn & 0x100) << 6);
532
533 /* Calculate offsets of saved R0-R7 and LR. */
534 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
535 if (mask & (1 << regno))
536 {
29d73ae4
DJ
537 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
538 -4);
539 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
540 }
da59e081 541 }
da3c6d4a
MS
542 else if ((insn & 0xff00) == 0xb000) /* add sp, #simm OR
543 sub sp, #simm */
da59e081 544 {
29d73ae4
DJ
545 offset = (insn & 0x7f) << 2; /* get scaled offset */
546 if (insn & 0x80) /* Check for SUB. */
547 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
548 -offset);
da59e081 549 else
29d73ae4
DJ
550 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
551 offset);
da59e081 552 }
0d39a070
DJ
553 else if ((insn & 0xf800) == 0xa800) /* add Rd, sp, #imm */
554 regs[bits (insn, 8, 10)] = pv_add_constant (regs[ARM_SP_REGNUM],
555 (insn & 0xff) << 2);
556 else if ((insn & 0xfe00) == 0x1c00 /* add Rd, Rn, #imm */
557 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
558 regs[bits (insn, 0, 2)] = pv_add_constant (regs[bits (insn, 3, 5)],
559 bits (insn, 6, 8));
560 else if ((insn & 0xf800) == 0x3000 /* add Rd, #imm */
561 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
562 regs[bits (insn, 8, 10)] = pv_add_constant (regs[bits (insn, 8, 10)],
563 bits (insn, 0, 7));
564 else if ((insn & 0xfe00) == 0x1800 /* add Rd, Rn, Rm */
565 && pv_is_register (regs[bits (insn, 6, 8)], ARM_SP_REGNUM)
566 && pv_is_constant (regs[bits (insn, 3, 5)]))
567 regs[bits (insn, 0, 2)] = pv_add (regs[bits (insn, 3, 5)],
568 regs[bits (insn, 6, 8)]);
569 else if ((insn & 0xff00) == 0x4400 /* add Rd, Rm */
570 && pv_is_constant (regs[bits (insn, 3, 6)]))
571 {
572 int rd = (bit (insn, 7) << 3) + bits (insn, 0, 2);
573 int rm = bits (insn, 3, 6);
574 regs[rd] = pv_add (regs[rd], regs[rm]);
575 }
29d73ae4 576 else if ((insn & 0xff00) == 0x4600) /* mov hi, lo or mov lo, hi */
da59e081 577 {
29d73ae4
DJ
578 int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
579 int src_reg = (insn & 0x78) >> 3;
580 regs[dst_reg] = regs[src_reg];
da59e081 581 }
29d73ae4 582 else if ((insn & 0xf800) == 0x9000) /* str rd, [sp, #off] */
da59e081 583 {
29d73ae4
DJ
584 /* Handle stores to the stack. Normally pushes are used,
585 but with GCC -mtpcs-frame, there may be other stores
586 in the prologue to create the frame. */
587 int regno = (insn >> 8) & 0x7;
588 pv_t addr;
589
590 offset = (insn & 0xff) << 2;
591 addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
592
593 if (pv_area_store_would_trash (stack, addr))
594 break;
595
596 pv_area_store (stack, addr, 4, regs[regno]);
da59e081 597 }
0d39a070
DJ
598 else if ((insn & 0xf800) == 0x6000) /* str rd, [rn, #off] */
599 {
600 int rd = bits (insn, 0, 2);
601 int rn = bits (insn, 3, 5);
602 pv_t addr;
603
604 offset = bits (insn, 6, 10) << 2;
605 addr = pv_add_constant (regs[rn], offset);
606
607 if (pv_area_store_would_trash (stack, addr))
608 break;
609
610 pv_area_store (stack, addr, 4, regs[rd]);
611 }
612 else if (((insn & 0xf800) == 0x7000 /* strb Rd, [Rn, #off] */
613 || (insn & 0xf800) == 0x8000) /* strh Rd, [Rn, #off] */
614 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
615 /* Ignore stores of argument registers to the stack. */
616 ;
617 else if ((insn & 0xf800) == 0xc800 /* ldmia Rn!, { registers } */
618 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
619 /* Ignore block loads from the stack, potentially copying
620 parameters from memory. */
621 ;
622 else if ((insn & 0xf800) == 0x9800 /* ldr Rd, [Rn, #immed] */
623 || ((insn & 0xf800) == 0x6800 /* ldr Rd, [sp, #immed] */
624 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM)))
625 /* Similarly ignore single loads from the stack. */
626 ;
627 else if ((insn & 0xffc0) == 0x0000 /* lsls Rd, Rm, #0 */
628 || (insn & 0xffc0) == 0x1c00) /* add Rd, Rn, #0 */
629 /* Skip register copies, i.e. saves to another register
630 instead of the stack. */
631 ;
632 else if ((insn & 0xf800) == 0x2000) /* movs Rd, #imm */
633 /* Recognize constant loads; even with small stacks these are necessary
634 on Thumb. */
635 regs[bits (insn, 8, 10)] = pv_constant (bits (insn, 0, 7));
636 else if ((insn & 0xf800) == 0x4800) /* ldr Rd, [pc, #imm] */
637 {
638 /* Constant pool loads, for the same reason. */
639 unsigned int constant;
640 CORE_ADDR loc;
641
642 loc = start + 4 + bits (insn, 0, 7) * 4;
643 constant = read_memory_unsigned_integer (loc, 4, byte_order);
644 regs[bits (insn, 8, 10)] = pv_constant (constant);
645 }
646 else if ((insn & 0xe000) == 0xe000 && cache == NULL)
647 {
648 /* Only recognize 32-bit instructions for prologue skipping. */
649 unsigned short inst2;
650
651 inst2 = read_memory_unsigned_integer (start + 2, 2,
652 byte_order_for_code);
653
654 if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
655 {
656 /* BL, BLX. Allow some special function calls when
657 skipping the prologue; GCC generates these before
658 storing arguments to the stack. */
659 CORE_ADDR nextpc;
660 int j1, j2, imm1, imm2;
661
662 imm1 = sbits (insn, 0, 10);
663 imm2 = bits (inst2, 0, 10);
664 j1 = bit (inst2, 13);
665 j2 = bit (inst2, 11);
666
667 offset = ((imm1 << 12) + (imm2 << 1));
668 offset ^= ((!j2) << 22) | ((!j1) << 23);
669
670 nextpc = start + 4 + offset;
671 /* For BLX make sure to clear the low bits. */
672 if (bit (inst2, 12) == 0)
673 nextpc = nextpc & 0xfffffffc;
674
675 if (!skip_prologue_function (nextpc))
676 break;
677 }
678 else if ((insn & 0xfe50) == 0xe800 /* stm{db,ia} Rn[!], { registers } */
679 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
680 ;
681 else if ((insn & 0xfe50) == 0xe840 /* strd Rt, Rt2, [Rn, #imm] */
682 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
683 ;
684 else if ((insn & 0xffd0) == 0xe890 /* ldmia Rn[!], { registers } */
685 && (inst2 & 0x8000) == 0x0000
686 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
687 ;
688 else if ((insn & 0xfbf0) == 0xf100 /* add.w Rd, Rn, #imm */
689 && (inst2 & 0x8000) == 0x0000)
690 /* Since we only recognize this for prologue skipping, do not bother
691 to compute the constant. */
692 regs[bits (inst2, 8, 11)] = regs[bits (insn, 0, 3)];
693 else if ((insn & 0xfbf0) == 0xf1a0 /* sub.w Rd, Rn, #imm12 */
694 && (inst2 & 0x8000) == 0x0000)
695 /* Since we only recognize this for prologue skipping, do not bother
696 to compute the constant. */
697 regs[bits (inst2, 8, 11)] = regs[bits (insn, 0, 3)];
698 else if ((insn & 0xfbf0) == 0xf2a0 /* sub.w Rd, Rn, #imm8 */
699 && (inst2 & 0x8000) == 0x0000)
700 /* Since we only recognize this for prologue skipping, do not bother
701 to compute the constant. */
702 regs[bits (inst2, 8, 11)] = regs[bits (insn, 0, 3)];
703 else if ((insn & 0xff50) == 0xf850 /* ldr.w Rd, [Rn, #imm]{!} */
704 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
705 ;
706 else if ((insn & 0xff50) == 0xe950 /* ldrd Rt, Rt2, [Rn, #imm]{!} */
707 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
708 ;
709 else if ((insn & 0xff50) == 0xf800 /* strb.w or strh.w */
710 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
711 ;
712 else
713 {
714 /* We don't know what this instruction is. We're finished
715 scanning. NOTE: Recognizing more safe-to-ignore
716 instructions here will improve support for optimized
717 code. */
718 break;
719 }
720
721 start += 2;
722 }
29d73ae4 723 else
3d74b771 724 {
29d73ae4
DJ
725 /* We don't know what this instruction is. We're finished
726 scanning. NOTE: Recognizing more safe-to-ignore
727 instructions here will improve support for optimized
728 code. */
da3c6d4a 729 break;
3d74b771 730 }
29d73ae4
DJ
731
732 start += 2;
c906108c
SS
733 }
734
0d39a070
DJ
735 if (arm_debug)
736 fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
737 paddress (gdbarch, start));
738
29d73ae4
DJ
739 if (cache == NULL)
740 {
741 do_cleanups (back_to);
742 return start;
743 }
744
29d73ae4
DJ
745 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
746 {
747 /* Frame pointer is fp. Frame size is constant. */
748 cache->framereg = ARM_FP_REGNUM;
749 cache->framesize = -regs[ARM_FP_REGNUM].k;
750 }
751 else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
752 {
753 /* Frame pointer is r7. Frame size is constant. */
754 cache->framereg = THUMB_FP_REGNUM;
755 cache->framesize = -regs[THUMB_FP_REGNUM].k;
756 }
757 else if (pv_is_register (regs[ARM_SP_REGNUM], ARM_SP_REGNUM))
758 {
759 /* Try the stack pointer... this is a bit desperate. */
760 cache->framereg = ARM_SP_REGNUM;
761 cache->framesize = -regs[ARM_SP_REGNUM].k;
762 }
763 else
764 {
765 /* We're just out of luck. We don't know where the frame is. */
766 cache->framereg = -1;
767 cache->framesize = 0;
768 }
769
770 for (i = 0; i < 16; i++)
771 if (pv_area_find_reg (stack, gdbarch, i, &offset))
772 cache->saved_regs[i].addr = offset;
773
774 do_cleanups (back_to);
775 return start;
c906108c
SS
776}
777
da3c6d4a
MS
778/* Advance the PC across any function entry prologue instructions to
779 reach some "real" code.
34e8f22d
RE
780
781 The APCS (ARM Procedure Call Standard) defines the following
ed9a39eb 782 prologue:
c906108c 783
c5aa993b
JM
784 mov ip, sp
785 [stmfd sp!, {a1,a2,a3,a4}]
786 stmfd sp!, {...,fp,ip,lr,pc}
ed9a39eb
JM
787 [stfe f7, [sp, #-12]!]
788 [stfe f6, [sp, #-12]!]
789 [stfe f5, [sp, #-12]!]
790 [stfe f4, [sp, #-12]!]
791 sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn */
c906108c 792
34e8f22d 793static CORE_ADDR
6093d2eb 794arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
c906108c 795{
e17a4113 796 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
c906108c
SS
797 unsigned long inst;
798 CORE_ADDR skip_pc;
a89fea3c 799 CORE_ADDR func_addr, limit_pc;
c906108c
SS
800 struct symtab_and_line sal;
801
a89fea3c
JL
802 /* See if we can determine the end of the prologue via the symbol table.
803 If so, then return either PC, or the PC after the prologue, whichever
804 is greater. */
805 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
c906108c 806 {
d80b854b
UW
807 CORE_ADDR post_prologue_pc
808 = skip_prologue_using_sal (gdbarch, func_addr);
0d39a070
DJ
809 struct symtab *s = find_pc_symtab (func_addr);
810
811 /* GCC always emits a line note before the prologue and another
812 one after, even if the two are at the same address or on the
813 same line. Take advantage of this so that we do not need to
814 know every instruction that might appear in the prologue. We
815 will have producer information for most binaries; if it is
816 missing (e.g. for -gstabs), assuming the GNU tools. */
817 if (post_prologue_pc
818 && (s == NULL
819 || s->producer == NULL
820 || strncmp (s->producer, "GNU ", sizeof ("GNU ") - 1) == 0))
821 return post_prologue_pc;
822
a89fea3c 823 if (post_prologue_pc != 0)
0d39a070
DJ
824 {
825 CORE_ADDR analyzed_limit;
826
827 /* For non-GCC compilers, make sure the entire line is an
828 acceptable prologue; GDB will round this function's
829 return value up to the end of the following line so we
830 can not skip just part of a line (and we do not want to).
831
832 RealView does not treat the prologue specially, but does
833 associate prologue code with the opening brace; so this
834 lets us skip the first line if we think it is the opening
835 brace. */
9779414d 836 if (arm_pc_is_thumb (gdbarch, func_addr))
0d39a070
DJ
837 analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
838 post_prologue_pc, NULL);
839 else
840 analyzed_limit = arm_analyze_prologue (gdbarch, func_addr,
841 post_prologue_pc, NULL);
842
843 if (analyzed_limit != post_prologue_pc)
844 return func_addr;
845
846 return post_prologue_pc;
847 }
c906108c
SS
848 }
849
a89fea3c
JL
850 /* Can't determine prologue from the symbol table, need to examine
851 instructions. */
c906108c 852
a89fea3c
JL
853 /* Find an upper limit on the function prologue using the debug
854 information. If the debug information could not be used to provide
855 that bound, then use an arbitrary large number as the upper bound. */
b8d5e71d 856 /* Like arm_scan_prologue, stop no later than pc + 64. */
d80b854b 857 limit_pc = skip_prologue_using_sal (gdbarch, pc);
a89fea3c
JL
858 if (limit_pc == 0)
859 limit_pc = pc + 64; /* Magic. */
860
c906108c 861
29d73ae4 862 /* Check if this is Thumb code. */
9779414d 863 if (arm_pc_is_thumb (gdbarch, pc))
a89fea3c 864 return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
29d73ae4 865
a89fea3c 866 for (skip_pc = pc; skip_pc < limit_pc; skip_pc += 4)
f43845b3 867 {
e17a4113 868 inst = read_memory_unsigned_integer (skip_pc, 4, byte_order_for_code);
9d4fde75 869
b8d5e71d
MS
870 /* "mov ip, sp" is no longer a required part of the prologue. */
871 if (inst == 0xe1a0c00d) /* mov ip, sp */
872 continue;
c906108c 873
28cd8767
JG
874 if ((inst & 0xfffff000) == 0xe28dc000) /* add ip, sp #n */
875 continue;
876
877 if ((inst & 0xfffff000) == 0xe24dc000) /* sub ip, sp #n */
878 continue;
879
b8d5e71d
MS
880 /* Some prologues begin with "str lr, [sp, #-4]!". */
881 if (inst == 0xe52de004) /* str lr, [sp, #-4]! */
882 continue;
c906108c 883
b8d5e71d
MS
884 if ((inst & 0xfffffff0) == 0xe92d0000) /* stmfd sp!,{a1,a2,a3,a4} */
885 continue;
c906108c 886
b8d5e71d
MS
887 if ((inst & 0xfffff800) == 0xe92dd800) /* stmfd sp!,{fp,ip,lr,pc} */
888 continue;
11d3b27d 889
b8d5e71d
MS
890 /* Any insns after this point may float into the code, if it makes
891 for better instruction scheduling, so we skip them only if we
892 find them, but still consider the function to be frame-ful. */
f43845b3 893
b8d5e71d
MS
894 /* We may have either one sfmfd instruction here, or several stfe
895 insns, depending on the version of floating point code we
896 support. */
897 if ((inst & 0xffbf0fff) == 0xec2d0200) /* sfmfd fn, <cnt>, [sp]! */
898 continue;
899
900 if ((inst & 0xffff8fff) == 0xed6d0103) /* stfe fn, [sp, #-12]! */
901 continue;
902
903 if ((inst & 0xfffff000) == 0xe24cb000) /* sub fp, ip, #nn */
904 continue;
905
906 if ((inst & 0xfffff000) == 0xe24dd000) /* sub sp, sp, #nn */
907 continue;
908
f8bf5763
PM
909 if ((inst & 0xffffc000) == 0xe54b0000 /* strb r(0123),[r11,#-nn] */
910 || (inst & 0xffffc0f0) == 0xe14b00b0 /* strh r(0123),[r11,#-nn] */
911 || (inst & 0xffffc000) == 0xe50b0000) /* str r(0123),[r11,#-nn] */
b8d5e71d
MS
912 continue;
913
f8bf5763
PM
914 if ((inst & 0xffffc000) == 0xe5cd0000 /* strb r(0123),[sp,#nn] */
915 || (inst & 0xffffc0f0) == 0xe1cd00b0 /* strh r(0123),[sp,#nn] */
916 || (inst & 0xffffc000) == 0xe58d0000) /* str r(0123),[sp,#nn] */
b8d5e71d
MS
917 continue;
918
919 /* Un-recognized instruction; stop scanning. */
920 break;
f43845b3 921 }
c906108c 922
b8d5e71d 923 return skip_pc; /* End of prologue */
c906108c 924}
94c30b78 925
c5aa993b 926/* *INDENT-OFF* */
c906108c
SS
927/* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
928 This function decodes a Thumb function prologue to determine:
929 1) the size of the stack frame
930 2) which registers are saved on it
931 3) the offsets of saved regs
932 4) the offset from the stack pointer to the frame pointer
c906108c 933
da59e081
JM
934 A typical Thumb function prologue would create this stack frame
935 (offsets relative to FP)
c906108c
SS
936 old SP -> 24 stack parameters
937 20 LR
938 16 R7
939 R7 -> 0 local variables (16 bytes)
940 SP -> -12 additional stack space (12 bytes)
941 The frame size would thus be 36 bytes, and the frame offset would be
da59e081
JM
942 12 bytes. The frame register is R7.
943
da3c6d4a
MS
944 The comments for thumb_skip_prolog() describe the algorithm we use
945 to detect the end of the prolog. */
c5aa993b
JM
946/* *INDENT-ON* */
947
c906108c 948static void
be8626e0 949thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
b39cc962 950 CORE_ADDR block_addr, struct arm_prologue_cache *cache)
c906108c
SS
951{
952 CORE_ADDR prologue_start;
953 CORE_ADDR prologue_end;
954 CORE_ADDR current_pc;
c906108c 955
b39cc962
DJ
956 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
957 &prologue_end))
c906108c
SS
958 {
959 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
960
94c30b78 961 if (sal.line == 0) /* no line info, use current PC */
eb5492fa 962 prologue_end = prev_pc;
c906108c 963 else if (sal.end < prologue_end) /* next line begins after fn end */
94c30b78 964 prologue_end = sal.end; /* (probably means no prologue) */
c906108c
SS
965 }
966 else
f7060f85
DJ
967 /* We're in the boondocks: we have no idea where the start of the
968 function is. */
969 return;
c906108c 970
eb5492fa 971 prologue_end = min (prologue_end, prev_pc);
c906108c 972
be8626e0 973 thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
974}
975
0d39a070 976/* Return 1 if THIS_INSTR might change control flow, 0 otherwise. */
c906108c 977
0d39a070
DJ
978static int
979arm_instruction_changes_pc (uint32_t this_instr)
c906108c 980{
0d39a070
DJ
981 if (bits (this_instr, 28, 31) == INST_NV)
982 /* Unconditional instructions. */
983 switch (bits (this_instr, 24, 27))
984 {
985 case 0xa:
986 case 0xb:
987 /* Branch with Link and change to Thumb. */
988 return 1;
989 case 0xc:
990 case 0xd:
991 case 0xe:
992 /* Coprocessor register transfer. */
993 if (bits (this_instr, 12, 15) == 15)
994 error (_("Invalid update to pc in instruction"));
995 return 0;
996 default:
997 return 0;
998 }
999 else
1000 switch (bits (this_instr, 25, 27))
1001 {
1002 case 0x0:
1003 if (bits (this_instr, 23, 24) == 2 && bit (this_instr, 20) == 0)
1004 {
1005 /* Multiplies and extra load/stores. */
1006 if (bit (this_instr, 4) == 1 && bit (this_instr, 7) == 1)
1007 /* Neither multiplies nor extension load/stores are allowed
1008 to modify PC. */
1009 return 0;
1010
1011 /* Otherwise, miscellaneous instructions. */
1012
1013 /* BX <reg>, BXJ <reg>, BLX <reg> */
1014 if (bits (this_instr, 4, 27) == 0x12fff1
1015 || bits (this_instr, 4, 27) == 0x12fff2
1016 || bits (this_instr, 4, 27) == 0x12fff3)
1017 return 1;
1018
1019 /* Other miscellaneous instructions are unpredictable if they
1020 modify PC. */
1021 return 0;
1022 }
1023 /* Data processing instruction. Fall through. */
c906108c 1024
0d39a070
DJ
1025 case 0x1:
1026 if (bits (this_instr, 12, 15) == 15)
1027 return 1;
1028 else
1029 return 0;
c906108c 1030
0d39a070
DJ
1031 case 0x2:
1032 case 0x3:
1033 /* Media instructions and architecturally undefined instructions. */
1034 if (bits (this_instr, 25, 27) == 3 && bit (this_instr, 4) == 1)
1035 return 0;
c906108c 1036
0d39a070
DJ
1037 /* Stores. */
1038 if (bit (this_instr, 20) == 0)
1039 return 0;
2a451106 1040
0d39a070
DJ
1041 /* Loads. */
1042 if (bits (this_instr, 12, 15) == ARM_PC_REGNUM)
1043 return 1;
1044 else
1045 return 0;
2a451106 1046
0d39a070
DJ
1047 case 0x4:
1048 /* Load/store multiple. */
1049 if (bit (this_instr, 20) == 1 && bit (this_instr, 15) == 1)
1050 return 1;
1051 else
1052 return 0;
2a451106 1053
0d39a070
DJ
1054 case 0x5:
1055 /* Branch and branch with link. */
1056 return 1;
2a451106 1057
0d39a070
DJ
1058 case 0x6:
1059 case 0x7:
1060 /* Coprocessor transfers or SWIs can not affect PC. */
1061 return 0;
eb5492fa 1062
0d39a070
DJ
1063 default:
1064 internal_error (__FILE__, __LINE__, "bad value in switch");
1065 }
1066}
c906108c 1067
0d39a070
DJ
1068/* Analyze an ARM mode prologue starting at PROLOGUE_START and
1069 continuing no further than PROLOGUE_END. If CACHE is non-NULL,
1070 fill it in. Return the first address not recognized as a prologue
1071 instruction.
eb5492fa 1072
0d39a070
DJ
1073 We recognize all the instructions typically found in ARM prologues,
1074 plus harmless instructions which can be skipped (either for analysis
1075 purposes, or a more restrictive set that can be skipped when finding
1076 the end of the prologue). */
1077
1078static CORE_ADDR
1079arm_analyze_prologue (struct gdbarch *gdbarch,
1080 CORE_ADDR prologue_start, CORE_ADDR prologue_end,
1081 struct arm_prologue_cache *cache)
1082{
1083 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1084 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1085 int regno;
1086 CORE_ADDR offset, current_pc;
1087 pv_t regs[ARM_FPS_REGNUM];
1088 struct pv_area *stack;
1089 struct cleanup *back_to;
1090 int framereg, framesize;
1091 CORE_ADDR unrecognized_pc = 0;
1092
1093 /* Search the prologue looking for instructions that set up the
96baa820 1094 frame pointer, adjust the stack pointer, and save registers.
ed9a39eb 1095
96baa820
JM
1096 Be careful, however, and if it doesn't look like a prologue,
1097 don't try to scan it. If, for instance, a frameless function
1098 begins with stmfd sp!, then we will tell ourselves there is
b8d5e71d 1099 a frame, which will confuse stack traceback, as well as "finish"
96baa820 1100 and other operations that rely on a knowledge of the stack
0d39a070 1101 traceback. */
d4473757 1102
4be43953
DJ
1103 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1104 regs[regno] = pv_register (regno, 0);
55f960e1 1105 stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
4be43953
DJ
1106 back_to = make_cleanup_free_pv_area (stack);
1107
94c30b78
MS
1108 for (current_pc = prologue_start;
1109 current_pc < prologue_end;
f43845b3 1110 current_pc += 4)
96baa820 1111 {
e17a4113
UW
1112 unsigned int insn
1113 = read_memory_unsigned_integer (current_pc, 4, byte_order_for_code);
9d4fde75 1114
94c30b78 1115 if (insn == 0xe1a0c00d) /* mov ip, sp */
f43845b3 1116 {
4be43953 1117 regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
28cd8767
JG
1118 continue;
1119 }
0d39a070
DJ
1120 else if ((insn & 0xfff00000) == 0xe2800000 /* add Rd, Rn, #n */
1121 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767
JG
1122 {
1123 unsigned imm = insn & 0xff; /* immediate value */
1124 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
0d39a070 1125 int rd = bits (insn, 12, 15);
28cd8767 1126 imm = (imm >> rot) | (imm << (32 - rot));
0d39a070 1127 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
28cd8767
JG
1128 continue;
1129 }
0d39a070
DJ
1130 else if ((insn & 0xfff00000) == 0xe2400000 /* sub Rd, Rn, #n */
1131 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767
JG
1132 {
1133 unsigned imm = insn & 0xff; /* immediate value */
1134 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
0d39a070 1135 int rd = bits (insn, 12, 15);
28cd8767 1136 imm = (imm >> rot) | (imm << (32 - rot));
0d39a070 1137 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
f43845b3
MS
1138 continue;
1139 }
0d39a070 1140 else if ((insn & 0xffff0fff) == 0xe52d0004) /* str Rd, [sp, #-4]! */
f43845b3 1141 {
4be43953
DJ
1142 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1143 break;
1144 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
0d39a070
DJ
1145 pv_area_store (stack, regs[ARM_SP_REGNUM], 4,
1146 regs[bits (insn, 12, 15)]);
f43845b3
MS
1147 continue;
1148 }
1149 else if ((insn & 0xffff0000) == 0xe92d0000)
d4473757
KB
1150 /* stmfd sp!, {..., fp, ip, lr, pc}
1151 or
1152 stmfd sp!, {a1, a2, a3, a4} */
c906108c 1153 {
d4473757 1154 int mask = insn & 0xffff;
ed9a39eb 1155
4be43953
DJ
1156 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1157 break;
1158
94c30b78 1159 /* Calculate offsets of saved registers. */
34e8f22d 1160 for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
d4473757
KB
1161 if (mask & (1 << regno))
1162 {
4be43953
DJ
1163 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
1164 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
d4473757
KB
1165 }
1166 }
0d39a070
DJ
1167 else if ((insn & 0xffff0000) == 0xe54b0000 /* strb rx,[r11,#-n] */
1168 || (insn & 0xffff00f0) == 0xe14b00b0 /* strh rx,[r11,#-n] */
f8bf5763 1169 || (insn & 0xffffc000) == 0xe50b0000) /* str rx,[r11,#-n] */
b8d5e71d
MS
1170 {
1171 /* No need to add this to saved_regs -- it's just an arg reg. */
1172 continue;
1173 }
0d39a070
DJ
1174 else if ((insn & 0xffff0000) == 0xe5cd0000 /* strb rx,[sp,#n] */
1175 || (insn & 0xffff00f0) == 0xe1cd00b0 /* strh rx,[sp,#n] */
f8bf5763 1176 || (insn & 0xffffc000) == 0xe58d0000) /* str rx,[sp,#n] */
f43845b3
MS
1177 {
1178 /* No need to add this to saved_regs -- it's just an arg reg. */
1179 continue;
1180 }
0d39a070
DJ
1181 else if ((insn & 0xfff00000) == 0xe8800000 /* stm Rn, { registers } */
1182 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1183 {
1184 /* No need to add this to saved_regs -- it's just arg regs. */
1185 continue;
1186 }
d4473757
KB
1187 else if ((insn & 0xfffff000) == 0xe24cb000) /* sub fp, ip #n */
1188 {
94c30b78
MS
1189 unsigned imm = insn & 0xff; /* immediate value */
1190 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
d4473757 1191 imm = (imm >> rot) | (imm << (32 - rot));
4be43953 1192 regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
d4473757
KB
1193 }
1194 else if ((insn & 0xfffff000) == 0xe24dd000) /* sub sp, sp #n */
1195 {
94c30b78
MS
1196 unsigned imm = insn & 0xff; /* immediate value */
1197 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
d4473757 1198 imm = (imm >> rot) | (imm << (32 - rot));
4be43953 1199 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
d4473757 1200 }
ff6f572f 1201 else if ((insn & 0xffff7fff) == 0xed6d0103 /* stfe f?, [sp, -#c]! */
2af46ca0 1202 && gdbarch_tdep (gdbarch)->have_fpa_registers)
d4473757 1203 {
4be43953
DJ
1204 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1205 break;
1206
1207 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
34e8f22d 1208 regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
4be43953 1209 pv_area_store (stack, regs[ARM_SP_REGNUM], 12, regs[regno]);
d4473757 1210 }
ff6f572f 1211 else if ((insn & 0xffbf0fff) == 0xec2d0200 /* sfmfd f0, 4, [sp!] */
2af46ca0 1212 && gdbarch_tdep (gdbarch)->have_fpa_registers)
d4473757
KB
1213 {
1214 int n_saved_fp_regs;
1215 unsigned int fp_start_reg, fp_bound_reg;
1216
4be43953
DJ
1217 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1218 break;
1219
94c30b78 1220 if ((insn & 0x800) == 0x800) /* N0 is set */
96baa820 1221 {
d4473757
KB
1222 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1223 n_saved_fp_regs = 3;
1224 else
1225 n_saved_fp_regs = 1;
96baa820 1226 }
d4473757 1227 else
96baa820 1228 {
d4473757
KB
1229 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1230 n_saved_fp_regs = 2;
1231 else
1232 n_saved_fp_regs = 4;
96baa820 1233 }
d4473757 1234
34e8f22d 1235 fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
d4473757
KB
1236 fp_bound_reg = fp_start_reg + n_saved_fp_regs;
1237 for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
96baa820 1238 {
4be43953
DJ
1239 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
1240 pv_area_store (stack, regs[ARM_SP_REGNUM], 12,
1241 regs[fp_start_reg++]);
96baa820 1242 }
c906108c 1243 }
0d39a070
DJ
1244 else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
1245 {
1246 /* Allow some special function calls when skipping the
1247 prologue; GCC generates these before storing arguments to
1248 the stack. */
1249 CORE_ADDR dest = BranchDest (current_pc, insn);
1250
1251 if (skip_prologue_function (dest))
1252 continue;
1253 else
1254 break;
1255 }
d4473757 1256 else if ((insn & 0xf0000000) != 0xe0000000)
94c30b78 1257 break; /* Condition not true, exit early */
0d39a070
DJ
1258 else if (arm_instruction_changes_pc (insn))
1259 /* Don't scan past anything that might change control flow. */
1260 break;
1261 else if ((insn & 0xfe500000) == 0xe8100000) /* ldm */
1262 {
1263 /* Ignore block loads from the stack, potentially copying
1264 parameters from memory. */
1265 if (pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1266 continue;
1267 else
1268 break;
1269 }
1270 else if ((insn & 0xfc500000) == 0xe4100000)
1271 {
1272 /* Similarly ignore single loads from the stack. */
1273 if (pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1274 continue;
1275 else
1276 break;
1277 }
1278 else if ((insn & 0xffff0ff0) == 0xe1a00000)
1279 /* MOV Rd, Rm. Skip register copies, i.e. saves to another
1280 register instead of the stack. */
d4473757 1281 continue;
0d39a070
DJ
1282 else
1283 {
1284 /* The optimizer might shove anything into the prologue,
1285 so we just skip what we don't recognize. */
1286 unrecognized_pc = current_pc;
1287 continue;
1288 }
c906108c
SS
1289 }
1290
0d39a070
DJ
1291 if (unrecognized_pc == 0)
1292 unrecognized_pc = current_pc;
1293
4be43953
DJ
1294 /* The frame size is just the distance from the frame register
1295 to the original stack pointer. */
1296 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1297 {
1298 /* Frame pointer is fp. */
0d39a070
DJ
1299 framereg = ARM_FP_REGNUM;
1300 framesize = -regs[ARM_FP_REGNUM].k;
4be43953
DJ
1301 }
1302 else if (pv_is_register (regs[ARM_SP_REGNUM], ARM_SP_REGNUM))
1303 {
1304 /* Try the stack pointer... this is a bit desperate. */
0d39a070
DJ
1305 framereg = ARM_SP_REGNUM;
1306 framesize = -regs[ARM_SP_REGNUM].k;
4be43953 1307 }
d4473757 1308 else
4be43953
DJ
1309 {
1310 /* We're just out of luck. We don't know where the frame is. */
0d39a070
DJ
1311 framereg = -1;
1312 framesize = 0;
4be43953
DJ
1313 }
1314
0d39a070
DJ
1315 if (cache)
1316 {
1317 cache->framereg = framereg;
1318 cache->framesize = framesize;
1319
1320 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1321 if (pv_area_find_reg (stack, gdbarch, regno, &offset))
1322 cache->saved_regs[regno].addr = offset;
1323 }
1324
1325 if (arm_debug)
1326 fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1327 paddress (gdbarch, unrecognized_pc));
4be43953
DJ
1328
1329 do_cleanups (back_to);
0d39a070
DJ
1330 return unrecognized_pc;
1331}
1332
1333static void
1334arm_scan_prologue (struct frame_info *this_frame,
1335 struct arm_prologue_cache *cache)
1336{
1337 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1338 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1339 int regno;
1340 CORE_ADDR prologue_start, prologue_end, current_pc;
1341 CORE_ADDR prev_pc = get_frame_pc (this_frame);
1342 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1343 pv_t regs[ARM_FPS_REGNUM];
1344 struct pv_area *stack;
1345 struct cleanup *back_to;
1346 CORE_ADDR offset;
1347
1348 /* Assume there is no frame until proven otherwise. */
1349 cache->framereg = ARM_SP_REGNUM;
1350 cache->framesize = 0;
1351
1352 /* Check for Thumb prologue. */
1353 if (arm_frame_is_thumb (this_frame))
1354 {
1355 thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
1356 return;
1357 }
1358
1359 /* Find the function prologue. If we can't find the function in
1360 the symbol table, peek in the stack frame to find the PC. */
1361 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1362 &prologue_end))
1363 {
1364 /* One way to find the end of the prologue (which works well
1365 for unoptimized code) is to do the following:
1366
1367 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
1368
1369 if (sal.line == 0)
1370 prologue_end = prev_pc;
1371 else if (sal.end < prologue_end)
1372 prologue_end = sal.end;
1373
1374 This mechanism is very accurate so long as the optimizer
1375 doesn't move any instructions from the function body into the
1376 prologue. If this happens, sal.end will be the last
1377 instruction in the first hunk of prologue code just before
1378 the first instruction that the scheduler has moved from
1379 the body to the prologue.
1380
1381 In order to make sure that we scan all of the prologue
1382 instructions, we use a slightly less accurate mechanism which
1383 may scan more than necessary. To help compensate for this
1384 lack of accuracy, the prologue scanning loop below contains
1385 several clauses which'll cause the loop to terminate early if
1386 an implausible prologue instruction is encountered.
1387
1388 The expression
1389
1390 prologue_start + 64
1391
1392 is a suitable endpoint since it accounts for the largest
1393 possible prologue plus up to five instructions inserted by
1394 the scheduler. */
1395
1396 if (prologue_end > prologue_start + 64)
1397 {
1398 prologue_end = prologue_start + 64; /* See above. */
1399 }
1400 }
1401 else
1402 {
1403 /* We have no symbol information. Our only option is to assume this
1404 function has a standard stack frame and the normal frame register.
1405 Then, we can find the value of our frame pointer on entrance to
1406 the callee (or at the present moment if this is the innermost frame).
1407 The value stored there should be the address of the stmfd + 8. */
1408 CORE_ADDR frame_loc;
1409 LONGEST return_value;
1410
1411 frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
1412 if (!safe_read_memory_integer (frame_loc, 4, byte_order, &return_value))
1413 return;
1414 else
1415 {
1416 prologue_start = gdbarch_addr_bits_remove
1417 (gdbarch, return_value) - 8;
1418 prologue_end = prologue_start + 64; /* See above. */
1419 }
1420 }
1421
1422 if (prev_pc < prologue_end)
1423 prologue_end = prev_pc;
1424
1425 arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
1426}
1427
eb5492fa 1428static struct arm_prologue_cache *
a262aec2 1429arm_make_prologue_cache (struct frame_info *this_frame)
c906108c 1430{
eb5492fa
DJ
1431 int reg;
1432 struct arm_prologue_cache *cache;
1433 CORE_ADDR unwound_fp;
c5aa993b 1434
35d5d4ee 1435 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
a262aec2 1436 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
c906108c 1437
a262aec2 1438 arm_scan_prologue (this_frame, cache);
848cfffb 1439
a262aec2 1440 unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
eb5492fa
DJ
1441 if (unwound_fp == 0)
1442 return cache;
c906108c 1443
4be43953 1444 cache->prev_sp = unwound_fp + cache->framesize;
c906108c 1445
eb5492fa
DJ
1446 /* Calculate actual addresses of saved registers using offsets
1447 determined by arm_scan_prologue. */
a262aec2 1448 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
e28a332c 1449 if (trad_frame_addr_p (cache->saved_regs, reg))
eb5492fa
DJ
1450 cache->saved_regs[reg].addr += cache->prev_sp;
1451
1452 return cache;
c906108c
SS
1453}
1454
eb5492fa
DJ
1455/* Our frame ID for a normal frame is the current function's starting PC
1456 and the caller's SP when we were called. */
c906108c 1457
148754e5 1458static void
a262aec2 1459arm_prologue_this_id (struct frame_info *this_frame,
eb5492fa
DJ
1460 void **this_cache,
1461 struct frame_id *this_id)
c906108c 1462{
eb5492fa
DJ
1463 struct arm_prologue_cache *cache;
1464 struct frame_id id;
2c404490 1465 CORE_ADDR pc, func;
f079148d 1466
eb5492fa 1467 if (*this_cache == NULL)
a262aec2 1468 *this_cache = arm_make_prologue_cache (this_frame);
eb5492fa 1469 cache = *this_cache;
2a451106 1470
2c404490
DJ
1471 /* This is meant to halt the backtrace at "_start". */
1472 pc = get_frame_pc (this_frame);
1473 if (pc <= gdbarch_tdep (get_frame_arch (this_frame))->lowest_pc)
eb5492fa 1474 return;
5a203e44 1475
eb5492fa
DJ
1476 /* If we've hit a wall, stop. */
1477 if (cache->prev_sp == 0)
1478 return;
24de872b 1479
2c404490 1480 func = get_frame_func (this_frame);
eb5492fa 1481 id = frame_id_build (cache->prev_sp, func);
eb5492fa 1482 *this_id = id;
c906108c
SS
1483}
1484
a262aec2
DJ
1485static struct value *
1486arm_prologue_prev_register (struct frame_info *this_frame,
eb5492fa 1487 void **this_cache,
a262aec2 1488 int prev_regnum)
24de872b 1489{
24568a2c 1490 struct gdbarch *gdbarch = get_frame_arch (this_frame);
24de872b
DJ
1491 struct arm_prologue_cache *cache;
1492
eb5492fa 1493 if (*this_cache == NULL)
a262aec2 1494 *this_cache = arm_make_prologue_cache (this_frame);
eb5492fa 1495 cache = *this_cache;
24de872b 1496
eb5492fa 1497 /* If we are asked to unwind the PC, then we need to return the LR
b39cc962
DJ
1498 instead. The prologue may save PC, but it will point into this
1499 frame's prologue, not the next frame's resume location. Also
1500 strip the saved T bit. A valid LR may have the low bit set, but
1501 a valid PC never does. */
eb5492fa 1502 if (prev_regnum == ARM_PC_REGNUM)
b39cc962
DJ
1503 {
1504 CORE_ADDR lr;
1505
1506 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
1507 return frame_unwind_got_constant (this_frame, prev_regnum,
24568a2c 1508 arm_addr_bits_remove (gdbarch, lr));
b39cc962 1509 }
24de872b 1510
eb5492fa 1511 /* SP is generally not saved to the stack, but this frame is
a262aec2 1512 identified by the next frame's stack pointer at the time of the call.
eb5492fa
DJ
1513 The value was already reconstructed into PREV_SP. */
1514 if (prev_regnum == ARM_SP_REGNUM)
a262aec2 1515 return frame_unwind_got_constant (this_frame, prev_regnum, cache->prev_sp);
eb5492fa 1516
b39cc962
DJ
1517 /* The CPSR may have been changed by the call instruction and by the
1518 called function. The only bit we can reconstruct is the T bit,
1519 by checking the low bit of LR as of the call. This is a reliable
1520 indicator of Thumb-ness except for some ARM v4T pre-interworking
1521 Thumb code, which could get away with a clear low bit as long as
1522 the called function did not use bx. Guess that all other
1523 bits are unchanged; the condition flags are presumably lost,
1524 but the processor status is likely valid. */
1525 if (prev_regnum == ARM_PS_REGNUM)
1526 {
1527 CORE_ADDR lr, cpsr;
9779414d 1528 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
b39cc962
DJ
1529
1530 cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
1531 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
1532 if (IS_THUMB_ADDR (lr))
9779414d 1533 cpsr |= t_bit;
b39cc962 1534 else
9779414d 1535 cpsr &= ~t_bit;
b39cc962
DJ
1536 return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
1537 }
1538
a262aec2
DJ
1539 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
1540 prev_regnum);
eb5492fa
DJ
1541}
1542
1543struct frame_unwind arm_prologue_unwind = {
1544 NORMAL_FRAME,
1545 arm_prologue_this_id,
a262aec2
DJ
1546 arm_prologue_prev_register,
1547 NULL,
1548 default_frame_sniffer
eb5492fa
DJ
1549};
1550
909cf6ea 1551static struct arm_prologue_cache *
a262aec2 1552arm_make_stub_cache (struct frame_info *this_frame)
909cf6ea 1553{
909cf6ea 1554 struct arm_prologue_cache *cache;
909cf6ea 1555
35d5d4ee 1556 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
a262aec2 1557 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
909cf6ea 1558
a262aec2 1559 cache->prev_sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
909cf6ea
DJ
1560
1561 return cache;
1562}
1563
1564/* Our frame ID for a stub frame is the current SP and LR. */
1565
1566static void
a262aec2 1567arm_stub_this_id (struct frame_info *this_frame,
909cf6ea
DJ
1568 void **this_cache,
1569 struct frame_id *this_id)
1570{
1571 struct arm_prologue_cache *cache;
1572
1573 if (*this_cache == NULL)
a262aec2 1574 *this_cache = arm_make_stub_cache (this_frame);
909cf6ea
DJ
1575 cache = *this_cache;
1576
a262aec2 1577 *this_id = frame_id_build (cache->prev_sp, get_frame_pc (this_frame));
909cf6ea
DJ
1578}
1579
a262aec2
DJ
1580static int
1581arm_stub_unwind_sniffer (const struct frame_unwind *self,
1582 struct frame_info *this_frame,
1583 void **this_prologue_cache)
909cf6ea 1584{
93d42b30 1585 CORE_ADDR addr_in_block;
909cf6ea
DJ
1586 char dummy[4];
1587
a262aec2 1588 addr_in_block = get_frame_address_in_block (this_frame);
93d42b30 1589 if (in_plt_section (addr_in_block, NULL)
fc36e839
DE
1590 /* We also use the stub winder if the target memory is unreadable
1591 to avoid having the prologue unwinder trying to read it. */
a262aec2
DJ
1592 || target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
1593 return 1;
909cf6ea 1594
a262aec2 1595 return 0;
909cf6ea
DJ
1596}
1597
a262aec2
DJ
1598struct frame_unwind arm_stub_unwind = {
1599 NORMAL_FRAME,
1600 arm_stub_this_id,
1601 arm_prologue_prev_register,
1602 NULL,
1603 arm_stub_unwind_sniffer
1604};
1605
24de872b 1606static CORE_ADDR
a262aec2 1607arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
24de872b
DJ
1608{
1609 struct arm_prologue_cache *cache;
1610
eb5492fa 1611 if (*this_cache == NULL)
a262aec2 1612 *this_cache = arm_make_prologue_cache (this_frame);
eb5492fa
DJ
1613 cache = *this_cache;
1614
4be43953 1615 return cache->prev_sp - cache->framesize;
24de872b
DJ
1616}
1617
eb5492fa
DJ
1618struct frame_base arm_normal_base = {
1619 &arm_prologue_unwind,
1620 arm_normal_frame_base,
1621 arm_normal_frame_base,
1622 arm_normal_frame_base
1623};
1624
a262aec2 1625/* Assuming THIS_FRAME is a dummy, return the frame ID of that
eb5492fa
DJ
1626 dummy frame. The frame ID's base needs to match the TOS value
1627 saved by save_dummy_frame_tos() and returned from
1628 arm_push_dummy_call, and the PC needs to match the dummy frame's
1629 breakpoint. */
c906108c 1630
eb5492fa 1631static struct frame_id
a262aec2 1632arm_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
c906108c 1633{
a262aec2
DJ
1634 return frame_id_build (get_frame_register_unsigned (this_frame, ARM_SP_REGNUM),
1635 get_frame_pc (this_frame));
eb5492fa 1636}
c3b4394c 1637
eb5492fa
DJ
1638/* Given THIS_FRAME, find the previous frame's resume PC (which will
1639 be used to construct the previous frame's ID, after looking up the
1640 containing function). */
c3b4394c 1641
eb5492fa
DJ
1642static CORE_ADDR
1643arm_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
1644{
1645 CORE_ADDR pc;
1646 pc = frame_unwind_register_unsigned (this_frame, ARM_PC_REGNUM);
24568a2c 1647 return arm_addr_bits_remove (gdbarch, pc);
eb5492fa
DJ
1648}
1649
1650static CORE_ADDR
1651arm_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
1652{
1653 return frame_unwind_register_unsigned (this_frame, ARM_SP_REGNUM);
c906108c
SS
1654}
1655
b39cc962
DJ
1656static struct value *
1657arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
1658 int regnum)
1659{
24568a2c 1660 struct gdbarch * gdbarch = get_frame_arch (this_frame);
b39cc962 1661 CORE_ADDR lr, cpsr;
9779414d 1662 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
b39cc962
DJ
1663
1664 switch (regnum)
1665 {
1666 case ARM_PC_REGNUM:
1667 /* The PC is normally copied from the return column, which
1668 describes saves of LR. However, that version may have an
1669 extra bit set to indicate Thumb state. The bit is not
1670 part of the PC. */
1671 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
1672 return frame_unwind_got_constant (this_frame, regnum,
24568a2c 1673 arm_addr_bits_remove (gdbarch, lr));
b39cc962
DJ
1674
1675 case ARM_PS_REGNUM:
1676 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
ca38c58e 1677 cpsr = get_frame_register_unsigned (this_frame, regnum);
b39cc962
DJ
1678 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
1679 if (IS_THUMB_ADDR (lr))
9779414d 1680 cpsr |= t_bit;
b39cc962 1681 else
9779414d 1682 cpsr &= ~t_bit;
ca38c58e 1683 return frame_unwind_got_constant (this_frame, regnum, cpsr);
b39cc962
DJ
1684
1685 default:
1686 internal_error (__FILE__, __LINE__,
1687 _("Unexpected register %d"), regnum);
1688 }
1689}
1690
1691static void
1692arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
1693 struct dwarf2_frame_state_reg *reg,
1694 struct frame_info *this_frame)
1695{
1696 switch (regnum)
1697 {
1698 case ARM_PC_REGNUM:
1699 case ARM_PS_REGNUM:
1700 reg->how = DWARF2_FRAME_REG_FN;
1701 reg->loc.fn = arm_dwarf2_prev_register;
1702 break;
1703 case ARM_SP_REGNUM:
1704 reg->how = DWARF2_FRAME_REG_CFA;
1705 break;
1706 }
1707}
1708
2dd604e7
RE
1709/* When arguments must be pushed onto the stack, they go on in reverse
1710 order. The code below implements a FILO (stack) to do this. */
1711
1712struct stack_item
1713{
1714 int len;
1715 struct stack_item *prev;
1716 void *data;
1717};
1718
1719static struct stack_item *
8c6363cf 1720push_stack_item (struct stack_item *prev, const void *contents, int len)
2dd604e7
RE
1721{
1722 struct stack_item *si;
1723 si = xmalloc (sizeof (struct stack_item));
226c7fbc 1724 si->data = xmalloc (len);
2dd604e7
RE
1725 si->len = len;
1726 si->prev = prev;
1727 memcpy (si->data, contents, len);
1728 return si;
1729}
1730
1731static struct stack_item *
1732pop_stack_item (struct stack_item *si)
1733{
1734 struct stack_item *dead = si;
1735 si = si->prev;
1736 xfree (dead->data);
1737 xfree (dead);
1738 return si;
1739}
1740
2af48f68
PB
1741
1742/* Return the alignment (in bytes) of the given type. */
1743
1744static int
1745arm_type_align (struct type *t)
1746{
1747 int n;
1748 int align;
1749 int falign;
1750
1751 t = check_typedef (t);
1752 switch (TYPE_CODE (t))
1753 {
1754 default:
1755 /* Should never happen. */
1756 internal_error (__FILE__, __LINE__, _("unknown type alignment"));
1757 return 4;
1758
1759 case TYPE_CODE_PTR:
1760 case TYPE_CODE_ENUM:
1761 case TYPE_CODE_INT:
1762 case TYPE_CODE_FLT:
1763 case TYPE_CODE_SET:
1764 case TYPE_CODE_RANGE:
1765 case TYPE_CODE_BITSTRING:
1766 case TYPE_CODE_REF:
1767 case TYPE_CODE_CHAR:
1768 case TYPE_CODE_BOOL:
1769 return TYPE_LENGTH (t);
1770
1771 case TYPE_CODE_ARRAY:
1772 case TYPE_CODE_COMPLEX:
1773 /* TODO: What about vector types? */
1774 return arm_type_align (TYPE_TARGET_TYPE (t));
1775
1776 case TYPE_CODE_STRUCT:
1777 case TYPE_CODE_UNION:
1778 align = 1;
1779 for (n = 0; n < TYPE_NFIELDS (t); n++)
1780 {
1781 falign = arm_type_align (TYPE_FIELD_TYPE (t, n));
1782 if (falign > align)
1783 align = falign;
1784 }
1785 return align;
1786 }
1787}
1788
90445bd3
DJ
1789/* Possible base types for a candidate for passing and returning in
1790 VFP registers. */
1791
1792enum arm_vfp_cprc_base_type
1793{
1794 VFP_CPRC_UNKNOWN,
1795 VFP_CPRC_SINGLE,
1796 VFP_CPRC_DOUBLE,
1797 VFP_CPRC_VEC64,
1798 VFP_CPRC_VEC128
1799};
1800
1801/* The length of one element of base type B. */
1802
1803static unsigned
1804arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
1805{
1806 switch (b)
1807 {
1808 case VFP_CPRC_SINGLE:
1809 return 4;
1810 case VFP_CPRC_DOUBLE:
1811 return 8;
1812 case VFP_CPRC_VEC64:
1813 return 8;
1814 case VFP_CPRC_VEC128:
1815 return 16;
1816 default:
1817 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
1818 (int) b);
1819 }
1820}
1821
1822/* The character ('s', 'd' or 'q') for the type of VFP register used
1823 for passing base type B. */
1824
1825static int
1826arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
1827{
1828 switch (b)
1829 {
1830 case VFP_CPRC_SINGLE:
1831 return 's';
1832 case VFP_CPRC_DOUBLE:
1833 return 'd';
1834 case VFP_CPRC_VEC64:
1835 return 'd';
1836 case VFP_CPRC_VEC128:
1837 return 'q';
1838 default:
1839 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
1840 (int) b);
1841 }
1842}
1843
1844/* Determine whether T may be part of a candidate for passing and
1845 returning in VFP registers, ignoring the limit on the total number
1846 of components. If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
1847 classification of the first valid component found; if it is not
1848 VFP_CPRC_UNKNOWN, all components must have the same classification
1849 as *BASE_TYPE. If it is found that T contains a type not permitted
1850 for passing and returning in VFP registers, a type differently
1851 classified from *BASE_TYPE, or two types differently classified
1852 from each other, return -1, otherwise return the total number of
1853 base-type elements found (possibly 0 in an empty structure or
1854 array). Vectors and complex types are not currently supported,
1855 matching the generic AAPCS support. */
1856
1857static int
1858arm_vfp_cprc_sub_candidate (struct type *t,
1859 enum arm_vfp_cprc_base_type *base_type)
1860{
1861 t = check_typedef (t);
1862 switch (TYPE_CODE (t))
1863 {
1864 case TYPE_CODE_FLT:
1865 switch (TYPE_LENGTH (t))
1866 {
1867 case 4:
1868 if (*base_type == VFP_CPRC_UNKNOWN)
1869 *base_type = VFP_CPRC_SINGLE;
1870 else if (*base_type != VFP_CPRC_SINGLE)
1871 return -1;
1872 return 1;
1873
1874 case 8:
1875 if (*base_type == VFP_CPRC_UNKNOWN)
1876 *base_type = VFP_CPRC_DOUBLE;
1877 else if (*base_type != VFP_CPRC_DOUBLE)
1878 return -1;
1879 return 1;
1880
1881 default:
1882 return -1;
1883 }
1884 break;
1885
1886 case TYPE_CODE_ARRAY:
1887 {
1888 int count;
1889 unsigned unitlen;
1890 count = arm_vfp_cprc_sub_candidate (TYPE_TARGET_TYPE (t), base_type);
1891 if (count == -1)
1892 return -1;
1893 if (TYPE_LENGTH (t) == 0)
1894 {
1895 gdb_assert (count == 0);
1896 return 0;
1897 }
1898 else if (count == 0)
1899 return -1;
1900 unitlen = arm_vfp_cprc_unit_length (*base_type);
1901 gdb_assert ((TYPE_LENGTH (t) % unitlen) == 0);
1902 return TYPE_LENGTH (t) / unitlen;
1903 }
1904 break;
1905
1906 case TYPE_CODE_STRUCT:
1907 {
1908 int count = 0;
1909 unsigned unitlen;
1910 int i;
1911 for (i = 0; i < TYPE_NFIELDS (t); i++)
1912 {
1913 int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
1914 base_type);
1915 if (sub_count == -1)
1916 return -1;
1917 count += sub_count;
1918 }
1919 if (TYPE_LENGTH (t) == 0)
1920 {
1921 gdb_assert (count == 0);
1922 return 0;
1923 }
1924 else if (count == 0)
1925 return -1;
1926 unitlen = arm_vfp_cprc_unit_length (*base_type);
1927 if (TYPE_LENGTH (t) != unitlen * count)
1928 return -1;
1929 return count;
1930 }
1931
1932 case TYPE_CODE_UNION:
1933 {
1934 int count = 0;
1935 unsigned unitlen;
1936 int i;
1937 for (i = 0; i < TYPE_NFIELDS (t); i++)
1938 {
1939 int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
1940 base_type);
1941 if (sub_count == -1)
1942 return -1;
1943 count = (count > sub_count ? count : sub_count);
1944 }
1945 if (TYPE_LENGTH (t) == 0)
1946 {
1947 gdb_assert (count == 0);
1948 return 0;
1949 }
1950 else if (count == 0)
1951 return -1;
1952 unitlen = arm_vfp_cprc_unit_length (*base_type);
1953 if (TYPE_LENGTH (t) != unitlen * count)
1954 return -1;
1955 return count;
1956 }
1957
1958 default:
1959 break;
1960 }
1961
1962 return -1;
1963}
1964
1965/* Determine whether T is a VFP co-processor register candidate (CPRC)
1966 if passed to or returned from a non-variadic function with the VFP
1967 ABI in effect. Return 1 if it is, 0 otherwise. If it is, set
1968 *BASE_TYPE to the base type for T and *COUNT to the number of
1969 elements of that base type before returning. */
1970
1971static int
1972arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
1973 int *count)
1974{
1975 enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
1976 int c = arm_vfp_cprc_sub_candidate (t, &b);
1977 if (c <= 0 || c > 4)
1978 return 0;
1979 *base_type = b;
1980 *count = c;
1981 return 1;
1982}
1983
1984/* Return 1 if the VFP ABI should be used for passing arguments to and
1985 returning values from a function of type FUNC_TYPE, 0
1986 otherwise. */
1987
1988static int
1989arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
1990{
1991 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1992 /* Variadic functions always use the base ABI. Assume that functions
1993 without debug info are not variadic. */
1994 if (func_type && TYPE_VARARGS (check_typedef (func_type)))
1995 return 0;
1996 /* The VFP ABI is only supported as a variant of AAPCS. */
1997 if (tdep->arm_abi != ARM_ABI_AAPCS)
1998 return 0;
1999 return gdbarch_tdep (gdbarch)->fp_model == ARM_FLOAT_VFP;
2000}
2001
2002/* We currently only support passing parameters in integer registers, which
2003 conforms with GCC's default model, and VFP argument passing following
2004 the VFP variant of AAPCS. Several other variants exist and
2dd604e7
RE
2005 we should probably support some of them based on the selected ABI. */
2006
2007static CORE_ADDR
7d9b040b 2008arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6a65450a
AC
2009 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
2010 struct value **args, CORE_ADDR sp, int struct_return,
2011 CORE_ADDR struct_addr)
2dd604e7 2012{
e17a4113 2013 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2dd604e7
RE
2014 int argnum;
2015 int argreg;
2016 int nstack;
2017 struct stack_item *si = NULL;
90445bd3
DJ
2018 int use_vfp_abi;
2019 struct type *ftype;
2020 unsigned vfp_regs_free = (1 << 16) - 1;
2021
2022 /* Determine the type of this function and whether the VFP ABI
2023 applies. */
2024 ftype = check_typedef (value_type (function));
2025 if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
2026 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
2027 use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
2dd604e7 2028
6a65450a
AC
2029 /* Set the return address. For the ARM, the return breakpoint is
2030 always at BP_ADDR. */
9779414d 2031 if (arm_pc_is_thumb (gdbarch, bp_addr))
9dca5578 2032 bp_addr |= 1;
6a65450a 2033 regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
2dd604e7
RE
2034
2035 /* Walk through the list of args and determine how large a temporary
2036 stack is required. Need to take care here as structs may be
2037 passed on the stack, and we have to to push them. */
2038 nstack = 0;
2039
2040 argreg = ARM_A1_REGNUM;
2041 nstack = 0;
2042
2dd604e7
RE
2043 /* The struct_return pointer occupies the first parameter
2044 passing register. */
2045 if (struct_return)
2046 {
2047 if (arm_debug)
5af949e3 2048 fprintf_unfiltered (gdb_stdlog, "struct return in %s = %s\n",
2af46ca0 2049 gdbarch_register_name (gdbarch, argreg),
5af949e3 2050 paddress (gdbarch, struct_addr));
2dd604e7
RE
2051 regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
2052 argreg++;
2053 }
2054
2055 for (argnum = 0; argnum < nargs; argnum++)
2056 {
2057 int len;
2058 struct type *arg_type;
2059 struct type *target_type;
2060 enum type_code typecode;
8c6363cf 2061 const bfd_byte *val;
2af48f68 2062 int align;
90445bd3
DJ
2063 enum arm_vfp_cprc_base_type vfp_base_type;
2064 int vfp_base_count;
2065 int may_use_core_reg = 1;
2dd604e7 2066
df407dfe 2067 arg_type = check_typedef (value_type (args[argnum]));
2dd604e7
RE
2068 len = TYPE_LENGTH (arg_type);
2069 target_type = TYPE_TARGET_TYPE (arg_type);
2070 typecode = TYPE_CODE (arg_type);
8c6363cf 2071 val = value_contents (args[argnum]);
2dd604e7 2072
2af48f68
PB
2073 align = arm_type_align (arg_type);
2074 /* Round alignment up to a whole number of words. */
2075 align = (align + INT_REGISTER_SIZE - 1) & ~(INT_REGISTER_SIZE - 1);
2076 /* Different ABIs have different maximum alignments. */
2077 if (gdbarch_tdep (gdbarch)->arm_abi == ARM_ABI_APCS)
2078 {
2079 /* The APCS ABI only requires word alignment. */
2080 align = INT_REGISTER_SIZE;
2081 }
2082 else
2083 {
2084 /* The AAPCS requires at most doubleword alignment. */
2085 if (align > INT_REGISTER_SIZE * 2)
2086 align = INT_REGISTER_SIZE * 2;
2087 }
2088
90445bd3
DJ
2089 if (use_vfp_abi
2090 && arm_vfp_call_candidate (arg_type, &vfp_base_type,
2091 &vfp_base_count))
2092 {
2093 int regno;
2094 int unit_length;
2095 int shift;
2096 unsigned mask;
2097
2098 /* Because this is a CPRC it cannot go in a core register or
2099 cause a core register to be skipped for alignment.
2100 Either it goes in VFP registers and the rest of this loop
2101 iteration is skipped for this argument, or it goes on the
2102 stack (and the stack alignment code is correct for this
2103 case). */
2104 may_use_core_reg = 0;
2105
2106 unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
2107 shift = unit_length / 4;
2108 mask = (1 << (shift * vfp_base_count)) - 1;
2109 for (regno = 0; regno < 16; regno += shift)
2110 if (((vfp_regs_free >> regno) & mask) == mask)
2111 break;
2112
2113 if (regno < 16)
2114 {
2115 int reg_char;
2116 int reg_scaled;
2117 int i;
2118
2119 vfp_regs_free &= ~(mask << regno);
2120 reg_scaled = regno / shift;
2121 reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
2122 for (i = 0; i < vfp_base_count; i++)
2123 {
2124 char name_buf[4];
2125 int regnum;
58d6951d
DJ
2126 if (reg_char == 'q')
2127 arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
90445bd3 2128 val + i * unit_length);
58d6951d
DJ
2129 else
2130 {
2131 sprintf (name_buf, "%c%d", reg_char, reg_scaled + i);
2132 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
2133 strlen (name_buf));
2134 regcache_cooked_write (regcache, regnum,
2135 val + i * unit_length);
2136 }
90445bd3
DJ
2137 }
2138 continue;
2139 }
2140 else
2141 {
2142 /* This CPRC could not go in VFP registers, so all VFP
2143 registers are now marked as used. */
2144 vfp_regs_free = 0;
2145 }
2146 }
2147
2af48f68
PB
2148 /* Push stack padding for dowubleword alignment. */
2149 if (nstack & (align - 1))
2150 {
2151 si = push_stack_item (si, val, INT_REGISTER_SIZE);
2152 nstack += INT_REGISTER_SIZE;
2153 }
2154
2155 /* Doubleword aligned quantities must go in even register pairs. */
90445bd3
DJ
2156 if (may_use_core_reg
2157 && argreg <= ARM_LAST_ARG_REGNUM
2af48f68
PB
2158 && align > INT_REGISTER_SIZE
2159 && argreg & 1)
2160 argreg++;
2161
2dd604e7
RE
2162 /* If the argument is a pointer to a function, and it is a
2163 Thumb function, create a LOCAL copy of the value and set
2164 the THUMB bit in it. */
2165 if (TYPE_CODE_PTR == typecode
2166 && target_type != NULL
f96b8fa0 2167 && TYPE_CODE_FUNC == TYPE_CODE (check_typedef (target_type)))
2dd604e7 2168 {
e17a4113 2169 CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
9779414d 2170 if (arm_pc_is_thumb (gdbarch, regval))
2dd604e7 2171 {
8c6363cf
TT
2172 bfd_byte *copy = alloca (len);
2173 store_unsigned_integer (copy, len, byte_order,
e17a4113 2174 MAKE_THUMB_ADDR (regval));
8c6363cf 2175 val = copy;
2dd604e7
RE
2176 }
2177 }
2178
2179 /* Copy the argument to general registers or the stack in
2180 register-sized pieces. Large arguments are split between
2181 registers and stack. */
2182 while (len > 0)
2183 {
f0c9063c 2184 int partial_len = len < INT_REGISTER_SIZE ? len : INT_REGISTER_SIZE;
2dd604e7 2185
90445bd3 2186 if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
2dd604e7
RE
2187 {
2188 /* The argument is being passed in a general purpose
2189 register. */
e17a4113
UW
2190 CORE_ADDR regval
2191 = extract_unsigned_integer (val, partial_len, byte_order);
2192 if (byte_order == BFD_ENDIAN_BIG)
8bf8793c 2193 regval <<= (INT_REGISTER_SIZE - partial_len) * 8;
2dd604e7
RE
2194 if (arm_debug)
2195 fprintf_unfiltered (gdb_stdlog, "arg %d in %s = 0x%s\n",
c9f4d572
UW
2196 argnum,
2197 gdbarch_register_name
2af46ca0 2198 (gdbarch, argreg),
f0c9063c 2199 phex (regval, INT_REGISTER_SIZE));
2dd604e7
RE
2200 regcache_cooked_write_unsigned (regcache, argreg, regval);
2201 argreg++;
2202 }
2203 else
2204 {
2205 /* Push the arguments onto the stack. */
2206 if (arm_debug)
2207 fprintf_unfiltered (gdb_stdlog, "arg %d @ sp + %d\n",
2208 argnum, nstack);
f0c9063c
UW
2209 si = push_stack_item (si, val, INT_REGISTER_SIZE);
2210 nstack += INT_REGISTER_SIZE;
2dd604e7
RE
2211 }
2212
2213 len -= partial_len;
2214 val += partial_len;
2215 }
2216 }
2217 /* If we have an odd number of words to push, then decrement the stack
2218 by one word now, so first stack argument will be dword aligned. */
2219 if (nstack & 4)
2220 sp -= 4;
2221
2222 while (si)
2223 {
2224 sp -= si->len;
2225 write_memory (sp, si->data, si->len);
2226 si = pop_stack_item (si);
2227 }
2228
2229 /* Finally, update teh SP register. */
2230 regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
2231
2232 return sp;
2233}
2234
f53f0d0b
PB
2235
2236/* Always align the frame to an 8-byte boundary. This is required on
2237 some platforms and harmless on the rest. */
2238
2239static CORE_ADDR
2240arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
2241{
2242 /* Align the stack to eight bytes. */
2243 return sp & ~ (CORE_ADDR) 7;
2244}
2245
c906108c 2246static void
ed9a39eb 2247print_fpu_flags (int flags)
c906108c 2248{
c5aa993b
JM
2249 if (flags & (1 << 0))
2250 fputs ("IVO ", stdout);
2251 if (flags & (1 << 1))
2252 fputs ("DVZ ", stdout);
2253 if (flags & (1 << 2))
2254 fputs ("OFL ", stdout);
2255 if (flags & (1 << 3))
2256 fputs ("UFL ", stdout);
2257 if (flags & (1 << 4))
2258 fputs ("INX ", stdout);
2259 putchar ('\n');
c906108c
SS
2260}
2261
5e74b15c
RE
2262/* Print interesting information about the floating point processor
2263 (if present) or emulator. */
34e8f22d 2264static void
d855c300 2265arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
23e3a7ac 2266 struct frame_info *frame, const char *args)
c906108c 2267{
9c9acae0 2268 unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
c5aa993b
JM
2269 int type;
2270
2271 type = (status >> 24) & 127;
edefbb7c
AC
2272 if (status & (1 << 31))
2273 printf (_("Hardware FPU type %d\n"), type);
2274 else
2275 printf (_("Software FPU type %d\n"), type);
2276 /* i18n: [floating point unit] mask */
2277 fputs (_("mask: "), stdout);
c5aa993b 2278 print_fpu_flags (status >> 16);
edefbb7c
AC
2279 /* i18n: [floating point unit] flags */
2280 fputs (_("flags: "), stdout);
c5aa993b 2281 print_fpu_flags (status);
c906108c
SS
2282}
2283
27067745
UW
2284/* Construct the ARM extended floating point type. */
2285static struct type *
2286arm_ext_type (struct gdbarch *gdbarch)
2287{
2288 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2289
2290 if (!tdep->arm_ext_type)
2291 tdep->arm_ext_type
e9bb382b 2292 = arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
27067745
UW
2293 floatformats_arm_ext);
2294
2295 return tdep->arm_ext_type;
2296}
2297
58d6951d
DJ
2298static struct type *
2299arm_neon_double_type (struct gdbarch *gdbarch)
2300{
2301 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2302
2303 if (tdep->neon_double_type == NULL)
2304 {
2305 struct type *t, *elem;
2306
2307 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
2308 TYPE_CODE_UNION);
2309 elem = builtin_type (gdbarch)->builtin_uint8;
2310 append_composite_type_field (t, "u8", init_vector_type (elem, 8));
2311 elem = builtin_type (gdbarch)->builtin_uint16;
2312 append_composite_type_field (t, "u16", init_vector_type (elem, 4));
2313 elem = builtin_type (gdbarch)->builtin_uint32;
2314 append_composite_type_field (t, "u32", init_vector_type (elem, 2));
2315 elem = builtin_type (gdbarch)->builtin_uint64;
2316 append_composite_type_field (t, "u64", elem);
2317 elem = builtin_type (gdbarch)->builtin_float;
2318 append_composite_type_field (t, "f32", init_vector_type (elem, 2));
2319 elem = builtin_type (gdbarch)->builtin_double;
2320 append_composite_type_field (t, "f64", elem);
2321
2322 TYPE_VECTOR (t) = 1;
2323 TYPE_NAME (t) = "neon_d";
2324 tdep->neon_double_type = t;
2325 }
2326
2327 return tdep->neon_double_type;
2328}
2329
2330/* FIXME: The vector types are not correctly ordered on big-endian
2331 targets. Just as s0 is the low bits of d0, d0[0] is also the low
2332 bits of d0 - regardless of what unit size is being held in d0. So
2333 the offset of the first uint8 in d0 is 7, but the offset of the
2334 first float is 4. This code works as-is for little-endian
2335 targets. */
2336
2337static struct type *
2338arm_neon_quad_type (struct gdbarch *gdbarch)
2339{
2340 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2341
2342 if (tdep->neon_quad_type == NULL)
2343 {
2344 struct type *t, *elem;
2345
2346 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
2347 TYPE_CODE_UNION);
2348 elem = builtin_type (gdbarch)->builtin_uint8;
2349 append_composite_type_field (t, "u8", init_vector_type (elem, 16));
2350 elem = builtin_type (gdbarch)->builtin_uint16;
2351 append_composite_type_field (t, "u16", init_vector_type (elem, 8));
2352 elem = builtin_type (gdbarch)->builtin_uint32;
2353 append_composite_type_field (t, "u32", init_vector_type (elem, 4));
2354 elem = builtin_type (gdbarch)->builtin_uint64;
2355 append_composite_type_field (t, "u64", init_vector_type (elem, 2));
2356 elem = builtin_type (gdbarch)->builtin_float;
2357 append_composite_type_field (t, "f32", init_vector_type (elem, 4));
2358 elem = builtin_type (gdbarch)->builtin_double;
2359 append_composite_type_field (t, "f64", init_vector_type (elem, 2));
2360
2361 TYPE_VECTOR (t) = 1;
2362 TYPE_NAME (t) = "neon_q";
2363 tdep->neon_quad_type = t;
2364 }
2365
2366 return tdep->neon_quad_type;
2367}
2368
34e8f22d
RE
2369/* Return the GDB type object for the "standard" data type of data in
2370 register N. */
2371
2372static struct type *
7a5ea0d4 2373arm_register_type (struct gdbarch *gdbarch, int regnum)
032758dc 2374{
58d6951d
DJ
2375 int num_regs = gdbarch_num_regs (gdbarch);
2376
2377 if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
2378 && regnum >= num_regs && regnum < num_regs + 32)
2379 return builtin_type (gdbarch)->builtin_float;
2380
2381 if (gdbarch_tdep (gdbarch)->have_neon_pseudos
2382 && regnum >= num_regs + 32 && regnum < num_regs + 32 + 16)
2383 return arm_neon_quad_type (gdbarch);
2384
2385 /* If the target description has register information, we are only
2386 in this function so that we can override the types of
2387 double-precision registers for NEON. */
2388 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
2389 {
2390 struct type *t = tdesc_register_type (gdbarch, regnum);
2391
2392 if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
2393 && TYPE_CODE (t) == TYPE_CODE_FLT
2394 && gdbarch_tdep (gdbarch)->have_neon)
2395 return arm_neon_double_type (gdbarch);
2396 else
2397 return t;
2398 }
2399
34e8f22d 2400 if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
58d6951d
DJ
2401 {
2402 if (!gdbarch_tdep (gdbarch)->have_fpa_registers)
2403 return builtin_type (gdbarch)->builtin_void;
2404
2405 return arm_ext_type (gdbarch);
2406 }
e4c16157 2407 else if (regnum == ARM_SP_REGNUM)
0dfff4cb 2408 return builtin_type (gdbarch)->builtin_data_ptr;
e4c16157 2409 else if (regnum == ARM_PC_REGNUM)
0dfff4cb 2410 return builtin_type (gdbarch)->builtin_func_ptr;
ff6f572f
DJ
2411 else if (regnum >= ARRAY_SIZE (arm_register_names))
2412 /* These registers are only supported on targets which supply
2413 an XML description. */
df4df182 2414 return builtin_type (gdbarch)->builtin_int0;
032758dc 2415 else
df4df182 2416 return builtin_type (gdbarch)->builtin_uint32;
032758dc
AC
2417}
2418
ff6f572f
DJ
2419/* Map a DWARF register REGNUM onto the appropriate GDB register
2420 number. */
2421
2422static int
d3f73121 2423arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
ff6f572f
DJ
2424{
2425 /* Core integer regs. */
2426 if (reg >= 0 && reg <= 15)
2427 return reg;
2428
2429 /* Legacy FPA encoding. These were once used in a way which
2430 overlapped with VFP register numbering, so their use is
2431 discouraged, but GDB doesn't support the ARM toolchain
2432 which used them for VFP. */
2433 if (reg >= 16 && reg <= 23)
2434 return ARM_F0_REGNUM + reg - 16;
2435
2436 /* New assignments for the FPA registers. */
2437 if (reg >= 96 && reg <= 103)
2438 return ARM_F0_REGNUM + reg - 96;
2439
2440 /* WMMX register assignments. */
2441 if (reg >= 104 && reg <= 111)
2442 return ARM_WCGR0_REGNUM + reg - 104;
2443
2444 if (reg >= 112 && reg <= 127)
2445 return ARM_WR0_REGNUM + reg - 112;
2446
2447 if (reg >= 192 && reg <= 199)
2448 return ARM_WC0_REGNUM + reg - 192;
2449
58d6951d
DJ
2450 /* VFP v2 registers. A double precision value is actually
2451 in d1 rather than s2, but the ABI only defines numbering
2452 for the single precision registers. This will "just work"
2453 in GDB for little endian targets (we'll read eight bytes,
2454 starting in s0 and then progressing to s1), but will be
2455 reversed on big endian targets with VFP. This won't
2456 be a problem for the new Neon quad registers; you're supposed
2457 to use DW_OP_piece for those. */
2458 if (reg >= 64 && reg <= 95)
2459 {
2460 char name_buf[4];
2461
2462 sprintf (name_buf, "s%d", reg - 64);
2463 return user_reg_map_name_to_regnum (gdbarch, name_buf,
2464 strlen (name_buf));
2465 }
2466
2467 /* VFP v3 / Neon registers. This range is also used for VFP v2
2468 registers, except that it now describes d0 instead of s0. */
2469 if (reg >= 256 && reg <= 287)
2470 {
2471 char name_buf[4];
2472
2473 sprintf (name_buf, "d%d", reg - 256);
2474 return user_reg_map_name_to_regnum (gdbarch, name_buf,
2475 strlen (name_buf));
2476 }
2477
ff6f572f
DJ
2478 return -1;
2479}
2480
26216b98
AC
2481/* Map GDB internal REGNUM onto the Arm simulator register numbers. */
2482static int
e7faf938 2483arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
26216b98
AC
2484{
2485 int reg = regnum;
e7faf938 2486 gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
26216b98 2487
ff6f572f
DJ
2488 if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
2489 return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
2490
2491 if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
2492 return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
2493
2494 if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
2495 return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
2496
26216b98
AC
2497 if (reg < NUM_GREGS)
2498 return SIM_ARM_R0_REGNUM + reg;
2499 reg -= NUM_GREGS;
2500
2501 if (reg < NUM_FREGS)
2502 return SIM_ARM_FP0_REGNUM + reg;
2503 reg -= NUM_FREGS;
2504
2505 if (reg < NUM_SREGS)
2506 return SIM_ARM_FPS_REGNUM + reg;
2507 reg -= NUM_SREGS;
2508
edefbb7c 2509 internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
26216b98 2510}
34e8f22d 2511
a37b3cc0
AC
2512/* NOTE: cagney/2001-08-20: Both convert_from_extended() and
2513 convert_to_extended() use floatformat_arm_ext_littlebyte_bigword.
2514 It is thought that this is is the floating-point register format on
2515 little-endian systems. */
c906108c 2516
ed9a39eb 2517static void
b508a996 2518convert_from_extended (const struct floatformat *fmt, const void *ptr,
be8626e0 2519 void *dbl, int endianess)
c906108c 2520{
a37b3cc0 2521 DOUBLEST d;
be8626e0
MD
2522
2523 if (endianess == BFD_ENDIAN_BIG)
a37b3cc0
AC
2524 floatformat_to_doublest (&floatformat_arm_ext_big, ptr, &d);
2525 else
2526 floatformat_to_doublest (&floatformat_arm_ext_littlebyte_bigword,
2527 ptr, &d);
b508a996 2528 floatformat_from_doublest (fmt, &d, dbl);
c906108c
SS
2529}
2530
34e8f22d 2531static void
be8626e0
MD
2532convert_to_extended (const struct floatformat *fmt, void *dbl, const void *ptr,
2533 int endianess)
c906108c 2534{
a37b3cc0 2535 DOUBLEST d;
be8626e0 2536
b508a996 2537 floatformat_to_doublest (fmt, ptr, &d);
be8626e0 2538 if (endianess == BFD_ENDIAN_BIG)
a37b3cc0
AC
2539 floatformat_from_doublest (&floatformat_arm_ext_big, &d, dbl);
2540 else
2541 floatformat_from_doublest (&floatformat_arm_ext_littlebyte_bigword,
2542 &d, dbl);
c906108c 2543}
ed9a39eb 2544
c906108c 2545static int
ed9a39eb 2546condition_true (unsigned long cond, unsigned long status_reg)
c906108c
SS
2547{
2548 if (cond == INST_AL || cond == INST_NV)
2549 return 1;
2550
2551 switch (cond)
2552 {
2553 case INST_EQ:
2554 return ((status_reg & FLAG_Z) != 0);
2555 case INST_NE:
2556 return ((status_reg & FLAG_Z) == 0);
2557 case INST_CS:
2558 return ((status_reg & FLAG_C) != 0);
2559 case INST_CC:
2560 return ((status_reg & FLAG_C) == 0);
2561 case INST_MI:
2562 return ((status_reg & FLAG_N) != 0);
2563 case INST_PL:
2564 return ((status_reg & FLAG_N) == 0);
2565 case INST_VS:
2566 return ((status_reg & FLAG_V) != 0);
2567 case INST_VC:
2568 return ((status_reg & FLAG_V) == 0);
2569 case INST_HI:
2570 return ((status_reg & (FLAG_C | FLAG_Z)) == FLAG_C);
2571 case INST_LS:
2572 return ((status_reg & (FLAG_C | FLAG_Z)) != FLAG_C);
2573 case INST_GE:
2574 return (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0));
2575 case INST_LT:
2576 return (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0));
2577 case INST_GT:
f8bf5763
PM
2578 return (((status_reg & FLAG_Z) == 0)
2579 && (((status_reg & FLAG_N) == 0)
2580 == ((status_reg & FLAG_V) == 0)));
c906108c 2581 case INST_LE:
f8bf5763
PM
2582 return (((status_reg & FLAG_Z) != 0)
2583 || (((status_reg & FLAG_N) == 0)
2584 != ((status_reg & FLAG_V) == 0)));
c906108c
SS
2585 }
2586 return 1;
2587}
2588
c906108c 2589static unsigned long
0b1b3e42
UW
2590shifted_reg_val (struct frame_info *frame, unsigned long inst, int carry,
2591 unsigned long pc_val, unsigned long status_reg)
c906108c
SS
2592{
2593 unsigned long res, shift;
2594 int rm = bits (inst, 0, 3);
2595 unsigned long shifttype = bits (inst, 5, 6);
c5aa993b
JM
2596
2597 if (bit (inst, 4))
c906108c
SS
2598 {
2599 int rs = bits (inst, 8, 11);
0b1b3e42
UW
2600 shift = (rs == 15 ? pc_val + 8
2601 : get_frame_register_unsigned (frame, rs)) & 0xFF;
c906108c
SS
2602 }
2603 else
2604 shift = bits (inst, 7, 11);
c5aa993b
JM
2605
2606 res = (rm == 15
0d39a070 2607 ? (pc_val + (bit (inst, 4) ? 12 : 8))
0b1b3e42 2608 : get_frame_register_unsigned (frame, rm));
c906108c
SS
2609
2610 switch (shifttype)
2611 {
c5aa993b 2612 case 0: /* LSL */
c906108c
SS
2613 res = shift >= 32 ? 0 : res << shift;
2614 break;
c5aa993b
JM
2615
2616 case 1: /* LSR */
c906108c
SS
2617 res = shift >= 32 ? 0 : res >> shift;
2618 break;
2619
c5aa993b
JM
2620 case 2: /* ASR */
2621 if (shift >= 32)
2622 shift = 31;
c906108c
SS
2623 res = ((res & 0x80000000L)
2624 ? ~((~res) >> shift) : res >> shift);
2625 break;
2626
c5aa993b 2627 case 3: /* ROR/RRX */
c906108c
SS
2628 shift &= 31;
2629 if (shift == 0)
2630 res = (res >> 1) | (carry ? 0x80000000L : 0);
2631 else
c5aa993b 2632 res = (res >> shift) | (res << (32 - shift));
c906108c
SS
2633 break;
2634 }
2635
2636 return res & 0xffffffff;
2637}
2638
c906108c
SS
2639/* Return number of 1-bits in VAL. */
2640
2641static int
ed9a39eb 2642bitcount (unsigned long val)
c906108c
SS
2643{
2644 int nbits;
2645 for (nbits = 0; val != 0; nbits++)
c5aa993b 2646 val &= val - 1; /* delete rightmost 1-bit in val */
c906108c
SS
2647 return nbits;
2648}
2649
177321bd
DJ
2650/* Return the size in bytes of the complete Thumb instruction whose
2651 first halfword is INST1. */
2652
2653static int
2654thumb_insn_size (unsigned short inst1)
2655{
2656 if ((inst1 & 0xe000) == 0xe000 && (inst1 & 0x1800) != 0)
2657 return 4;
2658 else
2659 return 2;
2660}
2661
2662static int
2663thumb_advance_itstate (unsigned int itstate)
2664{
2665 /* Preserve IT[7:5], the first three bits of the condition. Shift
2666 the upcoming condition flags left by one bit. */
2667 itstate = (itstate & 0xe0) | ((itstate << 1) & 0x1f);
2668
2669 /* If we have finished the IT block, clear the state. */
2670 if ((itstate & 0x0f) == 0)
2671 itstate = 0;
2672
2673 return itstate;
2674}
2675
2676/* Find the next PC after the current instruction executes. In some
2677 cases we can not statically determine the answer (see the IT state
2678 handling in this function); in that case, a breakpoint may be
2679 inserted in addition to the returned PC, which will be used to set
2680 another breakpoint by our caller. */
2681
ad527d2e 2682static CORE_ADDR
50e98be4 2683thumb_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc, int insert_bkpt)
c906108c 2684{
2af46ca0 2685 struct gdbarch *gdbarch = get_frame_arch (frame);
177321bd 2686 struct address_space *aspace = get_frame_address_space (frame);
e17a4113
UW
2687 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2688 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
c5aa993b 2689 unsigned long pc_val = ((unsigned long) pc) + 4; /* PC after prefetch */
e17a4113 2690 unsigned short inst1;
94c30b78 2691 CORE_ADDR nextpc = pc + 2; /* default is next instruction */
c906108c 2692 unsigned long offset;
177321bd 2693 ULONGEST status, itstate;
c906108c 2694
50e98be4
DJ
2695 nextpc = MAKE_THUMB_ADDR (nextpc);
2696 pc_val = MAKE_THUMB_ADDR (pc_val);
2697
e17a4113 2698 inst1 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
9d4fde75 2699
9dca5578
DJ
2700 /* Thumb-2 conditional execution support. There are eight bits in
2701 the CPSR which describe conditional execution state. Once
2702 reconstructed (they're in a funny order), the low five bits
2703 describe the low bit of the condition for each instruction and
2704 how many instructions remain. The high three bits describe the
2705 base condition. One of the low four bits will be set if an IT
2706 block is active. These bits read as zero on earlier
2707 processors. */
2708 status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
177321bd 2709 itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
9dca5578 2710
177321bd
DJ
2711 /* If-Then handling. On GNU/Linux, where this routine is used, we
2712 use an undefined instruction as a breakpoint. Unlike BKPT, IT
2713 can disable execution of the undefined instruction. So we might
2714 miss the breakpoint if we set it on a skipped conditional
2715 instruction. Because conditional instructions can change the
2716 flags, affecting the execution of further instructions, we may
2717 need to set two breakpoints. */
9dca5578 2718
177321bd
DJ
2719 if (gdbarch_tdep (gdbarch)->thumb2_breakpoint != NULL)
2720 {
2721 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
2722 {
2723 /* An IT instruction. Because this instruction does not
2724 modify the flags, we can accurately predict the next
2725 executed instruction. */
2726 itstate = inst1 & 0x00ff;
2727 pc += thumb_insn_size (inst1);
2728
2729 while (itstate != 0 && ! condition_true (itstate >> 4, status))
2730 {
2731 inst1 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
2732 pc += thumb_insn_size (inst1);
2733 itstate = thumb_advance_itstate (itstate);
2734 }
2735
50e98be4 2736 return MAKE_THUMB_ADDR (pc);
177321bd
DJ
2737 }
2738 else if (itstate != 0)
2739 {
2740 /* We are in a conditional block. Check the condition. */
2741 if (! condition_true (itstate >> 4, status))
2742 {
2743 /* Advance to the next executed instruction. */
2744 pc += thumb_insn_size (inst1);
2745 itstate = thumb_advance_itstate (itstate);
2746
2747 while (itstate != 0 && ! condition_true (itstate >> 4, status))
2748 {
2749 inst1 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
2750 pc += thumb_insn_size (inst1);
2751 itstate = thumb_advance_itstate (itstate);
2752 }
2753
50e98be4 2754 return MAKE_THUMB_ADDR (pc);
177321bd
DJ
2755 }
2756 else if ((itstate & 0x0f) == 0x08)
2757 {
2758 /* This is the last instruction of the conditional
2759 block, and it is executed. We can handle it normally
2760 because the following instruction is not conditional,
2761 and we must handle it normally because it is
2762 permitted to branch. Fall through. */
2763 }
2764 else
2765 {
2766 int cond_negated;
2767
2768 /* There are conditional instructions after this one.
2769 If this instruction modifies the flags, then we can
2770 not predict what the next executed instruction will
2771 be. Fortunately, this instruction is architecturally
2772 forbidden to branch; we know it will fall through.
2773 Start by skipping past it. */
2774 pc += thumb_insn_size (inst1);
2775 itstate = thumb_advance_itstate (itstate);
2776
2777 /* Set a breakpoint on the following instruction. */
2778 gdb_assert ((itstate & 0x0f) != 0);
50e98be4
DJ
2779 if (insert_bkpt)
2780 insert_single_step_breakpoint (gdbarch, aspace, pc);
177321bd
DJ
2781 cond_negated = (itstate >> 4) & 1;
2782
2783 /* Skip all following instructions with the same
2784 condition. If there is a later instruction in the IT
2785 block with the opposite condition, set the other
2786 breakpoint there. If not, then set a breakpoint on
2787 the instruction after the IT block. */
2788 do
2789 {
2790 inst1 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
2791 pc += thumb_insn_size (inst1);
2792 itstate = thumb_advance_itstate (itstate);
2793 }
2794 while (itstate != 0 && ((itstate >> 4) & 1) == cond_negated);
2795
50e98be4 2796 return MAKE_THUMB_ADDR (pc);
177321bd
DJ
2797 }
2798 }
2799 }
2800 else if (itstate & 0x0f)
9dca5578
DJ
2801 {
2802 /* We are in a conditional block. Check the condition. */
177321bd 2803 int cond = itstate >> 4;
9dca5578
DJ
2804
2805 if (! condition_true (cond, status))
2806 {
2807 /* Advance to the next instruction. All the 32-bit
2808 instructions share a common prefix. */
2809 if ((inst1 & 0xe000) == 0xe000 && (inst1 & 0x1800) != 0)
50e98be4 2810 return MAKE_THUMB_ADDR (pc + 4);
9dca5578 2811 else
50e98be4 2812 return MAKE_THUMB_ADDR (pc + 2);
9dca5578 2813 }
177321bd
DJ
2814
2815 /* Otherwise, handle the instruction normally. */
9dca5578
DJ
2816 }
2817
c906108c
SS
2818 if ((inst1 & 0xff00) == 0xbd00) /* pop {rlist, pc} */
2819 {
2820 CORE_ADDR sp;
2821
2822 /* Fetch the saved PC from the stack. It's stored above
2823 all of the other registers. */
f0c9063c 2824 offset = bitcount (bits (inst1, 0, 7)) * INT_REGISTER_SIZE;
0b1b3e42 2825 sp = get_frame_register_unsigned (frame, ARM_SP_REGNUM);
e17a4113 2826 nextpc = read_memory_unsigned_integer (sp + offset, 4, byte_order);
c906108c
SS
2827 }
2828 else if ((inst1 & 0xf000) == 0xd000) /* conditional branch */
2829 {
c5aa993b 2830 unsigned long cond = bits (inst1, 8, 11);
25b41d01
YQ
2831 if (cond == 0x0f) /* 0x0f = SWI */
2832 {
2833 struct gdbarch_tdep *tdep;
2834 tdep = gdbarch_tdep (gdbarch);
2835
2836 if (tdep->syscall_next_pc != NULL)
2837 nextpc = tdep->syscall_next_pc (frame);
2838
2839 }
2840 else if (cond != 0x0f && condition_true (cond, status))
c906108c
SS
2841 nextpc = pc_val + (sbits (inst1, 0, 7) << 1);
2842 }
2843 else if ((inst1 & 0xf800) == 0xe000) /* unconditional branch */
2844 {
2845 nextpc = pc_val + (sbits (inst1, 0, 10) << 1);
2846 }
9dca5578 2847 else if ((inst1 & 0xe000) == 0xe000) /* 32-bit instruction */
c906108c 2848 {
e17a4113
UW
2849 unsigned short inst2;
2850 inst2 = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);
9dca5578
DJ
2851
2852 /* Default to the next instruction. */
2853 nextpc = pc + 4;
50e98be4 2854 nextpc = MAKE_THUMB_ADDR (nextpc);
9dca5578
DJ
2855
2856 if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
2857 {
2858 /* Branches and miscellaneous control instructions. */
2859
2860 if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
2861 {
2862 /* B, BL, BLX. */
2863 int j1, j2, imm1, imm2;
2864
2865 imm1 = sbits (inst1, 0, 10);
2866 imm2 = bits (inst2, 0, 10);
2867 j1 = bit (inst2, 13);
2868 j2 = bit (inst2, 11);
2869
2870 offset = ((imm1 << 12) + (imm2 << 1));
2871 offset ^= ((!j2) << 22) | ((!j1) << 23);
2872
2873 nextpc = pc_val + offset;
2874 /* For BLX make sure to clear the low bits. */
2875 if (bit (inst2, 12) == 0)
2876 nextpc = nextpc & 0xfffffffc;
2877 }
2878 else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
2879 {
2880 /* SUBS PC, LR, #imm8. */
2881 nextpc = get_frame_register_unsigned (frame, ARM_LR_REGNUM);
2882 nextpc -= inst2 & 0x00ff;
2883 }
4069ebbe 2884 else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
9dca5578
DJ
2885 {
2886 /* Conditional branch. */
2887 if (condition_true (bits (inst1, 6, 9), status))
2888 {
2889 int sign, j1, j2, imm1, imm2;
2890
2891 sign = sbits (inst1, 10, 10);
2892 imm1 = bits (inst1, 0, 5);
2893 imm2 = bits (inst2, 0, 10);
2894 j1 = bit (inst2, 13);
2895 j2 = bit (inst2, 11);
2896
2897 offset = (sign << 20) + (j2 << 19) + (j1 << 18);
2898 offset += (imm1 << 12) + (imm2 << 1);
2899
2900 nextpc = pc_val + offset;
2901 }
2902 }
2903 }
2904 else if ((inst1 & 0xfe50) == 0xe810)
2905 {
2906 /* Load multiple or RFE. */
2907 int rn, offset, load_pc = 1;
2908
2909 rn = bits (inst1, 0, 3);
2910 if (bit (inst1, 7) && !bit (inst1, 8))
2911 {
2912 /* LDMIA or POP */
2913 if (!bit (inst2, 15))
2914 load_pc = 0;
2915 offset = bitcount (inst2) * 4 - 4;
2916 }
2917 else if (!bit (inst1, 7) && bit (inst1, 8))
2918 {
2919 /* LDMDB */
2920 if (!bit (inst2, 15))
2921 load_pc = 0;
2922 offset = -4;
2923 }
2924 else if (bit (inst1, 7) && bit (inst1, 8))
2925 {
2926 /* RFEIA */
2927 offset = 0;
2928 }
2929 else if (!bit (inst1, 7) && !bit (inst1, 8))
2930 {
2931 /* RFEDB */
2932 offset = -8;
2933 }
2934 else
2935 load_pc = 0;
2936
2937 if (load_pc)
2938 {
2939 CORE_ADDR addr = get_frame_register_unsigned (frame, rn);
2940 nextpc = get_frame_memory_unsigned (frame, addr + offset, 4);
2941 }
2942 }
2943 else if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
2944 {
2945 /* MOV PC or MOVS PC. */
2946 nextpc = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
50e98be4 2947 nextpc = MAKE_THUMB_ADDR (nextpc);
9dca5578
DJ
2948 }
2949 else if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
2950 {
2951 /* LDR PC. */
2952 CORE_ADDR base;
2953 int rn, load_pc = 1;
2954
2955 rn = bits (inst1, 0, 3);
2956 base = get_frame_register_unsigned (frame, rn);
2957 if (rn == 15)
2958 {
2959 base = (base + 4) & ~(CORE_ADDR) 0x3;
2960 if (bit (inst1, 7))
2961 base += bits (inst2, 0, 11);
2962 else
2963 base -= bits (inst2, 0, 11);
2964 }
2965 else if (bit (inst1, 7))
2966 base += bits (inst2, 0, 11);
2967 else if (bit (inst2, 11))
2968 {
2969 if (bit (inst2, 10))
2970 {
2971 if (bit (inst2, 9))
2972 base += bits (inst2, 0, 7);
2973 else
2974 base -= bits (inst2, 0, 7);
2975 }
2976 }
2977 else if ((inst2 & 0x0fc0) == 0x0000)
2978 {
2979 int shift = bits (inst2, 4, 5), rm = bits (inst2, 0, 3);
2980 base += get_frame_register_unsigned (frame, rm) << shift;
2981 }
2982 else
2983 /* Reserved. */
2984 load_pc = 0;
2985
2986 if (load_pc)
2987 nextpc = get_frame_memory_unsigned (frame, base, 4);
2988 }
2989 else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
2990 {
2991 /* TBB. */
d476da0e
RE
2992 CORE_ADDR tbl_reg, table, offset, length;
2993
2994 tbl_reg = bits (inst1, 0, 3);
2995 if (tbl_reg == 0x0f)
2996 table = pc + 4; /* Regcache copy of PC isn't right yet. */
2997 else
2998 table = get_frame_register_unsigned (frame, tbl_reg);
9dca5578 2999
9dca5578
DJ
3000 offset = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
3001 length = 2 * get_frame_memory_unsigned (frame, table + offset, 1);
3002 nextpc = pc_val + length;
3003 }
d476da0e 3004 else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
9dca5578
DJ
3005 {
3006 /* TBH. */
d476da0e
RE
3007 CORE_ADDR tbl_reg, table, offset, length;
3008
3009 tbl_reg = bits (inst1, 0, 3);
3010 if (tbl_reg == 0x0f)
3011 table = pc + 4; /* Regcache copy of PC isn't right yet. */
3012 else
3013 table = get_frame_register_unsigned (frame, tbl_reg);
9dca5578 3014
9dca5578
DJ
3015 offset = 2 * get_frame_register_unsigned (frame, bits (inst2, 0, 3));
3016 length = 2 * get_frame_memory_unsigned (frame, table + offset, 2);
3017 nextpc = pc_val + length;
3018 }
c906108c 3019 }
aa17d93e 3020 else if ((inst1 & 0xff00) == 0x4700) /* bx REG, blx REG */
9498281f
DJ
3021 {
3022 if (bits (inst1, 3, 6) == 0x0f)
3023 nextpc = pc_val;
3024 else
0b1b3e42 3025 nextpc = get_frame_register_unsigned (frame, bits (inst1, 3, 6));
9498281f 3026 }
9dca5578
DJ
3027 else if ((inst1 & 0xf500) == 0xb100)
3028 {
3029 /* CBNZ or CBZ. */
3030 int imm = (bit (inst1, 9) << 6) + (bits (inst1, 3, 7) << 1);
3031 ULONGEST reg = get_frame_register_unsigned (frame, bits (inst1, 0, 2));
3032
3033 if (bit (inst1, 11) && reg != 0)
3034 nextpc = pc_val + imm;
3035 else if (!bit (inst1, 11) && reg == 0)
3036 nextpc = pc_val + imm;
3037 }
c906108c
SS
3038 return nextpc;
3039}
3040
50e98be4
DJ
3041/* Get the raw next address. PC is the current program counter, in
3042 FRAME. INSERT_BKPT should be TRUE if we want a breakpoint set on
3043 the alternative next instruction if there are two options.
3044
3045 The value returned has the execution state of the next instruction
3046 encoded in it. Use IS_THUMB_ADDR () to see whether the instruction is
3047 in Thumb-State, and gdbarch_addr_bits_remove () to get the plain memory
3048 address.
3049*/
3050static CORE_ADDR
3051arm_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc, int insert_bkpt)
c906108c 3052{
2af46ca0 3053 struct gdbarch *gdbarch = get_frame_arch (frame);
e17a4113
UW
3054 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3055 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
c906108c
SS
3056 unsigned long pc_val;
3057 unsigned long this_instr;
3058 unsigned long status;
3059 CORE_ADDR nextpc;
3060
b39cc962 3061 if (arm_frame_is_thumb (frame))
50e98be4 3062 return thumb_get_next_pc_raw (frame, pc, insert_bkpt);
c906108c
SS
3063
3064 pc_val = (unsigned long) pc;
e17a4113 3065 this_instr = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
9d4fde75 3066
0b1b3e42 3067 status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
c5aa993b 3068 nextpc = (CORE_ADDR) (pc_val + 4); /* Default case */
c906108c 3069
daddc3c1
DJ
3070 if (bits (this_instr, 28, 31) == INST_NV)
3071 switch (bits (this_instr, 24, 27))
3072 {
3073 case 0xa:
3074 case 0xb:
3075 {
3076 /* Branch with Link and change to Thumb. */
3077 nextpc = BranchDest (pc, this_instr);
3078 nextpc |= bit (this_instr, 24) << 1;
50e98be4 3079 nextpc = MAKE_THUMB_ADDR (nextpc);
daddc3c1
DJ
3080 break;
3081 }
3082 case 0xc:
3083 case 0xd:
3084 case 0xe:
3085 /* Coprocessor register transfer. */
3086 if (bits (this_instr, 12, 15) == 15)
3087 error (_("Invalid update to pc in instruction"));
3088 break;
3089 }
3090 else if (condition_true (bits (this_instr, 28, 31), status))
c906108c
SS
3091 {
3092 switch (bits (this_instr, 24, 27))
3093 {
c5aa993b 3094 case 0x0:
94c30b78 3095 case 0x1: /* data processing */
c5aa993b
JM
3096 case 0x2:
3097 case 0x3:
c906108c
SS
3098 {
3099 unsigned long operand1, operand2, result = 0;
3100 unsigned long rn;
3101 int c;
c5aa993b 3102
c906108c
SS
3103 if (bits (this_instr, 12, 15) != 15)
3104 break;
3105
3106 if (bits (this_instr, 22, 25) == 0
c5aa993b 3107 && bits (this_instr, 4, 7) == 9) /* multiply */
edefbb7c 3108 error (_("Invalid update to pc in instruction"));
c906108c 3109
9498281f 3110 /* BX <reg>, BLX <reg> */
e150acc7
PB
3111 if (bits (this_instr, 4, 27) == 0x12fff1
3112 || bits (this_instr, 4, 27) == 0x12fff3)
9498281f
DJ
3113 {
3114 rn = bits (this_instr, 0, 3);
50e98be4 3115 nextpc = (rn == 15) ? pc_val + 8
0b1b3e42 3116 : get_frame_register_unsigned (frame, rn);
9498281f
DJ
3117 return nextpc;
3118 }
3119
c906108c
SS
3120 /* Multiply into PC */
3121 c = (status & FLAG_C) ? 1 : 0;
3122 rn = bits (this_instr, 16, 19);
0b1b3e42
UW
3123 operand1 = (rn == 15) ? pc_val + 8
3124 : get_frame_register_unsigned (frame, rn);
c5aa993b 3125
c906108c
SS
3126 if (bit (this_instr, 25))
3127 {
3128 unsigned long immval = bits (this_instr, 0, 7);
3129 unsigned long rotate = 2 * bits (this_instr, 8, 11);
c5aa993b
JM
3130 operand2 = ((immval >> rotate) | (immval << (32 - rotate)))
3131 & 0xffffffff;
c906108c 3132 }
c5aa993b 3133 else /* operand 2 is a shifted register */
0b1b3e42 3134 operand2 = shifted_reg_val (frame, this_instr, c, pc_val, status);
c5aa993b 3135
c906108c
SS
3136 switch (bits (this_instr, 21, 24))
3137 {
c5aa993b 3138 case 0x0: /*and */
c906108c
SS
3139 result = operand1 & operand2;
3140 break;
3141
c5aa993b 3142 case 0x1: /*eor */
c906108c
SS
3143 result = operand1 ^ operand2;
3144 break;
3145
c5aa993b 3146 case 0x2: /*sub */
c906108c
SS
3147 result = operand1 - operand2;
3148 break;
3149
c5aa993b 3150 case 0x3: /*rsb */
c906108c
SS
3151 result = operand2 - operand1;
3152 break;
3153
c5aa993b 3154 case 0x4: /*add */
c906108c
SS
3155 result = operand1 + operand2;
3156 break;
3157
c5aa993b 3158 case 0x5: /*adc */
c906108c
SS
3159 result = operand1 + operand2 + c;
3160 break;
3161
c5aa993b 3162 case 0x6: /*sbc */
c906108c
SS
3163 result = operand1 - operand2 + c;
3164 break;
3165
c5aa993b 3166 case 0x7: /*rsc */
c906108c
SS
3167 result = operand2 - operand1 + c;
3168 break;
3169
c5aa993b
JM
3170 case 0x8:
3171 case 0x9:
3172 case 0xa:
3173 case 0xb: /* tst, teq, cmp, cmn */
c906108c
SS
3174 result = (unsigned long) nextpc;
3175 break;
3176
c5aa993b 3177 case 0xc: /*orr */
c906108c
SS
3178 result = operand1 | operand2;
3179 break;
3180
c5aa993b 3181 case 0xd: /*mov */
c906108c
SS
3182 /* Always step into a function. */
3183 result = operand2;
c5aa993b 3184 break;
c906108c 3185
c5aa993b 3186 case 0xe: /*bic */
c906108c
SS
3187 result = operand1 & ~operand2;
3188 break;
3189
c5aa993b 3190 case 0xf: /*mvn */
c906108c
SS
3191 result = ~operand2;
3192 break;
3193 }
c906108c 3194
50e98be4
DJ
3195 /* In 26-bit APCS the bottom two bits of the result are
3196 ignored, and we always end up in ARM state. */
3197 if (!arm_apcs_32)
3198 nextpc = arm_addr_bits_remove (gdbarch, result);
3199 else
3200 nextpc = result;
3201
c906108c
SS
3202 break;
3203 }
c5aa993b
JM
3204
3205 case 0x4:
3206 case 0x5: /* data transfer */
3207 case 0x6:
3208 case 0x7:
c906108c
SS
3209 if (bit (this_instr, 20))
3210 {
3211 /* load */
3212 if (bits (this_instr, 12, 15) == 15)
3213 {
3214 /* rd == pc */
c5aa993b 3215 unsigned long rn;
c906108c 3216 unsigned long base;
c5aa993b 3217
c906108c 3218 if (bit (this_instr, 22))
edefbb7c 3219 error (_("Invalid update to pc in instruction"));
c906108c
SS
3220
3221 /* byte write to PC */
3222 rn = bits (this_instr, 16, 19);
0b1b3e42
UW
3223 base = (rn == 15) ? pc_val + 8
3224 : get_frame_register_unsigned (frame, rn);
c906108c
SS
3225 if (bit (this_instr, 24))
3226 {
3227 /* pre-indexed */
3228 int c = (status & FLAG_C) ? 1 : 0;
3229 unsigned long offset =
c5aa993b 3230 (bit (this_instr, 25)
0b1b3e42 3231 ? shifted_reg_val (frame, this_instr, c, pc_val, status)
c5aa993b 3232 : bits (this_instr, 0, 11));
c906108c
SS
3233
3234 if (bit (this_instr, 23))
3235 base += offset;
3236 else
3237 base -= offset;
3238 }
c5aa993b 3239 nextpc = (CORE_ADDR) read_memory_integer ((CORE_ADDR) base,
e17a4113 3240 4, byte_order);
c906108c
SS
3241 }
3242 }
3243 break;
c5aa993b
JM
3244
3245 case 0x8:
3246 case 0x9: /* block transfer */
c906108c
SS
3247 if (bit (this_instr, 20))
3248 {
3249 /* LDM */
3250 if (bit (this_instr, 15))
3251 {
3252 /* loading pc */
3253 int offset = 0;
3254
3255 if (bit (this_instr, 23))
3256 {
3257 /* up */
3258 unsigned long reglist = bits (this_instr, 0, 14);
3259 offset = bitcount (reglist) * 4;
c5aa993b 3260 if (bit (this_instr, 24)) /* pre */
c906108c
SS
3261 offset += 4;
3262 }
3263 else if (bit (this_instr, 24))
3264 offset = -4;
c5aa993b 3265
c906108c 3266 {
c5aa993b 3267 unsigned long rn_val =
0b1b3e42
UW
3268 get_frame_register_unsigned (frame,
3269 bits (this_instr, 16, 19));
c906108c
SS
3270 nextpc =
3271 (CORE_ADDR) read_memory_integer ((CORE_ADDR) (rn_val
c5aa993b 3272 + offset),
e17a4113 3273 4, byte_order);
c906108c 3274 }
c906108c
SS
3275 }
3276 }
3277 break;
c5aa993b
JM
3278
3279 case 0xb: /* branch & link */
3280 case 0xa: /* branch */
c906108c
SS
3281 {
3282 nextpc = BranchDest (pc, this_instr);
c906108c
SS
3283 break;
3284 }
c5aa993b
JM
3285
3286 case 0xc:
3287 case 0xd:
3288 case 0xe: /* coproc ops */
25b41d01 3289 break;
c5aa993b 3290 case 0xf: /* SWI */
25b41d01
YQ
3291 {
3292 struct gdbarch_tdep *tdep;
3293 tdep = gdbarch_tdep (gdbarch);
3294
3295 if (tdep->syscall_next_pc != NULL)
3296 nextpc = tdep->syscall_next_pc (frame);
3297
3298 }
c906108c
SS
3299 break;
3300
3301 default:
edefbb7c 3302 fprintf_filtered (gdb_stderr, _("Bad bit-field extraction\n"));
c906108c
SS
3303 return (pc);
3304 }
3305 }
3306
3307 return nextpc;
3308}
3309
50e98be4
DJ
3310CORE_ADDR
3311arm_get_next_pc (struct frame_info *frame, CORE_ADDR pc)
3312{
3313 struct gdbarch *gdbarch = get_frame_arch (frame);
3314 CORE_ADDR nextpc =
3315 gdbarch_addr_bits_remove (gdbarch,
3316 arm_get_next_pc_raw (frame, pc, TRUE));
3317 if (nextpc == pc)
3318 error (_("Infinite loop detected"));
3319 return nextpc;
3320}
3321
9512d7fd
FN
3322/* single_step() is called just before we want to resume the inferior,
3323 if we want to single-step it but there is no hardware or kernel
3324 single-step support. We find the target of the coming instruction
e0cd558a 3325 and breakpoint it. */
9512d7fd 3326
190dce09 3327int
0b1b3e42 3328arm_software_single_step (struct frame_info *frame)
9512d7fd 3329{
a6d9a66e 3330 struct gdbarch *gdbarch = get_frame_arch (frame);
6c95b8df 3331 struct address_space *aspace = get_frame_address_space (frame);
a6d9a66e 3332
8181d85f
DJ
3333 /* NOTE: This may insert the wrong breakpoint instruction when
3334 single-stepping over a mode-changing instruction, if the
3335 CPSR heuristics are used. */
9512d7fd 3336
0b1b3e42 3337 CORE_ADDR next_pc = arm_get_next_pc (frame, get_frame_pc (frame));
6c95b8df 3338 insert_single_step_breakpoint (gdbarch, aspace, next_pc);
e6590a1b
UW
3339
3340 return 1;
9512d7fd 3341}
9512d7fd 3342
f9d67f43
DJ
3343/* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
3344 the buffer to be NEW_LEN bytes ending at ENDADDR. Return
3345 NULL if an error occurs. BUF is freed. */
3346
3347static gdb_byte *
3348extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
3349 int old_len, int new_len)
3350{
3351 gdb_byte *new_buf, *middle;
3352 int bytes_to_read = new_len - old_len;
3353
3354 new_buf = xmalloc (new_len);
3355 memcpy (new_buf + bytes_to_read, buf, old_len);
3356 xfree (buf);
3357 if (target_read_memory (endaddr - new_len, new_buf, bytes_to_read) != 0)
3358 {
3359 xfree (new_buf);
3360 return NULL;
3361 }
3362 return new_buf;
3363}
3364
3365/* An IT block is at most the 2-byte IT instruction followed by
3366 four 4-byte instructions. The furthest back we must search to
3367 find an IT block that affects the current instruction is thus
3368 2 + 3 * 4 == 14 bytes. */
3369#define MAX_IT_BLOCK_PREFIX 14
3370
3371/* Use a quick scan if there are more than this many bytes of
3372 code. */
3373#define IT_SCAN_THRESHOLD 32
3374
3375/* Adjust a breakpoint's address to move breakpoints out of IT blocks.
3376 A breakpoint in an IT block may not be hit, depending on the
3377 condition flags. */
3378static CORE_ADDR
3379arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
3380{
3381 gdb_byte *buf;
3382 char map_type;
3383 CORE_ADDR boundary, func_start;
3384 int buf_len, buf2_len;
3385 enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
3386 int i, any, last_it, last_it_count;
3387
3388 /* If we are using BKPT breakpoints, none of this is necessary. */
3389 if (gdbarch_tdep (gdbarch)->thumb2_breakpoint == NULL)
3390 return bpaddr;
3391
3392 /* ARM mode does not have this problem. */
9779414d 3393 if (!arm_pc_is_thumb (gdbarch, bpaddr))
f9d67f43
DJ
3394 return bpaddr;
3395
3396 /* We are setting a breakpoint in Thumb code that could potentially
3397 contain an IT block. The first step is to find how much Thumb
3398 code there is; we do not need to read outside of known Thumb
3399 sequences. */
3400 map_type = arm_find_mapping_symbol (bpaddr, &boundary);
3401 if (map_type == 0)
3402 /* Thumb-2 code must have mapping symbols to have a chance. */
3403 return bpaddr;
3404
3405 bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
3406
3407 if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
3408 && func_start > boundary)
3409 boundary = func_start;
3410
3411 /* Search for a candidate IT instruction. We have to do some fancy
3412 footwork to distinguish a real IT instruction from the second
3413 half of a 32-bit instruction, but there is no need for that if
3414 there's no candidate. */
3415 buf_len = min (bpaddr - boundary, MAX_IT_BLOCK_PREFIX);
3416 if (buf_len == 0)
3417 /* No room for an IT instruction. */
3418 return bpaddr;
3419
3420 buf = xmalloc (buf_len);
3421 if (target_read_memory (bpaddr - buf_len, buf, buf_len) != 0)
3422 return bpaddr;
3423 any = 0;
3424 for (i = 0; i < buf_len; i += 2)
3425 {
3426 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
3427 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
3428 {
3429 any = 1;
3430 break;
3431 }
3432 }
3433 if (any == 0)
3434 {
3435 xfree (buf);
3436 return bpaddr;
3437 }
3438
3439 /* OK, the code bytes before this instruction contain at least one
3440 halfword which resembles an IT instruction. We know that it's
3441 Thumb code, but there are still two possibilities. Either the
3442 halfword really is an IT instruction, or it is the second half of
3443 a 32-bit Thumb instruction. The only way we can tell is to
3444 scan forwards from a known instruction boundary. */
3445 if (bpaddr - boundary > IT_SCAN_THRESHOLD)
3446 {
3447 int definite;
3448
3449 /* There's a lot of code before this instruction. Start with an
3450 optimistic search; it's easy to recognize halfwords that can
3451 not be the start of a 32-bit instruction, and use that to
3452 lock on to the instruction boundaries. */
3453 buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
3454 if (buf == NULL)
3455 return bpaddr;
3456 buf_len = IT_SCAN_THRESHOLD;
3457
3458 definite = 0;
3459 for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
3460 {
3461 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
3462 if (thumb_insn_size (inst1) == 2)
3463 {
3464 definite = 1;
3465 break;
3466 }
3467 }
3468
3469 /* At this point, if DEFINITE, BUF[I] is the first place we
3470 are sure that we know the instruction boundaries, and it is far
3471 enough from BPADDR that we could not miss an IT instruction
3472 affecting BPADDR. If ! DEFINITE, give up - start from a
3473 known boundary. */
3474 if (! definite)
3475 {
3476 buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
3477 if (buf == NULL)
3478 return bpaddr;
3479 buf_len = bpaddr - boundary;
3480 i = 0;
3481 }
3482 }
3483 else
3484 {
3485 buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
3486 if (buf == NULL)
3487 return bpaddr;
3488 buf_len = bpaddr - boundary;
3489 i = 0;
3490 }
3491
3492 /* Scan forwards. Find the last IT instruction before BPADDR. */
3493 last_it = -1;
3494 last_it_count = 0;
3495 while (i < buf_len)
3496 {
3497 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
3498 last_it_count--;
3499 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
3500 {
3501 last_it = i;
3502 if (inst1 & 0x0001)
3503 last_it_count = 4;
3504 else if (inst1 & 0x0002)
3505 last_it_count = 3;
3506 else if (inst1 & 0x0004)
3507 last_it_count = 2;
3508 else
3509 last_it_count = 1;
3510 }
3511 i += thumb_insn_size (inst1);
3512 }
3513
3514 xfree (buf);
3515
3516 if (last_it == -1)
3517 /* There wasn't really an IT instruction after all. */
3518 return bpaddr;
3519
3520 if (last_it_count < 1)
3521 /* It was too far away. */
3522 return bpaddr;
3523
3524 /* This really is a trouble spot. Move the breakpoint to the IT
3525 instruction. */
3526 return bpaddr - buf_len + last_it;
3527}
3528
cca44b1b 3529/* ARM displaced stepping support.
c906108c 3530
cca44b1b 3531 Generally ARM displaced stepping works as follows:
c906108c 3532
cca44b1b
JB
3533 1. When an instruction is to be single-stepped, it is first decoded by
3534 arm_process_displaced_insn (called from arm_displaced_step_copy_insn).
3535 Depending on the type of instruction, it is then copied to a scratch
3536 location, possibly in a modified form. The copy_* set of functions
3537 performs such modification, as necessary. A breakpoint is placed after
3538 the modified instruction in the scratch space to return control to GDB.
3539 Note in particular that instructions which modify the PC will no longer
3540 do so after modification.
c5aa993b 3541
cca44b1b
JB
3542 2. The instruction is single-stepped, by setting the PC to the scratch
3543 location address, and resuming. Control returns to GDB when the
3544 breakpoint is hit.
c5aa993b 3545
cca44b1b
JB
3546 3. A cleanup function (cleanup_*) is called corresponding to the copy_*
3547 function used for the current instruction. This function's job is to
3548 put the CPU/memory state back to what it would have been if the
3549 instruction had been executed unmodified in its original location. */
c5aa993b 3550
cca44b1b
JB
3551/* NOP instruction (mov r0, r0). */
3552#define ARM_NOP 0xe1a00000
3553
3554/* Helper for register reads for displaced stepping. In particular, this
3555 returns the PC as it would be seen by the instruction at its original
3556 location. */
3557
3558ULONGEST
3559displaced_read_reg (struct regcache *regs, CORE_ADDR from, int regno)
3560{
3561 ULONGEST ret;
3562
3563 if (regno == 15)
3564 {
3565 if (debug_displaced)
3566 fprintf_unfiltered (gdb_stdlog, "displaced: read pc value %.8lx\n",
3567 (unsigned long) from + 8);
3568 return (ULONGEST) from + 8; /* Pipeline offset. */
3569 }
c906108c 3570 else
cca44b1b
JB
3571 {
3572 regcache_cooked_read_unsigned (regs, regno, &ret);
3573 if (debug_displaced)
3574 fprintf_unfiltered (gdb_stdlog, "displaced: read r%d value %.8lx\n",
3575 regno, (unsigned long) ret);
3576 return ret;
3577 }
c906108c
SS
3578}
3579
cca44b1b
JB
3580static int
3581displaced_in_arm_mode (struct regcache *regs)
3582{
3583 ULONGEST ps;
9779414d 3584 ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
66e810cd 3585
cca44b1b 3586 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
66e810cd 3587
9779414d 3588 return (ps & t_bit) == 0;
cca44b1b 3589}
66e810cd 3590
cca44b1b 3591/* Write to the PC as from a branch instruction. */
c906108c 3592
cca44b1b
JB
3593static void
3594branch_write_pc (struct regcache *regs, ULONGEST val)
c906108c 3595{
cca44b1b
JB
3596 if (displaced_in_arm_mode (regs))
3597 /* Note: If bits 0/1 are set, this branch would be unpredictable for
3598 architecture versions < 6. */
3599 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & ~(ULONGEST) 0x3);
3600 else
3601 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & ~(ULONGEST) 0x1);
3602}
66e810cd 3603
cca44b1b
JB
3604/* Write to the PC as from a branch-exchange instruction. */
3605
3606static void
3607bx_write_pc (struct regcache *regs, ULONGEST val)
3608{
3609 ULONGEST ps;
9779414d 3610 ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
cca44b1b
JB
3611
3612 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
3613
3614 if ((val & 1) == 1)
c906108c 3615 {
9779414d 3616 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
cca44b1b
JB
3617 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
3618 }
3619 else if ((val & 2) == 0)
3620 {
9779414d 3621 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 3622 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
c906108c
SS
3623 }
3624 else
3625 {
cca44b1b
JB
3626 /* Unpredictable behaviour. Try to do something sensible (switch to ARM
3627 mode, align dest to 4 bytes). */
3628 warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
9779414d 3629 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 3630 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
c906108c
SS
3631 }
3632}
ed9a39eb 3633
cca44b1b 3634/* Write to the PC as if from a load instruction. */
ed9a39eb 3635
34e8f22d 3636static void
cca44b1b 3637load_write_pc (struct regcache *regs, ULONGEST val)
ed9a39eb 3638{
cca44b1b
JB
3639 if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
3640 bx_write_pc (regs, val);
3641 else
3642 branch_write_pc (regs, val);
3643}
be8626e0 3644
cca44b1b
JB
3645/* Write to the PC as if from an ALU instruction. */
3646
3647static void
3648alu_write_pc (struct regcache *regs, ULONGEST val)
3649{
3650 if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && displaced_in_arm_mode (regs))
3651 bx_write_pc (regs, val);
3652 else
3653 branch_write_pc (regs, val);
3654}
3655
3656/* Helper for writing to registers for displaced stepping. Writing to the PC
3657 has a varying effects depending on the instruction which does the write:
3658 this is controlled by the WRITE_PC argument. */
3659
3660void
3661displaced_write_reg (struct regcache *regs, struct displaced_step_closure *dsc,
3662 int regno, ULONGEST val, enum pc_write_style write_pc)
3663{
3664 if (regno == 15)
08216dd7 3665 {
cca44b1b
JB
3666 if (debug_displaced)
3667 fprintf_unfiltered (gdb_stdlog, "displaced: writing pc %.8lx\n",
3668 (unsigned long) val);
3669 switch (write_pc)
08216dd7 3670 {
cca44b1b
JB
3671 case BRANCH_WRITE_PC:
3672 branch_write_pc (regs, val);
08216dd7
RE
3673 break;
3674
cca44b1b
JB
3675 case BX_WRITE_PC:
3676 bx_write_pc (regs, val);
3677 break;
3678
3679 case LOAD_WRITE_PC:
3680 load_write_pc (regs, val);
3681 break;
3682
3683 case ALU_WRITE_PC:
3684 alu_write_pc (regs, val);
3685 break;
3686
3687 case CANNOT_WRITE_PC:
3688 warning (_("Instruction wrote to PC in an unexpected way when "
3689 "single-stepping"));
08216dd7
RE
3690 break;
3691
3692 default:
97b9747c
JB
3693 internal_error (__FILE__, __LINE__,
3694 _("Invalid argument to displaced_write_reg"));
08216dd7 3695 }
b508a996 3696
cca44b1b 3697 dsc->wrote_to_pc = 1;
b508a996 3698 }
ed9a39eb 3699 else
b508a996 3700 {
cca44b1b
JB
3701 if (debug_displaced)
3702 fprintf_unfiltered (gdb_stdlog, "displaced: writing r%d value %.8lx\n",
3703 regno, (unsigned long) val);
3704 regcache_cooked_write_unsigned (regs, regno, val);
b508a996 3705 }
34e8f22d
RE
3706}
3707
cca44b1b
JB
3708/* This function is used to concisely determine if an instruction INSN
3709 references PC. Register fields of interest in INSN should have the
3710 corresponding fields of BITMASK set to 0b1111. The function returns return 1
3711 if any of these fields in INSN reference the PC (also 0b1111, r15), else it
3712 returns 0. */
67255d04
RE
3713
3714static int
cca44b1b 3715insn_references_pc (uint32_t insn, uint32_t bitmask)
67255d04 3716{
cca44b1b 3717 uint32_t lowbit = 1;
67255d04 3718
cca44b1b
JB
3719 while (bitmask != 0)
3720 {
3721 uint32_t mask;
44e1a9eb 3722
cca44b1b
JB
3723 for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
3724 ;
67255d04 3725
cca44b1b
JB
3726 if (!lowbit)
3727 break;
67255d04 3728
cca44b1b 3729 mask = lowbit * 0xf;
67255d04 3730
cca44b1b
JB
3731 if ((insn & mask) == mask)
3732 return 1;
3733
3734 bitmask &= ~mask;
67255d04
RE
3735 }
3736
cca44b1b
JB
3737 return 0;
3738}
2af48f68 3739
cca44b1b
JB
3740/* The simplest copy function. Many instructions have the same effect no
3741 matter what address they are executed at: in those cases, use this. */
67255d04 3742
cca44b1b 3743static int
6e39997a 3744copy_unmodified (struct gdbarch *gdbarch, uint32_t insn,
cca44b1b
JB
3745 const char *iname, struct displaced_step_closure *dsc)
3746{
3747 if (debug_displaced)
3748 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx, "
3749 "opcode/class '%s' unmodified\n", (unsigned long) insn,
3750 iname);
67255d04 3751
cca44b1b 3752 dsc->modinsn[0] = insn;
67255d04 3753
cca44b1b
JB
3754 return 0;
3755}
3756
3757/* Preload instructions with immediate offset. */
3758
3759static void
6e39997a 3760cleanup_preload (struct gdbarch *gdbarch,
cca44b1b
JB
3761 struct regcache *regs, struct displaced_step_closure *dsc)
3762{
3763 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
3764 if (!dsc->u.preload.immed)
3765 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
3766}
3767
3768static int
3769copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
3770 struct displaced_step_closure *dsc)
3771{
3772 unsigned int rn = bits (insn, 16, 19);
3773 ULONGEST rn_val;
3774 CORE_ADDR from = dsc->insn_addr;
3775
3776 if (!insn_references_pc (insn, 0x000f0000ul))
3777 return copy_unmodified (gdbarch, insn, "preload", dsc);
3778
3779 if (debug_displaced)
3780 fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
3781 (unsigned long) insn);
3782
3783 /* Preload instructions:
3784
3785 {pli/pld} [rn, #+/-imm]
3786 ->
3787 {pli/pld} [r0, #+/-imm]. */
3788
3789 dsc->tmp[0] = displaced_read_reg (regs, from, 0);
3790 rn_val = displaced_read_reg (regs, from, rn);
3791 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
3792
3793 dsc->u.preload.immed = 1;
3794
3795 dsc->modinsn[0] = insn & 0xfff0ffff;
3796
3797 dsc->cleanup = &cleanup_preload;
3798
3799 return 0;
3800}
3801
3802/* Preload instructions with register offset. */
3803
3804static int
3805copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
3806 struct displaced_step_closure *dsc)
3807{
3808 unsigned int rn = bits (insn, 16, 19);
3809 unsigned int rm = bits (insn, 0, 3);
3810 ULONGEST rn_val, rm_val;
3811 CORE_ADDR from = dsc->insn_addr;
3812
3813 if (!insn_references_pc (insn, 0x000f000ful))
3814 return copy_unmodified (gdbarch, insn, "preload reg", dsc);
3815
3816 if (debug_displaced)
3817 fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
3818 (unsigned long) insn);
3819
3820 /* Preload register-offset instructions:
3821
3822 {pli/pld} [rn, rm {, shift}]
3823 ->
3824 {pli/pld} [r0, r1 {, shift}]. */
3825
3826 dsc->tmp[0] = displaced_read_reg (regs, from, 0);
3827 dsc->tmp[1] = displaced_read_reg (regs, from, 1);
3828 rn_val = displaced_read_reg (regs, from, rn);
3829 rm_val = displaced_read_reg (regs, from, rm);
3830 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
3831 displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
3832
3833 dsc->u.preload.immed = 0;
3834
3835 dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
3836
3837 dsc->cleanup = &cleanup_preload;
3838
3839 return 0;
3840}
3841
3842/* Copy/cleanup coprocessor load and store instructions. */
3843
3844static void
6e39997a 3845cleanup_copro_load_store (struct gdbarch *gdbarch,
cca44b1b
JB
3846 struct regcache *regs,
3847 struct displaced_step_closure *dsc)
3848{
3849 ULONGEST rn_val = displaced_read_reg (regs, dsc->insn_addr, 0);
3850
3851 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
3852
3853 if (dsc->u.ldst.writeback)
3854 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
3855}
3856
3857static int
3858copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
3859 struct regcache *regs,
3860 struct displaced_step_closure *dsc)
3861{
3862 unsigned int rn = bits (insn, 16, 19);
3863 ULONGEST rn_val;
3864 CORE_ADDR from = dsc->insn_addr;
3865
3866 if (!insn_references_pc (insn, 0x000f0000ul))
3867 return copy_unmodified (gdbarch, insn, "copro load/store", dsc);
3868
3869 if (debug_displaced)
3870 fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
3871 "load/store insn %.8lx\n", (unsigned long) insn);
3872
3873 /* Coprocessor load/store instructions:
3874
3875 {stc/stc2} [<Rn>, #+/-imm] (and other immediate addressing modes)
3876 ->
3877 {stc/stc2} [r0, #+/-imm].
3878
3879 ldc/ldc2 are handled identically. */
3880
3881 dsc->tmp[0] = displaced_read_reg (regs, from, 0);
3882 rn_val = displaced_read_reg (regs, from, rn);
3883 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
3884
3885 dsc->u.ldst.writeback = bit (insn, 25);
3886 dsc->u.ldst.rn = rn;
3887
3888 dsc->modinsn[0] = insn & 0xfff0ffff;
3889
3890 dsc->cleanup = &cleanup_copro_load_store;
3891
3892 return 0;
3893}
3894
3895/* Clean up branch instructions (actually perform the branch, by setting
3896 PC). */
3897
3898static void
6e39997a 3899cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
3900 struct displaced_step_closure *dsc)
3901{
3902 ULONGEST from = dsc->insn_addr;
3903 uint32_t status = displaced_read_reg (regs, from, ARM_PS_REGNUM);
3904 int branch_taken = condition_true (dsc->u.branch.cond, status);
3905 enum pc_write_style write_pc = dsc->u.branch.exchange
3906 ? BX_WRITE_PC : BRANCH_WRITE_PC;
3907
3908 if (!branch_taken)
3909 return;
3910
3911 if (dsc->u.branch.link)
3912 {
3913 ULONGEST pc = displaced_read_reg (regs, from, 15);
3914 displaced_write_reg (regs, dsc, 14, pc - 4, CANNOT_WRITE_PC);
3915 }
3916
3917 displaced_write_reg (regs, dsc, 15, dsc->u.branch.dest, write_pc);
3918}
3919
3920/* Copy B/BL/BLX instructions with immediate destinations. */
3921
3922static int
6e39997a 3923copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
cca44b1b
JB
3924 struct regcache *regs, struct displaced_step_closure *dsc)
3925{
3926 unsigned int cond = bits (insn, 28, 31);
3927 int exchange = (cond == 0xf);
3928 int link = exchange || bit (insn, 24);
3929 CORE_ADDR from = dsc->insn_addr;
3930 long offset;
3931
3932 if (debug_displaced)
3933 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s immediate insn "
3934 "%.8lx\n", (exchange) ? "blx" : (link) ? "bl" : "b",
3935 (unsigned long) insn);
3936
3937 /* Implement "BL<cond> <label>" as:
3938
3939 Preparation: cond <- instruction condition
3940 Insn: mov r0, r0 (nop)
3941 Cleanup: if (condition true) { r14 <- pc; pc <- label }.
3942
3943 B<cond> similar, but don't set r14 in cleanup. */
3944
3945 if (exchange)
3946 /* For BLX, set bit 0 of the destination. The cleanup_branch function will
3947 then arrange the switch into Thumb mode. */
3948 offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
3949 else
3950 offset = bits (insn, 0, 23) << 2;
3951
3952 if (bit (offset, 25))
3953 offset = offset | ~0x3ffffff;
3954
3955 dsc->u.branch.cond = cond;
3956 dsc->u.branch.link = link;
3957 dsc->u.branch.exchange = exchange;
3958 dsc->u.branch.dest = from + 8 + offset;
3959
3960 dsc->modinsn[0] = ARM_NOP;
3961
3962 dsc->cleanup = &cleanup_branch;
3963
3964 return 0;
3965}
3966
3967/* Copy BX/BLX with register-specified destinations. */
3968
3969static int
6e39997a 3970copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
cca44b1b
JB
3971 struct regcache *regs, struct displaced_step_closure *dsc)
3972{
3973 unsigned int cond = bits (insn, 28, 31);
3974 /* BX: x12xxx1x
3975 BLX: x12xxx3x. */
3976 int link = bit (insn, 5);
3977 unsigned int rm = bits (insn, 0, 3);
3978 CORE_ADDR from = dsc->insn_addr;
3979
3980 if (debug_displaced)
3981 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s register insn "
3982 "%.8lx\n", (link) ? "blx" : "bx", (unsigned long) insn);
3983
3984 /* Implement {BX,BLX}<cond> <reg>" as:
3985
3986 Preparation: cond <- instruction condition
3987 Insn: mov r0, r0 (nop)
3988 Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
3989
3990 Don't set r14 in cleanup for BX. */
3991
3992 dsc->u.branch.dest = displaced_read_reg (regs, from, rm);
3993
3994 dsc->u.branch.cond = cond;
3995 dsc->u.branch.link = link;
3996 dsc->u.branch.exchange = 1;
3997
3998 dsc->modinsn[0] = ARM_NOP;
3999
4000 dsc->cleanup = &cleanup_branch;
4001
4002 return 0;
4003}
4004
4005/* Copy/cleanup arithmetic/logic instruction with immediate RHS. */
4006
4007static void
6e39997a 4008cleanup_alu_imm (struct gdbarch *gdbarch,
cca44b1b
JB
4009 struct regcache *regs, struct displaced_step_closure *dsc)
4010{
4011 ULONGEST rd_val = displaced_read_reg (regs, dsc->insn_addr, 0);
4012 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
4013 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
4014 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
4015}
4016
4017static int
4018copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
4019 struct displaced_step_closure *dsc)
4020{
4021 unsigned int rn = bits (insn, 16, 19);
4022 unsigned int rd = bits (insn, 12, 15);
4023 unsigned int op = bits (insn, 21, 24);
4024 int is_mov = (op == 0xd);
4025 ULONGEST rd_val, rn_val;
4026 CORE_ADDR from = dsc->insn_addr;
4027
4028 if (!insn_references_pc (insn, 0x000ff000ul))
4029 return copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
4030
4031 if (debug_displaced)
4032 fprintf_unfiltered (gdb_stdlog, "displaced: copying immediate %s insn "
4033 "%.8lx\n", is_mov ? "move" : "ALU",
4034 (unsigned long) insn);
4035
4036 /* Instruction is of form:
4037
4038 <op><cond> rd, [rn,] #imm
4039
4040 Rewrite as:
4041
4042 Preparation: tmp1, tmp2 <- r0, r1;
4043 r0, r1 <- rd, rn
4044 Insn: <op><cond> r0, r1, #imm
4045 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
4046 */
4047
4048 dsc->tmp[0] = displaced_read_reg (regs, from, 0);
4049 dsc->tmp[1] = displaced_read_reg (regs, from, 1);
4050 rn_val = displaced_read_reg (regs, from, rn);
4051 rd_val = displaced_read_reg (regs, from, rd);
4052 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
4053 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
4054 dsc->rd = rd;
4055
4056 if (is_mov)
4057 dsc->modinsn[0] = insn & 0xfff00fff;
4058 else
4059 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
4060
4061 dsc->cleanup = &cleanup_alu_imm;
4062
4063 return 0;
4064}
4065
4066/* Copy/cleanup arithmetic/logic insns with register RHS. */
4067
4068static void
6e39997a 4069cleanup_alu_reg (struct gdbarch *gdbarch,
cca44b1b
JB
4070 struct regcache *regs, struct displaced_step_closure *dsc)
4071{
4072 ULONGEST rd_val;
4073 int i;
4074
4075 rd_val = displaced_read_reg (regs, dsc->insn_addr, 0);
4076
4077 for (i = 0; i < 3; i++)
4078 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
4079
4080 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
4081}
4082
4083static int
4084copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
4085 struct displaced_step_closure *dsc)
4086{
4087 unsigned int rn = bits (insn, 16, 19);
4088 unsigned int rm = bits (insn, 0, 3);
4089 unsigned int rd = bits (insn, 12, 15);
4090 unsigned int op = bits (insn, 21, 24);
4091 int is_mov = (op == 0xd);
4092 ULONGEST rd_val, rn_val, rm_val;
4093 CORE_ADDR from = dsc->insn_addr;
4094
4095 if (!insn_references_pc (insn, 0x000ff00ful))
4096 return copy_unmodified (gdbarch, insn, "ALU reg", dsc);
4097
4098 if (debug_displaced)
4099 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.8lx\n",
4100 is_mov ? "move" : "ALU", (unsigned long) insn);
4101
4102 /* Instruction is of form:
4103
4104 <op><cond> rd, [rn,] rm [, <shift>]
4105
4106 Rewrite as:
4107
4108 Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
4109 r0, r1, r2 <- rd, rn, rm
4110 Insn: <op><cond> r0, r1, r2 [, <shift>]
4111 Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
4112 */
4113
4114 dsc->tmp[0] = displaced_read_reg (regs, from, 0);
4115 dsc->tmp[1] = displaced_read_reg (regs, from, 1);
4116 dsc->tmp[2] = displaced_read_reg (regs, from, 2);
4117 rd_val = displaced_read_reg (regs, from, rd);
4118 rn_val = displaced_read_reg (regs, from, rn);
4119 rm_val = displaced_read_reg (regs, from, rm);
4120 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
4121 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
4122 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
4123 dsc->rd = rd;
4124
4125 if (is_mov)
4126 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
4127 else
4128 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
4129
4130 dsc->cleanup = &cleanup_alu_reg;
4131
4132 return 0;
4133}
4134
4135/* Cleanup/copy arithmetic/logic insns with shifted register RHS. */
4136
4137static void
6e39997a 4138cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
cca44b1b
JB
4139 struct regcache *regs,
4140 struct displaced_step_closure *dsc)
4141{
4142 ULONGEST rd_val = displaced_read_reg (regs, dsc->insn_addr, 0);
4143 int i;
4144
4145 for (i = 0; i < 4; i++)
4146 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
4147
4148 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
4149}
4150
4151static int
4152copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
4153 struct regcache *regs, struct displaced_step_closure *dsc)
4154{
4155 unsigned int rn = bits (insn, 16, 19);
4156 unsigned int rm = bits (insn, 0, 3);
4157 unsigned int rd = bits (insn, 12, 15);
4158 unsigned int rs = bits (insn, 8, 11);
4159 unsigned int op = bits (insn, 21, 24);
4160 int is_mov = (op == 0xd), i;
4161 ULONGEST rd_val, rn_val, rm_val, rs_val;
4162 CORE_ADDR from = dsc->insn_addr;
4163
4164 if (!insn_references_pc (insn, 0x000fff0ful))
4165 return copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
4166
4167 if (debug_displaced)
4168 fprintf_unfiltered (gdb_stdlog, "displaced: copying shifted reg %s insn "
4169 "%.8lx\n", is_mov ? "move" : "ALU",
4170 (unsigned long) insn);
4171
4172 /* Instruction is of form:
4173
4174 <op><cond> rd, [rn,] rm, <shift> rs
4175
4176 Rewrite as:
4177
4178 Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
4179 r0, r1, r2, r3 <- rd, rn, rm, rs
4180 Insn: <op><cond> r0, r1, r2, <shift> r3
4181 Cleanup: tmp5 <- r0
4182 r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
4183 rd <- tmp5
4184 */
4185
4186 for (i = 0; i < 4; i++)
4187 dsc->tmp[i] = displaced_read_reg (regs, from, i);
4188
4189 rd_val = displaced_read_reg (regs, from, rd);
4190 rn_val = displaced_read_reg (regs, from, rn);
4191 rm_val = displaced_read_reg (regs, from, rm);
4192 rs_val = displaced_read_reg (regs, from, rs);
4193 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
4194 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
4195 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
4196 displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
4197 dsc->rd = rd;
4198
4199 if (is_mov)
4200 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
4201 else
4202 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
4203
4204 dsc->cleanup = &cleanup_alu_shifted_reg;
4205
4206 return 0;
4207}
4208
4209/* Clean up load instructions. */
4210
4211static void
6e39997a 4212cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
4213 struct displaced_step_closure *dsc)
4214{
4215 ULONGEST rt_val, rt_val2 = 0, rn_val;
4216 CORE_ADDR from = dsc->insn_addr;
4217
4218 rt_val = displaced_read_reg (regs, from, 0);
4219 if (dsc->u.ldst.xfersize == 8)
4220 rt_val2 = displaced_read_reg (regs, from, 1);
4221 rn_val = displaced_read_reg (regs, from, 2);
4222
4223 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
4224 if (dsc->u.ldst.xfersize > 4)
4225 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
4226 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
4227 if (!dsc->u.ldst.immed)
4228 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
4229
4230 /* Handle register writeback. */
4231 if (dsc->u.ldst.writeback)
4232 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
4233 /* Put result in right place. */
4234 displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
4235 if (dsc->u.ldst.xfersize == 8)
4236 displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
4237}
4238
4239/* Clean up store instructions. */
4240
4241static void
6e39997a 4242cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
4243 struct displaced_step_closure *dsc)
4244{
4245 CORE_ADDR from = dsc->insn_addr;
4246 ULONGEST rn_val = displaced_read_reg (regs, from, 2);
4247
4248 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
4249 if (dsc->u.ldst.xfersize > 4)
4250 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
4251 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
4252 if (!dsc->u.ldst.immed)
4253 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
4254 if (!dsc->u.ldst.restore_r4)
4255 displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
4256
4257 /* Writeback. */
4258 if (dsc->u.ldst.writeback)
4259 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
4260}
4261
4262/* Copy "extra" load/store instructions. These are halfword/doubleword
4263 transfers, which have a different encoding to byte/word transfers. */
4264
4265static int
4266copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unpriveleged,
4267 struct regcache *regs, struct displaced_step_closure *dsc)
4268{
4269 unsigned int op1 = bits (insn, 20, 24);
4270 unsigned int op2 = bits (insn, 5, 6);
4271 unsigned int rt = bits (insn, 12, 15);
4272 unsigned int rn = bits (insn, 16, 19);
4273 unsigned int rm = bits (insn, 0, 3);
4274 char load[12] = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
4275 char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
4276 int immed = (op1 & 0x4) != 0;
4277 int opcode;
4278 ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
4279 CORE_ADDR from = dsc->insn_addr;
4280
4281 if (!insn_references_pc (insn, 0x000ff00ful))
4282 return copy_unmodified (gdbarch, insn, "extra load/store", dsc);
4283
4284 if (debug_displaced)
4285 fprintf_unfiltered (gdb_stdlog, "displaced: copying %sextra load/store "
4286 "insn %.8lx\n", unpriveleged ? "unpriveleged " : "",
4287 (unsigned long) insn);
4288
4289 opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
4290
4291 if (opcode < 0)
4292 internal_error (__FILE__, __LINE__,
4293 _("copy_extra_ld_st: instruction decode error"));
4294
4295 dsc->tmp[0] = displaced_read_reg (regs, from, 0);
4296 dsc->tmp[1] = displaced_read_reg (regs, from, 1);
4297 dsc->tmp[2] = displaced_read_reg (regs, from, 2);
4298 if (!immed)
4299 dsc->tmp[3] = displaced_read_reg (regs, from, 3);
4300
4301 rt_val = displaced_read_reg (regs, from, rt);
4302 if (bytesize[opcode] == 8)
4303 rt_val2 = displaced_read_reg (regs, from, rt + 1);
4304 rn_val = displaced_read_reg (regs, from, rn);
4305 if (!immed)
4306 rm_val = displaced_read_reg (regs, from, rm);
4307
4308 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
4309 if (bytesize[opcode] == 8)
4310 displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
4311 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
4312 if (!immed)
4313 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
4314
4315 dsc->rd = rt;
4316 dsc->u.ldst.xfersize = bytesize[opcode];
4317 dsc->u.ldst.rn = rn;
4318 dsc->u.ldst.immed = immed;
4319 dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
4320 dsc->u.ldst.restore_r4 = 0;
4321
4322 if (immed)
4323 /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
4324 ->
4325 {ldr,str}<width><cond> r0, [r1,] [r2, #imm]. */
4326 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
4327 else
4328 /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
4329 ->
4330 {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3]. */
4331 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
4332
4333 dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
4334
4335 return 0;
4336}
4337
4338/* Copy byte/word loads and stores. */
4339
4340static int
4341copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
4342 struct regcache *regs,
4343 struct displaced_step_closure *dsc, int load, int byte,
4344 int usermode)
4345{
4346 int immed = !bit (insn, 25);
4347 unsigned int rt = bits (insn, 12, 15);
4348 unsigned int rn = bits (insn, 16, 19);
4349 unsigned int rm = bits (insn, 0, 3); /* Only valid if !immed. */
4350 ULONGEST rt_val, rn_val, rm_val = 0;
4351 CORE_ADDR from = dsc->insn_addr;
4352
4353 if (!insn_references_pc (insn, 0x000ff00ful))
4354 return copy_unmodified (gdbarch, insn, "load/store", dsc);
4355
4356 if (debug_displaced)
4357 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s%s insn %.8lx\n",
4358 load ? (byte ? "ldrb" : "ldr")
4359 : (byte ? "strb" : "str"), usermode ? "t" : "",
4360 (unsigned long) insn);
4361
4362 dsc->tmp[0] = displaced_read_reg (regs, from, 0);
4363 dsc->tmp[2] = displaced_read_reg (regs, from, 2);
4364 if (!immed)
4365 dsc->tmp[3] = displaced_read_reg (regs, from, 3);
4366 if (!load)
4367 dsc->tmp[4] = displaced_read_reg (regs, from, 4);
4368
4369 rt_val = displaced_read_reg (regs, from, rt);
4370 rn_val = displaced_read_reg (regs, from, rn);
4371 if (!immed)
4372 rm_val = displaced_read_reg (regs, from, rm);
4373
4374 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
4375 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
4376 if (!immed)
4377 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
4378
4379 dsc->rd = rt;
4380 dsc->u.ldst.xfersize = byte ? 1 : 4;
4381 dsc->u.ldst.rn = rn;
4382 dsc->u.ldst.immed = immed;
4383 dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
4384
4385 /* To write PC we can do:
4386
4387 scratch+0: str pc, temp (*temp = scratch + 8 + offset)
4388 scratch+4: ldr r4, temp
4389 scratch+8: sub r4, r4, pc (r4 = scratch + 8 + offset - scratch - 8 - 8)
4390 scratch+12: add r4, r4, #8 (r4 = offset)
4391 scratch+16: add r0, r0, r4
4392 scratch+20: str r0, [r2, #imm] (or str r0, [r2, r3])
4393 scratch+24: <temp>
4394
4395 Otherwise we don't know what value to write for PC, since the offset is
4396 architecture-dependent (sometimes PC+8, sometimes PC+12). */
4397
4398 if (load || rt != 15)
4399 {
4400 dsc->u.ldst.restore_r4 = 0;
4401
4402 if (immed)
4403 /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
4404 ->
4405 {ldr,str}[b]<cond> r0, [r2, #imm]. */
4406 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
4407 else
4408 /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
4409 ->
4410 {ldr,str}[b]<cond> r0, [r2, r3]. */
4411 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
4412 }
4413 else
4414 {
4415 /* We need to use r4 as scratch. Make sure it's restored afterwards. */
4416 dsc->u.ldst.restore_r4 = 1;
4417
4418 dsc->modinsn[0] = 0xe58ff014; /* str pc, [pc, #20]. */
4419 dsc->modinsn[1] = 0xe59f4010; /* ldr r4, [pc, #16]. */
4420 dsc->modinsn[2] = 0xe044400f; /* sub r4, r4, pc. */
4421 dsc->modinsn[3] = 0xe2844008; /* add r4, r4, #8. */
4422 dsc->modinsn[4] = 0xe0800004; /* add r0, r0, r4. */
4423
4424 /* As above. */
4425 if (immed)
4426 dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
4427 else
4428 dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
4429
4430 dsc->modinsn[6] = 0x0; /* breakpoint location. */
4431 dsc->modinsn[7] = 0x0; /* scratch space. */
4432
4433 dsc->numinsns = 6;
4434 }
4435
4436 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
4437
4438 return 0;
4439}
4440
4441/* Cleanup LDM instructions with fully-populated register list. This is an
4442 unfortunate corner case: it's impossible to implement correctly by modifying
4443 the instruction. The issue is as follows: we have an instruction,
4444
4445 ldm rN, {r0-r15}
4446
4447 which we must rewrite to avoid loading PC. A possible solution would be to
4448 do the load in two halves, something like (with suitable cleanup
4449 afterwards):
4450
4451 mov r8, rN
4452 ldm[id][ab] r8!, {r0-r7}
4453 str r7, <temp>
4454 ldm[id][ab] r8, {r7-r14}
4455 <bkpt>
4456
4457 but at present there's no suitable place for <temp>, since the scratch space
4458 is overwritten before the cleanup routine is called. For now, we simply
4459 emulate the instruction. */
4460
4461static void
4462cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
4463 struct displaced_step_closure *dsc)
4464{
4465 ULONGEST from = dsc->insn_addr;
4466 int inc = dsc->u.block.increment;
4467 int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
4468 int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
4469 uint32_t regmask = dsc->u.block.regmask;
4470 int regno = inc ? 0 : 15;
4471 CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
4472 int exception_return = dsc->u.block.load && dsc->u.block.user
4473 && (regmask & 0x8000) != 0;
4474 uint32_t status = displaced_read_reg (regs, from, ARM_PS_REGNUM);
4475 int do_transfer = condition_true (dsc->u.block.cond, status);
4476 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4477
4478 if (!do_transfer)
4479 return;
4480
4481 /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
4482 sensible we can do here. Complain loudly. */
4483 if (exception_return)
4484 error (_("Cannot single-step exception return"));
4485
4486 /* We don't handle any stores here for now. */
4487 gdb_assert (dsc->u.block.load != 0);
4488
4489 if (debug_displaced)
4490 fprintf_unfiltered (gdb_stdlog, "displaced: emulating block transfer: "
4491 "%s %s %s\n", dsc->u.block.load ? "ldm" : "stm",
4492 dsc->u.block.increment ? "inc" : "dec",
4493 dsc->u.block.before ? "before" : "after");
4494
4495 while (regmask)
4496 {
4497 uint32_t memword;
4498
4499 if (inc)
4500 while (regno <= 15 && (regmask & (1 << regno)) == 0)
4501 regno++;
4502 else
4503 while (regno >= 0 && (regmask & (1 << regno)) == 0)
4504 regno--;
4505
4506 xfer_addr += bump_before;
4507
4508 memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
4509 displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
4510
4511 xfer_addr += bump_after;
4512
4513 regmask &= ~(1 << regno);
4514 }
4515
4516 if (dsc->u.block.writeback)
4517 displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
4518 CANNOT_WRITE_PC);
4519}
4520
4521/* Clean up an STM which included the PC in the register list. */
4522
4523static void
4524cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
4525 struct displaced_step_closure *dsc)
4526{
4527 ULONGEST from = dsc->insn_addr;
4528 uint32_t status = displaced_read_reg (regs, from, ARM_PS_REGNUM);
4529 int store_executed = condition_true (dsc->u.block.cond, status);
4530 CORE_ADDR pc_stored_at, transferred_regs = bitcount (dsc->u.block.regmask);
4531 CORE_ADDR stm_insn_addr;
4532 uint32_t pc_val;
4533 long offset;
4534 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4535
4536 /* If condition code fails, there's nothing else to do. */
4537 if (!store_executed)
4538 return;
4539
4540 if (dsc->u.block.increment)
4541 {
4542 pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
4543
4544 if (dsc->u.block.before)
4545 pc_stored_at += 4;
4546 }
4547 else
4548 {
4549 pc_stored_at = dsc->u.block.xfer_addr;
4550
4551 if (dsc->u.block.before)
4552 pc_stored_at -= 4;
4553 }
4554
4555 pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
4556 stm_insn_addr = dsc->scratch_base;
4557 offset = pc_val - stm_insn_addr;
4558
4559 if (debug_displaced)
4560 fprintf_unfiltered (gdb_stdlog, "displaced: detected PC offset %.8lx for "
4561 "STM instruction\n", offset);
4562
4563 /* Rewrite the stored PC to the proper value for the non-displaced original
4564 instruction. */
4565 write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
4566 dsc->insn_addr + offset);
4567}
4568
4569/* Clean up an LDM which includes the PC in the register list. We clumped all
4570 the registers in the transferred list into a contiguous range r0...rX (to
4571 avoid loading PC directly and losing control of the debugged program), so we
4572 must undo that here. */
4573
4574static void
6e39997a 4575cleanup_block_load_pc (struct gdbarch *gdbarch,
cca44b1b
JB
4576 struct regcache *regs,
4577 struct displaced_step_closure *dsc)
4578{
4579 ULONGEST from = dsc->insn_addr;
4580 uint32_t status = displaced_read_reg (regs, from, ARM_PS_REGNUM);
4581 int load_executed = condition_true (dsc->u.block.cond, status), i;
4582 unsigned int mask = dsc->u.block.regmask, write_reg = 15;
4583 unsigned int regs_loaded = bitcount (mask);
4584 unsigned int num_to_shuffle = regs_loaded, clobbered;
4585
4586 /* The method employed here will fail if the register list is fully populated
4587 (we need to avoid loading PC directly). */
4588 gdb_assert (num_to_shuffle < 16);
4589
4590 if (!load_executed)
4591 return;
4592
4593 clobbered = (1 << num_to_shuffle) - 1;
4594
4595 while (num_to_shuffle > 0)
4596 {
4597 if ((mask & (1 << write_reg)) != 0)
4598 {
4599 unsigned int read_reg = num_to_shuffle - 1;
4600
4601 if (read_reg != write_reg)
4602 {
4603 ULONGEST rval = displaced_read_reg (regs, from, read_reg);
4604 displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
4605 if (debug_displaced)
4606 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: move "
4607 "loaded register r%d to r%d\n"), read_reg,
4608 write_reg);
4609 }
4610 else if (debug_displaced)
4611 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: register "
4612 "r%d already in the right place\n"),
4613 write_reg);
4614
4615 clobbered &= ~(1 << write_reg);
4616
4617 num_to_shuffle--;
4618 }
4619
4620 write_reg--;
4621 }
4622
4623 /* Restore any registers we scribbled over. */
4624 for (write_reg = 0; clobbered != 0; write_reg++)
4625 {
4626 if ((clobbered & (1 << write_reg)) != 0)
4627 {
4628 displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
4629 CANNOT_WRITE_PC);
4630 if (debug_displaced)
4631 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: restored "
4632 "clobbered register r%d\n"), write_reg);
4633 clobbered &= ~(1 << write_reg);
4634 }
4635 }
4636
4637 /* Perform register writeback manually. */
4638 if (dsc->u.block.writeback)
4639 {
4640 ULONGEST new_rn_val = dsc->u.block.xfer_addr;
4641
4642 if (dsc->u.block.increment)
4643 new_rn_val += regs_loaded * 4;
4644 else
4645 new_rn_val -= regs_loaded * 4;
4646
4647 displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
4648 CANNOT_WRITE_PC);
4649 }
4650}
4651
4652/* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
4653 in user-level code (in particular exception return, ldm rn, {...pc}^). */
4654
4655static int
4656copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
4657 struct displaced_step_closure *dsc)
4658{
4659 int load = bit (insn, 20);
4660 int user = bit (insn, 22);
4661 int increment = bit (insn, 23);
4662 int before = bit (insn, 24);
4663 int writeback = bit (insn, 21);
4664 int rn = bits (insn, 16, 19);
4665 CORE_ADDR from = dsc->insn_addr;
4666
4667 /* Block transfers which don't mention PC can be run directly out-of-line. */
4668 if (rn != 15 && (insn & 0x8000) == 0)
4669 return copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
4670
4671 if (rn == 15)
4672 {
4673 warning (_("displaced: Unpredictable LDM or STM with base register r15"));
4674 return copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
4675 }
4676
4677 if (debug_displaced)
4678 fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
4679 "%.8lx\n", (unsigned long) insn);
4680
4681 dsc->u.block.xfer_addr = displaced_read_reg (regs, from, rn);
4682 dsc->u.block.rn = rn;
4683
4684 dsc->u.block.load = load;
4685 dsc->u.block.user = user;
4686 dsc->u.block.increment = increment;
4687 dsc->u.block.before = before;
4688 dsc->u.block.writeback = writeback;
4689 dsc->u.block.cond = bits (insn, 28, 31);
4690
4691 dsc->u.block.regmask = insn & 0xffff;
4692
4693 if (load)
4694 {
4695 if ((insn & 0xffff) == 0xffff)
4696 {
4697 /* LDM with a fully-populated register list. This case is
4698 particularly tricky. Implement for now by fully emulating the
4699 instruction (which might not behave perfectly in all cases, but
4700 these instructions should be rare enough for that not to matter
4701 too much). */
4702 dsc->modinsn[0] = ARM_NOP;
4703
4704 dsc->cleanup = &cleanup_block_load_all;
4705 }
4706 else
4707 {
4708 /* LDM of a list of registers which includes PC. Implement by
4709 rewriting the list of registers to be transferred into a
4710 contiguous chunk r0...rX before doing the transfer, then shuffling
4711 registers into the correct places in the cleanup routine. */
4712 unsigned int regmask = insn & 0xffff;
4713 unsigned int num_in_list = bitcount (regmask), new_regmask, bit = 1;
4714 unsigned int to = 0, from = 0, i, new_rn;
4715
4716 for (i = 0; i < num_in_list; i++)
4717 dsc->tmp[i] = displaced_read_reg (regs, from, i);
4718
4719 /* Writeback makes things complicated. We need to avoid clobbering
4720 the base register with one of the registers in our modified
4721 register list, but just using a different register can't work in
4722 all cases, e.g.:
4723
4724 ldm r14!, {r0-r13,pc}
4725
4726 which would need to be rewritten as:
4727
4728 ldm rN!, {r0-r14}
4729
4730 but that can't work, because there's no free register for N.
4731
4732 Solve this by turning off the writeback bit, and emulating
4733 writeback manually in the cleanup routine. */
4734
4735 if (writeback)
4736 insn &= ~(1 << 21);
4737
4738 new_regmask = (1 << num_in_list) - 1;
4739
4740 if (debug_displaced)
4741 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
4742 "{..., pc}: original reg list %.4x, modified "
4743 "list %.4x\n"), rn, writeback ? "!" : "",
4744 (int) insn & 0xffff, new_regmask);
4745
4746 dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
4747
4748 dsc->cleanup = &cleanup_block_load_pc;
4749 }
4750 }
4751 else
4752 {
4753 /* STM of a list of registers which includes PC. Run the instruction
4754 as-is, but out of line: this will store the wrong value for the PC,
4755 so we must manually fix up the memory in the cleanup routine.
4756 Doing things this way has the advantage that we can auto-detect
4757 the offset of the PC write (which is architecture-dependent) in
4758 the cleanup routine. */
4759 dsc->modinsn[0] = insn;
4760
4761 dsc->cleanup = &cleanup_block_store_pc;
4762 }
4763
4764 return 0;
4765}
4766
4767/* Cleanup/copy SVC (SWI) instructions. These two functions are overridden
4768 for Linux, where some SVC instructions must be treated specially. */
4769
4770static void
6e39997a 4771cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
4772 struct displaced_step_closure *dsc)
4773{
4774 CORE_ADDR from = dsc->insn_addr;
4775 CORE_ADDR resume_addr = from + 4;
4776
4777 if (debug_displaced)
4778 fprintf_unfiltered (gdb_stdlog, "displaced: cleanup for svc, resume at "
4779 "%.8lx\n", (unsigned long) resume_addr);
4780
4781 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
4782}
4783
4784static int
4785copy_svc (struct gdbarch *gdbarch, uint32_t insn, CORE_ADDR to,
4786 struct regcache *regs, struct displaced_step_closure *dsc)
4787{
4788 CORE_ADDR from = dsc->insn_addr;
4789
4790 /* Allow OS-specific code to override SVC handling. */
4791 if (dsc->u.svc.copy_svc_os)
4792 return dsc->u.svc.copy_svc_os (gdbarch, insn, to, regs, dsc);
4793
4794 if (debug_displaced)
4795 fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.8lx\n",
4796 (unsigned long) insn);
4797
4798 /* Preparation: none.
4799 Insn: unmodified svc.
4800 Cleanup: pc <- insn_addr + 4. */
4801
4802 dsc->modinsn[0] = insn;
4803
4804 dsc->cleanup = &cleanup_svc;
4805 /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
4806 instruction. */
4807 dsc->wrote_to_pc = 1;
4808
4809 return 0;
4810}
4811
4812/* Copy undefined instructions. */
4813
4814static int
6e39997a 4815copy_undef (struct gdbarch *gdbarch, uint32_t insn,
cca44b1b
JB
4816 struct displaced_step_closure *dsc)
4817{
4818 if (debug_displaced)
4819 fprintf_unfiltered (gdb_stdlog, "displaced: copying undefined insn %.8lx\n",
4820 (unsigned long) insn);
4821
4822 dsc->modinsn[0] = insn;
4823
4824 return 0;
4825}
4826
4827/* Copy unpredictable instructions. */
4828
4829static int
6e39997a 4830copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
cca44b1b
JB
4831 struct displaced_step_closure *dsc)
4832{
4833 if (debug_displaced)
4834 fprintf_unfiltered (gdb_stdlog, "displaced: copying unpredictable insn "
4835 "%.8lx\n", (unsigned long) insn);
4836
4837 dsc->modinsn[0] = insn;
4838
4839 return 0;
4840}
4841
4842/* The decode_* functions are instruction decoding helpers. They mostly follow
4843 the presentation in the ARM ARM. */
4844
4845static int
4846decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
4847 struct regcache *regs,
4848 struct displaced_step_closure *dsc)
4849{
4850 unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
4851 unsigned int rn = bits (insn, 16, 19);
4852
4853 if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0xe) == 0x0)
4854 return copy_unmodified (gdbarch, insn, "cps", dsc);
4855 else if (op1 == 0x10 && op2 == 0x0 && (rn & 0xe) == 0x1)
4856 return copy_unmodified (gdbarch, insn, "setend", dsc);
4857 else if ((op1 & 0x60) == 0x20)
4858 return copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
4859 else if ((op1 & 0x71) == 0x40)
4860 return copy_unmodified (gdbarch, insn, "neon elt/struct load/store", dsc);
4861 else if ((op1 & 0x77) == 0x41)
4862 return copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
4863 else if ((op1 & 0x77) == 0x45)
4864 return copy_preload (gdbarch, insn, regs, dsc); /* pli. */
4865 else if ((op1 & 0x77) == 0x51)
4866 {
4867 if (rn != 0xf)
4868 return copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
4869 else
4870 return copy_unpred (gdbarch, insn, dsc);
4871 }
4872 else if ((op1 & 0x77) == 0x55)
4873 return copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
4874 else if (op1 == 0x57)
4875 switch (op2)
4876 {
4877 case 0x1: return copy_unmodified (gdbarch, insn, "clrex", dsc);
4878 case 0x4: return copy_unmodified (gdbarch, insn, "dsb", dsc);
4879 case 0x5: return copy_unmodified (gdbarch, insn, "dmb", dsc);
4880 case 0x6: return copy_unmodified (gdbarch, insn, "isb", dsc);
4881 default: return copy_unpred (gdbarch, insn, dsc);
4882 }
4883 else if ((op1 & 0x63) == 0x43)
4884 return copy_unpred (gdbarch, insn, dsc);
4885 else if ((op2 & 0x1) == 0x0)
4886 switch (op1 & ~0x80)
4887 {
4888 case 0x61:
4889 return copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
4890 case 0x65:
4891 return copy_preload_reg (gdbarch, insn, regs, dsc); /* pli reg. */
4892 case 0x71: case 0x75:
4893 /* pld/pldw reg. */
4894 return copy_preload_reg (gdbarch, insn, regs, dsc);
4895 case 0x63: case 0x67: case 0x73: case 0x77:
4896 return copy_unpred (gdbarch, insn, dsc);
4897 default:
4898 return copy_undef (gdbarch, insn, dsc);
4899 }
4900 else
4901 return copy_undef (gdbarch, insn, dsc); /* Probably unreachable. */
4902}
4903
4904static int
4905decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
4906 struct regcache *regs, struct displaced_step_closure *dsc)
4907{
4908 if (bit (insn, 27) == 0)
4909 return decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
4910 /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx. */
4911 else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
4912 {
4913 case 0x0: case 0x2:
4914 return copy_unmodified (gdbarch, insn, "srs", dsc);
4915
4916 case 0x1: case 0x3:
4917 return copy_unmodified (gdbarch, insn, "rfe", dsc);
4918
4919 case 0x4: case 0x5: case 0x6: case 0x7:
4920 return copy_b_bl_blx (gdbarch, insn, regs, dsc);
4921
4922 case 0x8:
4923 switch ((insn & 0xe00000) >> 21)
4924 {
4925 case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
4926 /* stc/stc2. */
4927 return copy_copro_load_store (gdbarch, insn, regs, dsc);
4928
4929 case 0x2:
4930 return copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
4931
4932 default:
4933 return copy_undef (gdbarch, insn, dsc);
4934 }
4935
4936 case 0x9:
4937 {
4938 int rn_f = (bits (insn, 16, 19) == 0xf);
4939 switch ((insn & 0xe00000) >> 21)
4940 {
4941 case 0x1: case 0x3:
4942 /* ldc/ldc2 imm (undefined for rn == pc). */
4943 return rn_f ? copy_undef (gdbarch, insn, dsc)
4944 : copy_copro_load_store (gdbarch, insn, regs, dsc);
4945
4946 case 0x2:
4947 return copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
4948
4949 case 0x4: case 0x5: case 0x6: case 0x7:
4950 /* ldc/ldc2 lit (undefined for rn != pc). */
4951 return rn_f ? copy_copro_load_store (gdbarch, insn, regs, dsc)
4952 : copy_undef (gdbarch, insn, dsc);
4953
4954 default:
4955 return copy_undef (gdbarch, insn, dsc);
4956 }
4957 }
4958
4959 case 0xa:
4960 return copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
4961
4962 case 0xb:
4963 if (bits (insn, 16, 19) == 0xf)
4964 /* ldc/ldc2 lit. */
4965 return copy_copro_load_store (gdbarch, insn, regs, dsc);
4966 else
4967 return copy_undef (gdbarch, insn, dsc);
4968
4969 case 0xc:
4970 if (bit (insn, 4))
4971 return copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
4972 else
4973 return copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
4974
4975 case 0xd:
4976 if (bit (insn, 4))
4977 return copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
4978 else
4979 return copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
4980
4981 default:
4982 return copy_undef (gdbarch, insn, dsc);
4983 }
4984}
4985
4986/* Decode miscellaneous instructions in dp/misc encoding space. */
4987
4988static int
4989decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
4990 struct regcache *regs, struct displaced_step_closure *dsc)
4991{
4992 unsigned int op2 = bits (insn, 4, 6);
4993 unsigned int op = bits (insn, 21, 22);
4994 unsigned int op1 = bits (insn, 16, 19);
4995
4996 switch (op2)
4997 {
4998 case 0x0:
4999 return copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
5000
5001 case 0x1:
5002 if (op == 0x1) /* bx. */
5003 return copy_bx_blx_reg (gdbarch, insn, regs, dsc);
5004 else if (op == 0x3)
5005 return copy_unmodified (gdbarch, insn, "clz", dsc);
5006 else
5007 return copy_undef (gdbarch, insn, dsc);
5008
5009 case 0x2:
5010 if (op == 0x1)
5011 /* Not really supported. */
5012 return copy_unmodified (gdbarch, insn, "bxj", dsc);
5013 else
5014 return copy_undef (gdbarch, insn, dsc);
5015
5016 case 0x3:
5017 if (op == 0x1)
5018 return copy_bx_blx_reg (gdbarch, insn, regs, dsc); /* blx register. */
5019 else
5020 return copy_undef (gdbarch, insn, dsc);
5021
5022 case 0x5:
5023 return copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
5024
5025 case 0x7:
5026 if (op == 0x1)
5027 return copy_unmodified (gdbarch, insn, "bkpt", dsc);
5028 else if (op == 0x3)
5029 /* Not really supported. */
5030 return copy_unmodified (gdbarch, insn, "smc", dsc);
5031
5032 default:
5033 return copy_undef (gdbarch, insn, dsc);
5034 }
5035}
5036
5037static int
5038decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
5039 struct displaced_step_closure *dsc)
5040{
5041 if (bit (insn, 25))
5042 switch (bits (insn, 20, 24))
5043 {
5044 case 0x10:
5045 return copy_unmodified (gdbarch, insn, "movw", dsc);
5046
5047 case 0x14:
5048 return copy_unmodified (gdbarch, insn, "movt", dsc);
5049
5050 case 0x12: case 0x16:
5051 return copy_unmodified (gdbarch, insn, "msr imm", dsc);
5052
5053 default:
5054 return copy_alu_imm (gdbarch, insn, regs, dsc);
5055 }
5056 else
5057 {
5058 uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
5059
5060 if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
5061 return copy_alu_reg (gdbarch, insn, regs, dsc);
5062 else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
5063 return copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
5064 else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
5065 return decode_miscellaneous (gdbarch, insn, regs, dsc);
5066 else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
5067 return copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
5068 else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
5069 return copy_unmodified (gdbarch, insn, "mul/mla", dsc);
5070 else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
5071 return copy_unmodified (gdbarch, insn, "synch", dsc);
5072 else if (op2 == 0xb || (op2 & 0xd) == 0xd)
5073 /* 2nd arg means "unpriveleged". */
5074 return copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
5075 dsc);
5076 }
5077
5078 /* Should be unreachable. */
5079 return 1;
5080}
5081
5082static int
5083decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
5084 struct regcache *regs,
5085 struct displaced_step_closure *dsc)
5086{
5087 int a = bit (insn, 25), b = bit (insn, 4);
5088 uint32_t op1 = bits (insn, 20, 24);
5089 int rn_f = bits (insn, 16, 19) == 0xf;
5090
5091 if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
5092 || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
5093 return copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 0, 0);
5094 else if ((!a && (op1 & 0x17) == 0x02)
5095 || (a && (op1 & 0x17) == 0x02 && !b))
5096 return copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 0, 1);
5097 else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
5098 || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
5099 return copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 0, 0);
5100 else if ((!a && (op1 & 0x17) == 0x03)
5101 || (a && (op1 & 0x17) == 0x03 && !b))
5102 return copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 0, 1);
5103 else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
5104 || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
5105 return copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
5106 else if ((!a && (op1 & 0x17) == 0x06)
5107 || (a && (op1 & 0x17) == 0x06 && !b))
5108 return copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
5109 else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
5110 || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
5111 return copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
5112 else if ((!a && (op1 & 0x17) == 0x07)
5113 || (a && (op1 & 0x17) == 0x07 && !b))
5114 return copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
5115
5116 /* Should be unreachable. */
5117 return 1;
5118}
5119
5120static int
5121decode_media (struct gdbarch *gdbarch, uint32_t insn,
5122 struct displaced_step_closure *dsc)
5123{
5124 switch (bits (insn, 20, 24))
5125 {
5126 case 0x00: case 0x01: case 0x02: case 0x03:
5127 return copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
5128
5129 case 0x04: case 0x05: case 0x06: case 0x07:
5130 return copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
5131
5132 case 0x08: case 0x09: case 0x0a: case 0x0b:
5133 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
5134 return copy_unmodified (gdbarch, insn,
5135 "decode/pack/unpack/saturate/reverse", dsc);
5136
5137 case 0x18:
5138 if (bits (insn, 5, 7) == 0) /* op2. */
5139 {
5140 if (bits (insn, 12, 15) == 0xf)
5141 return copy_unmodified (gdbarch, insn, "usad8", dsc);
5142 else
5143 return copy_unmodified (gdbarch, insn, "usada8", dsc);
5144 }
5145 else
5146 return copy_undef (gdbarch, insn, dsc);
5147
5148 case 0x1a: case 0x1b:
5149 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
5150 return copy_unmodified (gdbarch, insn, "sbfx", dsc);
5151 else
5152 return copy_undef (gdbarch, insn, dsc);
5153
5154 case 0x1c: case 0x1d:
5155 if (bits (insn, 5, 6) == 0x0) /* op2[1:0]. */
5156 {
5157 if (bits (insn, 0, 3) == 0xf)
5158 return copy_unmodified (gdbarch, insn, "bfc", dsc);
5159 else
5160 return copy_unmodified (gdbarch, insn, "bfi", dsc);
5161 }
5162 else
5163 return copy_undef (gdbarch, insn, dsc);
5164
5165 case 0x1e: case 0x1f:
5166 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
5167 return copy_unmodified (gdbarch, insn, "ubfx", dsc);
5168 else
5169 return copy_undef (gdbarch, insn, dsc);
5170 }
5171
5172 /* Should be unreachable. */
5173 return 1;
5174}
5175
5176static int
5177decode_b_bl_ldmstm (struct gdbarch *gdbarch, int32_t insn,
5178 struct regcache *regs, struct displaced_step_closure *dsc)
5179{
5180 if (bit (insn, 25))
5181 return copy_b_bl_blx (gdbarch, insn, regs, dsc);
5182 else
5183 return copy_block_xfer (gdbarch, insn, regs, dsc);
5184}
5185
5186static int
5187decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
5188 struct regcache *regs, struct displaced_step_closure *dsc)
5189{
5190 unsigned int opcode = bits (insn, 20, 24);
5191
5192 switch (opcode)
5193 {
5194 case 0x04: case 0x05: /* VFP/Neon mrrc/mcrr. */
5195 return copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
5196
5197 case 0x08: case 0x0a: case 0x0c: case 0x0e:
5198 case 0x12: case 0x16:
5199 return copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
5200
5201 case 0x09: case 0x0b: case 0x0d: case 0x0f:
5202 case 0x13: case 0x17:
5203 return copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
5204
5205 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
5206 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
5207 /* Note: no writeback for these instructions. Bit 25 will always be
5208 zero though (via caller), so the following works OK. */
5209 return copy_copro_load_store (gdbarch, insn, regs, dsc);
5210 }
5211
5212 /* Should be unreachable. */
5213 return 1;
5214}
5215
5216static int
5217decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn, CORE_ADDR to,
5218 struct regcache *regs, struct displaced_step_closure *dsc)
5219{
5220 unsigned int op1 = bits (insn, 20, 25);
5221 int op = bit (insn, 4);
5222 unsigned int coproc = bits (insn, 8, 11);
5223 unsigned int rn = bits (insn, 16, 19);
5224
5225 if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
5226 return decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
5227 else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
5228 && (coproc & 0xe) != 0xa)
5229 /* stc/stc2. */
5230 return copy_copro_load_store (gdbarch, insn, regs, dsc);
5231 else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
5232 && (coproc & 0xe) != 0xa)
5233 /* ldc/ldc2 imm/lit. */
5234 return copy_copro_load_store (gdbarch, insn, regs, dsc);
5235 else if ((op1 & 0x3e) == 0x00)
5236 return copy_undef (gdbarch, insn, dsc);
5237 else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
5238 return copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
5239 else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
5240 return copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
5241 else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
5242 return copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
5243 else if ((op1 & 0x30) == 0x20 && !op)
5244 {
5245 if ((coproc & 0xe) == 0xa)
5246 return copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
5247 else
5248 return copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
5249 }
5250 else if ((op1 & 0x30) == 0x20 && op)
5251 return copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
5252 else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
5253 return copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
5254 else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
5255 return copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
5256 else if ((op1 & 0x30) == 0x30)
5257 return copy_svc (gdbarch, insn, to, regs, dsc);
5258 else
5259 return copy_undef (gdbarch, insn, dsc); /* Possibly unreachable. */
5260}
5261
5262void
5263arm_process_displaced_insn (struct gdbarch *gdbarch, uint32_t insn,
5264 CORE_ADDR from, CORE_ADDR to, struct regcache *regs,
5265 struct displaced_step_closure *dsc)
5266{
5267 int err = 0;
5268
5269 if (!displaced_in_arm_mode (regs))
5270 error (_("Displaced stepping is only supported in ARM mode"));
5271
5272 /* Most displaced instructions use a 1-instruction scratch space, so set this
5273 here and override below if/when necessary. */
5274 dsc->numinsns = 1;
5275 dsc->insn_addr = from;
5276 dsc->scratch_base = to;
5277 dsc->cleanup = NULL;
5278 dsc->wrote_to_pc = 0;
5279
5280 if ((insn & 0xf0000000) == 0xf0000000)
5281 err = decode_unconditional (gdbarch, insn, regs, dsc);
5282 else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
5283 {
5284 case 0x0: case 0x1: case 0x2: case 0x3:
5285 err = decode_dp_misc (gdbarch, insn, regs, dsc);
5286 break;
5287
5288 case 0x4: case 0x5: case 0x6:
5289 err = decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
5290 break;
5291
5292 case 0x7:
5293 err = decode_media (gdbarch, insn, dsc);
5294 break;
5295
5296 case 0x8: case 0x9: case 0xa: case 0xb:
5297 err = decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
5298 break;
5299
5300 case 0xc: case 0xd: case 0xe: case 0xf:
5301 err = decode_svc_copro (gdbarch, insn, to, regs, dsc);
5302 break;
5303 }
5304
5305 if (err)
5306 internal_error (__FILE__, __LINE__,
5307 _("arm_process_displaced_insn: Instruction decode error"));
5308}
5309
5310/* Actually set up the scratch space for a displaced instruction. */
5311
5312void
5313arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
5314 CORE_ADDR to, struct displaced_step_closure *dsc)
5315{
5316 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
5317 unsigned int i;
5318 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
5319
5320 /* Poke modified instruction(s). */
5321 for (i = 0; i < dsc->numinsns; i++)
5322 {
5323 if (debug_displaced)
5324 fprintf_unfiltered (gdb_stdlog, "displaced: writing insn %.8lx at "
5325 "%.8lx\n", (unsigned long) dsc->modinsn[i],
5326 (unsigned long) to + i * 4);
5327 write_memory_unsigned_integer (to + i * 4, 4, byte_order_for_code,
5328 dsc->modinsn[i]);
5329 }
5330
5331 /* Put breakpoint afterwards. */
5332 write_memory (to + dsc->numinsns * 4, tdep->arm_breakpoint,
5333 tdep->arm_breakpoint_size);
5334
5335 if (debug_displaced)
5336 fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
5337 paddress (gdbarch, from), paddress (gdbarch, to));
5338}
5339
5340/* Entry point for copying an instruction into scratch space for displaced
5341 stepping. */
5342
5343struct displaced_step_closure *
5344arm_displaced_step_copy_insn (struct gdbarch *gdbarch,
5345 CORE_ADDR from, CORE_ADDR to,
5346 struct regcache *regs)
5347{
5348 struct displaced_step_closure *dsc
5349 = xmalloc (sizeof (struct displaced_step_closure));
5350 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
5351 uint32_t insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
5352
5353 if (debug_displaced)
5354 fprintf_unfiltered (gdb_stdlog, "displaced: stepping insn %.8lx "
5355 "at %.8lx\n", (unsigned long) insn,
5356 (unsigned long) from);
5357
5358 arm_process_displaced_insn (gdbarch, insn, from, to, regs, dsc);
5359 arm_displaced_init_closure (gdbarch, from, to, dsc);
5360
5361 return dsc;
5362}
5363
5364/* Entry point for cleaning things up after a displaced instruction has been
5365 single-stepped. */
5366
5367void
5368arm_displaced_step_fixup (struct gdbarch *gdbarch,
5369 struct displaced_step_closure *dsc,
5370 CORE_ADDR from, CORE_ADDR to,
5371 struct regcache *regs)
5372{
5373 if (dsc->cleanup)
5374 dsc->cleanup (gdbarch, regs, dsc);
5375
5376 if (!dsc->wrote_to_pc)
5377 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, dsc->insn_addr + 4);
5378}
5379
5380#include "bfd-in2.h"
5381#include "libcoff.h"
5382
5383static int
5384gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
5385{
9779414d
DJ
5386 struct gdbarch *gdbarch = info->application_data;
5387
5388 if (arm_pc_is_thumb (gdbarch, memaddr))
cca44b1b
JB
5389 {
5390 static asymbol *asym;
5391 static combined_entry_type ce;
5392 static struct coff_symbol_struct csym;
5393 static struct bfd fake_bfd;
5394 static bfd_target fake_target;
5395
5396 if (csym.native == NULL)
5397 {
5398 /* Create a fake symbol vector containing a Thumb symbol.
5399 This is solely so that the code in print_insn_little_arm()
5400 and print_insn_big_arm() in opcodes/arm-dis.c will detect
5401 the presence of a Thumb symbol and switch to decoding
5402 Thumb instructions. */
5403
5404 fake_target.flavour = bfd_target_coff_flavour;
5405 fake_bfd.xvec = &fake_target;
5406 ce.u.syment.n_sclass = C_THUMBEXTFUNC;
5407 csym.native = &ce;
5408 csym.symbol.the_bfd = &fake_bfd;
5409 csym.symbol.name = "fake";
5410 asym = (asymbol *) & csym;
5411 }
5412
5413 memaddr = UNMAKE_THUMB_ADDR (memaddr);
5414 info->symbols = &asym;
5415 }
5416 else
5417 info->symbols = NULL;
5418
5419 if (info->endian == BFD_ENDIAN_BIG)
5420 return print_insn_big_arm (memaddr, info);
5421 else
5422 return print_insn_little_arm (memaddr, info);
5423}
5424
5425/* The following define instruction sequences that will cause ARM
5426 cpu's to take an undefined instruction trap. These are used to
5427 signal a breakpoint to GDB.
5428
5429 The newer ARMv4T cpu's are capable of operating in ARM or Thumb
5430 modes. A different instruction is required for each mode. The ARM
5431 cpu's can also be big or little endian. Thus four different
5432 instructions are needed to support all cases.
5433
5434 Note: ARMv4 defines several new instructions that will take the
5435 undefined instruction trap. ARM7TDMI is nominally ARMv4T, but does
5436 not in fact add the new instructions. The new undefined
5437 instructions in ARMv4 are all instructions that had no defined
5438 behaviour in earlier chips. There is no guarantee that they will
5439 raise an exception, but may be treated as NOP's. In practice, it
5440 may only safe to rely on instructions matching:
5441
5442 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
5443 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
5444 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
5445
5446 Even this may only true if the condition predicate is true. The
5447 following use a condition predicate of ALWAYS so it is always TRUE.
5448
5449 There are other ways of forcing a breakpoint. GNU/Linux, RISC iX,
5450 and NetBSD all use a software interrupt rather than an undefined
5451 instruction to force a trap. This can be handled by by the
5452 abi-specific code during establishment of the gdbarch vector. */
5453
5454#define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
5455#define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
5456#define THUMB_LE_BREAKPOINT {0xbe,0xbe}
5457#define THUMB_BE_BREAKPOINT {0xbe,0xbe}
5458
5459static const char arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
5460static const char arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
5461static const char arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
5462static const char arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
5463
5464/* Determine the type and size of breakpoint to insert at PCPTR. Uses
5465 the program counter value to determine whether a 16-bit or 32-bit
5466 breakpoint should be used. It returns a pointer to a string of
5467 bytes that encode a breakpoint instruction, stores the length of
5468 the string to *lenptr, and adjusts the program counter (if
5469 necessary) to point to the actual memory location where the
5470 breakpoint should be inserted. */
5471
5472static const unsigned char *
5473arm_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenptr)
5474{
5475 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
177321bd 5476 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
cca44b1b 5477
9779414d 5478 if (arm_pc_is_thumb (gdbarch, *pcptr))
cca44b1b
JB
5479 {
5480 *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
177321bd
DJ
5481
5482 /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
5483 check whether we are replacing a 32-bit instruction. */
5484 if (tdep->thumb2_breakpoint != NULL)
5485 {
5486 gdb_byte buf[2];
5487 if (target_read_memory (*pcptr, buf, 2) == 0)
5488 {
5489 unsigned short inst1;
5490 inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
5491 if ((inst1 & 0xe000) == 0xe000 && (inst1 & 0x1800) != 0)
5492 {
5493 *lenptr = tdep->thumb2_breakpoint_size;
5494 return tdep->thumb2_breakpoint;
5495 }
5496 }
5497 }
5498
cca44b1b
JB
5499 *lenptr = tdep->thumb_breakpoint_size;
5500 return tdep->thumb_breakpoint;
5501 }
5502 else
5503 {
5504 *lenptr = tdep->arm_breakpoint_size;
5505 return tdep->arm_breakpoint;
5506 }
5507}
5508
177321bd
DJ
5509static void
5510arm_remote_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
5511 int *kindptr)
5512{
5513 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
5514
5515 arm_breakpoint_from_pc (gdbarch, pcptr, kindptr);
5516
9779414d 5517 if (arm_pc_is_thumb (gdbarch, *pcptr) && *kindptr == 4)
177321bd
DJ
5518 /* The documented magic value for a 32-bit Thumb-2 breakpoint, so
5519 that this is not confused with a 32-bit ARM breakpoint. */
5520 *kindptr = 3;
5521}
5522
cca44b1b
JB
5523/* Extract from an array REGBUF containing the (raw) register state a
5524 function return value of type TYPE, and copy that, in virtual
5525 format, into VALBUF. */
5526
5527static void
5528arm_extract_return_value (struct type *type, struct regcache *regs,
5529 gdb_byte *valbuf)
5530{
5531 struct gdbarch *gdbarch = get_regcache_arch (regs);
5532 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5533
5534 if (TYPE_CODE_FLT == TYPE_CODE (type))
5535 {
5536 switch (gdbarch_tdep (gdbarch)->fp_model)
5537 {
5538 case ARM_FLOAT_FPA:
5539 {
5540 /* The value is in register F0 in internal format. We need to
5541 extract the raw value and then convert it to the desired
5542 internal type. */
5543 bfd_byte tmpbuf[FP_REGISTER_SIZE];
5544
5545 regcache_cooked_read (regs, ARM_F0_REGNUM, tmpbuf);
5546 convert_from_extended (floatformat_from_type (type), tmpbuf,
5547 valbuf, gdbarch_byte_order (gdbarch));
5548 }
5549 break;
5550
5551 case ARM_FLOAT_SOFT_FPA:
5552 case ARM_FLOAT_SOFT_VFP:
5553 /* ARM_FLOAT_VFP can arise if this is a variadic function so
5554 not using the VFP ABI code. */
5555 case ARM_FLOAT_VFP:
5556 regcache_cooked_read (regs, ARM_A1_REGNUM, valbuf);
5557 if (TYPE_LENGTH (type) > 4)
5558 regcache_cooked_read (regs, ARM_A1_REGNUM + 1,
5559 valbuf + INT_REGISTER_SIZE);
5560 break;
5561
5562 default:
5563 internal_error
5564 (__FILE__, __LINE__,
5565 _("arm_extract_return_value: Floating point model not supported"));
5566 break;
5567 }
5568 }
5569 else if (TYPE_CODE (type) == TYPE_CODE_INT
5570 || TYPE_CODE (type) == TYPE_CODE_CHAR
5571 || TYPE_CODE (type) == TYPE_CODE_BOOL
5572 || TYPE_CODE (type) == TYPE_CODE_PTR
5573 || TYPE_CODE (type) == TYPE_CODE_REF
5574 || TYPE_CODE (type) == TYPE_CODE_ENUM)
5575 {
5576 /* If the the type is a plain integer, then the access is
5577 straight-forward. Otherwise we have to play around a bit more. */
5578 int len = TYPE_LENGTH (type);
5579 int regno = ARM_A1_REGNUM;
5580 ULONGEST tmp;
5581
5582 while (len > 0)
5583 {
5584 /* By using store_unsigned_integer we avoid having to do
5585 anything special for small big-endian values. */
5586 regcache_cooked_read_unsigned (regs, regno++, &tmp);
5587 store_unsigned_integer (valbuf,
5588 (len > INT_REGISTER_SIZE
5589 ? INT_REGISTER_SIZE : len),
5590 byte_order, tmp);
5591 len -= INT_REGISTER_SIZE;
5592 valbuf += INT_REGISTER_SIZE;
5593 }
5594 }
5595 else
5596 {
5597 /* For a structure or union the behaviour is as if the value had
5598 been stored to word-aligned memory and then loaded into
5599 registers with 32-bit load instruction(s). */
5600 int len = TYPE_LENGTH (type);
5601 int regno = ARM_A1_REGNUM;
5602 bfd_byte tmpbuf[INT_REGISTER_SIZE];
5603
5604 while (len > 0)
5605 {
5606 regcache_cooked_read (regs, regno++, tmpbuf);
5607 memcpy (valbuf, tmpbuf,
5608 len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
5609 len -= INT_REGISTER_SIZE;
5610 valbuf += INT_REGISTER_SIZE;
5611 }
5612 }
5613}
5614
5615
5616/* Will a function return an aggregate type in memory or in a
5617 register? Return 0 if an aggregate type can be returned in a
5618 register, 1 if it must be returned in memory. */
5619
5620static int
5621arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
5622{
5623 int nRc;
5624 enum type_code code;
5625
5626 CHECK_TYPEDEF (type);
5627
5628 /* In the ARM ABI, "integer" like aggregate types are returned in
5629 registers. For an aggregate type to be integer like, its size
5630 must be less than or equal to INT_REGISTER_SIZE and the
5631 offset of each addressable subfield must be zero. Note that bit
5632 fields are not addressable, and all addressable subfields of
5633 unions always start at offset zero.
5634
5635 This function is based on the behaviour of GCC 2.95.1.
5636 See: gcc/arm.c: arm_return_in_memory() for details.
5637
5638 Note: All versions of GCC before GCC 2.95.2 do not set up the
5639 parameters correctly for a function returning the following
5640 structure: struct { float f;}; This should be returned in memory,
5641 not a register. Richard Earnshaw sent me a patch, but I do not
5642 know of any way to detect if a function like the above has been
5643 compiled with the correct calling convention. */
5644
5645 /* All aggregate types that won't fit in a register must be returned
5646 in memory. */
5647 if (TYPE_LENGTH (type) > INT_REGISTER_SIZE)
5648 {
5649 return 1;
5650 }
5651
5652 /* The AAPCS says all aggregates not larger than a word are returned
5653 in a register. */
5654 if (gdbarch_tdep (gdbarch)->arm_abi != ARM_ABI_APCS)
5655 return 0;
5656
5657 /* The only aggregate types that can be returned in a register are
5658 structs and unions. Arrays must be returned in memory. */
5659 code = TYPE_CODE (type);
5660 if ((TYPE_CODE_STRUCT != code) && (TYPE_CODE_UNION != code))
5661 {
5662 return 1;
5663 }
5664
5665 /* Assume all other aggregate types can be returned in a register.
5666 Run a check for structures, unions and arrays. */
5667 nRc = 0;
5668
5669 if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
5670 {
5671 int i;
5672 /* Need to check if this struct/union is "integer" like. For
5673 this to be true, its size must be less than or equal to
5674 INT_REGISTER_SIZE and the offset of each addressable
5675 subfield must be zero. Note that bit fields are not
5676 addressable, and unions always start at offset zero. If any
5677 of the subfields is a floating point type, the struct/union
5678 cannot be an integer type. */
5679
5680 /* For each field in the object, check:
5681 1) Is it FP? --> yes, nRc = 1;
67255d04
RE
5682 2) Is it addressable (bitpos != 0) and
5683 not packed (bitsize == 0)?
5684 --> yes, nRc = 1
5685 */
5686
5687 for (i = 0; i < TYPE_NFIELDS (type); i++)
5688 {
5689 enum type_code field_type_code;
44e1a9eb 5690 field_type_code = TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type, i)));
67255d04
RE
5691
5692 /* Is it a floating point type field? */
5693 if (field_type_code == TYPE_CODE_FLT)
5694 {
5695 nRc = 1;
5696 break;
5697 }
5698
5699 /* If bitpos != 0, then we have to care about it. */
5700 if (TYPE_FIELD_BITPOS (type, i) != 0)
5701 {
5702 /* Bitfields are not addressable. If the field bitsize is
5703 zero, then the field is not packed. Hence it cannot be
5704 a bitfield or any other packed type. */
5705 if (TYPE_FIELD_BITSIZE (type, i) == 0)
5706 {
5707 nRc = 1;
5708 break;
5709 }
5710 }
5711 }
5712 }
5713
5714 return nRc;
5715}
5716
34e8f22d
RE
5717/* Write into appropriate registers a function return value of type
5718 TYPE, given in virtual format. */
5719
5720static void
b508a996 5721arm_store_return_value (struct type *type, struct regcache *regs,
5238cf52 5722 const gdb_byte *valbuf)
34e8f22d 5723{
be8626e0 5724 struct gdbarch *gdbarch = get_regcache_arch (regs);
e17a4113 5725 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
be8626e0 5726
34e8f22d
RE
5727 if (TYPE_CODE (type) == TYPE_CODE_FLT)
5728 {
7a5ea0d4 5729 char buf[MAX_REGISTER_SIZE];
34e8f22d 5730
be8626e0 5731 switch (gdbarch_tdep (gdbarch)->fp_model)
08216dd7
RE
5732 {
5733 case ARM_FLOAT_FPA:
5734
be8626e0
MD
5735 convert_to_extended (floatformat_from_type (type), buf, valbuf,
5736 gdbarch_byte_order (gdbarch));
b508a996 5737 regcache_cooked_write (regs, ARM_F0_REGNUM, buf);
08216dd7
RE
5738 break;
5739
fd50bc42 5740 case ARM_FLOAT_SOFT_FPA:
08216dd7 5741 case ARM_FLOAT_SOFT_VFP:
90445bd3
DJ
5742 /* ARM_FLOAT_VFP can arise if this is a variadic function so
5743 not using the VFP ABI code. */
5744 case ARM_FLOAT_VFP:
b508a996
RE
5745 regcache_cooked_write (regs, ARM_A1_REGNUM, valbuf);
5746 if (TYPE_LENGTH (type) > 4)
5747 regcache_cooked_write (regs, ARM_A1_REGNUM + 1,
7a5ea0d4 5748 valbuf + INT_REGISTER_SIZE);
08216dd7
RE
5749 break;
5750
5751 default:
5752 internal_error
5753 (__FILE__, __LINE__,
edefbb7c 5754 _("arm_store_return_value: Floating point model not supported"));
08216dd7
RE
5755 break;
5756 }
34e8f22d 5757 }
b508a996
RE
5758 else if (TYPE_CODE (type) == TYPE_CODE_INT
5759 || TYPE_CODE (type) == TYPE_CODE_CHAR
5760 || TYPE_CODE (type) == TYPE_CODE_BOOL
5761 || TYPE_CODE (type) == TYPE_CODE_PTR
5762 || TYPE_CODE (type) == TYPE_CODE_REF
5763 || TYPE_CODE (type) == TYPE_CODE_ENUM)
5764 {
5765 if (TYPE_LENGTH (type) <= 4)
5766 {
5767 /* Values of one word or less are zero/sign-extended and
5768 returned in r0. */
7a5ea0d4 5769 bfd_byte tmpbuf[INT_REGISTER_SIZE];
b508a996
RE
5770 LONGEST val = unpack_long (type, valbuf);
5771
e17a4113 5772 store_signed_integer (tmpbuf, INT_REGISTER_SIZE, byte_order, val);
b508a996
RE
5773 regcache_cooked_write (regs, ARM_A1_REGNUM, tmpbuf);
5774 }
5775 else
5776 {
5777 /* Integral values greater than one word are stored in consecutive
5778 registers starting with r0. This will always be a multiple of
5779 the regiser size. */
5780 int len = TYPE_LENGTH (type);
5781 int regno = ARM_A1_REGNUM;
5782
5783 while (len > 0)
5784 {
5785 regcache_cooked_write (regs, regno++, valbuf);
7a5ea0d4
DJ
5786 len -= INT_REGISTER_SIZE;
5787 valbuf += INT_REGISTER_SIZE;
b508a996
RE
5788 }
5789 }
5790 }
34e8f22d 5791 else
b508a996
RE
5792 {
5793 /* For a structure or union the behaviour is as if the value had
5794 been stored to word-aligned memory and then loaded into
5795 registers with 32-bit load instruction(s). */
5796 int len = TYPE_LENGTH (type);
5797 int regno = ARM_A1_REGNUM;
7a5ea0d4 5798 bfd_byte tmpbuf[INT_REGISTER_SIZE];
b508a996
RE
5799
5800 while (len > 0)
5801 {
5802 memcpy (tmpbuf, valbuf,
7a5ea0d4 5803 len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
b508a996 5804 regcache_cooked_write (regs, regno++, tmpbuf);
7a5ea0d4
DJ
5805 len -= INT_REGISTER_SIZE;
5806 valbuf += INT_REGISTER_SIZE;
b508a996
RE
5807 }
5808 }
34e8f22d
RE
5809}
5810
2af48f68
PB
5811
5812/* Handle function return values. */
5813
5814static enum return_value_convention
c055b101
CV
5815arm_return_value (struct gdbarch *gdbarch, struct type *func_type,
5816 struct type *valtype, struct regcache *regcache,
5817 gdb_byte *readbuf, const gdb_byte *writebuf)
2af48f68 5818{
7c00367c 5819 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
90445bd3
DJ
5820 enum arm_vfp_cprc_base_type vfp_base_type;
5821 int vfp_base_count;
5822
5823 if (arm_vfp_abi_for_function (gdbarch, func_type)
5824 && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
5825 {
5826 int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
5827 int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
5828 int i;
5829 for (i = 0; i < vfp_base_count; i++)
5830 {
58d6951d
DJ
5831 if (reg_char == 'q')
5832 {
5833 if (writebuf)
5834 arm_neon_quad_write (gdbarch, regcache, i,
5835 writebuf + i * unit_length);
5836
5837 if (readbuf)
5838 arm_neon_quad_read (gdbarch, regcache, i,
5839 readbuf + i * unit_length);
5840 }
5841 else
5842 {
5843 char name_buf[4];
5844 int regnum;
5845
5846 sprintf (name_buf, "%c%d", reg_char, i);
5847 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
5848 strlen (name_buf));
5849 if (writebuf)
5850 regcache_cooked_write (regcache, regnum,
5851 writebuf + i * unit_length);
5852 if (readbuf)
5853 regcache_cooked_read (regcache, regnum,
5854 readbuf + i * unit_length);
5855 }
90445bd3
DJ
5856 }
5857 return RETURN_VALUE_REGISTER_CONVENTION;
5858 }
7c00367c 5859
2af48f68
PB
5860 if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
5861 || TYPE_CODE (valtype) == TYPE_CODE_UNION
5862 || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
5863 {
7c00367c
MK
5864 if (tdep->struct_return == pcc_struct_return
5865 || arm_return_in_memory (gdbarch, valtype))
2af48f68
PB
5866 return RETURN_VALUE_STRUCT_CONVENTION;
5867 }
5868
5869 if (writebuf)
5870 arm_store_return_value (valtype, regcache, writebuf);
5871
5872 if (readbuf)
5873 arm_extract_return_value (valtype, regcache, readbuf);
5874
5875 return RETURN_VALUE_REGISTER_CONVENTION;
5876}
5877
5878
9df628e0 5879static int
60ade65d 5880arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
9df628e0 5881{
e17a4113
UW
5882 struct gdbarch *gdbarch = get_frame_arch (frame);
5883 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
5884 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
9df628e0 5885 CORE_ADDR jb_addr;
7a5ea0d4 5886 char buf[INT_REGISTER_SIZE];
9df628e0 5887
60ade65d 5888 jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
9df628e0
RE
5889
5890 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
7a5ea0d4 5891 INT_REGISTER_SIZE))
9df628e0
RE
5892 return 0;
5893
e17a4113 5894 *pc = extract_unsigned_integer (buf, INT_REGISTER_SIZE, byte_order);
9df628e0
RE
5895 return 1;
5896}
5897
faa95490
DJ
5898/* Recognize GCC and GNU ld's trampolines. If we are in a trampoline,
5899 return the target PC. Otherwise return 0. */
c906108c
SS
5900
5901CORE_ADDR
52f729a7 5902arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
c906108c 5903{
c5aa993b 5904 char *name;
faa95490 5905 int namelen;
c906108c
SS
5906 CORE_ADDR start_addr;
5907
5908 /* Find the starting address and name of the function containing the PC. */
5909 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
5910 return 0;
5911
faa95490
DJ
5912 /* If PC is in a Thumb call or return stub, return the address of the
5913 target PC, which is in a register. The thunk functions are called
5914 _call_via_xx, where x is the register name. The possible names
3d8d5e79
DJ
5915 are r0-r9, sl, fp, ip, sp, and lr. ARM RealView has similar
5916 functions, named __ARM_call_via_r[0-7]. */
5917 if (strncmp (name, "_call_via_", 10) == 0
5918 || strncmp (name, "__ARM_call_via_", strlen ("__ARM_call_via_")) == 0)
c906108c 5919 {
ed9a39eb
JM
5920 /* Use the name suffix to determine which register contains the
5921 target PC. */
c5aa993b
JM
5922 static char *table[15] =
5923 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
5924 "r8", "r9", "sl", "fp", "ip", "sp", "lr"
5925 };
c906108c 5926 int regno;
faa95490 5927 int offset = strlen (name) - 2;
c906108c
SS
5928
5929 for (regno = 0; regno <= 14; regno++)
faa95490 5930 if (strcmp (&name[offset], table[regno]) == 0)
52f729a7 5931 return get_frame_register_unsigned (frame, regno);
c906108c 5932 }
ed9a39eb 5933
faa95490
DJ
5934 /* GNU ld generates __foo_from_arm or __foo_from_thumb for
5935 non-interworking calls to foo. We could decode the stubs
5936 to find the target but it's easier to use the symbol table. */
5937 namelen = strlen (name);
5938 if (name[0] == '_' && name[1] == '_'
5939 && ((namelen > 2 + strlen ("_from_thumb")
5940 && strncmp (name + namelen - strlen ("_from_thumb"), "_from_thumb",
5941 strlen ("_from_thumb")) == 0)
5942 || (namelen > 2 + strlen ("_from_arm")
5943 && strncmp (name + namelen - strlen ("_from_arm"), "_from_arm",
5944 strlen ("_from_arm")) == 0)))
5945 {
5946 char *target_name;
5947 int target_len = namelen - 2;
5948 struct minimal_symbol *minsym;
5949 struct objfile *objfile;
5950 struct obj_section *sec;
5951
5952 if (name[namelen - 1] == 'b')
5953 target_len -= strlen ("_from_thumb");
5954 else
5955 target_len -= strlen ("_from_arm");
5956
5957 target_name = alloca (target_len + 1);
5958 memcpy (target_name, name + 2, target_len);
5959 target_name[target_len] = '\0';
5960
5961 sec = find_pc_section (pc);
5962 objfile = (sec == NULL) ? NULL : sec->objfile;
5963 minsym = lookup_minimal_symbol (target_name, NULL, objfile);
5964 if (minsym != NULL)
5965 return SYMBOL_VALUE_ADDRESS (minsym);
5966 else
5967 return 0;
5968 }
5969
c5aa993b 5970 return 0; /* not a stub */
c906108c
SS
5971}
5972
afd7eef0
RE
5973static void
5974set_arm_command (char *args, int from_tty)
5975{
edefbb7c
AC
5976 printf_unfiltered (_("\
5977\"set arm\" must be followed by an apporpriate subcommand.\n"));
afd7eef0
RE
5978 help_list (setarmcmdlist, "set arm ", all_commands, gdb_stdout);
5979}
5980
5981static void
5982show_arm_command (char *args, int from_tty)
5983{
26304000 5984 cmd_show_list (showarmcmdlist, from_tty, "");
afd7eef0
RE
5985}
5986
28e97307
DJ
5987static void
5988arm_update_current_architecture (void)
fd50bc42 5989{
28e97307 5990 struct gdbarch_info info;
fd50bc42 5991
28e97307 5992 /* If the current architecture is not ARM, we have nothing to do. */
1cf3db46 5993 if (gdbarch_bfd_arch_info (target_gdbarch)->arch != bfd_arch_arm)
28e97307 5994 return;
fd50bc42 5995
28e97307
DJ
5996 /* Update the architecture. */
5997 gdbarch_info_init (&info);
fd50bc42 5998
28e97307
DJ
5999 if (!gdbarch_update_p (info))
6000 internal_error (__FILE__, __LINE__, "could not update architecture");
fd50bc42
RE
6001}
6002
6003static void
6004set_fp_model_sfunc (char *args, int from_tty,
6005 struct cmd_list_element *c)
6006{
6007 enum arm_float_model fp_model;
6008
6009 for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
6010 if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
6011 {
6012 arm_fp_model = fp_model;
6013 break;
6014 }
6015
6016 if (fp_model == ARM_FLOAT_LAST)
edefbb7c 6017 internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
fd50bc42
RE
6018 current_fp_model);
6019
28e97307 6020 arm_update_current_architecture ();
fd50bc42
RE
6021}
6022
6023static void
08546159
AC
6024show_fp_model (struct ui_file *file, int from_tty,
6025 struct cmd_list_element *c, const char *value)
fd50bc42 6026{
1cf3db46 6027 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
fd50bc42 6028
28e97307 6029 if (arm_fp_model == ARM_FLOAT_AUTO
1cf3db46 6030 && gdbarch_bfd_arch_info (target_gdbarch)->arch == bfd_arch_arm)
28e97307
DJ
6031 fprintf_filtered (file, _("\
6032The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
6033 fp_model_strings[tdep->fp_model]);
6034 else
6035 fprintf_filtered (file, _("\
6036The current ARM floating point model is \"%s\".\n"),
6037 fp_model_strings[arm_fp_model]);
6038}
6039
6040static void
6041arm_set_abi (char *args, int from_tty,
6042 struct cmd_list_element *c)
6043{
6044 enum arm_abi_kind arm_abi;
6045
6046 for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
6047 if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
6048 {
6049 arm_abi_global = arm_abi;
6050 break;
6051 }
6052
6053 if (arm_abi == ARM_ABI_LAST)
6054 internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
6055 arm_abi_string);
6056
6057 arm_update_current_architecture ();
6058}
6059
6060static void
6061arm_show_abi (struct ui_file *file, int from_tty,
6062 struct cmd_list_element *c, const char *value)
6063{
1cf3db46 6064 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
28e97307
DJ
6065
6066 if (arm_abi_global == ARM_ABI_AUTO
1cf3db46 6067 && gdbarch_bfd_arch_info (target_gdbarch)->arch == bfd_arch_arm)
28e97307
DJ
6068 fprintf_filtered (file, _("\
6069The current ARM ABI is \"auto\" (currently \"%s\").\n"),
6070 arm_abi_strings[tdep->arm_abi]);
6071 else
6072 fprintf_filtered (file, _("The current ARM ABI is \"%s\".\n"),
6073 arm_abi_string);
fd50bc42
RE
6074}
6075
0428b8f5
DJ
6076static void
6077arm_show_fallback_mode (struct ui_file *file, int from_tty,
6078 struct cmd_list_element *c, const char *value)
6079{
1cf3db46 6080 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
0428b8f5
DJ
6081
6082 fprintf_filtered (file, _("\
6083The current execution mode assumed (when symbols are unavailable) is \"%s\".\n"),
6084 arm_fallback_mode_string);
6085}
6086
6087static void
6088arm_show_force_mode (struct ui_file *file, int from_tty,
6089 struct cmd_list_element *c, const char *value)
6090{
1cf3db46 6091 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
0428b8f5
DJ
6092
6093 fprintf_filtered (file, _("\
6094The current execution mode assumed (even when symbols are available) is \"%s\".\n"),
6095 arm_force_mode_string);
6096}
6097
afd7eef0
RE
6098/* If the user changes the register disassembly style used for info
6099 register and other commands, we have to also switch the style used
6100 in opcodes for disassembly output. This function is run in the "set
6101 arm disassembly" command, and does that. */
bc90b915
FN
6102
6103static void
afd7eef0 6104set_disassembly_style_sfunc (char *args, int from_tty,
bc90b915
FN
6105 struct cmd_list_element *c)
6106{
afd7eef0 6107 set_disassembly_style ();
bc90b915
FN
6108}
6109\f
966fbf70 6110/* Return the ARM register name corresponding to register I. */
a208b0cb 6111static const char *
d93859e2 6112arm_register_name (struct gdbarch *gdbarch, int i)
966fbf70 6113{
58d6951d
DJ
6114 const int num_regs = gdbarch_num_regs (gdbarch);
6115
6116 if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
6117 && i >= num_regs && i < num_regs + 32)
6118 {
6119 static const char *const vfp_pseudo_names[] = {
6120 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
6121 "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
6122 "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
6123 "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
6124 };
6125
6126 return vfp_pseudo_names[i - num_regs];
6127 }
6128
6129 if (gdbarch_tdep (gdbarch)->have_neon_pseudos
6130 && i >= num_regs + 32 && i < num_regs + 32 + 16)
6131 {
6132 static const char *const neon_pseudo_names[] = {
6133 "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
6134 "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
6135 };
6136
6137 return neon_pseudo_names[i - num_regs - 32];
6138 }
6139
ff6f572f
DJ
6140 if (i >= ARRAY_SIZE (arm_register_names))
6141 /* These registers are only supported on targets which supply
6142 an XML description. */
6143 return "";
6144
966fbf70
RE
6145 return arm_register_names[i];
6146}
6147
bc90b915 6148static void
afd7eef0 6149set_disassembly_style (void)
bc90b915 6150{
123dc839 6151 int current;
bc90b915 6152
123dc839
DJ
6153 /* Find the style that the user wants. */
6154 for (current = 0; current < num_disassembly_options; current++)
6155 if (disassembly_style == valid_disassembly_styles[current])
6156 break;
6157 gdb_assert (current < num_disassembly_options);
bc90b915 6158
94c30b78 6159 /* Synchronize the disassembler. */
bc90b915
FN
6160 set_arm_regname_option (current);
6161}
6162
082fc60d
RE
6163/* Test whether the coff symbol specific value corresponds to a Thumb
6164 function. */
6165
6166static int
6167coff_sym_is_thumb (int val)
6168{
f8bf5763
PM
6169 return (val == C_THUMBEXT
6170 || val == C_THUMBSTAT
6171 || val == C_THUMBEXTFUNC
6172 || val == C_THUMBSTATFUNC
6173 || val == C_THUMBLABEL);
082fc60d
RE
6174}
6175
6176/* arm_coff_make_msymbol_special()
6177 arm_elf_make_msymbol_special()
6178
6179 These functions test whether the COFF or ELF symbol corresponds to
6180 an address in thumb code, and set a "special" bit in a minimal
6181 symbol to indicate that it does. */
6182
34e8f22d 6183static void
082fc60d
RE
6184arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
6185{
6186 /* Thumb symbols are of type STT_LOPROC, (synonymous with
6187 STT_ARM_TFUNC). */
6188 if (ELF_ST_TYPE (((elf_symbol_type *)sym)->internal_elf_sym.st_info)
6189 == STT_LOPROC)
6190 MSYMBOL_SET_SPECIAL (msym);
6191}
6192
34e8f22d 6193static void
082fc60d
RE
6194arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
6195{
6196 if (coff_sym_is_thumb (val))
6197 MSYMBOL_SET_SPECIAL (msym);
6198}
6199
60c5725c 6200static void
c1bd65d0 6201arm_objfile_data_free (struct objfile *objfile, void *arg)
60c5725c
DJ
6202{
6203 struct arm_per_objfile *data = arg;
6204 unsigned int i;
6205
6206 for (i = 0; i < objfile->obfd->section_count; i++)
6207 VEC_free (arm_mapping_symbol_s, data->section_maps[i]);
6208}
6209
6210static void
6211arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
6212 asymbol *sym)
6213{
6214 const char *name = bfd_asymbol_name (sym);
6215 struct arm_per_objfile *data;
6216 VEC(arm_mapping_symbol_s) **map_p;
6217 struct arm_mapping_symbol new_map_sym;
6218
6219 gdb_assert (name[0] == '$');
6220 if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
6221 return;
6222
6223 data = objfile_data (objfile, arm_objfile_data_key);
6224 if (data == NULL)
6225 {
6226 data = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6227 struct arm_per_objfile);
6228 set_objfile_data (objfile, arm_objfile_data_key, data);
6229 data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
6230 objfile->obfd->section_count,
6231 VEC(arm_mapping_symbol_s) *);
6232 }
6233 map_p = &data->section_maps[bfd_get_section (sym)->index];
6234
6235 new_map_sym.value = sym->value;
6236 new_map_sym.type = name[1];
6237
6238 /* Assume that most mapping symbols appear in order of increasing
6239 value. If they were randomly distributed, it would be faster to
6240 always push here and then sort at first use. */
6241 if (!VEC_empty (arm_mapping_symbol_s, *map_p))
6242 {
6243 struct arm_mapping_symbol *prev_map_sym;
6244
6245 prev_map_sym = VEC_last (arm_mapping_symbol_s, *map_p);
6246 if (prev_map_sym->value >= sym->value)
6247 {
6248 unsigned int idx;
6249 idx = VEC_lower_bound (arm_mapping_symbol_s, *map_p, &new_map_sym,
6250 arm_compare_mapping_symbols);
6251 VEC_safe_insert (arm_mapping_symbol_s, *map_p, idx, &new_map_sym);
6252 return;
6253 }
6254 }
6255
6256 VEC_safe_push (arm_mapping_symbol_s, *map_p, &new_map_sym);
6257}
6258
756fe439 6259static void
61a1198a 6260arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
756fe439 6261{
9779414d 6262 struct gdbarch *gdbarch = get_regcache_arch (regcache);
61a1198a 6263 regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
756fe439
DJ
6264
6265 /* If necessary, set the T bit. */
6266 if (arm_apcs_32)
6267 {
9779414d 6268 ULONGEST val, t_bit;
61a1198a 6269 regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
9779414d
DJ
6270 t_bit = arm_psr_thumb_bit (gdbarch);
6271 if (arm_pc_is_thumb (gdbarch, pc))
6272 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
6273 val | t_bit);
756fe439 6274 else
61a1198a 6275 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9779414d 6276 val & ~t_bit);
756fe439
DJ
6277 }
6278}
123dc839 6279
58d6951d
DJ
6280/* Read the contents of a NEON quad register, by reading from two
6281 double registers. This is used to implement the quad pseudo
6282 registers, and for argument passing in case the quad registers are
6283 missing; vectors are passed in quad registers when using the VFP
6284 ABI, even if a NEON unit is not present. REGNUM is the index of
6285 the quad register, in [0, 15]. */
6286
6287static void
6288arm_neon_quad_read (struct gdbarch *gdbarch, struct regcache *regcache,
6289 int regnum, gdb_byte *buf)
6290{
6291 char name_buf[4];
6292 gdb_byte reg_buf[8];
6293 int offset, double_regnum;
6294
6295 sprintf (name_buf, "d%d", regnum << 1);
6296 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
6297 strlen (name_buf));
6298
6299 /* d0 is always the least significant half of q0. */
6300 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
6301 offset = 8;
6302 else
6303 offset = 0;
6304
6305 regcache_raw_read (regcache, double_regnum, reg_buf);
6306 memcpy (buf + offset, reg_buf, 8);
6307
6308 offset = 8 - offset;
6309 regcache_raw_read (regcache, double_regnum + 1, reg_buf);
6310 memcpy (buf + offset, reg_buf, 8);
6311}
6312
6313static void
6314arm_pseudo_read (struct gdbarch *gdbarch, struct regcache *regcache,
6315 int regnum, gdb_byte *buf)
6316{
6317 const int num_regs = gdbarch_num_regs (gdbarch);
6318 char name_buf[4];
6319 gdb_byte reg_buf[8];
6320 int offset, double_regnum;
6321
6322 gdb_assert (regnum >= num_regs);
6323 regnum -= num_regs;
6324
6325 if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
6326 /* Quad-precision register. */
6327 arm_neon_quad_read (gdbarch, regcache, regnum - 32, buf);
6328 else
6329 {
6330 /* Single-precision register. */
6331 gdb_assert (regnum < 32);
6332
6333 /* s0 is always the least significant half of d0. */
6334 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
6335 offset = (regnum & 1) ? 0 : 4;
6336 else
6337 offset = (regnum & 1) ? 4 : 0;
6338
6339 sprintf (name_buf, "d%d", regnum >> 1);
6340 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
6341 strlen (name_buf));
6342
6343 regcache_raw_read (regcache, double_regnum, reg_buf);
6344 memcpy (buf, reg_buf + offset, 4);
6345 }
6346}
6347
6348/* Store the contents of BUF to a NEON quad register, by writing to
6349 two double registers. This is used to implement the quad pseudo
6350 registers, and for argument passing in case the quad registers are
6351 missing; vectors are passed in quad registers when using the VFP
6352 ABI, even if a NEON unit is not present. REGNUM is the index
6353 of the quad register, in [0, 15]. */
6354
6355static void
6356arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
6357 int regnum, const gdb_byte *buf)
6358{
6359 char name_buf[4];
6360 gdb_byte reg_buf[8];
6361 int offset, double_regnum;
6362
6363 sprintf (name_buf, "d%d", regnum << 1);
6364 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
6365 strlen (name_buf));
6366
6367 /* d0 is always the least significant half of q0. */
6368 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
6369 offset = 8;
6370 else
6371 offset = 0;
6372
6373 regcache_raw_write (regcache, double_regnum, buf + offset);
6374 offset = 8 - offset;
6375 regcache_raw_write (regcache, double_regnum + 1, buf + offset);
6376}
6377
6378static void
6379arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
6380 int regnum, const gdb_byte *buf)
6381{
6382 const int num_regs = gdbarch_num_regs (gdbarch);
6383 char name_buf[4];
6384 gdb_byte reg_buf[8];
6385 int offset, double_regnum;
6386
6387 gdb_assert (regnum >= num_regs);
6388 regnum -= num_regs;
6389
6390 if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
6391 /* Quad-precision register. */
6392 arm_neon_quad_write (gdbarch, regcache, regnum - 32, buf);
6393 else
6394 {
6395 /* Single-precision register. */
6396 gdb_assert (regnum < 32);
6397
6398 /* s0 is always the least significant half of d0. */
6399 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
6400 offset = (regnum & 1) ? 0 : 4;
6401 else
6402 offset = (regnum & 1) ? 4 : 0;
6403
6404 sprintf (name_buf, "d%d", regnum >> 1);
6405 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
6406 strlen (name_buf));
6407
6408 regcache_raw_read (regcache, double_regnum, reg_buf);
6409 memcpy (reg_buf + offset, buf, 4);
6410 regcache_raw_write (regcache, double_regnum, reg_buf);
6411 }
6412}
6413
123dc839
DJ
6414static struct value *
6415value_of_arm_user_reg (struct frame_info *frame, const void *baton)
6416{
6417 const int *reg_p = baton;
6418 return value_of_register (*reg_p, frame);
6419}
97e03143 6420\f
70f80edf
JT
6421static enum gdb_osabi
6422arm_elf_osabi_sniffer (bfd *abfd)
97e03143 6423{
2af48f68 6424 unsigned int elfosabi;
70f80edf 6425 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
97e03143 6426
70f80edf 6427 elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
97e03143 6428
28e97307
DJ
6429 if (elfosabi == ELFOSABI_ARM)
6430 /* GNU tools use this value. Check note sections in this case,
6431 as well. */
6432 bfd_map_over_sections (abfd,
6433 generic_elf_osabi_sniff_abi_tag_sections,
6434 &osabi);
97e03143 6435
28e97307 6436 /* Anything else will be handled by the generic ELF sniffer. */
70f80edf 6437 return osabi;
97e03143
RE
6438}
6439
70f80edf 6440\f
da3c6d4a
MS
6441/* Initialize the current architecture based on INFO. If possible,
6442 re-use an architecture from ARCHES, which is a list of
6443 architectures already created during this debugging session.
97e03143 6444
da3c6d4a
MS
6445 Called e.g. at program startup, when reading a core file, and when
6446 reading a binary file. */
97e03143 6447
39bbf761
RE
6448static struct gdbarch *
6449arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
6450{
97e03143 6451 struct gdbarch_tdep *tdep;
39bbf761 6452 struct gdbarch *gdbarch;
28e97307
DJ
6453 struct gdbarch_list *best_arch;
6454 enum arm_abi_kind arm_abi = arm_abi_global;
6455 enum arm_float_model fp_model = arm_fp_model;
123dc839 6456 struct tdesc_arch_data *tdesc_data = NULL;
9779414d 6457 int i, is_m = 0;
58d6951d
DJ
6458 int have_vfp_registers = 0, have_vfp_pseudos = 0, have_neon_pseudos = 0;
6459 int have_neon = 0;
ff6f572f 6460 int have_fpa_registers = 1;
9779414d
DJ
6461 const struct target_desc *tdesc = info.target_desc;
6462
6463 /* If we have an object to base this architecture on, try to determine
6464 its ABI. */
6465
6466 if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
6467 {
6468 int ei_osabi, e_flags;
6469
6470 switch (bfd_get_flavour (info.abfd))
6471 {
6472 case bfd_target_aout_flavour:
6473 /* Assume it's an old APCS-style ABI. */
6474 arm_abi = ARM_ABI_APCS;
6475 break;
6476
6477 case bfd_target_coff_flavour:
6478 /* Assume it's an old APCS-style ABI. */
6479 /* XXX WinCE? */
6480 arm_abi = ARM_ABI_APCS;
6481 break;
6482
6483 case bfd_target_elf_flavour:
6484 ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
6485 e_flags = elf_elfheader (info.abfd)->e_flags;
6486
6487 if (ei_osabi == ELFOSABI_ARM)
6488 {
6489 /* GNU tools used to use this value, but do not for EABI
6490 objects. There's nowhere to tag an EABI version
6491 anyway, so assume APCS. */
6492 arm_abi = ARM_ABI_APCS;
6493 }
6494 else if (ei_osabi == ELFOSABI_NONE)
6495 {
6496 int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
6497 int attr_arch, attr_profile;
6498
6499 switch (eabi_ver)
6500 {
6501 case EF_ARM_EABI_UNKNOWN:
6502 /* Assume GNU tools. */
6503 arm_abi = ARM_ABI_APCS;
6504 break;
6505
6506 case EF_ARM_EABI_VER4:
6507 case EF_ARM_EABI_VER5:
6508 arm_abi = ARM_ABI_AAPCS;
6509 /* EABI binaries default to VFP float ordering.
6510 They may also contain build attributes that can
6511 be used to identify if the VFP argument-passing
6512 ABI is in use. */
6513 if (fp_model == ARM_FLOAT_AUTO)
6514 {
6515#ifdef HAVE_ELF
6516 switch (bfd_elf_get_obj_attr_int (info.abfd,
6517 OBJ_ATTR_PROC,
6518 Tag_ABI_VFP_args))
6519 {
6520 case 0:
6521 /* "The user intended FP parameter/result
6522 passing to conform to AAPCS, base
6523 variant". */
6524 fp_model = ARM_FLOAT_SOFT_VFP;
6525 break;
6526 case 1:
6527 /* "The user intended FP parameter/result
6528 passing to conform to AAPCS, VFP
6529 variant". */
6530 fp_model = ARM_FLOAT_VFP;
6531 break;
6532 case 2:
6533 /* "The user intended FP parameter/result
6534 passing to conform to tool chain-specific
6535 conventions" - we don't know any such
6536 conventions, so leave it as "auto". */
6537 break;
6538 default:
6539 /* Attribute value not mentioned in the
6540 October 2008 ABI, so leave it as
6541 "auto". */
6542 break;
6543 }
6544#else
6545 fp_model = ARM_FLOAT_SOFT_VFP;
6546#endif
6547 }
6548 break;
6549
6550 default:
6551 /* Leave it as "auto". */
6552 warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
6553 break;
6554 }
6555
6556#ifdef HAVE_ELF
6557 /* Detect M-profile programs. This only works if the
6558 executable file includes build attributes; GCC does
6559 copy them to the executable, but e.g. RealView does
6560 not. */
6561 attr_arch = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
6562 Tag_CPU_arch);
6563 attr_profile = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
6564 Tag_CPU_arch_profile);
6565 /* GCC specifies the profile for v6-M; RealView only
6566 specifies the profile for architectures starting with
6567 V7 (as opposed to architectures with a tag
6568 numerically greater than TAG_CPU_ARCH_V7). */
6569 if (!tdesc_has_registers (tdesc)
6570 && (attr_arch == TAG_CPU_ARCH_V6_M
6571 || attr_arch == TAG_CPU_ARCH_V6S_M
6572 || attr_profile == 'M'))
6573 tdesc = tdesc_arm_with_m;
6574#endif
6575 }
6576
6577 if (fp_model == ARM_FLOAT_AUTO)
6578 {
6579 int e_flags = elf_elfheader (info.abfd)->e_flags;
6580
6581 switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
6582 {
6583 case 0:
6584 /* Leave it as "auto". Strictly speaking this case
6585 means FPA, but almost nobody uses that now, and
6586 many toolchains fail to set the appropriate bits
6587 for the floating-point model they use. */
6588 break;
6589 case EF_ARM_SOFT_FLOAT:
6590 fp_model = ARM_FLOAT_SOFT_FPA;
6591 break;
6592 case EF_ARM_VFP_FLOAT:
6593 fp_model = ARM_FLOAT_VFP;
6594 break;
6595 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
6596 fp_model = ARM_FLOAT_SOFT_VFP;
6597 break;
6598 }
6599 }
6600
6601 if (e_flags & EF_ARM_BE8)
6602 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
6603
6604 break;
6605
6606 default:
6607 /* Leave it as "auto". */
6608 break;
6609 }
6610 }
123dc839
DJ
6611
6612 /* Check any target description for validity. */
9779414d 6613 if (tdesc_has_registers (tdesc))
123dc839
DJ
6614 {
6615 /* For most registers we require GDB's default names; but also allow
6616 the numeric names for sp / lr / pc, as a convenience. */
6617 static const char *const arm_sp_names[] = { "r13", "sp", NULL };
6618 static const char *const arm_lr_names[] = { "r14", "lr", NULL };
6619 static const char *const arm_pc_names[] = { "r15", "pc", NULL };
6620
6621 const struct tdesc_feature *feature;
58d6951d 6622 int valid_p;
123dc839 6623
9779414d 6624 feature = tdesc_find_feature (tdesc,
123dc839
DJ
6625 "org.gnu.gdb.arm.core");
6626 if (feature == NULL)
9779414d
DJ
6627 {
6628 feature = tdesc_find_feature (tdesc,
6629 "org.gnu.gdb.arm.m-profile");
6630 if (feature == NULL)
6631 return NULL;
6632 else
6633 is_m = 1;
6634 }
123dc839
DJ
6635
6636 tdesc_data = tdesc_data_alloc ();
6637
6638 valid_p = 1;
6639 for (i = 0; i < ARM_SP_REGNUM; i++)
6640 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
6641 arm_register_names[i]);
6642 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
6643 ARM_SP_REGNUM,
6644 arm_sp_names);
6645 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
6646 ARM_LR_REGNUM,
6647 arm_lr_names);
6648 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
6649 ARM_PC_REGNUM,
6650 arm_pc_names);
9779414d
DJ
6651 if (is_m)
6652 valid_p &= tdesc_numbered_register (feature, tdesc_data,
6653 ARM_PS_REGNUM, "xpsr");
6654 else
6655 valid_p &= tdesc_numbered_register (feature, tdesc_data,
6656 ARM_PS_REGNUM, "cpsr");
123dc839
DJ
6657
6658 if (!valid_p)
6659 {
6660 tdesc_data_cleanup (tdesc_data);
6661 return NULL;
6662 }
6663
9779414d 6664 feature = tdesc_find_feature (tdesc,
123dc839
DJ
6665 "org.gnu.gdb.arm.fpa");
6666 if (feature != NULL)
6667 {
6668 valid_p = 1;
6669 for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
6670 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
6671 arm_register_names[i]);
6672 if (!valid_p)
6673 {
6674 tdesc_data_cleanup (tdesc_data);
6675 return NULL;
6676 }
6677 }
ff6f572f
DJ
6678 else
6679 have_fpa_registers = 0;
6680
9779414d 6681 feature = tdesc_find_feature (tdesc,
ff6f572f
DJ
6682 "org.gnu.gdb.xscale.iwmmxt");
6683 if (feature != NULL)
6684 {
6685 static const char *const iwmmxt_names[] = {
6686 "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
6687 "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
6688 "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
6689 "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
6690 };
6691
6692 valid_p = 1;
6693 for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
6694 valid_p
6695 &= tdesc_numbered_register (feature, tdesc_data, i,
6696 iwmmxt_names[i - ARM_WR0_REGNUM]);
6697
6698 /* Check for the control registers, but do not fail if they
6699 are missing. */
6700 for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
6701 tdesc_numbered_register (feature, tdesc_data, i,
6702 iwmmxt_names[i - ARM_WR0_REGNUM]);
6703
6704 for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
6705 valid_p
6706 &= tdesc_numbered_register (feature, tdesc_data, i,
6707 iwmmxt_names[i - ARM_WR0_REGNUM]);
6708
6709 if (!valid_p)
6710 {
6711 tdesc_data_cleanup (tdesc_data);
6712 return NULL;
6713 }
6714 }
58d6951d
DJ
6715
6716 /* If we have a VFP unit, check whether the single precision registers
6717 are present. If not, then we will synthesize them as pseudo
6718 registers. */
9779414d 6719 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
6720 "org.gnu.gdb.arm.vfp");
6721 if (feature != NULL)
6722 {
6723 static const char *const vfp_double_names[] = {
6724 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
6725 "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
6726 "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
6727 "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
6728 };
6729
6730 /* Require the double precision registers. There must be either
6731 16 or 32. */
6732 valid_p = 1;
6733 for (i = 0; i < 32; i++)
6734 {
6735 valid_p &= tdesc_numbered_register (feature, tdesc_data,
6736 ARM_D0_REGNUM + i,
6737 vfp_double_names[i]);
6738 if (!valid_p)
6739 break;
6740 }
6741
6742 if (!valid_p && i != 16)
6743 {
6744 tdesc_data_cleanup (tdesc_data);
6745 return NULL;
6746 }
6747
6748 if (tdesc_unnumbered_register (feature, "s0") == 0)
6749 have_vfp_pseudos = 1;
6750
6751 have_vfp_registers = 1;
6752
6753 /* If we have VFP, also check for NEON. The architecture allows
6754 NEON without VFP (integer vector operations only), but GDB
6755 does not support that. */
9779414d 6756 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
6757 "org.gnu.gdb.arm.neon");
6758 if (feature != NULL)
6759 {
6760 /* NEON requires 32 double-precision registers. */
6761 if (i != 32)
6762 {
6763 tdesc_data_cleanup (tdesc_data);
6764 return NULL;
6765 }
6766
6767 /* If there are quad registers defined by the stub, use
6768 their type; otherwise (normally) provide them with
6769 the default type. */
6770 if (tdesc_unnumbered_register (feature, "q0") == 0)
6771 have_neon_pseudos = 1;
6772
6773 have_neon = 1;
6774 }
6775 }
123dc839 6776 }
39bbf761 6777
28e97307
DJ
6778 /* If there is already a candidate, use it. */
6779 for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
6780 best_arch != NULL;
6781 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
6782 {
b8926edc
DJ
6783 if (arm_abi != ARM_ABI_AUTO
6784 && arm_abi != gdbarch_tdep (best_arch->gdbarch)->arm_abi)
28e97307
DJ
6785 continue;
6786
b8926edc
DJ
6787 if (fp_model != ARM_FLOAT_AUTO
6788 && fp_model != gdbarch_tdep (best_arch->gdbarch)->fp_model)
28e97307
DJ
6789 continue;
6790
58d6951d
DJ
6791 /* There are various other properties in tdep that we do not
6792 need to check here: those derived from a target description,
6793 since gdbarches with a different target description are
6794 automatically disqualified. */
6795
9779414d
DJ
6796 /* Do check is_m, though, since it might come from the binary. */
6797 if (is_m != gdbarch_tdep (best_arch->gdbarch)->is_m)
6798 continue;
6799
28e97307
DJ
6800 /* Found a match. */
6801 break;
6802 }
97e03143 6803
28e97307 6804 if (best_arch != NULL)
123dc839
DJ
6805 {
6806 if (tdesc_data != NULL)
6807 tdesc_data_cleanup (tdesc_data);
6808 return best_arch->gdbarch;
6809 }
28e97307
DJ
6810
6811 tdep = xcalloc (1, sizeof (struct gdbarch_tdep));
97e03143
RE
6812 gdbarch = gdbarch_alloc (&info, tdep);
6813
28e97307
DJ
6814 /* Record additional information about the architecture we are defining.
6815 These are gdbarch discriminators, like the OSABI. */
6816 tdep->arm_abi = arm_abi;
6817 tdep->fp_model = fp_model;
9779414d 6818 tdep->is_m = is_m;
ff6f572f 6819 tdep->have_fpa_registers = have_fpa_registers;
58d6951d
DJ
6820 tdep->have_vfp_registers = have_vfp_registers;
6821 tdep->have_vfp_pseudos = have_vfp_pseudos;
6822 tdep->have_neon_pseudos = have_neon_pseudos;
6823 tdep->have_neon = have_neon;
08216dd7
RE
6824
6825 /* Breakpoints. */
9d4fde75 6826 switch (info.byte_order_for_code)
67255d04
RE
6827 {
6828 case BFD_ENDIAN_BIG:
66e810cd
RE
6829 tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
6830 tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
6831 tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
6832 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
6833
67255d04
RE
6834 break;
6835
6836 case BFD_ENDIAN_LITTLE:
66e810cd
RE
6837 tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
6838 tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
6839 tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
6840 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
6841
67255d04
RE
6842 break;
6843
6844 default:
6845 internal_error (__FILE__, __LINE__,
edefbb7c 6846 _("arm_gdbarch_init: bad byte order for float format"));
67255d04
RE
6847 }
6848
d7b486e7
RE
6849 /* On ARM targets char defaults to unsigned. */
6850 set_gdbarch_char_signed (gdbarch, 0);
6851
cca44b1b
JB
6852 /* Note: for displaced stepping, this includes the breakpoint, and one word
6853 of additional scratch space. This setting isn't used for anything beside
6854 displaced stepping at present. */
6855 set_gdbarch_max_insn_length (gdbarch, 4 * DISPLACED_MODIFIED_INSNS);
6856
9df628e0 6857 /* This should be low enough for everything. */
97e03143 6858 tdep->lowest_pc = 0x20;
94c30b78 6859 tdep->jb_pc = -1; /* Longjump support not enabled by default. */
97e03143 6860
7c00367c
MK
6861 /* The default, for both APCS and AAPCS, is to return small
6862 structures in registers. */
6863 tdep->struct_return = reg_struct_return;
6864
2dd604e7 6865 set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
f53f0d0b 6866 set_gdbarch_frame_align (gdbarch, arm_frame_align);
39bbf761 6867
756fe439
DJ
6868 set_gdbarch_write_pc (gdbarch, arm_write_pc);
6869
148754e5 6870 /* Frame handling. */
a262aec2 6871 set_gdbarch_dummy_id (gdbarch, arm_dummy_id);
eb5492fa
DJ
6872 set_gdbarch_unwind_pc (gdbarch, arm_unwind_pc);
6873 set_gdbarch_unwind_sp (gdbarch, arm_unwind_sp);
6874
eb5492fa 6875 frame_base_set_default (gdbarch, &arm_normal_base);
148754e5 6876
34e8f22d
RE
6877 /* Address manipulation. */
6878 set_gdbarch_smash_text_address (gdbarch, arm_smash_text_address);
6879 set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
6880
34e8f22d
RE
6881 /* Advance PC across function entry code. */
6882 set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
6883
190dce09
UW
6884 /* Skip trampolines. */
6885 set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
6886
34e8f22d
RE
6887 /* The stack grows downward. */
6888 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
6889
6890 /* Breakpoint manipulation. */
6891 set_gdbarch_breakpoint_from_pc (gdbarch, arm_breakpoint_from_pc);
177321bd
DJ
6892 set_gdbarch_remote_breakpoint_from_pc (gdbarch,
6893 arm_remote_breakpoint_from_pc);
34e8f22d
RE
6894
6895 /* Information about registers, etc. */
0ba6dca9 6896 set_gdbarch_deprecated_fp_regnum (gdbarch, ARM_FP_REGNUM); /* ??? */
34e8f22d
RE
6897 set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
6898 set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
ff6f572f 6899 set_gdbarch_num_regs (gdbarch, ARM_NUM_REGS);
7a5ea0d4 6900 set_gdbarch_register_type (gdbarch, arm_register_type);
34e8f22d 6901
ff6f572f
DJ
6902 /* This "info float" is FPA-specific. Use the generic version if we
6903 do not have FPA. */
6904 if (gdbarch_tdep (gdbarch)->have_fpa_registers)
6905 set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
6906
26216b98 6907 /* Internal <-> external register number maps. */
ff6f572f 6908 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
26216b98
AC
6909 set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
6910
34e8f22d
RE
6911 set_gdbarch_register_name (gdbarch, arm_register_name);
6912
6913 /* Returning results. */
2af48f68 6914 set_gdbarch_return_value (gdbarch, arm_return_value);
34e8f22d 6915
03d48a7d
RE
6916 /* Disassembly. */
6917 set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
6918
34e8f22d
RE
6919 /* Minsymbol frobbing. */
6920 set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
6921 set_gdbarch_coff_make_msymbol_special (gdbarch,
6922 arm_coff_make_msymbol_special);
60c5725c 6923 set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
34e8f22d 6924
f9d67f43
DJ
6925 /* Thumb-2 IT block support. */
6926 set_gdbarch_adjust_breakpoint_address (gdbarch,
6927 arm_adjust_breakpoint_address);
6928
0d5de010
DJ
6929 /* Virtual tables. */
6930 set_gdbarch_vbit_in_delta (gdbarch, 1);
6931
97e03143 6932 /* Hook in the ABI-specific overrides, if they have been registered. */
4be87837 6933 gdbarch_init_osabi (info, gdbarch);
97e03143 6934
b39cc962
DJ
6935 dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
6936
eb5492fa 6937 /* Add some default predicates. */
a262aec2
DJ
6938 frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
6939 dwarf2_append_unwinders (gdbarch);
6940 frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
eb5492fa 6941
97e03143
RE
6942 /* Now we have tuned the configuration, set a few final things,
6943 based on what the OS ABI has told us. */
6944
b8926edc
DJ
6945 /* If the ABI is not otherwise marked, assume the old GNU APCS. EABI
6946 binaries are always marked. */
6947 if (tdep->arm_abi == ARM_ABI_AUTO)
6948 tdep->arm_abi = ARM_ABI_APCS;
6949
6950 /* We used to default to FPA for generic ARM, but almost nobody
6951 uses that now, and we now provide a way for the user to force
6952 the model. So default to the most useful variant. */
6953 if (tdep->fp_model == ARM_FLOAT_AUTO)
6954 tdep->fp_model = ARM_FLOAT_SOFT_FPA;
6955
9df628e0
RE
6956 if (tdep->jb_pc >= 0)
6957 set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
6958
08216dd7 6959 /* Floating point sizes and format. */
8da61cc4 6960 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
b8926edc 6961 if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
08216dd7 6962 {
8da61cc4
DJ
6963 set_gdbarch_double_format
6964 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
6965 set_gdbarch_long_double_format
6966 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
6967 }
6968 else
6969 {
6970 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
6971 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
08216dd7
RE
6972 }
6973
58d6951d
DJ
6974 if (have_vfp_pseudos)
6975 {
6976 /* NOTE: These are the only pseudo registers used by
6977 the ARM target at the moment. If more are added, a
6978 little more care in numbering will be needed. */
6979
6980 int num_pseudos = 32;
6981 if (have_neon_pseudos)
6982 num_pseudos += 16;
6983 set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
6984 set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
6985 set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
6986 }
6987
123dc839 6988 if (tdesc_data)
58d6951d
DJ
6989 {
6990 set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
6991
9779414d 6992 tdesc_use_registers (gdbarch, tdesc, tdesc_data);
58d6951d
DJ
6993
6994 /* Override tdesc_register_type to adjust the types of VFP
6995 registers for NEON. */
6996 set_gdbarch_register_type (gdbarch, arm_register_type);
6997 }
123dc839
DJ
6998
6999 /* Add standard register aliases. We add aliases even for those
7000 nanes which are used by the current architecture - it's simpler,
7001 and does no harm, since nothing ever lists user registers. */
7002 for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
7003 user_reg_add (gdbarch, arm_register_aliases[i].name,
7004 value_of_arm_user_reg, &arm_register_aliases[i].regnum);
7005
39bbf761
RE
7006 return gdbarch;
7007}
7008
97e03143 7009static void
2af46ca0 7010arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
97e03143 7011{
2af46ca0 7012 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
97e03143
RE
7013
7014 if (tdep == NULL)
7015 return;
7016
edefbb7c 7017 fprintf_unfiltered (file, _("arm_dump_tdep: Lowest pc = 0x%lx"),
97e03143
RE
7018 (unsigned long) tdep->lowest_pc);
7019}
7020
a78f21af
AC
7021extern initialize_file_ftype _initialize_arm_tdep; /* -Wmissing-prototypes */
7022
c906108c 7023void
ed9a39eb 7024_initialize_arm_tdep (void)
c906108c 7025{
bc90b915
FN
7026 struct ui_file *stb;
7027 long length;
26304000 7028 struct cmd_list_element *new_set, *new_show;
53904c9e
AC
7029 const char *setname;
7030 const char *setdesc;
4bd7b427 7031 const char *const *regnames;
bc90b915
FN
7032 int numregs, i, j;
7033 static char *helptext;
edefbb7c
AC
7034 char regdesc[1024], *rdptr = regdesc;
7035 size_t rest = sizeof (regdesc);
085dd6e6 7036
42cf1509 7037 gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
97e03143 7038
60c5725c 7039 arm_objfile_data_key
c1bd65d0 7040 = register_objfile_data_with_cleanup (NULL, arm_objfile_data_free);
60c5725c 7041
70f80edf
JT
7042 /* Register an ELF OS ABI sniffer for ARM binaries. */
7043 gdbarch_register_osabi_sniffer (bfd_arch_arm,
7044 bfd_target_elf_flavour,
7045 arm_elf_osabi_sniffer);
7046
9779414d
DJ
7047 /* Initialize the standard target descriptions. */
7048 initialize_tdesc_arm_with_m ();
7049
94c30b78 7050 /* Get the number of possible sets of register names defined in opcodes. */
afd7eef0
RE
7051 num_disassembly_options = get_arm_regname_num_options ();
7052
7053 /* Add root prefix command for all "set arm"/"show arm" commands. */
7054 add_prefix_cmd ("arm", no_class, set_arm_command,
edefbb7c 7055 _("Various ARM-specific commands."),
afd7eef0
RE
7056 &setarmcmdlist, "set arm ", 0, &setlist);
7057
7058 add_prefix_cmd ("arm", no_class, show_arm_command,
edefbb7c 7059 _("Various ARM-specific commands."),
afd7eef0 7060 &showarmcmdlist, "show arm ", 0, &showlist);
bc90b915 7061
94c30b78 7062 /* Sync the opcode insn printer with our register viewer. */
bc90b915 7063 parse_arm_disassembler_option ("reg-names-std");
c5aa993b 7064
eefe576e
AC
7065 /* Initialize the array that will be passed to
7066 add_setshow_enum_cmd(). */
afd7eef0
RE
7067 valid_disassembly_styles
7068 = xmalloc ((num_disassembly_options + 1) * sizeof (char *));
7069 for (i = 0; i < num_disassembly_options; i++)
bc90b915
FN
7070 {
7071 numregs = get_arm_regnames (i, &setname, &setdesc, &regnames);
afd7eef0 7072 valid_disassembly_styles[i] = setname;
edefbb7c
AC
7073 length = snprintf (rdptr, rest, "%s - %s\n", setname, setdesc);
7074 rdptr += length;
7075 rest -= length;
123dc839
DJ
7076 /* When we find the default names, tell the disassembler to use
7077 them. */
bc90b915
FN
7078 if (!strcmp (setname, "std"))
7079 {
afd7eef0 7080 disassembly_style = setname;
bc90b915
FN
7081 set_arm_regname_option (i);
7082 }
7083 }
94c30b78 7084 /* Mark the end of valid options. */
afd7eef0 7085 valid_disassembly_styles[num_disassembly_options] = NULL;
c906108c 7086
edefbb7c
AC
7087 /* Create the help text. */
7088 stb = mem_fileopen ();
7089 fprintf_unfiltered (stb, "%s%s%s",
7090 _("The valid values are:\n"),
7091 regdesc,
7092 _("The default is \"std\"."));
759ef836 7093 helptext = ui_file_xstrdup (stb, NULL);
bc90b915 7094 ui_file_delete (stb);
ed9a39eb 7095
edefbb7c
AC
7096 add_setshow_enum_cmd("disassembler", no_class,
7097 valid_disassembly_styles, &disassembly_style,
7098 _("Set the disassembly style."),
7099 _("Show the disassembly style."),
7100 helptext,
2c5b56ce 7101 set_disassembly_style_sfunc,
7915a72c 7102 NULL, /* FIXME: i18n: The disassembly style is \"%s\". */
7376b4c2 7103 &setarmcmdlist, &showarmcmdlist);
edefbb7c
AC
7104
7105 add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
7106 _("Set usage of ARM 32-bit mode."),
7107 _("Show usage of ARM 32-bit mode."),
7108 _("When off, a 26-bit PC will be used."),
2c5b56ce 7109 NULL,
7915a72c 7110 NULL, /* FIXME: i18n: Usage of ARM 32-bit mode is %s. */
26304000 7111 &setarmcmdlist, &showarmcmdlist);
c906108c 7112
fd50bc42 7113 /* Add a command to allow the user to force the FPU model. */
edefbb7c
AC
7114 add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
7115 _("Set the floating point type."),
7116 _("Show the floating point type."),
7117 _("auto - Determine the FP typefrom the OS-ABI.\n\
7118softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
7119fpa - FPA co-processor (GCC compiled).\n\
7120softvfp - Software FP with pure-endian doubles.\n\
7121vfp - VFP co-processor."),
edefbb7c 7122 set_fp_model_sfunc, show_fp_model,
7376b4c2 7123 &setarmcmdlist, &showarmcmdlist);
fd50bc42 7124
28e97307
DJ
7125 /* Add a command to allow the user to force the ABI. */
7126 add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
7127 _("Set the ABI."),
7128 _("Show the ABI."),
7129 NULL, arm_set_abi, arm_show_abi,
7130 &setarmcmdlist, &showarmcmdlist);
7131
0428b8f5
DJ
7132 /* Add two commands to allow the user to force the assumed
7133 execution mode. */
7134 add_setshow_enum_cmd ("fallback-mode", class_support,
7135 arm_mode_strings, &arm_fallback_mode_string,
7136 _("Set the mode assumed when symbols are unavailable."),
7137 _("Show the mode assumed when symbols are unavailable."),
7138 NULL, NULL, arm_show_fallback_mode,
7139 &setarmcmdlist, &showarmcmdlist);
7140 add_setshow_enum_cmd ("force-mode", class_support,
7141 arm_mode_strings, &arm_force_mode_string,
7142 _("Set the mode assumed even when symbols are available."),
7143 _("Show the mode assumed even when symbols are available."),
7144 NULL, NULL, arm_show_force_mode,
7145 &setarmcmdlist, &showarmcmdlist);
7146
6529d2dd 7147 /* Debugging flag. */
edefbb7c
AC
7148 add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
7149 _("Set ARM debugging."),
7150 _("Show ARM debugging."),
7151 _("When on, arm-specific debugging is enabled."),
2c5b56ce 7152 NULL,
7915a72c 7153 NULL, /* FIXME: i18n: "ARM debugging is %s. */
26304000 7154 &setdebuglist, &showdebuglist);
c906108c 7155}
This page took 1.095175 seconds and 4 git commands to generate.