Nick Clifton e0e1ae
diff --git a/bfd/elf.c b/bfd/elf.c
Nick Clifton e0e1ae
index c132098558..325bdd545a 100644
Nick Clifton e0e1ae
--- a/bfd/elf.c
Nick Clifton e0e1ae
+++ b/bfd/elf.c
Nick Clifton e0e1ae
@@ -4727,33 +4727,35 @@ _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
Nick Clifton e0e1ae
 		 the previous section, then we need a new segment.  */
Nick Clifton e0e1ae
 	      new_segment = TRUE;
Nick Clifton e0e1ae
 	    }
Nick Clifton e0e1ae
+	  else if ((abfd->flags & D_PAGED) != 0
Nick Clifton e0e1ae
+		   && (((last_hdr->lma + last_size - 1) & -maxpagesize)
Nick Clifton e0e1ae
+		       == (hdr->lma & -maxpagesize)))
Nick Clifton e0e1ae
+	    {
Nick Clifton e0e1ae
+	      /* If we are demand paged then we can't map two disk
Nick Clifton e0e1ae
+		 pages onto the same memory page.  */
Nick Clifton e0e1ae
+	      new_segment = FALSE;
Nick Clifton e0e1ae
+	    }
Nick Clifton e0e1ae
 	  /* In the next test we have to be careful when last_hdr->lma is close
Nick Clifton e0e1ae
 	     to the end of the address space.  If the aligned address wraps
Nick Clifton e0e1ae
 	     around to the start of the address space, then there are no more
Nick Clifton e0e1ae
 	     pages left in memory and it is OK to assume that the current
Nick Clifton e0e1ae
 	     section can be included in the current segment.  */
Nick Clifton e0e1ae
-	  else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
Nick Clifton e0e1ae
-		    > last_hdr->lma)
Nick Clifton e0e1ae
-		   && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
Nick Clifton e0e1ae
-		       <= hdr->lma))
Nick Clifton e0e1ae
+	  else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
Nick Clifton e0e1ae
+		    + maxpagesize > last_hdr->lma)
Nick Clifton e0e1ae
+		   && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
Nick Clifton e0e1ae
+		       + maxpagesize <= hdr->lma))
Nick Clifton e0e1ae
 	    {
Nick Clifton e0e1ae
 	      /* If putting this section in this segment would force us to
Nick Clifton e0e1ae
 		 skip a page in the segment, then we need a new segment.  */
Nick Clifton e0e1ae
 	      new_segment = TRUE;
Nick Clifton e0e1ae
 	    }
Nick Clifton e0e1ae
 	  else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
Nick Clifton e0e1ae
-		   && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0
Nick Clifton e0e1ae
-		   && ((abfd->flags & D_PAGED) == 0
Nick Clifton e0e1ae
-		       || (((last_hdr->lma + last_size - 1) & -maxpagesize)
Nick Clifton e0e1ae
-			   != (hdr->lma & -maxpagesize))))
Nick Clifton e0e1ae
+		   && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
Nick Clifton e0e1ae
 	    {
Nick Clifton e0e1ae
 	      /* We don't want to put a loaded section after a
Nick Clifton e0e1ae
 		 nonloaded (ie. bss style) section in the same segment
Nick Clifton e0e1ae
 		 as that will force the non-loaded section to be loaded.
Nick Clifton e0e1ae
-		 Consider .tbss sections as loaded for this purpose.
Nick Clifton e0e1ae
-		 However, like the writable/non-writable case below,
Nick Clifton e0e1ae
-		 if they are on the same page then they must be put
Nick Clifton e0e1ae
-		 in the same segment.  */
Nick Clifton e0e1ae
+		 Consider .tbss sections as loaded for this purpose.  */
Nick Clifton e0e1ae
 	      new_segment = TRUE;
Nick Clifton e0e1ae
 	    }
Nick Clifton e0e1ae
 	  else if ((abfd->flags & D_PAGED) == 0)
Nick Clifton e0e1ae
@@ -4769,21 +4771,11 @@ _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
Nick Clifton e0e1ae
 	    {
Nick Clifton e0e1ae
 	      new_segment = TRUE;
Nick Clifton e0e1ae
 	    }
Nick Clifton e0e1ae
-	  else if (! writable
Nick Clifton e0e1ae
-		   && (hdr->flags & SEC_READONLY) == 0
Nick Clifton e0e1ae
-		   && ((info != NULL
Nick Clifton e0e1ae
-			&& info->relro_end > info->relro_start)
Nick Clifton e0e1ae
-		       || (((last_hdr->lma + last_size - 1) & -maxpagesize)
Nick Clifton e0e1ae
-			   != (hdr->lma & -maxpagesize))))
Nick Clifton e0e1ae
+          else if (! writable
Nick Clifton e0e1ae
+		   && (hdr->flags & SEC_READONLY) == 0)
Nick Clifton e0e1ae
 	    {
Nick Clifton e0e1ae
 	      /* We don't want to put a writable section in a read only
Nick Clifton e0e1ae
-		 segment, unless they are on the same page in memory
Nick Clifton e0e1ae
-		 anyhow and there is no RELRO segment.  We already
Nick Clifton e0e1ae
-		 know that the last section does not bring us past the
Nick Clifton e0e1ae
-		 current section on the page, so the only case in which
Nick Clifton e0e1ae
-		 the new section is not on the same page as the previous
Nick Clifton e0e1ae
-		 section is when the previous section ends precisely on
Nick Clifton e0e1ae
-		 a page boundary.  */
Nick Clifton e0e1ae
+		 segment.  */
Nick Clifton e0e1ae
 	      new_segment = TRUE;
Nick Clifton e0e1ae
 	    }
Nick Clifton e0e1ae
 	  else