gdb/testsuite/
[deliverable/binutils-gdb.git] / gdb / arm-tdep.c
CommitLineData
ed9a39eb 1/* Common target dependent code for GDB on ARM systems.
0fd88904 2
0b302171
JB
3 Copyright (C) 1988-1989, 1991-1993, 1995-1996, 1998-2012 Free
4 Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
c5aa993b 11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b 18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 20
0963b4bd 21#include <ctype.h> /* XXX for isupper (). */
34e8f22d 22
c906108c
SS
23#include "defs.h"
24#include "frame.h"
25#include "inferior.h"
26#include "gdbcmd.h"
27#include "gdbcore.h"
c906108c 28#include "gdb_string.h"
0963b4bd 29#include "dis-asm.h" /* For register styles. */
4e052eda 30#include "regcache.h"
54483882 31#include "reggroups.h"
d16aafd8 32#include "doublest.h"
fd0407d6 33#include "value.h"
34e8f22d 34#include "arch-utils.h"
4be87837 35#include "osabi.h"
eb5492fa
DJ
36#include "frame-unwind.h"
37#include "frame-base.h"
38#include "trad-frame.h"
842e1f1e
DJ
39#include "objfiles.h"
40#include "dwarf2-frame.h"
e4c16157 41#include "gdbtypes.h"
29d73ae4 42#include "prologue-value.h"
25f8c692 43#include "remote.h"
123dc839
DJ
44#include "target-descriptions.h"
45#include "user-regs.h"
0e9e9abd 46#include "observer.h"
34e8f22d
RE
47
48#include "arm-tdep.h"
26216b98 49#include "gdb/sim-arm.h"
34e8f22d 50
082fc60d
RE
51#include "elf-bfd.h"
52#include "coff/internal.h"
97e03143 53#include "elf/arm.h"
c906108c 54
26216b98 55#include "gdb_assert.h"
60c5725c 56#include "vec.h"
26216b98 57
72508ac0
PO
58#include "record.h"
59
9779414d 60#include "features/arm-with-m.c"
25f8c692 61#include "features/arm-with-m-fpa-layout.c"
3184d3f9 62#include "features/arm-with-m-vfp-d16.c"
ef7e8358
UW
63#include "features/arm-with-iwmmxt.c"
64#include "features/arm-with-vfpv2.c"
65#include "features/arm-with-vfpv3.c"
66#include "features/arm-with-neon.c"
9779414d 67
6529d2dd
AC
68static int arm_debug;
69
082fc60d
RE
70/* Macros for setting and testing a bit in a minimal symbol that marks
71 it as Thumb function. The MSB of the minimal symbol's "info" field
f594e5e9 72 is used for this purpose.
082fc60d
RE
73
74 MSYMBOL_SET_SPECIAL Actually sets the "special" bit.
f594e5e9 75 MSYMBOL_IS_SPECIAL Tests the "special" bit in a minimal symbol. */
082fc60d 76
0963b4bd 77#define MSYMBOL_SET_SPECIAL(msym) \
b887350f 78 MSYMBOL_TARGET_FLAG_1 (msym) = 1
082fc60d
RE
79
80#define MSYMBOL_IS_SPECIAL(msym) \
b887350f 81 MSYMBOL_TARGET_FLAG_1 (msym)
082fc60d 82
60c5725c
DJ
83/* Per-objfile data used for mapping symbols. */
84static const struct objfile_data *arm_objfile_data_key;
85
86struct arm_mapping_symbol
87{
88 bfd_vma value;
89 char type;
90};
91typedef struct arm_mapping_symbol arm_mapping_symbol_s;
92DEF_VEC_O(arm_mapping_symbol_s);
93
94struct arm_per_objfile
95{
96 VEC(arm_mapping_symbol_s) **section_maps;
97};
98
afd7eef0
RE
99/* The list of available "set arm ..." and "show arm ..." commands. */
100static struct cmd_list_element *setarmcmdlist = NULL;
101static struct cmd_list_element *showarmcmdlist = NULL;
102
fd50bc42
RE
103/* The type of floating-point to use. Keep this in sync with enum
104 arm_float_model, and the help string in _initialize_arm_tdep. */
40478521 105static const char *const fp_model_strings[] =
fd50bc42
RE
106{
107 "auto",
108 "softfpa",
109 "fpa",
110 "softvfp",
28e97307
DJ
111 "vfp",
112 NULL
fd50bc42
RE
113};
114
115/* A variable that can be configured by the user. */
116static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
117static const char *current_fp_model = "auto";
118
28e97307 119/* The ABI to use. Keep this in sync with arm_abi_kind. */
40478521 120static const char *const arm_abi_strings[] =
28e97307
DJ
121{
122 "auto",
123 "APCS",
124 "AAPCS",
125 NULL
126};
127
128/* A variable that can be configured by the user. */
129static enum arm_abi_kind arm_abi_global = ARM_ABI_AUTO;
130static const char *arm_abi_string = "auto";
131
0428b8f5 132/* The execution mode to assume. */
40478521 133static const char *const arm_mode_strings[] =
0428b8f5
DJ
134 {
135 "auto",
136 "arm",
68770265
MGD
137 "thumb",
138 NULL
0428b8f5
DJ
139 };
140
141static const char *arm_fallback_mode_string = "auto";
142static const char *arm_force_mode_string = "auto";
143
18819fa6
UW
144/* Internal override of the execution mode. -1 means no override,
145 0 means override to ARM mode, 1 means override to Thumb mode.
146 The effect is the same as if arm_force_mode has been set by the
147 user (except the internal override has precedence over a user's
148 arm_force_mode override). */
149static int arm_override_mode = -1;
150
94c30b78 151/* Number of different reg name sets (options). */
afd7eef0 152static int num_disassembly_options;
bc90b915 153
f32bf4a4
YQ
154/* The standard register names, and all the valid aliases for them. Note
155 that `fp', `sp' and `pc' are not added in this alias list, because they
156 have been added as builtin user registers in
157 std-regs.c:_initialize_frame_reg. */
123dc839
DJ
158static const struct
159{
160 const char *name;
161 int regnum;
162} arm_register_aliases[] = {
163 /* Basic register numbers. */
164 { "r0", 0 },
165 { "r1", 1 },
166 { "r2", 2 },
167 { "r3", 3 },
168 { "r4", 4 },
169 { "r5", 5 },
170 { "r6", 6 },
171 { "r7", 7 },
172 { "r8", 8 },
173 { "r9", 9 },
174 { "r10", 10 },
175 { "r11", 11 },
176 { "r12", 12 },
177 { "r13", 13 },
178 { "r14", 14 },
179 { "r15", 15 },
180 /* Synonyms (argument and variable registers). */
181 { "a1", 0 },
182 { "a2", 1 },
183 { "a3", 2 },
184 { "a4", 3 },
185 { "v1", 4 },
186 { "v2", 5 },
187 { "v3", 6 },
188 { "v4", 7 },
189 { "v5", 8 },
190 { "v6", 9 },
191 { "v7", 10 },
192 { "v8", 11 },
193 /* Other platform-specific names for r9. */
194 { "sb", 9 },
195 { "tr", 9 },
196 /* Special names. */
197 { "ip", 12 },
123dc839 198 { "lr", 14 },
123dc839
DJ
199 /* Names used by GCC (not listed in the ARM EABI). */
200 { "sl", 10 },
123dc839
DJ
201 /* A special name from the older ATPCS. */
202 { "wr", 7 },
203};
bc90b915 204
123dc839 205static const char *const arm_register_names[] =
da59e081
JM
206{"r0", "r1", "r2", "r3", /* 0 1 2 3 */
207 "r4", "r5", "r6", "r7", /* 4 5 6 7 */
208 "r8", "r9", "r10", "r11", /* 8 9 10 11 */
209 "r12", "sp", "lr", "pc", /* 12 13 14 15 */
210 "f0", "f1", "f2", "f3", /* 16 17 18 19 */
211 "f4", "f5", "f6", "f7", /* 20 21 22 23 */
94c30b78 212 "fps", "cpsr" }; /* 24 25 */
ed9a39eb 213
afd7eef0
RE
214/* Valid register name styles. */
215static const char **valid_disassembly_styles;
ed9a39eb 216
afd7eef0
RE
217/* Disassembly style to use. Default to "std" register names. */
218static const char *disassembly_style;
96baa820 219
ed9a39eb 220/* This is used to keep the bfd arch_info in sync with the disassembly
afd7eef0
RE
221 style. */
222static void set_disassembly_style_sfunc(char *, int,
ed9a39eb 223 struct cmd_list_element *);
afd7eef0 224static void set_disassembly_style (void);
ed9a39eb 225
b508a996 226static void convert_from_extended (const struct floatformat *, const void *,
be8626e0 227 void *, int);
b508a996 228static void convert_to_extended (const struct floatformat *, void *,
be8626e0 229 const void *, int);
ed9a39eb 230
05d1431c
PA
231static enum register_status arm_neon_quad_read (struct gdbarch *gdbarch,
232 struct regcache *regcache,
233 int regnum, gdb_byte *buf);
58d6951d
DJ
234static void arm_neon_quad_write (struct gdbarch *gdbarch,
235 struct regcache *regcache,
236 int regnum, const gdb_byte *buf);
237
db24da6d
YQ
238static int thumb_insn_size (unsigned short inst1);
239
9b8d791a 240struct arm_prologue_cache
c3b4394c 241{
eb5492fa
DJ
242 /* The stack pointer at the time this frame was created; i.e. the
243 caller's stack pointer when this function was called. It is used
244 to identify this frame. */
245 CORE_ADDR prev_sp;
246
4be43953
DJ
247 /* The frame base for this frame is just prev_sp - frame size.
248 FRAMESIZE is the distance from the frame pointer to the
249 initial stack pointer. */
eb5492fa 250
c3b4394c 251 int framesize;
eb5492fa
DJ
252
253 /* The register used to hold the frame pointer for this frame. */
c3b4394c 254 int framereg;
eb5492fa
DJ
255
256 /* Saved register offsets. */
257 struct trad_frame_saved_reg *saved_regs;
c3b4394c 258};
ed9a39eb 259
0d39a070
DJ
260static CORE_ADDR arm_analyze_prologue (struct gdbarch *gdbarch,
261 CORE_ADDR prologue_start,
262 CORE_ADDR prologue_end,
263 struct arm_prologue_cache *cache);
264
cca44b1b
JB
265/* Architecture version for displaced stepping. This effects the behaviour of
266 certain instructions, and really should not be hard-wired. */
267
268#define DISPLACED_STEPPING_ARCH_VERSION 5
269
bc90b915
FN
270/* Addresses for calling Thumb functions have the bit 0 set.
271 Here are some macros to test, set, or clear bit 0 of addresses. */
272#define IS_THUMB_ADDR(addr) ((addr) & 1)
273#define MAKE_THUMB_ADDR(addr) ((addr) | 1)
274#define UNMAKE_THUMB_ADDR(addr) ((addr) & ~1)
275
94c30b78 276/* Set to true if the 32-bit mode is in use. */
c906108c
SS
277
278int arm_apcs_32 = 1;
279
9779414d
DJ
280/* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode. */
281
478fd957 282int
9779414d
DJ
283arm_psr_thumb_bit (struct gdbarch *gdbarch)
284{
285 if (gdbarch_tdep (gdbarch)->is_m)
286 return XPSR_T;
287 else
288 return CPSR_T;
289}
290
b39cc962
DJ
291/* Determine if FRAME is executing in Thumb mode. */
292
25b41d01 293int
b39cc962
DJ
294arm_frame_is_thumb (struct frame_info *frame)
295{
296 CORE_ADDR cpsr;
9779414d 297 ULONGEST t_bit = arm_psr_thumb_bit (get_frame_arch (frame));
b39cc962
DJ
298
299 /* Every ARM frame unwinder can unwind the T bit of the CPSR, either
300 directly (from a signal frame or dummy frame) or by interpreting
301 the saved LR (from a prologue or DWARF frame). So consult it and
302 trust the unwinders. */
303 cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
304
9779414d 305 return (cpsr & t_bit) != 0;
b39cc962
DJ
306}
307
60c5725c
DJ
308/* Callback for VEC_lower_bound. */
309
310static inline int
311arm_compare_mapping_symbols (const struct arm_mapping_symbol *lhs,
312 const struct arm_mapping_symbol *rhs)
313{
314 return lhs->value < rhs->value;
315}
316
f9d67f43
DJ
317/* Search for the mapping symbol covering MEMADDR. If one is found,
318 return its type. Otherwise, return 0. If START is non-NULL,
319 set *START to the location of the mapping symbol. */
c906108c 320
f9d67f43
DJ
321static char
322arm_find_mapping_symbol (CORE_ADDR memaddr, CORE_ADDR *start)
c906108c 323{
60c5725c 324 struct obj_section *sec;
0428b8f5 325
60c5725c
DJ
326 /* If there are mapping symbols, consult them. */
327 sec = find_pc_section (memaddr);
328 if (sec != NULL)
329 {
330 struct arm_per_objfile *data;
331 VEC(arm_mapping_symbol_s) *map;
aded6f54
PA
332 struct arm_mapping_symbol map_key = { memaddr - obj_section_addr (sec),
333 0 };
60c5725c
DJ
334 unsigned int idx;
335
336 data = objfile_data (sec->objfile, arm_objfile_data_key);
337 if (data != NULL)
338 {
339 map = data->section_maps[sec->the_bfd_section->index];
340 if (!VEC_empty (arm_mapping_symbol_s, map))
341 {
342 struct arm_mapping_symbol *map_sym;
343
344 idx = VEC_lower_bound (arm_mapping_symbol_s, map, &map_key,
345 arm_compare_mapping_symbols);
346
347 /* VEC_lower_bound finds the earliest ordered insertion
348 point. If the following symbol starts at this exact
349 address, we use that; otherwise, the preceding
350 mapping symbol covers this address. */
351 if (idx < VEC_length (arm_mapping_symbol_s, map))
352 {
353 map_sym = VEC_index (arm_mapping_symbol_s, map, idx);
354 if (map_sym->value == map_key.value)
f9d67f43
DJ
355 {
356 if (start)
357 *start = map_sym->value + obj_section_addr (sec);
358 return map_sym->type;
359 }
60c5725c
DJ
360 }
361
362 if (idx > 0)
363 {
364 map_sym = VEC_index (arm_mapping_symbol_s, map, idx - 1);
f9d67f43
DJ
365 if (start)
366 *start = map_sym->value + obj_section_addr (sec);
367 return map_sym->type;
60c5725c
DJ
368 }
369 }
370 }
371 }
372
f9d67f43
DJ
373 return 0;
374}
375
376/* Determine if the program counter specified in MEMADDR is in a Thumb
377 function. This function should be called for addresses unrelated to
378 any executing frame; otherwise, prefer arm_frame_is_thumb. */
379
e3039479 380int
9779414d 381arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
f9d67f43
DJ
382{
383 struct obj_section *sec;
384 struct minimal_symbol *sym;
385 char type;
a42244db
YQ
386 struct displaced_step_closure* dsc
387 = get_displaced_step_closure_by_addr(memaddr);
388
389 /* If checking the mode of displaced instruction in copy area, the mode
390 should be determined by instruction on the original address. */
391 if (dsc)
392 {
393 if (debug_displaced)
394 fprintf_unfiltered (gdb_stdlog,
395 "displaced: check mode of %.8lx instead of %.8lx\n",
396 (unsigned long) dsc->insn_addr,
397 (unsigned long) memaddr);
398 memaddr = dsc->insn_addr;
399 }
f9d67f43
DJ
400
401 /* If bit 0 of the address is set, assume this is a Thumb address. */
402 if (IS_THUMB_ADDR (memaddr))
403 return 1;
404
18819fa6
UW
405 /* Respect internal mode override if active. */
406 if (arm_override_mode != -1)
407 return arm_override_mode;
408
f9d67f43
DJ
409 /* If the user wants to override the symbol table, let him. */
410 if (strcmp (arm_force_mode_string, "arm") == 0)
411 return 0;
412 if (strcmp (arm_force_mode_string, "thumb") == 0)
413 return 1;
414
9779414d
DJ
415 /* ARM v6-M and v7-M are always in Thumb mode. */
416 if (gdbarch_tdep (gdbarch)->is_m)
417 return 1;
418
f9d67f43
DJ
419 /* If there are mapping symbols, consult them. */
420 type = arm_find_mapping_symbol (memaddr, NULL);
421 if (type)
422 return type == 't';
423
ed9a39eb 424 /* Thumb functions have a "special" bit set in minimal symbols. */
c906108c
SS
425 sym = lookup_minimal_symbol_by_pc (memaddr);
426 if (sym)
0428b8f5
DJ
427 return (MSYMBOL_IS_SPECIAL (sym));
428
429 /* If the user wants to override the fallback mode, let them. */
430 if (strcmp (arm_fallback_mode_string, "arm") == 0)
431 return 0;
432 if (strcmp (arm_fallback_mode_string, "thumb") == 0)
433 return 1;
434
435 /* If we couldn't find any symbol, but we're talking to a running
436 target, then trust the current value of $cpsr. This lets
437 "display/i $pc" always show the correct mode (though if there is
438 a symbol table we will not reach here, so it still may not be
18819fa6 439 displayed in the mode it will be executed). */
0428b8f5 440 if (target_has_registers)
18819fa6 441 return arm_frame_is_thumb (get_current_frame ());
0428b8f5
DJ
442
443 /* Otherwise we're out of luck; we assume ARM. */
444 return 0;
c906108c
SS
445}
446
181c1381 447/* Remove useless bits from addresses in a running program. */
34e8f22d 448static CORE_ADDR
24568a2c 449arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
c906108c 450{
a3a2ee65 451 if (arm_apcs_32)
dd6be234 452 return UNMAKE_THUMB_ADDR (val);
c906108c 453 else
a3a2ee65 454 return (val & 0x03fffffc);
c906108c
SS
455}
456
181c1381
RE
457/* When reading symbols, we need to zap the low bit of the address,
458 which may be set to 1 for Thumb functions. */
34e8f22d 459static CORE_ADDR
24568a2c 460arm_smash_text_address (struct gdbarch *gdbarch, CORE_ADDR val)
181c1381
RE
461{
462 return val & ~1;
463}
464
0d39a070 465/* Return 1 if PC is the start of a compiler helper function which
e0634ccf
UW
466 can be safely ignored during prologue skipping. IS_THUMB is true
467 if the function is known to be a Thumb function due to the way it
468 is being called. */
0d39a070 469static int
e0634ccf 470skip_prologue_function (struct gdbarch *gdbarch, CORE_ADDR pc, int is_thumb)
0d39a070 471{
e0634ccf 472 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
0d39a070 473 struct minimal_symbol *msym;
0d39a070
DJ
474
475 msym = lookup_minimal_symbol_by_pc (pc);
e0634ccf
UW
476 if (msym != NULL
477 && SYMBOL_VALUE_ADDRESS (msym) == pc
478 && SYMBOL_LINKAGE_NAME (msym) != NULL)
479 {
480 const char *name = SYMBOL_LINKAGE_NAME (msym);
0d39a070 481
e0634ccf
UW
482 /* The GNU linker's Thumb call stub to foo is named
483 __foo_from_thumb. */
484 if (strstr (name, "_from_thumb") != NULL)
485 name += 2;
0d39a070 486
e0634ccf
UW
487 /* On soft-float targets, __truncdfsf2 is called to convert promoted
488 arguments to their argument types in non-prototyped
489 functions. */
490 if (strncmp (name, "__truncdfsf2", strlen ("__truncdfsf2")) == 0)
491 return 1;
492 if (strncmp (name, "__aeabi_d2f", strlen ("__aeabi_d2f")) == 0)
493 return 1;
0d39a070 494
e0634ccf
UW
495 /* Internal functions related to thread-local storage. */
496 if (strncmp (name, "__tls_get_addr", strlen ("__tls_get_addr")) == 0)
497 return 1;
498 if (strncmp (name, "__aeabi_read_tp", strlen ("__aeabi_read_tp")) == 0)
499 return 1;
500 }
501 else
502 {
503 /* If we run against a stripped glibc, we may be unable to identify
504 special functions by name. Check for one important case,
505 __aeabi_read_tp, by comparing the *code* against the default
506 implementation (this is hand-written ARM assembler in glibc). */
507
508 if (!is_thumb
509 && read_memory_unsigned_integer (pc, 4, byte_order_for_code)
510 == 0xe3e00a0f /* mov r0, #0xffff0fff */
511 && read_memory_unsigned_integer (pc + 4, 4, byte_order_for_code)
512 == 0xe240f01f) /* sub pc, r0, #31 */
513 return 1;
514 }
ec3d575a 515
0d39a070
DJ
516 return 0;
517}
518
519/* Support routines for instruction parsing. */
520#define submask(x) ((1L << ((x) + 1)) - 1)
521#define bit(obj,st) (((obj) >> (st)) & 1)
522#define bits(obj,st,fn) (((obj) >> (st)) & submask ((fn) - (st)))
523#define sbits(obj,st,fn) \
524 ((long) (bits(obj,st,fn) | ((long) bit(obj,fn) * ~ submask (fn - st))))
525#define BranchDest(addr,instr) \
526 ((CORE_ADDR) (((long) (addr)) + 8 + (sbits (instr, 0, 23) << 2)))
527
621c6d5b
YQ
528/* Extract the immediate from instruction movw/movt of encoding T. INSN1 is
529 the first 16-bit of instruction, and INSN2 is the second 16-bit of
530 instruction. */
531#define EXTRACT_MOVW_MOVT_IMM_T(insn1, insn2) \
532 ((bits ((insn1), 0, 3) << 12) \
533 | (bits ((insn1), 10, 10) << 11) \
534 | (bits ((insn2), 12, 14) << 8) \
535 | bits ((insn2), 0, 7))
536
537/* Extract the immediate from instruction movw/movt of encoding A. INSN is
538 the 32-bit instruction. */
539#define EXTRACT_MOVW_MOVT_IMM_A(insn) \
540 ((bits ((insn), 16, 19) << 12) \
541 | bits ((insn), 0, 11))
542
ec3d575a
UW
543/* Decode immediate value; implements ThumbExpandImmediate pseudo-op. */
544
545static unsigned int
546thumb_expand_immediate (unsigned int imm)
547{
548 unsigned int count = imm >> 7;
549
550 if (count < 8)
551 switch (count / 2)
552 {
553 case 0:
554 return imm & 0xff;
555 case 1:
556 return (imm & 0xff) | ((imm & 0xff) << 16);
557 case 2:
558 return ((imm & 0xff) << 8) | ((imm & 0xff) << 24);
559 case 3:
560 return (imm & 0xff) | ((imm & 0xff) << 8)
561 | ((imm & 0xff) << 16) | ((imm & 0xff) << 24);
562 }
563
564 return (0x80 | (imm & 0x7f)) << (32 - count);
565}
566
567/* Return 1 if the 16-bit Thumb instruction INST might change
568 control flow, 0 otherwise. */
569
570static int
571thumb_instruction_changes_pc (unsigned short inst)
572{
573 if ((inst & 0xff00) == 0xbd00) /* pop {rlist, pc} */
574 return 1;
575
576 if ((inst & 0xf000) == 0xd000) /* conditional branch */
577 return 1;
578
579 if ((inst & 0xf800) == 0xe000) /* unconditional branch */
580 return 1;
581
582 if ((inst & 0xff00) == 0x4700) /* bx REG, blx REG */
583 return 1;
584
ad8b5167
UW
585 if ((inst & 0xff87) == 0x4687) /* mov pc, REG */
586 return 1;
587
ec3d575a
UW
588 if ((inst & 0xf500) == 0xb100) /* CBNZ or CBZ. */
589 return 1;
590
591 return 0;
592}
593
594/* Return 1 if the 32-bit Thumb instruction in INST1 and INST2
595 might change control flow, 0 otherwise. */
596
597static int
598thumb2_instruction_changes_pc (unsigned short inst1, unsigned short inst2)
599{
600 if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
601 {
602 /* Branches and miscellaneous control instructions. */
603
604 if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
605 {
606 /* B, BL, BLX. */
607 return 1;
608 }
609 else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
610 {
611 /* SUBS PC, LR, #imm8. */
612 return 1;
613 }
614 else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
615 {
616 /* Conditional branch. */
617 return 1;
618 }
619
620 return 0;
621 }
622
623 if ((inst1 & 0xfe50) == 0xe810)
624 {
625 /* Load multiple or RFE. */
626
627 if (bit (inst1, 7) && !bit (inst1, 8))
628 {
629 /* LDMIA or POP */
630 if (bit (inst2, 15))
631 return 1;
632 }
633 else if (!bit (inst1, 7) && bit (inst1, 8))
634 {
635 /* LDMDB */
636 if (bit (inst2, 15))
637 return 1;
638 }
639 else if (bit (inst1, 7) && bit (inst1, 8))
640 {
641 /* RFEIA */
642 return 1;
643 }
644 else if (!bit (inst1, 7) && !bit (inst1, 8))
645 {
646 /* RFEDB */
647 return 1;
648 }
649
650 return 0;
651 }
652
653 if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
654 {
655 /* MOV PC or MOVS PC. */
656 return 1;
657 }
658
659 if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
660 {
661 /* LDR PC. */
662 if (bits (inst1, 0, 3) == 15)
663 return 1;
664 if (bit (inst1, 7))
665 return 1;
666 if (bit (inst2, 11))
667 return 1;
668 if ((inst2 & 0x0fc0) == 0x0000)
669 return 1;
670
671 return 0;
672 }
673
674 if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
675 {
676 /* TBB. */
677 return 1;
678 }
679
680 if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
681 {
682 /* TBH. */
683 return 1;
684 }
685
686 return 0;
687}
688
29d73ae4
DJ
689/* Analyze a Thumb prologue, looking for a recognizable stack frame
690 and frame pointer. Scan until we encounter a store that could
0d39a070
DJ
691 clobber the stack frame unexpectedly, or an unknown instruction.
692 Return the last address which is definitely safe to skip for an
693 initial breakpoint. */
c906108c
SS
694
695static CORE_ADDR
29d73ae4
DJ
696thumb_analyze_prologue (struct gdbarch *gdbarch,
697 CORE_ADDR start, CORE_ADDR limit,
698 struct arm_prologue_cache *cache)
c906108c 699{
0d39a070 700 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
e17a4113 701 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
29d73ae4
DJ
702 int i;
703 pv_t regs[16];
704 struct pv_area *stack;
705 struct cleanup *back_to;
706 CORE_ADDR offset;
ec3d575a 707 CORE_ADDR unrecognized_pc = 0;
da3c6d4a 708
29d73ae4
DJ
709 for (i = 0; i < 16; i++)
710 regs[i] = pv_register (i, 0);
55f960e1 711 stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
29d73ae4
DJ
712 back_to = make_cleanup_free_pv_area (stack);
713
29d73ae4 714 while (start < limit)
c906108c 715 {
29d73ae4
DJ
716 unsigned short insn;
717
e17a4113 718 insn = read_memory_unsigned_integer (start, 2, byte_order_for_code);
9d4fde75 719
94c30b78 720 if ((insn & 0xfe00) == 0xb400) /* push { rlist } */
da59e081 721 {
29d73ae4
DJ
722 int regno;
723 int mask;
4be43953
DJ
724
725 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
726 break;
29d73ae4
DJ
727
728 /* Bits 0-7 contain a mask for registers R0-R7. Bit 8 says
729 whether to save LR (R14). */
730 mask = (insn & 0xff) | ((insn & 0x100) << 6);
731
732 /* Calculate offsets of saved R0-R7 and LR. */
733 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
734 if (mask & (1 << regno))
735 {
29d73ae4
DJ
736 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
737 -4);
738 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
739 }
da59e081 740 }
da3c6d4a
MS
741 else if ((insn & 0xff00) == 0xb000) /* add sp, #simm OR
742 sub sp, #simm */
da59e081 743 {
29d73ae4
DJ
744 offset = (insn & 0x7f) << 2; /* get scaled offset */
745 if (insn & 0x80) /* Check for SUB. */
746 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
747 -offset);
da59e081 748 else
29d73ae4
DJ
749 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
750 offset);
da59e081 751 }
0d39a070
DJ
752 else if ((insn & 0xf800) == 0xa800) /* add Rd, sp, #imm */
753 regs[bits (insn, 8, 10)] = pv_add_constant (regs[ARM_SP_REGNUM],
754 (insn & 0xff) << 2);
755 else if ((insn & 0xfe00) == 0x1c00 /* add Rd, Rn, #imm */
756 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
757 regs[bits (insn, 0, 2)] = pv_add_constant (regs[bits (insn, 3, 5)],
758 bits (insn, 6, 8));
759 else if ((insn & 0xf800) == 0x3000 /* add Rd, #imm */
760 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
761 regs[bits (insn, 8, 10)] = pv_add_constant (regs[bits (insn, 8, 10)],
762 bits (insn, 0, 7));
763 else if ((insn & 0xfe00) == 0x1800 /* add Rd, Rn, Rm */
764 && pv_is_register (regs[bits (insn, 6, 8)], ARM_SP_REGNUM)
765 && pv_is_constant (regs[bits (insn, 3, 5)]))
766 regs[bits (insn, 0, 2)] = pv_add (regs[bits (insn, 3, 5)],
767 regs[bits (insn, 6, 8)]);
768 else if ((insn & 0xff00) == 0x4400 /* add Rd, Rm */
769 && pv_is_constant (regs[bits (insn, 3, 6)]))
770 {
771 int rd = (bit (insn, 7) << 3) + bits (insn, 0, 2);
772 int rm = bits (insn, 3, 6);
773 regs[rd] = pv_add (regs[rd], regs[rm]);
774 }
29d73ae4 775 else if ((insn & 0xff00) == 0x4600) /* mov hi, lo or mov lo, hi */
da59e081 776 {
29d73ae4
DJ
777 int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
778 int src_reg = (insn & 0x78) >> 3;
779 regs[dst_reg] = regs[src_reg];
da59e081 780 }
29d73ae4 781 else if ((insn & 0xf800) == 0x9000) /* str rd, [sp, #off] */
da59e081 782 {
29d73ae4
DJ
783 /* Handle stores to the stack. Normally pushes are used,
784 but with GCC -mtpcs-frame, there may be other stores
785 in the prologue to create the frame. */
786 int regno = (insn >> 8) & 0x7;
787 pv_t addr;
788
789 offset = (insn & 0xff) << 2;
790 addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
791
792 if (pv_area_store_would_trash (stack, addr))
793 break;
794
795 pv_area_store (stack, addr, 4, regs[regno]);
da59e081 796 }
0d39a070
DJ
797 else if ((insn & 0xf800) == 0x6000) /* str rd, [rn, #off] */
798 {
799 int rd = bits (insn, 0, 2);
800 int rn = bits (insn, 3, 5);
801 pv_t addr;
802
803 offset = bits (insn, 6, 10) << 2;
804 addr = pv_add_constant (regs[rn], offset);
805
806 if (pv_area_store_would_trash (stack, addr))
807 break;
808
809 pv_area_store (stack, addr, 4, regs[rd]);
810 }
811 else if (((insn & 0xf800) == 0x7000 /* strb Rd, [Rn, #off] */
812 || (insn & 0xf800) == 0x8000) /* strh Rd, [Rn, #off] */
813 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
814 /* Ignore stores of argument registers to the stack. */
815 ;
816 else if ((insn & 0xf800) == 0xc800 /* ldmia Rn!, { registers } */
817 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
818 /* Ignore block loads from the stack, potentially copying
819 parameters from memory. */
820 ;
821 else if ((insn & 0xf800) == 0x9800 /* ldr Rd, [Rn, #immed] */
822 || ((insn & 0xf800) == 0x6800 /* ldr Rd, [sp, #immed] */
823 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM)))
824 /* Similarly ignore single loads from the stack. */
825 ;
826 else if ((insn & 0xffc0) == 0x0000 /* lsls Rd, Rm, #0 */
827 || (insn & 0xffc0) == 0x1c00) /* add Rd, Rn, #0 */
828 /* Skip register copies, i.e. saves to another register
829 instead of the stack. */
830 ;
831 else if ((insn & 0xf800) == 0x2000) /* movs Rd, #imm */
832 /* Recognize constant loads; even with small stacks these are necessary
833 on Thumb. */
834 regs[bits (insn, 8, 10)] = pv_constant (bits (insn, 0, 7));
835 else if ((insn & 0xf800) == 0x4800) /* ldr Rd, [pc, #imm] */
836 {
837 /* Constant pool loads, for the same reason. */
838 unsigned int constant;
839 CORE_ADDR loc;
840
841 loc = start + 4 + bits (insn, 0, 7) * 4;
842 constant = read_memory_unsigned_integer (loc, 4, byte_order);
843 regs[bits (insn, 8, 10)] = pv_constant (constant);
844 }
db24da6d 845 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instructions. */
0d39a070 846 {
0d39a070
DJ
847 unsigned short inst2;
848
849 inst2 = read_memory_unsigned_integer (start + 2, 2,
850 byte_order_for_code);
851
852 if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
853 {
854 /* BL, BLX. Allow some special function calls when
855 skipping the prologue; GCC generates these before
856 storing arguments to the stack. */
857 CORE_ADDR nextpc;
858 int j1, j2, imm1, imm2;
859
860 imm1 = sbits (insn, 0, 10);
861 imm2 = bits (inst2, 0, 10);
862 j1 = bit (inst2, 13);
863 j2 = bit (inst2, 11);
864
865 offset = ((imm1 << 12) + (imm2 << 1));
866 offset ^= ((!j2) << 22) | ((!j1) << 23);
867
868 nextpc = start + 4 + offset;
869 /* For BLX make sure to clear the low bits. */
870 if (bit (inst2, 12) == 0)
871 nextpc = nextpc & 0xfffffffc;
872
e0634ccf
UW
873 if (!skip_prologue_function (gdbarch, nextpc,
874 bit (inst2, 12) != 0))
0d39a070
DJ
875 break;
876 }
ec3d575a 877
0963b4bd
MS
878 else if ((insn & 0xffd0) == 0xe900 /* stmdb Rn{!},
879 { registers } */
ec3d575a
UW
880 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
881 {
882 pv_t addr = regs[bits (insn, 0, 3)];
883 int regno;
884
885 if (pv_area_store_would_trash (stack, addr))
886 break;
887
888 /* Calculate offsets of saved registers. */
889 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
890 if (inst2 & (1 << regno))
891 {
892 addr = pv_add_constant (addr, -4);
893 pv_area_store (stack, addr, 4, regs[regno]);
894 }
895
896 if (insn & 0x0020)
897 regs[bits (insn, 0, 3)] = addr;
898 }
899
0963b4bd
MS
900 else if ((insn & 0xff50) == 0xe940 /* strd Rt, Rt2,
901 [Rn, #+/-imm]{!} */
ec3d575a
UW
902 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
903 {
904 int regno1 = bits (inst2, 12, 15);
905 int regno2 = bits (inst2, 8, 11);
906 pv_t addr = regs[bits (insn, 0, 3)];
907
908 offset = inst2 & 0xff;
909 if (insn & 0x0080)
910 addr = pv_add_constant (addr, offset);
911 else
912 addr = pv_add_constant (addr, -offset);
913
914 if (pv_area_store_would_trash (stack, addr))
915 break;
916
917 pv_area_store (stack, addr, 4, regs[regno1]);
918 pv_area_store (stack, pv_add_constant (addr, 4),
919 4, regs[regno2]);
920
921 if (insn & 0x0020)
922 regs[bits (insn, 0, 3)] = addr;
923 }
924
925 else if ((insn & 0xfff0) == 0xf8c0 /* str Rt,[Rn,+/-#imm]{!} */
926 && (inst2 & 0x0c00) == 0x0c00
927 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
928 {
929 int regno = bits (inst2, 12, 15);
930 pv_t addr = regs[bits (insn, 0, 3)];
931
932 offset = inst2 & 0xff;
933 if (inst2 & 0x0200)
934 addr = pv_add_constant (addr, offset);
935 else
936 addr = pv_add_constant (addr, -offset);
937
938 if (pv_area_store_would_trash (stack, addr))
939 break;
940
941 pv_area_store (stack, addr, 4, regs[regno]);
942
943 if (inst2 & 0x0100)
944 regs[bits (insn, 0, 3)] = addr;
945 }
946
947 else if ((insn & 0xfff0) == 0xf8c0 /* str.w Rt,[Rn,#imm] */
948 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
949 {
950 int regno = bits (inst2, 12, 15);
951 pv_t addr;
952
953 offset = inst2 & 0xfff;
954 addr = pv_add_constant (regs[bits (insn, 0, 3)], offset);
955
956 if (pv_area_store_would_trash (stack, addr))
957 break;
958
959 pv_area_store (stack, addr, 4, regs[regno]);
960 }
961
962 else if ((insn & 0xffd0) == 0xf880 /* str{bh}.w Rt,[Rn,#imm] */
0d39a070 963 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 964 /* Ignore stores of argument registers to the stack. */
0d39a070 965 ;
ec3d575a
UW
966
967 else if ((insn & 0xffd0) == 0xf800 /* str{bh} Rt,[Rn,#+/-imm] */
968 && (inst2 & 0x0d00) == 0x0c00
0d39a070 969 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 970 /* Ignore stores of argument registers to the stack. */
0d39a070 971 ;
ec3d575a 972
0963b4bd
MS
973 else if ((insn & 0xffd0) == 0xe890 /* ldmia Rn[!],
974 { registers } */
ec3d575a
UW
975 && (inst2 & 0x8000) == 0x0000
976 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
977 /* Ignore block loads from the stack, potentially copying
978 parameters from memory. */
0d39a070 979 ;
ec3d575a 980
0963b4bd
MS
981 else if ((insn & 0xffb0) == 0xe950 /* ldrd Rt, Rt2,
982 [Rn, #+/-imm] */
0d39a070 983 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 984 /* Similarly ignore dual loads from the stack. */
0d39a070 985 ;
ec3d575a
UW
986
987 else if ((insn & 0xfff0) == 0xf850 /* ldr Rt,[Rn,#+/-imm] */
988 && (inst2 & 0x0d00) == 0x0c00
0d39a070 989 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 990 /* Similarly ignore single loads from the stack. */
0d39a070 991 ;
ec3d575a
UW
992
993 else if ((insn & 0xfff0) == 0xf8d0 /* ldr.w Rt,[Rn,#imm] */
0d39a070 994 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 995 /* Similarly ignore single loads from the stack. */
0d39a070 996 ;
ec3d575a
UW
997
998 else if ((insn & 0xfbf0) == 0xf100 /* add.w Rd, Rn, #imm */
999 && (inst2 & 0x8000) == 0x0000)
1000 {
1001 unsigned int imm = ((bits (insn, 10, 10) << 11)
1002 | (bits (inst2, 12, 14) << 8)
1003 | bits (inst2, 0, 7));
1004
1005 regs[bits (inst2, 8, 11)]
1006 = pv_add_constant (regs[bits (insn, 0, 3)],
1007 thumb_expand_immediate (imm));
1008 }
1009
1010 else if ((insn & 0xfbf0) == 0xf200 /* addw Rd, Rn, #imm */
1011 && (inst2 & 0x8000) == 0x0000)
0d39a070 1012 {
ec3d575a
UW
1013 unsigned int imm = ((bits (insn, 10, 10) << 11)
1014 | (bits (inst2, 12, 14) << 8)
1015 | bits (inst2, 0, 7));
1016
1017 regs[bits (inst2, 8, 11)]
1018 = pv_add_constant (regs[bits (insn, 0, 3)], imm);
1019 }
1020
1021 else if ((insn & 0xfbf0) == 0xf1a0 /* sub.w Rd, Rn, #imm */
1022 && (inst2 & 0x8000) == 0x0000)
1023 {
1024 unsigned int imm = ((bits (insn, 10, 10) << 11)
1025 | (bits (inst2, 12, 14) << 8)
1026 | bits (inst2, 0, 7));
1027
1028 regs[bits (inst2, 8, 11)]
1029 = pv_add_constant (regs[bits (insn, 0, 3)],
1030 - (CORE_ADDR) thumb_expand_immediate (imm));
1031 }
1032
1033 else if ((insn & 0xfbf0) == 0xf2a0 /* subw Rd, Rn, #imm */
1034 && (inst2 & 0x8000) == 0x0000)
1035 {
1036 unsigned int imm = ((bits (insn, 10, 10) << 11)
1037 | (bits (inst2, 12, 14) << 8)
1038 | bits (inst2, 0, 7));
1039
1040 regs[bits (inst2, 8, 11)]
1041 = pv_add_constant (regs[bits (insn, 0, 3)], - (CORE_ADDR) imm);
1042 }
1043
1044 else if ((insn & 0xfbff) == 0xf04f) /* mov.w Rd, #const */
1045 {
1046 unsigned int imm = ((bits (insn, 10, 10) << 11)
1047 | (bits (inst2, 12, 14) << 8)
1048 | bits (inst2, 0, 7));
1049
1050 regs[bits (inst2, 8, 11)]
1051 = pv_constant (thumb_expand_immediate (imm));
1052 }
1053
1054 else if ((insn & 0xfbf0) == 0xf240) /* movw Rd, #const */
1055 {
621c6d5b
YQ
1056 unsigned int imm
1057 = EXTRACT_MOVW_MOVT_IMM_T (insn, inst2);
ec3d575a
UW
1058
1059 regs[bits (inst2, 8, 11)] = pv_constant (imm);
1060 }
1061
1062 else if (insn == 0xea5f /* mov.w Rd,Rm */
1063 && (inst2 & 0xf0f0) == 0)
1064 {
1065 int dst_reg = (inst2 & 0x0f00) >> 8;
1066 int src_reg = inst2 & 0xf;
1067 regs[dst_reg] = regs[src_reg];
1068 }
1069
1070 else if ((insn & 0xff7f) == 0xf85f) /* ldr.w Rt,<label> */
1071 {
1072 /* Constant pool loads. */
1073 unsigned int constant;
1074 CORE_ADDR loc;
1075
1076 offset = bits (insn, 0, 11);
1077 if (insn & 0x0080)
1078 loc = start + 4 + offset;
1079 else
1080 loc = start + 4 - offset;
1081
1082 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1083 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1084 }
1085
1086 else if ((insn & 0xff7f) == 0xe95f) /* ldrd Rt,Rt2,<label> */
1087 {
1088 /* Constant pool loads. */
1089 unsigned int constant;
1090 CORE_ADDR loc;
1091
1092 offset = bits (insn, 0, 7) << 2;
1093 if (insn & 0x0080)
1094 loc = start + 4 + offset;
1095 else
1096 loc = start + 4 - offset;
1097
1098 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1099 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1100
1101 constant = read_memory_unsigned_integer (loc + 4, 4, byte_order);
1102 regs[bits (inst2, 8, 11)] = pv_constant (constant);
1103 }
1104
1105 else if (thumb2_instruction_changes_pc (insn, inst2))
1106 {
1107 /* Don't scan past anything that might change control flow. */
0d39a070
DJ
1108 break;
1109 }
ec3d575a
UW
1110 else
1111 {
1112 /* The optimizer might shove anything into the prologue,
1113 so we just skip what we don't recognize. */
1114 unrecognized_pc = start;
1115 }
0d39a070
DJ
1116
1117 start += 2;
1118 }
ec3d575a 1119 else if (thumb_instruction_changes_pc (insn))
3d74b771 1120 {
ec3d575a 1121 /* Don't scan past anything that might change control flow. */
da3c6d4a 1122 break;
3d74b771 1123 }
ec3d575a
UW
1124 else
1125 {
1126 /* The optimizer might shove anything into the prologue,
1127 so we just skip what we don't recognize. */
1128 unrecognized_pc = start;
1129 }
29d73ae4
DJ
1130
1131 start += 2;
c906108c
SS
1132 }
1133
0d39a070
DJ
1134 if (arm_debug)
1135 fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1136 paddress (gdbarch, start));
1137
ec3d575a
UW
1138 if (unrecognized_pc == 0)
1139 unrecognized_pc = start;
1140
29d73ae4
DJ
1141 if (cache == NULL)
1142 {
1143 do_cleanups (back_to);
ec3d575a 1144 return unrecognized_pc;
29d73ae4
DJ
1145 }
1146
29d73ae4
DJ
1147 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1148 {
1149 /* Frame pointer is fp. Frame size is constant. */
1150 cache->framereg = ARM_FP_REGNUM;
1151 cache->framesize = -regs[ARM_FP_REGNUM].k;
1152 }
1153 else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
1154 {
1155 /* Frame pointer is r7. Frame size is constant. */
1156 cache->framereg = THUMB_FP_REGNUM;
1157 cache->framesize = -regs[THUMB_FP_REGNUM].k;
1158 }
72a2e3dc 1159 else
29d73ae4
DJ
1160 {
1161 /* Try the stack pointer... this is a bit desperate. */
1162 cache->framereg = ARM_SP_REGNUM;
1163 cache->framesize = -regs[ARM_SP_REGNUM].k;
1164 }
29d73ae4
DJ
1165
1166 for (i = 0; i < 16; i++)
1167 if (pv_area_find_reg (stack, gdbarch, i, &offset))
1168 cache->saved_regs[i].addr = offset;
1169
1170 do_cleanups (back_to);
ec3d575a 1171 return unrecognized_pc;
c906108c
SS
1172}
1173
621c6d5b
YQ
1174
1175/* Try to analyze the instructions starting from PC, which load symbol
1176 __stack_chk_guard. Return the address of instruction after loading this
1177 symbol, set the dest register number to *BASEREG, and set the size of
1178 instructions for loading symbol in OFFSET. Return 0 if instructions are
1179 not recognized. */
1180
1181static CORE_ADDR
1182arm_analyze_load_stack_chk_guard(CORE_ADDR pc, struct gdbarch *gdbarch,
1183 unsigned int *destreg, int *offset)
1184{
1185 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1186 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1187 unsigned int low, high, address;
1188
1189 address = 0;
1190 if (is_thumb)
1191 {
1192 unsigned short insn1
1193 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
1194
1195 if ((insn1 & 0xf800) == 0x4800) /* ldr Rd, #immed */
1196 {
1197 *destreg = bits (insn1, 8, 10);
1198 *offset = 2;
1199 address = bits (insn1, 0, 7);
1200 }
1201 else if ((insn1 & 0xfbf0) == 0xf240) /* movw Rd, #const */
1202 {
1203 unsigned short insn2
1204 = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);
1205
1206 low = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1207
1208 insn1
1209 = read_memory_unsigned_integer (pc + 4, 2, byte_order_for_code);
1210 insn2
1211 = read_memory_unsigned_integer (pc + 6, 2, byte_order_for_code);
1212
1213 /* movt Rd, #const */
1214 if ((insn1 & 0xfbc0) == 0xf2c0)
1215 {
1216 high = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1217 *destreg = bits (insn2, 8, 11);
1218 *offset = 8;
1219 address = (high << 16 | low);
1220 }
1221 }
1222 }
1223 else
1224 {
2e9e421f
UW
1225 unsigned int insn
1226 = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
1227
1228 if ((insn & 0x0e5f0000) == 0x041f0000) /* ldr Rd, #immed */
1229 {
1230 address = bits (insn, 0, 11);
1231 *destreg = bits (insn, 12, 15);
1232 *offset = 4;
1233 }
1234 else if ((insn & 0x0ff00000) == 0x03000000) /* movw Rd, #const */
1235 {
1236 low = EXTRACT_MOVW_MOVT_IMM_A (insn);
1237
1238 insn
1239 = read_memory_unsigned_integer (pc + 4, 4, byte_order_for_code);
1240
1241 if ((insn & 0x0ff00000) == 0x03400000) /* movt Rd, #const */
1242 {
1243 high = EXTRACT_MOVW_MOVT_IMM_A (insn);
1244 *destreg = bits (insn, 12, 15);
1245 *offset = 8;
1246 address = (high << 16 | low);
1247 }
1248 }
621c6d5b
YQ
1249 }
1250
1251 return address;
1252}
1253
1254/* Try to skip a sequence of instructions used for stack protector. If PC
0963b4bd
MS
1255 points to the first instruction of this sequence, return the address of
1256 first instruction after this sequence, otherwise, return original PC.
621c6d5b
YQ
1257
1258 On arm, this sequence of instructions is composed of mainly three steps,
1259 Step 1: load symbol __stack_chk_guard,
1260 Step 2: load from address of __stack_chk_guard,
1261 Step 3: store it to somewhere else.
1262
1263 Usually, instructions on step 2 and step 3 are the same on various ARM
1264 architectures. On step 2, it is one instruction 'ldr Rx, [Rn, #0]', and
1265 on step 3, it is also one instruction 'str Rx, [r7, #immd]'. However,
1266 instructions in step 1 vary from different ARM architectures. On ARMv7,
1267 they are,
1268
1269 movw Rn, #:lower16:__stack_chk_guard
1270 movt Rn, #:upper16:__stack_chk_guard
1271
1272 On ARMv5t, it is,
1273
1274 ldr Rn, .Label
1275 ....
1276 .Lable:
1277 .word __stack_chk_guard
1278
1279 Since ldr/str is a very popular instruction, we can't use them as
1280 'fingerprint' or 'signature' of stack protector sequence. Here we choose
1281 sequence {movw/movt, ldr}/ldr/str plus symbol __stack_chk_guard, if not
1282 stripped, as the 'fingerprint' of a stack protector cdoe sequence. */
1283
1284static CORE_ADDR
1285arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
1286{
1287 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1288 unsigned int address, basereg;
1289 struct minimal_symbol *stack_chk_guard;
1290 int offset;
1291 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1292 CORE_ADDR addr;
1293
1294 /* Try to parse the instructions in Step 1. */
1295 addr = arm_analyze_load_stack_chk_guard (pc, gdbarch,
1296 &basereg, &offset);
1297 if (!addr)
1298 return pc;
1299
1300 stack_chk_guard = lookup_minimal_symbol_by_pc (addr);
1301 /* If name of symbol doesn't start with '__stack_chk_guard', this
1302 instruction sequence is not for stack protector. If symbol is
1303 removed, we conservatively think this sequence is for stack protector. */
1304 if (stack_chk_guard
c1c2ab58
UW
1305 && strncmp (SYMBOL_LINKAGE_NAME (stack_chk_guard), "__stack_chk_guard",
1306 strlen ("__stack_chk_guard")) != 0)
621c6d5b
YQ
1307 return pc;
1308
1309 if (is_thumb)
1310 {
1311 unsigned int destreg;
1312 unsigned short insn
1313 = read_memory_unsigned_integer (pc + offset, 2, byte_order_for_code);
1314
1315 /* Step 2: ldr Rd, [Rn, #immed], encoding T1. */
1316 if ((insn & 0xf800) != 0x6800)
1317 return pc;
1318 if (bits (insn, 3, 5) != basereg)
1319 return pc;
1320 destreg = bits (insn, 0, 2);
1321
1322 insn = read_memory_unsigned_integer (pc + offset + 2, 2,
1323 byte_order_for_code);
1324 /* Step 3: str Rd, [Rn, #immed], encoding T1. */
1325 if ((insn & 0xf800) != 0x6000)
1326 return pc;
1327 if (destreg != bits (insn, 0, 2))
1328 return pc;
1329 }
1330 else
1331 {
1332 unsigned int destreg;
1333 unsigned int insn
1334 = read_memory_unsigned_integer (pc + offset, 4, byte_order_for_code);
1335
1336 /* Step 2: ldr Rd, [Rn, #immed], encoding A1. */
1337 if ((insn & 0x0e500000) != 0x04100000)
1338 return pc;
1339 if (bits (insn, 16, 19) != basereg)
1340 return pc;
1341 destreg = bits (insn, 12, 15);
1342 /* Step 3: str Rd, [Rn, #immed], encoding A1. */
1343 insn = read_memory_unsigned_integer (pc + offset + 4,
1344 4, byte_order_for_code);
1345 if ((insn & 0x0e500000) != 0x04000000)
1346 return pc;
1347 if (bits (insn, 12, 15) != destreg)
1348 return pc;
1349 }
1350 /* The size of total two instructions ldr/str is 4 on Thumb-2, while 8
1351 on arm. */
1352 if (is_thumb)
1353 return pc + offset + 4;
1354 else
1355 return pc + offset + 8;
1356}
1357
da3c6d4a
MS
1358/* Advance the PC across any function entry prologue instructions to
1359 reach some "real" code.
34e8f22d
RE
1360
1361 The APCS (ARM Procedure Call Standard) defines the following
ed9a39eb 1362 prologue:
c906108c 1363
c5aa993b
JM
1364 mov ip, sp
1365 [stmfd sp!, {a1,a2,a3,a4}]
1366 stmfd sp!, {...,fp,ip,lr,pc}
ed9a39eb
JM
1367 [stfe f7, [sp, #-12]!]
1368 [stfe f6, [sp, #-12]!]
1369 [stfe f5, [sp, #-12]!]
1370 [stfe f4, [sp, #-12]!]
0963b4bd 1371 sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn. */
c906108c 1372
34e8f22d 1373static CORE_ADDR
6093d2eb 1374arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
c906108c 1375{
e17a4113 1376 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
c906108c
SS
1377 unsigned long inst;
1378 CORE_ADDR skip_pc;
a89fea3c 1379 CORE_ADDR func_addr, limit_pc;
c906108c
SS
1380 struct symtab_and_line sal;
1381
a89fea3c
JL
1382 /* See if we can determine the end of the prologue via the symbol table.
1383 If so, then return either PC, or the PC after the prologue, whichever
1384 is greater. */
1385 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
c906108c 1386 {
d80b854b
UW
1387 CORE_ADDR post_prologue_pc
1388 = skip_prologue_using_sal (gdbarch, func_addr);
0d39a070
DJ
1389 struct symtab *s = find_pc_symtab (func_addr);
1390
621c6d5b
YQ
1391 if (post_prologue_pc)
1392 post_prologue_pc
1393 = arm_skip_stack_protector (post_prologue_pc, gdbarch);
1394
1395
0d39a070
DJ
1396 /* GCC always emits a line note before the prologue and another
1397 one after, even if the two are at the same address or on the
1398 same line. Take advantage of this so that we do not need to
1399 know every instruction that might appear in the prologue. We
1400 will have producer information for most binaries; if it is
1401 missing (e.g. for -gstabs), assuming the GNU tools. */
1402 if (post_prologue_pc
1403 && (s == NULL
1404 || s->producer == NULL
1405 || strncmp (s->producer, "GNU ", sizeof ("GNU ") - 1) == 0))
1406 return post_prologue_pc;
1407
a89fea3c 1408 if (post_prologue_pc != 0)
0d39a070
DJ
1409 {
1410 CORE_ADDR analyzed_limit;
1411
1412 /* For non-GCC compilers, make sure the entire line is an
1413 acceptable prologue; GDB will round this function's
1414 return value up to the end of the following line so we
1415 can not skip just part of a line (and we do not want to).
1416
1417 RealView does not treat the prologue specially, but does
1418 associate prologue code with the opening brace; so this
1419 lets us skip the first line if we think it is the opening
1420 brace. */
9779414d 1421 if (arm_pc_is_thumb (gdbarch, func_addr))
0d39a070
DJ
1422 analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
1423 post_prologue_pc, NULL);
1424 else
1425 analyzed_limit = arm_analyze_prologue (gdbarch, func_addr,
1426 post_prologue_pc, NULL);
1427
1428 if (analyzed_limit != post_prologue_pc)
1429 return func_addr;
1430
1431 return post_prologue_pc;
1432 }
c906108c
SS
1433 }
1434
a89fea3c
JL
1435 /* Can't determine prologue from the symbol table, need to examine
1436 instructions. */
c906108c 1437
a89fea3c
JL
1438 /* Find an upper limit on the function prologue using the debug
1439 information. If the debug information could not be used to provide
1440 that bound, then use an arbitrary large number as the upper bound. */
0963b4bd 1441 /* Like arm_scan_prologue, stop no later than pc + 64. */
d80b854b 1442 limit_pc = skip_prologue_using_sal (gdbarch, pc);
a89fea3c
JL
1443 if (limit_pc == 0)
1444 limit_pc = pc + 64; /* Magic. */
1445
c906108c 1446
29d73ae4 1447 /* Check if this is Thumb code. */
9779414d 1448 if (arm_pc_is_thumb (gdbarch, pc))
a89fea3c 1449 return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
29d73ae4 1450
a89fea3c 1451 for (skip_pc = pc; skip_pc < limit_pc; skip_pc += 4)
f43845b3 1452 {
e17a4113 1453 inst = read_memory_unsigned_integer (skip_pc, 4, byte_order_for_code);
9d4fde75 1454
b8d5e71d
MS
1455 /* "mov ip, sp" is no longer a required part of the prologue. */
1456 if (inst == 0xe1a0c00d) /* mov ip, sp */
1457 continue;
c906108c 1458
28cd8767
JG
1459 if ((inst & 0xfffff000) == 0xe28dc000) /* add ip, sp #n */
1460 continue;
1461
1462 if ((inst & 0xfffff000) == 0xe24dc000) /* sub ip, sp #n */
1463 continue;
1464
b8d5e71d
MS
1465 /* Some prologues begin with "str lr, [sp, #-4]!". */
1466 if (inst == 0xe52de004) /* str lr, [sp, #-4]! */
1467 continue;
c906108c 1468
b8d5e71d
MS
1469 if ((inst & 0xfffffff0) == 0xe92d0000) /* stmfd sp!,{a1,a2,a3,a4} */
1470 continue;
c906108c 1471
b8d5e71d
MS
1472 if ((inst & 0xfffff800) == 0xe92dd800) /* stmfd sp!,{fp,ip,lr,pc} */
1473 continue;
11d3b27d 1474
b8d5e71d
MS
1475 /* Any insns after this point may float into the code, if it makes
1476 for better instruction scheduling, so we skip them only if we
1477 find them, but still consider the function to be frame-ful. */
f43845b3 1478
b8d5e71d
MS
1479 /* We may have either one sfmfd instruction here, or several stfe
1480 insns, depending on the version of floating point code we
1481 support. */
1482 if ((inst & 0xffbf0fff) == 0xec2d0200) /* sfmfd fn, <cnt>, [sp]! */
1483 continue;
1484
1485 if ((inst & 0xffff8fff) == 0xed6d0103) /* stfe fn, [sp, #-12]! */
1486 continue;
1487
1488 if ((inst & 0xfffff000) == 0xe24cb000) /* sub fp, ip, #nn */
1489 continue;
1490
1491 if ((inst & 0xfffff000) == 0xe24dd000) /* sub sp, sp, #nn */
1492 continue;
1493
f8bf5763
PM
1494 if ((inst & 0xffffc000) == 0xe54b0000 /* strb r(0123),[r11,#-nn] */
1495 || (inst & 0xffffc0f0) == 0xe14b00b0 /* strh r(0123),[r11,#-nn] */
1496 || (inst & 0xffffc000) == 0xe50b0000) /* str r(0123),[r11,#-nn] */
b8d5e71d
MS
1497 continue;
1498
f8bf5763
PM
1499 if ((inst & 0xffffc000) == 0xe5cd0000 /* strb r(0123),[sp,#nn] */
1500 || (inst & 0xffffc0f0) == 0xe1cd00b0 /* strh r(0123),[sp,#nn] */
1501 || (inst & 0xffffc000) == 0xe58d0000) /* str r(0123),[sp,#nn] */
b8d5e71d
MS
1502 continue;
1503
1504 /* Un-recognized instruction; stop scanning. */
1505 break;
f43845b3 1506 }
c906108c 1507
0963b4bd 1508 return skip_pc; /* End of prologue. */
c906108c 1509}
94c30b78 1510
c5aa993b 1511/* *INDENT-OFF* */
c906108c
SS
1512/* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
1513 This function decodes a Thumb function prologue to determine:
1514 1) the size of the stack frame
1515 2) which registers are saved on it
1516 3) the offsets of saved regs
1517 4) the offset from the stack pointer to the frame pointer
c906108c 1518
da59e081
JM
1519 A typical Thumb function prologue would create this stack frame
1520 (offsets relative to FP)
c906108c
SS
1521 old SP -> 24 stack parameters
1522 20 LR
1523 16 R7
1524 R7 -> 0 local variables (16 bytes)
1525 SP -> -12 additional stack space (12 bytes)
1526 The frame size would thus be 36 bytes, and the frame offset would be
0963b4bd 1527 12 bytes. The frame register is R7.
da59e081 1528
da3c6d4a
MS
1529 The comments for thumb_skip_prolog() describe the algorithm we use
1530 to detect the end of the prolog. */
c5aa993b
JM
1531/* *INDENT-ON* */
1532
c906108c 1533static void
be8626e0 1534thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
b39cc962 1535 CORE_ADDR block_addr, struct arm_prologue_cache *cache)
c906108c
SS
1536{
1537 CORE_ADDR prologue_start;
1538 CORE_ADDR prologue_end;
1539 CORE_ADDR current_pc;
c906108c 1540
b39cc962
DJ
1541 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1542 &prologue_end))
c906108c 1543 {
ec3d575a
UW
1544 /* See comment in arm_scan_prologue for an explanation of
1545 this heuristics. */
1546 if (prologue_end > prologue_start + 64)
1547 {
1548 prologue_end = prologue_start + 64;
1549 }
c906108c
SS
1550 }
1551 else
f7060f85
DJ
1552 /* We're in the boondocks: we have no idea where the start of the
1553 function is. */
1554 return;
c906108c 1555
eb5492fa 1556 prologue_end = min (prologue_end, prev_pc);
c906108c 1557
be8626e0 1558 thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
1559}
1560
0d39a070 1561/* Return 1 if THIS_INSTR might change control flow, 0 otherwise. */
c906108c 1562
0d39a070
DJ
1563static int
1564arm_instruction_changes_pc (uint32_t this_instr)
c906108c 1565{
0d39a070
DJ
1566 if (bits (this_instr, 28, 31) == INST_NV)
1567 /* Unconditional instructions. */
1568 switch (bits (this_instr, 24, 27))
1569 {
1570 case 0xa:
1571 case 0xb:
1572 /* Branch with Link and change to Thumb. */
1573 return 1;
1574 case 0xc:
1575 case 0xd:
1576 case 0xe:
1577 /* Coprocessor register transfer. */
1578 if (bits (this_instr, 12, 15) == 15)
1579 error (_("Invalid update to pc in instruction"));
1580 return 0;
1581 default:
1582 return 0;
1583 }
1584 else
1585 switch (bits (this_instr, 25, 27))
1586 {
1587 case 0x0:
1588 if (bits (this_instr, 23, 24) == 2 && bit (this_instr, 20) == 0)
1589 {
1590 /* Multiplies and extra load/stores. */
1591 if (bit (this_instr, 4) == 1 && bit (this_instr, 7) == 1)
1592 /* Neither multiplies nor extension load/stores are allowed
1593 to modify PC. */
1594 return 0;
1595
1596 /* Otherwise, miscellaneous instructions. */
1597
1598 /* BX <reg>, BXJ <reg>, BLX <reg> */
1599 if (bits (this_instr, 4, 27) == 0x12fff1
1600 || bits (this_instr, 4, 27) == 0x12fff2
1601 || bits (this_instr, 4, 27) == 0x12fff3)
1602 return 1;
1603
1604 /* Other miscellaneous instructions are unpredictable if they
1605 modify PC. */
1606 return 0;
1607 }
1608 /* Data processing instruction. Fall through. */
c906108c 1609
0d39a070
DJ
1610 case 0x1:
1611 if (bits (this_instr, 12, 15) == 15)
1612 return 1;
1613 else
1614 return 0;
c906108c 1615
0d39a070
DJ
1616 case 0x2:
1617 case 0x3:
1618 /* Media instructions and architecturally undefined instructions. */
1619 if (bits (this_instr, 25, 27) == 3 && bit (this_instr, 4) == 1)
1620 return 0;
c906108c 1621
0d39a070
DJ
1622 /* Stores. */
1623 if (bit (this_instr, 20) == 0)
1624 return 0;
2a451106 1625
0d39a070
DJ
1626 /* Loads. */
1627 if (bits (this_instr, 12, 15) == ARM_PC_REGNUM)
1628 return 1;
1629 else
1630 return 0;
2a451106 1631
0d39a070
DJ
1632 case 0x4:
1633 /* Load/store multiple. */
1634 if (bit (this_instr, 20) == 1 && bit (this_instr, 15) == 1)
1635 return 1;
1636 else
1637 return 0;
2a451106 1638
0d39a070
DJ
1639 case 0x5:
1640 /* Branch and branch with link. */
1641 return 1;
2a451106 1642
0d39a070
DJ
1643 case 0x6:
1644 case 0x7:
1645 /* Coprocessor transfers or SWIs can not affect PC. */
1646 return 0;
eb5492fa 1647
0d39a070 1648 default:
9b20d036 1649 internal_error (__FILE__, __LINE__, _("bad value in switch"));
0d39a070
DJ
1650 }
1651}
c906108c 1652
0d39a070
DJ
1653/* Analyze an ARM mode prologue starting at PROLOGUE_START and
1654 continuing no further than PROLOGUE_END. If CACHE is non-NULL,
1655 fill it in. Return the first address not recognized as a prologue
1656 instruction.
eb5492fa 1657
0d39a070
DJ
1658 We recognize all the instructions typically found in ARM prologues,
1659 plus harmless instructions which can be skipped (either for analysis
1660 purposes, or a more restrictive set that can be skipped when finding
1661 the end of the prologue). */
1662
1663static CORE_ADDR
1664arm_analyze_prologue (struct gdbarch *gdbarch,
1665 CORE_ADDR prologue_start, CORE_ADDR prologue_end,
1666 struct arm_prologue_cache *cache)
1667{
1668 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1669 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1670 int regno;
1671 CORE_ADDR offset, current_pc;
1672 pv_t regs[ARM_FPS_REGNUM];
1673 struct pv_area *stack;
1674 struct cleanup *back_to;
1675 int framereg, framesize;
1676 CORE_ADDR unrecognized_pc = 0;
1677
1678 /* Search the prologue looking for instructions that set up the
96baa820 1679 frame pointer, adjust the stack pointer, and save registers.
ed9a39eb 1680
96baa820
JM
1681 Be careful, however, and if it doesn't look like a prologue,
1682 don't try to scan it. If, for instance, a frameless function
1683 begins with stmfd sp!, then we will tell ourselves there is
b8d5e71d 1684 a frame, which will confuse stack traceback, as well as "finish"
96baa820 1685 and other operations that rely on a knowledge of the stack
0d39a070 1686 traceback. */
d4473757 1687
4be43953
DJ
1688 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1689 regs[regno] = pv_register (regno, 0);
55f960e1 1690 stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
4be43953
DJ
1691 back_to = make_cleanup_free_pv_area (stack);
1692
94c30b78
MS
1693 for (current_pc = prologue_start;
1694 current_pc < prologue_end;
f43845b3 1695 current_pc += 4)
96baa820 1696 {
e17a4113
UW
1697 unsigned int insn
1698 = read_memory_unsigned_integer (current_pc, 4, byte_order_for_code);
9d4fde75 1699
94c30b78 1700 if (insn == 0xe1a0c00d) /* mov ip, sp */
f43845b3 1701 {
4be43953 1702 regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
28cd8767
JG
1703 continue;
1704 }
0d39a070
DJ
1705 else if ((insn & 0xfff00000) == 0xe2800000 /* add Rd, Rn, #n */
1706 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767
JG
1707 {
1708 unsigned imm = insn & 0xff; /* immediate value */
1709 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
0d39a070 1710 int rd = bits (insn, 12, 15);
28cd8767 1711 imm = (imm >> rot) | (imm << (32 - rot));
0d39a070 1712 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
28cd8767
JG
1713 continue;
1714 }
0d39a070
DJ
1715 else if ((insn & 0xfff00000) == 0xe2400000 /* sub Rd, Rn, #n */
1716 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767
JG
1717 {
1718 unsigned imm = insn & 0xff; /* immediate value */
1719 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
0d39a070 1720 int rd = bits (insn, 12, 15);
28cd8767 1721 imm = (imm >> rot) | (imm << (32 - rot));
0d39a070 1722 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
f43845b3
MS
1723 continue;
1724 }
0963b4bd
MS
1725 else if ((insn & 0xffff0fff) == 0xe52d0004) /* str Rd,
1726 [sp, #-4]! */
f43845b3 1727 {
4be43953
DJ
1728 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1729 break;
1730 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
0d39a070
DJ
1731 pv_area_store (stack, regs[ARM_SP_REGNUM], 4,
1732 regs[bits (insn, 12, 15)]);
f43845b3
MS
1733 continue;
1734 }
1735 else if ((insn & 0xffff0000) == 0xe92d0000)
d4473757
KB
1736 /* stmfd sp!, {..., fp, ip, lr, pc}
1737 or
1738 stmfd sp!, {a1, a2, a3, a4} */
c906108c 1739 {
d4473757 1740 int mask = insn & 0xffff;
ed9a39eb 1741
4be43953
DJ
1742 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1743 break;
1744
94c30b78 1745 /* Calculate offsets of saved registers. */
34e8f22d 1746 for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
d4473757
KB
1747 if (mask & (1 << regno))
1748 {
0963b4bd
MS
1749 regs[ARM_SP_REGNUM]
1750 = pv_add_constant (regs[ARM_SP_REGNUM], -4);
4be43953 1751 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
d4473757
KB
1752 }
1753 }
0d39a070
DJ
1754 else if ((insn & 0xffff0000) == 0xe54b0000 /* strb rx,[r11,#-n] */
1755 || (insn & 0xffff00f0) == 0xe14b00b0 /* strh rx,[r11,#-n] */
f8bf5763 1756 || (insn & 0xffffc000) == 0xe50b0000) /* str rx,[r11,#-n] */
b8d5e71d
MS
1757 {
1758 /* No need to add this to saved_regs -- it's just an arg reg. */
1759 continue;
1760 }
0d39a070
DJ
1761 else if ((insn & 0xffff0000) == 0xe5cd0000 /* strb rx,[sp,#n] */
1762 || (insn & 0xffff00f0) == 0xe1cd00b0 /* strh rx,[sp,#n] */
f8bf5763 1763 || (insn & 0xffffc000) == 0xe58d0000) /* str rx,[sp,#n] */
f43845b3
MS
1764 {
1765 /* No need to add this to saved_regs -- it's just an arg reg. */
1766 continue;
1767 }
0963b4bd
MS
1768 else if ((insn & 0xfff00000) == 0xe8800000 /* stm Rn,
1769 { registers } */
0d39a070
DJ
1770 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1771 {
1772 /* No need to add this to saved_regs -- it's just arg regs. */
1773 continue;
1774 }
d4473757
KB
1775 else if ((insn & 0xfffff000) == 0xe24cb000) /* sub fp, ip #n */
1776 {
94c30b78
MS
1777 unsigned imm = insn & 0xff; /* immediate value */
1778 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
d4473757 1779 imm = (imm >> rot) | (imm << (32 - rot));
4be43953 1780 regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
d4473757
KB
1781 }
1782 else if ((insn & 0xfffff000) == 0xe24dd000) /* sub sp, sp #n */
1783 {
94c30b78
MS
1784 unsigned imm = insn & 0xff; /* immediate value */
1785 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
d4473757 1786 imm = (imm >> rot) | (imm << (32 - rot));
4be43953 1787 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
d4473757 1788 }
0963b4bd
MS
1789 else if ((insn & 0xffff7fff) == 0xed6d0103 /* stfe f?,
1790 [sp, -#c]! */
2af46ca0 1791 && gdbarch_tdep (gdbarch)->have_fpa_registers)
d4473757 1792 {
4be43953
DJ
1793 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1794 break;
1795
1796 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
34e8f22d 1797 regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
4be43953 1798 pv_area_store (stack, regs[ARM_SP_REGNUM], 12, regs[regno]);
d4473757 1799 }
0963b4bd
MS
1800 else if ((insn & 0xffbf0fff) == 0xec2d0200 /* sfmfd f0, 4,
1801 [sp!] */
2af46ca0 1802 && gdbarch_tdep (gdbarch)->have_fpa_registers)
d4473757
KB
1803 {
1804 int n_saved_fp_regs;
1805 unsigned int fp_start_reg, fp_bound_reg;
1806
4be43953
DJ
1807 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1808 break;
1809
94c30b78 1810 if ((insn & 0x800) == 0x800) /* N0 is set */
96baa820 1811 {
d4473757
KB
1812 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1813 n_saved_fp_regs = 3;
1814 else
1815 n_saved_fp_regs = 1;
96baa820 1816 }
d4473757 1817 else
96baa820 1818 {
d4473757
KB
1819 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1820 n_saved_fp_regs = 2;
1821 else
1822 n_saved_fp_regs = 4;
96baa820 1823 }
d4473757 1824
34e8f22d 1825 fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
d4473757
KB
1826 fp_bound_reg = fp_start_reg + n_saved_fp_regs;
1827 for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
96baa820 1828 {
4be43953
DJ
1829 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
1830 pv_area_store (stack, regs[ARM_SP_REGNUM], 12,
1831 regs[fp_start_reg++]);
96baa820 1832 }
c906108c 1833 }
0d39a070
DJ
1834 else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
1835 {
1836 /* Allow some special function calls when skipping the
1837 prologue; GCC generates these before storing arguments to
1838 the stack. */
1839 CORE_ADDR dest = BranchDest (current_pc, insn);
1840
e0634ccf 1841 if (skip_prologue_function (gdbarch, dest, 0))
0d39a070
DJ
1842 continue;
1843 else
1844 break;
1845 }
d4473757 1846 else if ((insn & 0xf0000000) != 0xe0000000)
0963b4bd 1847 break; /* Condition not true, exit early. */
0d39a070
DJ
1848 else if (arm_instruction_changes_pc (insn))
1849 /* Don't scan past anything that might change control flow. */
1850 break;
d19f7eee
UW
1851 else if ((insn & 0xfe500000) == 0xe8100000 /* ldm */
1852 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1853 /* Ignore block loads from the stack, potentially copying
1854 parameters from memory. */
1855 continue;
1856 else if ((insn & 0xfc500000) == 0xe4100000
1857 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1858 /* Similarly ignore single loads from the stack. */
1859 continue;
0d39a070
DJ
1860 else if ((insn & 0xffff0ff0) == 0xe1a00000)
1861 /* MOV Rd, Rm. Skip register copies, i.e. saves to another
1862 register instead of the stack. */
d4473757 1863 continue;
0d39a070
DJ
1864 else
1865 {
1866 /* The optimizer might shove anything into the prologue,
1867 so we just skip what we don't recognize. */
1868 unrecognized_pc = current_pc;
1869 continue;
1870 }
c906108c
SS
1871 }
1872
0d39a070
DJ
1873 if (unrecognized_pc == 0)
1874 unrecognized_pc = current_pc;
1875
4be43953
DJ
1876 /* The frame size is just the distance from the frame register
1877 to the original stack pointer. */
1878 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1879 {
1880 /* Frame pointer is fp. */
0d39a070
DJ
1881 framereg = ARM_FP_REGNUM;
1882 framesize = -regs[ARM_FP_REGNUM].k;
4be43953 1883 }
72a2e3dc 1884 else
4be43953
DJ
1885 {
1886 /* Try the stack pointer... this is a bit desperate. */
0d39a070
DJ
1887 framereg = ARM_SP_REGNUM;
1888 framesize = -regs[ARM_SP_REGNUM].k;
4be43953 1889 }
4be43953 1890
0d39a070
DJ
1891 if (cache)
1892 {
1893 cache->framereg = framereg;
1894 cache->framesize = framesize;
1895
1896 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1897 if (pv_area_find_reg (stack, gdbarch, regno, &offset))
1898 cache->saved_regs[regno].addr = offset;
1899 }
1900
1901 if (arm_debug)
1902 fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1903 paddress (gdbarch, unrecognized_pc));
4be43953
DJ
1904
1905 do_cleanups (back_to);
0d39a070
DJ
1906 return unrecognized_pc;
1907}
1908
1909static void
1910arm_scan_prologue (struct frame_info *this_frame,
1911 struct arm_prologue_cache *cache)
1912{
1913 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1914 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1915 int regno;
1916 CORE_ADDR prologue_start, prologue_end, current_pc;
1917 CORE_ADDR prev_pc = get_frame_pc (this_frame);
1918 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1919 pv_t regs[ARM_FPS_REGNUM];
1920 struct pv_area *stack;
1921 struct cleanup *back_to;
1922 CORE_ADDR offset;
1923
1924 /* Assume there is no frame until proven otherwise. */
1925 cache->framereg = ARM_SP_REGNUM;
1926 cache->framesize = 0;
1927
1928 /* Check for Thumb prologue. */
1929 if (arm_frame_is_thumb (this_frame))
1930 {
1931 thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
1932 return;
1933 }
1934
1935 /* Find the function prologue. If we can't find the function in
1936 the symbol table, peek in the stack frame to find the PC. */
1937 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1938 &prologue_end))
1939 {
1940 /* One way to find the end of the prologue (which works well
1941 for unoptimized code) is to do the following:
1942
1943 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
1944
1945 if (sal.line == 0)
1946 prologue_end = prev_pc;
1947 else if (sal.end < prologue_end)
1948 prologue_end = sal.end;
1949
1950 This mechanism is very accurate so long as the optimizer
1951 doesn't move any instructions from the function body into the
1952 prologue. If this happens, sal.end will be the last
1953 instruction in the first hunk of prologue code just before
1954 the first instruction that the scheduler has moved from
1955 the body to the prologue.
1956
1957 In order to make sure that we scan all of the prologue
1958 instructions, we use a slightly less accurate mechanism which
1959 may scan more than necessary. To help compensate for this
1960 lack of accuracy, the prologue scanning loop below contains
1961 several clauses which'll cause the loop to terminate early if
1962 an implausible prologue instruction is encountered.
1963
1964 The expression
1965
1966 prologue_start + 64
1967
1968 is a suitable endpoint since it accounts for the largest
1969 possible prologue plus up to five instructions inserted by
1970 the scheduler. */
1971
1972 if (prologue_end > prologue_start + 64)
1973 {
1974 prologue_end = prologue_start + 64; /* See above. */
1975 }
1976 }
1977 else
1978 {
1979 /* We have no symbol information. Our only option is to assume this
1980 function has a standard stack frame and the normal frame register.
1981 Then, we can find the value of our frame pointer on entrance to
1982 the callee (or at the present moment if this is the innermost frame).
1983 The value stored there should be the address of the stmfd + 8. */
1984 CORE_ADDR frame_loc;
1985 LONGEST return_value;
1986
1987 frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
1988 if (!safe_read_memory_integer (frame_loc, 4, byte_order, &return_value))
1989 return;
1990 else
1991 {
1992 prologue_start = gdbarch_addr_bits_remove
1993 (gdbarch, return_value) - 8;
1994 prologue_end = prologue_start + 64; /* See above. */
1995 }
1996 }
1997
1998 if (prev_pc < prologue_end)
1999 prologue_end = prev_pc;
2000
2001 arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
2002}
2003
eb5492fa 2004static struct arm_prologue_cache *
a262aec2 2005arm_make_prologue_cache (struct frame_info *this_frame)
c906108c 2006{
eb5492fa
DJ
2007 int reg;
2008 struct arm_prologue_cache *cache;
2009 CORE_ADDR unwound_fp;
c5aa993b 2010
35d5d4ee 2011 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
a262aec2 2012 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
c906108c 2013
a262aec2 2014 arm_scan_prologue (this_frame, cache);
848cfffb 2015
a262aec2 2016 unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
eb5492fa
DJ
2017 if (unwound_fp == 0)
2018 return cache;
c906108c 2019
4be43953 2020 cache->prev_sp = unwound_fp + cache->framesize;
c906108c 2021
eb5492fa
DJ
2022 /* Calculate actual addresses of saved registers using offsets
2023 determined by arm_scan_prologue. */
a262aec2 2024 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
e28a332c 2025 if (trad_frame_addr_p (cache->saved_regs, reg))
eb5492fa
DJ
2026 cache->saved_regs[reg].addr += cache->prev_sp;
2027
2028 return cache;
c906108c
SS
2029}
2030
eb5492fa
DJ
2031/* Our frame ID for a normal frame is the current function's starting PC
2032 and the caller's SP when we were called. */
c906108c 2033
148754e5 2034static void
a262aec2 2035arm_prologue_this_id (struct frame_info *this_frame,
eb5492fa
DJ
2036 void **this_cache,
2037 struct frame_id *this_id)
c906108c 2038{
eb5492fa
DJ
2039 struct arm_prologue_cache *cache;
2040 struct frame_id id;
2c404490 2041 CORE_ADDR pc, func;
f079148d 2042
eb5492fa 2043 if (*this_cache == NULL)
a262aec2 2044 *this_cache = arm_make_prologue_cache (this_frame);
eb5492fa 2045 cache = *this_cache;
2a451106 2046
2c404490
DJ
2047 /* This is meant to halt the backtrace at "_start". */
2048 pc = get_frame_pc (this_frame);
2049 if (pc <= gdbarch_tdep (get_frame_arch (this_frame))->lowest_pc)
eb5492fa 2050 return;
5a203e44 2051
eb5492fa
DJ
2052 /* If we've hit a wall, stop. */
2053 if (cache->prev_sp == 0)
2054 return;
24de872b 2055
0e9e9abd
UW
2056 /* Use function start address as part of the frame ID. If we cannot
2057 identify the start address (due to missing symbol information),
2058 fall back to just using the current PC. */
2c404490 2059 func = get_frame_func (this_frame);
0e9e9abd
UW
2060 if (!func)
2061 func = pc;
2062
eb5492fa 2063 id = frame_id_build (cache->prev_sp, func);
eb5492fa 2064 *this_id = id;
c906108c
SS
2065}
2066
a262aec2
DJ
2067static struct value *
2068arm_prologue_prev_register (struct frame_info *this_frame,
eb5492fa 2069 void **this_cache,
a262aec2 2070 int prev_regnum)
24de872b 2071{
24568a2c 2072 struct gdbarch *gdbarch = get_frame_arch (this_frame);
24de872b
DJ
2073 struct arm_prologue_cache *cache;
2074
eb5492fa 2075 if (*this_cache == NULL)
a262aec2 2076 *this_cache = arm_make_prologue_cache (this_frame);
eb5492fa 2077 cache = *this_cache;
24de872b 2078
eb5492fa 2079 /* If we are asked to unwind the PC, then we need to return the LR
b39cc962
DJ
2080 instead. The prologue may save PC, but it will point into this
2081 frame's prologue, not the next frame's resume location. Also
2082 strip the saved T bit. A valid LR may have the low bit set, but
2083 a valid PC never does. */
eb5492fa 2084 if (prev_regnum == ARM_PC_REGNUM)
b39cc962
DJ
2085 {
2086 CORE_ADDR lr;
2087
2088 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2089 return frame_unwind_got_constant (this_frame, prev_regnum,
24568a2c 2090 arm_addr_bits_remove (gdbarch, lr));
b39cc962 2091 }
24de872b 2092
eb5492fa 2093 /* SP is generally not saved to the stack, but this frame is
a262aec2 2094 identified by the next frame's stack pointer at the time of the call.
eb5492fa
DJ
2095 The value was already reconstructed into PREV_SP. */
2096 if (prev_regnum == ARM_SP_REGNUM)
a262aec2 2097 return frame_unwind_got_constant (this_frame, prev_regnum, cache->prev_sp);
eb5492fa 2098
b39cc962
DJ
2099 /* The CPSR may have been changed by the call instruction and by the
2100 called function. The only bit we can reconstruct is the T bit,
2101 by checking the low bit of LR as of the call. This is a reliable
2102 indicator of Thumb-ness except for some ARM v4T pre-interworking
2103 Thumb code, which could get away with a clear low bit as long as
2104 the called function did not use bx. Guess that all other
2105 bits are unchanged; the condition flags are presumably lost,
2106 but the processor status is likely valid. */
2107 if (prev_regnum == ARM_PS_REGNUM)
2108 {
2109 CORE_ADDR lr, cpsr;
9779414d 2110 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
b39cc962
DJ
2111
2112 cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
2113 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2114 if (IS_THUMB_ADDR (lr))
9779414d 2115 cpsr |= t_bit;
b39cc962 2116 else
9779414d 2117 cpsr &= ~t_bit;
b39cc962
DJ
2118 return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
2119 }
2120
a262aec2
DJ
2121 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
2122 prev_regnum);
eb5492fa
DJ
2123}
2124
2125struct frame_unwind arm_prologue_unwind = {
2126 NORMAL_FRAME,
8fbca658 2127 default_frame_unwind_stop_reason,
eb5492fa 2128 arm_prologue_this_id,
a262aec2
DJ
2129 arm_prologue_prev_register,
2130 NULL,
2131 default_frame_sniffer
eb5492fa
DJ
2132};
2133
0e9e9abd
UW
2134/* Maintain a list of ARM exception table entries per objfile, similar to the
2135 list of mapping symbols. We only cache entries for standard ARM-defined
2136 personality routines; the cache will contain only the frame unwinding
2137 instructions associated with the entry (not the descriptors). */
2138
2139static const struct objfile_data *arm_exidx_data_key;
2140
2141struct arm_exidx_entry
2142{
2143 bfd_vma addr;
2144 gdb_byte *entry;
2145};
2146typedef struct arm_exidx_entry arm_exidx_entry_s;
2147DEF_VEC_O(arm_exidx_entry_s);
2148
2149struct arm_exidx_data
2150{
2151 VEC(arm_exidx_entry_s) **section_maps;
2152};
2153
2154static void
2155arm_exidx_data_free (struct objfile *objfile, void *arg)
2156{
2157 struct arm_exidx_data *data = arg;
2158 unsigned int i;
2159
2160 for (i = 0; i < objfile->obfd->section_count; i++)
2161 VEC_free (arm_exidx_entry_s, data->section_maps[i]);
2162}
2163
2164static inline int
2165arm_compare_exidx_entries (const struct arm_exidx_entry *lhs,
2166 const struct arm_exidx_entry *rhs)
2167{
2168 return lhs->addr < rhs->addr;
2169}
2170
2171static struct obj_section *
2172arm_obj_section_from_vma (struct objfile *objfile, bfd_vma vma)
2173{
2174 struct obj_section *osect;
2175
2176 ALL_OBJFILE_OSECTIONS (objfile, osect)
2177 if (bfd_get_section_flags (objfile->obfd,
2178 osect->the_bfd_section) & SEC_ALLOC)
2179 {
2180 bfd_vma start, size;
2181 start = bfd_get_section_vma (objfile->obfd, osect->the_bfd_section);
2182 size = bfd_get_section_size (osect->the_bfd_section);
2183
2184 if (start <= vma && vma < start + size)
2185 return osect;
2186 }
2187
2188 return NULL;
2189}
2190
2191/* Parse contents of exception table and exception index sections
2192 of OBJFILE, and fill in the exception table entry cache.
2193
2194 For each entry that refers to a standard ARM-defined personality
2195 routine, extract the frame unwinding instructions (from either
2196 the index or the table section). The unwinding instructions
2197 are normalized by:
2198 - extracting them from the rest of the table data
2199 - converting to host endianness
2200 - appending the implicit 0xb0 ("Finish") code
2201
2202 The extracted and normalized instructions are stored for later
2203 retrieval by the arm_find_exidx_entry routine. */
2204
2205static void
2206arm_exidx_new_objfile (struct objfile *objfile)
2207{
3bb47e8b 2208 struct cleanup *cleanups;
0e9e9abd
UW
2209 struct arm_exidx_data *data;
2210 asection *exidx, *extab;
2211 bfd_vma exidx_vma = 0, extab_vma = 0;
2212 bfd_size_type exidx_size = 0, extab_size = 0;
2213 gdb_byte *exidx_data = NULL, *extab_data = NULL;
2214 LONGEST i;
2215
2216 /* If we've already touched this file, do nothing. */
2217 if (!objfile || objfile_data (objfile, arm_exidx_data_key) != NULL)
2218 return;
3bb47e8b 2219 cleanups = make_cleanup (null_cleanup, NULL);
0e9e9abd
UW
2220
2221 /* Read contents of exception table and index. */
2222 exidx = bfd_get_section_by_name (objfile->obfd, ".ARM.exidx");
2223 if (exidx)
2224 {
2225 exidx_vma = bfd_section_vma (objfile->obfd, exidx);
2226 exidx_size = bfd_get_section_size (exidx);
2227 exidx_data = xmalloc (exidx_size);
2228 make_cleanup (xfree, exidx_data);
2229
2230 if (!bfd_get_section_contents (objfile->obfd, exidx,
2231 exidx_data, 0, exidx_size))
2232 {
2233 do_cleanups (cleanups);
2234 return;
2235 }
2236 }
2237
2238 extab = bfd_get_section_by_name (objfile->obfd, ".ARM.extab");
2239 if (extab)
2240 {
2241 extab_vma = bfd_section_vma (objfile->obfd, extab);
2242 extab_size = bfd_get_section_size (extab);
2243 extab_data = xmalloc (extab_size);
2244 make_cleanup (xfree, extab_data);
2245
2246 if (!bfd_get_section_contents (objfile->obfd, extab,
2247 extab_data, 0, extab_size))
2248 {
2249 do_cleanups (cleanups);
2250 return;
2251 }
2252 }
2253
2254 /* Allocate exception table data structure. */
2255 data = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct arm_exidx_data);
2256 set_objfile_data (objfile, arm_exidx_data_key, data);
2257 data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
2258 objfile->obfd->section_count,
2259 VEC(arm_exidx_entry_s) *);
2260
2261 /* Fill in exception table. */
2262 for (i = 0; i < exidx_size / 8; i++)
2263 {
2264 struct arm_exidx_entry new_exidx_entry;
2265 bfd_vma idx = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8);
2266 bfd_vma val = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8 + 4);
2267 bfd_vma addr = 0, word = 0;
2268 int n_bytes = 0, n_words = 0;
2269 struct obj_section *sec;
2270 gdb_byte *entry = NULL;
2271
2272 /* Extract address of start of function. */
2273 idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2274 idx += exidx_vma + i * 8;
2275
2276 /* Find section containing function and compute section offset. */
2277 sec = arm_obj_section_from_vma (objfile, idx);
2278 if (sec == NULL)
2279 continue;
2280 idx -= bfd_get_section_vma (objfile->obfd, sec->the_bfd_section);
2281
2282 /* Determine address of exception table entry. */
2283 if (val == 1)
2284 {
2285 /* EXIDX_CANTUNWIND -- no exception table entry present. */
2286 }
2287 else if ((val & 0xff000000) == 0x80000000)
2288 {
2289 /* Exception table entry embedded in .ARM.exidx
2290 -- must be short form. */
2291 word = val;
2292 n_bytes = 3;
2293 }
2294 else if (!(val & 0x80000000))
2295 {
2296 /* Exception table entry in .ARM.extab. */
2297 addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2298 addr += exidx_vma + i * 8 + 4;
2299
2300 if (addr >= extab_vma && addr + 4 <= extab_vma + extab_size)
2301 {
2302 word = bfd_h_get_32 (objfile->obfd,
2303 extab_data + addr - extab_vma);
2304 addr += 4;
2305
2306 if ((word & 0xff000000) == 0x80000000)
2307 {
2308 /* Short form. */
2309 n_bytes = 3;
2310 }
2311 else if ((word & 0xff000000) == 0x81000000
2312 || (word & 0xff000000) == 0x82000000)
2313 {
2314 /* Long form. */
2315 n_bytes = 2;
2316 n_words = ((word >> 16) & 0xff);
2317 }
2318 else if (!(word & 0x80000000))
2319 {
2320 bfd_vma pers;
2321 struct obj_section *pers_sec;
2322 int gnu_personality = 0;
2323
2324 /* Custom personality routine. */
2325 pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2326 pers = UNMAKE_THUMB_ADDR (pers + addr - 4);
2327
2328 /* Check whether we've got one of the variants of the
2329 GNU personality routines. */
2330 pers_sec = arm_obj_section_from_vma (objfile, pers);
2331 if (pers_sec)
2332 {
2333 static const char *personality[] =
2334 {
2335 "__gcc_personality_v0",
2336 "__gxx_personality_v0",
2337 "__gcj_personality_v0",
2338 "__gnu_objc_personality_v0",
2339 NULL
2340 };
2341
2342 CORE_ADDR pc = pers + obj_section_offset (pers_sec);
2343 int k;
2344
2345 for (k = 0; personality[k]; k++)
2346 if (lookup_minimal_symbol_by_pc_name
2347 (pc, personality[k], objfile))
2348 {
2349 gnu_personality = 1;
2350 break;
2351 }
2352 }
2353
2354 /* If so, the next word contains a word count in the high
2355 byte, followed by the same unwind instructions as the
2356 pre-defined forms. */
2357 if (gnu_personality
2358 && addr + 4 <= extab_vma + extab_size)
2359 {
2360 word = bfd_h_get_32 (objfile->obfd,
2361 extab_data + addr - extab_vma);
2362 addr += 4;
2363 n_bytes = 3;
2364 n_words = ((word >> 24) & 0xff);
2365 }
2366 }
2367 }
2368 }
2369
2370 /* Sanity check address. */
2371 if (n_words)
2372 if (addr < extab_vma || addr + 4 * n_words > extab_vma + extab_size)
2373 n_words = n_bytes = 0;
2374
2375 /* The unwind instructions reside in WORD (only the N_BYTES least
2376 significant bytes are valid), followed by N_WORDS words in the
2377 extab section starting at ADDR. */
2378 if (n_bytes || n_words)
2379 {
2380 gdb_byte *p = entry = obstack_alloc (&objfile->objfile_obstack,
2381 n_bytes + n_words * 4 + 1);
2382
2383 while (n_bytes--)
2384 *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);
2385
2386 while (n_words--)
2387 {
2388 word = bfd_h_get_32 (objfile->obfd,
2389 extab_data + addr - extab_vma);
2390 addr += 4;
2391
2392 *p++ = (gdb_byte) ((word >> 24) & 0xff);
2393 *p++ = (gdb_byte) ((word >> 16) & 0xff);
2394 *p++ = (gdb_byte) ((word >> 8) & 0xff);
2395 *p++ = (gdb_byte) (word & 0xff);
2396 }
2397
2398 /* Implied "Finish" to terminate the list. */
2399 *p++ = 0xb0;
2400 }
2401
2402 /* Push entry onto vector. They are guaranteed to always
2403 appear in order of increasing addresses. */
2404 new_exidx_entry.addr = idx;
2405 new_exidx_entry.entry = entry;
2406 VEC_safe_push (arm_exidx_entry_s,
2407 data->section_maps[sec->the_bfd_section->index],
2408 &new_exidx_entry);
2409 }
2410
2411 do_cleanups (cleanups);
2412}
2413
2414/* Search for the exception table entry covering MEMADDR. If one is found,
2415 return a pointer to its data. Otherwise, return 0. If START is non-NULL,
2416 set *START to the start of the region covered by this entry. */
2417
2418static gdb_byte *
2419arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
2420{
2421 struct obj_section *sec;
2422
2423 sec = find_pc_section (memaddr);
2424 if (sec != NULL)
2425 {
2426 struct arm_exidx_data *data;
2427 VEC(arm_exidx_entry_s) *map;
2428 struct arm_exidx_entry map_key = { memaddr - obj_section_addr (sec), 0 };
2429 unsigned int idx;
2430
2431 data = objfile_data (sec->objfile, arm_exidx_data_key);
2432 if (data != NULL)
2433 {
2434 map = data->section_maps[sec->the_bfd_section->index];
2435 if (!VEC_empty (arm_exidx_entry_s, map))
2436 {
2437 struct arm_exidx_entry *map_sym;
2438
2439 idx = VEC_lower_bound (arm_exidx_entry_s, map, &map_key,
2440 arm_compare_exidx_entries);
2441
2442 /* VEC_lower_bound finds the earliest ordered insertion
2443 point. If the following symbol starts at this exact
2444 address, we use that; otherwise, the preceding
2445 exception table entry covers this address. */
2446 if (idx < VEC_length (arm_exidx_entry_s, map))
2447 {
2448 map_sym = VEC_index (arm_exidx_entry_s, map, idx);
2449 if (map_sym->addr == map_key.addr)
2450 {
2451 if (start)
2452 *start = map_sym->addr + obj_section_addr (sec);
2453 return map_sym->entry;
2454 }
2455 }
2456
2457 if (idx > 0)
2458 {
2459 map_sym = VEC_index (arm_exidx_entry_s, map, idx - 1);
2460 if (start)
2461 *start = map_sym->addr + obj_section_addr (sec);
2462 return map_sym->entry;
2463 }
2464 }
2465 }
2466 }
2467
2468 return NULL;
2469}
2470
2471/* Given the current frame THIS_FRAME, and its associated frame unwinding
2472 instruction list from the ARM exception table entry ENTRY, allocate and
2473 return a prologue cache structure describing how to unwind this frame.
2474
2475 Return NULL if the unwinding instruction list contains a "spare",
2476 "reserved" or "refuse to unwind" instruction as defined in section
2477 "9.3 Frame unwinding instructions" of the "Exception Handling ABI
2478 for the ARM Architecture" document. */
2479
2480static struct arm_prologue_cache *
2481arm_exidx_fill_cache (struct frame_info *this_frame, gdb_byte *entry)
2482{
2483 CORE_ADDR vsp = 0;
2484 int vsp_valid = 0;
2485
2486 struct arm_prologue_cache *cache;
2487 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2488 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2489
2490 for (;;)
2491 {
2492 gdb_byte insn;
2493
2494 /* Whenever we reload SP, we actually have to retrieve its
2495 actual value in the current frame. */
2496 if (!vsp_valid)
2497 {
2498 if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2499 {
2500 int reg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2501 vsp = get_frame_register_unsigned (this_frame, reg);
2502 }
2503 else
2504 {
2505 CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr;
2506 vsp = get_frame_memory_unsigned (this_frame, addr, 4);
2507 }
2508
2509 vsp_valid = 1;
2510 }
2511
2512 /* Decode next unwind instruction. */
2513 insn = *entry++;
2514
2515 if ((insn & 0xc0) == 0)
2516 {
2517 int offset = insn & 0x3f;
2518 vsp += (offset << 2) + 4;
2519 }
2520 else if ((insn & 0xc0) == 0x40)
2521 {
2522 int offset = insn & 0x3f;
2523 vsp -= (offset << 2) + 4;
2524 }
2525 else if ((insn & 0xf0) == 0x80)
2526 {
2527 int mask = ((insn & 0xf) << 8) | *entry++;
2528 int i;
2529
2530 /* The special case of an all-zero mask identifies
2531 "Refuse to unwind". We return NULL to fall back
2532 to the prologue analyzer. */
2533 if (mask == 0)
2534 return NULL;
2535
2536 /* Pop registers r4..r15 under mask. */
2537 for (i = 0; i < 12; i++)
2538 if (mask & (1 << i))
2539 {
2540 cache->saved_regs[4 + i].addr = vsp;
2541 vsp += 4;
2542 }
2543
2544 /* Special-case popping SP -- we need to reload vsp. */
2545 if (mask & (1 << (ARM_SP_REGNUM - 4)))
2546 vsp_valid = 0;
2547 }
2548 else if ((insn & 0xf0) == 0x90)
2549 {
2550 int reg = insn & 0xf;
2551
2552 /* Reserved cases. */
2553 if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
2554 return NULL;
2555
2556 /* Set SP from another register and mark VSP for reload. */
2557 cache->saved_regs[ARM_SP_REGNUM] = cache->saved_regs[reg];
2558 vsp_valid = 0;
2559 }
2560 else if ((insn & 0xf0) == 0xa0)
2561 {
2562 int count = insn & 0x7;
2563 int pop_lr = (insn & 0x8) != 0;
2564 int i;
2565
2566 /* Pop r4..r[4+count]. */
2567 for (i = 0; i <= count; i++)
2568 {
2569 cache->saved_regs[4 + i].addr = vsp;
2570 vsp += 4;
2571 }
2572
2573 /* If indicated by flag, pop LR as well. */
2574 if (pop_lr)
2575 {
2576 cache->saved_regs[ARM_LR_REGNUM].addr = vsp;
2577 vsp += 4;
2578 }
2579 }
2580 else if (insn == 0xb0)
2581 {
2582 /* We could only have updated PC by popping into it; if so, it
2583 will show up as address. Otherwise, copy LR into PC. */
2584 if (!trad_frame_addr_p (cache->saved_regs, ARM_PC_REGNUM))
2585 cache->saved_regs[ARM_PC_REGNUM]
2586 = cache->saved_regs[ARM_LR_REGNUM];
2587
2588 /* We're done. */
2589 break;
2590 }
2591 else if (insn == 0xb1)
2592 {
2593 int mask = *entry++;
2594 int i;
2595
2596 /* All-zero mask and mask >= 16 is "spare". */
2597 if (mask == 0 || mask >= 16)
2598 return NULL;
2599
2600 /* Pop r0..r3 under mask. */
2601 for (i = 0; i < 4; i++)
2602 if (mask & (1 << i))
2603 {
2604 cache->saved_regs[i].addr = vsp;
2605 vsp += 4;
2606 }
2607 }
2608 else if (insn == 0xb2)
2609 {
2610 ULONGEST offset = 0;
2611 unsigned shift = 0;
2612
2613 do
2614 {
2615 offset |= (*entry & 0x7f) << shift;
2616 shift += 7;
2617 }
2618 while (*entry++ & 0x80);
2619
2620 vsp += 0x204 + (offset << 2);
2621 }
2622 else if (insn == 0xb3)
2623 {
2624 int start = *entry >> 4;
2625 int count = (*entry++) & 0xf;
2626 int i;
2627
2628 /* Only registers D0..D15 are valid here. */
2629 if (start + count >= 16)
2630 return NULL;
2631
2632 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2633 for (i = 0; i <= count; i++)
2634 {
2635 cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2636 vsp += 8;
2637 }
2638
2639 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2640 vsp += 4;
2641 }
2642 else if ((insn & 0xf8) == 0xb8)
2643 {
2644 int count = insn & 0x7;
2645 int i;
2646
2647 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2648 for (i = 0; i <= count; i++)
2649 {
2650 cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2651 vsp += 8;
2652 }
2653
2654 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2655 vsp += 4;
2656 }
2657 else if (insn == 0xc6)
2658 {
2659 int start = *entry >> 4;
2660 int count = (*entry++) & 0xf;
2661 int i;
2662
2663 /* Only registers WR0..WR15 are valid. */
2664 if (start + count >= 16)
2665 return NULL;
2666
2667 /* Pop iwmmx registers WR[start]..WR[start+count]. */
2668 for (i = 0; i <= count; i++)
2669 {
2670 cache->saved_regs[ARM_WR0_REGNUM + start + i].addr = vsp;
2671 vsp += 8;
2672 }
2673 }
2674 else if (insn == 0xc7)
2675 {
2676 int mask = *entry++;
2677 int i;
2678
2679 /* All-zero mask and mask >= 16 is "spare". */
2680 if (mask == 0 || mask >= 16)
2681 return NULL;
2682
2683 /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask. */
2684 for (i = 0; i < 4; i++)
2685 if (mask & (1 << i))
2686 {
2687 cache->saved_regs[ARM_WCGR0_REGNUM + i].addr = vsp;
2688 vsp += 4;
2689 }
2690 }
2691 else if ((insn & 0xf8) == 0xc0)
2692 {
2693 int count = insn & 0x7;
2694 int i;
2695
2696 /* Pop iwmmx registers WR[10]..WR[10+count]. */
2697 for (i = 0; i <= count; i++)
2698 {
2699 cache->saved_regs[ARM_WR0_REGNUM + 10 + i].addr = vsp;
2700 vsp += 8;
2701 }
2702 }
2703 else if (insn == 0xc8)
2704 {
2705 int start = *entry >> 4;
2706 int count = (*entry++) & 0xf;
2707 int i;
2708
2709 /* Only registers D0..D31 are valid. */
2710 if (start + count >= 16)
2711 return NULL;
2712
2713 /* Pop VFP double-precision registers
2714 D[16+start]..D[16+start+count]. */
2715 for (i = 0; i <= count; i++)
2716 {
2717 cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].addr = vsp;
2718 vsp += 8;
2719 }
2720 }
2721 else if (insn == 0xc9)
2722 {
2723 int start = *entry >> 4;
2724 int count = (*entry++) & 0xf;
2725 int i;
2726
2727 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2728 for (i = 0; i <= count; i++)
2729 {
2730 cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2731 vsp += 8;
2732 }
2733 }
2734 else if ((insn & 0xf8) == 0xd0)
2735 {
2736 int count = insn & 0x7;
2737 int i;
2738
2739 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2740 for (i = 0; i <= count; i++)
2741 {
2742 cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2743 vsp += 8;
2744 }
2745 }
2746 else
2747 {
2748 /* Everything else is "spare". */
2749 return NULL;
2750 }
2751 }
2752
2753 /* If we restore SP from a register, assume this was the frame register.
2754 Otherwise just fall back to SP as frame register. */
2755 if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2756 cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2757 else
2758 cache->framereg = ARM_SP_REGNUM;
2759
2760 /* Determine offset to previous frame. */
2761 cache->framesize
2762 = vsp - get_frame_register_unsigned (this_frame, cache->framereg);
2763
2764 /* We already got the previous SP. */
2765 cache->prev_sp = vsp;
2766
2767 return cache;
2768}
2769
2770/* Unwinding via ARM exception table entries. Note that the sniffer
2771 already computes a filled-in prologue cache, which is then used
2772 with the same arm_prologue_this_id and arm_prologue_prev_register
2773 routines also used for prologue-parsing based unwinding. */
2774
2775static int
2776arm_exidx_unwind_sniffer (const struct frame_unwind *self,
2777 struct frame_info *this_frame,
2778 void **this_prologue_cache)
2779{
2780 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2781 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
2782 CORE_ADDR addr_in_block, exidx_region, func_start;
2783 struct arm_prologue_cache *cache;
2784 gdb_byte *entry;
2785
2786 /* See if we have an ARM exception table entry covering this address. */
2787 addr_in_block = get_frame_address_in_block (this_frame);
2788 entry = arm_find_exidx_entry (addr_in_block, &exidx_region);
2789 if (!entry)
2790 return 0;
2791
2792 /* The ARM exception table does not describe unwind information
2793 for arbitrary PC values, but is guaranteed to be correct only
2794 at call sites. We have to decide here whether we want to use
2795 ARM exception table information for this frame, or fall back
2796 to using prologue parsing. (Note that if we have DWARF CFI,
2797 this sniffer isn't even called -- CFI is always preferred.)
2798
2799 Before we make this decision, however, we check whether we
2800 actually have *symbol* information for the current frame.
2801 If not, prologue parsing would not work anyway, so we might
2802 as well use the exception table and hope for the best. */
2803 if (find_pc_partial_function (addr_in_block, NULL, &func_start, NULL))
2804 {
2805 int exc_valid = 0;
2806
2807 /* If the next frame is "normal", we are at a call site in this
2808 frame, so exception information is guaranteed to be valid. */
2809 if (get_next_frame (this_frame)
2810 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
2811 exc_valid = 1;
2812
2813 /* We also assume exception information is valid if we're currently
2814 blocked in a system call. The system library is supposed to
2815 ensure this, so that e.g. pthread cancellation works. */
2816 if (arm_frame_is_thumb (this_frame))
2817 {
2818 LONGEST insn;
2819
2820 if (safe_read_memory_integer (get_frame_pc (this_frame) - 2, 2,
2821 byte_order_for_code, &insn)
2822 && (insn & 0xff00) == 0xdf00 /* svc */)
2823 exc_valid = 1;
2824 }
2825 else
2826 {
2827 LONGEST insn;
2828
2829 if (safe_read_memory_integer (get_frame_pc (this_frame) - 4, 4,
2830 byte_order_for_code, &insn)
2831 && (insn & 0x0f000000) == 0x0f000000 /* svc */)
2832 exc_valid = 1;
2833 }
2834
2835 /* Bail out if we don't know that exception information is valid. */
2836 if (!exc_valid)
2837 return 0;
2838
2839 /* The ARM exception index does not mark the *end* of the region
2840 covered by the entry, and some functions will not have any entry.
2841 To correctly recognize the end of the covered region, the linker
2842 should have inserted dummy records with a CANTUNWIND marker.
2843
2844 Unfortunately, current versions of GNU ld do not reliably do
2845 this, and thus we may have found an incorrect entry above.
2846 As a (temporary) sanity check, we only use the entry if it
2847 lies *within* the bounds of the function. Note that this check
2848 might reject perfectly valid entries that just happen to cover
2849 multiple functions; therefore this check ought to be removed
2850 once the linker is fixed. */
2851 if (func_start > exidx_region)
2852 return 0;
2853 }
2854
2855 /* Decode the list of unwinding instructions into a prologue cache.
2856 Note that this may fail due to e.g. a "refuse to unwind" code. */
2857 cache = arm_exidx_fill_cache (this_frame, entry);
2858 if (!cache)
2859 return 0;
2860
2861 *this_prologue_cache = cache;
2862 return 1;
2863}
2864
2865struct frame_unwind arm_exidx_unwind = {
2866 NORMAL_FRAME,
8fbca658 2867 default_frame_unwind_stop_reason,
0e9e9abd
UW
2868 arm_prologue_this_id,
2869 arm_prologue_prev_register,
2870 NULL,
2871 arm_exidx_unwind_sniffer
2872};
2873
909cf6ea 2874static struct arm_prologue_cache *
a262aec2 2875arm_make_stub_cache (struct frame_info *this_frame)
909cf6ea 2876{
909cf6ea 2877 struct arm_prologue_cache *cache;
909cf6ea 2878
35d5d4ee 2879 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
a262aec2 2880 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
909cf6ea 2881
a262aec2 2882 cache->prev_sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
909cf6ea
DJ
2883
2884 return cache;
2885}
2886
2887/* Our frame ID for a stub frame is the current SP and LR. */
2888
2889static void
a262aec2 2890arm_stub_this_id (struct frame_info *this_frame,
909cf6ea
DJ
2891 void **this_cache,
2892 struct frame_id *this_id)
2893{
2894 struct arm_prologue_cache *cache;
2895
2896 if (*this_cache == NULL)
a262aec2 2897 *this_cache = arm_make_stub_cache (this_frame);
909cf6ea
DJ
2898 cache = *this_cache;
2899
a262aec2 2900 *this_id = frame_id_build (cache->prev_sp, get_frame_pc (this_frame));
909cf6ea
DJ
2901}
2902
a262aec2
DJ
2903static int
2904arm_stub_unwind_sniffer (const struct frame_unwind *self,
2905 struct frame_info *this_frame,
2906 void **this_prologue_cache)
909cf6ea 2907{
93d42b30 2908 CORE_ADDR addr_in_block;
909cf6ea
DJ
2909 char dummy[4];
2910
a262aec2 2911 addr_in_block = get_frame_address_in_block (this_frame);
93d42b30 2912 if (in_plt_section (addr_in_block, NULL)
fc36e839
DE
2913 /* We also use the stub winder if the target memory is unreadable
2914 to avoid having the prologue unwinder trying to read it. */
a262aec2
DJ
2915 || target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
2916 return 1;
909cf6ea 2917
a262aec2 2918 return 0;
909cf6ea
DJ
2919}
2920
a262aec2
DJ
2921struct frame_unwind arm_stub_unwind = {
2922 NORMAL_FRAME,
8fbca658 2923 default_frame_unwind_stop_reason,
a262aec2
DJ
2924 arm_stub_this_id,
2925 arm_prologue_prev_register,
2926 NULL,
2927 arm_stub_unwind_sniffer
2928};
2929
24de872b 2930static CORE_ADDR
a262aec2 2931arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
24de872b
DJ
2932{
2933 struct arm_prologue_cache *cache;
2934
eb5492fa 2935 if (*this_cache == NULL)
a262aec2 2936 *this_cache = arm_make_prologue_cache (this_frame);
eb5492fa
DJ
2937 cache = *this_cache;
2938
4be43953 2939 return cache->prev_sp - cache->framesize;
24de872b
DJ
2940}
2941
eb5492fa
DJ
2942struct frame_base arm_normal_base = {
2943 &arm_prologue_unwind,
2944 arm_normal_frame_base,
2945 arm_normal_frame_base,
2946 arm_normal_frame_base
2947};
2948
a262aec2 2949/* Assuming THIS_FRAME is a dummy, return the frame ID of that
eb5492fa
DJ
2950 dummy frame. The frame ID's base needs to match the TOS value
2951 saved by save_dummy_frame_tos() and returned from
2952 arm_push_dummy_call, and the PC needs to match the dummy frame's
2953 breakpoint. */
c906108c 2954
eb5492fa 2955static struct frame_id
a262aec2 2956arm_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
c906108c 2957{
0963b4bd
MS
2958 return frame_id_build (get_frame_register_unsigned (this_frame,
2959 ARM_SP_REGNUM),
a262aec2 2960 get_frame_pc (this_frame));
eb5492fa 2961}
c3b4394c 2962
eb5492fa
DJ
2963/* Given THIS_FRAME, find the previous frame's resume PC (which will
2964 be used to construct the previous frame's ID, after looking up the
2965 containing function). */
c3b4394c 2966
eb5492fa
DJ
2967static CORE_ADDR
2968arm_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
2969{
2970 CORE_ADDR pc;
2971 pc = frame_unwind_register_unsigned (this_frame, ARM_PC_REGNUM);
24568a2c 2972 return arm_addr_bits_remove (gdbarch, pc);
eb5492fa
DJ
2973}
2974
2975static CORE_ADDR
2976arm_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
2977{
2978 return frame_unwind_register_unsigned (this_frame, ARM_SP_REGNUM);
c906108c
SS
2979}
2980
b39cc962
DJ
2981static struct value *
2982arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
2983 int regnum)
2984{
24568a2c 2985 struct gdbarch * gdbarch = get_frame_arch (this_frame);
b39cc962 2986 CORE_ADDR lr, cpsr;
9779414d 2987 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
b39cc962
DJ
2988
2989 switch (regnum)
2990 {
2991 case ARM_PC_REGNUM:
2992 /* The PC is normally copied from the return column, which
2993 describes saves of LR. However, that version may have an
2994 extra bit set to indicate Thumb state. The bit is not
2995 part of the PC. */
2996 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2997 return frame_unwind_got_constant (this_frame, regnum,
24568a2c 2998 arm_addr_bits_remove (gdbarch, lr));
b39cc962
DJ
2999
3000 case ARM_PS_REGNUM:
3001 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
ca38c58e 3002 cpsr = get_frame_register_unsigned (this_frame, regnum);
b39cc962
DJ
3003 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3004 if (IS_THUMB_ADDR (lr))
9779414d 3005 cpsr |= t_bit;
b39cc962 3006 else
9779414d 3007 cpsr &= ~t_bit;
ca38c58e 3008 return frame_unwind_got_constant (this_frame, regnum, cpsr);
b39cc962
DJ
3009
3010 default:
3011 internal_error (__FILE__, __LINE__,
3012 _("Unexpected register %d"), regnum);
3013 }
3014}
3015
3016static void
3017arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
3018 struct dwarf2_frame_state_reg *reg,
3019 struct frame_info *this_frame)
3020{
3021 switch (regnum)
3022 {
3023 case ARM_PC_REGNUM:
3024 case ARM_PS_REGNUM:
3025 reg->how = DWARF2_FRAME_REG_FN;
3026 reg->loc.fn = arm_dwarf2_prev_register;
3027 break;
3028 case ARM_SP_REGNUM:
3029 reg->how = DWARF2_FRAME_REG_CFA;
3030 break;
3031 }
3032}
3033
4024ca99
UW
3034/* Return true if we are in the function's epilogue, i.e. after the
3035 instruction that destroyed the function's stack frame. */
3036
3037static int
3038thumb_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3039{
3040 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3041 unsigned int insn, insn2;
3042 int found_return = 0, found_stack_adjust = 0;
3043 CORE_ADDR func_start, func_end;
3044 CORE_ADDR scan_pc;
3045 gdb_byte buf[4];
3046
3047 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3048 return 0;
3049
3050 /* The epilogue is a sequence of instructions along the following lines:
3051
3052 - add stack frame size to SP or FP
3053 - [if frame pointer used] restore SP from FP
3054 - restore registers from SP [may include PC]
3055 - a return-type instruction [if PC wasn't already restored]
3056
3057 In a first pass, we scan forward from the current PC and verify the
3058 instructions we find as compatible with this sequence, ending in a
3059 return instruction.
3060
3061 However, this is not sufficient to distinguish indirect function calls
3062 within a function from indirect tail calls in the epilogue in some cases.
3063 Therefore, if we didn't already find any SP-changing instruction during
3064 forward scan, we add a backward scanning heuristic to ensure we actually
3065 are in the epilogue. */
3066
3067 scan_pc = pc;
3068 while (scan_pc < func_end && !found_return)
3069 {
3070 if (target_read_memory (scan_pc, buf, 2))
3071 break;
3072
3073 scan_pc += 2;
3074 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3075
3076 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
3077 found_return = 1;
3078 else if (insn == 0x46f7) /* mov pc, lr */
3079 found_return = 1;
3080 else if (insn == 0x46bd) /* mov sp, r7 */
3081 found_stack_adjust = 1;
3082 else if ((insn & 0xff00) == 0xb000) /* add sp, imm or sub sp, imm */
3083 found_stack_adjust = 1;
3084 else if ((insn & 0xfe00) == 0xbc00) /* pop <registers> */
3085 {
3086 found_stack_adjust = 1;
3087 if (insn & 0x0100) /* <registers> include PC. */
3088 found_return = 1;
3089 }
db24da6d 3090 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instruction */
4024ca99
UW
3091 {
3092 if (target_read_memory (scan_pc, buf, 2))
3093 break;
3094
3095 scan_pc += 2;
3096 insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3097
3098 if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3099 {
3100 found_stack_adjust = 1;
3101 if (insn2 & 0x8000) /* <registers> include PC. */
3102 found_return = 1;
3103 }
3104 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3105 && (insn2 & 0x0fff) == 0x0b04)
3106 {
3107 found_stack_adjust = 1;
3108 if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC. */
3109 found_return = 1;
3110 }
3111 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3112 && (insn2 & 0x0e00) == 0x0a00)
3113 found_stack_adjust = 1;
3114 else
3115 break;
3116 }
3117 else
3118 break;
3119 }
3120
3121 if (!found_return)
3122 return 0;
3123
3124 /* Since any instruction in the epilogue sequence, with the possible
3125 exception of return itself, updates the stack pointer, we need to
3126 scan backwards for at most one instruction. Try either a 16-bit or
3127 a 32-bit instruction. This is just a heuristic, so we do not worry
0963b4bd 3128 too much about false positives. */
4024ca99
UW
3129
3130 if (!found_stack_adjust)
3131 {
3132 if (pc - 4 < func_start)
3133 return 0;
3134 if (target_read_memory (pc - 4, buf, 4))
3135 return 0;
3136
3137 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3138 insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);
3139
3140 if (insn2 == 0x46bd) /* mov sp, r7 */
3141 found_stack_adjust = 1;
3142 else if ((insn2 & 0xff00) == 0xb000) /* add sp, imm or sub sp, imm */
3143 found_stack_adjust = 1;
3144 else if ((insn2 & 0xff00) == 0xbc00) /* pop <registers> without PC */
3145 found_stack_adjust = 1;
3146 else if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3147 found_stack_adjust = 1;
3148 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3149 && (insn2 & 0x0fff) == 0x0b04)
3150 found_stack_adjust = 1;
3151 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3152 && (insn2 & 0x0e00) == 0x0a00)
3153 found_stack_adjust = 1;
3154 }
3155
3156 return found_stack_adjust;
3157}
3158
3159/* Return true if we are in the function's epilogue, i.e. after the
3160 instruction that destroyed the function's stack frame. */
3161
3162static int
3163arm_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3164{
3165 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3166 unsigned int insn;
3167 int found_return, found_stack_adjust;
3168 CORE_ADDR func_start, func_end;
3169
3170 if (arm_pc_is_thumb (gdbarch, pc))
3171 return thumb_in_function_epilogue_p (gdbarch, pc);
3172
3173 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3174 return 0;
3175
3176 /* We are in the epilogue if the previous instruction was a stack
3177 adjustment and the next instruction is a possible return (bx, mov
3178 pc, or pop). We could have to scan backwards to find the stack
3179 adjustment, or forwards to find the return, but this is a decent
3180 approximation. First scan forwards. */
3181
3182 found_return = 0;
3183 insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
3184 if (bits (insn, 28, 31) != INST_NV)
3185 {
3186 if ((insn & 0x0ffffff0) == 0x012fff10)
3187 /* BX. */
3188 found_return = 1;
3189 else if ((insn & 0x0ffffff0) == 0x01a0f000)
3190 /* MOV PC. */
3191 found_return = 1;
3192 else if ((insn & 0x0fff0000) == 0x08bd0000
3193 && (insn & 0x0000c000) != 0)
3194 /* POP (LDMIA), including PC or LR. */
3195 found_return = 1;
3196 }
3197
3198 if (!found_return)
3199 return 0;
3200
3201 /* Scan backwards. This is just a heuristic, so do not worry about
3202 false positives from mode changes. */
3203
3204 if (pc < func_start + 4)
3205 return 0;
3206
73c964d6 3207 found_stack_adjust = 0;
4024ca99
UW
3208 insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
3209 if (bits (insn, 28, 31) != INST_NV)
3210 {
3211 if ((insn & 0x0df0f000) == 0x0080d000)
3212 /* ADD SP (register or immediate). */
3213 found_stack_adjust = 1;
3214 else if ((insn & 0x0df0f000) == 0x0040d000)
3215 /* SUB SP (register or immediate). */
3216 found_stack_adjust = 1;
3217 else if ((insn & 0x0ffffff0) == 0x01a0d000)
3218 /* MOV SP. */
77bc0675 3219 found_stack_adjust = 1;
4024ca99
UW
3220 else if ((insn & 0x0fff0000) == 0x08bd0000)
3221 /* POP (LDMIA). */
3222 found_stack_adjust = 1;
3223 }
3224
3225 if (found_stack_adjust)
3226 return 1;
3227
3228 return 0;
3229}
3230
3231
2dd604e7
RE
3232/* When arguments must be pushed onto the stack, they go on in reverse
3233 order. The code below implements a FILO (stack) to do this. */
3234
3235struct stack_item
3236{
3237 int len;
3238 struct stack_item *prev;
3239 void *data;
3240};
3241
3242static struct stack_item *
8c6363cf 3243push_stack_item (struct stack_item *prev, const void *contents, int len)
2dd604e7
RE
3244{
3245 struct stack_item *si;
3246 si = xmalloc (sizeof (struct stack_item));
226c7fbc 3247 si->data = xmalloc (len);
2dd604e7
RE
3248 si->len = len;
3249 si->prev = prev;
3250 memcpy (si->data, contents, len);
3251 return si;
3252}
3253
3254static struct stack_item *
3255pop_stack_item (struct stack_item *si)
3256{
3257 struct stack_item *dead = si;
3258 si = si->prev;
3259 xfree (dead->data);
3260 xfree (dead);
3261 return si;
3262}
3263
2af48f68
PB
3264
3265/* Return the alignment (in bytes) of the given type. */
3266
3267static int
3268arm_type_align (struct type *t)
3269{
3270 int n;
3271 int align;
3272 int falign;
3273
3274 t = check_typedef (t);
3275 switch (TYPE_CODE (t))
3276 {
3277 default:
3278 /* Should never happen. */
3279 internal_error (__FILE__, __LINE__, _("unknown type alignment"));
3280 return 4;
3281
3282 case TYPE_CODE_PTR:
3283 case TYPE_CODE_ENUM:
3284 case TYPE_CODE_INT:
3285 case TYPE_CODE_FLT:
3286 case TYPE_CODE_SET:
3287 case TYPE_CODE_RANGE:
3288 case TYPE_CODE_BITSTRING:
3289 case TYPE_CODE_REF:
3290 case TYPE_CODE_CHAR:
3291 case TYPE_CODE_BOOL:
3292 return TYPE_LENGTH (t);
3293
3294 case TYPE_CODE_ARRAY:
3295 case TYPE_CODE_COMPLEX:
3296 /* TODO: What about vector types? */
3297 return arm_type_align (TYPE_TARGET_TYPE (t));
3298
3299 case TYPE_CODE_STRUCT:
3300 case TYPE_CODE_UNION:
3301 align = 1;
3302 for (n = 0; n < TYPE_NFIELDS (t); n++)
3303 {
3304 falign = arm_type_align (TYPE_FIELD_TYPE (t, n));
3305 if (falign > align)
3306 align = falign;
3307 }
3308 return align;
3309 }
3310}
3311
90445bd3
DJ
3312/* Possible base types for a candidate for passing and returning in
3313 VFP registers. */
3314
3315enum arm_vfp_cprc_base_type
3316{
3317 VFP_CPRC_UNKNOWN,
3318 VFP_CPRC_SINGLE,
3319 VFP_CPRC_DOUBLE,
3320 VFP_CPRC_VEC64,
3321 VFP_CPRC_VEC128
3322};
3323
3324/* The length of one element of base type B. */
3325
3326static unsigned
3327arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
3328{
3329 switch (b)
3330 {
3331 case VFP_CPRC_SINGLE:
3332 return 4;
3333 case VFP_CPRC_DOUBLE:
3334 return 8;
3335 case VFP_CPRC_VEC64:
3336 return 8;
3337 case VFP_CPRC_VEC128:
3338 return 16;
3339 default:
3340 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3341 (int) b);
3342 }
3343}
3344
3345/* The character ('s', 'd' or 'q') for the type of VFP register used
3346 for passing base type B. */
3347
3348static int
3349arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
3350{
3351 switch (b)
3352 {
3353 case VFP_CPRC_SINGLE:
3354 return 's';
3355 case VFP_CPRC_DOUBLE:
3356 return 'd';
3357 case VFP_CPRC_VEC64:
3358 return 'd';
3359 case VFP_CPRC_VEC128:
3360 return 'q';
3361 default:
3362 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3363 (int) b);
3364 }
3365}
3366
3367/* Determine whether T may be part of a candidate for passing and
3368 returning in VFP registers, ignoring the limit on the total number
3369 of components. If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
3370 classification of the first valid component found; if it is not
3371 VFP_CPRC_UNKNOWN, all components must have the same classification
3372 as *BASE_TYPE. If it is found that T contains a type not permitted
3373 for passing and returning in VFP registers, a type differently
3374 classified from *BASE_TYPE, or two types differently classified
3375 from each other, return -1, otherwise return the total number of
3376 base-type elements found (possibly 0 in an empty structure or
3377 array). Vectors and complex types are not currently supported,
3378 matching the generic AAPCS support. */
3379
3380static int
3381arm_vfp_cprc_sub_candidate (struct type *t,
3382 enum arm_vfp_cprc_base_type *base_type)
3383{
3384 t = check_typedef (t);
3385 switch (TYPE_CODE (t))
3386 {
3387 case TYPE_CODE_FLT:
3388 switch (TYPE_LENGTH (t))
3389 {
3390 case 4:
3391 if (*base_type == VFP_CPRC_UNKNOWN)
3392 *base_type = VFP_CPRC_SINGLE;
3393 else if (*base_type != VFP_CPRC_SINGLE)
3394 return -1;
3395 return 1;
3396
3397 case 8:
3398 if (*base_type == VFP_CPRC_UNKNOWN)
3399 *base_type = VFP_CPRC_DOUBLE;
3400 else if (*base_type != VFP_CPRC_DOUBLE)
3401 return -1;
3402 return 1;
3403
3404 default:
3405 return -1;
3406 }
3407 break;
3408
3409 case TYPE_CODE_ARRAY:
3410 {
3411 int count;
3412 unsigned unitlen;
3413 count = arm_vfp_cprc_sub_candidate (TYPE_TARGET_TYPE (t), base_type);
3414 if (count == -1)
3415 return -1;
3416 if (TYPE_LENGTH (t) == 0)
3417 {
3418 gdb_assert (count == 0);
3419 return 0;
3420 }
3421 else if (count == 0)
3422 return -1;
3423 unitlen = arm_vfp_cprc_unit_length (*base_type);
3424 gdb_assert ((TYPE_LENGTH (t) % unitlen) == 0);
3425 return TYPE_LENGTH (t) / unitlen;
3426 }
3427 break;
3428
3429 case TYPE_CODE_STRUCT:
3430 {
3431 int count = 0;
3432 unsigned unitlen;
3433 int i;
3434 for (i = 0; i < TYPE_NFIELDS (t); i++)
3435 {
3436 int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3437 base_type);
3438 if (sub_count == -1)
3439 return -1;
3440 count += sub_count;
3441 }
3442 if (TYPE_LENGTH (t) == 0)
3443 {
3444 gdb_assert (count == 0);
3445 return 0;
3446 }
3447 else if (count == 0)
3448 return -1;
3449 unitlen = arm_vfp_cprc_unit_length (*base_type);
3450 if (TYPE_LENGTH (t) != unitlen * count)
3451 return -1;
3452 return count;
3453 }
3454
3455 case TYPE_CODE_UNION:
3456 {
3457 int count = 0;
3458 unsigned unitlen;
3459 int i;
3460 for (i = 0; i < TYPE_NFIELDS (t); i++)
3461 {
3462 int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3463 base_type);
3464 if (sub_count == -1)
3465 return -1;
3466 count = (count > sub_count ? count : sub_count);
3467 }
3468 if (TYPE_LENGTH (t) == 0)
3469 {
3470 gdb_assert (count == 0);
3471 return 0;
3472 }
3473 else if (count == 0)
3474 return -1;
3475 unitlen = arm_vfp_cprc_unit_length (*base_type);
3476 if (TYPE_LENGTH (t) != unitlen * count)
3477 return -1;
3478 return count;
3479 }
3480
3481 default:
3482 break;
3483 }
3484
3485 return -1;
3486}
3487
3488/* Determine whether T is a VFP co-processor register candidate (CPRC)
3489 if passed to or returned from a non-variadic function with the VFP
3490 ABI in effect. Return 1 if it is, 0 otherwise. If it is, set
3491 *BASE_TYPE to the base type for T and *COUNT to the number of
3492 elements of that base type before returning. */
3493
3494static int
3495arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
3496 int *count)
3497{
3498 enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
3499 int c = arm_vfp_cprc_sub_candidate (t, &b);
3500 if (c <= 0 || c > 4)
3501 return 0;
3502 *base_type = b;
3503 *count = c;
3504 return 1;
3505}
3506
3507/* Return 1 if the VFP ABI should be used for passing arguments to and
3508 returning values from a function of type FUNC_TYPE, 0
3509 otherwise. */
3510
3511static int
3512arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
3513{
3514 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3515 /* Variadic functions always use the base ABI. Assume that functions
3516 without debug info are not variadic. */
3517 if (func_type && TYPE_VARARGS (check_typedef (func_type)))
3518 return 0;
3519 /* The VFP ABI is only supported as a variant of AAPCS. */
3520 if (tdep->arm_abi != ARM_ABI_AAPCS)
3521 return 0;
3522 return gdbarch_tdep (gdbarch)->fp_model == ARM_FLOAT_VFP;
3523}
3524
3525/* We currently only support passing parameters in integer registers, which
3526 conforms with GCC's default model, and VFP argument passing following
3527 the VFP variant of AAPCS. Several other variants exist and
2dd604e7
RE
3528 we should probably support some of them based on the selected ABI. */
3529
3530static CORE_ADDR
7d9b040b 3531arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6a65450a
AC
3532 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
3533 struct value **args, CORE_ADDR sp, int struct_return,
3534 CORE_ADDR struct_addr)
2dd604e7 3535{
e17a4113 3536 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2dd604e7
RE
3537 int argnum;
3538 int argreg;
3539 int nstack;
3540 struct stack_item *si = NULL;
90445bd3
DJ
3541 int use_vfp_abi;
3542 struct type *ftype;
3543 unsigned vfp_regs_free = (1 << 16) - 1;
3544
3545 /* Determine the type of this function and whether the VFP ABI
3546 applies. */
3547 ftype = check_typedef (value_type (function));
3548 if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
3549 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
3550 use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
2dd604e7 3551
6a65450a
AC
3552 /* Set the return address. For the ARM, the return breakpoint is
3553 always at BP_ADDR. */
9779414d 3554 if (arm_pc_is_thumb (gdbarch, bp_addr))
9dca5578 3555 bp_addr |= 1;
6a65450a 3556 regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
2dd604e7
RE
3557
3558 /* Walk through the list of args and determine how large a temporary
3559 stack is required. Need to take care here as structs may be
7a9dd1b2 3560 passed on the stack, and we have to push them. */
2dd604e7
RE
3561 nstack = 0;
3562
3563 argreg = ARM_A1_REGNUM;
3564 nstack = 0;
3565
2dd604e7
RE
3566 /* The struct_return pointer occupies the first parameter
3567 passing register. */
3568 if (struct_return)
3569 {
3570 if (arm_debug)
5af949e3 3571 fprintf_unfiltered (gdb_stdlog, "struct return in %s = %s\n",
2af46ca0 3572 gdbarch_register_name (gdbarch, argreg),
5af949e3 3573 paddress (gdbarch, struct_addr));
2dd604e7
RE
3574 regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
3575 argreg++;
3576 }
3577
3578 for (argnum = 0; argnum < nargs; argnum++)
3579 {
3580 int len;
3581 struct type *arg_type;
3582 struct type *target_type;
3583 enum type_code typecode;
8c6363cf 3584 const bfd_byte *val;
2af48f68 3585 int align;
90445bd3
DJ
3586 enum arm_vfp_cprc_base_type vfp_base_type;
3587 int vfp_base_count;
3588 int may_use_core_reg = 1;
2dd604e7 3589
df407dfe 3590 arg_type = check_typedef (value_type (args[argnum]));
2dd604e7
RE
3591 len = TYPE_LENGTH (arg_type);
3592 target_type = TYPE_TARGET_TYPE (arg_type);
3593 typecode = TYPE_CODE (arg_type);
8c6363cf 3594 val = value_contents (args[argnum]);
2dd604e7 3595
2af48f68
PB
3596 align = arm_type_align (arg_type);
3597 /* Round alignment up to a whole number of words. */
3598 align = (align + INT_REGISTER_SIZE - 1) & ~(INT_REGISTER_SIZE - 1);
3599 /* Different ABIs have different maximum alignments. */
3600 if (gdbarch_tdep (gdbarch)->arm_abi == ARM_ABI_APCS)
3601 {
3602 /* The APCS ABI only requires word alignment. */
3603 align = INT_REGISTER_SIZE;
3604 }
3605 else
3606 {
3607 /* The AAPCS requires at most doubleword alignment. */
3608 if (align > INT_REGISTER_SIZE * 2)
3609 align = INT_REGISTER_SIZE * 2;
3610 }
3611
90445bd3
DJ
3612 if (use_vfp_abi
3613 && arm_vfp_call_candidate (arg_type, &vfp_base_type,
3614 &vfp_base_count))
3615 {
3616 int regno;
3617 int unit_length;
3618 int shift;
3619 unsigned mask;
3620
3621 /* Because this is a CPRC it cannot go in a core register or
3622 cause a core register to be skipped for alignment.
3623 Either it goes in VFP registers and the rest of this loop
3624 iteration is skipped for this argument, or it goes on the
3625 stack (and the stack alignment code is correct for this
3626 case). */
3627 may_use_core_reg = 0;
3628
3629 unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
3630 shift = unit_length / 4;
3631 mask = (1 << (shift * vfp_base_count)) - 1;
3632 for (regno = 0; regno < 16; regno += shift)
3633 if (((vfp_regs_free >> regno) & mask) == mask)
3634 break;
3635
3636 if (regno < 16)
3637 {
3638 int reg_char;
3639 int reg_scaled;
3640 int i;
3641
3642 vfp_regs_free &= ~(mask << regno);
3643 reg_scaled = regno / shift;
3644 reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
3645 for (i = 0; i < vfp_base_count; i++)
3646 {
3647 char name_buf[4];
3648 int regnum;
58d6951d
DJ
3649 if (reg_char == 'q')
3650 arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
90445bd3 3651 val + i * unit_length);
58d6951d
DJ
3652 else
3653 {
3654 sprintf (name_buf, "%c%d", reg_char, reg_scaled + i);
3655 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
3656 strlen (name_buf));
3657 regcache_cooked_write (regcache, regnum,
3658 val + i * unit_length);
3659 }
90445bd3
DJ
3660 }
3661 continue;
3662 }
3663 else
3664 {
3665 /* This CPRC could not go in VFP registers, so all VFP
3666 registers are now marked as used. */
3667 vfp_regs_free = 0;
3668 }
3669 }
3670
2af48f68
PB
3671 /* Push stack padding for dowubleword alignment. */
3672 if (nstack & (align - 1))
3673 {
3674 si = push_stack_item (si, val, INT_REGISTER_SIZE);
3675 nstack += INT_REGISTER_SIZE;
3676 }
3677
3678 /* Doubleword aligned quantities must go in even register pairs. */
90445bd3
DJ
3679 if (may_use_core_reg
3680 && argreg <= ARM_LAST_ARG_REGNUM
2af48f68
PB
3681 && align > INT_REGISTER_SIZE
3682 && argreg & 1)
3683 argreg++;
3684
2dd604e7
RE
3685 /* If the argument is a pointer to a function, and it is a
3686 Thumb function, create a LOCAL copy of the value and set
3687 the THUMB bit in it. */
3688 if (TYPE_CODE_PTR == typecode
3689 && target_type != NULL
f96b8fa0 3690 && TYPE_CODE_FUNC == TYPE_CODE (check_typedef (target_type)))
2dd604e7 3691 {
e17a4113 3692 CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
9779414d 3693 if (arm_pc_is_thumb (gdbarch, regval))
2dd604e7 3694 {
8c6363cf
TT
3695 bfd_byte *copy = alloca (len);
3696 store_unsigned_integer (copy, len, byte_order,
e17a4113 3697 MAKE_THUMB_ADDR (regval));
8c6363cf 3698 val = copy;
2dd604e7
RE
3699 }
3700 }
3701
3702 /* Copy the argument to general registers or the stack in
3703 register-sized pieces. Large arguments are split between
3704 registers and stack. */
3705 while (len > 0)
3706 {
f0c9063c 3707 int partial_len = len < INT_REGISTER_SIZE ? len : INT_REGISTER_SIZE;
2dd604e7 3708
90445bd3 3709 if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
2dd604e7
RE
3710 {
3711 /* The argument is being passed in a general purpose
3712 register. */
e17a4113
UW
3713 CORE_ADDR regval
3714 = extract_unsigned_integer (val, partial_len, byte_order);
3715 if (byte_order == BFD_ENDIAN_BIG)
8bf8793c 3716 regval <<= (INT_REGISTER_SIZE - partial_len) * 8;
2dd604e7
RE
3717 if (arm_debug)
3718 fprintf_unfiltered (gdb_stdlog, "arg %d in %s = 0x%s\n",
c9f4d572
UW
3719 argnum,
3720 gdbarch_register_name
2af46ca0 3721 (gdbarch, argreg),
f0c9063c 3722 phex (regval, INT_REGISTER_SIZE));
2dd604e7
RE
3723 regcache_cooked_write_unsigned (regcache, argreg, regval);
3724 argreg++;
3725 }
3726 else
3727 {
3728 /* Push the arguments onto the stack. */
3729 if (arm_debug)
3730 fprintf_unfiltered (gdb_stdlog, "arg %d @ sp + %d\n",
3731 argnum, nstack);
f0c9063c
UW
3732 si = push_stack_item (si, val, INT_REGISTER_SIZE);
3733 nstack += INT_REGISTER_SIZE;
2dd604e7
RE
3734 }
3735
3736 len -= partial_len;
3737 val += partial_len;
3738 }
3739 }
3740 /* If we have an odd number of words to push, then decrement the stack
3741 by one word now, so first stack argument will be dword aligned. */
3742 if (nstack & 4)
3743 sp -= 4;
3744
3745 while (si)
3746 {
3747 sp -= si->len;
3748 write_memory (sp, si->data, si->len);
3749 si = pop_stack_item (si);
3750 }
3751
3752 /* Finally, update teh SP register. */
3753 regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
3754
3755 return sp;
3756}
3757
f53f0d0b
PB
3758
3759/* Always align the frame to an 8-byte boundary. This is required on
3760 some platforms and harmless on the rest. */
3761
3762static CORE_ADDR
3763arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
3764{
3765 /* Align the stack to eight bytes. */
3766 return sp & ~ (CORE_ADDR) 7;
3767}
3768
c906108c 3769static void
ed9a39eb 3770print_fpu_flags (int flags)
c906108c 3771{
c5aa993b
JM
3772 if (flags & (1 << 0))
3773 fputs ("IVO ", stdout);
3774 if (flags & (1 << 1))
3775 fputs ("DVZ ", stdout);
3776 if (flags & (1 << 2))
3777 fputs ("OFL ", stdout);
3778 if (flags & (1 << 3))
3779 fputs ("UFL ", stdout);
3780 if (flags & (1 << 4))
3781 fputs ("INX ", stdout);
3782 putchar ('\n');
c906108c
SS
3783}
3784
5e74b15c
RE
3785/* Print interesting information about the floating point processor
3786 (if present) or emulator. */
34e8f22d 3787static void
d855c300 3788arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
23e3a7ac 3789 struct frame_info *frame, const char *args)
c906108c 3790{
9c9acae0 3791 unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
c5aa993b
JM
3792 int type;
3793
3794 type = (status >> 24) & 127;
edefbb7c
AC
3795 if (status & (1 << 31))
3796 printf (_("Hardware FPU type %d\n"), type);
3797 else
3798 printf (_("Software FPU type %d\n"), type);
3799 /* i18n: [floating point unit] mask */
3800 fputs (_("mask: "), stdout);
c5aa993b 3801 print_fpu_flags (status >> 16);
edefbb7c
AC
3802 /* i18n: [floating point unit] flags */
3803 fputs (_("flags: "), stdout);
c5aa993b 3804 print_fpu_flags (status);
c906108c
SS
3805}
3806
27067745
UW
3807/* Construct the ARM extended floating point type. */
3808static struct type *
3809arm_ext_type (struct gdbarch *gdbarch)
3810{
3811 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3812
3813 if (!tdep->arm_ext_type)
3814 tdep->arm_ext_type
e9bb382b 3815 = arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
27067745
UW
3816 floatformats_arm_ext);
3817
3818 return tdep->arm_ext_type;
3819}
3820
58d6951d
DJ
3821static struct type *
3822arm_neon_double_type (struct gdbarch *gdbarch)
3823{
3824 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3825
3826 if (tdep->neon_double_type == NULL)
3827 {
3828 struct type *t, *elem;
3829
3830 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
3831 TYPE_CODE_UNION);
3832 elem = builtin_type (gdbarch)->builtin_uint8;
3833 append_composite_type_field (t, "u8", init_vector_type (elem, 8));
3834 elem = builtin_type (gdbarch)->builtin_uint16;
3835 append_composite_type_field (t, "u16", init_vector_type (elem, 4));
3836 elem = builtin_type (gdbarch)->builtin_uint32;
3837 append_composite_type_field (t, "u32", init_vector_type (elem, 2));
3838 elem = builtin_type (gdbarch)->builtin_uint64;
3839 append_composite_type_field (t, "u64", elem);
3840 elem = builtin_type (gdbarch)->builtin_float;
3841 append_composite_type_field (t, "f32", init_vector_type (elem, 2));
3842 elem = builtin_type (gdbarch)->builtin_double;
3843 append_composite_type_field (t, "f64", elem);
3844
3845 TYPE_VECTOR (t) = 1;
3846 TYPE_NAME (t) = "neon_d";
3847 tdep->neon_double_type = t;
3848 }
3849
3850 return tdep->neon_double_type;
3851}
3852
3853/* FIXME: The vector types are not correctly ordered on big-endian
3854 targets. Just as s0 is the low bits of d0, d0[0] is also the low
3855 bits of d0 - regardless of what unit size is being held in d0. So
3856 the offset of the first uint8 in d0 is 7, but the offset of the
3857 first float is 4. This code works as-is for little-endian
3858 targets. */
3859
3860static struct type *
3861arm_neon_quad_type (struct gdbarch *gdbarch)
3862{
3863 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3864
3865 if (tdep->neon_quad_type == NULL)
3866 {
3867 struct type *t, *elem;
3868
3869 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
3870 TYPE_CODE_UNION);
3871 elem = builtin_type (gdbarch)->builtin_uint8;
3872 append_composite_type_field (t, "u8", init_vector_type (elem, 16));
3873 elem = builtin_type (gdbarch)->builtin_uint16;
3874 append_composite_type_field (t, "u16", init_vector_type (elem, 8));
3875 elem = builtin_type (gdbarch)->builtin_uint32;
3876 append_composite_type_field (t, "u32", init_vector_type (elem, 4));
3877 elem = builtin_type (gdbarch)->builtin_uint64;
3878 append_composite_type_field (t, "u64", init_vector_type (elem, 2));
3879 elem = builtin_type (gdbarch)->builtin_float;
3880 append_composite_type_field (t, "f32", init_vector_type (elem, 4));
3881 elem = builtin_type (gdbarch)->builtin_double;
3882 append_composite_type_field (t, "f64", init_vector_type (elem, 2));
3883
3884 TYPE_VECTOR (t) = 1;
3885 TYPE_NAME (t) = "neon_q";
3886 tdep->neon_quad_type = t;
3887 }
3888
3889 return tdep->neon_quad_type;
3890}
3891
34e8f22d
RE
3892/* Return the GDB type object for the "standard" data type of data in
3893 register N. */
3894
3895static struct type *
7a5ea0d4 3896arm_register_type (struct gdbarch *gdbarch, int regnum)
032758dc 3897{
58d6951d
DJ
3898 int num_regs = gdbarch_num_regs (gdbarch);
3899
3900 if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
3901 && regnum >= num_regs && regnum < num_regs + 32)
3902 return builtin_type (gdbarch)->builtin_float;
3903
3904 if (gdbarch_tdep (gdbarch)->have_neon_pseudos
3905 && regnum >= num_regs + 32 && regnum < num_regs + 32 + 16)
3906 return arm_neon_quad_type (gdbarch);
3907
3908 /* If the target description has register information, we are only
3909 in this function so that we can override the types of
3910 double-precision registers for NEON. */
3911 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
3912 {
3913 struct type *t = tdesc_register_type (gdbarch, regnum);
3914
3915 if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
3916 && TYPE_CODE (t) == TYPE_CODE_FLT
3917 && gdbarch_tdep (gdbarch)->have_neon)
3918 return arm_neon_double_type (gdbarch);
3919 else
3920 return t;
3921 }
3922
34e8f22d 3923 if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
58d6951d
DJ
3924 {
3925 if (!gdbarch_tdep (gdbarch)->have_fpa_registers)
3926 return builtin_type (gdbarch)->builtin_void;
3927
3928 return arm_ext_type (gdbarch);
3929 }
e4c16157 3930 else if (regnum == ARM_SP_REGNUM)
0dfff4cb 3931 return builtin_type (gdbarch)->builtin_data_ptr;
e4c16157 3932 else if (regnum == ARM_PC_REGNUM)
0dfff4cb 3933 return builtin_type (gdbarch)->builtin_func_ptr;
ff6f572f
DJ
3934 else if (regnum >= ARRAY_SIZE (arm_register_names))
3935 /* These registers are only supported on targets which supply
3936 an XML description. */
df4df182 3937 return builtin_type (gdbarch)->builtin_int0;
032758dc 3938 else
df4df182 3939 return builtin_type (gdbarch)->builtin_uint32;
032758dc
AC
3940}
3941
ff6f572f
DJ
3942/* Map a DWARF register REGNUM onto the appropriate GDB register
3943 number. */
3944
3945static int
d3f73121 3946arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
ff6f572f
DJ
3947{
3948 /* Core integer regs. */
3949 if (reg >= 0 && reg <= 15)
3950 return reg;
3951
3952 /* Legacy FPA encoding. These were once used in a way which
3953 overlapped with VFP register numbering, so their use is
3954 discouraged, but GDB doesn't support the ARM toolchain
3955 which used them for VFP. */
3956 if (reg >= 16 && reg <= 23)
3957 return ARM_F0_REGNUM + reg - 16;
3958
3959 /* New assignments for the FPA registers. */
3960 if (reg >= 96 && reg <= 103)
3961 return ARM_F0_REGNUM + reg - 96;
3962
3963 /* WMMX register assignments. */
3964 if (reg >= 104 && reg <= 111)
3965 return ARM_WCGR0_REGNUM + reg - 104;
3966
3967 if (reg >= 112 && reg <= 127)
3968 return ARM_WR0_REGNUM + reg - 112;
3969
3970 if (reg >= 192 && reg <= 199)
3971 return ARM_WC0_REGNUM + reg - 192;
3972
58d6951d
DJ
3973 /* VFP v2 registers. A double precision value is actually
3974 in d1 rather than s2, but the ABI only defines numbering
3975 for the single precision registers. This will "just work"
3976 in GDB for little endian targets (we'll read eight bytes,
3977 starting in s0 and then progressing to s1), but will be
3978 reversed on big endian targets with VFP. This won't
3979 be a problem for the new Neon quad registers; you're supposed
3980 to use DW_OP_piece for those. */
3981 if (reg >= 64 && reg <= 95)
3982 {
3983 char name_buf[4];
3984
3985 sprintf (name_buf, "s%d", reg - 64);
3986 return user_reg_map_name_to_regnum (gdbarch, name_buf,
3987 strlen (name_buf));
3988 }
3989
3990 /* VFP v3 / Neon registers. This range is also used for VFP v2
3991 registers, except that it now describes d0 instead of s0. */
3992 if (reg >= 256 && reg <= 287)
3993 {
3994 char name_buf[4];
3995
3996 sprintf (name_buf, "d%d", reg - 256);
3997 return user_reg_map_name_to_regnum (gdbarch, name_buf,
3998 strlen (name_buf));
3999 }
4000
ff6f572f
DJ
4001 return -1;
4002}
4003
26216b98
AC
4004/* Map GDB internal REGNUM onto the Arm simulator register numbers. */
4005static int
e7faf938 4006arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
26216b98
AC
4007{
4008 int reg = regnum;
e7faf938 4009 gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
26216b98 4010
ff6f572f
DJ
4011 if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
4012 return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
4013
4014 if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
4015 return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
4016
4017 if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
4018 return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
4019
26216b98
AC
4020 if (reg < NUM_GREGS)
4021 return SIM_ARM_R0_REGNUM + reg;
4022 reg -= NUM_GREGS;
4023
4024 if (reg < NUM_FREGS)
4025 return SIM_ARM_FP0_REGNUM + reg;
4026 reg -= NUM_FREGS;
4027
4028 if (reg < NUM_SREGS)
4029 return SIM_ARM_FPS_REGNUM + reg;
4030 reg -= NUM_SREGS;
4031
edefbb7c 4032 internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
26216b98 4033}
34e8f22d 4034
a37b3cc0
AC
4035/* NOTE: cagney/2001-08-20: Both convert_from_extended() and
4036 convert_to_extended() use floatformat_arm_ext_littlebyte_bigword.
4037 It is thought that this is is the floating-point register format on
4038 little-endian systems. */
c906108c 4039
ed9a39eb 4040static void
b508a996 4041convert_from_extended (const struct floatformat *fmt, const void *ptr,
be8626e0 4042 void *dbl, int endianess)
c906108c 4043{
a37b3cc0 4044 DOUBLEST d;
be8626e0
MD
4045
4046 if (endianess == BFD_ENDIAN_BIG)
a37b3cc0
AC
4047 floatformat_to_doublest (&floatformat_arm_ext_big, ptr, &d);
4048 else
4049 floatformat_to_doublest (&floatformat_arm_ext_littlebyte_bigword,
4050 ptr, &d);
b508a996 4051 floatformat_from_doublest (fmt, &d, dbl);
c906108c
SS
4052}
4053
34e8f22d 4054static void
be8626e0
MD
4055convert_to_extended (const struct floatformat *fmt, void *dbl, const void *ptr,
4056 int endianess)
c906108c 4057{
a37b3cc0 4058 DOUBLEST d;
be8626e0 4059
b508a996 4060 floatformat_to_doublest (fmt, ptr, &d);
be8626e0 4061 if (endianess == BFD_ENDIAN_BIG)
a37b3cc0
AC
4062 floatformat_from_doublest (&floatformat_arm_ext_big, &d, dbl);
4063 else
4064 floatformat_from_doublest (&floatformat_arm_ext_littlebyte_bigword,
4065 &d, dbl);
c906108c 4066}
ed9a39eb 4067
c906108c 4068static int
ed9a39eb 4069condition_true (unsigned long cond, unsigned long status_reg)
c906108c
SS
4070{
4071 if (cond == INST_AL || cond == INST_NV)
4072 return 1;
4073
4074 switch (cond)
4075 {
4076 case INST_EQ:
4077 return ((status_reg & FLAG_Z) != 0);
4078 case INST_NE:
4079 return ((status_reg & FLAG_Z) == 0);
4080 case INST_CS:
4081 return ((status_reg & FLAG_C) != 0);
4082 case INST_CC:
4083 return ((status_reg & FLAG_C) == 0);
4084 case INST_MI:
4085 return ((status_reg & FLAG_N) != 0);
4086 case INST_PL:
4087 return ((status_reg & FLAG_N) == 0);
4088 case INST_VS:
4089 return ((status_reg & FLAG_V) != 0);
4090 case INST_VC:
4091 return ((status_reg & FLAG_V) == 0);
4092 case INST_HI:
4093 return ((status_reg & (FLAG_C | FLAG_Z)) == FLAG_C);
4094 case INST_LS:
4095 return ((status_reg & (FLAG_C | FLAG_Z)) != FLAG_C);
4096 case INST_GE:
4097 return (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0));
4098 case INST_LT:
4099 return (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0));
4100 case INST_GT:
f8bf5763
PM
4101 return (((status_reg & FLAG_Z) == 0)
4102 && (((status_reg & FLAG_N) == 0)
4103 == ((status_reg & FLAG_V) == 0)));
c906108c 4104 case INST_LE:
f8bf5763
PM
4105 return (((status_reg & FLAG_Z) != 0)
4106 || (((status_reg & FLAG_N) == 0)
4107 != ((status_reg & FLAG_V) == 0)));
c906108c
SS
4108 }
4109 return 1;
4110}
4111
c906108c 4112static unsigned long
0b1b3e42
UW
4113shifted_reg_val (struct frame_info *frame, unsigned long inst, int carry,
4114 unsigned long pc_val, unsigned long status_reg)
c906108c
SS
4115{
4116 unsigned long res, shift;
4117 int rm = bits (inst, 0, 3);
4118 unsigned long shifttype = bits (inst, 5, 6);
c5aa993b
JM
4119
4120 if (bit (inst, 4))
c906108c
SS
4121 {
4122 int rs = bits (inst, 8, 11);
0b1b3e42
UW
4123 shift = (rs == 15 ? pc_val + 8
4124 : get_frame_register_unsigned (frame, rs)) & 0xFF;
c906108c
SS
4125 }
4126 else
4127 shift = bits (inst, 7, 11);
c5aa993b 4128
bf9f652a 4129 res = (rm == ARM_PC_REGNUM
0d39a070 4130 ? (pc_val + (bit (inst, 4) ? 12 : 8))
0b1b3e42 4131 : get_frame_register_unsigned (frame, rm));
c906108c
SS
4132
4133 switch (shifttype)
4134 {
c5aa993b 4135 case 0: /* LSL */
c906108c
SS
4136 res = shift >= 32 ? 0 : res << shift;
4137 break;
c5aa993b
JM
4138
4139 case 1: /* LSR */
c906108c
SS
4140 res = shift >= 32 ? 0 : res >> shift;
4141 break;
4142
c5aa993b
JM
4143 case 2: /* ASR */
4144 if (shift >= 32)
4145 shift = 31;
c906108c
SS
4146 res = ((res & 0x80000000L)
4147 ? ~((~res) >> shift) : res >> shift);
4148 break;
4149
c5aa993b 4150 case 3: /* ROR/RRX */
c906108c
SS
4151 shift &= 31;
4152 if (shift == 0)
4153 res = (res >> 1) | (carry ? 0x80000000L : 0);
4154 else
c5aa993b 4155 res = (res >> shift) | (res << (32 - shift));
c906108c
SS
4156 break;
4157 }
4158
4159 return res & 0xffffffff;
4160}
4161
c906108c
SS
4162/* Return number of 1-bits in VAL. */
4163
4164static int
ed9a39eb 4165bitcount (unsigned long val)
c906108c
SS
4166{
4167 int nbits;
4168 for (nbits = 0; val != 0; nbits++)
0963b4bd 4169 val &= val - 1; /* Delete rightmost 1-bit in val. */
c906108c
SS
4170 return nbits;
4171}
4172
177321bd
DJ
4173/* Return the size in bytes of the complete Thumb instruction whose
4174 first halfword is INST1. */
4175
4176static int
4177thumb_insn_size (unsigned short inst1)
4178{
4179 if ((inst1 & 0xe000) == 0xe000 && (inst1 & 0x1800) != 0)
4180 return 4;
4181 else
4182 return 2;
4183}
4184
4185static int
4186thumb_advance_itstate (unsigned int itstate)
4187{
4188 /* Preserve IT[7:5], the first three bits of the condition. Shift
4189 the upcoming condition flags left by one bit. */
4190 itstate = (itstate & 0xe0) | ((itstate << 1) & 0x1f);
4191
4192 /* If we have finished the IT block, clear the state. */
4193 if ((itstate & 0x0f) == 0)
4194 itstate = 0;
4195
4196 return itstate;
4197}
4198
4199/* Find the next PC after the current instruction executes. In some
4200 cases we can not statically determine the answer (see the IT state
4201 handling in this function); in that case, a breakpoint may be
4202 inserted in addition to the returned PC, which will be used to set
4203 another breakpoint by our caller. */
4204
ad527d2e 4205static CORE_ADDR
18819fa6 4206thumb_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc)
c906108c 4207{
2af46ca0 4208 struct gdbarch *gdbarch = get_frame_arch (frame);
177321bd 4209 struct address_space *aspace = get_frame_address_space (frame);
e17a4113
UW
4210 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4211 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
c5aa993b 4212 unsigned long pc_val = ((unsigned long) pc) + 4; /* PC after prefetch */
e17a4113 4213 unsigned short inst1;
0963b4bd 4214 CORE_ADDR nextpc = pc + 2; /* Default is next instruction. */
c906108c 4215 unsigned long offset;
177321bd 4216 ULONGEST status, itstate;
c906108c 4217
50e98be4
DJ
4218 nextpc = MAKE_THUMB_ADDR (nextpc);
4219 pc_val = MAKE_THUMB_ADDR (pc_val);
4220
e17a4113 4221 inst1 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
9d4fde75 4222
9dca5578
DJ
4223 /* Thumb-2 conditional execution support. There are eight bits in
4224 the CPSR which describe conditional execution state. Once
4225 reconstructed (they're in a funny order), the low five bits
4226 describe the low bit of the condition for each instruction and
4227 how many instructions remain. The high three bits describe the
4228 base condition. One of the low four bits will be set if an IT
4229 block is active. These bits read as zero on earlier
4230 processors. */
4231 status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
177321bd 4232 itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
9dca5578 4233
177321bd
DJ
4234 /* If-Then handling. On GNU/Linux, where this routine is used, we
4235 use an undefined instruction as a breakpoint. Unlike BKPT, IT
4236 can disable execution of the undefined instruction. So we might
4237 miss the breakpoint if we set it on a skipped conditional
4238 instruction. Because conditional instructions can change the
4239 flags, affecting the execution of further instructions, we may
4240 need to set two breakpoints. */
9dca5578 4241
177321bd
DJ
4242 if (gdbarch_tdep (gdbarch)->thumb2_breakpoint != NULL)
4243 {
4244 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
4245 {
4246 /* An IT instruction. Because this instruction does not
4247 modify the flags, we can accurately predict the next
4248 executed instruction. */
4249 itstate = inst1 & 0x00ff;
4250 pc += thumb_insn_size (inst1);
4251
4252 while (itstate != 0 && ! condition_true (itstate >> 4, status))
4253 {
0963b4bd
MS
4254 inst1 = read_memory_unsigned_integer (pc, 2,
4255 byte_order_for_code);
177321bd
DJ
4256 pc += thumb_insn_size (inst1);
4257 itstate = thumb_advance_itstate (itstate);
4258 }
4259
50e98be4 4260 return MAKE_THUMB_ADDR (pc);
177321bd
DJ
4261 }
4262 else if (itstate != 0)
4263 {
4264 /* We are in a conditional block. Check the condition. */
4265 if (! condition_true (itstate >> 4, status))
4266 {
4267 /* Advance to the next executed instruction. */
4268 pc += thumb_insn_size (inst1);
4269 itstate = thumb_advance_itstate (itstate);
4270
4271 while (itstate != 0 && ! condition_true (itstate >> 4, status))
4272 {
0963b4bd
MS
4273 inst1 = read_memory_unsigned_integer (pc, 2,
4274 byte_order_for_code);
177321bd
DJ
4275 pc += thumb_insn_size (inst1);
4276 itstate = thumb_advance_itstate (itstate);
4277 }
4278
50e98be4 4279 return MAKE_THUMB_ADDR (pc);
177321bd
DJ
4280 }
4281 else if ((itstate & 0x0f) == 0x08)
4282 {
4283 /* This is the last instruction of the conditional
4284 block, and it is executed. We can handle it normally
4285 because the following instruction is not conditional,
4286 and we must handle it normally because it is
4287 permitted to branch. Fall through. */
4288 }
4289 else
4290 {
4291 int cond_negated;
4292
4293 /* There are conditional instructions after this one.
4294 If this instruction modifies the flags, then we can
4295 not predict what the next executed instruction will
4296 be. Fortunately, this instruction is architecturally
4297 forbidden to branch; we know it will fall through.
4298 Start by skipping past it. */
4299 pc += thumb_insn_size (inst1);
4300 itstate = thumb_advance_itstate (itstate);
4301
4302 /* Set a breakpoint on the following instruction. */
4303 gdb_assert ((itstate & 0x0f) != 0);
18819fa6
UW
4304 arm_insert_single_step_breakpoint (gdbarch, aspace,
4305 MAKE_THUMB_ADDR (pc));
177321bd
DJ
4306 cond_negated = (itstate >> 4) & 1;
4307
4308 /* Skip all following instructions with the same
4309 condition. If there is a later instruction in the IT
4310 block with the opposite condition, set the other
4311 breakpoint there. If not, then set a breakpoint on
4312 the instruction after the IT block. */
4313 do
4314 {
0963b4bd
MS
4315 inst1 = read_memory_unsigned_integer (pc, 2,
4316 byte_order_for_code);
177321bd
DJ
4317 pc += thumb_insn_size (inst1);
4318 itstate = thumb_advance_itstate (itstate);
4319 }
4320 while (itstate != 0 && ((itstate >> 4) & 1) == cond_negated);
4321
50e98be4 4322 return MAKE_THUMB_ADDR (pc);
177321bd
DJ
4323 }
4324 }
4325 }
4326 else if (itstate & 0x0f)
9dca5578
DJ
4327 {
4328 /* We are in a conditional block. Check the condition. */
177321bd 4329 int cond = itstate >> 4;
9dca5578
DJ
4330
4331 if (! condition_true (cond, status))
db24da6d
YQ
4332 /* Advance to the next instruction. All the 32-bit
4333 instructions share a common prefix. */
4334 return MAKE_THUMB_ADDR (pc + thumb_insn_size (inst1));
177321bd
DJ
4335
4336 /* Otherwise, handle the instruction normally. */
9dca5578
DJ
4337 }
4338
c906108c
SS
4339 if ((inst1 & 0xff00) == 0xbd00) /* pop {rlist, pc} */
4340 {
4341 CORE_ADDR sp;
4342
4343 /* Fetch the saved PC from the stack. It's stored above
4344 all of the other registers. */
f0c9063c 4345 offset = bitcount (bits (inst1, 0, 7)) * INT_REGISTER_SIZE;
0b1b3e42 4346 sp = get_frame_register_unsigned (frame, ARM_SP_REGNUM);
e17a4113 4347 nextpc = read_memory_unsigned_integer (sp + offset, 4, byte_order);
c906108c
SS
4348 }
4349 else if ((inst1 & 0xf000) == 0xd000) /* conditional branch */
4350 {
c5aa993b 4351 unsigned long cond = bits (inst1, 8, 11);
25b41d01
YQ
4352 if (cond == 0x0f) /* 0x0f = SWI */
4353 {
4354 struct gdbarch_tdep *tdep;
4355 tdep = gdbarch_tdep (gdbarch);
4356
4357 if (tdep->syscall_next_pc != NULL)
4358 nextpc = tdep->syscall_next_pc (frame);
4359
4360 }
4361 else if (cond != 0x0f && condition_true (cond, status))
c906108c
SS
4362 nextpc = pc_val + (sbits (inst1, 0, 7) << 1);
4363 }
4364 else if ((inst1 & 0xf800) == 0xe000) /* unconditional branch */
4365 {
4366 nextpc = pc_val + (sbits (inst1, 0, 10) << 1);
4367 }
db24da6d 4368 else if (thumb_insn_size (inst1) == 4) /* 32-bit instruction */
c906108c 4369 {
e17a4113
UW
4370 unsigned short inst2;
4371 inst2 = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);
9dca5578
DJ
4372
4373 /* Default to the next instruction. */
4374 nextpc = pc + 4;
50e98be4 4375 nextpc = MAKE_THUMB_ADDR (nextpc);
9dca5578
DJ
4376
4377 if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
4378 {
4379 /* Branches and miscellaneous control instructions. */
4380
4381 if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
4382 {
4383 /* B, BL, BLX. */
4384 int j1, j2, imm1, imm2;
4385
4386 imm1 = sbits (inst1, 0, 10);
4387 imm2 = bits (inst2, 0, 10);
4388 j1 = bit (inst2, 13);
4389 j2 = bit (inst2, 11);
4390
4391 offset = ((imm1 << 12) + (imm2 << 1));
4392 offset ^= ((!j2) << 22) | ((!j1) << 23);
4393
4394 nextpc = pc_val + offset;
4395 /* For BLX make sure to clear the low bits. */
4396 if (bit (inst2, 12) == 0)
4397 nextpc = nextpc & 0xfffffffc;
4398 }
4399 else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
4400 {
4401 /* SUBS PC, LR, #imm8. */
4402 nextpc = get_frame_register_unsigned (frame, ARM_LR_REGNUM);
4403 nextpc -= inst2 & 0x00ff;
4404 }
4069ebbe 4405 else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
9dca5578
DJ
4406 {
4407 /* Conditional branch. */
4408 if (condition_true (bits (inst1, 6, 9), status))
4409 {
4410 int sign, j1, j2, imm1, imm2;
4411
4412 sign = sbits (inst1, 10, 10);
4413 imm1 = bits (inst1, 0, 5);
4414 imm2 = bits (inst2, 0, 10);
4415 j1 = bit (inst2, 13);
4416 j2 = bit (inst2, 11);
4417
4418 offset = (sign << 20) + (j2 << 19) + (j1 << 18);
4419 offset += (imm1 << 12) + (imm2 << 1);
4420
4421 nextpc = pc_val + offset;
4422 }
4423 }
4424 }
4425 else if ((inst1 & 0xfe50) == 0xe810)
4426 {
4427 /* Load multiple or RFE. */
4428 int rn, offset, load_pc = 1;
4429
4430 rn = bits (inst1, 0, 3);
4431 if (bit (inst1, 7) && !bit (inst1, 8))
4432 {
4433 /* LDMIA or POP */
4434 if (!bit (inst2, 15))
4435 load_pc = 0;
4436 offset = bitcount (inst2) * 4 - 4;
4437 }
4438 else if (!bit (inst1, 7) && bit (inst1, 8))
4439 {
4440 /* LDMDB */
4441 if (!bit (inst2, 15))
4442 load_pc = 0;
4443 offset = -4;
4444 }
4445 else if (bit (inst1, 7) && bit (inst1, 8))
4446 {
4447 /* RFEIA */
4448 offset = 0;
4449 }
4450 else if (!bit (inst1, 7) && !bit (inst1, 8))
4451 {
4452 /* RFEDB */
4453 offset = -8;
4454 }
4455 else
4456 load_pc = 0;
4457
4458 if (load_pc)
4459 {
4460 CORE_ADDR addr = get_frame_register_unsigned (frame, rn);
4461 nextpc = get_frame_memory_unsigned (frame, addr + offset, 4);
4462 }
4463 }
4464 else if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
4465 {
4466 /* MOV PC or MOVS PC. */
4467 nextpc = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
50e98be4 4468 nextpc = MAKE_THUMB_ADDR (nextpc);
9dca5578
DJ
4469 }
4470 else if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
4471 {
4472 /* LDR PC. */
4473 CORE_ADDR base;
4474 int rn, load_pc = 1;
4475
4476 rn = bits (inst1, 0, 3);
4477 base = get_frame_register_unsigned (frame, rn);
bf9f652a 4478 if (rn == ARM_PC_REGNUM)
9dca5578
DJ
4479 {
4480 base = (base + 4) & ~(CORE_ADDR) 0x3;
4481 if (bit (inst1, 7))
4482 base += bits (inst2, 0, 11);
4483 else
4484 base -= bits (inst2, 0, 11);
4485 }
4486 else if (bit (inst1, 7))
4487 base += bits (inst2, 0, 11);
4488 else if (bit (inst2, 11))
4489 {
4490 if (bit (inst2, 10))
4491 {
4492 if (bit (inst2, 9))
4493 base += bits (inst2, 0, 7);
4494 else
4495 base -= bits (inst2, 0, 7);
4496 }
4497 }
4498 else if ((inst2 & 0x0fc0) == 0x0000)
4499 {
4500 int shift = bits (inst2, 4, 5), rm = bits (inst2, 0, 3);
4501 base += get_frame_register_unsigned (frame, rm) << shift;
4502 }
4503 else
4504 /* Reserved. */
4505 load_pc = 0;
4506
4507 if (load_pc)
4508 nextpc = get_frame_memory_unsigned (frame, base, 4);
4509 }
4510 else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
4511 {
4512 /* TBB. */
d476da0e
RE
4513 CORE_ADDR tbl_reg, table, offset, length;
4514
4515 tbl_reg = bits (inst1, 0, 3);
4516 if (tbl_reg == 0x0f)
4517 table = pc + 4; /* Regcache copy of PC isn't right yet. */
4518 else
4519 table = get_frame_register_unsigned (frame, tbl_reg);
9dca5578 4520
9dca5578
DJ
4521 offset = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
4522 length = 2 * get_frame_memory_unsigned (frame, table + offset, 1);
4523 nextpc = pc_val + length;
4524 }
d476da0e 4525 else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
9dca5578
DJ
4526 {
4527 /* TBH. */
d476da0e
RE
4528 CORE_ADDR tbl_reg, table, offset, length;
4529
4530 tbl_reg = bits (inst1, 0, 3);
4531 if (tbl_reg == 0x0f)
4532 table = pc + 4; /* Regcache copy of PC isn't right yet. */
4533 else
4534 table = get_frame_register_unsigned (frame, tbl_reg);
9dca5578 4535
9dca5578
DJ
4536 offset = 2 * get_frame_register_unsigned (frame, bits (inst2, 0, 3));
4537 length = 2 * get_frame_memory_unsigned (frame, table + offset, 2);
4538 nextpc = pc_val + length;
4539 }
c906108c 4540 }
aa17d93e 4541 else if ((inst1 & 0xff00) == 0x4700) /* bx REG, blx REG */
9498281f
DJ
4542 {
4543 if (bits (inst1, 3, 6) == 0x0f)
4544 nextpc = pc_val;
4545 else
0b1b3e42 4546 nextpc = get_frame_register_unsigned (frame, bits (inst1, 3, 6));
9498281f 4547 }
ad8b5167
UW
4548 else if ((inst1 & 0xff87) == 0x4687) /* mov pc, REG */
4549 {
4550 if (bits (inst1, 3, 6) == 0x0f)
4551 nextpc = pc_val;
4552 else
4553 nextpc = get_frame_register_unsigned (frame, bits (inst1, 3, 6));
4554
4555 nextpc = MAKE_THUMB_ADDR (nextpc);
4556 }
9dca5578
DJ
4557 else if ((inst1 & 0xf500) == 0xb100)
4558 {
4559 /* CBNZ or CBZ. */
4560 int imm = (bit (inst1, 9) << 6) + (bits (inst1, 3, 7) << 1);
4561 ULONGEST reg = get_frame_register_unsigned (frame, bits (inst1, 0, 2));
4562
4563 if (bit (inst1, 11) && reg != 0)
4564 nextpc = pc_val + imm;
4565 else if (!bit (inst1, 11) && reg == 0)
4566 nextpc = pc_val + imm;
4567 }
c906108c
SS
4568 return nextpc;
4569}
4570
50e98be4 4571/* Get the raw next address. PC is the current program counter, in
18819fa6 4572 FRAME, which is assumed to be executing in ARM mode.
50e98be4
DJ
4573
4574 The value returned has the execution state of the next instruction
4575 encoded in it. Use IS_THUMB_ADDR () to see whether the instruction is
4576 in Thumb-State, and gdbarch_addr_bits_remove () to get the plain memory
0963b4bd
MS
4577 address. */
4578
50e98be4 4579static CORE_ADDR
18819fa6 4580arm_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc)
c906108c 4581{
2af46ca0 4582 struct gdbarch *gdbarch = get_frame_arch (frame);
e17a4113
UW
4583 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4584 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
c906108c
SS
4585 unsigned long pc_val;
4586 unsigned long this_instr;
4587 unsigned long status;
4588 CORE_ADDR nextpc;
4589
c906108c 4590 pc_val = (unsigned long) pc;
e17a4113 4591 this_instr = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
9d4fde75 4592
0b1b3e42 4593 status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
c5aa993b 4594 nextpc = (CORE_ADDR) (pc_val + 4); /* Default case */
c906108c 4595
daddc3c1
DJ
4596 if (bits (this_instr, 28, 31) == INST_NV)
4597 switch (bits (this_instr, 24, 27))
4598 {
4599 case 0xa:
4600 case 0xb:
4601 {
4602 /* Branch with Link and change to Thumb. */
4603 nextpc = BranchDest (pc, this_instr);
4604 nextpc |= bit (this_instr, 24) << 1;
50e98be4 4605 nextpc = MAKE_THUMB_ADDR (nextpc);
daddc3c1
DJ
4606 break;
4607 }
4608 case 0xc:
4609 case 0xd:
4610 case 0xe:
4611 /* Coprocessor register transfer. */
4612 if (bits (this_instr, 12, 15) == 15)
4613 error (_("Invalid update to pc in instruction"));
4614 break;
4615 }
4616 else if (condition_true (bits (this_instr, 28, 31), status))
c906108c
SS
4617 {
4618 switch (bits (this_instr, 24, 27))
4619 {
c5aa993b 4620 case 0x0:
94c30b78 4621 case 0x1: /* data processing */
c5aa993b
JM
4622 case 0x2:
4623 case 0x3:
c906108c
SS
4624 {
4625 unsigned long operand1, operand2, result = 0;
4626 unsigned long rn;
4627 int c;
c5aa993b 4628
c906108c
SS
4629 if (bits (this_instr, 12, 15) != 15)
4630 break;
4631
4632 if (bits (this_instr, 22, 25) == 0
c5aa993b 4633 && bits (this_instr, 4, 7) == 9) /* multiply */
edefbb7c 4634 error (_("Invalid update to pc in instruction"));
c906108c 4635
9498281f 4636 /* BX <reg>, BLX <reg> */
e150acc7
PB
4637 if (bits (this_instr, 4, 27) == 0x12fff1
4638 || bits (this_instr, 4, 27) == 0x12fff3)
9498281f
DJ
4639 {
4640 rn = bits (this_instr, 0, 3);
bf9f652a
YQ
4641 nextpc = ((rn == ARM_PC_REGNUM)
4642 ? (pc_val + 8)
4643 : get_frame_register_unsigned (frame, rn));
4644
9498281f
DJ
4645 return nextpc;
4646 }
4647
0963b4bd 4648 /* Multiply into PC. */
c906108c
SS
4649 c = (status & FLAG_C) ? 1 : 0;
4650 rn = bits (this_instr, 16, 19);
bf9f652a
YQ
4651 operand1 = ((rn == ARM_PC_REGNUM)
4652 ? (pc_val + 8)
4653 : get_frame_register_unsigned (frame, rn));
c5aa993b 4654
c906108c
SS
4655 if (bit (this_instr, 25))
4656 {
4657 unsigned long immval = bits (this_instr, 0, 7);
4658 unsigned long rotate = 2 * bits (this_instr, 8, 11);
c5aa993b
JM
4659 operand2 = ((immval >> rotate) | (immval << (32 - rotate)))
4660 & 0xffffffff;
c906108c 4661 }
0963b4bd
MS
4662 else /* operand 2 is a shifted register. */
4663 operand2 = shifted_reg_val (frame, this_instr, c,
4664 pc_val, status);
c5aa993b 4665
c906108c
SS
4666 switch (bits (this_instr, 21, 24))
4667 {
c5aa993b 4668 case 0x0: /*and */
c906108c
SS
4669 result = operand1 & operand2;
4670 break;
4671
c5aa993b 4672 case 0x1: /*eor */
c906108c
SS
4673 result = operand1 ^ operand2;
4674 break;
4675
c5aa993b 4676 case 0x2: /*sub */
c906108c
SS
4677 result = operand1 - operand2;
4678 break;
4679
c5aa993b 4680 case 0x3: /*rsb */
c906108c
SS
4681 result = operand2 - operand1;
4682 break;
4683
c5aa993b 4684 case 0x4: /*add */
c906108c
SS
4685 result = operand1 + operand2;
4686 break;
4687
c5aa993b 4688 case 0x5: /*adc */
c906108c
SS
4689 result = operand1 + operand2 + c;
4690 break;
4691
c5aa993b 4692 case 0x6: /*sbc */
c906108c
SS
4693 result = operand1 - operand2 + c;
4694 break;
4695
c5aa993b 4696 case 0x7: /*rsc */
c906108c
SS
4697 result = operand2 - operand1 + c;
4698 break;
4699
c5aa993b
JM
4700 case 0x8:
4701 case 0x9:
4702 case 0xa:
4703 case 0xb: /* tst, teq, cmp, cmn */
c906108c
SS
4704 result = (unsigned long) nextpc;
4705 break;
4706
c5aa993b 4707 case 0xc: /*orr */
c906108c
SS
4708 result = operand1 | operand2;
4709 break;
4710
c5aa993b 4711 case 0xd: /*mov */
c906108c
SS
4712 /* Always step into a function. */
4713 result = operand2;
c5aa993b 4714 break;
c906108c 4715
c5aa993b 4716 case 0xe: /*bic */
c906108c
SS
4717 result = operand1 & ~operand2;
4718 break;
4719
c5aa993b 4720 case 0xf: /*mvn */
c906108c
SS
4721 result = ~operand2;
4722 break;
4723 }
c906108c 4724
50e98be4
DJ
4725 /* In 26-bit APCS the bottom two bits of the result are
4726 ignored, and we always end up in ARM state. */
4727 if (!arm_apcs_32)
4728 nextpc = arm_addr_bits_remove (gdbarch, result);
4729 else
4730 nextpc = result;
4731
c906108c
SS
4732 break;
4733 }
c5aa993b
JM
4734
4735 case 0x4:
4736 case 0x5: /* data transfer */
4737 case 0x6:
4738 case 0x7:
c906108c
SS
4739 if (bit (this_instr, 20))
4740 {
4741 /* load */
4742 if (bits (this_instr, 12, 15) == 15)
4743 {
4744 /* rd == pc */
c5aa993b 4745 unsigned long rn;
c906108c 4746 unsigned long base;
c5aa993b 4747
c906108c 4748 if (bit (this_instr, 22))
edefbb7c 4749 error (_("Invalid update to pc in instruction"));
c906108c
SS
4750
4751 /* byte write to PC */
4752 rn = bits (this_instr, 16, 19);
bf9f652a
YQ
4753 base = ((rn == ARM_PC_REGNUM)
4754 ? (pc_val + 8)
4755 : get_frame_register_unsigned (frame, rn));
4756
c906108c
SS
4757 if (bit (this_instr, 24))
4758 {
4759 /* pre-indexed */
4760 int c = (status & FLAG_C) ? 1 : 0;
4761 unsigned long offset =
c5aa993b 4762 (bit (this_instr, 25)
0b1b3e42 4763 ? shifted_reg_val (frame, this_instr, c, pc_val, status)
c5aa993b 4764 : bits (this_instr, 0, 11));
c906108c
SS
4765
4766 if (bit (this_instr, 23))
4767 base += offset;
4768 else
4769 base -= offset;
4770 }
51370a33
YQ
4771 nextpc =
4772 (CORE_ADDR) read_memory_unsigned_integer ((CORE_ADDR) base,
4773 4, byte_order);
c906108c
SS
4774 }
4775 }
4776 break;
c5aa993b
JM
4777
4778 case 0x8:
4779 case 0x9: /* block transfer */
c906108c
SS
4780 if (bit (this_instr, 20))
4781 {
4782 /* LDM */
4783 if (bit (this_instr, 15))
4784 {
4785 /* loading pc */
4786 int offset = 0;
51370a33
YQ
4787 unsigned long rn_val
4788 = get_frame_register_unsigned (frame,
4789 bits (this_instr, 16, 19));
c906108c
SS
4790
4791 if (bit (this_instr, 23))
4792 {
4793 /* up */
4794 unsigned long reglist = bits (this_instr, 0, 14);
4795 offset = bitcount (reglist) * 4;
c5aa993b 4796 if (bit (this_instr, 24)) /* pre */
c906108c
SS
4797 offset += 4;
4798 }
4799 else if (bit (this_instr, 24))
4800 offset = -4;
c5aa993b 4801
51370a33
YQ
4802 nextpc =
4803 (CORE_ADDR) read_memory_unsigned_integer ((CORE_ADDR)
4804 (rn_val + offset),
4805 4, byte_order);
c906108c
SS
4806 }
4807 }
4808 break;
c5aa993b
JM
4809
4810 case 0xb: /* branch & link */
4811 case 0xa: /* branch */
c906108c
SS
4812 {
4813 nextpc = BranchDest (pc, this_instr);
c906108c
SS
4814 break;
4815 }
c5aa993b
JM
4816
4817 case 0xc:
4818 case 0xd:
4819 case 0xe: /* coproc ops */
25b41d01 4820 break;
c5aa993b 4821 case 0xf: /* SWI */
25b41d01
YQ
4822 {
4823 struct gdbarch_tdep *tdep;
4824 tdep = gdbarch_tdep (gdbarch);
4825
4826 if (tdep->syscall_next_pc != NULL)
4827 nextpc = tdep->syscall_next_pc (frame);
4828
4829 }
c906108c
SS
4830 break;
4831
4832 default:
edefbb7c 4833 fprintf_filtered (gdb_stderr, _("Bad bit-field extraction\n"));
c906108c
SS
4834 return (pc);
4835 }
4836 }
4837
4838 return nextpc;
4839}
4840
18819fa6
UW
4841/* Determine next PC after current instruction executes. Will call either
4842 arm_get_next_pc_raw or thumb_get_next_pc_raw. Error out if infinite
4843 loop is detected. */
4844
50e98be4
DJ
4845CORE_ADDR
4846arm_get_next_pc (struct frame_info *frame, CORE_ADDR pc)
4847{
18819fa6
UW
4848 CORE_ADDR nextpc;
4849
4850 if (arm_frame_is_thumb (frame))
4851 {
4852 nextpc = thumb_get_next_pc_raw (frame, pc);
4853 if (nextpc == MAKE_THUMB_ADDR (pc))
4854 error (_("Infinite loop detected"));
4855 }
4856 else
4857 {
4858 nextpc = arm_get_next_pc_raw (frame, pc);
4859 if (nextpc == pc)
4860 error (_("Infinite loop detected"));
4861 }
4862
50e98be4
DJ
4863 return nextpc;
4864}
4865
18819fa6
UW
4866/* Like insert_single_step_breakpoint, but make sure we use a breakpoint
4867 of the appropriate mode (as encoded in the PC value), even if this
4868 differs from what would be expected according to the symbol tables. */
4869
4870void
4871arm_insert_single_step_breakpoint (struct gdbarch *gdbarch,
4872 struct address_space *aspace,
4873 CORE_ADDR pc)
4874{
4875 struct cleanup *old_chain
4876 = make_cleanup_restore_integer (&arm_override_mode);
4877
4878 arm_override_mode = IS_THUMB_ADDR (pc);
4879 pc = gdbarch_addr_bits_remove (gdbarch, pc);
4880
4881 insert_single_step_breakpoint (gdbarch, aspace, pc);
4882
4883 do_cleanups (old_chain);
4884}
4885
35f73cfc
UW
4886/* Checks for an atomic sequence of instructions beginning with a LDREX{,B,H,D}
4887 instruction and ending with a STREX{,B,H,D} instruction. If such a sequence
4888 is found, attempt to step through it. A breakpoint is placed at the end of
4889 the sequence. */
4890
4891static int
4892thumb_deal_with_atomic_sequence_raw (struct frame_info *frame)
4893{
4894 struct gdbarch *gdbarch = get_frame_arch (frame);
4895 struct address_space *aspace = get_frame_address_space (frame);
4896 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4897 CORE_ADDR pc = get_frame_pc (frame);
4898 CORE_ADDR breaks[2] = {-1, -1};
4899 CORE_ADDR loc = pc;
4900 unsigned short insn1, insn2;
4901 int insn_count;
4902 int index;
4903 int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed). */
4904 const int atomic_sequence_length = 16; /* Instruction sequence length. */
4905 ULONGEST status, itstate;
4906
4907 /* We currently do not support atomic sequences within an IT block. */
4908 status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
4909 itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
4910 if (itstate & 0x0f)
4911 return 0;
4912
4913 /* Assume all atomic sequences start with a ldrex{,b,h,d} instruction. */
4914 insn1 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
4915 loc += 2;
4916 if (thumb_insn_size (insn1) != 4)
4917 return 0;
4918
4919 insn2 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
4920 loc += 2;
4921 if (!((insn1 & 0xfff0) == 0xe850
4922 || ((insn1 & 0xfff0) == 0xe8d0 && (insn2 & 0x00c0) == 0x0040)))
4923 return 0;
4924
4925 /* Assume that no atomic sequence is longer than "atomic_sequence_length"
4926 instructions. */
4927 for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
4928 {
4929 insn1 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
4930 loc += 2;
4931
4932 if (thumb_insn_size (insn1) != 4)
4933 {
4934 /* Assume that there is at most one conditional branch in the
4935 atomic sequence. If a conditional branch is found, put a
4936 breakpoint in its destination address. */
4937 if ((insn1 & 0xf000) == 0xd000 && bits (insn1, 8, 11) != 0x0f)
4938 {
4939 if (last_breakpoint > 0)
4940 return 0; /* More than one conditional branch found,
4941 fallback to the standard code. */
4942
4943 breaks[1] = loc + 2 + (sbits (insn1, 0, 7) << 1);
4944 last_breakpoint++;
4945 }
4946
4947 /* We do not support atomic sequences that use any *other*
4948 instructions but conditional branches to change the PC.
4949 Fall back to standard code to avoid losing control of
4950 execution. */
4951 else if (thumb_instruction_changes_pc (insn1))
4952 return 0;
4953 }
4954 else
4955 {
4956 insn2 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
4957 loc += 2;
4958
4959 /* Assume that there is at most one conditional branch in the
4960 atomic sequence. If a conditional branch is found, put a
4961 breakpoint in its destination address. */
4962 if ((insn1 & 0xf800) == 0xf000
4963 && (insn2 & 0xd000) == 0x8000
4964 && (insn1 & 0x0380) != 0x0380)
4965 {
4966 int sign, j1, j2, imm1, imm2;
4967 unsigned int offset;
4968
4969 sign = sbits (insn1, 10, 10);
4970 imm1 = bits (insn1, 0, 5);
4971 imm2 = bits (insn2, 0, 10);
4972 j1 = bit (insn2, 13);
4973 j2 = bit (insn2, 11);
4974
4975 offset = (sign << 20) + (j2 << 19) + (j1 << 18);
4976 offset += (imm1 << 12) + (imm2 << 1);
4977
4978 if (last_breakpoint > 0)
4979 return 0; /* More than one conditional branch found,
4980 fallback to the standard code. */
4981
4982 breaks[1] = loc + offset;
4983 last_breakpoint++;
4984 }
4985
4986 /* We do not support atomic sequences that use any *other*
4987 instructions but conditional branches to change the PC.
4988 Fall back to standard code to avoid losing control of
4989 execution. */
4990 else if (thumb2_instruction_changes_pc (insn1, insn2))
4991 return 0;
4992
4993 /* If we find a strex{,b,h,d}, we're done. */
4994 if ((insn1 & 0xfff0) == 0xe840
4995 || ((insn1 & 0xfff0) == 0xe8c0 && (insn2 & 0x00c0) == 0x0040))
4996 break;
4997 }
4998 }
4999
5000 /* If we didn't find the strex{,b,h,d}, we cannot handle the sequence. */
5001 if (insn_count == atomic_sequence_length)
5002 return 0;
5003
5004 /* Insert a breakpoint right after the end of the atomic sequence. */
5005 breaks[0] = loc;
5006
5007 /* Check for duplicated breakpoints. Check also for a breakpoint
5008 placed (branch instruction's destination) anywhere in sequence. */
5009 if (last_breakpoint
5010 && (breaks[1] == breaks[0]
5011 || (breaks[1] >= pc && breaks[1] < loc)))
5012 last_breakpoint = 0;
5013
5014 /* Effectively inserts the breakpoints. */
5015 for (index = 0; index <= last_breakpoint; index++)
5016 arm_insert_single_step_breakpoint (gdbarch, aspace,
5017 MAKE_THUMB_ADDR (breaks[index]));
5018
5019 return 1;
5020}
5021
5022static int
5023arm_deal_with_atomic_sequence_raw (struct frame_info *frame)
5024{
5025 struct gdbarch *gdbarch = get_frame_arch (frame);
5026 struct address_space *aspace = get_frame_address_space (frame);
5027 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
5028 CORE_ADDR pc = get_frame_pc (frame);
5029 CORE_ADDR breaks[2] = {-1, -1};
5030 CORE_ADDR loc = pc;
5031 unsigned int insn;
5032 int insn_count;
5033 int index;
5034 int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed). */
5035 const int atomic_sequence_length = 16; /* Instruction sequence length. */
5036
5037 /* Assume all atomic sequences start with a ldrex{,b,h,d} instruction.
5038 Note that we do not currently support conditionally executed atomic
5039 instructions. */
5040 insn = read_memory_unsigned_integer (loc, 4, byte_order_for_code);
5041 loc += 4;
5042 if ((insn & 0xff9000f0) != 0xe1900090)
5043 return 0;
5044
5045 /* Assume that no atomic sequence is longer than "atomic_sequence_length"
5046 instructions. */
5047 for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
5048 {
5049 insn = read_memory_unsigned_integer (loc, 4, byte_order_for_code);
5050 loc += 4;
5051
5052 /* Assume that there is at most one conditional branch in the atomic
5053 sequence. If a conditional branch is found, put a breakpoint in
5054 its destination address. */
5055 if (bits (insn, 24, 27) == 0xa)
5056 {
5057 if (last_breakpoint > 0)
5058 return 0; /* More than one conditional branch found, fallback
5059 to the standard single-step code. */
5060
5061 breaks[1] = BranchDest (loc - 4, insn);
5062 last_breakpoint++;
5063 }
5064
5065 /* We do not support atomic sequences that use any *other* instructions
5066 but conditional branches to change the PC. Fall back to standard
5067 code to avoid losing control of execution. */
5068 else if (arm_instruction_changes_pc (insn))
5069 return 0;
5070
5071 /* If we find a strex{,b,h,d}, we're done. */
5072 if ((insn & 0xff9000f0) == 0xe1800090)
5073 break;
5074 }
5075
5076 /* If we didn't find the strex{,b,h,d}, we cannot handle the sequence. */
5077 if (insn_count == atomic_sequence_length)
5078 return 0;
5079
5080 /* Insert a breakpoint right after the end of the atomic sequence. */
5081 breaks[0] = loc;
5082
5083 /* Check for duplicated breakpoints. Check also for a breakpoint
5084 placed (branch instruction's destination) anywhere in sequence. */
5085 if (last_breakpoint
5086 && (breaks[1] == breaks[0]
5087 || (breaks[1] >= pc && breaks[1] < loc)))
5088 last_breakpoint = 0;
5089
5090 /* Effectively inserts the breakpoints. */
5091 for (index = 0; index <= last_breakpoint; index++)
5092 arm_insert_single_step_breakpoint (gdbarch, aspace, breaks[index]);
5093
5094 return 1;
5095}
5096
5097int
5098arm_deal_with_atomic_sequence (struct frame_info *frame)
5099{
5100 if (arm_frame_is_thumb (frame))
5101 return thumb_deal_with_atomic_sequence_raw (frame);
5102 else
5103 return arm_deal_with_atomic_sequence_raw (frame);
5104}
5105
9512d7fd
FN
5106/* single_step() is called just before we want to resume the inferior,
5107 if we want to single-step it but there is no hardware or kernel
5108 single-step support. We find the target of the coming instruction
e0cd558a 5109 and breakpoint it. */
9512d7fd 5110
190dce09 5111int
0b1b3e42 5112arm_software_single_step (struct frame_info *frame)
9512d7fd 5113{
a6d9a66e 5114 struct gdbarch *gdbarch = get_frame_arch (frame);
6c95b8df 5115 struct address_space *aspace = get_frame_address_space (frame);
35f73cfc
UW
5116 CORE_ADDR next_pc;
5117
5118 if (arm_deal_with_atomic_sequence (frame))
5119 return 1;
18819fa6 5120
35f73cfc 5121 next_pc = arm_get_next_pc (frame, get_frame_pc (frame));
18819fa6 5122 arm_insert_single_step_breakpoint (gdbarch, aspace, next_pc);
e6590a1b
UW
5123
5124 return 1;
9512d7fd 5125}
9512d7fd 5126
f9d67f43
DJ
5127/* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
5128 the buffer to be NEW_LEN bytes ending at ENDADDR. Return
5129 NULL if an error occurs. BUF is freed. */
5130
5131static gdb_byte *
5132extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
5133 int old_len, int new_len)
5134{
5135 gdb_byte *new_buf, *middle;
5136 int bytes_to_read = new_len - old_len;
5137
5138 new_buf = xmalloc (new_len);
5139 memcpy (new_buf + bytes_to_read, buf, old_len);
5140 xfree (buf);
5141 if (target_read_memory (endaddr - new_len, new_buf, bytes_to_read) != 0)
5142 {
5143 xfree (new_buf);
5144 return NULL;
5145 }
5146 return new_buf;
5147}
5148
5149/* An IT block is at most the 2-byte IT instruction followed by
5150 four 4-byte instructions. The furthest back we must search to
5151 find an IT block that affects the current instruction is thus
5152 2 + 3 * 4 == 14 bytes. */
5153#define MAX_IT_BLOCK_PREFIX 14
5154
5155/* Use a quick scan if there are more than this many bytes of
5156 code. */
5157#define IT_SCAN_THRESHOLD 32
5158
5159/* Adjust a breakpoint's address to move breakpoints out of IT blocks.
5160 A breakpoint in an IT block may not be hit, depending on the
5161 condition flags. */
5162static CORE_ADDR
5163arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
5164{
5165 gdb_byte *buf;
5166 char map_type;
5167 CORE_ADDR boundary, func_start;
5168 int buf_len, buf2_len;
5169 enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
5170 int i, any, last_it, last_it_count;
5171
5172 /* If we are using BKPT breakpoints, none of this is necessary. */
5173 if (gdbarch_tdep (gdbarch)->thumb2_breakpoint == NULL)
5174 return bpaddr;
5175
5176 /* ARM mode does not have this problem. */
9779414d 5177 if (!arm_pc_is_thumb (gdbarch, bpaddr))
f9d67f43
DJ
5178 return bpaddr;
5179
5180 /* We are setting a breakpoint in Thumb code that could potentially
5181 contain an IT block. The first step is to find how much Thumb
5182 code there is; we do not need to read outside of known Thumb
5183 sequences. */
5184 map_type = arm_find_mapping_symbol (bpaddr, &boundary);
5185 if (map_type == 0)
5186 /* Thumb-2 code must have mapping symbols to have a chance. */
5187 return bpaddr;
5188
5189 bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
5190
5191 if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
5192 && func_start > boundary)
5193 boundary = func_start;
5194
5195 /* Search for a candidate IT instruction. We have to do some fancy
5196 footwork to distinguish a real IT instruction from the second
5197 half of a 32-bit instruction, but there is no need for that if
5198 there's no candidate. */
5199 buf_len = min (bpaddr - boundary, MAX_IT_BLOCK_PREFIX);
5200 if (buf_len == 0)
5201 /* No room for an IT instruction. */
5202 return bpaddr;
5203
5204 buf = xmalloc (buf_len);
5205 if (target_read_memory (bpaddr - buf_len, buf, buf_len) != 0)
5206 return bpaddr;
5207 any = 0;
5208 for (i = 0; i < buf_len; i += 2)
5209 {
5210 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5211 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5212 {
5213 any = 1;
5214 break;
5215 }
5216 }
5217 if (any == 0)
5218 {
5219 xfree (buf);
5220 return bpaddr;
5221 }
5222
5223 /* OK, the code bytes before this instruction contain at least one
5224 halfword which resembles an IT instruction. We know that it's
5225 Thumb code, but there are still two possibilities. Either the
5226 halfword really is an IT instruction, or it is the second half of
5227 a 32-bit Thumb instruction. The only way we can tell is to
5228 scan forwards from a known instruction boundary. */
5229 if (bpaddr - boundary > IT_SCAN_THRESHOLD)
5230 {
5231 int definite;
5232
5233 /* There's a lot of code before this instruction. Start with an
5234 optimistic search; it's easy to recognize halfwords that can
5235 not be the start of a 32-bit instruction, and use that to
5236 lock on to the instruction boundaries. */
5237 buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
5238 if (buf == NULL)
5239 return bpaddr;
5240 buf_len = IT_SCAN_THRESHOLD;
5241
5242 definite = 0;
5243 for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
5244 {
5245 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5246 if (thumb_insn_size (inst1) == 2)
5247 {
5248 definite = 1;
5249 break;
5250 }
5251 }
5252
5253 /* At this point, if DEFINITE, BUF[I] is the first place we
5254 are sure that we know the instruction boundaries, and it is far
5255 enough from BPADDR that we could not miss an IT instruction
5256 affecting BPADDR. If ! DEFINITE, give up - start from a
5257 known boundary. */
5258 if (! definite)
5259 {
0963b4bd
MS
5260 buf = extend_buffer_earlier (buf, bpaddr, buf_len,
5261 bpaddr - boundary);
f9d67f43
DJ
5262 if (buf == NULL)
5263 return bpaddr;
5264 buf_len = bpaddr - boundary;
5265 i = 0;
5266 }
5267 }
5268 else
5269 {
5270 buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
5271 if (buf == NULL)
5272 return bpaddr;
5273 buf_len = bpaddr - boundary;
5274 i = 0;
5275 }
5276
5277 /* Scan forwards. Find the last IT instruction before BPADDR. */
5278 last_it = -1;
5279 last_it_count = 0;
5280 while (i < buf_len)
5281 {
5282 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5283 last_it_count--;
5284 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5285 {
5286 last_it = i;
5287 if (inst1 & 0x0001)
5288 last_it_count = 4;
5289 else if (inst1 & 0x0002)
5290 last_it_count = 3;
5291 else if (inst1 & 0x0004)
5292 last_it_count = 2;
5293 else
5294 last_it_count = 1;
5295 }
5296 i += thumb_insn_size (inst1);
5297 }
5298
5299 xfree (buf);
5300
5301 if (last_it == -1)
5302 /* There wasn't really an IT instruction after all. */
5303 return bpaddr;
5304
5305 if (last_it_count < 1)
5306 /* It was too far away. */
5307 return bpaddr;
5308
5309 /* This really is a trouble spot. Move the breakpoint to the IT
5310 instruction. */
5311 return bpaddr - buf_len + last_it;
5312}
5313
cca44b1b 5314/* ARM displaced stepping support.
c906108c 5315
cca44b1b 5316 Generally ARM displaced stepping works as follows:
c906108c 5317
cca44b1b
JB
5318 1. When an instruction is to be single-stepped, it is first decoded by
5319 arm_process_displaced_insn (called from arm_displaced_step_copy_insn).
5320 Depending on the type of instruction, it is then copied to a scratch
5321 location, possibly in a modified form. The copy_* set of functions
0963b4bd 5322 performs such modification, as necessary. A breakpoint is placed after
cca44b1b
JB
5323 the modified instruction in the scratch space to return control to GDB.
5324 Note in particular that instructions which modify the PC will no longer
5325 do so after modification.
c5aa993b 5326
cca44b1b
JB
5327 2. The instruction is single-stepped, by setting the PC to the scratch
5328 location address, and resuming. Control returns to GDB when the
5329 breakpoint is hit.
c5aa993b 5330
cca44b1b
JB
5331 3. A cleanup function (cleanup_*) is called corresponding to the copy_*
5332 function used for the current instruction. This function's job is to
5333 put the CPU/memory state back to what it would have been if the
5334 instruction had been executed unmodified in its original location. */
c5aa993b 5335
cca44b1b
JB
5336/* NOP instruction (mov r0, r0). */
5337#define ARM_NOP 0xe1a00000
34518530 5338#define THUMB_NOP 0x4600
cca44b1b
JB
5339
5340/* Helper for register reads for displaced stepping. In particular, this
5341 returns the PC as it would be seen by the instruction at its original
5342 location. */
5343
5344ULONGEST
36073a92
YQ
5345displaced_read_reg (struct regcache *regs, struct displaced_step_closure *dsc,
5346 int regno)
cca44b1b
JB
5347{
5348 ULONGEST ret;
36073a92 5349 CORE_ADDR from = dsc->insn_addr;
cca44b1b 5350
bf9f652a 5351 if (regno == ARM_PC_REGNUM)
cca44b1b 5352 {
4db71c0b
YQ
5353 /* Compute pipeline offset:
5354 - When executing an ARM instruction, PC reads as the address of the
5355 current instruction plus 8.
5356 - When executing a Thumb instruction, PC reads as the address of the
5357 current instruction plus 4. */
5358
36073a92 5359 if (!dsc->is_thumb)
4db71c0b
YQ
5360 from += 8;
5361 else
5362 from += 4;
5363
cca44b1b
JB
5364 if (debug_displaced)
5365 fprintf_unfiltered (gdb_stdlog, "displaced: read pc value %.8lx\n",
4db71c0b
YQ
5366 (unsigned long) from);
5367 return (ULONGEST) from;
cca44b1b 5368 }
c906108c 5369 else
cca44b1b
JB
5370 {
5371 regcache_cooked_read_unsigned (regs, regno, &ret);
5372 if (debug_displaced)
5373 fprintf_unfiltered (gdb_stdlog, "displaced: read r%d value %.8lx\n",
5374 regno, (unsigned long) ret);
5375 return ret;
5376 }
c906108c
SS
5377}
5378
cca44b1b
JB
5379static int
5380displaced_in_arm_mode (struct regcache *regs)
5381{
5382 ULONGEST ps;
9779414d 5383 ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
66e810cd 5384
cca44b1b 5385 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
66e810cd 5386
9779414d 5387 return (ps & t_bit) == 0;
cca44b1b 5388}
66e810cd 5389
cca44b1b 5390/* Write to the PC as from a branch instruction. */
c906108c 5391
cca44b1b 5392static void
36073a92
YQ
5393branch_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5394 ULONGEST val)
c906108c 5395{
36073a92 5396 if (!dsc->is_thumb)
cca44b1b
JB
5397 /* Note: If bits 0/1 are set, this branch would be unpredictable for
5398 architecture versions < 6. */
0963b4bd
MS
5399 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5400 val & ~(ULONGEST) 0x3);
cca44b1b 5401 else
0963b4bd
MS
5402 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5403 val & ~(ULONGEST) 0x1);
cca44b1b 5404}
66e810cd 5405
cca44b1b
JB
5406/* Write to the PC as from a branch-exchange instruction. */
5407
5408static void
5409bx_write_pc (struct regcache *regs, ULONGEST val)
5410{
5411 ULONGEST ps;
9779414d 5412 ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
cca44b1b
JB
5413
5414 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
5415
5416 if ((val & 1) == 1)
c906108c 5417 {
9779414d 5418 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
cca44b1b
JB
5419 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
5420 }
5421 else if ((val & 2) == 0)
5422 {
9779414d 5423 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 5424 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
c906108c
SS
5425 }
5426 else
5427 {
cca44b1b
JB
5428 /* Unpredictable behaviour. Try to do something sensible (switch to ARM
5429 mode, align dest to 4 bytes). */
5430 warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
9779414d 5431 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 5432 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
c906108c
SS
5433 }
5434}
ed9a39eb 5435
cca44b1b 5436/* Write to the PC as if from a load instruction. */
ed9a39eb 5437
34e8f22d 5438static void
36073a92
YQ
5439load_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5440 ULONGEST val)
ed9a39eb 5441{
cca44b1b
JB
5442 if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
5443 bx_write_pc (regs, val);
5444 else
36073a92 5445 branch_write_pc (regs, dsc, val);
cca44b1b 5446}
be8626e0 5447
cca44b1b
JB
5448/* Write to the PC as if from an ALU instruction. */
5449
5450static void
36073a92
YQ
5451alu_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5452 ULONGEST val)
cca44b1b 5453{
36073a92 5454 if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
cca44b1b
JB
5455 bx_write_pc (regs, val);
5456 else
36073a92 5457 branch_write_pc (regs, dsc, val);
cca44b1b
JB
5458}
5459
5460/* Helper for writing to registers for displaced stepping. Writing to the PC
5461 has a varying effects depending on the instruction which does the write:
5462 this is controlled by the WRITE_PC argument. */
5463
5464void
5465displaced_write_reg (struct regcache *regs, struct displaced_step_closure *dsc,
5466 int regno, ULONGEST val, enum pc_write_style write_pc)
5467{
bf9f652a 5468 if (regno == ARM_PC_REGNUM)
08216dd7 5469 {
cca44b1b
JB
5470 if (debug_displaced)
5471 fprintf_unfiltered (gdb_stdlog, "displaced: writing pc %.8lx\n",
5472 (unsigned long) val);
5473 switch (write_pc)
08216dd7 5474 {
cca44b1b 5475 case BRANCH_WRITE_PC:
36073a92 5476 branch_write_pc (regs, dsc, val);
08216dd7
RE
5477 break;
5478
cca44b1b
JB
5479 case BX_WRITE_PC:
5480 bx_write_pc (regs, val);
5481 break;
5482
5483 case LOAD_WRITE_PC:
36073a92 5484 load_write_pc (regs, dsc, val);
cca44b1b
JB
5485 break;
5486
5487 case ALU_WRITE_PC:
36073a92 5488 alu_write_pc (regs, dsc, val);
cca44b1b
JB
5489 break;
5490
5491 case CANNOT_WRITE_PC:
5492 warning (_("Instruction wrote to PC in an unexpected way when "
5493 "single-stepping"));
08216dd7
RE
5494 break;
5495
5496 default:
97b9747c
JB
5497 internal_error (__FILE__, __LINE__,
5498 _("Invalid argument to displaced_write_reg"));
08216dd7 5499 }
b508a996 5500
cca44b1b 5501 dsc->wrote_to_pc = 1;
b508a996 5502 }
ed9a39eb 5503 else
b508a996 5504 {
cca44b1b
JB
5505 if (debug_displaced)
5506 fprintf_unfiltered (gdb_stdlog, "displaced: writing r%d value %.8lx\n",
5507 regno, (unsigned long) val);
5508 regcache_cooked_write_unsigned (regs, regno, val);
b508a996 5509 }
34e8f22d
RE
5510}
5511
cca44b1b
JB
5512/* This function is used to concisely determine if an instruction INSN
5513 references PC. Register fields of interest in INSN should have the
0963b4bd
MS
5514 corresponding fields of BITMASK set to 0b1111. The function
5515 returns return 1 if any of these fields in INSN reference the PC
5516 (also 0b1111, r15), else it returns 0. */
67255d04
RE
5517
5518static int
cca44b1b 5519insn_references_pc (uint32_t insn, uint32_t bitmask)
67255d04 5520{
cca44b1b 5521 uint32_t lowbit = 1;
67255d04 5522
cca44b1b
JB
5523 while (bitmask != 0)
5524 {
5525 uint32_t mask;
44e1a9eb 5526
cca44b1b
JB
5527 for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
5528 ;
67255d04 5529
cca44b1b
JB
5530 if (!lowbit)
5531 break;
67255d04 5532
cca44b1b 5533 mask = lowbit * 0xf;
67255d04 5534
cca44b1b
JB
5535 if ((insn & mask) == mask)
5536 return 1;
5537
5538 bitmask &= ~mask;
67255d04
RE
5539 }
5540
cca44b1b
JB
5541 return 0;
5542}
2af48f68 5543
cca44b1b
JB
5544/* The simplest copy function. Many instructions have the same effect no
5545 matter what address they are executed at: in those cases, use this. */
67255d04 5546
cca44b1b 5547static int
7ff120b4
YQ
5548arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn,
5549 const char *iname, struct displaced_step_closure *dsc)
cca44b1b
JB
5550{
5551 if (debug_displaced)
5552 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx, "
5553 "opcode/class '%s' unmodified\n", (unsigned long) insn,
5554 iname);
67255d04 5555
cca44b1b 5556 dsc->modinsn[0] = insn;
67255d04 5557
cca44b1b
JB
5558 return 0;
5559}
5560
34518530
YQ
5561static int
5562thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
5563 uint16_t insn2, const char *iname,
5564 struct displaced_step_closure *dsc)
5565{
5566 if (debug_displaced)
5567 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x %.4x, "
5568 "opcode/class '%s' unmodified\n", insn1, insn2,
5569 iname);
5570
5571 dsc->modinsn[0] = insn1;
5572 dsc->modinsn[1] = insn2;
5573 dsc->numinsns = 2;
5574
5575 return 0;
5576}
5577
5578/* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
5579 modification. */
5580static int
5581thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, unsigned int insn,
5582 const char *iname,
5583 struct displaced_step_closure *dsc)
5584{
5585 if (debug_displaced)
5586 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x, "
5587 "opcode/class '%s' unmodified\n", insn,
5588 iname);
5589
5590 dsc->modinsn[0] = insn;
5591
5592 return 0;
5593}
5594
cca44b1b
JB
5595/* Preload instructions with immediate offset. */
5596
5597static void
6e39997a 5598cleanup_preload (struct gdbarch *gdbarch,
cca44b1b
JB
5599 struct regcache *regs, struct displaced_step_closure *dsc)
5600{
5601 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5602 if (!dsc->u.preload.immed)
5603 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5604}
5605
7ff120b4
YQ
5606static void
5607install_preload (struct gdbarch *gdbarch, struct regcache *regs,
5608 struct displaced_step_closure *dsc, unsigned int rn)
cca44b1b 5609{
cca44b1b 5610 ULONGEST rn_val;
cca44b1b
JB
5611 /* Preload instructions:
5612
5613 {pli/pld} [rn, #+/-imm]
5614 ->
5615 {pli/pld} [r0, #+/-imm]. */
5616
36073a92
YQ
5617 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5618 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 5619 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
cca44b1b
JB
5620 dsc->u.preload.immed = 1;
5621
cca44b1b 5622 dsc->cleanup = &cleanup_preload;
cca44b1b
JB
5623}
5624
cca44b1b 5625static int
7ff120b4 5626arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
cca44b1b
JB
5627 struct displaced_step_closure *dsc)
5628{
5629 unsigned int rn = bits (insn, 16, 19);
cca44b1b 5630
7ff120b4
YQ
5631 if (!insn_references_pc (insn, 0x000f0000ul))
5632 return arm_copy_unmodified (gdbarch, insn, "preload", dsc);
cca44b1b
JB
5633
5634 if (debug_displaced)
5635 fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
5636 (unsigned long) insn);
5637
7ff120b4
YQ
5638 dsc->modinsn[0] = insn & 0xfff0ffff;
5639
5640 install_preload (gdbarch, regs, dsc, rn);
5641
5642 return 0;
5643}
5644
34518530
YQ
5645static int
5646thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
5647 struct regcache *regs, struct displaced_step_closure *dsc)
5648{
5649 unsigned int rn = bits (insn1, 0, 3);
5650 unsigned int u_bit = bit (insn1, 7);
5651 int imm12 = bits (insn2, 0, 11);
5652 ULONGEST pc_val;
5653
5654 if (rn != ARM_PC_REGNUM)
5655 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);
5656
5657 /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
5658 PLD (literal) Encoding T1. */
5659 if (debug_displaced)
5660 fprintf_unfiltered (gdb_stdlog,
5661 "displaced: copying pld/pli pc (0x%x) %c imm12 %.4x\n",
5662 (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
5663 imm12);
5664
5665 if (!u_bit)
5666 imm12 = -1 * imm12;
5667
5668 /* Rewrite instruction {pli/pld} PC imm12 into:
5669 Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12
5670
5671 {pli/pld} [r0, r1]
5672
5673 Cleanup: r0 <- tmp[0], r1 <- tmp[1]. */
5674
5675 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5676 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5677
5678 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
5679
5680 displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
5681 displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
5682 dsc->u.preload.immed = 0;
5683
5684 /* {pli/pld} [r0, r1] */
5685 dsc->modinsn[0] = insn1 & 0xfff0;
5686 dsc->modinsn[1] = 0xf001;
5687 dsc->numinsns = 2;
5688
5689 dsc->cleanup = &cleanup_preload;
5690 return 0;
5691}
5692
7ff120b4
YQ
5693/* Preload instructions with register offset. */
5694
5695static void
5696install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
5697 struct displaced_step_closure *dsc, unsigned int rn,
5698 unsigned int rm)
5699{
5700 ULONGEST rn_val, rm_val;
5701
cca44b1b
JB
5702 /* Preload register-offset instructions:
5703
5704 {pli/pld} [rn, rm {, shift}]
5705 ->
5706 {pli/pld} [r0, r1 {, shift}]. */
5707
36073a92
YQ
5708 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5709 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5710 rn_val = displaced_read_reg (regs, dsc, rn);
5711 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5712 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5713 displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
cca44b1b
JB
5714 dsc->u.preload.immed = 0;
5715
cca44b1b 5716 dsc->cleanup = &cleanup_preload;
7ff120b4
YQ
5717}
5718
5719static int
5720arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
5721 struct regcache *regs,
5722 struct displaced_step_closure *dsc)
5723{
5724 unsigned int rn = bits (insn, 16, 19);
5725 unsigned int rm = bits (insn, 0, 3);
5726
5727
5728 if (!insn_references_pc (insn, 0x000f000ful))
5729 return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);
5730
5731 if (debug_displaced)
5732 fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
5733 (unsigned long) insn);
5734
5735 dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
cca44b1b 5736
7ff120b4 5737 install_preload_reg (gdbarch, regs, dsc, rn, rm);
cca44b1b
JB
5738 return 0;
5739}
5740
5741/* Copy/cleanup coprocessor load and store instructions. */
5742
5743static void
6e39997a 5744cleanup_copro_load_store (struct gdbarch *gdbarch,
cca44b1b
JB
5745 struct regcache *regs,
5746 struct displaced_step_closure *dsc)
5747{
36073a92 5748 ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5749
5750 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5751
5752 if (dsc->u.ldst.writeback)
5753 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
5754}
5755
7ff120b4
YQ
5756static void
5757install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
5758 struct displaced_step_closure *dsc,
5759 int writeback, unsigned int rn)
cca44b1b 5760{
cca44b1b 5761 ULONGEST rn_val;
cca44b1b 5762
cca44b1b
JB
5763 /* Coprocessor load/store instructions:
5764
5765 {stc/stc2} [<Rn>, #+/-imm] (and other immediate addressing modes)
5766 ->
5767 {stc/stc2} [r0, #+/-imm].
5768
5769 ldc/ldc2 are handled identically. */
5770
36073a92
YQ
5771 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5772 rn_val = displaced_read_reg (regs, dsc, rn);
2b16b2e3
YQ
5773 /* PC should be 4-byte aligned. */
5774 rn_val = rn_val & 0xfffffffc;
cca44b1b
JB
5775 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5776
7ff120b4 5777 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
5778 dsc->u.ldst.rn = rn;
5779
7ff120b4
YQ
5780 dsc->cleanup = &cleanup_copro_load_store;
5781}
5782
5783static int
5784arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
5785 struct regcache *regs,
5786 struct displaced_step_closure *dsc)
5787{
5788 unsigned int rn = bits (insn, 16, 19);
5789
5790 if (!insn_references_pc (insn, 0x000f0000ul))
5791 return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);
5792
5793 if (debug_displaced)
5794 fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
5795 "load/store insn %.8lx\n", (unsigned long) insn);
5796
cca44b1b
JB
5797 dsc->modinsn[0] = insn & 0xfff0ffff;
5798
7ff120b4 5799 install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);
cca44b1b
JB
5800
5801 return 0;
5802}
5803
34518530
YQ
5804static int
5805thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
5806 uint16_t insn2, struct regcache *regs,
5807 struct displaced_step_closure *dsc)
5808{
5809 unsigned int rn = bits (insn1, 0, 3);
5810
5811 if (rn != ARM_PC_REGNUM)
5812 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
5813 "copro load/store", dsc);
5814
5815 if (debug_displaced)
5816 fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
5817 "load/store insn %.4x%.4x\n", insn1, insn2);
5818
5819 dsc->modinsn[0] = insn1 & 0xfff0;
5820 dsc->modinsn[1] = insn2;
5821 dsc->numinsns = 2;
5822
5823 /* This function is called for copying instruction LDC/LDC2/VLDR, which
5824 doesn't support writeback, so pass 0. */
5825 install_copro_load_store (gdbarch, regs, dsc, 0, rn);
5826
5827 return 0;
5828}
5829
cca44b1b
JB
5830/* Clean up branch instructions (actually perform the branch, by setting
5831 PC). */
5832
5833static void
6e39997a 5834cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
5835 struct displaced_step_closure *dsc)
5836{
36073a92 5837 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
5838 int branch_taken = condition_true (dsc->u.branch.cond, status);
5839 enum pc_write_style write_pc = dsc->u.branch.exchange
5840 ? BX_WRITE_PC : BRANCH_WRITE_PC;
5841
5842 if (!branch_taken)
5843 return;
5844
5845 if (dsc->u.branch.link)
5846 {
8c8dba6d
YQ
5847 /* The value of LR should be the next insn of current one. In order
5848 not to confuse logic hanlding later insn `bx lr', if current insn mode
5849 is Thumb, the bit 0 of LR value should be set to 1. */
5850 ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;
5851
5852 if (dsc->is_thumb)
5853 next_insn_addr |= 0x1;
5854
5855 displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
5856 CANNOT_WRITE_PC);
cca44b1b
JB
5857 }
5858
bf9f652a 5859 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
cca44b1b
JB
5860}
5861
5862/* Copy B/BL/BLX instructions with immediate destinations. */
5863
7ff120b4
YQ
5864static void
5865install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
5866 struct displaced_step_closure *dsc,
5867 unsigned int cond, int exchange, int link, long offset)
5868{
5869 /* Implement "BL<cond> <label>" as:
5870
5871 Preparation: cond <- instruction condition
5872 Insn: mov r0, r0 (nop)
5873 Cleanup: if (condition true) { r14 <- pc; pc <- label }.
5874
5875 B<cond> similar, but don't set r14 in cleanup. */
5876
5877 dsc->u.branch.cond = cond;
5878 dsc->u.branch.link = link;
5879 dsc->u.branch.exchange = exchange;
5880
2b16b2e3
YQ
5881 dsc->u.branch.dest = dsc->insn_addr;
5882 if (link && exchange)
5883 /* For BLX, offset is computed from the Align (PC, 4). */
5884 dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;
5885
7ff120b4 5886 if (dsc->is_thumb)
2b16b2e3 5887 dsc->u.branch.dest += 4 + offset;
7ff120b4 5888 else
2b16b2e3 5889 dsc->u.branch.dest += 8 + offset;
7ff120b4
YQ
5890
5891 dsc->cleanup = &cleanup_branch;
5892}
cca44b1b 5893static int
7ff120b4
YQ
5894arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
5895 struct regcache *regs, struct displaced_step_closure *dsc)
cca44b1b
JB
5896{
5897 unsigned int cond = bits (insn, 28, 31);
5898 int exchange = (cond == 0xf);
5899 int link = exchange || bit (insn, 24);
cca44b1b
JB
5900 long offset;
5901
5902 if (debug_displaced)
5903 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s immediate insn "
5904 "%.8lx\n", (exchange) ? "blx" : (link) ? "bl" : "b",
5905 (unsigned long) insn);
cca44b1b
JB
5906 if (exchange)
5907 /* For BLX, set bit 0 of the destination. The cleanup_branch function will
5908 then arrange the switch into Thumb mode. */
5909 offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
5910 else
5911 offset = bits (insn, 0, 23) << 2;
5912
5913 if (bit (offset, 25))
5914 offset = offset | ~0x3ffffff;
5915
cca44b1b
JB
5916 dsc->modinsn[0] = ARM_NOP;
5917
7ff120b4 5918 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
cca44b1b
JB
5919 return 0;
5920}
5921
34518530
YQ
5922static int
5923thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
5924 uint16_t insn2, struct regcache *regs,
5925 struct displaced_step_closure *dsc)
5926{
5927 int link = bit (insn2, 14);
5928 int exchange = link && !bit (insn2, 12);
5929 int cond = INST_AL;
5930 long offset = 0;
5931 int j1 = bit (insn2, 13);
5932 int j2 = bit (insn2, 11);
5933 int s = sbits (insn1, 10, 10);
5934 int i1 = !(j1 ^ bit (insn1, 10));
5935 int i2 = !(j2 ^ bit (insn1, 10));
5936
5937 if (!link && !exchange) /* B */
5938 {
5939 offset = (bits (insn2, 0, 10) << 1);
5940 if (bit (insn2, 12)) /* Encoding T4 */
5941 {
5942 offset |= (bits (insn1, 0, 9) << 12)
5943 | (i2 << 22)
5944 | (i1 << 23)
5945 | (s << 24);
5946 cond = INST_AL;
5947 }
5948 else /* Encoding T3 */
5949 {
5950 offset |= (bits (insn1, 0, 5) << 12)
5951 | (j1 << 18)
5952 | (j2 << 19)
5953 | (s << 20);
5954 cond = bits (insn1, 6, 9);
5955 }
5956 }
5957 else
5958 {
5959 offset = (bits (insn1, 0, 9) << 12);
5960 offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
5961 offset |= exchange ?
5962 (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
5963 }
5964
5965 if (debug_displaced)
5966 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s insn "
5967 "%.4x %.4x with offset %.8lx\n",
5968 link ? (exchange) ? "blx" : "bl" : "b",
5969 insn1, insn2, offset);
5970
5971 dsc->modinsn[0] = THUMB_NOP;
5972
5973 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
5974 return 0;
5975}
5976
5977/* Copy B Thumb instructions. */
5978static int
5979thumb_copy_b (struct gdbarch *gdbarch, unsigned short insn,
5980 struct displaced_step_closure *dsc)
5981{
5982 unsigned int cond = 0;
5983 int offset = 0;
5984 unsigned short bit_12_15 = bits (insn, 12, 15);
5985 CORE_ADDR from = dsc->insn_addr;
5986
5987 if (bit_12_15 == 0xd)
5988 {
5989 /* offset = SignExtend (imm8:0, 32) */
5990 offset = sbits ((insn << 1), 0, 8);
5991 cond = bits (insn, 8, 11);
5992 }
5993 else if (bit_12_15 == 0xe) /* Encoding T2 */
5994 {
5995 offset = sbits ((insn << 1), 0, 11);
5996 cond = INST_AL;
5997 }
5998
5999 if (debug_displaced)
6000 fprintf_unfiltered (gdb_stdlog,
6001 "displaced: copying b immediate insn %.4x "
6002 "with offset %d\n", insn, offset);
6003
6004 dsc->u.branch.cond = cond;
6005 dsc->u.branch.link = 0;
6006 dsc->u.branch.exchange = 0;
6007 dsc->u.branch.dest = from + 4 + offset;
6008
6009 dsc->modinsn[0] = THUMB_NOP;
6010
6011 dsc->cleanup = &cleanup_branch;
6012
6013 return 0;
6014}
6015
cca44b1b
JB
6016/* Copy BX/BLX with register-specified destinations. */
6017
7ff120b4
YQ
6018static void
6019install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
6020 struct displaced_step_closure *dsc, int link,
6021 unsigned int cond, unsigned int rm)
cca44b1b 6022{
cca44b1b
JB
6023 /* Implement {BX,BLX}<cond> <reg>" as:
6024
6025 Preparation: cond <- instruction condition
6026 Insn: mov r0, r0 (nop)
6027 Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
6028
6029 Don't set r14 in cleanup for BX. */
6030
36073a92 6031 dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6032
6033 dsc->u.branch.cond = cond;
6034 dsc->u.branch.link = link;
cca44b1b 6035
7ff120b4 6036 dsc->u.branch.exchange = 1;
cca44b1b
JB
6037
6038 dsc->cleanup = &cleanup_branch;
7ff120b4 6039}
cca44b1b 6040
7ff120b4
YQ
6041static int
6042arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
6043 struct regcache *regs, struct displaced_step_closure *dsc)
6044{
6045 unsigned int cond = bits (insn, 28, 31);
6046 /* BX: x12xxx1x
6047 BLX: x12xxx3x. */
6048 int link = bit (insn, 5);
6049 unsigned int rm = bits (insn, 0, 3);
6050
6051 if (debug_displaced)
6052 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx",
6053 (unsigned long) insn);
6054
6055 dsc->modinsn[0] = ARM_NOP;
6056
6057 install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
cca44b1b
JB
6058 return 0;
6059}
6060
34518530
YQ
6061static int
6062thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
6063 struct regcache *regs,
6064 struct displaced_step_closure *dsc)
6065{
6066 int link = bit (insn, 7);
6067 unsigned int rm = bits (insn, 3, 6);
6068
6069 if (debug_displaced)
6070 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x",
6071 (unsigned short) insn);
6072
6073 dsc->modinsn[0] = THUMB_NOP;
6074
6075 install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);
6076
6077 return 0;
6078}
6079
6080
0963b4bd 6081/* Copy/cleanup arithmetic/logic instruction with immediate RHS. */
cca44b1b
JB
6082
6083static void
6e39997a 6084cleanup_alu_imm (struct gdbarch *gdbarch,
cca44b1b
JB
6085 struct regcache *regs, struct displaced_step_closure *dsc)
6086{
36073a92 6087 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6088 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6089 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6090 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6091}
6092
6093static int
7ff120b4
YQ
6094arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
6095 struct displaced_step_closure *dsc)
cca44b1b
JB
6096{
6097 unsigned int rn = bits (insn, 16, 19);
6098 unsigned int rd = bits (insn, 12, 15);
6099 unsigned int op = bits (insn, 21, 24);
6100 int is_mov = (op == 0xd);
6101 ULONGEST rd_val, rn_val;
cca44b1b
JB
6102
6103 if (!insn_references_pc (insn, 0x000ff000ul))
7ff120b4 6104 return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
cca44b1b
JB
6105
6106 if (debug_displaced)
6107 fprintf_unfiltered (gdb_stdlog, "displaced: copying immediate %s insn "
6108 "%.8lx\n", is_mov ? "move" : "ALU",
6109 (unsigned long) insn);
6110
6111 /* Instruction is of form:
6112
6113 <op><cond> rd, [rn,] #imm
6114
6115 Rewrite as:
6116
6117 Preparation: tmp1, tmp2 <- r0, r1;
6118 r0, r1 <- rd, rn
6119 Insn: <op><cond> r0, r1, #imm
6120 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6121 */
6122
36073a92
YQ
6123 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6124 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6125 rn_val = displaced_read_reg (regs, dsc, rn);
6126 rd_val = displaced_read_reg (regs, dsc, rd);
cca44b1b
JB
6127 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6128 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6129 dsc->rd = rd;
6130
6131 if (is_mov)
6132 dsc->modinsn[0] = insn & 0xfff00fff;
6133 else
6134 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
6135
6136 dsc->cleanup = &cleanup_alu_imm;
6137
6138 return 0;
6139}
6140
34518530
YQ
6141static int
6142thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
6143 uint16_t insn2, struct regcache *regs,
6144 struct displaced_step_closure *dsc)
6145{
6146 unsigned int op = bits (insn1, 5, 8);
6147 unsigned int rn, rm, rd;
6148 ULONGEST rd_val, rn_val;
6149
6150 rn = bits (insn1, 0, 3); /* Rn */
6151 rm = bits (insn2, 0, 3); /* Rm */
6152 rd = bits (insn2, 8, 11); /* Rd */
6153
6154 /* This routine is only called for instruction MOV. */
6155 gdb_assert (op == 0x2 && rn == 0xf);
6156
6157 if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
6158 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);
6159
6160 if (debug_displaced)
6161 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.4x%.4x\n",
6162 "ALU", insn1, insn2);
6163
6164 /* Instruction is of form:
6165
6166 <op><cond> rd, [rn,] #imm
6167
6168 Rewrite as:
6169
6170 Preparation: tmp1, tmp2 <- r0, r1;
6171 r0, r1 <- rd, rn
6172 Insn: <op><cond> r0, r1, #imm
6173 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6174 */
6175
6176 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6177 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6178 rn_val = displaced_read_reg (regs, dsc, rn);
6179 rd_val = displaced_read_reg (regs, dsc, rd);
6180 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6181 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6182 dsc->rd = rd;
6183
6184 dsc->modinsn[0] = insn1;
6185 dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
6186 dsc->numinsns = 2;
6187
6188 dsc->cleanup = &cleanup_alu_imm;
6189
6190 return 0;
6191}
6192
cca44b1b
JB
6193/* Copy/cleanup arithmetic/logic insns with register RHS. */
6194
6195static void
6e39997a 6196cleanup_alu_reg (struct gdbarch *gdbarch,
cca44b1b
JB
6197 struct regcache *regs, struct displaced_step_closure *dsc)
6198{
6199 ULONGEST rd_val;
6200 int i;
6201
36073a92 6202 rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6203
6204 for (i = 0; i < 3; i++)
6205 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6206
6207 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6208}
6209
7ff120b4
YQ
6210static void
6211install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
6212 struct displaced_step_closure *dsc,
6213 unsigned int rd, unsigned int rn, unsigned int rm)
cca44b1b 6214{
cca44b1b 6215 ULONGEST rd_val, rn_val, rm_val;
cca44b1b 6216
cca44b1b
JB
6217 /* Instruction is of form:
6218
6219 <op><cond> rd, [rn,] rm [, <shift>]
6220
6221 Rewrite as:
6222
6223 Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
6224 r0, r1, r2 <- rd, rn, rm
6225 Insn: <op><cond> r0, r1, r2 [, <shift>]
6226 Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
6227 */
6228
36073a92
YQ
6229 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6230 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6231 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6232 rd_val = displaced_read_reg (regs, dsc, rd);
6233 rn_val = displaced_read_reg (regs, dsc, rn);
6234 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6235 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6236 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6237 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6238 dsc->rd = rd;
6239
7ff120b4
YQ
6240 dsc->cleanup = &cleanup_alu_reg;
6241}
6242
6243static int
6244arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
6245 struct displaced_step_closure *dsc)
6246{
6247 unsigned int op = bits (insn, 21, 24);
6248 int is_mov = (op == 0xd);
6249
6250 if (!insn_references_pc (insn, 0x000ff00ful))
6251 return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);
6252
6253 if (debug_displaced)
6254 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.8lx\n",
6255 is_mov ? "move" : "ALU", (unsigned long) insn);
6256
cca44b1b
JB
6257 if (is_mov)
6258 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
6259 else
6260 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
6261
7ff120b4
YQ
6262 install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
6263 bits (insn, 0, 3));
cca44b1b
JB
6264 return 0;
6265}
6266
34518530
YQ
6267static int
6268thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
6269 struct regcache *regs,
6270 struct displaced_step_closure *dsc)
6271{
6272 unsigned rn, rm, rd;
6273
6274 rd = bits (insn, 3, 6);
6275 rn = (bit (insn, 7) << 3) | bits (insn, 0, 2);
6276 rm = 2;
6277
6278 if (rd != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
6279 return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);
6280
6281 if (debug_displaced)
6282 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.4x\n",
6283 "ALU", (unsigned short) insn);
6284
6285 dsc->modinsn[0] = ((insn & 0xff00) | 0x08);
6286
6287 install_alu_reg (gdbarch, regs, dsc, rd, rn, rm);
6288
6289 return 0;
6290}
6291
cca44b1b
JB
6292/* Cleanup/copy arithmetic/logic insns with shifted register RHS. */
6293
6294static void
6e39997a 6295cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
cca44b1b
JB
6296 struct regcache *regs,
6297 struct displaced_step_closure *dsc)
6298{
36073a92 6299 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6300 int i;
6301
6302 for (i = 0; i < 4; i++)
6303 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6304
6305 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6306}
6307
7ff120b4
YQ
6308static void
6309install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
6310 struct displaced_step_closure *dsc,
6311 unsigned int rd, unsigned int rn, unsigned int rm,
6312 unsigned rs)
cca44b1b 6313{
7ff120b4 6314 int i;
cca44b1b 6315 ULONGEST rd_val, rn_val, rm_val, rs_val;
cca44b1b 6316
cca44b1b
JB
6317 /* Instruction is of form:
6318
6319 <op><cond> rd, [rn,] rm, <shift> rs
6320
6321 Rewrite as:
6322
6323 Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
6324 r0, r1, r2, r3 <- rd, rn, rm, rs
6325 Insn: <op><cond> r0, r1, r2, <shift> r3
6326 Cleanup: tmp5 <- r0
6327 r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
6328 rd <- tmp5
6329 */
6330
6331 for (i = 0; i < 4; i++)
36073a92 6332 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b 6333
36073a92
YQ
6334 rd_val = displaced_read_reg (regs, dsc, rd);
6335 rn_val = displaced_read_reg (regs, dsc, rn);
6336 rm_val = displaced_read_reg (regs, dsc, rm);
6337 rs_val = displaced_read_reg (regs, dsc, rs);
cca44b1b
JB
6338 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6339 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6340 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6341 displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
6342 dsc->rd = rd;
7ff120b4
YQ
6343 dsc->cleanup = &cleanup_alu_shifted_reg;
6344}
6345
6346static int
6347arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
6348 struct regcache *regs,
6349 struct displaced_step_closure *dsc)
6350{
6351 unsigned int op = bits (insn, 21, 24);
6352 int is_mov = (op == 0xd);
6353 unsigned int rd, rn, rm, rs;
6354
6355 if (!insn_references_pc (insn, 0x000fff0ful))
6356 return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
6357
6358 if (debug_displaced)
6359 fprintf_unfiltered (gdb_stdlog, "displaced: copying shifted reg %s insn "
6360 "%.8lx\n", is_mov ? "move" : "ALU",
6361 (unsigned long) insn);
6362
6363 rn = bits (insn, 16, 19);
6364 rm = bits (insn, 0, 3);
6365 rs = bits (insn, 8, 11);
6366 rd = bits (insn, 12, 15);
cca44b1b
JB
6367
6368 if (is_mov)
6369 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
6370 else
6371 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
6372
7ff120b4 6373 install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);
cca44b1b
JB
6374
6375 return 0;
6376}
6377
6378/* Clean up load instructions. */
6379
6380static void
6e39997a 6381cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
6382 struct displaced_step_closure *dsc)
6383{
6384 ULONGEST rt_val, rt_val2 = 0, rn_val;
cca44b1b 6385
36073a92 6386 rt_val = displaced_read_reg (regs, dsc, 0);
cca44b1b 6387 if (dsc->u.ldst.xfersize == 8)
36073a92
YQ
6388 rt_val2 = displaced_read_reg (regs, dsc, 1);
6389 rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
6390
6391 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6392 if (dsc->u.ldst.xfersize > 4)
6393 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6394 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6395 if (!dsc->u.ldst.immed)
6396 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6397
6398 /* Handle register writeback. */
6399 if (dsc->u.ldst.writeback)
6400 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6401 /* Put result in right place. */
6402 displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
6403 if (dsc->u.ldst.xfersize == 8)
6404 displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
6405}
6406
6407/* Clean up store instructions. */
6408
6409static void
6e39997a 6410cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
6411 struct displaced_step_closure *dsc)
6412{
36073a92 6413 ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
6414
6415 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6416 if (dsc->u.ldst.xfersize > 4)
6417 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6418 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6419 if (!dsc->u.ldst.immed)
6420 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6421 if (!dsc->u.ldst.restore_r4)
6422 displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
6423
6424 /* Writeback. */
6425 if (dsc->u.ldst.writeback)
6426 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6427}
6428
6429/* Copy "extra" load/store instructions. These are halfword/doubleword
6430 transfers, which have a different encoding to byte/word transfers. */
6431
6432static int
7ff120b4
YQ
6433arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unpriveleged,
6434 struct regcache *regs, struct displaced_step_closure *dsc)
cca44b1b
JB
6435{
6436 unsigned int op1 = bits (insn, 20, 24);
6437 unsigned int op2 = bits (insn, 5, 6);
6438 unsigned int rt = bits (insn, 12, 15);
6439 unsigned int rn = bits (insn, 16, 19);
6440 unsigned int rm = bits (insn, 0, 3);
6441 char load[12] = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
6442 char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
6443 int immed = (op1 & 0x4) != 0;
6444 int opcode;
6445 ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
cca44b1b
JB
6446
6447 if (!insn_references_pc (insn, 0x000ff00ful))
7ff120b4 6448 return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);
cca44b1b
JB
6449
6450 if (debug_displaced)
6451 fprintf_unfiltered (gdb_stdlog, "displaced: copying %sextra load/store "
6452 "insn %.8lx\n", unpriveleged ? "unpriveleged " : "",
6453 (unsigned long) insn);
6454
6455 opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
6456
6457 if (opcode < 0)
6458 internal_error (__FILE__, __LINE__,
6459 _("copy_extra_ld_st: instruction decode error"));
6460
36073a92
YQ
6461 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6462 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6463 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 6464 if (!immed)
36073a92 6465 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 6466
36073a92 6467 rt_val = displaced_read_reg (regs, dsc, rt);
cca44b1b 6468 if (bytesize[opcode] == 8)
36073a92
YQ
6469 rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
6470 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 6471 if (!immed)
36073a92 6472 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6473
6474 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6475 if (bytesize[opcode] == 8)
6476 displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
6477 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6478 if (!immed)
6479 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
6480
6481 dsc->rd = rt;
6482 dsc->u.ldst.xfersize = bytesize[opcode];
6483 dsc->u.ldst.rn = rn;
6484 dsc->u.ldst.immed = immed;
6485 dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
6486 dsc->u.ldst.restore_r4 = 0;
6487
6488 if (immed)
6489 /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
6490 ->
6491 {ldr,str}<width><cond> r0, [r1,] [r2, #imm]. */
6492 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6493 else
6494 /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
6495 ->
6496 {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3]. */
6497 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6498
6499 dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
6500
6501 return 0;
6502}
6503
0f6f04ba 6504/* Copy byte/half word/word loads and stores. */
cca44b1b 6505
7ff120b4 6506static void
0f6f04ba
YQ
6507install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
6508 struct displaced_step_closure *dsc, int load,
6509 int immed, int writeback, int size, int usermode,
6510 int rt, int rm, int rn)
cca44b1b 6511{
cca44b1b 6512 ULONGEST rt_val, rn_val, rm_val = 0;
cca44b1b 6513
36073a92
YQ
6514 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6515 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 6516 if (!immed)
36073a92 6517 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 6518 if (!load)
36073a92 6519 dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);
cca44b1b 6520
36073a92
YQ
6521 rt_val = displaced_read_reg (regs, dsc, rt);
6522 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 6523 if (!immed)
36073a92 6524 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6525
6526 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6527 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6528 if (!immed)
6529 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
cca44b1b 6530 dsc->rd = rt;
0f6f04ba 6531 dsc->u.ldst.xfersize = size;
cca44b1b
JB
6532 dsc->u.ldst.rn = rn;
6533 dsc->u.ldst.immed = immed;
7ff120b4 6534 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
6535
6536 /* To write PC we can do:
6537
494e194e
YQ
6538 Before this sequence of instructions:
6539 r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
6540 r2 is the Rn value got from dispalced_read_reg.
6541
6542 Insn1: push {pc} Write address of STR instruction + offset on stack
6543 Insn2: pop {r4} Read it back from stack, r4 = addr(Insn1) + offset
6544 Insn3: sub r4, r4, pc r4 = addr(Insn1) + offset - pc
6545 = addr(Insn1) + offset - addr(Insn3) - 8
6546 = offset - 16
6547 Insn4: add r4, r4, #8 r4 = offset - 8
6548 Insn5: add r0, r0, r4 r0 = from + 8 + offset - 8
6549 = from + offset
6550 Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])
cca44b1b
JB
6551
6552 Otherwise we don't know what value to write for PC, since the offset is
494e194e
YQ
6553 architecture-dependent (sometimes PC+8, sometimes PC+12). More details
6554 of this can be found in Section "Saving from r15" in
6555 http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */
cca44b1b 6556
7ff120b4
YQ
6557 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6558}
6559
34518530
YQ
6560
6561static int
6562thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
6563 uint16_t insn2, struct regcache *regs,
6564 struct displaced_step_closure *dsc, int size)
6565{
6566 unsigned int u_bit = bit (insn1, 7);
6567 unsigned int rt = bits (insn2, 12, 15);
6568 int imm12 = bits (insn2, 0, 11);
6569 ULONGEST pc_val;
6570
6571 if (debug_displaced)
6572 fprintf_unfiltered (gdb_stdlog,
6573 "displaced: copying ldr pc (0x%x) R%d %c imm12 %.4x\n",
6574 (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
6575 imm12);
6576
6577 if (!u_bit)
6578 imm12 = -1 * imm12;
6579
6580 /* Rewrite instruction LDR Rt imm12 into:
6581
6582 Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12
6583
6584 LDR R0, R2, R3,
6585
6586 Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2]. */
6587
6588
6589 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6590 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6591 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6592
6593 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6594
6595 pc_val = pc_val & 0xfffffffc;
6596
6597 displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
6598 displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);
6599
6600 dsc->rd = rt;
6601
6602 dsc->u.ldst.xfersize = size;
6603 dsc->u.ldst.immed = 0;
6604 dsc->u.ldst.writeback = 0;
6605 dsc->u.ldst.restore_r4 = 0;
6606
6607 /* LDR R0, R2, R3 */
6608 dsc->modinsn[0] = 0xf852;
6609 dsc->modinsn[1] = 0x3;
6610 dsc->numinsns = 2;
6611
6612 dsc->cleanup = &cleanup_load;
6613
6614 return 0;
6615}
6616
6617static int
6618thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
6619 uint16_t insn2, struct regcache *regs,
6620 struct displaced_step_closure *dsc,
6621 int writeback, int immed)
6622{
6623 unsigned int rt = bits (insn2, 12, 15);
6624 unsigned int rn = bits (insn1, 0, 3);
6625 unsigned int rm = bits (insn2, 0, 3); /* Only valid if !immed. */
6626 /* In LDR (register), there is also a register Rm, which is not allowed to
6627 be PC, so we don't have to check it. */
6628
6629 if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
6630 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
6631 dsc);
6632
6633 if (debug_displaced)
6634 fprintf_unfiltered (gdb_stdlog,
6635 "displaced: copying ldr r%d [r%d] insn %.4x%.4x\n",
6636 rt, rn, insn1, insn2);
6637
6638 install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
6639 0, rt, rm, rn);
6640
6641 dsc->u.ldst.restore_r4 = 0;
6642
6643 if (immed)
6644 /* ldr[b]<cond> rt, [rn, #imm], etc.
6645 ->
6646 ldr[b]<cond> r0, [r2, #imm]. */
6647 {
6648 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6649 dsc->modinsn[1] = insn2 & 0x0fff;
6650 }
6651 else
6652 /* ldr[b]<cond> rt, [rn, rm], etc.
6653 ->
6654 ldr[b]<cond> r0, [r2, r3]. */
6655 {
6656 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6657 dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
6658 }
6659
6660 dsc->numinsns = 2;
6661
6662 return 0;
6663}
6664
6665
7ff120b4
YQ
6666static int
6667arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
6668 struct regcache *regs,
6669 struct displaced_step_closure *dsc,
0f6f04ba 6670 int load, int size, int usermode)
7ff120b4
YQ
6671{
6672 int immed = !bit (insn, 25);
6673 int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
6674 unsigned int rt = bits (insn, 12, 15);
6675 unsigned int rn = bits (insn, 16, 19);
6676 unsigned int rm = bits (insn, 0, 3); /* Only valid if !immed. */
6677
6678 if (!insn_references_pc (insn, 0x000ff00ful))
6679 return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);
6680
6681 if (debug_displaced)
6682 fprintf_unfiltered (gdb_stdlog,
6683 "displaced: copying %s%s r%d [r%d] insn %.8lx\n",
0f6f04ba
YQ
6684 load ? (size == 1 ? "ldrb" : "ldr")
6685 : (size == 1 ? "strb" : "str"), usermode ? "t" : "",
7ff120b4
YQ
6686 rt, rn,
6687 (unsigned long) insn);
6688
0f6f04ba
YQ
6689 install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
6690 usermode, rt, rm, rn);
7ff120b4 6691
bf9f652a 6692 if (load || rt != ARM_PC_REGNUM)
cca44b1b
JB
6693 {
6694 dsc->u.ldst.restore_r4 = 0;
6695
6696 if (immed)
6697 /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
6698 ->
6699 {ldr,str}[b]<cond> r0, [r2, #imm]. */
6700 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6701 else
6702 /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
6703 ->
6704 {ldr,str}[b]<cond> r0, [r2, r3]. */
6705 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6706 }
6707 else
6708 {
6709 /* We need to use r4 as scratch. Make sure it's restored afterwards. */
6710 dsc->u.ldst.restore_r4 = 1;
494e194e
YQ
6711 dsc->modinsn[0] = 0xe92d8000; /* push {pc} */
6712 dsc->modinsn[1] = 0xe8bd0010; /* pop {r4} */
cca44b1b
JB
6713 dsc->modinsn[2] = 0xe044400f; /* sub r4, r4, pc. */
6714 dsc->modinsn[3] = 0xe2844008; /* add r4, r4, #8. */
6715 dsc->modinsn[4] = 0xe0800004; /* add r0, r0, r4. */
6716
6717 /* As above. */
6718 if (immed)
6719 dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
6720 else
6721 dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
6722
cca44b1b
JB
6723 dsc->numinsns = 6;
6724 }
6725
6726 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6727
6728 return 0;
6729}
6730
6731/* Cleanup LDM instructions with fully-populated register list. This is an
6732 unfortunate corner case: it's impossible to implement correctly by modifying
6733 the instruction. The issue is as follows: we have an instruction,
6734
6735 ldm rN, {r0-r15}
6736
6737 which we must rewrite to avoid loading PC. A possible solution would be to
6738 do the load in two halves, something like (with suitable cleanup
6739 afterwards):
6740
6741 mov r8, rN
6742 ldm[id][ab] r8!, {r0-r7}
6743 str r7, <temp>
6744 ldm[id][ab] r8, {r7-r14}
6745 <bkpt>
6746
6747 but at present there's no suitable place for <temp>, since the scratch space
6748 is overwritten before the cleanup routine is called. For now, we simply
6749 emulate the instruction. */
6750
6751static void
6752cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
6753 struct displaced_step_closure *dsc)
6754{
cca44b1b
JB
6755 int inc = dsc->u.block.increment;
6756 int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
6757 int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
6758 uint32_t regmask = dsc->u.block.regmask;
6759 int regno = inc ? 0 : 15;
6760 CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
6761 int exception_return = dsc->u.block.load && dsc->u.block.user
6762 && (regmask & 0x8000) != 0;
36073a92 6763 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
6764 int do_transfer = condition_true (dsc->u.block.cond, status);
6765 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6766
6767 if (!do_transfer)
6768 return;
6769
6770 /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
6771 sensible we can do here. Complain loudly. */
6772 if (exception_return)
6773 error (_("Cannot single-step exception return"));
6774
6775 /* We don't handle any stores here for now. */
6776 gdb_assert (dsc->u.block.load != 0);
6777
6778 if (debug_displaced)
6779 fprintf_unfiltered (gdb_stdlog, "displaced: emulating block transfer: "
6780 "%s %s %s\n", dsc->u.block.load ? "ldm" : "stm",
6781 dsc->u.block.increment ? "inc" : "dec",
6782 dsc->u.block.before ? "before" : "after");
6783
6784 while (regmask)
6785 {
6786 uint32_t memword;
6787
6788 if (inc)
bf9f652a 6789 while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
cca44b1b
JB
6790 regno++;
6791 else
6792 while (regno >= 0 && (regmask & (1 << regno)) == 0)
6793 regno--;
6794
6795 xfer_addr += bump_before;
6796
6797 memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
6798 displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
6799
6800 xfer_addr += bump_after;
6801
6802 regmask &= ~(1 << regno);
6803 }
6804
6805 if (dsc->u.block.writeback)
6806 displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
6807 CANNOT_WRITE_PC);
6808}
6809
6810/* Clean up an STM which included the PC in the register list. */
6811
6812static void
6813cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
6814 struct displaced_step_closure *dsc)
6815{
36073a92 6816 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
6817 int store_executed = condition_true (dsc->u.block.cond, status);
6818 CORE_ADDR pc_stored_at, transferred_regs = bitcount (dsc->u.block.regmask);
6819 CORE_ADDR stm_insn_addr;
6820 uint32_t pc_val;
6821 long offset;
6822 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6823
6824 /* If condition code fails, there's nothing else to do. */
6825 if (!store_executed)
6826 return;
6827
6828 if (dsc->u.block.increment)
6829 {
6830 pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
6831
6832 if (dsc->u.block.before)
6833 pc_stored_at += 4;
6834 }
6835 else
6836 {
6837 pc_stored_at = dsc->u.block.xfer_addr;
6838
6839 if (dsc->u.block.before)
6840 pc_stored_at -= 4;
6841 }
6842
6843 pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
6844 stm_insn_addr = dsc->scratch_base;
6845 offset = pc_val - stm_insn_addr;
6846
6847 if (debug_displaced)
6848 fprintf_unfiltered (gdb_stdlog, "displaced: detected PC offset %.8lx for "
6849 "STM instruction\n", offset);
6850
6851 /* Rewrite the stored PC to the proper value for the non-displaced original
6852 instruction. */
6853 write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
6854 dsc->insn_addr + offset);
6855}
6856
6857/* Clean up an LDM which includes the PC in the register list. We clumped all
6858 the registers in the transferred list into a contiguous range r0...rX (to
6859 avoid loading PC directly and losing control of the debugged program), so we
6860 must undo that here. */
6861
6862static void
6e39997a 6863cleanup_block_load_pc (struct gdbarch *gdbarch,
cca44b1b
JB
6864 struct regcache *regs,
6865 struct displaced_step_closure *dsc)
6866{
36073a92 6867 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b 6868 int load_executed = condition_true (dsc->u.block.cond, status), i;
bf9f652a 6869 unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
cca44b1b
JB
6870 unsigned int regs_loaded = bitcount (mask);
6871 unsigned int num_to_shuffle = regs_loaded, clobbered;
6872
6873 /* The method employed here will fail if the register list is fully populated
6874 (we need to avoid loading PC directly). */
6875 gdb_assert (num_to_shuffle < 16);
6876
6877 if (!load_executed)
6878 return;
6879
6880 clobbered = (1 << num_to_shuffle) - 1;
6881
6882 while (num_to_shuffle > 0)
6883 {
6884 if ((mask & (1 << write_reg)) != 0)
6885 {
6886 unsigned int read_reg = num_to_shuffle - 1;
6887
6888 if (read_reg != write_reg)
6889 {
36073a92 6890 ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
cca44b1b
JB
6891 displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
6892 if (debug_displaced)
6893 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: move "
6894 "loaded register r%d to r%d\n"), read_reg,
6895 write_reg);
6896 }
6897 else if (debug_displaced)
6898 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: register "
6899 "r%d already in the right place\n"),
6900 write_reg);
6901
6902 clobbered &= ~(1 << write_reg);
6903
6904 num_to_shuffle--;
6905 }
6906
6907 write_reg--;
6908 }
6909
6910 /* Restore any registers we scribbled over. */
6911 for (write_reg = 0; clobbered != 0; write_reg++)
6912 {
6913 if ((clobbered & (1 << write_reg)) != 0)
6914 {
6915 displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
6916 CANNOT_WRITE_PC);
6917 if (debug_displaced)
6918 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: restored "
6919 "clobbered register r%d\n"), write_reg);
6920 clobbered &= ~(1 << write_reg);
6921 }
6922 }
6923
6924 /* Perform register writeback manually. */
6925 if (dsc->u.block.writeback)
6926 {
6927 ULONGEST new_rn_val = dsc->u.block.xfer_addr;
6928
6929 if (dsc->u.block.increment)
6930 new_rn_val += regs_loaded * 4;
6931 else
6932 new_rn_val -= regs_loaded * 4;
6933
6934 displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
6935 CANNOT_WRITE_PC);
6936 }
6937}
6938
6939/* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
6940 in user-level code (in particular exception return, ldm rn, {...pc}^). */
6941
6942static int
7ff120b4
YQ
6943arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
6944 struct regcache *regs,
6945 struct displaced_step_closure *dsc)
cca44b1b
JB
6946{
6947 int load = bit (insn, 20);
6948 int user = bit (insn, 22);
6949 int increment = bit (insn, 23);
6950 int before = bit (insn, 24);
6951 int writeback = bit (insn, 21);
6952 int rn = bits (insn, 16, 19);
cca44b1b 6953
0963b4bd
MS
6954 /* Block transfers which don't mention PC can be run directly
6955 out-of-line. */
bf9f652a 6956 if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
7ff120b4 6957 return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
cca44b1b 6958
bf9f652a 6959 if (rn == ARM_PC_REGNUM)
cca44b1b 6960 {
0963b4bd
MS
6961 warning (_("displaced: Unpredictable LDM or STM with "
6962 "base register r15"));
7ff120b4 6963 return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
cca44b1b
JB
6964 }
6965
6966 if (debug_displaced)
6967 fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
6968 "%.8lx\n", (unsigned long) insn);
6969
36073a92 6970 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b
JB
6971 dsc->u.block.rn = rn;
6972
6973 dsc->u.block.load = load;
6974 dsc->u.block.user = user;
6975 dsc->u.block.increment = increment;
6976 dsc->u.block.before = before;
6977 dsc->u.block.writeback = writeback;
6978 dsc->u.block.cond = bits (insn, 28, 31);
6979
6980 dsc->u.block.regmask = insn & 0xffff;
6981
6982 if (load)
6983 {
6984 if ((insn & 0xffff) == 0xffff)
6985 {
6986 /* LDM with a fully-populated register list. This case is
6987 particularly tricky. Implement for now by fully emulating the
6988 instruction (which might not behave perfectly in all cases, but
6989 these instructions should be rare enough for that not to matter
6990 too much). */
6991 dsc->modinsn[0] = ARM_NOP;
6992
6993 dsc->cleanup = &cleanup_block_load_all;
6994 }
6995 else
6996 {
6997 /* LDM of a list of registers which includes PC. Implement by
6998 rewriting the list of registers to be transferred into a
6999 contiguous chunk r0...rX before doing the transfer, then shuffling
7000 registers into the correct places in the cleanup routine. */
7001 unsigned int regmask = insn & 0xffff;
7002 unsigned int num_in_list = bitcount (regmask), new_regmask, bit = 1;
7003 unsigned int to = 0, from = 0, i, new_rn;
7004
7005 for (i = 0; i < num_in_list; i++)
36073a92 7006 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b
JB
7007
7008 /* Writeback makes things complicated. We need to avoid clobbering
7009 the base register with one of the registers in our modified
7010 register list, but just using a different register can't work in
7011 all cases, e.g.:
7012
7013 ldm r14!, {r0-r13,pc}
7014
7015 which would need to be rewritten as:
7016
7017 ldm rN!, {r0-r14}
7018
7019 but that can't work, because there's no free register for N.
7020
7021 Solve this by turning off the writeback bit, and emulating
7022 writeback manually in the cleanup routine. */
7023
7024 if (writeback)
7025 insn &= ~(1 << 21);
7026
7027 new_regmask = (1 << num_in_list) - 1;
7028
7029 if (debug_displaced)
7030 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
7031 "{..., pc}: original reg list %.4x, modified "
7032 "list %.4x\n"), rn, writeback ? "!" : "",
7033 (int) insn & 0xffff, new_regmask);
7034
7035 dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
7036
7037 dsc->cleanup = &cleanup_block_load_pc;
7038 }
7039 }
7040 else
7041 {
7042 /* STM of a list of registers which includes PC. Run the instruction
7043 as-is, but out of line: this will store the wrong value for the PC,
7044 so we must manually fix up the memory in the cleanup routine.
7045 Doing things this way has the advantage that we can auto-detect
7046 the offset of the PC write (which is architecture-dependent) in
7047 the cleanup routine. */
7048 dsc->modinsn[0] = insn;
7049
7050 dsc->cleanup = &cleanup_block_store_pc;
7051 }
7052
7053 return 0;
7054}
7055
34518530
YQ
7056static int
7057thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
7058 struct regcache *regs,
7059 struct displaced_step_closure *dsc)
cca44b1b 7060{
34518530
YQ
7061 int rn = bits (insn1, 0, 3);
7062 int load = bit (insn1, 4);
7063 int writeback = bit (insn1, 5);
cca44b1b 7064
34518530
YQ
7065 /* Block transfers which don't mention PC can be run directly
7066 out-of-line. */
7067 if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
7068 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);
7ff120b4 7069
34518530
YQ
7070 if (rn == ARM_PC_REGNUM)
7071 {
7072 warning (_("displaced: Unpredictable LDM or STM with "
7073 "base register r15"));
7074 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7075 "unpredictable ldm/stm", dsc);
7076 }
cca44b1b
JB
7077
7078 if (debug_displaced)
34518530
YQ
7079 fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
7080 "%.4x%.4x\n", insn1, insn2);
cca44b1b 7081
34518530
YQ
7082 /* Clear bit 13, since it should be always zero. */
7083 dsc->u.block.regmask = (insn2 & 0xdfff);
7084 dsc->u.block.rn = rn;
cca44b1b 7085
34518530
YQ
7086 dsc->u.block.load = load;
7087 dsc->u.block.user = 0;
7088 dsc->u.block.increment = bit (insn1, 7);
7089 dsc->u.block.before = bit (insn1, 8);
7090 dsc->u.block.writeback = writeback;
7091 dsc->u.block.cond = INST_AL;
7092 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b 7093
34518530
YQ
7094 if (load)
7095 {
7096 if (dsc->u.block.regmask == 0xffff)
7097 {
7098 /* This branch is impossible to happen. */
7099 gdb_assert (0);
7100 }
7101 else
7102 {
7103 unsigned int regmask = dsc->u.block.regmask;
7104 unsigned int num_in_list = bitcount (regmask), new_regmask, bit = 1;
7105 unsigned int to = 0, from = 0, i, new_rn;
7106
7107 for (i = 0; i < num_in_list; i++)
7108 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7109
7110 if (writeback)
7111 insn1 &= ~(1 << 5);
7112
7113 new_regmask = (1 << num_in_list) - 1;
7114
7115 if (debug_displaced)
7116 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
7117 "{..., pc}: original reg list %.4x, modified "
7118 "list %.4x\n"), rn, writeback ? "!" : "",
7119 (int) dsc->u.block.regmask, new_regmask);
7120
7121 dsc->modinsn[0] = insn1;
7122 dsc->modinsn[1] = (new_regmask & 0xffff);
7123 dsc->numinsns = 2;
7124
7125 dsc->cleanup = &cleanup_block_load_pc;
7126 }
7127 }
7128 else
7129 {
7130 dsc->modinsn[0] = insn1;
7131 dsc->modinsn[1] = insn2;
7132 dsc->numinsns = 2;
7133 dsc->cleanup = &cleanup_block_store_pc;
7134 }
7135 return 0;
7136}
7137
7138/* Cleanup/copy SVC (SWI) instructions. These two functions are overridden
7139 for Linux, where some SVC instructions must be treated specially. */
7140
7141static void
7142cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
7143 struct displaced_step_closure *dsc)
7144{
7145 CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
7146
7147 if (debug_displaced)
7148 fprintf_unfiltered (gdb_stdlog, "displaced: cleanup for svc, resume at "
7149 "%.8lx\n", (unsigned long) resume_addr);
7150
7151 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
7152}
7153
7154
7155/* Common copy routine for svc instruciton. */
7156
7157static int
7158install_svc (struct gdbarch *gdbarch, struct regcache *regs,
7159 struct displaced_step_closure *dsc)
7160{
7161 /* Preparation: none.
7162 Insn: unmodified svc.
7163 Cleanup: pc <- insn_addr + insn_size. */
7164
7165 /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
7166 instruction. */
7167 dsc->wrote_to_pc = 1;
7168
7169 /* Allow OS-specific code to override SVC handling. */
bd18283a
YQ
7170 if (dsc->u.svc.copy_svc_os)
7171 return dsc->u.svc.copy_svc_os (gdbarch, regs, dsc);
7172 else
7173 {
7174 dsc->cleanup = &cleanup_svc;
7175 return 0;
7176 }
34518530
YQ
7177}
7178
7179static int
7180arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
7181 struct regcache *regs, struct displaced_step_closure *dsc)
7182{
7183
7184 if (debug_displaced)
7185 fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.8lx\n",
7186 (unsigned long) insn);
7187
7188 dsc->modinsn[0] = insn;
7189
7190 return install_svc (gdbarch, regs, dsc);
7191}
7192
7193static int
7194thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
7195 struct regcache *regs, struct displaced_step_closure *dsc)
7196{
7197
7198 if (debug_displaced)
7199 fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.4x\n",
7200 insn);
bd18283a 7201
34518530
YQ
7202 dsc->modinsn[0] = insn;
7203
7204 return install_svc (gdbarch, regs, dsc);
cca44b1b
JB
7205}
7206
7207/* Copy undefined instructions. */
7208
7209static int
7ff120b4
YQ
7210arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
7211 struct displaced_step_closure *dsc)
cca44b1b
JB
7212{
7213 if (debug_displaced)
0963b4bd
MS
7214 fprintf_unfiltered (gdb_stdlog,
7215 "displaced: copying undefined insn %.8lx\n",
cca44b1b
JB
7216 (unsigned long) insn);
7217
7218 dsc->modinsn[0] = insn;
7219
7220 return 0;
7221}
7222
34518530
YQ
7223static int
7224thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
7225 struct displaced_step_closure *dsc)
7226{
7227
7228 if (debug_displaced)
7229 fprintf_unfiltered (gdb_stdlog, "displaced: copying undefined insn "
7230 "%.4x %.4x\n", (unsigned short) insn1,
7231 (unsigned short) insn2);
7232
7233 dsc->modinsn[0] = insn1;
7234 dsc->modinsn[1] = insn2;
7235 dsc->numinsns = 2;
7236
7237 return 0;
7238}
7239
cca44b1b
JB
7240/* Copy unpredictable instructions. */
7241
7242static int
7ff120b4
YQ
7243arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
7244 struct displaced_step_closure *dsc)
cca44b1b
JB
7245{
7246 if (debug_displaced)
7247 fprintf_unfiltered (gdb_stdlog, "displaced: copying unpredictable insn "
7248 "%.8lx\n", (unsigned long) insn);
7249
7250 dsc->modinsn[0] = insn;
7251
7252 return 0;
7253}
7254
7255/* The decode_* functions are instruction decoding helpers. They mostly follow
7256 the presentation in the ARM ARM. */
7257
7258static int
7ff120b4
YQ
7259arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
7260 struct regcache *regs,
7261 struct displaced_step_closure *dsc)
cca44b1b
JB
7262{
7263 unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
7264 unsigned int rn = bits (insn, 16, 19);
7265
7266 if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0xe) == 0x0)
7ff120b4 7267 return arm_copy_unmodified (gdbarch, insn, "cps", dsc);
cca44b1b 7268 else if (op1 == 0x10 && op2 == 0x0 && (rn & 0xe) == 0x1)
7ff120b4 7269 return arm_copy_unmodified (gdbarch, insn, "setend", dsc);
cca44b1b 7270 else if ((op1 & 0x60) == 0x20)
7ff120b4 7271 return arm_copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
cca44b1b 7272 else if ((op1 & 0x71) == 0x40)
7ff120b4
YQ
7273 return arm_copy_unmodified (gdbarch, insn, "neon elt/struct load/store",
7274 dsc);
cca44b1b 7275 else if ((op1 & 0x77) == 0x41)
7ff120b4 7276 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 7277 else if ((op1 & 0x77) == 0x45)
7ff120b4 7278 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pli. */
cca44b1b
JB
7279 else if ((op1 & 0x77) == 0x51)
7280 {
7281 if (rn != 0xf)
7ff120b4 7282 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b 7283 else
7ff120b4 7284 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7285 }
7286 else if ((op1 & 0x77) == 0x55)
7ff120b4 7287 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b
JB
7288 else if (op1 == 0x57)
7289 switch (op2)
7290 {
7ff120b4
YQ
7291 case 0x1: return arm_copy_unmodified (gdbarch, insn, "clrex", dsc);
7292 case 0x4: return arm_copy_unmodified (gdbarch, insn, "dsb", dsc);
7293 case 0x5: return arm_copy_unmodified (gdbarch, insn, "dmb", dsc);
7294 case 0x6: return arm_copy_unmodified (gdbarch, insn, "isb", dsc);
7295 default: return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7296 }
7297 else if ((op1 & 0x63) == 0x43)
7ff120b4 7298 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7299 else if ((op2 & 0x1) == 0x0)
7300 switch (op1 & ~0x80)
7301 {
7302 case 0x61:
7ff120b4 7303 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 7304 case 0x65:
7ff120b4 7305 return arm_copy_preload_reg (gdbarch, insn, regs, dsc); /* pli reg. */
cca44b1b
JB
7306 case 0x71: case 0x75:
7307 /* pld/pldw reg. */
7ff120b4 7308 return arm_copy_preload_reg (gdbarch, insn, regs, dsc);
cca44b1b 7309 case 0x63: case 0x67: case 0x73: case 0x77:
7ff120b4 7310 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b 7311 default:
7ff120b4 7312 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7313 }
7314 else
7ff120b4 7315 return arm_copy_undef (gdbarch, insn, dsc); /* Probably unreachable. */
cca44b1b
JB
7316}
7317
7318static int
7ff120b4
YQ
7319arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
7320 struct regcache *regs,
7321 struct displaced_step_closure *dsc)
cca44b1b
JB
7322{
7323 if (bit (insn, 27) == 0)
7ff120b4 7324 return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
cca44b1b
JB
7325 /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx. */
7326 else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
7327 {
7328 case 0x0: case 0x2:
7ff120b4 7329 return arm_copy_unmodified (gdbarch, insn, "srs", dsc);
cca44b1b
JB
7330
7331 case 0x1: case 0x3:
7ff120b4 7332 return arm_copy_unmodified (gdbarch, insn, "rfe", dsc);
cca44b1b
JB
7333
7334 case 0x4: case 0x5: case 0x6: case 0x7:
7ff120b4 7335 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b
JB
7336
7337 case 0x8:
7338 switch ((insn & 0xe00000) >> 21)
7339 {
7340 case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
7341 /* stc/stc2. */
7ff120b4 7342 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7343
7344 case 0x2:
7ff120b4 7345 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b
JB
7346
7347 default:
7ff120b4 7348 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7349 }
7350
7351 case 0x9:
7352 {
7353 int rn_f = (bits (insn, 16, 19) == 0xf);
7354 switch ((insn & 0xe00000) >> 21)
7355 {
7356 case 0x1: case 0x3:
7357 /* ldc/ldc2 imm (undefined for rn == pc). */
7ff120b4
YQ
7358 return rn_f ? arm_copy_undef (gdbarch, insn, dsc)
7359 : arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7360
7361 case 0x2:
7ff120b4 7362 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
7363
7364 case 0x4: case 0x5: case 0x6: case 0x7:
7365 /* ldc/ldc2 lit (undefined for rn != pc). */
7ff120b4
YQ
7366 return rn_f ? arm_copy_copro_load_store (gdbarch, insn, regs, dsc)
7367 : arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7368
7369 default:
7ff120b4 7370 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7371 }
7372 }
7373
7374 case 0xa:
7ff120b4 7375 return arm_copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
cca44b1b
JB
7376
7377 case 0xb:
7378 if (bits (insn, 16, 19) == 0xf)
7379 /* ldc/ldc2 lit. */
7ff120b4 7380 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 7381 else
7ff120b4 7382 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7383
7384 case 0xc:
7385 if (bit (insn, 4))
7ff120b4 7386 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 7387 else
7ff120b4 7388 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7389
7390 case 0xd:
7391 if (bit (insn, 4))
7ff120b4 7392 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 7393 else
7ff120b4 7394 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7395
7396 default:
7ff120b4 7397 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7398 }
7399}
7400
7401/* Decode miscellaneous instructions in dp/misc encoding space. */
7402
7403static int
7ff120b4
YQ
7404arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
7405 struct regcache *regs,
7406 struct displaced_step_closure *dsc)
cca44b1b
JB
7407{
7408 unsigned int op2 = bits (insn, 4, 6);
7409 unsigned int op = bits (insn, 21, 22);
7410 unsigned int op1 = bits (insn, 16, 19);
7411
7412 switch (op2)
7413 {
7414 case 0x0:
7ff120b4 7415 return arm_copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
cca44b1b
JB
7416
7417 case 0x1:
7418 if (op == 0x1) /* bx. */
7ff120b4 7419 return arm_copy_bx_blx_reg (gdbarch, insn, regs, dsc);
cca44b1b 7420 else if (op == 0x3)
7ff120b4 7421 return arm_copy_unmodified (gdbarch, insn, "clz", dsc);
cca44b1b 7422 else
7ff120b4 7423 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7424
7425 case 0x2:
7426 if (op == 0x1)
7427 /* Not really supported. */
7ff120b4 7428 return arm_copy_unmodified (gdbarch, insn, "bxj", dsc);
cca44b1b 7429 else
7ff120b4 7430 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7431
7432 case 0x3:
7433 if (op == 0x1)
7ff120b4 7434 return arm_copy_bx_blx_reg (gdbarch, insn,
0963b4bd 7435 regs, dsc); /* blx register. */
cca44b1b 7436 else
7ff120b4 7437 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7438
7439 case 0x5:
7ff120b4 7440 return arm_copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
cca44b1b
JB
7441
7442 case 0x7:
7443 if (op == 0x1)
7ff120b4 7444 return arm_copy_unmodified (gdbarch, insn, "bkpt", dsc);
cca44b1b
JB
7445 else if (op == 0x3)
7446 /* Not really supported. */
7ff120b4 7447 return arm_copy_unmodified (gdbarch, insn, "smc", dsc);
cca44b1b
JB
7448
7449 default:
7ff120b4 7450 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7451 }
7452}
7453
7454static int
7ff120b4
YQ
7455arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
7456 struct regcache *regs,
7457 struct displaced_step_closure *dsc)
cca44b1b
JB
7458{
7459 if (bit (insn, 25))
7460 switch (bits (insn, 20, 24))
7461 {
7462 case 0x10:
7ff120b4 7463 return arm_copy_unmodified (gdbarch, insn, "movw", dsc);
cca44b1b
JB
7464
7465 case 0x14:
7ff120b4 7466 return arm_copy_unmodified (gdbarch, insn, "movt", dsc);
cca44b1b
JB
7467
7468 case 0x12: case 0x16:
7ff120b4 7469 return arm_copy_unmodified (gdbarch, insn, "msr imm", dsc);
cca44b1b
JB
7470
7471 default:
7ff120b4 7472 return arm_copy_alu_imm (gdbarch, insn, regs, dsc);
cca44b1b
JB
7473 }
7474 else
7475 {
7476 uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
7477
7478 if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
7ff120b4 7479 return arm_copy_alu_reg (gdbarch, insn, regs, dsc);
cca44b1b 7480 else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
7ff120b4 7481 return arm_copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
cca44b1b 7482 else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
7ff120b4 7483 return arm_decode_miscellaneous (gdbarch, insn, regs, dsc);
cca44b1b 7484 else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
7ff120b4 7485 return arm_copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
cca44b1b 7486 else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
7ff120b4 7487 return arm_copy_unmodified (gdbarch, insn, "mul/mla", dsc);
cca44b1b 7488 else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
7ff120b4 7489 return arm_copy_unmodified (gdbarch, insn, "synch", dsc);
cca44b1b
JB
7490 else if (op2 == 0xb || (op2 & 0xd) == 0xd)
7491 /* 2nd arg means "unpriveleged". */
7ff120b4
YQ
7492 return arm_copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
7493 dsc);
cca44b1b
JB
7494 }
7495
7496 /* Should be unreachable. */
7497 return 1;
7498}
7499
7500static int
7ff120b4
YQ
7501arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
7502 struct regcache *regs,
7503 struct displaced_step_closure *dsc)
cca44b1b
JB
7504{
7505 int a = bit (insn, 25), b = bit (insn, 4);
7506 uint32_t op1 = bits (insn, 20, 24);
7507 int rn_f = bits (insn, 16, 19) == 0xf;
7508
7509 if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
7510 || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
0f6f04ba 7511 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 0);
cca44b1b
JB
7512 else if ((!a && (op1 & 0x17) == 0x02)
7513 || (a && (op1 & 0x17) == 0x02 && !b))
0f6f04ba 7514 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 1);
cca44b1b
JB
7515 else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
7516 || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
0f6f04ba 7517 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 0);
cca44b1b
JB
7518 else if ((!a && (op1 & 0x17) == 0x03)
7519 || (a && (op1 & 0x17) == 0x03 && !b))
0f6f04ba 7520 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 1);
cca44b1b
JB
7521 else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
7522 || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
7ff120b4 7523 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
cca44b1b
JB
7524 else if ((!a && (op1 & 0x17) == 0x06)
7525 || (a && (op1 & 0x17) == 0x06 && !b))
7ff120b4 7526 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
cca44b1b
JB
7527 else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
7528 || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
7ff120b4 7529 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
cca44b1b
JB
7530 else if ((!a && (op1 & 0x17) == 0x07)
7531 || (a && (op1 & 0x17) == 0x07 && !b))
7ff120b4 7532 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
cca44b1b
JB
7533
7534 /* Should be unreachable. */
7535 return 1;
7536}
7537
7538static int
7ff120b4
YQ
7539arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
7540 struct displaced_step_closure *dsc)
cca44b1b
JB
7541{
7542 switch (bits (insn, 20, 24))
7543 {
7544 case 0x00: case 0x01: case 0x02: case 0x03:
7ff120b4 7545 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
cca44b1b
JB
7546
7547 case 0x04: case 0x05: case 0x06: case 0x07:
7ff120b4 7548 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
cca44b1b
JB
7549
7550 case 0x08: case 0x09: case 0x0a: case 0x0b:
7551 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
7ff120b4 7552 return arm_copy_unmodified (gdbarch, insn,
cca44b1b
JB
7553 "decode/pack/unpack/saturate/reverse", dsc);
7554
7555 case 0x18:
7556 if (bits (insn, 5, 7) == 0) /* op2. */
7557 {
7558 if (bits (insn, 12, 15) == 0xf)
7ff120b4 7559 return arm_copy_unmodified (gdbarch, insn, "usad8", dsc);
cca44b1b 7560 else
7ff120b4 7561 return arm_copy_unmodified (gdbarch, insn, "usada8", dsc);
cca44b1b
JB
7562 }
7563 else
7ff120b4 7564 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7565
7566 case 0x1a: case 0x1b:
7567 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 7568 return arm_copy_unmodified (gdbarch, insn, "sbfx", dsc);
cca44b1b 7569 else
7ff120b4 7570 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7571
7572 case 0x1c: case 0x1d:
7573 if (bits (insn, 5, 6) == 0x0) /* op2[1:0]. */
7574 {
7575 if (bits (insn, 0, 3) == 0xf)
7ff120b4 7576 return arm_copy_unmodified (gdbarch, insn, "bfc", dsc);
cca44b1b 7577 else
7ff120b4 7578 return arm_copy_unmodified (gdbarch, insn, "bfi", dsc);
cca44b1b
JB
7579 }
7580 else
7ff120b4 7581 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7582
7583 case 0x1e: case 0x1f:
7584 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 7585 return arm_copy_unmodified (gdbarch, insn, "ubfx", dsc);
cca44b1b 7586 else
7ff120b4 7587 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7588 }
7589
7590 /* Should be unreachable. */
7591 return 1;
7592}
7593
7594static int
7ff120b4
YQ
7595arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, int32_t insn,
7596 struct regcache *regs,
7597 struct displaced_step_closure *dsc)
cca44b1b
JB
7598{
7599 if (bit (insn, 25))
7ff120b4 7600 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b 7601 else
7ff120b4 7602 return arm_copy_block_xfer (gdbarch, insn, regs, dsc);
cca44b1b
JB
7603}
7604
7605static int
7ff120b4
YQ
7606arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
7607 struct regcache *regs,
7608 struct displaced_step_closure *dsc)
cca44b1b
JB
7609{
7610 unsigned int opcode = bits (insn, 20, 24);
7611
7612 switch (opcode)
7613 {
7614 case 0x04: case 0x05: /* VFP/Neon mrrc/mcrr. */
7ff120b4 7615 return arm_copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
cca44b1b
JB
7616
7617 case 0x08: case 0x0a: case 0x0c: case 0x0e:
7618 case 0x12: case 0x16:
7ff120b4 7619 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
cca44b1b
JB
7620
7621 case 0x09: case 0x0b: case 0x0d: case 0x0f:
7622 case 0x13: case 0x17:
7ff120b4 7623 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
cca44b1b
JB
7624
7625 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7626 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7627 /* Note: no writeback for these instructions. Bit 25 will always be
7628 zero though (via caller), so the following works OK. */
7ff120b4 7629 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7630 }
7631
7632 /* Should be unreachable. */
7633 return 1;
7634}
7635
34518530
YQ
7636/* Decode shifted register instructions. */
7637
7638static int
7639thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
7640 uint16_t insn2, struct regcache *regs,
7641 struct displaced_step_closure *dsc)
7642{
7643 /* PC is only allowed to be used in instruction MOV. */
7644
7645 unsigned int op = bits (insn1, 5, 8);
7646 unsigned int rn = bits (insn1, 0, 3);
7647
7648 if (op == 0x2 && rn == 0xf) /* MOV */
7649 return thumb2_copy_alu_imm (gdbarch, insn1, insn2, regs, dsc);
7650 else
7651 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7652 "dp (shift reg)", dsc);
7653}
7654
7655
7656/* Decode extension register load/store. Exactly the same as
7657 arm_decode_ext_reg_ld_st. */
7658
7659static int
7660thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
7661 uint16_t insn2, struct regcache *regs,
7662 struct displaced_step_closure *dsc)
7663{
7664 unsigned int opcode = bits (insn1, 4, 8);
7665
7666 switch (opcode)
7667 {
7668 case 0x04: case 0x05:
7669 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7670 "vfp/neon vmov", dsc);
7671
7672 case 0x08: case 0x0c: /* 01x00 */
7673 case 0x0a: case 0x0e: /* 01x10 */
7674 case 0x12: case 0x16: /* 10x10 */
7675 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7676 "vfp/neon vstm/vpush", dsc);
7677
7678 case 0x09: case 0x0d: /* 01x01 */
7679 case 0x0b: case 0x0f: /* 01x11 */
7680 case 0x13: case 0x17: /* 10x11 */
7681 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7682 "vfp/neon vldm/vpop", dsc);
7683
7684 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7685 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7686 "vstr", dsc);
7687 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7688 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2, regs, dsc);
7689 }
7690
7691 /* Should be unreachable. */
7692 return 1;
7693}
7694
cca44b1b 7695static int
7ff120b4
YQ
7696arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn, CORE_ADDR to,
7697 struct regcache *regs, struct displaced_step_closure *dsc)
cca44b1b
JB
7698{
7699 unsigned int op1 = bits (insn, 20, 25);
7700 int op = bit (insn, 4);
7701 unsigned int coproc = bits (insn, 8, 11);
7702 unsigned int rn = bits (insn, 16, 19);
7703
7704 if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
7ff120b4 7705 return arm_decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
cca44b1b
JB
7706 else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
7707 && (coproc & 0xe) != 0xa)
7708 /* stc/stc2. */
7ff120b4 7709 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7710 else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
7711 && (coproc & 0xe) != 0xa)
7712 /* ldc/ldc2 imm/lit. */
7ff120b4 7713 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 7714 else if ((op1 & 0x3e) == 0x00)
7ff120b4 7715 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b 7716 else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
7ff120b4 7717 return arm_copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
cca44b1b 7718 else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
7ff120b4 7719 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b 7720 else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
7ff120b4 7721 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
7722 else if ((op1 & 0x30) == 0x20 && !op)
7723 {
7724 if ((coproc & 0xe) == 0xa)
7ff120b4 7725 return arm_copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
cca44b1b 7726 else
7ff120b4 7727 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7728 }
7729 else if ((op1 & 0x30) == 0x20 && op)
7ff120b4 7730 return arm_copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
cca44b1b 7731 else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
7ff120b4 7732 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 7733 else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
7ff120b4 7734 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 7735 else if ((op1 & 0x30) == 0x30)
7ff120b4 7736 return arm_copy_svc (gdbarch, insn, regs, dsc);
cca44b1b 7737 else
7ff120b4 7738 return arm_copy_undef (gdbarch, insn, dsc); /* Possibly unreachable. */
cca44b1b
JB
7739}
7740
34518530
YQ
7741static int
7742thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
7743 uint16_t insn2, struct regcache *regs,
7744 struct displaced_step_closure *dsc)
7745{
7746 unsigned int coproc = bits (insn2, 8, 11);
7747 unsigned int op1 = bits (insn1, 4, 9);
7748 unsigned int bit_5_8 = bits (insn1, 5, 8);
7749 unsigned int bit_9 = bit (insn1, 9);
7750 unsigned int bit_4 = bit (insn1, 4);
7751 unsigned int rn = bits (insn1, 0, 3);
7752
7753 if (bit_9 == 0)
7754 {
7755 if (bit_5_8 == 2)
7756 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7757 "neon 64bit xfer/mrrc/mrrc2/mcrr/mcrr2",
7758 dsc);
7759 else if (bit_5_8 == 0) /* UNDEFINED. */
7760 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
7761 else
7762 {
7763 /*coproc is 101x. SIMD/VFP, ext registers load/store. */
7764 if ((coproc & 0xe) == 0xa)
7765 return thumb2_decode_ext_reg_ld_st (gdbarch, insn1, insn2, regs,
7766 dsc);
7767 else /* coproc is not 101x. */
7768 {
7769 if (bit_4 == 0) /* STC/STC2. */
7770 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7771 "stc/stc2", dsc);
7772 else /* LDC/LDC2 {literal, immeidate}. */
7773 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2,
7774 regs, dsc);
7775 }
7776 }
7777 }
7778 else
7779 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "coproc", dsc);
7780
7781 return 0;
7782}
7783
7784static void
7785install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
7786 struct displaced_step_closure *dsc, int rd)
7787{
7788 /* ADR Rd, #imm
7789
7790 Rewrite as:
7791
7792 Preparation: Rd <- PC
7793 Insn: ADD Rd, #imm
7794 Cleanup: Null.
7795 */
7796
7797 /* Rd <- PC */
7798 int val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
7799 displaced_write_reg (regs, dsc, rd, val, CANNOT_WRITE_PC);
7800}
7801
7802static int
7803thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
7804 struct displaced_step_closure *dsc,
7805 int rd, unsigned int imm)
7806{
7807
7808 /* Encoding T2: ADDS Rd, #imm */
7809 dsc->modinsn[0] = (0x3000 | (rd << 8) | imm);
7810
7811 install_pc_relative (gdbarch, regs, dsc, rd);
7812
7813 return 0;
7814}
7815
7816static int
7817thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
7818 struct regcache *regs,
7819 struct displaced_step_closure *dsc)
7820{
7821 unsigned int rd = bits (insn, 8, 10);
7822 unsigned int imm8 = bits (insn, 0, 7);
7823
7824 if (debug_displaced)
7825 fprintf_unfiltered (gdb_stdlog,
7826 "displaced: copying thumb adr r%d, #%d insn %.4x\n",
7827 rd, imm8, insn);
7828
7829 return thumb_copy_pc_relative_16bit (gdbarch, regs, dsc, rd, imm8);
7830}
7831
7832static int
7833thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
7834 uint16_t insn2, struct regcache *regs,
7835 struct displaced_step_closure *dsc)
7836{
7837 unsigned int rd = bits (insn2, 8, 11);
7838 /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
7839 extract raw immediate encoding rather than computing immediate. When
7840 generating ADD or SUB instruction, we can simply perform OR operation to
7841 set immediate into ADD. */
7842 unsigned int imm_3_8 = insn2 & 0x70ff;
7843 unsigned int imm_i = insn1 & 0x0400; /* Clear all bits except bit 10. */
7844
7845 if (debug_displaced)
7846 fprintf_unfiltered (gdb_stdlog,
7847 "displaced: copying thumb adr r%d, #%d:%d insn %.4x%.4x\n",
7848 rd, imm_i, imm_3_8, insn1, insn2);
7849
7850 if (bit (insn1, 7)) /* Encoding T2 */
7851 {
7852 /* Encoding T3: SUB Rd, Rd, #imm */
7853 dsc->modinsn[0] = (0xf1a0 | rd | imm_i);
7854 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
7855 }
7856 else /* Encoding T3 */
7857 {
7858 /* Encoding T3: ADD Rd, Rd, #imm */
7859 dsc->modinsn[0] = (0xf100 | rd | imm_i);
7860 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
7861 }
7862 dsc->numinsns = 2;
7863
7864 install_pc_relative (gdbarch, regs, dsc, rd);
7865
7866 return 0;
7867}
7868
7869static int
7870thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, unsigned short insn1,
7871 struct regcache *regs,
7872 struct displaced_step_closure *dsc)
7873{
7874 unsigned int rt = bits (insn1, 8, 10);
7875 unsigned int pc;
7876 int imm8 = (bits (insn1, 0, 7) << 2);
7877 CORE_ADDR from = dsc->insn_addr;
7878
7879 /* LDR Rd, #imm8
7880
7881 Rwrite as:
7882
7883 Preparation: tmp0 <- R0, tmp2 <- R2, tmp3 <- R3, R2 <- PC, R3 <- #imm8;
7884
7885 Insn: LDR R0, [R2, R3];
7886 Cleanup: R2 <- tmp2, R3 <- tmp3, Rd <- R0, R0 <- tmp0 */
7887
7888 if (debug_displaced)
7889 fprintf_unfiltered (gdb_stdlog,
7890 "displaced: copying thumb ldr r%d [pc #%d]\n"
7891 , rt, imm8);
7892
7893 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
7894 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
7895 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
7896 pc = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
7897 /* The assembler calculates the required value of the offset from the
7898 Align(PC,4) value of this instruction to the label. */
7899 pc = pc & 0xfffffffc;
7900
7901 displaced_write_reg (regs, dsc, 2, pc, CANNOT_WRITE_PC);
7902 displaced_write_reg (regs, dsc, 3, imm8, CANNOT_WRITE_PC);
7903
7904 dsc->rd = rt;
7905 dsc->u.ldst.xfersize = 4;
7906 dsc->u.ldst.rn = 0;
7907 dsc->u.ldst.immed = 0;
7908 dsc->u.ldst.writeback = 0;
7909 dsc->u.ldst.restore_r4 = 0;
7910
7911 dsc->modinsn[0] = 0x58d0; /* ldr r0, [r2, r3]*/
7912
7913 dsc->cleanup = &cleanup_load;
7914
7915 return 0;
7916}
7917
7918/* Copy Thumb cbnz/cbz insruction. */
7919
7920static int
7921thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
7922 struct regcache *regs,
7923 struct displaced_step_closure *dsc)
7924{
7925 int non_zero = bit (insn1, 11);
7926 unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
7927 CORE_ADDR from = dsc->insn_addr;
7928 int rn = bits (insn1, 0, 2);
7929 int rn_val = displaced_read_reg (regs, dsc, rn);
7930
7931 dsc->u.branch.cond = (rn_val && non_zero) || (!rn_val && !non_zero);
7932 /* CBNZ and CBZ do not affect the condition flags. If condition is true,
7933 set it INST_AL, so cleanup_branch will know branch is taken, otherwise,
7934 condition is false, let it be, cleanup_branch will do nothing. */
7935 if (dsc->u.branch.cond)
7936 {
7937 dsc->u.branch.cond = INST_AL;
7938 dsc->u.branch.dest = from + 4 + imm5;
7939 }
7940 else
7941 dsc->u.branch.dest = from + 2;
7942
7943 dsc->u.branch.link = 0;
7944 dsc->u.branch.exchange = 0;
7945
7946 if (debug_displaced)
7947 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s [r%d = 0x%x]"
7948 " insn %.4x to %.8lx\n", non_zero ? "cbnz" : "cbz",
7949 rn, rn_val, insn1, dsc->u.branch.dest);
7950
7951 dsc->modinsn[0] = THUMB_NOP;
7952
7953 dsc->cleanup = &cleanup_branch;
7954 return 0;
7955}
7956
7957/* Copy Table Branch Byte/Halfword */
7958static int
7959thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
7960 uint16_t insn2, struct regcache *regs,
7961 struct displaced_step_closure *dsc)
7962{
7963 ULONGEST rn_val, rm_val;
7964 int is_tbh = bit (insn2, 4);
7965 CORE_ADDR halfwords = 0;
7966 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7967
7968 rn_val = displaced_read_reg (regs, dsc, bits (insn1, 0, 3));
7969 rm_val = displaced_read_reg (regs, dsc, bits (insn2, 0, 3));
7970
7971 if (is_tbh)
7972 {
7973 gdb_byte buf[2];
7974
7975 target_read_memory (rn_val + 2 * rm_val, buf, 2);
7976 halfwords = extract_unsigned_integer (buf, 2, byte_order);
7977 }
7978 else
7979 {
7980 gdb_byte buf[1];
7981
7982 target_read_memory (rn_val + rm_val, buf, 1);
7983 halfwords = extract_unsigned_integer (buf, 1, byte_order);
7984 }
7985
7986 if (debug_displaced)
7987 fprintf_unfiltered (gdb_stdlog, "displaced: %s base 0x%x offset 0x%x"
7988 " offset 0x%x\n", is_tbh ? "tbh" : "tbb",
7989 (unsigned int) rn_val, (unsigned int) rm_val,
7990 (unsigned int) halfwords);
7991
7992 dsc->u.branch.cond = INST_AL;
7993 dsc->u.branch.link = 0;
7994 dsc->u.branch.exchange = 0;
7995 dsc->u.branch.dest = dsc->insn_addr + 4 + 2 * halfwords;
7996
7997 dsc->cleanup = &cleanup_branch;
7998
7999 return 0;
8000}
8001
8002static void
8003cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
8004 struct displaced_step_closure *dsc)
8005{
8006 /* PC <- r7 */
8007 int val = displaced_read_reg (regs, dsc, 7);
8008 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, val, BX_WRITE_PC);
8009
8010 /* r7 <- r8 */
8011 val = displaced_read_reg (regs, dsc, 8);
8012 displaced_write_reg (regs, dsc, 7, val, CANNOT_WRITE_PC);
8013
8014 /* r8 <- tmp[0] */
8015 displaced_write_reg (regs, dsc, 8, dsc->tmp[0], CANNOT_WRITE_PC);
8016
8017}
8018
8019static int
8020thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, unsigned short insn1,
8021 struct regcache *regs,
8022 struct displaced_step_closure *dsc)
8023{
8024 dsc->u.block.regmask = insn1 & 0x00ff;
8025
8026 /* Rewrite instruction: POP {rX, rY, ...,rZ, PC}
8027 to :
8028
8029 (1) register list is full, that is, r0-r7 are used.
8030 Prepare: tmp[0] <- r8
8031
8032 POP {r0, r1, ...., r6, r7}; remove PC from reglist
8033 MOV r8, r7; Move value of r7 to r8;
8034 POP {r7}; Store PC value into r7.
8035
8036 Cleanup: PC <- r7, r7 <- r8, r8 <-tmp[0]
8037
8038 (2) register list is not full, supposing there are N registers in
8039 register list (except PC, 0 <= N <= 7).
8040 Prepare: for each i, 0 - N, tmp[i] <- ri.
8041
8042 POP {r0, r1, ...., rN};
8043
8044 Cleanup: Set registers in original reglist from r0 - rN. Restore r0 - rN
8045 from tmp[] properly.
8046 */
8047 if (debug_displaced)
8048 fprintf_unfiltered (gdb_stdlog,
8049 "displaced: copying thumb pop {%.8x, pc} insn %.4x\n",
8050 dsc->u.block.regmask, insn1);
8051
8052 if (dsc->u.block.regmask == 0xff)
8053 {
8054 dsc->tmp[0] = displaced_read_reg (regs, dsc, 8);
8055
8056 dsc->modinsn[0] = (insn1 & 0xfeff); /* POP {r0,r1,...,r6, r7} */
8057 dsc->modinsn[1] = 0x46b8; /* MOV r8, r7 */
8058 dsc->modinsn[2] = 0xbc80; /* POP {r7} */
8059
8060 dsc->numinsns = 3;
8061 dsc->cleanup = &cleanup_pop_pc_16bit_all;
8062 }
8063 else
8064 {
8065 unsigned int num_in_list = bitcount (dsc->u.block.regmask);
8066 unsigned int new_regmask, bit = 1;
8067 unsigned int to = 0, from = 0, i, new_rn;
8068
8069 for (i = 0; i < num_in_list + 1; i++)
8070 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
8071
8072 new_regmask = (1 << (num_in_list + 1)) - 1;
8073
8074 if (debug_displaced)
8075 fprintf_unfiltered (gdb_stdlog, _("displaced: POP "
8076 "{..., pc}: original reg list %.4x,"
8077 " modified list %.4x\n"),
8078 (int) dsc->u.block.regmask, new_regmask);
8079
8080 dsc->u.block.regmask |= 0x8000;
8081 dsc->u.block.writeback = 0;
8082 dsc->u.block.cond = INST_AL;
8083
8084 dsc->modinsn[0] = (insn1 & ~0x1ff) | (new_regmask & 0xff);
8085
8086 dsc->cleanup = &cleanup_block_load_pc;
8087 }
8088
8089 return 0;
8090}
8091
8092static void
8093thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8094 struct regcache *regs,
8095 struct displaced_step_closure *dsc)
8096{
8097 unsigned short op_bit_12_15 = bits (insn1, 12, 15);
8098 unsigned short op_bit_10_11 = bits (insn1, 10, 11);
8099 int err = 0;
8100
8101 /* 16-bit thumb instructions. */
8102 switch (op_bit_12_15)
8103 {
8104 /* Shift (imme), add, subtract, move and compare. */
8105 case 0: case 1: case 2: case 3:
8106 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8107 "shift/add/sub/mov/cmp",
8108 dsc);
8109 break;
8110 case 4:
8111 switch (op_bit_10_11)
8112 {
8113 case 0: /* Data-processing */
8114 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8115 "data-processing",
8116 dsc);
8117 break;
8118 case 1: /* Special data instructions and branch and exchange. */
8119 {
8120 unsigned short op = bits (insn1, 7, 9);
8121 if (op == 6 || op == 7) /* BX or BLX */
8122 err = thumb_copy_bx_blx_reg (gdbarch, insn1, regs, dsc);
8123 else if (bits (insn1, 6, 7) != 0) /* ADD/MOV/CMP high registers. */
8124 err = thumb_copy_alu_reg (gdbarch, insn1, regs, dsc);
8125 else
8126 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "special data",
8127 dsc);
8128 }
8129 break;
8130 default: /* LDR (literal) */
8131 err = thumb_copy_16bit_ldr_literal (gdbarch, insn1, regs, dsc);
8132 }
8133 break;
8134 case 5: case 6: case 7: case 8: case 9: /* Load/Store single data item */
8135 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldr/str", dsc);
8136 break;
8137 case 10:
8138 if (op_bit_10_11 < 2) /* Generate PC-relative address */
8139 err = thumb_decode_pc_relative_16bit (gdbarch, insn1, regs, dsc);
8140 else /* Generate SP-relative address */
8141 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "sp-relative", dsc);
8142 break;
8143 case 11: /* Misc 16-bit instructions */
8144 {
8145 switch (bits (insn1, 8, 11))
8146 {
8147 case 1: case 3: case 9: case 11: /* CBNZ, CBZ */
8148 err = thumb_copy_cbnz_cbz (gdbarch, insn1, regs, dsc);
8149 break;
8150 case 12: case 13: /* POP */
8151 if (bit (insn1, 8)) /* PC is in register list. */
8152 err = thumb_copy_pop_pc_16bit (gdbarch, insn1, regs, dsc);
8153 else
8154 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "pop", dsc);
8155 break;
8156 case 15: /* If-Then, and hints */
8157 if (bits (insn1, 0, 3))
8158 /* If-Then makes up to four following instructions conditional.
8159 IT instruction itself is not conditional, so handle it as a
8160 common unmodified instruction. */
8161 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "If-Then",
8162 dsc);
8163 else
8164 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "hints", dsc);
8165 break;
8166 default:
8167 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "misc", dsc);
8168 }
8169 }
8170 break;
8171 case 12:
8172 if (op_bit_10_11 < 2) /* Store multiple registers */
8173 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "stm", dsc);
8174 else /* Load multiple registers */
8175 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldm", dsc);
8176 break;
8177 case 13: /* Conditional branch and supervisor call */
8178 if (bits (insn1, 9, 11) != 7) /* conditional branch */
8179 err = thumb_copy_b (gdbarch, insn1, dsc);
8180 else
8181 err = thumb_copy_svc (gdbarch, insn1, regs, dsc);
8182 break;
8183 case 14: /* Unconditional branch */
8184 err = thumb_copy_b (gdbarch, insn1, dsc);
8185 break;
8186 default:
8187 err = 1;
8188 }
8189
8190 if (err)
8191 internal_error (__FILE__, __LINE__,
8192 _("thumb_process_displaced_16bit_insn: Instruction decode error"));
8193}
8194
8195static int
8196decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
8197 uint16_t insn1, uint16_t insn2,
8198 struct regcache *regs,
8199 struct displaced_step_closure *dsc)
8200{
8201 int rt = bits (insn2, 12, 15);
8202 int rn = bits (insn1, 0, 3);
8203 int op1 = bits (insn1, 7, 8);
8204 int err = 0;
8205
8206 switch (bits (insn1, 5, 6))
8207 {
8208 case 0: /* Load byte and memory hints */
8209 if (rt == 0xf) /* PLD/PLI */
8210 {
8211 if (rn == 0xf)
8212 /* PLD literal or Encoding T3 of PLI(immediate, literal). */
8213 return thumb2_copy_preload (gdbarch, insn1, insn2, regs, dsc);
8214 else
8215 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8216 "pli/pld", dsc);
8217 }
8218 else
8219 {
8220 if (rn == 0xf) /* LDRB/LDRSB (literal) */
8221 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8222 1);
8223 else
8224 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8225 "ldrb{reg, immediate}/ldrbt",
8226 dsc);
8227 }
8228
8229 break;
8230 case 1: /* Load halfword and memory hints. */
8231 if (rt == 0xf) /* PLD{W} and Unalloc memory hint. */
8232 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8233 "pld/unalloc memhint", dsc);
8234 else
8235 {
8236 if (rn == 0xf)
8237 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8238 2);
8239 else
8240 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8241 "ldrh/ldrht", dsc);
8242 }
8243 break;
8244 case 2: /* Load word */
8245 {
8246 int insn2_bit_8_11 = bits (insn2, 8, 11);
8247
8248 if (rn == 0xf)
8249 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc, 4);
8250 else if (op1 == 0x1) /* Encoding T3 */
8251 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs, dsc,
8252 0, 1);
8253 else /* op1 == 0x0 */
8254 {
8255 if (insn2_bit_8_11 == 0xc || (insn2_bit_8_11 & 0x9) == 0x9)
8256 /* LDR (immediate) */
8257 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8258 dsc, bit (insn2, 8), 1);
8259 else if (insn2_bit_8_11 == 0xe) /* LDRT */
8260 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8261 "ldrt", dsc);
8262 else
8263 /* LDR (register) */
8264 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8265 dsc, 0, 0);
8266 }
8267 break;
8268 }
8269 default:
8270 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
8271 break;
8272 }
8273 return 0;
8274}
8275
8276static void
8277thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8278 uint16_t insn2, struct regcache *regs,
8279 struct displaced_step_closure *dsc)
8280{
8281 int err = 0;
8282 unsigned short op = bit (insn2, 15);
8283 unsigned int op1 = bits (insn1, 11, 12);
8284
8285 switch (op1)
8286 {
8287 case 1:
8288 {
8289 switch (bits (insn1, 9, 10))
8290 {
8291 case 0:
8292 if (bit (insn1, 6))
8293 {
8294 /* Load/store {dual, execlusive}, table branch. */
8295 if (bits (insn1, 7, 8) == 1 && bits (insn1, 4, 5) == 1
8296 && bits (insn2, 5, 7) == 0)
8297 err = thumb2_copy_table_branch (gdbarch, insn1, insn2, regs,
8298 dsc);
8299 else
8300 /* PC is not allowed to use in load/store {dual, exclusive}
8301 instructions. */
8302 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8303 "load/store dual/ex", dsc);
8304 }
8305 else /* load/store multiple */
8306 {
8307 switch (bits (insn1, 7, 8))
8308 {
8309 case 0: case 3: /* SRS, RFE */
8310 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8311 "srs/rfe", dsc);
8312 break;
8313 case 1: case 2: /* LDM/STM/PUSH/POP */
8314 err = thumb2_copy_block_xfer (gdbarch, insn1, insn2, regs, dsc);
8315 break;
8316 }
8317 }
8318 break;
8319
8320 case 1:
8321 /* Data-processing (shift register). */
8322 err = thumb2_decode_dp_shift_reg (gdbarch, insn1, insn2, regs,
8323 dsc);
8324 break;
8325 default: /* Coprocessor instructions. */
8326 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8327 break;
8328 }
8329 break;
8330 }
8331 case 2: /* op1 = 2 */
8332 if (op) /* Branch and misc control. */
8333 {
8334 if (bit (insn2, 14) /* BLX/BL */
8335 || bit (insn2, 12) /* Unconditional branch */
8336 || (bits (insn1, 7, 9) != 0x7)) /* Conditional branch */
8337 err = thumb2_copy_b_bl_blx (gdbarch, insn1, insn2, regs, dsc);
8338 else
8339 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8340 "misc ctrl", dsc);
8341 }
8342 else
8343 {
8344 if (bit (insn1, 9)) /* Data processing (plain binary imm). */
8345 {
8346 int op = bits (insn1, 4, 8);
8347 int rn = bits (insn1, 0, 3);
8348 if ((op == 0 || op == 0xa) && rn == 0xf)
8349 err = thumb_copy_pc_relative_32bit (gdbarch, insn1, insn2,
8350 regs, dsc);
8351 else
8352 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8353 "dp/pb", dsc);
8354 }
8355 else /* Data processing (modified immeidate) */
8356 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8357 "dp/mi", dsc);
8358 }
8359 break;
8360 case 3: /* op1 = 3 */
8361 switch (bits (insn1, 9, 10))
8362 {
8363 case 0:
8364 if (bit (insn1, 4))
8365 err = decode_thumb_32bit_ld_mem_hints (gdbarch, insn1, insn2,
8366 regs, dsc);
8367 else /* NEON Load/Store and Store single data item */
8368 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8369 "neon elt/struct load/store",
8370 dsc);
8371 break;
8372 case 1: /* op1 = 3, bits (9, 10) == 1 */
8373 switch (bits (insn1, 7, 8))
8374 {
8375 case 0: case 1: /* Data processing (register) */
8376 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8377 "dp(reg)", dsc);
8378 break;
8379 case 2: /* Multiply and absolute difference */
8380 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8381 "mul/mua/diff", dsc);
8382 break;
8383 case 3: /* Long multiply and divide */
8384 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8385 "lmul/lmua", dsc);
8386 break;
8387 }
8388 break;
8389 default: /* Coprocessor instructions */
8390 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8391 break;
8392 }
8393 break;
8394 default:
8395 err = 1;
8396 }
8397
8398 if (err)
8399 internal_error (__FILE__, __LINE__,
8400 _("thumb_process_displaced_32bit_insn: Instruction decode error"));
8401
8402}
8403
b434a28f
YQ
8404static void
8405thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8406 CORE_ADDR to, struct regcache *regs,
8407 struct displaced_step_closure *dsc)
8408{
34518530
YQ
8409 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8410 uint16_t insn1
8411 = read_memory_unsigned_integer (from, 2, byte_order_for_code);
8412
8413 if (debug_displaced)
8414 fprintf_unfiltered (gdb_stdlog, "displaced: process thumb insn %.4x "
8415 "at %.8lx\n", insn1, (unsigned long) from);
8416
8417 dsc->is_thumb = 1;
8418 dsc->insn_size = thumb_insn_size (insn1);
8419 if (thumb_insn_size (insn1) == 4)
8420 {
8421 uint16_t insn2
8422 = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
8423 thumb_process_displaced_32bit_insn (gdbarch, insn1, insn2, regs, dsc);
8424 }
8425 else
8426 thumb_process_displaced_16bit_insn (gdbarch, insn1, regs, dsc);
b434a28f
YQ
8427}
8428
cca44b1b 8429void
b434a28f
YQ
8430arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8431 CORE_ADDR to, struct regcache *regs,
cca44b1b
JB
8432 struct displaced_step_closure *dsc)
8433{
8434 int err = 0;
b434a28f
YQ
8435 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8436 uint32_t insn;
cca44b1b
JB
8437
8438 /* Most displaced instructions use a 1-instruction scratch space, so set this
8439 here and override below if/when necessary. */
8440 dsc->numinsns = 1;
8441 dsc->insn_addr = from;
8442 dsc->scratch_base = to;
8443 dsc->cleanup = NULL;
8444 dsc->wrote_to_pc = 0;
8445
b434a28f
YQ
8446 if (!displaced_in_arm_mode (regs))
8447 return thumb_process_displaced_insn (gdbarch, from, to, regs, dsc);
8448
4db71c0b
YQ
8449 dsc->is_thumb = 0;
8450 dsc->insn_size = 4;
b434a28f
YQ
8451 insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
8452 if (debug_displaced)
8453 fprintf_unfiltered (gdb_stdlog, "displaced: stepping insn %.8lx "
8454 "at %.8lx\n", (unsigned long) insn,
8455 (unsigned long) from);
8456
cca44b1b 8457 if ((insn & 0xf0000000) == 0xf0000000)
7ff120b4 8458 err = arm_decode_unconditional (gdbarch, insn, regs, dsc);
cca44b1b
JB
8459 else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
8460 {
8461 case 0x0: case 0x1: case 0x2: case 0x3:
7ff120b4 8462 err = arm_decode_dp_misc (gdbarch, insn, regs, dsc);
cca44b1b
JB
8463 break;
8464
8465 case 0x4: case 0x5: case 0x6:
7ff120b4 8466 err = arm_decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
cca44b1b
JB
8467 break;
8468
8469 case 0x7:
7ff120b4 8470 err = arm_decode_media (gdbarch, insn, dsc);
cca44b1b
JB
8471 break;
8472
8473 case 0x8: case 0x9: case 0xa: case 0xb:
7ff120b4 8474 err = arm_decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
cca44b1b
JB
8475 break;
8476
8477 case 0xc: case 0xd: case 0xe: case 0xf:
7ff120b4 8478 err = arm_decode_svc_copro (gdbarch, insn, to, regs, dsc);
cca44b1b
JB
8479 break;
8480 }
8481
8482 if (err)
8483 internal_error (__FILE__, __LINE__,
8484 _("arm_process_displaced_insn: Instruction decode error"));
8485}
8486
8487/* Actually set up the scratch space for a displaced instruction. */
8488
8489void
8490arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
8491 CORE_ADDR to, struct displaced_step_closure *dsc)
8492{
8493 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4db71c0b 8494 unsigned int i, len, offset;
cca44b1b 8495 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4db71c0b
YQ
8496 int size = dsc->is_thumb? 2 : 4;
8497 const unsigned char *bkp_insn;
cca44b1b 8498
4db71c0b 8499 offset = 0;
cca44b1b
JB
8500 /* Poke modified instruction(s). */
8501 for (i = 0; i < dsc->numinsns; i++)
8502 {
8503 if (debug_displaced)
4db71c0b
YQ
8504 {
8505 fprintf_unfiltered (gdb_stdlog, "displaced: writing insn ");
8506 if (size == 4)
8507 fprintf_unfiltered (gdb_stdlog, "%.8lx",
8508 dsc->modinsn[i]);
8509 else if (size == 2)
8510 fprintf_unfiltered (gdb_stdlog, "%.4x",
8511 (unsigned short)dsc->modinsn[i]);
8512
8513 fprintf_unfiltered (gdb_stdlog, " at %.8lx\n",
8514 (unsigned long) to + offset);
8515
8516 }
8517 write_memory_unsigned_integer (to + offset, size,
8518 byte_order_for_code,
cca44b1b 8519 dsc->modinsn[i]);
4db71c0b
YQ
8520 offset += size;
8521 }
8522
8523 /* Choose the correct breakpoint instruction. */
8524 if (dsc->is_thumb)
8525 {
8526 bkp_insn = tdep->thumb_breakpoint;
8527 len = tdep->thumb_breakpoint_size;
8528 }
8529 else
8530 {
8531 bkp_insn = tdep->arm_breakpoint;
8532 len = tdep->arm_breakpoint_size;
cca44b1b
JB
8533 }
8534
8535 /* Put breakpoint afterwards. */
4db71c0b 8536 write_memory (to + offset, bkp_insn, len);
cca44b1b
JB
8537
8538 if (debug_displaced)
8539 fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
8540 paddress (gdbarch, from), paddress (gdbarch, to));
8541}
8542
8543/* Entry point for copying an instruction into scratch space for displaced
8544 stepping. */
8545
8546struct displaced_step_closure *
8547arm_displaced_step_copy_insn (struct gdbarch *gdbarch,
8548 CORE_ADDR from, CORE_ADDR to,
8549 struct regcache *regs)
8550{
8551 struct displaced_step_closure *dsc
8552 = xmalloc (sizeof (struct displaced_step_closure));
b434a28f 8553 arm_process_displaced_insn (gdbarch, from, to, regs, dsc);
cca44b1b
JB
8554 arm_displaced_init_closure (gdbarch, from, to, dsc);
8555
8556 return dsc;
8557}
8558
8559/* Entry point for cleaning things up after a displaced instruction has been
8560 single-stepped. */
8561
8562void
8563arm_displaced_step_fixup (struct gdbarch *gdbarch,
8564 struct displaced_step_closure *dsc,
8565 CORE_ADDR from, CORE_ADDR to,
8566 struct regcache *regs)
8567{
8568 if (dsc->cleanup)
8569 dsc->cleanup (gdbarch, regs, dsc);
8570
8571 if (!dsc->wrote_to_pc)
4db71c0b
YQ
8572 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
8573 dsc->insn_addr + dsc->insn_size);
8574
cca44b1b
JB
8575}
8576
8577#include "bfd-in2.h"
8578#include "libcoff.h"
8579
8580static int
8581gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
8582{
9779414d
DJ
8583 struct gdbarch *gdbarch = info->application_data;
8584
8585 if (arm_pc_is_thumb (gdbarch, memaddr))
cca44b1b
JB
8586 {
8587 static asymbol *asym;
8588 static combined_entry_type ce;
8589 static struct coff_symbol_struct csym;
8590 static struct bfd fake_bfd;
8591 static bfd_target fake_target;
8592
8593 if (csym.native == NULL)
8594 {
8595 /* Create a fake symbol vector containing a Thumb symbol.
8596 This is solely so that the code in print_insn_little_arm()
8597 and print_insn_big_arm() in opcodes/arm-dis.c will detect
8598 the presence of a Thumb symbol and switch to decoding
8599 Thumb instructions. */
8600
8601 fake_target.flavour = bfd_target_coff_flavour;
8602 fake_bfd.xvec = &fake_target;
8603 ce.u.syment.n_sclass = C_THUMBEXTFUNC;
8604 csym.native = &ce;
8605 csym.symbol.the_bfd = &fake_bfd;
8606 csym.symbol.name = "fake";
8607 asym = (asymbol *) & csym;
8608 }
8609
8610 memaddr = UNMAKE_THUMB_ADDR (memaddr);
8611 info->symbols = &asym;
8612 }
8613 else
8614 info->symbols = NULL;
8615
8616 if (info->endian == BFD_ENDIAN_BIG)
8617 return print_insn_big_arm (memaddr, info);
8618 else
8619 return print_insn_little_arm (memaddr, info);
8620}
8621
8622/* The following define instruction sequences that will cause ARM
8623 cpu's to take an undefined instruction trap. These are used to
8624 signal a breakpoint to GDB.
8625
8626 The newer ARMv4T cpu's are capable of operating in ARM or Thumb
8627 modes. A different instruction is required for each mode. The ARM
8628 cpu's can also be big or little endian. Thus four different
8629 instructions are needed to support all cases.
8630
8631 Note: ARMv4 defines several new instructions that will take the
8632 undefined instruction trap. ARM7TDMI is nominally ARMv4T, but does
8633 not in fact add the new instructions. The new undefined
8634 instructions in ARMv4 are all instructions that had no defined
8635 behaviour in earlier chips. There is no guarantee that they will
8636 raise an exception, but may be treated as NOP's. In practice, it
8637 may only safe to rely on instructions matching:
8638
8639 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
8640 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
8641 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
8642
0963b4bd 8643 Even this may only true if the condition predicate is true. The
cca44b1b
JB
8644 following use a condition predicate of ALWAYS so it is always TRUE.
8645
8646 There are other ways of forcing a breakpoint. GNU/Linux, RISC iX,
8647 and NetBSD all use a software interrupt rather than an undefined
8648 instruction to force a trap. This can be handled by by the
8649 abi-specific code during establishment of the gdbarch vector. */
8650
8651#define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
8652#define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
8653#define THUMB_LE_BREAKPOINT {0xbe,0xbe}
8654#define THUMB_BE_BREAKPOINT {0xbe,0xbe}
8655
8656static const char arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
8657static const char arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
8658static const char arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
8659static const char arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
8660
8661/* Determine the type and size of breakpoint to insert at PCPTR. Uses
8662 the program counter value to determine whether a 16-bit or 32-bit
8663 breakpoint should be used. It returns a pointer to a string of
8664 bytes that encode a breakpoint instruction, stores the length of
8665 the string to *lenptr, and adjusts the program counter (if
8666 necessary) to point to the actual memory location where the
8667 breakpoint should be inserted. */
8668
8669static const unsigned char *
8670arm_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenptr)
8671{
8672 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
177321bd 8673 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
cca44b1b 8674
9779414d 8675 if (arm_pc_is_thumb (gdbarch, *pcptr))
cca44b1b
JB
8676 {
8677 *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
177321bd
DJ
8678
8679 /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
8680 check whether we are replacing a 32-bit instruction. */
8681 if (tdep->thumb2_breakpoint != NULL)
8682 {
8683 gdb_byte buf[2];
8684 if (target_read_memory (*pcptr, buf, 2) == 0)
8685 {
8686 unsigned short inst1;
8687 inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
db24da6d 8688 if (thumb_insn_size (inst1) == 4)
177321bd
DJ
8689 {
8690 *lenptr = tdep->thumb2_breakpoint_size;
8691 return tdep->thumb2_breakpoint;
8692 }
8693 }
8694 }
8695
cca44b1b
JB
8696 *lenptr = tdep->thumb_breakpoint_size;
8697 return tdep->thumb_breakpoint;
8698 }
8699 else
8700 {
8701 *lenptr = tdep->arm_breakpoint_size;
8702 return tdep->arm_breakpoint;
8703 }
8704}
8705
177321bd
DJ
8706static void
8707arm_remote_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
8708 int *kindptr)
8709{
8710 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
8711
8712 arm_breakpoint_from_pc (gdbarch, pcptr, kindptr);
8713
9779414d 8714 if (arm_pc_is_thumb (gdbarch, *pcptr) && *kindptr == 4)
177321bd
DJ
8715 /* The documented magic value for a 32-bit Thumb-2 breakpoint, so
8716 that this is not confused with a 32-bit ARM breakpoint. */
8717 *kindptr = 3;
8718}
8719
cca44b1b
JB
8720/* Extract from an array REGBUF containing the (raw) register state a
8721 function return value of type TYPE, and copy that, in virtual
8722 format, into VALBUF. */
8723
8724static void
8725arm_extract_return_value (struct type *type, struct regcache *regs,
8726 gdb_byte *valbuf)
8727{
8728 struct gdbarch *gdbarch = get_regcache_arch (regs);
8729 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8730
8731 if (TYPE_CODE_FLT == TYPE_CODE (type))
8732 {
8733 switch (gdbarch_tdep (gdbarch)->fp_model)
8734 {
8735 case ARM_FLOAT_FPA:
8736 {
8737 /* The value is in register F0 in internal format. We need to
8738 extract the raw value and then convert it to the desired
8739 internal type. */
8740 bfd_byte tmpbuf[FP_REGISTER_SIZE];
8741
8742 regcache_cooked_read (regs, ARM_F0_REGNUM, tmpbuf);
8743 convert_from_extended (floatformat_from_type (type), tmpbuf,
8744 valbuf, gdbarch_byte_order (gdbarch));
8745 }
8746 break;
8747
8748 case ARM_FLOAT_SOFT_FPA:
8749 case ARM_FLOAT_SOFT_VFP:
8750 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8751 not using the VFP ABI code. */
8752 case ARM_FLOAT_VFP:
8753 regcache_cooked_read (regs, ARM_A1_REGNUM, valbuf);
8754 if (TYPE_LENGTH (type) > 4)
8755 regcache_cooked_read (regs, ARM_A1_REGNUM + 1,
8756 valbuf + INT_REGISTER_SIZE);
8757 break;
8758
8759 default:
0963b4bd
MS
8760 internal_error (__FILE__, __LINE__,
8761 _("arm_extract_return_value: "
8762 "Floating point model not supported"));
cca44b1b
JB
8763 break;
8764 }
8765 }
8766 else if (TYPE_CODE (type) == TYPE_CODE_INT
8767 || TYPE_CODE (type) == TYPE_CODE_CHAR
8768 || TYPE_CODE (type) == TYPE_CODE_BOOL
8769 || TYPE_CODE (type) == TYPE_CODE_PTR
8770 || TYPE_CODE (type) == TYPE_CODE_REF
8771 || TYPE_CODE (type) == TYPE_CODE_ENUM)
8772 {
b021a221
MS
8773 /* If the type is a plain integer, then the access is
8774 straight-forward. Otherwise we have to play around a bit
8775 more. */
cca44b1b
JB
8776 int len = TYPE_LENGTH (type);
8777 int regno = ARM_A1_REGNUM;
8778 ULONGEST tmp;
8779
8780 while (len > 0)
8781 {
8782 /* By using store_unsigned_integer we avoid having to do
8783 anything special for small big-endian values. */
8784 regcache_cooked_read_unsigned (regs, regno++, &tmp);
8785 store_unsigned_integer (valbuf,
8786 (len > INT_REGISTER_SIZE
8787 ? INT_REGISTER_SIZE : len),
8788 byte_order, tmp);
8789 len -= INT_REGISTER_SIZE;
8790 valbuf += INT_REGISTER_SIZE;
8791 }
8792 }
8793 else
8794 {
8795 /* For a structure or union the behaviour is as if the value had
8796 been stored to word-aligned memory and then loaded into
8797 registers with 32-bit load instruction(s). */
8798 int len = TYPE_LENGTH (type);
8799 int regno = ARM_A1_REGNUM;
8800 bfd_byte tmpbuf[INT_REGISTER_SIZE];
8801
8802 while (len > 0)
8803 {
8804 regcache_cooked_read (regs, regno++, tmpbuf);
8805 memcpy (valbuf, tmpbuf,
8806 len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
8807 len -= INT_REGISTER_SIZE;
8808 valbuf += INT_REGISTER_SIZE;
8809 }
8810 }
8811}
8812
8813
8814/* Will a function return an aggregate type in memory or in a
8815 register? Return 0 if an aggregate type can be returned in a
8816 register, 1 if it must be returned in memory. */
8817
8818static int
8819arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
8820{
8821 int nRc;
8822 enum type_code code;
8823
8824 CHECK_TYPEDEF (type);
8825
8826 /* In the ARM ABI, "integer" like aggregate types are returned in
8827 registers. For an aggregate type to be integer like, its size
8828 must be less than or equal to INT_REGISTER_SIZE and the
8829 offset of each addressable subfield must be zero. Note that bit
8830 fields are not addressable, and all addressable subfields of
8831 unions always start at offset zero.
8832
8833 This function is based on the behaviour of GCC 2.95.1.
8834 See: gcc/arm.c: arm_return_in_memory() for details.
8835
8836 Note: All versions of GCC before GCC 2.95.2 do not set up the
8837 parameters correctly for a function returning the following
8838 structure: struct { float f;}; This should be returned in memory,
8839 not a register. Richard Earnshaw sent me a patch, but I do not
8840 know of any way to detect if a function like the above has been
8841 compiled with the correct calling convention. */
8842
8843 /* All aggregate types that won't fit in a register must be returned
8844 in memory. */
8845 if (TYPE_LENGTH (type) > INT_REGISTER_SIZE)
8846 {
8847 return 1;
8848 }
8849
8850 /* The AAPCS says all aggregates not larger than a word are returned
8851 in a register. */
8852 if (gdbarch_tdep (gdbarch)->arm_abi != ARM_ABI_APCS)
8853 return 0;
8854
8855 /* The only aggregate types that can be returned in a register are
8856 structs and unions. Arrays must be returned in memory. */
8857 code = TYPE_CODE (type);
8858 if ((TYPE_CODE_STRUCT != code) && (TYPE_CODE_UNION != code))
8859 {
8860 return 1;
8861 }
8862
8863 /* Assume all other aggregate types can be returned in a register.
8864 Run a check for structures, unions and arrays. */
8865 nRc = 0;
8866
8867 if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
8868 {
8869 int i;
8870 /* Need to check if this struct/union is "integer" like. For
8871 this to be true, its size must be less than or equal to
8872 INT_REGISTER_SIZE and the offset of each addressable
8873 subfield must be zero. Note that bit fields are not
8874 addressable, and unions always start at offset zero. If any
8875 of the subfields is a floating point type, the struct/union
8876 cannot be an integer type. */
8877
8878 /* For each field in the object, check:
8879 1) Is it FP? --> yes, nRc = 1;
67255d04
RE
8880 2) Is it addressable (bitpos != 0) and
8881 not packed (bitsize == 0)?
8882 --> yes, nRc = 1
8883 */
8884
8885 for (i = 0; i < TYPE_NFIELDS (type); i++)
8886 {
8887 enum type_code field_type_code;
0963b4bd
MS
8888 field_type_code = TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type,
8889 i)));
67255d04
RE
8890
8891 /* Is it a floating point type field? */
8892 if (field_type_code == TYPE_CODE_FLT)
8893 {
8894 nRc = 1;
8895 break;
8896 }
8897
8898 /* If bitpos != 0, then we have to care about it. */
8899 if (TYPE_FIELD_BITPOS (type, i) != 0)
8900 {
8901 /* Bitfields are not addressable. If the field bitsize is
8902 zero, then the field is not packed. Hence it cannot be
8903 a bitfield or any other packed type. */
8904 if (TYPE_FIELD_BITSIZE (type, i) == 0)
8905 {
8906 nRc = 1;
8907 break;
8908 }
8909 }
8910 }
8911 }
8912
8913 return nRc;
8914}
8915
34e8f22d
RE
8916/* Write into appropriate registers a function return value of type
8917 TYPE, given in virtual format. */
8918
8919static void
b508a996 8920arm_store_return_value (struct type *type, struct regcache *regs,
5238cf52 8921 const gdb_byte *valbuf)
34e8f22d 8922{
be8626e0 8923 struct gdbarch *gdbarch = get_regcache_arch (regs);
e17a4113 8924 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
be8626e0 8925
34e8f22d
RE
8926 if (TYPE_CODE (type) == TYPE_CODE_FLT)
8927 {
7a5ea0d4 8928 char buf[MAX_REGISTER_SIZE];
34e8f22d 8929
be8626e0 8930 switch (gdbarch_tdep (gdbarch)->fp_model)
08216dd7
RE
8931 {
8932 case ARM_FLOAT_FPA:
8933
be8626e0
MD
8934 convert_to_extended (floatformat_from_type (type), buf, valbuf,
8935 gdbarch_byte_order (gdbarch));
b508a996 8936 regcache_cooked_write (regs, ARM_F0_REGNUM, buf);
08216dd7
RE
8937 break;
8938
fd50bc42 8939 case ARM_FLOAT_SOFT_FPA:
08216dd7 8940 case ARM_FLOAT_SOFT_VFP:
90445bd3
DJ
8941 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8942 not using the VFP ABI code. */
8943 case ARM_FLOAT_VFP:
b508a996
RE
8944 regcache_cooked_write (regs, ARM_A1_REGNUM, valbuf);
8945 if (TYPE_LENGTH (type) > 4)
8946 regcache_cooked_write (regs, ARM_A1_REGNUM + 1,
7a5ea0d4 8947 valbuf + INT_REGISTER_SIZE);
08216dd7
RE
8948 break;
8949
8950 default:
9b20d036
MS
8951 internal_error (__FILE__, __LINE__,
8952 _("arm_store_return_value: Floating "
8953 "point model not supported"));
08216dd7
RE
8954 break;
8955 }
34e8f22d 8956 }
b508a996
RE
8957 else if (TYPE_CODE (type) == TYPE_CODE_INT
8958 || TYPE_CODE (type) == TYPE_CODE_CHAR
8959 || TYPE_CODE (type) == TYPE_CODE_BOOL
8960 || TYPE_CODE (type) == TYPE_CODE_PTR
8961 || TYPE_CODE (type) == TYPE_CODE_REF
8962 || TYPE_CODE (type) == TYPE_CODE_ENUM)
8963 {
8964 if (TYPE_LENGTH (type) <= 4)
8965 {
8966 /* Values of one word or less are zero/sign-extended and
8967 returned in r0. */
7a5ea0d4 8968 bfd_byte tmpbuf[INT_REGISTER_SIZE];
b508a996
RE
8969 LONGEST val = unpack_long (type, valbuf);
8970
e17a4113 8971 store_signed_integer (tmpbuf, INT_REGISTER_SIZE, byte_order, val);
b508a996
RE
8972 regcache_cooked_write (regs, ARM_A1_REGNUM, tmpbuf);
8973 }
8974 else
8975 {
8976 /* Integral values greater than one word are stored in consecutive
8977 registers starting with r0. This will always be a multiple of
8978 the regiser size. */
8979 int len = TYPE_LENGTH (type);
8980 int regno = ARM_A1_REGNUM;
8981
8982 while (len > 0)
8983 {
8984 regcache_cooked_write (regs, regno++, valbuf);
7a5ea0d4
DJ
8985 len -= INT_REGISTER_SIZE;
8986 valbuf += INT_REGISTER_SIZE;
b508a996
RE
8987 }
8988 }
8989 }
34e8f22d 8990 else
b508a996
RE
8991 {
8992 /* For a structure or union the behaviour is as if the value had
8993 been stored to word-aligned memory and then loaded into
8994 registers with 32-bit load instruction(s). */
8995 int len = TYPE_LENGTH (type);
8996 int regno = ARM_A1_REGNUM;
7a5ea0d4 8997 bfd_byte tmpbuf[INT_REGISTER_SIZE];
b508a996
RE
8998
8999 while (len > 0)
9000 {
9001 memcpy (tmpbuf, valbuf,
7a5ea0d4 9002 len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
b508a996 9003 regcache_cooked_write (regs, regno++, tmpbuf);
7a5ea0d4
DJ
9004 len -= INT_REGISTER_SIZE;
9005 valbuf += INT_REGISTER_SIZE;
b508a996
RE
9006 }
9007 }
34e8f22d
RE
9008}
9009
2af48f68
PB
9010
9011/* Handle function return values. */
9012
9013static enum return_value_convention
6a3a010b 9014arm_return_value (struct gdbarch *gdbarch, struct value *function,
c055b101
CV
9015 struct type *valtype, struct regcache *regcache,
9016 gdb_byte *readbuf, const gdb_byte *writebuf)
2af48f68 9017{
7c00367c 9018 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
6a3a010b 9019 struct type *func_type = function ? value_type (function) : NULL;
90445bd3
DJ
9020 enum arm_vfp_cprc_base_type vfp_base_type;
9021 int vfp_base_count;
9022
9023 if (arm_vfp_abi_for_function (gdbarch, func_type)
9024 && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
9025 {
9026 int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
9027 int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
9028 int i;
9029 for (i = 0; i < vfp_base_count; i++)
9030 {
58d6951d
DJ
9031 if (reg_char == 'q')
9032 {
9033 if (writebuf)
9034 arm_neon_quad_write (gdbarch, regcache, i,
9035 writebuf + i * unit_length);
9036
9037 if (readbuf)
9038 arm_neon_quad_read (gdbarch, regcache, i,
9039 readbuf + i * unit_length);
9040 }
9041 else
9042 {
9043 char name_buf[4];
9044 int regnum;
9045
9046 sprintf (name_buf, "%c%d", reg_char, i);
9047 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9048 strlen (name_buf));
9049 if (writebuf)
9050 regcache_cooked_write (regcache, regnum,
9051 writebuf + i * unit_length);
9052 if (readbuf)
9053 regcache_cooked_read (regcache, regnum,
9054 readbuf + i * unit_length);
9055 }
90445bd3
DJ
9056 }
9057 return RETURN_VALUE_REGISTER_CONVENTION;
9058 }
7c00367c 9059
2af48f68
PB
9060 if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
9061 || TYPE_CODE (valtype) == TYPE_CODE_UNION
9062 || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
9063 {
7c00367c
MK
9064 if (tdep->struct_return == pcc_struct_return
9065 || arm_return_in_memory (gdbarch, valtype))
2af48f68
PB
9066 return RETURN_VALUE_STRUCT_CONVENTION;
9067 }
9068
7052e42c
UW
9069 /* AAPCS returns complex types longer than a register in memory. */
9070 if (tdep->arm_abi != ARM_ABI_APCS
9071 && TYPE_CODE (valtype) == TYPE_CODE_COMPLEX
9072 && TYPE_LENGTH (valtype) > INT_REGISTER_SIZE)
9073 return RETURN_VALUE_STRUCT_CONVENTION;
9074
2af48f68
PB
9075 if (writebuf)
9076 arm_store_return_value (valtype, regcache, writebuf);
9077
9078 if (readbuf)
9079 arm_extract_return_value (valtype, regcache, readbuf);
9080
9081 return RETURN_VALUE_REGISTER_CONVENTION;
9082}
9083
9084
9df628e0 9085static int
60ade65d 9086arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
9df628e0 9087{
e17a4113
UW
9088 struct gdbarch *gdbarch = get_frame_arch (frame);
9089 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
9090 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
9df628e0 9091 CORE_ADDR jb_addr;
7a5ea0d4 9092 char buf[INT_REGISTER_SIZE];
9df628e0 9093
60ade65d 9094 jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
9df628e0
RE
9095
9096 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
7a5ea0d4 9097 INT_REGISTER_SIZE))
9df628e0
RE
9098 return 0;
9099
e17a4113 9100 *pc = extract_unsigned_integer (buf, INT_REGISTER_SIZE, byte_order);
9df628e0
RE
9101 return 1;
9102}
9103
faa95490
DJ
9104/* Recognize GCC and GNU ld's trampolines. If we are in a trampoline,
9105 return the target PC. Otherwise return 0. */
c906108c
SS
9106
9107CORE_ADDR
52f729a7 9108arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
c906108c 9109{
2c02bd72 9110 const char *name;
faa95490 9111 int namelen;
c906108c
SS
9112 CORE_ADDR start_addr;
9113
9114 /* Find the starting address and name of the function containing the PC. */
9115 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
9116 return 0;
9117
faa95490
DJ
9118 /* If PC is in a Thumb call or return stub, return the address of the
9119 target PC, which is in a register. The thunk functions are called
9120 _call_via_xx, where x is the register name. The possible names
3d8d5e79
DJ
9121 are r0-r9, sl, fp, ip, sp, and lr. ARM RealView has similar
9122 functions, named __ARM_call_via_r[0-7]. */
9123 if (strncmp (name, "_call_via_", 10) == 0
9124 || strncmp (name, "__ARM_call_via_", strlen ("__ARM_call_via_")) == 0)
c906108c 9125 {
ed9a39eb
JM
9126 /* Use the name suffix to determine which register contains the
9127 target PC. */
c5aa993b
JM
9128 static char *table[15] =
9129 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
9130 "r8", "r9", "sl", "fp", "ip", "sp", "lr"
9131 };
c906108c 9132 int regno;
faa95490 9133 int offset = strlen (name) - 2;
c906108c
SS
9134
9135 for (regno = 0; regno <= 14; regno++)
faa95490 9136 if (strcmp (&name[offset], table[regno]) == 0)
52f729a7 9137 return get_frame_register_unsigned (frame, regno);
c906108c 9138 }
ed9a39eb 9139
faa95490
DJ
9140 /* GNU ld generates __foo_from_arm or __foo_from_thumb for
9141 non-interworking calls to foo. We could decode the stubs
9142 to find the target but it's easier to use the symbol table. */
9143 namelen = strlen (name);
9144 if (name[0] == '_' && name[1] == '_'
9145 && ((namelen > 2 + strlen ("_from_thumb")
9146 && strncmp (name + namelen - strlen ("_from_thumb"), "_from_thumb",
9147 strlen ("_from_thumb")) == 0)
9148 || (namelen > 2 + strlen ("_from_arm")
9149 && strncmp (name + namelen - strlen ("_from_arm"), "_from_arm",
9150 strlen ("_from_arm")) == 0)))
9151 {
9152 char *target_name;
9153 int target_len = namelen - 2;
9154 struct minimal_symbol *minsym;
9155 struct objfile *objfile;
9156 struct obj_section *sec;
9157
9158 if (name[namelen - 1] == 'b')
9159 target_len -= strlen ("_from_thumb");
9160 else
9161 target_len -= strlen ("_from_arm");
9162
9163 target_name = alloca (target_len + 1);
9164 memcpy (target_name, name + 2, target_len);
9165 target_name[target_len] = '\0';
9166
9167 sec = find_pc_section (pc);
9168 objfile = (sec == NULL) ? NULL : sec->objfile;
9169 minsym = lookup_minimal_symbol (target_name, NULL, objfile);
9170 if (minsym != NULL)
9171 return SYMBOL_VALUE_ADDRESS (minsym);
9172 else
9173 return 0;
9174 }
9175
c5aa993b 9176 return 0; /* not a stub */
c906108c
SS
9177}
9178
afd7eef0
RE
9179static void
9180set_arm_command (char *args, int from_tty)
9181{
edefbb7c
AC
9182 printf_unfiltered (_("\
9183\"set arm\" must be followed by an apporpriate subcommand.\n"));
afd7eef0
RE
9184 help_list (setarmcmdlist, "set arm ", all_commands, gdb_stdout);
9185}
9186
9187static void
9188show_arm_command (char *args, int from_tty)
9189{
26304000 9190 cmd_show_list (showarmcmdlist, from_tty, "");
afd7eef0
RE
9191}
9192
28e97307
DJ
9193static void
9194arm_update_current_architecture (void)
fd50bc42 9195{
28e97307 9196 struct gdbarch_info info;
fd50bc42 9197
28e97307 9198 /* If the current architecture is not ARM, we have nothing to do. */
1cf3db46 9199 if (gdbarch_bfd_arch_info (target_gdbarch)->arch != bfd_arch_arm)
28e97307 9200 return;
fd50bc42 9201
28e97307
DJ
9202 /* Update the architecture. */
9203 gdbarch_info_init (&info);
fd50bc42 9204
28e97307 9205 if (!gdbarch_update_p (info))
9b20d036 9206 internal_error (__FILE__, __LINE__, _("could not update architecture"));
fd50bc42
RE
9207}
9208
9209static void
9210set_fp_model_sfunc (char *args, int from_tty,
9211 struct cmd_list_element *c)
9212{
9213 enum arm_float_model fp_model;
9214
9215 for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
9216 if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
9217 {
9218 arm_fp_model = fp_model;
9219 break;
9220 }
9221
9222 if (fp_model == ARM_FLOAT_LAST)
edefbb7c 9223 internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
fd50bc42
RE
9224 current_fp_model);
9225
28e97307 9226 arm_update_current_architecture ();
fd50bc42
RE
9227}
9228
9229static void
08546159
AC
9230show_fp_model (struct ui_file *file, int from_tty,
9231 struct cmd_list_element *c, const char *value)
fd50bc42 9232{
1cf3db46 9233 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
fd50bc42 9234
28e97307 9235 if (arm_fp_model == ARM_FLOAT_AUTO
1cf3db46 9236 && gdbarch_bfd_arch_info (target_gdbarch)->arch == bfd_arch_arm)
28e97307
DJ
9237 fprintf_filtered (file, _("\
9238The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
9239 fp_model_strings[tdep->fp_model]);
9240 else
9241 fprintf_filtered (file, _("\
9242The current ARM floating point model is \"%s\".\n"),
9243 fp_model_strings[arm_fp_model]);
9244}
9245
9246static void
9247arm_set_abi (char *args, int from_tty,
9248 struct cmd_list_element *c)
9249{
9250 enum arm_abi_kind arm_abi;
9251
9252 for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
9253 if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
9254 {
9255 arm_abi_global = arm_abi;
9256 break;
9257 }
9258
9259 if (arm_abi == ARM_ABI_LAST)
9260 internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
9261 arm_abi_string);
9262
9263 arm_update_current_architecture ();
9264}
9265
9266static void
9267arm_show_abi (struct ui_file *file, int from_tty,
9268 struct cmd_list_element *c, const char *value)
9269{
1cf3db46 9270 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
28e97307
DJ
9271
9272 if (arm_abi_global == ARM_ABI_AUTO
1cf3db46 9273 && gdbarch_bfd_arch_info (target_gdbarch)->arch == bfd_arch_arm)
28e97307
DJ
9274 fprintf_filtered (file, _("\
9275The current ARM ABI is \"auto\" (currently \"%s\").\n"),
9276 arm_abi_strings[tdep->arm_abi]);
9277 else
9278 fprintf_filtered (file, _("The current ARM ABI is \"%s\".\n"),
9279 arm_abi_string);
fd50bc42
RE
9280}
9281
0428b8f5
DJ
9282static void
9283arm_show_fallback_mode (struct ui_file *file, int from_tty,
9284 struct cmd_list_element *c, const char *value)
9285{
1cf3db46 9286 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
0428b8f5 9287
0963b4bd
MS
9288 fprintf_filtered (file,
9289 _("The current execution mode assumed "
9290 "(when symbols are unavailable) is \"%s\".\n"),
0428b8f5
DJ
9291 arm_fallback_mode_string);
9292}
9293
9294static void
9295arm_show_force_mode (struct ui_file *file, int from_tty,
9296 struct cmd_list_element *c, const char *value)
9297{
1cf3db46 9298 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
0428b8f5 9299
0963b4bd
MS
9300 fprintf_filtered (file,
9301 _("The current execution mode assumed "
9302 "(even when symbols are available) is \"%s\".\n"),
0428b8f5
DJ
9303 arm_force_mode_string);
9304}
9305
afd7eef0
RE
9306/* If the user changes the register disassembly style used for info
9307 register and other commands, we have to also switch the style used
9308 in opcodes for disassembly output. This function is run in the "set
9309 arm disassembly" command, and does that. */
bc90b915
FN
9310
9311static void
afd7eef0 9312set_disassembly_style_sfunc (char *args, int from_tty,
bc90b915
FN
9313 struct cmd_list_element *c)
9314{
afd7eef0 9315 set_disassembly_style ();
bc90b915
FN
9316}
9317\f
966fbf70 9318/* Return the ARM register name corresponding to register I. */
a208b0cb 9319static const char *
d93859e2 9320arm_register_name (struct gdbarch *gdbarch, int i)
966fbf70 9321{
58d6951d
DJ
9322 const int num_regs = gdbarch_num_regs (gdbarch);
9323
9324 if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
9325 && i >= num_regs && i < num_regs + 32)
9326 {
9327 static const char *const vfp_pseudo_names[] = {
9328 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
9329 "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
9330 "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
9331 "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
9332 };
9333
9334 return vfp_pseudo_names[i - num_regs];
9335 }
9336
9337 if (gdbarch_tdep (gdbarch)->have_neon_pseudos
9338 && i >= num_regs + 32 && i < num_regs + 32 + 16)
9339 {
9340 static const char *const neon_pseudo_names[] = {
9341 "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
9342 "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
9343 };
9344
9345 return neon_pseudo_names[i - num_regs - 32];
9346 }
9347
ff6f572f
DJ
9348 if (i >= ARRAY_SIZE (arm_register_names))
9349 /* These registers are only supported on targets which supply
9350 an XML description. */
9351 return "";
9352
966fbf70
RE
9353 return arm_register_names[i];
9354}
9355
bc90b915 9356static void
afd7eef0 9357set_disassembly_style (void)
bc90b915 9358{
123dc839 9359 int current;
bc90b915 9360
123dc839
DJ
9361 /* Find the style that the user wants. */
9362 for (current = 0; current < num_disassembly_options; current++)
9363 if (disassembly_style == valid_disassembly_styles[current])
9364 break;
9365 gdb_assert (current < num_disassembly_options);
bc90b915 9366
94c30b78 9367 /* Synchronize the disassembler. */
bc90b915
FN
9368 set_arm_regname_option (current);
9369}
9370
082fc60d
RE
9371/* Test whether the coff symbol specific value corresponds to a Thumb
9372 function. */
9373
9374static int
9375coff_sym_is_thumb (int val)
9376{
f8bf5763
PM
9377 return (val == C_THUMBEXT
9378 || val == C_THUMBSTAT
9379 || val == C_THUMBEXTFUNC
9380 || val == C_THUMBSTATFUNC
9381 || val == C_THUMBLABEL);
082fc60d
RE
9382}
9383
9384/* arm_coff_make_msymbol_special()
9385 arm_elf_make_msymbol_special()
9386
9387 These functions test whether the COFF or ELF symbol corresponds to
9388 an address in thumb code, and set a "special" bit in a minimal
9389 symbol to indicate that it does. */
9390
34e8f22d 9391static void
082fc60d
RE
9392arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
9393{
467d42c4
UW
9394 if (ARM_SYM_BRANCH_TYPE (&((elf_symbol_type *)sym)->internal_elf_sym)
9395 == ST_BRANCH_TO_THUMB)
082fc60d
RE
9396 MSYMBOL_SET_SPECIAL (msym);
9397}
9398
34e8f22d 9399static void
082fc60d
RE
9400arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
9401{
9402 if (coff_sym_is_thumb (val))
9403 MSYMBOL_SET_SPECIAL (msym);
9404}
9405
60c5725c 9406static void
c1bd65d0 9407arm_objfile_data_free (struct objfile *objfile, void *arg)
60c5725c
DJ
9408{
9409 struct arm_per_objfile *data = arg;
9410 unsigned int i;
9411
9412 for (i = 0; i < objfile->obfd->section_count; i++)
9413 VEC_free (arm_mapping_symbol_s, data->section_maps[i]);
9414}
9415
9416static void
9417arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
9418 asymbol *sym)
9419{
9420 const char *name = bfd_asymbol_name (sym);
9421 struct arm_per_objfile *data;
9422 VEC(arm_mapping_symbol_s) **map_p;
9423 struct arm_mapping_symbol new_map_sym;
9424
9425 gdb_assert (name[0] == '$');
9426 if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
9427 return;
9428
9429 data = objfile_data (objfile, arm_objfile_data_key);
9430 if (data == NULL)
9431 {
9432 data = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9433 struct arm_per_objfile);
9434 set_objfile_data (objfile, arm_objfile_data_key, data);
9435 data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
9436 objfile->obfd->section_count,
9437 VEC(arm_mapping_symbol_s) *);
9438 }
9439 map_p = &data->section_maps[bfd_get_section (sym)->index];
9440
9441 new_map_sym.value = sym->value;
9442 new_map_sym.type = name[1];
9443
9444 /* Assume that most mapping symbols appear in order of increasing
9445 value. If they were randomly distributed, it would be faster to
9446 always push here and then sort at first use. */
9447 if (!VEC_empty (arm_mapping_symbol_s, *map_p))
9448 {
9449 struct arm_mapping_symbol *prev_map_sym;
9450
9451 prev_map_sym = VEC_last (arm_mapping_symbol_s, *map_p);
9452 if (prev_map_sym->value >= sym->value)
9453 {
9454 unsigned int idx;
9455 idx = VEC_lower_bound (arm_mapping_symbol_s, *map_p, &new_map_sym,
9456 arm_compare_mapping_symbols);
9457 VEC_safe_insert (arm_mapping_symbol_s, *map_p, idx, &new_map_sym);
9458 return;
9459 }
9460 }
9461
9462 VEC_safe_push (arm_mapping_symbol_s, *map_p, &new_map_sym);
9463}
9464
756fe439 9465static void
61a1198a 9466arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
756fe439 9467{
9779414d 9468 struct gdbarch *gdbarch = get_regcache_arch (regcache);
61a1198a 9469 regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
756fe439
DJ
9470
9471 /* If necessary, set the T bit. */
9472 if (arm_apcs_32)
9473 {
9779414d 9474 ULONGEST val, t_bit;
61a1198a 9475 regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
9779414d
DJ
9476 t_bit = arm_psr_thumb_bit (gdbarch);
9477 if (arm_pc_is_thumb (gdbarch, pc))
9478 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9479 val | t_bit);
756fe439 9480 else
61a1198a 9481 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9779414d 9482 val & ~t_bit);
756fe439
DJ
9483 }
9484}
123dc839 9485
58d6951d
DJ
9486/* Read the contents of a NEON quad register, by reading from two
9487 double registers. This is used to implement the quad pseudo
9488 registers, and for argument passing in case the quad registers are
9489 missing; vectors are passed in quad registers when using the VFP
9490 ABI, even if a NEON unit is not present. REGNUM is the index of
9491 the quad register, in [0, 15]. */
9492
05d1431c 9493static enum register_status
58d6951d
DJ
9494arm_neon_quad_read (struct gdbarch *gdbarch, struct regcache *regcache,
9495 int regnum, gdb_byte *buf)
9496{
9497 char name_buf[4];
9498 gdb_byte reg_buf[8];
9499 int offset, double_regnum;
05d1431c 9500 enum register_status status;
58d6951d
DJ
9501
9502 sprintf (name_buf, "d%d", regnum << 1);
9503 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9504 strlen (name_buf));
9505
9506 /* d0 is always the least significant half of q0. */
9507 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9508 offset = 8;
9509 else
9510 offset = 0;
9511
05d1431c
PA
9512 status = regcache_raw_read (regcache, double_regnum, reg_buf);
9513 if (status != REG_VALID)
9514 return status;
58d6951d
DJ
9515 memcpy (buf + offset, reg_buf, 8);
9516
9517 offset = 8 - offset;
05d1431c
PA
9518 status = regcache_raw_read (regcache, double_regnum + 1, reg_buf);
9519 if (status != REG_VALID)
9520 return status;
58d6951d 9521 memcpy (buf + offset, reg_buf, 8);
05d1431c
PA
9522
9523 return REG_VALID;
58d6951d
DJ
9524}
9525
05d1431c 9526static enum register_status
58d6951d
DJ
9527arm_pseudo_read (struct gdbarch *gdbarch, struct regcache *regcache,
9528 int regnum, gdb_byte *buf)
9529{
9530 const int num_regs = gdbarch_num_regs (gdbarch);
9531 char name_buf[4];
9532 gdb_byte reg_buf[8];
9533 int offset, double_regnum;
9534
9535 gdb_assert (regnum >= num_regs);
9536 regnum -= num_regs;
9537
9538 if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
9539 /* Quad-precision register. */
05d1431c 9540 return arm_neon_quad_read (gdbarch, regcache, regnum - 32, buf);
58d6951d
DJ
9541 else
9542 {
05d1431c
PA
9543 enum register_status status;
9544
58d6951d
DJ
9545 /* Single-precision register. */
9546 gdb_assert (regnum < 32);
9547
9548 /* s0 is always the least significant half of d0. */
9549 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9550 offset = (regnum & 1) ? 0 : 4;
9551 else
9552 offset = (regnum & 1) ? 4 : 0;
9553
9554 sprintf (name_buf, "d%d", regnum >> 1);
9555 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9556 strlen (name_buf));
9557
05d1431c
PA
9558 status = regcache_raw_read (regcache, double_regnum, reg_buf);
9559 if (status == REG_VALID)
9560 memcpy (buf, reg_buf + offset, 4);
9561 return status;
58d6951d
DJ
9562 }
9563}
9564
9565/* Store the contents of BUF to a NEON quad register, by writing to
9566 two double registers. This is used to implement the quad pseudo
9567 registers, and for argument passing in case the quad registers are
9568 missing; vectors are passed in quad registers when using the VFP
9569 ABI, even if a NEON unit is not present. REGNUM is the index
9570 of the quad register, in [0, 15]. */
9571
9572static void
9573arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
9574 int regnum, const gdb_byte *buf)
9575{
9576 char name_buf[4];
9577 gdb_byte reg_buf[8];
9578 int offset, double_regnum;
9579
9580 sprintf (name_buf, "d%d", regnum << 1);
9581 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9582 strlen (name_buf));
9583
9584 /* d0 is always the least significant half of q0. */
9585 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9586 offset = 8;
9587 else
9588 offset = 0;
9589
9590 regcache_raw_write (regcache, double_regnum, buf + offset);
9591 offset = 8 - offset;
9592 regcache_raw_write (regcache, double_regnum + 1, buf + offset);
9593}
9594
9595static void
9596arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
9597 int regnum, const gdb_byte *buf)
9598{
9599 const int num_regs = gdbarch_num_regs (gdbarch);
9600 char name_buf[4];
9601 gdb_byte reg_buf[8];
9602 int offset, double_regnum;
9603
9604 gdb_assert (regnum >= num_regs);
9605 regnum -= num_regs;
9606
9607 if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
9608 /* Quad-precision register. */
9609 arm_neon_quad_write (gdbarch, regcache, regnum - 32, buf);
9610 else
9611 {
9612 /* Single-precision register. */
9613 gdb_assert (regnum < 32);
9614
9615 /* s0 is always the least significant half of d0. */
9616 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9617 offset = (regnum & 1) ? 0 : 4;
9618 else
9619 offset = (regnum & 1) ? 4 : 0;
9620
9621 sprintf (name_buf, "d%d", regnum >> 1);
9622 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9623 strlen (name_buf));
9624
9625 regcache_raw_read (regcache, double_regnum, reg_buf);
9626 memcpy (reg_buf + offset, buf, 4);
9627 regcache_raw_write (regcache, double_regnum, reg_buf);
9628 }
9629}
9630
123dc839
DJ
9631static struct value *
9632value_of_arm_user_reg (struct frame_info *frame, const void *baton)
9633{
9634 const int *reg_p = baton;
9635 return value_of_register (*reg_p, frame);
9636}
97e03143 9637\f
70f80edf
JT
9638static enum gdb_osabi
9639arm_elf_osabi_sniffer (bfd *abfd)
97e03143 9640{
2af48f68 9641 unsigned int elfosabi;
70f80edf 9642 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
97e03143 9643
70f80edf 9644 elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
97e03143 9645
28e97307
DJ
9646 if (elfosabi == ELFOSABI_ARM)
9647 /* GNU tools use this value. Check note sections in this case,
9648 as well. */
9649 bfd_map_over_sections (abfd,
9650 generic_elf_osabi_sniff_abi_tag_sections,
9651 &osabi);
97e03143 9652
28e97307 9653 /* Anything else will be handled by the generic ELF sniffer. */
70f80edf 9654 return osabi;
97e03143
RE
9655}
9656
54483882
YQ
9657static int
9658arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
9659 struct reggroup *group)
9660{
2c291032
YQ
9661 /* FPS register's type is INT, but belongs to float_reggroup. Beside
9662 this, FPS register belongs to save_regroup, restore_reggroup, and
9663 all_reggroup, of course. */
54483882 9664 if (regnum == ARM_FPS_REGNUM)
2c291032
YQ
9665 return (group == float_reggroup
9666 || group == save_reggroup
9667 || group == restore_reggroup
9668 || group == all_reggroup);
54483882
YQ
9669 else
9670 return default_register_reggroup_p (gdbarch, regnum, group);
9671}
9672
25f8c692
JL
9673\f
9674/* For backward-compatibility we allow two 'g' packet lengths with
9675 the remote protocol depending on whether FPA registers are
9676 supplied. M-profile targets do not have FPA registers, but some
9677 stubs already exist in the wild which use a 'g' packet which
9678 supplies them albeit with dummy values. The packet format which
9679 includes FPA registers should be considered deprecated for
9680 M-profile targets. */
9681
9682static void
9683arm_register_g_packet_guesses (struct gdbarch *gdbarch)
9684{
9685 if (gdbarch_tdep (gdbarch)->is_m)
9686 {
9687 /* If we know from the executable this is an M-profile target,
9688 cater for remote targets whose register set layout is the
9689 same as the FPA layout. */
9690 register_remote_g_packet_guess (gdbarch,
03145bf4 9691 /* r0-r12,sp,lr,pc; f0-f7; fps,xpsr */
25f8c692
JL
9692 (16 * INT_REGISTER_SIZE)
9693 + (8 * FP_REGISTER_SIZE)
9694 + (2 * INT_REGISTER_SIZE),
9695 tdesc_arm_with_m_fpa_layout);
9696
9697 /* The regular M-profile layout. */
9698 register_remote_g_packet_guess (gdbarch,
9699 /* r0-r12,sp,lr,pc; xpsr */
9700 (16 * INT_REGISTER_SIZE)
9701 + INT_REGISTER_SIZE,
9702 tdesc_arm_with_m);
3184d3f9
JL
9703
9704 /* M-profile plus M4F VFP. */
9705 register_remote_g_packet_guess (gdbarch,
9706 /* r0-r12,sp,lr,pc; d0-d15; fpscr,xpsr */
9707 (16 * INT_REGISTER_SIZE)
9708 + (16 * VFP_REGISTER_SIZE)
9709 + (2 * INT_REGISTER_SIZE),
9710 tdesc_arm_with_m_vfp_d16);
25f8c692
JL
9711 }
9712
9713 /* Otherwise we don't have a useful guess. */
9714}
9715
70f80edf 9716\f
da3c6d4a
MS
9717/* Initialize the current architecture based on INFO. If possible,
9718 re-use an architecture from ARCHES, which is a list of
9719 architectures already created during this debugging session.
97e03143 9720
da3c6d4a
MS
9721 Called e.g. at program startup, when reading a core file, and when
9722 reading a binary file. */
97e03143 9723
39bbf761
RE
9724static struct gdbarch *
9725arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
9726{
97e03143 9727 struct gdbarch_tdep *tdep;
39bbf761 9728 struct gdbarch *gdbarch;
28e97307
DJ
9729 struct gdbarch_list *best_arch;
9730 enum arm_abi_kind arm_abi = arm_abi_global;
9731 enum arm_float_model fp_model = arm_fp_model;
123dc839 9732 struct tdesc_arch_data *tdesc_data = NULL;
9779414d 9733 int i, is_m = 0;
58d6951d
DJ
9734 int have_vfp_registers = 0, have_vfp_pseudos = 0, have_neon_pseudos = 0;
9735 int have_neon = 0;
ff6f572f 9736 int have_fpa_registers = 1;
9779414d
DJ
9737 const struct target_desc *tdesc = info.target_desc;
9738
9739 /* If we have an object to base this architecture on, try to determine
9740 its ABI. */
9741
9742 if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
9743 {
9744 int ei_osabi, e_flags;
9745
9746 switch (bfd_get_flavour (info.abfd))
9747 {
9748 case bfd_target_aout_flavour:
9749 /* Assume it's an old APCS-style ABI. */
9750 arm_abi = ARM_ABI_APCS;
9751 break;
9752
9753 case bfd_target_coff_flavour:
9754 /* Assume it's an old APCS-style ABI. */
9755 /* XXX WinCE? */
9756 arm_abi = ARM_ABI_APCS;
9757 break;
9758
9759 case bfd_target_elf_flavour:
9760 ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
9761 e_flags = elf_elfheader (info.abfd)->e_flags;
9762
9763 if (ei_osabi == ELFOSABI_ARM)
9764 {
9765 /* GNU tools used to use this value, but do not for EABI
9766 objects. There's nowhere to tag an EABI version
9767 anyway, so assume APCS. */
9768 arm_abi = ARM_ABI_APCS;
9769 }
9770 else if (ei_osabi == ELFOSABI_NONE)
9771 {
9772 int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
9773 int attr_arch, attr_profile;
9774
9775 switch (eabi_ver)
9776 {
9777 case EF_ARM_EABI_UNKNOWN:
9778 /* Assume GNU tools. */
9779 arm_abi = ARM_ABI_APCS;
9780 break;
9781
9782 case EF_ARM_EABI_VER4:
9783 case EF_ARM_EABI_VER5:
9784 arm_abi = ARM_ABI_AAPCS;
9785 /* EABI binaries default to VFP float ordering.
9786 They may also contain build attributes that can
9787 be used to identify if the VFP argument-passing
9788 ABI is in use. */
9789 if (fp_model == ARM_FLOAT_AUTO)
9790 {
9791#ifdef HAVE_ELF
9792 switch (bfd_elf_get_obj_attr_int (info.abfd,
9793 OBJ_ATTR_PROC,
9794 Tag_ABI_VFP_args))
9795 {
9796 case 0:
9797 /* "The user intended FP parameter/result
9798 passing to conform to AAPCS, base
9799 variant". */
9800 fp_model = ARM_FLOAT_SOFT_VFP;
9801 break;
9802 case 1:
9803 /* "The user intended FP parameter/result
9804 passing to conform to AAPCS, VFP
9805 variant". */
9806 fp_model = ARM_FLOAT_VFP;
9807 break;
9808 case 2:
9809 /* "The user intended FP parameter/result
9810 passing to conform to tool chain-specific
9811 conventions" - we don't know any such
9812 conventions, so leave it as "auto". */
9813 break;
9814 default:
9815 /* Attribute value not mentioned in the
9816 October 2008 ABI, so leave it as
9817 "auto". */
9818 break;
9819 }
9820#else
9821 fp_model = ARM_FLOAT_SOFT_VFP;
9822#endif
9823 }
9824 break;
9825
9826 default:
9827 /* Leave it as "auto". */
9828 warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
9829 break;
9830 }
9831
9832#ifdef HAVE_ELF
9833 /* Detect M-profile programs. This only works if the
9834 executable file includes build attributes; GCC does
9835 copy them to the executable, but e.g. RealView does
9836 not. */
9837 attr_arch = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9838 Tag_CPU_arch);
0963b4bd
MS
9839 attr_profile = bfd_elf_get_obj_attr_int (info.abfd,
9840 OBJ_ATTR_PROC,
9779414d
DJ
9841 Tag_CPU_arch_profile);
9842 /* GCC specifies the profile for v6-M; RealView only
9843 specifies the profile for architectures starting with
9844 V7 (as opposed to architectures with a tag
9845 numerically greater than TAG_CPU_ARCH_V7). */
9846 if (!tdesc_has_registers (tdesc)
9847 && (attr_arch == TAG_CPU_ARCH_V6_M
9848 || attr_arch == TAG_CPU_ARCH_V6S_M
9849 || attr_profile == 'M'))
25f8c692 9850 is_m = 1;
9779414d
DJ
9851#endif
9852 }
9853
9854 if (fp_model == ARM_FLOAT_AUTO)
9855 {
9856 int e_flags = elf_elfheader (info.abfd)->e_flags;
9857
9858 switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
9859 {
9860 case 0:
9861 /* Leave it as "auto". Strictly speaking this case
9862 means FPA, but almost nobody uses that now, and
9863 many toolchains fail to set the appropriate bits
9864 for the floating-point model they use. */
9865 break;
9866 case EF_ARM_SOFT_FLOAT:
9867 fp_model = ARM_FLOAT_SOFT_FPA;
9868 break;
9869 case EF_ARM_VFP_FLOAT:
9870 fp_model = ARM_FLOAT_VFP;
9871 break;
9872 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
9873 fp_model = ARM_FLOAT_SOFT_VFP;
9874 break;
9875 }
9876 }
9877
9878 if (e_flags & EF_ARM_BE8)
9879 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
9880
9881 break;
9882
9883 default:
9884 /* Leave it as "auto". */
9885 break;
9886 }
9887 }
123dc839
DJ
9888
9889 /* Check any target description for validity. */
9779414d 9890 if (tdesc_has_registers (tdesc))
123dc839
DJ
9891 {
9892 /* For most registers we require GDB's default names; but also allow
9893 the numeric names for sp / lr / pc, as a convenience. */
9894 static const char *const arm_sp_names[] = { "r13", "sp", NULL };
9895 static const char *const arm_lr_names[] = { "r14", "lr", NULL };
9896 static const char *const arm_pc_names[] = { "r15", "pc", NULL };
9897
9898 const struct tdesc_feature *feature;
58d6951d 9899 int valid_p;
123dc839 9900
9779414d 9901 feature = tdesc_find_feature (tdesc,
123dc839
DJ
9902 "org.gnu.gdb.arm.core");
9903 if (feature == NULL)
9779414d
DJ
9904 {
9905 feature = tdesc_find_feature (tdesc,
9906 "org.gnu.gdb.arm.m-profile");
9907 if (feature == NULL)
9908 return NULL;
9909 else
9910 is_m = 1;
9911 }
123dc839
DJ
9912
9913 tdesc_data = tdesc_data_alloc ();
9914
9915 valid_p = 1;
9916 for (i = 0; i < ARM_SP_REGNUM; i++)
9917 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
9918 arm_register_names[i]);
9919 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
9920 ARM_SP_REGNUM,
9921 arm_sp_names);
9922 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
9923 ARM_LR_REGNUM,
9924 arm_lr_names);
9925 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
9926 ARM_PC_REGNUM,
9927 arm_pc_names);
9779414d
DJ
9928 if (is_m)
9929 valid_p &= tdesc_numbered_register (feature, tdesc_data,
9930 ARM_PS_REGNUM, "xpsr");
9931 else
9932 valid_p &= tdesc_numbered_register (feature, tdesc_data,
9933 ARM_PS_REGNUM, "cpsr");
123dc839
DJ
9934
9935 if (!valid_p)
9936 {
9937 tdesc_data_cleanup (tdesc_data);
9938 return NULL;
9939 }
9940
9779414d 9941 feature = tdesc_find_feature (tdesc,
123dc839
DJ
9942 "org.gnu.gdb.arm.fpa");
9943 if (feature != NULL)
9944 {
9945 valid_p = 1;
9946 for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
9947 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
9948 arm_register_names[i]);
9949 if (!valid_p)
9950 {
9951 tdesc_data_cleanup (tdesc_data);
9952 return NULL;
9953 }
9954 }
ff6f572f
DJ
9955 else
9956 have_fpa_registers = 0;
9957
9779414d 9958 feature = tdesc_find_feature (tdesc,
ff6f572f
DJ
9959 "org.gnu.gdb.xscale.iwmmxt");
9960 if (feature != NULL)
9961 {
9962 static const char *const iwmmxt_names[] = {
9963 "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
9964 "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
9965 "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
9966 "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
9967 };
9968
9969 valid_p = 1;
9970 for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
9971 valid_p
9972 &= tdesc_numbered_register (feature, tdesc_data, i,
9973 iwmmxt_names[i - ARM_WR0_REGNUM]);
9974
9975 /* Check for the control registers, but do not fail if they
9976 are missing. */
9977 for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
9978 tdesc_numbered_register (feature, tdesc_data, i,
9979 iwmmxt_names[i - ARM_WR0_REGNUM]);
9980
9981 for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
9982 valid_p
9983 &= tdesc_numbered_register (feature, tdesc_data, i,
9984 iwmmxt_names[i - ARM_WR0_REGNUM]);
9985
9986 if (!valid_p)
9987 {
9988 tdesc_data_cleanup (tdesc_data);
9989 return NULL;
9990 }
9991 }
58d6951d
DJ
9992
9993 /* If we have a VFP unit, check whether the single precision registers
9994 are present. If not, then we will synthesize them as pseudo
9995 registers. */
9779414d 9996 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
9997 "org.gnu.gdb.arm.vfp");
9998 if (feature != NULL)
9999 {
10000 static const char *const vfp_double_names[] = {
10001 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
10002 "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
10003 "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
10004 "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
10005 };
10006
10007 /* Require the double precision registers. There must be either
10008 16 or 32. */
10009 valid_p = 1;
10010 for (i = 0; i < 32; i++)
10011 {
10012 valid_p &= tdesc_numbered_register (feature, tdesc_data,
10013 ARM_D0_REGNUM + i,
10014 vfp_double_names[i]);
10015 if (!valid_p)
10016 break;
10017 }
2b9e5ea6
UW
10018 if (!valid_p && i == 16)
10019 valid_p = 1;
58d6951d 10020
2b9e5ea6
UW
10021 /* Also require FPSCR. */
10022 valid_p &= tdesc_numbered_register (feature, tdesc_data,
10023 ARM_FPSCR_REGNUM, "fpscr");
10024 if (!valid_p)
58d6951d
DJ
10025 {
10026 tdesc_data_cleanup (tdesc_data);
10027 return NULL;
10028 }
10029
10030 if (tdesc_unnumbered_register (feature, "s0") == 0)
10031 have_vfp_pseudos = 1;
10032
10033 have_vfp_registers = 1;
10034
10035 /* If we have VFP, also check for NEON. The architecture allows
10036 NEON without VFP (integer vector operations only), but GDB
10037 does not support that. */
9779414d 10038 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
10039 "org.gnu.gdb.arm.neon");
10040 if (feature != NULL)
10041 {
10042 /* NEON requires 32 double-precision registers. */
10043 if (i != 32)
10044 {
10045 tdesc_data_cleanup (tdesc_data);
10046 return NULL;
10047 }
10048
10049 /* If there are quad registers defined by the stub, use
10050 their type; otherwise (normally) provide them with
10051 the default type. */
10052 if (tdesc_unnumbered_register (feature, "q0") == 0)
10053 have_neon_pseudos = 1;
10054
10055 have_neon = 1;
10056 }
10057 }
123dc839 10058 }
39bbf761 10059
28e97307
DJ
10060 /* If there is already a candidate, use it. */
10061 for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
10062 best_arch != NULL;
10063 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
10064 {
b8926edc
DJ
10065 if (arm_abi != ARM_ABI_AUTO
10066 && arm_abi != gdbarch_tdep (best_arch->gdbarch)->arm_abi)
28e97307
DJ
10067 continue;
10068
b8926edc
DJ
10069 if (fp_model != ARM_FLOAT_AUTO
10070 && fp_model != gdbarch_tdep (best_arch->gdbarch)->fp_model)
28e97307
DJ
10071 continue;
10072
58d6951d
DJ
10073 /* There are various other properties in tdep that we do not
10074 need to check here: those derived from a target description,
10075 since gdbarches with a different target description are
10076 automatically disqualified. */
10077
9779414d
DJ
10078 /* Do check is_m, though, since it might come from the binary. */
10079 if (is_m != gdbarch_tdep (best_arch->gdbarch)->is_m)
10080 continue;
10081
28e97307
DJ
10082 /* Found a match. */
10083 break;
10084 }
97e03143 10085
28e97307 10086 if (best_arch != NULL)
123dc839
DJ
10087 {
10088 if (tdesc_data != NULL)
10089 tdesc_data_cleanup (tdesc_data);
10090 return best_arch->gdbarch;
10091 }
28e97307
DJ
10092
10093 tdep = xcalloc (1, sizeof (struct gdbarch_tdep));
97e03143
RE
10094 gdbarch = gdbarch_alloc (&info, tdep);
10095
28e97307
DJ
10096 /* Record additional information about the architecture we are defining.
10097 These are gdbarch discriminators, like the OSABI. */
10098 tdep->arm_abi = arm_abi;
10099 tdep->fp_model = fp_model;
9779414d 10100 tdep->is_m = is_m;
ff6f572f 10101 tdep->have_fpa_registers = have_fpa_registers;
58d6951d
DJ
10102 tdep->have_vfp_registers = have_vfp_registers;
10103 tdep->have_vfp_pseudos = have_vfp_pseudos;
10104 tdep->have_neon_pseudos = have_neon_pseudos;
10105 tdep->have_neon = have_neon;
08216dd7 10106
25f8c692
JL
10107 arm_register_g_packet_guesses (gdbarch);
10108
08216dd7 10109 /* Breakpoints. */
9d4fde75 10110 switch (info.byte_order_for_code)
67255d04
RE
10111 {
10112 case BFD_ENDIAN_BIG:
66e810cd
RE
10113 tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
10114 tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
10115 tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
10116 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
10117
67255d04
RE
10118 break;
10119
10120 case BFD_ENDIAN_LITTLE:
66e810cd
RE
10121 tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
10122 tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
10123 tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
10124 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
10125
67255d04
RE
10126 break;
10127
10128 default:
10129 internal_error (__FILE__, __LINE__,
edefbb7c 10130 _("arm_gdbarch_init: bad byte order for float format"));
67255d04
RE
10131 }
10132
d7b486e7
RE
10133 /* On ARM targets char defaults to unsigned. */
10134 set_gdbarch_char_signed (gdbarch, 0);
10135
cca44b1b
JB
10136 /* Note: for displaced stepping, this includes the breakpoint, and one word
10137 of additional scratch space. This setting isn't used for anything beside
10138 displaced stepping at present. */
10139 set_gdbarch_max_insn_length (gdbarch, 4 * DISPLACED_MODIFIED_INSNS);
10140
9df628e0 10141 /* This should be low enough for everything. */
97e03143 10142 tdep->lowest_pc = 0x20;
94c30b78 10143 tdep->jb_pc = -1; /* Longjump support not enabled by default. */
97e03143 10144
7c00367c
MK
10145 /* The default, for both APCS and AAPCS, is to return small
10146 structures in registers. */
10147 tdep->struct_return = reg_struct_return;
10148
2dd604e7 10149 set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
f53f0d0b 10150 set_gdbarch_frame_align (gdbarch, arm_frame_align);
39bbf761 10151
756fe439
DJ
10152 set_gdbarch_write_pc (gdbarch, arm_write_pc);
10153
148754e5 10154 /* Frame handling. */
a262aec2 10155 set_gdbarch_dummy_id (gdbarch, arm_dummy_id);
eb5492fa
DJ
10156 set_gdbarch_unwind_pc (gdbarch, arm_unwind_pc);
10157 set_gdbarch_unwind_sp (gdbarch, arm_unwind_sp);
10158
eb5492fa 10159 frame_base_set_default (gdbarch, &arm_normal_base);
148754e5 10160
34e8f22d
RE
10161 /* Address manipulation. */
10162 set_gdbarch_smash_text_address (gdbarch, arm_smash_text_address);
10163 set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
10164
34e8f22d
RE
10165 /* Advance PC across function entry code. */
10166 set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
10167
4024ca99
UW
10168 /* Detect whether PC is in function epilogue. */
10169 set_gdbarch_in_function_epilogue_p (gdbarch, arm_in_function_epilogue_p);
10170
190dce09
UW
10171 /* Skip trampolines. */
10172 set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
10173
34e8f22d
RE
10174 /* The stack grows downward. */
10175 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
10176
10177 /* Breakpoint manipulation. */
10178 set_gdbarch_breakpoint_from_pc (gdbarch, arm_breakpoint_from_pc);
177321bd
DJ
10179 set_gdbarch_remote_breakpoint_from_pc (gdbarch,
10180 arm_remote_breakpoint_from_pc);
34e8f22d
RE
10181
10182 /* Information about registers, etc. */
34e8f22d
RE
10183 set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
10184 set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
ff6f572f 10185 set_gdbarch_num_regs (gdbarch, ARM_NUM_REGS);
7a5ea0d4 10186 set_gdbarch_register_type (gdbarch, arm_register_type);
54483882 10187 set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);
34e8f22d 10188
ff6f572f
DJ
10189 /* This "info float" is FPA-specific. Use the generic version if we
10190 do not have FPA. */
10191 if (gdbarch_tdep (gdbarch)->have_fpa_registers)
10192 set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
10193
26216b98 10194 /* Internal <-> external register number maps. */
ff6f572f 10195 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
26216b98
AC
10196 set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
10197
34e8f22d
RE
10198 set_gdbarch_register_name (gdbarch, arm_register_name);
10199
10200 /* Returning results. */
2af48f68 10201 set_gdbarch_return_value (gdbarch, arm_return_value);
34e8f22d 10202
03d48a7d
RE
10203 /* Disassembly. */
10204 set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
10205
34e8f22d
RE
10206 /* Minsymbol frobbing. */
10207 set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
10208 set_gdbarch_coff_make_msymbol_special (gdbarch,
10209 arm_coff_make_msymbol_special);
60c5725c 10210 set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
34e8f22d 10211
f9d67f43
DJ
10212 /* Thumb-2 IT block support. */
10213 set_gdbarch_adjust_breakpoint_address (gdbarch,
10214 arm_adjust_breakpoint_address);
10215
0d5de010
DJ
10216 /* Virtual tables. */
10217 set_gdbarch_vbit_in_delta (gdbarch, 1);
10218
97e03143 10219 /* Hook in the ABI-specific overrides, if they have been registered. */
4be87837 10220 gdbarch_init_osabi (info, gdbarch);
97e03143 10221
b39cc962
DJ
10222 dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
10223
eb5492fa 10224 /* Add some default predicates. */
a262aec2
DJ
10225 frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
10226 dwarf2_append_unwinders (gdbarch);
0e9e9abd 10227 frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
a262aec2 10228 frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
eb5492fa 10229
97e03143
RE
10230 /* Now we have tuned the configuration, set a few final things,
10231 based on what the OS ABI has told us. */
10232
b8926edc
DJ
10233 /* If the ABI is not otherwise marked, assume the old GNU APCS. EABI
10234 binaries are always marked. */
10235 if (tdep->arm_abi == ARM_ABI_AUTO)
10236 tdep->arm_abi = ARM_ABI_APCS;
10237
e3039479
UW
10238 /* Watchpoints are not steppable. */
10239 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
10240
b8926edc
DJ
10241 /* We used to default to FPA for generic ARM, but almost nobody
10242 uses that now, and we now provide a way for the user to force
10243 the model. So default to the most useful variant. */
10244 if (tdep->fp_model == ARM_FLOAT_AUTO)
10245 tdep->fp_model = ARM_FLOAT_SOFT_FPA;
10246
9df628e0
RE
10247 if (tdep->jb_pc >= 0)
10248 set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
10249
08216dd7 10250 /* Floating point sizes and format. */
8da61cc4 10251 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
b8926edc 10252 if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
08216dd7 10253 {
8da61cc4
DJ
10254 set_gdbarch_double_format
10255 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10256 set_gdbarch_long_double_format
10257 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10258 }
10259 else
10260 {
10261 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
10262 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
08216dd7
RE
10263 }
10264
58d6951d
DJ
10265 if (have_vfp_pseudos)
10266 {
10267 /* NOTE: These are the only pseudo registers used by
10268 the ARM target at the moment. If more are added, a
10269 little more care in numbering will be needed. */
10270
10271 int num_pseudos = 32;
10272 if (have_neon_pseudos)
10273 num_pseudos += 16;
10274 set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
10275 set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
10276 set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
10277 }
10278
123dc839 10279 if (tdesc_data)
58d6951d
DJ
10280 {
10281 set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
10282
9779414d 10283 tdesc_use_registers (gdbarch, tdesc, tdesc_data);
58d6951d
DJ
10284
10285 /* Override tdesc_register_type to adjust the types of VFP
10286 registers for NEON. */
10287 set_gdbarch_register_type (gdbarch, arm_register_type);
10288 }
123dc839
DJ
10289
10290 /* Add standard register aliases. We add aliases even for those
10291 nanes which are used by the current architecture - it's simpler,
10292 and does no harm, since nothing ever lists user registers. */
10293 for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
10294 user_reg_add (gdbarch, arm_register_aliases[i].name,
10295 value_of_arm_user_reg, &arm_register_aliases[i].regnum);
10296
39bbf761
RE
10297 return gdbarch;
10298}
10299
97e03143 10300static void
2af46ca0 10301arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
97e03143 10302{
2af46ca0 10303 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
97e03143
RE
10304
10305 if (tdep == NULL)
10306 return;
10307
edefbb7c 10308 fprintf_unfiltered (file, _("arm_dump_tdep: Lowest pc = 0x%lx"),
97e03143
RE
10309 (unsigned long) tdep->lowest_pc);
10310}
10311
a78f21af
AC
10312extern initialize_file_ftype _initialize_arm_tdep; /* -Wmissing-prototypes */
10313
c906108c 10314void
ed9a39eb 10315_initialize_arm_tdep (void)
c906108c 10316{
bc90b915
FN
10317 struct ui_file *stb;
10318 long length;
26304000 10319 struct cmd_list_element *new_set, *new_show;
53904c9e
AC
10320 const char *setname;
10321 const char *setdesc;
4bd7b427 10322 const char *const *regnames;
bc90b915
FN
10323 int numregs, i, j;
10324 static char *helptext;
edefbb7c
AC
10325 char regdesc[1024], *rdptr = regdesc;
10326 size_t rest = sizeof (regdesc);
085dd6e6 10327
42cf1509 10328 gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
97e03143 10329
60c5725c 10330 arm_objfile_data_key
c1bd65d0 10331 = register_objfile_data_with_cleanup (NULL, arm_objfile_data_free);
60c5725c 10332
0e9e9abd
UW
10333 /* Add ourselves to objfile event chain. */
10334 observer_attach_new_objfile (arm_exidx_new_objfile);
10335 arm_exidx_data_key
10336 = register_objfile_data_with_cleanup (NULL, arm_exidx_data_free);
10337
70f80edf
JT
10338 /* Register an ELF OS ABI sniffer for ARM binaries. */
10339 gdbarch_register_osabi_sniffer (bfd_arch_arm,
10340 bfd_target_elf_flavour,
10341 arm_elf_osabi_sniffer);
10342
9779414d
DJ
10343 /* Initialize the standard target descriptions. */
10344 initialize_tdesc_arm_with_m ();
25f8c692 10345 initialize_tdesc_arm_with_m_fpa_layout ();
3184d3f9 10346 initialize_tdesc_arm_with_m_vfp_d16 ();
ef7e8358
UW
10347 initialize_tdesc_arm_with_iwmmxt ();
10348 initialize_tdesc_arm_with_vfpv2 ();
10349 initialize_tdesc_arm_with_vfpv3 ();
10350 initialize_tdesc_arm_with_neon ();
9779414d 10351
94c30b78 10352 /* Get the number of possible sets of register names defined in opcodes. */
afd7eef0
RE
10353 num_disassembly_options = get_arm_regname_num_options ();
10354
10355 /* Add root prefix command for all "set arm"/"show arm" commands. */
10356 add_prefix_cmd ("arm", no_class, set_arm_command,
edefbb7c 10357 _("Various ARM-specific commands."),
afd7eef0
RE
10358 &setarmcmdlist, "set arm ", 0, &setlist);
10359
10360 add_prefix_cmd ("arm", no_class, show_arm_command,
edefbb7c 10361 _("Various ARM-specific commands."),
afd7eef0 10362 &showarmcmdlist, "show arm ", 0, &showlist);
bc90b915 10363
94c30b78 10364 /* Sync the opcode insn printer with our register viewer. */
bc90b915 10365 parse_arm_disassembler_option ("reg-names-std");
c5aa993b 10366
eefe576e
AC
10367 /* Initialize the array that will be passed to
10368 add_setshow_enum_cmd(). */
afd7eef0
RE
10369 valid_disassembly_styles
10370 = xmalloc ((num_disassembly_options + 1) * sizeof (char *));
10371 for (i = 0; i < num_disassembly_options; i++)
bc90b915
FN
10372 {
10373 numregs = get_arm_regnames (i, &setname, &setdesc, &regnames);
afd7eef0 10374 valid_disassembly_styles[i] = setname;
edefbb7c
AC
10375 length = snprintf (rdptr, rest, "%s - %s\n", setname, setdesc);
10376 rdptr += length;
10377 rest -= length;
123dc839
DJ
10378 /* When we find the default names, tell the disassembler to use
10379 them. */
bc90b915
FN
10380 if (!strcmp (setname, "std"))
10381 {
afd7eef0 10382 disassembly_style = setname;
bc90b915
FN
10383 set_arm_regname_option (i);
10384 }
10385 }
94c30b78 10386 /* Mark the end of valid options. */
afd7eef0 10387 valid_disassembly_styles[num_disassembly_options] = NULL;
c906108c 10388
edefbb7c
AC
10389 /* Create the help text. */
10390 stb = mem_fileopen ();
10391 fprintf_unfiltered (stb, "%s%s%s",
10392 _("The valid values are:\n"),
10393 regdesc,
10394 _("The default is \"std\"."));
759ef836 10395 helptext = ui_file_xstrdup (stb, NULL);
bc90b915 10396 ui_file_delete (stb);
ed9a39eb 10397
edefbb7c
AC
10398 add_setshow_enum_cmd("disassembler", no_class,
10399 valid_disassembly_styles, &disassembly_style,
10400 _("Set the disassembly style."),
10401 _("Show the disassembly style."),
10402 helptext,
2c5b56ce 10403 set_disassembly_style_sfunc,
0963b4bd
MS
10404 NULL, /* FIXME: i18n: The disassembly style is
10405 \"%s\". */
7376b4c2 10406 &setarmcmdlist, &showarmcmdlist);
edefbb7c
AC
10407
10408 add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
10409 _("Set usage of ARM 32-bit mode."),
10410 _("Show usage of ARM 32-bit mode."),
10411 _("When off, a 26-bit PC will be used."),
2c5b56ce 10412 NULL,
0963b4bd
MS
10413 NULL, /* FIXME: i18n: Usage of ARM 32-bit
10414 mode is %s. */
26304000 10415 &setarmcmdlist, &showarmcmdlist);
c906108c 10416
fd50bc42 10417 /* Add a command to allow the user to force the FPU model. */
edefbb7c
AC
10418 add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
10419 _("Set the floating point type."),
10420 _("Show the floating point type."),
10421 _("auto - Determine the FP typefrom the OS-ABI.\n\
10422softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
10423fpa - FPA co-processor (GCC compiled).\n\
10424softvfp - Software FP with pure-endian doubles.\n\
10425vfp - VFP co-processor."),
edefbb7c 10426 set_fp_model_sfunc, show_fp_model,
7376b4c2 10427 &setarmcmdlist, &showarmcmdlist);
fd50bc42 10428
28e97307
DJ
10429 /* Add a command to allow the user to force the ABI. */
10430 add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
10431 _("Set the ABI."),
10432 _("Show the ABI."),
10433 NULL, arm_set_abi, arm_show_abi,
10434 &setarmcmdlist, &showarmcmdlist);
10435
0428b8f5
DJ
10436 /* Add two commands to allow the user to force the assumed
10437 execution mode. */
10438 add_setshow_enum_cmd ("fallback-mode", class_support,
10439 arm_mode_strings, &arm_fallback_mode_string,
10440 _("Set the mode assumed when symbols are unavailable."),
10441 _("Show the mode assumed when symbols are unavailable."),
10442 NULL, NULL, arm_show_fallback_mode,
10443 &setarmcmdlist, &showarmcmdlist);
10444 add_setshow_enum_cmd ("force-mode", class_support,
10445 arm_mode_strings, &arm_force_mode_string,
10446 _("Set the mode assumed even when symbols are available."),
10447 _("Show the mode assumed even when symbols are available."),
10448 NULL, NULL, arm_show_force_mode,
10449 &setarmcmdlist, &showarmcmdlist);
10450
6529d2dd 10451 /* Debugging flag. */
edefbb7c
AC
10452 add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
10453 _("Set ARM debugging."),
10454 _("Show ARM debugging."),
10455 _("When on, arm-specific debugging is enabled."),
2c5b56ce 10456 NULL,
7915a72c 10457 NULL, /* FIXME: i18n: "ARM debugging is %s. */
26304000 10458 &setdebuglist, &showdebuglist);
c906108c 10459}
72508ac0
PO
10460
10461/* ARM-reversible process record data structures. */
10462
10463#define ARM_INSN_SIZE_BYTES 4
10464#define THUMB_INSN_SIZE_BYTES 2
10465#define THUMB2_INSN_SIZE_BYTES 4
10466
10467
10468#define INSN_S_L_BIT_NUM 20
10469
10470#define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
10471 do \
10472 { \
10473 unsigned int reg_len = LENGTH; \
10474 if (reg_len) \
10475 { \
10476 REGS = XNEWVEC (uint32_t, reg_len); \
10477 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
10478 } \
10479 } \
10480 while (0)
10481
10482#define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
10483 do \
10484 { \
10485 unsigned int mem_len = LENGTH; \
10486 if (mem_len) \
10487 { \
10488 MEMS = XNEWVEC (struct arm_mem_r, mem_len); \
10489 memcpy(&MEMS->len, &RECORD_BUF[0], \
10490 sizeof(struct arm_mem_r) * LENGTH); \
10491 } \
10492 } \
10493 while (0)
10494
10495/* Checks whether insn is already recorded or yet to be decoded. (boolean expression). */
10496#define INSN_RECORDED(ARM_RECORD) \
10497 (0 != (ARM_RECORD)->reg_rec_count || 0 != (ARM_RECORD)->mem_rec_count)
10498
10499/* ARM memory record structure. */
10500struct arm_mem_r
10501{
10502 uint32_t len; /* Record length. */
10503 CORE_ADDR addr; /* Memory address. */
10504};
10505
10506/* ARM instruction record contains opcode of current insn
10507 and execution state (before entry to decode_insn()),
10508 contains list of to-be-modified registers and
10509 memory blocks (on return from decode_insn()). */
10510
10511typedef struct insn_decode_record_t
10512{
10513 struct gdbarch *gdbarch;
10514 struct regcache *regcache;
10515 CORE_ADDR this_addr; /* Address of the insn being decoded. */
10516 uint32_t arm_insn; /* Should accommodate thumb. */
10517 uint32_t cond; /* Condition code. */
10518 uint32_t opcode; /* Insn opcode. */
10519 uint32_t decode; /* Insn decode bits. */
10520 uint32_t mem_rec_count; /* No of mem records. */
10521 uint32_t reg_rec_count; /* No of reg records. */
10522 uint32_t *arm_regs; /* Registers to be saved for this record. */
10523 struct arm_mem_r *arm_mems; /* Memory to be saved for this record. */
10524} insn_decode_record;
10525
10526
10527/* Checks ARM SBZ and SBO mandatory fields. */
10528
10529static int
10530sbo_sbz (uint32_t insn, uint32_t bit_num, uint32_t len, uint32_t sbo)
10531{
10532 uint32_t ones = bits (insn, bit_num - 1, (bit_num -1) + (len - 1));
10533
10534 if (!len)
10535 return 1;
10536
10537 if (!sbo)
10538 ones = ~ones;
10539
10540 while (ones)
10541 {
10542 if (!(ones & sbo))
10543 {
10544 return 0;
10545 }
10546 ones = ones >> 1;
10547 }
10548 return 1;
10549}
10550
10551typedef enum
10552{
10553 ARM_RECORD_STRH=1,
10554 ARM_RECORD_STRD
10555} arm_record_strx_t;
10556
10557typedef enum
10558{
10559 ARM_RECORD=1,
10560 THUMB_RECORD,
10561 THUMB2_RECORD
10562} record_type_t;
10563
10564
10565static int
10566arm_record_strx (insn_decode_record *arm_insn_r, uint32_t *record_buf,
10567 uint32_t *record_buf_mem, arm_record_strx_t str_type)
10568{
10569
10570 struct regcache *reg_cache = arm_insn_r->regcache;
10571 ULONGEST u_regval[2]= {0};
10572
10573 uint32_t reg_src1 = 0, reg_src2 = 0;
10574 uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
10575 uint32_t opcode1 = 0;
10576
10577 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10578 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10579 opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
10580
10581
10582 if (14 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
10583 {
10584 /* 1) Handle misc store, immediate offset. */
10585 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
10586 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
10587 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10588 regcache_raw_read_unsigned (reg_cache, reg_src1,
10589 &u_regval[0]);
10590 if (ARM_PC_REGNUM == reg_src1)
10591 {
10592 /* If R15 was used as Rn, hence current PC+8. */
10593 u_regval[0] = u_regval[0] + 8;
10594 }
10595 offset_8 = (immed_high << 4) | immed_low;
10596 /* Calculate target store address. */
10597 if (14 == arm_insn_r->opcode)
10598 {
10599 tgt_mem_addr = u_regval[0] + offset_8;
10600 }
10601 else
10602 {
10603 tgt_mem_addr = u_regval[0] - offset_8;
10604 }
10605 if (ARM_RECORD_STRH == str_type)
10606 {
10607 record_buf_mem[0] = 2;
10608 record_buf_mem[1] = tgt_mem_addr;
10609 arm_insn_r->mem_rec_count = 1;
10610 }
10611 else if (ARM_RECORD_STRD == str_type)
10612 {
10613 record_buf_mem[0] = 4;
10614 record_buf_mem[1] = tgt_mem_addr;
10615 record_buf_mem[2] = 4;
10616 record_buf_mem[3] = tgt_mem_addr + 4;
10617 arm_insn_r->mem_rec_count = 2;
10618 }
10619 }
10620 else if (12 == arm_insn_r->opcode || 8 == arm_insn_r->opcode)
10621 {
10622 /* 2) Store, register offset. */
10623 /* Get Rm. */
10624 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10625 /* Get Rn. */
10626 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10627 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10628 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10629 if (15 == reg_src2)
10630 {
10631 /* If R15 was used as Rn, hence current PC+8. */
10632 u_regval[0] = u_regval[0] + 8;
10633 }
10634 /* Calculate target store address, Rn +/- Rm, register offset. */
10635 if (12 == arm_insn_r->opcode)
10636 {
10637 tgt_mem_addr = u_regval[0] + u_regval[1];
10638 }
10639 else
10640 {
10641 tgt_mem_addr = u_regval[1] - u_regval[0];
10642 }
10643 if (ARM_RECORD_STRH == str_type)
10644 {
10645 record_buf_mem[0] = 2;
10646 record_buf_mem[1] = tgt_mem_addr;
10647 arm_insn_r->mem_rec_count = 1;
10648 }
10649 else if (ARM_RECORD_STRD == str_type)
10650 {
10651 record_buf_mem[0] = 4;
10652 record_buf_mem[1] = tgt_mem_addr;
10653 record_buf_mem[2] = 4;
10654 record_buf_mem[3] = tgt_mem_addr + 4;
10655 arm_insn_r->mem_rec_count = 2;
10656 }
10657 }
10658 else if (11 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
10659 || 2 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
10660 {
10661 /* 3) Store, immediate pre-indexed. */
10662 /* 5) Store, immediate post-indexed. */
10663 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
10664 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
10665 offset_8 = (immed_high << 4) | immed_low;
10666 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10667 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10668 /* Calculate target store address, Rn +/- Rm, register offset. */
10669 if (15 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
10670 {
10671 tgt_mem_addr = u_regval[0] + offset_8;
10672 }
10673 else
10674 {
10675 tgt_mem_addr = u_regval[0] - offset_8;
10676 }
10677 if (ARM_RECORD_STRH == str_type)
10678 {
10679 record_buf_mem[0] = 2;
10680 record_buf_mem[1] = tgt_mem_addr;
10681 arm_insn_r->mem_rec_count = 1;
10682 }
10683 else if (ARM_RECORD_STRD == str_type)
10684 {
10685 record_buf_mem[0] = 4;
10686 record_buf_mem[1] = tgt_mem_addr;
10687 record_buf_mem[2] = 4;
10688 record_buf_mem[3] = tgt_mem_addr + 4;
10689 arm_insn_r->mem_rec_count = 2;
10690 }
10691 /* Record Rn also as it changes. */
10692 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
10693 arm_insn_r->reg_rec_count = 1;
10694 }
10695 else if (9 == arm_insn_r->opcode || 13 == arm_insn_r->opcode
10696 || 0 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
10697 {
10698 /* 4) Store, register pre-indexed. */
10699 /* 6) Store, register post -indexed. */
10700 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10701 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10702 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10703 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10704 /* Calculate target store address, Rn +/- Rm, register offset. */
10705 if (13 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
10706 {
10707 tgt_mem_addr = u_regval[0] + u_regval[1];
10708 }
10709 else
10710 {
10711 tgt_mem_addr = u_regval[1] - u_regval[0];
10712 }
10713 if (ARM_RECORD_STRH == str_type)
10714 {
10715 record_buf_mem[0] = 2;
10716 record_buf_mem[1] = tgt_mem_addr;
10717 arm_insn_r->mem_rec_count = 1;
10718 }
10719 else if (ARM_RECORD_STRD == str_type)
10720 {
10721 record_buf_mem[0] = 4;
10722 record_buf_mem[1] = tgt_mem_addr;
10723 record_buf_mem[2] = 4;
10724 record_buf_mem[3] = tgt_mem_addr + 4;
10725 arm_insn_r->mem_rec_count = 2;
10726 }
10727 /* Record Rn also as it changes. */
10728 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
10729 arm_insn_r->reg_rec_count = 1;
10730 }
10731 return 0;
10732}
10733
10734/* Handling ARM extension space insns. */
10735
10736static int
10737arm_record_extension_space (insn_decode_record *arm_insn_r)
10738{
10739 uint32_t ret = 0; /* Return value: -1:record failure ; 0:success */
10740 uint32_t opcode1 = 0, opcode2 = 0, insn_op1 = 0;
10741 uint32_t record_buf[8], record_buf_mem[8];
10742 uint32_t reg_src1 = 0;
10743 uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
10744 struct regcache *reg_cache = arm_insn_r->regcache;
10745 ULONGEST u_regval = 0;
10746
10747 gdb_assert (!INSN_RECORDED(arm_insn_r));
10748 /* Handle unconditional insn extension space. */
10749
10750 opcode1 = bits (arm_insn_r->arm_insn, 20, 27);
10751 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
10752 if (arm_insn_r->cond)
10753 {
10754 /* PLD has no affect on architectural state, it just affects
10755 the caches. */
10756 if (5 == ((opcode1 & 0xE0) >> 5))
10757 {
10758 /* BLX(1) */
10759 record_buf[0] = ARM_PS_REGNUM;
10760 record_buf[1] = ARM_LR_REGNUM;
10761 arm_insn_r->reg_rec_count = 2;
10762 }
10763 /* STC2, LDC2, MCR2, MRC2, CDP2: <TBD>, co-processor insn. */
10764 }
10765
10766
10767 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
10768 if (3 == opcode1 && bit (arm_insn_r->arm_insn, 4))
10769 {
10770 ret = -1;
10771 /* Undefined instruction on ARM V5; need to handle if later
10772 versions define it. */
10773 }
10774
10775 opcode1 = bits (arm_insn_r->arm_insn, 24, 27);
10776 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
10777 insn_op1 = bits (arm_insn_r->arm_insn, 20, 23);
10778
10779 /* Handle arithmetic insn extension space. */
10780 if (!opcode1 && 9 == opcode2 && 1 != arm_insn_r->cond
10781 && !INSN_RECORDED(arm_insn_r))
10782 {
10783 /* Handle MLA(S) and MUL(S). */
10784 if (0 <= insn_op1 && 3 >= insn_op1)
10785 {
10786 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10787 record_buf[1] = ARM_PS_REGNUM;
10788 arm_insn_r->reg_rec_count = 2;
10789 }
10790 else if (4 <= insn_op1 && 15 >= insn_op1)
10791 {
10792 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
10793 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
10794 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
10795 record_buf[2] = ARM_PS_REGNUM;
10796 arm_insn_r->reg_rec_count = 3;
10797 }
10798 }
10799
10800 opcode1 = bits (arm_insn_r->arm_insn, 26, 27);
10801 opcode2 = bits (arm_insn_r->arm_insn, 23, 24);
10802 insn_op1 = bits (arm_insn_r->arm_insn, 21, 22);
10803
10804 /* Handle control insn extension space. */
10805
10806 if (!opcode1 && 2 == opcode2 && !bit (arm_insn_r->arm_insn, 20)
10807 && 1 != arm_insn_r->cond && !INSN_RECORDED(arm_insn_r))
10808 {
10809 if (!bit (arm_insn_r->arm_insn,25))
10810 {
10811 if (!bits (arm_insn_r->arm_insn, 4, 7))
10812 {
10813 if ((0 == insn_op1) || (2 == insn_op1))
10814 {
10815 /* MRS. */
10816 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10817 arm_insn_r->reg_rec_count = 1;
10818 }
10819 else if (1 == insn_op1)
10820 {
10821 /* CSPR is going to be changed. */
10822 record_buf[0] = ARM_PS_REGNUM;
10823 arm_insn_r->reg_rec_count = 1;
10824 }
10825 else if (3 == insn_op1)
10826 {
10827 /* SPSR is going to be changed. */
10828 /* We need to get SPSR value, which is yet to be done. */
10829 printf_unfiltered (_("Process record does not support "
10830 "instruction 0x%0x at address %s.\n"),
10831 arm_insn_r->arm_insn,
10832 paddress (arm_insn_r->gdbarch,
10833 arm_insn_r->this_addr));
10834 return -1;
10835 }
10836 }
10837 else if (1 == bits (arm_insn_r->arm_insn, 4, 7))
10838 {
10839 if (1 == insn_op1)
10840 {
10841 /* BX. */
10842 record_buf[0] = ARM_PS_REGNUM;
10843 arm_insn_r->reg_rec_count = 1;
10844 }
10845 else if (3 == insn_op1)
10846 {
10847 /* CLZ. */
10848 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10849 arm_insn_r->reg_rec_count = 1;
10850 }
10851 }
10852 else if (3 == bits (arm_insn_r->arm_insn, 4, 7))
10853 {
10854 /* BLX. */
10855 record_buf[0] = ARM_PS_REGNUM;
10856 record_buf[1] = ARM_LR_REGNUM;
10857 arm_insn_r->reg_rec_count = 2;
10858 }
10859 else if (5 == bits (arm_insn_r->arm_insn, 4, 7))
10860 {
10861 /* QADD, QSUB, QDADD, QDSUB */
10862 record_buf[0] = ARM_PS_REGNUM;
10863 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
10864 arm_insn_r->reg_rec_count = 2;
10865 }
10866 else if (7 == bits (arm_insn_r->arm_insn, 4, 7))
10867 {
10868 /* BKPT. */
10869 record_buf[0] = ARM_PS_REGNUM;
10870 record_buf[1] = ARM_LR_REGNUM;
10871 arm_insn_r->reg_rec_count = 2;
10872
10873 /* Save SPSR also;how? */
10874 printf_unfiltered (_("Process record does not support "
10875 "instruction 0x%0x at address %s.\n"),
10876 arm_insn_r->arm_insn,
10877 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
10878 return -1;
10879 }
10880 else if(8 == bits (arm_insn_r->arm_insn, 4, 7)
10881 || 10 == bits (arm_insn_r->arm_insn, 4, 7)
10882 || 12 == bits (arm_insn_r->arm_insn, 4, 7)
10883 || 14 == bits (arm_insn_r->arm_insn, 4, 7)
10884 )
10885 {
10886 if (0 == insn_op1 || 1 == insn_op1)
10887 {
10888 /* SMLA<x><y>, SMLAW<y>, SMULW<y>. */
10889 /* We dont do optimization for SMULW<y> where we
10890 need only Rd. */
10891 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10892 record_buf[1] = ARM_PS_REGNUM;
10893 arm_insn_r->reg_rec_count = 2;
10894 }
10895 else if (2 == insn_op1)
10896 {
10897 /* SMLAL<x><y>. */
10898 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10899 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
10900 arm_insn_r->reg_rec_count = 2;
10901 }
10902 else if (3 == insn_op1)
10903 {
10904 /* SMUL<x><y>. */
10905 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10906 arm_insn_r->reg_rec_count = 1;
10907 }
10908 }
10909 }
10910 else
10911 {
10912 /* MSR : immediate form. */
10913 if (1 == insn_op1)
10914 {
10915 /* CSPR is going to be changed. */
10916 record_buf[0] = ARM_PS_REGNUM;
10917 arm_insn_r->reg_rec_count = 1;
10918 }
10919 else if (3 == insn_op1)
10920 {
10921 /* SPSR is going to be changed. */
10922 /* we need to get SPSR value, which is yet to be done */
10923 printf_unfiltered (_("Process record does not support "
10924 "instruction 0x%0x at address %s.\n"),
10925 arm_insn_r->arm_insn,
10926 paddress (arm_insn_r->gdbarch,
10927 arm_insn_r->this_addr));
10928 return -1;
10929 }
10930 }
10931 }
10932
10933 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
10934 opcode2 = bits (arm_insn_r->arm_insn, 20, 24);
10935 insn_op1 = bits (arm_insn_r->arm_insn, 5, 6);
10936
10937 /* Handle load/store insn extension space. */
10938
10939 if (!opcode1 && bit (arm_insn_r->arm_insn, 7)
10940 && bit (arm_insn_r->arm_insn, 4) && 1 != arm_insn_r->cond
10941 && !INSN_RECORDED(arm_insn_r))
10942 {
10943 /* SWP/SWPB. */
10944 if (0 == insn_op1)
10945 {
10946 /* These insn, changes register and memory as well. */
10947 /* SWP or SWPB insn. */
10948 /* Get memory address given by Rn. */
10949 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10950 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
10951 /* SWP insn ?, swaps word. */
10952 if (8 == arm_insn_r->opcode)
10953 {
10954 record_buf_mem[0] = 4;
10955 }
10956 else
10957 {
10958 /* SWPB insn, swaps only byte. */
10959 record_buf_mem[0] = 1;
10960 }
10961 record_buf_mem[1] = u_regval;
10962 arm_insn_r->mem_rec_count = 1;
10963 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10964 arm_insn_r->reg_rec_count = 1;
10965 }
10966 else if (1 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
10967 {
10968 /* STRH. */
10969 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
10970 ARM_RECORD_STRH);
10971 }
10972 else if (2 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
10973 {
10974 /* LDRD. */
10975 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10976 record_buf[1] = record_buf[0] + 1;
10977 arm_insn_r->reg_rec_count = 2;
10978 }
10979 else if (3 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
10980 {
10981 /* STRD. */
10982 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
10983 ARM_RECORD_STRD);
10984 }
10985 else if (bit (arm_insn_r->arm_insn, 20) && insn_op1 <= 3)
10986 {
10987 /* LDRH, LDRSB, LDRSH. */
10988 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10989 arm_insn_r->reg_rec_count = 1;
10990 }
10991
10992 }
10993
10994 opcode1 = bits (arm_insn_r->arm_insn, 23, 27);
10995 if (24 == opcode1 && bit (arm_insn_r->arm_insn, 21)
10996 && !INSN_RECORDED(arm_insn_r))
10997 {
10998 ret = -1;
10999 /* Handle coprocessor insn extension space. */
11000 }
11001
11002 /* To be done for ARMv5 and later; as of now we return -1. */
11003 if (-1 == ret)
11004 printf_unfiltered (_("Process record does not support instruction x%0x "
11005 "at address %s.\n"),arm_insn_r->arm_insn,
11006 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11007
11008
11009 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11010 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11011
11012 return ret;
11013}
11014
11015/* Handling opcode 000 insns. */
11016
11017static int
11018arm_record_data_proc_misc_ld_str (insn_decode_record *arm_insn_r)
11019{
11020 struct regcache *reg_cache = arm_insn_r->regcache;
11021 uint32_t record_buf[8], record_buf_mem[8];
11022 ULONGEST u_regval[2] = {0};
11023
11024 uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
11025 uint32_t immed_high = 0, immed_low = 0, offset_8 = 0, tgt_mem_addr = 0;
11026 uint32_t opcode1 = 0;
11027
11028 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11029 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11030 opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
11031
11032 /* Data processing insn /multiply insn. */
11033 if (9 == arm_insn_r->decode
11034 && ((4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11035 || (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)))
11036 {
11037 /* Handle multiply instructions. */
11038 /* MLA, MUL, SMLAL, SMULL, UMLAL, UMULL. */
11039 if (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)
11040 {
11041 /* Handle MLA and MUL. */
11042 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11043 record_buf[1] = ARM_PS_REGNUM;
11044 arm_insn_r->reg_rec_count = 2;
11045 }
11046 else if (4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11047 {
11048 /* Handle SMLAL, SMULL, UMLAL, UMULL. */
11049 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11050 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11051 record_buf[2] = ARM_PS_REGNUM;
11052 arm_insn_r->reg_rec_count = 3;
11053 }
11054 }
11055 else if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11056 && (11 == arm_insn_r->decode || 13 == arm_insn_r->decode))
11057 {
11058 /* Handle misc load insns, as 20th bit (L = 1). */
11059 /* LDR insn has a capability to do branching, if
11060 MOV LR, PC is precceded by LDR insn having Rn as R15
11061 in that case, it emulates branch and link insn, and hence we
11062 need to save CSPR and PC as well. I am not sure this is right
11063 place; as opcode = 010 LDR insn make this happen, if R15 was
11064 used. */
11065 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11066 if (15 != reg_dest)
11067 {
11068 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11069 arm_insn_r->reg_rec_count = 1;
11070 }
11071 else
11072 {
11073 record_buf[0] = reg_dest;
11074 record_buf[1] = ARM_PS_REGNUM;
11075 arm_insn_r->reg_rec_count = 2;
11076 }
11077 }
11078 else if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11079 && sbo_sbz (arm_insn_r->arm_insn, 5, 12, 0)
11080 && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
11081 && 2 == bits (arm_insn_r->arm_insn, 20, 21))
11082 {
11083 /* Handle MSR insn. */
11084 if (9 == arm_insn_r->opcode)
11085 {
11086 /* CSPR is going to be changed. */
11087 record_buf[0] = ARM_PS_REGNUM;
11088 arm_insn_r->reg_rec_count = 1;
11089 }
11090 else
11091 {
11092 /* SPSR is going to be changed. */
11093 /* How to read SPSR value? */
11094 printf_unfiltered (_("Process record does not support instruction "
11095 "0x%0x at address %s.\n"),
11096 arm_insn_r->arm_insn,
11097 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11098 return -1;
11099 }
11100 }
11101 else if (9 == arm_insn_r->decode
11102 && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11103 && !bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11104 {
11105 /* Handling SWP, SWPB. */
11106 /* These insn, changes register and memory as well. */
11107 /* SWP or SWPB insn. */
11108
11109 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11110 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11111 /* SWP insn ?, swaps word. */
11112 if (8 == arm_insn_r->opcode)
11113 {
11114 record_buf_mem[0] = 4;
11115 }
11116 else
11117 {
11118 /* SWPB insn, swaps only byte. */
11119 record_buf_mem[0] = 1;
11120 }
11121 record_buf_mem[1] = u_regval[0];
11122 arm_insn_r->mem_rec_count = 1;
11123 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11124 arm_insn_r->reg_rec_count = 1;
11125 }
11126 else if (3 == arm_insn_r->decode && 0x12 == opcode1
11127 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11128 {
11129 /* Handle BLX, branch and link/exchange. */
11130 if (9 == arm_insn_r->opcode)
11131 {
11132 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm,
11133 and R14 stores the return address. */
11134 record_buf[0] = ARM_PS_REGNUM;
11135 record_buf[1] = ARM_LR_REGNUM;
11136 arm_insn_r->reg_rec_count = 2;
11137 }
11138 }
11139 else if (7 == arm_insn_r->decode && 0x12 == opcode1)
11140 {
11141 /* Handle enhanced software breakpoint insn, BKPT. */
11142 /* CPSR is changed to be executed in ARM state, disabling normal
11143 interrupts, entering abort mode. */
11144 /* According to high vector configuration PC is set. */
11145 /* user hit breakpoint and type reverse, in
11146 that case, we need to go back with previous CPSR and
11147 Program Counter. */
11148 record_buf[0] = ARM_PS_REGNUM;
11149 record_buf[1] = ARM_LR_REGNUM;
11150 arm_insn_r->reg_rec_count = 2;
11151
11152 /* Save SPSR also; how? */
11153 printf_unfiltered (_("Process record does not support instruction "
11154 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
11155 paddress (arm_insn_r->gdbarch,
11156 arm_insn_r->this_addr));
11157 return -1;
11158 }
11159 else if (11 == arm_insn_r->decode
11160 && !bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11161 {
11162 /* Handle enhanced store insns and DSP insns (e.g. LDRD). */
11163
11164 /* Handle str(x) insn */
11165 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11166 ARM_RECORD_STRH);
11167 }
11168 else if (1 == arm_insn_r->decode && 0x12 == opcode1
11169 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11170 {
11171 /* Handle BX, branch and link/exchange. */
11172 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm. */
11173 record_buf[0] = ARM_PS_REGNUM;
11174 arm_insn_r->reg_rec_count = 1;
11175 }
11176 else if (1 == arm_insn_r->decode && 0x16 == opcode1
11177 && sbo_sbz (arm_insn_r->arm_insn, 9, 4, 1)
11178 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1))
11179 {
11180 /* Count leading zeros: CLZ. */
11181 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11182 arm_insn_r->reg_rec_count = 1;
11183 }
11184 else if (!bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11185 && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11186 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1)
11187 && sbo_sbz (arm_insn_r->arm_insn, 1, 12, 0)
11188 )
11189 {
11190 /* Handle MRS insn. */
11191 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11192 arm_insn_r->reg_rec_count = 1;
11193 }
11194 else if (arm_insn_r->opcode <= 15)
11195 {
11196 /* Normal data processing insns. */
11197 /* Out of 11 shifter operands mode, all the insn modifies destination
11198 register, which is specified by 13-16 decode. */
11199 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11200 record_buf[1] = ARM_PS_REGNUM;
11201 arm_insn_r->reg_rec_count = 2;
11202 }
11203 else
11204 {
11205 return -1;
11206 }
11207
11208 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11209 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11210 return 0;
11211}
11212
11213/* Handling opcode 001 insns. */
11214
11215static int
11216arm_record_data_proc_imm (insn_decode_record *arm_insn_r)
11217{
11218 uint32_t record_buf[8], record_buf_mem[8];
11219
11220 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11221 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11222
11223 if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11224 && 2 == bits (arm_insn_r->arm_insn, 20, 21)
11225 && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
11226 )
11227 {
11228 /* Handle MSR insn. */
11229 if (9 == arm_insn_r->opcode)
11230 {
11231 /* CSPR is going to be changed. */
11232 record_buf[0] = ARM_PS_REGNUM;
11233 arm_insn_r->reg_rec_count = 1;
11234 }
11235 else
11236 {
11237 /* SPSR is going to be changed. */
11238 }
11239 }
11240 else if (arm_insn_r->opcode <= 15)
11241 {
11242 /* Normal data processing insns. */
11243 /* Out of 11 shifter operands mode, all the insn modifies destination
11244 register, which is specified by 13-16 decode. */
11245 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11246 record_buf[1] = ARM_PS_REGNUM;
11247 arm_insn_r->reg_rec_count = 2;
11248 }
11249 else
11250 {
11251 return -1;
11252 }
11253
11254 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11255 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11256 return 0;
11257}
11258
11259/* Handling opcode 010 insns. */
11260
11261static int
11262arm_record_ld_st_imm_offset (insn_decode_record *arm_insn_r)
11263{
11264 struct regcache *reg_cache = arm_insn_r->regcache;
11265
11266 uint32_t reg_src1 = 0 , reg_dest = 0;
11267 uint32_t offset_12 = 0, tgt_mem_addr = 0;
11268 uint32_t record_buf[8], record_buf_mem[8];
11269
11270 ULONGEST u_regval = 0;
11271
11272 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11273 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11274
11275 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11276 {
11277 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11278 /* LDR insn has a capability to do branching, if
11279 MOV LR, PC is precedded by LDR insn having Rn as R15
11280 in that case, it emulates branch and link insn, and hence we
11281 need to save CSPR and PC as well. */
11282 if (ARM_PC_REGNUM != reg_dest)
11283 {
11284 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11285 arm_insn_r->reg_rec_count = 1;
11286 }
11287 else
11288 {
11289 record_buf[0] = reg_dest;
11290 record_buf[1] = ARM_PS_REGNUM;
11291 arm_insn_r->reg_rec_count = 2;
11292 }
11293 }
11294 else
11295 {
11296 /* Store, immediate offset, immediate pre-indexed,
11297 immediate post-indexed. */
11298 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11299 offset_12 = bits (arm_insn_r->arm_insn, 0, 11);
11300 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11301 /* U == 1 */
11302 if (bit (arm_insn_r->arm_insn, 23))
11303 {
11304 tgt_mem_addr = u_regval + offset_12;
11305 }
11306 else
11307 {
11308 tgt_mem_addr = u_regval - offset_12;
11309 }
11310
11311 switch (arm_insn_r->opcode)
11312 {
11313 /* STR. */
11314 case 8:
11315 case 12:
11316 /* STR. */
11317 case 9:
11318 case 13:
11319 /* STRT. */
11320 case 1:
11321 case 5:
11322 /* STR. */
11323 case 4:
11324 case 0:
11325 record_buf_mem[0] = 4;
11326 break;
11327
11328 /* STRB. */
11329 case 10:
11330 case 14:
11331 /* STRB. */
11332 case 11:
11333 case 15:
11334 /* STRBT. */
11335 case 3:
11336 case 7:
11337 /* STRB. */
11338 case 2:
11339 case 6:
11340 record_buf_mem[0] = 1;
11341 break;
11342
11343 default:
11344 gdb_assert_not_reached ("no decoding pattern found");
11345 break;
11346 }
11347 record_buf_mem[1] = tgt_mem_addr;
11348 arm_insn_r->mem_rec_count = 1;
11349
11350 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11351 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11352 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11353 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11354 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11355 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11356 )
11357 {
11358 /* We are handling pre-indexed mode; post-indexed mode;
11359 where Rn is going to be changed. */
11360 record_buf[0] = reg_src1;
11361 arm_insn_r->reg_rec_count = 1;
11362 }
11363 }
11364
11365 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11366 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11367 return 0;
11368}
11369
11370/* Handling opcode 011 insns. */
11371
11372static int
11373arm_record_ld_st_reg_offset (insn_decode_record *arm_insn_r)
11374{
11375 struct regcache *reg_cache = arm_insn_r->regcache;
11376
11377 uint32_t shift_imm = 0;
11378 uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
11379 uint32_t offset_12 = 0, tgt_mem_addr = 0;
11380 uint32_t record_buf[8], record_buf_mem[8];
11381
11382 LONGEST s_word;
11383 ULONGEST u_regval[2];
11384
11385 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11386 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11387
11388 /* Handle enhanced store insns and LDRD DSP insn,
11389 order begins according to addressing modes for store insns
11390 STRH insn. */
11391
11392 /* LDR or STR? */
11393 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11394 {
11395 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11396 /* LDR insn has a capability to do branching, if
11397 MOV LR, PC is precedded by LDR insn having Rn as R15
11398 in that case, it emulates branch and link insn, and hence we
11399 need to save CSPR and PC as well. */
11400 if (15 != reg_dest)
11401 {
11402 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11403 arm_insn_r->reg_rec_count = 1;
11404 }
11405 else
11406 {
11407 record_buf[0] = reg_dest;
11408 record_buf[1] = ARM_PS_REGNUM;
11409 arm_insn_r->reg_rec_count = 2;
11410 }
11411 }
11412 else
11413 {
11414 if (! bits (arm_insn_r->arm_insn, 4, 11))
11415 {
11416 /* Store insn, register offset and register pre-indexed,
11417 register post-indexed. */
11418 /* Get Rm. */
11419 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11420 /* Get Rn. */
11421 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11422 regcache_raw_read_unsigned (reg_cache, reg_src1
11423 , &u_regval[0]);
11424 regcache_raw_read_unsigned (reg_cache, reg_src2
11425 , &u_regval[1]);
11426 if (15 == reg_src2)
11427 {
11428 /* If R15 was used as Rn, hence current PC+8. */
11429 /* Pre-indexed mode doesnt reach here ; illegal insn. */
11430 u_regval[0] = u_regval[0] + 8;
11431 }
11432 /* Calculate target store address, Rn +/- Rm, register offset. */
11433 /* U == 1. */
11434 if (bit (arm_insn_r->arm_insn, 23))
11435 {
11436 tgt_mem_addr = u_regval[0] + u_regval[1];
11437 }
11438 else
11439 {
11440 tgt_mem_addr = u_regval[1] - u_regval[0];
11441 }
11442
11443 switch (arm_insn_r->opcode)
11444 {
11445 /* STR. */
11446 case 8:
11447 case 12:
11448 /* STR. */
11449 case 9:
11450 case 13:
11451 /* STRT. */
11452 case 1:
11453 case 5:
11454 /* STR. */
11455 case 0:
11456 case 4:
11457 record_buf_mem[0] = 4;
11458 break;
11459
11460 /* STRB. */
11461 case 10:
11462 case 14:
11463 /* STRB. */
11464 case 11:
11465 case 15:
11466 /* STRBT. */
11467 case 3:
11468 case 7:
11469 /* STRB. */
11470 case 2:
11471 case 6:
11472 record_buf_mem[0] = 1;
11473 break;
11474
11475 default:
11476 gdb_assert_not_reached ("no decoding pattern found");
11477 break;
11478 }
11479 record_buf_mem[1] = tgt_mem_addr;
11480 arm_insn_r->mem_rec_count = 1;
11481
11482 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11483 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11484 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11485 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11486 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11487 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11488 )
11489 {
11490 /* Rn is going to be changed in pre-indexed mode and
11491 post-indexed mode as well. */
11492 record_buf[0] = reg_src2;
11493 arm_insn_r->reg_rec_count = 1;
11494 }
11495 }
11496 else
11497 {
11498 /* Store insn, scaled register offset; scaled pre-indexed. */
11499 offset_12 = bits (arm_insn_r->arm_insn, 5, 6);
11500 /* Get Rm. */
11501 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11502 /* Get Rn. */
11503 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11504 /* Get shift_imm. */
11505 shift_imm = bits (arm_insn_r->arm_insn, 7, 11);
11506 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11507 regcache_raw_read_signed (reg_cache, reg_src1, &s_word);
11508 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11509 /* Offset_12 used as shift. */
11510 switch (offset_12)
11511 {
11512 case 0:
11513 /* Offset_12 used as index. */
11514 offset_12 = u_regval[0] << shift_imm;
11515 break;
11516
11517 case 1:
11518 offset_12 = (!shift_imm)?0:u_regval[0] >> shift_imm;
11519 break;
11520
11521 case 2:
11522 if (!shift_imm)
11523 {
11524 if (bit (u_regval[0], 31))
11525 {
11526 offset_12 = 0xFFFFFFFF;
11527 }
11528 else
11529 {
11530 offset_12 = 0;
11531 }
11532 }
11533 else
11534 {
11535 /* This is arithmetic shift. */
11536 offset_12 = s_word >> shift_imm;
11537 }
11538 break;
11539
11540 case 3:
11541 if (!shift_imm)
11542 {
11543 regcache_raw_read_unsigned (reg_cache, ARM_PS_REGNUM,
11544 &u_regval[1]);
11545 /* Get C flag value and shift it by 31. */
11546 offset_12 = (((bit (u_regval[1], 29)) << 31) \
11547 | (u_regval[0]) >> 1);
11548 }
11549 else
11550 {
11551 offset_12 = (u_regval[0] >> shift_imm) \
11552 | (u_regval[0] <<
11553 (sizeof(uint32_t) - shift_imm));
11554 }
11555 break;
11556
11557 default:
11558 gdb_assert_not_reached ("no decoding pattern found");
11559 break;
11560 }
11561
11562 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11563 /* bit U set. */
11564 if (bit (arm_insn_r->arm_insn, 23))
11565 {
11566 tgt_mem_addr = u_regval[1] + offset_12;
11567 }
11568 else
11569 {
11570 tgt_mem_addr = u_regval[1] - offset_12;
11571 }
11572
11573 switch (arm_insn_r->opcode)
11574 {
11575 /* STR. */
11576 case 8:
11577 case 12:
11578 /* STR. */
11579 case 9:
11580 case 13:
11581 /* STRT. */
11582 case 1:
11583 case 5:
11584 /* STR. */
11585 case 0:
11586 case 4:
11587 record_buf_mem[0] = 4;
11588 break;
11589
11590 /* STRB. */
11591 case 10:
11592 case 14:
11593 /* STRB. */
11594 case 11:
11595 case 15:
11596 /* STRBT. */
11597 case 3:
11598 case 7:
11599 /* STRB. */
11600 case 2:
11601 case 6:
11602 record_buf_mem[0] = 1;
11603 break;
11604
11605 default:
11606 gdb_assert_not_reached ("no decoding pattern found");
11607 break;
11608 }
11609 record_buf_mem[1] = tgt_mem_addr;
11610 arm_insn_r->mem_rec_count = 1;
11611
11612 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11613 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11614 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11615 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11616 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11617 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11618 )
11619 {
11620 /* Rn is going to be changed in register scaled pre-indexed
11621 mode,and scaled post indexed mode. */
11622 record_buf[0] = reg_src2;
11623 arm_insn_r->reg_rec_count = 1;
11624 }
11625 }
11626 }
11627
11628 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11629 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11630 return 0;
11631}
11632
11633/* Handling opcode 100 insns. */
11634
11635static int
11636arm_record_ld_st_multiple (insn_decode_record *arm_insn_r)
11637{
11638 struct regcache *reg_cache = arm_insn_r->regcache;
11639
11640 uint32_t register_list[16] = {0}, register_count = 0, register_bits = 0;
11641 uint32_t reg_src1 = 0, addr_mode = 0, no_of_regs = 0;
11642 uint32_t start_address = 0, index = 0;
11643 uint32_t record_buf[24], record_buf_mem[48];
11644
11645 ULONGEST u_regval[2] = {0};
11646
11647 /* This mode is exclusively for load and store multiple. */
11648 /* Handle incremenrt after/before and decrment after.before mode;
11649 Rn is changing depending on W bit, but as of now we store Rn too
11650 without optimization. */
11651
11652 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11653 {
11654 /* LDM (1,2,3) where LDM (3) changes CPSR too. */
11655
11656 if (bit (arm_insn_r->arm_insn, 20) && !bit (arm_insn_r->arm_insn, 22))
11657 {
11658 register_bits = bits (arm_insn_r->arm_insn, 0, 15);
11659 no_of_regs = 15;
11660 }
11661 else
11662 {
11663 register_bits = bits (arm_insn_r->arm_insn, 0, 14);
11664 no_of_regs = 14;
11665 }
11666 /* Get Rn. */
11667 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11668 while (register_bits)
11669 {
11670 if (register_bits & 0x00000001)
11671 register_list[register_count++] = 1;
11672 register_bits = register_bits >> 1;
11673 }
11674
11675 /* Extra space for Base Register and CPSR; wihtout optimization. */
11676 record_buf[register_count] = reg_src1;
11677 record_buf[register_count + 1] = ARM_PS_REGNUM;
11678 arm_insn_r->reg_rec_count = register_count + 2;
11679
11680 for (register_count = 0; register_count < no_of_regs; register_count++)
11681 {
11682 if (register_list[register_count])
11683 {
11684 /* Register_count gives total no of registers
11685 and dually working as reg number. */
11686 record_buf[index] = register_count;
11687 index++;
11688 }
11689 }
11690
11691 }
11692 else
11693 {
11694 /* It handles both STM(1) and STM(2). */
11695 addr_mode = bits (arm_insn_r->arm_insn, 23, 24);
11696
11697 register_bits = bits (arm_insn_r->arm_insn, 0, 15);
11698 /* Get Rn. */
11699 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11700 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11701 while (register_bits)
11702 {
11703 if (register_bits & 0x00000001)
11704 register_count++;
11705 register_bits = register_bits >> 1;
11706 }
11707
11708 switch (addr_mode)
11709 {
11710 /* Decrement after. */
11711 case 0:
11712 start_address = (u_regval[0]) - (register_count * 4) + 4;
11713 arm_insn_r->mem_rec_count = register_count;
11714 while (register_count)
11715 {
11716 record_buf_mem[(register_count * 2) - 1] = start_address;
11717 record_buf_mem[(register_count * 2) - 2] = 4;
11718 start_address = start_address + 4;
11719 register_count--;
11720 }
11721 break;
11722
11723 /* Increment after. */
11724 case 1:
11725 start_address = u_regval[0];
11726 arm_insn_r->mem_rec_count = register_count;
11727 while (register_count)
11728 {
11729 record_buf_mem[(register_count * 2) - 1] = start_address;
11730 record_buf_mem[(register_count * 2) - 2] = 4;
11731 start_address = start_address + 4;
11732 register_count--;
11733 }
11734 break;
11735
11736 /* Decrement before. */
11737 case 2:
11738
11739 start_address = (u_regval[0]) - (register_count * 4);
11740 arm_insn_r->mem_rec_count = register_count;
11741 while (register_count)
11742 {
11743 record_buf_mem[(register_count * 2) - 1] = start_address;
11744 record_buf_mem[(register_count * 2) - 2] = 4;
11745 start_address = start_address + 4;
11746 register_count--;
11747 }
11748 break;
11749
11750 /* Increment before. */
11751 case 3:
11752 start_address = u_regval[0] + 4;
11753 arm_insn_r->mem_rec_count = register_count;
11754 while (register_count)
11755 {
11756 record_buf_mem[(register_count * 2) - 1] = start_address;
11757 record_buf_mem[(register_count * 2) - 2] = 4;
11758 start_address = start_address + 4;
11759 register_count--;
11760 }
11761 break;
11762
11763 default:
11764 gdb_assert_not_reached ("no decoding pattern found");
11765 break;
11766 }
11767
11768 /* Base register also changes; based on condition and W bit. */
11769 /* We save it anyway without optimization. */
11770 record_buf[0] = reg_src1;
11771 arm_insn_r->reg_rec_count = 1;
11772 }
11773
11774 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11775 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11776 return 0;
11777}
11778
11779/* Handling opcode 101 insns. */
11780
11781static int
11782arm_record_b_bl (insn_decode_record *arm_insn_r)
11783{
11784 uint32_t record_buf[8];
11785
11786 /* Handle B, BL, BLX(1) insns. */
11787 /* B simply branches so we do nothing here. */
11788 /* Note: BLX(1) doesnt fall here but instead it falls into
11789 extension space. */
11790 if (bit (arm_insn_r->arm_insn, 24))
11791 {
11792 record_buf[0] = ARM_LR_REGNUM;
11793 arm_insn_r->reg_rec_count = 1;
11794 }
11795
11796 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11797
11798 return 0;
11799}
11800
11801/* Handling opcode 110 insns. */
11802
11803static int
11804arm_record_coproc (insn_decode_record *arm_insn_r)
11805{
11806 printf_unfiltered (_("Process record does not support instruction "
11807 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
11808 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11809
11810 return -1;
11811}
11812
11813/* Handling opcode 111 insns. */
11814
11815static int
11816arm_record_coproc_data_proc (insn_decode_record *arm_insn_r)
11817{
11818 struct gdbarch_tdep *tdep = gdbarch_tdep (arm_insn_r->gdbarch);
11819 struct regcache *reg_cache = arm_insn_r->regcache;
11820 uint32_t ret = 0; /* function return value: -1:record failure ; 0:success */
11821
11822 /* Handle SWI insn; system call would be handled over here. */
11823
11824 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 24, 27);
11825 if (15 == arm_insn_r->opcode)
11826 {
11827 /* Handle arm syscall insn. */
11828 if (tdep->arm_swi_record != NULL)
11829 {
11830 ret = tdep->arm_swi_record(reg_cache);
11831 }
11832 else
11833 {
11834 printf_unfiltered (_("no syscall record support\n"));
11835 ret = -1;
11836 }
11837 }
11838
11839 printf_unfiltered (_("Process record does not support instruction "
11840 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
11841 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11842 return ret;
11843}
11844
11845/* Handling opcode 000 insns. */
11846
11847static int
11848thumb_record_shift_add_sub (insn_decode_record *thumb_insn_r)
11849{
11850 uint32_t record_buf[8];
11851 uint32_t reg_src1 = 0;
11852
11853 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11854
11855 record_buf[0] = ARM_PS_REGNUM;
11856 record_buf[1] = reg_src1;
11857 thumb_insn_r->reg_rec_count = 2;
11858
11859 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11860
11861 return 0;
11862}
11863
11864
11865/* Handling opcode 001 insns. */
11866
11867static int
11868thumb_record_add_sub_cmp_mov (insn_decode_record *thumb_insn_r)
11869{
11870 uint32_t record_buf[8];
11871 uint32_t reg_src1 = 0;
11872
11873 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11874
11875 record_buf[0] = ARM_PS_REGNUM;
11876 record_buf[1] = reg_src1;
11877 thumb_insn_r->reg_rec_count = 2;
11878
11879 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11880
11881 return 0;
11882}
11883
11884/* Handling opcode 010 insns. */
11885
11886static int
11887thumb_record_ld_st_reg_offset (insn_decode_record *thumb_insn_r)
11888{
11889 struct regcache *reg_cache = thumb_insn_r->regcache;
11890 uint32_t record_buf[8], record_buf_mem[8];
11891
11892 uint32_t reg_src1 = 0, reg_src2 = 0;
11893 uint32_t opcode1 = 0, opcode2 = 0, opcode3 = 0;
11894
11895 ULONGEST u_regval[2] = {0};
11896
11897 opcode1 = bits (thumb_insn_r->arm_insn, 10, 12);
11898
11899 if (bit (thumb_insn_r->arm_insn, 12))
11900 {
11901 /* Handle load/store register offset. */
11902 opcode2 = bits (thumb_insn_r->arm_insn, 9, 10);
11903 if (opcode2 >= 12 && opcode2 <= 15)
11904 {
11905 /* LDR(2), LDRB(2) , LDRH(2), LDRSB, LDRSH. */
11906 reg_src1 = bits (thumb_insn_r->arm_insn,0, 2);
11907 record_buf[0] = reg_src1;
11908 thumb_insn_r->reg_rec_count = 1;
11909 }
11910 else if (opcode2 >= 8 && opcode2 <= 10)
11911 {
11912 /* STR(2), STRB(2), STRH(2) . */
11913 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
11914 reg_src2 = bits (thumb_insn_r->arm_insn, 6, 8);
11915 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11916 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11917 if (8 == opcode2)
11918 record_buf_mem[0] = 4; /* STR (2). */
11919 else if (10 == opcode2)
11920 record_buf_mem[0] = 1; /* STRB (2). */
11921 else if (9 == opcode2)
11922 record_buf_mem[0] = 2; /* STRH (2). */
11923 record_buf_mem[1] = u_regval[0] + u_regval[1];
11924 thumb_insn_r->mem_rec_count = 1;
11925 }
11926 }
11927 else if (bit (thumb_insn_r->arm_insn, 11))
11928 {
11929 /* Handle load from literal pool. */
11930 /* LDR(3). */
11931 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11932 record_buf[0] = reg_src1;
11933 thumb_insn_r->reg_rec_count = 1;
11934 }
11935 else if (opcode1)
11936 {
11937 opcode2 = bits (thumb_insn_r->arm_insn, 8, 9);
11938 opcode3 = bits (thumb_insn_r->arm_insn, 0, 2);
11939 if ((3 == opcode2) && (!opcode3))
11940 {
11941 /* Branch with exchange. */
11942 record_buf[0] = ARM_PS_REGNUM;
11943 thumb_insn_r->reg_rec_count = 1;
11944 }
11945 else
11946 {
11947 /* Format 8; special data processing insns. */
11948 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11949 record_buf[0] = ARM_PS_REGNUM;
11950 record_buf[1] = reg_src1;
11951 thumb_insn_r->reg_rec_count = 2;
11952 }
11953 }
11954 else
11955 {
11956 /* Format 5; data processing insns. */
11957 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11958 if (bit (thumb_insn_r->arm_insn, 7))
11959 {
11960 reg_src1 = reg_src1 + 8;
11961 }
11962 record_buf[0] = ARM_PS_REGNUM;
11963 record_buf[1] = reg_src1;
11964 thumb_insn_r->reg_rec_count = 2;
11965 }
11966
11967 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11968 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
11969 record_buf_mem);
11970
11971 return 0;
11972}
11973
11974/* Handling opcode 001 insns. */
11975
11976static int
11977thumb_record_ld_st_imm_offset (insn_decode_record *thumb_insn_r)
11978{
11979 struct regcache *reg_cache = thumb_insn_r->regcache;
11980 uint32_t record_buf[8], record_buf_mem[8];
11981
11982 uint32_t reg_src1 = 0;
11983 uint32_t opcode = 0, immed_5 = 0;
11984
11985 ULONGEST u_regval = 0;
11986
11987 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
11988
11989 if (opcode)
11990 {
11991 /* LDR(1). */
11992 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11993 record_buf[0] = reg_src1;
11994 thumb_insn_r->reg_rec_count = 1;
11995 }
11996 else
11997 {
11998 /* STR(1). */
11999 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12000 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
12001 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12002 record_buf_mem[0] = 4;
12003 record_buf_mem[1] = u_regval + (immed_5 * 4);
12004 thumb_insn_r->mem_rec_count = 1;
12005 }
12006
12007 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12008 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12009 record_buf_mem);
12010
12011 return 0;
12012}
12013
12014/* Handling opcode 100 insns. */
12015
12016static int
12017thumb_record_ld_st_stack (insn_decode_record *thumb_insn_r)
12018{
12019 struct regcache *reg_cache = thumb_insn_r->regcache;
12020 uint32_t record_buf[8], record_buf_mem[8];
12021
12022 uint32_t reg_src1 = 0;
12023 uint32_t opcode = 0, immed_8 = 0, immed_5 = 0;
12024
12025 ULONGEST u_regval = 0;
12026
12027 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12028
12029 if (3 == opcode)
12030 {
12031 /* LDR(4). */
12032 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12033 record_buf[0] = reg_src1;
12034 thumb_insn_r->reg_rec_count = 1;
12035 }
12036 else if (1 == opcode)
12037 {
12038 /* LDRH(1). */
12039 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12040 record_buf[0] = reg_src1;
12041 thumb_insn_r->reg_rec_count = 1;
12042 }
12043 else if (2 == opcode)
12044 {
12045 /* STR(3). */
12046 immed_8 = bits (thumb_insn_r->arm_insn, 0, 7);
12047 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
12048 record_buf_mem[0] = 4;
12049 record_buf_mem[1] = u_regval + (immed_8 * 4);
12050 thumb_insn_r->mem_rec_count = 1;
12051 }
12052 else if (0 == opcode)
12053 {
12054 /* STRH(1). */
12055 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
12056 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12057 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12058 record_buf_mem[0] = 2;
12059 record_buf_mem[1] = u_regval + (immed_5 * 2);
12060 thumb_insn_r->mem_rec_count = 1;
12061 }
12062
12063 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12064 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12065 record_buf_mem);
12066
12067 return 0;
12068}
12069
12070/* Handling opcode 101 insns. */
12071
12072static int
12073thumb_record_misc (insn_decode_record *thumb_insn_r)
12074{
12075 struct regcache *reg_cache = thumb_insn_r->regcache;
12076
12077 uint32_t opcode = 0, opcode1 = 0, opcode2 = 0;
12078 uint32_t register_bits = 0, register_count = 0;
12079 uint32_t register_list[8] = {0}, index = 0, start_address = 0;
12080 uint32_t record_buf[24], record_buf_mem[48];
12081 uint32_t reg_src1;
12082
12083 ULONGEST u_regval = 0;
12084
12085 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12086 opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
12087 opcode2 = bits (thumb_insn_r->arm_insn, 9, 12);
12088
12089 if (14 == opcode2)
12090 {
12091 /* POP. */
12092 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12093 while (register_bits)
12094 {
12095 if (register_bits & 0x00000001)
12096 register_list[register_count++] = 1;
12097 register_bits = register_bits >> 1;
12098 }
12099 record_buf[register_count] = ARM_PS_REGNUM;
12100 record_buf[register_count + 1] = ARM_SP_REGNUM;
12101 thumb_insn_r->reg_rec_count = register_count + 2;
12102 for (register_count = 0; register_count < 8; register_count++)
12103 {
12104 if (register_list[register_count])
12105 {
12106 record_buf[index] = register_count;
12107 index++;
12108 }
12109 }
12110 }
12111 else if (10 == opcode2)
12112 {
12113 /* PUSH. */
12114 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12115 regcache_raw_read_unsigned (reg_cache, ARM_PC_REGNUM, &u_regval);
12116 while (register_bits)
12117 {
12118 if (register_bits & 0x00000001)
12119 register_count++;
12120 register_bits = register_bits >> 1;
12121 }
12122 start_address = u_regval - \
12123 (4 * (bit (thumb_insn_r->arm_insn, 8) + register_count));
12124 thumb_insn_r->mem_rec_count = register_count;
12125 while (register_count)
12126 {
12127 record_buf_mem[(register_count * 2) - 1] = start_address;
12128 record_buf_mem[(register_count * 2) - 2] = 4;
12129 start_address = start_address + 4;
12130 register_count--;
12131 }
12132 record_buf[0] = ARM_SP_REGNUM;
12133 thumb_insn_r->reg_rec_count = 1;
12134 }
12135 else if (0x1E == opcode1)
12136 {
12137 /* BKPT insn. */
12138 /* Handle enhanced software breakpoint insn, BKPT. */
12139 /* CPSR is changed to be executed in ARM state, disabling normal
12140 interrupts, entering abort mode. */
12141 /* According to high vector configuration PC is set. */
12142 /* User hits breakpoint and type reverse, in that case, we need to go back with
12143 previous CPSR and Program Counter. */
12144 record_buf[0] = ARM_PS_REGNUM;
12145 record_buf[1] = ARM_LR_REGNUM;
12146 thumb_insn_r->reg_rec_count = 2;
12147 /* We need to save SPSR value, which is not yet done. */
12148 printf_unfiltered (_("Process record does not support instruction "
12149 "0x%0x at address %s.\n"),
12150 thumb_insn_r->arm_insn,
12151 paddress (thumb_insn_r->gdbarch,
12152 thumb_insn_r->this_addr));
12153 return -1;
12154 }
12155 else if ((0 == opcode) || (1 == opcode))
12156 {
12157 /* ADD(5), ADD(6). */
12158 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12159 record_buf[0] = reg_src1;
12160 thumb_insn_r->reg_rec_count = 1;
12161 }
12162 else if (2 == opcode)
12163 {
12164 /* ADD(7), SUB(4). */
12165 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12166 record_buf[0] = ARM_SP_REGNUM;
12167 thumb_insn_r->reg_rec_count = 1;
12168 }
12169
12170 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12171 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12172 record_buf_mem);
12173
12174 return 0;
12175}
12176
12177/* Handling opcode 110 insns. */
12178
12179static int
12180thumb_record_ldm_stm_swi (insn_decode_record *thumb_insn_r)
12181{
12182 struct gdbarch_tdep *tdep = gdbarch_tdep (thumb_insn_r->gdbarch);
12183 struct regcache *reg_cache = thumb_insn_r->regcache;
12184
12185 uint32_t ret = 0; /* function return value: -1:record failure ; 0:success */
12186 uint32_t reg_src1 = 0;
12187 uint32_t opcode1 = 0, opcode2 = 0, register_bits = 0, register_count = 0;
12188 uint32_t register_list[8] = {0}, index = 0, start_address = 0;
12189 uint32_t record_buf[24], record_buf_mem[48];
12190
12191 ULONGEST u_regval = 0;
12192
12193 opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
12194 opcode2 = bits (thumb_insn_r->arm_insn, 11, 12);
12195
12196 if (1 == opcode2)
12197 {
12198
12199 /* LDMIA. */
12200 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12201 /* Get Rn. */
12202 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12203 while (register_bits)
12204 {
12205 if (register_bits & 0x00000001)
12206 register_list[register_count++] = 1;
12207 register_bits = register_bits >> 1;
12208 }
12209 record_buf[register_count] = reg_src1;
12210 thumb_insn_r->reg_rec_count = register_count + 1;
12211 for (register_count = 0; register_count < 8; register_count++)
12212 {
12213 if (register_list[register_count])
12214 {
12215 record_buf[index] = register_count;
12216 index++;
12217 }
12218 }
12219 }
12220 else if (0 == opcode2)
12221 {
12222 /* It handles both STMIA. */
12223 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12224 /* Get Rn. */
12225 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12226 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12227 while (register_bits)
12228 {
12229 if (register_bits & 0x00000001)
12230 register_count++;
12231 register_bits = register_bits >> 1;
12232 }
12233 start_address = u_regval;
12234 thumb_insn_r->mem_rec_count = register_count;
12235 while (register_count)
12236 {
12237 record_buf_mem[(register_count * 2) - 1] = start_address;
12238 record_buf_mem[(register_count * 2) - 2] = 4;
12239 start_address = start_address + 4;
12240 register_count--;
12241 }
12242 }
12243 else if (0x1F == opcode1)
12244 {
12245 /* Handle arm syscall insn. */
12246 if (tdep->arm_swi_record != NULL)
12247 {
12248 ret = tdep->arm_swi_record(reg_cache);
12249 }
12250 else
12251 {
12252 printf_unfiltered (_("no syscall record support\n"));
12253 return -1;
12254 }
12255 }
12256
12257 /* B (1), conditional branch is automatically taken care in process_record,
12258 as PC is saved there. */
12259
12260 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12261 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12262 record_buf_mem);
12263
12264 return ret;
12265}
12266
12267/* Handling opcode 111 insns. */
12268
12269static int
12270thumb_record_branch (insn_decode_record *thumb_insn_r)
12271{
12272 uint32_t record_buf[8];
12273 uint32_t bits_h = 0;
12274
12275 bits_h = bits (thumb_insn_r->arm_insn, 11, 12);
12276
12277 if (2 == bits_h || 3 == bits_h)
12278 {
12279 /* BL */
12280 record_buf[0] = ARM_LR_REGNUM;
12281 thumb_insn_r->reg_rec_count = 1;
12282 }
12283 else if (1 == bits_h)
12284 {
12285 /* BLX(1). */
12286 record_buf[0] = ARM_PS_REGNUM;
12287 record_buf[1] = ARM_LR_REGNUM;
12288 thumb_insn_r->reg_rec_count = 2;
12289 }
12290
12291 /* B(2) is automatically taken care in process_record, as PC is
12292 saved there. */
12293
12294 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12295
12296 return 0;
12297}
12298
12299
12300/* Extracts arm/thumb/thumb2 insn depending on the size, and returns 0 on success
12301and positive val on fauilure. */
12302
12303static int
12304extract_arm_insn (insn_decode_record *insn_record, uint32_t insn_size)
12305{
12306 gdb_byte buf[insn_size];
12307
12308 memset (&buf[0], 0, insn_size);
12309
12310 if (target_read_memory (insn_record->this_addr, &buf[0], insn_size))
12311 return 1;
12312 insn_record->arm_insn = (uint32_t) extract_unsigned_integer (&buf[0],
12313 insn_size,
12314 gdbarch_byte_order (insn_record->gdbarch));
12315 return 0;
12316}
12317
12318typedef int (*sti_arm_hdl_fp_t) (insn_decode_record*);
12319
12320/* Decode arm/thumb insn depending on condition cods and opcodes; and
12321 dispatch it. */
12322
12323static int
12324decode_insn (insn_decode_record *arm_record, record_type_t record_type,
12325 uint32_t insn_size)
12326{
12327
12328 /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm instruction. */
12329 static const sti_arm_hdl_fp_t const arm_handle_insn[8] =
12330 {
12331 arm_record_data_proc_misc_ld_str, /* 000. */
12332 arm_record_data_proc_imm, /* 001. */
12333 arm_record_ld_st_imm_offset, /* 010. */
12334 arm_record_ld_st_reg_offset, /* 011. */
12335 arm_record_ld_st_multiple, /* 100. */
12336 arm_record_b_bl, /* 101. */
12337 arm_record_coproc, /* 110. */
12338 arm_record_coproc_data_proc /* 111. */
12339 };
12340
12341 /* (Starting from numerical 0); bits 13,14,15 decodes type of thumb instruction. */
12342 static const sti_arm_hdl_fp_t const thumb_handle_insn[8] =
12343 { \
12344 thumb_record_shift_add_sub, /* 000. */
12345 thumb_record_add_sub_cmp_mov, /* 001. */
12346 thumb_record_ld_st_reg_offset, /* 010. */
12347 thumb_record_ld_st_imm_offset, /* 011. */
12348 thumb_record_ld_st_stack, /* 100. */
12349 thumb_record_misc, /* 101. */
12350 thumb_record_ldm_stm_swi, /* 110. */
12351 thumb_record_branch /* 111. */
12352 };
12353
12354 uint32_t ret = 0; /* return value: negative:failure 0:success. */
12355 uint32_t insn_id = 0;
12356
12357 if (extract_arm_insn (arm_record, insn_size))
12358 {
12359 if (record_debug)
12360 {
12361 printf_unfiltered (_("Process record: error reading memory at "
12362 "addr %s len = %d.\n"),
12363 paddress (arm_record->gdbarch, arm_record->this_addr), insn_size);
12364 }
12365 return -1;
12366 }
12367 else if (ARM_RECORD == record_type)
12368 {
12369 arm_record->cond = bits (arm_record->arm_insn, 28, 31);
12370 insn_id = bits (arm_record->arm_insn, 25, 27);
12371 ret = arm_record_extension_space (arm_record);
12372 /* If this insn has fallen into extension space
12373 then we need not decode it anymore. */
12374 if (ret != -1 && !INSN_RECORDED(arm_record))
12375 {
12376 ret = arm_handle_insn[insn_id] (arm_record);
12377 }
12378 }
12379 else if (THUMB_RECORD == record_type)
12380 {
12381 /* As thumb does not have condition codes, we set negative. */
12382 arm_record->cond = -1;
12383 insn_id = bits (arm_record->arm_insn, 13, 15);
12384 ret = thumb_handle_insn[insn_id] (arm_record);
12385 }
12386 else if (THUMB2_RECORD == record_type)
12387 {
12388 printf_unfiltered (_("Process record doesnt support thumb32 instruction "
12389 "0x%0x at address %s.\n"),arm_record->arm_insn,
12390 paddress (arm_record->gdbarch,
12391 arm_record->this_addr));
12392 ret = -1;
12393 }
12394 else
12395 {
12396 /* Throw assertion. */
12397 gdb_assert_not_reached ("not a valid instruction, could not decode");
12398 }
12399
12400 return ret;
12401}
12402
12403
12404/* Cleans up local record registers and memory allocations. */
12405
12406static void
12407deallocate_reg_mem (insn_decode_record *record)
12408{
12409 xfree (record->arm_regs);
12410 xfree (record->arm_mems);
12411}
12412
12413
12414/* Parse the current instruction and record the values of the registers and
12415 memory that will be changed in current instruction to record_arch_list".
12416 Return -1 if something is wrong. */
12417
12418int
12419arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
12420 CORE_ADDR insn_addr)
12421{
12422
12423 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
12424 uint32_t no_of_rec = 0;
12425 uint32_t ret = 0; /* return value: -1:record failure ; 0:success */
12426 ULONGEST t_bit = 0, insn_id = 0;
12427
12428 ULONGEST u_regval = 0;
12429
12430 insn_decode_record arm_record;
12431
12432 memset (&arm_record, 0, sizeof (insn_decode_record));
12433 arm_record.regcache = regcache;
12434 arm_record.this_addr = insn_addr;
12435 arm_record.gdbarch = gdbarch;
12436
12437
12438 if (record_debug > 1)
12439 {
12440 fprintf_unfiltered (gdb_stdlog, "Process record: arm_process_record "
12441 "addr = %s\n",
12442 paddress (gdbarch, arm_record.this_addr));
12443 }
12444
12445 if (extract_arm_insn (&arm_record, 2))
12446 {
12447 if (record_debug)
12448 {
12449 printf_unfiltered (_("Process record: error reading memory at "
12450 "addr %s len = %d.\n"),
12451 paddress (arm_record.gdbarch,
12452 arm_record.this_addr), 2);
12453 }
12454 return -1;
12455 }
12456
12457 /* Check the insn, whether it is thumb or arm one. */
12458
12459 t_bit = arm_psr_thumb_bit (arm_record.gdbarch);
12460 regcache_raw_read_unsigned (arm_record.regcache, ARM_PS_REGNUM, &u_regval);
12461
12462
12463 if (!(u_regval & t_bit))
12464 {
12465 /* We are decoding arm insn. */
12466 ret = decode_insn (&arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES);
12467 }
12468 else
12469 {
12470 insn_id = bits (arm_record.arm_insn, 11, 15);
12471 /* is it thumb2 insn? */
12472 if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id))
12473 {
12474 ret = decode_insn (&arm_record, THUMB2_RECORD,
12475 THUMB2_INSN_SIZE_BYTES);
12476 }
12477 else
12478 {
12479 /* We are decoding thumb insn. */
12480 ret = decode_insn (&arm_record, THUMB_RECORD, THUMB_INSN_SIZE_BYTES);
12481 }
12482 }
12483
12484 if (0 == ret)
12485 {
12486 /* Record registers. */
12487 record_arch_list_add_reg (arm_record.regcache, ARM_PC_REGNUM);
12488 if (arm_record.arm_regs)
12489 {
12490 for (no_of_rec = 0; no_of_rec < arm_record.reg_rec_count; no_of_rec++)
12491 {
12492 if (record_arch_list_add_reg (arm_record.regcache ,
12493 arm_record.arm_regs[no_of_rec]))
12494 ret = -1;
12495 }
12496 }
12497 /* Record memories. */
12498 if (arm_record.arm_mems)
12499 {
12500 for (no_of_rec = 0; no_of_rec < arm_record.mem_rec_count; no_of_rec++)
12501 {
12502 if (record_arch_list_add_mem
12503 ((CORE_ADDR)arm_record.arm_mems[no_of_rec].addr,
12504 arm_record.arm_mems[no_of_rec].len))
12505 ret = -1;
12506 }
12507 }
12508
12509 if (record_arch_list_add_end ())
12510 ret = -1;
12511 }
12512
12513
12514 deallocate_reg_mem (&arm_record);
12515
12516 return ret;
12517}
12518
This page took 1.415276 seconds and 4 git commands to generate.