[PATCH] Exclude zero size section for RELRO segment match

H.J. Lu hongjiu.lu@intel.com
Thu Feb 15 11:58:00 GMT 2018


When seaching a LOAD segment which overlaps with the RELRO segment, we
need to check the size of the last non-zero size section in the LOAD
segment.

OK for master?

H.J.
---
bfd/

	PR ld/22845
	* elf.c (assign_file_positions_for_non_load_sections): Exclude
	zero size section when seaching a LOAD segment containing a
	section in the RELRO segment.

ld/

	PR ld/22845
	* testsuite/ld-elf/binutils.exp (tls_opts): Add tests for
	"-z noseparate-code" and "-z separate-code".
---
 bfd/elf.c                        | 27 ++++++++++++++++++++-------
 ld/testsuite/ld-elf/binutils.exp |  6 ++++--
 2 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/bfd/elf.c b/bfd/elf.c
index 934052d7fd..e3fd56ad77 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -5895,13 +5895,26 @@ assign_file_positions_for_non_load_sections (bfd *abfd,
 	      for (lm = elf_seg_map (abfd), lp = phdrs;
 		   lm != NULL;
 		   lm = lm->next, lp++)
-		{
-		  if (lp->p_type == PT_LOAD
-		      && lm->count != 0
-		      && lm->sections[lm->count - 1]->vma >= start
-		      && lm->sections[0]->vma < end)
-		    break;
-		}
+		if (lp->p_type == PT_LOAD && lm->count != 0)
+		  {
+		    /* Find the last non-zero size section.  */
+		    int last = lm->count - 1;
+		    for (; last >= 0; last--)
+		      {
+			asection *s = lm->sections[last];
+			/* .tbss sections effectively have zero size.  */
+			if (s->size != 0
+			    && ((s->flags
+				 & (SEC_LOAD | SEC_THREAD_LOCAL))
+				!= SEC_THREAD_LOCAL))
+			  break;
+		      }
+
+		    if (last >= 0
+			&& lm->sections[last]->vma >= start
+			&& lm->sections[0]->vma < end)
+		      break;
+		  }
 	      BFD_ASSERT (lm != NULL);
 
 	      /* Find the section starting the RELRO segment.  */
diff --git a/ld/testsuite/ld-elf/binutils.exp b/ld/testsuite/ld-elf/binutils.exp
index 748699d5d1..290f158710 100644
--- a/ld/testsuite/ld-elf/binutils.exp
+++ b/ld/testsuite/ld-elf/binutils.exp
@@ -186,9 +186,11 @@ if { ![istarget "hppa64-*-*"] } {
     lappend tls_tests "tdata3" "tbss1" "tbss2" "tbss3"
 }
 set tls_opts {
-    ""
+    "-z noseparate-code"
+    "-z separate-code"
     "-z relro"
-    "-shared"
+    "-shared -z noseparate-code"
+    "-shared -z separate-code"
     "-shared -z relro"
     "-z max-page-size=0x100000"
     "-z max-page-size=0x100000 -z common-page-size=0x1000"
-- 
2.14.3



More information about the Binutils mailing list