1 /* Dynamic architecture support for GDB, the GNU debugger.
3 Copyright (C) 1998-2022 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"
40 #include "gdbsupport/version.h"
42 #include "floatformat.h"
47 default_displaced_step_hw_singlestep (struct gdbarch
*gdbarch
)
49 return !gdbarch_software_single_step_p (gdbarch
);
53 displaced_step_at_entry_point (struct gdbarch
*gdbarch
)
58 addr
= entry_point_address ();
60 /* Inferior calls also use the entry point as a breakpoint location.
61 We don't want displaced stepping to interfere with those
62 breakpoints, so leave space. */
63 gdbarch_breakpoint_from_pc (gdbarch
, &addr
, &bp_len
);
70 legacy_register_sim_regno (struct gdbarch
*gdbarch
, int regnum
)
72 /* Only makes sense to supply raw registers. */
73 gdb_assert (regnum
>= 0 && regnum
< gdbarch_num_regs (gdbarch
));
74 /* NOTE: cagney/2002-05-13: The old code did it this way and it is
75 suspected that some GDB/SIM combinations may rely on this
76 behaviour. The default should be one2one_register_sim_regno
78 if (gdbarch_register_name (gdbarch
, regnum
) != NULL
79 && gdbarch_register_name (gdbarch
, regnum
)[0] != '\0')
82 return LEGACY_SIM_REGNO_IGNORE
;
86 /* See arch-utils.h */
89 default_memtag_to_string (struct gdbarch
*gdbarch
, struct value
*tag
)
91 error (_("This architecture has no method to convert a memory tag to"
95 /* See arch-utils.h */
98 default_tagged_address_p (struct gdbarch
*gdbarch
, struct value
*address
)
100 /* By default, assume the address is untagged. */
104 /* See arch-utils.h */
107 default_memtag_matches_p (struct gdbarch
*gdbarch
, struct value
*address
)
109 /* By default, assume the tags match. */
113 /* See arch-utils.h */
116 default_set_memtags (struct gdbarch
*gdbarch
, struct value
*address
,
117 size_t length
, const gdb::byte_vector
&tags
,
118 memtag_type tag_type
)
120 /* By default, return true (successful); */
124 /* See arch-utils.h */
127 default_get_memtag (struct gdbarch
*gdbarch
, struct value
*address
,
128 memtag_type tag_type
)
130 /* By default, return no tag. */
135 generic_skip_trampoline_code (struct frame_info
*frame
, CORE_ADDR pc
)
141 generic_skip_solib_resolver (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
147 generic_in_solib_return_trampoline (struct gdbarch
*gdbarch
,
148 CORE_ADDR pc
, const char *name
)
154 generic_stack_frame_destroyed_p (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
160 default_code_of_frame_writable (struct gdbarch
*gdbarch
,
161 struct frame_info
*frame
)
166 /* Helper functions for gdbarch_inner_than */
169 core_addr_lessthan (CORE_ADDR lhs
, CORE_ADDR rhs
)
175 core_addr_greaterthan (CORE_ADDR lhs
, CORE_ADDR rhs
)
180 /* Misc helper functions for targets. */
183 core_addr_identity (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
189 convert_from_func_ptr_addr_identity (struct gdbarch
*gdbarch
, CORE_ADDR addr
,
190 struct target_ops
*targ
)
196 no_op_reg_to_regnum (struct gdbarch
*gdbarch
, int reg
)
202 default_coff_make_msymbol_special (int val
, struct minimal_symbol
*msym
)
207 /* See arch-utils.h. */
210 default_make_symbol_special (struct symbol
*sym
, struct objfile
*objfile
)
215 /* See arch-utils.h. */
218 default_adjust_dwarf2_addr (CORE_ADDR pc
)
223 /* See arch-utils.h. */
226 default_adjust_dwarf2_line (CORE_ADDR addr
, int rel
)
231 /* See arch-utils.h. */
234 default_execute_dwarf_cfa_vendor_op (struct gdbarch
*gdbarch
, gdb_byte op
,
235 struct dwarf2_frame_state
*fs
)
241 cannot_register_not (struct gdbarch
*gdbarch
, int regnum
)
246 /* Legacy version of target_virtual_frame_pointer(). Assumes that
247 there is an gdbarch_deprecated_fp_regnum and that it is the same,
251 legacy_virtual_frame_pointer (struct gdbarch
*gdbarch
,
254 LONGEST
*frame_offset
)
256 /* FIXME: cagney/2002-09-13: This code is used when identifying the
257 frame pointer of the current PC. It is assuming that a single
258 register and an offset can determine this. I think it should
259 instead generate a byte code expression as that would work better
260 with things like Dwarf2's CFI. */
261 if (gdbarch_deprecated_fp_regnum (gdbarch
) >= 0
262 && gdbarch_deprecated_fp_regnum (gdbarch
)
263 < gdbarch_num_regs (gdbarch
))
264 *frame_regnum
= gdbarch_deprecated_fp_regnum (gdbarch
);
265 else if (gdbarch_sp_regnum (gdbarch
) >= 0
266 && gdbarch_sp_regnum (gdbarch
)
267 < gdbarch_num_regs (gdbarch
))
268 *frame_regnum
= gdbarch_sp_regnum (gdbarch
);
270 /* Should this be an internal error? I guess so, it is reflecting
271 an architectural limitation in the current design. */
272 internal_error (__FILE__
, __LINE__
,
273 _("No virtual frame pointer available"));
277 /* Return a floating-point format for a floating-point variable of
278 length LEN in bits. If non-NULL, NAME is the name of its type.
279 If no suitable type is found, return NULL. */
281 const struct floatformat
**
282 default_floatformat_for_type (struct gdbarch
*gdbarch
,
283 const char *name
, int len
)
285 const struct floatformat
**format
= NULL
;
287 /* Check if this is a bfloat16 type. It has the same size as the
288 IEEE half float type, so we use the base type name to tell them
290 if (name
!= nullptr && strcmp (name
, "__bf16") == 0
291 && len
== gdbarch_bfloat16_bit (gdbarch
))
292 format
= gdbarch_bfloat16_format (gdbarch
);
293 else if (len
== gdbarch_half_bit (gdbarch
))
294 format
= gdbarch_half_format (gdbarch
);
295 else if (len
== gdbarch_float_bit (gdbarch
))
296 format
= gdbarch_float_format (gdbarch
);
297 else if (len
== gdbarch_double_bit (gdbarch
))
298 format
= gdbarch_double_format (gdbarch
);
299 else if (len
== gdbarch_long_double_bit (gdbarch
))
300 format
= gdbarch_long_double_format (gdbarch
);
301 /* On i386 the 'long double' type takes 96 bits,
302 while the real number of used bits is only 80,
303 both in processor and in memory.
304 The code below accepts the real bit size. */
305 else if (gdbarch_long_double_format (gdbarch
) != NULL
306 && len
== gdbarch_long_double_format (gdbarch
)[0]->totalsize
)
307 format
= gdbarch_long_double_format (gdbarch
);
313 generic_convert_register_p (struct gdbarch
*gdbarch
, int regnum
,
320 default_stabs_argument_has_addr (struct gdbarch
*gdbarch
, struct type
*type
)
326 generic_instruction_nullified (struct gdbarch
*gdbarch
,
327 struct regcache
*regcache
)
333 default_remote_register_number (struct gdbarch
*gdbarch
,
339 /* See arch-utils.h. */
342 default_vsyscall_range (struct gdbarch
*gdbarch
, struct mem_range
*range
)
348 /* Functions to manipulate the endianness of the target. */
350 static enum bfd_endian target_byte_order_user
= BFD_ENDIAN_UNKNOWN
;
352 static const char endian_big
[] = "big";
353 static const char endian_little
[] = "little";
354 static const char endian_auto
[] = "auto";
355 static const char *const endian_enum
[] =
362 static const char *set_endian_string
;
365 selected_byte_order (void)
367 return target_byte_order_user
;
370 /* Called by ``show endian''. */
373 show_endian (struct ui_file
*file
, int from_tty
, struct cmd_list_element
*c
,
376 if (target_byte_order_user
== BFD_ENDIAN_UNKNOWN
)
377 if (gdbarch_byte_order (get_current_arch ()) == BFD_ENDIAN_BIG
)
378 fprintf_filtered (file
, _("The target endianness is set automatically "
379 "(currently big endian).\n"));
381 fprintf_filtered (file
, _("The target endianness is set automatically "
382 "(currently little endian).\n"));
384 if (target_byte_order_user
== BFD_ENDIAN_BIG
)
385 fprintf_filtered (file
,
386 _("The target is set to big endian.\n"));
388 fprintf_filtered (file
,
389 _("The target is set to little endian.\n"));
393 set_endian (const char *ignore_args
, int from_tty
, struct cmd_list_element
*c
)
395 struct gdbarch_info info
;
397 if (set_endian_string
== endian_auto
)
399 target_byte_order_user
= BFD_ENDIAN_UNKNOWN
;
400 if (! gdbarch_update_p (info
))
401 internal_error (__FILE__
, __LINE__
,
402 _("set_endian: architecture update failed"));
404 else if (set_endian_string
== endian_little
)
406 info
.byte_order
= BFD_ENDIAN_LITTLE
;
407 if (! gdbarch_update_p (info
))
408 fprintf_unfiltered (gdb_stderr
,
409 _("Little endian target not supported by GDB\n"));
411 target_byte_order_user
= BFD_ENDIAN_LITTLE
;
413 else if (set_endian_string
== endian_big
)
415 info
.byte_order
= BFD_ENDIAN_BIG
;
416 if (! gdbarch_update_p (info
))
417 fprintf_unfiltered (gdb_stderr
,
418 _("Big endian target not supported by GDB\n"));
420 target_byte_order_user
= BFD_ENDIAN_BIG
;
423 internal_error (__FILE__
, __LINE__
,
424 _("set_endian: bad value"));
426 show_endian (gdb_stdout
, from_tty
, NULL
, NULL
);
429 /* Given SELECTED, a currently selected BFD architecture, and
430 TARGET_DESC, the current target description, return what
433 SELECTED may be NULL, in which case we return the architecture
434 associated with TARGET_DESC. If SELECTED specifies a variant
435 of the architecture associated with TARGET_DESC, return the
436 more specific of the two.
438 If SELECTED is a different architecture, but it is accepted as
439 compatible by the target, we can use the target architecture.
441 If SELECTED is obviously incompatible, warn the user. */
443 static const struct bfd_arch_info
*
444 choose_architecture_for_target (const struct target_desc
*target_desc
,
445 const struct bfd_arch_info
*selected
)
447 const struct bfd_arch_info
*from_target
= tdesc_architecture (target_desc
);
448 const struct bfd_arch_info
*compat1
, *compat2
;
450 if (selected
== NULL
)
453 if (from_target
== NULL
)
456 /* struct bfd_arch_info objects are singletons: that is, there's
457 supposed to be exactly one instance for a given machine. So you
458 can tell whether two are equivalent by comparing pointers. */
459 if (from_target
== selected
)
462 /* BFD's 'A->compatible (A, B)' functions return zero if A and B are
463 incompatible. But if they are compatible, it returns the 'more
464 featureful' of the two arches. That is, if A can run code
465 written for B, but B can't run code written for A, then it'll
468 Some targets (e.g. MIPS as of 2006-12-04) don't fully
469 implement this, instead always returning NULL or the first
470 argument. We detect that case by checking both directions. */
472 compat1
= selected
->compatible (selected
, from_target
);
473 compat2
= from_target
->compatible (from_target
, selected
);
475 if (compat1
== NULL
&& compat2
== NULL
)
477 /* BFD considers the architectures incompatible. Check our
478 target description whether it accepts SELECTED as compatible
480 if (tdesc_compatible_p (target_desc
, selected
))
483 warning (_("Selected architecture %s is not compatible "
484 "with reported target architecture %s"),
485 selected
->printable_name
, from_target
->printable_name
);
493 if (compat1
== compat2
)
496 /* If the two didn't match, but one of them was a default
497 architecture, assume the more specific one is correct. This
498 handles the case where an executable or target description just
499 says "mips", but the other knows which MIPS variant. */
500 if (compat1
->the_default
)
502 if (compat2
->the_default
)
505 /* We have no idea which one is better. This is a bug, but not
506 a critical problem; warn the user. */
507 warning (_("Selected architecture %s is ambiguous with "
508 "reported target architecture %s"),
509 selected
->printable_name
, from_target
->printable_name
);
513 /* Functions to manipulate the architecture of the target. */
515 enum set_arch
{ set_arch_auto
, set_arch_manual
};
517 static const struct bfd_arch_info
*target_architecture_user
;
519 static const char *set_architecture_string
;
522 selected_architecture_name (void)
524 if (target_architecture_user
== NULL
)
527 return set_architecture_string
;
530 /* Called if the user enters ``show architecture'' without an
534 show_architecture (struct ui_file
*file
, int from_tty
,
535 struct cmd_list_element
*c
, const char *value
)
537 if (target_architecture_user
== NULL
)
538 fprintf_filtered (file
, _("The target architecture is set to "
539 "\"auto\" (currently \"%s\").\n"),
540 gdbarch_bfd_arch_info (get_current_arch ())->printable_name
);
542 fprintf_filtered (file
, _("The target architecture is set to \"%s\".\n"),
543 set_architecture_string
);
547 /* Called if the user enters ``set architecture'' with or without an
551 set_architecture (const char *ignore_args
,
552 int from_tty
, struct cmd_list_element
*c
)
554 struct gdbarch_info info
;
556 if (strcmp (set_architecture_string
, "auto") == 0)
558 target_architecture_user
= NULL
;
559 if (!gdbarch_update_p (info
))
560 internal_error (__FILE__
, __LINE__
,
561 _("could not select an architecture automatically"));
565 info
.bfd_arch_info
= bfd_scan_arch (set_architecture_string
);
566 if (info
.bfd_arch_info
== NULL
)
567 internal_error (__FILE__
, __LINE__
,
568 _("set_architecture: bfd_scan_arch failed"));
569 if (gdbarch_update_p (info
))
570 target_architecture_user
= info
.bfd_arch_info
;
572 fprintf_unfiltered (gdb_stderr
,
573 _("Architecture `%s' not recognized.\n"),
574 set_architecture_string
);
576 show_architecture (gdb_stdout
, from_tty
, NULL
, NULL
);
579 /* Try to select a global architecture that matches "info". Return
580 non-zero if the attempt succeeds. */
582 gdbarch_update_p (struct gdbarch_info info
)
584 struct gdbarch
*new_gdbarch
;
586 /* Check for the current file. */
587 if (info
.abfd
== NULL
)
588 info
.abfd
= current_program_space
->exec_bfd ();
589 if (info
.abfd
== NULL
)
590 info
.abfd
= core_bfd
;
592 /* Check for the current target description. */
593 if (info
.target_desc
== NULL
)
594 info
.target_desc
= target_current_description ();
596 new_gdbarch
= gdbarch_find_by_info (info
);
598 /* If there no architecture by that name, reject the request. */
599 if (new_gdbarch
== NULL
)
602 fprintf_unfiltered (gdb_stdlog
, "gdbarch_update_p: "
603 "Architecture not found\n");
607 /* If it is the same old architecture, accept the request (but don't
609 if (new_gdbarch
== target_gdbarch ())
612 fprintf_unfiltered (gdb_stdlog
, "gdbarch_update_p: "
613 "Architecture %s (%s) unchanged\n",
614 host_address_to_string (new_gdbarch
),
615 gdbarch_bfd_arch_info (new_gdbarch
)->printable_name
);
619 /* It's a new architecture, swap it in. */
621 fprintf_unfiltered (gdb_stdlog
, "gdbarch_update_p: "
622 "New architecture %s (%s) selected\n",
623 host_address_to_string (new_gdbarch
),
624 gdbarch_bfd_arch_info (new_gdbarch
)->printable_name
);
625 set_target_gdbarch (new_gdbarch
);
630 /* Return the architecture for ABFD. If no suitable architecture
631 could be find, return NULL. */
634 gdbarch_from_bfd (bfd
*abfd
)
636 struct gdbarch_info info
;
639 return gdbarch_find_by_info (info
);
642 /* Set the dynamic target-system-dependent parameters (architecture,
643 byte-order) using information found in the BFD */
646 set_gdbarch_from_file (bfd
*abfd
)
648 struct gdbarch_info info
;
649 struct gdbarch
*gdbarch
;
652 info
.target_desc
= target_current_description ();
653 gdbarch
= gdbarch_find_by_info (info
);
656 error (_("Architecture of file not recognized."));
657 set_target_gdbarch (gdbarch
);
660 /* Initialize the current architecture. Update the ``set
661 architecture'' command so that it specifies a list of valid
664 #ifdef DEFAULT_BFD_ARCH
665 extern const bfd_arch_info_type DEFAULT_BFD_ARCH
;
666 static const bfd_arch_info_type
*default_bfd_arch
= &DEFAULT_BFD_ARCH
;
668 static const bfd_arch_info_type
*default_bfd_arch
;
671 #ifdef DEFAULT_BFD_VEC
672 extern const bfd_target DEFAULT_BFD_VEC
;
673 static const bfd_target
*default_bfd_vec
= &DEFAULT_BFD_VEC
;
675 static const bfd_target
*default_bfd_vec
;
678 static enum bfd_endian default_byte_order
= BFD_ENDIAN_UNKNOWN
;
680 /* Printable names of architectures. Used as the enum list of the
681 "set arch" command. */
682 static std::vector
<const char *> arches
;
685 initialize_current_architecture (void)
687 arches
= gdbarch_printable_names ();
689 /* Find a default architecture. */
690 if (default_bfd_arch
== NULL
)
692 /* Choose the architecture by taking the first one
694 const char *chosen
= arches
[0];
696 for (const char *arch
: arches
)
698 if (strcmp (arch
, chosen
) < 0)
703 internal_error (__FILE__
, __LINE__
,
704 _("initialize_current_architecture: No arch"));
706 default_bfd_arch
= bfd_scan_arch (chosen
);
707 if (default_bfd_arch
== NULL
)
708 internal_error (__FILE__
, __LINE__
,
709 _("initialize_current_architecture: Arch not found"));
713 info
.bfd_arch_info
= default_bfd_arch
;
715 /* Take several guesses at a byte order. */
716 if (default_byte_order
== BFD_ENDIAN_UNKNOWN
717 && default_bfd_vec
!= NULL
)
719 /* Extract BFD's default vector's byte order. */
720 switch (default_bfd_vec
->byteorder
)
723 default_byte_order
= BFD_ENDIAN_BIG
;
725 case BFD_ENDIAN_LITTLE
:
726 default_byte_order
= BFD_ENDIAN_LITTLE
;
732 if (default_byte_order
== BFD_ENDIAN_UNKNOWN
)
734 /* look for ``*el-*'' in the target name. */
736 chp
= strchr (target_name
, '-');
738 && chp
- 2 >= target_name
739 && startswith (chp
- 2, "el"))
740 default_byte_order
= BFD_ENDIAN_LITTLE
;
742 if (default_byte_order
== BFD_ENDIAN_UNKNOWN
)
744 /* Wire it to big-endian!!! */
745 default_byte_order
= BFD_ENDIAN_BIG
;
748 info
.byte_order
= default_byte_order
;
749 info
.byte_order_for_code
= info
.byte_order
;
751 if (! gdbarch_update_p (info
))
752 internal_error (__FILE__
, __LINE__
,
753 _("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 arches
.push_back ("auto");
761 arches
.push_back (nullptr);
762 set_show_commands architecture_cmds
763 = add_setshow_enum_cmd ("architecture", class_support
,
764 arches
.data (), &set_architecture_string
,
765 _("Set architecture of target."),
766 _("Show architecture of target."), NULL
,
767 set_architecture
, show_architecture
,
768 &setlist
, &showlist
);
769 add_alias_cmd ("processor", architecture_cmds
.set
, class_support
, 1,
774 /* Similar to init, but this time fill in the blanks. Information is
775 obtained from the global "set ..." options and explicitly
776 initialized INFO fields. */
779 gdbarch_info_fill (struct gdbarch_info
*info
)
781 /* "(gdb) set architecture ...". */
782 if (info
->bfd_arch_info
== NULL
783 && target_architecture_user
)
784 info
->bfd_arch_info
= target_architecture_user
;
786 if (info
->bfd_arch_info
== NULL
787 && info
->abfd
!= NULL
788 && bfd_get_arch (info
->abfd
) != bfd_arch_unknown
789 && bfd_get_arch (info
->abfd
) != bfd_arch_obscure
)
790 info
->bfd_arch_info
= bfd_get_arch_info (info
->abfd
);
791 /* From the target. */
792 if (info
->target_desc
!= NULL
)
793 info
->bfd_arch_info
= choose_architecture_for_target
794 (info
->target_desc
, info
->bfd_arch_info
);
795 /* From the default. */
796 if (info
->bfd_arch_info
== NULL
)
797 info
->bfd_arch_info
= default_bfd_arch
;
799 /* "(gdb) set byte-order ...". */
800 if (info
->byte_order
== BFD_ENDIAN_UNKNOWN
801 && target_byte_order_user
!= BFD_ENDIAN_UNKNOWN
)
802 info
->byte_order
= target_byte_order_user
;
803 /* From the INFO struct. */
804 if (info
->byte_order
== BFD_ENDIAN_UNKNOWN
805 && info
->abfd
!= NULL
)
806 info
->byte_order
= (bfd_big_endian (info
->abfd
) ? BFD_ENDIAN_BIG
807 : bfd_little_endian (info
->abfd
) ? BFD_ENDIAN_LITTLE
808 : BFD_ENDIAN_UNKNOWN
);
809 /* From the default. */
810 if (info
->byte_order
== BFD_ENDIAN_UNKNOWN
)
811 info
->byte_order
= default_byte_order
;
812 info
->byte_order_for_code
= info
->byte_order
;
813 /* Wire the default to the last selected byte order. */
814 default_byte_order
= info
->byte_order
;
816 /* "(gdb) set osabi ...". Handled by gdbarch_lookup_osabi. */
817 /* From the manual override, or from file. */
818 if (info
->osabi
== GDB_OSABI_UNKNOWN
)
819 info
->osabi
= gdbarch_lookup_osabi (info
->abfd
);
820 /* From the target. */
822 if (info
->osabi
== GDB_OSABI_UNKNOWN
&& info
->target_desc
!= NULL
)
823 info
->osabi
= tdesc_osabi (info
->target_desc
);
824 /* From the configured default. */
825 #ifdef GDB_OSABI_DEFAULT
826 if (info
->osabi
== GDB_OSABI_UNKNOWN
)
827 info
->osabi
= GDB_OSABI_DEFAULT
;
829 /* If we still don't know which osabi to pick, pick none. */
830 if (info
->osabi
== GDB_OSABI_UNKNOWN
)
831 info
->osabi
= GDB_OSABI_NONE
;
833 /* Must have at least filled in the architecture. */
834 gdb_assert (info
->bfd_arch_info
!= NULL
);
837 /* Return "current" architecture. If the target is running, this is
838 the architecture of the selected frame. Otherwise, the "current"
839 architecture defaults to the target architecture.
841 This function should normally be called solely by the command
842 interpreter routines to determine the architecture to execute a
845 get_current_arch (void)
847 if (has_stack_frames ())
848 return get_frame_arch (get_selected_frame (NULL
));
850 return target_gdbarch ();
854 default_has_shared_address_space (struct gdbarch
*gdbarch
)
856 /* Simply say no. In most unix-like targets each inferior/process
857 has its own address space. */
862 default_fast_tracepoint_valid_at (struct gdbarch
*gdbarch
, CORE_ADDR addr
,
865 /* We don't know if maybe the target has some way to do fast
866 tracepoints that doesn't need gdbarch, so always say yes. */
873 default_breakpoint_from_pc (struct gdbarch
*gdbarch
, CORE_ADDR
*pcptr
,
876 int kind
= gdbarch_breakpoint_kind_from_pc (gdbarch
, pcptr
);
878 return gdbarch_sw_breakpoint_from_kind (gdbarch
, kind
, lenptr
);
881 default_breakpoint_kind_from_current_state (struct gdbarch
*gdbarch
,
882 struct regcache
*regcache
,
885 return gdbarch_breakpoint_kind_from_pc (gdbarch
, pcptr
);
890 default_gen_return_address (struct gdbarch
*gdbarch
,
891 struct agent_expr
*ax
, struct axs_value
*value
,
894 error (_("This architecture has no method to collect a return address."));
898 default_return_in_first_hidden_param_p (struct gdbarch
*gdbarch
,
901 /* Usually, the return value's address is stored the in the "first hidden"
902 parameter if the return value should be passed by reference, as
904 return !(language_pass_by_reference (type
).trivially_copyable
);
907 int default_insn_is_call (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
912 int default_insn_is_ret (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
917 int default_insn_is_jump (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
922 /* See arch-utils.h. */
925 default_program_breakpoint_here_p (struct gdbarch
*gdbarch
,
929 const gdb_byte
*bpoint
= gdbarch_breakpoint_from_pc (gdbarch
, &address
, &len
);
931 /* Software breakpoints unsupported? */
932 if (bpoint
== nullptr)
935 gdb_byte
*target_mem
= (gdb_byte
*) alloca (len
);
937 /* Enable the automatic memory restoration from breakpoints while
938 we read the memory. Otherwise we may find temporary breakpoints, ones
939 inserted by GDB, and flag them as permanent breakpoints. */
940 scoped_restore restore_memory
941 = make_scoped_restore_show_memory_breakpoints (0);
943 if (target_read_memory (address
, target_mem
, len
) == 0)
945 /* Check if this is a breakpoint instruction for this architecture,
946 including ones used by GDB. */
947 if (memcmp (target_mem
, bpoint
, len
) == 0)
955 default_skip_permanent_breakpoint (struct regcache
*regcache
)
957 struct gdbarch
*gdbarch
= regcache
->arch ();
958 CORE_ADDR current_pc
= regcache_read_pc (regcache
);
961 gdbarch_breakpoint_from_pc (gdbarch
, ¤t_pc
, &bp_len
);
962 current_pc
+= bp_len
;
963 regcache_write_pc (regcache
, current_pc
);
967 default_infcall_mmap (CORE_ADDR size
, unsigned prot
)
969 error (_("This target does not support inferior memory allocation by mmap."));
973 default_infcall_munmap (CORE_ADDR addr
, CORE_ADDR size
)
975 /* Memory reserved by inferior mmap is kept leaked. */
978 /* -mcmodel=large is used so that no GOT (Global Offset Table) is needed to be
979 created in inferior memory by GDB (normally it is set by ld.so). */
982 default_gcc_target_options (struct gdbarch
*gdbarch
)
984 return string_printf ("-m%d%s", gdbarch_ptr_bit (gdbarch
),
985 (gdbarch_ptr_bit (gdbarch
) == 64
986 ? " -mcmodel=large" : ""));
989 /* gdbarch gnu_triplet_regexp method. */
992 default_gnu_triplet_regexp (struct gdbarch
*gdbarch
)
994 return gdbarch_bfd_arch_info (gdbarch
)->arch_name
;
997 /* Default method for gdbarch_addressable_memory_unit_size. The default is
998 based on the bits_per_byte defined in the bfd library for the current
999 architecture, this is usually 8-bits, and so this function will usually
1000 return 1 indicating 1 byte is 1 octet. */
1003 default_addressable_memory_unit_size (struct gdbarch
*gdbarch
)
1005 return gdbarch_bfd_arch_info (gdbarch
)->bits_per_byte
/ 8;
1009 default_guess_tracepoint_registers (struct gdbarch
*gdbarch
,
1010 struct regcache
*regcache
,
1013 int pc_regno
= gdbarch_pc_regnum (gdbarch
);
1016 /* This guessing code below only works if the PC register isn't
1017 a pseudo-register. The value of a pseudo-register isn't stored
1018 in the (non-readonly) regcache -- instead it's recomputed
1019 (probably from some other cached raw register) whenever the
1020 register is read. In this case, a custom method implementation
1021 should be used by the architecture. */
1022 if (pc_regno
< 0 || pc_regno
>= gdbarch_num_regs (gdbarch
))
1025 regs
= (gdb_byte
*) alloca (register_size (gdbarch
, pc_regno
));
1026 store_unsigned_integer (regs
, register_size (gdbarch
, pc_regno
),
1027 gdbarch_byte_order (gdbarch
), addr
);
1028 regcache
->raw_supply (pc_regno
, regs
);
1032 default_print_insn (bfd_vma memaddr
, disassemble_info
*info
)
1034 disassembler_ftype disassemble_fn
;
1036 disassemble_fn
= disassembler (info
->arch
, info
->endian
== BFD_ENDIAN_BIG
,
1037 info
->mach
, current_program_space
->exec_bfd ());
1039 gdb_assert (disassemble_fn
!= NULL
);
1040 return (*disassemble_fn
) (memaddr
, info
);
1043 /* See arch-utils.h. */
1046 gdbarch_skip_prologue_noexcept (gdbarch
*gdbarch
, CORE_ADDR pc
) noexcept
1048 CORE_ADDR new_pc
= pc
;
1052 new_pc
= gdbarch_skip_prologue (gdbarch
, pc
);
1054 catch (const gdb_exception
&ex
)
1060 /* See arch-utils.h. */
1063 default_in_indirect_branch_thunk (gdbarch
*gdbarch
, CORE_ADDR pc
)
1068 /* See arch-utils.h. */
1071 default_type_align (struct gdbarch
*gdbarch
, struct type
*type
)
1076 /* See arch-utils.h. */
1079 default_get_pc_address_flags (frame_info
*frame
, CORE_ADDR pc
)
1084 /* See arch-utils.h. */
1086 default_read_core_file_mappings
1087 (struct gdbarch
*gdbarch
,
1089 read_core_file_mappings_pre_loop_ftype pre_loop_cb
,
1090 read_core_file_mappings_loop_ftype loop_cb
)
1094 /* Static function declarations */
1096 static void alloc_gdbarch_data (struct gdbarch
*);
1098 /* Non-zero if we want to trace architecture code. */
1100 #ifndef GDBARCH_DEBUG
1101 #define GDBARCH_DEBUG 0
1103 unsigned int gdbarch_debug
= GDBARCH_DEBUG
;
1105 show_gdbarch_debug (struct ui_file
*file
, int from_tty
,
1106 struct cmd_list_element
*c
, const char *value
)
1108 fprintf_filtered (file
, _("Architecture debugging is %s.\n"), value
);
1112 pformat (const struct floatformat
**format
)
1117 /* Just print out one of them - this is only for diagnostics. */
1118 return format
[0]->name
;
1122 pstring (const char *string
)
1130 pstring_ptr (char **string
)
1132 if (string
== NULL
|| *string
== NULL
)
1137 /* Helper function to print a list of strings, represented as "const
1138 char *const *". The list is printed comma-separated. */
1141 pstring_list (const char *const *list
)
1143 static char ret
[100];
1144 const char *const *p
;
1151 for (p
= list
; *p
!= NULL
&& offset
< sizeof (ret
); ++p
)
1153 size_t s
= xsnprintf (ret
+ offset
, sizeof (ret
) - offset
, "%s, ", *p
);
1159 gdb_assert (offset
- 2 < sizeof (ret
));
1160 ret
[offset
- 2] = '\0';
1166 #include "gdbarch.c"
1168 obstack
*gdbarch_obstack (gdbarch
*arch
)
1170 return arch
->obstack
;
1173 /* See gdbarch.h. */
1176 gdbarch_obstack_strdup (struct gdbarch
*arch
, const char *string
)
1178 return obstack_strdup (arch
->obstack
, string
);
1182 /* Free a gdbarch struct. This should never happen in normal
1183 operation --- once you've created a gdbarch, you keep it around.
1184 However, if an architecture's init function encounters an error
1185 building the structure, it may need to clean up a partially
1186 constructed gdbarch. */
1189 gdbarch_free (struct gdbarch
*arch
)
1191 struct obstack
*obstack
;
1193 gdb_assert (arch
!= NULL
);
1194 gdb_assert (!arch
->initialized_p
);
1195 obstack
= arch
->obstack
;
1196 obstack_free (obstack
, 0); /* Includes the ARCH. */
1200 struct gdbarch_tdep
*
1201 gdbarch_tdep (struct gdbarch
*gdbarch
)
1203 if (gdbarch_debug
>= 2)
1204 fprintf_unfiltered (gdb_stdlog
, "gdbarch_tdep called\n");
1205 return gdbarch
->tdep
;
1208 /* Keep a registry of per-architecture data-pointers required by GDB
1215 gdbarch_data_pre_init_ftype
*pre_init
;
1216 gdbarch_data_post_init_ftype
*post_init
;
1219 struct gdbarch_data_registration
1221 struct gdbarch_data
*data
;
1222 struct gdbarch_data_registration
*next
;
1225 struct gdbarch_data_registry
1228 struct gdbarch_data_registration
*registrations
;
1231 static struct gdbarch_data_registry gdbarch_data_registry
=
1236 static struct gdbarch_data
*
1237 gdbarch_data_register (gdbarch_data_pre_init_ftype
*pre_init
,
1238 gdbarch_data_post_init_ftype
*post_init
)
1240 struct gdbarch_data_registration
**curr
;
1242 /* Append the new registration. */
1243 for (curr
= &gdbarch_data_registry
.registrations
;
1245 curr
= &(*curr
)->next
);
1246 (*curr
) = XNEW (struct gdbarch_data_registration
);
1247 (*curr
)->next
= NULL
;
1248 (*curr
)->data
= XNEW (struct gdbarch_data
);
1249 (*curr
)->data
->index
= gdbarch_data_registry
.nr
++;
1250 (*curr
)->data
->pre_init
= pre_init
;
1251 (*curr
)->data
->post_init
= post_init
;
1252 (*curr
)->data
->init_p
= 1;
1253 return (*curr
)->data
;
1256 struct gdbarch_data
*
1257 gdbarch_data_register_pre_init (gdbarch_data_pre_init_ftype
*pre_init
)
1259 return gdbarch_data_register (pre_init
, NULL
);
1262 struct gdbarch_data
*
1263 gdbarch_data_register_post_init (gdbarch_data_post_init_ftype
*post_init
)
1265 return gdbarch_data_register (NULL
, post_init
);
1268 /* Create/delete the gdbarch data vector. */
1271 alloc_gdbarch_data (struct gdbarch
*gdbarch
)
1273 gdb_assert (gdbarch
->data
== NULL
);
1274 gdbarch
->nr_data
= gdbarch_data_registry
.nr
;
1275 gdbarch
->data
= GDBARCH_OBSTACK_CALLOC (gdbarch
, gdbarch
->nr_data
, void *);
1278 /* Return the current value of the specified per-architecture
1282 gdbarch_data (struct gdbarch
*gdbarch
, struct gdbarch_data
*data
)
1284 gdb_assert (data
->index
< gdbarch
->nr_data
);
1285 if (gdbarch
->data
[data
->index
] == NULL
)
1287 /* The data-pointer isn't initialized, call init() to get a
1289 if (data
->pre_init
!= NULL
)
1290 /* Mid architecture creation: pass just the obstack, and not
1291 the entire architecture, as that way it isn't possible for
1292 pre-init code to refer to undefined architecture
1294 gdbarch
->data
[data
->index
] = data
->pre_init (gdbarch
->obstack
);
1295 else if (gdbarch
->initialized_p
1296 && data
->post_init
!= NULL
)
1297 /* Post architecture creation: pass the entire architecture
1298 (as all fields are valid), but be careful to also detect
1299 recursive references. */
1301 gdb_assert (data
->init_p
);
1303 gdbarch
->data
[data
->index
] = data
->post_init (gdbarch
);
1307 internal_error (__FILE__
, __LINE__
,
1308 _("gdbarch post-init data field can only be used "
1309 "after gdbarch is fully initialised"));
1310 gdb_assert (gdbarch
->data
[data
->index
] != NULL
);
1312 return gdbarch
->data
[data
->index
];
1316 /* Keep a registry of the architectures known by GDB. */
1318 struct gdbarch_registration
1320 enum bfd_architecture bfd_architecture
;
1321 gdbarch_init_ftype
*init
;
1322 gdbarch_dump_tdep_ftype
*dump_tdep
;
1323 struct gdbarch_list
*arches
;
1324 struct gdbarch_registration
*next
;
1327 static struct gdbarch_registration
*gdbarch_registry
= NULL
;
1329 std::vector
<const char *>
1330 gdbarch_printable_names ()
1332 /* Accumulate a list of names based on the registed list of
1334 std::vector
<const char *> arches
;
1336 for (gdbarch_registration
*rego
= gdbarch_registry
;
1340 const struct bfd_arch_info
*ap
1341 = bfd_lookup_arch (rego
->bfd_architecture
, 0);
1343 internal_error (__FILE__
, __LINE__
,
1344 _("gdbarch_architecture_names: multi-arch unknown"));
1347 arches
.push_back (ap
->printable_name
);
1358 gdbarch_register (enum bfd_architecture bfd_architecture
,
1359 gdbarch_init_ftype
*init
,
1360 gdbarch_dump_tdep_ftype
*dump_tdep
)
1362 struct gdbarch_registration
**curr
;
1363 const struct bfd_arch_info
*bfd_arch_info
;
1365 /* Check that BFD recognizes this architecture */
1366 bfd_arch_info
= bfd_lookup_arch (bfd_architecture
, 0);
1367 if (bfd_arch_info
== NULL
)
1369 internal_error (__FILE__
, __LINE__
,
1370 _("gdbarch: Attempt to register "
1371 "unknown architecture (%d)"),
1374 /* Check that we haven't seen this architecture before. */
1375 for (curr
= &gdbarch_registry
;
1377 curr
= &(*curr
)->next
)
1379 if (bfd_architecture
== (*curr
)->bfd_architecture
)
1380 internal_error (__FILE__
, __LINE__
,
1381 _("gdbarch: Duplicate registration "
1382 "of architecture (%s)"),
1383 bfd_arch_info
->printable_name
);
1387 fprintf_unfiltered (gdb_stdlog
, "register_gdbarch_init (%s, %s)\n",
1388 bfd_arch_info
->printable_name
,
1389 host_address_to_string (init
));
1391 (*curr
) = XNEW (struct gdbarch_registration
);
1392 (*curr
)->bfd_architecture
= bfd_architecture
;
1393 (*curr
)->init
= init
;
1394 (*curr
)->dump_tdep
= dump_tdep
;
1395 (*curr
)->arches
= NULL
;
1396 (*curr
)->next
= NULL
;
1400 register_gdbarch_init (enum bfd_architecture bfd_architecture
,
1401 gdbarch_init_ftype
*init
)
1403 gdbarch_register (bfd_architecture
, init
, NULL
);
1407 /* Look for an architecture using gdbarch_info. */
1409 struct gdbarch_list
*
1410 gdbarch_list_lookup_by_info (struct gdbarch_list
*arches
,
1411 const struct gdbarch_info
*info
)
1413 for (; arches
!= NULL
; arches
= arches
->next
)
1415 if (info
->bfd_arch_info
!= arches
->gdbarch
->bfd_arch_info
)
1417 if (info
->byte_order
!= arches
->gdbarch
->byte_order
)
1419 if (info
->osabi
!= arches
->gdbarch
->osabi
)
1421 if (info
->target_desc
!= arches
->gdbarch
->target_desc
)
1429 /* Find an architecture that matches the specified INFO. Create a new
1430 architecture if needed. Return that new architecture. */
1433 gdbarch_find_by_info (struct gdbarch_info info
)
1435 struct gdbarch
*new_gdbarch
;
1436 struct gdbarch_registration
*rego
;
1438 /* Fill in missing parts of the INFO struct using a number of
1439 sources: "set ..."; INFOabfd supplied; and the global
1441 gdbarch_info_fill (&info
);
1443 /* Must have found some sort of architecture. */
1444 gdb_assert (info
.bfd_arch_info
!= NULL
);
1448 fprintf_unfiltered (gdb_stdlog
,
1449 "gdbarch_find_by_info: info.bfd_arch_info %s\n",
1450 (info
.bfd_arch_info
!= NULL
1451 ? info
.bfd_arch_info
->printable_name
1453 fprintf_unfiltered (gdb_stdlog
,
1454 "gdbarch_find_by_info: info.byte_order %d (%s)\n",
1456 (info
.byte_order
== BFD_ENDIAN_BIG
? "big"
1457 : info
.byte_order
== BFD_ENDIAN_LITTLE
? "little"
1459 fprintf_unfiltered (gdb_stdlog
,
1460 "gdbarch_find_by_info: info.osabi %d (%s)\n",
1461 info
.osabi
, gdbarch_osabi_name (info
.osabi
));
1462 fprintf_unfiltered (gdb_stdlog
,
1463 "gdbarch_find_by_info: info.abfd %s\n",
1464 host_address_to_string (info
.abfd
));
1467 /* Find the tdep code that knows about this architecture. */
1468 for (rego
= gdbarch_registry
;
1471 if (rego
->bfd_architecture
== info
.bfd_arch_info
->arch
)
1476 fprintf_unfiltered (gdb_stdlog
, "gdbarch_find_by_info: "
1477 "No matching architecture\n");
1481 /* Ask the tdep code for an architecture that matches "info". */
1482 new_gdbarch
= rego
->init (info
, rego
->arches
);
1484 /* Did the tdep code like it? No. Reject the change and revert to
1485 the old architecture. */
1486 if (new_gdbarch
== NULL
)
1489 fprintf_unfiltered (gdb_stdlog
, "gdbarch_find_by_info: "
1490 "Target rejected architecture\n");
1494 /* Is this a pre-existing architecture (as determined by already
1495 being initialized)? Move it to the front of the architecture
1496 list (keeping the list sorted Most Recently Used). */
1497 if (new_gdbarch
->initialized_p
)
1499 struct gdbarch_list
**list
;
1500 struct gdbarch_list
*self
;
1502 fprintf_unfiltered (gdb_stdlog
, "gdbarch_find_by_info: "
1503 "Previous architecture %s (%s) selected\n",
1504 host_address_to_string (new_gdbarch
),
1505 new_gdbarch
->bfd_arch_info
->printable_name
);
1506 /* Find the existing arch in the list. */
1507 for (list
= ®o
->arches
;
1508 (*list
) != NULL
&& (*list
)->gdbarch
!= new_gdbarch
;
1509 list
= &(*list
)->next
);
1510 /* It had better be in the list of architectures. */
1511 gdb_assert ((*list
) != NULL
&& (*list
)->gdbarch
== new_gdbarch
);
1514 (*list
) = self
->next
;
1515 /* Insert SELF at the front. */
1516 self
->next
= rego
->arches
;
1517 rego
->arches
= self
;
1522 /* It's a new architecture. */
1524 fprintf_unfiltered (gdb_stdlog
, "gdbarch_find_by_info: "
1525 "New architecture %s (%s) selected\n",
1526 host_address_to_string (new_gdbarch
),
1527 new_gdbarch
->bfd_arch_info
->printable_name
);
1529 /* Insert the new architecture into the front of the architecture
1530 list (keep the list sorted Most Recently Used). */
1532 struct gdbarch_list
*self
= XNEW (struct gdbarch_list
);
1533 self
->next
= rego
->arches
;
1534 self
->gdbarch
= new_gdbarch
;
1535 rego
->arches
= self
;
1538 /* Check that the newly installed architecture is valid. Plug in
1539 any post init values. */
1540 new_gdbarch
->dump_tdep
= rego
->dump_tdep
;
1541 verify_gdbarch (new_gdbarch
);
1542 new_gdbarch
->initialized_p
= 1;
1545 gdbarch_dump (new_gdbarch
, gdb_stdlog
);
1550 /* Make the specified architecture current. */
1553 set_target_gdbarch (struct gdbarch
*new_gdbarch
)
1555 gdb_assert (new_gdbarch
!= NULL
);
1556 gdb_assert (new_gdbarch
->initialized_p
);
1557 current_inferior ()->gdbarch
= new_gdbarch
;
1558 gdb::observers::architecture_changed
.notify (new_gdbarch
);
1559 registers_changed ();
1562 /* Return the current inferior's arch. */
1565 target_gdbarch (void)
1567 return current_inferior ()->gdbarch
;
1570 void _initialize_gdbarch_utils ();
1572 _initialize_gdbarch_utils ()
1574 add_setshow_enum_cmd ("endian", class_support
,
1575 endian_enum
, &set_endian_string
,
1576 _("Set endianness of target."),
1577 _("Show endianness of target."),
1578 NULL
, set_endian
, show_endian
,
1579 &setlist
, &showlist
);
1580 add_setshow_zuinteger_cmd ("arch", class_maintenance
, &gdbarch_debug
, _("\
1581 Set architecture debugging."), _("\
1582 Show architecture debugging."), _("\
1583 When non-zero, architecture debugging is enabled."),
1586 &setdebuglist
, &showdebuglist
);