| 1 | /* POSIX.1 `sigaction' call for Linux/x86-64. |
| 2 | Copyright (C) 2001-2016 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <sysdep.h> |
| 20 | #include <errno.h> |
| 21 | #include <stddef.h> |
| 22 | #include <signal.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | #include <sysdep.h> |
| 26 | #include <sys/syscall.h> |
| 27 | |
| 28 | /* The difference here is that the sigaction structure used in the |
| 29 | kernel is not the same as we use in the libc. Therefore we must |
| 30 | translate it here. */ |
| 31 | #include <kernel_sigaction.h> |
| 32 | |
| 33 | #include "ucontext_i.h" |
| 34 | |
| 35 | /* We do not globally define the SA_RESTORER flag so do it here. */ |
| 36 | #define SA_RESTORER 0x04000000 |
| 37 | |
| 38 | /* Using the hidden attribute here does not change the code but it |
| 39 | helps to avoid warnings. */ |
| 40 | extern void restore_rt (void) asm ("__restore_rt" ) attribute_hidden; |
| 41 | |
| 42 | |
| 43 | /* If ACT is not NULL, change the action for SIG to *ACT. |
| 44 | If OACT is not NULL, put the old action for SIG in *OACT. */ |
| 45 | int |
| 46 | __libc_sigaction (int sig, const struct sigaction *act, struct sigaction *oact) |
| 47 | { |
| 48 | int result; |
| 49 | struct kernel_sigaction kact, koact; |
| 50 | |
| 51 | if (act) |
| 52 | { |
| 53 | kact.k_sa_handler = act->sa_handler; |
| 54 | memcpy (&kact.sa_mask, &act->sa_mask, sizeof (sigset_t)); |
| 55 | kact.sa_flags = act->sa_flags | SA_RESTORER; |
| 56 | |
| 57 | kact.sa_restorer = &restore_rt; |
| 58 | } |
| 59 | |
| 60 | /* XXX The size argument hopefully will have to be changed to the |
| 61 | real size of the user-level sigset_t. */ |
| 62 | result = INLINE_SYSCALL (rt_sigaction, 4, |
| 63 | sig, act ? &kact : NULL, |
| 64 | oact ? &koact : NULL, _NSIG / 8); |
| 65 | if (oact && result >= 0) |
| 66 | { |
| 67 | oact->sa_handler = koact.k_sa_handler; |
| 68 | memcpy (&oact->sa_mask, &koact.sa_mask, sizeof (sigset_t)); |
| 69 | oact->sa_flags = koact.sa_flags; |
| 70 | oact->sa_restorer = koact.sa_restorer; |
| 71 | } |
| 72 | return result; |
| 73 | } |
| 74 | libc_hidden_def (__libc_sigaction) |
| 75 | |
| 76 | #include <nptl/sigaction.c> |
| 77 | |
| 78 | |
| 79 | /* NOTE: Please think twice before making any changes to the bits of |
| 80 | code below. GDB needs some intimate knowledge about it to |
| 81 | recognize them as signal trampolines, and make backtraces through |
| 82 | signal handlers work right. Important are both the names |
| 83 | (__restore_rt) and the exact instruction sequence. |
| 84 | If you ever feel the need to make any changes, please notify the |
| 85 | appropriate GDB maintainer. |
| 86 | |
| 87 | The unwind information starts a byte before __restore_rt, so that |
| 88 | it is found when unwinding, to get an address the unwinder assumes |
| 89 | will be in the middle of a call instruction. See the Linux kernel |
| 90 | (the i386 vsyscall, in particular) for an explanation of the complex |
| 91 | unwind information used here in order to get the traditional CFA. |
| 92 | We do not restore cs - it's only stored as two bytes here so that's |
| 93 | a bit tricky. We don't use the gas cfi directives, so that we can |
| 94 | reliably add .cfi_signal_frame. */ |
| 95 | |
| 96 | #define do_cfa_expr \ |
| 97 | " .byte 0x0f\n" /* DW_CFA_def_cfa_expression */ \ |
| 98 | " .uleb128 2f-1f\n" /* length */ \ |
| 99 | "1: .byte 0x77\n" /* DW_OP_breg7 */ \ |
| 100 | " .sleb128 " CFI_STRINGIFY (oRSP) "\n" \ |
| 101 | " .byte 0x06\n" /* DW_OP_deref */ \ |
| 102 | "2:" |
| 103 | |
| 104 | #define do_expr(regno, offset) \ |
| 105 | " .byte 0x10\n" /* DW_CFA_expression */ \ |
| 106 | " .uleb128 " CFI_STRINGIFY (regno) "\n" \ |
| 107 | " .uleb128 2f-1f\n" /* length */ \ |
| 108 | "1: .byte 0x77\n" /* DW_OP_breg7 */ \ |
| 109 | " .sleb128 " CFI_STRINGIFY (offset) "\n" \ |
| 110 | "2:" |
| 111 | |
| 112 | #define RESTORE(name, syscall) RESTORE2 (name, syscall) |
| 113 | # define RESTORE2(name, syscall) \ |
| 114 | asm \ |
| 115 | ( \ |
| 116 | /* `nop' for debuggers assuming `call' should not disalign the code. */ \ |
| 117 | " nop\n" \ |
| 118 | ".align 16\n" \ |
| 119 | ".LSTART_" #name ":\n" \ |
| 120 | " .type __" #name ",@function\n" \ |
| 121 | "__" #name ":\n" \ |
| 122 | " movq $" #syscall ", %rax\n" \ |
| 123 | " syscall\n" \ |
| 124 | ".LEND_" #name ":\n" \ |
| 125 | ".section .eh_frame,\"a\",@progbits\n" \ |
| 126 | ".LSTARTFRAME_" #name ":\n" \ |
| 127 | " .long .LENDCIE_" #name "-.LSTARTCIE_" #name "\n" \ |
| 128 | ".LSTARTCIE_" #name ":\n" \ |
| 129 | " .long 0\n" /* CIE ID */ \ |
| 130 | " .byte 1\n" /* Version number */ \ |
| 131 | " .string \"zRS\"\n" /* NUL-terminated augmentation string */ \ |
| 132 | " .uleb128 1\n" /* Code alignment factor */ \ |
| 133 | " .sleb128 -8\n" /* Data alignment factor */ \ |
| 134 | " .uleb128 16\n" /* Return address register column (rip) */ \ |
| 135 | /* Augmentation value length */ \ |
| 136 | " .uleb128 .LENDAUGMNT_" #name "-.LSTARTAUGMNT_" #name "\n" \ |
| 137 | ".LSTARTAUGMNT_" #name ":\n" \ |
| 138 | " .byte 0x1b\n" /* DW_EH_PE_pcrel|DW_EH_PE_sdata4. */ \ |
| 139 | ".LENDAUGMNT_" #name ":\n" \ |
| 140 | " .align " LP_SIZE "\n" \ |
| 141 | ".LENDCIE_" #name ":\n" \ |
| 142 | " .long .LENDFDE_" #name "-.LSTARTFDE_" #name "\n" /* FDE len */ \ |
| 143 | ".LSTARTFDE_" #name ":\n" \ |
| 144 | " .long .LSTARTFDE_" #name "-.LSTARTFRAME_" #name "\n" /* CIE */ \ |
| 145 | /* `LSTART_' is subtracted 1 as debuggers assume a `call' here. */ \ |
| 146 | " .long (.LSTART_" #name "-1)-.\n" /* PC-relative start addr. */ \ |
| 147 | " .long .LEND_" #name "-(.LSTART_" #name "-1)\n" \ |
| 148 | " .uleb128 0\n" /* FDE augmentation length */ \ |
| 149 | do_cfa_expr \ |
| 150 | do_expr (8 /* r8 */, oR8) \ |
| 151 | do_expr (9 /* r9 */, oR9) \ |
| 152 | do_expr (10 /* r10 */, oR10) \ |
| 153 | do_expr (11 /* r11 */, oR11) \ |
| 154 | do_expr (12 /* r12 */, oR12) \ |
| 155 | do_expr (13 /* r13 */, oR13) \ |
| 156 | do_expr (14 /* r14 */, oR14) \ |
| 157 | do_expr (15 /* r15 */, oR15) \ |
| 158 | do_expr (5 /* rdi */, oRDI) \ |
| 159 | do_expr (4 /* rsi */, oRSI) \ |
| 160 | do_expr (6 /* rbp */, oRBP) \ |
| 161 | do_expr (3 /* rbx */, oRBX) \ |
| 162 | do_expr (1 /* rdx */, oRDX) \ |
| 163 | do_expr (0 /* rax */, oRAX) \ |
| 164 | do_expr (2 /* rcx */, oRCX) \ |
| 165 | do_expr (7 /* rsp */, oRSP) \ |
| 166 | do_expr (16 /* rip */, oRIP) \ |
| 167 | /* libgcc-4.1.1 has only `DWARF_FRAME_REGISTERS == 17'. */ \ |
| 168 | /* do_expr (49 |* rflags *|, oEFL) */ \ |
| 169 | /* `cs'/`ds'/`fs' are unaligned and a different size. */ \ |
| 170 | /* gas: Error: register save offset not a multiple of 8 */ \ |
| 171 | " .align " LP_SIZE "\n" \ |
| 172 | ".LENDFDE_" #name ":\n" \ |
| 173 | " .previous\n" \ |
| 174 | ); |
| 175 | /* The return code for realtime-signals. */ |
| 176 | RESTORE (restore_rt, __NR_rt_sigreturn) |
| 177 | |