OSDN Git Service

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