annotate libgcc/enable-execute-stack-mprotect.c @ 158:494b0b89df80 default tip

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