- if (!abfd->target_defaulted) {
- if (bfd_seek (abfd, (file_ptr)0, SEEK_SET) != 0) /* rewind! */
- return false;
- right_targ = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
- if (right_targ) {
- abfd->xvec = right_targ; /* Set the target as returned */
- if (matching)
- free (matching_vector);
- return true; /* File position has moved, BTW */
+ for (target = bfd_target_vector; *target != NULL; target++)
+ {
+ const bfd_target *temp;
+ bfd_error_type err;
+
+ /* Don't check the default target twice. */
+ if (*target == &binary_vec
+ || (!abfd->target_defaulted && *target == save_targ)
+ || (*target)->match_priority > best_match)
+ continue;
+
+ abfd->xvec = *target; /* Change BFD's target temporarily. */
+
+ if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
+ goto err_ret;
+
+ /* If _bfd_check_format neglects to set bfd_error, assume
+ bfd_error_wrong_format. We didn't used to even pay any
+ attention to bfd_error, so I suspect that some
+ _bfd_check_format might have this problem. */
+ bfd_set_error (bfd_error_wrong_format);
+
+ temp = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
+ if (temp)
+ match_targ = temp;
+
+ if (temp && (abfd->format != bfd_archive || bfd_has_map (abfd)))
+ {
+ /* This format checks out as ok! */
+ right_targ = temp;
+
+ /* If this is the default target, accept it, even if other
+ targets might match. People who want those other targets
+ have to set the GNUTARGET variable. */
+ if (temp == bfd_default_vector[0])
+ goto ok_ret;
+
+ if (matching_vector)
+ matching_vector[match_count] = temp;
+ match_count++;
+
+ if (temp->match_priority < best_match)
+ {
+ best_match = temp->match_priority;
+ best_count = 0;
+ }
+ best_count++;
+ }
+ else if (temp
+ || (err = bfd_get_error ()) == bfd_error_wrong_object_format
+ || err == bfd_error_file_ambiguously_recognized)
+ {
+ /* An archive with no armap or objects of the wrong type,
+ or an ambiguous match. We want this target to match
+ if we get no better matches. */
+ if (ar_right_targ != bfd_default_vector[0])
+ ar_right_targ = *target;
+ if (matching_vector)
+ matching_vector[ar_match_index] = *target;
+ ar_match_index++;
+ }
+ else if (err != bfd_error_wrong_format)
+ goto err_ret;