]>
Commit | Line | Data |
---|---|---|
476d9098 | 1 | /* Darwin/powerpc host-specific hook definitions. |
ed23bd30 | 2 | Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc. |
476d9098 | 3 | |
5de601cf NC |
4 | This file is part of GCC. |
5 | ||
6 | GCC is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU General Public License as published | |
8 | by the Free Software Foundation; either version 2, or (at your | |
9 | option) any later version. | |
10 | ||
11 | GCC is distributed in the hope that it will be useful, but WITHOUT | |
12 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
13 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public | |
14 | License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GCC; see the file COPYING. If not, write to the | |
39d14dda KC |
18 | Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, |
19 | MA 02110-1301, USA. */ | |
476d9098 GK |
20 | |
21 | #include "config.h" | |
22 | #include "system.h" | |
23 | #include "coretypes.h" | |
24 | #include <signal.h> | |
25 | #include <sys/ucontext.h> | |
26 | #include "hosthooks.h" | |
27 | #include "hosthooks-def.h" | |
28 | #include "toplev.h" | |
505b0fd6 | 29 | #include "diagnostic.h" |
ed23bd30 | 30 | #include "config/host-darwin.h" |
476d9098 | 31 | |
5f5bfdd0 GK |
32 | static void segv_crash_handler (int); |
33 | static void segv_handler (int, siginfo_t *, void *); | |
34 | static void darwin_rs6000_extra_signals (void); | |
476d9098 | 35 | |
5f5bfdd0 GK |
36 | /* This doesn't have a prototype in signal.h in 10.2.x and earlier, |
37 | fixed in later releases. */ | |
38 | extern int sigaltstack(const struct sigaltstack *, struct sigaltstack *); | |
476d9098 GK |
39 | |
40 | #undef HOST_HOOKS_EXTRA_SIGNALS | |
41 | #define HOST_HOOKS_EXTRA_SIGNALS darwin_rs6000_extra_signals | |
42 | ||
a693fbb9 GK |
43 | /* On Darwin/powerpc, hitting the stack limit turns into a SIGSEGV. |
44 | This code detects the difference between hitting the stack limit and | |
45 | a true wild pointer dereference by looking at the instruction that | |
46 | faulted; only a few kinds of instruction are used to access below | |
47 | the previous bottom of the stack. */ | |
476d9098 GK |
48 | |
49 | static void | |
5f5bfdd0 | 50 | segv_crash_handler (int sig ATTRIBUTE_UNUSED) |
476d9098 GK |
51 | { |
52 | internal_error ("Segmentation Fault (code)"); | |
53 | } | |
54 | ||
55 | static void | |
5f5bfdd0 GK |
56 | segv_handler (int sig ATTRIBUTE_UNUSED, |
57 | siginfo_t *sip ATTRIBUTE_UNUSED, | |
58 | void *scp) | |
476d9098 GK |
59 | { |
60 | ucontext_t *uc = (ucontext_t *)scp; | |
68e58c33 | 61 | sigset_t sigset; |
476d9098 GK |
62 | unsigned faulting_insn; |
63 | ||
64 | /* The fault might have happened when trying to run some instruction, in | |
65 | which case the next line will segfault _again_. Handle this case. */ | |
66 | signal (SIGSEGV, segv_crash_handler); | |
68e58c33 GK |
67 | sigemptyset (&sigset); |
68 | sigaddset (&sigset, SIGSEGV); | |
69 | sigprocmask (SIG_UNBLOCK, &sigset, NULL); | |
476d9098 GK |
70 | |
71 | faulting_insn = *(unsigned *)uc->uc_mcontext->ss.srr0; | |
72 | ||
73 | /* Note that this only has to work for GCC, so we don't have to deal | |
74 | with all the possible cases (GCC has no AltiVec code, for | |
75 | instance). It's complicated because Darwin allows stores to | |
76 | below the stack pointer, and the prologue code takes advantage of | |
77 | this. */ | |
78 | ||
79 | if ((faulting_insn & 0xFFFF8000) == 0x94218000 /* stwu %r1, -xxx(%r1) */ | |
f8ed6473 | 80 | || (faulting_insn & 0xFC1F03FF) == 0x7C01016E /* stwux xxx, %r1, xxx */ |
476d9098 GK |
81 | || (faulting_insn & 0xFC1F8000) == 0x90018000 /* stw xxx, -yyy(%r1) */ |
82 | || (faulting_insn & 0xFC1F8000) == 0xD8018000 /* stfd xxx, -yyy(%r1) */ | |
83 | || (faulting_insn & 0xFC1F8000) == 0xBC018000 /* stmw xxx, -yyy(%r1) */) | |
84 | { | |
85 | char *shell_name; | |
86 | ||
87 | fnotice (stderr, "Out of stack space.\n"); | |
88 | shell_name = getenv ("SHELL"); | |
89 | if (shell_name != NULL) | |
90 | shell_name = strrchr (shell_name, '/'); | |
91 | if (shell_name != NULL) | |
92 | { | |
93 | static const char * shell_commands[][2] = { | |
94 | { "sh", "ulimit -S -s unlimited" }, | |
95 | { "bash", "ulimit -S -s unlimited" }, | |
96 | { "tcsh", "limit stacksize unlimited" }, | |
97 | { "csh", "limit stacksize unlimited" }, | |
98 | /* zsh doesn't have "unlimited", this will work under the | |
99 | default configuration. */ | |
100 | { "zsh", "limit stacksize 32m" } | |
101 | }; | |
102 | size_t i; | |
103 | ||
104 | for (i = 0; i < ARRAY_SIZE (shell_commands); i++) | |
105 | if (strcmp (shell_commands[i][0], shell_name + 1) == 0) | |
106 | { | |
107 | fnotice (stderr, | |
9e637a26 | 108 | "Try running '%s' in the shell to raise its limit.\n", |
476d9098 GK |
109 | shell_commands[i][1]); |
110 | } | |
111 | } | |
112 | ||
505b0fd6 | 113 | if (global_dc->abort_on_error) |
992d08b1 | 114 | fancy_abort (__FILE__, __LINE__, __FUNCTION__); |
505b0fd6 | 115 | |
476d9098 GK |
116 | exit (FATAL_EXIT_CODE); |
117 | } | |
118 | ||
119 | fprintf (stderr, "[address=%08lx pc=%08x]\n", | |
120 | uc->uc_mcontext->es.dar, uc->uc_mcontext->ss.srr0); | |
121 | internal_error ("Segmentation Fault"); | |
122 | exit (FATAL_EXIT_CODE); | |
123 | } | |
124 | ||
125 | static void | |
5f5bfdd0 | 126 | darwin_rs6000_extra_signals (void) |
476d9098 GK |
127 | { |
128 | struct sigaction sact; | |
129 | stack_t sigstk; | |
130 | ||
131 | sigstk.ss_sp = xmalloc (SIGSTKSZ); | |
132 | sigstk.ss_size = SIGSTKSZ; | |
133 | sigstk.ss_flags = 0; | |
134 | if (sigaltstack (&sigstk, NULL) < 0) | |
fa6ef813 | 135 | fatal_error ("While setting up signal stack: %m"); |
476d9098 GK |
136 | |
137 | sigemptyset(&sact.sa_mask); | |
138 | sact.sa_flags = SA_ONSTACK | SA_SIGINFO; | |
139 | sact.sa_sigaction = segv_handler; | |
140 | if (sigaction (SIGSEGV, &sact, 0) < 0) | |
fa6ef813 | 141 | fatal_error ("While setting up signal handler: %m"); |
476d9098 | 142 | } |
18c81520 | 143 | \f |
476d9098 GK |
144 | |
145 | const struct host_hooks host_hooks = HOST_HOOKS_INITIALIZER; |