1 1.1 christos /* Generic BFD support for file formats. 2 1.11 christos Copyright (C) 1990-2024 Free Software Foundation, Inc. 3 1.1 christos Written by Cygnus Support. 4 1.1 christos 5 1.1 christos This file is part of BFD, the Binary File Descriptor library. 6 1.1 christos 7 1.1 christos This program is free software; you can redistribute it and/or modify 8 1.1 christos it under the terms of the GNU General Public License as published by 9 1.1 christos the Free Software Foundation; either version 3 of the License, or 10 1.1 christos (at your option) any later version. 11 1.1 christos 12 1.1 christos This program is distributed in the hope that it will be useful, 13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 1.1 christos GNU General Public License for more details. 16 1.1 christos 17 1.1 christos You should have received a copy of the GNU General Public License 18 1.1 christos along with this program; if not, write to the Free Software 19 1.1 christos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 20 1.1 christos MA 02110-1301, USA. */ 21 1.1 christos 22 1.1 christos 23 1.1 christos /* 24 1.1 christos SECTION 25 1.1 christos File formats 26 1.1 christos 27 1.1 christos A format is a BFD concept of high level file contents type. The 28 1.1 christos formats supported by BFD are: 29 1.1 christos 30 1.1 christos o <<bfd_object>> 31 1.1 christos 32 1.1 christos The BFD may contain data, symbols, relocations and debug info. 33 1.1 christos 34 1.1 christos o <<bfd_archive>> 35 1.1 christos 36 1.1 christos The BFD contains other BFDs and an optional index. 37 1.1 christos 38 1.1 christos o <<bfd_core>> 39 1.1 christos 40 1.1 christos The BFD contains the result of an executable core dump. 41 1.1 christos 42 1.1 christos SUBSECTION 43 1.1 christos File format functions 44 1.1 christos */ 45 1.1 christos 46 1.1 christos #include "sysdep.h" 47 1.1 christos #include "bfd.h" 48 1.1 christos #include "libbfd.h" 49 1.1 christos 50 1.1 christos /* IMPORT from targets.c. */ 51 1.1 christos extern const size_t _bfd_target_vector_entries; 52 1.1 christos 53 1.1 christos /* 54 1.1 christos FUNCTION 55 1.1 christos bfd_check_format 56 1.1 christos 57 1.1 christos SYNOPSIS 58 1.10 christos bool bfd_check_format (bfd *abfd, bfd_format format); 59 1.1 christos 60 1.1 christos DESCRIPTION 61 1.1 christos Verify if the file attached to the BFD @var{abfd} is compatible 62 1.1 christos with the format @var{format} (i.e., one of <<bfd_object>>, 63 1.1 christos <<bfd_archive>> or <<bfd_core>>). 64 1.1 christos 65 1.1 christos If the BFD has been set to a specific target before the 66 1.1 christos call, only the named target and format combination is 67 1.1 christos checked. If the target has not been set, or has been set to 68 1.1 christos <<default>>, then all the known target backends is 69 1.1 christos interrogated to determine a match. If the default target 70 1.1 christos matches, it is used. If not, exactly one target must recognize 71 1.1 christos the file, or an error results. 72 1.1 christos 73 1.1 christos The function returns <<TRUE>> on success, otherwise <<FALSE>> 74 1.1 christos with one of the following error codes: 75 1.1 christos 76 1.1 christos o <<bfd_error_invalid_operation>> - 77 1.1 christos if <<format>> is not one of <<bfd_object>>, <<bfd_archive>> or 78 1.1 christos <<bfd_core>>. 79 1.1 christos 80 1.1 christos o <<bfd_error_system_call>> - 81 1.1 christos if an error occured during a read - even some file mismatches 82 1.1 christos can cause bfd_error_system_calls. 83 1.1 christos 84 1.1 christos o <<file_not_recognised>> - 85 1.1 christos none of the backends recognised the file format. 86 1.1 christos 87 1.1 christos o <<bfd_error_file_ambiguously_recognized>> - 88 1.1 christos more than one backend recognised the file format. 89 1.11 christos 90 1.11 christos When calling bfd_check_format (or bfd_check_format_matches), 91 1.11 christos any underlying file descriptor will be kept open for the 92 1.11 christos duration of the call. This is done to avoid races when 93 1.11 christos another thread calls bfd_cache_close_all. In this scenario, 94 1.11 christos the thread calling bfd_check_format must call bfd_cache_close 95 1.11 christos itself. 96 1.1 christos */ 97 1.1 christos 98 1.10 christos bool 99 1.1 christos bfd_check_format (bfd *abfd, bfd_format format) 100 1.1 christos { 101 1.1 christos return bfd_check_format_matches (abfd, format, NULL); 102 1.1 christos } 103 1.1 christos 104 1.1 christos struct bfd_preserve 105 1.1 christos { 106 1.1 christos void *marker; 107 1.1 christos void *tdata; 108 1.1 christos flagword flags; 109 1.11 christos const struct bfd_iovec *iovec; 110 1.11 christos void *iostream; 111 1.1 christos const struct bfd_arch_info *arch_info; 112 1.11 christos const struct bfd_build_id *build_id; 113 1.11 christos bfd_cleanup cleanup; 114 1.1 christos struct bfd_section *sections; 115 1.1 christos struct bfd_section *section_last; 116 1.1 christos unsigned int section_count; 117 1.8 christos unsigned int section_id; 118 1.11 christos unsigned int symcount; 119 1.11 christos bool read_only; 120 1.11 christos bfd_vma start_address; 121 1.1 christos struct bfd_hash_table section_htab; 122 1.1 christos }; 123 1.1 christos 124 1.1 christos /* When testing an object for compatibility with a particular target 125 1.1 christos back-end, the back-end object_p function needs to set up certain 126 1.1 christos fields in the bfd on successfully recognizing the object. This 127 1.1 christos typically happens in a piecemeal fashion, with failures possible at 128 1.1 christos many points. On failure, the bfd is supposed to be restored to its 129 1.1 christos initial state, which is virtually impossible. However, restoring a 130 1.1 christos subset of the bfd state works in practice. This function stores 131 1.1 christos the subset. */ 132 1.1 christos 133 1.10 christos static bool 134 1.9 christos bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve, 135 1.9 christos bfd_cleanup cleanup) 136 1.1 christos { 137 1.1 christos preserve->tdata = abfd->tdata.any; 138 1.1 christos preserve->arch_info = abfd->arch_info; 139 1.1 christos preserve->flags = abfd->flags; 140 1.11 christos preserve->iovec = abfd->iovec; 141 1.11 christos preserve->iostream = abfd->iostream; 142 1.1 christos preserve->sections = abfd->sections; 143 1.1 christos preserve->section_last = abfd->section_last; 144 1.1 christos preserve->section_count = abfd->section_count; 145 1.8 christos preserve->section_id = _bfd_section_id; 146 1.11 christos preserve->symcount = abfd->symcount; 147 1.11 christos preserve->read_only = abfd->read_only; 148 1.11 christos preserve->start_address = abfd->start_address; 149 1.1 christos preserve->section_htab = abfd->section_htab; 150 1.1 christos preserve->marker = bfd_alloc (abfd, 1); 151 1.7 christos preserve->build_id = abfd->build_id; 152 1.9 christos preserve->cleanup = cleanup; 153 1.1 christos if (preserve->marker == NULL) 154 1.10 christos return false; 155 1.1 christos 156 1.1 christos return bfd_hash_table_init (&abfd->section_htab, bfd_section_hash_newfunc, 157 1.1 christos sizeof (struct section_hash_entry)); 158 1.1 christos } 159 1.1 christos 160 1.11 christos /* A back-end object_p function may flip a bfd from file backed to 161 1.11 christos in-memory, eg. pe_ILF_object_p. In that case to restore the 162 1.11 christos original IO state we need to reopen the file. Conversely, if we 163 1.11 christos are restoring a previously matched pe ILF format and have been 164 1.11 christos checking further target matches using file IO then we need to close 165 1.11 christos the file and detach the bfd from the cache lru list. */ 166 1.11 christos 167 1.11 christos static void 168 1.11 christos io_reinit (bfd *abfd, struct bfd_preserve *preserve) 169 1.11 christos { 170 1.11 christos if (abfd->iovec != preserve->iovec) 171 1.11 christos { 172 1.11 christos /* Handle file backed to in-memory transition. bfd_cache_close 173 1.11 christos won't do anything unless abfd->iovec is the cache_iovec. 174 1.11 christos Don't be tempted to call iovec->bclose here. We don't want 175 1.11 christos to call memory_bclose, which would free the bim. The bim 176 1.11 christos must be kept if bfd_check_format_matches is going to decide 177 1.11 christos later that the PE format needing it is in fact the correct 178 1.11 christos target match. */ 179 1.11 christos bfd_cache_close (abfd); 180 1.11 christos abfd->iovec = preserve->iovec; 181 1.11 christos abfd->iostream = preserve->iostream; 182 1.11 christos 183 1.11 christos /* Handle in-memory to file backed transition. */ 184 1.11 christos if ((abfd->flags & BFD_CLOSED_BY_CACHE) != 0 185 1.11 christos && (abfd->flags & BFD_IN_MEMORY) != 0 186 1.11 christos && (preserve->flags & BFD_CLOSED_BY_CACHE) == 0 187 1.11 christos && (preserve->flags & BFD_IN_MEMORY) == 0) 188 1.11 christos bfd_open_file (abfd); 189 1.11 christos } 190 1.11 christos abfd->flags = preserve->flags; 191 1.11 christos } 192 1.11 christos 193 1.1 christos /* Clear out a subset of BFD state. */ 194 1.1 christos 195 1.1 christos static void 196 1.11 christos bfd_reinit (bfd *abfd, unsigned int section_id, 197 1.11 christos struct bfd_preserve *preserve, bfd_cleanup cleanup) 198 1.1 christos { 199 1.9 christos _bfd_section_id = section_id; 200 1.9 christos if (cleanup) 201 1.9 christos cleanup (abfd); 202 1.1 christos abfd->tdata.any = NULL; 203 1.1 christos abfd->arch_info = &bfd_default_arch_struct; 204 1.11 christos io_reinit (abfd, preserve); 205 1.11 christos abfd->symcount = 0; 206 1.11 christos abfd->read_only = 0; 207 1.11 christos abfd->start_address = 0; 208 1.10 christos abfd->build_id = NULL; 209 1.1 christos bfd_section_list_clear (abfd); 210 1.1 christos } 211 1.1 christos 212 1.1 christos /* Restores bfd state saved by bfd_preserve_save. */ 213 1.1 christos 214 1.9 christos static bfd_cleanup 215 1.1 christos bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve) 216 1.1 christos { 217 1.1 christos bfd_hash_table_free (&abfd->section_htab); 218 1.1 christos 219 1.1 christos abfd->tdata.any = preserve->tdata; 220 1.1 christos abfd->arch_info = preserve->arch_info; 221 1.11 christos io_reinit (abfd, preserve); 222 1.1 christos abfd->section_htab = preserve->section_htab; 223 1.1 christos abfd->sections = preserve->sections; 224 1.1 christos abfd->section_last = preserve->section_last; 225 1.1 christos abfd->section_count = preserve->section_count; 226 1.8 christos _bfd_section_id = preserve->section_id; 227 1.11 christos abfd->symcount = preserve->symcount; 228 1.11 christos abfd->read_only = preserve->read_only; 229 1.11 christos abfd->start_address = preserve->start_address; 230 1.7 christos abfd->build_id = preserve->build_id; 231 1.1 christos 232 1.1 christos /* bfd_release frees all memory more recently bfd_alloc'd than 233 1.1 christos its arg, as well as its arg. */ 234 1.1 christos bfd_release (abfd, preserve->marker); 235 1.1 christos preserve->marker = NULL; 236 1.9 christos return preserve->cleanup; 237 1.1 christos } 238 1.1 christos 239 1.1 christos /* Called when the bfd state saved by bfd_preserve_save is no longer 240 1.1 christos needed. */ 241 1.1 christos 242 1.1 christos static void 243 1.1 christos bfd_preserve_finish (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_preserve *preserve) 244 1.1 christos { 245 1.9 christos if (preserve->cleanup) 246 1.9 christos { 247 1.9 christos /* Run the cleanup, assuming that all it will need is the 248 1.9 christos tdata at the time the cleanup was returned. */ 249 1.9 christos void *tdata = abfd->tdata.any; 250 1.9 christos abfd->tdata.any = preserve->tdata; 251 1.9 christos preserve->cleanup (abfd); 252 1.9 christos abfd->tdata.any = tdata; 253 1.9 christos } 254 1.1 christos /* It would be nice to be able to free more memory here, eg. old 255 1.1 christos tdata, but that's not possible since these blocks are sitting 256 1.1 christos inside bfd_alloc'd memory. The section hash is on a separate 257 1.1 christos objalloc. */ 258 1.1 christos bfd_hash_table_free (&preserve->section_htab); 259 1.1 christos preserve->marker = NULL; 260 1.1 christos } 261 1.1 christos 262 1.10 christos static void 263 1.10 christos print_warnmsg (struct per_xvec_message **list) 264 1.10 christos { 265 1.10 christos for (struct per_xvec_message *warn = *list; warn; warn = warn->next) 266 1.11 christos _bfd_error_handler ("%s", warn->message); 267 1.10 christos } 268 1.10 christos 269 1.10 christos static void 270 1.10 christos clear_warnmsg (struct per_xvec_message **list) 271 1.10 christos { 272 1.10 christos struct per_xvec_message *warn = *list; 273 1.10 christos while (warn) 274 1.10 christos { 275 1.10 christos struct per_xvec_message *next = warn->next; 276 1.10 christos free (warn); 277 1.10 christos warn = next; 278 1.10 christos } 279 1.10 christos *list = NULL; 280 1.10 christos } 281 1.10 christos 282 1.11 christos /* Free all the storage in LIST. Note that the first element of LIST 283 1.11 christos is special and is assumed to be stack-allocated. TARG is used for 284 1.11 christos re-issuing warning messages. If TARG is PER_XVEC_NO_TARGET, then 285 1.12 christos it acts like a sort of wildcard -- messages are reissued if all 286 1.12 christos targets with messages have identical messages. One copy of the 287 1.12 christos messages are then reissued. If TARG is anything else, then only 288 1.12 christos messages associated with TARG are emitted. */ 289 1.11 christos 290 1.10 christos static void 291 1.11 christos print_and_clear_messages (struct per_xvec_messages *list, 292 1.11 christos const bfd_target *targ) 293 1.10 christos { 294 1.12 christos struct per_xvec_messages *iter; 295 1.11 christos 296 1.12 christos if (targ == PER_XVEC_NO_TARGET) 297 1.12 christos { 298 1.12 christos iter = list->next; 299 1.12 christos while (iter != NULL) 300 1.12 christos { 301 1.12 christos struct per_xvec_message *msg1 = list->messages; 302 1.12 christos struct per_xvec_message *msg2 = iter->messages; 303 1.12 christos do 304 1.12 christos { 305 1.12 christos if (strcmp (msg1->message, msg2->message)) 306 1.12 christos break; 307 1.12 christos msg1 = msg1->next; 308 1.12 christos msg2 = msg2->next; 309 1.12 christos } while (msg1 && msg2); 310 1.12 christos if (msg1 || msg2) 311 1.12 christos break; 312 1.12 christos iter = iter->next; 313 1.12 christos } 314 1.12 christos if (iter == NULL) 315 1.12 christos targ = list->targ; 316 1.12 christos } 317 1.11 christos 318 1.12 christos iter = list; 319 1.11 christos while (iter != NULL) 320 1.11 christos { 321 1.11 christos struct per_xvec_messages *next = iter->next; 322 1.11 christos 323 1.11 christos if (iter->targ == targ) 324 1.11 christos print_warnmsg (&iter->messages); 325 1.11 christos clear_warnmsg (&iter->messages); 326 1.11 christos if (iter != list) 327 1.11 christos free (iter); 328 1.11 christos iter = next; 329 1.11 christos } 330 1.11 christos } 331 1.11 christos 332 1.11 christos /* This a copy of lto_section defined in GCC (lto-streamer.h). */ 333 1.11 christos 334 1.11 christos struct lto_section 335 1.11 christos { 336 1.11 christos int16_t major_version; 337 1.11 christos int16_t minor_version; 338 1.11 christos unsigned char slim_object; 339 1.11 christos 340 1.11 christos /* Flags is a private field that is not defined publicly. */ 341 1.11 christos uint16_t flags; 342 1.11 christos }; 343 1.11 christos 344 1.11 christos /* Set lto_type in ABFD. */ 345 1.11 christos 346 1.11 christos static void 347 1.11 christos bfd_set_lto_type (bfd *abfd ATTRIBUTE_UNUSED) 348 1.11 christos { 349 1.11 christos #if BFD_SUPPORTS_PLUGINS 350 1.11 christos if (abfd->format == bfd_object 351 1.11 christos && abfd->lto_type == lto_non_object 352 1.11 christos && (abfd->flags & (DYNAMIC | EXEC_P)) == 0) 353 1.11 christos { 354 1.11 christos asection *sec; 355 1.11 christos enum bfd_lto_object_type type = lto_non_ir_object; 356 1.11 christos struct lto_section lsection; 357 1.11 christos /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information 358 1.11 christos section. */ 359 1.11 christos for (sec = abfd->sections; sec != NULL; sec = sec->next) 360 1.11 christos if (startswith (sec->name, ".gnu.lto_.lto.") 361 1.11 christos && bfd_get_section_contents (abfd, sec, &lsection, 0, 362 1.11 christos sizeof (struct lto_section))) 363 1.11 christos { 364 1.11 christos if (lsection.slim_object) 365 1.11 christos type = lto_slim_ir_object; 366 1.11 christos else 367 1.11 christos type = lto_fat_ir_object; 368 1.11 christos break; 369 1.11 christos } 370 1.11 christos 371 1.11 christos abfd->lto_type = type; 372 1.11 christos } 373 1.11 christos #endif 374 1.10 christos } 375 1.10 christos 376 1.1 christos /* 377 1.1 christos FUNCTION 378 1.1 christos bfd_check_format_matches 379 1.1 christos 380 1.1 christos SYNOPSIS 381 1.10 christos bool bfd_check_format_matches 382 1.1 christos (bfd *abfd, bfd_format format, char ***matching); 383 1.1 christos 384 1.1 christos DESCRIPTION 385 1.1 christos Like <<bfd_check_format>>, except when it returns FALSE with 386 1.1 christos <<bfd_errno>> set to <<bfd_error_file_ambiguously_recognized>>. In that 387 1.1 christos case, if @var{matching} is not NULL, it will be filled in with 388 1.1 christos a NULL-terminated list of the names of the formats that matched, 389 1.1 christos allocated with <<malloc>>. 390 1.1 christos Then the user may choose a format and try again. 391 1.1 christos 392 1.1 christos When done with the list that @var{matching} points to, the caller 393 1.1 christos should free it. 394 1.1 christos */ 395 1.1 christos 396 1.10 christos bool 397 1.1 christos bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching) 398 1.1 christos { 399 1.1 christos extern const bfd_target binary_vec; 400 1.6 christos #if BFD_SUPPORTS_PLUGINS 401 1.6 christos extern const bfd_target plugin_vec; 402 1.6 christos #endif 403 1.1 christos const bfd_target * const *target; 404 1.1 christos const bfd_target **matching_vector = NULL; 405 1.1 christos const bfd_target *save_targ, *right_targ, *ar_right_targ, *match_targ; 406 1.1 christos int match_count, best_count, best_match; 407 1.1 christos int ar_match_index; 408 1.8 christos unsigned int initial_section_id = _bfd_section_id; 409 1.9 christos struct bfd_preserve preserve, preserve_match; 410 1.9 christos bfd_cleanup cleanup = NULL; 411 1.11 christos struct per_xvec_messages messages = { abfd, PER_XVEC_NO_TARGET, NULL, NULL }; 412 1.11 christos struct per_xvec_messages *orig_messages; 413 1.11 christos bool old_in_format_matches; 414 1.1 christos 415 1.1 christos if (matching != NULL) 416 1.1 christos *matching = NULL; 417 1.1 christos 418 1.1 christos if (!bfd_read_p (abfd) 419 1.1 christos || (unsigned int) abfd->format >= (unsigned int) bfd_type_end) 420 1.1 christos { 421 1.1 christos bfd_set_error (bfd_error_invalid_operation); 422 1.10 christos return false; 423 1.1 christos } 424 1.1 christos 425 1.1 christos if (abfd->format != bfd_unknown) 426 1.11 christos { 427 1.11 christos bfd_set_lto_type (abfd); 428 1.11 christos return abfd->format == format; 429 1.11 christos } 430 1.1 christos 431 1.1 christos if (matching != NULL || *bfd_associated_vector != NULL) 432 1.1 christos { 433 1.9 christos size_t amt; 434 1.1 christos 435 1.1 christos amt = sizeof (*matching_vector) * 2 * _bfd_target_vector_entries; 436 1.1 christos matching_vector = (const bfd_target **) bfd_malloc (amt); 437 1.1 christos if (!matching_vector) 438 1.10 christos return false; 439 1.1 christos } 440 1.1 christos 441 1.11 christos /* Avoid clashes with bfd_cache_close_all running in another 442 1.11 christos thread. */ 443 1.11 christos if (!bfd_cache_set_uncloseable (abfd, true, &old_in_format_matches)) 444 1.11 christos return false; 445 1.11 christos 446 1.1 christos /* Presume the answer is yes. */ 447 1.1 christos abfd->format = format; 448 1.1 christos save_targ = abfd->xvec; 449 1.9 christos 450 1.10 christos /* Don't report errors on recursive calls checking the first element 451 1.10 christos of an archive. */ 452 1.11 christos orig_messages = _bfd_set_error_handler_caching (&messages); 453 1.10 christos 454 1.12 christos /* Locking is required here in order to manage _bfd_section_id. */ 455 1.12 christos if (!bfd_lock ()) 456 1.12 christos return false; 457 1.12 christos 458 1.9 christos preserve_match.marker = NULL; 459 1.9 christos if (!bfd_preserve_save (abfd, &preserve, NULL)) 460 1.9 christos goto err_ret; 461 1.1 christos 462 1.1 christos /* If the target type was explicitly specified, just check that target. */ 463 1.1 christos if (!abfd->target_defaulted) 464 1.1 christos { 465 1.11 christos if (bfd_seek (abfd, 0, SEEK_SET) != 0) /* rewind! */ 466 1.1 christos goto err_ret; 467 1.1 christos 468 1.9 christos cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd)); 469 1.1 christos 470 1.9 christos if (cleanup) 471 1.1 christos goto ok_ret; 472 1.1 christos 473 1.1 christos /* For a long time the code has dropped through to check all 474 1.1 christos targets if the specified target was wrong. I don't know why, 475 1.1 christos and I'm reluctant to change it. However, in the case of an 476 1.1 christos archive, it can cause problems. If the specified target does 477 1.1 christos not permit archives (e.g., the binary target), then we should 478 1.1 christos not allow some other target to recognize it as an archive, but 479 1.1 christos should instead allow the specified target to recognize it as an 480 1.1 christos object. When I first made this change, it broke the PE target, 481 1.1 christos because the specified pei-i386 target did not recognize the 482 1.1 christos actual pe-i386 archive. Since there may be other problems of 483 1.1 christos this sort, I changed this test to check only for the binary 484 1.1 christos target. */ 485 1.1 christos if (format == bfd_archive && save_targ == &binary_vec) 486 1.1 christos goto err_unrecog; 487 1.1 christos } 488 1.1 christos 489 1.1 christos /* Since the target type was defaulted, check them all in the hope 490 1.1 christos that one will be uniquely recognized. */ 491 1.1 christos right_targ = NULL; 492 1.1 christos ar_right_targ = NULL; 493 1.1 christos match_targ = NULL; 494 1.1 christos best_match = 256; 495 1.1 christos best_count = 0; 496 1.1 christos match_count = 0; 497 1.1 christos ar_match_index = _bfd_target_vector_entries; 498 1.1 christos 499 1.1 christos for (target = bfd_target_vector; *target != NULL; target++) 500 1.1 christos { 501 1.9 christos void **high_water; 502 1.1 christos 503 1.9 christos /* The binary target matches anything, so don't return it when 504 1.9 christos searching. Don't match the plugin target if we have another 505 1.9 christos alternative since we want to properly set the input format 506 1.9 christos before allowing a plugin to claim the file. Also, don't 507 1.9 christos check the default target twice. */ 508 1.1 christos if (*target == &binary_vec 509 1.9 christos #if BFD_SUPPORTS_PLUGINS 510 1.9 christos || (match_count != 0 && *target == &plugin_vec) 511 1.9 christos #endif 512 1.8 christos || (!abfd->target_defaulted && *target == save_targ)) 513 1.1 christos continue; 514 1.1 christos 515 1.1 christos /* If we already tried a match, the bfd is modified and may 516 1.1 christos have sections attached, which will confuse the next 517 1.1 christos _bfd_check_format call. */ 518 1.11 christos bfd_reinit (abfd, initial_section_id, &preserve, cleanup); 519 1.9 christos /* Free bfd_alloc memory too. If we have matched and preserved 520 1.9 christos a target then the high water mark is that much higher. */ 521 1.9 christos if (preserve_match.marker) 522 1.9 christos high_water = &preserve_match.marker; 523 1.9 christos else 524 1.9 christos high_water = &preserve.marker; 525 1.9 christos bfd_release (abfd, *high_water); 526 1.9 christos *high_water = bfd_alloc (abfd, 1); 527 1.1 christos 528 1.1 christos /* Change BFD's target temporarily. */ 529 1.1 christos abfd->xvec = *target; 530 1.1 christos 531 1.11 christos if (bfd_seek (abfd, 0, SEEK_SET) != 0) 532 1.1 christos goto err_ret; 533 1.1 christos 534 1.9 christos cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd)); 535 1.9 christos if (cleanup) 536 1.1 christos { 537 1.9 christos int match_priority = abfd->xvec->match_priority; 538 1.6 christos #if BFD_SUPPORTS_PLUGINS 539 1.6 christos /* If this object can be handled by a plugin, give that the 540 1.6 christos lowest priority; objects both handled by a plugin and 541 1.6 christos with an underlying object format will be claimed 542 1.6 christos separately by the plugin. */ 543 1.6 christos if (*target == &plugin_vec) 544 1.6 christos match_priority = (*target)->match_priority; 545 1.6 christos #endif 546 1.6 christos 547 1.1 christos if (abfd->format != bfd_archive 548 1.1 christos || (bfd_has_map (abfd) 549 1.1 christos && bfd_get_error () != bfd_error_wrong_object_format)) 550 1.1 christos { 551 1.1 christos /* If this is the default target, accept it, even if 552 1.1 christos other targets might match. People who want those 553 1.1 christos other targets have to set the GNUTARGET variable. */ 554 1.9 christos if (abfd->xvec == bfd_default_vector[0]) 555 1.1 christos goto ok_ret; 556 1.1 christos 557 1.1 christos if (matching_vector) 558 1.9 christos matching_vector[match_count] = abfd->xvec; 559 1.1 christos match_count++; 560 1.1 christos 561 1.6 christos if (match_priority < best_match) 562 1.1 christos { 563 1.6 christos best_match = match_priority; 564 1.1 christos best_count = 0; 565 1.1 christos } 566 1.8 christos if (match_priority <= best_match) 567 1.8 christos { 568 1.8 christos /* This format checks out as ok! */ 569 1.9 christos right_targ = abfd->xvec; 570 1.8 christos best_count++; 571 1.8 christos } 572 1.1 christos } 573 1.1 christos else 574 1.1 christos { 575 1.1 christos /* An archive with no armap or objects of the wrong 576 1.1 christos type. We want this target to match if we get no 577 1.1 christos better matches. */ 578 1.1 christos if (ar_right_targ != bfd_default_vector[0]) 579 1.1 christos ar_right_targ = *target; 580 1.1 christos if (matching_vector) 581 1.1 christos matching_vector[ar_match_index] = *target; 582 1.1 christos ar_match_index++; 583 1.1 christos } 584 1.1 christos 585 1.9 christos if (preserve_match.marker == NULL) 586 1.9 christos { 587 1.9 christos match_targ = abfd->xvec; 588 1.9 christos if (!bfd_preserve_save (abfd, &preserve_match, cleanup)) 589 1.9 christos goto err_ret; 590 1.9 christos cleanup = NULL; 591 1.9 christos } 592 1.1 christos } 593 1.1 christos } 594 1.1 christos 595 1.1 christos if (best_count == 1) 596 1.1 christos match_count = 1; 597 1.1 christos 598 1.1 christos if (match_count == 0) 599 1.1 christos { 600 1.1 christos /* Try partial matches. */ 601 1.1 christos right_targ = ar_right_targ; 602 1.1 christos 603 1.1 christos if (right_targ == bfd_default_vector[0]) 604 1.1 christos { 605 1.1 christos match_count = 1; 606 1.1 christos } 607 1.1 christos else 608 1.1 christos { 609 1.1 christos match_count = ar_match_index - _bfd_target_vector_entries; 610 1.1 christos 611 1.1 christos if (matching_vector && match_count > 1) 612 1.1 christos memcpy (matching_vector, 613 1.1 christos matching_vector + _bfd_target_vector_entries, 614 1.1 christos sizeof (*matching_vector) * match_count); 615 1.1 christos } 616 1.1 christos } 617 1.1 christos 618 1.1 christos /* We have more than one equally good match. If any of the best 619 1.1 christos matches is a target in config.bfd targ_defvec or targ_selvecs, 620 1.1 christos choose it. */ 621 1.1 christos if (match_count > 1) 622 1.1 christos { 623 1.1 christos const bfd_target * const *assoc = bfd_associated_vector; 624 1.1 christos 625 1.1 christos while ((right_targ = *assoc++) != NULL) 626 1.1 christos { 627 1.1 christos int i = match_count; 628 1.1 christos 629 1.1 christos while (--i >= 0) 630 1.1 christos if (matching_vector[i] == right_targ 631 1.1 christos && right_targ->match_priority <= best_match) 632 1.1 christos break; 633 1.1 christos 634 1.1 christos if (i >= 0) 635 1.1 christos { 636 1.1 christos match_count = 1; 637 1.1 christos break; 638 1.1 christos } 639 1.1 christos } 640 1.1 christos } 641 1.1 christos 642 1.1 christos /* We still have more than one equally good match, and at least some 643 1.1 christos of the targets support match priority. Choose the first of the 644 1.1 christos best matches. */ 645 1.3 christos if (matching_vector && match_count > 1 && best_count != match_count) 646 1.1 christos { 647 1.1 christos int i; 648 1.1 christos 649 1.1 christos for (i = 0; i < match_count; i++) 650 1.1 christos { 651 1.1 christos right_targ = matching_vector[i]; 652 1.1 christos if (right_targ->match_priority <= best_match) 653 1.1 christos break; 654 1.1 christos } 655 1.1 christos match_count = 1; 656 1.1 christos } 657 1.1 christos 658 1.1 christos /* There is way too much undoing of half-known state here. We 659 1.1 christos really shouldn't iterate on live bfd's. Note that saving the 660 1.1 christos whole bfd and restoring it would be even worse; the first thing 661 1.1 christos you notice is that the cached bfd file position gets out of sync. */ 662 1.9 christos if (preserve_match.marker != NULL) 663 1.9 christos cleanup = bfd_preserve_restore (abfd, &preserve_match); 664 1.1 christos 665 1.1 christos if (match_count == 1) 666 1.1 christos { 667 1.1 christos abfd->xvec = right_targ; 668 1.1 christos /* If we come out of the loop knowing that the last target that 669 1.1 christos matched is the one we want, then ABFD should still be in a usable 670 1.9 christos state (except possibly for XVEC). This is not just an 671 1.9 christos optimisation. In the case of plugins a match against the 672 1.9 christos plugin target can result in the bfd being changed such that 673 1.9 christos it no longer matches the plugin target, nor will it match 674 1.9 christos RIGHT_TARG again. */ 675 1.1 christos if (match_targ != right_targ) 676 1.1 christos { 677 1.11 christos bfd_reinit (abfd, initial_section_id, &preserve, cleanup); 678 1.9 christos bfd_release (abfd, preserve.marker); 679 1.11 christos if (bfd_seek (abfd, 0, SEEK_SET) != 0) 680 1.1 christos goto err_ret; 681 1.9 christos cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd)); 682 1.9 christos BFD_ASSERT (cleanup != NULL); 683 1.1 christos } 684 1.1 christos 685 1.1 christos ok_ret: 686 1.1 christos /* If the file was opened for update, then `output_has_begun' 687 1.1 christos some time ago when the file was created. Do not recompute 688 1.1 christos sections sizes or alignments in _bfd_set_section_contents. 689 1.1 christos We can not set this flag until after checking the format, 690 1.1 christos because it will interfere with creation of BFD sections. */ 691 1.1 christos if (abfd->direction == both_direction) 692 1.10 christos abfd->output_has_begun = true; 693 1.1 christos 694 1.9 christos free (matching_vector); 695 1.9 christos if (preserve_match.marker != NULL) 696 1.9 christos bfd_preserve_finish (abfd, &preserve_match); 697 1.9 christos bfd_preserve_finish (abfd, &preserve); 698 1.11 christos _bfd_restore_error_handler_caching (orig_messages); 699 1.10 christos 700 1.11 christos print_and_clear_messages (&messages, abfd->xvec); 701 1.11 christos 702 1.11 christos bfd_set_lto_type (abfd); 703 1.1 christos 704 1.1 christos /* File position has moved, BTW. */ 705 1.12 christos bool ret = bfd_cache_set_uncloseable (abfd, old_in_format_matches, NULL); 706 1.12 christos if (!bfd_unlock ()) 707 1.12 christos return false; 708 1.12 christos return ret; 709 1.1 christos } 710 1.1 christos 711 1.1 christos if (match_count == 0) 712 1.1 christos { 713 1.1 christos err_unrecog: 714 1.1 christos bfd_set_error (bfd_error_file_not_recognized); 715 1.1 christos err_ret: 716 1.9 christos if (cleanup) 717 1.9 christos cleanup (abfd); 718 1.1 christos abfd->xvec = save_targ; 719 1.1 christos abfd->format = bfd_unknown; 720 1.9 christos free (matching_vector); 721 1.10 christos goto out; 722 1.1 christos } 723 1.1 christos 724 1.1 christos /* Restore original target type and format. */ 725 1.1 christos abfd->xvec = save_targ; 726 1.1 christos abfd->format = bfd_unknown; 727 1.1 christos bfd_set_error (bfd_error_file_ambiguously_recognized); 728 1.1 christos 729 1.1 christos if (matching) 730 1.1 christos { 731 1.1 christos *matching = (char **) matching_vector; 732 1.1 christos matching_vector[match_count] = NULL; 733 1.1 christos /* Return target names. This is a little nasty. Maybe we 734 1.1 christos should do another bfd_malloc? */ 735 1.1 christos while (--match_count >= 0) 736 1.1 christos { 737 1.1 christos const char *name = matching_vector[match_count]->name; 738 1.1 christos *(const char **) &matching_vector[match_count] = name; 739 1.1 christos } 740 1.1 christos } 741 1.9 christos else 742 1.9 christos free (matching_vector); 743 1.9 christos if (cleanup) 744 1.9 christos cleanup (abfd); 745 1.10 christos out: 746 1.9 christos if (preserve_match.marker != NULL) 747 1.9 christos bfd_preserve_finish (abfd, &preserve_match); 748 1.9 christos bfd_preserve_restore (abfd, &preserve); 749 1.11 christos _bfd_restore_error_handler_caching (orig_messages); 750 1.11 christos print_and_clear_messages (&messages, PER_XVEC_NO_TARGET); 751 1.11 christos bfd_cache_set_uncloseable (abfd, old_in_format_matches, NULL); 752 1.12 christos bfd_unlock (); 753 1.10 christos return false; 754 1.1 christos } 755 1.1 christos 756 1.1 christos /* 757 1.1 christos FUNCTION 758 1.1 christos bfd_set_format 759 1.1 christos 760 1.1 christos SYNOPSIS 761 1.10 christos bool bfd_set_format (bfd *abfd, bfd_format format); 762 1.1 christos 763 1.1 christos DESCRIPTION 764 1.1 christos This function sets the file format of the BFD @var{abfd} to the 765 1.1 christos format @var{format}. If the target set in the BFD does not 766 1.1 christos support the format requested, the format is invalid, or the BFD 767 1.1 christos is not open for writing, then an error occurs. 768 1.1 christos */ 769 1.1 christos 770 1.10 christos bool 771 1.1 christos bfd_set_format (bfd *abfd, bfd_format format) 772 1.1 christos { 773 1.1 christos if (bfd_read_p (abfd) 774 1.1 christos || (unsigned int) abfd->format >= (unsigned int) bfd_type_end) 775 1.1 christos { 776 1.1 christos bfd_set_error (bfd_error_invalid_operation); 777 1.10 christos return false; 778 1.1 christos } 779 1.1 christos 780 1.1 christos if (abfd->format != bfd_unknown) 781 1.1 christos return abfd->format == format; 782 1.1 christos 783 1.1 christos /* Presume the answer is yes. */ 784 1.1 christos abfd->format = format; 785 1.1 christos 786 1.1 christos if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd))) 787 1.1 christos { 788 1.1 christos abfd->format = bfd_unknown; 789 1.10 christos return false; 790 1.1 christos } 791 1.1 christos 792 1.10 christos return true; 793 1.1 christos } 794 1.1 christos 795 1.1 christos /* 796 1.1 christos FUNCTION 797 1.1 christos bfd_format_string 798 1.1 christos 799 1.1 christos SYNOPSIS 800 1.1 christos const char *bfd_format_string (bfd_format format); 801 1.1 christos 802 1.1 christos DESCRIPTION 803 1.1 christos Return a pointer to a const string 804 1.1 christos <<invalid>>, <<object>>, <<archive>>, <<core>>, or <<unknown>>, 805 1.1 christos depending upon the value of @var{format}. 806 1.1 christos */ 807 1.1 christos 808 1.1 christos const char * 809 1.1 christos bfd_format_string (bfd_format format) 810 1.1 christos { 811 1.1 christos if (((int) format < (int) bfd_unknown) 812 1.1 christos || ((int) format >= (int) bfd_type_end)) 813 1.1 christos return "invalid"; 814 1.1 christos 815 1.1 christos switch (format) 816 1.1 christos { 817 1.1 christos case bfd_object: 818 1.1 christos return "object"; /* Linker/assembler/compiler output. */ 819 1.1 christos case bfd_archive: 820 1.1 christos return "archive"; /* Object archive file. */ 821 1.1 christos case bfd_core: 822 1.1 christos return "core"; /* Core dump. */ 823 1.1 christos default: 824 1.1 christos return "unknown"; 825 1.1 christos } 826 1.1 christos } 827