1 /* Dynamic architecture support for GDB, the GNU debugger.
3 Copyright 1998, 1999, 2000, 2001, 2002 Free Software Foundation,
6 This file is part of GDB.
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.
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.
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. */
26 #include "arch-utils.h"
28 #include "inferior.h" /* enum CALL_DUMMY_LOCATION et.al. */
30 /* Just include everything in sight so that the every old definition
31 of macro is visible. */
32 #include "gdb_string.h"
36 #include "breakpoint.h"
44 #include "gdb_assert.h"
48 #include "floatformat.h"
50 /* Use the program counter to determine the contents and size
51 of a breakpoint instruction. If no target-dependent macro
52 BREAKPOINT_FROM_PC has been defined to implement this function,
53 assume that the breakpoint doesn't depend on the PC, and
54 use the values of the BIG_BREAKPOINT and LITTLE_BREAKPOINT macros.
55 Return a pointer to a string of bytes that encode a breakpoint
56 instruction, stores the length of the string to *lenptr,
57 and optionally adjust the pc to point to the correct memory location
58 for inserting the breakpoint. */
61 legacy_breakpoint_from_pc (CORE_ADDR
* pcptr
, int *lenptr
)
63 /* {BIG_,LITTLE_}BREAKPOINT is the sequence of bytes we insert for a
64 breakpoint. On some machines, breakpoints are handled by the
65 target environment and we don't have to worry about them here. */
67 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
69 static unsigned char big_break_insn
[] = BIG_BREAKPOINT
;
70 *lenptr
= sizeof (big_break_insn
);
71 return big_break_insn
;
74 #ifdef LITTLE_BREAKPOINT
75 if (TARGET_BYTE_ORDER
!= BFD_ENDIAN_BIG
)
77 static unsigned char little_break_insn
[] = LITTLE_BREAKPOINT
;
78 *lenptr
= sizeof (little_break_insn
);
79 return little_break_insn
;
84 static unsigned char break_insn
[] = BREAKPOINT
;
85 *lenptr
= sizeof (break_insn
);
94 generic_frameless_function_invocation_not (struct frame_info
*fi
)
100 generic_return_value_on_stack_not (struct type
*type
)
106 generic_skip_trampoline_code (CORE_ADDR pc
)
112 generic_in_solib_call_trampoline (CORE_ADDR pc
, char *name
)
118 generic_in_function_epilogue_p (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
124 legacy_register_name (int i
)
126 #ifdef REGISTER_NAMES
127 static char *names
[] = REGISTER_NAMES
;
128 if (i
< 0 || i
>= (sizeof (names
) / sizeof (*names
)))
133 internal_error (__FILE__
, __LINE__
,
134 "legacy_register_name: called.");
139 #if defined (CALL_DUMMY)
140 LONGEST legacy_call_dummy_words
[] = CALL_DUMMY
;
142 LONGEST legacy_call_dummy_words
[1];
144 int legacy_sizeof_call_dummy_words
= sizeof (legacy_call_dummy_words
);
147 generic_remote_translate_xfer_address (CORE_ADDR gdb_addr
, int gdb_len
,
148 CORE_ADDR
* rem_addr
, int *rem_len
)
150 *rem_addr
= gdb_addr
;
155 generic_prologue_frameless_p (CORE_ADDR ip
)
157 return ip
== SKIP_PROLOGUE (ip
);
160 /* New/multi-arched targets should use the correct gdbarch field
161 instead of using this global pointer. */
163 legacy_print_insn (bfd_vma vma
, disassemble_info
*info
)
165 return (*tm_print_insn
) (vma
, info
);
168 /* Helper functions for INNER_THAN */
171 core_addr_lessthan (CORE_ADDR lhs
, CORE_ADDR rhs
)
177 core_addr_greaterthan (CORE_ADDR lhs
, CORE_ADDR rhs
)
183 /* Helper functions for TARGET_{FLOAT,DOUBLE}_FORMAT */
185 const struct floatformat
*
186 default_float_format (struct gdbarch
*gdbarch
)
189 int byte_order
= gdbarch_byte_order (gdbarch
);
191 int byte_order
= TARGET_BYTE_ORDER
;
196 return &floatformat_ieee_single_big
;
197 case BFD_ENDIAN_LITTLE
:
198 return &floatformat_ieee_single_little
;
200 internal_error (__FILE__
, __LINE__
,
201 "default_float_format: bad byte order");
206 const struct floatformat
*
207 default_double_format (struct gdbarch
*gdbarch
)
210 int byte_order
= gdbarch_byte_order (gdbarch
);
212 int byte_order
= TARGET_BYTE_ORDER
;
217 return &floatformat_ieee_double_big
;
218 case BFD_ENDIAN_LITTLE
:
219 return &floatformat_ieee_double_little
;
221 internal_error (__FILE__
, __LINE__
,
222 "default_double_format: bad byte order");
227 default_print_float_info (void)
230 #if GDB_MULTI_ARCH > GDB_MULTI_ARCH_PARTIAL
231 #error "FLOAT_INFO defined in multi-arch"
235 printf_filtered ("No floating point info available for this processor.\n");
239 /* Misc helper functions for targets. */
242 frame_num_args_unknown (struct frame_info
*fi
)
249 generic_register_convertible_not (int num
)
255 /* Under some ABI's that specify the `struct convention' for returning
256 structures by value, by the time we've returned from the function,
257 the return value is sitting there in the caller's buffer, but GDB
258 has no way to find the address of that buffer.
260 On such architectures, use this function as your
261 extract_struct_value_address method. When asked to a struct
262 returned by value in this fashion, GDB will print a nice error
263 message, instead of garbage. */
265 generic_cannot_extract_struct_value_address (char *dummy
)
271 default_register_sim_regno (int num
)
278 core_addr_identity (CORE_ADDR addr
)
284 no_op_reg_to_regnum (int reg
)
289 /* For use by frame_args_address and frame_locals_address. */
291 default_frame_address (struct frame_info
*fi
)
296 /* Default prepare_to_procced(). */
298 default_prepare_to_proceed (int select_it
)
303 /* Generic prepare_to_proceed(). This one should be suitable for most
304 targets that support threads. */
306 generic_prepare_to_proceed (int select_it
)
309 struct target_waitstatus wait_status
;
311 /* Get the last target status returned by target_wait(). */
312 get_last_target_status (&wait_ptid
, &wait_status
);
314 /* Make sure we were stopped either at a breakpoint, or because
316 if (wait_status
.kind
!= TARGET_WAITKIND_STOPPED
317 || (wait_status
.value
.sig
!= TARGET_SIGNAL_TRAP
&&
318 wait_status
.value
.sig
!= TARGET_SIGNAL_INT
))
323 if (!ptid_equal (wait_ptid
, minus_one_ptid
)
324 && !ptid_equal (inferior_ptid
, wait_ptid
))
326 /* Switched over from WAIT_PID. */
327 CORE_ADDR wait_pc
= read_pc_pid (wait_ptid
);
329 if (wait_pc
!= read_pc ())
333 /* Switch back to WAIT_PID thread. */
334 inferior_ptid
= wait_ptid
;
336 /* FIXME: This stuff came from switch_to_thread() in
337 thread.c (which should probably be a public function). */
338 flush_cached_frames ();
339 registers_changed ();
341 select_frame (get_current_frame ());
343 /* We return 1 to indicate that there is a breakpoint here,
344 so we need to step over it before continuing to avoid
345 hitting it straight away. */
346 if (breakpoint_here_p (wait_pc
))
357 init_frame_pc_noop (int fromleaf
, struct frame_info
*prev
)
363 init_frame_pc_default (int fromleaf
, struct frame_info
*prev
)
366 prev
->pc
= SAVED_PC_AFTER_CALL (prev
->next
);
367 else if (prev
->next
!= NULL
)
368 prev
->pc
= FRAME_SAVED_PC (prev
->next
);
370 prev
->pc
= read_pc ();
374 default_elf_make_msymbol_special (asymbol
*sym
, struct minimal_symbol
*msym
)
380 default_coff_make_msymbol_special (int val
, struct minimal_symbol
*msym
)
386 cannot_register_not (int regnum
)
391 /* Legacy version of target_virtual_frame_pointer(). Assumes that
392 there is an FP_REGNUM and that it is the same, cooked or raw. */
395 legacy_virtual_frame_pointer (CORE_ADDR pc
,
397 LONGEST
*frame_offset
)
399 gdb_assert (FP_REGNUM
>= 0);
400 *frame_regnum
= FP_REGNUM
;
404 /* Assume the world is flat. Every register is large enough to fit a
408 generic_register_raw_size (int regnum
)
410 gdb_assert (regnum
>= 0 && regnum
< NUM_REGS
+ NUM_PSEUDO_REGS
);
411 return TARGET_INT_BIT
/ HOST_CHAR_BIT
;
414 /* Assume the virtual size corresponds to the virtual type. */
417 generic_register_virtual_size (int regnum
)
419 return TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (regnum
));
422 #if !defined (IN_SIGTRAMP)
423 #if defined (SIGTRAMP_START)
424 #define IN_SIGTRAMP(pc, name) \
425 ((pc) >= SIGTRAMP_START(pc) \
426 && (pc) < SIGTRAMP_END(pc) \
429 #define IN_SIGTRAMP(pc, name) \
430 (name && STREQ ("_sigtramp", name))
435 legacy_pc_in_sigtramp (CORE_ADDR pc
, char *name
)
437 return IN_SIGTRAMP(pc
, name
);
441 /* Functions to manipulate the endianness of the target. */
443 /* ``target_byte_order'' is only used when non- multi-arch.
444 Multi-arch targets obtain the current byte order using the
445 TARGET_BYTE_ORDER gdbarch method.
447 The choice of initial value is entirely arbitrary. During startup,
448 the function initialize_current_architecture() updates this value
449 based on default byte-order information extracted from BFD. */
450 int target_byte_order
= BFD_ENDIAN_BIG
;
451 int target_byte_order_auto
= 1;
453 static const char endian_big
[] = "big";
454 static const char endian_little
[] = "little";
455 static const char endian_auto
[] = "auto";
456 static const char *endian_enum
[] =
463 static const char *set_endian_string
;
465 /* Called by ``show endian''. */
468 show_endian (char *args
, int from_tty
)
470 if (TARGET_BYTE_ORDER_AUTO
)
471 printf_unfiltered ("The target endianness is set automatically (currently %s endian)\n",
472 (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
? "big" : "little"));
474 printf_unfiltered ("The target is assumed to be %s endian\n",
475 (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
? "big" : "little"));
479 set_endian (char *ignore_args
, int from_tty
, struct cmd_list_element
*c
)
481 if (set_endian_string
== endian_auto
)
483 target_byte_order_auto
= 1;
485 else if (set_endian_string
== endian_little
)
487 target_byte_order_auto
= 0;
490 struct gdbarch_info info
;
491 gdbarch_info_init (&info
);
492 info
.byte_order
= BFD_ENDIAN_LITTLE
;
493 if (! gdbarch_update_p (info
))
495 printf_unfiltered ("Little endian target not supported by GDB\n");
500 target_byte_order
= BFD_ENDIAN_LITTLE
;
503 else if (set_endian_string
== endian_big
)
505 target_byte_order_auto
= 0;
508 struct gdbarch_info info
;
509 gdbarch_info_init (&info
);
510 info
.byte_order
= BFD_ENDIAN_BIG
;
511 if (! gdbarch_update_p (info
))
513 printf_unfiltered ("Big endian target not supported by GDB\n");
518 target_byte_order
= BFD_ENDIAN_BIG
;
522 internal_error (__FILE__
, __LINE__
,
523 "set_endian: bad value");
524 show_endian (NULL
, from_tty
);
527 /* Set the endianness from a BFD. */
530 set_endian_from_file (bfd
*abfd
)
534 internal_error (__FILE__
, __LINE__
,
535 "set_endian_from_file: not for multi-arch");
536 if (bfd_big_endian (abfd
))
537 want
= BFD_ENDIAN_BIG
;
539 want
= BFD_ENDIAN_LITTLE
;
540 if (TARGET_BYTE_ORDER_AUTO
)
541 target_byte_order
= want
;
542 else if (TARGET_BYTE_ORDER
!= want
)
543 warning ("%s endian file does not match %s endian target.",
544 want
== BFD_ENDIAN_BIG
? "big" : "little",
545 TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
? "big" : "little");
549 /* Functions to manipulate the architecture of the target */
551 enum set_arch
{ set_arch_auto
, set_arch_manual
};
553 int target_architecture_auto
= 1;
555 const char *set_architecture_string
;
557 /* Old way of changing the current architecture. */
559 extern const struct bfd_arch_info bfd_default_arch_struct
;
560 const struct bfd_arch_info
*target_architecture
= &bfd_default_arch_struct
;
561 int (*target_architecture_hook
) (const struct bfd_arch_info
*ap
);
564 arch_ok (const struct bfd_arch_info
*arch
)
567 internal_error (__FILE__
, __LINE__
,
568 "arch_ok: not multi-arched");
569 /* Should be performing the more basic check that the binary is
570 compatible with GDB. */
571 /* Check with the target that the architecture is valid. */
572 return (target_architecture_hook
== NULL
573 || target_architecture_hook (arch
));
577 set_arch (const struct bfd_arch_info
*arch
,
581 internal_error (__FILE__
, __LINE__
,
582 "set_arch: not multi-arched");
587 warning ("Target may not support %s architecture",
588 arch
->printable_name
);
589 target_architecture
= arch
;
591 case set_arch_manual
:
594 printf_unfiltered ("Target does not support `%s' architecture.\n",
595 arch
->printable_name
);
599 target_architecture_auto
= 0;
600 target_architecture
= arch
;
605 gdbarch_dump (current_gdbarch
, gdb_stdlog
);
608 /* Set the architecture from arch/machine (deprecated) */
611 set_architecture_from_arch_mach (enum bfd_architecture arch
,
614 const struct bfd_arch_info
*wanted
= bfd_lookup_arch (arch
, mach
);
616 internal_error (__FILE__
, __LINE__
,
617 "set_architecture_from_arch_mach: not multi-arched");
619 set_arch (wanted
, set_arch_manual
);
621 internal_error (__FILE__
, __LINE__
,
622 "gdbarch: hardwired architecture/machine not recognized");
625 /* Set the architecture from a BFD (deprecated) */
628 set_architecture_from_file (bfd
*abfd
)
630 const struct bfd_arch_info
*wanted
= bfd_get_arch_info (abfd
);
632 internal_error (__FILE__
, __LINE__
,
633 "set_architecture_from_file: not multi-arched");
634 if (target_architecture_auto
)
636 set_arch (wanted
, set_arch_auto
);
638 else if (wanted
!= target_architecture
)
640 warning ("%s architecture file may be incompatible with %s target.",
641 wanted
->printable_name
,
642 target_architecture
->printable_name
);
647 /* Called if the user enters ``show architecture'' without an
651 show_architecture (char *args
, int from_tty
)
654 arch
= TARGET_ARCHITECTURE
->printable_name
;
655 if (target_architecture_auto
)
656 printf_filtered ("The target architecture is set automatically (currently %s)\n", arch
);
658 printf_filtered ("The target architecture is assumed to be %s\n", arch
);
662 /* Called if the user enters ``set architecture'' with or without an
666 set_architecture (char *ignore_args
, int from_tty
, struct cmd_list_element
*c
)
668 if (strcmp (set_architecture_string
, "auto") == 0)
670 target_architecture_auto
= 1;
672 else if (GDB_MULTI_ARCH
)
674 struct gdbarch_info info
;
675 gdbarch_info_init (&info
);
676 info
.bfd_arch_info
= bfd_scan_arch (set_architecture_string
);
677 if (info
.bfd_arch_info
== NULL
)
678 internal_error (__FILE__
, __LINE__
,
679 "set_architecture: bfd_scan_arch failed");
680 if (gdbarch_update_p (info
))
681 target_architecture_auto
= 0;
683 printf_unfiltered ("Architecture `%s' not recognized.\n",
684 set_architecture_string
);
688 const struct bfd_arch_info
*arch
689 = bfd_scan_arch (set_architecture_string
);
691 internal_error (__FILE__
, __LINE__
,
692 "set_architecture: bfd_scan_arch failed");
693 set_arch (arch
, set_arch_manual
);
695 show_architecture (NULL
, from_tty
);
698 /* Set the dynamic target-system-dependent parameters (architecture,
699 byte-order) using information found in the BFD */
702 set_gdbarch_from_file (bfd
*abfd
)
706 struct gdbarch_info info
;
707 gdbarch_info_init (&info
);
709 if (! gdbarch_update_p (info
))
710 error ("Architecture of file not recognized.\n");
714 set_architecture_from_file (abfd
);
715 set_endian_from_file (abfd
);
719 /* Initialize the current architecture. Update the ``set
720 architecture'' command so that it specifies a list of valid
723 #ifdef DEFAULT_BFD_ARCH
724 extern const bfd_arch_info_type DEFAULT_BFD_ARCH
;
725 static const bfd_arch_info_type
*default_bfd_arch
= &DEFAULT_BFD_ARCH
;
727 static const bfd_arch_info_type
*default_bfd_arch
;
730 #ifdef DEFAULT_BFD_VEC
731 extern const bfd_target DEFAULT_BFD_VEC
;
732 static const bfd_target
*default_bfd_vec
= &DEFAULT_BFD_VEC
;
734 static const bfd_target
*default_bfd_vec
;
738 initialize_current_architecture (void)
740 const char **arches
= gdbarch_printable_names ();
742 /* determine a default architecture and byte order. */
743 struct gdbarch_info info
;
744 gdbarch_info_init (&info
);
746 /* Find a default architecture. */
747 if (info
.bfd_arch_info
== NULL
748 && default_bfd_arch
!= NULL
)
749 info
.bfd_arch_info
= default_bfd_arch
;
750 if (info
.bfd_arch_info
== NULL
)
752 /* Choose the architecture by taking the first one
754 const char *chosen
= arches
[0];
756 for (arch
= arches
; *arch
!= NULL
; arch
++)
758 if (strcmp (*arch
, chosen
) < 0)
762 internal_error (__FILE__
, __LINE__
,
763 "initialize_current_architecture: No arch");
764 info
.bfd_arch_info
= bfd_scan_arch (chosen
);
765 if (info
.bfd_arch_info
== NULL
)
766 internal_error (__FILE__
, __LINE__
,
767 "initialize_current_architecture: Arch not found");
770 /* Take several guesses at a byte order. */
771 if (info
.byte_order
== BFD_ENDIAN_UNKNOWN
772 && default_bfd_vec
!= NULL
)
774 /* Extract BFD's default vector's byte order. */
775 switch (default_bfd_vec
->byteorder
)
778 info
.byte_order
= BFD_ENDIAN_BIG
;
780 case BFD_ENDIAN_LITTLE
:
781 info
.byte_order
= BFD_ENDIAN_LITTLE
;
787 if (info
.byte_order
== BFD_ENDIAN_UNKNOWN
)
789 /* look for ``*el-*'' in the target name. */
791 chp
= strchr (target_name
, '-');
793 && chp
- 2 >= target_name
794 && strncmp (chp
- 2, "el", 2) == 0)
795 info
.byte_order
= BFD_ENDIAN_LITTLE
;
797 if (info
.byte_order
== BFD_ENDIAN_UNKNOWN
)
799 /* Wire it to big-endian!!! */
800 info
.byte_order
= BFD_ENDIAN_BIG
;
805 if (! gdbarch_update_p (info
))
807 internal_error (__FILE__
, __LINE__
,
808 "initialize_current_architecture: Selection of initial architecture failed");
813 /* If the multi-arch logic comes up with a byte-order (from BFD)
814 use it for the non-multi-arch case. */
815 if (info
.byte_order
!= BFD_ENDIAN_UNKNOWN
)
816 target_byte_order
= info
.byte_order
;
817 initialize_non_multiarch ();
820 /* Create the ``set architecture'' command appending ``auto'' to the
821 list of architectures. */
823 struct cmd_list_element
*c
;
824 /* Append ``auto''. */
826 for (nr
= 0; arches
[nr
] != NULL
; nr
++);
827 arches
= xrealloc (arches
, sizeof (char*) * (nr
+ 2));
828 arches
[nr
+ 0] = "auto";
829 arches
[nr
+ 1] = NULL
;
830 /* FIXME: add_set_enum_cmd() uses an array of ``char *'' instead
831 of ``const char *''. We just happen to know that the casts are
833 c
= add_set_enum_cmd ("architecture", class_support
,
834 arches
, &set_architecture_string
,
835 "Set architecture of target.",
837 set_cmd_sfunc (c
, set_architecture
);
838 add_alias_cmd ("processor", "architecture", class_support
, 1, &setlist
);
839 /* Don't use set_from_show - need to print both auto/manual and
841 add_cmd ("architecture", class_support
, show_architecture
,
842 "Show the current target architecture", &showlist
);
847 /* Initialize a gdbarch info to values that will be automatically
848 overridden. Note: Originally, this ``struct info'' was initialized
849 using memset(0). Unfortunatly, that ran into problems, namely
850 BFD_ENDIAN_BIG is zero. An explicit initialization function that
851 can explicitly set each field to a well defined value is used. */
854 gdbarch_info_init (struct gdbarch_info
*info
)
856 memset (info
, 0, sizeof (struct gdbarch_info
));
857 info
->byte_order
= BFD_ENDIAN_UNKNOWN
;
862 extern initialize_file_ftype _initialize_gdbarch_utils
;
865 _initialize_gdbarch_utils (void)
867 struct cmd_list_element
*c
;
868 c
= add_set_enum_cmd ("endian", class_support
,
869 endian_enum
, &set_endian_string
,
870 "Set endianness of target.",
872 set_cmd_sfunc (c
, set_endian
);
873 /* Don't use set_from_show - need to print both auto/manual and
875 add_cmd ("endian", class_support
, show_endian
,
876 "Show the current byte-order", &showlist
);