15796c8dcSSimon Schubert /* OS ABI variant handling for GDB.
25796c8dcSSimon Schubert
3ef5ccd6cSJohn Marino Copyright (C) 2001-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert
55796c8dcSSimon Schubert This file is part of GDB.
65796c8dcSSimon Schubert
75796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert (at your option) any later version.
115796c8dcSSimon Schubert
125796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
155796c8dcSSimon Schubert GNU General Public License for more details.
165796c8dcSSimon Schubert
175796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */
195796c8dcSSimon Schubert
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert
225796c8dcSSimon Schubert #include "gdb_assert.h"
235796c8dcSSimon Schubert #include "gdb_string.h"
245796c8dcSSimon Schubert
255796c8dcSSimon Schubert #include "osabi.h"
265796c8dcSSimon Schubert #include "arch-utils.h"
275796c8dcSSimon Schubert #include "gdbcmd.h"
285796c8dcSSimon Schubert #include "command.h"
295796c8dcSSimon Schubert
305796c8dcSSimon Schubert #include "elf-bfd.h"
315796c8dcSSimon Schubert
325796c8dcSSimon Schubert #ifndef GDB_OSABI_DEFAULT
335796c8dcSSimon Schubert #define GDB_OSABI_DEFAULT GDB_OSABI_UNKNOWN
345796c8dcSSimon Schubert #endif
355796c8dcSSimon Schubert
365796c8dcSSimon Schubert /* State for the "set osabi" command. */
375796c8dcSSimon Schubert static enum { osabi_auto, osabi_default, osabi_user } user_osabi_state;
385796c8dcSSimon Schubert static enum gdb_osabi user_selected_osabi;
395796c8dcSSimon Schubert static const char *gdb_osabi_available_names[GDB_OSABI_INVALID + 3] = {
405796c8dcSSimon Schubert "auto",
415796c8dcSSimon Schubert "default",
425796c8dcSSimon Schubert "none",
435796c8dcSSimon Schubert NULL
445796c8dcSSimon Schubert };
455796c8dcSSimon Schubert static const char *set_osabi_string;
465796c8dcSSimon Schubert
475796c8dcSSimon Schubert /* This table matches the indices assigned to enum gdb_osabi. Keep
485796c8dcSSimon Schubert them in sync. */
495796c8dcSSimon Schubert static const char * const gdb_osabi_names[] =
505796c8dcSSimon Schubert {
515796c8dcSSimon Schubert "none",
525796c8dcSSimon Schubert
535796c8dcSSimon Schubert "SVR4",
545796c8dcSSimon Schubert "GNU/Hurd",
555796c8dcSSimon Schubert "Solaris",
565796c8dcSSimon Schubert "OSF/1",
575796c8dcSSimon Schubert "GNU/Linux",
585796c8dcSSimon Schubert "FreeBSD a.out",
595796c8dcSSimon Schubert "FreeBSD ELF",
605796c8dcSSimon Schubert "NetBSD a.out",
615796c8dcSSimon Schubert "NetBSD ELF",
625796c8dcSSimon Schubert "OpenBSD ELF",
6369e0f06dSSimon Schubert "DragonFly",
645796c8dcSSimon Schubert "Windows CE",
655796c8dcSSimon Schubert "DJGPP",
665796c8dcSSimon Schubert "Irix",
675796c8dcSSimon Schubert "Interix",
685796c8dcSSimon Schubert "HP/UX ELF",
695796c8dcSSimon Schubert "HP/UX SOM",
705796c8dcSSimon Schubert "QNX Neutrino",
715796c8dcSSimon Schubert "Cygwin",
725796c8dcSSimon Schubert "AIX",
735796c8dcSSimon Schubert "DICOS",
745796c8dcSSimon Schubert "Darwin",
75cf7f2e2dSJohn Marino "Symbian",
76ef5ccd6cSJohn Marino "OpenVMS",
77ef5ccd6cSJohn Marino "LynxOS178",
78ef5ccd6cSJohn Marino "Newlib",
795796c8dcSSimon Schubert
805796c8dcSSimon Schubert "<invalid>"
815796c8dcSSimon Schubert };
825796c8dcSSimon Schubert
835796c8dcSSimon Schubert const char *
gdbarch_osabi_name(enum gdb_osabi osabi)845796c8dcSSimon Schubert gdbarch_osabi_name (enum gdb_osabi osabi)
855796c8dcSSimon Schubert {
865796c8dcSSimon Schubert if (osabi >= GDB_OSABI_UNKNOWN && osabi < GDB_OSABI_INVALID)
875796c8dcSSimon Schubert return gdb_osabi_names[osabi];
885796c8dcSSimon Schubert
895796c8dcSSimon Schubert return gdb_osabi_names[GDB_OSABI_INVALID];
905796c8dcSSimon Schubert }
915796c8dcSSimon Schubert
925796c8dcSSimon Schubert /* Lookup the OS ABI corresponding to the specified target description
935796c8dcSSimon Schubert string. */
945796c8dcSSimon Schubert
955796c8dcSSimon Schubert enum gdb_osabi
osabi_from_tdesc_string(const char * name)965796c8dcSSimon Schubert osabi_from_tdesc_string (const char *name)
975796c8dcSSimon Schubert {
985796c8dcSSimon Schubert int i;
995796c8dcSSimon Schubert
1005796c8dcSSimon Schubert for (i = 0; i < ARRAY_SIZE (gdb_osabi_names); i++)
1015796c8dcSSimon Schubert if (strcmp (name, gdb_osabi_names[i]) == 0)
1025796c8dcSSimon Schubert {
1035796c8dcSSimon Schubert /* See note above: the name table matches the indices assigned
1045796c8dcSSimon Schubert to enum gdb_osabi. */
1055796c8dcSSimon Schubert enum gdb_osabi osabi = (enum gdb_osabi) i;
1065796c8dcSSimon Schubert
1075796c8dcSSimon Schubert if (osabi == GDB_OSABI_INVALID)
1085796c8dcSSimon Schubert return GDB_OSABI_UNKNOWN;
1095796c8dcSSimon Schubert else
1105796c8dcSSimon Schubert return osabi;
1115796c8dcSSimon Schubert }
1125796c8dcSSimon Schubert
1135796c8dcSSimon Schubert return GDB_OSABI_UNKNOWN;
1145796c8dcSSimon Schubert }
1155796c8dcSSimon Schubert
1165796c8dcSSimon Schubert /* Handler for a given architecture/OS ABI pair. There should be only
1175796c8dcSSimon Schubert one handler for a given OS ABI each architecture family. */
1185796c8dcSSimon Schubert struct gdb_osabi_handler
1195796c8dcSSimon Schubert {
1205796c8dcSSimon Schubert struct gdb_osabi_handler *next;
1215796c8dcSSimon Schubert const struct bfd_arch_info *arch_info;
1225796c8dcSSimon Schubert enum gdb_osabi osabi;
1235796c8dcSSimon Schubert void (*init_osabi)(struct gdbarch_info, struct gdbarch *);
1245796c8dcSSimon Schubert };
1255796c8dcSSimon Schubert
1265796c8dcSSimon Schubert static struct gdb_osabi_handler *gdb_osabi_handler_list;
1275796c8dcSSimon Schubert
1285796c8dcSSimon Schubert void
gdbarch_register_osabi(enum bfd_architecture arch,unsigned long machine,enum gdb_osabi osabi,void (* init_osabi)(struct gdbarch_info,struct gdbarch *))1295796c8dcSSimon Schubert gdbarch_register_osabi (enum bfd_architecture arch, unsigned long machine,
1305796c8dcSSimon Schubert enum gdb_osabi osabi,
1315796c8dcSSimon Schubert void (*init_osabi)(struct gdbarch_info,
1325796c8dcSSimon Schubert struct gdbarch *))
1335796c8dcSSimon Schubert {
1345796c8dcSSimon Schubert struct gdb_osabi_handler **handler_p;
1355796c8dcSSimon Schubert const struct bfd_arch_info *arch_info = bfd_lookup_arch (arch, machine);
1365796c8dcSSimon Schubert const char **name_ptr;
1375796c8dcSSimon Schubert
1385796c8dcSSimon Schubert /* Registering an OS ABI handler for "unknown" is not allowed. */
1395796c8dcSSimon Schubert if (osabi == GDB_OSABI_UNKNOWN)
1405796c8dcSSimon Schubert {
1415796c8dcSSimon Schubert internal_error
1425796c8dcSSimon Schubert (__FILE__, __LINE__,
1435796c8dcSSimon Schubert _("gdbarch_register_osabi: An attempt to register a handler for "
1445796c8dcSSimon Schubert "OS ABI \"%s\" for architecture %s was made. The handler will "
1455796c8dcSSimon Schubert "not be registered"),
1465796c8dcSSimon Schubert gdbarch_osabi_name (osabi),
1475796c8dcSSimon Schubert bfd_printable_arch_mach (arch, machine));
1485796c8dcSSimon Schubert return;
1495796c8dcSSimon Schubert }
1505796c8dcSSimon Schubert
1515796c8dcSSimon Schubert gdb_assert (arch_info);
1525796c8dcSSimon Schubert
1535796c8dcSSimon Schubert for (handler_p = &gdb_osabi_handler_list; *handler_p != NULL;
1545796c8dcSSimon Schubert handler_p = &(*handler_p)->next)
1555796c8dcSSimon Schubert {
1565796c8dcSSimon Schubert if ((*handler_p)->arch_info == arch_info
1575796c8dcSSimon Schubert && (*handler_p)->osabi == osabi)
1585796c8dcSSimon Schubert {
1595796c8dcSSimon Schubert internal_error
1605796c8dcSSimon Schubert (__FILE__, __LINE__,
1615796c8dcSSimon Schubert _("gdbarch_register_osabi: A handler for OS ABI \"%s\" "
1625796c8dcSSimon Schubert "has already been registered for architecture %s"),
1635796c8dcSSimon Schubert gdbarch_osabi_name (osabi),
1645796c8dcSSimon Schubert arch_info->printable_name);
1655796c8dcSSimon Schubert /* If user wants to continue, override previous definition. */
1665796c8dcSSimon Schubert (*handler_p)->init_osabi = init_osabi;
1675796c8dcSSimon Schubert return;
1685796c8dcSSimon Schubert }
1695796c8dcSSimon Schubert }
1705796c8dcSSimon Schubert
1715796c8dcSSimon Schubert (*handler_p)
1725796c8dcSSimon Schubert = (struct gdb_osabi_handler *) xmalloc (sizeof (struct gdb_osabi_handler));
1735796c8dcSSimon Schubert (*handler_p)->next = NULL;
1745796c8dcSSimon Schubert (*handler_p)->arch_info = arch_info;
1755796c8dcSSimon Schubert (*handler_p)->osabi = osabi;
1765796c8dcSSimon Schubert (*handler_p)->init_osabi = init_osabi;
1775796c8dcSSimon Schubert
1785796c8dcSSimon Schubert /* Add this OS ABI to the list of enum values for "set osabi", if it isn't
1795796c8dcSSimon Schubert already there. */
1805796c8dcSSimon Schubert for (name_ptr = gdb_osabi_available_names; *name_ptr; name_ptr ++)
1815796c8dcSSimon Schubert {
1825796c8dcSSimon Schubert if (*name_ptr == gdbarch_osabi_name (osabi))
1835796c8dcSSimon Schubert return;
1845796c8dcSSimon Schubert }
1855796c8dcSSimon Schubert *name_ptr++ = gdbarch_osabi_name (osabi);
1865796c8dcSSimon Schubert *name_ptr = NULL;
1875796c8dcSSimon Schubert }
1885796c8dcSSimon Schubert
1895796c8dcSSimon Schubert
1905796c8dcSSimon Schubert /* Sniffer to find the OS ABI for a given file's architecture and flavour.
1915796c8dcSSimon Schubert It is legal to have multiple sniffers for each arch/flavour pair, to
1925796c8dcSSimon Schubert disambiguate one OS's a.out from another, for example. The first sniffer
1935796c8dcSSimon Schubert to return something other than GDB_OSABI_UNKNOWN wins, so a sniffer should
1945796c8dcSSimon Schubert be careful to claim a file only if it knows for sure what it is. */
1955796c8dcSSimon Schubert struct gdb_osabi_sniffer
1965796c8dcSSimon Schubert {
1975796c8dcSSimon Schubert struct gdb_osabi_sniffer *next;
1985796c8dcSSimon Schubert enum bfd_architecture arch; /* bfd_arch_unknown == wildcard */
1995796c8dcSSimon Schubert enum bfd_flavour flavour;
2005796c8dcSSimon Schubert enum gdb_osabi (*sniffer)(bfd *);
2015796c8dcSSimon Schubert };
2025796c8dcSSimon Schubert
2035796c8dcSSimon Schubert static struct gdb_osabi_sniffer *gdb_osabi_sniffer_list;
2045796c8dcSSimon Schubert
2055796c8dcSSimon Schubert void
gdbarch_register_osabi_sniffer(enum bfd_architecture arch,enum bfd_flavour flavour,enum gdb_osabi (* sniffer_fn)(bfd *))2065796c8dcSSimon Schubert gdbarch_register_osabi_sniffer (enum bfd_architecture arch,
2075796c8dcSSimon Schubert enum bfd_flavour flavour,
2085796c8dcSSimon Schubert enum gdb_osabi (*sniffer_fn)(bfd *))
2095796c8dcSSimon Schubert {
2105796c8dcSSimon Schubert struct gdb_osabi_sniffer *sniffer;
2115796c8dcSSimon Schubert
2125796c8dcSSimon Schubert sniffer =
2135796c8dcSSimon Schubert (struct gdb_osabi_sniffer *) xmalloc (sizeof (struct gdb_osabi_sniffer));
2145796c8dcSSimon Schubert sniffer->arch = arch;
2155796c8dcSSimon Schubert sniffer->flavour = flavour;
2165796c8dcSSimon Schubert sniffer->sniffer = sniffer_fn;
2175796c8dcSSimon Schubert
2185796c8dcSSimon Schubert sniffer->next = gdb_osabi_sniffer_list;
2195796c8dcSSimon Schubert gdb_osabi_sniffer_list = sniffer;
2205796c8dcSSimon Schubert }
2215796c8dcSSimon Schubert
2225796c8dcSSimon Schubert
2235796c8dcSSimon Schubert enum gdb_osabi
gdbarch_lookup_osabi(bfd * abfd)2245796c8dcSSimon Schubert gdbarch_lookup_osabi (bfd *abfd)
2255796c8dcSSimon Schubert {
2265796c8dcSSimon Schubert struct gdb_osabi_sniffer *sniffer;
2275796c8dcSSimon Schubert enum gdb_osabi osabi, match;
2285796c8dcSSimon Schubert int match_specific;
2295796c8dcSSimon Schubert
2305796c8dcSSimon Schubert /* If we aren't in "auto" mode, return the specified OS ABI. */
2315796c8dcSSimon Schubert if (user_osabi_state == osabi_user)
2325796c8dcSSimon Schubert return user_selected_osabi;
2335796c8dcSSimon Schubert
2345796c8dcSSimon Schubert /* If we don't have a binary, just return unknown. The caller may
2355796c8dcSSimon Schubert have other sources the OSABI can be extracted from, e.g., the
2365796c8dcSSimon Schubert target description. */
2375796c8dcSSimon Schubert if (abfd == NULL)
2385796c8dcSSimon Schubert return GDB_OSABI_UNKNOWN;
2395796c8dcSSimon Schubert
2405796c8dcSSimon Schubert match = GDB_OSABI_UNKNOWN;
2415796c8dcSSimon Schubert match_specific = 0;
2425796c8dcSSimon Schubert
2435796c8dcSSimon Schubert for (sniffer = gdb_osabi_sniffer_list; sniffer != NULL;
2445796c8dcSSimon Schubert sniffer = sniffer->next)
2455796c8dcSSimon Schubert {
2465796c8dcSSimon Schubert if ((sniffer->arch == bfd_arch_unknown /* wildcard */
2475796c8dcSSimon Schubert || sniffer->arch == bfd_get_arch (abfd))
2485796c8dcSSimon Schubert && sniffer->flavour == bfd_get_flavour (abfd))
2495796c8dcSSimon Schubert {
2505796c8dcSSimon Schubert osabi = (*sniffer->sniffer) (abfd);
2515796c8dcSSimon Schubert if (osabi < GDB_OSABI_UNKNOWN || osabi >= GDB_OSABI_INVALID)
2525796c8dcSSimon Schubert {
2535796c8dcSSimon Schubert internal_error
2545796c8dcSSimon Schubert (__FILE__, __LINE__,
2555796c8dcSSimon Schubert _("gdbarch_lookup_osabi: invalid OS ABI (%d) from sniffer "
2565796c8dcSSimon Schubert "for architecture %s flavour %d"),
2575796c8dcSSimon Schubert (int) osabi,
2585796c8dcSSimon Schubert bfd_printable_arch_mach (bfd_get_arch (abfd), 0),
2595796c8dcSSimon Schubert (int) bfd_get_flavour (abfd));
2605796c8dcSSimon Schubert }
2615796c8dcSSimon Schubert else if (osabi != GDB_OSABI_UNKNOWN)
2625796c8dcSSimon Schubert {
2635796c8dcSSimon Schubert /* A specific sniffer always overrides a generic sniffer.
2645796c8dcSSimon Schubert Croak on multiple match if the two matches are of the
2655796c8dcSSimon Schubert same class. If the user wishes to continue, we'll use
2665796c8dcSSimon Schubert the first match. */
2675796c8dcSSimon Schubert if (match != GDB_OSABI_UNKNOWN)
2685796c8dcSSimon Schubert {
2695796c8dcSSimon Schubert if ((match_specific && sniffer->arch != bfd_arch_unknown)
2705796c8dcSSimon Schubert || (!match_specific && sniffer->arch == bfd_arch_unknown))
2715796c8dcSSimon Schubert {
2725796c8dcSSimon Schubert internal_error
2735796c8dcSSimon Schubert (__FILE__, __LINE__,
2745796c8dcSSimon Schubert _("gdbarch_lookup_osabi: multiple %sspecific OS ABI "
2755796c8dcSSimon Schubert "match for architecture %s flavour %d: first "
2765796c8dcSSimon Schubert "match \"%s\", second match \"%s\""),
2775796c8dcSSimon Schubert match_specific ? "" : "non-",
2785796c8dcSSimon Schubert bfd_printable_arch_mach (bfd_get_arch (abfd), 0),
2795796c8dcSSimon Schubert (int) bfd_get_flavour (abfd),
2805796c8dcSSimon Schubert gdbarch_osabi_name (match),
2815796c8dcSSimon Schubert gdbarch_osabi_name (osabi));
2825796c8dcSSimon Schubert }
2835796c8dcSSimon Schubert else if (sniffer->arch != bfd_arch_unknown)
2845796c8dcSSimon Schubert {
2855796c8dcSSimon Schubert match = osabi;
2865796c8dcSSimon Schubert match_specific = 1;
2875796c8dcSSimon Schubert }
2885796c8dcSSimon Schubert }
2895796c8dcSSimon Schubert else
2905796c8dcSSimon Schubert {
2915796c8dcSSimon Schubert match = osabi;
2925796c8dcSSimon Schubert if (sniffer->arch != bfd_arch_unknown)
2935796c8dcSSimon Schubert match_specific = 1;
2945796c8dcSSimon Schubert }
2955796c8dcSSimon Schubert }
2965796c8dcSSimon Schubert }
2975796c8dcSSimon Schubert }
2985796c8dcSSimon Schubert
2995796c8dcSSimon Schubert return match;
3005796c8dcSSimon Schubert }
3015796c8dcSSimon Schubert
3025796c8dcSSimon Schubert
3035796c8dcSSimon Schubert /* Return non-zero if architecture A can run code written for
3045796c8dcSSimon Schubert architecture B. */
3055796c8dcSSimon Schubert static int
can_run_code_for(const struct bfd_arch_info * a,const struct bfd_arch_info * b)3065796c8dcSSimon Schubert can_run_code_for (const struct bfd_arch_info *a, const struct bfd_arch_info *b)
3075796c8dcSSimon Schubert {
3085796c8dcSSimon Schubert /* BFD's 'A->compatible (A, B)' functions return zero if A and B are
3095796c8dcSSimon Schubert incompatible. But if they are compatible, it returns the 'more
3105796c8dcSSimon Schubert featureful' of the two arches. That is, if A can run code
3115796c8dcSSimon Schubert written for B, but B can't run code written for A, then it'll
3125796c8dcSSimon Schubert return A.
3135796c8dcSSimon Schubert
3145796c8dcSSimon Schubert struct bfd_arch_info objects are singletons: that is, there's
3155796c8dcSSimon Schubert supposed to be exactly one instance for a given machine. So you
3165796c8dcSSimon Schubert can tell whether two are equivalent by comparing pointers. */
3175796c8dcSSimon Schubert return (a == b || a->compatible (a, b) == a);
3185796c8dcSSimon Schubert }
3195796c8dcSSimon Schubert
3205796c8dcSSimon Schubert
3215796c8dcSSimon Schubert void
gdbarch_init_osabi(struct gdbarch_info info,struct gdbarch * gdbarch)3225796c8dcSSimon Schubert gdbarch_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
3235796c8dcSSimon Schubert {
3245796c8dcSSimon Schubert struct gdb_osabi_handler *handler;
3255796c8dcSSimon Schubert
3265796c8dcSSimon Schubert if (info.osabi == GDB_OSABI_UNKNOWN)
3275796c8dcSSimon Schubert {
3285796c8dcSSimon Schubert /* Don't complain about an unknown OSABI. Assume the user knows
3295796c8dcSSimon Schubert what they are doing. */
3305796c8dcSSimon Schubert return;
3315796c8dcSSimon Schubert }
3325796c8dcSSimon Schubert
3335796c8dcSSimon Schubert for (handler = gdb_osabi_handler_list; handler != NULL;
3345796c8dcSSimon Schubert handler = handler->next)
3355796c8dcSSimon Schubert {
3365796c8dcSSimon Schubert if (handler->osabi != info.osabi)
3375796c8dcSSimon Schubert continue;
3385796c8dcSSimon Schubert
3395796c8dcSSimon Schubert /* If the architecture described by ARCH_INFO can run code for
3405796c8dcSSimon Schubert the architcture we registered the handler for, then the
3415796c8dcSSimon Schubert handler is applicable. Note, though, that if the handler is
3425796c8dcSSimon Schubert for an architecture that is a superset of ARCH_INFO, we can't
3435796c8dcSSimon Schubert use that --- it would be perfectly correct for it to install
3445796c8dcSSimon Schubert gdbarch methods that refer to registers / instructions /
3455796c8dcSSimon Schubert other facilities ARCH_INFO doesn't have.
3465796c8dcSSimon Schubert
3475796c8dcSSimon Schubert NOTE: kettenis/20021027: There may be more than one machine
3485796c8dcSSimon Schubert type that is compatible with the desired machine type. Right
3495796c8dcSSimon Schubert now we simply return the first match, which is fine for now.
3505796c8dcSSimon Schubert However, we might want to do something smarter in the future. */
3515796c8dcSSimon Schubert /* NOTE: cagney/2003-10-23: The code for "a can_run_code_for b"
3525796c8dcSSimon Schubert is implemented using BFD's compatible method (a->compatible
3535796c8dcSSimon Schubert (b) == a -- the lowest common denominator between a and b is
3545796c8dcSSimon Schubert a). That method's definition of compatible may not be as you
3555796c8dcSSimon Schubert expect. For instance the test "amd64 can run code for i386"
3565796c8dcSSimon Schubert (or more generally "64-bit ISA can run code for the 32-bit
3575796c8dcSSimon Schubert ISA"). BFD doesn't normally consider 32-bit and 64-bit
3585796c8dcSSimon Schubert "compatible" so it doesn't succeed. */
3595796c8dcSSimon Schubert if (can_run_code_for (info.bfd_arch_info, handler->arch_info))
3605796c8dcSSimon Schubert {
3615796c8dcSSimon Schubert (*handler->init_osabi) (info, gdbarch);
3625796c8dcSSimon Schubert return;
3635796c8dcSSimon Schubert }
3645796c8dcSSimon Schubert }
3655796c8dcSSimon Schubert
3665796c8dcSSimon Schubert warning
3675796c8dcSSimon Schubert ("A handler for the OS ABI \"%s\" is not built into this configuration\n"
3685796c8dcSSimon Schubert "of GDB. Attempting to continue with the default %s settings.\n",
3695796c8dcSSimon Schubert gdbarch_osabi_name (info.osabi),
3705796c8dcSSimon Schubert info.bfd_arch_info->printable_name);
3715796c8dcSSimon Schubert }
3725796c8dcSSimon Schubert
3735796c8dcSSimon Schubert /* Limit on the amount of data to be read. */
3745796c8dcSSimon Schubert #define MAX_NOTESZ 128
3755796c8dcSSimon Schubert
376ef5ccd6cSJohn Marino /* Return non-zero if NOTE matches NAME, DESCSZ and TYPE. If
377ef5ccd6cSJohn Marino *SECTSIZE is non-zero, then this reads that many bytes from
378ef5ccd6cSJohn Marino the start of the section and clears *SECTSIZE. */
3795796c8dcSSimon Schubert
3805796c8dcSSimon Schubert static int
check_note(bfd * abfd,asection * sect,char * note,unsigned int * sectsize,const char * name,unsigned long descsz,unsigned long type)381ef5ccd6cSJohn Marino check_note (bfd *abfd, asection *sect, char *note, unsigned int *sectsize,
3825796c8dcSSimon Schubert const char *name, unsigned long descsz, unsigned long type)
3835796c8dcSSimon Schubert {
3845796c8dcSSimon Schubert unsigned long notesz;
3855796c8dcSSimon Schubert
386ef5ccd6cSJohn Marino if (*sectsize)
387ef5ccd6cSJohn Marino {
388ef5ccd6cSJohn Marino if (!bfd_get_section_contents (abfd, sect, note, 0, *sectsize))
389ef5ccd6cSJohn Marino return 0;
390ef5ccd6cSJohn Marino *sectsize = 0;
391ef5ccd6cSJohn Marino }
392ef5ccd6cSJohn Marino
3935796c8dcSSimon Schubert /* Calculate the size of this note. */
3945796c8dcSSimon Schubert notesz = strlen (name) + 1;
3955796c8dcSSimon Schubert notesz = ((notesz + 3) & ~3);
3965796c8dcSSimon Schubert notesz += descsz;
3975796c8dcSSimon Schubert notesz = ((notesz + 3) & ~3);
3985796c8dcSSimon Schubert
3995796c8dcSSimon Schubert /* If this assertion triggers, increase MAX_NOTESZ. */
4005796c8dcSSimon Schubert gdb_assert (notesz <= MAX_NOTESZ);
4015796c8dcSSimon Schubert
4025796c8dcSSimon Schubert /* Check whether SECT is big enough to comtain the complete note. */
4035796c8dcSSimon Schubert if (notesz > bfd_section_size (abfd, sect))
4045796c8dcSSimon Schubert return 0;
4055796c8dcSSimon Schubert
4065796c8dcSSimon Schubert /* Check the note name. */
4075796c8dcSSimon Schubert if (bfd_h_get_32 (abfd, note) != (strlen (name) + 1)
4085796c8dcSSimon Schubert || strcmp (note + 12, name) != 0)
4095796c8dcSSimon Schubert return 0;
4105796c8dcSSimon Schubert
4115796c8dcSSimon Schubert /* Check the descriptor size. */
4125796c8dcSSimon Schubert if (bfd_h_get_32 (abfd, note + 4) != descsz)
4135796c8dcSSimon Schubert return 0;
4145796c8dcSSimon Schubert
4155796c8dcSSimon Schubert /* Check the note type. */
4165796c8dcSSimon Schubert if (bfd_h_get_32 (abfd, note + 8) != type)
4175796c8dcSSimon Schubert return 0;
4185796c8dcSSimon Schubert
4195796c8dcSSimon Schubert return 1;
4205796c8dcSSimon Schubert }
4215796c8dcSSimon Schubert
4225796c8dcSSimon Schubert /* Generic sniffer for ELF flavoured files. */
4235796c8dcSSimon Schubert
4245796c8dcSSimon Schubert void
generic_elf_osabi_sniff_abi_tag_sections(bfd * abfd,asection * sect,void * obj)4255796c8dcSSimon Schubert generic_elf_osabi_sniff_abi_tag_sections (bfd *abfd, asection *sect, void *obj)
4265796c8dcSSimon Schubert {
4275796c8dcSSimon Schubert enum gdb_osabi *osabi = obj;
4285796c8dcSSimon Schubert const char *name;
4295796c8dcSSimon Schubert unsigned int sectsize;
4305796c8dcSSimon Schubert char *note;
4315796c8dcSSimon Schubert
4325796c8dcSSimon Schubert name = bfd_get_section_name (abfd, sect);
4335796c8dcSSimon Schubert sectsize = bfd_section_size (abfd, sect);
4345796c8dcSSimon Schubert
4355796c8dcSSimon Schubert /* Limit the amount of data to read. */
4365796c8dcSSimon Schubert if (sectsize > MAX_NOTESZ)
4375796c8dcSSimon Schubert sectsize = MAX_NOTESZ;
4385796c8dcSSimon Schubert
439ef5ccd6cSJohn Marino /* We lazily read the section data here. Since we use
440ef5ccd6cSJohn Marino BFD_DECOMPRESS, we can't use bfd_get_section_contents on a
441ef5ccd6cSJohn Marino compressed section. But, since note sections are not compressed,
442ef5ccd6cSJohn Marino deferring the reading until we recognize the section avoids any
443ef5ccd6cSJohn Marino error. */
4445796c8dcSSimon Schubert note = alloca (sectsize);
4455796c8dcSSimon Schubert
4465796c8dcSSimon Schubert /* .note.ABI-tag notes, used by GNU/Linux and FreeBSD. */
4475796c8dcSSimon Schubert if (strcmp (name, ".note.ABI-tag") == 0)
4485796c8dcSSimon Schubert {
4495796c8dcSSimon Schubert /* GNU. */
450ef5ccd6cSJohn Marino if (check_note (abfd, sect, note, §size, "GNU", 16, NT_GNU_ABI_TAG))
4515796c8dcSSimon Schubert {
4525796c8dcSSimon Schubert unsigned int abi_tag = bfd_h_get_32 (abfd, note + 16);
4535796c8dcSSimon Schubert
4545796c8dcSSimon Schubert switch (abi_tag)
4555796c8dcSSimon Schubert {
4565796c8dcSSimon Schubert case GNU_ABI_TAG_LINUX:
4575796c8dcSSimon Schubert *osabi = GDB_OSABI_LINUX;
4585796c8dcSSimon Schubert break;
4595796c8dcSSimon Schubert
4605796c8dcSSimon Schubert case GNU_ABI_TAG_HURD:
4615796c8dcSSimon Schubert *osabi = GDB_OSABI_HURD;
4625796c8dcSSimon Schubert break;
4635796c8dcSSimon Schubert
4645796c8dcSSimon Schubert case GNU_ABI_TAG_SOLARIS:
4655796c8dcSSimon Schubert *osabi = GDB_OSABI_SOLARIS;
4665796c8dcSSimon Schubert break;
4675796c8dcSSimon Schubert
4685796c8dcSSimon Schubert case GNU_ABI_TAG_FREEBSD:
4695796c8dcSSimon Schubert *osabi = GDB_OSABI_FREEBSD_ELF;
4705796c8dcSSimon Schubert break;
4715796c8dcSSimon Schubert
4725796c8dcSSimon Schubert case GNU_ABI_TAG_NETBSD:
4735796c8dcSSimon Schubert *osabi = GDB_OSABI_NETBSD_ELF;
4745796c8dcSSimon Schubert break;
4755796c8dcSSimon Schubert
4765796c8dcSSimon Schubert default:
477c50c785cSJohn Marino internal_error (__FILE__, __LINE__,
478c50c785cSJohn Marino _("generic_elf_osabi_sniff_abi_tag_sections: "
479c50c785cSJohn Marino "unknown OS number %d"),
4805796c8dcSSimon Schubert abi_tag);
4815796c8dcSSimon Schubert }
4825796c8dcSSimon Schubert return;
4835796c8dcSSimon Schubert }
4845796c8dcSSimon Schubert
4855796c8dcSSimon Schubert /* FreeBSD. */
486ef5ccd6cSJohn Marino if (check_note (abfd, sect, note, §size, "FreeBSD", 4,
487ef5ccd6cSJohn Marino NT_FREEBSD_ABI_TAG))
4885796c8dcSSimon Schubert {
4895796c8dcSSimon Schubert /* There is no need to check the version yet. */
4905796c8dcSSimon Schubert *osabi = GDB_OSABI_FREEBSD_ELF;
4915796c8dcSSimon Schubert return;
4925796c8dcSSimon Schubert }
4935796c8dcSSimon Schubert
49469e0f06dSSimon Schubert /* DragonFly. */
495*8a286ab3SJohn Marino if (check_note (abfd, sect, note, §size, "DragonFly", 4,
496*8a286ab3SJohn Marino NT_DRAGONFLY_ABI_TAG))
49769e0f06dSSimon Schubert {
49869e0f06dSSimon Schubert /* There is no need to check the version yet. */
49969e0f06dSSimon Schubert *osabi = GDB_OSABI_DRAGONFLY;
50069e0f06dSSimon Schubert return;
50169e0f06dSSimon Schubert }
50269e0f06dSSimon Schubert
5035796c8dcSSimon Schubert return;
5045796c8dcSSimon Schubert }
5055796c8dcSSimon Schubert
5065796c8dcSSimon Schubert /* .note.netbsd.ident notes, used by NetBSD. */
5075796c8dcSSimon Schubert if (strcmp (name, ".note.netbsd.ident") == 0
508ef5ccd6cSJohn Marino && check_note (abfd, sect, note, §size, "NetBSD", 4, NT_NETBSD_IDENT))
5095796c8dcSSimon Schubert {
5105796c8dcSSimon Schubert /* There is no need to check the version yet. */
5115796c8dcSSimon Schubert *osabi = GDB_OSABI_NETBSD_ELF;
5125796c8dcSSimon Schubert return;
5135796c8dcSSimon Schubert }
5145796c8dcSSimon Schubert
5155796c8dcSSimon Schubert /* .note.openbsd.ident notes, used by OpenBSD. */
5165796c8dcSSimon Schubert if (strcmp (name, ".note.openbsd.ident") == 0
517ef5ccd6cSJohn Marino && check_note (abfd, sect, note, §size, "OpenBSD", 4,
518ef5ccd6cSJohn Marino NT_OPENBSD_IDENT))
5195796c8dcSSimon Schubert {
5205796c8dcSSimon Schubert /* There is no need to check the version yet. */
5215796c8dcSSimon Schubert *osabi = GDB_OSABI_OPENBSD_ELF;
5225796c8dcSSimon Schubert return;
5235796c8dcSSimon Schubert }
5245796c8dcSSimon Schubert
5255796c8dcSSimon Schubert /* .note.netbsdcore.procinfo notes, used by NetBSD. */
5265796c8dcSSimon Schubert if (strcmp (name, ".note.netbsdcore.procinfo") == 0)
5275796c8dcSSimon Schubert {
5285796c8dcSSimon Schubert *osabi = GDB_OSABI_NETBSD_ELF;
5295796c8dcSSimon Schubert return;
5305796c8dcSSimon Schubert }
5315796c8dcSSimon Schubert }
5325796c8dcSSimon Schubert
5335796c8dcSSimon Schubert static enum gdb_osabi
generic_elf_osabi_sniffer(bfd * abfd)5345796c8dcSSimon Schubert generic_elf_osabi_sniffer (bfd *abfd)
5355796c8dcSSimon Schubert {
5365796c8dcSSimon Schubert unsigned int elfosabi;
5375796c8dcSSimon Schubert enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
5385796c8dcSSimon Schubert
5395796c8dcSSimon Schubert elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
5405796c8dcSSimon Schubert
5415796c8dcSSimon Schubert switch (elfosabi)
5425796c8dcSSimon Schubert {
5435796c8dcSSimon Schubert case ELFOSABI_NONE:
544a45ae5f8SJohn Marino case ELFOSABI_GNU:
5455796c8dcSSimon Schubert /* When the EI_OSABI field in the ELF header is ELFOSABI_NONE
5465796c8dcSSimon Schubert (0), then the ELF structures in the file are conforming to
5475796c8dcSSimon Schubert the base specification for that machine (there are no
5485796c8dcSSimon Schubert OS-specific extensions). In order to determine the real OS
549a45ae5f8SJohn Marino in use, we must look for OS-specific notes.
550a45ae5f8SJohn Marino
551a45ae5f8SJohn Marino The same applies for ELFOSABI_GNU: this can mean GNU/Hurd,
552a45ae5f8SJohn Marino GNU/Linux, and possibly more. */
5535796c8dcSSimon Schubert bfd_map_over_sections (abfd,
5545796c8dcSSimon Schubert generic_elf_osabi_sniff_abi_tag_sections,
5555796c8dcSSimon Schubert &osabi);
5565796c8dcSSimon Schubert break;
5575796c8dcSSimon Schubert
5585796c8dcSSimon Schubert case ELFOSABI_FREEBSD:
5595796c8dcSSimon Schubert osabi = GDB_OSABI_FREEBSD_ELF;
5605796c8dcSSimon Schubert break;
5615796c8dcSSimon Schubert
5625796c8dcSSimon Schubert case ELFOSABI_NETBSD:
5635796c8dcSSimon Schubert osabi = GDB_OSABI_NETBSD_ELF;
5645796c8dcSSimon Schubert break;
5655796c8dcSSimon Schubert
5665796c8dcSSimon Schubert case ELFOSABI_SOLARIS:
5675796c8dcSSimon Schubert osabi = GDB_OSABI_SOLARIS;
5685796c8dcSSimon Schubert break;
5695796c8dcSSimon Schubert
5705796c8dcSSimon Schubert case ELFOSABI_HPUX:
5715796c8dcSSimon Schubert /* For some reason the default value for the EI_OSABI field is
5725796c8dcSSimon Schubert ELFOSABI_HPUX for all PA-RISC targets (with the exception of
5735796c8dcSSimon Schubert GNU/Linux). We use HP-UX ELF as the default, but let any
5745796c8dcSSimon Schubert OS-specific notes override this. */
5755796c8dcSSimon Schubert osabi = GDB_OSABI_HPUX_ELF;
5765796c8dcSSimon Schubert bfd_map_over_sections (abfd,
5775796c8dcSSimon Schubert generic_elf_osabi_sniff_abi_tag_sections,
5785796c8dcSSimon Schubert &osabi);
5795796c8dcSSimon Schubert break;
580ef5ccd6cSJohn Marino
581ef5ccd6cSJohn Marino case ELFOSABI_OPENVMS:
582ef5ccd6cSJohn Marino osabi = GDB_OSABI_OPENVMS;
583ef5ccd6cSJohn Marino break;
5845796c8dcSSimon Schubert }
5855796c8dcSSimon Schubert
5865796c8dcSSimon Schubert if (osabi == GDB_OSABI_UNKNOWN)
5875796c8dcSSimon Schubert {
5885796c8dcSSimon Schubert /* The FreeBSD folks have been naughty; they stored the string
5895796c8dcSSimon Schubert "FreeBSD" in the padding of the e_ident field of the ELF
5905796c8dcSSimon Schubert header to "brand" their ELF binaries in FreeBSD 3.x. */
5915796c8dcSSimon Schubert if (memcmp (&elf_elfheader (abfd)->e_ident[8],
5925796c8dcSSimon Schubert "FreeBSD", sizeof ("FreeBSD")) == 0)
5935796c8dcSSimon Schubert osabi = GDB_OSABI_FREEBSD_ELF;
5945796c8dcSSimon Schubert }
5955796c8dcSSimon Schubert
5965796c8dcSSimon Schubert return osabi;
5975796c8dcSSimon Schubert }
5985796c8dcSSimon Schubert
5995796c8dcSSimon Schubert static void
set_osabi(char * args,int from_tty,struct cmd_list_element * c)6005796c8dcSSimon Schubert set_osabi (char *args, int from_tty, struct cmd_list_element *c)
6015796c8dcSSimon Schubert {
6025796c8dcSSimon Schubert struct gdbarch_info info;
6035796c8dcSSimon Schubert
6045796c8dcSSimon Schubert if (strcmp (set_osabi_string, "auto") == 0)
6055796c8dcSSimon Schubert user_osabi_state = osabi_auto;
6065796c8dcSSimon Schubert else if (strcmp (set_osabi_string, "default") == 0)
6075796c8dcSSimon Schubert {
6085796c8dcSSimon Schubert user_selected_osabi = GDB_OSABI_DEFAULT;
6095796c8dcSSimon Schubert user_osabi_state = osabi_user;
6105796c8dcSSimon Schubert }
6115796c8dcSSimon Schubert else if (strcmp (set_osabi_string, "none") == 0)
6125796c8dcSSimon Schubert {
6135796c8dcSSimon Schubert user_selected_osabi = GDB_OSABI_UNKNOWN;
6145796c8dcSSimon Schubert user_osabi_state = osabi_user;
6155796c8dcSSimon Schubert }
6165796c8dcSSimon Schubert else
6175796c8dcSSimon Schubert {
6185796c8dcSSimon Schubert int i;
619cf7f2e2dSJohn Marino
6205796c8dcSSimon Schubert for (i = 1; i < GDB_OSABI_INVALID; i++)
6215796c8dcSSimon Schubert if (strcmp (set_osabi_string, gdbarch_osabi_name (i)) == 0)
6225796c8dcSSimon Schubert {
6235796c8dcSSimon Schubert user_selected_osabi = i;
6245796c8dcSSimon Schubert user_osabi_state = osabi_user;
6255796c8dcSSimon Schubert break;
6265796c8dcSSimon Schubert }
6275796c8dcSSimon Schubert if (i == GDB_OSABI_INVALID)
6285796c8dcSSimon Schubert internal_error (__FILE__, __LINE__,
6295796c8dcSSimon Schubert _("Invalid OS ABI \"%s\" passed to command handler."),
6305796c8dcSSimon Schubert set_osabi_string);
6315796c8dcSSimon Schubert }
6325796c8dcSSimon Schubert
6335796c8dcSSimon Schubert /* NOTE: At some point (true multiple architectures) we'll need to be more
6345796c8dcSSimon Schubert graceful here. */
6355796c8dcSSimon Schubert gdbarch_info_init (&info);
6365796c8dcSSimon Schubert if (! gdbarch_update_p (info))
6375796c8dcSSimon Schubert internal_error (__FILE__, __LINE__, _("Updating OS ABI failed."));
6385796c8dcSSimon Schubert }
6395796c8dcSSimon Schubert
6405796c8dcSSimon Schubert static void
show_osabi(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)6415796c8dcSSimon Schubert show_osabi (struct ui_file *file, int from_tty, struct cmd_list_element *c,
6425796c8dcSSimon Schubert const char *value)
6435796c8dcSSimon Schubert {
6445796c8dcSSimon Schubert if (user_osabi_state == osabi_auto)
6455796c8dcSSimon Schubert fprintf_filtered (file,
646c50c785cSJohn Marino _("The current OS ABI is \"auto\" "
647c50c785cSJohn Marino "(currently \"%s\").\n"),
6485796c8dcSSimon Schubert gdbarch_osabi_name (gdbarch_osabi (get_current_arch ())));
6495796c8dcSSimon Schubert else
6505796c8dcSSimon Schubert fprintf_filtered (file, _("The current OS ABI is \"%s\".\n"),
6515796c8dcSSimon Schubert gdbarch_osabi_name (user_selected_osabi));
6525796c8dcSSimon Schubert
6535796c8dcSSimon Schubert if (GDB_OSABI_DEFAULT != GDB_OSABI_UNKNOWN)
6545796c8dcSSimon Schubert fprintf_filtered (file, _("The default OS ABI is \"%s\".\n"),
6555796c8dcSSimon Schubert gdbarch_osabi_name (GDB_OSABI_DEFAULT));
6565796c8dcSSimon Schubert }
6575796c8dcSSimon Schubert
6585796c8dcSSimon Schubert extern initialize_file_ftype _initialize_gdb_osabi; /* -Wmissing-prototype */
6595796c8dcSSimon Schubert
6605796c8dcSSimon Schubert void
_initialize_gdb_osabi(void)6615796c8dcSSimon Schubert _initialize_gdb_osabi (void)
6625796c8dcSSimon Schubert {
6635796c8dcSSimon Schubert if (strcmp (gdb_osabi_names[GDB_OSABI_INVALID], "<invalid>") != 0)
6645796c8dcSSimon Schubert internal_error
6655796c8dcSSimon Schubert (__FILE__, __LINE__,
6665796c8dcSSimon Schubert _("_initialize_gdb_osabi: gdb_osabi_names[] is inconsistent"));
6675796c8dcSSimon Schubert
6685796c8dcSSimon Schubert /* Register a generic sniffer for ELF flavoured files. */
6695796c8dcSSimon Schubert gdbarch_register_osabi_sniffer (bfd_arch_unknown,
6705796c8dcSSimon Schubert bfd_target_elf_flavour,
6715796c8dcSSimon Schubert generic_elf_osabi_sniffer);
6725796c8dcSSimon Schubert
6735796c8dcSSimon Schubert /* Register the "set osabi" command. */
6745796c8dcSSimon Schubert add_setshow_enum_cmd ("osabi", class_support, gdb_osabi_available_names,
675c50c785cSJohn Marino &set_osabi_string,
676c50c785cSJohn Marino _("Set OS ABI of target."),
677c50c785cSJohn Marino _("Show OS ABI of target."),
678c50c785cSJohn Marino NULL, set_osabi, show_osabi,
6795796c8dcSSimon Schubert &setlist, &showlist);
6805796c8dcSSimon Schubert user_osabi_state = osabi_auto;
6815796c8dcSSimon Schubert }
682