1/* ELF program property for x86 ISA level.
2 Copyright (C) 2020-2022 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 In addition to the permissions in the GNU Lesser General Public
11 License, the Free Software Foundation gives you unlimited
12 permission to link the compiled version of this file with other
13 programs, and to distribute those programs without any restriction
14 coming from the use of this file. (The Lesser General Public
15 License restrictions do apply in other respects; for example, they
16 cover modification of the file, and distribution when not linked
17 into another program.)
18
19 The GNU C Library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
23
24 You should have received a copy of the GNU Lesser General Public
25 License along with the GNU C Library; if not, see
26 <https://www.gnu.org/licenses/>. */
27
28#include <elf.h>
29
30/* ELF program property for x86 ISA level. */
31#ifdef INCLUDE_X86_ISA_LEVEL
32# if defined __SSE__ && defined __SSE2__
33/* NB: ISAs, excluding MMX, in x86-64 ISA level baseline are used. */
34# define ISA_BASELINE GNU_PROPERTY_X86_ISA_1_BASELINE
35# else
36# define ISA_BASELINE 0
37# endif
38
39# if ISA_BASELINE && defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_16 \
40 && defined HAVE_X86_LAHF_SAHF && defined __POPCNT__ \
41 && defined __SSE3__ && defined __SSSE3__ && defined __SSE4_1__ \
42 && defined __SSE4_2__
43/* NB: ISAs in x86-64 ISA level v2 are used. */
44# define ISA_V2 GNU_PROPERTY_X86_ISA_1_V2
45# else
46# define ISA_V2 0
47# endif
48
49# if ISA_V2 && defined __AVX__ && defined __AVX2__ && defined __F16C__ \
50 && defined __FMA__ && defined __LZCNT__ && defined HAVE_X86_MOVBE
51/* NB: ISAs in x86-64 ISA level v3 are used. */
52# define ISA_V3 GNU_PROPERTY_X86_ISA_1_V3
53# else
54# define ISA_V3 0
55# endif
56
57# if ISA_V3 && defined __AVX512F__ && defined __AVX512BW__ \
58 && defined __AVX512CD__ && defined __AVX512DQ__ \
59 && defined __AVX512VL__
60/* NB: ISAs in x86-64 ISA level v4 are used. */
61# define ISA_V4 GNU_PROPERTY_X86_ISA_1_V4
62# else
63# define ISA_V4 0
64# endif
65
66# ifndef ISA_LEVEL
67# define ISA_LEVEL (ISA_BASELINE | ISA_V2 | ISA_V3 | ISA_V4)
68# endif
69
70# if ISA_LEVEL
71# ifdef __LP64__
72# define PROPERTY_ALIGN 3
73# else
74# define PROPERTY_ALIGN 2
75# endif
76
77# define note_stringify(arg) note_stringify_1(arg)
78# define note_stringify_1(arg) #arg
79
80asm(".pushsection \".note.gnu.property\",\"a\",@note\n"
81" .p2align " note_stringify (PROPERTY_ALIGN)
82 /* name length. */
83"\n .long 1f - 0f\n"
84 /* data length. */
85" .long 4f - 1f\n"
86 /* note type: NT_GNU_PROPERTY_TYPE_0. */
87" .long " note_stringify (NT_GNU_PROPERTY_TYPE_0)
88 /* vendor name. */
89"\n0: .asciz \"GNU\"\n"
90"1: .p2align " note_stringify (PROPERTY_ALIGN)
91 /* pr_type: GNU_PROPERTY_X86_ISA_1_NEEDED. */
92"\n .long " note_stringify (GNU_PROPERTY_X86_ISA_1_NEEDED)
93 /* pr_datasz. */
94"\n .long 3f - 2f\n"
95 /* GNU_PROPERTY_X86_ISA_1_V[234]. */
96"2:\n .long " note_stringify (ISA_LEVEL)
97"\n3:\n .p2align " note_stringify (PROPERTY_ALIGN)
98"\n4:\n .popsection");
99# endif /* ISA_LEVEL */
100#endif /* INCLUDE_X86_ISA_LEVEL */
101