1 /* OS ABI variant handling for GDB.
2 Copyright 2001, 2002 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. */
22 #include "gdb_string.h"
28 /* This table matches the indices assigned to enum gdb_osabi. Keep
30 static const char * const gdb_osabi_names
[] =
60 gdbarch_osabi_name (enum gdb_osabi osabi
)
62 if (osabi
>= GDB_OSABI_UNKNOWN
&& osabi
< GDB_OSABI_INVALID
)
63 return gdb_osabi_names
[osabi
];
65 return gdb_osabi_names
[GDB_OSABI_INVALID
];
68 /* Handler for a given architecture/OS ABI pair. There should be only
69 one handler for a given OS ABI each architecture family. */
70 struct gdb_osabi_handler
72 struct gdb_osabi_handler
*next
;
73 enum bfd_architecture arch
;
75 void (*init_osabi
)(struct gdbarch_info
, struct gdbarch
*);
78 static struct gdb_osabi_handler
*gdb_osabi_handler_list
;
81 gdbarch_register_osabi (enum bfd_architecture arch
, enum gdb_osabi osabi
,
82 void (*init_osabi
)(struct gdbarch_info
,
85 struct gdb_osabi_handler
**handler_p
;
87 /* Registering an OS ABI handler for "unknown" is not allowed. */
88 if (osabi
== GDB_OSABI_UNKNOWN
)
92 "gdbarch_register_osabi: An attempt to register a handler for "
93 "OS ABI \"%s\" for architecture %s was made. The handler will "
95 gdbarch_osabi_name (osabi
),
96 bfd_printable_arch_mach (arch
, 0));
100 for (handler_p
= &gdb_osabi_handler_list
; *handler_p
!= NULL
;
101 handler_p
= &(*handler_p
)->next
)
103 if ((*handler_p
)->arch
== arch
104 && (*handler_p
)->osabi
== osabi
)
108 "gdbarch_register_osabi: A handler for OS ABI \"%s\" "
109 "has already been registered for architecture %s",
110 gdbarch_osabi_name (osabi
),
111 bfd_printable_arch_mach (arch
, 0));
112 /* If user wants to continue, override previous definition. */
113 (*handler_p
)->init_osabi
= init_osabi
;
119 = (struct gdb_osabi_handler
*) xmalloc (sizeof (struct gdb_osabi_handler
));
120 (*handler_p
)->next
= NULL
;
121 (*handler_p
)->arch
= arch
;
122 (*handler_p
)->osabi
= osabi
;
123 (*handler_p
)->init_osabi
= init_osabi
;
127 /* Sniffer to find the OS ABI for a given file's architecture and flavour.
128 It is legal to have multiple sniffers for each arch/flavour pair, to
129 disambiguate one OS's a.out from another, for example. The first sniffer
130 to return something other than GDB_OSABI_UNKNOWN wins, so a sniffer should
131 be careful to claim a file only if it knows for sure what it is. */
132 struct gdb_osabi_sniffer
134 struct gdb_osabi_sniffer
*next
;
135 enum bfd_architecture arch
; /* bfd_arch_unknown == wildcard */
136 enum bfd_flavour flavour
;
137 enum gdb_osabi (*sniffer
)(bfd
*);
140 static struct gdb_osabi_sniffer
*gdb_osabi_sniffer_list
;
143 gdbarch_register_osabi_sniffer (enum bfd_architecture arch
,
144 enum bfd_flavour flavour
,
145 enum gdb_osabi (*sniffer_fn
)(bfd
*))
147 struct gdb_osabi_sniffer
*sniffer
;
150 (struct gdb_osabi_sniffer
*) xmalloc (sizeof (struct gdb_osabi_sniffer
));
151 sniffer
->arch
= arch
;
152 sniffer
->flavour
= flavour
;
153 sniffer
->sniffer
= sniffer_fn
;
155 sniffer
->next
= gdb_osabi_sniffer_list
;
156 gdb_osabi_sniffer_list
= sniffer
;
161 gdbarch_lookup_osabi (bfd
*abfd
)
163 struct gdb_osabi_sniffer
*sniffer
;
164 enum gdb_osabi osabi
, match
;
167 match
= GDB_OSABI_UNKNOWN
;
170 for (sniffer
= gdb_osabi_sniffer_list
; sniffer
!= NULL
;
171 sniffer
= sniffer
->next
)
173 if ((sniffer
->arch
== bfd_arch_unknown
/* wildcard */
174 || sniffer
->arch
== bfd_get_arch (abfd
))
175 && sniffer
->flavour
== bfd_get_flavour (abfd
))
177 osabi
= (*sniffer
->sniffer
) (abfd
);
178 if (osabi
< GDB_OSABI_UNKNOWN
|| osabi
>= GDB_OSABI_INVALID
)
182 "gdbarch_lookup_osabi: invalid OS ABI (%d) from sniffer "
183 "for architecture %s flavour %d",
185 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0),
186 (int) bfd_get_flavour (abfd
));
188 else if (osabi
!= GDB_OSABI_UNKNOWN
)
190 /* A specific sniffer always overrides a generic sniffer.
191 Croak on multiple match if the two matches are of the
192 same class. If the user wishes to continue, we'll use
194 if (match
!= GDB_OSABI_UNKNOWN
)
196 if ((match_specific
&& sniffer
->arch
!= bfd_arch_unknown
)
197 || (!match_specific
&& sniffer
->arch
== bfd_arch_unknown
))
201 "gdbarch_lookup_osabi: multiple %sspecific OS ABI "
202 "match for architecture %s flavour %d: first "
203 "match \"%s\", second match \"%s\"",
204 match_specific
? "" : "non-",
205 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0),
206 (int) bfd_get_flavour (abfd
),
207 gdbarch_osabi_name (match
),
208 gdbarch_osabi_name (osabi
));
210 else if (sniffer
->arch
!= bfd_arch_unknown
)
219 if (sniffer
->arch
!= bfd_arch_unknown
)
230 gdbarch_init_osabi (struct gdbarch_info info
, struct gdbarch
*gdbarch
,
231 enum gdb_osabi osabi
)
233 struct gdb_osabi_handler
*handler
;
234 bfd
*abfd
= info
.abfd
;
235 const struct bfd_arch_info
*arch_info
= gdbarch_bfd_arch_info (gdbarch
);
237 if (osabi
== GDB_OSABI_UNKNOWN
)
239 /* Don't complain about an unknown OSABI. Assume the user knows
240 what they are doing. */
244 for (handler
= gdb_osabi_handler_list
; handler
!= NULL
;
245 handler
= handler
->next
)
247 if (handler
->arch
== bfd_get_arch (abfd
)
248 && handler
->osabi
== osabi
)
250 (*handler
->init_osabi
) (info
, gdbarch
);
255 /* We assume that if GDB_MULTI_ARCH is less than GDB_MULTI_ARCH_TM
256 that an ABI variant can be supported by overriding definitions in
258 if (GDB_MULTI_ARCH
> GDB_MULTI_ARCH_PARTIAL
)
261 "A handler for the OS ABI \"%s\" is not built into this "
262 "configuration of GDB. "
263 "Attempting to continue with the default %s settings",
264 gdbarch_osabi_name (osabi
),
265 bfd_printable_arch_mach (arch_info
->arch
, arch_info
->mach
));
269 /* Generic sniffer for ELF flavoured files. */
272 generic_elf_osabi_sniff_abi_tag_sections (bfd
*abfd
, asection
*sect
, void *obj
)
274 enum gdb_osabi
*os_ident_ptr
= obj
;
276 unsigned int sectsize
;
278 name
= bfd_get_section_name (abfd
, sect
);
279 sectsize
= bfd_section_size (abfd
, sect
);
281 /* .note.ABI-tag notes, used by GNU/Linux and FreeBSD. */
282 if (strcmp (name
, ".note.ABI-tag") == 0 && sectsize
> 0)
284 unsigned int name_length
, data_length
, note_type
;
287 /* If the section is larger than this, it's probably not what we are
292 note
= alloca (sectsize
);
294 bfd_get_section_contents (abfd
, sect
, note
,
295 (file_ptr
) 0, (bfd_size_type
) sectsize
);
297 name_length
= bfd_h_get_32 (abfd
, note
);
298 data_length
= bfd_h_get_32 (abfd
, note
+ 4);
299 note_type
= bfd_h_get_32 (abfd
, note
+ 8);
301 if (name_length
== 4 && data_length
== 16 && note_type
== NT_GNU_ABI_TAG
302 && strcmp (note
+ 12, "GNU") == 0)
304 int os_number
= bfd_h_get_32 (abfd
, note
+ 16);
308 case GNU_ABI_TAG_LINUX
:
309 *os_ident_ptr
= GDB_OSABI_LINUX
;
312 case GNU_ABI_TAG_HURD
:
313 *os_ident_ptr
= GDB_OSABI_HURD
;
316 case GNU_ABI_TAG_SOLARIS
:
317 *os_ident_ptr
= GDB_OSABI_SOLARIS
;
323 "generic_elf_osabi_sniff_abi_tag_sections: unknown OS number %d",
328 else if (name_length
== 8 && data_length
== 4
329 && note_type
== NT_FREEBSD_ABI_TAG
330 && strcmp (note
+ 12, "FreeBSD") == 0)
332 /* XXX Should we check the version here? Probably not
334 *os_ident_ptr
= GDB_OSABI_FREEBSD_ELF
;
339 /* .note.netbsd.ident notes, used by NetBSD. */
340 if (strcmp (name
, ".note.netbsd.ident") == 0 && sectsize
> 0)
342 unsigned int name_length
, data_length
, note_type
;
345 /* If the section is larger than this, it's probably not what we are
350 note
= alloca (sectsize
);
352 bfd_get_section_contents (abfd
, sect
, note
,
353 (file_ptr
) 0, (bfd_size_type
) sectsize
);
355 name_length
= bfd_h_get_32 (abfd
, note
);
356 data_length
= bfd_h_get_32 (abfd
, note
+ 4);
357 note_type
= bfd_h_get_32 (abfd
, note
+ 8);
359 if (name_length
== 7 && data_length
== 4 && note_type
== NT_NETBSD_IDENT
360 && strcmp (note
+ 12, "NetBSD") == 0)
362 /* XXX Should we check the version here? Probably not
364 *os_ident_ptr
= GDB_OSABI_NETBSD_ELF
;
370 static enum gdb_osabi
371 generic_elf_osabi_sniffer (bfd
*abfd
)
373 unsigned int elfosabi
;
374 enum gdb_osabi osabi
= GDB_OSABI_UNKNOWN
;
376 elfosabi
= elf_elfheader (abfd
)->e_ident
[EI_OSABI
];
381 /* When elfosabi is ELFOSABI_NONE (0), then the ELF structures in the
382 file are conforming to the base specification for that machine
383 (there are no OS-specific extensions). In order to determine the
384 real OS in use we must look for OS notes that have been added. */
385 bfd_map_over_sections (abfd
,
386 generic_elf_osabi_sniff_abi_tag_sections
,
390 case ELFOSABI_FREEBSD
:
391 osabi
= GDB_OSABI_FREEBSD_ELF
;
394 case ELFOSABI_NETBSD
:
395 osabi
= GDB_OSABI_NETBSD_ELF
;
399 osabi
= GDB_OSABI_LINUX
;
403 osabi
= GDB_OSABI_HURD
;
406 case ELFOSABI_SOLARIS
:
407 osabi
= GDB_OSABI_SOLARIS
;
411 osabi
= GDB_OSABI_HPUX_ELF
;
415 if (osabi
== GDB_OSABI_UNKNOWN
)
417 /* The FreeBSD folks have been naughty; they stored the string
418 "FreeBSD" in the padding of the e_ident field of the ELF
419 header to "brand" their ELF binaries in FreeBSD 3.x. */
420 if (strcmp (&elf_elfheader (abfd
)->e_ident
[8], "FreeBSD") == 0)
421 osabi
= GDB_OSABI_FREEBSD_ELF
;
429 _initialize_gdb_osabi (void)
431 if (strcmp (gdb_osabi_names
[GDB_OSABI_INVALID
], "<invalid>") != 0)
434 "_initialize_gdb_osabi: gdb_osabi_names[] is inconsistent");
436 /* Register a generic sniffer for ELF flavoured files. */
437 gdbarch_register_osabi_sniffer (bfd_arch_unknown
,
438 bfd_target_elf_flavour
,
439 generic_elf_osabi_sniffer
);