1 /* Darwin/powerpc host-specific hook definitions.
2 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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
18 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
19 MA 02110-1301, USA. */
23 #include "coretypes.h"
25 #include <sys/ucontext.h>
26 #include "hosthooks.h"
27 #include "hosthooks-def.h"
29 #include "diagnostic.h"
30 #include "config/host-darwin.h"
32 static void segv_crash_handler (int);
33 static void segv_handler (int, siginfo_t
*, void *);
34 static void darwin_rs6000_extra_signals (void);
36 #undef HOST_HOOKS_EXTRA_SIGNALS
37 #define HOST_HOOKS_EXTRA_SIGNALS darwin_rs6000_extra_signals
39 /* On Darwin/powerpc, hitting the stack limit turns into a SIGSEGV.
40 This code detects the difference between hitting the stack limit and
41 a true wild pointer dereference by looking at the instruction that
42 faulted; only a few kinds of instruction are used to access below
43 the previous bottom of the stack. */
46 segv_crash_handler (int sig ATTRIBUTE_UNUSED
)
48 internal_error ("Segmentation Fault (code)");
52 segv_handler (int sig ATTRIBUTE_UNUSED
,
53 siginfo_t
*sip ATTRIBUTE_UNUSED
,
56 ucontext_t
*uc
= (ucontext_t
*)scp
;
58 unsigned faulting_insn
;
60 /* The fault might have happened when trying to run some instruction, in
61 which case the next line will segfault _again_. Handle this case. */
62 signal (SIGSEGV
, segv_crash_handler
);
63 sigemptyset (&sigset
);
64 sigaddset (&sigset
, SIGSEGV
);
65 sigprocmask (SIG_UNBLOCK
, &sigset
, NULL
);
68 faulting_insn
= *(unsigned *)uc
->uc_mcontext
->__ss
.__srr0
;
70 faulting_insn
= *(unsigned *)uc
->uc_mcontext
->ss
.srr0
;
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
79 if ((faulting_insn
& 0xFFFF8000) == 0x94218000 /* stwu %r1, -xxx(%r1) */
80 || (faulting_insn
& 0xFC1F03FF) == 0x7C01016E /* stwux xxx, %r1, xxx */
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) */)
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
)
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" }
104 for (i
= 0; i
< ARRAY_SIZE (shell_commands
); i
++)
105 if (strcmp (shell_commands
[i
][0], shell_name
+ 1) == 0)
108 "Try running '%s' in the shell to raise its limit.\n",
109 shell_commands
[i
][1]);
113 if (global_dc
->abort_on_error
)
114 fancy_abort (__FILE__
, __LINE__
, __FUNCTION__
);
116 exit (FATAL_EXIT_CODE
);
120 fprintf (stderr
, "[address=%08lx pc=%08x]\n",
121 uc
->uc_mcontext
->__es
.__dar
, uc
->uc_mcontext
->__ss
.__srr0
);
123 fprintf (stderr
, "[address=%08lx pc=%08x]\n",
124 uc
->uc_mcontext
->es
.dar
, uc
->uc_mcontext
->ss
.srr0
);
126 internal_error ("Segmentation Fault");
127 exit (FATAL_EXIT_CODE
);
131 darwin_rs6000_extra_signals (void)
133 struct sigaction sact
;
136 sigstk
.ss_sp
= xmalloc (SIGSTKSZ
);
137 sigstk
.ss_size
= SIGSTKSZ
;
139 if (sigaltstack (&sigstk
, NULL
) < 0)
140 fatal_error ("While setting up signal stack: %m");
142 sigemptyset(&sact
.sa_mask
);
143 sact
.sa_flags
= SA_ONSTACK
| SA_SIGINFO
;
144 sact
.sa_sigaction
= segv_handler
;
145 if (sigaction (SIGSEGV
, &sact
, 0) < 0)
146 fatal_error ("While setting up signal handler: %m");
150 const struct host_hooks host_hooks
= HOST_HOOKS_INITIALIZER
;