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