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