Commit | Line | Data |
---|---|---|
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. */ | |
38 | static enum { osabi_auto, osabi_default, osabi_user } user_osabi_state; | |
39 | static enum gdb_osabi user_selected_osabi; | |
40 | static const char *gdb_osabi_available_names[GDB_OSABI_INVALID + 3] = { | |
41 | "auto", | |
42 | "default", | |
43 | "none", | |
44 | NULL | |
45 | }; | |
46 | static const char *set_osabi_string; | |
70f80edf JT |
47 | |
48 | /* This table matches the indices assigned to enum gdb_osabi. Keep | |
49 | them in sync. */ | |
50 | static 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", | |
d33b9831 | 63 | "OpenBSD ELF", |
70f80edf | 64 | "Windows CE", |
1029b7fa MK |
65 | "DJGPP", |
66 | "NetWare", | |
b96d0a4e | 67 | "Irix", |
daa66587 | 68 | "LynxOS", |
e64f66d1 | 69 | "Interix", |
a09a320d JB |
70 | "HP/UX ELF", |
71 | "HP/UX SOM", | |
70f80edf JT |
72 | |
73 | "ARM EABI v1", | |
74 | "ARM EABI v2", | |
75 | "ARM APCS", | |
83461b86 | 76 | "QNX Neutrino", |
70f80edf | 77 | |
1762d96d CV |
78 | "Cygwin", |
79 | ||
70f80edf JT |
80 | "<invalid>" |
81 | }; | |
82 | ||
83 | const char * | |
84 | gdbarch_osabi_name (enum gdb_osabi osabi) | |
85 | { | |
86 | if (osabi >= GDB_OSABI_UNKNOWN && osabi < GDB_OSABI_INVALID) | |
87 | return gdb_osabi_names[osabi]; | |
88 | ||
89 | return gdb_osabi_names[GDB_OSABI_INVALID]; | |
90 | } | |
91 | ||
92 | /* Handler for a given architecture/OS ABI pair. There should be only | |
93 | one handler for a given OS ABI each architecture family. */ | |
94 | struct gdb_osabi_handler | |
95 | { | |
96 | struct gdb_osabi_handler *next; | |
05816f70 | 97 | const struct bfd_arch_info *arch_info; |
70f80edf JT |
98 | enum gdb_osabi osabi; |
99 | void (*init_osabi)(struct gdbarch_info, struct gdbarch *); | |
100 | }; | |
101 | ||
102 | static struct gdb_osabi_handler *gdb_osabi_handler_list; | |
103 | ||
104 | void | |
05816f70 MK |
105 | gdbarch_register_osabi (enum bfd_architecture arch, unsigned long machine, |
106 | enum gdb_osabi osabi, | |
70f80edf JT |
107 | void (*init_osabi)(struct gdbarch_info, |
108 | struct gdbarch *)) | |
109 | { | |
110 | struct gdb_osabi_handler **handler_p; | |
05816f70 | 111 | const struct bfd_arch_info *arch_info = bfd_lookup_arch (arch, machine); |
b00a8037 | 112 | const char **name_ptr; |
70f80edf JT |
113 | |
114 | /* Registering an OS ABI handler for "unknown" is not allowed. */ | |
115 | if (osabi == GDB_OSABI_UNKNOWN) | |
116 | { | |
117 | internal_error | |
118 | (__FILE__, __LINE__, | |
119 | "gdbarch_register_osabi: An attempt to register a handler for " | |
120 | "OS ABI \"%s\" for architecture %s was made. The handler will " | |
121 | "not be registered", | |
122 | gdbarch_osabi_name (osabi), | |
05816f70 | 123 | bfd_printable_arch_mach (arch, machine)); |
70f80edf JT |
124 | return; |
125 | } | |
126 | ||
05816f70 MK |
127 | gdb_assert (arch_info); |
128 | ||
70f80edf JT |
129 | for (handler_p = &gdb_osabi_handler_list; *handler_p != NULL; |
130 | handler_p = &(*handler_p)->next) | |
131 | { | |
05816f70 | 132 | if ((*handler_p)->arch_info == arch_info |
70f80edf JT |
133 | && (*handler_p)->osabi == osabi) |
134 | { | |
135 | internal_error | |
136 | (__FILE__, __LINE__, | |
137 | "gdbarch_register_osabi: A handler for OS ABI \"%s\" " | |
138 | "has already been registered for architecture %s", | |
139 | gdbarch_osabi_name (osabi), | |
05816f70 | 140 | arch_info->printable_name); |
70f80edf JT |
141 | /* If user wants to continue, override previous definition. */ |
142 | (*handler_p)->init_osabi = init_osabi; | |
143 | return; | |
144 | } | |
145 | } | |
146 | ||
147 | (*handler_p) | |
148 | = (struct gdb_osabi_handler *) xmalloc (sizeof (struct gdb_osabi_handler)); | |
149 | (*handler_p)->next = NULL; | |
05816f70 | 150 | (*handler_p)->arch_info = arch_info; |
70f80edf JT |
151 | (*handler_p)->osabi = osabi; |
152 | (*handler_p)->init_osabi = init_osabi; | |
b00a8037 DJ |
153 | |
154 | /* Add this OS ABI to the list of enum values for "set osabi", if it isn't | |
155 | already there. */ | |
156 | for (name_ptr = gdb_osabi_available_names; *name_ptr; name_ptr ++) | |
157 | { | |
158 | if (*name_ptr == gdbarch_osabi_name (osabi)) | |
159 | return; | |
160 | } | |
161 | *name_ptr++ = gdbarch_osabi_name (osabi); | |
162 | *name_ptr = NULL; | |
70f80edf JT |
163 | } |
164 | \f | |
165 | ||
166 | /* Sniffer to find the OS ABI for a given file's architecture and flavour. | |
167 | It is legal to have multiple sniffers for each arch/flavour pair, to | |
168 | disambiguate one OS's a.out from another, for example. The first sniffer | |
169 | to return something other than GDB_OSABI_UNKNOWN wins, so a sniffer should | |
170 | be careful to claim a file only if it knows for sure what it is. */ | |
171 | struct gdb_osabi_sniffer | |
172 | { | |
173 | struct gdb_osabi_sniffer *next; | |
174 | enum bfd_architecture arch; /* bfd_arch_unknown == wildcard */ | |
175 | enum bfd_flavour flavour; | |
176 | enum gdb_osabi (*sniffer)(bfd *); | |
177 | }; | |
178 | ||
179 | static struct gdb_osabi_sniffer *gdb_osabi_sniffer_list; | |
180 | ||
181 | void | |
182 | gdbarch_register_osabi_sniffer (enum bfd_architecture arch, | |
183 | enum bfd_flavour flavour, | |
184 | enum gdb_osabi (*sniffer_fn)(bfd *)) | |
185 | { | |
186 | struct gdb_osabi_sniffer *sniffer; | |
187 | ||
188 | sniffer = | |
189 | (struct gdb_osabi_sniffer *) xmalloc (sizeof (struct gdb_osabi_sniffer)); | |
190 | sniffer->arch = arch; | |
191 | sniffer->flavour = flavour; | |
192 | sniffer->sniffer = sniffer_fn; | |
193 | ||
194 | sniffer->next = gdb_osabi_sniffer_list; | |
195 | gdb_osabi_sniffer_list = sniffer; | |
196 | } | |
197 | \f | |
198 | ||
199 | enum gdb_osabi | |
200 | gdbarch_lookup_osabi (bfd *abfd) | |
201 | { | |
202 | struct gdb_osabi_sniffer *sniffer; | |
203 | enum gdb_osabi osabi, match; | |
204 | int match_specific; | |
205 | ||
b00a8037 DJ |
206 | /* If we aren't in "auto" mode, return the specified OS ABI. */ |
207 | if (user_osabi_state == osabi_user) | |
208 | return user_selected_osabi; | |
209 | ||
210 | /* If we don't have a binary, return the default OS ABI (if set) or | |
211 | an inconclusive result (otherwise). */ | |
212 | if (abfd == NULL) | |
213 | { | |
214 | if (GDB_OSABI_DEFAULT != GDB_OSABI_UNKNOWN) | |
215 | return GDB_OSABI_DEFAULT; | |
216 | else | |
217 | return GDB_OSABI_UNINITIALIZED; | |
218 | } | |
4be87837 | 219 | |
70f80edf JT |
220 | match = GDB_OSABI_UNKNOWN; |
221 | match_specific = 0; | |
222 | ||
223 | for (sniffer = gdb_osabi_sniffer_list; sniffer != NULL; | |
224 | sniffer = sniffer->next) | |
225 | { | |
226 | if ((sniffer->arch == bfd_arch_unknown /* wildcard */ | |
227 | || sniffer->arch == bfd_get_arch (abfd)) | |
228 | && sniffer->flavour == bfd_get_flavour (abfd)) | |
229 | { | |
230 | osabi = (*sniffer->sniffer) (abfd); | |
231 | if (osabi < GDB_OSABI_UNKNOWN || osabi >= GDB_OSABI_INVALID) | |
232 | { | |
233 | internal_error | |
234 | (__FILE__, __LINE__, | |
235 | "gdbarch_lookup_osabi: invalid OS ABI (%d) from sniffer " | |
236 | "for architecture %s flavour %d", | |
237 | (int) osabi, | |
238 | bfd_printable_arch_mach (bfd_get_arch (abfd), 0), | |
239 | (int) bfd_get_flavour (abfd)); | |
240 | } | |
241 | else if (osabi != GDB_OSABI_UNKNOWN) | |
242 | { | |
243 | /* A specific sniffer always overrides a generic sniffer. | |
244 | Croak on multiple match if the two matches are of the | |
245 | same class. If the user wishes to continue, we'll use | |
246 | the first match. */ | |
247 | if (match != GDB_OSABI_UNKNOWN) | |
248 | { | |
249 | if ((match_specific && sniffer->arch != bfd_arch_unknown) | |
250 | || (!match_specific && sniffer->arch == bfd_arch_unknown)) | |
251 | { | |
252 | internal_error | |
253 | (__FILE__, __LINE__, | |
254 | "gdbarch_lookup_osabi: multiple %sspecific OS ABI " | |
255 | "match for architecture %s flavour %d: first " | |
256 | "match \"%s\", second match \"%s\"", | |
257 | match_specific ? "" : "non-", | |
258 | bfd_printable_arch_mach (bfd_get_arch (abfd), 0), | |
259 | (int) bfd_get_flavour (abfd), | |
260 | gdbarch_osabi_name (match), | |
261 | gdbarch_osabi_name (osabi)); | |
262 | } | |
263 | else if (sniffer->arch != bfd_arch_unknown) | |
264 | { | |
265 | match = osabi; | |
266 | match_specific = 1; | |
267 | } | |
268 | } | |
269 | else | |
270 | { | |
271 | match = osabi; | |
272 | if (sniffer->arch != bfd_arch_unknown) | |
273 | match_specific = 1; | |
274 | } | |
275 | } | |
276 | } | |
277 | } | |
278 | ||
b00a8037 DJ |
279 | /* If we didn't find a match, but a default was specified at configure |
280 | time, return the default. */ | |
281 | if (GDB_OSABI_DEFAULT != GDB_OSABI_UNKNOWN && match == GDB_OSABI_UNKNOWN) | |
282 | return GDB_OSABI_DEFAULT; | |
283 | else | |
284 | return match; | |
70f80edf JT |
285 | } |
286 | ||
6cfae0bc JB |
287 | |
288 | /* Return non-zero if architecture A can run code written for | |
289 | architecture B. */ | |
290 | static int | |
291 | can_run_code_for (const struct bfd_arch_info *a, const struct bfd_arch_info *b) | |
292 | { | |
293 | /* BFD's 'A->compatible (A, B)' functions return zero if A and B are | |
294 | incompatible. But if they are compatible, it returns the 'more | |
295 | featureful' of the two arches. That is, if A can run code | |
296 | written for B, but B can't run code written for A, then it'll | |
297 | return A. | |
298 | ||
5f724446 AC |
299 | struct bfd_arch_info objects are singletons: that is, there's |
300 | supposed to be exactly one instance for a given machine. So you | |
301 | can tell whether two are equivalent by comparing pointers. */ | |
6cfae0bc JB |
302 | return (a == b || a->compatible (a, b) == a); |
303 | } | |
304 | ||
305 | ||
70f80edf | 306 | void |
4be87837 | 307 | gdbarch_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch) |
70f80edf | 308 | { |
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. */ | |
931758af | 344 | if (can_run_code_for (info.bfd_arch_info, handler->arch_info)) |
70f80edf JT |
345 | { |
346 | (*handler->init_osabi) (info, gdbarch); | |
347 | return; | |
348 | } | |
349 | } | |
350 | ||
e481341f JB |
351 | warning |
352 | ("A handler for the OS ABI \"%s\" is not built into this configuration\n" | |
353 | "of GDB. Attempting to continue with the default %s settings.\n", | |
354 | gdbarch_osabi_name (info.osabi), | |
355 | info.bfd_arch_info->printable_name); | |
70f80edf JT |
356 | } |
357 | \f | |
a49d618c MK |
358 | /* Limit on the amount of data to be read. */ |
359 | #define MAX_NOTESZ 128 | |
360 | ||
361 | /* Return non-zero if NOTE matches NAME, DESCSZ and TYPE. */ | |
362 | ||
363 | static int | |
364 | check_note (bfd *abfd, asection *sect, const char *note, | |
365 | const char *name, unsigned long descsz, unsigned long type) | |
366 | { | |
367 | unsigned long notesz; | |
368 | ||
369 | /* Calculate the size of this note. */ | |
370 | notesz = strlen (name) + 1; | |
371 | notesz = ((notesz + 3) & ~3); | |
372 | notesz += descsz; | |
373 | notesz = ((notesz + 3) & ~3); | |
374 | ||
375 | /* If this assertion triggers, increase MAX_NOTESZ. */ | |
376 | gdb_assert (notesz <= MAX_NOTESZ); | |
377 | ||
378 | /* Check whether SECT is big enough to comtain the complete note. */ | |
379 | if (notesz > bfd_section_size (abfd, sect)) | |
380 | return 0; | |
381 | ||
382 | /* Check the note name. */ | |
383 | if (bfd_h_get_32 (abfd, note) != (strlen (name) + 1) | |
384 | || strcmp (note + 12, name) != 0) | |
385 | return 0; | |
386 | ||
387 | /* Check the descriptor size. */ | |
388 | if (bfd_h_get_32 (abfd, note + 4) != descsz) | |
389 | return 0; | |
390 | ||
391 | /* Check the note type. */ | |
392 | if (bfd_h_get_32 (abfd, note + 8) != type) | |
393 | return 0; | |
394 | ||
395 | return 1; | |
396 | } | |
70f80edf JT |
397 | |
398 | /* Generic sniffer for ELF flavoured files. */ | |
399 | ||
400 | void | |
401 | generic_elf_osabi_sniff_abi_tag_sections (bfd *abfd, asection *sect, void *obj) | |
402 | { | |
a49d618c | 403 | enum gdb_osabi *osabi = obj; |
70f80edf JT |
404 | const char *name; |
405 | unsigned int sectsize; | |
a49d618c | 406 | char *note; |
70f80edf JT |
407 | |
408 | name = bfd_get_section_name (abfd, sect); | |
409 | sectsize = bfd_section_size (abfd, sect); | |
410 | ||
a49d618c MK |
411 | /* Limit the amount of data to read. */ |
412 | if (sectsize > MAX_NOTESZ) | |
413 | sectsize = MAX_NOTESZ; | |
70f80edf | 414 | |
a49d618c MK |
415 | note = alloca (sectsize); |
416 | bfd_get_section_contents (abfd, sect, note, 0, sectsize); | |
70f80edf | 417 | |
a49d618c MK |
418 | /* .note.ABI-tag notes, used by GNU/Linux and FreeBSD. */ |
419 | if (strcmp (name, ".note.ABI-tag") == 0) | |
420 | { | |
421 | /* GNU. */ | |
422 | if (check_note (abfd, sect, note, "GNU", 16, NT_GNU_ABI_TAG)) | |
70f80edf | 423 | { |
a49d618c | 424 | unsigned int abi_tag = bfd_h_get_32 (abfd, note + 16); |
70f80edf | 425 | |
a49d618c | 426 | switch (abi_tag) |
70f80edf JT |
427 | { |
428 | case GNU_ABI_TAG_LINUX: | |
a49d618c | 429 | *osabi = GDB_OSABI_LINUX; |
70f80edf JT |
430 | break; |
431 | ||
432 | case GNU_ABI_TAG_HURD: | |
a49d618c | 433 | *osabi = GDB_OSABI_HURD; |
70f80edf JT |
434 | break; |
435 | ||
436 | case GNU_ABI_TAG_SOLARIS: | |
a49d618c | 437 | *osabi = GDB_OSABI_SOLARIS; |
70f80edf JT |
438 | break; |
439 | ||
261de166 | 440 | case GNU_ABI_TAG_FREEBSD: |
a49d618c | 441 | *osabi = GDB_OSABI_FREEBSD_ELF; |
261de166 | 442 | break; |
a49d618c | 443 | |
261de166 | 444 | case GNU_ABI_TAG_NETBSD: |
a49d618c | 445 | *osabi = GDB_OSABI_NETBSD_ELF; |
261de166 | 446 | break; |
a49d618c | 447 | |
70f80edf | 448 | default: |
a49d618c MK |
449 | internal_error (__FILE__, __LINE__, "\ |
450 | generic_elf_osabi_sniff_abi_tag_sections: unknown OS number %d", | |
451 | abi_tag); | |
70f80edf JT |
452 | } |
453 | return; | |
454 | } | |
a49d618c MK |
455 | |
456 | /* FreeBSD. */ | |
457 | if (check_note (abfd, sect, note, "FreeBSD", 4, NT_FREEBSD_ABI_TAG)) | |
70f80edf | 458 | { |
a49d618c MK |
459 | /* There is no need to check the version yet. */ |
460 | *osabi = GDB_OSABI_FREEBSD_ELF; | |
461 | return; | |
70f80edf | 462 | } |
a49d618c | 463 | |
70f80edf JT |
464 | return; |
465 | } | |
a49d618c | 466 | |
70f80edf | 467 | /* .note.netbsd.ident notes, used by NetBSD. */ |
a49d618c MK |
468 | if (strcmp (name, ".note.netbsd.ident") == 0 |
469 | && check_note (abfd, sect, note, "NetBSD", 4, NT_NETBSD_IDENT)) | |
70f80edf | 470 | { |
a49d618c MK |
471 | /* There is no need to check the version yet. */ |
472 | *osabi = GDB_OSABI_NETBSD_ELF; | |
70f80edf JT |
473 | return; |
474 | } | |
597684c5 | 475 | |
642d8300 MK |
476 | /* .note.openbsd.ident notes, used by OpenBSD. */ |
477 | if (strcmp (name, ".note.openbsd.ident") == 0 | |
478 | && check_note (abfd, sect, note, "OpenBSD", 4, NT_OPENBSD_IDENT)) | |
479 | { | |
480 | /* There is no need to check the version yet. */ | |
481 | *osabi = GDB_OSABI_OPENBSD_ELF; | |
482 | return; | |
483 | } | |
484 | ||
597684c5 | 485 | /* .note.netbsdcore.procinfo notes, used by NetBSD. */ |
a49d618c | 486 | if (strcmp (name, ".note.netbsdcore.procinfo") == 0) |
597684c5 | 487 | { |
a49d618c | 488 | *osabi = GDB_OSABI_NETBSD_ELF; |
597684c5 MK |
489 | return; |
490 | } | |
70f80edf JT |
491 | } |
492 | ||
493 | static enum gdb_osabi | |
494 | generic_elf_osabi_sniffer (bfd *abfd) | |
495 | { | |
496 | unsigned int elfosabi; | |
497 | enum gdb_osabi osabi = GDB_OSABI_UNKNOWN; | |
498 | ||
499 | elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI]; | |
500 | ||
501 | switch (elfosabi) | |
502 | { | |
503 | case ELFOSABI_NONE: | |
504 | /* When elfosabi is ELFOSABI_NONE (0), then the ELF structures in the | |
505 | file are conforming to the base specification for that machine | |
506 | (there are no OS-specific extensions). In order to determine the | |
507 | real OS in use we must look for OS notes that have been added. */ | |
508 | bfd_map_over_sections (abfd, | |
509 | generic_elf_osabi_sniff_abi_tag_sections, | |
510 | &osabi); | |
511 | break; | |
512 | ||
513 | case ELFOSABI_FREEBSD: | |
514 | osabi = GDB_OSABI_FREEBSD_ELF; | |
515 | break; | |
516 | ||
517 | case ELFOSABI_NETBSD: | |
518 | osabi = GDB_OSABI_NETBSD_ELF; | |
519 | break; | |
520 | ||
521 | case ELFOSABI_LINUX: | |
522 | osabi = GDB_OSABI_LINUX; | |
523 | break; | |
524 | ||
525 | case ELFOSABI_HURD: | |
526 | osabi = GDB_OSABI_HURD; | |
527 | break; | |
528 | ||
529 | case ELFOSABI_SOLARIS: | |
530 | osabi = GDB_OSABI_SOLARIS; | |
531 | break; | |
a09a320d JB |
532 | |
533 | case ELFOSABI_HPUX: | |
534 | osabi = GDB_OSABI_HPUX_ELF; | |
535 | break; | |
70f80edf JT |
536 | } |
537 | ||
bb21884d MK |
538 | if (osabi == GDB_OSABI_UNKNOWN) |
539 | { | |
540 | /* The FreeBSD folks have been naughty; they stored the string | |
541 | "FreeBSD" in the padding of the e_ident field of the ELF | |
542 | header to "brand" their ELF binaries in FreeBSD 3.x. */ | |
543 | if (strcmp (&elf_elfheader (abfd)->e_ident[8], "FreeBSD") == 0) | |
544 | osabi = GDB_OSABI_FREEBSD_ELF; | |
545 | } | |
546 | ||
70f80edf JT |
547 | return osabi; |
548 | } | |
549 | \f | |
b00a8037 DJ |
550 | static void |
551 | set_osabi (char *args, int from_tty, struct cmd_list_element *c) | |
552 | { | |
553 | struct gdbarch_info info; | |
70f80edf | 554 | |
b00a8037 DJ |
555 | if (strcmp (set_osabi_string, "auto") == 0) |
556 | user_osabi_state = osabi_auto; | |
557 | else if (strcmp (set_osabi_string, "default") == 0) | |
558 | { | |
559 | user_selected_osabi = GDB_OSABI_DEFAULT; | |
560 | user_osabi_state = osabi_user; | |
561 | } | |
562 | else if (strcmp (set_osabi_string, "none") == 0) | |
563 | { | |
564 | user_selected_osabi = GDB_OSABI_UNKNOWN; | |
565 | user_osabi_state = osabi_user; | |
566 | } | |
567 | else | |
568 | { | |
569 | int i; | |
570 | for (i = 1; i < GDB_OSABI_INVALID; i++) | |
571 | if (strcmp (set_osabi_string, gdbarch_osabi_name (i)) == 0) | |
572 | { | |
573 | user_selected_osabi = i; | |
574 | user_osabi_state = osabi_user; | |
575 | break; | |
576 | } | |
577 | if (i == GDB_OSABI_INVALID) | |
578 | internal_error (__FILE__, __LINE__, | |
579 | "Invalid OS ABI \"%s\" passed to command handler.", | |
580 | set_osabi_string); | |
581 | } | |
582 | ||
583 | /* NOTE: At some point (true multiple architectures) we'll need to be more | |
584 | graceful here. */ | |
585 | gdbarch_info_init (&info); | |
586 | if (! gdbarch_update_p (info)) | |
587 | internal_error (__FILE__, __LINE__, "Updating OS ABI failed."); | |
588 | } | |
589 | ||
b9362cc7 | 590 | static void |
b00a8037 DJ |
591 | show_osabi (char *args, int from_tty) |
592 | { | |
593 | if (user_osabi_state == osabi_auto) | |
594 | printf_filtered ("The current OS ABI is \"auto\" (currently \"%s\").\n", | |
595 | gdbarch_osabi_name (gdbarch_osabi (current_gdbarch))); | |
596 | else | |
597 | printf_filtered ("The current OS ABI is \"%s\".\n", | |
598 | gdbarch_osabi_name (user_selected_osabi)); | |
599 | ||
600 | if (GDB_OSABI_DEFAULT != GDB_OSABI_UNKNOWN) | |
601 | printf_filtered ("The default OS ABI is \"%s\".\n", | |
602 | gdbarch_osabi_name (GDB_OSABI_DEFAULT)); | |
603 | } | |
604 | \f | |
b9362cc7 AC |
605 | extern initialize_file_ftype _initialize_gdb_osabi; /* -Wmissing-prototype */ |
606 | ||
70f80edf JT |
607 | void |
608 | _initialize_gdb_osabi (void) | |
609 | { | |
b00a8037 DJ |
610 | struct cmd_list_element *c; |
611 | ||
70f80edf JT |
612 | if (strcmp (gdb_osabi_names[GDB_OSABI_INVALID], "<invalid>") != 0) |
613 | internal_error | |
614 | (__FILE__, __LINE__, | |
615 | "_initialize_gdb_osabi: gdb_osabi_names[] is inconsistent"); | |
616 | ||
617 | /* Register a generic sniffer for ELF flavoured files. */ | |
618 | gdbarch_register_osabi_sniffer (bfd_arch_unknown, | |
619 | bfd_target_elf_flavour, | |
620 | generic_elf_osabi_sniffer); | |
b00a8037 | 621 | |
b00a8037 DJ |
622 | /* Register the "set osabi" command. */ |
623 | c = add_set_enum_cmd ("osabi", class_support, gdb_osabi_available_names, | |
624 | &set_osabi_string, "Set OS ABI of target.", &setlist); | |
625 | ||
626 | set_cmd_sfunc (c, set_osabi); | |
627 | add_cmd ("osabi", class_support, show_osabi, "Show OS/ABI of target.", | |
628 | &showlist); | |
629 | user_osabi_state = osabi_auto; | |
70f80edf | 630 | } |