linux-unwind.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* DWARF2 EH unwinding support for AMD x86-64 and x86.
  2. Copyright (C) 2004-2022 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. /* Unwind shadow stack for -fcf-protection -mshstk. */
  20. #if defined __SHSTK__ && defined __CET__ && (__CET__ & 2) != 0
  21. # include "config/i386/shadow-stack-unwind.h"
  22. #endif
  23. /* Do code reading to identify a signal frame, and set the frame
  24. state data appropriately. See unwind-dw2.c for the structs.
  25. Don't use this at all if inhibit_libc is used. */
  26. #ifndef inhibit_libc
  27. /* There's no sys/ucontext.h for glibc 2.0, so no
  28. signal-turned-exceptions for them. There's also no configure-run for
  29. the target, so we can't check on (e.g.) HAVE_SYS_UCONTEXT_H. Using the
  30. target libc version macro should be enough. */
  31. #if defined __GLIBC__ && !(__GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
  32. #include <signal.h>
  33. #include <sys/ucontext.h>
  34. #ifdef __x86_64__
  35. #define MD_FALLBACK_FRAME_STATE_FOR x86_64_fallback_frame_state
  36. static _Unwind_Reason_Code
  37. x86_64_fallback_frame_state (struct _Unwind_Context *context,
  38. _Unwind_FrameState *fs)
  39. {
  40. unsigned char *pc = context->ra;
  41. struct sigcontext *sc;
  42. long new_cfa;
  43. /* movq $__NR_rt_sigreturn, %rax ; syscall. */
  44. #ifdef __LP64__
  45. #define RT_SIGRETURN_SYSCALL 0x050f0000000fc0c7ULL
  46. #else
  47. #define RT_SIGRETURN_SYSCALL 0x050f40000201c0c7ULL
  48. #endif
  49. if (*(unsigned char *)(pc+0) == 0x48
  50. && *(unsigned long long *)(pc+1) == RT_SIGRETURN_SYSCALL)
  51. {
  52. ucontext_t *uc_ = context->cfa;
  53. /* The void * cast is necessary to avoid an aliasing warning.
  54. The aliasing warning is correct, but should not be a problem
  55. because it does not alias anything. */
  56. sc = (struct sigcontext *) (void *) &uc_->uc_mcontext;
  57. }
  58. else
  59. return _URC_END_OF_STACK;
  60. new_cfa = sc->rsp;
  61. fs->regs.cfa_how = CFA_REG_OFFSET;
  62. /* Register 7 is rsp */
  63. fs->regs.cfa_reg = 7;
  64. fs->regs.cfa_offset = new_cfa - (long) context->cfa;
  65. /* The SVR4 register numbering macros aren't usable in libgcc. */
  66. fs->regs.reg[0].how = REG_SAVED_OFFSET;
  67. fs->regs.reg[0].loc.offset = (long)&sc->rax - new_cfa;
  68. fs->regs.reg[1].how = REG_SAVED_OFFSET;
  69. fs->regs.reg[1].loc.offset = (long)&sc->rdx - new_cfa;
  70. fs->regs.reg[2].how = REG_SAVED_OFFSET;
  71. fs->regs.reg[2].loc.offset = (long)&sc->rcx - new_cfa;
  72. fs->regs.reg[3].how = REG_SAVED_OFFSET;
  73. fs->regs.reg[3].loc.offset = (long)&sc->rbx - new_cfa;
  74. fs->regs.reg[4].how = REG_SAVED_OFFSET;
  75. fs->regs.reg[4].loc.offset = (long)&sc->rsi - new_cfa;
  76. fs->regs.reg[5].how = REG_SAVED_OFFSET;
  77. fs->regs.reg[5].loc.offset = (long)&sc->rdi - new_cfa;
  78. fs->regs.reg[6].how = REG_SAVED_OFFSET;
  79. fs->regs.reg[6].loc.offset = (long)&sc->rbp - new_cfa;
  80. fs->regs.reg[8].how = REG_SAVED_OFFSET;
  81. fs->regs.reg[8].loc.offset = (long)&sc->r8 - new_cfa;
  82. fs->regs.reg[9].how = REG_SAVED_OFFSET;
  83. fs->regs.reg[9].loc.offset = (long)&sc->r9 - new_cfa;
  84. fs->regs.reg[10].how = REG_SAVED_OFFSET;
  85. fs->regs.reg[10].loc.offset = (long)&sc->r10 - new_cfa;
  86. fs->regs.reg[11].how = REG_SAVED_OFFSET;
  87. fs->regs.reg[11].loc.offset = (long)&sc->r11 - new_cfa;
  88. fs->regs.reg[12].how = REG_SAVED_OFFSET;
  89. fs->regs.reg[12].loc.offset = (long)&sc->r12 - new_cfa;
  90. fs->regs.reg[13].how = REG_SAVED_OFFSET;
  91. fs->regs.reg[13].loc.offset = (long)&sc->r13 - new_cfa;
  92. fs->regs.reg[14].how = REG_SAVED_OFFSET;
  93. fs->regs.reg[14].loc.offset = (long)&sc->r14 - new_cfa;
  94. fs->regs.reg[15].how = REG_SAVED_OFFSET;
  95. fs->regs.reg[15].loc.offset = (long)&sc->r15 - new_cfa;
  96. fs->regs.reg[16].how = REG_SAVED_OFFSET;
  97. fs->regs.reg[16].loc.offset = (long)&sc->rip - new_cfa;
  98. fs->retaddr_column = 16;
  99. fs->signal_frame = 1;
  100. return _URC_NO_REASON;
  101. }
  102. #else /* ifdef __x86_64__ */
  103. #define MD_FALLBACK_FRAME_STATE_FOR x86_fallback_frame_state
  104. static _Unwind_Reason_Code
  105. x86_fallback_frame_state (struct _Unwind_Context *context,
  106. _Unwind_FrameState *fs)
  107. {
  108. unsigned char *pc = context->ra;
  109. struct sigcontext *sc;
  110. long new_cfa;
  111. /* popl %eax ; movl $__NR_sigreturn,%eax ; int $0x80 */
  112. if (*(unsigned short *)(pc+0) == 0xb858
  113. && *(unsigned int *)(pc+2) == 119
  114. && *(unsigned short *)(pc+6) == 0x80cd)
  115. sc = context->cfa + 4;
  116. /* movl $__NR_rt_sigreturn,%eax ; int $0x80 */
  117. else if (*(unsigned char *)(pc+0) == 0xb8
  118. && *(unsigned int *)(pc+1) == 173
  119. && *(unsigned short *)(pc+5) == 0x80cd)
  120. {
  121. struct rt_sigframe {
  122. int sig;
  123. siginfo_t *pinfo;
  124. void *puc;
  125. siginfo_t info;
  126. ucontext_t uc;
  127. } *rt_ = context->cfa;
  128. /* The void * cast is necessary to avoid an aliasing warning.
  129. The aliasing warning is correct, but should not be a problem
  130. because it does not alias anything. */
  131. sc = (struct sigcontext *) (void *) &rt_->uc.uc_mcontext;
  132. }
  133. else
  134. return _URC_END_OF_STACK;
  135. new_cfa = sc->esp;
  136. fs->regs.cfa_how = CFA_REG_OFFSET;
  137. fs->regs.cfa_reg = 4;
  138. fs->regs.cfa_offset = new_cfa - (long) context->cfa;
  139. /* The SVR4 register numbering macros aren't usable in libgcc. */
  140. fs->regs.reg[0].how = REG_SAVED_OFFSET;
  141. fs->regs.reg[0].loc.offset = (long)&sc->eax - new_cfa;
  142. fs->regs.reg[3].how = REG_SAVED_OFFSET;
  143. fs->regs.reg[3].loc.offset = (long)&sc->ebx - new_cfa;
  144. fs->regs.reg[1].how = REG_SAVED_OFFSET;
  145. fs->regs.reg[1].loc.offset = (long)&sc->ecx - new_cfa;
  146. fs->regs.reg[2].how = REG_SAVED_OFFSET;
  147. fs->regs.reg[2].loc.offset = (long)&sc->edx - new_cfa;
  148. fs->regs.reg[6].how = REG_SAVED_OFFSET;
  149. fs->regs.reg[6].loc.offset = (long)&sc->esi - new_cfa;
  150. fs->regs.reg[7].how = REG_SAVED_OFFSET;
  151. fs->regs.reg[7].loc.offset = (long)&sc->edi - new_cfa;
  152. fs->regs.reg[5].how = REG_SAVED_OFFSET;
  153. fs->regs.reg[5].loc.offset = (long)&sc->ebp - new_cfa;
  154. fs->regs.reg[8].how = REG_SAVED_OFFSET;
  155. fs->regs.reg[8].loc.offset = (long)&sc->eip - new_cfa;
  156. fs->retaddr_column = 8;
  157. fs->signal_frame = 1;
  158. return _URC_NO_REASON;
  159. }
  160. #define MD_FROB_UPDATE_CONTEXT x86_frob_update_context
  161. /* Fix up for kernels that have vDSO, but don't have S flag in it. */
  162. static void
  163. x86_frob_update_context (struct _Unwind_Context *context,
  164. _Unwind_FrameState *fs ATTRIBUTE_UNUSED)
  165. {
  166. unsigned char *pc = context->ra;
  167. /* movl $__NR_rt_sigreturn,%eax ; {int $0x80 | syscall} */
  168. if (*(unsigned char *)(pc+0) == 0xb8
  169. && *(unsigned int *)(pc+1) == 173
  170. && (*(unsigned short *)(pc+5) == 0x80cd
  171. || *(unsigned short *)(pc+5) == 0x050f))
  172. _Unwind_SetSignalFrame (context, 1);
  173. }
  174. #endif /* ifdef __x86_64__ */
  175. #endif /* not glibc 2.0 */
  176. #endif /* ifdef inhibit_libc */