* readelf.c (dump_section): Don't display DEL characters.
[deliverable/binutils-gdb.git] / gdb / osabi.c
CommitLineData
70f80edf 1/* OS ABI variant handling for GDB.
4be87837 2 Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
70f80edf
JT
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
20
21#include "defs.h"
05816f70
MK
22
23#include "gdb_assert.h"
309367d4 24#include "gdb_string.h"
05816f70 25
70f80edf 26#include "osabi.h"
b00a8037
DJ
27#include "arch-utils.h"
28#include "gdbcmd.h"
29#include "command.h"
70f80edf
JT
30
31#include "elf-bfd.h"
32
b00a8037
DJ
33#ifndef GDB_OSABI_DEFAULT
34#define GDB_OSABI_DEFAULT GDB_OSABI_UNKNOWN
35#endif
36
37/* State for the "set osabi" command. */
38static enum { osabi_auto, osabi_default, osabi_user } user_osabi_state;
39static enum gdb_osabi user_selected_osabi;
40static const char *gdb_osabi_available_names[GDB_OSABI_INVALID + 3] = {
41 "auto",
42 "default",
43 "none",
44 NULL
45};
46static const char *set_osabi_string;
70f80edf
JT
47
48/* This table matches the indices assigned to enum gdb_osabi. Keep
49 them in sync. */
50static const char * const gdb_osabi_names[] =
51{
b00a8037 52 "none",
70f80edf
JT
53
54 "SVR4",
55 "GNU/Hurd",
56 "Solaris",
57 "OSF/1",
58 "GNU/Linux",
59 "FreeBSD a.out",
60 "FreeBSD ELF",
61 "NetBSD a.out",
62 "NetBSD ELF",
63 "Windows CE",
1029b7fa
MK
64 "DJGPP",
65 "NetWare",
b96d0a4e 66 "Irix",
daa66587 67 "LynxOS",
e64f66d1 68 "Interix",
a09a320d
JB
69 "HP/UX ELF",
70 "HP/UX SOM",
70f80edf
JT
71
72 "ARM EABI v1",
73 "ARM EABI v2",
74 "ARM APCS",
83461b86 75 "QNX Neutrino",
70f80edf 76
1762d96d
CV
77 "Cygwin",
78
70f80edf
JT
79 "<invalid>"
80};
81
82const char *
83gdbarch_osabi_name (enum gdb_osabi osabi)
84{
85 if (osabi >= GDB_OSABI_UNKNOWN && osabi < GDB_OSABI_INVALID)
86 return gdb_osabi_names[osabi];
87
88 return gdb_osabi_names[GDB_OSABI_INVALID];
89}
90
91/* Handler for a given architecture/OS ABI pair. There should be only
92 one handler for a given OS ABI each architecture family. */
93struct gdb_osabi_handler
94{
95 struct gdb_osabi_handler *next;
05816f70 96 const struct bfd_arch_info *arch_info;
70f80edf
JT
97 enum gdb_osabi osabi;
98 void (*init_osabi)(struct gdbarch_info, struct gdbarch *);
99};
100
101static struct gdb_osabi_handler *gdb_osabi_handler_list;
102
103void
05816f70
MK
104gdbarch_register_osabi (enum bfd_architecture arch, unsigned long machine,
105 enum gdb_osabi osabi,
70f80edf
JT
106 void (*init_osabi)(struct gdbarch_info,
107 struct gdbarch *))
108{
109 struct gdb_osabi_handler **handler_p;
05816f70 110 const struct bfd_arch_info *arch_info = bfd_lookup_arch (arch, machine);
b00a8037 111 const char **name_ptr;
70f80edf
JT
112
113 /* Registering an OS ABI handler for "unknown" is not allowed. */
114 if (osabi == GDB_OSABI_UNKNOWN)
115 {
116 internal_error
117 (__FILE__, __LINE__,
118 "gdbarch_register_osabi: An attempt to register a handler for "
119 "OS ABI \"%s\" for architecture %s was made. The handler will "
120 "not be registered",
121 gdbarch_osabi_name (osabi),
05816f70 122 bfd_printable_arch_mach (arch, machine));
70f80edf
JT
123 return;
124 }
125
05816f70
MK
126 gdb_assert (arch_info);
127
70f80edf
JT
128 for (handler_p = &gdb_osabi_handler_list; *handler_p != NULL;
129 handler_p = &(*handler_p)->next)
130 {
05816f70 131 if ((*handler_p)->arch_info == arch_info
70f80edf
JT
132 && (*handler_p)->osabi == osabi)
133 {
134 internal_error
135 (__FILE__, __LINE__,
136 "gdbarch_register_osabi: A handler for OS ABI \"%s\" "
137 "has already been registered for architecture %s",
138 gdbarch_osabi_name (osabi),
05816f70 139 arch_info->printable_name);
70f80edf
JT
140 /* If user wants to continue, override previous definition. */
141 (*handler_p)->init_osabi = init_osabi;
142 return;
143 }
144 }
145
146 (*handler_p)
147 = (struct gdb_osabi_handler *) xmalloc (sizeof (struct gdb_osabi_handler));
148 (*handler_p)->next = NULL;
05816f70 149 (*handler_p)->arch_info = arch_info;
70f80edf
JT
150 (*handler_p)->osabi = osabi;
151 (*handler_p)->init_osabi = init_osabi;
b00a8037
DJ
152
153 /* Add this OS ABI to the list of enum values for "set osabi", if it isn't
154 already there. */
155 for (name_ptr = gdb_osabi_available_names; *name_ptr; name_ptr ++)
156 {
157 if (*name_ptr == gdbarch_osabi_name (osabi))
158 return;
159 }
160 *name_ptr++ = gdbarch_osabi_name (osabi);
161 *name_ptr = NULL;
70f80edf
JT
162}
163\f
164
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. */
170struct gdb_osabi_sniffer
171{
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 *);
176};
177
178static struct gdb_osabi_sniffer *gdb_osabi_sniffer_list;
179
180void
181gdbarch_register_osabi_sniffer (enum bfd_architecture arch,
182 enum bfd_flavour flavour,
183 enum gdb_osabi (*sniffer_fn)(bfd *))
184{
185 struct gdb_osabi_sniffer *sniffer;
186
187 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;
192
193 sniffer->next = gdb_osabi_sniffer_list;
194 gdb_osabi_sniffer_list = sniffer;
195}
196\f
197
198enum gdb_osabi
199gdbarch_lookup_osabi (bfd *abfd)
200{
201 struct gdb_osabi_sniffer *sniffer;
202 enum gdb_osabi osabi, match;
203 int match_specific;
204
b00a8037
DJ
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;
208
209 /* If we don't have a binary, return the default OS ABI (if set) or
210 an inconclusive result (otherwise). */
211 if (abfd == NULL)
212 {
213 if (GDB_OSABI_DEFAULT != GDB_OSABI_UNKNOWN)
214 return GDB_OSABI_DEFAULT;
215 else
216 return GDB_OSABI_UNINITIALIZED;
217 }
4be87837 218
70f80edf
JT
219 match = GDB_OSABI_UNKNOWN;
220 match_specific = 0;
221
222 for (sniffer = gdb_osabi_sniffer_list; sniffer != NULL;
223 sniffer = sniffer->next)
224 {
225 if ((sniffer->arch == bfd_arch_unknown /* wildcard */
226 || sniffer->arch == bfd_get_arch (abfd))
227 && sniffer->flavour == bfd_get_flavour (abfd))
228 {
229 osabi = (*sniffer->sniffer) (abfd);
230 if (osabi < GDB_OSABI_UNKNOWN || osabi >= GDB_OSABI_INVALID)
231 {
232 internal_error
233 (__FILE__, __LINE__,
234 "gdbarch_lookup_osabi: invalid OS ABI (%d) from sniffer "
235 "for architecture %s flavour %d",
236 (int) osabi,
237 bfd_printable_arch_mach (bfd_get_arch (abfd), 0),
238 (int) bfd_get_flavour (abfd));
239 }
240 else if (osabi != GDB_OSABI_UNKNOWN)
241 {
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
245 the first match. */
246 if (match != GDB_OSABI_UNKNOWN)
247 {
248 if ((match_specific && sniffer->arch != bfd_arch_unknown)
249 || (!match_specific && sniffer->arch == bfd_arch_unknown))
250 {
251 internal_error
252 (__FILE__, __LINE__,
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));
261 }
262 else if (sniffer->arch != bfd_arch_unknown)
263 {
264 match = osabi;
265 match_specific = 1;
266 }
267 }
268 else
269 {
270 match = osabi;
271 if (sniffer->arch != bfd_arch_unknown)
272 match_specific = 1;
273 }
274 }
275 }
276 }
277
b00a8037
DJ
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;
282 else
283 return match;
70f80edf
JT
284}
285
6cfae0bc
JB
286
287/* Return non-zero if architecture A can run code written for
288 architecture B. */
289static int
290can_run_code_for (const struct bfd_arch_info *a, const struct bfd_arch_info *b)
291{
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
296 return A.
297
5f724446
AC
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. */
6cfae0bc
JB
301 return (a == b || a->compatible (a, b) == a);
302}
303
304
70f80edf 305void
4be87837 306gdbarch_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
70f80edf 307{
70f80edf 308 const struct bfd_arch_info *arch_info = gdbarch_bfd_arch_info (gdbarch);
05816f70 309 struct gdb_osabi_handler *handler;
70f80edf 310
4be87837 311 if (info.osabi == GDB_OSABI_UNKNOWN)
70f80edf 312 {
01fc4e33
AC
313 /* Don't complain about an unknown OSABI. Assume the user knows
314 what they are doing. */
70f80edf
JT
315 return;
316 }
317
318 for (handler = gdb_osabi_handler_list; handler != NULL;
319 handler = handler->next)
320 {
4be87837 321 if (handler->osabi != info.osabi)
05816f70
MK
322 continue;
323
6cfae0bc
JB
324 /* If the architecture described by ARCH_INFO can run code for
325 the architcture we registered the handler for, then the
326 handler is applicable. Note, though, that if the handler is
327 for an architecture that is a superset of ARCH_INFO, we can't
328 use that --- it would be perfectly correct for it to install
329 gdbarch methods that refer to registers / instructions /
330 other facilities ARCH_INFO doesn't have.
05816f70 331
6cfae0bc 332 NOTE: kettenis/20021027: There may be more than one machine
05816f70
MK
333 type that is compatible with the desired machine type. Right
334 now we simply return the first match, which is fine for now.
335 However, we might want to do something smarter in the future. */
5f724446
AC
336 /* NOTE: cagney/2003-10-23: The code for "a can_run_code_for b"
337 is implemented using BFD's compatible method (a->compatible
338 (b) == a -- the lowest common denominator between a and b is
339 a). That method's definition of compatible may not be as you
ce2826aa 340 expect. For instance the test "amd64 can run code for i386"
5f724446 341 (or more generally "64-bit ISA can run code for the 32-bit
ce2826aa
AC
342 ISA"). BFD doesn't normally consider 32-bit and 64-bit
343 "compatible" so it doesn't succeed. */
6cfae0bc 344 if (can_run_code_for (arch_info, handler->arch_info))
70f80edf
JT
345 {
346 (*handler->init_osabi) (info, gdbarch);
347 return;
348 }
349 }
350
42cf1509
AC
351 fprintf_filtered
352 (gdb_stderr,
353 "A handler for the OS ABI \"%s\" is not built into this "
354 "configuration of GDB. "
355 "Attempting to continue with the default %s settings",
356 gdbarch_osabi_name (info.osabi),
357 bfd_printable_arch_mach (arch_info->arch, arch_info->mach));
70f80edf
JT
358}
359\f
360
361/* Generic sniffer for ELF flavoured files. */
362
363void
364generic_elf_osabi_sniff_abi_tag_sections (bfd *abfd, asection *sect, void *obj)
365{
366 enum gdb_osabi *os_ident_ptr = obj;
367 const char *name;
368 unsigned int sectsize;
369
370 name = bfd_get_section_name (abfd, sect);
371 sectsize = bfd_section_size (abfd, sect);
372
373 /* .note.ABI-tag notes, used by GNU/Linux and FreeBSD. */
374 if (strcmp (name, ".note.ABI-tag") == 0 && sectsize > 0)
375 {
376 unsigned int name_length, data_length, note_type;
377 char *note;
378
379 /* If the section is larger than this, it's probably not what we are
380 looking for. */
381 if (sectsize > 128)
382 sectsize = 128;
383
384 note = alloca (sectsize);
385
386 bfd_get_section_contents (abfd, sect, note,
387 (file_ptr) 0, (bfd_size_type) sectsize);
388
389 name_length = bfd_h_get_32 (abfd, note);
390 data_length = bfd_h_get_32 (abfd, note + 4);
391 note_type = bfd_h_get_32 (abfd, note + 8);
392
393 if (name_length == 4 && data_length == 16 && note_type == NT_GNU_ABI_TAG
394 && strcmp (note + 12, "GNU") == 0)
395 {
396 int os_number = bfd_h_get_32 (abfd, note + 16);
397
398 switch (os_number)
399 {
400 case GNU_ABI_TAG_LINUX:
401 *os_ident_ptr = GDB_OSABI_LINUX;
402 break;
403
404 case GNU_ABI_TAG_HURD:
405 *os_ident_ptr = GDB_OSABI_HURD;
406 break;
407
408 case GNU_ABI_TAG_SOLARIS:
409 *os_ident_ptr = GDB_OSABI_SOLARIS;
410 break;
411
261de166
AC
412 case GNU_ABI_TAG_FREEBSD:
413 *os_ident_ptr = GDB_OSABI_FREEBSD_ELF;
414 break;
415
416 case GNU_ABI_TAG_NETBSD:
417 *os_ident_ptr = GDB_OSABI_NETBSD_ELF;
418 break;
419
70f80edf
JT
420 default:
421 internal_error
422 (__FILE__, __LINE__,
423 "generic_elf_osabi_sniff_abi_tag_sections: unknown OS number %d",
424 os_number);
425 }
426 return;
427 }
428 else if (name_length == 8 && data_length == 4
429 && note_type == NT_FREEBSD_ABI_TAG
430 && strcmp (note + 12, "FreeBSD") == 0)
431 {
432 /* XXX Should we check the version here? Probably not
433 necessary yet. */
434 *os_ident_ptr = GDB_OSABI_FREEBSD_ELF;
435 }
436 return;
437 }
438
439 /* .note.netbsd.ident notes, used by NetBSD. */
440 if (strcmp (name, ".note.netbsd.ident") == 0 && sectsize > 0)
441 {
442 unsigned int name_length, data_length, note_type;
443 char *note;
444
445 /* If the section is larger than this, it's probably not what we are
446 looking for. */
447 if (sectsize > 128)
448 sectsize = 128;
449
450 note = alloca (sectsize);
451
452 bfd_get_section_contents (abfd, sect, note,
453 (file_ptr) 0, (bfd_size_type) sectsize);
454
455 name_length = bfd_h_get_32 (abfd, note);
456 data_length = bfd_h_get_32 (abfd, note + 4);
457 note_type = bfd_h_get_32 (abfd, note + 8);
458
459 if (name_length == 7 && data_length == 4 && note_type == NT_NETBSD_IDENT
460 && strcmp (note + 12, "NetBSD") == 0)
461 {
462 /* XXX Should we check the version here? Probably not
463 necessary yet. */
464 *os_ident_ptr = GDB_OSABI_NETBSD_ELF;
465 }
466 return;
467 }
468}
469
470static enum gdb_osabi
471generic_elf_osabi_sniffer (bfd *abfd)
472{
473 unsigned int elfosabi;
474 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
475
476 elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
477
478 switch (elfosabi)
479 {
480 case ELFOSABI_NONE:
481 /* When elfosabi is ELFOSABI_NONE (0), then the ELF structures in the
482 file are conforming to the base specification for that machine
483 (there are no OS-specific extensions). In order to determine the
484 real OS in use we must look for OS notes that have been added. */
485 bfd_map_over_sections (abfd,
486 generic_elf_osabi_sniff_abi_tag_sections,
487 &osabi);
488 break;
489
490 case ELFOSABI_FREEBSD:
491 osabi = GDB_OSABI_FREEBSD_ELF;
492 break;
493
494 case ELFOSABI_NETBSD:
495 osabi = GDB_OSABI_NETBSD_ELF;
496 break;
497
498 case ELFOSABI_LINUX:
499 osabi = GDB_OSABI_LINUX;
500 break;
501
502 case ELFOSABI_HURD:
503 osabi = GDB_OSABI_HURD;
504 break;
505
506 case ELFOSABI_SOLARIS:
507 osabi = GDB_OSABI_SOLARIS;
508 break;
a09a320d
JB
509
510 case ELFOSABI_HPUX:
511 osabi = GDB_OSABI_HPUX_ELF;
512 break;
70f80edf
JT
513 }
514
bb21884d
MK
515 if (osabi == GDB_OSABI_UNKNOWN)
516 {
517 /* The FreeBSD folks have been naughty; they stored the string
518 "FreeBSD" in the padding of the e_ident field of the ELF
519 header to "brand" their ELF binaries in FreeBSD 3.x. */
520 if (strcmp (&elf_elfheader (abfd)->e_ident[8], "FreeBSD") == 0)
521 osabi = GDB_OSABI_FREEBSD_ELF;
522 }
523
70f80edf
JT
524 return osabi;
525}
526\f
b00a8037
DJ
527static void
528set_osabi (char *args, int from_tty, struct cmd_list_element *c)
529{
530 struct gdbarch_info info;
70f80edf 531
b00a8037
DJ
532 if (strcmp (set_osabi_string, "auto") == 0)
533 user_osabi_state = osabi_auto;
534 else if (strcmp (set_osabi_string, "default") == 0)
535 {
536 user_selected_osabi = GDB_OSABI_DEFAULT;
537 user_osabi_state = osabi_user;
538 }
539 else if (strcmp (set_osabi_string, "none") == 0)
540 {
541 user_selected_osabi = GDB_OSABI_UNKNOWN;
542 user_osabi_state = osabi_user;
543 }
544 else
545 {
546 int i;
547 for (i = 1; i < GDB_OSABI_INVALID; i++)
548 if (strcmp (set_osabi_string, gdbarch_osabi_name (i)) == 0)
549 {
550 user_selected_osabi = i;
551 user_osabi_state = osabi_user;
552 break;
553 }
554 if (i == GDB_OSABI_INVALID)
555 internal_error (__FILE__, __LINE__,
556 "Invalid OS ABI \"%s\" passed to command handler.",
557 set_osabi_string);
558 }
559
560 /* NOTE: At some point (true multiple architectures) we'll need to be more
561 graceful here. */
562 gdbarch_info_init (&info);
563 if (! gdbarch_update_p (info))
564 internal_error (__FILE__, __LINE__, "Updating OS ABI failed.");
565}
566
b9362cc7 567static void
b00a8037
DJ
568show_osabi (char *args, int from_tty)
569{
570 if (user_osabi_state == osabi_auto)
571 printf_filtered ("The current OS ABI is \"auto\" (currently \"%s\").\n",
572 gdbarch_osabi_name (gdbarch_osabi (current_gdbarch)));
573 else
574 printf_filtered ("The current OS ABI is \"%s\".\n",
575 gdbarch_osabi_name (user_selected_osabi));
576
577 if (GDB_OSABI_DEFAULT != GDB_OSABI_UNKNOWN)
578 printf_filtered ("The default OS ABI is \"%s\".\n",
579 gdbarch_osabi_name (GDB_OSABI_DEFAULT));
580}
581\f
b9362cc7
AC
582extern initialize_file_ftype _initialize_gdb_osabi; /* -Wmissing-prototype */
583
70f80edf
JT
584void
585_initialize_gdb_osabi (void)
586{
b00a8037
DJ
587 struct cmd_list_element *c;
588
70f80edf
JT
589 if (strcmp (gdb_osabi_names[GDB_OSABI_INVALID], "<invalid>") != 0)
590 internal_error
591 (__FILE__, __LINE__,
592 "_initialize_gdb_osabi: gdb_osabi_names[] is inconsistent");
593
594 /* Register a generic sniffer for ELF flavoured files. */
595 gdbarch_register_osabi_sniffer (bfd_arch_unknown,
596 bfd_target_elf_flavour,
597 generic_elf_osabi_sniffer);
b00a8037 598
42cf1509 599 return;
b00a8037
DJ
600
601 /* Register the "set osabi" command. */
602 c = add_set_enum_cmd ("osabi", class_support, gdb_osabi_available_names,
603 &set_osabi_string, "Set OS ABI of target.", &setlist);
604
605 set_cmd_sfunc (c, set_osabi);
606 add_cmd ("osabi", class_support, show_osabi, "Show OS/ABI of target.",
607 &showlist);
608 user_osabi_state = osabi_auto;
70f80edf 609}
This page took 0.220164 seconds and 4 git commands to generate.