* defs.h (extract_signed_integer, extract_unsigned_integer,
[deliverable/binutils-gdb.git] / gdb / arch-utils.c
CommitLineData
c0e8c252 1/* Dynamic architecture support for GDB, the GNU debugger.
f4f9705a 2
9b254dd1 3 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
0fb0cc75 4 2008, 2009 Free Software Foundation, Inc.
c0e8c252
AC
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
c0e8c252
AC
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c0e8c252
AC
20
21#include "defs.h"
22
fb6ecb0f 23#include "arch-utils.h"
192cb3d4 24#include "buildsym.h"
c0e8c252
AC
25#include "gdbcmd.h"
26#include "inferior.h" /* enum CALL_DUMMY_LOCATION et.al. */
5f8a3188 27#include "gdb_string.h"
fbec36e2 28#include "regcache.h"
39d4ef09 29#include "gdb_assert.h"
4182591f 30#include "sim-regno.h"
750eb019 31#include "gdbcore.h"
bf922ad9 32#include "osabi.h"
424163ea 33#include "target-descriptions.h"
237fc4c9 34#include "objfiles.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);
47 gdb_byte *buf = xmalloc (len);
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
70
71CORE_ADDR
72displaced_step_at_entry_point (struct gdbarch *gdbarch)
73{
74 CORE_ADDR addr;
75 int bp_len;
76
77 addr = entry_point_address ();
78
79 /* Make certain that the address points at real code, and not a
80 function descriptor. */
81 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr, &current_target);
82
83 /* Inferior calls also use the entry point as a breakpoint location.
84 We don't want displaced stepping to interfere with those
85 breakpoints, so leave space. */
86 gdbarch_breakpoint_from_pc (gdbarch, &addr, &bp_len);
87 addr += bp_len * 2;
88
89 return addr;
90}
91
4182591f 92int
e7faf938 93legacy_register_sim_regno (struct gdbarch *gdbarch, int regnum)
4182591f
AC
94{
95 /* Only makes sense to supply raw registers. */
e7faf938 96 gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch));
4182591f
AC
97 /* NOTE: cagney/2002-05-13: The old code did it this way and it is
98 suspected that some GDB/SIM combinations may rely on this
99 behavour. The default should be one2one_register_sim_regno
100 (below). */
e7faf938
MD
101 if (gdbarch_register_name (gdbarch, regnum) != NULL
102 && gdbarch_register_name (gdbarch, regnum)[0] != '\0')
4182591f
AC
103 return regnum;
104 else
105 return LEGACY_SIM_REGNO_IGNORE;
106}
107
bdcd319a 108CORE_ADDR
52f729a7 109generic_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
bdcd319a
CV
110{
111 return 0;
112}
113
dea0c52f 114CORE_ADDR
4c8c40e6 115generic_skip_solib_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
dea0c52f
MK
116{
117 return 0;
118}
119
d50355b6 120int
e17a4113
UW
121generic_in_solib_return_trampoline (struct gdbarch *gdbarch,
122 CORE_ADDR pc, char *name)
d50355b6
MS
123{
124 return 0;
125}
126
c12260ac
CV
127int
128generic_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
129{
130 return 0;
131}
132
4d1e7dd1 133/* Helper functions for gdbarch_inner_than */
3339cf8b
AC
134
135int
fba45db2 136core_addr_lessthan (CORE_ADDR lhs, CORE_ADDR rhs)
3339cf8b
AC
137{
138 return (lhs < rhs);
139}
140
141int
fba45db2 142core_addr_greaterthan (CORE_ADDR lhs, CORE_ADDR rhs)
3339cf8b
AC
143{
144 return (lhs > rhs);
145}
146
193e3b1a
AC
147/* Misc helper functions for targets. */
148
f517ea4e 149CORE_ADDR
24568a2c 150core_addr_identity (struct gdbarch *gdbarch, CORE_ADDR addr)
f517ea4e
PS
151{
152 return addr;
153}
154
e2d0e7eb
AC
155CORE_ADDR
156convert_from_func_ptr_addr_identity (struct gdbarch *gdbarch, CORE_ADDR addr,
157 struct target_ops *targ)
158{
159 return addr;
160}
161
88c72b7d 162int
d3f73121 163no_op_reg_to_regnum (struct gdbarch *gdbarch, int reg)
88c72b7d
AC
164{
165 return reg;
166}
167
a2cf933a
EZ
168void
169default_elf_make_msymbol_special (asymbol *sym, struct minimal_symbol *msym)
170{
171 return;
172}
173
174void
175default_coff_make_msymbol_special (int val, struct minimal_symbol *msym)
176{
177 return;
178}
179
01fb7433 180int
64a3914f 181cannot_register_not (struct gdbarch *gdbarch, int regnum)
01fb7433
AC
182{
183 return 0;
184}
39d4ef09
AC
185
186/* Legacy version of target_virtual_frame_pointer(). Assumes that
064f5156 187 there is an gdbarch_deprecated_fp_regnum and that it is the same, cooked or
0ba6dca9 188 raw. */
39d4ef09
AC
189
190void
a54fba4c
MD
191legacy_virtual_frame_pointer (struct gdbarch *gdbarch,
192 CORE_ADDR pc,
39d4ef09
AC
193 int *frame_regnum,
194 LONGEST *frame_offset)
195{
20bcf01c
AC
196 /* FIXME: cagney/2002-09-13: This code is used when identifying the
197 frame pointer of the current PC. It is assuming that a single
198 register and an offset can determine this. I think it should
199 instead generate a byte code expression as that would work better
200 with things like Dwarf2's CFI. */
a54fba4c
MD
201 if (gdbarch_deprecated_fp_regnum (gdbarch) >= 0
202 && gdbarch_deprecated_fp_regnum (gdbarch)
203 < gdbarch_num_regs (gdbarch))
204 *frame_regnum = gdbarch_deprecated_fp_regnum (gdbarch);
205 else if (gdbarch_sp_regnum (gdbarch) >= 0
206 && gdbarch_sp_regnum (gdbarch)
207 < gdbarch_num_regs (gdbarch))
208 *frame_regnum = gdbarch_sp_regnum (gdbarch);
20bcf01c
AC
209 else
210 /* Should this be an internal error? I guess so, it is reflecting
211 an architectural limitation in the current design. */
edefbb7c 212 internal_error (__FILE__, __LINE__, _("No virtual frame pointer available"));
39d4ef09
AC
213 *frame_offset = 0;
214}
46cd78fb 215
d7bd68ca 216\f
13d01224 217int
76a8ddb9
UW
218generic_convert_register_p (struct gdbarch *gdbarch, int regnum,
219 struct type *type)
13d01224 220{
9730f241 221 return 0;
13d01224
AC
222}
223
192cb3d4
MK
224int
225default_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
226{
192cb3d4
MK
227 return 0;
228}
229
3ca64954
RC
230int
231generic_instruction_nullified (struct gdbarch *gdbarch,
232 struct regcache *regcache)
233{
234 return 0;
235}
236
123dc839
DJ
237int
238default_remote_register_number (struct gdbarch *gdbarch,
239 int regno)
240{
241 return regno;
242}
243
01fb7433 244\f
b4a20239
AC
245/* Functions to manipulate the endianness of the target. */
246
7a107747 247static int target_byte_order_user = BFD_ENDIAN_UNKNOWN;
b4a20239 248
53904c9e
AC
249static const char endian_big[] = "big";
250static const char endian_little[] = "little";
251static const char endian_auto[] = "auto";
252static const char *endian_enum[] =
b4a20239
AC
253{
254 endian_big,
255 endian_little,
256 endian_auto,
257 NULL,
258};
53904c9e 259static const char *set_endian_string;
b4a20239 260
b6d373df
DJ
261enum bfd_endian
262selected_byte_order (void)
263{
e17c207e 264 return target_byte_order_user;
b6d373df
DJ
265}
266
b4a20239
AC
267/* Called by ``show endian''. */
268
269static void
7ab04401
AC
270show_endian (struct ui_file *file, int from_tty, struct cmd_list_element *c,
271 const char *value)
b4a20239 272{
7b6b9e83 273 if (target_byte_order_user == BFD_ENDIAN_UNKNOWN)
e17c207e 274 if (gdbarch_byte_order (get_current_arch ()) == BFD_ENDIAN_BIG)
7ab04401
AC
275 fprintf_unfiltered (file, _("The target endianness is set automatically "
276 "(currently big endian)\n"));
edefbb7c 277 else
7ab04401 278 fprintf_unfiltered (file, _("The target endianness is set automatically "
edefbb7c 279 "(currently little endian)\n"));
b4a20239 280 else
e17c207e 281 if (target_byte_order_user == BFD_ENDIAN_BIG)
7ab04401
AC
282 fprintf_unfiltered (file,
283 _("The target is assumed to be big endian\n"));
284 else
285 fprintf_unfiltered (file,
286 _("The target is assumed to be little endian\n"));
b4a20239
AC
287}
288
289static void
290set_endian (char *ignore_args, int from_tty, struct cmd_list_element *c)
291{
7a107747
DJ
292 struct gdbarch_info info;
293
294 gdbarch_info_init (&info);
295
3fd3d7d2 296 if (set_endian_string == endian_auto)
b4a20239 297 {
7a107747
DJ
298 target_byte_order_user = BFD_ENDIAN_UNKNOWN;
299 if (! gdbarch_update_p (info))
300 internal_error (__FILE__, __LINE__,
301 _("set_endian: architecture update failed"));
b4a20239
AC
302 }
303 else if (set_endian_string == endian_little)
304 {
d90cf509
AC
305 info.byte_order = BFD_ENDIAN_LITTLE;
306 if (! gdbarch_update_p (info))
edefbb7c 307 printf_unfiltered (_("Little endian target not supported by GDB\n"));
7a107747
DJ
308 else
309 target_byte_order_user = BFD_ENDIAN_LITTLE;
b4a20239
AC
310 }
311 else if (set_endian_string == endian_big)
312 {
d90cf509
AC
313 info.byte_order = BFD_ENDIAN_BIG;
314 if (! gdbarch_update_p (info))
edefbb7c 315 printf_unfiltered (_("Big endian target not supported by GDB\n"));
7a107747
DJ
316 else
317 target_byte_order_user = BFD_ENDIAN_BIG;
b4a20239
AC
318 }
319 else
8e65ff28 320 internal_error (__FILE__, __LINE__,
edefbb7c 321 _("set_endian: bad value"));
7a107747 322
7ab04401 323 show_endian (gdb_stdout, from_tty, NULL, NULL);
b4a20239
AC
324}
325
23181151
DJ
326/* Given SELECTED, a currently selected BFD architecture, and
327 FROM_TARGET, a BFD architecture reported by the target description,
328 return what architecture to use. Either may be NULL; if both are
329 specified, we use the more specific. If the two are obviously
330 incompatible, warn the user. */
331
332static const struct bfd_arch_info *
333choose_architecture_for_target (const struct bfd_arch_info *selected,
334 const struct bfd_arch_info *from_target)
335{
336 const struct bfd_arch_info *compat1, *compat2;
337
338 if (selected == NULL)
339 return from_target;
340
341 if (from_target == NULL)
342 return selected;
343
344 /* struct bfd_arch_info objects are singletons: that is, there's
345 supposed to be exactly one instance for a given machine. So you
346 can tell whether two are equivalent by comparing pointers. */
347 if (from_target == selected)
348 return selected;
349
350 /* BFD's 'A->compatible (A, B)' functions return zero if A and B are
351 incompatible. But if they are compatible, it returns the 'more
352 featureful' of the two arches. That is, if A can run code
353 written for B, but B can't run code written for A, then it'll
354 return A.
355
356 Some targets (e.g. MIPS as of 2006-12-04) don't fully
357 implement this, instead always returning NULL or the first
358 argument. We detect that case by checking both directions. */
359
360 compat1 = selected->compatible (selected, from_target);
361 compat2 = from_target->compatible (from_target, selected);
362
363 if (compat1 == NULL && compat2 == NULL)
364 {
365 warning (_("Selected architecture %s is not compatible "
366 "with reported target architecture %s"),
367 selected->printable_name, from_target->printable_name);
368 return selected;
369 }
370
371 if (compat1 == NULL)
372 return compat2;
373 if (compat2 == NULL)
374 return compat1;
375 if (compat1 == compat2)
376 return compat1;
377
378 /* If the two didn't match, but one of them was a default architecture,
379 assume the more specific one is correct. This handles the case
380 where an executable or target description just says "mips", but
381 the other knows which MIPS variant. */
382 if (compat1->the_default)
383 return compat2;
384 if (compat2->the_default)
385 return compat1;
386
387 /* We have no idea which one is better. This is a bug, but not
388 a critical problem; warn the user. */
389 warning (_("Selected architecture %s is ambiguous with "
390 "reported target architecture %s"),
391 selected->printable_name, from_target->printable_name);
392 return selected;
393}
394
b4a20239
AC
395/* Functions to manipulate the architecture of the target */
396
397enum set_arch { set_arch_auto, set_arch_manual };
398
7a107747 399static const struct bfd_arch_info *target_architecture_user;
b4a20239 400
a8cf2722
AC
401static const char *set_architecture_string;
402
403const char *
404selected_architecture_name (void)
405{
7a107747 406 if (target_architecture_user == NULL)
a8cf2722
AC
407 return NULL;
408 else
409 return set_architecture_string;
410}
b4a20239 411
b4a20239
AC
412/* Called if the user enters ``show architecture'' without an
413 argument. */
414
415static void
7ab04401
AC
416show_architecture (struct ui_file *file, int from_tty,
417 struct cmd_list_element *c, const char *value)
b4a20239 418{
7a107747 419 if (target_architecture_user == NULL)
7ab04401 420 fprintf_filtered (file, _("\
e17c207e
UW
421The target architecture is set automatically (currently %s)\n"),
422 gdbarch_bfd_arch_info (get_current_arch ())->printable_name);
b4a20239 423 else
7ab04401 424 fprintf_filtered (file, _("\
e17c207e 425The target architecture is assumed to be %s\n"), set_architecture_string);
b4a20239
AC
426}
427
428
429/* Called if the user enters ``set architecture'' with or without an
430 argument. */
431
432static void
433set_architecture (char *ignore_args, int from_tty, struct cmd_list_element *c)
434{
7a107747
DJ
435 struct gdbarch_info info;
436
437 gdbarch_info_init (&info);
438
b4a20239
AC
439 if (strcmp (set_architecture_string, "auto") == 0)
440 {
7a107747
DJ
441 target_architecture_user = NULL;
442 if (!gdbarch_update_p (info))
443 internal_error (__FILE__, __LINE__,
444 _("could not select an architecture automatically"));
b4a20239 445 }
d90cf509 446 else
b4a20239 447 {
b4a20239
AC
448 info.bfd_arch_info = bfd_scan_arch (set_architecture_string);
449 if (info.bfd_arch_info == NULL)
8e65ff28 450 internal_error (__FILE__, __LINE__,
edefbb7c 451 _("set_architecture: bfd_scan_arch failed"));
16f33e29 452 if (gdbarch_update_p (info))
7a107747 453 target_architecture_user = info.bfd_arch_info;
b4a20239 454 else
edefbb7c 455 printf_unfiltered (_("Architecture `%s' not recognized.\n"),
b4a20239
AC
456 set_architecture_string);
457 }
7ab04401 458 show_architecture (gdb_stdout, from_tty, NULL, NULL);
b4a20239
AC
459}
460
ebdba546
AC
461/* Try to select a global architecture that matches "info". Return
462 non-zero if the attempt succeds. */
463int
464gdbarch_update_p (struct gdbarch_info info)
465{
a7f1256d
UW
466 struct gdbarch *new_gdbarch;
467
468 /* Check for the current file. */
469 if (info.abfd == NULL)
470 info.abfd = exec_bfd;
471 if (info.abfd == NULL)
472 info.abfd = core_bfd;
473
474 /* Check for the current target description. */
475 if (info.target_desc == NULL)
476 info.target_desc = target_current_description ();
477
478 new_gdbarch = gdbarch_find_by_info (info);
ebdba546
AC
479
480 /* If there no architecture by that name, reject the request. */
481 if (new_gdbarch == NULL)
482 {
483 if (gdbarch_debug)
484 fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
485 "Architecture not found\n");
486 return 0;
487 }
488
489 /* If it is the same old architecture, accept the request (but don't
490 swap anything). */
1cf3db46 491 if (new_gdbarch == target_gdbarch)
ebdba546
AC
492 {
493 if (gdbarch_debug)
494 fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
e3cb3832
JB
495 "Architecture %s (%s) unchanged\n",
496 host_address_to_string (new_gdbarch),
ebdba546
AC
497 gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
498 return 1;
499 }
500
501 /* It's a new architecture, swap it in. */
502 if (gdbarch_debug)
503 fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
e3cb3832
JB
504 "New architecture %s (%s) selected\n",
505 host_address_to_string (new_gdbarch),
ebdba546
AC
506 gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
507 deprecated_current_gdbarch_select_hack (new_gdbarch);
508
509 return 1;
510}
511
2b026650
MK
512/* Return the architecture for ABFD. If no suitable architecture
513 could be find, return NULL. */
514
515struct gdbarch *
516gdbarch_from_bfd (bfd *abfd)
b4a20239 517{
d90cf509
AC
518 struct gdbarch_info info;
519 gdbarch_info_init (&info);
520 info.abfd = abfd;
b60eb90d 521 return gdbarch_find_by_info (info);
2b026650
MK
522}
523
524/* Set the dynamic target-system-dependent parameters (architecture,
525 byte-order) using information found in the BFD */
526
527void
528set_gdbarch_from_file (bfd *abfd)
529{
a7f1256d 530 struct gdbarch_info info;
2b026650
MK
531 struct gdbarch *gdbarch;
532
a7f1256d
UW
533 gdbarch_info_init (&info);
534 info.abfd = abfd;
535 info.target_desc = target_current_description ();
536 gdbarch = gdbarch_find_by_info (info);
537
2b026650 538 if (gdbarch == NULL)
8a3fe4f8 539 error (_("Architecture of file not recognized."));
b60eb90d 540 deprecated_current_gdbarch_select_hack (gdbarch);
b4a20239
AC
541}
542
543/* Initialize the current architecture. Update the ``set
544 architecture'' command so that it specifies a list of valid
545 architectures. */
546
1ba607ad
AC
547#ifdef DEFAULT_BFD_ARCH
548extern const bfd_arch_info_type DEFAULT_BFD_ARCH;
549static const bfd_arch_info_type *default_bfd_arch = &DEFAULT_BFD_ARCH;
550#else
4b9b3959 551static const bfd_arch_info_type *default_bfd_arch;
1ba607ad
AC
552#endif
553
554#ifdef DEFAULT_BFD_VEC
555extern const bfd_target DEFAULT_BFD_VEC;
556static const bfd_target *default_bfd_vec = &DEFAULT_BFD_VEC;
557#else
558static const bfd_target *default_bfd_vec;
559#endif
560
7a107747
DJ
561static int default_byte_order = BFD_ENDIAN_UNKNOWN;
562
b4a20239
AC
563void
564initialize_current_architecture (void)
565{
566 const char **arches = gdbarch_printable_names ();
b4a20239 567
1ba607ad
AC
568 /* determine a default architecture and byte order. */
569 struct gdbarch_info info;
fb6ecb0f 570 gdbarch_info_init (&info);
1ba607ad
AC
571
572 /* Find a default architecture. */
7a107747 573 if (default_bfd_arch == NULL)
b4a20239 574 {
1ba607ad
AC
575 /* Choose the architecture by taking the first one
576 alphabetically. */
577 const char *chosen = arches[0];
b4a20239 578 const char **arch;
b4a20239
AC
579 for (arch = arches; *arch != NULL; arch++)
580 {
b4a20239
AC
581 if (strcmp (*arch, chosen) < 0)
582 chosen = *arch;
583 }
584 if (chosen == NULL)
8e65ff28 585 internal_error (__FILE__, __LINE__,
edefbb7c 586 _("initialize_current_architecture: No arch"));
7a107747
DJ
587 default_bfd_arch = bfd_scan_arch (chosen);
588 if (default_bfd_arch == NULL)
8e65ff28 589 internal_error (__FILE__, __LINE__,
edefbb7c 590 _("initialize_current_architecture: Arch not found"));
1ba607ad
AC
591 }
592
7a107747
DJ
593 info.bfd_arch_info = default_bfd_arch;
594
afe64c1a 595 /* Take several guesses at a byte order. */
7a107747 596 if (default_byte_order == BFD_ENDIAN_UNKNOWN
1ba607ad
AC
597 && default_bfd_vec != NULL)
598 {
599 /* Extract BFD's default vector's byte order. */
600 switch (default_bfd_vec->byteorder)
601 {
602 case BFD_ENDIAN_BIG:
7a107747 603 default_byte_order = BFD_ENDIAN_BIG;
1ba607ad
AC
604 break;
605 case BFD_ENDIAN_LITTLE:
7a107747 606 default_byte_order = BFD_ENDIAN_LITTLE;
1ba607ad
AC
607 break;
608 default:
609 break;
610 }
611 }
7a107747 612 if (default_byte_order == BFD_ENDIAN_UNKNOWN)
1ba607ad
AC
613 {
614 /* look for ``*el-*'' in the target name. */
615 const char *chp;
616 chp = strchr (target_name, '-');
617 if (chp != NULL
618 && chp - 2 >= target_name
619 && strncmp (chp - 2, "el", 2) == 0)
7a107747 620 default_byte_order = BFD_ENDIAN_LITTLE;
1ba607ad 621 }
7a107747 622 if (default_byte_order == BFD_ENDIAN_UNKNOWN)
1ba607ad
AC
623 {
624 /* Wire it to big-endian!!! */
7a107747 625 default_byte_order = BFD_ENDIAN_BIG;
1ba607ad
AC
626 }
627
7a107747 628 info.byte_order = default_byte_order;
9d4fde75 629 info.byte_order_for_code = info.byte_order;
7a107747 630
d90cf509
AC
631 if (! gdbarch_update_p (info))
632 internal_error (__FILE__, __LINE__,
edefbb7c
AC
633 _("initialize_current_architecture: Selection of "
634 "initial architecture failed"));
b4a20239 635
1ba607ad
AC
636 /* Create the ``set architecture'' command appending ``auto'' to the
637 list of architectures. */
b4a20239
AC
638 {
639 struct cmd_list_element *c;
640 /* Append ``auto''. */
641 int nr;
642 for (nr = 0; arches[nr] != NULL; nr++);
643 arches = xrealloc (arches, sizeof (char*) * (nr + 2));
644 arches[nr + 0] = "auto";
645 arches[nr + 1] = NULL;
7ab04401
AC
646 add_setshow_enum_cmd ("architecture", class_support,
647 arches, &set_architecture_string, _("\
648Set architecture of target."), _("\
649Show architecture of target."), NULL,
650 set_architecture, show_architecture,
651 &setlist, &showlist);
b4a20239 652 add_alias_cmd ("processor", "architecture", class_support, 1, &setlist);
b4a20239
AC
653 }
654}
655
656
fb6ecb0f
AC
657/* Initialize a gdbarch info to values that will be automatically
658 overridden. Note: Originally, this ``struct info'' was initialized
ce2826aa 659 using memset(0). Unfortunately, that ran into problems, namely
fb6ecb0f
AC
660 BFD_ENDIAN_BIG is zero. An explicit initialization function that
661 can explicitly set each field to a well defined value is used. */
662
663void
664gdbarch_info_init (struct gdbarch_info *info)
665{
666 memset (info, 0, sizeof (struct gdbarch_info));
428721aa 667 info->byte_order = BFD_ENDIAN_UNKNOWN;
9d4fde75 668 info->byte_order_for_code = info->byte_order;
4be87837 669 info->osabi = GDB_OSABI_UNINITIALIZED;
fb6ecb0f
AC
670}
671
100bcc3f 672/* Similar to init, but this time fill in the blanks. Information is
7a107747
DJ
673 obtained from the global "set ..." options and explicitly
674 initialized INFO fields. */
bf922ad9
AC
675
676void
7a107747 677gdbarch_info_fill (struct gdbarch_info *info)
bf922ad9
AC
678{
679 /* "(gdb) set architecture ...". */
680 if (info->bfd_arch_info == NULL
7a107747
DJ
681 && target_architecture_user)
682 info->bfd_arch_info = target_architecture_user;
424163ea 683 /* From the file. */
bf922ad9
AC
684 if (info->bfd_arch_info == NULL
685 && info->abfd != NULL
686 && bfd_get_arch (info->abfd) != bfd_arch_unknown
687 && bfd_get_arch (info->abfd) != bfd_arch_obscure)
688 info->bfd_arch_info = bfd_get_arch_info (info->abfd);
23181151
DJ
689 /* From the target. */
690 if (info->target_desc != NULL)
691 info->bfd_arch_info = choose_architecture_for_target
692 (info->bfd_arch_info, tdesc_architecture (info->target_desc));
7a107747
DJ
693 /* From the default. */
694 if (info->bfd_arch_info == NULL)
695 info->bfd_arch_info = default_bfd_arch;
bf922ad9
AC
696
697 /* "(gdb) set byte-order ...". */
698 if (info->byte_order == BFD_ENDIAN_UNKNOWN
7a107747
DJ
699 && target_byte_order_user != BFD_ENDIAN_UNKNOWN)
700 info->byte_order = target_byte_order_user;
bf922ad9
AC
701 /* From the INFO struct. */
702 if (info->byte_order == BFD_ENDIAN_UNKNOWN
703 && info->abfd != NULL)
704 info->byte_order = (bfd_big_endian (info->abfd) ? BFD_ENDIAN_BIG
7a107747
DJ
705 : bfd_little_endian (info->abfd) ? BFD_ENDIAN_LITTLE
706 : BFD_ENDIAN_UNKNOWN);
707 /* From the default. */
708 if (info->byte_order == BFD_ENDIAN_UNKNOWN)
709 info->byte_order = default_byte_order;
9d4fde75 710 info->byte_order_for_code = info->byte_order;
bf922ad9
AC
711
712 /* "(gdb) set osabi ...". Handled by gdbarch_lookup_osabi. */
713 if (info->osabi == GDB_OSABI_UNINITIALIZED)
714 info->osabi = gdbarch_lookup_osabi (info->abfd);
bf922ad9
AC
715
716 /* Must have at least filled in the architecture. */
717 gdb_assert (info->bfd_arch_info != NULL);
718}
719
e17c207e
UW
720/* Return "current" architecture. If the target is running, this is the
721 architecture of the selected frame. Otherwise, the "current" architecture
722 defaults to the target architecture.
723
724 This function should normally be called solely by the command interpreter
725 routines to determine the architecture to execute a command in. */
726struct gdbarch *
727get_current_arch (void)
728{
729 if (has_stack_frames ())
730 return get_frame_arch (get_selected_frame (NULL));
731 else
732 return target_gdbarch;
733}
734
c0e8c252
AC
735/* */
736
a78f21af 737extern initialize_file_ftype _initialize_gdbarch_utils; /* -Wmissing-prototypes */
c0e8c252
AC
738
739void
b4a20239 740_initialize_gdbarch_utils (void)
c0e8c252 741{
b4a20239 742 struct cmd_list_element *c;
7ab04401
AC
743 add_setshow_enum_cmd ("endian", class_support,
744 endian_enum, &set_endian_string, _("\
745Set endianness of target."), _("\
746Show endianness of target."), NULL,
747 set_endian, show_endian,
748 &setlist, &showlist);
c0e8c252 749}
This page took 0.566642 seconds and 4 git commands to generate.