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