1 /* Dynamic architecture support for GDB, the GNU debugger.
3 Copyright (C) 1998-2023 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"
24 #include "inferior.h" /* enum CALL_DUMMY_LOCATION et al. */
27 #include "sim-regno.h"
30 #include "target-descriptions.h"
34 #include "dummy-frame.h"
35 #include "frame-unwind.h"
36 #include "reggroups.h"
38 #include "observable.h"
39 #include "solib-target.h"
41 #include "gdbsupport/version.h"
43 #include "floatformat.h"
48 default_displaced_step_hw_singlestep (struct gdbarch
*gdbarch
)
50 return !gdbarch_software_single_step_p (gdbarch
);
54 displaced_step_at_entry_point (struct gdbarch
*gdbarch
)
59 addr
= entry_point_address ();
61 /* Inferior calls also use the entry point as a breakpoint location.
62 We don't want displaced stepping to interfere with those
63 breakpoints, so leave space. */
64 gdbarch_breakpoint_from_pc (gdbarch
, &addr
, &bp_len
);
71 legacy_register_sim_regno (struct gdbarch
*gdbarch
, int regnum
)
73 /* Only makes sense to supply raw registers. */
74 gdb_assert (regnum
>= 0 && regnum
< gdbarch_num_regs (gdbarch
));
75 /* NOTE: cagney/2002-05-13: The old code did it this way and it is
76 suspected that some GDB/SIM combinations may rely on this
77 behaviour. The default should be one2one_register_sim_regno
79 if (gdbarch_register_name (gdbarch
, regnum
)[0] != '\0')
82 return LEGACY_SIM_REGNO_IGNORE
;
85 /* See arch-utils.h */
88 default_remove_non_address_bits (struct gdbarch
*gdbarch
, CORE_ADDR pointer
)
90 /* By default, just return the pointer value. */
94 /* See arch-utils.h */
97 default_memtag_to_string (struct gdbarch
*gdbarch
, struct value
*tag
)
99 error (_("This architecture has no method to convert a memory tag to"
103 /* See arch-utils.h */
106 default_tagged_address_p (struct gdbarch
*gdbarch
, struct value
*address
)
108 /* By default, assume the address is untagged. */
112 /* See arch-utils.h */
115 default_memtag_matches_p (struct gdbarch
*gdbarch
, struct value
*address
)
117 /* By default, assume the tags match. */
121 /* See arch-utils.h */
124 default_set_memtags (struct gdbarch
*gdbarch
, struct value
*address
,
125 size_t length
, const gdb::byte_vector
&tags
,
126 memtag_type tag_type
)
128 /* By default, return true (successful); */
132 /* See arch-utils.h */
135 default_get_memtag (struct gdbarch
*gdbarch
, struct value
*address
,
136 memtag_type tag_type
)
138 /* By default, return no tag. */
143 generic_skip_trampoline_code (frame_info_ptr frame
, CORE_ADDR pc
)
149 generic_skip_solib_resolver (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
155 generic_in_solib_return_trampoline (struct gdbarch
*gdbarch
,
156 CORE_ADDR pc
, const char *name
)
162 generic_stack_frame_destroyed_p (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
168 default_code_of_frame_writable (struct gdbarch
*gdbarch
,
169 frame_info_ptr frame
)
174 /* Helper functions for gdbarch_inner_than */
177 core_addr_lessthan (CORE_ADDR lhs
, CORE_ADDR rhs
)
183 core_addr_greaterthan (CORE_ADDR lhs
, CORE_ADDR rhs
)
188 /* Misc helper functions for targets. */
191 core_addr_identity (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
197 convert_from_func_ptr_addr_identity (struct gdbarch
*gdbarch
, CORE_ADDR addr
,
198 struct target_ops
*targ
)
204 no_op_reg_to_regnum (struct gdbarch
*gdbarch
, int reg
)
210 default_coff_make_msymbol_special (int val
, struct minimal_symbol
*msym
)
215 /* See arch-utils.h. */
218 default_make_symbol_special (struct symbol
*sym
, struct objfile
*objfile
)
223 /* See arch-utils.h. */
226 default_adjust_dwarf2_addr (CORE_ADDR pc
)
231 /* See arch-utils.h. */
234 default_adjust_dwarf2_line (CORE_ADDR addr
, int rel
)
239 /* See arch-utils.h. */
242 default_execute_dwarf_cfa_vendor_op (struct gdbarch
*gdbarch
, gdb_byte op
,
243 struct dwarf2_frame_state
*fs
)
249 cannot_register_not (struct gdbarch
*gdbarch
, int regnum
)
254 /* Legacy version of target_virtual_frame_pointer(). Assumes that
255 there is an gdbarch_deprecated_fp_regnum and that it is the same,
259 legacy_virtual_frame_pointer (struct gdbarch
*gdbarch
,
262 LONGEST
*frame_offset
)
264 /* FIXME: cagney/2002-09-13: This code is used when identifying the
265 frame pointer of the current PC. It is assuming that a single
266 register and an offset can determine this. I think it should
267 instead generate a byte code expression as that would work better
268 with things like Dwarf2's CFI. */
269 if (gdbarch_deprecated_fp_regnum (gdbarch
) >= 0
270 && gdbarch_deprecated_fp_regnum (gdbarch
)
271 < gdbarch_num_regs (gdbarch
))
272 *frame_regnum
= gdbarch_deprecated_fp_regnum (gdbarch
);
273 else if (gdbarch_sp_regnum (gdbarch
) >= 0
274 && gdbarch_sp_regnum (gdbarch
)
275 < gdbarch_num_regs (gdbarch
))
276 *frame_regnum
= gdbarch_sp_regnum (gdbarch
);
278 /* Should this be an internal error? I guess so, it is reflecting
279 an architectural limitation in the current design. */
280 internal_error (_("No virtual frame pointer available"));
284 /* Return a floating-point format for a floating-point variable of
285 length LEN in bits. If non-NULL, NAME is the name of its type.
286 If no suitable type is found, return NULL. */
288 const struct floatformat
**
289 default_floatformat_for_type (struct gdbarch
*gdbarch
,
290 const char *name
, int len
)
292 const struct floatformat
**format
= NULL
;
294 /* Check if this is a bfloat16 type. It has the same size as the
295 IEEE half float type, so we use the base type name to tell them
297 if (name
!= nullptr && strcmp (name
, "__bf16") == 0
298 && len
== gdbarch_bfloat16_bit (gdbarch
))
299 format
= gdbarch_bfloat16_format (gdbarch
);
300 else if (len
== gdbarch_half_bit (gdbarch
))
301 format
= gdbarch_half_format (gdbarch
);
302 else if (len
== gdbarch_float_bit (gdbarch
))
303 format
= gdbarch_float_format (gdbarch
);
304 else if (len
== gdbarch_double_bit (gdbarch
))
305 format
= gdbarch_double_format (gdbarch
);
306 else if (len
== gdbarch_long_double_bit (gdbarch
))
307 format
= gdbarch_long_double_format (gdbarch
);
308 /* On i386 the 'long double' type takes 96 bits,
309 while the real number of used bits is only 80,
310 both in processor and in memory.
311 The code below accepts the real bit size. */
312 else if (gdbarch_long_double_format (gdbarch
) != NULL
313 && len
== gdbarch_long_double_format (gdbarch
)[0]->totalsize
)
314 format
= gdbarch_long_double_format (gdbarch
);
320 generic_convert_register_p (struct gdbarch
*gdbarch
, int regnum
,
327 default_stabs_argument_has_addr (struct gdbarch
*gdbarch
, struct type
*type
)
333 generic_instruction_nullified (struct gdbarch
*gdbarch
,
334 struct regcache
*regcache
)
340 default_remote_register_number (struct gdbarch
*gdbarch
,
346 /* See arch-utils.h. */
349 default_vsyscall_range (struct gdbarch
*gdbarch
, struct mem_range
*range
)
355 /* Functions to manipulate the endianness of the target. */
357 static enum bfd_endian target_byte_order_user
= BFD_ENDIAN_UNKNOWN
;
359 static const char endian_big
[] = "big";
360 static const char endian_little
[] = "little";
361 static const char endian_auto
[] = "auto";
362 static const char *const endian_enum
[] =
369 static const char *set_endian_string
= endian_auto
;
372 selected_byte_order (void)
374 return target_byte_order_user
;
377 /* Called by ``show endian''. */
380 show_endian (struct ui_file
*file
, int from_tty
, struct cmd_list_element
*c
,
383 if (target_byte_order_user
== BFD_ENDIAN_UNKNOWN
)
384 if (gdbarch_byte_order (get_current_arch ()) == BFD_ENDIAN_BIG
)
385 gdb_printf (file
, _("The target endianness is set automatically "
386 "(currently big endian).\n"));
388 gdb_printf (file
, _("The target endianness is set automatically "
389 "(currently little endian).\n"));
391 if (target_byte_order_user
== BFD_ENDIAN_BIG
)
393 _("The target is set to big endian.\n"));
396 _("The target is set to little endian.\n"));
400 set_endian (const char *ignore_args
, int from_tty
, struct cmd_list_element
*c
)
402 struct gdbarch_info info
;
404 if (set_endian_string
== endian_auto
)
406 target_byte_order_user
= BFD_ENDIAN_UNKNOWN
;
407 if (! gdbarch_update_p (info
))
408 internal_error (_("set_endian: architecture update failed"));
410 else if (set_endian_string
== endian_little
)
412 info
.byte_order
= BFD_ENDIAN_LITTLE
;
413 if (! gdbarch_update_p (info
))
414 gdb_printf (gdb_stderr
,
415 _("Little endian target not supported by GDB\n"));
417 target_byte_order_user
= BFD_ENDIAN_LITTLE
;
419 else if (set_endian_string
== endian_big
)
421 info
.byte_order
= BFD_ENDIAN_BIG
;
422 if (! gdbarch_update_p (info
))
423 gdb_printf (gdb_stderr
,
424 _("Big endian target not supported by GDB\n"));
426 target_byte_order_user
= BFD_ENDIAN_BIG
;
429 internal_error (_("set_endian: bad value"));
431 show_endian (gdb_stdout
, from_tty
, NULL
, NULL
);
434 /* Given SELECTED, a currently selected BFD architecture, and
435 TARGET_DESC, the current target description, return what
438 SELECTED may be NULL, in which case we return the architecture
439 associated with TARGET_DESC. If SELECTED specifies a variant
440 of the architecture associated with TARGET_DESC, return the
441 more specific of the two.
443 If SELECTED is a different architecture, but it is accepted as
444 compatible by the target, we can use the target architecture.
446 If SELECTED is obviously incompatible, warn the user. */
448 static const struct bfd_arch_info
*
449 choose_architecture_for_target (const struct target_desc
*target_desc
,
450 const struct bfd_arch_info
*selected
)
452 const struct bfd_arch_info
*from_target
= tdesc_architecture (target_desc
);
453 const struct bfd_arch_info
*compat1
, *compat2
;
455 if (selected
== NULL
)
458 if (from_target
== NULL
)
461 /* struct bfd_arch_info objects are singletons: that is, there's
462 supposed to be exactly one instance for a given machine. So you
463 can tell whether two are equivalent by comparing pointers. */
464 if (from_target
== selected
)
467 /* BFD's 'A->compatible (A, B)' functions return zero if A and B are
468 incompatible. But if they are compatible, it returns the 'more
469 featureful' of the two arches. That is, if A can run code
470 written for B, but B can't run code written for A, then it'll
473 Some targets (e.g. MIPS as of 2006-12-04) don't fully
474 implement this, instead always returning NULL or the first
475 argument. We detect that case by checking both directions. */
477 compat1
= selected
->compatible (selected
, from_target
);
478 compat2
= from_target
->compatible (from_target
, selected
);
480 if (compat1
== NULL
&& compat2
== NULL
)
482 /* BFD considers the architectures incompatible. Check our
483 target description whether it accepts SELECTED as compatible
485 if (tdesc_compatible_p (target_desc
, selected
))
488 warning (_("Selected architecture %s is not compatible "
489 "with reported target architecture %s"),
490 selected
->printable_name
, from_target
->printable_name
);
498 if (compat1
== compat2
)
501 /* If the two didn't match, but one of them was a default
502 architecture, assume the more specific one is correct. This
503 handles the case where an executable or target description just
504 says "mips", but the other knows which MIPS variant. */
505 if (compat1
->the_default
)
507 if (compat2
->the_default
)
510 /* We have no idea which one is better. This is a bug, but not
511 a critical problem; warn the user. */
512 warning (_("Selected architecture %s is ambiguous with "
513 "reported target architecture %s"),
514 selected
->printable_name
, from_target
->printable_name
);
518 /* Functions to manipulate the architecture of the target. */
520 enum set_arch
{ set_arch_auto
, set_arch_manual
};
522 static const struct bfd_arch_info
*target_architecture_user
;
524 static const char *set_architecture_string
;
527 selected_architecture_name (void)
529 if (target_architecture_user
== NULL
)
532 return set_architecture_string
;
535 /* Called if the user enters ``show architecture'' without an
539 show_architecture (struct ui_file
*file
, int from_tty
,
540 struct cmd_list_element
*c
, const char *value
)
542 if (target_architecture_user
== NULL
)
543 gdb_printf (file
, _("The target architecture is set to "
544 "\"auto\" (currently \"%s\").\n"),
545 gdbarch_bfd_arch_info (get_current_arch ())->printable_name
);
547 gdb_printf (file
, _("The target architecture is set to \"%s\".\n"),
548 set_architecture_string
);
552 /* Called if the user enters ``set architecture'' with or without an
556 set_architecture (const char *ignore_args
,
557 int from_tty
, struct cmd_list_element
*c
)
559 struct gdbarch_info info
;
561 if (strcmp (set_architecture_string
, "auto") == 0)
563 target_architecture_user
= NULL
;
564 if (!gdbarch_update_p (info
))
565 internal_error (_("could not select an architecture automatically"));
569 info
.bfd_arch_info
= bfd_scan_arch (set_architecture_string
);
570 if (info
.bfd_arch_info
== NULL
)
571 internal_error (_("set_architecture: bfd_scan_arch failed"));
572 if (gdbarch_update_p (info
))
573 target_architecture_user
= info
.bfd_arch_info
;
575 gdb_printf (gdb_stderr
,
576 _("Architecture `%s' not recognized.\n"),
577 set_architecture_string
);
579 show_architecture (gdb_stdout
, from_tty
, NULL
, NULL
);
582 /* Try to select a global architecture that matches "info". Return
583 non-zero if the attempt succeeds. */
585 gdbarch_update_p (struct gdbarch_info info
)
587 struct gdbarch
*new_gdbarch
;
589 /* Check for the current file. */
590 if (info
.abfd
== NULL
)
591 info
.abfd
= current_program_space
->exec_bfd ();
592 if (info
.abfd
== NULL
)
593 info
.abfd
= core_bfd
;
595 /* Check for the current target description. */
596 if (info
.target_desc
== NULL
)
597 info
.target_desc
= target_current_description ();
599 new_gdbarch
= gdbarch_find_by_info (info
);
601 /* If there no architecture by that name, reject the request. */
602 if (new_gdbarch
== NULL
)
605 gdb_printf (gdb_stdlog
, "gdbarch_update_p: "
606 "Architecture not found\n");
610 /* If it is the same old architecture, accept the request (but don't
612 if (new_gdbarch
== target_gdbarch ())
615 gdb_printf (gdb_stdlog
, "gdbarch_update_p: "
616 "Architecture %s (%s) unchanged\n",
617 host_address_to_string (new_gdbarch
),
618 gdbarch_bfd_arch_info (new_gdbarch
)->printable_name
);
622 /* It's a new architecture, swap it in. */
624 gdb_printf (gdb_stdlog
, "gdbarch_update_p: "
625 "New architecture %s (%s) selected\n",
626 host_address_to_string (new_gdbarch
),
627 gdbarch_bfd_arch_info (new_gdbarch
)->printable_name
);
628 set_target_gdbarch (new_gdbarch
);
633 /* Return the architecture for ABFD. If no suitable architecture
634 could be find, return NULL. */
637 gdbarch_from_bfd (bfd
*abfd
)
639 struct gdbarch_info info
;
642 return gdbarch_find_by_info (info
);
645 /* Set the dynamic target-system-dependent parameters (architecture,
646 byte-order) using information found in the BFD */
649 set_gdbarch_from_file (bfd
*abfd
)
651 struct gdbarch_info info
;
652 struct gdbarch
*gdbarch
;
655 info
.target_desc
= target_current_description ();
656 gdbarch
= gdbarch_find_by_info (info
);
659 error (_("Architecture of file not recognized."));
660 set_target_gdbarch (gdbarch
);
663 /* Initialize the current architecture. Update the ``set
664 architecture'' command so that it specifies a list of valid
667 #ifdef DEFAULT_BFD_ARCH
668 extern const bfd_arch_info_type DEFAULT_BFD_ARCH
;
669 static const bfd_arch_info_type
*default_bfd_arch
= &DEFAULT_BFD_ARCH
;
671 static const bfd_arch_info_type
*default_bfd_arch
;
674 #ifdef DEFAULT_BFD_VEC
675 extern const bfd_target DEFAULT_BFD_VEC
;
676 static const bfd_target
*default_bfd_vec
= &DEFAULT_BFD_VEC
;
678 static const bfd_target
*default_bfd_vec
;
681 static enum bfd_endian default_byte_order
= BFD_ENDIAN_UNKNOWN
;
683 /* Printable names of architectures. Used as the enum list of the
684 "set arch" command. */
685 static std::vector
<const char *> arches
;
688 initialize_current_architecture (void)
690 arches
= gdbarch_printable_names ();
692 /* Find a default architecture. */
693 if (default_bfd_arch
== NULL
)
695 /* Choose the architecture by taking the first one
697 const char *chosen
= arches
[0];
699 for (const char *arch
: arches
)
701 if (strcmp (arch
, chosen
) < 0)
706 internal_error (_("initialize_current_architecture: No arch"));
708 default_bfd_arch
= bfd_scan_arch (chosen
);
709 if (default_bfd_arch
== NULL
)
710 internal_error (_("initialize_current_architecture: Arch not found"));
714 info
.bfd_arch_info
= default_bfd_arch
;
716 /* Take several guesses at a byte order. */
717 if (default_byte_order
== BFD_ENDIAN_UNKNOWN
718 && default_bfd_vec
!= NULL
)
720 /* Extract BFD's default vector's byte order. */
721 switch (default_bfd_vec
->byteorder
)
724 default_byte_order
= BFD_ENDIAN_BIG
;
726 case BFD_ENDIAN_LITTLE
:
727 default_byte_order
= BFD_ENDIAN_LITTLE
;
733 if (default_byte_order
== BFD_ENDIAN_UNKNOWN
)
735 /* look for ``*el-*'' in the target name. */
737 chp
= strchr (target_name
, '-');
739 && chp
- 2 >= target_name
740 && startswith (chp
- 2, "el"))
741 default_byte_order
= BFD_ENDIAN_LITTLE
;
743 if (default_byte_order
== BFD_ENDIAN_UNKNOWN
)
745 /* Wire it to big-endian!!! */
746 default_byte_order
= BFD_ENDIAN_BIG
;
749 info
.byte_order
= default_byte_order
;
750 info
.byte_order_for_code
= info
.byte_order
;
752 if (! gdbarch_update_p (info
))
753 internal_error (_("initialize_current_architecture: Selection of "
754 "initial architecture failed"));
756 /* Create the ``set architecture'' command appending ``auto'' to the
757 list of architectures. */
759 /* Append ``auto''. */
760 set_architecture_string
= "auto";
761 arches
.push_back (set_architecture_string
);
762 arches
.push_back (nullptr);
763 set_show_commands architecture_cmds
764 = add_setshow_enum_cmd ("architecture", class_support
,
765 arches
.data (), &set_architecture_string
,
766 _("Set architecture of target."),
767 _("Show architecture of target."), NULL
,
768 set_architecture
, show_architecture
,
769 &setlist
, &showlist
);
770 add_alias_cmd ("processor", architecture_cmds
.set
, class_support
, 1,
775 /* Similar to init, but this time fill in the blanks. Information is
776 obtained from the global "set ..." options and explicitly
777 initialized INFO fields. */
780 gdbarch_info_fill (struct gdbarch_info
*info
)
782 /* "(gdb) set architecture ...". */
783 if (info
->bfd_arch_info
== NULL
784 && target_architecture_user
)
785 info
->bfd_arch_info
= target_architecture_user
;
787 if (info
->bfd_arch_info
== NULL
788 && info
->abfd
!= NULL
789 && bfd_get_arch (info
->abfd
) != bfd_arch_unknown
790 && bfd_get_arch (info
->abfd
) != bfd_arch_obscure
)
791 info
->bfd_arch_info
= bfd_get_arch_info (info
->abfd
);
792 /* From the target. */
793 if (info
->target_desc
!= NULL
)
794 info
->bfd_arch_info
= choose_architecture_for_target
795 (info
->target_desc
, info
->bfd_arch_info
);
796 /* From the default. */
797 if (info
->bfd_arch_info
== NULL
)
798 info
->bfd_arch_info
= default_bfd_arch
;
800 /* "(gdb) set byte-order ...". */
801 if (info
->byte_order
== BFD_ENDIAN_UNKNOWN
802 && target_byte_order_user
!= BFD_ENDIAN_UNKNOWN
)
803 info
->byte_order
= target_byte_order_user
;
804 /* From the INFO struct. */
805 if (info
->byte_order
== BFD_ENDIAN_UNKNOWN
806 && info
->abfd
!= NULL
)
807 info
->byte_order
= (bfd_big_endian (info
->abfd
) ? BFD_ENDIAN_BIG
808 : bfd_little_endian (info
->abfd
) ? BFD_ENDIAN_LITTLE
809 : BFD_ENDIAN_UNKNOWN
);
810 /* From the default. */
811 if (info
->byte_order
== BFD_ENDIAN_UNKNOWN
)
812 info
->byte_order
= default_byte_order
;
813 info
->byte_order_for_code
= info
->byte_order
;
814 /* Wire the default to the last selected byte order. */
815 default_byte_order
= info
->byte_order
;
817 /* "(gdb) set osabi ...". Handled by gdbarch_lookup_osabi. */
818 /* From the manual override, or from file. */
819 if (info
->osabi
== GDB_OSABI_UNKNOWN
)
820 info
->osabi
= gdbarch_lookup_osabi (info
->abfd
);
821 /* From the target. */
823 if (info
->osabi
== GDB_OSABI_UNKNOWN
&& info
->target_desc
!= NULL
)
824 info
->osabi
= tdesc_osabi (info
->target_desc
);
825 /* From the configured default. */
826 #ifdef GDB_OSABI_DEFAULT
827 if (info
->osabi
== GDB_OSABI_UNKNOWN
)
828 info
->osabi
= GDB_OSABI_DEFAULT
;
830 /* If we still don't know which osabi to pick, pick none. */
831 if (info
->osabi
== GDB_OSABI_UNKNOWN
)
832 info
->osabi
= GDB_OSABI_NONE
;
834 /* Must have at least filled in the architecture. */
835 gdb_assert (info
->bfd_arch_info
!= NULL
);
838 /* Return "current" architecture. If the target is running, this is
839 the architecture of the selected frame. Otherwise, the "current"
840 architecture defaults to the target architecture.
842 This function should normally be called solely by the command
843 interpreter routines to determine the architecture to execute a
846 get_current_arch (void)
848 if (has_stack_frames ())
849 return get_frame_arch (get_selected_frame (NULL
));
851 return target_gdbarch ();
855 default_has_shared_address_space (struct gdbarch
*gdbarch
)
857 /* Simply say no. In most unix-like targets each inferior/process
858 has its own address space. */
863 default_fast_tracepoint_valid_at (struct gdbarch
*gdbarch
, CORE_ADDR addr
,
866 /* We don't know if maybe the target has some way to do fast
867 tracepoints that doesn't need gdbarch, so always say yes. */
874 default_breakpoint_from_pc (struct gdbarch
*gdbarch
, CORE_ADDR
*pcptr
,
877 int kind
= gdbarch_breakpoint_kind_from_pc (gdbarch
, pcptr
);
879 return gdbarch_sw_breakpoint_from_kind (gdbarch
, kind
, lenptr
);
882 default_breakpoint_kind_from_current_state (struct gdbarch
*gdbarch
,
883 struct regcache
*regcache
,
886 return gdbarch_breakpoint_kind_from_pc (gdbarch
, pcptr
);
891 default_gen_return_address (struct gdbarch
*gdbarch
,
892 struct agent_expr
*ax
, struct axs_value
*value
,
895 error (_("This architecture has no method to collect a return address."));
899 default_return_in_first_hidden_param_p (struct gdbarch
*gdbarch
,
902 /* Usually, the return value's address is stored the in the "first hidden"
903 parameter if the return value should be passed by reference, as
905 return !(language_pass_by_reference (type
).trivially_copyable
);
908 int default_insn_is_call (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
913 int default_insn_is_ret (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
918 int default_insn_is_jump (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
923 /* See arch-utils.h. */
926 default_program_breakpoint_here_p (struct gdbarch
*gdbarch
,
930 const gdb_byte
*bpoint
= gdbarch_breakpoint_from_pc (gdbarch
, &address
, &len
);
932 /* Software breakpoints unsupported? */
933 if (bpoint
== nullptr)
936 gdb_byte
*target_mem
= (gdb_byte
*) alloca (len
);
938 /* Enable the automatic memory restoration from breakpoints while
939 we read the memory. Otherwise we may find temporary breakpoints, ones
940 inserted by GDB, and flag them as permanent breakpoints. */
941 scoped_restore restore_memory
942 = make_scoped_restore_show_memory_breakpoints (0);
944 if (target_read_memory (address
, target_mem
, len
) == 0)
946 /* Check if this is a breakpoint instruction for this architecture,
947 including ones used by GDB. */
948 if (memcmp (target_mem
, bpoint
, len
) == 0)
956 default_skip_permanent_breakpoint (struct regcache
*regcache
)
958 struct gdbarch
*gdbarch
= regcache
->arch ();
959 CORE_ADDR current_pc
= regcache_read_pc (regcache
);
962 gdbarch_breakpoint_from_pc (gdbarch
, ¤t_pc
, &bp_len
);
963 current_pc
+= bp_len
;
964 regcache_write_pc (regcache
, current_pc
);
968 default_infcall_mmap (CORE_ADDR size
, unsigned prot
)
970 error (_("This target does not support inferior memory allocation by mmap."));
974 default_infcall_munmap (CORE_ADDR addr
, CORE_ADDR size
)
976 /* Memory reserved by inferior mmap is kept leaked. */
979 /* -mcmodel=large is used so that no GOT (Global Offset Table) is needed to be
980 created in inferior memory by GDB (normally it is set by ld.so). */
983 default_gcc_target_options (struct gdbarch
*gdbarch
)
985 return string_printf ("-m%d%s", gdbarch_ptr_bit (gdbarch
),
986 (gdbarch_ptr_bit (gdbarch
) == 64
987 ? " -mcmodel=large" : ""));
990 /* gdbarch gnu_triplet_regexp method. */
993 default_gnu_triplet_regexp (struct gdbarch
*gdbarch
)
995 return gdbarch_bfd_arch_info (gdbarch
)->arch_name
;
998 /* Default method for gdbarch_addressable_memory_unit_size. The default is
999 based on the bits_per_byte defined in the bfd library for the current
1000 architecture, this is usually 8-bits, and so this function will usually
1001 return 1 indicating 1 byte is 1 octet. */
1004 default_addressable_memory_unit_size (struct gdbarch
*gdbarch
)
1006 return gdbarch_bfd_arch_info (gdbarch
)->bits_per_byte
/ 8;
1010 default_guess_tracepoint_registers (struct gdbarch
*gdbarch
,
1011 struct regcache
*regcache
,
1014 int pc_regno
= gdbarch_pc_regnum (gdbarch
);
1017 /* This guessing code below only works if the PC register isn't
1018 a pseudo-register. The value of a pseudo-register isn't stored
1019 in the (non-readonly) regcache -- instead it's recomputed
1020 (probably from some other cached raw register) whenever the
1021 register is read. In this case, a custom method implementation
1022 should be used by the architecture. */
1023 if (pc_regno
< 0 || pc_regno
>= gdbarch_num_regs (gdbarch
))
1026 regs
= (gdb_byte
*) alloca (register_size (gdbarch
, pc_regno
));
1027 store_unsigned_integer (regs
, register_size (gdbarch
, pc_regno
),
1028 gdbarch_byte_order (gdbarch
), addr
);
1029 regcache
->raw_supply (pc_regno
, regs
);
1033 default_print_insn (bfd_vma memaddr
, disassemble_info
*info
)
1035 disassembler_ftype disassemble_fn
;
1037 disassemble_fn
= disassembler (info
->arch
, info
->endian
== BFD_ENDIAN_BIG
,
1038 info
->mach
, current_program_space
->exec_bfd ());
1040 gdb_assert (disassemble_fn
!= NULL
);
1041 return (*disassemble_fn
) (memaddr
, info
);
1044 /* See arch-utils.h. */
1047 gdbarch_skip_prologue_noexcept (gdbarch
*gdbarch
, CORE_ADDR pc
) noexcept
1049 CORE_ADDR new_pc
= pc
;
1053 new_pc
= gdbarch_skip_prologue (gdbarch
, pc
);
1055 catch (const gdb_exception
&ex
)
1061 /* See arch-utils.h. */
1064 default_in_indirect_branch_thunk (gdbarch
*gdbarch
, CORE_ADDR pc
)
1069 /* See arch-utils.h. */
1072 default_type_align (struct gdbarch
*gdbarch
, struct type
*type
)
1077 /* See arch-utils.h. */
1080 default_get_pc_address_flags (frame_info_ptr frame
, CORE_ADDR pc
)
1085 /* See arch-utils.h. */
1087 default_read_core_file_mappings
1088 (struct gdbarch
*gdbarch
,
1090 read_core_file_mappings_pre_loop_ftype pre_loop_cb
,
1091 read_core_file_mappings_loop_ftype loop_cb
)
1096 default_get_return_buf_addr (struct type
*val_type
, frame_info_ptr cur_frame
)
1102 default_dwarf2_omit_typedef_p (struct type
*target_type
, const char *producer
,
1109 default_update_call_site_pc (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
1114 /* Non-zero if we want to trace architecture code. */
1116 #ifndef GDBARCH_DEBUG
1117 #define GDBARCH_DEBUG 0
1119 unsigned int gdbarch_debug
= GDBARCH_DEBUG
;
1121 show_gdbarch_debug (struct ui_file
*file
, int from_tty
,
1122 struct cmd_list_element
*c
, const char *value
)
1124 gdb_printf (file
, _("Architecture debugging is %s.\n"), value
);
1128 pformat (struct gdbarch
*gdbarch
, const struct floatformat
**format
)
1133 int format_index
= gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_LITTLE
? 1 : 0;
1134 return format
[format_index
]->name
;
1138 pstring (const char *string
)
1146 pstring_ptr (char **string
)
1148 if (string
== NULL
|| *string
== NULL
)
1153 /* Helper function to print a list of strings, represented as "const
1154 char *const *". The list is printed comma-separated. */
1157 pstring_list (const char *const *list
)
1159 static char ret
[100];
1160 const char *const *p
;
1167 for (p
= list
; *p
!= NULL
&& offset
< sizeof (ret
); ++p
)
1169 size_t s
= xsnprintf (ret
+ offset
, sizeof (ret
) - offset
, "%s, ", *p
);
1175 gdb_assert (offset
- 2 < sizeof (ret
));
1176 ret
[offset
- 2] = '\0';
1182 #include "gdbarch.c"
1184 enum return_value_convention
1185 default_gdbarch_return_value
1186 (struct gdbarch
*gdbarch
, struct value
*function
, struct type
*valtype
,
1187 struct regcache
*regcache
, struct value
**read_value
,
1188 const gdb_byte
*writebuf
)
1190 gdb_byte
*readbuf
= nullptr;
1192 if (read_value
!= nullptr)
1194 *read_value
= value::allocate (valtype
);
1195 readbuf
= (*read_value
)->contents_raw ().data ();
1198 return gdbarch
->return_value (gdbarch
, function
, valtype
, regcache
,
1202 obstack
*gdbarch_obstack (gdbarch
*arch
)
1204 return &arch
->obstack
;
1207 /* See gdbarch.h. */
1210 gdbarch_obstack_strdup (struct gdbarch
*arch
, const char *string
)
1212 return obstack_strdup (&arch
->obstack
, string
);
1216 /* Free a gdbarch struct. This should never happen in normal
1217 operation --- once you've created a gdbarch, you keep it around.
1218 However, if an architecture's init function encounters an error
1219 building the structure, it may need to clean up a partially
1220 constructed gdbarch. */
1223 gdbarch_free (struct gdbarch
*arch
)
1225 gdb_assert (arch
!= NULL
);
1226 gdb_assert (!arch
->initialized_p
);
1230 /* See gdbarch.h. */
1232 struct gdbarch_tdep_base
*
1233 gdbarch_tdep_1 (struct gdbarch
*gdbarch
)
1235 if (gdbarch_debug
>= 2)
1236 gdb_printf (gdb_stdlog
, "gdbarch_tdep_1 called\n");
1237 return gdbarch
->tdep
.get ();
1241 registry_accessor
<gdbarch
>::get (gdbarch
*arch
)
1243 return &arch
->registry_fields
;
1246 /* Keep a registry of the architectures known by GDB. */
1248 struct gdbarch_registration
1250 enum bfd_architecture bfd_architecture
;
1251 gdbarch_init_ftype
*init
;
1252 gdbarch_dump_tdep_ftype
*dump_tdep
;
1253 gdbarch_supports_arch_info_ftype
*supports_arch_info
;
1254 struct gdbarch_list
*arches
;
1255 struct gdbarch_registration
*next
;
1258 static struct gdbarch_registration
*gdbarch_registry
= NULL
;
1260 std::vector
<const char *>
1261 gdbarch_printable_names ()
1263 /* Accumulate a list of names based on the registed list of
1265 std::vector
<const char *> arches
;
1267 for (gdbarch_registration
*rego
= gdbarch_registry
;
1271 const struct bfd_arch_info
*ap
1272 = bfd_lookup_arch (rego
->bfd_architecture
, 0);
1274 internal_error (_("gdbarch_architecture_names: multi-arch unknown"));
1277 if (rego
->supports_arch_info
== nullptr
1278 || rego
->supports_arch_info (ap
))
1279 arches
.push_back (ap
->printable_name
);
1290 gdbarch_register (enum bfd_architecture bfd_architecture
,
1291 gdbarch_init_ftype
*init
,
1292 gdbarch_dump_tdep_ftype
*dump_tdep
,
1293 gdbarch_supports_arch_info_ftype
*supports_arch_info
)
1295 struct gdbarch_registration
**curr
;
1296 const struct bfd_arch_info
*bfd_arch_info
;
1298 /* Check that BFD recognizes this architecture */
1299 bfd_arch_info
= bfd_lookup_arch (bfd_architecture
, 0);
1300 if (bfd_arch_info
== NULL
)
1302 internal_error (_("gdbarch: Attempt to register "
1303 "unknown architecture (%d)"),
1306 /* Check that we haven't seen this architecture before. */
1307 for (curr
= &gdbarch_registry
;
1309 curr
= &(*curr
)->next
)
1311 if (bfd_architecture
== (*curr
)->bfd_architecture
)
1312 internal_error (_("gdbarch: Duplicate registration "
1313 "of architecture (%s)"),
1314 bfd_arch_info
->printable_name
);
1318 gdb_printf (gdb_stdlog
, "gdbarch_register (%s, %s)\n",
1319 bfd_arch_info
->printable_name
,
1320 host_address_to_string (init
));
1322 (*curr
) = XNEW (struct gdbarch_registration
);
1323 (*curr
)->bfd_architecture
= bfd_architecture
;
1324 (*curr
)->init
= init
;
1325 (*curr
)->dump_tdep
= dump_tdep
;
1326 (*curr
)->supports_arch_info
= supports_arch_info
;
1327 (*curr
)->arches
= NULL
;
1328 (*curr
)->next
= NULL
;
1331 /* Look for an architecture using gdbarch_info. */
1333 struct gdbarch_list
*
1334 gdbarch_list_lookup_by_info (struct gdbarch_list
*arches
,
1335 const struct gdbarch_info
*info
)
1337 for (; arches
!= NULL
; arches
= arches
->next
)
1339 if (info
->bfd_arch_info
!= arches
->gdbarch
->bfd_arch_info
)
1341 if (info
->byte_order
!= arches
->gdbarch
->byte_order
)
1343 if (info
->osabi
!= arches
->gdbarch
->osabi
)
1345 if (info
->target_desc
!= arches
->gdbarch
->target_desc
)
1353 /* Find an architecture that matches the specified INFO. Create a new
1354 architecture if needed. Return that new architecture. */
1357 gdbarch_find_by_info (struct gdbarch_info info
)
1359 struct gdbarch
*new_gdbarch
;
1360 struct gdbarch_registration
*rego
;
1362 /* Fill in missing parts of the INFO struct using a number of
1363 sources: "set ..."; INFOabfd supplied; and the global
1365 gdbarch_info_fill (&info
);
1367 /* Must have found some sort of architecture. */
1368 gdb_assert (info
.bfd_arch_info
!= nullptr);
1372 gdb_printf (gdb_stdlog
,
1373 "gdbarch_find_by_info: info.bfd_arch_info %s\n",
1374 (info
.bfd_arch_info
!= nullptr
1375 ? info
.bfd_arch_info
->printable_name
1377 gdb_printf (gdb_stdlog
,
1378 "gdbarch_find_by_info: info.byte_order %d (%s)\n",
1380 (info
.byte_order
== BFD_ENDIAN_BIG
? "big"
1381 : info
.byte_order
== BFD_ENDIAN_LITTLE
? "little"
1383 gdb_printf (gdb_stdlog
,
1384 "gdbarch_find_by_info: info.osabi %d (%s)\n",
1385 info
.osabi
, gdbarch_osabi_name (info
.osabi
));
1386 gdb_printf (gdb_stdlog
,
1387 "gdbarch_find_by_info: info.abfd %s\n",
1388 host_address_to_string (info
.abfd
));
1391 /* Find the tdep code that knows about this architecture. */
1392 for (rego
= gdbarch_registry
;
1395 if (rego
->bfd_architecture
== info
.bfd_arch_info
->arch
)
1397 if (rego
== nullptr)
1400 gdb_printf (gdb_stdlog
, "gdbarch_find_by_info: "
1401 "No matching architecture\n");
1405 /* Ask the tdep code for an architecture that matches "info". */
1406 new_gdbarch
= rego
->init (info
, rego
->arches
);
1408 /* Did the tdep code like it? No. Reject the change and revert to
1409 the old architecture. */
1410 if (new_gdbarch
== nullptr)
1413 gdb_printf (gdb_stdlog
, "gdbarch_find_by_info: "
1414 "Target rejected architecture\n");
1418 /* Is this a pre-existing architecture (as determined by already
1419 being initialized)? Move it to the front of the architecture
1420 list (keeping the list sorted Most Recently Used). */
1421 if (new_gdbarch
->initialized_p
)
1423 struct gdbarch_list
**list
;
1424 struct gdbarch_list
*self
;
1426 gdb_printf (gdb_stdlog
, "gdbarch_find_by_info: "
1427 "Previous architecture %s (%s) selected\n",
1428 host_address_to_string (new_gdbarch
),
1429 new_gdbarch
->bfd_arch_info
->printable_name
);
1430 /* Find the existing arch in the list. */
1431 for (list
= ®o
->arches
;
1432 (*list
) != nullptr && (*list
)->gdbarch
!= new_gdbarch
;
1433 list
= &(*list
)->next
);
1434 /* It had better be in the list of architectures. */
1435 gdb_assert ((*list
) != nullptr && (*list
)->gdbarch
== new_gdbarch
);
1438 (*list
) = self
->next
;
1439 /* Insert SELF at the front. */
1440 self
->next
= rego
->arches
;
1441 rego
->arches
= self
;
1446 /* It's a new architecture. */
1448 gdb_printf (gdb_stdlog
, "gdbarch_find_by_info: "
1449 "New architecture %s (%s) selected\n",
1450 host_address_to_string (new_gdbarch
),
1451 new_gdbarch
->bfd_arch_info
->printable_name
);
1453 /* Insert the new architecture into the front of the architecture
1454 list (keep the list sorted Most Recently Used). */
1456 struct gdbarch_list
*self
= XNEW (struct gdbarch_list
);
1457 self
->next
= rego
->arches
;
1458 self
->gdbarch
= new_gdbarch
;
1459 rego
->arches
= self
;
1462 /* Check that the newly installed architecture is valid. Plug in
1463 any post init values. */
1464 new_gdbarch
->dump_tdep
= rego
->dump_tdep
;
1465 verify_gdbarch (new_gdbarch
);
1466 new_gdbarch
->initialized_p
= true;
1469 gdbarch_dump (new_gdbarch
, gdb_stdlog
);
1474 /* Make the specified architecture current. */
1477 set_target_gdbarch (struct gdbarch
*new_gdbarch
)
1479 gdb_assert (new_gdbarch
!= NULL
);
1480 gdb_assert (new_gdbarch
->initialized_p
);
1481 current_inferior ()->gdbarch
= new_gdbarch
;
1482 gdb::observers::architecture_changed
.notify (new_gdbarch
);
1483 registers_changed ();
1486 /* Return the current inferior's arch. */
1489 target_gdbarch (void)
1491 return current_inferior ()->gdbarch
;
1494 void _initialize_gdbarch_utils ();
1496 _initialize_gdbarch_utils ()
1498 add_setshow_enum_cmd ("endian", class_support
,
1499 endian_enum
, &set_endian_string
,
1500 _("Set endianness of target."),
1501 _("Show endianness of target."),
1502 NULL
, set_endian
, show_endian
,
1503 &setlist
, &showlist
);
1504 add_setshow_zuinteger_cmd ("arch", class_maintenance
, &gdbarch_debug
, _("\
1505 Set architecture debugging."), _("\
1506 Show architecture debugging."), _("\
1507 When non-zero, architecture debugging is enabled."),
1510 &setdebuglist
, &showdebuglist
);