locale: Fix localedata/sort-test undefined behavior
[glibc.git] / elf / setup-vdso.h
blob3f20578046de76ed1b91ec10bc76c2e489f3179b
1 /* Set up the data structures for the system-supplied DSO.
2 Copyright (C) 2012-2021 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 static inline void __attribute__ ((always_inline))
20 setup_vdso (struct link_map *main_map __attribute__ ((unused)),
21 struct link_map ***first_preload __attribute__ ((unused)))
23 #ifdef NEED_DL_SYSINFO_DSO
24 if (GLRO(dl_sysinfo_dso) == NULL)
25 return;
27 /* Do an abridged version of the work _dl_map_object_from_fd would do
28 to map in the object. It's already mapped and prelinked (and
29 better be, since it's read-only and so we couldn't relocate it).
30 We just want our data structures to describe it as if we had just
31 mapped and relocated it normally. */
32 struct link_map *l = _dl_new_object ((char *) "", "", lt_library, NULL,
33 0, LM_ID_BASE);
34 if (__glibc_likely (l != NULL))
36 l->l_phdr = ((const void *) GLRO(dl_sysinfo_dso)
37 + GLRO(dl_sysinfo_dso)->e_phoff);
38 l->l_phnum = GLRO(dl_sysinfo_dso)->e_phnum;
39 for (uint_fast16_t i = 0; i < l->l_phnum; ++i)
41 const ElfW(Phdr) *const ph = &l->l_phdr[i];
42 if (ph->p_type == PT_DYNAMIC)
44 l->l_ld = (void *) ph->p_vaddr;
45 l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
46 l->l_ld_readonly = (ph->p_flags & PF_W) == 0;
48 else if (ph->p_type == PT_LOAD)
50 if (! l->l_addr)
51 l->l_addr = ph->p_vaddr;
52 if (ph->p_vaddr + ph->p_memsz >= l->l_map_end)
53 l->l_map_end = ph->p_vaddr + ph->p_memsz;
54 if ((ph->p_flags & PF_X)
55 && ph->p_vaddr + ph->p_memsz >= l->l_text_end)
56 l->l_text_end = ph->p_vaddr + ph->p_memsz;
58 else
59 /* There must be no TLS segment. */
60 assert (ph->p_type != PT_TLS);
62 l->l_map_start = (ElfW(Addr)) GLRO(dl_sysinfo_dso);
63 l->l_addr = l->l_map_start - l->l_addr;
64 l->l_map_end += l->l_addr;
65 l->l_text_end += l->l_addr;
66 l->l_ld = (void *) ((ElfW(Addr)) l->l_ld + l->l_addr);
67 elf_get_dynamic_info (l, false, false);
68 _dl_setup_hash (l);
69 l->l_relocated = 1;
71 /* The vDSO is always used. */
72 l->l_used = 1;
74 /* Initialize l_local_scope to contain just this map. This allows
75 the use of dl_lookup_symbol_x to resolve symbols within the vdso.
76 So we create a single entry list pointing to l_real as its only
77 element */
78 l->l_local_scope[0]->r_nlist = 1;
79 l->l_local_scope[0]->r_list = &l->l_real;
81 /* Now that we have the info handy, use the DSO image's soname
82 so this object can be looked up by name. */
83 if (l->l_info[DT_SONAME] != NULL)
85 char *dsoname = ((char *) D_PTR (l, l_info[DT_STRTAB])
86 + l->l_info[DT_SONAME]->d_un.d_val);
87 l->l_libname->name = dsoname;
88 l->l_name = dsoname;
91 /* Add the vDSO to the object list. */
92 _dl_add_to_namespace_list (l, LM_ID_BASE);
94 # if IS_IN (rtld)
95 /* Rearrange the list so this DSO appears after rtld_map. */
96 assert (l->l_next == NULL);
97 assert (l->l_prev == main_map);
98 GL(dl_rtld_map).l_next = l;
99 l->l_prev = &GL(dl_rtld_map);
100 *first_preload = &l->l_next;
101 # else
102 GL(dl_nns) = 1;
103 # endif
105 /* We have a prelinked DSO preloaded by the system. */
106 GLRO(dl_sysinfo_map) = l;
107 # ifdef NEED_DL_SYSINFO
108 if (GLRO(dl_sysinfo) == DL_SYSINFO_DEFAULT)
109 GLRO(dl_sysinfo) = GLRO(dl_sysinfo_dso)->e_entry + l->l_addr;
110 # endif
112 #endif