Remove stray includes of kernel-features.h.
[glibc.git] / sysdeps / unix / sysv / linux / x86_64 / sigaction.c
blob6735b70c1001ca6c40ccee833d16d4c1d43ced65
1 /* POSIX.1 `sigaction' call for Linux/x86-64.
2 Copyright (C) 2001-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <sysdep.h>
20 #include <errno.h>
21 #include <stddef.h>
22 #include <signal.h>
23 #include <string.h>
25 #include <sysdep.h>
26 #include <sys/syscall.h>
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>
33 #include "ucontext_i.h"
35 /* We do not globally define the SA_RESTORER flag so do it here. */
36 #define SA_RESTORER 0x04000000
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;
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)
48 int result;
49 struct kernel_sigaction kact, koact;
51 if (act)
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;
57 kact.sa_restorer = &restore_rt;
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)
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;
72 return result;
74 libc_hidden_def (__libc_sigaction)
76 #ifdef WRAPPER_INCLUDE
77 # include WRAPPER_INCLUDE
78 #endif
80 #ifndef LIBC_SIGACTION
81 weak_alias (__libc_sigaction, __sigaction)
82 libc_hidden_weak (__sigaction)
83 weak_alias (__libc_sigaction, sigaction)
84 #endif
86 /* NOTE: Please think twice before making any changes to the bits of
87 code below. GDB needs some intimate knowledge about it to
88 recognize them as signal trampolines, and make backtraces through
89 signal handlers work right. Important are both the names
90 (__restore_rt) and the exact instruction sequence.
91 If you ever feel the need to make any changes, please notify the
92 appropriate GDB maintainer.
94 The unwind information starts a byte before __restore_rt, so that
95 it is found when unwinding, to get an address the unwinder assumes
96 will be in the middle of a call instruction. See the Linux kernel
97 (the i386 vsyscall, in particular) for an explanation of the complex
98 unwind information used here in order to get the traditional CFA.
99 We do not restore cs - it's only stored as two bytes here so that's
100 a bit tricky. We don't use the gas cfi directives, so that we can
101 reliably add .cfi_signal_frame. */
103 #define do_cfa_expr \
104 " .byte 0x0f\n" /* DW_CFA_def_cfa_expression */ \
105 " .uleb128 2f-1f\n" /* length */ \
106 "1: .byte 0x77\n" /* DW_OP_breg7 */ \
107 " .sleb128 " CFI_STRINGIFY (oRSP) "\n" \
108 " .byte 0x06\n" /* DW_OP_deref */ \
109 "2:"
111 #define do_expr(regno, offset) \
112 " .byte 0x10\n" /* DW_CFA_expression */ \
113 " .uleb128 " CFI_STRINGIFY (regno) "\n" \
114 " .uleb128 2f-1f\n" /* length */ \
115 "1: .byte 0x77\n" /* DW_OP_breg7 */ \
116 " .sleb128 " CFI_STRINGIFY (offset) "\n" \
117 "2:"
119 #define RESTORE(name, syscall) RESTORE2 (name, syscall)
120 # define RESTORE2(name, syscall) \
121 asm \
123 /* `nop' for debuggers assuming `call' should not disalign the code. */ \
124 " nop\n" \
125 ".align 16\n" \
126 ".LSTART_" #name ":\n" \
127 " .type __" #name ",@function\n" \
128 "__" #name ":\n" \
129 " movq $" #syscall ", %rax\n" \
130 " syscall\n" \
131 ".LEND_" #name ":\n" \
132 ".section .eh_frame,\"a\",@progbits\n" \
133 ".LSTARTFRAME_" #name ":\n" \
134 " .long .LENDCIE_" #name "-.LSTARTCIE_" #name "\n" \
135 ".LSTARTCIE_" #name ":\n" \
136 " .long 0\n" /* CIE ID */ \
137 " .byte 1\n" /* Version number */ \
138 " .string \"zRS\"\n" /* NUL-terminated augmentation string */ \
139 " .uleb128 1\n" /* Code alignment factor */ \
140 " .sleb128 -8\n" /* Data alignment factor */ \
141 " .uleb128 16\n" /* Return address register column (rip) */ \
142 /* Augmentation value length */ \
143 " .uleb128 .LENDAUGMNT_" #name "-.LSTARTAUGMNT_" #name "\n" \
144 ".LSTARTAUGMNT_" #name ":\n" \
145 " .byte 0x1b\n" /* DW_EH_PE_pcrel|DW_EH_PE_sdata4. */ \
146 ".LENDAUGMNT_" #name ":\n" \
147 " .align " LP_SIZE "\n" \
148 ".LENDCIE_" #name ":\n" \
149 " .long .LENDFDE_" #name "-.LSTARTFDE_" #name "\n" /* FDE len */ \
150 ".LSTARTFDE_" #name ":\n" \
151 " .long .LSTARTFDE_" #name "-.LSTARTFRAME_" #name "\n" /* CIE */ \
152 /* `LSTART_' is subtracted 1 as debuggers assume a `call' here. */ \
153 " .long (.LSTART_" #name "-1)-.\n" /* PC-relative start addr. */ \
154 " .long .LEND_" #name "-(.LSTART_" #name "-1)\n" \
155 " .uleb128 0\n" /* FDE augmentation length */ \
156 do_cfa_expr \
157 do_expr (8 /* r8 */, oR8) \
158 do_expr (9 /* r9 */, oR9) \
159 do_expr (10 /* r10 */, oR10) \
160 do_expr (11 /* r11 */, oR11) \
161 do_expr (12 /* r12 */, oR12) \
162 do_expr (13 /* r13 */, oR13) \
163 do_expr (14 /* r14 */, oR14) \
164 do_expr (15 /* r15 */, oR15) \
165 do_expr (5 /* rdi */, oRDI) \
166 do_expr (4 /* rsi */, oRSI) \
167 do_expr (6 /* rbp */, oRBP) \
168 do_expr (3 /* rbx */, oRBX) \
169 do_expr (1 /* rdx */, oRDX) \
170 do_expr (0 /* rax */, oRAX) \
171 do_expr (2 /* rcx */, oRCX) \
172 do_expr (7 /* rsp */, oRSP) \
173 do_expr (16 /* rip */, oRIP) \
174 /* libgcc-4.1.1 has only `DWARF_FRAME_REGISTERS == 17'. */ \
175 /* do_expr (49 |* rflags *|, oEFL) */ \
176 /* `cs'/`ds'/`fs' are unaligned and a different size. */ \
177 /* gas: Error: register save offset not a multiple of 8 */ \
178 " .align " LP_SIZE "\n" \
179 ".LENDFDE_" #name ":\n" \
180 " .previous\n" \
182 /* The return code for realtime-signals. */
183 RESTORE (restore_rt, __NR_rt_sigreturn)