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