Home | History | Annotate | Line # | Download | only in gdb
      1   1.1  christos /* Dynamic architecture support for GDB, the GNU debugger.
      2   1.1  christos 
      3  1.11  christos    Copyright (C) 1998-2024 Free Software Foundation, Inc.
      4   1.1  christos 
      5   1.1  christos    This file is part of GDB.
      6   1.1  christos 
      7   1.1  christos    This program is free software; you can redistribute it and/or modify
      8   1.1  christos    it under the terms of the GNU General Public License as published by
      9   1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10   1.1  christos    (at your option) any later version.
     11   1.1  christos 
     12   1.1  christos    This program is distributed in the hope that it will be useful,
     13   1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14   1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15   1.1  christos    GNU General Public License for more details.
     16   1.1  christos 
     17   1.1  christos    You should have received a copy of the GNU General Public License
     18   1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19   1.1  christos 
     20   1.1  christos 
     21   1.1  christos #include "arch-utils.h"
     22  1.11  christos #include "extract-store-integer.h"
     23  1.11  christos #include "cli/cli-cmds.h"
     24  1.11  christos #include "inferior.h"
     25   1.3  christos #include "infrun.h"
     26   1.1  christos #include "regcache.h"
     27   1.1  christos #include "sim-regno.h"
     28   1.1  christos #include "gdbcore.h"
     29   1.1  christos #include "osabi.h"
     30   1.1  christos #include "target-descriptions.h"
     31   1.1  christos #include "objfiles.h"
     32   1.1  christos #include "language.h"
     33   1.3  christos #include "symtab.h"
     34  1.10  christos #include "dummy-frame.h"
     35  1.10  christos #include "frame-unwind.h"
     36  1.10  christos #include "reggroups.h"
     37  1.10  christos #include "auxv.h"
     38  1.10  christos #include "observable.h"
     39  1.10  christos #include "solib-target.h"
     40  1.12  christos #include "event-top.h"
     41   1.1  christos 
     42   1.9  christos #include "gdbsupport/version.h"
     43   1.1  christos 
     44   1.1  christos #include "floatformat.h"
     45   1.1  christos 
     46   1.8  christos #include "dis-asm.h"
     47   1.1  christos 
     48  1.10  christos bool
     49  1.10  christos default_displaced_step_hw_singlestep (struct gdbarch *gdbarch)
     50   1.1  christos {
     51   1.1  christos   return !gdbarch_software_single_step_p (gdbarch);
     52   1.1  christos }
     53   1.1  christos 
     54   1.1  christos CORE_ADDR
     55   1.1  christos displaced_step_at_entry_point (struct gdbarch *gdbarch)
     56   1.1  christos {
     57   1.1  christos   CORE_ADDR addr;
     58   1.1  christos   int bp_len;
     59   1.1  christos 
     60  1.12  christos   addr = entry_point_address (current_program_space);
     61   1.1  christos 
     62   1.1  christos   /* Inferior calls also use the entry point as a breakpoint location.
     63   1.1  christos      We don't want displaced stepping to interfere with those
     64   1.1  christos      breakpoints, so leave space.  */
     65   1.1  christos   gdbarch_breakpoint_from_pc (gdbarch, &addr, &bp_len);
     66   1.1  christos   addr += bp_len * 2;
     67   1.1  christos 
     68   1.1  christos   return addr;
     69   1.1  christos }
     70   1.1  christos 
     71   1.1  christos int
     72   1.1  christos legacy_register_sim_regno (struct gdbarch *gdbarch, int regnum)
     73   1.1  christos {
     74   1.1  christos   /* Only makes sense to supply raw registers.  */
     75   1.1  christos   gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch));
     76   1.1  christos   /* NOTE: cagney/2002-05-13: The old code did it this way and it is
     77   1.1  christos      suspected that some GDB/SIM combinations may rely on this
     78  1.12  christos      behavior.  The default should be one2one_register_sim_regno
     79   1.1  christos      (below).  */
     80  1.10  christos   if (gdbarch_register_name (gdbarch, regnum)[0] != '\0')
     81   1.1  christos     return regnum;
     82   1.1  christos   else
     83   1.1  christos     return LEGACY_SIM_REGNO_IGNORE;
     84   1.1  christos }
     85   1.1  christos 
     86  1.10  christos /* See arch-utils.h */
     87  1.10  christos 
     88   1.1  christos CORE_ADDR
     89  1.10  christos default_remove_non_address_bits (struct gdbarch *gdbarch, CORE_ADDR pointer)
     90  1.10  christos {
     91  1.10  christos   /* By default, just return the pointer value.  */
     92  1.10  christos   return pointer;
     93  1.10  christos }
     94  1.10  christos 
     95  1.10  christos /* See arch-utils.h */
     96  1.10  christos 
     97  1.10  christos std::string
     98  1.10  christos default_memtag_to_string (struct gdbarch *gdbarch, struct value *tag)
     99  1.10  christos {
    100  1.10  christos   error (_("This architecture has no method to convert a memory tag to"
    101  1.10  christos 	   " a string."));
    102  1.10  christos }
    103  1.10  christos 
    104  1.10  christos /* See arch-utils.h */
    105  1.10  christos 
    106  1.10  christos bool
    107  1.11  christos default_tagged_address_p (struct gdbarch *gdbarch, CORE_ADDR address)
    108  1.10  christos {
    109  1.10  christos   /* By default, assume the address is untagged.  */
    110  1.10  christos   return false;
    111  1.10  christos }
    112  1.10  christos 
    113  1.10  christos /* See arch-utils.h */
    114  1.10  christos 
    115  1.10  christos bool
    116  1.10  christos default_memtag_matches_p (struct gdbarch *gdbarch, struct value *address)
    117  1.10  christos {
    118  1.10  christos   /* By default, assume the tags match.  */
    119  1.10  christos   return true;
    120  1.10  christos }
    121  1.10  christos 
    122  1.10  christos /* See arch-utils.h */
    123  1.10  christos 
    124  1.10  christos bool
    125  1.10  christos default_set_memtags (struct gdbarch *gdbarch, struct value *address,
    126  1.10  christos 		     size_t length, const gdb::byte_vector &tags,
    127  1.10  christos 		     memtag_type tag_type)
    128  1.10  christos {
    129  1.10  christos   /* By default, return true (successful);  */
    130  1.10  christos   return true;
    131  1.10  christos }
    132  1.10  christos 
    133  1.10  christos /* See arch-utils.h */
    134  1.10  christos 
    135  1.10  christos struct value *
    136  1.10  christos default_get_memtag (struct gdbarch *gdbarch, struct value *address,
    137  1.10  christos 		    memtag_type tag_type)
    138  1.10  christos {
    139  1.10  christos   /* By default, return no tag.  */
    140  1.10  christos   return nullptr;
    141  1.10  christos }
    142  1.10  christos 
    143  1.10  christos CORE_ADDR
    144  1.11  christos generic_skip_trampoline_code (const frame_info_ptr &frame, CORE_ADDR pc)
    145   1.1  christos {
    146   1.1  christos   return 0;
    147   1.1  christos }
    148   1.1  christos 
    149   1.1  christos CORE_ADDR
    150   1.1  christos generic_skip_solib_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
    151   1.1  christos {
    152   1.1  christos   return 0;
    153   1.1  christos }
    154   1.1  christos 
    155   1.1  christos int
    156   1.1  christos generic_in_solib_return_trampoline (struct gdbarch *gdbarch,
    157   1.1  christos 				    CORE_ADDR pc, const char *name)
    158   1.1  christos {
    159   1.1  christos   return 0;
    160   1.1  christos }
    161   1.1  christos 
    162   1.1  christos int
    163   1.5  christos generic_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
    164   1.1  christos {
    165   1.1  christos   return 0;
    166   1.1  christos }
    167   1.1  christos 
    168   1.6  christos int
    169   1.6  christos default_code_of_frame_writable (struct gdbarch *gdbarch,
    170  1.11  christos 				const frame_info_ptr &frame)
    171   1.6  christos {
    172   1.6  christos   return 1;
    173   1.6  christos }
    174   1.6  christos 
    175   1.1  christos /* Helper functions for gdbarch_inner_than */
    176   1.1  christos 
    177  1.11  christos bool
    178   1.1  christos core_addr_lessthan (CORE_ADDR lhs, CORE_ADDR rhs)
    179   1.1  christos {
    180  1.11  christos   return lhs < rhs;
    181   1.1  christos }
    182   1.1  christos 
    183  1.11  christos bool
    184   1.1  christos core_addr_greaterthan (CORE_ADDR lhs, CORE_ADDR rhs)
    185   1.1  christos {
    186  1.11  christos   return lhs > rhs;
    187   1.1  christos }
    188   1.1  christos 
    189   1.1  christos /* Misc helper functions for targets.  */
    190   1.1  christos 
    191   1.1  christos CORE_ADDR
    192   1.1  christos core_addr_identity (struct gdbarch *gdbarch, CORE_ADDR addr)
    193   1.1  christos {
    194   1.1  christos   return addr;
    195   1.1  christos }
    196   1.1  christos 
    197   1.1  christos CORE_ADDR
    198   1.1  christos convert_from_func_ptr_addr_identity (struct gdbarch *gdbarch, CORE_ADDR addr,
    199   1.1  christos 				     struct target_ops *targ)
    200   1.1  christos {
    201   1.1  christos   return addr;
    202   1.1  christos }
    203   1.1  christos 
    204   1.1  christos int
    205   1.1  christos no_op_reg_to_regnum (struct gdbarch *gdbarch, int reg)
    206   1.1  christos {
    207   1.1  christos   return reg;
    208   1.1  christos }
    209   1.1  christos 
    210   1.1  christos void
    211   1.3  christos default_coff_make_msymbol_special (int val, struct minimal_symbol *msym)
    212   1.1  christos {
    213   1.1  christos   return;
    214   1.1  christos }
    215   1.1  christos 
    216   1.3  christos /* See arch-utils.h.  */
    217   1.3  christos 
    218   1.1  christos void
    219   1.3  christos default_make_symbol_special (struct symbol *sym, struct objfile *objfile)
    220   1.1  christos {
    221   1.1  christos   return;
    222   1.1  christos }
    223   1.1  christos 
    224   1.3  christos /* See arch-utils.h.  */
    225   1.3  christos 
    226   1.3  christos CORE_ADDR
    227   1.3  christos default_adjust_dwarf2_addr (CORE_ADDR pc)
    228   1.3  christos {
    229   1.3  christos   return pc;
    230   1.3  christos }
    231   1.3  christos 
    232   1.3  christos /* See arch-utils.h.  */
    233   1.3  christos 
    234   1.3  christos CORE_ADDR
    235   1.3  christos default_adjust_dwarf2_line (CORE_ADDR addr, int rel)
    236   1.3  christos {
    237   1.3  christos   return addr;
    238   1.3  christos }
    239   1.3  christos 
    240   1.8  christos /* See arch-utils.h.  */
    241   1.8  christos 
    242   1.8  christos bool
    243   1.8  christos default_execute_dwarf_cfa_vendor_op (struct gdbarch *gdbarch, gdb_byte op,
    244   1.8  christos 				     struct dwarf2_frame_state *fs)
    245   1.8  christos {
    246   1.8  christos   return false;
    247   1.8  christos }
    248   1.8  christos 
    249   1.1  christos int
    250   1.1  christos cannot_register_not (struct gdbarch *gdbarch, int regnum)
    251   1.1  christos {
    252   1.1  christos   return 0;
    253   1.1  christos }
    254   1.1  christos 
    255   1.1  christos /* Legacy version of target_virtual_frame_pointer().  Assumes that
    256   1.1  christos    there is an gdbarch_deprecated_fp_regnum and that it is the same,
    257   1.1  christos    cooked or raw.  */
    258   1.1  christos 
    259   1.1  christos void
    260   1.1  christos legacy_virtual_frame_pointer (struct gdbarch *gdbarch,
    261   1.1  christos 			      CORE_ADDR pc,
    262   1.1  christos 			      int *frame_regnum,
    263   1.1  christos 			      LONGEST *frame_offset)
    264   1.1  christos {
    265   1.1  christos   /* FIXME: cagney/2002-09-13: This code is used when identifying the
    266   1.1  christos      frame pointer of the current PC.  It is assuming that a single
    267   1.1  christos      register and an offset can determine this.  I think it should
    268   1.1  christos      instead generate a byte code expression as that would work better
    269   1.1  christos      with things like Dwarf2's CFI.  */
    270   1.1  christos   if (gdbarch_deprecated_fp_regnum (gdbarch) >= 0
    271   1.1  christos       && gdbarch_deprecated_fp_regnum (gdbarch)
    272   1.1  christos 	   < gdbarch_num_regs (gdbarch))
    273   1.1  christos     *frame_regnum = gdbarch_deprecated_fp_regnum (gdbarch);
    274   1.1  christos   else if (gdbarch_sp_regnum (gdbarch) >= 0
    275   1.1  christos 	   && gdbarch_sp_regnum (gdbarch)
    276  1.10  christos 		< gdbarch_num_regs (gdbarch))
    277   1.1  christos     *frame_regnum = gdbarch_sp_regnum (gdbarch);
    278   1.1  christos   else
    279   1.1  christos     /* Should this be an internal error?  I guess so, it is reflecting
    280   1.1  christos        an architectural limitation in the current design.  */
    281  1.10  christos     internal_error (_("No virtual frame pointer available"));
    282   1.1  christos   *frame_offset = 0;
    283   1.1  christos }
    284   1.1  christos 
    285   1.7  christos /* Return a floating-point format for a floating-point variable of
    286   1.7  christos    length LEN in bits.  If non-NULL, NAME is the name of its type.
    287   1.7  christos    If no suitable type is found, return NULL.  */
    288   1.7  christos 
    289   1.7  christos const struct floatformat **
    290   1.7  christos default_floatformat_for_type (struct gdbarch *gdbarch,
    291   1.7  christos 			      const char *name, int len)
    292   1.7  christos {
    293   1.7  christos   const struct floatformat **format = NULL;
    294   1.7  christos 
    295  1.10  christos   /* Check if this is a bfloat16 type.  It has the same size as the
    296  1.10  christos      IEEE half float type, so we use the base type name to tell them
    297  1.10  christos      apart.  */
    298  1.10  christos   if (name != nullptr && strcmp (name, "__bf16") == 0
    299  1.10  christos       && len == gdbarch_bfloat16_bit (gdbarch))
    300  1.10  christos     format = gdbarch_bfloat16_format (gdbarch);
    301  1.10  christos   else if (len == gdbarch_half_bit (gdbarch))
    302   1.7  christos     format = gdbarch_half_format (gdbarch);
    303   1.7  christos   else if (len == gdbarch_float_bit (gdbarch))
    304   1.7  christos     format = gdbarch_float_format (gdbarch);
    305   1.7  christos   else if (len == gdbarch_double_bit (gdbarch))
    306   1.7  christos     format = gdbarch_double_format (gdbarch);
    307   1.7  christos   else if (len == gdbarch_long_double_bit (gdbarch))
    308   1.7  christos     format = gdbarch_long_double_format (gdbarch);
    309   1.7  christos   /* On i386 the 'long double' type takes 96 bits,
    310   1.7  christos      while the real number of used bits is only 80,
    311   1.7  christos      both in processor and in memory.
    312   1.7  christos      The code below accepts the real bit size.  */
    313   1.7  christos   else if (gdbarch_long_double_format (gdbarch) != NULL
    314   1.7  christos 	   && len == gdbarch_long_double_format (gdbarch)[0]->totalsize)
    315   1.7  christos     format = gdbarch_long_double_format (gdbarch);
    316   1.7  christos 
    317   1.7  christos   return format;
    318   1.7  christos }
    319   1.1  christos 
    320   1.1  christos int
    322   1.1  christos generic_convert_register_p (struct gdbarch *gdbarch, int regnum,
    323   1.1  christos 			    struct type *type)
    324   1.1  christos {
    325   1.1  christos   return 0;
    326   1.1  christos }
    327   1.1  christos 
    328   1.1  christos int
    329   1.1  christos default_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
    330   1.1  christos {
    331   1.1  christos   return 0;
    332   1.1  christos }
    333   1.1  christos 
    334   1.1  christos int
    335   1.1  christos generic_instruction_nullified (struct gdbarch *gdbarch,
    336   1.1  christos 			       struct regcache *regcache)
    337   1.1  christos {
    338   1.1  christos   return 0;
    339   1.1  christos }
    340   1.1  christos 
    341   1.1  christos int
    342   1.1  christos default_remote_register_number (struct gdbarch *gdbarch,
    343   1.1  christos 				int regno)
    344   1.1  christos {
    345   1.1  christos   return regno;
    346   1.1  christos }
    347   1.3  christos 
    348   1.3  christos /* See arch-utils.h.  */
    349   1.3  christos 
    350   1.3  christos int
    351   1.3  christos default_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range)
    352   1.3  christos {
    353   1.3  christos   return 0;
    354   1.3  christos }
    355   1.1  christos 
    356   1.1  christos 
    357   1.1  christos /* Functions to manipulate the endianness of the target.  */
    359   1.1  christos 
    360   1.1  christos static enum bfd_endian target_byte_order_user = BFD_ENDIAN_UNKNOWN;
    361   1.1  christos 
    362   1.1  christos static const char endian_big[] = "big";
    363   1.1  christos static const char endian_little[] = "little";
    364   1.1  christos static const char endian_auto[] = "auto";
    365   1.1  christos static const char *const endian_enum[] =
    366   1.1  christos {
    367   1.1  christos   endian_big,
    368   1.1  christos   endian_little,
    369   1.1  christos   endian_auto,
    370  1.10  christos   NULL,
    371   1.1  christos };
    372   1.1  christos static const char *set_endian_string = endian_auto;
    373   1.1  christos 
    374   1.1  christos enum bfd_endian
    375   1.1  christos selected_byte_order (void)
    376   1.1  christos {
    377   1.1  christos   return target_byte_order_user;
    378   1.1  christos }
    379   1.1  christos 
    380   1.1  christos /* Called by ``show endian''.  */
    381   1.1  christos 
    382   1.1  christos static void
    383   1.1  christos show_endian (struct ui_file *file, int from_tty, struct cmd_list_element *c,
    384   1.1  christos 	     const char *value)
    385   1.1  christos {
    386  1.10  christos   if (target_byte_order_user == BFD_ENDIAN_UNKNOWN)
    387  1.10  christos     if (gdbarch_byte_order (get_current_arch ()) == BFD_ENDIAN_BIG)
    388   1.1  christos       gdb_printf (file, _("The target endianness is set automatically "
    389  1.10  christos 			  "(currently big endian).\n"));
    390  1.10  christos     else
    391   1.1  christos       gdb_printf (file, _("The target endianness is set automatically "
    392   1.1  christos 			  "(currently little endian).\n"));
    393  1.10  christos   else
    394  1.10  christos     if (target_byte_order_user == BFD_ENDIAN_BIG)
    395   1.1  christos       gdb_printf (file,
    396  1.10  christos 		  _("The target is set to big endian.\n"));
    397  1.10  christos     else
    398   1.1  christos       gdb_printf (file,
    399   1.1  christos 		  _("The target is set to little endian.\n"));
    400   1.1  christos }
    401   1.8  christos 
    402   1.1  christos static void
    403   1.1  christos set_endian (const char *ignore_args, int from_tty, struct cmd_list_element *c)
    404   1.1  christos {
    405   1.1  christos   struct gdbarch_info info;
    406   1.1  christos 
    407   1.1  christos   if (set_endian_string == endian_auto)
    408  1.12  christos     {
    409  1.10  christos       target_byte_order_user = BFD_ENDIAN_UNKNOWN;
    410   1.1  christos       if (!gdbarch_update_p (current_inferior (), info))
    411   1.1  christos 	internal_error (_("set_endian: architecture update failed"));
    412   1.1  christos     }
    413   1.1  christos   else if (set_endian_string == endian_little)
    414  1.12  christos     {
    415  1.10  christos       info.byte_order = BFD_ENDIAN_LITTLE;
    416  1.10  christos       if (!gdbarch_update_p (current_inferior (), info))
    417   1.1  christos 	gdb_printf (gdb_stderr,
    418   1.1  christos 		    _("Little endian target not supported by GDB\n"));
    419   1.1  christos       else
    420   1.1  christos 	target_byte_order_user = BFD_ENDIAN_LITTLE;
    421   1.1  christos     }
    422   1.1  christos   else if (set_endian_string == endian_big)
    423  1.12  christos     {
    424  1.10  christos       info.byte_order = BFD_ENDIAN_BIG;
    425  1.10  christos       if (!gdbarch_update_p (current_inferior (), info))
    426   1.1  christos 	gdb_printf (gdb_stderr,
    427   1.1  christos 		    _("Big endian target not supported by GDB\n"));
    428   1.1  christos       else
    429   1.1  christos 	target_byte_order_user = BFD_ENDIAN_BIG;
    430  1.10  christos     }
    431   1.1  christos   else
    432   1.1  christos     internal_error (_("set_endian: bad value"));
    433   1.1  christos 
    434   1.1  christos   show_endian (gdb_stdout, from_tty, NULL, NULL);
    435   1.1  christos }
    436   1.1  christos 
    437   1.1  christos /* Given SELECTED, a currently selected BFD architecture, and
    438   1.1  christos    TARGET_DESC, the current target description, return what
    439   1.1  christos    architecture to use.
    440   1.1  christos 
    441   1.9  christos    SELECTED may be NULL, in which case we return the architecture
    442   1.1  christos    associated with TARGET_DESC.  If SELECTED specifies a variant
    443   1.1  christos    of the architecture associated with TARGET_DESC, return the
    444   1.1  christos    more specific of the two.
    445   1.1  christos 
    446   1.1  christos    If SELECTED is a different architecture, but it is accepted as
    447   1.1  christos    compatible by the target, we can use the target architecture.
    448   1.1  christos 
    449   1.1  christos    If SELECTED is obviously incompatible, warn the user.  */
    450   1.1  christos 
    451   1.1  christos static const struct bfd_arch_info *
    452   1.1  christos choose_architecture_for_target (const struct target_desc *target_desc,
    453   1.1  christos 				const struct bfd_arch_info *selected)
    454   1.1  christos {
    455   1.1  christos   const struct bfd_arch_info *from_target = tdesc_architecture (target_desc);
    456   1.1  christos   const struct bfd_arch_info *compat1, *compat2;
    457   1.1  christos 
    458   1.1  christos   if (selected == NULL)
    459   1.1  christos     return from_target;
    460   1.1  christos 
    461   1.1  christos   if (from_target == NULL)
    462   1.1  christos     return selected;
    463   1.1  christos 
    464   1.1  christos   /* struct bfd_arch_info objects are singletons: that is, there's
    465   1.1  christos      supposed to be exactly one instance for a given machine.  So you
    466   1.1  christos      can tell whether two are equivalent by comparing pointers.  */
    467   1.1  christos   if (from_target == selected)
    468   1.1  christos     return selected;
    469   1.1  christos 
    470   1.1  christos   /* BFD's 'A->compatible (A, B)' functions return zero if A and B are
    471   1.1  christos      incompatible.  But if they are compatible, it returns the 'more
    472   1.1  christos      featureful' of the two arches.  That is, if A can run code
    473   1.1  christos      written for B, but B can't run code written for A, then it'll
    474   1.1  christos      return A.
    475   1.1  christos 
    476   1.1  christos      Some targets (e.g. MIPS as of 2006-12-04) don't fully
    477   1.1  christos      implement this, instead always returning NULL or the first
    478   1.1  christos      argument.  We detect that case by checking both directions.  */
    479   1.1  christos 
    480   1.1  christos   compat1 = selected->compatible (selected, from_target);
    481   1.1  christos   compat2 = from_target->compatible (from_target, selected);
    482   1.1  christos 
    483   1.1  christos   if (compat1 == NULL && compat2 == NULL)
    484   1.1  christos     {
    485   1.1  christos       /* BFD considers the architectures incompatible.  Check our
    486   1.1  christos 	 target description whether it accepts SELECTED as compatible
    487   1.1  christos 	 anyway.  */
    488   1.1  christos       if (tdesc_compatible_p (target_desc, selected))
    489   1.1  christos 	return from_target;
    490   1.1  christos 
    491   1.1  christos       warning (_("Selected architecture %s is not compatible "
    492   1.1  christos 		 "with reported target architecture %s"),
    493   1.1  christos 	       selected->printable_name, from_target->printable_name);
    494   1.1  christos       return selected;
    495   1.1  christos     }
    496   1.1  christos 
    497   1.1  christos   if (compat1 == NULL)
    498   1.1  christos     return compat2;
    499   1.1  christos   if (compat2 == NULL)
    500   1.1  christos     return compat1;
    501   1.1  christos   if (compat1 == compat2)
    502   1.1  christos     return compat1;
    503   1.1  christos 
    504   1.1  christos   /* If the two didn't match, but one of them was a default
    505   1.1  christos      architecture, assume the more specific one is correct.  This
    506   1.1  christos      handles the case where an executable or target description just
    507   1.1  christos      says "mips", but the other knows which MIPS variant.  */
    508   1.1  christos   if (compat1->the_default)
    509   1.1  christos     return compat2;
    510   1.1  christos   if (compat2->the_default)
    511   1.1  christos     return compat1;
    512   1.1  christos 
    513   1.1  christos   /* We have no idea which one is better.  This is a bug, but not
    514   1.1  christos      a critical problem; warn the user.  */
    515   1.1  christos   warning (_("Selected architecture %s is ambiguous with "
    516   1.1  christos 	     "reported target architecture %s"),
    517   1.1  christos 	   selected->printable_name, from_target->printable_name);
    518   1.1  christos   return selected;
    519   1.1  christos }
    520   1.1  christos 
    521   1.1  christos /* Functions to manipulate the architecture of the target.  */
    522   1.1  christos 
    523   1.1  christos enum set_arch { set_arch_auto, set_arch_manual };
    524   1.1  christos 
    525   1.1  christos static const struct bfd_arch_info *target_architecture_user;
    526   1.1  christos 
    527   1.1  christos static const char *set_architecture_string;
    528   1.1  christos 
    529   1.1  christos const char *
    530   1.1  christos selected_architecture_name (void)
    531   1.1  christos {
    532   1.1  christos   if (target_architecture_user == NULL)
    533   1.1  christos     return NULL;
    534   1.1  christos   else
    535   1.1  christos     return set_architecture_string;
    536   1.1  christos }
    537   1.1  christos 
    538   1.1  christos /* Called if the user enters ``show architecture'' without an
    539   1.1  christos    argument.  */
    540   1.1  christos 
    541   1.1  christos static void
    542   1.1  christos show_architecture (struct ui_file *file, int from_tty,
    543   1.1  christos 		   struct cmd_list_element *c, const char *value)
    544  1.10  christos {
    545  1.10  christos   if (target_architecture_user == NULL)
    546  1.10  christos     gdb_printf (file, _("The target architecture is set to "
    547   1.1  christos 			"\"auto\" (currently \"%s\").\n"),
    548  1.10  christos 		gdbarch_bfd_arch_info (get_current_arch ())->printable_name);
    549  1.10  christos   else
    550   1.1  christos     gdb_printf (file, _("The target architecture is set to \"%s\".\n"),
    551   1.1  christos 		set_architecture_string);
    552   1.1  christos }
    553   1.1  christos 
    554   1.1  christos 
    555   1.1  christos /* Called if the user enters ``set architecture'' with or without an
    556   1.1  christos    argument.  */
    557   1.8  christos 
    558   1.8  christos static void
    559   1.1  christos set_architecture (const char *ignore_args,
    560   1.1  christos 		  int from_tty, struct cmd_list_element *c)
    561   1.1  christos {
    562   1.1  christos   struct gdbarch_info info;
    563   1.1  christos 
    564   1.1  christos   if (strcmp (set_architecture_string, "auto") == 0)
    565  1.12  christos     {
    566  1.10  christos       target_architecture_user = NULL;
    567   1.1  christos       if (!gdbarch_update_p (current_inferior (), info))
    568   1.1  christos 	internal_error (_("could not select an architecture automatically"));
    569   1.1  christos     }
    570   1.1  christos   else
    571   1.1  christos     {
    572  1.10  christos       info.bfd_arch_info = bfd_scan_arch (set_architecture_string);
    573  1.12  christos       if (info.bfd_arch_info == NULL)
    574   1.1  christos 	internal_error (_("set_architecture: bfd_scan_arch failed"));
    575   1.1  christos       if (gdbarch_update_p (current_inferior (), info))
    576  1.10  christos 	target_architecture_user = info.bfd_arch_info;
    577  1.10  christos       else
    578  1.10  christos 	gdb_printf (gdb_stderr,
    579   1.1  christos 		    _("Architecture `%s' not recognized.\n"),
    580   1.1  christos 		    set_architecture_string);
    581   1.1  christos     }
    582   1.1  christos   show_architecture (gdb_stdout, from_tty, NULL, NULL);
    583  1.12  christos }
    584  1.12  christos 
    585   1.1  christos /* See arch-utils.h.  */
    586  1.12  christos 
    587   1.1  christos int
    588   1.1  christos gdbarch_update_p (inferior *inf, struct gdbarch_info info)
    589   1.1  christos {
    590   1.1  christos   struct gdbarch *new_gdbarch;
    591   1.1  christos 
    592  1.12  christos   /* Check for the current file.  */
    593  1.12  christos   if (info.abfd == NULL)
    594   1.1  christos     info.abfd = inf->pspace->exec_bfd ();
    595  1.12  christos 
    596   1.1  christos   if (info.abfd == NULL)
    597   1.1  christos     info.abfd = inf->pspace->core_bfd ();
    598   1.1  christos 
    599  1.12  christos   /* Check for the current target description.  */
    600   1.1  christos   if (info.target_desc == NULL)
    601   1.1  christos     info.target_desc = target_current_description (inf);
    602   1.1  christos 
    603   1.1  christos   new_gdbarch = gdbarch_find_by_info (info);
    604   1.1  christos 
    605   1.1  christos   /* If there no architecture by that name, reject the request.  */
    606   1.1  christos   if (new_gdbarch == NULL)
    607  1.10  christos     {
    608  1.10  christos       if (gdbarch_debug)
    609   1.1  christos 	gdb_printf (gdb_stdlog, "gdbarch_update_p: "
    610   1.1  christos 		    "Architecture not found\n");
    611   1.1  christos       return 0;
    612   1.1  christos     }
    613   1.1  christos 
    614  1.12  christos   /* If it is the same old architecture, accept the request (but don't
    615   1.1  christos      swap anything).  */
    616   1.1  christos   if (new_gdbarch == inf->arch ())
    617  1.10  christos     {
    618  1.10  christos       if (gdbarch_debug)
    619  1.10  christos 	gdb_printf (gdb_stdlog, "gdbarch_update_p: "
    620  1.10  christos 		    "Architecture %s (%s) unchanged\n",
    621   1.1  christos 		    host_address_to_string (new_gdbarch),
    622   1.1  christos 		    gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
    623   1.1  christos       return 1;
    624   1.1  christos     }
    625   1.1  christos 
    626  1.10  christos   /* It's a new architecture, swap it in.  */
    627  1.10  christos   if (gdbarch_debug)
    628  1.10  christos     gdb_printf (gdb_stdlog, "gdbarch_update_p: "
    629  1.10  christos 		"New architecture %s (%s) selected\n",
    630  1.11  christos 		host_address_to_string (new_gdbarch),
    631  1.12  christos 		gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
    632   1.1  christos 
    633   1.1  christos   inf->set_arch (new_gdbarch);
    634   1.1  christos 
    635   1.1  christos   return 1;
    636   1.1  christos }
    637   1.1  christos 
    638   1.1  christos /* Return the architecture for ABFD.  If no suitable architecture
    639   1.1  christos    could be find, return NULL.  */
    640   1.1  christos 
    641   1.1  christos struct gdbarch *
    642   1.1  christos gdbarch_from_bfd (bfd *abfd)
    643   1.1  christos {
    644   1.1  christos   struct gdbarch_info info;
    645   1.1  christos 
    646   1.1  christos   info.abfd = abfd;
    647   1.1  christos   return gdbarch_find_by_info (info);
    648   1.1  christos }
    649   1.1  christos 
    650   1.1  christos /* Set the dynamic target-system-dependent parameters (architecture,
    651   1.1  christos    byte-order) using information found in the BFD */
    652   1.1  christos 
    653   1.1  christos void
    654   1.1  christos set_gdbarch_from_file (bfd *abfd)
    655   1.1  christos {
    656   1.1  christos   struct gdbarch_info info;
    657   1.1  christos   struct gdbarch *gdbarch;
    658  1.12  christos 
    659   1.1  christos   info.abfd = abfd;
    660   1.1  christos   info.target_desc = target_current_description (current_inferior ());
    661   1.1  christos   gdbarch = gdbarch_find_by_info (info);
    662   1.1  christos 
    663  1.11  christos   if (gdbarch == NULL)
    664  1.11  christos     error (_("Architecture of file not recognized."));
    665   1.1  christos 
    666   1.1  christos   current_inferior ()->set_arch (gdbarch);
    667   1.1  christos }
    668   1.1  christos 
    669   1.1  christos /* Initialize the current architecture.  Update the ``set
    670   1.1  christos    architecture'' command so that it specifies a list of valid
    671   1.1  christos    architectures.  */
    672   1.1  christos 
    673   1.1  christos #ifdef DEFAULT_BFD_ARCH
    674   1.1  christos extern const bfd_arch_info_type DEFAULT_BFD_ARCH;
    675   1.1  christos static const bfd_arch_info_type *default_bfd_arch = &DEFAULT_BFD_ARCH;
    676   1.1  christos #else
    677   1.1  christos static const bfd_arch_info_type *default_bfd_arch;
    678   1.1  christos #endif
    679   1.1  christos 
    680   1.1  christos #ifdef DEFAULT_BFD_VEC
    681   1.1  christos extern const bfd_target DEFAULT_BFD_VEC;
    682   1.1  christos static const bfd_target *default_bfd_vec = &DEFAULT_BFD_VEC;
    683   1.1  christos #else
    684   1.1  christos static const bfd_target *default_bfd_vec;
    685   1.6  christos #endif
    686   1.1  christos 
    687  1.10  christos static enum bfd_endian default_byte_order = BFD_ENDIAN_UNKNOWN;
    688  1.10  christos 
    689  1.10  christos /* Printable names of architectures.  Used as the enum list of the
    690  1.10  christos    "set arch" command.  */
    691   1.1  christos static std::vector<const char *> arches;
    692   1.1  christos 
    693   1.1  christos void
    694  1.10  christos initialize_current_architecture (void)
    695   1.1  christos {
    696   1.1  christos   arches = gdbarch_printable_names ();
    697   1.1  christos 
    698   1.1  christos   /* Find a default architecture.  */
    699   1.1  christos   if (default_bfd_arch == NULL)
    700   1.1  christos     {
    701   1.1  christos       /* Choose the architecture by taking the first one
    702  1.10  christos 	 alphabetically.  */
    703  1.10  christos       const char *chosen = arches[0];
    704   1.1  christos 
    705  1.10  christos       for (const char *arch : arches)
    706  1.10  christos 	{
    707   1.1  christos 	  if (strcmp (arch, chosen) < 0)
    708  1.10  christos 	    chosen = arch;
    709   1.1  christos 	}
    710  1.10  christos 
    711  1.10  christos       if (chosen == NULL)
    712   1.1  christos 	internal_error (_("initialize_current_architecture: No arch"));
    713   1.1  christos 
    714  1.10  christos       default_bfd_arch = bfd_scan_arch (chosen);
    715   1.1  christos       if (default_bfd_arch == NULL)
    716   1.1  christos 	internal_error (_("initialize_current_architecture: Arch not found"));
    717  1.10  christos     }
    718   1.1  christos 
    719   1.1  christos   gdbarch_info info;
    720   1.1  christos   info.bfd_arch_info = default_bfd_arch;
    721   1.1  christos 
    722   1.1  christos   /* Take several guesses at a byte order.  */
    723   1.1  christos   if (default_byte_order == BFD_ENDIAN_UNKNOWN
    724   1.1  christos       && default_bfd_vec != NULL)
    725   1.1  christos     {
    726   1.1  christos       /* Extract BFD's default vector's byte order.  */
    727   1.1  christos       switch (default_bfd_vec->byteorder)
    728   1.1  christos 	{
    729   1.1  christos 	case BFD_ENDIAN_BIG:
    730   1.1  christos 	  default_byte_order = BFD_ENDIAN_BIG;
    731   1.1  christos 	  break;
    732   1.1  christos 	case BFD_ENDIAN_LITTLE:
    733   1.1  christos 	  default_byte_order = BFD_ENDIAN_LITTLE;
    734   1.1  christos 	  break;
    735   1.1  christos 	default:
    736   1.1  christos 	  break;
    737   1.1  christos 	}
    738   1.1  christos     }
    739   1.1  christos   if (default_byte_order == BFD_ENDIAN_UNKNOWN)
    740   1.1  christos     {
    741   1.1  christos       /* look for ``*el-*'' in the target name.  */
    742   1.1  christos       const char *chp;
    743   1.1  christos       chp = strchr (target_name, '-');
    744   1.5  christos       if (chp != NULL
    745   1.1  christos 	  && chp - 2 >= target_name
    746   1.1  christos 	  && startswith (chp - 2, "el"))
    747   1.1  christos 	default_byte_order = BFD_ENDIAN_LITTLE;
    748   1.1  christos     }
    749   1.1  christos   if (default_byte_order == BFD_ENDIAN_UNKNOWN)
    750   1.1  christos     {
    751   1.1  christos       /* Wire it to big-endian!!! */
    752   1.1  christos       default_byte_order = BFD_ENDIAN_BIG;
    753   1.1  christos     }
    754   1.1  christos 
    755   1.1  christos   info.byte_order = default_byte_order;
    756  1.12  christos   info.byte_order_for_code = info.byte_order;
    757  1.10  christos 
    758   1.1  christos   if (!gdbarch_update_p (current_inferior (), info))
    759   1.1  christos     internal_error (_("initialize_current_architecture: Selection of "
    760   1.1  christos 		      "initial architecture failed"));
    761   1.1  christos 
    762   1.1  christos   /* Create the ``set architecture'' command appending ``auto'' to the
    763   1.1  christos      list of architectures.  */
    764  1.10  christos   {
    765  1.10  christos     /* Append ``auto''.  */
    766  1.10  christos     set_architecture_string = "auto";
    767  1.10  christos     arches.push_back (set_architecture_string);
    768  1.10  christos     arches.push_back (nullptr);
    769  1.10  christos     set_show_commands architecture_cmds
    770  1.10  christos       = add_setshow_enum_cmd ("architecture", class_support,
    771  1.10  christos 			      arches.data (), &set_architecture_string,
    772  1.10  christos 			      _("Set architecture of target."),
    773  1.10  christos 			      _("Show architecture of target."), NULL,
    774  1.10  christos 			      set_architecture, show_architecture,
    775  1.10  christos 			      &setlist, &showlist);
    776   1.1  christos     add_alias_cmd ("processor", architecture_cmds.set, class_support, 1,
    777   1.1  christos 		   &setlist);
    778   1.1  christos   }
    779   1.1  christos }
    780   1.1  christos 
    781   1.1  christos /* Similar to init, but this time fill in the blanks.  Information is
    782   1.1  christos    obtained from the global "set ..." options and explicitly
    783   1.1  christos    initialized INFO fields.  */
    784   1.1  christos 
    785   1.1  christos void
    786   1.1  christos gdbarch_info_fill (struct gdbarch_info *info)
    787   1.1  christos {
    788   1.1  christos   /* "(gdb) set architecture ...".  */
    789   1.1  christos   if (info->bfd_arch_info == NULL
    790   1.1  christos       && target_architecture_user)
    791   1.1  christos     info->bfd_arch_info = target_architecture_user;
    792   1.1  christos   /* From the file.  */
    793   1.1  christos   if (info->bfd_arch_info == NULL
    794   1.1  christos       && info->abfd != NULL
    795   1.1  christos       && bfd_get_arch (info->abfd) != bfd_arch_unknown
    796   1.1  christos       && bfd_get_arch (info->abfd) != bfd_arch_obscure)
    797   1.1  christos     info->bfd_arch_info = bfd_get_arch_info (info->abfd);
    798   1.1  christos   /* From the target.  */
    799   1.1  christos   if (info->target_desc != NULL)
    800   1.1  christos     info->bfd_arch_info = choose_architecture_for_target
    801   1.1  christos 			   (info->target_desc, info->bfd_arch_info);
    802   1.1  christos   /* From the default.  */
    803   1.1  christos   if (info->bfd_arch_info == NULL)
    804   1.1  christos     info->bfd_arch_info = default_bfd_arch;
    805   1.1  christos 
    806   1.1  christos   /* "(gdb) set byte-order ...".  */
    807   1.1  christos   if (info->byte_order == BFD_ENDIAN_UNKNOWN
    808   1.1  christos       && target_byte_order_user != BFD_ENDIAN_UNKNOWN)
    809   1.1  christos     info->byte_order = target_byte_order_user;
    810   1.1  christos   /* From the INFO struct.  */
    811   1.1  christos   if (info->byte_order == BFD_ENDIAN_UNKNOWN
    812   1.1  christos       && info->abfd != NULL)
    813   1.1  christos     info->byte_order = (bfd_big_endian (info->abfd) ? BFD_ENDIAN_BIG
    814   1.1  christos 			: bfd_little_endian (info->abfd) ? BFD_ENDIAN_LITTLE
    815   1.1  christos 			: BFD_ENDIAN_UNKNOWN);
    816   1.1  christos   /* From the default.  */
    817   1.1  christos   if (info->byte_order == BFD_ENDIAN_UNKNOWN)
    818   1.8  christos     info->byte_order = default_byte_order;
    819   1.8  christos   info->byte_order_for_code = info->byte_order;
    820   1.1  christos   /* Wire the default to the last selected byte order.  */
    821   1.1  christos   default_byte_order = info->byte_order;
    822   1.1  christos 
    823   1.8  christos   /* "(gdb) set osabi ...".  Handled by gdbarch_lookup_osabi.  */
    824   1.1  christos   /* From the manual override, or from file.  */
    825   1.1  christos   if (info->osabi == GDB_OSABI_UNKNOWN)
    826   1.8  christos     info->osabi = gdbarch_lookup_osabi (info->abfd);
    827   1.1  christos   /* From the target.  */
    828   1.1  christos 
    829   1.1  christos   if (info->osabi == GDB_OSABI_UNKNOWN && info->target_desc != NULL)
    830   1.1  christos     info->osabi = tdesc_osabi (info->target_desc);
    831   1.1  christos   /* From the configured default.  */
    832   1.1  christos #ifdef GDB_OSABI_DEFAULT
    833   1.1  christos   if (info->osabi == GDB_OSABI_UNKNOWN)
    834   1.8  christos     info->osabi = GDB_OSABI_DEFAULT;
    835   1.8  christos #endif
    836   1.8  christos   /* If we still don't know which osabi to pick, pick none.  */
    837   1.1  christos   if (info->osabi == GDB_OSABI_UNKNOWN)
    838   1.1  christos     info->osabi = GDB_OSABI_NONE;
    839   1.1  christos 
    840   1.1  christos   /* Must have at least filled in the architecture.  */
    841   1.1  christos   gdb_assert (info->bfd_arch_info != NULL);
    842   1.1  christos }
    843   1.1  christos 
    844   1.1  christos /* Return "current" architecture.  If the target is running, this is
    845   1.1  christos    the architecture of the selected frame.  Otherwise, the "current"
    846   1.1  christos    architecture defaults to the target architecture.
    847   1.1  christos 
    848   1.1  christos    This function should normally be called solely by the command
    849   1.1  christos    interpreter routines to determine the architecture to execute a
    850   1.1  christos    command in.  */
    851   1.1  christos struct gdbarch *
    852   1.1  christos get_current_arch (void)
    853   1.1  christos {
    854   1.1  christos   if (has_stack_frames ())
    855  1.11  christos     return get_frame_arch (get_selected_frame (NULL));
    856   1.1  christos   else
    857   1.1  christos     return current_inferior ()->arch ();
    858   1.1  christos }
    859   1.1  christos 
    860   1.1  christos int
    861   1.1  christos default_has_shared_address_space (struct gdbarch *gdbarch)
    862   1.1  christos {
    863   1.1  christos   /* Simply say no.  In most unix-like targets each inferior/process
    864   1.1  christos      has its own address space.  */
    865   1.1  christos   return 0;
    866   1.1  christos }
    867   1.6  christos 
    868   1.8  christos int
    869   1.1  christos default_fast_tracepoint_valid_at (struct gdbarch *gdbarch, CORE_ADDR addr,
    870   1.1  christos 				  std::string *msg)
    871   1.1  christos {
    872   1.1  christos   /* We don't know if maybe the target has some way to do fast
    873   1.8  christos      tracepoints that doesn't need gdbarch, so always say yes.  */
    874   1.1  christos   if (msg)
    875   1.1  christos     msg->clear ();
    876   1.1  christos   return 1;
    877   1.7  christos }
    878   1.7  christos 
    879   1.7  christos const gdb_byte *
    880   1.7  christos default_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
    881   1.7  christos 			    int *lenptr)
    882   1.7  christos {
    883   1.7  christos   int kind = gdbarch_breakpoint_kind_from_pc (gdbarch, pcptr);
    884   1.7  christos 
    885   1.7  christos   return gdbarch_sw_breakpoint_from_kind (gdbarch, kind, lenptr);
    886   1.7  christos }
    887   1.7  christos int
    888   1.7  christos default_breakpoint_kind_from_current_state (struct gdbarch *gdbarch,
    889   1.1  christos 					    struct regcache *regcache,
    890   1.7  christos 					    CORE_ADDR *pcptr)
    891   1.1  christos {
    892   1.1  christos   return gdbarch_breakpoint_kind_from_pc (gdbarch, pcptr);
    893   1.7  christos }
    894   1.1  christos 
    895   1.1  christos 
    896   1.1  christos void
    897   1.1  christos default_gen_return_address (struct gdbarch *gdbarch,
    898   1.1  christos 			    struct agent_expr *ax, struct axs_value *value,
    899   1.1  christos 			    CORE_ADDR scope)
    900   1.1  christos {
    901   1.1  christos   error (_("This architecture has no method to collect a return address."));
    902   1.1  christos }
    903   1.1  christos 
    904   1.1  christos int
    905   1.1  christos default_return_in_first_hidden_param_p (struct gdbarch *gdbarch,
    906   1.1  christos 					struct type *type)
    907   1.1  christos {
    908   1.1  christos   /* Usually, the return value's address is stored the in the "first hidden"
    909   1.9  christos      parameter if the return value should be passed by reference, as
    910   1.1  christos      specified in ABI.  */
    911   1.1  christos   return !(language_pass_by_reference (type).trivially_copyable);
    912   1.3  christos }
    913   1.3  christos 
    914   1.3  christos int default_insn_is_call (struct gdbarch *gdbarch, CORE_ADDR addr)
    915   1.3  christos {
    916   1.3  christos   return 0;
    917   1.3  christos }
    918   1.3  christos 
    919   1.3  christos int default_insn_is_ret (struct gdbarch *gdbarch, CORE_ADDR addr)
    920   1.3  christos {
    921   1.3  christos   return 0;
    922   1.3  christos }
    923   1.3  christos 
    924   1.3  christos int default_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
    925   1.3  christos {
    926   1.3  christos   return 0;
    927   1.9  christos }
    928   1.9  christos 
    929   1.9  christos /*  See arch-utils.h.  */
    930   1.9  christos 
    931   1.9  christos bool
    932   1.9  christos default_program_breakpoint_here_p (struct gdbarch *gdbarch,
    933   1.9  christos 				   CORE_ADDR address)
    934   1.9  christos {
    935   1.9  christos   int len;
    936   1.9  christos   const gdb_byte *bpoint = gdbarch_breakpoint_from_pc (gdbarch, &address, &len);
    937   1.9  christos 
    938   1.9  christos   /* Software breakpoints unsupported?  */
    939   1.9  christos   if (bpoint == nullptr)
    940   1.9  christos     return false;
    941   1.9  christos 
    942   1.9  christos   gdb_byte *target_mem = (gdb_byte *) alloca (len);
    943   1.9  christos 
    944   1.9  christos   /* Enable the automatic memory restoration from breakpoints while
    945   1.9  christos      we read the memory.  Otherwise we may find temporary breakpoints, ones
    946   1.9  christos      inserted by GDB, and flag them as permanent breakpoints.  */
    947   1.9  christos   scoped_restore restore_memory
    948   1.9  christos     = make_scoped_restore_show_memory_breakpoints (0);
    949   1.9  christos 
    950   1.9  christos   if (target_read_memory (address, target_mem, len) == 0)
    951   1.9  christos     {
    952   1.9  christos       /* Check if this is a breakpoint instruction for this architecture,
    953   1.9  christos 	 including ones used by GDB.  */
    954   1.9  christos       if (memcmp (target_mem, bpoint, len) == 0)
    955   1.9  christos 	return true;
    956   1.9  christos     }
    957   1.9  christos 
    958   1.9  christos   return false;
    959   1.3  christos }
    960   1.3  christos 
    961   1.3  christos void
    962   1.8  christos default_skip_permanent_breakpoint (struct regcache *regcache)
    963   1.3  christos {
    964   1.3  christos   struct gdbarch *gdbarch = regcache->arch ();
    965   1.3  christos   CORE_ADDR current_pc = regcache_read_pc (regcache);
    966   1.6  christos   int bp_len;
    967   1.3  christos 
    968   1.3  christos   gdbarch_breakpoint_from_pc (gdbarch, &current_pc, &bp_len);
    969   1.3  christos   current_pc += bp_len;
    970   1.3  christos   regcache_write_pc (regcache, current_pc);
    971   1.3  christos }
    972   1.3  christos 
    973   1.3  christos CORE_ADDR
    974   1.3  christos default_infcall_mmap (CORE_ADDR size, unsigned prot)
    975   1.3  christos {
    976   1.3  christos   error (_("This target does not support inferior memory allocation by mmap."));
    977   1.5  christos }
    978   1.5  christos 
    979   1.5  christos void
    980   1.5  christos default_infcall_munmap (CORE_ADDR addr, CORE_ADDR size)
    981   1.5  christos {
    982   1.5  christos   /* Memory reserved by inferior mmap is kept leaked.  */
    983   1.3  christos }
    984   1.3  christos 
    985   1.3  christos /* -mcmodel=large is used so that no GOT (Global Offset Table) is needed to be
    986   1.9  christos    created in inferior memory by GDB (normally it is set by ld.so).  */
    987   1.3  christos 
    988   1.3  christos std::string
    989   1.9  christos default_gcc_target_options (struct gdbarch *gdbarch)
    990   1.9  christos {
    991   1.9  christos   return string_printf ("-m%d%s", gdbarch_ptr_bit (gdbarch),
    992   1.3  christos 			(gdbarch_ptr_bit (gdbarch) == 64
    993   1.3  christos 			 ? " -mcmodel=large" : ""));
    994   1.3  christos }
    995   1.3  christos 
    996   1.3  christos /* gdbarch gnu_triplet_regexp method.  */
    997   1.3  christos 
    998   1.3  christos const char *
    999   1.3  christos default_gnu_triplet_regexp (struct gdbarch *gdbarch)
   1000   1.3  christos {
   1001   1.1  christos   return gdbarch_bfd_arch_info (gdbarch)->arch_name;
   1002  1.10  christos }
   1003  1.10  christos 
   1004  1.10  christos /* Default method for gdbarch_addressable_memory_unit_size.  The default is
   1005  1.10  christos    based on the bits_per_byte defined in the bfd library for the current
   1006   1.5  christos    architecture, this is usually 8-bits, and so this function will usually
   1007   1.5  christos    return 1 indicating 1 byte is 1 octet.  */
   1008   1.5  christos 
   1009   1.5  christos int
   1010  1.10  christos default_addressable_memory_unit_size (struct gdbarch *gdbarch)
   1011   1.5  christos {
   1012   1.5  christos   return gdbarch_bfd_arch_info (gdbarch)->bits_per_byte / 8;
   1013   1.6  christos }
   1014   1.6  christos 
   1015   1.6  christos void
   1016   1.6  christos default_guess_tracepoint_registers (struct gdbarch *gdbarch,
   1017   1.6  christos 				    struct regcache *regcache,
   1018   1.6  christos 				    CORE_ADDR addr)
   1019   1.6  christos {
   1020   1.6  christos   int pc_regno = gdbarch_pc_regnum (gdbarch);
   1021   1.6  christos   gdb_byte *regs;
   1022   1.6  christos 
   1023   1.6  christos   /* This guessing code below only works if the PC register isn't
   1024   1.6  christos      a pseudo-register.  The value of a pseudo-register isn't stored
   1025   1.6  christos      in the (non-readonly) regcache -- instead it's recomputed
   1026   1.6  christos      (probably from some other cached raw register) whenever the
   1027   1.6  christos      register is read.  In this case, a custom method implementation
   1028   1.6  christos      should be used by the architecture.  */
   1029   1.6  christos   if (pc_regno < 0 || pc_regno >= gdbarch_num_regs (gdbarch))
   1030   1.6  christos     return;
   1031   1.6  christos 
   1032   1.6  christos   regs = (gdb_byte *) alloca (register_size (gdbarch, pc_regno));
   1033   1.8  christos   store_unsigned_integer (regs, register_size (gdbarch, pc_regno),
   1034   1.8  christos 			  gdbarch_byte_order (gdbarch), addr);
   1035   1.8  christos   regcache->raw_supply (pc_regno, regs);
   1036   1.8  christos }
   1037   1.8  christos 
   1038   1.8  christos int
   1039   1.8  christos default_print_insn (bfd_vma memaddr, disassemble_info *info)
   1040   1.8  christos {
   1041   1.8  christos   disassembler_ftype disassemble_fn;
   1042  1.10  christos 
   1043   1.8  christos   disassemble_fn = disassembler (info->arch, info->endian == BFD_ENDIAN_BIG,
   1044   1.8  christos 				 info->mach, current_program_space->exec_bfd ());
   1045  1.12  christos 
   1046  1.12  christos   gdb_assert (disassemble_fn != NULL);
   1047  1.12  christos   int res = (*disassemble_fn) (memaddr, info);
   1048  1.12  christos 
   1049  1.12  christos   QUIT;
   1050   1.6  christos 
   1051   1.6  christos   return res;
   1052   1.7  christos }
   1053   1.7  christos 
   1054   1.7  christos /* See arch-utils.h.  */
   1055   1.7  christos 
   1056   1.7  christos CORE_ADDR
   1057   1.7  christos gdbarch_skip_prologue_noexcept (gdbarch *gdbarch, CORE_ADDR pc) noexcept
   1058   1.7  christos {
   1059   1.9  christos   CORE_ADDR new_pc = pc;
   1060   1.7  christos 
   1061   1.7  christos   try
   1062   1.7  christos     {
   1063   1.9  christos       new_pc = gdbarch_skip_prologue (gdbarch, pc);
   1064   1.7  christos     }
   1065   1.7  christos   catch (const gdb_exception &ex)
   1066   1.7  christos     {}
   1067   1.7  christos 
   1068   1.7  christos   return new_pc;
   1069   1.8  christos }
   1070   1.8  christos 
   1071   1.8  christos /* See arch-utils.h.  */
   1072   1.8  christos 
   1073   1.8  christos bool
   1074   1.8  christos default_in_indirect_branch_thunk (gdbarch *gdbarch, CORE_ADDR pc)
   1075   1.8  christos {
   1076   1.8  christos   return false;
   1077   1.8  christos }
   1078   1.8  christos 
   1079   1.8  christos /* See arch-utils.h.  */
   1080   1.8  christos 
   1081   1.8  christos ULONGEST
   1082   1.9  christos default_type_align (struct gdbarch *gdbarch, struct type *type)
   1083   1.9  christos {
   1084   1.9  christos   return 0;
   1085   1.9  christos }
   1086   1.9  christos 
   1087   1.9  christos /* See arch-utils.h.  */
   1088  1.11  christos 
   1089   1.9  christos std::string
   1090   1.9  christos default_get_pc_address_flags (const frame_info_ptr &frame, CORE_ADDR pc)
   1091   1.9  christos {
   1092   1.9  christos   return "";
   1093   1.9  christos }
   1094   1.9  christos 
   1095  1.10  christos /* See arch-utils.h.  */
   1096  1.10  christos void
   1097  1.10  christos default_read_core_file_mappings
   1098  1.10  christos   (struct gdbarch *gdbarch,
   1099  1.10  christos    struct bfd *cbfd,
   1100  1.10  christos    read_core_file_mappings_pre_loop_ftype pre_loop_cb,
   1101  1.10  christos    read_core_file_mappings_loop_ftype loop_cb)
   1102  1.10  christos {
   1103  1.11  christos }
   1104  1.11  christos 
   1105  1.11  christos /* See arch-utils.h.  */
   1106  1.11  christos bool
   1107  1.11  christos default_use_target_description_from_corefile_notes (struct gdbarch *gdbarch,
   1108  1.11  christos 						    struct bfd *corefile_bfd)
   1109  1.11  christos {
   1110  1.11  christos   /* Always trust the corefile target description contained in the target
   1111  1.11  christos      description note.  */
   1112  1.11  christos   return true;
   1113  1.10  christos }
   1114  1.11  christos 
   1115  1.11  christos CORE_ADDR
   1116  1.10  christos default_get_return_buf_addr (struct type *val_type,
   1117  1.10  christos 			     const frame_info_ptr &cur_frame)
   1118  1.10  christos {
   1119  1.10  christos   return 0;
   1120  1.11  christos }
   1121  1.11  christos 
   1122  1.11  christos bool
   1123  1.11  christos default_dwarf2_omit_typedef_p (struct type *target_type, const char *producer,
   1124  1.11  christos 			       const char *name)
   1125  1.11  christos {
   1126  1.11  christos   return false;
   1127  1.11  christos }
   1128  1.11  christos 
   1129  1.11  christos static CORE_ADDR
   1130  1.11  christos default_update_call_site_pc (struct gdbarch *gdbarch, CORE_ADDR pc)
   1131  1.11  christos {
   1132  1.11  christos   return pc;
   1133  1.10  christos }
   1134  1.10  christos 
   1135  1.10  christos /* Non-zero if we want to trace architecture code.  */
   1136  1.10  christos 
   1137  1.10  christos #ifndef GDBARCH_DEBUG
   1138  1.10  christos #define GDBARCH_DEBUG 0
   1139  1.10  christos #endif
   1140  1.10  christos unsigned int gdbarch_debug = GDBARCH_DEBUG;
   1141  1.10  christos static void
   1142  1.10  christos show_gdbarch_debug (struct ui_file *file, int from_tty,
   1143  1.10  christos 		    struct cmd_list_element *c, const char *value)
   1144  1.10  christos {
   1145  1.10  christos   gdb_printf (file, _("Architecture debugging is %s.\n"), value);
   1146  1.10  christos }
   1147  1.10  christos 
   1148  1.10  christos static const char *
   1149  1.10  christos pformat (struct gdbarch *gdbarch, const struct floatformat **format)
   1150  1.10  christos {
   1151  1.10  christos   if (format == NULL)
   1152  1.10  christos     return "(null)";
   1153  1.10  christos 
   1154  1.10  christos   int format_index = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE ? 1 : 0;
   1155  1.10  christos   return format[format_index]->name;
   1156  1.10  christos }
   1157  1.10  christos 
   1158  1.10  christos static const char *
   1159  1.10  christos pstring (const char *string)
   1160  1.10  christos {
   1161  1.10  christos   if (string == NULL)
   1162  1.10  christos     return "(null)";
   1163  1.10  christos   return string;
   1164  1.10  christos }
   1165  1.11  christos 
   1166  1.10  christos static const char *
   1167  1.11  christos pstring_ptr (std::string *string)
   1168  1.10  christos {
   1169  1.11  christos   if (string == nullptr)
   1170  1.10  christos     return "(null)";
   1171  1.10  christos   return string->c_str ();
   1172  1.10  christos }
   1173  1.10  christos 
   1174  1.10  christos /* Helper function to print a list of strings, represented as "const
   1175  1.10  christos    char *const *".  The list is printed comma-separated.  */
   1176  1.10  christos 
   1177  1.10  christos static const char *
   1178  1.10  christos pstring_list (const char *const *list)
   1179  1.10  christos {
   1180  1.10  christos   static char ret[100];
   1181  1.10  christos   const char *const *p;
   1182  1.10  christos   size_t offset = 0;
   1183  1.10  christos 
   1184  1.10  christos   if (list == NULL)
   1185  1.10  christos     return "(null)";
   1186  1.10  christos 
   1187  1.10  christos   ret[0] = '\0';
   1188  1.10  christos   for (p = list; *p != NULL && offset < sizeof (ret); ++p)
   1189  1.10  christos     {
   1190  1.10  christos       size_t s = xsnprintf (ret + offset, sizeof (ret) - offset, "%s, ", *p);
   1191  1.10  christos       offset += 2 + s;
   1192  1.10  christos     }
   1193  1.10  christos 
   1194  1.10  christos   if (offset > 0)
   1195  1.10  christos     {
   1196  1.10  christos       gdb_assert (offset - 2 < sizeof (ret));
   1197  1.10  christos       ret[offset - 2] = '\0';
   1198  1.10  christos     }
   1199  1.10  christos 
   1200  1.10  christos   return ret;
   1201  1.12  christos }
   1202  1.10  christos 
   1203  1.11  christos #include "gdbarch-gen.c"
   1204  1.11  christos 
   1205  1.11  christos enum return_value_convention
   1206  1.11  christos default_gdbarch_return_value
   1207  1.11  christos      (struct gdbarch *gdbarch, struct value *function, struct type *valtype,
   1208  1.11  christos       struct regcache *regcache, struct value **read_value,
   1209  1.11  christos       const gdb_byte *writebuf)
   1210  1.11  christos {
   1211  1.11  christos   gdb_byte *readbuf = nullptr;
   1212  1.11  christos 
   1213  1.11  christos   if (read_value != nullptr)
   1214  1.11  christos     {
   1215  1.11  christos       *read_value = value::allocate (valtype);
   1216  1.11  christos       readbuf = (*read_value)->contents_raw ().data ();
   1217  1.11  christos     }
   1218  1.11  christos 
   1219  1.11  christos   return gdbarch->return_value (gdbarch, function, valtype, regcache,
   1220  1.11  christos 				readbuf, writebuf);
   1221  1.10  christos }
   1222  1.10  christos 
   1223  1.10  christos obstack *gdbarch_obstack (gdbarch *arch)
   1224  1.10  christos {
   1225  1.10  christos   return &arch->obstack;
   1226  1.10  christos }
   1227  1.10  christos 
   1228  1.10  christos /* See gdbarch.h.  */
   1229  1.10  christos 
   1230  1.10  christos char *
   1231  1.10  christos gdbarch_obstack_strdup (struct gdbarch *arch, const char *string)
   1232  1.10  christos {
   1233  1.10  christos   return obstack_strdup (&arch->obstack, string);
   1234  1.10  christos }
   1235  1.10  christos 
   1236  1.10  christos /* Free a gdbarch struct.  This should never happen in normal
   1237  1.10  christos    operation --- once you've created a gdbarch, you keep it around.
   1238  1.10  christos    However, if an architecture's init function encounters an error
   1239  1.10  christos    building the structure, it may need to clean up a partially
   1240  1.10  christos    constructed gdbarch.  */
   1241  1.10  christos 
   1242  1.10  christos void
   1243  1.10  christos gdbarch_free (struct gdbarch *arch)
   1244  1.10  christos {
   1245  1.10  christos   gdb_assert (arch != NULL);
   1246  1.10  christos   gdb_assert (!arch->initialized_p);
   1247  1.10  christos   delete arch;
   1248  1.10  christos }
   1249  1.10  christos 
   1250  1.10  christos /* See gdbarch.h.  */
   1251  1.10  christos 
   1252  1.10  christos struct gdbarch_tdep_base *
   1253  1.10  christos gdbarch_tdep_1 (struct gdbarch *gdbarch)
   1254  1.10  christos {
   1255  1.11  christos   if (gdbarch_debug >= 2)
   1256  1.10  christos     gdb_printf (gdb_stdlog, "gdbarch_tdep_1 called\n");
   1257  1.10  christos   return gdbarch->tdep.get ();
   1258  1.10  christos }
   1259  1.10  christos 
   1260  1.10  christos registry<gdbarch> *
   1261  1.10  christos registry_accessor<gdbarch>::get (gdbarch *arch)
   1262  1.10  christos {
   1263  1.10  christos   return &arch->registry_fields;
   1264  1.10  christos }
   1265  1.10  christos 
   1266  1.10  christos /* Keep a registry of the architectures known by GDB.  */
   1267  1.10  christos 
   1268  1.10  christos struct gdbarch_registration
   1269  1.10  christos {
   1270  1.10  christos   enum bfd_architecture bfd_architecture;
   1271  1.11  christos   gdbarch_init_ftype *init;
   1272  1.10  christos   gdbarch_dump_tdep_ftype *dump_tdep;
   1273  1.10  christos   gdbarch_supports_arch_info_ftype *supports_arch_info;
   1274  1.10  christos   struct gdbarch_list *arches;
   1275  1.10  christos   struct gdbarch_registration *next;
   1276  1.10  christos };
   1277  1.10  christos 
   1278  1.10  christos static struct gdbarch_registration *gdbarch_registry = NULL;
   1279  1.10  christos 
   1280  1.10  christos std::vector<const char *>
   1281  1.11  christos gdbarch_printable_names ()
   1282  1.10  christos {
   1283  1.10  christos   /* Accumulate a list of names based on the registered list of
   1284  1.10  christos      architectures.  */
   1285  1.10  christos   std::vector<const char *> arches;
   1286  1.10  christos 
   1287  1.10  christos   for (gdbarch_registration *rego = gdbarch_registry;
   1288  1.10  christos        rego != nullptr;
   1289  1.10  christos        rego = rego->next)
   1290  1.10  christos     {
   1291  1.10  christos       const struct bfd_arch_info *ap
   1292  1.10  christos 	= bfd_lookup_arch (rego->bfd_architecture, 0);
   1293  1.10  christos       if (ap == nullptr)
   1294  1.10  christos 	internal_error (_("gdbarch_architecture_names: multi-arch unknown"));
   1295  1.11  christos       do
   1296  1.11  christos 	{
   1297  1.11  christos 	  if (rego->supports_arch_info == nullptr
   1298  1.10  christos 	      || rego->supports_arch_info (ap))
   1299  1.10  christos 	    arches.push_back (ap->printable_name);
   1300  1.10  christos 	  ap = ap->next;
   1301  1.10  christos 	}
   1302  1.10  christos       while (ap != NULL);
   1303  1.10  christos     }
   1304  1.10  christos 
   1305  1.10  christos   return arches;
   1306  1.10  christos }
   1307  1.10  christos 
   1308  1.10  christos 
   1309  1.10  christos void
   1310  1.11  christos gdbarch_register (enum bfd_architecture bfd_architecture,
   1311  1.11  christos 		  gdbarch_init_ftype *init,
   1312  1.10  christos 		  gdbarch_dump_tdep_ftype *dump_tdep,
   1313  1.10  christos 		  gdbarch_supports_arch_info_ftype *supports_arch_info)
   1314  1.10  christos {
   1315  1.10  christos   struct gdbarch_registration **curr;
   1316  1.10  christos   const struct bfd_arch_info *bfd_arch_info;
   1317  1.10  christos 
   1318  1.10  christos   /* Check that BFD recognizes this architecture */
   1319  1.10  christos   bfd_arch_info = bfd_lookup_arch (bfd_architecture, 0);
   1320  1.10  christos   if (bfd_arch_info == NULL)
   1321  1.10  christos     {
   1322  1.10  christos       internal_error (_("gdbarch: Attempt to register "
   1323  1.10  christos 			"unknown architecture (%d)"),
   1324  1.10  christos 		      bfd_architecture);
   1325  1.10  christos     }
   1326  1.10  christos   /* Check that we haven't seen this architecture before.  */
   1327  1.10  christos   for (curr = &gdbarch_registry;
   1328  1.10  christos        (*curr) != NULL;
   1329  1.10  christos        curr = &(*curr)->next)
   1330  1.10  christos     {
   1331  1.10  christos       if (bfd_architecture == (*curr)->bfd_architecture)
   1332  1.10  christos 	internal_error (_("gdbarch: Duplicate registration "
   1333  1.10  christos 			  "of architecture (%s)"),
   1334  1.10  christos 			bfd_arch_info->printable_name);
   1335  1.10  christos     }
   1336  1.10  christos   /* log it */
   1337  1.10  christos   if (gdbarch_debug)
   1338  1.10  christos     gdb_printf (gdb_stdlog, "gdbarch_register (%s, %s)\n",
   1339  1.10  christos 		bfd_arch_info->printable_name,
   1340  1.10  christos 		host_address_to_string (init));
   1341  1.10  christos   /* Append it */
   1342  1.10  christos   (*curr) = XNEW (struct gdbarch_registration);
   1343  1.10  christos   (*curr)->bfd_architecture = bfd_architecture;
   1344  1.11  christos   (*curr)->init = init;
   1345  1.10  christos   (*curr)->dump_tdep = dump_tdep;
   1346  1.10  christos   (*curr)->supports_arch_info = supports_arch_info;
   1347  1.10  christos   (*curr)->arches = NULL;
   1348  1.10  christos   (*curr)->next = NULL;
   1349  1.10  christos }
   1350  1.10  christos 
   1351  1.10  christos /* Look for an architecture using gdbarch_info.  */
   1352  1.10  christos 
   1353  1.10  christos struct gdbarch_list *
   1354  1.10  christos gdbarch_list_lookup_by_info (struct gdbarch_list *arches,
   1355  1.10  christos 			     const struct gdbarch_info *info)
   1356  1.10  christos {
   1357  1.10  christos   for (; arches != NULL; arches = arches->next)
   1358  1.10  christos     {
   1359  1.10  christos       if (info->bfd_arch_info != arches->gdbarch->bfd_arch_info)
   1360  1.10  christos 	continue;
   1361  1.10  christos       if (info->byte_order != arches->gdbarch->byte_order)
   1362  1.10  christos 	continue;
   1363  1.10  christos       if (info->osabi != arches->gdbarch->osabi)
   1364  1.10  christos 	continue;
   1365  1.10  christos       if (info->target_desc != arches->gdbarch->target_desc)
   1366  1.10  christos 	continue;
   1367  1.10  christos       return arches;
   1368  1.10  christos     }
   1369  1.10  christos   return NULL;
   1370  1.10  christos }
   1371  1.10  christos 
   1372  1.10  christos 
   1373  1.10  christos /* Find an architecture that matches the specified INFO.  Create a new
   1374  1.10  christos    architecture if needed.  Return that new architecture.  */
   1375  1.10  christos 
   1376  1.10  christos struct gdbarch *
   1377  1.10  christos gdbarch_find_by_info (struct gdbarch_info info)
   1378  1.10  christos {
   1379  1.10  christos   struct gdbarch *new_gdbarch;
   1380  1.10  christos   struct gdbarch_registration *rego;
   1381  1.10  christos 
   1382  1.10  christos   /* Fill in missing parts of the INFO struct using a number of
   1383  1.10  christos      sources: "set ..."; INFOabfd supplied; and the global
   1384  1.10  christos      defaults.  */
   1385  1.10  christos   gdbarch_info_fill (&info);
   1386  1.10  christos 
   1387  1.10  christos   /* Must have found some sort of architecture.  */
   1388  1.10  christos   gdb_assert (info.bfd_arch_info != nullptr);
   1389  1.10  christos 
   1390  1.10  christos   if (gdbarch_debug)
   1391  1.10  christos     {
   1392  1.10  christos       gdb_printf (gdb_stdlog,
   1393  1.10  christos 		  "gdbarch_find_by_info: info.bfd_arch_info %s\n",
   1394  1.10  christos 		  (info.bfd_arch_info != nullptr
   1395  1.10  christos 		   ? info.bfd_arch_info->printable_name
   1396  1.10  christos 		   : "(null)"));
   1397  1.10  christos       gdb_printf (gdb_stdlog,
   1398  1.10  christos 		  "gdbarch_find_by_info: info.byte_order %d (%s)\n",
   1399  1.10  christos 		  info.byte_order,
   1400  1.10  christos 		  (info.byte_order == BFD_ENDIAN_BIG ? "big"
   1401  1.10  christos 		   : info.byte_order == BFD_ENDIAN_LITTLE ? "little"
   1402  1.10  christos 		   : "default"));
   1403  1.10  christos       gdb_printf (gdb_stdlog,
   1404  1.10  christos 		  "gdbarch_find_by_info: info.osabi %d (%s)\n",
   1405  1.10  christos 		  info.osabi, gdbarch_osabi_name (info.osabi));
   1406  1.10  christos       gdb_printf (gdb_stdlog,
   1407  1.10  christos 		  "gdbarch_find_by_info: info.abfd %s\n",
   1408  1.10  christos 		  host_address_to_string (info.abfd));
   1409  1.10  christos     }
   1410  1.10  christos 
   1411  1.10  christos   /* Find the tdep code that knows about this architecture.  */
   1412  1.10  christos   for (rego = gdbarch_registry;
   1413  1.10  christos        rego != nullptr;
   1414  1.10  christos        rego = rego->next)
   1415  1.10  christos     if (rego->bfd_architecture == info.bfd_arch_info->arch)
   1416  1.10  christos       break;
   1417  1.10  christos   if (rego == nullptr)
   1418  1.10  christos     {
   1419  1.10  christos       if (gdbarch_debug)
   1420  1.10  christos 	gdb_printf (gdb_stdlog, "gdbarch_find_by_info: "
   1421  1.10  christos 		    "No matching architecture\n");
   1422  1.10  christos       return nullptr;
   1423  1.10  christos     }
   1424  1.10  christos 
   1425  1.10  christos   /* Ask the tdep code for an architecture that matches "info".  */
   1426  1.10  christos   new_gdbarch = rego->init (info, rego->arches);
   1427  1.10  christos 
   1428  1.10  christos   /* Did the tdep code like it?  No.  Reject the change and revert to
   1429  1.10  christos      the old architecture.  */
   1430  1.10  christos   if (new_gdbarch == nullptr)
   1431  1.10  christos     {
   1432  1.10  christos       if (gdbarch_debug)
   1433  1.10  christos 	gdb_printf (gdb_stdlog, "gdbarch_find_by_info: "
   1434  1.10  christos 		    "Target rejected architecture\n");
   1435  1.10  christos       return nullptr;
   1436  1.10  christos     }
   1437  1.10  christos 
   1438  1.10  christos   /* Is this a pre-existing architecture (as determined by already
   1439  1.10  christos      being initialized)?  Move it to the front of the architecture
   1440  1.10  christos      list (keeping the list sorted Most Recently Used).  */
   1441  1.10  christos   if (new_gdbarch->initialized_p)
   1442  1.10  christos     {
   1443  1.10  christos       struct gdbarch_list **list;
   1444  1.10  christos       struct gdbarch_list *self;
   1445  1.10  christos       if (gdbarch_debug)
   1446  1.10  christos 	gdb_printf (gdb_stdlog, "gdbarch_find_by_info: "
   1447  1.10  christos 		    "Previous architecture %s (%s) selected\n",
   1448  1.10  christos 		    host_address_to_string (new_gdbarch),
   1449  1.10  christos 		    new_gdbarch->bfd_arch_info->printable_name);
   1450  1.10  christos       /* Find the existing arch in the list.  */
   1451  1.10  christos       for (list = &rego->arches;
   1452  1.10  christos 	   (*list) != nullptr && (*list)->gdbarch != new_gdbarch;
   1453  1.10  christos 	   list = &(*list)->next);
   1454  1.10  christos       /* It had better be in the list of architectures.  */
   1455  1.10  christos       gdb_assert ((*list) != nullptr && (*list)->gdbarch == new_gdbarch);
   1456  1.10  christos       /* Unlink SELF.  */
   1457  1.10  christos       self = (*list);
   1458  1.10  christos       (*list) = self->next;
   1459  1.10  christos       /* Insert SELF at the front.  */
   1460  1.10  christos       self->next = rego->arches;
   1461  1.10  christos       rego->arches = self;
   1462  1.10  christos       /* Return it.  */
   1463  1.10  christos       return new_gdbarch;
   1464  1.10  christos     }
   1465  1.10  christos 
   1466  1.10  christos   /* It's a new architecture.  */
   1467  1.10  christos   if (gdbarch_debug)
   1468  1.10  christos     gdb_printf (gdb_stdlog, "gdbarch_find_by_info: "
   1469  1.10  christos 		"New architecture %s (%s) selected\n",
   1470  1.10  christos 		host_address_to_string (new_gdbarch),
   1471  1.10  christos 		new_gdbarch->bfd_arch_info->printable_name);
   1472  1.10  christos 
   1473  1.10  christos   /* Insert the new architecture into the front of the architecture
   1474  1.10  christos      list (keep the list sorted Most Recently Used).  */
   1475  1.10  christos   {
   1476  1.10  christos     struct gdbarch_list *self = XNEW (struct gdbarch_list);
   1477  1.10  christos     self->next = rego->arches;
   1478  1.10  christos     self->gdbarch = new_gdbarch;
   1479  1.10  christos     rego->arches = self;
   1480  1.10  christos   }
   1481  1.10  christos 
   1482  1.10  christos   /* Check that the newly installed architecture is valid.  Plug in
   1483  1.10  christos      any post init values.  */
   1484  1.10  christos   new_gdbarch->dump_tdep = rego->dump_tdep;
   1485  1.10  christos   verify_gdbarch (new_gdbarch);
   1486  1.10  christos   new_gdbarch->initialized_p = true;
   1487  1.10  christos 
   1488  1.10  christos   if (gdbarch_debug)
   1489  1.11  christos     gdbarch_dump (new_gdbarch, gdb_stdlog);
   1490  1.11  christos 
   1491  1.10  christos   gdb::observers::new_architecture.notify (new_gdbarch);
   1492  1.10  christos 
   1493  1.10  christos   return new_gdbarch;
   1494  1.11  christos }
   1495  1.10  christos 
   1496  1.11  christos /* See gdbarch.h.  */
   1497  1.11  christos 
   1498   1.9  christos bool
   1499  1.11  christos gdbarch_initialized_p (gdbarch *arch)
   1500   1.8  christos {
   1501   1.1  christos   return arch->initialized_p;
   1502  1.12  christos }
   1503  1.12  christos 
   1504  1.12  christos /* See arch-utils.h.  */
   1505  1.12  christos 
   1506  1.12  christos gdb_environ
   1507  1.12  christos core_file_exec_context::environment () const
   1508  1.12  christos {
   1509  1.12  christos   gdb_environ e;
   1510  1.12  christos 
   1511  1.12  christos   for (const auto &entry : m_environment)
   1512  1.12  christos     {
   1513  1.12  christos       char *eq = strchr (entry.get (), '=');
   1514  1.12  christos 
   1515  1.12  christos       /* If there's no '=' character, then skip this entry.  */
   1516  1.12  christos       if (eq == nullptr)
   1517  1.12  christos 	continue;
   1518  1.12  christos 
   1519  1.12  christos       const char *value = eq + 1;
   1520  1.12  christos       const char *var = entry.get ();
   1521  1.12  christos 
   1522  1.12  christos       *eq = '\0';
   1523  1.12  christos       e.set (var, value);
   1524  1.12  christos       *eq = '=';
   1525  1.12  christos     }
   1526  1.12  christos 
   1527  1.12  christos   return e;
   1528   1.9  christos }
   1529   1.1  christos 
   1530   1.9  christos void _initialize_gdbarch_utils ();
   1531   1.1  christos void
   1532   1.1  christos _initialize_gdbarch_utils ()
   1533   1.1  christos {
   1534   1.1  christos   add_setshow_enum_cmd ("endian", class_support,
   1535   1.1  christos 			endian_enum, &set_endian_string,
   1536   1.1  christos 			_("Set endianness of target."),
   1537   1.1  christos 			_("Show endianness of target."),
   1538  1.10  christos 			NULL, set_endian, show_endian,
   1539  1.10  christos 			&setlist, &showlist);
   1540  1.10  christos   add_setshow_zuinteger_cmd ("arch", class_maintenance, &gdbarch_debug, _("\
   1541  1.10  christos Set architecture debugging."), _("\
   1542  1.10  christos Show architecture debugging."), _("\
   1543  1.10  christos When non-zero, architecture debugging is enabled."),
   1544  1.10  christos 			    NULL,
   1545   1.1  christos 			    show_gdbarch_debug,
   1546                 			    &setdebuglist, &showdebuglist);
   1547                 }
   1548