* Makefile.in (osabi.o, i387-tdep.o, i386-linux-nat.o, lin-lwp.o,
[deliverable/binutils-gdb.git] / gdb / osabi.c
CommitLineData
70f80edf
JT
1/* OS ABI variant handling for GDB.
2 Copyright 2001, 2002 Free Software Foundation, Inc.
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"
309367d4 22#include "gdb_string.h"
70f80edf
JT
23#include "osabi.h"
24
25#include "elf-bfd.h"
26
27
28/* This table matches the indices assigned to enum gdb_osabi. Keep
29 them in sync. */
30static const char * const gdb_osabi_names[] =
31{
32 "<unknown>",
33
34 "SVR4",
35 "GNU/Hurd",
36 "Solaris",
37 "OSF/1",
38 "GNU/Linux",
39 "FreeBSD a.out",
40 "FreeBSD ELF",
41 "NetBSD a.out",
42 "NetBSD ELF",
43 "Windows CE",
1029b7fa
MK
44 "DJGPP",
45 "NetWare",
b96d0a4e 46 "Irix",
daa66587 47 "LynxOS",
70f80edf
JT
48
49 "ARM EABI v1",
50 "ARM EABI v2",
51 "ARM APCS",
52
53 "<invalid>"
54};
55
56const char *
57gdbarch_osabi_name (enum gdb_osabi osabi)
58{
59 if (osabi >= GDB_OSABI_UNKNOWN && osabi < GDB_OSABI_INVALID)
60 return gdb_osabi_names[osabi];
61
62 return gdb_osabi_names[GDB_OSABI_INVALID];
63}
64
65/* Handler for a given architecture/OS ABI pair. There should be only
66 one handler for a given OS ABI each architecture family. */
67struct gdb_osabi_handler
68{
69 struct gdb_osabi_handler *next;
70 enum bfd_architecture arch;
71 enum gdb_osabi osabi;
72 void (*init_osabi)(struct gdbarch_info, struct gdbarch *);
73};
74
75static struct gdb_osabi_handler *gdb_osabi_handler_list;
76
77void
78gdbarch_register_osabi (enum bfd_architecture arch, enum gdb_osabi osabi,
79 void (*init_osabi)(struct gdbarch_info,
80 struct gdbarch *))
81{
82 struct gdb_osabi_handler **handler_p;
83
84 /* Registering an OS ABI handler for "unknown" is not allowed. */
85 if (osabi == GDB_OSABI_UNKNOWN)
86 {
87 internal_error
88 (__FILE__, __LINE__,
89 "gdbarch_register_osabi: An attempt to register a handler for "
90 "OS ABI \"%s\" for architecture %s was made. The handler will "
91 "not be registered",
92 gdbarch_osabi_name (osabi),
93 bfd_printable_arch_mach (arch, 0));
94 return;
95 }
96
97 for (handler_p = &gdb_osabi_handler_list; *handler_p != NULL;
98 handler_p = &(*handler_p)->next)
99 {
100 if ((*handler_p)->arch == arch
101 && (*handler_p)->osabi == osabi)
102 {
103 internal_error
104 (__FILE__, __LINE__,
105 "gdbarch_register_osabi: A handler for OS ABI \"%s\" "
106 "has already been registered for architecture %s",
107 gdbarch_osabi_name (osabi),
108 bfd_printable_arch_mach (arch, 0));
109 /* If user wants to continue, override previous definition. */
110 (*handler_p)->init_osabi = init_osabi;
111 return;
112 }
113 }
114
115 (*handler_p)
116 = (struct gdb_osabi_handler *) xmalloc (sizeof (struct gdb_osabi_handler));
117 (*handler_p)->next = NULL;
118 (*handler_p)->arch = arch;
119 (*handler_p)->osabi = osabi;
120 (*handler_p)->init_osabi = init_osabi;
121}
122\f
123
124/* Sniffer to find the OS ABI for a given file's architecture and flavour.
125 It is legal to have multiple sniffers for each arch/flavour pair, to
126 disambiguate one OS's a.out from another, for example. The first sniffer
127 to return something other than GDB_OSABI_UNKNOWN wins, so a sniffer should
128 be careful to claim a file only if it knows for sure what it is. */
129struct gdb_osabi_sniffer
130{
131 struct gdb_osabi_sniffer *next;
132 enum bfd_architecture arch; /* bfd_arch_unknown == wildcard */
133 enum bfd_flavour flavour;
134 enum gdb_osabi (*sniffer)(bfd *);
135};
136
137static struct gdb_osabi_sniffer *gdb_osabi_sniffer_list;
138
139void
140gdbarch_register_osabi_sniffer (enum bfd_architecture arch,
141 enum bfd_flavour flavour,
142 enum gdb_osabi (*sniffer_fn)(bfd *))
143{
144 struct gdb_osabi_sniffer *sniffer;
145
146 sniffer =
147 (struct gdb_osabi_sniffer *) xmalloc (sizeof (struct gdb_osabi_sniffer));
148 sniffer->arch = arch;
149 sniffer->flavour = flavour;
150 sniffer->sniffer = sniffer_fn;
151
152 sniffer->next = gdb_osabi_sniffer_list;
153 gdb_osabi_sniffer_list = sniffer;
154}
155\f
156
157enum gdb_osabi
158gdbarch_lookup_osabi (bfd *abfd)
159{
160 struct gdb_osabi_sniffer *sniffer;
161 enum gdb_osabi osabi, match;
162 int match_specific;
163
164 match = GDB_OSABI_UNKNOWN;
165 match_specific = 0;
166
167 for (sniffer = gdb_osabi_sniffer_list; sniffer != NULL;
168 sniffer = sniffer->next)
169 {
170 if ((sniffer->arch == bfd_arch_unknown /* wildcard */
171 || sniffer->arch == bfd_get_arch (abfd))
172 && sniffer->flavour == bfd_get_flavour (abfd))
173 {
174 osabi = (*sniffer->sniffer) (abfd);
175 if (osabi < GDB_OSABI_UNKNOWN || osabi >= GDB_OSABI_INVALID)
176 {
177 internal_error
178 (__FILE__, __LINE__,
179 "gdbarch_lookup_osabi: invalid OS ABI (%d) from sniffer "
180 "for architecture %s flavour %d",
181 (int) osabi,
182 bfd_printable_arch_mach (bfd_get_arch (abfd), 0),
183 (int) bfd_get_flavour (abfd));
184 }
185 else if (osabi != GDB_OSABI_UNKNOWN)
186 {
187 /* A specific sniffer always overrides a generic sniffer.
188 Croak on multiple match if the two matches are of the
189 same class. If the user wishes to continue, we'll use
190 the first match. */
191 if (match != GDB_OSABI_UNKNOWN)
192 {
193 if ((match_specific && sniffer->arch != bfd_arch_unknown)
194 || (!match_specific && sniffer->arch == bfd_arch_unknown))
195 {
196 internal_error
197 (__FILE__, __LINE__,
198 "gdbarch_lookup_osabi: multiple %sspecific OS ABI "
199 "match for architecture %s flavour %d: first "
200 "match \"%s\", second match \"%s\"",
201 match_specific ? "" : "non-",
202 bfd_printable_arch_mach (bfd_get_arch (abfd), 0),
203 (int) bfd_get_flavour (abfd),
204 gdbarch_osabi_name (match),
205 gdbarch_osabi_name (osabi));
206 }
207 else if (sniffer->arch != bfd_arch_unknown)
208 {
209 match = osabi;
210 match_specific = 1;
211 }
212 }
213 else
214 {
215 match = osabi;
216 if (sniffer->arch != bfd_arch_unknown)
217 match_specific = 1;
218 }
219 }
220 }
221 }
222
223 return match;
224}
225
226void
227gdbarch_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch,
228 enum gdb_osabi osabi)
229{
230 struct gdb_osabi_handler *handler;
231 bfd *abfd = info.abfd;
232 const struct bfd_arch_info *arch_info = gdbarch_bfd_arch_info (gdbarch);
233
234 if (osabi == GDB_OSABI_UNKNOWN)
235 {
01fc4e33
AC
236 /* Don't complain about an unknown OSABI. Assume the user knows
237 what they are doing. */
70f80edf
JT
238 return;
239 }
240
241 for (handler = gdb_osabi_handler_list; handler != NULL;
242 handler = handler->next)
243 {
244 if (handler->arch == bfd_get_arch (abfd)
245 && handler->osabi == osabi)
246 {
247 (*handler->init_osabi) (info, gdbarch);
248 return;
249 }
250 }
251
252 /* We assume that if GDB_MULTI_ARCH is less than GDB_MULTI_ARCH_TM
253 that an ABI variant can be supported by overriding definitions in
254 the tm-file. */
255 if (GDB_MULTI_ARCH > GDB_MULTI_ARCH_PARTIAL)
256 fprintf_filtered
257 (gdb_stderr,
258 "A handler for the OS ABI \"%s\" is not built into this "
259 "configuration of GDB. "
260 "Attempting to continue with the default %s settings",
261 gdbarch_osabi_name (osabi),
262 bfd_printable_arch_mach (arch_info->arch, arch_info->mach));
263}
264\f
265
266/* Generic sniffer for ELF flavoured files. */
267
268void
269generic_elf_osabi_sniff_abi_tag_sections (bfd *abfd, asection *sect, void *obj)
270{
271 enum gdb_osabi *os_ident_ptr = obj;
272 const char *name;
273 unsigned int sectsize;
274
275 name = bfd_get_section_name (abfd, sect);
276 sectsize = bfd_section_size (abfd, sect);
277
278 /* .note.ABI-tag notes, used by GNU/Linux and FreeBSD. */
279 if (strcmp (name, ".note.ABI-tag") == 0 && sectsize > 0)
280 {
281 unsigned int name_length, data_length, note_type;
282 char *note;
283
284 /* If the section is larger than this, it's probably not what we are
285 looking for. */
286 if (sectsize > 128)
287 sectsize = 128;
288
289 note = alloca (sectsize);
290
291 bfd_get_section_contents (abfd, sect, note,
292 (file_ptr) 0, (bfd_size_type) sectsize);
293
294 name_length = bfd_h_get_32 (abfd, note);
295 data_length = bfd_h_get_32 (abfd, note + 4);
296 note_type = bfd_h_get_32 (abfd, note + 8);
297
298 if (name_length == 4 && data_length == 16 && note_type == NT_GNU_ABI_TAG
299 && strcmp (note + 12, "GNU") == 0)
300 {
301 int os_number = bfd_h_get_32 (abfd, note + 16);
302
303 switch (os_number)
304 {
305 case GNU_ABI_TAG_LINUX:
306 *os_ident_ptr = GDB_OSABI_LINUX;
307 break;
308
309 case GNU_ABI_TAG_HURD:
310 *os_ident_ptr = GDB_OSABI_HURD;
311 break;
312
313 case GNU_ABI_TAG_SOLARIS:
314 *os_ident_ptr = GDB_OSABI_SOLARIS;
315 break;
316
317 default:
318 internal_error
319 (__FILE__, __LINE__,
320 "generic_elf_osabi_sniff_abi_tag_sections: unknown OS number %d",
321 os_number);
322 }
323 return;
324 }
325 else if (name_length == 8 && data_length == 4
326 && note_type == NT_FREEBSD_ABI_TAG
327 && strcmp (note + 12, "FreeBSD") == 0)
328 {
329 /* XXX Should we check the version here? Probably not
330 necessary yet. */
331 *os_ident_ptr = GDB_OSABI_FREEBSD_ELF;
332 }
333 return;
334 }
335
336 /* .note.netbsd.ident notes, used by NetBSD. */
337 if (strcmp (name, ".note.netbsd.ident") == 0 && sectsize > 0)
338 {
339 unsigned int name_length, data_length, note_type;
340 char *note;
341
342 /* If the section is larger than this, it's probably not what we are
343 looking for. */
344 if (sectsize > 128)
345 sectsize = 128;
346
347 note = alloca (sectsize);
348
349 bfd_get_section_contents (abfd, sect, note,
350 (file_ptr) 0, (bfd_size_type) sectsize);
351
352 name_length = bfd_h_get_32 (abfd, note);
353 data_length = bfd_h_get_32 (abfd, note + 4);
354 note_type = bfd_h_get_32 (abfd, note + 8);
355
356 if (name_length == 7 && data_length == 4 && note_type == NT_NETBSD_IDENT
357 && strcmp (note + 12, "NetBSD") == 0)
358 {
359 /* XXX Should we check the version here? Probably not
360 necessary yet. */
361 *os_ident_ptr = GDB_OSABI_NETBSD_ELF;
362 }
363 return;
364 }
365}
366
367static enum gdb_osabi
368generic_elf_osabi_sniffer (bfd *abfd)
369{
370 unsigned int elfosabi;
371 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
372
373 elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
374
375 switch (elfosabi)
376 {
377 case ELFOSABI_NONE:
378 /* When elfosabi is ELFOSABI_NONE (0), then the ELF structures in the
379 file are conforming to the base specification for that machine
380 (there are no OS-specific extensions). In order to determine the
381 real OS in use we must look for OS notes that have been added. */
382 bfd_map_over_sections (abfd,
383 generic_elf_osabi_sniff_abi_tag_sections,
384 &osabi);
385 break;
386
387 case ELFOSABI_FREEBSD:
388 osabi = GDB_OSABI_FREEBSD_ELF;
389 break;
390
391 case ELFOSABI_NETBSD:
392 osabi = GDB_OSABI_NETBSD_ELF;
393 break;
394
395 case ELFOSABI_LINUX:
396 osabi = GDB_OSABI_LINUX;
397 break;
398
399 case ELFOSABI_HURD:
400 osabi = GDB_OSABI_HURD;
401 break;
402
403 case ELFOSABI_SOLARIS:
404 osabi = GDB_OSABI_SOLARIS;
405 break;
406 }
407
bb21884d
MK
408 if (osabi == GDB_OSABI_UNKNOWN)
409 {
410 /* The FreeBSD folks have been naughty; they stored the string
411 "FreeBSD" in the padding of the e_ident field of the ELF
412 header to "brand" their ELF binaries in FreeBSD 3.x. */
413 if (strcmp (&elf_elfheader (abfd)->e_ident[8], "FreeBSD") == 0)
414 osabi = GDB_OSABI_FREEBSD_ELF;
415 }
416
70f80edf
JT
417 return osabi;
418}
419\f
420
421void
422_initialize_gdb_osabi (void)
423{
424 if (strcmp (gdb_osabi_names[GDB_OSABI_INVALID], "<invalid>") != 0)
425 internal_error
426 (__FILE__, __LINE__,
427 "_initialize_gdb_osabi: gdb_osabi_names[] is inconsistent");
428
429 /* Register a generic sniffer for ELF flavoured files. */
430 gdbarch_register_osabi_sniffer (bfd_arch_unknown,
431 bfd_target_elf_flavour,
432 generic_elf_osabi_sniffer);
433}
This page took 0.067712 seconds and 4 git commands to generate.