public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* bfd_cleanup for object_p
@ 2020-03-02  8:59 Alan Modra
  2020-03-02 12:38 ` H.J. Lu
  2020-03-03 11:14 ` bfd_check_format_matches preserving matches vs. cleanups Alan Modra
  0 siblings, 2 replies; 6+ messages in thread
From: Alan Modra @ 2020-03-02  8:59 UTC (permalink / raw)
  To: binutils

The object_p (and archive_p, core_file_p) functions are not supposed
to have any target specific malloc'd memory attached to the bfd on
their return.  This should be obvious on a failure return, but it's
also true for a successful return.  The reason is that even though the
object_p recognises the file, that particular target may not be used
and thus the bfd won't be closed calling close_and_cleanup for the
target that allocated the memory.

It turns out that the object_p bfd_target* return value isn't needed.
In all cases except ld/plugin.c the target is abfd->xvec and with
ld/plugin.c the target isn't used.  So this patch returns a cleanup
function from object_p instead, called in bfd_check_format_matches to
tidy the bfd before trying a different target match.  The only cleanup
that does anything at this stage is the alpha-vms one.

bfd/
	* targets.c (bfd_cleanup): New typedef.
	(struct bfd <_bfd_check_format>): Return a bfd_cleanup.
	* libbfd-in.h (_bfd_no_cleanup): Define.
	* format.c (bfd_reinit): Add cleanup parameter, call it.
	(bfd_check_format_matches): Set cleanup from _bfd_check_format
	call and pass to bfd_reinit.  Delete temp, use abfd->xvec instead.
	* aout-target.h (callback, object_p): Return bfd_cleanup.
	* aout-tic30.c (tic30_aout_callback, tic30_aout_object_p): Likewise.
	* archive.c (bfd_generic_archive_p): Likewise.
	* binary.c (binary_object_p): Likewise.
	* coff-alpha.c (alpha_ecoff_object_p): Likewise.
	* coff-ia64.c (ia64coff_object_p): Likewise.
	* coff-rs6000.c (_bfd_xcoff_archive_p, rs6000coff_core_p): Likewise.
	* coff-sh.c (coff_small_object_p): Likewise.
	* coff-stgo32.c (go32_check_format): Likewise.
	* coff64-rs6000.c (xcoff64_archive_p, rs6000coff_core_p),
	(xcoff64_core_p): Likewise.
	* coffgen.c (coff_real_object_p, coff_object_p): Likewise.
	* elf-bfd.h (bfd_elf32_object_p, bfd_elf32_core_file_p),
	(bfd_elf64_object_p, bfd_elf64_core_file_p): Likewise.
	* elfcode.h (elf_object_p): Likewise.
	* elfcore.h (elf_core_file_p): Likewise.
	* i386msdos.c (msdos_object_p): Likewise.
	* ihex.c (ihex_object_p): Likewise.
	* libaout.h (some_aout_object_p): Likewise.
	* libbfd-in.h (bfd_generic_archive_p, _bfd_dummy_target),
	(_bfd_vms_lib_alpha_archive_p, _bfd_vms_lib_ia64_archive_p): Likewise.
	* libbfd.c (_bfd_dummy_target): Likewise.
	* libcoff-in.h (coff_object_p): Likewise.
	* mach-o-aarch64.c (bfd_mach_o_arm64_object_p),
	(bfd_mach_o_arm64_core_p): Likewise.
	* mach-o-arm.c (bfd_mach_o_arm_object_p),
	(bfd_mach_o_arm_core_p): Likewise.
	* mach-o-i386.c (bfd_mach_o_i386_object_p),
	(bfd_mach_o_i386_core_p): Likewise.
	* mach-o-x86-64.c (bfd_mach_o_x86_64_object_p),
	(bfd_mach_o_x86_64_core_p): Likewise.
	* mach-o.c (bfd_mach_o_header_p, bfd_mach_o_gen_object_p),
	(bfd_mach_o_gen_core_p, bfd_mach_o_fat_archive_p): Likewise.
	* mach-o.h (bfd_mach_o_object_p, bfd_mach_o_core_p),
	(bfd_mach_o_fat_archive_p, bfd_mach_o_header_p): Likewise.
	* mmo.c (mmo_object_p): Likewise.
	* pef.c (bfd_pef_object_p, bfd_pef_xlib_object_p): Likewise.
	* peicode.h (coff_real_object_p, pe_ILF_object_p),
	(pe_bfd_object_p): Likewise.
	* plugin.c (ld_plugin_object_p, bfd_plugin_object_p): Likewise.
	* ppcboot.c (ppcboot_object_p): Likewise.
	* rs6000-core.c (rs6000coff_core_p): Likewise.
	* som.c (som_object_setup, som_object_p): Likewise.
	* srec.c (srec_object_p, symbolsrec_object_p): Likewise.
	* tekhex.c (tekhex_object_p): Likewise.
	* vms-alpha.c (alpha_vms_object_p): Likewise.
	* vms-lib.c (_bfd_vms_lib_archive_p, _bfd_vms_lib_alpha_archive_p),
	(_bfd_vms_lib_ia64_archive_p, _bfd_vms_lib_txt_archive_p): Likewise.
	* wasm-module.c (wasm_object_p): Likewise.
	* xsym.c (bfd_sym_object_p): Likewise.
	* xsym.h (bfd_sym_object_p): Likewise.
	* aoutx.h (some_aout_object_p): Likewise, and callback parameter
	return type.
	* pdp11.c (some_aout_object_p): Likewise.
	* plugin.c (register_ld_plugin_object_p): Update object_p
	parameter type.
	* plugin.h (register_ld_plugin_object_p): Likewise.
	* bfd-in2.h: Regenerate.
	* libbfd.h: Regenerate.
	* libcoff.h: Regenerate.
ld/
	* plugin.c (plugin_object_p): Return a bfd_cleanup.
	(plugin_cleanup): New function.

diff --git a/bfd/aout-target.h b/bfd/aout-target.h
index 00024e6be2..365202e0ce 100644
--- a/bfd/aout-target.h
+++ b/bfd/aout-target.h
@@ -34,7 +34,7 @@ extern reloc_howto_type * NAME (aout, reloc_name_lookup) (bfd *, const char *);
    This routine is called from some_aout_object_p just before it returns.  */
 #ifndef MY_callback
 
-static const bfd_target *
+static bfd_cleanup
 MY (callback) (bfd *abfd)
 {
   struct internal_exec *execp = exec_hdr (abfd);
@@ -121,19 +121,19 @@ MY (callback) (bfd *abfd)
   /* Don't set sizes now -- can't be sure until we know arch & mach.
      Sizes get set in set_sizes callback, later.  */
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 }
 #endif
 
 #ifndef MY_object_p
 /* Finish up the reading of an a.out file header.  */
 
-static const bfd_target *
+static bfd_cleanup
 MY (object_p) (bfd *abfd)
 {
   struct external_exec exec_bytes;	/* Raw exec header from file.  */
   struct internal_exec exec;		/* Cleaned-up exec header.  */
-  const bfd_target *target;
+  bfd_cleanup cleanup;
   size_t amt = EXEC_BYTES_SIZE;
 
   if (bfd_bread ((void *) &exec_bytes, amt, abfd) != amt)
@@ -164,7 +164,7 @@ MY (object_p) (bfd *abfd)
   exec.a_info = SWAP_MAGIC (exec_bytes.e_info);
 #endif
 
-  target = NAME (aout, some_aout_object_p) (abfd, &exec, MY (callback));
+  cleanup = NAME (aout, some_aout_object_p) (abfd, &exec, MY (callback));
 
 #ifdef ENTRY_CAN_BE_ZERO
   /* The NEWSOS3 entry-point is/was 0, which (amongst other lossage)
@@ -185,7 +185,7 @@ MY (object_p) (bfd *abfd)
     }
 #endif /* ENTRY_CAN_BE_ZERO */
 
-  return target;
+  return cleanup;
 }
 #define MY_object_p MY (object_p)
 #endif
diff --git a/bfd/aout-tic30.c b/bfd/aout-tic30.c
index 06f401cc7e..222d1f0e30 100644
--- a/bfd/aout-tic30.c
+++ b/bfd/aout-tic30.c
@@ -331,7 +331,7 @@ tic30_aout_reloc_howto (bfd *abfd,
 /* Set parameters about this a.out file that are machine-dependent.
    This routine is called from some_aout_object_p just before it returns.  */
 
-static const bfd_target *
+static bfd_cleanup
 tic30_aout_callback (bfd *abfd)
 {
   struct internal_exec *execp = exec_hdr (abfd);
@@ -388,7 +388,7 @@ tic30_aout_callback (bfd *abfd)
       obj_datasec (abfd)->alignment_power = arch_align_power;
       obj_bsssec (abfd)->alignment_power = arch_align_power;
     }
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 }
 
 static bfd_reloc_status_type
@@ -547,12 +547,12 @@ tic30_aout_final_link_relocate (reloc_howto_type *howto,
 
 /* Finish up the reading of an a.out file header.  */
 
-static const bfd_target *
+static bfd_cleanup
 tic30_aout_object_p (bfd *abfd)
 {
   struct external_exec exec_bytes;	/* Raw exec header from file.  */
   struct internal_exec exec;		/* Cleaned-up exec header.  */
-  const bfd_target *target;
+  bfd_cleanup cleanup;
   size_t amt = EXEC_BYTES_SIZE;
 
   if (bfd_bread (& exec_bytes, amt, abfd) != amt)
@@ -582,7 +582,7 @@ tic30_aout_object_p (bfd *abfd)
   exec.a_info = SWAP_MAGIC (exec_bytes.e_info);
 #endif
 
-  target = NAME (aout, some_aout_object_p) (abfd, &exec, tic30_aout_callback);
+  cleanup = NAME (aout, some_aout_object_p) (abfd, &exec, tic30_aout_callback);
 
 #ifdef ENTRY_CAN_BE_ZERO
   /* The NEWSOS3 entry-point is/was 0, which (amongst other lossage)
@@ -603,7 +603,7 @@ tic30_aout_object_p (bfd *abfd)
     }
 #endif
 
-  return target;
+  return cleanup;
 }
 
 /* Copy private section data.  This actually does nothing with the
diff --git a/bfd/aoutx.h b/bfd/aoutx.h
index 4fb1282fb6..41ced3dc72 100644
--- a/bfd/aoutx.h
+++ b/bfd/aoutx.h
@@ -452,13 +452,13 @@ DESCRIPTION
 	handle any last-minute setup.
 */
 
-const bfd_target *
+bfd_cleanup
 NAME (aout, some_aout_object_p) (bfd *abfd,
 				 struct internal_exec *execp,
-				 const bfd_target *(*callback_to_real_object_p) (bfd *))
+				 bfd_cleanup (*callback_to_real_object_p) (bfd *))
 {
   struct aout_data_struct *rawptr, *oldrawptr;
-  const bfd_target *result;
+  bfd_cleanup result;
   size_t amt = sizeof (* rawptr);
 
   rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, amt);
@@ -591,7 +591,7 @@ NAME (aout, some_aout_object_p) (bfd *abfd,
   adata (abfd)->segment_size = SEGMENT_SIZE;
   adata (abfd)->exec_bytes_size = EXEC_BYTES_SIZE;
 
-  return abfd->xvec;
+  return _bfd_no_cleanup
 
   /* The architecture is encoded in various ways in various a.out variants,
      or is not encoded at all in some of them.  The relocation size depends
diff --git a/bfd/archive.c b/bfd/archive.c
index 71bc6a4323..0c009f10de 100644
--- a/bfd/archive.c
+++ b/bfd/archive.c
@@ -842,7 +842,7 @@ _bfd_noarchive_openr_next_archived_file (bfd *archive,
   return (bfd *) _bfd_ptr_bfd_null_error (archive);
 }
 
-const bfd_target *
+bfd_cleanup
 bfd_generic_archive_p (bfd *abfd)
 {
   struct artdata *tdata_hold;
@@ -924,7 +924,7 @@ bfd_generic_archive_p (bfd *abfd)
 	}
     }
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 }
 
 /* Some constants for a 32 bit BSD archive structure.  We do not
diff --git a/bfd/binary.c b/bfd/binary.c
index 0683e414d6..999de0d8c4 100644
--- a/bfd/binary.c
+++ b/bfd/binary.c
@@ -53,7 +53,7 @@ binary_mkobject (bfd *abfd ATTRIBUTE_UNUSED)
    was not defaulted.  That is, it must be explicitly specified as
    being binary.  */
 
-static const bfd_target *
+static bfd_cleanup
 binary_object_p (bfd *abfd)
 {
   struct stat statbuf;
@@ -86,7 +86,7 @@ binary_object_p (bfd *abfd)
 
   abfd->tdata.any = (void *) sec;
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 }
 
 #define binary_close_and_cleanup     _bfd_generic_close_and_cleanup
diff --git a/bfd/coff-alpha.c b/bfd/coff-alpha.c
index 821e5591a9..b86a8a259b 100644
--- a/bfd/coff-alpha.c
+++ b/bfd/coff-alpha.c
@@ -398,10 +398,10 @@ static reloc_howto_type alpha_howto_table[] =
 \f
 /* Recognize an Alpha ECOFF file.  */
 
-static const bfd_target *
+static bfd_cleanup
 alpha_ecoff_object_p (bfd *abfd)
 {
-  static const bfd_target *ret;
+  bfd_cleanup ret;
 
   ret = coff_object_p (abfd);
 
diff --git a/bfd/coff-ia64.c b/bfd/coff-ia64.c
index 180cf9db8a..07e54037f9 100644
--- a/bfd/coff-ia64.c
+++ b/bfd/coff-ia64.c
@@ -67,7 +67,7 @@ in_reloc_p (bfd * abfd ATTRIBUTE_UNUSED,
 
 #include "coffcode.h"
 
-static const bfd_target *
+static bfd_cleanup
 ia64coff_object_p (bfd *abfd)
 {
 #ifdef COFF_IMAGE_WITH_PE
diff --git a/bfd/coff-rs6000.c b/bfd/coff-rs6000.c
index 1cc708ce1b..2dd68e08c3 100644
--- a/bfd/coff-rs6000.c
+++ b/bfd/coff-rs6000.c
@@ -38,7 +38,7 @@ extern bfd_boolean _bfd_xcoff_is_local_label_name (bfd *, const char *);
 extern reloc_howto_type *_bfd_xcoff_reloc_type_lookup
   (bfd *, bfd_reloc_code_real_type);
 extern bfd_boolean _bfd_xcoff_slurp_armap (bfd *);
-extern const bfd_target *_bfd_xcoff_archive_p (bfd *);
+extern bfd_cleanup _bfd_xcoff_archive_p (bfd *);
 extern void * _bfd_xcoff_read_ar_hdr (bfd *);
 extern bfd *_bfd_xcoff_openr_next_archived_file (bfd *, bfd *);
 extern int _bfd_xcoff_stat_arch_elt (bfd *, struct stat *);
@@ -77,7 +77,7 @@ void xcoff_rtype2howto (arelent *, struct internal_reloc *);
 #define coff_mkobject _bfd_xcoff_mkobject
 #define coff_bfd_is_local_label_name _bfd_xcoff_is_local_label_name
 #ifdef AIX_CORE
-extern const bfd_target * rs6000coff_core_p (bfd *abfd);
+extern bfd_cleanup rs6000coff_core_p (bfd *abfd);
 extern bfd_boolean rs6000coff_core_file_matches_executable_p
   (bfd *cbfd, bfd *ebfd);
 extern char *rs6000coff_core_file_failing_command (bfd *abfd);
@@ -1380,7 +1380,7 @@ _bfd_xcoff_slurp_armap (bfd *abfd)
 
 /* See if this is an XCOFF archive.  */
 
-const bfd_target *
+bfd_cleanup
 _bfd_xcoff_archive_p (bfd *abfd)
 {
   struct artdata *tdata_hold;
@@ -1481,7 +1481,7 @@ _bfd_xcoff_archive_p (bfd *abfd)
       return NULL;
     }
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 }
 
 /* Read the archive header in an XCOFF archive.  */
diff --git a/bfd/coff-sh.c b/bfd/coff-sh.c
index 986cd4d1d0..b46a5e34f3 100644
--- a/bfd/coff-sh.c
+++ b/bfd/coff-sh.c
@@ -3044,7 +3044,7 @@ CREATE_LITTLE_COFF_TARGET_VEC (TARGET_SYM, TARGET_SHL_NAME, BFD_IS_RELAXABLE,
 /* Only recognize the small versions if the target was not defaulted.
    Otherwise we won't recognize the non default endianness.  */
 
-static const bfd_target *
+static bfd_cleanup
 coff_small_object_p (bfd *abfd)
 {
   if (abfd->target_defaulted)
diff --git a/bfd/coff-stgo32.c b/bfd/coff-stgo32.c
index 6984b4219e..676022872a 100644
--- a/bfd/coff-stgo32.c
+++ b/bfd/coff-stgo32.c
@@ -93,7 +93,7 @@ create_go32_stub (bfd *);
 #define COFF_ADJUST_AUX_OUT_PRE adjust_aux_out_pre
 #define COFF_ADJUST_AUX_OUT_POST adjust_aux_out_post
 
-static const bfd_target *go32_check_format (bfd *);
+static bfd_cleanup go32_check_format (bfd *);
 
 #define COFF_CHECK_FORMAT go32_check_format
 
@@ -406,7 +406,7 @@ go32_stubbed_coff_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
 /* coff_object_p only checks 2 bytes F_MAGIC at GO32_STUBSIZE inside the file
    which is too fragile.  */
 
-static const bfd_target *
+static bfd_cleanup
 go32_check_format (bfd *abfd)
 {
   char mz[2];
diff --git a/bfd/coff64-rs6000.c b/bfd/coff64-rs6000.c
index bb546239fc..cca876eb4e 100644
--- a/bfd/coff64-rs6000.c
+++ b/bfd/coff64-rs6000.c
@@ -155,7 +155,7 @@ static bfd_boolean xcoff64_ppc_relocate_section
    asection **);
 static bfd_boolean xcoff64_slurp_armap
   (bfd *);
-static const bfd_target *xcoff64_archive_p
+static bfd_cleanup xcoff64_archive_p
   (bfd *);
 static bfd *xcoff64_openr_next_archived_file
   (bfd *, bfd *);
@@ -238,7 +238,7 @@ bfd_boolean (*xcoff64_calculate_relocation[XCOFF_MAX_CALCULATE_RELOCATION])
 #define coff_bfd_reloc_type_lookup xcoff64_reloc_type_lookup
 #define coff_bfd_reloc_name_lookup xcoff64_reloc_name_lookup
 #ifdef AIX_CORE
-extern const bfd_target * rs6000coff_core_p
+extern bfd_cleanup rs6000coff_core_p
   (bfd *abfd);
 extern bfd_boolean rs6000coff_core_file_matches_executable_p
   (bfd *cbfd, bfd *ebfd);
@@ -1991,7 +1991,7 @@ xcoff64_slurp_armap (bfd *abfd)
 
 /* See if this is an NEW XCOFF archive.  */
 
-static const bfd_target *
+static bfd_cleanup
 xcoff64_archive_p (bfd *abfd)
 {
   struct artdata *tdata_hold;
@@ -2058,7 +2058,7 @@ xcoff64_archive_p (bfd *abfd)
       return NULL;
     }
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 }
 
 
@@ -2805,7 +2805,7 @@ const bfd_target rs6000_xcoff64_vec =
     &bfd_xcoff_backend_data,
   };
 
-extern const bfd_target *xcoff64_core_p
+extern bfd_cleanup xcoff64_core_p
   (bfd *);
 extern bfd_boolean xcoff64_core_file_matches_executable_p
   (bfd *, bfd *);
diff --git a/bfd/coffgen.c b/bfd/coffgen.c
index daaaba95c5..6d84d51284 100644
--- a/bfd/coffgen.c
+++ b/bfd/coffgen.c
@@ -225,12 +225,12 @@ make_a_section_from_file (bfd *abfd,
 
 /* Read in a COFF object and make it into a BFD.  This is used by
    ECOFF as well.  */
-const bfd_target *
+bfd_cleanup
 coff_real_object_p (bfd *,
 		    unsigned,
 		    struct internal_filehdr *,
 		    struct internal_aouthdr *);
-const bfd_target *
+bfd_cleanup
 coff_real_object_p (bfd *abfd,
 		    unsigned nscns,
 		    struct internal_filehdr *internal_f,
@@ -300,7 +300,7 @@ coff_real_object_p (bfd *abfd,
     }
 
   _bfd_coff_free_symbols (abfd);
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 
  fail:
   _bfd_coff_free_symbols (abfd);
@@ -309,13 +309,13 @@ coff_real_object_p (bfd *abfd,
   abfd->tdata.any = tdata_save;
   abfd->flags = oflags;
   abfd->start_address = ostart;
-  return (const bfd_target *) NULL;
+  return NULL;
 }
 
 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
    not a COFF file.  This is also used by ECOFF.  */
 
-const bfd_target *
+bfd_cleanup
 coff_object_p (bfd *abfd)
 {
   bfd_size_type filhsz;
diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index 61e733f068..38a9aa0601 100644
--- a/bfd/elf-bfd.h
+++ b/bfd/elf-bfd.h
@@ -2400,9 +2400,9 @@ extern bfd_boolean _bfd_elf_init_file_header (bfd *, struct bfd_link_info *);
 
 extern bfd_boolean _bfd_elf_final_write_processing (bfd *);
 
-extern const bfd_target *bfd_elf32_object_p
+extern bfd_cleanup bfd_elf32_object_p
   (bfd *);
-extern const bfd_target *bfd_elf32_core_file_p
+extern bfd_cleanup bfd_elf32_core_file_p
   (bfd *);
 extern char *bfd_elf32_core_file_failing_command
   (bfd *);
@@ -2448,9 +2448,9 @@ extern void bfd_elf32_write_relocs
 extern bfd_boolean bfd_elf32_slurp_reloc_table
   (bfd *, asection *, asymbol **, bfd_boolean);
 
-extern const bfd_target *bfd_elf64_object_p
+extern bfd_cleanup bfd_elf64_object_p
   (bfd *);
-extern const bfd_target *bfd_elf64_core_file_p
+extern bfd_cleanup bfd_elf64_core_file_p
   (bfd *);
 extern char *bfd_elf64_core_file_failing_command
   (bfd *);
diff --git a/bfd/elfcode.h b/bfd/elfcode.h
index b67ea8c7d2..600abfe836 100644
--- a/bfd/elfcode.h
+++ b/bfd/elfcode.h
@@ -497,7 +497,7 @@ elf_file_p (Elf_External_Ehdr *x_ehdrp)
    any side effects in ABFD, or any data it points to (like tdata), if the
    file does not match the target vector.  */
 
-const bfd_target *
+bfd_cleanup
 elf_object_p (bfd *abfd)
 {
   Elf_External_Ehdr x_ehdr;	/* Elf file header, external form */
@@ -853,7 +853,7 @@ elf_object_p (bfd *abfd)
 	    s->flags |= SEC_DEBUGGING;
 	}
     }
-  return target;
+  return _bfd_no_cleanup;
 
  got_wrong_format_error:
   bfd_set_error (bfd_error_wrong_format);
diff --git a/bfd/elfcore.h b/bfd/elfcore.h
index 591ca531a6..44707ebb60 100644
--- a/bfd/elfcore.h
+++ b/bfd/elfcore.h
@@ -83,7 +83,7 @@ elf_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
     that allow standard bfd access to the general registers (.reg) and the
     floating point registers (.reg2).  */
 
-const bfd_target *
+bfd_cleanup
 elf_core_file_p (bfd *abfd)
 {
   Elf_External_Ehdr x_ehdr;	/* Elf file header, external form.  */
@@ -314,7 +314,7 @@ elf_core_file_p (bfd *abfd)
 
   /* Save the entry point from the ELF header.  */
   abfd->start_address = i_ehdrp->e_entry;
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 
  wrong:
   bfd_set_error (bfd_error_wrong_format);
diff --git a/bfd/format.c b/bfd/format.c
index 659d407018..b181742f3b 100644
--- a/bfd/format.c
+++ b/bfd/format.c
@@ -140,13 +140,15 @@ bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve)
 /* Clear out a subset of BFD state.  */
 
 static void
-bfd_reinit (bfd *abfd, unsigned int section_id)
+bfd_reinit (bfd *abfd, unsigned int section_id, bfd_cleanup cleanup)
 {
+  _bfd_section_id = section_id;
+  if (cleanup)
+    cleanup (abfd);
   abfd->tdata.any = NULL;
   abfd->arch_info = &bfd_default_arch_struct;
   abfd->flags &= BFD_FLAGS_SAVED;
   bfd_section_list_clear (abfd);
-  _bfd_section_id = section_id;
 }
 
 /* Restores bfd state saved by bfd_preserve_save.  */
@@ -220,6 +222,7 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
   int ar_match_index;
   unsigned int initial_section_id = _bfd_section_id;
   struct bfd_preserve preserve, preserve_match;
+  bfd_cleanup cleanup = NULL;
 
   if (matching != NULL)
     *matching = NULL;
@@ -258,9 +261,9 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
       if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)	/* rewind! */
 	goto err_ret;
 
-      right_targ = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
+      cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
 
-      if (right_targ)
+      if (cleanup)
 	goto ok_ret;
 
       /* For a long time the code has dropped through to check all
@@ -291,7 +294,6 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
 
   for (target = bfd_target_vector; *target != NULL; target++)
     {
-      const bfd_target *temp;
       void **high_water;
 
       /* The binary target matches anything, so don't return it when
@@ -309,7 +311,7 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
       /* If we already tried a match, the bfd is modified and may
 	 have sections attached, which will confuse the next
 	 _bfd_check_format call.  */
-      bfd_reinit (abfd, initial_section_id);
+      bfd_reinit (abfd, initial_section_id, cleanup);
       /* Free bfd_alloc memory too.  If we have matched and preserved
 	 a target then the high water mark is that much higher.  */
       if (preserve_match.marker)
@@ -325,10 +327,10 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
       if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
 	goto err_ret;
 
-      temp = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
-      if (temp)
+      cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
+      if (cleanup)
 	{
-	  int match_priority = temp->match_priority;
+	  int match_priority = abfd->xvec->match_priority;
 #if BFD_SUPPORTS_PLUGINS
 	  /* If this object can be handled by a plugin, give that the
 	     lowest priority; objects both handled by a plugin and
@@ -345,11 +347,11 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
 	      /* 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])
+	      if (abfd->xvec == bfd_default_vector[0])
 		goto ok_ret;
 
 	      if (matching_vector)
-		matching_vector[match_count] = temp;
+		matching_vector[match_count] = abfd->xvec;
 	      match_count++;
 
 	      if (match_priority < best_match)
@@ -360,7 +362,7 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
 	      if (match_priority <= best_match)
 		{
 		  /* This format checks out as ok!  */
-		  right_targ = temp;
+		  right_targ = abfd->xvec;
 		  best_count++;
 		}
 	    }
@@ -378,7 +380,7 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
 
 	  if (preserve_match.marker == NULL)
 	    {
-	      match_targ = temp;
+	      match_targ = abfd->xvec;
 	      if (!bfd_preserve_save (abfd, &preserve_match))
 		goto err_ret;
 	    }
@@ -467,12 +469,12 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
 	 RIGHT_TARG again.  */
       if (match_targ != right_targ)
 	{
-	  bfd_reinit (abfd, initial_section_id);
+	  bfd_reinit (abfd, initial_section_id, cleanup);
 	  bfd_release (abfd, preserve.marker);
 	  if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
 	    goto err_ret;
-	  match_targ = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
-	  BFD_ASSERT (match_targ != NULL);
+	  cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
+	  BFD_ASSERT (cleanup != NULL);
 	}
 
     ok_ret:
diff --git a/bfd/i386msdos.c b/bfd/i386msdos.c
index 11c5da6bed..5b56751cd3 100644
--- a/bfd/i386msdos.c
+++ b/bfd/i386msdos.c
@@ -41,7 +41,7 @@ msdos_mkobject (bfd *abfd)
   return aout_32_mkobject (abfd);
 }
 
-static const bfd_target *
+static bfd_cleanup
 msdos_object_p (bfd *abfd)
 {
   struct external_DOS_hdr hdr;
@@ -112,7 +112,7 @@ msdos_object_p (bfd *abfd)
   bfd_set_section_size (section, size);
   section->alignment_power = 4;
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 }
 
 static int
diff --git a/bfd/ihex.c b/bfd/ihex.c
index 958a84b507..68671cc623 100644
--- a/bfd/ihex.c
+++ b/bfd/ihex.c
@@ -487,7 +487,7 @@ ihex_scan (bfd *abfd)
 
 /* Try to recognize an Intel Hex file.  */
 
-static const bfd_target *
+static bfd_cleanup
 ihex_object_p (bfd *abfd)
 {
   void * tdata_save;
@@ -538,7 +538,7 @@ ihex_object_p (bfd *abfd)
       return NULL;
     }
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 }
 
 /* Read the contents of a section in an Intel Hex file.  */
diff --git a/bfd/libaout.h b/bfd/libaout.h
index 81ef4cffd5..bdf917e556 100644
--- a/bfd/libaout.h
+++ b/bfd/libaout.h
@@ -475,8 +475,8 @@ extern bfd_boolean NAME (aout, squirt_out_relocs)
 extern bfd_boolean NAME (aout, make_sections)
   (bfd *);
 
-extern const bfd_target * NAME (aout, some_aout_object_p)
-  (bfd *, struct internal_exec *, const bfd_target *(*) (bfd *));
+extern bfd_cleanup NAME (aout, some_aout_object_p)
+  (bfd *, struct internal_exec *, bfd_cleanup (*) (bfd *));
 
 extern bfd_boolean NAME (aout, mkobject)
   (bfd *);
diff --git a/bfd/libbfd-in.h b/bfd/libbfd-in.h
index 6d796b7ef8..c8cf079a91 100644
--- a/bfd/libbfd-in.h
+++ b/bfd/libbfd-in.h
@@ -141,7 +141,7 @@ extern bfd_boolean _bfd_generic_mkarchive
   (bfd *) ATTRIBUTE_HIDDEN;
 extern char *_bfd_append_relative_path
   (bfd *, char *) ATTRIBUTE_HIDDEN;
-extern const bfd_target *bfd_generic_archive_p
+extern bfd_cleanup bfd_generic_archive_p
   (bfd *) ATTRIBUTE_HIDDEN;
 extern bfd_boolean bfd_slurp_armap
   (bfd *) ATTRIBUTE_HIDDEN;
@@ -227,8 +227,9 @@ extern void _bfd_void_bfd_asection
 
 extern bfd *_bfd_new_bfd_contained_in
   (bfd *) ATTRIBUTE_HIDDEN;
-extern const bfd_target *_bfd_dummy_target
+extern bfd_cleanup _bfd_dummy_target
   (bfd *) ATTRIBUTE_HIDDEN;
+#define _bfd_no_cleanup _bfd_void_bfd
 
 extern void bfd_dont_truncate_arname
   (bfd *, const char *, char *) ATTRIBUTE_HIDDEN;
@@ -425,9 +426,9 @@ extern symindex _bfd_vms_lib_find_symbol
   (bfd *, const char *) ATTRIBUTE_HIDDEN;
 extern bfd *_bfd_vms_lib_get_imagelib_file
   (bfd *) ATTRIBUTE_HIDDEN;
-extern const bfd_target *_bfd_vms_lib_alpha_archive_p
+extern bfd_cleanup _bfd_vms_lib_alpha_archive_p
   (bfd *) ATTRIBUTE_HIDDEN;
-extern const bfd_target *_bfd_vms_lib_ia64_archive_p
+extern bfd_cleanup _bfd_vms_lib_ia64_archive_p
   (bfd *) ATTRIBUTE_HIDDEN;
 extern bfd_boolean _bfd_vms_lib_alpha_mkarchive
   (bfd *) ATTRIBUTE_HIDDEN;
diff --git a/bfd/libbfd.c b/bfd/libbfd.c
index cb7c3b5aba..3579ddb855 100644
--- a/bfd/libbfd.c
+++ b/bfd/libbfd.c
@@ -245,7 +245,7 @@ _bfd_nocore_core_file_pid (bfd *ignore_abfd ATTRIBUTE_UNUSED)
   return 0;
 }
 
-const bfd_target *
+bfd_cleanup
 _bfd_dummy_target (bfd *ignore_abfd ATTRIBUTE_UNUSED)
 {
   bfd_set_error (bfd_error_wrong_format);
diff --git a/bfd/libcoff-in.h b/bfd/libcoff-in.h
index 807817c94e..3030a65fa7 100644
--- a/bfd/libcoff-in.h
+++ b/bfd/libcoff-in.h
@@ -299,7 +299,7 @@ struct coff_reloc_cookie
 #define coff_hash_table(p) ((struct coff_link_hash_table *) ((p)->hash))
 
 /* Functions in coffgen.c.  */
-extern const bfd_target *coff_object_p
+extern bfd_cleanup coff_object_p
   (bfd *);
 extern struct bfd_section *coff_section_from_bfd_index
   (bfd *, int);
diff --git a/bfd/mach-o-aarch64.c b/bfd/mach-o-aarch64.c
index f3406b3be2..c71b92e47c 100644
--- a/bfd/mach-o-aarch64.c
+++ b/bfd/mach-o-aarch64.c
@@ -40,13 +40,13 @@
 #define bfd_mach_o_tgt_seg_table NULL
 #define bfd_mach_o_section_type_valid_for_tgt NULL
 
-static const bfd_target *
+static bfd_cleanup
 bfd_mach_o_arm64_object_p (bfd *abfd)
 {
   return bfd_mach_o_header_p (abfd, 0, 0, BFD_MACH_O_CPU_TYPE_ARM64);
 }
 
-static const bfd_target *
+static bfd_cleanup
 bfd_mach_o_arm64_core_p (bfd *abfd)
 {
   return bfd_mach_o_header_p (abfd, 0,
diff --git a/bfd/mach-o-arm.c b/bfd/mach-o-arm.c
index efdb6bfb98..f4afce71c8 100644
--- a/bfd/mach-o-arm.c
+++ b/bfd/mach-o-arm.c
@@ -38,13 +38,13 @@
 #define bfd_mach_o_tgt_seg_table NULL
 #define bfd_mach_o_section_type_valid_for_tgt NULL
 
-static const bfd_target *
+static bfd_cleanup
 bfd_mach_o_arm_object_p (bfd *abfd)
 {
   return bfd_mach_o_header_p (abfd, 0, 0, BFD_MACH_O_CPU_TYPE_ARM);
 }
 
-static const bfd_target *
+static bfd_cleanup
 bfd_mach_o_arm_core_p (bfd *abfd)
 {
   return bfd_mach_o_header_p (abfd, 0,
diff --git a/bfd/mach-o-i386.c b/bfd/mach-o-i386.c
index 2fcd0deee1..6ef27d1cd1 100644
--- a/bfd/mach-o-i386.c
+++ b/bfd/mach-o-i386.c
@@ -29,13 +29,13 @@
 #define bfd_mach_o_core_p bfd_mach_o_i386_core_p
 #define bfd_mach_o_mkobject bfd_mach_o_i386_mkobject
 
-static const bfd_target *
+static bfd_cleanup
 bfd_mach_o_i386_object_p (bfd *abfd)
 {
   return bfd_mach_o_header_p (abfd, 0, 0, BFD_MACH_O_CPU_TYPE_I386);
 }
 
-static const bfd_target *
+static bfd_cleanup
 bfd_mach_o_i386_core_p (bfd *abfd)
 {
   return bfd_mach_o_header_p (abfd, 0,
diff --git a/bfd/mach-o-x86-64.c b/bfd/mach-o-x86-64.c
index fdc6558f54..9a6b58bc27 100644
--- a/bfd/mach-o-x86-64.c
+++ b/bfd/mach-o-x86-64.c
@@ -29,13 +29,13 @@
 #define bfd_mach_o_core_p bfd_mach_o_x86_64_core_p
 #define bfd_mach_o_mkobject bfd_mach_o_x86_64_mkobject
 
-static const bfd_target *
+static bfd_cleanup
 bfd_mach_o_x86_64_object_p (bfd *abfd)
 {
   return bfd_mach_o_header_p (abfd, 0, 0, BFD_MACH_O_CPU_TYPE_X86_64);
 }
 
-static const bfd_target *
+static bfd_cleanup
 bfd_mach_o_x86_64_core_p (bfd *abfd)
 {
   return bfd_mach_o_header_p (abfd, 0,
diff --git a/bfd/mach-o.c b/bfd/mach-o.c
index 4b7be6eb4e..ee58a7adfa 100644
--- a/bfd/mach-o.c
+++ b/bfd/mach-o.c
@@ -5365,7 +5365,7 @@ bfd_mach_o_gen_mkobject (bfd *abfd)
   return TRUE;
 }
 
-const bfd_target *
+bfd_cleanup
 bfd_mach_o_header_p (bfd *abfd,
 		     file_ptr hdr_off,
 		     bfd_mach_o_filetype file_type,
@@ -5436,7 +5436,7 @@ bfd_mach_o_header_p (bfd *abfd,
   if (!bfd_mach_o_scan (abfd, &header, mdata))
     goto wrong;
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 
  wrong:
   bfd_set_error (bfd_error_wrong_format);
@@ -5445,13 +5445,13 @@ bfd_mach_o_header_p (bfd *abfd,
   return NULL;
 }
 
-static const bfd_target *
+static bfd_cleanup
 bfd_mach_o_gen_object_p (bfd *abfd)
 {
   return bfd_mach_o_header_p (abfd, 0, 0, 0);
 }
 
-static const bfd_target *
+static bfd_cleanup
 bfd_mach_o_gen_core_p (bfd *abfd)
 {
   return bfd_mach_o_header_p (abfd, 0, BFD_MACH_O_MH_CORE, 0);
@@ -5501,7 +5501,7 @@ typedef struct mach_o_fat_data_struct
   mach_o_fat_archentry *archentries;
 } mach_o_fat_data_struct;
 
-const bfd_target *
+bfd_cleanup
 bfd_mach_o_fat_archive_p (bfd *abfd)
 {
   mach_o_fat_data_struct *adata = NULL;
@@ -5551,7 +5551,7 @@ bfd_mach_o_fat_archive_p (bfd *abfd)
 
   abfd->tdata.mach_o_fat_data = adata;
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 
  error:
   if (adata != NULL)
diff --git a/bfd/mach-o.h b/bfd/mach-o.h
index 468de8e96e..278bdd5315 100644
--- a/bfd/mach-o.h
+++ b/bfd/mach-o.h
@@ -679,9 +679,9 @@ bfd_mach_o_xlat_name;
 
 bfd_boolean bfd_mach_o_valid (bfd *);
 bfd_boolean bfd_mach_o_mkobject_init (bfd *);
-const bfd_target *bfd_mach_o_object_p (bfd *);
-const bfd_target *bfd_mach_o_core_p (bfd *);
-const bfd_target *bfd_mach_o_fat_archive_p (bfd *);
+bfd_cleanup bfd_mach_o_object_p (bfd *);
+bfd_cleanup bfd_mach_o_core_p (bfd *);
+bfd_cleanup bfd_mach_o_fat_archive_p (bfd *);
 bfd *bfd_mach_o_fat_openr_next_archived_file (bfd *, bfd *);
 bfd_boolean bfd_mach_o_set_arch_mach (bfd *, enum bfd_architecture,
 				      unsigned long);
@@ -713,8 +713,8 @@ char *bfd_mach_o_core_file_failing_command (bfd *);
 int bfd_mach_o_core_file_failing_signal (bfd *);
 bfd_boolean bfd_mach_o_core_file_matches_executable_p (bfd *, bfd *);
 bfd *bfd_mach_o_fat_extract (bfd *, bfd_format , const bfd_arch_info_type *);
-const bfd_target *bfd_mach_o_header_p (bfd *, file_ptr, bfd_mach_o_filetype,
-				       bfd_mach_o_cpu_type);
+bfd_cleanup bfd_mach_o_header_p (bfd *, file_ptr, bfd_mach_o_filetype,
+				 bfd_mach_o_cpu_type);
 bfd_boolean bfd_mach_o_build_commands (bfd *);
 bfd_boolean bfd_mach_o_set_section_contents (bfd *, asection *, const void *,
 					     file_ptr, bfd_size_type);
diff --git a/bfd/mmo.c b/bfd/mmo.c
index 073c37c0ac..3b7e5c0c33 100644
--- a/bfd/mmo.c
+++ b/bfd/mmo.c
@@ -386,7 +386,7 @@ static INLINE bfd_byte *mmo_get_loc (asection *, bfd_vma, int);
 static void mmo_xore_64 (asection *, bfd_vma vma, bfd_vma value);
 static void mmo_xore_32 (asection *, bfd_vma vma, unsigned int);
 static void mmo_xore_16 (asection *, bfd_vma vma, unsigned int);
-static const bfd_target *mmo_object_p (bfd *);
+static bfd_cleanup mmo_object_p (bfd *);
 static void mmo_map_set_sizes (bfd *, asection *, void *);
 static bfd_boolean mmo_get_symbols (bfd *);
 static bfd_boolean mmo_create_symbol (bfd *, const char *, bfd_vma,
@@ -500,7 +500,7 @@ mmo_init (void)
 
 /* Check whether an existing file is an mmo file.  */
 
-static const bfd_target *
+static bfd_cleanup
 mmo_object_p (bfd *abfd)
 {
   struct stat statbuf;
@@ -556,7 +556,7 @@ mmo_object_p (bfd *abfd)
   if (! bfd_default_set_arch_mach (abfd, bfd_arch_mmix, 0))
     goto bad_format_free;
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 
  bad_format_free:
   free (abfd->tdata.mmo_data->lop_stab_symbol);
diff --git a/bfd/pdp11.c b/bfd/pdp11.c
index 35d45e6480..c13e742c0d 100644
--- a/bfd/pdp11.c
+++ b/bfd/pdp11.c
@@ -452,13 +452,13 @@ NAME (aout, make_sections) (bfd *abfd)
    environment's "finish up" function just before returning, to
    handle any last-minute setup.  */
 
-const bfd_target *
+bfd_cleanup
 NAME (aout, some_aout_object_p) (bfd *abfd,
 				 struct internal_exec *execp,
-				 const bfd_target *(*callback_to_real_object_p) (bfd *))
+				 bfd_cleanup (*callback_to_real_object_p) (bfd *))
 {
   struct aout_data_struct *rawptr, *oldrawptr;
-  const bfd_target *result;
+  bfd_cleanup cleanup;
   size_t amt = sizeof (struct aout_data_struct);
 
   rawptr = bfd_zalloc (abfd, amt);
@@ -580,7 +580,7 @@ NAME (aout, some_aout_object_p) (bfd *abfd,
   adata(abfd)->segment_size = SEGMENT_SIZE;
   adata(abfd)->exec_bytes_size = EXEC_BYTES_SIZE;
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 
   /* The architecture is encoded in various ways in various a.out variants,
      or is not encoded at all in some of them.  The relocation size depends
@@ -592,7 +592,7 @@ NAME (aout, some_aout_object_p) (bfd *abfd,
      header, should cope with them in this callback as well.  */
 #endif	/* DOCUMENTATION */
 
-  result = (*callback_to_real_object_p)(abfd);
+  cleanup = (*callback_to_real_object_p)(abfd);
 
   /* Now that the segment addresses have been worked out, take a better
      guess at whether the file is executable.  If the entry point
@@ -633,12 +633,12 @@ NAME (aout, some_aout_object_p) (bfd *abfd,
     }
 #endif /* STAT_FOR_EXEC */
 
-  if (!result)
+  if (!cleanup)
     {
       free (rawptr);
       abfd->tdata.aout_data = oldrawptr;
     }
-  return result;
+  return cleanup;
 }
 
 /* Initialize ABFD for use with a.out files.  */
diff --git a/bfd/pef.c b/bfd/pef.c
index b0f6c2d2a7..4e749e1949 100644
--- a/bfd/pef.c
+++ b/bfd/pef.c
@@ -589,7 +589,7 @@ bfd_pef_read_header (bfd *abfd, bfd_pef_header *header)
   return 0;
 }
 
-static const bfd_target *
+static bfd_cleanup
 bfd_pef_object_p (bfd *abfd)
 {
   bfd_pef_header header;
@@ -608,7 +608,7 @@ bfd_pef_object_p (bfd *abfd)
   if (bfd_pef_scan (abfd, &header, mdata))
     goto wrong;
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 
  wrong:
   bfd_set_error (bfd_error_wrong_format);
@@ -1126,7 +1126,7 @@ bfd_pef_xlib_scan (bfd *abfd, bfd_pef_xlib_header *header)
   return 0;
 }
 
-static const bfd_target *
+static bfd_cleanup
 bfd_pef_xlib_object_p (bfd *abfd)
 {
   bfd_pef_xlib_header header;
@@ -1151,7 +1151,7 @@ bfd_pef_xlib_object_p (bfd *abfd)
       return NULL;
     }
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 }
 
 const bfd_target pef_xlib_vec =
diff --git a/bfd/peicode.h b/bfd/peicode.h
index f569ccf389..c5a92afefc 100644
--- a/bfd/peicode.h
+++ b/bfd/peicode.h
@@ -122,7 +122,7 @@ typedef struct
 pe_ILF_vars;
 #endif /* COFF_IMAGE_WITH_PE */
 
-const bfd_target *coff_real_object_p
+bfd_cleanup coff_real_object_p
   (bfd *, unsigned, struct internal_filehdr *, struct internal_aouthdr *);
 \f
 #ifndef NO_COFF_RELOCS
@@ -1142,7 +1142,7 @@ pe_ILF_build_a_bfd (bfd *	    abfd,
 /* We have detected a Image Library Format archive element.
    Decode the element and return the appropriate target.  */
 
-static const bfd_target *
+static bfd_cleanup
 pe_ILF_object_p (bfd * abfd)
 {
   bfd_byte	  buffer[14];
@@ -1300,7 +1300,7 @@ pe_ILF_object_p (bfd * abfd)
       return NULL;
     }
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 }
 
 static void
@@ -1394,7 +1394,7 @@ pe_bfd_read_buildid (bfd *abfd)
   free (data);
 }
 
-static const bfd_target *
+static bfd_cleanup
 pe_bfd_object_p (bfd * abfd)
 {
   bfd_byte buffer[6];
@@ -1404,7 +1404,7 @@ pe_bfd_object_p (bfd * abfd)
   struct internal_aouthdr internal_a;
   bfd_size_type opt_hdr_size;
   file_ptr offset;
-  const bfd_target *result;
+  bfd_cleanup result;
 
   /* Detect if this a Microsoft Import Library Format element.  */
   /* First read the beginning of the header.  */
diff --git a/bfd/plugin.c b/bfd/plugin.c
index 72e55a0f60..a0f172d363 100644
--- a/bfd/plugin.c
+++ b/bfd/plugin.c
@@ -733,7 +733,7 @@ try_load_plugin (const char *pname,
 /* There may be plugin libraries in lib/bfd-plugins.  */
 static int has_plugin_list = -1;
 
-static const bfd_target *(*ld_plugin_object_p) (bfd *);
+static bfd_cleanup (*ld_plugin_object_p) (bfd *);
 
 static const char *plugin_name;
 
@@ -774,7 +774,7 @@ bfd_plugin_target_p (const bfd_target *target)
 /* Register OBJECT_P to be used by bfd_plugin_object_p.  */
 
 void
-register_ld_plugin_object_p (const bfd_target *(*object_p) (bfd *))
+register_ld_plugin_object_p (bfd_cleanup (*object_p) (bfd *))
 {
   ld_plugin_object_p = object_p;
 }
@@ -862,7 +862,7 @@ load_plugin (bfd *abfd)
 }
 
 
-static const bfd_target *
+static bfd_cleanup
 bfd_plugin_object_p (bfd *abfd)
 {
   if (ld_plugin_object_p)
@@ -871,7 +871,7 @@ bfd_plugin_object_p (bfd *abfd)
   if (abfd->plugin_format == bfd_plugin_unknown && !load_plugin (abfd))
     return NULL;
 
-  return abfd->plugin_format == bfd_plugin_yes ? abfd->xvec : NULL;
+  return abfd->plugin_format == bfd_plugin_yes ? _bfd_no_cleanup : NULL;
 }
 
 /* Copy any private info we understand from the input bfd
diff --git a/bfd/plugin.h b/bfd/plugin.h
index b2d5e50137..af5d1f4cfa 100644
--- a/bfd/plugin.h
+++ b/bfd/plugin.h
@@ -27,7 +27,7 @@ void bfd_plugin_set_plugin (const char *);
 bfd_boolean bfd_plugin_target_p (const bfd_target *);
 bfd_boolean bfd_plugin_specified_p (void);
 bfd_boolean bfd_link_plugin_object_p (bfd *);
-void register_ld_plugin_object_p (const bfd_target *(*object_p) (bfd *));
+void register_ld_plugin_object_p (bfd_cleanup (*object_p) (bfd *));
 
 typedef struct plugin_data_struct
 {
diff --git a/bfd/ppcboot.c b/bfd/ppcboot.c
index 24f3e0655e..9d9d187613 100644
--- a/bfd/ppcboot.c
+++ b/bfd/ppcboot.c
@@ -130,7 +130,7 @@ ppcboot_set_arch_mach (bfd *abfd,
    was not defaulted.  That is, it must be explicitly specified as
    being ppcboot.  */
 
-static const bfd_target *
+static bfd_cleanup
 ppcboot_object_p (bfd *abfd)
 {
   struct stat statbuf;
@@ -207,7 +207,7 @@ ppcboot_object_p (bfd *abfd)
   memcpy (&tdata->header, &hdr, sizeof (ppcboot_hdr_t));
 
   ppcboot_set_arch_mach (abfd, bfd_arch_powerpc, 0L);
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 }
 
 #define ppcboot_close_and_cleanup _bfd_generic_close_and_cleanup
diff --git a/bfd/rs6000-core.c b/bfd/rs6000-core.c
index ba7f896b99..8e2dd06b46 100644
--- a/bfd/rs6000-core.c
+++ b/bfd/rs6000-core.c
@@ -277,7 +277,7 @@ typedef union
 /* Define prototypes for certain functions, to avoid a compiler warning
    saying that they are missing.  */
 
-const bfd_target * rs6000coff_core_p (bfd *abfd);
+const bfd_cleanup rs6000coff_core_p (bfd *abfd);
 bfd_boolean rs6000coff_core_file_matches_executable_p (bfd *core_bfd,
 						       bfd *exec_bfd);
 char * rs6000coff_core_file_failing_command (bfd *abfd);
@@ -332,7 +332,7 @@ make_bfd_asection (bfd *abfd, const char *name, flagword flags,
 /* Decide if a given bfd represents a `core' file or not. There really is no
    magic number or anything like, in rs6000coff.  */
 
-const bfd_target *
+bfd_cleanup
 rs6000coff_core_p (bfd *abfd)
 {
   CoreHdr core;
@@ -686,7 +686,7 @@ rs6000coff_core_p (bfd *abfd)
   }
 #endif
 
-  return abfd->xvec;		/* This is garbage for now.  */
+  return _bfd_no_cleanup;
 
  fail:
   bfd_release (abfd, abfd->tdata.any);
diff --git a/bfd/som.c b/bfd/som.c
index 9f37b10f96..3aa3d60b02 100644
--- a/bfd/som.c
+++ b/bfd/som.c
@@ -1945,7 +1945,7 @@ som_swap_lst_header_in (struct som_external_lst_header *src,
 /* Perform some initialization for an object.  Save results of this
    initialization in the BFD.  */
 
-static const bfd_target *
+static bfd_cleanup
 som_object_setup (bfd *abfd,
 		  struct som_header *file_hdrp,
 		  struct som_exec_auxhdr *aux_hdrp,
@@ -2060,7 +2060,7 @@ som_object_setup (bfd *abfd,
 				  + current_offset);
   obj_som_exec_data (abfd)->system_id = file_hdrp->system_id;
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 }
 
 /* Convert all of the space and subspace info into BFD sections.  Each space
@@ -2386,7 +2386,7 @@ setup_sections (bfd *abfd,
 
 /* Read in a SOM object and make it into a BFD.  */
 
-static const bfd_target *
+static bfd_cleanup
 som_object_p (bfd *abfd)
 {
   struct som_external_header ext_file_hdr;
diff --git a/bfd/srec.c b/bfd/srec.c
index 266c2b19eb..5ca4f36ae5 100644
--- a/bfd/srec.c
+++ b/bfd/srec.c
@@ -646,7 +646,7 @@ srec_scan (bfd *abfd)
 
 /* Check whether an existing file is an S-record file.  */
 
-static const bfd_target *
+static bfd_cleanup
 srec_object_p (bfd *abfd)
 {
   void * tdata_save;
@@ -676,12 +676,12 @@ srec_object_p (bfd *abfd)
   if (abfd->symcount > 0)
     abfd->flags |= HAS_SYMS;
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 }
 
 /* Check whether an existing file is an S-record file with symbols.  */
 
-static const bfd_target *
+static bfd_cleanup
 symbolsrec_object_p (bfd *abfd)
 {
   void * tdata_save;
@@ -711,7 +711,7 @@ symbolsrec_object_p (bfd *abfd)
   if (abfd->symcount > 0)
     abfd->flags |= HAS_SYMS;
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 }
 
 /* Read in the contents of a section in an S-record file.  */
diff --git a/bfd/targets.c b/bfd/targets.c
index e7cd9a810c..39683e83d4 100644
--- a/bfd/targets.c
+++ b/bfd/targets.c
@@ -176,6 +176,8 @@ DESCRIPTION
 .{* Forward declaration.  *}
 .typedef struct flag_info flag_info;
 .
+.typedef void (*bfd_cleanup) (bfd *);
+.
 .typedef struct bfd_target
 .{
 .  {* Identifies the kind of target, e.g., SunOS4, Ultrix, etc.  *}
@@ -240,9 +242,9 @@ DESCRIPTION
 .  {* Format dependent routines: these are vectors of entry points
 .     within the target vector structure, one for each format to check.  *}
 .
-.  {* Check the format of a file being read.  Return a <<bfd_target *>> or zero.  *}
-.  const struct bfd_target *
-.	       (*_bfd_check_format[bfd_type_end]) (bfd *);
+.  {* Check the format of a file being read.  Return a <<bfd_cleanup>> on
+.     success or zero on failure.  *}
+.  bfd_cleanup (*_bfd_check_format[bfd_type_end]) (bfd *);
 .
 .  {* Set the format of a file being written.  *}
 .  bfd_boolean (*_bfd_set_format[bfd_type_end]) (bfd *);
diff --git a/bfd/tekhex.c b/bfd/tekhex.c
index aaaffa05c2..c2834b32d0 100644
--- a/bfd/tekhex.c
+++ b/bfd/tekhex.c
@@ -600,7 +600,7 @@ tekhex_mkobject (bfd *abfd)
 /* Return TRUE if the file looks like it's in TekHex format. Just look
    for a percent sign and some hex digits.  */
 
-static const bfd_target *
+static bfd_cleanup
 tekhex_object_p (bfd *abfd)
 {
   char b[4];
@@ -619,7 +619,7 @@ tekhex_object_p (bfd *abfd)
   if (!pass_over (abfd, first_phase))
     return NULL;
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 }
 
 static void
diff --git a/bfd/vms-alpha.c b/bfd/vms-alpha.c
index 272d80ab4c..241dab340d 100644
--- a/bfd/vms-alpha.c
+++ b/bfd/vms-alpha.c
@@ -2707,7 +2707,7 @@ alpha_vms_free_private (bfd *abfd)
 /* Check the format for a file being read.
    Return a (bfd_target *) if it's an object file or zero if not.  */
 
-static const struct bfd_target *
+static bfd_cleanup
 alpha_vms_object_p (bfd *abfd)
 {
   void *tdata_save = abfd->tdata.any;
@@ -2826,7 +2826,7 @@ alpha_vms_object_p (bfd *abfd)
   if (! bfd_default_set_arch_mach (abfd, bfd_arch_alpha, 0))
     goto err_wrong_format;
 
-  return abfd->xvec;
+  return alpha_vms_free_private;
 
  err_wrong_format:
   bfd_set_error (bfd_error_wrong_format);
diff --git a/bfd/vms-lib.c b/bfd/vms-lib.c
index 66a40bc91c..a6335218fa 100644
--- a/bfd/vms-lib.c
+++ b/bfd/vms-lib.c
@@ -489,7 +489,7 @@ vms_lib_read_index (bfd *abfd, int idx, unsigned int *nbrel)
 
 /* Standard function.  */
 
-static const bfd_target *
+static bfd_cleanup
 _bfd_vms_lib_archive_p (bfd *abfd, enum vms_lib_kind kind)
 {
   struct vms_lhd lhd;
@@ -693,7 +693,7 @@ _bfd_vms_lib_archive_p (bfd *abfd, enum vms_lib_kind kind)
   if (tdata->type == LBR__C_TYP_ESHSTB || tdata->type == LBR__C_TYP_ISHSTB)
     abfd->is_thin_archive = TRUE;
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 
  err:
   bfd_release (abfd, tdata);
@@ -703,7 +703,7 @@ _bfd_vms_lib_archive_p (bfd *abfd, enum vms_lib_kind kind)
 
 /* Standard function for alpha libraries.  */
 
-const bfd_target *
+bfd_cleanup
 _bfd_vms_lib_alpha_archive_p (bfd *abfd)
 {
   return _bfd_vms_lib_archive_p (abfd, vms_lib_alpha);
@@ -711,7 +711,7 @@ _bfd_vms_lib_alpha_archive_p (bfd *abfd)
 
 /* Standard function for ia64 libraries.  */
 
-const bfd_target *
+bfd_cleanup
 _bfd_vms_lib_ia64_archive_p (bfd *abfd)
 {
   return _bfd_vms_lib_archive_p (abfd, vms_lib_ia64);
@@ -719,7 +719,7 @@ _bfd_vms_lib_ia64_archive_p (bfd *abfd)
 
 /* Standard function for text libraries.  */
 
-static const bfd_target *
+static bfd_cleanup
 _bfd_vms_lib_txt_archive_p (bfd *abfd)
 {
   return _bfd_vms_lib_archive_p (abfd, vms_lib_txt);
diff --git a/bfd/wasm-module.c b/bfd/wasm-module.c
index fa8648b9ac..ac78692816 100644
--- a/bfd/wasm-module.c
+++ b/bfd/wasm-module.c
@@ -731,7 +731,7 @@ wasm_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
 
 /* Check whether ABFD is a WebAssembly module; if so, scan it.  */
 
-static const bfd_target *
+static bfd_cleanup
 wasm_object_p (bfd *abfd)
 {
   bfd_boolean error;
@@ -761,7 +761,7 @@ wasm_object_p (bfd *abfd)
   if (s != NULL && wasm_scan_name_function_section (abfd, s))
     abfd->flags |= HAS_SYMS;
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 }
 
 /* BFD_JUMP_TABLE_WRITE */
diff --git a/bfd/xsym.c b/bfd/xsym.c
index 3071a9de3f..525d6d98ab 100644
--- a/bfd/xsym.c
+++ b/bfd/xsym.c
@@ -2230,7 +2230,7 @@ bfd_sym_scan (bfd *abfd, bfd_sym_version version, bfd_sym_data_struct *mdata)
   return 0;
 }
 
-const bfd_target *
+bfd_cleanup
 bfd_sym_object_p (bfd *abfd)
 {
   bfd_sym_version version = -1;
@@ -2247,7 +2247,7 @@ bfd_sym_object_p (bfd *abfd)
   if (bfd_sym_scan (abfd, version, mdata) != 0)
     goto wrong;
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 
  wrong:
   bfd_set_error (bfd_error_wrong_format);
diff --git a/bfd/xsym.h b/bfd/xsym.h
index 061efd6b05..393b72b5e0 100644
--- a/bfd/xsym.h
+++ b/bfd/xsym.h
@@ -686,7 +686,7 @@ extern void bfd_sym_display_type_information_table
   (bfd *, FILE *);
 extern int bfd_sym_scan
   (bfd *, bfd_sym_version, bfd_sym_data_struct *);
-extern const bfd_target * bfd_sym_object_p
+extern bfd_cleanup bfd_sym_object_p
   (bfd *);
 extern void bfd_sym_get_symbol_info
   (bfd *, asymbol *, symbol_info *);
diff --git a/ld/ChangeLog b/ld/ChangeLog
index 118b5c57e1..dea230a99c 100644
--- a/ld/ChangeLog
+++ b/ld/ChangeLog
@@ -1,3 +1,8 @@
+2020-03-02  Alan Modra  <amodra@gmail.com>
+
+	* plugin.c (plugin_object_p): Return a bfd_cleanup.
+	(plugin_cleanup): New function.
+
 2020-02-27  Alan Modra  <amodra@gmail.com>
 
 	PR 24511
diff --git a/ld/plugin.c b/ld/plugin.c
index 23a314543b..0fbcf9e698 100644
--- a/ld/plugin.c
+++ b/ld/plugin.c
@@ -175,7 +175,7 @@ static bfd_boolean plugin_notice (struct bfd_link_info *,
 				  struct bfd_link_hash_entry *,
 				  bfd *, asection *, bfd_vma, flagword);
 
-static const bfd_target * plugin_object_p (bfd *);
+static bfd_cleanup plugin_object_p (bfd *);
 
 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
 
@@ -1164,7 +1164,12 @@ plugin_strdup (bfd *abfd, const char *str)
   return copy;
 }
 
-static const bfd_target *
+static void
+plugin_cleanup (bfd *abfd ATTRIBUTE_UNUSED)
+{
+}
+
+static bfd_cleanup
 plugin_object_p (bfd *ibfd)
 {
   int claimed;
@@ -1179,7 +1184,7 @@ plugin_object_p (bfd *ibfd)
   if (ibfd->plugin_format != bfd_plugin_unknown)
     {
       if (ibfd->plugin_format == bfd_plugin_yes)
-	return ibfd->plugin_dummy_bfd->xvec;
+	return plugin_cleanup;
       else
 	return NULL;
     }
@@ -1239,7 +1244,7 @@ plugin_object_p (bfd *ibfd)
       ibfd->plugin_format = bfd_plugin_yes;
       ibfd->plugin_dummy_bfd = abfd;
       bfd_make_readable (abfd);
-      return abfd->xvec;
+      return plugin_cleanup;
     }
   else
     {

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 6+ messages in thread
* Re: bfd_cleanup for object_p
@ 2022-09-24  2:05 Alan Modra
  0 siblings, 0 replies; 6+ messages in thread
From: Alan Modra @ 2022-09-24  2:05 UTC (permalink / raw)
  To: binutils

Bits still missing from commit cb001c0d283d.

	* aoutx.h (aout_@var{size}_some_aout_object_p): Correct synopsis.
	* i386lynx.c (lynx_core_file_p): Correct return type.
	* ptrace-core.c (ptrace_unix_core_file_p): Likewise.

diff --git a/bfd/aoutx.h b/bfd/aoutx.h
index e9b5188e8e7..4aed23426ca 100644
--- a/bfd/aoutx.h
+++ b/bfd/aoutx.h
@@ -451,10 +451,10 @@ FUNCTION
 	aout_@var{size}_some_aout_object_p
 
 SYNOPSIS
-	const bfd_target *aout_@var{size}_some_aout_object_p
+	bfd_cleanup aout_@var{size}_some_aout_object_p
 	 (bfd *abfd,
 	  struct internal_exec *execp,
-	  const bfd_target *(*callback_to_real_object_p) (bfd *));
+	  bfd_cleanup (*callback_to_real_object_p) (bfd *));
 
 DESCRIPTION
 	Some a.out variant thinks that the file open in @var{abfd}
diff --git a/bfd/i386lynx.c b/bfd/i386lynx.c
index 840be8fd36d..5df3d19ffe0 100644
--- a/bfd/i386lynx.c
+++ b/bfd/i386lynx.c
@@ -87,7 +87,7 @@
 char *lynx_core_file_failing_command ();
 int lynx_core_file_failing_signal ();
 bool lynx_core_file_matches_executable_p ();
-const bfd_target *lynx_core_file_p ();
+bfd_cleanup lynx_core_file_p ();
 
 #define	MY_core_file_failing_command lynx_core_file_failing_command
 #define	MY_core_file_failing_signal lynx_core_file_failing_signal
diff --git a/bfd/ptrace-core.c b/bfd/ptrace-core.c
index c4afffbfb95..cf39ff42a6e 100644
--- a/bfd/ptrace-core.c
+++ b/bfd/ptrace-core.c
@@ -48,7 +48,7 @@ struct trad_core_struct
 
 /* forward declarations */
 
-const bfd_target *ptrace_unix_core_file_p (bfd *abfd);
+bfd_cleanup ptrace_unix_core_file_p (bfd *abfd);
 char * ptrace_unix_core_file_failing_command (bfd *abfd);
 int ptrace_unix_core_file_failing_signal (bfd *abfd);
 #define ptrace_unix_core_file_matches_executable_p generic_core_file_matches_executable_p

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-09-24  2:05 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-02  8:59 bfd_cleanup for object_p Alan Modra
2020-03-02 12:38 ` H.J. Lu
2020-03-02 13:22   ` Alan Modra
2020-03-02 13:46     ` Alan Modra
2020-03-03 11:14 ` bfd_check_format_matches preserving matches vs. cleanups Alan Modra
2022-09-24  2:05 bfd_cleanup for object_p Alan Modra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).