1 /* OS ABI variant handling for GDB.
2 Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 #include "gdb_assert.h"
24 #include "gdb_string.h"
27 #include "arch-utils.h"
33 #ifndef GDB_OSABI_DEFAULT
34 #define GDB_OSABI_DEFAULT GDB_OSABI_UNKNOWN
37 /* State for the "set osabi" command. */
38 static enum { osabi_auto, osabi_default, osabi_user } user_osabi_state;
39 static enum gdb_osabi user_selected_osabi;
40 static const char *gdb_osabi_available_names[GDB_OSABI_INVALID + 3] = {
46 static const char *set_osabi_string;
48 /* This table matches the indices assigned to enum gdb_osabi. Keep
50 static const char * const gdb_osabi_names[] =
83 gdbarch_osabi_name (enum gdb_osabi osabi)
85 if (osabi >= GDB_OSABI_UNKNOWN && osabi < GDB_OSABI_INVALID)
86 return gdb_osabi_names[osabi];
88 return gdb_osabi_names[GDB_OSABI_INVALID];
91 /* Handler for a given architecture/OS ABI pair. There should be only
92 one handler for a given OS ABI each architecture family. */
93 struct gdb_osabi_handler
95 struct gdb_osabi_handler *next;
96 const struct bfd_arch_info *arch_info;
98 void (*init_osabi)(struct gdbarch_info, struct gdbarch *);
101 static struct gdb_osabi_handler *gdb_osabi_handler_list;
104 gdbarch_register_osabi (enum bfd_architecture arch, unsigned long machine,
105 enum gdb_osabi osabi,
106 void (*init_osabi)(struct gdbarch_info,
109 struct gdb_osabi_handler **handler_p;
110 const struct bfd_arch_info *arch_info = bfd_lookup_arch (arch, machine);
111 const char **name_ptr;
113 /* Registering an OS ABI handler for "unknown" is not allowed. */
114 if (osabi == GDB_OSABI_UNKNOWN)
118 "gdbarch_register_osabi: An attempt to register a handler for "
119 "OS ABI \"%s\" for architecture %s was made. The handler will "
121 gdbarch_osabi_name (osabi),
122 bfd_printable_arch_mach (arch, machine));
126 gdb_assert (arch_info);
128 for (handler_p = &gdb_osabi_handler_list; *handler_p != NULL;
129 handler_p = &(*handler_p)->next)
131 if ((*handler_p)->arch_info == arch_info
132 && (*handler_p)->osabi == osabi)
136 "gdbarch_register_osabi: A handler for OS ABI \"%s\" "
137 "has already been registered for architecture %s",
138 gdbarch_osabi_name (osabi),
139 arch_info->printable_name);
140 /* If user wants to continue, override previous definition. */
141 (*handler_p)->init_osabi = init_osabi;
147 = (struct gdb_osabi_handler *) xmalloc (sizeof (struct gdb_osabi_handler));
148 (*handler_p)->next = NULL;
149 (*handler_p)->arch_info = arch_info;
150 (*handler_p)->osabi = osabi;
151 (*handler_p)->init_osabi = init_osabi;
153 /* Add this OS ABI to the list of enum values for "set osabi", if it isn't
155 for (name_ptr = gdb_osabi_available_names; *name_ptr; name_ptr ++)
157 if (*name_ptr == gdbarch_osabi_name (osabi))
160 *name_ptr++ = gdbarch_osabi_name (osabi);
165 /* Sniffer to find the OS ABI for a given file's architecture and flavour.
166 It is legal to have multiple sniffers for each arch/flavour pair, to
167 disambiguate one OS's a.out from another, for example. The first sniffer
168 to return something other than GDB_OSABI_UNKNOWN wins, so a sniffer should
169 be careful to claim a file only if it knows for sure what it is. */
170 struct gdb_osabi_sniffer
172 struct gdb_osabi_sniffer *next;
173 enum bfd_architecture arch; /* bfd_arch_unknown == wildcard */
174 enum bfd_flavour flavour;
175 enum gdb_osabi (*sniffer)(bfd *);
178 static struct gdb_osabi_sniffer *gdb_osabi_sniffer_list;
181 gdbarch_register_osabi_sniffer (enum bfd_architecture arch,
182 enum bfd_flavour flavour,
183 enum gdb_osabi (*sniffer_fn)(bfd *))
185 struct gdb_osabi_sniffer *sniffer;
188 (struct gdb_osabi_sniffer *) xmalloc (sizeof (struct gdb_osabi_sniffer));
189 sniffer->arch = arch;
190 sniffer->flavour = flavour;
191 sniffer->sniffer = sniffer_fn;
193 sniffer->next = gdb_osabi_sniffer_list;
194 gdb_osabi_sniffer_list = sniffer;
199 gdbarch_lookup_osabi (bfd *abfd)
201 struct gdb_osabi_sniffer *sniffer;
202 enum gdb_osabi osabi, match;
205 /* If we aren't in "auto" mode, return the specified OS ABI. */
206 if (user_osabi_state == osabi_user)
207 return user_selected_osabi;
209 /* If we don't have a binary, return the default OS ABI (if set) or
210 an inconclusive result (otherwise). */
213 if (GDB_OSABI_DEFAULT != GDB_OSABI_UNKNOWN)
214 return GDB_OSABI_DEFAULT;
216 return GDB_OSABI_UNINITIALIZED;
219 match = GDB_OSABI_UNKNOWN;
222 for (sniffer = gdb_osabi_sniffer_list; sniffer != NULL;
223 sniffer = sniffer->next)
225 if ((sniffer->arch == bfd_arch_unknown /* wildcard */
226 || sniffer->arch == bfd_get_arch (abfd))
227 && sniffer->flavour == bfd_get_flavour (abfd))
229 osabi = (*sniffer->sniffer) (abfd);
230 if (osabi < GDB_OSABI_UNKNOWN || osabi >= GDB_OSABI_INVALID)
234 "gdbarch_lookup_osabi: invalid OS ABI (%d) from sniffer "
235 "for architecture %s flavour %d",
237 bfd_printable_arch_mach (bfd_get_arch (abfd), 0),
238 (int) bfd_get_flavour (abfd));
240 else if (osabi != GDB_OSABI_UNKNOWN)
242 /* A specific sniffer always overrides a generic sniffer.
243 Croak on multiple match if the two matches are of the
244 same class. If the user wishes to continue, we'll use
246 if (match != GDB_OSABI_UNKNOWN)
248 if ((match_specific && sniffer->arch != bfd_arch_unknown)
249 || (!match_specific && sniffer->arch == bfd_arch_unknown))
253 "gdbarch_lookup_osabi: multiple %sspecific OS ABI "
254 "match for architecture %s flavour %d: first "
255 "match \"%s\", second match \"%s\"",
256 match_specific ? "" : "non-",
257 bfd_printable_arch_mach (bfd_get_arch (abfd), 0),
258 (int) bfd_get_flavour (abfd),
259 gdbarch_osabi_name (match),
260 gdbarch_osabi_name (osabi));
262 else if (sniffer->arch != bfd_arch_unknown)
271 if (sniffer->arch != bfd_arch_unknown)
278 /* If we didn't find a match, but a default was specified at configure
279 time, return the default. */
280 if (GDB_OSABI_DEFAULT != GDB_OSABI_UNKNOWN && match == GDB_OSABI_UNKNOWN)
281 return GDB_OSABI_DEFAULT;
287 /* Return non-zero if architecture A can run code written for
290 can_run_code_for (const struct bfd_arch_info *a, const struct bfd_arch_info *b)
292 /* BFD's 'A->compatible (A, B)' functions return zero if A and B are
293 incompatible. But if they are compatible, it returns the 'more
294 featureful' of the two arches. That is, if A can run code
295 written for B, but B can't run code written for A, then it'll
298 struct bfd_arch_info objects are singletons: that is, there's
299 supposed to be exactly one instance for a given machine. So you
300 can tell whether two are equivalent by comparing pointers. */
301 return (a == b || a->compatible (a, b) == a);
306 gdbarch_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
308 struct gdb_osabi_handler *handler;
310 if (info.osabi == GDB_OSABI_UNKNOWN)
312 /* Don't complain about an unknown OSABI. Assume the user knows
313 what they are doing. */
317 for (handler = gdb_osabi_handler_list; handler != NULL;
318 handler = handler->next)
320 if (handler->osabi != info.osabi)
323 /* If the architecture described by ARCH_INFO can run code for
324 the architcture we registered the handler for, then the
325 handler is applicable. Note, though, that if the handler is
326 for an architecture that is a superset of ARCH_INFO, we can't
327 use that --- it would be perfectly correct for it to install
328 gdbarch methods that refer to registers / instructions /
329 other facilities ARCH_INFO doesn't have.
331 NOTE: kettenis/20021027: There may be more than one machine
332 type that is compatible with the desired machine type. Right
333 now we simply return the first match, which is fine for now.
334 However, we might want to do something smarter in the future. */
335 /* NOTE: cagney/2003-10-23: The code for "a can_run_code_for b"
336 is implemented using BFD's compatible method (a->compatible
337 (b) == a -- the lowest common denominator between a and b is
338 a). That method's definition of compatible may not be as you
339 expect. For instance the test "amd64 can run code for i386"
340 (or more generally "64-bit ISA can run code for the 32-bit
341 ISA"). BFD doesn't normally consider 32-bit and 64-bit
342 "compatible" so it doesn't succeed. */
343 if (can_run_code_for (info.bfd_arch_info, handler->arch_info))
345 (*handler->init_osabi) (info, gdbarch);
350 warning ("A handler for the OS ABI \"%s\" is not built into this "
351 "configuration of GDB. "
352 "Attempting to continue with the default %s settings",
353 gdbarch_osabi_name (info.osabi),
354 info.bfd_arch_info->printable_name);
358 /* Generic sniffer for ELF flavoured files. */
361 generic_elf_osabi_sniff_abi_tag_sections (bfd *abfd, asection *sect, void *obj)
363 enum gdb_osabi *os_ident_ptr = obj;
365 unsigned int sectsize;
367 name = bfd_get_section_name (abfd, sect);
368 sectsize = bfd_section_size (abfd, sect);
370 /* .note.ABI-tag notes, used by GNU/Linux and FreeBSD. */
371 if (strcmp (name, ".note.ABI-tag") == 0 && sectsize > 0)
373 unsigned int name_length, data_length, note_type;
376 /* If the section is larger than this, it's probably not what we are
381 note = alloca (sectsize);
383 bfd_get_section_contents (abfd, sect, note,
384 (file_ptr) 0, (bfd_size_type) sectsize);
386 name_length = bfd_h_get_32 (abfd, note);
387 data_length = bfd_h_get_32 (abfd, note + 4);
388 note_type = bfd_h_get_32 (abfd, note + 8);
390 if (name_length == 4 && data_length == 16 && note_type == NT_GNU_ABI_TAG
391 && strcmp (note + 12, "GNU") == 0)
393 int os_number = bfd_h_get_32 (abfd, note + 16);
397 case GNU_ABI_TAG_LINUX:
398 *os_ident_ptr = GDB_OSABI_LINUX;
401 case GNU_ABI_TAG_HURD:
402 *os_ident_ptr = GDB_OSABI_HURD;
405 case GNU_ABI_TAG_SOLARIS:
406 *os_ident_ptr = GDB_OSABI_SOLARIS;
409 case GNU_ABI_TAG_FREEBSD:
410 *os_ident_ptr = GDB_OSABI_FREEBSD_ELF;
413 case GNU_ABI_TAG_NETBSD:
414 *os_ident_ptr = GDB_OSABI_NETBSD_ELF;
420 "generic_elf_osabi_sniff_abi_tag_sections: unknown OS number %d",
425 else if (name_length == 8 && data_length == 4
426 && note_type == NT_FREEBSD_ABI_TAG
427 && strcmp (note + 12, "FreeBSD") == 0)
429 /* XXX Should we check the version here? Probably not
431 *os_ident_ptr = GDB_OSABI_FREEBSD_ELF;
436 /* .note.netbsd.ident notes, used by NetBSD. */
437 if (strcmp (name, ".note.netbsd.ident") == 0 && sectsize > 0)
439 unsigned int name_length, data_length, note_type;
442 /* If the section is larger than this, it's probably not what we are
447 note = alloca (sectsize);
449 bfd_get_section_contents (abfd, sect, note,
450 (file_ptr) 0, (bfd_size_type) sectsize);
452 name_length = bfd_h_get_32 (abfd, note);
453 data_length = bfd_h_get_32 (abfd, note + 4);
454 note_type = bfd_h_get_32 (abfd, note + 8);
456 if (name_length == 7 && data_length == 4 && note_type == NT_NETBSD_IDENT
457 && strcmp (note + 12, "NetBSD") == 0)
459 /* XXX Should we check the version here? Probably not
461 *os_ident_ptr = GDB_OSABI_NETBSD_ELF;
466 /* .note.netbsdcore.procinfo notes, used by NetBSD. */
467 if (strcmp (name, ".note.netbsdcore.procinfo") == 0 && sectsize > 0)
469 *os_ident_ptr = GDB_OSABI_NETBSD_ELF;
474 static enum gdb_osabi
475 generic_elf_osabi_sniffer (bfd *abfd)
477 unsigned int elfosabi;
478 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
480 elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
485 /* When elfosabi is ELFOSABI_NONE (0), then the ELF structures in the
486 file are conforming to the base specification for that machine
487 (there are no OS-specific extensions). In order to determine the
488 real OS in use we must look for OS notes that have been added. */
489 bfd_map_over_sections (abfd,
490 generic_elf_osabi_sniff_abi_tag_sections,
494 case ELFOSABI_FREEBSD:
495 osabi = GDB_OSABI_FREEBSD_ELF;
498 case ELFOSABI_NETBSD:
499 osabi = GDB_OSABI_NETBSD_ELF;
503 osabi = GDB_OSABI_LINUX;
507 osabi = GDB_OSABI_HURD;
510 case ELFOSABI_SOLARIS:
511 osabi = GDB_OSABI_SOLARIS;
515 osabi = GDB_OSABI_HPUX_ELF;
519 if (osabi == GDB_OSABI_UNKNOWN)
521 /* The FreeBSD folks have been naughty; they stored the string
522 "FreeBSD" in the padding of the e_ident field of the ELF
523 header to "brand" their ELF binaries in FreeBSD 3.x. */
524 if (strcmp (&elf_elfheader (abfd)->e_ident[8], "FreeBSD") == 0)
525 osabi = GDB_OSABI_FREEBSD_ELF;
532 set_osabi (char *args, int from_tty, struct cmd_list_element *c)
534 struct gdbarch_info info;
536 if (strcmp (set_osabi_string, "auto") == 0)
537 user_osabi_state = osabi_auto;
538 else if (strcmp (set_osabi_string, "default") == 0)
540 user_selected_osabi = GDB_OSABI_DEFAULT;
541 user_osabi_state = osabi_user;
543 else if (strcmp (set_osabi_string, "none") == 0)
545 user_selected_osabi = GDB_OSABI_UNKNOWN;
546 user_osabi_state = osabi_user;
551 for (i = 1; i < GDB_OSABI_INVALID; i++)
552 if (strcmp (set_osabi_string, gdbarch_osabi_name (i)) == 0)
554 user_selected_osabi = i;
555 user_osabi_state = osabi_user;
558 if (i == GDB_OSABI_INVALID)
559 internal_error (__FILE__, __LINE__,
560 "Invalid OS ABI \"%s\" passed to command handler.",
564 /* NOTE: At some point (true multiple architectures) we'll need to be more
566 gdbarch_info_init (&info);
567 if (! gdbarch_update_p (info))
568 internal_error (__FILE__, __LINE__, "Updating OS ABI failed.");
572 show_osabi (char *args, int from_tty)
574 if (user_osabi_state == osabi_auto)
575 printf_filtered ("The current OS ABI is \"auto\" (currently \"%s\").\n",
576 gdbarch_osabi_name (gdbarch_osabi (current_gdbarch)));
578 printf_filtered ("The current OS ABI is \"%s\".\n",
579 gdbarch_osabi_name (user_selected_osabi));
581 if (GDB_OSABI_DEFAULT != GDB_OSABI_UNKNOWN)
582 printf_filtered ("The default OS ABI is \"%s\".\n",
583 gdbarch_osabi_name (GDB_OSABI_DEFAULT));
586 extern initialize_file_ftype _initialize_gdb_osabi; /* -Wmissing-prototype */
589 _initialize_gdb_osabi (void)
591 struct cmd_list_element *c;
593 if (strcmp (gdb_osabi_names[GDB_OSABI_INVALID], "<invalid>") != 0)
596 "_initialize_gdb_osabi: gdb_osabi_names[] is inconsistent");
598 /* Register a generic sniffer for ELF flavoured files. */
599 gdbarch_register_osabi_sniffer (bfd_arch_unknown,
600 bfd_target_elf_flavour,
601 generic_elf_osabi_sniffer);
603 /* Register the "set osabi" command. */
604 c = add_set_enum_cmd ("osabi", class_support, gdb_osabi_available_names,
605 &set_osabi_string, "Set OS ABI of target.", &setlist);
607 set_cmd_sfunc (c, set_osabi);
608 add_cmd ("osabi", class_support, show_osabi, "Show OS/ABI of target.",
610 user_osabi_state = osabi_auto;