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