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