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