OSDN Git Service

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