elf: Simplify output of hwcap subdirectories in ld.so help
[glibc.git] / libio / vtables.c
blob50acab7f216e59da3ec93f08c72fe40883b2c7ba
1 /* libio vtable validation.
2 Copyright (C) 2016-2022 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 #include <dlfcn.h>
20 #include <libioP.h>
21 #include <stdio.h>
22 #include <ldsodefs.h>
24 #ifdef SHARED
26 void (*IO_accept_foreign_vtables) (void) attribute_hidden;
28 #else /* !SHARED */
30 /* Used to check whether static dlopen support is needed. */
31 # pragma weak __dlopen
33 #endif
35 void attribute_hidden
36 _IO_vtable_check (void)
38 #ifdef SHARED
39 /* Honor the compatibility flag. */
40 void (*flag) (void) = atomic_load_relaxed (&IO_accept_foreign_vtables);
41 #ifdef PTR_DEMANGLE
42 PTR_DEMANGLE (flag);
43 #endif
44 if (flag == &_IO_vtable_check)
45 return;
47 /* In case this libc copy is in a non-default namespace, we always
48 need to accept foreign vtables because there is always a
49 possibility that FILE * objects are passed across the linking
50 boundary. */
52 Dl_info di;
53 struct link_map *l;
54 if (!rtld_active ()
55 || (_dl_addr (_IO_vtable_check, &di, &l, NULL) != 0
56 && l->l_ns != LM_ID_BASE))
57 return;
60 #else /* !SHARED */
61 /* We cannot perform vtable validation in the static dlopen case
62 because FILE * handles might be passed back and forth across the
63 boundary. Therefore, we disable checking in this case. */
64 if (__dlopen != NULL)
65 return;
66 #endif
68 __libc_fatal ("Fatal error: glibc detected an invalid stdio handle\n");
71 /* Some variants of libstdc++ interpose _IO_2_1_stdin_ etc. and
72 install their own vtables directly, without calling _IO_init or
73 other functions. Detect this by looking at the vtables values
74 during startup, and disable vtable validation in this case. */
75 #ifdef SHARED
76 __attribute__ ((constructor))
77 static void
78 check_stdfiles_vtables (void)
80 if (_IO_2_1_stdin_.vtable != &_IO_file_jumps
81 || _IO_2_1_stdout_.vtable != &_IO_file_jumps
82 || _IO_2_1_stderr_.vtable != &_IO_file_jumps)
83 IO_set_accept_foreign_vtables (&_IO_vtable_check);
85 #endif