public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* PATCH: PR binutils/3281: objcopy changes PT_GNU_RELRO when there is PT_TLS
@ 2006-09-29  5:38 H. J. Lu
  2006-10-19 16:31 ` H. J. Lu
  0 siblings, 1 reply; 9+ messages in thread
From: H. J. Lu @ 2006-09-29  5:38 UTC (permalink / raw)
  To: binutils

When linker creates PT_GNU_RELRO segment, it sets p_vaddr, p_paddr,
p_offset, p_filesz from the corresponding PT_LOAD segment and
set p_memsz to p_filesz, p_align to 1. My previous fix for PR 3015
only handles p_align for objcopy. PT_GNU_RELRO segment will be
different after objcopy if there is a PT_TLS segment. This patch
puts all PT_GNU_RELRO segment procossing for objcopy into
assign_file_positions_for_non_load_sections and uses the same logic
as linker.


H.J.
----
bfd/

2006-09-28  H.J. Lu  <hongjiu.lu@intel.com>

	PR binutils/3281
	* elf.c (assign_file_positions_for_load_sections): Don't set
	PT_GNU_RELRO segment alignment here.
	(assign_file_positions_for_non_load_sections): Properly set up
	PT_GNU_RELRO segment for copying executable/shared library.

ld/testsuite/

2006-09-28  H.J. Lu  <hongjiu.lu@intel.com>

	PR binutils/3281
	* ld-elf/binutils.exp: Update "-z relro" tests to use relro.s.
	Add "-z relro" tests with TLS for objcopy.

	* ld-elf/relro.s: New file.

--- binutils/bfd/elf.c.relro	2006-09-28 14:40:40.000000000 -0700
+++ binutils/bfd/elf.c	2006-09-28 14:55:39.000000000 -0700
@@ -4604,11 +4604,9 @@ assign_file_positions_for_load_sections 
 		    p->p_memsz += o->offset + o->size;
 		}
 
-	      if (p->p_type == PT_GNU_RELRO)
-		p->p_align = 1;
-	      else if (align > p->p_align
-		       && (p->p_type != PT_LOAD
-			   || (abfd->flags & D_PAGED) == 0))
+	      if (align > p->p_align
+		  && (p->p_type != PT_LOAD
+		      || (abfd->flags & D_PAGED) == 0))
 		p->p_align = align;
 	    }
 
@@ -4734,7 +4732,8 @@ assign_file_positions_for_non_load_secti
       if (m->count != 0)
 	{
 	  if (p->p_type != PT_LOAD
-	      && (p->p_type != PT_NOTE || bfd_get_format (abfd) != bfd_core))
+	      && (p->p_type != PT_NOTE
+		  || bfd_get_format (abfd) != bfd_core))
 	    {
 	      Elf_Internal_Shdr *hdr;
 	      BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
@@ -4745,7 +4744,33 @@ assign_file_positions_for_non_load_secti
 	      if (hdr->sh_type != SHT_NOBITS)
 		p->p_filesz += hdr->sh_size;
 
-	      p->p_offset = m->sections[0]->filepos;
+	      if (p->p_type == PT_GNU_RELRO)
+		{
+		  /* When we get here, we are copying executable
+		     or shared library. But we need to use the same
+		     linker logic.  */
+		  Elf_Internal_Phdr *lp;
+
+		  for (lp = phdrs; lp < phdrs + count; ++lp)
+		    {
+		      if (lp->p_type == PT_LOAD
+			  && lp->p_paddr == p->p_paddr)
+			break;
+		    }
+	  
+		  if (lp < phdrs + count)
+		    {
+		      p->p_filesz += p->p_vaddr - lp->p_vaddr;
+		      p->p_vaddr = lp->p_vaddr;
+		      p->p_offset = lp->p_offset;
+		      p->p_memsz = p->p_filesz;
+		      p->p_align = 1;
+		    }
+		  else
+		    abort ();
+		}
+	      else
+		p->p_offset = m->sections[0]->filepos;
 	    }
 	}
       else
--- binutils/ld/testsuite/ld-elf/binutils.exp.relro	2006-08-16 19:04:53.000000000 -0700
+++ binutils/ld/testsuite/ld-elf/binutils.exp	2006-09-28 14:48:35.000000000 -0700
@@ -101,24 +101,33 @@ binutils_test strip "-shared" maxpage1
 binutils_test objcopy "" maxpage1
 binutils_test objcopy "-shared" maxpage1
 
-binutils_test strip "-z relro" maxpage1
-binutils_test strip "-z relro -shared" maxpage1
-binutils_test objcopy "-z relro" maxpage1
-binutils_test objcopy "-z relro -shared" maxpage1
+binutils_test strip "-z relro" relro
+binutils_test strip "-z relro -shared" relro
+binutils_test objcopy "-z relro" relro
+binutils_test objcopy "-z relro -shared" relro
 
 binutils_test objcopy "" tbss1
+binutils_test objcopy "-z relro" tbss1
 binutils_test objcopy "-shared" tbss1
+binutils_test objcopy "-shared -z relro" tbss1
 binutils_test objcopy "-z max-page-size=0x100000" tbss1
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tbss1
 binutils_test objcopy "" tdata1
+binutils_test objcopy "-z relro" tdata1
 binutils_test objcopy "-shared" tdata1
+binutils_test objcopy "-shared -z relro" tdata1
 binutils_test objcopy "-z max-page-size=0x100000" tdata1
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tdata1
 binutils_test objcopy "" tbss2
+binutils_test objcopy "-z relro" tbss2
 binutils_test objcopy "-shared" tbss2
+binutils_test objcopy "-shared -z relro" tbss2
 binutils_test objcopy "-z max-page-size=0x100000" tbss2
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tbss2
-binutils_test objcopy "-z max-page-size=0x100000" tdata2
+
 binutils_test objcopy "" tdata2
+binutils_test objcopy "-z relro" tdata2
 binutils_test objcopy "-shared" tdata2
+binutils_test objcopy "-shared -z relro" tdata2
+binutils_test objcopy "-z max-page-size=0x100000" tdata2
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tdata2
--- binutils/ld/testsuite/ld-elf/relro.s.relro	2006-09-28 14:40:40.000000000 -0700
+++ binutils/ld/testsuite/ld-elf/relro.s	2006-09-28 14:40:40.000000000 -0700
@@ -0,0 +1,14 @@
+	.globl main
+	.globl start
+	.globl _start
+	.globl __start
+	.text
+main:
+start:
+_start:
+__start:
+	.long	0
+	.data
+	.long	0
+	.section .data.rel.ro,"aw",%progbits
+	.long	0

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: PATCH: PR binutils/3281: objcopy changes PT_GNU_RELRO when there is PT_TLS
  2006-09-29  5:38 PATCH: PR binutils/3281: objcopy changes PT_GNU_RELRO when there is PT_TLS H. J. Lu
@ 2006-10-19 16:31 ` H. J. Lu
  2006-10-20 12:57   ` H. J. Lu
  0 siblings, 1 reply; 9+ messages in thread
From: H. J. Lu @ 2006-10-19 16:31 UTC (permalink / raw)
  To: binutils

On Thu, Sep 28, 2006 at 03:09:16PM -0700, H. J. Lu wrote:
> When linker creates PT_GNU_RELRO segment, it sets p_vaddr, p_paddr,
> p_offset, p_filesz from the corresponding PT_LOAD segment and
> set p_memsz to p_filesz, p_align to 1. My previous fix for PR 3015
> only handles p_align for objcopy. PT_GNU_RELRO segment will be
> different after objcopy if there is a PT_TLS segment. This patch
> puts all PT_GNU_RELRO segment procossing for objcopy into
> assign_file_positions_for_non_load_sections and uses the same logic
> as linker.
> 

Could someone please take a look at it:

http://sourceware.org/ml/binutils/2006-09/msg00376.html

Thanks.


H.J.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: PATCH: PR binutils/3281: objcopy changes PT_GNU_RELRO when there is PT_TLS
  2006-10-19 16:31 ` H. J. Lu
@ 2006-10-20 12:57   ` H. J. Lu
  2006-10-20 13:44     ` H. J. Lu
  0 siblings, 1 reply; 9+ messages in thread
From: H. J. Lu @ 2006-10-20 12:57 UTC (permalink / raw)
  To: binutils

On Thu, Oct 19, 2006 at 08:05:40AM -0700, H. J. Lu wrote:
> On Thu, Sep 28, 2006 at 03:09:16PM -0700, H. J. Lu wrote:
> > When linker creates PT_GNU_RELRO segment, it sets p_vaddr, p_paddr,
> > p_offset, p_filesz from the corresponding PT_LOAD segment and
> > set p_memsz to p_filesz, p_align to 1. My previous fix for PR 3015
> > only handles p_align for objcopy. PT_GNU_RELRO segment will be
> > different after objcopy if there is a PT_TLS segment. This patch
> > puts all PT_GNU_RELRO segment procossing for objcopy into
> > assign_file_positions_for_non_load_sections and uses the same logic
> > as linker.
> > 
> 
> Could someone please take a look at it:
> 
> http://sourceware.org/ml/binutils/2006-09/msg00376.html
> 
> Thanks.
> 

It turns out that there are other problems with PT_GNU_RELRO. SHF_TLS
sections can be in PT_GNU_RELRO segment. We got it wrong. Also we
may create a PT_NULL segment if there are no relro sections. I noticed
them when I ran "make check" with my patch for Linux/mips64 where
GNU_RELRO segment may only contains .tdata section.

This updated patch addresses those 2 problems.


H.J.
---
bfd/

2006-10-20  H.J. Lu  <hongjiu.lu@intel.com>

	PR binutils/3281
	* elf-bfd.h (elf_obj_tdata): Remove relro.

	* elf.c (get_program_header_size): Check info->relro instead
	of elf_tdata (abfd)->relro.
	(_bfd_elf_map_sections_to_segments): Likewise.
	(assign_file_positions_for_load_sections): Don't set
	PT_GNU_RELRO segment alignment here.
	(assign_file_positions_for_non_load_sections): Properly set up
	PT_GNU_RELRO segment for copying executable/shared library.

include/elf/

2006-10-20  H.J. Lu  <hongjiu.lu@intel.com>

	PR binutils/3281
	* internal.h (ELF_IS_SECTION_IN_SEGMENT): Allow SHF_TLS
	sections in PT_GNU_RELRO segments.

ld/

2006-10-20  H.J. Lu  <hongjiu.lu@intel.com>

	PR binutils/3281
	* ldexp.h (ldexp_control): Add relro, relro_start_stat and
	relro_end_stat.

	* ldexp.c (fold_binary): Set expld.dataseg.relro to
	exp_dataseg_relro_start or exp_dataseg_relro_end when
	seeing DATA_SEGMENT_ALIGN or DATA_SEGMENT_RELRO_END,
	respectively.

	* ldlang.c (lang_size_sections_1): Properly set
	expld.dataseg.relro_start_stat and
	expld.dataseg.relro_end_stat.
	(size_relro_section_callback): New function.
	(lang_size_relro_sections_1): Likewise.
	(lang_size_relro_sections): Likewise.
	(lang_process): Call lang_size_relro_sections for
	non-relocatable link.

ld/testsuite/

2006-10-20  H.J. Lu  <hongjiu.lu@intel.com>

	PR binutils/3281
	* ld-elf/binutils.exp: Update "-z relro" tests to use relro.s.
	Add "-z relro" tests with TLS for objcopy.

	* ld-elf/relro.s: New file.

--- binutils/bfd/elf-bfd.h.relro	2006-10-19 22:42:57.000000000 -0700
+++ binutils/bfd/elf-bfd.h	2006-10-19 22:42:57.000000000 -0700
@@ -1375,9 +1375,6 @@ struct elf_obj_tdata
   /* Segment flags for the PT_GNU_STACK segment.  */
   unsigned int stack_flags;
 
-  /* Should the PT_GNU_RELRO segment be emitted?  */
-  bfd_boolean relro;
-
   /* Symbol version definitions in external objects.  */
   Elf_Internal_Verdef *verdef;
 
--- binutils/bfd/elf.c.relro	2006-10-19 22:42:57.000000000 -0700
+++ binutils/bfd/elf.c	2006-10-19 22:42:57.000000000 -0700
@@ -3633,7 +3633,7 @@ get_program_header_size (bfd *abfd, stru
       /* We need a PT_DYNAMIC segment.  */
       ++segs;
       
-      if (elf_tdata (abfd)->relro)
+      if (info->relro)
 	{
 	  /* We need a PT_GNU_RELRO segment only when there is a
 	     PT_DYNAMIC segment.  */
@@ -4121,7 +4121,7 @@ _bfd_elf_map_sections_to_segments (bfd *
 	  pm = &m->next;
 	}
 
-      if (dynsec != NULL && elf_tdata (abfd)->relro)
+      if (dynsec != NULL && info->relro)
 	{
 	  /* We make a PT_GNU_RELRO segment only when there is a
 	     PT_DYNAMIC segment.  */
@@ -4604,11 +4604,9 @@ assign_file_positions_for_load_sections 
 		    p->p_memsz += o->offset + o->size;
 		}
 
-	      if (p->p_type == PT_GNU_RELRO)
-		p->p_align = 1;
-	      else if (align > p->p_align
-		       && (p->p_type != PT_LOAD
-			   || (abfd->flags & D_PAGED) == 0))
+	      if (align > p->p_align
+		  && (p->p_type != PT_LOAD
+		      || (abfd->flags & D_PAGED) == 0))
 		p->p_align = align;
 	    }
 
@@ -4735,7 +4733,8 @@ assign_file_positions_for_non_load_secti
       if (m->count != 0)
 	{
 	  if (p->p_type != PT_LOAD
-	      && (p->p_type != PT_NOTE || bfd_get_format (abfd) != bfd_core))
+	      && (p->p_type != PT_NOTE
+		  || bfd_get_format (abfd) != bfd_core))
 	    {
 	      Elf_Internal_Shdr *hdr;
 	      BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
@@ -4746,7 +4745,33 @@ assign_file_positions_for_non_load_secti
 	      if (hdr->sh_type != SHT_NOBITS)
 		p->p_filesz += hdr->sh_size;
 
-	      p->p_offset = m->sections[0]->filepos;
+	      if (p->p_type == PT_GNU_RELRO)
+		{
+		  /* When we get here, we are copying executable
+		     or shared library. But we need to use the same
+		     linker logic.  */
+		  Elf_Internal_Phdr *lp;
+
+		  for (lp = phdrs; lp < phdrs + count; ++lp)
+		    {
+		      if (lp->p_type == PT_LOAD
+			  && lp->p_paddr == p->p_paddr)
+			break;
+		    }
+	  
+		  if (lp < phdrs + count)
+		    {
+		      p->p_filesz += p->p_vaddr - lp->p_vaddr;
+		      p->p_vaddr = lp->p_vaddr;
+		      p->p_offset = lp->p_offset;
+		      p->p_memsz = p->p_filesz;
+		      p->p_align = 1;
+		    }
+		  else
+		    abort ();
+		}
+	      else
+		p->p_offset = m->sections[0]->filepos;
 	    }
 	}
       else
--- binutils/bfd/elflink.c.relro	2006-10-19 22:42:57.000000000 -0700
+++ binutils/bfd/elflink.c	2006-10-19 22:42:57.000000000 -0700
@@ -5266,7 +5266,6 @@ bfd_elf_size_dynamic_sections (bfd *outp
   if (!is_elf_hash_table (info->hash))
     return TRUE;
 
-  elf_tdata (output_bfd)->relro = info->relro;
   if (info->execstack)
     elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
   else if (info->noexecstack)
--- binutils/include/elf/internal.h.relro	2006-05-31 22:40:24.000000000 -0700
+++ binutils/include/elf/internal.h	2006-10-19 22:42:57.000000000 -0700
@@ -264,11 +264,12 @@ struct elf_segment_map
      || segment->p_type == PT_TLS) ? sec_hdr->sh_size : 0)
 
 /* Decide if the given sec_hdr is in the given segment.  PT_TLS segment
-   contains only SHF_TLS sections.  Only PT_LOAD and PT_TLS segments
-   can contain SHF_TLS sections.  */
+   contains only SHF_TLS sections.  Only PT_LOAD, PT_GNU_RELRO and
+   PT_TLS segments can contain SHF_TLS sections.  */
 #define ELF_IS_SECTION_IN_SEGMENT(sec_hdr, segment)		\
   (((((sec_hdr->sh_flags & SHF_TLS) != 0)			\
      && (segment->p_type == PT_TLS				\
+	 || segment->p_type == PT_GNU_RELRO			\
 	 || segment->p_type == PT_LOAD))			\
     || ((sec_hdr->sh_flags & SHF_TLS) == 0			\
 	&& segment->p_type != PT_TLS))				\
--- binutils/ld/ldexp.c.relro	2006-08-22 18:35:31.000000000 -0700
+++ binutils/ld/ldexp.c	2006-10-19 22:42:57.000000000 -0700
@@ -384,6 +384,7 @@ fold_binary (etree_type *tree)
 	      break;
 
 	    case DATA_SEGMENT_ALIGN:
+	      expld.dataseg.relro = exp_dataseg_relro_start;
 	      if (expld.phase != lang_first_phase_enum
 		  && expld.section == bfd_abs_section_ptr
 		  && (expld.dataseg.phase == exp_dataseg_none
@@ -419,6 +420,7 @@ fold_binary (etree_type *tree)
 	      break;
 
 	    case DATA_SEGMENT_RELRO_END:
+	      expld.dataseg.relro = exp_dataseg_relro_end;
 	      if (expld.phase != lang_first_phase_enum
 		  && (expld.dataseg.phase == exp_dataseg_align_seen
 		      || expld.dataseg.phase == exp_dataseg_adjust
--- binutils/ld/ldexp.h.relro	2005-08-05 06:52:13.000000000 -0700
+++ binutils/ld/ldexp.h	2006-10-19 22:42:57.000000000 -0700
@@ -97,6 +97,8 @@ typedef enum {
   lang_final_phase_enum
 } lang_phase_type;
 
+union lang_statement_union;
+
 struct ldexp_control {
   /* Modify expression evaluation depending on this.  */
   lang_phase_type phase;
@@ -124,6 +126,15 @@ struct ldexp_control {
     } phase;
 
     bfd_vma base, min_base, relro_end, end, pagesize, maxpagesize;
+
+    enum {
+      exp_dataseg_relro_none,
+      exp_dataseg_relro_start,
+      exp_dataseg_relro_end,
+    } relro;
+
+    union lang_statement_union *relro_start_stat;
+    union lang_statement_union *relro_end_stat;
   } dataseg;
 };
 
--- binutils/ld/ldlang.c.relro	2006-10-19 22:42:57.000000000 -0700
+++ binutils/ld/ldlang.c	2006-10-19 23:03:21.000000000 -0700
@@ -4627,10 +4627,32 @@ lang_size_sections_1
 	    bfd_vma newdot = dot;
 	    etree_type *tree = s->assignment_statement.exp;
 
+	    expld.dataseg.relro = exp_dataseg_relro_none;
+
 	    exp_fold_tree (tree,
 			   output_section_statement->bfd_section,
 			   &newdot);
 
+	    if (expld.dataseg.relro == exp_dataseg_relro_start)
+	      {
+		if (!expld.dataseg.relro_start_stat)
+		  expld.dataseg.relro_start_stat = s;
+		else
+		  {
+		    ASSERT (expld.dataseg.relro_start_stat == s);
+		  }
+	      }
+	    else if (expld.dataseg.relro == exp_dataseg_relro_end)
+	      {
+		if (!expld.dataseg.relro_end_stat)
+		  expld.dataseg.relro_end_stat = s;
+		else
+		  {
+		    ASSERT (expld.dataseg.relro_end_stat == s);
+		  }
+	      }
+	    expld.dataseg.relro = exp_dataseg_relro_none;
+
 	    /* This symbol is relative to this section.  */
 	    if ((tree->type.node_class == etree_provided 
 		 || tree->type.node_class == etree_assign)
@@ -5607,6 +5629,74 @@ lang_gc_sections (void)
     bfd_gc_sections (output_bfd, &link_info);
 }
 
+/* Worker for lang_size_relro_sections_1.  */
+
+static void
+size_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
+			     struct wildcard_list *sec ATTRIBUTE_UNUSED,
+			     asection *section,
+			     lang_input_statement_type *file ATTRIBUTE_UNUSED,
+			     void *data)
+{
+  /* .tbss sections effectively have zero size.  */
+  if ((section->flags & SEC_HAS_CONTENTS) != 0
+      || (section->flags & SEC_THREAD_LOCAL) == 0)
+    {
+      bfd_boolean *has_relro_section = (bfd_boolean *) data;
+      *has_relro_section |= section->size != 0;
+    }
+}
+
+/* Iterate over sections for relro sections.  */
+
+static void
+lang_size_relro_sections_1 (lang_statement_union_type *s,
+			    bfd_boolean *has_relro_section)
+{
+  for (; s != NULL; s = s->header.next)
+    {
+      if (s == expld.dataseg.relro_end_stat)
+	break;
+
+      switch (s->header.type)
+	{
+	case lang_wild_statement_enum:
+	  walk_wild (&s->wild_statement,
+		     size_relro_section_callback,
+		     has_relro_section);
+	  break;
+	case lang_constructors_statement_enum:
+	  lang_size_relro_sections_1 (constructor_list.head,
+				      has_relro_section);
+	  break;
+	case lang_output_section_statement_enum:
+	  lang_size_relro_sections_1 (s->output_section_statement.children.head,
+				      has_relro_section);
+	  break;
+	case lang_group_statement_enum:
+	  lang_size_relro_sections_1 (s->group_statement.children.head,
+				      has_relro_section);
+	  break;
+	default:
+	  break;
+	}
+    }
+}
+
+static void
+lang_size_relro_sections (void)
+{
+  bfd_boolean has_relro_section = FALSE;
+
+  /* Check all sections in the link script.  */
+
+  lang_size_relro_sections_1 (expld.dataseg.relro_start_stat,
+			      &has_relro_section);
+
+  if (!has_relro_section)
+    link_info.relro = FALSE;
+}
+
 /* Relax all sections until bfd_relax_section gives up.  */
 
 static void
@@ -5732,6 +5822,10 @@ lang_process (void)
      section positions, since they will affect SIZEOF_HEADERS.  */
   lang_record_phdrs ();
 
+  /* Check relro sections.  */
+  if (! link_info.relocatable)
+    lang_size_relro_sections ();
+
   /* Size up the sections.  */
   lang_size_sections (NULL, !command_line.relax);
 
--- binutils/ld/testsuite/ld-elf/binutils.exp.relro	2006-08-16 19:04:53.000000000 -0700
+++ binutils/ld/testsuite/ld-elf/binutils.exp	2006-10-19 22:42:57.000000000 -0700
@@ -101,24 +101,33 @@ binutils_test strip "-shared" maxpage1
 binutils_test objcopy "" maxpage1
 binutils_test objcopy "-shared" maxpage1
 
-binutils_test strip "-z relro" maxpage1
-binutils_test strip "-z relro -shared" maxpage1
-binutils_test objcopy "-z relro" maxpage1
-binutils_test objcopy "-z relro -shared" maxpage1
+binutils_test strip "-z relro" relro
+binutils_test strip "-z relro -shared" relro
+binutils_test objcopy "-z relro" relro
+binutils_test objcopy "-z relro -shared" relro
 
 binutils_test objcopy "" tbss1
+binutils_test objcopy "-z relro" tbss1
 binutils_test objcopy "-shared" tbss1
+binutils_test objcopy "-shared -z relro" tbss1
 binutils_test objcopy "-z max-page-size=0x100000" tbss1
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tbss1
 binutils_test objcopy "" tdata1
+binutils_test objcopy "-z relro" tdata1
 binutils_test objcopy "-shared" tdata1
+binutils_test objcopy "-shared -z relro" tdata1
 binutils_test objcopy "-z max-page-size=0x100000" tdata1
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tdata1
 binutils_test objcopy "" tbss2
+binutils_test objcopy "-z relro" tbss2
 binutils_test objcopy "-shared" tbss2
+binutils_test objcopy "-shared -z relro" tbss2
 binutils_test objcopy "-z max-page-size=0x100000" tbss2
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tbss2
-binutils_test objcopy "-z max-page-size=0x100000" tdata2
+
 binutils_test objcopy "" tdata2
+binutils_test objcopy "-z relro" tdata2
 binutils_test objcopy "-shared" tdata2
+binutils_test objcopy "-shared -z relro" tdata2
+binutils_test objcopy "-z max-page-size=0x100000" tdata2
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tdata2
--- binutils/ld/testsuite/ld-elf/relro.s.relro	2006-10-19 22:42:57.000000000 -0700
+++ binutils/ld/testsuite/ld-elf/relro.s	2006-10-19 22:42:57.000000000 -0700
@@ -0,0 +1,14 @@
+	.globl main
+	.globl start
+	.globl _start
+	.globl __start
+	.text
+main:
+start:
+_start:
+__start:
+	.long	0
+	.data
+	.long	0
+	.section .data.rel.ro,"aw",%progbits
+	.long	0

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: PATCH: PR binutils/3281: objcopy changes PT_GNU_RELRO when there is PT_TLS
  2006-10-20 12:57   ` H. J. Lu
@ 2006-10-20 13:44     ` H. J. Lu
  2006-10-24  7:14       ` H. J. Lu
  0 siblings, 1 reply; 9+ messages in thread
From: H. J. Lu @ 2006-10-20 13:44 UTC (permalink / raw)
  To: binutils

On Thu, Oct 19, 2006 at 11:10:02PM -0700, H. J. Lu wrote:
> On Thu, Oct 19, 2006 at 08:05:40AM -0700, H. J. Lu wrote:
> > On Thu, Sep 28, 2006 at 03:09:16PM -0700, H. J. Lu wrote:
> > > When linker creates PT_GNU_RELRO segment, it sets p_vaddr, p_paddr,
> > > p_offset, p_filesz from the corresponding PT_LOAD segment and
> > > set p_memsz to p_filesz, p_align to 1. My previous fix for PR 3015
> > > only handles p_align for objcopy. PT_GNU_RELRO segment will be
> > > different after objcopy if there is a PT_TLS segment. This patch
> > > puts all PT_GNU_RELRO segment procossing for objcopy into
> > > assign_file_positions_for_non_load_sections and uses the same logic
> > > as linker.
> > > 
> > 
> > Could someone please take a look at it:
> > 
> > http://sourceware.org/ml/binutils/2006-09/msg00376.html
> > 
> > Thanks.
> > 
> 
> It turns out that there are other problems with PT_GNU_RELRO. SHF_TLS
> sections can be in PT_GNU_RELRO segment. We got it wrong. Also we
> may create a PT_NULL segment if there are no relro sections. I noticed
> them when I ran "make check" with my patch for Linux/mips64 where
> GNU_RELRO segment may only contains .tdata section.
> 
> This updated patch addresses those 2 problems.
> 
> 

We should ignore discarded sections.


H.J.
----
bfd/

2006-10-20  H.J. Lu  <hongjiu.lu@intel.com>

	PR binutils/3281
	* elf-bfd.h (elf_obj_tdata): Remove relro.

	* elf.c (get_program_header_size): Check info->relro instead
	of elf_tdata (abfd)->relro.
	(_bfd_elf_map_sections_to_segments): Likewise.
	(assign_file_positions_for_load_sections): Don't set
	PT_GNU_RELRO segment alignment here.
	(assign_file_positions_for_non_load_sections): Properly set up
	PT_GNU_RELRO segment for copying executable/shared library.

include/elf/

2006-10-20  H.J. Lu  <hongjiu.lu@intel.com>

	PR binutils/3281
	* internal.h (ELF_IS_SECTION_IN_SEGMENT): Allow SHF_TLS
	sections in PT_GNU_RELRO segments.

ld/

2006-10-20  H.J. Lu  <hongjiu.lu@intel.com>

	PR binutils/3281
	* ldexp.h (ldexp_control): Add relro, relro_start_stat and
	relro_end_stat.

	* ldexp.c (fold_binary): Set expld.dataseg.relro to
	exp_dataseg_relro_start or exp_dataseg_relro_end when
	seeing DATA_SEGMENT_ALIGN or DATA_SEGMENT_RELRO_END,
	respectively.

	* ldlang.c (lang_size_sections_1): Properly set
	expld.dataseg.relro_start_stat and
	expld.dataseg.relro_end_stat.
	(size_relro_section_callback): New function.
	(lang_size_relro_sections_1): Likewise.
	(lang_size_relro_sections): Likewise.
	(lang_process): Call lang_size_relro_sections for
	non-relocatable link.

ld/testsuite/

2006-10-20  H.J. Lu  <hongjiu.lu@intel.com>

	PR binutils/3281
	* ld-elf/binutils.exp: Update "-z relro" tests to use relro.s.
	Add "-z relro" tests with TLS for objcopy.

	* ld-elf/relro.s: New file.

--- binutils/bfd/elf-bfd.h.relro	2006-10-19 22:42:57.000000000 -0700
+++ binutils/bfd/elf-bfd.h	2006-10-19 22:42:57.000000000 -0700
@@ -1375,9 +1375,6 @@ struct elf_obj_tdata
   /* Segment flags for the PT_GNU_STACK segment.  */
   unsigned int stack_flags;
 
-  /* Should the PT_GNU_RELRO segment be emitted?  */
-  bfd_boolean relro;
-
   /* Symbol version definitions in external objects.  */
   Elf_Internal_Verdef *verdef;
 
--- binutils/bfd/elf.c.relro	2006-10-19 22:42:57.000000000 -0700
+++ binutils/bfd/elf.c	2006-10-19 22:42:57.000000000 -0700
@@ -3633,7 +3633,7 @@ get_program_header_size (bfd *abfd, stru
       /* We need a PT_DYNAMIC segment.  */
       ++segs;
       
-      if (elf_tdata (abfd)->relro)
+      if (info->relro)
 	{
 	  /* We need a PT_GNU_RELRO segment only when there is a
 	     PT_DYNAMIC segment.  */
@@ -4121,7 +4121,7 @@ _bfd_elf_map_sections_to_segments (bfd *
 	  pm = &m->next;
 	}
 
-      if (dynsec != NULL && elf_tdata (abfd)->relro)
+      if (dynsec != NULL && info->relro)
 	{
 	  /* We make a PT_GNU_RELRO segment only when there is a
 	     PT_DYNAMIC segment.  */
@@ -4604,11 +4604,9 @@ assign_file_positions_for_load_sections 
 		    p->p_memsz += o->offset + o->size;
 		}
 
-	      if (p->p_type == PT_GNU_RELRO)
-		p->p_align = 1;
-	      else if (align > p->p_align
-		       && (p->p_type != PT_LOAD
-			   || (abfd->flags & D_PAGED) == 0))
+	      if (align > p->p_align
+		  && (p->p_type != PT_LOAD
+		      || (abfd->flags & D_PAGED) == 0))
 		p->p_align = align;
 	    }
 
@@ -4735,7 +4733,8 @@ assign_file_positions_for_non_load_secti
       if (m->count != 0)
 	{
 	  if (p->p_type != PT_LOAD
-	      && (p->p_type != PT_NOTE || bfd_get_format (abfd) != bfd_core))
+	      && (p->p_type != PT_NOTE
+		  || bfd_get_format (abfd) != bfd_core))
 	    {
 	      Elf_Internal_Shdr *hdr;
 	      BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
@@ -4746,7 +4745,33 @@ assign_file_positions_for_non_load_secti
 	      if (hdr->sh_type != SHT_NOBITS)
 		p->p_filesz += hdr->sh_size;
 
-	      p->p_offset = m->sections[0]->filepos;
+	      if (p->p_type == PT_GNU_RELRO)
+		{
+		  /* When we get here, we are copying executable
+		     or shared library. But we need to use the same
+		     linker logic.  */
+		  Elf_Internal_Phdr *lp;
+
+		  for (lp = phdrs; lp < phdrs + count; ++lp)
+		    {
+		      if (lp->p_type == PT_LOAD
+			  && lp->p_paddr == p->p_paddr)
+			break;
+		    }
+	  
+		  if (lp < phdrs + count)
+		    {
+		      p->p_filesz += p->p_vaddr - lp->p_vaddr;
+		      p->p_vaddr = lp->p_vaddr;
+		      p->p_offset = lp->p_offset;
+		      p->p_memsz = p->p_filesz;
+		      p->p_align = 1;
+		    }
+		  else
+		    abort ();
+		}
+	      else
+		p->p_offset = m->sections[0]->filepos;
 	    }
 	}
       else
--- binutils/bfd/elflink.c.relro	2006-10-19 22:42:57.000000000 -0700
+++ binutils/bfd/elflink.c	2006-10-19 22:42:57.000000000 -0700
@@ -5266,7 +5266,6 @@ bfd_elf_size_dynamic_sections (bfd *outp
   if (!is_elf_hash_table (info->hash))
     return TRUE;
 
-  elf_tdata (output_bfd)->relro = info->relro;
   if (info->execstack)
     elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
   else if (info->noexecstack)
--- binutils/include/elf/internal.h.relro	2006-05-31 22:40:24.000000000 -0700
+++ binutils/include/elf/internal.h	2006-10-19 22:42:57.000000000 -0700
@@ -264,11 +264,12 @@ struct elf_segment_map
      || segment->p_type == PT_TLS) ? sec_hdr->sh_size : 0)
 
 /* Decide if the given sec_hdr is in the given segment.  PT_TLS segment
-   contains only SHF_TLS sections.  Only PT_LOAD and PT_TLS segments
-   can contain SHF_TLS sections.  */
+   contains only SHF_TLS sections.  Only PT_LOAD, PT_GNU_RELRO and
+   PT_TLS segments can contain SHF_TLS sections.  */
 #define ELF_IS_SECTION_IN_SEGMENT(sec_hdr, segment)		\
   (((((sec_hdr->sh_flags & SHF_TLS) != 0)			\
      && (segment->p_type == PT_TLS				\
+	 || segment->p_type == PT_GNU_RELRO			\
 	 || segment->p_type == PT_LOAD))			\
     || ((sec_hdr->sh_flags & SHF_TLS) == 0			\
 	&& segment->p_type != PT_TLS))				\
--- binutils/ld/ldexp.c.relro	2006-08-22 18:35:31.000000000 -0700
+++ binutils/ld/ldexp.c	2006-10-19 22:42:57.000000000 -0700
@@ -384,6 +384,7 @@ fold_binary (etree_type *tree)
 	      break;
 
 	    case DATA_SEGMENT_ALIGN:
+	      expld.dataseg.relro = exp_dataseg_relro_start;
 	      if (expld.phase != lang_first_phase_enum
 		  && expld.section == bfd_abs_section_ptr
 		  && (expld.dataseg.phase == exp_dataseg_none
@@ -419,6 +420,7 @@ fold_binary (etree_type *tree)
 	      break;
 
 	    case DATA_SEGMENT_RELRO_END:
+	      expld.dataseg.relro = exp_dataseg_relro_end;
 	      if (expld.phase != lang_first_phase_enum
 		  && (expld.dataseg.phase == exp_dataseg_align_seen
 		      || expld.dataseg.phase == exp_dataseg_adjust
--- binutils/ld/ldexp.h.relro	2005-08-05 06:52:13.000000000 -0700
+++ binutils/ld/ldexp.h	2006-10-19 22:42:57.000000000 -0700
@@ -97,6 +97,8 @@ typedef enum {
   lang_final_phase_enum
 } lang_phase_type;
 
+union lang_statement_union;
+
 struct ldexp_control {
   /* Modify expression evaluation depending on this.  */
   lang_phase_type phase;
@@ -124,6 +126,15 @@ struct ldexp_control {
     } phase;
 
     bfd_vma base, min_base, relro_end, end, pagesize, maxpagesize;
+
+    enum {
+      exp_dataseg_relro_none,
+      exp_dataseg_relro_start,
+      exp_dataseg_relro_end,
+    } relro;
+
+    union lang_statement_union *relro_start_stat;
+    union lang_statement_union *relro_end_stat;
   } dataseg;
 };
 
--- binutils/ld/ldlang.c.relro	2006-10-19 22:42:57.000000000 -0700
+++ binutils/ld/ldlang.c	2006-10-19 23:28:23.000000000 -0700
@@ -4627,10 +4627,32 @@ lang_size_sections_1
 	    bfd_vma newdot = dot;
 	    etree_type *tree = s->assignment_statement.exp;
 
+	    expld.dataseg.relro = exp_dataseg_relro_none;
+
 	    exp_fold_tree (tree,
 			   output_section_statement->bfd_section,
 			   &newdot);
 
+	    if (expld.dataseg.relro == exp_dataseg_relro_start)
+	      {
+		if (!expld.dataseg.relro_start_stat)
+		  expld.dataseg.relro_start_stat = s;
+		else
+		  {
+		    ASSERT (expld.dataseg.relro_start_stat == s);
+		  }
+	      }
+	    else if (expld.dataseg.relro == exp_dataseg_relro_end)
+	      {
+		if (!expld.dataseg.relro_end_stat)
+		  expld.dataseg.relro_end_stat = s;
+		else
+		  {
+		    ASSERT (expld.dataseg.relro_end_stat == s);
+		  }
+	      }
+	    expld.dataseg.relro = exp_dataseg_relro_none;
+
 	    /* This symbol is relative to this section.  */
 	    if ((tree->type.node_class == etree_provided 
 		 || tree->type.node_class == etree_assign)
@@ -5607,6 +5629,76 @@ lang_gc_sections (void)
     bfd_gc_sections (output_bfd, &link_info);
 }
 
+/* Worker for lang_size_relro_sections_1.  */
+
+static void
+size_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
+			     struct wildcard_list *sec ATTRIBUTE_UNUSED,
+			     asection *section,
+			     lang_input_statement_type *file ATTRIBUTE_UNUSED,
+			     void *data)
+{
+  /* Discarded and .tbss sections effectively have zero size.  */
+  if (section->output_section != NULL
+      && section->output_section->owner == output_bfd
+      && ((section->flags & SEC_HAS_CONTENTS) != 0
+	  || (section->flags & SEC_THREAD_LOCAL) == 0))
+    {
+      bfd_boolean *has_relro_section = (bfd_boolean *) data;
+      *has_relro_section |= section->size != 0;
+    }
+}
+
+/* Iterate over sections for relro sections.  */
+
+static void
+lang_size_relro_sections_1 (lang_statement_union_type *s,
+			    bfd_boolean *has_relro_section)
+{
+  for (; s != NULL; s = s->header.next)
+    {
+      if (s == expld.dataseg.relro_end_stat)
+	break;
+
+      switch (s->header.type)
+	{
+	case lang_wild_statement_enum:
+	  walk_wild (&s->wild_statement,
+		     size_relro_section_callback,
+		     has_relro_section);
+	  break;
+	case lang_constructors_statement_enum:
+	  lang_size_relro_sections_1 (constructor_list.head,
+				      has_relro_section);
+	  break;
+	case lang_output_section_statement_enum:
+	  lang_size_relro_sections_1 (s->output_section_statement.children.head,
+				      has_relro_section);
+	  break;
+	case lang_group_statement_enum:
+	  lang_size_relro_sections_1 (s->group_statement.children.head,
+				      has_relro_section);
+	  break;
+	default:
+	  break;
+	}
+    }
+}
+
+static void
+lang_size_relro_sections (void)
+{
+  bfd_boolean has_relro_section = FALSE;
+
+  /* Check all sections in the link script.  */
+
+  lang_size_relro_sections_1 (expld.dataseg.relro_start_stat,
+			      &has_relro_section);
+
+  if (!has_relro_section)
+    link_info.relro = FALSE;
+}
+
 /* Relax all sections until bfd_relax_section gives up.  */
 
 static void
@@ -5732,6 +5824,10 @@ lang_process (void)
      section positions, since they will affect SIZEOF_HEADERS.  */
   lang_record_phdrs ();
 
+  /* Check relro sections.  */
+  if (! link_info.relocatable)
+    lang_size_relro_sections ();
+
   /* Size up the sections.  */
   lang_size_sections (NULL, !command_line.relax);
 
--- binutils/ld/testsuite/ld-elf/binutils.exp.relro	2006-08-16 19:04:53.000000000 -0700
+++ binutils/ld/testsuite/ld-elf/binutils.exp	2006-10-19 22:42:57.000000000 -0700
@@ -101,24 +101,33 @@ binutils_test strip "-shared" maxpage1
 binutils_test objcopy "" maxpage1
 binutils_test objcopy "-shared" maxpage1
 
-binutils_test strip "-z relro" maxpage1
-binutils_test strip "-z relro -shared" maxpage1
-binutils_test objcopy "-z relro" maxpage1
-binutils_test objcopy "-z relro -shared" maxpage1
+binutils_test strip "-z relro" relro
+binutils_test strip "-z relro -shared" relro
+binutils_test objcopy "-z relro" relro
+binutils_test objcopy "-z relro -shared" relro
 
 binutils_test objcopy "" tbss1
+binutils_test objcopy "-z relro" tbss1
 binutils_test objcopy "-shared" tbss1
+binutils_test objcopy "-shared -z relro" tbss1
 binutils_test objcopy "-z max-page-size=0x100000" tbss1
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tbss1
 binutils_test objcopy "" tdata1
+binutils_test objcopy "-z relro" tdata1
 binutils_test objcopy "-shared" tdata1
+binutils_test objcopy "-shared -z relro" tdata1
 binutils_test objcopy "-z max-page-size=0x100000" tdata1
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tdata1
 binutils_test objcopy "" tbss2
+binutils_test objcopy "-z relro" tbss2
 binutils_test objcopy "-shared" tbss2
+binutils_test objcopy "-shared -z relro" tbss2
 binutils_test objcopy "-z max-page-size=0x100000" tbss2
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tbss2
-binutils_test objcopy "-z max-page-size=0x100000" tdata2
+
 binutils_test objcopy "" tdata2
+binutils_test objcopy "-z relro" tdata2
 binutils_test objcopy "-shared" tdata2
+binutils_test objcopy "-shared -z relro" tdata2
+binutils_test objcopy "-z max-page-size=0x100000" tdata2
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tdata2
--- binutils/ld/testsuite/ld-elf/relro.s.relro	2006-10-19 22:42:57.000000000 -0700
+++ binutils/ld/testsuite/ld-elf/relro.s	2006-10-19 22:42:57.000000000 -0700
@@ -0,0 +1,14 @@
+	.globl main
+	.globl start
+	.globl _start
+	.globl __start
+	.text
+main:
+start:
+_start:
+__start:
+	.long	0
+	.data
+	.long	0
+	.section .data.rel.ro,"aw",%progbits
+	.long	0

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: PATCH: PR binutils/3281: objcopy changes PT_GNU_RELRO when there is PT_TLS
  2006-10-20 13:44     ` H. J. Lu
@ 2006-10-24  7:14       ` H. J. Lu
  2006-10-27 15:00         ` Alan Modra
  0 siblings, 1 reply; 9+ messages in thread
From: H. J. Lu @ 2006-10-24  7:14 UTC (permalink / raw)
  To: binutils

On Thu, Oct 19, 2006 at 11:30:37PM -0700, H. J. Lu wrote:
> On Thu, Oct 19, 2006 at 11:10:02PM -0700, H. J. Lu wrote:
> > On Thu, Oct 19, 2006 at 08:05:40AM -0700, H. J. Lu wrote:
> > > On Thu, Sep 28, 2006 at 03:09:16PM -0700, H. J. Lu wrote:
> > > > When linker creates PT_GNU_RELRO segment, it sets p_vaddr, p_paddr,
> > > > p_offset, p_filesz from the corresponding PT_LOAD segment and
> > > > set p_memsz to p_filesz, p_align to 1. My previous fix for PR 3015
> > > > only handles p_align for objcopy. PT_GNU_RELRO segment will be
> > > > different after objcopy if there is a PT_TLS segment. This patch
> > > > puts all PT_GNU_RELRO segment procossing for objcopy into
> > > > assign_file_positions_for_non_load_sections and uses the same logic
> > > > as linker.
> > > > 
> > > 
> > > Could someone please take a look at it:
> > > 
> > > http://sourceware.org/ml/binutils/2006-09/msg00376.html
> > > 
> > > Thanks.
> > > 
> > 
> > It turns out that there are other problems with PT_GNU_RELRO. SHF_TLS
> > sections can be in PT_GNU_RELRO segment. We got it wrong. Also we
> > may create a PT_NULL segment if there are no relro sections. I noticed
> > them when I ran "make check" with my patch for Linux/mips64 where
> > GNU_RELRO segment may only contains .tdata section.
> > 
> > This updated patch addresses those 2 problems.
> > 
> > 
> 
> We should ignore discarded sections.
> 

We should skip discarded, excluded and ignored sections.


H.J.
----
bfd/

2006-10-20  H.J. Lu  <hongjiu.lu@intel.com>

	PR binutils/3281
	* elf-bfd.h (elf_obj_tdata): Remove relro.

	* elf.c (get_program_header_size): Check info->relro instead
	of elf_tdata (abfd)->relro.
	(_bfd_elf_map_sections_to_segments): Likewise.
	(assign_file_positions_for_load_sections): Don't set
	PT_GNU_RELRO segment alignment here.
	(assign_file_positions_for_non_load_sections): Properly set up
	PT_GNU_RELRO segment for copying executable/shared library.

include/elf/

2006-10-20  H.J. Lu  <hongjiu.lu@intel.com>

	PR binutils/3281
	* internal.h (ELF_IS_SECTION_IN_SEGMENT): Allow SHF_TLS
	sections in PT_GNU_RELRO segments.

ld/

2006-10-20  H.J. Lu  <hongjiu.lu@intel.com>

	PR binutils/3281
	* ldexp.h (ldexp_control): Add relro, relro_start_stat and
	relro_end_stat.

	* ldexp.c (fold_binary): Set expld.dataseg.relro to
	exp_dataseg_relro_start or exp_dataseg_relro_end when
	seeing DATA_SEGMENT_ALIGN or DATA_SEGMENT_RELRO_END,
	respectively.

	* ldlang.c (lang_size_sections_1): Properly set
	expld.dataseg.relro_start_stat and
	expld.dataseg.relro_end_stat.
	(size_relro_section_callback): New function.
	(lang_size_relro_sections_1): Likewise.
	(lang_size_relro_sections): Likewise.
	(lang_process): Call lang_size_relro_sections for
	non-relocatable link.

ld/testsuite/

2006-10-20  H.J. Lu  <hongjiu.lu@intel.com>

	PR binutils/3281
	* ld-elf/binutils.exp: Update "-z relro" tests to use relro.s.
	Add "-z relro" tests with TLS for objcopy.

	* ld-elf/relro.s: New file.

--- binutils/bfd/elf-bfd.h.relro	2006-10-20 09:28:37.000000000 -0700
+++ binutils/bfd/elf-bfd.h	2006-10-20 09:28:38.000000000 -0700
@@ -1375,9 +1375,6 @@ struct elf_obj_tdata
   /* Segment flags for the PT_GNU_STACK segment.  */
   unsigned int stack_flags;
 
-  /* Should the PT_GNU_RELRO segment be emitted?  */
-  bfd_boolean relro;
-
   /* Symbol version definitions in external objects.  */
   Elf_Internal_Verdef *verdef;
 
--- binutils/bfd/elf.c.relro	2006-10-20 09:28:38.000000000 -0700
+++ binutils/bfd/elf.c	2006-10-20 09:28:38.000000000 -0700
@@ -3637,7 +3637,7 @@ get_program_header_size (bfd *abfd, stru
       /* We need a PT_DYNAMIC segment.  */
       ++segs;
       
-      if (elf_tdata (abfd)->relro)
+      if (info->relro)
 	{
 	  /* We need a PT_GNU_RELRO segment only when there is a
 	     PT_DYNAMIC segment.  */
@@ -4223,7 +4223,7 @@ _bfd_elf_map_sections_to_segments (bfd *
 	  pm = &m->next;
 	}
 
-      if (dynsec != NULL && elf_tdata (abfd)->relro)
+      if (dynsec != NULL && info->relro)
 	{
 	  /* We make a PT_GNU_RELRO segment only when there is a
 	     PT_DYNAMIC segment.  */
@@ -4707,11 +4707,9 @@ assign_file_positions_for_load_sections 
 		    p->p_memsz += o->offset + o->size;
 		}
 
-	      if (p->p_type == PT_GNU_RELRO)
-		p->p_align = 1;
-	      else if (align > p->p_align
-		       && (p->p_type != PT_LOAD
-			   || (abfd->flags & D_PAGED) == 0))
+	      if (align > p->p_align
+		  && (p->p_type != PT_LOAD
+		      || (abfd->flags & D_PAGED) == 0))
 		p->p_align = align;
 	    }
 
@@ -4838,7 +4836,8 @@ assign_file_positions_for_non_load_secti
       if (m->count != 0)
 	{
 	  if (p->p_type != PT_LOAD
-	      && (p->p_type != PT_NOTE || bfd_get_format (abfd) != bfd_core))
+	      && (p->p_type != PT_NOTE
+		  || bfd_get_format (abfd) != bfd_core))
 	    {
 	      Elf_Internal_Shdr *hdr;
 	      BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
@@ -4849,7 +4848,33 @@ assign_file_positions_for_non_load_secti
 	      if (hdr->sh_type != SHT_NOBITS)
 		p->p_filesz += hdr->sh_size;
 
-	      p->p_offset = m->sections[0]->filepos;
+	      if (p->p_type == PT_GNU_RELRO)
+		{
+		  /* When we get here, we are copying executable
+		     or shared library. But we need to use the same
+		     linker logic.  */
+		  Elf_Internal_Phdr *lp;
+
+		  for (lp = phdrs; lp < phdrs + count; ++lp)
+		    {
+		      if (lp->p_type == PT_LOAD
+			  && lp->p_paddr == p->p_paddr)
+			break;
+		    }
+	  
+		  if (lp < phdrs + count)
+		    {
+		      p->p_filesz += p->p_vaddr - lp->p_vaddr;
+		      p->p_vaddr = lp->p_vaddr;
+		      p->p_offset = lp->p_offset;
+		      p->p_memsz = p->p_filesz;
+		      p->p_align = 1;
+		    }
+		  else
+		    abort ();
+		}
+	      else
+		p->p_offset = m->sections[0]->filepos;
 	    }
 	}
       else
--- binutils/bfd/elflink.c.relro	2006-10-20 09:28:38.000000000 -0700
+++ binutils/bfd/elflink.c	2006-10-20 09:28:38.000000000 -0700
@@ -5282,7 +5282,6 @@ bfd_elf_size_dynamic_sections (bfd *outp
   if (!is_elf_hash_table (info->hash))
     return TRUE;
 
-  elf_tdata (output_bfd)->relro = info->relro;
   if (info->execstack)
     elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
   else if (info->noexecstack)
--- binutils/include/elf/internal.h.relro	2006-09-18 10:08:02.000000000 -0700
+++ binutils/include/elf/internal.h	2006-10-20 09:28:38.000000000 -0700
@@ -264,11 +264,12 @@ struct elf_segment_map
      || segment->p_type == PT_TLS) ? sec_hdr->sh_size : 0)
 
 /* Decide if the given sec_hdr is in the given segment.  PT_TLS segment
-   contains only SHF_TLS sections.  Only PT_LOAD and PT_TLS segments
-   can contain SHF_TLS sections.  */
+   contains only SHF_TLS sections.  Only PT_LOAD, PT_GNU_RELRO and
+   PT_TLS segments can contain SHF_TLS sections.  */
 #define ELF_IS_SECTION_IN_SEGMENT(sec_hdr, segment)		\
   (((((sec_hdr->sh_flags & SHF_TLS) != 0)			\
      && (segment->p_type == PT_TLS				\
+	 || segment->p_type == PT_GNU_RELRO			\
 	 || segment->p_type == PT_LOAD))			\
     || ((sec_hdr->sh_flags & SHF_TLS) == 0			\
 	&& segment->p_type != PT_TLS))				\
--- binutils/ld/ldexp.c.relro	2006-09-18 10:08:08.000000000 -0700
+++ binutils/ld/ldexp.c	2006-10-20 09:28:38.000000000 -0700
@@ -384,6 +384,7 @@ fold_binary (etree_type *tree)
 	      break;
 
 	    case DATA_SEGMENT_ALIGN:
+	      expld.dataseg.relro = exp_dataseg_relro_start;
 	      if (expld.phase != lang_first_phase_enum
 		  && expld.section == bfd_abs_section_ptr
 		  && (expld.dataseg.phase == exp_dataseg_none
@@ -419,6 +420,7 @@ fold_binary (etree_type *tree)
 	      break;
 
 	    case DATA_SEGMENT_RELRO_END:
+	      expld.dataseg.relro = exp_dataseg_relro_end;
 	      if (expld.phase != lang_first_phase_enum
 		  && (expld.dataseg.phase == exp_dataseg_align_seen
 		      || expld.dataseg.phase == exp_dataseg_adjust
--- binutils/ld/ldexp.h.relro	2006-09-18 10:08:08.000000000 -0700
+++ binutils/ld/ldexp.h	2006-10-20 09:28:38.000000000 -0700
@@ -97,6 +97,8 @@ typedef enum {
   lang_final_phase_enum
 } lang_phase_type;
 
+union lang_statement_union;
+
 struct ldexp_control {
   /* Modify expression evaluation depending on this.  */
   lang_phase_type phase;
@@ -124,6 +126,15 @@ struct ldexp_control {
     } phase;
 
     bfd_vma base, min_base, relro_end, end, pagesize, maxpagesize;
+
+    enum {
+      exp_dataseg_relro_none,
+      exp_dataseg_relro_start,
+      exp_dataseg_relro_end,
+    } relro;
+
+    union lang_statement_union *relro_start_stat;
+    union lang_statement_union *relro_end_stat;
   } dataseg;
 };
 
--- binutils/ld/ldlang.c.relro	2006-10-20 09:28:37.000000000 -0700
+++ binutils/ld/ldlang.c	2006-10-20 09:53:48.000000000 -0700
@@ -4633,10 +4633,32 @@ lang_size_sections_1
 	    bfd_vma newdot = dot;
 	    etree_type *tree = s->assignment_statement.exp;
 
+	    expld.dataseg.relro = exp_dataseg_relro_none;
+
 	    exp_fold_tree (tree,
 			   output_section_statement->bfd_section,
 			   &newdot);
 
+	    if (expld.dataseg.relro == exp_dataseg_relro_start)
+	      {
+		if (!expld.dataseg.relro_start_stat)
+		  expld.dataseg.relro_start_stat = s;
+		else
+		  {
+		    ASSERT (expld.dataseg.relro_start_stat == s);
+		  }
+	      }
+	    else if (expld.dataseg.relro == exp_dataseg_relro_end)
+	      {
+		if (!expld.dataseg.relro_end_stat)
+		  expld.dataseg.relro_end_stat = s;
+		else
+		  {
+		    ASSERT (expld.dataseg.relro_end_stat == s);
+		  }
+	      }
+	    expld.dataseg.relro = exp_dataseg_relro_none;
+
 	    /* This symbol is relative to this section.  */
 	    if ((tree->type.node_class == etree_provided 
 		 || tree->type.node_class == etree_assign)
@@ -5613,6 +5635,81 @@ lang_gc_sections (void)
     bfd_gc_sections (output_bfd, &link_info);
 }
 
+/* Worker for lang_size_relro_sections_1.  */
+
+static void
+size_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
+			     struct wildcard_list *sec ATTRIBUTE_UNUSED,
+			     asection *section,
+			     lang_input_statement_type *file ATTRIBUTE_UNUSED,
+			     void *data)
+{
+  /* Discarded, excluded and ignored sections effectively have zero
+     size.  */
+  if (section->output_section != NULL
+      && section->output_section->owner == output_bfd
+      && (section->output_section->flags & SEC_EXCLUDE) == 0
+      && !IGNORE_SECTION (section)
+      && section->size != 0)
+    {
+      bfd_boolean *has_relro_section = (bfd_boolean *) data;
+      *has_relro_section = TRUE;
+    }
+}
+
+/* Iterate over sections for relro sections.  */
+
+static void
+lang_size_relro_sections_1 (lang_statement_union_type *s,
+			    bfd_boolean *has_relro_section)
+{
+  if (*has_relro_section)
+    return;
+
+  for (; s != NULL; s = s->header.next)
+    {
+      if (s == expld.dataseg.relro_end_stat)
+	break;
+
+      switch (s->header.type)
+	{
+	case lang_wild_statement_enum:
+	  walk_wild (&s->wild_statement,
+		     size_relro_section_callback,
+		     has_relro_section);
+	  break;
+	case lang_constructors_statement_enum:
+	  lang_size_relro_sections_1 (constructor_list.head,
+				      has_relro_section);
+	  break;
+	case lang_output_section_statement_enum:
+	  lang_size_relro_sections_1 (s->output_section_statement.children.head,
+				      has_relro_section);
+	  break;
+	case lang_group_statement_enum:
+	  lang_size_relro_sections_1 (s->group_statement.children.head,
+				      has_relro_section);
+	  break;
+	default:
+	  break;
+	}
+    }
+}
+
+static void
+lang_size_relro_sections (void)
+{
+  bfd_boolean has_relro_section = FALSE;
+
+  /* Check all sections in the link script.  */
+
+  lang_size_relro_sections_1 (expld.dataseg.relro_start_stat,
+			      &has_relro_section);
+
+  if (!has_relro_section)
+    link_info.relro = FALSE;
+}
+
 /* Relax all sections until bfd_relax_section gives up.  */
 
 static void
@@ -5738,6 +5835,10 @@ lang_process (void)
      section positions, since they will affect SIZEOF_HEADERS.  */
   lang_record_phdrs ();
 
+  /* Check relro sections.  */
+  if (link_info.relro && ! link_info.relocatable)
+    lang_size_relro_sections ();
+
   /* Size up the sections.  */
   lang_size_sections (NULL, !command_line.relax);
 
--- binutils/ld/testsuite/ld-elf/binutils.exp.relro	2006-09-18 10:08:09.000000000 -0700
+++ binutils/ld/testsuite/ld-elf/binutils.exp	2006-10-20 09:28:38.000000000 -0700
@@ -101,24 +101,33 @@ binutils_test strip "-shared" maxpage1
 binutils_test objcopy "" maxpage1
 binutils_test objcopy "-shared" maxpage1
 
-binutils_test strip "-z relro" maxpage1
-binutils_test strip "-z relro -shared" maxpage1
-binutils_test objcopy "-z relro" maxpage1
-binutils_test objcopy "-z relro -shared" maxpage1
+binutils_test strip "-z relro" relro
+binutils_test strip "-z relro -shared" relro
+binutils_test objcopy "-z relro" relro
+binutils_test objcopy "-z relro -shared" relro
 
 binutils_test objcopy "" tbss1
+binutils_test objcopy "-z relro" tbss1
 binutils_test objcopy "-shared" tbss1
+binutils_test objcopy "-shared -z relro" tbss1
 binutils_test objcopy "-z max-page-size=0x100000" tbss1
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tbss1
 binutils_test objcopy "" tdata1
+binutils_test objcopy "-z relro" tdata1
 binutils_test objcopy "-shared" tdata1
+binutils_test objcopy "-shared -z relro" tdata1
 binutils_test objcopy "-z max-page-size=0x100000" tdata1
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tdata1
 binutils_test objcopy "" tbss2
+binutils_test objcopy "-z relro" tbss2
 binutils_test objcopy "-shared" tbss2
+binutils_test objcopy "-shared -z relro" tbss2
 binutils_test objcopy "-z max-page-size=0x100000" tbss2
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tbss2
-binutils_test objcopy "-z max-page-size=0x100000" tdata2
+
 binutils_test objcopy "" tdata2
+binutils_test objcopy "-z relro" tdata2
 binutils_test objcopy "-shared" tdata2
+binutils_test objcopy "-shared -z relro" tdata2
+binutils_test objcopy "-z max-page-size=0x100000" tdata2
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tdata2
--- binutils/ld/testsuite/ld-elf/relro.s.relro	2006-10-20 09:28:38.000000000 -0700
+++ binutils/ld/testsuite/ld-elf/relro.s	2006-10-20 09:28:38.000000000 -0700
@@ -0,0 +1,14 @@
+	.globl main
+	.globl start
+	.globl _start
+	.globl __start
+	.text
+main:
+start:
+_start:
+__start:
+	.long	0
+	.data
+	.long	0
+	.section .data.rel.ro,"aw",%progbits
+	.long	0

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: PATCH: PR binutils/3281: objcopy changes PT_GNU_RELRO when there is PT_TLS
  2006-10-24  7:14       ` H. J. Lu
@ 2006-10-27 15:00         ` Alan Modra
  2006-10-28 22:19           ` H. J. Lu
  0 siblings, 1 reply; 9+ messages in thread
From: Alan Modra @ 2006-10-27 15:00 UTC (permalink / raw)
  To: H. J. Lu; +Cc: binutils

On Mon, Oct 23, 2006 at 10:55:59PM -0700, H. J. Lu wrote:
> +	      if (p->p_type == PT_GNU_RELRO)
> +		{
> +		  /* When we get here, we are copying executable
> +		     or shared library. But we need to use the same
> +		     linker logic.  */
> +		  Elf_Internal_Phdr *lp;
> +
> +		  for (lp = phdrs; lp < phdrs + count; ++lp)
> +		    {
> +		      if (lp->p_type == PT_LOAD
> +			  && lp->p_paddr == p->p_paddr)
> +			break;
> +		    }
> +	  
> +		  if (lp < phdrs + count)
> +		    {
> +		      p->p_filesz += p->p_vaddr - lp->p_vaddr;
> +		      p->p_vaddr = lp->p_vaddr;
> +		      p->p_offset = lp->p_offset;
> +		      p->p_memsz = p->p_filesz;
> +		      p->p_align = 1;
> +		    }
> +		  else
> +		    abort ();
> +		}

I don't think this is going to work in all cases.  objcopy can do some
really nasty things as far as GNU_RELRO segment is concerned, deleting
sections, adding sections, changing section lmas.  I think instead
you should set the GNU_RELRO segment info from the section map, and
try to get the section map correct in rewrite_elf_program_header.  I
noticed at least one problem with rewrite_elf_program_header "Step
Three":  It should not be allowed to split a GNU_RELRO segment into
two due to gaps.

> +lang_size_relro_sections (void)

Should probably be called lang_find_relro_sections.  You aren't doing
any sizing here.

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: PATCH: PR binutils/3281: objcopy changes PT_GNU_RELRO when there is PT_TLS
  2006-10-27 15:00         ` Alan Modra
@ 2006-10-28 22:19           ` H. J. Lu
  2006-10-28 22:59             ` H. J. Lu
  0 siblings, 1 reply; 9+ messages in thread
From: H. J. Lu @ 2006-10-28 22:19 UTC (permalink / raw)
  To: binutils

On Fri, Oct 27, 2006 at 02:35:56PM +0930, Alan Modra wrote:
> On Mon, Oct 23, 2006 at 10:55:59PM -0700, H. J. Lu wrote:
> > +	      if (p->p_type == PT_GNU_RELRO)
> > +		{
> > +		  /* When we get here, we are copying executable
> > +		     or shared library. But we need to use the same
> > +		     linker logic.  */
> > +		  Elf_Internal_Phdr *lp;
> > +
> > +		  for (lp = phdrs; lp < phdrs + count; ++lp)
> > +		    {
> > +		      if (lp->p_type == PT_LOAD
> > +			  && lp->p_paddr == p->p_paddr)
> > +			break;
> > +		    }
> > +	  
> > +		  if (lp < phdrs + count)
> > +		    {
> > +		      p->p_filesz += p->p_vaddr - lp->p_vaddr;
> > +		      p->p_vaddr = lp->p_vaddr;
> > +		      p->p_offset = lp->p_offset;
> > +		      p->p_memsz = p->p_filesz;
> > +		      p->p_align = 1;
> > +		    }
> > +		  else
> > +		    abort ();
> > +		}
> 
> I don't think this is going to work in all cases.  objcopy can do some
> really nasty things as far as GNU_RELRO segment is concerned, deleting
> sections, adding sections, changing section lmas.  I think instead
> you should set the GNU_RELRO segment info from the section map, and
> try to get the section map correct in rewrite_elf_program_header.  I
> noticed at least one problem with rewrite_elf_program_header "Step
> Three":  It should not be allowed to split a GNU_RELRO segment into
> two due to gaps.
> 

My patch works with deleted sections, which won't reach "Step Three".
I am not sure if we should keep GNU_RELRO segment when we add section
or change section lmas.

> > +lang_size_relro_sections (void)
> 
> Should probably be called lang_find_relro_sections.  You aren't doing
> any sizing here.

I will change it.


H.J.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: PATCH: PR binutils/3281: objcopy changes PT_GNU_RELRO when there is PT_TLS
  2006-10-28 22:19           ` H. J. Lu
@ 2006-10-28 22:59             ` H. J. Lu
  2006-10-29  0:01               ` H. J. Lu
  0 siblings, 1 reply; 9+ messages in thread
From: H. J. Lu @ 2006-10-28 22:59 UTC (permalink / raw)
  To: binutils

On Fri, Oct 27, 2006 at 10:49:20AM -0700, H. J. Lu wrote:
> On Fri, Oct 27, 2006 at 02:35:56PM +0930, Alan Modra wrote:
> > On Mon, Oct 23, 2006 at 10:55:59PM -0700, H. J. Lu wrote:
> > > +	      if (p->p_type == PT_GNU_RELRO)
> > > +		{
> > > +		  /* When we get here, we are copying executable
> > > +		     or shared library. But we need to use the same
> > > +		     linker logic.  */
> > > +		  Elf_Internal_Phdr *lp;
> > > +
> > > +		  for (lp = phdrs; lp < phdrs + count; ++lp)
> > > +		    {
> > > +		      if (lp->p_type == PT_LOAD
> > > +			  && lp->p_paddr == p->p_paddr)
> > > +			break;
> > > +		    }
> > > +	  
> > > +		  if (lp < phdrs + count)
> > > +		    {
> > > +		      p->p_filesz += p->p_vaddr - lp->p_vaddr;
> > > +		      p->p_vaddr = lp->p_vaddr;
> > > +		      p->p_offset = lp->p_offset;
> > > +		      p->p_memsz = p->p_filesz;
> > > +		      p->p_align = 1;
> > > +		    }
> > > +		  else
> > > +		    abort ();
> > > +		}
> > 
> > I don't think this is going to work in all cases.  objcopy can do some
> > really nasty things as far as GNU_RELRO segment is concerned, deleting
> > sections, adding sections, changing section lmas.  I think instead
> > you should set the GNU_RELRO segment info from the section map, and
> > try to get the section map correct in rewrite_elf_program_header.  I
> > noticed at least one problem with rewrite_elf_program_header "Step
> > Three":  It should not be allowed to split a GNU_RELRO segment into
> > two due to gaps.
> > 
> 
> My patch works with deleted sections, which won't reach "Step Three".
> I am not sure if we should keep GNU_RELRO segment when we add section
> or change section lmas.

I think we should delete GNU_RELRO segment if a section is added or
modified.

> 
> > > +lang_size_relro_sections (void)
> > 
> > Should probably be called lang_find_relro_sections.  You aren't doing
> > any sizing here.
> 
> I will change it.
> 
> 

Here is the updated patch.


H.J.
----
bfd/

2006-10-20  H.J. Lu  <hongjiu.lu@intel.com>

	PR binutils/3281
	* elf-bfd.h (elf_obj_tdata): Remove relro.

	* elf.c (get_program_header_size): Check info->relro instead
	of elf_tdata (abfd)->relro.
	(_bfd_elf_map_sections_to_segments): Likewise.
	(assign_file_positions_for_load_sections): Don't set
	PT_GNU_RELRO segment alignment here.
	(assign_file_positions_for_non_load_sections): Properly set up
	PT_GNU_RELRO segment for copying executable/shared library.
	(elf_section_status): New enum.
	(rewrite_elf_program_header): Add elf_section_status. Remove
	PT_GNU_RELRO segment if a section is modified.
	(copy_private_bfd_data): Updated rewrite_elf_program_header
	call.

include/elf/

2006-10-20  H.J. Lu  <hongjiu.lu@intel.com>

	PR binutils/3281
	* internal.h (ELF_IS_SECTION_IN_SEGMENT): Allow SHF_TLS
	sections in PT_GNU_RELRO segments.

ld/

2006-10-20  H.J. Lu  <hongjiu.lu@intel.com>

	PR binutils/3281
	* ldexp.h (ldexp_control): Add relro, relro_start_stat and
	relro_end_stat.

	* ldexp.c (fold_binary): Set expld.dataseg.relro to
	exp_dataseg_relro_start or exp_dataseg_relro_end when
	seeing DATA_SEGMENT_ALIGN or DATA_SEGMENT_RELRO_END,
	respectively.

	* ldlang.c (lang_size_sections_1): Properly set
	expld.dataseg.relro_start_stat and
	expld.dataseg.relro_end_stat.
	(find_relro_section_callback): New function.
	(lang_find_relro_sections_1): Likewise.
	(lang_find_relro_sections): Likewise.
	(lang_process): Call lang_find_relro_sections for
	non-relocatable link.

ld/testsuite/

2006-10-20  H.J. Lu  <hongjiu.lu@intel.com>

	PR binutils/3281
	* ld-elf/binutils.exp: Update "-z relro" tests to use relro.s.
	Add "-z relro" tests with TLS for objcopy.

	* ld-elf/relro.s: New file.

--- binutils/bfd/elf-bfd.h.relro	2006-10-27 10:20:57.000000000 -0700
+++ binutils/bfd/elf-bfd.h	2006-10-27 10:20:57.000000000 -0700
@@ -1375,9 +1375,6 @@ struct elf_obj_tdata
   /* Segment flags for the PT_GNU_STACK segment.  */
   unsigned int stack_flags;
 
-  /* Should the PT_GNU_RELRO segment be emitted?  */
-  bfd_boolean relro;
-
   /* Symbol version definitions in external objects.  */
   Elf_Internal_Verdef *verdef;
 
--- binutils/bfd/elf.c.relro	2006-10-27 10:20:57.000000000 -0700
+++ binutils/bfd/elf.c	2006-10-27 11:24:53.000000000 -0700
@@ -3631,7 +3631,7 @@ get_program_header_size (bfd *abfd, stru
       /* We need a PT_DYNAMIC segment.  */
       ++segs;
       
-      if (elf_tdata (abfd)->relro)
+      if (info->relro)
 	{
 	  /* We need a PT_GNU_RELRO segment only when there is a
 	     PT_DYNAMIC segment.  */
@@ -4119,7 +4119,7 @@ _bfd_elf_map_sections_to_segments (bfd *
 	  pm = &m->next;
 	}
 
-      if (dynsec != NULL && elf_tdata (abfd)->relro)
+      if (dynsec != NULL && info->relro)
 	{
 	  /* We make a PT_GNU_RELRO segment only when there is a
 	     PT_DYNAMIC segment.  */
@@ -4602,11 +4602,9 @@ assign_file_positions_for_load_sections 
 		    p->p_memsz += o->offset + o->size;
 		}
 
-	      if (p->p_type == PT_GNU_RELRO)
-		p->p_align = 1;
-	      else if (align > p->p_align
-		       && (p->p_type != PT_LOAD
-			   || (abfd->flags & D_PAGED) == 0))
+	      if (align > p->p_align
+		  && (p->p_type != PT_LOAD
+		      || (abfd->flags & D_PAGED) == 0))
 		p->p_align = align;
 	    }
 
@@ -4733,7 +4731,8 @@ assign_file_positions_for_non_load_secti
       if (m->count != 0)
 	{
 	  if (p->p_type != PT_LOAD
-	      && (p->p_type != PT_NOTE || bfd_get_format (abfd) != bfd_core))
+	      && (p->p_type != PT_NOTE
+		  || bfd_get_format (abfd) != bfd_core))
 	    {
 	      Elf_Internal_Shdr *hdr;
 	      BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
@@ -4744,7 +4743,33 @@ assign_file_positions_for_non_load_secti
 	      if (hdr->sh_type != SHT_NOBITS)
 		p->p_filesz += hdr->sh_size;
 
-	      p->p_offset = m->sections[0]->filepos;
+	      if (p->p_type == PT_GNU_RELRO)
+		{
+		  /* When we get here, we are copying executable
+		     or shared library. But we need to use the same
+		     linker logic.  */
+		  Elf_Internal_Phdr *lp;
+
+		  for (lp = phdrs; lp < phdrs + count; ++lp)
+		    {
+		      if (lp->p_type == PT_LOAD
+			  && lp->p_paddr == p->p_paddr)
+			break;
+		    }
+	  
+		  if (lp < phdrs + count)
+		    {
+		      p->p_filesz += p->p_vaddr - lp->p_vaddr;
+		      p->p_vaddr = lp->p_vaddr;
+		      p->p_offset = lp->p_offset;
+		      p->p_memsz = p->p_filesz;
+		      p->p_align = 1;
+		    }
+		  else
+		    abort ();
+		}
+	      else
+		p->p_offset = m->sections[0]->filepos;
 	    }
 	}
       else
@@ -5172,8 +5197,17 @@ _bfd_elf_symbol_from_bfd_symbol (bfd *ab
 
 /* Rewrite program header information.  */
 
+enum elf_section_status
+{
+  unknown,
+  added,
+  removed,
+  modified
+};
+
 static bfd_boolean
-rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
+rewrite_elf_program_header (bfd *ibfd, bfd *obfd,
+			    enum elf_section_status status)
 {
   Elf_Internal_Ehdr *iehdr;
   struct elf_segment_map *map;
@@ -5321,7 +5355,14 @@ rewrite_elf_program_header (bfd *ibfd, b
 	    }
 
       if (segment->p_type != PT_LOAD)
-	continue;
+	{
+	  /* If a section is added or mofied, remove PT_GNU_RELRO
+	     segment.  */
+	  if (status != removed &&
+	      segment->p_type == PT_GNU_RELRO)
+	    segment->p_type = PT_NULL;
+	  continue;
+	}
 
       /* Determine if this segment overlaps any previous segments.  */
       for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
@@ -5858,6 +5899,12 @@ copy_elf_program_header (bfd *ibfd, bfd 
 static bfd_boolean
 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
 {
+  enum elf_section_status status;
+  Elf_Internal_Phdr *segment;
+  asection *section, *osec;
+  unsigned int i, num_segments;
+  Elf_Internal_Shdr *this_hdr;
+
   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
     return TRUE;
@@ -5865,14 +5912,11 @@ copy_private_bfd_data (bfd *ibfd, bfd *o
   if (elf_tdata (ibfd)->phdr == NULL)
     return TRUE;
 
+  status = unknown;
   if (ibfd->xvec == obfd->xvec)
     {
       /* Check if any sections in the input BFD covered by ELF program
 	 header are changed.  */
-      Elf_Internal_Phdr *segment;
-      asection *section, *osec;
-      unsigned int i, num_segments;
-      Elf_Internal_Shdr *this_hdr;
 
       /* Initialize the segment mark field.  */
       for (section = obfd->sections; section != NULL;
@@ -5917,7 +5961,10 @@ copy_private_bfd_data (bfd *ibfd, bfd *o
 	   section = section->next)
 	{
 	  if (section->segment_mark == FALSE)
-	    goto rewrite;
+	    { 
+	      status = added;
+	      goto rewrite;
+	    }
 	  else
 	    section->segment_mark = FALSE;
 	}
@@ -5926,7 +5973,46 @@ copy_private_bfd_data (bfd *ibfd, bfd *o
     }
 
 rewrite:
-  return rewrite_elf_program_header (ibfd, obfd);
+  if (status == unknown)
+    {
+      /* We need to find out how we are changed.  */
+      num_segments = elf_elfheader (ibfd)->e_phnum;
+      for (i = 0, segment = elf_tdata (ibfd)->phdr;
+	   i < num_segments;
+	   i++, segment++)
+	{
+	  for (section = ibfd->sections;
+	       section != NULL; section = section->next)
+	    {
+	      /* We mark the output section so that we know it comes
+		 from the input BFD.  */
+	      osec = section->output_section;
+	      if (osec)
+		osec->segment_mark = TRUE;
+
+	      /* Check if this section is covered by the segment.  */
+	      this_hdr = &(elf_section_data(section)->this_hdr);
+	      if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
+		{
+		  if (osec == NULL)
+		    status = removed;
+		  else if (section->flags != osec->flags
+			   || section->lma != osec->lma
+			   || section->vma != osec->vma
+			   || section->size != osec->size
+			   || section->rawsize != osec->rawsize
+			   || section->alignment_power != osec->alignment_power)
+		    {
+		      /* Stop if a section is modified.  */
+		      status = modified;
+		      break;
+		    }
+		}
+	    }
+	}
+    }
+  BFD_ASSERT (status != unknown);
+  return rewrite_elf_program_header (ibfd, obfd, status);
 }
 
 /* Initialize private output section information from input section.  */
--- binutils/bfd/elflink.c.relro	2006-10-27 10:20:57.000000000 -0700
+++ binutils/bfd/elflink.c	2006-10-27 10:20:57.000000000 -0700
@@ -5266,7 +5266,6 @@ bfd_elf_size_dynamic_sections (bfd *outp
   if (!is_elf_hash_table (info->hash))
     return TRUE;
 
-  elf_tdata (output_bfd)->relro = info->relro;
   if (info->execstack)
     elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
   else if (info->noexecstack)
--- binutils/include/elf/internal.h.relro	2006-05-31 22:40:24.000000000 -0700
+++ binutils/include/elf/internal.h	2006-10-27 10:20:57.000000000 -0700
@@ -264,11 +264,12 @@ struct elf_segment_map
      || segment->p_type == PT_TLS) ? sec_hdr->sh_size : 0)
 
 /* Decide if the given sec_hdr is in the given segment.  PT_TLS segment
-   contains only SHF_TLS sections.  Only PT_LOAD and PT_TLS segments
-   can contain SHF_TLS sections.  */
+   contains only SHF_TLS sections.  Only PT_LOAD, PT_GNU_RELRO and
+   PT_TLS segments can contain SHF_TLS sections.  */
 #define ELF_IS_SECTION_IN_SEGMENT(sec_hdr, segment)		\
   (((((sec_hdr->sh_flags & SHF_TLS) != 0)			\
      && (segment->p_type == PT_TLS				\
+	 || segment->p_type == PT_GNU_RELRO			\
 	 || segment->p_type == PT_LOAD))			\
     || ((sec_hdr->sh_flags & SHF_TLS) == 0			\
 	&& segment->p_type != PT_TLS))				\
--- binutils/ld/ldexp.c.relro	2006-08-22 18:35:31.000000000 -0700
+++ binutils/ld/ldexp.c	2006-10-27 10:20:57.000000000 -0700
@@ -384,6 +384,7 @@ fold_binary (etree_type *tree)
 	      break;
 
 	    case DATA_SEGMENT_ALIGN:
+	      expld.dataseg.relro = exp_dataseg_relro_start;
 	      if (expld.phase != lang_first_phase_enum
 		  && expld.section == bfd_abs_section_ptr
 		  && (expld.dataseg.phase == exp_dataseg_none
@@ -419,6 +420,7 @@ fold_binary (etree_type *tree)
 	      break;
 
 	    case DATA_SEGMENT_RELRO_END:
+	      expld.dataseg.relro = exp_dataseg_relro_end;
 	      if (expld.phase != lang_first_phase_enum
 		  && (expld.dataseg.phase == exp_dataseg_align_seen
 		      || expld.dataseg.phase == exp_dataseg_adjust
--- binutils/ld/ldexp.h.relro	2005-08-05 06:52:13.000000000 -0700
+++ binutils/ld/ldexp.h	2006-10-27 10:20:57.000000000 -0700
@@ -97,6 +97,8 @@ typedef enum {
   lang_final_phase_enum
 } lang_phase_type;
 
+union lang_statement_union;
+
 struct ldexp_control {
   /* Modify expression evaluation depending on this.  */
   lang_phase_type phase;
@@ -124,6 +126,15 @@ struct ldexp_control {
     } phase;
 
     bfd_vma base, min_base, relro_end, end, pagesize, maxpagesize;
+
+    enum {
+      exp_dataseg_relro_none,
+      exp_dataseg_relro_start,
+      exp_dataseg_relro_end,
+    } relro;
+
+    union lang_statement_union *relro_start_stat;
+    union lang_statement_union *relro_end_stat;
   } dataseg;
 };
 
--- binutils/ld/ldlang.c.relro	2006-10-27 10:20:57.000000000 -0700
+++ binutils/ld/ldlang.c	2006-10-27 10:20:57.000000000 -0700
@@ -4626,10 +4626,32 @@ lang_size_sections_1
 	    bfd_vma newdot = dot;
 	    etree_type *tree = s->assignment_statement.exp;
 
+	    expld.dataseg.relro = exp_dataseg_relro_none;
+
 	    exp_fold_tree (tree,
 			   output_section_statement->bfd_section,
 			   &newdot);
 
+	    if (expld.dataseg.relro == exp_dataseg_relro_start)
+	      {
+		if (!expld.dataseg.relro_start_stat)
+		  expld.dataseg.relro_start_stat = s;
+		else
+		  {
+		    ASSERT (expld.dataseg.relro_start_stat == s);
+		  }
+	      }
+	    else if (expld.dataseg.relro == exp_dataseg_relro_end)
+	      {
+		if (!expld.dataseg.relro_end_stat)
+		  expld.dataseg.relro_end_stat = s;
+		else
+		  {
+		    ASSERT (expld.dataseg.relro_end_stat == s);
+		  }
+	      }
+	    expld.dataseg.relro = exp_dataseg_relro_none;
+
 	    /* This symbol is relative to this section.  */
 	    if ((tree->type.node_class == etree_provided
 		 || tree->type.node_class == etree_assign)
@@ -5606,6 +5628,81 @@ lang_gc_sections (void)
     bfd_gc_sections (output_bfd, &link_info);
 }
 
+/* Worker for lang_find_relro_sections_1.  */
+
+static void
+find_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
+			     struct wildcard_list *sec ATTRIBUTE_UNUSED,
+			     asection *section,
+			     lang_input_statement_type *file ATTRIBUTE_UNUSED,
+			     void *data)
+{
+  /* Discarded, excluded and ignored sections effectively have zero
+     size.  */
+  if (section->output_section != NULL
+      && section->output_section->owner == output_bfd
+      && (section->output_section->flags & SEC_EXCLUDE) == 0
+      && !IGNORE_SECTION (section)
+      && section->size != 0)
+    {
+      bfd_boolean *has_relro_section = (bfd_boolean *) data;
+      *has_relro_section = TRUE;
+    }
+}
+
+/* Iterate over sections for relro sections.  */
+
+static void
+lang_find_relro_sections_1 (lang_statement_union_type *s,
+			    bfd_boolean *has_relro_section)
+{
+  if (*has_relro_section)
+    return;
+
+  for (; s != NULL; s = s->header.next)
+    {
+      if (s == expld.dataseg.relro_end_stat)
+	break;
+
+      switch (s->header.type)
+	{
+	case lang_wild_statement_enum:
+	  walk_wild (&s->wild_statement,
+		     find_relro_section_callback,
+		     has_relro_section);
+	  break;
+	case lang_constructors_statement_enum:
+	  lang_find_relro_sections_1 (constructor_list.head,
+				      has_relro_section);
+	  break;
+	case lang_output_section_statement_enum:
+	  lang_find_relro_sections_1 (s->output_section_statement.children.head,
+				      has_relro_section);
+	  break;
+	case lang_group_statement_enum:
+	  lang_find_relro_sections_1 (s->group_statement.children.head,
+				      has_relro_section);
+	  break;
+	default:
+	  break;
+	}
+    }
+}
+
+static void
+lang_find_relro_sections (void)
+{
+  bfd_boolean has_relro_section = FALSE;
+
+  /* Check all sections in the link script.  */
+
+  lang_find_relro_sections_1 (expld.dataseg.relro_start_stat,
+			      &has_relro_section);
+
+  if (!has_relro_section)
+    link_info.relro = FALSE;
+}
+
 /* Relax all sections until bfd_relax_section gives up.  */
 
 static void
@@ -5731,6 +5828,10 @@ lang_process (void)
      section positions, since they will affect SIZEOF_HEADERS.  */
   lang_record_phdrs ();
 
+  /* Check relro sections.  */
+  if (link_info.relro && ! link_info.relocatable)
+    lang_find_relro_sections ();
+
   /* Size up the sections.  */
   lang_size_sections (NULL, !command_line.relax);
 
--- binutils/ld/testsuite/ld-elf/binutils.exp.relro	2006-08-16 19:04:53.000000000 -0700
+++ binutils/ld/testsuite/ld-elf/binutils.exp	2006-10-27 10:20:57.000000000 -0700
@@ -101,24 +101,33 @@ binutils_test strip "-shared" maxpage1
 binutils_test objcopy "" maxpage1
 binutils_test objcopy "-shared" maxpage1
 
-binutils_test strip "-z relro" maxpage1
-binutils_test strip "-z relro -shared" maxpage1
-binutils_test objcopy "-z relro" maxpage1
-binutils_test objcopy "-z relro -shared" maxpage1
+binutils_test strip "-z relro" relro
+binutils_test strip "-z relro -shared" relro
+binutils_test objcopy "-z relro" relro
+binutils_test objcopy "-z relro -shared" relro
 
 binutils_test objcopy "" tbss1
+binutils_test objcopy "-z relro" tbss1
 binutils_test objcopy "-shared" tbss1
+binutils_test objcopy "-shared -z relro" tbss1
 binutils_test objcopy "-z max-page-size=0x100000" tbss1
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tbss1
 binutils_test objcopy "" tdata1
+binutils_test objcopy "-z relro" tdata1
 binutils_test objcopy "-shared" tdata1
+binutils_test objcopy "-shared -z relro" tdata1
 binutils_test objcopy "-z max-page-size=0x100000" tdata1
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tdata1
 binutils_test objcopy "" tbss2
+binutils_test objcopy "-z relro" tbss2
 binutils_test objcopy "-shared" tbss2
+binutils_test objcopy "-shared -z relro" tbss2
 binutils_test objcopy "-z max-page-size=0x100000" tbss2
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tbss2
-binutils_test objcopy "-z max-page-size=0x100000" tdata2
+
 binutils_test objcopy "" tdata2
+binutils_test objcopy "-z relro" tdata2
 binutils_test objcopy "-shared" tdata2
+binutils_test objcopy "-shared -z relro" tdata2
+binutils_test objcopy "-z max-page-size=0x100000" tdata2
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tdata2
--- binutils/ld/testsuite/ld-elf/relro.s.relro	2006-10-27 10:20:57.000000000 -0700
+++ binutils/ld/testsuite/ld-elf/relro.s	2006-10-27 10:20:57.000000000 -0700
@@ -0,0 +1,14 @@
+	.globl main
+	.globl start
+	.globl _start
+	.globl __start
+	.text
+main:
+start:
+_start:
+__start:
+	.long	0
+	.data
+	.long	0
+	.section .data.rel.ro,"aw",%progbits
+	.long	0

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: PATCH: PR binutils/3281: objcopy changes PT_GNU_RELRO when there is PT_TLS
  2006-10-28 22:59             ` H. J. Lu
@ 2006-10-29  0:01               ` H. J. Lu
  0 siblings, 0 replies; 9+ messages in thread
From: H. J. Lu @ 2006-10-29  0:01 UTC (permalink / raw)
  To: binutils

On Fri, Oct 27, 2006 at 11:27:21AM -0700, H. J. Lu wrote:
> On Fri, Oct 27, 2006 at 10:49:20AM -0700, H. J. Lu wrote:
> > On Fri, Oct 27, 2006 at 02:35:56PM +0930, Alan Modra wrote:
> > > On Mon, Oct 23, 2006 at 10:55:59PM -0700, H. J. Lu wrote:
> > > > +	      if (p->p_type == PT_GNU_RELRO)
> > > > +		{
> > > > +		  /* When we get here, we are copying executable
> > > > +		     or shared library. But we need to use the same
> > > > +		     linker logic.  */
> > > > +		  Elf_Internal_Phdr *lp;
> > > > +
> > > > +		  for (lp = phdrs; lp < phdrs + count; ++lp)
> > > > +		    {
> > > > +		      if (lp->p_type == PT_LOAD
> > > > +			  && lp->p_paddr == p->p_paddr)
> > > > +			break;
> > > > +		    }
> > > > +	  
> > > > +		  if (lp < phdrs + count)
> > > > +		    {
> > > > +		      p->p_filesz += p->p_vaddr - lp->p_vaddr;
> > > > +		      p->p_vaddr = lp->p_vaddr;
> > > > +		      p->p_offset = lp->p_offset;
> > > > +		      p->p_memsz = p->p_filesz;
> > > > +		      p->p_align = 1;
> > > > +		    }
> > > > +		  else
> > > > +		    abort ();
> > > > +		}
> > > 
> > > I don't think this is going to work in all cases.  objcopy can do some
> > > really nasty things as far as GNU_RELRO segment is concerned, deleting
> > > sections, adding sections, changing section lmas.  I think instead
> > > you should set the GNU_RELRO segment info from the section map, and
> > > try to get the section map correct in rewrite_elf_program_header.  I
> > > noticed at least one problem with rewrite_elf_program_header "Step
> > > Three":  It should not be allowed to split a GNU_RELRO segment into
> > > two due to gaps.
> > > 
> > 
> > My patch works with deleted sections, which won't reach "Step Three".
> > I am not sure if we should keep GNU_RELRO segment when we add section
> > or change section lmas.
> 
> I think we should delete GNU_RELRO segment if a section is added or
> modified.
> 
> > 
> > > > +lang_size_relro_sections (void)
> > > 
> > > Should probably be called lang_find_relro_sections.  You aren't doing
> > > any sizing here.
> > 
> > I will change it.
> > 
> > 
> 
> Here is the updated patch.
> 
> 

Don't set segment_mark when checking section status.


H.J.
---
bfd/

2006-10-20  H.J. Lu  <hongjiu.lu@intel.com>

	PR binutils/3281
	* elf-bfd.h (elf_obj_tdata): Remove relro.

	* elf.c (get_program_header_size): Check info->relro instead
	of elf_tdata (abfd)->relro.
	(_bfd_elf_map_sections_to_segments): Likewise.
	(assign_file_positions_for_load_sections): Don't set
	PT_GNU_RELRO segment alignment here.
	(assign_file_positions_for_non_load_sections): Properly set up
	PT_GNU_RELRO segment for copying executable/shared library.
	(elf_section_status): New enum.
	(rewrite_elf_program_header): Add elf_section_status. Remove
	PT_GNU_RELRO segment if a section is modified.
	(copy_private_bfd_data): Updated rewrite_elf_program_header
	call.

include/elf/

2006-10-20  H.J. Lu  <hongjiu.lu@intel.com>

	PR binutils/3281
	* internal.h (ELF_IS_SECTION_IN_SEGMENT): Allow SHF_TLS
	sections in PT_GNU_RELRO segments.

ld/

2006-10-20  H.J. Lu  <hongjiu.lu@intel.com>

	PR binutils/3281
	* ldexp.h (ldexp_control): Add relro, relro_start_stat and
	relro_end_stat.

	* ldexp.c (fold_binary): Set expld.dataseg.relro to
	exp_dataseg_relro_start or exp_dataseg_relro_end when
	seeing DATA_SEGMENT_ALIGN or DATA_SEGMENT_RELRO_END,
	respectively.

	* ldlang.c (lang_size_sections_1): Properly set
	expld.dataseg.relro_start_stat and
	expld.dataseg.relro_end_stat.
	(find_relro_section_callback): New function.
	(lang_find_relro_sections_1): Likewise.
	(lang_find_relro_sections): Likewise.
	(lang_process): Call lang_find_relro_sections for
	non-relocatable link.

ld/testsuite/

2006-10-20  H.J. Lu  <hongjiu.lu@intel.com>

	PR binutils/3281
	* ld-elf/binutils.exp: Update "-z relro" tests to use relro.s.
	Add "-z relro" tests with TLS for objcopy.

	* ld-elf/relro.s: New file.

--- binutils/bfd/elf-bfd.h.relro	2006-10-27 10:20:57.000000000 -0700
+++ binutils/bfd/elf-bfd.h	2006-10-27 10:20:57.000000000 -0700
@@ -1375,9 +1375,6 @@ struct elf_obj_tdata
   /* Segment flags for the PT_GNU_STACK segment.  */
   unsigned int stack_flags;
 
-  /* Should the PT_GNU_RELRO segment be emitted?  */
-  bfd_boolean relro;
-
   /* Symbol version definitions in external objects.  */
   Elf_Internal_Verdef *verdef;
 
--- binutils/bfd/elf.c.relro	2006-10-27 10:20:57.000000000 -0700
+++ binutils/bfd/elf.c	2006-10-27 11:31:48.000000000 -0700
@@ -3631,7 +3631,7 @@ get_program_header_size (bfd *abfd, stru
       /* We need a PT_DYNAMIC segment.  */
       ++segs;
       
-      if (elf_tdata (abfd)->relro)
+      if (info->relro)
 	{
 	  /* We need a PT_GNU_RELRO segment only when there is a
 	     PT_DYNAMIC segment.  */
@@ -4119,7 +4119,7 @@ _bfd_elf_map_sections_to_segments (bfd *
 	  pm = &m->next;
 	}
 
-      if (dynsec != NULL && elf_tdata (abfd)->relro)
+      if (dynsec != NULL && info->relro)
 	{
 	  /* We make a PT_GNU_RELRO segment only when there is a
 	     PT_DYNAMIC segment.  */
@@ -4602,11 +4602,9 @@ assign_file_positions_for_load_sections 
 		    p->p_memsz += o->offset + o->size;
 		}
 
-	      if (p->p_type == PT_GNU_RELRO)
-		p->p_align = 1;
-	      else if (align > p->p_align
-		       && (p->p_type != PT_LOAD
-			   || (abfd->flags & D_PAGED) == 0))
+	      if (align > p->p_align
+		  && (p->p_type != PT_LOAD
+		      || (abfd->flags & D_PAGED) == 0))
 		p->p_align = align;
 	    }
 
@@ -4733,7 +4731,8 @@ assign_file_positions_for_non_load_secti
       if (m->count != 0)
 	{
 	  if (p->p_type != PT_LOAD
-	      && (p->p_type != PT_NOTE || bfd_get_format (abfd) != bfd_core))
+	      && (p->p_type != PT_NOTE
+		  || bfd_get_format (abfd) != bfd_core))
 	    {
 	      Elf_Internal_Shdr *hdr;
 	      BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
@@ -4744,7 +4743,33 @@ assign_file_positions_for_non_load_secti
 	      if (hdr->sh_type != SHT_NOBITS)
 		p->p_filesz += hdr->sh_size;
 
-	      p->p_offset = m->sections[0]->filepos;
+	      if (p->p_type == PT_GNU_RELRO)
+		{
+		  /* When we get here, we are copying executable
+		     or shared library. But we need to use the same
+		     linker logic.  */
+		  Elf_Internal_Phdr *lp;
+
+		  for (lp = phdrs; lp < phdrs + count; ++lp)
+		    {
+		      if (lp->p_type == PT_LOAD
+			  && lp->p_paddr == p->p_paddr)
+			break;
+		    }
+	  
+		  if (lp < phdrs + count)
+		    {
+		      p->p_filesz += p->p_vaddr - lp->p_vaddr;
+		      p->p_vaddr = lp->p_vaddr;
+		      p->p_offset = lp->p_offset;
+		      p->p_memsz = p->p_filesz;
+		      p->p_align = 1;
+		    }
+		  else
+		    abort ();
+		}
+	      else
+		p->p_offset = m->sections[0]->filepos;
 	    }
 	}
       else
@@ -5172,8 +5197,17 @@ _bfd_elf_symbol_from_bfd_symbol (bfd *ab
 
 /* Rewrite program header information.  */
 
+enum elf_section_status
+{
+  unknown,
+  added,
+  removed,
+  modified
+};
+
 static bfd_boolean
-rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
+rewrite_elf_program_header (bfd *ibfd, bfd *obfd,
+			    enum elf_section_status status)
 {
   Elf_Internal_Ehdr *iehdr;
   struct elf_segment_map *map;
@@ -5321,7 +5355,14 @@ rewrite_elf_program_header (bfd *ibfd, b
 	    }
 
       if (segment->p_type != PT_LOAD)
-	continue;
+	{
+	  /* If a section is added or mofied, remove PT_GNU_RELRO
+	     segment.  */
+	  if (status != removed &&
+	      segment->p_type == PT_GNU_RELRO)
+	    segment->p_type = PT_NULL;
+	  continue;
+	}
 
       /* Determine if this segment overlaps any previous segments.  */
       for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
@@ -5858,6 +5899,12 @@ copy_elf_program_header (bfd *ibfd, bfd 
 static bfd_boolean
 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
 {
+  enum elf_section_status status;
+  Elf_Internal_Phdr *segment;
+  asection *section, *osec;
+  unsigned int i, num_segments;
+  Elf_Internal_Shdr *this_hdr;
+
   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
     return TRUE;
@@ -5865,14 +5912,11 @@ copy_private_bfd_data (bfd *ibfd, bfd *o
   if (elf_tdata (ibfd)->phdr == NULL)
     return TRUE;
 
+  status = unknown;
   if (ibfd->xvec == obfd->xvec)
     {
       /* Check if any sections in the input BFD covered by ELF program
 	 header are changed.  */
-      Elf_Internal_Phdr *segment;
-      asection *section, *osec;
-      unsigned int i, num_segments;
-      Elf_Internal_Shdr *this_hdr;
 
       /* Initialize the segment mark field.  */
       for (section = obfd->sections; section != NULL;
@@ -5917,7 +5961,10 @@ copy_private_bfd_data (bfd *ibfd, bfd *o
 	   section = section->next)
 	{
 	  if (section->segment_mark == FALSE)
-	    goto rewrite;
+	    { 
+	      status = added;
+	      goto rewrite;
+	    }
 	  else
 	    section->segment_mark = FALSE;
 	}
@@ -5926,7 +5973,42 @@ copy_private_bfd_data (bfd *ibfd, bfd *o
     }
 
 rewrite:
-  return rewrite_elf_program_header (ibfd, obfd);
+  if (status == unknown)
+    {
+      /* We need to find out how we are changed.  */
+      num_segments = elf_elfheader (ibfd)->e_phnum;
+      for (i = 0, segment = elf_tdata (ibfd)->phdr;
+	   i < num_segments;
+	   i++, segment++)
+	{
+	  for (section = ibfd->sections;
+	       section != NULL; section = section->next)
+	    {
+	      osec = section->output_section;
+
+	      /* Check if this section is covered by the segment.  */
+	      this_hdr = &(elf_section_data(section)->this_hdr);
+	      if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
+		{
+		  if (osec == NULL)
+		    status = removed;
+		  else if (section->flags != osec->flags
+			   || section->lma != osec->lma
+			   || section->vma != osec->vma
+			   || section->size != osec->size
+			   || section->rawsize != osec->rawsize
+			   || section->alignment_power != osec->alignment_power)
+		    {
+		      /* Stop if a section is modified.  */
+		      status = modified;
+		      break;
+		    }
+		}
+	    }
+	}
+    }
+  BFD_ASSERT (status != unknown);
+  return rewrite_elf_program_header (ibfd, obfd, status);
 }
 
 /* Initialize private output section information from input section.  */
--- binutils/bfd/elflink.c.relro	2006-10-27 10:20:57.000000000 -0700
+++ binutils/bfd/elflink.c	2006-10-27 10:20:57.000000000 -0700
@@ -5266,7 +5266,6 @@ bfd_elf_size_dynamic_sections (bfd *outp
   if (!is_elf_hash_table (info->hash))
     return TRUE;
 
-  elf_tdata (output_bfd)->relro = info->relro;
   if (info->execstack)
     elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
   else if (info->noexecstack)
--- binutils/include/elf/internal.h.relro	2006-05-31 22:40:24.000000000 -0700
+++ binutils/include/elf/internal.h	2006-10-27 10:20:57.000000000 -0700
@@ -264,11 +264,12 @@ struct elf_segment_map
      || segment->p_type == PT_TLS) ? sec_hdr->sh_size : 0)
 
 /* Decide if the given sec_hdr is in the given segment.  PT_TLS segment
-   contains only SHF_TLS sections.  Only PT_LOAD and PT_TLS segments
-   can contain SHF_TLS sections.  */
+   contains only SHF_TLS sections.  Only PT_LOAD, PT_GNU_RELRO and
+   PT_TLS segments can contain SHF_TLS sections.  */
 #define ELF_IS_SECTION_IN_SEGMENT(sec_hdr, segment)		\
   (((((sec_hdr->sh_flags & SHF_TLS) != 0)			\
      && (segment->p_type == PT_TLS				\
+	 || segment->p_type == PT_GNU_RELRO			\
 	 || segment->p_type == PT_LOAD))			\
     || ((sec_hdr->sh_flags & SHF_TLS) == 0			\
 	&& segment->p_type != PT_TLS))				\
--- binutils/ld/ldexp.c.relro	2006-08-22 18:35:31.000000000 -0700
+++ binutils/ld/ldexp.c	2006-10-27 10:20:57.000000000 -0700
@@ -384,6 +384,7 @@ fold_binary (etree_type *tree)
 	      break;
 
 	    case DATA_SEGMENT_ALIGN:
+	      expld.dataseg.relro = exp_dataseg_relro_start;
 	      if (expld.phase != lang_first_phase_enum
 		  && expld.section == bfd_abs_section_ptr
 		  && (expld.dataseg.phase == exp_dataseg_none
@@ -419,6 +420,7 @@ fold_binary (etree_type *tree)
 	      break;
 
 	    case DATA_SEGMENT_RELRO_END:
+	      expld.dataseg.relro = exp_dataseg_relro_end;
 	      if (expld.phase != lang_first_phase_enum
 		  && (expld.dataseg.phase == exp_dataseg_align_seen
 		      || expld.dataseg.phase == exp_dataseg_adjust
--- binutils/ld/ldexp.h.relro	2005-08-05 06:52:13.000000000 -0700
+++ binutils/ld/ldexp.h	2006-10-27 10:20:57.000000000 -0700
@@ -97,6 +97,8 @@ typedef enum {
   lang_final_phase_enum
 } lang_phase_type;
 
+union lang_statement_union;
+
 struct ldexp_control {
   /* Modify expression evaluation depending on this.  */
   lang_phase_type phase;
@@ -124,6 +126,15 @@ struct ldexp_control {
     } phase;
 
     bfd_vma base, min_base, relro_end, end, pagesize, maxpagesize;
+
+    enum {
+      exp_dataseg_relro_none,
+      exp_dataseg_relro_start,
+      exp_dataseg_relro_end,
+    } relro;
+
+    union lang_statement_union *relro_start_stat;
+    union lang_statement_union *relro_end_stat;
   } dataseg;
 };
 
--- binutils/ld/ldlang.c.relro	2006-10-27 10:20:57.000000000 -0700
+++ binutils/ld/ldlang.c	2006-10-27 10:20:57.000000000 -0700
@@ -4626,10 +4626,32 @@ lang_size_sections_1
 	    bfd_vma newdot = dot;
 	    etree_type *tree = s->assignment_statement.exp;
 
+	    expld.dataseg.relro = exp_dataseg_relro_none;
+
 	    exp_fold_tree (tree,
 			   output_section_statement->bfd_section,
 			   &newdot);
 
+	    if (expld.dataseg.relro == exp_dataseg_relro_start)
+	      {
+		if (!expld.dataseg.relro_start_stat)
+		  expld.dataseg.relro_start_stat = s;
+		else
+		  {
+		    ASSERT (expld.dataseg.relro_start_stat == s);
+		  }
+	      }
+	    else if (expld.dataseg.relro == exp_dataseg_relro_end)
+	      {
+		if (!expld.dataseg.relro_end_stat)
+		  expld.dataseg.relro_end_stat = s;
+		else
+		  {
+		    ASSERT (expld.dataseg.relro_end_stat == s);
+		  }
+	      }
+	    expld.dataseg.relro = exp_dataseg_relro_none;
+
 	    /* This symbol is relative to this section.  */
 	    if ((tree->type.node_class == etree_provided
 		 || tree->type.node_class == etree_assign)
@@ -5606,6 +5628,81 @@ lang_gc_sections (void)
     bfd_gc_sections (output_bfd, &link_info);
 }
 
+/* Worker for lang_find_relro_sections_1.  */
+
+static void
+find_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
+			     struct wildcard_list *sec ATTRIBUTE_UNUSED,
+			     asection *section,
+			     lang_input_statement_type *file ATTRIBUTE_UNUSED,
+			     void *data)
+{
+  /* Discarded, excluded and ignored sections effectively have zero
+     size.  */
+  if (section->output_section != NULL
+      && section->output_section->owner == output_bfd
+      && (section->output_section->flags & SEC_EXCLUDE) == 0
+      && !IGNORE_SECTION (section)
+      && section->size != 0)
+    {
+      bfd_boolean *has_relro_section = (bfd_boolean *) data;
+      *has_relro_section = TRUE;
+    }
+}
+
+/* Iterate over sections for relro sections.  */
+
+static void
+lang_find_relro_sections_1 (lang_statement_union_type *s,
+			    bfd_boolean *has_relro_section)
+{
+  if (*has_relro_section)
+    return;
+
+  for (; s != NULL; s = s->header.next)
+    {
+      if (s == expld.dataseg.relro_end_stat)
+	break;
+
+      switch (s->header.type)
+	{
+	case lang_wild_statement_enum:
+	  walk_wild (&s->wild_statement,
+		     find_relro_section_callback,
+		     has_relro_section);
+	  break;
+	case lang_constructors_statement_enum:
+	  lang_find_relro_sections_1 (constructor_list.head,
+				      has_relro_section);
+	  break;
+	case lang_output_section_statement_enum:
+	  lang_find_relro_sections_1 (s->output_section_statement.children.head,
+				      has_relro_section);
+	  break;
+	case lang_group_statement_enum:
+	  lang_find_relro_sections_1 (s->group_statement.children.head,
+				      has_relro_section);
+	  break;
+	default:
+	  break;
+	}
+    }
+}
+
+static void
+lang_find_relro_sections (void)
+{
+  bfd_boolean has_relro_section = FALSE;
+
+  /* Check all sections in the link script.  */
+
+  lang_find_relro_sections_1 (expld.dataseg.relro_start_stat,
+			      &has_relro_section);
+
+  if (!has_relro_section)
+    link_info.relro = FALSE;
+}
+
 /* Relax all sections until bfd_relax_section gives up.  */
 
 static void
@@ -5731,6 +5828,10 @@ lang_process (void)
      section positions, since they will affect SIZEOF_HEADERS.  */
   lang_record_phdrs ();
 
+  /* Check relro sections.  */
+  if (link_info.relro && ! link_info.relocatable)
+    lang_find_relro_sections ();
+
   /* Size up the sections.  */
   lang_size_sections (NULL, !command_line.relax);
 
--- binutils/ld/testsuite/ld-elf/binutils.exp.relro	2006-08-16 19:04:53.000000000 -0700
+++ binutils/ld/testsuite/ld-elf/binutils.exp	2006-10-27 10:20:57.000000000 -0700
@@ -101,24 +101,33 @@ binutils_test strip "-shared" maxpage1
 binutils_test objcopy "" maxpage1
 binutils_test objcopy "-shared" maxpage1
 
-binutils_test strip "-z relro" maxpage1
-binutils_test strip "-z relro -shared" maxpage1
-binutils_test objcopy "-z relro" maxpage1
-binutils_test objcopy "-z relro -shared" maxpage1
+binutils_test strip "-z relro" relro
+binutils_test strip "-z relro -shared" relro
+binutils_test objcopy "-z relro" relro
+binutils_test objcopy "-z relro -shared" relro
 
 binutils_test objcopy "" tbss1
+binutils_test objcopy "-z relro" tbss1
 binutils_test objcopy "-shared" tbss1
+binutils_test objcopy "-shared -z relro" tbss1
 binutils_test objcopy "-z max-page-size=0x100000" tbss1
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tbss1
 binutils_test objcopy "" tdata1
+binutils_test objcopy "-z relro" tdata1
 binutils_test objcopy "-shared" tdata1
+binutils_test objcopy "-shared -z relro" tdata1
 binutils_test objcopy "-z max-page-size=0x100000" tdata1
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tdata1
 binutils_test objcopy "" tbss2
+binutils_test objcopy "-z relro" tbss2
 binutils_test objcopy "-shared" tbss2
+binutils_test objcopy "-shared -z relro" tbss2
 binutils_test objcopy "-z max-page-size=0x100000" tbss2
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tbss2
-binutils_test objcopy "-z max-page-size=0x100000" tdata2
+
 binutils_test objcopy "" tdata2
+binutils_test objcopy "-z relro" tdata2
 binutils_test objcopy "-shared" tdata2
+binutils_test objcopy "-shared -z relro" tdata2
+binutils_test objcopy "-z max-page-size=0x100000" tdata2
 binutils_test objcopy "-z max-page-size=0x100000 -z common-page-size=0x1000" tdata2
--- binutils/ld/testsuite/ld-elf/relro.s.relro	2006-10-27 10:20:57.000000000 -0700
+++ binutils/ld/testsuite/ld-elf/relro.s	2006-10-27 10:20:57.000000000 -0700
@@ -0,0 +1,14 @@
+	.globl main
+	.globl start
+	.globl _start
+	.globl __start
+	.text
+main:
+start:
+_start:
+__start:
+	.long	0
+	.data
+	.long	0
+	.section .data.rel.ro,"aw",%progbits
+	.long	0

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2006-10-27 18:34 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-09-29  5:38 PATCH: PR binutils/3281: objcopy changes PT_GNU_RELRO when there is PT_TLS H. J. Lu
2006-10-19 16:31 ` H. J. Lu
2006-10-20 12:57   ` H. J. Lu
2006-10-20 13:44     ` H. J. Lu
2006-10-24  7:14       ` H. J. Lu
2006-10-27 15:00         ` Alan Modra
2006-10-28 22:19           ` H. J. Lu
2006-10-28 22:59             ` H. J. Lu
2006-10-29  0:01               ` H. J. Lu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).