Commit | Line | Data |
---|---|---|
70f80edf | 1 | /* OS ABI variant handling for GDB. |
533f1d8f | 2 | |
ecd75fc8 | 3 | Copyright (C) 2001-2014 Free Software Foundation, Inc. |
70f80edf JT |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
70f80edf JT |
10 | (at your option) any later version. |
11 | ||
a9762ec7 | 12 | This program is distributed in the hope that it will be useful, |
70f80edf JT |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
a9762ec7 | 16 | |
70f80edf | 17 | You should have received a copy of the GNU General Public License |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
70f80edf JT |
19 | |
20 | #include "defs.h" | |
05816f70 | 21 | |
70f80edf | 22 | #include "osabi.h" |
b00a8037 DJ |
23 | #include "arch-utils.h" |
24 | #include "gdbcmd.h" | |
25 | #include "command.h" | |
70f80edf JT |
26 | |
27 | #include "elf-bfd.h" | |
28 | ||
b00a8037 DJ |
29 | #ifndef GDB_OSABI_DEFAULT |
30 | #define GDB_OSABI_DEFAULT GDB_OSABI_UNKNOWN | |
31 | #endif | |
32 | ||
33 | /* State for the "set osabi" command. */ | |
34 | static enum { osabi_auto, osabi_default, osabi_user } user_osabi_state; | |
35 | static enum gdb_osabi user_selected_osabi; | |
36 | static const char *gdb_osabi_available_names[GDB_OSABI_INVALID + 3] = { | |
37 | "auto", | |
38 | "default", | |
39 | "none", | |
40 | NULL | |
41 | }; | |
42 | static const char *set_osabi_string; | |
70f80edf JT |
43 | |
44 | /* This table matches the indices assigned to enum gdb_osabi. Keep | |
45 | them in sync. */ | |
46 | static const char * const gdb_osabi_names[] = | |
47 | { | |
b00a8037 | 48 | "none", |
70f80edf JT |
49 | |
50 | "SVR4", | |
51 | "GNU/Hurd", | |
52 | "Solaris", | |
53 | "OSF/1", | |
54 | "GNU/Linux", | |
55 | "FreeBSD a.out", | |
56 | "FreeBSD ELF", | |
57 | "NetBSD a.out", | |
58 | "NetBSD ELF", | |
d33b9831 | 59 | "OpenBSD ELF", |
70f80edf | 60 | "Windows CE", |
1029b7fa | 61 | "DJGPP", |
b96d0a4e | 62 | "Irix", |
a09a320d JB |
63 | "HP/UX ELF", |
64 | "HP/UX SOM", | |
83461b86 | 65 | "QNX Neutrino", |
1762d96d | 66 | "Cygwin", |
1f82754b | 67 | "AIX", |
a15c5c83 | 68 | "DICOS", |
a80b95ba | 69 | "Darwin", |
78664fa3 | 70 | "Symbian", |
34864976 | 71 | "OpenVMS", |
d5367fe1 | 72 | "LynxOS178", |
cf0dbd6f | 73 | "Newlib", |
1762d96d | 74 | |
70f80edf JT |
75 | "<invalid>" |
76 | }; | |
77 | ||
78 | const char * | |
79 | gdbarch_osabi_name (enum gdb_osabi osabi) | |
80 | { | |
81 | if (osabi >= GDB_OSABI_UNKNOWN && osabi < GDB_OSABI_INVALID) | |
82 | return gdb_osabi_names[osabi]; | |
83 | ||
84 | return gdb_osabi_names[GDB_OSABI_INVALID]; | |
85 | } | |
86 | ||
08d16641 PA |
87 | /* Lookup the OS ABI corresponding to the specified target description |
88 | string. */ | |
89 | ||
90 | enum gdb_osabi | |
91 | osabi_from_tdesc_string (const char *name) | |
92 | { | |
93 | int i; | |
94 | ||
95 | for (i = 0; i < ARRAY_SIZE (gdb_osabi_names); i++) | |
96 | if (strcmp (name, gdb_osabi_names[i]) == 0) | |
97 | { | |
98 | /* See note above: the name table matches the indices assigned | |
99 | to enum gdb_osabi. */ | |
100 | enum gdb_osabi osabi = (enum gdb_osabi) i; | |
101 | ||
102 | if (osabi == GDB_OSABI_INVALID) | |
103 | return GDB_OSABI_UNKNOWN; | |
104 | else | |
105 | return osabi; | |
106 | } | |
107 | ||
108 | return GDB_OSABI_UNKNOWN; | |
109 | } | |
110 | ||
70f80edf JT |
111 | /* Handler for a given architecture/OS ABI pair. There should be only |
112 | one handler for a given OS ABI each architecture family. */ | |
113 | struct gdb_osabi_handler | |
114 | { | |
115 | struct gdb_osabi_handler *next; | |
05816f70 | 116 | const struct bfd_arch_info *arch_info; |
70f80edf JT |
117 | enum gdb_osabi osabi; |
118 | void (*init_osabi)(struct gdbarch_info, struct gdbarch *); | |
119 | }; | |
120 | ||
121 | static struct gdb_osabi_handler *gdb_osabi_handler_list; | |
122 | ||
123 | void | |
05816f70 MK |
124 | gdbarch_register_osabi (enum bfd_architecture arch, unsigned long machine, |
125 | enum gdb_osabi osabi, | |
70f80edf JT |
126 | void (*init_osabi)(struct gdbarch_info, |
127 | struct gdbarch *)) | |
128 | { | |
129 | struct gdb_osabi_handler **handler_p; | |
05816f70 | 130 | const struct bfd_arch_info *arch_info = bfd_lookup_arch (arch, machine); |
b00a8037 | 131 | const char **name_ptr; |
70f80edf JT |
132 | |
133 | /* Registering an OS ABI handler for "unknown" is not allowed. */ | |
134 | if (osabi == GDB_OSABI_UNKNOWN) | |
135 | { | |
136 | internal_error | |
137 | (__FILE__, __LINE__, | |
e2e0b3e5 | 138 | _("gdbarch_register_osabi: An attempt to register a handler for " |
70f80edf | 139 | "OS ABI \"%s\" for architecture %s was made. The handler will " |
e2e0b3e5 | 140 | "not be registered"), |
70f80edf | 141 | gdbarch_osabi_name (osabi), |
05816f70 | 142 | bfd_printable_arch_mach (arch, machine)); |
70f80edf JT |
143 | return; |
144 | } | |
145 | ||
05816f70 MK |
146 | gdb_assert (arch_info); |
147 | ||
70f80edf JT |
148 | for (handler_p = &gdb_osabi_handler_list; *handler_p != NULL; |
149 | handler_p = &(*handler_p)->next) | |
150 | { | |
05816f70 | 151 | if ((*handler_p)->arch_info == arch_info |
70f80edf JT |
152 | && (*handler_p)->osabi == osabi) |
153 | { | |
154 | internal_error | |
155 | (__FILE__, __LINE__, | |
e2e0b3e5 AC |
156 | _("gdbarch_register_osabi: A handler for OS ABI \"%s\" " |
157 | "has already been registered for architecture %s"), | |
70f80edf | 158 | gdbarch_osabi_name (osabi), |
05816f70 | 159 | arch_info->printable_name); |
70f80edf JT |
160 | /* If user wants to continue, override previous definition. */ |
161 | (*handler_p)->init_osabi = init_osabi; | |
162 | return; | |
163 | } | |
164 | } | |
165 | ||
166 | (*handler_p) | |
167 | = (struct gdb_osabi_handler *) xmalloc (sizeof (struct gdb_osabi_handler)); | |
168 | (*handler_p)->next = NULL; | |
05816f70 | 169 | (*handler_p)->arch_info = arch_info; |
70f80edf JT |
170 | (*handler_p)->osabi = osabi; |
171 | (*handler_p)->init_osabi = init_osabi; | |
b00a8037 DJ |
172 | |
173 | /* Add this OS ABI to the list of enum values for "set osabi", if it isn't | |
174 | already there. */ | |
175 | for (name_ptr = gdb_osabi_available_names; *name_ptr; name_ptr ++) | |
176 | { | |
177 | if (*name_ptr == gdbarch_osabi_name (osabi)) | |
178 | return; | |
179 | } | |
180 | *name_ptr++ = gdbarch_osabi_name (osabi); | |
181 | *name_ptr = NULL; | |
70f80edf JT |
182 | } |
183 | \f | |
184 | ||
0df8b418 | 185 | /* Sniffer to find the OS ABI for a given file's architecture and flavour. |
70f80edf JT |
186 | It is legal to have multiple sniffers for each arch/flavour pair, to |
187 | disambiguate one OS's a.out from another, for example. The first sniffer | |
188 | to return something other than GDB_OSABI_UNKNOWN wins, so a sniffer should | |
189 | be careful to claim a file only if it knows for sure what it is. */ | |
190 | struct gdb_osabi_sniffer | |
191 | { | |
192 | struct gdb_osabi_sniffer *next; | |
193 | enum bfd_architecture arch; /* bfd_arch_unknown == wildcard */ | |
194 | enum bfd_flavour flavour; | |
195 | enum gdb_osabi (*sniffer)(bfd *); | |
196 | }; | |
197 | ||
198 | static struct gdb_osabi_sniffer *gdb_osabi_sniffer_list; | |
199 | ||
200 | void | |
201 | gdbarch_register_osabi_sniffer (enum bfd_architecture arch, | |
202 | enum bfd_flavour flavour, | |
203 | enum gdb_osabi (*sniffer_fn)(bfd *)) | |
204 | { | |
205 | struct gdb_osabi_sniffer *sniffer; | |
206 | ||
207 | sniffer = | |
208 | (struct gdb_osabi_sniffer *) xmalloc (sizeof (struct gdb_osabi_sniffer)); | |
209 | sniffer->arch = arch; | |
210 | sniffer->flavour = flavour; | |
211 | sniffer->sniffer = sniffer_fn; | |
212 | ||
213 | sniffer->next = gdb_osabi_sniffer_list; | |
214 | gdb_osabi_sniffer_list = sniffer; | |
215 | } | |
216 | \f | |
217 | ||
218 | enum gdb_osabi | |
219 | gdbarch_lookup_osabi (bfd *abfd) | |
220 | { | |
221 | struct gdb_osabi_sniffer *sniffer; | |
222 | enum gdb_osabi osabi, match; | |
223 | int match_specific; | |
224 | ||
b00a8037 DJ |
225 | /* If we aren't in "auto" mode, return the specified OS ABI. */ |
226 | if (user_osabi_state == osabi_user) | |
227 | return user_selected_osabi; | |
228 | ||
08d16641 PA |
229 | /* If we don't have a binary, just return unknown. The caller may |
230 | have other sources the OSABI can be extracted from, e.g., the | |
231 | target description. */ | |
b00a8037 | 232 | if (abfd == NULL) |
08d16641 | 233 | return GDB_OSABI_UNKNOWN; |
4be87837 | 234 | |
70f80edf JT |
235 | match = GDB_OSABI_UNKNOWN; |
236 | match_specific = 0; | |
237 | ||
238 | for (sniffer = gdb_osabi_sniffer_list; sniffer != NULL; | |
239 | sniffer = sniffer->next) | |
240 | { | |
241 | if ((sniffer->arch == bfd_arch_unknown /* wildcard */ | |
242 | || sniffer->arch == bfd_get_arch (abfd)) | |
243 | && sniffer->flavour == bfd_get_flavour (abfd)) | |
244 | { | |
245 | osabi = (*sniffer->sniffer) (abfd); | |
246 | if (osabi < GDB_OSABI_UNKNOWN || osabi >= GDB_OSABI_INVALID) | |
247 | { | |
248 | internal_error | |
249 | (__FILE__, __LINE__, | |
e2e0b3e5 AC |
250 | _("gdbarch_lookup_osabi: invalid OS ABI (%d) from sniffer " |
251 | "for architecture %s flavour %d"), | |
70f80edf JT |
252 | (int) osabi, |
253 | bfd_printable_arch_mach (bfd_get_arch (abfd), 0), | |
254 | (int) bfd_get_flavour (abfd)); | |
255 | } | |
256 | else if (osabi != GDB_OSABI_UNKNOWN) | |
257 | { | |
258 | /* A specific sniffer always overrides a generic sniffer. | |
259 | Croak on multiple match if the two matches are of the | |
260 | same class. If the user wishes to continue, we'll use | |
261 | the first match. */ | |
262 | if (match != GDB_OSABI_UNKNOWN) | |
263 | { | |
264 | if ((match_specific && sniffer->arch != bfd_arch_unknown) | |
265 | || (!match_specific && sniffer->arch == bfd_arch_unknown)) | |
266 | { | |
267 | internal_error | |
268 | (__FILE__, __LINE__, | |
e2e0b3e5 | 269 | _("gdbarch_lookup_osabi: multiple %sspecific OS ABI " |
70f80edf | 270 | "match for architecture %s flavour %d: first " |
e2e0b3e5 | 271 | "match \"%s\", second match \"%s\""), |
70f80edf JT |
272 | match_specific ? "" : "non-", |
273 | bfd_printable_arch_mach (bfd_get_arch (abfd), 0), | |
274 | (int) bfd_get_flavour (abfd), | |
275 | gdbarch_osabi_name (match), | |
276 | gdbarch_osabi_name (osabi)); | |
277 | } | |
278 | else if (sniffer->arch != bfd_arch_unknown) | |
279 | { | |
280 | match = osabi; | |
281 | match_specific = 1; | |
282 | } | |
283 | } | |
284 | else | |
285 | { | |
286 | match = osabi; | |
287 | if (sniffer->arch != bfd_arch_unknown) | |
288 | match_specific = 1; | |
289 | } | |
290 | } | |
291 | } | |
292 | } | |
293 | ||
08d16641 | 294 | return match; |
70f80edf JT |
295 | } |
296 | ||
6cfae0bc JB |
297 | |
298 | /* Return non-zero if architecture A can run code written for | |
299 | architecture B. */ | |
300 | static int | |
301 | can_run_code_for (const struct bfd_arch_info *a, const struct bfd_arch_info *b) | |
302 | { | |
303 | /* BFD's 'A->compatible (A, B)' functions return zero if A and B are | |
304 | incompatible. But if they are compatible, it returns the 'more | |
305 | featureful' of the two arches. That is, if A can run code | |
306 | written for B, but B can't run code written for A, then it'll | |
307 | return A. | |
308 | ||
5f724446 AC |
309 | struct bfd_arch_info objects are singletons: that is, there's |
310 | supposed to be exactly one instance for a given machine. So you | |
311 | can tell whether two are equivalent by comparing pointers. */ | |
6cfae0bc JB |
312 | return (a == b || a->compatible (a, b) == a); |
313 | } | |
314 | ||
315 | ||
70f80edf | 316 | void |
4be87837 | 317 | gdbarch_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch) |
70f80edf | 318 | { |
05816f70 | 319 | struct gdb_osabi_handler *handler; |
70f80edf | 320 | |
4be87837 | 321 | if (info.osabi == GDB_OSABI_UNKNOWN) |
70f80edf | 322 | { |
01fc4e33 AC |
323 | /* Don't complain about an unknown OSABI. Assume the user knows |
324 | what they are doing. */ | |
70f80edf JT |
325 | return; |
326 | } | |
327 | ||
328 | for (handler = gdb_osabi_handler_list; handler != NULL; | |
329 | handler = handler->next) | |
330 | { | |
4be87837 | 331 | if (handler->osabi != info.osabi) |
05816f70 MK |
332 | continue; |
333 | ||
6cfae0bc JB |
334 | /* If the architecture described by ARCH_INFO can run code for |
335 | the architcture we registered the handler for, then the | |
336 | handler is applicable. Note, though, that if the handler is | |
337 | for an architecture that is a superset of ARCH_INFO, we can't | |
338 | use that --- it would be perfectly correct for it to install | |
339 | gdbarch methods that refer to registers / instructions / | |
340 | other facilities ARCH_INFO doesn't have. | |
05816f70 | 341 | |
6cfae0bc | 342 | NOTE: kettenis/20021027: There may be more than one machine |
05816f70 MK |
343 | type that is compatible with the desired machine type. Right |
344 | now we simply return the first match, which is fine for now. | |
345 | However, we might want to do something smarter in the future. */ | |
5f724446 AC |
346 | /* NOTE: cagney/2003-10-23: The code for "a can_run_code_for b" |
347 | is implemented using BFD's compatible method (a->compatible | |
348 | (b) == a -- the lowest common denominator between a and b is | |
349 | a). That method's definition of compatible may not be as you | |
ce2826aa | 350 | expect. For instance the test "amd64 can run code for i386" |
5f724446 | 351 | (or more generally "64-bit ISA can run code for the 32-bit |
ce2826aa AC |
352 | ISA"). BFD doesn't normally consider 32-bit and 64-bit |
353 | "compatible" so it doesn't succeed. */ | |
931758af | 354 | if (can_run_code_for (info.bfd_arch_info, handler->arch_info)) |
70f80edf JT |
355 | { |
356 | (*handler->init_osabi) (info, gdbarch); | |
357 | return; | |
358 | } | |
359 | } | |
360 | ||
e481341f JB |
361 | warning |
362 | ("A handler for the OS ABI \"%s\" is not built into this configuration\n" | |
363 | "of GDB. Attempting to continue with the default %s settings.\n", | |
364 | gdbarch_osabi_name (info.osabi), | |
365 | info.bfd_arch_info->printable_name); | |
70f80edf JT |
366 | } |
367 | \f | |
a49d618c MK |
368 | /* Limit on the amount of data to be read. */ |
369 | #define MAX_NOTESZ 128 | |
370 | ||
ea9f10bb TT |
371 | /* Return non-zero if NOTE matches NAME, DESCSZ and TYPE. If |
372 | *SECTSIZE is non-zero, then this reads that many bytes from | |
373 | the start of the section and clears *SECTSIZE. */ | |
a49d618c MK |
374 | |
375 | static int | |
ea9f10bb | 376 | check_note (bfd *abfd, asection *sect, char *note, unsigned int *sectsize, |
a49d618c MK |
377 | const char *name, unsigned long descsz, unsigned long type) |
378 | { | |
379 | unsigned long notesz; | |
380 | ||
ea9f10bb TT |
381 | if (*sectsize) |
382 | { | |
383 | if (!bfd_get_section_contents (abfd, sect, note, 0, *sectsize)) | |
384 | return 0; | |
385 | *sectsize = 0; | |
386 | } | |
387 | ||
a49d618c MK |
388 | /* Calculate the size of this note. */ |
389 | notesz = strlen (name) + 1; | |
390 | notesz = ((notesz + 3) & ~3); | |
391 | notesz += descsz; | |
392 | notesz = ((notesz + 3) & ~3); | |
393 | ||
394 | /* If this assertion triggers, increase MAX_NOTESZ. */ | |
395 | gdb_assert (notesz <= MAX_NOTESZ); | |
396 | ||
397 | /* Check whether SECT is big enough to comtain the complete note. */ | |
398 | if (notesz > bfd_section_size (abfd, sect)) | |
399 | return 0; | |
400 | ||
401 | /* Check the note name. */ | |
402 | if (bfd_h_get_32 (abfd, note) != (strlen (name) + 1) | |
403 | || strcmp (note + 12, name) != 0) | |
404 | return 0; | |
405 | ||
406 | /* Check the descriptor size. */ | |
407 | if (bfd_h_get_32 (abfd, note + 4) != descsz) | |
408 | return 0; | |
409 | ||
410 | /* Check the note type. */ | |
411 | if (bfd_h_get_32 (abfd, note + 8) != type) | |
412 | return 0; | |
413 | ||
414 | return 1; | |
415 | } | |
70f80edf JT |
416 | |
417 | /* Generic sniffer for ELF flavoured files. */ | |
418 | ||
419 | void | |
420 | generic_elf_osabi_sniff_abi_tag_sections (bfd *abfd, asection *sect, void *obj) | |
421 | { | |
a49d618c | 422 | enum gdb_osabi *osabi = obj; |
70f80edf JT |
423 | const char *name; |
424 | unsigned int sectsize; | |
a49d618c | 425 | char *note; |
70f80edf JT |
426 | |
427 | name = bfd_get_section_name (abfd, sect); | |
428 | sectsize = bfd_section_size (abfd, sect); | |
429 | ||
a49d618c MK |
430 | /* Limit the amount of data to read. */ |
431 | if (sectsize > MAX_NOTESZ) | |
432 | sectsize = MAX_NOTESZ; | |
70f80edf | 433 | |
ea9f10bb TT |
434 | /* We lazily read the section data here. Since we use |
435 | BFD_DECOMPRESS, we can't use bfd_get_section_contents on a | |
436 | compressed section. But, since note sections are not compressed, | |
437 | deferring the reading until we recognize the section avoids any | |
438 | error. */ | |
a49d618c | 439 | note = alloca (sectsize); |
70f80edf | 440 | |
a49d618c MK |
441 | /* .note.ABI-tag notes, used by GNU/Linux and FreeBSD. */ |
442 | if (strcmp (name, ".note.ABI-tag") == 0) | |
443 | { | |
444 | /* GNU. */ | |
ea9f10bb | 445 | if (check_note (abfd, sect, note, §size, "GNU", 16, NT_GNU_ABI_TAG)) |
70f80edf | 446 | { |
a49d618c | 447 | unsigned int abi_tag = bfd_h_get_32 (abfd, note + 16); |
70f80edf | 448 | |
a49d618c | 449 | switch (abi_tag) |
70f80edf JT |
450 | { |
451 | case GNU_ABI_TAG_LINUX: | |
a49d618c | 452 | *osabi = GDB_OSABI_LINUX; |
70f80edf JT |
453 | break; |
454 | ||
455 | case GNU_ABI_TAG_HURD: | |
a49d618c | 456 | *osabi = GDB_OSABI_HURD; |
70f80edf JT |
457 | break; |
458 | ||
459 | case GNU_ABI_TAG_SOLARIS: | |
a49d618c | 460 | *osabi = GDB_OSABI_SOLARIS; |
70f80edf JT |
461 | break; |
462 | ||
261de166 | 463 | case GNU_ABI_TAG_FREEBSD: |
a49d618c | 464 | *osabi = GDB_OSABI_FREEBSD_ELF; |
261de166 | 465 | break; |
a49d618c | 466 | |
261de166 | 467 | case GNU_ABI_TAG_NETBSD: |
a49d618c | 468 | *osabi = GDB_OSABI_NETBSD_ELF; |
261de166 | 469 | break; |
a49d618c | 470 | |
70f80edf | 471 | default: |
3e43a32a MS |
472 | internal_error (__FILE__, __LINE__, |
473 | _("generic_elf_osabi_sniff_abi_tag_sections: " | |
474 | "unknown OS number %d"), | |
a49d618c | 475 | abi_tag); |
70f80edf JT |
476 | } |
477 | return; | |
478 | } | |
a49d618c MK |
479 | |
480 | /* FreeBSD. */ | |
ea9f10bb TT |
481 | if (check_note (abfd, sect, note, §size, "FreeBSD", 4, |
482 | NT_FREEBSD_ABI_TAG)) | |
70f80edf | 483 | { |
a49d618c MK |
484 | /* There is no need to check the version yet. */ |
485 | *osabi = GDB_OSABI_FREEBSD_ELF; | |
486 | return; | |
70f80edf | 487 | } |
a49d618c | 488 | |
70f80edf JT |
489 | return; |
490 | } | |
a49d618c | 491 | |
70f80edf | 492 | /* .note.netbsd.ident notes, used by NetBSD. */ |
a49d618c | 493 | if (strcmp (name, ".note.netbsd.ident") == 0 |
ea9f10bb | 494 | && check_note (abfd, sect, note, §size, "NetBSD", 4, NT_NETBSD_IDENT)) |
70f80edf | 495 | { |
a49d618c MK |
496 | /* There is no need to check the version yet. */ |
497 | *osabi = GDB_OSABI_NETBSD_ELF; | |
70f80edf JT |
498 | return; |
499 | } | |
597684c5 | 500 | |
642d8300 MK |
501 | /* .note.openbsd.ident notes, used by OpenBSD. */ |
502 | if (strcmp (name, ".note.openbsd.ident") == 0 | |
ea9f10bb TT |
503 | && check_note (abfd, sect, note, §size, "OpenBSD", 4, |
504 | NT_OPENBSD_IDENT)) | |
642d8300 MK |
505 | { |
506 | /* There is no need to check the version yet. */ | |
507 | *osabi = GDB_OSABI_OPENBSD_ELF; | |
508 | return; | |
509 | } | |
510 | ||
597684c5 | 511 | /* .note.netbsdcore.procinfo notes, used by NetBSD. */ |
a49d618c | 512 | if (strcmp (name, ".note.netbsdcore.procinfo") == 0) |
597684c5 | 513 | { |
a49d618c | 514 | *osabi = GDB_OSABI_NETBSD_ELF; |
597684c5 MK |
515 | return; |
516 | } | |
70f80edf JT |
517 | } |
518 | ||
519 | static enum gdb_osabi | |
520 | generic_elf_osabi_sniffer (bfd *abfd) | |
521 | { | |
522 | unsigned int elfosabi; | |
523 | enum gdb_osabi osabi = GDB_OSABI_UNKNOWN; | |
524 | ||
525 | elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI]; | |
526 | ||
527 | switch (elfosabi) | |
528 | { | |
529 | case ELFOSABI_NONE: | |
59adf69e | 530 | case ELFOSABI_GNU: |
533f1d8f MK |
531 | /* When the EI_OSABI field in the ELF header is ELFOSABI_NONE |
532 | (0), then the ELF structures in the file are conforming to | |
533 | the base specification for that machine (there are no | |
534 | OS-specific extensions). In order to determine the real OS | |
efae1d92 JB |
535 | in use, we must look for OS-specific notes. |
536 | ||
537 | The same applies for ELFOSABI_GNU: this can mean GNU/Hurd, | |
538 | GNU/Linux, and possibly more. */ | |
70f80edf JT |
539 | bfd_map_over_sections (abfd, |
540 | generic_elf_osabi_sniff_abi_tag_sections, | |
541 | &osabi); | |
542 | break; | |
543 | ||
544 | case ELFOSABI_FREEBSD: | |
545 | osabi = GDB_OSABI_FREEBSD_ELF; | |
546 | break; | |
547 | ||
548 | case ELFOSABI_NETBSD: | |
549 | osabi = GDB_OSABI_NETBSD_ELF; | |
550 | break; | |
551 | ||
70f80edf JT |
552 | case ELFOSABI_SOLARIS: |
553 | osabi = GDB_OSABI_SOLARIS; | |
554 | break; | |
a09a320d JB |
555 | |
556 | case ELFOSABI_HPUX: | |
533f1d8f MK |
557 | /* For some reason the default value for the EI_OSABI field is |
558 | ELFOSABI_HPUX for all PA-RISC targets (with the exception of | |
559 | GNU/Linux). We use HP-UX ELF as the default, but let any | |
560 | OS-specific notes override this. */ | |
a09a320d | 561 | osabi = GDB_OSABI_HPUX_ELF; |
533f1d8f MK |
562 | bfd_map_over_sections (abfd, |
563 | generic_elf_osabi_sniff_abi_tag_sections, | |
564 | &osabi); | |
a09a320d | 565 | break; |
34864976 TG |
566 | |
567 | case ELFOSABI_OPENVMS: | |
568 | osabi = GDB_OSABI_OPENVMS; | |
569 | break; | |
70f80edf JT |
570 | } |
571 | ||
bb21884d MK |
572 | if (osabi == GDB_OSABI_UNKNOWN) |
573 | { | |
574 | /* The FreeBSD folks have been naughty; they stored the string | |
575 | "FreeBSD" in the padding of the e_ident field of the ELF | |
576 | header to "brand" their ELF binaries in FreeBSD 3.x. */ | |
1731e543 | 577 | if (memcmp (&elf_elfheader (abfd)->e_ident[8], |
6062517a | 578 | "FreeBSD", sizeof ("FreeBSD")) == 0) |
bb21884d MK |
579 | osabi = GDB_OSABI_FREEBSD_ELF; |
580 | } | |
581 | ||
70f80edf JT |
582 | return osabi; |
583 | } | |
584 | \f | |
b00a8037 DJ |
585 | static void |
586 | set_osabi (char *args, int from_tty, struct cmd_list_element *c) | |
587 | { | |
588 | struct gdbarch_info info; | |
70f80edf | 589 | |
b00a8037 DJ |
590 | if (strcmp (set_osabi_string, "auto") == 0) |
591 | user_osabi_state = osabi_auto; | |
592 | else if (strcmp (set_osabi_string, "default") == 0) | |
593 | { | |
594 | user_selected_osabi = GDB_OSABI_DEFAULT; | |
595 | user_osabi_state = osabi_user; | |
596 | } | |
597 | else if (strcmp (set_osabi_string, "none") == 0) | |
598 | { | |
599 | user_selected_osabi = GDB_OSABI_UNKNOWN; | |
600 | user_osabi_state = osabi_user; | |
601 | } | |
602 | else | |
603 | { | |
604 | int i; | |
5cc80db3 | 605 | |
b00a8037 DJ |
606 | for (i = 1; i < GDB_OSABI_INVALID; i++) |
607 | if (strcmp (set_osabi_string, gdbarch_osabi_name (i)) == 0) | |
608 | { | |
609 | user_selected_osabi = i; | |
610 | user_osabi_state = osabi_user; | |
611 | break; | |
612 | } | |
613 | if (i == GDB_OSABI_INVALID) | |
614 | internal_error (__FILE__, __LINE__, | |
e2e0b3e5 | 615 | _("Invalid OS ABI \"%s\" passed to command handler."), |
b00a8037 DJ |
616 | set_osabi_string); |
617 | } | |
618 | ||
619 | /* NOTE: At some point (true multiple architectures) we'll need to be more | |
620 | graceful here. */ | |
621 | gdbarch_info_init (&info); | |
622 | if (! gdbarch_update_p (info)) | |
e2e0b3e5 | 623 | internal_error (__FILE__, __LINE__, _("Updating OS ABI failed.")); |
b00a8037 DJ |
624 | } |
625 | ||
b9362cc7 | 626 | static void |
7ab04401 AC |
627 | show_osabi (struct ui_file *file, int from_tty, struct cmd_list_element *c, |
628 | const char *value) | |
b00a8037 DJ |
629 | { |
630 | if (user_osabi_state == osabi_auto) | |
7ab04401 | 631 | fprintf_filtered (file, |
3e43a32a MS |
632 | _("The current OS ABI is \"auto\" " |
633 | "(currently \"%s\").\n"), | |
e17c207e | 634 | gdbarch_osabi_name (gdbarch_osabi (get_current_arch ()))); |
b00a8037 | 635 | else |
7ab04401 AC |
636 | fprintf_filtered (file, _("The current OS ABI is \"%s\".\n"), |
637 | gdbarch_osabi_name (user_selected_osabi)); | |
b00a8037 DJ |
638 | |
639 | if (GDB_OSABI_DEFAULT != GDB_OSABI_UNKNOWN) | |
7ab04401 AC |
640 | fprintf_filtered (file, _("The default OS ABI is \"%s\".\n"), |
641 | gdbarch_osabi_name (GDB_OSABI_DEFAULT)); | |
b00a8037 DJ |
642 | } |
643 | \f | |
b9362cc7 AC |
644 | extern initialize_file_ftype _initialize_gdb_osabi; /* -Wmissing-prototype */ |
645 | ||
70f80edf JT |
646 | void |
647 | _initialize_gdb_osabi (void) | |
648 | { | |
649 | if (strcmp (gdb_osabi_names[GDB_OSABI_INVALID], "<invalid>") != 0) | |
650 | internal_error | |
651 | (__FILE__, __LINE__, | |
e2e0b3e5 | 652 | _("_initialize_gdb_osabi: gdb_osabi_names[] is inconsistent")); |
70f80edf JT |
653 | |
654 | /* Register a generic sniffer for ELF flavoured files. */ | |
655 | gdbarch_register_osabi_sniffer (bfd_arch_unknown, | |
656 | bfd_target_elf_flavour, | |
657 | generic_elf_osabi_sniffer); | |
b00a8037 | 658 | |
b00a8037 | 659 | /* Register the "set osabi" command. */ |
7ab04401 | 660 | add_setshow_enum_cmd ("osabi", class_support, gdb_osabi_available_names, |
3e43a32a MS |
661 | &set_osabi_string, |
662 | _("Set OS ABI of target."), | |
663 | _("Show OS ABI of target."), | |
664 | NULL, set_osabi, show_osabi, | |
7ab04401 | 665 | &setlist, &showlist); |
b00a8037 | 666 | user_osabi_state = osabi_auto; |
70f80edf | 667 | } |