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


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

PATCH: Map GNU attributes section to PT_GNU_ATTR (aka PT_GNU_STACK) segment


On Wed, Aug 15, 2007 at 05:39:48PM +0100, Nick Clifton wrote:
> Hi H.J.
> 
> >The current usage of the PT_GNU_STACK segment is the flags field
> >only.  All other fields are ignored.  The new segment ignores the
> >flags field.  Overloading the PT_GNU_STACK segment seems a good
> >idea to me since it is totally backward and forward compatible.
> 
> I agree that it is forward and backward compatible.  I am just worried that 
> the name of the segment, if set to PT_GNU_ATTRIBUTES or left as 
> PT_GNU_STACK, would be confusing.  I think that we ought to have a more 
> generic type of name and then have a well defined scheme for storing 
> information in this segment.

I change it to PT_GNU_ATTR.

> 
> >That is what my proposal does. The only difference is I call the
> >new segment PT_GNU_ATTRIBUTES.  I don't mind we use a differrent
> >name at all.  The loader-accessible information belongs to the GNU
> >attributes section and it is extendable.
> 
> True, so maybe we ought to have a GNU_STACK attribute and use that.  For 
> backwards compatibility we can still set the flags of the segment 
> appropriately, but that would only be for backwards compatibility.  Any 
> future uses of the segment would not make use of the flags to specify any 
> information.
> 
> 

I am not sure if it is worth the change since a special note section,
.note.GNU-stack, is used to specify the stack flags. Moving it to
the .gnu.attributes section will require changes in gcc, binutils
and applications. We have to keep .note.GNU-stack because of it. We
can add new ones to .gnu.attributes, but should leave the stack flags
alone.

Thanks.


H.J.
---
bfd/

2007-08-15  H.J. Lu  <hongjiu.lu@intel.com>

	* elf.c (get_segment_type): Change PT_GNU_STACK to PT_GNU_ATTR.
	(bfd_section_from_phdr): Likewise.
	(get_program_header_size): Likewise. Add a PT_GNU_ATTR segment
	if there is an attribute section.
	(_bfd_elf_map_sections_to_segments): Likewise.
	(IS_SECTION_IN_INPUT_SEGMENT): Likewise.

binutils/

2007-08-15  H.J. Lu  <hongjiu.lu@intel.com>

	* readelf.c (get_segment_type): Change PT_GNU_STACK to
	PT_GNU_ATTR.

include/elf/

2007-08-15  H.J. Lu  <hongjiu.lu@intel.com>

	* common.h (PT_GNU_STACK): Renamed to ...
	(PT_GNU_ATTR): This.
	(PT_GNU_STACK): New.  Make an alias of PT_GNU_ATTR.

--- binutils/bfd/elf.c.attri	2007-08-14 08:41:55.000000000 -0700
+++ binutils/bfd/elf.c	2007-08-15 10:04:21.000000000 -0700
@@ -1083,7 +1083,7 @@ get_segment_type (unsigned int p_type)
     case PT_PHDR: pt = "PHDR"; break;
     case PT_TLS: pt = "TLS"; break;
     case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
-    case PT_GNU_STACK: pt = "STACK"; break;
+    case PT_GNU_ATTR: pt = "ATTR"; break;
     case PT_GNU_RELRO: pt = "RELRO"; break;
     default: pt = NULL; break;
     }
@@ -2355,8 +2355,8 @@ bfd_section_from_phdr (bfd *abfd, Elf_In
       return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
 					      "eh_frame_hdr");
 
-    case PT_GNU_STACK:
-      return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
+    case PT_GNU_ATTR:
+      return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "attr");
 
     case PT_GNU_RELRO:
       return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
@@ -3300,7 +3300,8 @@ get_program_header_size (bfd *abfd, stru
 {
   size_t segs;
   asection *s;
-  const struct elf_backend_data *bed;
+  asection *attr = NULL;
+  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
 
   /* Assume we will need exactly two PT_LOAD segments: one for text
      and one for data.  */
@@ -3335,14 +3336,26 @@ get_program_header_size (bfd *abfd, stru
       ++segs;
     }
 
-  if (elf_tdata (abfd)->stack_flags)
-    {
-      /* We need a PT_GNU_STACK segment.  */
-      ++segs;
-    }
-
   for (s = abfd->sections; s != NULL; s = s->next)
     {
+      if (elf_section_type (s) == bed->obj_attrs_section_type)
+	{
+	  BFD_ASSERT (attr == NULL);
+	  attr = s;
+
+	  /* elf_tdata (abfd)->stack_flags is checked for the
+	     PT_GNU_ATTR segment.  If there is an attribute
+	     section, we make sure that stack_flags isn't zero so
+	     that the PT_GNU_ATTR segment will be created.  */
+	  if (! elf_tdata (abfd)->stack_flags)
+	    {
+	      if (bed->default_execstack)
+		elf_tdata (abfd)->stack_flags = PF_R | PF_W | PF_X;
+	      else
+		elf_tdata (abfd)->stack_flags = PF_R | PF_W;
+	    }
+	}
+
       if ((s->flags & SEC_LOAD) != 0
 	  && CONST_STRNEQ (s->name, ".note"))
 	{
@@ -3364,6 +3377,12 @@ get_program_header_size (bfd *abfd, stru
 	}
     }
 
+  if (elf_tdata (abfd)->stack_flags)
+    {
+      /* We need a PT_GNU_ATTR segment.  */
+      ++segs;
+    }
+
   for (s = abfd->sections; s != NULL; s = s->next)
     {
       if (s->flags & SEC_THREAD_LOCAL)
@@ -3375,7 +3394,6 @@ get_program_header_size (bfd *abfd, stru
     }
 
   /* Let the backend count up any program headers it might need.  */
-  bed = get_elf_backend_data (abfd);
   if (bed->elf_backend_additional_program_headers)
     {
       int a;
@@ -3520,6 +3538,7 @@ _bfd_elf_map_sections_to_segments (bfd *
       asection *first_tls = NULL;
       asection *dynsec, *eh_frame_hdr;
       bfd_size_type amt;
+      asection *attr = NULL;
 
       /* Select the allocated sections, and sort them.  */
 
@@ -3530,6 +3549,12 @@ _bfd_elf_map_sections_to_segments (bfd *
       i = 0;
       for (s = abfd->sections; s != NULL; s = s->next)
 	{
+	  if (elf_section_type (s) == bed->obj_attrs_section_type)
+	    {
+	      BFD_ASSERT (attr == NULL);
+	      attr = s;
+	    }
+
 	  if ((s->flags & SEC_ALLOC) != 0)
 	    {
 	      sections[i] = s;
@@ -3843,10 +3868,17 @@ _bfd_elf_map_sections_to_segments (bfd *
 	  if (m == NULL)
 	    goto error_return;
 	  m->next = NULL;
-	  m->p_type = PT_GNU_STACK;
+	  m->p_type = PT_GNU_ATTR;
 	  m->p_flags = elf_tdata (abfd)->stack_flags;
 	  m->p_flags_valid = 1;
 
+	  if (attr)
+	    {
+	      /* Add the attribute section if needed.  */
+	      m->count = 1;
+	      m->sections[0] = attr;
+	    } 
+
 	  *pm = m;
 	  pm = &m->next;
 	}
@@ -5033,31 +5065,33 @@ rewrite_elf_program_header (bfd *ibfd, b
        2. It is an allocated segment,
        3. There is an output section associated with it,
        4. The section has not already been allocated to a previous segment.
-       5. PT_GNU_STACK segments do not include any sections.
+       5. PT_GNU_ATTR only contains attribute section.
        6. PT_TLS segment includes only SHF_TLS sections.
        7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
        8. PT_DYNAMIC should not contain empty sections at the beginning
 	  (with the possible exception of .dynamic).  */
 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed)		\
-  ((((segment->p_paddr							\
-      ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr)	\
-      : IS_CONTAINED_BY_VMA (section, segment))				\
-     && (section->flags & SEC_ALLOC) != 0)				\
-    || IS_COREFILE_NOTE (segment, section))				\
-   && segment->p_type != PT_GNU_STACK					\
-   && (segment->p_type != PT_TLS					\
-       || (section->flags & SEC_THREAD_LOCAL))				\
-   && (segment->p_type == PT_LOAD					\
-       || segment->p_type == PT_TLS					\
-       || (section->flags & SEC_THREAD_LOCAL) == 0)			\
-   && (segment->p_type != PT_DYNAMIC					\
-       || SECTION_SIZE (section, segment) > 0				\
-       || (segment->p_paddr						\
-	   ? segment->p_paddr != section->lma				\
-	   : segment->p_vaddr != section->vma)				\
-       || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic")	\
-	   == 0))							\
-   && ! section->segment_mark)
+  ((segment->p_type == PT_GNU_ATTR					\
+    && elf_section_type (section) == bed->obj_attrs_section_type)	\
+   || ((((segment->p_paddr						\
+	  ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr)	\
+	  : IS_CONTAINED_BY_VMA (section, segment))			\
+	 && (section->flags & SEC_ALLOC) != 0)				\
+	|| IS_COREFILE_NOTE (segment, section))				\
+       && segment->p_type != PT_GNU_ATTR				\
+       && (segment->p_type != PT_TLS					\
+	   || (section->flags & SEC_THREAD_LOCAL))			\
+       && (segment->p_type == PT_LOAD					\
+	   || segment->p_type == PT_TLS					\
+	   || (section->flags & SEC_THREAD_LOCAL) == 0)			\
+       && (segment->p_type != PT_DYNAMIC				\
+	   || SECTION_SIZE (section, segment) > 0			\
+	   || (segment->p_paddr						\
+	       ? segment->p_paddr != section->lma			\
+	       : segment->p_vaddr != section->vma)			\
+	   || (strcmp (bfd_get_section_name (ibfd, section),		\
+		       ".dynamic") == 0))				\
+       && ! section->segment_mark))
 
 /* If the output section of a section in the input segment is NULL,
    it is removed from the corresponding output segment.   */
--- binutils/binutils/readelf.c.attri	2007-07-27 12:52:07.000000000 -0700
+++ binutils/binutils/readelf.c	2007-08-15 09:55:17.000000000 -0700
@@ -2467,7 +2467,7 @@ get_segment_type (unsigned long p_type)
 
     case PT_GNU_EH_FRAME:
 			return "GNU_EH_FRAME";
-    case PT_GNU_STACK:	return "GNU_STACK";
+    case PT_GNU_ATTR:	return "GNU_ATTR";
     case PT_GNU_RELRO:  return "GNU_RELRO";
 
     default:
--- binutils/include/elf/common.h.attri	2007-07-24 15:03:52.000000000 -0700
+++ binutils/include/elf/common.h	2007-08-15 09:49:15.000000000 -0700
@@ -307,7 +307,8 @@
 
 #define PT_GNU_EH_FRAME	(PT_LOOS + 0x474e550) /* Frame unwind information */
 #define PT_SUNW_EH_FRAME PT_GNU_EH_FRAME      /* Solaris uses the same value */
-#define PT_GNU_STACK	(PT_LOOS + 0x474e551) /* Stack flags */
+#define PT_GNU_ATTR	(PT_LOOS + 0x474e551) /* Attribute info */
+#define PT_GNU_STACK	PT_GNU_ATTR	      /* Stack flags */
 #define PT_GNU_RELRO	(PT_LOOS + 0x474e552) /* Read-only after relocation */
 
 /* Program segment permissions, in program header p_flags field.  */


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