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
  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-03 11:14 ` bfd_check_format_matches preserving matches vs. cleanups Alan Modra
  1 sibling, 1 reply; 6+ messages in thread
From: H.J. Lu @ 2020-03-02 12:38 UTC (permalink / raw)
  To: Alan Modra; +Cc: Binutils

[-- Attachment #1: Type: text/plain, Size: 6369 bytes --]

On Mon, Mar 2, 2020 at 12:59 AM Alan Modra <amodra@gmail.com> wrote:
>
> 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.

I am checking in this to fix Linux/i386 build:

libtool: compile:  /usr/gcc-9.2.1-32bit/bin/gcc -m32 -DHAVE_CONFIG_H
-I. -I/export/gnu/import/git/sources/binutils-gdb/bfd
-DBINDIR=\"/usr/local/bin\" -DLIBDIR=\"/usr/local/lib\" -DTRAD_CORE
-I. -I/export/gnu/import/git/sources/binutils-gdb/bfd
-I/export/gnu/import/git/sources/binutils-gdb/bfd/../include
-DHAVE_i386_elf32_vec -DHAVE_iamcu_elf32_vec -DHAVE_i386_pei_vec
-DHAVE_elf32_le_vec -DHAVE_elf32_be_vec -W -Wall -Wstrict-prototypes
-Wmissing-prototypes -Wshadow -Wstack-usage=262144 -Werror
-I/export/gnu/import/git/sources/binutils-gdb/bfd/../zlib -g -O2 -MT
trad-core.lo -MD -MP -MF .deps/trad-core.Tpo -c
/export/gnu/import/git/sources/binutils-gdb/bfd/trad-core.c -o
trad-core.o
/export/gnu/import/git/sources/binutils-gdb/bfd/trad-core.c:293:7:
error: initialization of ‘void (* (*)(bfd *))(bfd *)’ {aka ‘void (*
(*)(struct bfd *))(struct bfd *)’} from incompatible pointer type
‘const bfd_target * (*)(bfd *)’ {aka ‘const struct bfd_target *
(*)(struct bfd *)’} [-Werror=incompatible-pointer-types]
  293 |       trad_unix_core_file_p  /* a core file */
      |       ^~~~~~~~~~~~~~~~~~~~~
/export/gnu/import/git/sources/binutils-gdb/bfd/trad-core.c:293:7:
note: (near initialization for ‘core_trad_vec._bfd_check_format[3]’)
mv -f .deps/cofflink.Tpo .deps/cofflink.Plo
cc1: all warnings being treated as errors

-- 
H.J.

[-- Attachment #2: 0001-trad_unix_core_file_p-Return-bfd_cleanup.patch --]
[-- Type: text/x-patch, Size: 1303 bytes --]

From 728d32c496435cbd2529f7de9f5277d88c9c04e2 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Mon, 2 Mar 2020 04:35:23 -0800
Subject: [PATCH] trad_unix_core_file_p: Return bfd_cleanup

	* trad-core.c (trad_unix_core_file_p): Return bfd_cleanup.
---
 bfd/ChangeLog   | 4 ++++
 bfd/trad-core.c | 4 ++--
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/bfd/ChangeLog b/bfd/ChangeLog
index a917631b38..505da06add 100644
--- a/bfd/ChangeLog
+++ b/bfd/ChangeLog
@@ -1,3 +1,7 @@
+2020-03-02  H.J. Lu  <hongjiu.lu@intel.com>
+
+	* trad-core.c (trad_unix_core_file_p): Return bfd_cleanup.
+
 2020-03-02  Alan Modra  <amodra@gmail.com>
 
 	* targets.c (bfd_cleanup): New typedef.
diff --git a/bfd/trad-core.c b/bfd/trad-core.c
index 10ab252134..1b2477a4c5 100644
--- a/bfd/trad-core.c
+++ b/bfd/trad-core.c
@@ -71,7 +71,7 @@ struct trad_core_struct
 
 /* Handle 4.2-style (and perhaps also sysV-style) core dump file.  */
 
-static const bfd_target *
+static bfd_cleanup
 trad_unix_core_file_p (bfd *abfd)
 {
   int val;
@@ -220,7 +220,7 @@ trad_unix_core_file_p (bfd *abfd)
   core_datasec (abfd)->alignment_power = 2;
   core_regsec (abfd)->alignment_power = 2;
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 
  fail:
   bfd_release (abfd, abfd->tdata.any);
-- 
2.24.1


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

* Re: bfd_cleanup for object_p
  2020-03-02 12:38 ` H.J. Lu
@ 2020-03-02 13:22   ` Alan Modra
  2020-03-02 13:46     ` Alan Modra
  0 siblings, 1 reply; 6+ messages in thread
From: Alan Modra @ 2020-03-02 13:22 UTC (permalink / raw)
  To: H.J. Lu; +Cc: Binutils

On Mon, Mar 02, 2020 at 04:37:30AM -0800, H.J. Lu wrote:
> 	* trad-core.c (trad_unix_core_file_p): Return bfd_cleanup.

Thanks, I missed some others too.

	* aix386-core.c (aix386_core_file_p): Return bfd_cleanup.
	* aix5ppc-core.c (xcoff64_core_p): Likewise.
	* cisco-core.c (cisco_core_file_validate): Likewise.
	* hppabsd-core.c (hppabsd_core_core_file_p): Likewise.
	* hpux-core.c (hpux_core_core_file_p): Likewise.
	* irix-core.c (irix_core_core_file_p): Likewise.
	* lynx-core.c (lynx_core_file_p): Likewise.
	* netbsd-core.c (netbsd_core_file_p): Likewise.
	* osf-core.c (osf_core_core_file_p): Likewise.
	* ptrace-core.c (ptrace_unix_core_file_p): Likewise.
	* sco5-core.c (sco5_core_file_p): Likewise.

diff --git a/bfd/aix386-core.c b/bfd/aix386-core.c
index c9ec756c4a..287ad04235 100644
--- a/bfd/aix386-core.c
+++ b/bfd/aix386-core.c
@@ -65,7 +65,7 @@ struct trad_core_struct
   asection *sections[MAX_CORE_SEGS];
 };
 
-static const bfd_target *
+static bfd_cleanup
 aix386_core_file_p (bfd *abfd)
 {
   int i, n;
@@ -189,7 +189,7 @@ aix386_core_file_p (bfd *abfd)
       n++;
     }
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 }
 
 static char *
diff --git a/bfd/aix5ppc-core.c b/bfd/aix5ppc-core.c
index 806d2ec02b..cddb647de1 100644
--- a/bfd/aix5ppc-core.c
+++ b/bfd/aix5ppc-core.c
@@ -23,7 +23,7 @@
 #include "sysdep.h"
 #include "bfd.h"
 
-const bfd_target *xcoff64_core_p (bfd *);
+bfd_cleanup xcoff64_core_p (bfd *);
 bfd_boolean xcoff64_core_file_matches_executable_p (bfd *, bfd *);
 char *xcoff64_core_file_failing_command (bfd *);
 int xcoff64_core_file_failing_signal (bfd *);
@@ -48,7 +48,7 @@ int xcoff64_core_file_failing_signal (bfd *);
 #define CHECK_FILE_OFFSET(s, v) \
   ((bfd_signed_vma)(v) < 0 || (bfd_signed_vma)(v) > (bfd_signed_vma)(s).st_size)
 
-const bfd_target *
+bfd_cleanup
 xcoff64_core_p (bfd *abfd)
 {
   enum bfd_architecture arch;
@@ -60,7 +60,6 @@ xcoff64_core_p (bfd *abfd)
   bfd_vma ld_offset;
   bfd_size_type i;
   struct vm_infox vminfo;
-  const bfd_target *return_value = NULL;
   flagword flags;
 
   /* Get the header.  */
@@ -109,12 +108,12 @@ xcoff64_core_p (bfd *abfd)
     {
       bfd_set_error (bfd_error_file_truncated);
 
-      return return_value;
+      return NULL;
     }
 
   new_core_hdr = bfd_zalloc (abfd, sizeof (struct core_dumpxx));
   if (NULL == new_core_hdr)
-    return return_value;
+    return NULL;
 
   memcpy (new_core_hdr, &core, sizeof (struct core_dumpxx));
   /* The core_hdr() macro is no longer used here because it would
@@ -126,7 +125,7 @@ xcoff64_core_p (bfd *abfd)
   flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
   sec = bfd_make_section_anyway_with_flags (abfd, ".stack", flags);
   if (NULL == sec)
-    return return_value;
+    return NULL;
 
   sec->size = core.c_size;
   sec->vma = core.c_stackorg;
@@ -136,7 +135,7 @@ xcoff64_core_p (bfd *abfd)
   flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
   sec = bfd_make_section_anyway_with_flags (abfd, ".reg", flags);
   if (NULL == sec)
-    return return_value;
+    return NULL;
 
   sec->size = sizeof (struct __context64);
   sec->vma = 0;
@@ -150,7 +149,7 @@ xcoff64_core_p (bfd *abfd)
   flags = SEC_HAS_CONTENTS;
   sec = bfd_make_section_anyway_with_flags (abfd, ".ldinfo", flags);
   if (NULL == sec)
-    return return_value;
+    return NULL;
 
   sec->size = core.c_lsize;
   sec->vma = 0;
@@ -164,7 +163,7 @@ xcoff64_core_p (bfd *abfd)
   flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
   sec = bfd_make_section_anyway_with_flags (abfd, ".data", flags);
   if (NULL == sec)
-    return return_value;
+    return NULL;
 
   sec->size = core.c_datasize;
   sec->vma = core.c_dataorg;
@@ -176,18 +175,18 @@ xcoff64_core_p (bfd *abfd)
   while (1)
     {
       if (bfd_seek (abfd, ld_offset, SEEK_SET) != 0)
-	return return_value;
+	return NULL;
 
       if (sizeof (struct __ld_info64) !=
 	  bfd_bread (&ldinfo, sizeof (struct __ld_info64), abfd))
-	return return_value;
+	return NULL;
 
       if (ldinfo.ldinfo_core)
 	{
 	  flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
 	  sec = bfd_make_section_anyway_with_flags (abfd, ".data", flags);
 	  if (NULL == sec)
-	    return return_value;
+	    return NULL;
 
 	  sec->size = ldinfo.ldinfo_datasize;
 	  sec->vma = ldinfo.ldinfo_dataorg;
@@ -203,19 +202,19 @@ xcoff64_core_p (bfd *abfd)
   if (core.c_vmregions)
     {
       if (bfd_seek (abfd, core.c_vmm, SEEK_SET) != 0)
-	return return_value;
+	return NULL;
 
       for (i = 0; i < core.c_vmregions; i++)
 	if (sizeof (struct vm_infox) !=
 	    bfd_bread (&vminfo, sizeof (struct vm_infox), abfd))
-	  return return_value;
+	  return NULL;
 
       if (vminfo.vminfo_offset)
 	{
 	  flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
 	  sec = bfd_make_section_anyway_with_flags (abfd, ".vmdata", flags);
 	  if (NULL == sec)
-	    return return_value;
+	    return NULL;
 
 	  sec->size = vminfo.vminfo_size;
 	  sec->vma = vminfo.vminfo_addr;
@@ -228,13 +227,13 @@ xcoff64_core_p (bfd *abfd)
   mach = DEFAULT_MACHINE;
   bfd_default_set_arch_mach (abfd, arch, mach);
 
-  return_value = (bfd_target *) abfd->xvec;	/* This is garbage for now.  */
+  return _bfd_no_cleanup;
 
  xcoff64_core_p_error:
   if (bfd_get_error () != bfd_error_system_call)
     bfd_set_error (bfd_error_wrong_format);
 
-  return return_value;
+  return NULL;
 }
 
 /* Return `TRUE' if given core is from the given executable.  */
@@ -329,7 +328,7 @@ xcoff64_core_file_failing_signal (bfd *abfd)
 
 #else /* AIX_5_CORE */
 
-const bfd_target *
+bfd_cleanup
 xcoff64_core_p (bfd *abfd ATTRIBUTE_UNUSED)
 {
   bfd_set_error (bfd_error_wrong_format);
diff --git a/bfd/cisco-core.c b/bfd/cisco-core.c
index da18d2edbf..e43982bf08 100644
--- a/bfd/cisco-core.c
+++ b/bfd/cisco-core.c
@@ -80,7 +80,7 @@ struct cisco_core_struct
 /* Examine the file for a crash info struct at the offset given by
    CRASH_INFO_LOC.  */
 
-static const bfd_target *
+static bfd_cleanup
 cisco_core_file_validate (bfd *abfd, int crash_info_loc)
 {
   char buf[4];
@@ -274,7 +274,7 @@ cisco_core_file_validate (bfd *abfd, int crash_info_loc)
   nread = statbuf.st_size - asect->filepos;
   asect->size = (nread < 1024) ? nread : 1024;
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 
   /* Get here if we have already started filling out the BFD
      and there is an error of some kind.  */
diff --git a/bfd/hppabsd-core.c b/bfd/hppabsd-core.c
index 347771c624..e0f2b50037 100644
--- a/bfd/hppabsd-core.c
+++ b/bfd/hppabsd-core.c
@@ -90,7 +90,7 @@ make_bfd_asection (bfd *abfd,
   return asect;
 }
 
-static const bfd_target *
+static bfd_cleanup
 hppabsd_core_core_file_p (bfd *abfd)
 {
   int val;
@@ -181,7 +181,7 @@ hppabsd_core_core_file_p (bfd *abfd)
 
   strncpy (core_command (abfd), u.u_comm, MAXCOMLEN + 1);
   core_signal (abfd) = u.u_code;
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 
  fail:
   bfd_release (abfd, abfd->tdata.any);
diff --git a/bfd/hpux-core.c b/bfd/hpux-core.c
index 36a17dc9d9..2b4de9e530 100644
--- a/bfd/hpux-core.c
+++ b/bfd/hpux-core.c
@@ -157,7 +157,7 @@ thread_section_p (bfd *abfd ATTRIBUTE_UNUSED,
    in which the core file is generated by some non-hpux application.
    (I am just guessing here!)
 */
-static const bfd_target *
+static bfd_cleanup
 hpux_core_core_file_p (bfd *abfd)
 {
   int  good_sections = 0;
@@ -342,7 +342,7 @@ hpux_core_core_file_p (bfd *abfd)
        "  As a result, some information may be unavailable.\n",
        abfd);
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 
  fail:
   bfd_release (abfd, core_hdr (abfd));
diff --git a/bfd/irix-core.c b/bfd/irix-core.c
index 7b214e0a0a..a63d4104d4 100644
--- a/bfd/irix-core.c
+++ b/bfd/irix-core.c
@@ -166,7 +166,7 @@ make_bfd_asection (bfd *abfd,
   return asect;
 }
 
-static const bfd_target *
+static bfd_cleanup
 irix_core_core_file_p (bfd *abfd)
 {
   int val;
@@ -244,7 +244,7 @@ irix_core_core_file_p (bfd *abfd)
   /* OK, we believe you.  You're a core file (sure, sure).  */
   bfd_default_set_arch_mach (abfd, bfd_arch_mips, 0);
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 
  fail:
   bfd_release (abfd, core_hdr (abfd));
diff --git a/bfd/lynx-core.c b/bfd/lynx-core.c
index 66b54a2fd2..08d0883861 100644
--- a/bfd/lynx-core.c
+++ b/bfd/lynx-core.c
@@ -85,7 +85,7 @@ make_bfd_asection (bfd *abfd,
   return asect;
 }
 
-const bfd_target *
+bfd_cleanup
 lynx_core_file_p (bfd *abfd)
 {
   int secnum;
@@ -191,7 +191,7 @@ lynx_core_file_p (bfd *abfd)
 	goto fail;
     }
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 
  fail:
   bfd_release (abfd, core_hdr (abfd));
diff --git a/bfd/netbsd-core.c b/bfd/netbsd-core.c
index ae88fd6f74..83ad33060f 100644
--- a/bfd/netbsd-core.c
+++ b/bfd/netbsd-core.c
@@ -51,7 +51,7 @@ struct netbsd_core_struct
 
 /* Handle NetBSD-style core dump file.  */
 
-static const bfd_target *
+static bfd_cleanup
 netbsd_core_file_p (bfd *abfd)
 {
   int val;
@@ -222,7 +222,7 @@ netbsd_core_file_p (bfd *abfd)
     }
 
   /* OK, we believe you.  You're a core file (sure, sure).  */
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 
  punt:
   bfd_release (abfd, abfd->tdata.any);
diff --git a/bfd/osf-core.c b/bfd/osf-core.c
index 19cd4f2b75..3f384e7bd4 100644
--- a/bfd/osf-core.c
+++ b/bfd/osf-core.c
@@ -70,7 +70,7 @@ make_bfd_asection (bfd *abfd,
   return asect;
 }
 
-static const bfd_target *
+static bfd_cleanup
 osf_core_core_file_p (bfd *abfd)
 {
   int val;
@@ -138,7 +138,7 @@ osf_core_core_file_p (bfd *abfd)
 
   /* OK, we believe you.  You're a core file (sure, sure).  */
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 
  fail:
   bfd_release (abfd, core_hdr (abfd));
diff --git a/bfd/ptrace-core.c b/bfd/ptrace-core.c
index 48a9b1c669..e9802f8ea0 100644
--- a/bfd/ptrace-core.c
+++ b/bfd/ptrace-core.c
@@ -55,7 +55,7 @@ int ptrace_unix_core_file_failing_signal (bfd *abfd);
 #define ptrace_unix_core_file_pid _bfd_nocore_core_file_pid
 static void swap_abort (void);
 
-const bfd_target *
+bfd_cleanup
 ptrace_unix_core_file_p (bfd *abfd)
 {
   int val;
@@ -124,7 +124,7 @@ ptrace_unix_core_file_p (bfd *abfd)
   core_datasec (abfd)->alignment_power = 2;
   core_regsec (abfd)->alignment_power = 2;
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 
  fail:
   bfd_release (abfd, abfd->tdata.any);
diff --git a/bfd/sco5-core.c b/bfd/sco5-core.c
index a9afbbacd9..84dbf4f27f 100644
--- a/bfd/sco5-core.c
+++ b/bfd/sco5-core.c
@@ -101,7 +101,7 @@ read_uarea (bfd *abfd, int filepos)
   return &rawptr->u;
 }
 
-const bfd_target *
+const bfd_cleanup
 sco5_core_file_p (bfd *abfd)
 {
   int coffset_siz, val, nsecs, cheadoffs;
@@ -300,7 +300,7 @@ sco5_core_file_p (bfd *abfd)
 
     }
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 
  fail:
   if (abfd->tdata.any)

-- 
Alan Modra
Australia Development Lab, IBM

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

* Re: bfd_cleanup for object_p
  2020-03-02 13:22   ` Alan Modra
@ 2020-03-02 13:46     ` Alan Modra
  0 siblings, 0 replies; 6+ messages in thread
From: Alan Modra @ 2020-03-02 13:46 UTC (permalink / raw)
  To: H.J. Lu; +Cc: Binutils

I hate files that you can't compile.

	* cisco-core.c (cisco_core_file_p): Return bfd_cleanup.
	* hpux-core.c (hpux_core_core_file_p): Update prototype.
	* sco5-core.c (sco5_core_file_p): Return bfd_cleanup.
	(core_sco5_vec): Correct initialisers.

diff --git a/bfd/cisco-core.c b/bfd/cisco-core.c
index e43982bf08..b733d14249 100644
--- a/bfd/cisco-core.c
+++ b/bfd/cisco-core.c
@@ -286,19 +286,19 @@ cisco_core_file_validate (bfd *abfd, int crash_info_loc)
   return NULL;
 }
 
-static const bfd_target *
+static bfd_cleanup
 cisco_core_file_p (bfd *abfd)
 {
   int *crash_info_locp;
-  const bfd_target *target = NULL;
+  bfd_cleanup cleanup = NULL;
 
   for (crash_info_locp = crash_info_locs;
-       *crash_info_locp != -1  &&  target == NULL;
+       *crash_info_locp != -1 && cleanup == NULL;
        crash_info_locp++)
     {
-      target = cisco_core_file_validate (abfd, *crash_info_locp);
+      cleanup = cisco_core_file_validate (abfd, *crash_info_locp);
     }
-  return (target);
+  return cleanup;
 }
 
 static char *
diff --git a/bfd/hpux-core.c b/bfd/hpux-core.c
index 2b4de9e530..2ecd4f17d5 100644
--- a/bfd/hpux-core.c
+++ b/bfd/hpux-core.c
@@ -106,7 +106,7 @@ struct hpux_core_struct
 
 static asection *make_bfd_asection (bfd *, const char *, flagword,
 				    bfd_size_type, bfd_vma, unsigned int);
-static const bfd_target *hpux_core_core_file_p (bfd *);
+static bfd_cleanup hpux_core_core_file_p (bfd *);
 static char *hpux_core_core_file_failing_command (bfd *);
 static int hpux_core_core_file_failing_signal (bfd *);
 static void swap_abort (void);
diff --git a/bfd/sco5-core.c b/bfd/sco5-core.c
index 84dbf4f27f..71e264da4a 100644
--- a/bfd/sco5-core.c
+++ b/bfd/sco5-core.c
@@ -101,7 +101,7 @@ read_uarea (bfd *abfd, int filepos)
   return &rawptr->u;
 }
 
-const bfd_cleanup
+bfd_cleanup
 sco5_core_file_p (bfd *abfd)
 {
   int coffset_siz, val, nsecs, cheadoffs;
@@ -172,7 +172,7 @@ sco5_core_file_p (bfd *abfd)
 			      (file_ptr) coffsets.u_stack))
 	goto fail;
 
-      return abfd->xvec;		/* Done for version 1 */
+      return _bfd_no_cleanup;		/* Done for version 1 */
     }
 
   /* Immediately before coreoffsets region is a long with offset in core
@@ -372,12 +372,16 @@ const bfd_target core_sco5_vec =
       sco5_core_file_p			/* a core file */
     },
     {				/* bfd_set_format */
-      _bfd_bool_bfd_false_error, bfd_false,
-      _bfd_bool_bfd_false_error, bfd_false
+      _bfd_bool_bfd_false_error,
+      _bfd_bool_bfd_false_error,
+      _bfd_bool_bfd_false_error,
+      _bfd_bool_bfd_false_error
     },
     {				/* bfd_write_contents */
-      _bfd_bool_bfd_false_error, bfd_false,
-      _bfd_bool_bfd_false_error, bfd_false
+      _bfd_bool_bfd_false_error,
+      _bfd_bool_bfd_false_error,
+      _bfd_bool_bfd_false_error,
+      _bfd_bool_bfd_false_error
     },
 
     BFD_JUMP_TABLE_GENERIC (_bfd_generic),

-- 
Alan Modra
Australia Development Lab, IBM

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

* bfd_check_format_matches preserving matches vs. cleanups
  2020-03-02  8:59 bfd_cleanup for object_p Alan Modra
  2020-03-02 12:38 ` H.J. Lu
@ 2020-03-03 11:14 ` Alan Modra
  1 sibling, 0 replies; 6+ messages in thread
From: Alan Modra @ 2020-03-03 11:14 UTC (permalink / raw)
  To: binutils

It didn't take long for oss-fuzz to find double frees due to a bug in
the cleanup logic.  It's seen when reading in any alpha-vms object
file except when alpha_vms_vec is the default.  But alpha_vms_vec is
of course the default when building for --target=alpha-dec-vms (and
naturally what I used to  test the cleanup support since that is the
only target with a cleanup that does anything currently).

Anyway, the bug is that if bfd_check_format_matches is to preserve a
match the cleanup for that match can't be run.  Quite obviously that
would destroy part of the match state.

	* format.c (struct bfd_preserve): Add cleanup field.
	(bfd_preserve_save): Add cleanup param and save.
	(bfd_preserve_restore): Return cleanup.
	(bfd_preserve_finish): Call the cleanup for the discarded match.
	(bfd_check_format_matches): Pass cleanup to bfd_preserve_save,
	and clear when preserving a match.  Restore cleanup too when
	restoring that match.

diff --git a/bfd/format.c b/bfd/format.c
index b181742f3b..61f26c6042 100644
--- a/bfd/format.c
+++ b/bfd/format.c
@@ -106,6 +106,7 @@ struct bfd_preserve
   unsigned int section_id;
   struct bfd_hash_table section_htab;
   const struct bfd_build_id *build_id;
+  bfd_cleanup cleanup;
 };
 
 /* When testing an object for compatibility with a particular target
@@ -118,7 +119,8 @@ struct bfd_preserve
    the subset.  */
 
 static bfd_boolean
-bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve)
+bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve,
+		   bfd_cleanup cleanup)
 {
   preserve->tdata = abfd->tdata.any;
   preserve->arch_info = abfd->arch_info;
@@ -130,6 +132,7 @@ bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve)
   preserve->section_htab = abfd->section_htab;
   preserve->marker = bfd_alloc (abfd, 1);
   preserve->build_id = abfd->build_id;
+  preserve->cleanup = cleanup;
   if (preserve->marker == NULL)
     return FALSE;
 
@@ -153,7 +156,7 @@ bfd_reinit (bfd *abfd, unsigned int section_id, bfd_cleanup cleanup)
 
 /* Restores bfd state saved by bfd_preserve_save.  */
 
-static void
+static bfd_cleanup
 bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve)
 {
   bfd_hash_table_free (&abfd->section_htab);
@@ -172,6 +175,7 @@ bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve)
      its arg, as well as its arg.  */
   bfd_release (abfd, preserve->marker);
   preserve->marker = NULL;
+  return preserve->cleanup;
 }
 
 /* Called when the bfd state saved by bfd_preserve_save is no longer
@@ -180,6 +184,15 @@ bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve)
 static void
 bfd_preserve_finish (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_preserve *preserve)
 {
+  if (preserve->cleanup)
+    {
+      /* Run the cleanup, assuming that all it will need is the
+	 tdata at the time the cleanup was returned.  */
+      void *tdata = abfd->tdata.any;
+      abfd->tdata.any = preserve->tdata;
+      preserve->cleanup (abfd);
+      abfd->tdata.any = tdata;
+    }
   /* It would be nice to be able to free more memory here, eg. old
      tdata, but that's not possible since these blocks are sitting
      inside bfd_alloc'd memory.  The section hash is on a separate
@@ -252,7 +265,7 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
   save_targ = abfd->xvec;
 
   preserve_match.marker = NULL;
-  if (!bfd_preserve_save (abfd, &preserve))
+  if (!bfd_preserve_save (abfd, &preserve, NULL))
     goto err_ret;
 
   /* If the target type was explicitly specified, just check that target.  */
@@ -381,8 +394,9 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
 	  if (preserve_match.marker == NULL)
 	    {
 	      match_targ = abfd->xvec;
-	      if (!bfd_preserve_save (abfd, &preserve_match))
+	      if (!bfd_preserve_save (abfd, &preserve_match, cleanup))
 		goto err_ret;
+	      cleanup = NULL;
 	    }
 	}
     }
@@ -455,7 +469,7 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
      whole bfd and restoring it would be even worse; the first thing
      you notice is that the cached bfd file position gets out of sync.  */
   if (preserve_match.marker != NULL)
-    bfd_preserve_restore (abfd, &preserve_match);
+    cleanup = bfd_preserve_restore (abfd, &preserve_match);
 
   if (match_count == 1)
     {

-- 
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).