This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Exclude zero size section for RELRO segment match


On Sat, Feb 17, 2018 at 03:19:26PM -0800, H.J. Lu wrote:
> On Fri, Feb 16, 2018 at 3:44 PM, Alan Modra <amodra@gmail.com> wrote:
> > --- a/bfd/elf.c
> > +++ b/bfd/elf.c
> > @@ -5897,6 +5897,7 @@ assign_file_positions_for_non_load_sections (bfd *abfd,
> >                    lm = lm->next, lp++)
> >                 {
> >                   if (lp->p_type == PT_LOAD
> > +                     && lp->p_memsz != 0
> >                       && lm->count != 0
> >                       && lm->sections[lm->count - 1]->vma >= start
> >                       && lm->sections[0]->vma < end)
> >
> 
> This patch isn't sufficient.  On x86, with this patch, I got
> 
> [hjl@gnu-tools-1 ld]$  ../gas/as-new
> -I/export/gnu/import/git/sources/binutils-gdb/ld/testsuite/ld-elf   -o
> tmpdir/tbss.o /export/gnu/import/git/sources/binutils-gdb/ld/testsuite/ld-elf/tbss.s
> [hjl@gnu-tools-1 ld]$ ../gas/as-new
> -I/export/gnu/import/git/sources/binutils-gdb/ld/testsuite/ld-elf   -o
> tmpdir/frame.o /export/gnu/import/git/sources/binutils-gdb/ld/testsuite/ld-elf/frame.s
> [hjl@gnu-tools-1 ld]$ ./ld-new   -o tmpdir/frame.so -z separate-code
> --shared tmpdir/frame.o tmpdir/tbss.o
> ./ld-new: BFD (GNU Binutils) 2.30.51.20180217 assertion fail
> /export/gnu/import/git/sources/binutils-gdb/bfd/elf.c:5917
> [hjl@gnu-tools-1 ld]$
> 
> My original patch fixes them.

You're correct, but the real underlying problem is that my load
section end calculation was wrong.  That can be fixed without a loop
scanning back through sections.

BTW, the testsuite changes will need some tweaks before they can be
committed, eg.
/home/alan/build/gas/d30v-elf/ld/ld-new: unrecognized option '-z'

	PR 22845
	* elf.c (IS_TBSS): Define.
	(_bfd_elf_map_sections_to_segments): Use IS_TBSS.
	(assign_file_positions_for_non_load_sections): Revert last change.
	Properly calculate load segment limits to compare against relro limits.

diff --git a/bfd/elf.c b/bfd/elf.c
index b069b59..f3a70f1 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -4542,6 +4542,9 @@ elf_modify_segment_map (bfd *abfd,
   return TRUE;
 }
 
+#define IS_TBSS(s) \
+  ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
+
 /* Set up a mapping from BFD sections to program segments.  */
 
 bfd_boolean
@@ -4801,11 +4804,7 @@ _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
 		executable = TRUE;
 	      last_hdr = hdr;
 	      /* .tbss sections effectively have zero size.  */
-	      if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
-		  != SEC_THREAD_LOCAL)
-		last_size = hdr->size;
-	      else
-		last_size = 0;
+	      last_size = !IS_TBSS (hdr) ? hdr->size : 0;
 	      continue;
 	    }
 
@@ -4831,10 +4830,7 @@ _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
 
 	  last_hdr = hdr;
 	  /* .tbss sections effectively have zero size.  */
-	  if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
-	    last_size = hdr->size;
-	  else
-	    last_size = 0;
+	  last_size = !IS_TBSS (hdr) ? hdr->size : 0;
 	  phdr_index = i;
 	  phdr_in_segment = FALSE;
 	}
@@ -4843,8 +4839,7 @@ _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
 	 for .tbss.  */
       if (last_hdr != NULL
 	  && (i - phdr_index != 1
-	      || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
-		  != SEC_THREAD_LOCAL)))
+	      || !IS_TBSS (last_hdr)))
 	{
 	  m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
 	  if (m == NULL)
@@ -5897,9 +5892,11 @@ assign_file_positions_for_non_load_sections (bfd *abfd,
 		   lm = lm->next, lp++)
 		{
 		  if (lp->p_type == PT_LOAD
-		      && lp->p_memsz != 0
 		      && lm->count != 0
-		      && lm->sections[lm->count - 1]->vma >= start
+		      && (lm->sections[lm->count - 1]->vma
+			  + (!IS_TBSS (lm->sections[lm->count - 1])
+			     ? lm->sections[lm->count - 1]->size
+			     : 0)) > start
 		      && lm->sections[0]->vma < end)
 		    break;
 		}

-- 
Alan Modra
Australia Development Lab, IBM


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]