remove some assumptions about dynobj

Message ID apS6I5hd4Fnz7pQU@squeak.grove.modra.org
State New
Headers
Series remove some assumptions about dynobj |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 fail Patch failed to apply

Commit Message

Alan Modra Aug. 30, 2026, 11:17 p.m. UTC
  A number of backends assume that if dynobj is non-NULL then some
dynamic sections must have been created.  That isn't the case if
dynobj is set without a create_dynamic_sections call, for instance, if
dynobj were set to the stub bfd as ppc64 does early in linking.
This patch fixes those assumptions, and tidies some code a little.

	* elf-m10300.c (_bfd_mn10300_elf_finish_dynamic_sections): Move
	assertions after dynamic_sections_created test.  Test for sgot
	existence.
	* elf32-cr16.c (_bfd_cr16_elf_finish_dynamic_sections): Likewise.
	* elf32-cris.c (elf_cris_finish_dynamic_sections): Likewise.
	* elf32-m68k.c (elf_m68k_finish_dynamic_sections): Likewise.
	* elf32-vax.c (elf_vax_finish_dynamic_sections): Likewise.
	* elf32-bfin.c (elf32_bfinfdpic_late_size_sections): Check that
	bfinfdpic_relocs_info is non-NULL before htab_traverse on same.
	(elf32_bfinfdpic_finish_dynamic_sections): Delay finding sdyn
	until after dynamic_section_created test.
	(bfin_finish_dynamic_sections): Likewise.
	* elf32-lm32.c (lm32_elf_late_size_sections): Check that
	lm32fdpic_fixup32_section exists.
	* elf32-m32c.c (m32c_elf_finish_dynamic_sections): Don't test
	dynobj, test splt directly.
	(m32c_elf_early_size_sections): Likewise.
	* elf32-rl78.c (rl78_elf_finish_dynamic_sections): Likewise.
	(rl78_elf_early_size_sections): Likewise.
	* elf32-xstormy16.c (xstormy16_elf_early_size_sections): Likewise.
	(xstormy16_elf_finish_dynamic_sections): Likewise.
	* elf32-score.c (score_elf_got_info): Don't assert.  Test for
	sgot existence.
	* elf32-score7.c (score_elf_got_info): Likewise.
	* elf64-hppa.c (elf64_hppa_late_size_sections): Don't create
	__text_seg or __data_seg dynamic symbols unless
	dynamic_sections_created.
  

Patch

diff --git a/bfd/elf-m10300.c b/bfd/elf-m10300.c
index 05f2a9811e3..332b294e1e2 100644
--- a/bfd/elf-m10300.c
+++ b/bfd/elf-m10300.c
@@ -5344,16 +5344,15 @@  _bfd_mn10300_elf_finish_dynamic_sections (struct bfd_link_info *info,
 
   dynobj = htab->root.dynobj;
   sgot = htab->root.sgotplt;
-  BFD_ASSERT (sgot != NULL);
   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
 
-  if (elf_hash_table (info)->dynamic_sections_created)
+  if (htab->root.dynamic_sections_created)
     {
       asection *	   splt;
       Elf32_External_Dyn * dyncon;
       Elf32_External_Dyn * dynconend;
 
-      BFD_ASSERT (sdyn != NULL);
+      BFD_ASSERT (sdyn != NULL && sgot != NULL);
 
       dyncon = (Elf32_External_Dyn *) sdyn->contents;
       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
@@ -5424,7 +5423,7 @@  _bfd_mn10300_elf_finish_dynamic_sections (struct bfd_link_info *info,
     }
 
   /* Fill in the first three entries in the global offset table.  */
-  if (sgot->size > 0)
+  if (sgot != NULL && sgot->size > 0)
     {
       if (sdyn == NULL)
 	bfd_put_32 (info->output_bfd, 0, sgot->contents);
@@ -5434,9 +5433,9 @@  _bfd_mn10300_elf_finish_dynamic_sections (struct bfd_link_info *info,
 		    sgot->contents);
       bfd_put_32 (info->output_bfd, 0, sgot->contents + 4);
       bfd_put_32 (info->output_bfd, 0, sgot->contents + 8);
-    }
 
-  elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
+      elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
+    }
 
   return true;
 }
diff --git a/bfd/elf32-bfin.c b/bfd/elf32-bfin.c
index 38546621864..c4d9389a04a 100644
--- a/bfd/elf32-bfin.c
+++ b/bfd/elf32-bfin.c
@@ -4038,7 +4038,6 @@  elf32_bfinfdpic_late_size_sections (struct bfd_link_info *info)
   struct elf_link_hash_table *htab;
   bfd *dynobj;
   asection *s;
-  struct _bfinfdpic_dynamic_got_plt_info gpinfo;
 
   htab = elf_hash_table (info);
   dynobj = htab->dynobj;
@@ -4058,28 +4057,33 @@  elf32_bfinfdpic_late_size_sections (struct bfd_link_info *info)
 	}
     }
 
-  memset (&gpinfo, 0, sizeof (gpinfo));
-  gpinfo.g.info = info;
-
-  for (;;)
+  if (bfinfdpic_relocs_info (info))
     {
-      htab_t relocs = bfinfdpic_relocs_info (info);
+      struct _bfinfdpic_dynamic_got_plt_info gpinfo;
+      memset (&gpinfo, 0, sizeof (gpinfo));
+      gpinfo.g.info = info;
 
-      htab_traverse (relocs, _bfinfdpic_resolve_final_relocs_info, &relocs);
+      for (;;)
+	{
+	  htab_t relocs = bfinfdpic_relocs_info (info);
 
-      if (relocs == bfinfdpic_relocs_info (info))
-	break;
-    }
+	  htab_traverse (relocs, _bfinfdpic_resolve_final_relocs_info, &relocs);
+
+	  if (relocs == bfinfdpic_relocs_info (info))
+	    break;
+	}
 
-  htab_traverse (bfinfdpic_relocs_info (info), _bfinfdpic_count_got_plt_entries,
-		 &gpinfo.g);
+      htab_traverse (bfinfdpic_relocs_info (info),
+		     _bfinfdpic_count_got_plt_entries, &gpinfo.g);
 
-  /* Allocate space to save the summary information, we're going to
-     use it if we're doing relaxations.  */
-  bfinfdpic_dynamic_got_plt_info (info) = bfd_alloc (dynobj, sizeof (gpinfo.g));
+      /* Allocate space to save the summary information, we're going to
+	 use it if we're doing relaxations.  */
+      bfinfdpic_dynamic_got_plt_info (info) = bfd_alloc (dynobj,
+							 sizeof (gpinfo.g));
 
-  if (!_bfinfdpic_size_got_plt (info->output_bfd, &gpinfo))
-      return false;
+      if (!_bfinfdpic_size_got_plt (info->output_bfd, &gpinfo))
+	return false;
+    }
 
   s = bfd_get_linker_section (dynobj, ".dynbss");
   if (s && s->size == 0)
@@ -4218,11 +4222,6 @@  static bool
 elf32_bfinfdpic_finish_dynamic_sections (struct bfd_link_info *info,
 					 bfd_byte *buf ATTRIBUTE_UNUSED)
 {
-  bfd *dynobj;
-  asection *sdyn;
-
-  dynobj = elf_hash_table (info)->dynobj;
-
   if (bfinfdpic_got_section (info))
     {
       BFD_ASSERT (bfinfdpic_gotrel_section (info)->size
@@ -4262,12 +4261,11 @@  elf32_bfinfdpic_finish_dynamic_sections (struct bfd_link_info *info,
 		      * sizeof (Elf32_External_Rel)));
     }
 
-  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
-
   if (elf_hash_table (info)->dynamic_sections_created)
     {
-      Elf32_External_Dyn * dyncon;
-      Elf32_External_Dyn * dynconend;
+      bfd *dynobj = elf_hash_table (info)->dynobj;
+      asection *sdyn = bfd_get_linker_section (dynobj, ".dynamic");
+      Elf32_External_Dyn *dyncon, *dynconend;
 
       BFD_ASSERT (sdyn != NULL);
 
@@ -4866,15 +4864,10 @@  static bool
 bfin_finish_dynamic_sections (struct bfd_link_info *info,
 			      bfd_byte *buf ATTRIBUTE_UNUSED)
 {
-  bfd *dynobj;
-  asection *sdyn;
-
-  dynobj = elf_hash_table (info)->dynobj;
-
-  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
-
   if (elf_hash_table (info)->dynamic_sections_created)
     {
+      bfd *dynobj = elf_hash_table (info)->dynobj;
+      asection *sdyn = bfd_get_linker_section (dynobj, ".dynamic");
       Elf32_External_Dyn *dyncon, *dynconend;
 
       BFD_ASSERT (sdyn != NULL);
diff --git a/bfd/elf32-cr16.c b/bfd/elf32-cr16.c
index 8e827afe3a3..1a8a3b28180 100644
--- a/bfd/elf32-cr16.c
+++ b/bfd/elf32-cr16.c
@@ -2589,9 +2589,7 @@  _bfd_cr16_elf_finish_dynamic_sections (struct bfd_link_info *info,
   asection * sdyn;
 
   dynobj = elf_hash_table (info)->dynobj;
-
   sgot = elf_hash_table (info)->sgotplt;
-  BFD_ASSERT (sgot != NULL);
   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
 
   if (elf_hash_table (info)->dynamic_sections_created)
@@ -2599,7 +2597,7 @@  _bfd_cr16_elf_finish_dynamic_sections (struct bfd_link_info *info,
       Elf32_External_Dyn * dyncon;
       Elf32_External_Dyn * dynconend;
 
-      BFD_ASSERT (sdyn != NULL);
+      BFD_ASSERT (sdyn != NULL && sgot != NULL);
 
       dyncon = (Elf32_External_Dyn *) sdyn->contents;
       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
@@ -2638,7 +2636,7 @@  _bfd_cr16_elf_finish_dynamic_sections (struct bfd_link_info *info,
     }
 
   /* Fill in the first three entries in the global offset table.  */
-  if (sgot->size > 0)
+  if (sgot != NULL && sgot->size > 0)
     {
       if (sdyn == NULL)
 	bfd_put_32 (info->output_bfd, 0, sgot->contents);
@@ -2646,9 +2644,9 @@  _bfd_cr16_elf_finish_dynamic_sections (struct bfd_link_info *info,
 	bfd_put_32 (info->output_bfd,
 		    sdyn->output_section->vma + sdyn->output_offset,
 		    sgot->contents);
-    }
 
-  elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
+      elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
+    }
 
   return true;
 }
diff --git a/bfd/elf32-cris.c b/bfd/elf32-cris.c
index 1df98bd55aa..938f3bc7bbb 100644
--- a/bfd/elf32-cris.c
+++ b/bfd/elf32-cris.c
@@ -2317,9 +2317,7 @@  elf_cris_finish_dynamic_sections (struct bfd_link_info *info,
   asection *sdyn;
 
   dynobj = elf_hash_table (info)->dynobj;
-
   sgot = elf_hash_table (info)->sgotplt;
-  BFD_ASSERT (sgot != NULL);
   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
 
   if (elf_hash_table (info)->dynamic_sections_created)
@@ -2328,7 +2326,7 @@  elf_cris_finish_dynamic_sections (struct bfd_link_info *info,
       Elf32_External_Dyn *dyncon, *dynconend;
 
       splt = elf_hash_table (info)->splt;
-      BFD_ASSERT (splt != NULL && sdyn != NULL);
+      BFD_ASSERT (splt != NULL && sdyn != NULL && sgot != NULL);
 
       dyncon = (Elf32_External_Dyn *) sdyn->contents;
       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
@@ -2416,7 +2414,7 @@  elf_cris_finish_dynamic_sections (struct bfd_link_info *info,
     }
 
   /* Fill in the first three entries in the global offset table.  */
-  if (sgot->size > 0)
+  if (sgot != NULL && sgot->size > 0)
     {
       if (sdyn == NULL)
 	bfd_put_32 (info->output_bfd, 0, sgot->contents);
@@ -2426,9 +2424,9 @@  elf_cris_finish_dynamic_sections (struct bfd_link_info *info,
 		    sgot->contents);
       bfd_put_32 (info->output_bfd, 0, sgot->contents + 4);
       bfd_put_32 (info->output_bfd, 0, sgot->contents + 8);
-    }
 
-  elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
+      elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
+    }
 
   return true;
 }
diff --git a/bfd/elf32-lm32.c b/bfd/elf32-lm32.c
index 72a021b7d12..b9768952e19 100644
--- a/bfd/elf32-lm32.c
+++ b/bfd/elf32-lm32.c
@@ -2084,7 +2084,8 @@  lm32_elf_late_size_sections (struct bfd_link_info *info)
     return false;
 
   /* Allocate .rofixup section.  */
-  if (IS_FDPIC (info->output_bfd))
+  if (IS_FDPIC (info->output_bfd)
+      && lm32fdpic_fixup32_section (info) != NULL)
     {
       struct weak_symbol_list *list_start = NULL, *list_end = NULL;
       int rgot_weak_count = 0;
diff --git a/bfd/elf32-m32c.c b/bfd/elf32-m32c.c
index 7fcf8216a3f..fbe9eb2978a 100644
--- a/bfd/elf32-m32c.c
+++ b/bfd/elf32-m32c.c
@@ -762,7 +762,7 @@  m32c_elf_finish_dynamic_sections (struct bfd_link_info *info,
   /* As an extra sanity check, verify that all plt entries have
      been filled in.  */
 
-  if (dynobj != NULL && splt != NULL)
+  if (splt != NULL)
     {
       bfd_byte *contents = splt->contents;
       unsigned int i, size = splt->size;
@@ -779,24 +779,20 @@  m32c_elf_finish_dynamic_sections (struct bfd_link_info *info,
 static bool
 m32c_elf_early_size_sections (struct bfd_link_info *info)
 {
-  bfd *dynobj;
   asection *splt;
 
   if (bfd_link_relocatable (info))
     return true;
 
-  dynobj = elf_hash_table (info)->dynobj;
-  if (dynobj == NULL)
-    return true;
-
   splt = elf_hash_table (info)->splt;
-  BFD_ASSERT (splt != NULL);
-
-  splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size);
-  if (splt->contents == NULL)
-    return false;
-  splt->alloced = 1;
-
+  if (splt != NULL)
+    {
+      bfd *dynobj = elf_hash_table (info)->dynobj;
+      splt->contents = bfd_zalloc (dynobj, splt->size);
+      if (splt->contents == NULL)
+	return false;
+      splt->alloced = 1;
+    }
   return true;
 }
 
diff --git a/bfd/elf32-m68k.c b/bfd/elf32-m68k.c
index b50a7706a85..1bc72c7ae42 100644
--- a/bfd/elf32-m68k.c
+++ b/bfd/elf32-m68k.c
@@ -4278,9 +4278,7 @@  elf_m68k_finish_dynamic_sections (struct bfd_link_info *info,
   asection *sdyn;
 
   dynobj = elf_hash_table (info)->dynobj;
-
   sgot = elf_hash_table (info)->sgotplt;
-  BFD_ASSERT (sgot != NULL);
   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
 
   if (elf_hash_table (info)->dynamic_sections_created)
@@ -4289,7 +4287,7 @@  elf_m68k_finish_dynamic_sections (struct bfd_link_info *info,
       Elf32_External_Dyn *dyncon, *dynconend;
 
       splt = elf_hash_table (info)->splt;
-      BFD_ASSERT (splt != NULL && sdyn != NULL);
+      BFD_ASSERT (splt != NULL && sdyn != NULL && sgot != NULL);
 
       dyncon = (Elf32_External_Dyn *) sdyn->contents;
       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
@@ -4347,7 +4345,7 @@  elf_m68k_finish_dynamic_sections (struct bfd_link_info *info,
     }
 
   /* Fill in the first three entries in the global offset table.  */
-  if (sgot->size > 0)
+  if (sgot != NULL && sgot->size > 0)
     {
       if (sdyn == NULL)
 	bfd_put_32 (info->output_bfd, 0, sgot->contents);
@@ -4357,9 +4355,9 @@  elf_m68k_finish_dynamic_sections (struct bfd_link_info *info,
 		    sgot->contents);
       bfd_put_32 (info->output_bfd, 0, sgot->contents + 4);
       bfd_put_32 (info->output_bfd, 0, sgot->contents + 8);
-    }
 
-  elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
+      elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
+    }
 
   return true;
 }
diff --git a/bfd/elf32-rl78.c b/bfd/elf32-rl78.c
index 9a3e2f22707..8ce087ef28b 100644
--- a/bfd/elf32-rl78.c
+++ b/bfd/elf32-rl78.c
@@ -1444,7 +1444,7 @@  rl78_elf_finish_dynamic_sections (struct bfd_link_info *info,
 
   dynobj = elf_hash_table (info)->dynobj;
   splt = elf_hash_table (info)->splt;
-  if (dynobj != NULL && splt != NULL)
+  if (splt != NULL)
     {
       bfd_byte *contents = splt->contents;
       unsigned int i, size = splt->size;
@@ -1462,24 +1462,20 @@  rl78_elf_finish_dynamic_sections (struct bfd_link_info *info,
 static bool
 rl78_elf_early_size_sections (struct bfd_link_info *info)
 {
-  bfd *dynobj;
   asection *splt;
 
   if (bfd_link_relocatable (info))
     return true;
 
-  dynobj = elf_hash_table (info)->dynobj;
-  if (dynobj == NULL)
-    return true;
-
   splt = elf_hash_table (info)->splt;
-  BFD_ASSERT (splt != NULL);
-
-  splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size);
-  if (splt->contents == NULL)
-    return false;
-  splt->alloced = 1;
-
+  if (splt != NULL)
+    {
+      bfd *dynobj = elf_hash_table (info)->dynobj;
+      splt->contents = bfd_zalloc (dynobj, splt->size);
+      if (splt->contents == NULL)
+	return false;
+      splt->alloced = 1;
+    }
   return true;
 }
 
diff --git a/bfd/elf32-score.c b/bfd/elf32-score.c
index dca6333e710..227e7ca245c 100644
--- a/bfd/elf32-score.c
+++ b/bfd/elf32-score.c
@@ -1100,13 +1100,13 @@  score_elf_got_info (bfd *abfd, asection **sgotp)
   struct score_got_info *g;
 
   sgot = score_elf_got_section (abfd, true);
-  BFD_ASSERT (sgot != NULL);
-  BFD_ASSERT (elf_section_data (sgot) != NULL);
-  g = score_elf_section_data (sgot)->u.got_info;
-  BFD_ASSERT (g != NULL);
-
   if (sgotp)
     *sgotp = sgot;
+
+  g = NULL;
+  if (sgot != NULL)
+    g = score_elf_section_data (sgot)->u.got_info;
+
   return g;
 }
 
@@ -3206,7 +3206,6 @@  s3_bfd_score_elf_early_size_sections (struct bfd_link_info *info)
 
   dynobj = elf_hash_table (info)->dynobj;
   if (dynobj == NULL)
-    /* Relocatable links don't have it.  */
     return true;
 
   g = score_elf_got_info (dynobj, &s);
diff --git a/bfd/elf32-score7.c b/bfd/elf32-score7.c
index 16378cddc30..6b8f0ae2ea6 100644
--- a/bfd/elf32-score7.c
+++ b/bfd/elf32-score7.c
@@ -984,13 +984,13 @@  score_elf_got_info (bfd *abfd, asection **sgotp)
   struct score_got_info *g;
 
   sgot = score_elf_got_section (abfd, true);
-  BFD_ASSERT (sgot != NULL);
-  BFD_ASSERT (elf_section_data (sgot) != NULL);
-  g = score_elf_section_data (sgot)->u.got_info;
-  BFD_ASSERT (g != NULL);
-
   if (sgotp)
     *sgotp = sgot;
+
+  g = NULL;
+  if (sgot != NULL)
+    g = score_elf_section_data (sgot)->u.got_info;
+
   return g;
 }
 
@@ -3010,7 +3010,6 @@  s7_bfd_score_elf_early_size_sections (struct bfd_link_info *info)
 
   dynobj = elf_hash_table (info)->dynobj;
   if (dynobj == NULL)
-    /* Relocatable links don't have it.  */
     return true;
 
   g = score_elf_got_info (dynobj, &s);
diff --git a/bfd/elf32-vax.c b/bfd/elf32-vax.c
index 8e5a5275428..3a302ddce9a 100644
--- a/bfd/elf32-vax.c
+++ b/bfd/elf32-vax.c
@@ -1729,9 +1729,7 @@  elf_vax_finish_dynamic_sections (struct bfd_link_info *info,
   asection *sdyn;
 
   dynobj = elf_hash_table (info)->dynobj;
-
   sgot = elf_hash_table (info)->sgotplt;
-  BFD_ASSERT (sgot != NULL);
   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
 
   if (elf_hash_table (info)->dynamic_sections_created)
@@ -1740,7 +1738,7 @@  elf_vax_finish_dynamic_sections (struct bfd_link_info *info,
       Elf32_External_Dyn *dyncon, *dynconend;
 
       splt = elf_hash_table (info)->splt;
-      BFD_ASSERT (splt != NULL && sdyn != NULL);
+      BFD_ASSERT (splt != NULL && sdyn != NULL && sgot != NULL);
 
       dyncon = (Elf32_External_Dyn *) sdyn->contents;
       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
@@ -1794,7 +1792,7 @@  elf_vax_finish_dynamic_sections (struct bfd_link_info *info,
     }
 
   /* Fill in the first three entries in the global offset table.  */
-  if (sgot->size > 0)
+  if (sgot != NULL && sgot->size > 0)
     {
       if (sdyn == NULL)
 	bfd_put_32 (info->output_bfd, 0, sgot->contents);
@@ -1804,10 +1802,10 @@  elf_vax_finish_dynamic_sections (struct bfd_link_info *info,
 		    sgot->contents);
       bfd_put_32 (info->output_bfd, 0, sgot->contents + 4);
       bfd_put_32 (info->output_bfd, 0, sgot->contents + 8);
-    }
 
-  if (elf_section_data (sgot->output_section) != NULL)
-    elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
+      if (elf_section_data (sgot->output_section) != NULL)
+	elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
+    }
 
   return true;
 }
diff --git a/bfd/elf32-xstormy16.c b/bfd/elf32-xstormy16.c
index 07270b8b66f..e7b05058f9d 100644
--- a/bfd/elf32-xstormy16.c
+++ b/bfd/elf32-xstormy16.c
@@ -713,24 +713,20 @@  xstormy16_elf_relax_section (bfd *dynobj,
 static bool
 xstormy16_elf_early_size_sections (struct bfd_link_info *info)
 {
-  bfd *dynobj;
   asection *splt;
 
   if (bfd_link_relocatable (info))
     return true;
 
-  dynobj = elf_hash_table (info)->dynobj;
-  if (dynobj == NULL)
-    return true;
-
   splt = elf_hash_table (info)->splt;
-  BFD_ASSERT (splt != NULL);
-
-  splt->contents = bfd_zalloc (dynobj, splt->size);
-  if (splt->contents == NULL)
-    return false;
-  splt->alloced = 1;
-
+  if (splt != NULL)
+    {
+      bfd *dynobj = elf_hash_table (info)->dynobj;
+      splt->contents = bfd_zalloc (dynobj, splt->size);
+      if (splt->contents == NULL)
+	return false;
+      splt->alloced = 1;
+    }
   return true;
 }
 
@@ -970,7 +966,7 @@  xstormy16_elf_finish_dynamic_sections (struct bfd_link_info *info,
   /* As an extra sanity check, verify that all plt entries have
      been filled in.  */
 
-  if (dynobj != NULL && splt != NULL)
+  if (splt != NULL)
     {
       bfd_byte *contents = splt->contents;
       unsigned int i, size = splt->size;
diff --git a/bfd/elf64-hppa.c b/bfd/elf64-hppa.c
index 0a14e2f1adc..e57a56f242b 100644
--- a/bfd/elf64-hppa.c
+++ b/bfd/elf64-hppa.c
@@ -1804,7 +1804,7 @@  elf64_hppa_late_size_sections (struct bfd_link_info *info)
     }
 
   /* Add __text_seg section symbol to dynamic table.  */
-  if (!hppa_info->text_hash_entry)
+  if (hppa_info->root.dynamic_sections_created && !hppa_info->text_hash_entry)
     {
       asection *s;
 
@@ -1833,7 +1833,7 @@  elf64_hppa_late_size_sections (struct bfd_link_info *info)
     }
 
   /* Add __data_seg section symbol to dynamic table.  */
-  if (!hppa_info->data_hash_entry)
+  if (hppa_info->root.dynamic_sections_created && !hppa_info->data_hash_entry)
     {
       asection *s;