Merge from vendor branch GCC:
[dragonfly.git] / contrib / gdb / 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
26 /* Non-zero if we want to trace architecture code.  */
27
28 #ifndef GDBARCH_DEBUG
29 #define GDBARCH_DEBUG 0
30 #endif
31 int gdbarch_debug = GDBARCH_DEBUG;
32
33
34 /* Functions to manipulate the endianness of the target.  */
35
36 #ifdef TARGET_BYTE_ORDER_SELECTABLE
37 /* compat - Catch old targets that expect a selectable byte-order to
38    default to BIG_ENDIAN */
39 #ifndef TARGET_BYTE_ORDER_DEFAULT
40 #define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN
41 #endif
42 #endif
43 #ifndef TARGET_BYTE_ORDER_DEFAULT
44 /* compat - Catch old non byte-order selectable targets that do not
45    define TARGET_BYTE_ORDER_DEFAULT and instead expect
46    TARGET_BYTE_ORDER to be used as the default.  For targets that
47    defined neither TARGET_BYTE_ORDER nor TARGET_BYTE_ORDER_DEFAULT the
48    below will get a strange compiler warning. */
49 #define TARGET_BYTE_ORDER_DEFAULT TARGET_BYTE_ORDER
50 #endif
51 int target_byte_order = TARGET_BYTE_ORDER_DEFAULT;
52 int target_byte_order_auto = 1;
53
54 /* Chain containing the \"set endian\" commands.  */
55 static struct cmd_list_element *endianlist = NULL;
56
57 /* Called by ``show endian''.  */
58 static void show_endian PARAMS ((char *, int));
59 static void
60 show_endian (args, from_tty)
61      char *args;
62      int from_tty;
63 {
64   char *msg =
65     (TARGET_BYTE_ORDER_AUTO
66      ? "The target endianness is set automatically (currently %s endian)\n"
67      : "The target is assumed to be %s endian\n");
68   printf_unfiltered (msg, (TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little"));
69 }
70
71 /* Called if the user enters ``set endian'' without an argument.  */
72 static void set_endian PARAMS ((char *, int));
73 static void
74 set_endian (args, from_tty)
75      char *args;
76      int from_tty;
77 {
78   printf_unfiltered ("\"set endian\" must be followed by \"auto\", \"big\" or \"little\".\n");
79   show_endian (args, from_tty);
80 }
81
82 /* Called by ``set endian big''.  */
83 static void set_endian_big PARAMS ((char *, int));
84 static void
85 set_endian_big (args, from_tty)
86      char *args;
87      int from_tty;
88 {
89   if (TARGET_BYTE_ORDER_SELECTABLE_P)
90     {
91       target_byte_order = BIG_ENDIAN;
92       target_byte_order_auto = 0;
93     }
94   else
95     {
96       printf_unfiltered ("Byte order is not selectable.");
97       show_endian (args, from_tty);
98     }
99 }
100
101 /* Called by ``set endian little''.  */
102 static void set_endian_little PARAMS ((char *, int));
103 static void
104 set_endian_little (args, from_tty)
105      char *args;
106      int from_tty;
107 {
108   if (TARGET_BYTE_ORDER_SELECTABLE_P)
109     {
110       target_byte_order = LITTLE_ENDIAN;
111       target_byte_order_auto = 0;
112     }
113   else
114     {
115       printf_unfiltered ("Byte order is not selectable.");
116       show_endian (args, from_tty);
117     }
118 }
119
120 /* Called by ``set endian auto''.  */
121 static void set_endian_auto PARAMS ((char *, int));
122 static void
123 set_endian_auto (args, from_tty)
124      char *args;
125      int from_tty;
126 {
127   if (TARGET_BYTE_ORDER_SELECTABLE_P)
128     {
129       target_byte_order_auto = 1;
130     }
131   else
132     {
133       printf_unfiltered ("Byte order is not selectable.");
134       show_endian (args, from_tty);
135     }
136 }
137
138 /* Set the endianness from a BFD.  */
139 static void set_endian_from_file PARAMS ((bfd *));
140 static void
141 set_endian_from_file (abfd)
142      bfd *abfd;
143 {
144   if (TARGET_BYTE_ORDER_SELECTABLE_P)
145     {
146       int want;
147       
148       if (bfd_big_endian (abfd))
149         want = BIG_ENDIAN;
150       else
151         want = LITTLE_ENDIAN;
152       if (TARGET_BYTE_ORDER_AUTO)
153         target_byte_order = want;
154       else if (TARGET_BYTE_ORDER != want)
155         warning ("%s endian file does not match %s endian target.",
156                  want == BIG_ENDIAN ? "big" : "little",
157                  TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little");
158     }
159   else
160     {
161       if (bfd_big_endian (abfd)
162           ? TARGET_BYTE_ORDER != BIG_ENDIAN
163           : TARGET_BYTE_ORDER == BIG_ENDIAN)
164         warning ("%s endian file does not match %s endian target.",
165                  bfd_big_endian (abfd) ? "big" : "little",
166                  TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little");
167     }
168 }
169
170
171
172 /* Functions to manipulate the architecture of the target */
173
174 int target_architecture_auto = 1;
175 extern const struct bfd_arch_info bfd_default_arch_struct;
176 const struct bfd_arch_info *target_architecture = &bfd_default_arch_struct;
177 int (*target_architecture_hook) PARAMS ((const struct bfd_arch_info *ap));
178
179 /* Do the real work of changing the current architecture */
180 static void
181 set_arch (arch)
182      const struct bfd_arch_info *arch;
183 {
184   /* FIXME: Is it compatible with gdb? */
185   /* Check with the target on the setting */
186   if (target_architecture_hook != NULL
187       && !target_architecture_hook (arch))
188     printf_unfiltered ("Target does not support `%s' architecture.\n",
189                        arch->printable_name);
190   else
191     {
192       target_architecture_auto = 0;
193       target_architecture = arch;
194     }
195 }
196
197 /* Called if the user enters ``show architecture'' without an argument. */
198 static void show_architecture PARAMS ((char *, int));
199 static void
200 show_architecture (args, from_tty)
201      char *args;
202      int from_tty;
203 {
204   const char *arch;
205   arch = TARGET_ARCHITECTURE->printable_name;
206   if (target_architecture_auto)
207     printf_filtered ("The target architecture is set automatically (currently %s)\n", arch);
208   else
209     printf_filtered ("The target architecture is assumed to be %s\n", arch);
210 }
211
212 /* Called if the user enters ``set architecture'' with or without an
213    argument. */
214 static void set_architecture PARAMS ((char *, int));
215 static void
216 set_architecture (args, from_tty)
217      char *args;
218      int from_tty;
219 {
220   if (args == NULL)
221     {
222       printf_unfiltered ("\"set architecture\" must be followed by \"auto\" or an architecture name.\n");
223     }
224   else if (strcmp (args, "auto") == 0)
225     {
226       target_architecture_auto = 1;
227     }
228   else
229     {
230       const struct bfd_arch_info *arch = bfd_scan_arch (args);
231       if (arch != NULL)
232         set_arch (arch);
233       else
234         printf_unfiltered ("Architecture `%s' not reconized.\n", args);
235     }
236 }
237
238 /* Called if the user enters ``info architecture'' without an argument. */
239 static void info_architecture PARAMS ((char *, int));
240 static void
241 info_architecture (args, from_tty)
242      char *args;
243      int from_tty;
244 {
245   enum bfd_architecture a;
246   printf_filtered ("Available architectures are:\n");
247   for (a = bfd_arch_obscure + 1; a < bfd_arch_last; a++)
248     {
249       const struct bfd_arch_info *ap = bfd_lookup_arch (a, 0);
250       if (ap != NULL)
251         {
252           do
253             {
254               printf_filtered (" %s", ap->printable_name);
255               ap = ap->next;
256             }
257           while (ap != NULL);
258           printf_filtered ("\n");
259         }
260     }
261 }
262
263 /* Set the architecture from arch/machine */
264 void
265 set_architecture_from_arch_mach (arch, mach)
266      enum bfd_architecture arch;
267      unsigned long mach;
268 {
269   const struct bfd_arch_info *wanted = bfd_lookup_arch (arch, mach);
270   if (wanted != NULL)
271     set_arch (wanted);
272   else
273     fatal ("hardwired architecture/machine not reconized");
274 }
275
276 /* Set the architecture from a BFD */
277 static void set_architecture_from_file PARAMS ((bfd *));
278 static void
279 set_architecture_from_file (abfd)
280      bfd *abfd;
281 {
282   const struct bfd_arch_info *wanted = bfd_get_arch_info (abfd);
283   if (target_architecture_auto)
284     {
285       if (target_architecture_hook != NULL
286           && !target_architecture_hook (wanted))
287         warning ("Target may not support %s architecture",
288                  wanted->printable_name);
289       target_architecture = wanted;
290     }
291   else if (wanted != target_architecture)
292     {
293       warning ("%s architecture file may be incompatible with %s target.",
294                wanted->printable_name,
295                target_architecture->printable_name);
296     }
297 }
298
299
300
301 /* Disassembler */
302
303 /* Pointer to the target-dependent disassembly function.  */
304 int (*tm_print_insn) PARAMS ((bfd_vma, disassemble_info *));
305 disassemble_info tm_print_insn_info;
306
307
308
309 /* Set the dynamic target-system-dependant parameters (architecture,
310    byte-order) using information found in the BFD */
311
312 void
313 set_gdbarch_from_file (abfd)
314      bfd *abfd;
315 {
316   set_architecture_from_file (abfd);
317   set_endian_from_file (abfd);
318 }
319
320
321 extern void _initialize_gdbarch PARAMS ((void));
322 void
323 _initialize_gdbarch ()
324 {
325   add_prefix_cmd ("endian", class_support, set_endian,
326                   "Set endianness of target.",
327                   &endianlist, "set endian ", 0, &setlist);
328   add_cmd ("big", class_support, set_endian_big,
329            "Set target as being big endian.", &endianlist);
330   add_cmd ("little", class_support, set_endian_little,
331            "Set target as being little endian.", &endianlist);
332   add_cmd ("auto", class_support, set_endian_auto,
333            "Select target endianness automatically.", &endianlist);
334   add_cmd ("endian", class_support, show_endian,
335            "Show endianness of target.", &showlist);
336
337   add_cmd ("architecture", class_support, set_architecture,
338            "Set architecture of target.", &setlist);
339   add_alias_cmd ("processor", "architecture", class_support, 1, &setlist);
340   add_cmd ("architecture", class_support, show_architecture,
341            "Show architecture of target.", &showlist);
342   add_cmd ("architecture", class_support, info_architecture,
343            "List supported target architectures", &infolist);
344
345   INIT_DISASSEMBLE_INFO_NO_ARCH (tm_print_insn_info, gdb_stdout, (fprintf_ftype)fprintf_filtered);
346   tm_print_insn_info.flavour = bfd_target_unknown_flavour;
347   tm_print_insn_info.read_memory_func = dis_asm_read_memory;
348   tm_print_insn_info.memory_error_func = dis_asm_memory_error;
349   tm_print_insn_info.print_address_func = dis_asm_print_address;
350
351 #ifdef MAINTENANCE_CMDS
352   add_show_from_set (add_set_cmd ("archdebug",
353                                   class_maintenance,
354                                   var_zinteger,
355                                   (char *)&gdbarch_debug,
356                                   "Set architecture debugging.\n\
357 When non-zero, architecture debugging is enabled.", &setlist),
358                      &showlist);
359 #endif
360 }