1*38fd1498Szrj /* Implement __enable_execute_stack using mprotect(2).
2*38fd1498Szrj Copyright (C) 2011-2018 Free Software Foundation, Inc.
3*38fd1498Szrj
4*38fd1498Szrj This file is part of GCC.
5*38fd1498Szrj
6*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
7*38fd1498Szrj the terms of the GNU General Public License as published by the Free
8*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
9*38fd1498Szrj version.
10*38fd1498Szrj
11*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14*38fd1498Szrj for more details.
15*38fd1498Szrj
16*38fd1498Szrj Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj
20*38fd1498Szrj You should have received a copy of the GNU General Public License and
21*38fd1498Szrj a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23*38fd1498Szrj <http://www.gnu.org/licenses/>. */
24*38fd1498Szrj
25*38fd1498Szrj #include <sys/mman.h>
26*38fd1498Szrj #include <unistd.h>
27*38fd1498Szrj #include <stdlib.h>
28*38fd1498Szrj
29*38fd1498Szrj #define STACK_PROT_RWX (PROT_READ | PROT_WRITE | PROT_EXEC)
30*38fd1498Szrj
31*38fd1498Szrj static int need_enable_exec_stack;
32*38fd1498Szrj
33*38fd1498Szrj static void check_enabling (void) __attribute__ ((unused));
34*38fd1498Szrj extern void __enable_execute_stack (void *);
35*38fd1498Szrj
36*38fd1498Szrj #if defined __sun__ && defined __svr4__
37*38fd1498Szrj static void __attribute__ ((constructor))
check_enabling(void)38*38fd1498Szrj check_enabling (void)
39*38fd1498Szrj {
40*38fd1498Szrj int prot = (int) sysconf (_SC_STACK_PROT);
41*38fd1498Szrj
42*38fd1498Szrj if (prot != STACK_PROT_RWX)
43*38fd1498Szrj need_enable_exec_stack = 1;
44*38fd1498Szrj }
45*38fd1498Szrj #else
46*38fd1498Szrj /* There is no way to query the execute permission of the stack, so
47*38fd1498Szrj we always issue the mprotect() call. */
48*38fd1498Szrj
49*38fd1498Szrj static int need_enable_exec_stack = 1;
50*38fd1498Szrj #endif
51*38fd1498Szrj
52*38fd1498Szrj /* Attempt to turn on access permissions for the stack. Unfortunately it
53*38fd1498Szrj is not possible to make this namespace-clean.*/
54*38fd1498Szrj
55*38fd1498Szrj void
__enable_execute_stack(void * addr)56*38fd1498Szrj __enable_execute_stack (void *addr)
57*38fd1498Szrj {
58*38fd1498Szrj if (!need_enable_exec_stack)
59*38fd1498Szrj return;
60*38fd1498Szrj else
61*38fd1498Szrj {
62*38fd1498Szrj static long size, mask;
63*38fd1498Szrj
64*38fd1498Szrj if (size == 0) {
65*38fd1498Szrj size = getpagesize ();
66*38fd1498Szrj mask = ~(size - 1);
67*38fd1498Szrj }
68*38fd1498Szrj
69*38fd1498Szrj char *page = (char *) (((long) addr) & mask);
70*38fd1498Szrj char *end = (char *)
71*38fd1498Szrj ((((long) (addr + __LIBGCC_TRAMPOLINE_SIZE__)) & mask) + size);
72*38fd1498Szrj
73*38fd1498Szrj if (mprotect (page, end - page, STACK_PROT_RWX) < 0)
74*38fd1498Szrj /* Note that no errors should be emitted by this code; it is
75*38fd1498Szrj considered dangerous for library calls to send messages to
76*38fd1498Szrj stdout/stderr. */
77*38fd1498Szrj abort ();
78*38fd1498Szrj }
79*38fd1498Szrj }
80