CARP:
[external/binutils.git] / gdb / gdbarch.c
1 /* Semi-dynamic architecture support for GDB, the GNU debugger.
2    Copyright 1998, 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, Boston, MA 02111-1307, USA.  */
19
20 #include "defs.h"
21 #include "bfd.h"
22 #include "gdbcmd.h"
23
24
25 /* start-sanitize-carp start-sanitize-vr4xxx */
26 /* Convenience macro for allocting memory. */
27
28 #ifndef XMALLOC
29 #define XMALLOC(TYPE) (TYPE*) xmalloc (sizeof (TYPE))
30 #endif
31
32 /* end-sanitize-carp end-sanitize-vr4xxx */
33
34 /* Non-zero if we want to trace architecture code.  */
35
36 #ifndef GDBARCH_DEBUG
37 #define GDBARCH_DEBUG 0
38 #endif
39 int gdbarch_debug = GDBARCH_DEBUG;
40
41 /* start-sanitize-carp start-sanitize-vr4xxx */
42
43 /* Maintain the struct gdbarch object */
44
45 struct gdbarch
46 {
47   /* basic architectural information */
48   const struct bfd_arch_info *bfd_arch_info;
49   int byte_order;
50
51   /* target specific vector. */
52   struct gdbarch_tdep *tdep;
53
54   /* per-architecture data-pointers */
55   int nr_data;
56   void **data;
57
58   /* per-architecture swap-regions */
59   struct gdbarch_swap *swap;
60
61   /* Multi-arch values.
62
63      When adding to the below you must also: declare/define set/get
64      value functions; override the corresponding macro in gdbarch.h;
65      if zero/NULL is not a suitable default, initialize the field in
66      gdbarch_alloc(); confirm that the target updated the value
67      correctly in verify_gdbarch(); add a fprintf_unfiltered call to
68      gdbarch_update() so that the new field is dumped out; append an
69      initial value to the static variable ``default_gdbarch'' (base
70      values on the host's c-type system). */
71
72   int long_bit;
73   int long_long_bit;
74   int ptr_bit;
75
76 };
77
78
79 struct gdbarch_tdep *
80 gdbarch_tdep (gdbarch)
81      struct gdbarch *gdbarch;
82 {
83   return gdbarch->tdep;
84 }
85
86 const struct bfd_arch_info *
87 gdbarch_bfd_arch_info (gdbarch)
88      struct gdbarch *gdbarch;
89 {
90   return gdbarch->bfd_arch_info;
91 }
92
93 int
94 gdbarch_byte_order (gdbarch)
95      struct gdbarch *gdbarch;
96 {
97   return gdbarch->byte_order;
98 }
99
100 int
101 gdbarch_long_bit (gdbarch)
102      struct gdbarch *gdbarch;
103 {
104   return gdbarch->long_bit;
105 }
106
107 void
108 set_gdbarch_long_bit (gdbarch, long_bit)
109      struct gdbarch *gdbarch;
110      int long_bit;
111 {
112   gdbarch->long_bit = long_bit;
113 }
114
115 int
116 gdbarch_long_long_bit (gdbarch)
117      struct gdbarch *gdbarch;
118 {
119   return gdbarch->long_long_bit;
120 }
121
122 void
123 set_gdbarch_long_long_bit (gdbarch, long_long_bit)
124      struct gdbarch *gdbarch;
125      int long_long_bit;
126 {
127   gdbarch->long_long_bit = long_long_bit;
128 }
129
130 int
131 gdbarch_ptr_bit (gdbarch)
132      struct gdbarch *gdbarch;
133 {
134   return gdbarch->ptr_bit;
135 }
136
137 void
138 set_gdbarch_ptr_bit (gdbarch, ptr_bit)
139      struct gdbarch *gdbarch;
140      int ptr_bit;
141 {
142   gdbarch->ptr_bit = ptr_bit;
143 }
144
145
146 /* Ensure that all values in a GDBARCH are reasonable. XXX - should
147    this instead return a success/fail indication? */
148
149 static void
150 verify_gdbarch (gdbarch)
151      struct gdbarch *gdbarch;
152 {
153   /* fundamental */
154   if (gdbarch->byte_order == 0)
155     fatal ("verify_gdbarch: byte-order unset");
156   if (gdbarch->bfd_arch_info == NULL)
157     fatal ("verify_gdbarch: bfd_arch_info unset");
158   /* more general */
159   if (gdbarch->long_bit == 0)
160     fatal ("verify_gdbarch: long_bit invalid");
161   if (gdbarch->long_long_bit == 0)
162     fatal ("verify_gdbarch: long_long_bit invalid");
163   if (gdbarch->ptr_bit == 0)
164     fatal ("verify_gdbarch: ptr_bit invalid");
165 }
166
167
168 /* Keep a registrary of per-architecture data-pointers required by GDB
169    modules. */
170
171 struct gdbarch_data
172 {
173   int index;
174 };
175
176 struct gdbarch_data_registration
177 {
178   gdbarch_data_ftype *init;
179   struct gdbarch_data *data;
180   struct gdbarch_data_registration *next;
181 };
182
183 struct gdbarch_data_registrary
184 {
185   int nr;
186   struct gdbarch_data_registration *registrations;
187 };
188
189 struct gdbarch_data_registrary gdbarch_data_registrary =
190 {
191   0, NULL,
192 };
193
194 struct gdbarch_data *
195 register_gdbarch_data (init)
196      gdbarch_data_ftype *init;
197 {
198   struct gdbarch_data_registration **curr;
199   for (curr = &gdbarch_data_registrary.registrations;
200        (*curr) != NULL;
201        curr = &(*curr)->next);
202   (*curr) = XMALLOC (struct gdbarch_data_registration);
203   (*curr)->next = NULL;
204   (*curr)->init = init;
205   (*curr)->data = XMALLOC (struct gdbarch_data);
206   (*curr)->data->index = gdbarch_data_registrary.nr++;
207   return (*curr)->data;
208 }
209
210
211 /* Walk through all the registered users initializing each in turn. */
212
213 static void init_gdbarch_data PARAMS ((struct gdbarch *));
214 static void
215 init_gdbarch_data (gdbarch)
216      struct gdbarch *gdbarch;
217 {
218   struct gdbarch_data_registration *rego;
219   gdbarch->nr_data = gdbarch_data_registrary.nr + 1;
220   gdbarch->data = xmalloc (sizeof (void*) * gdbarch->nr_data);
221   for (rego = gdbarch_data_registrary.registrations;
222        rego != NULL;
223        rego = rego->next)
224     {
225       if (rego->data->index < gdbarch->nr_data)
226         gdbarch->data[rego->data->index] = rego->init ();
227     }
228 }
229
230
231 /* Return the current value of the specified per-architecture
232    data-pointer. */
233
234 void *
235 gdbarch_data (data)
236      struct gdbarch_data *data;
237 {
238   if (data->index >= current_gdbarch->nr_data)
239     fatal ("gdbarch_data: request for non-existant data.");
240   return current_gdbarch->data[data->index];
241 }
242
243
244
245 /* Keep a registrary of swaped data required by GDB modules. */
246
247 struct gdbarch_swap
248 {
249   void *swap;
250   struct gdbarch_swap_registration *source;
251   struct gdbarch_swap *next;
252 };
253
254 struct gdbarch_swap_registration
255 {
256   void *data;
257   unsigned long sizeof_data;
258   gdbarch_swap_ftype *init;
259   struct gdbarch_swap_registration *next;
260 };
261
262 struct gdbarch_swap_registrary
263 {
264   int nr;
265   struct gdbarch_swap_registration *registrations;
266 };
267
268 struct gdbarch_swap_registrary gdbarch_swap_registrary = 
269 {
270   0, NULL,
271 };
272
273 void
274 register_gdbarch_swap (data, sizeof_data, init)
275      void *data;
276      unsigned long sizeof_data;
277      gdbarch_swap_ftype *init;
278 {
279   struct gdbarch_swap_registration **rego;
280   for (rego = &gdbarch_swap_registrary.registrations;
281        (*rego) != NULL;
282        rego = &(*rego)->next);
283   (*rego) = XMALLOC (struct gdbarch_swap_registration);
284   (*rego)->next = NULL;
285   (*rego)->init = init;
286   (*rego)->data = data;
287   (*rego)->sizeof_data = sizeof_data;
288 }
289
290
291 static void init_gdbarch_swap PARAMS ((struct gdbarch *));
292 static void
293 init_gdbarch_swap (gdbarch)
294      struct gdbarch *gdbarch;
295 {
296   struct gdbarch_swap_registration *rego;
297   struct gdbarch_swap **curr = &gdbarch->swap;
298   for (rego = gdbarch_swap_registrary.registrations;
299        rego != NULL;
300        rego = rego->next)
301     {
302       if (rego->data != NULL)
303         {
304           (*curr) = XMALLOC (struct gdbarch_swap);
305           (*curr)->source = rego;
306           (*curr)->swap = xmalloc (rego->sizeof_data);
307           (*curr)->next = NULL;
308           memset (rego->data, 0, rego->sizeof_data);
309           curr = &(*curr)->next;
310         }
311       if (rego->init != NULL)
312         rego->init ();
313     }
314 }
315
316 static void swapout_gdbarch_swap PARAMS ((struct gdbarch *));
317 static void
318 swapout_gdbarch_swap (gdbarch)
319      struct gdbarch *gdbarch;
320 {
321   struct gdbarch_swap *curr;
322   for (curr = gdbarch->swap;
323        curr != NULL;
324        curr = curr->next)
325     memcpy (curr->swap, curr->source->data, curr->source->sizeof_data);
326 }
327
328 static void swapin_gdbarch_swap PARAMS ((struct gdbarch *));
329 static void
330 swapin_gdbarch_swap (gdbarch)
331      struct gdbarch *gdbarch;
332 {
333   struct gdbarch_swap *curr;
334   for (curr = gdbarch->swap;
335        curr != NULL;
336        curr = curr->next)
337     memcpy (curr->source->data, curr->swap, curr->source->sizeof_data);
338 }
339
340
341 /* Keep a registrary of the architectures known by GDB. */
342
343 struct gdbarch_init_registration
344 {
345   enum bfd_architecture bfd_architecture;
346   gdbarch_init_ftype *init;
347   struct gdbarch_list *arches;
348   struct gdbarch_init_registration *next;
349 };
350
351 static struct gdbarch_init_registration *gdbarch_init_registrary = NULL;
352
353 void
354 register_gdbarch_init (bfd_architecture, init)
355      enum bfd_architecture bfd_architecture;
356      gdbarch_init_ftype *init;
357 {
358   struct gdbarch_init_registration **curr;
359   const struct bfd_arch_info *bfd_arch_info;
360   /* Check that BFD reconizes this architecture */
361   bfd_arch_info = bfd_lookup_arch (bfd_architecture, 0);
362   if (bfd_arch_info == NULL)
363     {
364       fatal ("Attempt to register unknown architecture (%d)", bfd_architecture);
365     }
366   /* Check that we haven't seen this architecture before */
367   for (curr = &gdbarch_init_registrary;
368        (*curr) != NULL;
369        curr = &(*curr)->next)
370     {
371       if (bfd_architecture == (*curr)->bfd_architecture)
372         fatal ("Duplicate registraration of architecture (%s)",
373                bfd_arch_info->printable_name);
374     }
375   /* log it */
376   if (gdbarch_debug)
377     fprintf_unfiltered (stderr, "register_gdbarch_init (%s, 0x%08lx)\n",
378                         bfd_arch_info->printable_name,
379                         (long) init);
380   /* Append it */
381   (*curr) = XMALLOC (struct gdbarch_init_registration);
382   (*curr)->bfd_architecture = bfd_architecture;
383   (*curr)->init = init;
384   (*curr)->arches = NULL;
385   (*curr)->next = NULL;
386 }
387   
388
389
390 /* Look for an architecture using gdbarch_info.  Base search on only
391    BFD_ARCH_INFO and BYTE_ORDER. */
392
393 struct gdbarch_list *
394 gdbarch_list_lookup_by_info (arches, info)
395      struct gdbarch_list *arches;
396      const struct gdbarch_info *info;
397 {
398   for (; arches != NULL; arches = arches->next)
399     {
400       if (info->bfd_arch_info != arches->gdbarch->bfd_arch_info)
401         continue;
402       if (info->byte_order != arches->gdbarch->byte_order)
403         continue;
404       return arches;
405     }
406   return NULL;
407 }
408
409
410 /* Create a new ``struct gdbarch'' based in information provied by
411    ``struct gdbarch_info'' */
412
413 struct gdbarch *
414 gdbarch_alloc (info, tdep)
415      const struct gdbarch_info *info;
416      struct gdbarch_tdep *tdep;
417 {
418   struct gdbarch *gdbarch = XMALLOC (struct gdbarch);
419   memset (gdbarch, 0, sizeof (*gdbarch));
420
421   gdbarch->tdep = tdep;
422
423   gdbarch->bfd_arch_info = info->bfd_arch_info;
424   gdbarch->byte_order = info->byte_order;
425
426   return gdbarch;
427 }
428
429 /* Update the current architecture. Return ZERO if the update request
430    failed. */
431
432 int
433 gdbarch_update (info)
434      struct gdbarch_info info;
435 {
436   struct gdbarch *new_gdbarch;
437   struct gdbarch_list **list;
438   struct gdbarch_init_registration *rego;
439
440   /* Fill in any missing bits. Most important is the bfd_architecture
441      which is used to select the target architecture. */
442   if (info.bfd_architecture == bfd_arch_unknown)
443     {
444       if (info.bfd_arch_info != NULL)
445         info.bfd_architecture = info.bfd_arch_info->arch;
446       else if (info.abfd != NULL)
447         info.bfd_architecture = bfd_get_arch (info.abfd);
448       /* FIXME - should query BFD for its default architecture. */
449       else
450         info.bfd_architecture = current_gdbarch->bfd_arch_info->arch;
451     }
452   if (info.bfd_arch_info == NULL)
453     {
454       if (target_architecture_auto && info.abfd != NULL)
455         info.bfd_arch_info = bfd_get_arch_info (info.abfd);
456       else
457         info.bfd_arch_info = current_gdbarch->bfd_arch_info;
458     }
459   if (info.byte_order == 0)
460     {
461       if (target_byte_order_auto && info.abfd != NULL)
462         info.byte_order = (bfd_big_endian (info.abfd) ? BIG_ENDIAN
463                            : bfd_little_endian (info.abfd) ? LITTLE_ENDIAN
464                            : 0);
465       else
466         info.byte_order = current_gdbarch->byte_order;
467     }
468   /* A default for abfd? */
469
470   /* Find the target that knows about this architecture. */
471   for (rego = gdbarch_init_registrary;
472        rego != NULL && rego->bfd_architecture != info.bfd_architecture;
473        rego = rego->next);
474   if (rego == NULL)
475     {
476       if (gdbarch_debug)
477         fprintf_unfiltered (stderr, "gdbarch_update: No matching architecture\n");
478       return 0;
479     }
480
481   if (gdbarch_debug)
482     {
483       fprintf_unfiltered (stderr,
484                           "gdbarch_update: info.bfd_architecture %d (%s)\n",
485                           info.bfd_architecture,
486                           bfd_lookup_arch (info.bfd_architecture, 0)->printable_name);
487       fprintf_unfiltered (stderr,
488                           "gdbarch_update: info.bfd_arch_info %s\n",
489                           (info.bfd_arch_info != NULL
490                            ? info.bfd_arch_info->printable_name
491                            : "(null)"));
492       fprintf_unfiltered (stderr,
493                           "gdbarch_update: info.byte_order %d (%s)\n",
494                           info.byte_order,
495                           (info.byte_order == BIG_ENDIAN ? "big"
496                            : info.byte_order == LITTLE_ENDIAN ? "little"
497                            : "default"));
498       fprintf_unfiltered (stderr,
499                           "gdbarch_update: info.abfd 0x%lx\n",
500                           (long) info.abfd);
501       fprintf_unfiltered (stderr,
502                           "gdbarch_update: info.tdep_info 0x%lx\n",
503                           (long) info.tdep_info);
504     }
505
506   /* Ask the target for a replacement architecture. */
507   new_gdbarch = rego->init (info, rego->arches);
508
509   /* Did the target like it?  No. Reject the change. */
510   if (new_gdbarch == NULL)
511     {
512       if (gdbarch_debug)
513         fprintf_unfiltered (stderr, "gdbarch_update: Target rejected architecture\n");
514       return 0;
515     }
516
517   /* Did the architecture change?  No. Do nothing. */
518   if (current_gdbarch == new_gdbarch)
519     {
520       if (gdbarch_debug)
521         fprintf_unfiltered (stderr, "gdbarch_update: Architecture 0x%08lx (%s) unchanged\n",
522                             (long) new_gdbarch,
523                             new_gdbarch->bfd_arch_info->printable_name);
524       return 1;
525     }
526
527   /* Swap all data belonging to the old target out */
528   swapout_gdbarch_swap (current_gdbarch);
529
530   /* Is this a pre-existing architecture?  Yes. Swap it in.  */
531   for (list = &rego->arches;
532        (*list) != NULL;
533        list = &(*list)->next)
534     {
535       if ((*list)->gdbarch == new_gdbarch)
536         {
537           if (gdbarch_debug)
538             fprintf_unfiltered (stderr, "gdbarch_update: Previous architecture 0x%08lx (%s) selected\n",
539                                 (long) new_gdbarch,
540                                 new_gdbarch->bfd_arch_info->printable_name);
541           current_gdbarch = new_gdbarch;
542           swapin_gdbarch_swap (new_gdbarch);
543           return 1;
544         }
545     }
546     
547   /* Append this new architecture to this targets list. */
548   (*list) = XMALLOC (struct gdbarch_list);
549   (*list)->next = NULL;
550   (*list)->gdbarch = new_gdbarch;
551
552   /* Switch to this new architecture.  Dump it out. */
553   current_gdbarch = new_gdbarch;
554   if (gdbarch_debug)
555     {
556       fprintf_unfiltered (stderr,
557                           "gdbarch_update: New architecture 0x%08lx (%s) selected\n",
558                           (long) new_gdbarch,
559                           new_gdbarch->bfd_arch_info->printable_name);
560       fprintf_unfiltered (stderr,
561                           "gdbarch_update: TARGET_BYTE_ORDER = %d (%s)\n",
562                           TARGET_BYTE_ORDER,
563                           (TARGET_BYTE_ORDER == BIG_ENDIAN ? "big"
564                            : TARGET_BYTE_ORDER == LITTLE_ENDIAN ? "little"
565                            : "default"));
566       fprintf_unfiltered (stderr,
567                           "gdbarch_update: TARGET_LONG_BIT = %d\n",
568                           TARGET_LONG_BIT);
569       fprintf_unfiltered (stderr,
570                           "gdbarch_update: TARGET_LONG_LONG_BIT = %d\n",
571                           TARGET_LONG_LONG_BIT);
572       fprintf_unfiltered (stderr,
573                           "gdbarch_update: TARGET_PTR_BIT = %d\n",
574                           TARGET_PTR_BIT);
575     }
576   
577   /* Check that the newly installed architecture is valid.  */
578   verify_gdbarch (new_gdbarch);
579
580   /* Initialize the per-architecture memory (swap) areas.
581      CURRENT_GDBARCH must be update before these modules are
582      called. */
583   init_gdbarch_swap (new_gdbarch);
584   
585   /* Initialize the per-architecture data-pointer of all parties that
586      registered an interest in this architecture.  CURRENT_GDBARCH
587      must be updated before these modules are called. */
588   init_gdbarch_data (new_gdbarch);
589   
590   return 1;
591 }
592
593
594 /* end-sanitize-carp end-sanitize-vr4xxx */
595
596 /* Functions to manipulate the endianness of the target.  */
597
598 #ifdef TARGET_BYTE_ORDER_SELECTABLE
599 /* compat - Catch old targets that expect a selectable byte-order to
600    default to BIG_ENDIAN */
601 #ifndef TARGET_BYTE_ORDER_DEFAULT
602 #define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN
603 #endif
604 #endif
605 #ifndef TARGET_BYTE_ORDER_DEFAULT
606 /* compat - Catch old non byte-order selectable targets that do not
607    define TARGET_BYTE_ORDER_DEFAULT and instead expect
608    TARGET_BYTE_ORDER to be used as the default.  For targets that
609    defined neither TARGET_BYTE_ORDER nor TARGET_BYTE_ORDER_DEFAULT the
610    below will get a strange compiler warning. */
611 #define TARGET_BYTE_ORDER_DEFAULT TARGET_BYTE_ORDER
612 #endif
613 int target_byte_order = TARGET_BYTE_ORDER_DEFAULT;
614 int target_byte_order_auto = 1;
615
616 /* Chain containing the \"set endian\" commands.  */
617 static struct cmd_list_element *endianlist = NULL;
618
619 /* Called by ``show endian''.  */
620 static void show_endian PARAMS ((char *, int));
621 static void
622 show_endian (args, from_tty)
623      char *args;
624      int from_tty;
625 {
626   char *msg =
627     (TARGET_BYTE_ORDER_AUTO
628      ? "The target endianness is set automatically (currently %s endian)\n"
629      : "The target is assumed to be %s endian\n");
630   printf_unfiltered (msg, (TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little"));
631 }
632
633 /* Called if the user enters ``set endian'' without an argument.  */
634 static void set_endian PARAMS ((char *, int));
635 static void
636 set_endian (args, from_tty)
637      char *args;
638      int from_tty;
639 {
640   printf_unfiltered ("\"set endian\" must be followed by \"auto\", \"big\" or \"little\".\n");
641   show_endian (args, from_tty);
642 }
643
644 /* Called by ``set endian big''.  */
645 static void set_endian_big PARAMS ((char *, int));
646 static void
647 set_endian_big (args, from_tty)
648      char *args;
649      int from_tty;
650 {
651   if (TARGET_BYTE_ORDER_SELECTABLE_P)
652     {
653       target_byte_order = BIG_ENDIAN;
654       target_byte_order_auto = 0;
655       /* start-sanitize-carp start-sanitize-vr4xxx */
656       if (GDB_MULTI_ARCH)
657         {
658           struct gdbarch_info info;
659           memset (&info, 0, sizeof info);
660           info.byte_order = BIG_ENDIAN;
661           gdbarch_update (info);
662         }
663       /* end-sanitize-carp end-sanitize-vr4xxx */
664     }
665   else
666     {
667       printf_unfiltered ("Byte order is not selectable.");
668       show_endian (args, from_tty);
669     }
670 }
671
672 /* Called by ``set endian little''.  */
673 static void set_endian_little PARAMS ((char *, int));
674 static void
675 set_endian_little (args, from_tty)
676      char *args;
677      int from_tty;
678 {
679   if (TARGET_BYTE_ORDER_SELECTABLE_P)
680     {
681       target_byte_order = LITTLE_ENDIAN;
682       target_byte_order_auto = 0;
683       /* start-sanitize-carp start-sanitize-vr4xxx */
684       if (GDB_MULTI_ARCH)
685         {
686           struct gdbarch_info info;
687           memset (&info, 0, sizeof info);
688           info.byte_order = LITTLE_ENDIAN;
689           gdbarch_update (info);
690         }
691       /* end-sanitize-carp end-sanitize-vr4xxx */
692     }
693   else
694     {
695       printf_unfiltered ("Byte order is not selectable.");
696       show_endian (args, from_tty);
697     }
698 }
699
700 /* Called by ``set endian auto''.  */
701 static void set_endian_auto PARAMS ((char *, int));
702 static void
703 set_endian_auto (args, from_tty)
704      char *args;
705      int from_tty;
706 {
707   if (TARGET_BYTE_ORDER_SELECTABLE_P)
708     {
709       target_byte_order_auto = 1;
710     }
711   else
712     {
713       printf_unfiltered ("Byte order is not selectable.");
714       show_endian (args, from_tty);
715     }
716 }
717
718 /* Set the endianness from a BFD.  */
719 static void set_endian_from_file PARAMS ((bfd *));
720 static void
721 set_endian_from_file (abfd)
722      bfd *abfd;
723 {
724   if (TARGET_BYTE_ORDER_SELECTABLE_P)
725     {
726       int want;
727       
728       if (bfd_big_endian (abfd))
729         want = BIG_ENDIAN;
730       else
731         want = LITTLE_ENDIAN;
732       if (TARGET_BYTE_ORDER_AUTO)
733         target_byte_order = want;
734       else if (TARGET_BYTE_ORDER != want)
735         warning ("%s endian file does not match %s endian target.",
736                  want == BIG_ENDIAN ? "big" : "little",
737                  TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little");
738     }
739   else
740     {
741       if (bfd_big_endian (abfd)
742           ? TARGET_BYTE_ORDER != BIG_ENDIAN
743           : TARGET_BYTE_ORDER == BIG_ENDIAN)
744         warning ("%s endian file does not match %s endian target.",
745                  bfd_big_endian (abfd) ? "big" : "little",
746                  TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little");
747     }
748 }
749
750
751
752 /* Functions to manipulate the architecture of the target */
753
754 int target_architecture_auto = 1;
755 extern const struct bfd_arch_info bfd_default_arch_struct;
756 const struct bfd_arch_info *target_architecture = &bfd_default_arch_struct;
757 int (*target_architecture_hook) PARAMS ((const struct bfd_arch_info *ap));
758
759 /* Do the real work of changing the current architecture */
760 static void
761 set_arch (arch)
762      const struct bfd_arch_info *arch;
763 {
764   /* FIXME: Is it compatible with gdb? */
765   /* Check with the target on the setting */
766   if (target_architecture_hook != NULL
767       && !target_architecture_hook (arch))
768     printf_unfiltered ("Target does not support `%s' architecture.\n",
769                        arch->printable_name);
770   else
771     {
772       target_architecture_auto = 0;
773       target_architecture = arch;
774     }
775 }
776
777 /* Called if the user enters ``show architecture'' without an argument. */
778 static void show_architecture PARAMS ((char *, int));
779 static void
780 show_architecture (args, from_tty)
781      char *args;
782      int from_tty;
783 {
784   const char *arch;
785   arch = TARGET_ARCHITECTURE->printable_name;
786   if (target_architecture_auto)
787     printf_filtered ("The target architecture is set automatically (currently %s)\n", arch);
788   else
789     printf_filtered ("The target architecture is assumed to be %s\n", arch);
790 }
791
792 /* Called if the user enters ``set architecture'' with or without an
793    argument. */
794 static void set_architecture PARAMS ((char *, int));
795 static void
796 set_architecture (args, from_tty)
797      char *args;
798      int from_tty;
799 {
800   if (args == NULL)
801     {
802       printf_unfiltered ("\"set architecture\" must be followed by \"auto\" or an architecture name.\n");
803     }
804   else if (strcmp (args, "auto") == 0)
805     {
806       target_architecture_auto = 1;
807     }
808   /* start-sanitize-carp start-sanitize-vr4xxx */
809   else if (GDB_MULTI_ARCH)
810     {
811       const struct bfd_arch_info *arch = bfd_scan_arch (args);
812       if (arch == NULL)
813         printf_unfiltered ("Architecture `%s' not reconized.\n", args);
814       else
815         {
816           struct gdbarch_info info;
817           memset (&info, 0, sizeof info);
818           info.bfd_arch_info = arch;
819           if (gdbarch_update (info))
820             target_architecture_auto = 0;
821           else
822             printf_unfiltered ("Architecture `%s' not reconized.\n", args);
823         }
824     }
825   /* end-sanitize-carp end-sanitize-vr4xxx */
826   else
827     {
828       const struct bfd_arch_info *arch = bfd_scan_arch (args);
829       if (arch != NULL)
830         set_arch (arch);
831       else
832         printf_unfiltered ("Architecture `%s' not reconized.\n", args);
833     }
834 }
835
836 /* Called if the user enters ``info architecture'' without an argument. */
837 static void info_architecture PARAMS ((char *, int));
838 static void
839 info_architecture (args, from_tty)
840      char *args;
841      int from_tty;
842 {
843   enum bfd_architecture a;
844   /* start-sanitize-carp start-sanitize-vr4xxx */
845   if (GDB_MULTI_ARCH)
846     {
847       if (gdbarch_init_registrary != NULL)
848         {
849           struct gdbarch_init_registration *rego;
850           printf_filtered ("Available architectures are:\n");
851           for (rego = gdbarch_init_registrary;
852                rego != NULL;
853                rego = rego->next)
854             {
855               const struct bfd_arch_info *ap;
856               ap = bfd_lookup_arch (rego->bfd_architecture, 0);
857               if (ap != NULL)
858                 {
859                   do
860                     {
861                       printf_filtered (" %s", ap->printable_name);
862                       ap = ap->next;
863                     }
864                   while (ap != NULL);
865                   printf_filtered ("\n");
866                 }
867             }
868         }
869       else
870         {
871           printf_filtered ("There are no available architectures.\n");
872         }
873       return;
874     }
875   /* end-sanitize-carp end-sanitize-vr4xxx */
876   printf_filtered ("Available architectures are:\n");
877   for (a = bfd_arch_obscure + 1; a < bfd_arch_last; a++)
878     {
879       const struct bfd_arch_info *ap = bfd_lookup_arch (a, 0);
880       if (ap != NULL)
881         {
882           do
883             {
884               printf_filtered (" %s", ap->printable_name);
885               ap = ap->next;
886             }
887           while (ap != NULL);
888           printf_filtered ("\n");
889         }
890     }
891 }
892
893 /* Set the architecture from arch/machine */
894 void
895 set_architecture_from_arch_mach (arch, mach)
896      enum bfd_architecture arch;
897      unsigned long mach;
898 {
899   const struct bfd_arch_info *wanted = bfd_lookup_arch (arch, mach);
900   if (wanted != NULL)
901     set_arch (wanted);
902   else
903     fatal ("hardwired architecture/machine not reconized");
904 }
905
906 /* Set the architecture from a BFD */
907 static void set_architecture_from_file PARAMS ((bfd *));
908 static void
909 set_architecture_from_file (abfd)
910      bfd *abfd;
911 {
912   const struct bfd_arch_info *wanted = bfd_get_arch_info (abfd);
913   if (target_architecture_auto)
914     {
915       if (target_architecture_hook != NULL
916           && !target_architecture_hook (wanted))
917         warning ("Target may not support %s architecture",
918                  wanted->printable_name);
919       target_architecture = wanted;
920     }
921   else if (wanted != target_architecture)
922     {
923       warning ("%s architecture file may be incompatible with %s target.",
924                wanted->printable_name,
925                target_architecture->printable_name);
926     }
927 }
928
929
930
931 /* Disassembler */
932
933 /* Pointer to the target-dependent disassembly function.  */
934 int (*tm_print_insn) PARAMS ((bfd_vma, disassemble_info *));
935 disassemble_info tm_print_insn_info;
936
937
938
939 /* Set the dynamic target-system-dependant parameters (architecture,
940    byte-order) using information found in the BFD */
941
942 void
943 set_gdbarch_from_file (abfd)
944      bfd *abfd;
945 {
946   /* start-sanitize-carp start-sanitize-vr4xxx */
947   if (GDB_MULTI_ARCH)
948     {
949       struct gdbarch_info info;
950       memset (&info, 0, sizeof info);
951       info.abfd = abfd;
952       gdbarch_update (info);
953       return;
954     }
955   /* end-sanitize-carp end-sanitize-vr4xxx */
956   set_architecture_from_file (abfd);
957   set_endian_from_file (abfd);
958 }
959
960 /* start-sanitize-carp start-sanitize-vr4xxx */
961
962 /* The default architecture uses host values (for want of a better
963    choice). */
964
965 struct gdbarch default_gdbarch = {
966   /* basic architecture information */
967   &bfd_default_arch_struct,
968   TARGET_BYTE_ORDER_DEFAULT,
969   /* target specific vector */
970   NULL,
971   /*per-architecture data-pointers and swap regions */
972   0, NULL, NULL,
973   /* Multi-arch values */
974   8 * sizeof (long), /* long */
975   8 * sizeof (LONGEST), /* long long */
976   8 * sizeof (void*), /* ptr */
977 };
978 struct gdbarch *current_gdbarch = &default_gdbarch;
979
980 /* end-sanitize-carp end-sanitize-vr4xxx */
981
982 extern void _initialize_gdbarch PARAMS ((void));
983 void
984 _initialize_gdbarch ()
985 {
986   add_prefix_cmd ("endian", class_support, set_endian,
987                   "Set endianness of target.",
988                   &endianlist, "set endian ", 0, &setlist);
989   add_cmd ("big", class_support, set_endian_big,
990            "Set target as being big endian.", &endianlist);
991   add_cmd ("little", class_support, set_endian_little,
992            "Set target as being little endian.", &endianlist);
993   add_cmd ("auto", class_support, set_endian_auto,
994            "Select target endianness automatically.", &endianlist);
995   add_cmd ("endian", class_support, show_endian,
996            "Show endianness of target.", &showlist);
997
998   add_cmd ("architecture", class_support, set_architecture,
999            "Set architecture of target.", &setlist);
1000   add_alias_cmd ("processor", "architecture", class_support, 1, &setlist);
1001   add_cmd ("architecture", class_support, show_architecture,
1002            "Show architecture of target.", &showlist);
1003   add_cmd ("architecture", class_support, info_architecture,
1004            "List supported target architectures", &infolist);
1005
1006   INIT_DISASSEMBLE_INFO_NO_ARCH (tm_print_insn_info, gdb_stdout, (fprintf_ftype)fprintf_filtered);
1007   tm_print_insn_info.flavour = bfd_target_unknown_flavour;
1008   tm_print_insn_info.read_memory_func = dis_asm_read_memory;
1009   tm_print_insn_info.memory_error_func = dis_asm_memory_error;
1010   tm_print_insn_info.print_address_func = dis_asm_print_address;
1011
1012 #ifdef MAINTENANCE_CMDS
1013   add_show_from_set (add_set_cmd ("archdebug",
1014                                   class_maintenance,
1015                                   var_zinteger,
1016                                   (char *)&gdbarch_debug,
1017                                   "Set architecture debugging.\n\
1018 When non-zero, architecture debugging is enabled.", &setlist),
1019                      &showlist);
1020 #endif
1021 }