1 /* Dynamic architecture support for GDB, the GNU debugger.
3 Copyright (C) 1998-2014 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "arch-utils.h"
25 #include "inferior.h" /* enum CALL_DUMMY_LOCATION et al. */
29 #include "gdb_assert.h"
30 #include "sim-regno.h"
33 #include "target-descriptions.h"
39 #include "floatformat.h"
42 struct displaced_step_closure *
43 simple_displaced_step_copy_insn (struct gdbarch *gdbarch,
44 CORE_ADDR from, CORE_ADDR to,
45 struct regcache *regs)
47 size_t len = gdbarch_max_insn_length (gdbarch);
48 gdb_byte *buf = xmalloc (len);
50 read_memory (from, buf, len);
51 write_memory (to, buf, len);
55 fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
56 paddress (gdbarch, from), paddress (gdbarch, to));
57 displaced_step_dump_bytes (gdb_stdlog, buf, len);
60 return (struct displaced_step_closure *) buf;
65 simple_displaced_step_free_closure (struct gdbarch *gdbarch,
66 struct displaced_step_closure *closure)
72 default_displaced_step_hw_singlestep (struct gdbarch *gdbarch,
73 struct displaced_step_closure *closure)
75 return !gdbarch_software_single_step_p (gdbarch);
79 displaced_step_at_entry_point (struct gdbarch *gdbarch)
84 addr = entry_point_address ();
86 /* Inferior calls also use the entry point as a breakpoint location.
87 We don't want displaced stepping to interfere with those
88 breakpoints, so leave space. */
89 gdbarch_breakpoint_from_pc (gdbarch, &addr, &bp_len);
96 legacy_register_sim_regno (struct gdbarch *gdbarch, int regnum)
98 /* Only makes sense to supply raw registers. */
99 gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch));
100 /* NOTE: cagney/2002-05-13: The old code did it this way and it is
101 suspected that some GDB/SIM combinations may rely on this
102 behavour. The default should be one2one_register_sim_regno
104 if (gdbarch_register_name (gdbarch, regnum) != NULL
105 && gdbarch_register_name (gdbarch, regnum)[0] != '\0')
108 return LEGACY_SIM_REGNO_IGNORE;
112 generic_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
118 generic_skip_solib_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
124 generic_in_solib_return_trampoline (struct gdbarch *gdbarch,
125 CORE_ADDR pc, const char *name)
131 generic_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
136 /* Helper functions for gdbarch_inner_than */
139 core_addr_lessthan (CORE_ADDR lhs, CORE_ADDR rhs)
145 core_addr_greaterthan (CORE_ADDR lhs, CORE_ADDR rhs)
150 /* Misc helper functions for targets. */
153 core_addr_identity (struct gdbarch *gdbarch, CORE_ADDR addr)
159 convert_from_func_ptr_addr_identity (struct gdbarch *gdbarch, CORE_ADDR addr,
160 struct target_ops *targ)
166 no_op_reg_to_regnum (struct gdbarch *gdbarch, int reg)
172 default_elf_make_msymbol_special (asymbol *sym, struct minimal_symbol *msym)
178 default_coff_make_msymbol_special (int val, struct minimal_symbol *msym)
184 cannot_register_not (struct gdbarch *gdbarch, int regnum)
189 /* Legacy version of target_virtual_frame_pointer(). Assumes that
190 there is an gdbarch_deprecated_fp_regnum and that it is the same,
194 legacy_virtual_frame_pointer (struct gdbarch *gdbarch,
197 LONGEST *frame_offset)
199 /* FIXME: cagney/2002-09-13: This code is used when identifying the
200 frame pointer of the current PC. It is assuming that a single
201 register and an offset can determine this. I think it should
202 instead generate a byte code expression as that would work better
203 with things like Dwarf2's CFI. */
204 if (gdbarch_deprecated_fp_regnum (gdbarch) >= 0
205 && gdbarch_deprecated_fp_regnum (gdbarch)
206 < gdbarch_num_regs (gdbarch))
207 *frame_regnum = gdbarch_deprecated_fp_regnum (gdbarch);
208 else if (gdbarch_sp_regnum (gdbarch) >= 0
209 && gdbarch_sp_regnum (gdbarch)
210 < gdbarch_num_regs (gdbarch))
211 *frame_regnum = gdbarch_sp_regnum (gdbarch);
213 /* Should this be an internal error? I guess so, it is reflecting
214 an architectural limitation in the current design. */
215 internal_error (__FILE__, __LINE__,
216 _("No virtual frame pointer available"));
222 generic_convert_register_p (struct gdbarch *gdbarch, int regnum,
229 default_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
235 generic_instruction_nullified (struct gdbarch *gdbarch,
236 struct regcache *regcache)
242 default_remote_register_number (struct gdbarch *gdbarch,
249 /* Functions to manipulate the endianness of the target. */
251 static int target_byte_order_user = BFD_ENDIAN_UNKNOWN;
253 static const char endian_big[] = "big";
254 static const char endian_little[] = "little";
255 static const char endian_auto[] = "auto";
256 static const char *const endian_enum[] =
263 static const char *set_endian_string;
266 selected_byte_order (void)
268 return target_byte_order_user;
271 /* Called by ``show endian''. */
274 show_endian (struct ui_file *file, int from_tty, struct cmd_list_element *c,
277 if (target_byte_order_user == BFD_ENDIAN_UNKNOWN)
278 if (gdbarch_byte_order (get_current_arch ()) == BFD_ENDIAN_BIG)
279 fprintf_unfiltered (file, _("The target endianness is set automatically "
280 "(currently big endian)\n"));
282 fprintf_unfiltered (file, _("The target endianness is set automatically "
283 "(currently little endian)\n"));
285 if (target_byte_order_user == BFD_ENDIAN_BIG)
286 fprintf_unfiltered (file,
287 _("The target is assumed to be big endian\n"));
289 fprintf_unfiltered (file,
290 _("The target is assumed to be little endian\n"));
294 set_endian (char *ignore_args, int from_tty, struct cmd_list_element *c)
296 struct gdbarch_info info;
298 gdbarch_info_init (&info);
300 if (set_endian_string == endian_auto)
302 target_byte_order_user = BFD_ENDIAN_UNKNOWN;
303 if (! gdbarch_update_p (info))
304 internal_error (__FILE__, __LINE__,
305 _("set_endian: architecture update failed"));
307 else if (set_endian_string == endian_little)
309 info.byte_order = BFD_ENDIAN_LITTLE;
310 if (! gdbarch_update_p (info))
311 printf_unfiltered (_("Little endian target not supported by GDB\n"));
313 target_byte_order_user = BFD_ENDIAN_LITTLE;
315 else if (set_endian_string == endian_big)
317 info.byte_order = BFD_ENDIAN_BIG;
318 if (! gdbarch_update_p (info))
319 printf_unfiltered (_("Big endian target not supported by GDB\n"));
321 target_byte_order_user = BFD_ENDIAN_BIG;
324 internal_error (__FILE__, __LINE__,
325 _("set_endian: bad value"));
327 show_endian (gdb_stdout, from_tty, NULL, NULL);
330 /* Given SELECTED, a currently selected BFD architecture, and
331 TARGET_DESC, the current target description, return what
334 SELECTED may be NULL, in which case we return the architecture
335 associated with TARGET_DESC. If SELECTED specifies a variant
336 of the architecture associtated with TARGET_DESC, return the
337 more specific of the two.
339 If SELECTED is a different architecture, but it is accepted as
340 compatible by the target, we can use the target architecture.
342 If SELECTED is obviously incompatible, warn the user. */
344 static const struct bfd_arch_info *
345 choose_architecture_for_target (const struct target_desc *target_desc,
346 const struct bfd_arch_info *selected)
348 const struct bfd_arch_info *from_target = tdesc_architecture (target_desc);
349 const struct bfd_arch_info *compat1, *compat2;
351 if (selected == NULL)
354 if (from_target == NULL)
357 /* struct bfd_arch_info objects are singletons: that is, there's
358 supposed to be exactly one instance for a given machine. So you
359 can tell whether two are equivalent by comparing pointers. */
360 if (from_target == selected)
363 /* BFD's 'A->compatible (A, B)' functions return zero if A and B are
364 incompatible. But if they are compatible, it returns the 'more
365 featureful' of the two arches. That is, if A can run code
366 written for B, but B can't run code written for A, then it'll
369 Some targets (e.g. MIPS as of 2006-12-04) don't fully
370 implement this, instead always returning NULL or the first
371 argument. We detect that case by checking both directions. */
373 compat1 = selected->compatible (selected, from_target);
374 compat2 = from_target->compatible (from_target, selected);
376 if (compat1 == NULL && compat2 == NULL)
378 /* BFD considers the architectures incompatible. Check our
379 target description whether it accepts SELECTED as compatible
381 if (tdesc_compatible_p (target_desc, selected))
384 warning (_("Selected architecture %s is not compatible "
385 "with reported target architecture %s"),
386 selected->printable_name, from_target->printable_name);
394 if (compat1 == compat2)
397 /* If the two didn't match, but one of them was a default
398 architecture, assume the more specific one is correct. This
399 handles the case where an executable or target description just
400 says "mips", but the other knows which MIPS variant. */
401 if (compat1->the_default)
403 if (compat2->the_default)
406 /* We have no idea which one is better. This is a bug, but not
407 a critical problem; warn the user. */
408 warning (_("Selected architecture %s is ambiguous with "
409 "reported target architecture %s"),
410 selected->printable_name, from_target->printable_name);
414 /* Functions to manipulate the architecture of the target. */
416 enum set_arch { set_arch_auto, set_arch_manual };
418 static const struct bfd_arch_info *target_architecture_user;
420 static const char *set_architecture_string;
423 selected_architecture_name (void)
425 if (target_architecture_user == NULL)
428 return set_architecture_string;
431 /* Called if the user enters ``show architecture'' without an
435 show_architecture (struct ui_file *file, int from_tty,
436 struct cmd_list_element *c, const char *value)
438 if (target_architecture_user == NULL)
439 fprintf_filtered (file, _("The target architecture is set "
440 "automatically (currently %s)\n"),
441 gdbarch_bfd_arch_info (get_current_arch ())->printable_name);
443 fprintf_filtered (file, _("The target architecture is assumed to be %s\n"),
444 set_architecture_string);
448 /* Called if the user enters ``set architecture'' with or without an
452 set_architecture (char *ignore_args, int from_tty, struct cmd_list_element *c)
454 struct gdbarch_info info;
456 gdbarch_info_init (&info);
458 if (strcmp (set_architecture_string, "auto") == 0)
460 target_architecture_user = NULL;
461 if (!gdbarch_update_p (info))
462 internal_error (__FILE__, __LINE__,
463 _("could not select an architecture automatically"));
467 info.bfd_arch_info = bfd_scan_arch (set_architecture_string);
468 if (info.bfd_arch_info == NULL)
469 internal_error (__FILE__, __LINE__,
470 _("set_architecture: bfd_scan_arch failed"));
471 if (gdbarch_update_p (info))
472 target_architecture_user = info.bfd_arch_info;
474 printf_unfiltered (_("Architecture `%s' not recognized.\n"),
475 set_architecture_string);
477 show_architecture (gdb_stdout, from_tty, NULL, NULL);
480 /* Try to select a global architecture that matches "info". Return
481 non-zero if the attempt succeeds. */
483 gdbarch_update_p (struct gdbarch_info info)
485 struct gdbarch *new_gdbarch;
487 /* Check for the current file. */
488 if (info.abfd == NULL)
489 info.abfd = exec_bfd;
490 if (info.abfd == NULL)
491 info.abfd = core_bfd;
493 /* Check for the current target description. */
494 if (info.target_desc == NULL)
495 info.target_desc = target_current_description ();
497 new_gdbarch = gdbarch_find_by_info (info);
499 /* If there no architecture by that name, reject the request. */
500 if (new_gdbarch == NULL)
503 fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
504 "Architecture not found\n");
508 /* If it is the same old architecture, accept the request (but don't
510 if (new_gdbarch == target_gdbarch ())
513 fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
514 "Architecture %s (%s) unchanged\n",
515 host_address_to_string (new_gdbarch),
516 gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
520 /* It's a new architecture, swap it in. */
522 fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
523 "New architecture %s (%s) selected\n",
524 host_address_to_string (new_gdbarch),
525 gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
526 set_target_gdbarch (new_gdbarch);
531 /* Return the architecture for ABFD. If no suitable architecture
532 could be find, return NULL. */
535 gdbarch_from_bfd (bfd *abfd)
537 struct gdbarch_info info;
538 gdbarch_info_init (&info);
541 return gdbarch_find_by_info (info);
544 /* Set the dynamic target-system-dependent parameters (architecture,
545 byte-order) using information found in the BFD */
548 set_gdbarch_from_file (bfd *abfd)
550 struct gdbarch_info info;
551 struct gdbarch *gdbarch;
553 gdbarch_info_init (&info);
555 info.target_desc = target_current_description ();
556 gdbarch = gdbarch_find_by_info (info);
559 error (_("Architecture of file not recognized."));
560 set_target_gdbarch (gdbarch);
563 /* Initialize the current architecture. Update the ``set
564 architecture'' command so that it specifies a list of valid
567 #ifdef DEFAULT_BFD_ARCH
568 extern const bfd_arch_info_type DEFAULT_BFD_ARCH;
569 static const bfd_arch_info_type *default_bfd_arch = &DEFAULT_BFD_ARCH;
571 static const bfd_arch_info_type *default_bfd_arch;
574 #ifdef DEFAULT_BFD_VEC
575 extern const bfd_target DEFAULT_BFD_VEC;
576 static const bfd_target *default_bfd_vec = &DEFAULT_BFD_VEC;
578 static const bfd_target *default_bfd_vec;
581 static int default_byte_order = BFD_ENDIAN_UNKNOWN;
584 initialize_current_architecture (void)
586 const char **arches = gdbarch_printable_names ();
587 struct gdbarch_info info;
589 /* determine a default architecture and byte order. */
590 gdbarch_info_init (&info);
592 /* Find a default architecture. */
593 if (default_bfd_arch == NULL)
595 /* Choose the architecture by taking the first one
597 const char *chosen = arches[0];
599 for (arch = arches; *arch != NULL; arch++)
601 if (strcmp (*arch, chosen) < 0)
605 internal_error (__FILE__, __LINE__,
606 _("initialize_current_architecture: No arch"));
607 default_bfd_arch = bfd_scan_arch (chosen);
608 if (default_bfd_arch == NULL)
609 internal_error (__FILE__, __LINE__,
610 _("initialize_current_architecture: Arch not found"));
613 info.bfd_arch_info = default_bfd_arch;
615 /* Take several guesses at a byte order. */
616 if (default_byte_order == BFD_ENDIAN_UNKNOWN
617 && default_bfd_vec != NULL)
619 /* Extract BFD's default vector's byte order. */
620 switch (default_bfd_vec->byteorder)
623 default_byte_order = BFD_ENDIAN_BIG;
625 case BFD_ENDIAN_LITTLE:
626 default_byte_order = BFD_ENDIAN_LITTLE;
632 if (default_byte_order == BFD_ENDIAN_UNKNOWN)
634 /* look for ``*el-*'' in the target name. */
636 chp = strchr (target_name, '-');
638 && chp - 2 >= target_name
639 && strncmp (chp - 2, "el", 2) == 0)
640 default_byte_order = BFD_ENDIAN_LITTLE;
642 if (default_byte_order == BFD_ENDIAN_UNKNOWN)
644 /* Wire it to big-endian!!! */
645 default_byte_order = BFD_ENDIAN_BIG;
648 info.byte_order = default_byte_order;
649 info.byte_order_for_code = info.byte_order;
651 if (! gdbarch_update_p (info))
652 internal_error (__FILE__, __LINE__,
653 _("initialize_current_architecture: Selection of "
654 "initial architecture failed"));
656 /* Create the ``set architecture'' command appending ``auto'' to the
657 list of architectures. */
659 /* Append ``auto''. */
661 for (nr = 0; arches[nr] != NULL; nr++);
662 arches = xrealloc (arches, sizeof (char*) * (nr + 2));
663 arches[nr + 0] = "auto";
664 arches[nr + 1] = NULL;
665 add_setshow_enum_cmd ("architecture", class_support,
666 arches, &set_architecture_string,
667 _("Set architecture of target."),
668 _("Show architecture of target."), NULL,
669 set_architecture, show_architecture,
670 &setlist, &showlist);
671 add_alias_cmd ("processor", "architecture", class_support, 1, &setlist);
676 /* Initialize a gdbarch info to values that will be automatically
677 overridden. Note: Originally, this ``struct info'' was initialized
678 using memset(0). Unfortunately, that ran into problems, namely
679 BFD_ENDIAN_BIG is zero. An explicit initialization function that
680 can explicitly set each field to a well defined value is used. */
683 gdbarch_info_init (struct gdbarch_info *info)
685 memset (info, 0, sizeof (struct gdbarch_info));
686 info->byte_order = BFD_ENDIAN_UNKNOWN;
687 info->byte_order_for_code = info->byte_order;
688 info->osabi = GDB_OSABI_UNINITIALIZED;
691 /* Similar to init, but this time fill in the blanks. Information is
692 obtained from the global "set ..." options and explicitly
693 initialized INFO fields. */
696 gdbarch_info_fill (struct gdbarch_info *info)
698 /* "(gdb) set architecture ...". */
699 if (info->bfd_arch_info == NULL
700 && target_architecture_user)
701 info->bfd_arch_info = target_architecture_user;
703 if (info->bfd_arch_info == NULL
704 && info->abfd != NULL
705 && bfd_get_arch (info->abfd) != bfd_arch_unknown
706 && bfd_get_arch (info->abfd) != bfd_arch_obscure)
707 info->bfd_arch_info = bfd_get_arch_info (info->abfd);
708 /* From the target. */
709 if (info->target_desc != NULL)
710 info->bfd_arch_info = choose_architecture_for_target
711 (info->target_desc, info->bfd_arch_info);
712 /* From the default. */
713 if (info->bfd_arch_info == NULL)
714 info->bfd_arch_info = default_bfd_arch;
716 /* "(gdb) set byte-order ...". */
717 if (info->byte_order == BFD_ENDIAN_UNKNOWN
718 && target_byte_order_user != BFD_ENDIAN_UNKNOWN)
719 info->byte_order = target_byte_order_user;
720 /* From the INFO struct. */
721 if (info->byte_order == BFD_ENDIAN_UNKNOWN
722 && info->abfd != NULL)
723 info->byte_order = (bfd_big_endian (info->abfd) ? BFD_ENDIAN_BIG
724 : bfd_little_endian (info->abfd) ? BFD_ENDIAN_LITTLE
725 : BFD_ENDIAN_UNKNOWN);
726 /* From the default. */
727 if (info->byte_order == BFD_ENDIAN_UNKNOWN)
728 info->byte_order = default_byte_order;
729 info->byte_order_for_code = info->byte_order;
731 /* "(gdb) set osabi ...". Handled by gdbarch_lookup_osabi. */
732 /* From the manual override, or from file. */
733 if (info->osabi == GDB_OSABI_UNINITIALIZED)
734 info->osabi = gdbarch_lookup_osabi (info->abfd);
735 /* From the target. */
736 if (info->osabi == GDB_OSABI_UNKNOWN && info->target_desc != NULL)
737 info->osabi = tdesc_osabi (info->target_desc);
738 /* From the configured default. */
739 #ifdef GDB_OSABI_DEFAULT
740 if (info->osabi == GDB_OSABI_UNKNOWN)
741 info->osabi = GDB_OSABI_DEFAULT;
744 /* Must have at least filled in the architecture. */
745 gdb_assert (info->bfd_arch_info != NULL);
748 /* Return "current" architecture. If the target is running, this is
749 the architecture of the selected frame. Otherwise, the "current"
750 architecture defaults to the target architecture.
752 This function should normally be called solely by the command
753 interpreter routines to determine the architecture to execute a
756 get_current_arch (void)
758 if (has_stack_frames ())
759 return get_frame_arch (get_selected_frame (NULL));
761 return target_gdbarch ();
765 default_has_shared_address_space (struct gdbarch *gdbarch)
767 /* Simply say no. In most unix-like targets each inferior/process
768 has its own address space. */
773 default_fast_tracepoint_valid_at (struct gdbarch *gdbarch,
774 CORE_ADDR addr, int *isize, char **msg)
776 /* We don't know if maybe the target has some way to do fast
777 tracepoints that doesn't need gdbarch, so always say yes. */
784 default_remote_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
787 gdbarch_breakpoint_from_pc (gdbarch, pcptr, kindptr);
791 default_gen_return_address (struct gdbarch *gdbarch,
792 struct agent_expr *ax, struct axs_value *value,
795 error (_("This architecture has no method to collect a return address."));
799 default_return_in_first_hidden_param_p (struct gdbarch *gdbarch,
802 /* Usually, the return value's address is stored the in the "first hidden"
803 parameter if the return value should be passed by reference, as
805 return language_pass_by_reference (type);
808 int default_insn_is_call (struct gdbarch *gdbarch, CORE_ADDR addr)
813 int default_insn_is_ret (struct gdbarch *gdbarch, CORE_ADDR addr)
818 int default_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
825 /* -Wmissing-prototypes */
826 extern initialize_file_ftype _initialize_gdbarch_utils;
829 _initialize_gdbarch_utils (void)
831 add_setshow_enum_cmd ("endian", class_support,
832 endian_enum, &set_endian_string,
833 _("Set endianness of target."),
834 _("Show endianness of target."),
835 NULL, set_endian, show_endian,
836 &setlist, &showlist);