host-darwin.c 4.9 KB

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