Add NEWS entry.
[deliverable/binutils-gdb.git] / gdb / arch-utils.c
CommitLineData
c0e8c252 1/* Dynamic architecture support for GDB, the GNU debugger.
f4f9705a 2
3666a048 3 Copyright (C) 1998-2021 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
AC
19
20#include "defs.h"
21
fb6ecb0f 22#include "arch-utils.h"
c0e8c252 23#include "gdbcmd.h"
4de283e4 24#include "inferior.h" /* enum CALL_DUMMY_LOCATION et al. */
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"
34
268a13a5 35#include "gdbsupport/version.h"
4de283e4
TT
36
37#include "floatformat.h"
38
39#include "dis-asm.h"
1fd35568 40
07fbbd01 41bool
40a53766 42default_displaced_step_hw_singlestep (struct gdbarch *gdbarch)
99e40580
UW
43{
44 return !gdbarch_software_single_step_p (gdbarch);
45}
237fc4c9
PA
46
47CORE_ADDR
48displaced_step_at_entry_point (struct gdbarch *gdbarch)
49{
50 CORE_ADDR addr;
51 int bp_len;
52
53 addr = entry_point_address ();
54
237fc4c9
PA
55 /* Inferior calls also use the entry point as a breakpoint location.
56 We don't want displaced stepping to interfere with those
57 breakpoints, so leave space. */
58 gdbarch_breakpoint_from_pc (gdbarch, &addr, &bp_len);
5931a2fa 59 addr += bp_len * 2;
237fc4c9
PA
60
61 return addr;
62}
63
4182591f 64int
e7faf938 65legacy_register_sim_regno (struct gdbarch *gdbarch, int regnum)
4182591f
AC
66{
67 /* Only makes sense to supply raw registers. */
e7faf938 68 gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch));
4182591f
AC
69 /* NOTE: cagney/2002-05-13: The old code did it this way and it is
70 suspected that some GDB/SIM combinations may rely on this
85102364 71 behaviour. The default should be one2one_register_sim_regno
4182591f 72 (below). */
e7faf938
MD
73 if (gdbarch_register_name (gdbarch, regnum) != NULL
74 && gdbarch_register_name (gdbarch, regnum)[0] != '\0')
4182591f
AC
75 return regnum;
76 else
77 return LEGACY_SIM_REGNO_IGNORE;
78}
79
c193949e
LM
80
81/* See arch-utils.h */
82
83std::string
84default_memtag_to_string (struct gdbarch *gdbarch, struct value *tag)
85{
86 error (_("This architecture has no method to convert a memory tag to"
87 " a string."));
88}
89
90/* See arch-utils.h */
91
92bool
93default_tagged_address_p (struct gdbarch *gdbarch, struct value *address)
94{
95 /* By default, assume the address is untagged. */
96 return false;
97}
98
99/* See arch-utils.h */
100
101bool
102default_memtag_matches_p (struct gdbarch *gdbarch, struct value *address)
103{
104 /* By default, assume the tags match. */
105 return true;
106}
107
108/* See arch-utils.h */
109
110bool
111default_set_memtags (struct gdbarch *gdbarch, struct value *address,
112 size_t length, const gdb::byte_vector &tags,
113 memtag_type tag_type)
114{
115 /* By default, return true (successful); */
116 return true;
117}
118
119/* See arch-utils.h */
120
121struct value *
122default_get_memtag (struct gdbarch *gdbarch, struct value *address,
123 memtag_type tag_type)
124{
125 /* By default, return no tag. */
126 return nullptr;
127}
128
bdcd319a 129CORE_ADDR
52f729a7 130generic_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
bdcd319a
CV
131{
132 return 0;
133}
134
dea0c52f 135CORE_ADDR
4c8c40e6 136generic_skip_solib_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
dea0c52f
MK
137{
138 return 0;
139}
140
d50355b6 141int
e17a4113 142generic_in_solib_return_trampoline (struct gdbarch *gdbarch,
2c02bd72 143 CORE_ADDR pc, const char *name)
d50355b6
MS
144{
145 return 0;
146}
147
c12260ac 148int
c9cf6e20 149generic_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
c12260ac
CV
150{
151 return 0;
152}
153
7eb89530
YQ
154int
155default_code_of_frame_writable (struct gdbarch *gdbarch,
156 struct frame_info *frame)
157{
158 return 1;
159}
160
4d1e7dd1 161/* Helper functions for gdbarch_inner_than */
3339cf8b
AC
162
163int
fba45db2 164core_addr_lessthan (CORE_ADDR lhs, CORE_ADDR rhs)
3339cf8b
AC
165{
166 return (lhs < rhs);
167}
168
169int
fba45db2 170core_addr_greaterthan (CORE_ADDR lhs, CORE_ADDR rhs)
3339cf8b
AC
171{
172 return (lhs > rhs);
173}
174
0e2de366 175/* Misc helper functions for targets. */
193e3b1a 176
f517ea4e 177CORE_ADDR
24568a2c 178core_addr_identity (struct gdbarch *gdbarch, CORE_ADDR addr)
f517ea4e
PS
179{
180 return addr;
181}
182
e2d0e7eb
AC
183CORE_ADDR
184convert_from_func_ptr_addr_identity (struct gdbarch *gdbarch, CORE_ADDR addr,
185 struct target_ops *targ)
186{
187 return addr;
188}
189
88c72b7d 190int
d3f73121 191no_op_reg_to_regnum (struct gdbarch *gdbarch, int reg)
88c72b7d
AC
192{
193 return reg;
194}
195
a2cf933a 196void
3e29f34a 197default_coff_make_msymbol_special (int val, struct minimal_symbol *msym)
a2cf933a
EZ
198{
199 return;
200}
201
3e29f34a
MR
202/* See arch-utils.h. */
203
a2cf933a 204void
3e29f34a 205default_make_symbol_special (struct symbol *sym, struct objfile *objfile)
a2cf933a
EZ
206{
207 return;
208}
209
3e29f34a
MR
210/* See arch-utils.h. */
211
212CORE_ADDR
213default_adjust_dwarf2_addr (CORE_ADDR pc)
214{
215 return pc;
216}
217
218/* See arch-utils.h. */
219
220CORE_ADDR
221default_adjust_dwarf2_line (CORE_ADDR addr, int rel)
222{
223 return addr;
224}
225
b41c5a85
JW
226/* See arch-utils.h. */
227
228bool
229default_execute_dwarf_cfa_vendor_op (struct gdbarch *gdbarch, gdb_byte op,
230 struct dwarf2_frame_state *fs)
231{
232 return false;
233}
234
01fb7433 235int
64a3914f 236cannot_register_not (struct gdbarch *gdbarch, int regnum)
01fb7433
AC
237{
238 return 0;
239}
39d4ef09
AC
240
241/* Legacy version of target_virtual_frame_pointer(). Assumes that
0e2de366
MS
242 there is an gdbarch_deprecated_fp_regnum and that it is the same,
243 cooked or raw. */
39d4ef09
AC
244
245void
a54fba4c
MD
246legacy_virtual_frame_pointer (struct gdbarch *gdbarch,
247 CORE_ADDR pc,
39d4ef09
AC
248 int *frame_regnum,
249 LONGEST *frame_offset)
250{
20bcf01c
AC
251 /* FIXME: cagney/2002-09-13: This code is used when identifying the
252 frame pointer of the current PC. It is assuming that a single
253 register and an offset can determine this. I think it should
254 instead generate a byte code expression as that would work better
255 with things like Dwarf2's CFI. */
a54fba4c
MD
256 if (gdbarch_deprecated_fp_regnum (gdbarch) >= 0
257 && gdbarch_deprecated_fp_regnum (gdbarch)
258 < gdbarch_num_regs (gdbarch))
259 *frame_regnum = gdbarch_deprecated_fp_regnum (gdbarch);
260 else if (gdbarch_sp_regnum (gdbarch) >= 0
261 && gdbarch_sp_regnum (gdbarch)
dda83cd7 262 < gdbarch_num_regs (gdbarch))
a54fba4c 263 *frame_regnum = gdbarch_sp_regnum (gdbarch);
20bcf01c
AC
264 else
265 /* Should this be an internal error? I guess so, it is reflecting
266 an architectural limitation in the current design. */
0e2de366
MS
267 internal_error (__FILE__, __LINE__,
268 _("No virtual frame pointer available"));
39d4ef09
AC
269 *frame_offset = 0;
270}
46cd78fb 271
9b790ce7
UW
272/* Return a floating-point format for a floating-point variable of
273 length LEN in bits. If non-NULL, NAME is the name of its type.
274 If no suitable type is found, return NULL. */
275
276const struct floatformat **
277default_floatformat_for_type (struct gdbarch *gdbarch,
278 const char *name, int len)
279{
280 const struct floatformat **format = NULL;
281
282 if (len == gdbarch_half_bit (gdbarch))
283 format = gdbarch_half_format (gdbarch);
284 else if (len == gdbarch_float_bit (gdbarch))
285 format = gdbarch_float_format (gdbarch);
286 else if (len == gdbarch_double_bit (gdbarch))
287 format = gdbarch_double_format (gdbarch);
288 else if (len == gdbarch_long_double_bit (gdbarch))
289 format = gdbarch_long_double_format (gdbarch);
290 /* On i386 the 'long double' type takes 96 bits,
291 while the real number of used bits is only 80,
292 both in processor and in memory.
293 The code below accepts the real bit size. */
294 else if (gdbarch_long_double_format (gdbarch) != NULL
295 && len == gdbarch_long_double_format (gdbarch)[0]->totalsize)
296 format = gdbarch_long_double_format (gdbarch);
297
298 return format;
299}
d7bd68ca 300\f
13d01224 301int
76a8ddb9
UW
302generic_convert_register_p (struct gdbarch *gdbarch, int regnum,
303 struct type *type)
13d01224 304{
9730f241 305 return 0;
13d01224
AC
306}
307
192cb3d4
MK
308int
309default_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
310{
192cb3d4
MK
311 return 0;
312}
313
3ca64954
RC
314int
315generic_instruction_nullified (struct gdbarch *gdbarch,
316 struct regcache *regcache)
317{
318 return 0;
319}
320
123dc839
DJ
321int
322default_remote_register_number (struct gdbarch *gdbarch,
323 int regno)
324{
325 return regno;
326}
327
3437254d
PA
328/* See arch-utils.h. */
329
330int
331default_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range)
332{
333 return 0;
334}
335
01fb7433 336\f
b4a20239
AC
337/* Functions to manipulate the endianness of the target. */
338
f486487f 339static enum bfd_endian target_byte_order_user = BFD_ENDIAN_UNKNOWN;
b4a20239 340
53904c9e
AC
341static const char endian_big[] = "big";
342static const char endian_little[] = "little";
343static const char endian_auto[] = "auto";
40478521 344static const char *const endian_enum[] =
b4a20239
AC
345{
346 endian_big,
347 endian_little,
348 endian_auto,
349 NULL,
350};
53904c9e 351static const char *set_endian_string;
b4a20239 352
b6d373df
DJ
353enum bfd_endian
354selected_byte_order (void)
355{
e17c207e 356 return target_byte_order_user;
b6d373df
DJ
357}
358
b4a20239
AC
359/* Called by ``show endian''. */
360
361static void
7ab04401
AC
362show_endian (struct ui_file *file, int from_tty, struct cmd_list_element *c,
363 const char *value)
b4a20239 364{
7b6b9e83 365 if (target_byte_order_user == BFD_ENDIAN_UNKNOWN)
e17c207e 366 if (gdbarch_byte_order (get_current_arch ()) == BFD_ENDIAN_BIG)
7ab04401 367 fprintf_unfiltered (file, _("The target endianness is set automatically "
f63dcaf8 368 "(currently big endian).\n"));
edefbb7c 369 else
7ab04401 370 fprintf_unfiltered (file, _("The target endianness is set automatically "
f63dcaf8 371 "(currently little endian).\n"));
b4a20239 372 else
e17c207e 373 if (target_byte_order_user == BFD_ENDIAN_BIG)
7ab04401 374 fprintf_unfiltered (file,
f63dcaf8 375 _("The target is set to big endian.\n"));
7ab04401
AC
376 else
377 fprintf_unfiltered (file,
f63dcaf8 378 _("The target is set to little endian.\n"));
b4a20239
AC
379}
380
381static void
eb4c3f4a 382set_endian (const char *ignore_args, int from_tty, struct cmd_list_element *c)
b4a20239 383{
7a107747
DJ
384 struct gdbarch_info info;
385
386 gdbarch_info_init (&info);
387
3fd3d7d2 388 if (set_endian_string == endian_auto)
b4a20239 389 {
7a107747
DJ
390 target_byte_order_user = BFD_ENDIAN_UNKNOWN;
391 if (! gdbarch_update_p (info))
392 internal_error (__FILE__, __LINE__,
393 _("set_endian: architecture update failed"));
b4a20239
AC
394 }
395 else if (set_endian_string == endian_little)
396 {
d90cf509
AC
397 info.byte_order = BFD_ENDIAN_LITTLE;
398 if (! gdbarch_update_p (info))
edefbb7c 399 printf_unfiltered (_("Little endian target not supported by GDB\n"));
7a107747
DJ
400 else
401 target_byte_order_user = BFD_ENDIAN_LITTLE;
b4a20239
AC
402 }
403 else if (set_endian_string == endian_big)
404 {
d90cf509
AC
405 info.byte_order = BFD_ENDIAN_BIG;
406 if (! gdbarch_update_p (info))
edefbb7c 407 printf_unfiltered (_("Big endian target not supported by GDB\n"));
7a107747
DJ
408 else
409 target_byte_order_user = BFD_ENDIAN_BIG;
b4a20239
AC
410 }
411 else
8e65ff28 412 internal_error (__FILE__, __LINE__,
edefbb7c 413 _("set_endian: bad value"));
7a107747 414
7ab04401 415 show_endian (gdb_stdout, from_tty, NULL, NULL);
b4a20239
AC
416}
417
23181151 418/* Given SELECTED, a currently selected BFD architecture, and
e35359c5
UW
419 TARGET_DESC, the current target description, return what
420 architecture to use.
421
422 SELECTED may be NULL, in which case we return the architecture
423 associated with TARGET_DESC. If SELECTED specifies a variant
85102364 424 of the architecture associated with TARGET_DESC, return the
e35359c5
UW
425 more specific of the two.
426
427 If SELECTED is a different architecture, but it is accepted as
428 compatible by the target, we can use the target architecture.
429
430 If SELECTED is obviously incompatible, warn the user. */
23181151
DJ
431
432static const struct bfd_arch_info *
e35359c5
UW
433choose_architecture_for_target (const struct target_desc *target_desc,
434 const struct bfd_arch_info *selected)
23181151 435{
e35359c5 436 const struct bfd_arch_info *from_target = tdesc_architecture (target_desc);
23181151
DJ
437 const struct bfd_arch_info *compat1, *compat2;
438
439 if (selected == NULL)
440 return from_target;
441
442 if (from_target == NULL)
443 return selected;
444
445 /* struct bfd_arch_info objects are singletons: that is, there's
446 supposed to be exactly one instance for a given machine. So you
447 can tell whether two are equivalent by comparing pointers. */
448 if (from_target == selected)
449 return selected;
450
451 /* BFD's 'A->compatible (A, B)' functions return zero if A and B are
452 incompatible. But if they are compatible, it returns the 'more
453 featureful' of the two arches. That is, if A can run code
454 written for B, but B can't run code written for A, then it'll
455 return A.
456
457 Some targets (e.g. MIPS as of 2006-12-04) don't fully
458 implement this, instead always returning NULL or the first
459 argument. We detect that case by checking both directions. */
460
461 compat1 = selected->compatible (selected, from_target);
462 compat2 = from_target->compatible (from_target, selected);
463
464 if (compat1 == NULL && compat2 == NULL)
465 {
0e2de366
MS
466 /* BFD considers the architectures incompatible. Check our
467 target description whether it accepts SELECTED as compatible
468 anyway. */
e35359c5
UW
469 if (tdesc_compatible_p (target_desc, selected))
470 return from_target;
471
23181151
DJ
472 warning (_("Selected architecture %s is not compatible "
473 "with reported target architecture %s"),
474 selected->printable_name, from_target->printable_name);
475 return selected;
476 }
477
478 if (compat1 == NULL)
479 return compat2;
480 if (compat2 == NULL)
481 return compat1;
482 if (compat1 == compat2)
483 return compat1;
484
0e2de366
MS
485 /* If the two didn't match, but one of them was a default
486 architecture, assume the more specific one is correct. This
487 handles the case where an executable or target description just
488 says "mips", but the other knows which MIPS variant. */
23181151
DJ
489 if (compat1->the_default)
490 return compat2;
491 if (compat2->the_default)
492 return compat1;
493
494 /* We have no idea which one is better. This is a bug, but not
495 a critical problem; warn the user. */
496 warning (_("Selected architecture %s is ambiguous with "
497 "reported target architecture %s"),
498 selected->printable_name, from_target->printable_name);
499 return selected;
500}
501
0e2de366 502/* Functions to manipulate the architecture of the target. */
b4a20239
AC
503
504enum set_arch { set_arch_auto, set_arch_manual };
505
7a107747 506static const struct bfd_arch_info *target_architecture_user;
b4a20239 507
a8cf2722
AC
508static const char *set_architecture_string;
509
510const char *
511selected_architecture_name (void)
512{
7a107747 513 if (target_architecture_user == NULL)
a8cf2722
AC
514 return NULL;
515 else
516 return set_architecture_string;
517}
b4a20239 518
b4a20239 519/* Called if the user enters ``show architecture'' without an
0e2de366 520 argument. */
b4a20239
AC
521
522static void
7ab04401
AC
523show_architecture (struct ui_file *file, int from_tty,
524 struct cmd_list_element *c, const char *value)
b4a20239 525{
7a107747 526 if (target_architecture_user == NULL)
ccb9eba6
AB
527 fprintf_filtered (file, _("The target architecture is set to "
528 "\"auto\" (currently \"%s\").\n"),
3e43a32a 529 gdbarch_bfd_arch_info (get_current_arch ())->printable_name);
b4a20239 530 else
ccb9eba6 531 fprintf_filtered (file, _("The target architecture is set to \"%s\".\n"),
3e43a32a 532 set_architecture_string);
b4a20239
AC
533}
534
535
536/* Called if the user enters ``set architecture'' with or without an
0e2de366 537 argument. */
b4a20239
AC
538
539static void
eb4c3f4a
TT
540set_architecture (const char *ignore_args,
541 int from_tty, struct cmd_list_element *c)
b4a20239 542{
7a107747
DJ
543 struct gdbarch_info info;
544
545 gdbarch_info_init (&info);
546
b4a20239
AC
547 if (strcmp (set_architecture_string, "auto") == 0)
548 {
7a107747
DJ
549 target_architecture_user = NULL;
550 if (!gdbarch_update_p (info))
551 internal_error (__FILE__, __LINE__,
552 _("could not select an architecture automatically"));
b4a20239 553 }
d90cf509 554 else
b4a20239 555 {
b4a20239
AC
556 info.bfd_arch_info = bfd_scan_arch (set_architecture_string);
557 if (info.bfd_arch_info == NULL)
8e65ff28 558 internal_error (__FILE__, __LINE__,
edefbb7c 559 _("set_architecture: bfd_scan_arch failed"));
16f33e29 560 if (gdbarch_update_p (info))
7a107747 561 target_architecture_user = info.bfd_arch_info;
b4a20239 562 else
edefbb7c 563 printf_unfiltered (_("Architecture `%s' not recognized.\n"),
b4a20239
AC
564 set_architecture_string);
565 }
7ab04401 566 show_architecture (gdb_stdout, from_tty, NULL, NULL);
b4a20239
AC
567}
568
ebdba546 569/* Try to select a global architecture that matches "info". Return
0f9741f2 570 non-zero if the attempt succeeds. */
ebdba546
AC
571int
572gdbarch_update_p (struct gdbarch_info info)
573{
a7f1256d
UW
574 struct gdbarch *new_gdbarch;
575
576 /* Check for the current file. */
577 if (info.abfd == NULL)
7e10abd1 578 info.abfd = current_program_space->exec_bfd ();
a7f1256d
UW
579 if (info.abfd == NULL)
580 info.abfd = core_bfd;
581
582 /* Check for the current target description. */
583 if (info.target_desc == NULL)
584 info.target_desc = target_current_description ();
585
586 new_gdbarch = gdbarch_find_by_info (info);
ebdba546
AC
587
588 /* If there no architecture by that name, reject the request. */
589 if (new_gdbarch == NULL)
590 {
591 if (gdbarch_debug)
592 fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
593 "Architecture not found\n");
594 return 0;
595 }
596
597 /* If it is the same old architecture, accept the request (but don't
598 swap anything). */
f5656ead 599 if (new_gdbarch == target_gdbarch ())
ebdba546
AC
600 {
601 if (gdbarch_debug)
602 fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
e3cb3832
JB
603 "Architecture %s (%s) unchanged\n",
604 host_address_to_string (new_gdbarch),
ebdba546
AC
605 gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
606 return 1;
607 }
608
609 /* It's a new architecture, swap it in. */
610 if (gdbarch_debug)
611 fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
e3cb3832
JB
612 "New architecture %s (%s) selected\n",
613 host_address_to_string (new_gdbarch),
ebdba546 614 gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
aff68abb 615 set_target_gdbarch (new_gdbarch);
ebdba546
AC
616
617 return 1;
618}
619
2b026650
MK
620/* Return the architecture for ABFD. If no suitable architecture
621 could be find, return NULL. */
622
623struct gdbarch *
624gdbarch_from_bfd (bfd *abfd)
b4a20239 625{
d90cf509
AC
626 struct gdbarch_info info;
627 gdbarch_info_init (&info);
05c547f6 628
d90cf509 629 info.abfd = abfd;
b60eb90d 630 return gdbarch_find_by_info (info);
2b026650
MK
631}
632
633/* Set the dynamic target-system-dependent parameters (architecture,
634 byte-order) using information found in the BFD */
635
636void
637set_gdbarch_from_file (bfd *abfd)
638{
a7f1256d 639 struct gdbarch_info info;
2b026650
MK
640 struct gdbarch *gdbarch;
641
a7f1256d
UW
642 gdbarch_info_init (&info);
643 info.abfd = abfd;
644 info.target_desc = target_current_description ();
645 gdbarch = gdbarch_find_by_info (info);
646
2b026650 647 if (gdbarch == NULL)
8a3fe4f8 648 error (_("Architecture of file not recognized."));
aff68abb 649 set_target_gdbarch (gdbarch);
b4a20239
AC
650}
651
652/* Initialize the current architecture. Update the ``set
653 architecture'' command so that it specifies a list of valid
654 architectures. */
655
1ba607ad
AC
656#ifdef DEFAULT_BFD_ARCH
657extern const bfd_arch_info_type DEFAULT_BFD_ARCH;
658static const bfd_arch_info_type *default_bfd_arch = &DEFAULT_BFD_ARCH;
659#else
4b9b3959 660static const bfd_arch_info_type *default_bfd_arch;
1ba607ad
AC
661#endif
662
663#ifdef DEFAULT_BFD_VEC
664extern const bfd_target DEFAULT_BFD_VEC;
665static const bfd_target *default_bfd_vec = &DEFAULT_BFD_VEC;
666#else
667static const bfd_target *default_bfd_vec;
668#endif
669
f486487f 670static enum bfd_endian default_byte_order = BFD_ENDIAN_UNKNOWN;
7a107747 671
b4a20239
AC
672void
673initialize_current_architecture (void)
674{
675 const char **arches = gdbarch_printable_names ();
05c547f6 676 struct gdbarch_info info;
b4a20239 677
0e2de366 678 /* determine a default architecture and byte order. */
fb6ecb0f 679 gdbarch_info_init (&info);
1ba607ad 680
0e2de366 681 /* Find a default architecture. */
7a107747 682 if (default_bfd_arch == NULL)
b4a20239 683 {
1ba607ad 684 /* Choose the architecture by taking the first one
0e2de366 685 alphabetically. */
1ba607ad 686 const char *chosen = arches[0];
b4a20239 687 const char **arch;
b4a20239
AC
688 for (arch = arches; *arch != NULL; arch++)
689 {
b4a20239
AC
690 if (strcmp (*arch, chosen) < 0)
691 chosen = *arch;
692 }
693 if (chosen == NULL)
8e65ff28 694 internal_error (__FILE__, __LINE__,
edefbb7c 695 _("initialize_current_architecture: No arch"));
7a107747
DJ
696 default_bfd_arch = bfd_scan_arch (chosen);
697 if (default_bfd_arch == NULL)
8e65ff28 698 internal_error (__FILE__, __LINE__,
edefbb7c 699 _("initialize_current_architecture: Arch not found"));
1ba607ad
AC
700 }
701
7a107747
DJ
702 info.bfd_arch_info = default_bfd_arch;
703
afe64c1a 704 /* Take several guesses at a byte order. */
7a107747 705 if (default_byte_order == BFD_ENDIAN_UNKNOWN
1ba607ad
AC
706 && default_bfd_vec != NULL)
707 {
0e2de366 708 /* Extract BFD's default vector's byte order. */
1ba607ad
AC
709 switch (default_bfd_vec->byteorder)
710 {
711 case BFD_ENDIAN_BIG:
7a107747 712 default_byte_order = BFD_ENDIAN_BIG;
1ba607ad
AC
713 break;
714 case BFD_ENDIAN_LITTLE:
7a107747 715 default_byte_order = BFD_ENDIAN_LITTLE;
1ba607ad
AC
716 break;
717 default:
718 break;
719 }
720 }
7a107747 721 if (default_byte_order == BFD_ENDIAN_UNKNOWN)
1ba607ad 722 {
0e2de366 723 /* look for ``*el-*'' in the target name. */
1ba607ad
AC
724 const char *chp;
725 chp = strchr (target_name, '-');
726 if (chp != NULL
727 && chp - 2 >= target_name
61012eef 728 && startswith (chp - 2, "el"))
7a107747 729 default_byte_order = BFD_ENDIAN_LITTLE;
1ba607ad 730 }
7a107747 731 if (default_byte_order == BFD_ENDIAN_UNKNOWN)
1ba607ad
AC
732 {
733 /* Wire it to big-endian!!! */
7a107747 734 default_byte_order = BFD_ENDIAN_BIG;
1ba607ad
AC
735 }
736
7a107747 737 info.byte_order = default_byte_order;
9d4fde75 738 info.byte_order_for_code = info.byte_order;
7a107747 739
d90cf509
AC
740 if (! gdbarch_update_p (info))
741 internal_error (__FILE__, __LINE__,
edefbb7c
AC
742 _("initialize_current_architecture: Selection of "
743 "initial architecture failed"));
b4a20239 744
1ba607ad 745 /* Create the ``set architecture'' command appending ``auto'' to the
0e2de366 746 list of architectures. */
b4a20239 747 {
0e2de366 748 /* Append ``auto''. */
b4a20239
AC
749 int nr;
750 for (nr = 0; arches[nr] != NULL; nr++);
224c3ddb 751 arches = XRESIZEVEC (const char *, arches, nr + 2);
b4a20239
AC
752 arches[nr + 0] = "auto";
753 arches[nr + 1] = NULL;
7ab04401 754 add_setshow_enum_cmd ("architecture", class_support,
3e43a32a
MS
755 arches, &set_architecture_string,
756 _("Set architecture of target."),
757 _("Show architecture of target."), NULL,
7ab04401
AC
758 set_architecture, show_architecture,
759 &setlist, &showlist);
b4a20239 760 add_alias_cmd ("processor", "architecture", class_support, 1, &setlist);
b4a20239
AC
761 }
762}
763
764
fb6ecb0f
AC
765/* Initialize a gdbarch info to values that will be automatically
766 overridden. Note: Originally, this ``struct info'' was initialized
ce2826aa 767 using memset(0). Unfortunately, that ran into problems, namely
fb6ecb0f
AC
768 BFD_ENDIAN_BIG is zero. An explicit initialization function that
769 can explicitly set each field to a well defined value is used. */
770
771void
772gdbarch_info_init (struct gdbarch_info *info)
773{
774 memset (info, 0, sizeof (struct gdbarch_info));
428721aa 775 info->byte_order = BFD_ENDIAN_UNKNOWN;
9d4fde75 776 info->byte_order_for_code = info->byte_order;
fb6ecb0f
AC
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
f5656ead 855 return target_gdbarch ();
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
3374165f
SM
1002/* Default method for gdbarch_addressable_memory_unit_size. By default, a memory byte has
1003 a size of 1 octet. */
1004
1005int
1006default_addressable_memory_unit_size (struct gdbarch *gdbarch)
1007{
1008 return 1;
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
1082default_get_pc_address_flags (frame_info *frame, CORE_ADDR pc)
1083{
1084 return "";
1085}
1086
7e183d27
KB
1087/* See arch-utils.h. */
1088void
1089default_read_core_file_mappings (struct gdbarch *gdbarch,
dda83cd7 1090 struct bfd *cbfd,
7e183d27
KB
1091 gdb::function_view<void (ULONGEST count)>
1092 pre_loop_cb,
1093 gdb::function_view<void (int num,
dda83cd7 1094 ULONGEST start,
7e183d27
KB
1095 ULONGEST end,
1096 ULONGEST file_ofs,
70125a45 1097 const char *filename)>
7e183d27
KB
1098 loop_cb)
1099{
1100}
1101
6c265988 1102void _initialize_gdbarch_utils ();
c0e8c252 1103void
6c265988 1104_initialize_gdbarch_utils ()
c0e8c252 1105{
7ab04401 1106 add_setshow_enum_cmd ("endian", class_support,
3e43a32a
MS
1107 endian_enum, &set_endian_string,
1108 _("Set endianness of target."),
1109 _("Show endianness of target."),
1110 NULL, set_endian, show_endian,
7ab04401 1111 &setlist, &showlist);
c0e8c252 1112}
This page took 1.367676 seconds and 4 git commands to generate.