1 /* Dynamic architecture support for GDB, the GNU debugger.
3 Copyright (C) 1998-2013 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. */
26 #include "gdb_string.h"
28 #include "gdb_assert.h"
29 #include "sim-regno.h"
32 #include "target-descriptions.h"
38 #include "floatformat.h"
41 struct displaced_step_closure *
42 simple_displaced_step_copy_insn (struct gdbarch *gdbarch,
43 CORE_ADDR from, CORE_ADDR to,
44 struct regcache *regs)
46 size_t len = gdbarch_max_insn_length (gdbarch);
47 gdb_byte *buf = xmalloc (len);
49 read_memory (from, buf, len);
50 write_memory (to, buf, len);
54 fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
55 paddress (gdbarch, from), paddress (gdbarch, to));
56 displaced_step_dump_bytes (gdb_stdlog, buf, len);
59 return (struct displaced_step_closure *) buf;
64 simple_displaced_step_free_closure (struct gdbarch *gdbarch,
65 struct displaced_step_closure *closure)
71 default_displaced_step_hw_singlestep (struct gdbarch *gdbarch,
72 struct displaced_step_closure *closure)
74 return !gdbarch_software_single_step_p (gdbarch);
78 displaced_step_at_entry_point (struct gdbarch *gdbarch)
83 addr = entry_point_address ();
85 /* Inferior calls also use the entry point as a breakpoint location.
86 We don't want displaced stepping to interfere with those
87 breakpoints, so leave space. */
88 gdbarch_breakpoint_from_pc (gdbarch, &addr, &bp_len);
95 legacy_register_sim_regno (struct gdbarch *gdbarch, int regnum)
97 /* Only makes sense to supply raw registers. */
98 gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch));
99 /* NOTE: cagney/2002-05-13: The old code did it this way and it is
100 suspected that some GDB/SIM combinations may rely on this
101 behavour. The default should be one2one_register_sim_regno
103 if (gdbarch_register_name (gdbarch, regnum) != NULL
104 && gdbarch_register_name (gdbarch, regnum)[0] != '\0')
107 return LEGACY_SIM_REGNO_IGNORE;
111 generic_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
117 generic_skip_solib_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
123 generic_in_solib_return_trampoline (struct gdbarch *gdbarch,
124 CORE_ADDR pc, const char *name)
130 generic_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
135 /* Helper functions for gdbarch_inner_than */
138 core_addr_lessthan (CORE_ADDR lhs, CORE_ADDR rhs)
144 core_addr_greaterthan (CORE_ADDR lhs, CORE_ADDR rhs)
149 /* Misc helper functions for targets. */
152 core_addr_identity (struct gdbarch *gdbarch, CORE_ADDR addr)
158 convert_from_func_ptr_addr_identity (struct gdbarch *gdbarch, CORE_ADDR addr,
159 struct target_ops *targ)
165 no_op_reg_to_regnum (struct gdbarch *gdbarch, int reg)
171 default_elf_make_msymbol_special (asymbol *sym, struct minimal_symbol *msym)
177 default_coff_make_msymbol_special (int val, struct minimal_symbol *msym)
183 cannot_register_not (struct gdbarch *gdbarch, int regnum)
188 /* Legacy version of target_virtual_frame_pointer(). Assumes that
189 there is an gdbarch_deprecated_fp_regnum and that it is the same,
193 legacy_virtual_frame_pointer (struct gdbarch *gdbarch,
196 LONGEST *frame_offset)
198 /* FIXME: cagney/2002-09-13: This code is used when identifying the
199 frame pointer of the current PC. It is assuming that a single
200 register and an offset can determine this. I think it should
201 instead generate a byte code expression as that would work better
202 with things like Dwarf2's CFI. */
203 if (gdbarch_deprecated_fp_regnum (gdbarch) >= 0
204 && gdbarch_deprecated_fp_regnum (gdbarch)
205 < gdbarch_num_regs (gdbarch))
206 *frame_regnum = gdbarch_deprecated_fp_regnum (gdbarch);
207 else if (gdbarch_sp_regnum (gdbarch) >= 0
208 && gdbarch_sp_regnum (gdbarch)
209 < gdbarch_num_regs (gdbarch))
210 *frame_regnum = gdbarch_sp_regnum (gdbarch);
212 /* Should this be an internal error? I guess so, it is reflecting
213 an architectural limitation in the current design. */
214 internal_error (__FILE__, __LINE__,
215 _("No virtual frame pointer available"));
221 generic_convert_register_p (struct gdbarch *gdbarch, int regnum,
228 default_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
234 generic_instruction_nullified (struct gdbarch *gdbarch,
235 struct regcache *regcache)
241 default_remote_register_number (struct gdbarch *gdbarch,
248 /* Functions to manipulate the endianness of the target. */
250 static int target_byte_order_user = BFD_ENDIAN_UNKNOWN;
252 static const char endian_big[] = "big";
253 static const char endian_little[] = "little";
254 static const char endian_auto[] = "auto";
255 static const char *const endian_enum[] =
262 static const char *set_endian_string;
265 selected_byte_order (void)
267 return target_byte_order_user;
270 /* Called by ``show endian''. */
273 show_endian (struct ui_file *file, int from_tty, struct cmd_list_element *c,
276 if (target_byte_order_user == BFD_ENDIAN_UNKNOWN)
277 if (gdbarch_byte_order (get_current_arch ()) == BFD_ENDIAN_BIG)
278 fprintf_unfiltered (file, _("The target endianness is set automatically "
279 "(currently big endian)\n"));
281 fprintf_unfiltered (file, _("The target endianness is set automatically "
282 "(currently little endian)\n"));
284 if (target_byte_order_user == BFD_ENDIAN_BIG)
285 fprintf_unfiltered (file,
286 _("The target is assumed to be big endian\n"));
288 fprintf_unfiltered (file,
289 _("The target is assumed to be little endian\n"));
293 set_endian (char *ignore_args, int from_tty, struct cmd_list_element *c)
295 struct gdbarch_info info;
297 gdbarch_info_init (&info);
299 if (set_endian_string == endian_auto)
301 target_byte_order_user = BFD_ENDIAN_UNKNOWN;
302 if (! gdbarch_update_p (info))
303 internal_error (__FILE__, __LINE__,
304 _("set_endian: architecture update failed"));
306 else if (set_endian_string == endian_little)
308 info.byte_order = BFD_ENDIAN_LITTLE;
309 if (! gdbarch_update_p (info))
310 printf_unfiltered (_("Little endian target not supported by GDB\n"));
312 target_byte_order_user = BFD_ENDIAN_LITTLE;
314 else if (set_endian_string == endian_big)
316 info.byte_order = BFD_ENDIAN_BIG;
317 if (! gdbarch_update_p (info))
318 printf_unfiltered (_("Big endian target not supported by GDB\n"));
320 target_byte_order_user = BFD_ENDIAN_BIG;
323 internal_error (__FILE__, __LINE__,
324 _("set_endian: bad value"));
326 show_endian (gdb_stdout, from_tty, NULL, NULL);
329 /* Given SELECTED, a currently selected BFD architecture, and
330 TARGET_DESC, the current target description, return what
333 SELECTED may be NULL, in which case we return the architecture
334 associated with TARGET_DESC. If SELECTED specifies a variant
335 of the architecture associtated with TARGET_DESC, return the
336 more specific of the two.
338 If SELECTED is a different architecture, but it is accepted as
339 compatible by the target, we can use the target architecture.
341 If SELECTED is obviously incompatible, warn the user. */
343 static const struct bfd_arch_info *
344 choose_architecture_for_target (const struct target_desc *target_desc,
345 const struct bfd_arch_info *selected)
347 const struct bfd_arch_info *from_target = tdesc_architecture (target_desc);
348 const struct bfd_arch_info *compat1, *compat2;
350 if (selected == NULL)
353 if (from_target == NULL)
356 /* struct bfd_arch_info objects are singletons: that is, there's
357 supposed to be exactly one instance for a given machine. So you
358 can tell whether two are equivalent by comparing pointers. */
359 if (from_target == selected)
362 /* BFD's 'A->compatible (A, B)' functions return zero if A and B are
363 incompatible. But if they are compatible, it returns the 'more
364 featureful' of the two arches. That is, if A can run code
365 written for B, but B can't run code written for A, then it'll
368 Some targets (e.g. MIPS as of 2006-12-04) don't fully
369 implement this, instead always returning NULL or the first
370 argument. We detect that case by checking both directions. */
372 compat1 = selected->compatible (selected, from_target);
373 compat2 = from_target->compatible (from_target, selected);
375 if (compat1 == NULL && compat2 == NULL)
377 /* BFD considers the architectures incompatible. Check our
378 target description whether it accepts SELECTED as compatible
380 if (tdesc_compatible_p (target_desc, selected))
383 warning (_("Selected architecture %s is not compatible "
384 "with reported target architecture %s"),
385 selected->printable_name, from_target->printable_name);
393 if (compat1 == compat2)
396 /* If the two didn't match, but one of them was a default
397 architecture, assume the more specific one is correct. This
398 handles the case where an executable or target description just
399 says "mips", but the other knows which MIPS variant. */
400 if (compat1->the_default)
402 if (compat2->the_default)
405 /* We have no idea which one is better. This is a bug, but not
406 a critical problem; warn the user. */
407 warning (_("Selected architecture %s is ambiguous with "
408 "reported target architecture %s"),
409 selected->printable_name, from_target->printable_name);
413 /* Functions to manipulate the architecture of the target. */
415 enum set_arch { set_arch_auto, set_arch_manual };
417 static const struct bfd_arch_info *target_architecture_user;
419 static const char *set_architecture_string;
422 selected_architecture_name (void)
424 if (target_architecture_user == NULL)
427 return set_architecture_string;
430 /* Called if the user enters ``show architecture'' without an
434 show_architecture (struct ui_file *file, int from_tty,
435 struct cmd_list_element *c, const char *value)
437 if (target_architecture_user == NULL)
438 fprintf_filtered (file, _("The target architecture is set "
439 "automatically (currently %s)\n"),
440 gdbarch_bfd_arch_info (get_current_arch ())->printable_name);
442 fprintf_filtered (file, _("The target architecture is assumed to be %s\n"),
443 set_architecture_string);
447 /* Called if the user enters ``set architecture'' with or without an
451 set_architecture (char *ignore_args, int from_tty, struct cmd_list_element *c)
453 struct gdbarch_info info;
455 gdbarch_info_init (&info);
457 if (strcmp (set_architecture_string, "auto") == 0)
459 target_architecture_user = NULL;
460 if (!gdbarch_update_p (info))
461 internal_error (__FILE__, __LINE__,
462 _("could not select an architecture automatically"));
466 info.bfd_arch_info = bfd_scan_arch (set_architecture_string);
467 if (info.bfd_arch_info == NULL)
468 internal_error (__FILE__, __LINE__,
469 _("set_architecture: bfd_scan_arch failed"));
470 if (gdbarch_update_p (info))
471 target_architecture_user = info.bfd_arch_info;
473 printf_unfiltered (_("Architecture `%s' not recognized.\n"),
474 set_architecture_string);
476 show_architecture (gdb_stdout, from_tty, NULL, NULL);
479 /* Try to select a global architecture that matches "info". Return
480 non-zero if the attempt succeds. */
482 gdbarch_update_p (struct gdbarch_info info)
484 struct gdbarch *new_gdbarch;
486 /* Check for the current file. */
487 if (info.abfd == NULL)
488 info.abfd = exec_bfd;
489 if (info.abfd == NULL)
490 info.abfd = core_bfd;
492 /* Check for the current target description. */
493 if (info.target_desc == NULL)
494 info.target_desc = target_current_description ();
496 new_gdbarch = gdbarch_find_by_info (info);
498 /* If there no architecture by that name, reject the request. */
499 if (new_gdbarch == NULL)
502 fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
503 "Architecture not found\n");
507 /* If it is the same old architecture, accept the request (but don't
509 if (new_gdbarch == target_gdbarch ())
512 fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
513 "Architecture %s (%s) unchanged\n",
514 host_address_to_string (new_gdbarch),
515 gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
519 /* It's a new architecture, swap it in. */
521 fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
522 "New architecture %s (%s) selected\n",
523 host_address_to_string (new_gdbarch),
524 gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
525 set_target_gdbarch (new_gdbarch);
530 /* Return the architecture for ABFD. If no suitable architecture
531 could be find, return NULL. */
534 gdbarch_from_bfd (bfd *abfd)
536 struct gdbarch_info info;
537 gdbarch_info_init (&info);
540 return gdbarch_find_by_info (info);
543 /* Set the dynamic target-system-dependent parameters (architecture,
544 byte-order) using information found in the BFD */
547 set_gdbarch_from_file (bfd *abfd)
549 struct gdbarch_info info;
550 struct gdbarch *gdbarch;
552 gdbarch_info_init (&info);
554 info.target_desc = target_current_description ();
555 gdbarch = gdbarch_find_by_info (info);
558 error (_("Architecture of file not recognized."));
559 set_target_gdbarch (gdbarch);
562 /* Initialize the current architecture. Update the ``set
563 architecture'' command so that it specifies a list of valid
566 #ifdef DEFAULT_BFD_ARCH
567 extern const bfd_arch_info_type DEFAULT_BFD_ARCH;
568 static const bfd_arch_info_type *default_bfd_arch = &DEFAULT_BFD_ARCH;
570 static const bfd_arch_info_type *default_bfd_arch;
573 #ifdef DEFAULT_BFD_VEC
574 extern const bfd_target DEFAULT_BFD_VEC;
575 static const bfd_target *default_bfd_vec = &DEFAULT_BFD_VEC;
577 static const bfd_target *default_bfd_vec;
580 static int default_byte_order = BFD_ENDIAN_UNKNOWN;
583 initialize_current_architecture (void)
585 const char **arches = gdbarch_printable_names ();
586 struct gdbarch_info info;
588 /* determine a default architecture and byte order. */
589 gdbarch_info_init (&info);
591 /* Find a default architecture. */
592 if (default_bfd_arch == NULL)
594 /* Choose the architecture by taking the first one
596 const char *chosen = arches[0];
598 for (arch = arches; *arch != NULL; arch++)
600 if (strcmp (*arch, chosen) < 0)
604 internal_error (__FILE__, __LINE__,
605 _("initialize_current_architecture: No arch"));
606 default_bfd_arch = bfd_scan_arch (chosen);
607 if (default_bfd_arch == NULL)
608 internal_error (__FILE__, __LINE__,
609 _("initialize_current_architecture: Arch not found"));
612 info.bfd_arch_info = default_bfd_arch;
614 /* Take several guesses at a byte order. */
615 if (default_byte_order == BFD_ENDIAN_UNKNOWN
616 && default_bfd_vec != NULL)
618 /* Extract BFD's default vector's byte order. */
619 switch (default_bfd_vec->byteorder)
622 default_byte_order = BFD_ENDIAN_BIG;
624 case BFD_ENDIAN_LITTLE:
625 default_byte_order = BFD_ENDIAN_LITTLE;
631 if (default_byte_order == BFD_ENDIAN_UNKNOWN)
633 /* look for ``*el-*'' in the target name. */
635 chp = strchr (target_name, '-');
637 && chp - 2 >= target_name
638 && strncmp (chp - 2, "el", 2) == 0)
639 default_byte_order = BFD_ENDIAN_LITTLE;
641 if (default_byte_order == BFD_ENDIAN_UNKNOWN)
643 /* Wire it to big-endian!!! */
644 default_byte_order = BFD_ENDIAN_BIG;
647 info.byte_order = default_byte_order;
648 info.byte_order_for_code = info.byte_order;
650 if (! gdbarch_update_p (info))
651 internal_error (__FILE__, __LINE__,
652 _("initialize_current_architecture: Selection of "
653 "initial architecture failed"));
655 /* Create the ``set architecture'' command appending ``auto'' to the
656 list of architectures. */
658 /* Append ``auto''. */
660 for (nr = 0; arches[nr] != NULL; nr++);
661 arches = xrealloc (arches, sizeof (char*) * (nr + 2));
662 arches[nr + 0] = "auto";
663 arches[nr + 1] = NULL;
664 add_setshow_enum_cmd ("architecture", class_support,
665 arches, &set_architecture_string,
666 _("Set architecture of target."),
667 _("Show architecture of target."), NULL,
668 set_architecture, show_architecture,
669 &setlist, &showlist);
670 add_alias_cmd ("processor", "architecture", class_support, 1, &setlist);
675 /* Initialize a gdbarch info to values that will be automatically
676 overridden. Note: Originally, this ``struct info'' was initialized
677 using memset(0). Unfortunately, that ran into problems, namely
678 BFD_ENDIAN_BIG is zero. An explicit initialization function that
679 can explicitly set each field to a well defined value is used. */
682 gdbarch_info_init (struct gdbarch_info *info)
684 memset (info, 0, sizeof (struct gdbarch_info));
685 info->byte_order = BFD_ENDIAN_UNKNOWN;
686 info->byte_order_for_code = info->byte_order;
687 info->osabi = GDB_OSABI_UNINITIALIZED;
690 /* Similar to init, but this time fill in the blanks. Information is
691 obtained from the global "set ..." options and explicitly
692 initialized INFO fields. */
695 gdbarch_info_fill (struct gdbarch_info *info)
697 /* "(gdb) set architecture ...". */
698 if (info->bfd_arch_info == NULL
699 && target_architecture_user)
700 info->bfd_arch_info = target_architecture_user;
702 if (info->bfd_arch_info == NULL
703 && info->abfd != NULL
704 && bfd_get_arch (info->abfd) != bfd_arch_unknown
705 && bfd_get_arch (info->abfd) != bfd_arch_obscure)
706 info->bfd_arch_info = bfd_get_arch_info (info->abfd);
707 /* From the target. */
708 if (info->target_desc != NULL)
709 info->bfd_arch_info = choose_architecture_for_target
710 (info->target_desc, info->bfd_arch_info);
711 /* From the default. */
712 if (info->bfd_arch_info == NULL)
713 info->bfd_arch_info = default_bfd_arch;
715 /* "(gdb) set byte-order ...". */
716 if (info->byte_order == BFD_ENDIAN_UNKNOWN
717 && target_byte_order_user != BFD_ENDIAN_UNKNOWN)
718 info->byte_order = target_byte_order_user;
719 /* From the INFO struct. */
720 if (info->byte_order == BFD_ENDIAN_UNKNOWN
721 && info->abfd != NULL)
722 info->byte_order = (bfd_big_endian (info->abfd) ? BFD_ENDIAN_BIG
723 : bfd_little_endian (info->abfd) ? BFD_ENDIAN_LITTLE
724 : BFD_ENDIAN_UNKNOWN);
725 /* From the default. */
726 if (info->byte_order == BFD_ENDIAN_UNKNOWN)
727 info->byte_order = default_byte_order;
728 info->byte_order_for_code = info->byte_order;
730 /* "(gdb) set osabi ...". Handled by gdbarch_lookup_osabi. */
731 /* From the manual override, or from file. */
732 if (info->osabi == GDB_OSABI_UNINITIALIZED)
733 info->osabi = gdbarch_lookup_osabi (info->abfd);
734 /* From the target. */
735 if (info->osabi == GDB_OSABI_UNKNOWN && info->target_desc != NULL)
736 info->osabi = tdesc_osabi (info->target_desc);
737 /* From the configured default. */
738 #ifdef GDB_OSABI_DEFAULT
739 if (info->osabi == GDB_OSABI_UNKNOWN)
740 info->osabi = GDB_OSABI_DEFAULT;
743 /* Must have at least filled in the architecture. */
744 gdb_assert (info->bfd_arch_info != NULL);
747 /* Return "current" architecture. If the target is running, this is
748 the architecture of the selected frame. Otherwise, the "current"
749 architecture defaults to the target architecture.
751 This function should normally be called solely by the command
752 interpreter routines to determine the architecture to execute a
755 get_current_arch (void)
757 if (has_stack_frames ())
758 return get_frame_arch (get_selected_frame (NULL));
760 return target_gdbarch ();
764 default_has_shared_address_space (struct gdbarch *gdbarch)
766 /* Simply say no. In most unix-like targets each inferior/process
767 has its own address space. */
772 default_fast_tracepoint_valid_at (struct gdbarch *gdbarch,
773 CORE_ADDR addr, int *isize, char **msg)
775 /* We don't know if maybe the target has some way to do fast
776 tracepoints that doesn't need gdbarch, so always say yes. */
783 default_remote_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
786 gdbarch_breakpoint_from_pc (gdbarch, pcptr, kindptr);
790 default_gen_return_address (struct gdbarch *gdbarch,
791 struct agent_expr *ax, struct axs_value *value,
794 error (_("This architecture has no method to collect a return address."));
798 default_return_in_first_hidden_param_p (struct gdbarch *gdbarch,
801 /* Usually, the return value's address is stored the in the "first hidden"
802 parameter if the return value should be passed by reference, as
804 return language_pass_by_reference (type);
809 /* -Wmissing-prototypes */
810 extern initialize_file_ftype _initialize_gdbarch_utils;
813 _initialize_gdbarch_utils (void)
815 add_setshow_enum_cmd ("endian", class_support,
816 endian_enum, &set_endian_string,
817 _("Set endianness of target."),
818 _("Show endianness of target."),
819 NULL, set_endian, show_endian,
820 &setlist, &showlist);