From ed15ac6a0a461de02bf1d4c50762300fc05e5ca6 Mon Sep 17 00:00:00 2001
From: Alan Modra <amodra@gmail.com>
Date: Thu, 7 Sep 2000 09:09:19 +0000
Subject: [PATCH] Fix an orphan section problem caused by weird linker scripts.

---
 ld/ChangeLog          |  1 +
 ld/emultempl/elf32.em | 43 +++++++++++++++++++++++--------------------
 2 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/ld/ChangeLog b/ld/ChangeLog
index 0292f837f21..37deddcff51 100644
--- a/ld/ChangeLog
+++ b/ld/ChangeLog
@@ -4,6 +4,7 @@
 	broken list handling.  Create __start_SECNAME and __stop_SECNAME
 	when no place-holder.  Add some comments.  Test both SEC_CODE and
 	SEC_READONLY for hold_text to prevent .rodata orphan poisoning.
+	Handle case where no output section statement created.
 
 2000-09-07  Niibe Yutaka  <gniibe@m17n.org>, Kaz Kojima  <kkojima@rr.iij4u.or.jp>, Alexandre Oliva  <aoliva@redhat.com>
 
diff --git a/ld/emultempl/elf32.em b/ld/emultempl/elf32.em
index 037ac5aec9b..c7ba4972730 100644
--- a/ld/emultempl/elf32.em
+++ b/ld/emultempl/elf32.em
@@ -1203,29 +1203,32 @@ gld${EMULATION_NAME}_place_orphan (file, s)
 	}
       place->section = &snew->next;	/* Save the end of this list.  */
 
-      /* We try to put the output statements in some sort of
-	 reasonable order here, because they determine the final load
-	 addresses of the orphan sections.  */
-      if (place->stmt == NULL)
+      if (add.head != NULL)
 	{
-	  /* Put the new statement list right at the head.  */
-	  *add.tail = place->os->header.next;
-	  place->os->header.next = add.head;
-	}
-      else
-	{
-	  /* Put it after the last orphan statement we added.  */
-	  *add.tail = *place->stmt;
-	  *place->stmt = add.head;
-	}
+	  /* We try to put the output statements in some sort of
+	     reasonable order here, because they determine the final
+	     load addresses of the orphan sections.  */
+	  if (place->stmt == NULL)
+	    {
+	      /* Put the new statement list right at the head.  */
+	      *add.tail = place->os->header.next;
+	      place->os->header.next = add.head;
+	    }
+	  else
+	    {
+	      /* Put it after the last orphan statement we added.  */
+	      *add.tail = *place->stmt;
+	      *place->stmt = add.head;
+	    }
 
-      /* Fix the global list pointer if we happened to tack our new
-	 list at the tail.  */
-      if (*old->tail == add.head)
-	old->tail = add.tail;
+	  /* Fix the global list pointer if we happened to tack our
+	     new list at the tail.  */
+	  if (*old->tail == add.head)
+	    old->tail = add.tail;
 
-      /* Save the end of this list.  */
-      place->stmt = add.tail;
+	  /* Save the end of this list.  */
+	  place->stmt = add.tail;
+	}
     }
 
   return true;