Extend "x" and "print" commands to support memory tagging
[deliverable/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 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 }
300 \f
301 int
302 generic_convert_register_p (struct gdbarch *gdbarch, int regnum,
303 struct type *type)
304 {
305 return 0;
306 }
307
308 int
309 default_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
310 {
311 return 0;
312 }
313
314 int
315 generic_instruction_nullified (struct gdbarch *gdbarch,
316 struct regcache *regcache)
317 {
318 return 0;
319 }
320
321 int
322 default_remote_register_number (struct gdbarch *gdbarch,
323 int regno)
324 {
325 return regno;
326 }
327
328 /* See arch-utils.h. */
329
330 int
331 default_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range)
332 {
333 return 0;
334 }
335
336 \f
337 /* Functions to manipulate the endianness of the target. */
338
339 static enum bfd_endian target_byte_order_user = BFD_ENDIAN_UNKNOWN;
340
341 static const char endian_big[] = "big";
342 static const char endian_little[] = "little";
343 static const char endian_auto[] = "auto";
344 static const char *const endian_enum[] =
345 {
346 endian_big,
347 endian_little,
348 endian_auto,
349 NULL,
350 };
351 static const char *set_endian_string;
352
353 enum bfd_endian
354 selected_byte_order (void)
355 {
356 return target_byte_order_user;
357 }
358
359 /* Called by ``show endian''. */
360
361 static void
362 show_endian (struct ui_file *file, int from_tty, struct cmd_list_element *c,
363 const char *value)
364 {
365 if (target_byte_order_user == BFD_ENDIAN_UNKNOWN)
366 if (gdbarch_byte_order (get_current_arch ()) == BFD_ENDIAN_BIG)
367 fprintf_unfiltered (file, _("The target endianness is set automatically "
368 "(currently big endian).\n"));
369 else
370 fprintf_unfiltered (file, _("The target endianness is set automatically "
371 "(currently little endian).\n"));
372 else
373 if (target_byte_order_user == BFD_ENDIAN_BIG)
374 fprintf_unfiltered (file,
375 _("The target is set to big endian.\n"));
376 else
377 fprintf_unfiltered (file,
378 _("The target is set to little endian.\n"));
379 }
380
381 static void
382 set_endian (const char *ignore_args, int from_tty, struct cmd_list_element *c)
383 {
384 struct gdbarch_info info;
385
386 gdbarch_info_init (&info);
387
388 if (set_endian_string == endian_auto)
389 {
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"));
394 }
395 else if (set_endian_string == endian_little)
396 {
397 info.byte_order = BFD_ENDIAN_LITTLE;
398 if (! gdbarch_update_p (info))
399 printf_unfiltered (_("Little endian target not supported by GDB\n"));
400 else
401 target_byte_order_user = BFD_ENDIAN_LITTLE;
402 }
403 else if (set_endian_string == endian_big)
404 {
405 info.byte_order = BFD_ENDIAN_BIG;
406 if (! gdbarch_update_p (info))
407 printf_unfiltered (_("Big endian target not supported by GDB\n"));
408 else
409 target_byte_order_user = BFD_ENDIAN_BIG;
410 }
411 else
412 internal_error (__FILE__, __LINE__,
413 _("set_endian: bad value"));
414
415 show_endian (gdb_stdout, from_tty, NULL, NULL);
416 }
417
418 /* Given SELECTED, a currently selected BFD architecture, and
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
424 of the architecture associated with TARGET_DESC, return the
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. */
431
432 static const struct bfd_arch_info *
433 choose_architecture_for_target (const struct target_desc *target_desc,
434 const struct bfd_arch_info *selected)
435 {
436 const struct bfd_arch_info *from_target = tdesc_architecture (target_desc);
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 {
466 /* BFD considers the architectures incompatible. Check our
467 target description whether it accepts SELECTED as compatible
468 anyway. */
469 if (tdesc_compatible_p (target_desc, selected))
470 return from_target;
471
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
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. */
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
502 /* Functions to manipulate the architecture of the target. */
503
504 enum set_arch { set_arch_auto, set_arch_manual };
505
506 static const struct bfd_arch_info *target_architecture_user;
507
508 static const char *set_architecture_string;
509
510 const char *
511 selected_architecture_name (void)
512 {
513 if (target_architecture_user == NULL)
514 return NULL;
515 else
516 return set_architecture_string;
517 }
518
519 /* Called if the user enters ``show architecture'' without an
520 argument. */
521
522 static void
523 show_architecture (struct ui_file *file, int from_tty,
524 struct cmd_list_element *c, const char *value)
525 {
526 if (target_architecture_user == NULL)
527 fprintf_filtered (file, _("The target architecture is set to "
528 "\"auto\" (currently \"%s\").\n"),
529 gdbarch_bfd_arch_info (get_current_arch ())->printable_name);
530 else
531 fprintf_filtered (file, _("The target architecture is set to \"%s\".\n"),
532 set_architecture_string);
533 }
534
535
536 /* Called if the user enters ``set architecture'' with or without an
537 argument. */
538
539 static void
540 set_architecture (const char *ignore_args,
541 int from_tty, struct cmd_list_element *c)
542 {
543 struct gdbarch_info info;
544
545 gdbarch_info_init (&info);
546
547 if (strcmp (set_architecture_string, "auto") == 0)
548 {
549 target_architecture_user = NULL;
550 if (!gdbarch_update_p (info))
551 internal_error (__FILE__, __LINE__,
552 _("could not select an architecture automatically"));
553 }
554 else
555 {
556 info.bfd_arch_info = bfd_scan_arch (set_architecture_string);
557 if (info.bfd_arch_info == NULL)
558 internal_error (__FILE__, __LINE__,
559 _("set_architecture: bfd_scan_arch failed"));
560 if (gdbarch_update_p (info))
561 target_architecture_user = info.bfd_arch_info;
562 else
563 printf_unfiltered (_("Architecture `%s' not recognized.\n"),
564 set_architecture_string);
565 }
566 show_architecture (gdb_stdout, from_tty, NULL, NULL);
567 }
568
569 /* Try to select a global architecture that matches "info". Return
570 non-zero if the attempt succeeds. */
571 int
572 gdbarch_update_p (struct gdbarch_info info)
573 {
574 struct gdbarch *new_gdbarch;
575
576 /* Check for the current file. */
577 if (info.abfd == NULL)
578 info.abfd = current_program_space->exec_bfd ();
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);
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). */
599 if (new_gdbarch == target_gdbarch ())
600 {
601 if (gdbarch_debug)
602 fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
603 "Architecture %s (%s) unchanged\n",
604 host_address_to_string (new_gdbarch),
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: "
612 "New architecture %s (%s) selected\n",
613 host_address_to_string (new_gdbarch),
614 gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
615 set_target_gdbarch (new_gdbarch);
616
617 return 1;
618 }
619
620 /* Return the architecture for ABFD. If no suitable architecture
621 could be find, return NULL. */
622
623 struct gdbarch *
624 gdbarch_from_bfd (bfd *abfd)
625 {
626 struct gdbarch_info info;
627 gdbarch_info_init (&info);
628
629 info.abfd = abfd;
630 return gdbarch_find_by_info (info);
631 }
632
633 /* Set the dynamic target-system-dependent parameters (architecture,
634 byte-order) using information found in the BFD */
635
636 void
637 set_gdbarch_from_file (bfd *abfd)
638 {
639 struct gdbarch_info info;
640 struct gdbarch *gdbarch;
641
642 gdbarch_info_init (&info);
643 info.abfd = abfd;
644 info.target_desc = target_current_description ();
645 gdbarch = gdbarch_find_by_info (info);
646
647 if (gdbarch == NULL)
648 error (_("Architecture of file not recognized."));
649 set_target_gdbarch (gdbarch);
650 }
651
652 /* Initialize the current architecture. Update the ``set
653 architecture'' command so that it specifies a list of valid
654 architectures. */
655
656 #ifdef DEFAULT_BFD_ARCH
657 extern const bfd_arch_info_type DEFAULT_BFD_ARCH;
658 static const bfd_arch_info_type *default_bfd_arch = &DEFAULT_BFD_ARCH;
659 #else
660 static const bfd_arch_info_type *default_bfd_arch;
661 #endif
662
663 #ifdef DEFAULT_BFD_VEC
664 extern const bfd_target DEFAULT_BFD_VEC;
665 static const bfd_target *default_bfd_vec = &DEFAULT_BFD_VEC;
666 #else
667 static const bfd_target *default_bfd_vec;
668 #endif
669
670 static enum bfd_endian default_byte_order = BFD_ENDIAN_UNKNOWN;
671
672 void
673 initialize_current_architecture (void)
674 {
675 const char **arches = gdbarch_printable_names ();
676 struct gdbarch_info info;
677
678 /* determine a default architecture and byte order. */
679 gdbarch_info_init (&info);
680
681 /* Find a default architecture. */
682 if (default_bfd_arch == NULL)
683 {
684 /* Choose the architecture by taking the first one
685 alphabetically. */
686 const char *chosen = arches[0];
687 const char **arch;
688 for (arch = arches; *arch != NULL; arch++)
689 {
690 if (strcmp (*arch, chosen) < 0)
691 chosen = *arch;
692 }
693 if (chosen == NULL)
694 internal_error (__FILE__, __LINE__,
695 _("initialize_current_architecture: No arch"));
696 default_bfd_arch = bfd_scan_arch (chosen);
697 if (default_bfd_arch == NULL)
698 internal_error (__FILE__, __LINE__,
699 _("initialize_current_architecture: Arch not found"));
700 }
701
702 info.bfd_arch_info = default_bfd_arch;
703
704 /* Take several guesses at a byte order. */
705 if (default_byte_order == BFD_ENDIAN_UNKNOWN
706 && default_bfd_vec != NULL)
707 {
708 /* Extract BFD's default vector's byte order. */
709 switch (default_bfd_vec->byteorder)
710 {
711 case BFD_ENDIAN_BIG:
712 default_byte_order = BFD_ENDIAN_BIG;
713 break;
714 case BFD_ENDIAN_LITTLE:
715 default_byte_order = BFD_ENDIAN_LITTLE;
716 break;
717 default:
718 break;
719 }
720 }
721 if (default_byte_order == BFD_ENDIAN_UNKNOWN)
722 {
723 /* look for ``*el-*'' in the target name. */
724 const char *chp;
725 chp = strchr (target_name, '-');
726 if (chp != NULL
727 && chp - 2 >= target_name
728 && startswith (chp - 2, "el"))
729 default_byte_order = BFD_ENDIAN_LITTLE;
730 }
731 if (default_byte_order == BFD_ENDIAN_UNKNOWN)
732 {
733 /* Wire it to big-endian!!! */
734 default_byte_order = BFD_ENDIAN_BIG;
735 }
736
737 info.byte_order = default_byte_order;
738 info.byte_order_for_code = info.byte_order;
739
740 if (! gdbarch_update_p (info))
741 internal_error (__FILE__, __LINE__,
742 _("initialize_current_architecture: Selection of "
743 "initial architecture failed"));
744
745 /* Create the ``set architecture'' command appending ``auto'' to the
746 list of architectures. */
747 {
748 /* Append ``auto''. */
749 int nr;
750 for (nr = 0; arches[nr] != NULL; nr++);
751 arches = XRESIZEVEC (const char *, arches, nr + 2);
752 arches[nr + 0] = "auto";
753 arches[nr + 1] = NULL;
754 add_setshow_enum_cmd ("architecture", class_support,
755 arches, &set_architecture_string,
756 _("Set architecture of target."),
757 _("Show architecture of target."), NULL,
758 set_architecture, show_architecture,
759 &setlist, &showlist);
760 add_alias_cmd ("processor", "architecture", class_support, 1, &setlist);
761 }
762 }
763
764
765 /* Initialize a gdbarch info to values that will be automatically
766 overridden. Note: Originally, this ``struct info'' was initialized
767 using memset(0). Unfortunately, that ran into problems, namely
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
771 void
772 gdbarch_info_init (struct gdbarch_info *info)
773 {
774 memset (info, 0, sizeof (struct gdbarch_info));
775 info->byte_order = BFD_ENDIAN_UNKNOWN;
776 info->byte_order_for_code = info->byte_order;
777 }
778
779 /* Similar to init, but this time fill in the blanks. Information is
780 obtained from the global "set ..." options and explicitly
781 initialized INFO fields. */
782
783 void
784 gdbarch_info_fill (struct gdbarch_info *info)
785 {
786 /* "(gdb) set architecture ...". */
787 if (info->bfd_arch_info == NULL
788 && target_architecture_user)
789 info->bfd_arch_info = target_architecture_user;
790 /* From the file. */
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);
796 /* From the target. */
797 if (info->target_desc != NULL)
798 info->bfd_arch_info = choose_architecture_for_target
799 (info->target_desc, info->bfd_arch_info);
800 /* From the default. */
801 if (info->bfd_arch_info == NULL)
802 info->bfd_arch_info = default_bfd_arch;
803
804 /* "(gdb) set byte-order ...". */
805 if (info->byte_order == BFD_ENDIAN_UNKNOWN
806 && target_byte_order_user != BFD_ENDIAN_UNKNOWN)
807 info->byte_order = target_byte_order_user;
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
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;
817 info->byte_order_for_code = info->byte_order;
818 /* Wire the default to the last selected byte order. */
819 default_byte_order = info->byte_order;
820
821 /* "(gdb) set osabi ...". Handled by gdbarch_lookup_osabi. */
822 /* From the manual override, or from file. */
823 if (info->osabi == GDB_OSABI_UNKNOWN)
824 info->osabi = gdbarch_lookup_osabi (info->abfd);
825 /* From the target. */
826
827 if (info->osabi == GDB_OSABI_UNKNOWN && info->target_desc != NULL)
828 info->osabi = tdesc_osabi (info->target_desc);
829 /* From the configured default. */
830 #ifdef GDB_OSABI_DEFAULT
831 if (info->osabi == GDB_OSABI_UNKNOWN)
832 info->osabi = GDB_OSABI_DEFAULT;
833 #endif
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;
837
838 /* Must have at least filled in the architecture. */
839 gdb_assert (info->bfd_arch_info != NULL);
840 }
841
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.
845
846 This function should normally be called solely by the command
847 interpreter routines to determine the architecture to execute a
848 command in. */
849 struct gdbarch *
850 get_current_arch (void)
851 {
852 if (has_stack_frames ())
853 return get_frame_arch (get_selected_frame (NULL));
854 else
855 return target_gdbarch ();
856 }
857
858 int
859 default_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
866 int
867 default_fast_tracepoint_valid_at (struct gdbarch *gdbarch, CORE_ADDR addr,
868 std::string *msg)
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)
873 msg->clear ();
874 return 1;
875 }
876
877 const gdb_byte *
878 default_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 }
885 int
886 default_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
893
894 void
895 default_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
902 int
903 default_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. */
909 return !(language_pass_by_reference (type).trivially_copyable);
910 }
911
912 int default_insn_is_call (struct gdbarch *gdbarch, CORE_ADDR addr)
913 {
914 return 0;
915 }
916
917 int default_insn_is_ret (struct gdbarch *gdbarch, CORE_ADDR addr)
918 {
919 return 0;
920 }
921
922 int default_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
923 {
924 return 0;
925 }
926
927 /* See arch-utils.h. */
928
929 bool
930 default_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
959 void
960 default_skip_permanent_breakpoint (struct regcache *regcache)
961 {
962 struct gdbarch *gdbarch = regcache->arch ();
963 CORE_ADDR current_pc = regcache_read_pc (regcache);
964 int bp_len;
965
966 gdbarch_breakpoint_from_pc (gdbarch, &current_pc, &bp_len);
967 current_pc += bp_len;
968 regcache_write_pc (regcache, current_pc);
969 }
970
971 CORE_ADDR
972 default_infcall_mmap (CORE_ADDR size, unsigned prot)
973 {
974 error (_("This target does not support inferior memory allocation by mmap."));
975 }
976
977 void
978 default_infcall_munmap (CORE_ADDR addr, CORE_ADDR size)
979 {
980 /* Memory reserved by inferior mmap is kept leaked. */
981 }
982
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
986 std::string
987 default_gcc_target_options (struct gdbarch *gdbarch)
988 {
989 return string_printf ("-m%d%s", gdbarch_ptr_bit (gdbarch),
990 (gdbarch_ptr_bit (gdbarch) == 64
991 ? " -mcmodel=large" : ""));
992 }
993
994 /* gdbarch gnu_triplet_regexp method. */
995
996 const char *
997 default_gnu_triplet_regexp (struct gdbarch *gdbarch)
998 {
999 return gdbarch_bfd_arch_info (gdbarch)->arch_name;
1000 }
1001
1002 /* Default method for gdbarch_addressable_memory_unit_size. By default, a memory byte has
1003 a size of 1 octet. */
1004
1005 int
1006 default_addressable_memory_unit_size (struct gdbarch *gdbarch)
1007 {
1008 return 1;
1009 }
1010
1011 void
1012 default_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);
1031 regcache->raw_supply (pc_regno, regs);
1032 }
1033
1034 int
1035 default_print_insn (bfd_vma memaddr, disassemble_info *info)
1036 {
1037 disassembler_ftype disassemble_fn;
1038
1039 disassemble_fn = disassembler (info->arch, info->endian == BFD_ENDIAN_BIG,
1040 info->mach, current_program_space->exec_bfd ());
1041
1042 gdb_assert (disassemble_fn != NULL);
1043 return (*disassemble_fn) (memaddr, info);
1044 }
1045
1046 /* See arch-utils.h. */
1047
1048 CORE_ADDR
1049 gdbarch_skip_prologue_noexcept (gdbarch *gdbarch, CORE_ADDR pc) noexcept
1050 {
1051 CORE_ADDR new_pc = pc;
1052
1053 try
1054 {
1055 new_pc = gdbarch_skip_prologue (gdbarch, pc);
1056 }
1057 catch (const gdb_exception &ex)
1058 {}
1059
1060 return new_pc;
1061 }
1062
1063 /* See arch-utils.h. */
1064
1065 bool
1066 default_in_indirect_branch_thunk (gdbarch *gdbarch, CORE_ADDR pc)
1067 {
1068 return false;
1069 }
1070
1071 /* See arch-utils.h. */
1072
1073 ULONGEST
1074 default_type_align (struct gdbarch *gdbarch, struct type *type)
1075 {
1076 return 0;
1077 }
1078
1079 /* See arch-utils.h. */
1080
1081 std::string
1082 default_get_pc_address_flags (frame_info *frame, CORE_ADDR pc)
1083 {
1084 return "";
1085 }
1086
1087 /* See arch-utils.h. */
1088 void
1089 default_read_core_file_mappings (struct gdbarch *gdbarch,
1090 struct bfd *cbfd,
1091 gdb::function_view<void (ULONGEST count)>
1092 pre_loop_cb,
1093 gdb::function_view<void (int num,
1094 ULONGEST start,
1095 ULONGEST end,
1096 ULONGEST file_ofs,
1097 const char *filename)>
1098 loop_cb)
1099 {
1100 }
1101
1102 void _initialize_gdbarch_utils ();
1103 void
1104 _initialize_gdbarch_utils ()
1105 {
1106 add_setshow_enum_cmd ("endian", class_support,
1107 endian_enum, &set_endian_string,
1108 _("Set endianness of target."),
1109 _("Show endianness of target."),
1110 NULL, set_endian, show_endian,
1111 &setlist, &showlist);
1112 }
This page took 0.052685 seconds and 4 git commands to generate.