Added GDB_OSABI_QNXNTO.
[platform/upstream/binutils.git] / gdb / osabi.c
1 /* OS ABI variant handling for GDB.
2    Copyright 2001, 2002, 2003 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 #include "gdb_assert.h"
24 #include "gdb_string.h"
25
26 #include "osabi.h"
27 #include "arch-utils.h"
28 #include "gdbcmd.h"
29 #include "command.h"
30
31 #include "elf-bfd.h"
32
33 #ifndef GDB_OSABI_DEFAULT
34 #define GDB_OSABI_DEFAULT GDB_OSABI_UNKNOWN
35 #endif
36
37 /* State for the "set osabi" command.  */
38 static enum { osabi_auto, osabi_default, osabi_user } user_osabi_state;
39 static enum gdb_osabi user_selected_osabi;
40 static const char *gdb_osabi_available_names[GDB_OSABI_INVALID + 3] = {
41   "auto",
42   "default",
43   "none",
44   NULL
45 };
46 static const char *set_osabi_string;
47
48 /* This table matches the indices assigned to enum gdb_osabi.  Keep
49    them in sync.  */
50 static const char * const gdb_osabi_names[] =
51 {
52   "none",
53
54   "SVR4",
55   "GNU/Hurd",
56   "Solaris",
57   "OSF/1",
58   "GNU/Linux",
59   "FreeBSD a.out",
60   "FreeBSD ELF",
61   "NetBSD a.out",
62   "NetBSD ELF",
63   "Windows CE",
64   "DJGPP",
65   "NetWare",
66   "Irix",
67   "LynxOS",
68   "Interix",
69   "HP/UX ELF",
70   "HP/UX SOM",
71
72   "ARM EABI v1",
73   "ARM EABI v2",
74   "ARM APCS",
75   "QNX Neutrino",
76
77   "<invalid>"
78 };
79
80 const char *
81 gdbarch_osabi_name (enum gdb_osabi osabi)
82 {
83   if (osabi >= GDB_OSABI_UNKNOWN && osabi < GDB_OSABI_INVALID)
84     return gdb_osabi_names[osabi];
85
86   return gdb_osabi_names[GDB_OSABI_INVALID];
87 }
88
89 /* Handler for a given architecture/OS ABI pair.  There should be only
90    one handler for a given OS ABI each architecture family.  */
91 struct gdb_osabi_handler  
92 {
93   struct gdb_osabi_handler *next;
94   const struct bfd_arch_info *arch_info;
95   enum gdb_osabi osabi;
96   void (*init_osabi)(struct gdbarch_info, struct gdbarch *);
97 };
98
99 static struct gdb_osabi_handler *gdb_osabi_handler_list;
100
101 void
102 gdbarch_register_osabi (enum bfd_architecture arch, unsigned long machine,
103                         enum gdb_osabi osabi,
104                         void (*init_osabi)(struct gdbarch_info,
105                                            struct gdbarch *))
106 {
107   struct gdb_osabi_handler **handler_p;
108   const struct bfd_arch_info *arch_info = bfd_lookup_arch (arch, machine);
109   const char **name_ptr;
110
111   /* Registering an OS ABI handler for "unknown" is not allowed.  */
112   if (osabi == GDB_OSABI_UNKNOWN)
113     {
114       internal_error
115         (__FILE__, __LINE__,
116          "gdbarch_register_osabi: An attempt to register a handler for "
117          "OS ABI \"%s\" for architecture %s was made.  The handler will "
118          "not be registered",
119          gdbarch_osabi_name (osabi),
120          bfd_printable_arch_mach (arch, machine));
121       return;
122     }
123
124   gdb_assert (arch_info);
125
126   for (handler_p = &gdb_osabi_handler_list; *handler_p != NULL;
127        handler_p = &(*handler_p)->next)
128     {
129       if ((*handler_p)->arch_info == arch_info
130           && (*handler_p)->osabi == osabi)
131         {
132           internal_error
133             (__FILE__, __LINE__,
134              "gdbarch_register_osabi: A handler for OS ABI \"%s\" "
135              "has already been registered for architecture %s",
136              gdbarch_osabi_name (osabi),
137              arch_info->printable_name);
138           /* If user wants to continue, override previous definition.  */
139           (*handler_p)->init_osabi = init_osabi;
140           return;
141         }
142     }
143
144   (*handler_p)
145     = (struct gdb_osabi_handler *) xmalloc (sizeof (struct gdb_osabi_handler));
146   (*handler_p)->next = NULL;
147   (*handler_p)->arch_info = arch_info;
148   (*handler_p)->osabi = osabi;
149   (*handler_p)->init_osabi = init_osabi;
150
151   /* Add this OS ABI to the list of enum values for "set osabi", if it isn't
152      already there.  */
153   for (name_ptr = gdb_osabi_available_names; *name_ptr; name_ptr ++)
154     {
155       if (*name_ptr == gdbarch_osabi_name (osabi))
156         return;
157     }
158   *name_ptr++ = gdbarch_osabi_name (osabi);
159   *name_ptr = NULL;
160 }
161 \f
162
163 /* Sniffer to find the OS ABI for a given file's architecture and flavour. 
164    It is legal to have multiple sniffers for each arch/flavour pair, to
165    disambiguate one OS's a.out from another, for example.  The first sniffer
166    to return something other than GDB_OSABI_UNKNOWN wins, so a sniffer should
167    be careful to claim a file only if it knows for sure what it is.  */
168 struct gdb_osabi_sniffer
169 {
170   struct gdb_osabi_sniffer *next;
171   enum bfd_architecture arch;   /* bfd_arch_unknown == wildcard */
172   enum bfd_flavour flavour;
173   enum gdb_osabi (*sniffer)(bfd *);
174 };
175
176 static struct gdb_osabi_sniffer *gdb_osabi_sniffer_list;
177
178 void
179 gdbarch_register_osabi_sniffer (enum bfd_architecture arch,
180                                 enum bfd_flavour flavour,
181                                 enum gdb_osabi (*sniffer_fn)(bfd *))
182 {
183   struct gdb_osabi_sniffer *sniffer;
184
185   sniffer =
186     (struct gdb_osabi_sniffer *) xmalloc (sizeof (struct gdb_osabi_sniffer));
187   sniffer->arch = arch;
188   sniffer->flavour = flavour;
189   sniffer->sniffer = sniffer_fn;
190
191   sniffer->next = gdb_osabi_sniffer_list;
192   gdb_osabi_sniffer_list = sniffer;
193 }
194 \f
195
196 enum gdb_osabi
197 gdbarch_lookup_osabi (bfd *abfd)
198 {
199   struct gdb_osabi_sniffer *sniffer;
200   enum gdb_osabi osabi, match;
201   int match_specific;
202
203   /* If we aren't in "auto" mode, return the specified OS ABI.  */
204   if (user_osabi_state == osabi_user)
205     return user_selected_osabi;
206
207   /* If we don't have a binary, return the default OS ABI (if set) or
208      an inconclusive result (otherwise).  */
209   if (abfd == NULL) 
210     {
211       if (GDB_OSABI_DEFAULT != GDB_OSABI_UNKNOWN)
212         return GDB_OSABI_DEFAULT;
213       else
214         return GDB_OSABI_UNINITIALIZED;
215     }
216
217   match = GDB_OSABI_UNKNOWN;
218   match_specific = 0;
219
220   for (sniffer = gdb_osabi_sniffer_list; sniffer != NULL;
221        sniffer = sniffer->next)
222     {
223       if ((sniffer->arch == bfd_arch_unknown /* wildcard */
224            || sniffer->arch == bfd_get_arch (abfd))
225           && sniffer->flavour == bfd_get_flavour (abfd))
226         {
227           osabi = (*sniffer->sniffer) (abfd);
228           if (osabi < GDB_OSABI_UNKNOWN || osabi >= GDB_OSABI_INVALID)
229             {
230               internal_error
231                 (__FILE__, __LINE__,
232                  "gdbarch_lookup_osabi: invalid OS ABI (%d) from sniffer "
233                  "for architecture %s flavour %d",
234                  (int) osabi,
235                  bfd_printable_arch_mach (bfd_get_arch (abfd), 0),
236                  (int) bfd_get_flavour (abfd));
237             }
238           else if (osabi != GDB_OSABI_UNKNOWN)
239             {
240               /* A specific sniffer always overrides a generic sniffer.
241                  Croak on multiple match if the two matches are of the
242                  same class.  If the user wishes to continue, we'll use
243                  the first match.  */
244               if (match != GDB_OSABI_UNKNOWN)
245                 {
246                   if ((match_specific && sniffer->arch != bfd_arch_unknown)
247                    || (!match_specific && sniffer->arch == bfd_arch_unknown))
248                     {
249                       internal_error
250                         (__FILE__, __LINE__,
251                          "gdbarch_lookup_osabi: multiple %sspecific OS ABI "
252                          "match for architecture %s flavour %d: first "
253                          "match \"%s\", second match \"%s\"",
254                          match_specific ? "" : "non-",
255                          bfd_printable_arch_mach (bfd_get_arch (abfd), 0),
256                          (int) bfd_get_flavour (abfd),
257                          gdbarch_osabi_name (match),
258                          gdbarch_osabi_name (osabi));
259                     }
260                   else if (sniffer->arch != bfd_arch_unknown)
261                     {
262                       match = osabi;
263                       match_specific = 1;
264                     }
265                 }
266               else
267                 {
268                   match = osabi;
269                   if (sniffer->arch != bfd_arch_unknown)
270                     match_specific = 1;
271                 }
272             }
273         }
274     }
275
276   /* If we didn't find a match, but a default was specified at configure
277      time, return the default.  */
278   if (GDB_OSABI_DEFAULT != GDB_OSABI_UNKNOWN && match == GDB_OSABI_UNKNOWN)
279     return GDB_OSABI_DEFAULT;
280   else
281     return match;
282 }
283
284 void
285 gdbarch_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
286 {
287   const struct bfd_arch_info *arch_info = gdbarch_bfd_arch_info (gdbarch);
288   const struct bfd_arch_info *compatible;
289   struct gdb_osabi_handler *handler;
290
291   if (info.osabi == GDB_OSABI_UNKNOWN)
292     {
293       /* Don't complain about an unknown OSABI.  Assume the user knows
294          what they are doing.  */
295       return;
296     }
297
298   for (handler = gdb_osabi_handler_list; handler != NULL;
299        handler = handler->next)
300     {
301       if (handler->osabi != info.osabi)
302         continue;
303
304       /* Check whether the machine type and architecture of the
305          handler are compatible with the desired machine type and
306          architecture.
307
308          NOTE: kettenis/20021027: There may be more than one machine
309          type that is compatible with the desired machine type.  Right
310          now we simply return the first match, which is fine for now.
311          However, we might want to do something smarter in the future.  */
312       compatible = arch_info->compatible (arch_info, handler->arch_info);
313       if (compatible == handler->arch_info)
314         {
315           (*handler->init_osabi) (info, gdbarch);
316           return;
317         }
318     }
319
320   /* We assume that if GDB_MULTI_ARCH is less than GDB_MULTI_ARCH_TM
321      that an ABI variant can be supported by overriding definitions in
322      the tm-file.  */
323   if (GDB_MULTI_ARCH > GDB_MULTI_ARCH_PARTIAL)
324     fprintf_filtered
325       (gdb_stderr,
326        "A handler for the OS ABI \"%s\" is not built into this "
327        "configuration of GDB.  "
328        "Attempting to continue with the default %s settings",
329        gdbarch_osabi_name (info.osabi),
330        bfd_printable_arch_mach (arch_info->arch, arch_info->mach));
331 }
332 \f
333
334 /* Generic sniffer for ELF flavoured files.  */
335
336 void
337 generic_elf_osabi_sniff_abi_tag_sections (bfd *abfd, asection *sect, void *obj)
338 {
339   enum gdb_osabi *os_ident_ptr = obj;
340   const char *name;
341   unsigned int sectsize;
342
343   name = bfd_get_section_name (abfd, sect);
344   sectsize = bfd_section_size (abfd, sect);
345
346   /* .note.ABI-tag notes, used by GNU/Linux and FreeBSD.  */
347   if (strcmp (name, ".note.ABI-tag") == 0 && sectsize > 0)
348     {
349       unsigned int name_length, data_length, note_type;
350       char *note;
351
352       /* If the section is larger than this, it's probably not what we are
353          looking for.  */
354       if (sectsize > 128)
355         sectsize = 128;
356
357       note = alloca (sectsize);
358
359       bfd_get_section_contents (abfd, sect, note,
360                                 (file_ptr) 0, (bfd_size_type) sectsize);
361
362       name_length = bfd_h_get_32 (abfd, note);
363       data_length = bfd_h_get_32 (abfd, note + 4);
364       note_type   = bfd_h_get_32 (abfd, note + 8);
365
366       if (name_length == 4 && data_length == 16 && note_type == NT_GNU_ABI_TAG
367           && strcmp (note + 12, "GNU") == 0)
368         {
369           int os_number = bfd_h_get_32 (abfd, note + 16);
370
371           switch (os_number)
372             {
373             case GNU_ABI_TAG_LINUX:
374               *os_ident_ptr = GDB_OSABI_LINUX;
375               break;
376
377             case GNU_ABI_TAG_HURD:
378               *os_ident_ptr = GDB_OSABI_HURD;
379               break;
380
381             case GNU_ABI_TAG_SOLARIS:
382               *os_ident_ptr = GDB_OSABI_SOLARIS;
383               break;
384
385             default:
386               internal_error
387                 (__FILE__, __LINE__,
388                  "generic_elf_osabi_sniff_abi_tag_sections: unknown OS number %d",
389                  os_number);
390             }
391           return;
392         }
393       else if (name_length == 8 && data_length == 4
394                && note_type == NT_FREEBSD_ABI_TAG
395                && strcmp (note + 12, "FreeBSD") == 0)
396         {
397           /* XXX Should we check the version here?  Probably not
398              necessary yet.  */
399           *os_ident_ptr = GDB_OSABI_FREEBSD_ELF;
400         }
401       return;
402     }
403
404   /* .note.netbsd.ident notes, used by NetBSD.  */
405   if (strcmp (name, ".note.netbsd.ident") == 0 && sectsize > 0)
406     {
407       unsigned int name_length, data_length, note_type;
408       char *note;
409
410       /* If the section is larger than this, it's probably not what we are
411          looking for.  */
412       if (sectsize > 128) 
413         sectsize = 128;
414
415       note = alloca (sectsize);
416
417       bfd_get_section_contents (abfd, sect, note,
418                                 (file_ptr) 0, (bfd_size_type) sectsize);
419       
420       name_length = bfd_h_get_32 (abfd, note);
421       data_length = bfd_h_get_32 (abfd, note + 4);
422       note_type   = bfd_h_get_32 (abfd, note + 8);
423
424       if (name_length == 7 && data_length == 4 && note_type == NT_NETBSD_IDENT
425           && strcmp (note + 12, "NetBSD") == 0)
426         {
427           /* XXX Should we check the version here?  Probably not
428              necessary yet.  */
429           *os_ident_ptr = GDB_OSABI_NETBSD_ELF;
430         }
431       return;
432     }
433 }
434
435 static enum gdb_osabi
436 generic_elf_osabi_sniffer (bfd *abfd)
437 {
438   unsigned int elfosabi;
439   enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
440
441   elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
442
443   switch (elfosabi)
444     {
445     case ELFOSABI_NONE:
446       /* When elfosabi is ELFOSABI_NONE (0), then the ELF structures in the
447          file are conforming to the base specification for that machine
448          (there are no OS-specific extensions).  In order to determine the
449          real OS in use we must look for OS notes that have been added.  */
450       bfd_map_over_sections (abfd,
451                              generic_elf_osabi_sniff_abi_tag_sections,
452                              &osabi);
453       break;
454
455     case ELFOSABI_FREEBSD:
456       osabi = GDB_OSABI_FREEBSD_ELF;
457       break;
458
459     case ELFOSABI_NETBSD:
460       osabi = GDB_OSABI_NETBSD_ELF;
461       break;
462
463     case ELFOSABI_LINUX:
464       osabi = GDB_OSABI_LINUX;
465       break;
466
467     case ELFOSABI_HURD:
468       osabi = GDB_OSABI_HURD;
469       break;
470
471     case ELFOSABI_SOLARIS:
472       osabi = GDB_OSABI_SOLARIS;
473       break;
474
475     case ELFOSABI_HPUX:
476       osabi = GDB_OSABI_HPUX_ELF;
477       break;
478     }
479
480   if (osabi == GDB_OSABI_UNKNOWN)
481     {
482       /* The FreeBSD folks have been naughty; they stored the string
483          "FreeBSD" in the padding of the e_ident field of the ELF
484          header to "brand" their ELF binaries in FreeBSD 3.x.  */
485       if (strcmp (&elf_elfheader (abfd)->e_ident[8], "FreeBSD") == 0)
486         osabi = GDB_OSABI_FREEBSD_ELF;
487     }
488
489   return osabi;
490 }
491 \f
492 static void
493 set_osabi (char *args, int from_tty, struct cmd_list_element *c)
494 {
495   struct gdbarch_info info;
496
497   if (strcmp (set_osabi_string, "auto") == 0)
498     user_osabi_state = osabi_auto;
499   else if (strcmp (set_osabi_string, "default") == 0)
500     {
501       user_selected_osabi = GDB_OSABI_DEFAULT;
502       user_osabi_state = osabi_user;
503     }
504   else if (strcmp (set_osabi_string, "none") == 0)
505     {
506       user_selected_osabi = GDB_OSABI_UNKNOWN;
507       user_osabi_state = osabi_user;
508     }
509   else
510     {
511       int i;
512       for (i = 1; i < GDB_OSABI_INVALID; i++)
513         if (strcmp (set_osabi_string, gdbarch_osabi_name (i)) == 0)
514           {
515             user_selected_osabi = i;
516             user_osabi_state = osabi_user;
517             break;
518           }
519       if (i == GDB_OSABI_INVALID)
520         internal_error (__FILE__, __LINE__,
521                         "Invalid OS ABI \"%s\" passed to command handler.",
522                         set_osabi_string);
523     }
524
525   /* NOTE: At some point (true multiple architectures) we'll need to be more
526      graceful here.  */
527   gdbarch_info_init (&info);
528   if (! gdbarch_update_p (info))
529     internal_error (__FILE__, __LINE__, "Updating OS ABI failed.");
530 }
531
532 void
533 show_osabi (char *args, int from_tty)
534 {
535   if (user_osabi_state == osabi_auto)
536     printf_filtered ("The current OS ABI is \"auto\" (currently \"%s\").\n",
537                      gdbarch_osabi_name (gdbarch_osabi (current_gdbarch)));
538   else
539     printf_filtered ("The current OS ABI is \"%s\".\n",
540                      gdbarch_osabi_name (user_selected_osabi));
541
542   if (GDB_OSABI_DEFAULT != GDB_OSABI_UNKNOWN)
543     printf_filtered ("The default OS ABI is \"%s\".\n",
544                      gdbarch_osabi_name (GDB_OSABI_DEFAULT));
545 }
546 \f
547 void
548 _initialize_gdb_osabi (void)
549 {
550   struct cmd_list_element *c;
551
552   if (strcmp (gdb_osabi_names[GDB_OSABI_INVALID], "<invalid>") != 0)
553     internal_error
554       (__FILE__, __LINE__,
555        "_initialize_gdb_osabi: gdb_osabi_names[] is inconsistent");
556
557   /* Register a generic sniffer for ELF flavoured files.  */
558   gdbarch_register_osabi_sniffer (bfd_arch_unknown,
559                                   bfd_target_elf_flavour,
560                                   generic_elf_osabi_sniffer);
561
562   if (!GDB_MULTI_ARCH)
563     return;
564
565   /* Register the "set osabi" command.  */
566   c = add_set_enum_cmd ("osabi", class_support, gdb_osabi_available_names,
567                         &set_osabi_string, "Set OS ABI of target.", &setlist);
568
569   set_cmd_sfunc (c, set_osabi);
570   add_cmd ("osabi", class_support, show_osabi, "Show OS/ABI of target.",
571            &showlist);
572   user_osabi_state = osabi_auto;
573 }