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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [gdb/] [arm-tdep.c] - Blame information for rev 1770

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

Line No. Rev Author Line
1 578 markom
/* Common target dependent code for GDB on ARM systems.
2
   Copyright 1988, 1989, 1991, 1992, 1993, 1995, 1996, 1998, 1999, 2000,
3
   2001 Free Software Foundation, Inc.
4
 
5
   This file is part of GDB.
6
 
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 2 of the License, or
10
   (at your option) any later version.
11
 
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
 
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 59 Temple Place - Suite 330,
20
   Boston, MA 02111-1307, USA.  */
21
 
22
#include "defs.h"
23
#include "frame.h"
24
#include "inferior.h"
25
#include "gdbcmd.h"
26
#include "gdbcore.h"
27
#include "symfile.h"
28
#include "gdb_string.h"
29
#include "coff/internal.h"      /* Internal format of COFF symbols in BFD */
30
#include "dis-asm.h"            /* For register flavors. */
31
#include <ctype.h>              /* for isupper () */
32
#include "regcache.h"
33
 
34
/* Each OS has a different mechanism for accessing the various
35
   registers stored in the sigcontext structure.
36
 
37
   SIGCONTEXT_REGISTER_ADDRESS should be defined to the name (or
38
   function pointer) which may be used to determine the addresses
39
   of the various saved registers in the sigcontext structure.
40
 
41
   For the ARM target, there are three parameters to this function.
42
   The first is the pc value of the frame under consideration, the
43
   second the stack pointer of this frame, and the last is the
44
   register number to fetch.
45
 
46
   If the tm.h file does not define this macro, then it's assumed that
47
   no mechanism is needed and we define SIGCONTEXT_REGISTER_ADDRESS to
48
   be 0.
49
 
50
   When it comes time to multi-arching this code, see the identically
51
   named machinery in ia64-tdep.c for an example of how it could be
52
   done.  It should not be necessary to modify the code below where
53
   this macro is used.  */
54
 
55
#ifdef SIGCONTEXT_REGISTER_ADDRESS
56
#ifndef SIGCONTEXT_REGISTER_ADDRESS_P
57
#define SIGCONTEXT_REGISTER_ADDRESS_P() 1
58
#endif
59
#else
60
#define SIGCONTEXT_REGISTER_ADDRESS(SP,PC,REG) 0
61
#define SIGCONTEXT_REGISTER_ADDRESS_P() 0
62
#endif
63
 
64
extern void _initialize_arm_tdep (void);
65
 
66
/* Number of different reg name sets (options). */
67
static int num_flavor_options;
68
 
69
/* We have more registers than the disassembler as gdb can print the value
70
   of special registers as well.
71
   The general register names are overwritten by whatever is being used by
72
   the disassembler at the moment. We also adjust the case of cpsr and fps. */
73
 
74
/* Initial value: Register names used in ARM's ISA documentation. */
75
static char * arm_register_name_strings[] =
76
{"r0",  "r1",  "r2",  "r3",     /*  0  1  2  3 */
77
 "r4",  "r5",  "r6",  "r7",     /*  4  5  6  7 */
78
 "r8",  "r9",  "r10", "r11",    /*  8  9 10 11 */
79
 "r12", "sp",  "lr",  "pc",     /* 12 13 14 15 */
80
 "f0",  "f1",  "f2",  "f3",     /* 16 17 18 19 */
81
 "f4",  "f5",  "f6",  "f7",     /* 20 21 22 23 */
82
 "fps", "cpsr" };               /* 24 25       */
83
char **arm_register_names = arm_register_name_strings;
84
 
85
/* Valid register name flavors.  */
86
static const char **valid_flavors;
87
 
88
/* Disassembly flavor to use. Default to "std" register names. */
89
static const char *disassembly_flavor;
90
static int current_option;      /* Index to that option in the opcodes table. */
91
 
92
/* This is used to keep the bfd arch_info in sync with the disassembly
93
   flavor.  */
94
static void set_disassembly_flavor_sfunc(char *, int,
95
                                         struct cmd_list_element *);
96
static void set_disassembly_flavor (void);
97
 
98
static void convert_from_extended (void *ptr, void *dbl);
99
 
100
/* Define other aspects of the stack frame.  We keep the offsets of
101
   all saved registers, 'cause we need 'em a lot!  We also keep the
102
   current size of the stack frame, and the offset of the frame
103
   pointer from the stack pointer (for frameless functions, and when
104
   we're still in the prologue of a function with a frame) */
105
 
106
struct frame_extra_info
107
  {
108
    struct frame_saved_regs fsr;
109
    int framesize;
110
    int frameoffset;
111
    int framereg;
112
  };
113
 
114
/* Addresses for calling Thumb functions have the bit 0 set.
115
   Here are some macros to test, set, or clear bit 0 of addresses.  */
116
#define IS_THUMB_ADDR(addr)     ((addr) & 1)
117
#define MAKE_THUMB_ADDR(addr)   ((addr) | 1)
118
#define UNMAKE_THUMB_ADDR(addr) ((addr) & ~1)
119
 
120
#define SWAP_TARGET_AND_HOST(buffer,len)                                \
121
  do                                                                    \
122
    {                                                                   \
123
      if (TARGET_BYTE_ORDER != HOST_BYTE_ORDER)                         \
124
        {                                                               \
125
          char tmp;                                                     \
126
          char *p = (char *)(buffer);                                   \
127
          char *q = ((char *)(buffer)) + len - 1;                       \
128
          for (; p < q; p++, q--)                                       \
129
            {                                                           \
130
              tmp = *q;                                                 \
131
              *q = *p;                                                  \
132
              *p = tmp;                                                 \
133
            }                                                           \
134
        }                                                               \
135
    }                                                                   \
136
  while (0)
137
 
138
/* Will a function return an aggregate type in memory or in a
139
   register?  Return 0 if an aggregate type can be returned in a
140
   register, 1 if it must be returned in memory.  */
141
 
142
int
143
arm_use_struct_convention (int gcc_p, struct type *type)
144
{
145
  int nRc;
146
  register enum type_code code;
147
 
148
  /* In the ARM ABI, "integer" like aggregate types are returned in
149
     registers.  For an aggregate type to be integer like, its size
150
     must be less than or equal to REGISTER_SIZE and the offset of
151
     each addressable subfield must be zero.  Note that bit fields are
152
     not addressable, and all addressable subfields of unions always
153
     start at offset zero.
154
 
155
     This function is based on the behaviour of GCC 2.95.1.
156
     See: gcc/arm.c: arm_return_in_memory() for details.
157
 
158
     Note: All versions of GCC before GCC 2.95.2 do not set up the
159
     parameters correctly for a function returning the following
160
     structure: struct { float f;}; This should be returned in memory,
161
     not a register.  Richard Earnshaw sent me a patch, but I do not
162
     know of any way to detect if a function like the above has been
163
     compiled with the correct calling convention.  */
164
 
165
  /* All aggregate types that won't fit in a register must be returned
166
     in memory.  */
167
  if (TYPE_LENGTH (type) > REGISTER_SIZE)
168
    {
169
      return 1;
170
    }
171
 
172
  /* The only aggregate types that can be returned in a register are
173
     structs and unions.  Arrays must be returned in memory.  */
174
  code = TYPE_CODE (type);
175
  if ((TYPE_CODE_STRUCT != code) && (TYPE_CODE_UNION != code))
176
    {
177
      return 1;
178
    }
179
 
180
  /* Assume all other aggregate types can be returned in a register.
181
     Run a check for structures, unions and arrays.  */
182
  nRc = 0;
183
 
184
  if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
185
    {
186
      int i;
187
      /* Need to check if this struct/union is "integer" like.  For
188
         this to be true, its size must be less than or equal to
189
         REGISTER_SIZE and the offset of each addressable subfield
190
         must be zero.  Note that bit fields are not addressable, and
191
         unions always start at offset zero.  If any of the subfields
192
         is a floating point type, the struct/union cannot be an
193
         integer type.  */
194
 
195
      /* For each field in the object, check:
196
         1) Is it FP? --> yes, nRc = 1;
197
         2) Is it addressable (bitpos != 0) and
198
         not packed (bitsize == 0)?
199
         --> yes, nRc = 1
200
       */
201
 
202
      for (i = 0; i < TYPE_NFIELDS (type); i++)
203
        {
204
          enum type_code field_type_code;
205
          field_type_code = TYPE_CODE (TYPE_FIELD_TYPE (type, i));
206
 
207
          /* Is it a floating point type field?  */
208
          if (field_type_code == TYPE_CODE_FLT)
209
            {
210
              nRc = 1;
211
              break;
212
            }
213
 
214
          /* If bitpos != 0, then we have to care about it.  */
215
          if (TYPE_FIELD_BITPOS (type, i) != 0)
216
            {
217
              /* Bitfields are not addressable.  If the field bitsize is
218
                 zero, then the field is not packed.  Hence it cannot be
219
                 a bitfield or any other packed type.  */
220
              if (TYPE_FIELD_BITSIZE (type, i) == 0)
221
                {
222
                  nRc = 1;
223
                  break;
224
                }
225
            }
226
        }
227
    }
228
 
229
  return nRc;
230
}
231
 
232
int
233
arm_frame_chain_valid (CORE_ADDR chain, struct frame_info *thisframe)
234
{
235
  return (chain != 0 && (FRAME_SAVED_PC (thisframe) >= LOWEST_PC));
236
}
237
 
238
/* Set to true if the 32-bit mode is in use. */
239
 
240
int arm_apcs_32 = 1;
241
 
242
/* Flag set by arm_fix_call_dummy that tells whether the target
243
   function is a Thumb function.  This flag is checked by
244
   arm_push_arguments.  FIXME: Change the PUSH_ARGUMENTS macro (and
245
   its use in valops.c) to pass the function address as an additional
246
   parameter.  */
247
 
248
static int target_is_thumb;
249
 
250
/* Flag set by arm_fix_call_dummy that tells whether the calling
251
   function is a Thumb function.  This flag is checked by
252
   arm_pc_is_thumb and arm_call_dummy_breakpoint_offset.  */
253
 
254
static int caller_is_thumb;
255
 
256
/* Determine if the program counter specified in MEMADDR is in a Thumb
257
   function.  */
258
 
259
int
260
arm_pc_is_thumb (CORE_ADDR memaddr)
261
{
262
  struct minimal_symbol *sym;
263
 
264
  /* If bit 0 of the address is set, assume this is a Thumb address.  */
265
  if (IS_THUMB_ADDR (memaddr))
266
    return 1;
267
 
268
  /* Thumb functions have a "special" bit set in minimal symbols.  */
269
  sym = lookup_minimal_symbol_by_pc (memaddr);
270
  if (sym)
271
    {
272
      return (MSYMBOL_IS_SPECIAL (sym));
273
    }
274
  else
275
    {
276
      return 0;
277
    }
278
}
279
 
280
/* Determine if the program counter specified in MEMADDR is in a call
281
   dummy being called from a Thumb function.  */
282
 
283
int
284
arm_pc_is_thumb_dummy (CORE_ADDR memaddr)
285
{
286
  CORE_ADDR sp = read_sp ();
287
 
288
  /* FIXME: Until we switch for the new call dummy macros, this heuristic
289
     is the best we can do.  We are trying to determine if the pc is on
290
     the stack, which (hopefully) will only happen in a call dummy.
291
     We hope the current stack pointer is not so far alway from the dummy
292
     frame location (true if we have not pushed large data structures or
293
     gone too many levels deep) and that our 1024 is not enough to consider
294
     code regions as part of the stack (true for most practical purposes) */
295
  if (PC_IN_CALL_DUMMY (memaddr, sp, sp + 1024))
296
    return caller_is_thumb;
297
  else
298
    return 0;
299
}
300
 
301
CORE_ADDR
302
arm_addr_bits_remove (CORE_ADDR val)
303
{
304
  if (arm_pc_is_thumb (val))
305
    return (val & (arm_apcs_32 ? 0xfffffffe : 0x03fffffe));
306
  else
307
    return (val & (arm_apcs_32 ? 0xfffffffc : 0x03fffffc));
308
}
309
 
310
CORE_ADDR
311
arm_saved_pc_after_call (struct frame_info *frame)
312
{
313
  return ADDR_BITS_REMOVE (read_register (LR_REGNUM));
314
}
315
 
316
int
317
arm_frameless_function_invocation (struct frame_info *fi)
318
{
319
  CORE_ADDR func_start, after_prologue;
320
  int frameless;
321
 
322
  func_start = (get_pc_function_start ((fi)->pc) + FUNCTION_START_OFFSET);
323
  after_prologue = SKIP_PROLOGUE (func_start);
324
 
325
  /* There are some frameless functions whose first two instructions
326
     follow the standard APCS form, in which case after_prologue will
327
     be func_start + 8. */
328
 
329
  frameless = (after_prologue < func_start + 12);
330
  return frameless;
331
}
332
 
333
/* A typical Thumb prologue looks like this:
334
   push    {r7, lr}
335
   add     sp, sp, #-28
336
   add     r7, sp, #12
337
   Sometimes the latter instruction may be replaced by:
338
   mov     r7, sp
339
 
340
   or like this:
341
   push    {r7, lr}
342
   mov     r7, sp
343
   sub     sp, #12
344
 
345
   or, on tpcs, like this:
346
   sub     sp,#16
347
   push    {r7, lr}
348
   (many instructions)
349
   mov     r7, sp
350
   sub     sp, #12
351
 
352
   There is always one instruction of three classes:
353
   1 - push
354
   2 - setting of r7
355
   3 - adjusting of sp
356
 
357
   When we have found at least one of each class we are done with the prolog.
358
   Note that the "sub sp, #NN" before the push does not count.
359
   */
360
 
361
static CORE_ADDR
362
thumb_skip_prologue (CORE_ADDR pc, CORE_ADDR func_end)
363
{
364
  CORE_ADDR current_pc;
365
  int findmask = 0;      /* findmask:
366
                           bit 0 - push { rlist }
367
                           bit 1 - mov r7, sp  OR  add r7, sp, #imm  (setting of r7)
368
                           bit 2 - sub sp, #simm  OR  add sp, #simm  (adjusting of sp)
369
                        */
370
 
371
  for (current_pc = pc; current_pc + 2 < func_end && current_pc < pc + 40; current_pc += 2)
372
    {
373
      unsigned short insn = read_memory_unsigned_integer (current_pc, 2);
374
 
375
      if ((insn & 0xfe00) == 0xb400)    /* push { rlist } */
376
        {
377
          findmask |= 1;  /* push found */
378
        }
379
      else if ((insn & 0xff00) == 0xb000)       /* add sp, #simm  OR  sub sp, #simm */
380
        {
381
          if ((findmask & 1) == 0)  /* before push ? */
382
            continue;
383
          else
384
            findmask |= 4;  /* add/sub sp found */
385
        }
386
      else if ((insn & 0xff00) == 0xaf00)       /* add r7, sp, #imm */
387
        {
388
          findmask |= 2;  /* setting of r7 found */
389
        }
390
      else if (insn == 0x466f)                  /* mov r7, sp */
391
        {
392
          findmask |= 2;  /* setting of r7 found */
393
        }
394
      else
395
        continue;       /* something in the prolog that we don't care about or some
396
                           instruction from outside the prolog scheduled here for optimization */
397
    }
398
 
399
  return current_pc;
400
}
401
 
402
/* The APCS (ARM Procedure Call Standard) defines the following
403
   prologue:
404
 
405
   mov          ip, sp
406
   [stmfd       sp!, {a1,a2,a3,a4}]
407
   stmfd        sp!, {...,fp,ip,lr,pc}
408
   [stfe        f7, [sp, #-12]!]
409
   [stfe        f6, [sp, #-12]!]
410
   [stfe        f5, [sp, #-12]!]
411
   [stfe        f4, [sp, #-12]!]
412
   sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn */
413
 
414
CORE_ADDR
415
arm_skip_prologue (CORE_ADDR pc)
416
{
417
  unsigned long inst;
418
  CORE_ADDR skip_pc;
419
  CORE_ADDR func_addr, func_end;
420
  struct symtab_and_line sal;
421
 
422
  /* See what the symbol table says.  */
423
 
424
  if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
425
    {
426
      sal = find_pc_line (func_addr, 0);
427
      if ((sal.line != 0) && (sal.end < func_end))
428
        return sal.end;
429
    }
430
 
431
  /* Check if this is Thumb code.  */
432
  if (arm_pc_is_thumb (pc))
433
    return thumb_skip_prologue (pc, func_end);
434
 
435
  /* Can't find the prologue end in the symbol table, try it the hard way
436
     by disassembling the instructions. */
437
  skip_pc = pc;
438
  inst = read_memory_integer (skip_pc, 4);
439
  if (inst != 0xe1a0c00d)       /* mov ip, sp */
440
    return pc;
441
 
442
  skip_pc += 4;
443
  inst = read_memory_integer (skip_pc, 4);
444
  if ((inst & 0xfffffff0) == 0xe92d0000)        /* stmfd sp!,{a1,a2,a3,a4}  */
445
    {
446
      skip_pc += 4;
447
      inst = read_memory_integer (skip_pc, 4);
448
    }
449
 
450
  if ((inst & 0xfffff800) != 0xe92dd800)        /* stmfd sp!,{...,fp,ip,lr,pc} */
451
    return pc;
452
 
453
  skip_pc += 4;
454
  inst = read_memory_integer (skip_pc, 4);
455
 
456
  /* Any insns after this point may float into the code, if it makes
457
     for better instruction scheduling, so we skip them only if we
458
     find them, but still consdier the function to be frame-ful.  */
459
 
460
  /* We may have either one sfmfd instruction here, or several stfe
461
     insns, depending on the version of floating point code we
462
     support.  */
463
  if ((inst & 0xffbf0fff) == 0xec2d0200)        /* sfmfd fn, <cnt>, [sp]! */
464
    {
465
      skip_pc += 4;
466
      inst = read_memory_integer (skip_pc, 4);
467
    }
468
  else
469
    {
470
      while ((inst & 0xffff8fff) == 0xed6d0103)         /* stfe fn, [sp, #-12]! */
471
        {
472
          skip_pc += 4;
473
          inst = read_memory_integer (skip_pc, 4);
474
        }
475
    }
476
 
477
  if ((inst & 0xfffff000) == 0xe24cb000)        /* sub fp, ip, #nn */
478
    skip_pc += 4;
479
 
480
  return skip_pc;
481
}
482
/* *INDENT-OFF* */
483
/* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
484
   This function decodes a Thumb function prologue to determine:
485
     1) the size of the stack frame
486
     2) which registers are saved on it
487
     3) the offsets of saved regs
488
     4) the offset from the stack pointer to the frame pointer
489
   This information is stored in the "extra" fields of the frame_info.
490
 
491
   A typical Thumb function prologue would create this stack frame
492
   (offsets relative to FP)
493
     old SP ->  24  stack parameters
494
                20  LR
495
                16  R7
496
     R7 ->       0  local variables (16 bytes)
497
     SP ->     -12  additional stack space (12 bytes)
498
   The frame size would thus be 36 bytes, and the frame offset would be
499
   12 bytes.  The frame register is R7.
500
 
501
   The comments for thumb_skip_prolog() describe the algorithm we use to detect
502
   the end of the prolog */
503
/* *INDENT-ON* */
504
 
505
static void
506
thumb_scan_prologue (struct frame_info *fi)
507
{
508
  CORE_ADDR prologue_start;
509
  CORE_ADDR prologue_end;
510
  CORE_ADDR current_pc;
511
  int saved_reg[16];            /* which register has been copied to register n? */
512
  int findmask = 0;      /* findmask:
513
                           bit 0 - push { rlist }
514
                           bit 1 - mov r7, sp  OR  add r7, sp, #imm  (setting of r7)
515
                           bit 2 - sub sp, #simm  OR  add sp, #simm  (adjusting of sp)
516
                        */
517
  int i;
518
 
519
  if (find_pc_partial_function (fi->pc, NULL, &prologue_start, &prologue_end))
520
    {
521
      struct symtab_and_line sal = find_pc_line (prologue_start, 0);
522
 
523
      if (sal.line == 0) /* no line info, use current PC */
524
        prologue_end = fi->pc;
525
      else if (sal.end < prologue_end)  /* next line begins after fn end */
526
        prologue_end = sal.end; /* (probably means no prologue)  */
527
    }
528
  else
529
    prologue_end = prologue_start + 40;         /* We're in the boondocks: allow for */
530
  /* 16 pushes, an add, and "mv fp,sp" */
531
 
532
  prologue_end = min (prologue_end, fi->pc);
533
 
534
  /* Initialize the saved register map.  When register H is copied to
535
     register L, we will put H in saved_reg[L].  */
536
  for (i = 0; i < 16; i++)
537
    saved_reg[i] = i;
538
 
539
  /* Search the prologue looking for instructions that set up the
540
     frame pointer, adjust the stack pointer, and save registers.
541
     Do this until all basic prolog instructions are found.  */
542
 
543
  fi->framesize = 0;
544
  for (current_pc = prologue_start;
545
       (current_pc < prologue_end) && ((findmask & 7) != 7);
546
       current_pc += 2)
547
    {
548
      unsigned short insn;
549
      int regno;
550
      int offset;
551
 
552
      insn = read_memory_unsigned_integer (current_pc, 2);
553
 
554
      if ((insn & 0xfe00) == 0xb400)    /* push { rlist } */
555
        {
556
          int mask;
557
          findmask |= 1;  /* push found */
558
          /* Bits 0-7 contain a mask for registers R0-R7.  Bit 8 says
559
             whether to save LR (R14).  */
560
          mask = (insn & 0xff) | ((insn & 0x100) << 6);
561
 
562
          /* Calculate offsets of saved R0-R7 and LR. */
563
          for (regno = LR_REGNUM; regno >= 0; regno--)
564
            if (mask & (1 << regno))
565
              {
566
                fi->framesize += 4;
567
                fi->fsr.regs[saved_reg[regno]] = -(fi->framesize);
568
                saved_reg[regno] = regno;       /* reset saved register map */
569
              }
570
        }
571
      else if ((insn & 0xff00) == 0xb000)       /* add sp, #simm  OR  sub sp, #simm */
572
        {
573
          if ((findmask & 1) == 0)  /* before push ? */
574
            continue;
575
          else
576
            findmask |= 4;  /* add/sub sp found */
577
 
578
          offset = (insn & 0x7f) << 2;  /* get scaled offset */
579
          if (insn & 0x80)      /* is it signed? (==subtracting) */
580
            {
581
              fi->frameoffset += offset;
582
              offset = -offset;
583
            }
584
          fi->framesize -= offset;
585
        }
586
      else if ((insn & 0xff00) == 0xaf00)       /* add r7, sp, #imm */
587
        {
588
          findmask |= 2;  /* setting of r7 found */
589
          fi->framereg = THUMB_FP_REGNUM;
590
          fi->frameoffset = (insn & 0xff) << 2;         /* get scaled offset */
591
        }
592
      else if (insn == 0x466f)                  /* mov r7, sp */
593
        {
594
          findmask |= 2;  /* setting of r7 found */
595
          fi->framereg = THUMB_FP_REGNUM;
596
          fi->frameoffset = 0;
597
          saved_reg[THUMB_FP_REGNUM] = SP_REGNUM;
598
        }
599
      else if ((insn & 0xffc0) == 0x4640)       /* mov r0-r7, r8-r15 */
600
        {
601
          int lo_reg = insn & 7;        /* dest. register (r0-r7) */
602
          int hi_reg = ((insn >> 3) & 7) + 8;   /* source register (r8-15) */
603
          saved_reg[lo_reg] = hi_reg;   /* remember hi reg was saved */
604
        }
605
      else
606
        continue;       /* something in the prolog that we don't care about or some
607
                           instruction from outside the prolog scheduled here for optimization */
608
    }
609
}
610
 
611
/* Check if prologue for this frame's PC has already been scanned.  If
612
   it has, copy the relevant information about that prologue and
613
   return non-zero.  Otherwise do not copy anything and return zero.
614
 
615
   The information saved in the cache includes:
616
   * the frame register number;
617
   * the size of the stack frame;
618
   * the offsets of saved regs (relative to the old SP); and
619
   * the offset from the stack pointer to the frame pointer
620
 
621
   The cache contains only one entry, since this is adequate for the
622
   typical sequence of prologue scan requests we get.  When performing
623
   a backtrace, GDB will usually ask to scan the same function twice
624
   in a row (once to get the frame chain, and once to fill in the
625
   extra frame information).  */
626
 
627
static struct frame_info prologue_cache;
628
 
629
static int
630
check_prologue_cache (struct frame_info *fi)
631
{
632
  int i;
633
 
634
  if (fi->pc == prologue_cache.pc)
635
    {
636
      fi->framereg = prologue_cache.framereg;
637
      fi->framesize = prologue_cache.framesize;
638
      fi->frameoffset = prologue_cache.frameoffset;
639
      for (i = 0; i < NUM_REGS; i++)
640
        fi->fsr.regs[i] = prologue_cache.fsr.regs[i];
641
      return 1;
642
    }
643
  else
644
    return 0;
645
}
646
 
647
 
648
/* Copy the prologue information from fi to the prologue cache.  */
649
 
650
static void
651
save_prologue_cache (struct frame_info *fi)
652
{
653
  int i;
654
 
655
  prologue_cache.pc = fi->pc;
656
  prologue_cache.framereg = fi->framereg;
657
  prologue_cache.framesize = fi->framesize;
658
  prologue_cache.frameoffset = fi->frameoffset;
659
 
660
  for (i = 0; i < NUM_REGS; i++)
661
    prologue_cache.fsr.regs[i] = fi->fsr.regs[i];
662
}
663
 
664
 
665
/* This function decodes an ARM function prologue to determine:
666
   1) the size of the stack frame
667
   2) which registers are saved on it
668
   3) the offsets of saved regs
669
   4) the offset from the stack pointer to the frame pointer
670
   This information is stored in the "extra" fields of the frame_info.
671
 
672
   There are two basic forms for the ARM prologue.  The fixed argument
673
   function call will look like:
674
 
675
   mov    ip, sp
676
   stmfd  sp!, {fp, ip, lr, pc}
677
   sub    fp, ip, #4
678
   [sub sp, sp, #4]
679
 
680
   Which would create this stack frame (offsets relative to FP):
681
   IP ->   4    (caller's stack)
682
   FP ->   0    PC (points to address of stmfd instruction + 8 in callee)
683
   -4   LR (return address in caller)
684
   -8   IP (copy of caller's SP)
685
   -12  FP (caller's FP)
686
   SP -> -28    Local variables
687
 
688
   The frame size would thus be 32 bytes, and the frame offset would be
689
   28 bytes.  The stmfd call can also save any of the vN registers it
690
   plans to use, which increases the frame size accordingly.
691
 
692
   Note: The stored PC is 8 off of the STMFD instruction that stored it
693
   because the ARM Store instructions always store PC + 8 when you read
694
   the PC register.
695
 
696
   A variable argument function call will look like:
697
 
698
   mov    ip, sp
699
   stmfd  sp!, {a1, a2, a3, a4}
700
   stmfd  sp!, {fp, ip, lr, pc}
701
   sub    fp, ip, #20
702
 
703
   Which would create this stack frame (offsets relative to FP):
704
   IP ->  20    (caller's stack)
705
   16  A4
706
   12  A3
707
   8  A2
708
   4  A1
709
   FP ->   0    PC (points to address of stmfd instruction + 8 in callee)
710
   -4   LR (return address in caller)
711
   -8   IP (copy of caller's SP)
712
   -12  FP (caller's FP)
713
   SP -> -28    Local variables
714
 
715
   The frame size would thus be 48 bytes, and the frame offset would be
716
   28 bytes.
717
 
718
   There is another potential complication, which is that the optimizer
719
   will try to separate the store of fp in the "stmfd" instruction from
720
   the "sub fp, ip, #NN" instruction.  Almost anything can be there, so
721
   we just key on the stmfd, and then scan for the "sub fp, ip, #NN"...
722
 
723
   Also, note, the original version of the ARM toolchain claimed that there
724
   should be an
725
 
726
   instruction at the end of the prologue.  I have never seen GCC produce
727
   this, and the ARM docs don't mention it.  We still test for it below in
728
   case it happens...
729
 
730
 */
731
 
732
static void
733
arm_scan_prologue (struct frame_info *fi)
734
{
735
  int regno, sp_offset, fp_offset;
736
  CORE_ADDR prologue_start, prologue_end, current_pc;
737
 
738
  /* Check if this function is already in the cache of frame information. */
739
  if (check_prologue_cache (fi))
740
    return;
741
 
742
  /* Assume there is no frame until proven otherwise.  */
743
  fi->framereg = SP_REGNUM;
744
  fi->framesize = 0;
745
  fi->frameoffset = 0;
746
 
747
  /* Check for Thumb prologue.  */
748
  if (arm_pc_is_thumb (fi->pc))
749
    {
750
      thumb_scan_prologue (fi);
751
      save_prologue_cache (fi);
752
      return;
753
    }
754
 
755
  /* Find the function prologue.  If we can't find the function in
756
     the symbol table, peek in the stack frame to find the PC.  */
757
  if (find_pc_partial_function (fi->pc, NULL, &prologue_start, &prologue_end))
758
    {
759
      /* One way to find the end of the prologue (which works well
760
         for unoptimized code) is to do the following:
761
 
762
            struct symtab_and_line sal = find_pc_line (prologue_start, 0);
763
 
764
            if (sal.line == 0)
765
              prologue_end = fi->pc;
766
            else if (sal.end < prologue_end)
767
              prologue_end = sal.end;
768
 
769
         This mechanism is very accurate so long as the optimizer
770
         doesn't move any instructions from the function body into the
771
         prologue.  If this happens, sal.end will be the last
772
         instruction in the first hunk of prologue code just before
773
         the first instruction that the scheduler has moved from
774
         the body to the prologue.
775
 
776
         In order to make sure that we scan all of the prologue
777
         instructions, we use a slightly less accurate mechanism which
778
         may scan more than necessary.  To help compensate for this
779
         lack of accuracy, the prologue scanning loop below contains
780
         several clauses which'll cause the loop to terminate early if
781
         an implausible prologue instruction is encountered.
782
 
783
         The expression
784
 
785
              prologue_start + 64
786
 
787
         is a suitable endpoint since it accounts for the largest
788
         possible prologue plus up to five instructions inserted by
789
         the scheduler. */
790
 
791
      if (prologue_end > prologue_start + 64)
792
        {
793
          prologue_end = prologue_start + 64;   /* See above. */
794
        }
795
    }
796
  else
797
    {
798
      /* Get address of the stmfd in the prologue of the callee; the saved
799
         PC is the address of the stmfd + 8.  */
800
      prologue_start = ADDR_BITS_REMOVE (read_memory_integer (fi->frame, 4))
801
        - 8;
802
      prologue_end = prologue_start + 64;       /* See above. */
803
    }
804
 
805
  /* Now search the prologue looking for instructions that set up the
806
     frame pointer, adjust the stack pointer, and save registers.
807
 
808
     Be careful, however, and if it doesn't look like a prologue,
809
     don't try to scan it.  If, for instance, a frameless function
810
     begins with stmfd sp!, then we will tell ourselves there is
811
     a frame, which will confuse stack traceback, as well ad"finish"
812
     and other operations that rely on a knowledge of the stack
813
     traceback.
814
 
815
     In the APCS, the prologue should start with  "mov ip, sp" so
816
     if we don't see this as the first insn, we will stop.  */
817
 
818
  sp_offset = fp_offset = 0;
819
 
820
  if (read_memory_unsigned_integer (prologue_start, 4)
821
      == 0xe1a0c00d)            /* mov ip, sp */
822
    {
823
      for (current_pc = prologue_start + 4; current_pc < prologue_end;
824
           current_pc += 4)
825
        {
826
          unsigned int insn = read_memory_unsigned_integer (current_pc, 4);
827
 
828
          if ((insn & 0xffff0000) == 0xe92d0000)
829
            /* stmfd sp!, {..., fp, ip, lr, pc}
830
               or
831
               stmfd sp!, {a1, a2, a3, a4}  */
832
            {
833
              int mask = insn & 0xffff;
834
 
835
              /* Calculate offsets of saved registers. */
836
              for (regno = PC_REGNUM; regno >= 0; regno--)
837
                if (mask & (1 << regno))
838
                  {
839
                    sp_offset -= 4;
840
                    fi->fsr.regs[regno] = sp_offset;
841
                  }
842
            }
843
          else if ((insn & 0xfffff000) == 0xe24cb000)   /* sub fp, ip #n */
844
            {
845
              unsigned imm = insn & 0xff;       /* immediate value */
846
              unsigned rot = (insn & 0xf00) >> 7;       /* rotate amount */
847
              imm = (imm >> rot) | (imm << (32 - rot));
848
              fp_offset = -imm;
849
              fi->framereg = FP_REGNUM;
850
            }
851
          else if ((insn & 0xfffff000) == 0xe24dd000)   /* sub sp, sp #n */
852
            {
853
              unsigned imm = insn & 0xff;       /* immediate value */
854
              unsigned rot = (insn & 0xf00) >> 7;       /* rotate amount */
855
              imm = (imm >> rot) | (imm << (32 - rot));
856
              sp_offset -= imm;
857
            }
858
          else if ((insn & 0xffff7fff) == 0xed6d0103)   /* stfe f?, [sp, -#c]! */
859
            {
860
              sp_offset -= 12;
861
              regno = F0_REGNUM + ((insn >> 12) & 0x07);
862
              fi->fsr.regs[regno] = sp_offset;
863
            }
864
          else if ((insn & 0xffbf0fff) == 0xec2d0200)   /* sfmfd f0, 4, [sp!] */
865
            {
866
              int n_saved_fp_regs;
867
              unsigned int fp_start_reg, fp_bound_reg;
868
 
869
              if ((insn & 0x800) == 0x800)      /* N0 is set */
870
                {
871
                  if ((insn & 0x40000) == 0x40000)      /* N1 is set */
872
                    n_saved_fp_regs = 3;
873
                  else
874
                    n_saved_fp_regs = 1;
875
                }
876
              else
877
                {
878
                  if ((insn & 0x40000) == 0x40000)      /* N1 is set */
879
                    n_saved_fp_regs = 2;
880
                  else
881
                    n_saved_fp_regs = 4;
882
                }
883
 
884
              fp_start_reg = F0_REGNUM + ((insn >> 12) & 0x7);
885
              fp_bound_reg = fp_start_reg + n_saved_fp_regs;
886
              for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
887
                {
888
                  sp_offset -= 12;
889
                  fi->fsr.regs[fp_start_reg++] = sp_offset;
890
                }
891
            }
892
          else if ((insn & 0xf0000000) != 0xe0000000)
893
            break;      /* Condition not true, exit early */
894
          else if ((insn & 0xfe200000) == 0xe8200000) /* ldm? */
895
            break;      /* Don't scan past a block load */
896
          else
897
            /* The optimizer might shove anything into the prologue,
898
               so we just skip what we don't recognize. */
899
            continue;
900
        }
901
    }
902
 
903
  /* The frame size is just the negative of the offset (from the original SP)
904
     of the last thing thing we pushed on the stack.  The frame offset is
905
     [new FP] - [new SP].  */
906
  fi->framesize = -sp_offset;
907
  fi->frameoffset = fp_offset - sp_offset;
908
 
909
  save_prologue_cache (fi);
910
}
911
 
912
/* Find REGNUM on the stack.  Otherwise, it's in an active register.
913
   One thing we might want to do here is to check REGNUM against the
914
   clobber mask, and somehow flag it as invalid if it isn't saved on
915
   the stack somewhere.  This would provide a graceful failure mode
916
   when trying to get the value of caller-saves registers for an inner
917
   frame.  */
918
 
919
static CORE_ADDR
920
arm_find_callers_reg (struct frame_info *fi, int regnum)
921
{
922
  for (; fi; fi = fi->next)
923
 
924
#if 0                           /* FIXME: enable this code if we convert to new call dummy scheme.  */
925
    if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
926
      return generic_read_register_dummy (fi->pc, fi->frame, regnum);
927
    else
928
#endif
929
    if (fi->fsr.regs[regnum] != 0)
930
      return read_memory_integer (fi->fsr.regs[regnum],
931
                                  REGISTER_RAW_SIZE (regnum));
932
  return read_register (regnum);
933
}
934
/* *INDENT-OFF* */
935
/* Function: frame_chain
936
   Given a GDB frame, determine the address of the calling function's frame.
937
   This will be used to create a new GDB frame struct, and then
938
   INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
939
   For ARM, we save the frame size when we initialize the frame_info.
940
 
941
   The original definition of this function was a macro in tm-arm.h:
942
      { In the case of the ARM, the frame's nominal address is the FP value,
943
         and 12 bytes before comes the saved previous FP value as a 4-byte word.  }
944
 
945
      #define FRAME_CHAIN(thisframe)  \
946
        ((thisframe)->pc >= LOWEST_PC ?    \
947
         read_memory_integer ((thisframe)->frame - 12, 4) :\
948
         0)
949
*/
950
/* *INDENT-ON* */
951
 
952
CORE_ADDR
953
arm_frame_chain (struct frame_info *fi)
954
{
955
#if 0                           /* FIXME: enable this code if we convert to new call dummy scheme.  */
956
  CORE_ADDR fn_start, callers_pc, fp;
957
 
958
  /* is this a dummy frame? */
959
  if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
960
    return fi->frame;           /* dummy frame same as caller's frame */
961
 
962
  /* is caller-of-this a dummy frame? */
963
  callers_pc = FRAME_SAVED_PC (fi);     /* find out who called us: */
964
  fp = arm_find_callers_reg (fi, FP_REGNUM);
965
  if (PC_IN_CALL_DUMMY (callers_pc, fp, fp))
966
    return fp;                  /* dummy frame's frame may bear no relation to ours */
967
 
968
  if (find_pc_partial_function (fi->pc, 0, &fn_start, 0))
969
    if (fn_start == entry_point_address ())
970
      return 0;                  /* in _start fn, don't chain further */
971
#endif
972
  CORE_ADDR caller_pc, fn_start;
973
  struct frame_info caller_fi;
974
  int framereg = fi->framereg;
975
 
976
  if (fi->pc < LOWEST_PC)
977
    return 0;
978
 
979
  /* If the caller is the startup code, we're at the end of the chain.  */
980
  caller_pc = FRAME_SAVED_PC (fi);
981
  if (find_pc_partial_function (caller_pc, 0, &fn_start, 0))
982
    if (fn_start == entry_point_address ())
983
      return 0;
984
 
985
  /* If the caller is Thumb and the caller is ARM, or vice versa,
986
     the frame register of the caller is different from ours.
987
     So we must scan the prologue of the caller to determine its
988
     frame register number. */
989
  if (arm_pc_is_thumb (caller_pc) != arm_pc_is_thumb (fi->pc))
990
    {
991
      memset (&caller_fi, 0, sizeof (caller_fi));
992
      caller_fi.pc = caller_pc;
993
      arm_scan_prologue (&caller_fi);
994
      framereg = caller_fi.framereg;
995
    }
996
 
997
  /* If the caller used a frame register, return its value.
998
     Otherwise, return the caller's stack pointer.  */
999
  if (framereg == FP_REGNUM || framereg == THUMB_FP_REGNUM)
1000
    return arm_find_callers_reg (fi, framereg);
1001
  else
1002
    return fi->frame + fi->framesize;
1003
}
1004
 
1005
/* This function actually figures out the frame address for a given pc
1006
   and sp.  This is tricky because we sometimes don't use an explicit
1007
   frame pointer, and the previous stack pointer isn't necessarily
1008
   recorded on the stack.  The only reliable way to get this info is
1009
   to examine the prologue.  FROMLEAF is a little confusing, it means
1010
   this is the next frame up the chain AFTER a frameless function.  If
1011
   this is true, then the frame value for this frame is still in the
1012
   fp register.  */
1013
 
1014
void
1015
arm_init_extra_frame_info (int fromleaf, struct frame_info *fi)
1016
{
1017
  int reg;
1018
 
1019
  if (fi->next)
1020
    fi->pc = FRAME_SAVED_PC (fi->next);
1021
 
1022
  memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
1023
 
1024
#if 0                           /* FIXME: enable this code if we convert to new call dummy scheme.  */
1025
  if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
1026
    {
1027
      /* We need to setup fi->frame here because run_stack_dummy gets it wrong
1028
         by assuming it's always FP.  */
1029
      fi->frame = generic_read_register_dummy (fi->pc, fi->frame, SP_REGNUM);
1030
      fi->framesize = 0;
1031
      fi->frameoffset = 0;
1032
      return;
1033
    }
1034
  else
1035
#endif
1036
 
1037
  /* Determine whether or not we're in a sigtramp frame.
1038
     Unfortunately, it isn't sufficient to test
1039
     fi->signal_handler_caller because this value is sometimes set
1040
     after invoking INIT_EXTRA_FRAME_INFO.  So we test *both*
1041
     fi->signal_handler_caller and IN_SIGTRAMP to determine if we need
1042
     to use the sigcontext addresses for the saved registers.
1043
 
1044
     Note: If an ARM IN_SIGTRAMP method ever needs to compare against
1045
     the name of the function, the code below will have to be changed
1046
     to first fetch the name of the function and then pass this name
1047
     to IN_SIGTRAMP. */
1048
 
1049
  if (SIGCONTEXT_REGISTER_ADDRESS_P ()
1050
      && (fi->signal_handler_caller || IN_SIGTRAMP (fi->pc, 0)))
1051
    {
1052
      CORE_ADDR sp;
1053
 
1054
      if (!fi->next)
1055
        sp = read_sp();
1056
      else
1057
        sp = fi->next->frame - fi->next->frameoffset + fi->next->framesize;
1058
 
1059
      for (reg = 0; reg < NUM_REGS; reg++)
1060
        fi->fsr.regs[reg] = SIGCONTEXT_REGISTER_ADDRESS (sp, fi->pc, reg);
1061
 
1062
      /* FIXME: What about thumb mode? */
1063
      fi->framereg = SP_REGNUM;
1064
      fi->frame = read_memory_integer (fi->fsr.regs[fi->framereg], 4);
1065
      fi->framesize = 0;
1066
      fi->frameoffset = 0;
1067
 
1068
    }
1069
  else
1070
    {
1071
      arm_scan_prologue (fi);
1072
 
1073
      if (!fi->next)
1074
        /* this is the innermost frame? */
1075
        fi->frame = read_register (fi->framereg);
1076
      else if (fi->framereg == FP_REGNUM || fi->framereg == THUMB_FP_REGNUM)
1077
        {
1078
          /* not the innermost frame */
1079
          /* If we have an FP, the callee saved it. */
1080
          if (fi->next->fsr.regs[fi->framereg] != 0)
1081
            fi->frame =
1082
              read_memory_integer (fi->next->fsr.regs[fi->framereg], 4);
1083
          else if (fromleaf)
1084
            /* If we were called by a frameless fn.  then our frame is
1085
               still in the frame pointer register on the board... */
1086
            fi->frame = read_fp ();
1087
        }
1088
 
1089
      /* Calculate actual addresses of saved registers using offsets
1090
         determined by arm_scan_prologue.  */
1091
      for (reg = 0; reg < NUM_REGS; reg++)
1092
        if (fi->fsr.regs[reg] != 0)
1093
          fi->fsr.regs[reg] += fi->frame + fi->framesize - fi->frameoffset;
1094
    }
1095
}
1096
 
1097
 
1098
/* Find the caller of this frame.  We do this by seeing if LR_REGNUM
1099
   is saved in the stack anywhere, otherwise we get it from the
1100
   registers.
1101
 
1102
   The old definition of this function was a macro:
1103
   #define FRAME_SAVED_PC(FRAME) \
1104
   ADDR_BITS_REMOVE (read_memory_integer ((FRAME)->frame - 4, 4)) */
1105
 
1106
CORE_ADDR
1107
arm_frame_saved_pc (struct frame_info *fi)
1108
{
1109
#if 0                           /* FIXME: enable this code if we convert to new call dummy scheme.  */
1110
  if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
1111
    return generic_read_register_dummy (fi->pc, fi->frame, PC_REGNUM);
1112
  else
1113
#endif
1114
    {
1115
      CORE_ADDR pc = arm_find_callers_reg (fi, LR_REGNUM);
1116
      return IS_THUMB_ADDR (pc) ? UNMAKE_THUMB_ADDR (pc) : pc;
1117
    }
1118
}
1119
 
1120
/* Return the frame address.  On ARM, it is R11; on Thumb it is R7.
1121
   Examine the Program Status Register to decide which state we're in.  */
1122
 
1123
CORE_ADDR
1124
arm_target_read_fp (void)
1125
{
1126
  if (read_register (PS_REGNUM) & 0x20)         /* Bit 5 is Thumb state bit */
1127
    return read_register (THUMB_FP_REGNUM);     /* R7 if Thumb */
1128
  else
1129
    return read_register (FP_REGNUM);   /* R11 if ARM */
1130
}
1131
 
1132
/* Calculate the frame offsets of the saved registers (ARM version).  */
1133
 
1134
void
1135
arm_frame_find_saved_regs (struct frame_info *fi,
1136
                           struct frame_saved_regs *regaddr)
1137
{
1138
  memcpy (regaddr, &fi->fsr, sizeof (struct frame_saved_regs));
1139
}
1140
 
1141
void
1142
arm_push_dummy_frame (void)
1143
{
1144
  CORE_ADDR old_sp = read_register (SP_REGNUM);
1145
  CORE_ADDR sp = old_sp;
1146
  CORE_ADDR fp, prologue_start;
1147
  int regnum;
1148
 
1149
  /* Push the two dummy prologue instructions in reverse order,
1150
     so that they'll be in the correct low-to-high order in memory.  */
1151
  /* sub     fp, ip, #4 */
1152
  sp = push_word (sp, 0xe24cb004);
1153
  /*  stmdb   sp!, {r0-r10, fp, ip, lr, pc} */
1154
  prologue_start = sp = push_word (sp, 0xe92ddfff);
1155
 
1156
  /* Push a pointer to the dummy prologue + 12, because when stm
1157
     instruction stores the PC, it stores the address of the stm
1158
     instruction itself plus 12.  */
1159
  fp = sp = push_word (sp, prologue_start + 12);
1160
  sp = push_word (sp, read_register (PC_REGNUM));       /* FIXME: was PS_REGNUM */
1161
  sp = push_word (sp, old_sp);
1162
  sp = push_word (sp, read_register (FP_REGNUM));
1163
 
1164
  for (regnum = 10; regnum >= 0; regnum--)
1165
    sp = push_word (sp, read_register (regnum));
1166
 
1167
  write_register (FP_REGNUM, fp);
1168
  write_register (THUMB_FP_REGNUM, fp);
1169
  write_register (SP_REGNUM, sp);
1170
}
1171
 
1172
/* Fix up the call dummy, based on whether the processor is currently
1173
   in Thumb or ARM mode, and whether the target function is Thumb or
1174
   ARM.  There are three different situations requiring three
1175
   different dummies:
1176
 
1177
   * ARM calling ARM: uses the call dummy in tm-arm.h, which has already
1178
   been copied into the dummy parameter to this function.
1179
   * ARM calling Thumb: uses the call dummy in tm-arm.h, but with the
1180
   "mov pc,r4" instruction patched to be a "bx r4" instead.
1181
   * Thumb calling anything: uses the Thumb dummy defined below, which
1182
   works for calling both ARM and Thumb functions.
1183
 
1184
   All three call dummies expect to receive the target function
1185
   address in R4, with the low bit set if it's a Thumb function.  */
1186
 
1187
void
1188
arm_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs,
1189
                    value_ptr *args, struct type *type, int gcc_p)
1190
{
1191
  static short thumb_dummy[4] =
1192
  {
1193
    0xf000, 0xf801,             /*        bl      label */
1194
    0xdf18,                     /*        swi     24 */
1195
    0x4720,                     /* label: bx      r4 */
1196
  };
1197
  static unsigned long arm_bx_r4 = 0xe12fff14;  /* bx r4 instruction */
1198
 
1199
  /* Set flag indicating whether the current PC is in a Thumb function. */
1200
  caller_is_thumb = arm_pc_is_thumb (read_pc ());
1201
 
1202
  /* If the target function is Thumb, set the low bit of the function
1203
     address.  And if the CPU is currently in ARM mode, patch the
1204
     second instruction of call dummy to use a BX instruction to
1205
     switch to Thumb mode.  */
1206
  target_is_thumb = arm_pc_is_thumb (fun);
1207
  if (target_is_thumb)
1208
    {
1209
      fun |= 1;
1210
      if (!caller_is_thumb)
1211
        store_unsigned_integer (dummy + 4, sizeof (arm_bx_r4), arm_bx_r4);
1212
    }
1213
 
1214
  /* If the CPU is currently in Thumb mode, use the Thumb call dummy
1215
     instead of the ARM one that's already been copied.  This will
1216
     work for both Thumb and ARM target functions.  */
1217
  if (caller_is_thumb)
1218
    {
1219
      int i;
1220
      char *p = dummy;
1221
      int len = sizeof (thumb_dummy) / sizeof (thumb_dummy[0]);
1222
 
1223
      for (i = 0; i < len; i++)
1224
        {
1225
          store_unsigned_integer (p, sizeof (thumb_dummy[0]), thumb_dummy[i]);
1226
          p += sizeof (thumb_dummy[0]);
1227
        }
1228
    }
1229
 
1230
  /* Put the target address in r4; the call dummy will copy this to
1231
     the PC. */
1232
  write_register (4, fun);
1233
}
1234
 
1235
/* Return the offset in the call dummy of the instruction that needs
1236
   to have a breakpoint placed on it.  This is the offset of the 'swi
1237
   24' instruction, which is no longer actually used, but simply acts
1238
   as a place-holder now.
1239
 
1240
   This implements the CALL_DUMMY_BREAK_OFFSET macro.  */
1241
 
1242
int
1243
arm_call_dummy_breakpoint_offset (void)
1244
{
1245
  if (caller_is_thumb)
1246
    return 4;
1247
  else
1248
    return 8;
1249
}
1250
 
1251
/* Note: ScottB
1252
 
1253
   This function does not support passing parameters using the FPA
1254
   variant of the APCS.  It passes any floating point arguments in the
1255
   general registers and/or on the stack.  */
1256
 
1257
CORE_ADDR
1258
arm_push_arguments (int nargs, value_ptr * args, CORE_ADDR sp,
1259
                    int struct_return, CORE_ADDR struct_addr)
1260
{
1261
  char *fp;
1262
  int argnum, argreg, nstack_size;
1263
 
1264
  /* Walk through the list of args and determine how large a temporary
1265
     stack is required.  Need to take care here as structs may be
1266
     passed on the stack, and we have to to push them.  */
1267
  nstack_size = -4 * REGISTER_SIZE;     /* Some arguments go into A1-A4.  */
1268
  if (struct_return)            /* The struct address goes in A1.  */
1269
    nstack_size += REGISTER_SIZE;
1270
 
1271
  /* Walk through the arguments and add their size to nstack_size.  */
1272
  for (argnum = 0; argnum < nargs; argnum++)
1273
    {
1274
      int len;
1275
      struct type *arg_type;
1276
 
1277
      arg_type = check_typedef (VALUE_TYPE (args[argnum]));
1278
      len = TYPE_LENGTH (arg_type);
1279
 
1280
      /* ANSI C code passes float arguments as integers, K&R code
1281
         passes float arguments as doubles.  Correct for this here.  */
1282
      if (TYPE_CODE_FLT == TYPE_CODE (arg_type) && REGISTER_SIZE == len)
1283
        nstack_size += FP_REGISTER_VIRTUAL_SIZE;
1284
      else
1285
        nstack_size += len;
1286
    }
1287
 
1288
  /* Allocate room on the stack, and initialize our stack frame
1289
     pointer.  */
1290
  fp = NULL;
1291
  if (nstack_size > 0)
1292
    {
1293
      sp -= nstack_size;
1294
      fp = (char *) sp;
1295
    }
1296
 
1297
  /* Initialize the integer argument register pointer.  */
1298
  argreg = A1_REGNUM;
1299
 
1300
  /* The struct_return pointer occupies the first parameter passing
1301
     register.  */
1302
  if (struct_return)
1303
    write_register (argreg++, struct_addr);
1304
 
1305
  /* Process arguments from left to right.  Store as many as allowed
1306
     in the parameter passing registers (A1-A4), and save the rest on
1307
     the temporary stack.  */
1308
  for (argnum = 0; argnum < nargs; argnum++)
1309
    {
1310
      int len;
1311
      char *val;
1312
      double dbl_arg;
1313
      CORE_ADDR regval;
1314
      enum type_code typecode;
1315
      struct type *arg_type, *target_type;
1316
 
1317
      arg_type = check_typedef (VALUE_TYPE (args[argnum]));
1318
      target_type = TYPE_TARGET_TYPE (arg_type);
1319
      len = TYPE_LENGTH (arg_type);
1320
      typecode = TYPE_CODE (arg_type);
1321
      val = (char *) VALUE_CONTENTS (args[argnum]);
1322
 
1323
      /* ANSI C code passes float arguments as integers, K&R code
1324
         passes float arguments as doubles.  The .stabs record for
1325
         for ANSI prototype floating point arguments records the
1326
         type as FP_INTEGER, while a K&R style (no prototype)
1327
         .stabs records the type as FP_FLOAT.  In this latter case
1328
         the compiler converts the float arguments to double before
1329
         calling the function.  */
1330
      if (TYPE_CODE_FLT == typecode && REGISTER_SIZE == len)
1331
        {
1332
          float f;
1333
          double d;
1334
          char * bufo = (char *) &d;
1335
          char * bufd = (char *) &dbl_arg;
1336
 
1337
          len = sizeof (double);
1338
          f = *(float *) val;
1339
          SWAP_TARGET_AND_HOST (&f, sizeof (float));  /* adjust endianess */
1340
          d = f;
1341
          /* We must revert the longwords so they get loaded into the
1342
             the right registers. */
1343
          memcpy (bufd, bufo + len / 2, len / 2);
1344
          SWAP_TARGET_AND_HOST (bufd, len / 2);  /* adjust endianess */
1345
          memcpy (bufd + len / 2, bufo, len / 2);
1346
          SWAP_TARGET_AND_HOST (bufd + len / 2, len / 2); /* adjust endianess */
1347
          val = (char *) &dbl_arg;
1348
        }
1349
#if 1
1350
      /* I don't know why this code was disable. The only logical use
1351
         for a function pointer is to call that function, so setting
1352
         the mode bit is perfectly fine. FN */
1353
      /* If the argument is a pointer to a function, and it is a Thumb
1354
         function, set the low bit of the pointer.  */
1355
      if (TYPE_CODE_PTR == typecode
1356
          && NULL != target_type
1357
          && TYPE_CODE_FUNC == TYPE_CODE (target_type))
1358
        {
1359
          CORE_ADDR regval = extract_address (val, len);
1360
          if (arm_pc_is_thumb (regval))
1361
            store_address (val, len, MAKE_THUMB_ADDR (regval));
1362
        }
1363
#endif
1364
      /* Copy the argument to general registers or the stack in
1365
         register-sized pieces.  Large arguments are split between
1366
         registers and stack.  */
1367
      while (len > 0)
1368
        {
1369
          int partial_len = len < REGISTER_SIZE ? len : REGISTER_SIZE;
1370
 
1371
          if (argreg <= ARM_LAST_ARG_REGNUM)
1372
            {
1373
              /* It's an argument being passed in a general register.  */
1374
              regval = extract_address (val, partial_len);
1375
              write_register (argreg++, regval);
1376
            }
1377
          else
1378
            {
1379
              /* Push the arguments onto the stack.  */
1380
              write_memory ((CORE_ADDR) fp, val, REGISTER_SIZE);
1381
              fp += REGISTER_SIZE;
1382
            }
1383
 
1384
          len -= partial_len;
1385
          val += partial_len;
1386
        }
1387
    }
1388
 
1389
  /* Return adjusted stack pointer.  */
1390
  return sp;
1391
}
1392
 
1393
void
1394
arm_pop_frame (void)
1395
{
1396
  int regnum;
1397
  struct frame_info *frame = get_current_frame ();
1398
 
1399
  if (!PC_IN_CALL_DUMMY(frame->pc, frame->frame, read_fp()))
1400
    {
1401
      CORE_ADDR old_SP;
1402
 
1403
      old_SP = read_register (frame->framereg);
1404
      for (regnum = 0; regnum < NUM_REGS; regnum++)
1405
        if (frame->fsr.regs[regnum] != 0)
1406
          write_register (regnum,
1407
                      read_memory_integer (frame->fsr.regs[regnum], 4));
1408
 
1409
      write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
1410
      write_register (SP_REGNUM, old_SP);
1411
    }
1412
  else
1413
    {
1414
      CORE_ADDR sp;
1415
 
1416
      sp = read_register (FP_REGNUM);
1417
      sp -= sizeof(CORE_ADDR); /* we don't care about this first word */
1418
 
1419
      write_register (PC_REGNUM, read_memory_integer (sp, 4));
1420
      sp -= sizeof(CORE_ADDR);
1421
      write_register (SP_REGNUM, read_memory_integer (sp, 4));
1422
      sp -= sizeof(CORE_ADDR);
1423
      write_register (FP_REGNUM, read_memory_integer (sp, 4));
1424
      sp -= sizeof(CORE_ADDR);
1425
 
1426
      for (regnum = 10; regnum >= 0; regnum--)
1427
        {
1428
          write_register (regnum, read_memory_integer (sp, 4));
1429
          sp -= sizeof(CORE_ADDR);
1430
        }
1431
    }
1432
 
1433
  flush_cached_frames ();
1434
}
1435
 
1436
static void
1437
print_fpu_flags (int flags)
1438
{
1439
  if (flags & (1 << 0))
1440
    fputs ("IVO ", stdout);
1441
  if (flags & (1 << 1))
1442
    fputs ("DVZ ", stdout);
1443
  if (flags & (1 << 2))
1444
    fputs ("OFL ", stdout);
1445
  if (flags & (1 << 3))
1446
    fputs ("UFL ", stdout);
1447
  if (flags & (1 << 4))
1448
    fputs ("INX ", stdout);
1449
  putchar ('\n');
1450
}
1451
 
1452
void
1453
arm_float_info (void)
1454
{
1455
  register unsigned long status = read_register (FPS_REGNUM);
1456
  int type;
1457
 
1458
  type = (status >> 24) & 127;
1459
  printf ("%s FPU type %d\n",
1460
          (status & (1 << 31)) ? "Hardware" : "Software",
1461
          type);
1462
  fputs ("mask: ", stdout);
1463
  print_fpu_flags (status >> 16);
1464
  fputs ("flags: ", stdout);
1465
  print_fpu_flags (status);
1466
}
1467
 
1468
#if 0
1469
/* FIXME:  The generated assembler works but sucks.  Instead of using
1470
   r0, r1 it pushes them on the stack, then loads them into r3, r4 and
1471
   uses those registers.  I must be missing something.  ScottB  */
1472
 
1473
void
1474
convert_from_extended (void *ptr, void *dbl)
1475
{
1476
  __asm__ ("
1477
           ldfe f0,[%0]
1478
           stfd f0,[%1] "
1479
:                               /* no output */
1480
:          "r" (ptr), "r" (dbl));
1481
}
1482
 
1483
void
1484
convert_to_extended (void *dbl, void *ptr)
1485
{
1486
  __asm__ ("
1487
           ldfd f0,[%0]
1488
           stfe f0,[%1] "
1489
:                               /* no output */
1490
:          "r" (dbl), "r" (ptr));
1491
}
1492
#else
1493
static void
1494
convert_from_extended (void *ptr, void *dbl)
1495
{
1496
  *(double *) dbl = *(double *) ptr;
1497
}
1498
 
1499
void
1500
convert_to_extended (void *dbl, void *ptr)
1501
{
1502
  *(double *) ptr = *(double *) dbl;
1503
}
1504
#endif
1505
 
1506
/* Nonzero if register N requires conversion from raw format to
1507
   virtual format.  */
1508
 
1509
int
1510
arm_register_convertible (unsigned int regnum)
1511
{
1512
  return ((regnum - F0_REGNUM) < 8);
1513
}
1514
 
1515
/* Convert data from raw format for register REGNUM in buffer FROM to
1516
   virtual format with type TYPE in buffer TO.  */
1517
 
1518
void
1519
arm_register_convert_to_virtual (unsigned int regnum, struct type *type,
1520
                                 void *from, void *to)
1521
{
1522
  double val;
1523
 
1524
  convert_from_extended (from, &val);
1525
  store_floating (to, TYPE_LENGTH (type), val);
1526
}
1527
 
1528
/* Convert data from virtual format with type TYPE in buffer FROM to
1529
   raw format for register REGNUM in buffer TO.  */
1530
 
1531
void
1532
arm_register_convert_to_raw (unsigned int regnum, struct type *type,
1533
                             void *from, void *to)
1534
{
1535
  double val = extract_floating (from, TYPE_LENGTH (type));
1536
 
1537
  convert_to_extended (&val, to);
1538
}
1539
 
1540
static int
1541
condition_true (unsigned long cond, unsigned long status_reg)
1542
{
1543
  if (cond == INST_AL || cond == INST_NV)
1544
    return 1;
1545
 
1546
  switch (cond)
1547
    {
1548
    case INST_EQ:
1549
      return ((status_reg & FLAG_Z) != 0);
1550
    case INST_NE:
1551
      return ((status_reg & FLAG_Z) == 0);
1552
    case INST_CS:
1553
      return ((status_reg & FLAG_C) != 0);
1554
    case INST_CC:
1555
      return ((status_reg & FLAG_C) == 0);
1556
    case INST_MI:
1557
      return ((status_reg & FLAG_N) != 0);
1558
    case INST_PL:
1559
      return ((status_reg & FLAG_N) == 0);
1560
    case INST_VS:
1561
      return ((status_reg & FLAG_V) != 0);
1562
    case INST_VC:
1563
      return ((status_reg & FLAG_V) == 0);
1564
    case INST_HI:
1565
      return ((status_reg & (FLAG_C | FLAG_Z)) == FLAG_C);
1566
    case INST_LS:
1567
      return ((status_reg & (FLAG_C | FLAG_Z)) != FLAG_C);
1568
    case INST_GE:
1569
      return (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0));
1570
    case INST_LT:
1571
      return (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0));
1572
    case INST_GT:
1573
      return (((status_reg & FLAG_Z) == 0) &&
1574
              (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0)));
1575
    case INST_LE:
1576
      return (((status_reg & FLAG_Z) != 0) ||
1577
              (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0)));
1578
    }
1579
  return 1;
1580
}
1581
 
1582
#define submask(x) ((1L << ((x) + 1)) - 1)
1583
#define bit(obj,st) (((obj) >> (st)) & 1)
1584
#define bits(obj,st,fn) (((obj) >> (st)) & submask ((fn) - (st)))
1585
#define sbits(obj,st,fn) \
1586
  ((long) (bits(obj,st,fn) | ((long) bit(obj,fn) * ~ submask (fn - st))))
1587
#define BranchDest(addr,instr) \
1588
  ((CORE_ADDR) (((long) (addr)) + 8 + (sbits (instr, 0, 23) << 2)))
1589
#define ARM_PC_32 1
1590
 
1591
static unsigned long
1592
shifted_reg_val (unsigned long inst, int carry, unsigned long pc_val,
1593
                 unsigned long status_reg)
1594
{
1595
  unsigned long res, shift;
1596
  int rm = bits (inst, 0, 3);
1597
  unsigned long shifttype = bits (inst, 5, 6);
1598
 
1599
  if (bit (inst, 4))
1600
    {
1601
      int rs = bits (inst, 8, 11);
1602
      shift = (rs == 15 ? pc_val + 8 : read_register (rs)) & 0xFF;
1603
    }
1604
  else
1605
    shift = bits (inst, 7, 11);
1606
 
1607
  res = (rm == 15
1608
         ? ((pc_val | (ARM_PC_32 ? 0 : status_reg))
1609
            + (bit (inst, 4) ? 12 : 8))
1610
         : read_register (rm));
1611
 
1612
  switch (shifttype)
1613
    {
1614
    case 0:                      /* LSL */
1615
      res = shift >= 32 ? 0 : res << shift;
1616
      break;
1617
 
1618
    case 1:                     /* LSR */
1619
      res = shift >= 32 ? 0 : res >> shift;
1620
      break;
1621
 
1622
    case 2:                     /* ASR */
1623
      if (shift >= 32)
1624
        shift = 31;
1625
      res = ((res & 0x80000000L)
1626
             ? ~((~res) >> shift) : res >> shift);
1627
      break;
1628
 
1629
    case 3:                     /* ROR/RRX */
1630
      shift &= 31;
1631
      if (shift == 0)
1632
        res = (res >> 1) | (carry ? 0x80000000L : 0);
1633
      else
1634
        res = (res >> shift) | (res << (32 - shift));
1635
      break;
1636
    }
1637
 
1638
  return res & 0xffffffff;
1639
}
1640
 
1641
/* Return number of 1-bits in VAL.  */
1642
 
1643
static int
1644
bitcount (unsigned long val)
1645
{
1646
  int nbits;
1647
  for (nbits = 0; val != 0; nbits++)
1648
    val &= val - 1;             /* delete rightmost 1-bit in val */
1649
  return nbits;
1650
}
1651
 
1652
static CORE_ADDR
1653
thumb_get_next_pc (CORE_ADDR pc)
1654
{
1655
  unsigned long pc_val = ((unsigned long) pc) + 4;      /* PC after prefetch */
1656
  unsigned short inst1 = read_memory_integer (pc, 2);
1657
  CORE_ADDR nextpc = pc + 2;    /* default is next instruction */
1658
  unsigned long offset;
1659
 
1660
  if ((inst1 & 0xff00) == 0xbd00)       /* pop {rlist, pc} */
1661
    {
1662
      CORE_ADDR sp;
1663
 
1664
      /* Fetch the saved PC from the stack.  It's stored above
1665
         all of the other registers.  */
1666
      offset = bitcount (bits (inst1, 0, 7)) * REGISTER_SIZE;
1667
      sp = read_register (SP_REGNUM);
1668
      nextpc = (CORE_ADDR) read_memory_integer (sp + offset, 4);
1669
      nextpc = ADDR_BITS_REMOVE (nextpc);
1670
      if (nextpc == pc)
1671
        error ("Infinite loop detected");
1672
    }
1673
  else if ((inst1 & 0xf000) == 0xd000)  /* conditional branch */
1674
    {
1675
      unsigned long status = read_register (PS_REGNUM);
1676
      unsigned long cond = bits (inst1, 8, 11);
1677
      if (cond != 0x0f && condition_true (cond, status))        /* 0x0f = SWI */
1678
        nextpc = pc_val + (sbits (inst1, 0, 7) << 1);
1679
    }
1680
  else if ((inst1 & 0xf800) == 0xe000)  /* unconditional branch */
1681
    {
1682
      nextpc = pc_val + (sbits (inst1, 0, 10) << 1);
1683
    }
1684
  else if ((inst1 & 0xf800) == 0xf000)  /* long branch with link */
1685
    {
1686
      unsigned short inst2 = read_memory_integer (pc + 2, 2);
1687
      offset = (sbits (inst1, 0, 10) << 12) + (bits (inst2, 0, 10) << 1);
1688
      nextpc = pc_val + offset;
1689
    }
1690
 
1691
  return nextpc;
1692
}
1693
 
1694
CORE_ADDR
1695
arm_get_next_pc (CORE_ADDR pc)
1696
{
1697
  unsigned long pc_val;
1698
  unsigned long this_instr;
1699
  unsigned long status;
1700
  CORE_ADDR nextpc;
1701
 
1702
  if (arm_pc_is_thumb (pc))
1703
    return thumb_get_next_pc (pc);
1704
 
1705
  pc_val = (unsigned long) pc;
1706
  this_instr = read_memory_integer (pc, 4);
1707
  status = read_register (PS_REGNUM);
1708
  nextpc = (CORE_ADDR) (pc_val + 4);    /* Default case */
1709
 
1710
  if (condition_true (bits (this_instr, 28, 31), status))
1711
    {
1712
      switch (bits (this_instr, 24, 27))
1713
        {
1714
        case 0x0:
1715
        case 0x1:               /* data processing */
1716
        case 0x2:
1717
        case 0x3:
1718
          {
1719
            unsigned long operand1, operand2, result = 0;
1720
            unsigned long rn;
1721
            int c;
1722
 
1723
            if (bits (this_instr, 12, 15) != 15)
1724
              break;
1725
 
1726
            if (bits (this_instr, 22, 25) == 0
1727
                && bits (this_instr, 4, 7) == 9)        /* multiply */
1728
              error ("Illegal update to pc in instruction");
1729
 
1730
            /* Multiply into PC */
1731
            c = (status & FLAG_C) ? 1 : 0;
1732
            rn = bits (this_instr, 16, 19);
1733
            operand1 = (rn == 15) ? pc_val + 8 : read_register (rn);
1734
 
1735
            if (bit (this_instr, 25))
1736
              {
1737
                unsigned long immval = bits (this_instr, 0, 7);
1738
                unsigned long rotate = 2 * bits (this_instr, 8, 11);
1739
                operand2 = ((immval >> rotate) | (immval << (32 - rotate)))
1740
                  & 0xffffffff;
1741
              }
1742
            else                /* operand 2 is a shifted register */
1743
              operand2 = shifted_reg_val (this_instr, c, pc_val, status);
1744
 
1745
            switch (bits (this_instr, 21, 24))
1746
              {
1747
              case 0x0: /*and */
1748
                result = operand1 & operand2;
1749
                break;
1750
 
1751
              case 0x1: /*eor */
1752
                result = operand1 ^ operand2;
1753
                break;
1754
 
1755
              case 0x2: /*sub */
1756
                result = operand1 - operand2;
1757
                break;
1758
 
1759
              case 0x3: /*rsb */
1760
                result = operand2 - operand1;
1761
                break;
1762
 
1763
              case 0x4: /*add */
1764
                result = operand1 + operand2;
1765
                break;
1766
 
1767
              case 0x5: /*adc */
1768
                result = operand1 + operand2 + c;
1769
                break;
1770
 
1771
              case 0x6: /*sbc */
1772
                result = operand1 - operand2 + c;
1773
                break;
1774
 
1775
              case 0x7: /*rsc */
1776
                result = operand2 - operand1 + c;
1777
                break;
1778
 
1779
              case 0x8:
1780
              case 0x9:
1781
              case 0xa:
1782
              case 0xb: /* tst, teq, cmp, cmn */
1783
                result = (unsigned long) nextpc;
1784
                break;
1785
 
1786
              case 0xc: /*orr */
1787
                result = operand1 | operand2;
1788
                break;
1789
 
1790
              case 0xd: /*mov */
1791
                /* Always step into a function.  */
1792
                result = operand2;
1793
                break;
1794
 
1795
              case 0xe: /*bic */
1796
                result = operand1 & ~operand2;
1797
                break;
1798
 
1799
              case 0xf: /*mvn */
1800
                result = ~operand2;
1801
                break;
1802
              }
1803
            nextpc = (CORE_ADDR) ADDR_BITS_REMOVE (result);
1804
 
1805
            if (nextpc == pc)
1806
              error ("Infinite loop detected");
1807
            break;
1808
          }
1809
 
1810
        case 0x4:
1811
        case 0x5:               /* data transfer */
1812
        case 0x6:
1813
        case 0x7:
1814
          if (bit (this_instr, 20))
1815
            {
1816
              /* load */
1817
              if (bits (this_instr, 12, 15) == 15)
1818
                {
1819
                  /* rd == pc */
1820
                  unsigned long rn;
1821
                  unsigned long base;
1822
 
1823
                  if (bit (this_instr, 22))
1824
                    error ("Illegal update to pc in instruction");
1825
 
1826
                  /* byte write to PC */
1827
                  rn = bits (this_instr, 16, 19);
1828
                  base = (rn == 15) ? pc_val + 8 : read_register (rn);
1829
                  if (bit (this_instr, 24))
1830
                    {
1831
                      /* pre-indexed */
1832
                      int c = (status & FLAG_C) ? 1 : 0;
1833
                      unsigned long offset =
1834
                      (bit (this_instr, 25)
1835
                       ? shifted_reg_val (this_instr, c, pc_val, status)
1836
                       : bits (this_instr, 0, 11));
1837
 
1838
                      if (bit (this_instr, 23))
1839
                        base += offset;
1840
                      else
1841
                        base -= offset;
1842
                    }
1843
                  nextpc = (CORE_ADDR) read_memory_integer ((CORE_ADDR) base,
1844
                                                            4);
1845
 
1846
                  nextpc = ADDR_BITS_REMOVE (nextpc);
1847
 
1848
                  if (nextpc == pc)
1849
                    error ("Infinite loop detected");
1850
                }
1851
            }
1852
          break;
1853
 
1854
        case 0x8:
1855
        case 0x9:               /* block transfer */
1856
          if (bit (this_instr, 20))
1857
            {
1858
              /* LDM */
1859
              if (bit (this_instr, 15))
1860
                {
1861
                  /* loading pc */
1862
                  int offset = 0;
1863
 
1864
                  if (bit (this_instr, 23))
1865
                    {
1866
                      /* up */
1867
                      unsigned long reglist = bits (this_instr, 0, 14);
1868
                      offset = bitcount (reglist) * 4;
1869
                      if (bit (this_instr, 24))         /* pre */
1870
                        offset += 4;
1871
                    }
1872
                  else if (bit (this_instr, 24))
1873
                    offset = -4;
1874
 
1875
                  {
1876
                    unsigned long rn_val =
1877
                    read_register (bits (this_instr, 16, 19));
1878
                    nextpc =
1879
                      (CORE_ADDR) read_memory_integer ((CORE_ADDR) (rn_val
1880
                                                                  + offset),
1881
                                                       4);
1882
                  }
1883
                  nextpc = ADDR_BITS_REMOVE (nextpc);
1884
                  if (nextpc == pc)
1885
                    error ("Infinite loop detected");
1886
                }
1887
            }
1888
          break;
1889
 
1890
        case 0xb:               /* branch & link */
1891
        case 0xa:               /* branch */
1892
          {
1893
            nextpc = BranchDest (pc, this_instr);
1894
 
1895
            nextpc = ADDR_BITS_REMOVE (nextpc);
1896
            if (nextpc == pc)
1897
              error ("Infinite loop detected");
1898
            break;
1899
          }
1900
 
1901
        case 0xc:
1902
        case 0xd:
1903
        case 0xe:               /* coproc ops */
1904
        case 0xf:               /* SWI */
1905
          break;
1906
 
1907
        default:
1908
          fprintf (stderr, "Bad bit-field extraction\n");
1909
          return (pc);
1910
        }
1911
    }
1912
 
1913
  return nextpc;
1914
}
1915
 
1916
#include "bfd-in2.h"
1917
#include "libcoff.h"
1918
 
1919
static int
1920
gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
1921
{
1922
  if (arm_pc_is_thumb (memaddr))
1923
    {
1924
      static asymbol *asym;
1925
      static combined_entry_type ce;
1926
      static struct coff_symbol_struct csym;
1927
      static struct _bfd fake_bfd;
1928
      static bfd_target fake_target;
1929
 
1930
      if (csym.native == NULL)
1931
        {
1932
          /* Create a fake symbol vector containing a Thumb symbol.  This is
1933
             solely so that the code in print_insn_little_arm() and
1934
             print_insn_big_arm() in opcodes/arm-dis.c will detect the presence
1935
             of a Thumb symbol and switch to decoding Thumb instructions.  */
1936
 
1937
          fake_target.flavour = bfd_target_coff_flavour;
1938
          fake_bfd.xvec = &fake_target;
1939
          ce.u.syment.n_sclass = C_THUMBEXTFUNC;
1940
          csym.native = &ce;
1941
          csym.symbol.the_bfd = &fake_bfd;
1942
          csym.symbol.name = "fake";
1943
          asym = (asymbol *) & csym;
1944
        }
1945
 
1946
      memaddr = UNMAKE_THUMB_ADDR (memaddr);
1947
      info->symbols = &asym;
1948
    }
1949
  else
1950
    info->symbols = NULL;
1951
 
1952
  if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1953
    return print_insn_big_arm (memaddr, info);
1954
  else
1955
    return print_insn_little_arm (memaddr, info);
1956
}
1957
 
1958
/* This function implements the BREAKPOINT_FROM_PC macro.  It uses the
1959
   program counter value to determine whether a 16-bit or 32-bit
1960
   breakpoint should be used.  It returns a pointer to a string of
1961
   bytes that encode a breakpoint instruction, stores the length of
1962
   the string to *lenptr, and adjusts the program counter (if
1963
   necessary) to point to the actual memory location where the
1964
   breakpoint should be inserted.  */
1965
 
1966
unsigned char *
1967
arm_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
1968
{
1969
  if (arm_pc_is_thumb (*pcptr) || arm_pc_is_thumb_dummy (*pcptr))
1970
    {
1971
      if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1972
        {
1973
          static char thumb_breakpoint[] = THUMB_BE_BREAKPOINT;
1974
          *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
1975
          *lenptr = sizeof (thumb_breakpoint);
1976
          return thumb_breakpoint;
1977
        }
1978
      else
1979
        {
1980
          static char thumb_breakpoint[] = THUMB_LE_BREAKPOINT;
1981
          *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
1982
          *lenptr = sizeof (thumb_breakpoint);
1983
          return thumb_breakpoint;
1984
        }
1985
    }
1986
  else
1987
    {
1988
      if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1989
        {
1990
          static char arm_breakpoint[] = ARM_BE_BREAKPOINT;
1991
          *lenptr = sizeof (arm_breakpoint);
1992
          return arm_breakpoint;
1993
        }
1994
      else
1995
        {
1996
          static char arm_breakpoint[] = ARM_LE_BREAKPOINT;
1997
          *lenptr = sizeof (arm_breakpoint);
1998
          return arm_breakpoint;
1999
        }
2000
    }
2001
}
2002
 
2003
/* Extract from an array REGBUF containing the (raw) register state a
2004
   function return value of type TYPE, and copy that, in virtual
2005
   format, into VALBUF.  */
2006
 
2007
void
2008
arm_extract_return_value (struct type *type,
2009
                          char regbuf[REGISTER_BYTES],
2010
                          char *valbuf)
2011
{
2012
  if (TYPE_CODE_FLT == TYPE_CODE (type))
2013
    convert_from_extended (&regbuf[REGISTER_BYTE (F0_REGNUM)], valbuf);
2014
  else
2015
    memcpy (valbuf, &regbuf[REGISTER_BYTE (A1_REGNUM)], TYPE_LENGTH (type));
2016
}
2017
 
2018
/* Return non-zero if the PC is inside a thumb call thunk.  */
2019
 
2020
int
2021
arm_in_call_stub (CORE_ADDR pc, char *name)
2022
{
2023
  CORE_ADDR start_addr;
2024
 
2025
  /* Find the starting address of the function containing the PC.  If
2026
     the caller didn't give us a name, look it up at the same time.  */
2027
  if (find_pc_partial_function (pc, name ? NULL : &name, &start_addr, NULL) == 0)
2028
    return 0;
2029
 
2030
  return strncmp (name, "_call_via_r", 11) == 0;
2031
}
2032
 
2033
/* If PC is in a Thumb call or return stub, return the address of the
2034
   target PC, which is in a register.  The thunk functions are called
2035
   _called_via_xx, where x is the register name.  The possible names
2036
   are r0-r9, sl, fp, ip, sp, and lr.  */
2037
 
2038
CORE_ADDR
2039
arm_skip_stub (CORE_ADDR pc)
2040
{
2041
  char *name;
2042
  CORE_ADDR start_addr;
2043
 
2044
  /* Find the starting address and name of the function containing the PC.  */
2045
  if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
2046
    return 0;
2047
 
2048
  /* Call thunks always start with "_call_via_".  */
2049
  if (strncmp (name, "_call_via_", 10) == 0)
2050
    {
2051
      /* Use the name suffix to determine which register contains the
2052
         target PC.  */
2053
      static char *table[15] =
2054
      {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
2055
       "r8", "r9", "sl", "fp", "ip", "sp", "lr"
2056
      };
2057
      int regno;
2058
 
2059
      for (regno = 0; regno <= 14; regno++)
2060
        if (strcmp (&name[10], table[regno]) == 0)
2061
          return read_register (regno);
2062
    }
2063
 
2064
  return 0;                      /* not a stub */
2065
}
2066
 
2067
/* If the user changes the register disassembly flavor used for info register
2068
   and other commands, we have to also switch the flavor used in opcodes
2069
   for disassembly output.
2070
   This function is run in the set disassembly_flavor command, and does that. */
2071
 
2072
static void
2073
set_disassembly_flavor_sfunc (char *args, int from_tty,
2074
                              struct cmd_list_element *c)
2075
{
2076
  set_disassembly_flavor ();
2077
}
2078
 
2079
static void
2080
set_disassembly_flavor (void)
2081
{
2082
  const char *setname, *setdesc, **regnames;
2083
  int numregs, j;
2084
 
2085
  /* Find the flavor that the user wants in the opcodes table. */
2086
  int current = 0;
2087
  numregs = get_arm_regnames (current, &setname, &setdesc, &regnames);
2088
  while ((disassembly_flavor != setname)
2089
         && (current < num_flavor_options))
2090
    get_arm_regnames (++current, &setname, &setdesc, &regnames);
2091
  current_option = current;
2092
 
2093
  /* Fill our copy. */
2094
  for (j = 0; j < numregs; j++)
2095
    arm_register_names[j] = (char *) regnames[j];
2096
 
2097
  /* Adjust case. */
2098
  if (isupper (*regnames[PC_REGNUM]))
2099
    {
2100
      arm_register_names[FPS_REGNUM] = "FPS";
2101
      arm_register_names[PS_REGNUM] = "CPSR";
2102
    }
2103
  else
2104
    {
2105
      arm_register_names[FPS_REGNUM] = "fps";
2106
      arm_register_names[PS_REGNUM] = "cpsr";
2107
    }
2108
 
2109
  /* Synchronize the disassembler. */
2110
  set_arm_regname_option (current);
2111
}
2112
 
2113
/* arm_othernames implements the "othernames" command.  This is kind
2114
   of hacky, and I prefer the set-show disassembly-flavor which is
2115
   also used for the x86 gdb.  I will keep this around, however, in
2116
   case anyone is actually using it. */
2117
 
2118
static void
2119
arm_othernames (char *names, int n)
2120
{
2121
  /* Circle through the various flavors. */
2122
  current_option = (current_option + 1) % num_flavor_options;
2123
 
2124
  disassembly_flavor = valid_flavors[current_option];
2125
  set_disassembly_flavor ();
2126
}
2127
 
2128
void
2129
_initialize_arm_tdep (void)
2130
{
2131
  struct ui_file *stb;
2132
  long length;
2133
  struct cmd_list_element *new_cmd;
2134
  const char *setname;
2135
  const char *setdesc;
2136
  const char **regnames;
2137
  int numregs, i, j;
2138
  static char *helptext;
2139
 
2140
  tm_print_insn = gdb_print_insn_arm;
2141
 
2142
  /* Get the number of possible sets of register names defined in opcodes. */
2143
  num_flavor_options = get_arm_regname_num_options ();
2144
 
2145
  /* Sync the opcode insn printer with our register viewer: */
2146
  parse_arm_disassembler_option ("reg-names-std");
2147
 
2148
  /* Begin creating the help text. */
2149
  stb = mem_fileopen ();
2150
  fprintf_unfiltered (stb, "Set the disassembly flavor.\n\
2151
The valid values are:\n");
2152
 
2153
  /* Initialize the array that will be passed to add_set_enum_cmd(). */
2154
  valid_flavors = xmalloc ((num_flavor_options + 1) * sizeof (char *));
2155
  for (i = 0; i < num_flavor_options; i++)
2156
    {
2157
      numregs = get_arm_regnames (i, &setname, &setdesc, &regnames);
2158
      valid_flavors[i] = setname;
2159
      fprintf_unfiltered (stb, "%s - %s\n", setname,
2160
                          setdesc);
2161
      /* Copy the default names (if found) and synchronize disassembler. */
2162
      if (!strcmp (setname, "std"))
2163
        {
2164
          disassembly_flavor = setname;
2165
          current_option = i;
2166
          for (j = 0; j < numregs; j++)
2167
            arm_register_names[j] = (char *) regnames[j];
2168
          set_arm_regname_option (i);
2169
        }
2170
    }
2171
  /* Mark the end of valid options. */
2172
  valid_flavors[num_flavor_options] = NULL;
2173
 
2174
  /* Finish the creation of the help text. */
2175
  fprintf_unfiltered (stb, "The default is \"std\".");
2176
  helptext = ui_file_xstrdup (stb, &length);
2177
  ui_file_delete (stb);
2178
 
2179
  /* Add the disassembly-flavor command */
2180
  new_cmd = add_set_enum_cmd ("disassembly-flavor", no_class,
2181
                              valid_flavors,
2182
                              &disassembly_flavor,
2183
                              helptext,
2184
                              &setlist);
2185
  new_cmd->function.sfunc = set_disassembly_flavor_sfunc;
2186
  add_show_from_set (new_cmd, &showlist);
2187
 
2188
  /* ??? Maybe this should be a boolean.  */
2189
  add_show_from_set (add_set_cmd ("apcs32", no_class,
2190
                                  var_zinteger, (char *) &arm_apcs_32,
2191
                                  "Set usage of ARM 32-bit mode.\n", &setlist),
2192
                     &showlist);
2193
 
2194
  /* Add the deprecated "othernames" command */
2195
 
2196
  add_com ("othernames", class_obscure, arm_othernames,
2197
           "Switch to the next set of register names.");
2198
}
2199
 
2200
/* Test whether the coff symbol specific value corresponds to a Thumb
2201
   function.  */
2202
 
2203
int
2204
coff_sym_is_thumb (int val)
2205
{
2206
  return (val == C_THUMBEXT ||
2207
          val == C_THUMBSTAT ||
2208
          val == C_THUMBEXTFUNC ||
2209
          val == C_THUMBSTATFUNC ||
2210
          val == C_THUMBLABEL);
2211
}

powered by: WebSVN 2.1.0

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