]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/aarch64/dl-bti.c
aarch64: enable BTI at runtime
[thirdparty/glibc.git] / sysdeps / aarch64 / dl-bti.c
CommitLineData
60533874
SD
1/* AArch64 BTI functions.
2 Copyright (C) 2020 Free Software Foundation, Inc.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18#include <unistd.h>
19#include <errno.h>
20#include <libintl.h>
21#include <ldsodefs.h>
22
23static int
24enable_bti (struct link_map *map, const char *program)
25{
26 const ElfW(Phdr) *phdr;
27 unsigned prot = PROT_READ | PROT_EXEC | PROT_BTI;
28
29 for (phdr = map->l_phdr; phdr < &map->l_phdr[map->l_phnum]; ++phdr)
30 if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_X))
31 {
32 void *start = (void *) (phdr->p_vaddr + map->l_addr);
33 size_t len = phdr->p_memsz;
34 if (__mprotect (start, len, prot) < 0)
35 {
36 if (program)
37 _dl_fatal_printf ("%s: mprotect failed to turn on BTI\n",
38 map->l_name);
39 else
40 _dl_signal_error (errno, map->l_name, "dlopen",
41 N_("mprotect failed to turn on BTI"));
42 }
43 }
44 return 0;
45}
46
47/* Enable BTI for L if required. */
48
49void
50_dl_bti_check (struct link_map *l, const char *program)
51{
52 if (GLRO(dl_aarch64_cpu_features).bti && l->l_mach.bti)
53 enable_bti (l, program);
54}