2002-08-21 Andrew Cagney <ac131313@redhat.com>
[deliverable/binutils-gdb.git] / gdb / arch-utils.c
1 /* Dynamic architecture support for GDB, the GNU debugger.
2
3 Copyright 1998, 1999, 2000, 2001, 2002 Free Software Foundation,
4 Inc.
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
26 #include "arch-utils.h"
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 "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"
40 #include "annotate.h"
41 #endif
42 #include "gdb_string.h"
43 #include "regcache.h"
44 #include "gdb_assert.h"
45 #include "sim-regno.h"
46
47 #include "version.h"
48
49 #include "floatformat.h"
50
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
61 const unsigned char *
62 legacy_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
68 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
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
76 if (TARGET_BYTE_ORDER != BFD_ENDIAN_BIG)
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
94 /* Implementation of extract return value that grubs around in the
95 register cache. */
96 void
97 legacy_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
104 int
105 legacy_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
120 int
121 generic_frameless_function_invocation_not (struct frame_info *fi)
122 {
123 return 0;
124 }
125
126 int
127 generic_return_value_on_stack_not (struct type *type)
128 {
129 return 0;
130 }
131
132 CORE_ADDR
133 generic_skip_trampoline_code (CORE_ADDR pc)
134 {
135 return 0;
136 }
137
138 int
139 generic_in_solib_call_trampoline (CORE_ADDR pc, char *name)
140 {
141 return 0;
142 }
143
144 int
145 generic_in_solib_return_trampoline (CORE_ADDR pc, char *name)
146 {
147 return 0;
148 }
149
150 int
151 generic_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
152 {
153 return 0;
154 }
155
156 const char *
157 legacy_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
166 internal_error (__FILE__, __LINE__,
167 "legacy_register_name: called.");
168 return NULL;
169 #endif
170 }
171
172 #if defined (CALL_DUMMY)
173 LONGEST legacy_call_dummy_words[] = CALL_DUMMY;
174 #else
175 LONGEST legacy_call_dummy_words[1];
176 #endif
177 int legacy_sizeof_call_dummy_words = sizeof (legacy_call_dummy_words);
178
179 void
180 generic_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
187 int
188 generic_prologue_frameless_p (CORE_ADDR ip)
189 {
190 return ip == SKIP_PROLOGUE (ip);
191 }
192
193 /* New/multi-arched targets should use the correct gdbarch field
194 instead of using this global pointer. */
195 int
196 legacy_print_insn (bfd_vma vma, disassemble_info *info)
197 {
198 return (*tm_print_insn) (vma, info);
199 }
200
201 /* Helper functions for INNER_THAN */
202
203 int
204 core_addr_lessthan (CORE_ADDR lhs, CORE_ADDR rhs)
205 {
206 return (lhs < rhs);
207 }
208
209 int
210 core_addr_greaterthan (CORE_ADDR lhs, CORE_ADDR rhs)
211 {
212 return (lhs > rhs);
213 }
214
215
216 /* Helper functions for TARGET_{FLOAT,DOUBLE}_FORMAT */
217
218 const struct floatformat *
219 default_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 {
228 case BFD_ENDIAN_BIG:
229 return &floatformat_ieee_single_big;
230 case BFD_ENDIAN_LITTLE:
231 return &floatformat_ieee_single_little;
232 default:
233 internal_error (__FILE__, __LINE__,
234 "default_float_format: bad byte order");
235 }
236 }
237
238
239 const struct floatformat *
240 default_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 {
249 case BFD_ENDIAN_BIG:
250 return &floatformat_ieee_double_big;
251 case BFD_ENDIAN_LITTLE:
252 return &floatformat_ieee_double_little;
253 default:
254 internal_error (__FILE__, __LINE__,
255 "default_double_format: bad byte order");
256 }
257 }
258
259 /* Misc helper functions for targets. */
260
261 int
262 frame_num_args_unknown (struct frame_info *fi)
263 {
264 return -1;
265 }
266
267
268 int
269 generic_register_convertible_not (int num)
270 {
271 return 0;
272 }
273
274
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. */
284 CORE_ADDR
285 generic_cannot_extract_struct_value_address (char *dummy)
286 {
287 return 0;
288 }
289
290 CORE_ADDR
291 core_addr_identity (CORE_ADDR addr)
292 {
293 return addr;
294 }
295
296 int
297 no_op_reg_to_regnum (int reg)
298 {
299 return reg;
300 }
301
302 /* For use by frame_args_address and frame_locals_address. */
303 CORE_ADDR
304 default_frame_address (struct frame_info *fi)
305 {
306 return fi->frame;
307 }
308
309 /* Default prepare_to_procced(). */
310 int
311 default_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. */
318 int
319 generic_prepare_to_proceed (int select_it)
320 {
321 ptid_t wait_ptid;
322 struct target_waitstatus wait_status;
323
324 /* Get the last target status returned by target_wait(). */
325 get_last_target_status (&wait_ptid, &wait_status);
326
327 /* Make sure we were stopped either at a breakpoint, or because
328 of a Ctrl-C. */
329 if (wait_status.kind != TARGET_WAITKIND_STOPPED
330 || (wait_status.value.sig != TARGET_SIGNAL_TRAP &&
331 wait_status.value.sig != TARGET_SIGNAL_INT))
332 {
333 return 0;
334 }
335
336 if (!ptid_equal (wait_ptid, minus_one_ptid)
337 && !ptid_equal (inferior_ptid, wait_ptid))
338 {
339 /* Switched over from WAIT_PID. */
340 CORE_ADDR wait_pc = read_pc_pid (wait_ptid);
341
342 if (wait_pc != read_pc ())
343 {
344 if (select_it)
345 {
346 /* Switch back to WAIT_PID thread. */
347 inferior_ptid = wait_ptid;
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;
354 select_frame (get_current_frame ());
355 }
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 }
363 }
364 }
365 return 0;
366
367 }
368
369 void
370 init_frame_pc_noop (int fromleaf, struct frame_info *prev)
371 {
372 return;
373 }
374
375 void
376 init_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
386 void
387 default_elf_make_msymbol_special (asymbol *sym, struct minimal_symbol *msym)
388 {
389 return;
390 }
391
392 void
393 default_coff_make_msymbol_special (int val, struct minimal_symbol *msym)
394 {
395 return;
396 }
397
398 int
399 cannot_register_not (int regnum)
400 {
401 return 0;
402 }
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
407 void
408 legacy_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 }
416
417 /* Assume the world is sane, every register's virtual and real size
418 is identical. */
419
420 int
421 generic_register_size (int regnum)
422 {
423 gdb_assert (regnum >= 0 && regnum < NUM_REGS + NUM_PSEUDO_REGS);
424 return TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (regnum));
425 }
426
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
438
439 /* Assume all registers are adjacent. */
440
441 int
442 generic_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
455 \f
456 int
457 legacy_pc_in_sigtramp (CORE_ADDR pc, char *name)
458 {
459 return IN_SIGTRAMP(pc, name);
460 }
461
462 int
463 legacy_convert_register_p (int regnum)
464 {
465 return REGISTER_CONVERTIBLE (regnum);
466 }
467
468 void
469 legacy_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
475 void
476 legacy_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
482 \f
483 /* Functions to manipulate the endianness of the target. */
484
485 /* ``target_byte_order'' is only used when non- multi-arch.
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. */
492 int target_byte_order = BFD_ENDIAN_BIG;
493 int target_byte_order_auto = 1;
494
495 static const char endian_big[] = "big";
496 static const char endian_little[] = "little";
497 static const char endian_auto[] = "auto";
498 static const char *endian_enum[] =
499 {
500 endian_big,
501 endian_little,
502 endian_auto,
503 NULL,
504 };
505 static const char *set_endian_string;
506
507 /* Called by ``show endian''. */
508
509 static void
510 show_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",
514 (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? "big" : "little"));
515 else
516 printf_unfiltered ("The target is assumed to be %s endian\n",
517 (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? "big" : "little"));
518 }
519
520 static void
521 set_endian (char *ignore_args, int from_tty, struct cmd_list_element *c)
522 {
523 if (set_endian_string == endian_auto)
524 {
525 target_byte_order_auto = 1;
526 }
527 else if (set_endian_string == endian_little)
528 {
529 target_byte_order_auto = 0;
530 if (GDB_MULTI_ARCH)
531 {
532 struct gdbarch_info info;
533 gdbarch_info_init (&info);
534 info.byte_order = BFD_ENDIAN_LITTLE;
535 if (! gdbarch_update_p (info))
536 {
537 printf_unfiltered ("Little endian target not supported by GDB\n");
538 }
539 }
540 else
541 {
542 target_byte_order = BFD_ENDIAN_LITTLE;
543 }
544 }
545 else if (set_endian_string == endian_big)
546 {
547 target_byte_order_auto = 0;
548 if (GDB_MULTI_ARCH)
549 {
550 struct gdbarch_info info;
551 gdbarch_info_init (&info);
552 info.byte_order = BFD_ENDIAN_BIG;
553 if (! gdbarch_update_p (info))
554 {
555 printf_unfiltered ("Big endian target not supported by GDB\n");
556 }
557 }
558 else
559 {
560 target_byte_order = BFD_ENDIAN_BIG;
561 }
562 }
563 else
564 internal_error (__FILE__, __LINE__,
565 "set_endian: bad value");
566 show_endian (NULL, from_tty);
567 }
568
569 /* Set the endianness from a BFD. */
570
571 static void
572 set_endian_from_file (bfd *abfd)
573 {
574 int want;
575 if (GDB_MULTI_ARCH)
576 internal_error (__FILE__, __LINE__,
577 "set_endian_from_file: not for multi-arch");
578 if (bfd_big_endian (abfd))
579 want = BFD_ENDIAN_BIG;
580 else
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");
588 }
589
590
591 /* Functions to manipulate the architecture of the target */
592
593 enum set_arch { set_arch_auto, set_arch_manual };
594
595 int target_architecture_auto = 1;
596
597 const char *set_architecture_string;
598
599 /* Old way of changing the current architecture. */
600
601 extern const struct bfd_arch_info bfd_default_arch_struct;
602 const struct bfd_arch_info *target_architecture = &bfd_default_arch_struct;
603 int (*target_architecture_hook) (const struct bfd_arch_info *ap);
604
605 static int
606 arch_ok (const struct bfd_arch_info *arch)
607 {
608 if (GDB_MULTI_ARCH)
609 internal_error (__FILE__, __LINE__,
610 "arch_ok: not multi-arched");
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
618 static void
619 set_arch (const struct bfd_arch_info *arch,
620 enum set_arch type)
621 {
622 if (GDB_MULTI_ARCH)
623 internal_error (__FILE__, __LINE__,
624 "set_arch: not multi-arched");
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)
647 gdbarch_dump (current_gdbarch, gdb_stdlog);
648 }
649
650 /* Set the architecture from arch/machine (deprecated) */
651
652 void
653 set_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)
658 internal_error (__FILE__, __LINE__,
659 "set_architecture_from_arch_mach: not multi-arched");
660 if (wanted != NULL)
661 set_arch (wanted, set_arch_manual);
662 else
663 internal_error (__FILE__, __LINE__,
664 "gdbarch: hardwired architecture/machine not recognized");
665 }
666
667 /* Set the architecture from a BFD (deprecated) */
668
669 static void
670 set_architecture_from_file (bfd *abfd)
671 {
672 const struct bfd_arch_info *wanted = bfd_get_arch_info (abfd);
673 if (GDB_MULTI_ARCH)
674 internal_error (__FILE__, __LINE__,
675 "set_architecture_from_file: not multi-arched");
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
692 static void
693 show_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
707 static void
708 set_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;
717 gdbarch_info_init (&info);
718 info.bfd_arch_info = bfd_scan_arch (set_architecture_string);
719 if (info.bfd_arch_info == NULL)
720 internal_error (__FILE__, __LINE__,
721 "set_architecture: bfd_scan_arch failed");
722 if (gdbarch_update_p (info))
723 target_architecture_auto = 0;
724 else
725 printf_unfiltered ("Architecture `%s' not recognized.\n",
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)
733 internal_error (__FILE__, __LINE__,
734 "set_architecture: bfd_scan_arch failed");
735 set_arch (arch, set_arch_manual);
736 }
737 show_architecture (NULL, from_tty);
738 }
739
740 /* Set the dynamic target-system-dependent parameters (architecture,
741 byte-order) using information found in the BFD */
742
743 void
744 set_gdbarch_from_file (bfd *abfd)
745 {
746 if (GDB_MULTI_ARCH)
747 {
748 struct gdbarch_info info;
749 gdbarch_info_init (&info);
750 info.abfd = abfd;
751 if (! gdbarch_update_p (info))
752 error ("Architecture of file not recognized.\n");
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
765 #ifdef DEFAULT_BFD_ARCH
766 extern const bfd_arch_info_type DEFAULT_BFD_ARCH;
767 static const bfd_arch_info_type *default_bfd_arch = &DEFAULT_BFD_ARCH;
768 #else
769 static const bfd_arch_info_type *default_bfd_arch;
770 #endif
771
772 #ifdef DEFAULT_BFD_VEC
773 extern const bfd_target DEFAULT_BFD_VEC;
774 static const bfd_target *default_bfd_vec = &DEFAULT_BFD_VEC;
775 #else
776 static const bfd_target *default_bfd_vec;
777 #endif
778
779 void
780 initialize_current_architecture (void)
781 {
782 const char **arches = gdbarch_printable_names ();
783
784 /* determine a default architecture and byte order. */
785 struct gdbarch_info info;
786 gdbarch_info_init (&info);
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)
793 {
794 /* Choose the architecture by taking the first one
795 alphabetically. */
796 const char *chosen = arches[0];
797 const char **arch;
798 for (arch = arches; *arch != NULL; arch++)
799 {
800 if (strcmp (*arch, chosen) < 0)
801 chosen = *arch;
802 }
803 if (chosen == NULL)
804 internal_error (__FILE__, __LINE__,
805 "initialize_current_architecture: No arch");
806 info.bfd_arch_info = bfd_scan_arch (chosen);
807 if (info.bfd_arch_info == NULL)
808 internal_error (__FILE__, __LINE__,
809 "initialize_current_architecture: Arch not found");
810 }
811
812 /* Take several guesses at a byte order. */
813 if (info.byte_order == BFD_ENDIAN_UNKNOWN
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:
820 info.byte_order = BFD_ENDIAN_BIG;
821 break;
822 case BFD_ENDIAN_LITTLE:
823 info.byte_order = BFD_ENDIAN_LITTLE;
824 break;
825 default:
826 break;
827 }
828 }
829 if (info.byte_order == BFD_ENDIAN_UNKNOWN)
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)
837 info.byte_order = BFD_ENDIAN_LITTLE;
838 }
839 if (info.byte_order == BFD_ENDIAN_UNKNOWN)
840 {
841 /* Wire it to big-endian!!! */
842 info.byte_order = BFD_ENDIAN_BIG;
843 }
844
845 if (GDB_MULTI_ARCH)
846 {
847 if (! gdbarch_update_p (info))
848 {
849 internal_error (__FILE__, __LINE__,
850 "initialize_current_architecture: Selection of initial architecture failed");
851 }
852 }
853 else
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 }
861
862 /* Create the ``set architecture'' command appending ``auto'' to the
863 list of architectures. */
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,
876 arches, &set_architecture_string,
877 "Set architecture of target.",
878 &setlist);
879 set_cmd_sfunc (c, set_architecture);
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);
885 }
886 }
887
888
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
895 void
896 gdbarch_info_init (struct gdbarch_info *info)
897 {
898 memset (info, 0, sizeof (struct gdbarch_info));
899 info->byte_order = BFD_ENDIAN_UNKNOWN;
900 }
901
902 /* */
903
904 extern initialize_file_ftype _initialize_gdbarch_utils;
905
906 void
907 _initialize_gdbarch_utils (void)
908 {
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);
914 set_cmd_sfunc (c, set_endian);
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);
919 }
This page took 0.050294 seconds and 4 git commands to generate.