1/* Dynamic architecture support for GDB, the GNU debugger.
2
3   Copyright (C) 1998-2020 Free Software Foundation, Inc.
4
5   This file is part of GDB.
6
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.
11
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.
16
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/>.  */
19
20#include "defs.h"
21
22#include "arch-utils.h"
23#include "gdbcmd.h"
24#include "inferior.h"		/* enum CALL_DUMMY_LOCATION et al.  */
25#include "infrun.h"
26#include "regcache.h"
27#include "sim-regno.h"
28#include "gdbcore.h"
29#include "osabi.h"
30#include "target-descriptions.h"
31#include "objfiles.h"
32#include "language.h"
33#include "symtab.h"
34
35#include "gdbsupport/version.h"
36
37#include "floatformat.h"
38
39#include "dis-asm.h"
40
41int
42default_displaced_step_hw_singlestep (struct gdbarch *gdbarch,
43				      struct displaced_step_closure *closure)
44{
45  return !gdbarch_software_single_step_p (gdbarch);
46}
47
48CORE_ADDR
49displaced_step_at_entry_point (struct gdbarch *gdbarch)
50{
51  CORE_ADDR addr;
52  int bp_len;
53
54  addr = entry_point_address ();
55
56  /* Inferior calls also use the entry point as a breakpoint location.
57     We don't want displaced stepping to interfere with those
58     breakpoints, so leave space.  */
59  gdbarch_breakpoint_from_pc (gdbarch, &addr, &bp_len);
60  addr += bp_len * 2;
61
62  return addr;
63}
64
65int
66legacy_register_sim_regno (struct gdbarch *gdbarch, int regnum)
67{
68  /* Only makes sense to supply raw registers.  */
69  gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch));
70  /* NOTE: cagney/2002-05-13: The old code did it this way and it is
71     suspected that some GDB/SIM combinations may rely on this
72     behaviour.  The default should be one2one_register_sim_regno
73     (below).  */
74  if (gdbarch_register_name (gdbarch, regnum) != NULL
75      && gdbarch_register_name (gdbarch, regnum)[0] != '\0')
76    return regnum;
77  else
78    return LEGACY_SIM_REGNO_IGNORE;
79}
80
81CORE_ADDR
82generic_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
83{
84  return 0;
85}
86
87CORE_ADDR
88generic_skip_solib_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
89{
90  return 0;
91}
92
93int
94generic_in_solib_return_trampoline (struct gdbarch *gdbarch,
95				    CORE_ADDR pc, const char *name)
96{
97  return 0;
98}
99
100int
101generic_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
102{
103  return 0;
104}
105
106int
107default_code_of_frame_writable (struct gdbarch *gdbarch,
108				struct frame_info *frame)
109{
110  return 1;
111}
112
113/* Helper functions for gdbarch_inner_than */
114
115int
116core_addr_lessthan (CORE_ADDR lhs, CORE_ADDR rhs)
117{
118  return (lhs < rhs);
119}
120
121int
122core_addr_greaterthan (CORE_ADDR lhs, CORE_ADDR rhs)
123{
124  return (lhs > rhs);
125}
126
127/* Misc helper functions for targets.  */
128
129CORE_ADDR
130core_addr_identity (struct gdbarch *gdbarch, CORE_ADDR addr)
131{
132  return addr;
133}
134
135CORE_ADDR
136convert_from_func_ptr_addr_identity (struct gdbarch *gdbarch, CORE_ADDR addr,
137				     struct target_ops *targ)
138{
139  return addr;
140}
141
142int
143no_op_reg_to_regnum (struct gdbarch *gdbarch, int reg)
144{
145  return reg;
146}
147
148void
149default_coff_make_msymbol_special (int val, struct minimal_symbol *msym)
150{
151  return;
152}
153
154/* See arch-utils.h.  */
155
156void
157default_make_symbol_special (struct symbol *sym, struct objfile *objfile)
158{
159  return;
160}
161
162/* See arch-utils.h.  */
163
164CORE_ADDR
165default_adjust_dwarf2_addr (CORE_ADDR pc)
166{
167  return pc;
168}
169
170/* See arch-utils.h.  */
171
172CORE_ADDR
173default_adjust_dwarf2_line (CORE_ADDR addr, int rel)
174{
175  return addr;
176}
177
178/* See arch-utils.h.  */
179
180bool
181default_execute_dwarf_cfa_vendor_op (struct gdbarch *gdbarch, gdb_byte op,
182				     struct dwarf2_frame_state *fs)
183{
184  return false;
185}
186
187int
188cannot_register_not (struct gdbarch *gdbarch, int regnum)
189{
190  return 0;
191}
192
193/* Legacy version of target_virtual_frame_pointer().  Assumes that
194   there is an gdbarch_deprecated_fp_regnum and that it is the same,
195   cooked or raw.  */
196
197void
198legacy_virtual_frame_pointer (struct gdbarch *gdbarch,
199			      CORE_ADDR pc,
200			      int *frame_regnum,
201			      LONGEST *frame_offset)
202{
203  /* FIXME: cagney/2002-09-13: This code is used when identifying the
204     frame pointer of the current PC.  It is assuming that a single
205     register and an offset can determine this.  I think it should
206     instead generate a byte code expression as that would work better
207     with things like Dwarf2's CFI.  */
208  if (gdbarch_deprecated_fp_regnum (gdbarch) >= 0
209      && gdbarch_deprecated_fp_regnum (gdbarch)
210	   < gdbarch_num_regs (gdbarch))
211    *frame_regnum = gdbarch_deprecated_fp_regnum (gdbarch);
212  else if (gdbarch_sp_regnum (gdbarch) >= 0
213	   && gdbarch_sp_regnum (gdbarch)
214	        < gdbarch_num_regs (gdbarch))
215    *frame_regnum = gdbarch_sp_regnum (gdbarch);
216  else
217    /* Should this be an internal error?  I guess so, it is reflecting
218       an architectural limitation in the current design.  */
219    internal_error (__FILE__, __LINE__,
220		    _("No virtual frame pointer available"));
221  *frame_offset = 0;
222}
223
224/* Return a floating-point format for a floating-point variable of
225   length LEN in bits.  If non-NULL, NAME is the name of its type.
226   If no suitable type is found, return NULL.  */
227
228const struct floatformat **
229default_floatformat_for_type (struct gdbarch *gdbarch,
230			      const char *name, int len)
231{
232  const struct floatformat **format = NULL;
233
234  if (len == gdbarch_half_bit (gdbarch))
235    format = gdbarch_half_format (gdbarch);
236  else if (len == gdbarch_float_bit (gdbarch))
237    format = gdbarch_float_format (gdbarch);
238  else if (len == gdbarch_double_bit (gdbarch))
239    format = gdbarch_double_format (gdbarch);
240  else if (len == gdbarch_long_double_bit (gdbarch))
241    format = gdbarch_long_double_format (gdbarch);
242  /* On i386 the 'long double' type takes 96 bits,
243     while the real number of used bits is only 80,
244     both in processor and in memory.
245     The code below accepts the real bit size.  */
246  else if (gdbarch_long_double_format (gdbarch) != NULL
247	   && len == gdbarch_long_double_format (gdbarch)[0]->totalsize)
248    format = gdbarch_long_double_format (gdbarch);
249
250  return format;
251}
252
253int
254generic_convert_register_p (struct gdbarch *gdbarch, int regnum,
255			    struct type *type)
256{
257  return 0;
258}
259
260int
261default_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
262{
263  return 0;
264}
265
266int
267generic_instruction_nullified (struct gdbarch *gdbarch,
268			       struct regcache *regcache)
269{
270  return 0;
271}
272
273int
274default_remote_register_number (struct gdbarch *gdbarch,
275				int regno)
276{
277  return regno;
278}
279
280/* See arch-utils.h.  */
281
282int
283default_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range)
284{
285  return 0;
286}
287
288
289/* Functions to manipulate the endianness of the target.  */
290
291static enum bfd_endian target_byte_order_user = BFD_ENDIAN_UNKNOWN;
292
293static const char endian_big[] = "big";
294static const char endian_little[] = "little";
295static const char endian_auto[] = "auto";
296static const char *const endian_enum[] =
297{
298  endian_big,
299  endian_little,
300  endian_auto,
301  NULL,
302};
303static const char *set_endian_string;
304
305enum bfd_endian
306selected_byte_order (void)
307{
308  return target_byte_order_user;
309}
310
311/* Called by ``show endian''.  */
312
313static void
314show_endian (struct ui_file *file, int from_tty, struct cmd_list_element *c,
315	     const char *value)
316{
317  if (target_byte_order_user == BFD_ENDIAN_UNKNOWN)
318    if (gdbarch_byte_order (get_current_arch ()) == BFD_ENDIAN_BIG)
319      fprintf_unfiltered (file, _("The target endianness is set automatically "
320				  "(currently big endian).\n"));
321    else
322      fprintf_unfiltered (file, _("The target endianness is set automatically "
323				  "(currently little endian).\n"));
324  else
325    if (target_byte_order_user == BFD_ENDIAN_BIG)
326      fprintf_unfiltered (file,
327			  _("The target is set to big endian.\n"));
328    else
329      fprintf_unfiltered (file,
330			  _("The target is set to little endian.\n"));
331}
332
333static void
334set_endian (const char *ignore_args, int from_tty, struct cmd_list_element *c)
335{
336  struct gdbarch_info info;
337
338  gdbarch_info_init (&info);
339
340  if (set_endian_string == endian_auto)
341    {
342      target_byte_order_user = BFD_ENDIAN_UNKNOWN;
343      if (! gdbarch_update_p (info))
344	internal_error (__FILE__, __LINE__,
345			_("set_endian: architecture update failed"));
346    }
347  else if (set_endian_string == endian_little)
348    {
349      info.byte_order = BFD_ENDIAN_LITTLE;
350      if (! gdbarch_update_p (info))
351	printf_unfiltered (_("Little endian target not supported by GDB\n"));
352      else
353	target_byte_order_user = BFD_ENDIAN_LITTLE;
354    }
355  else if (set_endian_string == endian_big)
356    {
357      info.byte_order = BFD_ENDIAN_BIG;
358      if (! gdbarch_update_p (info))
359	printf_unfiltered (_("Big endian target not supported by GDB\n"));
360      else
361	target_byte_order_user = BFD_ENDIAN_BIG;
362    }
363  else
364    internal_error (__FILE__, __LINE__,
365		    _("set_endian: bad value"));
366
367  show_endian (gdb_stdout, from_tty, NULL, NULL);
368}
369
370/* Given SELECTED, a currently selected BFD architecture, and
371   TARGET_DESC, the current target description, return what
372   architecture to use.
373
374   SELECTED may be NULL, in which case we return the architecture
375   associated with TARGET_DESC.  If SELECTED specifies a variant
376   of the architecture associated with TARGET_DESC, return the
377   more specific of the two.
378
379   If SELECTED is a different architecture, but it is accepted as
380   compatible by the target, we can use the target architecture.
381
382   If SELECTED is obviously incompatible, warn the user.  */
383
384static const struct bfd_arch_info *
385choose_architecture_for_target (const struct target_desc *target_desc,
386				const struct bfd_arch_info *selected)
387{
388  const struct bfd_arch_info *from_target = tdesc_architecture (target_desc);
389  const struct bfd_arch_info *compat1, *compat2;
390
391  if (selected == NULL)
392    return from_target;
393
394  if (from_target == NULL)
395    return selected;
396
397  /* struct bfd_arch_info objects are singletons: that is, there's
398     supposed to be exactly one instance for a given machine.  So you
399     can tell whether two are equivalent by comparing pointers.  */
400  if (from_target == selected)
401    return selected;
402
403  /* BFD's 'A->compatible (A, B)' functions return zero if A and B are
404     incompatible.  But if they are compatible, it returns the 'more
405     featureful' of the two arches.  That is, if A can run code
406     written for B, but B can't run code written for A, then it'll
407     return A.
408
409     Some targets (e.g. MIPS as of 2006-12-04) don't fully
410     implement this, instead always returning NULL or the first
411     argument.  We detect that case by checking both directions.  */
412
413  compat1 = selected->compatible (selected, from_target);
414  compat2 = from_target->compatible (from_target, selected);
415
416  if (compat1 == NULL && compat2 == NULL)
417    {
418      /* BFD considers the architectures incompatible.  Check our
419	 target description whether it accepts SELECTED as compatible
420	 anyway.  */
421      if (tdesc_compatible_p (target_desc, selected))
422	return from_target;
423
424      warning (_("Selected architecture %s is not compatible "
425		 "with reported target architecture %s"),
426	       selected->printable_name, from_target->printable_name);
427      return selected;
428    }
429
430  if (compat1 == NULL)
431    return compat2;
432  if (compat2 == NULL)
433    return compat1;
434  if (compat1 == compat2)
435    return compat1;
436
437  /* If the two didn't match, but one of them was a default
438     architecture, assume the more specific one is correct.  This
439     handles the case where an executable or target description just
440     says "mips", but the other knows which MIPS variant.  */
441  if (compat1->the_default)
442    return compat2;
443  if (compat2->the_default)
444    return compat1;
445
446  /* We have no idea which one is better.  This is a bug, but not
447     a critical problem; warn the user.  */
448  warning (_("Selected architecture %s is ambiguous with "
449	     "reported target architecture %s"),
450	   selected->printable_name, from_target->printable_name);
451  return selected;
452}
453
454/* Functions to manipulate the architecture of the target.  */
455
456enum set_arch { set_arch_auto, set_arch_manual };
457
458static const struct bfd_arch_info *target_architecture_user;
459
460static const char *set_architecture_string;
461
462const char *
463selected_architecture_name (void)
464{
465  if (target_architecture_user == NULL)
466    return NULL;
467  else
468    return set_architecture_string;
469}
470
471/* Called if the user enters ``show architecture'' without an
472   argument.  */
473
474static void
475show_architecture (struct ui_file *file, int from_tty,
476		   struct cmd_list_element *c, const char *value)
477{
478  if (target_architecture_user == NULL)
479    fprintf_filtered (file, _("The target architecture is set to "
480			      "\"auto\" (currently \"%s\").\n"),
481		      gdbarch_bfd_arch_info (get_current_arch ())->printable_name);
482  else
483    fprintf_filtered (file, _("The target architecture is set to \"%s\".\n"),
484		      set_architecture_string);
485}
486
487
488/* Called if the user enters ``set architecture'' with or without an
489   argument.  */
490
491static void
492set_architecture (const char *ignore_args,
493		  int from_tty, struct cmd_list_element *c)
494{
495  struct gdbarch_info info;
496
497  gdbarch_info_init (&info);
498
499  if (strcmp (set_architecture_string, "auto") == 0)
500    {
501      target_architecture_user = NULL;
502      if (!gdbarch_update_p (info))
503	internal_error (__FILE__, __LINE__,
504			_("could not select an architecture automatically"));
505    }
506  else
507    {
508      info.bfd_arch_info = bfd_scan_arch (set_architecture_string);
509      if (info.bfd_arch_info == NULL)
510	internal_error (__FILE__, __LINE__,
511			_("set_architecture: bfd_scan_arch failed"));
512      if (gdbarch_update_p (info))
513	target_architecture_user = info.bfd_arch_info;
514      else
515	printf_unfiltered (_("Architecture `%s' not recognized.\n"),
516			   set_architecture_string);
517    }
518  show_architecture (gdb_stdout, from_tty, NULL, NULL);
519}
520
521/* Try to select a global architecture that matches "info".  Return
522   non-zero if the attempt succeeds.  */
523int
524gdbarch_update_p (struct gdbarch_info info)
525{
526  struct gdbarch *new_gdbarch;
527
528  /* Check for the current file.  */
529  if (info.abfd == NULL)
530    info.abfd = exec_bfd;
531  if (info.abfd == NULL)
532    info.abfd = core_bfd;
533
534  /* Check for the current target description.  */
535  if (info.target_desc == NULL)
536    info.target_desc = target_current_description ();
537
538  new_gdbarch = gdbarch_find_by_info (info);
539
540  /* If there no architecture by that name, reject the request.  */
541  if (new_gdbarch == NULL)
542    {
543      if (gdbarch_debug)
544	fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
545			    "Architecture not found\n");
546      return 0;
547    }
548
549  /* If it is the same old architecture, accept the request (but don't
550     swap anything).  */
551  if (new_gdbarch == target_gdbarch ())
552    {
553      if (gdbarch_debug)
554	fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
555			    "Architecture %s (%s) unchanged\n",
556			    host_address_to_string (new_gdbarch),
557			    gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
558      return 1;
559    }
560
561  /* It's a new architecture, swap it in.  */
562  if (gdbarch_debug)
563    fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
564			"New architecture %s (%s) selected\n",
565			host_address_to_string (new_gdbarch),
566			gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
567  set_target_gdbarch (new_gdbarch);
568
569  return 1;
570}
571
572/* Return the architecture for ABFD.  If no suitable architecture
573   could be find, return NULL.  */
574
575struct gdbarch *
576gdbarch_from_bfd (bfd *abfd)
577{
578  struct gdbarch_info info;
579  gdbarch_info_init (&info);
580
581  info.abfd = abfd;
582  return gdbarch_find_by_info (info);
583}
584
585/* Set the dynamic target-system-dependent parameters (architecture,
586   byte-order) using information found in the BFD */
587
588void
589set_gdbarch_from_file (bfd *abfd)
590{
591  struct gdbarch_info info;
592  struct gdbarch *gdbarch;
593
594  gdbarch_info_init (&info);
595  info.abfd = abfd;
596  info.target_desc = target_current_description ();
597  gdbarch = gdbarch_find_by_info (info);
598
599  if (gdbarch == NULL)
600    error (_("Architecture of file not recognized."));
601  set_target_gdbarch (gdbarch);
602}
603
604/* Initialize the current architecture.  Update the ``set
605   architecture'' command so that it specifies a list of valid
606   architectures.  */
607
608#ifdef DEFAULT_BFD_ARCH
609extern const bfd_arch_info_type DEFAULT_BFD_ARCH;
610static const bfd_arch_info_type *default_bfd_arch = &DEFAULT_BFD_ARCH;
611#else
612static const bfd_arch_info_type *default_bfd_arch;
613#endif
614
615#ifdef DEFAULT_BFD_VEC
616extern const bfd_target DEFAULT_BFD_VEC;
617static const bfd_target *default_bfd_vec = &DEFAULT_BFD_VEC;
618#else
619static const bfd_target *default_bfd_vec;
620#endif
621
622static enum bfd_endian default_byte_order = BFD_ENDIAN_UNKNOWN;
623
624void
625initialize_current_architecture (void)
626{
627  const char **arches = gdbarch_printable_names ();
628  struct gdbarch_info info;
629
630  /* determine a default architecture and byte order.  */
631  gdbarch_info_init (&info);
632
633  /* Find a default architecture.  */
634  if (default_bfd_arch == NULL)
635    {
636      /* Choose the architecture by taking the first one
637	 alphabetically.  */
638      const char *chosen = arches[0];
639      const char **arch;
640      for (arch = arches; *arch != NULL; arch++)
641	{
642	  if (strcmp (*arch, chosen) < 0)
643	    chosen = *arch;
644	}
645      if (chosen == NULL)
646	internal_error (__FILE__, __LINE__,
647			_("initialize_current_architecture: No arch"));
648      default_bfd_arch = bfd_scan_arch (chosen);
649      if (default_bfd_arch == NULL)
650	internal_error (__FILE__, __LINE__,
651			_("initialize_current_architecture: Arch not found"));
652    }
653
654  info.bfd_arch_info = default_bfd_arch;
655
656  /* Take several guesses at a byte order.  */
657  if (default_byte_order == BFD_ENDIAN_UNKNOWN
658      && default_bfd_vec != NULL)
659    {
660      /* Extract BFD's default vector's byte order.  */
661      switch (default_bfd_vec->byteorder)
662	{
663	case BFD_ENDIAN_BIG:
664	  default_byte_order = BFD_ENDIAN_BIG;
665	  break;
666	case BFD_ENDIAN_LITTLE:
667	  default_byte_order = BFD_ENDIAN_LITTLE;
668	  break;
669	default:
670	  break;
671	}
672    }
673  if (default_byte_order == BFD_ENDIAN_UNKNOWN)
674    {
675      /* look for ``*el-*'' in the target name.  */
676      const char *chp;
677      chp = strchr (target_name, '-');
678      if (chp != NULL
679	  && chp - 2 >= target_name
680	  && startswith (chp - 2, "el"))
681	default_byte_order = BFD_ENDIAN_LITTLE;
682    }
683  if (default_byte_order == BFD_ENDIAN_UNKNOWN)
684    {
685      /* Wire it to big-endian!!! */
686      default_byte_order = BFD_ENDIAN_BIG;
687    }
688
689  info.byte_order = default_byte_order;
690  info.byte_order_for_code = info.byte_order;
691
692  if (! gdbarch_update_p (info))
693    internal_error (__FILE__, __LINE__,
694		    _("initialize_current_architecture: Selection of "
695		      "initial architecture failed"));
696
697  /* Create the ``set architecture'' command appending ``auto'' to the
698     list of architectures.  */
699  {
700    /* Append ``auto''.  */
701    int nr;
702    for (nr = 0; arches[nr] != NULL; nr++);
703    arches = XRESIZEVEC (const char *, arches, nr + 2);
704    arches[nr + 0] = "auto";
705    arches[nr + 1] = NULL;
706    add_setshow_enum_cmd ("architecture", class_support,
707			  arches, &set_architecture_string,
708			  _("Set architecture of target."),
709			  _("Show architecture of target."), NULL,
710			  set_architecture, show_architecture,
711			  &setlist, &showlist);
712    add_alias_cmd ("processor", "architecture", class_support, 1, &setlist);
713  }
714}
715
716
717/* Initialize a gdbarch info to values that will be automatically
718   overridden.  Note: Originally, this ``struct info'' was initialized
719   using memset(0).  Unfortunately, that ran into problems, namely
720   BFD_ENDIAN_BIG is zero.  An explicit initialization function that
721   can explicitly set each field to a well defined value is used.  */
722
723void
724gdbarch_info_init (struct gdbarch_info *info)
725{
726  memset (info, 0, sizeof (struct gdbarch_info));
727  info->byte_order = BFD_ENDIAN_UNKNOWN;
728  info->byte_order_for_code = info->byte_order;
729}
730
731/* Similar to init, but this time fill in the blanks.  Information is
732   obtained from the global "set ..." options and explicitly
733   initialized INFO fields.  */
734
735void
736gdbarch_info_fill (struct gdbarch_info *info)
737{
738  /* "(gdb) set architecture ...".  */
739  if (info->bfd_arch_info == NULL
740      && target_architecture_user)
741    info->bfd_arch_info = target_architecture_user;
742  /* From the file.  */
743  if (info->bfd_arch_info == NULL
744      && info->abfd != NULL
745      && bfd_get_arch (info->abfd) != bfd_arch_unknown
746      && bfd_get_arch (info->abfd) != bfd_arch_obscure)
747    info->bfd_arch_info = bfd_get_arch_info (info->abfd);
748  /* From the target.  */
749  if (info->target_desc != NULL)
750    info->bfd_arch_info = choose_architecture_for_target
751			   (info->target_desc, info->bfd_arch_info);
752  /* From the default.  */
753  if (info->bfd_arch_info == NULL)
754    info->bfd_arch_info = default_bfd_arch;
755
756  /* "(gdb) set byte-order ...".  */
757  if (info->byte_order == BFD_ENDIAN_UNKNOWN
758      && target_byte_order_user != BFD_ENDIAN_UNKNOWN)
759    info->byte_order = target_byte_order_user;
760  /* From the INFO struct.  */
761  if (info->byte_order == BFD_ENDIAN_UNKNOWN
762      && info->abfd != NULL)
763    info->byte_order = (bfd_big_endian (info->abfd) ? BFD_ENDIAN_BIG
764			: bfd_little_endian (info->abfd) ? BFD_ENDIAN_LITTLE
765			: BFD_ENDIAN_UNKNOWN);
766  /* From the default.  */
767  if (info->byte_order == BFD_ENDIAN_UNKNOWN)
768    info->byte_order = default_byte_order;
769  info->byte_order_for_code = info->byte_order;
770  /* Wire the default to the last selected byte order.  */
771  default_byte_order = info->byte_order;
772
773  /* "(gdb) set osabi ...".  Handled by gdbarch_lookup_osabi.  */
774  /* From the manual override, or from file.  */
775  if (info->osabi == GDB_OSABI_UNKNOWN)
776    info->osabi = gdbarch_lookup_osabi (info->abfd);
777  /* From the target.  */
778
779  if (info->osabi == GDB_OSABI_UNKNOWN && info->target_desc != NULL)
780    info->osabi = tdesc_osabi (info->target_desc);
781  /* From the configured default.  */
782#ifdef GDB_OSABI_DEFAULT
783  if (info->osabi == GDB_OSABI_UNKNOWN)
784    info->osabi = GDB_OSABI_DEFAULT;
785#endif
786  /* If we still don't know which osabi to pick, pick none.  */
787  if (info->osabi == GDB_OSABI_UNKNOWN)
788    info->osabi = GDB_OSABI_NONE;
789
790  /* Must have at least filled in the architecture.  */
791  gdb_assert (info->bfd_arch_info != NULL);
792}
793
794/* Return "current" architecture.  If the target is running, this is
795   the architecture of the selected frame.  Otherwise, the "current"
796   architecture defaults to the target architecture.
797
798   This function should normally be called solely by the command
799   interpreter routines to determine the architecture to execute a
800   command in.  */
801struct gdbarch *
802get_current_arch (void)
803{
804  if (has_stack_frames ())
805    return get_frame_arch (get_selected_frame (NULL));
806  else
807    return target_gdbarch ();
808}
809
810int
811default_has_shared_address_space (struct gdbarch *gdbarch)
812{
813  /* Simply say no.  In most unix-like targets each inferior/process
814     has its own address space.  */
815  return 0;
816}
817
818int
819default_fast_tracepoint_valid_at (struct gdbarch *gdbarch, CORE_ADDR addr,
820				  std::string *msg)
821{
822  /* We don't know if maybe the target has some way to do fast
823     tracepoints that doesn't need gdbarch, so always say yes.  */
824  if (msg)
825    msg->clear ();
826  return 1;
827}
828
829const gdb_byte *
830default_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
831			    int *lenptr)
832{
833  int kind = gdbarch_breakpoint_kind_from_pc (gdbarch, pcptr);
834
835  return gdbarch_sw_breakpoint_from_kind (gdbarch, kind, lenptr);
836}
837int
838default_breakpoint_kind_from_current_state (struct gdbarch *gdbarch,
839					    struct regcache *regcache,
840					    CORE_ADDR *pcptr)
841{
842  return gdbarch_breakpoint_kind_from_pc (gdbarch, pcptr);
843}
844
845
846void
847default_gen_return_address (struct gdbarch *gdbarch,
848			    struct agent_expr *ax, struct axs_value *value,
849			    CORE_ADDR scope)
850{
851  error (_("This architecture has no method to collect a return address."));
852}
853
854int
855default_return_in_first_hidden_param_p (struct gdbarch *gdbarch,
856					struct type *type)
857{
858  /* Usually, the return value's address is stored the in the "first hidden"
859     parameter if the return value should be passed by reference, as
860     specified in ABI.  */
861  return !(language_pass_by_reference (type).trivially_copyable);
862}
863
864int default_insn_is_call (struct gdbarch *gdbarch, CORE_ADDR addr)
865{
866  return 0;
867}
868
869int default_insn_is_ret (struct gdbarch *gdbarch, CORE_ADDR addr)
870{
871  return 0;
872}
873
874int default_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
875{
876  return 0;
877}
878
879/*  See arch-utils.h.  */
880
881bool
882default_program_breakpoint_here_p (struct gdbarch *gdbarch,
883				   CORE_ADDR address)
884{
885  int len;
886  const gdb_byte *bpoint = gdbarch_breakpoint_from_pc (gdbarch, &address, &len);
887
888  /* Software breakpoints unsupported?  */
889  if (bpoint == nullptr)
890    return false;
891
892  gdb_byte *target_mem = (gdb_byte *) alloca (len);
893
894  /* Enable the automatic memory restoration from breakpoints while
895     we read the memory.  Otherwise we may find temporary breakpoints, ones
896     inserted by GDB, and flag them as permanent breakpoints.  */
897  scoped_restore restore_memory
898    = make_scoped_restore_show_memory_breakpoints (0);
899
900  if (target_read_memory (address, target_mem, len) == 0)
901    {
902      /* Check if this is a breakpoint instruction for this architecture,
903	 including ones used by GDB.  */
904      if (memcmp (target_mem, bpoint, len) == 0)
905	return true;
906    }
907
908  return false;
909}
910
911void
912default_skip_permanent_breakpoint (struct regcache *regcache)
913{
914  struct gdbarch *gdbarch = regcache->arch ();
915  CORE_ADDR current_pc = regcache_read_pc (regcache);
916  int bp_len;
917
918  gdbarch_breakpoint_from_pc (gdbarch, &current_pc, &bp_len);
919  current_pc += bp_len;
920  regcache_write_pc (regcache, current_pc);
921}
922
923CORE_ADDR
924default_infcall_mmap (CORE_ADDR size, unsigned prot)
925{
926  error (_("This target does not support inferior memory allocation by mmap."));
927}
928
929void
930default_infcall_munmap (CORE_ADDR addr, CORE_ADDR size)
931{
932  /* Memory reserved by inferior mmap is kept leaked.  */
933}
934
935/* -mcmodel=large is used so that no GOT (Global Offset Table) is needed to be
936   created in inferior memory by GDB (normally it is set by ld.so).  */
937
938std::string
939default_gcc_target_options (struct gdbarch *gdbarch)
940{
941  return string_printf ("-m%d%s", gdbarch_ptr_bit (gdbarch),
942			(gdbarch_ptr_bit (gdbarch) == 64
943			 ? " -mcmodel=large" : ""));
944}
945
946/* gdbarch gnu_triplet_regexp method.  */
947
948const char *
949default_gnu_triplet_regexp (struct gdbarch *gdbarch)
950{
951  return gdbarch_bfd_arch_info (gdbarch)->arch_name;
952}
953
954/* Default method for gdbarch_addressable_memory_unit_size.  By default, a memory byte has
955   a size of 1 octet.  */
956
957int
958default_addressable_memory_unit_size (struct gdbarch *gdbarch)
959{
960  return 1;
961}
962
963void
964default_guess_tracepoint_registers (struct gdbarch *gdbarch,
965				    struct regcache *regcache,
966				    CORE_ADDR addr)
967{
968  int pc_regno = gdbarch_pc_regnum (gdbarch);
969  gdb_byte *regs;
970
971  /* This guessing code below only works if the PC register isn't
972     a pseudo-register.  The value of a pseudo-register isn't stored
973     in the (non-readonly) regcache -- instead it's recomputed
974     (probably from some other cached raw register) whenever the
975     register is read.  In this case, a custom method implementation
976     should be used by the architecture.  */
977  if (pc_regno < 0 || pc_regno >= gdbarch_num_regs (gdbarch))
978    return;
979
980  regs = (gdb_byte *) alloca (register_size (gdbarch, pc_regno));
981  store_unsigned_integer (regs, register_size (gdbarch, pc_regno),
982			  gdbarch_byte_order (gdbarch), addr);
983  regcache->raw_supply (pc_regno, regs);
984}
985
986int
987default_print_insn (bfd_vma memaddr, disassemble_info *info)
988{
989  disassembler_ftype disassemble_fn;
990
991  disassemble_fn = disassembler (info->arch, info->endian == BFD_ENDIAN_BIG,
992				 info->mach, exec_bfd);
993
994  gdb_assert (disassemble_fn != NULL);
995  return (*disassemble_fn) (memaddr, info);
996}
997
998/* See arch-utils.h.  */
999
1000CORE_ADDR
1001gdbarch_skip_prologue_noexcept (gdbarch *gdbarch, CORE_ADDR pc) noexcept
1002{
1003  CORE_ADDR new_pc = pc;
1004
1005  try
1006    {
1007      new_pc = gdbarch_skip_prologue (gdbarch, pc);
1008    }
1009  catch (const gdb_exception &ex)
1010    {}
1011
1012  return new_pc;
1013}
1014
1015/* See arch-utils.h.  */
1016
1017bool
1018default_in_indirect_branch_thunk (gdbarch *gdbarch, CORE_ADDR pc)
1019{
1020  return false;
1021}
1022
1023/* See arch-utils.h.  */
1024
1025ULONGEST
1026default_type_align (struct gdbarch *gdbarch, struct type *type)
1027{
1028  return 0;
1029}
1030
1031/* See arch-utils.h.  */
1032
1033std::string
1034default_get_pc_address_flags (frame_info *frame, CORE_ADDR pc)
1035{
1036  return "";
1037}
1038
1039/* See arch-utils.h.  */
1040void
1041default_read_core_file_mappings (struct gdbarch *gdbarch,
1042                                 struct bfd *cbfd,
1043				 gdb::function_view<void (ULONGEST count)>
1044				   pre_loop_cb,
1045				 gdb::function_view<void (int num,
1046				                          ULONGEST start,
1047							  ULONGEST end,
1048							  ULONGEST file_ofs,
1049							  const char *filename,
1050							  const void *other)>
1051				   loop_cb)
1052{
1053}
1054
1055void _initialize_gdbarch_utils ();
1056void
1057_initialize_gdbarch_utils ()
1058{
1059  add_setshow_enum_cmd ("endian", class_support,
1060			endian_enum, &set_endian_string,
1061			_("Set endianness of target."),
1062			_("Show endianness of target."),
1063			NULL, set_endian, show_endian,
1064			&setlist, &showlist);
1065}
1066