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