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