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