fix memory errors with demangled name hash
[platform/upstream/binutils.git] / gdb / osabi.c
1 /* OS ABI variant handling for GDB.
2
3    Copyright (C) 2001-2014 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21
22 #include "gdb_assert.h"
23 #include <string.h>
24
25 #include "osabi.h"
26 #include "arch-utils.h"
27 #include "gdbcmd.h"
28 #include "command.h"
29
30 #include "elf-bfd.h"
31
32 #ifndef GDB_OSABI_DEFAULT
33 #define GDB_OSABI_DEFAULT GDB_OSABI_UNKNOWN
34 #endif
35
36 /* State for the "set osabi" command.  */
37 static enum { osabi_auto, osabi_default, osabi_user } user_osabi_state;
38 static enum gdb_osabi user_selected_osabi;
39 static const char *gdb_osabi_available_names[GDB_OSABI_INVALID + 3] = {
40   "auto",
41   "default",
42   "none",
43   NULL
44 };
45 static const char *set_osabi_string;
46
47 /* This table matches the indices assigned to enum gdb_osabi.  Keep
48    them in sync.  */
49 static const char * const gdb_osabi_names[] =
50 {
51   "none",
52
53   "SVR4",
54   "GNU/Hurd",
55   "Solaris",
56   "OSF/1",
57   "GNU/Linux",
58   "FreeBSD a.out",
59   "FreeBSD ELF",
60   "NetBSD a.out",
61   "NetBSD ELF",
62   "OpenBSD ELF",
63   "Windows CE",
64   "DJGPP",
65   "Irix",
66   "HP/UX ELF",
67   "HP/UX SOM",
68   "QNX Neutrino",
69   "Cygwin",
70   "AIX",
71   "DICOS",
72   "Darwin",
73   "Symbian",
74   "OpenVMS",
75   "LynxOS178",
76   "Newlib",
77
78   "<invalid>"
79 };
80
81 const char *
82 gdbarch_osabi_name (enum gdb_osabi osabi)
83 {
84   if (osabi >= GDB_OSABI_UNKNOWN && osabi < GDB_OSABI_INVALID)
85     return gdb_osabi_names[osabi];
86
87   return gdb_osabi_names[GDB_OSABI_INVALID];
88 }
89
90 /* Lookup the OS ABI corresponding to the specified target description
91    string.  */
92
93 enum gdb_osabi
94 osabi_from_tdesc_string (const char *name)
95 {
96   int i;
97
98   for (i = 0; i < ARRAY_SIZE (gdb_osabi_names); i++)
99     if (strcmp (name, gdb_osabi_names[i]) == 0)
100       {
101         /* See note above: the name table matches the indices assigned
102            to enum gdb_osabi.  */
103         enum gdb_osabi osabi = (enum gdb_osabi) i;
104
105         if (osabi == GDB_OSABI_INVALID)
106           return GDB_OSABI_UNKNOWN;
107         else
108           return osabi;
109       }
110
111   return GDB_OSABI_UNKNOWN;
112 }
113
114 /* Handler for a given architecture/OS ABI pair.  There should be only
115    one handler for a given OS ABI each architecture family.  */
116 struct gdb_osabi_handler  
117 {
118   struct gdb_osabi_handler *next;
119   const struct bfd_arch_info *arch_info;
120   enum gdb_osabi osabi;
121   void (*init_osabi)(struct gdbarch_info, struct gdbarch *);
122 };
123
124 static struct gdb_osabi_handler *gdb_osabi_handler_list;
125
126 void
127 gdbarch_register_osabi (enum bfd_architecture arch, unsigned long machine,
128                         enum gdb_osabi osabi,
129                         void (*init_osabi)(struct gdbarch_info,
130                                            struct gdbarch *))
131 {
132   struct gdb_osabi_handler **handler_p;
133   const struct bfd_arch_info *arch_info = bfd_lookup_arch (arch, machine);
134   const char **name_ptr;
135
136   /* Registering an OS ABI handler for "unknown" is not allowed.  */
137   if (osabi == GDB_OSABI_UNKNOWN)
138     {
139       internal_error
140         (__FILE__, __LINE__,
141          _("gdbarch_register_osabi: An attempt to register a handler for "
142          "OS ABI \"%s\" for architecture %s was made.  The handler will "
143          "not be registered"),
144          gdbarch_osabi_name (osabi),
145          bfd_printable_arch_mach (arch, machine));
146       return;
147     }
148
149   gdb_assert (arch_info);
150
151   for (handler_p = &gdb_osabi_handler_list; *handler_p != NULL;
152        handler_p = &(*handler_p)->next)
153     {
154       if ((*handler_p)->arch_info == arch_info
155           && (*handler_p)->osabi == osabi)
156         {
157           internal_error
158             (__FILE__, __LINE__,
159              _("gdbarch_register_osabi: A handler for OS ABI \"%s\" "
160              "has already been registered for architecture %s"),
161              gdbarch_osabi_name (osabi),
162              arch_info->printable_name);
163           /* If user wants to continue, override previous definition.  */
164           (*handler_p)->init_osabi = init_osabi;
165           return;
166         }
167     }
168
169   (*handler_p)
170     = (struct gdb_osabi_handler *) xmalloc (sizeof (struct gdb_osabi_handler));
171   (*handler_p)->next = NULL;
172   (*handler_p)->arch_info = arch_info;
173   (*handler_p)->osabi = osabi;
174   (*handler_p)->init_osabi = init_osabi;
175
176   /* Add this OS ABI to the list of enum values for "set osabi", if it isn't
177      already there.  */
178   for (name_ptr = gdb_osabi_available_names; *name_ptr; name_ptr ++)
179     {
180       if (*name_ptr == gdbarch_osabi_name (osabi))
181         return;
182     }
183   *name_ptr++ = gdbarch_osabi_name (osabi);
184   *name_ptr = NULL;
185 }
186 \f
187
188 /* Sniffer to find the OS ABI for a given file's architecture and flavour.
189    It is legal to have multiple sniffers for each arch/flavour pair, to
190    disambiguate one OS's a.out from another, for example.  The first sniffer
191    to return something other than GDB_OSABI_UNKNOWN wins, so a sniffer should
192    be careful to claim a file only if it knows for sure what it is.  */
193 struct gdb_osabi_sniffer
194 {
195   struct gdb_osabi_sniffer *next;
196   enum bfd_architecture arch;   /* bfd_arch_unknown == wildcard */
197   enum bfd_flavour flavour;
198   enum gdb_osabi (*sniffer)(bfd *);
199 };
200
201 static struct gdb_osabi_sniffer *gdb_osabi_sniffer_list;
202
203 void
204 gdbarch_register_osabi_sniffer (enum bfd_architecture arch,
205                                 enum bfd_flavour flavour,
206                                 enum gdb_osabi (*sniffer_fn)(bfd *))
207 {
208   struct gdb_osabi_sniffer *sniffer;
209
210   sniffer =
211     (struct gdb_osabi_sniffer *) xmalloc (sizeof (struct gdb_osabi_sniffer));
212   sniffer->arch = arch;
213   sniffer->flavour = flavour;
214   sniffer->sniffer = sniffer_fn;
215
216   sniffer->next = gdb_osabi_sniffer_list;
217   gdb_osabi_sniffer_list = sniffer;
218 }
219 \f
220
221 enum gdb_osabi
222 gdbarch_lookup_osabi (bfd *abfd)
223 {
224   struct gdb_osabi_sniffer *sniffer;
225   enum gdb_osabi osabi, match;
226   int match_specific;
227
228   /* If we aren't in "auto" mode, return the specified OS ABI.  */
229   if (user_osabi_state == osabi_user)
230     return user_selected_osabi;
231
232   /* If we don't have a binary, just return unknown.  The caller may
233      have other sources the OSABI can be extracted from, e.g., the
234      target description.  */
235   if (abfd == NULL) 
236     return GDB_OSABI_UNKNOWN;
237
238   match = GDB_OSABI_UNKNOWN;
239   match_specific = 0;
240
241   for (sniffer = gdb_osabi_sniffer_list; sniffer != NULL;
242        sniffer = sniffer->next)
243     {
244       if ((sniffer->arch == bfd_arch_unknown /* wildcard */
245            || sniffer->arch == bfd_get_arch (abfd))
246           && sniffer->flavour == bfd_get_flavour (abfd))
247         {
248           osabi = (*sniffer->sniffer) (abfd);
249           if (osabi < GDB_OSABI_UNKNOWN || osabi >= GDB_OSABI_INVALID)
250             {
251               internal_error
252                 (__FILE__, __LINE__,
253                  _("gdbarch_lookup_osabi: invalid OS ABI (%d) from sniffer "
254                  "for architecture %s flavour %d"),
255                  (int) osabi,
256                  bfd_printable_arch_mach (bfd_get_arch (abfd), 0),
257                  (int) bfd_get_flavour (abfd));
258             }
259           else if (osabi != GDB_OSABI_UNKNOWN)
260             {
261               /* A specific sniffer always overrides a generic sniffer.
262                  Croak on multiple match if the two matches are of the
263                  same class.  If the user wishes to continue, we'll use
264                  the first match.  */
265               if (match != GDB_OSABI_UNKNOWN)
266                 {
267                   if ((match_specific && sniffer->arch != bfd_arch_unknown)
268                    || (!match_specific && sniffer->arch == bfd_arch_unknown))
269                     {
270                       internal_error
271                         (__FILE__, __LINE__,
272                          _("gdbarch_lookup_osabi: multiple %sspecific OS ABI "
273                          "match for architecture %s flavour %d: first "
274                          "match \"%s\", second match \"%s\""),
275                          match_specific ? "" : "non-",
276                          bfd_printable_arch_mach (bfd_get_arch (abfd), 0),
277                          (int) bfd_get_flavour (abfd),
278                          gdbarch_osabi_name (match),
279                          gdbarch_osabi_name (osabi));
280                     }
281                   else if (sniffer->arch != bfd_arch_unknown)
282                     {
283                       match = osabi;
284                       match_specific = 1;
285                     }
286                 }
287               else
288                 {
289                   match = osabi;
290                   if (sniffer->arch != bfd_arch_unknown)
291                     match_specific = 1;
292                 }
293             }
294         }
295     }
296
297   return match;
298 }
299
300
301 /* Return non-zero if architecture A can run code written for
302    architecture B.  */
303 static int
304 can_run_code_for (const struct bfd_arch_info *a, const struct bfd_arch_info *b)
305 {
306   /* BFD's 'A->compatible (A, B)' functions return zero if A and B are
307      incompatible.  But if they are compatible, it returns the 'more
308      featureful' of the two arches.  That is, if A can run code
309      written for B, but B can't run code written for A, then it'll
310      return A.
311
312      struct bfd_arch_info objects are singletons: that is, there's
313      supposed to be exactly one instance for a given machine.  So you
314      can tell whether two are equivalent by comparing pointers.  */
315   return (a == b || a->compatible (a, b) == a);
316 }
317
318
319 void
320 gdbarch_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
321 {
322   struct gdb_osabi_handler *handler;
323
324   if (info.osabi == GDB_OSABI_UNKNOWN)
325     {
326       /* Don't complain about an unknown OSABI.  Assume the user knows
327          what they are doing.  */
328       return;
329     }
330
331   for (handler = gdb_osabi_handler_list; handler != NULL;
332        handler = handler->next)
333     {
334       if (handler->osabi != info.osabi)
335         continue;
336
337       /* If the architecture described by ARCH_INFO can run code for
338          the architcture we registered the handler for, then the
339          handler is applicable.  Note, though, that if the handler is
340          for an architecture that is a superset of ARCH_INFO, we can't
341          use that --- it would be perfectly correct for it to install
342          gdbarch methods that refer to registers / instructions /
343          other facilities ARCH_INFO doesn't have.
344
345          NOTE: kettenis/20021027: There may be more than one machine
346          type that is compatible with the desired machine type.  Right
347          now we simply return the first match, which is fine for now.
348          However, we might want to do something smarter in the future.  */
349       /* NOTE: cagney/2003-10-23: The code for "a can_run_code_for b"
350          is implemented using BFD's compatible method (a->compatible
351          (b) == a -- the lowest common denominator between a and b is
352          a).  That method's definition of compatible may not be as you
353          expect.  For instance the test "amd64 can run code for i386"
354          (or more generally "64-bit ISA can run code for the 32-bit
355          ISA").  BFD doesn't normally consider 32-bit and 64-bit
356          "compatible" so it doesn't succeed.  */
357       if (can_run_code_for (info.bfd_arch_info, handler->arch_info))
358         {
359           (*handler->init_osabi) (info, gdbarch);
360           return;
361         }
362     }
363
364   warning
365     ("A handler for the OS ABI \"%s\" is not built into this configuration\n"
366      "of GDB.  Attempting to continue with the default %s settings.\n",
367      gdbarch_osabi_name (info.osabi),
368      info.bfd_arch_info->printable_name);
369 }
370 \f
371 /* Limit on the amount of data to be read.  */
372 #define MAX_NOTESZ      128
373
374 /* Return non-zero if NOTE matches NAME, DESCSZ and TYPE.  If
375    *SECTSIZE is non-zero, then this reads that many bytes from
376    the start of the section and clears *SECTSIZE.  */
377
378 static int
379 check_note (bfd *abfd, asection *sect, char *note, unsigned int *sectsize,
380             const char *name, unsigned long descsz, unsigned long type)
381 {
382   unsigned long notesz;
383
384   if (*sectsize)
385     {
386       if (!bfd_get_section_contents (abfd, sect, note, 0, *sectsize))
387         return 0;
388       *sectsize = 0;
389     }
390
391   /* Calculate the size of this note.  */
392   notesz = strlen (name) + 1;
393   notesz = ((notesz + 3) & ~3);
394   notesz += descsz;
395   notesz = ((notesz + 3) & ~3);
396
397   /* If this assertion triggers, increase MAX_NOTESZ.  */
398   gdb_assert (notesz <= MAX_NOTESZ);
399
400   /* Check whether SECT is big enough to comtain the complete note.  */
401   if (notesz > bfd_section_size (abfd, sect))
402     return 0;
403
404   /* Check the note name.  */
405   if (bfd_h_get_32 (abfd, note) != (strlen (name) + 1)
406       || strcmp (note + 12, name) != 0)
407     return 0;
408
409   /* Check the descriptor size.  */
410   if (bfd_h_get_32 (abfd, note + 4) != descsz)
411     return 0;
412
413   /* Check the note type.  */
414   if (bfd_h_get_32 (abfd, note + 8) != type)
415     return 0;
416
417   return 1;
418 }
419
420 /* Generic sniffer for ELF flavoured files.  */
421
422 void
423 generic_elf_osabi_sniff_abi_tag_sections (bfd *abfd, asection *sect, void *obj)
424 {
425   enum gdb_osabi *osabi = obj;
426   const char *name;
427   unsigned int sectsize;
428   char *note;
429
430   name = bfd_get_section_name (abfd, sect);
431   sectsize = bfd_section_size (abfd, sect);
432
433   /* Limit the amount of data to read.  */
434   if (sectsize > MAX_NOTESZ)
435     sectsize = MAX_NOTESZ;
436
437   /* We lazily read the section data here.  Since we use
438      BFD_DECOMPRESS, we can't use bfd_get_section_contents on a
439      compressed section.  But, since note sections are not compressed,
440      deferring the reading until we recognize the section avoids any
441      error.  */
442   note = alloca (sectsize);
443
444   /* .note.ABI-tag notes, used by GNU/Linux and FreeBSD.  */
445   if (strcmp (name, ".note.ABI-tag") == 0)
446     {
447       /* GNU.  */
448       if (check_note (abfd, sect, note, &sectsize, "GNU", 16, NT_GNU_ABI_TAG))
449         {
450           unsigned int abi_tag = bfd_h_get_32 (abfd, note + 16);
451
452           switch (abi_tag)
453             {
454             case GNU_ABI_TAG_LINUX:
455               *osabi = GDB_OSABI_LINUX;
456               break;
457
458             case GNU_ABI_TAG_HURD:
459               *osabi = GDB_OSABI_HURD;
460               break;
461
462             case GNU_ABI_TAG_SOLARIS:
463               *osabi = GDB_OSABI_SOLARIS;
464               break;
465
466             case GNU_ABI_TAG_FREEBSD:
467               *osabi = GDB_OSABI_FREEBSD_ELF;
468               break;
469
470             case GNU_ABI_TAG_NETBSD:
471               *osabi = GDB_OSABI_NETBSD_ELF;
472               break;
473
474             default:
475               internal_error (__FILE__, __LINE__,
476                               _("generic_elf_osabi_sniff_abi_tag_sections: "
477                                 "unknown OS number %d"),
478                               abi_tag);
479             }
480           return;
481         }
482
483       /* FreeBSD.  */
484       if (check_note (abfd, sect, note, &sectsize, "FreeBSD", 4,
485                       NT_FREEBSD_ABI_TAG))
486         {
487           /* There is no need to check the version yet.  */
488           *osabi = GDB_OSABI_FREEBSD_ELF;
489           return;
490         }
491
492       return;
493     }
494       
495   /* .note.netbsd.ident notes, used by NetBSD.  */
496   if (strcmp (name, ".note.netbsd.ident") == 0
497       && check_note (abfd, sect, note, &sectsize, "NetBSD", 4, NT_NETBSD_IDENT))
498     {
499       /* There is no need to check the version yet.  */
500       *osabi = GDB_OSABI_NETBSD_ELF;
501       return;
502     }
503
504   /* .note.openbsd.ident notes, used by OpenBSD.  */
505   if (strcmp (name, ".note.openbsd.ident") == 0
506       && check_note (abfd, sect, note, &sectsize, "OpenBSD", 4,
507                      NT_OPENBSD_IDENT))
508     {
509       /* There is no need to check the version yet.  */
510       *osabi = GDB_OSABI_OPENBSD_ELF;
511       return;
512     }
513
514   /* .note.netbsdcore.procinfo notes, used by NetBSD.  */
515   if (strcmp (name, ".note.netbsdcore.procinfo") == 0)
516     {
517       *osabi = GDB_OSABI_NETBSD_ELF;
518       return;
519     }
520 }
521
522 static enum gdb_osabi
523 generic_elf_osabi_sniffer (bfd *abfd)
524 {
525   unsigned int elfosabi;
526   enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
527
528   elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
529
530   switch (elfosabi)
531     {
532     case ELFOSABI_NONE:
533     case ELFOSABI_GNU:
534       /* When the EI_OSABI field in the ELF header is ELFOSABI_NONE
535          (0), then the ELF structures in the file are conforming to
536          the base specification for that machine (there are no
537          OS-specific extensions).  In order to determine the real OS
538          in use, we must look for OS-specific notes.
539
540          The same applies for ELFOSABI_GNU: this can mean GNU/Hurd,
541          GNU/Linux, and possibly more.  */
542       bfd_map_over_sections (abfd,
543                              generic_elf_osabi_sniff_abi_tag_sections,
544                              &osabi);
545       break;
546
547     case ELFOSABI_FREEBSD:
548       osabi = GDB_OSABI_FREEBSD_ELF;
549       break;
550
551     case ELFOSABI_NETBSD:
552       osabi = GDB_OSABI_NETBSD_ELF;
553       break;
554
555     case ELFOSABI_SOLARIS:
556       osabi = GDB_OSABI_SOLARIS;
557       break;
558
559     case ELFOSABI_HPUX:
560       /* For some reason the default value for the EI_OSABI field is
561          ELFOSABI_HPUX for all PA-RISC targets (with the exception of
562          GNU/Linux).  We use HP-UX ELF as the default, but let any
563          OS-specific notes override this.  */
564       osabi = GDB_OSABI_HPUX_ELF;
565       bfd_map_over_sections (abfd,
566                              generic_elf_osabi_sniff_abi_tag_sections,
567                              &osabi);
568       break;
569
570     case ELFOSABI_OPENVMS:
571       osabi = GDB_OSABI_OPENVMS;
572       break;
573     }
574
575   if (osabi == GDB_OSABI_UNKNOWN)
576     {
577       /* The FreeBSD folks have been naughty; they stored the string
578          "FreeBSD" in the padding of the e_ident field of the ELF
579          header to "brand" their ELF binaries in FreeBSD 3.x.  */
580       if (memcmp (&elf_elfheader (abfd)->e_ident[8],
581                   "FreeBSD", sizeof ("FreeBSD")) == 0)
582         osabi = GDB_OSABI_FREEBSD_ELF;
583     }
584
585   return osabi;
586 }
587 \f
588 static void
589 set_osabi (char *args, int from_tty, struct cmd_list_element *c)
590 {
591   struct gdbarch_info info;
592
593   if (strcmp (set_osabi_string, "auto") == 0)
594     user_osabi_state = osabi_auto;
595   else if (strcmp (set_osabi_string, "default") == 0)
596     {
597       user_selected_osabi = GDB_OSABI_DEFAULT;
598       user_osabi_state = osabi_user;
599     }
600   else if (strcmp (set_osabi_string, "none") == 0)
601     {
602       user_selected_osabi = GDB_OSABI_UNKNOWN;
603       user_osabi_state = osabi_user;
604     }
605   else
606     {
607       int i;
608
609       for (i = 1; i < GDB_OSABI_INVALID; i++)
610         if (strcmp (set_osabi_string, gdbarch_osabi_name (i)) == 0)
611           {
612             user_selected_osabi = i;
613             user_osabi_state = osabi_user;
614             break;
615           }
616       if (i == GDB_OSABI_INVALID)
617         internal_error (__FILE__, __LINE__,
618                         _("Invalid OS ABI \"%s\" passed to command handler."),
619                         set_osabi_string);
620     }
621
622   /* NOTE: At some point (true multiple architectures) we'll need to be more
623      graceful here.  */
624   gdbarch_info_init (&info);
625   if (! gdbarch_update_p (info))
626     internal_error (__FILE__, __LINE__, _("Updating OS ABI failed."));
627 }
628
629 static void
630 show_osabi (struct ui_file *file, int from_tty, struct cmd_list_element *c,
631             const char *value)
632 {
633   if (user_osabi_state == osabi_auto)
634     fprintf_filtered (file,
635                       _("The current OS ABI is \"auto\" "
636                         "(currently \"%s\").\n"),
637                       gdbarch_osabi_name (gdbarch_osabi (get_current_arch ())));
638   else
639     fprintf_filtered (file, _("The current OS ABI is \"%s\".\n"),
640                       gdbarch_osabi_name (user_selected_osabi));
641
642   if (GDB_OSABI_DEFAULT != GDB_OSABI_UNKNOWN)
643     fprintf_filtered (file, _("The default OS ABI is \"%s\".\n"),
644                       gdbarch_osabi_name (GDB_OSABI_DEFAULT));
645 }
646 \f
647 extern initialize_file_ftype _initialize_gdb_osabi; /* -Wmissing-prototype */
648
649 void
650 _initialize_gdb_osabi (void)
651 {
652   if (strcmp (gdb_osabi_names[GDB_OSABI_INVALID], "<invalid>") != 0)
653     internal_error
654       (__FILE__, __LINE__,
655        _("_initialize_gdb_osabi: gdb_osabi_names[] is inconsistent"));
656
657   /* Register a generic sniffer for ELF flavoured files.  */
658   gdbarch_register_osabi_sniffer (bfd_arch_unknown,
659                                   bfd_target_elf_flavour,
660                                   generic_elf_osabi_sniffer);
661
662   /* Register the "set osabi" command.  */
663   add_setshow_enum_cmd ("osabi", class_support, gdb_osabi_available_names,
664                         &set_osabi_string,
665                         _("Set OS ABI of target."),
666                         _("Show OS ABI of target."),
667                         NULL, set_osabi, show_osabi,
668                         &setlist, &showlist);
669   user_osabi_state = osabi_auto;
670 }