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