1 /* Dynamic architecture support for GDB, the GNU debugger.
3 Copyright (C) 1998-2015 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. */
28 #include "sim-regno.h"
31 #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_coff_make_msymbol_special (int val, struct minimal_symbol *msym)
176 /* See arch-utils.h. */
179 default_make_symbol_special (struct symbol *sym, struct objfile *objfile)
184 /* See arch-utils.h. */
187 default_adjust_dwarf2_addr (CORE_ADDR pc)
192 /* See arch-utils.h. */
195 default_adjust_dwarf2_line (CORE_ADDR addr, int rel)
201 cannot_register_not (struct gdbarch *gdbarch, int regnum)
206 /* Legacy version of target_virtual_frame_pointer(). Assumes that
207 there is an gdbarch_deprecated_fp_regnum and that it is the same,
211 legacy_virtual_frame_pointer (struct gdbarch *gdbarch,
214 LONGEST *frame_offset)
216 /* FIXME: cagney/2002-09-13: This code is used when identifying the
217 frame pointer of the current PC. It is assuming that a single
218 register and an offset can determine this. I think it should
219 instead generate a byte code expression as that would work better
220 with things like Dwarf2's CFI. */
221 if (gdbarch_deprecated_fp_regnum (gdbarch) >= 0
222 && gdbarch_deprecated_fp_regnum (gdbarch)
223 < gdbarch_num_regs (gdbarch))
224 *frame_regnum = gdbarch_deprecated_fp_regnum (gdbarch);
225 else if (gdbarch_sp_regnum (gdbarch) >= 0
226 && gdbarch_sp_regnum (gdbarch)
227 < gdbarch_num_regs (gdbarch))
228 *frame_regnum = gdbarch_sp_regnum (gdbarch);
230 /* Should this be an internal error? I guess so, it is reflecting
231 an architectural limitation in the current design. */
232 internal_error (__FILE__, __LINE__,
233 _("No virtual frame pointer available"));
239 generic_convert_register_p (struct gdbarch *gdbarch, int regnum,
246 default_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
252 generic_instruction_nullified (struct gdbarch *gdbarch,
253 struct regcache *regcache)
259 default_remote_register_number (struct gdbarch *gdbarch,
265 /* See arch-utils.h. */
268 default_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range)
274 /* Functions to manipulate the endianness of the target. */
276 static int target_byte_order_user = BFD_ENDIAN_UNKNOWN;
278 static const char endian_big[] = "big";
279 static const char endian_little[] = "little";
280 static const char endian_auto[] = "auto";
281 static const char *const endian_enum[] =
288 static const char *set_endian_string;
291 selected_byte_order (void)
293 return target_byte_order_user;
296 /* Called by ``show endian''. */
299 show_endian (struct ui_file *file, int from_tty, struct cmd_list_element *c,
302 if (target_byte_order_user == BFD_ENDIAN_UNKNOWN)
303 if (gdbarch_byte_order (get_current_arch ()) == BFD_ENDIAN_BIG)
304 fprintf_unfiltered (file, _("The target endianness is set automatically "
305 "(currently big endian)\n"));
307 fprintf_unfiltered (file, _("The target endianness is set automatically "
308 "(currently little endian)\n"));
310 if (target_byte_order_user == BFD_ENDIAN_BIG)
311 fprintf_unfiltered (file,
312 _("The target is assumed to be big endian\n"));
314 fprintf_unfiltered (file,
315 _("The target is assumed to be little endian\n"));
319 set_endian (char *ignore_args, int from_tty, struct cmd_list_element *c)
321 struct gdbarch_info info;
323 gdbarch_info_init (&info);
325 if (set_endian_string == endian_auto)
327 target_byte_order_user = BFD_ENDIAN_UNKNOWN;
328 if (! gdbarch_update_p (info))
329 internal_error (__FILE__, __LINE__,
330 _("set_endian: architecture update failed"));
332 else if (set_endian_string == endian_little)
334 info.byte_order = BFD_ENDIAN_LITTLE;
335 if (! gdbarch_update_p (info))
336 printf_unfiltered (_("Little endian target not supported by GDB\n"));
338 target_byte_order_user = BFD_ENDIAN_LITTLE;
340 else if (set_endian_string == endian_big)
342 info.byte_order = BFD_ENDIAN_BIG;
343 if (! gdbarch_update_p (info))
344 printf_unfiltered (_("Big endian target not supported by GDB\n"));
346 target_byte_order_user = BFD_ENDIAN_BIG;
349 internal_error (__FILE__, __LINE__,
350 _("set_endian: bad value"));
352 show_endian (gdb_stdout, from_tty, NULL, NULL);
355 /* Given SELECTED, a currently selected BFD architecture, and
356 TARGET_DESC, the current target description, return what
359 SELECTED may be NULL, in which case we return the architecture
360 associated with TARGET_DESC. If SELECTED specifies a variant
361 of the architecture associtated with TARGET_DESC, return the
362 more specific of the two.
364 If SELECTED is a different architecture, but it is accepted as
365 compatible by the target, we can use the target architecture.
367 If SELECTED is obviously incompatible, warn the user. */
369 static const struct bfd_arch_info *
370 choose_architecture_for_target (const struct target_desc *target_desc,
371 const struct bfd_arch_info *selected)
373 const struct bfd_arch_info *from_target = tdesc_architecture (target_desc);
374 const struct bfd_arch_info *compat1, *compat2;
376 if (selected == NULL)
379 if (from_target == NULL)
382 /* struct bfd_arch_info objects are singletons: that is, there's
383 supposed to be exactly one instance for a given machine. So you
384 can tell whether two are equivalent by comparing pointers. */
385 if (from_target == selected)
388 /* BFD's 'A->compatible (A, B)' functions return zero if A and B are
389 incompatible. But if they are compatible, it returns the 'more
390 featureful' of the two arches. That is, if A can run code
391 written for B, but B can't run code written for A, then it'll
394 Some targets (e.g. MIPS as of 2006-12-04) don't fully
395 implement this, instead always returning NULL or the first
396 argument. We detect that case by checking both directions. */
398 compat1 = selected->compatible (selected, from_target);
399 compat2 = from_target->compatible (from_target, selected);
401 if (compat1 == NULL && compat2 == NULL)
403 /* BFD considers the architectures incompatible. Check our
404 target description whether it accepts SELECTED as compatible
406 if (tdesc_compatible_p (target_desc, selected))
409 warning (_("Selected architecture %s is not compatible "
410 "with reported target architecture %s"),
411 selected->printable_name, from_target->printable_name);
419 if (compat1 == compat2)
422 /* If the two didn't match, but one of them was a default
423 architecture, assume the more specific one is correct. This
424 handles the case where an executable or target description just
425 says "mips", but the other knows which MIPS variant. */
426 if (compat1->the_default)
428 if (compat2->the_default)
431 /* We have no idea which one is better. This is a bug, but not
432 a critical problem; warn the user. */
433 warning (_("Selected architecture %s is ambiguous with "
434 "reported target architecture %s"),
435 selected->printable_name, from_target->printable_name);
439 /* Functions to manipulate the architecture of the target. */
441 enum set_arch { set_arch_auto, set_arch_manual };
443 static const struct bfd_arch_info *target_architecture_user;
445 static const char *set_architecture_string;
448 selected_architecture_name (void)
450 if (target_architecture_user == NULL)
453 return set_architecture_string;
456 /* Called if the user enters ``show architecture'' without an
460 show_architecture (struct ui_file *file, int from_tty,
461 struct cmd_list_element *c, const char *value)
463 if (target_architecture_user == NULL)
464 fprintf_filtered (file, _("The target architecture is set "
465 "automatically (currently %s)\n"),
466 gdbarch_bfd_arch_info (get_current_arch ())->printable_name);
468 fprintf_filtered (file, _("The target architecture is assumed to be %s\n"),
469 set_architecture_string);
473 /* Called if the user enters ``set architecture'' with or without an
477 set_architecture (char *ignore_args, int from_tty, struct cmd_list_element *c)
479 struct gdbarch_info info;
481 gdbarch_info_init (&info);
483 if (strcmp (set_architecture_string, "auto") == 0)
485 target_architecture_user = NULL;
486 if (!gdbarch_update_p (info))
487 internal_error (__FILE__, __LINE__,
488 _("could not select an architecture automatically"));
492 info.bfd_arch_info = bfd_scan_arch (set_architecture_string);
493 if (info.bfd_arch_info == NULL)
494 internal_error (__FILE__, __LINE__,
495 _("set_architecture: bfd_scan_arch failed"));
496 if (gdbarch_update_p (info))
497 target_architecture_user = info.bfd_arch_info;
499 printf_unfiltered (_("Architecture `%s' not recognized.\n"),
500 set_architecture_string);
502 show_architecture (gdb_stdout, from_tty, NULL, NULL);
505 /* Try to select a global architecture that matches "info". Return
506 non-zero if the attempt succeeds. */
508 gdbarch_update_p (struct gdbarch_info info)
510 struct gdbarch *new_gdbarch;
512 /* Check for the current file. */
513 if (info.abfd == NULL)
514 info.abfd = exec_bfd;
515 if (info.abfd == NULL)
516 info.abfd = core_bfd;
518 /* Check for the current target description. */
519 if (info.target_desc == NULL)
520 info.target_desc = target_current_description ();
522 new_gdbarch = gdbarch_find_by_info (info);
524 /* If there no architecture by that name, reject the request. */
525 if (new_gdbarch == NULL)
528 fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
529 "Architecture not found\n");
533 /* If it is the same old architecture, accept the request (but don't
535 if (new_gdbarch == target_gdbarch ())
538 fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
539 "Architecture %s (%s) unchanged\n",
540 host_address_to_string (new_gdbarch),
541 gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
545 /* It's a new architecture, swap it in. */
547 fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
548 "New architecture %s (%s) selected\n",
549 host_address_to_string (new_gdbarch),
550 gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
551 set_target_gdbarch (new_gdbarch);
556 /* Return the architecture for ABFD. If no suitable architecture
557 could be find, return NULL. */
560 gdbarch_from_bfd (bfd *abfd)
562 struct gdbarch_info info;
563 gdbarch_info_init (&info);
566 return gdbarch_find_by_info (info);
569 /* Set the dynamic target-system-dependent parameters (architecture,
570 byte-order) using information found in the BFD */
573 set_gdbarch_from_file (bfd *abfd)
575 struct gdbarch_info info;
576 struct gdbarch *gdbarch;
578 gdbarch_info_init (&info);
580 info.target_desc = target_current_description ();
581 gdbarch = gdbarch_find_by_info (info);
584 error (_("Architecture of file not recognized."));
585 set_target_gdbarch (gdbarch);
588 /* Initialize the current architecture. Update the ``set
589 architecture'' command so that it specifies a list of valid
592 #ifdef DEFAULT_BFD_ARCH
593 extern const bfd_arch_info_type DEFAULT_BFD_ARCH;
594 static const bfd_arch_info_type *default_bfd_arch = &DEFAULT_BFD_ARCH;
596 static const bfd_arch_info_type *default_bfd_arch;
599 #ifdef DEFAULT_BFD_VEC
600 extern const bfd_target DEFAULT_BFD_VEC;
601 static const bfd_target *default_bfd_vec = &DEFAULT_BFD_VEC;
603 static const bfd_target *default_bfd_vec;
606 static int default_byte_order = BFD_ENDIAN_UNKNOWN;
609 initialize_current_architecture (void)
611 const char **arches = gdbarch_printable_names ();
612 struct gdbarch_info info;
614 /* determine a default architecture and byte order. */
615 gdbarch_info_init (&info);
617 /* Find a default architecture. */
618 if (default_bfd_arch == NULL)
620 /* Choose the architecture by taking the first one
622 const char *chosen = arches[0];
624 for (arch = arches; *arch != NULL; arch++)
626 if (strcmp (*arch, chosen) < 0)
630 internal_error (__FILE__, __LINE__,
631 _("initialize_current_architecture: No arch"));
632 default_bfd_arch = bfd_scan_arch (chosen);
633 if (default_bfd_arch == NULL)
634 internal_error (__FILE__, __LINE__,
635 _("initialize_current_architecture: Arch not found"));
638 info.bfd_arch_info = default_bfd_arch;
640 /* Take several guesses at a byte order. */
641 if (default_byte_order == BFD_ENDIAN_UNKNOWN
642 && default_bfd_vec != NULL)
644 /* Extract BFD's default vector's byte order. */
645 switch (default_bfd_vec->byteorder)
648 default_byte_order = BFD_ENDIAN_BIG;
650 case BFD_ENDIAN_LITTLE:
651 default_byte_order = BFD_ENDIAN_LITTLE;
657 if (default_byte_order == BFD_ENDIAN_UNKNOWN)
659 /* look for ``*el-*'' in the target name. */
661 chp = strchr (target_name, '-');
663 && chp - 2 >= target_name
664 && strncmp (chp - 2, "el", 2) == 0)
665 default_byte_order = BFD_ENDIAN_LITTLE;
667 if (default_byte_order == BFD_ENDIAN_UNKNOWN)
669 /* Wire it to big-endian!!! */
670 default_byte_order = BFD_ENDIAN_BIG;
673 info.byte_order = default_byte_order;
674 info.byte_order_for_code = info.byte_order;
676 if (! gdbarch_update_p (info))
677 internal_error (__FILE__, __LINE__,
678 _("initialize_current_architecture: Selection of "
679 "initial architecture failed"));
681 /* Create the ``set architecture'' command appending ``auto'' to the
682 list of architectures. */
684 /* Append ``auto''. */
686 for (nr = 0; arches[nr] != NULL; nr++);
687 arches = xrealloc (arches, sizeof (char*) * (nr + 2));
688 arches[nr + 0] = "auto";
689 arches[nr + 1] = NULL;
690 add_setshow_enum_cmd ("architecture", class_support,
691 arches, &set_architecture_string,
692 _("Set architecture of target."),
693 _("Show architecture of target."), NULL,
694 set_architecture, show_architecture,
695 &setlist, &showlist);
696 add_alias_cmd ("processor", "architecture", class_support, 1, &setlist);
701 /* Initialize a gdbarch info to values that will be automatically
702 overridden. Note: Originally, this ``struct info'' was initialized
703 using memset(0). Unfortunately, that ran into problems, namely
704 BFD_ENDIAN_BIG is zero. An explicit initialization function that
705 can explicitly set each field to a well defined value is used. */
708 gdbarch_info_init (struct gdbarch_info *info)
710 memset (info, 0, sizeof (struct gdbarch_info));
711 info->byte_order = BFD_ENDIAN_UNKNOWN;
712 info->byte_order_for_code = info->byte_order;
713 info->osabi = GDB_OSABI_UNINITIALIZED;
716 /* Similar to init, but this time fill in the blanks. Information is
717 obtained from the global "set ..." options and explicitly
718 initialized INFO fields. */
721 gdbarch_info_fill (struct gdbarch_info *info)
723 /* "(gdb) set architecture ...". */
724 if (info->bfd_arch_info == NULL
725 && target_architecture_user)
726 info->bfd_arch_info = target_architecture_user;
728 if (info->bfd_arch_info == NULL
729 && info->abfd != NULL
730 && bfd_get_arch (info->abfd) != bfd_arch_unknown
731 && bfd_get_arch (info->abfd) != bfd_arch_obscure)
732 info->bfd_arch_info = bfd_get_arch_info (info->abfd);
733 /* From the target. */
734 if (info->target_desc != NULL)
735 info->bfd_arch_info = choose_architecture_for_target
736 (info->target_desc, info->bfd_arch_info);
737 /* From the default. */
738 if (info->bfd_arch_info == NULL)
739 info->bfd_arch_info = default_bfd_arch;
741 /* "(gdb) set byte-order ...". */
742 if (info->byte_order == BFD_ENDIAN_UNKNOWN
743 && target_byte_order_user != BFD_ENDIAN_UNKNOWN)
744 info->byte_order = target_byte_order_user;
745 /* From the INFO struct. */
746 if (info->byte_order == BFD_ENDIAN_UNKNOWN
747 && info->abfd != NULL)
748 info->byte_order = (bfd_big_endian (info->abfd) ? BFD_ENDIAN_BIG
749 : bfd_little_endian (info->abfd) ? BFD_ENDIAN_LITTLE
750 : BFD_ENDIAN_UNKNOWN);
751 /* From the default. */
752 if (info->byte_order == BFD_ENDIAN_UNKNOWN)
753 info->byte_order = default_byte_order;
754 info->byte_order_for_code = info->byte_order;
756 /* "(gdb) set osabi ...". Handled by gdbarch_lookup_osabi. */
757 /* From the manual override, or from file. */
758 if (info->osabi == GDB_OSABI_UNINITIALIZED)
759 info->osabi = gdbarch_lookup_osabi (info->abfd);
760 /* From the target. */
761 if (info->osabi == GDB_OSABI_UNKNOWN && info->target_desc != NULL)
762 info->osabi = tdesc_osabi (info->target_desc);
763 /* From the configured default. */
764 #ifdef GDB_OSABI_DEFAULT
765 if (info->osabi == GDB_OSABI_UNKNOWN)
766 info->osabi = GDB_OSABI_DEFAULT;
769 /* Must have at least filled in the architecture. */
770 gdb_assert (info->bfd_arch_info != NULL);
773 /* Return "current" architecture. If the target is running, this is
774 the architecture of the selected frame. Otherwise, the "current"
775 architecture defaults to the target architecture.
777 This function should normally be called solely by the command
778 interpreter routines to determine the architecture to execute a
781 get_current_arch (void)
783 if (has_stack_frames ())
784 return get_frame_arch (get_selected_frame (NULL));
786 return target_gdbarch ();
790 default_has_shared_address_space (struct gdbarch *gdbarch)
792 /* Simply say no. In most unix-like targets each inferior/process
793 has its own address space. */
798 default_fast_tracepoint_valid_at (struct gdbarch *gdbarch,
799 CORE_ADDR addr, int *isize, char **msg)
801 /* We don't know if maybe the target has some way to do fast
802 tracepoints that doesn't need gdbarch, so always say yes. */
809 default_remote_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
812 gdbarch_breakpoint_from_pc (gdbarch, pcptr, kindptr);
816 default_gen_return_address (struct gdbarch *gdbarch,
817 struct agent_expr *ax, struct axs_value *value,
820 error (_("This architecture has no method to collect a return address."));
824 default_return_in_first_hidden_param_p (struct gdbarch *gdbarch,
827 /* Usually, the return value's address is stored the in the "first hidden"
828 parameter if the return value should be passed by reference, as
830 return language_pass_by_reference (type);
833 int default_insn_is_call (struct gdbarch *gdbarch, CORE_ADDR addr)
838 int default_insn_is_ret (struct gdbarch *gdbarch, CORE_ADDR addr)
843 int default_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
849 default_skip_permanent_breakpoint (struct regcache *regcache)
851 struct gdbarch *gdbarch = get_regcache_arch (regcache);
852 CORE_ADDR current_pc = regcache_read_pc (regcache);
853 const gdb_byte *bp_insn;
856 bp_insn = gdbarch_breakpoint_from_pc (gdbarch, ¤t_pc, &bp_len);
857 current_pc += bp_len;
858 regcache_write_pc (regcache, current_pc);
862 default_infcall_mmap (CORE_ADDR size, unsigned prot)
864 error (_("This target does not support inferior memory allocation by mmap."));
867 /* -mcmodel=large is used so that no GOT (Global Offset Table) is needed to be
868 created in inferior memory by GDB (normally it is set by ld.so). */
871 default_gcc_target_options (struct gdbarch *gdbarch)
873 return xstrprintf ("-m%d%s", gdbarch_ptr_bit (gdbarch),
874 gdbarch_ptr_bit (gdbarch) == 64 ? " -mcmodel=large" : "");
877 /* gdbarch gnu_triplet_regexp method. */
880 default_gnu_triplet_regexp (struct gdbarch *gdbarch)
882 return gdbarch_bfd_arch_info (gdbarch)->arch_name;
885 /* -Wmissing-prototypes */
886 extern initialize_file_ftype _initialize_gdbarch_utils;
889 _initialize_gdbarch_utils (void)
891 add_setshow_enum_cmd ("endian", class_support,
892 endian_enum, &set_endian_string,
893 _("Set endianness of target."),
894 _("Show endianness of target."),
895 NULL, set_endian, show_endian,
896 &setlist, &showlist);