]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/arm-tdep.c
* arm-tdep.c (target_is_thumb, caller_is_thumb)
[thirdparty/binutils-gdb.git] / gdb / arm-tdep.c
1 /* Common target dependent code for GDB on ARM systems.
2
3 Copyright 1988, 1989, 1991, 1992, 1993, 1995, 1996, 1998, 1999,
4 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23 #include <ctype.h> /* XXX for isupper () */
24
25 #include "defs.h"
26 #include "frame.h"
27 #include "inferior.h"
28 #include "gdbcmd.h"
29 #include "gdbcore.h"
30 #include "gdb_string.h"
31 #include "dis-asm.h" /* For register styles. */
32 #include "regcache.h"
33 #include "doublest.h"
34 #include "value.h"
35 #include "arch-utils.h"
36 #include "osabi.h"
37 #include "frame-unwind.h"
38 #include "frame-base.h"
39 #include "trad-frame.h"
40
41 #include "arm-tdep.h"
42 #include "gdb/sim-arm.h"
43
44 #include "elf-bfd.h"
45 #include "coff/internal.h"
46 #include "elf/arm.h"
47
48 #include "gdb_assert.h"
49
50 static int arm_debug;
51
52 /* Each OS has a different mechanism for accessing the various
53 registers stored in the sigcontext structure.
54
55 SIGCONTEXT_REGISTER_ADDRESS should be defined to the name (or
56 function pointer) which may be used to determine the addresses
57 of the various saved registers in the sigcontext structure.
58
59 For the ARM target, there are three parameters to this function.
60 The first is the pc value of the frame under consideration, the
61 second the stack pointer of this frame, and the last is the
62 register number to fetch.
63
64 If the tm.h file does not define this macro, then it's assumed that
65 no mechanism is needed and we define SIGCONTEXT_REGISTER_ADDRESS to
66 be 0.
67
68 When it comes time to multi-arching this code, see the identically
69 named machinery in ia64-tdep.c for an example of how it could be
70 done. It should not be necessary to modify the code below where
71 this macro is used. */
72
73 #ifdef SIGCONTEXT_REGISTER_ADDRESS
74 #ifndef SIGCONTEXT_REGISTER_ADDRESS_P
75 #define SIGCONTEXT_REGISTER_ADDRESS_P() 1
76 #endif
77 #else
78 #define SIGCONTEXT_REGISTER_ADDRESS(SP,PC,REG) 0
79 #define SIGCONTEXT_REGISTER_ADDRESS_P() 0
80 #endif
81
82 /* Macros for setting and testing a bit in a minimal symbol that marks
83 it as Thumb function. The MSB of the minimal symbol's "info" field
84 is used for this purpose.
85
86 MSYMBOL_SET_SPECIAL Actually sets the "special" bit.
87 MSYMBOL_IS_SPECIAL Tests the "special" bit in a minimal symbol. */
88
89 #define MSYMBOL_SET_SPECIAL(msym) \
90 MSYMBOL_INFO (msym) = (char *) (((long) MSYMBOL_INFO (msym)) \
91 | 0x80000000)
92
93 #define MSYMBOL_IS_SPECIAL(msym) \
94 (((long) MSYMBOL_INFO (msym) & 0x80000000) != 0)
95
96 /* The list of available "set arm ..." and "show arm ..." commands. */
97 static struct cmd_list_element *setarmcmdlist = NULL;
98 static struct cmd_list_element *showarmcmdlist = NULL;
99
100 /* The type of floating-point to use. Keep this in sync with enum
101 arm_float_model, and the help string in _initialize_arm_tdep. */
102 static const char *fp_model_strings[] =
103 {
104 "auto",
105 "softfpa",
106 "fpa",
107 "softvfp",
108 "vfp"
109 };
110
111 /* A variable that can be configured by the user. */
112 static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
113 static const char *current_fp_model = "auto";
114
115 /* Number of different reg name sets (options). */
116 static int num_disassembly_options;
117
118 /* We have more registers than the disassembler as gdb can print the value
119 of special registers as well.
120 The general register names are overwritten by whatever is being used by
121 the disassembler at the moment. We also adjust the case of cpsr and fps. */
122
123 /* Initial value: Register names used in ARM's ISA documentation. */
124 static char * arm_register_name_strings[] =
125 {"r0", "r1", "r2", "r3", /* 0 1 2 3 */
126 "r4", "r5", "r6", "r7", /* 4 5 6 7 */
127 "r8", "r9", "r10", "r11", /* 8 9 10 11 */
128 "r12", "sp", "lr", "pc", /* 12 13 14 15 */
129 "f0", "f1", "f2", "f3", /* 16 17 18 19 */
130 "f4", "f5", "f6", "f7", /* 20 21 22 23 */
131 "fps", "cpsr" }; /* 24 25 */
132 static char **arm_register_names = arm_register_name_strings;
133
134 /* Valid register name styles. */
135 static const char **valid_disassembly_styles;
136
137 /* Disassembly style to use. Default to "std" register names. */
138 static const char *disassembly_style;
139 /* Index to that option in the opcodes table. */
140 static int current_option;
141
142 /* This is used to keep the bfd arch_info in sync with the disassembly
143 style. */
144 static void set_disassembly_style_sfunc(char *, int,
145 struct cmd_list_element *);
146 static void set_disassembly_style (void);
147
148 static void convert_from_extended (const struct floatformat *, const void *,
149 void *);
150 static void convert_to_extended (const struct floatformat *, void *,
151 const void *);
152
153 struct arm_prologue_cache
154 {
155 /* The stack pointer at the time this frame was created; i.e. the
156 caller's stack pointer when this function was called. It is used
157 to identify this frame. */
158 CORE_ADDR prev_sp;
159
160 /* The frame base for this frame is just prev_sp + frame offset -
161 frame size. FRAMESIZE is the size of this stack frame, and
162 FRAMEOFFSET if the initial offset from the stack pointer (this
163 frame's stack pointer, not PREV_SP) to the frame base. */
164
165 int framesize;
166 int frameoffset;
167
168 /* The register used to hold the frame pointer for this frame. */
169 int framereg;
170
171 /* Saved register offsets. */
172 struct trad_frame_saved_reg *saved_regs;
173 };
174
175 /* Addresses for calling Thumb functions have the bit 0 set.
176 Here are some macros to test, set, or clear bit 0 of addresses. */
177 #define IS_THUMB_ADDR(addr) ((addr) & 1)
178 #define MAKE_THUMB_ADDR(addr) ((addr) | 1)
179 #define UNMAKE_THUMB_ADDR(addr) ((addr) & ~1)
180
181 /* Set to true if the 32-bit mode is in use. */
182
183 int arm_apcs_32 = 1;
184
185 /* Determine if the program counter specified in MEMADDR is in a Thumb
186 function. */
187
188 int
189 arm_pc_is_thumb (CORE_ADDR memaddr)
190 {
191 struct minimal_symbol *sym;
192
193 /* If bit 0 of the address is set, assume this is a Thumb address. */
194 if (IS_THUMB_ADDR (memaddr))
195 return 1;
196
197 /* Thumb functions have a "special" bit set in minimal symbols. */
198 sym = lookup_minimal_symbol_by_pc (memaddr);
199 if (sym)
200 {
201 return (MSYMBOL_IS_SPECIAL (sym));
202 }
203 else
204 {
205 return 0;
206 }
207 }
208
209 /* Remove useless bits from addresses in a running program. */
210 static CORE_ADDR
211 arm_addr_bits_remove (CORE_ADDR val)
212 {
213 if (arm_apcs_32)
214 return (val & (arm_pc_is_thumb (val) ? 0xfffffffe : 0xfffffffc));
215 else
216 return (val & 0x03fffffc);
217 }
218
219 /* When reading symbols, we need to zap the low bit of the address,
220 which may be set to 1 for Thumb functions. */
221 static CORE_ADDR
222 arm_smash_text_address (CORE_ADDR val)
223 {
224 return val & ~1;
225 }
226
227 /* Immediately after a function call, return the saved pc. Can't
228 always go through the frames for this because on some machines the
229 new frame is not set up until the new function executes some
230 instructions. */
231
232 static CORE_ADDR
233 arm_saved_pc_after_call (struct frame_info *frame)
234 {
235 return ADDR_BITS_REMOVE (read_register (ARM_LR_REGNUM));
236 }
237
238 /* A typical Thumb prologue looks like this:
239 push {r7, lr}
240 add sp, sp, #-28
241 add r7, sp, #12
242 Sometimes the latter instruction may be replaced by:
243 mov r7, sp
244
245 or like this:
246 push {r7, lr}
247 mov r7, sp
248 sub sp, #12
249
250 or, on tpcs, like this:
251 sub sp,#16
252 push {r7, lr}
253 (many instructions)
254 mov r7, sp
255 sub sp, #12
256
257 There is always one instruction of three classes:
258 1 - push
259 2 - setting of r7
260 3 - adjusting of sp
261
262 When we have found at least one of each class we are done with the prolog.
263 Note that the "sub sp, #NN" before the push does not count.
264 */
265
266 static CORE_ADDR
267 thumb_skip_prologue (CORE_ADDR pc, CORE_ADDR func_end)
268 {
269 CORE_ADDR current_pc;
270 /* findmask:
271 bit 0 - push { rlist }
272 bit 1 - mov r7, sp OR add r7, sp, #imm (setting of r7)
273 bit 2 - sub sp, #simm OR add sp, #simm (adjusting of sp)
274 */
275 int findmask = 0;
276
277 for (current_pc = pc;
278 current_pc + 2 < func_end && current_pc < pc + 40;
279 current_pc += 2)
280 {
281 unsigned short insn = read_memory_unsigned_integer (current_pc, 2);
282
283 if ((insn & 0xfe00) == 0xb400) /* push { rlist } */
284 {
285 findmask |= 1; /* push found */
286 }
287 else if ((insn & 0xff00) == 0xb000) /* add sp, #simm OR
288 sub sp, #simm */
289 {
290 if ((findmask & 1) == 0) /* before push ? */
291 continue;
292 else
293 findmask |= 4; /* add/sub sp found */
294 }
295 else if ((insn & 0xff00) == 0xaf00) /* add r7, sp, #imm */
296 {
297 findmask |= 2; /* setting of r7 found */
298 }
299 else if (insn == 0x466f) /* mov r7, sp */
300 {
301 findmask |= 2; /* setting of r7 found */
302 }
303 else if (findmask == (4+2+1))
304 {
305 /* We have found one of each type of prologue instruction */
306 break;
307 }
308 else
309 /* Something in the prolog that we don't care about or some
310 instruction from outside the prolog scheduled here for
311 optimization. */
312 continue;
313 }
314
315 return current_pc;
316 }
317
318 /* Advance the PC across any function entry prologue instructions to
319 reach some "real" code.
320
321 The APCS (ARM Procedure Call Standard) defines the following
322 prologue:
323
324 mov ip, sp
325 [stmfd sp!, {a1,a2,a3,a4}]
326 stmfd sp!, {...,fp,ip,lr,pc}
327 [stfe f7, [sp, #-12]!]
328 [stfe f6, [sp, #-12]!]
329 [stfe f5, [sp, #-12]!]
330 [stfe f4, [sp, #-12]!]
331 sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn */
332
333 static CORE_ADDR
334 arm_skip_prologue (CORE_ADDR pc)
335 {
336 unsigned long inst;
337 CORE_ADDR skip_pc;
338 CORE_ADDR func_addr, func_end = 0;
339 char *func_name;
340 struct symtab_and_line sal;
341
342 /* If we're in a dummy frame, don't even try to skip the prologue. */
343 if (deprecated_pc_in_call_dummy (pc))
344 return pc;
345
346 /* See what the symbol table says. */
347
348 if (find_pc_partial_function (pc, &func_name, &func_addr, &func_end))
349 {
350 struct symbol *sym;
351
352 /* Found a function. */
353 sym = lookup_symbol (func_name, NULL, VAR_DOMAIN, NULL, NULL);
354 if (sym && SYMBOL_LANGUAGE (sym) != language_asm)
355 {
356 /* Don't use this trick for assembly source files. */
357 sal = find_pc_line (func_addr, 0);
358 if ((sal.line != 0) && (sal.end < func_end))
359 return sal.end;
360 }
361 }
362
363 /* Check if this is Thumb code. */
364 if (arm_pc_is_thumb (pc))
365 return thumb_skip_prologue (pc, func_end);
366
367 /* Can't find the prologue end in the symbol table, try it the hard way
368 by disassembling the instructions. */
369
370 /* Like arm_scan_prologue, stop no later than pc + 64. */
371 if (func_end == 0 || func_end > pc + 64)
372 func_end = pc + 64;
373
374 for (skip_pc = pc; skip_pc < func_end; skip_pc += 4)
375 {
376 inst = read_memory_integer (skip_pc, 4);
377
378 /* "mov ip, sp" is no longer a required part of the prologue. */
379 if (inst == 0xe1a0c00d) /* mov ip, sp */
380 continue;
381
382 if ((inst & 0xfffff000) == 0xe28dc000) /* add ip, sp #n */
383 continue;
384
385 if ((inst & 0xfffff000) == 0xe24dc000) /* sub ip, sp #n */
386 continue;
387
388 /* Some prologues begin with "str lr, [sp, #-4]!". */
389 if (inst == 0xe52de004) /* str lr, [sp, #-4]! */
390 continue;
391
392 if ((inst & 0xfffffff0) == 0xe92d0000) /* stmfd sp!,{a1,a2,a3,a4} */
393 continue;
394
395 if ((inst & 0xfffff800) == 0xe92dd800) /* stmfd sp!,{fp,ip,lr,pc} */
396 continue;
397
398 /* Any insns after this point may float into the code, if it makes
399 for better instruction scheduling, so we skip them only if we
400 find them, but still consider the function to be frame-ful. */
401
402 /* We may have either one sfmfd instruction here, or several stfe
403 insns, depending on the version of floating point code we
404 support. */
405 if ((inst & 0xffbf0fff) == 0xec2d0200) /* sfmfd fn, <cnt>, [sp]! */
406 continue;
407
408 if ((inst & 0xffff8fff) == 0xed6d0103) /* stfe fn, [sp, #-12]! */
409 continue;
410
411 if ((inst & 0xfffff000) == 0xe24cb000) /* sub fp, ip, #nn */
412 continue;
413
414 if ((inst & 0xfffff000) == 0xe24dd000) /* sub sp, sp, #nn */
415 continue;
416
417 if ((inst & 0xffffc000) == 0xe54b0000 || /* strb r(0123),[r11,#-nn] */
418 (inst & 0xffffc0f0) == 0xe14b00b0 || /* strh r(0123),[r11,#-nn] */
419 (inst & 0xffffc000) == 0xe50b0000) /* str r(0123),[r11,#-nn] */
420 continue;
421
422 if ((inst & 0xffffc000) == 0xe5cd0000 || /* strb r(0123),[sp,#nn] */
423 (inst & 0xffffc0f0) == 0xe1cd00b0 || /* strh r(0123),[sp,#nn] */
424 (inst & 0xffffc000) == 0xe58d0000) /* str r(0123),[sp,#nn] */
425 continue;
426
427 /* Un-recognized instruction; stop scanning. */
428 break;
429 }
430
431 return skip_pc; /* End of prologue */
432 }
433
434 /* *INDENT-OFF* */
435 /* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
436 This function decodes a Thumb function prologue to determine:
437 1) the size of the stack frame
438 2) which registers are saved on it
439 3) the offsets of saved regs
440 4) the offset from the stack pointer to the frame pointer
441
442 A typical Thumb function prologue would create this stack frame
443 (offsets relative to FP)
444 old SP -> 24 stack parameters
445 20 LR
446 16 R7
447 R7 -> 0 local variables (16 bytes)
448 SP -> -12 additional stack space (12 bytes)
449 The frame size would thus be 36 bytes, and the frame offset would be
450 12 bytes. The frame register is R7.
451
452 The comments for thumb_skip_prolog() describe the algorithm we use
453 to detect the end of the prolog. */
454 /* *INDENT-ON* */
455
456 static void
457 thumb_scan_prologue (CORE_ADDR prev_pc, struct arm_prologue_cache *cache)
458 {
459 CORE_ADDR prologue_start;
460 CORE_ADDR prologue_end;
461 CORE_ADDR current_pc;
462 /* Which register has been copied to register n? */
463 int saved_reg[16];
464 /* findmask:
465 bit 0 - push { rlist }
466 bit 1 - mov r7, sp OR add r7, sp, #imm (setting of r7)
467 bit 2 - sub sp, #simm OR add sp, #simm (adjusting of sp)
468 */
469 int findmask = 0;
470 int i;
471
472 if (find_pc_partial_function (prev_pc, NULL, &prologue_start, &prologue_end))
473 {
474 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
475
476 if (sal.line == 0) /* no line info, use current PC */
477 prologue_end = prev_pc;
478 else if (sal.end < prologue_end) /* next line begins after fn end */
479 prologue_end = sal.end; /* (probably means no prologue) */
480 }
481 else
482 /* We're in the boondocks: allow for
483 16 pushes, an add, and "mv fp,sp". */
484 prologue_end = prologue_start + 40;
485
486 prologue_end = min (prologue_end, prev_pc);
487
488 /* Initialize the saved register map. When register H is copied to
489 register L, we will put H in saved_reg[L]. */
490 for (i = 0; i < 16; i++)
491 saved_reg[i] = i;
492
493 /* Search the prologue looking for instructions that set up the
494 frame pointer, adjust the stack pointer, and save registers.
495 Do this until all basic prolog instructions are found. */
496
497 cache->framesize = 0;
498 for (current_pc = prologue_start;
499 (current_pc < prologue_end) && ((findmask & 7) != 7);
500 current_pc += 2)
501 {
502 unsigned short insn;
503 int regno;
504 int offset;
505
506 insn = read_memory_unsigned_integer (current_pc, 2);
507
508 if ((insn & 0xfe00) == 0xb400) /* push { rlist } */
509 {
510 int mask;
511 findmask |= 1; /* push found */
512 /* Bits 0-7 contain a mask for registers R0-R7. Bit 8 says
513 whether to save LR (R14). */
514 mask = (insn & 0xff) | ((insn & 0x100) << 6);
515
516 /* Calculate offsets of saved R0-R7 and LR. */
517 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
518 if (mask & (1 << regno))
519 {
520 cache->framesize += 4;
521 cache->saved_regs[saved_reg[regno]].addr = -cache->framesize;
522 /* Reset saved register map. */
523 saved_reg[regno] = regno;
524 }
525 }
526 else if ((insn & 0xff00) == 0xb000) /* add sp, #simm OR
527 sub sp, #simm */
528 {
529 if ((findmask & 1) == 0) /* before push? */
530 continue;
531 else
532 findmask |= 4; /* add/sub sp found */
533
534 offset = (insn & 0x7f) << 2; /* get scaled offset */
535 if (insn & 0x80) /* is it signed? (==subtracting) */
536 {
537 cache->frameoffset += offset;
538 offset = -offset;
539 }
540 cache->framesize -= offset;
541 }
542 else if ((insn & 0xff00) == 0xaf00) /* add r7, sp, #imm */
543 {
544 findmask |= 2; /* setting of r7 found */
545 cache->framereg = THUMB_FP_REGNUM;
546 /* get scaled offset */
547 cache->frameoffset = (insn & 0xff) << 2;
548 }
549 else if (insn == 0x466f) /* mov r7, sp */
550 {
551 findmask |= 2; /* setting of r7 found */
552 cache->framereg = THUMB_FP_REGNUM;
553 cache->frameoffset = 0;
554 saved_reg[THUMB_FP_REGNUM] = ARM_SP_REGNUM;
555 }
556 else if ((insn & 0xffc0) == 0x4640) /* mov r0-r7, r8-r15 */
557 {
558 int lo_reg = insn & 7; /* dest. register (r0-r7) */
559 int hi_reg = ((insn >> 3) & 7) + 8; /* source register (r8-15) */
560 saved_reg[lo_reg] = hi_reg; /* remember hi reg was saved */
561 }
562 else
563 /* Something in the prolog that we don't care about or some
564 instruction from outside the prolog scheduled here for
565 optimization. */
566 continue;
567 }
568 }
569
570 /* This function decodes an ARM function prologue to determine:
571 1) the size of the stack frame
572 2) which registers are saved on it
573 3) the offsets of saved regs
574 4) the offset from the stack pointer to the frame pointer
575 This information is stored in the "extra" fields of the frame_info.
576
577 There are two basic forms for the ARM prologue. The fixed argument
578 function call will look like:
579
580 mov ip, sp
581 stmfd sp!, {fp, ip, lr, pc}
582 sub fp, ip, #4
583 [sub sp, sp, #4]
584
585 Which would create this stack frame (offsets relative to FP):
586 IP -> 4 (caller's stack)
587 FP -> 0 PC (points to address of stmfd instruction + 8 in callee)
588 -4 LR (return address in caller)
589 -8 IP (copy of caller's SP)
590 -12 FP (caller's FP)
591 SP -> -28 Local variables
592
593 The frame size would thus be 32 bytes, and the frame offset would be
594 28 bytes. The stmfd call can also save any of the vN registers it
595 plans to use, which increases the frame size accordingly.
596
597 Note: The stored PC is 8 off of the STMFD instruction that stored it
598 because the ARM Store instructions always store PC + 8 when you read
599 the PC register.
600
601 A variable argument function call will look like:
602
603 mov ip, sp
604 stmfd sp!, {a1, a2, a3, a4}
605 stmfd sp!, {fp, ip, lr, pc}
606 sub fp, ip, #20
607
608 Which would create this stack frame (offsets relative to FP):
609 IP -> 20 (caller's stack)
610 16 A4
611 12 A3
612 8 A2
613 4 A1
614 FP -> 0 PC (points to address of stmfd instruction + 8 in callee)
615 -4 LR (return address in caller)
616 -8 IP (copy of caller's SP)
617 -12 FP (caller's FP)
618 SP -> -28 Local variables
619
620 The frame size would thus be 48 bytes, and the frame offset would be
621 28 bytes.
622
623 There is another potential complication, which is that the optimizer
624 will try to separate the store of fp in the "stmfd" instruction from
625 the "sub fp, ip, #NN" instruction. Almost anything can be there, so
626 we just key on the stmfd, and then scan for the "sub fp, ip, #NN"...
627
628 Also, note, the original version of the ARM toolchain claimed that there
629 should be an
630
631 instruction at the end of the prologue. I have never seen GCC produce
632 this, and the ARM docs don't mention it. We still test for it below in
633 case it happens...
634
635 */
636
637 static void
638 arm_scan_prologue (struct frame_info *next_frame, struct arm_prologue_cache *cache)
639 {
640 int regno, sp_offset, fp_offset, ip_offset;
641 CORE_ADDR prologue_start, prologue_end, current_pc;
642 CORE_ADDR prev_pc = frame_pc_unwind (next_frame);
643
644 /* Assume there is no frame until proven otherwise. */
645 cache->framereg = ARM_SP_REGNUM;
646 cache->framesize = 0;
647 cache->frameoffset = 0;
648
649 /* Check for Thumb prologue. */
650 if (arm_pc_is_thumb (prev_pc))
651 {
652 thumb_scan_prologue (prev_pc, cache);
653 return;
654 }
655
656 /* Find the function prologue. If we can't find the function in
657 the symbol table, peek in the stack frame to find the PC. */
658 if (find_pc_partial_function (prev_pc, NULL, &prologue_start, &prologue_end))
659 {
660 /* One way to find the end of the prologue (which works well
661 for unoptimized code) is to do the following:
662
663 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
664
665 if (sal.line == 0)
666 prologue_end = prev_pc;
667 else if (sal.end < prologue_end)
668 prologue_end = sal.end;
669
670 This mechanism is very accurate so long as the optimizer
671 doesn't move any instructions from the function body into the
672 prologue. If this happens, sal.end will be the last
673 instruction in the first hunk of prologue code just before
674 the first instruction that the scheduler has moved from
675 the body to the prologue.
676
677 In order to make sure that we scan all of the prologue
678 instructions, we use a slightly less accurate mechanism which
679 may scan more than necessary. To help compensate for this
680 lack of accuracy, the prologue scanning loop below contains
681 several clauses which'll cause the loop to terminate early if
682 an implausible prologue instruction is encountered.
683
684 The expression
685
686 prologue_start + 64
687
688 is a suitable endpoint since it accounts for the largest
689 possible prologue plus up to five instructions inserted by
690 the scheduler. */
691
692 if (prologue_end > prologue_start + 64)
693 {
694 prologue_end = prologue_start + 64; /* See above. */
695 }
696 }
697 else
698 {
699 /* We have no symbol information. Our only option is to assume this
700 function has a standard stack frame and the normal frame register.
701 Then, we can find the value of our frame pointer on entrance to
702 the callee (or at the present moment if this is the innermost frame).
703 The value stored there should be the address of the stmfd + 8. */
704 CORE_ADDR frame_loc;
705 LONGEST return_value;
706
707 frame_loc = frame_unwind_register_unsigned (next_frame, ARM_FP_REGNUM);
708 if (!safe_read_memory_integer (frame_loc, 4, &return_value))
709 return;
710 else
711 {
712 prologue_start = ADDR_BITS_REMOVE (return_value) - 8;
713 prologue_end = prologue_start + 64; /* See above. */
714 }
715 }
716
717 if (prev_pc < prologue_end)
718 prologue_end = prev_pc;
719
720 /* Now search the prologue looking for instructions that set up the
721 frame pointer, adjust the stack pointer, and save registers.
722
723 Be careful, however, and if it doesn't look like a prologue,
724 don't try to scan it. If, for instance, a frameless function
725 begins with stmfd sp!, then we will tell ourselves there is
726 a frame, which will confuse stack traceback, as well as "finish"
727 and other operations that rely on a knowledge of the stack
728 traceback.
729
730 In the APCS, the prologue should start with "mov ip, sp" so
731 if we don't see this as the first insn, we will stop.
732
733 [Note: This doesn't seem to be true any longer, so it's now an
734 optional part of the prologue. - Kevin Buettner, 2001-11-20]
735
736 [Note further: The "mov ip,sp" only seems to be missing in
737 frameless functions at optimization level "-O2" or above,
738 in which case it is often (but not always) replaced by
739 "str lr, [sp, #-4]!". - Michael Snyder, 2002-04-23] */
740
741 sp_offset = fp_offset = ip_offset = 0;
742
743 for (current_pc = prologue_start;
744 current_pc < prologue_end;
745 current_pc += 4)
746 {
747 unsigned int insn = read_memory_unsigned_integer (current_pc, 4);
748
749 if (insn == 0xe1a0c00d) /* mov ip, sp */
750 {
751 ip_offset = 0;
752 continue;
753 }
754 else if ((insn & 0xfffff000) == 0xe28dc000) /* add ip, sp #n */
755 {
756 unsigned imm = insn & 0xff; /* immediate value */
757 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
758 imm = (imm >> rot) | (imm << (32 - rot));
759 ip_offset = imm;
760 continue;
761 }
762 else if ((insn & 0xfffff000) == 0xe24dc000) /* sub ip, sp #n */
763 {
764 unsigned imm = insn & 0xff; /* immediate value */
765 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
766 imm = (imm >> rot) | (imm << (32 - rot));
767 ip_offset = -imm;
768 continue;
769 }
770 else if (insn == 0xe52de004) /* str lr, [sp, #-4]! */
771 {
772 sp_offset -= 4;
773 cache->saved_regs[ARM_LR_REGNUM].addr = sp_offset;
774 continue;
775 }
776 else if ((insn & 0xffff0000) == 0xe92d0000)
777 /* stmfd sp!, {..., fp, ip, lr, pc}
778 or
779 stmfd sp!, {a1, a2, a3, a4} */
780 {
781 int mask = insn & 0xffff;
782
783 /* Calculate offsets of saved registers. */
784 for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
785 if (mask & (1 << regno))
786 {
787 sp_offset -= 4;
788 cache->saved_regs[regno].addr = sp_offset;
789 }
790 }
791 else if ((insn & 0xffffc000) == 0xe54b0000 || /* strb rx,[r11,#-n] */
792 (insn & 0xffffc0f0) == 0xe14b00b0 || /* strh rx,[r11,#-n] */
793 (insn & 0xffffc000) == 0xe50b0000) /* str rx,[r11,#-n] */
794 {
795 /* No need to add this to saved_regs -- it's just an arg reg. */
796 continue;
797 }
798 else if ((insn & 0xffffc000) == 0xe5cd0000 || /* strb rx,[sp,#n] */
799 (insn & 0xffffc0f0) == 0xe1cd00b0 || /* strh rx,[sp,#n] */
800 (insn & 0xffffc000) == 0xe58d0000) /* str rx,[sp,#n] */
801 {
802 /* No need to add this to saved_regs -- it's just an arg reg. */
803 continue;
804 }
805 else if ((insn & 0xfffff000) == 0xe24cb000) /* sub fp, ip #n */
806 {
807 unsigned imm = insn & 0xff; /* immediate value */
808 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
809 imm = (imm >> rot) | (imm << (32 - rot));
810 fp_offset = -imm + ip_offset;
811 cache->framereg = ARM_FP_REGNUM;
812 }
813 else if ((insn & 0xfffff000) == 0xe24dd000) /* sub sp, sp #n */
814 {
815 unsigned imm = insn & 0xff; /* immediate value */
816 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
817 imm = (imm >> rot) | (imm << (32 - rot));
818 sp_offset -= imm;
819 }
820 else if ((insn & 0xffff7fff) == 0xed6d0103) /* stfe f?, [sp, -#c]! */
821 {
822 sp_offset -= 12;
823 regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
824 cache->saved_regs[regno].addr = sp_offset;
825 }
826 else if ((insn & 0xffbf0fff) == 0xec2d0200) /* sfmfd f0, 4, [sp!] */
827 {
828 int n_saved_fp_regs;
829 unsigned int fp_start_reg, fp_bound_reg;
830
831 if ((insn & 0x800) == 0x800) /* N0 is set */
832 {
833 if ((insn & 0x40000) == 0x40000) /* N1 is set */
834 n_saved_fp_regs = 3;
835 else
836 n_saved_fp_regs = 1;
837 }
838 else
839 {
840 if ((insn & 0x40000) == 0x40000) /* N1 is set */
841 n_saved_fp_regs = 2;
842 else
843 n_saved_fp_regs = 4;
844 }
845
846 fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
847 fp_bound_reg = fp_start_reg + n_saved_fp_regs;
848 for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
849 {
850 sp_offset -= 12;
851 cache->saved_regs[fp_start_reg++].addr = sp_offset;
852 }
853 }
854 else if ((insn & 0xf0000000) != 0xe0000000)
855 break; /* Condition not true, exit early */
856 else if ((insn & 0xfe200000) == 0xe8200000) /* ldm? */
857 break; /* Don't scan past a block load */
858 else
859 /* The optimizer might shove anything into the prologue,
860 so we just skip what we don't recognize. */
861 continue;
862 }
863
864 /* The frame size is just the negative of the offset (from the
865 original SP) of the last thing thing we pushed on the stack.
866 The frame offset is [new FP] - [new SP]. */
867 cache->framesize = -sp_offset;
868 if (cache->framereg == ARM_FP_REGNUM)
869 cache->frameoffset = fp_offset - sp_offset;
870 else
871 cache->frameoffset = 0;
872 }
873
874 static struct arm_prologue_cache *
875 arm_make_prologue_cache (struct frame_info *next_frame)
876 {
877 int reg;
878 struct arm_prologue_cache *cache;
879 CORE_ADDR unwound_fp;
880
881 cache = frame_obstack_zalloc (sizeof (struct arm_prologue_cache));
882 cache->saved_regs = trad_frame_alloc_saved_regs (next_frame);
883
884 arm_scan_prologue (next_frame, cache);
885
886 unwound_fp = frame_unwind_register_unsigned (next_frame, cache->framereg);
887 if (unwound_fp == 0)
888 return cache;
889
890 cache->prev_sp = unwound_fp + cache->framesize - cache->frameoffset;
891
892 /* Calculate actual addresses of saved registers using offsets
893 determined by arm_scan_prologue. */
894 for (reg = 0; reg < NUM_REGS; reg++)
895 if (trad_frame_addr_p (cache->saved_regs, reg))
896 cache->saved_regs[reg].addr += cache->prev_sp;
897
898 return cache;
899 }
900
901 /* Our frame ID for a normal frame is the current function's starting PC
902 and the caller's SP when we were called. */
903
904 static void
905 arm_prologue_this_id (struct frame_info *next_frame,
906 void **this_cache,
907 struct frame_id *this_id)
908 {
909 struct arm_prologue_cache *cache;
910 struct frame_id id;
911 CORE_ADDR func;
912
913 if (*this_cache == NULL)
914 *this_cache = arm_make_prologue_cache (next_frame);
915 cache = *this_cache;
916
917 func = frame_func_unwind (next_frame);
918
919 /* This is meant to halt the backtrace at "_start". Make sure we
920 don't halt it at a generic dummy frame. */
921 if (func <= LOWEST_PC)
922 return;
923
924 /* If we've hit a wall, stop. */
925 if (cache->prev_sp == 0)
926 return;
927
928 id = frame_id_build (cache->prev_sp, func);
929 *this_id = id;
930 }
931
932 static void
933 arm_prologue_prev_register (struct frame_info *next_frame,
934 void **this_cache,
935 int prev_regnum,
936 int *optimized,
937 enum lval_type *lvalp,
938 CORE_ADDR *addrp,
939 int *realnump,
940 void *valuep)
941 {
942 struct arm_prologue_cache *cache;
943
944 if (*this_cache == NULL)
945 *this_cache = arm_make_prologue_cache (next_frame);
946 cache = *this_cache;
947
948 /* If we are asked to unwind the PC, then we need to return the LR
949 instead. The saved value of PC points into this frame's
950 prologue, not the next frame's resume location. */
951 if (prev_regnum == ARM_PC_REGNUM)
952 prev_regnum = ARM_LR_REGNUM;
953
954 /* SP is generally not saved to the stack, but this frame is
955 identified by NEXT_FRAME's stack pointer at the time of the call.
956 The value was already reconstructed into PREV_SP. */
957 if (prev_regnum == ARM_SP_REGNUM)
958 {
959 *lvalp = not_lval;
960 if (valuep)
961 store_unsigned_integer (valuep, 4, cache->prev_sp);
962 return;
963 }
964
965 trad_frame_get_prev_register (next_frame, cache->saved_regs, prev_regnum,
966 optimized, lvalp, addrp, realnump, valuep);
967 }
968
969 struct frame_unwind arm_prologue_unwind = {
970 NORMAL_FRAME,
971 arm_prologue_this_id,
972 arm_prologue_prev_register
973 };
974
975 static const struct frame_unwind *
976 arm_prologue_unwind_sniffer (struct frame_info *next_frame)
977 {
978 return &arm_prologue_unwind;
979 }
980
981 static struct arm_prologue_cache *
982 arm_make_stub_cache (struct frame_info *next_frame)
983 {
984 int reg;
985 struct arm_prologue_cache *cache;
986 CORE_ADDR unwound_fp;
987
988 cache = frame_obstack_zalloc (sizeof (struct arm_prologue_cache));
989 cache->saved_regs = trad_frame_alloc_saved_regs (next_frame);
990
991 cache->prev_sp = frame_unwind_register_unsigned (next_frame, ARM_SP_REGNUM);
992
993 return cache;
994 }
995
996 /* Our frame ID for a stub frame is the current SP and LR. */
997
998 static void
999 arm_stub_this_id (struct frame_info *next_frame,
1000 void **this_cache,
1001 struct frame_id *this_id)
1002 {
1003 struct arm_prologue_cache *cache;
1004
1005 if (*this_cache == NULL)
1006 *this_cache = arm_make_stub_cache (next_frame);
1007 cache = *this_cache;
1008
1009 *this_id = frame_id_build (cache->prev_sp,
1010 frame_pc_unwind (next_frame));
1011 }
1012
1013 struct frame_unwind arm_stub_unwind = {
1014 NORMAL_FRAME,
1015 arm_stub_this_id,
1016 arm_prologue_prev_register
1017 };
1018
1019 static const struct frame_unwind *
1020 arm_stub_unwind_sniffer (struct frame_info *next_frame)
1021 {
1022 char dummy[4];
1023
1024 if (in_plt_section (frame_unwind_address_in_block (next_frame), NULL)
1025 || target_read_memory (frame_pc_unwind (next_frame), dummy, 4) != 0)
1026 return &arm_stub_unwind;
1027
1028 return NULL;
1029 }
1030
1031 static CORE_ADDR
1032 arm_normal_frame_base (struct frame_info *next_frame, void **this_cache)
1033 {
1034 struct arm_prologue_cache *cache;
1035
1036 if (*this_cache == NULL)
1037 *this_cache = arm_make_prologue_cache (next_frame);
1038 cache = *this_cache;
1039
1040 return cache->prev_sp + cache->frameoffset - cache->framesize;
1041 }
1042
1043 struct frame_base arm_normal_base = {
1044 &arm_prologue_unwind,
1045 arm_normal_frame_base,
1046 arm_normal_frame_base,
1047 arm_normal_frame_base
1048 };
1049
1050 static struct arm_prologue_cache *
1051 arm_make_sigtramp_cache (struct frame_info *next_frame)
1052 {
1053 struct arm_prologue_cache *cache;
1054 int reg;
1055
1056 cache = frame_obstack_zalloc (sizeof (struct arm_prologue_cache));
1057
1058 cache->prev_sp = frame_unwind_register_unsigned (next_frame, ARM_SP_REGNUM);
1059
1060 cache->saved_regs = trad_frame_alloc_saved_regs (next_frame);
1061
1062 for (reg = 0; reg < NUM_REGS; reg++)
1063 cache->saved_regs[reg].addr
1064 = SIGCONTEXT_REGISTER_ADDRESS (cache->prev_sp,
1065 frame_pc_unwind (next_frame), reg);
1066
1067 /* FIXME: What about thumb mode? */
1068 cache->framereg = ARM_SP_REGNUM;
1069 cache->prev_sp
1070 = read_memory_integer (cache->saved_regs[cache->framereg].addr,
1071 register_size (current_gdbarch, cache->framereg));
1072
1073 return cache;
1074 }
1075
1076 static void
1077 arm_sigtramp_this_id (struct frame_info *next_frame,
1078 void **this_cache,
1079 struct frame_id *this_id)
1080 {
1081 struct arm_prologue_cache *cache;
1082
1083 if (*this_cache == NULL)
1084 *this_cache = arm_make_sigtramp_cache (next_frame);
1085 cache = *this_cache;
1086
1087 /* FIXME drow/2003-07-07: This isn't right if we single-step within
1088 the sigtramp frame; the PC should be the beginning of the trampoline. */
1089 *this_id = frame_id_build (cache->prev_sp, frame_pc_unwind (next_frame));
1090 }
1091
1092 static void
1093 arm_sigtramp_prev_register (struct frame_info *next_frame,
1094 void **this_cache,
1095 int prev_regnum,
1096 int *optimized,
1097 enum lval_type *lvalp,
1098 CORE_ADDR *addrp,
1099 int *realnump,
1100 void *valuep)
1101 {
1102 struct arm_prologue_cache *cache;
1103
1104 if (*this_cache == NULL)
1105 *this_cache = arm_make_sigtramp_cache (next_frame);
1106 cache = *this_cache;
1107
1108 trad_frame_get_prev_register (next_frame, cache->saved_regs, prev_regnum,
1109 optimized, lvalp, addrp, realnump, valuep);
1110 }
1111
1112 struct frame_unwind arm_sigtramp_unwind = {
1113 SIGTRAMP_FRAME,
1114 arm_sigtramp_this_id,
1115 arm_sigtramp_prev_register
1116 };
1117
1118 static const struct frame_unwind *
1119 arm_sigtramp_unwind_sniffer (struct frame_info *next_frame)
1120 {
1121 if (SIGCONTEXT_REGISTER_ADDRESS_P ()
1122 && legacy_pc_in_sigtramp (frame_pc_unwind (next_frame), (char *) 0))
1123 return &arm_sigtramp_unwind;
1124
1125 return NULL;
1126 }
1127
1128 /* Assuming NEXT_FRAME->prev is a dummy, return the frame ID of that
1129 dummy frame. The frame ID's base needs to match the TOS value
1130 saved by save_dummy_frame_tos() and returned from
1131 arm_push_dummy_call, and the PC needs to match the dummy frame's
1132 breakpoint. */
1133
1134 static struct frame_id
1135 arm_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
1136 {
1137 return frame_id_build (frame_unwind_register_unsigned (next_frame, ARM_SP_REGNUM),
1138 frame_pc_unwind (next_frame));
1139 }
1140
1141 /* Given THIS_FRAME, find the previous frame's resume PC (which will
1142 be used to construct the previous frame's ID, after looking up the
1143 containing function). */
1144
1145 static CORE_ADDR
1146 arm_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
1147 {
1148 CORE_ADDR pc;
1149 pc = frame_unwind_register_unsigned (this_frame, ARM_PC_REGNUM);
1150 return IS_THUMB_ADDR (pc) ? UNMAKE_THUMB_ADDR (pc) : pc;
1151 }
1152
1153 static CORE_ADDR
1154 arm_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
1155 {
1156 return frame_unwind_register_unsigned (this_frame, ARM_SP_REGNUM);
1157 }
1158
1159 /* When arguments must be pushed onto the stack, they go on in reverse
1160 order. The code below implements a FILO (stack) to do this. */
1161
1162 struct stack_item
1163 {
1164 int len;
1165 struct stack_item *prev;
1166 void *data;
1167 };
1168
1169 static struct stack_item *
1170 push_stack_item (struct stack_item *prev, void *contents, int len)
1171 {
1172 struct stack_item *si;
1173 si = xmalloc (sizeof (struct stack_item));
1174 si->data = xmalloc (len);
1175 si->len = len;
1176 si->prev = prev;
1177 memcpy (si->data, contents, len);
1178 return si;
1179 }
1180
1181 static struct stack_item *
1182 pop_stack_item (struct stack_item *si)
1183 {
1184 struct stack_item *dead = si;
1185 si = si->prev;
1186 xfree (dead->data);
1187 xfree (dead);
1188 return si;
1189 }
1190
1191 /* We currently only support passing parameters in integer registers. This
1192 conforms with GCC's default model. Several other variants exist and
1193 we should probably support some of them based on the selected ABI. */
1194
1195 static CORE_ADDR
1196 arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
1197 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
1198 struct value **args, CORE_ADDR sp, int struct_return,
1199 CORE_ADDR struct_addr)
1200 {
1201 int argnum;
1202 int argreg;
1203 int nstack;
1204 struct stack_item *si = NULL;
1205
1206 /* Set the return address. For the ARM, the return breakpoint is
1207 always at BP_ADDR. */
1208 /* XXX Fix for Thumb. */
1209 regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
1210
1211 /* Walk through the list of args and determine how large a temporary
1212 stack is required. Need to take care here as structs may be
1213 passed on the stack, and we have to to push them. */
1214 nstack = 0;
1215
1216 argreg = ARM_A1_REGNUM;
1217 nstack = 0;
1218
1219 /* Some platforms require a double-word aligned stack. Make sure sp
1220 is correctly aligned before we start. We always do this even if
1221 it isn't really needed -- it can never hurt things. */
1222 sp &= ~(CORE_ADDR)(2 * DEPRECATED_REGISTER_SIZE - 1);
1223
1224 /* The struct_return pointer occupies the first parameter
1225 passing register. */
1226 if (struct_return)
1227 {
1228 if (arm_debug)
1229 fprintf_unfiltered (gdb_stdlog, "struct return in %s = 0x%s\n",
1230 REGISTER_NAME (argreg), paddr (struct_addr));
1231 regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
1232 argreg++;
1233 }
1234
1235 for (argnum = 0; argnum < nargs; argnum++)
1236 {
1237 int len;
1238 struct type *arg_type;
1239 struct type *target_type;
1240 enum type_code typecode;
1241 bfd_byte *val;
1242
1243 arg_type = check_typedef (value_type (args[argnum]));
1244 len = TYPE_LENGTH (arg_type);
1245 target_type = TYPE_TARGET_TYPE (arg_type);
1246 typecode = TYPE_CODE (arg_type);
1247 val = value_contents_writeable (args[argnum]);
1248
1249 /* If the argument is a pointer to a function, and it is a
1250 Thumb function, create a LOCAL copy of the value and set
1251 the THUMB bit in it. */
1252 if (TYPE_CODE_PTR == typecode
1253 && target_type != NULL
1254 && TYPE_CODE_FUNC == TYPE_CODE (target_type))
1255 {
1256 CORE_ADDR regval = extract_unsigned_integer (val, len);
1257 if (arm_pc_is_thumb (regval))
1258 {
1259 val = alloca (len);
1260 store_unsigned_integer (val, len, MAKE_THUMB_ADDR (regval));
1261 }
1262 }
1263
1264 /* Copy the argument to general registers or the stack in
1265 register-sized pieces. Large arguments are split between
1266 registers and stack. */
1267 while (len > 0)
1268 {
1269 int partial_len = len < DEPRECATED_REGISTER_SIZE ? len : DEPRECATED_REGISTER_SIZE;
1270
1271 if (argreg <= ARM_LAST_ARG_REGNUM)
1272 {
1273 /* The argument is being passed in a general purpose
1274 register. */
1275 CORE_ADDR regval = extract_unsigned_integer (val, partial_len);
1276 if (arm_debug)
1277 fprintf_unfiltered (gdb_stdlog, "arg %d in %s = 0x%s\n",
1278 argnum, REGISTER_NAME (argreg),
1279 phex (regval, DEPRECATED_REGISTER_SIZE));
1280 regcache_cooked_write_unsigned (regcache, argreg, regval);
1281 argreg++;
1282 }
1283 else
1284 {
1285 /* Push the arguments onto the stack. */
1286 if (arm_debug)
1287 fprintf_unfiltered (gdb_stdlog, "arg %d @ sp + %d\n",
1288 argnum, nstack);
1289 si = push_stack_item (si, val, DEPRECATED_REGISTER_SIZE);
1290 nstack += DEPRECATED_REGISTER_SIZE;
1291 }
1292
1293 len -= partial_len;
1294 val += partial_len;
1295 }
1296 }
1297 /* If we have an odd number of words to push, then decrement the stack
1298 by one word now, so first stack argument will be dword aligned. */
1299 if (nstack & 4)
1300 sp -= 4;
1301
1302 while (si)
1303 {
1304 sp -= si->len;
1305 write_memory (sp, si->data, si->len);
1306 si = pop_stack_item (si);
1307 }
1308
1309 /* Finally, update teh SP register. */
1310 regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
1311
1312 return sp;
1313 }
1314
1315 static void
1316 print_fpu_flags (int flags)
1317 {
1318 if (flags & (1 << 0))
1319 fputs ("IVO ", stdout);
1320 if (flags & (1 << 1))
1321 fputs ("DVZ ", stdout);
1322 if (flags & (1 << 2))
1323 fputs ("OFL ", stdout);
1324 if (flags & (1 << 3))
1325 fputs ("UFL ", stdout);
1326 if (flags & (1 << 4))
1327 fputs ("INX ", stdout);
1328 putchar ('\n');
1329 }
1330
1331 /* Print interesting information about the floating point processor
1332 (if present) or emulator. */
1333 static void
1334 arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
1335 struct frame_info *frame, const char *args)
1336 {
1337 unsigned long status = read_register (ARM_FPS_REGNUM);
1338 int type;
1339
1340 type = (status >> 24) & 127;
1341 if (status & (1 << 31))
1342 printf (_("Hardware FPU type %d\n"), type);
1343 else
1344 printf (_("Software FPU type %d\n"), type);
1345 /* i18n: [floating point unit] mask */
1346 fputs (_("mask: "), stdout);
1347 print_fpu_flags (status >> 16);
1348 /* i18n: [floating point unit] flags */
1349 fputs (_("flags: "), stdout);
1350 print_fpu_flags (status);
1351 }
1352
1353 /* Return the GDB type object for the "standard" data type of data in
1354 register N. */
1355
1356 static struct type *
1357 arm_register_type (struct gdbarch *gdbarch, int regnum)
1358 {
1359 if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
1360 {
1361 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
1362 return builtin_type_arm_ext_big;
1363 else
1364 return builtin_type_arm_ext_littlebyte_bigword;
1365 }
1366 else
1367 return builtin_type_int32;
1368 }
1369
1370 /* Index within `registers' of the first byte of the space for
1371 register N. */
1372
1373 static int
1374 arm_register_byte (int regnum)
1375 {
1376 if (regnum < ARM_F0_REGNUM)
1377 return regnum * INT_REGISTER_SIZE;
1378 else if (regnum < ARM_PS_REGNUM)
1379 return (NUM_GREGS * INT_REGISTER_SIZE
1380 + (regnum - ARM_F0_REGNUM) * FP_REGISTER_SIZE);
1381 else
1382 return (NUM_GREGS * INT_REGISTER_SIZE
1383 + NUM_FREGS * FP_REGISTER_SIZE
1384 + (regnum - ARM_FPS_REGNUM) * STATUS_REGISTER_SIZE);
1385 }
1386
1387 /* Map GDB internal REGNUM onto the Arm simulator register numbers. */
1388 static int
1389 arm_register_sim_regno (int regnum)
1390 {
1391 int reg = regnum;
1392 gdb_assert (reg >= 0 && reg < NUM_REGS);
1393
1394 if (reg < NUM_GREGS)
1395 return SIM_ARM_R0_REGNUM + reg;
1396 reg -= NUM_GREGS;
1397
1398 if (reg < NUM_FREGS)
1399 return SIM_ARM_FP0_REGNUM + reg;
1400 reg -= NUM_FREGS;
1401
1402 if (reg < NUM_SREGS)
1403 return SIM_ARM_FPS_REGNUM + reg;
1404 reg -= NUM_SREGS;
1405
1406 internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
1407 }
1408
1409 /* NOTE: cagney/2001-08-20: Both convert_from_extended() and
1410 convert_to_extended() use floatformat_arm_ext_littlebyte_bigword.
1411 It is thought that this is is the floating-point register format on
1412 little-endian systems. */
1413
1414 static void
1415 convert_from_extended (const struct floatformat *fmt, const void *ptr,
1416 void *dbl)
1417 {
1418 DOUBLEST d;
1419 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
1420 floatformat_to_doublest (&floatformat_arm_ext_big, ptr, &d);
1421 else
1422 floatformat_to_doublest (&floatformat_arm_ext_littlebyte_bigword,
1423 ptr, &d);
1424 floatformat_from_doublest (fmt, &d, dbl);
1425 }
1426
1427 static void
1428 convert_to_extended (const struct floatformat *fmt, void *dbl, const void *ptr)
1429 {
1430 DOUBLEST d;
1431 floatformat_to_doublest (fmt, ptr, &d);
1432 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
1433 floatformat_from_doublest (&floatformat_arm_ext_big, &d, dbl);
1434 else
1435 floatformat_from_doublest (&floatformat_arm_ext_littlebyte_bigword,
1436 &d, dbl);
1437 }
1438
1439 static int
1440 condition_true (unsigned long cond, unsigned long status_reg)
1441 {
1442 if (cond == INST_AL || cond == INST_NV)
1443 return 1;
1444
1445 switch (cond)
1446 {
1447 case INST_EQ:
1448 return ((status_reg & FLAG_Z) != 0);
1449 case INST_NE:
1450 return ((status_reg & FLAG_Z) == 0);
1451 case INST_CS:
1452 return ((status_reg & FLAG_C) != 0);
1453 case INST_CC:
1454 return ((status_reg & FLAG_C) == 0);
1455 case INST_MI:
1456 return ((status_reg & FLAG_N) != 0);
1457 case INST_PL:
1458 return ((status_reg & FLAG_N) == 0);
1459 case INST_VS:
1460 return ((status_reg & FLAG_V) != 0);
1461 case INST_VC:
1462 return ((status_reg & FLAG_V) == 0);
1463 case INST_HI:
1464 return ((status_reg & (FLAG_C | FLAG_Z)) == FLAG_C);
1465 case INST_LS:
1466 return ((status_reg & (FLAG_C | FLAG_Z)) != FLAG_C);
1467 case INST_GE:
1468 return (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0));
1469 case INST_LT:
1470 return (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0));
1471 case INST_GT:
1472 return (((status_reg & FLAG_Z) == 0) &&
1473 (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0)));
1474 case INST_LE:
1475 return (((status_reg & FLAG_Z) != 0) ||
1476 (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0)));
1477 }
1478 return 1;
1479 }
1480
1481 /* Support routines for single stepping. Calculate the next PC value. */
1482 #define submask(x) ((1L << ((x) + 1)) - 1)
1483 #define bit(obj,st) (((obj) >> (st)) & 1)
1484 #define bits(obj,st,fn) (((obj) >> (st)) & submask ((fn) - (st)))
1485 #define sbits(obj,st,fn) \
1486 ((long) (bits(obj,st,fn) | ((long) bit(obj,fn) * ~ submask (fn - st))))
1487 #define BranchDest(addr,instr) \
1488 ((CORE_ADDR) (((long) (addr)) + 8 + (sbits (instr, 0, 23) << 2)))
1489 #define ARM_PC_32 1
1490
1491 static unsigned long
1492 shifted_reg_val (unsigned long inst, int carry, unsigned long pc_val,
1493 unsigned long status_reg)
1494 {
1495 unsigned long res, shift;
1496 int rm = bits (inst, 0, 3);
1497 unsigned long shifttype = bits (inst, 5, 6);
1498
1499 if (bit (inst, 4))
1500 {
1501 int rs = bits (inst, 8, 11);
1502 shift = (rs == 15 ? pc_val + 8 : read_register (rs)) & 0xFF;
1503 }
1504 else
1505 shift = bits (inst, 7, 11);
1506
1507 res = (rm == 15
1508 ? ((pc_val | (ARM_PC_32 ? 0 : status_reg))
1509 + (bit (inst, 4) ? 12 : 8))
1510 : read_register (rm));
1511
1512 switch (shifttype)
1513 {
1514 case 0: /* LSL */
1515 res = shift >= 32 ? 0 : res << shift;
1516 break;
1517
1518 case 1: /* LSR */
1519 res = shift >= 32 ? 0 : res >> shift;
1520 break;
1521
1522 case 2: /* ASR */
1523 if (shift >= 32)
1524 shift = 31;
1525 res = ((res & 0x80000000L)
1526 ? ~((~res) >> shift) : res >> shift);
1527 break;
1528
1529 case 3: /* ROR/RRX */
1530 shift &= 31;
1531 if (shift == 0)
1532 res = (res >> 1) | (carry ? 0x80000000L : 0);
1533 else
1534 res = (res >> shift) | (res << (32 - shift));
1535 break;
1536 }
1537
1538 return res & 0xffffffff;
1539 }
1540
1541 /* Return number of 1-bits in VAL. */
1542
1543 static int
1544 bitcount (unsigned long val)
1545 {
1546 int nbits;
1547 for (nbits = 0; val != 0; nbits++)
1548 val &= val - 1; /* delete rightmost 1-bit in val */
1549 return nbits;
1550 }
1551
1552 CORE_ADDR
1553 thumb_get_next_pc (CORE_ADDR pc)
1554 {
1555 unsigned long pc_val = ((unsigned long) pc) + 4; /* PC after prefetch */
1556 unsigned short inst1 = read_memory_integer (pc, 2);
1557 CORE_ADDR nextpc = pc + 2; /* default is next instruction */
1558 unsigned long offset;
1559
1560 if ((inst1 & 0xff00) == 0xbd00) /* pop {rlist, pc} */
1561 {
1562 CORE_ADDR sp;
1563
1564 /* Fetch the saved PC from the stack. It's stored above
1565 all of the other registers. */
1566 offset = bitcount (bits (inst1, 0, 7)) * DEPRECATED_REGISTER_SIZE;
1567 sp = read_register (ARM_SP_REGNUM);
1568 nextpc = (CORE_ADDR) read_memory_integer (sp + offset, 4);
1569 nextpc = ADDR_BITS_REMOVE (nextpc);
1570 if (nextpc == pc)
1571 error (_("Infinite loop detected"));
1572 }
1573 else if ((inst1 & 0xf000) == 0xd000) /* conditional branch */
1574 {
1575 unsigned long status = read_register (ARM_PS_REGNUM);
1576 unsigned long cond = bits (inst1, 8, 11);
1577 if (cond != 0x0f && condition_true (cond, status)) /* 0x0f = SWI */
1578 nextpc = pc_val + (sbits (inst1, 0, 7) << 1);
1579 }
1580 else if ((inst1 & 0xf800) == 0xe000) /* unconditional branch */
1581 {
1582 nextpc = pc_val + (sbits (inst1, 0, 10) << 1);
1583 }
1584 else if ((inst1 & 0xf800) == 0xf000) /* long branch with link, and blx */
1585 {
1586 unsigned short inst2 = read_memory_integer (pc + 2, 2);
1587 offset = (sbits (inst1, 0, 10) << 12) + (bits (inst2, 0, 10) << 1);
1588 nextpc = pc_val + offset;
1589 /* For BLX make sure to clear the low bits. */
1590 if (bits (inst2, 11, 12) == 1)
1591 nextpc = nextpc & 0xfffffffc;
1592 }
1593 else if ((inst1 & 0xff00) == 0x4700) /* bx REG, blx REG */
1594 {
1595 if (bits (inst1, 3, 6) == 0x0f)
1596 nextpc = pc_val;
1597 else
1598 nextpc = read_register (bits (inst1, 3, 6));
1599
1600 nextpc = ADDR_BITS_REMOVE (nextpc);
1601 if (nextpc == pc)
1602 error (_("Infinite loop detected"));
1603 }
1604
1605 return nextpc;
1606 }
1607
1608 CORE_ADDR
1609 arm_get_next_pc (CORE_ADDR pc)
1610 {
1611 unsigned long pc_val;
1612 unsigned long this_instr;
1613 unsigned long status;
1614 CORE_ADDR nextpc;
1615
1616 if (arm_pc_is_thumb (pc))
1617 return thumb_get_next_pc (pc);
1618
1619 pc_val = (unsigned long) pc;
1620 this_instr = read_memory_integer (pc, 4);
1621 status = read_register (ARM_PS_REGNUM);
1622 nextpc = (CORE_ADDR) (pc_val + 4); /* Default case */
1623
1624 if (condition_true (bits (this_instr, 28, 31), status))
1625 {
1626 switch (bits (this_instr, 24, 27))
1627 {
1628 case 0x0:
1629 case 0x1: /* data processing */
1630 case 0x2:
1631 case 0x3:
1632 {
1633 unsigned long operand1, operand2, result = 0;
1634 unsigned long rn;
1635 int c;
1636
1637 if (bits (this_instr, 12, 15) != 15)
1638 break;
1639
1640 if (bits (this_instr, 22, 25) == 0
1641 && bits (this_instr, 4, 7) == 9) /* multiply */
1642 error (_("Invalid update to pc in instruction"));
1643
1644 /* BX <reg>, BLX <reg> */
1645 if (bits (this_instr, 4, 28) == 0x12fff1
1646 || bits (this_instr, 4, 28) == 0x12fff3)
1647 {
1648 rn = bits (this_instr, 0, 3);
1649 result = (rn == 15) ? pc_val + 8 : read_register (rn);
1650 nextpc = (CORE_ADDR) ADDR_BITS_REMOVE (result);
1651
1652 if (nextpc == pc)
1653 error (_("Infinite loop detected"));
1654
1655 return nextpc;
1656 }
1657
1658 /* Multiply into PC */
1659 c = (status & FLAG_C) ? 1 : 0;
1660 rn = bits (this_instr, 16, 19);
1661 operand1 = (rn == 15) ? pc_val + 8 : read_register (rn);
1662
1663 if (bit (this_instr, 25))
1664 {
1665 unsigned long immval = bits (this_instr, 0, 7);
1666 unsigned long rotate = 2 * bits (this_instr, 8, 11);
1667 operand2 = ((immval >> rotate) | (immval << (32 - rotate)))
1668 & 0xffffffff;
1669 }
1670 else /* operand 2 is a shifted register */
1671 operand2 = shifted_reg_val (this_instr, c, pc_val, status);
1672
1673 switch (bits (this_instr, 21, 24))
1674 {
1675 case 0x0: /*and */
1676 result = operand1 & operand2;
1677 break;
1678
1679 case 0x1: /*eor */
1680 result = operand1 ^ operand2;
1681 break;
1682
1683 case 0x2: /*sub */
1684 result = operand1 - operand2;
1685 break;
1686
1687 case 0x3: /*rsb */
1688 result = operand2 - operand1;
1689 break;
1690
1691 case 0x4: /*add */
1692 result = operand1 + operand2;
1693 break;
1694
1695 case 0x5: /*adc */
1696 result = operand1 + operand2 + c;
1697 break;
1698
1699 case 0x6: /*sbc */
1700 result = operand1 - operand2 + c;
1701 break;
1702
1703 case 0x7: /*rsc */
1704 result = operand2 - operand1 + c;
1705 break;
1706
1707 case 0x8:
1708 case 0x9:
1709 case 0xa:
1710 case 0xb: /* tst, teq, cmp, cmn */
1711 result = (unsigned long) nextpc;
1712 break;
1713
1714 case 0xc: /*orr */
1715 result = operand1 | operand2;
1716 break;
1717
1718 case 0xd: /*mov */
1719 /* Always step into a function. */
1720 result = operand2;
1721 break;
1722
1723 case 0xe: /*bic */
1724 result = operand1 & ~operand2;
1725 break;
1726
1727 case 0xf: /*mvn */
1728 result = ~operand2;
1729 break;
1730 }
1731 nextpc = (CORE_ADDR) ADDR_BITS_REMOVE (result);
1732
1733 if (nextpc == pc)
1734 error (_("Infinite loop detected"));
1735 break;
1736 }
1737
1738 case 0x4:
1739 case 0x5: /* data transfer */
1740 case 0x6:
1741 case 0x7:
1742 if (bit (this_instr, 20))
1743 {
1744 /* load */
1745 if (bits (this_instr, 12, 15) == 15)
1746 {
1747 /* rd == pc */
1748 unsigned long rn;
1749 unsigned long base;
1750
1751 if (bit (this_instr, 22))
1752 error (_("Invalid update to pc in instruction"));
1753
1754 /* byte write to PC */
1755 rn = bits (this_instr, 16, 19);
1756 base = (rn == 15) ? pc_val + 8 : read_register (rn);
1757 if (bit (this_instr, 24))
1758 {
1759 /* pre-indexed */
1760 int c = (status & FLAG_C) ? 1 : 0;
1761 unsigned long offset =
1762 (bit (this_instr, 25)
1763 ? shifted_reg_val (this_instr, c, pc_val, status)
1764 : bits (this_instr, 0, 11));
1765
1766 if (bit (this_instr, 23))
1767 base += offset;
1768 else
1769 base -= offset;
1770 }
1771 nextpc = (CORE_ADDR) read_memory_integer ((CORE_ADDR) base,
1772 4);
1773
1774 nextpc = ADDR_BITS_REMOVE (nextpc);
1775
1776 if (nextpc == pc)
1777 error (_("Infinite loop detected"));
1778 }
1779 }
1780 break;
1781
1782 case 0x8:
1783 case 0x9: /* block transfer */
1784 if (bit (this_instr, 20))
1785 {
1786 /* LDM */
1787 if (bit (this_instr, 15))
1788 {
1789 /* loading pc */
1790 int offset = 0;
1791
1792 if (bit (this_instr, 23))
1793 {
1794 /* up */
1795 unsigned long reglist = bits (this_instr, 0, 14);
1796 offset = bitcount (reglist) * 4;
1797 if (bit (this_instr, 24)) /* pre */
1798 offset += 4;
1799 }
1800 else if (bit (this_instr, 24))
1801 offset = -4;
1802
1803 {
1804 unsigned long rn_val =
1805 read_register (bits (this_instr, 16, 19));
1806 nextpc =
1807 (CORE_ADDR) read_memory_integer ((CORE_ADDR) (rn_val
1808 + offset),
1809 4);
1810 }
1811 nextpc = ADDR_BITS_REMOVE (nextpc);
1812 if (nextpc == pc)
1813 error (_("Infinite loop detected"));
1814 }
1815 }
1816 break;
1817
1818 case 0xb: /* branch & link */
1819 case 0xa: /* branch */
1820 {
1821 nextpc = BranchDest (pc, this_instr);
1822
1823 /* BLX */
1824 if (bits (this_instr, 28, 31) == INST_NV)
1825 nextpc |= bit (this_instr, 24) << 1;
1826
1827 nextpc = ADDR_BITS_REMOVE (nextpc);
1828 if (nextpc == pc)
1829 error (_("Infinite loop detected"));
1830 break;
1831 }
1832
1833 case 0xc:
1834 case 0xd:
1835 case 0xe: /* coproc ops */
1836 case 0xf: /* SWI */
1837 break;
1838
1839 default:
1840 fprintf_filtered (gdb_stderr, _("Bad bit-field extraction\n"));
1841 return (pc);
1842 }
1843 }
1844
1845 return nextpc;
1846 }
1847
1848 /* single_step() is called just before we want to resume the inferior,
1849 if we want to single-step it but there is no hardware or kernel
1850 single-step support. We find the target of the coming instruction
1851 and breakpoint it.
1852
1853 single_step() is also called just after the inferior stops. If we
1854 had set up a simulated single-step, we undo our damage. */
1855
1856 static void
1857 arm_software_single_step (enum target_signal sig, int insert_bpt)
1858 {
1859 static int next_pc; /* State between setting and unsetting. */
1860 static char break_mem[BREAKPOINT_MAX]; /* Temporary storage for mem@bpt */
1861
1862 if (insert_bpt)
1863 {
1864 next_pc = arm_get_next_pc (read_register (ARM_PC_REGNUM));
1865 target_insert_breakpoint (next_pc, break_mem);
1866 }
1867 else
1868 target_remove_breakpoint (next_pc, break_mem);
1869 }
1870
1871 #include "bfd-in2.h"
1872 #include "libcoff.h"
1873
1874 static int
1875 gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
1876 {
1877 if (arm_pc_is_thumb (memaddr))
1878 {
1879 static asymbol *asym;
1880 static combined_entry_type ce;
1881 static struct coff_symbol_struct csym;
1882 static struct bfd fake_bfd;
1883 static bfd_target fake_target;
1884
1885 if (csym.native == NULL)
1886 {
1887 /* Create a fake symbol vector containing a Thumb symbol.
1888 This is solely so that the code in print_insn_little_arm()
1889 and print_insn_big_arm() in opcodes/arm-dis.c will detect
1890 the presence of a Thumb symbol and switch to decoding
1891 Thumb instructions. */
1892
1893 fake_target.flavour = bfd_target_coff_flavour;
1894 fake_bfd.xvec = &fake_target;
1895 ce.u.syment.n_sclass = C_THUMBEXTFUNC;
1896 csym.native = &ce;
1897 csym.symbol.the_bfd = &fake_bfd;
1898 csym.symbol.name = "fake";
1899 asym = (asymbol *) & csym;
1900 }
1901
1902 memaddr = UNMAKE_THUMB_ADDR (memaddr);
1903 info->symbols = &asym;
1904 }
1905 else
1906 info->symbols = NULL;
1907
1908 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
1909 return print_insn_big_arm (memaddr, info);
1910 else
1911 return print_insn_little_arm (memaddr, info);
1912 }
1913
1914 /* The following define instruction sequences that will cause ARM
1915 cpu's to take an undefined instruction trap. These are used to
1916 signal a breakpoint to GDB.
1917
1918 The newer ARMv4T cpu's are capable of operating in ARM or Thumb
1919 modes. A different instruction is required for each mode. The ARM
1920 cpu's can also be big or little endian. Thus four different
1921 instructions are needed to support all cases.
1922
1923 Note: ARMv4 defines several new instructions that will take the
1924 undefined instruction trap. ARM7TDMI is nominally ARMv4T, but does
1925 not in fact add the new instructions. The new undefined
1926 instructions in ARMv4 are all instructions that had no defined
1927 behaviour in earlier chips. There is no guarantee that they will
1928 raise an exception, but may be treated as NOP's. In practice, it
1929 may only safe to rely on instructions matching:
1930
1931 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
1932 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
1933 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
1934
1935 Even this may only true if the condition predicate is true. The
1936 following use a condition predicate of ALWAYS so it is always TRUE.
1937
1938 There are other ways of forcing a breakpoint. GNU/Linux, RISC iX,
1939 and NetBSD all use a software interrupt rather than an undefined
1940 instruction to force a trap. This can be handled by by the
1941 abi-specific code during establishment of the gdbarch vector. */
1942
1943
1944 /* NOTE rearnsha 2002-02-18: for now we allow a non-multi-arch gdb to
1945 override these definitions. */
1946 #ifndef ARM_LE_BREAKPOINT
1947 #define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
1948 #endif
1949 #ifndef ARM_BE_BREAKPOINT
1950 #define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
1951 #endif
1952 #ifndef THUMB_LE_BREAKPOINT
1953 #define THUMB_LE_BREAKPOINT {0xfe,0xdf}
1954 #endif
1955 #ifndef THUMB_BE_BREAKPOINT
1956 #define THUMB_BE_BREAKPOINT {0xdf,0xfe}
1957 #endif
1958
1959 static const char arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
1960 static const char arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
1961 static const char arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
1962 static const char arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
1963
1964 /* Determine the type and size of breakpoint to insert at PCPTR. Uses
1965 the program counter value to determine whether a 16-bit or 32-bit
1966 breakpoint should be used. It returns a pointer to a string of
1967 bytes that encode a breakpoint instruction, stores the length of
1968 the string to *lenptr, and adjusts the program counter (if
1969 necessary) to point to the actual memory location where the
1970 breakpoint should be inserted. */
1971
1972 /* XXX ??? from old tm-arm.h: if we're using RDP, then we're inserting
1973 breakpoints and storing their handles instread of what was in
1974 memory. It is nice that this is the same size as a handle -
1975 otherwise remote-rdp will have to change. */
1976
1977 static const unsigned char *
1978 arm_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
1979 {
1980 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
1981
1982 if (arm_pc_is_thumb (*pcptr))
1983 {
1984 *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
1985 *lenptr = tdep->thumb_breakpoint_size;
1986 return tdep->thumb_breakpoint;
1987 }
1988 else
1989 {
1990 *lenptr = tdep->arm_breakpoint_size;
1991 return tdep->arm_breakpoint;
1992 }
1993 }
1994
1995 /* Extract from an array REGBUF containing the (raw) register state a
1996 function return value of type TYPE, and copy that, in virtual
1997 format, into VALBUF. */
1998
1999 static void
2000 arm_extract_return_value (struct type *type,
2001 struct regcache *regs,
2002 void *dst)
2003 {
2004 bfd_byte *valbuf = dst;
2005
2006 if (TYPE_CODE_FLT == TYPE_CODE (type))
2007 {
2008 switch (arm_get_fp_model (current_gdbarch))
2009 {
2010 case ARM_FLOAT_FPA:
2011 {
2012 /* The value is in register F0 in internal format. We need to
2013 extract the raw value and then convert it to the desired
2014 internal type. */
2015 bfd_byte tmpbuf[FP_REGISTER_SIZE];
2016
2017 regcache_cooked_read (regs, ARM_F0_REGNUM, tmpbuf);
2018 convert_from_extended (floatformat_from_type (type), tmpbuf,
2019 valbuf);
2020 }
2021 break;
2022
2023 case ARM_FLOAT_SOFT_FPA:
2024 case ARM_FLOAT_SOFT_VFP:
2025 regcache_cooked_read (regs, ARM_A1_REGNUM, valbuf);
2026 if (TYPE_LENGTH (type) > 4)
2027 regcache_cooked_read (regs, ARM_A1_REGNUM + 1,
2028 valbuf + INT_REGISTER_SIZE);
2029 break;
2030
2031 default:
2032 internal_error
2033 (__FILE__, __LINE__,
2034 _("arm_extract_return_value: Floating point model not supported"));
2035 break;
2036 }
2037 }
2038 else if (TYPE_CODE (type) == TYPE_CODE_INT
2039 || TYPE_CODE (type) == TYPE_CODE_CHAR
2040 || TYPE_CODE (type) == TYPE_CODE_BOOL
2041 || TYPE_CODE (type) == TYPE_CODE_PTR
2042 || TYPE_CODE (type) == TYPE_CODE_REF
2043 || TYPE_CODE (type) == TYPE_CODE_ENUM)
2044 {
2045 /* If the the type is a plain integer, then the access is
2046 straight-forward. Otherwise we have to play around a bit more. */
2047 int len = TYPE_LENGTH (type);
2048 int regno = ARM_A1_REGNUM;
2049 ULONGEST tmp;
2050
2051 while (len > 0)
2052 {
2053 /* By using store_unsigned_integer we avoid having to do
2054 anything special for small big-endian values. */
2055 regcache_cooked_read_unsigned (regs, regno++, &tmp);
2056 store_unsigned_integer (valbuf,
2057 (len > INT_REGISTER_SIZE
2058 ? INT_REGISTER_SIZE : len),
2059 tmp);
2060 len -= INT_REGISTER_SIZE;
2061 valbuf += INT_REGISTER_SIZE;
2062 }
2063 }
2064 else
2065 {
2066 /* For a structure or union the behaviour is as if the value had
2067 been stored to word-aligned memory and then loaded into
2068 registers with 32-bit load instruction(s). */
2069 int len = TYPE_LENGTH (type);
2070 int regno = ARM_A1_REGNUM;
2071 bfd_byte tmpbuf[INT_REGISTER_SIZE];
2072
2073 while (len > 0)
2074 {
2075 regcache_cooked_read (regs, regno++, tmpbuf);
2076 memcpy (valbuf, tmpbuf,
2077 len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
2078 len -= INT_REGISTER_SIZE;
2079 valbuf += INT_REGISTER_SIZE;
2080 }
2081 }
2082 }
2083
2084 /* Extract from an array REGBUF containing the (raw) register state
2085 the address in which a function should return its structure value. */
2086
2087 static CORE_ADDR
2088 arm_extract_struct_value_address (struct regcache *regcache)
2089 {
2090 ULONGEST ret;
2091
2092 regcache_cooked_read_unsigned (regcache, ARM_A1_REGNUM, &ret);
2093 return ret;
2094 }
2095
2096 /* Will a function return an aggregate type in memory or in a
2097 register? Return 0 if an aggregate type can be returned in a
2098 register, 1 if it must be returned in memory. */
2099
2100 static int
2101 arm_use_struct_convention (int gcc_p, struct type *type)
2102 {
2103 int nRc;
2104 enum type_code code;
2105
2106 CHECK_TYPEDEF (type);
2107
2108 /* In the ARM ABI, "integer" like aggregate types are returned in
2109 registers. For an aggregate type to be integer like, its size
2110 must be less than or equal to DEPRECATED_REGISTER_SIZE and the
2111 offset of each addressable subfield must be zero. Note that bit
2112 fields are not addressable, and all addressable subfields of
2113 unions always start at offset zero.
2114
2115 This function is based on the behaviour of GCC 2.95.1.
2116 See: gcc/arm.c: arm_return_in_memory() for details.
2117
2118 Note: All versions of GCC before GCC 2.95.2 do not set up the
2119 parameters correctly for a function returning the following
2120 structure: struct { float f;}; This should be returned in memory,
2121 not a register. Richard Earnshaw sent me a patch, but I do not
2122 know of any way to detect if a function like the above has been
2123 compiled with the correct calling convention. */
2124
2125 /* All aggregate types that won't fit in a register must be returned
2126 in memory. */
2127 if (TYPE_LENGTH (type) > DEPRECATED_REGISTER_SIZE)
2128 {
2129 return 1;
2130 }
2131
2132 /* The only aggregate types that can be returned in a register are
2133 structs and unions. Arrays must be returned in memory. */
2134 code = TYPE_CODE (type);
2135 if ((TYPE_CODE_STRUCT != code) && (TYPE_CODE_UNION != code))
2136 {
2137 return 1;
2138 }
2139
2140 /* Assume all other aggregate types can be returned in a register.
2141 Run a check for structures, unions and arrays. */
2142 nRc = 0;
2143
2144 if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
2145 {
2146 int i;
2147 /* Need to check if this struct/union is "integer" like. For
2148 this to be true, its size must be less than or equal to
2149 DEPRECATED_REGISTER_SIZE and the offset of each addressable
2150 subfield must be zero. Note that bit fields are not
2151 addressable, and unions always start at offset zero. If any
2152 of the subfields is a floating point type, the struct/union
2153 cannot be an integer type. */
2154
2155 /* For each field in the object, check:
2156 1) Is it FP? --> yes, nRc = 1;
2157 2) Is it addressable (bitpos != 0) and
2158 not packed (bitsize == 0)?
2159 --> yes, nRc = 1
2160 */
2161
2162 for (i = 0; i < TYPE_NFIELDS (type); i++)
2163 {
2164 enum type_code field_type_code;
2165 field_type_code = TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type, i)));
2166
2167 /* Is it a floating point type field? */
2168 if (field_type_code == TYPE_CODE_FLT)
2169 {
2170 nRc = 1;
2171 break;
2172 }
2173
2174 /* If bitpos != 0, then we have to care about it. */
2175 if (TYPE_FIELD_BITPOS (type, i) != 0)
2176 {
2177 /* Bitfields are not addressable. If the field bitsize is
2178 zero, then the field is not packed. Hence it cannot be
2179 a bitfield or any other packed type. */
2180 if (TYPE_FIELD_BITSIZE (type, i) == 0)
2181 {
2182 nRc = 1;
2183 break;
2184 }
2185 }
2186 }
2187 }
2188
2189 return nRc;
2190 }
2191
2192 /* Write into appropriate registers a function return value of type
2193 TYPE, given in virtual format. */
2194
2195 static void
2196 arm_store_return_value (struct type *type, struct regcache *regs,
2197 const void *src)
2198 {
2199 const bfd_byte *valbuf = src;
2200
2201 if (TYPE_CODE (type) == TYPE_CODE_FLT)
2202 {
2203 char buf[MAX_REGISTER_SIZE];
2204
2205 switch (arm_get_fp_model (current_gdbarch))
2206 {
2207 case ARM_FLOAT_FPA:
2208
2209 convert_to_extended (floatformat_from_type (type), buf, valbuf);
2210 regcache_cooked_write (regs, ARM_F0_REGNUM, buf);
2211 break;
2212
2213 case ARM_FLOAT_SOFT_FPA:
2214 case ARM_FLOAT_SOFT_VFP:
2215 regcache_cooked_write (regs, ARM_A1_REGNUM, valbuf);
2216 if (TYPE_LENGTH (type) > 4)
2217 regcache_cooked_write (regs, ARM_A1_REGNUM + 1,
2218 valbuf + INT_REGISTER_SIZE);
2219 break;
2220
2221 default:
2222 internal_error
2223 (__FILE__, __LINE__,
2224 _("arm_store_return_value: Floating point model not supported"));
2225 break;
2226 }
2227 }
2228 else if (TYPE_CODE (type) == TYPE_CODE_INT
2229 || TYPE_CODE (type) == TYPE_CODE_CHAR
2230 || TYPE_CODE (type) == TYPE_CODE_BOOL
2231 || TYPE_CODE (type) == TYPE_CODE_PTR
2232 || TYPE_CODE (type) == TYPE_CODE_REF
2233 || TYPE_CODE (type) == TYPE_CODE_ENUM)
2234 {
2235 if (TYPE_LENGTH (type) <= 4)
2236 {
2237 /* Values of one word or less are zero/sign-extended and
2238 returned in r0. */
2239 bfd_byte tmpbuf[INT_REGISTER_SIZE];
2240 LONGEST val = unpack_long (type, valbuf);
2241
2242 store_signed_integer (tmpbuf, INT_REGISTER_SIZE, val);
2243 regcache_cooked_write (regs, ARM_A1_REGNUM, tmpbuf);
2244 }
2245 else
2246 {
2247 /* Integral values greater than one word are stored in consecutive
2248 registers starting with r0. This will always be a multiple of
2249 the regiser size. */
2250 int len = TYPE_LENGTH (type);
2251 int regno = ARM_A1_REGNUM;
2252
2253 while (len > 0)
2254 {
2255 regcache_cooked_write (regs, regno++, valbuf);
2256 len -= INT_REGISTER_SIZE;
2257 valbuf += INT_REGISTER_SIZE;
2258 }
2259 }
2260 }
2261 else
2262 {
2263 /* For a structure or union the behaviour is as if the value had
2264 been stored to word-aligned memory and then loaded into
2265 registers with 32-bit load instruction(s). */
2266 int len = TYPE_LENGTH (type);
2267 int regno = ARM_A1_REGNUM;
2268 bfd_byte tmpbuf[INT_REGISTER_SIZE];
2269
2270 while (len > 0)
2271 {
2272 memcpy (tmpbuf, valbuf,
2273 len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
2274 regcache_cooked_write (regs, regno++, tmpbuf);
2275 len -= INT_REGISTER_SIZE;
2276 valbuf += INT_REGISTER_SIZE;
2277 }
2278 }
2279 }
2280
2281 static int
2282 arm_get_longjmp_target (CORE_ADDR *pc)
2283 {
2284 CORE_ADDR jb_addr;
2285 char buf[INT_REGISTER_SIZE];
2286 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
2287
2288 jb_addr = read_register (ARM_A1_REGNUM);
2289
2290 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
2291 INT_REGISTER_SIZE))
2292 return 0;
2293
2294 *pc = extract_unsigned_integer (buf, INT_REGISTER_SIZE);
2295 return 1;
2296 }
2297
2298 /* Return non-zero if the PC is inside a thumb call thunk. */
2299
2300 int
2301 arm_in_call_stub (CORE_ADDR pc, char *name)
2302 {
2303 CORE_ADDR start_addr;
2304
2305 /* Find the starting address of the function containing the PC. If
2306 the caller didn't give us a name, look it up at the same time. */
2307 if (0 == find_pc_partial_function (pc, name ? NULL : &name,
2308 &start_addr, NULL))
2309 return 0;
2310
2311 return strncmp (name, "_call_via_r", 11) == 0;
2312 }
2313
2314 /* If PC is in a Thumb call or return stub, return the address of the
2315 target PC, which is in a register. The thunk functions are called
2316 _called_via_xx, where x is the register name. The possible names
2317 are r0-r9, sl, fp, ip, sp, and lr. */
2318
2319 CORE_ADDR
2320 arm_skip_stub (CORE_ADDR pc)
2321 {
2322 char *name;
2323 CORE_ADDR start_addr;
2324
2325 /* Find the starting address and name of the function containing the PC. */
2326 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
2327 return 0;
2328
2329 /* Call thunks always start with "_call_via_". */
2330 if (strncmp (name, "_call_via_", 10) == 0)
2331 {
2332 /* Use the name suffix to determine which register contains the
2333 target PC. */
2334 static char *table[15] =
2335 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
2336 "r8", "r9", "sl", "fp", "ip", "sp", "lr"
2337 };
2338 int regno;
2339
2340 for (regno = 0; regno <= 14; regno++)
2341 if (strcmp (&name[10], table[regno]) == 0)
2342 return read_register (regno);
2343 }
2344
2345 return 0; /* not a stub */
2346 }
2347
2348 static void
2349 set_arm_command (char *args, int from_tty)
2350 {
2351 printf_unfiltered (_("\
2352 \"set arm\" must be followed by an apporpriate subcommand.\n"));
2353 help_list (setarmcmdlist, "set arm ", all_commands, gdb_stdout);
2354 }
2355
2356 static void
2357 show_arm_command (char *args, int from_tty)
2358 {
2359 cmd_show_list (showarmcmdlist, from_tty, "");
2360 }
2361
2362 enum arm_float_model
2363 arm_get_fp_model (struct gdbarch *gdbarch)
2364 {
2365 if (arm_fp_model == ARM_FLOAT_AUTO)
2366 return gdbarch_tdep (gdbarch)->fp_model;
2367
2368 return arm_fp_model;
2369 }
2370
2371 static void
2372 arm_set_fp (struct gdbarch *gdbarch)
2373 {
2374 enum arm_float_model fp_model = arm_get_fp_model (gdbarch);
2375
2376 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE
2377 && (fp_model == ARM_FLOAT_SOFT_FPA || fp_model == ARM_FLOAT_FPA))
2378 {
2379 set_gdbarch_double_format (gdbarch,
2380 &floatformat_ieee_double_littlebyte_bigword);
2381 set_gdbarch_long_double_format
2382 (gdbarch, &floatformat_ieee_double_littlebyte_bigword);
2383 }
2384 else
2385 {
2386 set_gdbarch_double_format (gdbarch, &floatformat_ieee_double_little);
2387 set_gdbarch_long_double_format (gdbarch,
2388 &floatformat_ieee_double_little);
2389 }
2390 }
2391
2392 static void
2393 set_fp_model_sfunc (char *args, int from_tty,
2394 struct cmd_list_element *c)
2395 {
2396 enum arm_float_model fp_model;
2397
2398 for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
2399 if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
2400 {
2401 arm_fp_model = fp_model;
2402 break;
2403 }
2404
2405 if (fp_model == ARM_FLOAT_LAST)
2406 internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
2407 current_fp_model);
2408
2409 if (gdbarch_bfd_arch_info (current_gdbarch)->arch == bfd_arch_arm)
2410 arm_set_fp (current_gdbarch);
2411 }
2412
2413 static void
2414 show_fp_model (struct ui_file *file, int from_tty,
2415 struct cmd_list_element *c, const char *value)
2416 {
2417 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
2418
2419 deprecated_show_value_hack (file, from_tty, c, value);
2420 if (arm_fp_model == ARM_FLOAT_AUTO
2421 && gdbarch_bfd_arch_info (current_gdbarch)->arch == bfd_arch_arm)
2422 /* i18n: "the default [floating point model] for the current ABI..." */
2423 printf_filtered (_(" - the default for the current ABI is \"%s\".\n"),
2424 fp_model_strings[tdep->fp_model]);
2425 }
2426
2427 /* If the user changes the register disassembly style used for info
2428 register and other commands, we have to also switch the style used
2429 in opcodes for disassembly output. This function is run in the "set
2430 arm disassembly" command, and does that. */
2431
2432 static void
2433 set_disassembly_style_sfunc (char *args, int from_tty,
2434 struct cmd_list_element *c)
2435 {
2436 set_disassembly_style ();
2437 }
2438 \f
2439 /* Return the ARM register name corresponding to register I. */
2440 static const char *
2441 arm_register_name (int i)
2442 {
2443 return arm_register_names[i];
2444 }
2445
2446 static void
2447 set_disassembly_style (void)
2448 {
2449 const char *setname, *setdesc, **regnames;
2450 int numregs, j;
2451
2452 /* Find the style that the user wants in the opcodes table. */
2453 int current = 0;
2454 numregs = get_arm_regnames (current, &setname, &setdesc, &regnames);
2455 while ((disassembly_style != setname)
2456 && (current < num_disassembly_options))
2457 get_arm_regnames (++current, &setname, &setdesc, &regnames);
2458 current_option = current;
2459
2460 /* Fill our copy. */
2461 for (j = 0; j < numregs; j++)
2462 arm_register_names[j] = (char *) regnames[j];
2463
2464 /* Adjust case. */
2465 if (isupper (*regnames[ARM_PC_REGNUM]))
2466 {
2467 arm_register_names[ARM_FPS_REGNUM] = "FPS";
2468 arm_register_names[ARM_PS_REGNUM] = "CPSR";
2469 }
2470 else
2471 {
2472 arm_register_names[ARM_FPS_REGNUM] = "fps";
2473 arm_register_names[ARM_PS_REGNUM] = "cpsr";
2474 }
2475
2476 /* Synchronize the disassembler. */
2477 set_arm_regname_option (current);
2478 }
2479
2480 /* Test whether the coff symbol specific value corresponds to a Thumb
2481 function. */
2482
2483 static int
2484 coff_sym_is_thumb (int val)
2485 {
2486 return (val == C_THUMBEXT ||
2487 val == C_THUMBSTAT ||
2488 val == C_THUMBEXTFUNC ||
2489 val == C_THUMBSTATFUNC ||
2490 val == C_THUMBLABEL);
2491 }
2492
2493 /* arm_coff_make_msymbol_special()
2494 arm_elf_make_msymbol_special()
2495
2496 These functions test whether the COFF or ELF symbol corresponds to
2497 an address in thumb code, and set a "special" bit in a minimal
2498 symbol to indicate that it does. */
2499
2500 static void
2501 arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
2502 {
2503 /* Thumb symbols are of type STT_LOPROC, (synonymous with
2504 STT_ARM_TFUNC). */
2505 if (ELF_ST_TYPE (((elf_symbol_type *)sym)->internal_elf_sym.st_info)
2506 == STT_LOPROC)
2507 MSYMBOL_SET_SPECIAL (msym);
2508 }
2509
2510 static void
2511 arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
2512 {
2513 if (coff_sym_is_thumb (val))
2514 MSYMBOL_SET_SPECIAL (msym);
2515 }
2516
2517 static void
2518 arm_write_pc (CORE_ADDR pc, ptid_t ptid)
2519 {
2520 write_register_pid (ARM_PC_REGNUM, pc, ptid);
2521
2522 /* If necessary, set the T bit. */
2523 if (arm_apcs_32)
2524 {
2525 CORE_ADDR val = read_register_pid (ARM_PS_REGNUM, ptid);
2526 if (arm_pc_is_thumb (pc))
2527 write_register_pid (ARM_PS_REGNUM, val | 0x20, ptid);
2528 else
2529 write_register_pid (ARM_PS_REGNUM, val & ~(CORE_ADDR) 0x20, ptid);
2530 }
2531 }
2532 \f
2533 static enum gdb_osabi
2534 arm_elf_osabi_sniffer (bfd *abfd)
2535 {
2536 unsigned int elfosabi, eflags;
2537 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
2538
2539 elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
2540
2541 switch (elfosabi)
2542 {
2543 case ELFOSABI_NONE:
2544 /* When elfosabi is ELFOSABI_NONE (0), then the ELF structures in the
2545 file are conforming to the base specification for that machine
2546 (there are no OS-specific extensions). In order to determine the
2547 real OS in use we must look for OS notes that have been added. */
2548 bfd_map_over_sections (abfd,
2549 generic_elf_osabi_sniff_abi_tag_sections,
2550 &osabi);
2551 if (osabi == GDB_OSABI_UNKNOWN)
2552 {
2553 /* Existing ARM tools don't set this field, so look at the EI_FLAGS
2554 field for more information. */
2555 eflags = EF_ARM_EABI_VERSION(elf_elfheader(abfd)->e_flags);
2556 switch (eflags)
2557 {
2558 case EF_ARM_EABI_VER1:
2559 osabi = GDB_OSABI_ARM_EABI_V1;
2560 break;
2561
2562 case EF_ARM_EABI_VER2:
2563 osabi = GDB_OSABI_ARM_EABI_V2;
2564 break;
2565
2566 case EF_ARM_EABI_UNKNOWN:
2567 /* Assume GNU tools. */
2568 osabi = GDB_OSABI_ARM_APCS;
2569 break;
2570
2571 default:
2572 internal_error (__FILE__, __LINE__,
2573 _("\
2574 arm_elf_osabi_sniffer: Unknown ARM EABI version 0x%x"),
2575 eflags);
2576 }
2577 }
2578 break;
2579
2580 case ELFOSABI_ARM:
2581 /* GNU tools use this value. Check note sections in this case,
2582 as well. */
2583 bfd_map_over_sections (abfd,
2584 generic_elf_osabi_sniff_abi_tag_sections,
2585 &osabi);
2586 if (osabi == GDB_OSABI_UNKNOWN)
2587 {
2588 /* Assume APCS ABI. */
2589 osabi = GDB_OSABI_ARM_APCS;
2590 }
2591 break;
2592
2593 case ELFOSABI_FREEBSD:
2594 osabi = GDB_OSABI_FREEBSD_ELF;
2595 break;
2596
2597 case ELFOSABI_NETBSD:
2598 osabi = GDB_OSABI_NETBSD_ELF;
2599 break;
2600
2601 case ELFOSABI_LINUX:
2602 osabi = GDB_OSABI_LINUX;
2603 break;
2604 }
2605
2606 return osabi;
2607 }
2608
2609 \f
2610 /* Initialize the current architecture based on INFO. If possible,
2611 re-use an architecture from ARCHES, which is a list of
2612 architectures already created during this debugging session.
2613
2614 Called e.g. at program startup, when reading a core file, and when
2615 reading a binary file. */
2616
2617 static struct gdbarch *
2618 arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2619 {
2620 struct gdbarch_tdep *tdep;
2621 struct gdbarch *gdbarch;
2622
2623 /* Try to deterimine the ABI of the object we are loading. */
2624
2625 if (info.abfd != NULL && info.osabi == GDB_OSABI_UNKNOWN)
2626 {
2627 switch (bfd_get_flavour (info.abfd))
2628 {
2629 case bfd_target_aout_flavour:
2630 /* Assume it's an old APCS-style ABI. */
2631 info.osabi = GDB_OSABI_ARM_APCS;
2632 break;
2633
2634 case bfd_target_coff_flavour:
2635 /* Assume it's an old APCS-style ABI. */
2636 /* XXX WinCE? */
2637 info.osabi = GDB_OSABI_ARM_APCS;
2638 break;
2639
2640 default:
2641 /* Leave it as "unknown". */
2642 break;
2643 }
2644 }
2645
2646 /* If there is already a candidate, use it. */
2647 arches = gdbarch_list_lookup_by_info (arches, &info);
2648 if (arches != NULL)
2649 return arches->gdbarch;
2650
2651 tdep = xmalloc (sizeof (struct gdbarch_tdep));
2652 gdbarch = gdbarch_alloc (&info, tdep);
2653
2654 /* We used to default to FPA for generic ARM, but almost nobody uses that
2655 now, and we now provide a way for the user to force the model. So
2656 default to the most useful variant. */
2657 tdep->fp_model = ARM_FLOAT_SOFT_FPA;
2658
2659 /* Breakpoints. */
2660 switch (info.byte_order)
2661 {
2662 case BFD_ENDIAN_BIG:
2663 tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
2664 tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
2665 tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
2666 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
2667
2668 break;
2669
2670 case BFD_ENDIAN_LITTLE:
2671 tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
2672 tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
2673 tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
2674 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
2675
2676 break;
2677
2678 default:
2679 internal_error (__FILE__, __LINE__,
2680 _("arm_gdbarch_init: bad byte order for float format"));
2681 }
2682
2683 /* On ARM targets char defaults to unsigned. */
2684 set_gdbarch_char_signed (gdbarch, 0);
2685
2686 /* This should be low enough for everything. */
2687 tdep->lowest_pc = 0x20;
2688 tdep->jb_pc = -1; /* Longjump support not enabled by default. */
2689
2690 set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
2691
2692 set_gdbarch_write_pc (gdbarch, arm_write_pc);
2693
2694 /* Frame handling. */
2695 set_gdbarch_unwind_dummy_id (gdbarch, arm_unwind_dummy_id);
2696 set_gdbarch_unwind_pc (gdbarch, arm_unwind_pc);
2697 set_gdbarch_unwind_sp (gdbarch, arm_unwind_sp);
2698
2699 frame_base_set_default (gdbarch, &arm_normal_base);
2700
2701 /* Address manipulation. */
2702 set_gdbarch_smash_text_address (gdbarch, arm_smash_text_address);
2703 set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
2704
2705 /* Advance PC across function entry code. */
2706 set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
2707
2708 /* Get the PC when a frame might not be available. */
2709 set_gdbarch_deprecated_saved_pc_after_call (gdbarch, arm_saved_pc_after_call);
2710
2711 /* The stack grows downward. */
2712 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2713
2714 /* Breakpoint manipulation. */
2715 set_gdbarch_breakpoint_from_pc (gdbarch, arm_breakpoint_from_pc);
2716
2717 /* Information about registers, etc. */
2718 set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
2719 set_gdbarch_deprecated_fp_regnum (gdbarch, ARM_FP_REGNUM); /* ??? */
2720 set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
2721 set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
2722 set_gdbarch_deprecated_register_byte (gdbarch, arm_register_byte);
2723 set_gdbarch_num_regs (gdbarch, NUM_GREGS + NUM_FREGS + NUM_SREGS);
2724 set_gdbarch_register_type (gdbarch, arm_register_type);
2725
2726 /* Internal <-> external register number maps. */
2727 set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
2728
2729 /* Integer registers are 4 bytes. */
2730 set_gdbarch_deprecated_register_size (gdbarch, 4);
2731 set_gdbarch_register_name (gdbarch, arm_register_name);
2732
2733 /* Returning results. */
2734 set_gdbarch_extract_return_value (gdbarch, arm_extract_return_value);
2735 set_gdbarch_store_return_value (gdbarch, arm_store_return_value);
2736 set_gdbarch_deprecated_use_struct_convention (gdbarch, arm_use_struct_convention);
2737 set_gdbarch_deprecated_extract_struct_value_address (gdbarch, arm_extract_struct_value_address);
2738
2739 /* Single stepping. */
2740 /* XXX For an RDI target we should ask the target if it can single-step. */
2741 set_gdbarch_software_single_step (gdbarch, arm_software_single_step);
2742
2743 /* Disassembly. */
2744 set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
2745
2746 /* Minsymbol frobbing. */
2747 set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
2748 set_gdbarch_coff_make_msymbol_special (gdbarch,
2749 arm_coff_make_msymbol_special);
2750
2751 /* Hook in the ABI-specific overrides, if they have been registered. */
2752 gdbarch_init_osabi (info, gdbarch);
2753
2754 /* Add some default predicates. */
2755 frame_unwind_append_sniffer (gdbarch, arm_stub_unwind_sniffer);
2756 frame_unwind_append_sniffer (gdbarch, arm_sigtramp_unwind_sniffer);
2757 frame_unwind_append_sniffer (gdbarch, arm_prologue_unwind_sniffer);
2758
2759 /* Now we have tuned the configuration, set a few final things,
2760 based on what the OS ABI has told us. */
2761
2762 if (tdep->jb_pc >= 0)
2763 set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
2764
2765 /* Floating point sizes and format. */
2766 switch (info.byte_order)
2767 {
2768 case BFD_ENDIAN_BIG:
2769 set_gdbarch_float_format (gdbarch, &floatformat_ieee_single_big);
2770 set_gdbarch_double_format (gdbarch, &floatformat_ieee_double_big);
2771 set_gdbarch_long_double_format (gdbarch, &floatformat_ieee_double_big);
2772
2773 break;
2774
2775 case BFD_ENDIAN_LITTLE:
2776 set_gdbarch_float_format (gdbarch, &floatformat_ieee_single_little);
2777 arm_set_fp (gdbarch);
2778 break;
2779
2780 default:
2781 internal_error (__FILE__, __LINE__,
2782 _("arm_gdbarch_init: bad byte order for float format"));
2783 }
2784
2785 return gdbarch;
2786 }
2787
2788 static void
2789 arm_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file)
2790 {
2791 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
2792
2793 if (tdep == NULL)
2794 return;
2795
2796 fprintf_unfiltered (file, _("arm_dump_tdep: Lowest pc = 0x%lx"),
2797 (unsigned long) tdep->lowest_pc);
2798 }
2799
2800 static void
2801 arm_init_abi_eabi_v1 (struct gdbarch_info info,
2802 struct gdbarch *gdbarch)
2803 {
2804 /* Place-holder. */
2805 }
2806
2807 static void
2808 arm_init_abi_eabi_v2 (struct gdbarch_info info,
2809 struct gdbarch *gdbarch)
2810 {
2811 /* Place-holder. */
2812 }
2813
2814 static void
2815 arm_init_abi_apcs (struct gdbarch_info info,
2816 struct gdbarch *gdbarch)
2817 {
2818 /* Place-holder. */
2819 }
2820
2821 extern initialize_file_ftype _initialize_arm_tdep; /* -Wmissing-prototypes */
2822
2823 void
2824 _initialize_arm_tdep (void)
2825 {
2826 struct ui_file *stb;
2827 long length;
2828 struct cmd_list_element *new_set, *new_show;
2829 const char *setname;
2830 const char *setdesc;
2831 const char **regnames;
2832 int numregs, i, j;
2833 static char *helptext;
2834 char regdesc[1024], *rdptr = regdesc;
2835 size_t rest = sizeof (regdesc);
2836
2837 gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
2838
2839 /* Register an ELF OS ABI sniffer for ARM binaries. */
2840 gdbarch_register_osabi_sniffer (bfd_arch_arm,
2841 bfd_target_elf_flavour,
2842 arm_elf_osabi_sniffer);
2843
2844 /* Register some ABI variants for embedded systems. */
2845 gdbarch_register_osabi (bfd_arch_arm, 0, GDB_OSABI_ARM_EABI_V1,
2846 arm_init_abi_eabi_v1);
2847 gdbarch_register_osabi (bfd_arch_arm, 0, GDB_OSABI_ARM_EABI_V2,
2848 arm_init_abi_eabi_v2);
2849 gdbarch_register_osabi (bfd_arch_arm, 0, GDB_OSABI_ARM_APCS,
2850 arm_init_abi_apcs);
2851
2852 /* Get the number of possible sets of register names defined in opcodes. */
2853 num_disassembly_options = get_arm_regname_num_options ();
2854
2855 /* Add root prefix command for all "set arm"/"show arm" commands. */
2856 add_prefix_cmd ("arm", no_class, set_arm_command,
2857 _("Various ARM-specific commands."),
2858 &setarmcmdlist, "set arm ", 0, &setlist);
2859
2860 add_prefix_cmd ("arm", no_class, show_arm_command,
2861 _("Various ARM-specific commands."),
2862 &showarmcmdlist, "show arm ", 0, &showlist);
2863
2864 /* Sync the opcode insn printer with our register viewer. */
2865 parse_arm_disassembler_option ("reg-names-std");
2866
2867 /* Initialize the array that will be passed to
2868 add_setshow_enum_cmd(). */
2869 valid_disassembly_styles
2870 = xmalloc ((num_disassembly_options + 1) * sizeof (char *));
2871 for (i = 0; i < num_disassembly_options; i++)
2872 {
2873 numregs = get_arm_regnames (i, &setname, &setdesc, &regnames);
2874 valid_disassembly_styles[i] = setname;
2875 length = snprintf (rdptr, rest, "%s - %s\n", setname, setdesc);
2876 rdptr += length;
2877 rest -= length;
2878 /* Copy the default names (if found) and synchronize disassembler. */
2879 if (!strcmp (setname, "std"))
2880 {
2881 disassembly_style = setname;
2882 current_option = i;
2883 for (j = 0; j < numregs; j++)
2884 arm_register_names[j] = (char *) regnames[j];
2885 set_arm_regname_option (i);
2886 }
2887 }
2888 /* Mark the end of valid options. */
2889 valid_disassembly_styles[num_disassembly_options] = NULL;
2890
2891 /* Create the help text. */
2892 stb = mem_fileopen ();
2893 fprintf_unfiltered (stb, "%s%s%s",
2894 _("The valid values are:\n"),
2895 regdesc,
2896 _("The default is \"std\"."));
2897 helptext = ui_file_xstrdup (stb, &length);
2898 ui_file_delete (stb);
2899
2900 add_setshow_enum_cmd("disassembler", no_class,
2901 valid_disassembly_styles, &disassembly_style,
2902 _("Set the disassembly style."),
2903 _("Show the disassembly style."),
2904 helptext,
2905 set_disassembly_style_sfunc,
2906 NULL, /* FIXME: i18n: The disassembly style is \"%s\". */
2907 &setarmcmdlist, &showarmcmdlist);
2908
2909 add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
2910 _("Set usage of ARM 32-bit mode."),
2911 _("Show usage of ARM 32-bit mode."),
2912 _("When off, a 26-bit PC will be used."),
2913 NULL,
2914 NULL, /* FIXME: i18n: Usage of ARM 32-bit mode is %s. */
2915 &setarmcmdlist, &showarmcmdlist);
2916
2917 /* Add a command to allow the user to force the FPU model. */
2918 add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
2919 _("Set the floating point type."),
2920 _("Show the floating point type."),
2921 _("auto - Determine the FP typefrom the OS-ABI.\n\
2922 softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
2923 fpa - FPA co-processor (GCC compiled).\n\
2924 softvfp - Software FP with pure-endian doubles.\n\
2925 vfp - VFP co-processor."),
2926 set_fp_model_sfunc, show_fp_model,
2927 &setarmcmdlist, &showarmcmdlist);
2928
2929 /* Debugging flag. */
2930 add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
2931 _("Set ARM debugging."),
2932 _("Show ARM debugging."),
2933 _("When on, arm-specific debugging is enabled."),
2934 NULL,
2935 NULL, /* FIXME: i18n: "ARM debugging is %s. */
2936 &setdebuglist, &showdebuglist);
2937 }