patches/binutils/2.20.1/150-pt-pax-flags-20090909.patch
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Tue Aug 17 19:48:49 2010 +0200 (2010-08-17)
changeset 2088 4f21ba5f8e91
parent 1614 patches/binutils/2.20/150-pt-pax-flags-20090909.patch@3f76cdbceb6e
permissions -rw-r--r--
binutils/binutils: forward-port the patchset from 2.20 to 2.20.1

Among other things, this should fix the issue reported by Thomas at:
http://sourceware.org/ml/crossgcc/2010-08/msg00115.html

Signed-off-by: "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
     1 
     2 
     3 diff -durN binutils-2.20.1.orig/bfd/elf-bfd.h binutils-2.20.1/bfd/elf-bfd.h
     4 --- binutils-2.20.1.orig/bfd/elf-bfd.h	2009-09-10 13:47:12.000000000 +0200
     5 +++ binutils-2.20.1/bfd/elf-bfd.h	2010-08-17 19:32:11.000000000 +0200
     6 @@ -1531,6 +1531,9 @@
     7    /* Segment flags for the PT_GNU_STACK segment.  */
     8    unsigned int stack_flags;
     9  
    10 +  /* Segment flags for the PT_PAX_FLAGS segment.  */
    11 +  unsigned int pax_flags;
    12 +
    13    /* Symbol version definitions in external objects.  */
    14    Elf_Internal_Verdef *verdef;
    15  
    16 diff -durN binutils-2.20.1.orig/bfd/elf.c binutils-2.20.1/bfd/elf.c
    17 --- binutils-2.20.1.orig/bfd/elf.c	2009-09-10 13:47:12.000000000 +0200
    18 +++ binutils-2.20.1/bfd/elf.c	2010-08-17 19:32:11.000000000 +0200
    19 @@ -1083,6 +1083,7 @@
    20      case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
    21      case PT_GNU_STACK: pt = "STACK"; break;
    22      case PT_GNU_RELRO: pt = "RELRO"; break;
    23 +    case PT_PAX_FLAGS: pt = "PAX_FLAGS"; break;
    24      default: pt = NULL; break;
    25      }
    26    return pt;
    27 @@ -2396,6 +2397,9 @@
    28      case PT_GNU_RELRO:
    29        return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
    30  
    31 +    case PT_PAX_FLAGS:
    32 +      return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "pax_flags");
    33 +
    34      default:
    35        /* Check for any processor-specific program segment types.  */
    36        bed = get_elf_backend_data (abfd);
    37 @@ -3413,6 +3417,11 @@
    38        ++segs;
    39      }
    40  
    41 +    {
    42 +      /* We need a PT_PAX_FLAGS segment.  */
    43 +      ++segs;
    44 +    }
    45 +
    46    for (s = abfd->sections; s != NULL; s = s->next)
    47      {
    48        if ((s->flags & SEC_LOAD) != 0
    49 @@ -3994,6 +4003,20 @@
    50  	    }
    51  	}
    52  
    53 +    {
    54 +      amt = sizeof (struct elf_segment_map);
    55 +      m = bfd_zalloc (abfd, amt);
    56 +      if (m == NULL)
    57 +	goto error_return;
    58 +      m->next = NULL;
    59 +      m->p_type = PT_PAX_FLAGS;
    60 +      m->p_flags = elf_tdata (abfd)->pax_flags;
    61 +      m->p_flags_valid = 1;
    62 +
    63 +      *pm = m;
    64 +      pm = &m->next;
    65 +    }
    66 +
    67        free (sections);
    68        elf_tdata (abfd)->segment_map = mfirst;
    69      }
    70 @@ -5198,7 +5221,8 @@
    71         6. PT_TLS segment includes only SHF_TLS sections.
    72         7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
    73         8. PT_DYNAMIC should not contain empty sections at the beginning
    74 -	  (with the possible exception of .dynamic).  */
    75 +	  (with the possible exception of .dynamic).
    76 +       9. PT_PAX_FLAGS segments do not include any sections.  */
    77  #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed)		\
    78    ((((segment->p_paddr							\
    79        ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr)	\
    80 @@ -5206,6 +5230,7 @@
    81       && (section->flags & SEC_ALLOC) != 0)				\
    82      || IS_NOTE (segment, section))					\
    83     && segment->p_type != PT_GNU_STACK					\
    84 +   && segment->p_type != PT_PAX_FLAGS					\
    85     && (segment->p_type != PT_TLS					\
    86         || (section->flags & SEC_THREAD_LOCAL))				\
    87     && (segment->p_type == PT_LOAD					\
    88 diff -durN binutils-2.20.1.orig/bfd/elflink.c binutils-2.20.1/bfd/elflink.c
    89 --- binutils-2.20.1.orig/bfd/elflink.c	2009-09-10 13:47:12.000000000 +0200
    90 +++ binutils-2.20.1/bfd/elflink.c	2010-08-17 19:32:11.000000000 +0200
    91 @@ -5469,16 +5469,30 @@
    92      return TRUE;
    93  
    94    bed = get_elf_backend_data (output_bfd);
    95 +
    96 +  elf_tdata (output_bfd)->pax_flags = PF_NORANDEXEC;
    97 +  if (info->execheap)
    98 +    elf_tdata (output_bfd)->pax_flags |= PF_NOMPROTECT;
    99 +  else if (info->noexecheap)
   100 +    elf_tdata (output_bfd)->pax_flags |= PF_MPROTECT;
   101 +
   102    if (info->execstack)
   103 -    elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
   104 +    {
   105 +      elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
   106 +      elf_tdata (output_bfd)->pax_flags |= PF_EMUTRAMP;
   107 +    }
   108    else if (info->noexecstack)
   109 -    elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
   110 +    {
   111 +      elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
   112 +      elf_tdata (output_bfd)->pax_flags |= PF_NOEMUTRAMP;
   113 +    }
   114    else
   115      {
   116        bfd *inputobj;
   117        asection *notesec = NULL;
   118        int exec = 0;
   119  
   120 +      elf_tdata (output_bfd)->pax_flags |= PF_NOEMUTRAMP;
   121        for (inputobj = info->input_bfds;
   122  	   inputobj;
   123  	   inputobj = inputobj->link_next)
   124 @@ -5491,7 +5505,11 @@
   125  	  if (s)
   126  	    {
   127  	      if (s->flags & SEC_CODE)
   128 -		exec = PF_X;
   129 +		{
   130 +		  elf_tdata (output_bfd)->pax_flags &= ~PF_NOEMUTRAMP;
   131 +		  elf_tdata (output_bfd)->pax_flags |= PF_EMUTRAMP;
   132 +		  exec = PF_X;
   133 +		}
   134  	      notesec = s;
   135  	    }
   136  	  else if (bed->default_execstack)
   137 diff -durN binutils-2.20.1.orig/binutils/readelf.c binutils-2.20.1/binutils/readelf.c
   138 --- binutils-2.20.1.orig/binutils/readelf.c	2010-01-14 11:48:23.000000000 +0100
   139 +++ binutils-2.20.1/binutils/readelf.c	2010-08-17 19:32:11.000000000 +0200
   140 @@ -2569,6 +2569,7 @@
   141  			return "GNU_EH_FRAME";
   142      case PT_GNU_STACK:	return "GNU_STACK";
   143      case PT_GNU_RELRO:  return "GNU_RELRO";
   144 +    case PT_PAX_FLAGS:  return "PAX_FLAGS";
   145  
   146      default:
   147        if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
   148 diff -durN binutils-2.20.1.orig/include/bfdlink.h binutils-2.20.1/include/bfdlink.h
   149 --- binutils-2.20.1.orig/include/bfdlink.h	2009-09-10 13:47:30.000000000 +0200
   150 +++ binutils-2.20.1/include/bfdlink.h	2010-08-17 19:32:11.000000000 +0200
   151 @@ -321,6 +321,14 @@
   152    /* TRUE if PT_GNU_RELRO segment should be created.  */
   153    unsigned int relro: 1;
   154  
   155 +  /* TRUE if PT_PAX_FLAGS segment should be created with PF_NOMPROTECT
   156 +     flags.  */
   157 +  unsigned int execheap: 1;
   158 +
   159 +  /* TRUE if PT_PAX_FLAGS segment should be created with PF_MPROTECT
   160 +     flags.  */
   161 +  unsigned int noexecheap: 1;
   162 +
   163    /* TRUE if we should warn when adding a DT_TEXTREL to a shared object.  */
   164    unsigned int warn_shared_textrel: 1;
   165  
   166 diff -durN binutils-2.20.1.orig/include/elf/common.h binutils-2.20.1/include/elf/common.h
   167 --- binutils-2.20.1.orig/include/elf/common.h	2009-08-09 15:42:26.000000000 +0200
   168 +++ binutils-2.20.1/include/elf/common.h	2010-08-17 19:32:11.000000000 +0200
   169 @@ -422,6 +422,7 @@
   170  #define PT_SUNW_EH_FRAME PT_GNU_EH_FRAME      /* Solaris uses the same value */
   171  #define PT_GNU_STACK	(PT_LOOS + 0x474e551) /* Stack flags */
   172  #define PT_GNU_RELRO	(PT_LOOS + 0x474e552) /* Read-only after relocation */
   173 +#define PT_PAX_FLAGS	(PT_LOOS + 0x5041580) /* PaX flags */
   174  
   175  /* Program segment permissions, in program header p_flags field.  */
   176  
   177 @@ -432,6 +433,21 @@
   178  #define PF_MASKOS	0x0FF00000	/* New value, Oct 4, 1999 Draft */
   179  #define PF_MASKPROC	0xF0000000	/* Processor-specific reserved bits */
   180  
   181 +/* Flags to control PaX behavior.  */
   182 +
   183 +#define PF_PAGEEXEC	(1 << 4)	/* Enable  PAGEEXEC */
   184 +#define PF_NOPAGEEXEC	(1 << 5)	/* Disable PAGEEXEC */
   185 +#define PF_SEGMEXEC	(1 << 6)	/* Enable  SEGMEXEC */
   186 +#define PF_NOSEGMEXEC	(1 << 7)	/* Disable SEGMEXEC */
   187 +#define PF_MPROTECT	(1 << 8)	/* Enable  MPROTECT */
   188 +#define PF_NOMPROTECT	(1 << 9)	/* Disable MPROTECT */
   189 +#define PF_RANDEXEC	(1 << 10)	/* Enable  RANDEXEC */
   190 +#define PF_NORANDEXEC	(1 << 11)	/* Disable RANDEXEC */
   191 +#define PF_EMUTRAMP	(1 << 12)	/* Enable  EMUTRAMP */
   192 +#define PF_NOEMUTRAMP	(1 << 13)	/* Disable EMUTRAMP */
   193 +#define PF_RANDMMAP	(1 << 14)	/* Enable  RANDMMAP */
   194 +#define PF_NORANDMMAP	(1 << 15)	/* Disable RANDMMAP */
   195 +
   196  /* Values for section header, sh_type field.  */
   197  
   198  #define SHT_NULL	0		/* Section header table entry unused */
   199 diff -durN binutils-2.20.1.orig/ld/emultempl/elf32.em binutils-2.20.1/ld/emultempl/elf32.em
   200 --- binutils-2.20.1.orig/ld/emultempl/elf32.em	2010-08-17 19:32:09.000000000 +0200
   201 +++ binutils-2.20.1/ld/emultempl/elf32.em	2010-08-17 19:32:11.000000000 +0200
   202 @@ -2165,6 +2165,16 @@
   203  	  link_info.noexecstack = TRUE;
   204  	  link_info.execstack = FALSE;
   205  	}
   206 +      else if (strcmp (optarg, "execheap") == 0)
   207 +	{
   208 +	  link_info.execheap = TRUE;
   209 +	  link_info.noexecheap = FALSE;
   210 +	}
   211 +      else if (strcmp (optarg, "noexecheap") == 0)
   212 +	{
   213 +	  link_info.noexecheap = TRUE;
   214 +	  link_info.execheap = FALSE;
   215 +	}
   216  EOF
   217  
   218    if test -n "$COMMONPAGESIZE"; then
   219 @@ -2243,6 +2253,8 @@
   220    fprintf (file, _("\
   221    -z execstack                Mark executable as requiring executable stack\n"));
   222    fprintf (file, _("\
   223 +  -z execheap                 Mark executable as requiring executable heap\n"));
   224 +  fprintf (file, _("\
   225    -z initfirst                Mark DSO to be initialized first at runtime\n"));
   226    fprintf (file, _("\
   227    -z interpose                Mark object to interpose all DSOs but executable\n"));
   228 @@ -2266,6 +2278,8 @@
   229    -z nodump                   Mark DSO not available to dldump\n"));
   230    fprintf (file, _("\
   231    -z noexecstack              Mark executable as not requiring executable stack\n"));
   232 +  fprintf (file, _("\
   233 +  -z noexecheap               Mark executable as not requiring executable heap\n"));
   234  EOF
   235  
   236    if test -n "$COMMONPAGESIZE"; then
   237 diff -durN binutils-2.20.1.orig/ld/ldgram.y binutils-2.20.1/ld/ldgram.y
   238 --- binutils-2.20.1.orig/ld/ldgram.y	2009-09-02 09:25:35.000000000 +0200
   239 +++ binutils-2.20.1/ld/ldgram.y	2010-08-17 19:32:11.000000000 +0200
   240 @@ -1116,6 +1116,8 @@
   241  			    $$ = exp_intop (0x6474e550);
   242  			  else if (strcmp (s, "PT_GNU_STACK") == 0)
   243  			    $$ = exp_intop (0x6474e551);
   244 +			  else if (strcmp (s, "PT_PAX_FLAGS") == 0)
   245 +			    $$ = exp_intop (0x65041580);
   246  			  else
   247  			    {
   248  			      einfo (_("\