OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gdb-7.2/] [gdb/] [arm-tdep.c] - Blame information for rev 850

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.