Correct/clarify credit for C++ work.
[deliverable/binutils-gdb.git] / gdb / arch-utils.c
1 /* Dynamic architecture support for GDB, the GNU debugger.
2 Copyright 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include "defs.h"
22
23 #if GDB_MULTI_ARCH
24 #include "gdbcmd.h"
25 #include "inferior.h" /* enum CALL_DUMMY_LOCATION et.al. */
26 #else
27 /* Just include everything in sight so that the every old definition
28 of macro is visible. */
29 #include "gdb_string.h"
30 #include "symtab.h"
31 #include "frame.h"
32 #include "inferior.h"
33 #include "breakpoint.h"
34 #include "gdb_wait.h"
35 #include "gdbcore.h"
36 #include "gdbcmd.h"
37 #include "target.h"
38 #include "gdbthread.h"
39 #include "annotate.h"
40 #include "symfile.h" /* for overlay functions */
41 #endif
42
43 #include "version.h"
44
45 #include "floatformat.h"
46
47 /* Convenience macro for allocting typesafe memory. */
48
49 #ifndef XMALLOC
50 #define XMALLOC(TYPE) (TYPE*) xmalloc (sizeof (TYPE))
51 #endif
52
53
54 /* Use the program counter to determine the contents and size
55 of a breakpoint instruction. If no target-dependent macro
56 BREAKPOINT_FROM_PC has been defined to implement this function,
57 assume that the breakpoint doesn't depend on the PC, and
58 use the values of the BIG_BREAKPOINT and LITTLE_BREAKPOINT macros.
59 Return a pointer to a string of bytes that encode a breakpoint
60 instruction, stores the length of the string to *lenptr,
61 and optionally adjust the pc to point to the correct memory location
62 for inserting the breakpoint. */
63
64 unsigned char *
65 legacy_breakpoint_from_pc (CORE_ADDR * pcptr, int *lenptr)
66 {
67 /* {BIG_,LITTLE_}BREAKPOINT is the sequence of bytes we insert for a
68 breakpoint. On some machines, breakpoints are handled by the
69 target environment and we don't have to worry about them here. */
70 #ifdef BIG_BREAKPOINT
71 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
72 {
73 static unsigned char big_break_insn[] = BIG_BREAKPOINT;
74 *lenptr = sizeof (big_break_insn);
75 return big_break_insn;
76 }
77 #endif
78 #ifdef LITTLE_BREAKPOINT
79 if (TARGET_BYTE_ORDER != BIG_ENDIAN)
80 {
81 static unsigned char little_break_insn[] = LITTLE_BREAKPOINT;
82 *lenptr = sizeof (little_break_insn);
83 return little_break_insn;
84 }
85 #endif
86 #ifdef BREAKPOINT
87 {
88 static unsigned char break_insn[] = BREAKPOINT;
89 *lenptr = sizeof (break_insn);
90 return break_insn;
91 }
92 #endif
93 *lenptr = 0;
94 return NULL;
95 }
96
97 int
98 generic_frameless_function_invocation_not (struct frame_info *fi)
99 {
100 return 0;
101 }
102
103 int
104 generic_return_value_on_stack_not (struct type *type)
105 {
106 return 0;
107 }
108
109 char *
110 legacy_register_name (int i)
111 {
112 #ifdef REGISTER_NAMES
113 static char *names[] = REGISTER_NAMES;
114 if (i < 0 || i >= (sizeof (names) / sizeof (*names)))
115 return NULL;
116 else
117 return names[i];
118 #else
119 internal_error (__FILE__, __LINE__,
120 "legacy_register_name: called.");
121 return NULL;
122 #endif
123 }
124
125 #if defined (CALL_DUMMY)
126 LONGEST legacy_call_dummy_words[] = CALL_DUMMY;
127 #else
128 LONGEST legacy_call_dummy_words[1];
129 #endif
130 int legacy_sizeof_call_dummy_words = sizeof (legacy_call_dummy_words);
131
132 void
133 generic_remote_translate_xfer_address (CORE_ADDR gdb_addr, int gdb_len,
134 CORE_ADDR * rem_addr, int *rem_len)
135 {
136 *rem_addr = gdb_addr;
137 *rem_len = gdb_len;
138 }
139
140 int
141 generic_prologue_frameless_p (CORE_ADDR ip)
142 {
143 #ifdef SKIP_PROLOGUE_FRAMELESS_P
144 return ip == SKIP_PROLOGUE_FRAMELESS_P (ip);
145 #else
146 return ip == SKIP_PROLOGUE (ip);
147 #endif
148 }
149
150
151 /* Helper functions for INNER_THAN */
152
153 int
154 core_addr_lessthan (CORE_ADDR lhs, CORE_ADDR rhs)
155 {
156 return (lhs < rhs);
157 }
158
159 int
160 core_addr_greaterthan (CORE_ADDR lhs, CORE_ADDR rhs)
161 {
162 return (lhs > rhs);
163 }
164
165
166 /* Helper functions for TARGET_{FLOAT,DOUBLE}_FORMAT */
167
168 const struct floatformat *
169 default_float_format (struct gdbarch *gdbarch)
170 {
171 #if GDB_MULTI_ARCH
172 int byte_order = gdbarch_byte_order (gdbarch);
173 #else
174 int byte_order = TARGET_BYTE_ORDER;
175 #endif
176 switch (byte_order)
177 {
178 case BIG_ENDIAN:
179 return &floatformat_ieee_single_big;
180 case LITTLE_ENDIAN:
181 return &floatformat_ieee_single_little;
182 default:
183 internal_error (__FILE__, __LINE__,
184 "default_float_format: bad byte order");
185 }
186 }
187
188
189 const struct floatformat *
190 default_double_format (struct gdbarch *gdbarch)
191 {
192 #if GDB_MULTI_ARCH
193 int byte_order = gdbarch_byte_order (gdbarch);
194 #else
195 int byte_order = TARGET_BYTE_ORDER;
196 #endif
197 switch (byte_order)
198 {
199 case BIG_ENDIAN:
200 return &floatformat_ieee_double_big;
201 case LITTLE_ENDIAN:
202 return &floatformat_ieee_double_little;
203 default:
204 internal_error (__FILE__, __LINE__,
205 "default_double_format: bad byte order");
206 }
207 }
208
209 /* Misc helper functions for targets. */
210
211 int
212 frame_num_args_unknown (struct frame_info *fi)
213 {
214 return -1;
215 }
216
217
218 int
219 generic_register_convertible_not (int num)
220 {
221 return 0;
222 }
223
224
225 int
226 default_register_sim_regno (int num)
227 {
228 return num;
229 }
230
231
232 CORE_ADDR
233 default_convert_from_func_ptr_addr (CORE_ADDR addr)
234 {
235 return addr;
236 }
237
238 int
239 no_op_reg_to_regnum (int reg)
240 {
241 return reg;
242 }
243
244 /* For use by frame_args_address and frame_locals_address. */
245 CORE_ADDR
246 default_frame_address (struct frame_info *fi)
247 {
248 return fi->frame;
249 }
250
251 /* Functions to manipulate the endianness of the target. */
252
253 #ifdef TARGET_BYTE_ORDER_SELECTABLE
254 /* compat - Catch old targets that expect a selectable byte-order to
255 default to BIG_ENDIAN */
256 #ifndef TARGET_BYTE_ORDER_DEFAULT
257 #define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN
258 #endif
259 #endif
260 #if !TARGET_BYTE_ORDER_SELECTABLE_P
261 #ifndef TARGET_BYTE_ORDER_DEFAULT
262 /* compat - Catch old non byte-order selectable targets that do not
263 define TARGET_BYTE_ORDER_DEFAULT and instead expect
264 TARGET_BYTE_ORDER to be used as the default. For targets that
265 defined neither TARGET_BYTE_ORDER nor TARGET_BYTE_ORDER_DEFAULT the
266 below will get a strange compiler warning. */
267 #define TARGET_BYTE_ORDER_DEFAULT TARGET_BYTE_ORDER
268 #endif
269 #endif
270 #ifndef TARGET_BYTE_ORDER_DEFAULT
271 #define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN /* arbitrary */
272 #endif
273 /* ``target_byte_order'' is only used when non- multi-arch.
274 Multi-arch targets obtain the current byte order using
275 TARGET_BYTE_ORDER which is controlled by gdbarch.*. */
276 int target_byte_order = TARGET_BYTE_ORDER_DEFAULT;
277 int target_byte_order_auto = 1;
278
279 static const char endian_big[] = "big";
280 static const char endian_little[] = "little";
281 static const char endian_auto[] = "auto";
282 static const char *endian_enum[] =
283 {
284 endian_big,
285 endian_little,
286 endian_auto,
287 NULL,
288 };
289 static const char *set_endian_string;
290
291 /* Called by ``show endian''. */
292
293 static void
294 show_endian (char *args, int from_tty)
295 {
296 if (TARGET_BYTE_ORDER_AUTO)
297 printf_unfiltered ("The target endianness is set automatically (currently %s endian)\n",
298 (TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little"));
299 else
300 printf_unfiltered ("The target is assumed to be %s endian\n",
301 (TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little"));
302 }
303
304 static void
305 set_endian (char *ignore_args, int from_tty, struct cmd_list_element *c)
306 {
307 if (!TARGET_BYTE_ORDER_SELECTABLE_P)
308 {
309 printf_unfiltered ("Byte order is not selectable.");
310 }
311 else if (set_endian_string == endian_auto)
312 {
313 target_byte_order_auto = 1;
314 }
315 else if (set_endian_string == endian_little)
316 {
317 target_byte_order_auto = 0;
318 if (GDB_MULTI_ARCH)
319 {
320 struct gdbarch_info info;
321 memset (&info, 0, sizeof info);
322 info.byte_order = LITTLE_ENDIAN;
323 if (! gdbarch_update_p (info))
324 {
325 printf_unfiltered ("Little endian target not supported by GDB\n");
326 }
327 }
328 else
329 {
330 target_byte_order = LITTLE_ENDIAN;
331 }
332 }
333 else if (set_endian_string == endian_big)
334 {
335 target_byte_order_auto = 0;
336 if (GDB_MULTI_ARCH)
337 {
338 struct gdbarch_info info;
339 memset (&info, 0, sizeof info);
340 info.byte_order = BIG_ENDIAN;
341 if (! gdbarch_update_p (info))
342 {
343 printf_unfiltered ("Big endian target not supported by GDB\n");
344 }
345 }
346 else
347 {
348 target_byte_order = BIG_ENDIAN;
349 }
350 }
351 else
352 internal_error (__FILE__, __LINE__,
353 "set_endian: bad value");
354 show_endian (NULL, from_tty);
355 }
356
357 /* Set the endianness from a BFD. */
358
359 static void
360 set_endian_from_file (bfd *abfd)
361 {
362 if (GDB_MULTI_ARCH)
363 internal_error (__FILE__, __LINE__,
364 "set_endian_from_file: not for multi-arch");
365 if (TARGET_BYTE_ORDER_SELECTABLE_P)
366 {
367 int want;
368
369 if (bfd_big_endian (abfd))
370 want = BIG_ENDIAN;
371 else
372 want = LITTLE_ENDIAN;
373 if (TARGET_BYTE_ORDER_AUTO)
374 target_byte_order = want;
375 else if (TARGET_BYTE_ORDER != want)
376 warning ("%s endian file does not match %s endian target.",
377 want == BIG_ENDIAN ? "big" : "little",
378 TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little");
379 }
380 else
381 {
382 if (bfd_big_endian (abfd)
383 ? TARGET_BYTE_ORDER != BIG_ENDIAN
384 : TARGET_BYTE_ORDER == BIG_ENDIAN)
385 warning ("%s endian file does not match %s endian target.",
386 bfd_big_endian (abfd) ? "big" : "little",
387 TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little");
388 }
389 }
390
391
392 /* Functions to manipulate the architecture of the target */
393
394 enum set_arch { set_arch_auto, set_arch_manual };
395
396 int target_architecture_auto = 1;
397
398 const char *set_architecture_string;
399
400 /* Old way of changing the current architecture. */
401
402 extern const struct bfd_arch_info bfd_default_arch_struct;
403 const struct bfd_arch_info *target_architecture = &bfd_default_arch_struct;
404 int (*target_architecture_hook) (const struct bfd_arch_info *ap);
405
406 static int
407 arch_ok (const struct bfd_arch_info *arch)
408 {
409 if (GDB_MULTI_ARCH)
410 internal_error (__FILE__, __LINE__,
411 "arch_ok: not multi-arched");
412 /* Should be performing the more basic check that the binary is
413 compatible with GDB. */
414 /* Check with the target that the architecture is valid. */
415 return (target_architecture_hook == NULL
416 || target_architecture_hook (arch));
417 }
418
419 static void
420 set_arch (const struct bfd_arch_info *arch,
421 enum set_arch type)
422 {
423 if (GDB_MULTI_ARCH)
424 internal_error (__FILE__, __LINE__,
425 "set_arch: not multi-arched");
426 switch (type)
427 {
428 case set_arch_auto:
429 if (!arch_ok (arch))
430 warning ("Target may not support %s architecture",
431 arch->printable_name);
432 target_architecture = arch;
433 break;
434 case set_arch_manual:
435 if (!arch_ok (arch))
436 {
437 printf_unfiltered ("Target does not support `%s' architecture.\n",
438 arch->printable_name);
439 }
440 else
441 {
442 target_architecture_auto = 0;
443 target_architecture = arch;
444 }
445 break;
446 }
447 if (gdbarch_debug)
448 gdbarch_dump (current_gdbarch, gdb_stdlog);
449 }
450
451 /* Set the architecture from arch/machine (deprecated) */
452
453 void
454 set_architecture_from_arch_mach (enum bfd_architecture arch,
455 unsigned long mach)
456 {
457 const struct bfd_arch_info *wanted = bfd_lookup_arch (arch, mach);
458 if (GDB_MULTI_ARCH)
459 internal_error (__FILE__, __LINE__,
460 "set_architecture_from_arch_mach: not multi-arched");
461 if (wanted != NULL)
462 set_arch (wanted, set_arch_manual);
463 else
464 internal_error (__FILE__, __LINE__,
465 "gdbarch: hardwired architecture/machine not recognized");
466 }
467
468 /* Set the architecture from a BFD (deprecated) */
469
470 static void
471 set_architecture_from_file (bfd *abfd)
472 {
473 const struct bfd_arch_info *wanted = bfd_get_arch_info (abfd);
474 if (GDB_MULTI_ARCH)
475 internal_error (__FILE__, __LINE__,
476 "set_architecture_from_file: not multi-arched");
477 if (target_architecture_auto)
478 {
479 set_arch (wanted, set_arch_auto);
480 }
481 else if (wanted != target_architecture)
482 {
483 warning ("%s architecture file may be incompatible with %s target.",
484 wanted->printable_name,
485 target_architecture->printable_name);
486 }
487 }
488
489
490 /* Called if the user enters ``show architecture'' without an
491 argument. */
492
493 static void
494 show_architecture (char *args, int from_tty)
495 {
496 const char *arch;
497 arch = TARGET_ARCHITECTURE->printable_name;
498 if (target_architecture_auto)
499 printf_filtered ("The target architecture is set automatically (currently %s)\n", arch);
500 else
501 printf_filtered ("The target architecture is assumed to be %s\n", arch);
502 }
503
504
505 /* Called if the user enters ``set architecture'' with or without an
506 argument. */
507
508 static void
509 set_architecture (char *ignore_args, int from_tty, struct cmd_list_element *c)
510 {
511 if (strcmp (set_architecture_string, "auto") == 0)
512 {
513 target_architecture_auto = 1;
514 }
515 else if (GDB_MULTI_ARCH)
516 {
517 struct gdbarch_info info;
518 memset (&info, 0, sizeof info);
519 info.bfd_arch_info = bfd_scan_arch (set_architecture_string);
520 if (info.bfd_arch_info == NULL)
521 internal_error (__FILE__, __LINE__,
522 "set_architecture: bfd_scan_arch failed");
523 if (gdbarch_update_p (info))
524 target_architecture_auto = 0;
525 else
526 printf_unfiltered ("Architecture `%s' not recognized.\n",
527 set_architecture_string);
528 }
529 else
530 {
531 const struct bfd_arch_info *arch
532 = bfd_scan_arch (set_architecture_string);
533 if (arch == NULL)
534 internal_error (__FILE__, __LINE__,
535 "set_architecture: bfd_scan_arch failed");
536 set_arch (arch, set_arch_manual);
537 }
538 show_architecture (NULL, from_tty);
539 }
540
541 /* Called if the user enters ``info architecture'' without an argument. */
542
543 static void
544 info_architecture (char *args, int from_tty)
545 {
546 printf_filtered ("Available architectures are:\n");
547 if (GDB_MULTI_ARCH)
548 {
549 const char **arches = gdbarch_printable_names ();
550 const char **arch;
551 for (arch = arches; *arch != NULL; arch++)
552 {
553 printf_filtered (" %s", *arch);
554 }
555 xfree (arches);
556 }
557 else
558 {
559 enum bfd_architecture a;
560 for (a = bfd_arch_obscure + 1; a < bfd_arch_last; a++)
561 {
562 const struct bfd_arch_info *ap;
563 for (ap = bfd_lookup_arch (a, 0);
564 ap != NULL;
565 ap = ap->next)
566 {
567 printf_filtered (" %s", ap->printable_name);
568 ap = ap->next;
569 }
570 }
571 }
572 printf_filtered ("\n");
573 }
574
575 /* Set the dynamic target-system-dependent parameters (architecture,
576 byte-order) using information found in the BFD */
577
578 void
579 set_gdbarch_from_file (bfd *abfd)
580 {
581 if (GDB_MULTI_ARCH)
582 {
583 struct gdbarch_info info;
584 memset (&info, 0, sizeof info);
585 info.abfd = abfd;
586 if (! gdbarch_update_p (info))
587 error ("Architecture of file not recognized.\n");
588 }
589 else
590 {
591 set_architecture_from_file (abfd);
592 set_endian_from_file (abfd);
593 }
594 }
595
596 /* Initialize the current architecture. Update the ``set
597 architecture'' command so that it specifies a list of valid
598 architectures. */
599
600 #ifdef DEFAULT_BFD_ARCH
601 extern const bfd_arch_info_type DEFAULT_BFD_ARCH;
602 static const bfd_arch_info_type *default_bfd_arch = &DEFAULT_BFD_ARCH;
603 #else
604 static const bfd_arch_info_type *default_bfd_arch;
605 #endif
606
607 #ifdef DEFAULT_BFD_VEC
608 extern const bfd_target DEFAULT_BFD_VEC;
609 static const bfd_target *default_bfd_vec = &DEFAULT_BFD_VEC;
610 #else
611 static const bfd_target *default_bfd_vec;
612 #endif
613
614 void
615 initialize_current_architecture (void)
616 {
617 const char **arches = gdbarch_printable_names ();
618
619 /* determine a default architecture and byte order. */
620 struct gdbarch_info info;
621 memset (&info, 0, sizeof (info));
622
623 /* Find a default architecture. */
624 if (info.bfd_arch_info == NULL
625 && default_bfd_arch != NULL)
626 info.bfd_arch_info = default_bfd_arch;
627 if (info.bfd_arch_info == NULL)
628 {
629 /* Choose the architecture by taking the first one
630 alphabetically. */
631 const char *chosen = arches[0];
632 const char **arch;
633 for (arch = arches; *arch != NULL; arch++)
634 {
635 if (strcmp (*arch, chosen) < 0)
636 chosen = *arch;
637 }
638 if (chosen == NULL)
639 internal_error (__FILE__, __LINE__,
640 "initialize_current_architecture: No arch");
641 info.bfd_arch_info = bfd_scan_arch (chosen);
642 if (info.bfd_arch_info == NULL)
643 internal_error (__FILE__, __LINE__,
644 "initialize_current_architecture: Arch not found");
645 }
646
647 /* take several guesses at a byte order. */
648 /* NB: can't use TARGET_BYTE_ORDER_DEFAULT as its definition is
649 forced above. */
650 if (info.byte_order == 0
651 && default_bfd_vec != NULL)
652 {
653 /* Extract BFD's default vector's byte order. */
654 switch (default_bfd_vec->byteorder)
655 {
656 case BFD_ENDIAN_BIG:
657 info.byte_order = BIG_ENDIAN;
658 break;
659 case BFD_ENDIAN_LITTLE:
660 info.byte_order = LITTLE_ENDIAN;
661 break;
662 default:
663 break;
664 }
665 }
666 if (info.byte_order == 0)
667 {
668 /* look for ``*el-*'' in the target name. */
669 const char *chp;
670 chp = strchr (target_name, '-');
671 if (chp != NULL
672 && chp - 2 >= target_name
673 && strncmp (chp - 2, "el", 2) == 0)
674 info.byte_order = LITTLE_ENDIAN;
675 }
676 if (info.byte_order == 0)
677 {
678 /* Wire it to big-endian!!! */
679 info.byte_order = BIG_ENDIAN;
680 }
681
682 if (GDB_MULTI_ARCH)
683 {
684 if (! gdbarch_update_p (info))
685 {
686 internal_error (__FILE__, __LINE__,
687 "initialize_current_architecture: Selection of initial architecture failed");
688 }
689 }
690
691 /* Create the ``set architecture'' command appending ``auto'' to the
692 list of architectures. */
693 {
694 struct cmd_list_element *c;
695 /* Append ``auto''. */
696 int nr;
697 for (nr = 0; arches[nr] != NULL; nr++);
698 arches = xrealloc (arches, sizeof (char*) * (nr + 2));
699 arches[nr + 0] = "auto";
700 arches[nr + 1] = NULL;
701 /* FIXME: add_set_enum_cmd() uses an array of ``char *'' instead
702 of ``const char *''. We just happen to know that the casts are
703 safe. */
704 c = add_set_enum_cmd ("architecture", class_support,
705 arches, &set_architecture_string,
706 "Set architecture of target.",
707 &setlist);
708 c->function.sfunc = set_architecture;
709 add_alias_cmd ("processor", "architecture", class_support, 1, &setlist);
710 /* Don't use set_from_show - need to print both auto/manual and
711 current setting. */
712 add_cmd ("architecture", class_support, show_architecture,
713 "Show the current target architecture", &showlist);
714 c = add_cmd ("architecture", class_support, info_architecture,
715 "List supported target architectures", &infolist);
716 deprecate_cmd (c, "set architecture");
717 }
718 }
719
720
721 /* */
722
723 extern initialize_file_ftype _initialize_gdbarch_utils;
724
725 void
726 _initialize_gdbarch_utils (void)
727 {
728 struct cmd_list_element *c;
729 c = add_set_enum_cmd ("endian", class_support,
730 endian_enum, &set_endian_string,
731 "Set endianness of target.",
732 &setlist);
733 c->function.sfunc = set_endian;
734 /* Don't use set_from_show - need to print both auto/manual and
735 current setting. */
736 add_cmd ("endian", class_support, show_endian,
737 "Show the current byte-order", &showlist);
738 }
This page took 0.044446 seconds and 4 git commands to generate.