gdb/osabi.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* OS ABI variant handling for GDB.

  2.    Copyright (C) 2001-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"

  15. #include "osabi.h"
  16. #include "arch-utils.h"
  17. #include "gdbcmd.h"
  18. #include "command.h"

  19. #include "elf-bfd.h"

  20. #ifndef GDB_OSABI_DEFAULT
  21. #define GDB_OSABI_DEFAULT GDB_OSABI_UNKNOWN
  22. #endif

  23. /* State for the "set osabi" command.  */
  24. static enum { osabi_auto, osabi_default, osabi_user } user_osabi_state;
  25. static enum gdb_osabi user_selected_osabi;
  26. static const char *gdb_osabi_available_names[GDB_OSABI_INVALID + 3] = {
  27.   "auto",
  28.   "default",
  29.   "none",
  30.   NULL
  31. };
  32. static const char *set_osabi_string;

  33. /* Names associated with each osabi.  */

  34. struct osabi_names
  35. {
  36.   /* The "pretty" name.  */

  37.   const char *pretty;

  38.   /* The triplet regexp, or NULL if not known.  */

  39.   const char *regexp;
  40. };

  41. /* This table matches the indices assigned to enum gdb_osabi.  Keep
  42.    them in sync.  */
  43. static const struct osabi_names gdb_osabi_names[] =
  44. {
  45.   { "none", NULL },

  46.   { "SVR4", NULL },
  47.   { "GNU/Hurd", NULL },
  48.   { "Solaris", NULL },
  49.   { "GNU/Linux", "linux(-gnu)?" },
  50.   { "FreeBSD a.out", NULL },
  51.   { "FreeBSD ELF", NULL },
  52.   { "NetBSD a.out", NULL },
  53.   { "NetBSD ELF", NULL },
  54.   { "OpenBSD ELF", NULL },
  55.   { "Windows CE", NULL },
  56.   { "DJGPP", NULL },
  57.   { "Irix", NULL },
  58.   { "HP/UX ELF", NULL },
  59.   { "HP/UX SOM", NULL },
  60.   { "QNX Neutrino", NULL },
  61.   { "Cygwin", NULL },
  62.   { "AIX", NULL },
  63.   { "DICOS", NULL },
  64.   { "Darwin", NULL },
  65.   { "Symbian", NULL },
  66.   { "OpenVMS", NULL },
  67.   { "LynxOS178", NULL },
  68.   { "Newlib", NULL },
  69.   { "SDE", NULL },

  70.   { "<invalid>", NULL }
  71. };

  72. const char *
  73. gdbarch_osabi_name (enum gdb_osabi osabi)
  74. {
  75.   if (osabi >= GDB_OSABI_UNKNOWN && osabi < GDB_OSABI_INVALID)
  76.     return gdb_osabi_names[osabi].pretty;

  77.   return gdb_osabi_names[GDB_OSABI_INVALID].pretty;
  78. }

  79. /* See osabi.h.  */

  80. const char *
  81. osabi_triplet_regexp (enum gdb_osabi osabi)
  82. {
  83.   if (osabi >= GDB_OSABI_UNKNOWN && osabi < GDB_OSABI_INVALID)
  84.     return gdb_osabi_names[osabi].regexp;

  85.   return gdb_osabi_names[GDB_OSABI_INVALID].regexp;
  86. }

  87. /* Lookup the OS ABI corresponding to the specified target description
  88.    string.  */

  89. enum gdb_osabi
  90. osabi_from_tdesc_string (const char *name)
  91. {
  92.   int i;

  93.   for (i = 0; i < ARRAY_SIZE (gdb_osabi_names); i++)
  94.     if (strcmp (name, gdb_osabi_names[i].pretty) == 0)
  95.       {
  96.         /* See note above: the name table matches the indices assigned
  97.            to enum gdb_osabi.  */
  98.         enum gdb_osabi osabi = (enum gdb_osabi) i;

  99.         if (osabi == GDB_OSABI_INVALID)
  100.           return GDB_OSABI_UNKNOWN;
  101.         else
  102.           return osabi;
  103.       }

  104.   return GDB_OSABI_UNKNOWN;
  105. }

  106. /* Handler for a given architecture/OS ABI pair.  There should be only
  107.    one handler for a given OS ABI each architecture family.  */
  108. struct gdb_osabi_handler
  109. {
  110.   struct gdb_osabi_handler *next;
  111.   const struct bfd_arch_info *arch_info;
  112.   enum gdb_osabi osabi;
  113.   void (*init_osabi)(struct gdbarch_info, struct gdbarch *);
  114. };

  115. static struct gdb_osabi_handler *gdb_osabi_handler_list;

  116. void
  117. gdbarch_register_osabi (enum bfd_architecture arch, unsigned long machine,
  118.                         enum gdb_osabi osabi,
  119.                         void (*init_osabi)(struct gdbarch_info,
  120.                                            struct gdbarch *))
  121. {
  122.   struct gdb_osabi_handler **handler_p;
  123.   const struct bfd_arch_info *arch_info = bfd_lookup_arch (arch, machine);
  124.   const char **name_ptr;

  125.   /* Registering an OS ABI handler for "unknown" is not allowed.  */
  126.   if (osabi == GDB_OSABI_UNKNOWN)
  127.     {
  128.       internal_error
  129.         (__FILE__, __LINE__,
  130.          _("gdbarch_register_osabi: An attempt to register a handler for "
  131.          "OS ABI \"%s\" for architecture %s was made.  The handler will "
  132.          "not be registered"),
  133.          gdbarch_osabi_name (osabi),
  134.          bfd_printable_arch_mach (arch, machine));
  135.       return;
  136.     }

  137.   gdb_assert (arch_info);

  138.   for (handler_p = &gdb_osabi_handler_list; *handler_p != NULL;
  139.        handler_p = &(*handler_p)->next)
  140.     {
  141.       if ((*handler_p)->arch_info == arch_info
  142.           && (*handler_p)->osabi == osabi)
  143.         {
  144.           internal_error
  145.             (__FILE__, __LINE__,
  146.              _("gdbarch_register_osabi: A handler for OS ABI \"%s\" "
  147.              "has already been registered for architecture %s"),
  148.              gdbarch_osabi_name (osabi),
  149.              arch_info->printable_name);
  150.           /* If user wants to continue, override previous definition.  */
  151.           (*handler_p)->init_osabi = init_osabi;
  152.           return;
  153.         }
  154.     }

  155.   (*handler_p)
  156.     = (struct gdb_osabi_handler *) xmalloc (sizeof (struct gdb_osabi_handler));
  157.   (*handler_p)->next = NULL;
  158.   (*handler_p)->arch_info = arch_info;
  159.   (*handler_p)->osabi = osabi;
  160.   (*handler_p)->init_osabi = init_osabi;

  161.   /* Add this OS ABI to the list of enum values for "set osabi", if it isn't
  162.      already there.  */
  163.   for (name_ptr = gdb_osabi_available_names; *name_ptr; name_ptr ++)
  164.     {
  165.       if (*name_ptr == gdbarch_osabi_name (osabi))
  166.         return;
  167.     }
  168.   *name_ptr++ = gdbarch_osabi_name (osabi);
  169.   *name_ptr = NULL;
  170. }


  171. /* Sniffer to find the OS ABI for a given file's architecture and flavour.
  172.    It is legal to have multiple sniffers for each arch/flavour pair, to
  173.    disambiguate one OS's a.out from another, for example.  The first sniffer
  174.    to return something other than GDB_OSABI_UNKNOWN wins, so a sniffer should
  175.    be careful to claim a file only if it knows for sure what it is.  */
  176. struct gdb_osabi_sniffer
  177. {
  178.   struct gdb_osabi_sniffer *next;
  179.   enum bfd_architecture arch;   /* bfd_arch_unknown == wildcard */
  180.   enum bfd_flavour flavour;
  181.   enum gdb_osabi (*sniffer)(bfd *);
  182. };

  183. static struct gdb_osabi_sniffer *gdb_osabi_sniffer_list;

  184. void
  185. gdbarch_register_osabi_sniffer (enum bfd_architecture arch,
  186.                                 enum bfd_flavour flavour,
  187.                                 enum gdb_osabi (*sniffer_fn)(bfd *))
  188. {
  189.   struct gdb_osabi_sniffer *sniffer;

  190.   sniffer =
  191.     (struct gdb_osabi_sniffer *) xmalloc (sizeof (struct gdb_osabi_sniffer));
  192.   sniffer->arch = arch;
  193.   sniffer->flavour = flavour;
  194.   sniffer->sniffer = sniffer_fn;

  195.   sniffer->next = gdb_osabi_sniffer_list;
  196.   gdb_osabi_sniffer_list = sniffer;
  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.   /* If we aren't in "auto" mode, return the specified OS ABI.  */
  205.   if (user_osabi_state == osabi_user)
  206.     return user_selected_osabi;

  207.   /* If we don't have a binary, just return unknown.  The caller may
  208.      have other sources the OSABI can be extracted from, e.g., the
  209.      target description.  */
  210.   if (abfd == NULL)
  211.     return GDB_OSABI_UNKNOWN;

  212.   match = GDB_OSABI_UNKNOWN;
  213.   match_specific = 0;

  214.   for (sniffer = gdb_osabi_sniffer_list; sniffer != NULL;
  215.        sniffer = sniffer->next)
  216.     {
  217.       if ((sniffer->arch == bfd_arch_unknown /* wildcard */
  218.            || sniffer->arch == bfd_get_arch (abfd))
  219.           && sniffer->flavour == bfd_get_flavour (abfd))
  220.         {
  221.           osabi = (*sniffer->sniffer) (abfd);
  222.           if (osabi < GDB_OSABI_UNKNOWN || osabi >= GDB_OSABI_INVALID)
  223.             {
  224.               internal_error
  225.                 (__FILE__, __LINE__,
  226.                  _("gdbarch_lookup_osabi: invalid OS ABI (%d) from sniffer "
  227.                  "for architecture %s flavour %d"),
  228.                  (int) osabi,
  229.                  bfd_printable_arch_mach (bfd_get_arch (abfd), 0),
  230.                  (int) bfd_get_flavour (abfd));
  231.             }
  232.           else if (osabi != GDB_OSABI_UNKNOWN)
  233.             {
  234.               /* A specific sniffer always overrides a generic sniffer.
  235.                  Croak on multiple match if the two matches are of the
  236.                  same class.  If the user wishes to continue, we'll use
  237.                  the first match.  */
  238.               if (match != GDB_OSABI_UNKNOWN)
  239.                 {
  240.                   if ((match_specific && sniffer->arch != bfd_arch_unknown)
  241.                    || (!match_specific && sniffer->arch == bfd_arch_unknown))
  242.                     {
  243.                       internal_error
  244.                         (__FILE__, __LINE__,
  245.                          _("gdbarch_lookup_osabi: multiple %sspecific OS ABI "
  246.                          "match for architecture %s flavour %d: first "
  247.                          "match \"%s\", second match \"%s\""),
  248.                          match_specific ? "" : "non-",
  249.                          bfd_printable_arch_mach (bfd_get_arch (abfd), 0),
  250.                          (int) bfd_get_flavour (abfd),
  251.                          gdbarch_osabi_name (match),
  252.                          gdbarch_osabi_name (osabi));
  253.                     }
  254.                   else if (sniffer->arch != bfd_arch_unknown)
  255.                     {
  256.                       match = osabi;
  257.                       match_specific = 1;
  258.                     }
  259.                 }
  260.               else
  261.                 {
  262.                   match = osabi;
  263.                   if (sniffer->arch != bfd_arch_unknown)
  264.                     match_specific = 1;
  265.                 }
  266.             }
  267.         }
  268.     }

  269.   return match;
  270. }


  271. /* Return non-zero if architecture A can run code written for
  272.    architecture B.  */
  273. static int
  274. can_run_code_for (const struct bfd_arch_info *a, const struct bfd_arch_info *b)
  275. {
  276.   /* BFD's 'A->compatible (A, B)' functions return zero if A and B are
  277.      incompatible.  But if they are compatible, it returns the 'more
  278.      featureful' of the two arches.  That is, if A can run code
  279.      written for B, but B can't run code written for A, then it'll
  280.      return A.

  281.      struct bfd_arch_info objects are singletons: that is, there's
  282.      supposed to be exactly one instance for a given machine.  So you
  283.      can tell whether two are equivalent by comparing pointers.  */
  284.   return (a == b || a->compatible (a, b) == a);
  285. }


  286. void
  287. gdbarch_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
  288. {
  289.   struct gdb_osabi_handler *handler;

  290.   if (info.osabi == GDB_OSABI_UNKNOWN)
  291.     {
  292.       /* Don't complain about an unknown OSABI.  Assume the user knows
  293.          what they are doing.  */
  294.       return;
  295.     }

  296.   for (handler = gdb_osabi_handler_list; handler != NULL;
  297.        handler = handler->next)
  298.     {
  299.       if (handler->osabi != info.osabi)
  300.         continue;

  301.       /* If the architecture described by ARCH_INFO can run code for
  302.          the architcture we registered the handler for, then the
  303.          handler is applicable.  Note, though, that if the handler is
  304.          for an architecture that is a superset of ARCH_INFO, we can't
  305.          use that --- it would be perfectly correct for it to install
  306.          gdbarch methods that refer to registers / instructions /
  307.          other facilities ARCH_INFO doesn't have.

  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.       /* NOTE: cagney/2003-10-23: The code for "a can_run_code_for b"
  313.          is implemented using BFD's compatible method (a->compatible
  314.          (b) == a -- the lowest common denominator between a and b is
  315.          a).  That method's definition of compatible may not be as you
  316.          expect.  For instance the test "amd64 can run code for i386"
  317.          (or more generally "64-bit ISA can run code for the 32-bit
  318.          ISA").  BFD doesn't normally consider 32-bit and 64-bit
  319.          "compatible" so it doesn't succeed.  */
  320.       if (can_run_code_for (info.bfd_arch_info, handler->arch_info))
  321.         {
  322.           (*handler->init_osabi) (info, gdbarch);
  323.           return;
  324.         }
  325.     }

  326.   warning
  327.     ("A handler for the OS ABI \"%s\" is not built into this configuration\n"
  328.      "of GDB.  Attempting to continue with the default %s settings.\n",
  329.      gdbarch_osabi_name (info.osabi),
  330.      info.bfd_arch_info->printable_name);
  331. }

  332. /* Limit on the amount of data to be read.  */
  333. #define MAX_NOTESZ        128

  334. /* Return non-zero if NOTE matches NAME, DESCSZ and TYPE.  If
  335.    *SECTSIZE is non-zero, then this reads that many bytes from
  336.    the start of the section and clears *SECTSIZE.  */

  337. static int
  338. check_note (bfd *abfd, asection *sect, char *note, unsigned int *sectsize,
  339.             const char *name, unsigned long descsz, unsigned long type)
  340. {
  341.   unsigned long notesz;

  342.   if (*sectsize)
  343.     {
  344.       if (!bfd_get_section_contents (abfd, sect, note, 0, *sectsize))
  345.         return 0;
  346.       *sectsize = 0;
  347.     }

  348.   /* Calculate the size of this note.  */
  349.   notesz = strlen (name) + 1;
  350.   notesz = ((notesz + 3) & ~3);
  351.   notesz += descsz;
  352.   notesz = ((notesz + 3) & ~3);

  353.   /* If this assertion triggers, increase MAX_NOTESZ.  */
  354.   gdb_assert (notesz <= MAX_NOTESZ);

  355.   /* Check whether SECT is big enough to comtain the complete note.  */
  356.   if (notesz > bfd_section_size (abfd, sect))
  357.     return 0;

  358.   /* Check the note name.  */
  359.   if (bfd_h_get_32 (abfd, note) != (strlen (name) + 1)
  360.       || strcmp (note + 12, name) != 0)
  361.     return 0;

  362.   /* Check the descriptor size.  */
  363.   if (bfd_h_get_32 (abfd, note + 4) != descsz)
  364.     return 0;

  365.   /* Check the note type.  */
  366.   if (bfd_h_get_32 (abfd, note + 8) != type)
  367.     return 0;

  368.   return 1;
  369. }

  370. /* Generic sniffer for ELF flavoured files.  */

  371. void
  372. generic_elf_osabi_sniff_abi_tag_sections (bfd *abfd, asection *sect, void *obj)
  373. {
  374.   enum gdb_osabi *osabi = obj;
  375.   const char *name;
  376.   unsigned int sectsize;
  377.   char *note;

  378.   name = bfd_get_section_name (abfd, sect);
  379.   sectsize = bfd_section_size (abfd, sect);

  380.   /* Limit the amount of data to read.  */
  381.   if (sectsize > MAX_NOTESZ)
  382.     sectsize = MAX_NOTESZ;

  383.   /* We lazily read the section data here.  Since we use
  384.      BFD_DECOMPRESS, we can't use bfd_get_section_contents on a
  385.      compressed section.  But, since note sections are not compressed,
  386.      deferring the reading until we recognize the section avoids any
  387.      error.  */
  388.   note = alloca (sectsize);

  389.   /* .note.ABI-tag notes, used by GNU/Linux and FreeBSD.  */
  390.   if (strcmp (name, ".note.ABI-tag") == 0)
  391.     {
  392.       /* GNU.  */
  393.       if (check_note (abfd, sect, note, &sectsize, "GNU", 16, NT_GNU_ABI_TAG))
  394.         {
  395.           unsigned int abi_tag = bfd_h_get_32 (abfd, note + 16);

  396.           switch (abi_tag)
  397.             {
  398.             case GNU_ABI_TAG_LINUX:
  399.               *osabi = GDB_OSABI_LINUX;
  400.               break;

  401.             case GNU_ABI_TAG_HURD:
  402.               *osabi = GDB_OSABI_HURD;
  403.               break;

  404.             case GNU_ABI_TAG_SOLARIS:
  405.               *osabi = GDB_OSABI_SOLARIS;
  406.               break;

  407.             case GNU_ABI_TAG_FREEBSD:
  408.               *osabi = GDB_OSABI_FREEBSD_ELF;
  409.               break;

  410.             case GNU_ABI_TAG_NETBSD:
  411.               *osabi = GDB_OSABI_NETBSD_ELF;
  412.               break;

  413.             default:
  414.               internal_error (__FILE__, __LINE__,
  415.                               _("generic_elf_osabi_sniff_abi_tag_sections: "
  416.                                 "unknown OS number %d"),
  417.                               abi_tag);
  418.             }
  419.           return;
  420.         }

  421.       /* FreeBSD.  */
  422.       if (check_note (abfd, sect, note, &sectsize, "FreeBSD", 4,
  423.                       NT_FREEBSD_ABI_TAG))
  424.         {
  425.           /* There is no need to check the version yet.  */
  426.           *osabi = GDB_OSABI_FREEBSD_ELF;
  427.           return;
  428.         }

  429.       return;
  430.     }

  431.   /* .note.netbsd.ident notes, used by NetBSD.  */
  432.   if (strcmp (name, ".note.netbsd.ident") == 0
  433.       && check_note (abfd, sect, note, &sectsize, "NetBSD", 4, NT_NETBSD_IDENT))
  434.     {
  435.       /* There is no need to check the version yet.  */
  436.       *osabi = GDB_OSABI_NETBSD_ELF;
  437.       return;
  438.     }

  439.   /* .note.openbsd.ident notes, used by OpenBSD.  */
  440.   if (strcmp (name, ".note.openbsd.ident") == 0
  441.       && check_note (abfd, sect, note, &sectsize, "OpenBSD", 4,
  442.                      NT_OPENBSD_IDENT))
  443.     {
  444.       /* There is no need to check the version yet.  */
  445.       *osabi = GDB_OSABI_OPENBSD_ELF;
  446.       return;
  447.     }

  448.   /* .note.netbsdcore.procinfo notes, used by NetBSD.  */
  449.   if (strcmp (name, ".note.netbsdcore.procinfo") == 0)
  450.     {
  451.       *osabi = GDB_OSABI_NETBSD_ELF;
  452.       return;
  453.     }
  454. }

  455. static enum gdb_osabi
  456. generic_elf_osabi_sniffer (bfd *abfd)
  457. {
  458.   unsigned int elfosabi;
  459.   enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;

  460.   elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];

  461.   switch (elfosabi)
  462.     {
  463.     case ELFOSABI_NONE:
  464.     case ELFOSABI_GNU:
  465.       /* When the EI_OSABI field in the ELF header is ELFOSABI_NONE
  466.          (0), then the ELF structures in the file are conforming to
  467.          the base specification for that machine (there are no
  468.          OS-specific extensions).  In order to determine the real OS
  469.          in use, we must look for OS-specific notes.

  470.          The same applies for ELFOSABI_GNU: this can mean GNU/Hurd,
  471.          GNU/Linux, and possibly more.  */
  472.       bfd_map_over_sections (abfd,
  473.                              generic_elf_osabi_sniff_abi_tag_sections,
  474.                              &osabi);
  475.       break;

  476.     case ELFOSABI_FREEBSD:
  477.       osabi = GDB_OSABI_FREEBSD_ELF;
  478.       break;

  479.     case ELFOSABI_NETBSD:
  480.       osabi = GDB_OSABI_NETBSD_ELF;
  481.       break;

  482.     case ELFOSABI_SOLARIS:
  483.       osabi = GDB_OSABI_SOLARIS;
  484.       break;

  485.     case ELFOSABI_HPUX:
  486.       /* For some reason the default value for the EI_OSABI field is
  487.          ELFOSABI_HPUX for all PA-RISC targets (with the exception of
  488.          GNU/Linux).  We use HP-UX ELF as the default, but let any
  489.          OS-specific notes override this.  */
  490.       osabi = GDB_OSABI_HPUX_ELF;
  491.       bfd_map_over_sections (abfd,
  492.                              generic_elf_osabi_sniff_abi_tag_sections,
  493.                              &osabi);
  494.       break;

  495.     case ELFOSABI_OPENVMS:
  496.       osabi = GDB_OSABI_OPENVMS;
  497.       break;
  498.     }

  499.   if (osabi == GDB_OSABI_UNKNOWN)
  500.     {
  501.       /* The FreeBSD folks have been naughty; they stored the string
  502.          "FreeBSD" in the padding of the e_ident field of the ELF
  503.          header to "brand" their ELF binaries in FreeBSD 3.x.  */
  504.       if (memcmp (&elf_elfheader (abfd)->e_ident[8],
  505.                   "FreeBSD", sizeof ("FreeBSD")) == 0)
  506.         osabi = GDB_OSABI_FREEBSD_ELF;
  507.     }

  508.   return osabi;
  509. }

  510. static void
  511. set_osabi (char *args, int from_tty, struct cmd_list_element *c)
  512. {
  513.   struct gdbarch_info info;

  514.   if (strcmp (set_osabi_string, "auto") == 0)
  515.     user_osabi_state = osabi_auto;
  516.   else if (strcmp (set_osabi_string, "default") == 0)
  517.     {
  518.       user_selected_osabi = GDB_OSABI_DEFAULT;
  519.       user_osabi_state = osabi_user;
  520.     }
  521.   else if (strcmp (set_osabi_string, "none") == 0)
  522.     {
  523.       user_selected_osabi = GDB_OSABI_UNKNOWN;
  524.       user_osabi_state = osabi_user;
  525.     }
  526.   else
  527.     {
  528.       int i;

  529.       for (i = 1; i < GDB_OSABI_INVALID; i++)
  530.         if (strcmp (set_osabi_string, gdbarch_osabi_name (i)) == 0)
  531.           {
  532.             user_selected_osabi = i;
  533.             user_osabi_state = osabi_user;
  534.             break;
  535.           }
  536.       if (i == GDB_OSABI_INVALID)
  537.         internal_error (__FILE__, __LINE__,
  538.                         _("Invalid OS ABI \"%s\" passed to command handler."),
  539.                         set_osabi_string);
  540.     }

  541.   /* NOTE: At some point (true multiple architectures) we'll need to be more
  542.      graceful here.  */
  543.   gdbarch_info_init (&info);
  544.   if (! gdbarch_update_p (info))
  545.     internal_error (__FILE__, __LINE__, _("Updating OS ABI failed."));
  546. }

  547. static void
  548. show_osabi (struct ui_file *file, int from_tty, struct cmd_list_element *c,
  549.             const char *value)
  550. {
  551.   if (user_osabi_state == osabi_auto)
  552.     fprintf_filtered (file,
  553.                       _("The current OS ABI is \"auto\" "
  554.                         "(currently \"%s\").\n"),
  555.                       gdbarch_osabi_name (gdbarch_osabi (get_current_arch ())));
  556.   else
  557.     fprintf_filtered (file, _("The current OS ABI is \"%s\".\n"),
  558.                       gdbarch_osabi_name (user_selected_osabi));

  559.   if (GDB_OSABI_DEFAULT != GDB_OSABI_UNKNOWN)
  560.     fprintf_filtered (file, _("The default OS ABI is \"%s\".\n"),
  561.                       gdbarch_osabi_name (GDB_OSABI_DEFAULT));
  562. }

  563. extern initialize_file_ftype _initialize_gdb_osabi; /* -Wmissing-prototype */

  564. void
  565. _initialize_gdb_osabi (void)
  566. {
  567.   if (strcmp (gdb_osabi_names[GDB_OSABI_INVALID].pretty, "<invalid>") != 0)
  568.     internal_error
  569.       (__FILE__, __LINE__,
  570.        _("_initialize_gdb_osabi: gdb_osabi_names[] is inconsistent"));

  571.   /* Register a generic sniffer for ELF flavoured files.  */
  572.   gdbarch_register_osabi_sniffer (bfd_arch_unknown,
  573.                                   bfd_target_elf_flavour,
  574.                                   generic_elf_osabi_sniffer);

  575.   /* Register the "set osabi" command.  */
  576.   add_setshow_enum_cmd ("osabi", class_support, gdb_osabi_available_names,
  577.                         &set_osabi_string,
  578.                         _("Set OS ABI of target."),
  579.                         _("Show OS ABI of target."),
  580.                         NULL, set_osabi, show_osabi,
  581.                         &setlist, &showlist);
  582.   user_osabi_state = osabi_auto;
  583. }