Add GLRO(dl_hwcap2), conditional on per platform HWCAP2_AVAIL.
[glibc.git] / elf / dl-support.c
blob94d35e94e22c33ca14ab32e54a9560b4f59a7d6e
1 /* Support for dynamic linking code in static libc.
2 Copyright (C) 1996-2013 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 <http://www.gnu.org/licenses/>. */
19 /* This file defines some things that for the dynamic linker are defined in
20 rtld.c and dl-sysdep.c in ways appropriate to bootstrap dynamic linking. */
22 #include <errno.h>
23 #include <libintl.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/param.h>
27 #include <stdint.h>
28 #include <ldsodefs.h>
29 #include <dl-machine.h>
30 #include <bits/libc-lock.h>
31 #include <dl-cache.h>
32 #include <dl-librecon.h>
33 #include <dl-procinfo.h>
34 #include <dl-hwcap2.h>
35 #include <unsecvars.h>
36 #include <hp-timing.h>
37 #include <stackinfo.h>
39 extern char *__progname;
40 char **_dl_argv = &__progname; /* This is checked for some error messages. */
42 /* Name of the architecture. */
43 const char *_dl_platform;
44 size_t _dl_platformlen;
46 int _dl_debug_mask;
47 int _dl_lazy;
48 ElfW(Addr) _dl_use_load_bias = -2;
49 int _dl_dynamic_weak;
51 /* If nonzero print warnings about problematic situations. */
52 int _dl_verbose;
54 /* We never do profiling. */
55 const char *_dl_profile;
56 const char *_dl_profile_output;
58 /* Names of shared object for which the RUNPATHs and RPATHs should be
59 ignored. */
60 const char *_dl_inhibit_rpath;
62 /* The map for the object we will profile. */
63 struct link_map *_dl_profile_map;
65 /* This is the address of the last stack address ever used. */
66 void *__libc_stack_end;
68 /* Path where the binary is found. */
69 const char *_dl_origin_path;
71 /* Nonzero if runtime lookup should not update the .got/.plt. */
72 int _dl_bind_not;
74 /* Namespace information. */
75 struct link_namespaces _dl_ns[DL_NNS];
76 size_t _dl_nns;
78 /* Incremented whenever something may have been added to dl_loaded. */
79 unsigned long long _dl_load_adds;
81 /* Fake scope. In dynamically linked binaries this is the scope of the
82 main application but here we don't have something like this. So
83 create a fake scope containing nothing. */
84 struct r_scope_elem _dl_initial_searchlist;
86 #ifndef HAVE_INLINED_SYSCALLS
87 /* Nonzero during startup. */
88 int _dl_starting_up = 1;
89 #endif
91 /* Random data provided by the kernel. */
92 void *_dl_random;
94 /* Get architecture specific initializer. */
95 #include <dl-procinfo.c>
97 /* We expect less than a second for relocation. */
98 #ifdef HP_SMALL_TIMING_AVAIL
99 # undef HP_TIMING_AVAIL
100 # define HP_TIMING_AVAIL HP_SMALL_TIMING_AVAIL
101 #endif
103 /* Initial value of the CPU clock. */
104 #ifndef HP_TIMING_NONAVAIL
105 hp_timing_t _dl_cpuclock_offset;
106 #endif
108 void (*_dl_init_static_tls) (struct link_map *) = &_dl_nothread_init_static_tls;
110 size_t _dl_pagesize = EXEC_PAGESIZE;
112 int _dl_inhibit_cache;
114 unsigned int _dl_osversion;
116 /* All known directories in sorted order. */
117 struct r_search_path_elem *_dl_all_dirs;
119 /* All directories after startup. */
120 struct r_search_path_elem *_dl_init_all_dirs;
122 /* The object to be initialized first. */
123 struct link_map *_dl_initfirst;
125 /* Descriptor to write debug messages to. */
126 int _dl_debug_fd = STDERR_FILENO;
128 int _dl_correct_cache_id = _DL_CACHE_DEFAULT_ID;
130 ElfW(auxv_t) *_dl_auxv;
131 const ElfW(Phdr) *_dl_phdr;
132 size_t _dl_phnum;
133 uint64_t _dl_hwcap __attribute__ ((nocommon));
135 #if HWCAP2_AVAIL
136 uint64_t _dl_hwcap2 __attribute__ ((nocommon));
137 #endif
139 /* This is not initialized to HWCAP_IMPORTANT, matching the definition
140 of _dl_important_hwcaps, below, where no hwcap strings are ever
141 used. This mask is still used to mediate the lookups in the cache
142 file. Since there is no way to set this nonzero (we don't grok the
143 LD_HWCAP_MASK environment variable here), there is no real point in
144 setting _dl_hwcap nonzero below, but we do anyway. */
145 uint64_t _dl_hwcap_mask __attribute__ ((nocommon));
147 /* Prevailing state of the stack. Generally this includes PF_X, indicating it's
148 * executable but this isn't true for all platforms. */
149 ElfW(Word) _dl_stack_flags = DEFAULT_STACK_PERMS;
151 /* If loading a shared object requires that we make the stack executable
152 when it was not, we do it by calling this function.
153 It returns an errno code or zero on success. */
154 int (*_dl_make_stack_executable_hook) (void **) internal_function
155 = _dl_make_stack_executable;
158 /* Function in libpthread to wait for termination of lookups. */
159 void (*_dl_wait_lookup_done) (void);
161 struct dl_scope_free_list *_dl_scope_free_list;
163 #ifdef NEED_DL_SYSINFO
164 /* Needed for improved syscall handling on at least x86/Linux. */
165 uintptr_t _dl_sysinfo = DL_SYSINFO_DEFAULT;
166 #endif
167 #ifdef NEED_DL_SYSINFO_DSO
168 /* Address of the ELF headers in the vsyscall page. */
169 const ElfW(Ehdr) *_dl_sysinfo_dso;
171 struct link_map *_dl_sysinfo_map;
173 # include "get-dynamic-info.h"
174 #endif
175 #include "setup-vdso.h"
177 /* During the program run we must not modify the global data of
178 loaded shared object simultanously in two threads. Therefore we
179 protect `_dl_open' and `_dl_close' in dl-close.c.
181 This must be a recursive lock since the initializer function of
182 the loaded object might as well require a call to this function.
183 At this time it is not anymore a problem to modify the tables. */
184 __rtld_lock_define_initialized_recursive (, _dl_load_lock)
185 /* This lock is used to keep __dl_iterate_phdr from inspecting the
186 list of loaded objects while an object is added to or removed from
187 that list. */
188 __rtld_lock_define_initialized_recursive (, _dl_load_write_lock)
191 #ifdef HAVE_AUX_VECTOR
192 int _dl_clktck;
194 void
195 internal_function
196 _dl_aux_init (ElfW(auxv_t) *av)
198 int seen = 0;
199 uid_t uid = 0;
200 gid_t gid = 0;
202 _dl_auxv = av;
203 for (; av->a_type != AT_NULL; ++av)
204 switch (av->a_type)
206 case AT_PAGESZ:
207 if (av->a_un.a_val != 0)
208 GLRO(dl_pagesize) = av->a_un.a_val;
209 break;
210 case AT_CLKTCK:
211 GLRO(dl_clktck) = av->a_un.a_val;
212 break;
213 case AT_PHDR:
214 GL(dl_phdr) = (const void *) av->a_un.a_val;
215 break;
216 case AT_PHNUM:
217 GL(dl_phnum) = av->a_un.a_val;
218 break;
219 case AT_HWCAP:
220 GLRO(dl_hwcap) = (unsigned long int) av->a_un.a_val;
221 break;
222 #if HWCAP2_AVAIL
223 case AT_HWCAP2:
224 GLRO(dl_hwcap2) = (unsigned long int) av->a_un.a_val;
225 break;
226 #endif
227 #ifdef NEED_DL_SYSINFO
228 case AT_SYSINFO:
229 GL(dl_sysinfo) = av->a_un.a_val;
230 break;
231 #endif
232 #ifdef NEED_DL_SYSINFO_DSO
233 case AT_SYSINFO_EHDR:
234 GL(dl_sysinfo_dso) = (void *) av->a_un.a_val;
235 break;
236 #endif
237 case AT_UID:
238 uid ^= av->a_un.a_val;
239 seen |= 1;
240 break;
241 case AT_EUID:
242 uid ^= av->a_un.a_val;
243 seen |= 2;
244 break;
245 case AT_GID:
246 gid ^= av->a_un.a_val;
247 seen |= 4;
248 break;
249 case AT_EGID:
250 gid ^= av->a_un.a_val;
251 seen |= 8;
252 break;
253 case AT_SECURE:
254 seen = -1;
255 __libc_enable_secure = av->a_un.a_val;
256 __libc_enable_secure_decided = 1;
257 break;
258 case AT_RANDOM:
259 _dl_random = (void *) av->a_un.a_val;
260 break;
261 # ifdef DL_PLATFORM_AUXV
262 DL_PLATFORM_AUXV
263 # endif
265 if (seen == 0xf)
267 __libc_enable_secure = uid != 0 || gid != 0;
268 __libc_enable_secure_decided = 1;
271 #endif
274 void
275 internal_function
276 _dl_non_dynamic_init (void)
278 if (HP_TIMING_AVAIL)
279 HP_TIMING_NOW (_dl_cpuclock_offset);
281 _dl_verbose = *(getenv ("LD_WARN") ?: "") == '\0' ? 0 : 1;
283 /* Set up the data structures for the system-supplied DSO early,
284 so they can influence _dl_init_paths. */
285 setup_vdso (NULL, NULL);
287 /* Initialize the data structures for the search paths for shared
288 objects. */
289 _dl_init_paths (getenv ("LD_LIBRARY_PATH"));
291 /* Remember the last search directory added at startup. */
292 _dl_init_all_dirs = GL(dl_all_dirs);
294 _dl_lazy = *(getenv ("LD_BIND_NOW") ?: "") == '\0';
296 _dl_bind_not = *(getenv ("LD_BIND_NOT") ?: "") != '\0';
298 _dl_dynamic_weak = *(getenv ("LD_DYNAMIC_WEAK") ?: "") == '\0';
300 _dl_profile_output = getenv ("LD_PROFILE_OUTPUT");
301 if (_dl_profile_output == NULL || _dl_profile_output[0] == '\0')
302 _dl_profile_output
303 = &"/var/tmp\0/var/profile"[__libc_enable_secure ? 9 : 0];
305 if (__libc_enable_secure)
307 static const char unsecure_envvars[] =
308 UNSECURE_ENVVARS
309 #ifdef EXTRA_UNSECURE_ENVVARS
310 EXTRA_UNSECURE_ENVVARS
311 #endif
313 const char *cp = unsecure_envvars;
315 while (cp < unsecure_envvars + sizeof (unsecure_envvars))
317 __unsetenv (cp);
318 cp = (const char *) __rawmemchr (cp, '\0') + 1;
321 if (__access ("/etc/suid-debug", F_OK) != 0)
322 __unsetenv ("MALLOC_CHECK_");
325 #ifdef DL_PLATFORM_INIT
326 DL_PLATFORM_INIT;
327 #endif
329 #ifdef DL_OSVERSION_INIT
330 DL_OSVERSION_INIT;
331 #endif
333 /* Now determine the length of the platform string. */
334 if (_dl_platform != NULL)
335 _dl_platformlen = strlen (_dl_platform);
337 /* Scan for a program header telling us the stack is nonexecutable. */
338 if (_dl_phdr != NULL)
339 for (uint_fast16_t i = 0; i < _dl_phnum; ++i)
340 if (_dl_phdr[i].p_type == PT_GNU_STACK)
342 _dl_stack_flags = _dl_phdr[i].p_flags;
343 break;
347 #ifdef DL_SYSINFO_IMPLEMENTATION
348 DL_SYSINFO_IMPLEMENTATION
349 #endif