sec->alloced and freeing section contents

Alan Modra amodra@gmail.com
Fri Jan 17 09:36:21 GMT 2025


This modifies _bfd_elf_free_cached_info to unmap/free section
contents.  To do that we need to *not* free sections where contents
are bfd_alloc'd or point to constant strings or somesuch.  I've chosen
to implement this be adding another flag to struct bfd_section,
"alloced" to say the section contents can't be freed.  Most of the
patch is about setting that flag in many places.

Waiting until after the branch for this one too.

diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h
index 1b82b6bcc99..4aa814abe79 100644
--- a/bfd/bfd-in2.h
+++ b/bfd/bfd-in2.h
@@ -688,9 +688,12 @@ typedef struct bfd_section
   /* Nonzero if this section uses RELA relocations, rather than REL.  */
   unsigned int use_rela_p:1;
 
-  /* Nonzero if this section contents are mmapped, rather than malloced.  */
+  /* Nonzero if section contents are mmapped.  */
   unsigned int mmapped_p:1;
 
+  /* Nonzero if section contents should not be freed.  */
+  unsigned int alloced:1;
+
   /* Bits used by various backends.  The generic code doesn't touch
      these fields.  */
 
@@ -980,8 +983,8 @@ discarded_section (const asection *sec)
   /* linker_mark, linker_has_input, gc_mark, decompress_status,     */ \
      0,           0,                1,       0,                        \
 								       \
-  /* segment_mark, sec_info_type, use_rela_p, mmapped_p,           */  \
-     0,            0,             0,          0,                       \
+  /* segment_mark, sec_info_type, use_rela_p, mmapped_p, alloced,   */ \
+     0,            0,             0,          0,         0,            \
 								       \
   /* sec_flg0, sec_flg1, sec_flg2, sec_flg3, sec_flg4, sec_flg5,    */ \
      0,        0,        0,        0,        0,        0,              \
diff --git a/bfd/coff-arm.c b/bfd/coff-arm.c
index 176101a5e4a..ab5f7b0663f 100644
--- a/bfd/coff-arm.c
+++ b/bfd/coff-arm.c
@@ -1809,6 +1809,7 @@ bfd_arm_allocate_interworking_sections (struct bfd_link_info * info)
 
       s->size = globals->arm_glue_size;
       s->contents = foo;
+      s->alloced = 1;
     }
 
   if (globals->thumb_glue_size != 0)
@@ -1824,6 +1825,7 @@ bfd_arm_allocate_interworking_sections (struct bfd_link_info * info)
 
       s->size = globals->thumb_glue_size;
       s->contents = foo;
+      s->alloced = 1;
     }
 
   return true;
diff --git a/bfd/compress.c b/bfd/compress.c
index 93c3aaed618..b6357506709 100644
--- a/bfd/compress.c
+++ b/bfd/compress.c
@@ -694,6 +694,7 @@ bfd_compress_section_contents (bfd *abfd, sec_ptr sec)
       sec->size = compressed_size;
       sec->compress_status = COMPRESS_SECTION_DONE;
     }
+  sec->alloced = 1;
   sec->contents = buffer;
   sec->flags |= SEC_IN_MEMORY;
   free (input_buffer);
diff --git a/bfd/elf-m10300.c b/bfd/elf-m10300.c
index 409804ecd38..ed399da904c 100644
--- a/bfd/elf-m10300.c
+++ b/bfd/elf-m10300.c
@@ -5034,6 +5034,7 @@ _bfd_mn10300_elf_late_size_sections (bfd * output_bfd,
 	  BFD_ASSERT (s != NULL);
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
   else
@@ -5120,6 +5121,7 @@ _bfd_mn10300_elf_late_size_sections (bfd * output_bfd,
       s->contents = bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
diff --git a/bfd/elf-properties.c b/bfd/elf-properties.c
index 23634a9d9c9..f150ab4899f 100644
--- a/bfd/elf-properties.c
+++ b/bfd/elf-properties.c
@@ -872,6 +872,7 @@ _bfd_elf_link_setup_gnu_properties (struct bfd_link_info *info)
 				align_size);
 
       /* Cache the section contents for elf_link_input_bfd.  */
+      sec->alloced = 1;
       elf_section_data (sec)->this_hdr.contents = contents;
 
       /* If GNU_PROPERTY_NO_COPY_ON_PROTECTED is set, protected data
diff --git a/bfd/elf.c b/bfd/elf.c
index 1aecb4786a2..bce2a18c961 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -3885,6 +3885,7 @@ bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
 	  *failedptr = true;
 	  return;
 	}
+      sec->alloced = 1;
     }
 
   loc = sec->contents + sec->size;
@@ -10125,6 +10126,15 @@ _bfd_elf_free_cached_info (bfd *abfd)
       _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
       _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
       _bfd_stab_cleanup (abfd, &tdata->line_info);
+      for (asection *sec = abfd->sections; sec != NULL; sec = sec->next)
+	{
+	  _bfd_elf_munmap_section_contents (sec, sec->contents);
+	  if (!sec->alloced)
+	    {
+	      free (elf_section_data (sec)->this_hdr.contents);
+	      elf_section_data (sec)->this_hdr.contents = NULL;
+	    }
+	}
       free (tdata->symtab_hdr.contents);
       tdata->symtab_hdr.contents = NULL;
     }
@@ -14090,6 +14100,7 @@ _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
 	  hdr->contents = bfd_alloc (abfd, hdr->sh_size);
 	  if (hdr->contents == NULL)
 	    continue;
+	  relsec->alloced = 1;
 
 #if DEBUG_SECONDARY_RELOCS
 	  fprintf (stderr, "write %u secondary relocs for %s from %s\n",
@@ -14235,6 +14246,7 @@ elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
 	}
     }
 #endif
+  /* FIXME: We should not get here if sec->alloced is set.  */
   bool ret = bfd_get_full_section_contents (abfd, sec, buf);
   if (ret && sec->mmapped_p)
     *buf = sec->contents;
@@ -14262,14 +14274,23 @@ _bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec,
 /* Munmap section contents.  */
 
 void
-_bfd_elf_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED,
-				  void *contents)
+_bfd_elf_munmap_section_contents (asection *sec, void *contents)
 {
   /* NB: Since _bfd_elf_munmap_section_contents is called like free,
      CONTENTS may be NULL.  */
   if (contents == NULL)
     return;
 
+  if (sec->alloced
+      /* What a tangled web we weave with section contents.
+	 FIXME: We shouldn't need to test anything but sec->alloced
+	 here, but there are cases where a buffer is allocated for a
+	 section but then another buffer is malloc'd anyway.  eg.
+	 trace through ld-elf/eh4 testcase on x86_64.  */
+      && (sec->contents == contents
+	  || elf_section_data (sec)->this_hdr.contents == contents))
+    return;
+
   /* Don't leave pointers to data we are about to munmap or free.  */
   if (sec->contents == contents)
     sec->contents = NULL;
@@ -14314,6 +14335,7 @@ _bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED)
 	abort ();
       sec->mmapped_p = 0;
       sec->contents = NULL;
+      elf_section_data (sec)->this_hdr.contents = NULL;
       elf_section_data (sec)->contents_addr = NULL;
       elf_section_data (sec)->contents_size = 0;
     }
diff --git a/bfd/elf32-arc.c b/bfd/elf32-arc.c
index 71de480a4f4..0a6e66569ba 100644
--- a/bfd/elf32-arc.c
+++ b/bfd/elf32-arc.c
@@ -2735,6 +2735,7 @@ elf_arc_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	  BFD_ASSERT (s != NULL);
 	  s->size = sizeof (ELF_DYNAMIC_INTERPRETER);
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
 
       /* Add some entries to the .dynamic section.  We fill in some of
@@ -2797,6 +2798,7 @@ elf_arc_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
       s->contents = bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs_exist);
diff --git a/bfd/elf32-arm.c b/bfd/elf32-arm.c
index ba60634997e..8865befc06c 100644
--- a/bfd/elf32-arm.c
+++ b/bfd/elf32-arm.c
@@ -7073,6 +7073,7 @@ elf32_arm_build_stubs (struct bfd_link_info *info)
       stub_sec->contents = (unsigned char *) bfd_zalloc (htab->stub_bfd, size);
       if (stub_sec->contents == NULL && size != 0)
 	return false;
+      stub_sec->alloced = 1;
 
       stub_sec->size = 0;
     }
@@ -7279,6 +7280,7 @@ arm_allocate_glue_section_space (bfd * abfd, bfd_size_type size, const char * na
 
   BFD_ASSERT (s->size == size);
   s->contents = contents;
+  s->alloced = 1;
 }
 
 bool
@@ -16823,6 +16825,7 @@ elf32_arm_late_size_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
 	  BFD_ASSERT (s != NULL);
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
 
@@ -17173,6 +17176,7 @@ elf32_arm_late_size_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
       s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   return _bfd_elf_maybe_vxworks_add_dynamic_tags (output_bfd, info,
diff --git a/bfd/elf32-avr.c b/bfd/elf32-avr.c
index b0826bde03a..0c1c07426c6 100644
--- a/bfd/elf32-avr.c
+++ b/bfd/elf32-avr.c
@@ -3893,6 +3893,7 @@ elf32_avr_build_stubs (struct bfd_link_info *info)
       stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
       if (stub_sec->contents == NULL && size != 0)
 	return false;
+      stub_sec->alloced = 1;
       stub_sec->size = 0;
     }
 
diff --git a/bfd/elf32-bfin.c b/bfd/elf32-bfin.c
index c7d03cfae46..547661a3098 100644
--- a/bfd/elf32-bfin.c
+++ b/bfd/elf32-bfin.c
@@ -3928,6 +3928,7 @@ _bfinfdpic_size_got_plt (bfd *output_bfd,
 				 bfinfdpic_got_section (info)->size);
       if (bfinfdpic_got_section (info)->contents == NULL)
 	return false;
+      bfinfdpic_got_section (info)->alloced = 1;
     }
 
   if (elf_hash_table (info)->dynamic_sections_created)
@@ -3947,6 +3948,7 @@ _bfinfdpic_size_got_plt (bfd *output_bfd,
 				 bfinfdpic_gotrel_section (info)->size);
       if (bfinfdpic_gotrel_section (info)->contents == NULL)
 	return false;
+      bfinfdpic_gotrel_section (info)->alloced = 1;
     }
 
   bfinfdpic_gotfixup_section (info)->size = (gpinfop->g.fixups + 1) * 4;
@@ -3959,6 +3961,7 @@ _bfinfdpic_size_got_plt (bfd *output_bfd,
 				 bfinfdpic_gotfixup_section (info)->size);
       if (bfinfdpic_gotfixup_section (info)->contents == NULL)
 	return false;
+      bfinfdpic_gotfixup_section (info)->alloced = 1;
     }
 
   if (elf_hash_table (info)->dynamic_sections_created)
@@ -3973,6 +3976,7 @@ _bfinfdpic_size_got_plt (bfd *output_bfd,
 				 bfinfdpic_pltrel_section (info)->size);
       if (bfinfdpic_pltrel_section (info)->contents == NULL)
 	return false;
+      bfinfdpic_pltrel_section (info)->alloced = 1;
     }
 
   /* Add 4 bytes for every block of at most 65535 lazy PLT entries,
@@ -4018,6 +4022,7 @@ _bfinfdpic_size_got_plt (bfd *output_bfd,
 				 bfinfdpic_plt_section (info)->size);
       if (bfinfdpic_plt_section (info)->contents == NULL)
 	return false;
+      bfinfdpic_plt_section (info)->alloced = 1;
     }
 
   return true;
@@ -4048,6 +4053,7 @@ elf32_bfinfdpic_late_size_sections (bfd *output_bfd,
 	  BFD_ASSERT (s != NULL);
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
 
@@ -5142,6 +5148,7 @@ bfin_late_size_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
 	  BFD_ASSERT (s != NULL);
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
   else
@@ -5229,6 +5236,7 @@ bfin_late_size_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL && s->size != 0)
 	return false;
+      s->alloced = 1;
     }
 
   if (elf_hash_table (info)->dynamic_sections_created)
@@ -5309,6 +5317,7 @@ bfd_bfin_elf32_create_embedded_relocs (bfd *abfd,
   relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
   if (relsec->contents == NULL)
     goto error_return;
+  relsec->alloced = 1;
 
   p = relsec->contents;
 
diff --git a/bfd/elf32-cr16.c b/bfd/elf32-cr16.c
index de3d5d0ee75..68ac71f69e5 100644
--- a/bfd/elf32-cr16.c
+++ b/bfd/elf32-cr16.c
@@ -2411,6 +2411,7 @@ _bfd_cr16_elf_late_size_sections (bfd * output_bfd,
 	  BFD_ASSERT (s != NULL);
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 #endif
 	}
     }
@@ -2491,6 +2492,7 @@ _bfd_cr16_elf_late_size_sections (bfd * output_bfd,
       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
@@ -2699,6 +2701,7 @@ bfd_cr16_elf32_create_embedded_relocs (bfd *abfd,
   relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
   if (relsec->contents == NULL)
     goto error_return;
+  relsec->alloced = 1;
 
   p = relsec->contents;
 
diff --git a/bfd/elf32-cris.c b/bfd/elf32-cris.c
index 91178fa7980..306d3e792b1 100644
--- a/bfd/elf32-cris.c
+++ b/bfd/elf32-cris.c
@@ -3530,6 +3530,7 @@ elf_cris_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	  BFD_ASSERT (s != NULL);
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
   else
@@ -3644,6 +3645,7 @@ elf_cris_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
diff --git a/bfd/elf32-csky.c b/bfd/elf32-csky.c
index 5cb45e1a426..dbb603b88bc 100644
--- a/bfd/elf32-csky.c
+++ b/bfd/elf32-csky.c
@@ -1916,6 +1916,7 @@ csky_elf_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	  BFD_ASSERT (s != NULL);
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
 
@@ -2084,6 +2085,7 @@ csky_elf_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   if (htab->elf.dynamic_sections_created)
@@ -3854,6 +3856,7 @@ elf32_csky_build_stubs (struct bfd_link_info *info)
       stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
       if (stub_sec->contents == NULL && size != 0)
 	return false;
+      stub_sec->alloced = 1;
       stub_sec->size = 0;
     }
 
diff --git a/bfd/elf32-frv.c b/bfd/elf32-frv.c
index 142b5a201b3..a7e8215ff5e 100644
--- a/bfd/elf32-frv.c
+++ b/bfd/elf32-frv.c
@@ -5313,6 +5313,7 @@ _frvfdpic_size_got_plt (bfd *output_bfd,
 				 frvfdpic_got_section (info)->size);
       if (frvfdpic_got_section (info)->contents == NULL)
 	return false;
+      frvfdpic_got_section (info)->alloced = 1;
     }
 
   if (frvfdpic_gotrel_section (info))
@@ -5332,6 +5333,7 @@ _frvfdpic_size_got_plt (bfd *output_bfd,
 				 frvfdpic_gotrel_section (info)->size);
       if (frvfdpic_gotrel_section (info)->contents == NULL)
 	return false;
+      frvfdpic_gotrel_section (info)->alloced = 1;
     }
 
   frvfdpic_gotfixup_section (info)->size = (gpinfop->g.fixups + 1) * 4;
@@ -5344,6 +5346,7 @@ _frvfdpic_size_got_plt (bfd *output_bfd,
 				 frvfdpic_gotfixup_section (info)->size);
       if (frvfdpic_gotfixup_section (info)->contents == NULL)
 	return false;
+      frvfdpic_gotfixup_section (info)->alloced = 1;
     }
 
   if (frvfdpic_pltrel_section (info))
@@ -5360,6 +5363,7 @@ _frvfdpic_size_got_plt (bfd *output_bfd,
 				     frvfdpic_pltrel_section (info)->size);
 	  if (frvfdpic_pltrel_section (info)->contents == NULL)
 	    return false;
+	  frvfdpic_pltrel_section (info)->alloced = 1;
 	}
     }
 
@@ -5413,6 +5417,7 @@ _frvfdpic_size_got_plt (bfd *output_bfd,
 				     frvfdpic_plt_section (info)->size);
 	  if (frvfdpic_plt_section (info)->contents == NULL)
 	    return false;
+	  frvfdpic_plt_section (info)->alloced = 1;
 	}
     }
 
@@ -5442,6 +5447,7 @@ elf32_frvfdpic_late_size_sections (bfd *output_bfd,
 	  BFD_ASSERT (s != NULL);
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
 
diff --git a/bfd/elf32-hppa.c b/bfd/elf32-hppa.c
index af11e0d15f0..ba20bc63e5a 100644
--- a/bfd/elf32-hppa.c
+++ b/bfd/elf32-hppa.c
@@ -2068,6 +2068,7 @@ elf32_hppa_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	    abort ();
 	  sec->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  sec->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  sec->alloced = 1;
 	}
 
       /* Force millicode symbols local.  */
@@ -2272,6 +2273,7 @@ elf32_hppa_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
       sec->contents = bfd_zalloc (dynobj, sec->size);
       if (sec->contents == NULL)
 	return false;
+      sec->alloced = 1;
     }
 
   return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
@@ -2995,6 +2997,7 @@ elf32_hppa_build_stubs (struct bfd_link_info *info)
 	stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
 	if (stub_sec->contents == NULL)
 	  return false;
+	stub_sec->alloced = 1;
 	stub_sec->size = 0;
       }
 
diff --git a/bfd/elf32-lm32.c b/bfd/elf32-lm32.c
index ee3d9be19be..45264d69c67 100644
--- a/bfd/elf32-lm32.c
+++ b/bfd/elf32-lm32.c
@@ -1929,6 +1929,7 @@ lm32_elf_late_size_sections (bfd *output_bfd,
 	  BFD_ASSERT (s != NULL);
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
 
@@ -2054,6 +2055,7 @@ lm32_elf_late_size_sections (bfd *output_bfd,
       s->contents = bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   if (!_bfd_elf_add_dynamic_tags (output_bfd, info, relocs))
@@ -2182,6 +2184,7 @@ lm32_elf_late_size_sections (bfd *output_bfd,
 	     bfd_zalloc (dynobj, lm32fdpic_fixup32_section (info)->size);
 	  if (lm32fdpic_fixup32_section (info)->contents == NULL)
 	    return false;
+	  lm32fdpic_fixup32_section (info)->alloced = 1;
 	}
     }
 
diff --git a/bfd/elf32-m32c.c b/bfd/elf32-m32c.c
index c72ae77f73a..890a0ac59a6 100644
--- a/bfd/elf32-m32c.c
+++ b/bfd/elf32-m32c.c
@@ -792,6 +792,7 @@ m32c_elf_early_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
   splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size);
   if (splt->contents == NULL)
     return false;
+  splt->alloced = 1;
 
   return true;
 }
diff --git a/bfd/elf32-m32r.c b/bfd/elf32-m32r.c
index 3bae1dd76d1..022b0ac30ac 100644
--- a/bfd/elf32-m32r.c
+++ b/bfd/elf32-m32r.c
@@ -1987,6 +1987,7 @@ m32r_elf_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	  BFD_ASSERT (s != NULL);
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
 
@@ -2112,6 +2113,7 @@ m32r_elf_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
       s->contents = bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
diff --git a/bfd/elf32-m68hc1x.c b/bfd/elf32-m68hc1x.c
index 86badee9f6c..7929d55cb88 100644
--- a/bfd/elf32-m68hc1x.c
+++ b/bfd/elf32-m68hc1x.c
@@ -658,6 +658,7 @@ elf32_m68hc11_build_stubs (bfd *abfd, struct bfd_link_info *info)
       stub_sec->contents = (unsigned char *) bfd_zalloc (htab->stub_bfd, size);
       if (stub_sec->contents == NULL && size != 0)
 	return false;
+      stub_sec->alloced = 1;
       stub_sec->size = 0;
     }
 
diff --git a/bfd/elf32-m68k.c b/bfd/elf32-m68k.c
index 6ecb34ecb67..d7387d938e8 100644
--- a/bfd/elf32-m68k.c
+++ b/bfd/elf32-m68k.c
@@ -3126,6 +3126,7 @@ elf_m68k_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	  BFD_ASSERT (s != NULL);
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
   else
@@ -3216,6 +3217,7 @@ elf_m68k_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
@@ -4400,6 +4402,7 @@ bfd_m68k_elf32_create_embedded_relocs (bfd *abfd, struct bfd_link_info *info,
   relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
   if (relsec->contents == NULL)
     goto error_return;
+  relsec->alloced = 1;
 
   p = relsec->contents;
 
diff --git a/bfd/elf32-metag.c b/bfd/elf32-metag.c
index 93069948fc0..479b9f4405d 100644
--- a/bfd/elf32-metag.c
+++ b/bfd/elf32-metag.c
@@ -2740,6 +2740,7 @@ elf_metag_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	    abort ();
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
 
@@ -2884,7 +2885,8 @@ elf_metag_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
       s->contents = bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
-      else if (reloc_section)
+      s->alloced = 1;
+      if (reloc_section)
 	{
 	  unsigned char *contents = s->contents;
 	  Elf32_External_Rela reloc;
@@ -3964,6 +3966,7 @@ elf_metag_build_stubs (struct bfd_link_info *info)
       stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
       if (stub_sec->contents == NULL && size != 0)
 	return false;
+      stub_sec->alloced = 1;
       stub_sec->size = 0;
     }
 
diff --git a/bfd/elf32-microblaze.c b/bfd/elf32-microblaze.c
index 4d36577b0fd..fb86b3e4bcf 100644
--- a/bfd/elf32-microblaze.c
+++ b/bfd/elf32-microblaze.c
@@ -3144,6 +3144,7 @@ microblaze_elf_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL && s->size != 0)
 	return false;
+      s->alloced = 1;
     }
 
   /* ??? Force DF_BIND_NOW?  */
diff --git a/bfd/elf32-nds32.c b/bfd/elf32-nds32.c
index 6b8d925479a..9db95e87ec6 100644
--- a/bfd/elf32-nds32.c
+++ b/bfd/elf32-nds32.c
@@ -4326,6 +4326,7 @@ nds32_elf_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	  BFD_ASSERT (s != NULL);
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
 
@@ -4525,6 +4526,7 @@ nds32_elf_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
diff --git a/bfd/elf32-or1k.c b/bfd/elf32-or1k.c
index 2374f1184f1..bdee1f1fb99 100644
--- a/bfd/elf32-or1k.c
+++ b/bfd/elf32-or1k.c
@@ -3069,6 +3069,7 @@ or1k_elf_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	  BFD_ASSERT (s != NULL);
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
 
@@ -3204,9 +3205,9 @@ or1k_elf_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	 but this way if it does, we get a R_OR1K_NONE reloc instead
 	 of garbage.  */
       s->contents = bfd_zalloc (dynobj, s->size);
-
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
diff --git a/bfd/elf32-ppc.c b/bfd/elf32-ppc.c
index 2ea1574a026..25cb31b8f3b 100644
--- a/bfd/elf32-ppc.c
+++ b/bfd/elf32-ppc.c
@@ -5502,6 +5502,7 @@ ppc_elf_late_size_sections (bfd *output_bfd,
 	  BFD_ASSERT (s != NULL);
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
 
@@ -5881,6 +5882,7 @@ ppc_elf_late_size_sections (bfd *output_bfd,
       s->contents = bfd_zalloc (htab->elf.dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   if (htab->elf.dynamic_sections_created)
diff --git a/bfd/elf32-rl78.c b/bfd/elf32-rl78.c
index ff3ca2e56c7..c0b8850cd93 100644
--- a/bfd/elf32-rl78.c
+++ b/bfd/elf32-rl78.c
@@ -1459,6 +1459,7 @@ rl78_elf_early_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
   splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size);
   if (splt->contents == NULL)
     return false;
+  splt->alloced = 1;
 
   return true;
 }
diff --git a/bfd/elf32-s390.c b/bfd/elf32-s390.c
index bc5e7cd5465..470335ca602 100644
--- a/bfd/elf32-s390.c
+++ b/bfd/elf32-s390.c
@@ -1798,6 +1798,7 @@ elf_s390_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	    abort ();
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
 
@@ -1957,6 +1958,7 @@ elf_s390_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
diff --git a/bfd/elf32-score.c b/bfd/elf32-score.c
index 9563098dc77..63a185442de 100644
--- a/bfd/elf32-score.c
+++ b/bfd/elf32-score.c
@@ -3246,6 +3246,7 @@ s3_bfd_score_elf_late_size_sections (bfd *output_bfd, struct bfd_link_info *info
 	  BFD_ASSERT (s != NULL);
 	  s->size = strlen (ELF_DYNAMIC_INTERPRETER) + 1;
 	  s->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
 
@@ -3328,6 +3329,7 @@ s3_bfd_score_elf_late_size_sections (bfd *output_bfd, struct bfd_link_info *info
 	  bfd_set_error (bfd_error_no_memory);
 	  return false;
 	}
+      s->alloced = 1;
     }
 
   if (elf_hash_table (info)->dynamic_sections_created)
diff --git a/bfd/elf32-score7.c b/bfd/elf32-score7.c
index 2ecabf6a47b..448215550ce 100644
--- a/bfd/elf32-score7.c
+++ b/bfd/elf32-score7.c
@@ -3056,6 +3056,7 @@ s7_bfd_score_elf_late_size_sections (bfd *output_bfd, struct bfd_link_info *info
 	  BFD_ASSERT (s != NULL);
 	  s->size = strlen (ELF_DYNAMIC_INTERPRETER) + 1;
 	  s->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
 
@@ -3138,6 +3139,7 @@ s7_bfd_score_elf_late_size_sections (bfd *output_bfd, struct bfd_link_info *info
 	  bfd_set_error (bfd_error_no_memory);
 	  return false;
 	}
+      s->alloced = 1;
     }
 
   if (elf_hash_table (info)->dynamic_sections_created)
diff --git a/bfd/elf32-sh.c b/bfd/elf32-sh.c
index 11491de419f..39a837ce3d8 100644
--- a/bfd/elf32-sh.c
+++ b/bfd/elf32-sh.c
@@ -2966,6 +2966,7 @@ sh_elf_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	  BFD_ASSERT (s != NULL);
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
 
@@ -3193,6 +3194,7 @@ sh_elf_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   return _bfd_elf_maybe_vxworks_add_dynamic_tags (output_bfd, info,
diff --git a/bfd/elf32-spu.c b/bfd/elf32-spu.c
index da54bf7019e..a6b486736ae 100644
--- a/bfd/elf32-spu.c
+++ b/bfd/elf32-spu.c
@@ -610,6 +610,7 @@ spu_elf_create_sections (struct bfd_link_info *info)
       memcpy (data + 12 + ((sizeof (SPU_PLUGIN_NAME) + 3) & -4),
 	      bfd_get_filename (info->output_bfd), name_len);
       s->contents = data;
+      s->alloced = 1;
     }
 
   if (htab->params->emit_fixups)
@@ -1965,6 +1966,7 @@ spu_elf_build_stubs (struct bfd_link_info *info)
 						      htab->stub_sec[i]->size);
 	    if (htab->stub_sec[i]->contents == NULL)
 	      return false;
+	    htab->stub_sec[i]->alloced = 1;
 	    htab->stub_sec[i]->rawsize = htab->stub_sec[i]->size;
 	    htab->stub_sec[i]->size = 0;
 	  }
@@ -1999,6 +2001,7 @@ spu_elf_build_stubs (struct bfd_link_info *info)
   htab->ovtab->contents = bfd_zalloc (htab->ovtab->owner, htab->ovtab->size);
   if (htab->ovtab->contents == NULL)
     return false;
+  htab->ovtab->alloced = 1;
 
   p = htab->ovtab->contents;
   if (htab->params->ovly_flavour == ovly_soft_icache)
@@ -2100,6 +2103,7 @@ spu_elf_build_stubs (struct bfd_link_info *info)
 					     htab->init->size);
 	  if (htab->init->contents == NULL)
 	    return false;
+	  htab->init->alloced = 1;
 
 	  h = define_ovtab_symbol (htab, "__icache_fileoff");
 	  if (h == NULL)
@@ -5502,6 +5506,7 @@ spu_elf_size_sections (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
       sfixup->contents = (bfd_byte *) bfd_zalloc (info->input_bfds, size);
       if (sfixup->contents == NULL)
 	return false;
+      sfixup->alloced = 1;
     }
   return true;
 }
diff --git a/bfd/elf32-tic6x.c b/bfd/elf32-tic6x.c
index 8a444f64453..0adab1acc07 100644
--- a/bfd/elf32-tic6x.c
+++ b/bfd/elf32-tic6x.c
@@ -3175,6 +3175,7 @@ elf32_tic6x_late_size_sections (bfd *output_bfd, struct bfd_link_info *info)
 	    abort ();
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
 
@@ -3319,6 +3320,7 @@ elf32_tic6x_late_size_sections (bfd *output_bfd, struct bfd_link_info *info)
       s->contents = bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   if (htab->elf.dynamic_sections_created)
diff --git a/bfd/elf32-tilepro.c b/bfd/elf32-tilepro.c
index e4ecb225dff..6f901725524 100644
--- a/bfd/elf32-tilepro.c
+++ b/bfd/elf32-tilepro.c
@@ -2203,6 +2203,7 @@ tilepro_elf_late_size_sections (bfd *output_bfd,
 	  BFD_ASSERT (s != NULL);
 	  s->size = sizeof ELF32_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF32_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
 
@@ -2368,6 +2369,7 @@ tilepro_elf_late_size_sections (bfd *output_bfd,
       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   return _bfd_elf_add_dynamic_tags (output_bfd, info, true);
diff --git a/bfd/elf32-v850.c b/bfd/elf32-v850.c
index 0599c11b028..0c523892164 100644
--- a/bfd/elf32-v850.c
+++ b/bfd/elf32-v850.c
@@ -2392,6 +2392,7 @@ v850_elf_make_note_section (bfd * abfd)
     return NULL;
 
   s->contents = data;
+  s->alloced = 1;
 
   /* Provide default (= uninitilaised) values for all of the notes.  */
   for (id = V850_NOTE_ALIGNMENT; id <= NUM_V850_NOTES; id++)
diff --git a/bfd/elf32-vax.c b/bfd/elf32-vax.c
index 3f63fe2b201..9d1fee86797 100644
--- a/bfd/elf32-vax.c
+++ b/bfd/elf32-vax.c
@@ -1041,6 +1041,7 @@ elf_vax_late_size_sections (bfd *output_bfd, struct bfd_link_info *info)
 	  BFD_ASSERT (s != NULL);
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
 
@@ -1121,6 +1122,7 @@ elf_vax_late_size_sections (bfd *output_bfd, struct bfd_link_info *info)
       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
diff --git a/bfd/elf32-xstormy16.c b/bfd/elf32-xstormy16.c
index c814c5a83d8..3fe619d5869 100644
--- a/bfd/elf32-xstormy16.c
+++ b/bfd/elf32-xstormy16.c
@@ -725,6 +725,7 @@ xstormy16_elf_early_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
   splt->contents = bfd_zalloc (dynobj, splt->size);
   if (splt->contents == NULL)
     return false;
+  splt->alloced = 1;
 
   return true;
 }
diff --git a/bfd/elf32-xtensa.c b/bfd/elf32-xtensa.c
index e21b00cd5a1..f9c006c345c 100644
--- a/bfd/elf32-xtensa.c
+++ b/bfd/elf32-xtensa.c
@@ -1593,6 +1593,7 @@ elf_xtensa_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	    abort ();
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
 
       /* Allocate room for one word in ".got".  */
@@ -1731,6 +1732,7 @@ elf_xtensa_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	  s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
 	  if (s->contents == NULL)
 	    return false;
+	  s->alloced = 1;
 	}
     }
 
diff --git a/bfd/elf64-alpha.c b/bfd/elf64-alpha.c
index 2be63df8b45..b3570cebf5a 100644
--- a/bfd/elf64-alpha.c
+++ b/bfd/elf64-alpha.c
@@ -2586,6 +2586,7 @@ elf64_alpha_early_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	  s->contents = (bfd_byte *) bfd_zalloc (i, s->size);
 	  if (s->contents == NULL)
 	    return false;
+	  s->alloced = 1;
 	}
     }
 
@@ -2812,6 +2813,7 @@ elf64_alpha_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	  BFD_ASSERT (s != NULL);
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
 
       /* Now that we've seen all of the input files, we can decide which
@@ -2882,6 +2884,7 @@ elf64_alpha_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	  s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
 	  if (s->contents == NULL)
 	    return false;
+	  s->alloced = 1;
 	}
     }
 
diff --git a/bfd/elf64-hppa.c b/bfd/elf64-hppa.c
index 85406dc7d98..3f5a3fea4ab 100644
--- a/bfd/elf64-hppa.c
+++ b/bfd/elf64-hppa.c
@@ -1559,6 +1559,7 @@ elf64_hppa_late_size_sections (bfd *output_bfd, struct bfd_link_info *info)
 	  BFD_ASSERT (sec != NULL);
 	  sec->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  sec->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  sec->alloced = 1;
 	}
     }
   else
@@ -1809,6 +1810,7 @@ elf64_hppa_late_size_sections (bfd *output_bfd, struct bfd_link_info *info)
 	  sec->contents = (bfd_byte *) bfd_zalloc (dynobj, sec->size);
 	  if (sec->contents == NULL)
 	    return false;
+	  sec->alloced = 1;
 	}
     }
 
diff --git a/bfd/elf64-ia64-vms.c b/bfd/elf64-ia64-vms.c
index d7165e371f0..26710bac97e 100644
--- a/bfd/elf64-ia64-vms.c
+++ b/bfd/elf64-ia64-vms.c
@@ -1248,6 +1248,7 @@ create_ia64_vms_notes (bfd *abfd, struct bfd_link_info *info,
     }
 
   ia64_info->note_sec->contents = note_contents;
+  ia64_info->note_sec->alloced = 1;
   ia64_info->note_sec->size = note_size;
 
   free (module_name);
@@ -2761,6 +2762,7 @@ elf64_ia64_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	  sec->contents = (bfd_byte *) bfd_zalloc (dynobj, sec->size);
 	  if (sec->contents == NULL && sec->size != 0)
 	    return false;
+	  sec->alloced = 1;
 	}
     }
 
diff --git a/bfd/elf64-mmix.c b/bfd/elf64-mmix.c
index 5bb6feee16e..1b5a65d8479 100644
--- a/bfd/elf64-mmix.c
+++ b/bfd/elf64-mmix.c
@@ -2385,6 +2385,7 @@ _bfd_mmix_after_linker_allocation (bfd *abfd ATTRIBUTE_UNUSED,
     = contents = bfd_alloc (bpo_greg_owner, bpo_gregs_section->size);
   if (contents == NULL)
     return false;
+  bpo_gregs_section->alloced = 1;
 
   /* Sanity check: If these numbers mismatch, some relocation has not been
      accounted for and the rest of gregdata is probably inconsistent.
diff --git a/bfd/elf64-ppc.c b/bfd/elf64-ppc.c
index 44861a05852..fa28a536378 100644
--- a/bfd/elf64-ppc.c
+++ b/bfd/elf64-ppc.c
@@ -6178,6 +6178,7 @@ sfpr_define (struct bfd_link_info *info,
 		    = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
 		  if (htab->sfpr->contents == NULL)
 		    return false;
+		  htab->sfpr->alloced = 1;
 		}
 	    }
 	}
@@ -10262,6 +10263,7 @@ ppc64_elf_late_size_sections (bfd *output_bfd,
 	    abort ();
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
 
@@ -10547,6 +10549,7 @@ ppc64_elf_late_size_sections (bfd *output_bfd,
       s->contents = bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
@@ -10564,6 +10567,7 @@ ppc64_elf_late_size_sections (bfd *output_bfd,
 	      s->contents = bfd_zalloc (ibfd, s->size);
 	      if (s->contents == NULL)
 		return false;
+	      s->alloced = 1;
 	    }
 	}
       s = ppc64_elf_tdata (ibfd)->relgot;
@@ -10576,6 +10580,7 @@ ppc64_elf_late_size_sections (bfd *output_bfd,
 	      s->contents = bfd_zalloc (ibfd, s->size);
 	      if (s->contents == NULL)
 		return false;
+	      s->alloced = 1;
 	      relocs = true;
 	      s->reloc_count = 0;
 	    }
@@ -14416,6 +14421,7 @@ ppc64_elf_size_stubs (struct bfd_link_info *info)
       if (p == NULL)
 	return false;
       htab->glink_eh_frame->contents = p;
+      htab->glink_eh_frame->alloced = 1;
       last_fde = p;
       align = 4;
 
@@ -14998,6 +15004,7 @@ ppc64_elf_build_stubs (struct bfd_link_info *info,
 					   stub_sec->size);
 	  if (stub_sec->contents == NULL)
 	    return false;
+	  stub_sec->alloced = 1;
 	  stub_sec->size = 0;
 	}
     }
@@ -15195,6 +15202,7 @@ ppc64_elf_build_stubs (struct bfd_link_info *info,
 					 htab->brlt->size);
       if (htab->brlt->contents == NULL)
 	return false;
+      htab->brlt->alloced = 1;
     }
   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
     {
@@ -15202,6 +15210,7 @@ ppc64_elf_build_stubs (struct bfd_link_info *info,
 					    htab->relbrlt->size);
       if (htab->relbrlt->contents == NULL)
 	return false;
+      htab->relbrlt->alloced = 1;
     }
 
   /* Build the stubs as directed by the stub hash table.  */
@@ -15294,6 +15303,7 @@ ppc64_elf_build_stubs (struct bfd_link_info *info,
 	= bfd_alloc (htab->elf.dynobj, htab->elf.srelrdyn->size);
       if (htab->elf.srelrdyn->contents == NULL)
 	return false;
+      htab->elf.srelrdyn->alloced = 1;
 
       bfd_vma *relr_addr = sort_relr (htab);
       if (htab->relr_count != 0 && relr_addr == NULL)
diff --git a/bfd/elf64-s390.c b/bfd/elf64-s390.c
index 422d89acb87..27836b87d6f 100644
--- a/bfd/elf64-s390.c
+++ b/bfd/elf64-s390.c
@@ -1739,6 +1739,7 @@ elf_s390_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	    abort ();
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
 
@@ -1926,6 +1927,7 @@ elf_s390_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
diff --git a/bfd/elflink.c b/bfd/elflink.c
index 91c77c211ef..d4e890df734 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -7044,6 +7044,7 @@ bfd_elf_size_dynamic_sections (bfd *output_bfd,
 	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
 	  if (s->contents == NULL && s->size != 0)
 	    return false;
+	  s->alloced = 1;
 
 	  /* Fill in the version definition section.  */
 
@@ -7287,6 +7288,7 @@ bfd_elf_size_dynamic_sections (bfd *output_bfd,
 	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
 	  if (s->contents == NULL)
 	    return false;
+	  s->alloced = 1;
 
 	  p = s->contents;
 	  for (vn = elf_tdata (output_bfd)->verref;
@@ -7824,6 +7826,7 @@ bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
 	  if (s->contents == NULL)
 	    return false;
+	  s->alloced = 1;
 
 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
 	    return false;
@@ -7842,6 +7845,7 @@ bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
       s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
 
       /* The first entry in .dynsym is a dummy symbol.  Clear all the
 	 section syms, in case we don't output them all.  */
@@ -7897,6 +7901,7 @@ bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
 	  if (s->contents == NULL)
 	    return false;
+	  s->alloced = 1;
 
 	  bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
 	  bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
@@ -7957,6 +7962,7 @@ bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
 	      if (contents == NULL)
 		return false;
 	      s->contents = contents;
+	      s->alloced = 1;
 	      /* 1 empty bucket.  */
 	      bfd_put_32 (output_bfd, 1, contents);
 	      /* SYMIDX above the special symbol 0.  */
@@ -8040,6 +8046,7 @@ bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
 		}
 
 	      s->contents = contents;
+	      s->alloced = 1;
 	      bfd_put_32 (output_bfd, bucketcount, contents);
 	      bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
 	      bfd_put_32 (output_bfd, maskwords, contents + 8);
@@ -8376,7 +8383,10 @@ _bfd_elf_link_hash_table_free (bfd *obfd)
   _bfd_merge_sections_free (htab->merge_info);
   /* NB: htab->dynamic->contents is always allocated by bfd_realloc.  */
   if (htab->dynamic != NULL)
-    free (htab->dynamic->contents);
+    {
+      free (htab->dynamic->contents);
+      htab->dynamic->contents = NULL;
+    }
   if (htab->first_hash != NULL)
     {
       bfd_hash_table_free (htab->first_hash);
diff --git a/bfd/elfnn-aarch64.c b/bfd/elfnn-aarch64.c
index f327b7a8a36..1fe0b615800 100644
--- a/bfd/elfnn-aarch64.c
+++ b/bfd/elfnn-aarch64.c
@@ -4850,6 +4850,7 @@ elfNN_aarch64_build_stubs (struct bfd_link_info *info)
       stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
       if (stub_sec->contents == NULL && size != 0)
 	return false;
+      stub_sec->alloced = 1;
       stub_sec->size = 0;
 
       /* Add a branch around the stub section, and a nop, to keep it 8 byte
@@ -9406,6 +9407,7 @@ elfNN_aarch64_finish_relative_relocs (struct bfd_link_info *info)
   srelrdyn->contents = bfd_alloc (dynobj, srelrdyn->size);
   if (srelrdyn->contents == NULL)
     return false;
+  srelrdyn->alloced = 1;
   bfd_vma *addr = htab->relr_sorted;
   bfd_byte *loc = srelrdyn->contents;
   for (bfd_size_type i = 0; i < htab->relr_count; )
@@ -9472,6 +9474,7 @@ elfNN_aarch64_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	    abort ();
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
 
@@ -9723,6 +9726,7 @@ elfNN_aarch64_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   if (htab->root.dynamic_sections_created)
diff --git a/bfd/elfnn-ia64.c b/bfd/elfnn-ia64.c
index d80fc425642..36ccfef0291 100644
--- a/bfd/elfnn-ia64.c
+++ b/bfd/elfnn-ia64.c
@@ -3010,6 +3010,7 @@ elfNN_ia64_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
       sec = bfd_get_linker_section (dynobj, ".interp");
       BFD_ASSERT (sec != NULL);
       sec->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
+      sec->alloced = 1;
       sec->size = strlen (ELF_DYNAMIC_INTERPRETER) + 1;
     }
 
@@ -3184,6 +3185,7 @@ elfNN_ia64_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	  sec->contents = (bfd_byte *) bfd_zalloc (dynobj, sec->size);
 	  if (sec->contents == NULL && sec->size != 0)
 	    return false;
+	  sec->alloced = 1;
 	}
     }
 
diff --git a/bfd/elfnn-kvx.c b/bfd/elfnn-kvx.c
index a8d2317ba5e..3720c5122c4 100644
--- a/bfd/elfnn-kvx.c
+++ b/bfd/elfnn-kvx.c
@@ -1610,6 +1610,7 @@ elfNN_kvx_build_stubs (struct bfd_link_info *info)
       stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
       if (stub_sec->contents == NULL && size != 0)
 	return false;
+      stub_sec->alloced = 1;
       stub_sec->size = 0;
     }
 
@@ -4049,6 +4050,7 @@ elfNN_kvx_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	    abort ();
 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
 
@@ -4210,6 +4212,7 @@ elfNN_kvx_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   if (htab->root.dynamic_sections_created)
diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
index be5ff00b416..858837370cd 100644
--- a/bfd/elfnn-loongarch.c
+++ b/bfd/elfnn-loongarch.c
@@ -2298,6 +2298,7 @@ loongarch_elf_finish_relative_relocs (struct bfd_link_info *info)
   srelrdyn->contents = bfd_alloc (dynobj, srelrdyn->size);
   if (!srelrdyn->contents)
     return false;
+  srelrdyn->alloced = 1;
 
   bfd_vma *addr = htab->relr_sorted;
   bfd_byte *loc = srelrdyn->contents;
@@ -2372,6 +2373,7 @@ loongarch_elf_late_size_sections (bfd *output_bfd,
 	    interpreter = "/lib/ld.so.1";
 
 	  s->contents = (unsigned char *) interpreter;
+	  s->alloced = 1;
 	  s->size = strlen (interpreter) + 1;
 	}
     }
@@ -2600,6 +2602,7 @@ loongarch_elf_late_size_sections (bfd *output_bfd,
       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   if (elf_hash_table (info)->dynamic_sections_created)
diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index 2122aa36c7c..1fe49fd5f06 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -1520,6 +1520,7 @@ riscv_elf_late_size_sections (bfd *output_bfd, struct bfd_link_info *info)
 	  BFD_ASSERT (s != NULL);
 	  s->size = strlen (ELFNN_DYNAMIC_INTERPRETER) + 1;
 	  s->contents = (unsigned char *) ELFNN_DYNAMIC_INTERPRETER;
+	  s->alloced = 1;
 	}
     }
 
@@ -1704,6 +1705,7 @@ riscv_elf_late_size_sections (bfd *output_bfd, struct bfd_link_info *info)
       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   /* Add dynamic entries.  */
diff --git a/bfd/elfxx-mips.c b/bfd/elfxx-mips.c
index fb42c741cbb..e428ae278ba 100644
--- a/bfd/elfxx-mips.c
+++ b/bfd/elfxx-mips.c
@@ -10001,6 +10001,7 @@ _bfd_mips_elf_late_size_sections (bfd *output_bfd,
 	    = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
 	  s->contents
 	    = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
+	  s->alloced = 1;
 	}
 
       /* Figure out the size of the PLT header if we know that we
@@ -10177,6 +10178,7 @@ _bfd_mips_elf_late_size_sections (bfd *output_bfd,
 	  bfd_set_error (bfd_error_no_memory);
 	  return false;
 	}
+      s->alloced = 1;
     }
 
   if (htab->root.dynamic_sections_created)
@@ -15405,6 +15407,7 @@ _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
 
 	  o->size = c * sizeof (Elf32_External_gptab);
 	  o->contents = (bfd_byte *) ext_tab;
+	  o->alloced = 1;
 
 	  /* Skip this section later on (I don't think this currently
 	     matters, but someday it might).  */
diff --git a/bfd/elfxx-sparc.c b/bfd/elfxx-sparc.c
index 91ffece38d0..71061621e8b 100644
--- a/bfd/elfxx-sparc.c
+++ b/bfd/elfxx-sparc.c
@@ -2403,6 +2403,7 @@ _bfd_sparc_elf_late_size_sections (bfd *output_bfd,
 	  BFD_ASSERT (s != NULL);
 	  s->size = htab->dynamic_interpreter_size;
 	  s->contents = (unsigned char *) htab->dynamic_interpreter;
+	  s->alloced = 1;
 	  htab->interp = s;
 	}
     }
@@ -2579,6 +2580,7 @@ _bfd_sparc_elf_late_size_sections (bfd *output_bfd,
       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   if (elf_hash_table (info)->dynamic_sections_created)
diff --git a/bfd/elfxx-tilegx.c b/bfd/elfxx-tilegx.c
index 71e91742033..afa9e86add5 100644
--- a/bfd/elfxx-tilegx.c
+++ b/bfd/elfxx-tilegx.c
@@ -2452,6 +2452,7 @@ tilegx_elf_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	  BFD_ASSERT (s != NULL);
 	  s->size = strlen (htab->dynamic_interpreter) + 1;
 	  s->contents = (unsigned char *) htab->dynamic_interpreter;
+	  s->alloced = 1;
 	}
     }
 
@@ -2617,6 +2618,7 @@ tilegx_elf_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   return _bfd_elf_add_dynamic_tags (output_bfd, info, true);
diff --git a/bfd/elfxx-x86.c b/bfd/elfxx-x86.c
index cd47575f589..8d8ee333dbe 100644
--- a/bfd/elfxx-x86.c
+++ b/bfd/elfxx-x86.c
@@ -1789,6 +1789,7 @@ elf_x86_write_dl_relr_bitmap (struct bfd_link_info *info,
 
   /* Cache the section contents for elf_link_input_bfd.  */
   sec->contents = contents;
+  sec->alloced = 1;
 
   if (ABI_64_P (info->output_bfd))
     for (i = 0; i < htab->dt_relr_bitmap.count; i++, contents += 8)
@@ -2018,6 +2019,7 @@ _bfd_x86_elf_write_sframe_plt (bfd *output_bfd,
 
   sec->size = (bfd_size_type) sec_size;
   sec->contents = (unsigned char *) bfd_zalloc (dynobj, sec->size);
+  sec->alloced = 1;
   memcpy (sec->contents, contents, sec_size);
 
   sframe_encoder_free (&ectx);
@@ -2675,6 +2677,7 @@ _bfd_x86_elf_late_size_sections (bfd *output_bfd,
       s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
       if (s->contents == NULL)
 	return false;
+      s->alloced = 1;
     }
 
   if (htab->plt_eh_frame != NULL
@@ -4706,6 +4709,7 @@ _bfd_x86_elf_link_setup_gnu_properties
 	    abort ();
 	  s->size = htab->dynamic_interpreter_size;
 	  s->contents = (unsigned char *) htab->dynamic_interpreter;
+	  s->alloced = 1;
 	  htab->interp = s;
 	}
 
diff --git a/bfd/opncls.c b/bfd/opncls.c
index ca7dbf23aa6..45f774c2c92 100644
--- a/bfd/opncls.c
+++ b/bfd/opncls.c
@@ -148,18 +148,6 @@ _bfd_new_bfd_contained_in (bfd *obfd)
 static void
 _bfd_delete_bfd (bfd *abfd)
 {
-#ifdef USE_MMAP
-  if (abfd->xvec
-      && abfd->xvec->flavour == bfd_target_elf_flavour)
-    {
-      asection *sec;
-      for (sec = abfd->sections; sec != NULL; sec = sec->next)
-	if (sec->mmapped_p)
-	  munmap (elf_section_data (sec)->contents_addr,
-		  elf_section_data (sec)->contents_size);
-    }
-#endif
-
   /* Give the target _bfd_free_cached_info a chance to free memory.  */
   if (abfd->memory && abfd->xvec)
     bfd_free_cached_info (abfd);
diff --git a/bfd/section.c b/bfd/section.c
index ffd28911297..b1f7564d190 100644
--- a/bfd/section.c
+++ b/bfd/section.c
@@ -422,9 +422,12 @@ CODE_FRAGMENT
 .  {* Nonzero if this section uses RELA relocations, rather than REL.  *}
 .  unsigned int use_rela_p:1;
 .
-.  {* Nonzero if this section contents are mmapped, rather than malloced.  *}
+.  {* Nonzero if section contents are mmapped.  *}
 .  unsigned int mmapped_p:1;
 .
+.  {* Nonzero if section contents should not be freed.  *}
+.  unsigned int alloced:1;
+.
 .  {* Bits used by various backends.  The generic code doesn't touch
 .     these fields.  *}
 .
@@ -716,8 +719,8 @@ EXTERNAL
 .  {* linker_mark, linker_has_input, gc_mark, decompress_status,     *}	\
 .     0,           0,                1,       0,			\
 .									\
-.  {* segment_mark, sec_info_type, use_rela_p, mmapped_p,           *}	\
-.     0,            0,             0,	       0,			\
+.  {* segment_mark, sec_info_type, use_rela_p, mmapped_p, alloced,   *}	\
+.     0,            0,             0,          0,         0,		\
 .									\
 .  {* sec_flg0, sec_flg1, sec_flg2, sec_flg3, sec_flg4, sec_flg5,    *}	\
 .     0,        0,        0,        0,        0,        0,		\
@@ -1656,6 +1659,10 @@ DESCRIPTION
 bool
 bfd_malloc_and_get_section (bfd *abfd, sec_ptr sec, bfd_byte **buf)
 {
+  /* FIXME: We sometimes get here when sec->alloced is set.
+     arm, aarch64, and xtensa targets all abort on some ld tests
+     if we also test sec->alloced here.  We really should not ever be
+     mallocing a buffer if we already have an alloced one.  */
   if (sec->mmapped_p)
     abort ();
   *buf = NULL;
diff --git a/bfd/vms-alpha.c b/bfd/vms-alpha.c
index f5eb99672c0..548a9790100 100644
--- a/bfd/vms-alpha.c
+++ b/bfd/vms-alpha.c
@@ -9280,6 +9280,7 @@ alpha_vms_build_fixups (struct bfd_link_info *info)
 
   sec = alpha_vms_link_hash (info)->fixup;
   sec->contents = content;
+  sec->alloced = 1;
   sec->size = sz;
 
   eiaf = (struct vms_eiaf *)content;
@@ -9685,6 +9686,7 @@ alpha_vms_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
 	  o->contents = bfd_alloc (abfd, o->size);
 	  if (o->contents == NULL)
 	    return false;
+	  o->alloced = 1;
 	}
       if (o->flags & SEC_LOAD)
 	{
@@ -9833,6 +9835,7 @@ alpha_vms_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
 	      if (contents == NULL)
 		return false;
 	      dmt->contents = contents;
+	      dmt->alloced = 1;
 	      dmt->size = off;
 	    }
 	  else
@@ -9891,6 +9894,7 @@ alpha_vms_get_section_contents (bfd *abfd, asection *section,
 	      sec->flags |= SEC_IN_MEMORY;
 	      if (sec->contents == NULL)
 		return false;
+	      sec->alloced = 1;
 	    }
 	}
       if (!alpha_vms_read_sections_content (abfd, NULL))
@@ -10111,6 +10115,7 @@ _bfd_vms_set_section_contents (bfd * abfd,
       section->contents = bfd_alloc (abfd, section->size);
       if (section->contents == NULL)
 	return false;
+      section->alloced = 1;
 
       memcpy (section->contents + offset, location, (size_t) count);
     }
diff --git a/bfd/wasm-module.c b/bfd/wasm-module.c
index d5d186e07c8..db4b08147a8 100644
--- a/bfd/wasm-module.c
+++ b/bfd/wasm-module.c
@@ -468,6 +468,7 @@ wasm_scan (bfd *abfd)
 						  bfdsec->size);
 	  if (!bfdsec->contents)
 	    goto error_return;
+	  bfdsec->alloced = 1;
 	}
 
       vma += bfdsec->size;
diff --git a/bfd/xcofflink.c b/bfd/xcofflink.c
index 502cd08a891..14eb008f967 100644
--- a/bfd/xcofflink.c
+++ b/bfd/xcofflink.c
@@ -3990,6 +3990,7 @@ xcoff_build_loader_section (struct xcoff_loader_info *ldinfo)
   lsec->contents = bfd_zalloc (output_bfd, lsec->size);
   if (lsec->contents == NULL)
     return false;
+  lsec->alloced = 1;
 
   /* Set up the header.  */
   bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
@@ -4062,6 +4063,7 @@ bfd_xcoff_build_dynamic_sections (bfd *output_bfd,
       sec->contents = bfd_zalloc (output_bfd, sec->size);
       if (sec->contents == NULL)
 	return false;
+      sec->alloced = 1;
     }
   sec = xcoff_hash_table (info)->toc_section;
   if (sec->size > 0)
@@ -4069,6 +4071,7 @@ bfd_xcoff_build_dynamic_sections (bfd *output_bfd,
       sec->contents = bfd_zalloc (output_bfd, sec->size);
       if (sec->contents == NULL)
 	return false;
+      sec->alloced = 1;
     }
   sec = xcoff_hash_table (info)->descriptor_section;
   if (sec->size > 0)
@@ -4076,6 +4079,7 @@ bfd_xcoff_build_dynamic_sections (bfd *output_bfd,
       sec->contents = bfd_zalloc (output_bfd, sec->size);
       if (sec->contents == NULL)
 	return false;
+      sec->alloced = 1;
     }
 
   /* Now that we've done garbage collection, decide which symbols to keep,
@@ -4931,7 +4935,7 @@ bfd_xcoff_build_stubs (struct bfd_link_info *info)
       stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, size);
       if (stub_sec->contents == NULL && size != 0)
 	return false;
-
+      stub_sec->alloced = 1;
     }
 
   /* Build the stubs as directed by the stub hash table.  */
diff --git a/gas/config/obj-elf.c b/gas/config/obj-elf.c
index c4af01808b6..52080cb5bc1 100644
--- a/gas/config/obj-elf.c
+++ b/gas/config/obj-elf.c
@@ -3076,6 +3076,7 @@ elf_frob_file_after_relocs (void)
       subseg_set (group, 0);
       bfd_set_section_size (group, size);
       group->contents = (unsigned char *) frag_more (size);
+      group->alloced = 1;
       frag_now->fr_fix = frag_now_fix_octets ();
       frag_wane (frag_now);
     }
diff --git a/ld/ldelf.c b/ld/ldelf.c
index 4a1aa044e70..d627b55419b 100644
--- a/ld/ldelf.c
+++ b/ld/ldelf.c
@@ -1857,6 +1857,7 @@ ldelf_before_allocation (char *audit, char *depaudit,
       if (default_interpreter_name != NULL)
 	{
 	  sinterp->contents = (bfd_byte *) default_interpreter_name;
+	  sinterp->alloced = 1;
 	  sinterp->size = strlen ((char *) sinterp->contents) + 1;
 	}
     }

-- 
Alan Modra


More information about the Binutils mailing list