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