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