patches/binutils/2.20/150-pt-pax-flags-20090909.patch
changeset 1614 3f76cdbceb6e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/patches/binutils/2.20/150-pt-pax-flags-20090909.patch	Tue Oct 27 20:06:46 2009 +0100
     1.3 @@ -0,0 +1,238 @@
     1.4 +--- binutils-2.20/bfd/elf-bfd.h
     1.5 ++++ binutils-2.20/bfd/elf-bfd.h
     1.6 +@@ -1527,6 +1527,9 @@ struct elf_obj_tdata
     1.7 +   /* Segment flags for the PT_GNU_STACK segment.  */
     1.8 +   unsigned int stack_flags;
     1.9 + 
    1.10 ++  /* Segment flags for the PT_PAX_FLAGS segment.  */
    1.11 ++  unsigned int pax_flags;
    1.12 ++
    1.13 +   /* Symbol version definitions in external objects.  */
    1.14 +   Elf_Internal_Verdef *verdef;
    1.15 + 
    1.16 +--- binutils-2.20/bfd/elf.c
    1.17 ++++ binutils-2.20/bfd/elf.c
    1.18 +@@ -1081,6 +1081,7 @@ get_segment_type (unsigned int p_type)
    1.19 +     case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
    1.20 +     case PT_GNU_STACK: pt = "STACK"; break;
    1.21 +     case PT_GNU_RELRO: pt = "RELRO"; break;
    1.22 ++    case PT_PAX_FLAGS: pt = "PAX_FLAGS"; break;
    1.23 +     default: pt = NULL; break;
    1.24 +     }
    1.25 +   return pt;
    1.26 +@@ -2379,6 +2380,9 @@ bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
    1.27 +     case PT_GNU_RELRO:
    1.28 +       return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
    1.29 + 
    1.30 ++    case PT_PAX_FLAGS:
    1.31 ++      return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "pax_flags");
    1.32 ++
    1.33 +     default:
    1.34 +       /* Check for any processor-specific program segment types.  */
    1.35 +       bed = get_elf_backend_data (abfd);
    1.36 +@@ -3393,6 +3397,11 @@ get_program_header_size (bfd *abfd, struct bfd_link_info *info)
    1.37 +       ++segs;
    1.38 +     }
    1.39 + 
    1.40 ++    {
    1.41 ++      /* We need a PT_PAX_FLAGS segment.  */
    1.42 ++      ++segs;
    1.43 ++    }
    1.44 ++
    1.45 +   for (s = abfd->sections; s != NULL; s = s->next)
    1.46 +     {
    1.47 +       if ((s->flags & SEC_LOAD) != 0
    1.48 +@@ -3972,6 +3981,20 @@ _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
    1.49 + 	    }
    1.50 + 	}
    1.51 + 
    1.52 ++    {
    1.53 ++      amt = sizeof (struct elf_segment_map);
    1.54 ++      m = bfd_zalloc (abfd, amt);
    1.55 ++      if (m == NULL)
    1.56 ++	goto error_return;
    1.57 ++      m->next = NULL;
    1.58 ++      m->p_type = PT_PAX_FLAGS;
    1.59 ++      m->p_flags = elf_tdata (abfd)->pax_flags;
    1.60 ++      m->p_flags_valid = 1;
    1.61 ++
    1.62 ++      *pm = m;
    1.63 ++      pm = &m->next;
    1.64 ++    }
    1.65 ++
    1.66 +       free (sections);
    1.67 +       elf_tdata (abfd)->segment_map = mfirst;
    1.68 +     }
    1.69 +@@ -5176,7 +5199,8 @@ rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
    1.70 +        6. PT_TLS segment includes only SHF_TLS sections.
    1.71 +        7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
    1.72 +        8. PT_DYNAMIC should not contain empty sections at the beginning
    1.73 +-	  (with the possible exception of .dynamic).  */
    1.74 ++	  (with the possible exception of .dynamic).
    1.75 ++       9. PT_PAX_FLAGS segments do not include any sections.  */
    1.76 + #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed)		\
    1.77 +   ((((segment->p_paddr							\
    1.78 +       ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr)	\
    1.79 +@@ -5184,6 +5208,7 @@ rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
    1.80 +      && (section->flags & SEC_ALLOC) != 0)				\
    1.81 +     || IS_NOTE (segment, section))					\
    1.82 +    && segment->p_type != PT_GNU_STACK					\
    1.83 ++   && segment->p_type != PT_PAX_FLAGS					\
    1.84 +    && (segment->p_type != PT_TLS					\
    1.85 +        || (section->flags & SEC_THREAD_LOCAL))				\
    1.86 +    && (segment->p_type == PT_LOAD					\
    1.87 +--- binutils-2.20/bfd/elflink.c
    1.88 ++++ binutils-2.20/bfd/elflink.c
    1.89 +@@ -5465,16 +5465,30 @@ bfd_elf_size_dynamic_sections (bfd *output_bfd,
    1.90 +     return TRUE;
    1.91 + 
    1.92 +   bed = get_elf_backend_data (output_bfd);
    1.93 ++
    1.94 ++  elf_tdata (output_bfd)->pax_flags = PF_NORANDEXEC;
    1.95 ++  if (info->execheap)
    1.96 ++    elf_tdata (output_bfd)->pax_flags |= PF_NOMPROTECT;
    1.97 ++  else if (info->noexecheap)
    1.98 ++    elf_tdata (output_bfd)->pax_flags |= PF_MPROTECT;
    1.99 ++
   1.100 +   if (info->execstack)
   1.101 +-    elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
   1.102 ++    {
   1.103 ++      elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
   1.104 ++      elf_tdata (output_bfd)->pax_flags |= PF_EMUTRAMP;
   1.105 ++    }
   1.106 +   else if (info->noexecstack)
   1.107 +-    elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
   1.108 ++    {
   1.109 ++      elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
   1.110 ++      elf_tdata (output_bfd)->pax_flags |= PF_NOEMUTRAMP;
   1.111 ++    }
   1.112 +   else
   1.113 +     {
   1.114 +       bfd *inputobj;
   1.115 +       asection *notesec = NULL;
   1.116 +       int exec = 0;
   1.117 + 
   1.118 ++      elf_tdata (output_bfd)->pax_flags |= PF_NOEMUTRAMP;
   1.119 +       for (inputobj = info->input_bfds;
   1.120 + 	   inputobj;
   1.121 + 	   inputobj = inputobj->link_next)
   1.122 +@@ -5487,7 +5501,11 @@ bfd_elf_size_dynamic_sections (bfd *output_bfd,
   1.123 + 	  if (s)
   1.124 + 	    {
   1.125 + 	      if (s->flags & SEC_CODE)
   1.126 +-		exec = PF_X;
   1.127 ++		{
   1.128 ++		  elf_tdata (output_bfd)->pax_flags &= ~PF_NOEMUTRAMP;
   1.129 ++		  elf_tdata (output_bfd)->pax_flags |= PF_EMUTRAMP;
   1.130 ++		  exec = PF_X;
   1.131 ++		}
   1.132 + 	      notesec = s;
   1.133 + 	    }
   1.134 + 	  else if (bed->default_execstack)
   1.135 +--- binutils-2.20/binutils/readelf.c
   1.136 ++++ binutils-2.20/binutils/readelf.c
   1.137 +@@ -2556,6 +2556,7 @@ get_segment_type (unsigned long p_type)
   1.138 + 			return "GNU_EH_FRAME";
   1.139 +     case PT_GNU_STACK:	return "GNU_STACK";
   1.140 +     case PT_GNU_RELRO:  return "GNU_RELRO";
   1.141 ++    case PT_PAX_FLAGS:  return "PAX_FLAGS";
   1.142 + 
   1.143 +     default:
   1.144 +       if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
   1.145 +--- binutils-2.20/include/bfdlink.h
   1.146 ++++ binutils-2.20/include/bfdlink.h
   1.147 +@@ -319,6 +319,14 @@ struct bfd_link_info
   1.148 +   /* TRUE if PT_GNU_RELRO segment should be created.  */
   1.149 +   unsigned int relro: 1;
   1.150 + 
   1.151 ++  /* TRUE if PT_PAX_FLAGS segment should be created with PF_NOMPROTECT
   1.152 ++     flags.  */
   1.153 ++  unsigned int execheap: 1;
   1.154 ++
   1.155 ++  /* TRUE if PT_PAX_FLAGS segment should be created with PF_MPROTECT
   1.156 ++     flags.  */
   1.157 ++  unsigned int noexecheap: 1;
   1.158 ++
   1.159 +   /* TRUE if we should warn when adding a DT_TEXTREL to a shared object.  */
   1.160 +   unsigned int warn_shared_textrel: 1;
   1.161 + 
   1.162 +--- binutils-2.20/include/elf/common.h
   1.163 ++++ binutils-2.20/include/elf/common.h
   1.164 +@@ -422,6 +422,7 @@
   1.165 + #define PT_SUNW_EH_FRAME PT_GNU_EH_FRAME      /* Solaris uses the same value */
   1.166 + #define PT_GNU_STACK	(PT_LOOS + 0x474e551) /* Stack flags */
   1.167 + #define PT_GNU_RELRO	(PT_LOOS + 0x474e552) /* Read-only after relocation */
   1.168 ++#define PT_PAX_FLAGS	(PT_LOOS + 0x5041580) /* PaX flags */
   1.169 + 
   1.170 + /* Program segment permissions, in program header p_flags field.  */
   1.171 + 
   1.172 +@@ -432,6 +433,21 @@
   1.173 + #define PF_MASKOS	0x0FF00000	/* New value, Oct 4, 1999 Draft */
   1.174 + #define PF_MASKPROC	0xF0000000	/* Processor-specific reserved bits */
   1.175 + 
   1.176 ++/* Flags to control PaX behavior.  */
   1.177 ++
   1.178 ++#define PF_PAGEEXEC	(1 << 4)	/* Enable  PAGEEXEC */
   1.179 ++#define PF_NOPAGEEXEC	(1 << 5)	/* Disable PAGEEXEC */
   1.180 ++#define PF_SEGMEXEC	(1 << 6)	/* Enable  SEGMEXEC */
   1.181 ++#define PF_NOSEGMEXEC	(1 << 7)	/* Disable SEGMEXEC */
   1.182 ++#define PF_MPROTECT	(1 << 8)	/* Enable  MPROTECT */
   1.183 ++#define PF_NOMPROTECT	(1 << 9)	/* Disable MPROTECT */
   1.184 ++#define PF_RANDEXEC	(1 << 10)	/* Enable  RANDEXEC */
   1.185 ++#define PF_NORANDEXEC	(1 << 11)	/* Disable RANDEXEC */
   1.186 ++#define PF_EMUTRAMP	(1 << 12)	/* Enable  EMUTRAMP */
   1.187 ++#define PF_NOEMUTRAMP	(1 << 13)	/* Disable EMUTRAMP */
   1.188 ++#define PF_RANDMMAP	(1 << 14)	/* Enable  RANDMMAP */
   1.189 ++#define PF_NORANDMMAP	(1 << 15)	/* Disable RANDMMAP */
   1.190 ++
   1.191 + /* Values for section header, sh_type field.  */
   1.192 + 
   1.193 + #define SHT_NULL	0		/* Section header table entry unused */
   1.194 +--- binutils-2.20/ld/emultempl/elf32.em
   1.195 ++++ binutils-2.20/ld/emultempl/elf32.em
   1.196 +@@ -2159,6 +2159,16 @@ fragment <<EOF
   1.197 + 	  link_info.noexecstack = TRUE;
   1.198 + 	  link_info.execstack = FALSE;
   1.199 + 	}
   1.200 ++      else if (strcmp (optarg, "execheap") == 0)
   1.201 ++	{
   1.202 ++	  link_info.execheap = TRUE;
   1.203 ++	  link_info.noexecheap = FALSE;
   1.204 ++	}
   1.205 ++      else if (strcmp (optarg, "noexecheap") == 0)
   1.206 ++	{
   1.207 ++	  link_info.noexecheap = TRUE;
   1.208 ++	  link_info.execheap = FALSE;
   1.209 ++	}
   1.210 + EOF
   1.211 + 
   1.212 +   if test -n "$COMMONPAGESIZE"; then
   1.213 +@@ -2237,6 +2247,8 @@ fragment <<EOF
   1.214 +   fprintf (file, _("\
   1.215 +   -z execstack                Mark executable as requiring executable stack\n"));
   1.216 +   fprintf (file, _("\
   1.217 ++  -z execheap                 Mark executable as requiring executable heap\n"));
   1.218 ++  fprintf (file, _("\
   1.219 +   -z initfirst                Mark DSO to be initialized first at runtime\n"));
   1.220 +   fprintf (file, _("\
   1.221 +   -z interpose                Mark object to interpose all DSOs but executable\n"));
   1.222 +@@ -2260,6 +2272,8 @@ fragment <<EOF
   1.223 +   -z nodump                   Mark DSO not available to dldump\n"));
   1.224 +   fprintf (file, _("\
   1.225 +   -z noexecstack              Mark executable as not requiring executable stack\n"));
   1.226 ++  fprintf (file, _("\
   1.227 ++  -z noexecheap               Mark executable as not requiring executable heap\n"));
   1.228 + EOF
   1.229 + 
   1.230 +   if test -n "$COMMONPAGESIZE"; then
   1.231 +--- binutils-2.20/ld/ldgram.y
   1.232 ++++ binutils-2.20/ld/ldgram.y
   1.233 +@@ -1116,6 +1116,8 @@ phdr_type:
   1.234 + 			    $$ = exp_intop (0x6474e550);
   1.235 + 			  else if (strcmp (s, "PT_GNU_STACK") == 0)
   1.236 + 			    $$ = exp_intop (0x6474e551);
   1.237 ++			  else if (strcmp (s, "PT_PAX_FLAGS") == 0)
   1.238 ++			    $$ = exp_intop (0x65041580);
   1.239 + 			  else
   1.240 + 			    {
   1.241 + 			      einfo (_("\