1 /* Dynamic architecture support for GDB, the GNU debugger.
2 Copyright 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
24 #include "arch-utils.h"
26 #include "inferior.h" /* enum CALL_DUMMY_LOCATION et.al. */
28 /* Just include everything in sight so that the every old definition
29 of macro is visible. */
30 #include "gdb_string.h"
34 #include "breakpoint.h"
42 #include "gdb_assert.h"
46 #include "floatformat.h"
48 /* Use the program counter to determine the contents and size
49 of a breakpoint instruction. If no target-dependent macro
50 BREAKPOINT_FROM_PC has been defined to implement this function,
51 assume that the breakpoint doesn't depend on the PC, and
52 use the values of the BIG_BREAKPOINT and LITTLE_BREAKPOINT macros.
53 Return a pointer to a string of bytes that encode a breakpoint
54 instruction, stores the length of the string to *lenptr,
55 and optionally adjust the pc to point to the correct memory location
56 for inserting the breakpoint. */
59 legacy_breakpoint_from_pc (CORE_ADDR * pcptr, int *lenptr)
61 /* {BIG_,LITTLE_}BREAKPOINT is the sequence of bytes we insert for a
62 breakpoint. On some machines, breakpoints are handled by the
63 target environment and we don't have to worry about them here. */
65 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
67 static unsigned char big_break_insn[] = BIG_BREAKPOINT;
68 *lenptr = sizeof (big_break_insn);
69 return big_break_insn;
72 #ifdef LITTLE_BREAKPOINT
73 if (TARGET_BYTE_ORDER != BFD_ENDIAN_BIG)
75 static unsigned char little_break_insn[] = LITTLE_BREAKPOINT;
76 *lenptr = sizeof (little_break_insn);
77 return little_break_insn;
82 static unsigned char break_insn[] = BREAKPOINT;
83 *lenptr = sizeof (break_insn);
92 generic_frameless_function_invocation_not (struct frame_info *fi)
98 generic_return_value_on_stack_not (struct type *type)
104 generic_skip_trampoline_code (CORE_ADDR pc)
110 generic_in_solib_call_trampoline (CORE_ADDR pc, char *name)
116 generic_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
122 legacy_register_name (int i)
124 #ifdef REGISTER_NAMES
125 static char *names[] = REGISTER_NAMES;
126 if (i < 0 || i >= (sizeof (names) / sizeof (*names)))
131 internal_error (__FILE__, __LINE__,
132 "legacy_register_name: called.");
137 #if defined (CALL_DUMMY)
138 LONGEST legacy_call_dummy_words[] = CALL_DUMMY;
140 LONGEST legacy_call_dummy_words[1];
142 int legacy_sizeof_call_dummy_words = sizeof (legacy_call_dummy_words);
145 generic_remote_translate_xfer_address (CORE_ADDR gdb_addr, int gdb_len,
146 CORE_ADDR * rem_addr, int *rem_len)
148 *rem_addr = gdb_addr;
153 generic_prologue_frameless_p (CORE_ADDR ip)
155 #ifdef SKIP_PROLOGUE_FRAMELESS_P
156 return ip == SKIP_PROLOGUE_FRAMELESS_P (ip);
158 return ip == SKIP_PROLOGUE (ip);
162 /* New/multi-arched targets should use the correct gdbarch field
163 instead of using this global pointer. */
165 legacy_print_insn (bfd_vma vma, disassemble_info *info)
167 return (*tm_print_insn) (vma, info);
170 /* Helper functions for INNER_THAN */
173 core_addr_lessthan (CORE_ADDR lhs, CORE_ADDR rhs)
179 core_addr_greaterthan (CORE_ADDR lhs, CORE_ADDR rhs)
185 /* Helper functions for TARGET_{FLOAT,DOUBLE}_FORMAT */
187 const struct floatformat *
188 default_float_format (struct gdbarch *gdbarch)
191 int byte_order = gdbarch_byte_order (gdbarch);
193 int byte_order = TARGET_BYTE_ORDER;
198 return &floatformat_ieee_single_big;
199 case BFD_ENDIAN_LITTLE:
200 return &floatformat_ieee_single_little;
202 internal_error (__FILE__, __LINE__,
203 "default_float_format: bad byte order");
208 const struct floatformat *
209 default_double_format (struct gdbarch *gdbarch)
212 int byte_order = gdbarch_byte_order (gdbarch);
214 int byte_order = TARGET_BYTE_ORDER;
219 return &floatformat_ieee_double_big;
220 case BFD_ENDIAN_LITTLE:
221 return &floatformat_ieee_double_little;
223 internal_error (__FILE__, __LINE__,
224 "default_double_format: bad byte order");
228 /* Misc helper functions for targets. */
231 frame_num_args_unknown (struct frame_info *fi)
238 generic_register_convertible_not (int num)
244 /* Under some ABI's that specify the `struct convention' for returning
245 structures by value, by the time we've returned from the function,
246 the return value is sitting there in the caller's buffer, but GDB
247 has no way to find the address of that buffer.
249 On such architectures, use this function as your
250 extract_struct_value_address method. When asked to a struct
251 returned by value in this fashion, GDB will print a nice error
252 message, instead of garbage. */
254 generic_cannot_extract_struct_value_address (char *dummy)
260 default_register_sim_regno (int num)
267 core_addr_identity (CORE_ADDR addr)
273 no_op_reg_to_regnum (int reg)
278 /* For use by frame_args_address and frame_locals_address. */
280 default_frame_address (struct frame_info *fi)
285 /* Default prepare_to_procced(). */
287 default_prepare_to_proceed (int select_it)
292 /* Generic prepare_to_proceed(). This one should be suitable for most
293 targets that support threads. */
295 generic_prepare_to_proceed (int select_it)
298 struct target_waitstatus wait_status;
300 /* Get the last target status returned by target_wait(). */
301 get_last_target_status (&wait_ptid, &wait_status);
303 /* Make sure we were stopped either at a breakpoint, or because
305 if (wait_status.kind != TARGET_WAITKIND_STOPPED
306 || (wait_status.value.sig != TARGET_SIGNAL_TRAP &&
307 wait_status.value.sig != TARGET_SIGNAL_INT))
312 if (!ptid_equal (wait_ptid, minus_one_ptid)
313 && !ptid_equal (inferior_ptid, wait_ptid))
315 /* Switched over from WAIT_PID. */
316 CORE_ADDR wait_pc = read_pc_pid (wait_ptid);
318 if (wait_pc != read_pc ())
322 /* Switch back to WAIT_PID thread. */
323 inferior_ptid = wait_ptid;
325 /* FIXME: This stuff came from switch_to_thread() in
326 thread.c (which should probably be a public function). */
327 flush_cached_frames ();
328 registers_changed ();
330 select_frame (get_current_frame (), 0);
332 /* We return 1 to indicate that there is a breakpoint here,
333 so we need to step over it before continuing to avoid
334 hitting it straight away. */
335 if (breakpoint_here_p (wait_pc))
346 init_frame_pc_noop (int fromleaf, struct frame_info *prev)
352 init_frame_pc_default (int fromleaf, struct frame_info *prev)
355 prev->pc = SAVED_PC_AFTER_CALL (prev->next);
356 else if (prev->next != NULL)
357 prev->pc = FRAME_SAVED_PC (prev->next);
359 prev->pc = read_pc ();
363 cannot_register_not (int regnum)
368 /* Legacy version of target_virtual_frame_pointer(). Assumes that
369 there is an FP_REGNUM and that it is the same, cooked or raw. */
372 legacy_virtual_frame_pointer (CORE_ADDR pc,
374 LONGEST *frame_offset)
376 gdb_assert (FP_REGNUM >= 0);
377 *frame_regnum = FP_REGNUM;
381 /* Assume the world is flat. Every register is large enough to fit a
385 generic_register_raw_size (int regnum)
387 gdb_assert (regnum >= 0 && regnum < NUM_REGS + NUM_PSEUDO_REGS);
388 return TARGET_INT_BIT / HOST_CHAR_BIT;
391 /* Assume the virtual size corresponds to the virtual type. */
394 generic_register_virtual_size (int regnum)
396 return TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (regnum));
400 /* Functions to manipulate the endianness of the target. */
402 /* ``target_byte_order'' is only used when non- multi-arch.
403 Multi-arch targets obtain the current byte order using the
404 TARGET_BYTE_ORDER gdbarch method.
406 The choice of initial value is entirely arbitrary. During startup,
407 the function initialize_current_architecture() updates this value
408 based on default byte-order information extracted from BFD. */
409 int target_byte_order = BFD_ENDIAN_BIG;
410 int target_byte_order_auto = 1;
412 static const char endian_big[] = "big";
413 static const char endian_little[] = "little";
414 static const char endian_auto[] = "auto";
415 static const char *endian_enum[] =
422 static const char *set_endian_string;
424 /* Called by ``show endian''. */
427 show_endian (char *args, int from_tty)
429 if (TARGET_BYTE_ORDER_AUTO)
430 printf_unfiltered ("The target endianness is set automatically (currently %s endian)\n",
431 (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? "big" : "little"));
433 printf_unfiltered ("The target is assumed to be %s endian\n",
434 (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? "big" : "little"));
438 set_endian (char *ignore_args, int from_tty, struct cmd_list_element *c)
440 if (set_endian_string == endian_auto)
442 target_byte_order_auto = 1;
444 else if (set_endian_string == endian_little)
446 target_byte_order_auto = 0;
449 struct gdbarch_info info;
450 gdbarch_info_init (&info);
451 info.byte_order = BFD_ENDIAN_LITTLE;
452 if (! gdbarch_update_p (info))
454 printf_unfiltered ("Little endian target not supported by GDB\n");
459 target_byte_order = BFD_ENDIAN_LITTLE;
462 else if (set_endian_string == endian_big)
464 target_byte_order_auto = 0;
467 struct gdbarch_info info;
468 gdbarch_info_init (&info);
469 info.byte_order = BFD_ENDIAN_BIG;
470 if (! gdbarch_update_p (info))
472 printf_unfiltered ("Big endian target not supported by GDB\n");
477 target_byte_order = BFD_ENDIAN_BIG;
481 internal_error (__FILE__, __LINE__,
482 "set_endian: bad value");
483 show_endian (NULL, from_tty);
486 /* Set the endianness from a BFD. */
489 set_endian_from_file (bfd *abfd)
493 internal_error (__FILE__, __LINE__,
494 "set_endian_from_file: not for multi-arch");
495 if (bfd_big_endian (abfd))
496 want = BFD_ENDIAN_BIG;
498 want = BFD_ENDIAN_LITTLE;
499 if (TARGET_BYTE_ORDER_AUTO)
500 target_byte_order = want;
501 else if (TARGET_BYTE_ORDER != want)
502 warning ("%s endian file does not match %s endian target.",
503 want == BFD_ENDIAN_BIG ? "big" : "little",
504 TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? "big" : "little");
508 /* Functions to manipulate the architecture of the target */
510 enum set_arch { set_arch_auto, set_arch_manual };
512 int target_architecture_auto = 1;
514 const char *set_architecture_string;
516 /* Old way of changing the current architecture. */
518 extern const struct bfd_arch_info bfd_default_arch_struct;
519 const struct bfd_arch_info *target_architecture = &bfd_default_arch_struct;
520 int (*target_architecture_hook) (const struct bfd_arch_info *ap);
523 arch_ok (const struct bfd_arch_info *arch)
526 internal_error (__FILE__, __LINE__,
527 "arch_ok: not multi-arched");
528 /* Should be performing the more basic check that the binary is
529 compatible with GDB. */
530 /* Check with the target that the architecture is valid. */
531 return (target_architecture_hook == NULL
532 || target_architecture_hook (arch));
536 set_arch (const struct bfd_arch_info *arch,
540 internal_error (__FILE__, __LINE__,
541 "set_arch: not multi-arched");
546 warning ("Target may not support %s architecture",
547 arch->printable_name);
548 target_architecture = arch;
550 case set_arch_manual:
553 printf_unfiltered ("Target does not support `%s' architecture.\n",
554 arch->printable_name);
558 target_architecture_auto = 0;
559 target_architecture = arch;
564 gdbarch_dump (current_gdbarch, gdb_stdlog);
567 /* Set the architecture from arch/machine (deprecated) */
570 set_architecture_from_arch_mach (enum bfd_architecture arch,
573 const struct bfd_arch_info *wanted = bfd_lookup_arch (arch, mach);
575 internal_error (__FILE__, __LINE__,
576 "set_architecture_from_arch_mach: not multi-arched");
578 set_arch (wanted, set_arch_manual);
580 internal_error (__FILE__, __LINE__,
581 "gdbarch: hardwired architecture/machine not recognized");
584 /* Set the architecture from a BFD (deprecated) */
587 set_architecture_from_file (bfd *abfd)
589 const struct bfd_arch_info *wanted = bfd_get_arch_info (abfd);
591 internal_error (__FILE__, __LINE__,
592 "set_architecture_from_file: not multi-arched");
593 if (target_architecture_auto)
595 set_arch (wanted, set_arch_auto);
597 else if (wanted != target_architecture)
599 warning ("%s architecture file may be incompatible with %s target.",
600 wanted->printable_name,
601 target_architecture->printable_name);
606 /* Called if the user enters ``show architecture'' without an
610 show_architecture (char *args, int from_tty)
613 arch = TARGET_ARCHITECTURE->printable_name;
614 if (target_architecture_auto)
615 printf_filtered ("The target architecture is set automatically (currently %s)\n", arch);
617 printf_filtered ("The target architecture is assumed to be %s\n", arch);
621 /* Called if the user enters ``set architecture'' with or without an
625 set_architecture (char *ignore_args, int from_tty, struct cmd_list_element *c)
627 if (strcmp (set_architecture_string, "auto") == 0)
629 target_architecture_auto = 1;
631 else if (GDB_MULTI_ARCH)
633 struct gdbarch_info info;
634 gdbarch_info_init (&info);
635 info.bfd_arch_info = bfd_scan_arch (set_architecture_string);
636 if (info.bfd_arch_info == NULL)
637 internal_error (__FILE__, __LINE__,
638 "set_architecture: bfd_scan_arch failed");
639 if (gdbarch_update_p (info))
640 target_architecture_auto = 0;
642 printf_unfiltered ("Architecture `%s' not recognized.\n",
643 set_architecture_string);
647 const struct bfd_arch_info *arch
648 = bfd_scan_arch (set_architecture_string);
650 internal_error (__FILE__, __LINE__,
651 "set_architecture: bfd_scan_arch failed");
652 set_arch (arch, set_arch_manual);
654 show_architecture (NULL, from_tty);
657 /* Set the dynamic target-system-dependent parameters (architecture,
658 byte-order) using information found in the BFD */
661 set_gdbarch_from_file (bfd *abfd)
665 struct gdbarch_info info;
666 gdbarch_info_init (&info);
668 if (! gdbarch_update_p (info))
669 error ("Architecture of file not recognized.\n");
673 set_architecture_from_file (abfd);
674 set_endian_from_file (abfd);
678 /* Initialize the current architecture. Update the ``set
679 architecture'' command so that it specifies a list of valid
682 #ifdef DEFAULT_BFD_ARCH
683 extern const bfd_arch_info_type DEFAULT_BFD_ARCH;
684 static const bfd_arch_info_type *default_bfd_arch = &DEFAULT_BFD_ARCH;
686 static const bfd_arch_info_type *default_bfd_arch;
689 #ifdef DEFAULT_BFD_VEC
690 extern const bfd_target DEFAULT_BFD_VEC;
691 static const bfd_target *default_bfd_vec = &DEFAULT_BFD_VEC;
693 static const bfd_target *default_bfd_vec;
697 initialize_current_architecture (void)
699 const char **arches = gdbarch_printable_names ();
701 /* determine a default architecture and byte order. */
702 struct gdbarch_info info;
703 gdbarch_info_init (&info);
705 /* Find a default architecture. */
706 if (info.bfd_arch_info == NULL
707 && default_bfd_arch != NULL)
708 info.bfd_arch_info = default_bfd_arch;
709 if (info.bfd_arch_info == NULL)
711 /* Choose the architecture by taking the first one
713 const char *chosen = arches[0];
715 for (arch = arches; *arch != NULL; arch++)
717 if (strcmp (*arch, chosen) < 0)
721 internal_error (__FILE__, __LINE__,
722 "initialize_current_architecture: No arch");
723 info.bfd_arch_info = bfd_scan_arch (chosen);
724 if (info.bfd_arch_info == NULL)
725 internal_error (__FILE__, __LINE__,
726 "initialize_current_architecture: Arch not found");
729 /* Take several guesses at a byte order. */
730 if (info.byte_order == BFD_ENDIAN_UNKNOWN
731 && default_bfd_vec != NULL)
733 /* Extract BFD's default vector's byte order. */
734 switch (default_bfd_vec->byteorder)
737 info.byte_order = BFD_ENDIAN_BIG;
739 case BFD_ENDIAN_LITTLE:
740 info.byte_order = BFD_ENDIAN_LITTLE;
746 if (info.byte_order == BFD_ENDIAN_UNKNOWN)
748 /* look for ``*el-*'' in the target name. */
750 chp = strchr (target_name, '-');
752 && chp - 2 >= target_name
753 && strncmp (chp - 2, "el", 2) == 0)
754 info.byte_order = BFD_ENDIAN_LITTLE;
756 if (info.byte_order == BFD_ENDIAN_UNKNOWN)
758 /* Wire it to big-endian!!! */
759 info.byte_order = BFD_ENDIAN_BIG;
764 if (! gdbarch_update_p (info))
766 internal_error (__FILE__, __LINE__,
767 "initialize_current_architecture: Selection of initial architecture failed");
772 /* If the multi-arch logic comes up with a byte-order (from BFD)
773 use it for the non-multi-arch case. */
774 if (info.byte_order != BFD_ENDIAN_UNKNOWN)
775 target_byte_order = info.byte_order;
776 initialize_non_multiarch ();
779 /* Create the ``set architecture'' command appending ``auto'' to the
780 list of architectures. */
782 struct cmd_list_element *c;
783 /* Append ``auto''. */
785 for (nr = 0; arches[nr] != NULL; nr++);
786 arches = xrealloc (arches, sizeof (char*) * (nr + 2));
787 arches[nr + 0] = "auto";
788 arches[nr + 1] = NULL;
789 /* FIXME: add_set_enum_cmd() uses an array of ``char *'' instead
790 of ``const char *''. We just happen to know that the casts are
792 c = add_set_enum_cmd ("architecture", class_support,
793 arches, &set_architecture_string,
794 "Set architecture of target.",
796 c->function.sfunc = set_architecture;
797 add_alias_cmd ("processor", "architecture", class_support, 1, &setlist);
798 /* Don't use set_from_show - need to print both auto/manual and
800 add_cmd ("architecture", class_support, show_architecture,
801 "Show the current target architecture", &showlist);
806 /* Initialize a gdbarch info to values that will be automatically
807 overridden. Note: Originally, this ``struct info'' was initialized
808 using memset(0). Unfortunatly, that ran into problems, namely
809 BFD_ENDIAN_BIG is zero. An explicit initialization function that
810 can explicitly set each field to a well defined value is used. */
813 gdbarch_info_init (struct gdbarch_info *info)
815 memset (info, 0, sizeof (struct gdbarch_info));
816 info->byte_order = BFD_ENDIAN_UNKNOWN;
821 extern initialize_file_ftype _initialize_gdbarch_utils;
824 _initialize_gdbarch_utils (void)
826 struct cmd_list_element *c;
827 c = add_set_enum_cmd ("endian", class_support,
828 endian_enum, &set_endian_string,
829 "Set endianness of target.",
831 c->function.sfunc = set_endian;
832 /* Don't use set_from_show - need to print both auto/manual and
834 add_cmd ("endian", class_support, show_endian,
835 "Show the current byte-order", &showlist);