unistd: Improve fortify with clang
[glibc.git] / sysdeps / x86_64 / libc-start.h
blob63fef17029a9a1970e2e464816d5eda59062bf5d
1 /* x86-64 definitions for libc main startup.
2 Copyright (C) 2018-2024 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 <https://www.gnu.org/licenses/>. */
19 #ifndef SHARED
20 # define ARCH_SETUP_IREL() apply_irel ()
21 # define ARCH_APPLY_IREL()
22 # ifdef __CET__
23 /* Get CET features enabled in the static executable. */
25 static inline unsigned int
26 get_cet_feature (void)
28 /* Check if CET is supported and not disabled by tunables. */
29 const struct cpu_features *cpu_features = __get_cpu_features ();
30 unsigned int cet_feature = 0;
31 if (CPU_FEATURE_USABLE_P (cpu_features, IBT))
32 cet_feature |= GNU_PROPERTY_X86_FEATURE_1_IBT;
33 if (CPU_FEATURE_USABLE_P (cpu_features, SHSTK))
34 cet_feature |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
35 if (!cet_feature)
36 return cet_feature;
38 struct link_map *main_map = _dl_get_dl_main_map ();
40 /* Scan program headers backward to check PT_GNU_PROPERTY early for
41 x86 feature bits on static executable. */
42 const ElfW(Phdr) *phdr = GL(dl_phdr);
43 const ElfW(Phdr) *ph;
44 for (ph = phdr + GL(dl_phnum); ph != phdr; ph--)
45 if (ph[-1].p_type == PT_GNU_PROPERTY)
47 _dl_process_pt_gnu_property (main_map, -1, &ph[-1]);
48 /* Enable IBT and SHSTK only if they are enabled on static
49 executable. */
50 cet_feature &= (main_map->l_x86_feature_1_and
51 & (GNU_PROPERTY_X86_FEATURE_1_IBT
52 | GNU_PROPERTY_X86_FEATURE_1_SHSTK));
53 /* Set GL(dl_x86_feature_1) to the enabled CET features. */
54 GL(dl_x86_feature_1) = cet_feature;
55 break;
58 return cet_feature;
61 /* The function using this macro to enable shadow stack must not return
62 to avoid shadow stack underflow. */
63 # define ARCH_SETUP_TLS() \
64 { \
65 __libc_setup_tls (); \
67 unsigned int cet_feature = get_cet_feature (); \
68 ENABLE_X86_CET (cet_feature); \
69 _dl_cet_setup_features (cet_feature); \
71 # else
72 # define ARCH_SETUP_TLS() __libc_setup_tls ()
73 # endif
74 #endif /* !SHARED */