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