2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / x86_64 / sigaction.c
blobab1012385839a068dacc9d8862a160dc83999ce8
1 /* POSIX.1 `sigaction' call for Linux/x86-64.
2 Copyright (C) 2001, 2002, 2003, 2005, 2006 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <sysdep.h>
21 #include <errno.h>
22 #include <stddef.h>
23 #include <signal.h>
24 #include <string.h>
26 #include <sysdep.h>
27 #include <sys/syscall.h>
29 #include <kernel-features.h>
31 /* The difference here is that the sigaction structure used in the
32 kernel is not the same as we use in the libc. Therefore we must
33 translate it here. */
34 #include <kernel_sigaction.h>
36 #include "ucontext_i.h"
38 /* We do not globally define the SA_RESTORER flag so do it here. */
39 #define SA_RESTORER 0x04000000
41 /* Using the hidden attribute here does not change the code but it
42 helps to avoid warnings. */
43 extern void restore_rt (void) asm ("__restore_rt") attribute_hidden;
46 /* If ACT is not NULL, change the action for SIG to *ACT.
47 If OACT is not NULL, put the old action for SIG in *OACT. */
48 int
49 __libc_sigaction (int sig, const struct sigaction *act, struct sigaction *oact)
51 int result;
52 struct kernel_sigaction kact, koact;
54 if (act)
56 kact.k_sa_handler = act->sa_handler;
57 memcpy (&kact.sa_mask, &act->sa_mask, sizeof (sigset_t));
58 kact.sa_flags = act->sa_flags | SA_RESTORER;
60 kact.sa_restorer = &restore_rt;
63 /* XXX The size argument hopefully will have to be changed to the
64 real size of the user-level sigset_t. */
65 result = INLINE_SYSCALL (rt_sigaction, 4,
66 sig, act ? __ptrvalue (&kact) : NULL,
67 oact ? __ptrvalue (&koact) : NULL, _NSIG / 8);
68 if (oact && result >= 0)
70 oact->sa_handler = koact.k_sa_handler;
71 memcpy (&oact->sa_mask, &koact.sa_mask, sizeof (sigset_t));
72 oact->sa_flags = koact.sa_flags;
73 oact->sa_restorer = koact.sa_restorer;
75 return result;
77 libc_hidden_def (__libc_sigaction)
79 #ifdef WRAPPER_INCLUDE
80 # include WRAPPER_INCLUDE
81 #endif
83 #ifndef LIBC_SIGACTION
84 weak_alias (__libc_sigaction, __sigaction)
85 libc_hidden_weak (__sigaction)
86 weak_alias (__libc_sigaction, sigaction)
87 #endif
89 /* NOTE: Please think twice before making any changes to the bits of
90 code below. GDB needs some intimate knowledge about it to
91 recognize them as signal trampolines, and make backtraces through
92 signal handlers work right. Important are both the names
93 (__restore_rt) and the exact instruction sequence.
94 If you ever feel the need to make any changes, please notify the
95 appropriate GDB maintainer.
97 The unwind information starts a byte before __restore_rt, so that
98 it is found when unwinding, to get an address the unwinder assumes
99 will be in the middle of a call instruction. See the Linux kernel
100 (the i386 vsyscall, in particular) for an explanation of the complex
101 unwind information used here in order to get the traditional CFA.
102 We do not restore cs - it's only stored as two bytes here so that's
103 a bit tricky. We don't use the gas cfi directives, so that we can
104 reliably add .cfi_signal_frame. */
106 #define do_cfa_expr \
107 " .byte 0x0f\n" /* DW_CFA_def_cfa_expression */ \
108 " .uleb128 2f-1f\n" /* length */ \
109 "1: .byte 0x77\n" /* DW_OP_breg7 */ \
110 " .sleb128 " CFI_STRINGIFY (oRSP) "\n" \
111 " .byte 0x06\n" /* DW_OP_deref */ \
112 "2:"
114 #define do_expr(regno, offset) \
115 " .byte 0x10\n" /* DW_CFA_expression */ \
116 " .uleb128 " CFI_STRINGIFY (regno) "\n" \
117 " .uleb128 2f-1f\n" /* length */ \
118 "1: .byte 0x77\n" /* DW_OP_breg7 */ \
119 " .sleb128 " CFI_STRINGIFY (offset) "\n" \
120 "2:"
122 #define RESTORE(name, syscall) RESTORE2 (name, syscall)
123 # define RESTORE2(name, syscall) \
124 asm \
126 /* `nop' for debuggers assuming `call' should not disalign the code. */ \
127 " nop\n" \
128 ".align 16\n" \
129 ".LSTART_" #name ":\n" \
130 " .type __" #name ",@function\n" \
131 "__" #name ":\n" \
132 " movq $" #syscall ", %rax\n" \
133 " syscall\n" \
134 ".LEND_" #name ":\n" \
135 ".section .eh_frame,\"a\",@progbits\n" \
136 ".LSTARTFRAME_" #name ":\n" \
137 " .long .LENDCIE_" #name "-.LSTARTCIE_" #name "\n" \
138 ".LSTARTCIE_" #name ":\n" \
139 " .long 0\n" /* CIE ID */ \
140 " .byte 1\n" /* Version number */ \
141 " .string \"zRS\"\n" /* NUL-terminated augmentation string */ \
142 " .uleb128 1\n" /* Code alignment factor */ \
143 " .sleb128 -8\n" /* Data alignment factor */ \
144 " .uleb128 16\n" /* Return address register column (rip) */ \
145 /* Augmentation value length */ \
146 " .uleb128 .LENDAUGMNT_" #name "-.LSTARTAUGMNT_" #name "\n" \
147 ".LSTARTAUGMNT_" #name ":\n" \
148 " .byte 0x1b\n" /* DW_EH_PE_pcrel|DW_EH_PE_sdata4. */ \
149 ".LENDAUGMNT_" #name ":\n" \
150 " .align 8\n" \
151 ".LENDCIE_" #name ":\n" \
152 " .long .LENDFDE_" #name "-.LSTARTFDE_" #name "\n" /* FDE len */ \
153 ".LSTARTFDE_" #name ":\n" \
154 " .long .LSTARTFDE_" #name "-.LSTARTFRAME_" #name "\n" /* CIE */ \
155 /* `LSTART_' is subtracted 1 as debuggers assume a `call' here. */ \
156 " .long (.LSTART_" #name "-1)-.\n" /* PC-relative start addr. */ \
157 " .long .LEND_" #name "-(.LSTART_" #name "-1)\n" \
158 " .uleb128 0\n" /* FDE augmentation length */ \
159 do_cfa_expr \
160 do_expr (8 /* r8 */, oR8) \
161 do_expr (9 /* r9 */, oR9) \
162 do_expr (10 /* r10 */, oR10) \
163 do_expr (11 /* r11 */, oR11) \
164 do_expr (12 /* r12 */, oR12) \
165 do_expr (13 /* r13 */, oR13) \
166 do_expr (14 /* r14 */, oR14) \
167 do_expr (15 /* r15 */, oR15) \
168 do_expr (5 /* rdi */, oRDI) \
169 do_expr (4 /* rsi */, oRSI) \
170 do_expr (6 /* rbp */, oRBP) \
171 do_expr (3 /* rbx */, oRBX) \
172 do_expr (1 /* rdx */, oRDX) \
173 do_expr (0 /* rax */, oRAX) \
174 do_expr (2 /* rcx */, oRCX) \
175 do_expr (7 /* rsp */, oRSP) \
176 do_expr (16 /* rip */, oRIP) \
177 /* libgcc-4.1.1 has only `DWARF_FRAME_REGISTERS == 17'. */ \
178 /* do_expr (49 |* rflags *|, oEFL) */ \
179 /* `cs'/`ds'/`fs' are unaligned and a different size. */ \
180 /* gas: Error: register save offset not a multiple of 8 */ \
181 " .align 8\n" \
182 ".LENDFDE_" #name ":\n" \
183 " .previous\n" \
185 /* The return code for realtime-signals. */
186 RESTORE (restore_rt, __NR_rt_sigreturn)