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