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. */
27 /* This table matches the indices assigned to enum gdb_osabi. Keep
29 static const char * const gdb_osabi_names
[] =
55 gdbarch_osabi_name (enum gdb_osabi osabi
)
57 if (osabi
>= GDB_OSABI_UNKNOWN
&& osabi
< GDB_OSABI_INVALID
)
58 return gdb_osabi_names
[osabi
];
60 return gdb_osabi_names
[GDB_OSABI_INVALID
];
63 /* Handler for a given architecture/OS ABI pair. There should be only
64 one handler for a given OS ABI each architecture family. */
65 struct gdb_osabi_handler
67 struct gdb_osabi_handler
*next
;
68 enum bfd_architecture arch
;
70 void (*init_osabi
)(struct gdbarch_info
, struct gdbarch
*);
73 static struct gdb_osabi_handler
*gdb_osabi_handler_list
;
76 gdbarch_register_osabi (enum bfd_architecture arch
, enum gdb_osabi osabi
,
77 void (*init_osabi
)(struct gdbarch_info
,
80 struct gdb_osabi_handler
**handler_p
;
82 /* Registering an OS ABI handler for "unknown" is not allowed. */
83 if (osabi
== GDB_OSABI_UNKNOWN
)
87 "gdbarch_register_osabi: An attempt to register a handler for "
88 "OS ABI \"%s\" for architecture %s was made. The handler will "
90 gdbarch_osabi_name (osabi
),
91 bfd_printable_arch_mach (arch
, 0));
95 for (handler_p
= &gdb_osabi_handler_list
; *handler_p
!= NULL
;
96 handler_p
= &(*handler_p
)->next
)
98 if ((*handler_p
)->arch
== arch
99 && (*handler_p
)->osabi
== osabi
)
103 "gdbarch_register_osabi: A handler for OS ABI \"%s\" "
104 "has already been registered for architecture %s",
105 gdbarch_osabi_name (osabi
),
106 bfd_printable_arch_mach (arch
, 0));
107 /* If user wants to continue, override previous definition. */
108 (*handler_p
)->init_osabi
= init_osabi
;
114 = (struct gdb_osabi_handler
*) xmalloc (sizeof (struct gdb_osabi_handler
));
115 (*handler_p
)->next
= NULL
;
116 (*handler_p
)->arch
= arch
;
117 (*handler_p
)->osabi
= osabi
;
118 (*handler_p
)->init_osabi
= init_osabi
;
122 /* Sniffer to find the OS ABI for a given file's architecture and flavour.
123 It is legal to have multiple sniffers for each arch/flavour pair, to
124 disambiguate one OS's a.out from another, for example. The first sniffer
125 to return something other than GDB_OSABI_UNKNOWN wins, so a sniffer should
126 be careful to claim a file only if it knows for sure what it is. */
127 struct gdb_osabi_sniffer
129 struct gdb_osabi_sniffer
*next
;
130 enum bfd_architecture arch
; /* bfd_arch_unknown == wildcard */
131 enum bfd_flavour flavour
;
132 enum gdb_osabi (*sniffer
)(bfd
*);
135 static struct gdb_osabi_sniffer
*gdb_osabi_sniffer_list
;
138 gdbarch_register_osabi_sniffer (enum bfd_architecture arch
,
139 enum bfd_flavour flavour
,
140 enum gdb_osabi (*sniffer_fn
)(bfd
*))
142 struct gdb_osabi_sniffer
*sniffer
;
145 (struct gdb_osabi_sniffer
*) xmalloc (sizeof (struct gdb_osabi_sniffer
));
146 sniffer
->arch
= arch
;
147 sniffer
->flavour
= flavour
;
148 sniffer
->sniffer
= sniffer_fn
;
150 sniffer
->next
= gdb_osabi_sniffer_list
;
151 gdb_osabi_sniffer_list
= sniffer
;
156 gdbarch_lookup_osabi (bfd
*abfd
)
158 struct gdb_osabi_sniffer
*sniffer
;
159 enum gdb_osabi osabi
, match
;
162 match
= GDB_OSABI_UNKNOWN
;
165 for (sniffer
= gdb_osabi_sniffer_list
; sniffer
!= NULL
;
166 sniffer
= sniffer
->next
)
168 if ((sniffer
->arch
== bfd_arch_unknown
/* wildcard */
169 || sniffer
->arch
== bfd_get_arch (abfd
))
170 && sniffer
->flavour
== bfd_get_flavour (abfd
))
172 osabi
= (*sniffer
->sniffer
) (abfd
);
173 if (osabi
< GDB_OSABI_UNKNOWN
|| osabi
>= GDB_OSABI_INVALID
)
177 "gdbarch_lookup_osabi: invalid OS ABI (%d) from sniffer "
178 "for architecture %s flavour %d",
180 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0),
181 (int) bfd_get_flavour (abfd
));
183 else if (osabi
!= GDB_OSABI_UNKNOWN
)
185 /* A specific sniffer always overrides a generic sniffer.
186 Croak on multiple match if the two matches are of the
187 same class. If the user wishes to continue, we'll use
189 if (match
!= GDB_OSABI_UNKNOWN
)
191 if ((match_specific
&& sniffer
->arch
!= bfd_arch_unknown
)
192 || (!match_specific
&& sniffer
->arch
== bfd_arch_unknown
))
196 "gdbarch_lookup_osabi: multiple %sspecific OS ABI "
197 "match for architecture %s flavour %d: first "
198 "match \"%s\", second match \"%s\"",
199 match_specific
? "" : "non-",
200 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0),
201 (int) bfd_get_flavour (abfd
),
202 gdbarch_osabi_name (match
),
203 gdbarch_osabi_name (osabi
));
205 else if (sniffer
->arch
!= bfd_arch_unknown
)
214 if (sniffer
->arch
!= bfd_arch_unknown
)
225 gdbarch_init_osabi (struct gdbarch_info info
, struct gdbarch
*gdbarch
,
226 enum gdb_osabi osabi
)
228 struct gdb_osabi_handler
*handler
;
229 bfd
*abfd
= info
.abfd
;
230 const struct bfd_arch_info
*arch_info
= gdbarch_bfd_arch_info (gdbarch
);
232 if (osabi
== GDB_OSABI_UNKNOWN
)
234 /* Don't complain about not knowing the OS ABI if we don't
238 (gdb_stderr
, "GDB doesn't recognize the OS ABI of the inferior. "
239 "Attempting to continue with the default %s settings",
240 bfd_printable_arch_mach (arch_info
->arch
, arch_info
->mach
));
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 if (osabi
== GDB_OSABI_UNKNOWN
)
413 /* The FreeBSD folks have been naughty; they stored the string
414 "FreeBSD" in the padding of the e_ident field of the ELF
415 header to "brand" their ELF binaries in FreeBSD 3.x. */
416 if (strcmp (&elf_elfheader (abfd
)->e_ident
[8], "FreeBSD") == 0)
417 osabi
= GDB_OSABI_FREEBSD_ELF
;
425 _initialize_gdb_osabi (void)
427 if (strcmp (gdb_osabi_names
[GDB_OSABI_INVALID
], "<invalid>") != 0)
430 "_initialize_gdb_osabi: gdb_osabi_names[] is inconsistent");
432 /* Register a generic sniffer for ELF flavoured files. */
433 gdbarch_register_osabi_sniffer (bfd_arch_unknown
,
434 bfd_target_elf_flavour
,
435 generic_elf_osabi_sniffer
);