1 /* Dynamic architecture support for GDB, the GNU debugger.
3 Copyright (C) 1998-2024 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/>. */
21 #include "arch-utils.h"
22 #include "extract-store-integer.h"
23 #include "cli/cli-cmds.h"
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"
40 #include "event-top.h"
42 #include "gdbsupport/version.h"
44 #include "floatformat.h"
49 default_displaced_step_hw_singlestep (struct gdbarch
*gdbarch
)
51 return !gdbarch_software_single_step_p (gdbarch
);
55 displaced_step_at_entry_point (struct gdbarch
*gdbarch
)
60 addr
= entry_point_address (current_program_space
);
62 /* Inferior calls also use the entry point as a breakpoint location.
63 We don't want displaced stepping to interfere with those
64 breakpoints, so leave space. */
65 gdbarch_breakpoint_from_pc (gdbarch
, &addr
, &bp_len
);
72 legacy_register_sim_regno (struct gdbarch
*gdbarch
, int regnum
)
74 /* Only makes sense to supply raw registers. */
75 gdb_assert (regnum
>= 0 && regnum
< gdbarch_num_regs (gdbarch
));
76 /* NOTE: cagney/2002-05-13: The old code did it this way and it is
77 suspected that some GDB/SIM combinations may rely on this
78 behaviour. The default should be one2one_register_sim_regno
80 if (gdbarch_register_name (gdbarch
, regnum
)[0] != '\0')
83 return LEGACY_SIM_REGNO_IGNORE
;
86 /* See arch-utils.h */
89 default_remove_non_address_bits (struct gdbarch
*gdbarch
, CORE_ADDR pointer
)
91 /* By default, just return the pointer value. */
95 /* See arch-utils.h */
98 default_memtag_to_string (struct gdbarch
*gdbarch
, struct value
*tag
)
100 error (_("This architecture has no method to convert a memory tag to"
104 /* See arch-utils.h */
107 default_tagged_address_p (struct gdbarch
*gdbarch
, CORE_ADDR address
)
109 /* By default, assume the address is untagged. */
113 /* See arch-utils.h */
116 default_memtag_matches_p (struct gdbarch
*gdbarch
, struct value
*address
)
118 /* By default, assume the tags match. */
122 /* See arch-utils.h */
125 default_set_memtags (struct gdbarch
*gdbarch
, struct value
*address
,
126 size_t length
, const gdb::byte_vector
&tags
,
127 memtag_type tag_type
)
129 /* By default, return true (successful); */
133 /* See arch-utils.h */
136 default_get_memtag (struct gdbarch
*gdbarch
, struct value
*address
,
137 memtag_type tag_type
)
139 /* By default, return no tag. */
144 generic_skip_trampoline_code (const frame_info_ptr
&frame
, CORE_ADDR pc
)
150 generic_skip_solib_resolver (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
156 generic_in_solib_return_trampoline (struct gdbarch
*gdbarch
,
157 CORE_ADDR pc
, const char *name
)
163 generic_stack_frame_destroyed_p (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
169 default_code_of_frame_writable (struct gdbarch
*gdbarch
,
170 const frame_info_ptr
&frame
)
175 /* Helper functions for gdbarch_inner_than */
178 core_addr_lessthan (CORE_ADDR lhs
, CORE_ADDR rhs
)
184 core_addr_greaterthan (CORE_ADDR lhs
, CORE_ADDR rhs
)
189 /* Misc helper functions for targets. */
192 core_addr_identity (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
198 convert_from_func_ptr_addr_identity (struct gdbarch
*gdbarch
, CORE_ADDR addr
,
199 struct target_ops
*targ
)
205 no_op_reg_to_regnum (struct gdbarch
*gdbarch
, int reg
)
211 default_coff_make_msymbol_special (int val
, struct minimal_symbol
*msym
)
216 /* See arch-utils.h. */
219 default_make_symbol_special (struct symbol
*sym
, struct objfile
*objfile
)
224 /* See arch-utils.h. */
227 default_adjust_dwarf2_addr (CORE_ADDR pc
)
232 /* See arch-utils.h. */
235 default_adjust_dwarf2_line (CORE_ADDR addr
, int rel
)
240 /* See arch-utils.h. */
243 default_execute_dwarf_cfa_vendor_op (struct gdbarch
*gdbarch
, gdb_byte op
,
244 struct dwarf2_frame_state
*fs
)
250 cannot_register_not (struct gdbarch
*gdbarch
, int regnum
)
255 /* Legacy version of target_virtual_frame_pointer(). Assumes that
256 there is an gdbarch_deprecated_fp_regnum and that it is the same,
260 legacy_virtual_frame_pointer (struct gdbarch
*gdbarch
,
263 LONGEST
*frame_offset
)
265 /* FIXME: cagney/2002-09-13: This code is used when identifying the
266 frame pointer of the current PC. It is assuming that a single
267 register and an offset can determine this. I think it should
268 instead generate a byte code expression as that would work better
269 with things like Dwarf2's CFI. */
270 if (gdbarch_deprecated_fp_regnum (gdbarch
) >= 0
271 && gdbarch_deprecated_fp_regnum (gdbarch
)
272 < gdbarch_num_regs (gdbarch
))
273 *frame_regnum
= gdbarch_deprecated_fp_regnum (gdbarch
);
274 else if (gdbarch_sp_regnum (gdbarch
) >= 0
275 && gdbarch_sp_regnum (gdbarch
)
276 < gdbarch_num_regs (gdbarch
))
277 *frame_regnum
= gdbarch_sp_regnum (gdbarch
);
279 /* Should this be an internal error? I guess so, it is reflecting
280 an architectural limitation in the current design. */
281 internal_error (_("No virtual frame pointer available"));
285 /* Return a floating-point format for a floating-point variable of
286 length LEN in bits. If non-NULL, NAME is the name of its type.
287 If no suitable type is found, return NULL. */
289 const struct floatformat
**
290 default_floatformat_for_type (struct gdbarch
*gdbarch
,
291 const char *name
, int len
)
293 const struct floatformat
**format
= NULL
;
295 /* Check if this is a bfloat16 type. It has the same size as the
296 IEEE half float type, so we use the base type name to tell them
298 if (name
!= nullptr && strcmp (name
, "__bf16") == 0
299 && len
== gdbarch_bfloat16_bit (gdbarch
))
300 format
= gdbarch_bfloat16_format (gdbarch
);
301 else if (len
== gdbarch_half_bit (gdbarch
))
302 format
= gdbarch_half_format (gdbarch
);
303 else if (len
== gdbarch_float_bit (gdbarch
))
304 format
= gdbarch_float_format (gdbarch
);
305 else if (len
== gdbarch_double_bit (gdbarch
))
306 format
= gdbarch_double_format (gdbarch
);
307 else if (len
== gdbarch_long_double_bit (gdbarch
))
308 format
= gdbarch_long_double_format (gdbarch
);
309 /* On i386 the 'long double' type takes 96 bits,
310 while the real number of used bits is only 80,
311 both in processor and in memory.
312 The code below accepts the real bit size. */
313 else if (gdbarch_long_double_format (gdbarch
) != NULL
314 && len
== gdbarch_long_double_format (gdbarch
)[0]->totalsize
)
315 format
= gdbarch_long_double_format (gdbarch
);
321 generic_convert_register_p (struct gdbarch
*gdbarch
, int regnum
,
328 default_stabs_argument_has_addr (struct gdbarch
*gdbarch
, struct type
*type
)
334 generic_instruction_nullified (struct gdbarch
*gdbarch
,
335 struct regcache
*regcache
)
341 default_remote_register_number (struct gdbarch
*gdbarch
,
347 /* See arch-utils.h. */
350 default_vsyscall_range (struct gdbarch
*gdbarch
, struct mem_range
*range
)
356 /* Functions to manipulate the endianness of the target. */
358 static enum bfd_endian target_byte_order_user
= BFD_ENDIAN_UNKNOWN
;
360 static const char endian_big
[] = "big";
361 static const char endian_little
[] = "little";
362 static const char endian_auto
[] = "auto";
363 static const char *const endian_enum
[] =
370 static const char *set_endian_string
= endian_auto
;
373 selected_byte_order (void)
375 return target_byte_order_user
;
378 /* Called by ``show endian''. */
381 show_endian (struct ui_file
*file
, int from_tty
, struct cmd_list_element
*c
,
384 if (target_byte_order_user
== BFD_ENDIAN_UNKNOWN
)
385 if (gdbarch_byte_order (get_current_arch ()) == BFD_ENDIAN_BIG
)
386 gdb_printf (file
, _("The target endianness is set automatically "
387 "(currently big endian).\n"));
389 gdb_printf (file
, _("The target endianness is set automatically "
390 "(currently little endian).\n"));
392 if (target_byte_order_user
== BFD_ENDIAN_BIG
)
394 _("The target is set to big endian.\n"));
397 _("The target is set to little endian.\n"));
401 set_endian (const char *ignore_args
, int from_tty
, struct cmd_list_element
*c
)
403 struct gdbarch_info info
;
405 if (set_endian_string
== endian_auto
)
407 target_byte_order_user
= BFD_ENDIAN_UNKNOWN
;
408 if (!gdbarch_update_p (current_inferior (), info
))
409 internal_error (_("set_endian: architecture update failed"));
411 else if (set_endian_string
== endian_little
)
413 info
.byte_order
= BFD_ENDIAN_LITTLE
;
414 if (!gdbarch_update_p (current_inferior (), info
))
415 gdb_printf (gdb_stderr
,
416 _("Little endian target not supported by GDB\n"));
418 target_byte_order_user
= BFD_ENDIAN_LITTLE
;
420 else if (set_endian_string
== endian_big
)
422 info
.byte_order
= BFD_ENDIAN_BIG
;
423 if (!gdbarch_update_p (current_inferior (), info
))
424 gdb_printf (gdb_stderr
,
425 _("Big endian target not supported by GDB\n"));
427 target_byte_order_user
= BFD_ENDIAN_BIG
;
430 internal_error (_("set_endian: bad value"));
432 show_endian (gdb_stdout
, from_tty
, NULL
, NULL
);
435 /* Given SELECTED, a currently selected BFD architecture, and
436 TARGET_DESC, the current target description, return what
439 SELECTED may be NULL, in which case we return the architecture
440 associated with TARGET_DESC. If SELECTED specifies a variant
441 of the architecture associated with TARGET_DESC, return the
442 more specific of the two.
444 If SELECTED is a different architecture, but it is accepted as
445 compatible by the target, we can use the target architecture.
447 If SELECTED is obviously incompatible, warn the user. */
449 static const struct bfd_arch_info
*
450 choose_architecture_for_target (const struct target_desc
*target_desc
,
451 const struct bfd_arch_info
*selected
)
453 const struct bfd_arch_info
*from_target
= tdesc_architecture (target_desc
);
454 const struct bfd_arch_info
*compat1
, *compat2
;
456 if (selected
== NULL
)
459 if (from_target
== NULL
)
462 /* struct bfd_arch_info objects are singletons: that is, there's
463 supposed to be exactly one instance for a given machine. So you
464 can tell whether two are equivalent by comparing pointers. */
465 if (from_target
== selected
)
468 /* BFD's 'A->compatible (A, B)' functions return zero if A and B are
469 incompatible. But if they are compatible, it returns the 'more
470 featureful' of the two arches. That is, if A can run code
471 written for B, but B can't run code written for A, then it'll
474 Some targets (e.g. MIPS as of 2006-12-04) don't fully
475 implement this, instead always returning NULL or the first
476 argument. We detect that case by checking both directions. */
478 compat1
= selected
->compatible (selected
, from_target
);
479 compat2
= from_target
->compatible (from_target
, selected
);
481 if (compat1
== NULL
&& compat2
== NULL
)
483 /* BFD considers the architectures incompatible. Check our
484 target description whether it accepts SELECTED as compatible
486 if (tdesc_compatible_p (target_desc
, selected
))
489 warning (_("Selected architecture %s is not compatible "
490 "with reported target architecture %s"),
491 selected
->printable_name
, from_target
->printable_name
);
499 if (compat1
== compat2
)
502 /* If the two didn't match, but one of them was a default
503 architecture, assume the more specific one is correct. This
504 handles the case where an executable or target description just
505 says "mips", but the other knows which MIPS variant. */
506 if (compat1
->the_default
)
508 if (compat2
->the_default
)
511 /* We have no idea which one is better. This is a bug, but not
512 a critical problem; warn the user. */
513 warning (_("Selected architecture %s is ambiguous with "
514 "reported target architecture %s"),
515 selected
->printable_name
, from_target
->printable_name
);
519 /* Functions to manipulate the architecture of the target. */
521 enum set_arch
{ set_arch_auto
, set_arch_manual
};
523 static const struct bfd_arch_info
*target_architecture_user
;
525 static const char *set_architecture_string
;
528 selected_architecture_name (void)
530 if (target_architecture_user
== NULL
)
533 return set_architecture_string
;
536 /* Called if the user enters ``show architecture'' without an
540 show_architecture (struct ui_file
*file
, int from_tty
,
541 struct cmd_list_element
*c
, const char *value
)
543 if (target_architecture_user
== NULL
)
544 gdb_printf (file
, _("The target architecture is set to "
545 "\"auto\" (currently \"%s\").\n"),
546 gdbarch_bfd_arch_info (get_current_arch ())->printable_name
);
548 gdb_printf (file
, _("The target architecture is set to \"%s\".\n"),
549 set_architecture_string
);
553 /* Called if the user enters ``set architecture'' with or without an
557 set_architecture (const char *ignore_args
,
558 int from_tty
, struct cmd_list_element
*c
)
560 struct gdbarch_info info
;
562 if (strcmp (set_architecture_string
, "auto") == 0)
564 target_architecture_user
= NULL
;
565 if (!gdbarch_update_p (current_inferior (), info
))
566 internal_error (_("could not select an architecture automatically"));
570 info
.bfd_arch_info
= bfd_scan_arch (set_architecture_string
);
571 if (info
.bfd_arch_info
== NULL
)
572 internal_error (_("set_architecture: bfd_scan_arch failed"));
573 if (gdbarch_update_p (current_inferior (), info
))
574 target_architecture_user
= info
.bfd_arch_info
;
576 gdb_printf (gdb_stderr
,
577 _("Architecture `%s' not recognized.\n"),
578 set_architecture_string
);
580 show_architecture (gdb_stdout
, from_tty
, NULL
, NULL
);
583 /* See arch-utils.h. */
586 gdbarch_update_p (inferior
*inf
, struct gdbarch_info info
)
588 struct gdbarch
*new_gdbarch
;
590 /* Check for the current file. */
591 if (info
.abfd
== NULL
)
592 info
.abfd
= inf
->pspace
->exec_bfd ();
594 if (info
.abfd
== NULL
)
595 info
.abfd
= inf
->pspace
->core_bfd ();
597 /* Check for the current target description. */
598 if (info
.target_desc
== NULL
)
599 info
.target_desc
= target_current_description (inf
);
601 new_gdbarch
= gdbarch_find_by_info (info
);
603 /* If there no architecture by that name, reject the request. */
604 if (new_gdbarch
== NULL
)
607 gdb_printf (gdb_stdlog
, "gdbarch_update_p: "
608 "Architecture not found\n");
612 /* If it is the same old architecture, accept the request (but don't
614 if (new_gdbarch
== inf
->arch ())
617 gdb_printf (gdb_stdlog
, "gdbarch_update_p: "
618 "Architecture %s (%s) unchanged\n",
619 host_address_to_string (new_gdbarch
),
620 gdbarch_bfd_arch_info (new_gdbarch
)->printable_name
);
624 /* It's a new architecture, swap it in. */
626 gdb_printf (gdb_stdlog
, "gdbarch_update_p: "
627 "New architecture %s (%s) selected\n",
628 host_address_to_string (new_gdbarch
),
629 gdbarch_bfd_arch_info (new_gdbarch
)->printable_name
);
631 inf
->set_arch (new_gdbarch
);
636 /* Return the architecture for ABFD. If no suitable architecture
637 could be find, return NULL. */
640 gdbarch_from_bfd (bfd
*abfd
)
642 struct gdbarch_info info
;
645 return gdbarch_find_by_info (info
);
648 /* Set the dynamic target-system-dependent parameters (architecture,
649 byte-order) using information found in the BFD */
652 set_gdbarch_from_file (bfd
*abfd
)
654 struct gdbarch_info info
;
655 struct gdbarch
*gdbarch
;
658 info
.target_desc
= target_current_description (current_inferior ());
659 gdbarch
= gdbarch_find_by_info (info
);
662 error (_("Architecture of file not recognized."));
664 current_inferior ()->set_arch (gdbarch
);
667 /* Initialize the current architecture. Update the ``set
668 architecture'' command so that it specifies a list of valid
671 #ifdef DEFAULT_BFD_ARCH
672 extern const bfd_arch_info_type DEFAULT_BFD_ARCH
;
673 static const bfd_arch_info_type
*default_bfd_arch
= &DEFAULT_BFD_ARCH
;
675 static const bfd_arch_info_type
*default_bfd_arch
;
678 #ifdef DEFAULT_BFD_VEC
679 extern const bfd_target DEFAULT_BFD_VEC
;
680 static const bfd_target
*default_bfd_vec
= &DEFAULT_BFD_VEC
;
682 static const bfd_target
*default_bfd_vec
;
685 static enum bfd_endian default_byte_order
= BFD_ENDIAN_UNKNOWN
;
687 /* Printable names of architectures. Used as the enum list of the
688 "set arch" command. */
689 static std::vector
<const char *> arches
;
692 initialize_current_architecture (void)
694 arches
= gdbarch_printable_names ();
696 /* Find a default architecture. */
697 if (default_bfd_arch
== NULL
)
699 /* Choose the architecture by taking the first one
701 const char *chosen
= arches
[0];
703 for (const char *arch
: arches
)
705 if (strcmp (arch
, chosen
) < 0)
710 internal_error (_("initialize_current_architecture: No arch"));
712 default_bfd_arch
= bfd_scan_arch (chosen
);
713 if (default_bfd_arch
== NULL
)
714 internal_error (_("initialize_current_architecture: Arch not found"));
718 info
.bfd_arch_info
= default_bfd_arch
;
720 /* Take several guesses at a byte order. */
721 if (default_byte_order
== BFD_ENDIAN_UNKNOWN
722 && default_bfd_vec
!= NULL
)
724 /* Extract BFD's default vector's byte order. */
725 switch (default_bfd_vec
->byteorder
)
728 default_byte_order
= BFD_ENDIAN_BIG
;
730 case BFD_ENDIAN_LITTLE
:
731 default_byte_order
= BFD_ENDIAN_LITTLE
;
737 if (default_byte_order
== BFD_ENDIAN_UNKNOWN
)
739 /* look for ``*el-*'' in the target name. */
741 chp
= strchr (target_name
, '-');
743 && chp
- 2 >= target_name
744 && startswith (chp
- 2, "el"))
745 default_byte_order
= BFD_ENDIAN_LITTLE
;
747 if (default_byte_order
== BFD_ENDIAN_UNKNOWN
)
749 /* Wire it to big-endian!!! */
750 default_byte_order
= BFD_ENDIAN_BIG
;
753 info
.byte_order
= default_byte_order
;
754 info
.byte_order_for_code
= info
.byte_order
;
756 if (!gdbarch_update_p (current_inferior (), info
))
757 internal_error (_("initialize_current_architecture: Selection of "
758 "initial architecture failed"));
760 /* Create the ``set architecture'' command appending ``auto'' to the
761 list of architectures. */
763 /* Append ``auto''. */
764 set_architecture_string
= "auto";
765 arches
.push_back (set_architecture_string
);
766 arches
.push_back (nullptr);
767 set_show_commands architecture_cmds
768 = add_setshow_enum_cmd ("architecture", class_support
,
769 arches
.data (), &set_architecture_string
,
770 _("Set architecture of target."),
771 _("Show architecture of target."), NULL
,
772 set_architecture
, show_architecture
,
773 &setlist
, &showlist
);
774 add_alias_cmd ("processor", architecture_cmds
.set
, class_support
, 1,
779 /* Similar to init, but this time fill in the blanks. Information is
780 obtained from the global "set ..." options and explicitly
781 initialized INFO fields. */
784 gdbarch_info_fill (struct gdbarch_info
*info
)
786 /* "(gdb) set architecture ...". */
787 if (info
->bfd_arch_info
== NULL
788 && target_architecture_user
)
789 info
->bfd_arch_info
= target_architecture_user
;
791 if (info
->bfd_arch_info
== NULL
792 && info
->abfd
!= NULL
793 && bfd_get_arch (info
->abfd
) != bfd_arch_unknown
794 && bfd_get_arch (info
->abfd
) != bfd_arch_obscure
)
795 info
->bfd_arch_info
= bfd_get_arch_info (info
->abfd
);
796 /* From the target. */
797 if (info
->target_desc
!= NULL
)
798 info
->bfd_arch_info
= choose_architecture_for_target
799 (info
->target_desc
, info
->bfd_arch_info
);
800 /* From the default. */
801 if (info
->bfd_arch_info
== NULL
)
802 info
->bfd_arch_info
= default_bfd_arch
;
804 /* "(gdb) set byte-order ...". */
805 if (info
->byte_order
== BFD_ENDIAN_UNKNOWN
806 && target_byte_order_user
!= BFD_ENDIAN_UNKNOWN
)
807 info
->byte_order
= target_byte_order_user
;
808 /* From the INFO struct. */
809 if (info
->byte_order
== BFD_ENDIAN_UNKNOWN
810 && info
->abfd
!= NULL
)
811 info
->byte_order
= (bfd_big_endian (info
->abfd
) ? BFD_ENDIAN_BIG
812 : bfd_little_endian (info
->abfd
) ? BFD_ENDIAN_LITTLE
813 : BFD_ENDIAN_UNKNOWN
);
814 /* From the default. */
815 if (info
->byte_order
== BFD_ENDIAN_UNKNOWN
)
816 info
->byte_order
= default_byte_order
;
817 info
->byte_order_for_code
= info
->byte_order
;
818 /* Wire the default to the last selected byte order. */
819 default_byte_order
= info
->byte_order
;
821 /* "(gdb) set osabi ...". Handled by gdbarch_lookup_osabi. */
822 /* From the manual override, or from file. */
823 if (info
->osabi
== GDB_OSABI_UNKNOWN
)
824 info
->osabi
= gdbarch_lookup_osabi (info
->abfd
);
825 /* From the target. */
827 if (info
->osabi
== GDB_OSABI_UNKNOWN
&& info
->target_desc
!= NULL
)
828 info
->osabi
= tdesc_osabi (info
->target_desc
);
829 /* From the configured default. */
830 #ifdef GDB_OSABI_DEFAULT
831 if (info
->osabi
== GDB_OSABI_UNKNOWN
)
832 info
->osabi
= GDB_OSABI_DEFAULT
;
834 /* If we still don't know which osabi to pick, pick none. */
835 if (info
->osabi
== GDB_OSABI_UNKNOWN
)
836 info
->osabi
= GDB_OSABI_NONE
;
838 /* Must have at least filled in the architecture. */
839 gdb_assert (info
->bfd_arch_info
!= NULL
);
842 /* Return "current" architecture. If the target is running, this is
843 the architecture of the selected frame. Otherwise, the "current"
844 architecture defaults to the target architecture.
846 This function should normally be called solely by the command
847 interpreter routines to determine the architecture to execute a
850 get_current_arch (void)
852 if (has_stack_frames ())
853 return get_frame_arch (get_selected_frame (NULL
));
855 return current_inferior ()->arch ();
859 default_has_shared_address_space (struct gdbarch
*gdbarch
)
861 /* Simply say no. In most unix-like targets each inferior/process
862 has its own address space. */
867 default_fast_tracepoint_valid_at (struct gdbarch
*gdbarch
, CORE_ADDR addr
,
870 /* We don't know if maybe the target has some way to do fast
871 tracepoints that doesn't need gdbarch, so always say yes. */
878 default_breakpoint_from_pc (struct gdbarch
*gdbarch
, CORE_ADDR
*pcptr
,
881 int kind
= gdbarch_breakpoint_kind_from_pc (gdbarch
, pcptr
);
883 return gdbarch_sw_breakpoint_from_kind (gdbarch
, kind
, lenptr
);
886 default_breakpoint_kind_from_current_state (struct gdbarch
*gdbarch
,
887 struct regcache
*regcache
,
890 return gdbarch_breakpoint_kind_from_pc (gdbarch
, pcptr
);
895 default_gen_return_address (struct gdbarch
*gdbarch
,
896 struct agent_expr
*ax
, struct axs_value
*value
,
899 error (_("This architecture has no method to collect a return address."));
903 default_return_in_first_hidden_param_p (struct gdbarch
*gdbarch
,
906 /* Usually, the return value's address is stored the in the "first hidden"
907 parameter if the return value should be passed by reference, as
909 return !(language_pass_by_reference (type
).trivially_copyable
);
912 int default_insn_is_call (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
917 int default_insn_is_ret (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
922 int default_insn_is_jump (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
927 /* See arch-utils.h. */
930 default_program_breakpoint_here_p (struct gdbarch
*gdbarch
,
934 const gdb_byte
*bpoint
= gdbarch_breakpoint_from_pc (gdbarch
, &address
, &len
);
936 /* Software breakpoints unsupported? */
937 if (bpoint
== nullptr)
940 gdb_byte
*target_mem
= (gdb_byte
*) alloca (len
);
942 /* Enable the automatic memory restoration from breakpoints while
943 we read the memory. Otherwise we may find temporary breakpoints, ones
944 inserted by GDB, and flag them as permanent breakpoints. */
945 scoped_restore restore_memory
946 = make_scoped_restore_show_memory_breakpoints (0);
948 if (target_read_memory (address
, target_mem
, len
) == 0)
950 /* Check if this is a breakpoint instruction for this architecture,
951 including ones used by GDB. */
952 if (memcmp (target_mem
, bpoint
, len
) == 0)
960 default_skip_permanent_breakpoint (struct regcache
*regcache
)
962 struct gdbarch
*gdbarch
= regcache
->arch ();
963 CORE_ADDR current_pc
= regcache_read_pc (regcache
);
966 gdbarch_breakpoint_from_pc (gdbarch
, ¤t_pc
, &bp_len
);
967 current_pc
+= bp_len
;
968 regcache_write_pc (regcache
, current_pc
);
972 default_infcall_mmap (CORE_ADDR size
, unsigned prot
)
974 error (_("This target does not support inferior memory allocation by mmap."));
978 default_infcall_munmap (CORE_ADDR addr
, CORE_ADDR size
)
980 /* Memory reserved by inferior mmap is kept leaked. */
983 /* -mcmodel=large is used so that no GOT (Global Offset Table) is needed to be
984 created in inferior memory by GDB (normally it is set by ld.so). */
987 default_gcc_target_options (struct gdbarch
*gdbarch
)
989 return string_printf ("-m%d%s", gdbarch_ptr_bit (gdbarch
),
990 (gdbarch_ptr_bit (gdbarch
) == 64
991 ? " -mcmodel=large" : ""));
994 /* gdbarch gnu_triplet_regexp method. */
997 default_gnu_triplet_regexp (struct gdbarch
*gdbarch
)
999 return gdbarch_bfd_arch_info (gdbarch
)->arch_name
;
1002 /* Default method for gdbarch_addressable_memory_unit_size. The default is
1003 based on the bits_per_byte defined in the bfd library for the current
1004 architecture, this is usually 8-bits, and so this function will usually
1005 return 1 indicating 1 byte is 1 octet. */
1008 default_addressable_memory_unit_size (struct gdbarch
*gdbarch
)
1010 return gdbarch_bfd_arch_info (gdbarch
)->bits_per_byte
/ 8;
1014 default_guess_tracepoint_registers (struct gdbarch
*gdbarch
,
1015 struct regcache
*regcache
,
1018 int pc_regno
= gdbarch_pc_regnum (gdbarch
);
1021 /* This guessing code below only works if the PC register isn't
1022 a pseudo-register. The value of a pseudo-register isn't stored
1023 in the (non-readonly) regcache -- instead it's recomputed
1024 (probably from some other cached raw register) whenever the
1025 register is read. In this case, a custom method implementation
1026 should be used by the architecture. */
1027 if (pc_regno
< 0 || pc_regno
>= gdbarch_num_regs (gdbarch
))
1030 regs
= (gdb_byte
*) alloca (register_size (gdbarch
, pc_regno
));
1031 store_unsigned_integer (regs
, register_size (gdbarch
, pc_regno
),
1032 gdbarch_byte_order (gdbarch
), addr
);
1033 regcache
->raw_supply (pc_regno
, regs
);
1037 default_print_insn (bfd_vma memaddr
, disassemble_info
*info
)
1039 disassembler_ftype disassemble_fn
;
1041 disassemble_fn
= disassembler (info
->arch
, info
->endian
== BFD_ENDIAN_BIG
,
1042 info
->mach
, current_program_space
->exec_bfd ());
1044 gdb_assert (disassemble_fn
!= NULL
);
1045 int res
= (*disassemble_fn
) (memaddr
, info
);
1052 /* See arch-utils.h. */
1055 gdbarch_skip_prologue_noexcept (gdbarch
*gdbarch
, CORE_ADDR pc
) noexcept
1057 CORE_ADDR new_pc
= pc
;
1061 new_pc
= gdbarch_skip_prologue (gdbarch
, pc
);
1063 catch (const gdb_exception
&ex
)
1069 /* See arch-utils.h. */
1072 default_in_indirect_branch_thunk (gdbarch
*gdbarch
, CORE_ADDR pc
)
1077 /* See arch-utils.h. */
1080 default_type_align (struct gdbarch
*gdbarch
, struct type
*type
)
1085 /* See arch-utils.h. */
1088 default_get_pc_address_flags (const frame_info_ptr
&frame
, CORE_ADDR pc
)
1093 /* See arch-utils.h. */
1095 default_read_core_file_mappings
1096 (struct gdbarch
*gdbarch
,
1098 read_core_file_mappings_pre_loop_ftype pre_loop_cb
,
1099 read_core_file_mappings_loop_ftype loop_cb
)
1103 /* See arch-utils.h. */
1105 default_use_target_description_from_corefile_notes (struct gdbarch
*gdbarch
,
1106 struct bfd
*corefile_bfd
)
1108 /* Always trust the corefile target description contained in the target
1109 description note. */
1114 default_get_return_buf_addr (struct type
*val_type
,
1115 const frame_info_ptr
&cur_frame
)
1121 default_dwarf2_omit_typedef_p (struct type
*target_type
, const char *producer
,
1128 default_update_call_site_pc (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
1133 /* Non-zero if we want to trace architecture code. */
1135 #ifndef GDBARCH_DEBUG
1136 #define GDBARCH_DEBUG 0
1138 unsigned int gdbarch_debug
= GDBARCH_DEBUG
;
1140 show_gdbarch_debug (struct ui_file
*file
, int from_tty
,
1141 struct cmd_list_element
*c
, const char *value
)
1143 gdb_printf (file
, _("Architecture debugging is %s.\n"), value
);
1147 pformat (struct gdbarch
*gdbarch
, const struct floatformat
**format
)
1152 int format_index
= gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_LITTLE
? 1 : 0;
1153 return format
[format_index
]->name
;
1157 pstring (const char *string
)
1165 pstring_ptr (std::string
*string
)
1167 if (string
== nullptr)
1169 return string
->c_str ();
1172 /* Helper function to print a list of strings, represented as "const
1173 char *const *". The list is printed comma-separated. */
1176 pstring_list (const char *const *list
)
1178 static char ret
[100];
1179 const char *const *p
;
1186 for (p
= list
; *p
!= NULL
&& offset
< sizeof (ret
); ++p
)
1188 size_t s
= xsnprintf (ret
+ offset
, sizeof (ret
) - offset
, "%s, ", *p
);
1194 gdb_assert (offset
- 2 < sizeof (ret
));
1195 ret
[offset
- 2] = '\0';
1201 #include "gdbarch-gen.c"
1203 enum return_value_convention
1204 default_gdbarch_return_value
1205 (struct gdbarch
*gdbarch
, struct value
*function
, struct type
*valtype
,
1206 struct regcache
*regcache
, struct value
**read_value
,
1207 const gdb_byte
*writebuf
)
1209 gdb_byte
*readbuf
= nullptr;
1211 if (read_value
!= nullptr)
1213 *read_value
= value::allocate (valtype
);
1214 readbuf
= (*read_value
)->contents_raw ().data ();
1217 return gdbarch
->return_value (gdbarch
, function
, valtype
, regcache
,
1221 obstack
*gdbarch_obstack (gdbarch
*arch
)
1223 return &arch
->obstack
;
1226 /* See gdbarch.h. */
1229 gdbarch_obstack_strdup (struct gdbarch
*arch
, const char *string
)
1231 return obstack_strdup (&arch
->obstack
, string
);
1234 /* Free a gdbarch struct. This should never happen in normal
1235 operation --- once you've created a gdbarch, you keep it around.
1236 However, if an architecture's init function encounters an error
1237 building the structure, it may need to clean up a partially
1238 constructed gdbarch. */
1241 gdbarch_free (struct gdbarch
*arch
)
1243 gdb_assert (arch
!= NULL
);
1244 gdb_assert (!arch
->initialized_p
);
1248 /* See gdbarch.h. */
1250 struct gdbarch_tdep_base
*
1251 gdbarch_tdep_1 (struct gdbarch
*gdbarch
)
1253 if (gdbarch_debug
>= 2)
1254 gdb_printf (gdb_stdlog
, "gdbarch_tdep_1 called\n");
1255 return gdbarch
->tdep
.get ();
1259 registry_accessor
<gdbarch
>::get (gdbarch
*arch
)
1261 return &arch
->registry_fields
;
1264 /* Keep a registry of the architectures known by GDB. */
1266 struct gdbarch_registration
1268 enum bfd_architecture bfd_architecture
;
1269 gdbarch_init_ftype
*init
;
1270 gdbarch_dump_tdep_ftype
*dump_tdep
;
1271 gdbarch_supports_arch_info_ftype
*supports_arch_info
;
1272 struct gdbarch_list
*arches
;
1273 struct gdbarch_registration
*next
;
1276 static struct gdbarch_registration
*gdbarch_registry
= NULL
;
1278 std::vector
<const char *>
1279 gdbarch_printable_names ()
1281 /* Accumulate a list of names based on the registered list of
1283 std::vector
<const char *> arches
;
1285 for (gdbarch_registration
*rego
= gdbarch_registry
;
1289 const struct bfd_arch_info
*ap
1290 = bfd_lookup_arch (rego
->bfd_architecture
, 0);
1292 internal_error (_("gdbarch_architecture_names: multi-arch unknown"));
1295 if (rego
->supports_arch_info
== nullptr
1296 || rego
->supports_arch_info (ap
))
1297 arches
.push_back (ap
->printable_name
);
1308 gdbarch_register (enum bfd_architecture bfd_architecture
,
1309 gdbarch_init_ftype
*init
,
1310 gdbarch_dump_tdep_ftype
*dump_tdep
,
1311 gdbarch_supports_arch_info_ftype
*supports_arch_info
)
1313 struct gdbarch_registration
**curr
;
1314 const struct bfd_arch_info
*bfd_arch_info
;
1316 /* Check that BFD recognizes this architecture */
1317 bfd_arch_info
= bfd_lookup_arch (bfd_architecture
, 0);
1318 if (bfd_arch_info
== NULL
)
1320 internal_error (_("gdbarch: Attempt to register "
1321 "unknown architecture (%d)"),
1324 /* Check that we haven't seen this architecture before. */
1325 for (curr
= &gdbarch_registry
;
1327 curr
= &(*curr
)->next
)
1329 if (bfd_architecture
== (*curr
)->bfd_architecture
)
1330 internal_error (_("gdbarch: Duplicate registration "
1331 "of architecture (%s)"),
1332 bfd_arch_info
->printable_name
);
1336 gdb_printf (gdb_stdlog
, "gdbarch_register (%s, %s)\n",
1337 bfd_arch_info
->printable_name
,
1338 host_address_to_string (init
));
1340 (*curr
) = XNEW (struct gdbarch_registration
);
1341 (*curr
)->bfd_architecture
= bfd_architecture
;
1342 (*curr
)->init
= init
;
1343 (*curr
)->dump_tdep
= dump_tdep
;
1344 (*curr
)->supports_arch_info
= supports_arch_info
;
1345 (*curr
)->arches
= NULL
;
1346 (*curr
)->next
= NULL
;
1349 /* Look for an architecture using gdbarch_info. */
1351 struct gdbarch_list
*
1352 gdbarch_list_lookup_by_info (struct gdbarch_list
*arches
,
1353 const struct gdbarch_info
*info
)
1355 for (; arches
!= NULL
; arches
= arches
->next
)
1357 if (info
->bfd_arch_info
!= arches
->gdbarch
->bfd_arch_info
)
1359 if (info
->byte_order
!= arches
->gdbarch
->byte_order
)
1361 if (info
->osabi
!= arches
->gdbarch
->osabi
)
1363 if (info
->target_desc
!= arches
->gdbarch
->target_desc
)
1371 /* Find an architecture that matches the specified INFO. Create a new
1372 architecture if needed. Return that new architecture. */
1375 gdbarch_find_by_info (struct gdbarch_info info
)
1377 struct gdbarch
*new_gdbarch
;
1378 struct gdbarch_registration
*rego
;
1380 /* Fill in missing parts of the INFO struct using a number of
1381 sources: "set ..."; INFOabfd supplied; and the global
1383 gdbarch_info_fill (&info
);
1385 /* Must have found some sort of architecture. */
1386 gdb_assert (info
.bfd_arch_info
!= nullptr);
1390 gdb_printf (gdb_stdlog
,
1391 "gdbarch_find_by_info: info.bfd_arch_info %s\n",
1392 (info
.bfd_arch_info
!= nullptr
1393 ? info
.bfd_arch_info
->printable_name
1395 gdb_printf (gdb_stdlog
,
1396 "gdbarch_find_by_info: info.byte_order %d (%s)\n",
1398 (info
.byte_order
== BFD_ENDIAN_BIG
? "big"
1399 : info
.byte_order
== BFD_ENDIAN_LITTLE
? "little"
1401 gdb_printf (gdb_stdlog
,
1402 "gdbarch_find_by_info: info.osabi %d (%s)\n",
1403 info
.osabi
, gdbarch_osabi_name (info
.osabi
));
1404 gdb_printf (gdb_stdlog
,
1405 "gdbarch_find_by_info: info.abfd %s\n",
1406 host_address_to_string (info
.abfd
));
1409 /* Find the tdep code that knows about this architecture. */
1410 for (rego
= gdbarch_registry
;
1413 if (rego
->bfd_architecture
== info
.bfd_arch_info
->arch
)
1415 if (rego
== nullptr)
1418 gdb_printf (gdb_stdlog
, "gdbarch_find_by_info: "
1419 "No matching architecture\n");
1423 /* Ask the tdep code for an architecture that matches "info". */
1424 new_gdbarch
= rego
->init (info
, rego
->arches
);
1426 /* Did the tdep code like it? No. Reject the change and revert to
1427 the old architecture. */
1428 if (new_gdbarch
== nullptr)
1431 gdb_printf (gdb_stdlog
, "gdbarch_find_by_info: "
1432 "Target rejected architecture\n");
1436 /* Is this a pre-existing architecture (as determined by already
1437 being initialized)? Move it to the front of the architecture
1438 list (keeping the list sorted Most Recently Used). */
1439 if (new_gdbarch
->initialized_p
)
1441 struct gdbarch_list
**list
;
1442 struct gdbarch_list
*self
;
1444 gdb_printf (gdb_stdlog
, "gdbarch_find_by_info: "
1445 "Previous architecture %s (%s) selected\n",
1446 host_address_to_string (new_gdbarch
),
1447 new_gdbarch
->bfd_arch_info
->printable_name
);
1448 /* Find the existing arch in the list. */
1449 for (list
= ®o
->arches
;
1450 (*list
) != nullptr && (*list
)->gdbarch
!= new_gdbarch
;
1451 list
= &(*list
)->next
);
1452 /* It had better be in the list of architectures. */
1453 gdb_assert ((*list
) != nullptr && (*list
)->gdbarch
== new_gdbarch
);
1456 (*list
) = self
->next
;
1457 /* Insert SELF at the front. */
1458 self
->next
= rego
->arches
;
1459 rego
->arches
= self
;
1464 /* It's a new architecture. */
1466 gdb_printf (gdb_stdlog
, "gdbarch_find_by_info: "
1467 "New architecture %s (%s) selected\n",
1468 host_address_to_string (new_gdbarch
),
1469 new_gdbarch
->bfd_arch_info
->printable_name
);
1471 /* Insert the new architecture into the front of the architecture
1472 list (keep the list sorted Most Recently Used). */
1474 struct gdbarch_list
*self
= XNEW (struct gdbarch_list
);
1475 self
->next
= rego
->arches
;
1476 self
->gdbarch
= new_gdbarch
;
1477 rego
->arches
= self
;
1480 /* Check that the newly installed architecture is valid. Plug in
1481 any post init values. */
1482 new_gdbarch
->dump_tdep
= rego
->dump_tdep
;
1483 verify_gdbarch (new_gdbarch
);
1484 new_gdbarch
->initialized_p
= true;
1487 gdbarch_dump (new_gdbarch
, gdb_stdlog
);
1489 gdb::observers::new_architecture
.notify (new_gdbarch
);
1494 /* See gdbarch.h. */
1497 gdbarch_initialized_p (gdbarch
*arch
)
1499 return arch
->initialized_p
;
1502 void _initialize_gdbarch_utils ();
1504 _initialize_gdbarch_utils ()
1506 add_setshow_enum_cmd ("endian", class_support
,
1507 endian_enum
, &set_endian_string
,
1508 _("Set endianness of target."),
1509 _("Show endianness of target."),
1510 NULL
, set_endian
, show_endian
,
1511 &setlist
, &showlist
);
1512 add_setshow_zuinteger_cmd ("arch", class_maintenance
, &gdbarch_debug
, _("\
1513 Set architecture debugging."), _("\
1514 Show architecture debugging."), _("\
1515 When non-zero, architecture debugging is enabled."),
1518 &setdebuglist
, &showdebuglist
);