PR gdb/2343
[external/binutils.git] / gdb / arch-utils.c
1 /* Dynamic architecture support for GDB, the GNU debugger.
2
3    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4    2008 Free Software Foundation, 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 3 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, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22
23 #include "arch-utils.h"
24 #include "buildsym.h"
25 #include "gdbcmd.h"
26 #include "inferior.h"           /* enum CALL_DUMMY_LOCATION et.al. */
27 #include "gdb_string.h"
28 #include "regcache.h"
29 #include "gdb_assert.h"
30 #include "sim-regno.h"
31 #include "gdbcore.h"
32 #include "osabi.h"
33 #include "target-descriptions.h"
34
35 #include "version.h"
36
37 #include "floatformat.h"
38
39
40 int
41 legacy_register_sim_regno (struct gdbarch *gdbarch, int regnum)
42 {
43   /* Only makes sense to supply raw registers.  */
44   gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch));
45   /* NOTE: cagney/2002-05-13: The old code did it this way and it is
46      suspected that some GDB/SIM combinations may rely on this
47      behavour.  The default should be one2one_register_sim_regno
48      (below).  */
49   if (gdbarch_register_name (gdbarch, regnum) != NULL
50       && gdbarch_register_name (gdbarch, regnum)[0] != '\0')
51     return regnum;
52   else
53     return LEGACY_SIM_REGNO_IGNORE;
54 }
55
56 CORE_ADDR
57 generic_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
58 {
59   return 0;
60 }
61
62 CORE_ADDR
63 generic_skip_solib_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
64 {
65   return 0;
66 }
67
68 int
69 generic_in_solib_return_trampoline (CORE_ADDR pc, char *name)
70 {
71   return 0;
72 }
73
74 int
75 generic_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
76 {
77   return 0;
78 }
79
80 /* Helper functions for gdbarch_inner_than */
81
82 int
83 core_addr_lessthan (CORE_ADDR lhs, CORE_ADDR rhs)
84 {
85   return (lhs < rhs);
86 }
87
88 int
89 core_addr_greaterthan (CORE_ADDR lhs, CORE_ADDR rhs)
90 {
91   return (lhs > rhs);
92 }
93
94 /* Misc helper functions for targets. */
95
96 CORE_ADDR
97 core_addr_identity (CORE_ADDR addr)
98 {
99   return addr;
100 }
101
102 CORE_ADDR
103 convert_from_func_ptr_addr_identity (struct gdbarch *gdbarch, CORE_ADDR addr,
104                                      struct target_ops *targ)
105 {
106   return addr;
107 }
108
109 int
110 no_op_reg_to_regnum (struct gdbarch *gdbarch, int reg)
111 {
112   return reg;
113 }
114
115 void
116 default_elf_make_msymbol_special (asymbol *sym, struct minimal_symbol *msym)
117 {
118   return;
119 }
120
121 void
122 default_coff_make_msymbol_special (int val, struct minimal_symbol *msym)
123 {
124   return;
125 }
126
127 int
128 cannot_register_not (struct gdbarch *gdbarch, int regnum)
129 {
130   return 0;
131 }
132
133 /* Legacy version of target_virtual_frame_pointer().  Assumes that
134    there is an gdbarch_deprecated_fp_regnum and that it is the same, cooked or
135    raw.  */
136
137 void
138 legacy_virtual_frame_pointer (struct gdbarch *gdbarch, 
139                               CORE_ADDR pc,
140                               int *frame_regnum,
141                               LONGEST *frame_offset)
142 {
143   /* FIXME: cagney/2002-09-13: This code is used when identifying the
144      frame pointer of the current PC.  It is assuming that a single
145      register and an offset can determine this.  I think it should
146      instead generate a byte code expression as that would work better
147      with things like Dwarf2's CFI.  */
148   if (gdbarch_deprecated_fp_regnum (gdbarch) >= 0
149       && gdbarch_deprecated_fp_regnum (gdbarch)
150            < gdbarch_num_regs (gdbarch))
151     *frame_regnum = gdbarch_deprecated_fp_regnum (gdbarch);
152   else if (gdbarch_sp_regnum (gdbarch) >= 0
153            && gdbarch_sp_regnum (gdbarch)
154                 < gdbarch_num_regs (gdbarch))
155     *frame_regnum = gdbarch_sp_regnum (gdbarch);
156   else
157     /* Should this be an internal error?  I guess so, it is reflecting
158        an architectural limitation in the current design.  */
159     internal_error (__FILE__, __LINE__, _("No virtual frame pointer available"));
160   *frame_offset = 0;
161 }
162
163 \f
164 int
165 generic_convert_register_p (struct gdbarch *gdbarch, int regnum,
166                             struct type *type)
167 {
168   return 0;
169 }
170
171 int
172 default_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
173 {
174   return 0;
175 }
176
177 int
178 generic_instruction_nullified (struct gdbarch *gdbarch,
179                                struct regcache *regcache)
180 {
181   return 0;
182 }
183
184 int
185 default_remote_register_number (struct gdbarch *gdbarch,
186                                 int regno)
187 {
188   return regno;
189 }
190
191 \f
192 /* Functions to manipulate the endianness of the target.  */
193
194 static int target_byte_order_user = BFD_ENDIAN_UNKNOWN;
195
196 static const char endian_big[] = "big";
197 static const char endian_little[] = "little";
198 static const char endian_auto[] = "auto";
199 static const char *endian_enum[] =
200 {
201   endian_big,
202   endian_little,
203   endian_auto,
204   NULL,
205 };
206 static const char *set_endian_string;
207
208 enum bfd_endian
209 selected_byte_order (void)
210 {
211   if (target_byte_order_user != BFD_ENDIAN_UNKNOWN)
212     return gdbarch_byte_order (current_gdbarch);
213   else
214     return BFD_ENDIAN_UNKNOWN;
215 }
216
217 /* Called by ``show endian''.  */
218
219 static void
220 show_endian (struct ui_file *file, int from_tty, struct cmd_list_element *c,
221              const char *value)
222 {
223   if (target_byte_order_user == BFD_ENDIAN_UNKNOWN)
224     if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
225       fprintf_unfiltered (file, _("The target endianness is set automatically "
226                                   "(currently big endian)\n"));
227     else
228       fprintf_unfiltered (file, _("The target endianness is set automatically "
229                            "(currently little endian)\n"));
230   else
231     if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
232       fprintf_unfiltered (file,
233                           _("The target is assumed to be big endian\n"));
234     else
235       fprintf_unfiltered (file,
236                           _("The target is assumed to be little endian\n"));
237 }
238
239 static void
240 set_endian (char *ignore_args, int from_tty, struct cmd_list_element *c)
241 {
242   struct gdbarch_info info;
243
244   gdbarch_info_init (&info);
245
246   if (set_endian_string == endian_auto)
247     {
248       target_byte_order_user = BFD_ENDIAN_UNKNOWN;
249       if (! gdbarch_update_p (info))
250         internal_error (__FILE__, __LINE__,
251                         _("set_endian: architecture update failed"));
252     }
253   else if (set_endian_string == endian_little)
254     {
255       info.byte_order = BFD_ENDIAN_LITTLE;
256       if (! gdbarch_update_p (info))
257         printf_unfiltered (_("Little endian target not supported by GDB\n"));
258       else
259         target_byte_order_user = BFD_ENDIAN_LITTLE;
260     }
261   else if (set_endian_string == endian_big)
262     {
263       info.byte_order = BFD_ENDIAN_BIG;
264       if (! gdbarch_update_p (info))
265         printf_unfiltered (_("Big endian target not supported by GDB\n"));
266       else
267         target_byte_order_user = BFD_ENDIAN_BIG;
268     }
269   else
270     internal_error (__FILE__, __LINE__,
271                     _("set_endian: bad value"));
272
273   show_endian (gdb_stdout, from_tty, NULL, NULL);
274 }
275
276 /* Given SELECTED, a currently selected BFD architecture, and
277    FROM_TARGET, a BFD architecture reported by the target description,
278    return what architecture to use.  Either may be NULL; if both are
279    specified, we use the more specific.  If the two are obviously
280    incompatible, warn the user.  */
281
282 static const struct bfd_arch_info *
283 choose_architecture_for_target (const struct bfd_arch_info *selected,
284                                 const struct bfd_arch_info *from_target)
285 {
286   const struct bfd_arch_info *compat1, *compat2;
287
288   if (selected == NULL)
289     return from_target;
290
291   if (from_target == NULL)
292     return selected;
293
294   /* struct bfd_arch_info objects are singletons: that is, there's
295      supposed to be exactly one instance for a given machine.  So you
296      can tell whether two are equivalent by comparing pointers.  */
297   if (from_target == selected)
298     return selected;
299
300   /* BFD's 'A->compatible (A, B)' functions return zero if A and B are
301      incompatible.  But if they are compatible, it returns the 'more
302      featureful' of the two arches.  That is, if A can run code
303      written for B, but B can't run code written for A, then it'll
304      return A.
305
306      Some targets (e.g. MIPS as of 2006-12-04) don't fully
307      implement this, instead always returning NULL or the first
308      argument.  We detect that case by checking both directions.  */
309
310   compat1 = selected->compatible (selected, from_target);
311   compat2 = from_target->compatible (from_target, selected);
312
313   if (compat1 == NULL && compat2 == NULL)
314     {
315       warning (_("Selected architecture %s is not compatible "
316                  "with reported target architecture %s"),
317                selected->printable_name, from_target->printable_name);
318       return selected;
319     }
320
321   if (compat1 == NULL)
322     return compat2;
323   if (compat2 == NULL)
324     return compat1;
325   if (compat1 == compat2)
326     return compat1;
327
328   /* If the two didn't match, but one of them was a default architecture,
329      assume the more specific one is correct.  This handles the case
330      where an executable or target description just says "mips", but
331      the other knows which MIPS variant.  */
332   if (compat1->the_default)
333     return compat2;
334   if (compat2->the_default)
335     return compat1;
336
337   /* We have no idea which one is better.  This is a bug, but not
338      a critical problem; warn the user.  */
339   warning (_("Selected architecture %s is ambiguous with "
340              "reported target architecture %s"),
341            selected->printable_name, from_target->printable_name);
342   return selected;
343 }
344
345 /* Functions to manipulate the architecture of the target */
346
347 enum set_arch { set_arch_auto, set_arch_manual };
348
349 static const struct bfd_arch_info *target_architecture_user;
350
351 static const char *set_architecture_string;
352
353 const char *
354 selected_architecture_name (void)
355 {
356   if (target_architecture_user == NULL)
357     return NULL;
358   else
359     return set_architecture_string;
360 }
361
362 /* Called if the user enters ``show architecture'' without an
363    argument. */
364
365 static void
366 show_architecture (struct ui_file *file, int from_tty,
367                    struct cmd_list_element *c, const char *value)
368 {
369   const char *arch;
370   arch = gdbarch_bfd_arch_info (current_gdbarch)->printable_name;
371   if (target_architecture_user == NULL)
372     fprintf_filtered (file, _("\
373 The target architecture is set automatically (currently %s)\n"), arch);
374   else
375     fprintf_filtered (file, _("\
376 The target architecture is assumed to be %s\n"), arch);
377 }
378
379
380 /* Called if the user enters ``set architecture'' with or without an
381    argument. */
382
383 static void
384 set_architecture (char *ignore_args, int from_tty, struct cmd_list_element *c)
385 {
386   struct gdbarch_info info;
387
388   gdbarch_info_init (&info);
389
390   if (strcmp (set_architecture_string, "auto") == 0)
391     {
392       target_architecture_user = NULL;
393       if (!gdbarch_update_p (info))
394         internal_error (__FILE__, __LINE__,
395                         _("could not select an architecture automatically"));
396     }
397   else
398     {
399       info.bfd_arch_info = bfd_scan_arch (set_architecture_string);
400       if (info.bfd_arch_info == NULL)
401         internal_error (__FILE__, __LINE__,
402                         _("set_architecture: bfd_scan_arch failed"));
403       if (gdbarch_update_p (info))
404         target_architecture_user = info.bfd_arch_info;
405       else
406         printf_unfiltered (_("Architecture `%s' not recognized.\n"),
407                            set_architecture_string);
408     }
409   show_architecture (gdb_stdout, from_tty, NULL, NULL);
410 }
411
412 /* Try to select a global architecture that matches "info".  Return
413    non-zero if the attempt succeds.  */
414 int
415 gdbarch_update_p (struct gdbarch_info info)
416 {
417   struct gdbarch *new_gdbarch = gdbarch_find_by_info (info);
418
419   /* If there no architecture by that name, reject the request.  */
420   if (new_gdbarch == NULL)
421     {
422       if (gdbarch_debug)
423         fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
424                             "Architecture not found\n");
425       return 0;
426     }
427
428   /* If it is the same old architecture, accept the request (but don't
429      swap anything).  */
430   if (new_gdbarch == current_gdbarch)
431     {
432       if (gdbarch_debug)
433         fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
434                             "Architecture 0x%08lx (%s) unchanged\n",
435                             (long) new_gdbarch,
436                             gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
437       return 1;
438     }
439
440   /* It's a new architecture, swap it in.  */
441   if (gdbarch_debug)
442     fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
443                         "New architecture 0x%08lx (%s) selected\n",
444                         (long) new_gdbarch,
445                         gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
446   deprecated_current_gdbarch_select_hack (new_gdbarch);
447
448   return 1;
449 }
450
451 /* Return the architecture for ABFD.  If no suitable architecture
452    could be find, return NULL.  */
453
454 struct gdbarch *
455 gdbarch_from_bfd (bfd *abfd)
456 {
457   struct gdbarch_info info;
458
459   /* If we call gdbarch_find_by_info without filling in info.abfd,
460      then it will use the global exec_bfd.  That's fine if we don't
461      have one of those either.  And that's the only time we should
462      reach here with a NULL ABFD argument - when we are discarding
463      the executable.  */
464   gdb_assert (abfd != NULL || exec_bfd == NULL);
465
466   gdbarch_info_init (&info);
467   info.abfd = abfd;
468   return gdbarch_find_by_info (info);
469 }
470
471 /* Set the dynamic target-system-dependent parameters (architecture,
472    byte-order) using information found in the BFD */
473
474 void
475 set_gdbarch_from_file (bfd *abfd)
476 {
477   struct gdbarch *gdbarch;
478
479   gdbarch = gdbarch_from_bfd (abfd);
480   if (gdbarch == NULL)
481     error (_("Architecture of file not recognized."));
482   deprecated_current_gdbarch_select_hack (gdbarch);
483 }
484
485 /* Initialize the current architecture.  Update the ``set
486    architecture'' command so that it specifies a list of valid
487    architectures.  */
488
489 #ifdef DEFAULT_BFD_ARCH
490 extern const bfd_arch_info_type DEFAULT_BFD_ARCH;
491 static const bfd_arch_info_type *default_bfd_arch = &DEFAULT_BFD_ARCH;
492 #else
493 static const bfd_arch_info_type *default_bfd_arch;
494 #endif
495
496 #ifdef DEFAULT_BFD_VEC
497 extern const bfd_target DEFAULT_BFD_VEC;
498 static const bfd_target *default_bfd_vec = &DEFAULT_BFD_VEC;
499 #else
500 static const bfd_target *default_bfd_vec;
501 #endif
502
503 static int default_byte_order = BFD_ENDIAN_UNKNOWN;
504
505 void
506 initialize_current_architecture (void)
507 {
508   const char **arches = gdbarch_printable_names ();
509
510   /* determine a default architecture and byte order. */
511   struct gdbarch_info info;
512   gdbarch_info_init (&info);
513   
514   /* Find a default architecture. */
515   if (default_bfd_arch == NULL)
516     {
517       /* Choose the architecture by taking the first one
518          alphabetically. */
519       const char *chosen = arches[0];
520       const char **arch;
521       for (arch = arches; *arch != NULL; arch++)
522         {
523           if (strcmp (*arch, chosen) < 0)
524             chosen = *arch;
525         }
526       if (chosen == NULL)
527         internal_error (__FILE__, __LINE__,
528                         _("initialize_current_architecture: No arch"));
529       default_bfd_arch = bfd_scan_arch (chosen);
530       if (default_bfd_arch == NULL)
531         internal_error (__FILE__, __LINE__,
532                         _("initialize_current_architecture: Arch not found"));
533     }
534
535   info.bfd_arch_info = default_bfd_arch;
536
537   /* Take several guesses at a byte order.  */
538   if (default_byte_order == BFD_ENDIAN_UNKNOWN
539       && default_bfd_vec != NULL)
540     {
541       /* Extract BFD's default vector's byte order. */
542       switch (default_bfd_vec->byteorder)
543         {
544         case BFD_ENDIAN_BIG:
545           default_byte_order = BFD_ENDIAN_BIG;
546           break;
547         case BFD_ENDIAN_LITTLE:
548           default_byte_order = BFD_ENDIAN_LITTLE;
549           break;
550         default:
551           break;
552         }
553     }
554   if (default_byte_order == BFD_ENDIAN_UNKNOWN)
555     {
556       /* look for ``*el-*'' in the target name. */
557       const char *chp;
558       chp = strchr (target_name, '-');
559       if (chp != NULL
560           && chp - 2 >= target_name
561           && strncmp (chp - 2, "el", 2) == 0)
562         default_byte_order = BFD_ENDIAN_LITTLE;
563     }
564   if (default_byte_order == BFD_ENDIAN_UNKNOWN)
565     {
566       /* Wire it to big-endian!!! */
567       default_byte_order = BFD_ENDIAN_BIG;
568     }
569
570   info.byte_order = default_byte_order;
571
572   if (! gdbarch_update_p (info))
573     internal_error (__FILE__, __LINE__,
574                     _("initialize_current_architecture: Selection of "
575                       "initial architecture failed"));
576
577   /* Create the ``set architecture'' command appending ``auto'' to the
578      list of architectures. */
579   {
580     struct cmd_list_element *c;
581     /* Append ``auto''. */
582     int nr;
583     for (nr = 0; arches[nr] != NULL; nr++);
584     arches = xrealloc (arches, sizeof (char*) * (nr + 2));
585     arches[nr + 0] = "auto";
586     arches[nr + 1] = NULL;
587     add_setshow_enum_cmd ("architecture", class_support,
588                           arches, &set_architecture_string, _("\
589 Set architecture of target."), _("\
590 Show architecture of target."), NULL,
591                           set_architecture, show_architecture,
592                           &setlist, &showlist);
593     add_alias_cmd ("processor", "architecture", class_support, 1, &setlist);
594   }
595 }
596
597
598 /* Initialize a gdbarch info to values that will be automatically
599    overridden.  Note: Originally, this ``struct info'' was initialized
600    using memset(0).  Unfortunately, that ran into problems, namely
601    BFD_ENDIAN_BIG is zero.  An explicit initialization function that
602    can explicitly set each field to a well defined value is used.  */
603
604 void
605 gdbarch_info_init (struct gdbarch_info *info)
606 {
607   memset (info, 0, sizeof (struct gdbarch_info));
608   info->byte_order = BFD_ENDIAN_UNKNOWN;
609   info->osabi = GDB_OSABI_UNINITIALIZED;
610 }
611
612 /* Similar to init, but this time fill in the blanks.  Information is
613    obtained from the global "set ..." options and explicitly
614    initialized INFO fields.  */
615
616 void
617 gdbarch_info_fill (struct gdbarch_info *info)
618 {
619   /* Check for the current file.  */
620   if (info->abfd == NULL)
621     info->abfd = exec_bfd;
622   if (info->abfd == NULL)
623     info->abfd = core_bfd;
624
625   /* Check for the current target description.  */
626   if (info->target_desc == NULL)
627     info->target_desc = target_current_description ();
628
629   /* "(gdb) set architecture ...".  */
630   if (info->bfd_arch_info == NULL
631       && target_architecture_user)
632     info->bfd_arch_info = target_architecture_user;
633   /* From the file.  */
634   if (info->bfd_arch_info == NULL
635       && info->abfd != NULL
636       && bfd_get_arch (info->abfd) != bfd_arch_unknown
637       && bfd_get_arch (info->abfd) != bfd_arch_obscure)
638     info->bfd_arch_info = bfd_get_arch_info (info->abfd);
639   /* From the target.  */
640   if (info->target_desc != NULL)
641     info->bfd_arch_info = choose_architecture_for_target
642       (info->bfd_arch_info, tdesc_architecture (info->target_desc));
643   /* From the default.  */
644   if (info->bfd_arch_info == NULL)
645     info->bfd_arch_info = default_bfd_arch;
646
647   /* "(gdb) set byte-order ...".  */
648   if (info->byte_order == BFD_ENDIAN_UNKNOWN
649       && target_byte_order_user != BFD_ENDIAN_UNKNOWN)
650     info->byte_order = target_byte_order_user;
651   /* From the INFO struct.  */
652   if (info->byte_order == BFD_ENDIAN_UNKNOWN
653       && info->abfd != NULL)
654     info->byte_order = (bfd_big_endian (info->abfd) ? BFD_ENDIAN_BIG
655                         : bfd_little_endian (info->abfd) ? BFD_ENDIAN_LITTLE
656                         : BFD_ENDIAN_UNKNOWN);
657   /* From the default.  */
658   if (info->byte_order == BFD_ENDIAN_UNKNOWN)
659     info->byte_order = default_byte_order;
660
661   /* "(gdb) set osabi ...".  Handled by gdbarch_lookup_osabi.  */
662   if (info->osabi == GDB_OSABI_UNINITIALIZED)
663     info->osabi = gdbarch_lookup_osabi (info->abfd);
664
665   /* Must have at least filled in the architecture.  */
666   gdb_assert (info->bfd_arch_info != NULL);
667 }
668
669 /* */
670
671 extern initialize_file_ftype _initialize_gdbarch_utils; /* -Wmissing-prototypes */
672
673 void
674 _initialize_gdbarch_utils (void)
675 {
676   struct cmd_list_element *c;
677   add_setshow_enum_cmd ("endian", class_support,
678                         endian_enum, &set_endian_string, _("\
679 Set endianness of target."), _("\
680 Show endianness of target."), NULL,
681                         set_endian, show_endian,
682                         &setlist, &showlist);
683 }