* amd64-tdep.c (amd64_return_value): Use TYPE_LENGTH directly.
[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 382{
f9d67f43
DJ
383 struct minimal_symbol *sym;
384 char type;
a42244db
YQ
385 struct displaced_step_closure* dsc
386 = get_displaced_step_closure_by_addr(memaddr);
387
388 /* If checking the mode of displaced instruction in copy area, the mode
389 should be determined by instruction on the original address. */
390 if (dsc)
391 {
392 if (debug_displaced)
393 fprintf_unfiltered (gdb_stdlog,
394 "displaced: check mode of %.8lx instead of %.8lx\n",
395 (unsigned long) dsc->insn_addr,
396 (unsigned long) memaddr);
397 memaddr = dsc->insn_addr;
398 }
f9d67f43
DJ
399
400 /* If bit 0 of the address is set, assume this is a Thumb address. */
401 if (IS_THUMB_ADDR (memaddr))
402 return 1;
403
18819fa6
UW
404 /* Respect internal mode override if active. */
405 if (arm_override_mode != -1)
406 return arm_override_mode;
407
f9d67f43
DJ
408 /* If the user wants to override the symbol table, let him. */
409 if (strcmp (arm_force_mode_string, "arm") == 0)
410 return 0;
411 if (strcmp (arm_force_mode_string, "thumb") == 0)
412 return 1;
413
9779414d
DJ
414 /* ARM v6-M and v7-M are always in Thumb mode. */
415 if (gdbarch_tdep (gdbarch)->is_m)
416 return 1;
417
f9d67f43
DJ
418 /* If there are mapping symbols, consult them. */
419 type = arm_find_mapping_symbol (memaddr, NULL);
420 if (type)
421 return type == 't';
422
ed9a39eb 423 /* Thumb functions have a "special" bit set in minimal symbols. */
c906108c
SS
424 sym = lookup_minimal_symbol_by_pc (memaddr);
425 if (sym)
0428b8f5
DJ
426 return (MSYMBOL_IS_SPECIAL (sym));
427
428 /* If the user wants to override the fallback mode, let them. */
429 if (strcmp (arm_fallback_mode_string, "arm") == 0)
430 return 0;
431 if (strcmp (arm_fallback_mode_string, "thumb") == 0)
432 return 1;
433
434 /* If we couldn't find any symbol, but we're talking to a running
435 target, then trust the current value of $cpsr. This lets
436 "display/i $pc" always show the correct mode (though if there is
437 a symbol table we will not reach here, so it still may not be
18819fa6 438 displayed in the mode it will be executed). */
0428b8f5 439 if (target_has_registers)
18819fa6 440 return arm_frame_is_thumb (get_current_frame ());
0428b8f5
DJ
441
442 /* Otherwise we're out of luck; we assume ARM. */
443 return 0;
c906108c
SS
444}
445
181c1381 446/* Remove useless bits from addresses in a running program. */
34e8f22d 447static CORE_ADDR
24568a2c 448arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
c906108c 449{
a3a2ee65 450 if (arm_apcs_32)
dd6be234 451 return UNMAKE_THUMB_ADDR (val);
c906108c 452 else
a3a2ee65 453 return (val & 0x03fffffc);
c906108c
SS
454}
455
181c1381
RE
456/* When reading symbols, we need to zap the low bit of the address,
457 which may be set to 1 for Thumb functions. */
34e8f22d 458static CORE_ADDR
24568a2c 459arm_smash_text_address (struct gdbarch *gdbarch, CORE_ADDR val)
181c1381
RE
460{
461 return val & ~1;
462}
463
0d39a070 464/* Return 1 if PC is the start of a compiler helper function which
e0634ccf
UW
465 can be safely ignored during prologue skipping. IS_THUMB is true
466 if the function is known to be a Thumb function due to the way it
467 is being called. */
0d39a070 468static int
e0634ccf 469skip_prologue_function (struct gdbarch *gdbarch, CORE_ADDR pc, int is_thumb)
0d39a070 470{
e0634ccf 471 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
0d39a070 472 struct minimal_symbol *msym;
0d39a070
DJ
473
474 msym = lookup_minimal_symbol_by_pc (pc);
e0634ccf
UW
475 if (msym != NULL
476 && SYMBOL_VALUE_ADDRESS (msym) == pc
477 && SYMBOL_LINKAGE_NAME (msym) != NULL)
478 {
479 const char *name = SYMBOL_LINKAGE_NAME (msym);
0d39a070 480
e0634ccf
UW
481 /* The GNU linker's Thumb call stub to foo is named
482 __foo_from_thumb. */
483 if (strstr (name, "_from_thumb") != NULL)
484 name += 2;
0d39a070 485
e0634ccf
UW
486 /* On soft-float targets, __truncdfsf2 is called to convert promoted
487 arguments to their argument types in non-prototyped
488 functions. */
489 if (strncmp (name, "__truncdfsf2", strlen ("__truncdfsf2")) == 0)
490 return 1;
491 if (strncmp (name, "__aeabi_d2f", strlen ("__aeabi_d2f")) == 0)
492 return 1;
0d39a070 493
e0634ccf
UW
494 /* Internal functions related to thread-local storage. */
495 if (strncmp (name, "__tls_get_addr", strlen ("__tls_get_addr")) == 0)
496 return 1;
497 if (strncmp (name, "__aeabi_read_tp", strlen ("__aeabi_read_tp")) == 0)
498 return 1;
499 }
500 else
501 {
502 /* If we run against a stripped glibc, we may be unable to identify
503 special functions by name. Check for one important case,
504 __aeabi_read_tp, by comparing the *code* against the default
505 implementation (this is hand-written ARM assembler in glibc). */
506
507 if (!is_thumb
508 && read_memory_unsigned_integer (pc, 4, byte_order_for_code)
509 == 0xe3e00a0f /* mov r0, #0xffff0fff */
510 && read_memory_unsigned_integer (pc + 4, 4, byte_order_for_code)
511 == 0xe240f01f) /* sub pc, r0, #31 */
512 return 1;
513 }
ec3d575a 514
0d39a070
DJ
515 return 0;
516}
517
518/* Support routines for instruction parsing. */
519#define submask(x) ((1L << ((x) + 1)) - 1)
520#define bit(obj,st) (((obj) >> (st)) & 1)
521#define bits(obj,st,fn) (((obj) >> (st)) & submask ((fn) - (st)))
522#define sbits(obj,st,fn) \
523 ((long) (bits(obj,st,fn) | ((long) bit(obj,fn) * ~ submask (fn - st))))
524#define BranchDest(addr,instr) \
525 ((CORE_ADDR) (((long) (addr)) + 8 + (sbits (instr, 0, 23) << 2)))
526
621c6d5b
YQ
527/* Extract the immediate from instruction movw/movt of encoding T. INSN1 is
528 the first 16-bit of instruction, and INSN2 is the second 16-bit of
529 instruction. */
530#define EXTRACT_MOVW_MOVT_IMM_T(insn1, insn2) \
531 ((bits ((insn1), 0, 3) << 12) \
532 | (bits ((insn1), 10, 10) << 11) \
533 | (bits ((insn2), 12, 14) << 8) \
534 | bits ((insn2), 0, 7))
535
536/* Extract the immediate from instruction movw/movt of encoding A. INSN is
537 the 32-bit instruction. */
538#define EXTRACT_MOVW_MOVT_IMM_A(insn) \
539 ((bits ((insn), 16, 19) << 12) \
540 | bits ((insn), 0, 11))
541
ec3d575a
UW
542/* Decode immediate value; implements ThumbExpandImmediate pseudo-op. */
543
544static unsigned int
545thumb_expand_immediate (unsigned int imm)
546{
547 unsigned int count = imm >> 7;
548
549 if (count < 8)
550 switch (count / 2)
551 {
552 case 0:
553 return imm & 0xff;
554 case 1:
555 return (imm & 0xff) | ((imm & 0xff) << 16);
556 case 2:
557 return ((imm & 0xff) << 8) | ((imm & 0xff) << 24);
558 case 3:
559 return (imm & 0xff) | ((imm & 0xff) << 8)
560 | ((imm & 0xff) << 16) | ((imm & 0xff) << 24);
561 }
562
563 return (0x80 | (imm & 0x7f)) << (32 - count);
564}
565
566/* Return 1 if the 16-bit Thumb instruction INST might change
567 control flow, 0 otherwise. */
568
569static int
570thumb_instruction_changes_pc (unsigned short inst)
571{
572 if ((inst & 0xff00) == 0xbd00) /* pop {rlist, pc} */
573 return 1;
574
575 if ((inst & 0xf000) == 0xd000) /* conditional branch */
576 return 1;
577
578 if ((inst & 0xf800) == 0xe000) /* unconditional branch */
579 return 1;
580
581 if ((inst & 0xff00) == 0x4700) /* bx REG, blx REG */
582 return 1;
583
ad8b5167
UW
584 if ((inst & 0xff87) == 0x4687) /* mov pc, REG */
585 return 1;
586
ec3d575a
UW
587 if ((inst & 0xf500) == 0xb100) /* CBNZ or CBZ. */
588 return 1;
589
590 return 0;
591}
592
593/* Return 1 if the 32-bit Thumb instruction in INST1 and INST2
594 might change control flow, 0 otherwise. */
595
596static int
597thumb2_instruction_changes_pc (unsigned short inst1, unsigned short inst2)
598{
599 if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
600 {
601 /* Branches and miscellaneous control instructions. */
602
603 if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
604 {
605 /* B, BL, BLX. */
606 return 1;
607 }
608 else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
609 {
610 /* SUBS PC, LR, #imm8. */
611 return 1;
612 }
613 else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
614 {
615 /* Conditional branch. */
616 return 1;
617 }
618
619 return 0;
620 }
621
622 if ((inst1 & 0xfe50) == 0xe810)
623 {
624 /* Load multiple or RFE. */
625
626 if (bit (inst1, 7) && !bit (inst1, 8))
627 {
628 /* LDMIA or POP */
629 if (bit (inst2, 15))
630 return 1;
631 }
632 else if (!bit (inst1, 7) && bit (inst1, 8))
633 {
634 /* LDMDB */
635 if (bit (inst2, 15))
636 return 1;
637 }
638 else if (bit (inst1, 7) && bit (inst1, 8))
639 {
640 /* RFEIA */
641 return 1;
642 }
643 else if (!bit (inst1, 7) && !bit (inst1, 8))
644 {
645 /* RFEDB */
646 return 1;
647 }
648
649 return 0;
650 }
651
652 if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
653 {
654 /* MOV PC or MOVS PC. */
655 return 1;
656 }
657
658 if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
659 {
660 /* LDR PC. */
661 if (bits (inst1, 0, 3) == 15)
662 return 1;
663 if (bit (inst1, 7))
664 return 1;
665 if (bit (inst2, 11))
666 return 1;
667 if ((inst2 & 0x0fc0) == 0x0000)
668 return 1;
669
670 return 0;
671 }
672
673 if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
674 {
675 /* TBB. */
676 return 1;
677 }
678
679 if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
680 {
681 /* TBH. */
682 return 1;
683 }
684
685 return 0;
686}
687
29d73ae4
DJ
688/* Analyze a Thumb prologue, looking for a recognizable stack frame
689 and frame pointer. Scan until we encounter a store that could
0d39a070
DJ
690 clobber the stack frame unexpectedly, or an unknown instruction.
691 Return the last address which is definitely safe to skip for an
692 initial breakpoint. */
c906108c
SS
693
694static CORE_ADDR
29d73ae4
DJ
695thumb_analyze_prologue (struct gdbarch *gdbarch,
696 CORE_ADDR start, CORE_ADDR limit,
697 struct arm_prologue_cache *cache)
c906108c 698{
0d39a070 699 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
e17a4113 700 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
29d73ae4
DJ
701 int i;
702 pv_t regs[16];
703 struct pv_area *stack;
704 struct cleanup *back_to;
705 CORE_ADDR offset;
ec3d575a 706 CORE_ADDR unrecognized_pc = 0;
da3c6d4a 707
29d73ae4
DJ
708 for (i = 0; i < 16; i++)
709 regs[i] = pv_register (i, 0);
55f960e1 710 stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
29d73ae4
DJ
711 back_to = make_cleanup_free_pv_area (stack);
712
29d73ae4 713 while (start < limit)
c906108c 714 {
29d73ae4
DJ
715 unsigned short insn;
716
e17a4113 717 insn = read_memory_unsigned_integer (start, 2, byte_order_for_code);
9d4fde75 718
94c30b78 719 if ((insn & 0xfe00) == 0xb400) /* push { rlist } */
da59e081 720 {
29d73ae4
DJ
721 int regno;
722 int mask;
4be43953
DJ
723
724 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
725 break;
29d73ae4
DJ
726
727 /* Bits 0-7 contain a mask for registers R0-R7. Bit 8 says
728 whether to save LR (R14). */
729 mask = (insn & 0xff) | ((insn & 0x100) << 6);
730
731 /* Calculate offsets of saved R0-R7 and LR. */
732 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
733 if (mask & (1 << regno))
734 {
29d73ae4
DJ
735 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
736 -4);
737 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
738 }
da59e081 739 }
da3c6d4a
MS
740 else if ((insn & 0xff00) == 0xb000) /* add sp, #simm OR
741 sub sp, #simm */
da59e081 742 {
29d73ae4
DJ
743 offset = (insn & 0x7f) << 2; /* get scaled offset */
744 if (insn & 0x80) /* Check for SUB. */
745 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
746 -offset);
da59e081 747 else
29d73ae4
DJ
748 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
749 offset);
da59e081 750 }
0d39a070
DJ
751 else if ((insn & 0xf800) == 0xa800) /* add Rd, sp, #imm */
752 regs[bits (insn, 8, 10)] = pv_add_constant (regs[ARM_SP_REGNUM],
753 (insn & 0xff) << 2);
754 else if ((insn & 0xfe00) == 0x1c00 /* add Rd, Rn, #imm */
755 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
756 regs[bits (insn, 0, 2)] = pv_add_constant (regs[bits (insn, 3, 5)],
757 bits (insn, 6, 8));
758 else if ((insn & 0xf800) == 0x3000 /* add Rd, #imm */
759 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
760 regs[bits (insn, 8, 10)] = pv_add_constant (regs[bits (insn, 8, 10)],
761 bits (insn, 0, 7));
762 else if ((insn & 0xfe00) == 0x1800 /* add Rd, Rn, Rm */
763 && pv_is_register (regs[bits (insn, 6, 8)], ARM_SP_REGNUM)
764 && pv_is_constant (regs[bits (insn, 3, 5)]))
765 regs[bits (insn, 0, 2)] = pv_add (regs[bits (insn, 3, 5)],
766 regs[bits (insn, 6, 8)]);
767 else if ((insn & 0xff00) == 0x4400 /* add Rd, Rm */
768 && pv_is_constant (regs[bits (insn, 3, 6)]))
769 {
770 int rd = (bit (insn, 7) << 3) + bits (insn, 0, 2);
771 int rm = bits (insn, 3, 6);
772 regs[rd] = pv_add (regs[rd], regs[rm]);
773 }
29d73ae4 774 else if ((insn & 0xff00) == 0x4600) /* mov hi, lo or mov lo, hi */
da59e081 775 {
29d73ae4
DJ
776 int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
777 int src_reg = (insn & 0x78) >> 3;
778 regs[dst_reg] = regs[src_reg];
da59e081 779 }
29d73ae4 780 else if ((insn & 0xf800) == 0x9000) /* str rd, [sp, #off] */
da59e081 781 {
29d73ae4
DJ
782 /* Handle stores to the stack. Normally pushes are used,
783 but with GCC -mtpcs-frame, there may be other stores
784 in the prologue to create the frame. */
785 int regno = (insn >> 8) & 0x7;
786 pv_t addr;
787
788 offset = (insn & 0xff) << 2;
789 addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
790
791 if (pv_area_store_would_trash (stack, addr))
792 break;
793
794 pv_area_store (stack, addr, 4, regs[regno]);
da59e081 795 }
0d39a070
DJ
796 else if ((insn & 0xf800) == 0x6000) /* str rd, [rn, #off] */
797 {
798 int rd = bits (insn, 0, 2);
799 int rn = bits (insn, 3, 5);
800 pv_t addr;
801
802 offset = bits (insn, 6, 10) << 2;
803 addr = pv_add_constant (regs[rn], offset);
804
805 if (pv_area_store_would_trash (stack, addr))
806 break;
807
808 pv_area_store (stack, addr, 4, regs[rd]);
809 }
810 else if (((insn & 0xf800) == 0x7000 /* strb Rd, [Rn, #off] */
811 || (insn & 0xf800) == 0x8000) /* strh Rd, [Rn, #off] */
812 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
813 /* Ignore stores of argument registers to the stack. */
814 ;
815 else if ((insn & 0xf800) == 0xc800 /* ldmia Rn!, { registers } */
816 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
817 /* Ignore block loads from the stack, potentially copying
818 parameters from memory. */
819 ;
820 else if ((insn & 0xf800) == 0x9800 /* ldr Rd, [Rn, #immed] */
821 || ((insn & 0xf800) == 0x6800 /* ldr Rd, [sp, #immed] */
822 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM)))
823 /* Similarly ignore single loads from the stack. */
824 ;
825 else if ((insn & 0xffc0) == 0x0000 /* lsls Rd, Rm, #0 */
826 || (insn & 0xffc0) == 0x1c00) /* add Rd, Rn, #0 */
827 /* Skip register copies, i.e. saves to another register
828 instead of the stack. */
829 ;
830 else if ((insn & 0xf800) == 0x2000) /* movs Rd, #imm */
831 /* Recognize constant loads; even with small stacks these are necessary
832 on Thumb. */
833 regs[bits (insn, 8, 10)] = pv_constant (bits (insn, 0, 7));
834 else if ((insn & 0xf800) == 0x4800) /* ldr Rd, [pc, #imm] */
835 {
836 /* Constant pool loads, for the same reason. */
837 unsigned int constant;
838 CORE_ADDR loc;
839
840 loc = start + 4 + bits (insn, 0, 7) * 4;
841 constant = read_memory_unsigned_integer (loc, 4, byte_order);
842 regs[bits (insn, 8, 10)] = pv_constant (constant);
843 }
db24da6d 844 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instructions. */
0d39a070 845 {
0d39a070
DJ
846 unsigned short inst2;
847
848 inst2 = read_memory_unsigned_integer (start + 2, 2,
849 byte_order_for_code);
850
851 if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
852 {
853 /* BL, BLX. Allow some special function calls when
854 skipping the prologue; GCC generates these before
855 storing arguments to the stack. */
856 CORE_ADDR nextpc;
857 int j1, j2, imm1, imm2;
858
859 imm1 = sbits (insn, 0, 10);
860 imm2 = bits (inst2, 0, 10);
861 j1 = bit (inst2, 13);
862 j2 = bit (inst2, 11);
863
864 offset = ((imm1 << 12) + (imm2 << 1));
865 offset ^= ((!j2) << 22) | ((!j1) << 23);
866
867 nextpc = start + 4 + offset;
868 /* For BLX make sure to clear the low bits. */
869 if (bit (inst2, 12) == 0)
870 nextpc = nextpc & 0xfffffffc;
871
e0634ccf
UW
872 if (!skip_prologue_function (gdbarch, nextpc,
873 bit (inst2, 12) != 0))
0d39a070
DJ
874 break;
875 }
ec3d575a 876
0963b4bd
MS
877 else if ((insn & 0xffd0) == 0xe900 /* stmdb Rn{!},
878 { registers } */
ec3d575a
UW
879 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
880 {
881 pv_t addr = regs[bits (insn, 0, 3)];
882 int regno;
883
884 if (pv_area_store_would_trash (stack, addr))
885 break;
886
887 /* Calculate offsets of saved registers. */
888 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
889 if (inst2 & (1 << regno))
890 {
891 addr = pv_add_constant (addr, -4);
892 pv_area_store (stack, addr, 4, regs[regno]);
893 }
894
895 if (insn & 0x0020)
896 regs[bits (insn, 0, 3)] = addr;
897 }
898
0963b4bd
MS
899 else if ((insn & 0xff50) == 0xe940 /* strd Rt, Rt2,
900 [Rn, #+/-imm]{!} */
ec3d575a
UW
901 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
902 {
903 int regno1 = bits (inst2, 12, 15);
904 int regno2 = bits (inst2, 8, 11);
905 pv_t addr = regs[bits (insn, 0, 3)];
906
907 offset = inst2 & 0xff;
908 if (insn & 0x0080)
909 addr = pv_add_constant (addr, offset);
910 else
911 addr = pv_add_constant (addr, -offset);
912
913 if (pv_area_store_would_trash (stack, addr))
914 break;
915
916 pv_area_store (stack, addr, 4, regs[regno1]);
917 pv_area_store (stack, pv_add_constant (addr, 4),
918 4, regs[regno2]);
919
920 if (insn & 0x0020)
921 regs[bits (insn, 0, 3)] = addr;
922 }
923
924 else if ((insn & 0xfff0) == 0xf8c0 /* str Rt,[Rn,+/-#imm]{!} */
925 && (inst2 & 0x0c00) == 0x0c00
926 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
927 {
928 int regno = bits (inst2, 12, 15);
929 pv_t addr = regs[bits (insn, 0, 3)];
930
931 offset = inst2 & 0xff;
932 if (inst2 & 0x0200)
933 addr = pv_add_constant (addr, offset);
934 else
935 addr = pv_add_constant (addr, -offset);
936
937 if (pv_area_store_would_trash (stack, addr))
938 break;
939
940 pv_area_store (stack, addr, 4, regs[regno]);
941
942 if (inst2 & 0x0100)
943 regs[bits (insn, 0, 3)] = addr;
944 }
945
946 else if ((insn & 0xfff0) == 0xf8c0 /* str.w Rt,[Rn,#imm] */
947 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
948 {
949 int regno = bits (inst2, 12, 15);
950 pv_t addr;
951
952 offset = inst2 & 0xfff;
953 addr = pv_add_constant (regs[bits (insn, 0, 3)], offset);
954
955 if (pv_area_store_would_trash (stack, addr))
956 break;
957
958 pv_area_store (stack, addr, 4, regs[regno]);
959 }
960
961 else if ((insn & 0xffd0) == 0xf880 /* str{bh}.w Rt,[Rn,#imm] */
0d39a070 962 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 963 /* Ignore stores of argument registers to the stack. */
0d39a070 964 ;
ec3d575a
UW
965
966 else if ((insn & 0xffd0) == 0xf800 /* str{bh} Rt,[Rn,#+/-imm] */
967 && (inst2 & 0x0d00) == 0x0c00
0d39a070 968 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 969 /* Ignore stores of argument registers to the stack. */
0d39a070 970 ;
ec3d575a 971
0963b4bd
MS
972 else if ((insn & 0xffd0) == 0xe890 /* ldmia Rn[!],
973 { registers } */
ec3d575a
UW
974 && (inst2 & 0x8000) == 0x0000
975 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
976 /* Ignore block loads from the stack, potentially copying
977 parameters from memory. */
0d39a070 978 ;
ec3d575a 979
0963b4bd
MS
980 else if ((insn & 0xffb0) == 0xe950 /* ldrd Rt, Rt2,
981 [Rn, #+/-imm] */
0d39a070 982 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 983 /* Similarly ignore dual loads from the stack. */
0d39a070 984 ;
ec3d575a
UW
985
986 else if ((insn & 0xfff0) == 0xf850 /* ldr Rt,[Rn,#+/-imm] */
987 && (inst2 & 0x0d00) == 0x0c00
0d39a070 988 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 989 /* Similarly ignore single loads from the stack. */
0d39a070 990 ;
ec3d575a
UW
991
992 else if ((insn & 0xfff0) == 0xf8d0 /* ldr.w Rt,[Rn,#imm] */
0d39a070 993 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 994 /* Similarly ignore single loads from the stack. */
0d39a070 995 ;
ec3d575a
UW
996
997 else if ((insn & 0xfbf0) == 0xf100 /* add.w Rd, Rn, #imm */
998 && (inst2 & 0x8000) == 0x0000)
999 {
1000 unsigned int imm = ((bits (insn, 10, 10) << 11)
1001 | (bits (inst2, 12, 14) << 8)
1002 | bits (inst2, 0, 7));
1003
1004 regs[bits (inst2, 8, 11)]
1005 = pv_add_constant (regs[bits (insn, 0, 3)],
1006 thumb_expand_immediate (imm));
1007 }
1008
1009 else if ((insn & 0xfbf0) == 0xf200 /* addw Rd, Rn, #imm */
1010 && (inst2 & 0x8000) == 0x0000)
0d39a070 1011 {
ec3d575a
UW
1012 unsigned int imm = ((bits (insn, 10, 10) << 11)
1013 | (bits (inst2, 12, 14) << 8)
1014 | bits (inst2, 0, 7));
1015
1016 regs[bits (inst2, 8, 11)]
1017 = pv_add_constant (regs[bits (insn, 0, 3)], imm);
1018 }
1019
1020 else if ((insn & 0xfbf0) == 0xf1a0 /* sub.w Rd, Rn, #imm */
1021 && (inst2 & 0x8000) == 0x0000)
1022 {
1023 unsigned int imm = ((bits (insn, 10, 10) << 11)
1024 | (bits (inst2, 12, 14) << 8)
1025 | bits (inst2, 0, 7));
1026
1027 regs[bits (inst2, 8, 11)]
1028 = pv_add_constant (regs[bits (insn, 0, 3)],
1029 - (CORE_ADDR) thumb_expand_immediate (imm));
1030 }
1031
1032 else if ((insn & 0xfbf0) == 0xf2a0 /* subw Rd, Rn, #imm */
1033 && (inst2 & 0x8000) == 0x0000)
1034 {
1035 unsigned int imm = ((bits (insn, 10, 10) << 11)
1036 | (bits (inst2, 12, 14) << 8)
1037 | bits (inst2, 0, 7));
1038
1039 regs[bits (inst2, 8, 11)]
1040 = pv_add_constant (regs[bits (insn, 0, 3)], - (CORE_ADDR) imm);
1041 }
1042
1043 else if ((insn & 0xfbff) == 0xf04f) /* mov.w Rd, #const */
1044 {
1045 unsigned int imm = ((bits (insn, 10, 10) << 11)
1046 | (bits (inst2, 12, 14) << 8)
1047 | bits (inst2, 0, 7));
1048
1049 regs[bits (inst2, 8, 11)]
1050 = pv_constant (thumb_expand_immediate (imm));
1051 }
1052
1053 else if ((insn & 0xfbf0) == 0xf240) /* movw Rd, #const */
1054 {
621c6d5b
YQ
1055 unsigned int imm
1056 = EXTRACT_MOVW_MOVT_IMM_T (insn, inst2);
ec3d575a
UW
1057
1058 regs[bits (inst2, 8, 11)] = pv_constant (imm);
1059 }
1060
1061 else if (insn == 0xea5f /* mov.w Rd,Rm */
1062 && (inst2 & 0xf0f0) == 0)
1063 {
1064 int dst_reg = (inst2 & 0x0f00) >> 8;
1065 int src_reg = inst2 & 0xf;
1066 regs[dst_reg] = regs[src_reg];
1067 }
1068
1069 else if ((insn & 0xff7f) == 0xf85f) /* ldr.w Rt,<label> */
1070 {
1071 /* Constant pool loads. */
1072 unsigned int constant;
1073 CORE_ADDR loc;
1074
1075 offset = bits (insn, 0, 11);
1076 if (insn & 0x0080)
1077 loc = start + 4 + offset;
1078 else
1079 loc = start + 4 - offset;
1080
1081 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1082 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1083 }
1084
1085 else if ((insn & 0xff7f) == 0xe95f) /* ldrd Rt,Rt2,<label> */
1086 {
1087 /* Constant pool loads. */
1088 unsigned int constant;
1089 CORE_ADDR loc;
1090
1091 offset = bits (insn, 0, 7) << 2;
1092 if (insn & 0x0080)
1093 loc = start + 4 + offset;
1094 else
1095 loc = start + 4 - offset;
1096
1097 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1098 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1099
1100 constant = read_memory_unsigned_integer (loc + 4, 4, byte_order);
1101 regs[bits (inst2, 8, 11)] = pv_constant (constant);
1102 }
1103
1104 else if (thumb2_instruction_changes_pc (insn, inst2))
1105 {
1106 /* Don't scan past anything that might change control flow. */
0d39a070
DJ
1107 break;
1108 }
ec3d575a
UW
1109 else
1110 {
1111 /* The optimizer might shove anything into the prologue,
1112 so we just skip what we don't recognize. */
1113 unrecognized_pc = start;
1114 }
0d39a070
DJ
1115
1116 start += 2;
1117 }
ec3d575a 1118 else if (thumb_instruction_changes_pc (insn))
3d74b771 1119 {
ec3d575a 1120 /* Don't scan past anything that might change control flow. */
da3c6d4a 1121 break;
3d74b771 1122 }
ec3d575a
UW
1123 else
1124 {
1125 /* The optimizer might shove anything into the prologue,
1126 so we just skip what we don't recognize. */
1127 unrecognized_pc = start;
1128 }
29d73ae4
DJ
1129
1130 start += 2;
c906108c
SS
1131 }
1132
0d39a070
DJ
1133 if (arm_debug)
1134 fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1135 paddress (gdbarch, start));
1136
ec3d575a
UW
1137 if (unrecognized_pc == 0)
1138 unrecognized_pc = start;
1139
29d73ae4
DJ
1140 if (cache == NULL)
1141 {
1142 do_cleanups (back_to);
ec3d575a 1143 return unrecognized_pc;
29d73ae4
DJ
1144 }
1145
29d73ae4
DJ
1146 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1147 {
1148 /* Frame pointer is fp. Frame size is constant. */
1149 cache->framereg = ARM_FP_REGNUM;
1150 cache->framesize = -regs[ARM_FP_REGNUM].k;
1151 }
1152 else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
1153 {
1154 /* Frame pointer is r7. Frame size is constant. */
1155 cache->framereg = THUMB_FP_REGNUM;
1156 cache->framesize = -regs[THUMB_FP_REGNUM].k;
1157 }
72a2e3dc 1158 else
29d73ae4
DJ
1159 {
1160 /* Try the stack pointer... this is a bit desperate. */
1161 cache->framereg = ARM_SP_REGNUM;
1162 cache->framesize = -regs[ARM_SP_REGNUM].k;
1163 }
29d73ae4
DJ
1164
1165 for (i = 0; i < 16; i++)
1166 if (pv_area_find_reg (stack, gdbarch, i, &offset))
1167 cache->saved_regs[i].addr = offset;
1168
1169 do_cleanups (back_to);
ec3d575a 1170 return unrecognized_pc;
c906108c
SS
1171}
1172
621c6d5b
YQ
1173
1174/* Try to analyze the instructions starting from PC, which load symbol
1175 __stack_chk_guard. Return the address of instruction after loading this
1176 symbol, set the dest register number to *BASEREG, and set the size of
1177 instructions for loading symbol in OFFSET. Return 0 if instructions are
1178 not recognized. */
1179
1180static CORE_ADDR
1181arm_analyze_load_stack_chk_guard(CORE_ADDR pc, struct gdbarch *gdbarch,
1182 unsigned int *destreg, int *offset)
1183{
1184 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1185 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1186 unsigned int low, high, address;
1187
1188 address = 0;
1189 if (is_thumb)
1190 {
1191 unsigned short insn1
1192 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
1193
1194 if ((insn1 & 0xf800) == 0x4800) /* ldr Rd, #immed */
1195 {
1196 *destreg = bits (insn1, 8, 10);
1197 *offset = 2;
1198 address = bits (insn1, 0, 7);
1199 }
1200 else if ((insn1 & 0xfbf0) == 0xf240) /* movw Rd, #const */
1201 {
1202 unsigned short insn2
1203 = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);
1204
1205 low = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1206
1207 insn1
1208 = read_memory_unsigned_integer (pc + 4, 2, byte_order_for_code);
1209 insn2
1210 = read_memory_unsigned_integer (pc + 6, 2, byte_order_for_code);
1211
1212 /* movt Rd, #const */
1213 if ((insn1 & 0xfbc0) == 0xf2c0)
1214 {
1215 high = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1216 *destreg = bits (insn2, 8, 11);
1217 *offset = 8;
1218 address = (high << 16 | low);
1219 }
1220 }
1221 }
1222 else
1223 {
2e9e421f
UW
1224 unsigned int insn
1225 = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
1226
1227 if ((insn & 0x0e5f0000) == 0x041f0000) /* ldr Rd, #immed */
1228 {
1229 address = bits (insn, 0, 11);
1230 *destreg = bits (insn, 12, 15);
1231 *offset = 4;
1232 }
1233 else if ((insn & 0x0ff00000) == 0x03000000) /* movw Rd, #const */
1234 {
1235 low = EXTRACT_MOVW_MOVT_IMM_A (insn);
1236
1237 insn
1238 = read_memory_unsigned_integer (pc + 4, 4, byte_order_for_code);
1239
1240 if ((insn & 0x0ff00000) == 0x03400000) /* movt Rd, #const */
1241 {
1242 high = EXTRACT_MOVW_MOVT_IMM_A (insn);
1243 *destreg = bits (insn, 12, 15);
1244 *offset = 8;
1245 address = (high << 16 | low);
1246 }
1247 }
621c6d5b
YQ
1248 }
1249
1250 return address;
1251}
1252
1253/* Try to skip a sequence of instructions used for stack protector. If PC
0963b4bd
MS
1254 points to the first instruction of this sequence, return the address of
1255 first instruction after this sequence, otherwise, return original PC.
621c6d5b
YQ
1256
1257 On arm, this sequence of instructions is composed of mainly three steps,
1258 Step 1: load symbol __stack_chk_guard,
1259 Step 2: load from address of __stack_chk_guard,
1260 Step 3: store it to somewhere else.
1261
1262 Usually, instructions on step 2 and step 3 are the same on various ARM
1263 architectures. On step 2, it is one instruction 'ldr Rx, [Rn, #0]', and
1264 on step 3, it is also one instruction 'str Rx, [r7, #immd]'. However,
1265 instructions in step 1 vary from different ARM architectures. On ARMv7,
1266 they are,
1267
1268 movw Rn, #:lower16:__stack_chk_guard
1269 movt Rn, #:upper16:__stack_chk_guard
1270
1271 On ARMv5t, it is,
1272
1273 ldr Rn, .Label
1274 ....
1275 .Lable:
1276 .word __stack_chk_guard
1277
1278 Since ldr/str is a very popular instruction, we can't use them as
1279 'fingerprint' or 'signature' of stack protector sequence. Here we choose
1280 sequence {movw/movt, ldr}/ldr/str plus symbol __stack_chk_guard, if not
1281 stripped, as the 'fingerprint' of a stack protector cdoe sequence. */
1282
1283static CORE_ADDR
1284arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
1285{
1286 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
22e048c9 1287 unsigned int basereg;
621c6d5b
YQ
1288 struct minimal_symbol *stack_chk_guard;
1289 int offset;
1290 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1291 CORE_ADDR addr;
1292
1293 /* Try to parse the instructions in Step 1. */
1294 addr = arm_analyze_load_stack_chk_guard (pc, gdbarch,
1295 &basereg, &offset);
1296 if (!addr)
1297 return pc;
1298
1299 stack_chk_guard = lookup_minimal_symbol_by_pc (addr);
1300 /* If name of symbol doesn't start with '__stack_chk_guard', this
1301 instruction sequence is not for stack protector. If symbol is
1302 removed, we conservatively think this sequence is for stack protector. */
1303 if (stack_chk_guard
c1c2ab58
UW
1304 && strncmp (SYMBOL_LINKAGE_NAME (stack_chk_guard), "__stack_chk_guard",
1305 strlen ("__stack_chk_guard")) != 0)
621c6d5b
YQ
1306 return pc;
1307
1308 if (is_thumb)
1309 {
1310 unsigned int destreg;
1311 unsigned short insn
1312 = read_memory_unsigned_integer (pc + offset, 2, byte_order_for_code);
1313
1314 /* Step 2: ldr Rd, [Rn, #immed], encoding T1. */
1315 if ((insn & 0xf800) != 0x6800)
1316 return pc;
1317 if (bits (insn, 3, 5) != basereg)
1318 return pc;
1319 destreg = bits (insn, 0, 2);
1320
1321 insn = read_memory_unsigned_integer (pc + offset + 2, 2,
1322 byte_order_for_code);
1323 /* Step 3: str Rd, [Rn, #immed], encoding T1. */
1324 if ((insn & 0xf800) != 0x6000)
1325 return pc;
1326 if (destreg != bits (insn, 0, 2))
1327 return pc;
1328 }
1329 else
1330 {
1331 unsigned int destreg;
1332 unsigned int insn
1333 = read_memory_unsigned_integer (pc + offset, 4, byte_order_for_code);
1334
1335 /* Step 2: ldr Rd, [Rn, #immed], encoding A1. */
1336 if ((insn & 0x0e500000) != 0x04100000)
1337 return pc;
1338 if (bits (insn, 16, 19) != basereg)
1339 return pc;
1340 destreg = bits (insn, 12, 15);
1341 /* Step 3: str Rd, [Rn, #immed], encoding A1. */
1342 insn = read_memory_unsigned_integer (pc + offset + 4,
1343 4, byte_order_for_code);
1344 if ((insn & 0x0e500000) != 0x04000000)
1345 return pc;
1346 if (bits (insn, 12, 15) != destreg)
1347 return pc;
1348 }
1349 /* The size of total two instructions ldr/str is 4 on Thumb-2, while 8
1350 on arm. */
1351 if (is_thumb)
1352 return pc + offset + 4;
1353 else
1354 return pc + offset + 8;
1355}
1356
da3c6d4a
MS
1357/* Advance the PC across any function entry prologue instructions to
1358 reach some "real" code.
34e8f22d
RE
1359
1360 The APCS (ARM Procedure Call Standard) defines the following
ed9a39eb 1361 prologue:
c906108c 1362
c5aa993b
JM
1363 mov ip, sp
1364 [stmfd sp!, {a1,a2,a3,a4}]
1365 stmfd sp!, {...,fp,ip,lr,pc}
ed9a39eb
JM
1366 [stfe f7, [sp, #-12]!]
1367 [stfe f6, [sp, #-12]!]
1368 [stfe f5, [sp, #-12]!]
1369 [stfe f4, [sp, #-12]!]
0963b4bd 1370 sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn. */
c906108c 1371
34e8f22d 1372static CORE_ADDR
6093d2eb 1373arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
c906108c 1374{
e17a4113 1375 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
c906108c
SS
1376 unsigned long inst;
1377 CORE_ADDR skip_pc;
a89fea3c 1378 CORE_ADDR func_addr, limit_pc;
c906108c 1379
a89fea3c
JL
1380 /* See if we can determine the end of the prologue via the symbol table.
1381 If so, then return either PC, or the PC after the prologue, whichever
1382 is greater. */
1383 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
c906108c 1384 {
d80b854b
UW
1385 CORE_ADDR post_prologue_pc
1386 = skip_prologue_using_sal (gdbarch, func_addr);
0d39a070
DJ
1387 struct symtab *s = find_pc_symtab (func_addr);
1388
621c6d5b
YQ
1389 if (post_prologue_pc)
1390 post_prologue_pc
1391 = arm_skip_stack_protector (post_prologue_pc, gdbarch);
1392
1393
0d39a070
DJ
1394 /* GCC always emits a line note before the prologue and another
1395 one after, even if the two are at the same address or on the
1396 same line. Take advantage of this so that we do not need to
1397 know every instruction that might appear in the prologue. We
1398 will have producer information for most binaries; if it is
1399 missing (e.g. for -gstabs), assuming the GNU tools. */
1400 if (post_prologue_pc
1401 && (s == NULL
1402 || s->producer == NULL
1403 || strncmp (s->producer, "GNU ", sizeof ("GNU ") - 1) == 0))
1404 return post_prologue_pc;
1405
a89fea3c 1406 if (post_prologue_pc != 0)
0d39a070
DJ
1407 {
1408 CORE_ADDR analyzed_limit;
1409
1410 /* For non-GCC compilers, make sure the entire line is an
1411 acceptable prologue; GDB will round this function's
1412 return value up to the end of the following line so we
1413 can not skip just part of a line (and we do not want to).
1414
1415 RealView does not treat the prologue specially, but does
1416 associate prologue code with the opening brace; so this
1417 lets us skip the first line if we think it is the opening
1418 brace. */
9779414d 1419 if (arm_pc_is_thumb (gdbarch, func_addr))
0d39a070
DJ
1420 analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
1421 post_prologue_pc, NULL);
1422 else
1423 analyzed_limit = arm_analyze_prologue (gdbarch, func_addr,
1424 post_prologue_pc, NULL);
1425
1426 if (analyzed_limit != post_prologue_pc)
1427 return func_addr;
1428
1429 return post_prologue_pc;
1430 }
c906108c
SS
1431 }
1432
a89fea3c
JL
1433 /* Can't determine prologue from the symbol table, need to examine
1434 instructions. */
c906108c 1435
a89fea3c
JL
1436 /* Find an upper limit on the function prologue using the debug
1437 information. If the debug information could not be used to provide
1438 that bound, then use an arbitrary large number as the upper bound. */
0963b4bd 1439 /* Like arm_scan_prologue, stop no later than pc + 64. */
d80b854b 1440 limit_pc = skip_prologue_using_sal (gdbarch, pc);
a89fea3c
JL
1441 if (limit_pc == 0)
1442 limit_pc = pc + 64; /* Magic. */
1443
c906108c 1444
29d73ae4 1445 /* Check if this is Thumb code. */
9779414d 1446 if (arm_pc_is_thumb (gdbarch, pc))
a89fea3c 1447 return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
29d73ae4 1448
a89fea3c 1449 for (skip_pc = pc; skip_pc < limit_pc; skip_pc += 4)
f43845b3 1450 {
e17a4113 1451 inst = read_memory_unsigned_integer (skip_pc, 4, byte_order_for_code);
9d4fde75 1452
b8d5e71d
MS
1453 /* "mov ip, sp" is no longer a required part of the prologue. */
1454 if (inst == 0xe1a0c00d) /* mov ip, sp */
1455 continue;
c906108c 1456
28cd8767
JG
1457 if ((inst & 0xfffff000) == 0xe28dc000) /* add ip, sp #n */
1458 continue;
1459
1460 if ((inst & 0xfffff000) == 0xe24dc000) /* sub ip, sp #n */
1461 continue;
1462
b8d5e71d
MS
1463 /* Some prologues begin with "str lr, [sp, #-4]!". */
1464 if (inst == 0xe52de004) /* str lr, [sp, #-4]! */
1465 continue;
c906108c 1466
b8d5e71d
MS
1467 if ((inst & 0xfffffff0) == 0xe92d0000) /* stmfd sp!,{a1,a2,a3,a4} */
1468 continue;
c906108c 1469
b8d5e71d
MS
1470 if ((inst & 0xfffff800) == 0xe92dd800) /* stmfd sp!,{fp,ip,lr,pc} */
1471 continue;
11d3b27d 1472
b8d5e71d
MS
1473 /* Any insns after this point may float into the code, if it makes
1474 for better instruction scheduling, so we skip them only if we
1475 find them, but still consider the function to be frame-ful. */
f43845b3 1476
b8d5e71d
MS
1477 /* We may have either one sfmfd instruction here, or several stfe
1478 insns, depending on the version of floating point code we
1479 support. */
1480 if ((inst & 0xffbf0fff) == 0xec2d0200) /* sfmfd fn, <cnt>, [sp]! */
1481 continue;
1482
1483 if ((inst & 0xffff8fff) == 0xed6d0103) /* stfe fn, [sp, #-12]! */
1484 continue;
1485
1486 if ((inst & 0xfffff000) == 0xe24cb000) /* sub fp, ip, #nn */
1487 continue;
1488
1489 if ((inst & 0xfffff000) == 0xe24dd000) /* sub sp, sp, #nn */
1490 continue;
1491
f8bf5763
PM
1492 if ((inst & 0xffffc000) == 0xe54b0000 /* strb r(0123),[r11,#-nn] */
1493 || (inst & 0xffffc0f0) == 0xe14b00b0 /* strh r(0123),[r11,#-nn] */
1494 || (inst & 0xffffc000) == 0xe50b0000) /* str r(0123),[r11,#-nn] */
b8d5e71d
MS
1495 continue;
1496
f8bf5763
PM
1497 if ((inst & 0xffffc000) == 0xe5cd0000 /* strb r(0123),[sp,#nn] */
1498 || (inst & 0xffffc0f0) == 0xe1cd00b0 /* strh r(0123),[sp,#nn] */
1499 || (inst & 0xffffc000) == 0xe58d0000) /* str r(0123),[sp,#nn] */
b8d5e71d
MS
1500 continue;
1501
1502 /* Un-recognized instruction; stop scanning. */
1503 break;
f43845b3 1504 }
c906108c 1505
0963b4bd 1506 return skip_pc; /* End of prologue. */
c906108c 1507}
94c30b78 1508
c5aa993b 1509/* *INDENT-OFF* */
c906108c
SS
1510/* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
1511 This function decodes a Thumb function prologue to determine:
1512 1) the size of the stack frame
1513 2) which registers are saved on it
1514 3) the offsets of saved regs
1515 4) the offset from the stack pointer to the frame pointer
c906108c 1516
da59e081
JM
1517 A typical Thumb function prologue would create this stack frame
1518 (offsets relative to FP)
c906108c
SS
1519 old SP -> 24 stack parameters
1520 20 LR
1521 16 R7
1522 R7 -> 0 local variables (16 bytes)
1523 SP -> -12 additional stack space (12 bytes)
1524 The frame size would thus be 36 bytes, and the frame offset would be
0963b4bd 1525 12 bytes. The frame register is R7.
da59e081 1526
da3c6d4a
MS
1527 The comments for thumb_skip_prolog() describe the algorithm we use
1528 to detect the end of the prolog. */
c5aa993b
JM
1529/* *INDENT-ON* */
1530
c906108c 1531static void
be8626e0 1532thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
b39cc962 1533 CORE_ADDR block_addr, struct arm_prologue_cache *cache)
c906108c
SS
1534{
1535 CORE_ADDR prologue_start;
1536 CORE_ADDR prologue_end;
c906108c 1537
b39cc962
DJ
1538 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1539 &prologue_end))
c906108c 1540 {
ec3d575a
UW
1541 /* See comment in arm_scan_prologue for an explanation of
1542 this heuristics. */
1543 if (prologue_end > prologue_start + 64)
1544 {
1545 prologue_end = prologue_start + 64;
1546 }
c906108c
SS
1547 }
1548 else
f7060f85
DJ
1549 /* We're in the boondocks: we have no idea where the start of the
1550 function is. */
1551 return;
c906108c 1552
eb5492fa 1553 prologue_end = min (prologue_end, prev_pc);
c906108c 1554
be8626e0 1555 thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
1556}
1557
0d39a070 1558/* Return 1 if THIS_INSTR might change control flow, 0 otherwise. */
c906108c 1559
0d39a070
DJ
1560static int
1561arm_instruction_changes_pc (uint32_t this_instr)
c906108c 1562{
0d39a070
DJ
1563 if (bits (this_instr, 28, 31) == INST_NV)
1564 /* Unconditional instructions. */
1565 switch (bits (this_instr, 24, 27))
1566 {
1567 case 0xa:
1568 case 0xb:
1569 /* Branch with Link and change to Thumb. */
1570 return 1;
1571 case 0xc:
1572 case 0xd:
1573 case 0xe:
1574 /* Coprocessor register transfer. */
1575 if (bits (this_instr, 12, 15) == 15)
1576 error (_("Invalid update to pc in instruction"));
1577 return 0;
1578 default:
1579 return 0;
1580 }
1581 else
1582 switch (bits (this_instr, 25, 27))
1583 {
1584 case 0x0:
1585 if (bits (this_instr, 23, 24) == 2 && bit (this_instr, 20) == 0)
1586 {
1587 /* Multiplies and extra load/stores. */
1588 if (bit (this_instr, 4) == 1 && bit (this_instr, 7) == 1)
1589 /* Neither multiplies nor extension load/stores are allowed
1590 to modify PC. */
1591 return 0;
1592
1593 /* Otherwise, miscellaneous instructions. */
1594
1595 /* BX <reg>, BXJ <reg>, BLX <reg> */
1596 if (bits (this_instr, 4, 27) == 0x12fff1
1597 || bits (this_instr, 4, 27) == 0x12fff2
1598 || bits (this_instr, 4, 27) == 0x12fff3)
1599 return 1;
1600
1601 /* Other miscellaneous instructions are unpredictable if they
1602 modify PC. */
1603 return 0;
1604 }
1605 /* Data processing instruction. Fall through. */
c906108c 1606
0d39a070
DJ
1607 case 0x1:
1608 if (bits (this_instr, 12, 15) == 15)
1609 return 1;
1610 else
1611 return 0;
c906108c 1612
0d39a070
DJ
1613 case 0x2:
1614 case 0x3:
1615 /* Media instructions and architecturally undefined instructions. */
1616 if (bits (this_instr, 25, 27) == 3 && bit (this_instr, 4) == 1)
1617 return 0;
c906108c 1618
0d39a070
DJ
1619 /* Stores. */
1620 if (bit (this_instr, 20) == 0)
1621 return 0;
2a451106 1622
0d39a070
DJ
1623 /* Loads. */
1624 if (bits (this_instr, 12, 15) == ARM_PC_REGNUM)
1625 return 1;
1626 else
1627 return 0;
2a451106 1628
0d39a070
DJ
1629 case 0x4:
1630 /* Load/store multiple. */
1631 if (bit (this_instr, 20) == 1 && bit (this_instr, 15) == 1)
1632 return 1;
1633 else
1634 return 0;
2a451106 1635
0d39a070
DJ
1636 case 0x5:
1637 /* Branch and branch with link. */
1638 return 1;
2a451106 1639
0d39a070
DJ
1640 case 0x6:
1641 case 0x7:
1642 /* Coprocessor transfers or SWIs can not affect PC. */
1643 return 0;
eb5492fa 1644
0d39a070 1645 default:
9b20d036 1646 internal_error (__FILE__, __LINE__, _("bad value in switch"));
0d39a070
DJ
1647 }
1648}
c906108c 1649
0d39a070
DJ
1650/* Analyze an ARM mode prologue starting at PROLOGUE_START and
1651 continuing no further than PROLOGUE_END. If CACHE is non-NULL,
1652 fill it in. Return the first address not recognized as a prologue
1653 instruction.
eb5492fa 1654
0d39a070
DJ
1655 We recognize all the instructions typically found in ARM prologues,
1656 plus harmless instructions which can be skipped (either for analysis
1657 purposes, or a more restrictive set that can be skipped when finding
1658 the end of the prologue). */
1659
1660static CORE_ADDR
1661arm_analyze_prologue (struct gdbarch *gdbarch,
1662 CORE_ADDR prologue_start, CORE_ADDR prologue_end,
1663 struct arm_prologue_cache *cache)
1664{
1665 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1666 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1667 int regno;
1668 CORE_ADDR offset, current_pc;
1669 pv_t regs[ARM_FPS_REGNUM];
1670 struct pv_area *stack;
1671 struct cleanup *back_to;
1672 int framereg, framesize;
1673 CORE_ADDR unrecognized_pc = 0;
1674
1675 /* Search the prologue looking for instructions that set up the
96baa820 1676 frame pointer, adjust the stack pointer, and save registers.
ed9a39eb 1677
96baa820
JM
1678 Be careful, however, and if it doesn't look like a prologue,
1679 don't try to scan it. If, for instance, a frameless function
1680 begins with stmfd sp!, then we will tell ourselves there is
b8d5e71d 1681 a frame, which will confuse stack traceback, as well as "finish"
96baa820 1682 and other operations that rely on a knowledge of the stack
0d39a070 1683 traceback. */
d4473757 1684
4be43953
DJ
1685 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1686 regs[regno] = pv_register (regno, 0);
55f960e1 1687 stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
4be43953
DJ
1688 back_to = make_cleanup_free_pv_area (stack);
1689
94c30b78
MS
1690 for (current_pc = prologue_start;
1691 current_pc < prologue_end;
f43845b3 1692 current_pc += 4)
96baa820 1693 {
e17a4113
UW
1694 unsigned int insn
1695 = read_memory_unsigned_integer (current_pc, 4, byte_order_for_code);
9d4fde75 1696
94c30b78 1697 if (insn == 0xe1a0c00d) /* mov ip, sp */
f43845b3 1698 {
4be43953 1699 regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
28cd8767
JG
1700 continue;
1701 }
0d39a070
DJ
1702 else if ((insn & 0xfff00000) == 0xe2800000 /* add Rd, Rn, #n */
1703 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767
JG
1704 {
1705 unsigned imm = insn & 0xff; /* immediate value */
1706 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
0d39a070 1707 int rd = bits (insn, 12, 15);
28cd8767 1708 imm = (imm >> rot) | (imm << (32 - rot));
0d39a070 1709 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
28cd8767
JG
1710 continue;
1711 }
0d39a070
DJ
1712 else if ((insn & 0xfff00000) == 0xe2400000 /* sub Rd, Rn, #n */
1713 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767
JG
1714 {
1715 unsigned imm = insn & 0xff; /* immediate value */
1716 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
0d39a070 1717 int rd = bits (insn, 12, 15);
28cd8767 1718 imm = (imm >> rot) | (imm << (32 - rot));
0d39a070 1719 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
f43845b3
MS
1720 continue;
1721 }
0963b4bd
MS
1722 else if ((insn & 0xffff0fff) == 0xe52d0004) /* str Rd,
1723 [sp, #-4]! */
f43845b3 1724 {
4be43953
DJ
1725 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1726 break;
1727 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
0d39a070
DJ
1728 pv_area_store (stack, regs[ARM_SP_REGNUM], 4,
1729 regs[bits (insn, 12, 15)]);
f43845b3
MS
1730 continue;
1731 }
1732 else if ((insn & 0xffff0000) == 0xe92d0000)
d4473757
KB
1733 /* stmfd sp!, {..., fp, ip, lr, pc}
1734 or
1735 stmfd sp!, {a1, a2, a3, a4} */
c906108c 1736 {
d4473757 1737 int mask = insn & 0xffff;
ed9a39eb 1738
4be43953
DJ
1739 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1740 break;
1741
94c30b78 1742 /* Calculate offsets of saved registers. */
34e8f22d 1743 for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
d4473757
KB
1744 if (mask & (1 << regno))
1745 {
0963b4bd
MS
1746 regs[ARM_SP_REGNUM]
1747 = pv_add_constant (regs[ARM_SP_REGNUM], -4);
4be43953 1748 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
d4473757
KB
1749 }
1750 }
0d39a070
DJ
1751 else if ((insn & 0xffff0000) == 0xe54b0000 /* strb rx,[r11,#-n] */
1752 || (insn & 0xffff00f0) == 0xe14b00b0 /* strh rx,[r11,#-n] */
f8bf5763 1753 || (insn & 0xffffc000) == 0xe50b0000) /* str rx,[r11,#-n] */
b8d5e71d
MS
1754 {
1755 /* No need to add this to saved_regs -- it's just an arg reg. */
1756 continue;
1757 }
0d39a070
DJ
1758 else if ((insn & 0xffff0000) == 0xe5cd0000 /* strb rx,[sp,#n] */
1759 || (insn & 0xffff00f0) == 0xe1cd00b0 /* strh rx,[sp,#n] */
f8bf5763 1760 || (insn & 0xffffc000) == 0xe58d0000) /* str rx,[sp,#n] */
f43845b3
MS
1761 {
1762 /* No need to add this to saved_regs -- it's just an arg reg. */
1763 continue;
1764 }
0963b4bd
MS
1765 else if ((insn & 0xfff00000) == 0xe8800000 /* stm Rn,
1766 { registers } */
0d39a070
DJ
1767 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1768 {
1769 /* No need to add this to saved_regs -- it's just arg regs. */
1770 continue;
1771 }
d4473757
KB
1772 else if ((insn & 0xfffff000) == 0xe24cb000) /* sub fp, ip #n */
1773 {
94c30b78
MS
1774 unsigned imm = insn & 0xff; /* immediate value */
1775 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
d4473757 1776 imm = (imm >> rot) | (imm << (32 - rot));
4be43953 1777 regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
d4473757
KB
1778 }
1779 else if ((insn & 0xfffff000) == 0xe24dd000) /* sub sp, sp #n */
1780 {
94c30b78
MS
1781 unsigned imm = insn & 0xff; /* immediate value */
1782 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
d4473757 1783 imm = (imm >> rot) | (imm << (32 - rot));
4be43953 1784 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
d4473757 1785 }
0963b4bd
MS
1786 else if ((insn & 0xffff7fff) == 0xed6d0103 /* stfe f?,
1787 [sp, -#c]! */
2af46ca0 1788 && gdbarch_tdep (gdbarch)->have_fpa_registers)
d4473757 1789 {
4be43953
DJ
1790 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1791 break;
1792
1793 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
34e8f22d 1794 regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
4be43953 1795 pv_area_store (stack, regs[ARM_SP_REGNUM], 12, regs[regno]);
d4473757 1796 }
0963b4bd
MS
1797 else if ((insn & 0xffbf0fff) == 0xec2d0200 /* sfmfd f0, 4,
1798 [sp!] */
2af46ca0 1799 && gdbarch_tdep (gdbarch)->have_fpa_registers)
d4473757
KB
1800 {
1801 int n_saved_fp_regs;
1802 unsigned int fp_start_reg, fp_bound_reg;
1803
4be43953
DJ
1804 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1805 break;
1806
94c30b78 1807 if ((insn & 0x800) == 0x800) /* N0 is set */
96baa820 1808 {
d4473757
KB
1809 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1810 n_saved_fp_regs = 3;
1811 else
1812 n_saved_fp_regs = 1;
96baa820 1813 }
d4473757 1814 else
96baa820 1815 {
d4473757
KB
1816 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1817 n_saved_fp_regs = 2;
1818 else
1819 n_saved_fp_regs = 4;
96baa820 1820 }
d4473757 1821
34e8f22d 1822 fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
d4473757
KB
1823 fp_bound_reg = fp_start_reg + n_saved_fp_regs;
1824 for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
96baa820 1825 {
4be43953
DJ
1826 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
1827 pv_area_store (stack, regs[ARM_SP_REGNUM], 12,
1828 regs[fp_start_reg++]);
96baa820 1829 }
c906108c 1830 }
0d39a070
DJ
1831 else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
1832 {
1833 /* Allow some special function calls when skipping the
1834 prologue; GCC generates these before storing arguments to
1835 the stack. */
1836 CORE_ADDR dest = BranchDest (current_pc, insn);
1837
e0634ccf 1838 if (skip_prologue_function (gdbarch, dest, 0))
0d39a070
DJ
1839 continue;
1840 else
1841 break;
1842 }
d4473757 1843 else if ((insn & 0xf0000000) != 0xe0000000)
0963b4bd 1844 break; /* Condition not true, exit early. */
0d39a070
DJ
1845 else if (arm_instruction_changes_pc (insn))
1846 /* Don't scan past anything that might change control flow. */
1847 break;
d19f7eee
UW
1848 else if ((insn & 0xfe500000) == 0xe8100000 /* ldm */
1849 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1850 /* Ignore block loads from the stack, potentially copying
1851 parameters from memory. */
1852 continue;
1853 else if ((insn & 0xfc500000) == 0xe4100000
1854 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1855 /* Similarly ignore single loads from the stack. */
1856 continue;
0d39a070
DJ
1857 else if ((insn & 0xffff0ff0) == 0xe1a00000)
1858 /* MOV Rd, Rm. Skip register copies, i.e. saves to another
1859 register instead of the stack. */
d4473757 1860 continue;
0d39a070
DJ
1861 else
1862 {
1863 /* The optimizer might shove anything into the prologue,
1864 so we just skip what we don't recognize. */
1865 unrecognized_pc = current_pc;
1866 continue;
1867 }
c906108c
SS
1868 }
1869
0d39a070
DJ
1870 if (unrecognized_pc == 0)
1871 unrecognized_pc = current_pc;
1872
4be43953
DJ
1873 /* The frame size is just the distance from the frame register
1874 to the original stack pointer. */
1875 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1876 {
1877 /* Frame pointer is fp. */
0d39a070
DJ
1878 framereg = ARM_FP_REGNUM;
1879 framesize = -regs[ARM_FP_REGNUM].k;
4be43953 1880 }
72a2e3dc 1881 else
4be43953
DJ
1882 {
1883 /* Try the stack pointer... this is a bit desperate. */
0d39a070
DJ
1884 framereg = ARM_SP_REGNUM;
1885 framesize = -regs[ARM_SP_REGNUM].k;
4be43953 1886 }
4be43953 1887
0d39a070
DJ
1888 if (cache)
1889 {
1890 cache->framereg = framereg;
1891 cache->framesize = framesize;
1892
1893 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1894 if (pv_area_find_reg (stack, gdbarch, regno, &offset))
1895 cache->saved_regs[regno].addr = offset;
1896 }
1897
1898 if (arm_debug)
1899 fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1900 paddress (gdbarch, unrecognized_pc));
4be43953
DJ
1901
1902 do_cleanups (back_to);
0d39a070
DJ
1903 return unrecognized_pc;
1904}
1905
1906static void
1907arm_scan_prologue (struct frame_info *this_frame,
1908 struct arm_prologue_cache *cache)
1909{
1910 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1911 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1912 int regno;
1913 CORE_ADDR prologue_start, prologue_end, current_pc;
1914 CORE_ADDR prev_pc = get_frame_pc (this_frame);
1915 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1916 pv_t regs[ARM_FPS_REGNUM];
1917 struct pv_area *stack;
1918 struct cleanup *back_to;
1919 CORE_ADDR offset;
1920
1921 /* Assume there is no frame until proven otherwise. */
1922 cache->framereg = ARM_SP_REGNUM;
1923 cache->framesize = 0;
1924
1925 /* Check for Thumb prologue. */
1926 if (arm_frame_is_thumb (this_frame))
1927 {
1928 thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
1929 return;
1930 }
1931
1932 /* Find the function prologue. If we can't find the function in
1933 the symbol table, peek in the stack frame to find the PC. */
1934 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1935 &prologue_end))
1936 {
1937 /* One way to find the end of the prologue (which works well
1938 for unoptimized code) is to do the following:
1939
1940 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
1941
1942 if (sal.line == 0)
1943 prologue_end = prev_pc;
1944 else if (sal.end < prologue_end)
1945 prologue_end = sal.end;
1946
1947 This mechanism is very accurate so long as the optimizer
1948 doesn't move any instructions from the function body into the
1949 prologue. If this happens, sal.end will be the last
1950 instruction in the first hunk of prologue code just before
1951 the first instruction that the scheduler has moved from
1952 the body to the prologue.
1953
1954 In order to make sure that we scan all of the prologue
1955 instructions, we use a slightly less accurate mechanism which
1956 may scan more than necessary. To help compensate for this
1957 lack of accuracy, the prologue scanning loop below contains
1958 several clauses which'll cause the loop to terminate early if
1959 an implausible prologue instruction is encountered.
1960
1961 The expression
1962
1963 prologue_start + 64
1964
1965 is a suitable endpoint since it accounts for the largest
1966 possible prologue plus up to five instructions inserted by
1967 the scheduler. */
1968
1969 if (prologue_end > prologue_start + 64)
1970 {
1971 prologue_end = prologue_start + 64; /* See above. */
1972 }
1973 }
1974 else
1975 {
1976 /* We have no symbol information. Our only option is to assume this
1977 function has a standard stack frame and the normal frame register.
1978 Then, we can find the value of our frame pointer on entrance to
1979 the callee (or at the present moment if this is the innermost frame).
1980 The value stored there should be the address of the stmfd + 8. */
1981 CORE_ADDR frame_loc;
1982 LONGEST return_value;
1983
1984 frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
1985 if (!safe_read_memory_integer (frame_loc, 4, byte_order, &return_value))
1986 return;
1987 else
1988 {
1989 prologue_start = gdbarch_addr_bits_remove
1990 (gdbarch, return_value) - 8;
1991 prologue_end = prologue_start + 64; /* See above. */
1992 }
1993 }
1994
1995 if (prev_pc < prologue_end)
1996 prologue_end = prev_pc;
1997
1998 arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
1999}
2000
eb5492fa 2001static struct arm_prologue_cache *
a262aec2 2002arm_make_prologue_cache (struct frame_info *this_frame)
c906108c 2003{
eb5492fa
DJ
2004 int reg;
2005 struct arm_prologue_cache *cache;
2006 CORE_ADDR unwound_fp;
c5aa993b 2007
35d5d4ee 2008 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
a262aec2 2009 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
c906108c 2010
a262aec2 2011 arm_scan_prologue (this_frame, cache);
848cfffb 2012
a262aec2 2013 unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
eb5492fa
DJ
2014 if (unwound_fp == 0)
2015 return cache;
c906108c 2016
4be43953 2017 cache->prev_sp = unwound_fp + cache->framesize;
c906108c 2018
eb5492fa
DJ
2019 /* Calculate actual addresses of saved registers using offsets
2020 determined by arm_scan_prologue. */
a262aec2 2021 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
e28a332c 2022 if (trad_frame_addr_p (cache->saved_regs, reg))
eb5492fa
DJ
2023 cache->saved_regs[reg].addr += cache->prev_sp;
2024
2025 return cache;
c906108c
SS
2026}
2027
eb5492fa
DJ
2028/* Our frame ID for a normal frame is the current function's starting PC
2029 and the caller's SP when we were called. */
c906108c 2030
148754e5 2031static void
a262aec2 2032arm_prologue_this_id (struct frame_info *this_frame,
eb5492fa
DJ
2033 void **this_cache,
2034 struct frame_id *this_id)
c906108c 2035{
eb5492fa
DJ
2036 struct arm_prologue_cache *cache;
2037 struct frame_id id;
2c404490 2038 CORE_ADDR pc, func;
f079148d 2039
eb5492fa 2040 if (*this_cache == NULL)
a262aec2 2041 *this_cache = arm_make_prologue_cache (this_frame);
eb5492fa 2042 cache = *this_cache;
2a451106 2043
2c404490
DJ
2044 /* This is meant to halt the backtrace at "_start". */
2045 pc = get_frame_pc (this_frame);
2046 if (pc <= gdbarch_tdep (get_frame_arch (this_frame))->lowest_pc)
eb5492fa 2047 return;
5a203e44 2048
eb5492fa
DJ
2049 /* If we've hit a wall, stop. */
2050 if (cache->prev_sp == 0)
2051 return;
24de872b 2052
0e9e9abd
UW
2053 /* Use function start address as part of the frame ID. If we cannot
2054 identify the start address (due to missing symbol information),
2055 fall back to just using the current PC. */
2c404490 2056 func = get_frame_func (this_frame);
0e9e9abd
UW
2057 if (!func)
2058 func = pc;
2059
eb5492fa 2060 id = frame_id_build (cache->prev_sp, func);
eb5492fa 2061 *this_id = id;
c906108c
SS
2062}
2063
a262aec2
DJ
2064static struct value *
2065arm_prologue_prev_register (struct frame_info *this_frame,
eb5492fa 2066 void **this_cache,
a262aec2 2067 int prev_regnum)
24de872b 2068{
24568a2c 2069 struct gdbarch *gdbarch = get_frame_arch (this_frame);
24de872b
DJ
2070 struct arm_prologue_cache *cache;
2071
eb5492fa 2072 if (*this_cache == NULL)
a262aec2 2073 *this_cache = arm_make_prologue_cache (this_frame);
eb5492fa 2074 cache = *this_cache;
24de872b 2075
eb5492fa 2076 /* If we are asked to unwind the PC, then we need to return the LR
b39cc962
DJ
2077 instead. The prologue may save PC, but it will point into this
2078 frame's prologue, not the next frame's resume location. Also
2079 strip the saved T bit. A valid LR may have the low bit set, but
2080 a valid PC never does. */
eb5492fa 2081 if (prev_regnum == ARM_PC_REGNUM)
b39cc962
DJ
2082 {
2083 CORE_ADDR lr;
2084
2085 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2086 return frame_unwind_got_constant (this_frame, prev_regnum,
24568a2c 2087 arm_addr_bits_remove (gdbarch, lr));
b39cc962 2088 }
24de872b 2089
eb5492fa 2090 /* SP is generally not saved to the stack, but this frame is
a262aec2 2091 identified by the next frame's stack pointer at the time of the call.
eb5492fa
DJ
2092 The value was already reconstructed into PREV_SP. */
2093 if (prev_regnum == ARM_SP_REGNUM)
a262aec2 2094 return frame_unwind_got_constant (this_frame, prev_regnum, cache->prev_sp);
eb5492fa 2095
b39cc962
DJ
2096 /* The CPSR may have been changed by the call instruction and by the
2097 called function. The only bit we can reconstruct is the T bit,
2098 by checking the low bit of LR as of the call. This is a reliable
2099 indicator of Thumb-ness except for some ARM v4T pre-interworking
2100 Thumb code, which could get away with a clear low bit as long as
2101 the called function did not use bx. Guess that all other
2102 bits are unchanged; the condition flags are presumably lost,
2103 but the processor status is likely valid. */
2104 if (prev_regnum == ARM_PS_REGNUM)
2105 {
2106 CORE_ADDR lr, cpsr;
9779414d 2107 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
b39cc962
DJ
2108
2109 cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
2110 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2111 if (IS_THUMB_ADDR (lr))
9779414d 2112 cpsr |= t_bit;
b39cc962 2113 else
9779414d 2114 cpsr &= ~t_bit;
b39cc962
DJ
2115 return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
2116 }
2117
a262aec2
DJ
2118 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
2119 prev_regnum);
eb5492fa
DJ
2120}
2121
2122struct frame_unwind arm_prologue_unwind = {
2123 NORMAL_FRAME,
8fbca658 2124 default_frame_unwind_stop_reason,
eb5492fa 2125 arm_prologue_this_id,
a262aec2
DJ
2126 arm_prologue_prev_register,
2127 NULL,
2128 default_frame_sniffer
eb5492fa
DJ
2129};
2130
0e9e9abd
UW
2131/* Maintain a list of ARM exception table entries per objfile, similar to the
2132 list of mapping symbols. We only cache entries for standard ARM-defined
2133 personality routines; the cache will contain only the frame unwinding
2134 instructions associated with the entry (not the descriptors). */
2135
2136static const struct objfile_data *arm_exidx_data_key;
2137
2138struct arm_exidx_entry
2139{
2140 bfd_vma addr;
2141 gdb_byte *entry;
2142};
2143typedef struct arm_exidx_entry arm_exidx_entry_s;
2144DEF_VEC_O(arm_exidx_entry_s);
2145
2146struct arm_exidx_data
2147{
2148 VEC(arm_exidx_entry_s) **section_maps;
2149};
2150
2151static void
2152arm_exidx_data_free (struct objfile *objfile, void *arg)
2153{
2154 struct arm_exidx_data *data = arg;
2155 unsigned int i;
2156
2157 for (i = 0; i < objfile->obfd->section_count; i++)
2158 VEC_free (arm_exidx_entry_s, data->section_maps[i]);
2159}
2160
2161static inline int
2162arm_compare_exidx_entries (const struct arm_exidx_entry *lhs,
2163 const struct arm_exidx_entry *rhs)
2164{
2165 return lhs->addr < rhs->addr;
2166}
2167
2168static struct obj_section *
2169arm_obj_section_from_vma (struct objfile *objfile, bfd_vma vma)
2170{
2171 struct obj_section *osect;
2172
2173 ALL_OBJFILE_OSECTIONS (objfile, osect)
2174 if (bfd_get_section_flags (objfile->obfd,
2175 osect->the_bfd_section) & SEC_ALLOC)
2176 {
2177 bfd_vma start, size;
2178 start = bfd_get_section_vma (objfile->obfd, osect->the_bfd_section);
2179 size = bfd_get_section_size (osect->the_bfd_section);
2180
2181 if (start <= vma && vma < start + size)
2182 return osect;
2183 }
2184
2185 return NULL;
2186}
2187
2188/* Parse contents of exception table and exception index sections
2189 of OBJFILE, and fill in the exception table entry cache.
2190
2191 For each entry that refers to a standard ARM-defined personality
2192 routine, extract the frame unwinding instructions (from either
2193 the index or the table section). The unwinding instructions
2194 are normalized by:
2195 - extracting them from the rest of the table data
2196 - converting to host endianness
2197 - appending the implicit 0xb0 ("Finish") code
2198
2199 The extracted and normalized instructions are stored for later
2200 retrieval by the arm_find_exidx_entry routine. */
2201
2202static void
2203arm_exidx_new_objfile (struct objfile *objfile)
2204{
3bb47e8b 2205 struct cleanup *cleanups;
0e9e9abd
UW
2206 struct arm_exidx_data *data;
2207 asection *exidx, *extab;
2208 bfd_vma exidx_vma = 0, extab_vma = 0;
2209 bfd_size_type exidx_size = 0, extab_size = 0;
2210 gdb_byte *exidx_data = NULL, *extab_data = NULL;
2211 LONGEST i;
2212
2213 /* If we've already touched this file, do nothing. */
2214 if (!objfile || objfile_data (objfile, arm_exidx_data_key) != NULL)
2215 return;
3bb47e8b 2216 cleanups = make_cleanup (null_cleanup, NULL);
0e9e9abd
UW
2217
2218 /* Read contents of exception table and index. */
2219 exidx = bfd_get_section_by_name (objfile->obfd, ".ARM.exidx");
2220 if (exidx)
2221 {
2222 exidx_vma = bfd_section_vma (objfile->obfd, exidx);
2223 exidx_size = bfd_get_section_size (exidx);
2224 exidx_data = xmalloc (exidx_size);
2225 make_cleanup (xfree, exidx_data);
2226
2227 if (!bfd_get_section_contents (objfile->obfd, exidx,
2228 exidx_data, 0, exidx_size))
2229 {
2230 do_cleanups (cleanups);
2231 return;
2232 }
2233 }
2234
2235 extab = bfd_get_section_by_name (objfile->obfd, ".ARM.extab");
2236 if (extab)
2237 {
2238 extab_vma = bfd_section_vma (objfile->obfd, extab);
2239 extab_size = bfd_get_section_size (extab);
2240 extab_data = xmalloc (extab_size);
2241 make_cleanup (xfree, extab_data);
2242
2243 if (!bfd_get_section_contents (objfile->obfd, extab,
2244 extab_data, 0, extab_size))
2245 {
2246 do_cleanups (cleanups);
2247 return;
2248 }
2249 }
2250
2251 /* Allocate exception table data structure. */
2252 data = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct arm_exidx_data);
2253 set_objfile_data (objfile, arm_exidx_data_key, data);
2254 data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
2255 objfile->obfd->section_count,
2256 VEC(arm_exidx_entry_s) *);
2257
2258 /* Fill in exception table. */
2259 for (i = 0; i < exidx_size / 8; i++)
2260 {
2261 struct arm_exidx_entry new_exidx_entry;
2262 bfd_vma idx = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8);
2263 bfd_vma val = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8 + 4);
2264 bfd_vma addr = 0, word = 0;
2265 int n_bytes = 0, n_words = 0;
2266 struct obj_section *sec;
2267 gdb_byte *entry = NULL;
2268
2269 /* Extract address of start of function. */
2270 idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2271 idx += exidx_vma + i * 8;
2272
2273 /* Find section containing function and compute section offset. */
2274 sec = arm_obj_section_from_vma (objfile, idx);
2275 if (sec == NULL)
2276 continue;
2277 idx -= bfd_get_section_vma (objfile->obfd, sec->the_bfd_section);
2278
2279 /* Determine address of exception table entry. */
2280 if (val == 1)
2281 {
2282 /* EXIDX_CANTUNWIND -- no exception table entry present. */
2283 }
2284 else if ((val & 0xff000000) == 0x80000000)
2285 {
2286 /* Exception table entry embedded in .ARM.exidx
2287 -- must be short form. */
2288 word = val;
2289 n_bytes = 3;
2290 }
2291 else if (!(val & 0x80000000))
2292 {
2293 /* Exception table entry in .ARM.extab. */
2294 addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2295 addr += exidx_vma + i * 8 + 4;
2296
2297 if (addr >= extab_vma && addr + 4 <= extab_vma + extab_size)
2298 {
2299 word = bfd_h_get_32 (objfile->obfd,
2300 extab_data + addr - extab_vma);
2301 addr += 4;
2302
2303 if ((word & 0xff000000) == 0x80000000)
2304 {
2305 /* Short form. */
2306 n_bytes = 3;
2307 }
2308 else if ((word & 0xff000000) == 0x81000000
2309 || (word & 0xff000000) == 0x82000000)
2310 {
2311 /* Long form. */
2312 n_bytes = 2;
2313 n_words = ((word >> 16) & 0xff);
2314 }
2315 else if (!(word & 0x80000000))
2316 {
2317 bfd_vma pers;
2318 struct obj_section *pers_sec;
2319 int gnu_personality = 0;
2320
2321 /* Custom personality routine. */
2322 pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2323 pers = UNMAKE_THUMB_ADDR (pers + addr - 4);
2324
2325 /* Check whether we've got one of the variants of the
2326 GNU personality routines. */
2327 pers_sec = arm_obj_section_from_vma (objfile, pers);
2328 if (pers_sec)
2329 {
2330 static const char *personality[] =
2331 {
2332 "__gcc_personality_v0",
2333 "__gxx_personality_v0",
2334 "__gcj_personality_v0",
2335 "__gnu_objc_personality_v0",
2336 NULL
2337 };
2338
2339 CORE_ADDR pc = pers + obj_section_offset (pers_sec);
2340 int k;
2341
2342 for (k = 0; personality[k]; k++)
2343 if (lookup_minimal_symbol_by_pc_name
2344 (pc, personality[k], objfile))
2345 {
2346 gnu_personality = 1;
2347 break;
2348 }
2349 }
2350
2351 /* If so, the next word contains a word count in the high
2352 byte, followed by the same unwind instructions as the
2353 pre-defined forms. */
2354 if (gnu_personality
2355 && addr + 4 <= extab_vma + extab_size)
2356 {
2357 word = bfd_h_get_32 (objfile->obfd,
2358 extab_data + addr - extab_vma);
2359 addr += 4;
2360 n_bytes = 3;
2361 n_words = ((word >> 24) & 0xff);
2362 }
2363 }
2364 }
2365 }
2366
2367 /* Sanity check address. */
2368 if (n_words)
2369 if (addr < extab_vma || addr + 4 * n_words > extab_vma + extab_size)
2370 n_words = n_bytes = 0;
2371
2372 /* The unwind instructions reside in WORD (only the N_BYTES least
2373 significant bytes are valid), followed by N_WORDS words in the
2374 extab section starting at ADDR. */
2375 if (n_bytes || n_words)
2376 {
2377 gdb_byte *p = entry = obstack_alloc (&objfile->objfile_obstack,
2378 n_bytes + n_words * 4 + 1);
2379
2380 while (n_bytes--)
2381 *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);
2382
2383 while (n_words--)
2384 {
2385 word = bfd_h_get_32 (objfile->obfd,
2386 extab_data + addr - extab_vma);
2387 addr += 4;
2388
2389 *p++ = (gdb_byte) ((word >> 24) & 0xff);
2390 *p++ = (gdb_byte) ((word >> 16) & 0xff);
2391 *p++ = (gdb_byte) ((word >> 8) & 0xff);
2392 *p++ = (gdb_byte) (word & 0xff);
2393 }
2394
2395 /* Implied "Finish" to terminate the list. */
2396 *p++ = 0xb0;
2397 }
2398
2399 /* Push entry onto vector. They are guaranteed to always
2400 appear in order of increasing addresses. */
2401 new_exidx_entry.addr = idx;
2402 new_exidx_entry.entry = entry;
2403 VEC_safe_push (arm_exidx_entry_s,
2404 data->section_maps[sec->the_bfd_section->index],
2405 &new_exidx_entry);
2406 }
2407
2408 do_cleanups (cleanups);
2409}
2410
2411/* Search for the exception table entry covering MEMADDR. If one is found,
2412 return a pointer to its data. Otherwise, return 0. If START is non-NULL,
2413 set *START to the start of the region covered by this entry. */
2414
2415static gdb_byte *
2416arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
2417{
2418 struct obj_section *sec;
2419
2420 sec = find_pc_section (memaddr);
2421 if (sec != NULL)
2422 {
2423 struct arm_exidx_data *data;
2424 VEC(arm_exidx_entry_s) *map;
2425 struct arm_exidx_entry map_key = { memaddr - obj_section_addr (sec), 0 };
2426 unsigned int idx;
2427
2428 data = objfile_data (sec->objfile, arm_exidx_data_key);
2429 if (data != NULL)
2430 {
2431 map = data->section_maps[sec->the_bfd_section->index];
2432 if (!VEC_empty (arm_exidx_entry_s, map))
2433 {
2434 struct arm_exidx_entry *map_sym;
2435
2436 idx = VEC_lower_bound (arm_exidx_entry_s, map, &map_key,
2437 arm_compare_exidx_entries);
2438
2439 /* VEC_lower_bound finds the earliest ordered insertion
2440 point. If the following symbol starts at this exact
2441 address, we use that; otherwise, the preceding
2442 exception table entry covers this address. */
2443 if (idx < VEC_length (arm_exidx_entry_s, map))
2444 {
2445 map_sym = VEC_index (arm_exidx_entry_s, map, idx);
2446 if (map_sym->addr == map_key.addr)
2447 {
2448 if (start)
2449 *start = map_sym->addr + obj_section_addr (sec);
2450 return map_sym->entry;
2451 }
2452 }
2453
2454 if (idx > 0)
2455 {
2456 map_sym = VEC_index (arm_exidx_entry_s, map, idx - 1);
2457 if (start)
2458 *start = map_sym->addr + obj_section_addr (sec);
2459 return map_sym->entry;
2460 }
2461 }
2462 }
2463 }
2464
2465 return NULL;
2466}
2467
2468/* Given the current frame THIS_FRAME, and its associated frame unwinding
2469 instruction list from the ARM exception table entry ENTRY, allocate and
2470 return a prologue cache structure describing how to unwind this frame.
2471
2472 Return NULL if the unwinding instruction list contains a "spare",
2473 "reserved" or "refuse to unwind" instruction as defined in section
2474 "9.3 Frame unwinding instructions" of the "Exception Handling ABI
2475 for the ARM Architecture" document. */
2476
2477static struct arm_prologue_cache *
2478arm_exidx_fill_cache (struct frame_info *this_frame, gdb_byte *entry)
2479{
2480 CORE_ADDR vsp = 0;
2481 int vsp_valid = 0;
2482
2483 struct arm_prologue_cache *cache;
2484 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2485 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2486
2487 for (;;)
2488 {
2489 gdb_byte insn;
2490
2491 /* Whenever we reload SP, we actually have to retrieve its
2492 actual value in the current frame. */
2493 if (!vsp_valid)
2494 {
2495 if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2496 {
2497 int reg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2498 vsp = get_frame_register_unsigned (this_frame, reg);
2499 }
2500 else
2501 {
2502 CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr;
2503 vsp = get_frame_memory_unsigned (this_frame, addr, 4);
2504 }
2505
2506 vsp_valid = 1;
2507 }
2508
2509 /* Decode next unwind instruction. */
2510 insn = *entry++;
2511
2512 if ((insn & 0xc0) == 0)
2513 {
2514 int offset = insn & 0x3f;
2515 vsp += (offset << 2) + 4;
2516 }
2517 else if ((insn & 0xc0) == 0x40)
2518 {
2519 int offset = insn & 0x3f;
2520 vsp -= (offset << 2) + 4;
2521 }
2522 else if ((insn & 0xf0) == 0x80)
2523 {
2524 int mask = ((insn & 0xf) << 8) | *entry++;
2525 int i;
2526
2527 /* The special case of an all-zero mask identifies
2528 "Refuse to unwind". We return NULL to fall back
2529 to the prologue analyzer. */
2530 if (mask == 0)
2531 return NULL;
2532
2533 /* Pop registers r4..r15 under mask. */
2534 for (i = 0; i < 12; i++)
2535 if (mask & (1 << i))
2536 {
2537 cache->saved_regs[4 + i].addr = vsp;
2538 vsp += 4;
2539 }
2540
2541 /* Special-case popping SP -- we need to reload vsp. */
2542 if (mask & (1 << (ARM_SP_REGNUM - 4)))
2543 vsp_valid = 0;
2544 }
2545 else if ((insn & 0xf0) == 0x90)
2546 {
2547 int reg = insn & 0xf;
2548
2549 /* Reserved cases. */
2550 if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
2551 return NULL;
2552
2553 /* Set SP from another register and mark VSP for reload. */
2554 cache->saved_regs[ARM_SP_REGNUM] = cache->saved_regs[reg];
2555 vsp_valid = 0;
2556 }
2557 else if ((insn & 0xf0) == 0xa0)
2558 {
2559 int count = insn & 0x7;
2560 int pop_lr = (insn & 0x8) != 0;
2561 int i;
2562
2563 /* Pop r4..r[4+count]. */
2564 for (i = 0; i <= count; i++)
2565 {
2566 cache->saved_regs[4 + i].addr = vsp;
2567 vsp += 4;
2568 }
2569
2570 /* If indicated by flag, pop LR as well. */
2571 if (pop_lr)
2572 {
2573 cache->saved_regs[ARM_LR_REGNUM].addr = vsp;
2574 vsp += 4;
2575 }
2576 }
2577 else if (insn == 0xb0)
2578 {
2579 /* We could only have updated PC by popping into it; if so, it
2580 will show up as address. Otherwise, copy LR into PC. */
2581 if (!trad_frame_addr_p (cache->saved_regs, ARM_PC_REGNUM))
2582 cache->saved_regs[ARM_PC_REGNUM]
2583 = cache->saved_regs[ARM_LR_REGNUM];
2584
2585 /* We're done. */
2586 break;
2587 }
2588 else if (insn == 0xb1)
2589 {
2590 int mask = *entry++;
2591 int i;
2592
2593 /* All-zero mask and mask >= 16 is "spare". */
2594 if (mask == 0 || mask >= 16)
2595 return NULL;
2596
2597 /* Pop r0..r3 under mask. */
2598 for (i = 0; i < 4; i++)
2599 if (mask & (1 << i))
2600 {
2601 cache->saved_regs[i].addr = vsp;
2602 vsp += 4;
2603 }
2604 }
2605 else if (insn == 0xb2)
2606 {
2607 ULONGEST offset = 0;
2608 unsigned shift = 0;
2609
2610 do
2611 {
2612 offset |= (*entry & 0x7f) << shift;
2613 shift += 7;
2614 }
2615 while (*entry++ & 0x80);
2616
2617 vsp += 0x204 + (offset << 2);
2618 }
2619 else if (insn == 0xb3)
2620 {
2621 int start = *entry >> 4;
2622 int count = (*entry++) & 0xf;
2623 int i;
2624
2625 /* Only registers D0..D15 are valid here. */
2626 if (start + count >= 16)
2627 return NULL;
2628
2629 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2630 for (i = 0; i <= count; i++)
2631 {
2632 cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2633 vsp += 8;
2634 }
2635
2636 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2637 vsp += 4;
2638 }
2639 else if ((insn & 0xf8) == 0xb8)
2640 {
2641 int count = insn & 0x7;
2642 int i;
2643
2644 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2645 for (i = 0; i <= count; i++)
2646 {
2647 cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2648 vsp += 8;
2649 }
2650
2651 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2652 vsp += 4;
2653 }
2654 else if (insn == 0xc6)
2655 {
2656 int start = *entry >> 4;
2657 int count = (*entry++) & 0xf;
2658 int i;
2659
2660 /* Only registers WR0..WR15 are valid. */
2661 if (start + count >= 16)
2662 return NULL;
2663
2664 /* Pop iwmmx registers WR[start]..WR[start+count]. */
2665 for (i = 0; i <= count; i++)
2666 {
2667 cache->saved_regs[ARM_WR0_REGNUM + start + i].addr = vsp;
2668 vsp += 8;
2669 }
2670 }
2671 else if (insn == 0xc7)
2672 {
2673 int mask = *entry++;
2674 int i;
2675
2676 /* All-zero mask and mask >= 16 is "spare". */
2677 if (mask == 0 || mask >= 16)
2678 return NULL;
2679
2680 /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask. */
2681 for (i = 0; i < 4; i++)
2682 if (mask & (1 << i))
2683 {
2684 cache->saved_regs[ARM_WCGR0_REGNUM + i].addr = vsp;
2685 vsp += 4;
2686 }
2687 }
2688 else if ((insn & 0xf8) == 0xc0)
2689 {
2690 int count = insn & 0x7;
2691 int i;
2692
2693 /* Pop iwmmx registers WR[10]..WR[10+count]. */
2694 for (i = 0; i <= count; i++)
2695 {
2696 cache->saved_regs[ARM_WR0_REGNUM + 10 + i].addr = vsp;
2697 vsp += 8;
2698 }
2699 }
2700 else if (insn == 0xc8)
2701 {
2702 int start = *entry >> 4;
2703 int count = (*entry++) & 0xf;
2704 int i;
2705
2706 /* Only registers D0..D31 are valid. */
2707 if (start + count >= 16)
2708 return NULL;
2709
2710 /* Pop VFP double-precision registers
2711 D[16+start]..D[16+start+count]. */
2712 for (i = 0; i <= count; i++)
2713 {
2714 cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].addr = vsp;
2715 vsp += 8;
2716 }
2717 }
2718 else if (insn == 0xc9)
2719 {
2720 int start = *entry >> 4;
2721 int count = (*entry++) & 0xf;
2722 int i;
2723
2724 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2725 for (i = 0; i <= count; i++)
2726 {
2727 cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2728 vsp += 8;
2729 }
2730 }
2731 else if ((insn & 0xf8) == 0xd0)
2732 {
2733 int count = insn & 0x7;
2734 int i;
2735
2736 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2737 for (i = 0; i <= count; i++)
2738 {
2739 cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2740 vsp += 8;
2741 }
2742 }
2743 else
2744 {
2745 /* Everything else is "spare". */
2746 return NULL;
2747 }
2748 }
2749
2750 /* If we restore SP from a register, assume this was the frame register.
2751 Otherwise just fall back to SP as frame register. */
2752 if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2753 cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2754 else
2755 cache->framereg = ARM_SP_REGNUM;
2756
2757 /* Determine offset to previous frame. */
2758 cache->framesize
2759 = vsp - get_frame_register_unsigned (this_frame, cache->framereg);
2760
2761 /* We already got the previous SP. */
2762 cache->prev_sp = vsp;
2763
2764 return cache;
2765}
2766
2767/* Unwinding via ARM exception table entries. Note that the sniffer
2768 already computes a filled-in prologue cache, which is then used
2769 with the same arm_prologue_this_id and arm_prologue_prev_register
2770 routines also used for prologue-parsing based unwinding. */
2771
2772static int
2773arm_exidx_unwind_sniffer (const struct frame_unwind *self,
2774 struct frame_info *this_frame,
2775 void **this_prologue_cache)
2776{
2777 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2778 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
2779 CORE_ADDR addr_in_block, exidx_region, func_start;
2780 struct arm_prologue_cache *cache;
2781 gdb_byte *entry;
2782
2783 /* See if we have an ARM exception table entry covering this address. */
2784 addr_in_block = get_frame_address_in_block (this_frame);
2785 entry = arm_find_exidx_entry (addr_in_block, &exidx_region);
2786 if (!entry)
2787 return 0;
2788
2789 /* The ARM exception table does not describe unwind information
2790 for arbitrary PC values, but is guaranteed to be correct only
2791 at call sites. We have to decide here whether we want to use
2792 ARM exception table information for this frame, or fall back
2793 to using prologue parsing. (Note that if we have DWARF CFI,
2794 this sniffer isn't even called -- CFI is always preferred.)
2795
2796 Before we make this decision, however, we check whether we
2797 actually have *symbol* information for the current frame.
2798 If not, prologue parsing would not work anyway, so we might
2799 as well use the exception table and hope for the best. */
2800 if (find_pc_partial_function (addr_in_block, NULL, &func_start, NULL))
2801 {
2802 int exc_valid = 0;
2803
2804 /* If the next frame is "normal", we are at a call site in this
2805 frame, so exception information is guaranteed to be valid. */
2806 if (get_next_frame (this_frame)
2807 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
2808 exc_valid = 1;
2809
2810 /* We also assume exception information is valid if we're currently
2811 blocked in a system call. The system library is supposed to
2812 ensure this, so that e.g. pthread cancellation works. */
2813 if (arm_frame_is_thumb (this_frame))
2814 {
2815 LONGEST insn;
2816
2817 if (safe_read_memory_integer (get_frame_pc (this_frame) - 2, 2,
2818 byte_order_for_code, &insn)
2819 && (insn & 0xff00) == 0xdf00 /* svc */)
2820 exc_valid = 1;
2821 }
2822 else
2823 {
2824 LONGEST insn;
2825
2826 if (safe_read_memory_integer (get_frame_pc (this_frame) - 4, 4,
2827 byte_order_for_code, &insn)
2828 && (insn & 0x0f000000) == 0x0f000000 /* svc */)
2829 exc_valid = 1;
2830 }
2831
2832 /* Bail out if we don't know that exception information is valid. */
2833 if (!exc_valid)
2834 return 0;
2835
2836 /* The ARM exception index does not mark the *end* of the region
2837 covered by the entry, and some functions will not have any entry.
2838 To correctly recognize the end of the covered region, the linker
2839 should have inserted dummy records with a CANTUNWIND marker.
2840
2841 Unfortunately, current versions of GNU ld do not reliably do
2842 this, and thus we may have found an incorrect entry above.
2843 As a (temporary) sanity check, we only use the entry if it
2844 lies *within* the bounds of the function. Note that this check
2845 might reject perfectly valid entries that just happen to cover
2846 multiple functions; therefore this check ought to be removed
2847 once the linker is fixed. */
2848 if (func_start > exidx_region)
2849 return 0;
2850 }
2851
2852 /* Decode the list of unwinding instructions into a prologue cache.
2853 Note that this may fail due to e.g. a "refuse to unwind" code. */
2854 cache = arm_exidx_fill_cache (this_frame, entry);
2855 if (!cache)
2856 return 0;
2857
2858 *this_prologue_cache = cache;
2859 return 1;
2860}
2861
2862struct frame_unwind arm_exidx_unwind = {
2863 NORMAL_FRAME,
8fbca658 2864 default_frame_unwind_stop_reason,
0e9e9abd
UW
2865 arm_prologue_this_id,
2866 arm_prologue_prev_register,
2867 NULL,
2868 arm_exidx_unwind_sniffer
2869};
2870
909cf6ea 2871static struct arm_prologue_cache *
a262aec2 2872arm_make_stub_cache (struct frame_info *this_frame)
909cf6ea 2873{
909cf6ea 2874 struct arm_prologue_cache *cache;
909cf6ea 2875
35d5d4ee 2876 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
a262aec2 2877 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
909cf6ea 2878
a262aec2 2879 cache->prev_sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
909cf6ea
DJ
2880
2881 return cache;
2882}
2883
2884/* Our frame ID for a stub frame is the current SP and LR. */
2885
2886static void
a262aec2 2887arm_stub_this_id (struct frame_info *this_frame,
909cf6ea
DJ
2888 void **this_cache,
2889 struct frame_id *this_id)
2890{
2891 struct arm_prologue_cache *cache;
2892
2893 if (*this_cache == NULL)
a262aec2 2894 *this_cache = arm_make_stub_cache (this_frame);
909cf6ea
DJ
2895 cache = *this_cache;
2896
a262aec2 2897 *this_id = frame_id_build (cache->prev_sp, get_frame_pc (this_frame));
909cf6ea
DJ
2898}
2899
a262aec2
DJ
2900static int
2901arm_stub_unwind_sniffer (const struct frame_unwind *self,
2902 struct frame_info *this_frame,
2903 void **this_prologue_cache)
909cf6ea 2904{
93d42b30 2905 CORE_ADDR addr_in_block;
909cf6ea
DJ
2906 char dummy[4];
2907
a262aec2 2908 addr_in_block = get_frame_address_in_block (this_frame);
93d42b30 2909 if (in_plt_section (addr_in_block, NULL)
fc36e839
DE
2910 /* We also use the stub winder if the target memory is unreadable
2911 to avoid having the prologue unwinder trying to read it. */
a262aec2
DJ
2912 || target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
2913 return 1;
909cf6ea 2914
a262aec2 2915 return 0;
909cf6ea
DJ
2916}
2917
a262aec2
DJ
2918struct frame_unwind arm_stub_unwind = {
2919 NORMAL_FRAME,
8fbca658 2920 default_frame_unwind_stop_reason,
a262aec2
DJ
2921 arm_stub_this_id,
2922 arm_prologue_prev_register,
2923 NULL,
2924 arm_stub_unwind_sniffer
2925};
2926
24de872b 2927static CORE_ADDR
a262aec2 2928arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
24de872b
DJ
2929{
2930 struct arm_prologue_cache *cache;
2931
eb5492fa 2932 if (*this_cache == NULL)
a262aec2 2933 *this_cache = arm_make_prologue_cache (this_frame);
eb5492fa
DJ
2934 cache = *this_cache;
2935
4be43953 2936 return cache->prev_sp - cache->framesize;
24de872b
DJ
2937}
2938
eb5492fa
DJ
2939struct frame_base arm_normal_base = {
2940 &arm_prologue_unwind,
2941 arm_normal_frame_base,
2942 arm_normal_frame_base,
2943 arm_normal_frame_base
2944};
2945
a262aec2 2946/* Assuming THIS_FRAME is a dummy, return the frame ID of that
eb5492fa
DJ
2947 dummy frame. The frame ID's base needs to match the TOS value
2948 saved by save_dummy_frame_tos() and returned from
2949 arm_push_dummy_call, and the PC needs to match the dummy frame's
2950 breakpoint. */
c906108c 2951
eb5492fa 2952static struct frame_id
a262aec2 2953arm_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
c906108c 2954{
0963b4bd
MS
2955 return frame_id_build (get_frame_register_unsigned (this_frame,
2956 ARM_SP_REGNUM),
a262aec2 2957 get_frame_pc (this_frame));
eb5492fa 2958}
c3b4394c 2959
eb5492fa
DJ
2960/* Given THIS_FRAME, find the previous frame's resume PC (which will
2961 be used to construct the previous frame's ID, after looking up the
2962 containing function). */
c3b4394c 2963
eb5492fa
DJ
2964static CORE_ADDR
2965arm_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
2966{
2967 CORE_ADDR pc;
2968 pc = frame_unwind_register_unsigned (this_frame, ARM_PC_REGNUM);
24568a2c 2969 return arm_addr_bits_remove (gdbarch, pc);
eb5492fa
DJ
2970}
2971
2972static CORE_ADDR
2973arm_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
2974{
2975 return frame_unwind_register_unsigned (this_frame, ARM_SP_REGNUM);
c906108c
SS
2976}
2977
b39cc962
DJ
2978static struct value *
2979arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
2980 int regnum)
2981{
24568a2c 2982 struct gdbarch * gdbarch = get_frame_arch (this_frame);
b39cc962 2983 CORE_ADDR lr, cpsr;
9779414d 2984 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
b39cc962
DJ
2985
2986 switch (regnum)
2987 {
2988 case ARM_PC_REGNUM:
2989 /* The PC is normally copied from the return column, which
2990 describes saves of LR. However, that version may have an
2991 extra bit set to indicate Thumb state. The bit is not
2992 part of the PC. */
2993 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2994 return frame_unwind_got_constant (this_frame, regnum,
24568a2c 2995 arm_addr_bits_remove (gdbarch, lr));
b39cc962
DJ
2996
2997 case ARM_PS_REGNUM:
2998 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
ca38c58e 2999 cpsr = get_frame_register_unsigned (this_frame, regnum);
b39cc962
DJ
3000 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3001 if (IS_THUMB_ADDR (lr))
9779414d 3002 cpsr |= t_bit;
b39cc962 3003 else
9779414d 3004 cpsr &= ~t_bit;
ca38c58e 3005 return frame_unwind_got_constant (this_frame, regnum, cpsr);
b39cc962
DJ
3006
3007 default:
3008 internal_error (__FILE__, __LINE__,
3009 _("Unexpected register %d"), regnum);
3010 }
3011}
3012
3013static void
3014arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
3015 struct dwarf2_frame_state_reg *reg,
3016 struct frame_info *this_frame)
3017{
3018 switch (regnum)
3019 {
3020 case ARM_PC_REGNUM:
3021 case ARM_PS_REGNUM:
3022 reg->how = DWARF2_FRAME_REG_FN;
3023 reg->loc.fn = arm_dwarf2_prev_register;
3024 break;
3025 case ARM_SP_REGNUM:
3026 reg->how = DWARF2_FRAME_REG_CFA;
3027 break;
3028 }
3029}
3030
4024ca99
UW
3031/* Return true if we are in the function's epilogue, i.e. after the
3032 instruction that destroyed the function's stack frame. */
3033
3034static int
3035thumb_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3036{
3037 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3038 unsigned int insn, insn2;
3039 int found_return = 0, found_stack_adjust = 0;
3040 CORE_ADDR func_start, func_end;
3041 CORE_ADDR scan_pc;
3042 gdb_byte buf[4];
3043
3044 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3045 return 0;
3046
3047 /* The epilogue is a sequence of instructions along the following lines:
3048
3049 - add stack frame size to SP or FP
3050 - [if frame pointer used] restore SP from FP
3051 - restore registers from SP [may include PC]
3052 - a return-type instruction [if PC wasn't already restored]
3053
3054 In a first pass, we scan forward from the current PC and verify the
3055 instructions we find as compatible with this sequence, ending in a
3056 return instruction.
3057
3058 However, this is not sufficient to distinguish indirect function calls
3059 within a function from indirect tail calls in the epilogue in some cases.
3060 Therefore, if we didn't already find any SP-changing instruction during
3061 forward scan, we add a backward scanning heuristic to ensure we actually
3062 are in the epilogue. */
3063
3064 scan_pc = pc;
3065 while (scan_pc < func_end && !found_return)
3066 {
3067 if (target_read_memory (scan_pc, buf, 2))
3068 break;
3069
3070 scan_pc += 2;
3071 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3072
3073 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
3074 found_return = 1;
3075 else if (insn == 0x46f7) /* mov pc, lr */
3076 found_return = 1;
3077 else if (insn == 0x46bd) /* mov sp, r7 */
3078 found_stack_adjust = 1;
3079 else if ((insn & 0xff00) == 0xb000) /* add sp, imm or sub sp, imm */
3080 found_stack_adjust = 1;
3081 else if ((insn & 0xfe00) == 0xbc00) /* pop <registers> */
3082 {
3083 found_stack_adjust = 1;
3084 if (insn & 0x0100) /* <registers> include PC. */
3085 found_return = 1;
3086 }
db24da6d 3087 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instruction */
4024ca99
UW
3088 {
3089 if (target_read_memory (scan_pc, buf, 2))
3090 break;
3091
3092 scan_pc += 2;
3093 insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3094
3095 if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3096 {
3097 found_stack_adjust = 1;
3098 if (insn2 & 0x8000) /* <registers> include PC. */
3099 found_return = 1;
3100 }
3101 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3102 && (insn2 & 0x0fff) == 0x0b04)
3103 {
3104 found_stack_adjust = 1;
3105 if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC. */
3106 found_return = 1;
3107 }
3108 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3109 && (insn2 & 0x0e00) == 0x0a00)
3110 found_stack_adjust = 1;
3111 else
3112 break;
3113 }
3114 else
3115 break;
3116 }
3117
3118 if (!found_return)
3119 return 0;
3120
3121 /* Since any instruction in the epilogue sequence, with the possible
3122 exception of return itself, updates the stack pointer, we need to
3123 scan backwards for at most one instruction. Try either a 16-bit or
3124 a 32-bit instruction. This is just a heuristic, so we do not worry
0963b4bd 3125 too much about false positives. */
4024ca99
UW
3126
3127 if (!found_stack_adjust)
3128 {
3129 if (pc - 4 < func_start)
3130 return 0;
3131 if (target_read_memory (pc - 4, buf, 4))
3132 return 0;
3133
3134 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3135 insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);
3136
3137 if (insn2 == 0x46bd) /* mov sp, r7 */
3138 found_stack_adjust = 1;
3139 else if ((insn2 & 0xff00) == 0xb000) /* add sp, imm or sub sp, imm */
3140 found_stack_adjust = 1;
3141 else if ((insn2 & 0xff00) == 0xbc00) /* pop <registers> without PC */
3142 found_stack_adjust = 1;
3143 else if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3144 found_stack_adjust = 1;
3145 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3146 && (insn2 & 0x0fff) == 0x0b04)
3147 found_stack_adjust = 1;
3148 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3149 && (insn2 & 0x0e00) == 0x0a00)
3150 found_stack_adjust = 1;
3151 }
3152
3153 return found_stack_adjust;
3154}
3155
3156/* Return true if we are in the function's epilogue, i.e. after the
3157 instruction that destroyed the function's stack frame. */
3158
3159static int
3160arm_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3161{
3162 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3163 unsigned int insn;
3164 int found_return, found_stack_adjust;
3165 CORE_ADDR func_start, func_end;
3166
3167 if (arm_pc_is_thumb (gdbarch, pc))
3168 return thumb_in_function_epilogue_p (gdbarch, pc);
3169
3170 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3171 return 0;
3172
3173 /* We are in the epilogue if the previous instruction was a stack
3174 adjustment and the next instruction is a possible return (bx, mov
3175 pc, or pop). We could have to scan backwards to find the stack
3176 adjustment, or forwards to find the return, but this is a decent
3177 approximation. First scan forwards. */
3178
3179 found_return = 0;
3180 insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
3181 if (bits (insn, 28, 31) != INST_NV)
3182 {
3183 if ((insn & 0x0ffffff0) == 0x012fff10)
3184 /* BX. */
3185 found_return = 1;
3186 else if ((insn & 0x0ffffff0) == 0x01a0f000)
3187 /* MOV PC. */
3188 found_return = 1;
3189 else if ((insn & 0x0fff0000) == 0x08bd0000
3190 && (insn & 0x0000c000) != 0)
3191 /* POP (LDMIA), including PC or LR. */
3192 found_return = 1;
3193 }
3194
3195 if (!found_return)
3196 return 0;
3197
3198 /* Scan backwards. This is just a heuristic, so do not worry about
3199 false positives from mode changes. */
3200
3201 if (pc < func_start + 4)
3202 return 0;
3203
73c964d6 3204 found_stack_adjust = 0;
4024ca99
UW
3205 insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
3206 if (bits (insn, 28, 31) != INST_NV)
3207 {
3208 if ((insn & 0x0df0f000) == 0x0080d000)
3209 /* ADD SP (register or immediate). */
3210 found_stack_adjust = 1;
3211 else if ((insn & 0x0df0f000) == 0x0040d000)
3212 /* SUB SP (register or immediate). */
3213 found_stack_adjust = 1;
3214 else if ((insn & 0x0ffffff0) == 0x01a0d000)
3215 /* MOV SP. */
77bc0675 3216 found_stack_adjust = 1;
4024ca99
UW
3217 else if ((insn & 0x0fff0000) == 0x08bd0000)
3218 /* POP (LDMIA). */
3219 found_stack_adjust = 1;
fc51cce1
MGD
3220 else if ((insn & 0x0fff0000) == 0x049d0000)
3221 /* POP of a single register. */
3222 found_stack_adjust = 1;
4024ca99
UW
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:
2af48f68
PB
3288 case TYPE_CODE_REF:
3289 case TYPE_CODE_CHAR:
3290 case TYPE_CODE_BOOL:
3291 return TYPE_LENGTH (t);
3292
3293 case TYPE_CODE_ARRAY:
3294 case TYPE_CODE_COMPLEX:
3295 /* TODO: What about vector types? */
3296 return arm_type_align (TYPE_TARGET_TYPE (t));
3297
3298 case TYPE_CODE_STRUCT:
3299 case TYPE_CODE_UNION:
3300 align = 1;
3301 for (n = 0; n < TYPE_NFIELDS (t); n++)
3302 {
3303 falign = arm_type_align (TYPE_FIELD_TYPE (t, n));
3304 if (falign > align)
3305 align = falign;
3306 }
3307 return align;
3308 }
3309}
3310
90445bd3
DJ
3311/* Possible base types for a candidate for passing and returning in
3312 VFP registers. */
3313
3314enum arm_vfp_cprc_base_type
3315{
3316 VFP_CPRC_UNKNOWN,
3317 VFP_CPRC_SINGLE,
3318 VFP_CPRC_DOUBLE,
3319 VFP_CPRC_VEC64,
3320 VFP_CPRC_VEC128
3321};
3322
3323/* The length of one element of base type B. */
3324
3325static unsigned
3326arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
3327{
3328 switch (b)
3329 {
3330 case VFP_CPRC_SINGLE:
3331 return 4;
3332 case VFP_CPRC_DOUBLE:
3333 return 8;
3334 case VFP_CPRC_VEC64:
3335 return 8;
3336 case VFP_CPRC_VEC128:
3337 return 16;
3338 default:
3339 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3340 (int) b);
3341 }
3342}
3343
3344/* The character ('s', 'd' or 'q') for the type of VFP register used
3345 for passing base type B. */
3346
3347static int
3348arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
3349{
3350 switch (b)
3351 {
3352 case VFP_CPRC_SINGLE:
3353 return 's';
3354 case VFP_CPRC_DOUBLE:
3355 return 'd';
3356 case VFP_CPRC_VEC64:
3357 return 'd';
3358 case VFP_CPRC_VEC128:
3359 return 'q';
3360 default:
3361 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3362 (int) b);
3363 }
3364}
3365
3366/* Determine whether T may be part of a candidate for passing and
3367 returning in VFP registers, ignoring the limit on the total number
3368 of components. If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
3369 classification of the first valid component found; if it is not
3370 VFP_CPRC_UNKNOWN, all components must have the same classification
3371 as *BASE_TYPE. If it is found that T contains a type not permitted
3372 for passing and returning in VFP registers, a type differently
3373 classified from *BASE_TYPE, or two types differently classified
3374 from each other, return -1, otherwise return the total number of
3375 base-type elements found (possibly 0 in an empty structure or
3376 array). Vectors and complex types are not currently supported,
3377 matching the generic AAPCS support. */
3378
3379static int
3380arm_vfp_cprc_sub_candidate (struct type *t,
3381 enum arm_vfp_cprc_base_type *base_type)
3382{
3383 t = check_typedef (t);
3384 switch (TYPE_CODE (t))
3385 {
3386 case TYPE_CODE_FLT:
3387 switch (TYPE_LENGTH (t))
3388 {
3389 case 4:
3390 if (*base_type == VFP_CPRC_UNKNOWN)
3391 *base_type = VFP_CPRC_SINGLE;
3392 else if (*base_type != VFP_CPRC_SINGLE)
3393 return -1;
3394 return 1;
3395
3396 case 8:
3397 if (*base_type == VFP_CPRC_UNKNOWN)
3398 *base_type = VFP_CPRC_DOUBLE;
3399 else if (*base_type != VFP_CPRC_DOUBLE)
3400 return -1;
3401 return 1;
3402
3403 default:
3404 return -1;
3405 }
3406 break;
3407
3408 case TYPE_CODE_ARRAY:
3409 {
3410 int count;
3411 unsigned unitlen;
3412 count = arm_vfp_cprc_sub_candidate (TYPE_TARGET_TYPE (t), base_type);
3413 if (count == -1)
3414 return -1;
3415 if (TYPE_LENGTH (t) == 0)
3416 {
3417 gdb_assert (count == 0);
3418 return 0;
3419 }
3420 else if (count == 0)
3421 return -1;
3422 unitlen = arm_vfp_cprc_unit_length (*base_type);
3423 gdb_assert ((TYPE_LENGTH (t) % unitlen) == 0);
3424 return TYPE_LENGTH (t) / unitlen;
3425 }
3426 break;
3427
3428 case TYPE_CODE_STRUCT:
3429 {
3430 int count = 0;
3431 unsigned unitlen;
3432 int i;
3433 for (i = 0; i < TYPE_NFIELDS (t); i++)
3434 {
3435 int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3436 base_type);
3437 if (sub_count == -1)
3438 return -1;
3439 count += sub_count;
3440 }
3441 if (TYPE_LENGTH (t) == 0)
3442 {
3443 gdb_assert (count == 0);
3444 return 0;
3445 }
3446 else if (count == 0)
3447 return -1;
3448 unitlen = arm_vfp_cprc_unit_length (*base_type);
3449 if (TYPE_LENGTH (t) != unitlen * count)
3450 return -1;
3451 return count;
3452 }
3453
3454 case TYPE_CODE_UNION:
3455 {
3456 int count = 0;
3457 unsigned unitlen;
3458 int i;
3459 for (i = 0; i < TYPE_NFIELDS (t); i++)
3460 {
3461 int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3462 base_type);
3463 if (sub_count == -1)
3464 return -1;
3465 count = (count > sub_count ? count : sub_count);
3466 }
3467 if (TYPE_LENGTH (t) == 0)
3468 {
3469 gdb_assert (count == 0);
3470 return 0;
3471 }
3472 else if (count == 0)
3473 return -1;
3474 unitlen = arm_vfp_cprc_unit_length (*base_type);
3475 if (TYPE_LENGTH (t) != unitlen * count)
3476 return -1;
3477 return count;
3478 }
3479
3480 default:
3481 break;
3482 }
3483
3484 return -1;
3485}
3486
3487/* Determine whether T is a VFP co-processor register candidate (CPRC)
3488 if passed to or returned from a non-variadic function with the VFP
3489 ABI in effect. Return 1 if it is, 0 otherwise. If it is, set
3490 *BASE_TYPE to the base type for T and *COUNT to the number of
3491 elements of that base type before returning. */
3492
3493static int
3494arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
3495 int *count)
3496{
3497 enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
3498 int c = arm_vfp_cprc_sub_candidate (t, &b);
3499 if (c <= 0 || c > 4)
3500 return 0;
3501 *base_type = b;
3502 *count = c;
3503 return 1;
3504}
3505
3506/* Return 1 if the VFP ABI should be used for passing arguments to and
3507 returning values from a function of type FUNC_TYPE, 0
3508 otherwise. */
3509
3510static int
3511arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
3512{
3513 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3514 /* Variadic functions always use the base ABI. Assume that functions
3515 without debug info are not variadic. */
3516 if (func_type && TYPE_VARARGS (check_typedef (func_type)))
3517 return 0;
3518 /* The VFP ABI is only supported as a variant of AAPCS. */
3519 if (tdep->arm_abi != ARM_ABI_AAPCS)
3520 return 0;
3521 return gdbarch_tdep (gdbarch)->fp_model == ARM_FLOAT_VFP;
3522}
3523
3524/* We currently only support passing parameters in integer registers, which
3525 conforms with GCC's default model, and VFP argument passing following
3526 the VFP variant of AAPCS. Several other variants exist and
2dd604e7
RE
3527 we should probably support some of them based on the selected ABI. */
3528
3529static CORE_ADDR
7d9b040b 3530arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6a65450a
AC
3531 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
3532 struct value **args, CORE_ADDR sp, int struct_return,
3533 CORE_ADDR struct_addr)
2dd604e7 3534{
e17a4113 3535 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2dd604e7
RE
3536 int argnum;
3537 int argreg;
3538 int nstack;
3539 struct stack_item *si = NULL;
90445bd3
DJ
3540 int use_vfp_abi;
3541 struct type *ftype;
3542 unsigned vfp_regs_free = (1 << 16) - 1;
3543
3544 /* Determine the type of this function and whether the VFP ABI
3545 applies. */
3546 ftype = check_typedef (value_type (function));
3547 if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
3548 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
3549 use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
2dd604e7 3550
6a65450a
AC
3551 /* Set the return address. For the ARM, the return breakpoint is
3552 always at BP_ADDR. */
9779414d 3553 if (arm_pc_is_thumb (gdbarch, bp_addr))
9dca5578 3554 bp_addr |= 1;
6a65450a 3555 regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
2dd604e7
RE
3556
3557 /* Walk through the list of args and determine how large a temporary
3558 stack is required. Need to take care here as structs may be
7a9dd1b2 3559 passed on the stack, and we have to push them. */
2dd604e7
RE
3560 nstack = 0;
3561
3562 argreg = ARM_A1_REGNUM;
3563 nstack = 0;
3564
2dd604e7
RE
3565 /* The struct_return pointer occupies the first parameter
3566 passing register. */
3567 if (struct_return)
3568 {
3569 if (arm_debug)
5af949e3 3570 fprintf_unfiltered (gdb_stdlog, "struct return in %s = %s\n",
2af46ca0 3571 gdbarch_register_name (gdbarch, argreg),
5af949e3 3572 paddress (gdbarch, struct_addr));
2dd604e7
RE
3573 regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
3574 argreg++;
3575 }
3576
3577 for (argnum = 0; argnum < nargs; argnum++)
3578 {
3579 int len;
3580 struct type *arg_type;
3581 struct type *target_type;
3582 enum type_code typecode;
8c6363cf 3583 const bfd_byte *val;
2af48f68 3584 int align;
90445bd3
DJ
3585 enum arm_vfp_cprc_base_type vfp_base_type;
3586 int vfp_base_count;
3587 int may_use_core_reg = 1;
2dd604e7 3588
df407dfe 3589 arg_type = check_typedef (value_type (args[argnum]));
2dd604e7
RE
3590 len = TYPE_LENGTH (arg_type);
3591 target_type = TYPE_TARGET_TYPE (arg_type);
3592 typecode = TYPE_CODE (arg_type);
8c6363cf 3593 val = value_contents (args[argnum]);
2dd604e7 3594
2af48f68
PB
3595 align = arm_type_align (arg_type);
3596 /* Round alignment up to a whole number of words. */
3597 align = (align + INT_REGISTER_SIZE - 1) & ~(INT_REGISTER_SIZE - 1);
3598 /* Different ABIs have different maximum alignments. */
3599 if (gdbarch_tdep (gdbarch)->arm_abi == ARM_ABI_APCS)
3600 {
3601 /* The APCS ABI only requires word alignment. */
3602 align = INT_REGISTER_SIZE;
3603 }
3604 else
3605 {
3606 /* The AAPCS requires at most doubleword alignment. */
3607 if (align > INT_REGISTER_SIZE * 2)
3608 align = INT_REGISTER_SIZE * 2;
3609 }
3610
90445bd3
DJ
3611 if (use_vfp_abi
3612 && arm_vfp_call_candidate (arg_type, &vfp_base_type,
3613 &vfp_base_count))
3614 {
3615 int regno;
3616 int unit_length;
3617 int shift;
3618 unsigned mask;
3619
3620 /* Because this is a CPRC it cannot go in a core register or
3621 cause a core register to be skipped for alignment.
3622 Either it goes in VFP registers and the rest of this loop
3623 iteration is skipped for this argument, or it goes on the
3624 stack (and the stack alignment code is correct for this
3625 case). */
3626 may_use_core_reg = 0;
3627
3628 unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
3629 shift = unit_length / 4;
3630 mask = (1 << (shift * vfp_base_count)) - 1;
3631 for (regno = 0; regno < 16; regno += shift)
3632 if (((vfp_regs_free >> regno) & mask) == mask)
3633 break;
3634
3635 if (regno < 16)
3636 {
3637 int reg_char;
3638 int reg_scaled;
3639 int i;
3640
3641 vfp_regs_free &= ~(mask << regno);
3642 reg_scaled = regno / shift;
3643 reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
3644 for (i = 0; i < vfp_base_count; i++)
3645 {
3646 char name_buf[4];
3647 int regnum;
58d6951d
DJ
3648 if (reg_char == 'q')
3649 arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
90445bd3 3650 val + i * unit_length);
58d6951d
DJ
3651 else
3652 {
3653 sprintf (name_buf, "%c%d", reg_char, reg_scaled + i);
3654 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
3655 strlen (name_buf));
3656 regcache_cooked_write (regcache, regnum,
3657 val + i * unit_length);
3658 }
90445bd3
DJ
3659 }
3660 continue;
3661 }
3662 else
3663 {
3664 /* This CPRC could not go in VFP registers, so all VFP
3665 registers are now marked as used. */
3666 vfp_regs_free = 0;
3667 }
3668 }
3669
2af48f68
PB
3670 /* Push stack padding for dowubleword alignment. */
3671 if (nstack & (align - 1))
3672 {
3673 si = push_stack_item (si, val, INT_REGISTER_SIZE);
3674 nstack += INT_REGISTER_SIZE;
3675 }
3676
3677 /* Doubleword aligned quantities must go in even register pairs. */
90445bd3
DJ
3678 if (may_use_core_reg
3679 && argreg <= ARM_LAST_ARG_REGNUM
2af48f68
PB
3680 && align > INT_REGISTER_SIZE
3681 && argreg & 1)
3682 argreg++;
3683
2dd604e7
RE
3684 /* If the argument is a pointer to a function, and it is a
3685 Thumb function, create a LOCAL copy of the value and set
3686 the THUMB bit in it. */
3687 if (TYPE_CODE_PTR == typecode
3688 && target_type != NULL
f96b8fa0 3689 && TYPE_CODE_FUNC == TYPE_CODE (check_typedef (target_type)))
2dd604e7 3690 {
e17a4113 3691 CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
9779414d 3692 if (arm_pc_is_thumb (gdbarch, regval))
2dd604e7 3693 {
8c6363cf
TT
3694 bfd_byte *copy = alloca (len);
3695 store_unsigned_integer (copy, len, byte_order,
e17a4113 3696 MAKE_THUMB_ADDR (regval));
8c6363cf 3697 val = copy;
2dd604e7
RE
3698 }
3699 }
3700
3701 /* Copy the argument to general registers or the stack in
3702 register-sized pieces. Large arguments are split between
3703 registers and stack. */
3704 while (len > 0)
3705 {
f0c9063c 3706 int partial_len = len < INT_REGISTER_SIZE ? len : INT_REGISTER_SIZE;
2dd604e7 3707
90445bd3 3708 if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
2dd604e7
RE
3709 {
3710 /* The argument is being passed in a general purpose
3711 register. */
e17a4113
UW
3712 CORE_ADDR regval
3713 = extract_unsigned_integer (val, partial_len, byte_order);
3714 if (byte_order == BFD_ENDIAN_BIG)
8bf8793c 3715 regval <<= (INT_REGISTER_SIZE - partial_len) * 8;
2dd604e7
RE
3716 if (arm_debug)
3717 fprintf_unfiltered (gdb_stdlog, "arg %d in %s = 0x%s\n",
c9f4d572
UW
3718 argnum,
3719 gdbarch_register_name
2af46ca0 3720 (gdbarch, argreg),
f0c9063c 3721 phex (regval, INT_REGISTER_SIZE));
2dd604e7
RE
3722 regcache_cooked_write_unsigned (regcache, argreg, regval);
3723 argreg++;
3724 }
3725 else
3726 {
3727 /* Push the arguments onto the stack. */
3728 if (arm_debug)
3729 fprintf_unfiltered (gdb_stdlog, "arg %d @ sp + %d\n",
3730 argnum, nstack);
f0c9063c
UW
3731 si = push_stack_item (si, val, INT_REGISTER_SIZE);
3732 nstack += INT_REGISTER_SIZE;
2dd604e7
RE
3733 }
3734
3735 len -= partial_len;
3736 val += partial_len;
3737 }
3738 }
3739 /* If we have an odd number of words to push, then decrement the stack
3740 by one word now, so first stack argument will be dword aligned. */
3741 if (nstack & 4)
3742 sp -= 4;
3743
3744 while (si)
3745 {
3746 sp -= si->len;
3747 write_memory (sp, si->data, si->len);
3748 si = pop_stack_item (si);
3749 }
3750
3751 /* Finally, update teh SP register. */
3752 regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
3753
3754 return sp;
3755}
3756
f53f0d0b
PB
3757
3758/* Always align the frame to an 8-byte boundary. This is required on
3759 some platforms and harmless on the rest. */
3760
3761static CORE_ADDR
3762arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
3763{
3764 /* Align the stack to eight bytes. */
3765 return sp & ~ (CORE_ADDR) 7;
3766}
3767
c906108c 3768static void
ed9a39eb 3769print_fpu_flags (int flags)
c906108c 3770{
c5aa993b
JM
3771 if (flags & (1 << 0))
3772 fputs ("IVO ", stdout);
3773 if (flags & (1 << 1))
3774 fputs ("DVZ ", stdout);
3775 if (flags & (1 << 2))
3776 fputs ("OFL ", stdout);
3777 if (flags & (1 << 3))
3778 fputs ("UFL ", stdout);
3779 if (flags & (1 << 4))
3780 fputs ("INX ", stdout);
3781 putchar ('\n');
c906108c
SS
3782}
3783
5e74b15c
RE
3784/* Print interesting information about the floating point processor
3785 (if present) or emulator. */
34e8f22d 3786static void
d855c300 3787arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
23e3a7ac 3788 struct frame_info *frame, const char *args)
c906108c 3789{
9c9acae0 3790 unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
c5aa993b
JM
3791 int type;
3792
3793 type = (status >> 24) & 127;
edefbb7c
AC
3794 if (status & (1 << 31))
3795 printf (_("Hardware FPU type %d\n"), type);
3796 else
3797 printf (_("Software FPU type %d\n"), type);
3798 /* i18n: [floating point unit] mask */
3799 fputs (_("mask: "), stdout);
c5aa993b 3800 print_fpu_flags (status >> 16);
edefbb7c
AC
3801 /* i18n: [floating point unit] flags */
3802 fputs (_("flags: "), stdout);
c5aa993b 3803 print_fpu_flags (status);
c906108c
SS
3804}
3805
27067745
UW
3806/* Construct the ARM extended floating point type. */
3807static struct type *
3808arm_ext_type (struct gdbarch *gdbarch)
3809{
3810 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3811
3812 if (!tdep->arm_ext_type)
3813 tdep->arm_ext_type
e9bb382b 3814 = arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
27067745
UW
3815 floatformats_arm_ext);
3816
3817 return tdep->arm_ext_type;
3818}
3819
58d6951d
DJ
3820static struct type *
3821arm_neon_double_type (struct gdbarch *gdbarch)
3822{
3823 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3824
3825 if (tdep->neon_double_type == NULL)
3826 {
3827 struct type *t, *elem;
3828
3829 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
3830 TYPE_CODE_UNION);
3831 elem = builtin_type (gdbarch)->builtin_uint8;
3832 append_composite_type_field (t, "u8", init_vector_type (elem, 8));
3833 elem = builtin_type (gdbarch)->builtin_uint16;
3834 append_composite_type_field (t, "u16", init_vector_type (elem, 4));
3835 elem = builtin_type (gdbarch)->builtin_uint32;
3836 append_composite_type_field (t, "u32", init_vector_type (elem, 2));
3837 elem = builtin_type (gdbarch)->builtin_uint64;
3838 append_composite_type_field (t, "u64", elem);
3839 elem = builtin_type (gdbarch)->builtin_float;
3840 append_composite_type_field (t, "f32", init_vector_type (elem, 2));
3841 elem = builtin_type (gdbarch)->builtin_double;
3842 append_composite_type_field (t, "f64", elem);
3843
3844 TYPE_VECTOR (t) = 1;
3845 TYPE_NAME (t) = "neon_d";
3846 tdep->neon_double_type = t;
3847 }
3848
3849 return tdep->neon_double_type;
3850}
3851
3852/* FIXME: The vector types are not correctly ordered on big-endian
3853 targets. Just as s0 is the low bits of d0, d0[0] is also the low
3854 bits of d0 - regardless of what unit size is being held in d0. So
3855 the offset of the first uint8 in d0 is 7, but the offset of the
3856 first float is 4. This code works as-is for little-endian
3857 targets. */
3858
3859static struct type *
3860arm_neon_quad_type (struct gdbarch *gdbarch)
3861{
3862 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3863
3864 if (tdep->neon_quad_type == NULL)
3865 {
3866 struct type *t, *elem;
3867
3868 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
3869 TYPE_CODE_UNION);
3870 elem = builtin_type (gdbarch)->builtin_uint8;
3871 append_composite_type_field (t, "u8", init_vector_type (elem, 16));
3872 elem = builtin_type (gdbarch)->builtin_uint16;
3873 append_composite_type_field (t, "u16", init_vector_type (elem, 8));
3874 elem = builtin_type (gdbarch)->builtin_uint32;
3875 append_composite_type_field (t, "u32", init_vector_type (elem, 4));
3876 elem = builtin_type (gdbarch)->builtin_uint64;
3877 append_composite_type_field (t, "u64", init_vector_type (elem, 2));
3878 elem = builtin_type (gdbarch)->builtin_float;
3879 append_composite_type_field (t, "f32", init_vector_type (elem, 4));
3880 elem = builtin_type (gdbarch)->builtin_double;
3881 append_composite_type_field (t, "f64", init_vector_type (elem, 2));
3882
3883 TYPE_VECTOR (t) = 1;
3884 TYPE_NAME (t) = "neon_q";
3885 tdep->neon_quad_type = t;
3886 }
3887
3888 return tdep->neon_quad_type;
3889}
3890
34e8f22d
RE
3891/* Return the GDB type object for the "standard" data type of data in
3892 register N. */
3893
3894static struct type *
7a5ea0d4 3895arm_register_type (struct gdbarch *gdbarch, int regnum)
032758dc 3896{
58d6951d
DJ
3897 int num_regs = gdbarch_num_regs (gdbarch);
3898
3899 if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
3900 && regnum >= num_regs && regnum < num_regs + 32)
3901 return builtin_type (gdbarch)->builtin_float;
3902
3903 if (gdbarch_tdep (gdbarch)->have_neon_pseudos
3904 && regnum >= num_regs + 32 && regnum < num_regs + 32 + 16)
3905 return arm_neon_quad_type (gdbarch);
3906
3907 /* If the target description has register information, we are only
3908 in this function so that we can override the types of
3909 double-precision registers for NEON. */
3910 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
3911 {
3912 struct type *t = tdesc_register_type (gdbarch, regnum);
3913
3914 if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
3915 && TYPE_CODE (t) == TYPE_CODE_FLT
3916 && gdbarch_tdep (gdbarch)->have_neon)
3917 return arm_neon_double_type (gdbarch);
3918 else
3919 return t;
3920 }
3921
34e8f22d 3922 if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
58d6951d
DJ
3923 {
3924 if (!gdbarch_tdep (gdbarch)->have_fpa_registers)
3925 return builtin_type (gdbarch)->builtin_void;
3926
3927 return arm_ext_type (gdbarch);
3928 }
e4c16157 3929 else if (regnum == ARM_SP_REGNUM)
0dfff4cb 3930 return builtin_type (gdbarch)->builtin_data_ptr;
e4c16157 3931 else if (regnum == ARM_PC_REGNUM)
0dfff4cb 3932 return builtin_type (gdbarch)->builtin_func_ptr;
ff6f572f
DJ
3933 else if (regnum >= ARRAY_SIZE (arm_register_names))
3934 /* These registers are only supported on targets which supply
3935 an XML description. */
df4df182 3936 return builtin_type (gdbarch)->builtin_int0;
032758dc 3937 else
df4df182 3938 return builtin_type (gdbarch)->builtin_uint32;
032758dc
AC
3939}
3940
ff6f572f
DJ
3941/* Map a DWARF register REGNUM onto the appropriate GDB register
3942 number. */
3943
3944static int
d3f73121 3945arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
ff6f572f
DJ
3946{
3947 /* Core integer regs. */
3948 if (reg >= 0 && reg <= 15)
3949 return reg;
3950
3951 /* Legacy FPA encoding. These were once used in a way which
3952 overlapped with VFP register numbering, so their use is
3953 discouraged, but GDB doesn't support the ARM toolchain
3954 which used them for VFP. */
3955 if (reg >= 16 && reg <= 23)
3956 return ARM_F0_REGNUM + reg - 16;
3957
3958 /* New assignments for the FPA registers. */
3959 if (reg >= 96 && reg <= 103)
3960 return ARM_F0_REGNUM + reg - 96;
3961
3962 /* WMMX register assignments. */
3963 if (reg >= 104 && reg <= 111)
3964 return ARM_WCGR0_REGNUM + reg - 104;
3965
3966 if (reg >= 112 && reg <= 127)
3967 return ARM_WR0_REGNUM + reg - 112;
3968
3969 if (reg >= 192 && reg <= 199)
3970 return ARM_WC0_REGNUM + reg - 192;
3971
58d6951d
DJ
3972 /* VFP v2 registers. A double precision value is actually
3973 in d1 rather than s2, but the ABI only defines numbering
3974 for the single precision registers. This will "just work"
3975 in GDB for little endian targets (we'll read eight bytes,
3976 starting in s0 and then progressing to s1), but will be
3977 reversed on big endian targets with VFP. This won't
3978 be a problem for the new Neon quad registers; you're supposed
3979 to use DW_OP_piece for those. */
3980 if (reg >= 64 && reg <= 95)
3981 {
3982 char name_buf[4];
3983
3984 sprintf (name_buf, "s%d", reg - 64);
3985 return user_reg_map_name_to_regnum (gdbarch, name_buf,
3986 strlen (name_buf));
3987 }
3988
3989 /* VFP v3 / Neon registers. This range is also used for VFP v2
3990 registers, except that it now describes d0 instead of s0. */
3991 if (reg >= 256 && reg <= 287)
3992 {
3993 char name_buf[4];
3994
3995 sprintf (name_buf, "d%d", reg - 256);
3996 return user_reg_map_name_to_regnum (gdbarch, name_buf,
3997 strlen (name_buf));
3998 }
3999
ff6f572f
DJ
4000 return -1;
4001}
4002
26216b98
AC
4003/* Map GDB internal REGNUM onto the Arm simulator register numbers. */
4004static int
e7faf938 4005arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
26216b98
AC
4006{
4007 int reg = regnum;
e7faf938 4008 gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
26216b98 4009
ff6f572f
DJ
4010 if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
4011 return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
4012
4013 if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
4014 return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
4015
4016 if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
4017 return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
4018
26216b98
AC
4019 if (reg < NUM_GREGS)
4020 return SIM_ARM_R0_REGNUM + reg;
4021 reg -= NUM_GREGS;
4022
4023 if (reg < NUM_FREGS)
4024 return SIM_ARM_FP0_REGNUM + reg;
4025 reg -= NUM_FREGS;
4026
4027 if (reg < NUM_SREGS)
4028 return SIM_ARM_FPS_REGNUM + reg;
4029 reg -= NUM_SREGS;
4030
edefbb7c 4031 internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
26216b98 4032}
34e8f22d 4033
a37b3cc0
AC
4034/* NOTE: cagney/2001-08-20: Both convert_from_extended() and
4035 convert_to_extended() use floatformat_arm_ext_littlebyte_bigword.
4036 It is thought that this is is the floating-point register format on
4037 little-endian systems. */
c906108c 4038
ed9a39eb 4039static void
b508a996 4040convert_from_extended (const struct floatformat *fmt, const void *ptr,
be8626e0 4041 void *dbl, int endianess)
c906108c 4042{
a37b3cc0 4043 DOUBLEST d;
be8626e0
MD
4044
4045 if (endianess == BFD_ENDIAN_BIG)
a37b3cc0
AC
4046 floatformat_to_doublest (&floatformat_arm_ext_big, ptr, &d);
4047 else
4048 floatformat_to_doublest (&floatformat_arm_ext_littlebyte_bigword,
4049 ptr, &d);
b508a996 4050 floatformat_from_doublest (fmt, &d, dbl);
c906108c
SS
4051}
4052
34e8f22d 4053static void
be8626e0
MD
4054convert_to_extended (const struct floatformat *fmt, void *dbl, const void *ptr,
4055 int endianess)
c906108c 4056{
a37b3cc0 4057 DOUBLEST d;
be8626e0 4058
b508a996 4059 floatformat_to_doublest (fmt, ptr, &d);
be8626e0 4060 if (endianess == BFD_ENDIAN_BIG)
a37b3cc0
AC
4061 floatformat_from_doublest (&floatformat_arm_ext_big, &d, dbl);
4062 else
4063 floatformat_from_doublest (&floatformat_arm_ext_littlebyte_bigword,
4064 &d, dbl);
c906108c 4065}
ed9a39eb 4066
c906108c 4067static int
ed9a39eb 4068condition_true (unsigned long cond, unsigned long status_reg)
c906108c
SS
4069{
4070 if (cond == INST_AL || cond == INST_NV)
4071 return 1;
4072
4073 switch (cond)
4074 {
4075 case INST_EQ:
4076 return ((status_reg & FLAG_Z) != 0);
4077 case INST_NE:
4078 return ((status_reg & FLAG_Z) == 0);
4079 case INST_CS:
4080 return ((status_reg & FLAG_C) != 0);
4081 case INST_CC:
4082 return ((status_reg & FLAG_C) == 0);
4083 case INST_MI:
4084 return ((status_reg & FLAG_N) != 0);
4085 case INST_PL:
4086 return ((status_reg & FLAG_N) == 0);
4087 case INST_VS:
4088 return ((status_reg & FLAG_V) != 0);
4089 case INST_VC:
4090 return ((status_reg & FLAG_V) == 0);
4091 case INST_HI:
4092 return ((status_reg & (FLAG_C | FLAG_Z)) == FLAG_C);
4093 case INST_LS:
4094 return ((status_reg & (FLAG_C | FLAG_Z)) != FLAG_C);
4095 case INST_GE:
4096 return (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0));
4097 case INST_LT:
4098 return (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0));
4099 case INST_GT:
f8bf5763
PM
4100 return (((status_reg & FLAG_Z) == 0)
4101 && (((status_reg & FLAG_N) == 0)
4102 == ((status_reg & FLAG_V) == 0)));
c906108c 4103 case INST_LE:
f8bf5763
PM
4104 return (((status_reg & FLAG_Z) != 0)
4105 || (((status_reg & FLAG_N) == 0)
4106 != ((status_reg & FLAG_V) == 0)));
c906108c
SS
4107 }
4108 return 1;
4109}
4110
c906108c 4111static unsigned long
0b1b3e42
UW
4112shifted_reg_val (struct frame_info *frame, unsigned long inst, int carry,
4113 unsigned long pc_val, unsigned long status_reg)
c906108c
SS
4114{
4115 unsigned long res, shift;
4116 int rm = bits (inst, 0, 3);
4117 unsigned long shifttype = bits (inst, 5, 6);
c5aa993b
JM
4118
4119 if (bit (inst, 4))
c906108c
SS
4120 {
4121 int rs = bits (inst, 8, 11);
0b1b3e42
UW
4122 shift = (rs == 15 ? pc_val + 8
4123 : get_frame_register_unsigned (frame, rs)) & 0xFF;
c906108c
SS
4124 }
4125 else
4126 shift = bits (inst, 7, 11);
c5aa993b 4127
bf9f652a 4128 res = (rm == ARM_PC_REGNUM
0d39a070 4129 ? (pc_val + (bit (inst, 4) ? 12 : 8))
0b1b3e42 4130 : get_frame_register_unsigned (frame, rm));
c906108c
SS
4131
4132 switch (shifttype)
4133 {
c5aa993b 4134 case 0: /* LSL */
c906108c
SS
4135 res = shift >= 32 ? 0 : res << shift;
4136 break;
c5aa993b
JM
4137
4138 case 1: /* LSR */
c906108c
SS
4139 res = shift >= 32 ? 0 : res >> shift;
4140 break;
4141
c5aa993b
JM
4142 case 2: /* ASR */
4143 if (shift >= 32)
4144 shift = 31;
c906108c
SS
4145 res = ((res & 0x80000000L)
4146 ? ~((~res) >> shift) : res >> shift);
4147 break;
4148
c5aa993b 4149 case 3: /* ROR/RRX */
c906108c
SS
4150 shift &= 31;
4151 if (shift == 0)
4152 res = (res >> 1) | (carry ? 0x80000000L : 0);
4153 else
c5aa993b 4154 res = (res >> shift) | (res << (32 - shift));
c906108c
SS
4155 break;
4156 }
4157
4158 return res & 0xffffffff;
4159}
4160
c906108c
SS
4161/* Return number of 1-bits in VAL. */
4162
4163static int
ed9a39eb 4164bitcount (unsigned long val)
c906108c
SS
4165{
4166 int nbits;
4167 for (nbits = 0; val != 0; nbits++)
0963b4bd 4168 val &= val - 1; /* Delete rightmost 1-bit in val. */
c906108c
SS
4169 return nbits;
4170}
4171
177321bd
DJ
4172/* Return the size in bytes of the complete Thumb instruction whose
4173 first halfword is INST1. */
4174
4175static int
4176thumb_insn_size (unsigned short inst1)
4177{
4178 if ((inst1 & 0xe000) == 0xe000 && (inst1 & 0x1800) != 0)
4179 return 4;
4180 else
4181 return 2;
4182}
4183
4184static int
4185thumb_advance_itstate (unsigned int itstate)
4186{
4187 /* Preserve IT[7:5], the first three bits of the condition. Shift
4188 the upcoming condition flags left by one bit. */
4189 itstate = (itstate & 0xe0) | ((itstate << 1) & 0x1f);
4190
4191 /* If we have finished the IT block, clear the state. */
4192 if ((itstate & 0x0f) == 0)
4193 itstate = 0;
4194
4195 return itstate;
4196}
4197
4198/* Find the next PC after the current instruction executes. In some
4199 cases we can not statically determine the answer (see the IT state
4200 handling in this function); in that case, a breakpoint may be
4201 inserted in addition to the returned PC, which will be used to set
4202 another breakpoint by our caller. */
4203
ad527d2e 4204static CORE_ADDR
18819fa6 4205thumb_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc)
c906108c 4206{
2af46ca0 4207 struct gdbarch *gdbarch = get_frame_arch (frame);
177321bd 4208 struct address_space *aspace = get_frame_address_space (frame);
e17a4113
UW
4209 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4210 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
c5aa993b 4211 unsigned long pc_val = ((unsigned long) pc) + 4; /* PC after prefetch */
e17a4113 4212 unsigned short inst1;
0963b4bd 4213 CORE_ADDR nextpc = pc + 2; /* Default is next instruction. */
c906108c 4214 unsigned long offset;
177321bd 4215 ULONGEST status, itstate;
c906108c 4216
50e98be4
DJ
4217 nextpc = MAKE_THUMB_ADDR (nextpc);
4218 pc_val = MAKE_THUMB_ADDR (pc_val);
4219
e17a4113 4220 inst1 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
9d4fde75 4221
9dca5578
DJ
4222 /* Thumb-2 conditional execution support. There are eight bits in
4223 the CPSR which describe conditional execution state. Once
4224 reconstructed (they're in a funny order), the low five bits
4225 describe the low bit of the condition for each instruction and
4226 how many instructions remain. The high three bits describe the
4227 base condition. One of the low four bits will be set if an IT
4228 block is active. These bits read as zero on earlier
4229 processors. */
4230 status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
177321bd 4231 itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
9dca5578 4232
177321bd
DJ
4233 /* If-Then handling. On GNU/Linux, where this routine is used, we
4234 use an undefined instruction as a breakpoint. Unlike BKPT, IT
4235 can disable execution of the undefined instruction. So we might
4236 miss the breakpoint if we set it on a skipped conditional
4237 instruction. Because conditional instructions can change the
4238 flags, affecting the execution of further instructions, we may
4239 need to set two breakpoints. */
9dca5578 4240
177321bd
DJ
4241 if (gdbarch_tdep (gdbarch)->thumb2_breakpoint != NULL)
4242 {
4243 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
4244 {
4245 /* An IT instruction. Because this instruction does not
4246 modify the flags, we can accurately predict the next
4247 executed instruction. */
4248 itstate = inst1 & 0x00ff;
4249 pc += thumb_insn_size (inst1);
4250
4251 while (itstate != 0 && ! condition_true (itstate >> 4, status))
4252 {
0963b4bd
MS
4253 inst1 = read_memory_unsigned_integer (pc, 2,
4254 byte_order_for_code);
177321bd
DJ
4255 pc += thumb_insn_size (inst1);
4256 itstate = thumb_advance_itstate (itstate);
4257 }
4258
50e98be4 4259 return MAKE_THUMB_ADDR (pc);
177321bd
DJ
4260 }
4261 else if (itstate != 0)
4262 {
4263 /* We are in a conditional block. Check the condition. */
4264 if (! condition_true (itstate >> 4, status))
4265 {
4266 /* Advance to the next executed instruction. */
4267 pc += thumb_insn_size (inst1);
4268 itstate = thumb_advance_itstate (itstate);
4269
4270 while (itstate != 0 && ! condition_true (itstate >> 4, status))
4271 {
0963b4bd
MS
4272 inst1 = read_memory_unsigned_integer (pc, 2,
4273 byte_order_for_code);
177321bd
DJ
4274 pc += thumb_insn_size (inst1);
4275 itstate = thumb_advance_itstate (itstate);
4276 }
4277
50e98be4 4278 return MAKE_THUMB_ADDR (pc);
177321bd
DJ
4279 }
4280 else if ((itstate & 0x0f) == 0x08)
4281 {
4282 /* This is the last instruction of the conditional
4283 block, and it is executed. We can handle it normally
4284 because the following instruction is not conditional,
4285 and we must handle it normally because it is
4286 permitted to branch. Fall through. */
4287 }
4288 else
4289 {
4290 int cond_negated;
4291
4292 /* There are conditional instructions after this one.
4293 If this instruction modifies the flags, then we can
4294 not predict what the next executed instruction will
4295 be. Fortunately, this instruction is architecturally
4296 forbidden to branch; we know it will fall through.
4297 Start by skipping past it. */
4298 pc += thumb_insn_size (inst1);
4299 itstate = thumb_advance_itstate (itstate);
4300
4301 /* Set a breakpoint on the following instruction. */
4302 gdb_assert ((itstate & 0x0f) != 0);
18819fa6
UW
4303 arm_insert_single_step_breakpoint (gdbarch, aspace,
4304 MAKE_THUMB_ADDR (pc));
177321bd
DJ
4305 cond_negated = (itstate >> 4) & 1;
4306
4307 /* Skip all following instructions with the same
4308 condition. If there is a later instruction in the IT
4309 block with the opposite condition, set the other
4310 breakpoint there. If not, then set a breakpoint on
4311 the instruction after the IT block. */
4312 do
4313 {
0963b4bd
MS
4314 inst1 = read_memory_unsigned_integer (pc, 2,
4315 byte_order_for_code);
177321bd
DJ
4316 pc += thumb_insn_size (inst1);
4317 itstate = thumb_advance_itstate (itstate);
4318 }
4319 while (itstate != 0 && ((itstate >> 4) & 1) == cond_negated);
4320
50e98be4 4321 return MAKE_THUMB_ADDR (pc);
177321bd
DJ
4322 }
4323 }
4324 }
4325 else if (itstate & 0x0f)
9dca5578
DJ
4326 {
4327 /* We are in a conditional block. Check the condition. */
177321bd 4328 int cond = itstate >> 4;
9dca5578
DJ
4329
4330 if (! condition_true (cond, status))
db24da6d
YQ
4331 /* Advance to the next instruction. All the 32-bit
4332 instructions share a common prefix. */
4333 return MAKE_THUMB_ADDR (pc + thumb_insn_size (inst1));
177321bd
DJ
4334
4335 /* Otherwise, handle the instruction normally. */
9dca5578
DJ
4336 }
4337
c906108c
SS
4338 if ((inst1 & 0xff00) == 0xbd00) /* pop {rlist, pc} */
4339 {
4340 CORE_ADDR sp;
4341
4342 /* Fetch the saved PC from the stack. It's stored above
4343 all of the other registers. */
f0c9063c 4344 offset = bitcount (bits (inst1, 0, 7)) * INT_REGISTER_SIZE;
0b1b3e42 4345 sp = get_frame_register_unsigned (frame, ARM_SP_REGNUM);
e17a4113 4346 nextpc = read_memory_unsigned_integer (sp + offset, 4, byte_order);
c906108c
SS
4347 }
4348 else if ((inst1 & 0xf000) == 0xd000) /* conditional branch */
4349 {
c5aa993b 4350 unsigned long cond = bits (inst1, 8, 11);
25b41d01
YQ
4351 if (cond == 0x0f) /* 0x0f = SWI */
4352 {
4353 struct gdbarch_tdep *tdep;
4354 tdep = gdbarch_tdep (gdbarch);
4355
4356 if (tdep->syscall_next_pc != NULL)
4357 nextpc = tdep->syscall_next_pc (frame);
4358
4359 }
4360 else if (cond != 0x0f && condition_true (cond, status))
c906108c
SS
4361 nextpc = pc_val + (sbits (inst1, 0, 7) << 1);
4362 }
4363 else if ((inst1 & 0xf800) == 0xe000) /* unconditional branch */
4364 {
4365 nextpc = pc_val + (sbits (inst1, 0, 10) << 1);
4366 }
db24da6d 4367 else if (thumb_insn_size (inst1) == 4) /* 32-bit instruction */
c906108c 4368 {
e17a4113
UW
4369 unsigned short inst2;
4370 inst2 = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);
9dca5578
DJ
4371
4372 /* Default to the next instruction. */
4373 nextpc = pc + 4;
50e98be4 4374 nextpc = MAKE_THUMB_ADDR (nextpc);
9dca5578
DJ
4375
4376 if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
4377 {
4378 /* Branches and miscellaneous control instructions. */
4379
4380 if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
4381 {
4382 /* B, BL, BLX. */
4383 int j1, j2, imm1, imm2;
4384
4385 imm1 = sbits (inst1, 0, 10);
4386 imm2 = bits (inst2, 0, 10);
4387 j1 = bit (inst2, 13);
4388 j2 = bit (inst2, 11);
4389
4390 offset = ((imm1 << 12) + (imm2 << 1));
4391 offset ^= ((!j2) << 22) | ((!j1) << 23);
4392
4393 nextpc = pc_val + offset;
4394 /* For BLX make sure to clear the low bits. */
4395 if (bit (inst2, 12) == 0)
4396 nextpc = nextpc & 0xfffffffc;
4397 }
4398 else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
4399 {
4400 /* SUBS PC, LR, #imm8. */
4401 nextpc = get_frame_register_unsigned (frame, ARM_LR_REGNUM);
4402 nextpc -= inst2 & 0x00ff;
4403 }
4069ebbe 4404 else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
9dca5578
DJ
4405 {
4406 /* Conditional branch. */
4407 if (condition_true (bits (inst1, 6, 9), status))
4408 {
4409 int sign, j1, j2, imm1, imm2;
4410
4411 sign = sbits (inst1, 10, 10);
4412 imm1 = bits (inst1, 0, 5);
4413 imm2 = bits (inst2, 0, 10);
4414 j1 = bit (inst2, 13);
4415 j2 = bit (inst2, 11);
4416
4417 offset = (sign << 20) + (j2 << 19) + (j1 << 18);
4418 offset += (imm1 << 12) + (imm2 << 1);
4419
4420 nextpc = pc_val + offset;
4421 }
4422 }
4423 }
4424 else if ((inst1 & 0xfe50) == 0xe810)
4425 {
4426 /* Load multiple or RFE. */
4427 int rn, offset, load_pc = 1;
4428
4429 rn = bits (inst1, 0, 3);
4430 if (bit (inst1, 7) && !bit (inst1, 8))
4431 {
4432 /* LDMIA or POP */
4433 if (!bit (inst2, 15))
4434 load_pc = 0;
4435 offset = bitcount (inst2) * 4 - 4;
4436 }
4437 else if (!bit (inst1, 7) && bit (inst1, 8))
4438 {
4439 /* LDMDB */
4440 if (!bit (inst2, 15))
4441 load_pc = 0;
4442 offset = -4;
4443 }
4444 else if (bit (inst1, 7) && bit (inst1, 8))
4445 {
4446 /* RFEIA */
4447 offset = 0;
4448 }
4449 else if (!bit (inst1, 7) && !bit (inst1, 8))
4450 {
4451 /* RFEDB */
4452 offset = -8;
4453 }
4454 else
4455 load_pc = 0;
4456
4457 if (load_pc)
4458 {
4459 CORE_ADDR addr = get_frame_register_unsigned (frame, rn);
4460 nextpc = get_frame_memory_unsigned (frame, addr + offset, 4);
4461 }
4462 }
4463 else if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
4464 {
4465 /* MOV PC or MOVS PC. */
4466 nextpc = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
50e98be4 4467 nextpc = MAKE_THUMB_ADDR (nextpc);
9dca5578
DJ
4468 }
4469 else if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
4470 {
4471 /* LDR PC. */
4472 CORE_ADDR base;
4473 int rn, load_pc = 1;
4474
4475 rn = bits (inst1, 0, 3);
4476 base = get_frame_register_unsigned (frame, rn);
bf9f652a 4477 if (rn == ARM_PC_REGNUM)
9dca5578
DJ
4478 {
4479 base = (base + 4) & ~(CORE_ADDR) 0x3;
4480 if (bit (inst1, 7))
4481 base += bits (inst2, 0, 11);
4482 else
4483 base -= bits (inst2, 0, 11);
4484 }
4485 else if (bit (inst1, 7))
4486 base += bits (inst2, 0, 11);
4487 else if (bit (inst2, 11))
4488 {
4489 if (bit (inst2, 10))
4490 {
4491 if (bit (inst2, 9))
4492 base += bits (inst2, 0, 7);
4493 else
4494 base -= bits (inst2, 0, 7);
4495 }
4496 }
4497 else if ((inst2 & 0x0fc0) == 0x0000)
4498 {
4499 int shift = bits (inst2, 4, 5), rm = bits (inst2, 0, 3);
4500 base += get_frame_register_unsigned (frame, rm) << shift;
4501 }
4502 else
4503 /* Reserved. */
4504 load_pc = 0;
4505
4506 if (load_pc)
4507 nextpc = get_frame_memory_unsigned (frame, base, 4);
4508 }
4509 else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
4510 {
4511 /* TBB. */
d476da0e
RE
4512 CORE_ADDR tbl_reg, table, offset, length;
4513
4514 tbl_reg = bits (inst1, 0, 3);
4515 if (tbl_reg == 0x0f)
4516 table = pc + 4; /* Regcache copy of PC isn't right yet. */
4517 else
4518 table = get_frame_register_unsigned (frame, tbl_reg);
9dca5578 4519
9dca5578
DJ
4520 offset = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
4521 length = 2 * get_frame_memory_unsigned (frame, table + offset, 1);
4522 nextpc = pc_val + length;
4523 }
d476da0e 4524 else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
9dca5578
DJ
4525 {
4526 /* TBH. */
d476da0e
RE
4527 CORE_ADDR tbl_reg, table, offset, length;
4528
4529 tbl_reg = bits (inst1, 0, 3);
4530 if (tbl_reg == 0x0f)
4531 table = pc + 4; /* Regcache copy of PC isn't right yet. */
4532 else
4533 table = get_frame_register_unsigned (frame, tbl_reg);
9dca5578 4534
9dca5578
DJ
4535 offset = 2 * get_frame_register_unsigned (frame, bits (inst2, 0, 3));
4536 length = 2 * get_frame_memory_unsigned (frame, table + offset, 2);
4537 nextpc = pc_val + length;
4538 }
c906108c 4539 }
aa17d93e 4540 else if ((inst1 & 0xff00) == 0x4700) /* bx REG, blx REG */
9498281f
DJ
4541 {
4542 if (bits (inst1, 3, 6) == 0x0f)
6ca1b147 4543 nextpc = UNMAKE_THUMB_ADDR (pc_val);
9498281f 4544 else
0b1b3e42 4545 nextpc = get_frame_register_unsigned (frame, bits (inst1, 3, 6));
9498281f 4546 }
ad8b5167
UW
4547 else if ((inst1 & 0xff87) == 0x4687) /* mov pc, REG */
4548 {
4549 if (bits (inst1, 3, 6) == 0x0f)
4550 nextpc = pc_val;
4551 else
4552 nextpc = get_frame_register_unsigned (frame, bits (inst1, 3, 6));
4553
4554 nextpc = MAKE_THUMB_ADDR (nextpc);
4555 }
9dca5578
DJ
4556 else if ((inst1 & 0xf500) == 0xb100)
4557 {
4558 /* CBNZ or CBZ. */
4559 int imm = (bit (inst1, 9) << 6) + (bits (inst1, 3, 7) << 1);
4560 ULONGEST reg = get_frame_register_unsigned (frame, bits (inst1, 0, 2));
4561
4562 if (bit (inst1, 11) && reg != 0)
4563 nextpc = pc_val + imm;
4564 else if (!bit (inst1, 11) && reg == 0)
4565 nextpc = pc_val + imm;
4566 }
c906108c
SS
4567 return nextpc;
4568}
4569
50e98be4 4570/* Get the raw next address. PC is the current program counter, in
18819fa6 4571 FRAME, which is assumed to be executing in ARM mode.
50e98be4
DJ
4572
4573 The value returned has the execution state of the next instruction
4574 encoded in it. Use IS_THUMB_ADDR () to see whether the instruction is
4575 in Thumb-State, and gdbarch_addr_bits_remove () to get the plain memory
0963b4bd
MS
4576 address. */
4577
50e98be4 4578static CORE_ADDR
18819fa6 4579arm_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc)
c906108c 4580{
2af46ca0 4581 struct gdbarch *gdbarch = get_frame_arch (frame);
e17a4113
UW
4582 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4583 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
c906108c
SS
4584 unsigned long pc_val;
4585 unsigned long this_instr;
4586 unsigned long status;
4587 CORE_ADDR nextpc;
4588
c906108c 4589 pc_val = (unsigned long) pc;
e17a4113 4590 this_instr = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
9d4fde75 4591
0b1b3e42 4592 status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
c5aa993b 4593 nextpc = (CORE_ADDR) (pc_val + 4); /* Default case */
c906108c 4594
daddc3c1
DJ
4595 if (bits (this_instr, 28, 31) == INST_NV)
4596 switch (bits (this_instr, 24, 27))
4597 {
4598 case 0xa:
4599 case 0xb:
4600 {
4601 /* Branch with Link and change to Thumb. */
4602 nextpc = BranchDest (pc, this_instr);
4603 nextpc |= bit (this_instr, 24) << 1;
50e98be4 4604 nextpc = MAKE_THUMB_ADDR (nextpc);
daddc3c1
DJ
4605 break;
4606 }
4607 case 0xc:
4608 case 0xd:
4609 case 0xe:
4610 /* Coprocessor register transfer. */
4611 if (bits (this_instr, 12, 15) == 15)
4612 error (_("Invalid update to pc in instruction"));
4613 break;
4614 }
4615 else if (condition_true (bits (this_instr, 28, 31), status))
c906108c
SS
4616 {
4617 switch (bits (this_instr, 24, 27))
4618 {
c5aa993b 4619 case 0x0:
94c30b78 4620 case 0x1: /* data processing */
c5aa993b
JM
4621 case 0x2:
4622 case 0x3:
c906108c
SS
4623 {
4624 unsigned long operand1, operand2, result = 0;
4625 unsigned long rn;
4626 int c;
c5aa993b 4627
c906108c
SS
4628 if (bits (this_instr, 12, 15) != 15)
4629 break;
4630
4631 if (bits (this_instr, 22, 25) == 0
c5aa993b 4632 && bits (this_instr, 4, 7) == 9) /* multiply */
edefbb7c 4633 error (_("Invalid update to pc in instruction"));
c906108c 4634
9498281f 4635 /* BX <reg>, BLX <reg> */
e150acc7
PB
4636 if (bits (this_instr, 4, 27) == 0x12fff1
4637 || bits (this_instr, 4, 27) == 0x12fff3)
9498281f
DJ
4638 {
4639 rn = bits (this_instr, 0, 3);
bf9f652a
YQ
4640 nextpc = ((rn == ARM_PC_REGNUM)
4641 ? (pc_val + 8)
4642 : get_frame_register_unsigned (frame, rn));
4643
9498281f
DJ
4644 return nextpc;
4645 }
4646
0963b4bd 4647 /* Multiply into PC. */
c906108c
SS
4648 c = (status & FLAG_C) ? 1 : 0;
4649 rn = bits (this_instr, 16, 19);
bf9f652a
YQ
4650 operand1 = ((rn == ARM_PC_REGNUM)
4651 ? (pc_val + 8)
4652 : get_frame_register_unsigned (frame, rn));
c5aa993b 4653
c906108c
SS
4654 if (bit (this_instr, 25))
4655 {
4656 unsigned long immval = bits (this_instr, 0, 7);
4657 unsigned long rotate = 2 * bits (this_instr, 8, 11);
c5aa993b
JM
4658 operand2 = ((immval >> rotate) | (immval << (32 - rotate)))
4659 & 0xffffffff;
c906108c 4660 }
0963b4bd
MS
4661 else /* operand 2 is a shifted register. */
4662 operand2 = shifted_reg_val (frame, this_instr, c,
4663 pc_val, status);
c5aa993b 4664
c906108c
SS
4665 switch (bits (this_instr, 21, 24))
4666 {
c5aa993b 4667 case 0x0: /*and */
c906108c
SS
4668 result = operand1 & operand2;
4669 break;
4670
c5aa993b 4671 case 0x1: /*eor */
c906108c
SS
4672 result = operand1 ^ operand2;
4673 break;
4674
c5aa993b 4675 case 0x2: /*sub */
c906108c
SS
4676 result = operand1 - operand2;
4677 break;
4678
c5aa993b 4679 case 0x3: /*rsb */
c906108c
SS
4680 result = operand2 - operand1;
4681 break;
4682
c5aa993b 4683 case 0x4: /*add */
c906108c
SS
4684 result = operand1 + operand2;
4685 break;
4686
c5aa993b 4687 case 0x5: /*adc */
c906108c
SS
4688 result = operand1 + operand2 + c;
4689 break;
4690
c5aa993b 4691 case 0x6: /*sbc */
c906108c
SS
4692 result = operand1 - operand2 + c;
4693 break;
4694
c5aa993b 4695 case 0x7: /*rsc */
c906108c
SS
4696 result = operand2 - operand1 + c;
4697 break;
4698
c5aa993b
JM
4699 case 0x8:
4700 case 0x9:
4701 case 0xa:
4702 case 0xb: /* tst, teq, cmp, cmn */
c906108c
SS
4703 result = (unsigned long) nextpc;
4704 break;
4705
c5aa993b 4706 case 0xc: /*orr */
c906108c
SS
4707 result = operand1 | operand2;
4708 break;
4709
c5aa993b 4710 case 0xd: /*mov */
c906108c
SS
4711 /* Always step into a function. */
4712 result = operand2;
c5aa993b 4713 break;
c906108c 4714
c5aa993b 4715 case 0xe: /*bic */
c906108c
SS
4716 result = operand1 & ~operand2;
4717 break;
4718
c5aa993b 4719 case 0xf: /*mvn */
c906108c
SS
4720 result = ~operand2;
4721 break;
4722 }
c906108c 4723
50e98be4
DJ
4724 /* In 26-bit APCS the bottom two bits of the result are
4725 ignored, and we always end up in ARM state. */
4726 if (!arm_apcs_32)
4727 nextpc = arm_addr_bits_remove (gdbarch, result);
4728 else
4729 nextpc = result;
4730
c906108c
SS
4731 break;
4732 }
c5aa993b
JM
4733
4734 case 0x4:
4735 case 0x5: /* data transfer */
4736 case 0x6:
4737 case 0x7:
c906108c
SS
4738 if (bit (this_instr, 20))
4739 {
4740 /* load */
4741 if (bits (this_instr, 12, 15) == 15)
4742 {
4743 /* rd == pc */
c5aa993b 4744 unsigned long rn;
c906108c 4745 unsigned long base;
c5aa993b 4746
c906108c 4747 if (bit (this_instr, 22))
edefbb7c 4748 error (_("Invalid update to pc in instruction"));
c906108c
SS
4749
4750 /* byte write to PC */
4751 rn = bits (this_instr, 16, 19);
bf9f652a
YQ
4752 base = ((rn == ARM_PC_REGNUM)
4753 ? (pc_val + 8)
4754 : get_frame_register_unsigned (frame, rn));
4755
c906108c
SS
4756 if (bit (this_instr, 24))
4757 {
4758 /* pre-indexed */
4759 int c = (status & FLAG_C) ? 1 : 0;
4760 unsigned long offset =
c5aa993b 4761 (bit (this_instr, 25)
0b1b3e42 4762 ? shifted_reg_val (frame, this_instr, c, pc_val, status)
c5aa993b 4763 : bits (this_instr, 0, 11));
c906108c
SS
4764
4765 if (bit (this_instr, 23))
4766 base += offset;
4767 else
4768 base -= offset;
4769 }
51370a33
YQ
4770 nextpc =
4771 (CORE_ADDR) read_memory_unsigned_integer ((CORE_ADDR) base,
4772 4, byte_order);
c906108c
SS
4773 }
4774 }
4775 break;
c5aa993b
JM
4776
4777 case 0x8:
4778 case 0x9: /* block transfer */
c906108c
SS
4779 if (bit (this_instr, 20))
4780 {
4781 /* LDM */
4782 if (bit (this_instr, 15))
4783 {
4784 /* loading pc */
4785 int offset = 0;
51370a33
YQ
4786 unsigned long rn_val
4787 = get_frame_register_unsigned (frame,
4788 bits (this_instr, 16, 19));
c906108c
SS
4789
4790 if (bit (this_instr, 23))
4791 {
4792 /* up */
4793 unsigned long reglist = bits (this_instr, 0, 14);
4794 offset = bitcount (reglist) * 4;
c5aa993b 4795 if (bit (this_instr, 24)) /* pre */
c906108c
SS
4796 offset += 4;
4797 }
4798 else if (bit (this_instr, 24))
4799 offset = -4;
c5aa993b 4800
51370a33
YQ
4801 nextpc =
4802 (CORE_ADDR) read_memory_unsigned_integer ((CORE_ADDR)
4803 (rn_val + offset),
4804 4, byte_order);
c906108c
SS
4805 }
4806 }
4807 break;
c5aa993b
JM
4808
4809 case 0xb: /* branch & link */
4810 case 0xa: /* branch */
c906108c
SS
4811 {
4812 nextpc = BranchDest (pc, this_instr);
c906108c
SS
4813 break;
4814 }
c5aa993b
JM
4815
4816 case 0xc:
4817 case 0xd:
4818 case 0xe: /* coproc ops */
25b41d01 4819 break;
c5aa993b 4820 case 0xf: /* SWI */
25b41d01
YQ
4821 {
4822 struct gdbarch_tdep *tdep;
4823 tdep = gdbarch_tdep (gdbarch);
4824
4825 if (tdep->syscall_next_pc != NULL)
4826 nextpc = tdep->syscall_next_pc (frame);
4827
4828 }
c906108c
SS
4829 break;
4830
4831 default:
edefbb7c 4832 fprintf_filtered (gdb_stderr, _("Bad bit-field extraction\n"));
c906108c
SS
4833 return (pc);
4834 }
4835 }
4836
4837 return nextpc;
4838}
4839
18819fa6
UW
4840/* Determine next PC after current instruction executes. Will call either
4841 arm_get_next_pc_raw or thumb_get_next_pc_raw. Error out if infinite
4842 loop is detected. */
4843
50e98be4
DJ
4844CORE_ADDR
4845arm_get_next_pc (struct frame_info *frame, CORE_ADDR pc)
4846{
18819fa6
UW
4847 CORE_ADDR nextpc;
4848
4849 if (arm_frame_is_thumb (frame))
4850 {
4851 nextpc = thumb_get_next_pc_raw (frame, pc);
4852 if (nextpc == MAKE_THUMB_ADDR (pc))
4853 error (_("Infinite loop detected"));
4854 }
4855 else
4856 {
4857 nextpc = arm_get_next_pc_raw (frame, pc);
4858 if (nextpc == pc)
4859 error (_("Infinite loop detected"));
4860 }
4861
50e98be4
DJ
4862 return nextpc;
4863}
4864
18819fa6
UW
4865/* Like insert_single_step_breakpoint, but make sure we use a breakpoint
4866 of the appropriate mode (as encoded in the PC value), even if this
4867 differs from what would be expected according to the symbol tables. */
4868
4869void
4870arm_insert_single_step_breakpoint (struct gdbarch *gdbarch,
4871 struct address_space *aspace,
4872 CORE_ADDR pc)
4873{
4874 struct cleanup *old_chain
4875 = make_cleanup_restore_integer (&arm_override_mode);
4876
4877 arm_override_mode = IS_THUMB_ADDR (pc);
4878 pc = gdbarch_addr_bits_remove (gdbarch, pc);
4879
4880 insert_single_step_breakpoint (gdbarch, aspace, pc);
4881
4882 do_cleanups (old_chain);
4883}
4884
35f73cfc
UW
4885/* Checks for an atomic sequence of instructions beginning with a LDREX{,B,H,D}
4886 instruction and ending with a STREX{,B,H,D} instruction. If such a sequence
4887 is found, attempt to step through it. A breakpoint is placed at the end of
4888 the sequence. */
4889
4890static int
4891thumb_deal_with_atomic_sequence_raw (struct frame_info *frame)
4892{
4893 struct gdbarch *gdbarch = get_frame_arch (frame);
4894 struct address_space *aspace = get_frame_address_space (frame);
4895 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4896 CORE_ADDR pc = get_frame_pc (frame);
4897 CORE_ADDR breaks[2] = {-1, -1};
4898 CORE_ADDR loc = pc;
4899 unsigned short insn1, insn2;
4900 int insn_count;
4901 int index;
4902 int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed). */
4903 const int atomic_sequence_length = 16; /* Instruction sequence length. */
4904 ULONGEST status, itstate;
4905
4906 /* We currently do not support atomic sequences within an IT block. */
4907 status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
4908 itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
4909 if (itstate & 0x0f)
4910 return 0;
4911
4912 /* Assume all atomic sequences start with a ldrex{,b,h,d} instruction. */
4913 insn1 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
4914 loc += 2;
4915 if (thumb_insn_size (insn1) != 4)
4916 return 0;
4917
4918 insn2 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
4919 loc += 2;
4920 if (!((insn1 & 0xfff0) == 0xe850
4921 || ((insn1 & 0xfff0) == 0xe8d0 && (insn2 & 0x00c0) == 0x0040)))
4922 return 0;
4923
4924 /* Assume that no atomic sequence is longer than "atomic_sequence_length"
4925 instructions. */
4926 for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
4927 {
4928 insn1 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
4929 loc += 2;
4930
4931 if (thumb_insn_size (insn1) != 4)
4932 {
4933 /* Assume that there is at most one conditional branch in the
4934 atomic sequence. If a conditional branch is found, put a
4935 breakpoint in its destination address. */
4936 if ((insn1 & 0xf000) == 0xd000 && bits (insn1, 8, 11) != 0x0f)
4937 {
4938 if (last_breakpoint > 0)
4939 return 0; /* More than one conditional branch found,
4940 fallback to the standard code. */
4941
4942 breaks[1] = loc + 2 + (sbits (insn1, 0, 7) << 1);
4943 last_breakpoint++;
4944 }
4945
4946 /* We do not support atomic sequences that use any *other*
4947 instructions but conditional branches to change the PC.
4948 Fall back to standard code to avoid losing control of
4949 execution. */
4950 else if (thumb_instruction_changes_pc (insn1))
4951 return 0;
4952 }
4953 else
4954 {
4955 insn2 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
4956 loc += 2;
4957
4958 /* Assume that there is at most one conditional branch in the
4959 atomic sequence. If a conditional branch is found, put a
4960 breakpoint in its destination address. */
4961 if ((insn1 & 0xf800) == 0xf000
4962 && (insn2 & 0xd000) == 0x8000
4963 && (insn1 & 0x0380) != 0x0380)
4964 {
4965 int sign, j1, j2, imm1, imm2;
4966 unsigned int offset;
4967
4968 sign = sbits (insn1, 10, 10);
4969 imm1 = bits (insn1, 0, 5);
4970 imm2 = bits (insn2, 0, 10);
4971 j1 = bit (insn2, 13);
4972 j2 = bit (insn2, 11);
4973
4974 offset = (sign << 20) + (j2 << 19) + (j1 << 18);
4975 offset += (imm1 << 12) + (imm2 << 1);
4976
4977 if (last_breakpoint > 0)
4978 return 0; /* More than one conditional branch found,
4979 fallback to the standard code. */
4980
4981 breaks[1] = loc + offset;
4982 last_breakpoint++;
4983 }
4984
4985 /* We do not support atomic sequences that use any *other*
4986 instructions but conditional branches to change the PC.
4987 Fall back to standard code to avoid losing control of
4988 execution. */
4989 else if (thumb2_instruction_changes_pc (insn1, insn2))
4990 return 0;
4991
4992 /* If we find a strex{,b,h,d}, we're done. */
4993 if ((insn1 & 0xfff0) == 0xe840
4994 || ((insn1 & 0xfff0) == 0xe8c0 && (insn2 & 0x00c0) == 0x0040))
4995 break;
4996 }
4997 }
4998
4999 /* If we didn't find the strex{,b,h,d}, we cannot handle the sequence. */
5000 if (insn_count == atomic_sequence_length)
5001 return 0;
5002
5003 /* Insert a breakpoint right after the end of the atomic sequence. */
5004 breaks[0] = loc;
5005
5006 /* Check for duplicated breakpoints. Check also for a breakpoint
5007 placed (branch instruction's destination) anywhere in sequence. */
5008 if (last_breakpoint
5009 && (breaks[1] == breaks[0]
5010 || (breaks[1] >= pc && breaks[1] < loc)))
5011 last_breakpoint = 0;
5012
5013 /* Effectively inserts the breakpoints. */
5014 for (index = 0; index <= last_breakpoint; index++)
5015 arm_insert_single_step_breakpoint (gdbarch, aspace,
5016 MAKE_THUMB_ADDR (breaks[index]));
5017
5018 return 1;
5019}
5020
5021static int
5022arm_deal_with_atomic_sequence_raw (struct frame_info *frame)
5023{
5024 struct gdbarch *gdbarch = get_frame_arch (frame);
5025 struct address_space *aspace = get_frame_address_space (frame);
5026 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
5027 CORE_ADDR pc = get_frame_pc (frame);
5028 CORE_ADDR breaks[2] = {-1, -1};
5029 CORE_ADDR loc = pc;
5030 unsigned int insn;
5031 int insn_count;
5032 int index;
5033 int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed). */
5034 const int atomic_sequence_length = 16; /* Instruction sequence length. */
5035
5036 /* Assume all atomic sequences start with a ldrex{,b,h,d} instruction.
5037 Note that we do not currently support conditionally executed atomic
5038 instructions. */
5039 insn = read_memory_unsigned_integer (loc, 4, byte_order_for_code);
5040 loc += 4;
5041 if ((insn & 0xff9000f0) != 0xe1900090)
5042 return 0;
5043
5044 /* Assume that no atomic sequence is longer than "atomic_sequence_length"
5045 instructions. */
5046 for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
5047 {
5048 insn = read_memory_unsigned_integer (loc, 4, byte_order_for_code);
5049 loc += 4;
5050
5051 /* Assume that there is at most one conditional branch in the atomic
5052 sequence. If a conditional branch is found, put a breakpoint in
5053 its destination address. */
5054 if (bits (insn, 24, 27) == 0xa)
5055 {
5056 if (last_breakpoint > 0)
5057 return 0; /* More than one conditional branch found, fallback
5058 to the standard single-step code. */
5059
5060 breaks[1] = BranchDest (loc - 4, insn);
5061 last_breakpoint++;
5062 }
5063
5064 /* We do not support atomic sequences that use any *other* instructions
5065 but conditional branches to change the PC. Fall back to standard
5066 code to avoid losing control of execution. */
5067 else if (arm_instruction_changes_pc (insn))
5068 return 0;
5069
5070 /* If we find a strex{,b,h,d}, we're done. */
5071 if ((insn & 0xff9000f0) == 0xe1800090)
5072 break;
5073 }
5074
5075 /* If we didn't find the strex{,b,h,d}, we cannot handle the sequence. */
5076 if (insn_count == atomic_sequence_length)
5077 return 0;
5078
5079 /* Insert a breakpoint right after the end of the atomic sequence. */
5080 breaks[0] = loc;
5081
5082 /* Check for duplicated breakpoints. Check also for a breakpoint
5083 placed (branch instruction's destination) anywhere in sequence. */
5084 if (last_breakpoint
5085 && (breaks[1] == breaks[0]
5086 || (breaks[1] >= pc && breaks[1] < loc)))
5087 last_breakpoint = 0;
5088
5089 /* Effectively inserts the breakpoints. */
5090 for (index = 0; index <= last_breakpoint; index++)
5091 arm_insert_single_step_breakpoint (gdbarch, aspace, breaks[index]);
5092
5093 return 1;
5094}
5095
5096int
5097arm_deal_with_atomic_sequence (struct frame_info *frame)
5098{
5099 if (arm_frame_is_thumb (frame))
5100 return thumb_deal_with_atomic_sequence_raw (frame);
5101 else
5102 return arm_deal_with_atomic_sequence_raw (frame);
5103}
5104
9512d7fd
FN
5105/* single_step() is called just before we want to resume the inferior,
5106 if we want to single-step it but there is no hardware or kernel
5107 single-step support. We find the target of the coming instruction
e0cd558a 5108 and breakpoint it. */
9512d7fd 5109
190dce09 5110int
0b1b3e42 5111arm_software_single_step (struct frame_info *frame)
9512d7fd 5112{
a6d9a66e 5113 struct gdbarch *gdbarch = get_frame_arch (frame);
6c95b8df 5114 struct address_space *aspace = get_frame_address_space (frame);
35f73cfc
UW
5115 CORE_ADDR next_pc;
5116
5117 if (arm_deal_with_atomic_sequence (frame))
5118 return 1;
18819fa6 5119
35f73cfc 5120 next_pc = arm_get_next_pc (frame, get_frame_pc (frame));
18819fa6 5121 arm_insert_single_step_breakpoint (gdbarch, aspace, next_pc);
e6590a1b
UW
5122
5123 return 1;
9512d7fd 5124}
9512d7fd 5125
f9d67f43
DJ
5126/* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
5127 the buffer to be NEW_LEN bytes ending at ENDADDR. Return
5128 NULL if an error occurs. BUF is freed. */
5129
5130static gdb_byte *
5131extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
5132 int old_len, int new_len)
5133{
22e048c9 5134 gdb_byte *new_buf;
f9d67f43
DJ
5135 int bytes_to_read = new_len - old_len;
5136
5137 new_buf = xmalloc (new_len);
5138 memcpy (new_buf + bytes_to_read, buf, old_len);
5139 xfree (buf);
5140 if (target_read_memory (endaddr - new_len, new_buf, bytes_to_read) != 0)
5141 {
5142 xfree (new_buf);
5143 return NULL;
5144 }
5145 return new_buf;
5146}
5147
5148/* An IT block is at most the 2-byte IT instruction followed by
5149 four 4-byte instructions. The furthest back we must search to
5150 find an IT block that affects the current instruction is thus
5151 2 + 3 * 4 == 14 bytes. */
5152#define MAX_IT_BLOCK_PREFIX 14
5153
5154/* Use a quick scan if there are more than this many bytes of
5155 code. */
5156#define IT_SCAN_THRESHOLD 32
5157
5158/* Adjust a breakpoint's address to move breakpoints out of IT blocks.
5159 A breakpoint in an IT block may not be hit, depending on the
5160 condition flags. */
5161static CORE_ADDR
5162arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
5163{
5164 gdb_byte *buf;
5165 char map_type;
5166 CORE_ADDR boundary, func_start;
22e048c9 5167 int buf_len;
f9d67f43
DJ
5168 enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
5169 int i, any, last_it, last_it_count;
5170
5171 /* If we are using BKPT breakpoints, none of this is necessary. */
5172 if (gdbarch_tdep (gdbarch)->thumb2_breakpoint == NULL)
5173 return bpaddr;
5174
5175 /* ARM mode does not have this problem. */
9779414d 5176 if (!arm_pc_is_thumb (gdbarch, bpaddr))
f9d67f43
DJ
5177 return bpaddr;
5178
5179 /* We are setting a breakpoint in Thumb code that could potentially
5180 contain an IT block. The first step is to find how much Thumb
5181 code there is; we do not need to read outside of known Thumb
5182 sequences. */
5183 map_type = arm_find_mapping_symbol (bpaddr, &boundary);
5184 if (map_type == 0)
5185 /* Thumb-2 code must have mapping symbols to have a chance. */
5186 return bpaddr;
5187
5188 bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
5189
5190 if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
5191 && func_start > boundary)
5192 boundary = func_start;
5193
5194 /* Search for a candidate IT instruction. We have to do some fancy
5195 footwork to distinguish a real IT instruction from the second
5196 half of a 32-bit instruction, but there is no need for that if
5197 there's no candidate. */
5198 buf_len = min (bpaddr - boundary, MAX_IT_BLOCK_PREFIX);
5199 if (buf_len == 0)
5200 /* No room for an IT instruction. */
5201 return bpaddr;
5202
5203 buf = xmalloc (buf_len);
5204 if (target_read_memory (bpaddr - buf_len, buf, buf_len) != 0)
5205 return bpaddr;
5206 any = 0;
5207 for (i = 0; i < buf_len; i += 2)
5208 {
5209 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5210 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5211 {
5212 any = 1;
5213 break;
5214 }
5215 }
5216 if (any == 0)
5217 {
5218 xfree (buf);
5219 return bpaddr;
5220 }
5221
5222 /* OK, the code bytes before this instruction contain at least one
5223 halfword which resembles an IT instruction. We know that it's
5224 Thumb code, but there are still two possibilities. Either the
5225 halfword really is an IT instruction, or it is the second half of
5226 a 32-bit Thumb instruction. The only way we can tell is to
5227 scan forwards from a known instruction boundary. */
5228 if (bpaddr - boundary > IT_SCAN_THRESHOLD)
5229 {
5230 int definite;
5231
5232 /* There's a lot of code before this instruction. Start with an
5233 optimistic search; it's easy to recognize halfwords that can
5234 not be the start of a 32-bit instruction, and use that to
5235 lock on to the instruction boundaries. */
5236 buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
5237 if (buf == NULL)
5238 return bpaddr;
5239 buf_len = IT_SCAN_THRESHOLD;
5240
5241 definite = 0;
5242 for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
5243 {
5244 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5245 if (thumb_insn_size (inst1) == 2)
5246 {
5247 definite = 1;
5248 break;
5249 }
5250 }
5251
5252 /* At this point, if DEFINITE, BUF[I] is the first place we
5253 are sure that we know the instruction boundaries, and it is far
5254 enough from BPADDR that we could not miss an IT instruction
5255 affecting BPADDR. If ! DEFINITE, give up - start from a
5256 known boundary. */
5257 if (! definite)
5258 {
0963b4bd
MS
5259 buf = extend_buffer_earlier (buf, bpaddr, buf_len,
5260 bpaddr - boundary);
f9d67f43
DJ
5261 if (buf == NULL)
5262 return bpaddr;
5263 buf_len = bpaddr - boundary;
5264 i = 0;
5265 }
5266 }
5267 else
5268 {
5269 buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
5270 if (buf == NULL)
5271 return bpaddr;
5272 buf_len = bpaddr - boundary;
5273 i = 0;
5274 }
5275
5276 /* Scan forwards. Find the last IT instruction before BPADDR. */
5277 last_it = -1;
5278 last_it_count = 0;
5279 while (i < buf_len)
5280 {
5281 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5282 last_it_count--;
5283 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5284 {
5285 last_it = i;
5286 if (inst1 & 0x0001)
5287 last_it_count = 4;
5288 else if (inst1 & 0x0002)
5289 last_it_count = 3;
5290 else if (inst1 & 0x0004)
5291 last_it_count = 2;
5292 else
5293 last_it_count = 1;
5294 }
5295 i += thumb_insn_size (inst1);
5296 }
5297
5298 xfree (buf);
5299
5300 if (last_it == -1)
5301 /* There wasn't really an IT instruction after all. */
5302 return bpaddr;
5303
5304 if (last_it_count < 1)
5305 /* It was too far away. */
5306 return bpaddr;
5307
5308 /* This really is a trouble spot. Move the breakpoint to the IT
5309 instruction. */
5310 return bpaddr - buf_len + last_it;
5311}
5312
cca44b1b 5313/* ARM displaced stepping support.
c906108c 5314
cca44b1b 5315 Generally ARM displaced stepping works as follows:
c906108c 5316
cca44b1b
JB
5317 1. When an instruction is to be single-stepped, it is first decoded by
5318 arm_process_displaced_insn (called from arm_displaced_step_copy_insn).
5319 Depending on the type of instruction, it is then copied to a scratch
5320 location, possibly in a modified form. The copy_* set of functions
0963b4bd 5321 performs such modification, as necessary. A breakpoint is placed after
cca44b1b
JB
5322 the modified instruction in the scratch space to return control to GDB.
5323 Note in particular that instructions which modify the PC will no longer
5324 do so after modification.
c5aa993b 5325
cca44b1b
JB
5326 2. The instruction is single-stepped, by setting the PC to the scratch
5327 location address, and resuming. Control returns to GDB when the
5328 breakpoint is hit.
c5aa993b 5329
cca44b1b
JB
5330 3. A cleanup function (cleanup_*) is called corresponding to the copy_*
5331 function used for the current instruction. This function's job is to
5332 put the CPU/memory state back to what it would have been if the
5333 instruction had been executed unmodified in its original location. */
c5aa993b 5334
cca44b1b
JB
5335/* NOP instruction (mov r0, r0). */
5336#define ARM_NOP 0xe1a00000
34518530 5337#define THUMB_NOP 0x4600
cca44b1b
JB
5338
5339/* Helper for register reads for displaced stepping. In particular, this
5340 returns the PC as it would be seen by the instruction at its original
5341 location. */
5342
5343ULONGEST
36073a92
YQ
5344displaced_read_reg (struct regcache *regs, struct displaced_step_closure *dsc,
5345 int regno)
cca44b1b
JB
5346{
5347 ULONGEST ret;
36073a92 5348 CORE_ADDR from = dsc->insn_addr;
cca44b1b 5349
bf9f652a 5350 if (regno == ARM_PC_REGNUM)
cca44b1b 5351 {
4db71c0b
YQ
5352 /* Compute pipeline offset:
5353 - When executing an ARM instruction, PC reads as the address of the
5354 current instruction plus 8.
5355 - When executing a Thumb instruction, PC reads as the address of the
5356 current instruction plus 4. */
5357
36073a92 5358 if (!dsc->is_thumb)
4db71c0b
YQ
5359 from += 8;
5360 else
5361 from += 4;
5362
cca44b1b
JB
5363 if (debug_displaced)
5364 fprintf_unfiltered (gdb_stdlog, "displaced: read pc value %.8lx\n",
4db71c0b
YQ
5365 (unsigned long) from);
5366 return (ULONGEST) from;
cca44b1b 5367 }
c906108c 5368 else
cca44b1b
JB
5369 {
5370 regcache_cooked_read_unsigned (regs, regno, &ret);
5371 if (debug_displaced)
5372 fprintf_unfiltered (gdb_stdlog, "displaced: read r%d value %.8lx\n",
5373 regno, (unsigned long) ret);
5374 return ret;
5375 }
c906108c
SS
5376}
5377
cca44b1b
JB
5378static int
5379displaced_in_arm_mode (struct regcache *regs)
5380{
5381 ULONGEST ps;
9779414d 5382 ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
66e810cd 5383
cca44b1b 5384 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
66e810cd 5385
9779414d 5386 return (ps & t_bit) == 0;
cca44b1b 5387}
66e810cd 5388
cca44b1b 5389/* Write to the PC as from a branch instruction. */
c906108c 5390
cca44b1b 5391static void
36073a92
YQ
5392branch_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5393 ULONGEST val)
c906108c 5394{
36073a92 5395 if (!dsc->is_thumb)
cca44b1b
JB
5396 /* Note: If bits 0/1 are set, this branch would be unpredictable for
5397 architecture versions < 6. */
0963b4bd
MS
5398 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5399 val & ~(ULONGEST) 0x3);
cca44b1b 5400 else
0963b4bd
MS
5401 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5402 val & ~(ULONGEST) 0x1);
cca44b1b 5403}
66e810cd 5404
cca44b1b
JB
5405/* Write to the PC as from a branch-exchange instruction. */
5406
5407static void
5408bx_write_pc (struct regcache *regs, ULONGEST val)
5409{
5410 ULONGEST ps;
9779414d 5411 ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
cca44b1b
JB
5412
5413 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
5414
5415 if ((val & 1) == 1)
c906108c 5416 {
9779414d 5417 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
cca44b1b
JB
5418 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
5419 }
5420 else if ((val & 2) == 0)
5421 {
9779414d 5422 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 5423 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
c906108c
SS
5424 }
5425 else
5426 {
cca44b1b
JB
5427 /* Unpredictable behaviour. Try to do something sensible (switch to ARM
5428 mode, align dest to 4 bytes). */
5429 warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
9779414d 5430 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 5431 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
c906108c
SS
5432 }
5433}
ed9a39eb 5434
cca44b1b 5435/* Write to the PC as if from a load instruction. */
ed9a39eb 5436
34e8f22d 5437static void
36073a92
YQ
5438load_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5439 ULONGEST val)
ed9a39eb 5440{
cca44b1b
JB
5441 if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
5442 bx_write_pc (regs, val);
5443 else
36073a92 5444 branch_write_pc (regs, dsc, val);
cca44b1b 5445}
be8626e0 5446
cca44b1b
JB
5447/* Write to the PC as if from an ALU instruction. */
5448
5449static void
36073a92
YQ
5450alu_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5451 ULONGEST val)
cca44b1b 5452{
36073a92 5453 if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
cca44b1b
JB
5454 bx_write_pc (regs, val);
5455 else
36073a92 5456 branch_write_pc (regs, dsc, val);
cca44b1b
JB
5457}
5458
5459/* Helper for writing to registers for displaced stepping. Writing to the PC
5460 has a varying effects depending on the instruction which does the write:
5461 this is controlled by the WRITE_PC argument. */
5462
5463void
5464displaced_write_reg (struct regcache *regs, struct displaced_step_closure *dsc,
5465 int regno, ULONGEST val, enum pc_write_style write_pc)
5466{
bf9f652a 5467 if (regno == ARM_PC_REGNUM)
08216dd7 5468 {
cca44b1b
JB
5469 if (debug_displaced)
5470 fprintf_unfiltered (gdb_stdlog, "displaced: writing pc %.8lx\n",
5471 (unsigned long) val);
5472 switch (write_pc)
08216dd7 5473 {
cca44b1b 5474 case BRANCH_WRITE_PC:
36073a92 5475 branch_write_pc (regs, dsc, val);
08216dd7
RE
5476 break;
5477
cca44b1b
JB
5478 case BX_WRITE_PC:
5479 bx_write_pc (regs, val);
5480 break;
5481
5482 case LOAD_WRITE_PC:
36073a92 5483 load_write_pc (regs, dsc, val);
cca44b1b
JB
5484 break;
5485
5486 case ALU_WRITE_PC:
36073a92 5487 alu_write_pc (regs, dsc, val);
cca44b1b
JB
5488 break;
5489
5490 case CANNOT_WRITE_PC:
5491 warning (_("Instruction wrote to PC in an unexpected way when "
5492 "single-stepping"));
08216dd7
RE
5493 break;
5494
5495 default:
97b9747c
JB
5496 internal_error (__FILE__, __LINE__,
5497 _("Invalid argument to displaced_write_reg"));
08216dd7 5498 }
b508a996 5499
cca44b1b 5500 dsc->wrote_to_pc = 1;
b508a996 5501 }
ed9a39eb 5502 else
b508a996 5503 {
cca44b1b
JB
5504 if (debug_displaced)
5505 fprintf_unfiltered (gdb_stdlog, "displaced: writing r%d value %.8lx\n",
5506 regno, (unsigned long) val);
5507 regcache_cooked_write_unsigned (regs, regno, val);
b508a996 5508 }
34e8f22d
RE
5509}
5510
cca44b1b
JB
5511/* This function is used to concisely determine if an instruction INSN
5512 references PC. Register fields of interest in INSN should have the
0963b4bd
MS
5513 corresponding fields of BITMASK set to 0b1111. The function
5514 returns return 1 if any of these fields in INSN reference the PC
5515 (also 0b1111, r15), else it returns 0. */
67255d04
RE
5516
5517static int
cca44b1b 5518insn_references_pc (uint32_t insn, uint32_t bitmask)
67255d04 5519{
cca44b1b 5520 uint32_t lowbit = 1;
67255d04 5521
cca44b1b
JB
5522 while (bitmask != 0)
5523 {
5524 uint32_t mask;
44e1a9eb 5525
cca44b1b
JB
5526 for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
5527 ;
67255d04 5528
cca44b1b
JB
5529 if (!lowbit)
5530 break;
67255d04 5531
cca44b1b 5532 mask = lowbit * 0xf;
67255d04 5533
cca44b1b
JB
5534 if ((insn & mask) == mask)
5535 return 1;
5536
5537 bitmask &= ~mask;
67255d04
RE
5538 }
5539
cca44b1b
JB
5540 return 0;
5541}
2af48f68 5542
cca44b1b
JB
5543/* The simplest copy function. Many instructions have the same effect no
5544 matter what address they are executed at: in those cases, use this. */
67255d04 5545
cca44b1b 5546static int
7ff120b4
YQ
5547arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn,
5548 const char *iname, struct displaced_step_closure *dsc)
cca44b1b
JB
5549{
5550 if (debug_displaced)
5551 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx, "
5552 "opcode/class '%s' unmodified\n", (unsigned long) insn,
5553 iname);
67255d04 5554
cca44b1b 5555 dsc->modinsn[0] = insn;
67255d04 5556
cca44b1b
JB
5557 return 0;
5558}
5559
34518530
YQ
5560static int
5561thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
5562 uint16_t insn2, const char *iname,
5563 struct displaced_step_closure *dsc)
5564{
5565 if (debug_displaced)
5566 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x %.4x, "
5567 "opcode/class '%s' unmodified\n", insn1, insn2,
5568 iname);
5569
5570 dsc->modinsn[0] = insn1;
5571 dsc->modinsn[1] = insn2;
5572 dsc->numinsns = 2;
5573
5574 return 0;
5575}
5576
5577/* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
5578 modification. */
5579static int
5580thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, unsigned int insn,
5581 const char *iname,
5582 struct displaced_step_closure *dsc)
5583{
5584 if (debug_displaced)
5585 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x, "
5586 "opcode/class '%s' unmodified\n", insn,
5587 iname);
5588
5589 dsc->modinsn[0] = insn;
5590
5591 return 0;
5592}
5593
cca44b1b
JB
5594/* Preload instructions with immediate offset. */
5595
5596static void
6e39997a 5597cleanup_preload (struct gdbarch *gdbarch,
cca44b1b
JB
5598 struct regcache *regs, struct displaced_step_closure *dsc)
5599{
5600 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5601 if (!dsc->u.preload.immed)
5602 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5603}
5604
7ff120b4
YQ
5605static void
5606install_preload (struct gdbarch *gdbarch, struct regcache *regs,
5607 struct displaced_step_closure *dsc, unsigned int rn)
cca44b1b 5608{
cca44b1b 5609 ULONGEST rn_val;
cca44b1b
JB
5610 /* Preload instructions:
5611
5612 {pli/pld} [rn, #+/-imm]
5613 ->
5614 {pli/pld} [r0, #+/-imm]. */
5615
36073a92
YQ
5616 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5617 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 5618 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
cca44b1b
JB
5619 dsc->u.preload.immed = 1;
5620
cca44b1b 5621 dsc->cleanup = &cleanup_preload;
cca44b1b
JB
5622}
5623
cca44b1b 5624static int
7ff120b4 5625arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
cca44b1b
JB
5626 struct displaced_step_closure *dsc)
5627{
5628 unsigned int rn = bits (insn, 16, 19);
cca44b1b 5629
7ff120b4
YQ
5630 if (!insn_references_pc (insn, 0x000f0000ul))
5631 return arm_copy_unmodified (gdbarch, insn, "preload", dsc);
cca44b1b
JB
5632
5633 if (debug_displaced)
5634 fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
5635 (unsigned long) insn);
5636
7ff120b4
YQ
5637 dsc->modinsn[0] = insn & 0xfff0ffff;
5638
5639 install_preload (gdbarch, regs, dsc, rn);
5640
5641 return 0;
5642}
5643
34518530
YQ
5644static int
5645thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
5646 struct regcache *regs, struct displaced_step_closure *dsc)
5647{
5648 unsigned int rn = bits (insn1, 0, 3);
5649 unsigned int u_bit = bit (insn1, 7);
5650 int imm12 = bits (insn2, 0, 11);
5651 ULONGEST pc_val;
5652
5653 if (rn != ARM_PC_REGNUM)
5654 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);
5655
5656 /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
5657 PLD (literal) Encoding T1. */
5658 if (debug_displaced)
5659 fprintf_unfiltered (gdb_stdlog,
5660 "displaced: copying pld/pli pc (0x%x) %c imm12 %.4x\n",
5661 (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
5662 imm12);
5663
5664 if (!u_bit)
5665 imm12 = -1 * imm12;
5666
5667 /* Rewrite instruction {pli/pld} PC imm12 into:
5668 Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12
5669
5670 {pli/pld} [r0, r1]
5671
5672 Cleanup: r0 <- tmp[0], r1 <- tmp[1]. */
5673
5674 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5675 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5676
5677 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
5678
5679 displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
5680 displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
5681 dsc->u.preload.immed = 0;
5682
5683 /* {pli/pld} [r0, r1] */
5684 dsc->modinsn[0] = insn1 & 0xfff0;
5685 dsc->modinsn[1] = 0xf001;
5686 dsc->numinsns = 2;
5687
5688 dsc->cleanup = &cleanup_preload;
5689 return 0;
5690}
5691
7ff120b4
YQ
5692/* Preload instructions with register offset. */
5693
5694static void
5695install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
5696 struct displaced_step_closure *dsc, unsigned int rn,
5697 unsigned int rm)
5698{
5699 ULONGEST rn_val, rm_val;
5700
cca44b1b
JB
5701 /* Preload register-offset instructions:
5702
5703 {pli/pld} [rn, rm {, shift}]
5704 ->
5705 {pli/pld} [r0, r1 {, shift}]. */
5706
36073a92
YQ
5707 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5708 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5709 rn_val = displaced_read_reg (regs, dsc, rn);
5710 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5711 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5712 displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
cca44b1b
JB
5713 dsc->u.preload.immed = 0;
5714
cca44b1b 5715 dsc->cleanup = &cleanup_preload;
7ff120b4
YQ
5716}
5717
5718static int
5719arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
5720 struct regcache *regs,
5721 struct displaced_step_closure *dsc)
5722{
5723 unsigned int rn = bits (insn, 16, 19);
5724 unsigned int rm = bits (insn, 0, 3);
5725
5726
5727 if (!insn_references_pc (insn, 0x000f000ful))
5728 return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);
5729
5730 if (debug_displaced)
5731 fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
5732 (unsigned long) insn);
5733
5734 dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
cca44b1b 5735
7ff120b4 5736 install_preload_reg (gdbarch, regs, dsc, rn, rm);
cca44b1b
JB
5737 return 0;
5738}
5739
5740/* Copy/cleanup coprocessor load and store instructions. */
5741
5742static void
6e39997a 5743cleanup_copro_load_store (struct gdbarch *gdbarch,
cca44b1b
JB
5744 struct regcache *regs,
5745 struct displaced_step_closure *dsc)
5746{
36073a92 5747 ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5748
5749 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5750
5751 if (dsc->u.ldst.writeback)
5752 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
5753}
5754
7ff120b4
YQ
5755static void
5756install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
5757 struct displaced_step_closure *dsc,
5758 int writeback, unsigned int rn)
cca44b1b 5759{
cca44b1b 5760 ULONGEST rn_val;
cca44b1b 5761
cca44b1b
JB
5762 /* Coprocessor load/store instructions:
5763
5764 {stc/stc2} [<Rn>, #+/-imm] (and other immediate addressing modes)
5765 ->
5766 {stc/stc2} [r0, #+/-imm].
5767
5768 ldc/ldc2 are handled identically. */
5769
36073a92
YQ
5770 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5771 rn_val = displaced_read_reg (regs, dsc, rn);
2b16b2e3
YQ
5772 /* PC should be 4-byte aligned. */
5773 rn_val = rn_val & 0xfffffffc;
cca44b1b
JB
5774 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5775
7ff120b4 5776 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
5777 dsc->u.ldst.rn = rn;
5778
7ff120b4
YQ
5779 dsc->cleanup = &cleanup_copro_load_store;
5780}
5781
5782static int
5783arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
5784 struct regcache *regs,
5785 struct displaced_step_closure *dsc)
5786{
5787 unsigned int rn = bits (insn, 16, 19);
5788
5789 if (!insn_references_pc (insn, 0x000f0000ul))
5790 return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);
5791
5792 if (debug_displaced)
5793 fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
5794 "load/store insn %.8lx\n", (unsigned long) insn);
5795
cca44b1b
JB
5796 dsc->modinsn[0] = insn & 0xfff0ffff;
5797
7ff120b4 5798 install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);
cca44b1b
JB
5799
5800 return 0;
5801}
5802
34518530
YQ
5803static int
5804thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
5805 uint16_t insn2, struct regcache *regs,
5806 struct displaced_step_closure *dsc)
5807{
5808 unsigned int rn = bits (insn1, 0, 3);
5809
5810 if (rn != ARM_PC_REGNUM)
5811 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
5812 "copro load/store", dsc);
5813
5814 if (debug_displaced)
5815 fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
5816 "load/store insn %.4x%.4x\n", insn1, insn2);
5817
5818 dsc->modinsn[0] = insn1 & 0xfff0;
5819 dsc->modinsn[1] = insn2;
5820 dsc->numinsns = 2;
5821
5822 /* This function is called for copying instruction LDC/LDC2/VLDR, which
5823 doesn't support writeback, so pass 0. */
5824 install_copro_load_store (gdbarch, regs, dsc, 0, rn);
5825
5826 return 0;
5827}
5828
cca44b1b
JB
5829/* Clean up branch instructions (actually perform the branch, by setting
5830 PC). */
5831
5832static void
6e39997a 5833cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
5834 struct displaced_step_closure *dsc)
5835{
36073a92 5836 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
5837 int branch_taken = condition_true (dsc->u.branch.cond, status);
5838 enum pc_write_style write_pc = dsc->u.branch.exchange
5839 ? BX_WRITE_PC : BRANCH_WRITE_PC;
5840
5841 if (!branch_taken)
5842 return;
5843
5844 if (dsc->u.branch.link)
5845 {
8c8dba6d
YQ
5846 /* The value of LR should be the next insn of current one. In order
5847 not to confuse logic hanlding later insn `bx lr', if current insn mode
5848 is Thumb, the bit 0 of LR value should be set to 1. */
5849 ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;
5850
5851 if (dsc->is_thumb)
5852 next_insn_addr |= 0x1;
5853
5854 displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
5855 CANNOT_WRITE_PC);
cca44b1b
JB
5856 }
5857
bf9f652a 5858 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
cca44b1b
JB
5859}
5860
5861/* Copy B/BL/BLX instructions with immediate destinations. */
5862
7ff120b4
YQ
5863static void
5864install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
5865 struct displaced_step_closure *dsc,
5866 unsigned int cond, int exchange, int link, long offset)
5867{
5868 /* Implement "BL<cond> <label>" as:
5869
5870 Preparation: cond <- instruction condition
5871 Insn: mov r0, r0 (nop)
5872 Cleanup: if (condition true) { r14 <- pc; pc <- label }.
5873
5874 B<cond> similar, but don't set r14 in cleanup. */
5875
5876 dsc->u.branch.cond = cond;
5877 dsc->u.branch.link = link;
5878 dsc->u.branch.exchange = exchange;
5879
2b16b2e3
YQ
5880 dsc->u.branch.dest = dsc->insn_addr;
5881 if (link && exchange)
5882 /* For BLX, offset is computed from the Align (PC, 4). */
5883 dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;
5884
7ff120b4 5885 if (dsc->is_thumb)
2b16b2e3 5886 dsc->u.branch.dest += 4 + offset;
7ff120b4 5887 else
2b16b2e3 5888 dsc->u.branch.dest += 8 + offset;
7ff120b4
YQ
5889
5890 dsc->cleanup = &cleanup_branch;
5891}
cca44b1b 5892static int
7ff120b4
YQ
5893arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
5894 struct regcache *regs, struct displaced_step_closure *dsc)
cca44b1b
JB
5895{
5896 unsigned int cond = bits (insn, 28, 31);
5897 int exchange = (cond == 0xf);
5898 int link = exchange || bit (insn, 24);
cca44b1b
JB
5899 long offset;
5900
5901 if (debug_displaced)
5902 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s immediate insn "
5903 "%.8lx\n", (exchange) ? "blx" : (link) ? "bl" : "b",
5904 (unsigned long) insn);
cca44b1b
JB
5905 if (exchange)
5906 /* For BLX, set bit 0 of the destination. The cleanup_branch function will
5907 then arrange the switch into Thumb mode. */
5908 offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
5909 else
5910 offset = bits (insn, 0, 23) << 2;
5911
5912 if (bit (offset, 25))
5913 offset = offset | ~0x3ffffff;
5914
cca44b1b
JB
5915 dsc->modinsn[0] = ARM_NOP;
5916
7ff120b4 5917 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
cca44b1b
JB
5918 return 0;
5919}
5920
34518530
YQ
5921static int
5922thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
5923 uint16_t insn2, struct regcache *regs,
5924 struct displaced_step_closure *dsc)
5925{
5926 int link = bit (insn2, 14);
5927 int exchange = link && !bit (insn2, 12);
5928 int cond = INST_AL;
5929 long offset = 0;
5930 int j1 = bit (insn2, 13);
5931 int j2 = bit (insn2, 11);
5932 int s = sbits (insn1, 10, 10);
5933 int i1 = !(j1 ^ bit (insn1, 10));
5934 int i2 = !(j2 ^ bit (insn1, 10));
5935
5936 if (!link && !exchange) /* B */
5937 {
5938 offset = (bits (insn2, 0, 10) << 1);
5939 if (bit (insn2, 12)) /* Encoding T4 */
5940 {
5941 offset |= (bits (insn1, 0, 9) << 12)
5942 | (i2 << 22)
5943 | (i1 << 23)
5944 | (s << 24);
5945 cond = INST_AL;
5946 }
5947 else /* Encoding T3 */
5948 {
5949 offset |= (bits (insn1, 0, 5) << 12)
5950 | (j1 << 18)
5951 | (j2 << 19)
5952 | (s << 20);
5953 cond = bits (insn1, 6, 9);
5954 }
5955 }
5956 else
5957 {
5958 offset = (bits (insn1, 0, 9) << 12);
5959 offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
5960 offset |= exchange ?
5961 (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
5962 }
5963
5964 if (debug_displaced)
5965 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s insn "
5966 "%.4x %.4x with offset %.8lx\n",
5967 link ? (exchange) ? "blx" : "bl" : "b",
5968 insn1, insn2, offset);
5969
5970 dsc->modinsn[0] = THUMB_NOP;
5971
5972 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
5973 return 0;
5974}
5975
5976/* Copy B Thumb instructions. */
5977static int
5978thumb_copy_b (struct gdbarch *gdbarch, unsigned short insn,
5979 struct displaced_step_closure *dsc)
5980{
5981 unsigned int cond = 0;
5982 int offset = 0;
5983 unsigned short bit_12_15 = bits (insn, 12, 15);
5984 CORE_ADDR from = dsc->insn_addr;
5985
5986 if (bit_12_15 == 0xd)
5987 {
5988 /* offset = SignExtend (imm8:0, 32) */
5989 offset = sbits ((insn << 1), 0, 8);
5990 cond = bits (insn, 8, 11);
5991 }
5992 else if (bit_12_15 == 0xe) /* Encoding T2 */
5993 {
5994 offset = sbits ((insn << 1), 0, 11);
5995 cond = INST_AL;
5996 }
5997
5998 if (debug_displaced)
5999 fprintf_unfiltered (gdb_stdlog,
6000 "displaced: copying b immediate insn %.4x "
6001 "with offset %d\n", insn, offset);
6002
6003 dsc->u.branch.cond = cond;
6004 dsc->u.branch.link = 0;
6005 dsc->u.branch.exchange = 0;
6006 dsc->u.branch.dest = from + 4 + offset;
6007
6008 dsc->modinsn[0] = THUMB_NOP;
6009
6010 dsc->cleanup = &cleanup_branch;
6011
6012 return 0;
6013}
6014
cca44b1b
JB
6015/* Copy BX/BLX with register-specified destinations. */
6016
7ff120b4
YQ
6017static void
6018install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
6019 struct displaced_step_closure *dsc, int link,
6020 unsigned int cond, unsigned int rm)
cca44b1b 6021{
cca44b1b
JB
6022 /* Implement {BX,BLX}<cond> <reg>" as:
6023
6024 Preparation: cond <- instruction condition
6025 Insn: mov r0, r0 (nop)
6026 Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
6027
6028 Don't set r14 in cleanup for BX. */
6029
36073a92 6030 dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6031
6032 dsc->u.branch.cond = cond;
6033 dsc->u.branch.link = link;
cca44b1b 6034
7ff120b4 6035 dsc->u.branch.exchange = 1;
cca44b1b
JB
6036
6037 dsc->cleanup = &cleanup_branch;
7ff120b4 6038}
cca44b1b 6039
7ff120b4
YQ
6040static int
6041arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
6042 struct regcache *regs, struct displaced_step_closure *dsc)
6043{
6044 unsigned int cond = bits (insn, 28, 31);
6045 /* BX: x12xxx1x
6046 BLX: x12xxx3x. */
6047 int link = bit (insn, 5);
6048 unsigned int rm = bits (insn, 0, 3);
6049
6050 if (debug_displaced)
6051 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx",
6052 (unsigned long) insn);
6053
6054 dsc->modinsn[0] = ARM_NOP;
6055
6056 install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
cca44b1b
JB
6057 return 0;
6058}
6059
34518530
YQ
6060static int
6061thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
6062 struct regcache *regs,
6063 struct displaced_step_closure *dsc)
6064{
6065 int link = bit (insn, 7);
6066 unsigned int rm = bits (insn, 3, 6);
6067
6068 if (debug_displaced)
6069 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x",
6070 (unsigned short) insn);
6071
6072 dsc->modinsn[0] = THUMB_NOP;
6073
6074 install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);
6075
6076 return 0;
6077}
6078
6079
0963b4bd 6080/* Copy/cleanup arithmetic/logic instruction with immediate RHS. */
cca44b1b
JB
6081
6082static void
6e39997a 6083cleanup_alu_imm (struct gdbarch *gdbarch,
cca44b1b
JB
6084 struct regcache *regs, struct displaced_step_closure *dsc)
6085{
36073a92 6086 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6087 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6088 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6089 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6090}
6091
6092static int
7ff120b4
YQ
6093arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
6094 struct displaced_step_closure *dsc)
cca44b1b
JB
6095{
6096 unsigned int rn = bits (insn, 16, 19);
6097 unsigned int rd = bits (insn, 12, 15);
6098 unsigned int op = bits (insn, 21, 24);
6099 int is_mov = (op == 0xd);
6100 ULONGEST rd_val, rn_val;
cca44b1b
JB
6101
6102 if (!insn_references_pc (insn, 0x000ff000ul))
7ff120b4 6103 return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
cca44b1b
JB
6104
6105 if (debug_displaced)
6106 fprintf_unfiltered (gdb_stdlog, "displaced: copying immediate %s insn "
6107 "%.8lx\n", is_mov ? "move" : "ALU",
6108 (unsigned long) insn);
6109
6110 /* Instruction is of form:
6111
6112 <op><cond> rd, [rn,] #imm
6113
6114 Rewrite as:
6115
6116 Preparation: tmp1, tmp2 <- r0, r1;
6117 r0, r1 <- rd, rn
6118 Insn: <op><cond> r0, r1, #imm
6119 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6120 */
6121
36073a92
YQ
6122 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6123 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6124 rn_val = displaced_read_reg (regs, dsc, rn);
6125 rd_val = displaced_read_reg (regs, dsc, rd);
cca44b1b
JB
6126 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6127 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6128 dsc->rd = rd;
6129
6130 if (is_mov)
6131 dsc->modinsn[0] = insn & 0xfff00fff;
6132 else
6133 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
6134
6135 dsc->cleanup = &cleanup_alu_imm;
6136
6137 return 0;
6138}
6139
34518530
YQ
6140static int
6141thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
6142 uint16_t insn2, struct regcache *regs,
6143 struct displaced_step_closure *dsc)
6144{
6145 unsigned int op = bits (insn1, 5, 8);
6146 unsigned int rn, rm, rd;
6147 ULONGEST rd_val, rn_val;
6148
6149 rn = bits (insn1, 0, 3); /* Rn */
6150 rm = bits (insn2, 0, 3); /* Rm */
6151 rd = bits (insn2, 8, 11); /* Rd */
6152
6153 /* This routine is only called for instruction MOV. */
6154 gdb_assert (op == 0x2 && rn == 0xf);
6155
6156 if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
6157 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);
6158
6159 if (debug_displaced)
6160 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.4x%.4x\n",
6161 "ALU", insn1, insn2);
6162
6163 /* Instruction is of form:
6164
6165 <op><cond> rd, [rn,] #imm
6166
6167 Rewrite as:
6168
6169 Preparation: tmp1, tmp2 <- r0, r1;
6170 r0, r1 <- rd, rn
6171 Insn: <op><cond> r0, r1, #imm
6172 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6173 */
6174
6175 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6176 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6177 rn_val = displaced_read_reg (regs, dsc, rn);
6178 rd_val = displaced_read_reg (regs, dsc, rd);
6179 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6180 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6181 dsc->rd = rd;
6182
6183 dsc->modinsn[0] = insn1;
6184 dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
6185 dsc->numinsns = 2;
6186
6187 dsc->cleanup = &cleanup_alu_imm;
6188
6189 return 0;
6190}
6191
cca44b1b
JB
6192/* Copy/cleanup arithmetic/logic insns with register RHS. */
6193
6194static void
6e39997a 6195cleanup_alu_reg (struct gdbarch *gdbarch,
cca44b1b
JB
6196 struct regcache *regs, struct displaced_step_closure *dsc)
6197{
6198 ULONGEST rd_val;
6199 int i;
6200
36073a92 6201 rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6202
6203 for (i = 0; i < 3; i++)
6204 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6205
6206 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6207}
6208
7ff120b4
YQ
6209static void
6210install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
6211 struct displaced_step_closure *dsc,
6212 unsigned int rd, unsigned int rn, unsigned int rm)
cca44b1b 6213{
cca44b1b 6214 ULONGEST rd_val, rn_val, rm_val;
cca44b1b 6215
cca44b1b
JB
6216 /* Instruction is of form:
6217
6218 <op><cond> rd, [rn,] rm [, <shift>]
6219
6220 Rewrite as:
6221
6222 Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
6223 r0, r1, r2 <- rd, rn, rm
6224 Insn: <op><cond> r0, r1, r2 [, <shift>]
6225 Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
6226 */
6227
36073a92
YQ
6228 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6229 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6230 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6231 rd_val = displaced_read_reg (regs, dsc, rd);
6232 rn_val = displaced_read_reg (regs, dsc, rn);
6233 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6234 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6235 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6236 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6237 dsc->rd = rd;
6238
7ff120b4
YQ
6239 dsc->cleanup = &cleanup_alu_reg;
6240}
6241
6242static int
6243arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
6244 struct displaced_step_closure *dsc)
6245{
6246 unsigned int op = bits (insn, 21, 24);
6247 int is_mov = (op == 0xd);
6248
6249 if (!insn_references_pc (insn, 0x000ff00ful))
6250 return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);
6251
6252 if (debug_displaced)
6253 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.8lx\n",
6254 is_mov ? "move" : "ALU", (unsigned long) insn);
6255
cca44b1b
JB
6256 if (is_mov)
6257 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
6258 else
6259 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
6260
7ff120b4
YQ
6261 install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
6262 bits (insn, 0, 3));
cca44b1b
JB
6263 return 0;
6264}
6265
34518530
YQ
6266static int
6267thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
6268 struct regcache *regs,
6269 struct displaced_step_closure *dsc)
6270{
6271 unsigned rn, rm, rd;
6272
6273 rd = bits (insn, 3, 6);
6274 rn = (bit (insn, 7) << 3) | bits (insn, 0, 2);
6275 rm = 2;
6276
6277 if (rd != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
6278 return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);
6279
6280 if (debug_displaced)
6281 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.4x\n",
6282 "ALU", (unsigned short) insn);
6283
6284 dsc->modinsn[0] = ((insn & 0xff00) | 0x08);
6285
6286 install_alu_reg (gdbarch, regs, dsc, rd, rn, rm);
6287
6288 return 0;
6289}
6290
cca44b1b
JB
6291/* Cleanup/copy arithmetic/logic insns with shifted register RHS. */
6292
6293static void
6e39997a 6294cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
cca44b1b
JB
6295 struct regcache *regs,
6296 struct displaced_step_closure *dsc)
6297{
36073a92 6298 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6299 int i;
6300
6301 for (i = 0; i < 4; i++)
6302 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6303
6304 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6305}
6306
7ff120b4
YQ
6307static void
6308install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
6309 struct displaced_step_closure *dsc,
6310 unsigned int rd, unsigned int rn, unsigned int rm,
6311 unsigned rs)
cca44b1b 6312{
7ff120b4 6313 int i;
cca44b1b 6314 ULONGEST rd_val, rn_val, rm_val, rs_val;
cca44b1b 6315
cca44b1b
JB
6316 /* Instruction is of form:
6317
6318 <op><cond> rd, [rn,] rm, <shift> rs
6319
6320 Rewrite as:
6321
6322 Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
6323 r0, r1, r2, r3 <- rd, rn, rm, rs
6324 Insn: <op><cond> r0, r1, r2, <shift> r3
6325 Cleanup: tmp5 <- r0
6326 r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
6327 rd <- tmp5
6328 */
6329
6330 for (i = 0; i < 4; i++)
36073a92 6331 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b 6332
36073a92
YQ
6333 rd_val = displaced_read_reg (regs, dsc, rd);
6334 rn_val = displaced_read_reg (regs, dsc, rn);
6335 rm_val = displaced_read_reg (regs, dsc, rm);
6336 rs_val = displaced_read_reg (regs, dsc, rs);
cca44b1b
JB
6337 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6338 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6339 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6340 displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
6341 dsc->rd = rd;
7ff120b4
YQ
6342 dsc->cleanup = &cleanup_alu_shifted_reg;
6343}
6344
6345static int
6346arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
6347 struct regcache *regs,
6348 struct displaced_step_closure *dsc)
6349{
6350 unsigned int op = bits (insn, 21, 24);
6351 int is_mov = (op == 0xd);
6352 unsigned int rd, rn, rm, rs;
6353
6354 if (!insn_references_pc (insn, 0x000fff0ful))
6355 return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
6356
6357 if (debug_displaced)
6358 fprintf_unfiltered (gdb_stdlog, "displaced: copying shifted reg %s insn "
6359 "%.8lx\n", is_mov ? "move" : "ALU",
6360 (unsigned long) insn);
6361
6362 rn = bits (insn, 16, 19);
6363 rm = bits (insn, 0, 3);
6364 rs = bits (insn, 8, 11);
6365 rd = bits (insn, 12, 15);
cca44b1b
JB
6366
6367 if (is_mov)
6368 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
6369 else
6370 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
6371
7ff120b4 6372 install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);
cca44b1b
JB
6373
6374 return 0;
6375}
6376
6377/* Clean up load instructions. */
6378
6379static void
6e39997a 6380cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
6381 struct displaced_step_closure *dsc)
6382{
6383 ULONGEST rt_val, rt_val2 = 0, rn_val;
cca44b1b 6384
36073a92 6385 rt_val = displaced_read_reg (regs, dsc, 0);
cca44b1b 6386 if (dsc->u.ldst.xfersize == 8)
36073a92
YQ
6387 rt_val2 = displaced_read_reg (regs, dsc, 1);
6388 rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
6389
6390 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6391 if (dsc->u.ldst.xfersize > 4)
6392 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6393 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6394 if (!dsc->u.ldst.immed)
6395 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6396
6397 /* Handle register writeback. */
6398 if (dsc->u.ldst.writeback)
6399 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6400 /* Put result in right place. */
6401 displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
6402 if (dsc->u.ldst.xfersize == 8)
6403 displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
6404}
6405
6406/* Clean up store instructions. */
6407
6408static void
6e39997a 6409cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
cca44b1b
JB
6410 struct displaced_step_closure *dsc)
6411{
36073a92 6412 ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
6413
6414 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6415 if (dsc->u.ldst.xfersize > 4)
6416 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6417 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6418 if (!dsc->u.ldst.immed)
6419 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6420 if (!dsc->u.ldst.restore_r4)
6421 displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
6422
6423 /* Writeback. */
6424 if (dsc->u.ldst.writeback)
6425 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6426}
6427
6428/* Copy "extra" load/store instructions. These are halfword/doubleword
6429 transfers, which have a different encoding to byte/word transfers. */
6430
6431static int
7ff120b4
YQ
6432arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unpriveleged,
6433 struct regcache *regs, struct displaced_step_closure *dsc)
cca44b1b
JB
6434{
6435 unsigned int op1 = bits (insn, 20, 24);
6436 unsigned int op2 = bits (insn, 5, 6);
6437 unsigned int rt = bits (insn, 12, 15);
6438 unsigned int rn = bits (insn, 16, 19);
6439 unsigned int rm = bits (insn, 0, 3);
6440 char load[12] = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
6441 char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
6442 int immed = (op1 & 0x4) != 0;
6443 int opcode;
6444 ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
cca44b1b
JB
6445
6446 if (!insn_references_pc (insn, 0x000ff00ful))
7ff120b4 6447 return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);
cca44b1b
JB
6448
6449 if (debug_displaced)
6450 fprintf_unfiltered (gdb_stdlog, "displaced: copying %sextra load/store "
6451 "insn %.8lx\n", unpriveleged ? "unpriveleged " : "",
6452 (unsigned long) insn);
6453
6454 opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
6455
6456 if (opcode < 0)
6457 internal_error (__FILE__, __LINE__,
6458 _("copy_extra_ld_st: instruction decode error"));
6459
36073a92
YQ
6460 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6461 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6462 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 6463 if (!immed)
36073a92 6464 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 6465
36073a92 6466 rt_val = displaced_read_reg (regs, dsc, rt);
cca44b1b 6467 if (bytesize[opcode] == 8)
36073a92
YQ
6468 rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
6469 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 6470 if (!immed)
36073a92 6471 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6472
6473 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6474 if (bytesize[opcode] == 8)
6475 displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
6476 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6477 if (!immed)
6478 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
6479
6480 dsc->rd = rt;
6481 dsc->u.ldst.xfersize = bytesize[opcode];
6482 dsc->u.ldst.rn = rn;
6483 dsc->u.ldst.immed = immed;
6484 dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
6485 dsc->u.ldst.restore_r4 = 0;
6486
6487 if (immed)
6488 /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
6489 ->
6490 {ldr,str}<width><cond> r0, [r1,] [r2, #imm]. */
6491 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6492 else
6493 /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
6494 ->
6495 {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3]. */
6496 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6497
6498 dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
6499
6500 return 0;
6501}
6502
0f6f04ba 6503/* Copy byte/half word/word loads and stores. */
cca44b1b 6504
7ff120b4 6505static void
0f6f04ba
YQ
6506install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
6507 struct displaced_step_closure *dsc, int load,
6508 int immed, int writeback, int size, int usermode,
6509 int rt, int rm, int rn)
cca44b1b 6510{
cca44b1b 6511 ULONGEST rt_val, rn_val, rm_val = 0;
cca44b1b 6512
36073a92
YQ
6513 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6514 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 6515 if (!immed)
36073a92 6516 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 6517 if (!load)
36073a92 6518 dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);
cca44b1b 6519
36073a92
YQ
6520 rt_val = displaced_read_reg (regs, dsc, rt);
6521 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 6522 if (!immed)
36073a92 6523 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6524
6525 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6526 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6527 if (!immed)
6528 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
cca44b1b 6529 dsc->rd = rt;
0f6f04ba 6530 dsc->u.ldst.xfersize = size;
cca44b1b
JB
6531 dsc->u.ldst.rn = rn;
6532 dsc->u.ldst.immed = immed;
7ff120b4 6533 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
6534
6535 /* To write PC we can do:
6536
494e194e
YQ
6537 Before this sequence of instructions:
6538 r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
6539 r2 is the Rn value got from dispalced_read_reg.
6540
6541 Insn1: push {pc} Write address of STR instruction + offset on stack
6542 Insn2: pop {r4} Read it back from stack, r4 = addr(Insn1) + offset
6543 Insn3: sub r4, r4, pc r4 = addr(Insn1) + offset - pc
6544 = addr(Insn1) + offset - addr(Insn3) - 8
6545 = offset - 16
6546 Insn4: add r4, r4, #8 r4 = offset - 8
6547 Insn5: add r0, r0, r4 r0 = from + 8 + offset - 8
6548 = from + offset
6549 Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])
cca44b1b
JB
6550
6551 Otherwise we don't know what value to write for PC, since the offset is
494e194e
YQ
6552 architecture-dependent (sometimes PC+8, sometimes PC+12). More details
6553 of this can be found in Section "Saving from r15" in
6554 http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */
cca44b1b 6555
7ff120b4
YQ
6556 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6557}
6558
34518530
YQ
6559
6560static int
6561thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
6562 uint16_t insn2, struct regcache *regs,
6563 struct displaced_step_closure *dsc, int size)
6564{
6565 unsigned int u_bit = bit (insn1, 7);
6566 unsigned int rt = bits (insn2, 12, 15);
6567 int imm12 = bits (insn2, 0, 11);
6568 ULONGEST pc_val;
6569
6570 if (debug_displaced)
6571 fprintf_unfiltered (gdb_stdlog,
6572 "displaced: copying ldr pc (0x%x) R%d %c imm12 %.4x\n",
6573 (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
6574 imm12);
6575
6576 if (!u_bit)
6577 imm12 = -1 * imm12;
6578
6579 /* Rewrite instruction LDR Rt imm12 into:
6580
6581 Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12
6582
6583 LDR R0, R2, R3,
6584
6585 Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2]. */
6586
6587
6588 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6589 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6590 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6591
6592 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6593
6594 pc_val = pc_val & 0xfffffffc;
6595
6596 displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
6597 displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);
6598
6599 dsc->rd = rt;
6600
6601 dsc->u.ldst.xfersize = size;
6602 dsc->u.ldst.immed = 0;
6603 dsc->u.ldst.writeback = 0;
6604 dsc->u.ldst.restore_r4 = 0;
6605
6606 /* LDR R0, R2, R3 */
6607 dsc->modinsn[0] = 0xf852;
6608 dsc->modinsn[1] = 0x3;
6609 dsc->numinsns = 2;
6610
6611 dsc->cleanup = &cleanup_load;
6612
6613 return 0;
6614}
6615
6616static int
6617thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
6618 uint16_t insn2, struct regcache *regs,
6619 struct displaced_step_closure *dsc,
6620 int writeback, int immed)
6621{
6622 unsigned int rt = bits (insn2, 12, 15);
6623 unsigned int rn = bits (insn1, 0, 3);
6624 unsigned int rm = bits (insn2, 0, 3); /* Only valid if !immed. */
6625 /* In LDR (register), there is also a register Rm, which is not allowed to
6626 be PC, so we don't have to check it. */
6627
6628 if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
6629 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
6630 dsc);
6631
6632 if (debug_displaced)
6633 fprintf_unfiltered (gdb_stdlog,
6634 "displaced: copying ldr r%d [r%d] insn %.4x%.4x\n",
6635 rt, rn, insn1, insn2);
6636
6637 install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
6638 0, rt, rm, rn);
6639
6640 dsc->u.ldst.restore_r4 = 0;
6641
6642 if (immed)
6643 /* ldr[b]<cond> rt, [rn, #imm], etc.
6644 ->
6645 ldr[b]<cond> r0, [r2, #imm]. */
6646 {
6647 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6648 dsc->modinsn[1] = insn2 & 0x0fff;
6649 }
6650 else
6651 /* ldr[b]<cond> rt, [rn, rm], etc.
6652 ->
6653 ldr[b]<cond> r0, [r2, r3]. */
6654 {
6655 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6656 dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
6657 }
6658
6659 dsc->numinsns = 2;
6660
6661 return 0;
6662}
6663
6664
7ff120b4
YQ
6665static int
6666arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
6667 struct regcache *regs,
6668 struct displaced_step_closure *dsc,
0f6f04ba 6669 int load, int size, int usermode)
7ff120b4
YQ
6670{
6671 int immed = !bit (insn, 25);
6672 int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
6673 unsigned int rt = bits (insn, 12, 15);
6674 unsigned int rn = bits (insn, 16, 19);
6675 unsigned int rm = bits (insn, 0, 3); /* Only valid if !immed. */
6676
6677 if (!insn_references_pc (insn, 0x000ff00ful))
6678 return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);
6679
6680 if (debug_displaced)
6681 fprintf_unfiltered (gdb_stdlog,
6682 "displaced: copying %s%s r%d [r%d] insn %.8lx\n",
0f6f04ba
YQ
6683 load ? (size == 1 ? "ldrb" : "ldr")
6684 : (size == 1 ? "strb" : "str"), usermode ? "t" : "",
7ff120b4
YQ
6685 rt, rn,
6686 (unsigned long) insn);
6687
0f6f04ba
YQ
6688 install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
6689 usermode, rt, rm, rn);
7ff120b4 6690
bf9f652a 6691 if (load || rt != ARM_PC_REGNUM)
cca44b1b
JB
6692 {
6693 dsc->u.ldst.restore_r4 = 0;
6694
6695 if (immed)
6696 /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
6697 ->
6698 {ldr,str}[b]<cond> r0, [r2, #imm]. */
6699 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6700 else
6701 /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
6702 ->
6703 {ldr,str}[b]<cond> r0, [r2, r3]. */
6704 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6705 }
6706 else
6707 {
6708 /* We need to use r4 as scratch. Make sure it's restored afterwards. */
6709 dsc->u.ldst.restore_r4 = 1;
494e194e
YQ
6710 dsc->modinsn[0] = 0xe92d8000; /* push {pc} */
6711 dsc->modinsn[1] = 0xe8bd0010; /* pop {r4} */
cca44b1b
JB
6712 dsc->modinsn[2] = 0xe044400f; /* sub r4, r4, pc. */
6713 dsc->modinsn[3] = 0xe2844008; /* add r4, r4, #8. */
6714 dsc->modinsn[4] = 0xe0800004; /* add r0, r0, r4. */
6715
6716 /* As above. */
6717 if (immed)
6718 dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
6719 else
6720 dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
6721
cca44b1b
JB
6722 dsc->numinsns = 6;
6723 }
6724
6725 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6726
6727 return 0;
6728}
6729
6730/* Cleanup LDM instructions with fully-populated register list. This is an
6731 unfortunate corner case: it's impossible to implement correctly by modifying
6732 the instruction. The issue is as follows: we have an instruction,
6733
6734 ldm rN, {r0-r15}
6735
6736 which we must rewrite to avoid loading PC. A possible solution would be to
6737 do the load in two halves, something like (with suitable cleanup
6738 afterwards):
6739
6740 mov r8, rN
6741 ldm[id][ab] r8!, {r0-r7}
6742 str r7, <temp>
6743 ldm[id][ab] r8, {r7-r14}
6744 <bkpt>
6745
6746 but at present there's no suitable place for <temp>, since the scratch space
6747 is overwritten before the cleanup routine is called. For now, we simply
6748 emulate the instruction. */
6749
6750static void
6751cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
6752 struct displaced_step_closure *dsc)
6753{
cca44b1b
JB
6754 int inc = dsc->u.block.increment;
6755 int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
6756 int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
6757 uint32_t regmask = dsc->u.block.regmask;
6758 int regno = inc ? 0 : 15;
6759 CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
6760 int exception_return = dsc->u.block.load && dsc->u.block.user
6761 && (regmask & 0x8000) != 0;
36073a92 6762 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
6763 int do_transfer = condition_true (dsc->u.block.cond, status);
6764 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6765
6766 if (!do_transfer)
6767 return;
6768
6769 /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
6770 sensible we can do here. Complain loudly. */
6771 if (exception_return)
6772 error (_("Cannot single-step exception return"));
6773
6774 /* We don't handle any stores here for now. */
6775 gdb_assert (dsc->u.block.load != 0);
6776
6777 if (debug_displaced)
6778 fprintf_unfiltered (gdb_stdlog, "displaced: emulating block transfer: "
6779 "%s %s %s\n", dsc->u.block.load ? "ldm" : "stm",
6780 dsc->u.block.increment ? "inc" : "dec",
6781 dsc->u.block.before ? "before" : "after");
6782
6783 while (regmask)
6784 {
6785 uint32_t memword;
6786
6787 if (inc)
bf9f652a 6788 while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
cca44b1b
JB
6789 regno++;
6790 else
6791 while (regno >= 0 && (regmask & (1 << regno)) == 0)
6792 regno--;
6793
6794 xfer_addr += bump_before;
6795
6796 memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
6797 displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
6798
6799 xfer_addr += bump_after;
6800
6801 regmask &= ~(1 << regno);
6802 }
6803
6804 if (dsc->u.block.writeback)
6805 displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
6806 CANNOT_WRITE_PC);
6807}
6808
6809/* Clean up an STM which included the PC in the register list. */
6810
6811static void
6812cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
6813 struct displaced_step_closure *dsc)
6814{
36073a92 6815 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
6816 int store_executed = condition_true (dsc->u.block.cond, status);
6817 CORE_ADDR pc_stored_at, transferred_regs = bitcount (dsc->u.block.regmask);
6818 CORE_ADDR stm_insn_addr;
6819 uint32_t pc_val;
6820 long offset;
6821 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6822
6823 /* If condition code fails, there's nothing else to do. */
6824 if (!store_executed)
6825 return;
6826
6827 if (dsc->u.block.increment)
6828 {
6829 pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
6830
6831 if (dsc->u.block.before)
6832 pc_stored_at += 4;
6833 }
6834 else
6835 {
6836 pc_stored_at = dsc->u.block.xfer_addr;
6837
6838 if (dsc->u.block.before)
6839 pc_stored_at -= 4;
6840 }
6841
6842 pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
6843 stm_insn_addr = dsc->scratch_base;
6844 offset = pc_val - stm_insn_addr;
6845
6846 if (debug_displaced)
6847 fprintf_unfiltered (gdb_stdlog, "displaced: detected PC offset %.8lx for "
6848 "STM instruction\n", offset);
6849
6850 /* Rewrite the stored PC to the proper value for the non-displaced original
6851 instruction. */
6852 write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
6853 dsc->insn_addr + offset);
6854}
6855
6856/* Clean up an LDM which includes the PC in the register list. We clumped all
6857 the registers in the transferred list into a contiguous range r0...rX (to
6858 avoid loading PC directly and losing control of the debugged program), so we
6859 must undo that here. */
6860
6861static void
6e39997a 6862cleanup_block_load_pc (struct gdbarch *gdbarch,
cca44b1b
JB
6863 struct regcache *regs,
6864 struct displaced_step_closure *dsc)
6865{
36073a92 6866 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
22e048c9 6867 int load_executed = condition_true (dsc->u.block.cond, status);
bf9f652a 6868 unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
cca44b1b
JB
6869 unsigned int regs_loaded = bitcount (mask);
6870 unsigned int num_to_shuffle = regs_loaded, clobbered;
6871
6872 /* The method employed here will fail if the register list is fully populated
6873 (we need to avoid loading PC directly). */
6874 gdb_assert (num_to_shuffle < 16);
6875
6876 if (!load_executed)
6877 return;
6878
6879 clobbered = (1 << num_to_shuffle) - 1;
6880
6881 while (num_to_shuffle > 0)
6882 {
6883 if ((mask & (1 << write_reg)) != 0)
6884 {
6885 unsigned int read_reg = num_to_shuffle - 1;
6886
6887 if (read_reg != write_reg)
6888 {
36073a92 6889 ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
cca44b1b
JB
6890 displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
6891 if (debug_displaced)
6892 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: move "
6893 "loaded register r%d to r%d\n"), read_reg,
6894 write_reg);
6895 }
6896 else if (debug_displaced)
6897 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: register "
6898 "r%d already in the right place\n"),
6899 write_reg);
6900
6901 clobbered &= ~(1 << write_reg);
6902
6903 num_to_shuffle--;
6904 }
6905
6906 write_reg--;
6907 }
6908
6909 /* Restore any registers we scribbled over. */
6910 for (write_reg = 0; clobbered != 0; write_reg++)
6911 {
6912 if ((clobbered & (1 << write_reg)) != 0)
6913 {
6914 displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
6915 CANNOT_WRITE_PC);
6916 if (debug_displaced)
6917 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: restored "
6918 "clobbered register r%d\n"), write_reg);
6919 clobbered &= ~(1 << write_reg);
6920 }
6921 }
6922
6923 /* Perform register writeback manually. */
6924 if (dsc->u.block.writeback)
6925 {
6926 ULONGEST new_rn_val = dsc->u.block.xfer_addr;
6927
6928 if (dsc->u.block.increment)
6929 new_rn_val += regs_loaded * 4;
6930 else
6931 new_rn_val -= regs_loaded * 4;
6932
6933 displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
6934 CANNOT_WRITE_PC);
6935 }
6936}
6937
6938/* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
6939 in user-level code (in particular exception return, ldm rn, {...pc}^). */
6940
6941static int
7ff120b4
YQ
6942arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
6943 struct regcache *regs,
6944 struct displaced_step_closure *dsc)
cca44b1b
JB
6945{
6946 int load = bit (insn, 20);
6947 int user = bit (insn, 22);
6948 int increment = bit (insn, 23);
6949 int before = bit (insn, 24);
6950 int writeback = bit (insn, 21);
6951 int rn = bits (insn, 16, 19);
cca44b1b 6952
0963b4bd
MS
6953 /* Block transfers which don't mention PC can be run directly
6954 out-of-line. */
bf9f652a 6955 if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
7ff120b4 6956 return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
cca44b1b 6957
bf9f652a 6958 if (rn == ARM_PC_REGNUM)
cca44b1b 6959 {
0963b4bd
MS
6960 warning (_("displaced: Unpredictable LDM or STM with "
6961 "base register r15"));
7ff120b4 6962 return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
cca44b1b
JB
6963 }
6964
6965 if (debug_displaced)
6966 fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
6967 "%.8lx\n", (unsigned long) insn);
6968
36073a92 6969 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b
JB
6970 dsc->u.block.rn = rn;
6971
6972 dsc->u.block.load = load;
6973 dsc->u.block.user = user;
6974 dsc->u.block.increment = increment;
6975 dsc->u.block.before = before;
6976 dsc->u.block.writeback = writeback;
6977 dsc->u.block.cond = bits (insn, 28, 31);
6978
6979 dsc->u.block.regmask = insn & 0xffff;
6980
6981 if (load)
6982 {
6983 if ((insn & 0xffff) == 0xffff)
6984 {
6985 /* LDM with a fully-populated register list. This case is
6986 particularly tricky. Implement for now by fully emulating the
6987 instruction (which might not behave perfectly in all cases, but
6988 these instructions should be rare enough for that not to matter
6989 too much). */
6990 dsc->modinsn[0] = ARM_NOP;
6991
6992 dsc->cleanup = &cleanup_block_load_all;
6993 }
6994 else
6995 {
6996 /* LDM of a list of registers which includes PC. Implement by
6997 rewriting the list of registers to be transferred into a
6998 contiguous chunk r0...rX before doing the transfer, then shuffling
6999 registers into the correct places in the cleanup routine. */
7000 unsigned int regmask = insn & 0xffff;
7001 unsigned int num_in_list = bitcount (regmask), new_regmask, bit = 1;
7002 unsigned int to = 0, from = 0, i, new_rn;
7003
7004 for (i = 0; i < num_in_list; i++)
36073a92 7005 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b
JB
7006
7007 /* Writeback makes things complicated. We need to avoid clobbering
7008 the base register with one of the registers in our modified
7009 register list, but just using a different register can't work in
7010 all cases, e.g.:
7011
7012 ldm r14!, {r0-r13,pc}
7013
7014 which would need to be rewritten as:
7015
7016 ldm rN!, {r0-r14}
7017
7018 but that can't work, because there's no free register for N.
7019
7020 Solve this by turning off the writeback bit, and emulating
7021 writeback manually in the cleanup routine. */
7022
7023 if (writeback)
7024 insn &= ~(1 << 21);
7025
7026 new_regmask = (1 << num_in_list) - 1;
7027
7028 if (debug_displaced)
7029 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
7030 "{..., pc}: original reg list %.4x, modified "
7031 "list %.4x\n"), rn, writeback ? "!" : "",
7032 (int) insn & 0xffff, new_regmask);
7033
7034 dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
7035
7036 dsc->cleanup = &cleanup_block_load_pc;
7037 }
7038 }
7039 else
7040 {
7041 /* STM of a list of registers which includes PC. Run the instruction
7042 as-is, but out of line: this will store the wrong value for the PC,
7043 so we must manually fix up the memory in the cleanup routine.
7044 Doing things this way has the advantage that we can auto-detect
7045 the offset of the PC write (which is architecture-dependent) in
7046 the cleanup routine. */
7047 dsc->modinsn[0] = insn;
7048
7049 dsc->cleanup = &cleanup_block_store_pc;
7050 }
7051
7052 return 0;
7053}
7054
34518530
YQ
7055static int
7056thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
7057 struct regcache *regs,
7058 struct displaced_step_closure *dsc)
cca44b1b 7059{
34518530
YQ
7060 int rn = bits (insn1, 0, 3);
7061 int load = bit (insn1, 4);
7062 int writeback = bit (insn1, 5);
cca44b1b 7063
34518530
YQ
7064 /* Block transfers which don't mention PC can be run directly
7065 out-of-line. */
7066 if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
7067 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);
7ff120b4 7068
34518530
YQ
7069 if (rn == ARM_PC_REGNUM)
7070 {
7071 warning (_("displaced: Unpredictable LDM or STM with "
7072 "base register r15"));
7073 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7074 "unpredictable ldm/stm", dsc);
7075 }
cca44b1b
JB
7076
7077 if (debug_displaced)
34518530
YQ
7078 fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
7079 "%.4x%.4x\n", insn1, insn2);
cca44b1b 7080
34518530
YQ
7081 /* Clear bit 13, since it should be always zero. */
7082 dsc->u.block.regmask = (insn2 & 0xdfff);
7083 dsc->u.block.rn = rn;
cca44b1b 7084
34518530
YQ
7085 dsc->u.block.load = load;
7086 dsc->u.block.user = 0;
7087 dsc->u.block.increment = bit (insn1, 7);
7088 dsc->u.block.before = bit (insn1, 8);
7089 dsc->u.block.writeback = writeback;
7090 dsc->u.block.cond = INST_AL;
7091 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b 7092
34518530
YQ
7093 if (load)
7094 {
7095 if (dsc->u.block.regmask == 0xffff)
7096 {
7097 /* This branch is impossible to happen. */
7098 gdb_assert (0);
7099 }
7100 else
7101 {
7102 unsigned int regmask = dsc->u.block.regmask;
7103 unsigned int num_in_list = bitcount (regmask), new_regmask, bit = 1;
7104 unsigned int to = 0, from = 0, i, new_rn;
7105
7106 for (i = 0; i < num_in_list; i++)
7107 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7108
7109 if (writeback)
7110 insn1 &= ~(1 << 5);
7111
7112 new_regmask = (1 << num_in_list) - 1;
7113
7114 if (debug_displaced)
7115 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
7116 "{..., pc}: original reg list %.4x, modified "
7117 "list %.4x\n"), rn, writeback ? "!" : "",
7118 (int) dsc->u.block.regmask, new_regmask);
7119
7120 dsc->modinsn[0] = insn1;
7121 dsc->modinsn[1] = (new_regmask & 0xffff);
7122 dsc->numinsns = 2;
7123
7124 dsc->cleanup = &cleanup_block_load_pc;
7125 }
7126 }
7127 else
7128 {
7129 dsc->modinsn[0] = insn1;
7130 dsc->modinsn[1] = insn2;
7131 dsc->numinsns = 2;
7132 dsc->cleanup = &cleanup_block_store_pc;
7133 }
7134 return 0;
7135}
7136
7137/* Cleanup/copy SVC (SWI) instructions. These two functions are overridden
7138 for Linux, where some SVC instructions must be treated specially. */
7139
7140static void
7141cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
7142 struct displaced_step_closure *dsc)
7143{
7144 CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
7145
7146 if (debug_displaced)
7147 fprintf_unfiltered (gdb_stdlog, "displaced: cleanup for svc, resume at "
7148 "%.8lx\n", (unsigned long) resume_addr);
7149
7150 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
7151}
7152
7153
7154/* Common copy routine for svc instruciton. */
7155
7156static int
7157install_svc (struct gdbarch *gdbarch, struct regcache *regs,
7158 struct displaced_step_closure *dsc)
7159{
7160 /* Preparation: none.
7161 Insn: unmodified svc.
7162 Cleanup: pc <- insn_addr + insn_size. */
7163
7164 /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
7165 instruction. */
7166 dsc->wrote_to_pc = 1;
7167
7168 /* Allow OS-specific code to override SVC handling. */
bd18283a
YQ
7169 if (dsc->u.svc.copy_svc_os)
7170 return dsc->u.svc.copy_svc_os (gdbarch, regs, dsc);
7171 else
7172 {
7173 dsc->cleanup = &cleanup_svc;
7174 return 0;
7175 }
34518530
YQ
7176}
7177
7178static int
7179arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
7180 struct regcache *regs, struct displaced_step_closure *dsc)
7181{
7182
7183 if (debug_displaced)
7184 fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.8lx\n",
7185 (unsigned long) insn);
7186
7187 dsc->modinsn[0] = insn;
7188
7189 return install_svc (gdbarch, regs, dsc);
7190}
7191
7192static int
7193thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
7194 struct regcache *regs, struct displaced_step_closure *dsc)
7195{
7196
7197 if (debug_displaced)
7198 fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.4x\n",
7199 insn);
bd18283a 7200
34518530
YQ
7201 dsc->modinsn[0] = insn;
7202
7203 return install_svc (gdbarch, regs, dsc);
cca44b1b
JB
7204}
7205
7206/* Copy undefined instructions. */
7207
7208static int
7ff120b4
YQ
7209arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
7210 struct displaced_step_closure *dsc)
cca44b1b
JB
7211{
7212 if (debug_displaced)
0963b4bd
MS
7213 fprintf_unfiltered (gdb_stdlog,
7214 "displaced: copying undefined insn %.8lx\n",
cca44b1b
JB
7215 (unsigned long) insn);
7216
7217 dsc->modinsn[0] = insn;
7218
7219 return 0;
7220}
7221
34518530
YQ
7222static int
7223thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
7224 struct displaced_step_closure *dsc)
7225{
7226
7227 if (debug_displaced)
7228 fprintf_unfiltered (gdb_stdlog, "displaced: copying undefined insn "
7229 "%.4x %.4x\n", (unsigned short) insn1,
7230 (unsigned short) insn2);
7231
7232 dsc->modinsn[0] = insn1;
7233 dsc->modinsn[1] = insn2;
7234 dsc->numinsns = 2;
7235
7236 return 0;
7237}
7238
cca44b1b
JB
7239/* Copy unpredictable instructions. */
7240
7241static int
7ff120b4
YQ
7242arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
7243 struct displaced_step_closure *dsc)
cca44b1b
JB
7244{
7245 if (debug_displaced)
7246 fprintf_unfiltered (gdb_stdlog, "displaced: copying unpredictable insn "
7247 "%.8lx\n", (unsigned long) insn);
7248
7249 dsc->modinsn[0] = insn;
7250
7251 return 0;
7252}
7253
7254/* The decode_* functions are instruction decoding helpers. They mostly follow
7255 the presentation in the ARM ARM. */
7256
7257static int
7ff120b4
YQ
7258arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
7259 struct regcache *regs,
7260 struct displaced_step_closure *dsc)
cca44b1b
JB
7261{
7262 unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
7263 unsigned int rn = bits (insn, 16, 19);
7264
7265 if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0xe) == 0x0)
7ff120b4 7266 return arm_copy_unmodified (gdbarch, insn, "cps", dsc);
cca44b1b 7267 else if (op1 == 0x10 && op2 == 0x0 && (rn & 0xe) == 0x1)
7ff120b4 7268 return arm_copy_unmodified (gdbarch, insn, "setend", dsc);
cca44b1b 7269 else if ((op1 & 0x60) == 0x20)
7ff120b4 7270 return arm_copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
cca44b1b 7271 else if ((op1 & 0x71) == 0x40)
7ff120b4
YQ
7272 return arm_copy_unmodified (gdbarch, insn, "neon elt/struct load/store",
7273 dsc);
cca44b1b 7274 else if ((op1 & 0x77) == 0x41)
7ff120b4 7275 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 7276 else if ((op1 & 0x77) == 0x45)
7ff120b4 7277 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pli. */
cca44b1b
JB
7278 else if ((op1 & 0x77) == 0x51)
7279 {
7280 if (rn != 0xf)
7ff120b4 7281 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b 7282 else
7ff120b4 7283 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7284 }
7285 else if ((op1 & 0x77) == 0x55)
7ff120b4 7286 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b
JB
7287 else if (op1 == 0x57)
7288 switch (op2)
7289 {
7ff120b4
YQ
7290 case 0x1: return arm_copy_unmodified (gdbarch, insn, "clrex", dsc);
7291 case 0x4: return arm_copy_unmodified (gdbarch, insn, "dsb", dsc);
7292 case 0x5: return arm_copy_unmodified (gdbarch, insn, "dmb", dsc);
7293 case 0x6: return arm_copy_unmodified (gdbarch, insn, "isb", dsc);
7294 default: return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7295 }
7296 else if ((op1 & 0x63) == 0x43)
7ff120b4 7297 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7298 else if ((op2 & 0x1) == 0x0)
7299 switch (op1 & ~0x80)
7300 {
7301 case 0x61:
7ff120b4 7302 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 7303 case 0x65:
7ff120b4 7304 return arm_copy_preload_reg (gdbarch, insn, regs, dsc); /* pli reg. */
cca44b1b
JB
7305 case 0x71: case 0x75:
7306 /* pld/pldw reg. */
7ff120b4 7307 return arm_copy_preload_reg (gdbarch, insn, regs, dsc);
cca44b1b 7308 case 0x63: case 0x67: case 0x73: case 0x77:
7ff120b4 7309 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b 7310 default:
7ff120b4 7311 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7312 }
7313 else
7ff120b4 7314 return arm_copy_undef (gdbarch, insn, dsc); /* Probably unreachable. */
cca44b1b
JB
7315}
7316
7317static int
7ff120b4
YQ
7318arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
7319 struct regcache *regs,
7320 struct displaced_step_closure *dsc)
cca44b1b
JB
7321{
7322 if (bit (insn, 27) == 0)
7ff120b4 7323 return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
cca44b1b
JB
7324 /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx. */
7325 else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
7326 {
7327 case 0x0: case 0x2:
7ff120b4 7328 return arm_copy_unmodified (gdbarch, insn, "srs", dsc);
cca44b1b
JB
7329
7330 case 0x1: case 0x3:
7ff120b4 7331 return arm_copy_unmodified (gdbarch, insn, "rfe", dsc);
cca44b1b
JB
7332
7333 case 0x4: case 0x5: case 0x6: case 0x7:
7ff120b4 7334 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b
JB
7335
7336 case 0x8:
7337 switch ((insn & 0xe00000) >> 21)
7338 {
7339 case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
7340 /* stc/stc2. */
7ff120b4 7341 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7342
7343 case 0x2:
7ff120b4 7344 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b
JB
7345
7346 default:
7ff120b4 7347 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7348 }
7349
7350 case 0x9:
7351 {
7352 int rn_f = (bits (insn, 16, 19) == 0xf);
7353 switch ((insn & 0xe00000) >> 21)
7354 {
7355 case 0x1: case 0x3:
7356 /* ldc/ldc2 imm (undefined for rn == pc). */
7ff120b4
YQ
7357 return rn_f ? arm_copy_undef (gdbarch, insn, dsc)
7358 : arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7359
7360 case 0x2:
7ff120b4 7361 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
7362
7363 case 0x4: case 0x5: case 0x6: case 0x7:
7364 /* ldc/ldc2 lit (undefined for rn != pc). */
7ff120b4
YQ
7365 return rn_f ? arm_copy_copro_load_store (gdbarch, insn, regs, dsc)
7366 : arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7367
7368 default:
7ff120b4 7369 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7370 }
7371 }
7372
7373 case 0xa:
7ff120b4 7374 return arm_copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
cca44b1b
JB
7375
7376 case 0xb:
7377 if (bits (insn, 16, 19) == 0xf)
7378 /* ldc/ldc2 lit. */
7ff120b4 7379 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 7380 else
7ff120b4 7381 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7382
7383 case 0xc:
7384 if (bit (insn, 4))
7ff120b4 7385 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 7386 else
7ff120b4 7387 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7388
7389 case 0xd:
7390 if (bit (insn, 4))
7ff120b4 7391 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 7392 else
7ff120b4 7393 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7394
7395 default:
7ff120b4 7396 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7397 }
7398}
7399
7400/* Decode miscellaneous instructions in dp/misc encoding space. */
7401
7402static int
7ff120b4
YQ
7403arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
7404 struct regcache *regs,
7405 struct displaced_step_closure *dsc)
cca44b1b
JB
7406{
7407 unsigned int op2 = bits (insn, 4, 6);
7408 unsigned int op = bits (insn, 21, 22);
7409 unsigned int op1 = bits (insn, 16, 19);
7410
7411 switch (op2)
7412 {
7413 case 0x0:
7ff120b4 7414 return arm_copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
cca44b1b
JB
7415
7416 case 0x1:
7417 if (op == 0x1) /* bx. */
7ff120b4 7418 return arm_copy_bx_blx_reg (gdbarch, insn, regs, dsc);
cca44b1b 7419 else if (op == 0x3)
7ff120b4 7420 return arm_copy_unmodified (gdbarch, insn, "clz", dsc);
cca44b1b 7421 else
7ff120b4 7422 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7423
7424 case 0x2:
7425 if (op == 0x1)
7426 /* Not really supported. */
7ff120b4 7427 return arm_copy_unmodified (gdbarch, insn, "bxj", dsc);
cca44b1b 7428 else
7ff120b4 7429 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7430
7431 case 0x3:
7432 if (op == 0x1)
7ff120b4 7433 return arm_copy_bx_blx_reg (gdbarch, insn,
0963b4bd 7434 regs, dsc); /* blx register. */
cca44b1b 7435 else
7ff120b4 7436 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7437
7438 case 0x5:
7ff120b4 7439 return arm_copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
cca44b1b
JB
7440
7441 case 0x7:
7442 if (op == 0x1)
7ff120b4 7443 return arm_copy_unmodified (gdbarch, insn, "bkpt", dsc);
cca44b1b
JB
7444 else if (op == 0x3)
7445 /* Not really supported. */
7ff120b4 7446 return arm_copy_unmodified (gdbarch, insn, "smc", dsc);
cca44b1b
JB
7447
7448 default:
7ff120b4 7449 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7450 }
7451}
7452
7453static int
7ff120b4
YQ
7454arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
7455 struct regcache *regs,
7456 struct displaced_step_closure *dsc)
cca44b1b
JB
7457{
7458 if (bit (insn, 25))
7459 switch (bits (insn, 20, 24))
7460 {
7461 case 0x10:
7ff120b4 7462 return arm_copy_unmodified (gdbarch, insn, "movw", dsc);
cca44b1b
JB
7463
7464 case 0x14:
7ff120b4 7465 return arm_copy_unmodified (gdbarch, insn, "movt", dsc);
cca44b1b
JB
7466
7467 case 0x12: case 0x16:
7ff120b4 7468 return arm_copy_unmodified (gdbarch, insn, "msr imm", dsc);
cca44b1b
JB
7469
7470 default:
7ff120b4 7471 return arm_copy_alu_imm (gdbarch, insn, regs, dsc);
cca44b1b
JB
7472 }
7473 else
7474 {
7475 uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
7476
7477 if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
7ff120b4 7478 return arm_copy_alu_reg (gdbarch, insn, regs, dsc);
cca44b1b 7479 else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
7ff120b4 7480 return arm_copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
cca44b1b 7481 else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
7ff120b4 7482 return arm_decode_miscellaneous (gdbarch, insn, regs, dsc);
cca44b1b 7483 else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
7ff120b4 7484 return arm_copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
cca44b1b 7485 else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
7ff120b4 7486 return arm_copy_unmodified (gdbarch, insn, "mul/mla", dsc);
cca44b1b 7487 else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
7ff120b4 7488 return arm_copy_unmodified (gdbarch, insn, "synch", dsc);
cca44b1b
JB
7489 else if (op2 == 0xb || (op2 & 0xd) == 0xd)
7490 /* 2nd arg means "unpriveleged". */
7ff120b4
YQ
7491 return arm_copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
7492 dsc);
cca44b1b
JB
7493 }
7494
7495 /* Should be unreachable. */
7496 return 1;
7497}
7498
7499static int
7ff120b4
YQ
7500arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
7501 struct regcache *regs,
7502 struct displaced_step_closure *dsc)
cca44b1b
JB
7503{
7504 int a = bit (insn, 25), b = bit (insn, 4);
7505 uint32_t op1 = bits (insn, 20, 24);
7506 int rn_f = bits (insn, 16, 19) == 0xf;
7507
7508 if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
7509 || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
0f6f04ba 7510 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 0);
cca44b1b
JB
7511 else if ((!a && (op1 & 0x17) == 0x02)
7512 || (a && (op1 & 0x17) == 0x02 && !b))
0f6f04ba 7513 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 1);
cca44b1b
JB
7514 else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
7515 || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
0f6f04ba 7516 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 0);
cca44b1b
JB
7517 else if ((!a && (op1 & 0x17) == 0x03)
7518 || (a && (op1 & 0x17) == 0x03 && !b))
0f6f04ba 7519 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 1);
cca44b1b
JB
7520 else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
7521 || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
7ff120b4 7522 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
cca44b1b
JB
7523 else if ((!a && (op1 & 0x17) == 0x06)
7524 || (a && (op1 & 0x17) == 0x06 && !b))
7ff120b4 7525 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
cca44b1b
JB
7526 else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
7527 || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
7ff120b4 7528 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
cca44b1b
JB
7529 else if ((!a && (op1 & 0x17) == 0x07)
7530 || (a && (op1 & 0x17) == 0x07 && !b))
7ff120b4 7531 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
cca44b1b
JB
7532
7533 /* Should be unreachable. */
7534 return 1;
7535}
7536
7537static int
7ff120b4
YQ
7538arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
7539 struct displaced_step_closure *dsc)
cca44b1b
JB
7540{
7541 switch (bits (insn, 20, 24))
7542 {
7543 case 0x00: case 0x01: case 0x02: case 0x03:
7ff120b4 7544 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
cca44b1b
JB
7545
7546 case 0x04: case 0x05: case 0x06: case 0x07:
7ff120b4 7547 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
cca44b1b
JB
7548
7549 case 0x08: case 0x09: case 0x0a: case 0x0b:
7550 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
7ff120b4 7551 return arm_copy_unmodified (gdbarch, insn,
cca44b1b
JB
7552 "decode/pack/unpack/saturate/reverse", dsc);
7553
7554 case 0x18:
7555 if (bits (insn, 5, 7) == 0) /* op2. */
7556 {
7557 if (bits (insn, 12, 15) == 0xf)
7ff120b4 7558 return arm_copy_unmodified (gdbarch, insn, "usad8", dsc);
cca44b1b 7559 else
7ff120b4 7560 return arm_copy_unmodified (gdbarch, insn, "usada8", dsc);
cca44b1b
JB
7561 }
7562 else
7ff120b4 7563 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7564
7565 case 0x1a: case 0x1b:
7566 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 7567 return arm_copy_unmodified (gdbarch, insn, "sbfx", dsc);
cca44b1b 7568 else
7ff120b4 7569 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7570
7571 case 0x1c: case 0x1d:
7572 if (bits (insn, 5, 6) == 0x0) /* op2[1:0]. */
7573 {
7574 if (bits (insn, 0, 3) == 0xf)
7ff120b4 7575 return arm_copy_unmodified (gdbarch, insn, "bfc", dsc);
cca44b1b 7576 else
7ff120b4 7577 return arm_copy_unmodified (gdbarch, insn, "bfi", dsc);
cca44b1b
JB
7578 }
7579 else
7ff120b4 7580 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7581
7582 case 0x1e: case 0x1f:
7583 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 7584 return arm_copy_unmodified (gdbarch, insn, "ubfx", dsc);
cca44b1b 7585 else
7ff120b4 7586 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7587 }
7588
7589 /* Should be unreachable. */
7590 return 1;
7591}
7592
7593static int
7ff120b4
YQ
7594arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, int32_t insn,
7595 struct regcache *regs,
7596 struct displaced_step_closure *dsc)
cca44b1b
JB
7597{
7598 if (bit (insn, 25))
7ff120b4 7599 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b 7600 else
7ff120b4 7601 return arm_copy_block_xfer (gdbarch, insn, regs, dsc);
cca44b1b
JB
7602}
7603
7604static int
7ff120b4
YQ
7605arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
7606 struct regcache *regs,
7607 struct displaced_step_closure *dsc)
cca44b1b
JB
7608{
7609 unsigned int opcode = bits (insn, 20, 24);
7610
7611 switch (opcode)
7612 {
7613 case 0x04: case 0x05: /* VFP/Neon mrrc/mcrr. */
7ff120b4 7614 return arm_copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
cca44b1b
JB
7615
7616 case 0x08: case 0x0a: case 0x0c: case 0x0e:
7617 case 0x12: case 0x16:
7ff120b4 7618 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
cca44b1b
JB
7619
7620 case 0x09: case 0x0b: case 0x0d: case 0x0f:
7621 case 0x13: case 0x17:
7ff120b4 7622 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
cca44b1b
JB
7623
7624 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7625 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7626 /* Note: no writeback for these instructions. Bit 25 will always be
7627 zero though (via caller), so the following works OK. */
7ff120b4 7628 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7629 }
7630
7631 /* Should be unreachable. */
7632 return 1;
7633}
7634
34518530
YQ
7635/* Decode shifted register instructions. */
7636
7637static int
7638thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
7639 uint16_t insn2, struct regcache *regs,
7640 struct displaced_step_closure *dsc)
7641{
7642 /* PC is only allowed to be used in instruction MOV. */
7643
7644 unsigned int op = bits (insn1, 5, 8);
7645 unsigned int rn = bits (insn1, 0, 3);
7646
7647 if (op == 0x2 && rn == 0xf) /* MOV */
7648 return thumb2_copy_alu_imm (gdbarch, insn1, insn2, regs, dsc);
7649 else
7650 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7651 "dp (shift reg)", dsc);
7652}
7653
7654
7655/* Decode extension register load/store. Exactly the same as
7656 arm_decode_ext_reg_ld_st. */
7657
7658static int
7659thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
7660 uint16_t insn2, struct regcache *regs,
7661 struct displaced_step_closure *dsc)
7662{
7663 unsigned int opcode = bits (insn1, 4, 8);
7664
7665 switch (opcode)
7666 {
7667 case 0x04: case 0x05:
7668 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7669 "vfp/neon vmov", dsc);
7670
7671 case 0x08: case 0x0c: /* 01x00 */
7672 case 0x0a: case 0x0e: /* 01x10 */
7673 case 0x12: case 0x16: /* 10x10 */
7674 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7675 "vfp/neon vstm/vpush", dsc);
7676
7677 case 0x09: case 0x0d: /* 01x01 */
7678 case 0x0b: case 0x0f: /* 01x11 */
7679 case 0x13: case 0x17: /* 10x11 */
7680 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7681 "vfp/neon vldm/vpop", dsc);
7682
7683 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7684 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7685 "vstr", dsc);
7686 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7687 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2, regs, dsc);
7688 }
7689
7690 /* Should be unreachable. */
7691 return 1;
7692}
7693
cca44b1b 7694static int
7ff120b4
YQ
7695arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn, CORE_ADDR to,
7696 struct regcache *regs, struct displaced_step_closure *dsc)
cca44b1b
JB
7697{
7698 unsigned int op1 = bits (insn, 20, 25);
7699 int op = bit (insn, 4);
7700 unsigned int coproc = bits (insn, 8, 11);
7701 unsigned int rn = bits (insn, 16, 19);
7702
7703 if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
7ff120b4 7704 return arm_decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
cca44b1b
JB
7705 else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
7706 && (coproc & 0xe) != 0xa)
7707 /* stc/stc2. */
7ff120b4 7708 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7709 else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
7710 && (coproc & 0xe) != 0xa)
7711 /* ldc/ldc2 imm/lit. */
7ff120b4 7712 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 7713 else if ((op1 & 0x3e) == 0x00)
7ff120b4 7714 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b 7715 else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
7ff120b4 7716 return arm_copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
cca44b1b 7717 else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
7ff120b4 7718 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b 7719 else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
7ff120b4 7720 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
7721 else if ((op1 & 0x30) == 0x20 && !op)
7722 {
7723 if ((coproc & 0xe) == 0xa)
7ff120b4 7724 return arm_copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
cca44b1b 7725 else
7ff120b4 7726 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7727 }
7728 else if ((op1 & 0x30) == 0x20 && op)
7ff120b4 7729 return arm_copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
cca44b1b 7730 else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
7ff120b4 7731 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 7732 else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
7ff120b4 7733 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 7734 else if ((op1 & 0x30) == 0x30)
7ff120b4 7735 return arm_copy_svc (gdbarch, insn, regs, dsc);
cca44b1b 7736 else
7ff120b4 7737 return arm_copy_undef (gdbarch, insn, dsc); /* Possibly unreachable. */
cca44b1b
JB
7738}
7739
34518530
YQ
7740static int
7741thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
7742 uint16_t insn2, struct regcache *regs,
7743 struct displaced_step_closure *dsc)
7744{
7745 unsigned int coproc = bits (insn2, 8, 11);
7746 unsigned int op1 = bits (insn1, 4, 9);
7747 unsigned int bit_5_8 = bits (insn1, 5, 8);
7748 unsigned int bit_9 = bit (insn1, 9);
7749 unsigned int bit_4 = bit (insn1, 4);
7750 unsigned int rn = bits (insn1, 0, 3);
7751
7752 if (bit_9 == 0)
7753 {
7754 if (bit_5_8 == 2)
7755 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7756 "neon 64bit xfer/mrrc/mrrc2/mcrr/mcrr2",
7757 dsc);
7758 else if (bit_5_8 == 0) /* UNDEFINED. */
7759 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
7760 else
7761 {
7762 /*coproc is 101x. SIMD/VFP, ext registers load/store. */
7763 if ((coproc & 0xe) == 0xa)
7764 return thumb2_decode_ext_reg_ld_st (gdbarch, insn1, insn2, regs,
7765 dsc);
7766 else /* coproc is not 101x. */
7767 {
7768 if (bit_4 == 0) /* STC/STC2. */
7769 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7770 "stc/stc2", dsc);
7771 else /* LDC/LDC2 {literal, immeidate}. */
7772 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2,
7773 regs, dsc);
7774 }
7775 }
7776 }
7777 else
7778 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "coproc", dsc);
7779
7780 return 0;
7781}
7782
7783static void
7784install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
7785 struct displaced_step_closure *dsc, int rd)
7786{
7787 /* ADR Rd, #imm
7788
7789 Rewrite as:
7790
7791 Preparation: Rd <- PC
7792 Insn: ADD Rd, #imm
7793 Cleanup: Null.
7794 */
7795
7796 /* Rd <- PC */
7797 int val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
7798 displaced_write_reg (regs, dsc, rd, val, CANNOT_WRITE_PC);
7799}
7800
7801static int
7802thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
7803 struct displaced_step_closure *dsc,
7804 int rd, unsigned int imm)
7805{
7806
7807 /* Encoding T2: ADDS Rd, #imm */
7808 dsc->modinsn[0] = (0x3000 | (rd << 8) | imm);
7809
7810 install_pc_relative (gdbarch, regs, dsc, rd);
7811
7812 return 0;
7813}
7814
7815static int
7816thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
7817 struct regcache *regs,
7818 struct displaced_step_closure *dsc)
7819{
7820 unsigned int rd = bits (insn, 8, 10);
7821 unsigned int imm8 = bits (insn, 0, 7);
7822
7823 if (debug_displaced)
7824 fprintf_unfiltered (gdb_stdlog,
7825 "displaced: copying thumb adr r%d, #%d insn %.4x\n",
7826 rd, imm8, insn);
7827
7828 return thumb_copy_pc_relative_16bit (gdbarch, regs, dsc, rd, imm8);
7829}
7830
7831static int
7832thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
7833 uint16_t insn2, struct regcache *regs,
7834 struct displaced_step_closure *dsc)
7835{
7836 unsigned int rd = bits (insn2, 8, 11);
7837 /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
7838 extract raw immediate encoding rather than computing immediate. When
7839 generating ADD or SUB instruction, we can simply perform OR operation to
7840 set immediate into ADD. */
7841 unsigned int imm_3_8 = insn2 & 0x70ff;
7842 unsigned int imm_i = insn1 & 0x0400; /* Clear all bits except bit 10. */
7843
7844 if (debug_displaced)
7845 fprintf_unfiltered (gdb_stdlog,
7846 "displaced: copying thumb adr r%d, #%d:%d insn %.4x%.4x\n",
7847 rd, imm_i, imm_3_8, insn1, insn2);
7848
7849 if (bit (insn1, 7)) /* Encoding T2 */
7850 {
7851 /* Encoding T3: SUB Rd, Rd, #imm */
7852 dsc->modinsn[0] = (0xf1a0 | rd | imm_i);
7853 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
7854 }
7855 else /* Encoding T3 */
7856 {
7857 /* Encoding T3: ADD Rd, Rd, #imm */
7858 dsc->modinsn[0] = (0xf100 | rd | imm_i);
7859 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
7860 }
7861 dsc->numinsns = 2;
7862
7863 install_pc_relative (gdbarch, regs, dsc, rd);
7864
7865 return 0;
7866}
7867
7868static int
7869thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, unsigned short insn1,
7870 struct regcache *regs,
7871 struct displaced_step_closure *dsc)
7872{
7873 unsigned int rt = bits (insn1, 8, 10);
7874 unsigned int pc;
7875 int imm8 = (bits (insn1, 0, 7) << 2);
7876 CORE_ADDR from = dsc->insn_addr;
7877
7878 /* LDR Rd, #imm8
7879
7880 Rwrite as:
7881
7882 Preparation: tmp0 <- R0, tmp2 <- R2, tmp3 <- R3, R2 <- PC, R3 <- #imm8;
7883
7884 Insn: LDR R0, [R2, R3];
7885 Cleanup: R2 <- tmp2, R3 <- tmp3, Rd <- R0, R0 <- tmp0 */
7886
7887 if (debug_displaced)
7888 fprintf_unfiltered (gdb_stdlog,
7889 "displaced: copying thumb ldr r%d [pc #%d]\n"
7890 , rt, imm8);
7891
7892 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
7893 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
7894 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
7895 pc = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
7896 /* The assembler calculates the required value of the offset from the
7897 Align(PC,4) value of this instruction to the label. */
7898 pc = pc & 0xfffffffc;
7899
7900 displaced_write_reg (regs, dsc, 2, pc, CANNOT_WRITE_PC);
7901 displaced_write_reg (regs, dsc, 3, imm8, CANNOT_WRITE_PC);
7902
7903 dsc->rd = rt;
7904 dsc->u.ldst.xfersize = 4;
7905 dsc->u.ldst.rn = 0;
7906 dsc->u.ldst.immed = 0;
7907 dsc->u.ldst.writeback = 0;
7908 dsc->u.ldst.restore_r4 = 0;
7909
7910 dsc->modinsn[0] = 0x58d0; /* ldr r0, [r2, r3]*/
7911
7912 dsc->cleanup = &cleanup_load;
7913
7914 return 0;
7915}
7916
7917/* Copy Thumb cbnz/cbz insruction. */
7918
7919static int
7920thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
7921 struct regcache *regs,
7922 struct displaced_step_closure *dsc)
7923{
7924 int non_zero = bit (insn1, 11);
7925 unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
7926 CORE_ADDR from = dsc->insn_addr;
7927 int rn = bits (insn1, 0, 2);
7928 int rn_val = displaced_read_reg (regs, dsc, rn);
7929
7930 dsc->u.branch.cond = (rn_val && non_zero) || (!rn_val && !non_zero);
7931 /* CBNZ and CBZ do not affect the condition flags. If condition is true,
7932 set it INST_AL, so cleanup_branch will know branch is taken, otherwise,
7933 condition is false, let it be, cleanup_branch will do nothing. */
7934 if (dsc->u.branch.cond)
7935 {
7936 dsc->u.branch.cond = INST_AL;
7937 dsc->u.branch.dest = from + 4 + imm5;
7938 }
7939 else
7940 dsc->u.branch.dest = from + 2;
7941
7942 dsc->u.branch.link = 0;
7943 dsc->u.branch.exchange = 0;
7944
7945 if (debug_displaced)
7946 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s [r%d = 0x%x]"
7947 " insn %.4x to %.8lx\n", non_zero ? "cbnz" : "cbz",
7948 rn, rn_val, insn1, dsc->u.branch.dest);
7949
7950 dsc->modinsn[0] = THUMB_NOP;
7951
7952 dsc->cleanup = &cleanup_branch;
7953 return 0;
7954}
7955
7956/* Copy Table Branch Byte/Halfword */
7957static int
7958thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
7959 uint16_t insn2, struct regcache *regs,
7960 struct displaced_step_closure *dsc)
7961{
7962 ULONGEST rn_val, rm_val;
7963 int is_tbh = bit (insn2, 4);
7964 CORE_ADDR halfwords = 0;
7965 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7966
7967 rn_val = displaced_read_reg (regs, dsc, bits (insn1, 0, 3));
7968 rm_val = displaced_read_reg (regs, dsc, bits (insn2, 0, 3));
7969
7970 if (is_tbh)
7971 {
7972 gdb_byte buf[2];
7973
7974 target_read_memory (rn_val + 2 * rm_val, buf, 2);
7975 halfwords = extract_unsigned_integer (buf, 2, byte_order);
7976 }
7977 else
7978 {
7979 gdb_byte buf[1];
7980
7981 target_read_memory (rn_val + rm_val, buf, 1);
7982 halfwords = extract_unsigned_integer (buf, 1, byte_order);
7983 }
7984
7985 if (debug_displaced)
7986 fprintf_unfiltered (gdb_stdlog, "displaced: %s base 0x%x offset 0x%x"
7987 " offset 0x%x\n", is_tbh ? "tbh" : "tbb",
7988 (unsigned int) rn_val, (unsigned int) rm_val,
7989 (unsigned int) halfwords);
7990
7991 dsc->u.branch.cond = INST_AL;
7992 dsc->u.branch.link = 0;
7993 dsc->u.branch.exchange = 0;
7994 dsc->u.branch.dest = dsc->insn_addr + 4 + 2 * halfwords;
7995
7996 dsc->cleanup = &cleanup_branch;
7997
7998 return 0;
7999}
8000
8001static void
8002cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
8003 struct displaced_step_closure *dsc)
8004{
8005 /* PC <- r7 */
8006 int val = displaced_read_reg (regs, dsc, 7);
8007 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, val, BX_WRITE_PC);
8008
8009 /* r7 <- r8 */
8010 val = displaced_read_reg (regs, dsc, 8);
8011 displaced_write_reg (regs, dsc, 7, val, CANNOT_WRITE_PC);
8012
8013 /* r8 <- tmp[0] */
8014 displaced_write_reg (regs, dsc, 8, dsc->tmp[0], CANNOT_WRITE_PC);
8015
8016}
8017
8018static int
8019thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, unsigned short insn1,
8020 struct regcache *regs,
8021 struct displaced_step_closure *dsc)
8022{
8023 dsc->u.block.regmask = insn1 & 0x00ff;
8024
8025 /* Rewrite instruction: POP {rX, rY, ...,rZ, PC}
8026 to :
8027
8028 (1) register list is full, that is, r0-r7 are used.
8029 Prepare: tmp[0] <- r8
8030
8031 POP {r0, r1, ...., r6, r7}; remove PC from reglist
8032 MOV r8, r7; Move value of r7 to r8;
8033 POP {r7}; Store PC value into r7.
8034
8035 Cleanup: PC <- r7, r7 <- r8, r8 <-tmp[0]
8036
8037 (2) register list is not full, supposing there are N registers in
8038 register list (except PC, 0 <= N <= 7).
8039 Prepare: for each i, 0 - N, tmp[i] <- ri.
8040
8041 POP {r0, r1, ...., rN};
8042
8043 Cleanup: Set registers in original reglist from r0 - rN. Restore r0 - rN
8044 from tmp[] properly.
8045 */
8046 if (debug_displaced)
8047 fprintf_unfiltered (gdb_stdlog,
8048 "displaced: copying thumb pop {%.8x, pc} insn %.4x\n",
8049 dsc->u.block.regmask, insn1);
8050
8051 if (dsc->u.block.regmask == 0xff)
8052 {
8053 dsc->tmp[0] = displaced_read_reg (regs, dsc, 8);
8054
8055 dsc->modinsn[0] = (insn1 & 0xfeff); /* POP {r0,r1,...,r6, r7} */
8056 dsc->modinsn[1] = 0x46b8; /* MOV r8, r7 */
8057 dsc->modinsn[2] = 0xbc80; /* POP {r7} */
8058
8059 dsc->numinsns = 3;
8060 dsc->cleanup = &cleanup_pop_pc_16bit_all;
8061 }
8062 else
8063 {
8064 unsigned int num_in_list = bitcount (dsc->u.block.regmask);
8065 unsigned int new_regmask, bit = 1;
8066 unsigned int to = 0, from = 0, i, new_rn;
8067
8068 for (i = 0; i < num_in_list + 1; i++)
8069 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
8070
8071 new_regmask = (1 << (num_in_list + 1)) - 1;
8072
8073 if (debug_displaced)
8074 fprintf_unfiltered (gdb_stdlog, _("displaced: POP "
8075 "{..., pc}: original reg list %.4x,"
8076 " modified list %.4x\n"),
8077 (int) dsc->u.block.regmask, new_regmask);
8078
8079 dsc->u.block.regmask |= 0x8000;
8080 dsc->u.block.writeback = 0;
8081 dsc->u.block.cond = INST_AL;
8082
8083 dsc->modinsn[0] = (insn1 & ~0x1ff) | (new_regmask & 0xff);
8084
8085 dsc->cleanup = &cleanup_block_load_pc;
8086 }
8087
8088 return 0;
8089}
8090
8091static void
8092thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8093 struct regcache *regs,
8094 struct displaced_step_closure *dsc)
8095{
8096 unsigned short op_bit_12_15 = bits (insn1, 12, 15);
8097 unsigned short op_bit_10_11 = bits (insn1, 10, 11);
8098 int err = 0;
8099
8100 /* 16-bit thumb instructions. */
8101 switch (op_bit_12_15)
8102 {
8103 /* Shift (imme), add, subtract, move and compare. */
8104 case 0: case 1: case 2: case 3:
8105 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8106 "shift/add/sub/mov/cmp",
8107 dsc);
8108 break;
8109 case 4:
8110 switch (op_bit_10_11)
8111 {
8112 case 0: /* Data-processing */
8113 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8114 "data-processing",
8115 dsc);
8116 break;
8117 case 1: /* Special data instructions and branch and exchange. */
8118 {
8119 unsigned short op = bits (insn1, 7, 9);
8120 if (op == 6 || op == 7) /* BX or BLX */
8121 err = thumb_copy_bx_blx_reg (gdbarch, insn1, regs, dsc);
8122 else if (bits (insn1, 6, 7) != 0) /* ADD/MOV/CMP high registers. */
8123 err = thumb_copy_alu_reg (gdbarch, insn1, regs, dsc);
8124 else
8125 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "special data",
8126 dsc);
8127 }
8128 break;
8129 default: /* LDR (literal) */
8130 err = thumb_copy_16bit_ldr_literal (gdbarch, insn1, regs, dsc);
8131 }
8132 break;
8133 case 5: case 6: case 7: case 8: case 9: /* Load/Store single data item */
8134 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldr/str", dsc);
8135 break;
8136 case 10:
8137 if (op_bit_10_11 < 2) /* Generate PC-relative address */
8138 err = thumb_decode_pc_relative_16bit (gdbarch, insn1, regs, dsc);
8139 else /* Generate SP-relative address */
8140 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "sp-relative", dsc);
8141 break;
8142 case 11: /* Misc 16-bit instructions */
8143 {
8144 switch (bits (insn1, 8, 11))
8145 {
8146 case 1: case 3: case 9: case 11: /* CBNZ, CBZ */
8147 err = thumb_copy_cbnz_cbz (gdbarch, insn1, regs, dsc);
8148 break;
8149 case 12: case 13: /* POP */
8150 if (bit (insn1, 8)) /* PC is in register list. */
8151 err = thumb_copy_pop_pc_16bit (gdbarch, insn1, regs, dsc);
8152 else
8153 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "pop", dsc);
8154 break;
8155 case 15: /* If-Then, and hints */
8156 if (bits (insn1, 0, 3))
8157 /* If-Then makes up to four following instructions conditional.
8158 IT instruction itself is not conditional, so handle it as a
8159 common unmodified instruction. */
8160 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "If-Then",
8161 dsc);
8162 else
8163 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "hints", dsc);
8164 break;
8165 default:
8166 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "misc", dsc);
8167 }
8168 }
8169 break;
8170 case 12:
8171 if (op_bit_10_11 < 2) /* Store multiple registers */
8172 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "stm", dsc);
8173 else /* Load multiple registers */
8174 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldm", dsc);
8175 break;
8176 case 13: /* Conditional branch and supervisor call */
8177 if (bits (insn1, 9, 11) != 7) /* conditional branch */
8178 err = thumb_copy_b (gdbarch, insn1, dsc);
8179 else
8180 err = thumb_copy_svc (gdbarch, insn1, regs, dsc);
8181 break;
8182 case 14: /* Unconditional branch */
8183 err = thumb_copy_b (gdbarch, insn1, dsc);
8184 break;
8185 default:
8186 err = 1;
8187 }
8188
8189 if (err)
8190 internal_error (__FILE__, __LINE__,
8191 _("thumb_process_displaced_16bit_insn: Instruction decode error"));
8192}
8193
8194static int
8195decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
8196 uint16_t insn1, uint16_t insn2,
8197 struct regcache *regs,
8198 struct displaced_step_closure *dsc)
8199{
8200 int rt = bits (insn2, 12, 15);
8201 int rn = bits (insn1, 0, 3);
8202 int op1 = bits (insn1, 7, 8);
8203 int err = 0;
8204
8205 switch (bits (insn1, 5, 6))
8206 {
8207 case 0: /* Load byte and memory hints */
8208 if (rt == 0xf) /* PLD/PLI */
8209 {
8210 if (rn == 0xf)
8211 /* PLD literal or Encoding T3 of PLI(immediate, literal). */
8212 return thumb2_copy_preload (gdbarch, insn1, insn2, regs, dsc);
8213 else
8214 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8215 "pli/pld", dsc);
8216 }
8217 else
8218 {
8219 if (rn == 0xf) /* LDRB/LDRSB (literal) */
8220 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8221 1);
8222 else
8223 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8224 "ldrb{reg, immediate}/ldrbt",
8225 dsc);
8226 }
8227
8228 break;
8229 case 1: /* Load halfword and memory hints. */
8230 if (rt == 0xf) /* PLD{W} and Unalloc memory hint. */
8231 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8232 "pld/unalloc memhint", dsc);
8233 else
8234 {
8235 if (rn == 0xf)
8236 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8237 2);
8238 else
8239 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8240 "ldrh/ldrht", dsc);
8241 }
8242 break;
8243 case 2: /* Load word */
8244 {
8245 int insn2_bit_8_11 = bits (insn2, 8, 11);
8246
8247 if (rn == 0xf)
8248 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc, 4);
8249 else if (op1 == 0x1) /* Encoding T3 */
8250 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs, dsc,
8251 0, 1);
8252 else /* op1 == 0x0 */
8253 {
8254 if (insn2_bit_8_11 == 0xc || (insn2_bit_8_11 & 0x9) == 0x9)
8255 /* LDR (immediate) */
8256 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8257 dsc, bit (insn2, 8), 1);
8258 else if (insn2_bit_8_11 == 0xe) /* LDRT */
8259 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8260 "ldrt", dsc);
8261 else
8262 /* LDR (register) */
8263 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8264 dsc, 0, 0);
8265 }
8266 break;
8267 }
8268 default:
8269 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
8270 break;
8271 }
8272 return 0;
8273}
8274
8275static void
8276thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8277 uint16_t insn2, struct regcache *regs,
8278 struct displaced_step_closure *dsc)
8279{
8280 int err = 0;
8281 unsigned short op = bit (insn2, 15);
8282 unsigned int op1 = bits (insn1, 11, 12);
8283
8284 switch (op1)
8285 {
8286 case 1:
8287 {
8288 switch (bits (insn1, 9, 10))
8289 {
8290 case 0:
8291 if (bit (insn1, 6))
8292 {
8293 /* Load/store {dual, execlusive}, table branch. */
8294 if (bits (insn1, 7, 8) == 1 && bits (insn1, 4, 5) == 1
8295 && bits (insn2, 5, 7) == 0)
8296 err = thumb2_copy_table_branch (gdbarch, insn1, insn2, regs,
8297 dsc);
8298 else
8299 /* PC is not allowed to use in load/store {dual, exclusive}
8300 instructions. */
8301 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8302 "load/store dual/ex", dsc);
8303 }
8304 else /* load/store multiple */
8305 {
8306 switch (bits (insn1, 7, 8))
8307 {
8308 case 0: case 3: /* SRS, RFE */
8309 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8310 "srs/rfe", dsc);
8311 break;
8312 case 1: case 2: /* LDM/STM/PUSH/POP */
8313 err = thumb2_copy_block_xfer (gdbarch, insn1, insn2, regs, dsc);
8314 break;
8315 }
8316 }
8317 break;
8318
8319 case 1:
8320 /* Data-processing (shift register). */
8321 err = thumb2_decode_dp_shift_reg (gdbarch, insn1, insn2, regs,
8322 dsc);
8323 break;
8324 default: /* Coprocessor instructions. */
8325 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8326 break;
8327 }
8328 break;
8329 }
8330 case 2: /* op1 = 2 */
8331 if (op) /* Branch and misc control. */
8332 {
8333 if (bit (insn2, 14) /* BLX/BL */
8334 || bit (insn2, 12) /* Unconditional branch */
8335 || (bits (insn1, 7, 9) != 0x7)) /* Conditional branch */
8336 err = thumb2_copy_b_bl_blx (gdbarch, insn1, insn2, regs, dsc);
8337 else
8338 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8339 "misc ctrl", dsc);
8340 }
8341 else
8342 {
8343 if (bit (insn1, 9)) /* Data processing (plain binary imm). */
8344 {
8345 int op = bits (insn1, 4, 8);
8346 int rn = bits (insn1, 0, 3);
8347 if ((op == 0 || op == 0xa) && rn == 0xf)
8348 err = thumb_copy_pc_relative_32bit (gdbarch, insn1, insn2,
8349 regs, dsc);
8350 else
8351 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8352 "dp/pb", dsc);
8353 }
8354 else /* Data processing (modified immeidate) */
8355 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8356 "dp/mi", dsc);
8357 }
8358 break;
8359 case 3: /* op1 = 3 */
8360 switch (bits (insn1, 9, 10))
8361 {
8362 case 0:
8363 if (bit (insn1, 4))
8364 err = decode_thumb_32bit_ld_mem_hints (gdbarch, insn1, insn2,
8365 regs, dsc);
8366 else /* NEON Load/Store and Store single data item */
8367 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8368 "neon elt/struct load/store",
8369 dsc);
8370 break;
8371 case 1: /* op1 = 3, bits (9, 10) == 1 */
8372 switch (bits (insn1, 7, 8))
8373 {
8374 case 0: case 1: /* Data processing (register) */
8375 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8376 "dp(reg)", dsc);
8377 break;
8378 case 2: /* Multiply and absolute difference */
8379 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8380 "mul/mua/diff", dsc);
8381 break;
8382 case 3: /* Long multiply and divide */
8383 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8384 "lmul/lmua", dsc);
8385 break;
8386 }
8387 break;
8388 default: /* Coprocessor instructions */
8389 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8390 break;
8391 }
8392 break;
8393 default:
8394 err = 1;
8395 }
8396
8397 if (err)
8398 internal_error (__FILE__, __LINE__,
8399 _("thumb_process_displaced_32bit_insn: Instruction decode error"));
8400
8401}
8402
b434a28f
YQ
8403static void
8404thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8405 CORE_ADDR to, struct regcache *regs,
8406 struct displaced_step_closure *dsc)
8407{
34518530
YQ
8408 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8409 uint16_t insn1
8410 = read_memory_unsigned_integer (from, 2, byte_order_for_code);
8411
8412 if (debug_displaced)
8413 fprintf_unfiltered (gdb_stdlog, "displaced: process thumb insn %.4x "
8414 "at %.8lx\n", insn1, (unsigned long) from);
8415
8416 dsc->is_thumb = 1;
8417 dsc->insn_size = thumb_insn_size (insn1);
8418 if (thumb_insn_size (insn1) == 4)
8419 {
8420 uint16_t insn2
8421 = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
8422 thumb_process_displaced_32bit_insn (gdbarch, insn1, insn2, regs, dsc);
8423 }
8424 else
8425 thumb_process_displaced_16bit_insn (gdbarch, insn1, regs, dsc);
b434a28f
YQ
8426}
8427
cca44b1b 8428void
b434a28f
YQ
8429arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8430 CORE_ADDR to, struct regcache *regs,
cca44b1b
JB
8431 struct displaced_step_closure *dsc)
8432{
8433 int err = 0;
b434a28f
YQ
8434 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8435 uint32_t insn;
cca44b1b
JB
8436
8437 /* Most displaced instructions use a 1-instruction scratch space, so set this
8438 here and override below if/when necessary. */
8439 dsc->numinsns = 1;
8440 dsc->insn_addr = from;
8441 dsc->scratch_base = to;
8442 dsc->cleanup = NULL;
8443 dsc->wrote_to_pc = 0;
8444
b434a28f
YQ
8445 if (!displaced_in_arm_mode (regs))
8446 return thumb_process_displaced_insn (gdbarch, from, to, regs, dsc);
8447
4db71c0b
YQ
8448 dsc->is_thumb = 0;
8449 dsc->insn_size = 4;
b434a28f
YQ
8450 insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
8451 if (debug_displaced)
8452 fprintf_unfiltered (gdb_stdlog, "displaced: stepping insn %.8lx "
8453 "at %.8lx\n", (unsigned long) insn,
8454 (unsigned long) from);
8455
cca44b1b 8456 if ((insn & 0xf0000000) == 0xf0000000)
7ff120b4 8457 err = arm_decode_unconditional (gdbarch, insn, regs, dsc);
cca44b1b
JB
8458 else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
8459 {
8460 case 0x0: case 0x1: case 0x2: case 0x3:
7ff120b4 8461 err = arm_decode_dp_misc (gdbarch, insn, regs, dsc);
cca44b1b
JB
8462 break;
8463
8464 case 0x4: case 0x5: case 0x6:
7ff120b4 8465 err = arm_decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
cca44b1b
JB
8466 break;
8467
8468 case 0x7:
7ff120b4 8469 err = arm_decode_media (gdbarch, insn, dsc);
cca44b1b
JB
8470 break;
8471
8472 case 0x8: case 0x9: case 0xa: case 0xb:
7ff120b4 8473 err = arm_decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
cca44b1b
JB
8474 break;
8475
8476 case 0xc: case 0xd: case 0xe: case 0xf:
7ff120b4 8477 err = arm_decode_svc_copro (gdbarch, insn, to, regs, dsc);
cca44b1b
JB
8478 break;
8479 }
8480
8481 if (err)
8482 internal_error (__FILE__, __LINE__,
8483 _("arm_process_displaced_insn: Instruction decode error"));
8484}
8485
8486/* Actually set up the scratch space for a displaced instruction. */
8487
8488void
8489arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
8490 CORE_ADDR to, struct displaced_step_closure *dsc)
8491{
8492 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4db71c0b 8493 unsigned int i, len, offset;
cca44b1b 8494 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4db71c0b
YQ
8495 int size = dsc->is_thumb? 2 : 4;
8496 const unsigned char *bkp_insn;
cca44b1b 8497
4db71c0b 8498 offset = 0;
cca44b1b
JB
8499 /* Poke modified instruction(s). */
8500 for (i = 0; i < dsc->numinsns; i++)
8501 {
8502 if (debug_displaced)
4db71c0b
YQ
8503 {
8504 fprintf_unfiltered (gdb_stdlog, "displaced: writing insn ");
8505 if (size == 4)
8506 fprintf_unfiltered (gdb_stdlog, "%.8lx",
8507 dsc->modinsn[i]);
8508 else if (size == 2)
8509 fprintf_unfiltered (gdb_stdlog, "%.4x",
8510 (unsigned short)dsc->modinsn[i]);
8511
8512 fprintf_unfiltered (gdb_stdlog, " at %.8lx\n",
8513 (unsigned long) to + offset);
8514
8515 }
8516 write_memory_unsigned_integer (to + offset, size,
8517 byte_order_for_code,
cca44b1b 8518 dsc->modinsn[i]);
4db71c0b
YQ
8519 offset += size;
8520 }
8521
8522 /* Choose the correct breakpoint instruction. */
8523 if (dsc->is_thumb)
8524 {
8525 bkp_insn = tdep->thumb_breakpoint;
8526 len = tdep->thumb_breakpoint_size;
8527 }
8528 else
8529 {
8530 bkp_insn = tdep->arm_breakpoint;
8531 len = tdep->arm_breakpoint_size;
cca44b1b
JB
8532 }
8533
8534 /* Put breakpoint afterwards. */
4db71c0b 8535 write_memory (to + offset, bkp_insn, len);
cca44b1b
JB
8536
8537 if (debug_displaced)
8538 fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
8539 paddress (gdbarch, from), paddress (gdbarch, to));
8540}
8541
8542/* Entry point for copying an instruction into scratch space for displaced
8543 stepping. */
8544
8545struct displaced_step_closure *
8546arm_displaced_step_copy_insn (struct gdbarch *gdbarch,
8547 CORE_ADDR from, CORE_ADDR to,
8548 struct regcache *regs)
8549{
8550 struct displaced_step_closure *dsc
8551 = xmalloc (sizeof (struct displaced_step_closure));
b434a28f 8552 arm_process_displaced_insn (gdbarch, from, to, regs, dsc);
cca44b1b
JB
8553 arm_displaced_init_closure (gdbarch, from, to, dsc);
8554
8555 return dsc;
8556}
8557
8558/* Entry point for cleaning things up after a displaced instruction has been
8559 single-stepped. */
8560
8561void
8562arm_displaced_step_fixup (struct gdbarch *gdbarch,
8563 struct displaced_step_closure *dsc,
8564 CORE_ADDR from, CORE_ADDR to,
8565 struct regcache *regs)
8566{
8567 if (dsc->cleanup)
8568 dsc->cleanup (gdbarch, regs, dsc);
8569
8570 if (!dsc->wrote_to_pc)
4db71c0b
YQ
8571 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
8572 dsc->insn_addr + dsc->insn_size);
8573
cca44b1b
JB
8574}
8575
8576#include "bfd-in2.h"
8577#include "libcoff.h"
8578
8579static int
8580gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
8581{
9779414d
DJ
8582 struct gdbarch *gdbarch = info->application_data;
8583
8584 if (arm_pc_is_thumb (gdbarch, memaddr))
cca44b1b
JB
8585 {
8586 static asymbol *asym;
8587 static combined_entry_type ce;
8588 static struct coff_symbol_struct csym;
8589 static struct bfd fake_bfd;
8590 static bfd_target fake_target;
8591
8592 if (csym.native == NULL)
8593 {
8594 /* Create a fake symbol vector containing a Thumb symbol.
8595 This is solely so that the code in print_insn_little_arm()
8596 and print_insn_big_arm() in opcodes/arm-dis.c will detect
8597 the presence of a Thumb symbol and switch to decoding
8598 Thumb instructions. */
8599
8600 fake_target.flavour = bfd_target_coff_flavour;
8601 fake_bfd.xvec = &fake_target;
8602 ce.u.syment.n_sclass = C_THUMBEXTFUNC;
8603 csym.native = &ce;
8604 csym.symbol.the_bfd = &fake_bfd;
8605 csym.symbol.name = "fake";
8606 asym = (asymbol *) & csym;
8607 }
8608
8609 memaddr = UNMAKE_THUMB_ADDR (memaddr);
8610 info->symbols = &asym;
8611 }
8612 else
8613 info->symbols = NULL;
8614
8615 if (info->endian == BFD_ENDIAN_BIG)
8616 return print_insn_big_arm (memaddr, info);
8617 else
8618 return print_insn_little_arm (memaddr, info);
8619}
8620
8621/* The following define instruction sequences that will cause ARM
8622 cpu's to take an undefined instruction trap. These are used to
8623 signal a breakpoint to GDB.
8624
8625 The newer ARMv4T cpu's are capable of operating in ARM or Thumb
8626 modes. A different instruction is required for each mode. The ARM
8627 cpu's can also be big or little endian. Thus four different
8628 instructions are needed to support all cases.
8629
8630 Note: ARMv4 defines several new instructions that will take the
8631 undefined instruction trap. ARM7TDMI is nominally ARMv4T, but does
8632 not in fact add the new instructions. The new undefined
8633 instructions in ARMv4 are all instructions that had no defined
8634 behaviour in earlier chips. There is no guarantee that they will
8635 raise an exception, but may be treated as NOP's. In practice, it
8636 may only safe to rely on instructions matching:
8637
8638 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
8639 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
8640 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
8641
0963b4bd 8642 Even this may only true if the condition predicate is true. The
cca44b1b
JB
8643 following use a condition predicate of ALWAYS so it is always TRUE.
8644
8645 There are other ways of forcing a breakpoint. GNU/Linux, RISC iX,
8646 and NetBSD all use a software interrupt rather than an undefined
8647 instruction to force a trap. This can be handled by by the
8648 abi-specific code during establishment of the gdbarch vector. */
8649
8650#define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
8651#define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
8652#define THUMB_LE_BREAKPOINT {0xbe,0xbe}
8653#define THUMB_BE_BREAKPOINT {0xbe,0xbe}
8654
8655static const char arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
8656static const char arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
8657static const char arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
8658static const char arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
8659
8660/* Determine the type and size of breakpoint to insert at PCPTR. Uses
8661 the program counter value to determine whether a 16-bit or 32-bit
8662 breakpoint should be used. It returns a pointer to a string of
8663 bytes that encode a breakpoint instruction, stores the length of
8664 the string to *lenptr, and adjusts the program counter (if
8665 necessary) to point to the actual memory location where the
8666 breakpoint should be inserted. */
8667
8668static const unsigned char *
8669arm_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenptr)
8670{
8671 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
177321bd 8672 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
cca44b1b 8673
9779414d 8674 if (arm_pc_is_thumb (gdbarch, *pcptr))
cca44b1b
JB
8675 {
8676 *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
177321bd
DJ
8677
8678 /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
8679 check whether we are replacing a 32-bit instruction. */
8680 if (tdep->thumb2_breakpoint != NULL)
8681 {
8682 gdb_byte buf[2];
8683 if (target_read_memory (*pcptr, buf, 2) == 0)
8684 {
8685 unsigned short inst1;
8686 inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
db24da6d 8687 if (thumb_insn_size (inst1) == 4)
177321bd
DJ
8688 {
8689 *lenptr = tdep->thumb2_breakpoint_size;
8690 return tdep->thumb2_breakpoint;
8691 }
8692 }
8693 }
8694
cca44b1b
JB
8695 *lenptr = tdep->thumb_breakpoint_size;
8696 return tdep->thumb_breakpoint;
8697 }
8698 else
8699 {
8700 *lenptr = tdep->arm_breakpoint_size;
8701 return tdep->arm_breakpoint;
8702 }
8703}
8704
177321bd
DJ
8705static void
8706arm_remote_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
8707 int *kindptr)
8708{
177321bd
DJ
8709 arm_breakpoint_from_pc (gdbarch, pcptr, kindptr);
8710
9779414d 8711 if (arm_pc_is_thumb (gdbarch, *pcptr) && *kindptr == 4)
177321bd
DJ
8712 /* The documented magic value for a 32-bit Thumb-2 breakpoint, so
8713 that this is not confused with a 32-bit ARM breakpoint. */
8714 *kindptr = 3;
8715}
8716
cca44b1b
JB
8717/* Extract from an array REGBUF containing the (raw) register state a
8718 function return value of type TYPE, and copy that, in virtual
8719 format, into VALBUF. */
8720
8721static void
8722arm_extract_return_value (struct type *type, struct regcache *regs,
8723 gdb_byte *valbuf)
8724{
8725 struct gdbarch *gdbarch = get_regcache_arch (regs);
8726 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8727
8728 if (TYPE_CODE_FLT == TYPE_CODE (type))
8729 {
8730 switch (gdbarch_tdep (gdbarch)->fp_model)
8731 {
8732 case ARM_FLOAT_FPA:
8733 {
8734 /* The value is in register F0 in internal format. We need to
8735 extract the raw value and then convert it to the desired
8736 internal type. */
8737 bfd_byte tmpbuf[FP_REGISTER_SIZE];
8738
8739 regcache_cooked_read (regs, ARM_F0_REGNUM, tmpbuf);
8740 convert_from_extended (floatformat_from_type (type), tmpbuf,
8741 valbuf, gdbarch_byte_order (gdbarch));
8742 }
8743 break;
8744
8745 case ARM_FLOAT_SOFT_FPA:
8746 case ARM_FLOAT_SOFT_VFP:
8747 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8748 not using the VFP ABI code. */
8749 case ARM_FLOAT_VFP:
8750 regcache_cooked_read (regs, ARM_A1_REGNUM, valbuf);
8751 if (TYPE_LENGTH (type) > 4)
8752 regcache_cooked_read (regs, ARM_A1_REGNUM + 1,
8753 valbuf + INT_REGISTER_SIZE);
8754 break;
8755
8756 default:
0963b4bd
MS
8757 internal_error (__FILE__, __LINE__,
8758 _("arm_extract_return_value: "
8759 "Floating point model not supported"));
cca44b1b
JB
8760 break;
8761 }
8762 }
8763 else if (TYPE_CODE (type) == TYPE_CODE_INT
8764 || TYPE_CODE (type) == TYPE_CODE_CHAR
8765 || TYPE_CODE (type) == TYPE_CODE_BOOL
8766 || TYPE_CODE (type) == TYPE_CODE_PTR
8767 || TYPE_CODE (type) == TYPE_CODE_REF
8768 || TYPE_CODE (type) == TYPE_CODE_ENUM)
8769 {
b021a221
MS
8770 /* If the type is a plain integer, then the access is
8771 straight-forward. Otherwise we have to play around a bit
8772 more. */
cca44b1b
JB
8773 int len = TYPE_LENGTH (type);
8774 int regno = ARM_A1_REGNUM;
8775 ULONGEST tmp;
8776
8777 while (len > 0)
8778 {
8779 /* By using store_unsigned_integer we avoid having to do
8780 anything special for small big-endian values. */
8781 regcache_cooked_read_unsigned (regs, regno++, &tmp);
8782 store_unsigned_integer (valbuf,
8783 (len > INT_REGISTER_SIZE
8784 ? INT_REGISTER_SIZE : len),
8785 byte_order, tmp);
8786 len -= INT_REGISTER_SIZE;
8787 valbuf += INT_REGISTER_SIZE;
8788 }
8789 }
8790 else
8791 {
8792 /* For a structure or union the behaviour is as if the value had
8793 been stored to word-aligned memory and then loaded into
8794 registers with 32-bit load instruction(s). */
8795 int len = TYPE_LENGTH (type);
8796 int regno = ARM_A1_REGNUM;
8797 bfd_byte tmpbuf[INT_REGISTER_SIZE];
8798
8799 while (len > 0)
8800 {
8801 regcache_cooked_read (regs, regno++, tmpbuf);
8802 memcpy (valbuf, tmpbuf,
8803 len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
8804 len -= INT_REGISTER_SIZE;
8805 valbuf += INT_REGISTER_SIZE;
8806 }
8807 }
8808}
8809
8810
8811/* Will a function return an aggregate type in memory or in a
8812 register? Return 0 if an aggregate type can be returned in a
8813 register, 1 if it must be returned in memory. */
8814
8815static int
8816arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
8817{
8818 int nRc;
8819 enum type_code code;
8820
8821 CHECK_TYPEDEF (type);
8822
8823 /* In the ARM ABI, "integer" like aggregate types are returned in
8824 registers. For an aggregate type to be integer like, its size
8825 must be less than or equal to INT_REGISTER_SIZE and the
8826 offset of each addressable subfield must be zero. Note that bit
8827 fields are not addressable, and all addressable subfields of
8828 unions always start at offset zero.
8829
8830 This function is based on the behaviour of GCC 2.95.1.
8831 See: gcc/arm.c: arm_return_in_memory() for details.
8832
8833 Note: All versions of GCC before GCC 2.95.2 do not set up the
8834 parameters correctly for a function returning the following
8835 structure: struct { float f;}; This should be returned in memory,
8836 not a register. Richard Earnshaw sent me a patch, but I do not
8837 know of any way to detect if a function like the above has been
8838 compiled with the correct calling convention. */
8839
8840 /* All aggregate types that won't fit in a register must be returned
8841 in memory. */
8842 if (TYPE_LENGTH (type) > INT_REGISTER_SIZE)
8843 {
8844 return 1;
8845 }
8846
8847 /* The AAPCS says all aggregates not larger than a word are returned
8848 in a register. */
8849 if (gdbarch_tdep (gdbarch)->arm_abi != ARM_ABI_APCS)
8850 return 0;
8851
8852 /* The only aggregate types that can be returned in a register are
8853 structs and unions. Arrays must be returned in memory. */
8854 code = TYPE_CODE (type);
8855 if ((TYPE_CODE_STRUCT != code) && (TYPE_CODE_UNION != code))
8856 {
8857 return 1;
8858 }
8859
8860 /* Assume all other aggregate types can be returned in a register.
8861 Run a check for structures, unions and arrays. */
8862 nRc = 0;
8863
8864 if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
8865 {
8866 int i;
8867 /* Need to check if this struct/union is "integer" like. For
8868 this to be true, its size must be less than or equal to
8869 INT_REGISTER_SIZE and the offset of each addressable
8870 subfield must be zero. Note that bit fields are not
8871 addressable, and unions always start at offset zero. If any
8872 of the subfields is a floating point type, the struct/union
8873 cannot be an integer type. */
8874
8875 /* For each field in the object, check:
8876 1) Is it FP? --> yes, nRc = 1;
67255d04
RE
8877 2) Is it addressable (bitpos != 0) and
8878 not packed (bitsize == 0)?
8879 --> yes, nRc = 1
8880 */
8881
8882 for (i = 0; i < TYPE_NFIELDS (type); i++)
8883 {
8884 enum type_code field_type_code;
0963b4bd
MS
8885 field_type_code = TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type,
8886 i)));
67255d04
RE
8887
8888 /* Is it a floating point type field? */
8889 if (field_type_code == TYPE_CODE_FLT)
8890 {
8891 nRc = 1;
8892 break;
8893 }
8894
8895 /* If bitpos != 0, then we have to care about it. */
8896 if (TYPE_FIELD_BITPOS (type, i) != 0)
8897 {
8898 /* Bitfields are not addressable. If the field bitsize is
8899 zero, then the field is not packed. Hence it cannot be
8900 a bitfield or any other packed type. */
8901 if (TYPE_FIELD_BITSIZE (type, i) == 0)
8902 {
8903 nRc = 1;
8904 break;
8905 }
8906 }
8907 }
8908 }
8909
8910 return nRc;
8911}
8912
34e8f22d
RE
8913/* Write into appropriate registers a function return value of type
8914 TYPE, given in virtual format. */
8915
8916static void
b508a996 8917arm_store_return_value (struct type *type, struct regcache *regs,
5238cf52 8918 const gdb_byte *valbuf)
34e8f22d 8919{
be8626e0 8920 struct gdbarch *gdbarch = get_regcache_arch (regs);
e17a4113 8921 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
be8626e0 8922
34e8f22d
RE
8923 if (TYPE_CODE (type) == TYPE_CODE_FLT)
8924 {
7a5ea0d4 8925 char buf[MAX_REGISTER_SIZE];
34e8f22d 8926
be8626e0 8927 switch (gdbarch_tdep (gdbarch)->fp_model)
08216dd7
RE
8928 {
8929 case ARM_FLOAT_FPA:
8930
be8626e0
MD
8931 convert_to_extended (floatformat_from_type (type), buf, valbuf,
8932 gdbarch_byte_order (gdbarch));
b508a996 8933 regcache_cooked_write (regs, ARM_F0_REGNUM, buf);
08216dd7
RE
8934 break;
8935
fd50bc42 8936 case ARM_FLOAT_SOFT_FPA:
08216dd7 8937 case ARM_FLOAT_SOFT_VFP:
90445bd3
DJ
8938 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8939 not using the VFP ABI code. */
8940 case ARM_FLOAT_VFP:
b508a996
RE
8941 regcache_cooked_write (regs, ARM_A1_REGNUM, valbuf);
8942 if (TYPE_LENGTH (type) > 4)
8943 regcache_cooked_write (regs, ARM_A1_REGNUM + 1,
7a5ea0d4 8944 valbuf + INT_REGISTER_SIZE);
08216dd7
RE
8945 break;
8946
8947 default:
9b20d036
MS
8948 internal_error (__FILE__, __LINE__,
8949 _("arm_store_return_value: Floating "
8950 "point model not supported"));
08216dd7
RE
8951 break;
8952 }
34e8f22d 8953 }
b508a996
RE
8954 else if (TYPE_CODE (type) == TYPE_CODE_INT
8955 || TYPE_CODE (type) == TYPE_CODE_CHAR
8956 || TYPE_CODE (type) == TYPE_CODE_BOOL
8957 || TYPE_CODE (type) == TYPE_CODE_PTR
8958 || TYPE_CODE (type) == TYPE_CODE_REF
8959 || TYPE_CODE (type) == TYPE_CODE_ENUM)
8960 {
8961 if (TYPE_LENGTH (type) <= 4)
8962 {
8963 /* Values of one word or less are zero/sign-extended and
8964 returned in r0. */
7a5ea0d4 8965 bfd_byte tmpbuf[INT_REGISTER_SIZE];
b508a996
RE
8966 LONGEST val = unpack_long (type, valbuf);
8967
e17a4113 8968 store_signed_integer (tmpbuf, INT_REGISTER_SIZE, byte_order, val);
b508a996
RE
8969 regcache_cooked_write (regs, ARM_A1_REGNUM, tmpbuf);
8970 }
8971 else
8972 {
8973 /* Integral values greater than one word are stored in consecutive
8974 registers starting with r0. This will always be a multiple of
8975 the regiser size. */
8976 int len = TYPE_LENGTH (type);
8977 int regno = ARM_A1_REGNUM;
8978
8979 while (len > 0)
8980 {
8981 regcache_cooked_write (regs, regno++, valbuf);
7a5ea0d4
DJ
8982 len -= INT_REGISTER_SIZE;
8983 valbuf += INT_REGISTER_SIZE;
b508a996
RE
8984 }
8985 }
8986 }
34e8f22d 8987 else
b508a996
RE
8988 {
8989 /* For a structure or union the behaviour is as if the value had
8990 been stored to word-aligned memory and then loaded into
8991 registers with 32-bit load instruction(s). */
8992 int len = TYPE_LENGTH (type);
8993 int regno = ARM_A1_REGNUM;
7a5ea0d4 8994 bfd_byte tmpbuf[INT_REGISTER_SIZE];
b508a996
RE
8995
8996 while (len > 0)
8997 {
8998 memcpy (tmpbuf, valbuf,
7a5ea0d4 8999 len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
b508a996 9000 regcache_cooked_write (regs, regno++, tmpbuf);
7a5ea0d4
DJ
9001 len -= INT_REGISTER_SIZE;
9002 valbuf += INT_REGISTER_SIZE;
b508a996
RE
9003 }
9004 }
34e8f22d
RE
9005}
9006
2af48f68
PB
9007
9008/* Handle function return values. */
9009
9010static enum return_value_convention
6a3a010b 9011arm_return_value (struct gdbarch *gdbarch, struct value *function,
c055b101
CV
9012 struct type *valtype, struct regcache *regcache,
9013 gdb_byte *readbuf, const gdb_byte *writebuf)
2af48f68 9014{
7c00367c 9015 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
6a3a010b 9016 struct type *func_type = function ? value_type (function) : NULL;
90445bd3
DJ
9017 enum arm_vfp_cprc_base_type vfp_base_type;
9018 int vfp_base_count;
9019
9020 if (arm_vfp_abi_for_function (gdbarch, func_type)
9021 && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
9022 {
9023 int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
9024 int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
9025 int i;
9026 for (i = 0; i < vfp_base_count; i++)
9027 {
58d6951d
DJ
9028 if (reg_char == 'q')
9029 {
9030 if (writebuf)
9031 arm_neon_quad_write (gdbarch, regcache, i,
9032 writebuf + i * unit_length);
9033
9034 if (readbuf)
9035 arm_neon_quad_read (gdbarch, regcache, i,
9036 readbuf + i * unit_length);
9037 }
9038 else
9039 {
9040 char name_buf[4];
9041 int regnum;
9042
9043 sprintf (name_buf, "%c%d", reg_char, i);
9044 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9045 strlen (name_buf));
9046 if (writebuf)
9047 regcache_cooked_write (regcache, regnum,
9048 writebuf + i * unit_length);
9049 if (readbuf)
9050 regcache_cooked_read (regcache, regnum,
9051 readbuf + i * unit_length);
9052 }
90445bd3
DJ
9053 }
9054 return RETURN_VALUE_REGISTER_CONVENTION;
9055 }
7c00367c 9056
2af48f68
PB
9057 if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
9058 || TYPE_CODE (valtype) == TYPE_CODE_UNION
9059 || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
9060 {
7c00367c
MK
9061 if (tdep->struct_return == pcc_struct_return
9062 || arm_return_in_memory (gdbarch, valtype))
2af48f68
PB
9063 return RETURN_VALUE_STRUCT_CONVENTION;
9064 }
9065
7052e42c
UW
9066 /* AAPCS returns complex types longer than a register in memory. */
9067 if (tdep->arm_abi != ARM_ABI_APCS
9068 && TYPE_CODE (valtype) == TYPE_CODE_COMPLEX
9069 && TYPE_LENGTH (valtype) > INT_REGISTER_SIZE)
9070 return RETURN_VALUE_STRUCT_CONVENTION;
9071
2af48f68
PB
9072 if (writebuf)
9073 arm_store_return_value (valtype, regcache, writebuf);
9074
9075 if (readbuf)
9076 arm_extract_return_value (valtype, regcache, readbuf);
9077
9078 return RETURN_VALUE_REGISTER_CONVENTION;
9079}
9080
9081
9df628e0 9082static int
60ade65d 9083arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
9df628e0 9084{
e17a4113
UW
9085 struct gdbarch *gdbarch = get_frame_arch (frame);
9086 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
9087 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
9df628e0 9088 CORE_ADDR jb_addr;
7a5ea0d4 9089 char buf[INT_REGISTER_SIZE];
9df628e0 9090
60ade65d 9091 jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
9df628e0
RE
9092
9093 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
7a5ea0d4 9094 INT_REGISTER_SIZE))
9df628e0
RE
9095 return 0;
9096
e17a4113 9097 *pc = extract_unsigned_integer (buf, INT_REGISTER_SIZE, byte_order);
9df628e0
RE
9098 return 1;
9099}
9100
faa95490
DJ
9101/* Recognize GCC and GNU ld's trampolines. If we are in a trampoline,
9102 return the target PC. Otherwise return 0. */
c906108c
SS
9103
9104CORE_ADDR
52f729a7 9105arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
c906108c 9106{
2c02bd72 9107 const char *name;
faa95490 9108 int namelen;
c906108c
SS
9109 CORE_ADDR start_addr;
9110
9111 /* Find the starting address and name of the function containing the PC. */
9112 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
9113 return 0;
9114
faa95490
DJ
9115 /* If PC is in a Thumb call or return stub, return the address of the
9116 target PC, which is in a register. The thunk functions are called
9117 _call_via_xx, where x is the register name. The possible names
3d8d5e79
DJ
9118 are r0-r9, sl, fp, ip, sp, and lr. ARM RealView has similar
9119 functions, named __ARM_call_via_r[0-7]. */
9120 if (strncmp (name, "_call_via_", 10) == 0
9121 || strncmp (name, "__ARM_call_via_", strlen ("__ARM_call_via_")) == 0)
c906108c 9122 {
ed9a39eb
JM
9123 /* Use the name suffix to determine which register contains the
9124 target PC. */
c5aa993b
JM
9125 static char *table[15] =
9126 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
9127 "r8", "r9", "sl", "fp", "ip", "sp", "lr"
9128 };
c906108c 9129 int regno;
faa95490 9130 int offset = strlen (name) - 2;
c906108c
SS
9131
9132 for (regno = 0; regno <= 14; regno++)
faa95490 9133 if (strcmp (&name[offset], table[regno]) == 0)
52f729a7 9134 return get_frame_register_unsigned (frame, regno);
c906108c 9135 }
ed9a39eb 9136
faa95490
DJ
9137 /* GNU ld generates __foo_from_arm or __foo_from_thumb for
9138 non-interworking calls to foo. We could decode the stubs
9139 to find the target but it's easier to use the symbol table. */
9140 namelen = strlen (name);
9141 if (name[0] == '_' && name[1] == '_'
9142 && ((namelen > 2 + strlen ("_from_thumb")
9143 && strncmp (name + namelen - strlen ("_from_thumb"), "_from_thumb",
9144 strlen ("_from_thumb")) == 0)
9145 || (namelen > 2 + strlen ("_from_arm")
9146 && strncmp (name + namelen - strlen ("_from_arm"), "_from_arm",
9147 strlen ("_from_arm")) == 0)))
9148 {
9149 char *target_name;
9150 int target_len = namelen - 2;
9151 struct minimal_symbol *minsym;
9152 struct objfile *objfile;
9153 struct obj_section *sec;
9154
9155 if (name[namelen - 1] == 'b')
9156 target_len -= strlen ("_from_thumb");
9157 else
9158 target_len -= strlen ("_from_arm");
9159
9160 target_name = alloca (target_len + 1);
9161 memcpy (target_name, name + 2, target_len);
9162 target_name[target_len] = '\0';
9163
9164 sec = find_pc_section (pc);
9165 objfile = (sec == NULL) ? NULL : sec->objfile;
9166 minsym = lookup_minimal_symbol (target_name, NULL, objfile);
9167 if (minsym != NULL)
9168 return SYMBOL_VALUE_ADDRESS (minsym);
9169 else
9170 return 0;
9171 }
9172
c5aa993b 9173 return 0; /* not a stub */
c906108c
SS
9174}
9175
afd7eef0
RE
9176static void
9177set_arm_command (char *args, int from_tty)
9178{
edefbb7c
AC
9179 printf_unfiltered (_("\
9180\"set arm\" must be followed by an apporpriate subcommand.\n"));
afd7eef0
RE
9181 help_list (setarmcmdlist, "set arm ", all_commands, gdb_stdout);
9182}
9183
9184static void
9185show_arm_command (char *args, int from_tty)
9186{
26304000 9187 cmd_show_list (showarmcmdlist, from_tty, "");
afd7eef0
RE
9188}
9189
28e97307
DJ
9190static void
9191arm_update_current_architecture (void)
fd50bc42 9192{
28e97307 9193 struct gdbarch_info info;
fd50bc42 9194
28e97307 9195 /* If the current architecture is not ARM, we have nothing to do. */
1cf3db46 9196 if (gdbarch_bfd_arch_info (target_gdbarch)->arch != bfd_arch_arm)
28e97307 9197 return;
fd50bc42 9198
28e97307
DJ
9199 /* Update the architecture. */
9200 gdbarch_info_init (&info);
fd50bc42 9201
28e97307 9202 if (!gdbarch_update_p (info))
9b20d036 9203 internal_error (__FILE__, __LINE__, _("could not update architecture"));
fd50bc42
RE
9204}
9205
9206static void
9207set_fp_model_sfunc (char *args, int from_tty,
9208 struct cmd_list_element *c)
9209{
9210 enum arm_float_model fp_model;
9211
9212 for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
9213 if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
9214 {
9215 arm_fp_model = fp_model;
9216 break;
9217 }
9218
9219 if (fp_model == ARM_FLOAT_LAST)
edefbb7c 9220 internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
fd50bc42
RE
9221 current_fp_model);
9222
28e97307 9223 arm_update_current_architecture ();
fd50bc42
RE
9224}
9225
9226static void
08546159
AC
9227show_fp_model (struct ui_file *file, int from_tty,
9228 struct cmd_list_element *c, const char *value)
fd50bc42 9229{
1cf3db46 9230 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
fd50bc42 9231
28e97307 9232 if (arm_fp_model == ARM_FLOAT_AUTO
1cf3db46 9233 && gdbarch_bfd_arch_info (target_gdbarch)->arch == bfd_arch_arm)
28e97307
DJ
9234 fprintf_filtered (file, _("\
9235The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
9236 fp_model_strings[tdep->fp_model]);
9237 else
9238 fprintf_filtered (file, _("\
9239The current ARM floating point model is \"%s\".\n"),
9240 fp_model_strings[arm_fp_model]);
9241}
9242
9243static void
9244arm_set_abi (char *args, int from_tty,
9245 struct cmd_list_element *c)
9246{
9247 enum arm_abi_kind arm_abi;
9248
9249 for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
9250 if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
9251 {
9252 arm_abi_global = arm_abi;
9253 break;
9254 }
9255
9256 if (arm_abi == ARM_ABI_LAST)
9257 internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
9258 arm_abi_string);
9259
9260 arm_update_current_architecture ();
9261}
9262
9263static void
9264arm_show_abi (struct ui_file *file, int from_tty,
9265 struct cmd_list_element *c, const char *value)
9266{
1cf3db46 9267 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
28e97307
DJ
9268
9269 if (arm_abi_global == ARM_ABI_AUTO
1cf3db46 9270 && gdbarch_bfd_arch_info (target_gdbarch)->arch == bfd_arch_arm)
28e97307
DJ
9271 fprintf_filtered (file, _("\
9272The current ARM ABI is \"auto\" (currently \"%s\").\n"),
9273 arm_abi_strings[tdep->arm_abi]);
9274 else
9275 fprintf_filtered (file, _("The current ARM ABI is \"%s\".\n"),
9276 arm_abi_string);
fd50bc42
RE
9277}
9278
0428b8f5
DJ
9279static void
9280arm_show_fallback_mode (struct ui_file *file, int from_tty,
9281 struct cmd_list_element *c, const char *value)
9282{
0963b4bd
MS
9283 fprintf_filtered (file,
9284 _("The current execution mode assumed "
9285 "(when symbols are unavailable) is \"%s\".\n"),
0428b8f5
DJ
9286 arm_fallback_mode_string);
9287}
9288
9289static void
9290arm_show_force_mode (struct ui_file *file, int from_tty,
9291 struct cmd_list_element *c, const char *value)
9292{
1cf3db46 9293 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
0428b8f5 9294
0963b4bd
MS
9295 fprintf_filtered (file,
9296 _("The current execution mode assumed "
9297 "(even when symbols are available) is \"%s\".\n"),
0428b8f5
DJ
9298 arm_force_mode_string);
9299}
9300
afd7eef0
RE
9301/* If the user changes the register disassembly style used for info
9302 register and other commands, we have to also switch the style used
9303 in opcodes for disassembly output. This function is run in the "set
9304 arm disassembly" command, and does that. */
bc90b915
FN
9305
9306static void
afd7eef0 9307set_disassembly_style_sfunc (char *args, int from_tty,
bc90b915
FN
9308 struct cmd_list_element *c)
9309{
afd7eef0 9310 set_disassembly_style ();
bc90b915
FN
9311}
9312\f
966fbf70 9313/* Return the ARM register name corresponding to register I. */
a208b0cb 9314static const char *
d93859e2 9315arm_register_name (struct gdbarch *gdbarch, int i)
966fbf70 9316{
58d6951d
DJ
9317 const int num_regs = gdbarch_num_regs (gdbarch);
9318
9319 if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
9320 && i >= num_regs && i < num_regs + 32)
9321 {
9322 static const char *const vfp_pseudo_names[] = {
9323 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
9324 "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
9325 "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
9326 "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
9327 };
9328
9329 return vfp_pseudo_names[i - num_regs];
9330 }
9331
9332 if (gdbarch_tdep (gdbarch)->have_neon_pseudos
9333 && i >= num_regs + 32 && i < num_regs + 32 + 16)
9334 {
9335 static const char *const neon_pseudo_names[] = {
9336 "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
9337 "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
9338 };
9339
9340 return neon_pseudo_names[i - num_regs - 32];
9341 }
9342
ff6f572f
DJ
9343 if (i >= ARRAY_SIZE (arm_register_names))
9344 /* These registers are only supported on targets which supply
9345 an XML description. */
9346 return "";
9347
966fbf70
RE
9348 return arm_register_names[i];
9349}
9350
bc90b915 9351static void
afd7eef0 9352set_disassembly_style (void)
bc90b915 9353{
123dc839 9354 int current;
bc90b915 9355
123dc839
DJ
9356 /* Find the style that the user wants. */
9357 for (current = 0; current < num_disassembly_options; current++)
9358 if (disassembly_style == valid_disassembly_styles[current])
9359 break;
9360 gdb_assert (current < num_disassembly_options);
bc90b915 9361
94c30b78 9362 /* Synchronize the disassembler. */
bc90b915
FN
9363 set_arm_regname_option (current);
9364}
9365
082fc60d
RE
9366/* Test whether the coff symbol specific value corresponds to a Thumb
9367 function. */
9368
9369static int
9370coff_sym_is_thumb (int val)
9371{
f8bf5763
PM
9372 return (val == C_THUMBEXT
9373 || val == C_THUMBSTAT
9374 || val == C_THUMBEXTFUNC
9375 || val == C_THUMBSTATFUNC
9376 || val == C_THUMBLABEL);
082fc60d
RE
9377}
9378
9379/* arm_coff_make_msymbol_special()
9380 arm_elf_make_msymbol_special()
9381
9382 These functions test whether the COFF or ELF symbol corresponds to
9383 an address in thumb code, and set a "special" bit in a minimal
9384 symbol to indicate that it does. */
9385
34e8f22d 9386static void
082fc60d
RE
9387arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
9388{
467d42c4
UW
9389 if (ARM_SYM_BRANCH_TYPE (&((elf_symbol_type *)sym)->internal_elf_sym)
9390 == ST_BRANCH_TO_THUMB)
082fc60d
RE
9391 MSYMBOL_SET_SPECIAL (msym);
9392}
9393
34e8f22d 9394static void
082fc60d
RE
9395arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
9396{
9397 if (coff_sym_is_thumb (val))
9398 MSYMBOL_SET_SPECIAL (msym);
9399}
9400
60c5725c 9401static void
c1bd65d0 9402arm_objfile_data_free (struct objfile *objfile, void *arg)
60c5725c
DJ
9403{
9404 struct arm_per_objfile *data = arg;
9405 unsigned int i;
9406
9407 for (i = 0; i < objfile->obfd->section_count; i++)
9408 VEC_free (arm_mapping_symbol_s, data->section_maps[i]);
9409}
9410
9411static void
9412arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
9413 asymbol *sym)
9414{
9415 const char *name = bfd_asymbol_name (sym);
9416 struct arm_per_objfile *data;
9417 VEC(arm_mapping_symbol_s) **map_p;
9418 struct arm_mapping_symbol new_map_sym;
9419
9420 gdb_assert (name[0] == '$');
9421 if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
9422 return;
9423
9424 data = objfile_data (objfile, arm_objfile_data_key);
9425 if (data == NULL)
9426 {
9427 data = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9428 struct arm_per_objfile);
9429 set_objfile_data (objfile, arm_objfile_data_key, data);
9430 data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
9431 objfile->obfd->section_count,
9432 VEC(arm_mapping_symbol_s) *);
9433 }
9434 map_p = &data->section_maps[bfd_get_section (sym)->index];
9435
9436 new_map_sym.value = sym->value;
9437 new_map_sym.type = name[1];
9438
9439 /* Assume that most mapping symbols appear in order of increasing
9440 value. If they were randomly distributed, it would be faster to
9441 always push here and then sort at first use. */
9442 if (!VEC_empty (arm_mapping_symbol_s, *map_p))
9443 {
9444 struct arm_mapping_symbol *prev_map_sym;
9445
9446 prev_map_sym = VEC_last (arm_mapping_symbol_s, *map_p);
9447 if (prev_map_sym->value >= sym->value)
9448 {
9449 unsigned int idx;
9450 idx = VEC_lower_bound (arm_mapping_symbol_s, *map_p, &new_map_sym,
9451 arm_compare_mapping_symbols);
9452 VEC_safe_insert (arm_mapping_symbol_s, *map_p, idx, &new_map_sym);
9453 return;
9454 }
9455 }
9456
9457 VEC_safe_push (arm_mapping_symbol_s, *map_p, &new_map_sym);
9458}
9459
756fe439 9460static void
61a1198a 9461arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
756fe439 9462{
9779414d 9463 struct gdbarch *gdbarch = get_regcache_arch (regcache);
61a1198a 9464 regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
756fe439
DJ
9465
9466 /* If necessary, set the T bit. */
9467 if (arm_apcs_32)
9468 {
9779414d 9469 ULONGEST val, t_bit;
61a1198a 9470 regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
9779414d
DJ
9471 t_bit = arm_psr_thumb_bit (gdbarch);
9472 if (arm_pc_is_thumb (gdbarch, pc))
9473 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9474 val | t_bit);
756fe439 9475 else
61a1198a 9476 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9779414d 9477 val & ~t_bit);
756fe439
DJ
9478 }
9479}
123dc839 9480
58d6951d
DJ
9481/* Read the contents of a NEON quad register, by reading from two
9482 double registers. This is used to implement the quad pseudo
9483 registers, and for argument passing in case the quad registers are
9484 missing; vectors are passed in quad registers when using the VFP
9485 ABI, even if a NEON unit is not present. REGNUM is the index of
9486 the quad register, in [0, 15]. */
9487
05d1431c 9488static enum register_status
58d6951d
DJ
9489arm_neon_quad_read (struct gdbarch *gdbarch, struct regcache *regcache,
9490 int regnum, gdb_byte *buf)
9491{
9492 char name_buf[4];
9493 gdb_byte reg_buf[8];
9494 int offset, double_regnum;
05d1431c 9495 enum register_status status;
58d6951d
DJ
9496
9497 sprintf (name_buf, "d%d", regnum << 1);
9498 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9499 strlen (name_buf));
9500
9501 /* d0 is always the least significant half of q0. */
9502 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9503 offset = 8;
9504 else
9505 offset = 0;
9506
05d1431c
PA
9507 status = regcache_raw_read (regcache, double_regnum, reg_buf);
9508 if (status != REG_VALID)
9509 return status;
58d6951d
DJ
9510 memcpy (buf + offset, reg_buf, 8);
9511
9512 offset = 8 - offset;
05d1431c
PA
9513 status = regcache_raw_read (regcache, double_regnum + 1, reg_buf);
9514 if (status != REG_VALID)
9515 return status;
58d6951d 9516 memcpy (buf + offset, reg_buf, 8);
05d1431c
PA
9517
9518 return REG_VALID;
58d6951d
DJ
9519}
9520
05d1431c 9521static enum register_status
58d6951d
DJ
9522arm_pseudo_read (struct gdbarch *gdbarch, struct regcache *regcache,
9523 int regnum, gdb_byte *buf)
9524{
9525 const int num_regs = gdbarch_num_regs (gdbarch);
9526 char name_buf[4];
9527 gdb_byte reg_buf[8];
9528 int offset, double_regnum;
9529
9530 gdb_assert (regnum >= num_regs);
9531 regnum -= num_regs;
9532
9533 if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
9534 /* Quad-precision register. */
05d1431c 9535 return arm_neon_quad_read (gdbarch, regcache, regnum - 32, buf);
58d6951d
DJ
9536 else
9537 {
05d1431c
PA
9538 enum register_status status;
9539
58d6951d
DJ
9540 /* Single-precision register. */
9541 gdb_assert (regnum < 32);
9542
9543 /* s0 is always the least significant half of d0. */
9544 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9545 offset = (regnum & 1) ? 0 : 4;
9546 else
9547 offset = (regnum & 1) ? 4 : 0;
9548
9549 sprintf (name_buf, "d%d", regnum >> 1);
9550 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9551 strlen (name_buf));
9552
05d1431c
PA
9553 status = regcache_raw_read (regcache, double_regnum, reg_buf);
9554 if (status == REG_VALID)
9555 memcpy (buf, reg_buf + offset, 4);
9556 return status;
58d6951d
DJ
9557 }
9558}
9559
9560/* Store the contents of BUF to a NEON quad register, by writing to
9561 two double registers. This is used to implement the quad pseudo
9562 registers, and for argument passing in case the quad registers are
9563 missing; vectors are passed in quad registers when using the VFP
9564 ABI, even if a NEON unit is not present. REGNUM is the index
9565 of the quad register, in [0, 15]. */
9566
9567static void
9568arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
9569 int regnum, const gdb_byte *buf)
9570{
9571 char name_buf[4];
58d6951d
DJ
9572 int offset, double_regnum;
9573
9574 sprintf (name_buf, "d%d", regnum << 1);
9575 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9576 strlen (name_buf));
9577
9578 /* d0 is always the least significant half of q0. */
9579 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9580 offset = 8;
9581 else
9582 offset = 0;
9583
9584 regcache_raw_write (regcache, double_regnum, buf + offset);
9585 offset = 8 - offset;
9586 regcache_raw_write (regcache, double_regnum + 1, buf + offset);
9587}
9588
9589static void
9590arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
9591 int regnum, const gdb_byte *buf)
9592{
9593 const int num_regs = gdbarch_num_regs (gdbarch);
9594 char name_buf[4];
9595 gdb_byte reg_buf[8];
9596 int offset, double_regnum;
9597
9598 gdb_assert (regnum >= num_regs);
9599 regnum -= num_regs;
9600
9601 if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
9602 /* Quad-precision register. */
9603 arm_neon_quad_write (gdbarch, regcache, regnum - 32, buf);
9604 else
9605 {
9606 /* Single-precision register. */
9607 gdb_assert (regnum < 32);
9608
9609 /* s0 is always the least significant half of d0. */
9610 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9611 offset = (regnum & 1) ? 0 : 4;
9612 else
9613 offset = (regnum & 1) ? 4 : 0;
9614
9615 sprintf (name_buf, "d%d", regnum >> 1);
9616 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9617 strlen (name_buf));
9618
9619 regcache_raw_read (regcache, double_regnum, reg_buf);
9620 memcpy (reg_buf + offset, buf, 4);
9621 regcache_raw_write (regcache, double_regnum, reg_buf);
9622 }
9623}
9624
123dc839
DJ
9625static struct value *
9626value_of_arm_user_reg (struct frame_info *frame, const void *baton)
9627{
9628 const int *reg_p = baton;
9629 return value_of_register (*reg_p, frame);
9630}
97e03143 9631\f
70f80edf
JT
9632static enum gdb_osabi
9633arm_elf_osabi_sniffer (bfd *abfd)
97e03143 9634{
2af48f68 9635 unsigned int elfosabi;
70f80edf 9636 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
97e03143 9637
70f80edf 9638 elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
97e03143 9639
28e97307
DJ
9640 if (elfosabi == ELFOSABI_ARM)
9641 /* GNU tools use this value. Check note sections in this case,
9642 as well. */
9643 bfd_map_over_sections (abfd,
9644 generic_elf_osabi_sniff_abi_tag_sections,
9645 &osabi);
97e03143 9646
28e97307 9647 /* Anything else will be handled by the generic ELF sniffer. */
70f80edf 9648 return osabi;
97e03143
RE
9649}
9650
54483882
YQ
9651static int
9652arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
9653 struct reggroup *group)
9654{
2c291032
YQ
9655 /* FPS register's type is INT, but belongs to float_reggroup. Beside
9656 this, FPS register belongs to save_regroup, restore_reggroup, and
9657 all_reggroup, of course. */
54483882 9658 if (regnum == ARM_FPS_REGNUM)
2c291032
YQ
9659 return (group == float_reggroup
9660 || group == save_reggroup
9661 || group == restore_reggroup
9662 || group == all_reggroup);
54483882
YQ
9663 else
9664 return default_register_reggroup_p (gdbarch, regnum, group);
9665}
9666
25f8c692
JL
9667\f
9668/* For backward-compatibility we allow two 'g' packet lengths with
9669 the remote protocol depending on whether FPA registers are
9670 supplied. M-profile targets do not have FPA registers, but some
9671 stubs already exist in the wild which use a 'g' packet which
9672 supplies them albeit with dummy values. The packet format which
9673 includes FPA registers should be considered deprecated for
9674 M-profile targets. */
9675
9676static void
9677arm_register_g_packet_guesses (struct gdbarch *gdbarch)
9678{
9679 if (gdbarch_tdep (gdbarch)->is_m)
9680 {
9681 /* If we know from the executable this is an M-profile target,
9682 cater for remote targets whose register set layout is the
9683 same as the FPA layout. */
9684 register_remote_g_packet_guess (gdbarch,
03145bf4 9685 /* r0-r12,sp,lr,pc; f0-f7; fps,xpsr */
25f8c692
JL
9686 (16 * INT_REGISTER_SIZE)
9687 + (8 * FP_REGISTER_SIZE)
9688 + (2 * INT_REGISTER_SIZE),
9689 tdesc_arm_with_m_fpa_layout);
9690
9691 /* The regular M-profile layout. */
9692 register_remote_g_packet_guess (gdbarch,
9693 /* r0-r12,sp,lr,pc; xpsr */
9694 (16 * INT_REGISTER_SIZE)
9695 + INT_REGISTER_SIZE,
9696 tdesc_arm_with_m);
3184d3f9
JL
9697
9698 /* M-profile plus M4F VFP. */
9699 register_remote_g_packet_guess (gdbarch,
9700 /* r0-r12,sp,lr,pc; d0-d15; fpscr,xpsr */
9701 (16 * INT_REGISTER_SIZE)
9702 + (16 * VFP_REGISTER_SIZE)
9703 + (2 * INT_REGISTER_SIZE),
9704 tdesc_arm_with_m_vfp_d16);
25f8c692
JL
9705 }
9706
9707 /* Otherwise we don't have a useful guess. */
9708}
9709
70f80edf 9710\f
da3c6d4a
MS
9711/* Initialize the current architecture based on INFO. If possible,
9712 re-use an architecture from ARCHES, which is a list of
9713 architectures already created during this debugging session.
97e03143 9714
da3c6d4a
MS
9715 Called e.g. at program startup, when reading a core file, and when
9716 reading a binary file. */
97e03143 9717
39bbf761
RE
9718static struct gdbarch *
9719arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
9720{
97e03143 9721 struct gdbarch_tdep *tdep;
39bbf761 9722 struct gdbarch *gdbarch;
28e97307
DJ
9723 struct gdbarch_list *best_arch;
9724 enum arm_abi_kind arm_abi = arm_abi_global;
9725 enum arm_float_model fp_model = arm_fp_model;
123dc839 9726 struct tdesc_arch_data *tdesc_data = NULL;
9779414d 9727 int i, is_m = 0;
58d6951d
DJ
9728 int have_vfp_registers = 0, have_vfp_pseudos = 0, have_neon_pseudos = 0;
9729 int have_neon = 0;
ff6f572f 9730 int have_fpa_registers = 1;
9779414d
DJ
9731 const struct target_desc *tdesc = info.target_desc;
9732
9733 /* If we have an object to base this architecture on, try to determine
9734 its ABI. */
9735
9736 if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
9737 {
9738 int ei_osabi, e_flags;
9739
9740 switch (bfd_get_flavour (info.abfd))
9741 {
9742 case bfd_target_aout_flavour:
9743 /* Assume it's an old APCS-style ABI. */
9744 arm_abi = ARM_ABI_APCS;
9745 break;
9746
9747 case bfd_target_coff_flavour:
9748 /* Assume it's an old APCS-style ABI. */
9749 /* XXX WinCE? */
9750 arm_abi = ARM_ABI_APCS;
9751 break;
9752
9753 case bfd_target_elf_flavour:
9754 ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
9755 e_flags = elf_elfheader (info.abfd)->e_flags;
9756
9757 if (ei_osabi == ELFOSABI_ARM)
9758 {
9759 /* GNU tools used to use this value, but do not for EABI
9760 objects. There's nowhere to tag an EABI version
9761 anyway, so assume APCS. */
9762 arm_abi = ARM_ABI_APCS;
9763 }
9764 else if (ei_osabi == ELFOSABI_NONE)
9765 {
9766 int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
9767 int attr_arch, attr_profile;
9768
9769 switch (eabi_ver)
9770 {
9771 case EF_ARM_EABI_UNKNOWN:
9772 /* Assume GNU tools. */
9773 arm_abi = ARM_ABI_APCS;
9774 break;
9775
9776 case EF_ARM_EABI_VER4:
9777 case EF_ARM_EABI_VER5:
9778 arm_abi = ARM_ABI_AAPCS;
9779 /* EABI binaries default to VFP float ordering.
9780 They may also contain build attributes that can
9781 be used to identify if the VFP argument-passing
9782 ABI is in use. */
9783 if (fp_model == ARM_FLOAT_AUTO)
9784 {
9785#ifdef HAVE_ELF
9786 switch (bfd_elf_get_obj_attr_int (info.abfd,
9787 OBJ_ATTR_PROC,
9788 Tag_ABI_VFP_args))
9789 {
9790 case 0:
9791 /* "The user intended FP parameter/result
9792 passing to conform to AAPCS, base
9793 variant". */
9794 fp_model = ARM_FLOAT_SOFT_VFP;
9795 break;
9796 case 1:
9797 /* "The user intended FP parameter/result
9798 passing to conform to AAPCS, VFP
9799 variant". */
9800 fp_model = ARM_FLOAT_VFP;
9801 break;
9802 case 2:
9803 /* "The user intended FP parameter/result
9804 passing to conform to tool chain-specific
9805 conventions" - we don't know any such
9806 conventions, so leave it as "auto". */
9807 break;
9808 default:
9809 /* Attribute value not mentioned in the
9810 October 2008 ABI, so leave it as
9811 "auto". */
9812 break;
9813 }
9814#else
9815 fp_model = ARM_FLOAT_SOFT_VFP;
9816#endif
9817 }
9818 break;
9819
9820 default:
9821 /* Leave it as "auto". */
9822 warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
9823 break;
9824 }
9825
9826#ifdef HAVE_ELF
9827 /* Detect M-profile programs. This only works if the
9828 executable file includes build attributes; GCC does
9829 copy them to the executable, but e.g. RealView does
9830 not. */
9831 attr_arch = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9832 Tag_CPU_arch);
0963b4bd
MS
9833 attr_profile = bfd_elf_get_obj_attr_int (info.abfd,
9834 OBJ_ATTR_PROC,
9779414d
DJ
9835 Tag_CPU_arch_profile);
9836 /* GCC specifies the profile for v6-M; RealView only
9837 specifies the profile for architectures starting with
9838 V7 (as opposed to architectures with a tag
9839 numerically greater than TAG_CPU_ARCH_V7). */
9840 if (!tdesc_has_registers (tdesc)
9841 && (attr_arch == TAG_CPU_ARCH_V6_M
9842 || attr_arch == TAG_CPU_ARCH_V6S_M
9843 || attr_profile == 'M'))
25f8c692 9844 is_m = 1;
9779414d
DJ
9845#endif
9846 }
9847
9848 if (fp_model == ARM_FLOAT_AUTO)
9849 {
9850 int e_flags = elf_elfheader (info.abfd)->e_flags;
9851
9852 switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
9853 {
9854 case 0:
9855 /* Leave it as "auto". Strictly speaking this case
9856 means FPA, but almost nobody uses that now, and
9857 many toolchains fail to set the appropriate bits
9858 for the floating-point model they use. */
9859 break;
9860 case EF_ARM_SOFT_FLOAT:
9861 fp_model = ARM_FLOAT_SOFT_FPA;
9862 break;
9863 case EF_ARM_VFP_FLOAT:
9864 fp_model = ARM_FLOAT_VFP;
9865 break;
9866 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
9867 fp_model = ARM_FLOAT_SOFT_VFP;
9868 break;
9869 }
9870 }
9871
9872 if (e_flags & EF_ARM_BE8)
9873 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
9874
9875 break;
9876
9877 default:
9878 /* Leave it as "auto". */
9879 break;
9880 }
9881 }
123dc839
DJ
9882
9883 /* Check any target description for validity. */
9779414d 9884 if (tdesc_has_registers (tdesc))
123dc839
DJ
9885 {
9886 /* For most registers we require GDB's default names; but also allow
9887 the numeric names for sp / lr / pc, as a convenience. */
9888 static const char *const arm_sp_names[] = { "r13", "sp", NULL };
9889 static const char *const arm_lr_names[] = { "r14", "lr", NULL };
9890 static const char *const arm_pc_names[] = { "r15", "pc", NULL };
9891
9892 const struct tdesc_feature *feature;
58d6951d 9893 int valid_p;
123dc839 9894
9779414d 9895 feature = tdesc_find_feature (tdesc,
123dc839
DJ
9896 "org.gnu.gdb.arm.core");
9897 if (feature == NULL)
9779414d
DJ
9898 {
9899 feature = tdesc_find_feature (tdesc,
9900 "org.gnu.gdb.arm.m-profile");
9901 if (feature == NULL)
9902 return NULL;
9903 else
9904 is_m = 1;
9905 }
123dc839
DJ
9906
9907 tdesc_data = tdesc_data_alloc ();
9908
9909 valid_p = 1;
9910 for (i = 0; i < ARM_SP_REGNUM; i++)
9911 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
9912 arm_register_names[i]);
9913 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
9914 ARM_SP_REGNUM,
9915 arm_sp_names);
9916 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
9917 ARM_LR_REGNUM,
9918 arm_lr_names);
9919 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
9920 ARM_PC_REGNUM,
9921 arm_pc_names);
9779414d
DJ
9922 if (is_m)
9923 valid_p &= tdesc_numbered_register (feature, tdesc_data,
9924 ARM_PS_REGNUM, "xpsr");
9925 else
9926 valid_p &= tdesc_numbered_register (feature, tdesc_data,
9927 ARM_PS_REGNUM, "cpsr");
123dc839
DJ
9928
9929 if (!valid_p)
9930 {
9931 tdesc_data_cleanup (tdesc_data);
9932 return NULL;
9933 }
9934
9779414d 9935 feature = tdesc_find_feature (tdesc,
123dc839
DJ
9936 "org.gnu.gdb.arm.fpa");
9937 if (feature != NULL)
9938 {
9939 valid_p = 1;
9940 for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
9941 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
9942 arm_register_names[i]);
9943 if (!valid_p)
9944 {
9945 tdesc_data_cleanup (tdesc_data);
9946 return NULL;
9947 }
9948 }
ff6f572f
DJ
9949 else
9950 have_fpa_registers = 0;
9951
9779414d 9952 feature = tdesc_find_feature (tdesc,
ff6f572f
DJ
9953 "org.gnu.gdb.xscale.iwmmxt");
9954 if (feature != NULL)
9955 {
9956 static const char *const iwmmxt_names[] = {
9957 "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
9958 "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
9959 "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
9960 "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
9961 };
9962
9963 valid_p = 1;
9964 for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
9965 valid_p
9966 &= tdesc_numbered_register (feature, tdesc_data, i,
9967 iwmmxt_names[i - ARM_WR0_REGNUM]);
9968
9969 /* Check for the control registers, but do not fail if they
9970 are missing. */
9971 for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
9972 tdesc_numbered_register (feature, tdesc_data, i,
9973 iwmmxt_names[i - ARM_WR0_REGNUM]);
9974
9975 for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
9976 valid_p
9977 &= tdesc_numbered_register (feature, tdesc_data, i,
9978 iwmmxt_names[i - ARM_WR0_REGNUM]);
9979
9980 if (!valid_p)
9981 {
9982 tdesc_data_cleanup (tdesc_data);
9983 return NULL;
9984 }
9985 }
58d6951d
DJ
9986
9987 /* If we have a VFP unit, check whether the single precision registers
9988 are present. If not, then we will synthesize them as pseudo
9989 registers. */
9779414d 9990 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
9991 "org.gnu.gdb.arm.vfp");
9992 if (feature != NULL)
9993 {
9994 static const char *const vfp_double_names[] = {
9995 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
9996 "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
9997 "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
9998 "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
9999 };
10000
10001 /* Require the double precision registers. There must be either
10002 16 or 32. */
10003 valid_p = 1;
10004 for (i = 0; i < 32; i++)
10005 {
10006 valid_p &= tdesc_numbered_register (feature, tdesc_data,
10007 ARM_D0_REGNUM + i,
10008 vfp_double_names[i]);
10009 if (!valid_p)
10010 break;
10011 }
2b9e5ea6
UW
10012 if (!valid_p && i == 16)
10013 valid_p = 1;
58d6951d 10014
2b9e5ea6
UW
10015 /* Also require FPSCR. */
10016 valid_p &= tdesc_numbered_register (feature, tdesc_data,
10017 ARM_FPSCR_REGNUM, "fpscr");
10018 if (!valid_p)
58d6951d
DJ
10019 {
10020 tdesc_data_cleanup (tdesc_data);
10021 return NULL;
10022 }
10023
10024 if (tdesc_unnumbered_register (feature, "s0") == 0)
10025 have_vfp_pseudos = 1;
10026
10027 have_vfp_registers = 1;
10028
10029 /* If we have VFP, also check for NEON. The architecture allows
10030 NEON without VFP (integer vector operations only), but GDB
10031 does not support that. */
9779414d 10032 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
10033 "org.gnu.gdb.arm.neon");
10034 if (feature != NULL)
10035 {
10036 /* NEON requires 32 double-precision registers. */
10037 if (i != 32)
10038 {
10039 tdesc_data_cleanup (tdesc_data);
10040 return NULL;
10041 }
10042
10043 /* If there are quad registers defined by the stub, use
10044 their type; otherwise (normally) provide them with
10045 the default type. */
10046 if (tdesc_unnumbered_register (feature, "q0") == 0)
10047 have_neon_pseudos = 1;
10048
10049 have_neon = 1;
10050 }
10051 }
123dc839 10052 }
39bbf761 10053
28e97307
DJ
10054 /* If there is already a candidate, use it. */
10055 for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
10056 best_arch != NULL;
10057 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
10058 {
b8926edc
DJ
10059 if (arm_abi != ARM_ABI_AUTO
10060 && arm_abi != gdbarch_tdep (best_arch->gdbarch)->arm_abi)
28e97307
DJ
10061 continue;
10062
b8926edc
DJ
10063 if (fp_model != ARM_FLOAT_AUTO
10064 && fp_model != gdbarch_tdep (best_arch->gdbarch)->fp_model)
28e97307
DJ
10065 continue;
10066
58d6951d
DJ
10067 /* There are various other properties in tdep that we do not
10068 need to check here: those derived from a target description,
10069 since gdbarches with a different target description are
10070 automatically disqualified. */
10071
9779414d
DJ
10072 /* Do check is_m, though, since it might come from the binary. */
10073 if (is_m != gdbarch_tdep (best_arch->gdbarch)->is_m)
10074 continue;
10075
28e97307
DJ
10076 /* Found a match. */
10077 break;
10078 }
97e03143 10079
28e97307 10080 if (best_arch != NULL)
123dc839
DJ
10081 {
10082 if (tdesc_data != NULL)
10083 tdesc_data_cleanup (tdesc_data);
10084 return best_arch->gdbarch;
10085 }
28e97307
DJ
10086
10087 tdep = xcalloc (1, sizeof (struct gdbarch_tdep));
97e03143
RE
10088 gdbarch = gdbarch_alloc (&info, tdep);
10089
28e97307
DJ
10090 /* Record additional information about the architecture we are defining.
10091 These are gdbarch discriminators, like the OSABI. */
10092 tdep->arm_abi = arm_abi;
10093 tdep->fp_model = fp_model;
9779414d 10094 tdep->is_m = is_m;
ff6f572f 10095 tdep->have_fpa_registers = have_fpa_registers;
58d6951d
DJ
10096 tdep->have_vfp_registers = have_vfp_registers;
10097 tdep->have_vfp_pseudos = have_vfp_pseudos;
10098 tdep->have_neon_pseudos = have_neon_pseudos;
10099 tdep->have_neon = have_neon;
08216dd7 10100
25f8c692
JL
10101 arm_register_g_packet_guesses (gdbarch);
10102
08216dd7 10103 /* Breakpoints. */
9d4fde75 10104 switch (info.byte_order_for_code)
67255d04
RE
10105 {
10106 case BFD_ENDIAN_BIG:
66e810cd
RE
10107 tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
10108 tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
10109 tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
10110 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
10111
67255d04
RE
10112 break;
10113
10114 case BFD_ENDIAN_LITTLE:
66e810cd
RE
10115 tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
10116 tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
10117 tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
10118 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
10119
67255d04
RE
10120 break;
10121
10122 default:
10123 internal_error (__FILE__, __LINE__,
edefbb7c 10124 _("arm_gdbarch_init: bad byte order for float format"));
67255d04
RE
10125 }
10126
d7b486e7
RE
10127 /* On ARM targets char defaults to unsigned. */
10128 set_gdbarch_char_signed (gdbarch, 0);
10129
cca44b1b
JB
10130 /* Note: for displaced stepping, this includes the breakpoint, and one word
10131 of additional scratch space. This setting isn't used for anything beside
10132 displaced stepping at present. */
10133 set_gdbarch_max_insn_length (gdbarch, 4 * DISPLACED_MODIFIED_INSNS);
10134
9df628e0 10135 /* This should be low enough for everything. */
97e03143 10136 tdep->lowest_pc = 0x20;
94c30b78 10137 tdep->jb_pc = -1; /* Longjump support not enabled by default. */
97e03143 10138
7c00367c
MK
10139 /* The default, for both APCS and AAPCS, is to return small
10140 structures in registers. */
10141 tdep->struct_return = reg_struct_return;
10142
2dd604e7 10143 set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
f53f0d0b 10144 set_gdbarch_frame_align (gdbarch, arm_frame_align);
39bbf761 10145
756fe439
DJ
10146 set_gdbarch_write_pc (gdbarch, arm_write_pc);
10147
148754e5 10148 /* Frame handling. */
a262aec2 10149 set_gdbarch_dummy_id (gdbarch, arm_dummy_id);
eb5492fa
DJ
10150 set_gdbarch_unwind_pc (gdbarch, arm_unwind_pc);
10151 set_gdbarch_unwind_sp (gdbarch, arm_unwind_sp);
10152
eb5492fa 10153 frame_base_set_default (gdbarch, &arm_normal_base);
148754e5 10154
34e8f22d
RE
10155 /* Address manipulation. */
10156 set_gdbarch_smash_text_address (gdbarch, arm_smash_text_address);
10157 set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
10158
34e8f22d
RE
10159 /* Advance PC across function entry code. */
10160 set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
10161
4024ca99
UW
10162 /* Detect whether PC is in function epilogue. */
10163 set_gdbarch_in_function_epilogue_p (gdbarch, arm_in_function_epilogue_p);
10164
190dce09
UW
10165 /* Skip trampolines. */
10166 set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
10167
34e8f22d
RE
10168 /* The stack grows downward. */
10169 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
10170
10171 /* Breakpoint manipulation. */
10172 set_gdbarch_breakpoint_from_pc (gdbarch, arm_breakpoint_from_pc);
177321bd
DJ
10173 set_gdbarch_remote_breakpoint_from_pc (gdbarch,
10174 arm_remote_breakpoint_from_pc);
34e8f22d
RE
10175
10176 /* Information about registers, etc. */
34e8f22d
RE
10177 set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
10178 set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
ff6f572f 10179 set_gdbarch_num_regs (gdbarch, ARM_NUM_REGS);
7a5ea0d4 10180 set_gdbarch_register_type (gdbarch, arm_register_type);
54483882 10181 set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);
34e8f22d 10182
ff6f572f
DJ
10183 /* This "info float" is FPA-specific. Use the generic version if we
10184 do not have FPA. */
10185 if (gdbarch_tdep (gdbarch)->have_fpa_registers)
10186 set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
10187
26216b98 10188 /* Internal <-> external register number maps. */
ff6f572f 10189 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
26216b98
AC
10190 set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
10191
34e8f22d
RE
10192 set_gdbarch_register_name (gdbarch, arm_register_name);
10193
10194 /* Returning results. */
2af48f68 10195 set_gdbarch_return_value (gdbarch, arm_return_value);
34e8f22d 10196
03d48a7d
RE
10197 /* Disassembly. */
10198 set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
10199
34e8f22d
RE
10200 /* Minsymbol frobbing. */
10201 set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
10202 set_gdbarch_coff_make_msymbol_special (gdbarch,
10203 arm_coff_make_msymbol_special);
60c5725c 10204 set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
34e8f22d 10205
f9d67f43
DJ
10206 /* Thumb-2 IT block support. */
10207 set_gdbarch_adjust_breakpoint_address (gdbarch,
10208 arm_adjust_breakpoint_address);
10209
0d5de010
DJ
10210 /* Virtual tables. */
10211 set_gdbarch_vbit_in_delta (gdbarch, 1);
10212
97e03143 10213 /* Hook in the ABI-specific overrides, if they have been registered. */
4be87837 10214 gdbarch_init_osabi (info, gdbarch);
97e03143 10215
b39cc962
DJ
10216 dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
10217
eb5492fa 10218 /* Add some default predicates. */
a262aec2
DJ
10219 frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
10220 dwarf2_append_unwinders (gdbarch);
0e9e9abd 10221 frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
a262aec2 10222 frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
eb5492fa 10223
97e03143
RE
10224 /* Now we have tuned the configuration, set a few final things,
10225 based on what the OS ABI has told us. */
10226
b8926edc
DJ
10227 /* If the ABI is not otherwise marked, assume the old GNU APCS. EABI
10228 binaries are always marked. */
10229 if (tdep->arm_abi == ARM_ABI_AUTO)
10230 tdep->arm_abi = ARM_ABI_APCS;
10231
e3039479
UW
10232 /* Watchpoints are not steppable. */
10233 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
10234
b8926edc
DJ
10235 /* We used to default to FPA for generic ARM, but almost nobody
10236 uses that now, and we now provide a way for the user to force
10237 the model. So default to the most useful variant. */
10238 if (tdep->fp_model == ARM_FLOAT_AUTO)
10239 tdep->fp_model = ARM_FLOAT_SOFT_FPA;
10240
9df628e0
RE
10241 if (tdep->jb_pc >= 0)
10242 set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
10243
08216dd7 10244 /* Floating point sizes and format. */
8da61cc4 10245 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
b8926edc 10246 if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
08216dd7 10247 {
8da61cc4
DJ
10248 set_gdbarch_double_format
10249 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10250 set_gdbarch_long_double_format
10251 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10252 }
10253 else
10254 {
10255 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
10256 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
08216dd7
RE
10257 }
10258
58d6951d
DJ
10259 if (have_vfp_pseudos)
10260 {
10261 /* NOTE: These are the only pseudo registers used by
10262 the ARM target at the moment. If more are added, a
10263 little more care in numbering will be needed. */
10264
10265 int num_pseudos = 32;
10266 if (have_neon_pseudos)
10267 num_pseudos += 16;
10268 set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
10269 set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
10270 set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
10271 }
10272
123dc839 10273 if (tdesc_data)
58d6951d
DJ
10274 {
10275 set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
10276
9779414d 10277 tdesc_use_registers (gdbarch, tdesc, tdesc_data);
58d6951d
DJ
10278
10279 /* Override tdesc_register_type to adjust the types of VFP
10280 registers for NEON. */
10281 set_gdbarch_register_type (gdbarch, arm_register_type);
10282 }
123dc839
DJ
10283
10284 /* Add standard register aliases. We add aliases even for those
10285 nanes which are used by the current architecture - it's simpler,
10286 and does no harm, since nothing ever lists user registers. */
10287 for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
10288 user_reg_add (gdbarch, arm_register_aliases[i].name,
10289 value_of_arm_user_reg, &arm_register_aliases[i].regnum);
10290
39bbf761
RE
10291 return gdbarch;
10292}
10293
97e03143 10294static void
2af46ca0 10295arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
97e03143 10296{
2af46ca0 10297 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
97e03143
RE
10298
10299 if (tdep == NULL)
10300 return;
10301
edefbb7c 10302 fprintf_unfiltered (file, _("arm_dump_tdep: Lowest pc = 0x%lx"),
97e03143
RE
10303 (unsigned long) tdep->lowest_pc);
10304}
10305
a78f21af
AC
10306extern initialize_file_ftype _initialize_arm_tdep; /* -Wmissing-prototypes */
10307
c906108c 10308void
ed9a39eb 10309_initialize_arm_tdep (void)
c906108c 10310{
bc90b915
FN
10311 struct ui_file *stb;
10312 long length;
26304000 10313 struct cmd_list_element *new_set, *new_show;
53904c9e
AC
10314 const char *setname;
10315 const char *setdesc;
4bd7b427 10316 const char *const *regnames;
bc90b915
FN
10317 int numregs, i, j;
10318 static char *helptext;
edefbb7c
AC
10319 char regdesc[1024], *rdptr = regdesc;
10320 size_t rest = sizeof (regdesc);
085dd6e6 10321
42cf1509 10322 gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
97e03143 10323
60c5725c 10324 arm_objfile_data_key
c1bd65d0 10325 = register_objfile_data_with_cleanup (NULL, arm_objfile_data_free);
60c5725c 10326
0e9e9abd
UW
10327 /* Add ourselves to objfile event chain. */
10328 observer_attach_new_objfile (arm_exidx_new_objfile);
10329 arm_exidx_data_key
10330 = register_objfile_data_with_cleanup (NULL, arm_exidx_data_free);
10331
70f80edf
JT
10332 /* Register an ELF OS ABI sniffer for ARM binaries. */
10333 gdbarch_register_osabi_sniffer (bfd_arch_arm,
10334 bfd_target_elf_flavour,
10335 arm_elf_osabi_sniffer);
10336
9779414d
DJ
10337 /* Initialize the standard target descriptions. */
10338 initialize_tdesc_arm_with_m ();
25f8c692 10339 initialize_tdesc_arm_with_m_fpa_layout ();
3184d3f9 10340 initialize_tdesc_arm_with_m_vfp_d16 ();
ef7e8358
UW
10341 initialize_tdesc_arm_with_iwmmxt ();
10342 initialize_tdesc_arm_with_vfpv2 ();
10343 initialize_tdesc_arm_with_vfpv3 ();
10344 initialize_tdesc_arm_with_neon ();
9779414d 10345
94c30b78 10346 /* Get the number of possible sets of register names defined in opcodes. */
afd7eef0
RE
10347 num_disassembly_options = get_arm_regname_num_options ();
10348
10349 /* Add root prefix command for all "set arm"/"show arm" commands. */
10350 add_prefix_cmd ("arm", no_class, set_arm_command,
edefbb7c 10351 _("Various ARM-specific commands."),
afd7eef0
RE
10352 &setarmcmdlist, "set arm ", 0, &setlist);
10353
10354 add_prefix_cmd ("arm", no_class, show_arm_command,
edefbb7c 10355 _("Various ARM-specific commands."),
afd7eef0 10356 &showarmcmdlist, "show arm ", 0, &showlist);
bc90b915 10357
94c30b78 10358 /* Sync the opcode insn printer with our register viewer. */
bc90b915 10359 parse_arm_disassembler_option ("reg-names-std");
c5aa993b 10360
eefe576e
AC
10361 /* Initialize the array that will be passed to
10362 add_setshow_enum_cmd(). */
afd7eef0
RE
10363 valid_disassembly_styles
10364 = xmalloc ((num_disassembly_options + 1) * sizeof (char *));
10365 for (i = 0; i < num_disassembly_options; i++)
bc90b915
FN
10366 {
10367 numregs = get_arm_regnames (i, &setname, &setdesc, &regnames);
afd7eef0 10368 valid_disassembly_styles[i] = setname;
edefbb7c
AC
10369 length = snprintf (rdptr, rest, "%s - %s\n", setname, setdesc);
10370 rdptr += length;
10371 rest -= length;
123dc839
DJ
10372 /* When we find the default names, tell the disassembler to use
10373 them. */
bc90b915
FN
10374 if (!strcmp (setname, "std"))
10375 {
afd7eef0 10376 disassembly_style = setname;
bc90b915
FN
10377 set_arm_regname_option (i);
10378 }
10379 }
94c30b78 10380 /* Mark the end of valid options. */
afd7eef0 10381 valid_disassembly_styles[num_disassembly_options] = NULL;
c906108c 10382
edefbb7c
AC
10383 /* Create the help text. */
10384 stb = mem_fileopen ();
10385 fprintf_unfiltered (stb, "%s%s%s",
10386 _("The valid values are:\n"),
10387 regdesc,
10388 _("The default is \"std\"."));
759ef836 10389 helptext = ui_file_xstrdup (stb, NULL);
bc90b915 10390 ui_file_delete (stb);
ed9a39eb 10391
edefbb7c
AC
10392 add_setshow_enum_cmd("disassembler", no_class,
10393 valid_disassembly_styles, &disassembly_style,
10394 _("Set the disassembly style."),
10395 _("Show the disassembly style."),
10396 helptext,
2c5b56ce 10397 set_disassembly_style_sfunc,
0963b4bd
MS
10398 NULL, /* FIXME: i18n: The disassembly style is
10399 \"%s\". */
7376b4c2 10400 &setarmcmdlist, &showarmcmdlist);
edefbb7c
AC
10401
10402 add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
10403 _("Set usage of ARM 32-bit mode."),
10404 _("Show usage of ARM 32-bit mode."),
10405 _("When off, a 26-bit PC will be used."),
2c5b56ce 10406 NULL,
0963b4bd
MS
10407 NULL, /* FIXME: i18n: Usage of ARM 32-bit
10408 mode is %s. */
26304000 10409 &setarmcmdlist, &showarmcmdlist);
c906108c 10410
fd50bc42 10411 /* Add a command to allow the user to force the FPU model. */
edefbb7c
AC
10412 add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
10413 _("Set the floating point type."),
10414 _("Show the floating point type."),
10415 _("auto - Determine the FP typefrom the OS-ABI.\n\
10416softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
10417fpa - FPA co-processor (GCC compiled).\n\
10418softvfp - Software FP with pure-endian doubles.\n\
10419vfp - VFP co-processor."),
edefbb7c 10420 set_fp_model_sfunc, show_fp_model,
7376b4c2 10421 &setarmcmdlist, &showarmcmdlist);
fd50bc42 10422
28e97307
DJ
10423 /* Add a command to allow the user to force the ABI. */
10424 add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
10425 _("Set the ABI."),
10426 _("Show the ABI."),
10427 NULL, arm_set_abi, arm_show_abi,
10428 &setarmcmdlist, &showarmcmdlist);
10429
0428b8f5
DJ
10430 /* Add two commands to allow the user to force the assumed
10431 execution mode. */
10432 add_setshow_enum_cmd ("fallback-mode", class_support,
10433 arm_mode_strings, &arm_fallback_mode_string,
10434 _("Set the mode assumed when symbols are unavailable."),
10435 _("Show the mode assumed when symbols are unavailable."),
10436 NULL, NULL, arm_show_fallback_mode,
10437 &setarmcmdlist, &showarmcmdlist);
10438 add_setshow_enum_cmd ("force-mode", class_support,
10439 arm_mode_strings, &arm_force_mode_string,
10440 _("Set the mode assumed even when symbols are available."),
10441 _("Show the mode assumed even when symbols are available."),
10442 NULL, NULL, arm_show_force_mode,
10443 &setarmcmdlist, &showarmcmdlist);
10444
6529d2dd 10445 /* Debugging flag. */
edefbb7c
AC
10446 add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
10447 _("Set ARM debugging."),
10448 _("Show ARM debugging."),
10449 _("When on, arm-specific debugging is enabled."),
2c5b56ce 10450 NULL,
7915a72c 10451 NULL, /* FIXME: i18n: "ARM debugging is %s. */
26304000 10452 &setdebuglist, &showdebuglist);
c906108c 10453}
72508ac0
PO
10454
10455/* ARM-reversible process record data structures. */
10456
10457#define ARM_INSN_SIZE_BYTES 4
10458#define THUMB_INSN_SIZE_BYTES 2
10459#define THUMB2_INSN_SIZE_BYTES 4
10460
10461
10462#define INSN_S_L_BIT_NUM 20
10463
10464#define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
10465 do \
10466 { \
10467 unsigned int reg_len = LENGTH; \
10468 if (reg_len) \
10469 { \
10470 REGS = XNEWVEC (uint32_t, reg_len); \
10471 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
10472 } \
10473 } \
10474 while (0)
10475
10476#define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
10477 do \
10478 { \
10479 unsigned int mem_len = LENGTH; \
10480 if (mem_len) \
10481 { \
10482 MEMS = XNEWVEC (struct arm_mem_r, mem_len); \
10483 memcpy(&MEMS->len, &RECORD_BUF[0], \
10484 sizeof(struct arm_mem_r) * LENGTH); \
10485 } \
10486 } \
10487 while (0)
10488
10489/* Checks whether insn is already recorded or yet to be decoded. (boolean expression). */
10490#define INSN_RECORDED(ARM_RECORD) \
10491 (0 != (ARM_RECORD)->reg_rec_count || 0 != (ARM_RECORD)->mem_rec_count)
10492
10493/* ARM memory record structure. */
10494struct arm_mem_r
10495{
10496 uint32_t len; /* Record length. */
10497 CORE_ADDR addr; /* Memory address. */
10498};
10499
10500/* ARM instruction record contains opcode of current insn
10501 and execution state (before entry to decode_insn()),
10502 contains list of to-be-modified registers and
10503 memory blocks (on return from decode_insn()). */
10504
10505typedef struct insn_decode_record_t
10506{
10507 struct gdbarch *gdbarch;
10508 struct regcache *regcache;
10509 CORE_ADDR this_addr; /* Address of the insn being decoded. */
10510 uint32_t arm_insn; /* Should accommodate thumb. */
10511 uint32_t cond; /* Condition code. */
10512 uint32_t opcode; /* Insn opcode. */
10513 uint32_t decode; /* Insn decode bits. */
10514 uint32_t mem_rec_count; /* No of mem records. */
10515 uint32_t reg_rec_count; /* No of reg records. */
10516 uint32_t *arm_regs; /* Registers to be saved for this record. */
10517 struct arm_mem_r *arm_mems; /* Memory to be saved for this record. */
10518} insn_decode_record;
10519
10520
10521/* Checks ARM SBZ and SBO mandatory fields. */
10522
10523static int
10524sbo_sbz (uint32_t insn, uint32_t bit_num, uint32_t len, uint32_t sbo)
10525{
10526 uint32_t ones = bits (insn, bit_num - 1, (bit_num -1) + (len - 1));
10527
10528 if (!len)
10529 return 1;
10530
10531 if (!sbo)
10532 ones = ~ones;
10533
10534 while (ones)
10535 {
10536 if (!(ones & sbo))
10537 {
10538 return 0;
10539 }
10540 ones = ones >> 1;
10541 }
10542 return 1;
10543}
10544
10545typedef enum
10546{
10547 ARM_RECORD_STRH=1,
10548 ARM_RECORD_STRD
10549} arm_record_strx_t;
10550
10551typedef enum
10552{
10553 ARM_RECORD=1,
10554 THUMB_RECORD,
10555 THUMB2_RECORD
10556} record_type_t;
10557
10558
10559static int
10560arm_record_strx (insn_decode_record *arm_insn_r, uint32_t *record_buf,
10561 uint32_t *record_buf_mem, arm_record_strx_t str_type)
10562{
10563
10564 struct regcache *reg_cache = arm_insn_r->regcache;
10565 ULONGEST u_regval[2]= {0};
10566
10567 uint32_t reg_src1 = 0, reg_src2 = 0;
10568 uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
10569 uint32_t opcode1 = 0;
10570
10571 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10572 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10573 opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
10574
10575
10576 if (14 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
10577 {
10578 /* 1) Handle misc store, immediate offset. */
10579 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
10580 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
10581 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10582 regcache_raw_read_unsigned (reg_cache, reg_src1,
10583 &u_regval[0]);
10584 if (ARM_PC_REGNUM == reg_src1)
10585 {
10586 /* If R15 was used as Rn, hence current PC+8. */
10587 u_regval[0] = u_regval[0] + 8;
10588 }
10589 offset_8 = (immed_high << 4) | immed_low;
10590 /* Calculate target store address. */
10591 if (14 == arm_insn_r->opcode)
10592 {
10593 tgt_mem_addr = u_regval[0] + offset_8;
10594 }
10595 else
10596 {
10597 tgt_mem_addr = u_regval[0] - offset_8;
10598 }
10599 if (ARM_RECORD_STRH == str_type)
10600 {
10601 record_buf_mem[0] = 2;
10602 record_buf_mem[1] = tgt_mem_addr;
10603 arm_insn_r->mem_rec_count = 1;
10604 }
10605 else if (ARM_RECORD_STRD == str_type)
10606 {
10607 record_buf_mem[0] = 4;
10608 record_buf_mem[1] = tgt_mem_addr;
10609 record_buf_mem[2] = 4;
10610 record_buf_mem[3] = tgt_mem_addr + 4;
10611 arm_insn_r->mem_rec_count = 2;
10612 }
10613 }
10614 else if (12 == arm_insn_r->opcode || 8 == arm_insn_r->opcode)
10615 {
10616 /* 2) Store, register offset. */
10617 /* Get Rm. */
10618 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10619 /* Get Rn. */
10620 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10621 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10622 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10623 if (15 == reg_src2)
10624 {
10625 /* If R15 was used as Rn, hence current PC+8. */
10626 u_regval[0] = u_regval[0] + 8;
10627 }
10628 /* Calculate target store address, Rn +/- Rm, register offset. */
10629 if (12 == arm_insn_r->opcode)
10630 {
10631 tgt_mem_addr = u_regval[0] + u_regval[1];
10632 }
10633 else
10634 {
10635 tgt_mem_addr = u_regval[1] - u_regval[0];
10636 }
10637 if (ARM_RECORD_STRH == str_type)
10638 {
10639 record_buf_mem[0] = 2;
10640 record_buf_mem[1] = tgt_mem_addr;
10641 arm_insn_r->mem_rec_count = 1;
10642 }
10643 else if (ARM_RECORD_STRD == str_type)
10644 {
10645 record_buf_mem[0] = 4;
10646 record_buf_mem[1] = tgt_mem_addr;
10647 record_buf_mem[2] = 4;
10648 record_buf_mem[3] = tgt_mem_addr + 4;
10649 arm_insn_r->mem_rec_count = 2;
10650 }
10651 }
10652 else if (11 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
10653 || 2 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
10654 {
10655 /* 3) Store, immediate pre-indexed. */
10656 /* 5) Store, immediate post-indexed. */
10657 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
10658 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
10659 offset_8 = (immed_high << 4) | immed_low;
10660 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10661 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10662 /* Calculate target store address, Rn +/- Rm, register offset. */
10663 if (15 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
10664 {
10665 tgt_mem_addr = u_regval[0] + offset_8;
10666 }
10667 else
10668 {
10669 tgt_mem_addr = u_regval[0] - offset_8;
10670 }
10671 if (ARM_RECORD_STRH == str_type)
10672 {
10673 record_buf_mem[0] = 2;
10674 record_buf_mem[1] = tgt_mem_addr;
10675 arm_insn_r->mem_rec_count = 1;
10676 }
10677 else if (ARM_RECORD_STRD == str_type)
10678 {
10679 record_buf_mem[0] = 4;
10680 record_buf_mem[1] = tgt_mem_addr;
10681 record_buf_mem[2] = 4;
10682 record_buf_mem[3] = tgt_mem_addr + 4;
10683 arm_insn_r->mem_rec_count = 2;
10684 }
10685 /* Record Rn also as it changes. */
10686 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
10687 arm_insn_r->reg_rec_count = 1;
10688 }
10689 else if (9 == arm_insn_r->opcode || 13 == arm_insn_r->opcode
10690 || 0 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
10691 {
10692 /* 4) Store, register pre-indexed. */
10693 /* 6) Store, register post -indexed. */
10694 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10695 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10696 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10697 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10698 /* Calculate target store address, Rn +/- Rm, register offset. */
10699 if (13 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
10700 {
10701 tgt_mem_addr = u_regval[0] + u_regval[1];
10702 }
10703 else
10704 {
10705 tgt_mem_addr = u_regval[1] - u_regval[0];
10706 }
10707 if (ARM_RECORD_STRH == str_type)
10708 {
10709 record_buf_mem[0] = 2;
10710 record_buf_mem[1] = tgt_mem_addr;
10711 arm_insn_r->mem_rec_count = 1;
10712 }
10713 else if (ARM_RECORD_STRD == str_type)
10714 {
10715 record_buf_mem[0] = 4;
10716 record_buf_mem[1] = tgt_mem_addr;
10717 record_buf_mem[2] = 4;
10718 record_buf_mem[3] = tgt_mem_addr + 4;
10719 arm_insn_r->mem_rec_count = 2;
10720 }
10721 /* Record Rn also as it changes. */
10722 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
10723 arm_insn_r->reg_rec_count = 1;
10724 }
10725 return 0;
10726}
10727
10728/* Handling ARM extension space insns. */
10729
10730static int
10731arm_record_extension_space (insn_decode_record *arm_insn_r)
10732{
10733 uint32_t ret = 0; /* Return value: -1:record failure ; 0:success */
10734 uint32_t opcode1 = 0, opcode2 = 0, insn_op1 = 0;
10735 uint32_t record_buf[8], record_buf_mem[8];
10736 uint32_t reg_src1 = 0;
10737 uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
10738 struct regcache *reg_cache = arm_insn_r->regcache;
10739 ULONGEST u_regval = 0;
10740
10741 gdb_assert (!INSN_RECORDED(arm_insn_r));
10742 /* Handle unconditional insn extension space. */
10743
10744 opcode1 = bits (arm_insn_r->arm_insn, 20, 27);
10745 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
10746 if (arm_insn_r->cond)
10747 {
10748 /* PLD has no affect on architectural state, it just affects
10749 the caches. */
10750 if (5 == ((opcode1 & 0xE0) >> 5))
10751 {
10752 /* BLX(1) */
10753 record_buf[0] = ARM_PS_REGNUM;
10754 record_buf[1] = ARM_LR_REGNUM;
10755 arm_insn_r->reg_rec_count = 2;
10756 }
10757 /* STC2, LDC2, MCR2, MRC2, CDP2: <TBD>, co-processor insn. */
10758 }
10759
10760
10761 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
10762 if (3 == opcode1 && bit (arm_insn_r->arm_insn, 4))
10763 {
10764 ret = -1;
10765 /* Undefined instruction on ARM V5; need to handle if later
10766 versions define it. */
10767 }
10768
10769 opcode1 = bits (arm_insn_r->arm_insn, 24, 27);
10770 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
10771 insn_op1 = bits (arm_insn_r->arm_insn, 20, 23);
10772
10773 /* Handle arithmetic insn extension space. */
10774 if (!opcode1 && 9 == opcode2 && 1 != arm_insn_r->cond
10775 && !INSN_RECORDED(arm_insn_r))
10776 {
10777 /* Handle MLA(S) and MUL(S). */
10778 if (0 <= insn_op1 && 3 >= insn_op1)
10779 {
10780 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10781 record_buf[1] = ARM_PS_REGNUM;
10782 arm_insn_r->reg_rec_count = 2;
10783 }
10784 else if (4 <= insn_op1 && 15 >= insn_op1)
10785 {
10786 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
10787 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
10788 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
10789 record_buf[2] = ARM_PS_REGNUM;
10790 arm_insn_r->reg_rec_count = 3;
10791 }
10792 }
10793
10794 opcode1 = bits (arm_insn_r->arm_insn, 26, 27);
10795 opcode2 = bits (arm_insn_r->arm_insn, 23, 24);
10796 insn_op1 = bits (arm_insn_r->arm_insn, 21, 22);
10797
10798 /* Handle control insn extension space. */
10799
10800 if (!opcode1 && 2 == opcode2 && !bit (arm_insn_r->arm_insn, 20)
10801 && 1 != arm_insn_r->cond && !INSN_RECORDED(arm_insn_r))
10802 {
10803 if (!bit (arm_insn_r->arm_insn,25))
10804 {
10805 if (!bits (arm_insn_r->arm_insn, 4, 7))
10806 {
10807 if ((0 == insn_op1) || (2 == insn_op1))
10808 {
10809 /* MRS. */
10810 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10811 arm_insn_r->reg_rec_count = 1;
10812 }
10813 else if (1 == insn_op1)
10814 {
10815 /* CSPR is going to be changed. */
10816 record_buf[0] = ARM_PS_REGNUM;
10817 arm_insn_r->reg_rec_count = 1;
10818 }
10819 else if (3 == insn_op1)
10820 {
10821 /* SPSR is going to be changed. */
10822 /* We need to get SPSR value, which is yet to be done. */
10823 printf_unfiltered (_("Process record does not support "
10824 "instruction 0x%0x at address %s.\n"),
10825 arm_insn_r->arm_insn,
10826 paddress (arm_insn_r->gdbarch,
10827 arm_insn_r->this_addr));
10828 return -1;
10829 }
10830 }
10831 else if (1 == bits (arm_insn_r->arm_insn, 4, 7))
10832 {
10833 if (1 == insn_op1)
10834 {
10835 /* BX. */
10836 record_buf[0] = ARM_PS_REGNUM;
10837 arm_insn_r->reg_rec_count = 1;
10838 }
10839 else if (3 == insn_op1)
10840 {
10841 /* CLZ. */
10842 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10843 arm_insn_r->reg_rec_count = 1;
10844 }
10845 }
10846 else if (3 == bits (arm_insn_r->arm_insn, 4, 7))
10847 {
10848 /* BLX. */
10849 record_buf[0] = ARM_PS_REGNUM;
10850 record_buf[1] = ARM_LR_REGNUM;
10851 arm_insn_r->reg_rec_count = 2;
10852 }
10853 else if (5 == bits (arm_insn_r->arm_insn, 4, 7))
10854 {
10855 /* QADD, QSUB, QDADD, QDSUB */
10856 record_buf[0] = ARM_PS_REGNUM;
10857 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
10858 arm_insn_r->reg_rec_count = 2;
10859 }
10860 else if (7 == bits (arm_insn_r->arm_insn, 4, 7))
10861 {
10862 /* BKPT. */
10863 record_buf[0] = ARM_PS_REGNUM;
10864 record_buf[1] = ARM_LR_REGNUM;
10865 arm_insn_r->reg_rec_count = 2;
10866
10867 /* Save SPSR also;how? */
10868 printf_unfiltered (_("Process record does not support "
10869 "instruction 0x%0x at address %s.\n"),
10870 arm_insn_r->arm_insn,
10871 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
10872 return -1;
10873 }
10874 else if(8 == bits (arm_insn_r->arm_insn, 4, 7)
10875 || 10 == bits (arm_insn_r->arm_insn, 4, 7)
10876 || 12 == bits (arm_insn_r->arm_insn, 4, 7)
10877 || 14 == bits (arm_insn_r->arm_insn, 4, 7)
10878 )
10879 {
10880 if (0 == insn_op1 || 1 == insn_op1)
10881 {
10882 /* SMLA<x><y>, SMLAW<y>, SMULW<y>. */
10883 /* We dont do optimization for SMULW<y> where we
10884 need only Rd. */
10885 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10886 record_buf[1] = ARM_PS_REGNUM;
10887 arm_insn_r->reg_rec_count = 2;
10888 }
10889 else if (2 == insn_op1)
10890 {
10891 /* SMLAL<x><y>. */
10892 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10893 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
10894 arm_insn_r->reg_rec_count = 2;
10895 }
10896 else if (3 == insn_op1)
10897 {
10898 /* SMUL<x><y>. */
10899 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10900 arm_insn_r->reg_rec_count = 1;
10901 }
10902 }
10903 }
10904 else
10905 {
10906 /* MSR : immediate form. */
10907 if (1 == insn_op1)
10908 {
10909 /* CSPR is going to be changed. */
10910 record_buf[0] = ARM_PS_REGNUM;
10911 arm_insn_r->reg_rec_count = 1;
10912 }
10913 else if (3 == insn_op1)
10914 {
10915 /* SPSR is going to be changed. */
10916 /* we need to get SPSR value, which is yet to be done */
10917 printf_unfiltered (_("Process record does not support "
10918 "instruction 0x%0x at address %s.\n"),
10919 arm_insn_r->arm_insn,
10920 paddress (arm_insn_r->gdbarch,
10921 arm_insn_r->this_addr));
10922 return -1;
10923 }
10924 }
10925 }
10926
10927 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
10928 opcode2 = bits (arm_insn_r->arm_insn, 20, 24);
10929 insn_op1 = bits (arm_insn_r->arm_insn, 5, 6);
10930
10931 /* Handle load/store insn extension space. */
10932
10933 if (!opcode1 && bit (arm_insn_r->arm_insn, 7)
10934 && bit (arm_insn_r->arm_insn, 4) && 1 != arm_insn_r->cond
10935 && !INSN_RECORDED(arm_insn_r))
10936 {
10937 /* SWP/SWPB. */
10938 if (0 == insn_op1)
10939 {
10940 /* These insn, changes register and memory as well. */
10941 /* SWP or SWPB insn. */
10942 /* Get memory address given by Rn. */
10943 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10944 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
10945 /* SWP insn ?, swaps word. */
10946 if (8 == arm_insn_r->opcode)
10947 {
10948 record_buf_mem[0] = 4;
10949 }
10950 else
10951 {
10952 /* SWPB insn, swaps only byte. */
10953 record_buf_mem[0] = 1;
10954 }
10955 record_buf_mem[1] = u_regval;
10956 arm_insn_r->mem_rec_count = 1;
10957 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10958 arm_insn_r->reg_rec_count = 1;
10959 }
10960 else if (1 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
10961 {
10962 /* STRH. */
10963 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
10964 ARM_RECORD_STRH);
10965 }
10966 else if (2 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
10967 {
10968 /* LDRD. */
10969 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10970 record_buf[1] = record_buf[0] + 1;
10971 arm_insn_r->reg_rec_count = 2;
10972 }
10973 else if (3 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
10974 {
10975 /* STRD. */
10976 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
10977 ARM_RECORD_STRD);
10978 }
10979 else if (bit (arm_insn_r->arm_insn, 20) && insn_op1 <= 3)
10980 {
10981 /* LDRH, LDRSB, LDRSH. */
10982 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10983 arm_insn_r->reg_rec_count = 1;
10984 }
10985
10986 }
10987
10988 opcode1 = bits (arm_insn_r->arm_insn, 23, 27);
10989 if (24 == opcode1 && bit (arm_insn_r->arm_insn, 21)
10990 && !INSN_RECORDED(arm_insn_r))
10991 {
10992 ret = -1;
10993 /* Handle coprocessor insn extension space. */
10994 }
10995
10996 /* To be done for ARMv5 and later; as of now we return -1. */
10997 if (-1 == ret)
10998 printf_unfiltered (_("Process record does not support instruction x%0x "
10999 "at address %s.\n"),arm_insn_r->arm_insn,
11000 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11001
11002
11003 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11004 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11005
11006 return ret;
11007}
11008
11009/* Handling opcode 000 insns. */
11010
11011static int
11012arm_record_data_proc_misc_ld_str (insn_decode_record *arm_insn_r)
11013{
11014 struct regcache *reg_cache = arm_insn_r->regcache;
11015 uint32_t record_buf[8], record_buf_mem[8];
11016 ULONGEST u_regval[2] = {0};
11017
11018 uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
11019 uint32_t immed_high = 0, immed_low = 0, offset_8 = 0, tgt_mem_addr = 0;
11020 uint32_t opcode1 = 0;
11021
11022 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11023 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11024 opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
11025
11026 /* Data processing insn /multiply insn. */
11027 if (9 == arm_insn_r->decode
11028 && ((4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11029 || (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)))
11030 {
11031 /* Handle multiply instructions. */
11032 /* MLA, MUL, SMLAL, SMULL, UMLAL, UMULL. */
11033 if (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)
11034 {
11035 /* Handle MLA and MUL. */
11036 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11037 record_buf[1] = ARM_PS_REGNUM;
11038 arm_insn_r->reg_rec_count = 2;
11039 }
11040 else if (4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11041 {
11042 /* Handle SMLAL, SMULL, UMLAL, UMULL. */
11043 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11044 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11045 record_buf[2] = ARM_PS_REGNUM;
11046 arm_insn_r->reg_rec_count = 3;
11047 }
11048 }
11049 else if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11050 && (11 == arm_insn_r->decode || 13 == arm_insn_r->decode))
11051 {
11052 /* Handle misc load insns, as 20th bit (L = 1). */
11053 /* LDR insn has a capability to do branching, if
11054 MOV LR, PC is precceded by LDR insn having Rn as R15
11055 in that case, it emulates branch and link insn, and hence we
11056 need to save CSPR and PC as well. I am not sure this is right
11057 place; as opcode = 010 LDR insn make this happen, if R15 was
11058 used. */
11059 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11060 if (15 != reg_dest)
11061 {
11062 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11063 arm_insn_r->reg_rec_count = 1;
11064 }
11065 else
11066 {
11067 record_buf[0] = reg_dest;
11068 record_buf[1] = ARM_PS_REGNUM;
11069 arm_insn_r->reg_rec_count = 2;
11070 }
11071 }
11072 else if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11073 && sbo_sbz (arm_insn_r->arm_insn, 5, 12, 0)
11074 && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
11075 && 2 == bits (arm_insn_r->arm_insn, 20, 21))
11076 {
11077 /* Handle MSR insn. */
11078 if (9 == arm_insn_r->opcode)
11079 {
11080 /* CSPR is going to be changed. */
11081 record_buf[0] = ARM_PS_REGNUM;
11082 arm_insn_r->reg_rec_count = 1;
11083 }
11084 else
11085 {
11086 /* SPSR is going to be changed. */
11087 /* How to read SPSR value? */
11088 printf_unfiltered (_("Process record does not support instruction "
11089 "0x%0x at address %s.\n"),
11090 arm_insn_r->arm_insn,
11091 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11092 return -1;
11093 }
11094 }
11095 else if (9 == arm_insn_r->decode
11096 && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11097 && !bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11098 {
11099 /* Handling SWP, SWPB. */
11100 /* These insn, changes register and memory as well. */
11101 /* SWP or SWPB insn. */
11102
11103 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11104 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11105 /* SWP insn ?, swaps word. */
11106 if (8 == arm_insn_r->opcode)
11107 {
11108 record_buf_mem[0] = 4;
11109 }
11110 else
11111 {
11112 /* SWPB insn, swaps only byte. */
11113 record_buf_mem[0] = 1;
11114 }
11115 record_buf_mem[1] = u_regval[0];
11116 arm_insn_r->mem_rec_count = 1;
11117 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11118 arm_insn_r->reg_rec_count = 1;
11119 }
11120 else if (3 == arm_insn_r->decode && 0x12 == opcode1
11121 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11122 {
11123 /* Handle BLX, branch and link/exchange. */
11124 if (9 == arm_insn_r->opcode)
11125 {
11126 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm,
11127 and R14 stores the return address. */
11128 record_buf[0] = ARM_PS_REGNUM;
11129 record_buf[1] = ARM_LR_REGNUM;
11130 arm_insn_r->reg_rec_count = 2;
11131 }
11132 }
11133 else if (7 == arm_insn_r->decode && 0x12 == opcode1)
11134 {
11135 /* Handle enhanced software breakpoint insn, BKPT. */
11136 /* CPSR is changed to be executed in ARM state, disabling normal
11137 interrupts, entering abort mode. */
11138 /* According to high vector configuration PC is set. */
11139 /* user hit breakpoint and type reverse, in
11140 that case, we need to go back with previous CPSR and
11141 Program Counter. */
11142 record_buf[0] = ARM_PS_REGNUM;
11143 record_buf[1] = ARM_LR_REGNUM;
11144 arm_insn_r->reg_rec_count = 2;
11145
11146 /* Save SPSR also; how? */
11147 printf_unfiltered (_("Process record does not support instruction "
11148 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
11149 paddress (arm_insn_r->gdbarch,
11150 arm_insn_r->this_addr));
11151 return -1;
11152 }
11153 else if (11 == arm_insn_r->decode
11154 && !bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11155 {
11156 /* Handle enhanced store insns and DSP insns (e.g. LDRD). */
11157
11158 /* Handle str(x) insn */
11159 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11160 ARM_RECORD_STRH);
11161 }
11162 else if (1 == arm_insn_r->decode && 0x12 == opcode1
11163 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11164 {
11165 /* Handle BX, branch and link/exchange. */
11166 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm. */
11167 record_buf[0] = ARM_PS_REGNUM;
11168 arm_insn_r->reg_rec_count = 1;
11169 }
11170 else if (1 == arm_insn_r->decode && 0x16 == opcode1
11171 && sbo_sbz (arm_insn_r->arm_insn, 9, 4, 1)
11172 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1))
11173 {
11174 /* Count leading zeros: CLZ. */
11175 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11176 arm_insn_r->reg_rec_count = 1;
11177 }
11178 else if (!bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11179 && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11180 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1)
11181 && sbo_sbz (arm_insn_r->arm_insn, 1, 12, 0)
11182 )
11183 {
11184 /* Handle MRS insn. */
11185 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11186 arm_insn_r->reg_rec_count = 1;
11187 }
11188 else if (arm_insn_r->opcode <= 15)
11189 {
11190 /* Normal data processing insns. */
11191 /* Out of 11 shifter operands mode, all the insn modifies destination
11192 register, which is specified by 13-16 decode. */
11193 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11194 record_buf[1] = ARM_PS_REGNUM;
11195 arm_insn_r->reg_rec_count = 2;
11196 }
11197 else
11198 {
11199 return -1;
11200 }
11201
11202 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11203 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11204 return 0;
11205}
11206
11207/* Handling opcode 001 insns. */
11208
11209static int
11210arm_record_data_proc_imm (insn_decode_record *arm_insn_r)
11211{
11212 uint32_t record_buf[8], record_buf_mem[8];
11213
11214 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11215 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11216
11217 if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11218 && 2 == bits (arm_insn_r->arm_insn, 20, 21)
11219 && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
11220 )
11221 {
11222 /* Handle MSR insn. */
11223 if (9 == arm_insn_r->opcode)
11224 {
11225 /* CSPR is going to be changed. */
11226 record_buf[0] = ARM_PS_REGNUM;
11227 arm_insn_r->reg_rec_count = 1;
11228 }
11229 else
11230 {
11231 /* SPSR is going to be changed. */
11232 }
11233 }
11234 else if (arm_insn_r->opcode <= 15)
11235 {
11236 /* Normal data processing insns. */
11237 /* Out of 11 shifter operands mode, all the insn modifies destination
11238 register, which is specified by 13-16 decode. */
11239 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11240 record_buf[1] = ARM_PS_REGNUM;
11241 arm_insn_r->reg_rec_count = 2;
11242 }
11243 else
11244 {
11245 return -1;
11246 }
11247
11248 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11249 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11250 return 0;
11251}
11252
11253/* Handling opcode 010 insns. */
11254
11255static int
11256arm_record_ld_st_imm_offset (insn_decode_record *arm_insn_r)
11257{
11258 struct regcache *reg_cache = arm_insn_r->regcache;
11259
11260 uint32_t reg_src1 = 0 , reg_dest = 0;
11261 uint32_t offset_12 = 0, tgt_mem_addr = 0;
11262 uint32_t record_buf[8], record_buf_mem[8];
11263
11264 ULONGEST u_regval = 0;
11265
11266 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11267 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11268
11269 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11270 {
11271 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11272 /* LDR insn has a capability to do branching, if
11273 MOV LR, PC is precedded by LDR insn having Rn as R15
11274 in that case, it emulates branch and link insn, and hence we
11275 need to save CSPR and PC as well. */
11276 if (ARM_PC_REGNUM != reg_dest)
11277 {
11278 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11279 arm_insn_r->reg_rec_count = 1;
11280 }
11281 else
11282 {
11283 record_buf[0] = reg_dest;
11284 record_buf[1] = ARM_PS_REGNUM;
11285 arm_insn_r->reg_rec_count = 2;
11286 }
11287 }
11288 else
11289 {
11290 /* Store, immediate offset, immediate pre-indexed,
11291 immediate post-indexed. */
11292 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11293 offset_12 = bits (arm_insn_r->arm_insn, 0, 11);
11294 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11295 /* U == 1 */
11296 if (bit (arm_insn_r->arm_insn, 23))
11297 {
11298 tgt_mem_addr = u_regval + offset_12;
11299 }
11300 else
11301 {
11302 tgt_mem_addr = u_regval - offset_12;
11303 }
11304
11305 switch (arm_insn_r->opcode)
11306 {
11307 /* STR. */
11308 case 8:
11309 case 12:
11310 /* STR. */
11311 case 9:
11312 case 13:
11313 /* STRT. */
11314 case 1:
11315 case 5:
11316 /* STR. */
11317 case 4:
11318 case 0:
11319 record_buf_mem[0] = 4;
11320 break;
11321
11322 /* STRB. */
11323 case 10:
11324 case 14:
11325 /* STRB. */
11326 case 11:
11327 case 15:
11328 /* STRBT. */
11329 case 3:
11330 case 7:
11331 /* STRB. */
11332 case 2:
11333 case 6:
11334 record_buf_mem[0] = 1;
11335 break;
11336
11337 default:
11338 gdb_assert_not_reached ("no decoding pattern found");
11339 break;
11340 }
11341 record_buf_mem[1] = tgt_mem_addr;
11342 arm_insn_r->mem_rec_count = 1;
11343
11344 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11345 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11346 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11347 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11348 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11349 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11350 )
11351 {
11352 /* We are handling pre-indexed mode; post-indexed mode;
11353 where Rn is going to be changed. */
11354 record_buf[0] = reg_src1;
11355 arm_insn_r->reg_rec_count = 1;
11356 }
11357 }
11358
11359 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11360 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11361 return 0;
11362}
11363
11364/* Handling opcode 011 insns. */
11365
11366static int
11367arm_record_ld_st_reg_offset (insn_decode_record *arm_insn_r)
11368{
11369 struct regcache *reg_cache = arm_insn_r->regcache;
11370
11371 uint32_t shift_imm = 0;
11372 uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
11373 uint32_t offset_12 = 0, tgt_mem_addr = 0;
11374 uint32_t record_buf[8], record_buf_mem[8];
11375
11376 LONGEST s_word;
11377 ULONGEST u_regval[2];
11378
11379 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11380 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11381
11382 /* Handle enhanced store insns and LDRD DSP insn,
11383 order begins according to addressing modes for store insns
11384 STRH insn. */
11385
11386 /* LDR or STR? */
11387 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11388 {
11389 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11390 /* LDR insn has a capability to do branching, if
11391 MOV LR, PC is precedded by LDR insn having Rn as R15
11392 in that case, it emulates branch and link insn, and hence we
11393 need to save CSPR and PC as well. */
11394 if (15 != reg_dest)
11395 {
11396 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11397 arm_insn_r->reg_rec_count = 1;
11398 }
11399 else
11400 {
11401 record_buf[0] = reg_dest;
11402 record_buf[1] = ARM_PS_REGNUM;
11403 arm_insn_r->reg_rec_count = 2;
11404 }
11405 }
11406 else
11407 {
11408 if (! bits (arm_insn_r->arm_insn, 4, 11))
11409 {
11410 /* Store insn, register offset and register pre-indexed,
11411 register post-indexed. */
11412 /* Get Rm. */
11413 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11414 /* Get Rn. */
11415 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11416 regcache_raw_read_unsigned (reg_cache, reg_src1
11417 , &u_regval[0]);
11418 regcache_raw_read_unsigned (reg_cache, reg_src2
11419 , &u_regval[1]);
11420 if (15 == reg_src2)
11421 {
11422 /* If R15 was used as Rn, hence current PC+8. */
11423 /* Pre-indexed mode doesnt reach here ; illegal insn. */
11424 u_regval[0] = u_regval[0] + 8;
11425 }
11426 /* Calculate target store address, Rn +/- Rm, register offset. */
11427 /* U == 1. */
11428 if (bit (arm_insn_r->arm_insn, 23))
11429 {
11430 tgt_mem_addr = u_regval[0] + u_regval[1];
11431 }
11432 else
11433 {
11434 tgt_mem_addr = u_regval[1] - u_regval[0];
11435 }
11436
11437 switch (arm_insn_r->opcode)
11438 {
11439 /* STR. */
11440 case 8:
11441 case 12:
11442 /* STR. */
11443 case 9:
11444 case 13:
11445 /* STRT. */
11446 case 1:
11447 case 5:
11448 /* STR. */
11449 case 0:
11450 case 4:
11451 record_buf_mem[0] = 4;
11452 break;
11453
11454 /* STRB. */
11455 case 10:
11456 case 14:
11457 /* STRB. */
11458 case 11:
11459 case 15:
11460 /* STRBT. */
11461 case 3:
11462 case 7:
11463 /* STRB. */
11464 case 2:
11465 case 6:
11466 record_buf_mem[0] = 1;
11467 break;
11468
11469 default:
11470 gdb_assert_not_reached ("no decoding pattern found");
11471 break;
11472 }
11473 record_buf_mem[1] = tgt_mem_addr;
11474 arm_insn_r->mem_rec_count = 1;
11475
11476 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11477 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11478 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11479 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11480 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11481 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11482 )
11483 {
11484 /* Rn is going to be changed in pre-indexed mode and
11485 post-indexed mode as well. */
11486 record_buf[0] = reg_src2;
11487 arm_insn_r->reg_rec_count = 1;
11488 }
11489 }
11490 else
11491 {
11492 /* Store insn, scaled register offset; scaled pre-indexed. */
11493 offset_12 = bits (arm_insn_r->arm_insn, 5, 6);
11494 /* Get Rm. */
11495 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11496 /* Get Rn. */
11497 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11498 /* Get shift_imm. */
11499 shift_imm = bits (arm_insn_r->arm_insn, 7, 11);
11500 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11501 regcache_raw_read_signed (reg_cache, reg_src1, &s_word);
11502 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11503 /* Offset_12 used as shift. */
11504 switch (offset_12)
11505 {
11506 case 0:
11507 /* Offset_12 used as index. */
11508 offset_12 = u_regval[0] << shift_imm;
11509 break;
11510
11511 case 1:
11512 offset_12 = (!shift_imm)?0:u_regval[0] >> shift_imm;
11513 break;
11514
11515 case 2:
11516 if (!shift_imm)
11517 {
11518 if (bit (u_regval[0], 31))
11519 {
11520 offset_12 = 0xFFFFFFFF;
11521 }
11522 else
11523 {
11524 offset_12 = 0;
11525 }
11526 }
11527 else
11528 {
11529 /* This is arithmetic shift. */
11530 offset_12 = s_word >> shift_imm;
11531 }
11532 break;
11533
11534 case 3:
11535 if (!shift_imm)
11536 {
11537 regcache_raw_read_unsigned (reg_cache, ARM_PS_REGNUM,
11538 &u_regval[1]);
11539 /* Get C flag value and shift it by 31. */
11540 offset_12 = (((bit (u_regval[1], 29)) << 31) \
11541 | (u_regval[0]) >> 1);
11542 }
11543 else
11544 {
11545 offset_12 = (u_regval[0] >> shift_imm) \
11546 | (u_regval[0] <<
11547 (sizeof(uint32_t) - shift_imm));
11548 }
11549 break;
11550
11551 default:
11552 gdb_assert_not_reached ("no decoding pattern found");
11553 break;
11554 }
11555
11556 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11557 /* bit U set. */
11558 if (bit (arm_insn_r->arm_insn, 23))
11559 {
11560 tgt_mem_addr = u_regval[1] + offset_12;
11561 }
11562 else
11563 {
11564 tgt_mem_addr = u_regval[1] - offset_12;
11565 }
11566
11567 switch (arm_insn_r->opcode)
11568 {
11569 /* STR. */
11570 case 8:
11571 case 12:
11572 /* STR. */
11573 case 9:
11574 case 13:
11575 /* STRT. */
11576 case 1:
11577 case 5:
11578 /* STR. */
11579 case 0:
11580 case 4:
11581 record_buf_mem[0] = 4;
11582 break;
11583
11584 /* STRB. */
11585 case 10:
11586 case 14:
11587 /* STRB. */
11588 case 11:
11589 case 15:
11590 /* STRBT. */
11591 case 3:
11592 case 7:
11593 /* STRB. */
11594 case 2:
11595 case 6:
11596 record_buf_mem[0] = 1;
11597 break;
11598
11599 default:
11600 gdb_assert_not_reached ("no decoding pattern found");
11601 break;
11602 }
11603 record_buf_mem[1] = tgt_mem_addr;
11604 arm_insn_r->mem_rec_count = 1;
11605
11606 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11607 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11608 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11609 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11610 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11611 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11612 )
11613 {
11614 /* Rn is going to be changed in register scaled pre-indexed
11615 mode,and scaled post indexed mode. */
11616 record_buf[0] = reg_src2;
11617 arm_insn_r->reg_rec_count = 1;
11618 }
11619 }
11620 }
11621
11622 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11623 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11624 return 0;
11625}
11626
11627/* Handling opcode 100 insns. */
11628
11629static int
11630arm_record_ld_st_multiple (insn_decode_record *arm_insn_r)
11631{
11632 struct regcache *reg_cache = arm_insn_r->regcache;
11633
11634 uint32_t register_list[16] = {0}, register_count = 0, register_bits = 0;
11635 uint32_t reg_src1 = 0, addr_mode = 0, no_of_regs = 0;
11636 uint32_t start_address = 0, index = 0;
11637 uint32_t record_buf[24], record_buf_mem[48];
11638
11639 ULONGEST u_regval[2] = {0};
11640
11641 /* This mode is exclusively for load and store multiple. */
11642 /* Handle incremenrt after/before and decrment after.before mode;
11643 Rn is changing depending on W bit, but as of now we store Rn too
11644 without optimization. */
11645
11646 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11647 {
11648 /* LDM (1,2,3) where LDM (3) changes CPSR too. */
11649
11650 if (bit (arm_insn_r->arm_insn, 20) && !bit (arm_insn_r->arm_insn, 22))
11651 {
11652 register_bits = bits (arm_insn_r->arm_insn, 0, 15);
11653 no_of_regs = 15;
11654 }
11655 else
11656 {
11657 register_bits = bits (arm_insn_r->arm_insn, 0, 14);
11658 no_of_regs = 14;
11659 }
11660 /* Get Rn. */
11661 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11662 while (register_bits)
11663 {
11664 if (register_bits & 0x00000001)
11665 register_list[register_count++] = 1;
11666 register_bits = register_bits >> 1;
11667 }
11668
11669 /* Extra space for Base Register and CPSR; wihtout optimization. */
11670 record_buf[register_count] = reg_src1;
11671 record_buf[register_count + 1] = ARM_PS_REGNUM;
11672 arm_insn_r->reg_rec_count = register_count + 2;
11673
11674 for (register_count = 0; register_count < no_of_regs; register_count++)
11675 {
11676 if (register_list[register_count])
11677 {
11678 /* Register_count gives total no of registers
11679 and dually working as reg number. */
11680 record_buf[index] = register_count;
11681 index++;
11682 }
11683 }
11684
11685 }
11686 else
11687 {
11688 /* It handles both STM(1) and STM(2). */
11689 addr_mode = bits (arm_insn_r->arm_insn, 23, 24);
11690
11691 register_bits = bits (arm_insn_r->arm_insn, 0, 15);
11692 /* Get Rn. */
11693 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11694 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11695 while (register_bits)
11696 {
11697 if (register_bits & 0x00000001)
11698 register_count++;
11699 register_bits = register_bits >> 1;
11700 }
11701
11702 switch (addr_mode)
11703 {
11704 /* Decrement after. */
11705 case 0:
11706 start_address = (u_regval[0]) - (register_count * 4) + 4;
11707 arm_insn_r->mem_rec_count = register_count;
11708 while (register_count)
11709 {
11710 record_buf_mem[(register_count * 2) - 1] = start_address;
11711 record_buf_mem[(register_count * 2) - 2] = 4;
11712 start_address = start_address + 4;
11713 register_count--;
11714 }
11715 break;
11716
11717 /* Increment after. */
11718 case 1:
11719 start_address = u_regval[0];
11720 arm_insn_r->mem_rec_count = register_count;
11721 while (register_count)
11722 {
11723 record_buf_mem[(register_count * 2) - 1] = start_address;
11724 record_buf_mem[(register_count * 2) - 2] = 4;
11725 start_address = start_address + 4;
11726 register_count--;
11727 }
11728 break;
11729
11730 /* Decrement before. */
11731 case 2:
11732
11733 start_address = (u_regval[0]) - (register_count * 4);
11734 arm_insn_r->mem_rec_count = register_count;
11735 while (register_count)
11736 {
11737 record_buf_mem[(register_count * 2) - 1] = start_address;
11738 record_buf_mem[(register_count * 2) - 2] = 4;
11739 start_address = start_address + 4;
11740 register_count--;
11741 }
11742 break;
11743
11744 /* Increment before. */
11745 case 3:
11746 start_address = u_regval[0] + 4;
11747 arm_insn_r->mem_rec_count = register_count;
11748 while (register_count)
11749 {
11750 record_buf_mem[(register_count * 2) - 1] = start_address;
11751 record_buf_mem[(register_count * 2) - 2] = 4;
11752 start_address = start_address + 4;
11753 register_count--;
11754 }
11755 break;
11756
11757 default:
11758 gdb_assert_not_reached ("no decoding pattern found");
11759 break;
11760 }
11761
11762 /* Base register also changes; based on condition and W bit. */
11763 /* We save it anyway without optimization. */
11764 record_buf[0] = reg_src1;
11765 arm_insn_r->reg_rec_count = 1;
11766 }
11767
11768 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11769 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11770 return 0;
11771}
11772
11773/* Handling opcode 101 insns. */
11774
11775static int
11776arm_record_b_bl (insn_decode_record *arm_insn_r)
11777{
11778 uint32_t record_buf[8];
11779
11780 /* Handle B, BL, BLX(1) insns. */
11781 /* B simply branches so we do nothing here. */
11782 /* Note: BLX(1) doesnt fall here but instead it falls into
11783 extension space. */
11784 if (bit (arm_insn_r->arm_insn, 24))
11785 {
11786 record_buf[0] = ARM_LR_REGNUM;
11787 arm_insn_r->reg_rec_count = 1;
11788 }
11789
11790 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11791
11792 return 0;
11793}
11794
11795/* Handling opcode 110 insns. */
11796
11797static int
11798arm_record_coproc (insn_decode_record *arm_insn_r)
11799{
11800 printf_unfiltered (_("Process record does not support instruction "
11801 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
11802 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11803
11804 return -1;
11805}
11806
11807/* Handling opcode 111 insns. */
11808
11809static int
11810arm_record_coproc_data_proc (insn_decode_record *arm_insn_r)
11811{
11812 struct gdbarch_tdep *tdep = gdbarch_tdep (arm_insn_r->gdbarch);
11813 struct regcache *reg_cache = arm_insn_r->regcache;
11814 uint32_t ret = 0; /* function return value: -1:record failure ; 0:success */
11815
11816 /* Handle SWI insn; system call would be handled over here. */
11817
11818 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 24, 27);
11819 if (15 == arm_insn_r->opcode)
11820 {
11821 /* Handle arm syscall insn. */
11822 if (tdep->arm_swi_record != NULL)
11823 {
11824 ret = tdep->arm_swi_record(reg_cache);
11825 }
11826 else
11827 {
11828 printf_unfiltered (_("no syscall record support\n"));
11829 ret = -1;
11830 }
11831 }
11832
11833 printf_unfiltered (_("Process record does not support instruction "
11834 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
11835 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11836 return ret;
11837}
11838
11839/* Handling opcode 000 insns. */
11840
11841static int
11842thumb_record_shift_add_sub (insn_decode_record *thumb_insn_r)
11843{
11844 uint32_t record_buf[8];
11845 uint32_t reg_src1 = 0;
11846
11847 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11848
11849 record_buf[0] = ARM_PS_REGNUM;
11850 record_buf[1] = reg_src1;
11851 thumb_insn_r->reg_rec_count = 2;
11852
11853 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11854
11855 return 0;
11856}
11857
11858
11859/* Handling opcode 001 insns. */
11860
11861static int
11862thumb_record_add_sub_cmp_mov (insn_decode_record *thumb_insn_r)
11863{
11864 uint32_t record_buf[8];
11865 uint32_t reg_src1 = 0;
11866
11867 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11868
11869 record_buf[0] = ARM_PS_REGNUM;
11870 record_buf[1] = reg_src1;
11871 thumb_insn_r->reg_rec_count = 2;
11872
11873 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11874
11875 return 0;
11876}
11877
11878/* Handling opcode 010 insns. */
11879
11880static int
11881thumb_record_ld_st_reg_offset (insn_decode_record *thumb_insn_r)
11882{
11883 struct regcache *reg_cache = thumb_insn_r->regcache;
11884 uint32_t record_buf[8], record_buf_mem[8];
11885
11886 uint32_t reg_src1 = 0, reg_src2 = 0;
11887 uint32_t opcode1 = 0, opcode2 = 0, opcode3 = 0;
11888
11889 ULONGEST u_regval[2] = {0};
11890
11891 opcode1 = bits (thumb_insn_r->arm_insn, 10, 12);
11892
11893 if (bit (thumb_insn_r->arm_insn, 12))
11894 {
11895 /* Handle load/store register offset. */
11896 opcode2 = bits (thumb_insn_r->arm_insn, 9, 10);
11897 if (opcode2 >= 12 && opcode2 <= 15)
11898 {
11899 /* LDR(2), LDRB(2) , LDRH(2), LDRSB, LDRSH. */
11900 reg_src1 = bits (thumb_insn_r->arm_insn,0, 2);
11901 record_buf[0] = reg_src1;
11902 thumb_insn_r->reg_rec_count = 1;
11903 }
11904 else if (opcode2 >= 8 && opcode2 <= 10)
11905 {
11906 /* STR(2), STRB(2), STRH(2) . */
11907 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
11908 reg_src2 = bits (thumb_insn_r->arm_insn, 6, 8);
11909 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11910 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11911 if (8 == opcode2)
11912 record_buf_mem[0] = 4; /* STR (2). */
11913 else if (10 == opcode2)
11914 record_buf_mem[0] = 1; /* STRB (2). */
11915 else if (9 == opcode2)
11916 record_buf_mem[0] = 2; /* STRH (2). */
11917 record_buf_mem[1] = u_regval[0] + u_regval[1];
11918 thumb_insn_r->mem_rec_count = 1;
11919 }
11920 }
11921 else if (bit (thumb_insn_r->arm_insn, 11))
11922 {
11923 /* Handle load from literal pool. */
11924 /* LDR(3). */
11925 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11926 record_buf[0] = reg_src1;
11927 thumb_insn_r->reg_rec_count = 1;
11928 }
11929 else if (opcode1)
11930 {
11931 opcode2 = bits (thumb_insn_r->arm_insn, 8, 9);
11932 opcode3 = bits (thumb_insn_r->arm_insn, 0, 2);
11933 if ((3 == opcode2) && (!opcode3))
11934 {
11935 /* Branch with exchange. */
11936 record_buf[0] = ARM_PS_REGNUM;
11937 thumb_insn_r->reg_rec_count = 1;
11938 }
11939 else
11940 {
11941 /* Format 8; special data processing insns. */
11942 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11943 record_buf[0] = ARM_PS_REGNUM;
11944 record_buf[1] = reg_src1;
11945 thumb_insn_r->reg_rec_count = 2;
11946 }
11947 }
11948 else
11949 {
11950 /* Format 5; data processing insns. */
11951 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11952 if (bit (thumb_insn_r->arm_insn, 7))
11953 {
11954 reg_src1 = reg_src1 + 8;
11955 }
11956 record_buf[0] = ARM_PS_REGNUM;
11957 record_buf[1] = reg_src1;
11958 thumb_insn_r->reg_rec_count = 2;
11959 }
11960
11961 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11962 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
11963 record_buf_mem);
11964
11965 return 0;
11966}
11967
11968/* Handling opcode 001 insns. */
11969
11970static int
11971thumb_record_ld_st_imm_offset (insn_decode_record *thumb_insn_r)
11972{
11973 struct regcache *reg_cache = thumb_insn_r->regcache;
11974 uint32_t record_buf[8], record_buf_mem[8];
11975
11976 uint32_t reg_src1 = 0;
11977 uint32_t opcode = 0, immed_5 = 0;
11978
11979 ULONGEST u_regval = 0;
11980
11981 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
11982
11983 if (opcode)
11984 {
11985 /* LDR(1). */
11986 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11987 record_buf[0] = reg_src1;
11988 thumb_insn_r->reg_rec_count = 1;
11989 }
11990 else
11991 {
11992 /* STR(1). */
11993 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
11994 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
11995 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11996 record_buf_mem[0] = 4;
11997 record_buf_mem[1] = u_regval + (immed_5 * 4);
11998 thumb_insn_r->mem_rec_count = 1;
11999 }
12000
12001 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12002 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12003 record_buf_mem);
12004
12005 return 0;
12006}
12007
12008/* Handling opcode 100 insns. */
12009
12010static int
12011thumb_record_ld_st_stack (insn_decode_record *thumb_insn_r)
12012{
12013 struct regcache *reg_cache = thumb_insn_r->regcache;
12014 uint32_t record_buf[8], record_buf_mem[8];
12015
12016 uint32_t reg_src1 = 0;
12017 uint32_t opcode = 0, immed_8 = 0, immed_5 = 0;
12018
12019 ULONGEST u_regval = 0;
12020
12021 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12022
12023 if (3 == opcode)
12024 {
12025 /* LDR(4). */
12026 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12027 record_buf[0] = reg_src1;
12028 thumb_insn_r->reg_rec_count = 1;
12029 }
12030 else if (1 == opcode)
12031 {
12032 /* LDRH(1). */
12033 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12034 record_buf[0] = reg_src1;
12035 thumb_insn_r->reg_rec_count = 1;
12036 }
12037 else if (2 == opcode)
12038 {
12039 /* STR(3). */
12040 immed_8 = bits (thumb_insn_r->arm_insn, 0, 7);
12041 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
12042 record_buf_mem[0] = 4;
12043 record_buf_mem[1] = u_regval + (immed_8 * 4);
12044 thumb_insn_r->mem_rec_count = 1;
12045 }
12046 else if (0 == opcode)
12047 {
12048 /* STRH(1). */
12049 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
12050 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12051 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12052 record_buf_mem[0] = 2;
12053 record_buf_mem[1] = u_regval + (immed_5 * 2);
12054 thumb_insn_r->mem_rec_count = 1;
12055 }
12056
12057 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12058 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12059 record_buf_mem);
12060
12061 return 0;
12062}
12063
12064/* Handling opcode 101 insns. */
12065
12066static int
12067thumb_record_misc (insn_decode_record *thumb_insn_r)
12068{
12069 struct regcache *reg_cache = thumb_insn_r->regcache;
12070
12071 uint32_t opcode = 0, opcode1 = 0, opcode2 = 0;
12072 uint32_t register_bits = 0, register_count = 0;
12073 uint32_t register_list[8] = {0}, index = 0, start_address = 0;
12074 uint32_t record_buf[24], record_buf_mem[48];
12075 uint32_t reg_src1;
12076
12077 ULONGEST u_regval = 0;
12078
12079 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12080 opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
12081 opcode2 = bits (thumb_insn_r->arm_insn, 9, 12);
12082
12083 if (14 == opcode2)
12084 {
12085 /* POP. */
12086 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12087 while (register_bits)
12088 {
12089 if (register_bits & 0x00000001)
12090 register_list[register_count++] = 1;
12091 register_bits = register_bits >> 1;
12092 }
12093 record_buf[register_count] = ARM_PS_REGNUM;
12094 record_buf[register_count + 1] = ARM_SP_REGNUM;
12095 thumb_insn_r->reg_rec_count = register_count + 2;
12096 for (register_count = 0; register_count < 8; register_count++)
12097 {
12098 if (register_list[register_count])
12099 {
12100 record_buf[index] = register_count;
12101 index++;
12102 }
12103 }
12104 }
12105 else if (10 == opcode2)
12106 {
12107 /* PUSH. */
12108 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12109 regcache_raw_read_unsigned (reg_cache, ARM_PC_REGNUM, &u_regval);
12110 while (register_bits)
12111 {
12112 if (register_bits & 0x00000001)
12113 register_count++;
12114 register_bits = register_bits >> 1;
12115 }
12116 start_address = u_regval - \
12117 (4 * (bit (thumb_insn_r->arm_insn, 8) + register_count));
12118 thumb_insn_r->mem_rec_count = register_count;
12119 while (register_count)
12120 {
12121 record_buf_mem[(register_count * 2) - 1] = start_address;
12122 record_buf_mem[(register_count * 2) - 2] = 4;
12123 start_address = start_address + 4;
12124 register_count--;
12125 }
12126 record_buf[0] = ARM_SP_REGNUM;
12127 thumb_insn_r->reg_rec_count = 1;
12128 }
12129 else if (0x1E == opcode1)
12130 {
12131 /* BKPT insn. */
12132 /* Handle enhanced software breakpoint insn, BKPT. */
12133 /* CPSR is changed to be executed in ARM state, disabling normal
12134 interrupts, entering abort mode. */
12135 /* According to high vector configuration PC is set. */
12136 /* User hits breakpoint and type reverse, in that case, we need to go back with
12137 previous CPSR and Program Counter. */
12138 record_buf[0] = ARM_PS_REGNUM;
12139 record_buf[1] = ARM_LR_REGNUM;
12140 thumb_insn_r->reg_rec_count = 2;
12141 /* We need to save SPSR value, which is not yet done. */
12142 printf_unfiltered (_("Process record does not support instruction "
12143 "0x%0x at address %s.\n"),
12144 thumb_insn_r->arm_insn,
12145 paddress (thumb_insn_r->gdbarch,
12146 thumb_insn_r->this_addr));
12147 return -1;
12148 }
12149 else if ((0 == opcode) || (1 == opcode))
12150 {
12151 /* ADD(5), ADD(6). */
12152 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12153 record_buf[0] = reg_src1;
12154 thumb_insn_r->reg_rec_count = 1;
12155 }
12156 else if (2 == opcode)
12157 {
12158 /* ADD(7), SUB(4). */
12159 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12160 record_buf[0] = ARM_SP_REGNUM;
12161 thumb_insn_r->reg_rec_count = 1;
12162 }
12163
12164 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12165 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12166 record_buf_mem);
12167
12168 return 0;
12169}
12170
12171/* Handling opcode 110 insns. */
12172
12173static int
12174thumb_record_ldm_stm_swi (insn_decode_record *thumb_insn_r)
12175{
12176 struct gdbarch_tdep *tdep = gdbarch_tdep (thumb_insn_r->gdbarch);
12177 struct regcache *reg_cache = thumb_insn_r->regcache;
12178
12179 uint32_t ret = 0; /* function return value: -1:record failure ; 0:success */
12180 uint32_t reg_src1 = 0;
12181 uint32_t opcode1 = 0, opcode2 = 0, register_bits = 0, register_count = 0;
12182 uint32_t register_list[8] = {0}, index = 0, start_address = 0;
12183 uint32_t record_buf[24], record_buf_mem[48];
12184
12185 ULONGEST u_regval = 0;
12186
12187 opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
12188 opcode2 = bits (thumb_insn_r->arm_insn, 11, 12);
12189
12190 if (1 == opcode2)
12191 {
12192
12193 /* LDMIA. */
12194 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12195 /* Get Rn. */
12196 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12197 while (register_bits)
12198 {
12199 if (register_bits & 0x00000001)
12200 register_list[register_count++] = 1;
12201 register_bits = register_bits >> 1;
12202 }
12203 record_buf[register_count] = reg_src1;
12204 thumb_insn_r->reg_rec_count = register_count + 1;
12205 for (register_count = 0; register_count < 8; register_count++)
12206 {
12207 if (register_list[register_count])
12208 {
12209 record_buf[index] = register_count;
12210 index++;
12211 }
12212 }
12213 }
12214 else if (0 == opcode2)
12215 {
12216 /* It handles both STMIA. */
12217 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12218 /* Get Rn. */
12219 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12220 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12221 while (register_bits)
12222 {
12223 if (register_bits & 0x00000001)
12224 register_count++;
12225 register_bits = register_bits >> 1;
12226 }
12227 start_address = u_regval;
12228 thumb_insn_r->mem_rec_count = register_count;
12229 while (register_count)
12230 {
12231 record_buf_mem[(register_count * 2) - 1] = start_address;
12232 record_buf_mem[(register_count * 2) - 2] = 4;
12233 start_address = start_address + 4;
12234 register_count--;
12235 }
12236 }
12237 else if (0x1F == opcode1)
12238 {
12239 /* Handle arm syscall insn. */
12240 if (tdep->arm_swi_record != NULL)
12241 {
12242 ret = tdep->arm_swi_record(reg_cache);
12243 }
12244 else
12245 {
12246 printf_unfiltered (_("no syscall record support\n"));
12247 return -1;
12248 }
12249 }
12250
12251 /* B (1), conditional branch is automatically taken care in process_record,
12252 as PC is saved there. */
12253
12254 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12255 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12256 record_buf_mem);
12257
12258 return ret;
12259}
12260
12261/* Handling opcode 111 insns. */
12262
12263static int
12264thumb_record_branch (insn_decode_record *thumb_insn_r)
12265{
12266 uint32_t record_buf[8];
12267 uint32_t bits_h = 0;
12268
12269 bits_h = bits (thumb_insn_r->arm_insn, 11, 12);
12270
12271 if (2 == bits_h || 3 == bits_h)
12272 {
12273 /* BL */
12274 record_buf[0] = ARM_LR_REGNUM;
12275 thumb_insn_r->reg_rec_count = 1;
12276 }
12277 else if (1 == bits_h)
12278 {
12279 /* BLX(1). */
12280 record_buf[0] = ARM_PS_REGNUM;
12281 record_buf[1] = ARM_LR_REGNUM;
12282 thumb_insn_r->reg_rec_count = 2;
12283 }
12284
12285 /* B(2) is automatically taken care in process_record, as PC is
12286 saved there. */
12287
12288 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12289
12290 return 0;
12291}
12292
12293
12294/* Extracts arm/thumb/thumb2 insn depending on the size, and returns 0 on success
12295and positive val on fauilure. */
12296
12297static int
12298extract_arm_insn (insn_decode_record *insn_record, uint32_t insn_size)
12299{
12300 gdb_byte buf[insn_size];
12301
12302 memset (&buf[0], 0, insn_size);
12303
12304 if (target_read_memory (insn_record->this_addr, &buf[0], insn_size))
12305 return 1;
12306 insn_record->arm_insn = (uint32_t) extract_unsigned_integer (&buf[0],
12307 insn_size,
12308 gdbarch_byte_order (insn_record->gdbarch));
12309 return 0;
12310}
12311
12312typedef int (*sti_arm_hdl_fp_t) (insn_decode_record*);
12313
12314/* Decode arm/thumb insn depending on condition cods and opcodes; and
12315 dispatch it. */
12316
12317static int
12318decode_insn (insn_decode_record *arm_record, record_type_t record_type,
12319 uint32_t insn_size)
12320{
12321
12322 /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm instruction. */
12323 static const sti_arm_hdl_fp_t const arm_handle_insn[8] =
12324 {
12325 arm_record_data_proc_misc_ld_str, /* 000. */
12326 arm_record_data_proc_imm, /* 001. */
12327 arm_record_ld_st_imm_offset, /* 010. */
12328 arm_record_ld_st_reg_offset, /* 011. */
12329 arm_record_ld_st_multiple, /* 100. */
12330 arm_record_b_bl, /* 101. */
12331 arm_record_coproc, /* 110. */
12332 arm_record_coproc_data_proc /* 111. */
12333 };
12334
12335 /* (Starting from numerical 0); bits 13,14,15 decodes type of thumb instruction. */
12336 static const sti_arm_hdl_fp_t const thumb_handle_insn[8] =
12337 { \
12338 thumb_record_shift_add_sub, /* 000. */
12339 thumb_record_add_sub_cmp_mov, /* 001. */
12340 thumb_record_ld_st_reg_offset, /* 010. */
12341 thumb_record_ld_st_imm_offset, /* 011. */
12342 thumb_record_ld_st_stack, /* 100. */
12343 thumb_record_misc, /* 101. */
12344 thumb_record_ldm_stm_swi, /* 110. */
12345 thumb_record_branch /* 111. */
12346 };
12347
12348 uint32_t ret = 0; /* return value: negative:failure 0:success. */
12349 uint32_t insn_id = 0;
12350
12351 if (extract_arm_insn (arm_record, insn_size))
12352 {
12353 if (record_debug)
12354 {
12355 printf_unfiltered (_("Process record: error reading memory at "
12356 "addr %s len = %d.\n"),
12357 paddress (arm_record->gdbarch, arm_record->this_addr), insn_size);
12358 }
12359 return -1;
12360 }
12361 else if (ARM_RECORD == record_type)
12362 {
12363 arm_record->cond = bits (arm_record->arm_insn, 28, 31);
12364 insn_id = bits (arm_record->arm_insn, 25, 27);
12365 ret = arm_record_extension_space (arm_record);
12366 /* If this insn has fallen into extension space
12367 then we need not decode it anymore. */
12368 if (ret != -1 && !INSN_RECORDED(arm_record))
12369 {
12370 ret = arm_handle_insn[insn_id] (arm_record);
12371 }
12372 }
12373 else if (THUMB_RECORD == record_type)
12374 {
12375 /* As thumb does not have condition codes, we set negative. */
12376 arm_record->cond = -1;
12377 insn_id = bits (arm_record->arm_insn, 13, 15);
12378 ret = thumb_handle_insn[insn_id] (arm_record);
12379 }
12380 else if (THUMB2_RECORD == record_type)
12381 {
12382 printf_unfiltered (_("Process record doesnt support thumb32 instruction "
12383 "0x%0x at address %s.\n"),arm_record->arm_insn,
12384 paddress (arm_record->gdbarch,
12385 arm_record->this_addr));
12386 ret = -1;
12387 }
12388 else
12389 {
12390 /* Throw assertion. */
12391 gdb_assert_not_reached ("not a valid instruction, could not decode");
12392 }
12393
12394 return ret;
12395}
12396
12397
12398/* Cleans up local record registers and memory allocations. */
12399
12400static void
12401deallocate_reg_mem (insn_decode_record *record)
12402{
12403 xfree (record->arm_regs);
12404 xfree (record->arm_mems);
12405}
12406
12407
12408/* Parse the current instruction and record the values of the registers and
12409 memory that will be changed in current instruction to record_arch_list".
12410 Return -1 if something is wrong. */
12411
12412int
12413arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
12414 CORE_ADDR insn_addr)
12415{
12416
12417 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
12418 uint32_t no_of_rec = 0;
12419 uint32_t ret = 0; /* return value: -1:record failure ; 0:success */
12420 ULONGEST t_bit = 0, insn_id = 0;
12421
12422 ULONGEST u_regval = 0;
12423
12424 insn_decode_record arm_record;
12425
12426 memset (&arm_record, 0, sizeof (insn_decode_record));
12427 arm_record.regcache = regcache;
12428 arm_record.this_addr = insn_addr;
12429 arm_record.gdbarch = gdbarch;
12430
12431
12432 if (record_debug > 1)
12433 {
12434 fprintf_unfiltered (gdb_stdlog, "Process record: arm_process_record "
12435 "addr = %s\n",
12436 paddress (gdbarch, arm_record.this_addr));
12437 }
12438
12439 if (extract_arm_insn (&arm_record, 2))
12440 {
12441 if (record_debug)
12442 {
12443 printf_unfiltered (_("Process record: error reading memory at "
12444 "addr %s len = %d.\n"),
12445 paddress (arm_record.gdbarch,
12446 arm_record.this_addr), 2);
12447 }
12448 return -1;
12449 }
12450
12451 /* Check the insn, whether it is thumb or arm one. */
12452
12453 t_bit = arm_psr_thumb_bit (arm_record.gdbarch);
12454 regcache_raw_read_unsigned (arm_record.regcache, ARM_PS_REGNUM, &u_regval);
12455
12456
12457 if (!(u_regval & t_bit))
12458 {
12459 /* We are decoding arm insn. */
12460 ret = decode_insn (&arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES);
12461 }
12462 else
12463 {
12464 insn_id = bits (arm_record.arm_insn, 11, 15);
12465 /* is it thumb2 insn? */
12466 if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id))
12467 {
12468 ret = decode_insn (&arm_record, THUMB2_RECORD,
12469 THUMB2_INSN_SIZE_BYTES);
12470 }
12471 else
12472 {
12473 /* We are decoding thumb insn. */
12474 ret = decode_insn (&arm_record, THUMB_RECORD, THUMB_INSN_SIZE_BYTES);
12475 }
12476 }
12477
12478 if (0 == ret)
12479 {
12480 /* Record registers. */
12481 record_arch_list_add_reg (arm_record.regcache, ARM_PC_REGNUM);
12482 if (arm_record.arm_regs)
12483 {
12484 for (no_of_rec = 0; no_of_rec < arm_record.reg_rec_count; no_of_rec++)
12485 {
12486 if (record_arch_list_add_reg (arm_record.regcache ,
12487 arm_record.arm_regs[no_of_rec]))
12488 ret = -1;
12489 }
12490 }
12491 /* Record memories. */
12492 if (arm_record.arm_mems)
12493 {
12494 for (no_of_rec = 0; no_of_rec < arm_record.mem_rec_count; no_of_rec++)
12495 {
12496 if (record_arch_list_add_mem
12497 ((CORE_ADDR)arm_record.arm_mems[no_of_rec].addr,
12498 arm_record.arm_mems[no_of_rec].len))
12499 ret = -1;
12500 }
12501 }
12502
12503 if (record_arch_list_add_end ())
12504 ret = -1;
12505 }
12506
12507
12508 deallocate_reg_mem (&arm_record);
12509
12510 return ret;
12511}
12512
This page took 1.789091 seconds and 4 git commands to generate.