FSF GCC merge 02/23/03
[official-gcc.git] / gcc / config / rs6000 / host-darwin.c
blob5caf50d66ef7612d5efa15ef5321038a3dda5bde
1 /* Darwin/powerpc host-specific hook definitions.
2 Copyright (C) 2003 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
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 Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include <signal.h>
26 #include <sys/ucontext.h>
27 #include "hosthooks.h"
28 #include "hosthooks-def.h"
29 #include "toplev.h"
31 static void segv_crash_handler PARAMS ((int));
32 static void segv_handler PARAMS ((int, siginfo_t *, void *));
33 static void darwin_rs6000_extra_signals PARAMS ((void));
35 /* No prototype for this, filed as Radar 3150910. */
36 extern int sigaltstack(const stack_t *, stack_t *);
38 #undef HOST_HOOKS_EXTRA_SIGNALS
39 #define HOST_HOOKS_EXTRA_SIGNALS darwin_rs6000_extra_signals
41 /* On Darwin/powerpc, hitting the stack limit turns into a SIGSEGV.
42 This code detects the difference between hitting the stack limit and
43 a true wild pointer dereference by looking at the instruction that
44 faulted; only a few kinds of instruction are used to access below
45 the previous bottom of the stack. */
47 static void
48 segv_crash_handler (sig)
49 int sig ATTRIBUTE_UNUSED;
51 internal_error ("Segmentation Fault (code)");
54 static void
55 segv_handler (sig, sip, scp)
56 int sig ATTRIBUTE_UNUSED;
57 siginfo_t *sip ATTRIBUTE_UNUSED;
58 void *scp;
60 ucontext_t *uc = (ucontext_t *)scp;
61 unsigned faulting_insn;
63 /* The fault might have happened when trying to run some instruction, in
64 which case the next line will segfault _again_. Handle this case. */
65 signal (SIGSEGV, segv_crash_handler);
67 faulting_insn = *(unsigned *)uc->uc_mcontext->ss.srr0;
69 /* Note that this only has to work for GCC, so we don't have to deal
70 with all the possible cases (GCC has no AltiVec code, for
71 instance). It's complicated because Darwin allows stores to
72 below the stack pointer, and the prologue code takes advantage of
73 this. */
75 if ((faulting_insn & 0xFFFF8000) == 0x94218000 /* stwu %r1, -xxx(%r1) */
76 || (faulting_insn & 0xFFFF03FF) == 0x7C21016E /* stwux %r1, xxx, %r1 */
77 || (faulting_insn & 0xFC1F8000) == 0x90018000 /* stw xxx, -yyy(%r1) */
78 || (faulting_insn & 0xFC1F8000) == 0xD8018000 /* stfd xxx, -yyy(%r1) */
79 || (faulting_insn & 0xFC1F8000) == 0xBC018000 /* stmw xxx, -yyy(%r1) */)
81 char *shell_name;
83 fnotice (stderr, "Out of stack space.\n");
84 shell_name = getenv ("SHELL");
85 if (shell_name != NULL)
86 shell_name = strrchr (shell_name, '/');
87 if (shell_name != NULL)
89 static const char * shell_commands[][2] = {
90 { "sh", "ulimit -S -s unlimited" },
91 { "bash", "ulimit -S -s unlimited" },
92 { "tcsh", "limit stacksize unlimited" },
93 { "csh", "limit stacksize unlimited" },
94 /* zsh doesn't have "unlimited", this will work under the
95 default configuration. */
96 { "zsh", "limit stacksize 32m" }
98 size_t i;
100 for (i = 0; i < ARRAY_SIZE (shell_commands); i++)
101 if (strcmp (shell_commands[i][0], shell_name + 1) == 0)
103 fnotice (stderr,
104 "Try running `%s' in the shell to raise its limit.\n",
105 shell_commands[i][1]);
109 exit (FATAL_EXIT_CODE);
112 fprintf (stderr, "[address=%08lx pc=%08x]\n",
113 uc->uc_mcontext->es.dar, uc->uc_mcontext->ss.srr0);
114 internal_error ("Segmentation Fault");
115 exit (FATAL_EXIT_CODE);
118 static void
119 darwin_rs6000_extra_signals ()
121 struct sigaction sact;
122 stack_t sigstk;
124 sigstk.ss_sp = xmalloc (SIGSTKSZ);
125 sigstk.ss_size = SIGSTKSZ;
126 sigstk.ss_flags = 0;
127 if (sigaltstack (&sigstk, NULL) < 0)
128 fatal_io_error ("While setting up signal stack");
130 sigemptyset(&sact.sa_mask);
131 sact.sa_flags = SA_ONSTACK | SA_SIGINFO;
132 sact.sa_sigaction = segv_handler;
133 if (sigaction (SIGSEGV, &sact, 0) < 0)
134 fatal_io_error ("While setting up signal handler");
137 const struct host_hooks host_hooks = HOST_HOOKS_INITIALIZER;