]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/compiler-rt/lib/enable_execute_stack.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / compiler-rt / lib / enable_execute_stack.c
1 /* ===-- enable_execute_stack.c - Implement __enable_execute_stack ---------===
2  *
3  *                     The LLVM Compiler Infrastructure
4  *
5  * This file is dual licensed under the MIT and the University of Illinois Open
6  * Source Licenses. See LICENSE.TXT for details.
7  *
8  * ===----------------------------------------------------------------------===
9  */
10
11 #include <stdint.h>
12 #include <sys/mman.h>
13
14 /* #include "config.h"
15  * FIXME: CMake - include when cmake system is ready.
16  * Remove #define HAVE_SYSCONF 1 line.
17  */
18 #define HAVE_SYSCONF 1
19
20 #ifndef __APPLE__
21 #include <unistd.h>
22 #endif /* __APPLE__ */
23
24 #if __LP64__
25         #define TRAMPOLINE_SIZE 48
26 #else
27         #define TRAMPOLINE_SIZE 40
28 #endif
29
30 /*
31  * The compiler generates calls to __enable_execute_stack() when creating 
32  * trampoline functions on the stack for use with nested functions.
33  * It is expected to mark the page(s) containing the address 
34  * and the next 48 bytes as executable.  Since the stack is normally rw-
35  * that means changing the protection on those page(s) to rwx. 
36  */
37
38 void __enable_execute_stack(void* addr)
39 {
40
41 #if __APPLE__
42         /* On Darwin, pagesize is always 4096 bytes */
43         const uintptr_t pageSize = 4096;
44 #elif !defined(HAVE_SYSCONF)
45 #error "HAVE_SYSCONF not defined! See enable_execute_stack.c"
46 #else
47         const uintptr_t pageSize = sysconf(_SC_PAGESIZE);
48 #endif /* __APPLE__ */
49
50         const uintptr_t pageAlignMask = ~(pageSize-1);
51         uintptr_t p = (uintptr_t)addr;
52         unsigned char* startPage = (unsigned char*)(p & pageAlignMask);
53         unsigned char* endPage = (unsigned char*)((p+TRAMPOLINE_SIZE+pageSize) & pageAlignMask);
54         size_t length = endPage - startPage;
55         (void) mprotect((void *)startPage, length, PROT_READ | PROT_WRITE | PROT_EXEC);
56 }
57
58