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