2002-08-20 Michael Snyder <msnyder@redhat.com>
[deliverable/binutils-gdb.git] / gdb / arch-utils.c
CommitLineData
c0e8c252 1/* Dynamic architecture support for GDB, the GNU debugger.
f4f9705a
AC
2
3 Copyright 1998, 1999, 2000, 2001, 2002 Free Software Foundation,
4 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
10 the Free Software Foundation; either version 2 of the License, or
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
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23#include "defs.h"
24
25#if GDB_MULTI_ARCH
fb6ecb0f 26#include "arch-utils.h"
c0e8c252
AC
27#include "gdbcmd.h"
28#include "inferior.h" /* enum CALL_DUMMY_LOCATION et.al. */
29#else
30/* Just include everything in sight so that the every old definition
31 of macro is visible. */
c0e8c252
AC
32#include "symtab.h"
33#include "frame.h"
34#include "inferior.h"
35#include "breakpoint.h"
36#include "gdb_wait.h"
37#include "gdbcore.h"
38#include "gdbcmd.h"
39#include "target.h"
c0e8c252 40#include "annotate.h"
c0e8c252 41#endif
5f8a3188 42#include "gdb_string.h"
fbec36e2 43#include "regcache.h"
39d4ef09 44#include "gdb_assert.h"
4182591f 45#include "sim-regno.h"
c0e8c252 46
1ba607ad
AC
47#include "version.h"
48
f0d4cc9e
AC
49#include "floatformat.h"
50
c0e8c252
AC
51/* Use the program counter to determine the contents and size
52 of a breakpoint instruction. If no target-dependent macro
53 BREAKPOINT_FROM_PC has been defined to implement this function,
54 assume that the breakpoint doesn't depend on the PC, and
55 use the values of the BIG_BREAKPOINT and LITTLE_BREAKPOINT macros.
56 Return a pointer to a string of bytes that encode a breakpoint
57 instruction, stores the length of the string to *lenptr,
58 and optionally adjust the pc to point to the correct memory location
59 for inserting the breakpoint. */
60
f4f9705a 61const unsigned char *
c0e8c252
AC
62legacy_breakpoint_from_pc (CORE_ADDR * pcptr, int *lenptr)
63{
64 /* {BIG_,LITTLE_}BREAKPOINT is the sequence of bytes we insert for a
65 breakpoint. On some machines, breakpoints are handled by the
66 target environment and we don't have to worry about them here. */
67#ifdef BIG_BREAKPOINT
d7449b42 68 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
c0e8c252
AC
69 {
70 static unsigned char big_break_insn[] = BIG_BREAKPOINT;
71 *lenptr = sizeof (big_break_insn);
72 return big_break_insn;
73 }
74#endif
75#ifdef LITTLE_BREAKPOINT
d7449b42 76 if (TARGET_BYTE_ORDER != BFD_ENDIAN_BIG)
c0e8c252
AC
77 {
78 static unsigned char little_break_insn[] = LITTLE_BREAKPOINT;
79 *lenptr = sizeof (little_break_insn);
80 return little_break_insn;
81 }
82#endif
83#ifdef BREAKPOINT
84 {
85 static unsigned char break_insn[] = BREAKPOINT;
86 *lenptr = sizeof (break_insn);
87 return break_insn;
88 }
89#endif
90 *lenptr = 0;
91 return NULL;
92}
93
049ee0e4
AC
94/* Implementation of extract return value that grubs around in the
95 register cache. */
96void
97legacy_extract_return_value (struct type *type, struct regcache *regcache,
98 char *valbuf)
99{
100 char *registers = deprecated_grub_regcache_for_registers (regcache);
101 DEPRECATED_EXTRACT_RETURN_VALUE (type, registers, valbuf);
102}
103
4182591f
AC
104int
105legacy_register_sim_regno (int regnum)
106{
107 /* Only makes sense to supply raw registers. */
108 gdb_assert (regnum >= 0 && regnum < NUM_REGS);
109 /* NOTE: cagney/2002-05-13: The old code did it this way and it is
110 suspected that some GDB/SIM combinations may rely on this
111 behavour. The default should be one2one_register_sim_regno
112 (below). */
113 if (REGISTER_NAME (regnum) != NULL
114 && REGISTER_NAME (regnum)[0] != '\0')
115 return regnum;
116 else
117 return LEGACY_SIM_REGNO_IGNORE;
118}
119
c0e8c252
AC
120int
121generic_frameless_function_invocation_not (struct frame_info *fi)
122{
123 return 0;
124}
125
71a9f22e
JB
126int
127generic_return_value_on_stack_not (struct type *type)
128{
129 return 0;
130}
131
bdcd319a
CV
132CORE_ADDR
133generic_skip_trampoline_code (CORE_ADDR pc)
134{
135 return 0;
136}
137
68e9cc94
CV
138int
139generic_in_solib_call_trampoline (CORE_ADDR pc, char *name)
140{
141 return 0;
142}
143
d50355b6
MS
144int
145generic_in_solib_return_trampoline (CORE_ADDR pc, char *name)
146{
147 return 0;
148}
149
c12260ac
CV
150int
151generic_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
152{
153 return 0;
154}
155
fa88f677 156const char *
c0e8c252
AC
157legacy_register_name (int i)
158{
159#ifdef REGISTER_NAMES
160 static char *names[] = REGISTER_NAMES;
161 if (i < 0 || i >= (sizeof (names) / sizeof (*names)))
162 return NULL;
163 else
164 return names[i];
165#else
8e65ff28
AC
166 internal_error (__FILE__, __LINE__,
167 "legacy_register_name: called.");
c0e8c252
AC
168 return NULL;
169#endif
170}
171
172#if defined (CALL_DUMMY)
173LONGEST legacy_call_dummy_words[] = CALL_DUMMY;
174#else
175LONGEST legacy_call_dummy_words[1];
176#endif
177int legacy_sizeof_call_dummy_words = sizeof (legacy_call_dummy_words);
178
179void
180generic_remote_translate_xfer_address (CORE_ADDR gdb_addr, int gdb_len,
181 CORE_ADDR * rem_addr, int *rem_len)
182{
183 *rem_addr = gdb_addr;
184 *rem_len = gdb_len;
185}
186
dad41f9a
AC
187int
188generic_prologue_frameless_p (CORE_ADDR ip)
189{
dad41f9a 190 return ip == SKIP_PROLOGUE (ip);
dad41f9a
AC
191}
192
2bf0cb65
EZ
193/* New/multi-arched targets should use the correct gdbarch field
194 instead of using this global pointer. */
195int
196legacy_print_insn (bfd_vma vma, disassemble_info *info)
197{
198 return (*tm_print_insn) (vma, info);
199}
dad41f9a 200
3339cf8b
AC
201/* Helper functions for INNER_THAN */
202
203int
fba45db2 204core_addr_lessthan (CORE_ADDR lhs, CORE_ADDR rhs)
3339cf8b
AC
205{
206 return (lhs < rhs);
207}
208
209int
fba45db2 210core_addr_greaterthan (CORE_ADDR lhs, CORE_ADDR rhs)
3339cf8b
AC
211{
212 return (lhs > rhs);
213}
214
215
f0d4cc9e
AC
216/* Helper functions for TARGET_{FLOAT,DOUBLE}_FORMAT */
217
218const struct floatformat *
219default_float_format (struct gdbarch *gdbarch)
220{
221#if GDB_MULTI_ARCH
222 int byte_order = gdbarch_byte_order (gdbarch);
223#else
224 int byte_order = TARGET_BYTE_ORDER;
225#endif
226 switch (byte_order)
227 {
d7449b42 228 case BFD_ENDIAN_BIG:
f0d4cc9e 229 return &floatformat_ieee_single_big;
778eb05e 230 case BFD_ENDIAN_LITTLE:
f0d4cc9e
AC
231 return &floatformat_ieee_single_little;
232 default:
8e65ff28
AC
233 internal_error (__FILE__, __LINE__,
234 "default_float_format: bad byte order");
f0d4cc9e
AC
235 }
236}
237
238
239const struct floatformat *
240default_double_format (struct gdbarch *gdbarch)
241{
242#if GDB_MULTI_ARCH
243 int byte_order = gdbarch_byte_order (gdbarch);
244#else
245 int byte_order = TARGET_BYTE_ORDER;
246#endif
247 switch (byte_order)
248 {
d7449b42 249 case BFD_ENDIAN_BIG:
f0d4cc9e 250 return &floatformat_ieee_double_big;
778eb05e 251 case BFD_ENDIAN_LITTLE:
f0d4cc9e
AC
252 return &floatformat_ieee_double_little;
253 default:
8e65ff28
AC
254 internal_error (__FILE__, __LINE__,
255 "default_double_format: bad byte order");
f0d4cc9e
AC
256 }
257}
258
193e3b1a
AC
259/* Misc helper functions for targets. */
260
261int
fba45db2 262frame_num_args_unknown (struct frame_info *fi)
193e3b1a
AC
263{
264 return -1;
265}
266
267
268int
fba45db2 269generic_register_convertible_not (int num)
193e3b1a
AC
270{
271 return 0;
272}
273
b4a20239 274
c8f9d51c
JB
275/* Under some ABI's that specify the `struct convention' for returning
276 structures by value, by the time we've returned from the function,
277 the return value is sitting there in the caller's buffer, but GDB
278 has no way to find the address of that buffer.
279
280 On such architectures, use this function as your
281 extract_struct_value_address method. When asked to a struct
282 returned by value in this fashion, GDB will print a nice error
283 message, instead of garbage. */
284CORE_ADDR
285generic_cannot_extract_struct_value_address (char *dummy)
286{
287 return 0;
288}
289
f517ea4e 290CORE_ADDR
875e1767 291core_addr_identity (CORE_ADDR addr)
f517ea4e
PS
292{
293 return addr;
294}
295
88c72b7d
AC
296int
297no_op_reg_to_regnum (int reg)
298{
299 return reg;
300}
301
c347ee3e
MS
302/* For use by frame_args_address and frame_locals_address. */
303CORE_ADDR
304default_frame_address (struct frame_info *fi)
305{
306 return fi->frame;
307}
308
e02bc4cc
DS
309/* Default prepare_to_procced(). */
310int
311default_prepare_to_proceed (int select_it)
312{
313 return 0;
314}
315
316/* Generic prepare_to_proceed(). This one should be suitable for most
317 targets that support threads. */
318int
319generic_prepare_to_proceed (int select_it)
320{
39f77062 321 ptid_t wait_ptid;
e02bc4cc
DS
322 struct target_waitstatus wait_status;
323
324 /* Get the last target status returned by target_wait(). */
39f77062 325 get_last_target_status (&wait_ptid, &wait_status);
e02bc4cc 326
8849f47d
JL
327 /* Make sure we were stopped either at a breakpoint, or because
328 of a Ctrl-C. */
e02bc4cc 329 if (wait_status.kind != TARGET_WAITKIND_STOPPED
8849f47d
JL
330 || (wait_status.value.sig != TARGET_SIGNAL_TRAP &&
331 wait_status.value.sig != TARGET_SIGNAL_INT))
e02bc4cc
DS
332 {
333 return 0;
334 }
335
39f77062
KB
336 if (!ptid_equal (wait_ptid, minus_one_ptid)
337 && !ptid_equal (inferior_ptid, wait_ptid))
e02bc4cc
DS
338 {
339 /* Switched over from WAIT_PID. */
39f77062 340 CORE_ADDR wait_pc = read_pc_pid (wait_ptid);
e02bc4cc 341
8849f47d 342 if (wait_pc != read_pc ())
e02bc4cc
DS
343 {
344 if (select_it)
345 {
8849f47d 346 /* Switch back to WAIT_PID thread. */
39f77062 347 inferior_ptid = wait_ptid;
e02bc4cc
DS
348
349 /* FIXME: This stuff came from switch_to_thread() in
350 thread.c (which should probably be a public function). */
351 flush_cached_frames ();
352 registers_changed ();
353 stop_pc = wait_pc;
0f7d239c 354 select_frame (get_current_frame ());
e02bc4cc 355 }
8849f47d
JL
356 /* We return 1 to indicate that there is a breakpoint here,
357 so we need to step over it before continuing to avoid
358 hitting it straight away. */
359 if (breakpoint_here_p (wait_pc))
360 {
361 return 1;
362 }
e02bc4cc
DS
363 }
364 }
365 return 0;
366
367}
368
10312cc4
AC
369void
370init_frame_pc_noop (int fromleaf, struct frame_info *prev)
371{
372 return;
373}
374
7824d2f2
AC
375void
376init_frame_pc_default (int fromleaf, struct frame_info *prev)
377{
378 if (fromleaf)
379 prev->pc = SAVED_PC_AFTER_CALL (prev->next);
380 else if (prev->next != NULL)
381 prev->pc = FRAME_SAVED_PC (prev->next);
382 else
383 prev->pc = read_pc ();
384}
385
a2cf933a
EZ
386void
387default_elf_make_msymbol_special (asymbol *sym, struct minimal_symbol *msym)
388{
389 return;
390}
391
392void
393default_coff_make_msymbol_special (int val, struct minimal_symbol *msym)
394{
395 return;
396}
397
01fb7433
AC
398int
399cannot_register_not (int regnum)
400{
401 return 0;
402}
39d4ef09
AC
403
404/* Legacy version of target_virtual_frame_pointer(). Assumes that
405 there is an FP_REGNUM and that it is the same, cooked or raw. */
406
407void
408legacy_virtual_frame_pointer (CORE_ADDR pc,
409 int *frame_regnum,
410 LONGEST *frame_offset)
411{
412 gdb_assert (FP_REGNUM >= 0);
413 *frame_regnum = FP_REGNUM;
414 *frame_offset = 0;
415}
46cd78fb 416
b2e75d78
AC
417/* Assume the world is sane, every register's virtual and real size
418 is identical. */
46cd78fb
AC
419
420int
b2e75d78 421generic_register_size (int regnum)
46cd78fb
AC
422{
423 gdb_assert (regnum >= 0 && regnum < NUM_REGS + NUM_PSEUDO_REGS);
ef8570de 424 return TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (regnum));
ce29138a
MS
425}
426
d7bd68ca
AC
427#if !defined (IN_SIGTRAMP)
428#if defined (SIGTRAMP_START)
429#define IN_SIGTRAMP(pc, name) \
430 ((pc) >= SIGTRAMP_START(pc) \
431 && (pc) < SIGTRAMP_END(pc) \
432 )
433#else
434#define IN_SIGTRAMP(pc, name) \
435 (name && STREQ ("_sigtramp", name))
436#endif
437#endif
a7e3c2ad
AC
438
439/* Assume all registers are adjacent. */
440
441int
442generic_register_byte (int regnum)
443{
444 int byte;
445 int i;
446 gdb_assert (regnum >= 0 && regnum < NUM_REGS + NUM_PSEUDO_REGS);
447 byte = 0;
448 for (i = 0; i < regnum; i++)
449 {
450 byte += TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (i));
451 }
452 return byte;
453}
454
d7bd68ca
AC
455\f
456int
457legacy_pc_in_sigtramp (CORE_ADDR pc, char *name)
458{
459 return IN_SIGTRAMP(pc, name);
460}
461
13d01224
AC
462int
463legacy_convert_register_p (int regnum)
464{
465 return REGISTER_CONVERTIBLE (regnum);
466}
467
468void
469legacy_register_to_value (int regnum, struct type *type,
470 char *from, char *to)
471{
472 REGISTER_CONVERT_TO_VIRTUAL (regnum, type, from, to);
473}
474
475void
476legacy_value_to_register (struct type *type, int regnum,
477 char *from, char *to)
478{
479 REGISTER_CONVERT_TO_RAW (type, regnum, from, to);
480}
481
01fb7433 482\f
b4a20239
AC
483/* Functions to manipulate the endianness of the target. */
484
1ba607ad 485/* ``target_byte_order'' is only used when non- multi-arch.
afe64c1a
AC
486 Multi-arch targets obtain the current byte order using the
487 TARGET_BYTE_ORDER gdbarch method.
488
489 The choice of initial value is entirely arbitrary. During startup,
490 the function initialize_current_architecture() updates this value
491 based on default byte-order information extracted from BFD. */
492int target_byte_order = BFD_ENDIAN_BIG;
b4a20239
AC
493int target_byte_order_auto = 1;
494
53904c9e
AC
495static const char endian_big[] = "big";
496static const char endian_little[] = "little";
497static const char endian_auto[] = "auto";
498static const char *endian_enum[] =
b4a20239
AC
499{
500 endian_big,
501 endian_little,
502 endian_auto,
503 NULL,
504};
53904c9e 505static const char *set_endian_string;
b4a20239
AC
506
507/* Called by ``show endian''. */
508
509static void
510show_endian (char *args, int from_tty)
511{
512 if (TARGET_BYTE_ORDER_AUTO)
513 printf_unfiltered ("The target endianness is set automatically (currently %s endian)\n",
d7449b42 514 (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? "big" : "little"));
b4a20239
AC
515 else
516 printf_unfiltered ("The target is assumed to be %s endian\n",
d7449b42 517 (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? "big" : "little"));
b4a20239
AC
518}
519
520static void
521set_endian (char *ignore_args, int from_tty, struct cmd_list_element *c)
522{
3fd3d7d2 523 if (set_endian_string == endian_auto)
b4a20239
AC
524 {
525 target_byte_order_auto = 1;
526 }
527 else if (set_endian_string == endian_little)
528 {
b4a20239
AC
529 target_byte_order_auto = 0;
530 if (GDB_MULTI_ARCH)
531 {
532 struct gdbarch_info info;
fb6ecb0f 533 gdbarch_info_init (&info);
778eb05e 534 info.byte_order = BFD_ENDIAN_LITTLE;
16f33e29
AC
535 if (! gdbarch_update_p (info))
536 {
537 printf_unfiltered ("Little endian target not supported by GDB\n");
538 }
b4a20239 539 }
1ba607ad
AC
540 else
541 {
778eb05e 542 target_byte_order = BFD_ENDIAN_LITTLE;
1ba607ad 543 }
b4a20239
AC
544 }
545 else if (set_endian_string == endian_big)
546 {
b4a20239
AC
547 target_byte_order_auto = 0;
548 if (GDB_MULTI_ARCH)
549 {
550 struct gdbarch_info info;
fb6ecb0f 551 gdbarch_info_init (&info);
d7449b42 552 info.byte_order = BFD_ENDIAN_BIG;
16f33e29
AC
553 if (! gdbarch_update_p (info))
554 {
555 printf_unfiltered ("Big endian target not supported by GDB\n");
556 }
b4a20239 557 }
1ba607ad
AC
558 else
559 {
d7449b42 560 target_byte_order = BFD_ENDIAN_BIG;
1ba607ad 561 }
b4a20239
AC
562 }
563 else
8e65ff28
AC
564 internal_error (__FILE__, __LINE__,
565 "set_endian: bad value");
b4a20239
AC
566 show_endian (NULL, from_tty);
567}
568
569/* Set the endianness from a BFD. */
570
571static void
572set_endian_from_file (bfd *abfd)
573{
3fd3d7d2 574 int want;
1ba607ad 575 if (GDB_MULTI_ARCH)
8e65ff28
AC
576 internal_error (__FILE__, __LINE__,
577 "set_endian_from_file: not for multi-arch");
3fd3d7d2
AC
578 if (bfd_big_endian (abfd))
579 want = BFD_ENDIAN_BIG;
b4a20239 580 else
3fd3d7d2
AC
581 want = BFD_ENDIAN_LITTLE;
582 if (TARGET_BYTE_ORDER_AUTO)
583 target_byte_order = want;
584 else if (TARGET_BYTE_ORDER != want)
585 warning ("%s endian file does not match %s endian target.",
586 want == BFD_ENDIAN_BIG ? "big" : "little",
587 TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? "big" : "little");
b4a20239
AC
588}
589
590
591/* Functions to manipulate the architecture of the target */
592
593enum set_arch { set_arch_auto, set_arch_manual };
594
595int target_architecture_auto = 1;
596
53904c9e 597const char *set_architecture_string;
b4a20239
AC
598
599/* Old way of changing the current architecture. */
600
601extern const struct bfd_arch_info bfd_default_arch_struct;
602const struct bfd_arch_info *target_architecture = &bfd_default_arch_struct;
603int (*target_architecture_hook) (const struct bfd_arch_info *ap);
604
605static int
606arch_ok (const struct bfd_arch_info *arch)
607{
608 if (GDB_MULTI_ARCH)
8e65ff28
AC
609 internal_error (__FILE__, __LINE__,
610 "arch_ok: not multi-arched");
b4a20239
AC
611 /* Should be performing the more basic check that the binary is
612 compatible with GDB. */
613 /* Check with the target that the architecture is valid. */
614 return (target_architecture_hook == NULL
615 || target_architecture_hook (arch));
616}
617
618static void
619set_arch (const struct bfd_arch_info *arch,
620 enum set_arch type)
621{
622 if (GDB_MULTI_ARCH)
8e65ff28
AC
623 internal_error (__FILE__, __LINE__,
624 "set_arch: not multi-arched");
b4a20239
AC
625 switch (type)
626 {
627 case set_arch_auto:
628 if (!arch_ok (arch))
629 warning ("Target may not support %s architecture",
630 arch->printable_name);
631 target_architecture = arch;
632 break;
633 case set_arch_manual:
634 if (!arch_ok (arch))
635 {
636 printf_unfiltered ("Target does not support `%s' architecture.\n",
637 arch->printable_name);
638 }
639 else
640 {
641 target_architecture_auto = 0;
642 target_architecture = arch;
643 }
644 break;
645 }
646 if (gdbarch_debug)
4b9b3959 647 gdbarch_dump (current_gdbarch, gdb_stdlog);
b4a20239
AC
648}
649
650/* Set the architecture from arch/machine (deprecated) */
651
652void
653set_architecture_from_arch_mach (enum bfd_architecture arch,
654 unsigned long mach)
655{
656 const struct bfd_arch_info *wanted = bfd_lookup_arch (arch, mach);
657 if (GDB_MULTI_ARCH)
8e65ff28
AC
658 internal_error (__FILE__, __LINE__,
659 "set_architecture_from_arch_mach: not multi-arched");
b4a20239
AC
660 if (wanted != NULL)
661 set_arch (wanted, set_arch_manual);
662 else
8e65ff28
AC
663 internal_error (__FILE__, __LINE__,
664 "gdbarch: hardwired architecture/machine not recognized");
b4a20239
AC
665}
666
667/* Set the architecture from a BFD (deprecated) */
668
669static void
670set_architecture_from_file (bfd *abfd)
671{
672 const struct bfd_arch_info *wanted = bfd_get_arch_info (abfd);
673 if (GDB_MULTI_ARCH)
8e65ff28
AC
674 internal_error (__FILE__, __LINE__,
675 "set_architecture_from_file: not multi-arched");
b4a20239
AC
676 if (target_architecture_auto)
677 {
678 set_arch (wanted, set_arch_auto);
679 }
680 else if (wanted != target_architecture)
681 {
682 warning ("%s architecture file may be incompatible with %s target.",
683 wanted->printable_name,
684 target_architecture->printable_name);
685 }
686}
687
688
689/* Called if the user enters ``show architecture'' without an
690 argument. */
691
692static void
693show_architecture (char *args, int from_tty)
694{
695 const char *arch;
696 arch = TARGET_ARCHITECTURE->printable_name;
697 if (target_architecture_auto)
698 printf_filtered ("The target architecture is set automatically (currently %s)\n", arch);
699 else
700 printf_filtered ("The target architecture is assumed to be %s\n", arch);
701}
702
703
704/* Called if the user enters ``set architecture'' with or without an
705 argument. */
706
707static void
708set_architecture (char *ignore_args, int from_tty, struct cmd_list_element *c)
709{
710 if (strcmp (set_architecture_string, "auto") == 0)
711 {
712 target_architecture_auto = 1;
713 }
714 else if (GDB_MULTI_ARCH)
715 {
716 struct gdbarch_info info;
fb6ecb0f 717 gdbarch_info_init (&info);
b4a20239
AC
718 info.bfd_arch_info = bfd_scan_arch (set_architecture_string);
719 if (info.bfd_arch_info == NULL)
8e65ff28
AC
720 internal_error (__FILE__, __LINE__,
721 "set_architecture: bfd_scan_arch failed");
16f33e29 722 if (gdbarch_update_p (info))
b4a20239
AC
723 target_architecture_auto = 0;
724 else
ec3d358c 725 printf_unfiltered ("Architecture `%s' not recognized.\n",
b4a20239
AC
726 set_architecture_string);
727 }
728 else
729 {
730 const struct bfd_arch_info *arch
731 = bfd_scan_arch (set_architecture_string);
732 if (arch == NULL)
8e65ff28
AC
733 internal_error (__FILE__, __LINE__,
734 "set_architecture: bfd_scan_arch failed");
b4a20239
AC
735 set_arch (arch, set_arch_manual);
736 }
737 show_architecture (NULL, from_tty);
738}
739
b7d6b182 740/* Set the dynamic target-system-dependent parameters (architecture,
b4a20239
AC
741 byte-order) using information found in the BFD */
742
743void
fba45db2 744set_gdbarch_from_file (bfd *abfd)
b4a20239
AC
745{
746 if (GDB_MULTI_ARCH)
747 {
748 struct gdbarch_info info;
fb6ecb0f 749 gdbarch_info_init (&info);
b4a20239 750 info.abfd = abfd;
16f33e29 751 if (! gdbarch_update_p (info))
ec3d358c 752 error ("Architecture of file not recognized.\n");
b4a20239
AC
753 }
754 else
755 {
756 set_architecture_from_file (abfd);
757 set_endian_from_file (abfd);
758 }
759}
760
761/* Initialize the current architecture. Update the ``set
762 architecture'' command so that it specifies a list of valid
763 architectures. */
764
1ba607ad
AC
765#ifdef DEFAULT_BFD_ARCH
766extern const bfd_arch_info_type DEFAULT_BFD_ARCH;
767static const bfd_arch_info_type *default_bfd_arch = &DEFAULT_BFD_ARCH;
768#else
4b9b3959 769static const bfd_arch_info_type *default_bfd_arch;
1ba607ad
AC
770#endif
771
772#ifdef DEFAULT_BFD_VEC
773extern const bfd_target DEFAULT_BFD_VEC;
774static const bfd_target *default_bfd_vec = &DEFAULT_BFD_VEC;
775#else
776static const bfd_target *default_bfd_vec;
777#endif
778
b4a20239
AC
779void
780initialize_current_architecture (void)
781{
782 const char **arches = gdbarch_printable_names ();
b4a20239 783
1ba607ad
AC
784 /* determine a default architecture and byte order. */
785 struct gdbarch_info info;
fb6ecb0f 786 gdbarch_info_init (&info);
1ba607ad
AC
787
788 /* Find a default architecture. */
789 if (info.bfd_arch_info == NULL
790 && default_bfd_arch != NULL)
791 info.bfd_arch_info = default_bfd_arch;
792 if (info.bfd_arch_info == NULL)
b4a20239 793 {
1ba607ad
AC
794 /* Choose the architecture by taking the first one
795 alphabetically. */
796 const char *chosen = arches[0];
b4a20239 797 const char **arch;
b4a20239
AC
798 for (arch = arches; *arch != NULL; arch++)
799 {
b4a20239
AC
800 if (strcmp (*arch, chosen) < 0)
801 chosen = *arch;
802 }
803 if (chosen == NULL)
8e65ff28
AC
804 internal_error (__FILE__, __LINE__,
805 "initialize_current_architecture: No arch");
b4a20239
AC
806 info.bfd_arch_info = bfd_scan_arch (chosen);
807 if (info.bfd_arch_info == NULL)
8e65ff28
AC
808 internal_error (__FILE__, __LINE__,
809 "initialize_current_architecture: Arch not found");
1ba607ad
AC
810 }
811
afe64c1a 812 /* Take several guesses at a byte order. */
428721aa 813 if (info.byte_order == BFD_ENDIAN_UNKNOWN
1ba607ad
AC
814 && default_bfd_vec != NULL)
815 {
816 /* Extract BFD's default vector's byte order. */
817 switch (default_bfd_vec->byteorder)
818 {
819 case BFD_ENDIAN_BIG:
d7449b42 820 info.byte_order = BFD_ENDIAN_BIG;
1ba607ad
AC
821 break;
822 case BFD_ENDIAN_LITTLE:
778eb05e 823 info.byte_order = BFD_ENDIAN_LITTLE;
1ba607ad
AC
824 break;
825 default:
826 break;
827 }
828 }
428721aa 829 if (info.byte_order == BFD_ENDIAN_UNKNOWN)
1ba607ad
AC
830 {
831 /* look for ``*el-*'' in the target name. */
832 const char *chp;
833 chp = strchr (target_name, '-');
834 if (chp != NULL
835 && chp - 2 >= target_name
836 && strncmp (chp - 2, "el", 2) == 0)
778eb05e 837 info.byte_order = BFD_ENDIAN_LITTLE;
1ba607ad 838 }
428721aa 839 if (info.byte_order == BFD_ENDIAN_UNKNOWN)
1ba607ad
AC
840 {
841 /* Wire it to big-endian!!! */
d7449b42 842 info.byte_order = BFD_ENDIAN_BIG;
1ba607ad
AC
843 }
844
845 if (GDB_MULTI_ARCH)
846 {
16f33e29
AC
847 if (! gdbarch_update_p (info))
848 {
8e65ff28
AC
849 internal_error (__FILE__, __LINE__,
850 "initialize_current_architecture: Selection of initial architecture failed");
16f33e29 851 }
b4a20239 852 }
ceaa8edf 853 else
afe64c1a
AC
854 {
855 /* If the multi-arch logic comes up with a byte-order (from BFD)
856 use it for the non-multi-arch case. */
857 if (info.byte_order != BFD_ENDIAN_UNKNOWN)
858 target_byte_order = info.byte_order;
859 initialize_non_multiarch ();
860 }
b4a20239 861
1ba607ad
AC
862 /* Create the ``set architecture'' command appending ``auto'' to the
863 list of architectures. */
b4a20239
AC
864 {
865 struct cmd_list_element *c;
866 /* Append ``auto''. */
867 int nr;
868 for (nr = 0; arches[nr] != NULL; nr++);
869 arches = xrealloc (arches, sizeof (char*) * (nr + 2));
870 arches[nr + 0] = "auto";
871 arches[nr + 1] = NULL;
872 /* FIXME: add_set_enum_cmd() uses an array of ``char *'' instead
873 of ``const char *''. We just happen to know that the casts are
874 safe. */
875 c = add_set_enum_cmd ("architecture", class_support,
53904c9e 876 arches, &set_architecture_string,
b4a20239
AC
877 "Set architecture of target.",
878 &setlist);
9f60d481 879 set_cmd_sfunc (c, set_architecture);
b4a20239
AC
880 add_alias_cmd ("processor", "architecture", class_support, 1, &setlist);
881 /* Don't use set_from_show - need to print both auto/manual and
882 current setting. */
883 add_cmd ("architecture", class_support, show_architecture,
884 "Show the current target architecture", &showlist);
b4a20239
AC
885 }
886}
887
888
fb6ecb0f
AC
889/* Initialize a gdbarch info to values that will be automatically
890 overridden. Note: Originally, this ``struct info'' was initialized
891 using memset(0). Unfortunatly, that ran into problems, namely
892 BFD_ENDIAN_BIG is zero. An explicit initialization function that
893 can explicitly set each field to a well defined value is used. */
894
895void
896gdbarch_info_init (struct gdbarch_info *info)
897{
898 memset (info, 0, sizeof (struct gdbarch_info));
428721aa 899 info->byte_order = BFD_ENDIAN_UNKNOWN;
fb6ecb0f
AC
900}
901
c0e8c252
AC
902/* */
903
b4a20239 904extern initialize_file_ftype _initialize_gdbarch_utils;
c0e8c252
AC
905
906void
b4a20239 907_initialize_gdbarch_utils (void)
c0e8c252 908{
b4a20239
AC
909 struct cmd_list_element *c;
910 c = add_set_enum_cmd ("endian", class_support,
911 endian_enum, &set_endian_string,
912 "Set endianness of target.",
913 &setlist);
9f60d481 914 set_cmd_sfunc (c, set_endian);
b4a20239
AC
915 /* Don't use set_from_show - need to print both auto/manual and
916 current setting. */
917 add_cmd ("endian", class_support, show_endian,
918 "Show the current byte-order", &showlist);
c0e8c252 919}
This page took 0.20662 seconds and 4 git commands to generate.