patches/binutils/2.19.1a/140-pt-pax-flags-20081101.patch
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Sun Sep 11 18:18:53 2011 +0200 (2011-09-11)
changeset 2664 346263a07115
parent 1243 patches/binutils/2.19.1/140-pt-pax-flags-20081101.patch@5d15872659ec
permissions -rw-r--r--
binutils/binutils: fixup version strings

Recently, all binutils versions have been renamed after a GPL compliance
issue was found and fixed in binutils;
http://sourceware.org/ml/binutils/2011-08/msg00198.html

Although legacy symlinks have been put in place, we should now use
the new, real version strings.

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