0000: 2f 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20 /* This file is
0010: 64 65 72 69 76 65 64 20 66 72 6f 6d 20 74 68 65 derived from the
0020: 20 47 43 43 20 73 6f 75 72 63 65 73 2e 20 20 2a GCC sources. *
0030: 2f 0a 0a 2f 2a 20 43 6f 70 79 72 69 67 68 74 20 /../* Copyright
0040: 28 43 29 20 31 39 38 39 2d 32 30 30 36 20 46 72 (C) 1989-2006 Fr
0050: 65 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e ee Software Foun
0060: 64 61 74 69 6f 6e 2c 20 49 6e 63 2e 0a 0a 54 68 dation, Inc...Th
0070: 69 73 20 66 69 6c 65 20 69 73 20 70 61 72 74 20 is file is part
0080: 6f 66 20 47 4e 55 20 43 43 2e 0a 0a 47 4e 55 20 of GNU CC...GNU
0090: 43 43 20 69 73 20 66 72 65 65 20 73 6f 66 74 77 CC is free softw
00a0: 61 72 65 3b 20 79 6f 75 20 63 61 6e 20 72 65 64 are; you can red
00b0: 69 73 74 72 69 62 75 74 65 20 69 74 20 61 6e 64 istribute it and
00c0: 2f 6f 72 20 6d 6f 64 69 66 79 0a 69 74 20 75 6e /or modify.it un
00d0: 64 65 72 20 74 68 65 20 74 65 72 6d 73 20 6f 66 der the terms of
00e0: 20 74 68 65 20 47 4e 55 20 47 65 6e 65 72 61 6c the GNU General
00f0: 20 50 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 Public License
0100: 61 73 20 70 75 62 6c 69 73 68 65 64 20 62 79 0a as published by.
0110: 74 68 65 20 46 72 65 65 20 53 6f 66 74 77 61 72 the Free Softwar
0120: 65 20 46 6f 75 6e 64 61 74 69 6f 6e 3b 20 65 69 e Foundation; ei
0130: 74 68 65 72 20 76 65 72 73 69 6f 6e 20 32 2c 20 ther version 2,
0140: 6f 72 20 28 61 74 20 79 6f 75 72 20 6f 70 74 69 or (at your opti
0150: 6f 6e 29 0a 61 6e 79 20 6c 61 74 65 72 20 76 65 on).any later ve
0160: 72 73 69 6f 6e 2e 0a 0a 47 4e 55 20 43 43 20 69 rsion...GNU CC i
0170: 73 20 64 69 73 74 72 69 62 75 74 65 64 20 69 6e s distributed in
0180: 20 74 68 65 20 68 6f 70 65 20 74 68 61 74 20 69 the hope that i
0190: 74 20 77 69 6c 6c 20 62 65 20 75 73 65 66 75 6c t will be useful
01a0: 2c 0a 62 75 74 20 57 49 54 48 4f 55 54 20 41 4e ,.but WITHOUT AN
01b0: 59 20 57 41 52 52 41 4e 54 59 3b 20 77 69 74 68 Y WARRANTY; with
01c0: 6f 75 74 20 65 76 65 6e 20 74 68 65 20 69 6d 70 out even the imp
01d0: 6c 69 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66 lied warranty of
01e0: 0a 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54 59 .MERCHANTABILITY
01f0: 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52 20 or FITNESS FOR
0200: 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 55 52 A PARTICULAR PUR
0210: 50 4f 53 45 2e 20 20 53 65 65 20 74 68 65 0a 47 POSE. See the.G
0220: 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 NU General Publi
0230: 63 20 4c 69 63 65 6e 73 65 20 66 6f 72 20 6d 6f c License for mo
0240: 72 65 20 64 65 74 61 69 6c 73 2e 0a 0a 59 6f 75 re details...You
0250: 20 73 68 6f 75 6c 64 20 68 61 76 65 20 72 65 63 should have rec
0260: 65 69 76 65 64 20 61 20 63 6f 70 79 20 6f 66 20 eived a copy of
0270: 74 68 65 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 the GNU General
0280: 50 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 0a 61 Public License.a
0290: 6c 6f 6e 67 20 77 69 74 68 20 47 4e 55 20 43 43 long with GNU CC
02a0: 3b 20 73 65 65 20 74 68 65 20 66 69 6c 65 20 43 ; see the file C
02b0: 4f 50 59 49 4e 47 2e 20 20 49 66 20 6e 6f 74 2c OPYING. If not,
02c0: 20 77 72 69 74 65 20 74 6f 0a 74 68 65 20 46 72 write to.the Fr
02d0: 65 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e ee Software Foun
02e0: 64 61 74 69 6f 6e 2c 20 36 37 35 20 4d 61 73 73 dation, 675 Mass
02f0: 20 41 76 65 2c 20 43 61 6d 62 72 69 64 67 65 2c Ave, Cambridge,
0300: 20 4d 41 20 30 32 31 33 39 2c 20 55 53 41 2e 20 MA 02139, USA.
0310: 20 2a 2f 0a 0a 0a 2f 2a 20 54 61 6b 65 6e 20 66 */.../* Taken f
0320: 72 6f 6d 20 67 63 63 2d 34 2e 31 2e 30 2f 67 63 rom gcc-4.1.0/gc
0330: 63 2f 63 6f 6e 66 69 67 2f 61 6c 70 68 61 2f 6f c/config/alpha/o
0340: 73 66 2e 68 2e 20 20 2a 2f 0a 0a 23 69 66 20 64 sf.h. */..#if d
0350: 65 66 69 6e 65 64 20 28 5f 5f 61 6c 70 68 61 5f efined (__alpha_
0360: 5f 29 0a 0a 76 6f 69 64 0a 5f 5f 65 6e 61 62 6c _)..void.__enabl
0370: 65 5f 65 78 65 63 75 74 65 5f 73 74 61 63 6b 20 e_execute_stack
0380: 28 76 6f 69 64 20 2a 61 64 64 72 29 0a 7b 0a 20 (void *addr).{.
0390: 20 6c 6f 6e 67 20 73 69 7a 65 20 3d 20 67 65 74 long size = get
03a0: 70 61 67 65 73 69 7a 65 20 28 29 3b 0a 20 20 6c pagesize ();. l
03b0: 6f 6e 67 20 6d 61 73 6b 20 3d 20 7e 28 73 69 7a ong mask = ~(siz
03c0: 65 2d 31 29 3b 0a 20 20 63 68 61 72 20 2a 70 61 e-1);. char *pa
03d0: 67 65 20 3d 20 28 63 68 61 72 20 2a 29 20 28 28 ge = (char *) ((
03e0: 28 6c 6f 6e 67 29 20 61 64 64 72 29 20 26 20 6d (long) addr) & m
03f0: 61 73 6b 29 3b 0a 20 20 63 68 61 72 20 2a 65 6e ask);. char *en
0400: 64 20 20 3d 20 28 63 68 61 72 20 2a 29 20 28 28 d = (char *) ((
0410: 28 28 6c 6f 6e 67 29 20 28 61 64 64 72 20 2b 20 ((long) (addr +
0420: 54 52 41 4d 50 4f 4c 49 4e 45 5f 53 49 5a 45 29 TRAMPOLINE_SIZE)
0430: 29 20 26 20 6d 61 73 6b 29 20 2b 20 73 69 7a 65 ) & mask) + size
0440: 29 3b 0a 0a 20 20 2f 2a 20 37 20 69 73 20 50 52 );.. /* 7 is PR
0450: 4f 54 5f 52 45 41 44 20 7c 20 50 52 4f 54 5f 57 OT_READ | PROT_W
0460: 52 49 54 45 20 7c 20 50 52 4f 54 5f 45 58 45 43 RITE | PROT_EXEC
0470: 20 2a 2f 0a 20 20 69 66 20 28 6d 70 72 6f 74 65 */. if (mprote
0480: 63 74 20 28 70 61 67 65 2c 20 65 6e 64 20 2d 20 ct (page, end -
0490: 70 61 67 65 2c 20 37 29 20 3c 20 30 29 0a 20 20 page, 7) < 0).
04a0: 20 20 70 65 72 72 6f 72 20 28 22 6d 70 72 6f 74 perror ("mprot
04b0: 65 63 74 20 6f 66 20 74 72 61 6d 70 6f 6c 69 6e ect of trampolin
04c0: 65 20 63 6f 64 65 22 29 3b 0a 7d 0a 0a 23 65 6e e code");.}..#en
04d0: 64 69 66 0a 0a 2f 2a 20 54 61 6b 65 6e 20 66 72 dif../* Taken fr
04e0: 6f 6d 20 67 63 63 2d 34 2e 31 2e 30 2f 67 63 63 om gcc-4.1.0/gcc
04f0: 2f 63 6f 6e 66 69 67 2f 73 70 61 72 63 2f 66 72 /config/sparc/fr
0500: 65 65 62 73 64 2e 68 2e 20 20 2a 2f 0a 0a 23 69 eebsd.h. */..#i
0510: 66 20 64 65 66 69 6e 65 64 20 28 5f 5f 73 70 61 f defined (__spa
0520: 72 63 5f 5f 29 0a 0a 65 78 74 65 72 6e 20 69 6e rc__)..extern in
0530: 74 20 73 79 73 63 74 6c 62 79 6e 61 6d 65 28 63 t sysctlbyname(c
0540: 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 76 6f 69 onst char *, voi
0550: 64 20 2a 2c 20 73 69 7a 65 5f 74 20 2a 2c 20 76 d *, size_t *, v
0560: 6f 69 64 20 2a 2c 20 73 69 7a 65 5f 74 29 3b 0a oid *, size_t);.
0570: 73 74 61 74 69 63 20 69 6e 74 20 6e 65 65 64 5f static int need_
0580: 65 6e 61 62 6c 65 5f 65 78 65 63 5f 73 74 61 63 enable_exec_stac
0590: 6b 3b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 k;.static void c
05a0: 68 65 63 6b 5f 65 6e 61 62 6c 69 6e 67 28 76 6f heck_enabling(vo
05b0: 69 64 29 20 5f 5f 61 74 74 72 69 62 75 74 65 5f id) __attribute_
05c0: 5f 20 28 28 63 6f 6e 73 74 72 75 63 74 6f 72 29 _ ((constructor)
05d0: 29 3b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 );.static void c
05e0: 68 65 63 6b 5f 65 6e 61 62 6c 69 6e 67 28 76 6f heck_enabling(vo
05f0: 69 64 29 0a 7b 0a 20 20 69 6e 74 20 70 72 6f 74 id).{. int prot
0600: 20 3d 20 30 3b 0a 20 20 73 69 7a 65 5f 74 20 6c = 0;. size_t l
0610: 65 6e 20 3d 20 73 69 7a 65 6f 66 28 70 72 6f 74 en = sizeof(prot
0620: 29 3b 0a 0a 20 20 73 79 73 63 74 6c 62 79 6e 61 );.. sysctlbyna
0630: 6d 65 20 28 22 6b 65 72 6e 2e 73 74 61 63 6b 70 me ("kern.stackp
0640: 72 6f 74 22 2c 20 26 70 72 6f 74 2c 20 26 6c 65 rot", &prot, &le
0650: 6e 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a 20 20 69 n, NULL, 0);. i
0660: 66 20 28 70 72 6f 74 20 21 3d 20 37 29 0a 20 20 f (prot != 7).
0670: 20 20 6e 65 65 64 5f 65 6e 61 62 6c 65 5f 65 78 need_enable_ex
0680: 65 63 5f 73 74 61 63 6b 20 3d 20 31 3b 0a 7d 0a ec_stack = 1;.}.
0690: 76 6f 69 64 20 5f 5f 65 6e 61 62 6c 65 5f 65 78 void __enable_ex
06a0: 65 63 75 74 65 5f 73 74 61 63 6b 20 28 76 6f 69 ecute_stack (voi
06b0: 64 20 2a 61 64 64 72 29 0a 7b 0a 20 20 69 66 20 d *addr).{. if
06c0: 28 21 6e 65 65 64 5f 65 6e 61 62 6c 65 5f 65 78 (!need_enable_ex
06d0: 65 63 5f 73 74 61 63 6b 29 0a 20 20 20 20 72 65 ec_stack). re
06e0: 74 75 72 6e 3b 0a 20 20 65 6c 73 65 20 7b 0a 20 turn;. else {.
06f0: 20 20 20 2f 2a 20 37 20 69 73 20 50 52 4f 54 5f /* 7 is PROT_
0700: 52 45 41 44 20 7c 20 50 52 4f 54 5f 57 52 49 54 READ | PROT_WRIT
0710: 45 20 7c 20 50 52 4f 54 5f 45 58 45 43 20 2a 2f E | PROT_EXEC */
0720: 0a 20 20 20 20 69 66 20 28 6d 70 72 6f 74 65 63 . if (mprotec
0730: 74 20 28 61 64 64 72 2c 20 54 52 41 4d 50 4f 4c t (addr, TRAMPOL
0740: 49 4e 45 5f 53 49 5a 45 2c 20 37 29 20 3c 20 30 INE_SIZE, 7) < 0
0750: 29 0a 20 20 20 20 20 20 70 65 72 72 6f 72 20 28 ). perror (
0760: 22 6d 70 72 6f 74 65 63 74 20 6f 66 20 74 72 61 "mprotect of tra
0770: 6d 70 6f 6c 69 6e 65 20 63 6f 64 65 22 29 3b 0a mpoline code");.
0780: 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 0a 0a 2f }.}..#endif../
0790: 2a 20 54 61 6b 65 6e 20 66 72 6f 6d 20 67 63 63 * Taken from gcc
07a0: 2d 34 2e 31 2e 30 2f 67 63 63 2f 63 6f 6e 66 69 -4.1.0/gcc/confi
07b0: 67 2f 73 6f 6c 32 2e 68 2e 20 20 2a 2f 0a 0a 23 g/sol2.h. */..#
07c0: 69 66 20 28 64 65 66 69 6e 65 64 20 28 5f 5f 73 if (defined (__s
07d0: 70 61 72 63 5f 5f 29 20 7c 7c 20 64 65 66 69 6e parc__) || defin
07e0: 65 64 20 28 5f 5f 69 33 38 36 5f 5f 29 29 20 26 ed (__i386__)) &
07f0: 26 20 64 65 66 69 6e 65 64 28 5f 5f 73 76 72 34 & defined(__svr4
0800: 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f __) && defined(_
0810: 5f 73 75 6e 29 0a 0a 65 78 74 65 72 6e 20 6c 6f _sun)..extern lo
0820: 6e 67 20 73 79 73 63 6f 6e 66 28 69 6e 74 29 3b ng sysconf(int);
0830: 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 65 65 ..static int nee
0840: 64 5f 65 6e 61 62 6c 65 5f 65 78 65 63 5f 73 74 d_enable_exec_st
0850: 61 63 6b 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 ack;..static voi
0860: 64 20 63 68 65 63 6b 5f 65 6e 61 62 6c 69 6e 67 d check_enabling
0870: 28 76 6f 69 64 29 20 5f 5f 61 74 74 72 69 62 75 (void) __attribu
0880: 74 65 5f 5f 20 28 28 63 6f 6e 73 74 72 75 63 74 te__ ((construct
0890: 6f 72 29 29 3b 0a 73 74 61 74 69 63 20 76 6f 69 or));.static voi
08a0: 64 20 63 68 65 63 6b 5f 65 6e 61 62 6c 69 6e 67 d check_enabling
08b0: 28 76 6f 69 64 29 0a 7b 0a 20 20 69 6e 74 20 70 (void).{. int p
08c0: 72 6f 74 20 3d 20 28 69 6e 74 29 20 73 79 73 63 rot = (int) sysc
08d0: 6f 6e 66 28 35 31 35 20 2f 2a 20 5f 53 43 5f 53 onf(515 /* _SC_S
08e0: 54 41 43 4b 5f 50 52 4f 54 20 2a 2f 29 3b 0a 20 TACK_PROT */);.
08f0: 20 69 66 20 28 70 72 6f 74 20 21 3d 20 37 20 2f if (prot != 7 /
0900: 2a 20 53 54 41 43 4b 5f 50 52 4f 54 5f 52 57 58 * STACK_PROT_RWX
0910: 20 2a 2f 29 0a 20 20 20 20 6e 65 65 64 5f 65 6e */). need_en
0920: 61 62 6c 65 5f 65 78 65 63 5f 73 74 61 63 6b 20 able_exec_stack
0930: 3d 20 31 3b 0a 7d 0a 0a 76 6f 69 64 0a 5f 5f 65 = 1;.}..void.__e
0940: 6e 61 62 6c 65 5f 65 78 65 63 75 74 65 5f 73 74 nable_execute_st
0950: 61 63 6b 20 28 76 6f 69 64 20 2a 61 64 64 72 29 ack (void *addr)
0960: 0a 7b 0a 20 20 69 66 20 28 21 6e 65 65 64 5f 65 .{. if (!need_e
0970: 6e 61 62 6c 65 5f 65 78 65 63 5f 73 74 61 63 6b nable_exec_stack
0980: 29 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 ). return;.
0990: 65 6c 73 65 20 7b 0a 20 20 20 20 6c 6f 6e 67 20 else {. long
09a0: 73 69 7a 65 20 3d 20 67 65 74 70 61 67 65 73 69 size = getpagesi
09b0: 7a 65 20 28 29 3b 0a 20 20 20 20 6c 6f 6e 67 20 ze ();. long
09c0: 6d 61 73 6b 20 3d 20 7e 28 73 69 7a 65 2d 31 29 mask = ~(size-1)
09d0: 3b 0a 20 20 20 20 63 68 61 72 20 2a 70 61 67 65 ;. char *page
09e0: 20 3d 20 28 63 68 61 72 20 2a 29 20 28 28 28 6c = (char *) (((l
09f0: 6f 6e 67 29 20 61 64 64 72 29 20 26 20 6d 61 73 ong) addr) & mas
0a00: 6b 29 3b 0a 20 20 20 20 63 68 61 72 20 2a 65 6e k);. char *en
0a10: 64 20 20 3d 20 28 63 68 61 72 20 2a 29 20 28 28 d = (char *) ((
0a20: 28 28 6c 6f 6e 67 29 20 28 61 64 64 72 20 2b 20 ((long) (addr +
0a30: 54 52 41 4d 50 4f 4c 49 4e 45 5f 53 49 5a 45 29 TRAMPOLINE_SIZE)
0a40: 29 20 26 20 6d 61 73 6b 29 20 2b 20 73 69 7a 65 ) & mask) + size
0a50: 29 3b 0a 0a 20 20 20 20 69 66 20 28 6d 70 72 6f );.. if (mpro
0a60: 74 65 63 74 20 28 70 61 67 65 2c 20 65 6e 64 20 tect (page, end
0a70: 2d 20 70 61 67 65 2c 20 37 20 2f 2a 20 53 54 41 - page, 7 /* STA
0a80: 43 4b 5f 50 52 4f 54 5f 52 57 58 20 2a 2f 29 20 CK_PROT_RWX */)
0a90: 3c 20 30 29 0a 20 20 20 20 20 20 70 65 72 72 6f < 0). perro
0aa0: 72 20 28 22 6d 70 72 6f 74 65 63 74 20 6f 66 20 r ("mprotect of
0ab0: 74 72 61 6d 70 6f 6c 69 6e 65 20 63 6f 64 65 22 trampoline code"
0ac0: 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 );. }.}..#endif
0ad0: 0a 0a 2f 2a 20 54 61 6b 65 6e 20 66 72 6f 6d 20 ../* Taken from
0ae0: 67 63 63 2d 34 2e 31 2e 30 2f 67 63 63 2f 63 6f gcc-4.1.0/gcc/co
0af0: 6e 66 69 67 2f 6f 70 65 6e 62 73 64 2e 68 2e 20 nfig/openbsd.h.
0b00: 20 2a 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 */..#if defined
0b10: 20 28 5f 5f 4f 70 65 6e 42 53 44 5f 5f 29 0a 0a (__OpenBSD__)..
0b20: 2f 2a 20 53 74 61 63 6b 20 69 73 20 65 78 70 6c /* Stack is expl
0b30: 69 63 69 74 6c 79 20 64 65 6e 69 65 64 20 65 78 icitly denied ex
0b40: 65 63 75 74 69 6f 6e 20 72 69 67 68 74 73 20 6f ecution rights o
0b50: 6e 20 4f 70 65 6e 42 53 44 20 70 6c 61 74 66 6f n OpenBSD platfo
0b60: 72 6d 73 2e 20 20 2a 2f 0a 76 6f 69 64 0a 5f 5f rms. */.void.__
0b70: 65 6e 61 62 6c 65 5f 65 78 65 63 75 74 65 5f 73 enable_execute_s
0b80: 74 61 63 6b 20 28 76 6f 69 64 20 2a 61 64 64 72 tack (void *addr
0b90: 29 0a 7b 0a 20 20 6c 6f 6e 67 20 73 69 7a 65 20 ).{. long size
0ba0: 3d 20 67 65 74 70 61 67 65 73 69 7a 65 20 28 29 = getpagesize ()
0bb0: 3b 0a 20 20 6c 6f 6e 67 20 6d 61 73 6b 20 3d 20 ;. long mask =
0bc0: 7e 28 73 69 7a 65 2d 31 29 3b 0a 20 20 63 68 61 ~(size-1);. cha
0bd0: 72 20 2a 70 61 67 65 20 3d 20 28 63 68 61 72 20 r *page = (char
0be0: 2a 29 20 28 28 28 6c 6f 6e 67 29 20 61 64 64 72 *) (((long) addr
0bf0: 29 20 26 20 6d 61 73 6b 29 3b 0a 20 20 63 68 61 ) & mask);. cha
0c00: 72 20 2a 65 6e 64 20 20 3d 20 28 63 68 61 72 20 r *end = (char
0c10: 2a 29 20 28 28 28 28 6c 6f 6e 67 29 20 28 61 64 *) ((((long) (ad
0c20: 64 72 20 2b 20 54 52 41 4d 50 4f 4c 49 4e 45 5f dr + TRAMPOLINE_
0c30: 53 49 5a 45 29 29 20 26 20 6d 61 73 6b 29 20 2b SIZE)) & mask) +
0c40: 20 73 69 7a 65 29 3b 0a 0a 20 20 69 66 20 28 6d size);.. if (m
0c50: 70 72 6f 74 65 63 74 20 28 70 61 67 65 2c 20 65 protect (page, e
0c60: 6e 64 20 2d 20 70 61 67 65 2c 20 50 52 4f 54 5f nd - page, PROT_
0c70: 52 45 41 44 20 7c 20 50 52 4f 54 5f 57 52 49 54 READ | PROT_WRIT
0c80: 45 20 7c 20 50 52 4f 54 5f 45 58 45 43 29 20 3c E | PROT_EXEC) <
0c90: 20 30 29 0a 20 20 20 20 70 65 72 72 6f 72 20 28 0). perror (
0ca0: 22 6d 70 72 6f 74 65 63 74 20 6f 66 20 74 72 61 "mprotect of tra
0cb0: 6d 70 6f 6c 69 6e 65 20 63 6f 64 65 22 29 3b 0a mpoline code");.
0cc0: 7d 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 54 61 }..#endif../* Ta
0cd0: 6b 65 6e 20 66 72 6f 6d 20 67 63 63 2d 34 2e 31 ken from gcc-4.1
0ce0: 2e 30 2f 67 63 63 2f 63 6f 6e 66 69 67 2f 6e 65 .0/gcc/config/ne
0cf0: 74 62 73 64 2e 68 2e 20 20 2a 2f 0a 0a 23 69 66 tbsd.h. */..#if
0d00: 20 64 65 66 69 6e 65 64 20 28 5f 5f 4e 65 74 42 defined (__NetB
0d10: 53 44 5f 5f 29 0a 0a 65 78 74 65 72 6e 20 69 6e SD__)..extern in
0d20: 74 20 5f 5f 73 79 73 63 74 6c 20 28 69 6e 74 20 t __sysctl (int
0d30: 2a 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 2c *, unsigned int,
0d40: 20 76 6f 69 64 20 2a 2c 20 73 69 7a 65 5f 74 20 void *, size_t
0d50: 2a 2c 20 76 6f 69 64 20 2a 2c 20 73 69 7a 65 5f *, void *, size_
0d60: 74 29 3b 0a 0a 76 6f 69 64 0a 5f 5f 65 6e 61 62 t);..void.__enab
0d70: 6c 65 5f 65 78 65 63 75 74 65 5f 73 74 61 63 6b le_execute_stack
0d80: 20 28 76 6f 69 64 20 2a 61 64 64 72 29 0a 7b 0a (void *addr).{.
0d90: 20 20 73 74 61 74 69 63 20 69 6e 74 20 73 69 7a static int siz
0da0: 65 3b 0a 20 20 73 74 61 74 69 63 20 6c 6f 6e 67 e;. static long
0db0: 20 6d 61 73 6b 3b 0a 0a 20 20 63 68 61 72 20 2a mask;.. char *
0dc0: 70 61 67 65 2c 20 2a 65 6e 64 3b 0a 0a 20 20 69 page, *end;.. i
0dd0: 66 20 28 73 69 7a 65 20 3d 3d 20 30 29 0a 20 20 f (size == 0).
0de0: 20 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6d 69 {. int mi
0df0: 62 5b 32 5d 3b 0a 20 20 20 20 20 20 73 69 7a 65 b[2];. size
0e00: 5f 74 20 6c 65 6e 3b 0a 0a 20 20 20 20 20 20 6d _t len;.. m
0e10: 69 62 5b 30 5d 20 3d 20 36 3b 20 2f 2a 20 43 54 ib[0] = 6; /* CT
0e20: 4c 5f 48 57 20 2a 2f 0a 20 20 20 20 20 20 6d 69 L_HW */. mi
0e30: 62 5b 31 5d 20 3d 20 37 3b 20 2f 2a 20 48 57 5f b[1] = 7; /* HW_
0e40: 50 41 47 45 53 49 5a 45 20 2a 2f 0a 20 20 20 20 PAGESIZE */.
0e50: 20 20 6c 65 6e 20 3d 20 73 69 7a 65 6f 66 20 28 len = sizeof (
0e60: 73 69 7a 65 29 3b 0a 20 20 20 20 20 20 28 76 6f size);. (vo
0e70: 69 64 29 20 5f 5f 73 79 73 63 74 6c 20 28 6d 69 id) __sysctl (mi
0e80: 62 2c 20 32 2c 20 26 73 69 7a 65 2c 20 26 6c 65 b, 2, &size, &le
0e90: 6e 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a 20 20 20 n, NULL, 0);.
0ea0: 20 20 20 6d 61 73 6b 20 3d 20 7e 28 28 6c 6f 6e mask = ~((lon
0eb0: 67 29 20 73 69 7a 65 20 2d 20 31 29 3b 0a 20 20 g) size - 1);.
0ec0: 20 20 7d 0a 0a 20 20 70 61 67 65 20 3d 20 28 63 }.. page = (c
0ed0: 68 61 72 20 2a 29 20 28 28 28 6c 6f 6e 67 29 20 har *) (((long)
0ee0: 61 64 64 72 29 20 26 20 6d 61 73 6b 29 3b 0a 20 addr) & mask);.
0ef0: 20 65 6e 64 20 20 3d 20 28 63 68 61 72 20 2a 29 end = (char *)
0f00: 20 28 28 28 28 6c 6f 6e 67 29 20 28 61 64 64 72 ((((long) (addr
0f10: 20 2b 20 54 52 41 4d 50 4f 4c 49 4e 45 5f 53 49 + TRAMPOLINE_SI
0f20: 5a 45 29 29 20 26 20 6d 61 73 6b 29 20 2b 20 73 ZE)) & mask) + s
0f30: 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 37 20 3d 3d ize);.. /* 7 ==
0f40: 20 50 52 4f 54 5f 52 45 41 44 20 7c 20 50 52 4f PROT_READ | PRO
0f50: 54 5f 57 52 49 54 45 20 7c 20 50 52 4f 54 5f 45 T_WRITE | PROT_E
0f60: 58 45 43 20 2a 2f 0a 20 20 28 76 6f 69 64 29 20 XEC */. (void)
0f70: 6d 70 72 6f 74 65 63 74 20 28 70 61 67 65 2c 20 mprotect (page,
0f80: 65 6e 64 20 2d 20 70 61 67 65 2c 20 37 29 3b 0a end - page, 7);.
0f90: 7d 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 54 68 }..#endif../* Th
0fa0: 65 20 72 65 73 74 20 69 73 20 74 61 6b 65 6e 20 e rest is taken
0fb0: 66 72 6f 6d 20 67 63 63 2d 32 2e 36 2e 33 2f 6c from gcc-2.6.3/l
0fc0: 69 62 67 63 63 32 2e 63 2e 20 20 2a 2f 0a 0a 23 ibgcc2.c. */..#
0fd0: 69 66 20 64 65 66 69 6e 65 64 20 28 4e 65 58 54 if defined (NeXT
0fe0: 29 20 26 26 20 64 65 66 69 6e 65 64 20 28 5f 5f ) && defined (__
0ff0: 4d 41 43 48 5f 5f 29 0a 0a 2f 2a 20 4d 61 6b 65 MACH__)../* Make
1000: 20 73 74 61 63 6b 20 65 78 65 63 75 74 61 62 6c stack executabl
1010: 65 20 73 6f 20 77 65 20 63 61 6e 20 63 61 6c 6c e so we can call
1020: 20 74 72 61 6d 70 6f 6c 69 6e 65 73 20 6f 6e 20 trampolines on
1030: 73 74 61 63 6b 2e 0a 20 20 20 54 68 69 73 20 69 stack.. This i
1040: 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 49 4e s called from IN
1050: 49 54 49 41 4c 49 5a 45 5f 54 52 41 4d 50 4f 4c ITIALIZE_TRAMPOL
1060: 49 4e 45 20 69 6e 20 6e 65 78 74 2e 68 2e 20 20 INE in next.h.
1070: 2a 2f 0a 23 69 66 64 65 66 20 4e 65 58 54 53 74 */.#ifdef NeXTSt
1080: 65 70 32 31 0a 20 23 69 6e 63 6c 75 64 65 20 3c ep21. #include <
1090: 6d 61 63 68 2e 68 3e 0a 23 65 6c 73 65 0a 20 23 mach.h>.#else. #
10a0: 69 6e 63 6c 75 64 65 20 3c 6d 61 63 68 2f 6d 61 include <mach/ma
10b0: 63 68 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 76 6f ch.h>.#endif..vo
10c0: 69 64 0a 5f 5f 65 6e 61 62 6c 65 5f 65 78 65 63 id.__enable_exec
10d0: 75 74 65 5f 73 74 61 63 6b 20 28 61 64 64 72 29 ute_stack (addr)
10e0: 0a 20 20 20 20 20 63 68 61 72 20 2a 61 64 64 72 . char *addr
10f0: 3b 0a 7b 0a 20 20 6b 65 72 6e 5f 72 65 74 75 72 ;.{. kern_retur
1100: 6e 5f 74 20 72 3b 0a 20 20 63 68 61 72 20 2a 65 n_t r;. char *e
1110: 61 64 64 72 20 3d 20 61 64 64 72 20 2b 20 54 52 addr = addr + TR
1120: 41 4d 50 4f 4c 49 4e 45 5f 53 49 5a 45 3b 0a 20 AMPOLINE_SIZE;.
1130: 20 76 6d 5f 61 64 64 72 65 73 73 5f 74 20 61 20 vm_address_t a
1140: 3d 20 28 76 6d 5f 61 64 64 72 65 73 73 5f 74 29 = (vm_address_t)
1150: 20 61 64 64 72 3b 0a 0a 20 20 2f 2a 20 74 75 72 addr;.. /* tur
1160: 6e 20 6f 6e 20 65 78 65 63 75 74 65 20 61 63 63 n on execute acc
1170: 65 73 73 20 6f 6e 20 73 74 61 63 6b 20 2a 2f 0a ess on stack */.
1180: 20 20 72 20 3d 20 76 6d 5f 70 72 6f 74 65 63 74 r = vm_protect
1190: 20 28 74 61 73 6b 5f 73 65 6c 66 20 28 29 2c 20 (task_self (),
11a0: 61 2c 20 54 52 41 4d 50 4f 4c 49 4e 45 5f 53 49 a, TRAMPOLINE_SI
11b0: 5a 45 2c 20 46 41 4c 53 45 2c 20 56 4d 5f 50 52 ZE, FALSE, VM_PR
11c0: 4f 54 5f 41 4c 4c 29 3b 0a 20 20 69 66 20 28 72 OT_ALL);. if (r
11d0: 20 21 3d 20 4b 45 52 4e 5f 53 55 43 43 45 53 53 != KERN_SUCCESS
11e0: 29 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 6d 61 ). {. ma
11f0: 63 68 5f 65 72 72 6f 72 28 22 76 6d 5f 70 72 6f ch_error("vm_pro
1200: 74 65 63 74 20 56 4d 5f 50 52 4f 54 5f 41 4c 4c tect VM_PROT_ALL
1210: 22 2c 20 72 29 3b 0a 20 20 20 20 20 20 65 78 69 ", r);. exi
1220: 74 28 31 29 3b 0a 20 20 20 20 7d 0a 7d 20 0a 0a t(1);. }.} ..
1230: 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 #endif /* define
1240: 64 20 28 4e 65 58 54 29 20 26 26 20 64 65 66 69 d (NeXT) && defi
1250: 6e 65 64 20 28 5f 5f 4d 41 43 48 5f 5f 29 20 2a ned (__MACH__) *
1260: 2f 0a 0a 23 69 66 64 65 66 20 5f 5f 63 6f 6e 76 /..#ifdef __conv
1270: 65 78 5f 5f 0a 0a 2f 2a 20 4d 61 6b 65 20 73 74 ex__../* Make st
1280: 61 63 6b 20 65 78 65 63 75 74 61 62 6c 65 20 73 ack executable s
1290: 6f 20 77 65 20 63 61 6e 20 63 61 6c 6c 20 74 72 o we can call tr
12a0: 61 6d 70 6f 6c 69 6e 65 73 20 6f 6e 20 73 74 61 ampolines on sta
12b0: 63 6b 2e 0a 20 20 20 54 68 69 73 20 69 73 20 63 ck.. This is c
12c0: 61 6c 6c 65 64 20 66 72 6f 6d 20 49 4e 49 54 49 alled from INITI
12d0: 41 4c 49 5a 45 5f 54 52 41 4d 50 4f 4c 49 4e 45 ALIZE_TRAMPOLINE
12e0: 20 69 6e 20 63 6f 6e 76 65 78 2e 68 2e 20 20 2a in convex.h. *
12f0: 2f 0a 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 /..#include <sys
1300: 2f 6d 6d 61 6e 2e 68 3e 0a 23 69 6e 63 6c 75 64 /mman.h>.#includ
1310: 65 20 3c 73 79 73 2f 76 6d 70 61 72 61 6d 2e 68 e <sys/vmparam.h
1320: 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 6d 61 63 68 >.#include <mach
1330: 69 6e 65 2f 6d 61 63 68 70 61 72 61 6d 2e 68 3e ine/machparam.h>
1340: 0a 0a 76 6f 69 64 0a 5f 5f 65 6e 61 62 6c 65 5f ..void.__enable_
1350: 65 78 65 63 75 74 65 5f 73 74 61 63 6b 20 28 29 execute_stack ()
1360: 0a 7b 0a 20 20 69 6e 74 20 66 70 3b 0a 20 20 73 .{. int fp;. s
1370: 74 61 74 69 63 20 75 6e 73 69 67 6e 65 64 20 6c tatic unsigned l
1380: 6f 77 65 73 74 20 3d 20 55 53 52 53 54 41 43 4b owest = USRSTACK
1390: 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 75 72 ;. unsigned cur
13a0: 72 65 6e 74 20 3d 20 28 75 6e 73 69 67 6e 65 64 rent = (unsigned
13b0: 29 20 26 66 70 20 26 20 2d 4e 42 50 47 3b 0a 0a ) &fp & -NBPG;..
13c0: 20 20 69 66 20 28 6c 6f 77 65 73 74 20 3e 20 63 if (lowest > c
13d0: 75 72 72 65 6e 74 29 0a 20 20 20 20 7b 0a 20 20 urrent). {.
13e0: 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 65 6e unsigned len
13f0: 20 3d 20 6c 6f 77 65 73 74 20 2d 20 63 75 72 72 = lowest - curr
1400: 65 6e 74 3b 0a 20 20 20 20 20 20 6d 72 65 6d 61 ent;. mrema
1410: 70 20 28 63 75 72 72 65 6e 74 2c 20 26 6c 65 6e p (current, &len
1420: 2c 20 50 52 4f 54 5f 52 45 41 44 20 7c 20 50 52 , PROT_READ | PR
1430: 4f 54 5f 57 52 49 54 45 20 7c 20 50 52 4f 54 5f OT_WRITE | PROT_
1440: 45 58 45 43 2c 20 4d 41 50 5f 50 52 49 56 41 54 EXEC, MAP_PRIVAT
1450: 45 29 3b 0a 20 20 20 20 20 20 6c 6f 77 65 73 74 E);. lowest
1460: 20 3d 20 63 75 72 72 65 6e 74 3b 0a 20 20 20 20 = current;.
1470: 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 5f }.}.#endif /* __
1480: 63 6f 6e 76 65 78 5f 5f 20 2a 2f 0a 0a 23 69 66 convex__ */..#if
1490: 64 65 66 20 5f 5f 44 4f 4c 50 48 49 4e 5f 5f 0a def __DOLPHIN__.
14a0: 0a 2f 2a 20 4d 6f 64 69 66 69 65 64 20 66 72 6f ./* Modified fro
14b0: 6d 20 74 68 65 20 63 6f 6e 76 65 78 20 2d 63 6f m the convex -co
14c0: 64 65 20 61 62 6f 76 65 2e 20 2a 2f 0a 0a 23 69 de above. */..#i
14d0: 6e 63 6c 75 64 65 20 3c 73 79 73 2f 70 61 72 61 nclude <sys/para
14e0: 6d 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 65 m.h>.#include <e
14f0: 72 72 6e 6f 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 rrno.h>.#include
1500: 20 3c 73 79 73 2f 6d 38 38 6b 62 63 73 2e 68 3e <sys/m88kbcs.h>
1510: 0a 0a 76 6f 69 64 0a 5f 5f 65 6e 61 62 6c 65 5f ..void.__enable_
1520: 65 78 65 63 75 74 65 5f 73 74 61 63 6b 20 28 29 execute_stack ()
1530: 0a 7b 0a 20 20 69 6e 74 20 73 61 76 65 5f 65 72 .{. int save_er
1540: 72 6e 6f 3b 0a 20 20 73 74 61 74 69 63 20 75 6e rno;. static un
1550: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 77 65 signed long lowe
1560: 73 74 20 3d 20 55 53 52 53 54 41 43 4b 3b 0a 20 st = USRSTACK;.
1570: 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 63 unsigned long c
1580: 75 72 72 65 6e 74 20 3d 20 28 75 6e 73 69 67 6e urrent = (unsign
1590: 65 64 20 6c 6f 6e 67 29 20 26 73 61 76 65 5f 65 ed long) &save_e
15a0: 72 72 6e 6f 20 26 20 2d 4e 42 50 43 3b 0a 20 20 rrno & -NBPC;.
15b0: 0a 20 20 2f 2a 20 49 67 6e 6f 72 65 20 65 72 72 . /* Ignore err
15c0: 6e 6f 20 62 65 69 6e 67 20 73 65 74 2e 20 6d 65 no being set. me
15d0: 6d 63 74 6c 20 73 65 74 73 20 65 72 72 6e 6f 20 mctl sets errno
15e0: 74 6f 20 45 49 4e 56 41 4c 20 77 68 65 6e 65 76 to EINVAL whenev
15f0: 65 72 20 74 68 65 0a 20 20 20 20 20 61 64 64 72 er the. addr
1600: 65 73 73 20 69 73 20 73 65 65 6e 20 61 73 20 27 ess is seen as '
1610: 6e 65 67 61 74 69 76 65 27 2e 20 54 68 61 74 20 negative'. That
1620: 69 73 20 74 68 65 20 63 61 73 65 20 77 69 74 68 is the case with
1630: 20 74 68 65 20 73 74 61 63 6b 2e 20 20 20 2a 2f the stack. */
1640: 0a 0a 20 20 73 61 76 65 5f 65 72 72 6e 6f 3d 65 .. save_errno=e
1650: 72 72 6e 6f 3b 0a 20 20 69 66 20 28 6c 6f 77 65 rrno;. if (lowe
1660: 73 74 20 3e 20 63 75 72 72 65 6e 74 29 0a 20 20 st > current).
1670: 20 20 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e {. unsign
1680: 65 64 20 6c 65 6e 3d 6c 6f 77 65 73 74 2d 63 75 ed len=lowest-cu
1690: 72 72 65 6e 74 3b 0a 20 20 20 20 20 20 6d 65 6d rrent;. mem
16a0: 63 74 6c 28 63 75 72 72 65 6e 74 2c 6c 65 6e 2c ctl(current,len,
16b0: 4d 43 54 5f 54 45 58 54 29 3b 0a 20 20 20 20 20 MCT_TEXT);.
16c0: 20 6c 6f 77 65 73 74 20 3d 20 63 75 72 72 65 6e lowest = curren
16d0: 74 3b 0a 20 20 20 20 7d 0a 20 20 65 6c 73 65 0a t;. }. else.
16e0: 20 20 20 20 6d 65 6d 63 74 6c 28 63 75 72 72 65 memctl(curre
16f0: 6e 74 2c 4e 42 50 43 2c 4d 43 54 5f 54 45 58 54 nt,NBPC,MCT_TEXT
1700: 29 3b 0a 20 20 65 72 72 6e 6f 3d 73 61 76 65 5f );. errno=save_
1710: 65 72 72 6e 6f 3b 0a 7d 0a 0a 23 65 6e 64 69 66 errno;.}..#endif
1720: 20 2f 2a 20 5f 5f 44 4f 4c 50 48 49 4e 5f 5f 20 /* __DOLPHIN__
1730: 2a 2f 0a 0a 23 69 66 64 65 66 20 5f 5f 70 79 72 */..#ifdef __pyr
1740: 5f 5f 0a 0a 23 75 6e 64 65 66 20 4e 55 4c 4c 20 __..#undef NULL
1750: 2f 2a 20 41 76 6f 69 64 20 65 72 72 6f 72 73 20 /* Avoid errors
1760: 69 66 20 73 74 64 69 6f 2e 68 20 61 6e 64 20 6f if stdio.h and o
1770: 75 72 20 73 74 64 64 65 66 2e 68 20 6d 69 73 6d ur stddef.h mism
1780: 61 74 63 68 2e 20 20 2a 2f 0a 23 69 6e 63 6c 75 atch. */.#inclu
1790: 64 65 20 3c 73 74 64 69 6f 2e 68 3e 0a 23 69 6e de <stdio.h>.#in
17a0: 63 6c 75 64 65 20 3c 73 79 73 2f 6d 6d 61 6e 2e clude <sys/mman.
17b0: 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 h>.#include <sys
17c0: 2f 74 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c 75 /types.h>.#inclu
17d0: 64 65 20 3c 73 79 73 2f 70 61 72 61 6d 2e 68 3e de <sys/param.h>
17e0: 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 76 .#include <sys/v
17f0: 6d 6d 61 63 2e 68 3e 0a 0a 2f 2a 20 4d 6f 64 69 mmac.h>../* Modi
1800: 66 69 65 64 20 66 72 6f 6d 20 74 68 65 20 63 6f fied from the co
1810: 6e 76 65 78 20 2d 63 6f 64 65 20 61 62 6f 76 65 nvex -code above
1820: 2e 0a 20 20 20 6d 72 65 6d 61 70 20 70 72 6f 6d .. mremap prom
1830: 69 73 65 73 20 74 6f 20 63 6c 65 61 72 20 74 68 ises to clear th
1840: 65 20 69 2d 63 61 63 68 65 2e 20 2a 2f 0a 0a 76 e i-cache. */..v
1850: 6f 69 64 0a 5f 5f 65 6e 61 62 6c 65 5f 65 78 65 oid.__enable_exe
1860: 63 75 74 65 5f 73 74 61 63 6b 20 28 29 0a 7b 0a cute_stack ().{.
1870: 20 20 69 6e 74 20 66 70 3b 0a 20 20 69 66 20 28 int fp;. if (
1880: 6d 70 72 6f 74 65 63 74 20 28 28 28 75 6e 73 69 mprotect (((unsi
1890: 67 6e 65 64 20 69 6e 74 29 26 66 70 2f 50 41 47 gned int)&fp/PAG
18a0: 53 49 5a 29 2a 50 41 47 53 49 5a 2c 20 50 41 47 SIZ)*PAGSIZ, PAG
18b0: 53 49 5a 2c 0a 09 09 50 52 4f 54 5f 52 45 41 44 SIZ,...PROT_READ
18c0: 7c 50 52 4f 54 5f 57 52 49 54 45 7c 50 52 4f 54 |PROT_WRITE|PROT
18d0: 5f 45 58 45 43 29 29 0a 20 20 20 20 7b 0a 20 20 _EXEC)). {.
18e0: 20 20 20 20 70 65 72 72 6f 72 20 28 22 6d 70 72 perror ("mpr
18f0: 6f 74 65 63 74 20 69 6e 20 5f 5f 65 6e 61 62 6c otect in __enabl
1900: 65 5f 65 78 65 63 75 74 65 5f 73 74 61 63 6b 22 e_execute_stack"
1910: 29 3b 0a 20 20 20 20 20 20 66 66 6c 75 73 68 20 );. fflush
1920: 28 73 74 64 65 72 72 29 3b 0a 20 20 20 20 20 20 (stderr);.
1930: 61 62 6f 72 74 20 28 29 3b 0a 20 20 20 20 7d 0a abort ();. }.
1940: 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 5f 70 79 }.#endif /* __py
1950: 72 5f 5f 20 2a 2f 0a r__ */.