Handle biased types
[external/binutils.git] / gdb / arch-utils.c
1 /* Dynamic architecture support for GDB, the GNU debugger.
2
3    Copyright (C) 1998-2019 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
41 int
42 default_displaced_step_hw_singlestep (struct gdbarch *gdbarch,
43                                       struct displaced_step_closure *closure)
44 {
45   return !gdbarch_software_single_step_p (gdbarch);
46 }
47
48 CORE_ADDR
49 displaced_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
65 int
66 legacy_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      behavour.  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
81 CORE_ADDR
82 generic_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
83 {
84   return 0;
85 }
86
87 CORE_ADDR
88 generic_skip_solib_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
89 {
90   return 0;
91 }
92
93 int
94 generic_in_solib_return_trampoline (struct gdbarch *gdbarch,
95                                     CORE_ADDR pc, const char *name)
96 {
97   return 0;
98 }
99
100 int
101 generic_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
102 {
103   return 0;
104 }
105
106 int
107 default_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
115 int
116 core_addr_lessthan (CORE_ADDR lhs, CORE_ADDR rhs)
117 {
118   return (lhs < rhs);
119 }
120
121 int
122 core_addr_greaterthan (CORE_ADDR lhs, CORE_ADDR rhs)
123 {
124   return (lhs > rhs);
125 }
126
127 /* Misc helper functions for targets.  */
128
129 CORE_ADDR
130 core_addr_identity (struct gdbarch *gdbarch, CORE_ADDR addr)
131 {
132   return addr;
133 }
134
135 CORE_ADDR
136 convert_from_func_ptr_addr_identity (struct gdbarch *gdbarch, CORE_ADDR addr,
137                                      struct target_ops *targ)
138 {
139   return addr;
140 }
141
142 int
143 no_op_reg_to_regnum (struct gdbarch *gdbarch, int reg)
144 {
145   return reg;
146 }
147
148 void
149 default_coff_make_msymbol_special (int val, struct minimal_symbol *msym)
150 {
151   return;
152 }
153
154 /* See arch-utils.h.  */
155
156 void
157 default_make_symbol_special (struct symbol *sym, struct objfile *objfile)
158 {
159   return;
160 }
161
162 /* See arch-utils.h.  */
163
164 CORE_ADDR
165 default_adjust_dwarf2_addr (CORE_ADDR pc)
166 {
167   return pc;
168 }
169
170 /* See arch-utils.h.  */
171
172 CORE_ADDR
173 default_adjust_dwarf2_line (CORE_ADDR addr, int rel)
174 {
175   return addr;
176 }
177
178 /* See arch-utils.h.  */
179
180 bool
181 default_execute_dwarf_cfa_vendor_op (struct gdbarch *gdbarch, gdb_byte op,
182                                      struct dwarf2_frame_state *fs)
183 {
184   return false;
185 }
186
187 int
188 cannot_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
197 void
198 legacy_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
228 const struct floatformat **
229 default_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 \f
253 int
254 generic_convert_register_p (struct gdbarch *gdbarch, int regnum,
255                             struct type *type)
256 {
257   return 0;
258 }
259
260 int
261 default_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
262 {
263   return 0;
264 }
265
266 int
267 generic_instruction_nullified (struct gdbarch *gdbarch,
268                                struct regcache *regcache)
269 {
270   return 0;
271 }
272
273 int
274 default_remote_register_number (struct gdbarch *gdbarch,
275                                 int regno)
276 {
277   return regno;
278 }
279
280 /* See arch-utils.h.  */
281
282 int
283 default_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range)
284 {
285   return 0;
286 }
287
288 \f
289 /* Functions to manipulate the endianness of the target.  */
290
291 static enum bfd_endian target_byte_order_user = BFD_ENDIAN_UNKNOWN;
292
293 static const char endian_big[] = "big";
294 static const char endian_little[] = "little";
295 static const char endian_auto[] = "auto";
296 static const char *const endian_enum[] =
297 {
298   endian_big,
299   endian_little,
300   endian_auto,
301   NULL,
302 };
303 static const char *set_endian_string;
304
305 enum bfd_endian
306 selected_byte_order (void)
307 {
308   return target_byte_order_user;
309 }
310
311 /* Called by ``show endian''.  */
312
313 static void
314 show_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 assumed to be big endian\n"));
328     else
329       fprintf_unfiltered (file,
330                           _("The target is assumed to be little endian\n"));
331 }
332
333 static void
334 set_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 associtated 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
384 static const struct bfd_arch_info *
385 choose_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
456 enum set_arch { set_arch_auto, set_arch_manual };
457
458 static const struct bfd_arch_info *target_architecture_user;
459
460 static const char *set_architecture_string;
461
462 const char *
463 selected_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
474 static void
475 show_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 "
480                               "automatically (currently %s)\n"),
481                       gdbarch_bfd_arch_info (get_current_arch ())->printable_name);
482   else
483     fprintf_filtered (file, _("The target architecture is assumed to be %s\n"),
484                       set_architecture_string);
485 }
486
487
488 /* Called if the user enters ``set architecture'' with or without an
489    argument.  */
490
491 static void
492 set_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.  */
523 int
524 gdbarch_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
575 struct gdbarch *
576 gdbarch_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
588 void
589 set_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
609 extern const bfd_arch_info_type DEFAULT_BFD_ARCH;
610 static const bfd_arch_info_type *default_bfd_arch = &DEFAULT_BFD_ARCH;
611 #else
612 static const bfd_arch_info_type *default_bfd_arch;
613 #endif
614
615 #ifdef DEFAULT_BFD_VEC
616 extern const bfd_target DEFAULT_BFD_VEC;
617 static const bfd_target *default_bfd_vec = &DEFAULT_BFD_VEC;
618 #else
619 static const bfd_target *default_bfd_vec;
620 #endif
621
622 static enum bfd_endian default_byte_order = BFD_ENDIAN_UNKNOWN;
623
624 void
625 initialize_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
723 void
724 gdbarch_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
735 void
736 gdbarch_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.  */
801 struct gdbarch *
802 get_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
810 int
811 default_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
818 int
819 default_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
829 const gdb_byte *
830 default_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 }
837 int
838 default_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
846 void
847 default_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
854 int
855 default_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);
862 }
863
864 int default_insn_is_call (struct gdbarch *gdbarch, CORE_ADDR addr)
865 {
866   return 0;
867 }
868
869 int default_insn_is_ret (struct gdbarch *gdbarch, CORE_ADDR addr)
870 {
871   return 0;
872 }
873
874 int default_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
875 {
876   return 0;
877 }
878
879 void
880 default_skip_permanent_breakpoint (struct regcache *regcache)
881 {
882   struct gdbarch *gdbarch = regcache->arch ();
883   CORE_ADDR current_pc = regcache_read_pc (regcache);
884   int bp_len;
885
886   gdbarch_breakpoint_from_pc (gdbarch, &current_pc, &bp_len);
887   current_pc += bp_len;
888   regcache_write_pc (regcache, current_pc);
889 }
890
891 CORE_ADDR
892 default_infcall_mmap (CORE_ADDR size, unsigned prot)
893 {
894   error (_("This target does not support inferior memory allocation by mmap."));
895 }
896
897 void
898 default_infcall_munmap (CORE_ADDR addr, CORE_ADDR size)
899 {
900   /* Memory reserved by inferior mmap is kept leaked.  */
901 }
902
903 /* -mcmodel=large is used so that no GOT (Global Offset Table) is needed to be
904    created in inferior memory by GDB (normally it is set by ld.so).  */
905
906 char *
907 default_gcc_target_options (struct gdbarch *gdbarch)
908 {
909   return xstrprintf ("-m%d%s", gdbarch_ptr_bit (gdbarch),
910                      gdbarch_ptr_bit (gdbarch) == 64 ? " -mcmodel=large" : "");
911 }
912
913 /* gdbarch gnu_triplet_regexp method.  */
914
915 const char *
916 default_gnu_triplet_regexp (struct gdbarch *gdbarch)
917 {
918   return gdbarch_bfd_arch_info (gdbarch)->arch_name;
919 }
920
921 /* Default method for gdbarch_addressable_memory_unit_size.  By default, a memory byte has
922    a size of 1 octet.  */
923
924 int
925 default_addressable_memory_unit_size (struct gdbarch *gdbarch)
926 {
927   return 1;
928 }
929
930 void
931 default_guess_tracepoint_registers (struct gdbarch *gdbarch,
932                                     struct regcache *regcache,
933                                     CORE_ADDR addr)
934 {
935   int pc_regno = gdbarch_pc_regnum (gdbarch);
936   gdb_byte *regs;
937
938   /* This guessing code below only works if the PC register isn't
939      a pseudo-register.  The value of a pseudo-register isn't stored
940      in the (non-readonly) regcache -- instead it's recomputed
941      (probably from some other cached raw register) whenever the
942      register is read.  In this case, a custom method implementation
943      should be used by the architecture.  */
944   if (pc_regno < 0 || pc_regno >= gdbarch_num_regs (gdbarch))
945     return;
946
947   regs = (gdb_byte *) alloca (register_size (gdbarch, pc_regno));
948   store_unsigned_integer (regs, register_size (gdbarch, pc_regno),
949                           gdbarch_byte_order (gdbarch), addr);
950   regcache->raw_supply (pc_regno, regs);
951 }
952
953 int
954 default_print_insn (bfd_vma memaddr, disassemble_info *info)
955 {
956   disassembler_ftype disassemble_fn;
957
958   disassemble_fn = disassembler (info->arch, info->endian == BFD_ENDIAN_BIG,
959                                  info->mach, exec_bfd);
960
961   gdb_assert (disassemble_fn != NULL);
962   return (*disassemble_fn) (memaddr, info);
963 }
964
965 /* See arch-utils.h.  */
966
967 CORE_ADDR
968 gdbarch_skip_prologue_noexcept (gdbarch *gdbarch, CORE_ADDR pc) noexcept
969 {
970   CORE_ADDR new_pc = pc;
971
972   try
973     {
974       new_pc = gdbarch_skip_prologue (gdbarch, pc);
975     }
976   catch (const gdb_exception &ex)
977     {}
978
979   return new_pc;
980 }
981
982 /* See arch-utils.h.  */
983
984 bool
985 default_in_indirect_branch_thunk (gdbarch *gdbarch, CORE_ADDR pc)
986 {
987   return false;
988 }
989
990 /* See arch-utils.h.  */
991
992 ULONGEST
993 default_type_align (struct gdbarch *gdbarch, struct type *type)
994 {
995   return 0;
996 }
997
998 /* See arch-utils.h.  */
999
1000 std::string
1001 default_get_pc_address_flags (frame_info *frame, CORE_ADDR pc)
1002 {
1003   return "";
1004 }
1005
1006 void
1007 _initialize_gdbarch_utils (void)
1008 {
1009   add_setshow_enum_cmd ("endian", class_support,
1010                         endian_enum, &set_endian_string, 
1011                         _("Set endianness of target."),
1012                         _("Show endianness of target."),
1013                         NULL, set_endian, show_endian,
1014                         &setlist, &showlist);
1015 }