1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/patches/binutils/2.19.1/140-pt-pax-flags-20081101.patch Tue Nov 17 11:51:57 2009 +0100
1.3 @@ -0,0 +1,254 @@
1.4 +Original patch from: ../crosstool-NG/ct-ng.trunk/patches/binutils/2.19/140-pt-pax-flags-20081101.patch
1.5 +
1.6 +-= BEGIN original header =-
1.7 +Original patch from Gentoo:
1.8 +gentoo/src/patchsets/binutils/2.19/63_all_binutils-2.19-pt-pax-flags-20081101.patch
1.9 +
1.10 +-= END original header =-
1.11 +
1.12 +diff -durN binutils-2.19.1.orig/bfd/elf-bfd.h binutils-2.19.1/bfd/elf-bfd.h
1.13 +--- binutils-2.19.1.orig/bfd/elf-bfd.h 2008-08-21 01:28:58.000000000 +0200
1.14 ++++ binutils-2.19.1/bfd/elf-bfd.h 2009-03-08 11:57:02.000000000 +0100
1.15 +@@ -1526,6 +1526,9 @@
1.16 + /* Segment flags for the PT_GNU_STACK segment. */
1.17 + unsigned int stack_flags;
1.18 +
1.19 ++ /* Segment flags for the PT_PAX_FLAGS segment. */
1.20 ++ unsigned int pax_flags;
1.21 ++
1.22 + /* Symbol version definitions in external objects. */
1.23 + Elf_Internal_Verdef *verdef;
1.24 +
1.25 +diff -durN binutils-2.19.1.orig/bfd/elf.c binutils-2.19.1/bfd/elf.c
1.26 +--- binutils-2.19.1.orig/bfd/elf.c 2008-12-23 14:54:48.000000000 +0100
1.27 ++++ binutils-2.19.1/bfd/elf.c 2009-03-08 11:57:02.000000000 +0100
1.28 +@@ -1136,6 +1136,7 @@
1.29 + case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1.30 + case PT_GNU_STACK: pt = "STACK"; break;
1.31 + case PT_GNU_RELRO: pt = "RELRO"; break;
1.32 ++ case PT_PAX_FLAGS: pt = "PAX_FLAGS"; break;
1.33 + default: pt = NULL; break;
1.34 + }
1.35 + return pt;
1.36 +@@ -2442,6 +2443,9 @@
1.37 + case PT_GNU_RELRO:
1.38 + return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
1.39 +
1.40 ++ case PT_PAX_FLAGS:
1.41 ++ return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "pax_flags");
1.42 ++
1.43 + default:
1.44 + /* Check for any processor-specific program segment types. */
1.45 + bed = get_elf_backend_data (abfd);
1.46 +@@ -3404,6 +3408,11 @@
1.47 + ++segs;
1.48 + }
1.49 +
1.50 ++ {
1.51 ++ /* We need a PT_PAX_FLAGS segment. */
1.52 ++ ++segs;
1.53 ++ }
1.54 ++
1.55 + for (s = abfd->sections; s != NULL; s = s->next)
1.56 + {
1.57 + if ((s->flags & SEC_LOAD) != 0
1.58 +@@ -3983,6 +3992,20 @@
1.59 + }
1.60 + }
1.61 +
1.62 ++ {
1.63 ++ amt = sizeof (struct elf_segment_map);
1.64 ++ m = bfd_zalloc (abfd, amt);
1.65 ++ if (m == NULL)
1.66 ++ goto error_return;
1.67 ++ m->next = NULL;
1.68 ++ m->p_type = PT_PAX_FLAGS;
1.69 ++ m->p_flags = elf_tdata (abfd)->pax_flags;
1.70 ++ m->p_flags_valid = 1;
1.71 ++
1.72 ++ *pm = m;
1.73 ++ pm = &m->next;
1.74 ++ }
1.75 ++
1.76 + free (sections);
1.77 + elf_tdata (abfd)->segment_map = mfirst;
1.78 + }
1.79 +@@ -5160,7 +5183,8 @@
1.80 + 6. PT_TLS segment includes only SHF_TLS sections.
1.81 + 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
1.82 + 8. PT_DYNAMIC should not contain empty sections at the beginning
1.83 +- (with the possible exception of .dynamic). */
1.84 ++ (with the possible exception of .dynamic).
1.85 ++ 9. PT_PAX_FLAGS segments does not include any sections. */
1.86 + #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
1.87 + ((((segment->p_paddr \
1.88 + ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
1.89 +@@ -5168,6 +5192,7 @@
1.90 + && (section->flags & SEC_ALLOC) != 0) \
1.91 + || IS_NOTE (segment, section)) \
1.92 + && segment->p_type != PT_GNU_STACK \
1.93 ++ && segment->p_type != PT_PAX_FLAGS \
1.94 + && (segment->p_type != PT_TLS \
1.95 + || (section->flags & SEC_THREAD_LOCAL)) \
1.96 + && (segment->p_type == PT_LOAD \
1.97 +diff -durN binutils-2.19.1.orig/bfd/elflink.c binutils-2.19.1/bfd/elflink.c
1.98 +--- binutils-2.19.1.orig/bfd/elflink.c 2008-08-22 10:32:39.000000000 +0200
1.99 ++++ binutils-2.19.1/bfd/elflink.c 2009-03-08 11:57:02.000000000 +0100
1.100 +@@ -5397,16 +5397,30 @@
1.101 + return TRUE;
1.102 +
1.103 + bed = get_elf_backend_data (output_bfd);
1.104 ++ elf_tdata (output_bfd)->pax_flags = PF_NORANDEXEC;
1.105 ++
1.106 ++ if (info->execheap)
1.107 ++ elf_tdata (output_bfd)->pax_flags |= PF_NOMPROTECT;
1.108 ++ else if (info->noexecheap)
1.109 ++ elf_tdata (output_bfd)->pax_flags |= PF_MPROTECT;
1.110 ++
1.111 + if (info->execstack)
1.112 +- elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
1.113 ++ {
1.114 ++ elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
1.115 ++ elf_tdata (output_bfd)->pax_flags |= PF_EMUTRAMP;
1.116 ++ }
1.117 + else if (info->noexecstack)
1.118 +- elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
1.119 ++ {
1.120 ++ elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
1.121 ++ elf_tdata (output_bfd)->pax_flags |= PF_NOEMUTRAMP;
1.122 ++ }
1.123 + else
1.124 + {
1.125 + bfd *inputobj;
1.126 + asection *notesec = NULL;
1.127 + int exec = 0;
1.128 +
1.129 ++ elf_tdata (output_bfd)->pax_flags |= PF_NOEMUTRAMP;
1.130 + for (inputobj = info->input_bfds;
1.131 + inputobj;
1.132 + inputobj = inputobj->link_next)
1.133 +@@ -5419,7 +5433,11 @@
1.134 + if (s)
1.135 + {
1.136 + if (s->flags & SEC_CODE)
1.137 +- exec = PF_X;
1.138 ++ {
1.139 ++ elf_tdata (output_bfd)->pax_flags &= ~PF_NOEMUTRAMP;
1.140 ++ elf_tdata (output_bfd)->pax_flags |= PF_EMUTRAMP;
1.141 ++ exec = PF_X;
1.142 ++ }
1.143 + notesec = s;
1.144 + }
1.145 + else if (bed->default_execstack)
1.146 +diff -durN binutils-2.19.1.orig/binutils/readelf.c binutils-2.19.1/binutils/readelf.c
1.147 +--- binutils-2.19.1.orig/binutils/readelf.c 2008-09-17 11:00:44.000000000 +0200
1.148 ++++ binutils-2.19.1/binutils/readelf.c 2009-03-08 11:57:02.000000000 +0100
1.149 +@@ -2505,6 +2505,7 @@
1.150 + return "GNU_EH_FRAME";
1.151 + case PT_GNU_STACK: return "GNU_STACK";
1.152 + case PT_GNU_RELRO: return "GNU_RELRO";
1.153 ++ case PT_PAX_FLAGS: return "PAX_FLAGS";
1.154 +
1.155 + default:
1.156 + if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
1.157 +diff -durN binutils-2.19.1.orig/include/bfdlink.h binutils-2.19.1/include/bfdlink.h
1.158 +--- binutils-2.19.1.orig/include/bfdlink.h 2008-08-17 05:12:50.000000000 +0200
1.159 ++++ binutils-2.19.1/include/bfdlink.h 2009-03-08 11:57:02.000000000 +0100
1.160 +@@ -319,6 +319,14 @@
1.161 + /* TRUE if PT_GNU_RELRO segment should be created. */
1.162 + unsigned int relro: 1;
1.163 +
1.164 ++ /* TRUE if PT_PAX_FLAGS segment should be created with PF_NOMPROTECT
1.165 ++ flags. */
1.166 ++ unsigned int execheap: 1;
1.167 ++
1.168 ++ /* TRUE if PT_PAX_FLAGS segment should be created with PF_MPROTECT
1.169 ++ flags. */
1.170 ++ unsigned int noexecheap: 1;
1.171 ++
1.172 + /* TRUE if we should warn when adding a DT_TEXTREL to a shared object. */
1.173 + unsigned int warn_shared_textrel: 1;
1.174 +
1.175 +diff -durN binutils-2.19.1.orig/include/elf/common.h binutils-2.19.1/include/elf/common.h
1.176 +--- binutils-2.19.1.orig/include/elf/common.h 2008-08-04 01:20:42.000000000 +0200
1.177 ++++ binutils-2.19.1/include/elf/common.h 2009-03-08 11:57:02.000000000 +0100
1.178 +@@ -360,6 +360,7 @@
1.179 + #define PT_SUNW_EH_FRAME PT_GNU_EH_FRAME /* Solaris uses the same value */
1.180 + #define PT_GNU_STACK (PT_LOOS + 0x474e551) /* Stack flags */
1.181 + #define PT_GNU_RELRO (PT_LOOS + 0x474e552) /* Read-only after relocation */
1.182 ++#define PT_PAX_FLAGS (PT_LOOS + 0x5041580) /* PaX flags */
1.183 +
1.184 + /* Program segment permissions, in program header p_flags field. */
1.185 +
1.186 +@@ -370,6 +371,21 @@
1.187 + #define PF_MASKOS 0x0FF00000 /* New value, Oct 4, 1999 Draft */
1.188 + #define PF_MASKPROC 0xF0000000 /* Processor-specific reserved bits */
1.189 +
1.190 ++/* Flags to control PaX behavior. */
1.191 ++
1.192 ++#define PF_PAGEEXEC (1 << 4) /* Enable PAGEEXEC */
1.193 ++#define PF_NOPAGEEXEC (1 << 5) /* Disable PAGEEXEC */
1.194 ++#define PF_SEGMEXEC (1 << 6) /* Enable SEGMEXEC */
1.195 ++#define PF_NOSEGMEXEC (1 << 7) /* Disable SEGMEXEC */
1.196 ++#define PF_MPROTECT (1 << 8) /* Enable MPROTECT */
1.197 ++#define PF_NOMPROTECT (1 << 9) /* Disable MPROTECT */
1.198 ++#define PF_RANDEXEC (1 << 10) /* Enable RANDEXEC */
1.199 ++#define PF_NORANDEXEC (1 << 11) /* Disable RANDEXEC */
1.200 ++#define PF_EMUTRAMP (1 << 12) /* Enable EMUTRAMP */
1.201 ++#define PF_NOEMUTRAMP (1 << 13) /* Disable EMUTRAMP */
1.202 ++#define PF_RANDMMAP (1 << 14) /* Enable RANDMMAP */
1.203 ++#define PF_NORANDMMAP (1 << 15) /* Disable RANDMMAP */
1.204 ++
1.205 + /* Values for section header, sh_type field. */
1.206 +
1.207 + #define SHT_NULL 0 /* Section header table entry unused */
1.208 +diff -durN binutils-2.19.1.orig/ld/emultempl/elf32.em binutils-2.19.1/ld/emultempl/elf32.em
1.209 +--- binutils-2.19.1.orig/ld/emultempl/elf32.em 2009-03-08 11:57:01.000000000 +0100
1.210 ++++ binutils-2.19.1/ld/emultempl/elf32.em 2009-03-08 11:57:02.000000000 +0100
1.211 +@@ -2146,6 +2146,16 @@
1.212 + link_info.noexecstack = TRUE;
1.213 + link_info.execstack = FALSE;
1.214 + }
1.215 ++ else if (strcmp (optarg, "execheap") == 0)
1.216 ++ {
1.217 ++ link_info.execheap = TRUE;
1.218 ++ link_info.noexecheap = FALSE;
1.219 ++ }
1.220 ++ else if (strcmp (optarg, "noexecheap") == 0)
1.221 ++ {
1.222 ++ link_info.noexecheap = TRUE;
1.223 ++ link_info.execheap = FALSE;
1.224 ++ }
1.225 + EOF
1.226 +
1.227 + if test -n "$COMMONPAGESIZE"; then
1.228 +@@ -2229,6 +2239,8 @@
1.229 + fprintf (file, _("\
1.230 + -z execstack Mark executable as requiring executable stack\n"));
1.231 + fprintf (file, _("\
1.232 ++ -z execheap\t\tMark executable as requiring executable heap\n"));
1.233 ++ fprintf (file, _("\
1.234 + -z initfirst Mark DSO to be initialized first at runtime\n"));
1.235 + fprintf (file, _("\
1.236 + -z interpose Mark object to interpose all DSOs but executable\n"));
1.237 +@@ -2252,6 +2264,8 @@
1.238 + -z nodump Mark DSO not available to dldump\n"));
1.239 + fprintf (file, _("\
1.240 + -z noexecstack Mark executable as not requiring executable stack\n"));
1.241 ++ fprintf (file, _("\
1.242 ++ -z noexecheap\tMark executable as not requiring executable heap\n"));
1.243 + EOF
1.244 +
1.245 + if test -n "$COMMONPAGESIZE"; then
1.246 +diff -durN binutils-2.19.1.orig/ld/ldgram.y binutils-2.19.1/ld/ldgram.y
1.247 +--- binutils-2.19.1.orig/ld/ldgram.y 2008-07-06 15:38:36.000000000 +0200
1.248 ++++ binutils-2.19.1/ld/ldgram.y 2009-03-08 11:57:02.000000000 +0100
1.249 +@@ -1112,6 +1112,8 @@
1.250 + $$ = exp_intop (0x6474e550);
1.251 + else if (strcmp (s, "PT_GNU_STACK") == 0)
1.252 + $$ = exp_intop (0x6474e551);
1.253 ++ else if (strcmp (s, "PT_PAX_FLAGS") == 0)
1.254 ++ $$ = exp_intop (0x65041580);
1.255 + else
1.256 + {
1.257 + einfo (_("\